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