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.
57 #include <sys/param.h>
59 #ifdef PPP_WITH_PLUGINS
63 #ifdef PPP_WITH_FILTER
66 * There have been 3 or 4 different names for this in libpcap CVS, but
67 * this seems to be what they have settled on...
68 * For older versions of libpcap, use DLT_PPP - but that means
69 * we lose the inbound and outbound qualifiers.
72 #ifdef DLT_PPP_WITHDIRECTION
73 #define DLT_PPP_PPPD DLT_PPP_WITHDIRECTION
75 #define DLT_PPP_PPPD DLT_PPP
78 #endif /* PPP_WITH_FILTER */
81 #include "pathnames.h"
83 #if defined(ultrix) || defined(NeXT)
89 struct option_value *next;
95 * Option variables and default values.
97 int debug = 0; /* Debug flag */
98 int kdebugflag = 0; /* Tell kernel to print debug messages */
99 int default_device = 1; /* Using /dev/tty or equivalent */
100 char devnam[MAXPATHLEN]; /* Device name */
101 bool nodetach = 0; /* Don't detach from controlling tty */
102 bool updetach = 0; /* Detach once link is up */
103 bool master_detach; /* Detach when we're (only) multilink master */
105 bool up_sdnotify = 0; /* Notify systemd once link is up */
107 int maxconnect = 0; /* Maximum connect time */
108 char user[MAXNAMELEN]; /* Username for PAP */
109 char passwd[MAXSECRETLEN]; /* Password for PAP */
110 bool persist = 0; /* Reopen link after it goes down */
111 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
112 bool demand = 0; /* do dial-on-demand */
113 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
114 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
115 int holdoff = 30; /* # seconds to pause before reconnecting */
116 bool holdoff_specified; /* true if a holdoff value has been given */
117 int log_to_fd = 1; /* send log messages to this fd too */
118 bool log_default = 1; /* log_to_fd is default (stdout) */
119 int maxfail = 10; /* max # of unsuccessful connection attempts */
120 char linkname[MAXPATHLEN]; /* logical name for link */
121 bool tune_kernel; /* may alter kernel settings */
122 int connect_delay = 1000; /* wait this many ms after connect script */
123 int req_unit = -1; /* requested interface unit */
124 char path_ipup[MAXPATHLEN]; /* pathname of ip-up script */
125 char path_ipdown[MAXPATHLEN];/* pathname of ip-down script */
126 char req_ifname[IFNAMSIZ]; /* requested interface name */
127 bool multilink = 0; /* Enable multilink operation */
128 char *bundle_name = NULL; /* bundle name for multilink */
129 bool dump_options; /* print out option values */
130 bool dryrun; /* print out option values and exit */
131 char *domain; /* domain name set by domain option */
132 int child_wait = 5; /* # seconds to wait for children at exit */
133 struct userenv *userenv_list; /* user environment variables */
134 int dfl_route_metric = -1; /* metric of the default route to set over the PPP link */
136 #ifdef PPP_WITH_IPV6CP
137 char path_ipv6up[MAXPATHLEN]; /* pathname of ipv6-up script */
138 char path_ipv6down[MAXPATHLEN]; /* pathname of ipv6-down script */
141 #ifdef PPP_WITH_MAXOCTETS
142 unsigned int maxoctets = 0; /* default - no limit */
143 int maxoctets_dir = 0; /* default - sum of traffic */
144 int maxoctets_timeout = 1; /* default 1 second */
148 extern option_t auth_options[];
149 extern struct stat devstat;
151 #ifdef PPP_WITH_FILTER
152 struct bpf_program pass_filter;/* Filter program for packets to pass */
153 struct bpf_program active_filter; /* Filter program for link-active pkts */
156 static option_t *curopt; /* pointer to option being processed */
157 char *current_option; /* the name of the option being parsed */
158 int privileged_option; /* set iff the current option came from root */
159 char *option_source; /* string saying where the option came from */
160 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
161 bool devnam_fixed; /* can no longer change device name */
163 static int logfile_fd = -1; /* fd opened for log file */
164 static char logfile_name[MAXPATHLEN]; /* name of log file */
166 static bool noipx_opt; /* dummy for noipx option */
171 static int setdomain(char **);
172 static int readfile(char **);
173 static int callfile(char **);
174 static int showversion(char **);
175 static int showhelp(char **);
176 static void usage(void);
177 static int setlogfile(char **);
178 #ifdef PPP_WITH_PLUGINS
179 static int loadplugin(char **);
182 #ifdef PPP_WITH_FILTER
183 static int setpassfilter(char **);
184 static int setactivefilter(char **);
187 #ifdef PPP_WITH_MAXOCTETS
188 static int setmodir(char **);
191 static int user_setenv(char **);
192 static void user_setprint(option_t *, printer_func, void *);
193 static int user_unsetenv(char **);
194 static void user_unsetprint(option_t *, printer_func, void *);
196 static option_t *find_option(char *name);
197 static int process_option(option_t *, char *, char **);
198 static int n_arguments(option_t *);
199 static int number_option(char *, u_int32_t *, int);
202 * Structure to store extra lists of options.
206 struct option_list *next;
209 static struct option_list *extra_options = NULL;
214 option_t general_options[] = {
215 { "debug", o_int, &debug,
216 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
217 { "-d", o_int, &debug,
218 "Increase debugging level",
219 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
221 { "kdebug", o_int, &kdebugflag,
222 "Set kernel driver debug level", OPT_PRIO },
224 { "nodetach", o_bool, &nodetach,
225 "Don't detach from controlling tty", OPT_PRIO | 1 },
226 { "-detach", o_bool, &nodetach,
227 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
229 { "up_sdnotify", o_bool, &up_sdnotify,
230 "Notify systemd once link is up (implies nodetach)",
231 OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
233 { "updetach", o_bool, &updetach,
234 "Detach from controlling tty once link is up",
235 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
237 { "master_detach", o_bool, &master_detach,
238 "Detach when we're multilink master but have no link", 1 },
240 { "holdoff", o_int, &holdoff,
241 "Set time in seconds before retrying connection",
242 OPT_PRIO, &holdoff_specified },
244 { "idle", o_int, &idle_time_limit,
245 "Set time in seconds before disconnecting idle link", OPT_PRIO },
247 { "maxconnect", o_int, &maxconnect,
248 "Set connection time limit",
249 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
251 { "domain", o_special, (void *)setdomain,
252 "Add given domain name to hostname",
253 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
255 { "file", o_special, (void *)readfile,
256 "Take options from a file", OPT_NOPRINT },
257 { "call", o_special, (void *)callfile,
258 "Take options from a privileged file", OPT_NOPRINT },
260 { "persist", o_bool, &persist,
261 "Keep on reopening connection after close", OPT_PRIO | 1 },
262 { "nopersist", o_bool, &persist,
263 "Turn off persist option", OPT_PRIOSUB },
265 { "demand", o_bool, &demand,
266 "Dial on demand", OPT_INITONLY | 1, &persist },
268 { "--version", o_special_noarg, (void *)showversion,
269 "Show version number" },
270 { "--help", o_special_noarg, (void *)showhelp,
271 "Show brief listing of options" },
272 { "-h", o_special_noarg, (void *)showhelp,
273 "Show brief listing of options", OPT_ALIAS },
275 { "logfile", o_special, (void *)setlogfile,
276 "Append log messages to this file",
277 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
278 { "logfd", o_int, &log_to_fd,
279 "Send log messages to this file descriptor",
280 OPT_PRIOSUB | OPT_A2CLR, &log_default },
281 { "nolog", o_int, &log_to_fd,
282 "Don't send log messages to any file",
283 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
284 { "nologfd", o_int, &log_to_fd,
285 "Don't send log messages to any file descriptor",
286 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
288 { "linkname", o_string, linkname,
289 "Set logical name for link",
290 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
292 { "maxfail", o_int, &maxfail,
293 "Maximum number of unsuccessful connection attempts to allow",
296 { "ktune", o_bool, &tune_kernel,
297 "Alter kernel settings as necessary", OPT_PRIO | 1 },
298 { "noktune", o_bool, &tune_kernel,
299 "Don't alter kernel settings", OPT_PRIOSUB },
301 { "connect-delay", o_int, &connect_delay,
302 "Maximum time (in ms) to wait after connect script finishes",
305 { "unit", o_int, &req_unit,
306 "PPP interface unit number to use if possible",
307 OPT_PRIO | OPT_LLIMIT, 0, 0 },
309 { "ifname", o_string, req_ifname,
310 "Set PPP interface name",
311 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, IFNAMSIZ },
313 { "dump", o_bool, &dump_options,
314 "Print out option values after parsing all options", 1 },
315 { "dryrun", o_bool, &dryrun,
316 "Stop after parsing, printing, and checking options", 1 },
318 { "child-timeout", o_int, &child_wait,
319 "Number of seconds to wait for child processes at exit",
322 { "set", o_special, (void *)user_setenv,
323 "Set user environment variable",
324 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
325 { "unset", o_special, (void *)user_unsetenv,
326 "Unset user environment variable",
327 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
329 { "defaultroute-metric", o_int, &dfl_route_metric,
330 "Metric to use for the default route (Linux only; -1 for default behavior)",
331 OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
333 { "ip-up-script", o_string, path_ipup,
334 "Set pathname of ip-up script",
335 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
336 { "ip-down-script", o_string, path_ipdown,
337 "Set pathname of ip-down script",
338 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
340 #ifdef PPP_WITH_IPV6CP
341 { "ipv6-up-script", o_string, path_ipv6up,
342 "Set pathname of ipv6-up script",
343 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
344 { "ipv6-down-script", o_string, path_ipv6down,
345 "Set pathname of ipv6-down script",
346 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
349 #ifdef PPP_WITH_MULTILINK
350 { "multilink", o_bool, &multilink,
351 "Enable multilink operation", OPT_PRIO | 1 },
352 { "mp", o_bool, &multilink,
353 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
354 { "nomultilink", o_bool, &multilink,
355 "Disable multilink operation", OPT_PRIOSUB | 0 },
356 { "nomp", o_bool, &multilink,
357 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
359 { "bundle", o_string, &bundle_name,
360 "Bundle name for multilink", OPT_PRIO },
361 #endif /* PPP_WITH_MULTILINK */
363 #ifdef PPP_WITH_PLUGINS
364 { "plugin", o_special, (void *)loadplugin,
365 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
368 #ifdef PPP_WITH_FILTER
369 { "pass-filter", o_special, setpassfilter,
370 "set filter for packets to pass", OPT_PRIO },
372 { "active-filter", o_special, setactivefilter,
373 "set filter for active pkts", OPT_PRIO },
376 #ifdef PPP_WITH_MAXOCTETS
377 { "maxoctets", o_int, &maxoctets,
378 "Set connection traffic limit",
379 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
380 { "mo", o_int, &maxoctets,
381 "Set connection traffic limit",
382 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
383 { "mo-direction", o_special, setmodir,
384 "Set direction for limit traffic (sum,in,out,max)" },
385 { "mo-timeout", o_int, &maxoctets_timeout,
386 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
389 /* Dummy option, does nothing */
390 { "noipx", o_bool, &noipx_opt, NULL, OPT_NOPRINT | 1 },
395 #ifndef IMPLEMENTATION
396 #define IMPLEMENTATION ""
399 static char *usage_string = "\
401 Usage: %s [ options ], where options are:\n\
402 <device> Communicate over the named device\n\
403 <speed> Set the baud rate to <speed>\n\
404 <loc>:<rem> Set the local and/or remote interface IP\n\
405 addresses. Either one may be omitted.\n\
406 asyncmap <n> Set the desired async map to hex <n>\n\
407 auth Require authentication from peer\n\
408 connect <p> Invoke shell command <p> to set up the serial line\n\
409 crtscts Use hardware RTS/CTS flow control\n\
410 defaultroute Add default route through interface\n\
411 file <f> Take options from file <f>\n\
412 modem Use modem control lines\n\
413 mru <n> Set MRU value to <n> for negotiation\n\
414 See pppd(8) for more options.\n\
418 * parse_args - parse a string of arguments from the command line.
421 parse_args(int argc, char **argv)
427 privileged_option = privileged;
428 option_source = "command line";
429 option_priority = OPRIO_CMDLINE;
433 opt = find_option(arg);
435 option_error("unrecognized option '%s'", arg);
439 n = n_arguments(opt);
441 option_error("too few parameters for option %s", arg);
444 if (!process_option(opt, arg, argv))
453 * options_from_file - Read a string of options from a file,
454 * and interpret them.
457 options_from_file(char *filename, int must_exist, int check_prot, int priv)
460 int i, newline, ret, err;
466 char args[MAXARGS][MAXWORDLEN];
467 char cmd[MAXWORDLEN];
470 if (check_prot && seteuid(getuid()) == -1) {
471 option_error("unable to drop privileges to open %s: %m", filename);
474 f = fopen(filename, "r");
476 if (check_prot && seteuid(euid) == -1)
477 fatal("unable to regain privileges");
481 if (err != ENOENT && err != ENOTDIR)
482 warn("Warning: can't open options file %s: %m", filename);
485 option_error("Can't open options file %s: %m", filename);
489 oldpriv = privileged_option;
490 privileged_option = priv;
491 oldsource = option_source;
492 option_source = strdup(filename);
493 if (option_source == NULL)
494 option_source = "file";
496 while (getword(f, cmd, &newline, filename)) {
497 opt = find_option(cmd);
499 option_error("In file %s: unrecognized option '%s'",
503 n = n_arguments(opt);
504 for (i = 0; i < n; ++i) {
505 if (!getword(f, args[i], &newline, filename)) {
507 "In file %s: too few parameters for option '%s'",
513 if (!process_option(opt, cmd, argv))
520 privileged_option = oldpriv;
521 option_source = oldsource;
526 * options_from_user - See if the use has a ~/.ppprc file,
527 * and if so, interpret options from it.
530 options_from_user(void)
532 char *user, *path, *file;
537 pw = getpwuid(getuid());
538 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
540 file = _PATH_USEROPT;
541 pl = strlen(user) + strlen(file) + 2;
544 novm("init file name");
545 slprintf(path, pl, "%s/%s", user, file);
546 option_priority = OPRIO_CFGFILE;
547 ret = options_from_file(path, 0, 1, privileged);
553 * options_for_tty - See if an options file exists for the serial
554 * device, and if so, interpret options from it.
555 * We only allow the per-tty options file to override anything from
556 * the command line if it is something that the user can't override
557 * once it has been set by root; this is done by giving configuration
558 * files a lower priority than the command line.
561 options_for_tty(void)
563 char *dev, *path, *p;
568 if ((p = strstr(dev, "/dev/")) != NULL)
570 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
571 return 1; /* don't look for /etc/ppp/options.tty */
572 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
575 novm("tty init file name");
576 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
577 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
578 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
581 option_priority = OPRIO_CFGFILE;
582 ret = options_from_file(path, 0, 0, 1);
588 * options_from_list - process a string of options in a wordlist.
591 options_from_list(struct wordlist *w, int priv)
598 privileged_option = priv;
599 option_source = "secrets file";
600 option_priority = OPRIO_SECFILE;
603 opt = find_option(w->word);
605 option_error("In secrets file: unrecognized option '%s'",
609 n = n_arguments(opt);
611 for (i = 0; i < n; ++i) {
615 "In secrets file: too few parameters for option '%s'",
621 if (!process_option(opt, w0->word, argv))
632 * match_option - see if this option matches an option_t structure.
635 match_option(char *name, option_t *opt, int dowild)
637 int (*match)(char *, char **, int);
639 if (dowild != (opt->type == o_wild))
642 return strcmp(name, opt->name) == 0;
643 match = (int (*)(char *, char **, int)) opt->addr;
644 return (*match)(name, NULL, 0);
648 * find_option - scan the option lists for the various protocols
649 * looking for an entry with the given name.
650 * This could be optimized by using a hash table.
653 find_option(char *name)
656 struct option_list *list;
659 for (dowild = 0; dowild <= 1; ++dowild) {
660 for (opt = general_options; opt->name != NULL; ++opt)
661 if (match_option(name, opt, dowild))
663 for (opt = auth_options; opt->name != NULL; ++opt)
664 if (match_option(name, opt, dowild))
666 for (list = extra_options; list != NULL; list = list->next)
667 for (opt = list->options; opt->name != NULL; ++opt)
668 if (match_option(name, opt, dowild))
670 for (opt = the_channel->options; opt->name != NULL; ++opt)
671 if (match_option(name, opt, dowild))
673 for (i = 0; protocols[i] != NULL; ++i)
674 if ((opt = protocols[i]->options) != NULL)
675 for (; opt->name != NULL; ++opt)
676 if (match_option(name, opt, dowild))
683 * process_option - process one new-style option.
686 process_option(option_t *opt, char *cmd, char **argv)
691 int (*parser)(char **);
692 int (*wildp)(char *, char **, int);
693 char *optopt = (opt->type == o_wild)? "": " option";
694 int prio = option_priority;
695 option_t *mainopt = opt;
697 current_option = opt->name;
698 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
700 while (mainopt->flags & OPT_PRIOSUB)
702 if (mainopt->flags & OPT_PRIO) {
703 if (prio < mainopt->priority) {
704 /* new value doesn't override old */
705 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
706 option_error("%s%s set in %s cannot be overridden\n",
707 opt->name, optopt, mainopt->source);
712 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
713 warn("%s%s from %s overrides command line",
714 opt->name, optopt, option_source);
717 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
718 option_error("%s%s cannot be changed after initialization",
722 if ((opt->flags & OPT_PRIV) && !privileged_option) {
723 option_error("using the %s%s requires root privilege",
727 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
728 option_error("%s%s is disabled", opt->name, optopt);
731 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
732 option_error("the %s%s may not be changed in %s",
733 opt->name, optopt, option_source);
739 v = opt->flags & OPT_VALUE;
740 *(bool *)(opt->addr) = v;
741 if (opt->addr2 && (opt->flags & OPT_A2COPY))
742 *(bool *)(opt->addr2) = v;
743 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
744 *(bool *)(opt->addr2) = 0;
745 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
746 *(u_char *)(opt->addr2) &= ~v;
747 else if (opt->addr2 && (opt->flags & OPT_A2OR))
748 *(u_char *)(opt->addr2) |= v;
753 if ((opt->flags & OPT_NOARG) == 0) {
754 if (!int_option(*argv, &iv))
756 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
757 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
758 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
759 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
760 switch (opt->flags & OPT_LIMITS) {
762 option_error("%s value must be%s >= %d",
763 opt->name, zok, opt->lower_limit);
766 option_error("%s value must be%s <= %d",
767 opt->name, zok, opt->upper_limit);
770 option_error("%s value must be%s between %d and %d",
771 opt->name, zok, opt->lower_limit, opt->upper_limit);
777 a = opt->flags & OPT_VALUE;
779 a -= 256; /* sign extend */
781 if (opt->flags & OPT_INC)
782 iv += *(int *)(opt->addr);
783 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
784 int oldv = *(int *)(opt->addr);
785 if ((opt->flags & OPT_ZEROINF) ?
786 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
787 option_error("%s value cannot be increased", opt->name);
791 *(int *)(opt->addr) = iv;
792 if (opt->addr2 && (opt->flags & OPT_A2COPY))
793 *(int *)(opt->addr2) = iv;
797 if (opt->flags & OPT_NOARG) {
798 v = opt->flags & OPT_VALUE;
801 } else if (!number_option(*argv, &v, 16))
803 if (opt->flags & OPT_OR)
804 v |= *(u_int32_t *)(opt->addr);
805 *(u_int32_t *)(opt->addr) = v;
806 if (opt->addr2 && (opt->flags & OPT_A2COPY))
807 *(u_int32_t *)(opt->addr2) = v;
811 if (opt->flags & OPT_STATIC) {
812 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
814 char **optptr = (char **)(opt->addr);
817 novm("option argument");
822 /* obfuscate original argument for things like password */
823 if (opt->flags & OPT_HIDE) {
824 memset(*argv, '?', strlen(*argv));
829 case o_special_noarg:
831 parser = (int (*)(char **)) opt->addr;
833 if (!(*parser)(argv))
835 if (opt->flags & OPT_A2LIST) {
836 struct option_value *ovp, *pp;
838 ovp = malloc(sizeof(*ovp) + strlen(*argv));
840 strcpy(ovp->value, *argv);
841 ovp->source = option_source;
843 if (opt->addr2 == NULL) {
846 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
855 wildp = (int (*)(char *, char **, int)) opt->addr;
856 if (!(*wildp)(cmd, argv, 1))
862 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
863 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
865 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
866 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
867 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
869 mainopt->source = option_source;
870 mainopt->priority = prio;
871 mainopt->winner = opt - mainopt;
877 * override_value - if the option priorities would permit us to
878 * override the value of option, return 1 and update the priority
879 * and source of the option value. Otherwise returns 0.
882 override_value(char *option, int priority, const char *source)
886 opt = find_option(option);
889 while (opt->flags & OPT_PRIOSUB)
891 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
893 opt->priority = priority;
894 opt->source = source;
900 * n_arguments - tell how many arguments an option takes
903 n_arguments(option_t *opt)
905 return (opt->type == o_bool || opt->type == o_special_noarg
906 || (opt->flags & OPT_NOARG))? 0: 1;
910 * add_options - add a list of options to the set we grok.
913 add_options(option_t *opt)
915 struct option_list *list;
917 list = malloc(sizeof(*list));
919 novm("option list entry");
921 list->next = extra_options;
922 extra_options = list;
926 * check_options - check that options are valid and consistent.
931 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
936 * print_option - print out an option and its value
939 print_option(option_t *opt, option_t *mainopt, printer_func printer, void *arg)
944 if (opt->flags & OPT_NOPRINT)
948 v = opt->flags & OPT_VALUE;
949 if (*(bool *)opt->addr != v)
950 /* this can happen legitimately, e.g. lock
951 option turned off for default device */
953 printer(arg, "%s", opt->name);
956 v = opt->flags & OPT_VALUE;
959 i = *(int *)opt->addr;
960 if (opt->flags & OPT_NOARG) {
961 printer(arg, "%s", opt->name);
963 if (opt->flags & OPT_INC) {
964 for (; i > v; i -= v)
965 printer(arg, " %s", opt->name);
967 printer(arg, " # oops: %d not %d\n",
971 printer(arg, "%s %d", opt->name, i);
975 printer(arg, "%s", opt->name);
976 if ((opt->flags & OPT_NOARG) == 0)
977 printer(arg, " %x", *(u_int32_t *)opt->addr);
981 if (opt->flags & OPT_HIDE) {
984 p = (char *) opt->addr;
985 if ((opt->flags & OPT_STATIC) == 0)
988 printer(arg, "%s %q", opt->name, p);
992 case o_special_noarg:
994 if (opt->type != o_wild) {
995 printer(arg, "%s", opt->name);
996 if (n_arguments(opt) == 0)
1000 if (opt->flags & OPT_A2PRINTER) {
1001 void (*oprt)(option_t *, printer_func, void *);
1002 oprt = (void (*)(option_t *, printer_func, void *))
1004 (*oprt)(opt, printer, arg);
1005 } else if (opt->flags & OPT_A2STRVAL) {
1006 p = (char *) opt->addr2;
1007 if ((opt->flags & OPT_STATIC) == 0)
1009 printer(arg, "%q", p);
1010 } else if (opt->flags & OPT_A2LIST) {
1011 struct option_value *ovp;
1013 ovp = (struct option_value *) opt->addr2;
1015 printer(arg, "%q", ovp->value);
1016 if ((ovp = ovp->next) == NULL)
1018 printer(arg, "\t\t# (from %s)\n%s ",
1019 ovp->source, opt->name);
1022 printer(arg, "xxx # [don't know how to print value]");
1027 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1030 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1034 * print_option_list - print out options in effect from an
1038 print_option_list(option_t *opt, printer_func printer, void *arg)
1040 while (opt->name != NULL) {
1041 if (opt->priority != OPRIO_DEFAULT
1042 && opt->winner != (short int) -1)
1043 print_option(opt + opt->winner, opt, printer, arg);
1046 } while (opt->flags & OPT_PRIOSUB);
1051 * print_options - print out what options are in effect.
1054 print_options(printer_func printer, void *arg)
1056 struct option_list *list;
1059 printer(arg, "pppd options in effect:\n");
1060 print_option_list(general_options, printer, arg);
1061 print_option_list(auth_options, printer, arg);
1062 for (list = extra_options; list != NULL; list = list->next)
1063 print_option_list(list->options, printer, arg);
1064 print_option_list(the_channel->options, printer, arg);
1065 for (i = 0; protocols[i] != NULL; ++i)
1066 print_option_list(protocols[i]->options, printer, arg);
1070 * usage - print out a message telling how to use the program.
1075 if (phase == PHASE_INITIALIZE)
1076 fprintf(stderr, usage_string, VERSION, progname);
1080 * showhelp - print out usage message and exit.
1083 showhelp(char **argv)
1085 if (phase == PHASE_INITIALIZE) {
1093 * showversion - print out the version number and exit.
1096 showversion(char **argv)
1098 if (phase == PHASE_INITIALIZE) {
1099 fprintf(stdout, "pppd version %s\n", VERSION);
1106 * option_error - print a message about an error in an option.
1107 * The message is logged, and also sent to
1108 * stderr if phase == PHASE_INITIALIZE.
1111 option_error(char *fmt, ...)
1116 va_start(args, fmt);
1117 vslprintf(buf, sizeof(buf), fmt, args);
1119 if (phase == PHASE_INITIALIZE)
1120 fprintf(stderr, "%s: %s\n", progname, buf);
1121 syslog(LOG_ERR, "%s", buf);
1126 * readable - check if a file is readable by the real user.
1138 if (fstat(fd, &sbuf) != 0)
1140 if (sbuf.st_uid == uid)
1141 return sbuf.st_mode & S_IRUSR;
1142 if (sbuf.st_gid == getgid())
1143 return sbuf.st_mode & S_IRGRP;
1144 for (i = 0; i < ngroups; ++i)
1145 if (sbuf.st_gid == groups[i])
1146 return sbuf.st_mode & S_IRGRP;
1147 return sbuf.st_mode & S_IROTH;
1152 * Read a word from a file.
1153 * Words are delimited by white-space or by quotes (" or ').
1154 * Quotes, white-space and \ may be escaped with \.
1155 * \<newline> is ignored.
1158 getword(FILE *f, char *word, int *newlinep, char *filename)
1161 int quoted, comment;
1162 int value, digit, got, n;
1164 #define isoctal(c) ((c) >= '0' && (c) < '8')
1173 * First skip white-space and comments.
1181 * A newline means the end of a comment; backslash-newline
1182 * is ignored. Note that we cannot have escape && comment.
1194 * Ignore characters other than newline in a comment.
1200 * If this character is escaped, we have a word start.
1206 * If this is the escape character, look at the next character.
1214 * If this is the start of a comment, ignore the rest of the line.
1222 * A non-whitespace character is the start of a word.
1229 * Process characters until the end of the word.
1234 * This character is escaped: backslash-newline is ignored,
1235 * various other characters indicate particular values
1236 * as for C backslash-escapes.
1271 * \ddd octal sequence
1274 for (n = 0; n < 3 && isoctal(c); ++n) {
1275 value = (value << 3) + (c & 07);
1284 * \x<hex_string> sequence
1288 for (n = 0; n < 2 && isxdigit(c); ++n) {
1289 digit = toupper(c) - '0';
1291 digit += '0' + 10 - 'A';
1292 value = (value << 4) + digit;
1300 * Otherwise the character stands for itself.
1307 * Store the resulting character for the escape sequence.
1309 if (len < MAXWORDLEN) {
1320 * Backslash starts a new escape sequence.
1329 * Not escaped: check for the start or end of a quoted
1330 * section and see if we've reached the end of the word.
1338 } else if (c == '"' || c == '\'') {
1342 } else if (isspace(c) || c == '#') {
1348 * An ordinary character: store it in the word and get another.
1350 if (len < MAXWORDLEN) {
1357 word[MAXWORDLEN-1] = 0; /* make sure word is null-terminated */
1360 * End of the word: check for errors.
1366 option_error("Error reading %s: %m", filename);
1370 * If len is zero, then we didn't find a word before the
1376 option_error("warning: quoted word runs to end of file (%.20s...)",
1381 * Warn if the word was too long, and append a terminating null.
1383 if (len >= MAXWORDLEN) {
1384 option_error("warning: word in file %s too long (%.20s...)",
1386 len = MAXWORDLEN - 1;
1397 * number_option - parse an unsigned numeric parameter for an option.
1400 number_option(char *str, u_int32_t *valp, int base)
1404 *valp = strtoul(str, &ptr, base);
1406 option_error("invalid numeric parameter '%s' for %s option",
1407 str, current_option);
1415 * int_option - like number_option, but valp is int *,
1416 * the base is assumed to be 0, and *valp is not changed
1417 * if there is an error.
1420 int_option(char *str, int *valp)
1424 if (!number_option(str, &v, 0))
1432 * The following procedures parse options.
1436 * readfile - take commands from a file.
1439 readfile(char **argv)
1441 return options_from_file(*argv, 1, 1, privileged_option);
1445 * callfile - take commands from /etc/ppp/peers/<name>.
1446 * Name may not contain /../, start with / or ../, or end in /..
1449 callfile(char **argv)
1451 char *fname, *arg, *p;
1456 if (arg[0] == '/' || arg[0] == 0)
1459 for (p = arg; *p != 0; ) {
1460 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1464 while (*p != '/' && *p != 0)
1471 option_error("call option value may not contain .. or start with /");
1475 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1476 if ((fname = (char *) malloc(l)) == NULL)
1477 novm("call file name");
1478 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1479 script_setenv("CALL_FILE", arg, 0);
1481 ok = options_from_file(fname, 1, 1, 1);
1487 #ifdef PPP_WITH_FILTER
1489 * setpassfilter - Set the pass filter for packets
1492 setpassfilter(char **argv)
1497 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1498 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1499 option_error("error in pass-filter expression: %s\n",
1509 * setactivefilter - Set the active filter for packets
1512 setactivefilter(char **argv)
1517 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1518 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1519 option_error("error in active-filter expression: %s\n",
1530 * setdomain - Set domain name to append to hostname
1533 setdomain(char **argv)
1535 gethostname(hostname, MAXNAMELEN);
1538 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1539 domain = hostname + strlen(hostname);
1540 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1542 hostname[MAXNAMELEN-1] = 0;
1547 setlogfile(char **argv)
1553 if (!privileged_option && seteuid(getuid()) == -1) {
1554 option_error("unable to drop permissions to open %s: %m", *argv);
1557 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1558 if (fd < 0 && errno == EEXIST)
1559 fd = open(*argv, O_WRONLY | O_APPEND);
1561 if (!privileged_option && seteuid(euid) == -1)
1562 fatal("unable to regain privileges: %m");
1565 option_error("Can't open log file %s: %m", *argv);
1568 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1569 if (logfile_fd >= 0)
1577 #ifdef PPP_WITH_MAXOCTETS
1579 setmodir(char **argv)
1583 if(!strcmp(*argv,"in")) {
1584 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1585 } else if (!strcmp(*argv,"out")) {
1586 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1587 } else if (!strcmp(*argv,"max")) {
1588 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1590 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1596 #ifdef PPP_WITH_PLUGINS
1598 loadplugin(char **argv)
1607 if (strchr(arg, '/') == 0) {
1608 const char *base = _PATH_PLUGIN;
1609 int l = strlen(base) + strlen(arg) + 2;
1612 novm("plugin file path");
1613 strlcpy(path, base, l);
1614 strlcat(path, "/", l);
1615 strlcat(path, arg, l);
1617 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1621 option_error("%s", err);
1622 option_error("Couldn't load plugin %s", arg);
1625 init = (void (*)(void))dlsym(handle, "plugin_init");
1627 option_error("%s has no initialization entry point", arg);
1630 vers = (const char *) dlsym(handle, "pppd_version");
1632 warn("Warning: plugin %s has no version information", arg);
1633 } else if (strcmp(vers, VERSION) != 0) {
1634 option_error("Plugin %s is for pppd version %s, this is %s",
1635 arg, vers, VERSION);
1638 info("Plugin %s loaded.", arg);
1649 #endif /* PPP_WITH_PLUGINS */
1652 * Set an environment variable specified by the user.
1655 user_setenv(char **argv)
1657 char *arg = argv[0];
1659 struct userenv *uep, **insp;
1661 if ((eqp = strchr(arg, '=')) == NULL) {
1662 option_error("missing = in name=value: %s", arg);
1666 option_error("missing variable name: %s", arg);
1669 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1670 int nlen = strlen(uep->ue_name);
1671 if (nlen == (eqp - arg) &&
1672 strncmp(arg, uep->ue_name, nlen) == 0)
1675 /* Ignore attempts by unprivileged users to override privileged sources */
1676 if (uep != NULL && !privileged_option && uep->ue_priv)
1678 /* The name never changes, so allocate it with the structure */
1680 uep = malloc(sizeof (*uep) + (eqp-arg));
1681 strncpy(uep->ue_name, arg, eqp-arg);
1682 uep->ue_name[eqp-arg] = '\0';
1683 uep->ue_next = NULL;
1684 insp = &userenv_list;
1685 while (*insp != NULL)
1686 insp = &(*insp)->ue_next;
1689 struct userenv *uep2;
1690 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1691 if (uep2 != uep && !uep2->ue_isset)
1694 if (uep2 == NULL && !uep->ue_isset)
1695 find_option("unset")->flags |= OPT_NOPRINT;
1696 free(uep->ue_value);
1699 uep->ue_priv = privileged_option;
1700 uep->ue_source = option_source;
1701 uep->ue_value = strdup(eqp + 1);
1702 curopt->flags &= ~OPT_NOPRINT;
1707 user_setprint(option_t *opt, printer_func printer, void *arg)
1709 struct userenv *uep, *uepnext;
1711 uepnext = userenv_list;
1712 while (uepnext != NULL && !uepnext->ue_isset)
1713 uepnext = uepnext->ue_next;
1714 while ((uep = uepnext) != NULL) {
1715 uepnext = uep->ue_next;
1716 while (uepnext != NULL && !uepnext->ue_isset)
1717 uepnext = uepnext->ue_next;
1718 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1719 if (uepnext != NULL)
1720 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1722 opt->source = uep->ue_source;
1727 user_unsetenv(char **argv)
1729 struct userenv *uep, **insp;
1730 char *arg = argv[0];
1732 if (strchr(arg, '=') != NULL) {
1733 option_error("unexpected = in name: %s", arg);
1737 option_error("missing variable name for unset");
1740 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1741 if (strcmp(arg, uep->ue_name) == 0)
1744 /* Ignore attempts by unprivileged users to override privileged sources */
1745 if (uep != NULL && !privileged_option && uep->ue_priv)
1747 /* The name never changes, so allocate it with the structure */
1749 uep = malloc(sizeof (*uep) + strlen(arg));
1750 strcpy(uep->ue_name, arg);
1751 uep->ue_next = NULL;
1752 insp = &userenv_list;
1753 while (*insp != NULL)
1754 insp = &(*insp)->ue_next;
1757 struct userenv *uep2;
1758 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1759 if (uep2 != uep && uep2->ue_isset)
1762 if (uep2 == NULL && uep->ue_isset)
1763 find_option("set")->flags |= OPT_NOPRINT;
1764 free(uep->ue_value);
1767 uep->ue_priv = privileged_option;
1768 uep->ue_source = option_source;
1769 uep->ue_value = NULL;
1770 curopt->flags &= ~OPT_NOPRINT;
1775 user_unsetprint(option_t *opt, printer_func printer, void *arg)
1777 struct userenv *uep, *uepnext;
1779 uepnext = userenv_list;
1780 while (uepnext != NULL && uepnext->ue_isset)
1781 uepnext = uepnext->ue_next;
1782 while ((uep = uepnext) != NULL) {
1783 uepnext = uep->ue_next;
1784 while (uepnext != NULL && uepnext->ue_isset)
1785 uepnext = uepnext->ue_next;
1786 (*printer)(arg, "%s", uep->ue_name);
1787 if (uepnext != NULL)
1788 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1790 opt->source = uep->ue_source;