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