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