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