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.74 2000/04/15 01:27:13 masputra 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 */
131 bool log_to_specific_fd; /* log_to_fd was specified by user option */
136 static int setdevname __P((char *));
137 static int setipaddr __P((char *));
138 static int setspeed __P((char *));
139 static int noopt __P((char **));
140 static int setdomain __P((char **));
141 static int setnetmask __P((char **));
142 static int setxonxoff __P((char **));
143 static int readfile __P((char **));
144 static int callfile __P((char **));
145 static int showversion __P((char **));
146 static int showhelp __P((char **));
147 static void usage __P((void));
148 static int setlogfile __P((char **));
150 static int loadplugin __P((char **));
154 static int setpassfilter __P((char **));
155 static int setactivefilter __P((char **));
158 static option_t *find_option __P((char *name));
159 static int process_option __P((option_t *, char **));
160 static int n_arguments __P((option_t *));
161 static int number_option __P((char *, u_int32_t *, int));
164 * Structure to store extra lists of options.
168 struct option_list *next;
171 static struct option_list *extra_options = NULL;
176 option_t general_options[] = {
177 { "debug", o_int, &debug,
178 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
179 { "-d", o_int, &debug,
180 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
181 { "kdebug", o_int, &kdebugflag,
182 "Set kernel driver debug level" },
183 { "nodetach", o_bool, &nodetach,
184 "Don't detach from controlling tty", 1 },
185 { "-detach", o_bool, &nodetach,
186 "Don't detach from controlling tty", 1 },
187 { "updetach", o_bool, &updetach,
188 "Detach from controlling tty once link is up", 1 },
189 { "holdoff", o_int, &holdoff,
190 "Set time in seconds before retrying connection" },
191 { "idle", o_int, &idle_time_limit,
192 "Set time in seconds before disconnecting idle link" },
193 { "lock", o_bool, &lockflag,
194 "Lock serial device with UUCP-style lock file", 1 },
195 { "-all", o_special_noarg, (void *)noopt,
196 "Don't request/allow any LCP or IPCP options (useless)" },
197 { "init", o_string, &initializer,
198 "A program to initialize the device",
199 OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
200 { "connect", o_string, &connect_script,
201 "A program to set up a connection",
202 OPT_A2INFO | OPT_PRIVFIX, &connect_script_info },
203 { "disconnect", o_string, &disconnect_script,
204 "Program to disconnect serial device",
205 OPT_A2INFO | OPT_PRIVFIX, &disconnect_script_info },
206 { "welcome", o_string, &welcomer,
207 "Script to welcome client",
208 OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
209 { "pty", o_string, &ptycommand,
210 "Script to run on pseudo-tty master side",
211 OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
212 { "notty", o_bool, ¬ty,
213 "Input/output is not a tty", OPT_DEVNAM | 1 },
214 { "socket", o_string, &pty_socket,
215 "Send and receive over socket, arg is host:port", OPT_DEVNAM },
216 { "record", o_string, &record_file,
217 "Record characters sent/received to file" },
218 { "maxconnect", o_int, &maxconnect,
219 "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
220 { "crtscts", o_int, &crtscts,
221 "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
222 { "nocrtscts", o_int, &crtscts,
223 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
224 { "-crtscts", o_int, &crtscts,
225 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
226 { "cdtrcts", o_int, &crtscts,
227 "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
228 { "nocdtrcts", o_int, &crtscts,
229 "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
230 { "xonxoff", o_special_noarg, (void *)setxonxoff,
231 "Set software (XON/XOFF) flow control" },
232 { "domain", o_special, (void *)setdomain,
233 "Add given domain name to hostname" },
234 { "mtu", o_int, &lcp_allowoptions[0].mru,
235 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
236 { "netmask", o_special, (void *)setnetmask,
238 { "modem", o_bool, &modem,
239 "Use modem control lines", 1 },
240 { "local", o_bool, &modem,
241 "Don't use modem control lines" },
242 { "file", o_special, (void *)readfile,
243 "Take options from a file", OPT_PREPASS },
244 { "call", o_special, (void *)callfile,
245 "Take options from a privileged file", OPT_PREPASS },
246 { "persist", o_bool, &persist,
247 "Keep on reopening connection after close", 1 },
248 { "nopersist", o_bool, &persist,
249 "Turn off persist option" },
250 { "demand", o_bool, &demand,
251 "Dial on demand", OPT_INITONLY | 1, &persist },
252 { "--version", o_special_noarg, (void *)showversion,
253 "Show version number" },
254 { "--help", o_special_noarg, (void *)showhelp,
255 "Show brief listing of options" },
256 { "-h", o_special_noarg, (void *)showhelp,
257 "Show brief listing of options" },
258 { "sync", o_bool, &sync_serial,
259 "Use synchronous HDLC serial encoding", 1 },
260 { "logfd", o_int, &log_to_fd,
261 "Send log messages to this file descriptor",
262 0, &log_to_specific_fd },
263 { "logfile", o_special, (void *)setlogfile,
264 "Append log messages to this file" },
265 { "nolog", o_int, &log_to_fd,
266 "Don't send log messages to any file",
267 OPT_NOARG | OPT_VAL(-1) },
268 { "nologfd", o_int, &log_to_fd,
269 "Don't send log messages to any file descriptor",
270 OPT_NOARG | OPT_VAL(-1) },
271 { "linkname", o_string, linkname,
272 "Set logical name for link",
273 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
274 { "maxfail", o_int, &maxfail,
275 "Maximum number of unsuccessful connection attempts to allow" },
276 { "ktune", o_bool, &tune_kernel,
277 "Alter kernel settings as necessary", 1 },
278 { "noktune", o_bool, &tune_kernel,
279 "Don't alter kernel settings", 0 },
280 { "connect-delay", o_int, &connect_delay,
281 "Maximum time (in ms) to wait after connect script finishes" },
282 { "datarate", o_int, &max_data_rate,
283 "Maximum data rate in bytes/sec (with pty, notty or record option)" },
284 { "unit", o_int, &req_unit,
285 "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
286 #ifdef HAVE_MULTILINK
287 { "multilink", o_bool, &multilink,
288 "Enable multilink operation", 1 },
289 { "nomultilink", o_bool, &multilink,
290 "Disable multilink operation", 0 },
291 { "mp", o_bool, &multilink,
292 "Enable multilink operation", 1 },
293 { "nomp", o_bool, &multilink,
294 "Disable multilink operation", 0 },
295 { "bundle", o_string, &bundle_name,
296 "Bundle name for multilink" },
297 #endif /* HAVE_MULTILINK */
299 { "plugin", o_special, (void *)loadplugin,
300 "Load a plug-in module into pppd", OPT_PRIV },
304 { "pdebug", o_int, &dflag,
305 "libpcap debugging" },
306 { "pass-filter", 1, setpassfilter,
307 "set filter for packets to pass" },
308 { "active-filter", 1, setactivefilter,
309 "set filter for active pkts" },
315 #ifndef IMPLEMENTATION
316 #define IMPLEMENTATION ""
319 static char *usage_string = "\
320 pppd version %s.%d%s\n\
321 Usage: %s [ options ], where options are:\n\
322 <device> Communicate over the named device\n\
323 <speed> Set the baud rate to <speed>\n\
324 <loc>:<rem> Set the local and/or remote interface IP\n\
325 addresses. Either one may be omitted.\n\
326 asyncmap <n> Set the desired async map to hex <n>\n\
327 auth Require authentication from peer\n\
328 connect <p> Invoke shell command <p> to set up the serial line\n\
329 crtscts Use hardware RTS/CTS flow control\n\
330 defaultroute Add default route through interface\n\
331 file <f> Take options from file <f>\n\
332 modem Use modem control lines\n\
333 mru <n> Set MRU value to <n> for negotiation\n\
334 See pppd(8) for more options.\n\
338 * parse_args - parse a string of arguments from the command line.
339 * If prepass is true, we are scanning for the device name and only
340 * processing a few options, so error messages are suppressed.
343 parse_args(argc, argv)
351 privileged_option = privileged;
352 option_source = "command line";
358 * First see if it's an option in the new option list.
360 opt = find_option(arg);
362 int n = n_arguments(opt);
364 option_error("too few parameters for option %s", arg);
367 current_option = arg;
368 if (!process_option(opt, argv))
376 * Maybe a tty name, speed or IP address?
378 if ((ret = setdevname(arg)) == 0
379 && (ret = setspeed(arg)) == 0
380 && (ret = setipaddr(arg)) == 0
382 option_error("unrecognized option '%s'", arg);
386 if (ret < 0) /* error */
394 * scan_args - scan the command line arguments to get the tty name,
395 * if specified. Also checks whether the notty or pty option was given.
398 scan_args(argc, argv)
405 privileged_option = privileged;
410 if (strcmp(arg, "notty") == 0 || strcmp(arg, "pty") == 0)
413 /* Skip options and their arguments */
414 opt = find_option(arg);
416 int n = n_arguments(opt);
422 /* Check if it's a tty name and copy it if so */
423 (void) setdevname(arg, 1);
429 * options_from_file - Read a string of options from a file,
430 * and interpret them.
433 options_from_file(filename, must_exist, check_prot, priv)
440 int i, newline, ret, err;
445 char args[MAXARGS][MAXWORDLEN];
446 char cmd[MAXWORDLEN];
450 f = fopen(filename, "r");
455 if (!must_exist && err == ENOENT)
458 option_error("Can't open options file %s: %m", filename);
462 oldpriv = privileged_option;
463 privileged_option = priv;
464 oldsource = option_source;
465 option_source = strdup(filename);
466 if (option_source == NULL)
467 option_source = "file";
469 while (getword(f, cmd, &newline, filename)) {
471 * First see if it's a command.
473 opt = find_option(cmd);
475 int n = n_arguments(opt);
476 for (i = 0; i < n; ++i) {
477 if (!getword(f, args[i], &newline, filename)) {
479 "In file %s: too few parameters for option '%s'",
485 current_option = cmd;
486 if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
487 option_error("the %s option may not be used in the %s file",
491 if (!process_option(opt, argv))
497 * Maybe a tty name, speed or IP address?
499 if ((i = setdevname(cmd)) == 0
500 && (i = setspeed(cmd)) == 0
501 && (i = setipaddr(cmd)) == 0) {
502 option_error("In file %s: unrecognized option '%s'",
506 if (i < 0) /* error */
513 privileged_option = oldpriv;
514 option_source = oldsource;
519 * options_from_user - See if the use has a ~/.ppprc file,
520 * and if so, interpret options from it.
525 char *user, *path, *file;
530 pw = getpwuid(getuid());
531 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
533 file = _PATH_USEROPT;
534 pl = strlen(user) + strlen(file) + 2;
537 novm("init file name");
538 slprintf(path, pl, "%s/%s", user, file);
539 ret = options_from_file(path, 0, 1, privileged);
545 * options_for_tty - See if an options file exists for the serial
546 * device, and if so, interpret options from it.
551 char *dev, *path, *p;
556 if (strncmp(dev, "/dev/", 5) == 0)
558 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
559 return 1; /* don't look for /etc/ppp/options.tty */
560 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
563 novm("tty init file name");
564 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
565 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
566 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
569 ret = options_from_file(path, 0, 0, 1);
575 * options_from_list - process a string of options in a wordlist.
578 options_from_list(w, priv)
586 privileged_option = priv;
587 option_source = "secrets file";
591 * First see if it's a command.
593 opt = find_option(w->word);
595 int n = n_arguments(opt);
596 struct wordlist *w0 = w;
597 for (i = 0; i < n; ++i) {
601 "In secrets file: too few parameters for option '%s'",
607 current_option = w0->word;
608 if (!process_option(opt, argv))
614 * Maybe a tty name, speed or IP address?
616 if ((i = setdevname(w->word)) == 0
617 && (i = setspeed(w->word)) == 0
618 && (i = setipaddr(w->word)) == 0) {
619 option_error("In secrets file: unrecognized option '%s'",
623 if (i < 0) /* error */
633 * find_option - scan the option lists for the various protocols
634 * looking for an entry with the given name.
635 * This could be optimized by using a hash table.
642 struct option_list *list;
645 for (list = extra_options; list != NULL; list = list->next)
646 for (opt = list->options; opt->name != NULL; ++opt)
647 if (strcmp(name, opt->name) == 0)
649 for (opt = general_options; opt->name != NULL; ++opt)
650 if (strcmp(name, opt->name) == 0)
652 for (opt = auth_options; opt->name != NULL; ++opt)
653 if (strcmp(name, opt->name) == 0)
655 for (i = 0; protocols[i] != NULL; ++i)
656 if ((opt = protocols[i]->options) != NULL)
657 for (; opt->name != NULL; ++opt)
658 if (strcmp(name, opt->name) == 0)
664 * process_option - process one new-style option.
667 process_option(opt, argv)
674 int (*parser) __P((char **));
676 if ((opt->flags & OPT_PREPASS) == 0 && prepass)
678 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
679 option_error("it's too late to use the %s option", opt->name);
682 if ((opt->flags & OPT_PRIV) && !privileged_option) {
683 option_error("using the %s option requires root privilege", opt->name);
686 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
687 option_error("%s option is disabled", opt->name);
690 if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
691 struct option_info *ip = (struct option_info *) opt->addr2;
692 if (ip && ip->priv) {
693 option_error("%s option cannot be overridden", opt->name);
700 v = opt->flags & OPT_VALUE;
701 *(bool *)(opt->addr) = v;
702 if (opt->addr2 && (opt->flags & OPT_A2COPY))
703 *(bool *)(opt->addr2) = v;
708 if ((opt->flags & OPT_NOARG) == 0) {
709 if (!int_option(*argv, &iv))
711 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
712 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
713 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
714 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
715 switch (opt->flags & OPT_LIMITS) {
717 option_error("%s value must be%s >= %d",
718 opt->name, zok, opt->lower_limit);
721 option_error("%s value must be%s <= %d",
722 opt->name, zok, opt->upper_limit);
725 option_error("%s value must be%s between %d and %d",
726 opt->name, opt->lower_limit, opt->upper_limit);
732 a = opt->flags & OPT_VALUE;
734 a -= 256; /* sign extend */
736 if (opt->flags & OPT_INC)
737 iv += *(int *)(opt->addr);
738 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
739 int oldv = *(int *)(opt->addr);
740 if ((opt->flags & OPT_ZEROINF) ?
741 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
742 option_error("%s value cannot be increased", opt->name);
746 *(int *)(opt->addr) = iv;
747 if (opt->addr2 && (opt->flags & OPT_A2COPY))
748 *(int *)(opt->addr2) = iv;
752 if (opt->flags & OPT_NOARG) {
753 v = opt->flags & OPT_VALUE;
754 } else if (!number_option(*argv, &v, 16))
756 if (opt->flags & OPT_OR)
757 v |= *(u_int32_t *)(opt->addr);
758 *(u_int32_t *)(opt->addr) = v;
759 if (opt->addr2 && (opt->flags & OPT_A2COPY))
760 *(u_int32_t *)(opt->addr2) = v;
764 if (opt->flags & OPT_STATIC) {
765 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
769 novm("option argument");
770 *(char **)(opt->addr) = sv;
774 case o_special_noarg:
776 parser = (int (*) __P((char **))) opt->addr;
777 if (!(*parser)(argv))
783 if (opt->flags & OPT_A2INFO) {
784 struct option_info *ip = (struct option_info *) opt->addr2;
785 ip->priv = privileged_option;
786 ip->source = option_source;
787 } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
788 *(bool *)(opt->addr2) = 1;
795 * n_arguments - tell how many arguments an option takes
801 return (opt->type == o_bool || opt->type == o_special_noarg
802 || (opt->flags & OPT_NOARG))? 0: 1;
806 * add_options - add a list of options to the set we grok.
812 struct option_list *list;
814 list = malloc(sizeof(*list));
816 novm("option list entry");
818 list->next = extra_options;
819 extra_options = list;
823 * usage - print out a message telling how to use the program.
828 if (phase == PHASE_INITIALIZE)
829 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
834 * showhelp - print out usage message and exit.
840 if (phase == PHASE_INITIALIZE) {
848 * showversion - print out the version number and exit.
854 if (phase == PHASE_INITIALIZE) {
855 fprintf(stderr, "pppd version %s.%d%s\n",
856 VERSION, PATCHLEVEL, IMPLEMENTATION);
863 * option_error - print a message about an error in an option.
864 * The message is logged, and also sent to
865 * stderr if phase == PHASE_INITIALIZE.
868 option_error __V((char *fmt, ...))
873 #if defined(__STDC__)
878 fmt = va_arg(args, char *);
884 vslprintf(buf, sizeof(buf), fmt, args);
886 if (phase == PHASE_INITIALIZE)
887 fprintf(stderr, "%s: %s\n", progname, buf);
888 syslog(LOG_ERR, "%s", buf);
893 * readable - check if a file is readable by the real user.
906 if (fstat(fd, &sbuf) != 0)
908 if (sbuf.st_uid == uid)
909 return sbuf.st_mode & S_IRUSR;
910 if (sbuf.st_gid == getgid())
911 return sbuf.st_mode & S_IRGRP;
912 for (i = 0; i < ngroups; ++i)
913 if (sbuf.st_gid == groups[i])
914 return sbuf.st_mode & S_IRGRP;
915 return sbuf.st_mode & S_IROTH;
920 * Read a word from a file.
921 * Words are delimited by white-space or by quotes (" or ').
922 * Quotes, white-space and \ may be escaped with \.
923 * \<newline> is ignored.
926 getword(f, word, newlinep, filename)
934 int value, digit, got, n;
936 #define isoctal(c) ((c) >= '0' && (c) < '8')
944 * First skip white-space and comments.
952 * A newline means the end of a comment; backslash-newline
953 * is ignored. Note that we cannot have escape && comment.
965 * Ignore characters other than newline in a comment.
971 * If this character is escaped, we have a word start.
977 * If this is the escape character, look at the next character.
985 * If this is the start of a comment, ignore the rest of the line.
993 * A non-whitespace character is the start of a word.
1000 * Save the delimiter for quoted strings.
1002 if (!escape && (c == '"' || c == '\'')) {
1009 * Process characters until the end of the word.
1014 * This character is escaped: backslash-newline is ignored,
1015 * various other characters indicate particular values
1016 * as for C backslash-escapes.
1051 * \ddd octal sequence
1054 for (n = 0; n < 3 && isoctal(c); ++n) {
1055 value = (value << 3) + (c & 07);
1064 * \x<hex_string> sequence
1068 for (n = 0; n < 2 && isxdigit(c); ++n) {
1069 digit = toupper(c) - '0';
1071 digit += '0' + 10 - 'A';
1072 value = (value << 4) + digit;
1080 * Otherwise the character stands for itself.
1087 * Store the resulting character for the escape sequence.
1089 if (len < MAXWORDLEN-1)
1100 * Not escaped: see if we've reached the end of the word.
1106 if (isspace(c) || c == '#') {
1113 * Backslash starts an escape sequence.
1122 * An ordinary character: store it in the word and get another.
1124 if (len < MAXWORDLEN-1)
1132 * End of the word: check for errors.
1138 option_error("Error reading %s: %m", filename);
1142 * If len is zero, then we didn't find a word before the
1150 * Warn if the word was too long, and append a terminating null.
1152 if (len >= MAXWORDLEN) {
1153 option_error("warning: word in file %s too long (%.20s...)",
1155 len = MAXWORDLEN - 1;
1166 * number_option - parse an unsigned numeric parameter for an option.
1169 number_option(str, valp, base)
1176 *valp = strtoul(str, &ptr, base);
1178 option_error("invalid numeric parameter '%s' for %s option",
1179 str, current_option);
1187 * int_option - like number_option, but valp is int *,
1188 * the base is assumed to be 0, and *valp is not changed
1189 * if there is an error.
1192 int_option(str, valp)
1198 if (!number_option(str, &v, 0))
1206 * The following procedures parse options.
1210 * readfile - take commands from a file.
1216 return options_from_file(*argv, 1, 1, privileged_option);
1220 * callfile - take commands from /etc/ppp/peers/<name>.
1221 * Name may not contain /../, start with / or ../, or end in /..
1227 char *fname, *arg, *p;
1232 if (arg[0] == '/' || arg[0] == 0)
1235 for (p = arg; *p != 0; ) {
1236 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1240 while (*p != '/' && *p != 0)
1247 option_error("call option value may not contain .. or start with /");
1251 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1252 if ((fname = (char *) malloc(l)) == NULL)
1253 novm("call file name");
1254 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1256 ok = options_from_file(fname, 1, 1, 1);
1264 * setpdebug - Set libpcap debugging level.
1270 return int_option(*argv, &dflag);
1274 * setpassfilter - Set the pass filter for packets
1280 pc.linktype = DLT_PPP;
1281 pc.snapshot = PPP_HDRLEN;
1283 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1285 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1290 * setactivefilter - Set the active filter for packets
1293 setactivefilter(argv)
1296 pc.linktype = DLT_PPP;
1297 pc.snapshot = PPP_HDRLEN;
1299 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1301 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1307 * noopt - Disable all options.
1313 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1314 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1315 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1316 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1322 * setdomain - Set domain name to append to hostname
1328 if (!privileged_option) {
1329 option_error("using the domain option requires root privilege");
1332 gethostname(hostname, MAXNAMELEN);
1335 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1336 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1338 hostname[MAXNAMELEN-1] = 0;
1344 * setspeed - Set the speed.
1355 spd = strtol(arg, &ptr, 0);
1356 if (ptr == arg || *ptr != 0 || spd == 0)
1364 * setdevname - Set the device name.
1370 struct stat statbuf;
1371 char dev[MAXPATHLEN];
1376 if (strncmp("/dev/", cp, 5) != 0) {
1377 strlcpy(dev, "/dev/", sizeof(dev));
1378 strlcat(dev, cp, sizeof(dev));
1383 * Check if there is a character device by this name.
1385 if (stat(cp, &statbuf) < 0) {
1386 if (errno == ENOENT)
1388 option_error("Couldn't stat %s: %m", cp);
1391 if (!S_ISCHR(statbuf.st_mode)) {
1392 option_error("%s is not a character device", cp);
1396 if (phase != PHASE_INITIALIZE) {
1397 option_error("device name cannot be changed after initialization");
1399 } else if (devnam_fixed) {
1400 option_error("per-tty options file may not specify device name");
1404 if (devnam_info.priv && !privileged_option) {
1405 option_error("device name cannot be overridden");
1409 strlcpy(devnam, cp, sizeof(devnam));
1412 devnam_info.priv = privileged_option;
1413 devnam_info.source = option_source;
1420 * setipaddr - Set the IP address
1428 u_int32_t local, remote;
1429 ipcp_options *wo = &ipcp_wantoptions[0];
1432 * IP address pair separated by ":".
1434 if ((colon = strchr(arg, ':')) == NULL)
1440 * If colon first character, then no local addr.
1444 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
1445 if ((hp = gethostbyname(arg)) == NULL) {
1446 option_error("unknown host: %s", arg);
1449 local = *(u_int32_t *)hp->h_addr;
1452 if (bad_ip_adrs(local)) {
1453 option_error("bad local IP address %s", ip_ntoa(local));
1457 wo->ouraddr = local;
1462 * If colon last character, then no remote addr.
1464 if (*++colon != '\0') {
1465 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
1466 if ((hp = gethostbyname(colon)) == NULL) {
1467 option_error("unknown host: %s", colon);
1470 remote = *(u_int32_t *)hp->h_addr;
1471 if (remote_name[0] == 0)
1472 strlcpy(remote_name, colon, sizeof(remote_name));
1475 if (bad_ip_adrs(remote)) {
1476 option_error("bad remote IP address %s", ip_ntoa(remote));
1480 wo->hisaddr = remote;
1488 * setnetmask - set the netmask to be used on the interface.
1499 * Unfortunately, if we use inet_addr, we can't tell whether
1500 * a result of all 1s is an error or a valid 255.255.255.255.
1503 n = parse_dotted_ip(p, &mask);
1507 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
1508 option_error("invalid netmask value '%s'", *argv);
1517 parse_dotted_ip(p, vp)
1523 char *endp, *p0 = p;
1527 b = strtoul(p, &endp, 0);
1533 /* accept e.g. 0xffffff00 */
1553 lcp_wantoptions[0].asyncmap |= 0x000A0000; /* escape ^S and ^Q */
1554 lcp_wantoptions[0].neg_asyncmap = 1;
1566 if (!privileged_option)
1568 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1569 if (fd < 0 && errno == EEXIST)
1570 fd = open(*argv, O_WRONLY | O_APPEND);
1572 if (!privileged_option)
1576 option_error("Can't open log file %s: %m", *argv);
1579 if (log_to_file && log_to_fd >= 0)
1594 void (*init) __P((void));
1596 handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
1600 option_error("%s", err);
1601 option_error("Couldn't load plugin %s", arg);
1604 init = (void (*)(void))dlsym(handle, "plugin_init");
1606 option_error("%s has no initialization entry point", arg);
1610 info("Plugin %s loaded.", arg);