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