]> git.ozlabs.org Git - ppp.git/blob - pppd/options.c
Fix authentication on second time around with multilink and persist
[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
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     uid_t euid;
403     char *argv[MAXARGS];
404     char args[MAXARGS][MAXWORDLEN];
405     char cmd[MAXWORDLEN];
406
407     euid = geteuid();
408     if (check_prot && seteuid(getuid()) == -1) {
409         option_error("unable to drop privileges to open %s: %m", filename);
410         return 0;
411     }
412     f = fopen(filename, "r");
413     err = errno;
414     if (check_prot && seteuid(euid) == -1)
415         fatal("unable to regain privileges");
416     if (f == NULL) {
417         errno = err;
418         if (!must_exist) {
419             if (err != ENOENT && err != ENOTDIR)
420                 warn("Warning: can't open options file %s: %m", filename);
421             return 1;
422         }
423         option_error("Can't open options file %s: %m", filename);
424         return 0;
425     }
426
427     oldpriv = privileged_option;
428     privileged_option = priv;
429     oldsource = option_source;
430     option_source = strdup(filename);
431     if (option_source == NULL)
432         option_source = "file";
433     ret = 0;
434     while (getword(f, cmd, &newline, filename)) {
435         opt = find_option(cmd);
436         if (opt == NULL) {
437             option_error("In file %s: unrecognized option '%s'",
438                          filename, cmd);
439             goto err;
440         }
441         n = n_arguments(opt);
442         for (i = 0; i < n; ++i) {
443             if (!getword(f, args[i], &newline, filename)) {
444                 option_error(
445                         "In file %s: too few parameters for option '%s'",
446                         filename, cmd);
447                 goto err;
448             }
449             argv[i] = args[i];
450         }
451         if (!process_option(opt, cmd, argv))
452             goto err;
453     }
454     ret = 1;
455
456 err:
457     fclose(f);
458     privileged_option = oldpriv;
459     option_source = oldsource;
460     return ret;
461 }
462
463 /*
464  * options_from_user - See if the use has a ~/.ppprc file,
465  * and if so, interpret options from it.
466  */
467 int
468 options_from_user()
469 {
470     char *user, *path, *file;
471     int ret;
472     struct passwd *pw;
473     size_t pl;
474
475     pw = getpwuid(getuid());
476     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
477         return 1;
478     file = _PATH_USEROPT;
479     pl = strlen(user) + strlen(file) + 2;
480     path = malloc(pl);
481     if (path == NULL)
482         novm("init file name");
483     slprintf(path, pl, "%s/%s", user, file);
484     option_priority = OPRIO_CFGFILE;
485     ret = options_from_file(path, 0, 1, privileged);
486     free(path);
487     return ret;
488 }
489
490 /*
491  * options_for_tty - See if an options file exists for the serial
492  * device, and if so, interpret options from it.
493  * We only allow the per-tty options file to override anything from
494  * the command line if it is something that the user can't override
495  * once it has been set by root; this is done by giving configuration
496  * files a lower priority than the command line.
497  */
498 int
499 options_for_tty()
500 {
501     char *dev, *path, *p;
502     int ret;
503     size_t pl;
504
505     dev = devnam;
506     if ((p = strstr(dev, "/dev/")) != NULL)
507         dev = p + 5;
508     if (dev[0] == 0 || strcmp(dev, "tty") == 0)
509         return 1;               /* don't look for /etc/ppp/options.tty */
510     pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
511     path = malloc(pl);
512     if (path == NULL)
513         novm("tty init file name");
514     slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
515     /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
516     for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
517         if (*p == '/')
518             *p = '.';
519     option_priority = OPRIO_CFGFILE;
520     ret = options_from_file(path, 0, 0, 1);
521     free(path);
522     return ret;
523 }
524
525 /*
526  * options_from_list - process a string of options in a wordlist.
527  */
528 int
529 options_from_list(w, priv)
530     struct wordlist *w;
531     int priv;
532 {
533     char *argv[MAXARGS];
534     option_t *opt;
535     int i, n, ret = 0;
536     struct wordlist *w0;
537
538     privileged_option = priv;
539     option_source = "secrets file";
540     option_priority = OPRIO_SECFILE;
541
542     while (w != NULL) {
543         opt = find_option(w->word);
544         if (opt == NULL) {
545             option_error("In secrets file: unrecognized option '%s'",
546                          w->word);
547             goto err;
548         }
549         n = n_arguments(opt);
550         w0 = w;
551         for (i = 0; i < n; ++i) {
552             w = w->next;
553             if (w == NULL) {
554                 option_error(
555                         "In secrets file: too few parameters for option '%s'",
556                         w0->word);
557                 goto err;
558             }
559             argv[i] = w->word;
560         }
561         if (!process_option(opt, w0->word, argv))
562             goto err;
563         w = w->next;
564     }
565     ret = 1;
566
567 err:
568     return ret;
569 }
570
571 /*
572  * match_option - see if this option matches an option_t structure.
573  */
574 static int
575 match_option(name, opt, dowild)
576     char *name;
577     option_t *opt;
578     int dowild;
579 {
580         int (*match) __P((char *, char **, int));
581
582         if (dowild != (opt->type == o_wild))
583                 return 0;
584         if (!dowild)
585                 return strcmp(name, opt->name) == 0;
586         match = (int (*) __P((char *, char **, int))) opt->addr;
587         return (*match)(name, NULL, 0);
588 }
589
590 /*
591  * find_option - scan the option lists for the various protocols
592  * looking for an entry with the given name.
593  * This could be optimized by using a hash table.
594  */
595 static option_t *
596 find_option(name)
597     const char *name;
598 {
599         option_t *opt;
600         struct option_list *list;
601         int i, dowild;
602
603         for (dowild = 0; dowild <= 1; ++dowild) {
604                 for (opt = general_options; opt->name != NULL; ++opt)
605                         if (match_option(name, opt, dowild))
606                                 return opt;
607                 for (opt = auth_options; opt->name != NULL; ++opt)
608                         if (match_option(name, opt, dowild))
609                                 return opt;
610                 for (list = extra_options; list != NULL; list = list->next)
611                         for (opt = list->options; opt->name != NULL; ++opt)
612                                 if (match_option(name, opt, dowild))
613                                         return opt;
614                 for (opt = the_channel->options; opt->name != NULL; ++opt)
615                         if (match_option(name, opt, dowild))
616                                 return opt;
617                 for (i = 0; protocols[i] != NULL; ++i)
618                         if ((opt = protocols[i]->options) != NULL)
619                                 for (; opt->name != NULL; ++opt)
620                                         if (match_option(name, opt, dowild))
621                                                 return opt;
622         }
623         return NULL;
624 }
625
626 /*
627  * process_option - process one new-style option.
628  */
629 static int
630 process_option(opt, cmd, argv)
631     option_t *opt;
632     char *cmd;
633     char **argv;
634 {
635     u_int32_t v;
636     int iv, a;
637     char *sv;
638     int (*parser) __P((char **));
639     int (*wildp) __P((char *, char **, int));
640     char *optopt = (opt->type == o_wild)? "": " option";
641     int prio = option_priority;
642     option_t *mainopt = opt;
643
644     current_option = opt->name;
645     if ((opt->flags & OPT_PRIVFIX) && privileged_option)
646         prio += OPRIO_ROOT;
647     while (mainopt->flags & OPT_PRIOSUB)
648         --mainopt;
649     if (mainopt->flags & OPT_PRIO) {
650         if (prio < mainopt->priority) {
651             /* new value doesn't override old */
652             if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
653                 option_error("%s%s set in %s cannot be overridden\n",
654                              opt->name, optopt, mainopt->source);
655                 return 0;
656             }
657             return 1;
658         }
659         if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
660             warn("%s%s from %s overrides command line",
661                  opt->name, optopt, option_source);
662     }
663
664     if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
665         option_error("%s%s cannot be changed after initialization",
666                      opt->name, optopt);
667         return 0;
668     }
669     if ((opt->flags & OPT_PRIV) && !privileged_option) {
670         option_error("using the %s%s requires root privilege",
671                      opt->name, optopt);
672         return 0;
673     }
674     if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
675         option_error("%s%s is disabled", opt->name, optopt);
676         return 0;
677     }
678     if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
679         option_error("the %s%s may not be changed in %s",
680                      opt->name, optopt, option_source);
681         return 0;
682     }
683
684     switch (opt->type) {
685     case o_bool:
686         v = opt->flags & OPT_VALUE;
687         *(bool *)(opt->addr) = v;
688         if (opt->addr2 && (opt->flags & OPT_A2COPY))
689             *(bool *)(opt->addr2) = v;
690         else if (opt->addr2 && (opt->flags & OPT_A2CLR))
691             *(bool *)(opt->addr2) = 0;
692         else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
693             *(u_char *)(opt->addr2) &= ~v;
694         else if (opt->addr2 && (opt->flags & OPT_A2OR))
695             *(u_char *)(opt->addr2) |= v;
696         break;
697
698     case o_int:
699         iv = 0;
700         if ((opt->flags & OPT_NOARG) == 0) {
701             if (!int_option(*argv, &iv))
702                 return 0;
703             if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
704                  || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
705                 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
706                 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
707                 switch (opt->flags & OPT_LIMITS) {
708                 case OPT_LLIMIT:
709                     option_error("%s value must be%s >= %d",
710                                  opt->name, zok, opt->lower_limit);
711                     break;
712                 case OPT_ULIMIT:
713                     option_error("%s value must be%s <= %d",
714                                  opt->name, zok, opt->upper_limit);
715                     break;
716                 case OPT_LIMITS:
717                     option_error("%s value must be%s between %d and %d",
718                                 opt->name, zok, opt->lower_limit, opt->upper_limit);
719                     break;
720                 }
721                 return 0;
722             }
723         }
724         a = opt->flags & OPT_VALUE;
725         if (a >= 128)
726             a -= 256;           /* sign extend */
727         iv += a;
728         if (opt->flags & OPT_INC)
729             iv += *(int *)(opt->addr);
730         if ((opt->flags & OPT_NOINCR) && !privileged_option) {
731             int oldv = *(int *)(opt->addr);
732             if ((opt->flags & OPT_ZEROINF) ?
733                 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
734                 option_error("%s value cannot be increased", opt->name);
735                 return 0;
736             }
737         }
738         *(int *)(opt->addr) = iv;
739         if (opt->addr2 && (opt->flags & OPT_A2COPY))
740             *(int *)(opt->addr2) = iv;
741         break;
742
743     case o_uint32:
744         if (opt->flags & OPT_NOARG) {
745             v = opt->flags & OPT_VALUE;
746             if (v & 0x80)
747                     v |= 0xffffff00U;
748         } else if (!number_option(*argv, &v, 16))
749             return 0;
750         if (opt->flags & OPT_OR)
751             v |= *(u_int32_t *)(opt->addr);
752         *(u_int32_t *)(opt->addr) = v;
753         if (opt->addr2 && (opt->flags & OPT_A2COPY))
754             *(u_int32_t *)(opt->addr2) = v;
755         break;
756
757     case o_string:
758         if (opt->flags & OPT_STATIC) {
759             strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
760         } else {
761             sv = strdup(*argv);
762             if (sv == NULL)
763                 novm("option argument");
764             *(char **)(opt->addr) = sv;
765         }
766         break;
767
768     case o_special_noarg:
769     case o_special:
770         parser = (int (*) __P((char **))) opt->addr;
771         if (!(*parser)(argv))
772             return 0;
773         if (opt->flags & OPT_A2LIST) {
774             struct option_value *ovp, *pp;
775
776             ovp = malloc(sizeof(*ovp) + strlen(*argv));
777             if (ovp != 0) {
778                 strcpy(ovp->value, *argv);
779                 ovp->source = option_source;
780                 ovp->next = NULL;
781                 if (opt->addr2 == NULL) {
782                     opt->addr2 = ovp;
783                 } else {
784                     for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
785                         ;
786                     pp->next = ovp;
787                 }
788             }
789         }
790         break;
791
792     case o_wild:
793         wildp = (int (*) __P((char *, char **, int))) opt->addr;
794         if (!(*wildp)(cmd, argv, 1))
795             return 0;
796         break;
797     }
798
799     /*
800      * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
801      * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
802      */
803     if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
804                 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
805         *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
806
807     mainopt->source = option_source;
808     mainopt->priority = prio;
809     mainopt->winner = opt - mainopt;
810
811     return 1;
812 }
813
814 /*
815  * override_value - if the option priorities would permit us to
816  * override the value of option, return 1 and update the priority
817  * and source of the option value.  Otherwise returns 0.
818  */
819 int
820 override_value(option, priority, source)
821     const char *option;
822     int priority;
823     const char *source;
824 {
825         option_t *opt;
826
827         opt = find_option(option);
828         if (opt == NULL)
829                 return 0;
830         while (opt->flags & OPT_PRIOSUB)
831                 --opt;
832         if ((opt->flags & OPT_PRIO) && priority < opt->priority)
833                 return 0;
834         opt->priority = priority;
835         opt->source = source;
836         opt->winner = -1;
837         return 1;
838 }
839
840 /*
841  * n_arguments - tell how many arguments an option takes
842  */
843 static int
844 n_arguments(opt)
845     option_t *opt;
846 {
847         return (opt->type == o_bool || opt->type == o_special_noarg
848                 || (opt->flags & OPT_NOARG))? 0: 1;
849 }
850
851 /*
852  * add_options - add a list of options to the set we grok.
853  */
854 void
855 add_options(opt)
856     option_t *opt;
857 {
858     struct option_list *list;
859
860     list = malloc(sizeof(*list));
861     if (list == 0)
862         novm("option list entry");
863     list->options = opt;
864     list->next = extra_options;
865     extra_options = list;
866 }
867
868 /*
869  * check_options - check that options are valid and consistent.
870  */
871 void
872 check_options()
873 {
874         if (logfile_fd >= 0 && logfile_fd != log_to_fd)
875                 close(logfile_fd);
876 }
877
878 /*
879  * print_option - print out an option and its value
880  */
881 static void
882 print_option(opt, mainopt, printer, arg)
883     option_t *opt, *mainopt;
884     void (*printer) __P((void *, char *, ...));
885     void *arg;
886 {
887         int i, v;
888         char *p;
889
890         if (opt->flags & OPT_NOPRINT)
891                 return;
892         switch (opt->type) {
893         case o_bool:
894                 v = opt->flags & OPT_VALUE;
895                 if (*(bool *)opt->addr != v)
896                         /* this can happen legitimately, e.g. lock
897                            option turned off for default device */
898                         break;
899                 printer(arg, "%s", opt->name);
900                 break;
901         case o_int:
902                 v = opt->flags & OPT_VALUE;
903                 if (v >= 128)
904                         v -= 256;
905                 i = *(int *)opt->addr;
906                 if (opt->flags & OPT_NOARG) {
907                         printer(arg, "%s", opt->name);
908                         if (i != v) {
909                                 if (opt->flags & OPT_INC) {
910                                         for (; i > v; i -= v)
911                                                 printer(arg, " %s", opt->name);
912                                 } else
913                                         printer(arg, " # oops: %d not %d\n",
914                                                 i, v);
915                         }
916                 } else {
917                         printer(arg, "%s %d", opt->name, i);
918                 }
919                 break;
920         case o_uint32:
921                 printer(arg, "%s", opt->name);
922                 if ((opt->flags & OPT_NOARG) == 0)
923                         printer(arg, " %x", *(u_int32_t *)opt->addr);
924                 break;
925
926         case o_string:
927                 if (opt->flags & OPT_HIDE) {
928                         p = "??????";
929                 } else {
930                         p = (char *) opt->addr;
931                         if ((opt->flags & OPT_STATIC) == 0)
932                                 p = *(char **)p;
933                 }
934                 printer(arg, "%s %q", opt->name, p);
935                 break;
936
937         case o_special:
938         case o_special_noarg:
939         case o_wild:
940                 if (opt->type != o_wild) {
941                         printer(arg, "%s", opt->name);
942                         if (n_arguments(opt) == 0)
943                                 break;
944                         printer(arg, " ");
945                 }
946                 if (opt->flags & OPT_A2PRINTER) {
947                         void (*oprt) __P((option_t *,
948                                           void ((*)__P((void *, char *, ...))),
949                                           void *));
950                         oprt = (void (*) __P((option_t *,
951                                          void ((*)__P((void *, char *, ...))),
952                                          void *)))opt->addr2;
953                         (*oprt)(opt, printer, arg);
954                 } else if (opt->flags & OPT_A2STRVAL) {
955                         p = (char *) opt->addr2;
956                         if ((opt->flags & OPT_STATIC) == 0)
957                                 p = *(char **)p;
958                         printer("%q", p);
959                 } else if (opt->flags & OPT_A2LIST) {
960                         struct option_value *ovp;
961
962                         ovp = (struct option_value *) opt->addr2;
963                         for (;;) {
964                                 printer(arg, "%q", ovp->value);
965                                 if ((ovp = ovp->next) == NULL)
966                                         break;
967                                 printer(arg, "\t\t# (from %s)\n%s ",
968                                         ovp->source, opt->name);
969                         }
970                 } else {
971                         printer(arg, "xxx # [don't know how to print value]");
972                 }
973                 break;
974
975         default:
976                 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
977                 break;
978         }
979         printer(arg, "\t\t# (from %s)\n", mainopt->source);
980 }
981
982 /*
983  * print_option_list - print out options in effect from an
984  * array of options.
985  */
986 static void
987 print_option_list(opt, printer, arg)
988     option_t *opt;
989     void (*printer) __P((void *, char *, ...));
990     void *arg;
991 {
992         while (opt->name != NULL) {
993                 if (opt->priority != OPRIO_DEFAULT
994                     && opt->winner != (short int) -1)
995                         print_option(opt + opt->winner, opt, printer, arg);
996                 do {
997                         ++opt;
998                 } while (opt->flags & OPT_PRIOSUB);
999         }
1000 }
1001
1002 /*
1003  * print_options - print out what options are in effect.
1004  */
1005 void
1006 print_options(printer, arg)
1007     void (*printer) __P((void *, char *, ...));
1008     void *arg;
1009 {
1010         struct option_list *list;
1011         int i;
1012
1013         printer(arg, "pppd options in effect:\n");
1014         print_option_list(general_options, printer, arg);
1015         print_option_list(auth_options, printer, arg);
1016         for (list = extra_options; list != NULL; list = list->next)
1017                 print_option_list(list->options, printer, arg);
1018         print_option_list(the_channel->options, printer, arg);
1019         for (i = 0; protocols[i] != NULL; ++i)
1020                 print_option_list(protocols[i]->options, printer, arg);
1021 }
1022
1023 /*
1024  * usage - print out a message telling how to use the program.
1025  */
1026 static void
1027 usage()
1028 {
1029     if (phase == PHASE_INITIALIZE)
1030         fprintf(stderr, usage_string, VERSION, progname);
1031 }
1032
1033 /*
1034  * showhelp - print out usage message and exit.
1035  */
1036 static int
1037 showhelp(argv)
1038     char **argv;
1039 {
1040     if (phase == PHASE_INITIALIZE) {
1041         usage();
1042         exit(0);
1043     }
1044     return 0;
1045 }
1046
1047 /*
1048  * showversion - print out the version number and exit.
1049  */
1050 static int
1051 showversion(argv)
1052     char **argv;
1053 {
1054     if (phase == PHASE_INITIALIZE) {
1055         fprintf(stderr, "pppd version %s\n", VERSION);
1056         exit(0);
1057     }
1058     return 0;
1059 }
1060
1061 /*
1062  * option_error - print a message about an error in an option.
1063  * The message is logged, and also sent to
1064  * stderr if phase == PHASE_INITIALIZE.
1065  */
1066 void
1067 option_error __V((char *fmt, ...))
1068 {
1069     va_list args;
1070     char buf[1024];
1071
1072 #if defined(__STDC__)
1073     va_start(args, fmt);
1074 #else
1075     char *fmt;
1076     va_start(args);
1077     fmt = va_arg(args, char *);
1078 #endif
1079     vslprintf(buf, sizeof(buf), fmt, args);
1080     va_end(args);
1081     if (phase == PHASE_INITIALIZE)
1082         fprintf(stderr, "%s: %s\n", progname, buf);
1083     syslog(LOG_ERR, "%s", buf);
1084 }
1085
1086 #if 0
1087 /*
1088  * readable - check if a file is readable by the real user.
1089  */
1090 int
1091 readable(fd)
1092     int fd;
1093 {
1094     uid_t uid;
1095     int i;
1096     struct stat sbuf;
1097
1098     uid = getuid();
1099     if (uid == 0)
1100         return 1;
1101     if (fstat(fd, &sbuf) != 0)
1102         return 0;
1103     if (sbuf.st_uid == uid)
1104         return sbuf.st_mode & S_IRUSR;
1105     if (sbuf.st_gid == getgid())
1106         return sbuf.st_mode & S_IRGRP;
1107     for (i = 0; i < ngroups; ++i)
1108         if (sbuf.st_gid == groups[i])
1109             return sbuf.st_mode & S_IRGRP;
1110     return sbuf.st_mode & S_IROTH;
1111 }
1112 #endif
1113
1114 /*
1115  * Read a word from a file.
1116  * Words are delimited by white-space or by quotes (" or ').
1117  * Quotes, white-space and \ may be escaped with \.
1118  * \<newline> is ignored.
1119  */
1120 int
1121 getword(f, word, newlinep, filename)
1122     FILE *f;
1123     char *word;
1124     int *newlinep;
1125     char *filename;
1126 {
1127     int c, len, escape;
1128     int quoted, comment;
1129     int value, digit, got, n;
1130
1131 #define isoctal(c) ((c) >= '0' && (c) < '8')
1132
1133     *newlinep = 0;
1134     len = 0;
1135     escape = 0;
1136     comment = 0;
1137
1138     /*
1139      * First skip white-space and comments.
1140      */
1141     for (;;) {
1142         c = getc(f);
1143         if (c == EOF)
1144             break;
1145
1146         /*
1147          * A newline means the end of a comment; backslash-newline
1148          * is ignored.  Note that we cannot have escape && comment.
1149          */
1150         if (c == '\n') {
1151             if (!escape) {
1152                 *newlinep = 1;
1153                 comment = 0;
1154             } else
1155                 escape = 0;
1156             continue;
1157         }
1158
1159         /*
1160          * Ignore characters other than newline in a comment.
1161          */
1162         if (comment)
1163             continue;
1164
1165         /*
1166          * If this character is escaped, we have a word start.
1167          */
1168         if (escape)
1169             break;
1170
1171         /*
1172          * If this is the escape character, look at the next character.
1173          */
1174         if (c == '\\') {
1175             escape = 1;
1176             continue;
1177         }
1178
1179         /*
1180          * If this is the start of a comment, ignore the rest of the line.
1181          */
1182         if (c == '#') {
1183             comment = 1;
1184             continue;
1185         }
1186
1187         /*
1188          * A non-whitespace character is the start of a word.
1189          */
1190         if (!isspace(c))
1191             break;
1192     }
1193
1194     /*
1195      * Save the delimiter for quoted strings.
1196      */
1197     if (!escape && (c == '"' || c == '\'')) {
1198         quoted = c;
1199         c = getc(f);
1200     } else
1201         quoted = 0;
1202
1203     /*
1204      * Process characters until the end of the word.
1205      */
1206     while (c != EOF) {
1207         if (escape) {
1208             /*
1209              * This character is escaped: backslash-newline is ignored,
1210              * various other characters indicate particular values
1211              * as for C backslash-escapes.
1212              */
1213             escape = 0;
1214             if (c == '\n') {
1215                 c = getc(f);
1216                 continue;
1217             }
1218
1219             got = 0;
1220             switch (c) {
1221             case 'a':
1222                 value = '\a';
1223                 break;
1224             case 'b':
1225                 value = '\b';
1226                 break;
1227             case 'f':
1228                 value = '\f';
1229                 break;
1230             case 'n':
1231                 value = '\n';
1232                 break;
1233             case 'r':
1234                 value = '\r';
1235                 break;
1236             case 's':
1237                 value = ' ';
1238                 break;
1239             case 't':
1240                 value = '\t';
1241                 break;
1242
1243             default:
1244                 if (isoctal(c)) {
1245                     /*
1246                      * \ddd octal sequence
1247                      */
1248                     value = 0;
1249                     for (n = 0; n < 3 && isoctal(c); ++n) {
1250                         value = (value << 3) + (c & 07);
1251                         c = getc(f);
1252                     }
1253                     got = 1;
1254                     break;
1255                 }
1256
1257                 if (c == 'x') {
1258                     /*
1259                      * \x<hex_string> sequence
1260                      */
1261                     value = 0;
1262                     c = getc(f);
1263                     for (n = 0; n < 2 && isxdigit(c); ++n) {
1264                         digit = toupper(c) - '0';
1265                         if (digit > 10)
1266                             digit += '0' + 10 - 'A';
1267                         value = (value << 4) + digit;
1268                         c = getc (f);
1269                     }
1270                     got = 1;
1271                     break;
1272                 }
1273
1274                 /*
1275                  * Otherwise the character stands for itself.
1276                  */
1277                 value = c;
1278                 break;
1279             }
1280
1281             /*
1282              * Store the resulting character for the escape sequence.
1283              */
1284             if (len < MAXWORDLEN-1)
1285                 word[len] = value;
1286             ++len;
1287
1288             if (!got)
1289                 c = getc(f);
1290             continue;
1291
1292         }
1293
1294         /*
1295          * Not escaped: see if we've reached the end of the word.
1296          */
1297         if (quoted) {
1298             if (c == quoted)
1299                 break;
1300         } else {
1301             if (isspace(c) || c == '#') {
1302                 ungetc (c, f);
1303                 break;
1304             }
1305         }
1306
1307         /*
1308          * Backslash starts an escape sequence.
1309          */
1310         if (c == '\\') {
1311             escape = 1;
1312             c = getc(f);
1313             continue;
1314         }
1315
1316         /*
1317          * An ordinary character: store it in the word and get another.
1318          */
1319         if (len < MAXWORDLEN-1)
1320             word[len] = c;
1321         ++len;
1322
1323         c = getc(f);
1324     }
1325
1326     /*
1327      * End of the word: check for errors.
1328      */
1329     if (c == EOF) {
1330         if (ferror(f)) {
1331             if (errno == 0)
1332                 errno = EIO;
1333             option_error("Error reading %s: %m", filename);
1334             die(1);
1335         }
1336         /*
1337          * If len is zero, then we didn't find a word before the
1338          * end of the file.
1339          */
1340         if (len == 0)
1341             return 0;
1342     }
1343
1344     /*
1345      * Warn if the word was too long, and append a terminating null.
1346      */
1347     if (len >= MAXWORDLEN) {
1348         option_error("warning: word in file %s too long (%.20s...)",
1349                      filename, word);
1350         len = MAXWORDLEN - 1;
1351     }
1352     word[len] = 0;
1353
1354     return 1;
1355
1356 #undef isoctal
1357
1358 }
1359
1360 /*
1361  * number_option - parse an unsigned numeric parameter for an option.
1362  */
1363 static int
1364 number_option(str, valp, base)
1365     char *str;
1366     u_int32_t *valp;
1367     int base;
1368 {
1369     char *ptr;
1370
1371     *valp = strtoul(str, &ptr, base);
1372     if (ptr == str) {
1373         option_error("invalid numeric parameter '%s' for %s option",
1374                      str, current_option);
1375         return 0;
1376     }
1377     return 1;
1378 }
1379
1380
1381 /*
1382  * int_option - like number_option, but valp is int *,
1383  * the base is assumed to be 0, and *valp is not changed
1384  * if there is an error.
1385  */
1386 int
1387 int_option(str, valp)
1388     char *str;
1389     int *valp;
1390 {
1391     u_int32_t v;
1392
1393     if (!number_option(str, &v, 0))
1394         return 0;
1395     *valp = (int) v;
1396     return 1;
1397 }
1398
1399
1400 /*
1401  * The following procedures parse options.
1402  */
1403
1404 /*
1405  * readfile - take commands from a file.
1406  */
1407 static int
1408 readfile(argv)
1409     char **argv;
1410 {
1411     return options_from_file(*argv, 1, 1, privileged_option);
1412 }
1413
1414 /*
1415  * callfile - take commands from /etc/ppp/peers/<name>.
1416  * Name may not contain /../, start with / or ../, or end in /..
1417  */
1418 static int
1419 callfile(argv)
1420     char **argv;
1421 {
1422     char *fname, *arg, *p;
1423     int l, ok;
1424
1425     arg = *argv;
1426     ok = 1;
1427     if (arg[0] == '/' || arg[0] == 0)
1428         ok = 0;
1429     else {
1430         for (p = arg; *p != 0; ) {
1431             if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1432                 ok = 0;
1433                 break;
1434             }
1435             while (*p != '/' && *p != 0)
1436                 ++p;
1437             if (*p == '/')
1438                 ++p;
1439         }
1440     }
1441     if (!ok) {
1442         option_error("call option value may not contain .. or start with /");
1443         return 0;
1444     }
1445
1446     l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1447     if ((fname = (char *) malloc(l)) == NULL)
1448         novm("call file name");
1449     slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1450
1451     ok = options_from_file(fname, 1, 1, 1);
1452
1453     free(fname);
1454     return ok;
1455 }
1456
1457 #ifdef PPP_FILTER
1458 /*
1459  * setpassfilter - Set the pass filter for packets
1460  */
1461 static int
1462 setpassfilter(argv)
1463     char **argv;
1464 {
1465     pcap_t *pc;
1466     int ret = 1;
1467
1468     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1469     if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1470         option_error("error in pass-filter expression: %s\n",
1471                      pcap_geterr(pc));
1472         ret = 0;
1473     }
1474     pcap_close(pc);
1475
1476     return ret;
1477 }
1478
1479 /*
1480  * setactivefilter - Set the active filter for packets
1481  */
1482 static int
1483 setactivefilter(argv)
1484     char **argv;
1485 {
1486     pcap_t *pc;
1487     int ret = 1;
1488
1489     pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1490     if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1491         option_error("error in active-filter expression: %s\n",
1492                      pcap_geterr(pc));
1493         ret = 0;
1494     }
1495     pcap_close(pc);
1496
1497     return ret;
1498 }
1499 #endif
1500
1501 /*
1502  * setdomain - Set domain name to append to hostname 
1503  */
1504 static int
1505 setdomain(argv)
1506     char **argv;
1507 {
1508     gethostname(hostname, MAXNAMELEN);
1509     if (**argv != 0) {
1510         if (**argv != '.')
1511             strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1512         domain = hostname + strlen(hostname);
1513         strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1514     }
1515     hostname[MAXNAMELEN-1] = 0;
1516     return (1);
1517 }
1518
1519 static int
1520 setlogfile(argv)
1521     char **argv;
1522 {
1523     int fd, err;
1524     uid_t euid;
1525
1526     euid = geteuid();
1527     if (!privileged_option && seteuid(getuid()) == -1) {
1528         option_error("unable to drop permissions to open %s: %m", *argv);
1529         return 0;
1530     }
1531     fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1532     if (fd < 0 && errno == EEXIST)
1533         fd = open(*argv, O_WRONLY | O_APPEND);
1534     err = errno;
1535     if (!privileged_option && seteuid(euid) == -1)
1536         fatal("unable to regain privileges: %m");
1537     if (fd < 0) {
1538         errno = err;
1539         option_error("Can't open log file %s: %m", *argv);
1540         return 0;
1541     }
1542     strlcpy(logfile_name, *argv, sizeof(logfile_name));
1543     if (logfile_fd >= 0)
1544         close(logfile_fd);
1545     logfile_fd = fd;
1546     log_to_fd = fd;
1547     log_default = 0;
1548     return 1;
1549 }
1550
1551 #ifdef MAXOCTETS
1552 static int
1553 setmodir(argv)
1554     char **argv;
1555 {
1556     if(*argv == NULL)
1557         return 0;
1558     if(!strcmp(*argv,"in")) {
1559         maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1560     } else if (!strcmp(*argv,"out")) {
1561         maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1562     } else if (!strcmp(*argv,"max")) {
1563         maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1564     } else {
1565         maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1566     }
1567     return 1;
1568 }
1569 #endif
1570
1571 #ifdef PLUGIN
1572 static int
1573 loadplugin(argv)
1574     char **argv;
1575 {
1576     char *arg = *argv;
1577     void *handle;
1578     const char *err;
1579     void (*init) __P((void));
1580     char *path = arg;
1581     const char *vers;
1582
1583     if (strchr(arg, '/') == 0) {
1584         const char *base = _PATH_PLUGIN;
1585         int l = strlen(base) + strlen(arg) + 2;
1586         path = malloc(l);
1587         if (path == 0)
1588             novm("plugin file path");
1589         strlcpy(path, base, l);
1590         strlcat(path, "/", l);
1591         strlcat(path, arg, l);
1592     }
1593     handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1594     if (handle == 0) {
1595         err = dlerror();
1596         if (err != 0)
1597             option_error("%s", err);
1598         option_error("Couldn't load plugin %s", arg);
1599         goto err;
1600     }
1601     init = (void (*)(void))dlsym(handle, "plugin_init");
1602     if (init == 0) {
1603         option_error("%s has no initialization entry point", arg);
1604         goto errclose;
1605     }
1606     vers = (const char *) dlsym(handle, "pppd_version");
1607     if (vers == 0) {
1608         warn("Warning: plugin %s has no version information", arg);
1609     } else if (strcmp(vers, VERSION) != 0) {
1610         option_error("Plugin %s is for pppd version %s, this is %s",
1611                      arg, vers, VERSION);
1612         goto errclose;
1613     }
1614     info("Plugin %s loaded.", arg);
1615     (*init)();
1616     return 1;
1617
1618  errclose:
1619     dlclose(handle);
1620  err:
1621     if (path != arg)
1622         free(path);
1623     return 0;
1624 }
1625 #endif /* PLUGIN */