2 * options.c - handles option processing for PPP.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 #define RCSID "$Id: options.c,v 1.102 2008/06/15 06:53:06 paulus Exp $"
61 * There have been 3 or 4 different names for this in libpcap CVS, but
62 * this seems to be what they have settled on...
63 * For older versions of libpcap, use DLT_PPP - but that means
64 * we lose the inbound and outbound qualifiers.
67 #ifdef DLT_PPP_WITHDIRECTION
68 #define DLT_PPP_PPPD DLT_PPP_WITHDIRECTION
70 #define DLT_PPP_PPPD DLT_PPP
73 #endif /* PPP_FILTER */
76 #include "pathnames.h"
78 #if defined(ultrix) || defined(NeXT)
79 char *strdup __P((char *));
82 static const char rcsid[] = RCSID;
85 struct option_value *next;
91 * Option variables and default values.
93 int debug = 0; /* Debug flag */
94 int kdebugflag = 0; /* Tell kernel to print debug messages */
95 int default_device = 1; /* Using /dev/tty or equivalent */
96 char devnam[MAXPATHLEN]; /* Device name */
97 bool nodetach = 0; /* Don't detach from controlling tty */
98 bool updetach = 0; /* Detach once link is up */
99 bool master_detach; /* Detach when we're (only) multilink master */
101 bool up_sdnotify = 0; /* Notify systemd once link is up */
103 int maxconnect = 0; /* Maximum connect time */
104 char user[MAXNAMELEN]; /* Username for PAP */
105 char passwd[MAXSECRETLEN]; /* Password for PAP */
106 bool persist = 0; /* Reopen link after it goes down */
107 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
108 bool demand = 0; /* do dial-on-demand */
109 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
110 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
111 int holdoff = 30; /* # seconds to pause before reconnecting */
112 bool holdoff_specified; /* true if a holdoff value has been given */
113 int log_to_fd = 1; /* send log messages to this fd too */
114 bool log_default = 1; /* log_to_fd is default (stdout) */
115 int maxfail = 10; /* max # of unsuccessful connection attempts */
116 char linkname[MAXPATHLEN]; /* logical name for link */
117 bool tune_kernel; /* may alter kernel settings */
118 int connect_delay = 1000; /* wait this many ms after connect script */
119 int req_unit = -1; /* requested interface unit */
120 char req_ifname[MAXIFNAMELEN]; /* requested interface name */
121 bool multilink = 0; /* Enable multilink operation */
122 char *bundle_name = NULL; /* bundle name for multilink */
123 bool dump_options; /* print out option values */
124 bool dryrun; /* print out option values and exit */
125 char *domain; /* domain name set by domain option */
126 int child_wait = 5; /* # seconds to wait for children at exit */
127 struct userenv *userenv_list; /* user environment variables */
128 int dfl_route_metric = -1; /* metric of the default route to set over the PPP link */
131 unsigned int maxoctets = 0; /* default - no limit */
132 int maxoctets_dir = 0; /* default - sum of traffic */
133 int maxoctets_timeout = 1; /* default 1 second */
137 extern option_t auth_options[];
138 extern struct stat devstat;
141 struct bpf_program pass_filter;/* Filter program for packets to pass */
142 struct bpf_program active_filter; /* Filter program for link-active pkts */
145 static option_t *curopt; /* pointer to option being processed */
146 char *current_option; /* the name of the option being parsed */
147 int privileged_option; /* set iff the current option came from root */
148 char *option_source; /* string saying where the option came from */
149 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
150 bool devnam_fixed; /* can no longer change device name */
152 static int logfile_fd = -1; /* fd opened for log file */
153 static char logfile_name[MAXPATHLEN]; /* name of log file */
158 static int setdomain __P((char **));
159 static int readfile __P((char **));
160 static int callfile __P((char **));
161 static int showversion __P((char **));
162 static int showhelp __P((char **));
163 static void usage __P((void));
164 static int setlogfile __P((char **));
166 static int loadplugin __P((char **));
170 static int setpassfilter __P((char **));
171 static int setactivefilter __P((char **));
175 static int setmodir __P((char **));
178 static int user_setenv __P((char **));
179 static void user_setprint __P((option_t *, printer_func, void *));
180 static int user_unsetenv __P((char **));
181 static void user_unsetprint __P((option_t *, printer_func, void *));
183 static option_t *find_option __P((const char *name));
184 static int process_option __P((option_t *, char *, char **));
185 static int n_arguments __P((option_t *));
186 static int number_option __P((char *, u_int32_t *, int));
189 * Structure to store extra lists of options.
193 struct option_list *next;
196 static struct option_list *extra_options = NULL;
201 option_t general_options[] = {
202 { "debug", o_int, &debug,
203 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
204 { "-d", o_int, &debug,
205 "Increase debugging level",
206 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
208 { "kdebug", o_int, &kdebugflag,
209 "Set kernel driver debug level", OPT_PRIO },
211 { "nodetach", o_bool, &nodetach,
212 "Don't detach from controlling tty", OPT_PRIO | 1 },
213 { "-detach", o_bool, &nodetach,
214 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
216 { "up_sdnotify", o_bool, &up_sdnotify,
217 "Notify systemd once link is up (implies nodetach)",
218 OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
220 { "updetach", o_bool, &updetach,
221 "Detach from controlling tty once link is up",
222 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
224 { "master_detach", o_bool, &master_detach,
225 "Detach when we're multilink master but have no link", 1 },
227 { "holdoff", o_int, &holdoff,
228 "Set time in seconds before retrying connection",
229 OPT_PRIO, &holdoff_specified },
231 { "idle", o_int, &idle_time_limit,
232 "Set time in seconds before disconnecting idle link", OPT_PRIO },
234 { "maxconnect", o_int, &maxconnect,
235 "Set connection time limit",
236 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
238 { "domain", o_special, (void *)setdomain,
239 "Add given domain name to hostname",
240 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
242 { "file", o_special, (void *)readfile,
243 "Take options from a file", OPT_NOPRINT },
244 { "call", o_special, (void *)callfile,
245 "Take options from a privileged file", OPT_NOPRINT },
247 { "persist", o_bool, &persist,
248 "Keep on reopening connection after close", OPT_PRIO | 1 },
249 { "nopersist", o_bool, &persist,
250 "Turn off persist option", OPT_PRIOSUB },
252 { "demand", o_bool, &demand,
253 "Dial on demand", OPT_INITONLY | 1, &persist },
255 { "--version", o_special_noarg, (void *)showversion,
256 "Show version number" },
257 { "--help", o_special_noarg, (void *)showhelp,
258 "Show brief listing of options" },
259 { "-h", o_special_noarg, (void *)showhelp,
260 "Show brief listing of options", OPT_ALIAS },
262 { "logfile", o_special, (void *)setlogfile,
263 "Append log messages to this file",
264 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
265 { "logfd", o_int, &log_to_fd,
266 "Send log messages to this file descriptor",
267 OPT_PRIOSUB | OPT_A2CLR, &log_default },
268 { "nolog", o_int, &log_to_fd,
269 "Don't send log messages to any file",
270 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
271 { "nologfd", o_int, &log_to_fd,
272 "Don't send log messages to any file descriptor",
273 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
275 { "linkname", o_string, linkname,
276 "Set logical name for link",
277 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
279 { "maxfail", o_int, &maxfail,
280 "Maximum number of unsuccessful connection attempts to allow",
283 { "ktune", o_bool, &tune_kernel,
284 "Alter kernel settings as necessary", OPT_PRIO | 1 },
285 { "noktune", o_bool, &tune_kernel,
286 "Don't alter kernel settings", OPT_PRIOSUB },
288 { "connect-delay", o_int, &connect_delay,
289 "Maximum time (in ms) to wait after connect script finishes",
292 { "unit", o_int, &req_unit,
293 "PPP interface unit number to use if possible",
294 OPT_PRIO | OPT_LLIMIT, 0, 0 },
296 { "ifname", o_string, req_ifname,
297 "Set PPP interface name",
298 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXIFNAMELEN },
300 { "dump", o_bool, &dump_options,
301 "Print out option values after parsing all options", 1 },
302 { "dryrun", o_bool, &dryrun,
303 "Stop after parsing, printing, and checking options", 1 },
305 { "child-timeout", o_int, &child_wait,
306 "Number of seconds to wait for child processes at exit",
309 { "set", o_special, (void *)user_setenv,
310 "Set user environment variable",
311 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
312 { "unset", o_special, (void *)user_unsetenv,
313 "Unset user environment variable",
314 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
316 { "defaultroute-metric", o_int, &dfl_route_metric,
317 "Metric to use for the default route (Linux only; -1 for default behavior)",
318 OPT_PRIV|OPT_LLIMIT|OPT_INITONLY, NULL, 0, -1 },
320 #ifdef HAVE_MULTILINK
321 { "multilink", o_bool, &multilink,
322 "Enable multilink operation", OPT_PRIO | 1 },
323 { "mp", o_bool, &multilink,
324 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
325 { "nomultilink", o_bool, &multilink,
326 "Disable multilink operation", OPT_PRIOSUB | 0 },
327 { "nomp", o_bool, &multilink,
328 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
330 { "bundle", o_string, &bundle_name,
331 "Bundle name for multilink", OPT_PRIO },
332 #endif /* HAVE_MULTILINK */
335 { "plugin", o_special, (void *)loadplugin,
336 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
340 { "pass-filter", o_special, setpassfilter,
341 "set filter for packets to pass", OPT_PRIO },
343 { "active-filter", o_special, setactivefilter,
344 "set filter for active pkts", OPT_PRIO },
348 { "maxoctets", o_int, &maxoctets,
349 "Set connection traffic limit",
350 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
351 { "mo", o_int, &maxoctets,
352 "Set connection traffic limit",
353 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
354 { "mo-direction", o_special, setmodir,
355 "Set direction for limit traffic (sum,in,out,max)" },
356 { "mo-timeout", o_int, &maxoctets_timeout,
357 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
363 #ifndef IMPLEMENTATION
364 #define IMPLEMENTATION ""
367 static char *usage_string = "\
369 Usage: %s [ options ], where options are:\n\
370 <device> Communicate over the named device\n\
371 <speed> Set the baud rate to <speed>\n\
372 <loc>:<rem> Set the local and/or remote interface IP\n\
373 addresses. Either one may be omitted.\n\
374 asyncmap <n> Set the desired async map to hex <n>\n\
375 auth Require authentication from peer\n\
376 connect <p> Invoke shell command <p> to set up the serial line\n\
377 crtscts Use hardware RTS/CTS flow control\n\
378 defaultroute Add default route through interface\n\
379 file <f> Take options from file <f>\n\
380 modem Use modem control lines\n\
381 mru <n> Set MRU value to <n> for negotiation\n\
382 See pppd(8) for more options.\n\
386 * parse_args - parse a string of arguments from the command line.
389 parse_args(argc, argv)
397 privileged_option = privileged;
398 option_source = "command line";
399 option_priority = OPRIO_CMDLINE;
403 opt = find_option(arg);
405 option_error("unrecognized option '%s'", arg);
409 n = n_arguments(opt);
411 option_error("too few parameters for option %s", arg);
414 if (!process_option(opt, arg, argv))
423 * options_from_file - Read a string of options from a file,
424 * and interpret them.
427 options_from_file(filename, must_exist, check_prot, priv)
434 int i, newline, ret, err;
440 char args[MAXARGS][MAXWORDLEN];
441 char cmd[MAXWORDLEN];
444 if (check_prot && seteuid(getuid()) == -1) {
445 option_error("unable to drop privileges to open %s: %m", filename);
448 f = fopen(filename, "r");
450 if (check_prot && seteuid(euid) == -1)
451 fatal("unable to regain privileges");
455 if (err != ENOENT && err != ENOTDIR)
456 warn("Warning: can't open options file %s: %m", filename);
459 option_error("Can't open options file %s: %m", filename);
463 oldpriv = privileged_option;
464 privileged_option = priv;
465 oldsource = option_source;
466 option_source = strdup(filename);
467 if (option_source == NULL)
468 option_source = "file";
470 while (getword(f, cmd, &newline, filename)) {
471 opt = find_option(cmd);
473 option_error("In file %s: unrecognized option '%s'",
477 n = n_arguments(opt);
478 for (i = 0; i < n; ++i) {
479 if (!getword(f, args[i], &newline, filename)) {
481 "In file %s: too few parameters for option '%s'",
487 if (!process_option(opt, cmd, argv))
494 privileged_option = oldpriv;
495 option_source = oldsource;
500 * options_from_user - See if the use has a ~/.ppprc file,
501 * and if so, interpret options from it.
506 char *user, *path, *file;
511 pw = getpwuid(getuid());
512 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
514 file = _PATH_USEROPT;
515 pl = strlen(user) + strlen(file) + 2;
518 novm("init file name");
519 slprintf(path, pl, "%s/%s", user, file);
520 option_priority = OPRIO_CFGFILE;
521 ret = options_from_file(path, 0, 1, privileged);
527 * options_for_tty - See if an options file exists for the serial
528 * device, and if so, interpret options from it.
529 * We only allow the per-tty options file to override anything from
530 * the command line if it is something that the user can't override
531 * once it has been set by root; this is done by giving configuration
532 * files a lower priority than the command line.
537 char *dev, *path, *p;
542 if ((p = strstr(dev, "/dev/")) != NULL)
544 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
545 return 1; /* don't look for /etc/ppp/options.tty */
546 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
549 novm("tty init file name");
550 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
551 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
552 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
555 option_priority = OPRIO_CFGFILE;
556 ret = options_from_file(path, 0, 0, 1);
562 * options_from_list - process a string of options in a wordlist.
565 options_from_list(w, priv)
574 privileged_option = priv;
575 option_source = "secrets file";
576 option_priority = OPRIO_SECFILE;
579 opt = find_option(w->word);
581 option_error("In secrets file: unrecognized option '%s'",
585 n = n_arguments(opt);
587 for (i = 0; i < n; ++i) {
591 "In secrets file: too few parameters for option '%s'",
597 if (!process_option(opt, w0->word, argv))
608 * match_option - see if this option matches an option_t structure.
611 match_option(name, opt, dowild)
616 int (*match) __P((char *, char **, int));
618 if (dowild != (opt->type == o_wild))
621 return strcmp(name, opt->name) == 0;
622 match = (int (*) __P((char *, char **, int))) opt->addr;
623 return (*match)(name, NULL, 0);
627 * find_option - scan the option lists for the various protocols
628 * looking for an entry with the given name.
629 * This could be optimized by using a hash table.
636 struct option_list *list;
639 for (dowild = 0; dowild <= 1; ++dowild) {
640 for (opt = general_options; opt->name != NULL; ++opt)
641 if (match_option(name, opt, dowild))
643 for (opt = auth_options; opt->name != NULL; ++opt)
644 if (match_option(name, opt, dowild))
646 for (list = extra_options; list != NULL; list = list->next)
647 for (opt = list->options; opt->name != NULL; ++opt)
648 if (match_option(name, opt, dowild))
650 for (opt = the_channel->options; opt->name != NULL; ++opt)
651 if (match_option(name, opt, dowild))
653 for (i = 0; protocols[i] != NULL; ++i)
654 if ((opt = protocols[i]->options) != NULL)
655 for (; opt->name != NULL; ++opt)
656 if (match_option(name, opt, dowild))
663 * process_option - process one new-style option.
666 process_option(opt, cmd, argv)
674 int (*parser) __P((char **));
675 int (*wildp) __P((char *, char **, int));
676 char *optopt = (opt->type == o_wild)? "": " option";
677 int prio = option_priority;
678 option_t *mainopt = opt;
680 current_option = opt->name;
681 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
683 while (mainopt->flags & OPT_PRIOSUB)
685 if (mainopt->flags & OPT_PRIO) {
686 if (prio < mainopt->priority) {
687 /* new value doesn't override old */
688 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
689 option_error("%s%s set in %s cannot be overridden\n",
690 opt->name, optopt, mainopt->source);
695 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
696 warn("%s%s from %s overrides command line",
697 opt->name, optopt, option_source);
700 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
701 option_error("%s%s cannot be changed after initialization",
705 if ((opt->flags & OPT_PRIV) && !privileged_option) {
706 option_error("using the %s%s requires root privilege",
710 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
711 option_error("%s%s is disabled", opt->name, optopt);
714 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
715 option_error("the %s%s may not be changed in %s",
716 opt->name, optopt, option_source);
722 v = opt->flags & OPT_VALUE;
723 *(bool *)(opt->addr) = v;
724 if (opt->addr2 && (opt->flags & OPT_A2COPY))
725 *(bool *)(opt->addr2) = v;
726 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
727 *(bool *)(opt->addr2) = 0;
728 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
729 *(u_char *)(opt->addr2) &= ~v;
730 else if (opt->addr2 && (opt->flags & OPT_A2OR))
731 *(u_char *)(opt->addr2) |= v;
736 if ((opt->flags & OPT_NOARG) == 0) {
737 if (!int_option(*argv, &iv))
739 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
740 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
741 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
742 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
743 switch (opt->flags & OPT_LIMITS) {
745 option_error("%s value must be%s >= %d",
746 opt->name, zok, opt->lower_limit);
749 option_error("%s value must be%s <= %d",
750 opt->name, zok, opt->upper_limit);
753 option_error("%s value must be%s between %d and %d",
754 opt->name, zok, opt->lower_limit, opt->upper_limit);
760 a = opt->flags & OPT_VALUE;
762 a -= 256; /* sign extend */
764 if (opt->flags & OPT_INC)
765 iv += *(int *)(opt->addr);
766 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
767 int oldv = *(int *)(opt->addr);
768 if ((opt->flags & OPT_ZEROINF) ?
769 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
770 option_error("%s value cannot be increased", opt->name);
774 *(int *)(opt->addr) = iv;
775 if (opt->addr2 && (opt->flags & OPT_A2COPY))
776 *(int *)(opt->addr2) = iv;
780 if (opt->flags & OPT_NOARG) {
781 v = opt->flags & OPT_VALUE;
784 } else if (!number_option(*argv, &v, 16))
786 if (opt->flags & OPT_OR)
787 v |= *(u_int32_t *)(opt->addr);
788 *(u_int32_t *)(opt->addr) = v;
789 if (opt->addr2 && (opt->flags & OPT_A2COPY))
790 *(u_int32_t *)(opt->addr2) = v;
794 if (opt->flags & OPT_STATIC) {
795 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
797 char **optptr = (char **)(opt->addr);
800 novm("option argument");
807 case o_special_noarg:
809 parser = (int (*) __P((char **))) opt->addr;
811 if (!(*parser)(argv))
813 if (opt->flags & OPT_A2LIST) {
814 struct option_value *ovp, *pp;
816 ovp = malloc(sizeof(*ovp) + strlen(*argv));
818 strcpy(ovp->value, *argv);
819 ovp->source = option_source;
821 if (opt->addr2 == NULL) {
824 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
833 wildp = (int (*) __P((char *, char **, int))) opt->addr;
834 if (!(*wildp)(cmd, argv, 1))
840 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
841 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
843 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
844 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
845 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
847 mainopt->source = option_source;
848 mainopt->priority = prio;
849 mainopt->winner = opt - mainopt;
855 * override_value - if the option priorities would permit us to
856 * override the value of option, return 1 and update the priority
857 * and source of the option value. Otherwise returns 0.
860 override_value(option, priority, source)
867 opt = find_option(option);
870 while (opt->flags & OPT_PRIOSUB)
872 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
874 opt->priority = priority;
875 opt->source = source;
881 * n_arguments - tell how many arguments an option takes
887 return (opt->type == o_bool || opt->type == o_special_noarg
888 || (opt->flags & OPT_NOARG))? 0: 1;
892 * add_options - add a list of options to the set we grok.
898 struct option_list *list;
900 list = malloc(sizeof(*list));
902 novm("option list entry");
904 list->next = extra_options;
905 extra_options = list;
909 * check_options - check that options are valid and consistent.
914 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
919 * print_option - print out an option and its value
922 print_option(opt, mainopt, printer, arg)
923 option_t *opt, *mainopt;
924 printer_func printer;
930 if (opt->flags & OPT_NOPRINT)
934 v = opt->flags & OPT_VALUE;
935 if (*(bool *)opt->addr != v)
936 /* this can happen legitimately, e.g. lock
937 option turned off for default device */
939 printer(arg, "%s", opt->name);
942 v = opt->flags & OPT_VALUE;
945 i = *(int *)opt->addr;
946 if (opt->flags & OPT_NOARG) {
947 printer(arg, "%s", opt->name);
949 if (opt->flags & OPT_INC) {
950 for (; i > v; i -= v)
951 printer(arg, " %s", opt->name);
953 printer(arg, " # oops: %d not %d\n",
957 printer(arg, "%s %d", opt->name, i);
961 printer(arg, "%s", opt->name);
962 if ((opt->flags & OPT_NOARG) == 0)
963 printer(arg, " %x", *(u_int32_t *)opt->addr);
967 if (opt->flags & OPT_HIDE) {
970 p = (char *) opt->addr;
971 if ((opt->flags & OPT_STATIC) == 0)
974 printer(arg, "%s %q", opt->name, p);
978 case o_special_noarg:
980 if (opt->type != o_wild) {
981 printer(arg, "%s", opt->name);
982 if (n_arguments(opt) == 0)
986 if (opt->flags & OPT_A2PRINTER) {
987 void (*oprt) __P((option_t *, printer_func, void *));
988 oprt = (void (*) __P((option_t *, printer_func,
990 (*oprt)(opt, printer, arg);
991 } else if (opt->flags & OPT_A2STRVAL) {
992 p = (char *) opt->addr2;
993 if ((opt->flags & OPT_STATIC) == 0)
995 printer(arg, "%q", p);
996 } else if (opt->flags & OPT_A2LIST) {
997 struct option_value *ovp;
999 ovp = (struct option_value *) opt->addr2;
1001 printer(arg, "%q", ovp->value);
1002 if ((ovp = ovp->next) == NULL)
1004 printer(arg, "\t\t# (from %s)\n%s ",
1005 ovp->source, opt->name);
1008 printer(arg, "xxx # [don't know how to print value]");
1013 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
1016 printer(arg, "\t\t# (from %s)\n", mainopt->source);
1020 * print_option_list - print out options in effect from an
1024 print_option_list(opt, printer, arg)
1026 printer_func printer;
1029 while (opt->name != NULL) {
1030 if (opt->priority != OPRIO_DEFAULT
1031 && opt->winner != (short int) -1)
1032 print_option(opt + opt->winner, opt, printer, arg);
1035 } while (opt->flags & OPT_PRIOSUB);
1040 * print_options - print out what options are in effect.
1043 print_options(printer, arg)
1044 printer_func printer;
1047 struct option_list *list;
1050 printer(arg, "pppd options in effect:\n");
1051 print_option_list(general_options, printer, arg);
1052 print_option_list(auth_options, printer, arg);
1053 for (list = extra_options; list != NULL; list = list->next)
1054 print_option_list(list->options, printer, arg);
1055 print_option_list(the_channel->options, printer, arg);
1056 for (i = 0; protocols[i] != NULL; ++i)
1057 print_option_list(protocols[i]->options, printer, arg);
1061 * usage - print out a message telling how to use the program.
1066 if (phase == PHASE_INITIALIZE)
1067 fprintf(stderr, usage_string, VERSION, progname);
1071 * showhelp - print out usage message and exit.
1077 if (phase == PHASE_INITIALIZE) {
1085 * showversion - print out the version number and exit.
1091 if (phase == PHASE_INITIALIZE) {
1092 fprintf(stderr, "pppd version %s\n", VERSION);
1099 * option_error - print a message about an error in an option.
1100 * The message is logged, and also sent to
1101 * stderr if phase == PHASE_INITIALIZE.
1104 option_error __V((char *fmt, ...))
1109 #if defined(__STDC__)
1110 va_start(args, fmt);
1114 fmt = va_arg(args, char *);
1116 vslprintf(buf, sizeof(buf), fmt, args);
1118 if (phase == PHASE_INITIALIZE)
1119 fprintf(stderr, "%s: %s\n", progname, buf);
1120 syslog(LOG_ERR, "%s", buf);
1125 * 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(f, word, newlinep, filename)
1165 int quoted, comment;
1166 int value, digit, got, n;
1168 #define isoctal(c) ((c) >= '0' && (c) < '8')
1177 * First skip white-space and comments.
1185 * A newline means the end of a comment; backslash-newline
1186 * is ignored. Note that we cannot have escape && comment.
1198 * Ignore characters other than newline in a comment.
1204 * If this character is escaped, we have a word start.
1210 * If this is the escape character, look at the next character.
1218 * If this is the start of a comment, ignore the rest of the line.
1226 * A non-whitespace character is the start of a word.
1233 * Process characters until the end of the word.
1238 * This character is escaped: backslash-newline is ignored,
1239 * various other characters indicate particular values
1240 * as for C backslash-escapes.
1275 * \ddd octal sequence
1278 for (n = 0; n < 3 && isoctal(c); ++n) {
1279 value = (value << 3) + (c & 07);
1288 * \x<hex_string> sequence
1292 for (n = 0; n < 2 && isxdigit(c); ++n) {
1293 digit = toupper(c) - '0';
1295 digit += '0' + 10 - 'A';
1296 value = (value << 4) + digit;
1304 * Otherwise the character stands for itself.
1311 * Store the resulting character for the escape sequence.
1313 if (len < MAXWORDLEN) {
1324 * Backslash starts a new escape sequence.
1333 * Not escaped: check for the start or end of a quoted
1334 * section and see if we've reached the end of the word.
1342 } else if (c == '"' || c == '\'') {
1346 } else if (isspace(c) || c == '#') {
1352 * An ordinary character: store it in the word and get another.
1354 if (len < MAXWORDLEN) {
1363 * End of the word: check for errors.
1369 option_error("Error reading %s: %m", filename);
1373 * If len is zero, then we didn't find a word before the
1379 option_error("warning: quoted word runs to end of file (%.20s...)",
1384 * Warn if the word was too long, and append a terminating null.
1386 if (len >= MAXWORDLEN) {
1387 option_error("warning: word in file %s too long (%.20s...)",
1389 len = MAXWORDLEN - 1;
1400 * number_option - parse an unsigned numeric parameter for an option.
1403 number_option(str, valp, base)
1410 *valp = strtoul(str, &ptr, base);
1412 option_error("invalid numeric parameter '%s' for %s option",
1413 str, current_option);
1421 * int_option - like number_option, but valp is int *,
1422 * the base is assumed to be 0, and *valp is not changed
1423 * if there is an error.
1426 int_option(str, valp)
1432 if (!number_option(str, &v, 0))
1440 * The following procedures parse options.
1444 * readfile - take commands from a file.
1450 return options_from_file(*argv, 1, 1, privileged_option);
1454 * callfile - take commands from /etc/ppp/peers/<name>.
1455 * Name may not contain /../, start with / or ../, or end in /..
1461 char *fname, *arg, *p;
1466 if (arg[0] == '/' || arg[0] == 0)
1469 for (p = arg; *p != 0; ) {
1470 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1474 while (*p != '/' && *p != 0)
1481 option_error("call option value may not contain .. or start with /");
1485 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1486 if ((fname = (char *) malloc(l)) == NULL)
1487 novm("call file name");
1488 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1490 ok = options_from_file(fname, 1, 1, 1);
1498 * setpassfilter - Set the pass filter for packets
1507 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1508 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1509 option_error("error in pass-filter expression: %s\n",
1519 * setactivefilter - Set the active filter for packets
1522 setactivefilter(argv)
1528 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1529 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1530 option_error("error in active-filter expression: %s\n",
1541 * setdomain - Set domain name to append to hostname
1547 gethostname(hostname, MAXNAMELEN);
1550 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1551 domain = hostname + strlen(hostname);
1552 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1554 hostname[MAXNAMELEN-1] = 0;
1566 if (!privileged_option && seteuid(getuid()) == -1) {
1567 option_error("unable to drop permissions to open %s: %m", *argv);
1570 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1571 if (fd < 0 && errno == EEXIST)
1572 fd = open(*argv, O_WRONLY | O_APPEND);
1574 if (!privileged_option && seteuid(euid) == -1)
1575 fatal("unable to regain privileges: %m");
1578 option_error("Can't open log file %s: %m", *argv);
1581 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1582 if (logfile_fd >= 0)
1597 if(!strcmp(*argv,"in")) {
1598 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1599 } else if (!strcmp(*argv,"out")) {
1600 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1601 } else if (!strcmp(*argv,"max")) {
1602 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1604 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1618 void (*init) __P((void));
1622 if (strchr(arg, '/') == 0) {
1623 const char *base = _PATH_PLUGIN;
1624 int l = strlen(base) + strlen(arg) + 2;
1627 novm("plugin file path");
1628 strlcpy(path, base, l);
1629 strlcat(path, "/", l);
1630 strlcat(path, arg, l);
1632 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1636 option_error("%s", err);
1637 option_error("Couldn't load plugin %s", arg);
1640 init = (void (*)(void))dlsym(handle, "plugin_init");
1642 option_error("%s has no initialization entry point", arg);
1645 vers = (const char *) dlsym(handle, "pppd_version");
1647 warn("Warning: plugin %s has no version information", arg);
1648 } else if (strcmp(vers, VERSION) != 0) {
1649 option_error("Plugin %s is for pppd version %s, this is %s",
1650 arg, vers, VERSION);
1653 info("Plugin %s loaded.", arg);
1667 * Set an environment variable specified by the user.
1673 char *arg = argv[0];
1675 struct userenv *uep, **insp;
1677 if ((eqp = strchr(arg, '=')) == NULL) {
1678 option_error("missing = in name=value: %s", arg);
1682 option_error("missing variable name: %s", arg);
1685 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1686 int nlen = strlen(uep->ue_name);
1687 if (nlen == (eqp - arg) &&
1688 strncmp(arg, uep->ue_name, nlen) == 0)
1691 /* Ignore attempts by unprivileged users to override privileged sources */
1692 if (uep != NULL && !privileged_option && uep->ue_priv)
1694 /* The name never changes, so allocate it with the structure */
1696 uep = malloc(sizeof (*uep) + (eqp-arg));
1697 strncpy(uep->ue_name, arg, eqp-arg);
1698 uep->ue_name[eqp-arg] = '\0';
1699 uep->ue_next = NULL;
1700 insp = &userenv_list;
1701 while (*insp != NULL)
1702 insp = &(*insp)->ue_next;
1705 struct userenv *uep2;
1706 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1707 if (uep2 != uep && !uep2->ue_isset)
1710 if (uep2 == NULL && !uep->ue_isset)
1711 find_option("unset")->flags |= OPT_NOPRINT;
1712 free(uep->ue_value);
1715 uep->ue_priv = privileged_option;
1716 uep->ue_source = option_source;
1717 uep->ue_value = strdup(eqp + 1);
1718 curopt->flags &= ~OPT_NOPRINT;
1723 user_setprint(opt, printer, arg)
1725 printer_func printer;
1728 struct userenv *uep, *uepnext;
1730 uepnext = userenv_list;
1731 while (uepnext != NULL && !uepnext->ue_isset)
1732 uepnext = uepnext->ue_next;
1733 while ((uep = uepnext) != NULL) {
1734 uepnext = uep->ue_next;
1735 while (uepnext != NULL && !uepnext->ue_isset)
1736 uepnext = uepnext->ue_next;
1737 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1738 if (uepnext != NULL)
1739 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1741 opt->source = uep->ue_source;
1749 struct userenv *uep, **insp;
1750 char *arg = argv[0];
1752 if (strchr(arg, '=') != NULL) {
1753 option_error("unexpected = in name: %s", arg);
1757 option_error("missing variable name for unset");
1760 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1761 if (strcmp(arg, uep->ue_name) == 0)
1764 /* Ignore attempts by unprivileged users to override privileged sources */
1765 if (uep != NULL && !privileged_option && uep->ue_priv)
1767 /* The name never changes, so allocate it with the structure */
1769 uep = malloc(sizeof (*uep) + strlen(arg));
1770 strcpy(uep->ue_name, arg);
1771 uep->ue_next = NULL;
1772 insp = &userenv_list;
1773 while (*insp != NULL)
1774 insp = &(*insp)->ue_next;
1777 struct userenv *uep2;
1778 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1779 if (uep2 != uep && uep2->ue_isset)
1782 if (uep2 == NULL && uep->ue_isset)
1783 find_option("set")->flags |= OPT_NOPRINT;
1784 free(uep->ue_value);
1787 uep->ue_priv = privileged_option;
1788 uep->ue_source = option_source;
1789 uep->ue_value = NULL;
1790 curopt->flags &= ~OPT_NOPRINT;
1795 user_unsetprint(opt, printer, arg)
1797 printer_func printer;
1800 struct userenv *uep, *uepnext;
1802 uepnext = userenv_list;
1803 while (uepnext != NULL && uepnext->ue_isset)
1804 uepnext = uepnext->ue_next;
1805 while ((uep = uepnext) != NULL) {
1806 uepnext = uep->ue_next;
1807 while (uepnext != NULL && uepnext->ue_isset)
1808 uepnext = uepnext->ue_next;
1809 (*printer)(arg, "%s", uep->ue_name);
1810 if (uepnext != NULL)
1811 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1813 opt->source = uep->ue_source;