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