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