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