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.72 2000/04/04 07:06:52 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 */
108 char *bundle_name = NULL; /* bundle name for multilink */
110 extern option_t auth_options[];
111 extern struct stat devstat;
112 extern int prepass; /* Doing pre-pass to find device name */
114 struct option_info initializer_info;
115 struct option_info connect_script_info;
116 struct option_info disconnect_script_info;
117 struct option_info welcomer_info;
118 struct option_info devnam_info;
119 struct option_info ptycommand_info;
122 struct bpf_program pass_filter;/* Filter program for packets to pass */
123 struct bpf_program active_filter; /* Filter program for link-active pkts */
124 pcap_t pc; /* Fake struct pcap so we can compile expr */
127 char *current_option; /* the name of the option being parsed */
128 int privileged_option; /* set iff the current option came from root */
129 char *option_source; /* string saying where the option came from */
130 bool log_to_file; /* log_to_fd is a file opened by us */
135 static int setdevname __P((char *));
136 static int setipaddr __P((char *));
137 static int setspeed __P((char *));
138 static int noopt __P((char **));
139 static int setdomain __P((char **));
140 static int setnetmask __P((char **));
141 static int setxonxoff __P((char **));
142 static int readfile __P((char **));
143 static int callfile __P((char **));
144 static int showversion __P((char **));
145 static int showhelp __P((char **));
146 static void usage __P((void));
147 static int setlogfile __P((char **));
149 static int loadplugin __P((char **));
153 static int setpassfilter __P((char **));
154 static int setactivefilter __P((char **));
157 static option_t *find_option __P((char *name));
158 static int process_option __P((option_t *, char **));
159 static int n_arguments __P((option_t *));
160 static int number_option __P((char *, u_int32_t *, int));
163 * Structure to store extra lists of options.
167 struct option_list *next;
170 static struct option_list *extra_options = NULL;
175 option_t general_options[] = {
176 { "debug", o_int, &debug,
177 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
178 { "-d", o_int, &debug,
179 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
180 { "kdebug", o_int, &kdebugflag,
181 "Set kernel driver debug level" },
182 { "nodetach", o_bool, &nodetach,
183 "Don't detach from controlling tty", 1 },
184 { "-detach", o_bool, &nodetach,
185 "Don't detach from controlling tty", 1 },
186 { "updetach", o_bool, &updetach,
187 "Detach from controlling tty once link is up", 1 },
188 { "holdoff", o_int, &holdoff,
189 "Set time in seconds before retrying connection" },
190 { "idle", o_int, &idle_time_limit,
191 "Set time in seconds before disconnecting idle link" },
192 { "lock", o_bool, &lockflag,
193 "Lock serial device with UUCP-style lock file", 1 },
194 { "-all", o_special_noarg, noopt,
195 "Don't request/allow any LCP or IPCP options (useless)" },
196 { "init", o_string, &initializer,
197 "A program to initialize the device",
198 OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
199 { "connect", o_string, &connect_script,
200 "A program to set up a connection",
201 OPT_A2INFO | OPT_PRIVFIX, &connect_script_info },
202 { "disconnect", o_string, &disconnect_script,
203 "Program to disconnect serial device",
204 OPT_A2INFO | OPT_PRIVFIX, &disconnect_script_info },
205 { "welcome", o_string, &welcomer,
206 "Script to welcome client",
207 OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
208 { "pty", o_string, &ptycommand,
209 "Script to run on pseudo-tty master side",
210 OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
211 { "notty", o_bool, ¬ty,
212 "Input/output is not a tty", OPT_DEVNAM | 1 },
213 { "socket", o_string, &pty_socket,
214 "Send and receive over socket, arg is host:port", OPT_DEVNAM },
215 { "record", o_string, &record_file,
216 "Record characters sent/received to file" },
217 { "maxconnect", o_int, &maxconnect,
218 "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
219 { "crtscts", o_int, &crtscts,
220 "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
221 { "nocrtscts", o_int, &crtscts,
222 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
223 { "-crtscts", o_int, &crtscts,
224 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
225 { "cdtrcts", o_int, &crtscts,
226 "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
227 { "nocdtrcts", o_int, &crtscts,
228 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
229 { "xonxoff", o_special_noarg, setxonxoff,
230 "Set software (XON/XOFF) flow control" },
231 { "domain", o_special, setdomain,
232 "Add given domain name to hostname" },
233 { "mtu", o_int, &lcp_allowoptions[0].mru,
234 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
235 { "netmask", o_special, setnetmask,
237 { "modem", o_bool, &modem,
238 "Use modem control lines", 1 },
239 { "local", o_bool, &modem,
240 "Don't use modem control lines" },
241 { "file", o_special, readfile,
242 "Take options from a file", OPT_PREPASS },
243 { "call", o_special, callfile,
244 "Take options from a privileged file", OPT_PREPASS },
245 { "persist", o_bool, &persist,
246 "Keep on reopening connection after close", 1 },
247 { "nopersist", o_bool, &persist,
248 "Turn off persist option" },
249 { "demand", o_bool, &demand,
250 "Dial on demand", OPT_INITONLY | 1, &persist },
251 { "--version", o_special_noarg, showversion,
252 "Show version number" },
253 { "--help", o_special_noarg, showhelp,
254 "Show brief listing of options" },
255 { "-h", o_special_noarg, showhelp,
256 "Show brief listing of options" },
257 { "sync", o_bool, &sync_serial,
258 "Use synchronous HDLC serial encoding", 1 },
259 { "logfd", o_int, &log_to_fd,
260 "Send log messages to this file descriptor" },
261 { "logfile", o_special, setlogfile,
262 "Append log messages to this file" },
263 { "nolog", o_int, &log_to_fd,
264 "Don't send log messages to any file",
265 OPT_NOARG | OPT_VAL(-1) },
266 { "nologfd", o_int, &log_to_fd,
267 "Don't send log messages to any file descriptor",
268 OPT_NOARG | OPT_VAL(-1) },
269 { "linkname", o_string, linkname,
270 "Set logical name for link",
271 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
272 { "maxfail", o_int, &maxfail,
273 "Maximum number of unsuccessful connection attempts to allow" },
274 { "ktune", o_bool, &tune_kernel,
275 "Alter kernel settings as necessary", 1 },
276 { "noktune", o_bool, &tune_kernel,
277 "Don't alter kernel settings", 0 },
278 { "connect-delay", o_int, &connect_delay,
279 "Maximum time (in ms) to wait after connect script finishes" },
280 { "datarate", o_int, &max_data_rate,
281 "Maximum data rate in bytes/sec (with pty, notty or record option)" },
282 { "unit", o_int, &req_unit,
283 "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
284 #ifdef HAVE_MULTILINK
285 { "multilink", o_bool, &multilink,
286 "Enable multilink operation", 1 },
287 { "nomultilink", o_bool, &multilink,
288 "Disable multilink operation", 0 },
289 { "mp", o_bool, &multilink,
290 "Enable multilink operation", 1 },
291 { "nomp", o_bool, &multilink,
292 "Disable multilink operation", 0 },
293 { "bundle", o_string, &bundle_name,
294 "Bundle name for multilink" },
295 #endif /* HAVE_MULTILINK */
297 { "plugin", o_special, loadplugin,
298 "Load a plug-in module into pppd", OPT_PRIV },
302 { "pdebug", o_int, &dflag,
303 "libpcap debugging" },
304 { "pass-filter", 1, setpassfilter,
305 "set filter for packets to pass" },
306 { "active-filter", 1, setactivefilter,
307 "set filter for active pkts" },
313 #ifndef IMPLEMENTATION
314 #define IMPLEMENTATION ""
317 static char *usage_string = "\
318 pppd version %s.%d%s\n\
319 Usage: %s [ options ], where options are:\n\
320 <device> Communicate over the named device\n\
321 <speed> Set the baud rate to <speed>\n\
322 <loc>:<rem> Set the local and/or remote interface IP\n\
323 addresses. Either one may be omitted.\n\
324 asyncmap <n> Set the desired async map to hex <n>\n\
325 auth Require authentication from peer\n\
326 connect <p> Invoke shell command <p> to set up the serial line\n\
327 crtscts Use hardware RTS/CTS flow control\n\
328 defaultroute Add default route through interface\n\
329 file <f> Take options from file <f>\n\
330 modem Use modem control lines\n\
331 mru <n> Set MRU value to <n> for negotiation\n\
332 See pppd(8) for more options.\n\
336 * parse_args - parse a string of arguments from the command line.
337 * If prepass is true, we are scanning for the device name and only
338 * processing a few options, so error messages are suppressed.
341 parse_args(argc, argv)
349 privileged_option = privileged;
350 option_source = "command line";
356 * First see if it's an option in the new option list.
358 opt = find_option(arg);
360 int n = n_arguments(opt);
362 option_error("too few parameters for option %s", arg);
365 current_option = arg;
366 if (!process_option(opt, argv))
374 * Maybe a tty name, speed or IP address?
376 if ((ret = setdevname(arg)) == 0
377 && (ret = setspeed(arg)) == 0
378 && (ret = setipaddr(arg)) == 0
380 option_error("unrecognized option '%s'", arg);
384 if (ret < 0) /* error */
392 * scan_args - scan the command line arguments to get the tty name,
393 * if specified. Also checks whether the notty or pty option was given.
396 scan_args(argc, argv)
403 privileged_option = privileged;
408 if (strcmp(arg, "notty") == 0 || strcmp(arg, "pty") == 0)
411 /* Skip options and their arguments */
412 opt = find_option(arg);
414 int n = n_arguments(opt);
420 /* Check if it's a tty name and copy it if so */
421 (void) setdevname(arg, 1);
427 * options_from_file - Read a string of options from a file,
428 * and interpret them.
431 options_from_file(filename, must_exist, check_prot, priv)
438 int i, newline, ret, err;
443 char args[MAXARGS][MAXWORDLEN];
444 char cmd[MAXWORDLEN];
448 f = fopen(filename, "r");
453 if (!must_exist && err == ENOENT)
456 option_error("Can't open options file %s: %m", filename);
460 oldpriv = privileged_option;
461 privileged_option = priv;
462 oldsource = option_source;
463 option_source = strdup(filename);
464 if (option_source == NULL)
465 option_source = "file";
467 while (getword(f, cmd, &newline, filename)) {
469 * First see if it's a command.
471 opt = find_option(cmd);
473 int n = n_arguments(opt);
474 for (i = 0; i < n; ++i) {
475 if (!getword(f, args[i], &newline, filename)) {
477 "In file %s: too few parameters for option '%s'",
483 current_option = cmd;
484 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
485 option_error("the %s option may not be used in the %s file",
489 if (!process_option(opt, argv))
495 * Maybe a tty name, speed or IP address?
497 if ((i = setdevname(cmd)) == 0
498 && (i = setspeed(cmd)) == 0
499 && (i = setipaddr(cmd)) == 0) {
500 option_error("In file %s: unrecognized option '%s'",
504 if (i < 0) /* error */
511 privileged_option = oldpriv;
512 option_source = oldsource;
517 * options_from_user - See if the use has a ~/.ppprc file,
518 * and if so, interpret options from it.
523 char *user, *path, *file;
528 pw = getpwuid(getuid());
529 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
531 file = _PATH_USEROPT;
532 pl = strlen(user) + strlen(file) + 2;
535 novm("init file name");
536 slprintf(path, pl, "%s/%s", user, file);
537 ret = options_from_file(path, 0, 1, privileged);
543 * options_for_tty - See if an options file exists for the serial
544 * device, and if so, interpret options from it.
549 char *dev, *path, *p;
554 if (strncmp(dev, "/dev/", 5) == 0)
556 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
557 return 1; /* don't look for /etc/ppp/options.tty */
558 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
561 novm("tty init file name");
562 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
563 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
564 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
567 ret = options_from_file(path, 0, 0, 1);
573 * options_from_list - process a string of options in a wordlist.
576 options_from_list(w, priv)
584 privileged_option = priv;
585 option_source = "secrets file";
589 * First see if it's a command.
591 opt = find_option(w->word);
593 int n = n_arguments(opt);
594 struct wordlist *w0 = w;
595 for (i = 0; i < n; ++i) {
599 "In secrets file: too few parameters for option '%s'",
605 current_option = w0->word;
606 if (!process_option(opt, argv))
612 * Maybe a tty name, speed or IP address?
614 if ((i = setdevname(w->word)) == 0
615 && (i = setspeed(w->word)) == 0
616 && (i = setipaddr(w->word)) == 0) {
617 option_error("In secrets file: unrecognized option '%s'",
621 if (i < 0) /* error */
631 * find_option - scan the option lists for the various protocols
632 * looking for an entry with the given name.
633 * This could be optimized by using a hash table.
640 struct option_list *list;
643 for (list = extra_options; list != NULL; list = list->next)
644 for (opt = list->options; opt->name != NULL; ++opt)
645 if (strcmp(name, opt->name) == 0)
647 for (opt = general_options; opt->name != NULL; ++opt)
648 if (strcmp(name, opt->name) == 0)
650 for (opt = auth_options; opt->name != NULL; ++opt)
651 if (strcmp(name, opt->name) == 0)
653 for (i = 0; protocols[i] != NULL; ++i)
654 if ((opt = protocols[i]->options) != NULL)
655 for (; opt->name != NULL; ++opt)
656 if (strcmp(name, opt->name) == 0)
662 * process_option - process one new-style option.
665 process_option(opt, argv)
672 int (*parser) __P((char **));
674 if ((opt->flags & OPT_PREPASS) == 0 && prepass)
676 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
677 option_error("it's too late to use the %s option", opt->name);
680 if ((opt->flags & OPT_PRIV) && !privileged_option) {
681 option_error("using the %s option requires root privilege", opt->name);
684 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
685 option_error("%s option is disabled", opt->name);
688 if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
689 struct option_info *ip = (struct option_info *) opt->addr2;
690 if (ip && ip->priv) {
691 option_error("%s option cannot be overridden", opt->name);
698 v = opt->flags & OPT_VALUE;
699 *(bool *)(opt->addr) = v;
700 if (opt->addr2 && (opt->flags & OPT_A2COPY))
701 *(bool *)(opt->addr2) = v;
706 if ((opt->flags & OPT_NOARG) == 0) {
707 if (!int_option(*argv, &iv))
709 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
710 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
711 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
712 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
713 switch (opt->flags & OPT_LIMITS) {
715 option_error("%s value must be%s >= %d",
716 opt->name, zok, opt->lower_limit);
719 option_error("%s value must be%s <= %d",
720 opt->name, zok, opt->upper_limit);
723 option_error("%s value must be%s between %d and %d",
724 opt->name, opt->lower_limit, opt->upper_limit);
730 a = opt->flags & OPT_VALUE;
732 a -= 256; /* sign extend */
734 if (opt->flags & OPT_INC)
735 iv += *(int *)(opt->addr);
736 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
737 int oldv = *(int *)(opt->addr);
738 if ((opt->flags & OPT_ZEROINF) ?
739 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
740 option_error("%s value cannot be increased", opt->name);
744 *(int *)(opt->addr) = iv;
745 if (opt->addr2 && (opt->flags & OPT_A2COPY))
746 *(int *)(opt->addr2) = iv;
750 if (opt->flags & OPT_NOARG) {
751 v = opt->flags & OPT_VALUE;
752 } else if (!number_option(*argv, &v, 16))
754 if (opt->flags & OPT_OR)
755 v |= *(u_int32_t *)(opt->addr);
756 *(u_int32_t *)(opt->addr) = v;
757 if (opt->addr2 && (opt->flags & OPT_A2COPY))
758 *(u_int32_t *)(opt->addr2) = v;
762 if (opt->flags & OPT_STATIC) {
763 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
767 novm("option argument");
768 *(char **)(opt->addr) = sv;
772 case o_special_noarg:
774 parser = (int (*) __P((char **))) opt->addr;
775 if (!(*parser)(argv))
781 if (opt->flags & OPT_A2INFO) {
782 struct option_info *ip = (struct option_info *) opt->addr2;
783 ip->priv = privileged_option;
784 ip->source = option_source;
785 } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
786 *(bool *)(opt->addr2) = 1;
793 * n_arguments - tell how many arguments an option takes
799 return (opt->type == o_bool || opt->type == o_special_noarg
800 || (opt->flags & OPT_NOARG))? 0: 1;
804 * add_options - add a list of options to the set we grok.
810 struct option_list *list;
812 list = malloc(sizeof(*list));
814 novm("option list entry");
816 list->next = extra_options;
817 extra_options = list;
821 * usage - print out a message telling how to use the program.
826 if (phase == PHASE_INITIALIZE)
827 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
832 * showhelp - print out usage message and exit.
838 if (phase == PHASE_INITIALIZE) {
846 * showversion - print out the version number and exit.
852 if (phase == PHASE_INITIALIZE) {
853 fprintf(stderr, "pppd version %s.%d%s\n",
854 VERSION, PATCHLEVEL, IMPLEMENTATION);
861 * option_error - print a message about an error in an option.
862 * The message is logged, and also sent to
863 * stderr if phase == PHASE_INITIALIZE.
866 option_error __V((char *fmt, ...))
871 #if defined(__STDC__)
876 fmt = va_arg(args, char *);
882 vslprintf(buf, sizeof(buf), fmt, args);
884 if (phase == PHASE_INITIALIZE)
885 fprintf(stderr, "%s: %s\n", progname, buf);
886 syslog(LOG_ERR, "%s", buf);
891 * readable - check if a file is readable by the real user.
904 if (fstat(fd, &sbuf) != 0)
906 if (sbuf.st_uid == uid)
907 return sbuf.st_mode & S_IRUSR;
908 if (sbuf.st_gid == getgid())
909 return sbuf.st_mode & S_IRGRP;
910 for (i = 0; i < ngroups; ++i)
911 if (sbuf.st_gid == groups[i])
912 return sbuf.st_mode & S_IRGRP;
913 return sbuf.st_mode & S_IROTH;
918 * Read a word from a file.
919 * Words are delimited by white-space or by quotes (" or ').
920 * Quotes, white-space and \ may be escaped with \.
921 * \<newline> is ignored.
924 getword(f, word, newlinep, filename)
932 int value, digit, got, n;
934 #define isoctal(c) ((c) >= '0' && (c) < '8')
942 * First skip white-space and comments.
950 * A newline means the end of a comment; backslash-newline
951 * is ignored. Note that we cannot have escape && comment.
963 * Ignore characters other than newline in a comment.
969 * If this character is escaped, we have a word start.
975 * If this is the escape character, look at the next character.
983 * If this is the start of a comment, ignore the rest of the line.
991 * A non-whitespace character is the start of a word.
998 * Save the delimiter for quoted strings.
1000 if (!escape && (c == '"' || c == '\'')) {
1007 * Process characters until the end of the word.
1012 * This character is escaped: backslash-newline is ignored,
1013 * various other characters indicate particular values
1014 * as for C backslash-escapes.
1049 * \ddd octal sequence
1052 for (n = 0; n < 3 && isoctal(c); ++n) {
1053 value = (value << 3) + (c & 07);
1062 * \x<hex_string> sequence
1066 for (n = 0; n < 2 && isxdigit(c); ++n) {
1067 digit = toupper(c) - '0';
1069 digit += '0' + 10 - 'A';
1070 value = (value << 4) + digit;
1078 * Otherwise the character stands for itself.
1085 * Store the resulting character for the escape sequence.
1087 if (len < MAXWORDLEN-1)
1098 * Not escaped: see if we've reached the end of the word.
1104 if (isspace(c) || c == '#') {
1111 * Backslash starts an escape sequence.
1120 * An ordinary character: store it in the word and get another.
1122 if (len < MAXWORDLEN-1)
1130 * End of the word: check for errors.
1136 option_error("Error reading %s: %m", filename);
1140 * If len is zero, then we didn't find a word before the
1148 * Warn if the word was too long, and append a terminating null.
1150 if (len >= MAXWORDLEN) {
1151 option_error("warning: word in file %s too long (%.20s...)",
1153 len = MAXWORDLEN - 1;
1164 * number_option - parse an unsigned numeric parameter for an option.
1167 number_option(str, valp, base)
1174 *valp = strtoul(str, &ptr, base);
1176 option_error("invalid numeric parameter '%s' for %s option",
1177 str, current_option);
1185 * int_option - like number_option, but valp is int *,
1186 * the base is assumed to be 0, and *valp is not changed
1187 * if there is an error.
1190 int_option(str, valp)
1196 if (!number_option(str, &v, 0))
1204 * The following procedures parse options.
1208 * readfile - take commands from a file.
1214 return options_from_file(*argv, 1, 1, privileged_option);
1218 * callfile - take commands from /etc/ppp/peers/<name>.
1219 * Name may not contain /../, start with / or ../, or end in /..
1225 char *fname, *arg, *p;
1230 if (arg[0] == '/' || arg[0] == 0)
1233 for (p = arg; *p != 0; ) {
1234 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1238 while (*p != '/' && *p != 0)
1245 option_error("call option value may not contain .. or start with /");
1249 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1250 if ((fname = (char *) malloc(l)) == NULL)
1251 novm("call file name");
1252 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1254 ok = options_from_file(fname, 1, 1, 1);
1262 * setpdebug - Set libpcap debugging level.
1268 return int_option(*argv, &dflag);
1272 * setpassfilter - Set the pass filter for packets
1278 pc.linktype = DLT_PPP;
1279 pc.snapshot = PPP_HDRLEN;
1281 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1283 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1288 * setactivefilter - Set the active filter for packets
1291 setactivefilter(argv)
1294 pc.linktype = DLT_PPP;
1295 pc.snapshot = PPP_HDRLEN;
1297 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1299 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1305 * noopt - Disable all options.
1311 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1312 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1313 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1314 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1320 * setdomain - Set domain name to append to hostname
1326 if (!privileged_option) {
1327 option_error("using the domain option requires root privilege");
1330 gethostname(hostname, MAXNAMELEN);
1333 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1334 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1336 hostname[MAXNAMELEN-1] = 0;
1342 * setspeed - Set the speed.
1353 spd = strtol(arg, &ptr, 0);
1354 if (ptr == arg || *ptr != 0 || spd == 0)
1362 * setdevname - Set the device name.
1368 struct stat statbuf;
1369 char dev[MAXPATHLEN];
1374 if (strncmp("/dev/", cp, 5) != 0) {
1375 strlcpy(dev, "/dev/", sizeof(dev));
1376 strlcat(dev, cp, sizeof(dev));
1381 * Check if there is a character device by this name.
1383 if (stat(cp, &statbuf) < 0) {
1384 if (errno == ENOENT)
1386 option_error("Couldn't stat %s: %m", cp);
1389 if (!S_ISCHR(statbuf.st_mode)) {
1390 option_error("%s is not a character device", cp);
1394 if (phase != PHASE_INITIALIZE) {
1395 option_error("device name cannot be changed after initialization");
1397 } else if (devnam_fixed) {
1398 option_error("per-tty options file may not specify device name");
1402 if (devnam_info.priv && !privileged_option) {
1403 option_error("device name cannot be overridden");
1407 strlcpy(devnam, cp, sizeof(devnam));
1410 devnam_info.priv = privileged_option;
1411 devnam_info.source = option_source;
1418 * setipaddr - Set the IP address
1426 u_int32_t local, remote;
1427 ipcp_options *wo = &ipcp_wantoptions[0];
1430 * IP address pair separated by ":".
1432 if ((colon = strchr(arg, ':')) == NULL)
1438 * If colon first character, then no local addr.
1442 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
1443 if ((hp = gethostbyname(arg)) == NULL) {
1444 option_error("unknown host: %s", arg);
1447 local = *(u_int32_t *)hp->h_addr;
1450 if (bad_ip_adrs(local)) {
1451 option_error("bad local IP address %s", ip_ntoa(local));
1455 wo->ouraddr = local;
1460 * If colon last character, then no remote addr.
1462 if (*++colon != '\0') {
1463 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
1464 if ((hp = gethostbyname(colon)) == NULL) {
1465 option_error("unknown host: %s", colon);
1468 remote = *(u_int32_t *)hp->h_addr;
1469 if (remote_name[0] == 0)
1470 strlcpy(remote_name, colon, sizeof(remote_name));
1473 if (bad_ip_adrs(remote)) {
1474 option_error("bad remote IP address %s", ip_ntoa(remote));
1478 wo->hisaddr = remote;
1486 * setnetmask - set the netmask to be used on the interface.
1497 * Unfortunately, if we use inet_addr, we can't tell whether
1498 * a result of all 1s is an error or a valid 255.255.255.255.
1501 n = parse_dotted_ip(p, &mask);
1505 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
1506 option_error("invalid netmask value '%s'", *argv);
1515 parse_dotted_ip(p, vp)
1521 char *endp, *p0 = p;
1525 b = strtoul(p, &endp, 0);
1531 /* accept e.g. 0xffffff00 */
1551 lcp_wantoptions[0].asyncmap |= 0x000A0000; /* escape ^S and ^Q */
1552 lcp_wantoptions[0].neg_asyncmap = 1;
1564 if (!privileged_option)
1566 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1567 if (fd < 0 && errno == EEXIST)
1568 fd = open(*argv, O_WRONLY | O_APPEND);
1570 if (!privileged_option)
1574 option_error("Can't open log file %s: %m", *argv);
1577 if (log_to_file && log_to_fd >= 0)
1592 void (*init) __P((void));
1594 handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
1598 option_error("%s", err);
1599 option_error("Couldn't load plugin %s", arg);
1602 init = dlsym(handle, "plugin_init");
1604 option_error("%s has no initialization entry point", arg);
1608 info("Plugin %s loaded.", arg);