2 * options.c - handles option processing for PPP.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #define RCSID "$Id: options.c,v 1.89 2002/10/27 13:00:13 fcusack Exp $"
36 #include <pcap-int.h> /* XXX: To get struct pcap */
40 #include "pathnames.h"
42 #if defined(ultrix) || defined(NeXT)
43 char *strdup __P((char *));
46 static const char rcsid[] = RCSID;
49 struct option_value *next;
55 * Option variables and default values.
58 int dflag = 0; /* Tell libpcap we want debugging */
60 int debug = 0; /* Debug flag */
61 int kdebugflag = 0; /* Tell kernel to print debug messages */
62 int default_device = 1; /* Using /dev/tty or equivalent */
63 char devnam[MAXPATHLEN]; /* Device name */
64 bool nodetach = 0; /* Don't detach from controlling tty */
65 bool updetach = 0; /* Detach once link is up */
66 int maxconnect = 0; /* Maximum connect time */
67 char user[MAXNAMELEN]; /* Username for PAP */
68 char passwd[MAXSECRETLEN]; /* Password for PAP */
69 bool persist = 0; /* Reopen link after it goes down */
70 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
71 bool demand = 0; /* do dial-on-demand */
72 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
73 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
74 int holdoff = 30; /* # seconds to pause before reconnecting */
75 bool holdoff_specified; /* true if a holdoff value has been given */
76 int log_to_fd = 1; /* send log messages to this fd too */
77 bool log_default = 1; /* log_to_fd is default (stdout) */
78 int maxfail = 10; /* max # of unsuccessful connection attempts */
79 char linkname[MAXPATHLEN]; /* logical name for link */
80 bool tune_kernel; /* may alter kernel settings */
81 int connect_delay = 1000; /* wait this many ms after connect script */
82 int req_unit = -1; /* requested interface unit */
83 bool multilink = 0; /* Enable multilink operation */
84 char *bundle_name = NULL; /* bundle name for multilink */
85 bool dump_options; /* print out option values */
86 bool dryrun; /* print out option values and exit */
87 char *domain; /* domain name set by domain option */
90 unsigned int maxoctets = 0; /* default - no limit */
91 int maxoctets_dir = 0; /* default - sum of traffic */
92 int maxoctets_timeout = 1; /* default 1 second */
96 extern option_t auth_options[];
97 extern struct stat devstat;
100 struct bpf_program pass_filter;/* Filter program for packets to pass */
101 struct bpf_program active_filter; /* Filter program for link-active pkts */
102 pcap_t pc; /* Fake struct pcap so we can compile expr */
105 char *current_option; /* the name of the option being parsed */
106 int privileged_option; /* set iff the current option came from root */
107 char *option_source; /* string saying where the option came from */
108 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
109 bool devnam_fixed; /* can no longer change device name */
111 static int logfile_fd = -1; /* fd opened for log file */
112 static char logfile_name[MAXPATHLEN]; /* name of log file */
117 static int setdomain __P((char **));
118 static int readfile __P((char **));
119 static int callfile __P((char **));
120 static int showversion __P((char **));
121 static int showhelp __P((char **));
122 static void usage __P((void));
123 static int setlogfile __P((char **));
125 static int loadplugin __P((char **));
129 static int setpassfilter __P((char **));
130 static int setactivefilter __P((char **));
134 static int setmodir __P((char **));
137 static option_t *find_option __P((const char *name));
138 static int process_option __P((option_t *, char *, char **));
139 static int n_arguments __P((option_t *));
140 static int number_option __P((char *, u_int32_t *, int));
143 * Structure to store extra lists of options.
147 struct option_list *next;
150 static struct option_list *extra_options = NULL;
155 option_t general_options[] = {
156 { "debug", o_int, &debug,
157 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
158 { "-d", o_int, &debug,
159 "Increase debugging level",
160 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
162 { "kdebug", o_int, &kdebugflag,
163 "Set kernel driver debug level", OPT_PRIO },
165 { "nodetach", o_bool, &nodetach,
166 "Don't detach from controlling tty", OPT_PRIO | 1 },
167 { "-detach", o_bool, &nodetach,
168 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
169 { "updetach", o_bool, &updetach,
170 "Detach from controlling tty once link is up",
171 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
173 { "holdoff", o_int, &holdoff,
174 "Set time in seconds before retrying connection", OPT_PRIO },
176 { "idle", o_int, &idle_time_limit,
177 "Set time in seconds before disconnecting idle link", OPT_PRIO },
179 { "maxconnect", o_int, &maxconnect,
180 "Set connection time limit",
181 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
183 { "domain", o_special, (void *)setdomain,
184 "Add given domain name to hostname",
185 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
187 { "file", o_special, (void *)readfile,
188 "Take options from a file", OPT_NOPRINT },
189 { "call", o_special, (void *)callfile,
190 "Take options from a privileged file", OPT_NOPRINT },
192 { "persist", o_bool, &persist,
193 "Keep on reopening connection after close", OPT_PRIO | 1 },
194 { "nopersist", o_bool, &persist,
195 "Turn off persist option", OPT_PRIOSUB },
197 { "demand", o_bool, &demand,
198 "Dial on demand", OPT_INITONLY | 1, &persist },
200 { "--version", o_special_noarg, (void *)showversion,
201 "Show version number" },
202 { "--help", o_special_noarg, (void *)showhelp,
203 "Show brief listing of options" },
204 { "-h", o_special_noarg, (void *)showhelp,
205 "Show brief listing of options", OPT_ALIAS },
207 { "logfile", o_special, (void *)setlogfile,
208 "Append log messages to this file",
209 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
210 { "logfd", o_int, &log_to_fd,
211 "Send log messages to this file descriptor",
212 OPT_PRIOSUB | OPT_A2CLR, &log_default },
213 { "nolog", o_int, &log_to_fd,
214 "Don't send log messages to any file",
215 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
216 { "nologfd", o_int, &log_to_fd,
217 "Don't send log messages to any file descriptor",
218 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
220 { "linkname", o_string, linkname,
221 "Set logical name for link",
222 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
224 { "maxfail", o_int, &maxfail,
225 "Maximum number of unsuccessful connection attempts to allow",
228 { "ktune", o_bool, &tune_kernel,
229 "Alter kernel settings as necessary", OPT_PRIO | 1 },
230 { "noktune", o_bool, &tune_kernel,
231 "Don't alter kernel settings", OPT_PRIOSUB },
233 { "connect-delay", o_int, &connect_delay,
234 "Maximum time (in ms) to wait after connect script finishes",
237 { "unit", o_int, &req_unit,
238 "PPP interface unit number to use if possible",
239 OPT_PRIO | OPT_LLIMIT, 0, 0 },
241 { "dump", o_bool, &dump_options,
242 "Print out option values after parsing all options", 1 },
243 { "dryrun", o_bool, &dryrun,
244 "Stop after parsing, printing, and checking options", 1 },
246 #ifdef HAVE_MULTILINK
247 { "multilink", o_bool, &multilink,
248 "Enable multilink operation", OPT_PRIO | 1 },
249 { "mp", o_bool, &multilink,
250 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
251 { "nomultilink", o_bool, &multilink,
252 "Disable multilink operation", OPT_PRIOSUB | 0 },
253 { "nomp", o_bool, &multilink,
254 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
256 { "bundle", o_string, &bundle_name,
257 "Bundle name for multilink", OPT_PRIO },
258 #endif /* HAVE_MULTILINK */
261 { "plugin", o_special, (void *)loadplugin,
262 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
266 { "pdebug", o_int, &dflag,
267 "libpcap debugging", OPT_PRIO },
269 { "pass-filter", 1, setpassfilter,
270 "set filter for packets to pass", OPT_PRIO },
272 { "active-filter", 1, setactivefilter,
273 "set filter for active pkts", OPT_PRIO },
277 { "maxoctets", o_int, &maxoctets,
278 "Set connection traffic limit",
279 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
280 { "mo", o_int, &maxoctets,
281 "Set connection traffic limit",
282 OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
283 { "mo-direction", o_special, setmodir,
284 "Set direction for limit traffic (sum,in,out,max)" },
285 { "mo-timeout", o_int, &maxoctets_timeout,
286 "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
292 #ifndef IMPLEMENTATION
293 #define IMPLEMENTATION ""
296 static char *usage_string = "\
298 Usage: %s [ options ], where options are:\n\
299 <device> Communicate over the named device\n\
300 <speed> Set the baud rate to <speed>\n\
301 <loc>:<rem> Set the local and/or remote interface IP\n\
302 addresses. Either one may be omitted.\n\
303 asyncmap <n> Set the desired async map to hex <n>\n\
304 auth Require authentication from peer\n\
305 connect <p> Invoke shell command <p> to set up the serial line\n\
306 crtscts Use hardware RTS/CTS flow control\n\
307 defaultroute Add default route through interface\n\
308 file <f> Take options from file <f>\n\
309 modem Use modem control lines\n\
310 mru <n> Set MRU value to <n> for negotiation\n\
311 See pppd(8) for more options.\n\
315 * parse_args - parse a string of arguments from the command line.
318 parse_args(argc, argv)
326 privileged_option = privileged;
327 option_source = "command line";
328 option_priority = OPRIO_CMDLINE;
332 opt = find_option(arg);
334 option_error("unrecognized option '%s'", arg);
338 n = n_arguments(opt);
340 option_error("too few parameters for option %s", arg);
343 if (!process_option(opt, arg, argv))
352 * options_from_file - Read a string of options from a file,
353 * and interpret them.
356 options_from_file(filename, must_exist, check_prot, priv)
363 int i, newline, ret, err;
368 char args[MAXARGS][MAXWORDLEN];
369 char cmd[MAXWORDLEN];
373 f = fopen(filename, "r");
380 if (err != ENOENT && err != ENOTDIR)
381 warn("Warning: can't open options file %s: %m", filename);
384 option_error("Can't open options file %s: %m", filename);
388 oldpriv = privileged_option;
389 privileged_option = priv;
390 oldsource = option_source;
391 option_source = strdup(filename);
392 if (option_source == NULL)
393 option_source = "file";
395 while (getword(f, cmd, &newline, filename)) {
396 opt = find_option(cmd);
398 option_error("In file %s: unrecognized option '%s'",
402 n = n_arguments(opt);
403 for (i = 0; i < n; ++i) {
404 if (!getword(f, args[i], &newline, filename)) {
406 "In file %s: too few parameters for option '%s'",
412 if (!process_option(opt, cmd, argv))
419 privileged_option = oldpriv;
420 option_source = oldsource;
425 * options_from_user - See if the use has a ~/.ppprc file,
426 * and if so, interpret options from it.
431 char *user, *path, *file;
436 pw = getpwuid(getuid());
437 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
439 file = _PATH_USEROPT;
440 pl = strlen(user) + strlen(file) + 2;
443 novm("init file name");
444 slprintf(path, pl, "%s/%s", user, file);
445 option_priority = OPRIO_CFGFILE;
446 ret = options_from_file(path, 0, 1, privileged);
452 * options_for_tty - See if an options file exists for the serial
453 * device, and if so, interpret options from it.
454 * We only allow the per-tty options file to override anything from
455 * the command line if it is something that the user can't override
456 * once it has been set by root; this is done by giving configuration
457 * files a lower priority than the command line.
462 char *dev, *path, *p;
467 if (strncmp(dev, "/dev/", 5) == 0)
469 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
470 return 1; /* don't look for /etc/ppp/options.tty */
471 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
474 novm("tty init file name");
475 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
476 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
477 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
480 option_priority = OPRIO_CFGFILE;
481 ret = options_from_file(path, 0, 0, 1);
487 * options_from_list - process a string of options in a wordlist.
490 options_from_list(w, priv)
499 privileged_option = priv;
500 option_source = "secrets file";
501 option_priority = OPRIO_SECFILE;
504 opt = find_option(w->word);
506 option_error("In secrets file: unrecognized option '%s'",
510 n = n_arguments(opt);
512 for (i = 0; i < n; ++i) {
516 "In secrets file: too few parameters for option '%s'",
522 if (!process_option(opt, w0->word, argv))
533 * match_option - see if this option matches an option_t structure.
536 match_option(name, opt, dowild)
541 int (*match) __P((char *, char **, int));
543 if (dowild != (opt->type == o_wild))
546 return strcmp(name, opt->name) == 0;
547 match = (int (*) __P((char *, char **, int))) opt->addr;
548 return (*match)(name, NULL, 0);
552 * find_option - scan the option lists for the various protocols
553 * looking for an entry with the given name.
554 * This could be optimized by using a hash table.
561 struct option_list *list;
564 for (dowild = 0; dowild <= 1; ++dowild) {
565 for (opt = general_options; opt->name != NULL; ++opt)
566 if (match_option(name, opt, dowild))
568 for (opt = auth_options; opt->name != NULL; ++opt)
569 if (match_option(name, opt, dowild))
571 for (list = extra_options; list != NULL; list = list->next)
572 for (opt = list->options; opt->name != NULL; ++opt)
573 if (match_option(name, opt, dowild))
575 for (opt = the_channel->options; opt->name != NULL; ++opt)
576 if (match_option(name, opt, dowild))
578 for (i = 0; protocols[i] != NULL; ++i)
579 if ((opt = protocols[i]->options) != NULL)
580 for (; opt->name != NULL; ++opt)
581 if (match_option(name, opt, dowild))
588 * process_option - process one new-style option.
591 process_option(opt, cmd, argv)
599 int (*parser) __P((char **));
600 int (*wildp) __P((char *, char **, int));
601 char *optopt = (opt->type == o_wild)? "": " option";
602 int prio = option_priority;
603 option_t *mainopt = opt;
605 current_option = opt->name;
606 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
608 while (mainopt->flags & OPT_PRIOSUB)
610 if (mainopt->flags & OPT_PRIO) {
611 if (prio < mainopt->priority) {
612 /* new value doesn't override old */
613 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
614 option_error("%s%s set in %s cannot be overridden\n",
615 opt->name, optopt, mainopt->source);
620 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
621 warn("%s%s from %s overrides command line",
622 opt->name, optopt, option_source);
625 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
626 option_error("%s%s cannot be changed after initialization",
630 if ((opt->flags & OPT_PRIV) && !privileged_option) {
631 option_error("using the %s%s requires root privilege",
635 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
636 option_error("%s%s is disabled", opt->name, optopt);
639 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
640 option_error("the %s%s may not be changed in %s",
641 opt->name, optopt, option_source);
647 v = opt->flags & OPT_VALUE;
648 *(bool *)(opt->addr) = v;
649 if (opt->addr2 && (opt->flags & OPT_A2COPY))
650 *(bool *)(opt->addr2) = v;
651 else if (opt->addr2 && (opt->flags & OPT_A2CLR))
652 *(bool *)(opt->addr2) = 0;
653 else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
654 *(u_char *)(opt->addr2) &= ~v;
655 else if (opt->addr2 && (opt->flags & OPT_A2OR))
656 *(u_char *)(opt->addr2) |= v;
661 if ((opt->flags & OPT_NOARG) == 0) {
662 if (!int_option(*argv, &iv))
664 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
665 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
666 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
667 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
668 switch (opt->flags & OPT_LIMITS) {
670 option_error("%s value must be%s >= %d",
671 opt->name, zok, opt->lower_limit);
674 option_error("%s value must be%s <= %d",
675 opt->name, zok, opt->upper_limit);
678 option_error("%s value must be%s between %d and %d",
679 opt->name, zok, opt->lower_limit, opt->upper_limit);
685 a = opt->flags & OPT_VALUE;
687 a -= 256; /* sign extend */
689 if (opt->flags & OPT_INC)
690 iv += *(int *)(opt->addr);
691 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
692 int oldv = *(int *)(opt->addr);
693 if ((opt->flags & OPT_ZEROINF) ?
694 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
695 option_error("%s value cannot be increased", opt->name);
699 *(int *)(opt->addr) = iv;
700 if (opt->addr2 && (opt->flags & OPT_A2COPY))
701 *(int *)(opt->addr2) = iv;
705 if (opt->flags & OPT_NOARG) {
706 v = opt->flags & OPT_VALUE;
709 } else if (!number_option(*argv, &v, 16))
711 if (opt->flags & OPT_OR)
712 v |= *(u_int32_t *)(opt->addr);
713 *(u_int32_t *)(opt->addr) = v;
714 if (opt->addr2 && (opt->flags & OPT_A2COPY))
715 *(u_int32_t *)(opt->addr2) = v;
719 if (opt->flags & OPT_STATIC) {
720 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
724 novm("option argument");
725 *(char **)(opt->addr) = sv;
729 case o_special_noarg:
731 parser = (int (*) __P((char **))) opt->addr;
732 if (!(*parser)(argv))
734 if (opt->flags & OPT_A2LIST) {
735 struct option_value *ovp, **pp;
737 ovp = malloc(sizeof(*ovp) + strlen(*argv));
739 strcpy(ovp->value, *argv);
740 ovp->source = option_source;
742 pp = (struct option_value **) &opt->addr2;
751 wildp = (int (*) __P((char *, char **, int))) opt->addr;
752 if (!(*wildp)(cmd, argv, 1))
757 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
758 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
759 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
761 mainopt->source = option_source;
762 mainopt->priority = prio;
763 mainopt->winner = opt - mainopt;
769 * override_value - if the option priorities would permit us to
770 * override the value of option, return 1 and update the priority
771 * and source of the option value. Otherwise returns 0.
774 override_value(option, priority, source)
781 opt = find_option(option);
784 while (opt->flags & OPT_PRIOSUB)
786 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
788 opt->priority = priority;
789 opt->source = source;
795 * n_arguments - tell how many arguments an option takes
801 return (opt->type == o_bool || opt->type == o_special_noarg
802 || (opt->flags & OPT_NOARG))? 0: 1;
806 * add_options - add a list of options to the set we grok.
812 struct option_list *list;
814 list = malloc(sizeof(*list));
816 novm("option list entry");
818 list->next = extra_options;
819 extra_options = list;
823 * check_options - check that options are valid and consistent.
828 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
833 * print_option - print out an option and its value
836 print_option(opt, mainopt, printer, arg)
837 option_t *opt, *mainopt;
838 void (*printer) __P((void *, char *, ...));
844 if (opt->flags & OPT_NOPRINT)
848 v = opt->flags & OPT_VALUE;
849 if (*(bool *)opt->addr != v)
850 /* this can happen legitimately, e.g. lock
851 option turned off for default device */
853 printer(arg, "%s", opt->name);
856 v = opt->flags & OPT_VALUE;
859 i = *(int *)opt->addr;
860 if (opt->flags & OPT_NOARG) {
861 printer(arg, "%s", opt->name);
863 if (opt->flags & OPT_INC) {
864 for (; i > v; i -= v)
865 printer(arg, " %s", opt->name);
867 printer(arg, " # oops: %d not %d\n",
871 printer(arg, "%s %d", opt->name, i);
875 printer(arg, "%s", opt->name);
876 if ((opt->flags & OPT_NOARG) == 0)
877 printer(arg, " %x", *(u_int32_t *)opt->addr);
881 if (opt->flags & OPT_HIDE) {
884 p = (char *) opt->addr;
885 if ((opt->flags & OPT_STATIC) == 0)
888 printer(arg, "%s %q", opt->name, p);
892 case o_special_noarg:
894 if (opt->type != o_wild) {
895 printer(arg, "%s", opt->name);
896 if (n_arguments(opt) == 0)
900 if (opt->flags & OPT_A2PRINTER) {
901 void (*oprt) __P((option_t *,
902 void ((*)__P((void *, char *, ...))),
904 oprt = (void (*) __P((option_t *,
905 void ((*)__P((void *, char *, ...))),
907 (*oprt)(opt, printer, arg);
908 } else if (opt->flags & OPT_A2STRVAL) {
909 p = (char *) opt->addr2;
910 if ((opt->flags & OPT_STATIC) == 0)
913 } else if (opt->flags & OPT_A2LIST) {
914 struct option_value *ovp;
916 ovp = (struct option_value *) opt->addr2;
918 printer(arg, "%q", ovp->value);
919 if ((ovp = ovp->next) == NULL)
921 printer(arg, "\t\t# (from %s)\n%s ",
922 ovp->source, opt->name);
925 printer(arg, "xxx # [don't know how to print value]");
930 printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
933 printer(arg, "\t\t# (from %s)\n", mainopt->source);
937 * print_option_list - print out options in effect from an
941 print_option_list(opt, printer, arg)
943 void (*printer) __P((void *, char *, ...));
946 while (opt->name != NULL) {
947 if (opt->priority != OPRIO_DEFAULT
948 && opt->winner != (short int) -1)
949 print_option(opt + opt->winner, opt, printer, arg);
952 } while (opt->flags & OPT_PRIOSUB);
957 * print_options - print out what options are in effect.
960 print_options(printer, arg)
961 void (*printer) __P((void *, char *, ...));
964 struct option_list *list;
967 printer(arg, "pppd options in effect:\n");
968 print_option_list(general_options, printer, arg);
969 print_option_list(auth_options, printer, arg);
970 for (list = extra_options; list != NULL; list = list->next)
971 print_option_list(list->options, printer, arg);
972 print_option_list(the_channel->options, printer, arg);
973 for (i = 0; protocols[i] != NULL; ++i)
974 print_option_list(protocols[i]->options, printer, arg);
978 * usage - print out a message telling how to use the program.
983 if (phase == PHASE_INITIALIZE)
984 fprintf(stderr, usage_string, VERSION, progname);
988 * showhelp - print out usage message and exit.
994 if (phase == PHASE_INITIALIZE) {
1002 * showversion - print out the version number and exit.
1008 if (phase == PHASE_INITIALIZE) {
1009 fprintf(stderr, "pppd version %s\n", VERSION);
1016 * option_error - print a message about an error in an option.
1017 * The message is logged, and also sent to
1018 * stderr if phase == PHASE_INITIALIZE.
1021 option_error __V((char *fmt, ...))
1026 #if defined(__STDC__)
1027 va_start(args, fmt);
1031 fmt = va_arg(args, char *);
1033 vslprintf(buf, sizeof(buf), fmt, args);
1035 if (phase == PHASE_INITIALIZE)
1036 fprintf(stderr, "%s: %s\n", progname, buf);
1037 syslog(LOG_ERR, "%s", buf);
1042 * readable - check if a file is readable by the real user.
1055 if (fstat(fd, &sbuf) != 0)
1057 if (sbuf.st_uid == uid)
1058 return sbuf.st_mode & S_IRUSR;
1059 if (sbuf.st_gid == getgid())
1060 return sbuf.st_mode & S_IRGRP;
1061 for (i = 0; i < ngroups; ++i)
1062 if (sbuf.st_gid == groups[i])
1063 return sbuf.st_mode & S_IRGRP;
1064 return sbuf.st_mode & S_IROTH;
1069 * Read a word from a file.
1070 * Words are delimited by white-space or by quotes (" or ').
1071 * Quotes, white-space and \ may be escaped with \.
1072 * \<newline> is ignored.
1075 getword(f, word, newlinep, filename)
1082 int quoted, comment;
1083 int value, digit, got, n;
1085 #define isoctal(c) ((c) >= '0' && (c) < '8')
1093 * First skip white-space and comments.
1101 * A newline means the end of a comment; backslash-newline
1102 * is ignored. Note that we cannot have escape && comment.
1114 * Ignore characters other than newline in a comment.
1120 * If this character is escaped, we have a word start.
1126 * If this is the escape character, look at the next character.
1134 * If this is the start of a comment, ignore the rest of the line.
1142 * A non-whitespace character is the start of a word.
1149 * Save the delimiter for quoted strings.
1151 if (!escape && (c == '"' || c == '\'')) {
1158 * Process characters until the end of the word.
1163 * This character is escaped: backslash-newline is ignored,
1164 * various other characters indicate particular values
1165 * as for C backslash-escapes.
1200 * \ddd octal sequence
1203 for (n = 0; n < 3 && isoctal(c); ++n) {
1204 value = (value << 3) + (c & 07);
1213 * \x<hex_string> sequence
1217 for (n = 0; n < 2 && isxdigit(c); ++n) {
1218 digit = toupper(c) - '0';
1220 digit += '0' + 10 - 'A';
1221 value = (value << 4) + digit;
1229 * Otherwise the character stands for itself.
1236 * Store the resulting character for the escape sequence.
1238 if (len < MAXWORDLEN-1)
1249 * Not escaped: see if we've reached the end of the word.
1255 if (isspace(c) || c == '#') {
1262 * Backslash starts an escape sequence.
1271 * An ordinary character: store it in the word and get another.
1273 if (len < MAXWORDLEN-1)
1281 * End of the word: check for errors.
1287 option_error("Error reading %s: %m", filename);
1291 * If len is zero, then we didn't find a word before the
1299 * Warn if the word was too long, and append a terminating null.
1301 if (len >= MAXWORDLEN) {
1302 option_error("warning: word in file %s too long (%.20s...)",
1304 len = MAXWORDLEN - 1;
1315 * number_option - parse an unsigned numeric parameter for an option.
1318 number_option(str, valp, base)
1325 *valp = strtoul(str, &ptr, base);
1327 option_error("invalid numeric parameter '%s' for %s option",
1328 str, current_option);
1336 * int_option - like number_option, but valp is int *,
1337 * the base is assumed to be 0, and *valp is not changed
1338 * if there is an error.
1341 int_option(str, valp)
1347 if (!number_option(str, &v, 0))
1355 * The following procedures parse options.
1359 * readfile - take commands from a file.
1365 return options_from_file(*argv, 1, 1, privileged_option);
1369 * callfile - take commands from /etc/ppp/peers/<name>.
1370 * Name may not contain /../, start with / or ../, or end in /..
1376 char *fname, *arg, *p;
1381 if (arg[0] == '/' || arg[0] == 0)
1384 for (p = arg; *p != 0; ) {
1385 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1389 while (*p != '/' && *p != 0)
1396 option_error("call option value may not contain .. or start with /");
1400 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1401 if ((fname = (char *) malloc(l)) == NULL)
1402 novm("call file name");
1403 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1405 ok = options_from_file(fname, 1, 1, 1);
1413 * setpassfilter - Set the pass filter for packets
1419 pc.linktype = DLT_PPP;
1420 pc.snapshot = PPP_HDRLEN;
1422 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1424 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1429 * setactivefilter - Set the active filter for packets
1432 setactivefilter(argv)
1435 pc.linktype = DLT_PPP;
1436 pc.snapshot = PPP_HDRLEN;
1438 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1440 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1446 * setdomain - Set domain name to append to hostname
1452 gethostname(hostname, MAXNAMELEN);
1455 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1456 domain = hostname + strlen(hostname);
1457 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1459 hostname[MAXNAMELEN-1] = 0;
1470 if (!privileged_option)
1472 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1473 if (fd < 0 && errno == EEXIST)
1474 fd = open(*argv, O_WRONLY | O_APPEND);
1476 if (!privileged_option)
1480 option_error("Can't open log file %s: %m", *argv);
1483 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1484 if (logfile_fd >= 0)
1498 if(!strcmp(*argv,"in")) {
1499 maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1500 } else if (!strcmp(*argv,"out")) {
1501 maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1502 } else if (!strcmp(*argv,"max")) {
1503 maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1505 maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1519 void (*init) __P((void));
1523 if (strchr(arg, '/') == 0) {
1524 const char *base = _PATH_PLUGIN;
1525 int l = strlen(base) + strlen(arg) + 2;
1528 novm("plugin file path");
1529 strlcpy(path, base, l);
1530 strlcat(path, "/", l);
1531 strlcat(path, arg, l);
1533 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1537 option_error("%s", err);
1538 option_error("Couldn't load plugin %s", arg);
1541 init = (void (*)(void))dlsym(handle, "plugin_init");
1543 option_error("%s has no initialization entry point", arg);
1546 vers = (const char *) dlsym(handle, "pppd_version");
1548 warn("Warning: plugin %s has no version information", arg);
1549 } else if (strcmp(vers, VERSION) != 0) {
1550 option_error("Plugin %s is for pppd version %s, this is %s",
1551 arg, vers, VERSION);
1554 info("Plugin %s loaded.", arg);