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