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