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