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