Changes to update Tomato RAF.
[tomato.git] / release / src / router / pppd / pppd / options.c
blob28c85a12465cc3f853d859991736d170396ea680
1 /*
2 * options.c - handles option processing for PPP.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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/)."
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.
43 #define RCSID "$Id: options.c,v 1.102 2008/06/15 06:53:06 paulus Exp $"
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
58 #ifdef PPP_FILTER
59 #include <pcap.h>
61 * There have been 3 or 4 different names for this in libpcap CVS, but
62 * this seems to be what they have settled on...
63 * For older versions of libpcap, use DLT_PPP - but that means
64 * we lose the inbound and outbound qualifiers.
66 #ifndef DLT_PPP_PPPD
67 #ifdef DLT_PPP_WITHDIRECTION
68 #define DLT_PPP_PPPD DLT_PPP_WITHDIRECTION
69 #else
70 #define DLT_PPP_PPPD DLT_PPP
71 #endif
72 #endif
73 #endif /* PPP_FILTER */
75 #include "pppd.h"
76 #include "pathnames.h"
78 #if defined(ultrix) || defined(NeXT)
79 char *strdup __P((char *));
80 #endif
81 bool tx_only; /* JYWeng 20031216: idle time counting on tx traffic */
83 static const char rcsid[] = RCSID;
85 struct option_value {
86 struct option_value *next;
87 const char *source;
88 char value[1];
92 * Option variables and default values.
94 bool nochecktime = 0; /* Don't check time */
95 int debug = 0; /* Debug flag */
96 int kdebugflag = 0; /* Tell kernel to print debug messages */
97 int default_device = 1; /* Using /dev/tty or equivalent */
98 char devnam[MAXPATHLEN]; /* Device name */
99 bool nodetach = 0; /* Don't detach from controlling tty */
100 bool updetach = 0; /* Detach once link is up */
101 int maxconnect = 0; /* Maximum connect time */
102 char user[MAXNAMELEN]; /* Username for PAP */
103 char passwd[MAXSECRETLEN]; /* Password for PAP */
104 bool persist = 0; /* Reopen link after it goes down */
105 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
106 bool demand = 0; /* do dial-on-demand */
107 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
108 char *chapseccustom = NULL; /* Custom chap-secrets file */
110 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
111 int holdoff = 30; /* # seconds to pause before reconnecting */
112 bool holdoff_specified; /* true if a holdoff value has been given */
113 int log_to_fd = 1; /* send log messages to this fd too */
114 bool log_default = 1; /* log_to_fd is default (stdout) */
115 int maxfail = 10; /* max # of unsuccessful connection attempts */
116 char linkname[MAXPATHLEN]; /* logical name for link */
117 bool tune_kernel; /* may alter kernel settings */
118 int connect_delay = 1000; /* wait this many ms after connect script */
119 int req_unit = -1; /* requested interface unit */
120 int req_minunit = -1; /* requested minimal interface unit */
121 char path_ipup[MAXPATHLEN]; /* pathname of ip-up script */
122 char path_ipdown[MAXPATHLEN];/* pathname of ip-down script */
123 bool multilink = 0; /* Enable multilink operation */
124 char *bundle_name = NULL; /* bundle name for multilink */
125 bool dump_options; /* print out option values */
126 bool dryrun; /* print out option values and exit */
127 char *domain; /* domain name set by domain option */
128 int child_wait = 5; /* # seconds to wait for children at exit */
130 #ifdef MAXOCTETS
131 unsigned int maxoctets = 0; /* default - no limit */
132 int maxoctets_dir = 0; /* default - sum of traffic */
133 int maxoctets_timeout = 1; /* default 1 second */
134 #endif
137 extern option_t auth_options[];
138 extern struct stat devstat;
140 #ifdef PPP_FILTER
141 struct bpf_program pass_filter;/* Filter program for packets to pass */
142 struct bpf_program active_filter; /* Filter program for link-active pkts */
143 #endif
145 char *current_option; /* the name of the option being parsed */
146 int privileged_option; /* set iff the current option came from root */
147 char *option_source; /* string saying where the option came from */
148 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
149 bool devnam_fixed; /* can no longer change device name */
151 static int logfile_fd = -1; /* fd opened for log file */
152 static char logfile_name[MAXPATHLEN]; /* name of log file */
155 * Prototypes
157 static int setdomain __P((char **));
158 static int readfile __P((char **));
159 static int callfile __P((char **));
160 static int showversion __P((char **));
161 static int showhelp __P((char **));
162 static void usage __P((void));
163 static int setlogfile __P((char **));
164 #ifdef PLUGIN
165 static int loadplugin __P((char **));
166 #endif
168 #ifdef PPP_FILTER
169 static int setpassfilter __P((char **));
170 static int setactivefilter __P((char **));
171 #endif
173 #ifdef MAXOCTETS
174 static int setmodir __P((char **));
175 #endif
177 static option_t *find_option __P((const char *name));
178 static int process_option __P((option_t *, char *, char **));
179 static int n_arguments __P((option_t *));
180 static int number_option __P((char *, u_int32_t *, int));
183 * Structure to store extra lists of options.
185 struct option_list {
186 option_t *options;
187 struct option_list *next;
190 static struct option_list *extra_options = NULL;
193 * Valid arguments.
195 option_t general_options[] = {
196 { "nochecktime", o_bool, &nochecktime,
197 "Don't check time", OPT_PRIO | 1 },
198 { "debug", o_int, &debug,
199 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
200 { "-d", o_int, &debug,
201 "Increase debugging level",
202 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
204 { "kdebug", o_int, &kdebugflag,
205 "Set kernel driver debug level", OPT_PRIO },
207 { "nodetach", o_bool, &nodetach,
208 "Don't detach from controlling tty", OPT_PRIO | 1 },
209 { "-detach", o_bool, &nodetach,
210 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
211 { "updetach", o_bool, &updetach,
212 "Detach from controlling tty once link is up",
213 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
215 { "holdoff", o_int, &holdoff,
216 "Set time in seconds before retrying connection",
217 OPT_PRIO, &holdoff_specified },
219 { "idle", o_int, &idle_time_limit,
220 "Set time in seconds before disconnecting idle link", OPT_PRIO },
222 { "maxconnect", o_int, &maxconnect,
223 "Set connection time limit",
224 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
226 { "domain", o_special, (void *)setdomain,
227 "Add given domain name to hostname",
228 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
230 { "file", o_special, (void *)readfile,
231 "Take options from a file", OPT_NOPRINT },
232 { "call", o_special, (void *)callfile,
233 "Take options from a privileged file", OPT_NOPRINT },
235 { "persist", o_bool, &persist,
236 "Keep on reopening connection after close", OPT_PRIO | 1 },
237 { "nopersist", o_bool, &persist,
238 "Turn off persist option", OPT_PRIOSUB },
240 { "demand", o_bool, &demand,
241 "Dial on demand", OPT_INITONLY | 1, &persist },
243 { "--version", o_special_noarg, (void *)showversion,
244 "Show version number" },
245 { "--help", o_special_noarg, (void *)showhelp,
246 "Show brief listing of options" },
247 { "-h", o_special_noarg, (void *)showhelp,
248 "Show brief listing of options", OPT_ALIAS },
250 { "logfile", o_special, (void *)setlogfile,
251 "Append log messages to this file",
252 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
253 { "logfd", o_int, &log_to_fd,
254 "Send log messages to this file descriptor",
255 OPT_PRIOSUB | OPT_A2CLR, &log_default },
256 { "nolog", o_int, &log_to_fd,
257 "Don't send log messages to any file",
258 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
259 { "nologfd", o_int, &log_to_fd,
260 "Don't send log messages to any file descriptor",
261 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
263 { "linkname", o_string, linkname,
264 "Set logical name for link",
265 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
267 { "maxfail", o_int, &maxfail,
268 "Maximum number of unsuccessful connection attempts to allow",
269 OPT_PRIO },
271 { "ktune", o_bool, &tune_kernel,
272 "Alter kernel settings as necessary", OPT_PRIO | 1 },
273 { "noktune", o_bool, &tune_kernel,
274 "Don't alter kernel settings", OPT_PRIOSUB },
276 { "connect-delay", o_int, &connect_delay,
277 "Maximum time (in ms) to wait after connect script finishes",
278 OPT_PRIO },
280 { "unit", o_int, &req_unit,
281 "PPP interface unit number to use if possible",
282 OPT_PRIO | OPT_LLIMIT, 0, 0 },
283 { "minunit", o_int, &req_minunit,
284 "PPP interface minimal unit number",
285 OPT_PRIO | OPT_LLIMIT, 0, 0 },
287 { "dump", o_bool, &dump_options,
288 "Print out option values after parsing all options", 1 },
289 { "dryrun", o_bool, &dryrun,
290 "Stop after parsing, printing, and checking options", 1 },
292 { "child-timeout", o_int, &child_wait,
293 "Number of seconds to wait for child processes at exit",
294 OPT_PRIO },
296 { "ip-up-script", o_string, path_ipup,
297 "Set pathname of ip-up script",
298 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
299 { "ip-down-script", o_string, path_ipdown,
300 "Set pathname of ip-down script",
301 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
303 #ifdef HAVE_MULTILINK
304 { "multilink", o_bool, &multilink,
305 "Enable multilink operation", OPT_PRIO | 1 },
306 { "mp", o_bool, &multilink,
307 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
308 { "nomultilink", o_bool, &multilink,
309 "Disable multilink operation", OPT_PRIOSUB | 0 },
310 { "nomp", o_bool, &multilink,
311 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
313 { "bundle", o_string, &bundle_name,
314 "Bundle name for multilink", OPT_PRIO },
315 #endif /* HAVE_MULTILINK */
317 #ifdef PLUGIN
318 { "plugin", o_special, (void *)loadplugin,
319 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
320 #endif
322 #ifdef PPP_FILTER
323 { "pass-filter", o_special, setpassfilter,
324 "set filter for packets to pass", OPT_PRIO },
326 { "active-filter", o_special, setactivefilter,
327 "set filter for active pkts", OPT_PRIO },
328 #endif
330 #ifdef MAXOCTETS
331 { "maxoctets", o_int, &maxoctets,
332 "Set connection traffic limit",
333 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
334 { "mo", o_int, &maxoctets,
335 "Set connection traffic limit",
336 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
337 { "mo-direction", o_special, setmodir,
338 "Set direction for limit traffic (sum,in,out,max)" },
339 { "mo-timeout", o_int, &maxoctets_timeout,
340 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
341 #endif
343 /* JYWeng 20031216: add for tx_only option*/
344 { "tx_only", o_bool, &tx_only,
345 "set idle time counting on tx_only or not", 1 },
347 { NULL }
350 #ifndef IMPLEMENTATION
351 #define IMPLEMENTATION ""
352 #endif
354 static char *usage_string = "\
355 pppd version %s\n\
356 Usage: %s [ options ], where options are:\n\
357 <device> Communicate over the named device\n\
358 <speed> Set the baud rate to <speed>\n\
359 <loc>:<rem> Set the local and/or remote interface IP\n\
360 addresses. Either one may be omitted.\n\
361 asyncmap <n> Set the desired async map to hex <n>\n\
362 auth Require authentication from peer\n\
363 connect <p> Invoke shell command <p> to set up the serial line\n\
364 crtscts Use hardware RTS/CTS flow control\n\
365 defaultroute Add default route through interface\n\
366 file <f> Take options from file <f>\n\
367 modem Use modem control lines\n\
368 mru <n> Set MRU value to <n> for negotiation\n\
369 See pppd(8) for more options.\n\
373 * parse_args - parse a string of arguments from the command line.
376 parse_args(argc, argv)
377 int argc;
378 char **argv;
380 char *arg;
381 option_t *opt;
382 int n;
384 privileged_option = privileged;
385 option_source = "command line";
386 option_priority = OPRIO_CMDLINE;
387 while (argc > 0) {
388 arg = *argv++;
389 --argc;
390 opt = find_option(arg);
391 if (opt == NULL) {
392 option_error("unrecognized option '%s'", arg);
393 usage();
394 return 0;
396 n = n_arguments(opt);
397 if (argc < n) {
398 option_error("too few parameters for option %s", arg);
399 return 0;
401 if (!process_option(opt, arg, argv))
402 return 0;
403 argc -= n;
404 argv += n;
406 return 1;
410 * options_from_file - Read a string of options from a file,
411 * and interpret them.
414 options_from_file(filename, must_exist, check_prot, priv)
415 char *filename;
416 int must_exist;
417 int check_prot;
418 int priv;
420 FILE *f;
421 int i, newline, ret, err;
422 option_t *opt;
423 int oldpriv, n;
424 char *oldsource;
425 uid_t euid;
426 char *argv[MAXARGS];
427 char args[MAXARGS][MAXWORDLEN];
428 char cmd[MAXWORDLEN];
430 euid = geteuid();
431 if (check_prot && seteuid(getuid()) == -1) {
432 option_error("unable to drop privileges to open %s: %m", filename);
433 return 0;
435 f = fopen(filename, "r");
436 err = errno;
437 if (check_prot && seteuid(euid) == -1)
438 fatal("unable to regain privileges");
439 if (f == NULL) {
440 errno = err;
441 if (!must_exist) {
442 if (err != ENOENT && err != ENOTDIR)
443 warn("Warning: can't open options file %s: %m", filename);
444 return 1;
446 option_error("Can't open options file %s: %m", filename);
447 return 0;
450 oldpriv = privileged_option;
451 privileged_option = priv;
452 oldsource = option_source;
453 option_source = strdup(filename);
454 if (option_source == NULL)
455 option_source = "file";
456 ret = 0;
457 while (getword(f, cmd, &newline, filename)) {
458 opt = find_option(cmd);
459 if (opt == NULL) {
460 option_error("In file %s: unrecognized option '%s'",
461 filename, cmd);
462 goto err;
464 n = n_arguments(opt);
465 for (i = 0; i < n; ++i) {
466 if (!getword(f, args[i], &newline, filename)) {
467 option_error(
468 "In file %s: too few parameters for option '%s'",
469 filename, cmd);
470 goto err;
472 argv[i] = args[i];
474 if (!process_option(opt, cmd, argv))
475 goto err;
477 ret = 1;
479 err:
480 fclose(f);
481 privileged_option = oldpriv;
482 option_source = oldsource;
483 return ret;
487 * options_from_user - See if the use has a ~/.ppprc file,
488 * and if so, interpret options from it.
491 options_from_user()
493 char *user, *path, *file;
494 int ret;
495 struct passwd *pw;
496 size_t pl;
498 pw = getpwuid(getuid());
499 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
500 return 1;
501 file = _PATH_USEROPT;
502 pl = strlen(user) + strlen(file) + 2;
503 path = malloc(pl);
504 if (path == NULL)
505 novm("init file name");
506 slprintf(path, pl, "%s/%s", user, file);
507 option_priority = OPRIO_CFGFILE;
508 ret = options_from_file(path, 0, 1, privileged);
509 free(path);
510 return ret;
514 * options_for_tty - See if an options file exists for the serial
515 * device, and if so, interpret options from it.
516 * We only allow the per-tty options file to override anything from
517 * the command line if it is something that the user can't override
518 * once it has been set by root; this is done by giving configuration
519 * files a lower priority than the command line.
522 options_for_tty()
524 char *dev, *path, *p;
525 int ret;
526 size_t pl;
528 dev = devnam;
529 if ((p = strstr(dev, "/dev/")) != NULL)
530 dev = p + 5;
531 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
532 return 1; /* don't look for /etc/ppp/options.tty */
533 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
534 path = malloc(pl);
535 if (path == NULL)
536 novm("tty init file name");
537 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
538 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
539 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
540 if (*p == '/')
541 *p = '.';
542 option_priority = OPRIO_CFGFILE;
543 ret = options_from_file(path, 0, 0, 1);
544 free(path);
545 return ret;
549 * options_from_list - process a string of options in a wordlist.
552 options_from_list(w, priv)
553 struct wordlist *w;
554 int priv;
556 char *argv[MAXARGS];
557 option_t *opt;
558 int i, n, ret = 0;
559 struct wordlist *w0;
561 privileged_option = priv;
562 option_source = "secrets file";
563 option_priority = OPRIO_SECFILE;
565 while (w != NULL) {
566 opt = find_option(w->word);
567 if (opt == NULL) {
568 option_error("In secrets file: unrecognized option '%s'",
569 w->word);
570 goto err;
572 n = n_arguments(opt);
573 w0 = w;
574 for (i = 0; i < n; ++i) {
575 w = w->next;
576 if (w == NULL) {
577 option_error(
578 "In secrets file: too few parameters for option '%s'",
579 w0->word);
580 goto err;
582 argv[i] = w->word;
584 if (!process_option(opt, w0->word, argv))
585 goto err;
586 w = w->next;
588 ret = 1;
590 err:
591 return ret;
595 * match_option - see if this option matches an option_t structure.
597 static int
598 match_option(name, opt, dowild)
599 char *name;
600 option_t *opt;
601 int dowild;
603 int (*match) __P((char *, char **, int));
605 if (dowild != (opt->type == o_wild))
606 return 0;
607 if (!dowild)
608 return strcmp(name, opt->name) == 0;
609 match = (int (*) __P((char *, char **, int))) opt->addr;
610 return (*match)(name, NULL, 0);
614 * find_option - scan the option lists for the various protocols
615 * looking for an entry with the given name.
616 * This could be optimized by using a hash table.
618 static option_t *
619 find_option(name)
620 const char *name;
622 option_t *opt;
623 struct option_list *list;
624 int i, dowild;
626 for (dowild = 0; dowild <= 1; ++dowild) {
627 for (opt = general_options; opt->name != NULL; ++opt)
628 if (match_option(name, opt, dowild))
629 return opt;
630 for (opt = auth_options; opt->name != NULL; ++opt)
631 if (match_option(name, opt, dowild))
632 return opt;
633 for (list = extra_options; list != NULL; list = list->next)
634 for (opt = list->options; opt->name != NULL; ++opt)
635 if (match_option(name, opt, dowild))
636 return opt;
637 for (opt = the_channel->options; opt->name != NULL; ++opt)
638 if (match_option(name, opt, dowild))
639 return opt;
640 for (i = 0; protocols[i] != NULL; ++i)
641 if ((opt = protocols[i]->options) != NULL)
642 for (; opt->name != NULL; ++opt)
643 if (match_option(name, opt, dowild))
644 return opt;
646 return NULL;
650 * process_option - process one new-style option.
652 static int
653 process_option(opt, cmd, argv)
654 option_t *opt;
655 char *cmd;
656 char **argv;
658 u_int32_t v;
659 int iv, a;
660 char *sv;
661 int (*parser) __P((char **));
662 int (*wildp) __P((char *, char **, int));
663 char *optopt = (opt->type == o_wild)? "": " option";
664 int prio = option_priority;
665 option_t *mainopt = opt;
667 current_option = opt->name;
668 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
669 prio += OPRIO_ROOT;
670 while (mainopt->flags & OPT_PRIOSUB)
671 --mainopt;
672 if (mainopt->flags & OPT_PRIO) {
673 if (prio < mainopt->priority) {
674 /* new value doesn't override old */
675 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
676 option_error("%s%s set in %s cannot be overridden\n",
677 opt->name, optopt, mainopt->source);
678 return 0;
680 return 1;
682 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
683 warn("%s%s from %s overrides command line",
684 opt->name, optopt, option_source);
687 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
688 option_error("%s%s cannot be changed after initialization",
689 opt->name, optopt);
690 return 0;
692 if ((opt->flags & OPT_PRIV) && !privileged_option) {
693 option_error("using the %s%s requires root privilege",
694 opt->name, optopt);
695 return 0;
697 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
698 option_error("%s%s is disabled", opt->name, optopt);
699 return 0;
701 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
702 option_error("the %s%s may not be changed in %s",
703 opt->name, optopt, option_source);
704 return 0;
707 switch (opt->type) {
708 case o_bool:
709 v = opt->flags & OPT_VALUE;
710 *(bool *)(opt->addr) = v;
711 if (opt->addr2 && (opt->flags & OPT_A2COPY))
712 *(bool *)(opt->addr2) = v;
713 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
714 *(bool *)(opt->addr2) = 0;
715 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
716 *(u_char *)(opt->addr2) &= ~v;
717 else if (opt->addr2 && (opt->flags & OPT_A2OR))
718 *(u_char *)(opt->addr2) |= v;
719 break;
721 case o_int:
722 iv = 0;
723 if ((opt->flags & OPT_NOARG) == 0) {
724 if (!int_option(*argv, &iv))
725 return 0;
726 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
727 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
728 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
729 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
730 switch (opt->flags & OPT_LIMITS) {
731 case OPT_LLIMIT:
732 option_error("%s value must be%s >= %d",
733 opt->name, zok, opt->lower_limit);
734 break;
735 case OPT_ULIMIT:
736 option_error("%s value must be%s <= %d",
737 opt->name, zok, opt->upper_limit);
738 break;
739 case OPT_LIMITS:
740 option_error("%s value must be%s between %d and %d",
741 opt->name, zok, opt->lower_limit, opt->upper_limit);
742 break;
744 return 0;
747 a = opt->flags & OPT_VALUE;
748 if (a >= 128)
749 a -= 256; /* sign extend */
750 iv += a;
751 if (opt->flags & OPT_INC)
752 iv += *(int *)(opt->addr);
753 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
754 int oldv = *(int *)(opt->addr);
755 if ((opt->flags & OPT_ZEROINF) ?
756 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
757 option_error("%s value cannot be increased", opt->name);
758 return 0;
761 *(int *)(opt->addr) = iv;
762 if (opt->addr2 && (opt->flags & OPT_A2COPY))
763 *(int *)(opt->addr2) = iv;
764 break;
766 case o_uint32:
767 if (opt->flags & OPT_NOARG) {
768 v = opt->flags & OPT_VALUE;
769 if (v & 0x80)
770 v |= 0xffffff00U;
771 } else if (!number_option(*argv, &v, 16))
772 return 0;
773 if (opt->flags & OPT_OR)
774 v |= *(u_int32_t *)(opt->addr);
775 *(u_int32_t *)(opt->addr) = v;
776 if (opt->addr2 && (opt->flags & OPT_A2COPY))
777 *(u_int32_t *)(opt->addr2) = v;
778 break;
780 case o_string:
781 if (opt->flags & OPT_STATIC) {
782 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
783 } else {
784 sv = strdup(*argv);
785 if (sv == NULL)
786 novm("option argument");
787 *(char **)(opt->addr) = sv;
789 break;
791 case o_special_noarg:
792 case o_special:
793 parser = (int (*) __P((char **))) opt->addr;
794 if (!(*parser)(argv))
795 return 0;
796 if (opt->flags & OPT_A2LIST) {
797 struct option_value *ovp, *pp;
799 ovp = malloc(sizeof(*ovp) + strlen(*argv));
800 if (ovp != 0) {
801 strcpy(ovp->value, *argv);
802 ovp->source = option_source;
803 ovp->next = NULL;
804 if (opt->addr2 == NULL) {
805 opt->addr2 = ovp;
806 } else {
807 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
809 pp->next = ovp;
813 break;
815 case o_wild:
816 wildp = (int (*) __P((char *, char **, int))) opt->addr;
817 if (!(*wildp)(cmd, argv, 1))
818 return 0;
819 break;
823 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
824 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
826 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
827 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
828 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
830 mainopt->source = option_source;
831 mainopt->priority = prio;
832 mainopt->winner = opt - mainopt;
834 return 1;
838 * override_value - if the option priorities would permit us to
839 * override the value of option, return 1 and update the priority
840 * and source of the option value. Otherwise returns 0.
843 override_value(option, priority, source)
844 const char *option;
845 int priority;
846 const char *source;
848 option_t *opt;
850 opt = find_option(option);
851 if (opt == NULL)
852 return 0;
853 while (opt->flags & OPT_PRIOSUB)
854 --opt;
855 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
856 return 0;
857 opt->priority = priority;
858 opt->source = source;
859 opt->winner = -1;
860 return 1;
864 * n_arguments - tell how many arguments an option takes
866 static int
867 n_arguments(opt)
868 option_t *opt;
870 return (opt->type == o_bool || opt->type == o_special_noarg
871 || (opt->flags & OPT_NOARG))? 0: 1;
875 * add_options - add a list of options to the set we grok.
877 void
878 add_options(opt)
879 option_t *opt;
881 struct option_list *list;
883 list = malloc(sizeof(*list));
884 if (list == 0)
885 novm("option list entry");
886 list->options = opt;
887 list->next = extra_options;
888 extra_options = list;
892 * check_options - check that options are valid and consistent.
894 void
895 check_options()
897 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
898 close(logfile_fd);
902 * print_option - print out an option and its value
904 static void
905 print_option(opt, mainopt, printer, arg)
906 option_t *opt, *mainopt;
907 void (*printer) __P((void *, char *, ...));
908 void *arg;
910 int i, v;
911 char *p;
913 if (opt->flags & OPT_NOPRINT)
914 return;
915 switch (opt->type) {
916 case o_bool:
917 v = opt->flags & OPT_VALUE;
918 if (*(bool *)opt->addr != v)
919 /* this can happen legitimately, e.g. lock
920 option turned off for default device */
921 break;
922 printer(arg, "%s", opt->name);
923 break;
924 case o_int:
925 v = opt->flags & OPT_VALUE;
926 if (v >= 128)
927 v -= 256;
928 i = *(int *)opt->addr;
929 if (opt->flags & OPT_NOARG) {
930 printer(arg, "%s", opt->name);
931 if (i != v) {
932 if (opt->flags & OPT_INC) {
933 for (; i > v; i -= v)
934 printer(arg, " %s", opt->name);
935 } else
936 printer(arg, " # oops: %d not %d\n",
937 i, v);
939 } else {
940 printer(arg, "%s %d", opt->name, i);
942 break;
943 case o_uint32:
944 printer(arg, "%s", opt->name);
945 if ((opt->flags & OPT_NOARG) == 0)
946 printer(arg, " %x", *(u_int32_t *)opt->addr);
947 break;
949 case o_string:
950 if (opt->flags & OPT_HIDE) {
951 p = "??????";
952 } else {
953 p = (char *) opt->addr;
954 if ((opt->flags & OPT_STATIC) == 0)
955 p = *(char **)p;
957 printer(arg, "%s %q", opt->name, p);
958 break;
960 case o_special:
961 case o_special_noarg:
962 case o_wild:
963 if (opt->type != o_wild) {
964 printer(arg, "%s", opt->name);
965 if (n_arguments(opt) == 0)
966 break;
967 printer(arg, " ");
969 if (opt->flags & OPT_A2PRINTER) {
970 void (*oprt) __P((option_t *,
971 void ((*)__P((void *, char *, ...))),
972 void *));
973 oprt = (void (*) __P((option_t *,
974 void ((*)__P((void *, char *, ...))),
975 void *)))opt->addr2;
976 (*oprt)(opt, printer, arg);
977 } else if (opt->flags & OPT_A2STRVAL) {
978 p = (char *) opt->addr2;
979 if ((opt->flags & OPT_STATIC) == 0)
980 p = *(char **)p;
981 printer("%q", p);
982 } else if (opt->flags & OPT_A2LIST) {
983 struct option_value *ovp;
985 ovp = (struct option_value *) opt->addr2;
986 for (;;) {
987 printer(arg, "%q", ovp->value);
988 if ((ovp = ovp->next) == NULL)
989 break;
990 printer(arg, "\t\t# (from %s)\n%s ",
991 ovp->source, opt->name);
993 } else {
994 printer(arg, "xxx # [don't know how to print value]");
996 break;
998 default:
999 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1000 break;
1002 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1006 * print_option_list - print out options in effect from an
1007 * array of options.
1009 static void
1010 print_option_list(opt, printer, arg)
1011 option_t *opt;
1012 void (*printer) __P((void *, char *, ...));
1013 void *arg;
1015 while (opt->name != NULL) {
1016 if (opt->priority != OPRIO_DEFAULT
1017 && opt->winner != (short int) -1)
1018 print_option(opt + opt->winner, opt, printer, arg);
1019 do {
1020 ++opt;
1021 } while (opt->flags & OPT_PRIOSUB);
1026 * print_options - print out what options are in effect.
1028 void
1029 print_options(printer, arg)
1030 void (*printer) __P((void *, char *, ...));
1031 void *arg;
1033 struct option_list *list;
1034 int i;
1036 printer(arg, "pppd options in effect:\n");
1037 print_option_list(general_options, printer, arg);
1038 print_option_list(auth_options, printer, arg);
1039 for (list = extra_options; list != NULL; list = list->next)
1040 print_option_list(list->options, printer, arg);
1041 print_option_list(the_channel->options, printer, arg);
1042 for (i = 0; protocols[i] != NULL; ++i)
1043 print_option_list(protocols[i]->options, printer, arg);
1047 * usage - print out a message telling how to use the program.
1049 static void
1050 usage()
1052 if (phase == PHASE_INITIALIZE)
1053 fprintf(stderr, usage_string, VERSION, progname);
1057 * showhelp - print out usage message and exit.
1059 static int
1060 showhelp(argv)
1061 char **argv;
1063 if (phase == PHASE_INITIALIZE) {
1064 usage();
1065 exit(0);
1067 return 0;
1071 * showversion - print out the version number and exit.
1073 static int
1074 showversion(argv)
1075 char **argv;
1077 if (phase == PHASE_INITIALIZE) {
1078 fprintf(stderr, "pppd version %s\n", VERSION);
1079 exit(0);
1081 return 0;
1085 * option_error - print a message about an error in an option.
1086 * The message is logged, and also sent to
1087 * stderr if phase == PHASE_INITIALIZE.
1089 void
1090 option_error __V((char *fmt, ...))
1092 va_list args;
1093 char buf[1024];
1095 #if defined(__STDC__)
1096 va_start(args, fmt);
1097 #else
1098 char *fmt;
1099 va_start(args);
1100 fmt = va_arg(args, char *);
1101 #endif
1102 vslprintf(buf, sizeof(buf), fmt, args);
1103 va_end(args);
1104 if (phase == PHASE_INITIALIZE)
1105 fprintf(stderr, "%s: %s\n", progname, buf);
1106 syslog(LOG_ERR, "%s", buf);
1109 #if 0
1111 * readable - check if a file is readable by the real user.
1114 readable(fd)
1115 int fd;
1117 uid_t uid;
1118 int i;
1119 struct stat sbuf;
1121 uid = getuid();
1122 if (uid == 0)
1123 return 1;
1124 if (fstat(fd, &sbuf) != 0)
1125 return 0;
1126 if (sbuf.st_uid == uid)
1127 return sbuf.st_mode & S_IRUSR;
1128 if (sbuf.st_gid == getgid())
1129 return sbuf.st_mode & S_IRGRP;
1130 for (i = 0; i < ngroups; ++i)
1131 if (sbuf.st_gid == groups[i])
1132 return sbuf.st_mode & S_IRGRP;
1133 return sbuf.st_mode & S_IROTH;
1135 #endif
1138 * Read a word from a file.
1139 * Words are delimited by white-space or by quotes (" or ').
1140 * Quotes, white-space and \ may be escaped with \.
1141 * \<newline> is ignored.
1144 getword(f, word, newlinep, filename)
1145 FILE *f;
1146 char *word;
1147 int *newlinep;
1148 char *filename;
1150 int c, len, escape;
1151 int quoted, comment;
1152 int value, digit, got, n;
1154 #define isoctal(c) ((c) >= '0' && (c) < '8')
1156 *newlinep = 0;
1157 len = 0;
1158 escape = 0;
1159 comment = 0;
1160 quoted = 0;
1163 * First skip white-space and comments.
1165 for (;;) {
1166 c = getc(f);
1167 if (c == EOF)
1168 break;
1171 * A newline means the end of a comment; backslash-newline
1172 * is ignored. Note that we cannot have escape && comment.
1174 if (c == '\n') {
1175 if (!escape) {
1176 *newlinep = 1;
1177 comment = 0;
1178 } else
1179 escape = 0;
1180 continue;
1184 * Ignore characters other than newline in a comment.
1186 if (comment)
1187 continue;
1190 * If this character is escaped, we have a word start.
1192 if (escape)
1193 break;
1196 * If this is the escape character, look at the next character.
1198 if (c == '\\') {
1199 escape = 1;
1200 continue;
1204 * If this is the start of a comment, ignore the rest of the line.
1206 if (c == '#') {
1207 comment = 1;
1208 continue;
1212 * A non-whitespace character is the start of a word.
1214 if (!isspace(c))
1215 break;
1219 * Process characters until the end of the word.
1221 while (c != EOF) {
1222 if (escape) {
1224 * This character is escaped: backslash-newline is ignored,
1225 * various other characters indicate particular values
1226 * as for C backslash-escapes.
1228 escape = 0;
1229 if (c == '\n') {
1230 c = getc(f);
1231 continue;
1234 got = 0;
1235 switch (c) {
1236 case 'a':
1237 value = '\a';
1238 break;
1239 case 'b':
1240 value = '\b';
1241 break;
1242 case 'f':
1243 value = '\f';
1244 break;
1245 case 'n':
1246 value = '\n';
1247 break;
1248 case 'r':
1249 value = '\r';
1250 break;
1251 case 's':
1252 value = ' ';
1253 break;
1254 case 't':
1255 value = '\t';
1256 break;
1258 default:
1259 if (isoctal(c)) {
1261 * \ddd octal sequence
1263 value = 0;
1264 for (n = 0; n < 3 && isoctal(c); ++n) {
1265 value = (value << 3) + (c & 07);
1266 c = getc(f);
1268 got = 1;
1269 break;
1272 if (c == 'x') {
1274 * \x<hex_string> sequence
1276 value = 0;
1277 c = getc(f);
1278 for (n = 0; n < 2 && isxdigit(c); ++n) {
1279 digit = toupper(c) - '0';
1280 if (digit > 10)
1281 digit += '0' + 10 - 'A';
1282 value = (value << 4) + digit;
1283 c = getc (f);
1285 got = 1;
1286 break;
1290 * Otherwise the character stands for itself.
1292 value = c;
1293 break;
1297 * Store the resulting character for the escape sequence.
1299 if (len < MAXWORDLEN-1)
1300 word[len] = value;
1301 ++len;
1303 if (!got)
1304 c = getc(f);
1305 continue;
1309 * Backslash starts a new escape sequence.
1311 if (c == '\\') {
1312 escape = 1;
1313 c = getc(f);
1314 continue;
1318 * Not escaped: check for the start or end of a quoted
1319 * section and see if we've reached the end of the word.
1321 if (quoted) {
1322 if (c == quoted) {
1323 quoted = 0;
1324 c = getc(f);
1325 continue;
1327 } else if (c == '"' || c == '\'') {
1328 quoted = c;
1329 c = getc(f);
1330 continue;
1331 } else if (isspace(c) || c == '#') {
1332 ungetc (c, f);
1333 break;
1337 * An ordinary character: store it in the word and get another.
1339 if (len < MAXWORDLEN-1)
1340 word[len] = c;
1341 ++len;
1343 c = getc(f);
1347 * End of the word: check for errors.
1349 if (c == EOF) {
1350 if (ferror(f)) {
1351 if (errno == 0)
1352 errno = EIO;
1353 option_error("Error reading %s: %m", filename);
1354 die(1);
1357 * If len is zero, then we didn't find a word before the
1358 * end of the file.
1360 if (len == 0)
1361 return 0;
1362 if (quoted)
1363 option_error("warning: quoted word runs to end of file (%.20s...)",
1364 filename, word);
1368 * Warn if the word was too long, and append a terminating null.
1370 if (len >= MAXWORDLEN) {
1371 option_error("warning: word in file %s too long (%.20s...)",
1372 filename, word);
1373 len = MAXWORDLEN - 1;
1375 word[len] = 0;
1377 return 1;
1379 #undef isoctal
1384 * number_option - parse an unsigned numeric parameter for an option.
1386 static int
1387 number_option(str, valp, base)
1388 char *str;
1389 u_int32_t *valp;
1390 int base;
1392 char *ptr;
1394 *valp = strtoul(str, &ptr, base);
1395 if (ptr == str) {
1396 option_error("invalid numeric parameter '%s' for %s option",
1397 str, current_option);
1398 return 0;
1400 return 1;
1405 * int_option - like number_option, but valp is int *,
1406 * the base is assumed to be 0, and *valp is not changed
1407 * if there is an error.
1410 int_option(str, valp)
1411 char *str;
1412 int *valp;
1414 u_int32_t v;
1416 if (!number_option(str, &v, 0))
1417 return 0;
1418 *valp = (int) v;
1419 return 1;
1424 * The following procedures parse options.
1428 * readfile - take commands from a file.
1430 static int
1431 readfile(argv)
1432 char **argv;
1434 return options_from_file(*argv, 1, 1, privileged_option);
1438 * callfile - take commands from /etc/ppp/peers/<name>.
1439 * Name may not contain /../, start with / or ../, or end in /..
1441 static int
1442 callfile(argv)
1443 char **argv;
1445 char *fname, *arg, *p;
1446 int l, ok;
1448 arg = *argv;
1449 ok = 1;
1450 if (arg[0] == '/' || arg[0] == 0)
1451 ok = 0;
1452 else {
1453 for (p = arg; *p != 0; ) {
1454 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1455 ok = 0;
1456 break;
1458 while (*p != '/' && *p != 0)
1459 ++p;
1460 if (*p == '/')
1461 ++p;
1464 if (!ok) {
1465 option_error("call option value may not contain .. or start with /");
1466 return 0;
1469 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1470 if ((fname = (char *) malloc(l)) == NULL)
1471 novm("call file name");
1472 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1474 ok = options_from_file(fname, 1, 1, 1);
1476 free(fname);
1477 return ok;
1480 #ifdef PPP_FILTER
1482 * setpassfilter - Set the pass filter for packets
1484 static int
1485 setpassfilter(argv)
1486 char **argv;
1488 pcap_t *pc;
1489 int ret = 1;
1491 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1492 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1493 option_error("error in pass-filter expression: %s\n",
1494 pcap_geterr(pc));
1495 ret = 0;
1497 pcap_close(pc);
1499 return ret;
1503 * setactivefilter - Set the active filter for packets
1505 static int
1506 setactivefilter(argv)
1507 char **argv;
1509 pcap_t *pc;
1510 int ret = 1;
1512 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1513 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1514 option_error("error in active-filter expression: %s\n",
1515 pcap_geterr(pc));
1516 ret = 0;
1518 pcap_close(pc);
1520 return ret;
1522 #endif
1525 * setdomain - Set domain name to append to hostname
1527 static int
1528 setdomain(argv)
1529 char **argv;
1531 gethostname(hostname, MAXNAMELEN);
1532 if (**argv != 0) {
1533 if (**argv != '.')
1534 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1535 domain = hostname + strlen(hostname);
1536 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1538 hostname[MAXNAMELEN-1] = 0;
1539 return (1);
1542 static int
1543 setlogfile(argv)
1544 char **argv;
1546 int fd, err;
1547 uid_t euid;
1549 euid = geteuid();
1550 if (!privileged_option && seteuid(getuid()) == -1) {
1551 option_error("unable to drop permissions to open %s: %m", *argv);
1552 return 0;
1554 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1555 if (fd < 0 && errno == EEXIST)
1556 fd = open(*argv, O_WRONLY | O_APPEND);
1557 err = errno;
1558 if (!privileged_option && seteuid(euid) == -1)
1559 fatal("unable to regain privileges: %m");
1560 if (fd < 0) {
1561 errno = err;
1562 option_error("Can't open log file %s: %m", *argv);
1563 return 0;
1565 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1566 if (logfile_fd >= 0)
1567 close(logfile_fd);
1568 logfile_fd = fd;
1569 log_to_fd = fd;
1570 log_default = 0;
1571 return 1;
1574 #ifdef MAXOCTETS
1575 static int
1576 setmodir(argv)
1577 char **argv;
1579 if(*argv == NULL)
1580 return 0;
1581 if(!strcmp(*argv,"in")) {
1582 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1583 } else if (!strcmp(*argv,"out")) {
1584 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1585 } else if (!strcmp(*argv,"max")) {
1586 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1587 } else {
1588 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1590 return 1;
1592 #endif
1594 #ifdef PLUGIN
1595 static int
1596 loadplugin(argv)
1597 char **argv;
1599 char *arg = *argv;
1600 void *handle;
1601 const char *err;
1602 void (*init) __P((void));
1603 char *path = arg;
1604 const char *vers;
1606 if (strchr(arg, '/') == 0) {
1607 const char *base = _PATH_PLUGIN;
1608 int l = strlen(base) + strlen(arg) + 2;
1609 path = malloc(l);
1610 if (path == 0)
1611 novm("plugin file path");
1612 strlcpy(path, base, l);
1613 strlcat(path, "/", l);
1614 strlcat(path, arg, l);
1616 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1617 if (handle == 0) {
1618 err = dlerror();
1619 if (err != 0)
1620 option_error("%s", err);
1621 option_error("Couldn't load plugin %s", arg);
1622 goto err;
1624 init = (void (*)(void))dlsym(handle, "plugin_init");
1625 if (init == 0) {
1626 option_error("%s has no initialization entry point", arg);
1627 goto errclose;
1629 vers = (const char *) dlsym(handle, "pppd_version");
1630 if (vers == 0) {
1631 warn("Warning: plugin %s has no version information", arg);
1632 } else if (strcmp(vers, VERSION) != 0) {
1633 option_error("Plugin %s is for pppd version %s, this is %s",
1634 arg, vers, VERSION);
1635 goto errclose;
1637 info("Plugin %s loaded.", arg);
1638 (*init)();
1639 return 1;
1641 errclose:
1642 dlclose(handle);
1643 err:
1644 if (path != arg)
1645 free(path);
1646 return 0;
1648 #endif /* PLUGIN */