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