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.71 2000/03/27 06:03:03 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 *connect_script = NULL; /* Script to establish physical link */
82 char *disconnect_script = 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 *pty_socket = NULL; /* Socket to connect to pty */
97 char *record_file = NULL; /* File to record chars sent/received */
99 bool sync_serial = 0; /* Device is synchronous serial device */
100 int log_to_fd = 1; /* send log messages to this fd too */
101 int maxfail = 10; /* max # of unsuccessful connection attempts */
102 char linkname[MAXPATHLEN]; /* logical name for link */
103 bool tune_kernel; /* may alter kernel settings */
104 int connect_delay = 1000; /* wait this many ms after connect script */
105 int max_data_rate; /* max bytes/sec through charshunt */
106 int req_unit = -1; /* requested interface unit */
107 bool multilink = 0; /* Enable multilink operation */
109 extern option_t auth_options[];
110 extern struct stat devstat;
111 extern int prepass; /* Doing pre-pass to find device name */
113 struct option_info initializer_info;
114 struct option_info connect_script_info;
115 struct option_info disconnect_script_info;
116 struct option_info welcomer_info;
117 struct option_info devnam_info;
118 struct option_info ptycommand_info;
121 struct bpf_program pass_filter;/* Filter program for packets to pass */
122 struct bpf_program active_filter; /* Filter program for link-active pkts */
123 pcap_t pc; /* Fake struct pcap so we can compile expr */
126 char *current_option; /* the name of the option being parsed */
127 int privileged_option; /* set iff the current option came from root */
128 char *option_source; /* string saying where the option came from */
129 bool log_to_file; /* log_to_fd is a file opened by us */
134 static int setdevname __P((char *));
135 static int setipaddr __P((char *));
136 static int setspeed __P((char *));
137 static int noopt __P((char **));
138 static int setdomain __P((char **));
139 static int setnetmask __P((char **));
140 static int setxonxoff __P((char **));
141 static int readfile __P((char **));
142 static int callfile __P((char **));
143 static int showversion __P((char **));
144 static int showhelp __P((char **));
145 static void usage __P((void));
146 static int setlogfile __P((char **));
148 static int loadplugin __P((char **));
152 static int setpassfilter __P((char **));
153 static int setactivefilter __P((char **));
156 static option_t *find_option __P((char *name));
157 static int process_option __P((option_t *, char **));
158 static int n_arguments __P((option_t *));
159 static int number_option __P((char *, u_int32_t *, int));
162 * Structure to store extra lists of options.
166 struct option_list *next;
169 static struct option_list *extra_options = NULL;
174 option_t general_options[] = {
175 { "debug", o_int, &debug,
176 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
177 { "-d", o_int, &debug,
178 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
179 { "kdebug", o_int, &kdebugflag,
180 "Set kernel driver debug level" },
181 { "nodetach", o_bool, &nodetach,
182 "Don't detach from controlling tty", 1 },
183 { "-detach", o_bool, &nodetach,
184 "Don't detach from controlling tty", 1 },
185 { "updetach", o_bool, &updetach,
186 "Detach from controlling tty once link is up", 1 },
187 { "holdoff", o_int, &holdoff,
188 "Set time in seconds before retrying connection" },
189 { "idle", o_int, &idle_time_limit,
190 "Set time in seconds before disconnecting idle link" },
191 { "lock", o_bool, &lockflag,
192 "Lock serial device with UUCP-style lock file", 1 },
193 { "-all", o_special_noarg, noopt,
194 "Don't request/allow any LCP or IPCP options (useless)" },
195 { "init", o_string, &initializer,
196 "A program to initialize the device",
197 OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
198 { "connect", o_string, &connect_script,
199 "A program to set up a connection",
200 OPT_A2INFO | OPT_PRIVFIX, &connect_script_info },
201 { "disconnect", o_string, &disconnect_script,
202 "Program to disconnect serial device",
203 OPT_A2INFO | OPT_PRIVFIX, &disconnect_script_info },
204 { "welcome", o_string, &welcomer,
205 "Script to welcome client",
206 OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
207 { "pty", o_string, &ptycommand,
208 "Script to run on pseudo-tty master side",
209 OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
210 { "notty", o_bool, ¬ty,
211 "Input/output is not a tty", OPT_DEVNAM | 1 },
212 { "socket", o_string, &pty_socket,
213 "Send and receive over socket, arg is host:port", OPT_DEVNAM },
214 { "record", o_string, &record_file,
215 "Record characters sent/received to file" },
216 { "maxconnect", o_int, &maxconnect,
217 "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
218 { "crtscts", o_int, &crtscts,
219 "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
220 { "nocrtscts", o_int, &crtscts,
221 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
222 { "-crtscts", o_int, &crtscts,
223 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
224 { "cdtrcts", o_int, &crtscts,
225 "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
226 { "nocdtrcts", o_int, &crtscts,
227 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
228 { "xonxoff", o_special_noarg, setxonxoff,
229 "Set software (XON/XOFF) flow control" },
230 { "domain", o_special, setdomain,
231 "Add given domain name to hostname" },
232 { "mtu", o_int, &lcp_allowoptions[0].mru,
233 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
234 { "netmask", o_special, setnetmask,
236 { "modem", o_bool, &modem,
237 "Use modem control lines", 1 },
238 { "local", o_bool, &modem,
239 "Don't use modem control lines" },
240 { "file", o_special, readfile,
241 "Take options from a file", OPT_PREPASS },
242 { "call", o_special, callfile,
243 "Take options from a privileged file", OPT_PREPASS },
244 { "persist", o_bool, &persist,
245 "Keep on reopening connection after close", 1 },
246 { "nopersist", o_bool, &persist,
247 "Turn off persist option" },
248 { "demand", o_bool, &demand,
249 "Dial on demand", OPT_INITONLY | 1, &persist },
250 { "--version", o_special_noarg, showversion,
251 "Show version number" },
252 { "--help", o_special_noarg, showhelp,
253 "Show brief listing of options" },
254 { "-h", o_special_noarg, showhelp,
255 "Show brief listing of options" },
256 { "sync", o_bool, &sync_serial,
257 "Use synchronous HDLC serial encoding", 1 },
258 { "logfd", o_int, &log_to_fd,
259 "Send log messages to this file descriptor" },
260 { "logfile", o_special, setlogfile,
261 "Append log messages to this file" },
262 { "nolog", o_int, &log_to_fd,
263 "Don't send log messages to any file",
264 OPT_NOARG | OPT_VAL(-1) },
265 { "nologfd", o_int, &log_to_fd,
266 "Don't send log messages to any file descriptor",
267 OPT_NOARG | OPT_VAL(-1) },
268 { "linkname", o_string, linkname,
269 "Set logical name for link",
270 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
271 { "maxfail", o_int, &maxfail,
272 "Maximum number of unsuccessful connection attempts to allow" },
273 { "ktune", o_bool, &tune_kernel,
274 "Alter kernel settings as necessary", 1 },
275 { "noktune", o_bool, &tune_kernel,
276 "Don't alter kernel settings", 0 },
277 { "connect-delay", o_int, &connect_delay,
278 "Maximum time (in ms) to wait after connect script finishes" },
279 { "datarate", o_int, &max_data_rate,
280 "Maximum data rate in bytes/sec (with pty, notty or record option)" },
281 { "unit", o_int, &req_unit,
282 "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
283 #ifdef HAVE_MULTILINK
284 { "multilink", o_bool, &multilink,
285 "Enable multilink operation", 1 },
286 { "nomultilink", o_bool, &multilink,
287 "Disable multilink operation", 0 },
288 { "mp", o_bool, &multilink,
289 "Enable multilink operation", 1 },
290 { "nomp", o_bool, &multilink,
291 "Disable multilink operation", 0 },
292 #endif /* HAVE_MULTILINK */
294 { "plugin", o_special, loadplugin,
295 "Load a plug-in module into pppd", OPT_PRIV },
299 { "pdebug", o_int, &dflag,
300 "libpcap debugging" },
301 { "pass-filter", 1, setpassfilter,
302 "set filter for packets to pass" },
303 { "active-filter", 1, setactivefilter,
304 "set filter for active pkts" },
310 #ifndef IMPLEMENTATION
311 #define IMPLEMENTATION ""
314 static char *usage_string = "\
315 pppd version %s.%d%s\n\
316 Usage: %s [ options ], where options are:\n\
317 <device> Communicate over the named device\n\
318 <speed> Set the baud rate to <speed>\n\
319 <loc>:<rem> Set the local and/or remote interface IP\n\
320 addresses. Either one may be omitted.\n\
321 asyncmap <n> Set the desired async map to hex <n>\n\
322 auth Require authentication from peer\n\
323 connect <p> Invoke shell command <p> to set up the serial line\n\
324 crtscts Use hardware RTS/CTS flow control\n\
325 defaultroute Add default route through interface\n\
326 file <f> Take options from file <f>\n\
327 modem Use modem control lines\n\
328 mru <n> Set MRU value to <n> for negotiation\n\
329 See pppd(8) for more options.\n\
333 * parse_args - parse a string of arguments from the command line.
334 * If prepass is true, we are scanning for the device name and only
335 * processing a few options, so error messages are suppressed.
338 parse_args(argc, argv)
346 privileged_option = privileged;
347 option_source = "command line";
353 * First see if it's an option in the new option list.
355 opt = find_option(arg);
357 int n = n_arguments(opt);
359 option_error("too few parameters for option %s", arg);
362 current_option = arg;
363 if (!process_option(opt, argv))
371 * Maybe a tty name, speed or IP address?
373 if ((ret = setdevname(arg)) == 0
374 && (ret = setspeed(arg)) == 0
375 && (ret = setipaddr(arg)) == 0
377 option_error("unrecognized option '%s'", arg);
381 if (ret < 0) /* error */
389 * scan_args - scan the command line arguments to get the tty name,
390 * if specified. Also checks whether the notty or pty option was given.
393 scan_args(argc, argv)
400 privileged_option = privileged;
405 if (strcmp(arg, "notty") == 0 || strcmp(arg, "pty") == 0)
408 /* Skip options and their arguments */
409 opt = find_option(arg);
411 int n = n_arguments(opt);
417 /* Check if it's a tty name and copy it if so */
418 (void) setdevname(arg, 1);
424 * options_from_file - Read a string of options from a file,
425 * and interpret them.
428 options_from_file(filename, must_exist, check_prot, priv)
435 int i, newline, ret, err;
440 char args[MAXARGS][MAXWORDLEN];
441 char cmd[MAXWORDLEN];
445 f = fopen(filename, "r");
450 if (!must_exist && err == ENOENT)
453 option_error("Can't open options file %s: %m", filename);
457 oldpriv = privileged_option;
458 privileged_option = priv;
459 oldsource = option_source;
460 option_source = strdup(filename);
461 if (option_source == NULL)
462 option_source = "file";
464 while (getword(f, cmd, &newline, filename)) {
466 * First see if it's a command.
468 opt = find_option(cmd);
470 int n = n_arguments(opt);
471 for (i = 0; i < n; ++i) {
472 if (!getword(f, args[i], &newline, filename)) {
474 "In file %s: too few parameters for option '%s'",
480 current_option = cmd;
481 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
482 option_error("the %s option may not be used in the %s file",
486 if (!process_option(opt, argv))
492 * Maybe a tty name, speed or IP address?
494 if ((i = setdevname(cmd)) == 0
495 && (i = setspeed(cmd)) == 0
496 && (i = setipaddr(cmd)) == 0) {
497 option_error("In file %s: unrecognized option '%s'",
501 if (i < 0) /* error */
508 privileged_option = oldpriv;
509 option_source = oldsource;
514 * options_from_user - See if the use has a ~/.ppprc file,
515 * and if so, interpret options from it.
520 char *user, *path, *file;
525 pw = getpwuid(getuid());
526 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
528 file = _PATH_USEROPT;
529 pl = strlen(user) + strlen(file) + 2;
532 novm("init file name");
533 slprintf(path, pl, "%s/%s", user, file);
534 ret = options_from_file(path, 0, 1, privileged);
540 * options_for_tty - See if an options file exists for the serial
541 * device, and if so, interpret options from it.
546 char *dev, *path, *p;
551 if (strncmp(dev, "/dev/", 5) == 0)
553 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
554 return 1; /* don't look for /etc/ppp/options.tty */
555 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
558 novm("tty init file name");
559 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
560 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
561 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
564 ret = options_from_file(path, 0, 0, 1);
570 * options_from_list - process a string of options in a wordlist.
573 options_from_list(w, priv)
581 privileged_option = priv;
582 option_source = "secrets file";
586 * First see if it's a command.
588 opt = find_option(w->word);
590 int n = n_arguments(opt);
591 struct wordlist *w0 = w;
592 for (i = 0; i < n; ++i) {
596 "In secrets file: too few parameters for option '%s'",
602 current_option = w0->word;
603 if (!process_option(opt, argv))
609 * Maybe a tty name, speed or IP address?
611 if ((i = setdevname(w->word)) == 0
612 && (i = setspeed(w->word)) == 0
613 && (i = setipaddr(w->word)) == 0) {
614 option_error("In secrets file: unrecognized option '%s'",
618 if (i < 0) /* error */
628 * find_option - scan the option lists for the various protocols
629 * looking for an entry with the given name.
630 * This could be optimized by using a hash table.
637 struct option_list *list;
640 for (list = extra_options; list != NULL; list = list->next)
641 for (opt = list->options; opt->name != NULL; ++opt)
642 if (strcmp(name, opt->name) == 0)
644 for (opt = general_options; opt->name != NULL; ++opt)
645 if (strcmp(name, opt->name) == 0)
647 for (opt = auth_options; opt->name != NULL; ++opt)
648 if (strcmp(name, opt->name) == 0)
650 for (i = 0; protocols[i] != NULL; ++i)
651 if ((opt = protocols[i]->options) != NULL)
652 for (; opt->name != NULL; ++opt)
653 if (strcmp(name, opt->name) == 0)
659 * process_option - process one new-style option.
662 process_option(opt, argv)
669 int (*parser) __P((char **));
671 if ((opt->flags & OPT_PREPASS) == 0 && prepass)
673 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
674 option_error("it's too late to use the %s option", opt->name);
677 if ((opt->flags & OPT_PRIV) && !privileged_option) {
678 option_error("using the %s option requires root privilege", opt->name);
681 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
682 option_error("%s option is disabled", opt->name);
685 if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
686 struct option_info *ip = (struct option_info *) opt->addr2;
687 if (ip && ip->priv) {
688 option_error("%s option cannot be overridden", opt->name);
695 v = opt->flags & OPT_VALUE;
696 *(bool *)(opt->addr) = v;
697 if (opt->addr2 && (opt->flags & OPT_A2COPY))
698 *(bool *)(opt->addr2) = v;
703 if ((opt->flags & OPT_NOARG) == 0) {
704 if (!int_option(*argv, &iv))
706 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
707 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
708 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
709 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
710 switch (opt->flags & OPT_LIMITS) {
712 option_error("%s value must be%s >= %d",
713 opt->name, zok, opt->lower_limit);
716 option_error("%s value must be%s <= %d",
717 opt->name, zok, opt->upper_limit);
720 option_error("%s value must be%s between %d and %d",
721 opt->name, opt->lower_limit, opt->upper_limit);
727 a = opt->flags & OPT_VALUE;
729 a -= 256; /* sign extend */
731 if (opt->flags & OPT_INC)
732 iv += *(int *)(opt->addr);
733 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
734 int oldv = *(int *)(opt->addr);
735 if ((opt->flags & OPT_ZEROINF) ?
736 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
737 option_error("%s value cannot be increased", opt->name);
741 *(int *)(opt->addr) = iv;
742 if (opt->addr2 && (opt->flags & OPT_A2COPY))
743 *(int *)(opt->addr2) = iv;
747 if (opt->flags & OPT_NOARG) {
748 v = opt->flags & OPT_VALUE;
749 } else if (!number_option(*argv, &v, 16))
751 if (opt->flags & OPT_OR)
752 v |= *(u_int32_t *)(opt->addr);
753 *(u_int32_t *)(opt->addr) = v;
754 if (opt->addr2 && (opt->flags & OPT_A2COPY))
755 *(u_int32_t *)(opt->addr2) = v;
759 if (opt->flags & OPT_STATIC) {
760 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
764 novm("option argument");
765 *(char **)(opt->addr) = sv;
769 case o_special_noarg:
771 parser = (int (*) __P((char **))) opt->addr;
772 if (!(*parser)(argv))
778 if (opt->flags & OPT_A2INFO) {
779 struct option_info *ip = (struct option_info *) opt->addr2;
780 ip->priv = privileged_option;
781 ip->source = option_source;
782 } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
783 *(bool *)(opt->addr2) = 1;
790 * n_arguments - tell how many arguments an option takes
796 return (opt->type == o_bool || opt->type == o_special_noarg
797 || (opt->flags & OPT_NOARG))? 0: 1;
801 * add_options - add a list of options to the set we grok.
807 struct option_list *list;
809 list = malloc(sizeof(*list));
811 novm("option list entry");
813 list->next = extra_options;
814 extra_options = list;
818 * usage - print out a message telling how to use the program.
823 if (phase == PHASE_INITIALIZE)
824 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
829 * showhelp - print out usage message and exit.
835 if (phase == PHASE_INITIALIZE) {
843 * showversion - print out the version number and exit.
849 if (phase == PHASE_INITIALIZE) {
850 fprintf(stderr, "pppd version %s.%d%s\n",
851 VERSION, PATCHLEVEL, IMPLEMENTATION);
858 * option_error - print a message about an error in an option.
859 * The message is logged, and also sent to
860 * stderr if phase == PHASE_INITIALIZE.
863 option_error __V((char *fmt, ...))
868 #if defined(__STDC__)
873 fmt = va_arg(args, char *);
879 vslprintf(buf, sizeof(buf), fmt, args);
881 if (phase == PHASE_INITIALIZE)
882 fprintf(stderr, "%s: %s\n", progname, buf);
883 syslog(LOG_ERR, "%s", buf);
888 * readable - check if a file is readable by the real user.
901 if (fstat(fd, &sbuf) != 0)
903 if (sbuf.st_uid == uid)
904 return sbuf.st_mode & S_IRUSR;
905 if (sbuf.st_gid == getgid())
906 return sbuf.st_mode & S_IRGRP;
907 for (i = 0; i < ngroups; ++i)
908 if (sbuf.st_gid == groups[i])
909 return sbuf.st_mode & S_IRGRP;
910 return sbuf.st_mode & S_IROTH;
915 * Read a word from a file.
916 * Words are delimited by white-space or by quotes (" or ').
917 * Quotes, white-space and \ may be escaped with \.
918 * \<newline> is ignored.
921 getword(f, word, newlinep, filename)
929 int value, digit, got, n;
931 #define isoctal(c) ((c) >= '0' && (c) < '8')
939 * First skip white-space and comments.
947 * A newline means the end of a comment; backslash-newline
948 * is ignored. Note that we cannot have escape && comment.
960 * Ignore characters other than newline in a comment.
966 * If this character is escaped, we have a word start.
972 * If this is the escape character, look at the next character.
980 * If this is the start of a comment, ignore the rest of the line.
988 * A non-whitespace character is the start of a word.
995 * Save the delimiter for quoted strings.
997 if (!escape && (c == '"' || c == '\'')) {
1004 * Process characters until the end of the word.
1009 * This character is escaped: backslash-newline is ignored,
1010 * various other characters indicate particular values
1011 * as for C backslash-escapes.
1046 * \ddd octal sequence
1049 for (n = 0; n < 3 && isoctal(c); ++n) {
1050 value = (value << 3) + (c & 07);
1059 * \x<hex_string> sequence
1063 for (n = 0; n < 2 && isxdigit(c); ++n) {
1064 digit = toupper(c) - '0';
1066 digit += '0' + 10 - 'A';
1067 value = (value << 4) + digit;
1075 * Otherwise the character stands for itself.
1082 * Store the resulting character for the escape sequence.
1084 if (len < MAXWORDLEN-1)
1095 * Not escaped: see if we've reached the end of the word.
1101 if (isspace(c) || c == '#') {
1108 * Backslash starts an escape sequence.
1117 * An ordinary character: store it in the word and get another.
1119 if (len < MAXWORDLEN-1)
1127 * End of the word: check for errors.
1133 option_error("Error reading %s: %m", filename);
1137 * If len is zero, then we didn't find a word before the
1145 * Warn if the word was too long, and append a terminating null.
1147 if (len >= MAXWORDLEN) {
1148 option_error("warning: word in file %s too long (%.20s...)",
1150 len = MAXWORDLEN - 1;
1161 * number_option - parse an unsigned numeric parameter for an option.
1164 number_option(str, valp, base)
1171 *valp = strtoul(str, &ptr, base);
1173 option_error("invalid numeric parameter '%s' for %s option",
1174 str, current_option);
1182 * int_option - like number_option, but valp is int *,
1183 * the base is assumed to be 0, and *valp is not changed
1184 * if there is an error.
1187 int_option(str, valp)
1193 if (!number_option(str, &v, 0))
1201 * The following procedures parse options.
1205 * readfile - take commands from a file.
1211 return options_from_file(*argv, 1, 1, privileged_option);
1215 * callfile - take commands from /etc/ppp/peers/<name>.
1216 * Name may not contain /../, start with / or ../, or end in /..
1222 char *fname, *arg, *p;
1227 if (arg[0] == '/' || arg[0] == 0)
1230 for (p = arg; *p != 0; ) {
1231 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1235 while (*p != '/' && *p != 0)
1242 option_error("call option value may not contain .. or start with /");
1246 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1247 if ((fname = (char *) malloc(l)) == NULL)
1248 novm("call file name");
1249 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1251 ok = options_from_file(fname, 1, 1, 1);
1259 * setpdebug - Set libpcap debugging level.
1265 return int_option(*argv, &dflag);
1269 * setpassfilter - Set the pass filter for packets
1275 pc.linktype = DLT_PPP;
1276 pc.snapshot = PPP_HDRLEN;
1278 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1280 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1285 * setactivefilter - Set the active filter for packets
1288 setactivefilter(argv)
1291 pc.linktype = DLT_PPP;
1292 pc.snapshot = PPP_HDRLEN;
1294 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1296 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1302 * noopt - Disable all options.
1308 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1309 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1310 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1311 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1317 * setdomain - Set domain name to append to hostname
1323 if (!privileged_option) {
1324 option_error("using the domain option requires root privilege");
1327 gethostname(hostname, MAXNAMELEN);
1330 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1331 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1333 hostname[MAXNAMELEN-1] = 0;
1339 * setspeed - Set the speed.
1350 spd = strtol(arg, &ptr, 0);
1351 if (ptr == arg || *ptr != 0 || spd == 0)
1359 * setdevname - Set the device name.
1365 struct stat statbuf;
1366 char dev[MAXPATHLEN];
1371 if (strncmp("/dev/", cp, 5) != 0) {
1372 strlcpy(dev, "/dev/", sizeof(dev));
1373 strlcat(dev, cp, sizeof(dev));
1378 * Check if there is a character device by this name.
1380 if (stat(cp, &statbuf) < 0) {
1381 if (errno == ENOENT)
1383 option_error("Couldn't stat %s: %m", cp);
1386 if (!S_ISCHR(statbuf.st_mode)) {
1387 option_error("%s is not a character device", cp);
1391 if (phase != PHASE_INITIALIZE) {
1392 option_error("device name cannot be changed after initialization");
1394 } else if (devnam_fixed) {
1395 option_error("per-tty options file may not specify device name");
1399 if (devnam_info.priv && !privileged_option) {
1400 option_error("device name cannot be overridden");
1404 strlcpy(devnam, cp, sizeof(devnam));
1407 devnam_info.priv = privileged_option;
1408 devnam_info.source = option_source;
1415 * setipaddr - Set the IP address
1423 u_int32_t local, remote;
1424 ipcp_options *wo = &ipcp_wantoptions[0];
1427 * IP address pair separated by ":".
1429 if ((colon = strchr(arg, ':')) == NULL)
1435 * If colon first character, then no local addr.
1439 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
1440 if ((hp = gethostbyname(arg)) == NULL) {
1441 option_error("unknown host: %s", arg);
1444 local = *(u_int32_t *)hp->h_addr;
1447 if (bad_ip_adrs(local)) {
1448 option_error("bad local IP address %s", ip_ntoa(local));
1452 wo->ouraddr = local;
1457 * If colon last character, then no remote addr.
1459 if (*++colon != '\0') {
1460 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
1461 if ((hp = gethostbyname(colon)) == NULL) {
1462 option_error("unknown host: %s", colon);
1465 remote = *(u_int32_t *)hp->h_addr;
1466 if (remote_name[0] == 0)
1467 strlcpy(remote_name, colon, sizeof(remote_name));
1470 if (bad_ip_adrs(remote)) {
1471 option_error("bad remote IP address %s", ip_ntoa(remote));
1475 wo->hisaddr = remote;
1483 * setnetmask - set the netmask to be used on the interface.
1494 * Unfortunately, if we use inet_addr, we can't tell whether
1495 * a result of all 1s is an error or a valid 255.255.255.255.
1501 b = strtoul(p, &endp, 0);
1506 /* accept e.g. 0xffffff00 */
1512 mask |= b << (n * 8);
1514 if (*p != '.' || n == 0)
1521 if (*p != 0 || (netmask & ~mask) != 0) {
1522 option_error("invalid netmask value '%s'", *argv);
1534 lcp_wantoptions[0].asyncmap |= 0x000A0000; /* escape ^S and ^Q */
1535 lcp_wantoptions[0].neg_asyncmap = 1;
1547 if (!privileged_option)
1549 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1550 if (fd < 0 && errno == EEXIST)
1551 fd = open(*argv, O_WRONLY | O_APPEND);
1553 if (!privileged_option)
1557 option_error("Can't open log file %s: %m", *argv);
1560 if (log_to_file && log_to_fd >= 0)
1575 void (*init) __P((void));
1577 handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
1581 option_error("%s", err);
1582 option_error("Couldn't load plugin %s", arg);
1585 init = dlsym(handle, "plugin_init");
1587 option_error("%s has no initialization entry point", arg);
1591 info("Plugin %s loaded.", arg);