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 #define RCSID "$Id: options.c,v 1.74 2000/04/15 01:27:13 masputra Exp $"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <netdb.h>
457c478bd9Sstevel@tonic-gate #include <pwd.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <netinet/in.h>
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate #ifdef PLUGIN
517c478bd9Sstevel@tonic-gate #include <dlfcn.h>
527c478bd9Sstevel@tonic-gate #endif /* PLUGIN */
537c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
547c478bd9Sstevel@tonic-gate #include <pcap.h>
557c478bd9Sstevel@tonic-gate #include <pcap-int.h> /* XXX: To get struct pcap */
567c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #include "pppd.h"
597c478bd9Sstevel@tonic-gate #include "pathnames.h"
607c478bd9Sstevel@tonic-gate #include "patchlevel.h"
617c478bd9Sstevel@tonic-gate #include "fsm.h"
627c478bd9Sstevel@tonic-gate #include "lcp.h"
637c478bd9Sstevel@tonic-gate #include "ipcp.h"
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate #if defined(ultrix) || defined(NeXT)
667c478bd9Sstevel@tonic-gate char *strdup __P((char *));
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(_lint)
707c478bd9Sstevel@tonic-gate static const char rcsid[] = RCSID;
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * Option variables and default values.
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
777c478bd9Sstevel@tonic-gate int dflag = 0; /* Tell libpcap we want debugging */
787c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
797c478bd9Sstevel@tonic-gate int debug = 0; /* Debug flag */
807c478bd9Sstevel@tonic-gate int kdebugflag = 0; /* Tell kernel to print debug messages */
817c478bd9Sstevel@tonic-gate int default_device = 1; /* Using /dev/tty or equivalent */
827c478bd9Sstevel@tonic-gate char devnam[MAXPATHLEN]; /* Device name */
837c478bd9Sstevel@tonic-gate int crtscts = 0; /* Use hardware flow control */
847c478bd9Sstevel@tonic-gate bool modem = 1; /* Use modem control lines */
857c478bd9Sstevel@tonic-gate int inspeed = 0; /* Input/Output speed requested */
867c478bd9Sstevel@tonic-gate u_int32_t netmask = 0; /* IP netmask to set on interface */
877c478bd9Sstevel@tonic-gate bool lockflag = 0; /* Create lock file to lock the serial dev */
887c478bd9Sstevel@tonic-gate bool nodetach = 0; /* Don't detach from controlling tty */
897c478bd9Sstevel@tonic-gate bool updetach = 0; /* Detach once link is up */
907c478bd9Sstevel@tonic-gate char *initializer = NULL; /* Script to initialize physical link */
917c478bd9Sstevel@tonic-gate char *connect_script = NULL; /* Script to establish physical link */
927c478bd9Sstevel@tonic-gate char *disconnect_script = NULL; /* Script to disestablish physical link */
937c478bd9Sstevel@tonic-gate char *welcomer = NULL; /* Script to run after phys link estab. */
947c478bd9Sstevel@tonic-gate char *ptycommand = NULL; /* Command to run on other side of pty */
957c478bd9Sstevel@tonic-gate int maxconnect = 0; /* Maximum connect time */
967c478bd9Sstevel@tonic-gate char user[MAXNAMELEN]; /* Username for PAP */
977c478bd9Sstevel@tonic-gate char passwd[MAXSECRETLEN]; /* Password for PAP */
987c478bd9Sstevel@tonic-gate bool persist = 0; /* Reopen link after it goes down */
997c478bd9Sstevel@tonic-gate char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
1007c478bd9Sstevel@tonic-gate bool demand = 0; /* do dial-on-demand */
1017c478bd9Sstevel@tonic-gate char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
1027c478bd9Sstevel@tonic-gate int idle_time_limit = 0; /* Disconnect if idle for this many seconds */
1037c478bd9Sstevel@tonic-gate int holdoff = 30; /* # seconds to pause before reconnecting */
1047c478bd9Sstevel@tonic-gate bool holdoff_specified; /* true if a holdoff value has been given */
1057c478bd9Sstevel@tonic-gate bool notty = 0; /* Stdin/out is not a tty */
1067c478bd9Sstevel@tonic-gate char *pty_socket = NULL; /* Socket to connect to pty */
1077c478bd9Sstevel@tonic-gate char *record_file = NULL; /* File to record chars sent/received */
1087c478bd9Sstevel@tonic-gate int using_pty = 0;
1097c478bd9Sstevel@tonic-gate bool sync_serial = 0; /* Device is synchronous serial device */
1107c478bd9Sstevel@tonic-gate int log_to_fd = 1; /* send log messages to this fd too */
1117c478bd9Sstevel@tonic-gate int maxfail = 10; /* max # of unsuccessful connection attempts */
1127c478bd9Sstevel@tonic-gate char linkname[MAXPATHLEN]; /* logical name for link */
1137c478bd9Sstevel@tonic-gate bool tune_kernel; /* may alter kernel settings */
1147c478bd9Sstevel@tonic-gate int connect_delay = 1000; /* wait this many ms after connect script */
1157c478bd9Sstevel@tonic-gate int max_data_rate; /* max bytes/sec through charshunt */
1167c478bd9Sstevel@tonic-gate int req_unit = -1; /* requested interface unit */
1177c478bd9Sstevel@tonic-gate bool multilink = 0; /* Enable multilink operation */
1187c478bd9Sstevel@tonic-gate char *bundle_name = NULL; /* bundle name for multilink */
1197c478bd9Sstevel@tonic-gate bool direct_tty = 0; /* use standard input directly; not a tty */
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* Maximum depth of include files; prevents looping. */
1227c478bd9Sstevel@tonic-gate #define MAXFILENESTING 10
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate struct option_info initializer_info;
1257c478bd9Sstevel@tonic-gate struct option_info connect_script_info;
1267c478bd9Sstevel@tonic-gate struct option_info disconnect_script_info;
1277c478bd9Sstevel@tonic-gate struct option_info welcomer_info;
1287c478bd9Sstevel@tonic-gate struct option_info devnam_info;
1297c478bd9Sstevel@tonic-gate struct option_info ptycommand_info;
1307c478bd9Sstevel@tonic-gate struct option_info ipsrc_info;
1317c478bd9Sstevel@tonic-gate struct option_info ipdst_info;
1327c478bd9Sstevel@tonic-gate struct option_info speed_info;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
1357c478bd9Sstevel@tonic-gate struct bpf_program pass_filter;/* Filter program for packets to pass */
1367c478bd9Sstevel@tonic-gate struct bpf_program active_filter; /* Filter program for link-active pkts */
1377c478bd9Sstevel@tonic-gate pcap_t pc; /* Fake struct pcap so we can compile expr */
1387c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate char *current_option; /* the name of the option being parsed */
1417c478bd9Sstevel@tonic-gate bool privileged_option; /* set iff the current option came from root */
1427c478bd9Sstevel@tonic-gate char *option_source = NULL; /* string saying where the option came from */
1437c478bd9Sstevel@tonic-gate int option_line = 0; /* line number in file */
1447c478bd9Sstevel@tonic-gate bool log_to_file; /* log_to_fd is a file opened by us */
1457c478bd9Sstevel@tonic-gate bool log_to_specific_fd; /* log_to_fd was specified by user option */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * Prototypes.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate static int setdevname __P((char *));
1517c478bd9Sstevel@tonic-gate static int setipaddr __P((char *));
1527c478bd9Sstevel@tonic-gate static int setspeed __P((char *));
1537c478bd9Sstevel@tonic-gate static int noopt __P((char **, option_t *));
1547c478bd9Sstevel@tonic-gate static int setdomain __P((char **, option_t *));
1557c478bd9Sstevel@tonic-gate static int setnetmask __P((char **, option_t *));
1567c478bd9Sstevel@tonic-gate static int setxonxoff __P((char **, option_t *));
1577c478bd9Sstevel@tonic-gate static int readfile __P((char **, option_t *));
1587c478bd9Sstevel@tonic-gate static int callfile __P((char **, option_t *));
1597c478bd9Sstevel@tonic-gate static int showversion __P((char **, option_t *));
1607c478bd9Sstevel@tonic-gate static int showhelp __P((char **, option_t *));
1617c478bd9Sstevel@tonic-gate static int showalloptions __P((char **, option_t *));
1627c478bd9Sstevel@tonic-gate static void usage __P((void));
1637c478bd9Sstevel@tonic-gate static int setlogfile __P((char **, option_t *));
1647c478bd9Sstevel@tonic-gate #ifdef PLUGIN
1657c478bd9Sstevel@tonic-gate static int loadplugin __P((char **, option_t *));
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
1687c478bd9Sstevel@tonic-gate static int setpassfilter __P((char **, option_t *));
1697c478bd9Sstevel@tonic-gate static int setactivefilter __P((char **, option_t *));
1707c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
1717c478bd9Sstevel@tonic-gate static option_t *find_option __P((char *name));
1727c478bd9Sstevel@tonic-gate static int process_option __P((option_t *opt, char **argv, int sline));
1737c478bd9Sstevel@tonic-gate static int n_arguments __P((option_t *opt));
1747c478bd9Sstevel@tonic-gate static int number_option __P((char *str, u_int32_t *valp, int base));
1757c478bd9Sstevel@tonic-gate static u_int32_t opt_hash __P((const void *key));
1767c478bd9Sstevel@tonic-gate static int opt_compare __P((const void *p1, const void *p2));
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate typedef struct _opt_t {
1797c478bd9Sstevel@tonic-gate option_t *p;
1807c478bd9Sstevel@tonic-gate } opt_t;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate typedef struct _hashentry_t {
1837c478bd9Sstevel@tonic-gate struct _hashentry_t *next;
1847c478bd9Sstevel@tonic-gate opt_t opt;
1857c478bd9Sstevel@tonic-gate } hashentry_t;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * A prime number describing the size of hash table.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate #define OPTHASH_TBLSIZE 101
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * Chained hash table containing pointers to available options.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate static hashentry_t *hash_tbl[OPTHASH_TBLSIZE] = { NULL };
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate * Total number of entries in the hash table.
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate int hash_tblcnt = 0;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * Valid arguments.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate option_t general_options[] = {
2067c478bd9Sstevel@tonic-gate { "debug", o_int, &debug,
2077c478bd9Sstevel@tonic-gate "Increase debugging level", OPT_INC|OPT_NOARG|1 },
2087c478bd9Sstevel@tonic-gate { "-d", o_int, &debug,
2097c478bd9Sstevel@tonic-gate "Increase debugging level", OPT_INC|OPT_NOARG|1 },
2107c478bd9Sstevel@tonic-gate { "kdebug", o_int, &kdebugflag,
2117c478bd9Sstevel@tonic-gate "Set kernel driver debug level" },
2127c478bd9Sstevel@tonic-gate { "nodetach", o_bool, &nodetach,
2137c478bd9Sstevel@tonic-gate "Don't detach from controlling tty", 1 },
2147c478bd9Sstevel@tonic-gate { "-detach", o_bool, &nodetach,
2157c478bd9Sstevel@tonic-gate "Don't detach from controlling tty", 1 },
2167c478bd9Sstevel@tonic-gate { "updetach", o_bool, &updetach,
2177c478bd9Sstevel@tonic-gate "Detach from controlling tty once link is up", 1 },
2187c478bd9Sstevel@tonic-gate { "holdoff", o_int, &holdoff,
2197c478bd9Sstevel@tonic-gate "Set time in seconds before retrying connection" },
2207c478bd9Sstevel@tonic-gate { "idle", o_int, &idle_time_limit,
2217c478bd9Sstevel@tonic-gate "Set time in seconds before disconnecting idle link" },
2227c478bd9Sstevel@tonic-gate { "lock", o_bool, &lockflag,
2237c478bd9Sstevel@tonic-gate "Lock serial device with UUCP-style lock file", 1 },
2247c478bd9Sstevel@tonic-gate { "-all", o_special_noarg, (void *)noopt,
2257c478bd9Sstevel@tonic-gate "Don't request/allow any LCP or IPCP options (useless)" },
2267c478bd9Sstevel@tonic-gate { "init", o_string, &initializer,
2277c478bd9Sstevel@tonic-gate "A program to initialize the device",
2287c478bd9Sstevel@tonic-gate OPT_A2INFO | OPT_PRIVFIX, &initializer_info },
2297c478bd9Sstevel@tonic-gate { "connect", o_string, &connect_script,
2307c478bd9Sstevel@tonic-gate "A program to set up a connection",
2317c478bd9Sstevel@tonic-gate OPT_A2INFO | OPT_PRIVFIX, &connect_script_info },
2327c478bd9Sstevel@tonic-gate { "disconnect", o_string, &disconnect_script,
2337c478bd9Sstevel@tonic-gate "Program to disconnect serial device",
2347c478bd9Sstevel@tonic-gate OPT_A2INFO | OPT_PRIVFIX, &disconnect_script_info },
2357c478bd9Sstevel@tonic-gate { "welcome", o_string, &welcomer,
2367c478bd9Sstevel@tonic-gate "Script to welcome client",
2377c478bd9Sstevel@tonic-gate OPT_A2INFO | OPT_PRIVFIX, &welcomer_info },
2387c478bd9Sstevel@tonic-gate { "pty", o_string, &ptycommand,
2397c478bd9Sstevel@tonic-gate "Script to run on pseudo-tty master side",
2407c478bd9Sstevel@tonic-gate OPT_A2INFO | OPT_PRIVFIX | OPT_DEVNAM, &ptycommand_info },
2417c478bd9Sstevel@tonic-gate { "notty", o_bool, ¬ty,
2427c478bd9Sstevel@tonic-gate "Input/output is not a tty", OPT_DEVNAM | 1 },
2437c478bd9Sstevel@tonic-gate { "directtty", o_bool, &direct_tty,
2447c478bd9Sstevel@tonic-gate "Use standard input as tty without checking", OPT_DEVNAM | 1 },
2457c478bd9Sstevel@tonic-gate { "socket", o_string, &pty_socket,
2467c478bd9Sstevel@tonic-gate "Send and receive over socket, arg is host:port", OPT_DEVNAM },
2477c478bd9Sstevel@tonic-gate { "record", o_string, &record_file,
2487c478bd9Sstevel@tonic-gate "Record characters sent/received to file" },
2497c478bd9Sstevel@tonic-gate { "maxconnect", o_int, &maxconnect,
2507c478bd9Sstevel@tonic-gate "Set connection time limit", OPT_LLIMIT|OPT_NOINCR|OPT_ZEROINF },
2517c478bd9Sstevel@tonic-gate { "crtscts", o_int, &crtscts,
2527c478bd9Sstevel@tonic-gate "Set hardware (RTS/CTS) flow control", OPT_NOARG|OPT_VAL(1) },
2537c478bd9Sstevel@tonic-gate { "nocrtscts", o_int, &crtscts,
2547c478bd9Sstevel@tonic-gate "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2557c478bd9Sstevel@tonic-gate { "-crtscts", o_int, &crtscts,
2567c478bd9Sstevel@tonic-gate "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2577c478bd9Sstevel@tonic-gate { "cdtrcts", o_int, &crtscts,
2587c478bd9Sstevel@tonic-gate "Set alternate hardware (DTR/CTS) flow control", OPT_NOARG|OPT_VAL(2) },
2597c478bd9Sstevel@tonic-gate { "nocdtrcts", o_int, &crtscts,
2607c478bd9Sstevel@tonic-gate "Disable hardware flow control", OPT_NOARG|OPT_VAL(-1) },
2617c478bd9Sstevel@tonic-gate { "xonxoff", o_special_noarg, (void *)setxonxoff,
2627c478bd9Sstevel@tonic-gate "Set software (XON/XOFF) flow control" },
2637c478bd9Sstevel@tonic-gate { "domain", o_special, (void *)setdomain,
2647c478bd9Sstevel@tonic-gate "Add given domain name to hostname" },
2657c478bd9Sstevel@tonic-gate { "netmask", o_special, (void *)setnetmask,
2667c478bd9Sstevel@tonic-gate "set netmask" },
2677c478bd9Sstevel@tonic-gate { "modem", o_bool, &modem,
2687c478bd9Sstevel@tonic-gate "Use modem control lines", 1 },
2697c478bd9Sstevel@tonic-gate { "local", o_bool, &modem,
2707c478bd9Sstevel@tonic-gate "Don't use modem control lines" },
2717c478bd9Sstevel@tonic-gate { "file", o_special, (void *)readfile,
2727c478bd9Sstevel@tonic-gate "Take options from a file", OPT_PREPASS },
2737c478bd9Sstevel@tonic-gate { "call", o_special, (void *)callfile,
2747c478bd9Sstevel@tonic-gate "Take options from a privileged file", OPT_PREPASS },
2757c478bd9Sstevel@tonic-gate { "persist", o_bool, &persist,
2767c478bd9Sstevel@tonic-gate "Keep on reopening connection after close", 1 },
2777c478bd9Sstevel@tonic-gate { "nopersist", o_bool, &persist,
2787c478bd9Sstevel@tonic-gate "Turn off persist option" },
2797c478bd9Sstevel@tonic-gate { "demand", o_bool, &demand,
2807c478bd9Sstevel@tonic-gate "Dial on demand", OPT_INITONLY | 1, &persist },
2817c478bd9Sstevel@tonic-gate { "--version", o_special_noarg, (void *)showversion,
2827c478bd9Sstevel@tonic-gate "Show version number" },
2837c478bd9Sstevel@tonic-gate { "--help", o_special_noarg, (void *)showhelp,
2847c478bd9Sstevel@tonic-gate "Show brief listing of options" },
2857c478bd9Sstevel@tonic-gate { "-h", o_special_noarg, (void *)showhelp,
2867c478bd9Sstevel@tonic-gate "Show brief listing of options" },
2877c478bd9Sstevel@tonic-gate { "options", o_special_noarg, (void *)showalloptions,
2887c478bd9Sstevel@tonic-gate "Show full listing of options" },
2897c478bd9Sstevel@tonic-gate { "sync", o_bool, &sync_serial,
2907c478bd9Sstevel@tonic-gate "Use synchronous HDLC serial encoding", 1 },
2917c478bd9Sstevel@tonic-gate { "logfd", o_int, &log_to_fd,
2927c478bd9Sstevel@tonic-gate "Send log messages to this file descriptor",
2937c478bd9Sstevel@tonic-gate 0, &log_to_specific_fd },
2947c478bd9Sstevel@tonic-gate { "logfile", o_special, (void *)setlogfile,
2957c478bd9Sstevel@tonic-gate "Append log messages to this file" },
2967c478bd9Sstevel@tonic-gate { "nolog", o_int, &log_to_fd,
2977c478bd9Sstevel@tonic-gate "Don't send log messages to any file",
2987c478bd9Sstevel@tonic-gate OPT_NOARG | OPT_VAL(-1) },
2997c478bd9Sstevel@tonic-gate { "nologfd", o_int, &log_to_fd,
3007c478bd9Sstevel@tonic-gate "Don't send log messages to any file descriptor",
3017c478bd9Sstevel@tonic-gate OPT_NOARG | OPT_VAL(-1) },
3027c478bd9Sstevel@tonic-gate { "linkname", o_string, linkname,
3037c478bd9Sstevel@tonic-gate "Set logical name for link",
3047c478bd9Sstevel@tonic-gate OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN },
3057c478bd9Sstevel@tonic-gate { "maxfail", o_int, &maxfail,
3067c478bd9Sstevel@tonic-gate "Number of unsuccessful connection attempts to allow" },
3077c478bd9Sstevel@tonic-gate { "ktune", o_bool, &tune_kernel,
3087c478bd9Sstevel@tonic-gate "Alter kernel settings as necessary", 1 },
3097c478bd9Sstevel@tonic-gate { "noktune", o_bool, &tune_kernel,
3107c478bd9Sstevel@tonic-gate "Don't alter kernel settings", 0 },
3117c478bd9Sstevel@tonic-gate { "connect-delay", o_int, &connect_delay,
3127c478bd9Sstevel@tonic-gate "Maximum wait time (msec) after connect script finishes" },
3137c478bd9Sstevel@tonic-gate { "datarate", o_int, &max_data_rate,
3147c478bd9Sstevel@tonic-gate "Max data rate in bytes/sec for pty, notty, or record" },
3157c478bd9Sstevel@tonic-gate { "unit", o_int, &req_unit,
3167c478bd9Sstevel@tonic-gate "PPP interface unit number to use if possible", OPT_LLIMIT, 0, 0 },
3177c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
3187c478bd9Sstevel@tonic-gate { "multilink", o_bool, &multilink,
3197c478bd9Sstevel@tonic-gate "Enable multilink operation", 1 },
3207c478bd9Sstevel@tonic-gate { "nomultilink", o_bool, &multilink,
3217c478bd9Sstevel@tonic-gate "Disable multilink operation", 0 },
3227c478bd9Sstevel@tonic-gate { "mp", o_bool, &multilink,
3237c478bd9Sstevel@tonic-gate "Enable multilink operation", 1 },
3247c478bd9Sstevel@tonic-gate { "nomp", o_bool, &multilink,
3257c478bd9Sstevel@tonic-gate "Disable multilink operation", 0 },
3267c478bd9Sstevel@tonic-gate { "bundle", o_string, &bundle_name,
3277c478bd9Sstevel@tonic-gate "Bundle name for multilink" },
3287c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
3297c478bd9Sstevel@tonic-gate #ifdef PLUGIN
3307c478bd9Sstevel@tonic-gate { "plugin", o_special, (void *)loadplugin,
3317c478bd9Sstevel@tonic-gate "Load a plug-in module into pppd", OPT_PRIV },
3327c478bd9Sstevel@tonic-gate #endif /* PLUGIN */
3337c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
3347c478bd9Sstevel@tonic-gate { "pdebug", o_int, &dflag,
3357c478bd9Sstevel@tonic-gate "libpcap debugging" },
3367c478bd9Sstevel@tonic-gate { "pass-filter", o_special, setpassfilter,
3377c478bd9Sstevel@tonic-gate "set filter for packets to pass" },
3387c478bd9Sstevel@tonic-gate { "active-filter", o_special, setactivefilter,
3397c478bd9Sstevel@tonic-gate "set filter for active pkts" },
3407c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
3417c478bd9Sstevel@tonic-gate { NULL }
3427c478bd9Sstevel@tonic-gate };
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * This string gets printed out when "options" is given on the command
3467c478bd9Sstevel@tonic-gate * line. Following this string, all of the available options and
3477c478bd9Sstevel@tonic-gate * their descriptions are printed out as well. Certain options which
3487c478bd9Sstevel@tonic-gate * are not available as part of the option_t structure are placed in
3497c478bd9Sstevel@tonic-gate * the "dummy" option structure.
3507c478bd9Sstevel@tonic-gate */
3517c478bd9Sstevel@tonic-gate static const char pre_allopt_string[] = "\
3527c478bd9Sstevel@tonic-gate pppd version %s.%d%s\n\
3537c478bd9Sstevel@tonic-gate Usage: %s [ options ], where options are:\n\n\
3547c478bd9Sstevel@tonic-gate ";
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /* Do not call add_options() on this structure */
3577c478bd9Sstevel@tonic-gate static option_t dummy_options[] = {
3587c478bd9Sstevel@tonic-gate { "<device>", o_special_noarg, NULL,
3597c478bd9Sstevel@tonic-gate "Communicate over the named device" },
3607c478bd9Sstevel@tonic-gate { "<speed>", o_special_noarg, NULL,
3617c478bd9Sstevel@tonic-gate "Set the baud rate to <speed>" },
3627c478bd9Sstevel@tonic-gate { "[<loc>]:[<rem>]", o_special_noarg, NULL,
3637c478bd9Sstevel@tonic-gate "Set the local and/or remote interface IP addresses" },
3647c478bd9Sstevel@tonic-gate { NULL }
3657c478bd9Sstevel@tonic-gate };
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate static const char post_allopt_string[] = "\
3687c478bd9Sstevel@tonic-gate \n\
3697c478bd9Sstevel@tonic-gate Notes:\
3707c478bd9Sstevel@tonic-gate \t<n>\tinteger type argument\n\
3717c478bd9Sstevel@tonic-gate \t<s>\tstring type argument\n\
3727c478bd9Sstevel@tonic-gate \t<r>\tspecial type argument\n\
3737c478bd9Sstevel@tonic-gate \t(!)\tprivileged option available only when pppd is executed by root\n\
3747c478bd9Sstevel@tonic-gate \t\tor when found in the privileged option files (/etc/ppp/options,\n\
3757c478bd9Sstevel@tonic-gate \t\t/etc/ppp/options.ttyname, /etc/ppp/peers/name, or following\n\
3767c478bd9Sstevel@tonic-gate \t\t\"--\" in /etc/ppp/pap-secrets or /etc/ppp/chap-secrets).\n\
3777c478bd9Sstevel@tonic-gate \t(#)\tdisabled option\n\
3787c478bd9Sstevel@tonic-gate \n\
3797c478bd9Sstevel@tonic-gate Please see the pppd man page for details.\n";
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * parse_args - parse a string of arguments from the command line. If prepass
3837c478bd9Sstevel@tonic-gate * is true, we are scanning for the device name and only processing a few
3847c478bd9Sstevel@tonic-gate * options, so error messages are suppressed. Returns 1 upon successful
3857c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate int
parse_args(argc,argv)3887c478bd9Sstevel@tonic-gate parse_args(argc, argv)
3897c478bd9Sstevel@tonic-gate int argc;
3907c478bd9Sstevel@tonic-gate char **argv;
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate char *arg;
3937c478bd9Sstevel@tonic-gate option_t *opt;
3947c478bd9Sstevel@tonic-gate int ret;
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate privileged_option = privileged;
3977c478bd9Sstevel@tonic-gate option_source = "command line";
3987c478bd9Sstevel@tonic-gate option_line = 0;
3997c478bd9Sstevel@tonic-gate while (argc > 0) {
4007c478bd9Sstevel@tonic-gate arg = *argv++;
4017c478bd9Sstevel@tonic-gate --argc;
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate * First check to see if it's a known option name. If so, parse the
4057c478bd9Sstevel@tonic-gate * argument(s) and set the option.
4067c478bd9Sstevel@tonic-gate */
4077c478bd9Sstevel@tonic-gate opt = find_option(arg);
4087c478bd9Sstevel@tonic-gate if (opt != NULL) {
4097c478bd9Sstevel@tonic-gate int n = n_arguments(opt);
4107c478bd9Sstevel@tonic-gate if (argc < n) {
4117c478bd9Sstevel@tonic-gate option_error("too few parameters for option '%s'", arg);
4127c478bd9Sstevel@tonic-gate return (0);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate current_option = arg;
4157c478bd9Sstevel@tonic-gate if (!process_option(opt, argv, 0))
4167c478bd9Sstevel@tonic-gate return (0);
4177c478bd9Sstevel@tonic-gate argc -= n;
4187c478bd9Sstevel@tonic-gate argv += n;
4197c478bd9Sstevel@tonic-gate continue;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Maybe a tty name, speed or IP address ?
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate if (((ret = setdevname(arg)) == 0) &&
4267c478bd9Sstevel@tonic-gate ((ret = setspeed(arg)) == 0) &&
4277c478bd9Sstevel@tonic-gate ((ret = setipaddr(arg)) == 0) && !prepass) {
4287c478bd9Sstevel@tonic-gate option_error("unrecognized option '%s'", arg);
4297c478bd9Sstevel@tonic-gate usage();
4307c478bd9Sstevel@tonic-gate return (0);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate if (ret < 0) /* error */
4337c478bd9Sstevel@tonic-gate return (0);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate return (1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * options_from_file - read a string of options from a file, and
4407c478bd9Sstevel@tonic-gate * interpret them. Returns 1 upon successful processing of options,
4417c478bd9Sstevel@tonic-gate * and 0 otherwise.
4427c478bd9Sstevel@tonic-gate */
4437c478bd9Sstevel@tonic-gate int
options_from_file(char * filename,bool must_exist,bool check_prot,bool priv)4447c478bd9Sstevel@tonic-gate options_from_file
4457c478bd9Sstevel@tonic-gate #ifdef __STDC__
4467c478bd9Sstevel@tonic-gate (char *filename, bool must_exist, bool check_prot, bool priv)
4477c478bd9Sstevel@tonic-gate #else
4487c478bd9Sstevel@tonic-gate (filename, must_exist, check_prot, priv)
4497c478bd9Sstevel@tonic-gate char *filename;
4507c478bd9Sstevel@tonic-gate bool must_exist;
4517c478bd9Sstevel@tonic-gate bool check_prot;
4527c478bd9Sstevel@tonic-gate bool priv;
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate FILE *f;
4567c478bd9Sstevel@tonic-gate int i, newline, ret, err;
4577c478bd9Sstevel@tonic-gate option_t *opt;
4587c478bd9Sstevel@tonic-gate bool oldpriv;
4597c478bd9Sstevel@tonic-gate int oldline, sline;
4607c478bd9Sstevel@tonic-gate char *oldsource;
4617c478bd9Sstevel@tonic-gate char *argv[MAXARGS];
4627c478bd9Sstevel@tonic-gate char args[MAXARGS][MAXWORDLEN];
4637c478bd9Sstevel@tonic-gate char cmd[MAXWORDLEN];
4647c478bd9Sstevel@tonic-gate static bool firsterr = 1;
4657c478bd9Sstevel@tonic-gate static int nestlevel = 0;
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate if (nestlevel >= MAXFILENESTING) {
4687c478bd9Sstevel@tonic-gate option_error("file nesting too deep");
4697c478bd9Sstevel@tonic-gate return (0);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate if (check_prot)
4727c478bd9Sstevel@tonic-gate (void) seteuid(getuid());
4737c478bd9Sstevel@tonic-gate errno = 0;
4747c478bd9Sstevel@tonic-gate f = fopen(filename, "r");
4757c478bd9Sstevel@tonic-gate err = errno;
4767c478bd9Sstevel@tonic-gate if (check_prot)
4777c478bd9Sstevel@tonic-gate (void) seteuid(0);
4787c478bd9Sstevel@tonic-gate if (f == NULL) {
4797c478bd9Sstevel@tonic-gate if (!must_exist && err == ENOENT)
4807c478bd9Sstevel@tonic-gate return (1);
4817c478bd9Sstevel@tonic-gate errno = err;
4827c478bd9Sstevel@tonic-gate option_error("Can't open options file %s: %m", filename);
4837c478bd9Sstevel@tonic-gate return (0);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate nestlevel++;
4877c478bd9Sstevel@tonic-gate oldpriv = privileged_option;
4887c478bd9Sstevel@tonic-gate privileged_option = priv;
4897c478bd9Sstevel@tonic-gate oldsource = option_source;
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * strdup() is used here because the pointer might refer to the
4927c478bd9Sstevel@tonic-gate * caller's automatic (stack) storage, and the option_info array
4937c478bd9Sstevel@tonic-gate * records the source file name.
4947c478bd9Sstevel@tonic-gate */
4957c478bd9Sstevel@tonic-gate option_source = strdup(filename);
4967c478bd9Sstevel@tonic-gate oldline = option_line;
4977c478bd9Sstevel@tonic-gate option_line = 1;
4987c478bd9Sstevel@tonic-gate if (option_source == NULL)
4997c478bd9Sstevel@tonic-gate option_source = "file";
5007c478bd9Sstevel@tonic-gate ret = 0;
5017c478bd9Sstevel@tonic-gate while (getword(f, cmd, &newline, filename)) {
5027c478bd9Sstevel@tonic-gate sline = option_line;
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate * First see if it's a command.
5057c478bd9Sstevel@tonic-gate */
5067c478bd9Sstevel@tonic-gate opt = find_option(cmd);
5077c478bd9Sstevel@tonic-gate if (opt != NULL) {
5087c478bd9Sstevel@tonic-gate int n = n_arguments(opt);
5097c478bd9Sstevel@tonic-gate for (i = 0; i < n; ++i) {
5107c478bd9Sstevel@tonic-gate if (!getword(f, args[i], &newline, filename)) {
5117c478bd9Sstevel@tonic-gate option_error("too few parameters for option '%s'", cmd);
5127c478bd9Sstevel@tonic-gate goto err;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate argv[i] = args[i];
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate current_option = cmd;
5177c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_DEVEQUIV) && devnam_fixed) {
5187c478bd9Sstevel@tonic-gate option_error("the '%s' option may not be used here", cmd);
5197c478bd9Sstevel@tonic-gate goto err;
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate if (!process_option(opt, argv, sline))
5227c478bd9Sstevel@tonic-gate goto err;
5237c478bd9Sstevel@tonic-gate continue;
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate * Maybe a tty name, speed or IP address ?
5287c478bd9Sstevel@tonic-gate */
5297c478bd9Sstevel@tonic-gate if (((i = setdevname(cmd)) == 0) &&
5307c478bd9Sstevel@tonic-gate ((i = setspeed(cmd)) == 0) &&
5317c478bd9Sstevel@tonic-gate ((i = setipaddr(cmd)) == 0)) {
5327c478bd9Sstevel@tonic-gate option_error("unrecognized option '%s'", cmd);
5337c478bd9Sstevel@tonic-gate goto err;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate if (i < 0) /* error */
5367c478bd9Sstevel@tonic-gate goto err;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate ret = 1;
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate err:
5417c478bd9Sstevel@tonic-gate (void) fclose(f);
5427c478bd9Sstevel@tonic-gate /* We assume here that we abort all processing on the first error. */
5437c478bd9Sstevel@tonic-gate if (firsterr)
5447c478bd9Sstevel@tonic-gate firsterr = 0;
5457c478bd9Sstevel@tonic-gate else if (!prepass && !ret)
5467c478bd9Sstevel@tonic-gate option_error("error in included file");
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate * Cannot free option_source because it might be referenced in one
5497c478bd9Sstevel@tonic-gate * or more option_info structures now.
5507c478bd9Sstevel@tonic-gate */
5517c478bd9Sstevel@tonic-gate privileged_option = oldpriv;
5527c478bd9Sstevel@tonic-gate option_source = oldsource;
5537c478bd9Sstevel@tonic-gate option_line = oldline;
5547c478bd9Sstevel@tonic-gate nestlevel--;
5557c478bd9Sstevel@tonic-gate return (ret);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate * options_from_user - see if the user has a ~/.ppprc file, and if so,
5607c478bd9Sstevel@tonic-gate * interpret options from it. Returns 1 upon successful processing of
5617c478bd9Sstevel@tonic-gate * options, and 0 otherwise.
5627c478bd9Sstevel@tonic-gate */
5637c478bd9Sstevel@tonic-gate int
options_from_user()5647c478bd9Sstevel@tonic-gate options_from_user()
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate char *user, *path, *file;
5677c478bd9Sstevel@tonic-gate int ret;
5687c478bd9Sstevel@tonic-gate struct passwd *pw;
5697c478bd9Sstevel@tonic-gate size_t pl;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate pw = getpwuid(getuid());
5727c478bd9Sstevel@tonic-gate if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == '\0')
5737c478bd9Sstevel@tonic-gate return (1);
5747c478bd9Sstevel@tonic-gate file = _PATH_USEROPT;
5757c478bd9Sstevel@tonic-gate pl = strlen(user) + strlen(file) + 2;
5767c478bd9Sstevel@tonic-gate path = malloc(pl);
5777c478bd9Sstevel@tonic-gate if (path == NULL)
5787c478bd9Sstevel@tonic-gate novm("init file name");
5797c478bd9Sstevel@tonic-gate (void) slprintf(path, pl, "%s/%s", user, file);
5807c478bd9Sstevel@tonic-gate ret = options_from_file(path, 0, 1, privileged);
5817c478bd9Sstevel@tonic-gate free(path);
5827c478bd9Sstevel@tonic-gate return (ret);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * options_for_tty - see if an options file exists for the serial device, and
5877c478bd9Sstevel@tonic-gate * if so, interpret options from it. Returns 1 upon successful processing of
5887c478bd9Sstevel@tonic-gate * options, and 0 otherwise.
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate int
options_for_tty()5917c478bd9Sstevel@tonic-gate options_for_tty()
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate char *dev, *path, *p;
5947c478bd9Sstevel@tonic-gate int ret;
5957c478bd9Sstevel@tonic-gate size_t pl;
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate dev = devnam;
5987c478bd9Sstevel@tonic-gate if (strncmp(dev, "/dev/", 5) == 0)
5997c478bd9Sstevel@tonic-gate dev += 5;
6007c478bd9Sstevel@tonic-gate if (dev[0] == '\0' || strcmp(dev, "tty") == 0)
6017c478bd9Sstevel@tonic-gate return (1); /* don't look for /etc/ppp/options.tty */
6027c478bd9Sstevel@tonic-gate pl = strlen(_PATH_TTYOPT) + strlen(dev) + 1;
6037c478bd9Sstevel@tonic-gate path = malloc(pl);
6047c478bd9Sstevel@tonic-gate if (path == NULL)
6057c478bd9Sstevel@tonic-gate novm("tty init file name");
6067c478bd9Sstevel@tonic-gate (void) slprintf(path, pl, "%s%s", _PATH_TTYOPT, dev);
6077c478bd9Sstevel@tonic-gate /* Turn slashes into dots, for Solaris case (e.g. /dev/term/a) */
6087c478bd9Sstevel@tonic-gate for (p = path + strlen(_PATH_TTYOPT); *p != '\0'; ++p)
6097c478bd9Sstevel@tonic-gate if (*p == '/')
6107c478bd9Sstevel@tonic-gate *p = '.';
6117c478bd9Sstevel@tonic-gate ret = options_from_file(path, 0, 0, 1);
6127c478bd9Sstevel@tonic-gate free(path);
6137c478bd9Sstevel@tonic-gate return (ret);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate * options_from_list - process a string of options in a wordlist. Returns 1
6187c478bd9Sstevel@tonic-gate * upon successful processing of options, and 0 otherwise.
6197c478bd9Sstevel@tonic-gate */
6207c478bd9Sstevel@tonic-gate int
options_from_list(struct wordlist * w,bool priv)6217c478bd9Sstevel@tonic-gate options_from_list
6227c478bd9Sstevel@tonic-gate #ifdef __STDC__
6237c478bd9Sstevel@tonic-gate (struct wordlist *w, bool priv)
6247c478bd9Sstevel@tonic-gate #else
6257c478bd9Sstevel@tonic-gate (w, priv)
6267c478bd9Sstevel@tonic-gate struct wordlist *w;
6277c478bd9Sstevel@tonic-gate bool priv;
6287c478bd9Sstevel@tonic-gate #endif
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate char *argv[MAXARGS];
6317c478bd9Sstevel@tonic-gate option_t *opt;
6327c478bd9Sstevel@tonic-gate int i, ret = 0;
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate privileged_option = priv;
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate /* Caller is expected to set option_source and option_line. */
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate while (w != NULL) {
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate * First see if it's a command.
6417c478bd9Sstevel@tonic-gate */
6427c478bd9Sstevel@tonic-gate opt = find_option(w->word);
6437c478bd9Sstevel@tonic-gate if (opt != NULL) {
6447c478bd9Sstevel@tonic-gate int n = n_arguments(opt);
6457c478bd9Sstevel@tonic-gate struct wordlist *w0 = w;
6467c478bd9Sstevel@tonic-gate for (i = 0; i < n; ++i) {
6477c478bd9Sstevel@tonic-gate w = w->next;
6487c478bd9Sstevel@tonic-gate if (w == NULL) {
6497c478bd9Sstevel@tonic-gate option_error("too few parameters for option '%s'",
6507c478bd9Sstevel@tonic-gate w0->word);
6517c478bd9Sstevel@tonic-gate goto err;
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate argv[i] = w->word;
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate current_option = w0->word;
6567c478bd9Sstevel@tonic-gate if (!process_option(opt, argv, option_line))
6577c478bd9Sstevel@tonic-gate goto err;
6587c478bd9Sstevel@tonic-gate continue;
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Options from the {p,ch}ap-secrets files can't change the device
6637c478bd9Sstevel@tonic-gate * name nor the speed. Therefore, calls to setdevname() and
6647c478bd9Sstevel@tonic-gate * setspeed() were removed.
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate if ((i = setipaddr(w->word)) == 0) {
6677c478bd9Sstevel@tonic-gate option_error("unrecognized option '%s'", w->word);
6687c478bd9Sstevel@tonic-gate goto err;
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate if (i < 0) /* error */
6717c478bd9Sstevel@tonic-gate goto err;
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate ret = 1;
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate err:
6767c478bd9Sstevel@tonic-gate return (ret);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate * find_option - scan the option lists for the various protocols looking for an
6817c478bd9Sstevel@tonic-gate * entry with the given name. Returns a pointer to the matching option_t
6827c478bd9Sstevel@tonic-gate * structure upon successful processing of options, and NULL otherwise.
6837c478bd9Sstevel@tonic-gate */
6847c478bd9Sstevel@tonic-gate static option_t *
find_option(name)6857c478bd9Sstevel@tonic-gate find_option(name)
6867c478bd9Sstevel@tonic-gate char *name;
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate hashentry_t *bucket;
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate bucket = hash_tbl[opt_hash(name)];
6917c478bd9Sstevel@tonic-gate for (; bucket != NULL; bucket = bucket->next) {
6927c478bd9Sstevel@tonic-gate if (bucket->opt.p->name != NULL) {
6937c478bd9Sstevel@tonic-gate if ((strcmp(bucket->opt.p->name, name) == 0) &&
6947c478bd9Sstevel@tonic-gate !(bucket->opt.p->flags & OPT_DISABLE)) {
6957c478bd9Sstevel@tonic-gate return (bucket->opt.p);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate return (NULL);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate * process_option - process one new-style option (something other than a
7047c478bd9Sstevel@tonic-gate * port name, bit rate, or IP address). Returns 1 upon successful
7057c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
7067c478bd9Sstevel@tonic-gate */
7077c478bd9Sstevel@tonic-gate static int
process_option(opt,argv,sline)7087c478bd9Sstevel@tonic-gate process_option(opt, argv, sline)
7097c478bd9Sstevel@tonic-gate option_t *opt;
7107c478bd9Sstevel@tonic-gate char **argv;
7117c478bd9Sstevel@tonic-gate int sline;
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate u_int32_t v;
7147c478bd9Sstevel@tonic-gate int iv, a;
7157c478bd9Sstevel@tonic-gate char *sv;
7167c478bd9Sstevel@tonic-gate int (*parser) __P((char **, option_t *));
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_PREPASS) == 0 && prepass)
7197c478bd9Sstevel@tonic-gate return (1);
7207c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_INITONLY) && phase != PHASE_INITIALIZE) {
7217c478bd9Sstevel@tonic-gate option_error("it's too late to use the '%s' option", opt->name);
7227c478bd9Sstevel@tonic-gate return (0);
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_PRIV) && !privileged_option) {
7257c478bd9Sstevel@tonic-gate option_error("using the '%s' option requires root privilege",
7267c478bd9Sstevel@tonic-gate opt->name);
7277c478bd9Sstevel@tonic-gate return (0);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_ENABLE) && !privileged_option &&
7307c478bd9Sstevel@tonic-gate *(bool *)(opt->addr2) == 0) {
7317c478bd9Sstevel@tonic-gate option_error("'%s' option is disabled", opt->name);
7327c478bd9Sstevel@tonic-gate return (0);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_PRIVFIX) && !privileged_option) {
7357c478bd9Sstevel@tonic-gate struct option_info *ip = (struct option_info *) opt->addr2;
7367c478bd9Sstevel@tonic-gate if ((ip != NULL) && ip->priv) {
7377c478bd9Sstevel@tonic-gate option_error("'%s' option cannot be overridden", opt->name);
7387c478bd9Sstevel@tonic-gate return (0);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate switch (opt->type) {
7437c478bd9Sstevel@tonic-gate case o_bool:
7447c478bd9Sstevel@tonic-gate v = opt->flags & OPT_VALUE;
7457c478bd9Sstevel@tonic-gate *(bool *)(opt->addr) = (v != 0);
7467c478bd9Sstevel@tonic-gate if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
7477c478bd9Sstevel@tonic-gate *(bool *)(opt->addr2) = (v != 0);
7487c478bd9Sstevel@tonic-gate break;
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate case o_int:
7517c478bd9Sstevel@tonic-gate iv = 0;
7527c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_NOARG) == 0) {
7537c478bd9Sstevel@tonic-gate if (!int_option(*argv, &iv))
7547c478bd9Sstevel@tonic-gate return (0);
7557c478bd9Sstevel@tonic-gate if ((((opt->flags & OPT_LLIMIT) && (iv < opt->lower_limit)) ||
7567c478bd9Sstevel@tonic-gate ((opt->flags & OPT_ULIMIT) && (iv > opt->upper_limit))) &&
7577c478bd9Sstevel@tonic-gate !((opt->flags & OPT_ZEROOK) && (iv == 0))) {
7587c478bd9Sstevel@tonic-gate char *zok = (opt->flags & OPT_ZEROOK) ? " zero or" : "";
7597c478bd9Sstevel@tonic-gate switch (opt->flags & OPT_LIMITS) {
7607c478bd9Sstevel@tonic-gate case OPT_LLIMIT:
7617c478bd9Sstevel@tonic-gate option_error("%s value must be%s >= %d",
7627c478bd9Sstevel@tonic-gate opt->name, zok, opt->lower_limit);
7637c478bd9Sstevel@tonic-gate break;
7647c478bd9Sstevel@tonic-gate case OPT_ULIMIT:
7657c478bd9Sstevel@tonic-gate option_error("%s value must be%s <= %d",
7667c478bd9Sstevel@tonic-gate opt->name, zok, opt->upper_limit);
7677c478bd9Sstevel@tonic-gate break;
7687c478bd9Sstevel@tonic-gate case OPT_LIMITS:
7697c478bd9Sstevel@tonic-gate option_error("%s value must be%s between %d and %d",
7707c478bd9Sstevel@tonic-gate opt->name, zok, opt->lower_limit, opt->upper_limit);
7717c478bd9Sstevel@tonic-gate break;
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate return (0);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate a = opt->flags & OPT_VALUE;
7777c478bd9Sstevel@tonic-gate if (a >= 128)
7787c478bd9Sstevel@tonic-gate a -= 256; /* sign extend */
7797c478bd9Sstevel@tonic-gate iv += a;
7807c478bd9Sstevel@tonic-gate if (opt->flags & OPT_INC)
7817c478bd9Sstevel@tonic-gate iv += *(int *)(opt->addr);
7827c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_NOINCR) && !privileged_option) {
7837c478bd9Sstevel@tonic-gate int oldv = *(int *)(opt->addr);
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate if ((opt->flags & OPT_ZEROINF) && (iv == 0)) {
7867c478bd9Sstevel@tonic-gate if (oldv > 0) {
7877c478bd9Sstevel@tonic-gate option_error("%s value cannot be set to infinity; limited to %d",
7887c478bd9Sstevel@tonic-gate opt->name, oldv);
7897c478bd9Sstevel@tonic-gate return (0);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate } else if (iv > oldv) {
7927c478bd9Sstevel@tonic-gate option_error("%s value cannot be increased beyond %d",
7937c478bd9Sstevel@tonic-gate opt->name, oldv);
7947c478bd9Sstevel@tonic-gate return (0);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate *(int *)(opt->addr) = iv;
7987c478bd9Sstevel@tonic-gate if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
7997c478bd9Sstevel@tonic-gate *(int *)(opt->addr2) = iv;
8007c478bd9Sstevel@tonic-gate break;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate case o_uint32:
8037c478bd9Sstevel@tonic-gate if (opt->flags & OPT_NOARG) {
8047c478bd9Sstevel@tonic-gate v = opt->flags & OPT_VALUE;
8057c478bd9Sstevel@tonic-gate } else if (!number_option(*argv, &v, 16))
8067c478bd9Sstevel@tonic-gate return (0);
8077c478bd9Sstevel@tonic-gate if (opt->flags & OPT_OR)
8087c478bd9Sstevel@tonic-gate v |= *(u_int32_t *)(opt->addr);
8097c478bd9Sstevel@tonic-gate *(u_int32_t *)(opt->addr) = v;
8107c478bd9Sstevel@tonic-gate if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY))
8117c478bd9Sstevel@tonic-gate *(u_int32_t *)(opt->addr2) = v;
8127c478bd9Sstevel@tonic-gate break;
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate case o_string:
8157c478bd9Sstevel@tonic-gate if (opt->flags & OPT_STATIC) {
8167c478bd9Sstevel@tonic-gate (void) strlcpy((char *)(opt->addr), *argv, opt->upper_limit);
8177c478bd9Sstevel@tonic-gate if ((opt->addr2 != NULL) && (opt->flags & OPT_A2COPY)) {
8187c478bd9Sstevel@tonic-gate (void) strlcpy((char *)(opt->addr2), *argv, opt->upper_limit);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate } else {
8217c478bd9Sstevel@tonic-gate sv = strdup(*argv);
8227c478bd9Sstevel@tonic-gate if (sv == NULL)
8237c478bd9Sstevel@tonic-gate novm("option argument");
8247c478bd9Sstevel@tonic-gate *(char **)(opt->addr) = sv;
8257c478bd9Sstevel@tonic-gate if (opt->addr2 != NULL && (opt->flags & OPT_A2COPY))
8267c478bd9Sstevel@tonic-gate *(char **)(opt->addr2) = sv;
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate break;
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate case o_special_noarg:
8317c478bd9Sstevel@tonic-gate case o_special:
8327c478bd9Sstevel@tonic-gate parser = (int (*) __P((char **, option_t *))) opt->addr;
8337c478bd9Sstevel@tonic-gate if (!(*parser)(argv, opt))
8347c478bd9Sstevel@tonic-gate return (0);
8357c478bd9Sstevel@tonic-gate break;
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate if (opt->addr2 != NULL) {
8397c478bd9Sstevel@tonic-gate if (opt->flags & OPT_A2INFO) {
8407c478bd9Sstevel@tonic-gate struct option_info *ip = (struct option_info *) opt->addr2;
8417c478bd9Sstevel@tonic-gate ip->priv = privileged_option;
8427c478bd9Sstevel@tonic-gate ip->source = option_source;
8437c478bd9Sstevel@tonic-gate ip->line = sline;
8447c478bd9Sstevel@tonic-gate } else if ((opt->flags & (OPT_A2COPY|OPT_ENABLE)) == 0)
8457c478bd9Sstevel@tonic-gate *(bool *)(opt->addr2) = 1;
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate return (1);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * n_arguments - tell how many arguments an option takes. Returns 1 upon
8537c478bd9Sstevel@tonic-gate * successful processing of options, and 0 otherwise.
8547c478bd9Sstevel@tonic-gate */
8557c478bd9Sstevel@tonic-gate static int
n_arguments(opt)8567c478bd9Sstevel@tonic-gate n_arguments(opt)
8577c478bd9Sstevel@tonic-gate option_t *opt;
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate return ((opt->type == o_bool || opt->type == o_special_noarg ||
8607c478bd9Sstevel@tonic-gate (opt->flags & OPT_NOARG)) ? 0 : 1);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate * opt_hash - a hash function that works quite well for strings. Returns
8657c478bd9Sstevel@tonic-gate * the hash key of the supplied string.
8667c478bd9Sstevel@tonic-gate */
8677c478bd9Sstevel@tonic-gate static u_int32_t
opt_hash(key)8687c478bd9Sstevel@tonic-gate opt_hash(key)
8697c478bd9Sstevel@tonic-gate const void *key;
8707c478bd9Sstevel@tonic-gate {
8717c478bd9Sstevel@tonic-gate register const char *ptr;
8727c478bd9Sstevel@tonic-gate register u_int32_t val;
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate val = 0;
8757c478bd9Sstevel@tonic-gate ptr = key;
8767c478bd9Sstevel@tonic-gate while (*ptr != '\0') {
8777c478bd9Sstevel@tonic-gate int tmp;
8787c478bd9Sstevel@tonic-gate val = (val << 4) + (*ptr);
8797c478bd9Sstevel@tonic-gate tmp = val & 0xf0000000;
8807c478bd9Sstevel@tonic-gate if (tmp) {
8817c478bd9Sstevel@tonic-gate val ^= (tmp >> 24);
8827c478bd9Sstevel@tonic-gate val ^= tmp;
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate ptr++;
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate return (val % OPTHASH_TBLSIZE);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate * add_options - add a list of options to the chained hash table.
8917c478bd9Sstevel@tonic-gate * Also detect duplicate options, and if found, disable the older
8927c478bd9Sstevel@tonic-gate * definition and log it as an error.
8937c478bd9Sstevel@tonic-gate */
8947c478bd9Sstevel@tonic-gate void
add_options(opt)8957c478bd9Sstevel@tonic-gate add_options(opt)
8967c478bd9Sstevel@tonic-gate option_t *opt;
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate register option_t *sopt;
8997c478bd9Sstevel@tonic-gate register hashentry_t *bucket;
9007c478bd9Sstevel@tonic-gate register u_int32_t loc;
9017c478bd9Sstevel@tonic-gate hashentry_t *he;
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate /* fill hash-table */
9047c478bd9Sstevel@tonic-gate for (sopt = opt; sopt->name != NULL; ++sopt, hash_tblcnt++) {
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate /* first, allocate a hash entry */
9077c478bd9Sstevel@tonic-gate he = (hashentry_t *)malloc(sizeof(*he));
9087c478bd9Sstevel@tonic-gate if (he == NULL) {
9097c478bd9Sstevel@tonic-gate novm("option hash table entry");
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate he->opt.p = sopt;
9127c478bd9Sstevel@tonic-gate he->next = NULL;
9137c478bd9Sstevel@tonic-gate
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate * fill the chained hash table and take care of any collisions or
9167c478bd9Sstevel@tonic-gate * duplicate items.
9177c478bd9Sstevel@tonic-gate */
9187c478bd9Sstevel@tonic-gate loc = opt_hash(sopt->name);
9197c478bd9Sstevel@tonic-gate bucket = hash_tbl[loc];
9207c478bd9Sstevel@tonic-gate if (bucket != NULL) {
9217c478bd9Sstevel@tonic-gate for (;;) {
9227c478bd9Sstevel@tonic-gate if (!(bucket->opt.p->flags & OPT_DISABLE) &&
9237c478bd9Sstevel@tonic-gate strcmp(sopt->name, bucket->opt.p->name) == 0) {
9247c478bd9Sstevel@tonic-gate info("option '%s' redefined; old definition disabled",
9257c478bd9Sstevel@tonic-gate sopt->name);
9267c478bd9Sstevel@tonic-gate bucket->opt.p->flags |= OPT_DISABLE;
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate if (bucket->next == NULL)
9297c478bd9Sstevel@tonic-gate break;
9307c478bd9Sstevel@tonic-gate bucket = bucket->next;
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate bucket->next = he;
9337c478bd9Sstevel@tonic-gate } else {
9347c478bd9Sstevel@tonic-gate hash_tbl[loc] = he;
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate * remove_option - disable an option. Returns the option_t structure
9417c478bd9Sstevel@tonic-gate * of the disabled option, or NULL if the option name is invalid or if
9427c478bd9Sstevel@tonic-gate * the option has already been disabled.
9437c478bd9Sstevel@tonic-gate */
9447c478bd9Sstevel@tonic-gate option_t *
remove_option(name)9457c478bd9Sstevel@tonic-gate remove_option(name)
9467c478bd9Sstevel@tonic-gate char *name;
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate option_t *opt;
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate if ((opt = find_option(name)) != NULL) {
9517c478bd9Sstevel@tonic-gate opt->flags |= OPT_DISABLE;
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate return (opt);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate
9567c478bd9Sstevel@tonic-gate /*
9577c478bd9Sstevel@tonic-gate * opt_compare - a compare function supplied to the quicksort routine.
9587c478bd9Sstevel@tonic-gate * Returns an integer less than, equal to, or greater than zero to indicate
9597c478bd9Sstevel@tonic-gate * if the first argument is considered less than, equal to, or greater
9607c478bd9Sstevel@tonic-gate * than the second argument.
9617c478bd9Sstevel@tonic-gate */
9627c478bd9Sstevel@tonic-gate static int
opt_compare(p1,p2)9637c478bd9Sstevel@tonic-gate opt_compare(p1, p2)
9647c478bd9Sstevel@tonic-gate const void *p1;
9657c478bd9Sstevel@tonic-gate const void *p2;
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate opt_t *o1 = (opt_t *)p1;
9687c478bd9Sstevel@tonic-gate opt_t *o2 = (opt_t *)p2;
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate return (strcmp(o1->p->name, o2->p->name));
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate
9737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9747c478bd9Sstevel@tonic-gate static int
showalloptions(argv,topt)9757c478bd9Sstevel@tonic-gate showalloptions(argv, topt)
9767c478bd9Sstevel@tonic-gate char **argv;
9777c478bd9Sstevel@tonic-gate option_t *topt;
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate #define MAXOPTSTRLEN 257
9807c478bd9Sstevel@tonic-gate #define PRINTOPTIONS() { \
9817c478bd9Sstevel@tonic-gate (void) slprintf(opt_str, sizeof(opt_str), "%s", opt->name); \
9827c478bd9Sstevel@tonic-gate if ((opt->type == o_int || opt->type == o_uint32) && \
9837c478bd9Sstevel@tonic-gate !(opt->flags & OPT_NOARG)) { \
9847c478bd9Sstevel@tonic-gate (void) strlcat(opt_str, " <n>", sizeof(opt_str)); \
9857c478bd9Sstevel@tonic-gate } else if (opt->type == o_string) { \
9867c478bd9Sstevel@tonic-gate (void) strlcat(opt_str, " <s>", sizeof(opt_str)); \
9877c478bd9Sstevel@tonic-gate } else if (opt->type == o_special) { \
9887c478bd9Sstevel@tonic-gate (void) strlcat(opt_str, " <r>", sizeof(opt_str)); \
9897c478bd9Sstevel@tonic-gate } \
9907c478bd9Sstevel@tonic-gate if (opt->flags & OPT_PRIV) { \
9917c478bd9Sstevel@tonic-gate (void) strlcat(opt_str, " (!)", sizeof(opt_str)); \
9927c478bd9Sstevel@tonic-gate } else if (opt->flags & OPT_DISABLE) { \
9937c478bd9Sstevel@tonic-gate (void) strlcat(opt_str, " (#)", sizeof(opt_str)); \
9947c478bd9Sstevel@tonic-gate } \
9957c478bd9Sstevel@tonic-gate (void) printf("%-26s%s\n", opt_str, opt->description); \
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate char opt_str[MAXOPTSTRLEN];
9997c478bd9Sstevel@tonic-gate option_t *opt;
10007c478bd9Sstevel@tonic-gate hashentry_t *bucket;
10017c478bd9Sstevel@tonic-gate int i, sofar;
10027c478bd9Sstevel@tonic-gate opt_t *sopt;
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate if (phase != PHASE_INITIALIZE) {
10057c478bd9Sstevel@tonic-gate return (0);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate (void) printf(pre_allopt_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
10087c478bd9Sstevel@tonic-gate progname);
10097c478bd9Sstevel@tonic-gate for (opt = dummy_options; opt->name != NULL; ++opt) {
10107c478bd9Sstevel@tonic-gate PRINTOPTIONS();
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate sopt = malloc(sizeof(*sopt) * hash_tblcnt);
10147c478bd9Sstevel@tonic-gate if (sopt == NULL) {
10157c478bd9Sstevel@tonic-gate novm("sorted option table");
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate sofar = 0;
10197c478bd9Sstevel@tonic-gate for (i = 0; i < OPTHASH_TBLSIZE; i++) {
10207c478bd9Sstevel@tonic-gate for (bucket = hash_tbl[i]; bucket != NULL; bucket = bucket->next) {
10217c478bd9Sstevel@tonic-gate if (sofar >= hash_tblcnt) {
10227c478bd9Sstevel@tonic-gate fatal("options hash table corrupted; size mismatch");
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate sopt[sofar++].p = bucket->opt.p;
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate qsort((void *)sopt, sofar, sizeof(sopt[0]), opt_compare);
10297c478bd9Sstevel@tonic-gate for (i = 0; i < sofar; i++) {
10307c478bd9Sstevel@tonic-gate opt = sopt[i].p;
10317c478bd9Sstevel@tonic-gate PRINTOPTIONS();
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate
10347c478bd9Sstevel@tonic-gate (void) printf(post_allopt_string);
10357c478bd9Sstevel@tonic-gate (void) free(sopt);
10367c478bd9Sstevel@tonic-gate
10377c478bd9Sstevel@tonic-gate #undef MAXOPTSTRLEN
10387c478bd9Sstevel@tonic-gate #undef PRINTOPTIONS
10397c478bd9Sstevel@tonic-gate return (0);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate
10427c478bd9Sstevel@tonic-gate /*
10437c478bd9Sstevel@tonic-gate * usage - print out a message telling how to use the program.
10447c478bd9Sstevel@tonic-gate * This string gets printed out when either "--help" or an invalid option
10457c478bd9Sstevel@tonic-gate * is specified.
10467c478bd9Sstevel@tonic-gate */
10477c478bd9Sstevel@tonic-gate static void
usage()10487c478bd9Sstevel@tonic-gate usage()
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate static const char usage_string[] = "\
10517c478bd9Sstevel@tonic-gate pppd version %s.%d%s\n\
10527c478bd9Sstevel@tonic-gate Usage: %s [ options ], where options are:\n\
10537c478bd9Sstevel@tonic-gate \t<device>\tCommunicate over the named device\n\
10547c478bd9Sstevel@tonic-gate \t<speed>\t\tSet the baud rate to <speed>\n\
10557c478bd9Sstevel@tonic-gate \t<loc>:<rem>\tSet the local and/or remote interface IP\n\
10567c478bd9Sstevel@tonic-gate \t\t\taddresses. Either one may be omitted.\n\
10577c478bd9Sstevel@tonic-gate \tnoauth\t\tDon't require authentication from peer\n\
10587c478bd9Sstevel@tonic-gate \tconnect <p>\tInvoke shell command <p> to set up the serial line\n\
10597c478bd9Sstevel@tonic-gate \tdefaultroute\tAdd default route through interface\n\
10607c478bd9Sstevel@tonic-gate Use \"%s options\" or \"man pppd\" for more options.\n\
10617c478bd9Sstevel@tonic-gate ";
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate if (phase == PHASE_INITIALIZE)
10647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage_string, VERSION, PATCHLEVEL,
10657c478bd9Sstevel@tonic-gate IMPLEMENTATION, progname, progname);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate * showhelp - print out usage message and exit program upon success, or
10707c478bd9Sstevel@tonic-gate * return 0 otherwise.
10717c478bd9Sstevel@tonic-gate */
10727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10737c478bd9Sstevel@tonic-gate static int
showhelp(argv,opt)10747c478bd9Sstevel@tonic-gate showhelp(argv, opt)
10757c478bd9Sstevel@tonic-gate char **argv;
10767c478bd9Sstevel@tonic-gate option_t *opt;
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate if (phase == PHASE_INITIALIZE) {
10797c478bd9Sstevel@tonic-gate usage();
10807c478bd9Sstevel@tonic-gate exit(0);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate return (0);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate /*
10867c478bd9Sstevel@tonic-gate * showversion - print out the version number and exit program upon success,
10877c478bd9Sstevel@tonic-gate * or return 0 otherwise.
10887c478bd9Sstevel@tonic-gate */
10897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10907c478bd9Sstevel@tonic-gate static int
showversion(argv,opt)10917c478bd9Sstevel@tonic-gate showversion(argv, opt)
10927c478bd9Sstevel@tonic-gate char **argv;
10937c478bd9Sstevel@tonic-gate option_t *opt;
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate if (phase == PHASE_INITIALIZE) {
10967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "pppd version %s.%d%s\n", VERSION, PATCHLEVEL,
10977c478bd9Sstevel@tonic-gate IMPLEMENTATION);
10987c478bd9Sstevel@tonic-gate exit(0);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate return (0);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate * option_error - print a message about an error in an option. The message is
11057c478bd9Sstevel@tonic-gate * logged, and also sent to stderr if phase == PHASE_INITIALIZE.
11067c478bd9Sstevel@tonic-gate */
11077c478bd9Sstevel@tonic-gate void
option_error(char * fmt,...)11087c478bd9Sstevel@tonic-gate option_error __V((char *fmt, ...))
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate va_list args;
11117c478bd9Sstevel@tonic-gate char buf[256];
11127c478bd9Sstevel@tonic-gate int i, err;
11137c478bd9Sstevel@tonic-gate
11147c478bd9Sstevel@tonic-gate #if defined(__STDC__)
11157c478bd9Sstevel@tonic-gate va_start(args, fmt);
11167c478bd9Sstevel@tonic-gate #else
11177c478bd9Sstevel@tonic-gate char *fmt;
11187c478bd9Sstevel@tonic-gate va_start(args);
11197c478bd9Sstevel@tonic-gate fmt = va_arg(args, char *);
11207c478bd9Sstevel@tonic-gate #endif
11217c478bd9Sstevel@tonic-gate if (prepass) {
11227c478bd9Sstevel@tonic-gate va_end(args);
11237c478bd9Sstevel@tonic-gate return;
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate err = errno;
11267c478bd9Sstevel@tonic-gate if (option_source == NULL) {
11277c478bd9Sstevel@tonic-gate i = 0;
11287c478bd9Sstevel@tonic-gate } else if (option_line <= 0) {
11297c478bd9Sstevel@tonic-gate (void) strlcpy(buf, option_source, sizeof (buf));
11307c478bd9Sstevel@tonic-gate i = strlen(buf);
11317c478bd9Sstevel@tonic-gate } else {
11327c478bd9Sstevel@tonic-gate i = slprintf(buf, sizeof(buf), "%s:%d", option_source, option_line);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate if (i != 0) {
11357c478bd9Sstevel@tonic-gate (void) strlcat(buf, ": ", sizeof (buf));
11367c478bd9Sstevel@tonic-gate i += 2;
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate errno = err;
11397c478bd9Sstevel@tonic-gate (void) vslprintf(buf + i, sizeof (buf) - i, fmt, args);
11407c478bd9Sstevel@tonic-gate va_end(args);
11417c478bd9Sstevel@tonic-gate if ((phase == PHASE_INITIALIZE) && !detached)
11427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", progname, buf);
11437c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s", buf);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate
11467c478bd9Sstevel@tonic-gate /*
11477c478bd9Sstevel@tonic-gate * getword - read a word from a file. Words are delimited by white-space or by
11487c478bd9Sstevel@tonic-gate * quotes (" or '). Quotes, white-space and \ may be escaped with \.
11497c478bd9Sstevel@tonic-gate * \<newline> is ignored. Returns 1 upon successful processing of options,
11507c478bd9Sstevel@tonic-gate * and 0 otherwise.
11517c478bd9Sstevel@tonic-gate */
11527c478bd9Sstevel@tonic-gate int
getword(f,word,newlinep,filename)11537c478bd9Sstevel@tonic-gate getword(f, word, newlinep, filename)
11547c478bd9Sstevel@tonic-gate FILE *f;
11557c478bd9Sstevel@tonic-gate char *word;
11567c478bd9Sstevel@tonic-gate int *newlinep;
11577c478bd9Sstevel@tonic-gate char *filename;
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate int c, len, escape;
11607c478bd9Sstevel@tonic-gate int quoted, comment;
11617c478bd9Sstevel@tonic-gate int value, digit, got, n;
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate #define isoctal(c) ((c) >= '0' && (c) < '8')
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate *newlinep = 0;
11667c478bd9Sstevel@tonic-gate len = 0;
11677c478bd9Sstevel@tonic-gate escape = 0;
11687c478bd9Sstevel@tonic-gate comment = 0;
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate /*
11717c478bd9Sstevel@tonic-gate * First skip white-space and comments.
11727c478bd9Sstevel@tonic-gate */
11737c478bd9Sstevel@tonic-gate for (;;) {
11747c478bd9Sstevel@tonic-gate c = getc(f);
11757c478bd9Sstevel@tonic-gate if (c == EOF)
11767c478bd9Sstevel@tonic-gate break;
11777c478bd9Sstevel@tonic-gate
11787c478bd9Sstevel@tonic-gate /*
11797c478bd9Sstevel@tonic-gate * A newline means the end of a comment; backslash-newline
11807c478bd9Sstevel@tonic-gate * is ignored. Note that we cannot have escape && comment.
11817c478bd9Sstevel@tonic-gate */
11827c478bd9Sstevel@tonic-gate if (c == '\n') {
11837c478bd9Sstevel@tonic-gate option_line++;
11847c478bd9Sstevel@tonic-gate if (!escape) {
11857c478bd9Sstevel@tonic-gate *newlinep = 1;
11867c478bd9Sstevel@tonic-gate comment = 0;
11877c478bd9Sstevel@tonic-gate } else
11887c478bd9Sstevel@tonic-gate escape = 0;
11897c478bd9Sstevel@tonic-gate continue;
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate
11927c478bd9Sstevel@tonic-gate /*
11937c478bd9Sstevel@tonic-gate * Ignore characters other than newline in a comment.
11947c478bd9Sstevel@tonic-gate */
11957c478bd9Sstevel@tonic-gate if (comment)
11967c478bd9Sstevel@tonic-gate continue;
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate * If this character is escaped, we have a word start.
12007c478bd9Sstevel@tonic-gate */
12017c478bd9Sstevel@tonic-gate if (escape)
12027c478bd9Sstevel@tonic-gate break;
12037c478bd9Sstevel@tonic-gate
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate * If this is the escape character, look at the next character.
12067c478bd9Sstevel@tonic-gate */
12077c478bd9Sstevel@tonic-gate if (c == '\\') {
12087c478bd9Sstevel@tonic-gate escape = 1;
12097c478bd9Sstevel@tonic-gate continue;
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate * If this is the start of a comment, ignore the rest of the line.
12147c478bd9Sstevel@tonic-gate */
12157c478bd9Sstevel@tonic-gate if (c == '#') {
12167c478bd9Sstevel@tonic-gate comment = 1;
12177c478bd9Sstevel@tonic-gate continue;
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate /*
12217c478bd9Sstevel@tonic-gate * A non-whitespace character is the start of a word.
12227c478bd9Sstevel@tonic-gate */
12237c478bd9Sstevel@tonic-gate if (!isspace(c))
12247c478bd9Sstevel@tonic-gate break;
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate * Save the delimiter for quoted strings.
12297c478bd9Sstevel@tonic-gate */
12307c478bd9Sstevel@tonic-gate if (!escape && (c == '"' || c == '\'')) {
12317c478bd9Sstevel@tonic-gate quoted = c;
12327c478bd9Sstevel@tonic-gate c = getc(f);
12337c478bd9Sstevel@tonic-gate } else
12347c478bd9Sstevel@tonic-gate quoted = 0;
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate /*
12377c478bd9Sstevel@tonic-gate * Process characters until the end of the word.
12387c478bd9Sstevel@tonic-gate */
12397c478bd9Sstevel@tonic-gate while (c != EOF) {
12407c478bd9Sstevel@tonic-gate if (escape) {
12417c478bd9Sstevel@tonic-gate /*
12427c478bd9Sstevel@tonic-gate * This character is escaped: backslash-newline is ignored,
12437c478bd9Sstevel@tonic-gate * various other characters indicate particular values
12447c478bd9Sstevel@tonic-gate * as for C backslash-escapes.
12457c478bd9Sstevel@tonic-gate */
12467c478bd9Sstevel@tonic-gate escape = 0;
12477c478bd9Sstevel@tonic-gate if (c == '\n') {
12487c478bd9Sstevel@tonic-gate c = getc(f);
12497c478bd9Sstevel@tonic-gate continue;
12507c478bd9Sstevel@tonic-gate }
12517c478bd9Sstevel@tonic-gate
12527c478bd9Sstevel@tonic-gate got = 0;
12537c478bd9Sstevel@tonic-gate switch (c) {
12547c478bd9Sstevel@tonic-gate case 'a':
12557c478bd9Sstevel@tonic-gate value = '\a';
12567c478bd9Sstevel@tonic-gate break;
12577c478bd9Sstevel@tonic-gate case 'b':
12587c478bd9Sstevel@tonic-gate value = '\b';
12597c478bd9Sstevel@tonic-gate break;
12607c478bd9Sstevel@tonic-gate case 'f':
12617c478bd9Sstevel@tonic-gate value = '\f';
12627c478bd9Sstevel@tonic-gate break;
12637c478bd9Sstevel@tonic-gate case 'n':
12647c478bd9Sstevel@tonic-gate value = '\n';
12657c478bd9Sstevel@tonic-gate break;
12667c478bd9Sstevel@tonic-gate case 'r':
12677c478bd9Sstevel@tonic-gate value = '\r';
12687c478bd9Sstevel@tonic-gate break;
12697c478bd9Sstevel@tonic-gate case 's':
12707c478bd9Sstevel@tonic-gate value = ' ';
12717c478bd9Sstevel@tonic-gate break;
12727c478bd9Sstevel@tonic-gate case 't':
12737c478bd9Sstevel@tonic-gate value = '\t';
12747c478bd9Sstevel@tonic-gate break;
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate default:
12777c478bd9Sstevel@tonic-gate if (isoctal(c)) {
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate * \ddd octal sequence
12807c478bd9Sstevel@tonic-gate */
12817c478bd9Sstevel@tonic-gate value = 0;
12827c478bd9Sstevel@tonic-gate for (n = 0; n < 3 && isoctal(c); ++n) {
12837c478bd9Sstevel@tonic-gate value = (value << 3) + (c & 07);
12847c478bd9Sstevel@tonic-gate c = getc(f);
12857c478bd9Sstevel@tonic-gate }
12867c478bd9Sstevel@tonic-gate got = 1;
12877c478bd9Sstevel@tonic-gate break;
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate if (c == 'x') {
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate * \x<hex_string> sequence
12937c478bd9Sstevel@tonic-gate */
12947c478bd9Sstevel@tonic-gate value = 0;
12957c478bd9Sstevel@tonic-gate c = getc(f);
12967c478bd9Sstevel@tonic-gate for (n = 0; n < 2 && isxdigit(c); ++n) {
12977c478bd9Sstevel@tonic-gate digit = (islower(c) ? toupper(c) : c) - '0';
12987c478bd9Sstevel@tonic-gate if (digit > 10 || digit < 0) /* allow non-ASCII */
12997c478bd9Sstevel@tonic-gate digit += '0' + 10 - 'A';
13007c478bd9Sstevel@tonic-gate value = (value << 4) + digit;
13017c478bd9Sstevel@tonic-gate c = getc (f);
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate got = 1;
13047c478bd9Sstevel@tonic-gate break;
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate * Otherwise the character stands for itself.
13097c478bd9Sstevel@tonic-gate */
13107c478bd9Sstevel@tonic-gate value = c;
13117c478bd9Sstevel@tonic-gate break;
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate
13147c478bd9Sstevel@tonic-gate /*
13157c478bd9Sstevel@tonic-gate * Store the resulting character for the escape sequence.
13167c478bd9Sstevel@tonic-gate */
1317*084c1840SAlexander Pyhalov if (len < MAXWORDLEN) {
13187c478bd9Sstevel@tonic-gate word[len] = value;
13197c478bd9Sstevel@tonic-gate ++len;
1320*084c1840SAlexander Pyhalov }
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate if (!got)
13237c478bd9Sstevel@tonic-gate c = getc(f);
13247c478bd9Sstevel@tonic-gate continue;
13257c478bd9Sstevel@tonic-gate
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate
13287c478bd9Sstevel@tonic-gate /*
13297c478bd9Sstevel@tonic-gate * Not escaped: see if we've reached the end of the word.
13307c478bd9Sstevel@tonic-gate */
13317c478bd9Sstevel@tonic-gate if (quoted) {
13327c478bd9Sstevel@tonic-gate if (c == quoted)
13337c478bd9Sstevel@tonic-gate break;
13347c478bd9Sstevel@tonic-gate } else {
13357c478bd9Sstevel@tonic-gate if (isspace(c) || c == '#') {
13367c478bd9Sstevel@tonic-gate (void) ungetc (c, f);
13377c478bd9Sstevel@tonic-gate break;
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate
13417c478bd9Sstevel@tonic-gate /*
13427c478bd9Sstevel@tonic-gate * Backslash starts an escape sequence.
13437c478bd9Sstevel@tonic-gate */
13447c478bd9Sstevel@tonic-gate if (c == '\\') {
13457c478bd9Sstevel@tonic-gate escape = 1;
13467c478bd9Sstevel@tonic-gate c = getc(f);
13477c478bd9Sstevel@tonic-gate continue;
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate /*
13517c478bd9Sstevel@tonic-gate * An ordinary character: store it in the word and get another.
13527c478bd9Sstevel@tonic-gate */
1353*084c1840SAlexander Pyhalov if (len < MAXWORDLEN) {
13547c478bd9Sstevel@tonic-gate word[len] = c;
13557c478bd9Sstevel@tonic-gate ++len;
1356*084c1840SAlexander Pyhalov }
13577c478bd9Sstevel@tonic-gate
13587c478bd9Sstevel@tonic-gate c = getc(f);
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate
13617c478bd9Sstevel@tonic-gate /*
13627c478bd9Sstevel@tonic-gate * End of the word: check for errors.
13637c478bd9Sstevel@tonic-gate */
13647c478bd9Sstevel@tonic-gate if (c == EOF) {
13657c478bd9Sstevel@tonic-gate if (ferror(f)) {
13667c478bd9Sstevel@tonic-gate if (errno == 0)
13677c478bd9Sstevel@tonic-gate errno = EIO;
13687c478bd9Sstevel@tonic-gate option_error("Error reading %s: %m", filename);
13697c478bd9Sstevel@tonic-gate die(1);
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate * If len is zero, then we didn't find a word before the
13737c478bd9Sstevel@tonic-gate * end of the file.
13747c478bd9Sstevel@tonic-gate */
13757c478bd9Sstevel@tonic-gate if (len == 0)
13767c478bd9Sstevel@tonic-gate return (0);
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate
13797c478bd9Sstevel@tonic-gate /*
13807c478bd9Sstevel@tonic-gate * Warn if the word was too long, and append a terminating null.
13817c478bd9Sstevel@tonic-gate */
13827c478bd9Sstevel@tonic-gate if (len >= MAXWORDLEN) {
13837c478bd9Sstevel@tonic-gate option_error("warning: word in file %s too long (%.20s...)",
13847c478bd9Sstevel@tonic-gate filename, word);
13857c478bd9Sstevel@tonic-gate len = MAXWORDLEN - 1;
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate word[len] = '\0';
13887c478bd9Sstevel@tonic-gate
13897c478bd9Sstevel@tonic-gate return (1);
13907c478bd9Sstevel@tonic-gate
13917c478bd9Sstevel@tonic-gate #undef isoctal
13927c478bd9Sstevel@tonic-gate
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate
13957c478bd9Sstevel@tonic-gate /*
13967c478bd9Sstevel@tonic-gate * number_option - parse an unsigned numeric parameter for an option.
13977c478bd9Sstevel@tonic-gate * Returns 1 upon successful processing of options, and 0 otherwise.
13987c478bd9Sstevel@tonic-gate */
13997c478bd9Sstevel@tonic-gate static int
number_option(str,valp,base)14007c478bd9Sstevel@tonic-gate number_option(str, valp, base)
14017c478bd9Sstevel@tonic-gate char *str;
14027c478bd9Sstevel@tonic-gate u_int32_t *valp;
14037c478bd9Sstevel@tonic-gate int base;
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate char *ptr;
14067c478bd9Sstevel@tonic-gate
14077c478bd9Sstevel@tonic-gate *valp = strtoul(str, &ptr, base);
14087c478bd9Sstevel@tonic-gate if (ptr == str || *ptr != '\0') {
14097c478bd9Sstevel@tonic-gate option_error("invalid numeric parameter '%s' for '%s' option",
14107c478bd9Sstevel@tonic-gate str, current_option);
14117c478bd9Sstevel@tonic-gate return (0);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate return (1);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate
14167c478bd9Sstevel@tonic-gate /*
14177c478bd9Sstevel@tonic-gate * save_source - store option source, line, and privilege into an
14187c478bd9Sstevel@tonic-gate * option_info structure.
14197c478bd9Sstevel@tonic-gate */
14207c478bd9Sstevel@tonic-gate void
save_source(info)14217c478bd9Sstevel@tonic-gate save_source(info)
14227c478bd9Sstevel@tonic-gate struct option_info *info;
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate info->priv = privileged_option;
14257c478bd9Sstevel@tonic-gate info->source = option_source;
14267c478bd9Sstevel@tonic-gate info->line = option_line;
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate
14297c478bd9Sstevel@tonic-gate /*
14307c478bd9Sstevel@tonic-gate * set_source - set option source, line, and privilege from an
14317c478bd9Sstevel@tonic-gate * option_info structure.
14327c478bd9Sstevel@tonic-gate */
14337c478bd9Sstevel@tonic-gate void
set_source(info)14347c478bd9Sstevel@tonic-gate set_source(info)
14357c478bd9Sstevel@tonic-gate struct option_info *info;
14367c478bd9Sstevel@tonic-gate {
14377c478bd9Sstevel@tonic-gate privileged_option = info->priv;
14387c478bd9Sstevel@tonic-gate option_source = info->source;
14397c478bd9Sstevel@tonic-gate option_line = info->line;
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate /*
14437c478bd9Sstevel@tonic-gate * name_source - return string containing option source and line. Can
14447c478bd9Sstevel@tonic-gate * be used as part of an option_error call.
14457c478bd9Sstevel@tonic-gate */
14467c478bd9Sstevel@tonic-gate const char *
name_source(info)14477c478bd9Sstevel@tonic-gate name_source(info)
14487c478bd9Sstevel@tonic-gate struct option_info *info;
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate static char buf[MAXPATHLEN];
14517c478bd9Sstevel@tonic-gate
14527c478bd9Sstevel@tonic-gate if (info->source == NULL)
14537c478bd9Sstevel@tonic-gate return "none";
14547c478bd9Sstevel@tonic-gate if (info->line <= 0)
14557c478bd9Sstevel@tonic-gate return info->source;
14567c478bd9Sstevel@tonic-gate (void) slprintf(buf, sizeof (buf), "%s:%d", info->source, info->line);
14577c478bd9Sstevel@tonic-gate return (const char *)buf;
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate * int_option - like number_option, but valp is int *, the base is assumed to
14627c478bd9Sstevel@tonic-gate * be 0, and *valp is not changed if there is an error. Returns 1 upon
14637c478bd9Sstevel@tonic-gate * successful processing of options, and 0 otherwise.
14647c478bd9Sstevel@tonic-gate */
14657c478bd9Sstevel@tonic-gate int
int_option(str,valp)14667c478bd9Sstevel@tonic-gate int_option(str, valp)
14677c478bd9Sstevel@tonic-gate char *str;
14687c478bd9Sstevel@tonic-gate int *valp;
14697c478bd9Sstevel@tonic-gate {
14707c478bd9Sstevel@tonic-gate u_int32_t v;
14717c478bd9Sstevel@tonic-gate
14727c478bd9Sstevel@tonic-gate if (!number_option(str, &v, 0))
14737c478bd9Sstevel@tonic-gate return (0);
14747c478bd9Sstevel@tonic-gate *valp = (int) v;
14757c478bd9Sstevel@tonic-gate return (1);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate
14787c478bd9Sstevel@tonic-gate
14797c478bd9Sstevel@tonic-gate /*
14807c478bd9Sstevel@tonic-gate * The following procedures parse options.
14817c478bd9Sstevel@tonic-gate */
14827c478bd9Sstevel@tonic-gate
14837c478bd9Sstevel@tonic-gate /*
14847c478bd9Sstevel@tonic-gate * readfile - take commands from a file.
14857c478bd9Sstevel@tonic-gate */
14867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14877c478bd9Sstevel@tonic-gate static int
readfile(argv,opt)14887c478bd9Sstevel@tonic-gate readfile(argv, opt)
14897c478bd9Sstevel@tonic-gate char **argv;
14907c478bd9Sstevel@tonic-gate option_t *opt;
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate return (options_from_file(*argv, 1, 1, privileged_option));
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate * callfile - take commands from /etc/ppp/peers/<name>. Name may not contain
14977c478bd9Sstevel@tonic-gate * /../, start with / or ../, or end in /. Returns 1 upon successful
14987c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
14997c478bd9Sstevel@tonic-gate */
15007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15017c478bd9Sstevel@tonic-gate static int
callfile(argv,opt)15027c478bd9Sstevel@tonic-gate callfile(argv, opt)
15037c478bd9Sstevel@tonic-gate char **argv;
15047c478bd9Sstevel@tonic-gate option_t *opt;
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate char *fname, *arg, *p;
15077c478bd9Sstevel@tonic-gate int l, ok;
15087c478bd9Sstevel@tonic-gate
15097c478bd9Sstevel@tonic-gate arg = *argv;
15107c478bd9Sstevel@tonic-gate ok = 1;
15117c478bd9Sstevel@tonic-gate if (arg[0] == '/' || arg[0] == '\0')
15127c478bd9Sstevel@tonic-gate ok = 0;
15137c478bd9Sstevel@tonic-gate else {
15147c478bd9Sstevel@tonic-gate for (p = arg; *p != '\0'; ) {
15157c478bd9Sstevel@tonic-gate if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == '\0')) {
15167c478bd9Sstevel@tonic-gate ok = 0;
15177c478bd9Sstevel@tonic-gate break;
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate while (*p != '/' && *p != '\0')
15207c478bd9Sstevel@tonic-gate ++p;
15217c478bd9Sstevel@tonic-gate if (*p == '/')
15227c478bd9Sstevel@tonic-gate ++p;
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate if (!ok) {
15267c478bd9Sstevel@tonic-gate option_error("call option value may not contain .. or start with /");
15277c478bd9Sstevel@tonic-gate return (0);
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate
15307c478bd9Sstevel@tonic-gate l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
15317c478bd9Sstevel@tonic-gate if ((fname = (char *) malloc(l)) == NULL)
15327c478bd9Sstevel@tonic-gate novm("call file name");
15337c478bd9Sstevel@tonic-gate (void) slprintf(fname, l, "%s%s", _PATH_PEERFILES, arg);
15347c478bd9Sstevel@tonic-gate
15357c478bd9Sstevel@tonic-gate ok = options_from_file(fname, 1, 1, 1);
15367c478bd9Sstevel@tonic-gate
15377c478bd9Sstevel@tonic-gate free(fname);
15387c478bd9Sstevel@tonic-gate return (ok);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate
15417c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
15427c478bd9Sstevel@tonic-gate /*
15437c478bd9Sstevel@tonic-gate * setpdebug - set libpcap debugging level. Returns 1 upon successful
15447c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
15457c478bd9Sstevel@tonic-gate */
15467c478bd9Sstevel@tonic-gate static int
setpdebug(argv)15477c478bd9Sstevel@tonic-gate setpdebug(argv)
15487c478bd9Sstevel@tonic-gate char **argv;
15497c478bd9Sstevel@tonic-gate {
15507c478bd9Sstevel@tonic-gate return (int_option(*argv, &dflag));
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate /*
15547c478bd9Sstevel@tonic-gate * setpassfilter - set the pass filter for packets. Returns 1 upon successful
15557c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
15567c478bd9Sstevel@tonic-gate */
15577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15587c478bd9Sstevel@tonic-gate static int
setpassfilter(argv,opt)15597c478bd9Sstevel@tonic-gate setpassfilter(argv, opt)
15607c478bd9Sstevel@tonic-gate char **argv;
15617c478bd9Sstevel@tonic-gate option_t *opt;
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate pc.linktype = DLT_PPP;
15647c478bd9Sstevel@tonic-gate pc.snapshot = PPP_HDRLEN;
15657c478bd9Sstevel@tonic-gate
15667c478bd9Sstevel@tonic-gate if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
15677c478bd9Sstevel@tonic-gate return (1);
15687c478bd9Sstevel@tonic-gate option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
15697c478bd9Sstevel@tonic-gate return (0);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate * setactivefilter - set the active filter for packets. Returns 1 upon
15747c478bd9Sstevel@tonic-gate * successful processing of options, and 0 otherwise.
15757c478bd9Sstevel@tonic-gate */
15767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15777c478bd9Sstevel@tonic-gate static int
setactivefilter(argv,opt)15787c478bd9Sstevel@tonic-gate setactivefilter(argv, opt)
15797c478bd9Sstevel@tonic-gate char **argv;
15807c478bd9Sstevel@tonic-gate option_t *opt;
15817c478bd9Sstevel@tonic-gate {
15827c478bd9Sstevel@tonic-gate pc.linktype = DLT_PPP;
15837c478bd9Sstevel@tonic-gate pc.snapshot = PPP_HDRLEN;
15847c478bd9Sstevel@tonic-gate
15857c478bd9Sstevel@tonic-gate if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
15867c478bd9Sstevel@tonic-gate return (1);
15877c478bd9Sstevel@tonic-gate option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
15887c478bd9Sstevel@tonic-gate return (0);
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate #endif /* PPP_FILTER */
15917c478bd9Sstevel@tonic-gate
15927c478bd9Sstevel@tonic-gate /*
15937c478bd9Sstevel@tonic-gate * noopt - disable all options. Returns 1 upon successful processing of
15947c478bd9Sstevel@tonic-gate * options, and 0 otherwise.
15957c478bd9Sstevel@tonic-gate */
15967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15977c478bd9Sstevel@tonic-gate static int
noopt(argv,opt)15987c478bd9Sstevel@tonic-gate noopt(argv, opt)
15997c478bd9Sstevel@tonic-gate char **argv;
16007c478bd9Sstevel@tonic-gate option_t *opt;
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
16037c478bd9Sstevel@tonic-gate BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
16047c478bd9Sstevel@tonic-gate BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
16057c478bd9Sstevel@tonic-gate BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
16067c478bd9Sstevel@tonic-gate
16077c478bd9Sstevel@tonic-gate return (1);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate
16107c478bd9Sstevel@tonic-gate /*
16117c478bd9Sstevel@tonic-gate * setdomain - set domain name to append to hostname. Returns 1 upon
16127c478bd9Sstevel@tonic-gate * successful processing of options, and 0 otherwise.
16137c478bd9Sstevel@tonic-gate */
16147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16157c478bd9Sstevel@tonic-gate static int
setdomain(argv,opt)16167c478bd9Sstevel@tonic-gate setdomain(argv, opt)
16177c478bd9Sstevel@tonic-gate char **argv;
16187c478bd9Sstevel@tonic-gate option_t *opt;
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate if (!privileged_option) {
16217c478bd9Sstevel@tonic-gate option_error("using the domain option requires root privilege");
16227c478bd9Sstevel@tonic-gate return (0);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate (void) gethostname(hostname, MAXHOSTNAMELEN+1);
16257c478bd9Sstevel@tonic-gate if (**argv != '\0') {
16267c478bd9Sstevel@tonic-gate if (**argv != '.')
16277c478bd9Sstevel@tonic-gate (void) strncat(hostname, ".", MAXHOSTNAMELEN - strlen(hostname));
16287c478bd9Sstevel@tonic-gate (void) strncat(hostname, *argv, MAXHOSTNAMELEN - strlen(hostname));
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate hostname[MAXHOSTNAMELEN] = '\0';
16317c478bd9Sstevel@tonic-gate return (1);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate
16347c478bd9Sstevel@tonic-gate
16357c478bd9Sstevel@tonic-gate /*
16367c478bd9Sstevel@tonic-gate * setspeed - set the speed. Returns 1 upon successful processing of options,
16377c478bd9Sstevel@tonic-gate * and 0 otherwise.
16387c478bd9Sstevel@tonic-gate */
16397c478bd9Sstevel@tonic-gate static int
setspeed(arg)16407c478bd9Sstevel@tonic-gate setspeed(arg)
16417c478bd9Sstevel@tonic-gate char *arg;
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate char *ptr;
16447c478bd9Sstevel@tonic-gate int spd;
16457c478bd9Sstevel@tonic-gate
16467c478bd9Sstevel@tonic-gate if (prepass)
16477c478bd9Sstevel@tonic-gate return (1);
16487c478bd9Sstevel@tonic-gate spd = strtol(arg, &ptr, 0);
16497c478bd9Sstevel@tonic-gate if (ptr == arg || *ptr != '\0' || spd <= 0)
16507c478bd9Sstevel@tonic-gate return (0);
16517c478bd9Sstevel@tonic-gate inspeed = spd;
16527c478bd9Sstevel@tonic-gate save_source(&speed_info);
16537c478bd9Sstevel@tonic-gate return (1);
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate
16567c478bd9Sstevel@tonic-gate
16577c478bd9Sstevel@tonic-gate /*
16587c478bd9Sstevel@tonic-gate * setdevname - set the device name. Returns 1 upon successful processing of
16597c478bd9Sstevel@tonic-gate * options, 0 when the device does not exist, and -1 when an error is
16607c478bd9Sstevel@tonic-gate * encountered.
16617c478bd9Sstevel@tonic-gate */
16627c478bd9Sstevel@tonic-gate static int
setdevname(cp)16637c478bd9Sstevel@tonic-gate setdevname(cp)
16647c478bd9Sstevel@tonic-gate char *cp;
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate struct stat statbuf;
16677c478bd9Sstevel@tonic-gate char dev[MAXPATHLEN];
16687c478bd9Sstevel@tonic-gate
16697c478bd9Sstevel@tonic-gate if (*cp == '\0')
16707c478bd9Sstevel@tonic-gate return (0);
16717c478bd9Sstevel@tonic-gate
16727c478bd9Sstevel@tonic-gate if (strncmp("/dev/", cp, 5) != 0) {
16737c478bd9Sstevel@tonic-gate (void) strlcpy(dev, "/dev/", sizeof(dev));
16747c478bd9Sstevel@tonic-gate (void) strlcat(dev, cp, sizeof(dev));
16757c478bd9Sstevel@tonic-gate cp = dev;
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate
16787c478bd9Sstevel@tonic-gate /*
16797c478bd9Sstevel@tonic-gate * Check if there is a character device by this name.
16807c478bd9Sstevel@tonic-gate */
16817c478bd9Sstevel@tonic-gate if (stat(cp, &statbuf) < 0) {
16827c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
16837c478bd9Sstevel@tonic-gate return (0);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate option_error("Couldn't stat '%s': %m", cp);
16867c478bd9Sstevel@tonic-gate return (-1);
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate if (!S_ISCHR(statbuf.st_mode)) {
16897c478bd9Sstevel@tonic-gate option_error("'%s' is not a character device", cp);
16907c478bd9Sstevel@tonic-gate return (-1);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate
16937c478bd9Sstevel@tonic-gate if (phase != PHASE_INITIALIZE) {
16947c478bd9Sstevel@tonic-gate option_error("device name cannot be changed after initialization");
16957c478bd9Sstevel@tonic-gate return (-1);
16967c478bd9Sstevel@tonic-gate } else if (devnam_fixed) {
16977c478bd9Sstevel@tonic-gate option_error("per-tty options file may not specify device name");
16987c478bd9Sstevel@tonic-gate return (-1);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate
17017c478bd9Sstevel@tonic-gate if (devnam_info.priv && !privileged_option) {
17027c478bd9Sstevel@tonic-gate option_error("device name %s from %s cannot be overridden",
17037c478bd9Sstevel@tonic-gate devnam, name_source(&devnam_info));
17047c478bd9Sstevel@tonic-gate return (-1);
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate (void) strlcpy(devnam, cp, sizeof(devnam));
17087c478bd9Sstevel@tonic-gate devstat = statbuf;
17097c478bd9Sstevel@tonic-gate default_device = 0;
17107c478bd9Sstevel@tonic-gate save_source(&devnam_info);
17117c478bd9Sstevel@tonic-gate
17127c478bd9Sstevel@tonic-gate return (1);
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate
17167c478bd9Sstevel@tonic-gate /*
17177c478bd9Sstevel@tonic-gate * setipaddr - set the IP address. Returns 1 upon successful processing of
17187c478bd9Sstevel@tonic-gate * options, 0 when the argument does not contain a `:', and -1 for error.
17197c478bd9Sstevel@tonic-gate */
17207c478bd9Sstevel@tonic-gate static int
setipaddr(arg)17217c478bd9Sstevel@tonic-gate setipaddr(arg)
17227c478bd9Sstevel@tonic-gate char *arg;
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate struct hostent *hp;
17257c478bd9Sstevel@tonic-gate char *colon;
17267c478bd9Sstevel@tonic-gate u_int32_t local, remote;
17277c478bd9Sstevel@tonic-gate ipcp_options *wo = &ipcp_wantoptions[0];
17287c478bd9Sstevel@tonic-gate
17297c478bd9Sstevel@tonic-gate /*
17307c478bd9Sstevel@tonic-gate * IP address pair separated by ":".
17317c478bd9Sstevel@tonic-gate */
17327c478bd9Sstevel@tonic-gate if ((colon = strchr(arg, ':')) == NULL)
17337c478bd9Sstevel@tonic-gate return (0);
17347c478bd9Sstevel@tonic-gate if (prepass)
17357c478bd9Sstevel@tonic-gate return (1);
17367c478bd9Sstevel@tonic-gate
17377c478bd9Sstevel@tonic-gate /*
17387c478bd9Sstevel@tonic-gate * If colon first character, then no local addr.
17397c478bd9Sstevel@tonic-gate */
17407c478bd9Sstevel@tonic-gate if (colon != arg) {
17417c478bd9Sstevel@tonic-gate *colon = '\0';
17427c478bd9Sstevel@tonic-gate if ((local = inet_addr(arg)) == (u_int32_t) -1) {
17437c478bd9Sstevel@tonic-gate if ((hp = gethostbyname(arg)) == NULL) {
17447c478bd9Sstevel@tonic-gate option_error("unknown host: %s", arg);
17457c478bd9Sstevel@tonic-gate return (-1);
17467c478bd9Sstevel@tonic-gate } else {
17477c478bd9Sstevel@tonic-gate BCOPY(hp->h_addr, &local, sizeof(local));
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate if (bad_ip_adrs(local)) {
17517c478bd9Sstevel@tonic-gate option_error("bad local IP address %I", local);
17527c478bd9Sstevel@tonic-gate return (-1);
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate if (local != 0) {
17557c478bd9Sstevel@tonic-gate save_source(&ipsrc_info);
17567c478bd9Sstevel@tonic-gate wo->ouraddr = local;
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate *colon = ':';
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate
17617c478bd9Sstevel@tonic-gate /*
17627c478bd9Sstevel@tonic-gate * If colon last character, then no remote addr.
17637c478bd9Sstevel@tonic-gate */
17647c478bd9Sstevel@tonic-gate if (*++colon != '\0') {
17657c478bd9Sstevel@tonic-gate if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
17667c478bd9Sstevel@tonic-gate if ((hp = gethostbyname(colon)) == NULL) {
17677c478bd9Sstevel@tonic-gate option_error("unknown host: %s", colon);
17687c478bd9Sstevel@tonic-gate return (-1);
17697c478bd9Sstevel@tonic-gate } else {
17707c478bd9Sstevel@tonic-gate BCOPY(hp->h_addr, &remote, sizeof(remote));
17717c478bd9Sstevel@tonic-gate if (remote_name[0] == '\0')
17727c478bd9Sstevel@tonic-gate (void) strlcpy(remote_name, colon, sizeof(remote_name));
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate if (bad_ip_adrs(remote)) {
17767c478bd9Sstevel@tonic-gate option_error("bad remote IP address %I", remote);
17777c478bd9Sstevel@tonic-gate return (-1);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate if (remote != 0) {
17807c478bd9Sstevel@tonic-gate save_source(&ipdst_info);
17817c478bd9Sstevel@tonic-gate wo->hisaddr = remote;
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate
17857c478bd9Sstevel@tonic-gate return (1);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate
17887c478bd9Sstevel@tonic-gate
17897c478bd9Sstevel@tonic-gate /*
17907c478bd9Sstevel@tonic-gate * setnetmask - set the netmask to be used on the interface. Returns 1 upon
17917c478bd9Sstevel@tonic-gate * successful processing of options, and 0 otherwise.
17927c478bd9Sstevel@tonic-gate */
17937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17947c478bd9Sstevel@tonic-gate static int
setnetmask(argv,opt)17957c478bd9Sstevel@tonic-gate setnetmask(argv, opt)
17967c478bd9Sstevel@tonic-gate char **argv;
17977c478bd9Sstevel@tonic-gate option_t *opt;
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate u_int32_t mask;
18007c478bd9Sstevel@tonic-gate int n;
18017c478bd9Sstevel@tonic-gate char *p;
18027c478bd9Sstevel@tonic-gate
18037c478bd9Sstevel@tonic-gate /*
18047c478bd9Sstevel@tonic-gate * Unfortunately, if we use inet_addr, we can't tell whether
18057c478bd9Sstevel@tonic-gate * a result of all 1s is an error or a valid 255.255.255.255.
18067c478bd9Sstevel@tonic-gate */
18077c478bd9Sstevel@tonic-gate p = *argv;
18087c478bd9Sstevel@tonic-gate n = parse_dotted_ip(p, &mask);
18097c478bd9Sstevel@tonic-gate
18107c478bd9Sstevel@tonic-gate mask = htonl(mask);
18117c478bd9Sstevel@tonic-gate
18127c478bd9Sstevel@tonic-gate if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
18137c478bd9Sstevel@tonic-gate option_error("invalid netmask value '%s'", *argv);
18147c478bd9Sstevel@tonic-gate return (0);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate
18177c478bd9Sstevel@tonic-gate netmask = mask;
18187c478bd9Sstevel@tonic-gate return (1);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate
18217c478bd9Sstevel@tonic-gate /*
18227c478bd9Sstevel@tonic-gate * parse_dotted_ip - parse and convert the IP address string to make
18237c478bd9Sstevel@tonic-gate * sure it conforms to the dotted notation. Returns the length of
18247c478bd9Sstevel@tonic-gate * processed characters upon success, and 0 otherwise. If successful,
18257c478bd9Sstevel@tonic-gate * the converted IP address number is stored in vp, in the host byte
18267c478bd9Sstevel@tonic-gate * order.
18277c478bd9Sstevel@tonic-gate */
18287c478bd9Sstevel@tonic-gate int
parse_dotted_ip(cp,vp)18297c478bd9Sstevel@tonic-gate parse_dotted_ip(cp, vp)
18307c478bd9Sstevel@tonic-gate register char *cp;
18317c478bd9Sstevel@tonic-gate u_int32_t *vp;
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate register u_int32_t val, base, n;
18347c478bd9Sstevel@tonic-gate register char c;
18357c478bd9Sstevel@tonic-gate char *cp0 = cp;
18367c478bd9Sstevel@tonic-gate u_char parts[3], *pp = parts;
18377c478bd9Sstevel@tonic-gate
18387c478bd9Sstevel@tonic-gate if ((*cp == '\0') || (vp == NULL))
18397c478bd9Sstevel@tonic-gate return (0); /* disallow null string in cp */
18407c478bd9Sstevel@tonic-gate *vp = 0;
18417c478bd9Sstevel@tonic-gate again:
18427c478bd9Sstevel@tonic-gate /*
18437c478bd9Sstevel@tonic-gate * Collect number up to ``.''. Values are specified as for C:
18447c478bd9Sstevel@tonic-gate * 0x=hex, 0=octal, other=decimal.
18457c478bd9Sstevel@tonic-gate */
18467c478bd9Sstevel@tonic-gate val = 0; base = 10;
18477c478bd9Sstevel@tonic-gate if (*cp == '0') {
18487c478bd9Sstevel@tonic-gate if (*++cp == 'x' || *cp == 'X')
18497c478bd9Sstevel@tonic-gate base = 16, cp++;
18507c478bd9Sstevel@tonic-gate else
18517c478bd9Sstevel@tonic-gate base = 8;
18527c478bd9Sstevel@tonic-gate }
18537c478bd9Sstevel@tonic-gate while ((c = *cp) != '\0') {
18547c478bd9Sstevel@tonic-gate if (isdigit(c)) {
18557c478bd9Sstevel@tonic-gate if ((c - '0') >= base)
18567c478bd9Sstevel@tonic-gate break;
18577c478bd9Sstevel@tonic-gate val = (val * base) + (c - '0');
18587c478bd9Sstevel@tonic-gate cp++;
18597c478bd9Sstevel@tonic-gate continue;
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate if (base == 16 && isxdigit(c)) {
18627c478bd9Sstevel@tonic-gate val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
18637c478bd9Sstevel@tonic-gate cp++;
18647c478bd9Sstevel@tonic-gate continue;
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate break;
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate if (*cp == '.') {
18697c478bd9Sstevel@tonic-gate /*
18707c478bd9Sstevel@tonic-gate * Internet format:
18717c478bd9Sstevel@tonic-gate * a.b.c.d
18727c478bd9Sstevel@tonic-gate * a.b.c (with c treated as 16-bits)
18737c478bd9Sstevel@tonic-gate * a.b (with b treated as 24 bits)
18747c478bd9Sstevel@tonic-gate */
18757c478bd9Sstevel@tonic-gate if ((pp >= parts + 3) || (val > 0xff)) {
18767c478bd9Sstevel@tonic-gate return (0);
18777c478bd9Sstevel@tonic-gate }
18787c478bd9Sstevel@tonic-gate *pp++ = (u_char)val;
18797c478bd9Sstevel@tonic-gate cp++;
18807c478bd9Sstevel@tonic-gate goto again;
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate /*
18837c478bd9Sstevel@tonic-gate * Check for trailing characters.
18847c478bd9Sstevel@tonic-gate */
18857c478bd9Sstevel@tonic-gate if (*cp != '\0' && !isspace(*cp)) {
18867c478bd9Sstevel@tonic-gate return (0);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate /*
18897c478bd9Sstevel@tonic-gate * Concoct the address according to the number of parts specified.
18907c478bd9Sstevel@tonic-gate */
18917c478bd9Sstevel@tonic-gate n = pp - parts;
18927c478bd9Sstevel@tonic-gate switch (n) {
18937c478bd9Sstevel@tonic-gate case 0: /* a -- 32 bits */
18947c478bd9Sstevel@tonic-gate break;
18957c478bd9Sstevel@tonic-gate case 1: /* a.b -- 8.24 bits */
18967c478bd9Sstevel@tonic-gate if (val > 0xffffff)
18977c478bd9Sstevel@tonic-gate return (0);
18987c478bd9Sstevel@tonic-gate val |= parts[0] << 24;
18997c478bd9Sstevel@tonic-gate break;
19007c478bd9Sstevel@tonic-gate case 2: /* a.b.c -- 8.8.16 bits */
19017c478bd9Sstevel@tonic-gate if (val > 0xffff)
19027c478bd9Sstevel@tonic-gate return (0);
19037c478bd9Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16);
19047c478bd9Sstevel@tonic-gate break;
19057c478bd9Sstevel@tonic-gate case 3: /* a.b.c.d -- 8.8.8.8 bits */
19067c478bd9Sstevel@tonic-gate if (val > 0xff)
19077c478bd9Sstevel@tonic-gate return (0);
19087c478bd9Sstevel@tonic-gate val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
19097c478bd9Sstevel@tonic-gate break;
19107c478bd9Sstevel@tonic-gate default:
19117c478bd9Sstevel@tonic-gate return (0);
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate *vp = val;
19147c478bd9Sstevel@tonic-gate return (cp - cp0);
19157c478bd9Sstevel@tonic-gate }
19167c478bd9Sstevel@tonic-gate
19177c478bd9Sstevel@tonic-gate /*
19187c478bd9Sstevel@tonic-gate * setxonxoff - modify the asyncmap to include escaping XON and XOFF
19197c478bd9Sstevel@tonic-gate * characters used for software flow control. Returns 1 upon successful
19207c478bd9Sstevel@tonic-gate * processing of options, and 0 otherwise.
19217c478bd9Sstevel@tonic-gate */
19227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19237c478bd9Sstevel@tonic-gate static int
setxonxoff(argv,opt)19247c478bd9Sstevel@tonic-gate setxonxoff(argv, opt)
19257c478bd9Sstevel@tonic-gate char **argv;
19267c478bd9Sstevel@tonic-gate option_t *opt;
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate int xonxoff = 0x000A0000;
19297c478bd9Sstevel@tonic-gate
19307c478bd9Sstevel@tonic-gate lcp_wantoptions[0].neg_asyncmap = 1;
19317c478bd9Sstevel@tonic-gate lcp_wantoptions[0].asyncmap |= xonxoff; /* escape ^S and ^Q */
19327c478bd9Sstevel@tonic-gate lcp_allowoptions[0].asyncmap |= xonxoff;
19337c478bd9Sstevel@tonic-gate xmit_accm[0][0] |= xonxoff;
19347c478bd9Sstevel@tonic-gate xmit_accm[0][4] |= xonxoff; /* escape 0x91 and 0x93 as well */
19357c478bd9Sstevel@tonic-gate
19367c478bd9Sstevel@tonic-gate crtscts = -2;
19377c478bd9Sstevel@tonic-gate return (1);
19387c478bd9Sstevel@tonic-gate }
19397c478bd9Sstevel@tonic-gate
19407c478bd9Sstevel@tonic-gate /*
19417c478bd9Sstevel@tonic-gate * setlogfile - open (or create) a file used for logging purposes. Returns 1
19427c478bd9Sstevel@tonic-gate * upon success, and 0 otherwise.
19437c478bd9Sstevel@tonic-gate */
19447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19457c478bd9Sstevel@tonic-gate static int
setlogfile(argv,opt)19467c478bd9Sstevel@tonic-gate setlogfile(argv, opt)
19477c478bd9Sstevel@tonic-gate char **argv;
19487c478bd9Sstevel@tonic-gate option_t *opt;
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate int fd, err;
19517c478bd9Sstevel@tonic-gate
19527c478bd9Sstevel@tonic-gate if (!privileged_option)
19537c478bd9Sstevel@tonic-gate (void) seteuid(getuid());
19547c478bd9Sstevel@tonic-gate fd = open(*argv, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0644);
19557c478bd9Sstevel@tonic-gate if (fd < 0 && errno == EEXIST)
19567c478bd9Sstevel@tonic-gate fd = open(*argv, O_WRONLY | O_APPEND);
19577c478bd9Sstevel@tonic-gate err = errno;
19587c478bd9Sstevel@tonic-gate if (!privileged_option)
19597c478bd9Sstevel@tonic-gate (void) seteuid(0);
19607c478bd9Sstevel@tonic-gate if (fd < 0) {
19617c478bd9Sstevel@tonic-gate errno = err;
19627c478bd9Sstevel@tonic-gate option_error("Can't open log file %s: %m", *argv);
19637c478bd9Sstevel@tonic-gate return (0);
19647c478bd9Sstevel@tonic-gate }
19657c478bd9Sstevel@tonic-gate if (log_to_file && log_to_fd >= 0)
19667c478bd9Sstevel@tonic-gate (void) close(log_to_fd);
19677c478bd9Sstevel@tonic-gate log_to_fd = fd;
19687c478bd9Sstevel@tonic-gate log_to_file = 1;
19697c478bd9Sstevel@tonic-gate early_log = 0;
19707c478bd9Sstevel@tonic-gate return (1);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate
19737c478bd9Sstevel@tonic-gate #ifdef PLUGIN
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate * loadplugin - load and initialize the plugin. Returns 1 upon successful
19767c478bd9Sstevel@tonic-gate * processing of the plugin, and 0 otherwise.
19777c478bd9Sstevel@tonic-gate */
19787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19797c478bd9Sstevel@tonic-gate static int
loadplugin(argv,opt)19807c478bd9Sstevel@tonic-gate loadplugin(argv, opt)
19817c478bd9Sstevel@tonic-gate char **argv;
19827c478bd9Sstevel@tonic-gate option_t *opt;
19837c478bd9Sstevel@tonic-gate {
19847c478bd9Sstevel@tonic-gate char *arg = *argv;
19857c478bd9Sstevel@tonic-gate void *handle;
19867c478bd9Sstevel@tonic-gate const char *err;
19877c478bd9Sstevel@tonic-gate void (*init) __P((void));
19887c478bd9Sstevel@tonic-gate
19897c478bd9Sstevel@tonic-gate handle = dlopen(arg, RTLD_GLOBAL | RTLD_NOW);
19907c478bd9Sstevel@tonic-gate if (handle == NULL) {
19917c478bd9Sstevel@tonic-gate err = dlerror();
19927c478bd9Sstevel@tonic-gate if (err != NULL)
19937c478bd9Sstevel@tonic-gate option_error("%s", err);
19947c478bd9Sstevel@tonic-gate option_error("Couldn't load plugin %s", arg);
19957c478bd9Sstevel@tonic-gate return (0);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate init = (void (*)(void))dlsym(handle, "plugin_init");
19987c478bd9Sstevel@tonic-gate if (init == NULL) {
19997c478bd9Sstevel@tonic-gate option_error("%s has no initialization entry point", arg);
20007c478bd9Sstevel@tonic-gate (void) dlclose(handle);
20017c478bd9Sstevel@tonic-gate return (0);
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate info("Plugin %s loaded.", arg);
20047c478bd9Sstevel@tonic-gate (*init)();
20057c478bd9Sstevel@tonic-gate return (1);
20067c478bd9Sstevel@tonic-gate }
20077c478bd9Sstevel@tonic-gate #endif /* PLUGIN */
2008