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