17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * options.c - handles option processing for PPP.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
57c478bd9Sstevel@tonic-gate  * All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software and its
87c478bd9Sstevel@tonic-gate  * documentation is hereby granted, provided that the above copyright
97c478bd9Sstevel@tonic-gate  * notice appears in all copies.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
127c478bd9Sstevel@tonic-gate  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
137c478bd9Sstevel@tonic-gate  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
147c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
157c478bd9Sstevel@tonic-gate  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
167c478bd9Sstevel@tonic-gate  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * Copyright (c) 1989 Carnegie Mellon University.
197c478bd9Sstevel@tonic-gate  * All rights reserved.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
227c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
237c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
247c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
257c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
267c478bd9Sstevel@tonic-gate  * by Carnegie Mellon University.  The name of the
277c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
287c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
297c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
307c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
317c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <syslog.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <netdb.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <netinet/in.h>
477c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
487c478bd9Sstevel@tonic-gate #ifdef PLUGIN
497c478bd9Sstevel@tonic-gate #include <dlfcn.h>
507c478bd9Sstevel@tonic-gate #endif /* PLUGIN */
517c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
527c478bd9Sstevel@tonic-gate #include <pcap.h>
537c478bd9Sstevel@tonic-gate #include <pcap-int.h>	/* XXX: To get struct pcap */
547c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "pppd.h"
577c478bd9Sstevel@tonic-gate #include "pathnames.h"
587c478bd9Sstevel@tonic-gate #include "patchlevel.h"
597c478bd9Sstevel@tonic-gate #include "fsm.h"
607c478bd9Sstevel@tonic-gate #include "lcp.h"
617c478bd9Sstevel@tonic-gate #include "ipcp.h"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #if defined(ultrix) || defined(NeXT)
647c478bd9Sstevel@tonic-gate char *strdup __P((char *));
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Option variables and default values.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
717c478bd9Sstevel@tonic-gate int	dflag = 0;		/* Tell libpcap we want debugging */
727c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
737c478bd9Sstevel@tonic-gate int	debug = 0;		/* Debug flag */
747c478bd9Sstevel@tonic-gate int	kdebugflag = 0;		/* Tell kernel to print debug messages */
757c478bd9Sstevel@tonic-gate int	default_device = 1;	/* Using /dev/tty or equivalent */
767c478bd9Sstevel@tonic-gate char	devnam[MAXPATHLEN];	/* Device name */
777c478bd9Sstevel@tonic-gate int	crtscts = 0;		/* Use hardware flow control */
787c478bd9Sstevel@tonic-gate bool	modem = 1;		/* Use modem control lines */
797c478bd9Sstevel@tonic-gate int	inspeed = 0;		/* Input/Output speed requested */
807c478bd9Sstevel@tonic-gate u_int32_t netmask = 0;		/* IP netmask to set on interface */
817c478bd9Sstevel@tonic-gate bool	lockflag = 0;		/* Create lock file to lock the serial dev */
827c478bd9Sstevel@tonic-gate bool	nodetach = 0;		/* Don't detach from controlling tty */
837c478bd9Sstevel@tonic-gate bool	updetach = 0;		/* Detach once link is up */
847c478bd9Sstevel@tonic-gate char	*initializer = NULL;	/* Script to initialize physical link */
857c478bd9Sstevel@tonic-gate char	*connect_script = NULL;	/* Script to establish physical link */
867c478bd9Sstevel@tonic-gate char	*disconnect_script = NULL; /* Script to disestablish physical link */
877c478bd9Sstevel@tonic-gate char	*welcomer = NULL;	/* Script to run after phys link estab. */
887c478bd9Sstevel@tonic-gate char	*ptycommand = NULL;	/* Command to run on other side of pty */
897c478bd9Sstevel@tonic-gate int	maxconnect = 0;		/* Maximum connect time */
907c478bd9Sstevel@tonic-gate char	user[MAXNAMELEN];	/* Username for PAP */
917c478bd9Sstevel@tonic-gate char	passwd[MAXSECRETLEN];	/* Password for PAP */
927c478bd9Sstevel@tonic-gate bool	persist = 0;		/* Reopen link after it goes down */
937c478bd9Sstevel@tonic-gate char	our_name[MAXNAMELEN];	/* Our name for authentication purposes */
947c478bd9Sstevel@tonic-gate bool	demand = 0;		/* do dial-on-demand */
957c478bd9Sstevel@tonic-gate char	*ipparam = NULL;	/* Extra parameter for ip up/down scripts */
967c478bd9Sstevel@tonic-gate int	idle_time_limit = 0;	/* Disconnect if idle for this many seconds */
977c478bd9Sstevel@tonic-gate int	holdoff = 30;		/* # seconds to pause before reconnecting */
987c478bd9Sstevel@tonic-gate bool	holdoff_specified;	/* true if a holdoff value has been given */
997c478bd9Sstevel@tonic-gate bool	notty = 0;		/* Stdin/out is not a tty */
1007c478bd9Sstevel@tonic-gate char	*pty_socket = NULL;	/* Socket to connect to pty */
1017c478bd9Sstevel@tonic-gate char	*record_file = NULL;	/* File to record chars sent/received */
1027c478bd9Sstevel@tonic-gate int	using_pty = 0;
1037c478bd9Sstevel@tonic-gate bool	sync_serial = 0;	/* Device is synchronous serial device */
1047c478bd9Sstevel@tonic-gate int	log_to_fd = 1;		/* send log messages to this fd too */
1057c478bd9Sstevel@tonic-gate int	maxfail = 10;		/* max # of unsuccessful connection attempts */
1067c478bd9Sstevel@tonic-gate char	linkname[MAXPATHLEN];	/* logical name for link */
1077c478bd9Sstevel@tonic-gate bool	tune_kernel;		/* may alter kernel settings */
1087c478bd9Sstevel@tonic-gate int	connect_delay = 1000;	/* wait this many ms after connect script */
1097c478bd9Sstevel@tonic-gate int	max_data_rate;		/* max bytes/sec through charshunt */
1107c478bd9Sstevel@tonic-gate int	req_unit = -1;		/* requested interface unit */
1117c478bd9Sstevel@tonic-gate bool	multilink = 0;		/* Enable multilink operation */
1127c478bd9Sstevel@tonic-gate char	*bundle_name = NULL;	/* bundle name for multilink */
1137c478bd9Sstevel@tonic-gate bool	direct_tty = 0;		/* use standard input directly; not a tty */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Maximum depth of include files; prevents looping. */
1167c478bd9Sstevel@tonic-gate #define	MAXFILENESTING	10
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate struct option_info initializer_info;
1197c478bd9Sstevel@tonic-gate struct option_info connect_script_info;
1207c478bd9Sstevel@tonic-gate struct option_info disconnect_script_info;
1217c478bd9Sstevel@tonic-gate struct option_info welcomer_info;
1227c478bd9Sstevel@tonic-gate struct option_info devnam_info;
1237c478bd9Sstevel@tonic-gate struct option_info ptycommand_info;
1247c478bd9Sstevel@tonic-gate struct option_info ipsrc_info;
1257c478bd9Sstevel@tonic-gate struct option_info ipdst_info;
1267c478bd9Sstevel@tonic-gate struct option_info speed_info;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
1297c478bd9Sstevel@tonic-gate struct	bpf_program pass_filter;/* Filter program for packets to pass */
1307c478bd9Sstevel@tonic-gate struct	bpf_program active_filter; /* Filter program for link-active pkts */
1317c478bd9Sstevel@tonic-gate pcap_t  pc;			/* Fake struct pcap so we can compile expr */
1327c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate char *current_option;		/* the name of the option being parsed */
1357c478bd9Sstevel@tonic-gate bool privileged_option;		/* set iff the current option came from root */
1367c478bd9Sstevel@tonic-gate char *option_source = NULL;	/* string saying where the option came from */
1377c478bd9Sstevel@tonic-gate int option_line = 0;		/* line number in file */
1387c478bd9Sstevel@tonic-gate bool log_to_file;		/* log_to_fd is a file opened by us */
1397c478bd9Sstevel@tonic-gate bool log_to_specific_fd;	/* log_to_fd was specified by user option */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Prototypes.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static int setdevname __P((char *));
1457c478bd9Sstevel@tonic-gate static int setipaddr __P((char *));
1467c478bd9Sstevel@tonic-gate static int setspeed __P((char *));
1477c478bd9Sstevel@tonic-gate static int noopt __P((char **, option_t *));
1487c478bd9Sstevel@tonic-gate static int setdomain __P((char **, option_t *));
1497c478bd9Sstevel@tonic-gate static int setnetmask __P((char **, option_t *));
1507c478bd9Sstevel@tonic-gate static int setxonxoff __P((char **, option_t *));
1517c478bd9Sstevel@tonic-gate static int readfile __P((char **, option_t *));
1527c478bd9Sstevel@tonic-gate static int callfile __P((char **, option_t *));
1537c478bd9Sstevel@tonic-gate static int showversion __P((char **, option_t *));
1547c478bd9Sstevel@tonic-gate static int showhelp __P((char **, option_t *));
1557c478bd9Sstevel@tonic-gate static int showalloptions __P((char **, option_t *));
1567c478bd9Sstevel@tonic-gate static void usage __P((void));
1577c478bd9Sstevel@tonic-gate static int setlogfile __P((char **, option_t *));
1587c478bd9Sstevel@tonic-gate #ifdef PLUGIN
1597c478bd9Sstevel@tonic-gate static int loadplugin __P((char **, option_t *));
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
1627c478bd9Sstevel@tonic-gate static int setpassfilter __P((char **, option_t *));
1637c478bd9Sstevel@tonic-gate static int setactivefilter __P((char **, option_t *));
1647c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
1657c478bd9Sstevel@tonic-gate static option_t *find_option __P((char *name));
1667c478bd9Sstevel@tonic-gate static int process_option __P((option_t *opt, char **argv, int sline));
1677c478bd9Sstevel@tonic-gate static int n_arguments __P((option_t *opt));
1687c478bd9Sstevel@tonic-gate static int number_option __P((char *str, u_int32_t *valp, int base));
1697c478bd9Sstevel@tonic-gate static u_int32_t opt_hash __P((const void *key));
1707c478bd9Sstevel@tonic-gate static int opt_compare __P((const void *p1, const void *p2));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate typedef struct _opt_t {
1737c478bd9Sstevel@tonic-gate     option_t	*p;
1747c478bd9Sstevel@tonic-gate } opt_t;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate typedef struct _hashentry_t {
1777c478bd9Sstevel@tonic-gate     struct _hashentry_t	*next;
1787c478bd9Sstevel@tonic-gate     opt_t		opt;
1797c478bd9Sstevel@tonic-gate } hashentry_t;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * A prime number describing the size of hash table.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate #define	OPTHASH_TBLSIZE	101
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Chained hash table containing pointers to available options.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static hashentry_t *hash_tbl[OPTHASH_TBLSIZE] = { NULL };
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Total number of entries in the hash table.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate int hash_tblcnt = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Valid arguments.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate option_t general_options[] = {
2007c478bd9Sstevel@tonic-gate     { "debug", o_int, &debug,
2017c478bd9Sstevel@tonic-gate       "Increase debugging level", OPT_INC|OPT_NOARG|1 },
2027c478bd9Sstevel@tonic-gate     { "-d", o_int, &debug,
2037c478bd9Sstevel@tonic-gate       "Increase debugging level", OPT_INC|OPT_NOARG|1 },
2047c478bd9Sstevel@tonic-gate     { "kdebug", o_int, &kdebugflag,
2057c478bd9Sstevel@tonic-gate       "Set kernel driver debug level" },
2067c478bd9Sstevel@tonic-gate     { "nodetach", o_bool, &nodetach,
2077c478bd9Sstevel@tonic-gate       "Don't detach from controlling tty", 1 },
2087c478bd9Sstevel@tonic-gate     { "-detach", o_bool, &nodetach,
2097c478bd9Sstevel@tonic-gate       "Don't detach from controlling tty", 1 },
2107c478bd9Sstevel@tonic-gate     { "updetach", o_bool, &updetach,
2117c478bd9Sstevel@tonic-gate       "Detach from controlling tty once link is up", 1 },
2127c478bd9Sstevel@tonic-gate     { "holdoff", o_int, &holdoff,
2137c478bd9Sstevel@tonic-gate       "Set time in seconds before retrying connection" },
2147c478bd9Sstevel@tonic-gate     { "idle", o_int, &idle_time_limit,
2157c478bd9Sstevel@tonic-gate       "Set time in seconds before disconnecting idle link" },
2167c478bd9Sstevel@tonic-gate     { "lock", o_bool, &lockflag,
2177c478bd9Sstevel@tonic-gate       "Lock serial device with UUCP-style lock file", 1 },
2187c478bd9Sstevel@tonic-gate     { "-all", o_special_noarg, (void *)noopt,
2197c478bd9Sstevel@tonic-gate       "Don't request/allow any LCP or IPCP options (useless)" },
2207c478bd9Sstevel@tonic-gate     { "init", o_string, &initializer,
2217c478bd9Sstevel@tonic-gate       "A program to initialize the device",
2227c478bd9Sstevel@tonic-gate       OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
2237c478bd9Sstevel@tonic-gate     { "connect", o_string, &connect_script,
2247c478bd9Sstevel@tonic-gate       "A program to set up a connection",
2257c478bd9Sstevel@tonic-gate       OPT_A2INFO | OPT_PRIVFIX, &connect_script_info },
2267c478bd9Sstevel@tonic-gate     { "disconnect", o_string, &disconnect_script,
2277c478bd9Sstevel@tonic-gate       "Program to disconnect serial device",
2287c478bd9Sstevel@tonic-gate       OPT_A2INFO | OPT_PRIVFIX, &disconnect_script_info },
2297c478bd9Sstevel@tonic-gate     { "welcome", o_string, &welcomer,
2307c478bd9Sstevel@tonic-gate       "Script to welcome client",
2317c478bd9Sstevel@tonic-gate       OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
2327c478bd9Sstevel@tonic-gate     { "pty", o_string, &ptycommand,
2337c478bd9Sstevel@tonic-gate       "Script to run on pseudo-tty master side",
2347c478bd9Sstevel@tonic-gate       OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
2357c478bd9Sstevel@tonic-gate     { "notty", o_bool, &notty,
2367c478bd9Sstevel@tonic-gate       "Input/output is not a tty", OPT_DEVNAM | 1 },
2377c478bd9Sstevel@tonic-gate     { "directtty", o_bool, &direct_tty,
2387c478bd9Sstevel@tonic-gate       "Use standard input as tty without checking", OPT_DEVNAM | 1 },
2397c478bd9Sstevel@tonic-gate     { "socket", o_string, &pty_socket,
2407c478bd9Sstevel@tonic-gate       "Send and receive over socket, arg is host:port", OPT_DEVNAM },
2417c478bd9Sstevel@tonic-gate     { "record", o_string, &record_file,
2427c478bd9Sstevel@tonic-gate       "Record characters sent/received to file" },
2437c478bd9Sstevel@tonic-gate     { "maxconnect", o_int, &maxconnect,
2447c478bd9Sstevel@tonic-gate       "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
2457c478bd9Sstevel@tonic-gate     { "crtscts", o_int, &crtscts,
2467c478bd9Sstevel@tonic-gate       "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
2477c478bd9Sstevel@tonic-gate     { "nocrtscts", o_int, &crtscts,
2487c478bd9Sstevel@tonic-gate       "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2497c478bd9Sstevel@tonic-gate     { "-crtscts", o_int, &crtscts,
2507c478bd9Sstevel@tonic-gate       "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2517c478bd9Sstevel@tonic-gate     { "cdtrcts", o_int, &crtscts,
2527c478bd9Sstevel@tonic-gate       "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
2537c478bd9Sstevel@tonic-gate     { "nocdtrcts", o_int, &crtscts,
2547c478bd9Sstevel@tonic-gate       "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2557c478bd9Sstevel@tonic-gate     { "xonxoff", o_special_noarg, (void *)setxonxoff,
2567c478bd9Sstevel@tonic-gate       "Set software (XON/XOFF) flow control" },
2577c478bd9Sstevel@tonic-gate     { "domain", o_special, (void *)setdomain,
2587c478bd9Sstevel@tonic-gate       "Add given domain name to hostname" },
2597c478bd9Sstevel@tonic-gate     { "netmask", o_special, (void *)setnetmask,
2607c478bd9Sstevel@tonic-gate       "set netmask" },
2617c478bd9Sstevel@tonic-gate     { "modem", o_bool, &modem,
2627c478bd9Sstevel@tonic-gate       "Use modem control lines", 1 },
2637c478bd9Sstevel@tonic-gate     { "local", o_bool, &modem,
2647c478bd9Sstevel@tonic-gate       "Don't use modem control lines" },
2657c478bd9Sstevel@tonic-gate     { "file", o_special, (void *)readfile,
2667c478bd9Sstevel@tonic-gate       "Take options from a file", OPT_PREPASS },
2677c478bd9Sstevel@tonic-gate     { "call", o_special, (void *)callfile,
2687c478bd9Sstevel@tonic-gate       "Take options from a privileged file", OPT_PREPASS },
2697c478bd9Sstevel@tonic-gate     { "persist", o_bool, &persist,
2707c478bd9Sstevel@tonic-gate       "Keep on reopening connection after close", 1 },
2717c478bd9Sstevel@tonic-gate     { "nopersist", o_bool, &persist,
2727c478bd9Sstevel@tonic-gate       "Turn off persist option" },
2737c478bd9Sstevel@tonic-gate     { "demand", o_bool, &demand,
2747c478bd9Sstevel@tonic-gate       "Dial on demand", OPT_INITONLY | 1, &persist },
2757c478bd9Sstevel@tonic-gate     { "--version", o_special_noarg, (void *)showversion,
2767c478bd9Sstevel@tonic-gate       "Show version number" },
2777c478bd9Sstevel@tonic-gate     { "--help", o_special_noarg, (void *)showhelp,
2787c478bd9Sstevel@tonic-gate       "Show brief listing of options" },
2797c478bd9Sstevel@tonic-gate     { "-h", o_special_noarg, (void *)showhelp,
2807c478bd9Sstevel@tonic-gate       "Show brief listing of options" },
2817c478bd9Sstevel@tonic-gate     { "options", o_special_noarg, (void *)showalloptions,
2827c478bd9Sstevel@tonic-gate       "Show full listing of options" },
2837c478bd9Sstevel@tonic-gate     { "sync", o_bool, &sync_serial,
2847c478bd9Sstevel@tonic-gate       "Use synchronous HDLC serial encoding", 1 },
2857c478bd9Sstevel@tonic-gate     { "logfd", o_int, &log_to_fd,
2867c478bd9Sstevel@tonic-gate       "Send log messages to this file descriptor",
2877c478bd9Sstevel@tonic-gate       0, &log_to_specific_fd },
2887c478bd9Sstevel@tonic-gate     { "logfile", o_special, (void *)setlogfile,
2897c478bd9Sstevel@tonic-gate       "Append log messages to this file" },
2907c478bd9Sstevel@tonic-gate     { "nolog", o_int, &log_to_fd,
2917c478bd9Sstevel@tonic-gate       "Don't send log messages to any file",
2927c478bd9Sstevel@tonic-gate       OPT_NOARG | OPT_VAL(-1) },
2937c478bd9Sstevel@tonic-gate     { "nologfd", o_int, &log_to_fd,
2947c478bd9Sstevel@tonic-gate       "Don't send log messages to any file descriptor",
2957c478bd9Sstevel@tonic-gate       OPT_NOARG | OPT_VAL(-1) },
2967c478bd9Sstevel@tonic-gate     { "linkname", o_string, linkname,
2977c478bd9Sstevel@tonic-gate       "Set logical name for link",
2987c478bd9Sstevel@tonic-gate       OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
2997c478bd9Sstevel@tonic-gate     { "maxfail", o_int, &maxfail,
3007c478bd9Sstevel@tonic-gate       "Number of unsuccessful connection attempts to allow" },
3017c478bd9Sstevel@tonic-gate     { "ktune", o_bool, &tune_kernel,
3027c478bd9Sstevel@tonic-gate       "Alter kernel settings as necessary", 1 },
3037c478bd9Sstevel@tonic-gate     { "noktune", o_bool, &tune_kernel,
3047c478bd9Sstevel@tonic-gate       "Don't alter kernel settings", 0 },
3057c478bd9Sstevel@tonic-gate     { "connect-delay", o_int, &connect_delay,
3067c478bd9Sstevel@tonic-gate       "Maximum wait time (msec) after connect script finishes" },
3077c478bd9Sstevel@tonic-gate     { "datarate", o_int, &max_data_rate,
3087c478bd9Sstevel@tonic-gate       "Max data rate in bytes/sec for pty, notty, or record" },
3097c478bd9Sstevel@tonic-gate     { "unit", o_int, &req_unit,
3107c478bd9Sstevel@tonic-gate       "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
3117c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
3127c478bd9Sstevel@tonic-gate     { "multilink", o_bool, &multilink,
3137c478bd9Sstevel@tonic-gate       "Enable multilink operation", 1 },
3147c478bd9Sstevel@tonic-gate     { "nomultilink", o_bool, &multilink,
3157c478bd9Sstevel@tonic-gate       "Disable multilink operation", 0 },
3167c478bd9Sstevel@tonic-gate     { "mp", o_bool, &multilink,
3177c478bd9Sstevel@tonic-gate       "Enable multilink operation", 1 },
3187c478bd9Sstevel@tonic-gate     { "nomp", o_bool, &multilink,
3197c478bd9Sstevel@tonic-gate       "Disable multilink operation", 0 },
3207c478bd9Sstevel@tonic-gate     { "bundle", o_string, &bundle_name,
3217c478bd9Sstevel@tonic-gate       "Bundle name for multilink" },
3227c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
3237c478bd9Sstevel@tonic-gate #ifdef PLUGIN
3247c478bd9Sstevel@tonic-gate     { "plugin", o_special, (void *)loadplugin,
3257c478bd9Sstevel@tonic-gate       "Load a plug-in module into pppd", OPT_PRIV },
3267c478bd9Sstevel@tonic-gate #endif /* PLUGIN */
3277c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
3287c478bd9Sstevel@tonic-gate     { "pdebug", o_int, &dflag,
3297c478bd9Sstevel@tonic-gate       "libpcap debugging" },
3307c478bd9Sstevel@tonic-gate     { "pass-filter", o_special, setpassfilter,
3317c478bd9Sstevel@tonic-gate       "set filter for packets to pass" },
3327c478bd9Sstevel@tonic-gate     { "active-filter", o_special, setactivefilter,
3337c478bd9Sstevel@tonic-gate       "set filter for active pkts" },
3347c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
3357c478bd9Sstevel@tonic-gate     { NULL }
3367c478bd9Sstevel@tonic-gate };
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate  * This string gets printed out when "options" is given on the command
3407c478bd9Sstevel@tonic-gate  * line.  Following this string, all of the available options and
3417c478bd9Sstevel@tonic-gate  * their descriptions are printed out as well.  Certain options which
3427c478bd9Sstevel@tonic-gate  * are not available as part of the option_t structure are placed in
3437c478bd9Sstevel@tonic-gate  * the "dummy" option structure.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate static const char pre_allopt_string[] = "\
3467c478bd9Sstevel@tonic-gate pppd version %s.%d%s\n\
3477c478bd9Sstevel@tonic-gate Usage: %s [ options ], where options are:\n\n\
3487c478bd9Sstevel@tonic-gate ";
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /* Do not call add_options() on this structure */
3517c478bd9Sstevel@tonic-gate static option_t dummy_options[] = {
3527c478bd9Sstevel@tonic-gate     { "<device>", o_special_noarg, NULL,
3537c478bd9Sstevel@tonic-gate       "Communicate over the named device" },
3547c478bd9Sstevel@tonic-gate     { "<speed>", o_special_noarg, NULL,
3557c478bd9Sstevel@tonic-gate       "Set the baud rate to <speed>" },
3567c478bd9Sstevel@tonic-gate     { "[<loc>]:[<rem>]", o_special_noarg, NULL,
3577c478bd9Sstevel@tonic-gate       "Set the local and/or remote interface IP addresses" },
3587c478bd9Sstevel@tonic-gate     { NULL }
3597c478bd9Sstevel@tonic-gate };
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static const char post_allopt_string[] = "\
3627c478bd9Sstevel@tonic-gate \n\
3637c478bd9Sstevel@tonic-gate Notes:\
3647c478bd9Sstevel@tonic-gate \t<n>\tinteger type argument\n\
3657c478bd9Sstevel@tonic-gate \t<s>\tstring type argument\n\
3667c478bd9Sstevel@tonic-gate \t<r>\tspecial type argument\n\
3677c478bd9Sstevel@tonic-gate \t(!)\tprivileged option available only when pppd is executed by root\n\
3687c478bd9Sstevel@tonic-gate \t\tor when found in the privileged option files (/etc/ppp/options,\n\
3697c478bd9Sstevel@tonic-gate \t\t/etc/ppp/options.ttyname, /etc/ppp/peers/name, or following\n\
3707c478bd9Sstevel@tonic-gate \t\t\"--\" in /etc/ppp/pap-secrets or /etc/ppp/chap-secrets).\n\
3717c478bd9Sstevel@tonic-gate \t(#)\tdisabled option\n\
3727c478bd9Sstevel@tonic-gate \n\
3737c478bd9Sstevel@tonic-gate Please see the pppd man page for details.\n";
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * parse_args - parse a string of arguments from the command line.  If prepass
3777c478bd9Sstevel@tonic-gate  * is true, we are scanning for the device name and only processing a few
3787c478bd9Sstevel@tonic-gate  * options, so error messages are suppressed.  Returns 1 upon successful
3797c478bd9Sstevel@tonic-gate  * processing of options, and 0 otherwise.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int
parse_args(argc,argv)3827c478bd9Sstevel@tonic-gate parse_args(argc, argv)
3837c478bd9Sstevel@tonic-gate     int argc;
3847c478bd9Sstevel@tonic-gate     char **argv;
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate     char *arg;
3877c478bd9Sstevel@tonic-gate     option_t *opt;
3887c478bd9Sstevel@tonic-gate     int ret;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate     privileged_option = privileged;
3917c478bd9Sstevel@tonic-gate     option_source = "command line";
3927c478bd9Sstevel@tonic-gate     option_line = 0;
3937c478bd9Sstevel@tonic-gate     while (argc > 0) {
3947c478bd9Sstevel@tonic-gate 	arg = *argv++;
3957c478bd9Sstevel@tonic-gate 	--argc;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * First check to see if it's a known option name.  If so, parse the
3997c478bd9Sstevel@tonic-gate 	 * argument(s) and set the option.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	opt = find_option(arg);
4027c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
4037c478bd9Sstevel@tonic-gate 	    int n = n_arguments(opt);
4047c478bd9Sstevel@tonic-gate 	    if (argc < n) {
4057c478bd9Sstevel@tonic-gate 		option_error("too few parameters for option '%s'", arg);
4067c478bd9Sstevel@tonic-gate 		return (0);
4077c478bd9Sstevel@tonic-gate 	    }
4087c478bd9Sstevel@tonic-gate 	    current_option = arg;
4097c478bd9Sstevel@tonic-gate 	    if (!process_option(opt, argv, 0))
4107c478bd9Sstevel@tonic-gate 		return (0);
4117c478bd9Sstevel@tonic-gate 	    argc -= n;
4127c478bd9Sstevel@tonic-gate 	    argv += n;
4137c478bd9Sstevel@tonic-gate 	    continue;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Maybe a tty name, speed or IP address ?
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (((ret = setdevname(arg)) == 0) &&
4207c478bd9Sstevel@tonic-gate 	    ((ret = setspeed(arg)) == 0) &&
4217c478bd9Sstevel@tonic-gate 	    ((ret = setipaddr(arg)) == 0) && !prepass) {
4227c478bd9Sstevel@tonic-gate 	    option_error("unrecognized option '%s'", arg);
4237c478bd9Sstevel@tonic-gate 	    usage();
4247c478bd9Sstevel@tonic-gate 	    return (0);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	if (ret < 0)	/* error */
4277c478bd9Sstevel@tonic-gate 	    return (0);
4287c478bd9Sstevel@tonic-gate     }
4297c478bd9Sstevel@tonic-gate     return (1);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * options_from_file - read a string of options from a file, and
4347c478bd9Sstevel@tonic-gate  * interpret them.  Returns 1 upon successful processing of options,
4357c478bd9Sstevel@tonic-gate  * and 0 otherwise.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate int
options_from_file(char * filename,bool must_exist,bool check_prot,bool priv)4387c478bd9Sstevel@tonic-gate options_from_file
4397c478bd9Sstevel@tonic-gate #ifdef __STDC__
4407c478bd9Sstevel@tonic-gate     (char *filename, bool must_exist, bool check_prot, bool priv)
4417c478bd9Sstevel@tonic-gate #else
4427c478bd9Sstevel@tonic-gate     (filename, must_exist, check_prot, priv)
4437c478bd9Sstevel@tonic-gate     char *filename;
4447c478bd9Sstevel@tonic-gate     bool must_exist;
4457c478bd9Sstevel@tonic-gate     bool check_prot;
4467c478bd9Sstevel@tonic-gate     bool priv;
4477c478bd9Sstevel@tonic-gate #endif
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate     FILE *f;
4507c478bd9Sstevel@tonic-gate     int i, newline, ret, err;
4517c478bd9Sstevel@tonic-gate     option_t *opt;
4527c478bd9Sstevel@tonic-gate     bool oldpriv;
4537c478bd9Sstevel@tonic-gate     int oldline, sline;
4547c478bd9Sstevel@tonic-gate     char *oldsource;
4557c478bd9Sstevel@tonic-gate     char *argv[MAXARGS];
4567c478bd9Sstevel@tonic-gate     char args[MAXARGS][MAXWORDLEN];
4577c478bd9Sstevel@tonic-gate     char cmd[MAXWORDLEN];
4587c478bd9Sstevel@tonic-gate     static bool firsterr = 1;
4597c478bd9Sstevel@tonic-gate     static int nestlevel = 0;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate     if (nestlevel >= MAXFILENESTING) {
4627c478bd9Sstevel@tonic-gate 	option_error("file nesting too deep");
4637c478bd9Sstevel@tonic-gate 	return (0);
4647c478bd9Sstevel@tonic-gate     }
4657c478bd9Sstevel@tonic-gate     if (check_prot)
4667c478bd9Sstevel@tonic-gate 	(void) seteuid(getuid());
4677c478bd9Sstevel@tonic-gate     errno = 0;
4687c478bd9Sstevel@tonic-gate     f = fopen(filename, "r");
4697c478bd9Sstevel@tonic-gate     err = errno;
4707c478bd9Sstevel@tonic-gate     if (check_prot)
4717c478bd9Sstevel@tonic-gate 	(void) seteuid(0);
4727c478bd9Sstevel@tonic-gate     if (f == NULL) {
4737c478bd9Sstevel@tonic-gate 	if (!must_exist && err == ENOENT)
4747c478bd9Sstevel@tonic-gate 	    return (1);
4757c478bd9Sstevel@tonic-gate 	errno = err;
4767c478bd9Sstevel@tonic-gate 	option_error("Can't open options file %s: %m", filename);
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate     }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate     nestlevel++;
4817c478bd9Sstevel@tonic-gate     oldpriv = privileged_option;
4827c478bd9Sstevel@tonic-gate     privileged_option = priv;
4837c478bd9Sstevel@tonic-gate     oldsource = option_source;
4847c478bd9Sstevel@tonic-gate     /*
4857c478bd9Sstevel@tonic-gate      * strdup() is used here because the pointer might refer to the
4867c478bd9Sstevel@tonic-gate      * caller's automatic (stack) storage, and the option_info array
4877c478bd9Sstevel@tonic-gate      * records the source file name.
4887c478bd9Sstevel@tonic-gate      */
4897c478bd9Sstevel@tonic-gate     option_source = strdup(filename);
4907c478bd9Sstevel@tonic-gate     oldline = option_line;
4917c478bd9Sstevel@tonic-gate     option_line = 1;
4927c478bd9Sstevel@tonic-gate     if (option_source == NULL)
4937c478bd9Sstevel@tonic-gate 	option_source = "file";
4947c478bd9Sstevel@tonic-gate     ret = 0;
4957c478bd9Sstevel@tonic-gate     while (getword(f, cmd, &newline, filename)) {
4967c478bd9Sstevel@tonic-gate 	sline = option_line;
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * First see if it's a command.
4997c478bd9Sstevel@tonic-gate 	 */
5007c478bd9Sstevel@tonic-gate 	opt = find_option(cmd);
5017c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
5027c478bd9Sstevel@tonic-gate 	    int n = n_arguments(opt);
5037c478bd9Sstevel@tonic-gate 	    for (i = 0; i < n; ++i) {
5047c478bd9Sstevel@tonic-gate 		if (!getword(f, args[i], &newline, filename)) {
5057c478bd9Sstevel@tonic-gate 		    option_error("too few parameters for option '%s'", cmd);
5067c478bd9Sstevel@tonic-gate 		    goto err;
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		argv[i] = args[i];
5097c478bd9Sstevel@tonic-gate 	    }
5107c478bd9Sstevel@tonic-gate 	    current_option = cmd;
5117c478bd9Sstevel@tonic-gate 	    if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
5127c478bd9Sstevel@tonic-gate 		option_error("the '%s' option may not be used here", cmd);
5137c478bd9Sstevel@tonic-gate 		goto err;
5147c478bd9Sstevel@tonic-gate 	    }
5157c478bd9Sstevel@tonic-gate 	    if (!process_option(opt, argv, sline))
5167c478bd9Sstevel@tonic-gate 		goto err;
5177c478bd9Sstevel@tonic-gate 	    continue;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * Maybe a tty name, speed or IP address ?
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (((i = setdevname(cmd)) == 0) &&
5247c478bd9Sstevel@tonic-gate 	    ((i = setspeed(cmd)) == 0) &&
5257c478bd9Sstevel@tonic-gate 	    ((i = setipaddr(cmd)) == 0)) {
5267c478bd9Sstevel@tonic-gate 	    option_error("unrecognized option '%s'", cmd);
5277c478bd9Sstevel@tonic-gate 	    goto err;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	if (i < 0)		/* error */
5307c478bd9Sstevel@tonic-gate 	    goto err;
5317c478bd9Sstevel@tonic-gate     }
5327c478bd9Sstevel@tonic-gate     ret = 1;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate err:
5357c478bd9Sstevel@tonic-gate     (void) fclose(f);
5367c478bd9Sstevel@tonic-gate     /* We assume here that we abort all processing on the first error. */
5377c478bd9Sstevel@tonic-gate     if (firsterr)
5387c478bd9Sstevel@tonic-gate 	firsterr = 0;
5397c478bd9Sstevel@tonic-gate     else if (!prepass && !ret)
5407c478bd9Sstevel@tonic-gate 	option_error("error in included file");
5417c478bd9Sstevel@tonic-gate     /*
5427c478bd9Sstevel@tonic-gate      * Cannot free option_source because it might be referenced in one
5437c478bd9Sstevel@tonic-gate      * or more option_info structures now.
5447c478bd9Sstevel@tonic-gate      */
5457c478bd9Sstevel@tonic-gate     privileged_option = oldpriv;
5467c478bd9Sstevel@tonic-gate     option_source = oldsource;
5477c478bd9Sstevel@tonic-gate     option_line = oldline;
5487c478bd9Sstevel@tonic-gate     nestlevel--;
5497c478bd9Sstevel@tonic-gate     return (ret);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate  * options_from_user - see if the user has a ~/.ppprc file, and if so,
5547c478bd9Sstevel@tonic-gate  * interpret options from it.  Returns 1 upon successful processing of
5557c478bd9Sstevel@tonic-gate  * options, and 0 otherwise.
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate int
options_from_user()5587c478bd9Sstevel@tonic-gate options_from_user()
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate     char *user, *path, *file;
5617c478bd9Sstevel@tonic-gate     int ret;
5627c478bd9Sstevel@tonic-gate     struct passwd *pw;
5637c478bd9Sstevel@tonic-gate     size_t pl;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate     pw = getpwuid(getuid());
5667c478bd9Sstevel@tonic-gate     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == '\0')
5677c478bd9Sstevel@tonic-gate 	return (1);
5687c478bd9Sstevel@tonic-gate     file = _PATH_USEROPT;
5697c478bd9Sstevel@tonic-gate     pl = strlen(user) + strlen(file) + 2;
5707c478bd9Sstevel@tonic-gate     path = malloc(pl);
5717c478bd9Sstevel@tonic-gate     if (path == NULL)
5727c478bd9Sstevel@tonic-gate 	novm("init file name");
5737c478bd9Sstevel@tonic-gate     (void) slprintf(path, pl, "%s/%s", user, file);
5747c478bd9Sstevel@tonic-gate     ret = options_from_file(path, 0, 1, privileged);
5757c478bd9Sstevel@tonic-gate     free(path);
5767c478bd9Sstevel@tonic-gate     return (ret);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * options_for_tty - see if an options file exists for the serial device, and
5817c478bd9Sstevel@tonic-gate  * if so, interpret options from it.  Returns 1 upon successful processing of
5827c478bd9Sstevel@tonic-gate  * options, and 0 otherwise.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate int
options_for_tty()5857c478bd9Sstevel@tonic-gate options_for_tty()
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate     char *dev, *path, *p;
5887c478bd9Sstevel@tonic-gate     int ret;
5897c478bd9Sstevel@tonic-gate     size_t pl;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate     dev = devnam;
5927c478bd9Sstevel@tonic-gate     if (strncmp(dev, "/dev/", 5) == 0)
5937c478bd9Sstevel@tonic-gate 	dev += 5;
5947c478bd9Sstevel@tonic-gate     if (dev[0] == '\0' || strcmp(dev, "tty") == 0)
5957c478bd9Sstevel@tonic-gate 	return (1);		/* don't look for /etc/ppp/options.tty */
5967c478bd9Sstevel@tonic-gate     pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
5977c478bd9Sstevel@tonic-gate     path = malloc(pl);
5987c478bd9Sstevel@tonic-gate     if (path == NULL)
5997c478bd9Sstevel@tonic-gate 	novm("tty init file name");
6007c478bd9Sstevel@tonic-gate     (void) slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
6017c478bd9Sstevel@tonic-gate     /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
6027c478bd9Sstevel@tonic-gate     for (p = path + strlen(_PATH_TTYOPT); *p != '\0'; ++p)
6037c478bd9Sstevel@tonic-gate 	if (*p == '/')
6047c478bd9Sstevel@tonic-gate 	    *p = '.';
6057c478bd9Sstevel@tonic-gate     ret = options_from_file(path, 0, 0, 1);
6067c478bd9Sstevel@tonic-gate     free(path);
6077c478bd9Sstevel@tonic-gate     return (ret);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * options_from_list - process a string of options in a wordlist.  Returns 1
6127c478bd9Sstevel@tonic-gate  * upon successful processing of options, and 0 otherwise.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate int
options_from_list(struct wordlist * w,bool priv)6157c478bd9Sstevel@tonic-gate options_from_list
6167c478bd9Sstevel@tonic-gate #ifdef __STDC__
6177c478bd9Sstevel@tonic-gate     (struct wordlist *w, bool priv)
6187c478bd9Sstevel@tonic-gate #else
6197c478bd9Sstevel@tonic-gate     (w, priv)
6207c478bd9Sstevel@tonic-gate     struct wordlist *w;
6217c478bd9Sstevel@tonic-gate     bool priv;
6227c478bd9Sstevel@tonic-gate #endif
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate     char *argv[MAXARGS];
6257c478bd9Sstevel@tonic-gate     option_t *opt;
6267c478bd9Sstevel@tonic-gate     int i, ret = 0;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate     privileged_option = priv;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate     /* Caller is expected to set option_source and option_line. */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate     while (w != NULL) {
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * First see if it's a command.
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	opt = find_option(w->word);
6377c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
6387c478bd9Sstevel@tonic-gate 	    int n = n_arguments(opt);
6397c478bd9Sstevel@tonic-gate 	    struct wordlist *w0 = w;
6407c478bd9Sstevel@tonic-gate 	    for (i = 0; i < n; ++i) {
6417c478bd9Sstevel@tonic-gate 		w = w->next;
6427c478bd9Sstevel@tonic-gate 		if (w == NULL) {
6437c478bd9Sstevel@tonic-gate 		    option_error("too few parameters for option '%s'",
6447c478bd9Sstevel@tonic-gate 			w0->word);
6457c478bd9Sstevel@tonic-gate 		    goto err;
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		argv[i] = w->word;
6487c478bd9Sstevel@tonic-gate 	    }
6497c478bd9Sstevel@tonic-gate 	    current_option = w0->word;
6507c478bd9Sstevel@tonic-gate 	    if (!process_option(opt, argv, option_line))
6517c478bd9Sstevel@tonic-gate 		goto err;
6527c478bd9Sstevel@tonic-gate 	    continue;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/*
6567c478bd9Sstevel@tonic-gate 	 * Options from the {p,ch}ap-secrets files can't change the device
6577c478bd9Sstevel@tonic-gate 	 * name nor the speed.  Therefore, calls to setdevname() and
6587c478bd9Sstevel@tonic-gate 	 * setspeed() were removed.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if ((i = setipaddr(w->word)) == 0) {
6617c478bd9Sstevel@tonic-gate 	    option_error("unrecognized option '%s'", w->word);
6627c478bd9Sstevel@tonic-gate 	    goto err;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	if (i < 0)		/* error */
6657c478bd9Sstevel@tonic-gate 	    goto err;
6667c478bd9Sstevel@tonic-gate     }
6677c478bd9Sstevel@tonic-gate     ret = 1;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate err:
6707c478bd9Sstevel@tonic-gate     return (ret);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * find_option - scan the option lists for the various protocols looking for an
6757c478bd9Sstevel@tonic-gate  * entry with the given name.  Returns a pointer to the matching option_t
6767c478bd9Sstevel@tonic-gate  * structure upon successful processing of options, and NULL otherwise.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate static option_t *
find_option(name)6797c478bd9Sstevel@tonic-gate find_option(name)
6807c478bd9Sstevel@tonic-gate     char *name;
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate     hashentry_t *bucket;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate     bucket = hash_tbl[opt_hash(name)];
6857c478bd9Sstevel@tonic-gate     for (; bucket != NULL; bucket = bucket->next) {
6867c478bd9Sstevel@tonic-gate 	if (bucket->opt.p->name != NULL) {
6877c478bd9Sstevel@tonic-gate 	    if ((strcmp(bucket->opt.p->name, name) == 0) &&
6887c478bd9Sstevel@tonic-gate 		!(bucket->opt.p->flags & OPT_DISABLE)) {
6897c478bd9Sstevel@tonic-gate 		return (bucket->opt.p);
6907c478bd9Sstevel@tonic-gate 	    }
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate     }
6937c478bd9Sstevel@tonic-gate     return (NULL);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * process_option - process one new-style option (something other than a
6987c478bd9Sstevel@tonic-gate  * port name, bit rate, or IP address).  Returns 1 upon successful
6997c478bd9Sstevel@tonic-gate  * processing of options, and 0 otherwise.
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate static int
process_option(opt,argv,sline)7027c478bd9Sstevel@tonic-gate process_option(opt, argv, sline)
7037c478bd9Sstevel@tonic-gate     option_t *opt;
7047c478bd9Sstevel@tonic-gate     char **argv;
7057c478bd9Sstevel@tonic-gate     int sline;
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate     u_int32_t v;
7087c478bd9Sstevel@tonic-gate     int iv, a;
7097c478bd9Sstevel@tonic-gate     char *sv;
7107c478bd9Sstevel@tonic-gate     int (*parser) __P((char **, option_t *));
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate     if ((opt->flags & OPT_PREPASS) == 0 && prepass)
7137c478bd9Sstevel@tonic-gate 	return (1);
7147c478bd9Sstevel@tonic-gate     if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
7157c478bd9Sstevel@tonic-gate 	option_error("it's too late to use the '%s' option", opt->name);
7167c478bd9Sstevel@tonic-gate 	return (0);
7177c478bd9Sstevel@tonic-gate     }
7187c478bd9Sstevel@tonic-gate     if ((opt->flags & OPT_PRIV) && !privileged_option) {
7197c478bd9Sstevel@tonic-gate 	option_error("using the '%s' option requires root privilege",
7207c478bd9Sstevel@tonic-gate 	    opt->name);
7217c478bd9Sstevel@tonic-gate 	return (0);
7227c478bd9Sstevel@tonic-gate     }
7237c478bd9Sstevel@tonic-gate     if ((opt->flags & OPT_ENABLE) && !privileged_option &&
7247c478bd9Sstevel@tonic-gate 	*(bool *)(opt->addr2) == 0) {
7257c478bd9Sstevel@tonic-gate 	option_error("'%s' option is disabled", opt->name);
7267c478bd9Sstevel@tonic-gate 	return (0);
7277c478bd9Sstevel@tonic-gate     }
7287c478bd9Sstevel@tonic-gate     if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
7297c478bd9Sstevel@tonic-gate 	struct option_info *ip = (struct option_info *) opt->addr2;
7307c478bd9Sstevel@tonic-gate 	if ((ip != NULL) && ip->priv) {
7317c478bd9Sstevel@tonic-gate 	    option_error("'%s' option cannot be overridden", opt->name);
7327c478bd9Sstevel@tonic-gate 	    return (0);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate     }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate     switch (opt->type) {
7377c478bd9Sstevel@tonic-gate     case o_bool:
7387c478bd9Sstevel@tonic-gate 	v = opt->flags & OPT_VALUE;
7397c478bd9Sstevel@tonic-gate 	*(bool *)(opt->addr) = (v != 0);
7407c478bd9Sstevel@tonic-gate 	if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
7417c478bd9Sstevel@tonic-gate 	    *(bool *)(opt->addr2) = (v != 0);
7427c478bd9Sstevel@tonic-gate 	break;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate     case o_int:
7457c478bd9Sstevel@tonic-gate 	iv = 0;
7467c478bd9Sstevel@tonic-gate 	if ((opt->flags & OPT_NOARG) == 0) {
7477c478bd9Sstevel@tonic-gate 	    if (!int_option(*argv, &iv))
7487c478bd9Sstevel@tonic-gate 		return (0);
7497c478bd9Sstevel@tonic-gate 	    if ((((opt->flags & OPT_LLIMIT) && (iv < opt->lower_limit)) ||
7507c478bd9Sstevel@tonic-gate 		((opt->flags & OPT_ULIMIT) && (iv > opt->upper_limit))) &&
7517c478bd9Sstevel@tonic-gate 		!((opt->flags & OPT_ZEROOK) && (iv == 0))) {
7527c478bd9Sstevel@tonic-gate 		char *zok = (opt->flags & OPT_ZEROOK) ? " zero or" : "";
7537c478bd9Sstevel@tonic-gate 		switch (opt->flags & OPT_LIMITS) {
7547c478bd9Sstevel@tonic-gate 		case OPT_LLIMIT:
7557c478bd9Sstevel@tonic-gate 		    option_error("%s value must be%s >= %d",
7567c478bd9Sstevel@tonic-gate 				 opt->name, zok, opt->lower_limit);
7577c478bd9Sstevel@tonic-gate 		    break;
7587c478bd9Sstevel@tonic-gate 		case OPT_ULIMIT:
7597c478bd9Sstevel@tonic-gate 		    option_error("%s value must be%s <= %d",
7607c478bd9Sstevel@tonic-gate 				 opt->name, zok, opt->upper_limit);
7617c478bd9Sstevel@tonic-gate 		    break;
7627c478bd9Sstevel@tonic-gate 		case OPT_LIMITS:
7637c478bd9Sstevel@tonic-gate 		    option_error("%s value must be%s between %d and %d",
7647c478bd9Sstevel@tonic-gate 				opt->name, zok, opt->lower_limit, opt->upper_limit);
7657c478bd9Sstevel@tonic-gate 		    break;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 		return (0);
7687c478bd9Sstevel@tonic-gate 	    }
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	a = opt->flags & OPT_VALUE;
7717c478bd9Sstevel@tonic-gate 	if (a >= 128)
7727c478bd9Sstevel@tonic-gate 	    a -= 256;		/* sign extend */
7737c478bd9Sstevel@tonic-gate 	iv += a;
7747c478bd9Sstevel@tonic-gate 	if (opt->flags & OPT_INC)
7757c478bd9Sstevel@tonic-gate 	    iv += *(int *)(opt->addr);
7767c478bd9Sstevel@tonic-gate 	if ((opt->flags & OPT_NOINCR) && !privileged_option) {
7777c478bd9Sstevel@tonic-gate 	    int oldv = *(int *)(opt->addr);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	    if ((opt->flags & OPT_ZEROINF) && (iv == 0)) {
7807c478bd9Sstevel@tonic-gate 		if (oldv > 0) {
7817c478bd9Sstevel@tonic-gate 		    option_error("%s value cannot be set to infinity; limited to %d",
7827c478bd9Sstevel@tonic-gate 			opt->name, oldv);
7837c478bd9Sstevel@tonic-gate 		    return (0);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 	    } else if (iv > oldv) {
7867c478bd9Sstevel@tonic-gate 		option_error("%s value cannot be increased beyond %d",
7877c478bd9Sstevel@tonic-gate 		    opt->name, oldv);
7887c478bd9Sstevel@tonic-gate 		return (0);
7897c478bd9Sstevel@tonic-gate 	    }
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	*(int *)(opt->addr) = iv;
7927c478bd9Sstevel@tonic-gate 	if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
7937c478bd9Sstevel@tonic-gate 	    *(int *)(opt->addr2) = iv;
7947c478bd9Sstevel@tonic-gate 	break;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate     case o_uint32:
7977c478bd9Sstevel@tonic-gate 	if (opt->flags & OPT_NOARG) {
7987c478bd9Sstevel@tonic-gate 	    v = opt->flags & OPT_VALUE;
7997c478bd9Sstevel@tonic-gate 	} else if (!number_option(*argv, &v, 16))
8007c478bd9Sstevel@tonic-gate 	    return (0);
8017c478bd9Sstevel@tonic-gate 	if (opt->flags & OPT_OR)
8027c478bd9Sstevel@tonic-gate 	    v |= *(u_int32_t *)(opt->addr);
8037c478bd9Sstevel@tonic-gate 	*(u_int32_t *)(opt->addr) = v;
8047c478bd9Sstevel@tonic-gate 	if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
8057c478bd9Sstevel@tonic-gate 	    *(u_int32_t *)(opt->addr2) = v;
8067c478bd9Sstevel@tonic-gate 	break;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate     case o_string:
8097c478bd9Sstevel@tonic-gate 	if (opt->flags & OPT_STATIC) {
8107c478bd9Sstevel@tonic-gate 	    (void) strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
8117c478bd9Sstevel@tonic-gate 	    if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY)) {
8127c478bd9Sstevel@tonic-gate 		(void) strlcpy((char *)(opt->addr2), *argv, opt->upper_limit);
8137c478bd9Sstevel@tonic-gate 	    }
8147c478bd9Sstevel@tonic-gate 	} else {
8157c478bd9Sstevel@tonic-gate 	    sv = strdup(*argv);
8167c478bd9Sstevel@tonic-gate 	    if (sv == NULL)
8177c478bd9Sstevel@tonic-gate 		novm("option argument");
8187c478bd9Sstevel@tonic-gate 	    *(char **)(opt->addr) = sv;
8197c478bd9Sstevel@tonic-gate 	    if (opt->addr2 != NULL && (opt->flags & OPT_A2COPY))
8207c478bd9Sstevel@tonic-gate 		*(char **)(opt->addr2) = sv;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	break;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate     case o_special_noarg:
8257c478bd9Sstevel@tonic-gate     case o_special:
8267c478bd9Sstevel@tonic-gate 	parser = (int (*) __P((char **, option_t *))) opt->addr;
8277c478bd9Sstevel@tonic-gate 	if (!(*parser)(argv, opt))
8287c478bd9Sstevel@tonic-gate 	    return (0);
8297c478bd9Sstevel@tonic-gate 	break;
8307c478bd9Sstevel@tonic-gate     }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate     if (opt->addr2 != NULL) {
8337c478bd9Sstevel@tonic-gate 	if (opt->flags & OPT_A2INFO) {
8347c478bd9Sstevel@tonic-gate 	    struct option_info *ip = (struct option_info *) opt->addr2;
8357c478bd9Sstevel@tonic-gate 	    ip->priv = privileged_option;
8367c478bd9Sstevel@tonic-gate 	    ip->source = option_source;
8377c478bd9Sstevel@tonic-gate 	    ip->line = sline;
8387c478bd9Sstevel@tonic-gate 	} else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
8397c478bd9Sstevel@tonic-gate 	    *(bool *)(opt->addr2) = 1;
8407c478bd9Sstevel@tonic-gate     }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate     return (1);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * n_arguments - tell how many arguments an option takes.  Returns 1 upon
8477c478bd9Sstevel@tonic-gate  * successful processing of options, and 0 otherwise.
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate static int
n_arguments(opt)8507c478bd9Sstevel@tonic-gate n_arguments(opt)
8517c478bd9Sstevel@tonic-gate     option_t *opt;
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate     return ((opt->type == o_bool || opt->type == o_special_noarg ||
8547c478bd9Sstevel@tonic-gate 	    (opt->flags & OPT_NOARG)) ? 0 : 1);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * opt_hash - a hash function that works quite well for strings.  Returns
8597c478bd9Sstevel@tonic-gate  * the hash key of the supplied string.
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate static u_int32_t
opt_hash(key)8627c478bd9Sstevel@tonic-gate opt_hash(key)
8637c478bd9Sstevel@tonic-gate     const void *key;
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate     register const char *ptr;
8667c478bd9Sstevel@tonic-gate     register u_int32_t val;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate     val = 0;
8697c478bd9Sstevel@tonic-gate     ptr = key;
8707c478bd9Sstevel@tonic-gate     while (*ptr != '\0') {
8717c478bd9Sstevel@tonic-gate 	int tmp;
8727c478bd9Sstevel@tonic-gate 	val = (val << 4) + (*ptr);
8737c478bd9Sstevel@tonic-gate 	tmp = val & 0xf0000000;
8747c478bd9Sstevel@tonic-gate 	if (tmp) {
8757c478bd9Sstevel@tonic-gate 	    val ^= (tmp >> 24);
8767c478bd9Sstevel@tonic-gate 	    val ^= tmp;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	ptr++;
8797c478bd9Sstevel@tonic-gate     }
8807c478bd9Sstevel@tonic-gate     return (val % OPTHASH_TBLSIZE);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate /*
8847c478bd9Sstevel@tonic-gate  * add_options - add a list of options to the chained hash table.
8857c478bd9Sstevel@tonic-gate  * Also detect duplicate options, and if found, disable the older
8867c478bd9Sstevel@tonic-gate  * definition and log it as an error.
8877c478bd9Sstevel@tonic-gate  */
8887c478bd9Sstevel@tonic-gate void
add_options(opt)8897c478bd9Sstevel@tonic-gate add_options(opt)
8907c478bd9Sstevel@tonic-gate     option_t *opt;
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate     register option_t *sopt;
8937c478bd9Sstevel@tonic-gate     register hashentry_t *bucket;
8947c478bd9Sstevel@tonic-gate     register u_int32_t loc;
8957c478bd9Sstevel@tonic-gate     hashentry_t *he;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate     /* fill hash-table */
8987c478bd9Sstevel@tonic-gate     for (sopt = opt; sopt->name != NULL; ++sopt, hash_tblcnt++) {
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/* first, allocate a hash entry */
9017c478bd9Sstevel@tonic-gate 	he = (hashentry_t *)malloc(sizeof(*he));
9027c478bd9Sstevel@tonic-gate 	if (he == NULL) {
9037c478bd9Sstevel@tonic-gate 	    novm("option hash table entry");
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 	he->opt.p = sopt;
9067c478bd9Sstevel@tonic-gate 	he->next = NULL;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * fill the chained hash table and take care of any collisions or
9107c478bd9Sstevel@tonic-gate 	 * duplicate items.
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 	loc = opt_hash(sopt->name);
9137c478bd9Sstevel@tonic-gate 	bucket = hash_tbl[loc];
9147c478bd9Sstevel@tonic-gate 	if (bucket != NULL) {
9157c478bd9Sstevel@tonic-gate 	    for (;;) {
9167c478bd9Sstevel@tonic-gate 		if (!(bucket->opt.p->flags & OPT_DISABLE) &&
9177c478bd9Sstevel@tonic-gate 		    strcmp(sopt->name, bucket->opt.p->name) == 0) {
9187c478bd9Sstevel@tonic-gate 		    info("option '%s' redefined; old definition disabled",
9197c478bd9Sstevel@tonic-gate 			sopt->name);
9207c478bd9Sstevel@tonic-gate 		    bucket->opt.p->flags |= OPT_DISABLE;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		if (bucket->next == NULL)
9237c478bd9Sstevel@tonic-gate 		    break;
9247c478bd9Sstevel@tonic-gate 		bucket = bucket->next;
9257c478bd9Sstevel@tonic-gate 	    }
9267c478bd9Sstevel@tonic-gate 	    bucket->next = he;
9277c478bd9Sstevel@tonic-gate 	} else {
9287c478bd9Sstevel@tonic-gate 	    hash_tbl[loc] = he;
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate     }
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * remove_option - disable an option.  Returns the option_t structure
9357c478bd9Sstevel@tonic-gate  * of the disabled option, or NULL if the option name is invalid or if
9367c478bd9Sstevel@tonic-gate  * the option has already been disabled.
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate option_t *
remove_option(name)9397c478bd9Sstevel@tonic-gate remove_option(name)
9407c478bd9Sstevel@tonic-gate     char *name;
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate     option_t *opt;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate     if ((opt = find_option(name)) != NULL) {
9457c478bd9Sstevel@tonic-gate 	opt->flags |= OPT_DISABLE;
9467c478bd9Sstevel@tonic-gate     }
9477c478bd9Sstevel@tonic-gate     return (opt);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate  * opt_compare - a compare function supplied to the quicksort routine.
9527c478bd9Sstevel@tonic-gate  * Returns an integer less than, equal to, or greater than zero to indicate
9537c478bd9Sstevel@tonic-gate  * if the first argument is considered less than, equal to, or greater
9547c478bd9Sstevel@tonic-gate  * than the second argument.
9557c478bd9Sstevel@tonic-gate  */
9567c478bd9Sstevel@tonic-gate static int
opt_compare(p1,p2)9577c478bd9Sstevel@tonic-gate opt_compare(p1, p2)
9587c478bd9Sstevel@tonic-gate     const void *p1;
9597c478bd9Sstevel@tonic-gate     const void *p2;
9607c478bd9Sstevel@tonic-gate {
9617c478bd9Sstevel@tonic-gate     opt_t *o1 = (opt_t *)p1;
9627c478bd9Sstevel@tonic-gate     opt_t *o2 = (opt_t *)p2;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate     return (strcmp(o1->p->name, o2->p->name));
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9687c478bd9Sstevel@tonic-gate static int
showalloptions(argv,topt)9697c478bd9Sstevel@tonic-gate showalloptions(argv, topt)
9707c478bd9Sstevel@tonic-gate     char **argv;
9717c478bd9Sstevel@tonic-gate     option_t *topt;
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate #define	MAXOPTSTRLEN	257
9747c478bd9Sstevel@tonic-gate #define	PRINTOPTIONS()	{					\
9757c478bd9Sstevel@tonic-gate     (void) slprintf(opt_str, sizeof(opt_str), "%s", opt->name);	\
9767c478bd9Sstevel@tonic-gate     if ((opt->type == o_int || opt->type == o_uint32) &&	\
9777c478bd9Sstevel@tonic-gate 	!(opt->flags & OPT_NOARG)) {				\
9787c478bd9Sstevel@tonic-gate 	(void) strlcat(opt_str, " <n>", sizeof(opt_str));	\
9797c478bd9Sstevel@tonic-gate     } else if (opt->type == o_string) {				\
9807c478bd9Sstevel@tonic-gate 	(void) strlcat(opt_str, " <s>", sizeof(opt_str));	\
9817c478bd9Sstevel@tonic-gate     } else if (opt->type == o_special) {			\
9827c478bd9Sstevel@tonic-gate 	(void) strlcat(opt_str, " <r>", sizeof(opt_str));	\
9837c478bd9Sstevel@tonic-gate     }								\
9847c478bd9Sstevel@tonic-gate     if (opt->flags & OPT_PRIV) {				\
9857c478bd9Sstevel@tonic-gate 	(void) strlcat(opt_str, " (!)", sizeof(opt_str));	\
9867c478bd9Sstevel@tonic-gate     } else if (opt->flags & OPT_DISABLE) {			\
9877c478bd9Sstevel@tonic-gate 	(void) strlcat(opt_str, " (#)", sizeof(opt_str));	\
9887c478bd9Sstevel@tonic-gate     }								\
9897c478bd9Sstevel@tonic-gate     (void) printf("%-26s%s\n", opt_str, opt->description);	\
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate     char opt_str[MAXOPTSTRLEN];
9937c478bd9Sstevel@tonic-gate     option_t *opt;
9947c478bd9Sstevel@tonic-gate     hashentry_t *bucket;
9957c478bd9Sstevel@tonic-gate     int i, sofar;
9967c478bd9Sstevel@tonic-gate     opt_t *sopt;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate     if (phase != PHASE_INITIALIZE) {
9997c478bd9Sstevel@tonic-gate 	return (0);
10007c478bd9Sstevel@tonic-gate     }
10017c478bd9Sstevel@tonic-gate     (void) printf(pre_allopt_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
10027c478bd9Sstevel@tonic-gate 	progname);
10037c478bd9Sstevel@tonic-gate     for (opt = dummy_options; opt->name != NULL; ++opt) {
10047c478bd9Sstevel@tonic-gate 	PRINTOPTIONS();
10057c478bd9Sstevel@tonic-gate     }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate     sopt = malloc(sizeof(*sopt) * hash_tblcnt);
10087c478bd9Sstevel@tonic-gate     if (sopt == NULL) {
10097c478bd9Sstevel@tonic-gate 	novm("sorted option table");
10107c478bd9Sstevel@tonic-gate     }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate     sofar = 0;
10137c478bd9Sstevel@tonic-gate     for (i = 0; i < OPTHASH_TBLSIZE; i++) {
10147c478bd9Sstevel@tonic-gate 	for (bucket = hash_tbl[i]; bucket != NULL; bucket = bucket->next) {
10157c478bd9Sstevel@tonic-gate 	    if (sofar >= hash_tblcnt) {
10167c478bd9Sstevel@tonic-gate 		fatal("options hash table corrupted; size mismatch");
10177c478bd9Sstevel@tonic-gate 	    }
10187c478bd9Sstevel@tonic-gate 	    sopt[sofar++].p = bucket->opt.p;
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate     }
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate     qsort((void *)sopt, sofar, sizeof(sopt[0]), opt_compare);
10237c478bd9Sstevel@tonic-gate     for (i = 0; i < sofar; i++) {
10247c478bd9Sstevel@tonic-gate 	opt = sopt[i].p;
10257c478bd9Sstevel@tonic-gate 	PRINTOPTIONS();
10267c478bd9Sstevel@tonic-gate     }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate     (void) printf(post_allopt_string);
10297c478bd9Sstevel@tonic-gate     (void) free(sopt);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate #undef	MAXOPTSTRLEN
10327c478bd9Sstevel@tonic-gate #undef	PRINTOPTIONS
10337c478bd9Sstevel@tonic-gate     return (0);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * usage - print out a message telling how to use the program.
10387c478bd9Sstevel@tonic-gate  * This string gets printed out when either "--help" or an invalid option
10397c478bd9Sstevel@tonic-gate  * is specified.
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate static void
usage()10427c478bd9Sstevel@tonic-gate usage()
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	static const char usage_string[] = "\
10457c478bd9Sstevel@tonic-gate pppd version %s.%d%s\n\
10467c478bd9Sstevel@tonic-gate Usage: %s [ options ], where options are:\n\
10477c478bd9Sstevel@tonic-gate \t<device>\tCommunicate over the named device\n\
10487c478bd9Sstevel@tonic-gate \t<speed>\t\tSet the baud rate to <speed>\n\
10497c478bd9Sstevel@tonic-gate \t<loc>:<rem>\tSet the local and/or remote interface IP\n\
10507c478bd9Sstevel@tonic-gate \t\t\taddresses.  Either one may be omitted.\n\
10517c478bd9Sstevel@tonic-gate \tnoauth\t\tDon't require authentication from peer\n\
10527c478bd9Sstevel@tonic-gate \tconnect <p>\tInvoke shell command <p> to set up the serial line\n\
10537c478bd9Sstevel@tonic-gate \tdefaultroute\tAdd default route through interface\n\
10547c478bd9Sstevel@tonic-gate Use \"%s options\" or \"man pppd\" for more options.\n\
10557c478bd9Sstevel@tonic-gate ";
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate     if (phase == PHASE_INITIALIZE)
10587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, usage_string, VERSION, PATCHLEVEL,
10597c478bd9Sstevel@tonic-gate 	    IMPLEMENTATION, progname, progname);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * showhelp - print out usage message and exit program upon success, or
10647c478bd9Sstevel@tonic-gate  * return 0 otherwise.
10657c478bd9Sstevel@tonic-gate  */
10667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10677c478bd9Sstevel@tonic-gate static int
showhelp(argv,opt)10687c478bd9Sstevel@tonic-gate showhelp(argv, opt)
10697c478bd9Sstevel@tonic-gate     char **argv;
10707c478bd9Sstevel@tonic-gate     option_t *opt;
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate     if (phase == PHASE_INITIALIZE) {
10737c478bd9Sstevel@tonic-gate 	usage();
10747c478bd9Sstevel@tonic-gate 	exit(0);
10757c478bd9Sstevel@tonic-gate     }
10767c478bd9Sstevel@tonic-gate     return (0);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate /*
10807c478bd9Sstevel@tonic-gate  * showversion - print out the version number and exit program  upon success,
10817c478bd9Sstevel@tonic-gate  * or return 0 otherwise.
10827c478bd9Sstevel@tonic-gate  */
10837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10847c478bd9Sstevel@tonic-gate static int
showversion(argv,opt)10857c478bd9Sstevel@tonic-gate showversion(argv, opt)
10867c478bd9Sstevel@tonic-gate     char **argv;
10877c478bd9Sstevel@tonic-gate     option_t *opt;
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate     if (phase == PHASE_INITIALIZE) {
10907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "pppd version %s.%d%s\n", VERSION, PATCHLEVEL,
10917c478bd9Sstevel@tonic-gate 	    IMPLEMENTATION);
10927c478bd9Sstevel@tonic-gate 	exit(0);
10937c478bd9Sstevel@tonic-gate     }
10947c478bd9Sstevel@tonic-gate     return (0);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate /*
10987c478bd9Sstevel@tonic-gate  * option_error - print a message about an error in an option.  The message is
10997c478bd9Sstevel@tonic-gate  * logged, and also sent to stderr if phase == PHASE_INITIALIZE.
11007c478bd9Sstevel@tonic-gate  */
11017c478bd9Sstevel@tonic-gate void
option_error(char * fmt,...)11027c478bd9Sstevel@tonic-gate option_error __V((char *fmt, ...))
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate     va_list args;
11057c478bd9Sstevel@tonic-gate     char buf[256];
11067c478bd9Sstevel@tonic-gate     int i, err;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate #if defined(__STDC__)
11097c478bd9Sstevel@tonic-gate     va_start(args, fmt);
11107c478bd9Sstevel@tonic-gate #else
11117c478bd9Sstevel@tonic-gate     char *fmt;
11127c478bd9Sstevel@tonic-gate     va_start(args);
11137c478bd9Sstevel@tonic-gate     fmt = va_arg(args, char *);
11147c478bd9Sstevel@tonic-gate #endif
11157c478bd9Sstevel@tonic-gate     if (prepass) {
11167c478bd9Sstevel@tonic-gate 	va_end(args);
11177c478bd9Sstevel@tonic-gate 	return;
11187c478bd9Sstevel@tonic-gate     }
11197c478bd9Sstevel@tonic-gate     err = errno;
11207c478bd9Sstevel@tonic-gate     if (option_source == NULL) {
11217c478bd9Sstevel@tonic-gate 	i = 0;
11227c478bd9Sstevel@tonic-gate     } else if (option_line <= 0) {
11237c478bd9Sstevel@tonic-gate 	(void) strlcpy(buf, option_source, sizeof (buf));
11247c478bd9Sstevel@tonic-gate 	i = strlen(buf);
11257c478bd9Sstevel@tonic-gate     } else {
11267c478bd9Sstevel@tonic-gate 	i = slprintf(buf, sizeof(buf), "%s:%d", option_source, option_line);
11277c478bd9Sstevel@tonic-gate     }
11287c478bd9Sstevel@tonic-gate     if (i != 0) {
11297c478bd9Sstevel@tonic-gate 	(void) strlcat(buf, ": ", sizeof (buf));
11307c478bd9Sstevel@tonic-gate 	i += 2;
11317c478bd9Sstevel@tonic-gate     }
11327c478bd9Sstevel@tonic-gate     errno = err;
11337c478bd9Sstevel@tonic-gate     (void) vslprintf(buf + i, sizeof (buf) - i, fmt, args);
11347c478bd9Sstevel@tonic-gate     va_end(args);
11357c478bd9Sstevel@tonic-gate     if ((phase == PHASE_INITIALIZE) && !detached)
11367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", progname, buf);
11377c478bd9Sstevel@tonic-gate     syslog(LOG_ERR, "%s", buf);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate /*
11417c478bd9Sstevel@tonic-gate  * getword - read a word from a file.  Words are delimited by white-space or by
11427c478bd9Sstevel@tonic-gate  * quotes (" or ').  Quotes, white-space and \ may be escaped with \.
11437c478bd9Sstevel@tonic-gate  * \<newline> is ignored.  Returns 1 upon successful processing of options,
11447c478bd9Sstevel@tonic-gate  * and 0 otherwise.
11457c478bd9Sstevel@tonic-gate  */
11467c478bd9Sstevel@tonic-gate int
getword(f,word,newlinep,filename)11477c478bd9Sstevel@tonic-gate getword(f, word, newlinep, filename)
11487c478bd9Sstevel@tonic-gate     FILE *f;
11497c478bd9Sstevel@tonic-gate     char *word;
11507c478bd9Sstevel@tonic-gate     int *newlinep;
11517c478bd9Sstevel@tonic-gate     char *filename;
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate     int c, len, escape;
11547c478bd9Sstevel@tonic-gate     int quoted, comment;
11557c478bd9Sstevel@tonic-gate     int value, digit, got, n;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate #define isoctal(c) ((c) >= '0' && (c) < '8')
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate     *newlinep = 0;
11607c478bd9Sstevel@tonic-gate     len = 0;
11617c478bd9Sstevel@tonic-gate     escape = 0;
11627c478bd9Sstevel@tonic-gate     comment = 0;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate     /*
11657c478bd9Sstevel@tonic-gate      * First skip white-space and comments.
11667c478bd9Sstevel@tonic-gate      */
11677c478bd9Sstevel@tonic-gate     for (;;) {
11687c478bd9Sstevel@tonic-gate 	c = getc(f);
11697c478bd9Sstevel@tonic-gate 	if (c == EOF)
11707c478bd9Sstevel@tonic-gate 	    break;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/*
11737c478bd9Sstevel@tonic-gate 	 * A newline means the end of a comment; backslash-newline
11747c478bd9Sstevel@tonic-gate 	 * is ignored.  Note that we cannot have escape && comment.
11757c478bd9Sstevel@tonic-gate 	 */
11767c478bd9Sstevel@tonic-gate 	if (c == '\n') {
11777c478bd9Sstevel@tonic-gate 	    option_line++;
11787c478bd9Sstevel@tonic-gate 	    if (!escape) {
1179