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