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 */
100 int maxconnect = 0; /* Maximum connect time */
101 char user[MAXNAMELEN]; /* Username for PAP */
102 char passwd[MAXSECRETLEN]; /* Password for PAP */
103 bool persist = 0; /* Reopen link after it goes down */
104 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
105 bool demand = 0; /* do dial-on-demand */
106 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
107 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
108 int holdoff = 30; /* # seconds to pause before reconnecting */
109 bool holdoff_specified; /* true if a holdoff value has been given */
110 int log_to_fd = 1; /* send log messages to this fd too */
111 bool log_default = 1; /* log_to_fd is default (stdout) */
112 int maxfail = 10; /* max # of unsuccessful connection attempts */
113 char linkname[MAXPATHLEN]; /* logical name for link */
114 bool tune_kernel; /* may alter kernel settings */
115 int connect_delay = 1000; /* wait this many ms after connect script */
116 int req_unit = -1; /* requested interface unit */
117 bool multilink = 0; /* Enable multilink operation */
118 char *bundle_name = NULL; /* bundle name for multilink */
119 bool dump_options; /* print out option values */
120 bool dryrun; /* print out option values and exit */
121 char *domain; /* domain name set by domain option */
122 int child_wait = 5; /* # seconds to wait for children at exit */
123 struct userenv *userenv_list; /* user environment variables */
126 unsigned int maxoctets = 0; /* default - no limit */
127 int maxoctets_dir = 0; /* default - sum of traffic */
128 int maxoctets_timeout = 1; /* default 1 second */
132 extern option_t auth_options[];
133 extern struct stat devstat;
136 struct bpf_program pass_filter;/* Filter program for packets to pass */
137 struct bpf_program active_filter; /* Filter program for link-active pkts */
140 static option_t *curopt; /* pointer to option being processed */
141 char *current_option; /* the name of the option being parsed */
142 int privileged_option; /* set iff the current option came from root */
143 char *option_source; /* string saying where the option came from */
144 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
145 bool devnam_fixed; /* can no longer change device name */
147 static int logfile_fd = -1; /* fd opened for log file */
148 static char logfile_name[MAXPATHLEN]; /* name of log file */
153 static int setdomain __P((char **));
154 static int readfile __P((char **));
155 static int callfile __P((char **));
156 static int showversion __P((char **));
157 static int showhelp __P((char **));
158 static void usage __P((void));
159 static int setlogfile __P((char **));
161 static int loadplugin __P((char **));
165 static int setpassfilter __P((char **));
166 static int setactivefilter __P((char **));
170 static int setmodir __P((char **));
173 static int user_setenv __P((char **));
174 static void user_setprint __P((option_t *, printer_func, void *));
175 static int user_unsetenv __P((char **));
176 static void user_unsetprint __P((option_t *, printer_func, void *));
178 static option_t *find_option __P((const char *name));
179 static int process_option __P((option_t *, char *, char **));
180 static int n_arguments __P((option_t *));
181 static int number_option __P((char *, u_int32_t *, int));
184 * Structure to store extra lists of options.
188 struct option_list *next;
191 static struct option_list *extra_options = NULL;
196 option_t general_options[] = {
197 { "debug", o_int, &debug,
198 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
199 { "-d", o_int, &debug,
200 "Increase debugging level",
201 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
203 { "kdebug", o_int, &kdebugflag,
204 "Set kernel driver debug level", OPT_PRIO },
206 { "nodetach", o_bool, &nodetach,
207 "Don't detach from controlling tty", OPT_PRIO | 1 },
208 { "-detach", o_bool, &nodetach,
209 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
210 { "updetach", o_bool, &updetach,
211 "Detach from controlling tty once link is up",
212 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
214 { "master_detach", o_bool, &master_detach,
215 "Detach when we're multilink master but have no link", 1 },
217 { "holdoff", o_int, &holdoff,
218 "Set time in seconds before retrying connection",
219 OPT_PRIO, &holdoff_specified },
221 { "idle", o_int, &idle_time_limit,
222 "Set time in seconds before disconnecting idle link", OPT_PRIO },
224 { "maxconnect", o_int, &maxconnect,
225 "Set connection time limit",
226 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
228 { "domain", o_special, (void *)setdomain,
229 "Add given domain name to hostname",
230 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
232 { "file", o_special, (void *)readfile,
233 "Take options from a file", OPT_NOPRINT },
234 { "call", o_special, (void *)callfile,
235 "Take options from a privileged file", OPT_NOPRINT },
237 { "persist", o_bool, &persist,
238 "Keep on reopening connection after close", OPT_PRIO | 1 },
239 { "nopersist", o_bool, &persist,
240 "Turn off persist option", OPT_PRIOSUB },
242 { "demand", o_bool, &demand,
243 "Dial on demand", OPT_INITONLY | 1, &persist },
245 { "--version", o_special_noarg, (void *)showversion,
246 "Show version number" },
247 { "--help", o_special_noarg, (void *)showhelp,
248 "Show brief listing of options" },
249 { "-h", o_special_noarg, (void *)showhelp,
250 "Show brief listing of options", OPT_ALIAS },
252 { "logfile", o_special, (void *)setlogfile,
253 "Append log messages to this file",
254 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
255 { "logfd", o_int, &log_to_fd,
256 "Send log messages to this file descriptor",
257 OPT_PRIOSUB | OPT_A2CLR, &log_default },
258 { "nolog", o_int, &log_to_fd,
259 "Don't send log messages to any file",
260 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
261 { "nologfd", o_int, &log_to_fd,
262 "Don't send log messages to any file descriptor",
263 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
265 { "linkname", o_string, linkname,
266 "Set logical name for link",
267 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
269 { "maxfail", o_int, &maxfail,
270 "Maximum number of unsuccessful connection attempts to allow",
273 { "ktune", o_bool, &tune_kernel,
274 "Alter kernel settings as necessary", OPT_PRIO | 1 },
275 { "noktune", o_bool, &tune_kernel,
276 "Don't alter kernel settings", OPT_PRIOSUB },
278 { "connect-delay", o_int, &connect_delay,
279 "Maximum time (in ms) to wait after connect script finishes",
282 { "unit", o_int, &req_unit,
283 "PPP interface unit number to use if possible",
284 OPT_PRIO | OPT_LLIMIT, 0, 0 },
286 { "dump", o_bool, &dump_options,
287 "Print out option values after parsing all options", 1 },
288 { "dryrun", o_bool, &dryrun,
289 "Stop after parsing, printing, and checking options", 1 },
291 { "child-timeout", o_int, &child_wait,
292 "Number of seconds to wait for child processes at exit",
295 { "set", o_special, (void *)user_setenv,
296 "Set user environment variable",
297 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
298 { "unset", o_special, (void *)user_unsetenv,
299 "Unset user environment variable",
300 OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
302 #ifdef HAVE_MULTILINK
303 { "multilink", o_bool, &multilink,
304 "Enable multilink operation", OPT_PRIO | 1 },
305 { "mp", o_bool, &multilink,
306 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
307 { "nomultilink", o_bool, &multilink,
308 "Disable multilink operation", OPT_PRIOSUB | 0 },
309 { "nomp", o_bool, &multilink,
310 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
312 { "bundle", o_string, &bundle_name,
313 "Bundle name for multilink", OPT_PRIO },
314 #endif /* HAVE_MULTILINK */
317 { "plugin", o_special, (void *)loadplugin,
318 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
322 { "pass-filter", o_special, setpassfilter,
323 "set filter for packets to pass", OPT_PRIO },
325 { "active-filter", o_special, setactivefilter,
326 "set filter for active pkts", OPT_PRIO },
330 { "maxoctets", o_int, &maxoctets,
331 "Set connection traffic limit",
332 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
333 { "mo", o_int, &maxoctets,
334 "Set connection traffic limit",
335 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
336 { "mo-direction", o_special, setmodir,
337 "Set direction for limit traffic (sum,in,out,max)" },
338 { "mo-timeout", o_int, &maxoctets_timeout,
339 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
345 #ifndef IMPLEMENTATION
346 #define IMPLEMENTATION ""
349 static char *usage_string = "\
351 Usage: %s [ options ], where options are:\n\
352 <device> Communicate over the named device\n\
353 <speed> Set the baud rate to <speed>\n\
354 <loc>:<rem> Set the local and/or remote interface IP\n\
355 addresses. Either one may be omitted.\n\
356 asyncmap <n> Set the desired async map to hex <n>\n\
357 auth Require authentication from peer\n\
358 connect <p> Invoke shell command <p> to set up the serial line\n\
359 crtscts Use hardware RTS/CTS flow control\n\
360 defaultroute Add default route through interface\n\
361 file <f> Take options from file <f>\n\
362 modem Use modem control lines\n\
363 mru <n> Set MRU value to <n> for negotiation\n\
364 See pppd(8) for more options.\n\
368 * parse_args - parse a string of arguments from the command line.
371 parse_args(argc, argv)
379 privileged_option = privileged;
380 option_source = "command line";
381 option_priority = OPRIO_CMDLINE;
385 opt = find_option(arg);
387 option_error("unrecognized option '%s'", arg);
391 n = n_arguments(opt);
393 option_error("too few parameters for option %s", arg);
396 if (!process_option(opt, arg, argv))
405 * options_from_file - Read a string of options from a file,
406 * and interpret them.
409 options_from_file(filename, must_exist, check_prot, priv)
416 int i, newline, ret, err;
422 char args[MAXARGS][MAXWORDLEN];
423 char cmd[MAXWORDLEN];
426 if (check_prot && seteuid(getuid()) == -1) {
427 option_error("unable to drop privileges to open %s: %m", filename);
430 f = fopen(filename, "r");
432 if (check_prot && seteuid(euid) == -1)
433 fatal("unable to regain privileges");
437 if (err != ENOENT && err != ENOTDIR)
438 warn("Warning: can't open options file %s: %m", filename);
441 option_error("Can't open options file %s: %m", filename);
445 oldpriv = privileged_option;
446 privileged_option = priv;
447 oldsource = option_source;
448 option_source = strdup(filename);
449 if (option_source == NULL)
450 option_source = "file";
452 while (getword(f, cmd, &newline, filename)) {
453 opt = find_option(cmd);
455 option_error("In file %s: unrecognized option '%s'",
459 n = n_arguments(opt);
460 for (i = 0; i < n; ++i) {
461 if (!getword(f, args[i], &newline, filename)) {
463 "In file %s: too few parameters for option '%s'",
469 if (!process_option(opt, cmd, argv))
476 privileged_option = oldpriv;
477 option_source = oldsource;
482 * options_from_user - See if the use has a ~/.ppprc file,
483 * and if so, interpret options from it.
488 char *user, *path, *file;
493 pw = getpwuid(getuid());
494 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
496 file = _PATH_USEROPT;
497 pl = strlen(user) + strlen(file) + 2;
500 novm("init file name");
501 slprintf(path, pl, "%s/%s", user, file);
502 option_priority = OPRIO_CFGFILE;
503 ret = options_from_file(path, 0, 1, privileged);
509 * options_for_tty - See if an options file exists for the serial
510 * device, and if so, interpret options from it.
511 * We only allow the per-tty options file to override anything from
512 * the command line if it is something that the user can't override
513 * once it has been set by root; this is done by giving configuration
514 * files a lower priority than the command line.
519 char *dev, *path, *p;
524 if ((p = strstr(dev, "/dev/")) != NULL)
526 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
527 return 1; /* don't look for /etc/ppp/options.tty */
528 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
531 novm("tty init file name");
532 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
533 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
534 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
537 option_priority = OPRIO_CFGFILE;
538 ret = options_from_file(path, 0, 0, 1);
544 * options_from_list - process a string of options in a wordlist.
547 options_from_list(w, priv)
556 privileged_option = priv;
557 option_source = "secrets file";
558 option_priority = OPRIO_SECFILE;
561 opt = find_option(w->word);
563 option_error("In secrets file: unrecognized option '%s'",
567 n = n_arguments(opt);
569 for (i = 0; i < n; ++i) {
573 "In secrets file: too few parameters for option '%s'",
579 if (!process_option(opt, w0->word, argv))
590 * match_option - see if this option matches an option_t structure.
593 match_option(name, opt, dowild)
598 int (*match) __P((char *, char **, int));
600 if (dowild != (opt->type == o_wild))
603 return strcmp(name, opt->name) == 0;
604 match = (int (*) __P((char *, char **, int))) opt->addr;
605 return (*match)(name, NULL, 0);
609 * find_option - scan the option lists for the various protocols
610 * looking for an entry with the given name.
611 * This could be optimized by using a hash table.
618 struct option_list *list;
621 for (dowild = 0; dowild <= 1; ++dowild) {
622 for (opt = general_options; opt->name != NULL; ++opt)
623 if (match_option(name, opt, dowild))
625 for (opt = auth_options; opt->name != NULL; ++opt)
626 if (match_option(name, opt, dowild))
628 for (list = extra_options; list != NULL; list = list->next)
629 for (opt = list->options; opt->name != NULL; ++opt)
630 if (match_option(name, opt, dowild))
632 for (opt = the_channel->options; opt->name != NULL; ++opt)
633 if (match_option(name, opt, dowild))
635 for (i = 0; protocols[i] != NULL; ++i)
636 if ((opt = protocols[i]->options) != NULL)
637 for (; opt->name != NULL; ++opt)
638 if (match_option(name, opt, dowild))
645 * process_option - process one new-style option.
648 process_option(opt, cmd, argv)
656 int (*parser) __P((char **));
657 int (*wildp) __P((char *, char **, int));
658 char *optopt = (opt->type == o_wild)? "": " option";
659 int prio = option_priority;
660 option_t *mainopt = opt;
662 current_option = opt->name;
663 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
665 while (mainopt->flags & OPT_PRIOSUB)
667 if (mainopt->flags & OPT_PRIO) {
668 if (prio < mainopt->priority) {
669 /* new value doesn't override old */
670 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
671 option_error("%s%s set in %s cannot be overridden\n",
672 opt->name, optopt, mainopt->source);
677 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
678 warn("%s%s from %s overrides command line",
679 opt->name, optopt, option_source);
682 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
683 option_error("%s%s cannot be changed after initialization",
687 if ((opt->flags & OPT_PRIV) && !privileged_option) {
688 option_error("using the %s%s requires root privilege",
692 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
693 option_error("%s%s is disabled", opt->name, optopt);
696 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
697 option_error("the %s%s may not be changed in %s",
698 opt->name, optopt, option_source);
704 v = opt->flags & OPT_VALUE;
705 *(bool *)(opt->addr) = v;
706 if (opt->addr2 && (opt->flags & OPT_A2COPY))
707 *(bool *)(opt->addr2) = v;
708 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
709 *(bool *)(opt->addr2) = 0;
710 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
711 *(u_char *)(opt->addr2) &= ~v;
712 else if (opt->addr2 && (opt->flags & OPT_A2OR))
713 *(u_char *)(opt->addr2) |= v;
718 if ((opt->flags & OPT_NOARG) == 0) {
719 if (!int_option(*argv, &iv))
721 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
722 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
723 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
724 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
725 switch (opt->flags & OPT_LIMITS) {
727 option_error("%s value must be%s >= %d",
728 opt->name, zok, opt->lower_limit);
731 option_error("%s value must be%s <= %d",
732 opt->name, zok, opt->upper_limit);
735 option_error("%s value must be%s between %d and %d",
736 opt->name, zok, opt->lower_limit, opt->upper_limit);
742 a = opt->flags & OPT_VALUE;
744 a -= 256; /* sign extend */
746 if (opt->flags & OPT_INC)
747 iv += *(int *)(opt->addr);
748 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
749 int oldv = *(int *)(opt->addr);
750 if ((opt->flags & OPT_ZEROINF) ?
751 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
752 option_error("%s value cannot be increased", opt->name);
756 *(int *)(opt->addr) = iv;
757 if (opt->addr2 && (opt->flags & OPT_A2COPY))
758 *(int *)(opt->addr2) = iv;
762 if (opt->flags & OPT_NOARG) {
763 v = opt->flags & OPT_VALUE;
766 } else if (!number_option(*argv, &v, 16))
768 if (opt->flags & OPT_OR)
769 v |= *(u_int32_t *)(opt->addr);
770 *(u_int32_t *)(opt->addr) = v;
771 if (opt->addr2 && (opt->flags & OPT_A2COPY))
772 *(u_int32_t *)(opt->addr2) = v;
776 if (opt->flags & OPT_STATIC) {
777 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
781 novm("option argument");
782 *(char **)(opt->addr) = sv;
786 case o_special_noarg:
788 parser = (int (*) __P((char **))) opt->addr;
790 if (!(*parser)(argv))
792 if (opt->flags & OPT_A2LIST) {
793 struct option_value *ovp, *pp;
795 ovp = malloc(sizeof(*ovp) + strlen(*argv));
797 strcpy(ovp->value, *argv);
798 ovp->source = option_source;
800 if (opt->addr2 == NULL) {
803 for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
812 wildp = (int (*) __P((char *, char **, int))) opt->addr;
813 if (!(*wildp)(cmd, argv, 1))
819 * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
820 * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
822 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
823 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
824 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
826 mainopt->source = option_source;
827 mainopt->priority = prio;
828 mainopt->winner = opt - mainopt;
834 * override_value - if the option priorities would permit us to
835 * override the value of option, return 1 and update the priority
836 * and source of the option value. Otherwise returns 0.
839 override_value(option, priority, source)
846 opt = find_option(option);
849 while (opt->flags & OPT_PRIOSUB)
851 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
853 opt->priority = priority;
854 opt->source = source;
860 * n_arguments - tell how many arguments an option takes
866 return (opt->type == o_bool || opt->type == o_special_noarg
867 || (opt->flags & OPT_NOARG))? 0: 1;
871 * add_options - add a list of options to the set we grok.
877 struct option_list *list;
879 list = malloc(sizeof(*list));
881 novm("option list entry");
883 list->next = extra_options;
884 extra_options = list;
888 * check_options - check that options are valid and consistent.
893 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
898 * print_option - print out an option and its value
901 print_option(opt, mainopt, printer, arg)
902 option_t *opt, *mainopt;
903 printer_func printer;
909 if (opt->flags & OPT_NOPRINT)
913 v = opt->flags & OPT_VALUE;
914 if (*(bool *)opt->addr != v)
915 /* this can happen legitimately, e.g. lock
916 option turned off for default device */
918 printer(arg, "%s", opt->name);
921 v = opt->flags & OPT_VALUE;
924 i = *(int *)opt->addr;
925 if (opt->flags & OPT_NOARG) {
926 printer(arg, "%s", opt->name);
928 if (opt->flags & OPT_INC) {
929 for (; i > v; i -= v)
930 printer(arg, " %s", opt->name);
932 printer(arg, " # oops: %d not %d\n",
936 printer(arg, "%s %d", opt->name, i);
940 printer(arg, "%s", opt->name);
941 if ((opt->flags & OPT_NOARG) == 0)
942 printer(arg, " %x", *(u_int32_t *)opt->addr);
946 if (opt->flags & OPT_HIDE) {
949 p = (char *) opt->addr;
950 if ((opt->flags & OPT_STATIC) == 0)
953 printer(arg, "%s %q", opt->name, p);
957 case o_special_noarg:
959 if (opt->type != o_wild) {
960 printer(arg, "%s", opt->name);
961 if (n_arguments(opt) == 0)
965 if (opt->flags & OPT_A2PRINTER) {
966 void (*oprt) __P((option_t *, printer_func, void *));
967 oprt = (void (*) __P((option_t *, printer_func,
969 (*oprt)(opt, printer, arg);
970 } else if (opt->flags & OPT_A2STRVAL) {
971 p = (char *) opt->addr2;
972 if ((opt->flags & OPT_STATIC) == 0)
975 } else if (opt->flags & OPT_A2LIST) {
976 struct option_value *ovp;
978 ovp = (struct option_value *) opt->addr2;
980 printer(arg, "%q", ovp->value);
981 if ((ovp = ovp->next) == NULL)
983 printer(arg, "\t\t# (from %s)\n%s ",
984 ovp->source, opt->name);
987 printer(arg, "xxx # [don't know how to print value]");
992 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
995 printer(arg, "\t\t# (from %s)\n", mainopt->source);
999 * print_option_list - print out options in effect from an
1003 print_option_list(opt, printer, arg)
1005 printer_func printer;
1008 while (opt->name != NULL) {
1009 if (opt->priority != OPRIO_DEFAULT
1010 && opt->winner != (short int) -1)
1011 print_option(opt + opt->winner, opt, printer, arg);
1014 } while (opt->flags & OPT_PRIOSUB);
1019 * print_options - print out what options are in effect.
1022 print_options(printer, arg)
1023 printer_func printer;
1026 struct option_list *list;
1029 printer(arg, "pppd options in effect:\n");
1030 print_option_list(general_options, printer, arg);
1031 print_option_list(auth_options, printer, arg);
1032 for (list = extra_options; list != NULL; list = list->next)
1033 print_option_list(list->options, printer, arg);
1034 print_option_list(the_channel->options, printer, arg);
1035 for (i = 0; protocols[i] != NULL; ++i)
1036 print_option_list(protocols[i]->options, printer, arg);
1040 * usage - print out a message telling how to use the program.
1045 if (phase == PHASE_INITIALIZE)
1046 fprintf(stderr, usage_string, VERSION, progname);
1050 * showhelp - print out usage message and exit.
1056 if (phase == PHASE_INITIALIZE) {
1064 * showversion - print out the version number and exit.
1070 if (phase == PHASE_INITIALIZE) {
1071 fprintf(stderr, "pppd version %s\n", VERSION);
1078 * option_error - print a message about an error in an option.
1079 * The message is logged, and also sent to
1080 * stderr if phase == PHASE_INITIALIZE.
1083 option_error __V((char *fmt, ...))
1088 #if defined(__STDC__)
1089 va_start(args, fmt);
1093 fmt = va_arg(args, char *);
1095 vslprintf(buf, sizeof(buf), fmt, args);
1097 if (phase == PHASE_INITIALIZE)
1098 fprintf(stderr, "%s: %s\n", progname, buf);
1099 syslog(LOG_ERR, "%s", buf);
1104 * readable - check if a file is readable by the real user.
1117 if (fstat(fd, &sbuf) != 0)
1119 if (sbuf.st_uid == uid)
1120 return sbuf.st_mode & S_IRUSR;
1121 if (sbuf.st_gid == getgid())
1122 return sbuf.st_mode & S_IRGRP;
1123 for (i = 0; i < ngroups; ++i)
1124 if (sbuf.st_gid == groups[i])
1125 return sbuf.st_mode & S_IRGRP;
1126 return sbuf.st_mode & S_IROTH;
1131 * Read a word from a file.
1132 * Words are delimited by white-space or by quotes (" or ').
1133 * Quotes, white-space and \ may be escaped with \.
1134 * \<newline> is ignored.
1137 getword(f, word, newlinep, filename)
1144 int quoted, comment;
1145 int value, digit, got, n;
1147 #define isoctal(c) ((c) >= '0' && (c) < '8')
1156 * First skip white-space and comments.
1164 * A newline means the end of a comment; backslash-newline
1165 * is ignored. Note that we cannot have escape && comment.
1177 * Ignore characters other than newline in a comment.
1183 * If this character is escaped, we have a word start.
1189 * If this is the escape character, look at the next character.
1197 * If this is the start of a comment, ignore the rest of the line.
1205 * A non-whitespace character is the start of a word.
1212 * Process characters until the end of the word.
1217 * This character is escaped: backslash-newline is ignored,
1218 * various other characters indicate particular values
1219 * as for C backslash-escapes.
1254 * \ddd octal sequence
1257 for (n = 0; n < 3 && isoctal(c); ++n) {
1258 value = (value << 3) + (c & 07);
1267 * \x<hex_string> sequence
1271 for (n = 0; n < 2 && isxdigit(c); ++n) {
1272 digit = toupper(c) - '0';
1274 digit += '0' + 10 - 'A';
1275 value = (value << 4) + digit;
1283 * Otherwise the character stands for itself.
1290 * Store the resulting character for the escape sequence.
1292 if (len < MAXWORDLEN-1)
1302 * Backslash starts a new escape sequence.
1311 * Not escaped: check for the start or end of a quoted
1312 * section and see if we've reached the end of the word.
1320 } else if (c == '"' || c == '\'') {
1324 } else if (isspace(c) || c == '#') {
1330 * An ordinary character: store it in the word and get another.
1332 if (len < MAXWORDLEN-1)
1340 * End of the word: check for errors.
1346 option_error("Error reading %s: %m", filename);
1350 * If len is zero, then we didn't find a word before the
1356 option_error("warning: quoted word runs to end of file (%.20s...)",
1361 * Warn if the word was too long, and append a terminating null.
1363 if (len >= MAXWORDLEN) {
1364 option_error("warning: word in file %s too long (%.20s...)",
1366 len = MAXWORDLEN - 1;
1377 * number_option - parse an unsigned numeric parameter for an option.
1380 number_option(str, valp, base)
1387 *valp = strtoul(str, &ptr, base);
1389 option_error("invalid numeric parameter '%s' for %s option",
1390 str, current_option);
1398 * int_option - like number_option, but valp is int *,
1399 * the base is assumed to be 0, and *valp is not changed
1400 * if there is an error.
1403 int_option(str, valp)
1409 if (!number_option(str, &v, 0))
1417 * The following procedures parse options.
1421 * readfile - take commands from a file.
1427 return options_from_file(*argv, 1, 1, privileged_option);
1431 * callfile - take commands from /etc/ppp/peers/<name>.
1432 * Name may not contain /../, start with / or ../, or end in /..
1438 char *fname, *arg, *p;
1443 if (arg[0] == '/' || arg[0] == 0)
1446 for (p = arg; *p != 0; ) {
1447 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1451 while (*p != '/' && *p != 0)
1458 option_error("call option value may not contain .. or start with /");
1462 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1463 if ((fname = (char *) malloc(l)) == NULL)
1464 novm("call file name");
1465 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1467 ok = options_from_file(fname, 1, 1, 1);
1475 * setpassfilter - Set the pass filter for packets
1484 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1485 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1486 option_error("error in pass-filter expression: %s\n",
1496 * setactivefilter - Set the active filter for packets
1499 setactivefilter(argv)
1505 pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1506 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1507 option_error("error in active-filter expression: %s\n",
1518 * setdomain - Set domain name to append to hostname
1524 gethostname(hostname, MAXNAMELEN);
1527 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1528 domain = hostname + strlen(hostname);
1529 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1531 hostname[MAXNAMELEN-1] = 0;
1543 if (!privileged_option && seteuid(getuid()) == -1) {
1544 option_error("unable to drop permissions to open %s: %m", *argv);
1547 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1548 if (fd < 0 && errno == EEXIST)
1549 fd = open(*argv, O_WRONLY | O_APPEND);
1551 if (!privileged_option && seteuid(euid) == -1)
1552 fatal("unable to regain privileges: %m");
1555 option_error("Can't open log file %s: %m", *argv);
1558 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1559 if (logfile_fd >= 0)
1574 if(!strcmp(*argv,"in")) {
1575 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1576 } else if (!strcmp(*argv,"out")) {
1577 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1578 } else if (!strcmp(*argv,"max")) {
1579 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1581 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1595 void (*init) __P((void));
1599 if (strchr(arg, '/') == 0) {
1600 const char *base = _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);
1644 * Set an environment variable specified by the user.
1650 char *arg = argv[0];
1652 struct userenv *uep, **insp;
1654 if ((eqp = strchr(arg, '=')) == NULL) {
1655 option_error("missing = in name=value: %s", arg);
1659 option_error("missing variable name: %s", arg);
1662 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1663 int nlen = strlen(uep->ue_name);
1664 if (nlen == (eqp - arg) &&
1665 strncmp(arg, uep->ue_name, nlen) == 0)
1668 /* Ignore attempts by unprivileged users to override privileged sources */
1669 if (uep != NULL && !privileged_option && uep->ue_priv)
1671 /* The name never changes, so allocate it with the structure */
1673 uep = malloc(sizeof (*uep) + (eqp-arg));
1674 strncpy(uep->ue_name, arg, eqp-arg);
1675 uep->ue_name[eqp-arg] = '\0';
1676 uep->ue_next = NULL;
1677 insp = &userenv_list;
1678 while (*insp != NULL)
1679 insp = &(*insp)->ue_next;
1682 struct userenv *uep2;
1683 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1684 if (uep2 != uep && !uep2->ue_isset)
1687 if (uep2 == NULL && !uep->ue_isset)
1688 find_option("unset")->flags |= OPT_NOPRINT;
1689 free(uep->ue_value);
1692 uep->ue_priv = privileged_option;
1693 uep->ue_source = option_source;
1694 uep->ue_value = strdup(eqp + 1);
1695 curopt->flags &= ~OPT_NOPRINT;
1700 user_setprint(opt, printer, arg)
1702 printer_func printer;
1705 struct userenv *uep, *uepnext;
1707 uepnext = userenv_list;
1708 while (uepnext != NULL && !uepnext->ue_isset)
1709 uepnext = uepnext->ue_next;
1710 while ((uep = uepnext) != NULL) {
1711 uepnext = uep->ue_next;
1712 while (uepnext != NULL && !uepnext->ue_isset)
1713 uepnext = uepnext->ue_next;
1714 (*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1715 if (uepnext != NULL)
1716 (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1718 opt->source = uep->ue_source;
1726 struct userenv *uep, **insp;
1727 char *arg = argv[0];
1729 if (strchr(arg, '=') != NULL) {
1730 option_error("unexpected = in name: %s", arg);
1734 option_error("missing variable name for unset");
1737 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1738 if (strcmp(arg, uep->ue_name) == 0)
1741 /* Ignore attempts by unprivileged users to override privileged sources */
1742 if (uep != NULL && !privileged_option && uep->ue_priv)
1744 /* The name never changes, so allocate it with the structure */
1746 uep = malloc(sizeof (*uep) + strlen(arg));
1747 strcpy(uep->ue_name, arg);
1748 uep->ue_next = NULL;
1749 insp = &userenv_list;
1750 while (*insp != NULL)
1751 insp = &(*insp)->ue_next;
1754 struct userenv *uep2;
1755 for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1756 if (uep2 != uep && uep2->ue_isset)
1759 if (uep2 == NULL && uep->ue_isset)
1760 find_option("set")->flags |= OPT_NOPRINT;
1761 free(uep->ue_value);
1764 uep->ue_priv = privileged_option;
1765 uep->ue_source = option_source;
1766 uep->ue_value = NULL;
1767 curopt->flags &= ~OPT_NOPRINT;
1772 user_unsetprint(opt, printer, arg)
1774 printer_func printer;
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;