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