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