xref: /openbsd/usr.sbin/pppd/options.c (revision 404b540a)
1 /*	$OpenBSD: options.c,v 1.23 2007/10/17 20:10:44 chl Exp $	*/
2 
3 /*
4  * options.c - handles option processing for PPP.
5  *
6  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. The name "Carnegie Mellon University" must not be used to
21  *    endorse or promote products derived from this software without
22  *    prior written permission. For permission or any legal
23  *    details, please contact
24  *      Office of Technology Transfer
25  *      Carnegie Mellon University
26  *      5000 Forbes Avenue
27  *      Pittsburgh, PA  15213-3890
28  *      (412) 268-4387, fax: (412) 268-7395
29  *      tech-transfer@andrew.cmu.edu
30  *
31  * 4. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by Computing Services
34  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
35  *
36  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43  */
44 
45 #ifndef lint
46 #if 0
47 static char rcsid[] = "Id: options.c,v 1.42 1998/03/26 04:46:06 paulus Exp $";
48 #else
49 static char rcsid[] = "$OpenBSD: options.c,v 1.23 2007/10/17 20:10:44 chl Exp $";
50 #endif
51 #endif
52 
53 #include <ctype.h>
54 #include <stdio.h>
55 #include <errno.h>
56 #include <unistd.h>
57 #include <limits.h>
58 #include <stdlib.h>
59 #include <termios.h>
60 #include <syslog.h>
61 #include <string.h>
62 #include <netdb.h>
63 #include <pwd.h>
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #ifdef PPP_FILTER
69 #include <pcap.h>
70 #include <pcap-int.h>	/* XXX: To get struct pcap */
71 #endif
72 
73 #include "pppd.h"
74 #include "pathnames.h"
75 #include "patchlevel.h"
76 #include "fsm.h"
77 #include "lcp.h"
78 #include "ipcp.h"
79 #include "upap.h"
80 #include "chap.h"
81 #include "ccp.h"
82 #ifdef CBCP_SUPPORT
83 #include "cbcp.h"
84 #endif
85 
86 #include <net/ppp-comp.h>
87 
88 #define FALSE	0
89 #define TRUE	1
90 
91 #if defined(ultrix) || defined(NeXT)
92 char *strdup(char *);
93 #endif
94 
95 #ifndef GIDSET_TYPE
96 #define GIDSET_TYPE	gid_t
97 #endif
98 
99 /*
100  * Option variables and default values.
101  */
102 #ifdef PPP_FILTER
103 int	dflag = 0;		/* Tell libpcap we want debugging */
104 #endif
105 int	debug = 0;		/* Debug flag */
106 int	kdebugflag = 0;		/* Tell kernel to print debug messages */
107 int	default_device = 1;	/* Using /dev/tty or equivalent */
108 char	devnam[MAXPATHLEN] = "/dev/tty";	/* Device name */
109 int	crtscts = 0;		/* Use hardware flow control */
110 int	modem = 1;		/* Use modem control lines */
111 int	modem_chat = 0;		/* Use modem control lines during chat */
112 int	inspeed = 0;		/* Input/Output speed requested */
113 u_int32_t netmask = 0;		/* IP netmask to set on interface */
114 int	lockflag = 0;		/* Create lock file to lock the serial dev */
115 int	nodetach = 0;		/* Don't detach from controlling tty */
116 char	*connector = NULL;	/* Script to establish physical link */
117 char	*disconnector = NULL;	/* Script to disestablish physical link */
118 char	*welcomer = NULL;	/* Script to run after phys link estab. */
119 int	maxconnect = 0;		/* Maximum connect time */
120 char	user[MAXNAMELEN];	/* Username for PAP */
121 char	passwd[MAXSECRETLEN];	/* Password for PAP */
122 int	auth_required = 0;	/* Peer is required to authenticate */
123 int	defaultroute = 0;	/* assign default route through interface */
124 int	proxyarp = 0;		/* Set up proxy ARP entry for peer */
125 int	persist = 0;		/* Reopen link after it goes down */
126 int	uselogin = 0;		/* Use /etc/passwd for checking PAP */
127 int	lcp_echo_interval = 0; 	/* Interval between LCP echo-requests */
128 int	lcp_echo_fails = 0;	/* Tolerance to unanswered echo-requests */
129 char	our_name[MAXNAMELEN];	/* Our name for authentication purposes */
130 char	remote_name[MAXNAMELEN]; /* Peer's name for authentication */
131 int	explicit_remote = 0;	/* User specified explicit remote name */
132 int	usehostname = 0;	/* Use hostname for our_name */
133 int	disable_defaultip = 0;	/* Don't use hostname for default IP adrs */
134 int	demand = 0;		/* do dial-on-demand */
135 char	*ipparam = NULL;	/* Extra parameter for ip up/down scripts */
136 int	cryptpap;		/* Passwords in pap-secrets are encrypted */
137 int	idle_time_limit = 0;	/* Disconnect if idle for this many seconds */
138 int	holdoff = 30;		/* # seconds to pause before reconnecting */
139 int	refuse_pap = 0;		/* Set to say we won't do PAP */
140 int	refuse_chap = 0;	/* Set to say we won't do CHAP */
141 
142 #ifdef MSLANMAN
143 int	ms_lanman = 0;    	/* Nonzero if use LanMan password instead of NT */
144 			  	/* Has meaning only with MS-CHAP challenges */
145 #endif
146 
147 struct option_info auth_req_info;
148 struct option_info connector_info;
149 struct option_info disconnector_info;
150 struct option_info welcomer_info;
151 struct option_info devnam_info;
152 #ifdef PPP_FILTER
153 struct	bpf_program pass_filter;/* Filter program for packets to pass */
154 struct	bpf_program active_filter; /* Filter program for link-active pkts */
155 pcap_t  pc;			/* Fake struct pcap so we can compile expr */
156 #endif
157 
158 /*
159  * Prototypes
160  */
161 static int setdevname(char *, int);
162 static int setipaddr(char *);
163 static int setspeed(char *);
164 static int setdebug(char **);
165 static int setkdebug(char **);
166 static int setpassive(char **);
167 static int setsilent(char **);
168 static int noopt(char **);
169 static int setnovj(char **);
170 static int setnovjccomp(char **);
171 static int setvjslots(char **);
172 static int reqpap(char **);
173 static int nopap(char **);
174 static int nochap(char **);
175 static int reqchap(char **);
176 static int noaccomp(char **);
177 static int noasyncmap(char **);
178 static int noip(char **);
179 static int nomagicnumber(char **);
180 static int setasyncmap(char **);
181 static int setescape(char **);
182 static int setmru(char **);
183 static int setmtu(char **);
184 #ifdef CBCP_SUPPORT
185 static int setcbcp(char **);
186 #endif
187 static int nomru(char **);
188 static int nopcomp(char **);
189 static int setconnector(char **);
190 static int setdisconnector(char **);
191 static int setwelcomer(char **);
192 static int setmaxconnect(char **);
193 static int setdomain(char **);
194 static int setnetmask(char **);
195 static int setcrtscts(char **);
196 static int setnocrtscts(char **);
197 static int setxonxoff(char **);
198 static int setnodetach(char **);
199 static int setupdetach(char **);
200 static int setmodem(char **);
201 static int setmodem_chat(char **);
202 static int setlocal(char **);
203 static int setlock(char **);
204 static int setname(char **);
205 static int setuser(char **);
206 static int setremote(char **);
207 static int setauth(char **);
208 static int setnoauth(char **);
209 static int readfile(char **);
210 static int callfile(char **);
211 static int setdefaultroute(char **);
212 static int setnodefaultroute(char **);
213 static int setproxyarp(char **);
214 static int setnoproxyarp(char **);
215 static int setpersist(char **);
216 static int setnopersist(char **);
217 static int setdologin(char **);
218 static int setusehostname(char **);
219 static int setnoipdflt(char **);
220 static int setlcptimeout(char **);
221 static int setlcpterm(char **);
222 static int setlcpconf(char **);
223 static int setlcpfails(char **);
224 static int setipcptimeout(char **);
225 static int setipcpterm(char **);
226 static int setipcpconf(char **);
227 static int setipcpfails(char **);
228 static int setpaptimeout(char **);
229 static int setpapreqs(char **);
230 static int setpapreqtime(char **);
231 static int setchaptimeout(char **);
232 static int setchapchal(char **);
233 static int setchapintv(char **);
234 static int setipcpaccl(char **);
235 static int setipcpaccr(char **);
236 static int setlcpechointv(char **);
237 static int setlcpechofails(char **);
238 static int noccp(char **);
239 static int setbsdcomp(char **);
240 static int setnobsdcomp(char **);
241 static int setdeflate(char **);
242 static int setnodeflate(char **);
243 static int setnodeflatedraft(char **);
244 static int setdemand(char **);
245 static int setpred1comp(char **);
246 static int setnopred1comp(char **);
247 static int setipparam(char **);
248 static int setpapcrypt(char **);
249 static int setidle(char **);
250 static int setholdoff(char **);
251 static int setdnsaddr(char **);
252 static int setwinsaddr(char **);
253 static int showversion(char **);
254 static int showhelp(char **);
255 
256 #ifdef PPP_FILTER
257 static int setpdebug(char **);
258 static int setpassfilter(char **);
259 static int setactivefilter(char **);
260 #endif
261 
262 #ifdef MSLANMAN
263 static int setmslanman(char **);
264 #endif
265 
266 static int number_option(char *, u_int32_t *, int);
267 static int int_option(char *, int *);
268 static int readable(int fd);
269 
270 /*
271  * Valid arguments.
272  */
273 static struct cmd {
274     char *cmd_name;
275     int num_args;
276     int (*cmd_func)(char **);
277 } cmds[] = {
278     {"-all", 0, noopt},		/* Don't request/allow any options (useless) */
279     {"noaccomp", 0, noaccomp},	/* Disable Address/Control compression */
280     {"-ac", 0, noaccomp},	/* Disable Address/Control compress */
281     {"default-asyncmap", 0, noasyncmap}, /* Disable asyncmap negoatiation */
282     {"-am", 0, noasyncmap},	/* Disable asyncmap negotiation */
283     {"-as", 1, setasyncmap},	/* set the desired async map */
284     {"-d", 0, setdebug},	/* Increase debugging level */
285     {"nodetach", 0, setnodetach}, /* Don't detach from controlling tty */
286     {"-detach", 0, setnodetach}, /* don't fork */
287     {"updetach", 0, setupdetach}, /* Detach once an NP has come up */
288     {"noip", 0, noip},		/* Disable IP and IPCP */
289     {"-ip", 0, noip},		/* Disable IP and IPCP */
290     {"nomagic", 0, nomagicnumber}, /* Disable magic number negotiation */
291     {"-mn", 0, nomagicnumber},	/* Disable magic number negotiation */
292     {"default-mru", 0, nomru},	/* Disable MRU negotiation */
293     {"-mru", 0, nomru},		/* Disable mru negotiation */
294     {"-p", 0, setpassive},	/* Set passive mode */
295     {"nopcomp", 0, nopcomp},	/* Disable protocol field compression */
296     {"-pc", 0, nopcomp},	/* Disable protocol field compress */
297     {"require-pap", 0, reqpap},	/* Require PAP authentication from peer */
298     {"+pap", 0, reqpap},	/* Require PAP auth from peer */
299     {"refuse-pap", 0, nopap},	/* Don't agree to auth to peer with PAP */
300     {"-pap", 0, nopap},		/* Don't allow UPAP authentication with peer */
301     {"require-chap", 0, reqchap}, /* Require CHAP authentication from peer */
302     {"+chap", 0, reqchap},	/* Require CHAP authentication from peer */
303     {"refuse-chap", 0, nochap},	/* Don't agree to auth to peer with CHAP */
304     {"-chap", 0, nochap},	/* Don't allow CHAP authentication with peer */
305     {"novj", 0, setnovj},	/* Disable VJ compression */
306     {"-vj", 0, setnovj},	/* disable VJ compression */
307     {"novjccomp", 0, setnovjccomp}, /* disable VJ connection-ID compression */
308     {"-vjccomp", 0, setnovjccomp}, /* disable VJ connection-ID compression */
309     {"vj-max-slots", 1, setvjslots}, /* Set maximum VJ header slots */
310     {"asyncmap", 1, setasyncmap}, /* set the desired async map */
311     {"escape", 1, setescape},	/* set chars to escape on transmission */
312     {"connect", 1, setconnector}, /* A program to set up a connection */
313     {"disconnect", 1, setdisconnector},	/* program to disconnect serial dev. */
314     {"welcome", 1, setwelcomer},/* Script to welcome client */
315     {"maxconnect", 1, setmaxconnect},  /* specify a maximum connect time */
316     {"crtscts", 0, setcrtscts},	/* set h/w flow control */
317     {"nocrtscts", 0, setnocrtscts}, /* clear h/w flow control */
318     {"-crtscts", 0, setnocrtscts}, /* clear h/w flow control */
319     {"xonxoff", 0, setxonxoff},	/* set s/w flow control */
320     {"debug", 0, setdebug},	/* Increase debugging level */
321     {"kdebug", 1, setkdebug},	/* Enable kernel-level debugging */
322     {"domain", 1, setdomain},	/* Add given domain name to hostname*/
323     {"mru", 1, setmru},		/* Set MRU value for negotiation */
324     {"mtu", 1, setmtu},		/* Set our MTU */
325 #ifdef CBCP_SUPPORT
326     {"callback", 1, setcbcp},	/* Ask for callback */
327 #endif
328     {"netmask", 1, setnetmask},	/* set netmask */
329     {"passive", 0, setpassive},	/* Set passive mode */
330     {"silent", 0, setsilent},	/* Set silent mode */
331     {"modem", 0, setmodem},	/* Use modem control lines */
332     {"modem_chat", 0, setmodem_chat}, /* Use modem control lines during chat */
333     {"local", 0, setlocal},	/* Don't use modem control lines */
334     {"lock", 0, setlock},	/* Lock serial device (with lock file) */
335     {"name", 1, setname},	/* Set local name for authentication */
336     {"user", 1, setuser},	/* Set name for auth with peer */
337     {"usehostname", 0, setusehostname},	/* Must use hostname for auth. */
338     {"remotename", 1, setremote}, /* Set remote name for authentication */
339     {"auth", 0, setauth},	/* Require authentication from peer */
340     {"noauth", 0, setnoauth},	/* Don't require peer to authenticate */
341     {"file", 1, readfile},	/* Take options from a file */
342     {"call", 1, callfile},	/* Take options from a privileged file */
343     {"defaultroute", 0, setdefaultroute}, /* Add default route */
344     {"nodefaultroute", 0, setnodefaultroute}, /* disable defaultroute option */
345     {"-defaultroute", 0, setnodefaultroute}, /* disable defaultroute option */
346     {"proxyarp", 0, setproxyarp}, /* Add proxy ARP entry */
347     {"noproxyarp", 0, setnoproxyarp}, /* disable proxyarp option */
348     {"-proxyarp", 0, setnoproxyarp}, /* disable proxyarp option */
349     {"persist", 0, setpersist},	/* Keep on reopening connection after close */
350     {"nopersist", 0, setnopersist},  /* Turn off persist option */
351     {"demand", 0, setdemand},	/* Dial on demand */
352     {"login", 0, setdologin},	/* Use system password database for UPAP */
353     {"noipdefault", 0, setnoipdflt}, /* Don't use name for default IP adrs */
354     {"lcp-echo-failure", 1, setlcpechofails}, /* consecutive echo failures */
355     {"lcp-echo-interval", 1, setlcpechointv}, /* time for lcp echo events */
356     {"lcp-restart", 1, setlcptimeout}, /* Set timeout for LCP */
357     {"lcp-max-terminate", 1, setlcpterm}, /* Set max #xmits for term-reqs */
358     {"lcp-max-configure", 1, setlcpconf}, /* Set max #xmits for conf-reqs */
359     {"lcp-max-failure", 1, setlcpfails}, /* Set max #conf-naks for LCP */
360     {"ipcp-restart", 1, setipcptimeout}, /* Set timeout for IPCP */
361     {"ipcp-max-terminate", 1, setipcpterm}, /* Set max #xmits for term-reqs */
362     {"ipcp-max-configure", 1, setipcpconf}, /* Set max #xmits for conf-reqs */
363     {"ipcp-max-failure", 1, setipcpfails}, /* Set max #conf-naks for IPCP */
364     {"pap-restart", 1, setpaptimeout},	/* Set retransmit timeout for PAP */
365     {"pap-max-authreq", 1, setpapreqs}, /* Set max #xmits for auth-reqs */
366     {"pap-timeout", 1, setpapreqtime},	/* Set time limit for peer PAP auth. */
367     {"chap-restart", 1, setchaptimeout}, /* Set timeout for CHAP */
368     {"chap-max-challenge", 1, setchapchal}, /* Set max #xmits for challenge */
369     {"chap-interval", 1, setchapintv}, /* Set interval for rechallenge */
370     {"ipcp-accept-local", 0, setipcpaccl}, /* Accept peer's address for us */
371     {"ipcp-accept-remote", 0, setipcpaccr}, /* Accept peer's address for it */
372     {"noccp", 0, noccp},		/* Disable CCP negotiation */
373     {"-ccp", 0, noccp},			/* Disable CCP negotiation */
374     {"bsdcomp", 1, setbsdcomp},		/* request BSD-Compress */
375     {"nobsdcomp", 0, setnobsdcomp},	/* don't allow BSD-Compress */
376     {"-bsdcomp", 0, setnobsdcomp},	/* don't allow BSD-Compress */
377     {"deflate", 1, setdeflate},		/* request Deflate compression */
378     {"nodeflate", 0, setnodeflate},	/* don't allow Deflate compression */
379     {"-deflate", 0, setnodeflate},	/* don't allow Deflate compression */
380     {"nodeflatedraft", 0, setnodeflatedraft}, /* don't use draft deflate # */
381     {"predictor1", 0, setpred1comp},	/* request Predictor-1 */
382     {"nopredictor1", 0, setnopred1comp},/* don't allow Predictor-1 */
383     {"-predictor1", 0, setnopred1comp},	/* don't allow Predictor-1 */
384     {"ipparam", 1, setipparam},		/* set ip script parameter */
385     {"papcrypt", 0, setpapcrypt},	/* PAP passwords encrypted */
386     {"idle", 1, setidle},		/* idle time limit (seconds) */
387     {"holdoff", 1, setholdoff},		/* set holdoff time (seconds) */
388     {"ms-dns", 1, setdnsaddr},		/* DNS address for the peer's use */
389     {"ms-wins", 1, setwinsaddr},	/* Nameserver for SMB over TCP/IP for peer */
390     {"--version", 0, showversion},	/* Show version number */
391     {"--help", 0, showhelp},		/* Show brief listing of options */
392     {"-h", 0, showhelp},		/* ditto */
393 
394 #ifdef PPP_FILTER
395     {"pdebug", 1, setpdebug},		/* libpcap debugging */
396     {"pass-filter", 1, setpassfilter},	/* set filter for packets to pass */
397     {"active-filter", 1, setactivefilter}, /* set filter for active pkts */
398 #endif
399 
400 #ifdef MSLANMAN
401     {"ms-lanman", 0, setmslanman},	/* Use LanMan psswd when using MS-CHAP */
402 #endif
403 
404     {NULL, 0, NULL}
405 };
406 
407 
408 #ifndef IMPLEMENTATION
409 #define IMPLEMENTATION ""
410 #endif
411 
412 static char *usage_string = "\
413 pppd version %s patch level %d%s\n\
414 Usage: %s [ options ], where options are:\n\
415 	<device>	Communicate over the named device\n\
416 	<speed>		Set the baud rate to <speed>\n\
417 	<loc>:<rem>	Set the local and/or remote interface IP\n\
418 			addresses.  Either one may be omitted.\n\
419 	asyncmap <n>	Set the desired async map to hex <n>\n\
420 	auth		Require authentication from peer\n\
421         connect <p>     Invoke shell command <p> to set up the serial line\n\
422 	crtscts		Use hardware RTS/CTS flow control\n\
423 	defaultroute	Add default route through interface\n\
424 	file <f>	Take options from file <f>\n\
425 	modem		Use modem control lines\n\
426 	modem_chat	Use modem control lines during chat\n\
427 	mru <n>		Set MRU value to <n> for negotiation\n\
428 	netmask <n>	Set interface netmask to <n>\n\
429 See pppd(8) for more options.\n\
430 ";
431 
432 static char *current_option;	/* the name of the option being parsed */
433 static int privileged_option;	/* set iff the current option came from root */
434 static char *option_source;	/* string saying where the option came from */
435 
436 /*
437  * parse_args - parse a string of arguments from the command line.
438  */
439 int
440 parse_args(argc, argv)
441     int argc;
442     char **argv;
443 {
444     char *arg;
445     struct cmd *cmdp;
446     int ret;
447 
448     privileged_option = privileged;
449     option_source = "command line";
450     while (argc > 0) {
451 	arg = *argv++;
452 	--argc;
453 
454 	/*
455 	 * First see if it's a command.
456 	 */
457 	for (cmdp = cmds; cmdp->cmd_name; cmdp++)
458 	    if (!strcmp(arg, cmdp->cmd_name))
459 		break;
460 
461 	if (cmdp->cmd_name != NULL) {
462 	    if (argc < cmdp->num_args) {
463 		option_error("too few parameters for option %s", arg);
464 		return 0;
465 	    }
466 	    current_option = arg;
467 	    if (!(*cmdp->cmd_func)(argv))
468 		return 0;
469 	    argc -= cmdp->num_args;
470 	    argv += cmdp->num_args;
471 
472 	} else {
473 	    /*
474 	     * Maybe a tty name, speed or IP address?
475 	     */
476 	    if ((ret = setdevname(arg, 0)) == 0
477 		&& (ret = setspeed(arg)) == 0
478 		&& (ret = setipaddr(arg)) == 0) {
479 		option_error("unrecognized option '%s'", arg);
480 		usage();
481 		return 0;
482 	    }
483 	    if (ret < 0)	/* error */
484 		return 0;
485 	}
486     }
487     return 1;
488 }
489 
490 /*
491  * scan_args - scan the command line arguments to get the tty name,
492  * if specified.
493  */
494 void
495 scan_args(argc, argv)
496     int argc;
497     char **argv;
498 {
499     char *arg;
500     struct cmd *cmdp;
501 
502     while (argc > 0) {
503 	arg = *argv++;
504 	--argc;
505 
506 	/* Skip options and their arguments */
507 	for (cmdp = cmds; cmdp->cmd_name; cmdp++)
508 	    if (!strcmp(arg, cmdp->cmd_name))
509 		break;
510 
511 	if (cmdp->cmd_name != NULL) {
512 	    argc -= cmdp->num_args;
513 	    argv += cmdp->num_args;
514 	    continue;
515 	}
516 
517 	/* Check if it's a tty name and copy it if so */
518 	(void) setdevname(arg, 1);
519     }
520 }
521 
522 /*
523  * usage - print out a message telling how to use the program.
524  */
525 void
526 usage()
527 {
528     if (phase == PHASE_INITIALIZE)
529 	fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
530 		progname);
531 }
532 
533 /*
534  * showhelp - print out usage message and exit.
535  */
536 static int
537 showhelp(argv)
538     char **argv;
539 {
540     if (phase == PHASE_INITIALIZE) {
541 	usage();
542 	exit(0);
543     }
544     return 0;
545 }
546 
547 /*
548  * showversion - print out the version number and exit.
549  */
550 static int
551 showversion(argv)
552     char **argv;
553 {
554     if (phase == PHASE_INITIALIZE) {
555 	fprintf(stderr, "pppd version %s patch level %d%s\n",
556 		VERSION, PATCHLEVEL, IMPLEMENTATION);
557 	exit(0);
558     }
559     return 0;
560 }
561 
562 /*
563  * options_from_file - Read a string of options from a file,
564  * and interpret them.
565  */
566 int
567 options_from_file(filename, must_exist, check_prot, priv)
568     char *filename;
569     int must_exist;
570     int check_prot;
571     int priv;
572 {
573     FILE *f;
574     int i, newline, ret;
575     struct cmd *cmdp;
576     int oldpriv;
577     char *argv[MAXARGS];
578     char args[MAXARGS][MAXWORDLEN];
579     char cmd[MAXWORDLEN];
580 
581     if ((f = fopen(filename, "r")) == NULL) {
582 	if (!must_exist && errno == ENOENT)
583 	    return 1;
584 	option_error("Can't open options file %s: %m", filename);
585 	return 0;
586     }
587     if (check_prot && !readable(fileno(f))) {
588 	option_error("Can't open options file %s: access denied", filename);
589 	fclose(f);
590 	return 0;
591     }
592 
593     oldpriv = privileged_option;
594     privileged_option = priv;
595     ret = 0;
596     while (getword(f, cmd, &newline, filename)) {
597 	/*
598 	 * First see if it's a command.
599 	 */
600 	for (cmdp = cmds; cmdp->cmd_name; cmdp++)
601 	    if (!strcmp(cmd, cmdp->cmd_name))
602 		break;
603 
604 	if (cmdp->cmd_name != NULL) {
605 	    for (i = 0; i < cmdp->num_args; ++i) {
606 		if (!getword(f, args[i], &newline, filename)) {
607 		    option_error(
608 			"In file %s: too few parameters for option '%s'",
609 			filename, cmd);
610 		    goto err;
611 		}
612 		argv[i] = args[i];
613 	    }
614 	    current_option = cmd;
615 	    if (!(*cmdp->cmd_func)(argv))
616 		goto err;
617 
618 	} else {
619 	    /*
620 	     * Maybe a tty name, speed or IP address?
621 	     */
622 	    if ((i = setdevname(cmd, 0)) == 0
623 		&& (i = setspeed(cmd)) == 0
624 		&& (i = setipaddr(cmd)) == 0) {
625 		option_error("In file %s: unrecognized option '%s'",
626 			     filename, cmd);
627 		goto err;
628 	    }
629 	    if (i < 0)		/* error */
630 		goto err;
631 	}
632     }
633     ret = 1;
634 
635 err:
636     fclose(f);
637     privileged_option = oldpriv;
638     return ret;
639 }
640 
641 /*
642  * options_from_user - See if the use has a ~/.ppprc file,
643  * and if so, interpret options from it.
644  */
645 int
646 options_from_user()
647 {
648     char *user, *path, *file;
649     int ret;
650     struct passwd *pw;
651 
652     pw = getpwuid(getuid());
653     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
654 	return 1;
655     file = _PATH_USEROPT;
656     if (asprintf(&path, "%s/%s", user, file) == -1)
657 	novm("init file name");
658     ret = options_from_file(path, 0, 1, privileged);
659     free(path);
660     return ret;
661 }
662 
663 /*
664  * options_for_tty - See if an options file exists for the serial
665  * device, and if so, interpret options from it.
666  */
667 int
668 options_for_tty()
669 {
670     char *dev, *path;
671     int ret;
672 
673     dev = devnam;
674     if (strncmp(dev, "/dev/", 5) == 0)
675 	dev += 5;
676     if (strcmp(dev, "tty") == 0)
677 	return 1;		/* don't look for /etc/ppp/options.tty */
678     if (asprintf(&path, "%s%s", _PATH_TTYOPT, dev) == -1)
679 	novm("tty init file name");
680     ret = options_from_file(path, 0, 0, 1);
681     free(path);
682     return ret;
683 }
684 
685 /*
686  * option_error - print a message about an error in an option.
687  * The message is logged, and also sent to
688  * stderr if phase == PHASE_INITIALIZE.
689  */
690 void
691 option_error(char *fmt, ...)
692 {
693     va_list args;
694     char buf[256];
695 
696     va_start(args, fmt);
697     vfmtmsg(buf, sizeof(buf), fmt, args);
698     va_end(args);
699     if (phase == PHASE_INITIALIZE)
700 	fprintf(stderr, "%s: %s\n", progname, buf);
701     syslog(LOG_ERR, "%s", buf);
702 }
703 
704 /*
705  * readable - check if a file is readable by the real user.
706  */
707 static int
708 readable(fd)
709     int fd;
710 {
711     uid_t uid;
712     int ngroups, i;
713     struct stat sbuf;
714     GIDSET_TYPE groups[NGROUPS_MAX];
715 
716     uid = getuid();
717     if (uid == 0)
718 	return 1;
719     if (fstat(fd, &sbuf) != 0)
720 	return 0;
721     if (sbuf.st_uid == uid)
722 	return sbuf.st_mode & S_IRUSR;
723     if (sbuf.st_gid == getgid())
724 	return sbuf.st_mode & S_IRGRP;
725     ngroups = getgroups(NGROUPS_MAX, groups);
726     for (i = 0; i < ngroups; ++i)
727 	if (sbuf.st_gid == groups[i])
728 	    return sbuf.st_mode & S_IRGRP;
729     return sbuf.st_mode & S_IROTH;
730 }
731 
732 /*
733  * Read a word from a file.
734  * Words are delimited by white-space or by quotes (" or ').
735  * Quotes, white-space and \ may be escaped with \.
736  * \<newline> is ignored.
737  */
738 int
739 getword(f, word, newlinep, filename)
740     FILE *f;
741     char *word;
742     int *newlinep;
743     char *filename;
744 {
745     int c, len, escape;
746     int quoted, comment;
747     int value, digit, got, n;
748 
749 #define isoctal(c) ((c) >= '0' && (c) < '8')
750 
751     *newlinep = 0;
752     len = 0;
753     escape = 0;
754     comment = 0;
755 
756     /*
757      * First skip white-space and comments.
758      */
759     for (;;) {
760 	c = getc(f);
761 	if (c == EOF)
762 	    break;
763 
764 	/*
765 	 * A newline means the end of a comment; backslash-newline
766 	 * is ignored.  Note that we cannot have escape && comment.
767 	 */
768 	if (c == '\n') {
769 	    if (!escape) {
770 		*newlinep = 1;
771 		comment = 0;
772 	    } else
773 		escape = 0;
774 	    continue;
775 	}
776 
777 	/*
778 	 * Ignore characters other than newline in a comment.
779 	 */
780 	if (comment)
781 	    continue;
782 
783 	/*
784 	 * If this character is escaped, we have a word start.
785 	 */
786 	if (escape)
787 	    break;
788 
789 	/*
790 	 * If this is the escape character, look at the next character.
791 	 */
792 	if (c == '\\') {
793 	    escape = 1;
794 	    continue;
795 	}
796 
797 	/*
798 	 * If this is the start of a comment, ignore the rest of the line.
799 	 */
800 	if (c == '#') {
801 	    comment = 1;
802 	    continue;
803 	}
804 
805 	/*
806 	 * A non-whitespace character is the start of a word.
807 	 */
808 	if (!isspace(c))
809 	    break;
810     }
811 
812     /*
813      * Save the delimiter for quoted strings.
814      */
815     if (!escape && (c == '"' || c == '\'')) {
816         quoted = c;
817 	c = getc(f);
818     } else
819         quoted = 0;
820 
821     /*
822      * Process characters until the end of the word.
823      */
824     while (c != EOF) {
825 	if (escape) {
826 	    /*
827 	     * This character is escaped: backslash-newline is ignored,
828 	     * various other characters indicate particular values
829 	     * as for C backslash-escapes.
830 	     */
831 	    escape = 0;
832 	    if (c == '\n') {
833 	        c = getc(f);
834 		continue;
835 	    }
836 
837 	    got = 0;
838 	    switch (c) {
839 	    case 'a':
840 		value = '\a';
841 		break;
842 	    case 'b':
843 		value = '\b';
844 		break;
845 	    case 'f':
846 		value = '\f';
847 		break;
848 	    case 'n':
849 		value = '\n';
850 		break;
851 	    case 'r':
852 		value = '\r';
853 		break;
854 	    case 's':
855 		value = ' ';
856 		break;
857 	    case 't':
858 		value = '\t';
859 		break;
860 
861 	    default:
862 		if (isoctal(c)) {
863 		    /*
864 		     * \ddd octal sequence
865 		     */
866 		    value = 0;
867 		    for (n = 0; n < 3 && isoctal(c); ++n) {
868 			value = (value << 3) + (c & 07);
869 			c = getc(f);
870 		    }
871 		    got = 1;
872 		    break;
873 		}
874 
875 		if (c == 'x') {
876 		    /*
877 		     * \x<hex_string> sequence
878 		     */
879 		    value = 0;
880 		    c = getc(f);
881 		    for (n = 0; n < 2 && isxdigit(c); ++n) {
882 			digit = toupper(c) - '0';
883 			if (digit > 10)
884 			    digit += '0' + 10 - 'A';
885 			value = (value << 4) + digit;
886 			c = getc (f);
887 		    }
888 		    got = 1;
889 		    break;
890 		}
891 
892 		/*
893 		 * Otherwise the character stands for itself.
894 		 */
895 		value = c;
896 		break;
897 	    }
898 
899 	    /*
900 	     * Store the resulting character for the escape sequence.
901 	     */
902 	    if (len < MAXWORDLEN-1)
903 		word[len] = value;
904 	    ++len;
905 
906 	    if (!got)
907 		c = getc(f);
908 	    continue;
909 
910 	}
911 
912 	/*
913 	 * Not escaped: see if we've reached the end of the word.
914 	 */
915 	if (quoted) {
916 	    if (c == quoted)
917 		break;
918 	} else {
919 	    if (isspace(c) || c == '#') {
920 		ungetc (c, f);
921 		break;
922 	    }
923 	}
924 
925 	/*
926 	 * Backslash starts an escape sequence.
927 	 */
928 	if (c == '\\') {
929 	    escape = 1;
930 	    c = getc(f);
931 	    continue;
932 	}
933 
934 	/*
935 	 * An ordinary character: store it in the word and get another.
936 	 */
937 	if (len < MAXWORDLEN-1)
938 	    word[len] = c;
939 	++len;
940 
941 	c = getc(f);
942     }
943 
944     /*
945      * End of the word: check for errors.
946      */
947     if (c == EOF) {
948 	if (ferror(f)) {
949 	    if (errno == 0)
950 		errno = EIO;
951 	    option_error("Error reading %s: %m", filename);
952 	    die(1);
953 	}
954 	/*
955 	 * If len is zero, then we didn't find a word before the
956 	 * end of the file.
957 	 */
958 	if (len == 0)
959 	    return 0;
960     }
961 
962     /*
963      * Warn if the word was too long, and append a terminating null.
964      */
965     if (len >= MAXWORDLEN) {
966 	option_error("warning: word in file %s too long (%.20s...)",
967 		     filename, word);
968 	len = MAXWORDLEN - 1;
969     }
970     word[len] = 0;
971 
972     return 1;
973 
974 #undef isoctal
975 
976 }
977 
978 /*
979  * number_option - parse an unsigned numeric parameter for an option.
980  */
981 static int
982 number_option(str, valp, base)
983     char *str;
984     u_int32_t *valp;
985     int base;
986 {
987     char *ptr;
988 
989     *valp = strtoul(str, &ptr, base);
990     if (ptr == str) {
991 	option_error("invalid numeric parameter '%s' for %s option",
992 		     str, current_option);
993 	return 0;
994     }
995     return 1;
996 }
997 
998 
999 /*
1000  * int_option - like number_option, but valp is int *,
1001  * the base is assumed to be 0, and *valp is not changed
1002  * if there is an error.
1003  */
1004 static int
1005 int_option(str, valp)
1006     char *str;
1007     int *valp;
1008 {
1009     u_int32_t v;
1010 
1011     if (!number_option(str, &v, 0))
1012 	return 0;
1013     *valp = (int) v;
1014     return 1;
1015 }
1016 
1017 
1018 /*
1019  * The following procedures parse options.
1020  */
1021 
1022 /*
1023  * readfile - take commands from a file.
1024  */
1025 static int
1026 readfile(argv)
1027     char **argv;
1028 {
1029     return options_from_file(*argv, 1, 1, privileged_option);
1030 }
1031 
1032 /*
1033  * callfile - take commands from /etc/ppp/peers/<name>.
1034  * Name may not contain /../, start with / or ../, or end in /..
1035  */
1036 static int
1037 callfile(argv)
1038     char **argv;
1039 {
1040     char *fname, *arg, *p;
1041     int l, ok;
1042 
1043     arg = *argv;
1044     ok = 1;
1045     if (arg[0] == '/' || arg[0] == 0)
1046 	ok = 0;
1047     else {
1048 	for (p = arg; *p != 0; ) {
1049 	    if (p[0] == '.' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
1050 		ok = 0;
1051 		break;
1052 	    }
1053 	    while (*p != '/' && *p != 0)
1054 		++p;
1055 	    if (*p == '/')
1056 		++p;
1057 	}
1058     }
1059     if (!ok) {
1060 	option_error("call option value may not contain .. or start with /");
1061 	return 0;
1062     }
1063 
1064     l = strlen(arg) + strlen(_PATH_PEERFILES) + 1;
1065     if ((fname = (char *) malloc(l)) == NULL)
1066 	novm("call file name");
1067     strlcpy(fname, _PATH_PEERFILES, l);
1068     strlcat(fname, arg, l);
1069 
1070     ok = options_from_file(fname, 1, 1, 1);
1071 
1072     free(fname);
1073     return ok;
1074 }
1075 
1076 
1077 /*
1078  * setdebug - Set debug (command line argument).
1079  */
1080 static int
1081 setdebug(argv)
1082     char **argv;
1083 {
1084     debug++;
1085     return (1);
1086 }
1087 
1088 /*
1089  * setkdebug - Set kernel debugging level.
1090  */
1091 static int
1092 setkdebug(argv)
1093     char **argv;
1094 {
1095     return int_option(*argv, &kdebugflag);
1096 }
1097 
1098 #ifdef PPP_FILTER
1099 /*
1100  * setpdebug - Set libpcap debugging level.
1101  */
1102 static int
1103 setpdebug(argv)
1104     char **argv;
1105 {
1106     return int_option(*argv, &dflag);
1107 }
1108 
1109 /*
1110  * setpassfilter - Set the pass filter for packets
1111  */
1112 static int
1113 setpassfilter(argv)
1114     char **argv;
1115 {
1116     pc.linktype = DLT_PPP;
1117     pc.snapshot = PPP_HDRLEN;
1118 
1119     if (pcap_compile(&pc, &pass_filter, *argv, 1, netmask) == 0)
1120 	return 1;
1121     option_error("error in pass-filter expression: %s\n", pcap_geterr(&pc));
1122     return 0;
1123 }
1124 
1125 /*
1126  * setactivefilter - Set the active filter for packets
1127  */
1128 static int
1129 setactivefilter(argv)
1130     char **argv;
1131 {
1132     pc.linktype = DLT_PPP;
1133     pc.snapshot = PPP_HDRLEN;
1134 
1135     if (pcap_compile(&pc, &active_filter, *argv, 1, netmask) == 0)
1136 	return 1;
1137     option_error("error in active-filter expression: %s\n", pcap_geterr(&pc));
1138     return 0;
1139 }
1140 #endif
1141 
1142 /*
1143  * noopt - Disable all options.
1144  */
1145 static int
1146 noopt(argv)
1147     char **argv;
1148 {
1149     BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
1150     BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
1151     BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
1152     BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
1153 
1154     return (1);
1155 }
1156 
1157 /*
1158  * noaccomp - Disable Address/Control field compression negotiation.
1159  */
1160 static int
1161 noaccomp(argv)
1162     char **argv;
1163 {
1164     lcp_wantoptions[0].neg_accompression = 0;
1165     lcp_allowoptions[0].neg_accompression = 0;
1166     return (1);
1167 }
1168 
1169 
1170 /*
1171  * noasyncmap - Disable async map negotiation.
1172  */
1173 static int
1174 noasyncmap(argv)
1175     char **argv;
1176 {
1177     lcp_wantoptions[0].neg_asyncmap = 0;
1178     lcp_allowoptions[0].neg_asyncmap = 0;
1179     return (1);
1180 }
1181 
1182 
1183 /*
1184  * noip - Disable IP and IPCP.
1185  */
1186 static int
1187 noip(argv)
1188     char **argv;
1189 {
1190     ipcp_protent.enabled_flag = 0;
1191     return (1);
1192 }
1193 
1194 
1195 /*
1196  * nomagicnumber - Disable magic number negotiation.
1197  */
1198 static int
1199 nomagicnumber(argv)
1200     char **argv;
1201 {
1202     lcp_wantoptions[0].neg_magicnumber = 0;
1203     lcp_allowoptions[0].neg_magicnumber = 0;
1204     return (1);
1205 }
1206 
1207 
1208 /*
1209  * nomru - Disable mru negotiation.
1210  */
1211 static int
1212 nomru(argv)
1213     char **argv;
1214 {
1215     lcp_wantoptions[0].neg_mru = 0;
1216     lcp_allowoptions[0].neg_mru = 0;
1217     return (1);
1218 }
1219 
1220 
1221 /*
1222  * setmru - Set MRU for negotiation.
1223  */
1224 static int
1225 setmru(argv)
1226     char **argv;
1227 {
1228     u_int32_t mru;
1229 
1230     if (!number_option(*argv, &mru, 0))
1231 	return 0;
1232     lcp_wantoptions[0].mru = mru;
1233     lcp_wantoptions[0].neg_mru = 1;
1234     return (1);
1235 }
1236 
1237 
1238 /*
1239  * setmru - Set the largest MTU we'll use.
1240  */
1241 static int
1242 setmtu(argv)
1243     char **argv;
1244 {
1245     u_int32_t mtu;
1246 
1247     if (!number_option(*argv, &mtu, 0))
1248 	return 0;
1249     if (mtu < MINMRU || mtu > MAXMRU) {
1250 	option_error("mtu option value of %u is too %s", mtu,
1251 		     (mtu < MINMRU? "small": "large"));
1252 	return 0;
1253     }
1254     lcp_allowoptions[0].mru = mtu;
1255     return (1);
1256 }
1257 
1258 #ifdef CBCP_SUPPORT
1259 static int
1260 setcbcp(argv)
1261     char **argv;
1262 {
1263     lcp_wantoptions[0].neg_cbcp = 1;
1264     cbcp_protent.enabled_flag = 1;
1265     cbcp[0].us_number = strdup(*argv);
1266     if (cbcp[0].us_number == 0)
1267 	novm("callback number");
1268     cbcp[0].us_type |= (1 << CB_CONF_USER);
1269     cbcp[0].us_type |= (1 << CB_CONF_ADMIN);
1270     return (1);
1271 }
1272 #endif
1273 
1274 /*
1275  * nopcomp - Disable Protocol field compression negotiation.
1276  */
1277 static int
1278 nopcomp(argv)
1279     char **argv;
1280 {
1281     lcp_wantoptions[0].neg_pcompression = 0;
1282     lcp_allowoptions[0].neg_pcompression = 0;
1283     return (1);
1284 }
1285 
1286 
1287 /*
1288  * setpassive - Set passive mode (don't give up if we time out sending
1289  * LCP configure-requests).
1290  */
1291 static int
1292 setpassive(argv)
1293     char **argv;
1294 {
1295     lcp_wantoptions[0].passive = 1;
1296     return (1);
1297 }
1298 
1299 
1300 /*
1301  * setsilent - Set silent mode (don't start sending LCP configure-requests
1302  * until we get one from the peer).
1303  */
1304 static int
1305 setsilent(argv)
1306     char **argv;
1307 {
1308     lcp_wantoptions[0].silent = 1;
1309     return 1;
1310 }
1311 
1312 
1313 /*
1314  * nopap - Disable PAP authentication with peer.
1315  */
1316 static int
1317 nopap(argv)
1318     char **argv;
1319 {
1320     refuse_pap = 1;
1321     return (1);
1322 }
1323 
1324 
1325 /*
1326  * reqpap - Require PAP authentication from peer.
1327  */
1328 static int
1329 reqpap(argv)
1330     char **argv;
1331 {
1332     lcp_wantoptions[0].neg_upap = 1;
1333     setauth(NULL);
1334     return 1;
1335 }
1336 
1337 /*
1338  * nochap - Disable CHAP authentication with peer.
1339  */
1340 static int
1341 nochap(argv)
1342     char **argv;
1343 {
1344     refuse_chap = 1;
1345     return (1);
1346 }
1347 
1348 
1349 /*
1350  * reqchap - Require CHAP authentication from peer.
1351  */
1352 static int
1353 reqchap(argv)
1354     char **argv;
1355 {
1356     lcp_wantoptions[0].neg_chap = 1;
1357     setauth(NULL);
1358     return (1);
1359 }
1360 
1361 
1362 /*
1363  * setnovj - disable vj compression
1364  */
1365 static int
1366 setnovj(argv)
1367     char **argv;
1368 {
1369     ipcp_wantoptions[0].neg_vj = 0;
1370     ipcp_allowoptions[0].neg_vj = 0;
1371     return (1);
1372 }
1373 
1374 
1375 /*
1376  * setnovjccomp - disable VJ connection-ID compression
1377  */
1378 static int
1379 setnovjccomp(argv)
1380     char **argv;
1381 {
1382     ipcp_wantoptions[0].cflag = 0;
1383     ipcp_allowoptions[0].cflag = 0;
1384     return 1;
1385 }
1386 
1387 
1388 /*
1389  * setvjslots - set maximum number of connection slots for VJ compression
1390  */
1391 static int
1392 setvjslots(argv)
1393     char **argv;
1394 {
1395     int value;
1396 
1397     if (!int_option(*argv, &value))
1398 	return 0;
1399     if (value < 2 || value > 16) {
1400 	option_error("vj-max-slots value must be between 2 and 16");
1401 	return 0;
1402     }
1403     ipcp_wantoptions [0].maxslotindex =
1404         ipcp_allowoptions[0].maxslotindex = value - 1;
1405     return 1;
1406 }
1407 
1408 
1409 /*
1410  * setconnector - Set a program to connect to a serial line
1411  */
1412 static int
1413 setconnector(argv)
1414     char **argv;
1415 {
1416     connector = strdup(*argv);
1417     if (connector == NULL)
1418 	novm("connect script");
1419     connector_info.priv = privileged_option;
1420     connector_info.source = option_source;
1421 
1422     return (1);
1423 }
1424 
1425 /*
1426  * setdisconnector - Set a program to disconnect from the serial line
1427  */
1428 static int
1429 setdisconnector(argv)
1430     char **argv;
1431 {
1432     disconnector = strdup(*argv);
1433     if (disconnector == NULL)
1434 	novm("disconnect script");
1435     disconnector_info.priv = privileged_option;
1436     disconnector_info.source = option_source;
1437 
1438     return (1);
1439 }
1440 
1441 /*
1442  * setwelcomer - Set a program to welcome a client after connection
1443  */
1444 static int
1445 setwelcomer(argv)
1446     char **argv;
1447 {
1448     welcomer = strdup(*argv);
1449     if (welcomer == NULL)
1450 	novm("welcome script");
1451     welcomer_info.priv = privileged_option;
1452     welcomer_info.source = option_source;
1453 
1454     return (1);
1455 }
1456 
1457 /*
1458  * setmaxconnect - Set the maximum connect time
1459  */
1460 static int
1461 setmaxconnect(argv)
1462     char **argv;
1463 {
1464     int value;
1465 
1466     if (!int_option(*argv, &value))
1467 	return 0;
1468     if (value < 0) {
1469 	option_error("maxconnect time must be positive");
1470 	return 0;
1471     }
1472     if (maxconnect > 0 && (value == 0 || value > maxconnect)) {
1473 	option_error("maxconnect time cannot be increased");
1474 	return 0;
1475     }
1476     maxconnect = value;
1477     return 1;
1478 }
1479 
1480 /*
1481  * setdomain - Set domain name to append to hostname
1482  */
1483 static int
1484 setdomain(argv)
1485     char **argv;
1486 {
1487     if (!privileged_option) {
1488 	option_error("using the domain option requires root privilege");
1489 	return 0;
1490     }
1491     gethostname(hostname, MAXNAMELEN);
1492     if (**argv != 0) {
1493 	if (**argv != '.')
1494 	    strlcat(hostname, ".", MAXNAMELEN);
1495 	strlcat(hostname, *argv, MAXNAMELEN);
1496     }
1497     hostname[MAXNAMELEN-1] = 0;
1498     return (1);
1499 }
1500 
1501 
1502 /*
1503  * setasyncmap - add bits to asyncmap (what we request peer to escape).
1504  */
1505 static int
1506 setasyncmap(argv)
1507     char **argv;
1508 {
1509     u_int32_t asyncmap;
1510 
1511     if (!number_option(*argv, &asyncmap, 16))
1512 	return 0;
1513     lcp_wantoptions[0].asyncmap |= asyncmap;
1514     lcp_wantoptions[0].neg_asyncmap = 1;
1515     return(1);
1516 }
1517 
1518 
1519 /*
1520  * setescape - add chars to the set we escape on transmission.
1521  */
1522 static int
1523 setescape(argv)
1524     char **argv;
1525 {
1526     int n, ret;
1527     char *p, *endp;
1528 
1529     p = *argv;
1530     ret = 1;
1531     while (*p) {
1532 	n = strtol(p, &endp, 16);
1533 	if (p == endp) {
1534 	    option_error("escape parameter contains invalid hex number '%s'",
1535 			 p);
1536 	    return 0;
1537 	}
1538 	p = endp;
1539 	if (n < 0 || (0x20 <= n && n <= 0x3F) || n == 0x5E || n > 0xFF) {
1540 	    option_error("can't escape character 0x%x", n);
1541 	    ret = 0;
1542 	} else
1543 	    xmit_accm[0][n >> 5] |= 1 << (n & 0x1F);
1544 	while (*p == ',' || *p == ' ')
1545 	    ++p;
1546     }
1547     return ret;
1548 }
1549 
1550 
1551 /*
1552  * setspeed - Set the speed.
1553  */
1554 static int
1555 setspeed(arg)
1556     char *arg;
1557 {
1558     char *ptr;
1559     int spd;
1560 
1561     spd = strtol(arg, &ptr, 0);
1562     if (ptr == arg || *ptr != 0 || spd == 0)
1563 	return 0;
1564     inspeed = spd;
1565     return 1;
1566 }
1567 
1568 
1569 /*
1570  * setdevname - Set the device name.
1571  */
1572 static int
1573 setdevname(cp, quiet)
1574     char *cp;
1575     int quiet;
1576 {
1577     struct stat statbuf;
1578     char dev[MAXPATHLEN];
1579 
1580     if (*cp == 0)
1581 	return 0;
1582 
1583     if (strncmp("/dev/", cp, 5) != 0) {
1584 	strlcpy(dev, "/dev/", sizeof dev);
1585 	strlcat(dev, cp, sizeof dev);
1586 	cp = dev;
1587     }
1588 
1589     /*
1590      * Check if there is a device by this name.
1591      */
1592     if (stat(cp, &statbuf) < 0) {
1593 	if (errno == ENOENT || quiet)
1594 	    return 0;
1595 	option_error("Couldn't stat %s: %m", cp);
1596 	return -1;
1597     }
1598 
1599     (void) strlcpy(devnam, cp, MAXPATHLEN);
1600     default_device = FALSE;
1601     devnam_info.priv = privileged_option;
1602     devnam_info.source = option_source;
1603 
1604     return 1;
1605 }
1606 
1607 
1608 /*
1609  * setipaddr - Set the IP address
1610  */
1611 static int
1612 setipaddr(arg)
1613     char *arg;
1614 {
1615     struct hostent *hp;
1616     char *colon;
1617     struct in_addr ina;
1618     u_int32_t local, remote;
1619     ipcp_options *wo = &ipcp_wantoptions[0];
1620 
1621     /*
1622      * IP address pair separated by ":".
1623      */
1624     if ((colon = strchr(arg, ':')) == NULL)
1625 	return 0;
1626 
1627     /*
1628      * If colon first character, then no local addr.
1629      */
1630     if (colon != arg) {
1631 	*colon = '\0';
1632 	if (inet_aton(arg, &ina) == 0) {
1633 	    if ((hp = gethostbyname(arg)) == NULL) {
1634 		option_error("unknown host: %s", arg);
1635 		return -1;
1636 	    } else {
1637 		local = *(u_int32_t *)hp->h_addr;
1638 		if (our_name[0] == 0)
1639 		    strlcpy(our_name, arg, MAXNAMELEN);
1640 	    }
1641 	} else
1642 	    local = ina.s_addr;
1643 	if (bad_ip_adrs(local)) {
1644 	    option_error("bad local IP address %s", ip_ntoa(local));
1645 	    return -1;
1646 	}
1647 	if (local != 0)
1648 	    wo->ouraddr = local;
1649 	*colon = ':';
1650     }
1651 
1652     /*
1653      * If colon last character, then no remote addr.
1654      */
1655     if (*++colon != '\0') {
1656 	if (inet_aton(colon, &ina) == 0) {
1657 	    if ((hp = gethostbyname(colon)) == NULL) {
1658 		option_error("unknown host: %s", colon);
1659 		return -1;
1660 	    } else {
1661 		remote = *(u_int32_t *)hp->h_addr;
1662 		if (remote_name[0] == 0)
1663 		    strlcpy(remote_name, colon, MAXNAMELEN);
1664 	    }
1665 	} else
1666 	    remote = ina.s_addr;
1667 	if (bad_ip_adrs(remote)) {
1668 	    option_error("bad remote IP address %s", ip_ntoa(remote));
1669 	    return -1;
1670 	}
1671 	if (remote != 0)
1672 	    wo->hisaddr = remote;
1673     }
1674 
1675     return 1;
1676 }
1677 
1678 
1679 /*
1680  * setnoipdflt - disable setipdefault()
1681  */
1682 static int
1683 setnoipdflt(argv)
1684     char **argv;
1685 {
1686     disable_defaultip = 1;
1687     return 1;
1688 }
1689 
1690 
1691 /*
1692  * setipcpaccl - accept peer's idea of our address
1693  */
1694 static int
1695 setipcpaccl(argv)
1696     char **argv;
1697 {
1698     ipcp_wantoptions[0].accept_local = 1;
1699     return 1;
1700 }
1701 
1702 
1703 /*
1704  * setipcpaccr - accept peer's idea of its address
1705  */
1706 static int
1707 setipcpaccr(argv)
1708     char **argv;
1709 {
1710     ipcp_wantoptions[0].accept_remote = 1;
1711     return 1;
1712 }
1713 
1714 
1715 /*
1716  * setnetmask - set the netmask to be used on the interface.
1717  */
1718 static int
1719 setnetmask(argv)
1720     char **argv;
1721 {
1722     struct in_addr ina;
1723 
1724     if (inet_aton(*argv, &ina) == 0 || (netmask & ~ina.s_addr) != 0) {
1725 	option_error("invalid netmask value '%s'", *argv);
1726 	return (0);
1727     }
1728 
1729     netmask = ina.s_addr;
1730     return (1);
1731 }
1732 
1733 static int
1734 setcrtscts(argv)
1735     char **argv;
1736 {
1737     crtscts = 1;
1738     return (1);
1739 }
1740 
1741 static int
1742 setnocrtscts(argv)
1743     char **argv;
1744 {
1745     crtscts = -1;
1746     return (1);
1747 }
1748 
1749 static int
1750 setxonxoff(argv)
1751     char **argv;
1752 {
1753     lcp_wantoptions[0].asyncmap |= 0x000A0000;	/* escape ^S and ^Q */
1754     lcp_wantoptions[0].neg_asyncmap = 1;
1755 
1756     crtscts = -2;
1757     return (1);
1758 }
1759 
1760 static int
1761 setnodetach(argv)
1762     char **argv;
1763 {
1764     nodetach = 1;
1765     return (1);
1766 }
1767 
1768 static int
1769 setupdetach(argv)
1770     char **argv;
1771 {
1772     nodetach = -1;
1773     return (1);
1774 }
1775 
1776 static int
1777 setdemand(argv)
1778     char **argv;
1779 {
1780     demand = 1;
1781     persist = 1;
1782     return 1;
1783 }
1784 
1785 static int
1786 setmodem(argv)
1787     char **argv;
1788 {
1789     modem = 1;
1790     return 1;
1791 }
1792 
1793 static int
1794 setmodem_chat(argv)
1795     char **argv;
1796 {
1797     modem_chat = 1;
1798     return 1;
1799 }
1800 
1801 static int
1802 setlocal(argv)
1803     char **argv;
1804 {
1805     modem = 0;
1806     return 1;
1807 }
1808 
1809 static int
1810 setlock(argv)
1811     char **argv;
1812 {
1813     lockflag = 1;
1814     return 1;
1815 }
1816 
1817 static int
1818 setusehostname(argv)
1819     char **argv;
1820 {
1821     usehostname = 1;
1822     return 1;
1823 }
1824 
1825 static int
1826 setname(argv)
1827     char **argv;
1828 {
1829     if (!privileged_option) {
1830 	option_error("using the name option requires root privilege");
1831 	return 0;
1832     }
1833     strlcpy(our_name, argv[0], MAXNAMELEN);
1834     return 1;
1835 }
1836 
1837 static int
1838 setuser(argv)
1839     char **argv;
1840 {
1841     strlcpy(user, argv[0], MAXNAMELEN);
1842     return 1;
1843 }
1844 
1845 static int
1846 setremote(argv)
1847     char **argv;
1848 {
1849     strlcpy(remote_name, argv[0], MAXNAMELEN);
1850     return 1;
1851 }
1852 
1853 static int
1854 setauth(argv)
1855     char **argv;
1856 {
1857     auth_required = 1;
1858     if (privileged_option > auth_req_info.priv) {
1859 	auth_req_info.priv = privileged_option;
1860 	auth_req_info.source = option_source;
1861     }
1862     return 1;
1863 }
1864 
1865 static int
1866 setnoauth(argv)
1867     char **argv;
1868 {
1869     if (auth_required && privileged_option < auth_req_info.priv) {
1870 	if (auth_req_info.source == NULL)
1871 	    option_error("cannot override default auth option");
1872 	else
1873 	    option_error("cannot override auth option set by %s",
1874 	        auth_req_info.source);
1875 	return 0;
1876     }
1877     auth_required = 0;
1878     return 1;
1879 }
1880 
1881 static int
1882 setdefaultroute(argv)
1883     char **argv;
1884 {
1885     if (!ipcp_allowoptions[0].default_route) {
1886 	option_error("defaultroute option is disabled");
1887 	return 0;
1888     }
1889     ipcp_wantoptions[0].default_route = 1;
1890     return 1;
1891 }
1892 
1893 static int
1894 setnodefaultroute(argv)
1895     char **argv;
1896 {
1897     ipcp_allowoptions[0].default_route = 0;
1898     ipcp_wantoptions[0].default_route = 0;
1899     return 1;
1900 }
1901 
1902 static int
1903 setproxyarp(argv)
1904     char **argv;
1905 {
1906     if (!ipcp_allowoptions[0].proxy_arp) {
1907 	option_error("proxyarp option is disabled");
1908 	return 0;
1909     }
1910     ipcp_wantoptions[0].proxy_arp = 1;
1911     return 1;
1912 }
1913 
1914 static int
1915 setnoproxyarp(argv)
1916     char **argv;
1917 {
1918     ipcp_wantoptions[0].proxy_arp = 0;
1919     ipcp_allowoptions[0].proxy_arp = 0;
1920     return 1;
1921 }
1922 
1923 static int
1924 setpersist(argv)
1925     char **argv;
1926 {
1927     persist = 1;
1928     return 1;
1929 }
1930 
1931 static int
1932 setnopersist(argv)
1933     char **argv;
1934 {
1935     persist = 0;
1936     return 1;
1937 }
1938 
1939 static int
1940 setdologin(argv)
1941     char **argv;
1942 {
1943     uselogin = 1;
1944     return 1;
1945 }
1946 
1947 /*
1948  * Functions to set the echo interval for modem-less monitors
1949  */
1950 
1951 static int
1952 setlcpechointv(argv)
1953     char **argv;
1954 {
1955     return int_option(*argv, &lcp_echo_interval);
1956 }
1957 
1958 static int
1959 setlcpechofails(argv)
1960     char **argv;
1961 {
1962     return int_option(*argv, &lcp_echo_fails);
1963 }
1964 
1965 /*
1966  * Functions to set timeouts, max transmits, etc.
1967  */
1968 static int
1969 setlcptimeout(argv)
1970     char **argv;
1971 {
1972     return int_option(*argv, &lcp_fsm[0].timeouttime);
1973 }
1974 
1975 static int
1976 setlcpterm(argv)
1977     char **argv;
1978 {
1979     return int_option(*argv, &lcp_fsm[0].maxtermtransmits);
1980 }
1981 
1982 static int
1983 setlcpconf(argv)
1984     char **argv;
1985 {
1986     return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits);
1987 }
1988 
1989 static int
1990 setlcpfails(argv)
1991     char **argv;
1992 {
1993     return int_option(*argv, &lcp_fsm[0].maxnakloops);
1994 }
1995 
1996 static int
1997 setipcptimeout(argv)
1998     char **argv;
1999 {
2000     return int_option(*argv, &ipcp_fsm[0].timeouttime);
2001 }
2002 
2003 static int
2004 setipcpterm(argv)
2005     char **argv;
2006 {
2007     return int_option(*argv, &ipcp_fsm[0].maxtermtransmits);
2008 }
2009 
2010 static int
2011 setipcpconf(argv)
2012     char **argv;
2013 {
2014     return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits);
2015 }
2016 
2017 static int
2018 setipcpfails(argv)
2019     char **argv;
2020 {
2021     return int_option(*argv, &lcp_fsm[0].maxnakloops);
2022 }
2023 
2024 static int
2025 setpaptimeout(argv)
2026     char **argv;
2027 {
2028     return int_option(*argv, &upap[0].us_timeouttime);
2029 }
2030 
2031 static int
2032 setpapreqtime(argv)
2033     char **argv;
2034 {
2035     return int_option(*argv, &upap[0].us_reqtimeout);
2036 }
2037 
2038 static int
2039 setpapreqs(argv)
2040     char **argv;
2041 {
2042     return int_option(*argv, &upap[0].us_maxtransmits);
2043 }
2044 
2045 static int
2046 setchaptimeout(argv)
2047     char **argv;
2048 {
2049     return int_option(*argv, &chap[0].timeouttime);
2050 }
2051 
2052 static int
2053 setchapchal(argv)
2054     char **argv;
2055 {
2056     return int_option(*argv, &chap[0].max_transmits);
2057 }
2058 
2059 static int
2060 setchapintv(argv)
2061     char **argv;
2062 {
2063     return int_option(*argv, &chap[0].chal_interval);
2064 }
2065 
2066 static int
2067 noccp(argv)
2068     char **argv;
2069 {
2070     ccp_protent.enabled_flag = 0;
2071     return 1;
2072 }
2073 
2074 static int
2075 setbsdcomp(argv)
2076     char **argv;
2077 {
2078     int rbits, abits;
2079     char *str, *endp;
2080 
2081     str = *argv;
2082     abits = rbits = strtol(str, &endp, 0);
2083     if (endp != str && *endp == ',') {
2084 	str = endp + 1;
2085 	abits = strtol(str, &endp, 0);
2086     }
2087     if (*endp != 0 || endp == str) {
2088 	option_error("invalid parameter '%s' for bsdcomp option", *argv);
2089 	return 0;
2090     }
2091     if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
2092 	|| (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
2093 	option_error("bsdcomp option values must be 0 or %d .. %d",
2094 		     BSD_MIN_BITS, BSD_MAX_BITS);
2095 	return 0;
2096     }
2097     if (rbits > 0) {
2098 	ccp_wantoptions[0].bsd_compress = 1;
2099 	ccp_wantoptions[0].bsd_bits = rbits;
2100     } else
2101 	ccp_wantoptions[0].bsd_compress = 0;
2102     if (abits > 0) {
2103 	ccp_allowoptions[0].bsd_compress = 1;
2104 	ccp_allowoptions[0].bsd_bits = abits;
2105     } else
2106 	ccp_allowoptions[0].bsd_compress = 0;
2107     return 1;
2108 }
2109 
2110 static int
2111 setnobsdcomp(argv)
2112     char **argv;
2113 {
2114     ccp_wantoptions[0].bsd_compress = 0;
2115     ccp_allowoptions[0].bsd_compress = 0;
2116     return 1;
2117 }
2118 
2119 static int
2120 setdeflate(argv)
2121     char **argv;
2122 {
2123     int rbits, abits;
2124     char *str, *endp;
2125 
2126     str = *argv;
2127     abits = rbits = strtol(str, &endp, 0);
2128     if (endp != str && *endp == ',') {
2129 	str = endp + 1;
2130 	abits = strtol(str, &endp, 0);
2131     }
2132     if (*endp != 0 || endp == str) {
2133 	option_error("invalid parameter '%s' for deflate option", *argv);
2134 	return 0;
2135     }
2136     if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
2137 	|| (abits != 0 && (abits < DEFLATE_MIN_SIZE
2138 			  || abits > DEFLATE_MAX_SIZE))) {
2139 	option_error("deflate option values must be 0 or %d .. %d",
2140 		     DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
2141 	return 0;
2142     }
2143     if (rbits > 0) {
2144 	ccp_wantoptions[0].deflate = 1;
2145 	ccp_wantoptions[0].deflate_size = rbits;
2146     } else
2147 	ccp_wantoptions[0].deflate = 0;
2148     if (abits > 0) {
2149 	ccp_allowoptions[0].deflate = 1;
2150 	ccp_allowoptions[0].deflate_size = abits;
2151     } else
2152 	ccp_allowoptions[0].deflate = 0;
2153     return 1;
2154 }
2155 
2156 static int
2157 setnodeflate(argv)
2158     char **argv;
2159 {
2160     ccp_wantoptions[0].deflate = 0;
2161     ccp_allowoptions[0].deflate = 0;
2162     return 1;
2163 }
2164 
2165 static int
2166 setnodeflatedraft(argv)
2167     char **argv;
2168 {
2169     ccp_wantoptions[0].deflate_draft = 0;
2170     ccp_allowoptions[0].deflate_draft = 0;
2171     return 1;
2172 }
2173 
2174 static int
2175 setpred1comp(argv)
2176     char **argv;
2177 {
2178     ccp_wantoptions[0].predictor_1 = 1;
2179     ccp_allowoptions[0].predictor_1 = 1;
2180     return 1;
2181 }
2182 
2183 static int
2184 setnopred1comp(argv)
2185     char **argv;
2186 {
2187     ccp_wantoptions[0].predictor_1 = 0;
2188     ccp_allowoptions[0].predictor_1 = 0;
2189     return 1;
2190 }
2191 
2192 static int
2193 setipparam(argv)
2194     char **argv;
2195 {
2196     ipparam = strdup(*argv);
2197     if (ipparam == NULL)
2198 	novm("ipparam string");
2199 
2200     return 1;
2201 }
2202 
2203 static int
2204 setpapcrypt(argv)
2205     char **argv;
2206 {
2207     cryptpap = 1;
2208     return 1;
2209 }
2210 
2211 static int
2212 setidle(argv)
2213     char **argv;
2214 {
2215     return int_option(*argv, &idle_time_limit);
2216 }
2217 
2218 static int
2219 setholdoff(argv)
2220     char **argv;
2221 {
2222     return int_option(*argv, &holdoff);
2223 }
2224 
2225 /*
2226  * setdnsaddr - set the dns address(es)
2227  */
2228 static int
2229 setdnsaddr(argv)
2230     char **argv;
2231 {
2232     struct in_addr ina;
2233     struct hostent *hp;
2234 
2235     if (inet_aton(*argv, &ina) == 0) {
2236 	if ((hp = gethostbyname(*argv)) == NULL) {
2237 	    option_error("invalid address parameter '%s' for ms-dns option",
2238 			 *argv);
2239 	    return (0);
2240 	}
2241 	ina.s_addr = *(u_int32_t *)hp->h_addr;
2242     }
2243 
2244     /* if there is no primary then update it. */
2245     if (ipcp_allowoptions[0].dnsaddr[0] == 0)
2246 	ipcp_allowoptions[0].dnsaddr[0] = ina.s_addr;
2247 
2248     /* always set the secondary address value to the same value. */
2249     ipcp_allowoptions[0].dnsaddr[1] = ina.s_addr;
2250 
2251     return (1);
2252 }
2253 
2254 /*
2255  * setwinsaddr - set the wins address(es)
2256  * This is primrarly used with the Samba package under UNIX or for pointing
2257  * the caller to the existing WINS server on a Windows NT platform.
2258  */
2259 static int
2260 setwinsaddr(argv)
2261     char **argv;
2262 {
2263     struct in_addr ina;
2264     struct hostent *hp;
2265 
2266     if (inet_aton(*argv, &ina) == 0) {
2267 	if ((hp = gethostbyname(*argv)) == NULL) {
2268 	    option_error("invalid address parameter '%s' for ms-wins option",
2269 			 *argv);
2270 	    return (0);
2271 	}
2272 	ina.s_addr = *(u_int32_t *)hp->h_addr;
2273     }
2274 
2275     /* if there is no primary then update it. */
2276     if (ipcp_allowoptions[0].winsaddr[0] == 0)
2277 	ipcp_allowoptions[0].winsaddr[0] = ina.s_addr;
2278 
2279     /* always set the secondary address value to the same value. */
2280     ipcp_allowoptions[0].winsaddr[1] = ina.s_addr;
2281 
2282     return (1);
2283 }
2284 
2285 #ifdef MSLANMAN
2286 static int
2287 setmslanman(argv)
2288     char **argv;
2289 {
2290     ms_lanman = 1;
2291     return (1);
2292 }
2293 #endif
2294