]> git.ozlabs.org Git - ppp.git/blob - pppd/options.c
pppd.8: Document netmask option
[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-private.h"
81 #include "options.h"
82 #include "upap.h"
83 #include "pathnames.h"
84
85 #if defined(ultrix) || defined(NeXT)
86 char *strdup(char *);
87 #endif
88
89
90 struct option_value {
91     struct option_value *next;
92     const char *source;
93     char value[1];
94 };
95
96 /*
97  * Option variables and default values.
98  */
99 int     debug = 0;              /* Debug flag */
100 int     kdebugflag = 0;         /* Tell kernel to print debug messages */
101 int     default_device = 1;     /* Using /dev/tty or equivalent */
102 bool    nodetach = 0;           /* Don't detach from controlling tty */
103 bool    updetach = 0;           /* Detach once link is up */
104 bool    master_detach;          /* Detach when we're (only) multilink master */
105 #ifdef SYSTEMD
106 bool    up_sdnotify = 0;        /* Notify systemd once link is up */
107 #endif
108 int     maxconnect = 0;         /* Maximum connect time */
109 char    user[MAXNAMELEN];       /* Username for PAP */
110 char    passwd[MAXSECRETLEN];   /* Password for PAP */
111 bool    persist = 0;            /* Reopen link after it goes down */
112 char    our_name[MAXNAMELEN];   /* Our name for authentication purposes */
113 bool    demand = 0;             /* do dial-on-demand */
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 session_limit_dir_t maxoctets_dir = PPP_OCTETS_DIRECTION_SUM; /* default - sum of traffic */
144 int maxoctets_timeout = 1;   /* default 1 second */ 
145
146
147 extern struct option 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 struct option *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(struct option *, printer_func, void *);
190 static int user_unsetenv(char **);
191 static void user_unsetprint(struct option *, printer_func, void *);
192
193 static struct option *find_option(char *name);
194 static int process_option(struct option *, char *, char **);
195 static int n_arguments(struct option *);
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     struct option *options;
203     struct option_list *next;
204 };
205
206 static struct option_list *extra_options = NULL;
207
208 /*
209  * Valid arguments.
210  */
211 struct option 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 int
399 ppp_get_max_idle_time()
400 {
401     return idle_time_limit;
402 }
403
404 void
405 ppp_set_max_idle_time(unsigned int max)
406 {
407     idle_time_limit = max;
408 }
409
410 int
411 ppp_get_max_connect_time()
412 {
413     return maxconnect;
414 }
415
416 void
417 ppp_set_max_connect_time(unsigned int max)
418 {
419     maxconnect = max;
420 }
421
422 void
423 ppp_set_session_limit(unsigned int octets)
424 {
425     maxoctets = octets;
426 }
427
428 void
429 ppp_set_session_limit_dir(unsigned int dir)
430 {
431     if (dir > 4)
432         dir = PPP_OCTETS_DIRECTION_SUM;
433     maxoctets_dir = (session_limit_dir_t) dir;
434 }
435
436 bool
437 debug_on()
438 {
439     return !!debug;
440 }
441
442 int
443 ppp_get_path(ppp_path_t type, char *buf, size_t bufsz)
444 {
445     const char *path;
446
447     if (buf && bufsz > 0) {
448         switch (type) {
449         case PPP_DIR_LOG:
450             path = PPP_PATH_VARLOG;
451             break;
452         case PPP_DIR_RUNTIME:
453             path = PPP_PATH_VARRUN;
454             break;
455 #ifdef PPP_WITH_PLUGINS
456         case PPP_DIR_PLUGIN:
457             path = PPP_PATH_PLUGIN;
458             break;
459 #endif
460         case PPP_DIR_CONF:
461             path = PPP_PATH_CONFDIR;
462             break;
463         }
464         return strlcpy(buf, path, bufsz);
465     }
466     return -1;
467 }
468
469 int
470 ppp_get_filepath(ppp_path_t type, const char *name, char *buf, size_t bufsz)
471 {
472     const char *path;
473
474     if (buf && bufsz > 0) {
475         switch (type) {
476         case PPP_DIR_LOG:
477             path = PPP_PATH_VARLOG;
478             break;
479         case PPP_DIR_RUNTIME:
480             path = PPP_PATH_VARRUN;
481             break;
482 #ifdef PPP_WITH_PLUGINS
483         case PPP_DIR_PLUGIN:
484             path = PPP_PATH_PLUGIN;
485             break;
486 #endif
487         case PPP_DIR_CONF:
488             path = PPP_PATH_CONFDIR;
489             break;
490         }
491         return slprintf(buf, bufsz, "%s/%s", path, name);
492     }
493     return -1;
494 }
495
496 bool ppp_persist()
497 {
498     return !!persist;
499 }
500
501 /*
502  * parse_args - parse a string of arguments from the command line.
503  */
504 int
505 parse_args(int argc, char **argv)
506 {
507     char *arg;
508     struct option *opt;
509     int n;
510
511     privileged_option = privileged;
512     option_source = "command line";
513     option_priority = OPRIO_CMDLINE;
514     while (argc > 0) {
515         arg = *argv++;
516         --argc;
517         opt = find_option(arg);
518         if (opt == NULL) {
519             ppp_option_error("unrecognized option '%s'", arg);
520             usage();
521             return 0;
522         }
523         n = n_arguments(opt);
524         if (argc < n) {
525             ppp_option_error("too few parameters for option %s", arg);
526             return 0;
527         }
528         if (!process_option(opt, arg, argv))
529             return 0;
530         argc -= n;
531         argv += n;
532     }
533     return 1;
534 }
535
536 /*
537  * options_from_file - Read a string of options from a file,
538  * and interpret them.
539  */
540 int
541 ppp_options_from_file(char *filename, int must_exist, int check_prot, int priv)
542 {
543     FILE *f;
544     int i, newline, ret, err;
545     struct option *opt;
546     int oldpriv, n;
547     char *oldsource;
548     uid_t euid;
549     char *argv[MAXARGS];
550     char args[MAXARGS][MAXWORDLEN];
551     char cmd[MAXWORDLEN];
552
553     euid = geteuid();
554     if (check_prot && seteuid(getuid()) == -1) {
555         ppp_option_error("unable to drop privileges to open %s: %m", filename);
556         return 0;
557     }
558     f = fopen(filename, "r");
559     err = errno;
560     if (check_prot && seteuid(euid) == -1)
561         fatal("unable to regain privileges");
562     if (f == NULL) {
563         errno = err;
564         if (!must_exist) {
565             if (err != ENOENT && err != ENOTDIR)
566                 warn("Warning: can't open options file %s: %m", filename);
567             return 1;
568         }
569         ppp_option_error("Can't open options file %s: %m", filename);
570         return 0;
571     }
572
573     oldpriv = privileged_option;
574     privileged_option = priv;
575     oldsource = option_source;
576     option_source = strdup(filename);
577     if (option_source == NULL)
578         option_source = "file";
579     ret = 0;
580     while (getword(f, cmd, &newline, filename)) {
581         opt = find_option(cmd);
582         if (opt == NULL) {
583             ppp_option_error("In file %s: unrecognized option '%s'",
584                          filename, cmd);
585             goto err;
586         }
587         n = n_arguments(opt);
588         for (i = 0; i < n; ++i) {
589             if (!getword(f, args[i], &newline, filename)) {
590                 ppp_option_error(
591                         "In file %s: too few parameters for option '%s'",
592                         filename, cmd);
593                 goto err;
594             }
595             argv[i] = args[i];
596         }
597         if (!process_option(opt, cmd, argv))
598             goto err;
599     }
600     ret = 1;
601
602 err:
603     fclose(f);
604     free(option_source);
605     privileged_option = oldpriv;
606     option_source = oldsource;
607     return ret;
608 }
609
610 /*
611  * options_from_user - See if the use has a ~/.ppprc file,
612  * and if so, interpret options from it.
613  */
614 int
615 options_from_user(void)
616 {
617     char *user, *path, *file;
618     int ret;
619     struct passwd *pw;
620     size_t pl;
621
622     pw = getpwuid(getuid());
623     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
624         return 1;
625     file = PPP_PATH_USEROPT;
626     pl = strlen(user) + strlen(file) + 2;
627     path = malloc(pl);
628     if (path == NULL)
629         novm("init file name");
630     slprintf(path, pl, "%s/%s", user, file);
631     option_priority = OPRIO_CFGFILE;
632     ret = ppp_options_from_file(path, 0, 1, privileged);
633     free(path);
634     return ret;
635 }
636
637 /*
638  * options_for_tty - See if an options file exists for the serial
639  * device, and if so, interpret options from it.
640  * We only allow the per-tty options file to override anything from
641  * the command line if it is something that the user can't override
642  * once it has been set by root; this is done by giving configuration
643  * files a lower priority than the command line.
644  */
645 int
646 options_for_tty(void)
647 {
648     char *dev, *path, *p;
649     int ret;
650     size_t pl;
651
652     dev = devnam;
653     if ((p = strstr(dev, "/dev/")) != NULL)
654         dev = p + 5;
655     if (dev[0] == 0 || strcmp(dev, "tty") == 0)
656         return 1;               /* don't look for /etc/ppp/options.tty */
657     pl = strlen(PPP_PATH_TTYOPT) + strlen(dev) + 1;
658     path = malloc(pl);
659     if (path == NULL)
660         novm("tty init file name");
661     slprintf(path, pl, "%s%s", PPP_PATH_TTYOPT, dev);
662     /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
663     for (p = path + strlen(PPP_PATH_TTYOPT); *p != 0; ++p)
664         if (*p == '/')
665             *p = '.';
666     option_priority = OPRIO_CFGFILE;
667     ret = ppp_options_from_file(path, 0, 0, 1);
668     free(path);
669     return ret;
670 }
671
672 /*
673  * options_from_list - process a string of options in a wordlist.
674  */
675 int
676 options_from_list(struct wordlist *w, int priv)
677 {
678     char *argv[MAXARGS];
679     struct option *opt;
680     int i, n, ret = 0;
681     struct wordlist *w0;
682
683     privileged_option = priv;
684     option_source = "secrets file";
685     option_priority = OPRIO_SECFILE;
686
687     while (w != NULL) {
688         opt = find_option(w->word);
689         if (opt == NULL) {
690             ppp_option_error("In secrets file: unrecognized option '%s'",
691                          w->word);
692             goto err;
693         }
694         n = n_arguments(opt);
695         w0 = w;
696         for (i = 0; i < n; ++i) {
697             w = w->next;
698             if (w == NULL) {
699                 ppp_option_error(
700                         "In secrets file: too few parameters for option '%s'",
701                         w0->word);
702                 goto err;
703             }
704             argv[i] = w->word;
705         }
706         if (!process_option(opt, w0->word, argv))
707             goto err;
708         w = w->next;
709     }
710     ret = 1;
711
712 err:
713     return ret;
714 }
715
716 /*
717  * match_option - see if this option matches an option structure.
718  */
719 static int
720 match_option(char *name, struct option *opt, int dowild)
721 {
722         int (*match)(char *, char **, int);
723
724         if (dowild != (opt->type == o_wild))
725                 return 0;
726         if (!dowild)
727                 return strcmp(name, opt->name) == 0;
728         match = (int (*)(char *, char **, int)) opt->addr;
729         return (*match)(name, NULL, 0);
730 }
731
732 /*
733  * find_option - scan the option lists for the various protocols
734  * looking for an entry with the given name.
735  * This could be optimized by using a hash table.
736  */
737 static struct option *
738 find_option(char *name)
739 {
740         struct option *opt;
741         struct option_list *list;
742         int i, dowild;
743
744         for (dowild = 0; dowild <= 1; ++dowild) {
745                 for (opt = general_options; opt->name != NULL; ++opt)
746                         if (match_option(name, opt, dowild))
747                                 return opt;
748                 for (opt = auth_options; opt->name != NULL; ++opt)
749                         if (match_option(name, opt, dowild))
750                                 return opt;
751                 for (list = extra_options; list != NULL; list = list->next)
752                         for (opt = list->options; opt->name != NULL; ++opt)
753                                 if (match_option(name, opt, dowild))
754                                         return opt;
755                 for (opt = the_channel->options; opt->name != NULL; ++opt)
756                         if (match_option(name, opt, dowild))
757                                 return opt;
758                 for (i = 0; protocols[i] != NULL; ++i)
759                         if ((opt = protocols[i]->options) != NULL)
760                                 for (; opt->name != NULL; ++opt)
761                                         if (match_option(name, opt, dowild))
762                                                 return opt;
763         }
764         return NULL;
765 }
766
767 /*
768  * process_option - process one new-style option.
769  */
770 static int
771 process_option(struct option *opt, char *cmd, char **argv)
772 {
773     u_int32_t v;
774     int iv, a;
775     char *sv;
776     int (*parser)(char **);
777     int (*wildp)(char *, char **, int);
778     char *optopt = (opt->type == o_wild)? "": " option";
779     int prio = option_priority;
780     struct option *mainopt = opt;
781
782     current_option = opt->name;
783     if ((opt->flags & OPT_PRIVFIX) && privileged_option)
784         prio += OPRIO_ROOT;
785     while (mainopt->flags & OPT_PRIOSUB)
786         --mainopt;
787     if (mainopt->flags & OPT_PRIO) {
788         if (prio < mainopt->priority) {
789             /* new value doesn't override old */
790             if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
791                 ppp_option_error("%s%s set in %s cannot be overridden\n",
792                              opt->name, optopt, mainopt->source);
793                 return 0;
794             }
795             return 1;
796         }
797         if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
798             warn("%s%s from %s overrides command line",
799                  opt->name, optopt, option_source);
800     }
801
802     if ((opt->flags & OPT_INITONLY) && !in_phase(PHASE_INITIALIZE)) {
803         ppp_option_error("%s%s cannot be changed after initialization",
804                      opt->name, optopt);
805         return 0;
806     }
807     if ((opt->flags & OPT_PRIV) && !privileged_option) {
808         ppp_option_error("using the %s%s requires root privilege",
809                      opt->name, optopt);
810         return 0;
811     }
812     if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
813         ppp_option_error("%s%s is disabled", opt->name, optopt);
814         return 0;
815     }
816     if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
817         ppp_option_error("the %s%s may not be changed in %s",
818                      opt->name, optopt, option_source);
819         return 0;
820     }
821
822     switch (opt->type) {
823     case o_bool:
824         v = opt->flags & OPT_VALUE;
825         *(bool *)(opt->addr) = v;
826         if (opt->addr2 && (opt->flags & OPT_A2COPY))
827             *(bool *)(opt->addr2) = v;
828         else if (opt->addr2 && (opt->flags & OPT_A2CLR))
829             *(bool *)(opt->addr2) = 0;
830         else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
831             *(u_char *)(opt->addr2) &= ~v;
832         else if (opt->addr2 && (opt->flags & OPT_A2OR))
833             *(u_char *)(opt->addr2) |= v;
834         break;
835
836     case o_int:
837         iv = 0;
838         if ((opt->flags & OPT_NOARG) == 0) {
839             if (!ppp_int_option(*argv, &iv))
840                 return 0;
841             if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
842                  || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
843                 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
844                 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
845                 switch (opt->flags & OPT_LIMITS) {
846                 case OPT_LLIMIT:
847                     ppp_option_error("%s value must be%s >= %d",
848                                  opt->name, zok, opt->lower_limit);
849                     break;
850                 case OPT_ULIMIT:
851                     ppp_option_error("%s value must be%s <= %d",
852                                  opt->name, zok, opt->upper_limit);
853                     break;
854                 case OPT_LIMITS:
855                     ppp_option_error("%s value must be%s between %d and %d",
856                                 opt->name, zok, opt->lower_limit, opt->upper_limit);
857                     break;
858                 }
859                 return 0;
860             }
861         }
862         a = opt->flags & OPT_VALUE;
863         if (a >= 128)
864             a -= 256;           /* sign extend */
865         iv += a;
866         if (opt->flags & OPT_INC)
867             iv += *(int *)(opt->addr);
868         if ((opt->flags & OPT_NOINCR) && !privileged_option) {
869             int oldv = *(int *)(opt->addr);
870             if ((opt->flags & OPT_ZEROINF) ?
871                 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
872                 ppp_option_error("%s value cannot be increased", opt->name);
873                 return 0;
874             }
875         }
876         *(int *)(opt->addr) = iv;
877         if (opt->addr2 && (opt->flags & OPT_A2COPY))
878             *(int *)(opt->addr2) = iv;
879         break;
880
881     case o_uint32:
882         if (opt->flags & OPT_NOARG) {
883             v = opt->flags & OPT_VALUE;
884             if (v & 0x80)
885                     v |= 0xffffff00U;
886         } else if (!number_option(*argv, &v, 16))
887             return 0;
888         if (opt->flags & OPT_OR)
889             v |= *(u_int32_t *)(opt->addr);
890         *(u_int32_t *)(opt->addr) = v;
891         if (opt->addr2 && (opt->flags & OPT_A2COPY))
892             *(u_int32_t *)(opt->addr2) = v;
893         break;
894
895     case o_string:
896         if (opt->flags & OPT_STATIC) {
897             strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
898         } else {
899             char **optptr = (char **)(opt->addr);
900             sv = strdup(*argv);
901             if (sv == NULL)
902                 novm("option argument");
903             if (*optptr)
904                 free(*optptr);
905             *optptr = sv;
906         }
907         /* obfuscate original argument for things like password */
908         if (opt->flags & OPT_HIDE) {
909             memset(*argv, '?', strlen(*argv));
910             *argv = "********";
911         }
912         break;
913
914     case o_special_noarg:
915     case o_special:
916         parser = (int (*)(char **)) opt->addr;
917         curopt = opt;
918         if (!(*parser)(argv))
919             return 0;
920         if (opt->flags & OPT_A2LIST) {
921             struct option_value *ovp, *pp;
922
923             ovp = malloc(sizeof(*ovp) + strlen(*argv));
924             if (ovp != 0) {
925                 strcpy(ovp->value, *argv);
926                 ovp->source = option_source;
927                 ovp->next = NULL;
928                 if (opt->addr2 == NULL) {
929                     opt->addr2 = ovp;
930                 } else {
931                     for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
932                         ;
933                     pp->next = ovp;
934                 }
935             }
936         }
937         break;
938
939     case o_wild:
940         wildp = (int (*)(char *, char **, int)) opt->addr;
941         if (!(*wildp)(cmd, argv, 1))
942             return 0;
943         break;
944     }
945
946     /*
947      * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
948      * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
949      */
950     if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
951                 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
952         *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
953
954     mainopt->source = option_source;
955     mainopt->priority = prio;
956     mainopt->winner = opt - mainopt;
957
958     return 1;
959 }
960
961 /*
962  * override_value - if the option priorities would permit us to
963  * override the value of option, return 1 and update the priority
964  * and source of the option value.  Otherwise returns 0.
965  */
966 int
967 override_value(char *option, int priority, const char *source)
968 {
969         struct option *opt;
970
971         opt = find_option(option);
972         if (opt == NULL)
973                 return 0;
974         while (opt->flags & OPT_PRIOSUB)
975                 --opt;
976         if ((opt->flags & OPT_PRIO) && priority < opt->priority)
977                 return 0;
978         opt->priority = priority;
979         opt->source = source;
980         opt->winner = -1;
981         return 1;
982 }
983
984 /*
985  * n_arguments - tell how many arguments an option takes
986  */
987 static int
988 n_arguments(struct option *opt)
989 {
990         return (opt->type == o_bool || opt->type == o_special_noarg
991                 || (opt->flags & OPT_NOARG))? 0: 1;
992 }
993
994 /*
995  * add_options - add a list of options to the set we grok.
996  */
997 void
998 ppp_add_options(struct option *opt)
999 {
1000     struct option_list *list;
1001
1002     list = malloc(sizeof(*list));
1003     if (list == 0)
1004         novm("option list entry");
1005     list->options = opt;
1006     list->next = extra_options;
1007     extra_options = list;
1008 }
1009
1010 /*
1011  * check_options - check that options are valid and consistent.
1012  */
1013 void
1014 check_options(void)
1015 {
1016         if (logfile_fd >= 0 && logfile_fd != log_to_fd)
1017                 close(logfile_fd);
1018 }
1019
1020 /*
1021  * print_option - print out an option and its value
1022  */
1023 static void
1024 print_option(struct option *opt, struct option *mainopt, printer_func printer, void *arg)
1025 {
1026         int i, v;
1027         char *p;
1028
1029         if (opt->flags & OPT_NOPRINT)
1030                 return;
1031         switch (opt->type) {
1032         case o_bool:
1033                 v = opt->flags & OPT_VALUE;
1034                 if (*(bool *)opt->addr != v)
1035                         /* this can happen legitimately, e.g. lock
1036                            option turned off for default device */
1037                         break;
1038                 printer(arg, "%s", opt->name);
1039                 break;
1040         case o_int:
1041                 v = opt->flags & OPT_VALUE;
1042                 if (v >= 128)
1043                         v -= 256;
1044                 i = *(int *)opt->addr;
1045                 if (opt->flags & OPT_NOARG) {
1046                         printer(arg, "%s", opt->name);
1047                         if (i != v) {
1048                                 if (opt->flags & OPT_INC) {
1049                                         for (; i > v; i -= v)
1050                                                 printer(arg, " %s", opt->name);
1051                                 } else
1052                                         printer(arg, " # oops: %d not %d\n",
1053                                                 i, v);
1054                         }
1055                 } else {
1056                         printer(arg, "%s %d", opt->name, i);
1057                 }
1058                 break;
1059         case o_uint32:
1060                 printer(arg, "%s", opt->name);
1061                 if ((opt->flags & OPT_NOARG) == 0)
1062                         printer(arg, " %x", *(u_int32_t *)opt->addr);
1063                 break;
1064
1065         case o_string:
1066                 if (opt->flags & OPT_HIDE) {
1067                         p = "??????";
1068                 } else {
1069                         p = (char *) opt->addr;
1070                         if ((opt->flags & OPT_STATIC) == 0)
1071                                 p = *(char **)p;
1072                 }
1073                 printer(arg, "%s %q", opt->name, p);
1074                 break;
1075
1076         case o_special:
1077         case o_special_noarg:
1078         case o_wild:
1079                 if (opt->type != o_wild) {
1080                         printer(arg, "%s", opt->name);
1081                         if (n_arguments(opt) == 0)
1082                                 break;
1083                         printer(arg, " ");
1084                 }
1085                 if (opt->flags & OPT_A2PRINTER) {
1086                         void (*oprt)(struct option *, printer_func, void *);
1087                         oprt = (void (*)(struct option *, printer_func, void *))
1088                                 opt->addr2;
1089                         (*oprt)(opt, printer, arg);
1090                 } else if (opt->flags & OPT_A2STRVAL) {
1091                         p = (char *) opt->addr2;
1092                         if ((opt->flags & OPT_STATIC) == 0)
1093                                 p = *(char **)p;
1094                         printer(arg, "%q", p);
1095                 } else if (opt->flags & OPT_A2LIST) {
1096                         struct option_value *ovp;
1097
1098                         ovp = (struct option_value *) opt->addr2;
1099                         for (;;) {
1100                                 printer(arg, "%q", ovp->value);
1101                                 if ((ovp = ovp->next) == NULL)
1102                                         break;
1103                                 printer(arg, "\t\t# (from %s)\n%s ",
1104                                         ovp->source, opt->name);
1105                         }
1106                 } else {
1107                         printer(arg, "xxx # [don't know how to print value]");
1108                 }
1109                 break;
1110
1111         default:
1112                 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1113                 break;
1114         }
1115         printer(arg, "\t\t# (from %s)\n", mainopt->source);
1116 }
1117
1118 /*
1119  * print_option_list - print out options in effect from an
1120  * array of options.
1121  */
1122 static void
1123 print_option_list(struct option *opt, printer_func printer, void *arg)
1124 {
1125         while (opt->name != NULL) {
1126                 if (opt->priority != OPRIO_DEFAULT
1127                     && opt->winner != (short int) -1)
1128                         print_option(opt + opt->winner, opt, printer, arg);
1129                 do {
1130                         ++opt;
1131                 } while (opt->flags & OPT_PRIOSUB);
1132         }
1133 }
1134
1135 /*
1136  * print_options - print out what options are in effect.
1137  */
1138 void
1139 print_options(printer_func printer, void *arg)
1140 {
1141         struct option_list *list;
1142         int i;
1143
1144         printer(arg, "pppd options in effect:\n");
1145         print_option_list(general_options, printer, arg);
1146         print_option_list(auth_options, printer, arg);
1147         for (list = extra_options; list != NULL; list = list->next)
1148                 print_option_list(list->options, printer, arg);
1149         print_option_list(the_channel->options, printer, arg);
1150         for (i = 0; protocols[i] != NULL; ++i)
1151                 print_option_list(protocols[i]->options, printer, arg);
1152 }
1153
1154 /*
1155  * usage - print out a message telling how to use the program.
1156  */
1157 static void
1158 usage(void)
1159 {
1160     FILE *fp = stderr;
1161     if (in_phase(PHASE_INITIALIZE)) {
1162         fprintf(fp, "%s v%s\n", PACKAGE_NAME, PACKAGE_VERSION);
1163         fprintf(fp, "Copyright (C) 1999-2022 Paul Mackerras, and others. All rights reserved.\n\n");
1164
1165
1166         fprintf(fp, "License BSD: The 3 clause BSD license <https://opensource.org/licenses/BSD-3-Clause>\n");
1167         fprintf(fp, "This is free software: you are free to change and redistribute it.\n");
1168         fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n\n");
1169
1170         fprintf(fp, "Report Bugs:\n   %s\n\n", PACKAGE_BUGREPORT);
1171         fprintf(fp, "Usage: %s [ options ], where options are:\n", progname);
1172         fprintf(fp, "   <device>        Communicate over the named device\n");
1173         fprintf(fp, "   <speed>         Set the baud rate to <speed>\n");
1174         fprintf(fp, "   <loc>:<rem>     Set the local and/or remote interface IP\n");
1175         fprintf(fp, "                   addresses.  Either one may be omitted.\n");
1176         fprintf(fp, "   asyncmap <n>    Set the desired async map to hex <n>\n");
1177         fprintf(fp, "   auth            Require authentication from peer\n");
1178         fprintf(fp, "   connect <p>     Invoke shell command <p> to set up the serial line\n");
1179         fprintf(fp, "   crtscts         Use hardware RTS/CTS flow control\n");
1180         fprintf(fp, "   defaultroute    Add default route through interface\n");
1181         fprintf(fp, "   file <f>        Take options from file <f>\n");
1182         fprintf(fp, "   modem           Use modem control lines\n");
1183         fprintf(fp, "   mru <n>         Set MRU value to <n> for negotiation\n");
1184         fprintf(fp, "   show-options    Display an extended list of options\n");
1185         fprintf(fp, "See pppd(8) for more options.\n");
1186     }
1187 }
1188
1189 /*
1190  * showhelp - print out usage message and exit.
1191  */
1192 static int
1193 showhelp(char **argv)
1194 {
1195     if (in_phase(PHASE_INITIALIZE)) {
1196         usage();
1197         exit(0);
1198     }
1199     return 0;
1200 }
1201
1202 /*
1203  * showversion - print out the version number and exit.
1204  */
1205 static int
1206 showversion(char **argv)
1207 {
1208     if (in_phase(PHASE_INITIALIZE)) {
1209         fprintf(stdout, "pppd version %s\n", VERSION);
1210         exit(0);
1211     }
1212     return 0;
1213 }
1214
1215 /*
1216  * Print a set of options including the name of the group of options
1217  */
1218 static void
1219 showopts_list(FILE *fp, const char *title, struct option *list, ...)
1220 {
1221         struct option *opt = list;
1222     va_list varg;
1223
1224     if (opt && opt->name) {
1225         va_start(varg, list);
1226         vfprintf(fp, title, varg);
1227         fprintf(fp, ":\n");
1228         va_end(varg);
1229
1230         do {
1231             fprintf(fp, "    %-22s %s\n", opt->name, opt->description?:"");
1232             opt++;
1233         } while (opt && opt->name);
1234
1235         fprintf(fp, "\n");
1236     }
1237 }
1238
1239 /*
1240  * Dumps the list of available options
1241  */
1242 void
1243 showopts(void)
1244 {
1245     struct option_list *list;
1246     FILE *fp = stderr;
1247     int i = 0;
1248
1249     showopts_list(fp, "General Options",
1250             general_options);
1251
1252     showopts_list(fp, "Authentication Options",
1253             auth_options);
1254
1255     for (list = extra_options; list != NULL; list = list->next)
1256                 showopts_list(fp, "Extra Options", list->options);
1257
1258     showopts_list(fp, "Channel Options",
1259             the_channel->options);
1260
1261     for (i = 0; protocols[i] != NULL; ++i) {
1262         if (protocols[i]->options != NULL) {
1263             showopts_list(fp, "%s Options",
1264                     protocols[i]->options,
1265                     protocols[i]->name);
1266         }
1267     }
1268 }
1269
1270 /*
1271  * ppp_option_error - print a message about an error in an option.
1272  * The message is logged, and also sent to
1273  * stderr if in_phase(PHASE_INITIALIZE).
1274  */
1275 void
1276 ppp_option_error(char *fmt, ...)
1277 {
1278     va_list args;
1279     char buf[1024];
1280
1281     va_start(args, fmt);
1282     vslprintf(buf, sizeof(buf), fmt, args);
1283     va_end(args);
1284     if (in_phase(PHASE_INITIALIZE))
1285         fprintf(stderr, "%s: %s\n", progname, buf);
1286     syslog(LOG_ERR, "%s", buf);
1287 }
1288
1289 #if 0
1290 /*
1291  * readable - check if a file is readable by the real user.
1292  */
1293 int
1294 readable(int fd)
1295 {
1296     uid_t uid;
1297     int i;
1298     struct stat sbuf;
1299
1300     uid = getuid();
1301     if (uid == 0)
1302         return 1;
1303     if (fstat(fd, &sbuf) != 0)
1304         return 0;
1305     if (sbuf.st_uid == uid)
1306         return sbuf.st_mode & S_IRUSR;
1307     if (sbuf.st_gid == getgid())
1308         return sbuf.st_mode & S_IRGRP;
1309     for (i = 0; i < ngroups; ++i)
1310         if (sbuf.st_gid == groups[i])
1311             return sbuf.st_mode & S_IRGRP;
1312     return sbuf.st_mode & S_IROTH;
1313 }
1314 #endif
1315
1316 /*
1317  * Read a word from a file.
1318  * Words are delimited by white-space or by quotes (" or ').
1319  * Quotes, white-space and \ may be escaped with \.
1320  * \<newline> is ignored.
1321  */
1322 int
1323 getword(FILE *f, char *word, int *newlinep, char *filename)
1324 {
1325     int c, len, escape;
1326     int quoted, comment;
1327     int value, digit, got, n;
1328
1329 #define isoctal(c) ((c) >= '0' && (c) < '8')
1330
1331     *newlinep = 0;
1332     len = 0;
1333     escape = 0;
1334     comment = 0;
1335     quoted = 0;
1336
1337     /*
1338      * First skip white-space and comments.
1339      */
1340     for (;;) {
1341         c = getc(f);
1342         if (c == EOF)
1343             break;
1344
1345         /*
1346          * A newline means the end of a comment; backslash-newline
1347          * is ignored.  Note that we cannot have escape && comment.
1348          */
1349         if (c == '\n') {
1350             if (!escape) {
1351                 *newlinep = 1;
1352                 comment = 0;
1353             } else
1354                 escape = 0;
1355             continue;
1356         }
1357
1358         /*
1359          * Ignore characters other than newline in a comment.
1360          */
1361         if (comment)
1362             continue;
1363
1364         /*
1365          * If this character is escaped, we have a word start.
1366          */
1367         if (escape)
1368             break;
1369
1370         /*
1371          * If this is the escape character, look at the next character.
1372          */
1373         if (c == '\\') {
1374             escape = 1;
1375             continue;
1376         }
1377
1378         /*
1379          * If this is the start of a comment, ignore the rest of the line.
1380          */
1381         if (c == '#') {
1382             comment = 1;
1383             continue;
1384         }
1385
1386         /*
1387          * A non-whitespace character is the start of a word.
1388          */
1389         if (!isspace(c))
1390             break;
1391     }
1392
1393     /*
1394      * Process characters until the end of the word.
1395      */
1396     while (c != EOF) {
1397         if (escape) {
1398             /*
1399              * This character is escaped: backslash-newline is ignored,
1400              * various other characters indicate particular values
1401              * as for C backslash-escapes.
1402              */
1403             escape = 0;
1404             if (c == '\n') {
1405                 c = getc(f);
1406                 continue;
1407             }
1408
1409             got = 0;
1410             switch (c) {
1411             case 'a':
1412                 value = '\a';
1413                 break;
1414             case 'b':
1415                 value = '\b';
1416                 break;
1417             case 'f':
1418                 value = '\f';
1419                 break;
1420             case 'n':
1421                 value = '\n';
1422                 break;
1423             case 'r':
1424                 value = '\r';
1425                 break;
1426             case 's':
1427                 value = ' ';
1428                 break;
1429             case 't':
1430                 value = '\t';
1431                 break;
1432
1433             default:
1434                 if (isoctal(c)) {
1435                     /*
1436                      * \ddd octal sequence
1437                      */
1438                     value = 0;
1439                     for (n = 0; n < 3 && isoctal(c); ++n) {
1440                         value = (value << 3) + (c & 07);
1441                         c = getc(f);
1442                     }
1443                     got = 1;
1444                     break;
1445                 }
1446
1447                 if (c == 'x') {
1448                     /*
1449                      * \x<hex_string> sequence
1450                      */
1451                     value = 0;
1452                     c = getc(f);
1453                     for (n = 0; n < 2 && isxdigit(c); ++n) {
1454                         digit = toupper(c) - '0';
1455                         if (digit > 10)
1456                             digit += '0' + 10 - 'A';
1457                         value = (value << 4) + digit;
1458                         c = getc (f);
1459                     }
1460                     got = 1;
1461                     break;
1462                 }
1463
1464                 /*
1465                  * Otherwise the character stands for itself.
1466                  */
1467                 value = c;
1468                 break;
1469             }
1470
1471             /*
1472              * Store the resulting character for the escape sequence.
1473              */
1474             if (len < MAXWORDLEN) {
1475                 word[len] = value;
1476                 ++len;
1477             }
1478
1479             if (!got)
1480                 c = getc(f);
1481             continue;
1482         }
1483
1484         /*
1485          * Backslash starts a new escape sequence.
1486          */
1487         if (c == '\\') {
1488             escape = 1;
1489             c = getc(f);
1490             continue;
1491         }
1492
1493         /*
1494          * Not escaped: check for the start or end of a quoted
1495          * section and see if we've reached the end of the word.
1496          */
1497         if (quoted) {
1498             if (c == quoted) {
1499                 quoted = 0;
1500                 c = getc(f);
1501                 continue;
1502             }
1503         } else if (c == '"' || c == '\'') {
1504             quoted = c;
1505             c = getc(f);
1506             continue;
1507         } else if (isspace(c) || c == '#') {
1508             ungetc (c, f);
1509             break;
1510         }
1511
1512         /*
1513          * An ordinary character: store it in the word and get another.
1514          */
1515         if (len < MAXWORDLEN) {
1516             word[len] = c;
1517             ++len;
1518         }
1519
1520         c = getc(f);
1521     }
1522     word[MAXWORDLEN-1] = 0;     /* make sure word is null-terminated */
1523
1524     /*
1525      * End of the word: check for errors.
1526      */
1527     if (c == EOF) {
1528         if (ferror(f)) {
1529             if (errno == 0)
1530                 errno = EIO;
1531             ppp_option_error("Error reading %s: %m", filename);
1532             die(1);
1533         }
1534         /*
1535          * If len is zero, then we didn't find a word before the
1536          * end of the file.
1537          */
1538         if (len == 0)
1539             return 0;
1540         if (quoted)
1541             ppp_option_error("warning: quoted word runs to end of file (%.20s...)",
1542                          filename, word);
1543     }
1544
1545     /*
1546      * Warn if the word was too long, and append a terminating null.
1547      */
1548     if (len >= MAXWORDLEN) {
1549         ppp_option_error("warning: word in file %s too long (%.20s...)",
1550                      filename, word);
1551         len = MAXWORDLEN - 1;
1552     }
1553     word[len] = 0;
1554
1555     return 1;
1556
1557 #undef isoctal
1558
1559 }
1560
1561 /*
1562  * number_option - parse an unsigned numeric parameter for an option.
1563  */
1564 static int
1565 number_option(char *str, u_int32_t *valp, int base)
1566 {
1567     char *ptr;
1568
1569     *valp = strtoul(str, &ptr, base);
1570     if (ptr == str) {
1571         ppp_option_error("invalid numeric parameter '%s' for %s option",
1572                      str, current_option);
1573         return 0;
1574     }
1575     return 1;
1576 }
1577
1578
1579 /*
1580  * int_option - like number_option, but valp is int *,
1581  * the base is assumed to be 0, and *valp is not changed
1582  * if there is an error.
1583  */
1584 int
1585 ppp_int_option(char *str, int *valp)
1586 {
1587     u_int32_t v;
1588
1589     if (!number_option(str, &v, 0))
1590         return 0;
1591     *valp = (int) v;
1592     return 1;
1593 }
1594
1595
1596 /*
1597  * The following procedures parse options.
1598  */
1599
1600 /*
1601  * readfile - take commands from a file.
1602  */
1603 static int
1604 readfile(char **argv)
1605 {
1606     return ppp_options_from_file(*argv, 1, 1, privileged_option);
1607 }
1608
1609 /*
1610  * callfile - take commands from /etc/ppp/peers/<name>.
1611  * Name may not contain /../, start with / or ../, or end in /..
1612  */
1613 static int
1614 callfile(char **argv)
1615 {
1616     char *fname, *arg, *p;
1617     int l, ok;
1618
1619     arg = *argv;
1620     ok = 1;
1621     if (arg[0] == '/' || arg[0] == 0)
1622         ok = 0;
1623     else {
1624         for (p = arg; *p != 0; ) {
1625             if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1626                 ok = 0;
1627                 break;
1628             }
1629             while (*p != '/' && *p != 0)
1630                 ++p;
1631             if (*p == '/')
1632                 ++p;
1633         }
1634     }
1635     if (!ok) {
1636         ppp_option_error("call option value may not contain .. or start with /");
1637         return 0;
1638     }
1639
1640     l = strlen(arg) + strlen(PPP_PATH_PEERFILES) + 1;
1641     if ((fname = (char *) malloc(l)) == NULL)
1642         novm("call file name");
1643     slprintf(fname, l, "%s%s", PPP_PATH_PEERFILES, arg);
1644     ppp_script_setenv("CALL_FILE", arg, 0);
1645
1646     ok = ppp_options_from_file(fname, 1, 1, 1);
1647
1648     free(fname);
1649     return ok;
1650 }
1651
1652 #ifdef PPP_WITH_FILTER
1653 /*
1654  * setpassfilter - Set the pass filter for packets
1655  */
1656 static int
1657 setpassfilter(char **argv)
1658 {
1659     pcap_t *pc;
1660     int ret = 1;
1661
1662     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1663     if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1664         ppp_option_error("error in pass-filter expression: %s\n",
1665                      pcap_geterr(pc));
1666         ret = 0;
1667     }
1668     pcap_close(pc);
1669
1670     return ret;
1671 }
1672
1673 /*
1674  * setactivefilter - Set the active filter for packets
1675  */
1676 static int
1677 setactivefilter(char **argv)
1678 {
1679     pcap_t *pc;
1680     int ret = 1;
1681
1682     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1683     if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1684         ppp_option_error("error in active-filter expression: %s\n",
1685                      pcap_geterr(pc));
1686         ret = 0;
1687     }
1688     pcap_close(pc);
1689
1690     return ret;
1691 }
1692 #endif
1693
1694 /*
1695  * setdomain - Set domain name to append to hostname 
1696  */
1697 static int
1698 setdomain(char **argv)
1699 {
1700     gethostname(hostname, MAXNAMELEN);
1701     if (**argv != 0) {
1702         if (**argv != '.')
1703             strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1704         domain = hostname + strlen(hostname);
1705         strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1706     }
1707     hostname[MAXNAMELEN-1] = 0;
1708     return (1);
1709 }
1710
1711 static int
1712 setlogfile(char **argv)
1713 {
1714     int fd, err;
1715     uid_t euid;
1716
1717     euid = geteuid();
1718     if (!privileged_option && seteuid(getuid()) == -1) {
1719         ppp_option_error("unable to drop permissions to open %s: %m", *argv);
1720         return 0;
1721     }
1722     fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1723     if (fd < 0 && errno == EEXIST)
1724         fd = open(*argv, O_WRONLY | O_APPEND);
1725     err = errno;
1726     if (!privileged_option && seteuid(euid) == -1)
1727         fatal("unable to regain privileges: %m");
1728     if (fd < 0) {
1729         errno = err;
1730         ppp_option_error("Can't open log file %s: %m", *argv);
1731         return 0;
1732     }
1733     strlcpy(logfile_name, *argv, sizeof(logfile_name));
1734     if (logfile_fd >= 0)
1735         close(logfile_fd);
1736     logfile_fd = fd;
1737     log_to_fd = fd;
1738     log_default = 0;
1739     return 1;
1740 }
1741
1742 static int
1743 setmodir(char **argv)
1744 {
1745     if(*argv == NULL)
1746         return 0;
1747     if(!strcmp(*argv,"in")) {
1748         maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1749     } else if (!strcmp(*argv,"out")) {
1750         maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1751     } else if (!strcmp(*argv,"max")) {
1752         maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1753     } else {
1754         maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1755     }
1756     return 1;
1757 }
1758
1759 #ifdef PPP_WITH_PLUGINS
1760 static int
1761 loadplugin(char **argv)
1762 {
1763     char *arg = *argv;
1764     void *handle;
1765     const char *err;
1766     void (*init)(void);
1767     char *path = arg;
1768     const char *vers;
1769
1770     if (strchr(arg, '/') == 0) {
1771         const char *base = PPP_PATH_PLUGIN;
1772         int l = strlen(base) + strlen(arg) + 2;
1773         path = malloc(l);
1774         if (path == 0)
1775             novm("plugin file path");
1776         strlcpy(path, base, l);
1777         strlcat(path, "/", l);
1778         strlcat(path, arg, l);
1779     }
1780     handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1781     if (handle == 0) {
1782         err = dlerror();
1783         if (err != 0)
1784             ppp_option_error("%s", err);
1785         ppp_option_error("Couldn't load plugin %s", arg);
1786         goto err;
1787     }
1788     init = (void (*)(void))dlsym(handle, "plugin_init");
1789     if (init == 0) {
1790         ppp_option_error("%s has no initialization entry point", arg);
1791         goto errclose;
1792     }
1793     vers = (const char *) dlsym(handle, "pppd_version");
1794     if (vers == 0) {
1795         warn("Warning: plugin %s has no version information", arg);
1796     } else if (strcmp(vers, VERSION) != 0) {
1797         ppp_option_error("Plugin %s is for pppd version %s, this is %s",
1798                      arg, vers, VERSION);
1799         goto errclose;
1800     }
1801     info("Plugin %s loaded.", arg);
1802     (*init)();
1803     if (path != arg)
1804         free(path);
1805     return 1;
1806
1807  errclose:
1808     dlclose(handle);
1809  err:
1810     if (path != arg)
1811         free(path);
1812     return 0;
1813 }
1814 #endif /* PPP_WITH_PLUGINS */
1815
1816 /*
1817  * Set an environment variable specified by the user.
1818  */
1819 static int
1820 user_setenv(char **argv)
1821 {
1822     char *arg = argv[0];
1823     char *eqp;
1824     struct userenv *uep, **insp;
1825
1826     if ((eqp = strchr(arg, '=')) == NULL) {
1827         ppp_option_error("missing = in name=value: %s", arg);
1828         return 0;
1829     }
1830     if (eqp == arg) {
1831         ppp_option_error("missing variable name: %s", arg);
1832         return 0;
1833     }
1834     for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1835         int nlen = strlen(uep->ue_name);
1836         if (nlen == (eqp - arg) &&
1837             strncmp(arg, uep->ue_name, nlen) == 0)
1838             break;
1839     }
1840     /* Ignore attempts by unprivileged users to override privileged sources */
1841     if (uep != NULL && !privileged_option && uep->ue_priv)
1842         return 1;
1843     /* The name never changes, so allocate it with the structure */
1844     if (uep == NULL) {
1845         uep = malloc(sizeof (*uep) + (eqp-arg));
1846         if (uep == NULL) {
1847                 novm("environment variable");
1848                 return 1;
1849         }
1850         strncpy(uep->ue_name, arg, eqp-arg);
1851         uep->ue_name[eqp-arg] = '\0';
1852         uep->ue_next = NULL;
1853         insp = &userenv_list;
1854         while (*insp != NULL)
1855             insp = &(*insp)->ue_next;
1856         *insp = uep;
1857     } else {
1858         struct userenv *uep2;
1859         for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1860             if (uep2 != uep && !uep2->ue_isset)
1861                 break;
1862         }
1863         if (uep2 == NULL && !uep->ue_isset)
1864             find_option("unset")->flags |= OPT_NOPRINT;
1865         free(uep->ue_value);
1866     }
1867     uep->ue_isset = 1;
1868     uep->ue_priv = privileged_option;
1869     uep->ue_source = option_source;
1870     uep->ue_value = strdup(eqp + 1);
1871     curopt->flags &= ~OPT_NOPRINT;
1872     return 1;
1873 }
1874
1875 static void
1876 user_setprint(struct option *opt, printer_func printer, void *arg)
1877 {
1878     struct userenv *uep, *uepnext;
1879
1880     uepnext = userenv_list;
1881     while (uepnext != NULL && !uepnext->ue_isset)
1882         uepnext = uepnext->ue_next;
1883     while ((uep = uepnext) != NULL) {
1884         uepnext = uep->ue_next;
1885         while (uepnext != NULL && !uepnext->ue_isset)
1886             uepnext = uepnext->ue_next;
1887         (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1888         if (uepnext != NULL)
1889             (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1890         else
1891             opt->source = uep->ue_source;
1892     }
1893 }
1894
1895 static int
1896 user_unsetenv(char **argv)
1897 {
1898     struct userenv *uep, **insp;
1899     char *arg = argv[0];
1900
1901     if (strchr(arg, '=') != NULL) {
1902         ppp_option_error("unexpected = in name: %s", arg);
1903         return 0;
1904     }
1905     if (*arg == '\0') {
1906         ppp_option_error("missing variable name for unset");
1907         return 0;
1908     }
1909     for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1910         if (strcmp(arg, uep->ue_name) == 0)
1911             break;
1912     }
1913     /* Ignore attempts by unprivileged users to override privileged sources */
1914     if (uep != NULL && !privileged_option && uep->ue_priv)
1915         return 1;
1916     /* The name never changes, so allocate it with the structure */
1917     if (uep == NULL) {
1918         uep = malloc(sizeof (*uep) + strlen(arg));
1919         if (uep == NULL) {
1920                 novm("environment variable");
1921                 return 1;
1922         }
1923         strcpy(uep->ue_name, arg);
1924         uep->ue_next = NULL;
1925         insp = &userenv_list;
1926         while (*insp != NULL)
1927             insp = &(*insp)->ue_next;
1928         *insp = uep;
1929     } else {
1930         struct userenv *uep2;
1931         for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1932             if (uep2 != uep && uep2->ue_isset)
1933                 break;
1934         }
1935         if (uep2 == NULL && uep->ue_isset)
1936             find_option("set")->flags |= OPT_NOPRINT;
1937         free(uep->ue_value);
1938     }
1939     uep->ue_isset = 0;
1940     uep->ue_priv = privileged_option;
1941     uep->ue_source = option_source;
1942     uep->ue_value = NULL;
1943     curopt->flags &= ~OPT_NOPRINT;
1944     return 1;
1945 }
1946
1947 static void
1948 user_unsetprint(struct option *opt, printer_func printer, void *arg)
1949 {
1950     struct userenv *uep, *uepnext;
1951
1952     uepnext = userenv_list;
1953     while (uepnext != NULL && uepnext->ue_isset)
1954         uepnext = uepnext->ue_next;
1955     while ((uep = uepnext) != NULL) {
1956         uepnext = uep->ue_next;
1957         while (uepnext != NULL && uepnext->ue_isset)
1958             uepnext = uepnext->ue_next;
1959         (*printer)(arg, "%s", uep->ue_name);
1960         if (uepnext != NULL)
1961             (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1962         else
1963             opt->source = uep->ue_source;
1964     }
1965 }