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