1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* System configuration routines
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1999-05-24 09:04:35 +00:00
|
|
|
* $Id: systems.c,v 1.44 1999/05/09 20:02:26 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
1997-11-09 17:51:27 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <pwd.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1999-05-08 11:07:56 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1998-06-15 19:06:25 +00:00
|
|
|
#include "defs.h"
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "log.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "systems.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-09 17:51:27 +00:00
|
|
|
#define issep(ch) ((ch) == ' ' || (ch) == '\t')
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
FILE *
|
1997-11-22 03:37:54 +00:00
|
|
|
OpenSecret(const char *file)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[100];
|
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
snprintf(line, sizeof line, "%s/%s", _PATH_PPP, file);
|
|
|
|
fp = ID0fopen(line, "r");
|
|
|
|
if (fp == NULL)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "OpenSecret: Can't open %s.\n", line);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (fp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-09 20:02:29 +00:00
|
|
|
CloseSecret(FILE *fp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
1997-11-09 17:51:27 +00:00
|
|
|
/* Move string from ``from'' to ``to'', interpreting ``~'' and $.... */
|
|
|
|
static void
|
|
|
|
InterpretArg(char *from, char *to)
|
|
|
|
{
|
|
|
|
const char *env;
|
|
|
|
char *ptr, *startto, *endto;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
startto = to;
|
|
|
|
endto = to + LINE_LEN - 1;
|
|
|
|
|
|
|
|
while(issep(*from))
|
|
|
|
from++;
|
|
|
|
if (*from == '~') {
|
|
|
|
ptr = strchr(++from, '/');
|
|
|
|
len = ptr ? ptr - from : strlen(from);
|
|
|
|
if (len == 0) {
|
|
|
|
if ((env = getenv("HOME")) == NULL)
|
|
|
|
env = _PATH_PPP;
|
|
|
|
strncpy(to, env, endto - to);
|
|
|
|
} else {
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
strncpy(to, from, len);
|
|
|
|
to[len] = '\0';
|
|
|
|
pwd = getpwnam(to);
|
|
|
|
if (pwd)
|
|
|
|
strncpy(to, pwd->pw_dir, endto-to);
|
|
|
|
else
|
|
|
|
strncpy(to, _PATH_PPP, endto - to);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
*endto = '\0';
|
|
|
|
to += strlen(to);
|
|
|
|
from += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (to < endto && *from != '\0') {
|
|
|
|
if (*from == '$') {
|
|
|
|
if (from[1] == '$') {
|
|
|
|
*to = '\0'; /* For an empty var name below */
|
|
|
|
from += 2;
|
|
|
|
} else if (from[1] == '{') {
|
|
|
|
ptr = strchr(from+2, '}');
|
|
|
|
if (ptr) {
|
|
|
|
len = ptr - from - 2;
|
|
|
|
if (endto - to < len )
|
|
|
|
len = endto - to;
|
|
|
|
if (len) {
|
|
|
|
strncpy(to, from+2, len);
|
|
|
|
to[len] = '\0';
|
|
|
|
from = ptr+1;
|
|
|
|
} else {
|
|
|
|
*to++ = *from++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*to++ = *from++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptr = to;
|
|
|
|
for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++)
|
|
|
|
*ptr++ = *from;
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
if (*to == '\0')
|
|
|
|
*to++ = '$';
|
|
|
|
else if ((env = getenv(to)) != NULL) {
|
|
|
|
strncpy(to, env, endto - to);
|
|
|
|
*endto = '\0';
|
|
|
|
to += strlen(to);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*to++ = *from++;
|
|
|
|
}
|
|
|
|
while (to > startto) {
|
|
|
|
to--;
|
|
|
|
if (!issep(*to)) {
|
|
|
|
to++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CTRL_UNKNOWN (0)
|
|
|
|
#define CTRL_INCLUDE (1)
|
|
|
|
|
|
|
|
static int
|
|
|
|
DecodeCtrlCommand(char *line, char *arg)
|
|
|
|
{
|
|
|
|
if (!strncasecmp(line, "include", 7) && issep(line[7])) {
|
|
|
|
InterpretArg(line+8, arg);
|
|
|
|
return CTRL_INCLUDE;
|
|
|
|
}
|
|
|
|
return CTRL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
/*
|
|
|
|
* Initialised in system_IsValid(), set in ReadSystem(),
|
|
|
|
* used by system_IsValid()
|
|
|
|
*/
|
1998-04-10 13:19:23 +00:00
|
|
|
static int modeok;
|
1997-11-11 22:58:14 +00:00
|
|
|
static int userok;
|
1998-04-10 13:19:23 +00:00
|
|
|
static int modereq;
|
1997-11-11 22:58:14 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1997-11-22 03:37:54 +00:00
|
|
|
AllowUsers(struct cmdargs const *arg)
|
1997-11-11 22:58:14 +00:00
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
/* arg->bundle may be NULL (see system_IsValid()) ! */
|
1997-11-11 22:58:14 +00:00
|
|
|
int f;
|
|
|
|
char *user;
|
|
|
|
|
|
|
|
userok = 0;
|
|
|
|
user = getlogin();
|
|
|
|
if (user && *user)
|
1998-04-14 23:17:24 +00:00
|
|
|
for (f = arg->argn; f < arg->argc; f++)
|
1997-11-22 03:37:54 +00:00
|
|
|
if (!strcmp("*", arg->argv[f]) || !strcmp(user, arg->argv[f])) {
|
1997-11-11 22:58:14 +00:00
|
|
|
userok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-11-22 03:37:54 +00:00
|
|
|
AllowModes(struct cmdargs const *arg)
|
1997-11-11 22:58:14 +00:00
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
/* arg->bundle may be NULL (see system_IsValid()) ! */
|
1998-05-15 23:58:30 +00:00
|
|
|
int f, mode, allowed;
|
1997-11-11 22:58:14 +00:00
|
|
|
|
|
|
|
allowed = 0;
|
1998-04-14 23:17:24 +00:00
|
|
|
for (f = arg->argn; f < arg->argc; f++) {
|
1998-05-15 23:58:30 +00:00
|
|
|
mode = Nam2mode(arg->argv[f]);
|
|
|
|
if (mode == PHYS_NONE || mode == PHYS_ALL)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "allow modes: %s: Invalid mode\n", arg->argv[f]);
|
1998-05-15 23:58:30 +00:00
|
|
|
else
|
|
|
|
allowed |= mode;
|
1997-11-11 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
modeok = modereq & allowed ? 1 : 0;
|
1997-11-11 22:58:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-02-04 01:03:36 +00:00
|
|
|
static char *
|
|
|
|
strip(char *line)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(line);
|
|
|
|
while (len && (line[len-1] == '\n' || line[len-1] == '\r' ||
|
|
|
|
issep(line[len-1])))
|
|
|
|
line[--len] = '\0';
|
|
|
|
|
|
|
|
while (issep(*line))
|
|
|
|
line++;
|
|
|
|
|
|
|
|
if (*line == '#')
|
|
|
|
*line = '\0';
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xgets(char *buf, int buflen, FILE *fp)
|
|
|
|
{
|
|
|
|
int len, n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (fgets(buf, buflen-1, fp)) {
|
|
|
|
n++;
|
|
|
|
buf[buflen-1] = '\0';
|
|
|
|
len = strlen(buf);
|
|
|
|
while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
|
|
|
|
buf[--len] = '\0';
|
|
|
|
if (len && buf[len-1] == '\\') {
|
|
|
|
buf += len - 1;
|
|
|
|
buflen -= len - 1;
|
|
|
|
if (!buflen) /* No buffer space */
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1999-02-02 09:35:30 +00:00
|
|
|
/* Values for ``how'' in ReadSystem */
|
|
|
|
#define SYSTEM_EXISTS 1
|
|
|
|
#define SYSTEM_VALIDATE 2
|
|
|
|
#define SYSTEM_EXEC 3
|
|
|
|
|
1999-03-09 20:39:03 +00:00
|
|
|
/* Returns -2 for ``file not found'' and -1 for ``label not found'' */
|
|
|
|
|
1997-11-11 22:58:14 +00:00
|
|
|
static int
|
1998-02-02 19:32:16 +00:00
|
|
|
ReadSystem(struct bundle *bundle, const char *name, const char *file,
|
1999-02-02 09:35:30 +00:00
|
|
|
struct prompt *prompt, struct datalink *cx, int how)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *cp, *wp;
|
1997-11-04 01:17:05 +00:00
|
|
|
int n, len;
|
1997-11-09 14:18:55 +00:00
|
|
|
char line[LINE_LEN];
|
1997-12-17 21:22:01 +00:00
|
|
|
char filename[MAXPATHLEN];
|
1997-05-10 01:22:19 +00:00
|
|
|
int linenum;
|
1997-11-11 22:58:14 +00:00
|
|
|
int argc;
|
1998-06-15 19:06:25 +00:00
|
|
|
char *argv[MAXARGS];
|
1997-11-11 22:58:14 +00:00
|
|
|
int allowcmd;
|
1998-02-04 01:03:36 +00:00
|
|
|
int indent;
|
|
|
|
char arg[LINE_LEN];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-09 17:51:27 +00:00
|
|
|
if (*file == '/')
|
|
|
|
snprintf(filename, sizeof filename, "%s", file);
|
|
|
|
else
|
|
|
|
snprintf(filename, sizeof filename, "%s/%s", _PATH_PPP, file);
|
1997-11-09 06:22:49 +00:00
|
|
|
fp = ID0fopen(filename, "r");
|
1995-01-31 06:29:58 +00:00
|
|
|
if (fp == NULL) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "ReadSystem: Can't open %s.\n", filename);
|
1999-03-09 20:39:03 +00:00
|
|
|
return -2;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "ReadSystem: Checking %s (%s).\n", name, filename);
|
1997-05-10 01:22:19 +00:00
|
|
|
|
|
|
|
linenum = 0;
|
1998-02-04 01:03:36 +00:00
|
|
|
while ((n = xgets(line, sizeof line, fp))) {
|
|
|
|
linenum += n;
|
|
|
|
if (issep(*line))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cp = strip(line);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
switch (*cp) {
|
1998-02-04 01:03:36 +00:00
|
|
|
case '\0': /* empty/comment */
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
1998-02-04 01:03:36 +00:00
|
|
|
|
|
|
|
case '!':
|
|
|
|
switch (DecodeCtrlCommand(cp+1, arg)) {
|
|
|
|
case CTRL_INCLUDE:
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCOMMAND, "%s: Including \"%s\"\n", filename, arg);
|
1999-02-02 09:35:30 +00:00
|
|
|
n = ReadSystem(bundle, name, arg, prompt, cx, how);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCOMMAND, "%s: Done include of \"%s\"\n", filename, arg);
|
1998-02-04 01:03:36 +00:00
|
|
|
if (!n)
|
|
|
|
return 0; /* got it */
|
|
|
|
break;
|
|
|
|
default:
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "%s: %s: Invalid command\n", filename, cp);
|
1998-02-04 01:03:36 +00:00
|
|
|
break;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
1998-02-04 01:03:36 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
default:
|
1998-02-04 01:03:36 +00:00
|
|
|
wp = strchr(cp, ':');
|
|
|
|
if (wp == NULL || wp[1] != '\0') {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Bad rule in %s (line %d) - missing colon.\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
filename, linenum);
|
1998-02-04 01:03:36 +00:00
|
|
|
continue;
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
*wp = '\0';
|
1998-02-04 01:03:36 +00:00
|
|
|
cp = strip(cp); /* lose any spaces between the label and the ':' */
|
|
|
|
|
|
|
|
if (strcmp(cp, name) == 0) {
|
|
|
|
/* We're in business */
|
1999-02-02 09:35:30 +00:00
|
|
|
if (how == SYSTEM_EXISTS)
|
|
|
|
return 0;
|
1998-02-04 01:03:36 +00:00
|
|
|
while ((n = xgets(line, sizeof line, fp))) {
|
|
|
|
linenum += n;
|
|
|
|
indent = issep(*line);
|
|
|
|
cp = strip(line);
|
|
|
|
|
|
|
|
if (*cp == '\0') /* empty / comment */
|
|
|
|
continue;
|
|
|
|
|
1998-10-17 12:28:03 +00:00
|
|
|
if (!indent) { /* start of next section */
|
1999-05-24 09:04:35 +00:00
|
|
|
if (*cp != '!') {
|
|
|
|
wp = strchr(cp, ':');
|
|
|
|
if ((how == SYSTEM_EXEC) && (wp == NULL || wp[1] != '\0'))
|
|
|
|
log_Printf(LogWARN, "Unindented command (%s line %d) -"
|
|
|
|
" ignored\n", filename, linenum);
|
|
|
|
}
|
1998-02-04 01:03:36 +00:00
|
|
|
break;
|
1998-10-17 12:28:03 +00:00
|
|
|
}
|
1998-02-04 01:03:36 +00:00
|
|
|
|
|
|
|
len = strlen(cp);
|
1998-06-15 19:06:25 +00:00
|
|
|
argc = command_Interpret(cp, len, argv);
|
|
|
|
allowcmd = argc > 0 && !strcasecmp(argv[0], "allow");
|
1999-02-02 09:35:30 +00:00
|
|
|
if ((!(how == SYSTEM_EXEC) && allowcmd) ||
|
|
|
|
((how == SYSTEM_EXEC) && !allowcmd))
|
1998-06-15 19:05:51 +00:00
|
|
|
command_Run(bundle, argc, (char const *const *)argv, prompt,
|
|
|
|
name, cx);
|
1997-11-09 17:51:27 +00:00
|
|
|
}
|
1998-02-04 01:03:36 +00:00
|
|
|
|
|
|
|
fclose(fp); /* everything read - get out */
|
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
1997-06-09 03:27:43 +00:00
|
|
|
return -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-10-31 17:38:47 +00:00
|
|
|
const char *
|
1998-05-01 19:26:12 +00:00
|
|
|
system_IsValid(const char *name, struct prompt *prompt, int mode)
|
1997-11-11 22:58:14 +00:00
|
|
|
{
|
1998-02-02 19:32:16 +00:00
|
|
|
/*
|
|
|
|
* Note: The ReadSystem() calls only result in calls to the Allow*
|
|
|
|
* functions. arg->bundle will be set to NULL for these commands !
|
|
|
|
*/
|
1999-03-09 20:39:03 +00:00
|
|
|
int def, how, rs;
|
1998-10-31 17:38:47 +00:00
|
|
|
|
|
|
|
def = !strcmp(name, "default");
|
1999-02-02 09:35:30 +00:00
|
|
|
how = ID0realuid() == 0 ? SYSTEM_EXISTS : SYSTEM_VALIDATE;
|
1997-11-11 22:58:14 +00:00
|
|
|
userok = 0;
|
|
|
|
modeok = 1;
|
1998-04-10 13:19:23 +00:00
|
|
|
modereq = mode;
|
1998-10-31 17:38:47 +00:00
|
|
|
|
1999-03-09 20:39:03 +00:00
|
|
|
rs = ReadSystem(NULL, "default", CONFFILE, prompt, NULL, how);
|
|
|
|
|
|
|
|
if (!def) {
|
|
|
|
if (rs == -1)
|
|
|
|
rs = 0; /* we don't care that ``default'' doesn't exist */
|
|
|
|
|
|
|
|
if (rs == 0)
|
|
|
|
rs = ReadSystem(NULL, name, CONFFILE, prompt, NULL, how);
|
1999-02-02 09:35:30 +00:00
|
|
|
|
1999-03-09 20:39:03 +00:00
|
|
|
if (rs == -1)
|
|
|
|
return "Configuration label not found";
|
|
|
|
|
|
|
|
if (rs == -2)
|
|
|
|
return _PATH_PPP "/" CONFFILE ": File not found";
|
|
|
|
}
|
1998-10-31 17:38:47 +00:00
|
|
|
|
1999-02-02 09:35:30 +00:00
|
|
|
if (how == SYSTEM_EXISTS)
|
|
|
|
userok = modeok = 1;
|
1998-10-31 17:38:47 +00:00
|
|
|
|
|
|
|
if (!userok)
|
1999-02-02 09:35:30 +00:00
|
|
|
return "User access denied";
|
1998-10-31 17:38:47 +00:00
|
|
|
|
|
|
|
if (!modeok)
|
1999-02-02 09:35:30 +00:00
|
|
|
return "Mode denied for this label";
|
1998-10-31 17:38:47 +00:00
|
|
|
|
|
|
|
return NULL;
|
1997-11-11 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
system_Select(struct bundle *bundle, const char *name, const char *file,
|
1998-06-15 19:05:51 +00:00
|
|
|
struct prompt *prompt, struct datalink *cx)
|
1997-11-11 22:58:14 +00:00
|
|
|
{
|
|
|
|
userok = modeok = 1;
|
1998-04-10 13:19:23 +00:00
|
|
|
modereq = PHYS_ALL;
|
1999-02-02 09:35:30 +00:00
|
|
|
return ReadSystem(bundle, name, file, prompt, cx, SYSTEM_EXEC);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|