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