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