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.66 1999/09/11 12:08:58 paulus Exp $"
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
42 #include <pcap-int.h> /* XXX: To get struct pcap */
46 #include "pathnames.h"
47 #include "patchlevel.h"
55 #include <net/ppp-comp.h>
57 #if defined(ultrix) || defined(NeXT)
58 char *strdup __P((char *));
61 static const char rcsid[] = RCSID;
64 * Option variables and default values.
67 int dflag = 0; /* Tell libpcap we want debugging */
69 int debug = 0; /* Debug flag */
70 int kdebugflag = 0; /* Tell kernel to print debug messages */
71 int default_device = 1; /* Using /dev/tty or equivalent */
72 char devnam[MAXPATHLEN]; /* Device name */
73 int crtscts = 0; /* Use hardware flow control */
74 bool modem = 1; /* Use modem control lines */
75 int inspeed = 0; /* Input/Output speed requested */
76 u_int32_t netmask = 0; /* IP netmask to set on interface */
77 bool lockflag = 0; /* Create lock file to lock the serial dev */
78 bool nodetach = 0; /* Don't detach from controlling tty */
79 bool updetach = 0; /* Detach once link is up */
80 char *initializer = NULL; /* Script to initialize physical link */
81 char *connector = NULL; /* Script to establish physical link */
82 char *disconnector = NULL; /* Script to disestablish physical link */
83 char *welcomer = NULL; /* Script to run after phys link estab. */
84 char *ptycommand = NULL; /* Command to run on other side of pty */
85 int maxconnect = 0; /* Maximum connect time */
86 char user[MAXNAMELEN]; /* Username for PAP */
87 char passwd[MAXSECRETLEN]; /* Password for PAP */
88 bool persist = 0; /* Reopen link after it goes down */
89 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
90 bool demand = 0; /* do dial-on-demand */
91 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
92 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
93 int holdoff = 30; /* # seconds to pause before reconnecting */
94 bool holdoff_specified; /* true if a holdoff value has been given */
95 bool notty = 0; /* Stdin/out is not a tty */
96 char *record_file = NULL; /* File to record chars sent/received */
98 bool sync_serial = 0; /* Device is synchronous serial device */
99 int log_to_fd = 1; /* send log messages to this fd too */
100 int maxfail = 10; /* max # of unsuccessful connection attempts */
101 char linkname[MAXPATHLEN]; /* logical name for link */
102 bool tune_kernel; /* may alter kernel settings */
104 extern option_t auth_options[];
105 extern struct stat devstat;
106 extern int prepass; /* Doing pre-pass to find device name */
108 struct option_info initializer_info;
109 struct option_info connector_info;
110 struct option_info disconnector_info;
111 struct option_info welcomer_info;
112 struct option_info devnam_info;
113 struct option_info ptycommand_info;
116 struct bpf_program pass_filter;/* Filter program for packets to pass */
117 struct bpf_program active_filter; /* Filter program for link-active pkts */
118 pcap_t pc; /* Fake struct pcap so we can compile expr */
121 char *current_option; /* the name of the option being parsed */
122 int privileged_option; /* set iff the current option came from root */
123 char *option_source; /* string saying where the option came from */
124 bool log_to_file; /* log_to_fd is a file opened by us */
129 static int setdevname __P((char *));
130 static int setipaddr __P((char *));
131 static int setspeed __P((char *));
132 static int noopt __P((char **));
133 static int setdomain __P((char **));
134 static int setnetmask __P((char **));
135 static int setxonxoff __P((char **));
136 static int readfile __P((char **));
137 static int callfile __P((char **));
138 static int showversion __P((char **));
139 static int showhelp __P((char **));
140 static void usage __P((void));
141 static int setlogfile __P((char **));
143 static int loadplugin __P((char **));
147 static int setpassfilter __P((char **));
148 static int setactivefilter __P((char **));
151 static option_t *find_option __P((char *name));
152 static int process_option __P((option_t *, char **));
153 static int n_arguments __P((option_t *));
154 static int number_option __P((char *, u_int32_t *, int));
157 * Structure to store extra lists of options.
161 struct option_list *next;
164 static struct option_list *extra_options = NULL;
169 option_t general_options[] = {
170 { "debug", o_int, &debug,
171 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
172 { "-d", o_int, &debug,
173 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
174 { "kdebug", o_int, &kdebugflag,
175 "Set kernel driver debug level" },
176 { "nodetach", o_bool, &nodetach,
177 "Don't detach from controlling tty", 1 },
178 { "-detach", o_bool, &nodetach,
179 "Don't detach from controlling tty", 1 },
180 { "updetach", o_bool, &updetach,
181 "Detach from controlling tty once link is up", 1 },
182 { "holdoff", o_int, &holdoff,
183 "Set time in seconds before retrying connection" },
184 { "idle", o_int, &idle_time_limit,
185 "Set time in seconds before disconnecting idle link" },
186 { "lock", o_bool, &lockflag,
187 "Lock serial device with UUCP-style lock file", 1 },
188 { "-all", o_special_noarg, noopt,
189 "Don't request/allow any LCP or IPCP options (useless)" },
190 { "init", o_string, &initializer,
191 "A program to initialize the device",
192 OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
193 { "connect", o_string, &connector,
194 "A program to set up a connection",
195 OPT_A2INFO | OPT_PRIVFIX, &connector_info },
196 { "disconnect", o_string, &disconnector,
197 "Program to disconnect serial device",
198 OPT_A2INFO | OPT_PRIVFIX, &disconnector_info },
199 { "welcome", o_string, &welcomer,
200 "Script to welcome client",
201 OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
202 { "pty", o_string, &ptycommand,
203 "Script to run on pseudo-tty master side",
204 OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
205 { "notty", o_bool, ¬ty,
206 "Input/output is not a tty", OPT_DEVNAM | 1 },
207 { "record", o_string, &record_file,
208 "Record characters sent/received to file" },
209 { "maxconnect", o_int, &maxconnect,
210 "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
211 { "crtscts", o_int, &crtscts,
212 "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
213 { "nocrtscts", o_int, &crtscts,
214 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
215 { "-crtscts", o_int, &crtscts,
216 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
217 { "cdtrcts", o_int, &crtscts,
218 "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
219 { "nocdtrcts", o_int, &crtscts,
220 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
221 { "xonxoff", o_special_noarg, setxonxoff,
222 "Set software (XON/XOFF) flow control" },
223 { "domain", o_special, setdomain,
224 "Add given domain name to hostname" },
225 { "mtu", o_int, &lcp_allowoptions[0].mru,
226 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
227 { "netmask", o_special, setnetmask,
229 { "modem", o_bool, &modem,
230 "Use modem control lines", 1 },
231 { "local", o_bool, &modem,
232 "Don't use modem control lines" },
233 { "file", o_special, readfile,
234 "Take options from a file", OPT_PREPASS },
235 { "call", o_special, callfile,
236 "Take options from a privileged file", OPT_PREPASS },
237 { "persist", o_bool, &persist,
238 "Keep on reopening connection after close", 1 },
239 { "nopersist", o_bool, &persist,
240 "Turn off persist option" },
241 { "demand", o_bool, &demand,
242 "Dial on demand", OPT_INITONLY | 1, &persist },
243 { "--version", o_special_noarg, showversion,
244 "Show version number" },
245 { "--help", o_special_noarg, showhelp,
246 "Show brief listing of options" },
247 { "-h", o_special_noarg, showhelp,
248 "Show brief listing of options" },
249 { "sync", o_bool, &sync_serial,
250 "Use synchronous HDLC serial encoding", 1 },
251 { "logfd", o_int, &log_to_fd,
252 "Send log messages to this file descriptor" },
253 { "logfile", o_special, setlogfile,
254 "Append log messages to this file" },
255 { "nolog", o_int, &log_to_fd,
256 "Don't send log messages to any file",
257 OPT_NOARG | OPT_VAL(-1) },
258 { "nologfd", o_int, &log_to_fd,
259 "Don't send log messages to any file descriptor",
260 OPT_NOARG | OPT_VAL(-1) },
261 { "linkname", o_string, linkname,
262 "Set logical name for link",
263 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
264 { "maxfail", o_int, &maxfail,
265 "Maximum number of unsuccessful connection attempts to allow" },
266 { "ktune", o_bool, &tune_kernel,
267 "Alter kernel settings as necessary", 1 },
268 { "noktune", o_bool, &tune_kernel,
269 "Don't alter kernel settings", 0 },
271 { "plugin", o_special, loadplugin,
272 "Load a plug-in module into pppd", OPT_PRIV },
276 { "pdebug", o_int, &dflag,
277 "libpcap debugging" },
278 { "pass-filter", 1, setpassfilter,
279 "set filter for packets to pass" },
280 { "active-filter", 1, setactivefilter,
281 "set filter for active pkts" },
287 #ifndef IMPLEMENTATION
288 #define IMPLEMENTATION ""
291 static char *usage_string = "\
292 pppd version %s.%d%s\n\
293 Usage: %s [ options ], where options are:\n\
294 <device> Communicate over the named device\n\
295 <speed> Set the baud rate to <speed>\n\
296 <loc>:<rem> Set the local and/or remote interface IP\n\
297 addresses. Either one may be omitted.\n\
298 asyncmap <n> Set the desired async map to hex <n>\n\
299 auth Require authentication from peer\n\
300 connect <p> Invoke shell command <p> to set up the serial line\n\
301 crtscts Use hardware RTS/CTS flow control\n\
302 defaultroute Add default route through interface\n\
303 file <f> Take options from file <f>\n\
304 modem Use modem control lines\n\
305 mru <n> Set MRU value to <n> for negotiation\n\
306 See pppd(8) for more options.\n\
310 * parse_args - parse a string of arguments from the command line.
311 * If prepass is true, we are scanning for the device name and only
312 * processing a few options, so error messages are suppressed.
315 parse_args(argc, argv)
323 privileged_option = privileged;
324 option_source = "command line";
330 * First see if it's an option in the new option list.
332 opt = find_option(arg);
334 int n = n_arguments(opt);
336 option_error("too few parameters for option %s", arg);
339 current_option = arg;
340 if (!process_option(opt, argv))
348 * Maybe a tty name, speed or IP address?
350 if ((ret = setdevname(arg)) == 0
351 && (ret = setspeed(arg)) == 0
352 && (ret = setipaddr(arg)) == 0
354 option_error("unrecognized option '%s'", arg);
358 if (ret < 0) /* error */
366 * scan_args - scan the command line arguments to get the tty name,
367 * if specified. Also checks whether the notty or pty option was given.
370 scan_args(argc, argv)
377 privileged_option = privileged;
382 if (strcmp(arg, "notty") == 0 || strcmp(arg, "pty") == 0)
385 /* Skip options and their arguments */
386 opt = find_option(arg);
388 int n = n_arguments(opt);
394 /* Check if it's a tty name and copy it if so */
395 (void) setdevname(arg, 1);
401 * options_from_file - Read a string of options from a file,
402 * and interpret them.
405 options_from_file(filename, must_exist, check_prot, priv)
412 int i, newline, ret, err;
417 char args[MAXARGS][MAXWORDLEN];
418 char cmd[MAXWORDLEN];
422 f = fopen(filename, "r");
427 if (!must_exist && err == ENOENT)
430 option_error("Can't open options file %s: %m", filename);
434 oldpriv = privileged_option;
435 privileged_option = priv;
436 oldsource = option_source;
437 option_source = strdup(filename);
438 if (option_source == NULL)
439 option_source = "file";
441 while (getword(f, cmd, &newline, filename)) {
443 * First see if it's a command.
445 opt = find_option(cmd);
447 int n = n_arguments(opt);
448 for (i = 0; i < n; ++i) {
449 if (!getword(f, args[i], &newline, filename)) {
451 "In file %s: too few parameters for option '%s'",
457 current_option = cmd;
458 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
459 option_error("the %s option may not be used in the %s file",
463 if (!process_option(opt, argv))
469 * Maybe a tty name, speed or IP address?
471 if ((i = setdevname(cmd)) == 0
472 && (i = setspeed(cmd)) == 0
473 && (i = setipaddr(cmd)) == 0) {
474 option_error("In file %s: unrecognized option '%s'",
478 if (i < 0) /* error */
485 privileged_option = oldpriv;
486 option_source = oldsource;
491 * options_from_user - See if the use has a ~/.ppprc file,
492 * and if so, interpret options from it.
497 char *user, *path, *file;
502 pw = getpwuid(getuid());
503 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
505 file = _PATH_USEROPT;
506 pl = strlen(user) + strlen(file) + 2;
509 novm("init file name");
510 slprintf(path, pl, "%s/%s", user, file);
511 ret = options_from_file(path, 0, 1, privileged);
517 * options_for_tty - See if an options file exists for the serial
518 * device, and if so, interpret options from it.
523 char *dev, *path, *p;
528 if (strncmp(dev, "/dev/", 5) == 0)
530 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
531 return 1; /* don't look for /etc/ppp/options.tty */
532 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
535 novm("tty init file name");
536 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
537 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
538 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
541 ret = options_from_file(path, 0, 0, 1);
547 * options_from_list - process a string of options in a wordlist.
550 options_from_list(w, priv)
558 privileged_option = priv;
559 option_source = "secrets file";
563 * First see if it's a command.
565 opt = find_option(w->word);
567 int n = n_arguments(opt);
568 struct wordlist *w0 = w;
569 for (i = 0; i < n; ++i) {
573 "In secrets file: too few parameters for option '%s'",
579 current_option = w0->word;
580 if (!process_option(opt, argv))
586 * Maybe a tty name, speed or IP address?
588 if ((i = setdevname(w->word)) == 0
589 && (i = setspeed(w->word)) == 0
590 && (i = setipaddr(w->word)) == 0) {
591 option_error("In secrets file: unrecognized option '%s'",
595 if (i < 0) /* error */
605 * find_option - scan the option lists for the various protocols
606 * looking for an entry with the given name.
607 * This could be optimized by using a hash table.
614 struct option_list *list;
617 for (list = extra_options; list != NULL; list = list->next)
618 for (opt = list->options; opt->name != NULL; ++opt)
619 if (strcmp(name, opt->name) == 0)
621 for (opt = general_options; opt->name != NULL; ++opt)
622 if (strcmp(name, opt->name) == 0)
624 for (opt = auth_options; opt->name != NULL; ++opt)
625 if (strcmp(name, opt->name) == 0)
627 for (i = 0; protocols[i] != NULL; ++i)
628 if ((opt = protocols[i]->options) != NULL)
629 for (; opt->name != NULL; ++opt)
630 if (strcmp(name, opt->name) == 0)
636 * process_option - process one new-style option.
639 process_option(opt, argv)
646 int (*parser) __P((char **));
648 if ((opt->flags & OPT_PREPASS) == 0 && prepass)
650 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
651 option_error("it's too late to use the %s option", opt->name);
654 if ((opt->flags & OPT_PRIV) && !privileged_option) {
655 option_error("using the %s option requires root privilege", opt->name);
658 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
659 option_error("%s option is disabled", opt->name);
662 if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
663 struct option_info *ip = (struct option_info *) opt->addr2;
664 if (ip && ip->priv) {
665 option_error("%s option cannot be overridden", opt->name);
672 v = opt->flags & OPT_VALUE;
673 *(bool *)(opt->addr) = v;
674 if (opt->addr2 && (opt->flags & OPT_A2COPY))
675 *(bool *)(opt->addr2) = v;
680 if ((opt->flags & OPT_NOARG) == 0) {
681 if (!int_option(*argv, &iv))
683 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
684 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
685 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
686 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
687 switch (opt->flags & OPT_LIMITS) {
689 option_error("%s value must be%s >= %d",
690 opt->name, zok, opt->lower_limit);
693 option_error("%s value must be%s <= %d",
694 opt->name, zok, opt->upper_limit);
697 option_error("%s value must be%s between %d and %d",
698 opt->name, opt->lower_limit, opt->upper_limit);
704 a = opt->flags & OPT_VALUE;
706 a -= 256; /* sign extend */
708 if (opt->flags & OPT_INC)
709 iv += *(int *)(opt->addr);
710 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
711 int oldv = *(int *)(opt->addr);
712 if ((opt->flags & OPT_ZEROINF) ?
713 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
714 option_error("%s value cannot be increased", opt->name);
718 *(int *)(opt->addr) = iv;
719 if (opt->addr2 && (opt->flags & OPT_A2COPY))
720 *(int *)(opt->addr2) = iv;
724 if (opt->flags & OPT_NOARG) {
725 v = opt->flags & OPT_VALUE;
726 } else if (!number_option(*argv, &v, 16))
728 if (opt->flags & OPT_OR)
729 v |= *(u_int32_t *)(opt->addr);
730 *(u_int32_t *)(opt->addr) = v;
731 if (opt->addr2 && (opt->flags & OPT_A2COPY))
732 *(u_int32_t *)(opt->addr2) = v;
736 if (opt->flags & OPT_STATIC) {
737 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
741 novm("option argument");
742 *(char **)(opt->addr) = sv;
746 case o_special_noarg:
748 parser = (int (*) __P((char **))) opt->addr;
749 if (!(*parser)(argv))
755 if (opt->flags & OPT_A2INFO) {
756 struct option_info *ip = (struct option_info *) opt->addr2;
757 ip->priv = privileged_option;
758 ip->source = option_source;
759 } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
760 *(bool *)(opt->addr2) = 1;
767 * n_arguments - tell how many arguments an option takes
773 return (opt->type == o_bool || opt->type == o_special_noarg
774 || (opt->flags & OPT_NOARG))? 0: 1;
778 * add_options - add a list of options to the set we grok.
784 struct option_list *list;
786 list = malloc(sizeof(*list));
788 novm("option list entry");
790 list->next = extra_options;
791 extra_options = list;
795 * usage - print out a message telling how to use the program.
800 if (phase == PHASE_INITIALIZE)
801 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
806 * showhelp - print out usage message and exit.
812 if (phase == PHASE_INITIALIZE) {
820 * showversion - print out the version number and exit.
826 if (phase == PHASE_INITIALIZE) {
827 fprintf(stderr, "pppd version %s.%d%s\n",
828 VERSION, PATCHLEVEL, IMPLEMENTATION);
835 * option_error - print a message about an error in an option.
836 * The message is logged, and also sent to
837 * stderr if phase == PHASE_INITIALIZE.
840 option_error __V((char *fmt, ...))
845 #if defined(__STDC__)
850 fmt = va_arg(args, char *);
856 vslprintf(buf, sizeof(buf), fmt, args);
858 if (phase == PHASE_INITIALIZE)
859 fprintf(stderr, "%s: %s\n", progname, buf);
860 syslog(LOG_ERR, "%s", buf);
865 * readable - check if a file is readable by the real user.
878 if (fstat(fd, &sbuf) != 0)
880 if (sbuf.st_uid == uid)
881 return sbuf.st_mode & S_IRUSR;
882 if (sbuf.st_gid == getgid())
883 return sbuf.st_mode & S_IRGRP;
884 for (i = 0; i < ngroups; ++i)
885 if (sbuf.st_gid == groups[i])
886 return sbuf.st_mode & S_IRGRP;
887 return sbuf.st_mode & S_IROTH;
892 * Read a word from a file.
893 * Words are delimited by white-space or by quotes (" or ').
894 * Quotes, white-space and \ may be escaped with \.
895 * \<newline> is ignored.
898 getword(f, word, newlinep, filename)
906 int value, digit, got, n;
908 #define isoctal(c) ((c) >= '0' && (c) < '8')
916 * First skip white-space and comments.
924 * A newline means the end of a comment; backslash-newline
925 * is ignored. Note that we cannot have escape && comment.
937 * Ignore characters other than newline in a comment.
943 * If this character is escaped, we have a word start.
949 * If this is the escape character, look at the next character.
957 * If this is the start of a comment, ignore the rest of the line.
965 * A non-whitespace character is the start of a word.
972 * Save the delimiter for quoted strings.
974 if (!escape && (c == '"' || c == '\'')) {
981 * Process characters until the end of the word.
986 * This character is escaped: backslash-newline is ignored,
987 * various other characters indicate particular values
988 * as for C backslash-escapes.
1023 * \ddd octal sequence
1026 for (n = 0; n < 3 && isoctal(c); ++n) {
1027 value = (value << 3) + (c & 07);
1036 * \x<hex_string> sequence
1040 for (n = 0; n < 2 && isxdigit(c); ++n) {
1041 digit = toupper(c) - '0';
1043 digit += '0' + 10 - 'A';
1044 value = (value << 4) + digit;
1052 * Otherwise the character stands for itself.
1059 * Store the resulting character for the escape sequence.
1061 if (len < MAXWORDLEN-1)
1072 * Not escaped: see if we've reached the end of the word.
1078 if (isspace(c) || c == '#') {
1085 * Backslash starts an escape sequence.
1094 * An ordinary character: store it in the word and get another.
1096 if (len < MAXWORDLEN-1)
1104 * End of the word: check for errors.
1110 option_error("Error reading %s: %m", filename);
1114 * If len is zero, then we didn't find a word before the
1122 * Warn if the word was too long, and append a terminating null.
1124 if (len >= MAXWORDLEN) {
1125 option_error("warning: word in file %s too long (%.20s...)",
1127 len = MAXWORDLEN - 1;
1138 * number_option - parse an unsigned numeric parameter for an option.
1141 number_option(str, valp, base)
1148 *valp = strtoul(str, &ptr, base);
1150 option_error("invalid numeric parameter '%s' for %s option",
1151 str, current_option);
1159 * int_option - like number_option, but valp is int *,
1160 * the base is assumed to be 0, and *valp is not changed
1161 * if there is an error.
1164 int_option(str, valp)
1170 if (!number_option(str, &v, 0))
1178 * The following procedures parse options.
1182 * readfile - take commands from a file.
1188 return options_from_file(*argv, 1, 1, privileged_option);
1192 * callfile - take commands from /etc/ppp/peers/<name>.
1193 * Name may not contain /../, start with / or ../, or end in /..
1199 char *fname, *arg, *p;
1204 if (arg[0] == '/' || arg[0] == 0)
1207 for (p = arg; *p != 0; ) {
1208 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1212 while (*p != '/' && *p != 0)
1219 option_error("call option value may not contain .. or start with /");
1223 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1224 if ((fname = (char *) malloc(l)) == NULL)
1225 novm("call file name");
1226 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1228 ok = options_from_file(fname, 1, 1, 1);
1236 * setpdebug - Set libpcap debugging level.
1242 return int_option(*argv, &dflag);
1246 * setpassfilter - Set the pass filter for packets
1252 pc.linktype = DLT_PPP;
1253 pc.snapshot = PPP_HDRLEN;
1255 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1257 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1262 * setactivefilter - Set the active filter for packets
1265 setactivefilter(argv)
1268 pc.linktype = DLT_PPP;
1269 pc.snapshot = PPP_HDRLEN;
1271 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1273 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1279 * noopt - Disable all options.
1285 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1286 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1287 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1288 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1294 * setdomain - Set domain name to append to hostname
1300 if (!privileged_option) {
1301 option_error("using the domain option requires root privilege");
1304 gethostname(hostname, MAXNAMELEN);
1307 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1308 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1310 hostname[MAXNAMELEN-1] = 0;
1316 * setspeed - Set the speed.
1327 spd = strtol(arg, &ptr, 0);
1328 if (ptr == arg || *ptr != 0 || spd == 0)
1336 * setdevname - Set the device name.
1342 struct stat statbuf;
1343 char dev[MAXPATHLEN];
1348 if (strncmp("/dev/", cp, 5) != 0) {
1349 strlcpy(dev, "/dev/", sizeof(dev));
1350 strlcat(dev, cp, sizeof(dev));
1355 * Check if there is a character device by this name.
1357 if (stat(cp, &statbuf) < 0) {
1358 if (errno == ENOENT)
1360 option_error("Couldn't stat %s: %m", cp);
1363 if (!S_ISCHR(statbuf.st_mode)) {
1364 option_error("%s is not a character device", cp);
1368 if (phase != PHASE_INITIALIZE) {
1369 option_error("device name cannot be changed after initialization");
1371 } else if (devnam_fixed) {
1372 option_error("per-tty options file may not specify device name");
1376 if (devnam_info.priv && !privileged_option) {
1377 option_error("device name cannot be overridden");
1381 strlcpy(devnam, cp, sizeof(devnam));
1384 devnam_info.priv = privileged_option;
1385 devnam_info.source = option_source;
1392 * setipaddr - Set the IP address
1400 u_int32_t local, remote;
1401 ipcp_options *wo = &ipcp_wantoptions[0];
1404 * IP address pair separated by ":".
1406 if ((colon = strchr(arg, ':')) == NULL)
1412 * If colon first character, then no local addr.
1416 if ((local = inet_addr(arg)) == -1) {
1417 if ((hp = gethostbyname(arg)) == NULL) {
1418 option_error("unknown host: %s", arg);
1421 local = *(u_int32_t *)hp->h_addr;
1424 if (bad_ip_adrs(local)) {
1425 option_error("bad local IP address %s", ip_ntoa(local));
1429 wo->ouraddr = local;
1434 * If colon last character, then no remote addr.
1436 if (*++colon != '\0') {
1437 if ((remote = inet_addr(colon)) == -1) {
1438 if ((hp = gethostbyname(colon)) == NULL) {
1439 option_error("unknown host: %s", colon);
1442 remote = *(u_int32_t *)hp->h_addr;
1443 if (remote_name[0] == 0)
1444 strlcpy(remote_name, colon, sizeof(remote_name));
1447 if (bad_ip_adrs(remote)) {
1448 option_error("bad remote IP address %s", ip_ntoa(remote));
1452 wo->hisaddr = remote;
1460 * setnetmask - set the netmask to be used on the interface.
1471 * Unfortunately, if we use inet_addr, we can't tell whether
1472 * a result of all 1s is an error or a valid 255.255.255.255.
1478 b = strtoul(p, &endp, 0);
1481 if (b < 0 || b > 255) {
1483 /* accept e.g. 0xffffff00 */
1489 mask |= b << (n * 8);
1491 if (*p != '.' || n == 0)
1498 if (*p != 0 || (netmask & ~mask) != 0) {
1499 option_error("invalid netmask value '%s'", *argv);
1511 lcp_wantoptions[0].asyncmap |= 0x000A0000; /* escape ^S and ^Q */
1512 lcp_wantoptions[0].neg_asyncmap = 1;
1524 if (!privileged_option)
1526 fd = open(*argv, O_WRONLY | O_APPEND);
1528 if (!privileged_option)
1532 option_error("Can't open log file %s: %m", *argv);
1535 if (log_to_file && log_to_fd >= 0)
1550 void (*init) __P((void));
1552 handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
1556 option_error("%s", err);
1557 option_error("Couldn't load plugin %s", arg);
1560 init = dlsym(handle, "plugin_init");
1562 option_error("%s has no initialization entry point", arg);
1566 info("Plugin %s loaded.", arg);