systems.c revision 58045
1/*
2 *	          System configuration routines
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $FreeBSD: head/usr.sbin/ppp/systems.c 58045 2000-03-14 01:47:31Z brian $
21 *
22 *  TODO:
23 */
24#include <sys/param.h>
25
26#include <ctype.h>
27#include <pwd.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <termios.h>
32#include <unistd.h>
33
34#include "defs.h"
35#include "command.h"
36#include "log.h"
37#include "id.h"
38#include "systems.h"
39
40#define issep(ch) ((ch) == ' ' || (ch) == '\t')
41
42FILE *
43OpenSecret(const char *file)
44{
45  FILE *fp;
46  char line[100];
47
48  snprintf(line, sizeof line, "%s/%s", _PATH_PPP, file);
49  fp = ID0fopen(line, "r");
50  if (fp == NULL)
51    log_Printf(LogWARN, "OpenSecret: Can't open %s.\n", line);
52  return (fp);
53}
54
55void
56CloseSecret(FILE *fp)
57{
58  fclose(fp);
59}
60
61/* Move string from ``from'' to ``to'', interpreting ``~'' and $.... */
62const char *
63InterpretArg(const char *from, char *to)
64{
65  char *ptr, *startto, *endto;
66  struct passwd *pwd;
67  int len, instring;
68  const char *env;
69
70  instring = 0;
71  startto = to;
72  endto = to + LINE_LEN - 1;
73
74  while(issep(*from))
75    from++;
76
77  while (*from != '\0') {
78    switch (*from) {
79      case '"':
80        instring = !instring;
81        *to++ = *from++;
82        break;
83      case '\\':
84        switch (*++from) {
85          case '$':
86          case '~':
87            break;		/* Swallow the escapes */
88
89          default:
90            *to++ = '\\';	/* Pass the escapes on, maybe skipping \# */
91            break;
92        }
93        *to++ = *from++;
94        break;
95      case '$':
96        if (from[1] == '$') {
97          *to = '\0';	/* For an empty var name below */
98          from += 2;
99        } else if (from[1] == '{') {
100          ptr = strchr(from+2, '}');
101          if (ptr) {
102            len = ptr - from - 2;
103            if (endto - to < len )
104              len = endto - to;
105            if (len) {
106              strncpy(to, from+2, len);
107              to[len] = '\0';
108              from = ptr+1;
109            } else {
110              *to++ = *from++;
111              continue;
112            }
113          } else {
114            *to++ = *from++;
115            continue;
116          }
117        } else {
118          ptr = to;
119          for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++)
120            *ptr++ = *from;
121          *ptr = '\0';
122        }
123        if (*to == '\0')
124          *to++ = '$';
125        else if ((env = getenv(to)) != NULL) {
126          strncpy(to, env, endto - to);
127          *endto = '\0';
128          to += strlen(to);
129        }
130        break;
131
132      case '~':
133        ptr = strchr(++from, '/');
134        len = ptr ? ptr - from : strlen(from);
135        if (len == 0)
136          pwd = getpwuid(ID0realuid());
137        else {
138          strncpy(to, from, len);
139          to[len] = '\0';
140          pwd = getpwnam(to);
141        }
142        if (pwd == NULL)
143          *to++ = '~';
144        else {
145          strncpy(to, pwd->pw_dir, endto - to);
146          *endto = '\0';
147          to += strlen(to);
148          from += len;
149        }
150        endpwent();
151        break;
152
153      case '#':
154        if (!instring)
155          while (*from != '\0')
156            *to++ = *from++;
157        break;
158
159      default:
160        *to++ = *from++;
161        break;
162    }
163  }
164
165  while (to > startto) {
166    to--;
167    if (!issep(*to)) {
168      to++;
169      break;
170    }
171  }
172  *to = '\0';
173
174  return from;
175}
176
177#define CTRL_UNKNOWN (0)
178#define CTRL_INCLUDE (1)
179
180static int
181DecodeCtrlCommand(char *line, char *arg)
182{
183  const char *end;
184
185  if (!strncasecmp(line, "include", 7) && issep(line[7])) {
186    end = InterpretArg(line+8, arg);
187    if (*end && *end != '#')
188      log_Printf(LogWARN, "Usage: !include filename\n");
189    else
190      return CTRL_INCLUDE;
191  }
192  return CTRL_UNKNOWN;
193}
194
195/*
196 * Initialised in system_IsValid(), set in ReadSystem(),
197 * used by system_IsValid()
198 */
199static int modeok;
200static int userok;
201static int modereq;
202
203int
204AllowUsers(struct cmdargs const *arg)
205{
206  /* arg->bundle may be NULL (see system_IsValid()) ! */
207  int f;
208  struct passwd *pwd;
209
210  userok = 0;
211  pwd = getpwuid(ID0realuid());
212  if (pwd != NULL)
213    for (f = arg->argn; f < arg->argc; f++)
214      if (!strcmp("*", arg->argv[f]) || !strcmp(pwd->pw_name, arg->argv[f])) {
215        userok = 1;
216        break;
217      }
218  endpwent();
219
220  return 0;
221}
222
223int
224AllowModes(struct cmdargs const *arg)
225{
226  /* arg->bundle may be NULL (see system_IsValid()) ! */
227  int f, mode, allowed;
228
229  allowed = 0;
230  for (f = arg->argn; f < arg->argc; f++) {
231    mode = Nam2mode(arg->argv[f]);
232    if (mode == PHYS_NONE || mode == PHYS_ALL)
233      log_Printf(LogWARN, "allow modes: %s: Invalid mode\n", arg->argv[f]);
234    else
235      allowed |= mode;
236  }
237
238  modeok = modereq & allowed ? 1 : 0;
239  return 0;
240}
241
242static char *
243strip(char *line)
244{
245  int len;
246
247  len = strlen(line);
248  while (len && (line[len-1] == '\n' || line[len-1] == '\r' ||
249                 issep(line[len-1])))
250    line[--len] = '\0';
251
252  while (issep(*line))
253    line++;
254
255  if (*line == '#')
256    *line = '\0';
257
258  return line;
259}
260
261static int
262xgets(char *buf, int buflen, FILE *fp)
263{
264  int len, n;
265
266  n = 0;
267  while (fgets(buf, buflen-1, fp)) {
268    n++;
269    buf[buflen-1] = '\0';
270    len = strlen(buf);
271    while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
272      buf[--len] = '\0';
273    if (len && buf[len-1] == '\\') {
274      buf += len - 1;
275      buflen -= len - 1;
276      if (!buflen)        /* No buffer space */
277        break;
278    } else
279      break;
280  }
281  return n;
282}
283
284/* Values for ``how'' in ReadSystem */
285#define SYSTEM_EXISTS	1
286#define SYSTEM_VALIDATE	2
287#define SYSTEM_EXEC	3
288
289static char *
290GetLabel(char *line, const char *filename, int linenum)
291{
292  char *argv[MAXARGS];
293  int argc, len;
294
295  argc = MakeArgs(line, argv, MAXARGS, PARSE_REDUCE);
296
297  if (argc == 2 && !strcmp(argv[1], ":"))
298    return argv[0];
299
300  if (argc != 1 || (len = strlen(argv[0])) < 2 || argv[0][len-1] != ':') {
301      log_Printf(LogWARN, "Bad label in %s (line %d) - missing colon\n",
302                 filename, linenum);
303      return NULL;
304  }
305  argv[0][len-1] = '\0';	/* Lose the ':' */
306
307  return argv[0];
308}
309
310/* Returns -2 for ``file not found'' and -1 for ``label not found'' */
311
312static int
313ReadSystem(struct bundle *bundle, const char *name, const char *file,
314           struct prompt *prompt, struct datalink *cx, int how)
315{
316  FILE *fp;
317  char *cp;
318  int n, len;
319  char line[LINE_LEN];
320  char filename[MAXPATHLEN];
321  int linenum;
322  int argc;
323  char *argv[MAXARGS];
324  int allowcmd;
325  int indent;
326  char arg[LINE_LEN];
327  struct prompt *op;
328
329  if (*file == '/')
330    snprintf(filename, sizeof filename, "%s", file);
331  else
332    snprintf(filename, sizeof filename, "%s/%s", _PATH_PPP, file);
333  fp = ID0fopen(filename, "r");
334  if (fp == NULL) {
335    log_Printf(LogDEBUG, "ReadSystem: Can't open %s.\n", filename);
336    return -2;
337  }
338  log_Printf(LogDEBUG, "ReadSystem: Checking %s (%s).\n", name, filename);
339
340  linenum = 0;
341  while ((n = xgets(line, sizeof line, fp))) {
342    linenum += n;
343    if (issep(*line))
344      continue;
345
346    cp = strip(line);
347
348    switch (*cp) {
349    case '\0':			/* empty/comment */
350      break;
351
352    case '!':
353      switch (DecodeCtrlCommand(cp+1, arg)) {
354      case CTRL_INCLUDE:
355        log_Printf(LogCOMMAND, "%s: Including \"%s\"\n", filename, arg);
356        n = ReadSystem(bundle, name, arg, prompt, cx, how);
357        log_Printf(LogCOMMAND, "%s: Done include of \"%s\"\n", filename, arg);
358        if (!n)
359          return 0;	/* got it */
360        break;
361      default:
362        log_Printf(LogWARN, "%s: %s: Invalid command\n", filename, cp);
363        break;
364      }
365      break;
366
367    default:
368      if ((cp = GetLabel(cp, filename, linenum)) == NULL)
369        continue;
370
371      if (strcmp(cp, name) == 0) {
372        /* We're in business */
373        if (how == SYSTEM_EXISTS)
374	  return 0;
375	while ((n = xgets(line, sizeof line, fp))) {
376          linenum += n;
377          indent = issep(*line);
378          cp = strip(line);
379
380          if (*cp == '\0')			/* empty / comment */
381            continue;
382
383          if (!indent) {			/* start of next section */
384            if (*cp != '!' && how == SYSTEM_EXEC)
385              cp = GetLabel(cp, filename, linenum);
386            break;
387          }
388
389          len = strlen(cp);
390          if ((argc = command_Expand_Interpret(cp, len, argv, cp - line)) < 0)
391            log_Printf(LogWARN, "%s: %d: Syntax error\n", filename, linenum);
392          else {
393            allowcmd = argc > 0 && !strcasecmp(argv[0], "allow");
394            if ((how != SYSTEM_EXEC && allowcmd) ||
395                (how == SYSTEM_EXEC && !allowcmd)) {
396              /*
397               * Disable any context so that warnings are given to everyone,
398               * including syslog.
399               */
400              op = log_PromptContext;
401              log_PromptContext = NULL;
402	      command_Run(bundle, argc, (char const *const *)argv, prompt,
403                          name, cx);
404              log_PromptContext = op;
405            }
406          }
407        }
408
409	fclose(fp);  /* everything read - get out */
410	return 0;
411      }
412      break;
413    }
414  }
415  fclose(fp);
416  return -1;
417}
418
419const char *
420system_IsValid(const char *name, struct prompt *prompt, int mode)
421{
422  /*
423   * Note:  The ReadSystem() calls only result in calls to the Allow*
424   * functions.  arg->bundle will be set to NULL for these commands !
425   */
426  int def, how, rs;
427
428  def = !strcmp(name, "default");
429  how = ID0realuid() == 0 ? SYSTEM_EXISTS : SYSTEM_VALIDATE;
430  userok = 0;
431  modeok = 1;
432  modereq = mode;
433
434  rs = ReadSystem(NULL, "default", CONFFILE, prompt, NULL, how);
435
436  if (!def) {
437    if (rs == -1)
438      rs = 0;		/* we don't care that ``default'' doesn't exist */
439
440    if (rs == 0)
441      rs = ReadSystem(NULL, name, CONFFILE, prompt, NULL, how);
442
443    if (rs == -1)
444      return "Configuration label not found";
445
446    if (rs == -2)
447      return _PATH_PPP "/" CONFFILE ": File not found";
448  }
449
450  if (how == SYSTEM_EXISTS)
451    userok = modeok = 1;
452
453  if (!userok)
454    return "User access denied";
455
456  if (!modeok)
457    return "Mode denied for this label";
458
459  return NULL;
460}
461
462int
463system_Select(struct bundle *bundle, const char *name, const char *file,
464             struct prompt *prompt, struct datalink *cx)
465{
466  userok = modeok = 1;
467  modereq = PHYS_ALL;
468  return ReadSystem(bundle, name, file, prompt, cx, SYSTEM_EXEC);
469}
470