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.93 2004/10/28 00:15:08 paulus Exp $"
61 * DLT_PPP_WITH_DIRECTION is in current libpcap cvs, and should be in
62 * libpcap-0.8.4. Until that is released, use DLT_PPP - but that means
63 * we lose the inbound and outbound qualifiers.
65 #ifndef DLT_PPP_WITH_DIRECTION
66 #define DLT_PPP_WITH_DIRECTION DLT_PPP
71 #include "pathnames.h"
73 #if defined(ultrix) || defined(NeXT)
74 char *strdup __P((char *));
77 static const char rcsid[] = RCSID;
80 struct option_value *next;
86 * Option variables and default values.
88 int debug = 0; /* Debug flag */
89 int kdebugflag = 0; /* Tell kernel to print debug messages */
90 int default_device = 1; /* Using /dev/tty or equivalent */
91 char devnam[MAXPATHLEN]; /* Device name */
92 bool nodetach = 0; /* Don't detach from controlling tty */
93 bool updetach = 0; /* Detach once link is up */
94 int maxconnect = 0; /* Maximum connect time */
95 char user[MAXNAMELEN]; /* Username for PAP */
96 char passwd[MAXSECRETLEN]; /* Password for PAP */
97 bool persist = 0; /* Reopen link after it goes down */
98 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
99 bool demand = 0; /* do dial-on-demand */
100 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
101 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
102 int holdoff = 30; /* # seconds to pause before reconnecting */
103 bool holdoff_specified; /* true if a holdoff value has been given */
104 int log_to_fd = 1; /* send log messages to this fd too */
105 bool log_default = 1; /* log_to_fd is default (stdout) */
106 int maxfail = 10; /* max # of unsuccessful connection attempts */
107 char linkname[MAXPATHLEN]; /* logical name for link */
108 bool tune_kernel; /* may alter kernel settings */
109 int connect_delay = 1000; /* wait this many ms after connect script */
110 int req_unit = -1; /* requested interface unit */
111 bool multilink = 0; /* Enable multilink operation */
112 char *bundle_name = NULL; /* bundle name for multilink */
113 bool dump_options; /* print out option values */
114 bool dryrun; /* print out option values and exit */
115 char *domain; /* domain name set by domain option */
118 unsigned int maxoctets = 0; /* default - no limit */
119 int maxoctets_dir = 0; /* default - sum of traffic */
120 int maxoctets_timeout = 1; /* default 1 second */
124 extern option_t auth_options[];
125 extern struct stat devstat;
128 struct bpf_program pass_filter;/* Filter program for packets to pass */
129 struct bpf_program active_filter; /* Filter program for link-active pkts */
132 char *current_option; /* the name of the option being parsed */
133 int privileged_option; /* set iff the current option came from root */
134 char *option_source; /* string saying where the option came from */
135 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
136 bool devnam_fixed; /* can no longer change device name */
138 static int logfile_fd = -1; /* fd opened for log file */
139 static char logfile_name[MAXPATHLEN]; /* name of log file */
144 static int setdomain __P((char **));
145 static int readfile __P((char **));
146 static int callfile __P((char **));
147 static int showversion __P((char **));
148 static int showhelp __P((char **));
149 static void usage __P((void));
150 static int setlogfile __P((char **));
152 static int loadplugin __P((char **));
156 static int setpassfilter __P((char **));
157 static int setactivefilter __P((char **));
161 static int setmodir __P((char **));
164 static option_t *find_option __P((const char *name));
165 static int process_option __P((option_t *, char *, char **));
166 static int n_arguments __P((option_t *));
167 static int number_option __P((char *, u_int32_t *, int));
170 * Structure to store extra lists of options.
174 struct option_list *next;
177 static struct option_list *extra_options = NULL;
182 option_t general_options[] = {
183 { "debug", o_int, &debug,
184 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
185 { "-d", o_int, &debug,
186 "Increase debugging level",
187 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
189 { "kdebug", o_int, &kdebugflag,
190 "Set kernel driver debug level", OPT_PRIO },
192 { "nodetach", o_bool, &nodetach,
193 "Don't detach from controlling tty", OPT_PRIO | 1 },
194 { "-detach", o_bool, &nodetach,
195 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
196 { "updetach", o_bool, &updetach,
197 "Detach from controlling tty once link is up",
198 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
200 { "holdoff", o_int, &holdoff,
201 "Set time in seconds before retrying connection", OPT_PRIO },
203 { "idle", o_int, &idle_time_limit,
204 "Set time in seconds before disconnecting idle link", OPT_PRIO },
206 { "maxconnect", o_int, &maxconnect,
207 "Set connection time limit",
208 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
210 { "domain", o_special, (void *)setdomain,
211 "Add given domain name to hostname",
212 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
214 { "file", o_special, (void *)readfile,
215 "Take options from a file", OPT_NOPRINT },
216 { "call", o_special, (void *)callfile,
217 "Take options from a privileged file", OPT_NOPRINT },
219 { "persist", o_bool, &persist,
220 "Keep on reopening connection after close", OPT_PRIO | 1 },
221 { "nopersist", o_bool, &persist,
222 "Turn off persist option", OPT_PRIOSUB },
224 { "demand", o_bool, &demand,
225 "Dial on demand", OPT_INITONLY | 1, &persist },
227 { "--version", o_special_noarg, (void *)showversion,
228 "Show version number" },
229 { "--help", o_special_noarg, (void *)showhelp,
230 "Show brief listing of options" },
231 { "-h", o_special_noarg, (void *)showhelp,
232 "Show brief listing of options", OPT_ALIAS },
234 { "logfile", o_special, (void *)setlogfile,
235 "Append log messages to this file",
236 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
237 { "logfd", o_int, &log_to_fd,
238 "Send log messages to this file descriptor",
239 OPT_PRIOSUB | OPT_A2CLR, &log_default },
240 { "nolog", o_int, &log_to_fd,
241 "Don't send log messages to any file",
242 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
243 { "nologfd", o_int, &log_to_fd,
244 "Don't send log messages to any file descriptor",
245 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
247 { "linkname", o_string, linkname,
248 "Set logical name for link",
249 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
251 { "maxfail", o_int, &maxfail,
252 "Maximum number of unsuccessful connection attempts to allow",
255 { "ktune", o_bool, &tune_kernel,
256 "Alter kernel settings as necessary", OPT_PRIO | 1 },
257 { "noktune", o_bool, &tune_kernel,
258 "Don't alter kernel settings", OPT_PRIOSUB },
260 { "connect-delay", o_int, &connect_delay,
261 "Maximum time (in ms) to wait after connect script finishes",
264 { "unit", o_int, &req_unit,
265 "PPP interface unit number to use if possible",
266 OPT_PRIO | OPT_LLIMIT, 0, 0 },
268 { "dump", o_bool, &dump_options,
269 "Print out option values after parsing all options", 1 },
270 { "dryrun", o_bool, &dryrun,
271 "Stop after parsing, printing, and checking options", 1 },
273 #ifdef HAVE_MULTILINK
274 { "multilink", o_bool, &multilink,
275 "Enable multilink operation", OPT_PRIO | 1 },
276 { "mp", o_bool, &multilink,
277 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
278 { "nomultilink", o_bool, &multilink,
279 "Disable multilink operation", OPT_PRIOSUB | 0 },
280 { "nomp", o_bool, &multilink,
281 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
283 { "bundle", o_string, &bundle_name,
284 "Bundle name for multilink", OPT_PRIO },
285 #endif /* HAVE_MULTILINK */
288 { "plugin", o_special, (void *)loadplugin,
289 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
293 { "pass-filter", 1, setpassfilter,
294 "set filter for packets to pass", OPT_PRIO },
296 { "active-filter", 1, setactivefilter,
297 "set filter for active pkts", OPT_PRIO },
301 { "maxoctets", o_int, &maxoctets,
302 "Set connection traffic limit",
303 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
304 { "mo", o_int, &maxoctets,
305 "Set connection traffic limit",
306 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
307 { "mo-direction", o_special, setmodir,
308 "Set direction for limit traffic (sum,in,out,max)" },
309 { "mo-timeout", o_int, &maxoctets_timeout,
310 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
316 #ifndef IMPLEMENTATION
317 #define IMPLEMENTATION ""
320 static char *usage_string = "\
322 Usage: %s [ options ], where options are:\n\
323 <device> Communicate over the named device\n\
324 <speed> Set the baud rate to <speed>\n\
325 <loc>:<rem> Set the local and/or remote interface IP\n\
326 addresses. Either one may be omitted.\n\
327 asyncmap <n> Set the desired async map to hex <n>\n\
328 auth Require authentication from peer\n\
329 connect <p> Invoke shell command <p> to set up the serial line\n\
330 crtscts Use hardware RTS/CTS flow control\n\
331 defaultroute Add default route through interface\n\
332 file <f> Take options from file <f>\n\
333 modem Use modem control lines\n\
334 mru <n> Set MRU value to <n> for negotiation\n\
335 See pppd(8) for more options.\n\
339 * parse_args - parse a string of arguments from the command line.
342 parse_args(argc, argv)
350 privileged_option = privileged;
351 option_source = "command line";
352 option_priority = OPRIO_CMDLINE;
356 opt = find_option(arg);
358 option_error("unrecognized option '%s'", arg);
362 n = n_arguments(opt);
364 option_error("too few parameters for option %s", arg);
367 if (!process_option(opt, arg, argv))
376 * options_from_file - Read a string of options from a file,
377 * and interpret them.
380 options_from_file(filename, must_exist, check_prot, priv)
387 int i, newline, ret, err;
392 char args[MAXARGS][MAXWORDLEN];
393 char cmd[MAXWORDLEN];
397 f = fopen(filename, "r");
404 if (err != ENOENT && err != ENOTDIR)
405 warn("Warning: can't open options file %s: %m", filename);
408 option_error("Can't open options file %s: %m", filename);
412 oldpriv = privileged_option;
413 privileged_option = priv;
414 oldsource = option_source;
415 option_source = strdup(filename);
416 if (option_source == NULL)
417 option_source = "file";
419 while (getword(f, cmd, &newline, filename)) {
420 opt = find_option(cmd);
422 option_error("In file %s: unrecognized option '%s'",
426 n = n_arguments(opt);
427 for (i = 0; i < n; ++i) {
428 if (!getword(f, args[i], &newline, filename)) {
430 "In file %s: too few parameters for option '%s'",
436 if (!process_option(opt, cmd, argv))
443 privileged_option = oldpriv;
444 option_source = oldsource;
449 * options_from_user - See if the use has a ~/.ppprc file,
450 * and if so, interpret options from it.
455 char *user, *path, *file;
460 pw = getpwuid(getuid());
461 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
463 file = _PATH_USEROPT;
464 pl = strlen(user) + strlen(file) + 2;
467 novm("init file name");
468 slprintf(path, pl, "%s/%s", user, file);
469 option_priority = OPRIO_CFGFILE;
470 ret = options_from_file(path, 0, 1, privileged);
476 * options_for_tty - See if an options file exists for the serial
477 * device, and if so, interpret options from it.
478 * We only allow the per-tty options file to override anything from
479 * the command line if it is something that the user can't override
480 * once it has been set by root; this is done by giving configuration
481 * files a lower priority than the command line.
486 char *dev, *path, *p;
491 if ((p = strstr(dev, "/dev/")) != NULL)
493 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
494 return 1; /* don't look for /etc/ppp/options.tty */
495 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
498 novm("tty init file name");
499 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
500 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
501 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
504 option_priority = OPRIO_CFGFILE;
505 ret = options_from_file(path, 0, 0, 1);
511 * options_from_list - process a string of options in a wordlist.
514 options_from_list(w, priv)
523 privileged_option = priv;
524 option_source = "secrets file";
525 option_priority = OPRIO_SECFILE;
528 opt = find_option(w->word);
530 option_error("In secrets file: unrecognized option '%s'",
534 n = n_arguments(opt);
536 for (i = 0; i < n; ++i) {
540 "In secrets file: too few parameters for option '%s'",
546 if (!process_option(opt, w0->word, argv))
557 * match_option - see if this option matches an option_t structure.
560 match_option(name, opt, dowild)
565 int (*match) __P((char *, char **, int));
567 if (dowild != (opt->type == o_wild))
570 return strcmp(name, opt->name) == 0;
571 match = (int (*) __P((char *, char **, int))) opt->addr;
572 return (*match)(name, NULL, 0);
576 * find_option - scan the option lists for the various protocols
577 * looking for an entry with the given name.
578 * This could be optimized by using a hash table.
585 struct option_list *list;
588 for (dowild = 0; dowild <= 1; ++dowild) {
589 for (opt = general_options; opt->name != NULL; ++opt)
590 if (match_option(name, opt, dowild))
592 for (opt = auth_options; opt->name != NULL; ++opt)
593 if (match_option(name, opt, dowild))
595 for (list = extra_options; list != NULL; list = list->next)
596 for (opt = list->options; opt->name != NULL; ++opt)
597 if (match_option(name, opt, dowild))
599 for (opt = the_channel->options; opt->name != NULL; ++opt)
600 if (match_option(name, opt, dowild))
602 for (i = 0; protocols[i] != NULL; ++i)
603 if ((opt = protocols[i]->options) != NULL)
604 for (; opt->name != NULL; ++opt)
605 if (match_option(name, opt, dowild))
612 * process_option - process one new-style option.
615 process_option(opt, cmd, argv)
623 int (*parser) __P((char **));
624 int (*wildp) __P((char *, char **, int));
625 char *optopt = (opt->type == o_wild)? "": " option";
626 int prio = option_priority;
627 option_t *mainopt = opt;
629 current_option = opt->name;
630 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
632 while (mainopt->flags & OPT_PRIOSUB)
634 if (mainopt->flags & OPT_PRIO) {
635 if (prio < mainopt->priority) {
636 /* new value doesn't override old */
637 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
638 option_error("%s%s set in %s cannot be overridden\n",
639 opt->name, optopt, mainopt->source);
644 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
645 warn("%s%s from %s overrides command line",
646 opt->name, optopt, option_source);
649 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
650 option_error("%s%s cannot be changed after initialization",
654 if ((opt->flags & OPT_PRIV) && !privileged_option) {
655 option_error("using the %s%s requires root privilege",
659 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
660 option_error("%s%s is disabled", opt->name, optopt);
663 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
664 option_error("the %s%s may not be changed in %s",
665 opt->name, optopt, option_source);
671 v = opt->flags & OPT_VALUE;
672 *(bool *)(opt->addr) = v;
673 if (opt->addr2 && (opt->flags & OPT_A2COPY))
674 *(bool *)(opt->addr2) = v;
675 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
676 *(bool *)(opt->addr2) = 0;
677 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
678 *(u_char *)(opt->addr2) &= ~v;
679 else if (opt->addr2 && (opt->flags & OPT_A2OR))
680 *(u_char *)(opt->addr2) |= v;
685 if ((opt->flags & OPT_NOARG) == 0) {
686 if (!int_option(*argv, &iv))
688 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
689 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
690 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
691 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
692 switch (opt->flags & OPT_LIMITS) {
694 option_error("%s value must be%s >= %d",
695 opt->name, zok, opt->lower_limit);
698 option_error("%s value must be%s <= %d",
699 opt->name, zok, opt->upper_limit);
702 option_error("%s value must be%s between %d and %d",
703 opt->name, zok, opt->lower_limit, opt->upper_limit);
709 a = opt->flags & OPT_VALUE;
711 a -= 256; /* sign extend */
713 if (opt->flags & OPT_INC)
714 iv += *(int *)(opt->addr);
715 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
716 int oldv = *(int *)(opt->addr);
717 if ((opt->flags & OPT_ZEROINF) ?
718 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
719 option_error("%s value cannot be increased", opt->name);
723 *(int *)(opt->addr) = iv;
724 if (opt->addr2 && (opt->flags & OPT_A2COPY))
725 *(int *)(opt->addr2) = iv;
729 if (opt->flags & OPT_NOARG) {
730 v = opt->flags & OPT_VALUE;
733 } else if (!number_option(*argv, &v, 16))
735 if (opt->flags & OPT_OR)
736 v |= *(u_int32_t *)(opt->addr);
737 *(u_int32_t *)(opt->addr) = v;
738 if (opt->addr2 && (opt->flags & OPT_A2COPY))
739 *(u_int32_t *)(opt->addr2) = v;
743 if (opt->flags & OPT_STATIC) {
744 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
748 novm("option argument");
749 *(char **)(opt->addr) = sv;
753 case o_special_noarg:
755 parser = (int (*) __P((char **))) opt->addr;
756 if (!(*parser)(argv))
758 if (opt->flags & OPT_A2LIST) {
759 struct option_value *ovp, **pp;
761 ovp = malloc(sizeof(*ovp) + strlen(*argv));
763 strcpy(ovp->value, *argv);
764 ovp->source = option_source;
766 pp = (struct option_value **) &opt->addr2;
775 wildp = (int (*) __P((char *, char **, int))) opt->addr;
776 if (!(*wildp)(cmd, argv, 1))
781 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
782 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
783 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
785 mainopt->source = option_source;
786 mainopt->priority = prio;
787 mainopt->winner = opt - mainopt;
793 * override_value - if the option priorities would permit us to
794 * override the value of option, return 1 and update the priority
795 * and source of the option value. Otherwise returns 0.
798 override_value(option, priority, source)
805 opt = find_option(option);
808 while (opt->flags & OPT_PRIOSUB)
810 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
812 opt->priority = priority;
813 opt->source = source;
819 * n_arguments - tell how many arguments an option takes
825 return (opt->type == o_bool || opt->type == o_special_noarg
826 || (opt->flags & OPT_NOARG))? 0: 1;
830 * add_options - add a list of options to the set we grok.
836 struct option_list *list;
838 list = malloc(sizeof(*list));
840 novm("option list entry");
842 list->next = extra_options;
843 extra_options = list;
847 * check_options - check that options are valid and consistent.
852 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
857 * print_option - print out an option and its value
860 print_option(opt, mainopt, printer, arg)
861 option_t *opt, *mainopt;
862 void (*printer) __P((void *, char *, ...));
868 if (opt->flags & OPT_NOPRINT)
872 v = opt->flags & OPT_VALUE;
873 if (*(bool *)opt->addr != v)
874 /* this can happen legitimately, e.g. lock
875 option turned off for default device */
877 printer(arg, "%s", opt->name);
880 v = opt->flags & OPT_VALUE;
883 i = *(int *)opt->addr;
884 if (opt->flags & OPT_NOARG) {
885 printer(arg, "%s", opt->name);
887 if (opt->flags & OPT_INC) {
888 for (; i > v; i -= v)
889 printer(arg, " %s", opt->name);
891 printer(arg, " # oops: %d not %d\n",
895 printer(arg, "%s %d", opt->name, i);
899 printer(arg, "%s", opt->name);
900 if ((opt->flags & OPT_NOARG) == 0)
901 printer(arg, " %x", *(u_int32_t *)opt->addr);
905 if (opt->flags & OPT_HIDE) {
908 p = (char *) opt->addr;
909 if ((opt->flags & OPT_STATIC) == 0)
912 printer(arg, "%s %q", opt->name, p);
916 case o_special_noarg:
918 if (opt->type != o_wild) {
919 printer(arg, "%s", opt->name);
920 if (n_arguments(opt) == 0)
924 if (opt->flags & OPT_A2PRINTER) {
925 void (*oprt) __P((option_t *,
926 void ((*)__P((void *, char *, ...))),
928 oprt = (void (*) __P((option_t *,
929 void ((*)__P((void *, char *, ...))),
931 (*oprt)(opt, printer, arg);
932 } else if (opt->flags & OPT_A2STRVAL) {
933 p = (char *) opt->addr2;
934 if ((opt->flags & OPT_STATIC) == 0)
937 } else if (opt->flags & OPT_A2LIST) {
938 struct option_value *ovp;
940 ovp = (struct option_value *) opt->addr2;
942 printer(arg, "%q", ovp->value);
943 if ((ovp = ovp->next) == NULL)
945 printer(arg, "\t\t# (from %s)\n%s ",
946 ovp->source, opt->name);
949 printer(arg, "xxx # [don't know how to print value]");
954 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
957 printer(arg, "\t\t# (from %s)\n", mainopt->source);
961 * print_option_list - print out options in effect from an
965 print_option_list(opt, printer, arg)
967 void (*printer) __P((void *, char *, ...));
970 while (opt->name != NULL) {
971 if (opt->priority != OPRIO_DEFAULT
972 && opt->winner != (short int) -1)
973 print_option(opt + opt->winner, opt, printer, arg);
976 } while (opt->flags & OPT_PRIOSUB);
981 * print_options - print out what options are in effect.
984 print_options(printer, arg)
985 void (*printer) __P((void *, char *, ...));
988 struct option_list *list;
991 printer(arg, "pppd options in effect:\n");
992 print_option_list(general_options, printer, arg);
993 print_option_list(auth_options, printer, arg);
994 for (list = extra_options; list != NULL; list = list->next)
995 print_option_list(list->options, printer, arg);
996 print_option_list(the_channel->options, printer, arg);
997 for (i = 0; protocols[i] != NULL; ++i)
998 print_option_list(protocols[i]->options, printer, arg);
1002 * usage - print out a message telling how to use the program.
1007 if (phase == PHASE_INITIALIZE)
1008 fprintf(stderr, usage_string, VERSION, progname);
1012 * showhelp - print out usage message and exit.
1018 if (phase == PHASE_INITIALIZE) {
1026 * showversion - print out the version number and exit.
1032 if (phase == PHASE_INITIALIZE) {
1033 fprintf(stderr, "pppd version %s\n", VERSION);
1040 * option_error - print a message about an error in an option.
1041 * The message is logged, and also sent to
1042 * stderr if phase == PHASE_INITIALIZE.
1045 option_error __V((char *fmt, ...))
1050 #if defined(__STDC__)
1051 va_start(args, fmt);
1055 fmt = va_arg(args, char *);
1057 vslprintf(buf, sizeof(buf), fmt, args);
1059 if (phase == PHASE_INITIALIZE)
1060 fprintf(stderr, "%s: %s\n", progname, buf);
1061 syslog(LOG_ERR, "%s", buf);
1066 * readable - check if a file is readable by the real user.
1079 if (fstat(fd, &sbuf) != 0)
1081 if (sbuf.st_uid == uid)
1082 return sbuf.st_mode & S_IRUSR;
1083 if (sbuf.st_gid == getgid())
1084 return sbuf.st_mode & S_IRGRP;
1085 for (i = 0; i < ngroups; ++i)
1086 if (sbuf.st_gid == groups[i])
1087 return sbuf.st_mode & S_IRGRP;
1088 return sbuf.st_mode & S_IROTH;
1093 * Read a word from a file.
1094 * Words are delimited by white-space or by quotes (" or ').
1095 * Quotes, white-space and \ may be escaped with \.
1096 * \<newline> is ignored.
1099 getword(f, word, newlinep, filename)
1106 int quoted, comment;
1107 int value, digit, got, n;
1109 #define isoctal(c) ((c) >= '0' && (c) < '8')
1117 * First skip white-space and comments.
1125 * A newline means the end of a comment; backslash-newline
1126 * is ignored. Note that we cannot have escape && comment.
1138 * Ignore characters other than newline in a comment.
1144 * If this character is escaped, we have a word start.
1150 * If this is the escape character, look at the next character.
1158 * If this is the start of a comment, ignore the rest of the line.
1166 * A non-whitespace character is the start of a word.
1173 * Save the delimiter for quoted strings.
1175 if (!escape && (c == '"' || c == '\'')) {
1182 * Process characters until the end of the word.
1187 * This character is escaped: backslash-newline is ignored,
1188 * various other characters indicate particular values
1189 * as for C backslash-escapes.
1224 * \ddd octal sequence
1227 for (n = 0; n < 3 && isoctal(c); ++n) {
1228 value = (value << 3) + (c & 07);
1237 * \x<hex_string> sequence
1241 for (n = 0; n < 2 && isxdigit(c); ++n) {
1242 digit = toupper(c) - '0';
1244 digit += '0' + 10 - 'A';
1245 value = (value << 4) + digit;
1253 * Otherwise the character stands for itself.
1260 * Store the resulting character for the escape sequence.
1262 if (len < MAXWORDLEN-1)
1273 * Not escaped: see if we've reached the end of the word.
1279 if (isspace(c) || c == '#') {
1286 * Backslash starts an escape sequence.
1295 * An ordinary character: store it in the word and get another.
1297 if (len < MAXWORDLEN-1)
1305 * End of the word: check for errors.
1311 option_error("Error reading %s: %m", filename);
1315 * If len is zero, then we didn't find a word before the
1323 * Warn if the word was too long, and append a terminating null.
1325 if (len >= MAXWORDLEN) {
1326 option_error("warning: word in file %s too long (%.20s...)",
1328 len = MAXWORDLEN - 1;
1339 * number_option - parse an unsigned numeric parameter for an option.
1342 number_option(str, valp, base)
1349 *valp = strtoul(str, &ptr, base);
1351 option_error("invalid numeric parameter '%s' for %s option",
1352 str, current_option);
1360 * int_option - like number_option, but valp is int *,
1361 * the base is assumed to be 0, and *valp is not changed
1362 * if there is an error.
1365 int_option(str, valp)
1371 if (!number_option(str, &v, 0))
1379 * The following procedures parse options.
1383 * readfile - take commands from a file.
1389 return options_from_file(*argv, 1, 1, privileged_option);
1393 * callfile - take commands from /etc/ppp/peers/<name>.
1394 * Name may not contain /../, start with / or ../, or end in /..
1400 char *fname, *arg, *p;
1405 if (arg[0] == '/' || arg[0] == 0)
1408 for (p = arg; *p != 0; ) {
1409 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1413 while (*p != '/' && *p != 0)
1420 option_error("call option value may not contain .. or start with /");
1424 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1425 if ((fname = (char *) malloc(l)) == NULL)
1426 novm("call file name");
1427 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1429 ok = options_from_file(fname, 1, 1, 1);
1437 * setpassfilter - Set the pass filter for packets
1446 pc = pcap_open_dead(DLT_PPP_WITH_DIRECTION, 65535);
1447 if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1448 option_error("error in pass-filter expression: %s\n",
1458 * setactivefilter - Set the active filter for packets
1461 setactivefilter(argv)
1467 pc = pcap_open_dead(DLT_PPP_WITH_DIRECTION, 65535);
1468 if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1469 option_error("error in active-filter expression: %s\n",
1480 * setdomain - Set domain name to append to hostname
1486 gethostname(hostname, MAXNAMELEN);
1489 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1490 domain = hostname + strlen(hostname);
1491 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1493 hostname[MAXNAMELEN-1] = 0;
1503 if (!privileged_option)
1505 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1506 if (fd < 0 && errno == EEXIST)
1507 fd = open(*argv, O_WRONLY | O_APPEND);
1509 if (!privileged_option)
1513 option_error("Can't open log file %s: %m", *argv);
1516 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1517 if (logfile_fd >= 0)
1532 if(!strcmp(*argv,"in")) {
1533 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1534 } else if (!strcmp(*argv,"out")) {
1535 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1536 } else if (!strcmp(*argv,"max")) {
1537 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1539 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1553 void (*init) __P((void));
1557 if (strchr(arg, '/') == 0) {
1558 const char *base = _PATH_PLUGIN;
1559 int l = strlen(base) + strlen(arg) + 2;
1562 novm("plugin file path");
1563 strlcpy(path, base, l);
1564 strlcat(path, "/", l);
1565 strlcat(path, arg, l);
1567 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1571 option_error("%s", err);
1572 option_error("Couldn't load plugin %s", arg);
1575 init = (void (*)(void))dlsym(handle, "plugin_init");
1577 option_error("%s has no initialization entry point", arg);
1580 vers = (const char *) dlsym(handle, "pppd_version");
1582 warn("Warning: plugin %s has no version information", arg);
1583 } else if (strcmp(vers, VERSION) != 0) {
1584 option_error("Plugin %s is for pppd version %s, this is %s",
1585 arg, vers, VERSION);
1588 info("Plugin %s loaded.", arg);