]> git.ozlabs.org Git - ppp.git/blob - pppd/options.c
The use of <net/ppp_defs.h> isn't guranteed to exist on Linux (e.g. uclibc, buildroot...
[ppp.git] / pppd / options.c
1 /*
2  * options.c - handles option processing for PPP.
3  *
4  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. The name "Carnegie Mellon University" must not be used to
19  *    endorse or promote products derived from this software without
20  *    prior written permission. For permission or any legal
21  *    details, please contact
22  *      Office of Technology Transfer
23  *      Carnegie Mellon University
24  *      5000 Forbes Avenue
25  *      Pittsburgh, PA  15213-3890
26  *      (412) 268-4387, fax: (412) 268-7395
27  *      tech-transfer@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <syslog.h>
55 #include <string.h>
56 #include <pwd.h>
57 #include <sys/param.h>
58 #include <net/if.h>
59 #ifdef PPP_WITH_PLUGINS
60 #include <dlfcn.h>
61 #endif
62
63 #ifdef PPP_WITH_FILTER
64 #include <pcap.h>
65 /*
66  * There have been 3 or 4 different names for this in libpcap CVS, but
67  * this seems to be what they have settled on...
68  * For older versions of libpcap, use DLT_PPP - but that means
69  * we lose the inbound and outbound qualifiers.
70  */
71 #ifndef DLT_PPP_PPPD
72 #ifdef DLT_PPP_WITHDIRECTION
73 #define DLT_PPP_PPPD    DLT_PPP_WITHDIRECTION
74 #else
75 #define DLT_PPP_PPPD    DLT_PPP
76 #endif
77 #endif
78 #endif /* PPP_WITH_FILTER */
79
80 #include "pppd.h"
81 #include "pathnames.h"
82
83 #if defined(ultrix) || defined(NeXT)
84 char *strdup(char *);
85 #endif
86
87
88 struct option_value {
89     struct option_value *next;
90     const char *source;
91     char value[1];
92 };
93
94 /*
95  * Option variables and default values.
96  */
97 int     debug = 0;              /* Debug flag */
98 int     kdebugflag = 0;         /* Tell kernel to print debug messages */
99 int     default_device = 1;     /* Using /dev/tty or equivalent */
100 char    devnam[MAXPATHLEN];     /* Device name */
101 bool    nodetach = 0;           /* Don't detach from controlling tty */
102 bool    updetach = 0;           /* Detach once link is up */
103 bool    master_detach;          /* Detach when we're (only) multilink master */
104 #ifdef SYSTEMD
105 bool    up_sdnotify = 0;        /* Notify systemd once link is up */
106 #endif
107 int     maxconnect = 0;         /* Maximum connect time */
108 char    user[MAXNAMELEN];       /* Username for PAP */
109 char    passwd[MAXSECRETLEN];   /* Password for PAP */
110 bool    persist = 0;            /* Reopen link after it goes down */
111 char    our_name[MAXNAMELEN];   /* Our name for authentication purposes */
112 bool    demand = 0;             /* do dial-on-demand */
113 char    *ipparam = NULL;        /* Extra parameter for ip up/down scripts */
114 int     idle_time_limit = 0;    /* Disconnect if idle for this many seconds */
115 int     holdoff = 30;           /* # seconds to pause before reconnecting */
116 bool    holdoff_specified;      /* true if a holdoff value has been given */
117 int     log_to_fd = 1;          /* send log messages to this fd too */
118 bool    log_default = 1;        /* log_to_fd is default (stdout) */
119 int     maxfail = 10;           /* max # of unsuccessful connection attempts */
120 char    linkname[MAXPATHLEN];   /* logical name for link */
121 bool    tune_kernel;            /* may alter kernel settings */
122 int     connect_delay = 1000;   /* wait this many ms after connect script */
123 int     req_unit = -1;          /* requested interface unit */
124 char    path_ipup[MAXPATHLEN];  /* pathname of ip-up script */
125 char    path_ipdown[MAXPATHLEN];/* pathname of ip-down script */
126 char    req_ifname[IFNAMSIZ];   /* requested interface name */
127 bool    multilink = 0;          /* Enable multilink operation */
128 char    *bundle_name = NULL;    /* bundle name for multilink */
129 bool    dump_options;           /* print out option values */
130 bool    dryrun;                 /* print out option values and exit */
131 char    *domain;                /* domain name set by domain option */
132 int     child_wait = 5;         /* # seconds to wait for children at exit */
133 struct userenv *userenv_list;   /* user environment variables */
134 int     dfl_route_metric = -1;  /* metric of the default route to set over the PPP link */
135
136 #ifdef PPP_WITH_IPV6CP
137 char    path_ipv6up[MAXPATHLEN];   /* pathname of ipv6-up script */
138 char    path_ipv6down[MAXPATHLEN]; /* pathname of ipv6-down script */
139 #endif
140
141 #ifdef PPP_WITH_MAXOCTETS
142 unsigned int  maxoctets = 0;    /* default - no limit */
143 int maxoctets_dir = 0;       /* default - sum of traffic */
144 int maxoctets_timeout = 1;   /* default 1 second */ 
145 #endif
146
147
148 extern option_t auth_options[];
149 extern struct stat devstat;
150
151 #ifdef PPP_WITH_FILTER
152 struct  bpf_program pass_filter;/* Filter program for packets to pass */
153 struct  bpf_program active_filter; /* Filter program for link-active pkts */
154 #endif
155
156 static option_t *curopt;        /* pointer to option being processed */
157 char *current_option;           /* the name of the option being parsed */
158 int  privileged_option;         /* set iff the current option came from root */
159 char *option_source;            /* string saying where the option came from */
160 int  option_priority = OPRIO_CFGFILE; /* priority of the current options */
161 bool devnam_fixed;              /* can no longer change device name */
162
163 static int logfile_fd = -1;     /* fd opened for log file */
164 static char logfile_name[MAXPATHLEN];   /* name of log file */
165
166 static bool noipx_opt;          /* dummy for noipx option */
167
168 /*
169  * Prototypes
170  */
171 static int setdomain(char **);
172 static int readfile(char **);
173 static int callfile(char **);
174 static int showversion(char **);
175 static int showhelp(char **);
176 static void usage(void);
177 static int setlogfile(char **);
178 #ifdef PPP_WITH_PLUGINS
179 static int loadplugin(char **);
180 #endif
181
182 #ifdef PPP_WITH_FILTER
183 static int setpassfilter(char **);
184 static int setactivefilter(char **);
185 #endif
186
187 #ifdef PPP_WITH_MAXOCTETS
188 static int setmodir(char **);
189 #endif
190
191 static int user_setenv(char **);
192 static void user_setprint(option_t *, printer_func, void *);
193 static int user_unsetenv(char **);
194 static void user_unsetprint(option_t *, printer_func, void *);
195
196 static option_t *find_option(char *name);
197 static int process_option(option_t *, char *, char **);
198 static int n_arguments(option_t *);
199 static int number_option(char *, u_int32_t *, int);
200
201 /*
202  * Structure to store extra lists of options.
203  */
204 struct option_list {
205     option_t *options;
206     struct option_list *next;
207 };
208
209 static struct option_list *extra_options = NULL;
210
211 /*
212  * Valid arguments.
213  */
214 option_t general_options[] = {
215     { "debug", o_int, &debug,
216       "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
217     { "-d", o_int, &debug,
218       "Increase debugging level",
219       OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
220
221     { "kdebug", o_int, &kdebugflag,
222       "Set kernel driver debug level", OPT_PRIO },
223
224     { "nodetach", o_bool, &nodetach,
225       "Don't detach from controlling tty", OPT_PRIO | 1 },
226     { "-detach", o_bool, &nodetach,
227       "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
228 #ifdef SYSTEMD
229     { "up_sdnotify", o_bool, &up_sdnotify,
230       "Notify systemd once link is up (implies nodetach)",
231       OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
232 #endif
233     { "updetach", o_bool, &updetach,
234       "Detach from controlling tty once link is up",
235       OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
236
237     { "master_detach", o_bool, &master_detach,
238       "Detach when we're multilink master but have no link", 1 },
239
240     { "holdoff", o_int, &holdoff,
241       "Set time in seconds before retrying connection",
242       OPT_PRIO, &holdoff_specified },
243
244     { "idle", o_int, &idle_time_limit,
245       "Set time in seconds before disconnecting idle link", OPT_PRIO },
246
247     { "maxconnect", o_int, &maxconnect,
248       "Set connection time limit",
249       OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
250
251     { "domain", o_special, (void *)setdomain,
252       "Add given domain name to hostname",
253       OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
254
255     { "file", o_special, (void *)readfile,
256       "Take options from a file", OPT_NOPRINT },
257     { "call", o_special, (void *)callfile,
258       "Take options from a privileged file", OPT_NOPRINT },
259
260     { "persist", o_bool, &persist,
261       "Keep on reopening connection after close", OPT_PRIO | 1 },
262     { "nopersist", o_bool, &persist,
263       "Turn off persist option", OPT_PRIOSUB },
264
265     { "demand", o_bool, &demand,
266       "Dial on demand", OPT_INITONLY | 1, &persist },
267
268     { "--version", o_special_noarg, (void *)showversion,
269       "Show version number" },
270     { "--help", o_special_noarg, (void *)showhelp,
271       "Show brief listing of options" },
272     { "-h", o_special_noarg, (void *)showhelp,
273       "Show brief listing of options", OPT_ALIAS },
274
275     { "logfile", o_special, (void *)setlogfile,
276       "Append log messages to this file",
277       OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
278     { "logfd", o_int, &log_to_fd,
279       "Send log messages to this file descriptor",
280       OPT_PRIOSUB | OPT_A2CLR, &log_default },
281     { "nolog", o_int, &log_to_fd,
282       "Don't send log messages to any file",
283       OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
284     { "nologfd", o_int, &log_to_fd,
285       "Don't send log messages to any file descriptor",
286       OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
287
288     { "linkname", o_string, linkname,
289       "Set logical name for link",
290       OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
291
292     { "maxfail", o_int, &maxfail,
293       "Maximum number of unsuccessful connection attempts to allow",
294       OPT_PRIO },
295
296     { "ktune", o_bool, &tune_kernel,
297       "Alter kernel settings as necessary", OPT_PRIO | 1 },
298     { "noktune", o_bool, &tune_kernel,
299       "Don't alter kernel settings", OPT_PRIOSUB },
300
301     { "connect-delay", o_int, &connect_delay,
302       "Maximum time (in ms) to wait after connect script finishes",
303       OPT_PRIO },
304
305     { "unit", o_int, &req_unit,
306       "PPP interface unit number to use if possible",
307       OPT_PRIO | OPT_LLIMIT, 0, 0 },
308
309     { "ifname", o_string, req_ifname,
310       "Set PPP interface name",
311       OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, IFNAMSIZ },
312
313     { "dump", o_bool, &dump_options,
314       "Print out option values after parsing all options", 1 },
315     { "dryrun", o_bool, &dryrun,
316       "Stop after parsing, printing, and checking options", 1 },
317
318     { "child-timeout", o_int, &child_wait,
319       "Number of seconds to wait for child processes at exit",
320       OPT_PRIO },
321
322     { "set", o_special, (void *)user_setenv,
323       "Set user environment variable",
324       OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
325     { "unset", o_special, (void *)user_unsetenv,
326       "Unset user environment variable",
327       OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
328
329     { "defaultroute-metric", o_int, &dfl_route_metric,
330       "Metric to use for the default route (Linux only; -1 for default behavior)",
331       OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
332
333     { "ip-up-script", o_string, path_ipup,
334       "Set pathname of ip-up script",
335       OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
336     { "ip-down-script", o_string, path_ipdown,
337       "Set pathname of ip-down script",
338       OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
339
340 #ifdef PPP_WITH_IPV6CP
341     { "ipv6-up-script", o_string, path_ipv6up,
342       "Set pathname of ipv6-up script",
343       OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
344     { "ipv6-down-script", o_string, path_ipv6down,
345       "Set pathname of ipv6-down script",
346       OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
347 #endif
348
349 #ifdef PPP_WITH_MULTILINK
350     { "multilink", o_bool, &multilink,
351       "Enable multilink operation", OPT_PRIO | 1 },
352     { "mp", o_bool, &multilink,
353       "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
354     { "nomultilink", o_bool, &multilink,
355       "Disable multilink operation", OPT_PRIOSUB | 0 },
356     { "nomp", o_bool, &multilink,
357       "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
358
359     { "bundle", o_string, &bundle_name,
360       "Bundle name for multilink", OPT_PRIO },
361 #endif /* PPP_WITH_MULTILINK */
362
363 #ifdef PPP_WITH_PLUGINS
364     { "plugin", o_special, (void *)loadplugin,
365       "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
366 #endif
367
368 #ifdef PPP_WITH_FILTER
369     { "pass-filter", o_special, setpassfilter,
370       "set filter for packets to pass", OPT_PRIO },
371
372     { "active-filter", o_special, setactivefilter,
373       "set filter for active pkts", OPT_PRIO },
374 #endif
375
376 #ifdef PPP_WITH_MAXOCTETS
377     { "maxoctets", o_int, &maxoctets,
378       "Set connection traffic limit",
379       OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
380     { "mo", o_int, &maxoctets,
381       "Set connection traffic limit",
382       OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
383     { "mo-direction", o_special, setmodir,
384       "Set direction for limit traffic (sum,in,out,max)" },
385     { "mo-timeout", o_int, &maxoctets_timeout,
386       "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
387 #endif
388
389     /* Dummy option, does nothing */
390     { "noipx", o_bool, &noipx_opt, NULL, OPT_NOPRINT | 1 },
391
392     { NULL }
393 };
394
395 #ifndef IMPLEMENTATION
396 #define IMPLEMENTATION ""
397 #endif
398
399 static char *usage_string = "\
400 pppd version %s\n\
401 Usage: %s [ options ], where options are:\n\
402         <device>        Communicate over the named device\n\
403         <speed>         Set the baud rate to <speed>\n\
404         <loc>:<rem>     Set the local and/or remote interface IP\n\
405                         addresses.  Either one may be omitted.\n\
406         asyncmap <n>    Set the desired async map to hex <n>\n\
407         auth            Require authentication from peer\n\
408         connect <p>     Invoke shell command <p> to set up the serial line\n\
409         crtscts         Use hardware RTS/CTS flow control\n\
410         defaultroute    Add default route through interface\n\
411         file <f>        Take options from file <f>\n\
412         modem           Use modem control lines\n\
413         mru <n>         Set MRU value to <n> for negotiation\n\
414 See pppd(8) for more options.\n\
415 ";
416
417 /*
418  * parse_args - parse a string of arguments from the command line.
419  */
420 int
421 parse_args(int argc, char **argv)
422 {
423     char *arg;
424     option_t *opt;
425     int n;
426
427     privileged_option = privileged;
428     option_source = "command line";
429     option_priority = OPRIO_CMDLINE;
430     while (argc > 0) {
431         arg = *argv++;
432         --argc;
433         opt = find_option(arg);
434         if (opt == NULL) {
435             option_error("unrecognized option '%s'", arg);
436             usage();
437             return 0;
438         }
439         n = n_arguments(opt);
440         if (argc < n) {
441             option_error("too few parameters for option %s", arg);
442             return 0;
443         }
444         if (!process_option(opt, arg, argv))
445             return 0;
446         argc -= n;
447         argv += n;
448     }
449     return 1;
450 }
451
452 /*
453  * options_from_file - Read a string of options from a file,
454  * and interpret them.
455  */
456 int
457 options_from_file(char *filename, int must_exist, int check_prot, int priv)
458 {
459     FILE *f;
460     int i, newline, ret, err;
461     option_t *opt;
462     int oldpriv, n;
463     char *oldsource;
464     uid_t euid;
465     char *argv[MAXARGS];
466     char args[MAXARGS][MAXWORDLEN];
467     char cmd[MAXWORDLEN];
468
469     euid = geteuid();
470     if (check_prot && seteuid(getuid()) == -1) {
471         option_error("unable to drop privileges to open %s: %m", filename);
472         return 0;
473     }
474     f = fopen(filename, "r");
475     err = errno;
476     if (check_prot && seteuid(euid) == -1)
477         fatal("unable to regain privileges");
478     if (f == NULL) {
479         errno = err;
480         if (!must_exist) {
481             if (err != ENOENT && err != ENOTDIR)
482                 warn("Warning: can't open options file %s: %m", filename);
483             return 1;
484         }
485         option_error("Can't open options file %s: %m", filename);
486         return 0;
487     }
488
489     oldpriv = privileged_option;
490     privileged_option = priv;
491     oldsource = option_source;
492     option_source = strdup(filename);
493     if (option_source == NULL)
494         option_source = "file";
495     ret = 0;
496     while (getword(f, cmd, &newline, filename)) {
497         opt = find_option(cmd);
498         if (opt == NULL) {
499             option_error("In file %s: unrecognized option '%s'",
500                          filename, cmd);
501             goto err;
502         }
503         n = n_arguments(opt);
504         for (i = 0; i < n; ++i) {
505             if (!getword(f, args[i], &newline, filename)) {
506                 option_error(
507                         "In file %s: too few parameters for option '%s'",
508                         filename, cmd);
509                 goto err;
510             }
511             argv[i] = args[i];
512         }
513         if (!process_option(opt, cmd, argv))
514             goto err;
515     }
516     ret = 1;
517
518 err:
519     fclose(f);
520     privileged_option = oldpriv;
521     option_source = oldsource;
522     return ret;
523 }
524
525 /*
526  * options_from_user - See if the use has a ~/.ppprc file,
527  * and if so, interpret options from it.
528  */
529 int
530 options_from_user(void)
531 {
532     char *user, *path, *file;
533     int ret;
534     struct passwd *pw;
535     size_t pl;
536
537     pw = getpwuid(getuid());
538     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
539         return 1;
540     file = _PATH_USEROPT;
541     pl = strlen(user) + strlen(file) + 2;
542     path = malloc(pl);
543     if (path == NULL)
544         novm("init file name");
545     slprintf(path, pl, "%s/%s", user, file);
546     option_priority = OPRIO_CFGFILE;
547     ret = options_from_file(path, 0, 1, privileged);
548     free(path);
549     return ret;
550 }
551
552 /*
553  * options_for_tty - See if an options file exists for the serial
554  * device, and if so, interpret options from it.
555  * We only allow the per-tty options file to override anything from
556  * the command line if it is something that the user can't override
557  * once it has been set by root; this is done by giving configuration
558  * files a lower priority than the command line.
559  */
560 int
561 options_for_tty(void)
562 {
563     char *dev, *path, *p;
564     int ret;
565     size_t pl;
566
567     dev = devnam;
568     if ((p = strstr(dev, "/dev/")) != NULL)
569         dev = p + 5;
570     if (dev[0] == 0 || strcmp(dev, "tty") == 0)
571         return 1;               /* don't look for /etc/ppp/options.tty */
572     pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
573     path = malloc(pl);
574     if (path == NULL)
575         novm("tty init file name");
576     slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
577     /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
578     for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
579         if (*p == '/')
580             *p = '.';
581     option_priority = OPRIO_CFGFILE;
582     ret = options_from_file(path, 0, 0, 1);
583     free(path);
584     return ret;
585 }
586
587 /*
588  * options_from_list - process a string of options in a wordlist.
589  */
590 int
591 options_from_list(struct wordlist *w, int priv)
592 {
593     char *argv[MAXARGS];
594     option_t *opt;
595     int i, n, ret = 0;
596     struct wordlist *w0;
597
598     privileged_option = priv;
599     option_source = "secrets file";
600     option_priority = OPRIO_SECFILE;
601
602     while (w != NULL) {
603         opt = find_option(w->word);
604         if (opt == NULL) {
605             option_error("In secrets file: unrecognized option '%s'",
606                          w->word);
607             goto err;
608         }
609         n = n_arguments(opt);
610         w0 = w;
611         for (i = 0; i < n; ++i) {
612             w = w->next;
613             if (w == NULL) {
614                 option_error(
615                         "In secrets file: too few parameters for option '%s'",
616                         w0->word);
617                 goto err;
618             }
619             argv[i] = w->word;
620         }
621         if (!process_option(opt, w0->word, argv))
622             goto err;
623         w = w->next;
624     }
625     ret = 1;
626
627 err:
628     return ret;
629 }
630
631 /*
632  * match_option - see if this option matches an option_t structure.
633  */
634 static int
635 match_option(char *name, option_t *opt, int dowild)
636 {
637         int (*match)(char *, char **, int);
638
639         if (dowild != (opt->type == o_wild))
640                 return 0;
641         if (!dowild)
642                 return strcmp(name, opt->name) == 0;
643         match = (int (*)(char *, char **, int)) opt->addr;
644         return (*match)(name, NULL, 0);
645 }
646
647 /*
648  * find_option - scan the option lists for the various protocols
649  * looking for an entry with the given name.
650  * This could be optimized by using a hash table.
651  */
652 static option_t *
653 find_option(char *name)
654 {
655         option_t *opt;
656         struct option_list *list;
657         int i, dowild;
658
659         for (dowild = 0; dowild <= 1; ++dowild) {
660                 for (opt = general_options; opt->name != NULL; ++opt)
661                         if (match_option(name, opt, dowild))
662                                 return opt;
663                 for (opt = auth_options; opt->name != NULL; ++opt)
664                         if (match_option(name, opt, dowild))
665                                 return opt;
666                 for (list = extra_options; list != NULL; list = list->next)
667                         for (opt = list->options; opt->name != NULL; ++opt)
668                                 if (match_option(name, opt, dowild))
669                                         return opt;
670                 for (opt = the_channel->options; opt->name != NULL; ++opt)
671                         if (match_option(name, opt, dowild))
672                                 return opt;
673                 for (i = 0; protocols[i] != NULL; ++i)
674                         if ((opt = protocols[i]->options) != NULL)
675                                 for (; opt->name != NULL; ++opt)
676                                         if (match_option(name, opt, dowild))
677                                                 return opt;
678         }
679         return NULL;
680 }
681
682 /*
683  * process_option - process one new-style option.
684  */
685 static int
686 process_option(option_t *opt, char *cmd, char **argv)
687 {
688     u_int32_t v;
689     int iv, a;
690     char *sv;
691     int (*parser)(char **);
692     int (*wildp)(char *, char **, int);
693     char *optopt = (opt->type == o_wild)? "": " option";
694     int prio = option_priority;
695     option_t *mainopt = opt;
696
697     current_option = opt->name;
698     if ((opt->flags & OPT_PRIVFIX) && privileged_option)
699         prio += OPRIO_ROOT;
700     while (mainopt->flags & OPT_PRIOSUB)
701         --mainopt;
702     if (mainopt->flags & OPT_PRIO) {
703         if (prio < mainopt->priority) {
704             /* new value doesn't override old */
705             if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
706                 option_error("%s%s set in %s cannot be overridden\n",
707                              opt->name, optopt, mainopt->source);
708                 return 0;
709             }
710             return 1;
711         }
712         if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
713             warn("%s%s from %s overrides command line",
714                  opt->name, optopt, option_source);
715     }
716
717     if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
718         option_error("%s%s cannot be changed after initialization",
719                      opt->name, optopt);
720         return 0;
721     }
722     if ((opt->flags & OPT_PRIV) && !privileged_option) {
723         option_error("using the %s%s requires root privilege",
724                      opt->name, optopt);
725         return 0;
726     }
727     if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
728         option_error("%s%s is disabled", opt->name, optopt);
729         return 0;
730     }
731     if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
732         option_error("the %s%s may not be changed in %s",
733                      opt->name, optopt, option_source);
734         return 0;
735     }
736
737     switch (opt->type) {
738     case o_bool:
739         v = opt->flags & OPT_VALUE;
740         *(bool *)(opt->addr) = v;
741         if (opt->addr2 && (opt->flags & OPT_A2COPY))
742             *(bool *)(opt->addr2) = v;
743         else if (opt->addr2 && (opt->flags & OPT_A2CLR))
744             *(bool *)(opt->addr2) = 0;
745         else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
746             *(u_char *)(opt->addr2) &= ~v;
747         else if (opt->addr2 && (opt->flags & OPT_A2OR))
748             *(u_char *)(opt->addr2) |= v;
749         break;
750
751     case o_int:
752         iv = 0;
753         if ((opt->flags & OPT_NOARG) == 0) {
754             if (!int_option(*argv, &iv))
755                 return 0;
756             if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
757                  || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
758                 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
759                 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
760                 switch (opt->flags & OPT_LIMITS) {
761                 case OPT_LLIMIT:
762                     option_error("%s value must be%s >= %d",
763                                  opt->name, zok, opt->lower_limit);
764                     break;
765                 case OPT_ULIMIT:
766                     option_error("%s value must be%s <= %d",
767                                  opt->name, zok, opt->upper_limit);
768                     break;
769                 case OPT_LIMITS:
770                     option_error("%s value must be%s between %d and %d",
771                                 opt->name, zok, opt->lower_limit, opt->upper_limit);
772                     break;
773                 }
774                 return 0;
775             }
776         }
777         a = opt->flags & OPT_VALUE;
778         if (a >= 128)
779             a -= 256;           /* sign extend */
780         iv += a;
781         if (opt->flags & OPT_INC)
782             iv += *(int *)(opt->addr);
783         if ((opt->flags & OPT_NOINCR) && !privileged_option) {
784             int oldv = *(int *)(opt->addr);
785             if ((opt->flags & OPT_ZEROINF) ?
786                 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
787                 option_error("%s value cannot be increased", opt->name);
788                 return 0;
789             }
790         }
791         *(int *)(opt->addr) = iv;
792         if (opt->addr2 && (opt->flags & OPT_A2COPY))
793             *(int *)(opt->addr2) = iv;
794         break;
795
796     case o_uint32:
797         if (opt->flags & OPT_NOARG) {
798             v = opt->flags & OPT_VALUE;
799             if (v & 0x80)
800                     v |= 0xffffff00U;
801         } else if (!number_option(*argv, &v, 16))
802             return 0;
803         if (opt->flags & OPT_OR)
804             v |= *(u_int32_t *)(opt->addr);
805         *(u_int32_t *)(opt->addr) = v;
806         if (opt->addr2 && (opt->flags & OPT_A2COPY))
807             *(u_int32_t *)(opt->addr2) = v;
808         break;
809
810     case o_string:
811         if (opt->flags & OPT_STATIC) {
812             strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
813         } else {
814             char **optptr = (char **)(opt->addr);
815             sv = strdup(*argv);
816             if (sv == NULL)
817                 novm("option argument");
818             if (*optptr)
819                 free(*optptr);
820             *optptr = sv;
821         }
822         /* obfuscate original argument for things like password */
823         if (opt->flags & OPT_HIDE) {
824             memset(*argv, '?', strlen(*argv));
825             *argv = "********";
826         }
827         break;
828
829     case o_special_noarg:
830     case o_special:
831         parser = (int (*)(char **)) opt->addr;
832         curopt = opt;
833         if (!(*parser)(argv))
834             return 0;
835         if (opt->flags & OPT_A2LIST) {
836             struct option_value *ovp, *pp;
837
838             ovp = malloc(sizeof(*ovp) + strlen(*argv));
839             if (ovp != 0) {
840                 strcpy(ovp->value, *argv);
841                 ovp->source = option_source;
842                 ovp->next = NULL;
843                 if (opt->addr2 == NULL) {
844                     opt->addr2 = ovp;
845                 } else {
846                     for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
847                         ;
848                     pp->next = ovp;
849                 }
850             }
851         }
852         break;
853
854     case o_wild:
855         wildp = (int (*)(char *, char **, int)) opt->addr;
856         if (!(*wildp)(cmd, argv, 1))
857             return 0;
858         break;
859     }
860
861     /*
862      * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
863      * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
864      */
865     if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
866                 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
867         *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
868
869     mainopt->source = option_source;
870     mainopt->priority = prio;
871     mainopt->winner = opt - mainopt;
872
873     return 1;
874 }
875
876 /*
877  * override_value - if the option priorities would permit us to
878  * override the value of option, return 1 and update the priority
879  * and source of the option value.  Otherwise returns 0.
880  */
881 int
882 override_value(char *option, int priority, const char *source)
883 {
884         option_t *opt;
885
886         opt = find_option(option);
887         if (opt == NULL)
888                 return 0;
889         while (opt->flags & OPT_PRIOSUB)
890                 --opt;
891         if ((opt->flags & OPT_PRIO) && priority < opt->priority)
892                 return 0;
893         opt->priority = priority;
894         opt->source = source;
895         opt->winner = -1;
896         return 1;
897 }
898
899 /*
900  * n_arguments - tell how many arguments an option takes
901  */
902 static int
903 n_arguments(option_t *opt)
904 {
905         return (opt->type == o_bool || opt->type == o_special_noarg
906                 || (opt->flags & OPT_NOARG))? 0: 1;
907 }
908
909 /*
910  * add_options - add a list of options to the set we grok.
911  */
912 void
913 add_options(option_t *opt)
914 {
915     struct option_list *list;
916
917     list = malloc(sizeof(*list));
918     if (list == 0)
919         novm("option list entry");
920     list->options = opt;
921     list->next = extra_options;
922     extra_options = list;
923 }
924
925 /*
926  * check_options - check that options are valid and consistent.
927  */
928 void
929 check_options(void)
930 {
931         if (logfile_fd >= 0 && logfile_fd != log_to_fd)
932                 close(logfile_fd);
933 }
934
935 /*
936  * print_option - print out an option and its value
937  */
938 static void
939 print_option(option_t *opt, option_t *mainopt, printer_func printer, void *arg)
940 {
941         int i, v;
942         char *p;
943
944         if (opt->flags & OPT_NOPRINT)
945                 return;
946         switch (opt->type) {
947         case o_bool:
948                 v = opt->flags & OPT_VALUE;
949                 if (*(bool *)opt->addr != v)
950                         /* this can happen legitimately, e.g. lock
951                            option turned off for default device */
952                         break;
953                 printer(arg, "%s", opt->name);
954                 break;
955         case o_int:
956                 v = opt->flags & OPT_VALUE;
957                 if (v >= 128)
958                         v -= 256;
959                 i = *(int *)opt->addr;
960                 if (opt->flags & OPT_NOARG) {
961                         printer(arg, "%s", opt->name);
962                         if (i != v) {
963                                 if (opt->flags & OPT_INC) {
964                                         for (; i > v; i -= v)
965                                                 printer(arg, " %s", opt->name);
966                                 } else
967                                         printer(arg, " # oops: %d not %d\n",
968                                                 i, v);
969                         }
970                 } else {
971                         printer(arg, "%s %d", opt->name, i);
972                 }
973                 break;
974         case o_uint32:
975                 printer(arg, "%s", opt->name);
976                 if ((opt->flags & OPT_NOARG) == 0)
977                         printer(arg, " %x", *(u_int32_t *)opt->addr);
978                 break;
979
980         case o_string:
981                 if (opt->flags & OPT_HIDE) {
982                         p = "??????";
983                 } else {
984                         p = (char *) opt->addr;
985                         if ((opt->flags & OPT_STATIC) == 0)
986                                 p = *(char **)p;
987                 }
988                 printer(arg, "%s %q", opt->name, p);
989                 break;
990
991         case o_special:
992         case o_special_noarg:
993         case o_wild:
994                 if (opt->type != o_wild) {
995                         printer(arg, "%s", opt->name);
996                         if (n_arguments(opt) == 0)
997                                 break;
998                         printer(arg, " ");
999                 }
1000                 if (opt->flags & OPT_A2PRINTER) {
1001                         void (*oprt)(option_t *, printer_func, void *);
1002                         oprt = (void (*)(option_t *, printer_func, void *))
1003                                 opt->addr2;
1004                         (*oprt)(opt, printer, arg);
1005                 } else if (opt->flags & OPT_A2STRVAL) {
1006                         p = (char *) opt->addr2;
1007                         if ((opt->flags & OPT_STATIC) == 0)
1008                                 p = *(char **)p;
1009                         printer(arg, "%q", p);
1010                 } else if (opt->flags & OPT_A2LIST) {
1011                         struct option_value *ovp;
1012
1013                         ovp = (struct option_value *) opt->addr2;
1014                         for (;;) {
1015                                 printer(arg, "%q", ovp->value);
1016                                 if ((ovp = ovp->next) == NULL)
1017                                         break;
1018                                 printer(arg, "\t\t# (from %s)\n%s ",
1019                                         ovp->source, opt->name);
1020                         }
1021                 } else {
1022                         printer(arg, "xxx # [don't know how to print value]");
1023                 }
1024                 break;
1025
1026         default:
1027                 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1028                 break;
1029         }
1030         printer(arg, "\t\t# (from %s)\n", mainopt->source);
1031 }
1032
1033 /*
1034  * print_option_list - print out options in effect from an
1035  * array of options.
1036  */
1037 static void
1038 print_option_list(option_t *opt, printer_func printer, void *arg)
1039 {
1040         while (opt->name != NULL) {
1041                 if (opt->priority != OPRIO_DEFAULT
1042                     && opt->winner != (short int) -1)
1043                         print_option(opt + opt->winner, opt, printer, arg);
1044                 do {
1045                         ++opt;
1046                 } while (opt->flags & OPT_PRIOSUB);
1047         }
1048 }
1049
1050 /*
1051  * print_options - print out what options are in effect.
1052  */
1053 void
1054 print_options(printer_func printer, void *arg)
1055 {
1056         struct option_list *list;
1057         int i;
1058
1059         printer(arg, "pppd options in effect:\n");
1060         print_option_list(general_options, printer, arg);
1061         print_option_list(auth_options, printer, arg);
1062         for (list = extra_options; list != NULL; list = list->next)
1063                 print_option_list(list->options, printer, arg);
1064         print_option_list(the_channel->options, printer, arg);
1065         for (i = 0; protocols[i] != NULL; ++i)
1066                 print_option_list(protocols[i]->options, printer, arg);
1067 }
1068
1069 /*
1070  * usage - print out a message telling how to use the program.
1071  */
1072 static void
1073 usage(void)
1074 {
1075     if (phase == PHASE_INITIALIZE)
1076         fprintf(stderr, usage_string, VERSION, progname);
1077 }
1078
1079 /*
1080  * showhelp - print out usage message and exit.
1081  */
1082 static int
1083 showhelp(char **argv)
1084 {
1085     if (phase == PHASE_INITIALIZE) {
1086         usage();
1087         exit(0);
1088     }
1089     return 0;
1090 }
1091
1092 /*
1093  * showversion - print out the version number and exit.
1094  */
1095 static int
1096 showversion(char **argv)
1097 {
1098     if (phase == PHASE_INITIALIZE) {
1099         fprintf(stdout, "pppd version %s\n", VERSION);
1100         exit(0);
1101     }
1102     return 0;
1103 }
1104
1105 /*
1106  * option_error - print a message about an error in an option.
1107  * The message is logged, and also sent to
1108  * stderr if phase == PHASE_INITIALIZE.
1109  */
1110 void
1111 option_error(char *fmt, ...)
1112 {
1113     va_list args;
1114     char buf[1024];
1115
1116     va_start(args, fmt);
1117     vslprintf(buf, sizeof(buf), fmt, args);
1118     va_end(args);
1119     if (phase == PHASE_INITIALIZE)
1120         fprintf(stderr, "%s: %s\n", progname, buf);
1121     syslog(LOG_ERR, "%s", buf);
1122 }
1123
1124 #if 0
1125 /*
1126  * readable - check if a file is readable by the real user.
1127  */
1128 int
1129 readable(int fd)
1130 {
1131     uid_t uid;
1132     int i;
1133     struct stat sbuf;
1134
1135     uid = getuid();
1136     if (uid == 0)
1137         return 1;
1138     if (fstat(fd, &sbuf) != 0)
1139         return 0;
1140     if (sbuf.st_uid == uid)
1141         return sbuf.st_mode & S_IRUSR;
1142     if (sbuf.st_gid == getgid())
1143         return sbuf.st_mode & S_IRGRP;
1144     for (i = 0; i < ngroups; ++i)
1145         if (sbuf.st_gid == groups[i])
1146             return sbuf.st_mode & S_IRGRP;
1147     return sbuf.st_mode & S_IROTH;
1148 }
1149 #endif
1150
1151 /*
1152  * Read a word from a file.
1153  * Words are delimited by white-space or by quotes (" or ').
1154  * Quotes, white-space and \ may be escaped with \.
1155  * \<newline> is ignored.
1156  */
1157 int
1158 getword(FILE *f, char *word, int *newlinep, char *filename)
1159 {
1160     int c, len, escape;
1161     int quoted, comment;
1162     int value, digit, got, n;
1163
1164 #define isoctal(c) ((c) >= '0' && (c) < '8')
1165
1166     *newlinep = 0;
1167     len = 0;
1168     escape = 0;
1169     comment = 0;
1170     quoted = 0;
1171
1172     /*
1173      * First skip white-space and comments.
1174      */
1175     for (;;) {
1176         c = getc(f);
1177         if (c == EOF)
1178             break;
1179
1180         /*
1181          * A newline means the end of a comment; backslash-newline
1182          * is ignored.  Note that we cannot have escape && comment.
1183          */
1184         if (c == '\n') {
1185             if (!escape) {
1186                 *newlinep = 1;
1187                 comment = 0;
1188             } else
1189                 escape = 0;
1190             continue;
1191         }
1192
1193         /*
1194          * Ignore characters other than newline in a comment.
1195          */
1196         if (comment)
1197             continue;
1198
1199         /*
1200          * If this character is escaped, we have a word start.
1201          */
1202         if (escape)
1203             break;
1204
1205         /*
1206          * If this is the escape character, look at the next character.
1207          */
1208         if (c == '\\') {
1209             escape = 1;
1210             continue;
1211         }
1212
1213         /*
1214          * If this is the start of a comment, ignore the rest of the line.
1215          */
1216         if (c == '#') {
1217             comment = 1;
1218             continue;
1219         }
1220
1221         /*
1222          * A non-whitespace character is the start of a word.
1223          */
1224         if (!isspace(c))
1225             break;
1226     }
1227
1228     /*
1229      * Process characters until the end of the word.
1230      */
1231     while (c != EOF) {
1232         if (escape) {
1233             /*
1234              * This character is escaped: backslash-newline is ignored,
1235              * various other characters indicate particular values
1236              * as for C backslash-escapes.
1237              */
1238             escape = 0;
1239             if (c == '\n') {
1240                 c = getc(f);
1241                 continue;
1242             }
1243
1244             got = 0;
1245             switch (c) {
1246             case 'a':
1247                 value = '\a';
1248                 break;
1249             case 'b':
1250                 value = '\b';
1251                 break;
1252             case 'f':
1253                 value = '\f';
1254                 break;
1255             case 'n':
1256                 value = '\n';
1257                 break;
1258             case 'r':
1259                 value = '\r';
1260                 break;
1261             case 's':
1262                 value = ' ';
1263                 break;
1264             case 't':
1265                 value = '\t';
1266                 break;
1267
1268             default:
1269                 if (isoctal(c)) {
1270                     /*
1271                      * \ddd octal sequence
1272                      */
1273                     value = 0;
1274                     for (n = 0; n < 3 && isoctal(c); ++n) {
1275                         value = (value << 3) + (c & 07);
1276                         c = getc(f);
1277                     }
1278                     got = 1;
1279                     break;
1280                 }
1281
1282                 if (c == 'x') {
1283                     /*
1284                      * \x<hex_string> sequence
1285                      */
1286                     value = 0;
1287                     c = getc(f);
1288                     for (n = 0; n < 2 && isxdigit(c); ++n) {
1289                         digit = toupper(c) - '0';
1290                         if (digit > 10)
1291                             digit += '0' + 10 - 'A';
1292                         value = (value << 4) + digit;
1293                         c = getc (f);
1294                     }
1295                     got = 1;
1296                     break;
1297                 }
1298
1299                 /*
1300                  * Otherwise the character stands for itself.
1301                  */
1302                 value = c;
1303                 break;
1304             }
1305
1306             /*
1307              * Store the resulting character for the escape sequence.
1308              */
1309             if (len < MAXWORDLEN) {
1310                 word[len] = value;
1311                 ++len;
1312             }
1313
1314             if (!got)
1315                 c = getc(f);
1316             continue;
1317         }
1318
1319         /*
1320          * Backslash starts a new escape sequence.
1321          */
1322         if (c == '\\') {
1323             escape = 1;
1324             c = getc(f);
1325             continue;
1326         }
1327
1328         /*
1329          * Not escaped: check for the start or end of a quoted
1330          * section and see if we've reached the end of the word.
1331          */
1332         if (quoted) {
1333             if (c == quoted) {
1334                 quoted = 0;
1335                 c = getc(f);
1336                 continue;
1337             }
1338         } else if (c == '"' || c == '\'') {
1339             quoted = c;
1340             c = getc(f);
1341             continue;
1342         } else if (isspace(c) || c == '#') {
1343             ungetc (c, f);
1344             break;
1345         }
1346
1347         /*
1348          * An ordinary character: store it in the word and get another.
1349          */
1350         if (len < MAXWORDLEN) {
1351             word[len] = c;
1352             ++len;
1353         }
1354
1355         c = getc(f);
1356     }
1357     word[MAXWORDLEN-1] = 0;     /* make sure word is null-terminated */
1358
1359     /*
1360      * End of the word: check for errors.
1361      */
1362     if (c == EOF) {
1363         if (ferror(f)) {
1364             if (errno == 0)
1365                 errno = EIO;
1366             option_error("Error reading %s: %m", filename);
1367             die(1);
1368         }
1369         /*
1370          * If len is zero, then we didn't find a word before the
1371          * end of the file.
1372          */
1373         if (len == 0)
1374             return 0;
1375         if (quoted)
1376             option_error("warning: quoted word runs to end of file (%.20s...)",
1377                          filename, word);
1378     }
1379
1380     /*
1381      * Warn if the word was too long, and append a terminating null.
1382      */
1383     if (len >= MAXWORDLEN) {
1384         option_error("warning: word in file %s too long (%.20s...)",
1385                      filename, word);
1386         len = MAXWORDLEN - 1;
1387     }
1388     word[len] = 0;
1389
1390     return 1;
1391
1392 #undef isoctal
1393
1394 }
1395
1396 /*
1397  * number_option - parse an unsigned numeric parameter for an option.
1398  */
1399 static int
1400 number_option(char *str, u_int32_t *valp, int base)
1401 {
1402     char *ptr;
1403
1404     *valp = strtoul(str, &ptr, base);
1405     if (ptr == str) {
1406         option_error("invalid numeric parameter '%s' for %s option",
1407                      str, current_option);
1408         return 0;
1409     }
1410     return 1;
1411 }
1412
1413
1414 /*
1415  * int_option - like number_option, but valp is int *,
1416  * the base is assumed to be 0, and *valp is not changed
1417  * if there is an error.
1418  */
1419 int
1420 int_option(char *str, int *valp)
1421 {
1422     u_int32_t v;
1423
1424     if (!number_option(str, &v, 0))
1425         return 0;
1426     *valp = (int) v;
1427     return 1;
1428 }
1429
1430
1431 /*
1432  * The following procedures parse options.
1433  */
1434
1435 /*
1436  * readfile - take commands from a file.
1437  */
1438 static int
1439 readfile(char **argv)
1440 {
1441     return options_from_file(*argv, 1, 1, privileged_option);
1442 }
1443
1444 /*
1445  * callfile - take commands from /etc/ppp/peers/<name>.
1446  * Name may not contain /../, start with / or ../, or end in /..
1447  */
1448 static int
1449 callfile(char **argv)
1450 {
1451     char *fname, *arg, *p;
1452     int l, ok;
1453
1454     arg = *argv;
1455     ok = 1;
1456     if (arg[0] == '/' || arg[0] == 0)
1457         ok = 0;
1458     else {
1459         for (p = arg; *p != 0; ) {
1460             if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1461                 ok = 0;
1462                 break;
1463             }
1464             while (*p != '/' && *p != 0)
1465                 ++p;
1466             if (*p == '/')
1467                 ++p;
1468         }
1469     }
1470     if (!ok) {
1471         option_error("call option value may not contain .. or start with /");
1472         return 0;
1473     }
1474
1475     l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1476     if ((fname = (char *) malloc(l)) == NULL)
1477         novm("call file name");
1478     slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1479     script_setenv("CALL_FILE", arg, 0);
1480
1481     ok = options_from_file(fname, 1, 1, 1);
1482
1483     free(fname);
1484     return ok;
1485 }
1486
1487 #ifdef PPP_WITH_FILTER
1488 /*
1489  * setpassfilter - Set the pass filter for packets
1490  */
1491 static int
1492 setpassfilter(char **argv)
1493 {
1494     pcap_t *pc;
1495     int ret = 1;
1496
1497     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1498     if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1499         option_error("error in pass-filter expression: %s\n",
1500                      pcap_geterr(pc));
1501         ret = 0;
1502     }
1503     pcap_close(pc);
1504
1505     return ret;
1506 }
1507
1508 /*
1509  * setactivefilter - Set the active filter for packets
1510  */
1511 static int
1512 setactivefilter(char **argv)
1513 {
1514     pcap_t *pc;
1515     int ret = 1;
1516
1517     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1518     if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1519         option_error("error in active-filter expression: %s\n",
1520                      pcap_geterr(pc));
1521         ret = 0;
1522     }
1523     pcap_close(pc);
1524
1525     return ret;
1526 }
1527 #endif
1528
1529 /*
1530  * setdomain - Set domain name to append to hostname 
1531  */
1532 static int
1533 setdomain(char **argv)
1534 {
1535     gethostname(hostname, MAXNAMELEN);
1536     if (**argv != 0) {
1537         if (**argv != '.')
1538             strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1539         domain = hostname + strlen(hostname);
1540         strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1541     }
1542     hostname[MAXNAMELEN-1] = 0;
1543     return (1);
1544 }
1545
1546 static int
1547 setlogfile(char **argv)
1548 {
1549     int fd, err;
1550     uid_t euid;
1551
1552     euid = geteuid();
1553     if (!privileged_option && seteuid(getuid()) == -1) {
1554         option_error("unable to drop permissions to open %s: %m", *argv);
1555         return 0;
1556     }
1557     fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1558     if (fd < 0 && errno == EEXIST)
1559         fd = open(*argv, O_WRONLY | O_APPEND);
1560     err = errno;
1561     if (!privileged_option && seteuid(euid) == -1)
1562         fatal("unable to regain privileges: %m");
1563     if (fd < 0) {
1564         errno = err;
1565         option_error("Can't open log file %s: %m", *argv);
1566         return 0;
1567     }
1568     strlcpy(logfile_name, *argv, sizeof(logfile_name));
1569     if (logfile_fd >= 0)
1570         close(logfile_fd);
1571     logfile_fd = fd;
1572     log_to_fd = fd;
1573     log_default = 0;
1574     return 1;
1575 }
1576
1577 #ifdef PPP_WITH_MAXOCTETS
1578 static int
1579 setmodir(char **argv)
1580 {
1581     if(*argv == NULL)
1582         return 0;
1583     if(!strcmp(*argv,"in")) {
1584         maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1585     } else if (!strcmp(*argv,"out")) {
1586         maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1587     } else if (!strcmp(*argv,"max")) {
1588         maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1589     } else {
1590         maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1591     }
1592     return 1;
1593 }
1594 #endif
1595
1596 #ifdef PPP_WITH_PLUGINS
1597 static int
1598 loadplugin(char **argv)
1599 {
1600     char *arg = *argv;
1601     void *handle;
1602     const char *err;
1603     void (*init)(void);
1604     char *path = arg;
1605     const char *vers;
1606
1607     if (strchr(arg, '/') == 0) {
1608         const char *base = _PATH_PLUGIN;
1609         int l = strlen(base) + strlen(arg) + 2;
1610         path = malloc(l);
1611         if (path == 0)
1612             novm("plugin file path");
1613         strlcpy(path, base, l);
1614         strlcat(path, "/", l);
1615         strlcat(path, arg, l);
1616     }
1617     handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1618     if (handle == 0) {
1619         err = dlerror();
1620         if (err != 0)
1621             option_error("%s", err);
1622         option_error("Couldn't load plugin %s", arg);
1623         goto err;
1624     }
1625     init = (void (*)(void))dlsym(handle, "plugin_init");
1626     if (init == 0) {
1627         option_error("%s has no initialization entry point", arg);
1628         goto errclose;
1629     }
1630     vers = (const char *) dlsym(handle, "pppd_version");
1631     if (vers == 0) {
1632         warn("Warning: plugin %s has no version information", arg);
1633     } else if (strcmp(vers, VERSION) != 0) {
1634         option_error("Plugin %s is for pppd version %s, this is %s",
1635                      arg, vers, VERSION);
1636         goto errclose;
1637     }
1638     info("Plugin %s loaded.", arg);
1639     (*init)();
1640     return 1;
1641
1642  errclose:
1643     dlclose(handle);
1644  err:
1645     if (path != arg)
1646         free(path);
1647     return 0;
1648 }
1649 #endif /* PPP_WITH_PLUGINS */
1650
1651 /*
1652  * Set an environment variable specified by the user.
1653  */
1654 static int
1655 user_setenv(char **argv)
1656 {
1657     char *arg = argv[0];
1658     char *eqp;
1659     struct userenv *uep, **insp;
1660
1661     if ((eqp = strchr(arg, '=')) == NULL) {
1662         option_error("missing = in name=value: %s", arg);
1663         return 0;
1664     }
1665     if (eqp == arg) {
1666         option_error("missing variable name: %s", arg);
1667         return 0;
1668     }
1669     for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1670         int nlen = strlen(uep->ue_name);
1671         if (nlen == (eqp - arg) &&
1672             strncmp(arg, uep->ue_name, nlen) == 0)
1673             break;
1674     }
1675     /* Ignore attempts by unprivileged users to override privileged sources */
1676     if (uep != NULL && !privileged_option && uep->ue_priv)
1677         return 1;
1678     /* The name never changes, so allocate it with the structure */
1679     if (uep == NULL) {
1680         uep = malloc(sizeof (*uep) + (eqp-arg));
1681         strncpy(uep->ue_name, arg, eqp-arg);
1682         uep->ue_name[eqp-arg] = '\0';
1683         uep->ue_next = NULL;
1684         insp = &userenv_list;
1685         while (*insp != NULL)
1686             insp = &(*insp)->ue_next;
1687         *insp = uep;
1688     } else {
1689         struct userenv *uep2;
1690         for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1691             if (uep2 != uep && !uep2->ue_isset)
1692                 break;
1693         }
1694         if (uep2 == NULL && !uep->ue_isset)
1695             find_option("unset")->flags |= OPT_NOPRINT;
1696         free(uep->ue_value);
1697     }
1698     uep->ue_isset = 1;
1699     uep->ue_priv = privileged_option;
1700     uep->ue_source = option_source;
1701     uep->ue_value = strdup(eqp + 1);
1702     curopt->flags &= ~OPT_NOPRINT;
1703     return 1;
1704 }
1705
1706 static void
1707 user_setprint(option_t *opt, printer_func printer, void *arg)
1708 {
1709     struct userenv *uep, *uepnext;
1710
1711     uepnext = userenv_list;
1712     while (uepnext != NULL && !uepnext->ue_isset)
1713         uepnext = uepnext->ue_next;
1714     while ((uep = uepnext) != NULL) {
1715         uepnext = uep->ue_next;
1716         while (uepnext != NULL && !uepnext->ue_isset)
1717             uepnext = uepnext->ue_next;
1718         (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1719         if (uepnext != NULL)
1720             (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1721         else
1722             opt->source = uep->ue_source;
1723     }
1724 }
1725
1726 static int
1727 user_unsetenv(char **argv)
1728 {
1729     struct userenv *uep, **insp;
1730     char *arg = argv[0];
1731
1732     if (strchr(arg, '=') != NULL) {
1733         option_error("unexpected = in name: %s", arg);
1734         return 0;
1735     }
1736     if (*arg == '\0') {
1737         option_error("missing variable name for unset");
1738         return 0;
1739     }
1740     for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1741         if (strcmp(arg, uep->ue_name) == 0)
1742             break;
1743     }
1744     /* Ignore attempts by unprivileged users to override privileged sources */
1745     if (uep != NULL && !privileged_option && uep->ue_priv)
1746         return 1;
1747     /* The name never changes, so allocate it with the structure */
1748     if (uep == NULL) {
1749         uep = malloc(sizeof (*uep) + strlen(arg));
1750         strcpy(uep->ue_name, arg);
1751         uep->ue_next = NULL;
1752         insp = &userenv_list;
1753         while (*insp != NULL)
1754             insp = &(*insp)->ue_next;
1755         *insp = uep;
1756     } else {
1757         struct userenv *uep2;
1758         for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1759             if (uep2 != uep && uep2->ue_isset)
1760                 break;
1761         }
1762         if (uep2 == NULL && uep->ue_isset)
1763             find_option("set")->flags |= OPT_NOPRINT;
1764         free(uep->ue_value);
1765     }
1766     uep->ue_isset = 0;
1767     uep->ue_priv = privileged_option;
1768     uep->ue_source = option_source;
1769     uep->ue_value = NULL;
1770     curopt->flags &= ~OPT_NOPRINT;
1771     return 1;
1772 }
1773
1774 static void
1775 user_unsetprint(option_t *opt, printer_func printer, void *arg)
1776 {
1777     struct userenv *uep, *uepnext;
1778
1779     uepnext = userenv_list;
1780     while (uepnext != NULL && uepnext->ue_isset)
1781         uepnext = uepnext->ue_next;
1782     while ((uep = uepnext) != NULL) {
1783         uepnext = uep->ue_next;
1784         while (uepnext != NULL && uepnext->ue_isset)
1785             uepnext = uepnext->ue_next;
1786         (*printer)(arg, "%s", uep->ue_name);
1787         if (uepnext != NULL)
1788             (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1789         else
1790             opt->source = uep->ue_source;
1791     }
1792 }