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 unsigned int maxoctets = 0; /* default - no limit */
142 int maxoctets_dir = 0; /* default - sum of traffic */
143 int maxoctets_timeout = 1; /* default 1 second */
146 extern option_t auth_options[];
147 extern struct stat devstat;
149 #ifdef PPP_WITH_FILTER
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 **);
176 #ifdef PPP_WITH_PLUGINS
177 static int loadplugin(char **);
180 #ifdef PPP_WITH_FILTER
181 static int setpassfilter(char **);
182 static int setactivefilter(char **);
185 static int setmodir(char **);
187 static int user_setenv(char **);
188 static void user_setprint(option_t *, printer_func, void *);
189 static int user_unsetenv(char **);
190 static void user_unsetprint(option_t *, printer_func, void *);
192 static option_t *find_option(char *name);
193 static int process_option(option_t *, char *, char **);
194 static int n_arguments(option_t *);
195 static int number_option(char *, u_int32_t *, int);
198 * Structure to store extra lists of options.
202 struct option_list *next;
205 static struct option_list *extra_options = NULL;
210 option_t general_options[] = {
211 { "debug", o_int, &debug,
212 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
213 { "-d", o_int, &debug,
214 "Increase debugging level",
215 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
217 { "kdebug", o_int, &kdebugflag,
218 "Set kernel driver debug level", OPT_PRIO },
220 { "nodetach", o_bool, &nodetach,
221 "Don't detach from controlling tty", OPT_PRIO | 1 },
222 { "-detach", o_bool, &nodetach,
223 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
225 { "up_sdnotify", o_bool, &up_sdnotify,
226 "Notify systemd once link is up (implies nodetach)",
227 OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
229 { "updetach", o_bool, &updetach,
230 "Detach from controlling tty once link is up",
231 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
233 { "master_detach", o_bool, &master_detach,
234 "Detach when we're multilink master but have no link", 1 },
236 { "holdoff", o_int, &holdoff,
237 "Set time in seconds before retrying connection",
238 OPT_PRIO, &holdoff_specified },
240 { "idle", o_int, &idle_time_limit,
241 "Set time in seconds before disconnecting idle link", OPT_PRIO },
243 { "maxconnect", o_int, &maxconnect,
244 "Set connection time limit",
245 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
247 { "domain", o_special, (void *)setdomain,
248 "Add given domain name to hostname",
249 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
251 { "file", o_special, (void *)readfile,
252 "Take options from a file", OPT_NOPRINT },
253 { "call", o_special, (void *)callfile,
254 "Take options from a privileged file", OPT_NOPRINT },
256 { "persist", o_bool, &persist,
257 "Keep on reopening connection after close", OPT_PRIO | 1 },
258 { "nopersist", o_bool, &persist,
259 "Turn off persist option", OPT_PRIOSUB },
261 { "demand", o_bool, &demand,
262 "Dial on demand", OPT_INITONLY | 1, &persist },
264 { "--version", o_special_noarg, (void *)showversion,
265 "Show version number" },
266 { "--help", o_special_noarg, (void *)showhelp,
267 "Show brief listing of options" },
268 { "-h", o_special_noarg, (void *)showhelp,
269 "Show brief listing of options", OPT_ALIAS },
271 { "logfile", o_special, (void *)setlogfile,
272 "Append log messages to this file",
273 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
274 { "logfd", o_int, &log_to_fd,
275 "Send log messages to this file descriptor",
276 OPT_PRIOSUB | OPT_A2CLR, &log_default },
277 { "nolog", o_int, &log_to_fd,
278 "Don't send log messages to any file",
279 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
280 { "nologfd", o_int, &log_to_fd,
281 "Don't send log messages to any file descriptor",
282 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
284 { "linkname", o_string, linkname,
285 "Set logical name for link",
286 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
288 { "maxfail", o_int, &maxfail,
289 "Maximum number of unsuccessful connection attempts to allow",
292 { "ktune", o_bool, &tune_kernel,
293 "Alter kernel settings as necessary", OPT_PRIO | 1 },
294 { "noktune", o_bool, &tune_kernel,
295 "Don't alter kernel settings", OPT_PRIOSUB },
297 { "connect-delay", o_int, &connect_delay,
298 "Maximum time (in ms) to wait after connect script finishes",
301 { "unit", o_int, &req_unit,
302 "PPP interface unit number to use if possible",
303 OPT_PRIO | OPT_LLIMIT, 0, 0 },
305 { "ifname", o_string, req_ifname,
306 "Set PPP interface name",
307 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, IFNAMSIZ },
309 { "dump", o_bool, &dump_options,
310 "Print out option values after parsing all options", 1 },
311 { "dryrun", o_bool, &dryrun,
312 "Stop after parsing, printing, and checking options", 1 },
314 { "child-timeout", o_int, &child_wait,
315 "Number of seconds to wait for child processes at exit",
318 { "set", o_special, (void *)user_setenv,
319 "Set user environment variable",
320 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
321 { "unset", o_special, (void *)user_unsetenv,
322 "Unset user environment variable",
323 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
325 { "defaultroute-metric", o_int, &dfl_route_metric,
326 "Metric to use for the default route (Linux only; -1 for default behavior)",
327 OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
329 { "ip-up-script", o_string, path_ipup,
330 "Set pathname of ip-up script",
331 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
332 { "ip-down-script", o_string, path_ipdown,
333 "Set pathname of ip-down script",
334 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
336 #ifdef PPP_WITH_IPV6CP
337 { "ipv6-up-script", o_string, path_ipv6up,
338 "Set pathname of ipv6-up script",
339 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
340 { "ipv6-down-script", o_string, path_ipv6down,
341 "Set pathname of ipv6-down script",
342 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
345 #ifdef PPP_WITH_MULTILINK
346 { "multilink", o_bool, &multilink,
347 "Enable multilink operation", OPT_PRIO | 1 },
348 { "mp", o_bool, &multilink,
349 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
350 { "nomultilink", o_bool, &multilink,
351 "Disable multilink operation", OPT_PRIOSUB | 0 },
352 { "nomp", o_bool, &multilink,
353 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
355 { "bundle", o_string, &bundle_name,
356 "Bundle name for multilink", OPT_PRIO },
357 #endif /* PPP_WITH_MULTILINK */
359 #ifdef PPP_WITH_PLUGINS
360 { "plugin", o_special, (void *)loadplugin,
361 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
364 #ifdef PPP_WITH_FILTER
365 { "pass-filter", o_special, setpassfilter,
366 "set filter for packets to pass", OPT_PRIO },
368 { "active-filter", o_special, setactivefilter,
369 "set filter for active pkts", OPT_PRIO },
372 { "maxoctets", o_int, &maxoctets,
373 "Set connection traffic limit",
374 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
375 { "mo", o_int, &maxoctets,
376 "Set connection traffic limit",
377 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
378 { "mo-direction", o_special, setmodir,
379 "Set direction for limit traffic (sum,in,out,max)" },
380 { "mo-timeout", o_int, &maxoctets_timeout,
381 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
383 /* Dummy option, does nothing */
384 { "noipx", o_bool, &noipx_opt, NULL, OPT_NOPRINT | 1 },
389 #ifndef IMPLEMENTATION
390 #define IMPLEMENTATION ""
393 static char *usage_string = "\
395 Usage: %s [ options ], where options are:\n\
396 <device> Communicate over the named device\n\
397 <speed> Set the baud rate to <speed>\n\
398 <loc>:<rem> Set the local and/or remote interface IP\n\
399 addresses. Either one may be omitted.\n\
400 asyncmap <n> Set the desired async map to hex <n>\n\
401 auth Require authentication from peer\n\
402 connect <p> Invoke shell command <p> to set up the serial line\n\
403 crtscts Use hardware RTS/CTS flow control\n\
404 defaultroute Add default route through interface\n\
405 file <f> Take options from file <f>\n\
406 modem Use modem control lines\n\
407 mru <n> Set MRU value to <n> for negotiation\n\
408 See pppd(8) for more options.\n\
412 * parse_args - parse a string of arguments from the command line.
415 parse_args(int argc, char **argv)
421 privileged_option = privileged;
422 option_source = "command line";
423 option_priority = OPRIO_CMDLINE;
427 opt = find_option(arg);
429 option_error("unrecognized option '%s'", arg);
433 n = n_arguments(opt);
435 option_error("too few parameters for option %s", arg);
438 if (!process_option(opt, arg, argv))
447 * options_from_file - Read a string of options from a file,
448 * and interpret them.
451 options_from_file(char *filename, int must_exist, int check_prot, int priv)
454 int i, newline, ret, err;
460 char args[MAXARGS][MAXWORDLEN];
461 char cmd[MAXWORDLEN];
464 if (check_prot && seteuid(getuid()) == -1) {
465 option_error("unable to drop privileges to open %s: %m", filename);
468 f = fopen(filename, "r");
470 if (check_prot && seteuid(euid) == -1)
471 fatal("unable to regain privileges");
475 if (err != ENOENT && err != ENOTDIR)
476 warn("Warning: can't open options file %s: %m", filename);
479 option_error("Can't open options file %s: %m", filename);
483 oldpriv = privileged_option;
484 privileged_option = priv;
485 oldsource = option_source;
486 option_source = strdup(filename);
487 if (option_source == NULL)
488 option_source = "file";
490 while (getword(f, cmd, &newline, filename)) {
491 opt = find_option(cmd);
493 option_error("In file %s: unrecognized option '%s'",
497 n = n_arguments(opt);
498 for (i = 0; i < n; ++i) {
499 if (!getword(f, args[i], &newline, filename)) {
501 "In file %s: too few parameters for option '%s'",
507 if (!process_option(opt, cmd, argv))
514 privileged_option = oldpriv;
515 option_source = oldsource;
520 * options_from_user - See if the use has a ~/.ppprc file,
521 * and if so, interpret options from it.
524 options_from_user(void)
526 char *user, *path, *file;
531 pw = getpwuid(getuid());
532 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
534 file = PPP_PATH_USEROPT;
535 pl = strlen(user) + strlen(file) + 2;
538 novm("init file name");
539 slprintf(path, pl, "%s/%s", user, file);
540 option_priority = OPRIO_CFGFILE;
541 ret = options_from_file(path, 0, 1, privileged);
547 * options_for_tty - See if an options file exists for the serial
548 * device, and if so, interpret options from it.
549 * We only allow the per-tty options file to override anything from
550 * the command line if it is something that the user can't override
551 * once it has been set by root; this is done by giving configuration
552 * files a lower priority than the command line.
555 options_for_tty(void)
557 char *dev, *path, *p;
562 if ((p = strstr(dev, "/dev/")) != NULL)
564 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
565 return 1; /* don't look for /etc/ppp/options.tty */
566 pl = strlen(PPP_PATH_TTYOPT) + strlen(dev) + 1;
569 novm("tty init file name");
570 slprintf(path, pl, "%s%s", PPP_PATH_TTYOPT, dev);
571 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
572 for (p = path + strlen(PPP_PATH_TTYOPT); *p != 0; ++p)
575 option_priority = OPRIO_CFGFILE;
576 ret = options_from_file(path, 0, 0, 1);
582 * options_from_list - process a string of options in a wordlist.
585 options_from_list(struct wordlist *w, int priv)
592 privileged_option = priv;
593 option_source = "secrets file";
594 option_priority = OPRIO_SECFILE;
597 opt = find_option(w->word);
599 option_error("In secrets file: unrecognized option '%s'",
603 n = n_arguments(opt);
605 for (i = 0; i < n; ++i) {
609 "In secrets file: too few parameters for option '%s'",
615 if (!process_option(opt, w0->word, argv))
626 * match_option - see if this option matches an option_t structure.
629 match_option(char *name, option_t *opt, int dowild)
631 int (*match)(char *, char **, int);
633 if (dowild != (opt->type == o_wild))
636 return strcmp(name, opt->name) == 0;
637 match = (int (*)(char *, char **, int)) opt->addr;
638 return (*match)(name, NULL, 0);
642 * find_option - scan the option lists for the various protocols
643 * looking for an entry with the given name.
644 * This could be optimized by using a hash table.
647 find_option(char *name)
650 struct option_list *list;
653 for (dowild = 0; dowild <= 1; ++dowild) {
654 for (opt = general_options; opt->name != NULL; ++opt)
655 if (match_option(name, opt, dowild))
657 for (opt = auth_options; opt->name != NULL; ++opt)
658 if (match_option(name, opt, dowild))
660 for (list = extra_options; list != NULL; list = list->next)
661 for (opt = list->options; opt->name != NULL; ++opt)
662 if (match_option(name, opt, dowild))
664 for (opt = the_channel->options; opt->name != NULL; ++opt)
665 if (match_option(name, opt, dowild))
667 for (i = 0; protocols[i] != NULL; ++i)
668 if ((opt = protocols[i]->options) != NULL)
669 for (; opt->name != NULL; ++opt)
670 if (match_option(name, opt, dowild))
677 * process_option - process one new-style option.
680 process_option(option_t *opt, char *cmd, char **argv)
685 int (*parser)(char **);
686 int (*wildp)(char *, char **, int);
687 char *optopt = (opt->type == o_wild)? "": " option";
688 int prio = option_priority;
689 option_t *mainopt = opt;
691 current_option = opt->name;
692 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
694 while (mainopt->flags & OPT_PRIOSUB)
696 if (mainopt->flags & OPT_PRIO) {
697 if (prio < mainopt->priority) {
698 /* new value doesn't override old */
699 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
700 option_error("%s%s set in %s cannot be overridden\n",
701 opt->name, optopt, mainopt->source);
706 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
707 warn("%s%s from %s overrides command line",
708 opt->name, optopt, option_source);
711 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
712 option_error("%s%s cannot be changed after initialization",
716 if ((opt->flags & OPT_PRIV) && !privileged_option) {
717 option_error("using the %s%s requires root privilege",
721 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
722 option_error("%s%s is disabled", opt->name, optopt);
725 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
726 option_error("the %s%s may not be changed in %s",
727 opt->name, optopt, option_source);
733 v = opt->flags & OPT_VALUE;
734 *(bool *)(opt->addr) = v;
735 if (opt->addr2 && (opt->flags & OPT_A2COPY))
736 *(bool *)(opt->addr2) = v;
737 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
738 *(bool *)(opt->addr2) = 0;
739 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
740 *(u_char *)(opt->addr2) &= ~v;
741 else if (opt->addr2 && (opt->flags & OPT_A2OR))
742 *(u_char *)(opt->addr2) |= v;
747 if ((opt->flags & OPT_NOARG) == 0) {
748 if (!int_option(*argv, &iv))
750 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
751 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
752 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
753 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
754 switch (opt->flags & OPT_LIMITS) {
756 option_error("%s value must be%s >= %d",
757 opt->name, zok, opt->lower_limit);
760 option_error("%s value must be%s <= %d",
761 opt->name, zok, opt->upper_limit);
764 option_error("%s value must be%s between %d and %d",
765 opt->name, zok, opt->lower_limit, opt->upper_limit);
771 a = opt->flags & OPT_VALUE;
773 a -= 256; /* sign extend */
775 if (opt->flags & OPT_INC)
776 iv += *(int *)(opt->addr);
777 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
778 int oldv = *(int *)(opt->addr);
779 if ((opt->flags & OPT_ZEROINF) ?
780 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
781 option_error("%s value cannot be increased", opt->name);
785 *(int *)(opt->addr) = iv;
786 if (opt->addr2 && (opt->flags & OPT_A2COPY))
787 *(int *)(opt->addr2) = iv;
791 if (opt->flags & OPT_NOARG) {
792 v = opt->flags & OPT_VALUE;
795 } else if (!number_option(*argv, &v, 16))
797 if (opt->flags & OPT_OR)
798 v |= *(u_int32_t *)(opt->addr);
799 *(u_int32_t *)(opt->addr) = v;
800 if (opt->addr2 && (opt->flags & OPT_A2COPY))
801 *(u_int32_t *)(opt->addr2) = v;
805 if (opt->flags & OPT_STATIC) {
806 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
808 char **optptr = (char **)(opt->addr);
811 novm("option argument");
816 /* obfuscate original argument for things like password */
817 if (opt->flags & OPT_HIDE) {
818 memset(*argv, '?', strlen(*argv));
823 case o_special_noarg:
825 parser = (int (*)(char **)) opt->addr;
827 if (!(*parser)(argv))
829 if (opt->flags & OPT_A2LIST) {
830 struct option_value *ovp, *pp;
832 ovp = malloc(sizeof(*ovp) + strlen(*argv));
834 strcpy(ovp->value, *argv);
835 ovp->source = option_source;
837 if (opt->addr2 == NULL) {
840 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
849 wildp = (int (*)(char *, char **, int)) opt->addr;
850 if (!(*wildp)(cmd, argv, 1))
856 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
857 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
859 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
860 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
861 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
863 mainopt->source = option_source;
864 mainopt->priority = prio;
865 mainopt->winner = opt - mainopt;
871 * override_value - if the option priorities would permit us to
872 * override the value of option, return 1 and update the priority
873 * and source of the option value. Otherwise returns 0.
876 override_value(char *option, int priority, const char *source)
880 opt = find_option(option);
883 while (opt->flags & OPT_PRIOSUB)
885 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
887 opt->priority = priority;
888 opt->source = source;
894 * n_arguments - tell how many arguments an option takes
897 n_arguments(option_t *opt)
899 return (opt->type == o_bool || opt->type == o_special_noarg
900 || (opt->flags & OPT_NOARG))? 0: 1;
904 * add_options - add a list of options to the set we grok.
907 add_options(option_t *opt)
909 struct option_list *list;
911 list = malloc(sizeof(*list));
913 novm("option list entry");
915 list->next = extra_options;
916 extra_options = list;
920 * check_options - check that options are valid and consistent.
925 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
930 * print_option - print out an option and its value
933 print_option(option_t *opt, option_t *mainopt, printer_func printer, void *arg)
938 if (opt->flags & OPT_NOPRINT)
942 v = opt->flags & OPT_VALUE;
943 if (*(bool *)opt->addr != v)
944 /* this can happen legitimately, e.g. lock
945 option turned off for default device */
947 printer(arg, "%s", opt->name);
950 v = opt->flags & OPT_VALUE;
953 i = *(int *)opt->addr;
954 if (opt->flags & OPT_NOARG) {
955 printer(arg, "%s", opt->name);
957 if (opt->flags & OPT_INC) {
958 for (; i > v; i -= v)
959 printer(arg, " %s", opt->name);
961 printer(arg, " # oops: %d not %d\n",
965 printer(arg, "%s %d", opt->name, i);
969 printer(arg, "%s", opt->name);
970 if ((opt->flags & OPT_NOARG) == 0)
971 printer(arg, " %x", *(u_int32_t *)opt->addr);
975 if (opt->flags & OPT_HIDE) {
978 p = (char *) opt->addr;
979 if ((opt->flags & OPT_STATIC) == 0)
982 printer(arg, "%s %q", opt->name, p);
986 case o_special_noarg:
988 if (opt->type != o_wild) {
989 printer(arg, "%s", opt->name);
990 if (n_arguments(opt) == 0)
994 if (opt->flags & OPT_A2PRINTER) {
995 void (*oprt)(option_t *, printer_func, void *);
996 oprt = (void (*)(option_t *, printer_func, void *))
998 (*oprt)(opt, printer, arg);
999 } else if (opt->flags & OPT_A2STRVAL) {
1000 p = (char *) opt->addr2;
1001 if ((opt->flags & OPT_STATIC) == 0)
1003 printer(arg, "%q", p);
1004 } else if (opt->flags & OPT_A2LIST) {
1005 struct option_value *ovp;
1007 ovp = (struct option_value *) opt->addr2;
1009 printer(arg, "%q", ovp->value);
1010 if ((ovp = ovp->next) == NULL)
1012 printer(arg, "\t\t# (from %s)\n%s ",
1013 ovp->source, opt->name);
1016 printer(arg, "xxx # [don't know how to print value]");
1021 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1024 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1028 * print_option_list - print out options in effect from an
1032 print_option_list(option_t *opt, printer_func printer, void *arg)
1034 while (opt->name != NULL) {
1035 if (opt->priority != OPRIO_DEFAULT
1036 && opt->winner != (short int) -1)
1037 print_option(opt + opt->winner, opt, printer, arg);
1040 } while (opt->flags & OPT_PRIOSUB);
1045 * print_options - print out what options are in effect.
1048 print_options(printer_func printer, void *arg)
1050 struct option_list *list;
1053 printer(arg, "pppd options in effect:\n");
1054 print_option_list(general_options, printer, arg);
1055 print_option_list(auth_options, printer, arg);
1056 for (list = extra_options; list != NULL; list = list->next)
1057 print_option_list(list->options, printer, arg);
1058 print_option_list(the_channel->options, printer, arg);
1059 for (i = 0; protocols[i] != NULL; ++i)
1060 print_option_list(protocols[i]->options, printer, arg);
1064 * usage - print out a message telling how to use the program.
1069 if (phase == PHASE_INITIALIZE)
1070 fprintf(stderr, usage_string, VERSION, progname);
1074 * showhelp - print out usage message and exit.
1077 showhelp(char **argv)
1079 if (phase == PHASE_INITIALIZE) {
1087 * showversion - print out the version number and exit.
1090 showversion(char **argv)
1092 if (phase == PHASE_INITIALIZE) {
1093 fprintf(stdout, "pppd version %s\n", VERSION);
1100 * option_error - print a message about an error in an option.
1101 * The message is logged, and also sent to
1102 * stderr if phase == PHASE_INITIALIZE.
1105 option_error(char *fmt, ...)
1110 va_start(args, fmt);
1111 vslprintf(buf, sizeof(buf), fmt, args);
1113 if (phase == PHASE_INITIALIZE)
1114 fprintf(stderr, "%s: %s\n", progname, buf);
1115 syslog(LOG_ERR, "%s", buf);
1120 * readable - check if a file is readable by the real user.
1132 if (fstat(fd, &sbuf) != 0)
1134 if (sbuf.st_uid == uid)
1135 return sbuf.st_mode & S_IRUSR;
1136 if (sbuf.st_gid == getgid())
1137 return sbuf.st_mode & S_IRGRP;
1138 for (i = 0; i < ngroups; ++i)
1139 if (sbuf.st_gid == groups[i])
1140 return sbuf.st_mode & S_IRGRP;
1141 return sbuf.st_mode & S_IROTH;
1146 * Read a word from a file.
1147 * Words are delimited by white-space or by quotes (" or ').
1148 * Quotes, white-space and \ may be escaped with \.
1149 * \<newline> is ignored.
1152 getword(FILE *f, char *word, int *newlinep, char *filename)
1155 int quoted, comment;
1156 int value, digit, got, n;
1158 #define isoctal(c) ((c) >= '0' && (c) < '8')
1167 * First skip white-space and comments.
1175 * A newline means the end of a comment; backslash-newline
1176 * is ignored. Note that we cannot have escape && comment.
1188 * Ignore characters other than newline in a comment.
1194 * If this character is escaped, we have a word start.
1200 * If this is the escape character, look at the next character.
1208 * If this is the start of a comment, ignore the rest of the line.
1216 * A non-whitespace character is the start of a word.
1223 * Process characters until the end of the word.
1228 * This character is escaped: backslash-newline is ignored,
1229 * various other characters indicate particular values
1230 * as for C backslash-escapes.
1265 * \ddd octal sequence
1268 for (n = 0; n < 3 && isoctal(c); ++n) {
1269 value = (value << 3) + (c & 07);
1278 * \x<hex_string> sequence
1282 for (n = 0; n < 2 && isxdigit(c); ++n) {
1283 digit = toupper(c) - '0';
1285 digit += '0' + 10 - 'A';
1286 value = (value << 4) + digit;
1294 * Otherwise the character stands for itself.
1301 * Store the resulting character for the escape sequence.
1303 if (len < MAXWORDLEN) {
1314 * Backslash starts a new escape sequence.
1323 * Not escaped: check for the start or end of a quoted
1324 * section and see if we've reached the end of the word.
1332 } else if (c == '"' || c == '\'') {
1336 } else if (isspace(c) || c == '#') {
1342 * An ordinary character: store it in the word and get another.
1344 if (len < MAXWORDLEN) {
1351 word[MAXWORDLEN-1] = 0; /* make sure word is null-terminated */
1354 * End of the word: check for errors.
1360 option_error("Error reading %s: %m", filename);
1364 * If len is zero, then we didn't find a word before the
1370 option_error("warning: quoted word runs to end of file (%.20s...)",
1375 * Warn if the word was too long, and append a terminating null.
1377 if (len >= MAXWORDLEN) {
1378 option_error("warning: word in file %s too long (%.20s...)",
1380 len = MAXWORDLEN - 1;
1391 * number_option - parse an unsigned numeric parameter for an option.
1394 number_option(char *str, u_int32_t *valp, int base)
1398 *valp = strtoul(str, &ptr, base);
1400 option_error("invalid numeric parameter '%s' for %s option",
1401 str, current_option);
1409 * int_option - like number_option, but valp is int *,
1410 * the base is assumed to be 0, and *valp is not changed
1411 * if there is an error.
1414 int_option(char *str, int *valp)
1418 if (!number_option(str, &v, 0))
1426 * The following procedures parse options.
1430 * readfile - take commands from a file.
1433 readfile(char **argv)
1435 return options_from_file(*argv, 1, 1, privileged_option);
1439 * callfile - take commands from /etc/ppp/peers/<name>.
1440 * Name may not contain /../, start with / or ../, or end in /..
1443 callfile(char **argv)
1445 char *fname, *arg, *p;
1450 if (arg[0] == '/' || arg[0] == 0)
1453 for (p = arg; *p != 0; ) {
1454 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1458 while (*p != '/' && *p != 0)
1465 option_error("call option value may not contain .. or start with /");
1469 l = strlen(arg) + strlen(PPP_PATH_PEERFILES) + 1;
1470 if ((fname = (char *) malloc(l)) == NULL)
1471 novm("call file name");
1472 slprintf(fname, l, "%s%s", PPP_PATH_PEERFILES, arg);
1473 script_setenv("CALL_FILE", arg, 0);
1475 ok = options_from_file(fname, 1, 1, 1);
1481 #ifdef PPP_WITH_FILTER
1483 * setpassfilter - Set the pass filter for packets
1486 setpassfilter(char **argv)
1491 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1492 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1493 option_error("error in pass-filter expression: %s\n",
1503 * setactivefilter - Set the active filter for packets
1506 setactivefilter(char **argv)
1511 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1512 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1513 option_error("error in active-filter expression: %s\n",
1524 * setdomain - Set domain name to append to hostname
1527 setdomain(char **argv)
1529 gethostname(hostname, MAXNAMELEN);
1532 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1533 domain = hostname + strlen(hostname);
1534 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1536 hostname[MAXNAMELEN-1] = 0;
1541 setlogfile(char **argv)
1547 if (!privileged_option && seteuid(getuid()) == -1) {
1548 option_error("unable to drop permissions to open %s: %m", *argv);
1551 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1552 if (fd < 0 && errno == EEXIST)
1553 fd = open(*argv, O_WRONLY | O_APPEND);
1555 if (!privileged_option && seteuid(euid) == -1)
1556 fatal("unable to regain privileges: %m");
1559 option_error("Can't open log file %s: %m", *argv);
1562 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1563 if (logfile_fd >= 0)
1572 setmodir(char **argv)
1576 if(!strcmp(*argv,"in")) {
1577 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1578 } else if (!strcmp(*argv,"out")) {
1579 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1580 } else if (!strcmp(*argv,"max")) {
1581 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1583 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1588 #ifdef PPP_WITH_PLUGINS
1590 loadplugin(char **argv)
1599 if (strchr(arg, '/') == 0) {
1600 const char *base = PPP_PATH_PLUGIN;
1601 int l = strlen(base) + strlen(arg) + 2;
1604 novm("plugin file path");
1605 strlcpy(path, base, l);
1606 strlcat(path, "/", l);
1607 strlcat(path, arg, l);
1609 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1613 option_error("%s", err);
1614 option_error("Couldn't load plugin %s", arg);
1617 init = (void (*)(void))dlsym(handle, "plugin_init");
1619 option_error("%s has no initialization entry point", arg);
1622 vers = (const char *) dlsym(handle, "pppd_version");
1624 warn("Warning: plugin %s has no version information", arg);
1625 } else if (strcmp(vers, VERSION) != 0) {
1626 option_error("Plugin %s is for pppd version %s, this is %s",
1627 arg, vers, VERSION);
1630 info("Plugin %s loaded.", arg);
1641 #endif /* PPP_WITH_PLUGINS */
1644 * Set an environment variable specified by the user.
1647 user_setenv(char **argv)
1649 char *arg = argv[0];
1651 struct userenv *uep, **insp;
1653 if ((eqp = strchr(arg, '=')) == NULL) {
1654 option_error("missing = in name=value: %s", arg);
1658 option_error("missing variable name: %s", arg);
1661 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1662 int nlen = strlen(uep->ue_name);
1663 if (nlen == (eqp - arg) &&
1664 strncmp(arg, uep->ue_name, nlen) == 0)
1667 /* Ignore attempts by unprivileged users to override privileged sources */
1668 if (uep != NULL && !privileged_option && uep->ue_priv)
1670 /* The name never changes, so allocate it with the structure */
1672 uep = malloc(sizeof (*uep) + (eqp-arg));
1673 strncpy(uep->ue_name, arg, eqp-arg);
1674 uep->ue_name[eqp-arg] = '\0';
1675 uep->ue_next = NULL;
1676 insp = &userenv_list;
1677 while (*insp != NULL)
1678 insp = &(*insp)->ue_next;
1681 struct userenv *uep2;
1682 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1683 if (uep2 != uep && !uep2->ue_isset)
1686 if (uep2 == NULL && !uep->ue_isset)
1687 find_option("unset")->flags |= OPT_NOPRINT;
1688 free(uep->ue_value);
1691 uep->ue_priv = privileged_option;
1692 uep->ue_source = option_source;
1693 uep->ue_value = strdup(eqp + 1);
1694 curopt->flags &= ~OPT_NOPRINT;
1699 user_setprint(option_t *opt, printer_func printer, void *arg)
1701 struct userenv *uep, *uepnext;
1703 uepnext = userenv_list;
1704 while (uepnext != NULL && !uepnext->ue_isset)
1705 uepnext = uepnext->ue_next;
1706 while ((uep = uepnext) != NULL) {
1707 uepnext = uep->ue_next;
1708 while (uepnext != NULL && !uepnext->ue_isset)
1709 uepnext = uepnext->ue_next;
1710 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1711 if (uepnext != NULL)
1712 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1714 opt->source = uep->ue_source;
1719 user_unsetenv(char **argv)
1721 struct userenv *uep, **insp;
1722 char *arg = argv[0];
1724 if (strchr(arg, '=') != NULL) {
1725 option_error("unexpected = in name: %s", arg);
1729 option_error("missing variable name for unset");
1732 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1733 if (strcmp(arg, uep->ue_name) == 0)
1736 /* Ignore attempts by unprivileged users to override privileged sources */
1737 if (uep != NULL && !privileged_option && uep->ue_priv)
1739 /* The name never changes, so allocate it with the structure */
1741 uep = malloc(sizeof (*uep) + strlen(arg));
1742 strcpy(uep->ue_name, arg);
1743 uep->ue_next = NULL;
1744 insp = &userenv_list;
1745 while (*insp != NULL)
1746 insp = &(*insp)->ue_next;
1749 struct userenv *uep2;
1750 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1751 if (uep2 != uep && uep2->ue_isset)
1754 if (uep2 == NULL && uep->ue_isset)
1755 find_option("set")->flags |= OPT_NOPRINT;
1756 free(uep->ue_value);
1759 uep->ue_priv = privileged_option;
1760 uep->ue_source = option_source;
1761 uep->ue_value = NULL;
1762 curopt->flags &= ~OPT_NOPRINT;
1767 user_unsetprint(option_t *opt, printer_func printer, void *arg)
1769 struct userenv *uep, *uepnext;
1771 uepnext = userenv_list;
1772 while (uepnext != NULL && uepnext->ue_isset)
1773 uepnext = uepnext->ue_next;
1774 while ((uep = uepnext) != NULL) {
1775 uepnext = uep->ue_next;
1776 while (uepnext != NULL && uepnext->ue_isset)
1777 uepnext = uepnext->ue_next;
1778 (*printer)(arg, "%s", uep->ue_name);
1779 if (uepnext != NULL)
1780 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1782 opt->source = uep->ue_source;