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.79 2001/03/08 05:11:15 paulus 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 */
89 extern option_t auth_options[];
90 extern struct stat devstat;
93 struct bpf_program pass_filter;/* Filter program for packets to pass */
94 struct bpf_program active_filter; /* Filter program for link-active pkts */
95 pcap_t pc; /* Fake struct pcap so we can compile expr */
98 char *current_option; /* the name of the option being parsed */
99 int privileged_option; /* set iff the current option came from root */
100 char *option_source; /* string saying where the option came from */
101 int option_priority = OPRIO_CFGFILE; /* priority of the current options */
102 bool devnam_fixed; /* can no longer change device name */
104 static int logfile_fd = -1; /* fd opened for log file */
105 static char logfile_name[MAXPATHLEN]; /* name of log file */
110 static int setdomain __P((char **));
111 static int readfile __P((char **));
112 static int callfile __P((char **));
113 static int showversion __P((char **));
114 static int showhelp __P((char **));
115 static void usage __P((void));
116 static int setlogfile __P((char **));
118 static int loadplugin __P((char **));
122 static int setpassfilter __P((char **));
123 static int setactivefilter __P((char **));
126 static option_t *find_option __P((const char *name));
127 static int process_option __P((option_t *, char *, char **));
128 static int n_arguments __P((option_t *));
129 static int number_option __P((char *, u_int32_t *, int));
132 * Structure to store extra lists of options.
136 struct option_list *next;
139 static struct option_list *extra_options = NULL;
144 option_t general_options[] = {
145 { "debug", o_int, &debug,
146 "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
147 { "-d", o_int, &debug,
148 "Increase debugging level",
149 OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
151 { "kdebug", o_int, &kdebugflag,
152 "Set kernel driver debug level", OPT_PRIO },
154 { "nodetach", o_bool, &nodetach,
155 "Don't detach from controlling tty", OPT_PRIO | 1 },
156 { "-detach", o_bool, &nodetach,
157 "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
158 { "updetach", o_bool, &updetach,
159 "Detach from controlling tty once link is up",
160 OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
162 { "holdoff", o_int, &holdoff,
163 "Set time in seconds before retrying connection", OPT_PRIO },
165 { "idle", o_int, &idle_time_limit,
166 "Set time in seconds before disconnecting idle link", OPT_PRIO },
168 { "maxconnect", o_int, &maxconnect,
169 "Set connection time limit",
170 OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
172 { "domain", o_special, (void *)setdomain,
173 "Add given domain name to hostname",
174 OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
176 { "file", o_special, (void *)readfile,
177 "Take options from a file", OPT_NOPRINT },
178 { "call", o_special, (void *)callfile,
179 "Take options from a privileged file", OPT_NOPRINT },
181 { "persist", o_bool, &persist,
182 "Keep on reopening connection after close", OPT_PRIO | 1 },
183 { "nopersist", o_bool, &persist,
184 "Turn off persist option", OPT_PRIOSUB },
186 { "demand", o_bool, &demand,
187 "Dial on demand", OPT_INITONLY | 1, &persist },
189 { "--version", o_special_noarg, (void *)showversion,
190 "Show version number" },
191 { "--help", o_special_noarg, (void *)showhelp,
192 "Show brief listing of options" },
193 { "-h", o_special_noarg, (void *)showhelp,
194 "Show brief listing of options", OPT_ALIAS },
196 { "logfile", o_special, (void *)setlogfile,
197 "Append log messages to this file",
198 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
199 { "logfd", o_int, &log_to_fd,
200 "Send log messages to this file descriptor",
201 OPT_PRIOSUB | OPT_A2CLR, &log_default },
202 { "nolog", o_int, &log_to_fd,
203 "Don't send log messages to any file",
204 OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
205 { "nologfd", o_int, &log_to_fd,
206 "Don't send log messages to any file descriptor",
207 OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
209 { "linkname", o_string, linkname,
210 "Set logical name for link",
211 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
213 { "maxfail", o_int, &maxfail,
214 "Maximum number of unsuccessful connection attempts to allow",
217 { "ktune", o_bool, &tune_kernel,
218 "Alter kernel settings as necessary", OPT_PRIO | 1 },
219 { "noktune", o_bool, &tune_kernel,
220 "Don't alter kernel settings", OPT_PRIOSUB },
222 { "connect-delay", o_int, &connect_delay,
223 "Maximum time (in ms) to wait after connect script finishes",
226 { "unit", o_int, &req_unit,
227 "PPP interface unit number to use if possible",
228 OPT_PRIO | OPT_LLIMIT, 0, 0 },
230 { "dump", o_bool, &dump_options,
231 "Print out option values after parsing all options", 1 },
232 { "dryrun", o_bool, &dryrun,
233 "Stop after parsing, printing, and checking options", 1 },
235 #ifdef HAVE_MULTILINK
236 { "multilink", o_bool, &multilink,
237 "Enable multilink operation", OPT_PRIO | 1 },
238 { "mp", o_bool, &multilink,
239 "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
240 { "nomultilink", o_bool, &multilink,
241 "Disable multilink operation", OPT_PRIOSUB | 0 },
242 { "nomp", o_bool, &multilink,
243 "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
245 { "bundle", o_string, &bundle_name,
246 "Bundle name for multilink", OPT_PRIO },
247 #endif /* HAVE_MULTILINK */
250 { "plugin", o_special, (void *)loadplugin,
251 "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
255 { "pdebug", o_int, &dflag,
256 "libpcap debugging", OPT_PRIO },
258 { "pass-filter", 1, setpassfilter,
259 "set filter for packets to pass", OPT_PRIO },
261 { "active-filter", 1, setactivefilter,
262 "set filter for active pkts", OPT_PRIO },
268 #ifndef IMPLEMENTATION
269 #define IMPLEMENTATION ""
272 static char *usage_string = "\
274 Usage: %s [ options ], where options are:\n\
275 <device> Communicate over the named device\n\
276 <speed> Set the baud rate to <speed>\n\
277 <loc>:<rem> Set the local and/or remote interface IP\n\
278 addresses. Either one may be omitted.\n\
279 asyncmap <n> Set the desired async map to hex <n>\n\
280 auth Require authentication from peer\n\
281 connect <p> Invoke shell command <p> to set up the serial line\n\
282 crtscts Use hardware RTS/CTS flow control\n\
283 defaultroute Add default route through interface\n\
284 file <f> Take options from file <f>\n\
285 modem Use modem control lines\n\
286 mru <n> Set MRU value to <n> for negotiation\n\
287 See pppd(8) for more options.\n\
291 * parse_args - parse a string of arguments from the command line.
294 parse_args(argc, argv)
302 privileged_option = privileged;
303 option_source = "command line";
304 option_priority = OPRIO_CMDLINE;
308 opt = find_option(arg);
310 option_error("unrecognized option '%s'", arg);
314 n = n_arguments(opt);
316 option_error("too few parameters for option %s", arg);
319 if (!process_option(opt, arg, argv))
328 * options_from_file - Read a string of options from a file,
329 * and interpret them.
332 options_from_file(filename, must_exist, check_prot, priv)
339 int i, newline, ret, err;
344 char args[MAXARGS][MAXWORDLEN];
345 char cmd[MAXWORDLEN];
349 f = fopen(filename, "r");
356 if (err != ENOENT && err != ENOTDIR)
357 warn("Warning: can't open options file %s: %m", filename);
360 option_error("Can't open options file %s: %m", filename);
364 oldpriv = privileged_option;
365 privileged_option = priv;
366 oldsource = option_source;
367 option_source = strdup(filename);
368 if (option_source == NULL)
369 option_source = "file";
371 while (getword(f, cmd, &newline, filename)) {
372 opt = find_option(cmd);
374 option_error("In file %s: unrecognized option '%s'",
378 n = n_arguments(opt);
379 for (i = 0; i < n; ++i) {
380 if (!getword(f, args[i], &newline, filename)) {
382 "In file %s: too few parameters for option '%s'",
388 if (!process_option(opt, cmd, argv))
395 privileged_option = oldpriv;
396 option_source = oldsource;
401 * options_from_user - See if the use has a ~/.ppprc file,
402 * and if so, interpret options from it.
407 char *user, *path, *file;
412 pw = getpwuid(getuid());
413 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
415 file = _PATH_USEROPT;
416 pl = strlen(user) + strlen(file) + 2;
419 novm("init file name");
420 slprintf(path, pl, "%s/%s", user, file);
421 option_priority = OPRIO_CFGFILE;
422 ret = options_from_file(path, 0, 1, privileged);
428 * options_for_tty - See if an options file exists for the serial
429 * device, and if so, interpret options from it.
430 * We only allow the per-tty options file to override anything from
431 * the command line if it is something that the user can't override
432 * once it has been set by root; this is done by giving configuration
433 * files a lower priority than the command line.
438 char *dev, *path, *p;
443 if (strncmp(dev, "/dev/", 5) == 0)
445 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
446 return 1; /* don't look for /etc/ppp/options.tty */
447 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
450 novm("tty init file name");
451 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
452 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
453 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
456 option_priority = OPRIO_CFGFILE;
457 ret = options_from_file(path, 0, 0, 1);
463 * options_from_list - process a string of options in a wordlist.
466 options_from_list(w, priv)
475 privileged_option = priv;
476 option_source = "secrets file";
477 option_priority = OPRIO_SECFILE;
480 opt = find_option(w->word);
482 option_error("In secrets file: unrecognized option '%s'",
486 n = n_arguments(opt);
488 for (i = 0; i < n; ++i) {
492 "In secrets file: too few parameters for option '%s'",
498 if (!process_option(opt, w0->word, argv))
508 * match_option - see if this option matches an option_t structure.
511 match_option(name, opt, dowild)
516 int (*match) __P((char *, char **, int));
518 if (dowild != (opt->type == o_wild))
521 return strcmp(name, opt->name) == 0;
522 match = (int (*) __P((char *, char **, int))) opt->addr;
523 return (*match)(name, NULL, 0);
527 * find_option - scan the option lists for the various protocols
528 * looking for an entry with the given name.
529 * This could be optimized by using a hash table.
536 struct option_list *list;
539 for (dowild = 0; dowild <= 1; ++dowild) {
540 for (opt = general_options; opt->name != NULL; ++opt)
541 if (match_option(name, opt, dowild))
543 for (opt = auth_options; opt->name != NULL; ++opt)
544 if (match_option(name, opt, dowild))
546 for (list = extra_options; list != NULL; list = list->next)
547 for (opt = list->options; opt->name != NULL; ++opt)
548 if (match_option(name, opt, dowild))
550 for (opt = the_channel->options; opt->name != NULL; ++opt)
551 if (match_option(name, opt, dowild))
553 for (i = 0; protocols[i] != NULL; ++i)
554 if ((opt = protocols[i]->options) != NULL)
555 for (; opt->name != NULL; ++opt)
556 if (match_option(name, opt, dowild))
563 * process_option - process one new-style option.
566 process_option(opt, cmd, argv)
574 int (*parser) __P((char **));
575 int (*wildp) __P((char *, char **, int));
576 char *optopt = (opt->type == o_wild)? "": " option";
577 int prio = option_priority;
578 option_t *mainopt = opt;
580 if ((opt->flags & OPT_PRIVFIX) && privileged_option)
582 while (mainopt->flags & OPT_PRIOSUB)
584 if (mainopt->flags & OPT_PRIO) {
585 if (prio < mainopt->priority) {
586 /* new value doesn't override old */
587 if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
588 option_error("%s%s set in %s cannot be overridden\n",
589 opt->name, optopt, mainopt->source);
594 if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
595 warn("%s%s from %s overrides command line",
596 opt->name, optopt, option_source);
599 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
600 option_error("%s%s cannot be changed after initialization",
604 if ((opt->flags & OPT_PRIV) && !privileged_option) {
605 option_error("using the %s%s requires root privilege",
609 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
610 option_error("%s%s is disabled", opt->name, optopt);
613 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
614 option_error("the %s%s may not be changed in %s",
615 opt->name, optopt, option_source);
621 v = opt->flags & OPT_VALUE;
622 *(bool *)(opt->addr) = v;
623 if (opt->addr2 && (opt->flags & OPT_A2COPY))
624 *(bool *)(opt->addr2) = v;
629 if ((opt->flags & OPT_NOARG) == 0) {
630 if (!int_option(*argv, &iv))
632 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
633 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
634 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
635 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
636 switch (opt->flags & OPT_LIMITS) {
638 option_error("%s value must be%s >= %d",
639 opt->name, zok, opt->lower_limit);
642 option_error("%s value must be%s <= %d",
643 opt->name, zok, opt->upper_limit);
646 option_error("%s value must be%s between %d and %d",
647 opt->name, opt->lower_limit, opt->upper_limit);
653 a = opt->flags & OPT_VALUE;
655 a -= 256; /* sign extend */
657 if (opt->flags & OPT_INC)
658 iv += *(int *)(opt->addr);
659 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
660 int oldv = *(int *)(opt->addr);
661 if ((opt->flags & OPT_ZEROINF) ?
662 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
663 option_error("%s value cannot be increased", opt->name);
667 *(int *)(opt->addr) = iv;
668 if (opt->addr2 && (opt->flags & OPT_A2COPY))
669 *(int *)(opt->addr2) = iv;
673 if (opt->flags & OPT_NOARG) {
674 v = opt->flags & OPT_VALUE;
677 } else if (!number_option(*argv, &v, 16))
679 if (opt->flags & OPT_OR)
680 v |= *(u_int32_t *)(opt->addr);
681 *(u_int32_t *)(opt->addr) = v;
682 if (opt->addr2 && (opt->flags & OPT_A2COPY))
683 *(u_int32_t *)(opt->addr2) = v;
687 if (opt->flags & OPT_STATIC) {
688 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
692 novm("option argument");
693 *(char **)(opt->addr) = sv;
697 case o_special_noarg:
699 parser = (int (*) __P((char **))) opt->addr;
700 if (!(*parser)(argv))
702 if (opt->flags & OPT_A2LIST) {
703 struct option_value *ovp, **pp;
705 ovp = malloc(sizeof(*ovp) + strlen(*argv));
707 strcpy(ovp->value, *argv);
708 ovp->source = option_source;
710 pp = (struct option_value **) &opt->addr2;
719 wildp = (int (*) __P((char *, char **, int))) opt->addr;
720 if (!(*wildp)(cmd, argv, 1))
725 if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
726 |OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST)) == 0)
727 *(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
729 mainopt->source = option_source;
730 mainopt->priority = prio;
731 mainopt->winner = opt - mainopt;
737 * override_value - if the option priorities would permit us to
738 * override the value of option, return 1 and update the priority
739 * and source of the option value. Otherwise returns 0.
742 override_value(option, priority, source)
749 opt = find_option(option);
752 while (opt->flags & OPT_PRIOSUB)
754 if ((opt->flags & OPT_PRIO) && priority < opt->priority)
756 opt->priority = priority;
757 opt->source = source;
763 * n_arguments - tell how many arguments an option takes
769 return (opt->type == o_bool || opt->type == o_special_noarg
770 || (opt->flags & OPT_NOARG))? 0: 1;
774 * add_options - add a list of options to the set we grok.
780 struct option_list *list;
782 list = malloc(sizeof(*list));
784 novm("option list entry");
786 list->next = extra_options;
787 extra_options = list;
791 * check_options - check that options are valid and consistent.
796 if (logfile_fd >= 0 && logfile_fd != log_to_fd)
801 * print_option - print out an option and its value
804 print_option(opt, mainopt, printer, arg)
805 option_t *opt, *mainopt;
806 void (*printer) __P((void *, char *, ...));
812 if (opt->flags & OPT_NOPRINT)
816 v = opt->flags & OPT_VALUE;
817 if (*(bool *)opt->addr != v)
818 printer(arg, "oops, %s value is %d not %d?\n",
819 opt->name, *(bool *)opt->addr, v);
820 printer(arg, "%s", opt->name);
823 v = opt->flags & OPT_VALUE;
826 i = *(int *)opt->addr;
827 if (opt->flags & OPT_NOARG) {
828 printer(arg, "%s", opt->name);
830 if (opt->flags & OPT_INC) {
831 for (; i > v; i -= v)
832 printer(arg, " %s", opt->name);
834 printer(arg, " # oops: %d not %d\n",
838 printer(arg, "%s %d", opt->name, i);
842 printer(arg, "%s", opt->name);
843 if ((opt->flags & OPT_NOARG) == 0)
844 printer(arg, " %x", *(u_int32_t *)opt->addr);
848 if (opt->flags & OPT_HIDE) {
851 p = (char *) opt->addr;
852 if ((opt->flags & OPT_STATIC) == 0)
855 printer(arg, "%s %q", opt->name, p);
859 case o_special_noarg:
861 if (opt->type != o_wild) {
862 printer(arg, "%s", opt->name);
863 if (n_arguments(opt) == 0)
867 if (opt->flags & OPT_A2PRINTER) {
868 void (*oprt) __P((option_t *,
869 void ((*)__P((void *, char *, ...))),
872 (*oprt)(opt, printer, arg);
873 } else if (opt->flags & OPT_A2STRVAL) {
874 p = (char *) opt->addr2;
875 if ((opt->flags & OPT_STATIC) == 0)
878 } else if (opt->flags & OPT_A2LIST) {
879 struct option_value *ovp;
881 ovp = (struct option_value *) opt->addr2;
883 printer(arg, "%q", ovp->value);
884 if ((ovp = ovp->next) == NULL)
886 printer(arg, "\t\t# (from %s)\n%s ",
887 ovp->source, opt->name);
890 printer(arg, "xxx # [don't know how to print value]");
895 printer(arg, "# %s value (type %d??)", opt->name, opt->type);
898 printer(arg, "\t\t# (from %s)\n", mainopt->source);
902 * print_option_list - print out options in effect from an
906 print_option_list(opt, printer, arg)
908 void (*printer) __P((void *, char *, ...));
911 while (opt->name != NULL) {
912 if (opt->priority != OPRIO_DEFAULT
913 && opt->winner != (short int) -1)
914 print_option(opt + opt->winner, opt, printer, arg);
917 } while (opt->flags & OPT_PRIOSUB);
922 * print_options - print out what options are in effect.
925 print_options(printer, arg)
926 void (*printer) __P((void *, char *, ...));
929 struct option_list *list;
932 print_option_list(general_options, printer, arg);
933 print_option_list(auth_options, printer, arg);
934 for (list = extra_options; list != NULL; list = list->next)
935 print_option_list(list->options, printer, arg);
936 print_option_list(the_channel->options, printer, arg);
937 for (i = 0; protocols[i] != NULL; ++i)
938 print_option_list(protocols[i]->options, printer, arg);
942 * usage - print out a message telling how to use the program.
947 if (phase == PHASE_INITIALIZE)
948 fprintf(stderr, usage_string, VERSION, progname);
952 * showhelp - print out usage message and exit.
958 if (phase == PHASE_INITIALIZE) {
966 * showversion - print out the version number and exit.
972 if (phase == PHASE_INITIALIZE) {
973 fprintf(stderr, "pppd version %s\n", VERSION);
980 * option_error - print a message about an error in an option.
981 * The message is logged, and also sent to
982 * stderr if phase == PHASE_INITIALIZE.
985 option_error __V((char *fmt, ...))
990 #if defined(__STDC__)
995 fmt = va_arg(args, char *);
997 vslprintf(buf, sizeof(buf), fmt, args);
999 if (phase == PHASE_INITIALIZE)
1000 fprintf(stderr, "%s: %s\n", progname, buf);
1001 syslog(LOG_ERR, "%s", buf);
1006 * readable - check if a file is readable by the real user.
1019 if (fstat(fd, &sbuf) != 0)
1021 if (sbuf.st_uid == uid)
1022 return sbuf.st_mode & S_IRUSR;
1023 if (sbuf.st_gid == getgid())
1024 return sbuf.st_mode & S_IRGRP;
1025 for (i = 0; i < ngroups; ++i)
1026 if (sbuf.st_gid == groups[i])
1027 return sbuf.st_mode & S_IRGRP;
1028 return sbuf.st_mode & S_IROTH;
1033 * Read a word from a file.
1034 * Words are delimited by white-space or by quotes (" or ').
1035 * Quotes, white-space and \ may be escaped with \.
1036 * \<newline> is ignored.
1039 getword(f, word, newlinep, filename)
1046 int quoted, comment;
1047 int value, digit, got, n;
1049 #define isoctal(c) ((c) >= '0' && (c) < '8')
1057 * First skip white-space and comments.
1065 * A newline means the end of a comment; backslash-newline
1066 * is ignored. Note that we cannot have escape && comment.
1078 * Ignore characters other than newline in a comment.
1084 * If this character is escaped, we have a word start.
1090 * If this is the escape character, look at the next character.
1098 * If this is the start of a comment, ignore the rest of the line.
1106 * A non-whitespace character is the start of a word.
1113 * Save the delimiter for quoted strings.
1115 if (!escape && (c == '"' || c == '\'')) {
1122 * Process characters until the end of the word.
1127 * This character is escaped: backslash-newline is ignored,
1128 * various other characters indicate particular values
1129 * as for C backslash-escapes.
1164 * \ddd octal sequence
1167 for (n = 0; n < 3 && isoctal(c); ++n) {
1168 value = (value << 3) + (c & 07);
1177 * \x<hex_string> sequence
1181 for (n = 0; n < 2 && isxdigit(c); ++n) {
1182 digit = toupper(c) - '0';
1184 digit += '0' + 10 - 'A';
1185 value = (value << 4) + digit;
1193 * Otherwise the character stands for itself.
1200 * Store the resulting character for the escape sequence.
1202 if (len < MAXWORDLEN-1)
1213 * Not escaped: see if we've reached the end of the word.
1219 if (isspace(c) || c == '#') {
1226 * Backslash starts an escape sequence.
1235 * An ordinary character: store it in the word and get another.
1237 if (len < MAXWORDLEN-1)
1245 * End of the word: check for errors.
1251 option_error("Error reading %s: %m", filename);
1255 * If len is zero, then we didn't find a word before the
1263 * Warn if the word was too long, and append a terminating null.
1265 if (len >= MAXWORDLEN) {
1266 option_error("warning: word in file %s too long (%.20s...)",
1268 len = MAXWORDLEN - 1;
1279 * number_option - parse an unsigned numeric parameter for an option.
1282 number_option(str, valp, base)
1289 *valp = strtoul(str, &ptr, base);
1291 option_error("invalid numeric parameter '%s' for %s option",
1292 str, current_option);
1300 * int_option - like number_option, but valp is int *,
1301 * the base is assumed to be 0, and *valp is not changed
1302 * if there is an error.
1305 int_option(str, valp)
1311 if (!number_option(str, &v, 0))
1319 * The following procedures parse options.
1323 * readfile - take commands from a file.
1329 return options_from_file(*argv, 1, 1, privileged_option);
1333 * callfile - take commands from /etc/ppp/peers/<name>.
1334 * Name may not contain /../, start with / or ../, or end in /..
1340 char *fname, *arg, *p;
1345 if (arg[0] == '/' || arg[0] == 0)
1348 for (p = arg; *p != 0; ) {
1349 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1353 while (*p != '/' && *p != 0)
1360 option_error("call option value may not contain .. or start with /");
1364 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1365 if ((fname = (char *) malloc(l)) == NULL)
1366 novm("call file name");
1367 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1369 ok = options_from_file(fname, 1, 1, 1);
1377 * setpassfilter - Set the pass filter for packets
1383 pc.linktype = DLT_PPP;
1384 pc.snapshot = PPP_HDRLEN;
1386 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1388 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1393 * setactivefilter - Set the active filter for packets
1396 setactivefilter(argv)
1399 pc.linktype = DLT_PPP;
1400 pc.snapshot = PPP_HDRLEN;
1402 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1404 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1410 * setdomain - Set domain name to append to hostname
1416 gethostname(hostname, MAXNAMELEN);
1419 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1420 domain = hostname + strlen(hostname);
1421 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1423 hostname[MAXNAMELEN-1] = 0;
1434 if (!privileged_option)
1436 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1437 if (fd < 0 && errno == EEXIST)
1438 fd = open(*argv, O_WRONLY | O_APPEND);
1440 if (!privileged_option)
1444 option_error("Can't open log file %s: %m", *argv);
1447 strlcpy(logfile_name, *argv, sizeof(logfile_name));
1448 if (logfile_fd >= 0)
1464 void (*init) __P((void));
1468 if (strchr(arg, '/') == 0) {
1469 const char *base = _PATH_PLUGIN;
1470 int l = strlen(base) + strlen(arg) + 2;
1473 novm("plugin file path");
1474 strlcpy(path, base, l);
1475 strlcat(path, "/", l);
1476 strlcat(path, arg, l);
1478 handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1482 option_error("%s", err);
1483 option_error("Couldn't load plugin %s", arg);
1486 init = (void (*)(void))dlsym(handle, "plugin_init");
1488 option_error("%s has no initialization entry point", arg);
1491 vers = (const char *) dlsym(handle, "pppd_version");
1493 warn("Warning: plugin %s has no version information", arg);
1494 } else if (strcmp(vers, VERSION) != 0) {
1495 option_error("Plugin %s is for pppd version %s, this is %s",
1499 info("Plugin %s loaded.", arg);