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
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
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
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
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 $"
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.
67 #ifdef DLT_PPP_WITHDIRECTION
68 #define DLT_PPP_PPPD DLT_PPP_WITHDIRECTION
70 #define DLT_PPP_PPPD DLT_PPP
73 #endif /* PPP_FILTER */
76 #include "pathnames.h"
78 #if defined(ultrix) || defined(NeXT)
79 char *strdup __P((char *));
84 struct option_value *next;
90 * Option variables and default values.
92 int debug = 0; /* Debug flag */
93 int kdebugflag = 0; /* Tell kernel to print debug messages */
94 int default_device = 1; /* Using /dev/tty or equivalent */
95 char devnam[MAXPATHLEN]; /* Device name */
96 bool nodetach = 0; /* Don't detach from controlling tty */
97 bool updetach = 0; /* Detach once link is up */
98 bool master_detach; /* Detach when we're (only) multilink master */
100 bool up_sdnotify = 0; /* Notify systemd once link is up */
102 int maxconnect = 0; /* Maximum connect time */
103 char user[MAXNAMELEN]; /* Username for PAP */
104 char passwd[MAXSECRETLEN]; /* Password for PAP */
105 bool persist = 0; /* Reopen link after it goes down */
106 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
107 bool demand = 0; /* do dial-on-demand */
108 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
109 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
110 int holdoff = 30; /* # seconds to pause before reconnecting */
111 bool holdoff_specified; /* true if a holdoff value has been given */
112 int log_to_fd = 1; /* send log messages to this fd too */
113 bool log_default = 1; /* log_to_fd is default (stdout) */
114 int maxfail = 10; /* max # of unsuccessful connection attempts */
115 char linkname[MAXPATHLEN]; /* logical name for link */
116 bool tune_kernel; /* may alter kernel settings */
117 int connect_delay = 1000; /* wait this many ms after connect script */
118 int req_unit = -1; /* requested interface unit */
119 char req_ifname[MAXIFNAMELEN]; /* requested interface name */
120 bool multilink = 0; /* Enable multilink operation */
121 char *bundle_name = NULL; /* bundle name for multilink */
122 bool dump_options; /* print out option values */
123 bool dryrun; /* print out option values and exit */
124 char *domain; /* domain name set by domain option */
125 int child_wait = 5; /* # seconds to wait for children at exit */
126 struct userenv *userenv_list; /* user environment variables */
127 int dfl_route_metric = -1; /* metric of the default route to set over the PPP link */
130 unsigned int maxoctets = 0; /* default - no limit */
131 int maxoctets_dir = 0; /* default - sum of traffic */
132 int maxoctets_timeout = 1; /* default 1 second */
136 extern option_t auth_options[];
137 extern struct stat devstat;
140 struct bpf_program pass_filter;/* Filter program for packets to pass */
141 struct bpf_program active_filter; /* Filter program for link-active pkts */
144 static option_t *curopt; /* pointer to option being processed */
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 */
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 **));
165 static int loadplugin __P((char **));
169 static int setpassfilter __P((char **));
170 static int setactivefilter __P((char **));
174 static int setmodir __P((char **));
177 static int user_setenv __P((char **));
178 static void user_setprint __P((option_t *, printer_func, void *));
179 static int user_unsetenv __P((char **));
180 static void user_unsetprint __P((option_t *, printer_func, void *));
182 static option_t *find_option __P((const char *name));
183 static int process_option __P((option_t *, char *, char **));
184 static int n_arguments __P((option_t *));
185 static int number_option __P((char *, u_int32_t *, int));
188 * Structure to store extra lists of options.
192 struct option_list *next;
195 static struct option_list *extra_options = NULL;
200 option_t general_options[] = {
201 { "debug", o_int, &debug,
202 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
203 { "-d", o_int, &debug,
204 "Increase debugging level",
205 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
207 { "kdebug", o_int, &kdebugflag,
208 "Set kernel driver debug level", OPT_PRIO },
210 { "nodetach", o_bool, &nodetach,
211 "Don't detach from controlling tty", OPT_PRIO | 1 },
212 { "-detach", o_bool, &nodetach,
213 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
215 { "up_sdnotify", o_bool, &up_sdnotify,
216 "Notify systemd once link is up (implies nodetach)",
217 OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
219 { "updetach", o_bool, &updetach,
220 "Detach from controlling tty once link is up",
221 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
223 { "master_detach", o_bool, &master_detach,
224 "Detach when we're multilink master but have no link", 1 },
226 { "holdoff", o_int, &holdoff,
227 "Set time in seconds before retrying connection",
228 OPT_PRIO, &holdoff_specified },
230 { "idle", o_int, &idle_time_limit,
231 "Set time in seconds before disconnecting idle link", OPT_PRIO },
233 { "maxconnect", o_int, &maxconnect,
234 "Set connection time limit",
235 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
237 { "domain", o_special, (void *)setdomain,
238 "Add given domain name to hostname",
239 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
241 { "file", o_special, (void *)readfile,
242 "Take options from a file", OPT_NOPRINT },
243 { "call", o_special, (void *)callfile,
244 "Take options from a privileged file", OPT_NOPRINT },
246 { "persist", o_bool, &persist,
247 "Keep on reopening connection after close", OPT_PRIO | 1 },
248 { "nopersist", o_bool, &persist,
249 "Turn off persist option", OPT_PRIOSUB },
251 { "demand", o_bool, &demand,
252 "Dial on demand", OPT_INITONLY | 1, &persist },
254 { "--version", o_special_noarg, (void *)showversion,
255 "Show version number" },
256 { "--help", o_special_noarg, (void *)showhelp,
257 "Show brief listing of options" },
258 { "-h", o_special_noarg, (void *)showhelp,
259 "Show brief listing of options", OPT_ALIAS },
261 { "logfile", o_special, (void *)setlogfile,
262 "Append log messages to this file",
263 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
264 { "logfd", o_int, &log_to_fd,
265 "Send log messages to this file descriptor",
266 OPT_PRIOSUB | OPT_A2CLR, &log_default },
267 { "nolog", o_int, &log_to_fd,
268 "Don't send log messages to any file",
269 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
270 { "nologfd", o_int, &log_to_fd,
271 "Don't send log messages to any file descriptor",
272 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
274 { "linkname", o_string, linkname,
275 "Set logical name for link",
276 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
278 { "maxfail", o_int, &maxfail,
279 "Maximum number of unsuccessful connection attempts to allow",
282 { "ktune", o_bool, &tune_kernel,
283 "Alter kernel settings as necessary", OPT_PRIO | 1 },
284 { "noktune", o_bool, &tune_kernel,
285 "Don't alter kernel settings", OPT_PRIOSUB },
287 { "connect-delay", o_int, &connect_delay,
288 "Maximum time (in ms) to wait after connect script finishes",
291 { "unit", o_int, &req_unit,
292 "PPP interface unit number to use if possible",
293 OPT_PRIO | OPT_LLIMIT, 0, 0 },
295 { "ifname", o_string, req_ifname,
296 "Set PPP interface name",
297 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXIFNAMELEN },
299 { "dump", o_bool, &dump_options,
300 "Print out option values after parsing all options", 1 },
301 { "dryrun", o_bool, &dryrun,
302 "Stop after parsing, printing, and checking options", 1 },
304 { "child-timeout", o_int, &child_wait,
305 "Number of seconds to wait for child processes at exit",
308 { "set", o_special, (void *)user_setenv,
309 "Set user environment variable",
310 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
311 { "unset", o_special, (void *)user_unsetenv,
312 "Unset user environment variable",
313 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
315 { "defaultroute-metric", o_int, &dfl_route_metric,
316 "Metric to use for the default route (Linux only; -1 for default behavior)",
317 OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
319 #ifdef HAVE_MULTILINK
320 { "multilink", o_bool, &multilink,
321 "Enable multilink operation", OPT_PRIO | 1 },
322 { "mp", o_bool, &multilink,
323 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
324 { "nomultilink", o_bool, &multilink,
325 "Disable multilink operation", OPT_PRIOSUB | 0 },
326 { "nomp", o_bool, &multilink,
327 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
329 { "bundle", o_string, &bundle_name,
330 "Bundle name for multilink", OPT_PRIO },
331 #endif /* HAVE_MULTILINK */
334 { "plugin", o_special, (void *)loadplugin,
335 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
339 { "pass-filter", o_special, setpassfilter,
340 "set filter for packets to pass", OPT_PRIO },
342 { "active-filter", o_special, setactivefilter,
343 "set filter for active pkts", OPT_PRIO },
347 { "maxoctets", o_int, &maxoctets,
348 "Set connection traffic limit",
349 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
350 { "mo", o_int, &maxoctets,
351 "Set connection traffic limit",
352 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
353 { "mo-direction", o_special, setmodir,
354 "Set direction for limit traffic (sum,in,out,max)" },
355 { "mo-timeout", o_int, &maxoctets_timeout,
356 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
362 #ifndef IMPLEMENTATION
363 #define IMPLEMENTATION ""
366 static char *usage_string = "\
368 Usage: %s [ options ], where options are:\n\
369 <device> Communicate over the named device\n\
370 <speed> Set the baud rate to <speed>\n\
371 <loc>:<rem> Set the local and/or remote interface IP\n\
372 addresses. Either one may be omitted.\n\
373 asyncmap <n> Set the desired async map to hex <n>\n\
374 auth Require authentication from peer\n\
375 connect <p> Invoke shell command <p> to set up the serial line\n\
376 crtscts Use hardware RTS/CTS flow control\n\
377 defaultroute Add default route through interface\n\
378 file <f> Take options from file <f>\n\
379 modem Use modem control lines\n\
380 mru <n> Set MRU value to <n> for negotiation\n\
381 See pppd(8) for more options.\n\
385 * parse_args - parse a string of arguments from the command line.
388 parse_args(argc, argv)
396 privileged_option = privileged;
397 option_source = "command line";
398 option_priority = OPRIO_CMDLINE;
402 opt = find_option(arg);
404 option_error("unrecognized option '%s'", arg);
408 n = n_arguments(opt);
410 option_error("too few parameters for option %s", arg);
413 if (!process_option(opt, arg, argv))
422 * options_from_file - Read a string of options from a file,
423 * and interpret them.
426 options_from_file(filename, must_exist, check_prot, priv)
433 int i, newline, ret, err;
439 char args[MAXARGS][MAXWORDLEN];
440 char cmd[MAXWORDLEN];
443 if (check_prot && seteuid(getuid()) == -1) {
444 option_error("unable to drop privileges to open %s: %m", filename);
447 f = fopen(filename, "r");
449 if (check_prot && seteuid(euid) == -1)
450 fatal("unable to regain privileges");
454 if (err != ENOENT && err != ENOTDIR)
455 warn("Warning: can't open options file %s: %m", filename);
458 option_error("Can't open options file %s: %m", filename);
462 oldpriv = privileged_option;
463 privileged_option = priv;
464 oldsource = option_source;
465 option_source = strdup(filename);
466 if (option_source == NULL)
467 option_source = "file";
469 while (getword(f, cmd, &newline, filename)) {
470 opt = find_option(cmd);
472 option_error("In file %s: unrecognized option '%s'",
476 n = n_arguments(opt);
477 for (i = 0; i < n; ++i) {
478 if (!getword(f, args[i], &newline, filename)) {
480 "In file %s: too few parameters for option '%s'",
486 if (!process_option(opt, cmd, argv))
493 privileged_option = oldpriv;
494 option_source = oldsource;
499 * options_from_user - See if the use has a ~/.ppprc file,
500 * and if so, interpret options from it.
505 char *user, *path, *file;
510 pw = getpwuid(getuid());
511 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
513 file = _PATH_USEROPT;
514 pl = strlen(user) + strlen(file) + 2;
517 novm("init file name");
518 slprintf(path, pl, "%s/%s", user, file);
519 option_priority = OPRIO_CFGFILE;
520 ret = options_from_file(path, 0, 1, privileged);
526 * options_for_tty - See if an options file exists for the serial
527 * device, and if so, interpret options from it.
528 * We only allow the per-tty options file to override anything from
529 * the command line if it is something that the user can't override
530 * once it has been set by root; this is done by giving configuration
531 * files a lower priority than the command line.
536 char *dev, *path, *p;
541 if ((p = strstr(dev, "/dev/")) != NULL)
543 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
544 return 1; /* don't look for /etc/ppp/options.tty */
545 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
548 novm("tty init file name");
549 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
550 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
551 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
554 option_priority = OPRIO_CFGFILE;
555 ret = options_from_file(path, 0, 0, 1);
561 * options_from_list - process a string of options in a wordlist.
564 options_from_list(w, priv)
573 privileged_option = priv;
574 option_source = "secrets file";
575 option_priority = OPRIO_SECFILE;
578 opt = find_option(w->word);
580 option_error("In secrets file: unrecognized option '%s'",
584 n = n_arguments(opt);
586 for (i = 0; i < n; ++i) {
590 "In secrets file: too few parameters for option '%s'",
596 if (!process_option(opt, w0->word, argv))
607 * match_option - see if this option matches an option_t structure.
610 match_option(name, opt, dowild)
615 int (*match) __P((char *, char **, int));
617 if (dowild != (opt->type == o_wild))
620 return strcmp(name, opt->name) == 0;
621 match = (int (*) __P((char *, char **, int))) opt->addr;
622 return (*match)(name, NULL, 0);
626 * find_option - scan the option lists for the various protocols
627 * looking for an entry with the given name.
628 * This could be optimized by using a hash table.
635 struct option_list *list;
638 for (dowild = 0; dowild <= 1; ++dowild) {
639 for (opt = general_options; opt->name != NULL; ++opt)
640 if (match_option(name, opt, dowild))
642 for (opt = auth_options; opt->name != NULL; ++opt)
643 if (match_option(name, opt, dowild))
645 for (list = extra_options; list != NULL; list = list->next)
646 for (opt = list->options; opt->name != NULL; ++opt)
647 if (match_option(name, opt, dowild))
649 for (opt = the_channel->options; opt->name != NULL; ++opt)
650 if (match_option(name, opt, dowild))
652 for (i = 0; protocols[i] != NULL; ++i)
653 if ((opt = protocols[i]->options) != NULL)
654 for (; opt->name != NULL; ++opt)
655 if (match_option(name, opt, dowild))
662 * process_option - process one new-style option.
665 process_option(opt, cmd, argv)
673 int (*parser) __P((char **));
674 int (*wildp) __P((char *, char **, int));
675 char *optopt = (opt->type == o_wild)? "": " option";
676 int prio = option_priority;
677 option_t *mainopt = opt;
679 current_option = opt->name;
680 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
682 while (mainopt->flags & OPT_PRIOSUB)
684 if (mainopt->flags & OPT_PRIO) {
685 if (prio < mainopt->priority) {
686 /* new value doesn't override old */
687 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
688 option_error("%s%s set in %s cannot be overridden\n",
689 opt->name, optopt, mainopt->source);
694 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
695 warn("%s%s from %s overrides command line",
696 opt->name, optopt, option_source);
699 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
700 option_error("%s%s cannot be changed after initialization",
704 if ((opt->flags & OPT_PRIV) && !privileged_option) {
705 option_error("using the %s%s requires root privilege",
709 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
710 option_error("%s%s is disabled", opt->name, optopt);
713 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
714 option_error("the %s%s may not be changed in %s",
715 opt->name, optopt, option_source);
721 v = opt->flags & OPT_VALUE;
722 *(bool *)(opt->addr) = v;
723 if (opt->addr2 && (opt->flags & OPT_A2COPY))
724 *(bool *)(opt->addr2) = v;
725 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
726 *(bool *)(opt->addr2) = 0;
727 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
728 *(u_char *)(opt->addr2) &= ~v;
729 else if (opt->addr2 && (opt->flags & OPT_A2OR))
730 *(u_char *)(opt->addr2) |= v;
735 if ((opt->flags & OPT_NOARG) == 0) {
736 if (!int_option(*argv, &iv))
738 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
739 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
740 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
741 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
742 switch (opt->flags & OPT_LIMITS) {
744 option_error("%s value must be%s >= %d",
745 opt->name, zok, opt->lower_limit);
748 option_error("%s value must be%s <= %d",
749 opt->name, zok, opt->upper_limit);
752 option_error("%s value must be%s between %d and %d",
753 opt->name, zok, opt->lower_limit, opt->upper_limit);
759 a = opt->flags & OPT_VALUE;
761 a -= 256; /* sign extend */
763 if (opt->flags & OPT_INC)
764 iv += *(int *)(opt->addr);
765 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
766 int oldv = *(int *)(opt->addr);
767 if ((opt->flags & OPT_ZEROINF) ?
768 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
769 option_error("%s value cannot be increased", opt->name);
773 *(int *)(opt->addr) = iv;
774 if (opt->addr2 && (opt->flags & OPT_A2COPY))
775 *(int *)(opt->addr2) = iv;
779 if (opt->flags & OPT_NOARG) {
780 v = opt->flags & OPT_VALUE;
783 } else if (!number_option(*argv, &v, 16))
785 if (opt->flags & OPT_OR)
786 v |= *(u_int32_t *)(opt->addr);
787 *(u_int32_t *)(opt->addr) = v;
788 if (opt->addr2 && (opt->flags & OPT_A2COPY))
789 *(u_int32_t *)(opt->addr2) = v;
793 if (opt->flags & OPT_STATIC) {
794 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
796 char **optptr = (char **)(opt->addr);
799 novm("option argument");
806 case o_special_noarg:
808 parser = (int (*) __P((char **))) opt->addr;
810 if (!(*parser)(argv))
812 if (opt->flags & OPT_A2LIST) {
813 struct option_value *ovp, *pp;
815 ovp = malloc(sizeof(*ovp) + strlen(*argv));
817 strcpy(ovp->value, *argv);
818 ovp->source = option_source;
820 if (opt->addr2 == NULL) {
823 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
832 wildp = (int (*) __P((char *, char **, int))) opt->addr;
833 if (!(*wildp)(cmd, argv, 1))
839 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
840 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
842 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
843 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
844 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
846 mainopt->source = option_source;
847 mainopt->priority = prio;
848 mainopt->winner = opt - mainopt;
854 * override_value - if the option priorities would permit us to
855 * override the value of option, return 1 and update the priority
856 * and source of the option value. Otherwise returns 0.
859 override_value(option, priority, source)
866 opt = find_option(option);
869 while (opt->flags & OPT_PRIOSUB)
871 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
873 opt->priority = priority;
874 opt->source = source;
880 * n_arguments - tell how many arguments an option takes
886 return (opt->type == o_bool || opt->type == o_special_noarg
887 || (opt->flags & OPT_NOARG))? 0: 1;
891 * add_options - add a list of options to the set we grok.
897 struct option_list *list;
899 list = malloc(sizeof(*list));
901 novm("option list entry");
903 list->next = extra_options;
904 extra_options = list;
908 * check_options - check that options are valid and consistent.
913 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
918 * print_option - print out an option and its value
921 print_option(opt, mainopt, printer, arg)
922 option_t *opt, *mainopt;
923 printer_func printer;
929 if (opt->flags & OPT_NOPRINT)
933 v = opt->flags & OPT_VALUE;
934 if (*(bool *)opt->addr != v)
935 /* this can happen legitimately, e.g. lock
936 option turned off for default device */
938 printer(arg, "%s", opt->name);
941 v = opt->flags & OPT_VALUE;
944 i = *(int *)opt->addr;
945 if (opt->flags & OPT_NOARG) {
946 printer(arg, "%s", opt->name);
948 if (opt->flags & OPT_INC) {
949 for (; i > v; i -= v)
950 printer(arg, " %s", opt->name);
952 printer(arg, " # oops: %d not %d\n",
956 printer(arg, "%s %d", opt->name, i);
960 printer(arg, "%s", opt->name);
961 if ((opt->flags & OPT_NOARG) == 0)
962 printer(arg, " %x", *(u_int32_t *)opt->addr);
966 if (opt->flags & OPT_HIDE) {
969 p = (char *) opt->addr;
970 if ((opt->flags & OPT_STATIC) == 0)
973 printer(arg, "%s %q", opt->name, p);
977 case o_special_noarg:
979 if (opt->type != o_wild) {
980 printer(arg, "%s", opt->name);
981 if (n_arguments(opt) == 0)
985 if (opt->flags & OPT_A2PRINTER) {
986 void (*oprt) __P((option_t *, printer_func, void *));
987 oprt = (void (*) __P((option_t *, printer_func,
989 (*oprt)(opt, printer, arg);
990 } else if (opt->flags & OPT_A2STRVAL) {
991 p = (char *) opt->addr2;
992 if ((opt->flags & OPT_STATIC) == 0)
994 printer(arg, "%q", p);
995 } else if (opt->flags & OPT_A2LIST) {
996 struct option_value *ovp;
998 ovp = (struct option_value *) opt->addr2;
1000 printer(arg, "%q", ovp->value);
1001 if ((ovp = ovp->next) == NULL)
1003 printer(arg, "\t\t# (from %s)\n%s ",
1004 ovp->source, opt->name);
1007 printer(arg, "xxx # [don't know how to print value]");
1012 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1015 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1019 * print_option_list - print out options in effect from an
1023 print_option_list(opt, printer, arg)
1025 printer_func printer;
1028 while (opt->name != NULL) {
1029 if (opt->priority != OPRIO_DEFAULT
1030 && opt->winner != (short int) -1)
1031 print_option(opt + opt->winner, opt, printer, arg);
1034 } while (opt->flags & OPT_PRIOSUB);
1039 * print_options - print out what options are in effect.
1042 print_options(printer, arg)
1043 printer_func printer;
1046 struct option_list *list;
1049 printer(arg, "pppd options in effect:\n");
1050 print_option_list(general_options, printer, arg);
1051 print_option_list(auth_options, printer, arg);
1052 for (list = extra_options; list != NULL; list = list->next)
1053 print_option_list(list->options, printer, arg);
1054 print_option_list(the_channel->options, printer, arg);
1055 for (i = 0; protocols[i] != NULL; ++i)
1056 print_option_list(protocols[i]->options, printer, arg);
1060 * usage - print out a message telling how to use the program.
1065 if (phase == PHASE_INITIALIZE)
1066 fprintf(stderr, usage_string, VERSION, progname);
1070 * showhelp - print out usage message and exit.
1076 if (phase == PHASE_INITIALIZE) {
1084 * showversion - print out the version number and exit.
1090 if (phase == PHASE_INITIALIZE) {
1091 fprintf(stderr, "pppd version %s\n", VERSION);
1098 * option_error - print a message about an error in an option.
1099 * The message is logged, and also sent to
1100 * stderr if phase == PHASE_INITIALIZE.
1103 option_error __V((char *fmt, ...))
1108 #if defined(__STDC__)
1109 va_start(args, fmt);
1113 fmt = va_arg(args, char *);
1115 vslprintf(buf, sizeof(buf), fmt, args);
1117 if (phase == PHASE_INITIALIZE)
1118 fprintf(stderr, "%s: %s\n", progname, buf);
1119 syslog(LOG_ERR, "%s", buf);
1124 * readable - check if a file is readable by the real user.
1137 if (fstat(fd, &sbuf) != 0)
1139 if (sbuf.st_uid == uid)
1140 return sbuf.st_mode & S_IRUSR;
1141 if (sbuf.st_gid == getgid())
1142 return sbuf.st_mode & S_IRGRP;
1143 for (i = 0; i < ngroups; ++i)
1144 if (sbuf.st_gid == groups[i])
1145 return sbuf.st_mode & S_IRGRP;
1146 return sbuf.st_mode & S_IROTH;
1151 * Read a word from a file.
1152 * Words are delimited by white-space or by quotes (" or ').
1153 * Quotes, white-space and \ may be escaped with \.
1154 * \<newline> is ignored.
1157 getword(f, word, newlinep, filename)
1164 int quoted, comment;
1165 int value, digit, got, n;
1167 #define isoctal(c) ((c) >= '0' && (c) < '8')
1176 * First skip white-space and comments.
1184 * A newline means the end of a comment; backslash-newline
1185 * is ignored. Note that we cannot have escape && comment.
1197 * Ignore characters other than newline in a comment.
1203 * If this character is escaped, we have a word start.
1209 * If this is the escape character, look at the next character.
1217 * If this is the start of a comment, ignore the rest of the line.
1225 * A non-whitespace character is the start of a word.
1232 * Process characters until the end of the word.
1237 * This character is escaped: backslash-newline is ignored,
1238 * various other characters indicate particular values
1239 * as for C backslash-escapes.
1274 * \ddd octal sequence
1277 for (n = 0; n < 3 && isoctal(c); ++n) {
1278 value = (value << 3) + (c & 07);
1287 * \x<hex_string> sequence
1291 for (n = 0; n < 2 && isxdigit(c); ++n) {
1292 digit = toupper(c) - '0';
1294 digit += '0' + 10 - 'A';
1295 value = (value << 4) + digit;
1303 * Otherwise the character stands for itself.
1310 * Store the resulting character for the escape sequence.
1312 if (len < MAXWORDLEN) {
1323 * Backslash starts a new escape sequence.
1332 * Not escaped: check for the start or end of a quoted
1333 * section and see if we've reached the end of the word.
1341 } else if (c == '"' || c == '\'') {
1345 } else if (isspace(c) || c == '#') {
1351 * An ordinary character: store it in the word and get another.
1353 if (len < MAXWORDLEN) {
1362 * End of the word: check for errors.
1368 option_error("Error reading %s: %m", filename);
1372 * If len is zero, then we didn't find a word before the
1378 option_error("warning: quoted word runs to end of file (%.20s...)",
1383 * Warn if the word was too long, and append a terminating null.
1385 if (len >= MAXWORDLEN) {
1386 option_error("warning: word in file %s too long (%.20s...)",
1388 len = MAXWORDLEN - 1;
1399 * number_option - parse an unsigned numeric parameter for an option.
1402 number_option(str, valp, base)
1409 *valp = strtoul(str, &ptr, base);
1411 option_error("invalid numeric parameter '%s' for %s option",
1412 str, current_option);
1420 * int_option - like number_option, but valp is int *,
1421 * the base is assumed to be 0, and *valp is not changed
1422 * if there is an error.
1425 int_option(str, valp)
1431 if (!number_option(str, &v, 0))
1439 * The following procedures parse options.
1443 * readfile - take commands from a file.
1449 return options_from_file(*argv, 1, 1, privileged_option);
1453 * callfile - take commands from /etc/ppp/peers/<name>.
1454 * Name may not contain /../, start with / or ../, or end in /..
1460 char *fname, *arg, *p;
1465 if (arg[0] == '/' || arg[0] == 0)
1468 for (p = arg; *p != 0; ) {
1469 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1473 while (*p != '/' && *p != 0)
1480 option_error("call option value may not contain .. or start with /");
1484 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1485 if ((fname = (char *) malloc(l)) == NULL)
1486 novm("call file name");
1487 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1489 ok = options_from_file(fname, 1, 1, 1);
1497 * setpassfilter - Set the pass filter for packets
1506 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1507 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1508 option_error("error in pass-filter expression: %s\n",
1518 * setactivefilter - Set the active filter for packets
1521 setactivefilter(argv)
1527 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1528 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1529 option_error("error in active-filter expression: %s\n",
1540 * setdomain - Set domain name to append to hostname
1546 gethostname(hostname, MAXNAMELEN);
1549 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1550 domain = hostname + strlen(hostname);
1551 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1553 hostname[MAXNAMELEN-1] = 0;
1565 if (!privileged_option && seteuid(getuid()) == -1) {
1566 option_error("unable to drop permissions to open %s: %m", *argv);
1569 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1570 if (fd < 0 && errno == EEXIST)
1571 fd = open(*argv, O_WRONLY | O_APPEND);
1573 if (!privileged_option && seteuid(euid) == -1)
1574 fatal("unable to regain privileges: %m");
1577 option_error("Can't open log file %s: %m", *argv);
1580 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1581 if (logfile_fd >= 0)
1596 if(!strcmp(*argv,"in")) {
1597 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1598 } else if (!strcmp(*argv,"out")) {
1599 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1600 } else if (!strcmp(*argv,"max")) {
1601 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1603 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1617 void (*init) __P((void));
1621 if (strchr(arg, '/') == 0) {
1622 const char *base = _PATH_PLUGIN;
1623 int l = strlen(base) + strlen(arg) + 2;
1626 novm("plugin file path");
1627 strlcpy(path, base, l);
1628 strlcat(path, "/", l);
1629 strlcat(path, arg, l);
1631 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1635 option_error("%s", err);
1636 option_error("Couldn't load plugin %s", arg);
1639 init = (void (*)(void))dlsym(handle, "plugin_init");
1641 option_error("%s has no initialization entry point", arg);
1644 vers = (const char *) dlsym(handle, "pppd_version");
1646 warn("Warning: plugin %s has no version information", arg);
1647 } else if (strcmp(vers, VERSION) != 0) {
1648 option_error("Plugin %s is for pppd version %s, this is %s",
1649 arg, vers, VERSION);
1652 info("Plugin %s loaded.", arg);
1666 * Set an environment variable specified by the user.
1672 char *arg = argv[0];
1674 struct userenv *uep, **insp;
1676 if ((eqp = strchr(arg, '=')) == NULL) {
1677 option_error("missing = in name=value: %s", arg);
1681 option_error("missing variable name: %s", arg);
1684 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1685 int nlen = strlen(uep->ue_name);
1686 if (nlen == (eqp - arg) &&
1687 strncmp(arg, uep->ue_name, nlen) == 0)
1690 /* Ignore attempts by unprivileged users to override privileged sources */
1691 if (uep != NULL && !privileged_option && uep->ue_priv)
1693 /* The name never changes, so allocate it with the structure */
1695 uep = malloc(sizeof (*uep) + (eqp-arg));
1696 strncpy(uep->ue_name, arg, eqp-arg);
1697 uep->ue_name[eqp-arg] = '\0';
1698 uep->ue_next = NULL;
1699 insp = &userenv_list;
1700 while (*insp != NULL)
1701 insp = &(*insp)->ue_next;
1704 struct userenv *uep2;
1705 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1706 if (uep2 != uep && !uep2->ue_isset)
1709 if (uep2 == NULL && !uep->ue_isset)
1710 find_option("unset")->flags |= OPT_NOPRINT;
1711 free(uep->ue_value);
1714 uep->ue_priv = privileged_option;
1715 uep->ue_source = option_source;
1716 uep->ue_value = strdup(eqp + 1);
1717 curopt->flags &= ~OPT_NOPRINT;
1722 user_setprint(opt, printer, arg)
1724 printer_func printer;
1727 struct userenv *uep, *uepnext;
1729 uepnext = userenv_list;
1730 while (uepnext != NULL && !uepnext->ue_isset)
1731 uepnext = uepnext->ue_next;
1732 while ((uep = uepnext) != NULL) {
1733 uepnext = uep->ue_next;
1734 while (uepnext != NULL && !uepnext->ue_isset)
1735 uepnext = uepnext->ue_next;
1736 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1737 if (uepnext != NULL)
1738 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1740 opt->source = uep->ue_source;
1748 struct userenv *uep, **insp;
1749 char *arg = argv[0];
1751 if (strchr(arg, '=') != NULL) {
1752 option_error("unexpected = in name: %s", arg);
1756 option_error("missing variable name for unset");
1759 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1760 if (strcmp(arg, uep->ue_name) == 0)
1763 /* Ignore attempts by unprivileged users to override privileged sources */
1764 if (uep != NULL && !privileged_option && uep->ue_priv)
1766 /* The name never changes, so allocate it with the structure */
1768 uep = malloc(sizeof (*uep) + strlen(arg));
1769 strcpy(uep->ue_name, arg);
1770 uep->ue_next = NULL;
1771 insp = &userenv_list;
1772 while (*insp != NULL)
1773 insp = &(*insp)->ue_next;
1776 struct userenv *uep2;
1777 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1778 if (uep2 != uep && uep2->ue_isset)
1781 if (uep2 == NULL && uep->ue_isset)
1782 find_option("set")->flags |= OPT_NOPRINT;
1783 free(uep->ue_value);
1786 uep->ue_priv = privileged_option;
1787 uep->ue_source = option_source;
1788 uep->ue_value = NULL;
1789 curopt->flags &= ~OPT_NOPRINT;
1794 user_unsetprint(opt, printer, arg)
1796 printer_func printer;
1799 struct userenv *uep, *uepnext;
1801 uepnext = userenv_list;
1802 while (uepnext != NULL && uepnext->ue_isset)
1803 uepnext = uepnext->ue_next;
1804 while ((uep = uepnext) != NULL) {
1805 uepnext = uep->ue_next;
1806 while (uepnext != NULL && uepnext->ue_isset)
1807 uepnext = uepnext->ue_next;
1808 (*printer)(arg, "%s", uep->ue_name);
1809 if (uepnext != NULL)
1810 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1812 opt->source = uep->ue_source;