1/*
2 * options.c - handles option processing for PPP.
3 *
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * 3. The name "Carnegie Mellon University" must not be used to
19 *    endorse or promote products derived from this software without
20 *    prior written permission. For permission or any legal
21 *    details, please contact
22 *      Office of Technology Transfer
23 *      Carnegie Mellon University
24 *      5000 Forbes Avenue
25 *      Pittsburgh, PA  15213-3890
26 *      (412) 268-4387, fax: (412) 268-7395
27 *      tech-transfer@andrew.cmu.edu
28 *
29 * 4. Redistributions of any form whatsoever must retain the following
30 *    acknowledgment:
31 *    "This product includes software developed by Computing Services
32 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33 *
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 */
42
43#define RCSID	"$Id: options.c,v 1.102 2008/06/15 06:53:06 paulus Exp $"
44
45#include <ctype.h>
46#include <stdio.h>
47#include <errno.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <stdlib.h>
51#include <syslog.h>
52#include <string.h>
53#include <pwd.h>
54#ifdef PLUGIN
55#include <dlfcn.h>
56#endif
57
58#ifdef PPP_FILTER
59#include <pcap.h>
60/*
61 * There have been 3 or 4 different names for this in libpcap CVS, but
62 * this seems to be what they have settled on...
63 * For older versions of libpcap, use DLT_PPP - but that means
64 * we lose the inbound and outbound qualifiers.
65 */
66#ifndef DLT_PPP_PPPD
67#ifdef DLT_PPP_WITHDIRECTION
68#define DLT_PPP_PPPD	DLT_PPP_WITHDIRECTION
69#else
70#define DLT_PPP_PPPD	DLT_PPP
71#endif
72#endif
73#endif /* PPP_FILTER */
74
75#include "pppd.h"
76#include "pathnames.h"
77
78#if defined(ultrix) || defined(NeXT)
79char *strdup __P((char *));
80#endif
81
82static const char rcsid[] = RCSID;
83
84struct option_value {
85    struct option_value *next;
86    const char *source;
87    char value[1];
88};
89
90/*
91 * Option variables and default values.
92 */
93int	debug = 0;		/* Debug flag */
94int	kdebugflag = 0;		/* Tell kernel to print debug messages */
95int	default_device = 1;	/* Using /dev/tty or equivalent */
96char	devnam[MAXPATHLEN];	/* Device name */
97bool	nodetach = 0;		/* Don't detach from controlling tty */
98bool	updetach = 0;		/* Detach once link is up */
99bool	master_detach;		/* Detach when we're (only) multilink master */
100int	maxconnect = 0;		/* Maximum connect time */
101char	user[MAXNAMELEN];	/* Username for PAP */
102char	passwd[MAXSECRETLEN];	/* Password for PAP */
103bool	persist = 0;		/* Reopen link after it goes down */
104char	our_name[MAXNAMELEN];	/* Our name for authentication purposes */
105bool	demand = 0;		/* do dial-on-demand */
106char	*ipparam = NULL;	/* Extra parameter for ip up/down scripts */
107int	idle_time_limit = 0;	/* Disconnect if idle for this many seconds */
108int	holdoff = 30;		/* # seconds to pause before reconnecting */
109bool	holdoff_specified;	/* true if a holdoff value has been given */
110int	log_to_fd = 1;		/* send log messages to this fd too */
111bool	log_default = 1;	/* log_to_fd is default (stdout) */
112int	maxfail = 10;		/* max # of unsuccessful connection attempts */
113char	linkname[MAXPATHLEN];	/* logical name for link */
114bool	tune_kernel;		/* may alter kernel settings */
115int	connect_delay = 1000;	/* wait this many ms after connect script */
116int	req_unit = -1;		/* requested interface unit */
117bool	multilink = 0;		/* Enable multilink operation */
118char	*bundle_name = NULL;	/* bundle name for multilink */
119bool	dump_options;		/* print out option values */
120bool	dryrun;			/* print out option values and exit */
121char	*domain;		/* domain name set by domain option */
122int	child_wait = 5;		/* # seconds to wait for children at exit */
123struct userenv *userenv_list;	/* user environment variables */
124
125#ifdef MAXOCTETS
126unsigned int  maxoctets = 0;    /* default - no limit */
127int maxoctets_dir = 0;       /* default - sum of traffic */
128int maxoctets_timeout = 1;   /* default 1 second */
129#endif
130
131
132extern option_t auth_options[];
133extern struct stat devstat;
134
135#ifdef PPP_FILTER
136struct	bpf_program pass_filter;/* Filter program for packets to pass */
137struct	bpf_program active_filter; /* Filter program for link-active pkts */
138#endif
139
140static option_t *curopt;	/* pointer to option being processed */
141char *current_option;		/* the name of the option being parsed */
142int  privileged_option;		/* set iff the current option came from root */
143char *option_source;		/* string saying where the option came from */
144int  option_priority = OPRIO_CFGFILE; /* priority of the current options */
145bool devnam_fixed;		/* can no longer change device name */
146
147static int logfile_fd = -1;	/* fd opened for log file */
148static char logfile_name[MAXPATHLEN];	/* name of log file */
149
150/*
151 * Prototypes
152 */
153static int setdomain __P((char **));
154static int readfile __P((char **));
155static int callfile __P((char **));
156static int showversion __P((char **));
157static int showhelp __P((char **));
158static void usage __P((void));
159static int setlogfile __P((char **));
160#ifdef PLUGIN
161static int loadplugin __P((char **));
162#endif
163
164#ifdef PPP_FILTER
165static int setpassfilter __P((char **));
166static int setactivefilter __P((char **));
167#endif
168
169#ifdef MAXOCTETS
170static int setmodir __P((char **));
171#endif
172
173static int user_setenv __P((char **));
174static void user_setprint __P((option_t *, printer_func, void *));
175static int user_unsetenv __P((char **));
176static void user_unsetprint __P((option_t *, printer_func, void *));
177
178static option_t *find_option __P((const char *name));
179static int process_option __P((option_t *, char *, char **));
180static int n_arguments __P((option_t *));
181static int number_option __P((char *, u_int32_t *, int));
182
183/*
184 * Structure to store extra lists of options.
185 */
186struct option_list {
187    option_t *options;
188    struct option_list *next;
189};
190
191static struct option_list *extra_options = NULL;
192
193/*
194 * Valid arguments.
195 */
196option_t general_options[] = {
197    { "debug", o_int, &debug,
198      "Increase debugging level", OPT_INC | OPT_NOARG | 1 },
199    { "-d", o_int, &debug,
200      "Increase debugging level",
201      OPT_ALIAS | OPT_INC | OPT_NOARG | 1 },
202
203    { "kdebug", o_int, &kdebugflag,
204      "Set kernel driver debug level", OPT_PRIO },
205
206    { "nodetach", o_bool, &nodetach,
207      "Don't detach from controlling tty", OPT_PRIO | 1 },
208    { "-detach", o_bool, &nodetach,
209      "Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
210    { "updetach", o_bool, &updetach,
211      "Detach from controlling tty once link is up",
212      OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
213
214    { "master_detach", o_bool, &master_detach,
215      "Detach when we're multilink master but have no link", 1 },
216
217    { "holdoff", o_int, &holdoff,
218      "Set time in seconds before retrying connection",
219      OPT_PRIO, &holdoff_specified },
220
221    { "idle", o_int, &idle_time_limit,
222      "Set time in seconds before disconnecting idle link", OPT_PRIO },
223
224    { "maxconnect", o_int, &maxconnect,
225      "Set connection time limit",
226      OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
227
228    { "domain", o_special, (void *)setdomain,
229      "Add given domain name to hostname",
230      OPT_PRIO | OPT_PRIV | OPT_A2STRVAL, &domain },
231
232    { "file", o_special, (void *)readfile,
233      "Take options from a file", OPT_NOPRINT },
234    { "call", o_special, (void *)callfile,
235      "Take options from a privileged file", OPT_NOPRINT },
236
237    { "persist", o_bool, &persist,
238      "Keep on reopening connection after close", OPT_PRIO | 1 },
239    { "nopersist", o_bool, &persist,
240      "Turn off persist option", OPT_PRIOSUB },
241
242    { "demand", o_bool, &demand,
243      "Dial on demand", OPT_INITONLY | 1, &persist },
244
245    { "--version", o_special_noarg, (void *)showversion,
246      "Show version number" },
247    { "--help", o_special_noarg, (void *)showhelp,
248      "Show brief listing of options" },
249    { "-h", o_special_noarg, (void *)showhelp,
250      "Show brief listing of options", OPT_ALIAS },
251
252    { "logfile", o_special, (void *)setlogfile,
253      "Append log messages to this file",
254      OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, &logfile_name },
255    { "logfd", o_int, &log_to_fd,
256      "Send log messages to this file descriptor",
257      OPT_PRIOSUB | OPT_A2CLR, &log_default },
258    { "nolog", o_int, &log_to_fd,
259      "Don't send log messages to any file",
260      OPT_PRIOSUB | OPT_NOARG | OPT_VAL(-1) },
261    { "nologfd", o_int, &log_to_fd,
262      "Don't send log messages to any file descriptor",
263      OPT_PRIOSUB | OPT_ALIAS | OPT_NOARG | OPT_VAL(-1) },
264
265    { "linkname", o_string, linkname,
266      "Set logical name for link",
267      OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXPATHLEN },
268
269    { "maxfail", o_int, &maxfail,
270      "Maximum number of unsuccessful connection attempts to allow",
271      OPT_PRIO },
272
273    { "ktune", o_bool, &tune_kernel,
274      "Alter kernel settings as necessary", OPT_PRIO | 1 },
275    { "noktune", o_bool, &tune_kernel,
276      "Don't alter kernel settings", OPT_PRIOSUB },
277
278    { "connect-delay", o_int, &connect_delay,
279      "Maximum time (in ms) to wait after connect script finishes",
280      OPT_PRIO },
281
282    { "unit", o_int, &req_unit,
283      "PPP interface unit number to use if possible",
284      OPT_PRIO | OPT_LLIMIT, 0, 0 },
285
286    { "dump", o_bool, &dump_options,
287      "Print out option values after parsing all options", 1 },
288    { "dryrun", o_bool, &dryrun,
289      "Stop after parsing, printing, and checking options", 1 },
290
291    { "child-timeout", o_int, &child_wait,
292      "Number of seconds to wait for child processes at exit",
293      OPT_PRIO },
294
295    { "set", o_special, (void *)user_setenv,
296      "Set user environment variable",
297      OPT_A2PRINTER | OPT_NOPRINT, (void *)user_setprint },
298    { "unset", o_special, (void *)user_unsetenv,
299      "Unset user environment variable",
300      OPT_A2PRINTER | OPT_NOPRINT, (void *)user_unsetprint },
301
302#ifdef HAVE_MULTILINK
303    { "multilink", o_bool, &multilink,
304      "Enable multilink operation", OPT_PRIO | 1 },
305    { "mp", o_bool, &multilink,
306      "Enable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 1 },
307    { "nomultilink", o_bool, &multilink,
308      "Disable multilink operation", OPT_PRIOSUB | 0 },
309    { "nomp", o_bool, &multilink,
310      "Disable multilink operation", OPT_PRIOSUB | OPT_ALIAS | 0 },
311
312    { "bundle", o_string, &bundle_name,
313      "Bundle name for multilink", OPT_PRIO },
314#endif /* HAVE_MULTILINK */
315
316#ifdef PLUGIN
317    { "plugin", o_special, (void *)loadplugin,
318      "Load a plug-in module into pppd", OPT_PRIV | OPT_A2LIST },
319#endif
320
321#ifdef PPP_FILTER
322    { "pass-filter", o_special, setpassfilter,
323      "set filter for packets to pass", OPT_PRIO },
324
325    { "active-filter", o_special, setactivefilter,
326      "set filter for active pkts", OPT_PRIO },
327#endif
328
329#ifdef MAXOCTETS
330    { "maxoctets", o_int, &maxoctets,
331      "Set connection traffic limit",
332      OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
333    { "mo", o_int, &maxoctets,
334      "Set connection traffic limit",
335      OPT_ALIAS | OPT_PRIO | OPT_LLIMIT | OPT_NOINCR | OPT_ZEROINF },
336    { "mo-direction", o_special, setmodir,
337      "Set direction for limit traffic (sum,in,out,max)" },
338    { "mo-timeout", o_int, &maxoctets_timeout,
339      "Check for traffic limit every N seconds", OPT_PRIO | OPT_LLIMIT | 1 },
340#endif
341
342    { NULL }
343};
344
345#ifndef IMPLEMENTATION
346#define IMPLEMENTATION ""
347#endif
348
349static char *usage_string = "\
350pppd version %s\n\
351Usage: %s [ options ], where options are:\n\
352	<device>	Communicate over the named device\n\
353	<speed>		Set the baud rate to <speed>\n\
354	<loc>:<rem>	Set the local and/or remote interface IP\n\
355			addresses.  Either one may be omitted.\n\
356	asyncmap <n>	Set the desired async map to hex <n>\n\
357	auth		Require authentication from peer\n\
358        connect <p>     Invoke shell command <p> to set up the serial line\n\
359	crtscts		Use hardware RTS/CTS flow control\n\
360	defaultroute	Add default route through interface\n\
361	file <f>	Take options from file <f>\n\
362	modem		Use modem control lines\n\
363	mru <n>		Set MRU value to <n> for negotiation\n\
364See pppd(8) for more options.\n\
365";
366
367/*
368 * parse_args - parse a string of arguments from the command line.
369 */
370int
371parse_args(argc, argv)
372    int argc;
373    char **argv;
374{
375    char *arg;
376    option_t *opt;
377    int n;
378
379    privileged_option = privileged;
380    option_source = "command line";
381    option_priority = OPRIO_CMDLINE;
382    while (argc > 0) {
383	arg = *argv++;
384	--argc;
385	opt = find_option(arg);
386	if (opt == NULL) {
387	    option_error("unrecognized option '%s'", arg);
388	    usage();
389	    return 0;
390	}
391	n = n_arguments(opt);
392	if (argc < n) {
393	    option_error("too few parameters for option %s", arg);
394	    return 0;
395	}
396	if (!process_option(opt, arg, argv))
397	    return 0;
398	argc -= n;
399	argv += n;
400    }
401    return 1;
402}
403
404/*
405 * options_from_file - Read a string of options from a file,
406 * and interpret them.
407 */
408int
409options_from_file(filename, must_exist, check_prot, priv)
410    char *filename;
411    int must_exist;
412    int check_prot;
413    int priv;
414{
415    FILE *f;
416    int i, newline, ret, err;
417    option_t *opt;
418    int oldpriv, n;
419    char *oldsource;
420    uid_t euid;
421    char *argv[MAXARGS];
422    char args[MAXARGS][MAXWORDLEN];
423    char cmd[MAXWORDLEN];
424
425    euid = geteuid();
426    if (check_prot && seteuid(getuid()) == -1) {
427	option_error("unable to drop privileges to open %s: %m", filename);
428	return 0;
429    }
430    f = fopen(filename, "r");
431    err = errno;
432    if (check_prot && seteuid(euid) == -1)
433	fatal("unable to regain privileges");
434    if (f == NULL) {
435	errno = err;
436	if (!must_exist) {
437	    if (err != ENOENT && err != ENOTDIR)
438		warn("Warning: can't open options file %s: %m", filename);
439	    return 1;
440	}
441	option_error("Can't open options file %s: %m", filename);
442	return 0;
443    }
444
445    oldpriv = privileged_option;
446    privileged_option = priv;
447    oldsource = option_source;
448    option_source = strdup(filename);
449    if (option_source == NULL)
450	option_source = "file";
451    ret = 0;
452    while (getword(f, cmd, &newline, filename)) {
453	opt = find_option(cmd);
454	if (opt == NULL) {
455	    option_error("In file %s: unrecognized option '%s'",
456			 filename, cmd);
457	    goto err;
458	}
459	n = n_arguments(opt);
460	for (i = 0; i < n; ++i) {
461	    if (!getword(f, args[i], &newline, filename)) {
462		option_error(
463			"In file %s: too few parameters for option '%s'",
464			filename, cmd);
465		goto err;
466	    }
467	    argv[i] = args[i];
468	}
469	if (!process_option(opt, cmd, argv))
470	    goto err;
471    }
472    ret = 1;
473
474err:
475    fclose(f);
476    privileged_option = oldpriv;
477    option_source = oldsource;
478    return ret;
479}
480
481/*
482 * options_from_user - See if the use has a ~/.ppprc file,
483 * and if so, interpret options from it.
484 */
485int
486options_from_user()
487{
488    char *user, *path, *file;
489    int ret;
490    struct passwd *pw;
491    size_t pl;
492
493    pw = getpwuid(getuid());
494    if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
495	return 1;
496    file = _PATH_USEROPT;
497    pl = strlen(user) + strlen(file) + 2;
498    path = malloc(pl);
499    if (path == NULL)
500	novm("init file name");
501    slprintf(path, pl, "%s/%s", user, file);
502    option_priority = OPRIO_CFGFILE;
503    ret = options_from_file(path, 0, 1, privileged);
504    free(path);
505    return ret;
506}
507
508/*
509 * options_for_tty - See if an options file exists for the serial
510 * device, and if so, interpret options from it.
511 * We only allow the per-tty options file to override anything from
512 * the command line if it is something that the user can't override
513 * once it has been set by root; this is done by giving configuration
514 * files a lower priority than the command line.
515 */
516int
517options_for_tty()
518{
519    char *dev, *path, *p;
520    int ret;
521    size_t pl;
522
523    dev = devnam;
524    if ((p = strstr(dev, "/dev/")) != NULL)
525	dev = p + 5;
526    if (dev[0] == 0 || strcmp(dev, "tty") == 0)
527	return 1;		/* don't look for /etc/ppp/options.tty */
528    pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
529    path = malloc(pl);
530    if (path == NULL)
531	novm("tty init file name");
532    slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
533    /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
534    for (p = path + strlen(_PATH_TTYOPT); *p != 0; ++p)
535	if (*p == '/')
536	    *p = '.';
537    option_priority = OPRIO_CFGFILE;
538    ret = options_from_file(path, 0, 0, 1);
539    free(path);
540    return ret;
541}
542
543/*
544 * options_from_list - process a string of options in a wordlist.
545 */
546int
547options_from_list(w, priv)
548    struct wordlist *w;
549    int priv;
550{
551    char *argv[MAXARGS];
552    option_t *opt;
553    int i, n, ret = 0;
554    struct wordlist *w0;
555
556    privileged_option = priv;
557    option_source = "secrets file";
558    option_priority = OPRIO_SECFILE;
559
560    while (w != NULL) {
561	opt = find_option(w->word);
562	if (opt == NULL) {
563	    option_error("In secrets file: unrecognized option '%s'",
564			 w->word);
565	    goto err;
566	}
567	n = n_arguments(opt);
568	w0 = w;
569	for (i = 0; i < n; ++i) {
570	    w = w->next;
571	    if (w == NULL) {
572		option_error(
573			"In secrets file: too few parameters for option '%s'",
574			w0->word);
575		goto err;
576	    }
577	    argv[i] = w->word;
578	}
579	if (!process_option(opt, w0->word, argv))
580	    goto err;
581	w = w->next;
582    }
583    ret = 1;
584
585err:
586    return ret;
587}
588
589/*
590 * match_option - see if this option matches an option_t structure.
591 */
592static int
593match_option(name, opt, dowild)
594    char *name;
595    option_t *opt;
596    int dowild;
597{
598	int (*match) __P((char *, char **, int));
599
600	if (dowild != (opt->type == o_wild))
601		return 0;
602	if (!dowild)
603		return strcmp(name, opt->name) == 0;
604	match = (int (*) __P((char *, char **, int))) opt->addr;
605	return (*match)(name, NULL, 0);
606}
607
608/*
609 * find_option - scan the option lists for the various protocols
610 * looking for an entry with the given name.
611 * This could be optimized by using a hash table.
612 */
613static option_t *
614find_option(name)
615    const char *name;
616{
617	option_t *opt;
618	struct option_list *list;
619	int i, dowild;
620
621	for (dowild = 0; dowild <= 1; ++dowild) {
622		for (opt = general_options; opt->name != NULL; ++opt)
623			if (match_option(name, opt, dowild))
624				return opt;
625		for (opt = auth_options; opt->name != NULL; ++opt)
626			if (match_option(name, opt, dowild))
627				return opt;
628		for (list = extra_options; list != NULL; list = list->next)
629			for (opt = list->options; opt->name != NULL; ++opt)
630				if (match_option(name, opt, dowild))
631					return opt;
632		for (opt = the_channel->options; opt->name != NULL; ++opt)
633			if (match_option(name, opt, dowild))
634				return opt;
635		for (i = 0; protocols[i] != NULL; ++i)
636			if ((opt = protocols[i]->options) != NULL)
637				for (; opt->name != NULL; ++opt)
638					if (match_option(name, opt, dowild))
639						return opt;
640	}
641	return NULL;
642}
643
644/*
645 * process_option - process one new-style option.
646 */
647static int
648process_option(opt, cmd, argv)
649    option_t *opt;
650    char *cmd;
651    char **argv;
652{
653    u_int32_t v;
654    int iv, a;
655    char *sv;
656    int (*parser) __P((char **));
657    int (*wildp) __P((char *, char **, int));
658    char *optopt = (opt->type == o_wild)? "": " option";
659    int prio = option_priority;
660    option_t *mainopt = opt;
661
662    current_option = opt->name;
663    if ((opt->flags & OPT_PRIVFIX) && privileged_option)
664	prio += OPRIO_ROOT;
665    while (mainopt->flags & OPT_PRIOSUB)
666	--mainopt;
667    if (mainopt->flags & OPT_PRIO) {
668	if (prio < mainopt->priority) {
669	    /* new value doesn't override old */
670	    if (prio == OPRIO_CMDLINE && mainopt->priority > OPRIO_ROOT) {
671		option_error("%s%s set in %s cannot be overridden\n",
672			     opt->name, optopt, mainopt->source);
673		return 0;
674	    }
675	    return 1;
676	}
677	if (prio > OPRIO_ROOT && mainopt->priority == OPRIO_CMDLINE)
678	    warn("%s%s from %s overrides command line",
679		 opt->name, optopt, option_source);
680    }
681
682    if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
683	option_error("%s%s cannot be changed after initialization",
684		     opt->name, optopt);
685	return 0;
686    }
687    if ((opt->flags & OPT_PRIV) && !privileged_option) {
688	option_error("using the %s%s requires root privilege",
689		     opt->name, optopt);
690	return 0;
691    }
692    if ((opt->flags & OPT_ENABLE) && *(bool *)(opt->addr2) == 0) {
693	option_error("%s%s is disabled", opt->name, optopt);
694	return 0;
695    }
696    if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
697	option_error("the %s%s may not be changed in %s",
698		     opt->name, optopt, option_source);
699	return 0;
700    }
701
702    switch (opt->type) {
703    case o_bool:
704	v = opt->flags & OPT_VALUE;
705	*(bool *)(opt->addr) = v;
706	if (opt->addr2 && (opt->flags & OPT_A2COPY))
707	    *(bool *)(opt->addr2) = v;
708	else if (opt->addr2 && (opt->flags & OPT_A2CLR))
709	    *(bool *)(opt->addr2) = 0;
710	else if (opt->addr2 && (opt->flags & OPT_A2CLRB))
711	    *(u_char *)(opt->addr2) &= ~v;
712	else if (opt->addr2 && (opt->flags & OPT_A2OR))
713	    *(u_char *)(opt->addr2) |= v;
714	break;
715
716    case o_int:
717	iv = 0;
718	if ((opt->flags & OPT_NOARG) == 0) {
719	    if (!int_option(*argv, &iv))
720		return 0;
721	    if ((((opt->flags & OPT_LLIMIT) && iv < opt->lower_limit)
722		 || ((opt->flags & OPT_ULIMIT) && iv > opt->upper_limit))
723		&& !((opt->flags & OPT_ZEROOK && iv == 0))) {
724		char *zok = (opt->flags & OPT_ZEROOK)? " zero or": "";
725		switch (opt->flags & OPT_LIMITS) {
726		case OPT_LLIMIT:
727		    option_error("%s value must be%s >= %d",
728				 opt->name, zok, opt->lower_limit);
729		    break;
730		case OPT_ULIMIT:
731		    option_error("%s value must be%s <= %d",
732				 opt->name, zok, opt->upper_limit);
733		    break;
734		case OPT_LIMITS:
735		    option_error("%s value must be%s between %d and %d",
736				opt->name, zok, opt->lower_limit, opt->upper_limit);
737		    break;
738		}
739		return 0;
740	    }
741	}
742	a = opt->flags & OPT_VALUE;
743	if (a >= 128)
744	    a -= 256;		/* sign extend */
745	iv += a;
746	if (opt->flags & OPT_INC)
747	    iv += *(int *)(opt->addr);
748	if ((opt->flags & OPT_NOINCR) && !privileged_option) {
749	    int oldv = *(int *)(opt->addr);
750	    if ((opt->flags & OPT_ZEROINF) ?
751		(oldv != 0 && (iv == 0 || iv > oldv)) : (iv > oldv)) {
752		option_error("%s value cannot be increased", opt->name);
753		return 0;
754	    }
755	}
756	*(int *)(opt->addr) = iv;
757	if (opt->addr2 && (opt->flags & OPT_A2COPY))
758	    *(int *)(opt->addr2) = iv;
759	break;
760
761    case o_uint32:
762	if (opt->flags & OPT_NOARG) {
763	    v = opt->flags & OPT_VALUE;
764	    if (v & 0x80)
765		    v |= 0xffffff00U;
766	} else if (!number_option(*argv, &v, 16))
767	    return 0;
768	if (opt->flags & OPT_OR)
769	    v |= *(u_int32_t *)(opt->addr);
770	*(u_int32_t *)(opt->addr) = v;
771	if (opt->addr2 && (opt->flags & OPT_A2COPY))
772	    *(u_int32_t *)(opt->addr2) = v;
773	break;
774
775    case o_string:
776	if (opt->flags & OPT_STATIC) {
777	    strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
778	} else {
779	    char **optptr = (char **)(opt->addr);
780	    sv = strdup(*argv);
781	    if (sv == NULL)
782		novm("option argument");
783	    if (*optptr)
784		free(*optptr);
785	    *optptr = sv;
786	}
787	break;
788
789    case o_special_noarg:
790    case o_special:
791	parser = (int (*) __P((char **))) opt->addr;
792	curopt = opt;
793	if (!(*parser)(argv))
794	    return 0;
795	if (opt->flags & OPT_A2LIST) {
796	    struct option_value *ovp, *pp;
797
798	    ovp = malloc(sizeof(*ovp) + strlen(*argv));
799	    if (ovp != 0) {
800		strcpy(ovp->value, *argv);
801		ovp->source = option_source;
802		ovp->next = NULL;
803		if (opt->addr2 == NULL) {
804		    opt->addr2 = ovp;
805		} else {
806		    for (pp = opt->addr2; pp->next != NULL; pp = pp->next)
807			;
808		    pp->next = ovp;
809		}
810	    }
811	}
812	break;
813
814    case o_wild:
815	wildp = (int (*) __P((char *, char **, int))) opt->addr;
816	if (!(*wildp)(cmd, argv, 1))
817	    return 0;
818	break;
819    }
820
821    /*
822     * If addr2 wasn't used by any flag (OPT_A2COPY, etc.) but is set,
823     * treat it as a bool and set/clear it based on the OPT_A2CLR bit.
824     */
825    if (opt->addr2 && (opt->flags & (OPT_A2COPY|OPT_ENABLE
826		|OPT_A2PRINTER|OPT_A2STRVAL|OPT_A2LIST|OPT_A2OR)) == 0)
827	*(bool *)(opt->addr2) = !(opt->flags & OPT_A2CLR);
828
829    mainopt->source = option_source;
830    mainopt->priority = prio;
831    mainopt->winner = opt - mainopt;
832
833    return 1;
834}
835
836/*
837 * override_value - if the option priorities would permit us to
838 * override the value of option, return 1 and update the priority
839 * and source of the option value.  Otherwise returns 0.
840 */
841int
842override_value(option, priority, source)
843    const char *option;
844    int priority;
845    const char *source;
846{
847	option_t *opt;
848
849	opt = find_option(option);
850	if (opt == NULL)
851		return 0;
852	while (opt->flags & OPT_PRIOSUB)
853		--opt;
854	if ((opt->flags & OPT_PRIO) && priority < opt->priority)
855		return 0;
856	opt->priority = priority;
857	opt->source = source;
858	opt->winner = -1;
859	return 1;
860}
861
862/*
863 * n_arguments - tell how many arguments an option takes
864 */
865static int
866n_arguments(opt)
867    option_t *opt;
868{
869	return (opt->type == o_bool || opt->type == o_special_noarg
870		|| (opt->flags & OPT_NOARG))? 0: 1;
871}
872
873/*
874 * add_options - add a list of options to the set we grok.
875 */
876void
877add_options(opt)
878    option_t *opt;
879{
880    struct option_list *list;
881
882    list = malloc(sizeof(*list));
883    if (list == 0)
884	novm("option list entry");
885    list->options = opt;
886    list->next = extra_options;
887    extra_options = list;
888}
889
890/*
891 * check_options - check that options are valid and consistent.
892 */
893void
894check_options()
895{
896	if (logfile_fd >= 0 && logfile_fd != log_to_fd)
897		close(logfile_fd);
898}
899
900/*
901 * print_option - print out an option and its value
902 */
903static void
904print_option(opt, mainopt, printer, arg)
905    option_t *opt, *mainopt;
906    printer_func printer;
907    void *arg;
908{
909	int i, v;
910	char *p;
911
912	if (opt->flags & OPT_NOPRINT)
913		return;
914	switch (opt->type) {
915	case o_bool:
916		v = opt->flags & OPT_VALUE;
917		if (*(bool *)opt->addr != v)
918			/* this can happen legitimately, e.g. lock
919			   option turned off for default device */
920			break;
921		printer(arg, "%s", opt->name);
922		break;
923	case o_int:
924		v = opt->flags & OPT_VALUE;
925		if (v >= 128)
926			v -= 256;
927		i = *(int *)opt->addr;
928		if (opt->flags & OPT_NOARG) {
929			printer(arg, "%s", opt->name);
930			if (i != v) {
931				if (opt->flags & OPT_INC) {
932					for (; i > v; i -= v)
933						printer(arg, " %s", opt->name);
934				} else
935					printer(arg, " # oops: %d not %d\n",
936						i, v);
937			}
938		} else {
939			printer(arg, "%s %d", opt->name, i);
940		}
941		break;
942	case o_uint32:
943		printer(arg, "%s", opt->name);
944		if ((opt->flags & OPT_NOARG) == 0)
945			printer(arg, " %x", *(u_int32_t *)opt->addr);
946		break;
947
948	case o_string:
949		if (opt->flags & OPT_HIDE) {
950			p = "??????";
951		} else {
952			p = (char *) opt->addr;
953			if ((opt->flags & OPT_STATIC) == 0)
954				p = *(char **)p;
955		}
956		printer(arg, "%s %q", opt->name, p);
957		break;
958
959	case o_special:
960	case o_special_noarg:
961	case o_wild:
962		if (opt->type != o_wild) {
963			printer(arg, "%s", opt->name);
964			if (n_arguments(opt) == 0)
965				break;
966			printer(arg, " ");
967		}
968		if (opt->flags & OPT_A2PRINTER) {
969			void (*oprt) __P((option_t *, printer_func, void *));
970			oprt = (void (*) __P((option_t *, printer_func,
971					 void *)))opt->addr2;
972			(*oprt)(opt, printer, arg);
973		} else if (opt->flags & OPT_A2STRVAL) {
974			p = (char *) opt->addr2;
975			if ((opt->flags & OPT_STATIC) == 0)
976				p = *(char **)p;
977			printer("%q", p);
978		} else if (opt->flags & OPT_A2LIST) {
979			struct option_value *ovp;
980
981			ovp = (struct option_value *) opt->addr2;
982			for (;;) {
983				printer(arg, "%q", ovp->value);
984				if ((ovp = ovp->next) == NULL)
985					break;
986				printer(arg, "\t\t# (from %s)\n%s ",
987					ovp->source, opt->name);
988			}
989		} else {
990			printer(arg, "xxx # [don't know how to print value]");
991		}
992		break;
993
994	default:
995		printer(arg, "# %s value (type %d\?\?)", opt->name, opt->type);
996		break;
997	}
998	printer(arg, "\t\t# (from %s)\n", mainopt->source);
999}
1000
1001/*
1002 * print_option_list - print out options in effect from an
1003 * array of options.
1004 */
1005static void
1006print_option_list(opt, printer, arg)
1007    option_t *opt;
1008    printer_func printer;
1009    void *arg;
1010{
1011	while (opt->name != NULL) {
1012		if (opt->priority != OPRIO_DEFAULT
1013		    && opt->winner != (short int) -1)
1014			print_option(opt + opt->winner, opt, printer, arg);
1015		do {
1016			++opt;
1017		} while (opt->flags & OPT_PRIOSUB);
1018	}
1019}
1020
1021/*
1022 * print_options - print out what options are in effect.
1023 */
1024void
1025print_options(printer, arg)
1026    printer_func printer;
1027    void *arg;
1028{
1029	struct option_list *list;
1030	int i;
1031
1032	printer(arg, "pppd options in effect:\n");
1033	print_option_list(general_options, printer, arg);
1034	print_option_list(auth_options, printer, arg);
1035	for (list = extra_options; list != NULL; list = list->next)
1036		print_option_list(list->options, printer, arg);
1037	print_option_list(the_channel->options, printer, arg);
1038	for (i = 0; protocols[i] != NULL; ++i)
1039		print_option_list(protocols[i]->options, printer, arg);
1040}
1041
1042/*
1043 * usage - print out a message telling how to use the program.
1044 */
1045static void
1046usage()
1047{
1048    if (phase == PHASE_INITIALIZE)
1049	fprintf(stderr, usage_string, VERSION, progname);
1050}
1051
1052/*
1053 * showhelp - print out usage message and exit.
1054 */
1055static int
1056showhelp(argv)
1057    char **argv;
1058{
1059    if (phase == PHASE_INITIALIZE) {
1060	usage();
1061	exit(0);
1062    }
1063    return 0;
1064}
1065
1066/*
1067 * showversion - print out the version number and exit.
1068 */
1069static int
1070showversion(argv)
1071    char **argv;
1072{
1073    if (phase == PHASE_INITIALIZE) {
1074	fprintf(stderr, "pppd version %s\n", VERSION);
1075	exit(0);
1076    }
1077    return 0;
1078}
1079
1080/*
1081 * option_error - print a message about an error in an option.
1082 * The message is logged, and also sent to
1083 * stderr if phase == PHASE_INITIALIZE.
1084 */
1085void
1086option_error __V((char *fmt, ...))
1087{
1088    va_list args;
1089    char buf[1024];
1090
1091#if defined(__STDC__)
1092    va_start(args, fmt);
1093#else
1094    char *fmt;
1095    va_start(args);
1096    fmt = va_arg(args, char *);
1097#endif
1098    vslprintf(buf, sizeof(buf), fmt, args);
1099    va_end(args);
1100    if (phase == PHASE_INITIALIZE)
1101	fprintf(stderr, "%s: %s\n", progname, buf);
1102    syslog(LOG_ERR, "%s", buf);
1103}
1104
1105#if 0
1106/*
1107 * readable - check if a file is readable by the real user.
1108 */
1109int
1110readable(fd)
1111    int fd;
1112{
1113    uid_t uid;
1114    int i;
1115    struct stat sbuf;
1116
1117    uid = getuid();
1118    if (uid == 0)
1119	return 1;
1120    if (fstat(fd, &sbuf) != 0)
1121	return 0;
1122    if (sbuf.st_uid == uid)
1123	return sbuf.st_mode & S_IRUSR;
1124    if (sbuf.st_gid == getgid())
1125	return sbuf.st_mode & S_IRGRP;
1126    for (i = 0; i < ngroups; ++i)
1127	if (sbuf.st_gid == groups[i])
1128	    return sbuf.st_mode & S_IRGRP;
1129    return sbuf.st_mode & S_IROTH;
1130}
1131#endif
1132
1133/*
1134 * Read a word from a file.
1135 * Words are delimited by white-space or by quotes (" or ').
1136 * Quotes, white-space and \ may be escaped with \.
1137 * \<newline> is ignored.
1138 */
1139int
1140getword(f, word, newlinep, filename)
1141    FILE *f;
1142    char *word;
1143    int *newlinep;
1144    char *filename;
1145{
1146    int c, len, escape;
1147    int quoted, comment;
1148    int value, digit, got, n;
1149
1150#define isoctal(c) ((c) >= '0' && (c) < '8')
1151
1152    *newlinep = 0;
1153    len = 0;
1154    escape = 0;
1155    comment = 0;
1156    quoted = 0;
1157
1158    /*
1159     * First skip white-space and comments.
1160     */
1161    for (;;) {
1162	c = getc(f);
1163	if (c == EOF)
1164	    break;
1165
1166	/*
1167	 * A newline means the end of a comment; backslash-newline
1168	 * is ignored.  Note that we cannot have escape && comment.
1169	 */
1170	if (c == '\n') {
1171	    if (!escape) {
1172		*newlinep = 1;
1173		comment = 0;
1174	    } else
1175		escape = 0;
1176	    continue;
1177	}
1178
1179	/*
1180	 * Ignore characters other than newline in a comment.
1181	 */
1182	if (comment)
1183	    continue;
1184
1185	/*
1186	 * If this character is escaped, we have a word start.
1187	 */
1188	if (escape)
1189	    break;
1190
1191	/*
1192	 * If this is the escape character, look at the next character.
1193	 */
1194	if (c == '\\') {
1195	    escape = 1;
1196	    continue;
1197	}
1198
1199	/*
1200	 * If this is the start of a comment, ignore the rest of the line.
1201	 */
1202	if (c == '#') {
1203	    comment = 1;
1204	    continue;
1205	}
1206
1207	/*
1208	 * A non-whitespace character is the start of a word.
1209	 */
1210	if (!isspace(c))
1211	    break;
1212    }
1213
1214    /*
1215     * Process characters until the end of the word.
1216     */
1217    while (c != EOF) {
1218	if (escape) {
1219	    /*
1220	     * This character is escaped: backslash-newline is ignored,
1221	     * various other characters indicate particular values
1222	     * as for C backslash-escapes.
1223	     */
1224	    escape = 0;
1225	    if (c == '\n') {
1226	        c = getc(f);
1227		continue;
1228	    }
1229
1230	    got = 0;
1231	    switch (c) {
1232	    case 'a':
1233		value = '\a';
1234		break;
1235	    case 'b':
1236		value = '\b';
1237		break;
1238	    case 'f':
1239		value = '\f';
1240		break;
1241	    case 'n':
1242		value = '\n';
1243		break;
1244	    case 'r':
1245		value = '\r';
1246		break;
1247	    case 's':
1248		value = ' ';
1249		break;
1250	    case 't':
1251		value = '\t';
1252		break;
1253
1254	    default:
1255		if (isoctal(c)) {
1256		    /*
1257		     * \ddd octal sequence
1258		     */
1259		    value = 0;
1260		    for (n = 0; n < 3 && isoctal(c); ++n) {
1261			value = (value << 3) + (c & 07);
1262			c = getc(f);
1263		    }
1264		    got = 1;
1265		    break;
1266		}
1267
1268		if (c == 'x') {
1269		    /*
1270		     * \x<hex_string> sequence
1271		     */
1272		    value = 0;
1273		    c = getc(f);
1274		    for (n = 0; n < 2 && isxdigit(c); ++n) {
1275			digit = toupper(c) - '0';
1276			if (digit > 10)
1277			    digit += '0' + 10 - 'A';
1278			value = (value << 4) + digit;
1279			c = getc (f);
1280		    }
1281		    got = 1;
1282		    break;
1283		}
1284
1285		/*
1286		 * Otherwise the character stands for itself.
1287		 */
1288		value = c;
1289		break;
1290	    }
1291
1292	    /*
1293	     * Store the resulting character for the escape sequence.
1294	     */
1295	    if (len < MAXWORDLEN) {
1296		word[len] = value;
1297		++len;
1298	    }
1299
1300	    if (!got)
1301		c = getc(f);
1302	    continue;
1303	}
1304
1305	/*
1306	 * Backslash starts a new escape sequence.
1307	 */
1308	if (c == '\\') {
1309	    escape = 1;
1310	    c = getc(f);
1311	    continue;
1312	}
1313
1314	/*
1315	 * Not escaped: check for the start or end of a quoted
1316	 * section and see if we've reached the end of the word.
1317	 */
1318	if (quoted) {
1319	    if (c == quoted) {
1320		quoted = 0;
1321		c = getc(f);
1322		continue;
1323	    }
1324	} else if (c == '"' || c == '\'') {
1325	    quoted = c;
1326	    c = getc(f);
1327	    continue;
1328	} else if (isspace(c) || c == '#') {
1329	    ungetc (c, f);
1330	    break;
1331	}
1332
1333	/*
1334	 * An ordinary character: store it in the word and get another.
1335	 */
1336	if (len < MAXWORDLEN) {
1337	    word[len] = c;
1338	    ++len;
1339	}
1340
1341	c = getc(f);
1342    }
1343
1344    /*
1345     * End of the word: check for errors.
1346     */
1347    if (c == EOF) {
1348	if (ferror(f)) {
1349	    if (errno == 0)
1350		errno = EIO;
1351	    option_error("Error reading %s: %m", filename);
1352	    die(1);
1353	}
1354	/*
1355	 * If len is zero, then we didn't find a word before the
1356	 * end of the file.
1357	 */
1358	if (len == 0)
1359	    return 0;
1360	if (quoted)
1361	    option_error("warning: quoted word runs to end of file (%.20s...)",
1362			 filename, word);
1363    }
1364
1365    /*
1366     * Warn if the word was too long, and append a terminating null.
1367     */
1368    if (len >= MAXWORDLEN) {
1369	option_error("warning: word in file %s too long (%.20s...)",
1370		     filename, word);
1371	len = MAXWORDLEN - 1;
1372    }
1373    word[len] = 0;
1374
1375    return 1;
1376
1377#undef isoctal
1378
1379}
1380
1381/*
1382 * number_option - parse an unsigned numeric parameter for an option.
1383 */
1384static int
1385number_option(str, valp, base)
1386    char *str;
1387    u_int32_t *valp;
1388    int base;
1389{
1390    char *ptr;
1391
1392    *valp = strtoul(str, &ptr, base);
1393    if (ptr == str) {
1394	option_error("invalid numeric parameter '%s' for %s option",
1395		     str, current_option);
1396	return 0;
1397    }
1398    return 1;
1399}
1400
1401
1402/*
1403 * int_option - like number_option, but valp is int *,
1404 * the base is assumed to be 0, and *valp is not changed
1405 * if there is an error.
1406 */
1407int
1408int_option(str, valp)
1409    char *str;
1410    int *valp;
1411{
1412    u_int32_t v;
1413
1414    if (!number_option(str, &v, 0))
1415	return 0;
1416    *valp = (int) v;
1417    return 1;
1418}
1419
1420
1421/*
1422 * The following procedures parse options.
1423 */
1424
1425/*
1426 * readfile - take commands from a file.
1427 */
1428static int
1429readfile(argv)
1430    char **argv;
1431{
1432    return options_from_file(*argv, 1, 1, privileged_option);
1433}
1434
1435/*
1436 * callfile - take commands from /etc/ppp/peers/<name>.
1437 * Name may not contain /../, start with / or ../, or end in /..
1438 */
1439static int
1440callfile(argv)
1441    char **argv;
1442{
1443    char *fname, *arg, *p;
1444    int l, ok;
1445
1446    arg = *argv;
1447    ok = 1;
1448    if (arg[0] == '/' || arg[0] == 0)
1449	ok = 0;
1450    else {
1451	for (p = arg; *p != 0; ) {
1452	    if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1453		ok = 0;
1454		break;
1455	    }
1456	    while (*p != '/' && *p != 0)
1457		++p;
1458	    if (*p == '/')
1459		++p;
1460	}
1461    }
1462    if (!ok) {
1463	option_error("call option value may not contain .. or start with /");
1464	return 0;
1465    }
1466
1467    l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1468    if ((fname = (char *) malloc(l)) == NULL)
1469	novm("call file name");
1470    slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
1471
1472    ok = options_from_file(fname, 1, 1, 1);
1473
1474    free(fname);
1475    return ok;
1476}
1477
1478#ifdef PPP_FILTER
1479/*
1480 * setpassfilter - Set the pass filter for packets
1481 */
1482static int
1483setpassfilter(argv)
1484    char **argv;
1485{
1486    pcap_t *pc;
1487    int ret = 1;
1488
1489    pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1490    if (pcap_compile(pc, &pass_filter, *argv, 1, netmask) == -1) {
1491	option_error("error in pass-filter expression: %s\n",
1492		     pcap_geterr(pc));
1493	ret = 0;
1494    }
1495    pcap_close(pc);
1496
1497    return ret;
1498}
1499
1500/*
1501 * setactivefilter - Set the active filter for packets
1502 */
1503static int
1504setactivefilter(argv)
1505    char **argv;
1506{
1507    pcap_t *pc;
1508    int ret = 1;
1509
1510    pc = pcap_open_dead(DLT_PPP_PPPD, 65535);
1511    if (pcap_compile(pc, &active_filter, *argv, 1, netmask) == -1) {
1512	option_error("error in active-filter expression: %s\n",
1513		     pcap_geterr(pc));
1514	ret = 0;
1515    }
1516    pcap_close(pc);
1517
1518    return ret;
1519}
1520#endif
1521
1522/*
1523 * setdomain - Set domain name to append to hostname
1524 */
1525static int
1526setdomain(argv)
1527    char **argv;
1528{
1529    gethostname(hostname, MAXNAMELEN);
1530    if (**argv != 0) {
1531	if (**argv != '.')
1532	    strncat(hostname, ".", MAXNAMELEN - strlen(hostname));
1533	domain = hostname + strlen(hostname);
1534	strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
1535    }
1536    hostname[MAXNAMELEN-1] = 0;
1537    return (1);
1538}
1539
1540static int
1541setlogfile(argv)
1542    char **argv;
1543{
1544    int fd, err;
1545    uid_t euid;
1546
1547    euid = geteuid();
1548    if (!privileged_option && seteuid(getuid()) == -1) {
1549	option_error("unable to drop permissions to open %s: %m", *argv);
1550	return 0;
1551    }
1552    fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
1553    if (fd < 0 && errno == EEXIST)
1554	fd = open(*argv, O_WRONLY | O_APPEND);
1555    err = errno;
1556    if (!privileged_option && seteuid(euid) == -1)
1557	fatal("unable to regain privileges: %m");
1558    if (fd < 0) {
1559	errno = err;
1560	option_error("Can't open log file %s: %m", *argv);
1561	return 0;
1562    }
1563    strlcpy(logfile_name, *argv, sizeof(logfile_name));
1564    if (logfile_fd >= 0)
1565	close(logfile_fd);
1566    logfile_fd = fd;
1567    log_to_fd = fd;
1568    log_default = 0;
1569    return 1;
1570}
1571
1572#ifdef MAXOCTETS
1573static int
1574setmodir(argv)
1575    char **argv;
1576{
1577    if(*argv == NULL)
1578	return 0;
1579    if(!strcmp(*argv,"in")) {
1580        maxoctets_dir = PPP_OCTETS_DIRECTION_IN;
1581    } else if (!strcmp(*argv,"out")) {
1582        maxoctets_dir = PPP_OCTETS_DIRECTION_OUT;
1583    } else if (!strcmp(*argv,"max")) {
1584        maxoctets_dir = PPP_OCTETS_DIRECTION_MAXOVERAL;
1585    } else {
1586        maxoctets_dir = PPP_OCTETS_DIRECTION_SUM;
1587    }
1588    return 1;
1589}
1590#endif
1591
1592#ifdef PLUGIN
1593static int
1594loadplugin(argv)
1595    char **argv;
1596{
1597    char *arg = *argv;
1598    void *handle;
1599    const char *err;
1600    void (*init) __P((void));
1601    char *path = arg;
1602    const char *vers;
1603
1604    if (strchr(arg, '/') == 0) {
1605	const char *base = _PATH_PLUGIN;
1606	int l = strlen(base) + strlen(arg) + 2;
1607	path = malloc(l);
1608	if (path == 0)
1609	    novm("plugin file path");
1610	strlcpy(path, base, l);
1611	strlcat(path, "/", l);
1612	strlcat(path, arg, l);
1613    }
1614    handle = dlopen(path, RTLD_GLOBAL | RTLD_NOW);
1615    if (handle == 0) {
1616	err = dlerror();
1617	if (err != 0)
1618	    option_error("%s", err);
1619	option_error("Couldn't load plugin %s", arg);
1620	goto err;
1621    }
1622    init = (void (*)(void))dlsym(handle, "plugin_init");
1623    if (init == 0) {
1624	option_error("%s has no initialization entry point", arg);
1625	goto errclose;
1626    }
1627    vers = (const char *) dlsym(handle, "pppd_version");
1628    if (vers == 0) {
1629	warn("Warning: plugin %s has no version information", arg);
1630    } else if (strcmp(vers, VERSION) != 0) {
1631	option_error("Plugin %s is for pppd version %s, this is %s",
1632		     arg, vers, VERSION);
1633	goto errclose;
1634    }
1635    info("Plugin %s loaded.", arg);
1636    (*init)();
1637    return 1;
1638
1639 errclose:
1640    dlclose(handle);
1641 err:
1642    if (path != arg)
1643	free(path);
1644    return 0;
1645}
1646#endif /* PLUGIN */
1647
1648/*
1649 * Set an environment variable specified by the user.
1650 */
1651static int
1652user_setenv(argv)
1653    char **argv;
1654{
1655    char *arg = argv[0];
1656    char *eqp;
1657    struct userenv *uep, **insp;
1658
1659    if ((eqp = strchr(arg, '=')) == NULL) {
1660	option_error("missing = in name=value: %s", arg);
1661	return 0;
1662    }
1663    if (eqp == arg) {
1664	option_error("missing variable name: %s", arg);
1665	return 0;
1666    }
1667    for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1668	int nlen = strlen(uep->ue_name);
1669	if (nlen == (eqp - arg) &&
1670	    strncmp(arg, uep->ue_name, nlen) == 0)
1671	    break;
1672    }
1673    /* Ignore attempts by unprivileged users to override privileged sources */
1674    if (uep != NULL && !privileged_option && uep->ue_priv)
1675	return 1;
1676    /* The name never changes, so allocate it with the structure */
1677    if (uep == NULL) {
1678	uep = malloc(sizeof (*uep) + (eqp-arg));
1679	strncpy(uep->ue_name, arg, eqp-arg);
1680	uep->ue_name[eqp-arg] = '\0';
1681	uep->ue_next = NULL;
1682	insp = &userenv_list;
1683	while (*insp != NULL)
1684	    insp = &(*insp)->ue_next;
1685	*insp = uep;
1686    } else {
1687	struct userenv *uep2;
1688	for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1689	    if (uep2 != uep && !uep2->ue_isset)
1690		break;
1691	}
1692	if (uep2 == NULL && !uep->ue_isset)
1693	    find_option("unset")->flags |= OPT_NOPRINT;
1694	free(uep->ue_value);
1695    }
1696    uep->ue_isset = 1;
1697    uep->ue_priv = privileged_option;
1698    uep->ue_source = option_source;
1699    uep->ue_value = strdup(eqp + 1);
1700    curopt->flags &= ~OPT_NOPRINT;
1701    return 1;
1702}
1703
1704static void
1705user_setprint(opt, printer, arg)
1706    option_t *opt;
1707    printer_func printer;
1708    void *arg;
1709{
1710    struct userenv *uep, *uepnext;
1711
1712    uepnext = userenv_list;
1713    while (uepnext != NULL && !uepnext->ue_isset)
1714	uepnext = uepnext->ue_next;
1715    while ((uep = uepnext) != NULL) {
1716	uepnext = uep->ue_next;
1717	while (uepnext != NULL && !uepnext->ue_isset)
1718	    uepnext = uepnext->ue_next;
1719	(*printer)(arg, "%s=%s", uep->ue_name, uep->ue_value);
1720	if (uepnext != NULL)
1721	    (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1722	else
1723	    opt->source = uep->ue_source;
1724    }
1725}
1726
1727static int
1728user_unsetenv(argv)
1729    char **argv;
1730{
1731    struct userenv *uep, **insp;
1732    char *arg = argv[0];
1733
1734    if (strchr(arg, '=') != NULL) {
1735	option_error("unexpected = in name: %s", arg);
1736	return 0;
1737    }
1738    if (arg == '\0') {
1739	option_error("missing variable name for unset");
1740	return 0;
1741    }
1742    for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1743	if (strcmp(arg, uep->ue_name) == 0)
1744	    break;
1745    }
1746    /* Ignore attempts by unprivileged users to override privileged sources */
1747    if (uep != NULL && !privileged_option && uep->ue_priv)
1748	return 1;
1749    /* The name never changes, so allocate it with the structure */
1750    if (uep == NULL) {
1751	uep = malloc(sizeof (*uep) + strlen(arg));
1752	strcpy(uep->ue_name, arg);
1753	uep->ue_next = NULL;
1754	insp = &userenv_list;
1755	while (*insp != NULL)
1756	    insp = &(*insp)->ue_next;
1757	*insp = uep;
1758    } else {
1759	struct userenv *uep2;
1760	for (uep2 = userenv_list; uep2 != NULL; uep2 = uep2->ue_next) {
1761	    if (uep2 != uep && uep2->ue_isset)
1762		break;
1763	}
1764	if (uep2 == NULL && uep->ue_isset)
1765	    find_option("set")->flags |= OPT_NOPRINT;
1766	free(uep->ue_value);
1767    }
1768    uep->ue_isset = 0;
1769    uep->ue_priv = privileged_option;
1770    uep->ue_source = option_source;
1771    uep->ue_value = NULL;
1772    curopt->flags &= ~OPT_NOPRINT;
1773    return 1;
1774}
1775
1776static void
1777user_unsetprint(opt, printer, arg)
1778    option_t *opt;
1779    printer_func printer;
1780    void *arg;
1781{
1782    struct userenv *uep, *uepnext;
1783
1784    uepnext = userenv_list;
1785    while (uepnext != NULL && uepnext->ue_isset)
1786	uepnext = uepnext->ue_next;
1787    while ((uep = uepnext) != NULL) {
1788	uepnext = uep->ue_next;
1789	while (uepnext != NULL && uepnext->ue_isset)
1790	    uepnext = uepnext->ue_next;
1791	(*printer)(arg, "%s", uep->ue_name);
1792	if (uepnext != NULL)
1793	    (*printer)(arg, "\t\t# (from %s)\n%s ", uep->ue_source, opt->name);
1794	else
1795	    opt->source = uep->ue_source;
1796    }
1797}
1798