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