Move the tty-related stuff out to tty.c as far as possible.
[mpls-ppp.git] / pppd / options.c
blob1844e3efa7ce9ea2ede568ec0e23ff47e46a80bc
1 /*
2 * options.c - handles option processing for PPP.
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
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.75 2000/06/30 04:54:21 paulus Exp $"
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <termios.h>
29 #include <syslog.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <pwd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #ifdef PLUGIN
38 #include <dlfcn.h>
39 #endif
40 #ifdef PPP_FILTER
41 #include <pcap.h>
42 #include <pcap-int.h> /* XXX: To get struct pcap */
43 #endif
45 #include "pppd.h"
46 #include "pathnames.h"
47 #include "patchlevel.h"
48 #include "fsm.h"
49 #include "lcp.h"
50 #include "ipcp.h"
51 #include "upap.h"
52 #include "chap.h"
53 #include "ccp.h"
55 #include <net/ppp-comp.h>
57 #if defined(ultrix) || defined(NeXT)
58 char *strdup __P((char *));
59 #endif
61 static const char rcsid[] = RCSID;
64 * Option variables and default values.
66 #ifdef PPP_FILTER
67 int dflag = 0; /* Tell libpcap we want debugging */
68 #endif
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 u_int32_t netmask = 0; /* IP netmask to set on interface */
74 bool nodetach = 0; /* Don't detach from controlling tty */
75 bool updetach = 0; /* Detach once link is up */
76 int maxconnect = 0; /* Maximum connect time */
77 char user[MAXNAMELEN]; /* Username for PAP */
78 char passwd[MAXSECRETLEN]; /* Password for PAP */
79 bool persist = 0; /* Reopen link after it goes down */
80 char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
81 bool demand = 0; /* do dial-on-demand */
82 char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
83 int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
84 int holdoff = 30; /* # seconds to pause before reconnecting */
85 bool holdoff_specified; /* true if a holdoff value has been given */
86 int log_to_fd = 1; /* send log messages to this fd too */
87 int maxfail = 10; /* max # of unsuccessful connection attempts */
88 char linkname[MAXPATHLEN]; /* logical name for link */
89 bool tune_kernel; /* may alter kernel settings */
90 int connect_delay = 1000; /* wait this many ms after connect script */
91 int req_unit = -1; /* requested interface unit */
92 bool multilink = 0; /* Enable multilink operation */
93 char *bundle_name = NULL; /* bundle name for multilink */
95 extern option_t auth_options[];
96 extern struct stat devstat;
98 struct option_info initializer_info;
99 struct option_info connect_script_info;
100 struct option_info disconnect_script_info;
101 struct option_info welcomer_info;
102 struct option_info devnam_info;
103 struct option_info ptycommand_info;
105 #ifdef PPP_FILTER
106 struct bpf_program pass_filter;/* Filter program for packets to pass */
107 struct bpf_program active_filter; /* Filter program for link-active pkts */
108 pcap_t pc; /* Fake struct pcap so we can compile expr */
109 #endif
111 char *current_option; /* the name of the option being parsed */
112 int privileged_option; /* set iff the current option came from root */
113 char *option_source; /* string saying where the option came from */
114 bool log_to_file; /* log_to_fd is a file opened by us */
115 bool log_to_specific_fd; /* log_to_fd was specified by user option */
116 bool no_override; /* don't override previously-set options */
119 * Prototypes
121 static int setdevname __P((char *));
122 static int setipaddr __P((char *));
123 static int setspeed __P((char *));
124 static int noopt __P((char **));
125 static int setdomain __P((char **));
126 static int setnetmask __P((char **));
127 static int readfile __P((char **));
128 static int callfile __P((char **));
129 static int showversion __P((char **));
130 static int showhelp __P((char **));
131 static void usage __P((void));
132 static int setlogfile __P((char **));
133 #ifdef PLUGIN
134 static int loadplugin __P((char **));
135 #endif
137 #ifdef PPP_FILTER
138 static int setpassfilter __P((char **));
139 static int setactivefilter __P((char **));
140 #endif
142 static option_t *find_option __P((char *name));
143 static int process_option __P((option_t *, char **));
144 static int n_arguments __P((option_t *));
145 static int number_option __P((char *, u_int32_t *, int));
148 * Structure to store extra lists of options.
150 struct option_list {
151 option_t *options;
152 struct option_list *next;
155 static struct option_list *extra_options = NULL;
158 * Valid arguments.
160 option_t general_options[] = {
161 { "debug", o_int, &debug,
162 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
163 { "-d", o_int, &debug,
164 "Increase debugging level", OPT_INC|OPT_NOARG|1 },
165 { "kdebug", o_int, &kdebugflag,
166 "Set kernel driver debug level" },
167 { "nodetach", o_bool, &nodetach,
168 "Don't detach from controlling tty", 1 },
169 { "-detach", o_bool, &nodetach,
170 "Don't detach from controlling tty", 1 },
171 { "updetach", o_bool, &updetach,
172 "Detach from controlling tty once link is up", 1 },
173 { "holdoff", o_int, &holdoff,
174 "Set time in seconds before retrying connection" },
175 { "idle", o_int, &idle_time_limit,
176 "Set time in seconds before disconnecting idle link" },
177 { "-all", o_special_noarg, (void *)noopt,
178 "Don't request/allow any LCP or IPCP options (useless)" },
179 { "maxconnect", o_int, &maxconnect,
180 "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
181 { "domain", o_special, (void *)setdomain,
182 "Add given domain name to hostname" },
183 { "mtu", o_int, &lcp_allowoptions[0].mru,
184 "Set our MTU", OPT_LIMITS, NULL, MAXMRU, MINMRU },
185 { "netmask", o_special, (void *)setnetmask,
186 "set netmask" },
187 { "file", o_special, (void *)readfile,
188 "Take options from a file", OPT_PREPASS },
189 { "call", o_special, (void *)callfile,
190 "Take options from a privileged file", OPT_PREPASS },
191 { "persist", o_bool, &persist,
192 "Keep on reopening connection after close", 1 },
193 { "nopersist", o_bool, &persist,
194 "Turn off persist option" },
195 { "demand", o_bool, &demand,
196 "Dial on demand", OPT_INITONLY | 1, &persist },
197 { "--version", o_special_noarg, (void *)showversion,
198 "Show version number" },
199 { "--help", o_special_noarg, (void *)showhelp,
200 "Show brief listing of options" },
201 { "-h", o_special_noarg, (void *)showhelp,
202 "Show brief listing of options" },
203 { "logfd", o_int, &log_to_fd,
204 "Send log messages to this file descriptor",
205 0, &log_to_specific_fd },
206 { "logfile", o_special, (void *)setlogfile,
207 "Append log messages to this file" },
208 { "nolog", o_int, &log_to_fd,
209 "Don't send log messages to any file",
210 OPT_NOARG | OPT_VAL(-1) },
211 { "nologfd", o_int, &log_to_fd,
212 "Don't send log messages to any file descriptor",
213 OPT_NOARG | OPT_VAL(-1) },
214 { "linkname", o_string, linkname,
215 "Set logical name for link",
216 OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
217 { "maxfail", o_int, &maxfail,
218 "Maximum number of unsuccessful connection attempts to allow" },
219 { "ktune", o_bool, &tune_kernel,
220 "Alter kernel settings as necessary", 1 },
221 { "noktune", o_bool, &tune_kernel,
222 "Don't alter kernel settings", 0 },
223 { "connect-delay", o_int, &connect_delay,
224 "Maximum time (in ms) to wait after connect script finishes" },
225 { "unit", o_int, &req_unit,
226 "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
227 #ifdef HAVE_MULTILINK
228 { "multilink", o_bool, &multilink,
229 "Enable multilink operation", 1 },
230 { "nomultilink", o_bool, &multilink,
231 "Disable multilink operation", 0 },
232 { "mp", o_bool, &multilink,
233 "Enable multilink operation", 1 },
234 { "nomp", o_bool, &multilink,
235 "Disable multilink operation", 0 },
236 { "bundle", o_string, &bundle_name,
237 "Bundle name for multilink" },
238 #endif /* HAVE_MULTILINK */
239 #ifdef PLUGIN
240 { "plugin", o_special, (void *)loadplugin,
241 "Load a plug-in module into pppd", OPT_PRIV },
242 #endif
244 #ifdef PPP_FILTER
245 { "pdebug", o_int, &dflag,
246 "libpcap debugging" },
247 { "pass-filter", 1, setpassfilter,
248 "set filter for packets to pass" },
249 { "active-filter", 1, setactivefilter,
250 "set filter for active pkts" },
251 #endif
253 { NULL }
256 #ifndef IMPLEMENTATION
257 #define IMPLEMENTATION ""
258 #endif
260 static char *usage_string = "\
261 pppd version %s.%d%s\n\
262 Usage: %s [ options ], where options are:\n\
263 <device> Communicate over the named device\n\
264 <speed> Set the baud rate to <speed>\n\
265 <loc>:<rem> Set the local and/or remote interface IP\n\
266 addresses. Either one may be omitted.\n\
267 asyncmap <n> Set the desired async map to hex <n>\n\
268 auth Require authentication from peer\n\
269 connect <p> Invoke shell command <p> to set up the serial line\n\
270 crtscts Use hardware RTS/CTS flow control\n\
271 defaultroute Add default route through interface\n\
272 file <f> Take options from file <f>\n\
273 modem Use modem control lines\n\
274 mru <n> Set MRU value to <n> for negotiation\n\
275 See pppd(8) for more options.\n\
279 * parse_args - parse a string of arguments from the command line.
282 parse_args(argc, argv)
283 int argc;
284 char **argv;
286 char *arg;
287 option_t *opt;
288 int ret;
290 privileged_option = privileged;
291 option_source = "command line";
292 while (argc > 0) {
293 arg = *argv++;
294 --argc;
297 * First see if it's an option in the new option list.
299 opt = find_option(arg);
300 if (opt != NULL) {
301 int n = n_arguments(opt);
302 if (argc < n) {
303 option_error("too few parameters for option %s", arg);
304 return 0;
306 current_option = arg;
307 if (!process_option(opt, argv))
308 return 0;
309 argc -= n;
310 argv += n;
311 continue;
315 * Maybe a tty name, speed or IP address?
317 if ((ret = setdevname(arg)) == 0
318 && (ret = setspeed(arg)) == 0
319 && (ret = setipaddr(arg)) == 0) {
320 option_error("unrecognized option '%s'", arg);
321 usage();
322 return 0;
324 if (ret < 0) /* error */
325 return 0;
327 return 1;
330 #if 0
332 * scan_args - scan the command line arguments to get the tty name,
333 * if specified. Also checks whether the notty or pty option was given.
335 void
336 scan_args(argc, argv)
337 int argc;
338 char **argv;
340 char *arg;
341 option_t *opt;
343 privileged_option = privileged;
344 while (argc > 0) {
345 arg = *argv++;
346 --argc;
348 if (strcmp(arg, "notty") == 0 || strcmp(arg, "pty") == 0)
349 using_pty = 1;
351 /* Skip options and their arguments */
352 opt = find_option(arg);
353 if (opt != NULL) {
354 int n = n_arguments(opt);
355 argc -= n;
356 argv += n;
357 continue;
360 /* Check if it's a tty name and copy it if so */
361 (void) setdevname(arg, 1);
364 #endif
367 * options_from_file - Read a string of options from a file,
368 * and interpret them.
371 options_from_file(filename, must_exist, check_prot, priv)
372 char *filename;
373 int must_exist;
374 int check_prot;
375 int priv;
377 FILE *f;
378 int i, newline, ret, err;
379 option_t *opt;
380 int oldpriv;
381 char *oldsource;
382 char *argv[MAXARGS];
383 char args[MAXARGS][MAXWORDLEN];
384 char cmd[MAXWORDLEN];
386 if (check_prot)
387 seteuid(getuid());
388 f = fopen(filename, "r");
389 err = errno;
390 if (check_prot)
391 seteuid(0);
392 if (f == NULL) {
393 errno = err;
394 if (!must_exist) {
395 if (err != ENOENT && err != ENOTDIR)
396 warn("Warning: can't open options file %s: %m", filename);
397 return 1;
399 option_error("Can't open options file %s: %m", filename);
400 return 0;
403 oldpriv = privileged_option;
404 privileged_option = priv;
405 oldsource = option_source;
406 option_source = strdup(filename);
407 if (option_source == NULL)
408 option_source = "file";
409 ret = 0;
410 while (getword(f, cmd, &newline, filename)) {
412 * First see if it's a command.
414 opt = find_option(cmd);
415 if (opt != NULL) {
416 int n = n_arguments(opt);
417 for (i = 0; i < n; ++i) {
418 if (!getword(f, args[i], &newline, filename)) {
419 option_error(
420 "In file %s: too few parameters for option '%s'",
421 filename, cmd);
422 goto err;
424 argv[i] = args[i];
426 current_option = cmd;
427 if ((opt->flags & OPT_DEVEQUIV) && no_override) {
428 option_error("the %s option may not be used in the %s file",
429 cmd, filename);
430 goto err;
432 if (!process_option(opt, argv))
433 goto err;
434 continue;
438 * Maybe a tty name, speed or IP address?
440 if ((i = setdevname(cmd)) == 0
441 && (i = setspeed(cmd)) == 0
442 && (i = setipaddr(cmd)) == 0) {
443 option_error("In file %s: unrecognized option '%s'",
444 filename, cmd);
445 goto err;
447 if (i < 0) /* error */
448 goto err;
450 ret = 1;
452 err:
453 fclose(f);
454 privileged_option = oldpriv;
455 option_source = oldsource;
456 return ret;
460 * options_from_user - See if the use has a ~/.ppprc file,
461 * and if so, interpret options from it.
464 options_from_user()
466 char *user, *path, *file;
467 int ret;
468 struct passwd *pw;
469 size_t pl;
471 pw = getpwuid(getuid());
472 if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
473 return 1;
474 file = _PATH_USEROPT;
475 pl = strlen(user) + strlen(file) + 2;
476 path = malloc(pl);
477 if (path == NULL)
478 novm("init file name");
479 slprintf(path, pl, "%s/%s", user, file);
480 ret = options_from_file(path, 0, 1, privileged);
481 free(path);
482 return ret;
486 * options_for_tty - See if an options file exists for the serial
487 * device, and if so, interpret options from it.
488 * We only allow the per-tty options file to override anything from
489 * the command line if it is something that the user can't override
490 * once it has been set by root.
493 options_for_tty()
495 char *dev, *path, *p;
496 int ret;
497 size_t pl;
499 dev = devnam;
500 if (strncmp(dev, "/dev/", 5) == 0)
501 dev += 5;
502 if (dev[0] == 0 || strcmp(dev, "tty") == 0)
503 return 1; /* don't look for /etc/ppp/options.tty */
504 pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
505 path = malloc(pl);
506 if (path == NULL)
507 novm("tty init file name");
508 slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
509 /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
510 for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
511 if (*p == '/')
512 *p = '.';
513 no_override = 1;
514 ret = options_from_file(path, 0, 0, 1);
515 no_override = 0;
516 free(path);
517 return ret;
521 * options_from_list - process a string of options in a wordlist.
524 options_from_list(w, priv)
525 struct wordlist *w;
526 int priv;
528 char *argv[MAXARGS];
529 option_t *opt;
530 int i, ret = 0;
532 privileged_option = priv;
533 option_source = "secrets file";
535 while (w != NULL) {
537 * First see if it's a command.
539 opt = find_option(w->word);
540 if (opt != NULL) {
541 int n = n_arguments(opt);
542 struct wordlist *w0 = w;
543 for (i = 0; i < n; ++i) {
544 w = w->next;
545 if (w == NULL) {
546 option_error(
547 "In secrets file: too few parameters for option '%s'",
548 w0->word);
549 goto err;
551 argv[i] = w->word;
553 current_option = w0->word;
554 if (!process_option(opt, argv))
555 goto err;
556 continue;
560 * Maybe a tty name, speed or IP address?
562 if ((i = setdevname(w->word)) == 0
563 && (i = setspeed(w->word)) == 0
564 && (i = setipaddr(w->word)) == 0) {
565 option_error("In secrets file: unrecognized option '%s'",
566 w->word);
567 goto err;
569 if (i < 0) /* error */
570 goto err;
572 ret = 1;
574 err:
575 return ret;
579 * find_option - scan the option lists for the various protocols
580 * looking for an entry with the given name.
581 * This could be optimized by using a hash table.
583 static option_t *
584 find_option(name)
585 char *name;
587 option_t *opt;
588 struct option_list *list;
589 int i;
591 for (list = extra_options; list != NULL; list = list->next)
592 for (opt = list->options; opt->name != NULL; ++opt)
593 if (strcmp(name, opt->name) == 0)
594 return opt;
595 for (opt = general_options; opt->name != NULL; ++opt)
596 if (strcmp(name, opt->name) == 0)
597 return opt;
598 for (opt = auth_options; opt->name != NULL; ++opt)
599 if (strcmp(name, opt->name) == 0)
600 return opt;
601 for (i = 0; protocols[i] != NULL; ++i)
602 if ((opt = protocols[i]->options) != NULL)
603 for (; opt->name != NULL; ++opt)
604 if (strcmp(name, opt->name) == 0)
605 return opt;
606 return NULL;
610 * process_option - process one new-style option.
612 static int
613 process_option(opt, argv)
614 option_t *opt;
615 char **argv;
617 u_int32_t v;
618 int iv, a;
619 char *sv;
620 int (*parser) __P((char **));
622 if (no_override && (opt->flags & OPT_SEENIT)) {
623 struct option_info *ip = (struct option_info *) opt->addr2;
624 if (!(privileged && (opt->flags & OPT_PRIVFIX)))
625 return 1;
626 if (!ip || ip->priv)
627 return 1;
628 warn("%s option from per-tty file overrides command line", opt->name);
630 if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
631 option_error("it's too late to use the %s option", opt->name);
632 return 0;
634 if ((opt->flags & OPT_PRIV) && !privileged_option) {
635 option_error("using the %s option requires root privilege", opt->name);
636 return 0;
638 if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
639 option_error("%s option is disabled", opt->name);
640 return 0;
642 if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
643 struct option_info *ip = (struct option_info *) opt->addr2;
644 if (ip && ip->priv) {
645 option_error("%s option cannot be overridden", opt->name);
646 return 0;
649 opt->flags |= OPT_SEENIT;
651 switch (opt->type) {
652 case o_bool:
653 v = opt->flags & OPT_VALUE;
654 *(bool *)(opt->addr) = v;
655 if (opt->addr2 && (opt->flags & OPT_A2COPY))
656 *(bool *)(opt->addr2) = v;
657 break;
659 case o_int:
660 iv = 0;
661 if ((opt->flags & OPT_NOARG) == 0) {
662 if (!int_option(*argv, &iv))
663 return 0;
664 if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
665 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
666 && !((opt->flags & OPT_ZEROOK && iv == 0))) {
667 char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
668 switch (opt->flags & OPT_LIMITS) {
669 case OPT_LLIMIT:
670 option_error("%s value must be%s >= %d",
671 opt->name, zok, opt->lower_limit);
672 break;
673 case OPT_ULIMIT:
674 option_error("%s value must be%s <= %d",
675 opt->name, zok, opt->upper_limit);
676 break;
677 case OPT_LIMITS:
678 option_error("%s value must be%s between %d and %d",
679 opt->name, opt->lower_limit, opt->upper_limit);
680 break;
682 return 0;
685 a = opt->flags & OPT_VALUE;
686 if (a >= 128)
687 a -= 256; /* sign extend */
688 iv += a;
689 if (opt->flags & OPT_INC)
690 iv += *(int *)(opt->addr);
691 if ((opt->flags & OPT_NOINCR) && !privileged_option) {
692 int oldv = *(int *)(opt->addr);
693 if ((opt->flags & OPT_ZEROINF) ?
694 (oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
695 option_error("%s value cannot be increased", opt->name);
696 return 0;
699 *(int *)(opt->addr) = iv;
700 if (opt->addr2 && (opt->flags & OPT_A2COPY))
701 *(int *)(opt->addr2) = iv;
702 break;
704 case o_uint32:
705 if (opt->flags & OPT_NOARG) {
706 v = opt->flags & OPT_VALUE;
707 } else if (!number_option(*argv, &v, 16))
708 return 0;
709 if (opt->flags & OPT_OR)
710 v |= *(u_int32_t *)(opt->addr);
711 *(u_int32_t *)(opt->addr) = v;
712 if (opt->addr2 && (opt->flags & OPT_A2COPY))
713 *(u_int32_t *)(opt->addr2) = v;
714 break;
716 case o_string:
717 if (opt->flags & OPT_STATIC) {
718 strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
719 } else {
720 sv = strdup(*argv);
721 if (sv == NULL)
722 novm("option argument");
723 *(char **)(opt->addr) = sv;
725 break;
727 case o_special_noarg:
728 case o_special:
729 parser = (int (*) __P((char **))) opt->addr;
730 if (!(*parser)(argv))
731 return 0;
732 break;
735 if (opt->addr2) {
736 if (opt->flags & OPT_A2INFO) {
737 struct option_info *ip = (struct option_info *) opt->addr2;
738 ip->priv = privileged_option;
739 ip->source = option_source;
740 } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
741 *(bool *)(opt->addr2) = 1;
744 return 1;
748 * n_arguments - tell how many arguments an option takes
750 static int
751 n_arguments(opt)
752 option_t *opt;
754 return (opt->type == o_bool || opt->type == o_special_noarg
755 || (opt->flags & OPT_NOARG))? 0: 1;
759 * add_options - add a list of options to the set we grok.
761 void
762 add_options(opt)
763 option_t *opt;
765 struct option_list *list;
767 list = malloc(sizeof(*list));
768 if (list == 0)
769 novm("option list entry");
770 list->options = opt;
771 list->next = extra_options;
772 extra_options = list;
776 * usage - print out a message telling how to use the program.
778 static void
779 usage()
781 if (phase == PHASE_INITIALIZE)
782 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
783 progname);
787 * showhelp - print out usage message and exit.
789 static int
790 showhelp(argv)
791 char **argv;
793 if (phase == PHASE_INITIALIZE) {
794 usage();
795 exit(0);
797 return 0;
801 * showversion - print out the version number and exit.
803 static int
804 showversion(argv)
805 char **argv;
807 if (phase == PHASE_INITIALIZE) {
808 fprintf(stderr, "pppd version %s.%d%s\n",
809 VERSION, PATCHLEVEL, IMPLEMENTATION);
810 exit(0);
812 return 0;
816 * option_error - print a message about an error in an option.
817 * The message is logged, and also sent to
818 * stderr if phase == PHASE_INITIALIZE.
820 void
821 option_error __V((char *fmt, ...))
823 va_list args;
824 char buf[256];
826 #if defined(__STDC__)
827 va_start(args, fmt);
828 #else
829 char *fmt;
830 va_start(args);
831 fmt = va_arg(args, char *);
832 #endif
833 vslprintf(buf, sizeof(buf), fmt, args);
834 va_end(args);
835 if (phase == PHASE_INITIALIZE)
836 fprintf(stderr, "%s: %s\n", progname, buf);
837 syslog(LOG_ERR, "%s", buf);
840 #if 0
842 * readable - check if a file is readable by the real user.
845 readable(fd)
846 int fd;
848 uid_t uid;
849 int i;
850 struct stat sbuf;
852 uid = getuid();
853 if (uid == 0)
854 return 1;
855 if (fstat(fd, &sbuf) != 0)
856 return 0;
857 if (sbuf.st_uid == uid)
858 return sbuf.st_mode & S_IRUSR;
859 if (sbuf.st_gid == getgid())
860 return sbuf.st_mode & S_IRGRP;
861 for (i = 0; i < ngroups; ++i)
862 if (sbuf.st_gid == groups[i])
863 return sbuf.st_mode & S_IRGRP;
864 return sbuf.st_mode & S_IROTH;
866 #endif
869 * Read a word from a file.
870 * Words are delimited by white-space or by quotes (" or ').
871 * Quotes, white-space and \ may be escaped with \.
872 * \<newline> is ignored.
875 getword(f, word, newlinep, filename)
876 FILE *f;
877 char *word;
878 int *newlinep;
879 char *filename;
881 int c, len, escape;
882 int quoted, comment;
883 int value, digit, got, n;
885 #define isoctal(c) ((c) >= '0' && (c) < '8')
887 *newlinep = 0;
888 len = 0;
889 escape = 0;
890 comment = 0;
893 * First skip white-space and comments.
895 for (;;) {
896 c = getc(f);
897 if (c == EOF)
898 break;
901 * A newline means the end of a comment; backslash-newline
902 * is ignored. Note that we cannot have escape && comment.
904 if (c == '\n') {
905 if (!escape) {
906 *newlinep = 1;
907 comment = 0;
908 } else
909 escape = 0;
910 continue;
914 * Ignore characters other than newline in a comment.
916 if (comment)
917 continue;
920 * If this character is escaped, we have a word start.
922 if (escape)
923 break;
926 * If this is the escape character, look at the next character.
928 if (c == '\\') {
929 escape = 1;
930 continue;
934 * If this is the start of a comment, ignore the rest of the line.
936 if (c == '#') {
937 comment = 1;
938 continue;
942 * A non-whitespace character is the start of a word.
944 if (!isspace(c))
945 break;
949 * Save the delimiter for quoted strings.
951 if (!escape && (c == '"' || c == '\'')) {
952 quoted = c;
953 c = getc(f);
954 } else
955 quoted = 0;
958 * Process characters until the end of the word.
960 while (c != EOF) {
961 if (escape) {
963 * This character is escaped: backslash-newline is ignored,
964 * various other characters indicate particular values
965 * as for C backslash-escapes.
967 escape = 0;
968 if (c == '\n') {
969 c = getc(f);
970 continue;
973 got = 0;
974 switch (c) {
975 case 'a':
976 value = '\a';
977 break;
978 case 'b':
979 value = '\b';
980 break;
981 case 'f':
982 value = '\f';
983 break;
984 case 'n':
985 value = '\n';
986 break;
987 case 'r':
988 value = '\r';
989 break;
990 case 's':
991 value = ' ';
992 break;
993 case 't':
994 value = '\t';
995 break;
997 default:
998 if (isoctal(c)) {
1000 * \ddd octal sequence
1002 value = 0;
1003 for (n = 0; n < 3 && isoctal(c); ++n) {
1004 value = (value << 3) + (c & 07);
1005 c = getc(f);
1007 got = 1;
1008 break;
1011 if (c == 'x') {
1013 * \x<hex_string> sequence
1015 value = 0;
1016 c = getc(f);
1017 for (n = 0; n < 2 && isxdigit(c); ++n) {
1018 digit = toupper(c) - '0';
1019 if (digit > 10)
1020 digit += '0' + 10 - 'A';
1021 value = (value << 4) + digit;
1022 c = getc (f);
1024 got = 1;
1025 break;
1029 * Otherwise the character stands for itself.
1031 value = c;
1032 break;
1036 * Store the resulting character for the escape sequence.
1038 if (len < MAXWORDLEN-1)
1039 word[len] = value;
1040 ++len;
1042 if (!got)
1043 c = getc(f);
1044 continue;
1049 * Not escaped: see if we've reached the end of the word.
1051 if (quoted) {
1052 if (c == quoted)
1053 break;
1054 } else {
1055 if (isspace(c) || c == '#') {
1056 ungetc (c, f);
1057 break;
1062 * Backslash starts an escape sequence.
1064 if (c == '\\') {
1065 escape = 1;
1066 c = getc(f);
1067 continue;
1071 * An ordinary character: store it in the word and get another.
1073 if (len < MAXWORDLEN-1)
1074 word[len] = c;
1075 ++len;
1077 c = getc(f);
1081 * End of the word: check for errors.
1083 if (c == EOF) {
1084 if (ferror(f)) {
1085 if (errno == 0)
1086 errno = EIO;
1087 option_error("Error reading %s: %m", filename);
1088 die(1);
1091 * If len is zero, then we didn't find a word before the
1092 * end of the file.
1094 if (len == 0)
1095 return 0;
1099 * Warn if the word was too long, and append a terminating null.
1101 if (len >= MAXWORDLEN) {
1102 option_error("warning: word in file %s too long (%.20s...)",
1103 filename, word);
1104 len = MAXWORDLEN - 1;
1106 word[len] = 0;
1108 return 1;
1110 #undef isoctal
1115 * number_option - parse an unsigned numeric parameter for an option.
1117 static int
1118 number_option(str, valp, base)
1119 char *str;
1120 u_int32_t *valp;
1121 int base;
1123 char *ptr;
1125 *valp = strtoul(str, &ptr, base);
1126 if (ptr == str) {
1127 option_error("invalid numeric parameter '%s' for %s option",
1128 str, current_option);
1129 return 0;
1131 return 1;
1136 * int_option - like number_option, but valp is int *,
1137 * the base is assumed to be 0, and *valp is not changed
1138 * if there is an error.
1141 int_option(str, valp)
1142 char *str;
1143 int *valp;
1145 u_int32_t v;
1147 if (!number_option(str, &v, 0))
1148 return 0;
1149 *valp = (int) v;
1150 return 1;
1155 * The following procedures parse options.
1159 * readfile - take commands from a file.
1161 static int
1162 readfile(argv)
1163 char **argv;
1165 return options_from_file(*argv, 1, 1, privileged_option);
1169 * callfile - take commands from /etc/ppp/peers/<name>.
1170 * Name may not contain /../, start with / or ../, or end in /..
1172 static int
1173 callfile(argv)
1174 char **argv;
1176 char *fname, *arg, *p;
1177 int l, ok;
1179 arg = *argv;
1180 ok = 1;
1181 if (arg[0] == '/' || arg[0] == 0)
1182 ok = 0;
1183 else {
1184 for (p = arg; *p != 0; ) {
1185 if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1186 ok = 0;
1187 break;
1189 while (*p != '/' && *p != 0)
1190 ++p;
1191 if (*p == '/')
1192 ++p;
1195 if (!ok) {
1196 option_error("call option value may not contain .. or start with /");
1197 return 0;
1200 l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1201 if ((fname = (char *) malloc(l)) == NULL)
1202 novm("call file name");
1203 slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1205 ok = options_from_file(fname, 1, 1, 1);
1207 free(fname);
1208 return ok;
1211 #ifdef PPP_FILTER
1213 * setpdebug - Set libpcap debugging level.
1215 static int
1216 setpdebug(argv)
1217 char **argv;
1219 return int_option(*argv, &dflag);
1223 * setpassfilter - Set the pass filter for packets
1225 static int
1226 setpassfilter(argv)
1227 char **argv;
1229 pc.linktype = DLT_PPP;
1230 pc.snapshot = PPP_HDRLEN;
1232 if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1233 return 1;
1234 option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1235 return 0;
1239 * setactivefilter - Set the active filter for packets
1241 static int
1242 setactivefilter(argv)
1243 char **argv;
1245 pc.linktype = DLT_PPP;
1246 pc.snapshot = PPP_HDRLEN;
1248 if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1249 return 1;
1250 option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1251 return 0;
1253 #endif
1256 * noopt - Disable all options.
1258 static int
1259 noopt(argv)
1260 char **argv;
1262 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1263 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1264 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1265 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1267 return (1);
1271 * setdomain - Set domain name to append to hostname
1273 static int
1274 setdomain(argv)
1275 char **argv;
1277 if (!privileged_option) {
1278 option_error("using the domain option requires root privilege");
1279 return 0;
1281 gethostname(hostname, MAXNAMELEN);
1282 if (**argv != 0) {
1283 if (**argv != '.')
1284 strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1285 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1287 hostname[MAXNAMELEN-1] = 0;
1288 return (1);
1293 * setspeed - Set the speed.
1295 static int
1296 setspeed(arg)
1297 char *arg;
1299 char *ptr;
1300 int spd;
1302 if (no_override && inspeed != 0)
1303 return 1;
1304 spd = strtol(arg, &ptr, 0);
1305 if (ptr == arg || *ptr != 0 || spd == 0)
1306 return 0;
1307 if (!no_override || inspeed == 0)
1308 inspeed = spd;
1309 return 1;
1314 * setdevname - Set the device name.
1316 static int
1317 setdevname(cp)
1318 char *cp;
1320 struct stat statbuf;
1321 char dev[MAXPATHLEN];
1323 if (*cp == 0)
1324 return 0;
1326 if (strncmp("/dev/", cp, 5) != 0) {
1327 strlcpy(dev, "/dev/", sizeof(dev));
1328 strlcat(dev, cp, sizeof(dev));
1329 cp = dev;
1333 * Check if there is a character device by this name.
1335 if (stat(cp, &statbuf) < 0) {
1336 if (errno == ENOENT)
1337 return 0;
1338 option_error("Couldn't stat %s: %m", cp);
1339 return -1;
1341 if (!S_ISCHR(statbuf.st_mode)) {
1342 option_error("%s is not a character device", cp);
1343 return -1;
1346 if (phase != PHASE_INITIALIZE) {
1347 option_error("device name cannot be changed after initialization");
1348 return -1;
1350 if (no_override) {
1351 option_error("per-tty options file may not specify device name");
1352 return -1;
1355 if (devnam_info.priv && !privileged_option) {
1356 option_error("device name cannot be overridden");
1357 return -1;
1360 strlcpy(devnam, cp, sizeof(devnam));
1361 devstat = statbuf;
1362 default_device = 0;
1363 devnam_info.priv = privileged_option;
1364 devnam_info.source = option_source;
1366 return 1;
1371 * setipaddr - Set the IP address
1373 static int
1374 setipaddr(arg)
1375 char *arg;
1377 struct hostent *hp;
1378 char *colon;
1379 u_int32_t local, remote;
1380 ipcp_options *wo = &ipcp_wantoptions[0];
1381 static int seen_local = 0, seen_remote = 0;
1384 * IP address pair separated by ":".
1386 if ((colon = strchr(arg, ':')) == NULL)
1387 return 0;
1390 * If colon first character, then no local addr.
1392 if (colon != arg && !(no_override && seen_local)) {
1393 *colon = '\0';
1394 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
1395 if ((hp = gethostbyname(arg)) == NULL) {
1396 option_error("unknown host: %s", arg);
1397 return -1;
1398 } else {
1399 local = *(u_int32_t *)hp->h_addr;
1402 if (bad_ip_adrs(local)) {
1403 option_error("bad local IP address %s", ip_ntoa(local));
1404 return -1;
1406 if (local != 0)
1407 wo->ouraddr = local;
1408 *colon = ':';
1409 seen_local = 1;
1413 * If colon last character, then no remote addr.
1415 if (*++colon != '\0' && !(no_override && seen_remote)) {
1416 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
1417 if ((hp = gethostbyname(colon)) == NULL) {
1418 option_error("unknown host: %s", colon);
1419 return -1;
1420 } else {
1421 remote = *(u_int32_t *)hp->h_addr;
1422 if (remote_name[0] == 0)
1423 strlcpy(remote_name, colon, sizeof(remote_name));
1426 if (bad_ip_adrs(remote)) {
1427 option_error("bad remote IP address %s", ip_ntoa(remote));
1428 return -1;
1430 if (remote != 0)
1431 wo->hisaddr = remote;
1432 seen_remote = 1;
1435 return 1;
1440 * setnetmask - set the netmask to be used on the interface.
1442 static int
1443 setnetmask(argv)
1444 char **argv;
1446 u_int32_t mask;
1447 int n;
1448 char *p;
1451 * Unfortunately, if we use inet_addr, we can't tell whether
1452 * a result of all 1s is an error or a valid 255.255.255.255.
1454 p = *argv;
1455 n = parse_dotted_ip(p, &mask);
1457 mask = htonl(mask);
1459 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
1460 option_error("invalid netmask value '%s'", *argv);
1461 return 0;
1464 netmask = mask;
1465 return (1);
1469 parse_dotted_ip(p, vp)
1470 char *p;
1471 u_int32_t *vp;
1473 int n;
1474 u_int32_t v, b;
1475 char *endp, *p0 = p;
1477 v = 0;
1478 for (n = 3;; --n) {
1479 b = strtoul(p, &endp, 0);
1480 if (endp == p)
1481 return 0;
1482 if (b > 255) {
1483 if (n < 3)
1484 return 0;
1485 /* accept e.g. 0xffffff00 */
1486 *vp = b;
1487 return endp - p0;
1489 v |= b << (n * 8);
1490 p = endp;
1491 if (n == 0)
1492 break;
1493 if (*p != '.')
1494 return 0;
1495 ++p;
1497 *vp = v;
1498 return p - p0;
1501 static int
1502 setlogfile(argv)
1503 char **argv;
1505 int fd, err;
1507 if (!privileged_option)
1508 seteuid(getuid());
1509 fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1510 if (fd < 0 && errno == EEXIST)
1511 fd = open(*argv, O_WRONLY | O_APPEND);
1512 err = errno;
1513 if (!privileged_option)
1514 seteuid(0);
1515 if (fd < 0) {
1516 errno = err;
1517 option_error("Can't open log file %s: %m", *argv);
1518 return 0;
1520 if (log_to_file && log_to_fd >= 0)
1521 close(log_to_fd);
1522 log_to_fd = fd;
1523 log_to_file = 1;
1524 return 1;
1527 #ifdef PLUGIN
1528 static int
1529 loadplugin(argv)
1530 char **argv;
1532 char *arg = *argv;
1533 void *handle;
1534 const char *err;
1535 void (*init) __P((void));
1537 handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
1538 if (handle == 0) {
1539 err = dlerror();
1540 if (err != 0)
1541 option_error("%s", err);
1542 option_error("Couldn't load plugin %s", arg);
1543 return 0;
1545 init = (void (*)(void))dlsym(handle, "plugin_init");
1546 if (init == 0) {
1547 option_error("%s has no initialization entry point", arg);
1548 dlclose(handle);
1549 return 0;
1551 info("Plugin %s loaded.", arg);
1552 (*init)();
1553 return 1;
1555 #endif /* PLUGIN */