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.
64 * There have been 3 or 4 different names for this in libpcap CVS, but
65 * this seems to be what they have settled on...
66 * For older versions of libpcap, use DLT_PPP - but that means
67 * we lose the inbound and outbound qualifiers.
70 #ifdef DLT_PPP_WITHDIRECTION
71 #define DLT_PPP_PPPD DLT_PPP_WITHDIRECTION
73 #define DLT_PPP_PPPD DLT_PPP
76 #endif /* PPP_FILTER */
79 #include "pathnames.h"
81 #if defined(ultrix) || defined(NeXT)
87 struct option_value *next;
93 * Option variables and default values.
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 bool master_detach; /* Detach when we're (only) multilink master */
103 bool up_sdnotify = 0; /* Notify systemd once link is up */
105 int maxconnect = 0; /* Maximum connect time */
106 char user[MAXNAMELEN]; /* Username for PAP */
107 char passwd[MAXSECRETLEN]; /* Password for PAP */
108 bool persist = 0; /* Reopen link after it goes down */
109 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
110 bool demand = 0; /* do dial-on-demand */
111 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
112 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
113 int holdoff = 30; /* # seconds to pause before reconnecting */
114 bool holdoff_specified; /* true if a holdoff value has been given */
115 int log_to_fd = 1; /* send log messages to this fd too */
116 bool log_default = 1; /* log_to_fd is default (stdout) */
117 int maxfail = 10; /* max # of unsuccessful connection attempts */
118 char linkname[MAXPATHLEN]; /* logical name for link */
119 bool tune_kernel; /* may alter kernel settings */
120 int connect_delay = 1000; /* wait this many ms after connect script */
121 int req_unit = -1; /* requested interface unit */
122 char path_ipup[MAXPATHLEN]; /* pathname of ip-up script */
123 char path_ipdown[MAXPATHLEN];/* pathname of ip-down script */
124 char req_ifname[IFNAMSIZ]; /* requested interface name */
125 bool multilink = 0; /* Enable multilink operation */
126 char *bundle_name = NULL; /* bundle name for multilink */
127 bool dump_options; /* print out option values */
128 bool dryrun; /* print out option values and exit */
129 char *domain; /* domain name set by domain option */
130 int child_wait = 5; /* # seconds to wait for children at exit */
131 struct userenv *userenv_list; /* user environment variables */
132 int dfl_route_metric = -1; /* metric of the default route to set over the PPP link */
135 char path_ipv6up[MAXPATHLEN]; /* pathname of ipv6-up script */
136 char path_ipv6down[MAXPATHLEN]; /* pathname of ipv6-down script */
140 unsigned int maxoctets = 0; /* default - no limit */
141 int maxoctets_dir = 0; /* default - sum of traffic */
142 int maxoctets_timeout = 1; /* default 1 second */
146 extern option_t auth_options[];
147 extern struct stat devstat;
150 struct bpf_program pass_filter;/* Filter program for packets to pass */
151 struct bpf_program active_filter; /* Filter program for link-active pkts */
154 static option_t *curopt; /* pointer to option being processed */
155 char *current_option; /* the name of the option being parsed */
156 int privileged_option; /* set iff the current option came from root */
157 char *option_source; /* string saying where the option came from */
158 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
159 bool devnam_fixed; /* can no longer change device name */
161 static int logfile_fd = -1; /* fd opened for log file */
162 static char logfile_name[MAXPATHLEN]; /* name of log file */
164 static bool noipx_opt; /* dummy for noipx option */
169 static int setdomain(char **);
170 static int readfile(char **);
171 static int callfile(char **);
172 static int showversion(char **);
173 static int showhelp(char **);
174 static void usage(void);
175 static int setlogfile(char **);
177 static int loadplugin(char **);
181 static int setpassfilter(char **);
182 static int setactivefilter(char **);
186 static int setmodir(char **);
189 static int user_setenv(char **);
190 static void user_setprint(option_t *, printer_func, void *);
191 static int user_unsetenv(char **);
192 static void user_unsetprint(option_t *, printer_func, void *);
194 static option_t *find_option(char *name);
195 static int process_option(option_t *, char *, char **);
196 static int n_arguments(option_t *);
197 static int number_option(char *, u_int32_t *, int);
200 * Structure to store extra lists of options.
204 struct option_list *next;
207 static struct option_list *extra_options = NULL;
212 option_t general_options[] = {
213 { "debug", o_int, &debug,
214 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
215 { "-d", o_int, &debug,
216 "Increase debugging level",
217 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
219 { "kdebug", o_int, &kdebugflag,
220 "Set kernel driver debug level", OPT_PRIO },
222 { "nodetach", o_bool, &nodetach,
223 "Don't detach from controlling tty", OPT_PRIO | 1 },
224 { "-detach", o_bool, &nodetach,
225 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
227 { "up_sdnotify", o_bool, &up_sdnotify,
228 "Notify systemd once link is up (implies nodetach)",
229 OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
231 { "updetach", o_bool, &updetach,
232 "Detach from controlling tty once link is up",
233 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
235 { "master_detach", o_bool, &master_detach,
236 "Detach when we're multilink master but have no link", 1 },
238 { "holdoff", o_int, &holdoff,
239 "Set time in seconds before retrying connection",
240 OPT_PRIO, &holdoff_specified },
242 { "idle", o_int, &idle_time_limit,
243 "Set time in seconds before disconnecting idle link", OPT_PRIO },
245 { "maxconnect", o_int, &maxconnect,
246 "Set connection time limit",
247 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
249 { "domain", o_special, (void *)setdomain,
250 "Add given domain name to hostname",
251 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
253 { "file", o_special, (void *)readfile,
254 "Take options from a file", OPT_NOPRINT },
255 { "call", o_special, (void *)callfile,
256 "Take options from a privileged file", OPT_NOPRINT },
258 { "persist", o_bool, &persist,
259 "Keep on reopening connection after close", OPT_PRIO | 1 },
260 { "nopersist", o_bool, &persist,
261 "Turn off persist option", OPT_PRIOSUB },
263 { "demand", o_bool, &demand,
264 "Dial on demand", OPT_INITONLY | 1, &persist },
266 { "--version", o_special_noarg, (void *)showversion,
267 "Show version number" },
268 { "--help", o_special_noarg, (void *)showhelp,
269 "Show brief listing of options" },
270 { "-h", o_special_noarg, (void *)showhelp,
271 "Show brief listing of options", OPT_ALIAS },
273 { "logfile", o_special, (void *)setlogfile,
274 "Append log messages to this file",
275 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
276 { "logfd", o_int, &log_to_fd,
277 "Send log messages to this file descriptor",
278 OPT_PRIOSUB | OPT_A2CLR, &log_default },
279 { "nolog", o_int, &log_to_fd,
280 "Don't send log messages to any file",
281 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
282 { "nologfd", o_int, &log_to_fd,
283 "Don't send log messages to any file descriptor",
284 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
286 { "linkname", o_string, linkname,
287 "Set logical name for link",
288 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
290 { "maxfail", o_int, &maxfail,
291 "Maximum number of unsuccessful connection attempts to allow",
294 { "ktune", o_bool, &tune_kernel,
295 "Alter kernel settings as necessary", OPT_PRIO | 1 },
296 { "noktune", o_bool, &tune_kernel,
297 "Don't alter kernel settings", OPT_PRIOSUB },
299 { "connect-delay", o_int, &connect_delay,
300 "Maximum time (in ms) to wait after connect script finishes",
303 { "unit", o_int, &req_unit,
304 "PPP interface unit number to use if possible",
305 OPT_PRIO | OPT_LLIMIT, 0, 0 },
307 { "ifname", o_string, req_ifname,
308 "Set PPP interface name",
309 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, IFNAMSIZ },
311 { "dump", o_bool, &dump_options,
312 "Print out option values after parsing all options", 1 },
313 { "dryrun", o_bool, &dryrun,
314 "Stop after parsing, printing, and checking options", 1 },
316 { "child-timeout", o_int, &child_wait,
317 "Number of seconds to wait for child processes at exit",
320 { "set", o_special, (void *)user_setenv,
321 "Set user environment variable",
322 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
323 { "unset", o_special, (void *)user_unsetenv,
324 "Unset user environment variable",
325 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
327 { "defaultroute-metric", o_int, &dfl_route_metric,
328 "Metric to use for the default route (Linux only; -1 for default behavior)",
329 OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
331 { "ip-up-script", o_string, path_ipup,
332 "Set pathname of ip-up script",
333 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
334 { "ip-down-script", o_string, path_ipdown,
335 "Set pathname of ip-down script",
336 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
339 { "ipv6-up-script", o_string, path_ipv6up,
340 "Set pathname of ipv6-up script",
341 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
342 { "ipv6-down-script", o_string, path_ipv6down,
343 "Set pathname of ipv6-down script",
344 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
347 #ifdef HAVE_MULTILINK
348 { "multilink", o_bool, &multilink,
349 "Enable multilink operation", OPT_PRIO | 1 },
350 { "mp", o_bool, &multilink,
351 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
352 { "nomultilink", o_bool, &multilink,
353 "Disable multilink operation", OPT_PRIOSUB | 0 },
354 { "nomp", o_bool, &multilink,
355 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
357 { "bundle", o_string, &bundle_name,
358 "Bundle name for multilink", OPT_PRIO },
359 #endif /* HAVE_MULTILINK */
362 { "plugin", o_special, (void *)loadplugin,
363 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
367 { "pass-filter", o_special, setpassfilter,
368 "set filter for packets to pass", OPT_PRIO },
370 { "active-filter", o_special, setactivefilter,
371 "set filter for active pkts", OPT_PRIO },
375 { "maxoctets", o_int, &maxoctets,
376 "Set connection traffic limit",
377 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
378 { "mo", o_int, &maxoctets,
379 "Set connection traffic limit",
380 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
381 { "mo-direction", o_special, setmodir,
382 "Set direction for limit traffic (sum,in,out,max)" },
383 { "mo-timeout", o_int, &maxoctets_timeout,
384 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
387 /* Dummy option, does nothing */
388 { "noipx", o_bool, &noipx_opt, NULL, OPT_NOPRINT | 1 },
393 #ifndef IMPLEMENTATION
394 #define IMPLEMENTATION ""
397 static char *usage_string = "\
399 Usage: %s [ options ], where options are:\n\
400 <device> Communicate over the named device\n\
401 <speed> Set the baud rate to <speed>\n\
402 <loc>:<rem> Set the local and/or remote interface IP\n\
403 addresses. Either one may be omitted.\n\
404 asyncmap <n> Set the desired async map to hex <n>\n\
405 auth Require authentication from peer\n\
406 connect <p> Invoke shell command <p> to set up the serial line\n\
407 crtscts Use hardware RTS/CTS flow control\n\
408 defaultroute Add default route through interface\n\
409 file <f> Take options from file <f>\n\
410 modem Use modem control lines\n\
411 mru <n> Set MRU value to <n> for negotiation\n\
412 See pppd(8) for more options.\n\
416 * parse_args - parse a string of arguments from the command line.
419 parse_args(int argc, char **argv)
425 privileged_option = privileged;
426 option_source = "command line";
427 option_priority = OPRIO_CMDLINE;
431 opt = find_option(arg);
433 option_error("unrecognized option '%s'", arg);
437 n = n_arguments(opt);
439 option_error("too few parameters for option %s", arg);
442 if (!process_option(opt, arg, argv))
451 * options_from_file - Read a string of options from a file,
452 * and interpret them.
455 options_from_file(char *filename, int must_exist, int check_prot, int priv)
458 int i, newline, ret, err;
464 char args[MAXARGS][MAXWORDLEN];
465 char cmd[MAXWORDLEN];
468 if (check_prot && seteuid(getuid()) == -1) {
469 option_error("unable to drop privileges to open %s: %m", filename);
472 f = fopen(filename, "r");
474 if (check_prot && seteuid(euid) == -1)
475 fatal("unable to regain privileges");
479 if (err != ENOENT && err != ENOTDIR)
480 warn("Warning: can't open options file %s: %m", filename);
483 option_error("Can't open options file %s: %m", filename);
487 oldpriv = privileged_option;
488 privileged_option = priv;
489 oldsource = option_source;
490 option_source = strdup(filename);
491 if (option_source == NULL)
492 option_source = "file";
494 while (getword(f, cmd, &newline, filename)) {
495 opt = find_option(cmd);
497 option_error("In file %s: unrecognized option '%s'",
501 n = n_arguments(opt);
502 for (i = 0; i < n; ++i) {
503 if (!getword(f, args[i], &newline, filename)) {
505 "In file %s: too few parameters for option '%s'",
511 if (!process_option(opt, cmd, argv))
518 privileged_option = oldpriv;
519 option_source = oldsource;
524 * options_from_user - See if the use has a ~/.ppprc file,
525 * and if so, interpret options from it.
528 options_from_user(void)
530 char *user, *path, *file;
535 pw = getpwuid(getuid());
536 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
538 file = _PATH_USEROPT;
539 pl = strlen(user) + strlen(file) + 2;
542 novm("init file name");
543 slprintf(path, pl, "%s/%s", user, file);
544 option_priority = OPRIO_CFGFILE;
545 ret = options_from_file(path, 0, 1, privileged);
551 * options_for_tty - See if an options file exists for the serial
552 * device, and if so, interpret options from it.
553 * We only allow the per-tty options file to override anything from
554 * the command line if it is something that the user can't override
555 * once it has been set by root; this is done by giving configuration
556 * files a lower priority than the command line.
559 options_for_tty(void)
561 char *dev, *path, *p;
566 if ((p = strstr(dev, "/dev/")) != NULL)
568 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
569 return 1; /* don't look for /etc/ppp/options.tty */
570 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
573 novm("tty init file name");
574 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
575 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
576 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
579 option_priority = OPRIO_CFGFILE;
580 ret = options_from_file(path, 0, 0, 1);
586 * options_from_list - process a string of options in a wordlist.
589 options_from_list(struct wordlist *w, int priv)
596 privileged_option = priv;
597 option_source = "secrets file";
598 option_priority = OPRIO_SECFILE;
601 opt = find_option(w->word);
603 option_error("In secrets file: unrecognized option '%s'",
607 n = n_arguments(opt);
609 for (i = 0; i < n; ++i) {
613 "In secrets file: too few parameters for option '%s'",
619 if (!process_option(opt, w0->word, argv))
630 * match_option - see if this option matches an option_t structure.
633 match_option(char *name, option_t *opt, int dowild)
635 int (*match)(char *, char **, int);
637 if (dowild != (opt->type == o_wild))
640 return strcmp(name, opt->name) == 0;
641 match = (int (*)(char *, char **, int)) opt->addr;
642 return (*match)(name, NULL, 0);
646 * find_option - scan the option lists for the various protocols
647 * looking for an entry with the given name.
648 * This could be optimized by using a hash table.
651 find_option(char *name)
654 struct option_list *list;
657 for (dowild = 0; dowild <= 1; ++dowild) {
658 for (opt = general_options; opt->name != NULL; ++opt)
659 if (match_option(name, opt, dowild))
661 for (opt = auth_options; opt->name != NULL; ++opt)
662 if (match_option(name, opt, dowild))
664 for (list = extra_options; list != NULL; list = list->next)
665 for (opt = list->options; opt->name != NULL; ++opt)
666 if (match_option(name, opt, dowild))
668 for (opt = the_channel->options; opt->name != NULL; ++opt)
669 if (match_option(name, opt, dowild))
671 for (i = 0; protocols[i] != NULL; ++i)
672 if ((opt = protocols[i]->options) != NULL)
673 for (; opt->name != NULL; ++opt)
674 if (match_option(name, opt, dowild))
681 * process_option - process one new-style option.
684 process_option(option_t *opt, char *cmd, char **argv)
689 int (*parser)(char **);
690 int (*wildp)(char *, char **, int);
691 char *optopt = (opt->type == o_wild)? "": " option";
692 int prio = option_priority;
693 option_t *mainopt = opt;
695 current_option = opt->name;
696 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
698 while (mainopt->flags & OPT_PRIOSUB)
700 if (mainopt->flags & OPT_PRIO) {
701 if (prio < mainopt->priority) {
702 /* new value doesn't override old */
703 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
704 option_error("%s%s set in %s cannot be overridden\n",
705 opt->name, optopt, mainopt->source);
710 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
711 warn("%s%s from %s overrides command line",
712 opt->name, optopt, option_source);
715 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
716 option_error("%s%s cannot be changed after initialization",
720 if ((opt->flags & OPT_PRIV) && !privileged_option) {
721 option_error("using the %s%s requires root privilege",
725 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
726 option_error("%s%s is disabled", opt->name, optopt);
729 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
730 option_error("the %s%s may not be changed in %s",
731 opt->name, optopt, option_source);
737 v = opt->flags & OPT_VALUE;
738 *(bool *)(opt->addr) = v;
739 if (opt->addr2 && (opt->flags & OPT_A2COPY))
740 *(bool *)(opt->addr2) = v;
741 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
742 *(bool *)(opt->addr2) = 0;
743 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
744 *(u_char *)(opt->addr2) &= ~v;
745 else if (opt->addr2 && (opt->flags & OPT_A2OR))
746 *(u_char *)(opt->addr2) |= v;
751 if ((opt->flags & OPT_NOARG) == 0) {
752 if (!int_option(*argv, &iv))
754 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
755 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
756 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
757 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
758 switch (opt->flags & OPT_LIMITS) {
760 option_error("%s value must be%s >= %d",
761 opt->name, zok, opt->lower_limit);
764 option_error("%s value must be%s <= %d",
765 opt->name, zok, opt->upper_limit);
768 option_error("%s value must be%s between %d and %d",
769 opt->name, zok, opt->lower_limit, opt->upper_limit);
775 a = opt->flags & OPT_VALUE;
777 a -= 256; /* sign extend */
779 if (opt->flags & OPT_INC)
780 iv += *(int *)(opt->addr);
781 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
782 int oldv = *(int *)(opt->addr);
783 if ((opt->flags & OPT_ZEROINF) ?
784 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
785 option_error("%s value cannot be increased", opt->name);
789 *(int *)(opt->addr) = iv;
790 if (opt->addr2 && (opt->flags & OPT_A2COPY))
791 *(int *)(opt->addr2) = iv;
795 if (opt->flags & OPT_NOARG) {
796 v = opt->flags & OPT_VALUE;
799 } else if (!number_option(*argv, &v, 16))
801 if (opt->flags & OPT_OR)
802 v |= *(u_int32_t *)(opt->addr);
803 *(u_int32_t *)(opt->addr) = v;
804 if (opt->addr2 && (opt->flags & OPT_A2COPY))
805 *(u_int32_t *)(opt->addr2) = v;
809 if (opt->flags & OPT_STATIC) {
810 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
812 char **optptr = (char **)(opt->addr);
815 novm("option argument");
820 /* obfuscate original argument for things like password */
821 if (opt->flags & OPT_HIDE) {
822 memset(*argv, '?', strlen(*argv));
827 case o_special_noarg:
829 parser = (int (*)(char **)) opt->addr;
831 if (!(*parser)(argv))
833 if (opt->flags & OPT_A2LIST) {
834 struct option_value *ovp, *pp;
836 ovp = malloc(sizeof(*ovp) + strlen(*argv));
838 strcpy(ovp->value, *argv);
839 ovp->source = option_source;
841 if (opt->addr2 == NULL) {
844 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
853 wildp = (int (*)(char *, char **, int)) opt->addr;
854 if (!(*wildp)(cmd, argv, 1))
860 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
861 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
863 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
864 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
865 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
867 mainopt->source = option_source;
868 mainopt->priority = prio;
869 mainopt->winner = opt - mainopt;
875 * override_value - if the option priorities would permit us to
876 * override the value of option, return 1 and update the priority
877 * and source of the option value. Otherwise returns 0.
880 override_value(char *option, int priority, const char *source)
884 opt = find_option(option);
887 while (opt->flags & OPT_PRIOSUB)
889 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
891 opt->priority = priority;
892 opt->source = source;
898 * n_arguments - tell how many arguments an option takes
901 n_arguments(option_t *opt)
903 return (opt->type == o_bool || opt->type == o_special_noarg
904 || (opt->flags & OPT_NOARG))? 0: 1;
908 * add_options - add a list of options to the set we grok.
911 add_options(option_t *opt)
913 struct option_list *list;
915 list = malloc(sizeof(*list));
917 novm("option list entry");
919 list->next = extra_options;
920 extra_options = list;
924 * check_options - check that options are valid and consistent.
929 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
934 * print_option - print out an option and its value
937 print_option(option_t *opt, option_t *mainopt, printer_func printer, void *arg)
942 if (opt->flags & OPT_NOPRINT)
946 v = opt->flags & OPT_VALUE;
947 if (*(bool *)opt->addr != v)
948 /* this can happen legitimately, e.g. lock
949 option turned off for default device */
951 printer(arg, "%s", opt->name);
954 v = opt->flags & OPT_VALUE;
957 i = *(int *)opt->addr;
958 if (opt->flags & OPT_NOARG) {
959 printer(arg, "%s", opt->name);
961 if (opt->flags & OPT_INC) {
962 for (; i > v; i -= v)
963 printer(arg, " %s", opt->name);
965 printer(arg, " # oops: %d not %d\n",
969 printer(arg, "%s %d", opt->name, i);
973 printer(arg, "%s", opt->name);
974 if ((opt->flags & OPT_NOARG) == 0)
975 printer(arg, " %x", *(u_int32_t *)opt->addr);
979 if (opt->flags & OPT_HIDE) {
982 p = (char *) opt->addr;
983 if ((opt->flags & OPT_STATIC) == 0)
986 printer(arg, "%s %q", opt->name, p);
990 case o_special_noarg:
992 if (opt->type != o_wild) {
993 printer(arg, "%s", opt->name);
994 if (n_arguments(opt) == 0)
998 if (opt->flags & OPT_A2PRINTER) {
999 void (*oprt)(option_t *, printer_func, void *);
1000 oprt = (void (*)(option_t *, printer_func, void *))
1002 (*oprt)(opt, printer, arg);
1003 } else if (opt->flags & OPT_A2STRVAL) {
1004 p = (char *) opt->addr2;
1005 if ((opt->flags & OPT_STATIC) == 0)
1007 printer(arg, "%q", p);
1008 } else if (opt->flags & OPT_A2LIST) {
1009 struct option_value *ovp;
1011 ovp = (struct option_value *) opt->addr2;
1013 printer(arg, "%q", ovp->value);
1014 if ((ovp = ovp->next) == NULL)
1016 printer(arg, "\t\t# (from %s)\n%s ",
1017 ovp->source, opt->name);
1020 printer(arg, "xxx # [don't know how to print value]");
1025 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1028 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1032 * print_option_list - print out options in effect from an
1036 print_option_list(option_t *opt, printer_func printer, void *arg)
1038 while (opt->name != NULL) {
1039 if (opt->priority != OPRIO_DEFAULT
1040 && opt->winner != (short int) -1)
1041 print_option(opt + opt->winner, opt, printer, arg);
1044 } while (opt->flags & OPT_PRIOSUB);
1049 * print_options - print out what options are in effect.
1052 print_options(printer_func printer, void *arg)
1054 struct option_list *list;
1057 printer(arg, "pppd options in effect:\n");
1058 print_option_list(general_options, printer, arg);
1059 print_option_list(auth_options, printer, arg);
1060 for (list = extra_options; list != NULL; list = list->next)
1061 print_option_list(list->options, printer, arg);
1062 print_option_list(the_channel->options, printer, arg);
1063 for (i = 0; protocols[i] != NULL; ++i)
1064 print_option_list(protocols[i]->options, printer, arg);
1068 * usage - print out a message telling how to use the program.
1073 if (phase == PHASE_INITIALIZE)
1074 fprintf(stderr, usage_string, VERSION, progname);
1078 * showhelp - print out usage message and exit.
1081 showhelp(char **argv)
1083 if (phase == PHASE_INITIALIZE) {
1091 * showversion - print out the version number and exit.
1094 showversion(char **argv)
1096 if (phase == PHASE_INITIALIZE) {
1097 fprintf(stdout, "pppd version %s\n", VERSION);
1104 * option_error - print a message about an error in an option.
1105 * The message is logged, and also sent to
1106 * stderr if phase == PHASE_INITIALIZE.
1109 option_error(char *fmt, ...)
1114 va_start(args, fmt);
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.
1136 if (fstat(fd, &sbuf) != 0)
1138 if (sbuf.st_uid == uid)
1139 return sbuf.st_mode & S_IRUSR;
1140 if (sbuf.st_gid == getgid())
1141 return sbuf.st_mode & S_IRGRP;
1142 for (i = 0; i < ngroups; ++i)
1143 if (sbuf.st_gid == groups[i])
1144 return sbuf.st_mode & S_IRGRP;
1145 return sbuf.st_mode & S_IROTH;
1150 * Read a word from a file.
1151 * Words are delimited by white-space or by quotes (" or ').
1152 * Quotes, white-space and \ may be escaped with \.
1153 * \<newline> is ignored.
1156 getword(FILE *f, char *word, int *newlinep, char *filename)
1159 int quoted, comment;
1160 int value, digit, got, n;
1162 #define isoctal(c) ((c) >= '0' && (c) < '8')
1171 * First skip white-space and comments.
1179 * A newline means the end of a comment; backslash-newline
1180 * is ignored. Note that we cannot have escape && comment.
1192 * Ignore characters other than newline in a comment.
1198 * If this character is escaped, we have a word start.
1204 * If this is the escape character, look at the next character.
1212 * If this is the start of a comment, ignore the rest of the line.
1220 * A non-whitespace character is the start of a word.
1227 * Process characters until the end of the word.
1232 * This character is escaped: backslash-newline is ignored,
1233 * various other characters indicate particular values
1234 * as for C backslash-escapes.
1269 * \ddd octal sequence
1272 for (n = 0; n < 3 && isoctal(c); ++n) {
1273 value = (value << 3) + (c & 07);
1282 * \x<hex_string> sequence
1286 for (n = 0; n < 2 && isxdigit(c); ++n) {
1287 digit = toupper(c) - '0';
1289 digit += '0' + 10 - 'A';
1290 value = (value << 4) + digit;
1298 * Otherwise the character stands for itself.
1305 * Store the resulting character for the escape sequence.
1307 if (len < MAXWORDLEN) {
1318 * Backslash starts a new escape sequence.
1327 * Not escaped: check for the start or end of a quoted
1328 * section and see if we've reached the end of the word.
1336 } else if (c == '"' || c == '\'') {
1340 } else if (isspace(c) || c == '#') {
1346 * An ordinary character: store it in the word and get another.
1348 if (len < MAXWORDLEN) {
1355 word[MAXWORDLEN-1] = 0; /* make sure word is null-terminated */
1358 * End of the word: check for errors.
1364 option_error("Error reading %s: %m", filename);
1368 * If len is zero, then we didn't find a word before the
1374 option_error("warning: quoted word runs to end of file (%.20s...)",
1379 * Warn if the word was too long, and append a terminating null.
1381 if (len >= MAXWORDLEN) {
1382 option_error("warning: word in file %s too long (%.20s...)",
1384 len = MAXWORDLEN - 1;
1395 * number_option - parse an unsigned numeric parameter for an option.
1398 number_option(char *str, u_int32_t *valp, int base)
1402 *valp = strtoul(str, &ptr, base);
1404 option_error("invalid numeric parameter '%s' for %s option",
1405 str, current_option);
1413 * int_option - like number_option, but valp is int *,
1414 * the base is assumed to be 0, and *valp is not changed
1415 * if there is an error.
1418 int_option(char *str, int *valp)
1422 if (!number_option(str, &v, 0))
1430 * The following procedures parse options.
1434 * readfile - take commands from a file.
1437 readfile(char **argv)
1439 return options_from_file(*argv, 1, 1, privileged_option);
1443 * callfile - take commands from /etc/ppp/peers/<name>.
1444 * Name may not contain /../, start with / or ../, or end in /..
1447 callfile(char **argv)
1449 char *fname, *arg, *p;
1454 if (arg[0] == '/' || arg[0] == 0)
1457 for (p = arg; *p != 0; ) {
1458 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1462 while (*p != '/' && *p != 0)
1469 option_error("call option value may not contain .. or start with /");
1473 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1474 if ((fname = (char *) malloc(l)) == NULL)
1475 novm("call file name");
1476 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1477 script_setenv("CALL_FILE", arg, 0);
1479 ok = options_from_file(fname, 1, 1, 1);
1487 * setpassfilter - Set the pass filter for packets
1490 setpassfilter(char **argv)
1495 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1496 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1497 option_error("error in pass-filter expression: %s\n",
1507 * setactivefilter - Set the active filter for packets
1510 setactivefilter(char **argv)
1515 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1516 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1517 option_error("error in active-filter expression: %s\n",
1528 * setdomain - Set domain name to append to hostname
1531 setdomain(char **argv)
1533 gethostname(hostname, MAXNAMELEN);
1536 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1537 domain = hostname + strlen(hostname);
1538 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1540 hostname[MAXNAMELEN-1] = 0;
1545 setlogfile(char **argv)
1551 if (!privileged_option && seteuid(getuid()) == -1) {
1552 option_error("unable to drop permissions to open %s: %m", *argv);
1555 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1556 if (fd < 0 && errno == EEXIST)
1557 fd = open(*argv, O_WRONLY | O_APPEND);
1559 if (!privileged_option && seteuid(euid) == -1)
1560 fatal("unable to regain privileges: %m");
1563 option_error("Can't open log file %s: %m", *argv);
1566 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1567 if (logfile_fd >= 0)
1577 setmodir(char **argv)
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;
1588 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1596 loadplugin(char **argv)
1605 if (strchr(arg, '/') == 0) {
1606 const char *base = _PATH_PLUGIN;
1607 int l = strlen(base) + strlen(arg) + 2;
1610 novm("plugin file path");
1611 strlcpy(path, base, l);
1612 strlcat(path, "/", l);
1613 strlcat(path, arg, l);
1615 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1619 option_error("%s", err);
1620 option_error("Couldn't load plugin %s", arg);
1623 init = (void (*)(void))dlsym(handle, "plugin_init");
1625 option_error("%s has no initialization entry point", arg);
1628 vers = (const char *) dlsym(handle, "pppd_version");
1630 warn("Warning: plugin %s has no version information", arg);
1631 } else if (strcmp(vers, VERSION) != 0) {
1632 option_error("Plugin %s is for pppd version %s, this is %s",
1633 arg, vers, VERSION);
1636 info("Plugin %s loaded.", arg);
1650 * Set an environment variable specified by the user.
1653 user_setenv(char **argv)
1655 char *arg = argv[0];
1657 struct userenv *uep, **insp;
1659 if ((eqp = strchr(arg, '=')) == NULL) {
1660 option_error("missing = in name=value: %s", arg);
1664 option_error("missing variable name: %s", arg);
1667 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1668 int nlen = strlen(uep->ue_name);
1669 if (nlen == (eqp - arg) &&
1670 strncmp(arg, uep->ue_name, nlen) == 0)
1673 /* Ignore attempts by unprivileged users to override privileged sources */
1674 if (uep != NULL && !privileged_option && uep->ue_priv)
1676 /* The name never changes, so allocate it with the structure */
1678 uep = malloc(sizeof (*uep) + (eqp-arg));
1679 strncpy(uep->ue_name, arg, eqp-arg);
1680 uep->ue_name[eqp-arg] = '\0';
1681 uep->ue_next = NULL;
1682 insp = &userenv_list;
1683 while (*insp != NULL)
1684 insp = &(*insp)->ue_next;
1687 struct userenv *uep2;
1688 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1689 if (uep2 != uep && !uep2->ue_isset)
1692 if (uep2 == NULL && !uep->ue_isset)
1693 find_option("unset")->flags |= OPT_NOPRINT;
1694 free(uep->ue_value);
1697 uep->ue_priv = privileged_option;
1698 uep->ue_source = option_source;
1699 uep->ue_value = strdup(eqp + 1);
1700 curopt->flags &= ~OPT_NOPRINT;
1705 user_setprint(option_t *opt, printer_func printer, void *arg)
1707 struct userenv *uep, *uepnext;
1709 uepnext = userenv_list;
1710 while (uepnext != NULL && !uepnext->ue_isset)
1711 uepnext = uepnext->ue_next;
1712 while ((uep = uepnext) != NULL) {
1713 uepnext = uep->ue_next;
1714 while (uepnext != NULL && !uepnext->ue_isset)
1715 uepnext = uepnext->ue_next;
1716 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1717 if (uepnext != NULL)
1718 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1720 opt->source = uep->ue_source;
1725 user_unsetenv(char **argv)
1727 struct userenv *uep, **insp;
1728 char *arg = argv[0];
1730 if (strchr(arg, '=') != NULL) {
1731 option_error("unexpected = in name: %s", arg);
1735 option_error("missing variable name for unset");
1738 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1739 if (strcmp(arg, uep->ue_name) == 0)
1742 /* Ignore attempts by unprivileged users to override privileged sources */
1743 if (uep != NULL && !privileged_option && uep->ue_priv)
1745 /* The name never changes, so allocate it with the structure */
1747 uep = malloc(sizeof (*uep) + strlen(arg));
1748 strcpy(uep->ue_name, arg);
1749 uep->ue_next = NULL;
1750 insp = &userenv_list;
1751 while (*insp != NULL)
1752 insp = &(*insp)->ue_next;
1755 struct userenv *uep2;
1756 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1757 if (uep2 != uep && uep2->ue_isset)
1760 if (uep2 == NULL && uep->ue_isset)
1761 find_option("set")->flags |= OPT_NOPRINT;
1762 free(uep->ue_value);
1765 uep->ue_priv = privileged_option;
1766 uep->ue_source = option_source;
1767 uep->ue_value = NULL;
1768 curopt->flags &= ~OPT_NOPRINT;
1773 user_unsetprint(option_t *opt, printer_func printer, void *arg)
1775 struct userenv *uep, *uepnext;
1777 uepnext = userenv_list;
1778 while (uepnext != NULL && uepnext->ue_isset)
1779 uepnext = uepnext->ue_next;
1780 while ((uep = uepnext) != NULL) {
1781 uepnext = uep->ue_next;
1782 while (uepnext != NULL && uepnext->ue_isset)
1783 uepnext = uepnext->ue_next;
1784 (*printer)(arg, "%s", uep->ue_name);
1785 if (uepnext != NULL)
1786 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1788 opt->source = uep->ue_source;