]> git.ozlabs.org Git - ppp.git/blob - pppd/options.c
use PACKETPTR rather than PACKET *
[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.16 1994/09/21 06:47:37 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 #define FALSE   0
49 #define TRUE    1
50
51 #ifdef ultrix
52 char *strdup __P((char *));
53 #endif
54
55 #ifndef GIDSET_TYPE
56 #define GIDSET_TYPE     gid_t
57 #endif
58
59 /*
60  * Option variables and default values.
61  */
62 int     debug = 0;              /* Debug flag */
63 int     kdebugflag = 0;         /* Tell kernel to print debug messages */
64 int     default_device = 1;     /* Using /dev/tty or equivalent */
65 char    devnam[MAXPATHLEN] = "/dev/tty";        /* Device name */
66 int     crtscts = 0;            /* Use hardware flow control */
67 int     modem = 1;              /* Use modem control lines */
68 int     inspeed = 0;            /* Input/Output speed requested */
69 u_int32_t netmask = 0;          /* IP netmask to set on interface */
70 int     lockflag = 0;           /* Create lock file to lock the serial dev */
71 int     nodetach = 0;           /* Don't detach from controlling tty */
72 char    *connector = NULL;      /* Script to establish physical link */
73 char    *disconnector = NULL;   /* Script to disestablish physical link */
74 char    user[MAXNAMELEN];       /* Username for PAP */
75 char    passwd[MAXSECRETLEN];   /* Password for PAP */
76 int     auth_required = 0;      /* Peer is required to authenticate */
77 int     defaultroute = 0;       /* assign default route through interface */
78 int     proxyarp = 0;           /* Set up proxy ARP entry for peer */
79 int     persist = 0;            /* Reopen link after it goes down */
80 int     uselogin = 0;           /* Use /etc/passwd for checking PAP */
81 int     lcp_echo_interval = 0;  /* Interval between LCP echo-requests */
82 int     lcp_echo_fails = 0;     /* Tolerance to unanswered echo-requests */
83 char    our_name[MAXNAMELEN];   /* Our name for authentication purposes */
84 char    remote_name[MAXNAMELEN]; /* Peer's name for authentication */
85 int     usehostname = 0;        /* Use hostname for our_name */
86 int     disable_defaultip = 0;  /* Don't use hostname for default IP adrs */
87
88 /*
89  * Prototypes
90  */
91 static int setdebug __P((void));
92 static int setkdebug __P((char **));
93 static int setpassive __P((void));
94 static int setsilent __P((void));
95 static int noopt __P((void));
96 static int setnovj __P((void));
97 static int setnovjccomp __P((void));
98 static int setvjslots __P((char **));
99 static int reqpap __P((void));
100 static int nopap __P((void));
101 static int setupapfile __P((char **));
102 static int nochap __P((void));
103 static int reqchap __P((void));
104 static int setspeed __P((char *));
105 static int noaccomp __P((void));
106 static int noasyncmap __P((void));
107 static int noipaddr __P((void));
108 static int nomagicnumber __P((void));
109 static int setasyncmap __P((char **));
110 static int setescape __P((char **));
111 static int setmru __P((char **));
112 static int setmtu __P((char **));
113 static int nomru __P((void));
114 static int nopcomp __P((void));
115 static int setconnector __P((char **));
116 static int setdisconnector __P((char **));
117 static int setdomain __P((char **));
118 static int setnetmask __P((char **));
119 static int setcrtscts __P((void));
120 static int setnocrtscts __P((void));
121 static int setxonxoff __P((void));
122 static int setnodetach __P((void));
123 static int setmodem __P((void));
124 static int setlocal __P((void));
125 static int setlock __P((void));
126 static int setname __P((char **));
127 static int setuser __P((char **));
128 static int setremote __P((char **));
129 static int setauth __P((void));
130 static int readfile __P((char **));
131 static int setdefaultroute __P((void));
132 static int setproxyarp __P((void));
133 static int setpersist __P((void));
134 static int setdologin __P((void));
135 static int setusehostname __P((void));
136 static int setnoipdflt __P((void));
137 static int setlcptimeout __P((char **));
138 static int setlcpterm __P((char **));
139 static int setlcpconf __P((char **));
140 static int setlcpfails __P((char **));
141 static int setipcptimeout __P((char **));
142 static int setipcpterm __P((char **));
143 static int setipcpconf __P((char **));
144 static int setipcpfails __P((char **));
145 static int setpaptimeout __P((char **));
146 static int setpapreqs __P((char **));
147 static int setchaptimeout __P((char **));
148 static int setchapchal __P((char **));
149 static int setchapintv __P((char **));
150 static int setipcpaccl __P((void));
151 static int setipcpaccr __P((void));
152 static int setlcpechointv __P((char **));
153 static int setlcpechofails __P((char **));
154 static int setbsdcomp __P((char **));
155 static int setnobsdcomp __P((void));
156
157 static int number_option __P((char *, long *, int));
158 static int readable __P((int fd));
159
160 /*
161  * Valid arguments.
162  */
163 static struct cmd {
164     char *cmd_name;
165     int num_args;
166     int (*cmd_func)();
167 } cmds[] = {
168     {"-all", 0, noopt},         /* Don't request/allow any options */
169     {"-ac", 0, noaccomp},       /* Disable Address/Control compress */
170     {"-am", 0, noasyncmap},     /* Disable asyncmap negotiation */
171     {"-as", 1, setasyncmap},    /* set the desired async map */
172     {"-d", 0, setdebug},        /* Increase debugging level */
173     {"-detach", 0, setnodetach}, /* don't fork */
174     {"-ip", 0, noipaddr},       /* Disable IP address negotiation */
175     {"-mn", 0, nomagicnumber},  /* Disable magic number negotiation */
176     {"-mru", 0, nomru},         /* Disable mru negotiation */
177     {"-p", 0, setpassive},      /* Set passive mode */
178     {"-pc", 0, nopcomp},        /* Disable protocol field compress */
179     {"+ua", 1, setupapfile},    /* Get PAP user and password from file */
180     {"+pap", 0, reqpap},        /* Require PAP auth from peer */
181     {"-pap", 0, nopap},         /* Don't allow UPAP authentication with peer */
182     {"+chap", 0, reqchap},      /* Require CHAP authentication from peer */
183     {"-chap", 0, nochap},       /* Don't allow CHAP authentication with peer */
184     {"-vj", 0, setnovj},        /* disable VJ compression */
185     {"-vjccomp", 0, setnovjccomp}, /* disable VJ connection-ID compression */
186     {"vj-max-slots", 1, setvjslots}, /* Set maximum VJ header slots */
187     {"asyncmap", 1, setasyncmap}, /* set the desired async map */
188     {"escape", 1, setescape},   /* set chars to escape on transmission */
189     {"connect", 1, setconnector}, /* A program to set up a connection */
190     {"disconnect", 1, setdisconnector}, /* program to disconnect serial dev. */
191     {"crtscts", 0, setcrtscts}, /* set h/w flow control */
192     {"-crtscts", 0, setnocrtscts}, /* clear h/w flow control */
193     {"xonxoff", 0, setxonxoff}, /* set s/w flow control */
194     {"debug", 0, setdebug},     /* Increase debugging level */
195     {"kdebug", 1, setkdebug},   /* Enable kernel-level debugging */
196     {"domain", 1, setdomain},   /* Add given domain name to hostname*/
197     {"mru", 1, setmru},         /* Set MRU value for negotiation */
198     {"mtu", 1, setmtu},         /* Set our MTU */
199     {"netmask", 1, setnetmask}, /* set netmask */
200     {"passive", 0, setpassive}, /* Set passive mode */
201     {"silent", 0, setsilent},   /* Set silent mode */
202     {"modem", 0, setmodem},     /* Use modem control lines */
203     {"local", 0, setlocal},     /* Don't use modem control lines */
204     {"lock", 0, setlock},       /* Lock serial device (with lock file) */
205     {"name", 1, setname},       /* Set local name for authentication */
206     {"user", 1, setuser},       /* Set username for PAP auth with peer */
207     {"usehostname", 0, setusehostname}, /* Must use hostname for auth. */
208     {"remotename", 1, setremote}, /* Set remote name for authentication */
209     {"auth", 0, setauth},       /* Require authentication from peer */
210     {"file", 1, readfile},      /* Take options from a file */
211     {"defaultroute", 0, setdefaultroute}, /* Add default route */
212     {"proxyarp", 0, setproxyarp}, /* Add proxy ARP entry */
213     {"persist", 0, setpersist}, /* Keep on reopening connection after close */
214     {"login", 0, setdologin},   /* Use system password database for UPAP */
215     {"noipdefault", 0, setnoipdflt}, /* Don't use name for default IP adrs */
216     {"lcp-echo-failure", 1, setlcpechofails}, /* consecutive echo failures */
217     {"lcp-echo-interval", 1, setlcpechointv}, /* time for lcp echo events */
218     {"lcp-restart", 1, setlcptimeout}, /* Set timeout for LCP */
219     {"lcp-max-terminate", 1, setlcpterm}, /* Set max #xmits for term-reqs */
220     {"lcp-max-configure", 1, setlcpconf}, /* Set max #xmits for conf-reqs */
221     {"lcp-max-failure", 1, setlcpfails}, /* Set max #conf-naks for LCP */
222     {"ipcp-restart", 1, setipcptimeout}, /* Set timeout for IPCP */
223     {"ipcp-max-terminate", 1, setipcpterm}, /* Set max #xmits for term-reqs */
224     {"ipcp-max-configure", 1, setipcpconf}, /* Set max #xmits for conf-reqs */
225     {"ipcp-max-failure", 1, setipcpfails}, /* Set max #conf-naks for IPCP */
226     {"pap-restart", 1, setpaptimeout}, /* Set timeout for UPAP */
227     {"pap-max-authreq", 1, setpapreqs}, /* Set max #xmits for auth-reqs */
228     {"chap-restart", 1, setchaptimeout}, /* Set timeout for CHAP */
229     {"chap-max-challenge", 1, setchapchal}, /* Set max #xmits for challenge */
230     {"chap-interval", 1, setchapintv}, /* Set interval for rechallenge */
231     {"ipcp-accept-local", 0, setipcpaccl}, /* Accept peer's address for us */
232     {"ipcp-accept-remote", 0, setipcpaccr}, /* Accept peer's address for it */
233     {"bsdcomp", 1, setbsdcomp},         /* request BSD-Compress */
234     {"-bsdcomp", 0, setnobsdcomp},      /* don't allow BSD-Compress */
235     {NULL, 0, NULL}
236 };
237
238
239 #ifndef IMPLEMENTATION
240 #define IMPLEMENTATION ""
241 #endif
242
243 static char *usage_string = "\
244 pppd version %s patch level %d%s\n\
245 Usage: %s [ arguments ], where arguments are:\n\
246         <device>        Communicate over the named device\n\
247         <speed>         Set the baud rate to <speed>\n\
248         <loc>:<rem>     Set the local and/or remote interface IP\n\
249                         addresses.  Either one may be omitted.\n\
250         asyncmap <n>    Set the desired async map to hex <n>\n\
251         auth            Require authentication from peer\n\
252         connect <p>     Invoke shell command <p> to set up the serial line\n\
253         crtscts         Use hardware RTS/CTS flow control\n\
254         defaultroute    Add default route through interface\n\
255         file <f>        Take options from file <f>\n\
256         modem           Use modem control lines\n\
257         mru <n>         Set MRU value to <n> for negotiation\n\
258         netmask <n>     Set interface netmask to <n>\n\
259 See pppd(8) for more options.\n\
260 ";
261
262
263 /*
264  * parse_args - parse a string of arguments, from the command
265  * line or from a file.
266  */
267 int
268 parse_args(argc, argv)
269     int argc;
270     char **argv;
271 {
272     char *arg, *val;
273     struct cmd *cmdp;
274     int ret;
275
276     while (argc > 0) {
277         arg = *argv++;
278         --argc;
279
280         /*
281          * First see if it's a command.
282          */
283         for (cmdp = cmds; cmdp->cmd_name; cmdp++)
284             if (!strcmp(arg, cmdp->cmd_name))
285                 break;
286
287         if (cmdp->cmd_name != NULL) {
288             if (argc < cmdp->num_args) {
289                 fprintf(stderr, "Too few parameters for command %s\n", arg);
290                 return 0;
291             }
292             if (!(*cmdp->cmd_func)(argv))
293                 return 0;
294             argc -= cmdp->num_args;
295             argv += cmdp->num_args;
296
297         } else {
298             /*
299              * Maybe a tty name, speed or IP address?
300              */
301             if ((ret = setdevname(arg)) == 0
302                 && (ret = setspeed(arg)) == 0
303                 && (ret = setipaddr(arg)) == 0) {
304                 fprintf(stderr, "%s: unrecognized command\n", arg);
305                 usage();
306                 return 0;
307             }
308             if (ret < 0)        /* error */
309                 return 0;
310         }
311     }
312     return 1;
313 }
314
315 /*
316  * usage - print out a message telling how to use the program.
317  */
318 usage()
319 {
320     fprintf(stderr, usage_string, VERSION, PATCHLEVEL, IMPLEMENTATION,
321             progname);
322 }
323
324 /*
325  * options_from_file - Read a string of options from a file,
326  * and interpret them.
327  */
328 int
329 options_from_file(filename, must_exist, check_prot)
330     char *filename;
331     int must_exist;
332     int check_prot;
333 {
334     FILE *f;
335     int i, newline, ret;
336     struct cmd *cmdp;
337     char *argv[MAXARGS];
338     char args[MAXARGS][MAXWORDLEN];
339     char cmd[MAXWORDLEN];
340
341     if ((f = fopen(filename, "r")) == NULL) {
342         if (!must_exist && errno == ENOENT)
343             return 1;
344         perror(filename);
345         return 0;
346     }
347     if (check_prot && !readable(fileno(f))) {
348         fprintf(stderr, "%s: access denied\n", filename);
349         fclose(f);
350         return 0;
351     }
352
353     while (getword(f, cmd, &newline, filename)) {
354         /*
355          * First see if it's a command.
356          */
357         for (cmdp = cmds; cmdp->cmd_name; cmdp++)
358             if (!strcmp(cmd, cmdp->cmd_name))
359                 break;
360
361         if (cmdp->cmd_name != NULL) {
362             for (i = 0; i < cmdp->num_args; ++i) {
363                 if (!getword(f, args[i], &newline, filename)) {
364                     fprintf(stderr,
365                             "In file %s: too few parameters for command %s\n",
366                             filename, cmd);
367                     fclose(f);
368                     return 0;
369                 }
370                 argv[i] = args[i];
371             }
372             if (!(*cmdp->cmd_func)(argv)) {
373                 fclose(f);
374                 return 0;
375             }
376
377         } else {
378             /*
379              * Maybe a tty name, speed or IP address?
380              */
381             if ((ret = setdevname(cmd)) == 0
382                 && (ret = setspeed(cmd)) == 0
383                 && (ret = setipaddr(cmd)) == 0) {
384                 fprintf(stderr, "In file %s: unrecognized command %s\n",
385                         filename, cmd);
386                 fclose(f);
387                 return 0;
388             }
389             if (ret < 0)        /* error */
390                 return 0;
391         }
392     }
393     return 1;
394 }
395
396 /*
397  * options_from_user - See if the use has a ~/.ppprc file,
398  * and if so, interpret options from it.
399  */
400 int
401 options_from_user()
402 {
403     char *user, *path, *file;
404     int ret;
405     struct passwd *pw;
406
407     pw = getpwuid(getuid());
408     if (pw == NULL || (user = pw->pw_dir) == NULL || user[0] == 0)
409         return 1;
410     file = _PATH_USEROPT;
411     path = malloc(strlen(user) + strlen(file) + 2);
412     if (path == NULL)
413         novm("init file name");
414     strcpy(path, user);
415     strcat(path, "/");
416     strcat(path, file);
417     ret = options_from_file(path, 0, 1);
418     free(path);
419     return ret;
420 }
421
422 /*
423  * options_for_tty - See if an options file exists for the serial
424  * device, and if so, interpret options from it.
425  */
426 int
427 options_for_tty()
428 {
429     char *dev, *path;
430     int ret;
431
432     dev = strrchr(devnam, '/');
433     if (dev == NULL)
434         dev = devnam;
435     else
436         ++dev;
437     if (strcmp(dev, "tty") == 0)
438         return 1;               /* don't look for /etc/ppp/options.tty */
439     path = malloc(strlen(_PATH_TTYOPT) + strlen(dev) + 1);
440     if (path == NULL)
441         novm("tty init file name");
442     strcpy(path, _PATH_TTYOPT);
443     strcat(path, dev);
444     ret = options_from_file(path, 0, 0);
445     free(path);
446     return ret;
447 }
448
449 /*
450  * readable - check if a file is readable by the real user.
451  */
452 static int
453 readable(fd)
454     int fd;
455 {
456     uid_t uid;
457     int ngroups, i;
458     struct stat sbuf;
459     GIDSET_TYPE groups[NGROUPS_MAX];
460
461     uid = getuid();
462     if (uid == 0)
463         return 1;
464     if (fstat(fd, &sbuf) != 0)
465         return 0;
466     if (sbuf.st_uid == uid)
467         return sbuf.st_mode & S_IRUSR;
468     if (sbuf.st_gid == getgid())
469         return sbuf.st_mode & S_IRGRP;
470     ngroups = getgroups(NGROUPS_MAX, groups);
471     for (i = 0; i < ngroups; ++i)
472         if (sbuf.st_gid == groups[i])
473             return sbuf.st_mode & S_IRGRP;
474     return sbuf.st_mode & S_IROTH;
475 }
476
477 /*
478  * Read a word from a file.
479  * Words are delimited by white-space or by quotes (").
480  * Quotes, white-space and \ may be escaped with \.
481  * \<newline> is ignored.
482  */
483 int
484 getword(f, word, newlinep, filename)
485     FILE *f;
486     char *word;
487     int *newlinep;
488     char *filename;
489 {
490     int c, len, escape;
491     int quoted;
492
493     *newlinep = 0;
494     len = 0;
495     escape = 0;
496     quoted = 0;
497
498     /*
499      * First skip white-space and comments
500      */
501     while ((c = getc(f)) != EOF) {
502         if (c == '\\') {
503             /*
504              * \<newline> is ignored; \ followed by anything else
505              * starts a word.
506              */
507             if ((c = getc(f)) == '\n')
508                 continue;
509             word[len++] = '\\';
510             escape = 1;
511             break;
512         }
513         if (c == '\n')
514             *newlinep = 1;      /* next word starts a line */
515         else if (c == '#') {
516             /* comment - ignore until EOF or \n */
517             while ((c = getc(f)) != EOF && c != '\n')
518                 ;
519             if (c == EOF)
520                 break;
521             *newlinep = 1;
522         } else if (!isspace(c))
523             break;
524     }
525
526     /*
527      * End of file or error - fail
528      */
529     if (c == EOF) {
530         if (ferror(f)) {
531             perror(filename);
532             die(1);
533         }
534         return 0;
535     }
536
537     for (;;) {
538         /*
539          * Is this character escaped by \ ?
540          */
541         if (escape) {
542             if (c == '\n')
543                 --len;                  /* ignore \<newline> */
544             else if (c == '"' || isspace(c) || c == '\\')
545                 word[len-1] = c;        /* put special char in word */
546             else {
547                 if (len < MAXWORDLEN-1)
548                     word[len] = c;
549                 ++len;
550             }
551             escape = 0;
552         } else if (c == '"') {
553             quoted = !quoted;
554         } else if (!quoted && (isspace(c) || c == '#')) {
555             ungetc(c, f);
556             break;
557         } else {
558             if (len < MAXWORDLEN-1)
559                 word[len] = c;
560             ++len;
561             if (c == '\\')
562                 escape = 1;
563         }
564         if ((c = getc(f)) == EOF)
565             break;
566     }
567
568     if (ferror(f)) {
569         perror(filename);
570         die(1);
571     }
572
573     if (len >= MAXWORDLEN) {
574         word[MAXWORDLEN-1] = 0;
575         fprintf(stderr, "%s: warning: word in file %s too long (%.20s...)\n",
576                 progname, filename, word);
577     } else
578         word[len] = 0;
579
580     return 1;
581 }
582
583 /*
584  * number_option - parse a numeric parameter for an option
585  */
586 static int
587 number_option(str, valp, base)
588     char *str;
589     long *valp;
590     int base;
591 {
592     char *ptr;
593
594     *valp = strtol(str, &ptr, base);
595     if (ptr == str) {
596         fprintf(stderr, "%s: invalid number: %s\n", progname, str);
597         return 0;
598     }
599     return 1;
600 }
601
602
603 /*
604  * int_option - like number_option, but valp is int *,
605  * the base is assumed to be 0, and *valp is not changed
606  * if there is an error.
607  */
608 static int
609 int_option(str, valp)
610     char *str;
611     int *valp;
612 {
613     long v;
614
615     if (!number_option(str, &v, 0))
616         return 0;
617     *valp = (int) v;
618     return 1;
619 }
620
621
622 /*
623  * The following procedures execute commands.
624  */
625
626 /*
627  * readfile - take commands from a file.
628  */
629 static int
630 readfile(argv)
631     char **argv;
632 {
633     return options_from_file(*argv, 1, 1);
634 }
635
636 /*
637  * setdebug - Set debug (command line argument).
638  */
639 static int
640 setdebug()
641 {
642     debug++;
643     return (1);
644 }
645
646 /*
647  * setkdebug - Set kernel debugging level.
648  */
649 static int
650 setkdebug(argv)
651     char **argv;
652 {
653     return int_option(*argv, &kdebugflag);
654 }
655
656 /*
657  * noopt - Disable all options.
658  */
659 static int
660 noopt()
661 {
662     BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
663     BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
664     BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
665     BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
666     return (1);
667 }
668
669 /*
670  * noaccomp - Disable Address/Control field compression negotiation.
671  */
672 static int
673 noaccomp()
674 {
675     lcp_wantoptions[0].neg_accompression = 0;
676     lcp_allowoptions[0].neg_accompression = 0;
677     return (1);
678 }
679
680
681 /*
682  * noasyncmap - Disable async map negotiation.
683  */
684 static int
685 noasyncmap()
686 {
687     lcp_wantoptions[0].neg_asyncmap = 0;
688     lcp_allowoptions[0].neg_asyncmap = 0;
689     return (1);
690 }
691
692
693 /*
694  * noipaddr - Disable IP address negotiation.
695  */
696 static int
697 noipaddr()
698 {
699     ipcp_wantoptions[0].neg_addr = 0;
700     ipcp_allowoptions[0].neg_addr = 0;
701     return (1);
702 }
703
704
705 /*
706  * nomagicnumber - Disable magic number negotiation.
707  */
708 static int
709 nomagicnumber()
710 {
711     lcp_wantoptions[0].neg_magicnumber = 0;
712     lcp_allowoptions[0].neg_magicnumber = 0;
713     return (1);
714 }
715
716
717 /*
718  * nomru - Disable mru negotiation.
719  */
720 static int
721 nomru()
722 {
723     lcp_wantoptions[0].neg_mru = 0;
724     lcp_allowoptions[0].neg_mru = 0;
725     return (1);
726 }
727
728
729 /*
730  * setmru - Set MRU for negotiation.
731  */
732 static int
733 setmru(argv)
734     char **argv;
735 {
736     long mru;
737
738     if (!number_option(*argv, &mru, 0))
739         return 0;
740     lcp_wantoptions[0].mru = mru;
741     lcp_wantoptions[0].neg_mru = 1;
742     return (1);
743 }
744
745
746 /*
747  * setmru - Set the largest MTU we'll use.
748  */
749 static int
750 setmtu(argv)
751     char **argv;
752 {
753     long mtu;
754
755     if (!number_option(*argv, &mtu, 0))
756         return 0;
757     if (mtu < MINMRU || mtu > MAXMRU) {
758         fprintf(stderr, "mtu option value of %d is too %s\n", mtu,
759                 (mtu < MINMRU? "small": "large"));
760         return 0;
761     }
762     lcp_allowoptions[0].mru = mtu;
763     return (1);
764 }
765
766
767 /*
768  * nopcomp - Disable Protocol field compression negotiation.
769  */
770 static int
771 nopcomp()
772 {
773     lcp_wantoptions[0].neg_pcompression = 0;
774     lcp_allowoptions[0].neg_pcompression = 0;
775     return (1);
776 }
777
778
779 /*
780  * setpassive - Set passive mode (don't give up if we time out sending
781  * LCP configure-requests).
782  */
783 static int
784 setpassive()
785 {
786     lcp_wantoptions[0].passive = 1;
787     return (1);
788 }
789
790
791 /*
792  * setsilent - Set silent mode (don't start sending LCP configure-requests
793  * until we get one from the peer).
794  */
795 static int
796 setsilent()
797 {
798     lcp_wantoptions[0].silent = 1;
799     return 1;
800 }
801
802
803 /*
804  * nopap - Disable PAP authentication with peer.
805  */
806 static int
807 nopap()
808 {
809     lcp_allowoptions[0].neg_upap = 0;
810     return (1);
811 }
812
813
814 /*
815  * reqpap - Require PAP authentication from peer.
816  */
817 static int
818 reqpap()
819 {
820     lcp_wantoptions[0].neg_upap = 1;
821     auth_required = 1;
822 }
823
824
825 /*
826  * setupapfile - specifies UPAP info for authenticating with peer.
827  */
828 static int
829 setupapfile(argv)
830     char **argv;
831 {
832     FILE * ufile;
833     int l;
834
835     lcp_allowoptions[0].neg_upap = 1;
836
837     /* open user info file */
838     if ((ufile = fopen(*argv, "r")) == NULL) {
839         fprintf(stderr, "unable to open user login data file %s\n", *argv);
840         return 0;
841     }
842     if (!readable(fileno(ufile))) {
843         fprintf(stderr, "%s: access denied\n", *argv);
844         return 0;
845     }
846     check_access(ufile, *argv);
847
848     /* get username */
849     if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
850         || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
851         fprintf(stderr, "Unable to read user login data file %s.\n", *argv);
852         return 0;
853     }
854     fclose(ufile);
855
856     /* get rid of newlines */
857     l = strlen(user);
858     if (l > 0 && user[l-1] == '\n')
859         user[l-1] = 0;
860     l = strlen(passwd);
861     if (l > 0 && passwd[l-1] == '\n')
862         passwd[l-1] = 0;
863
864     return (1);
865 }
866
867
868 /*
869  * nochap - Disable CHAP authentication with peer.
870  */
871 static int
872 nochap()
873 {
874     lcp_allowoptions[0].neg_chap = 0;
875     return (1);
876 }
877
878
879 /*
880  * reqchap - Require CHAP authentication from peer.
881  */
882 static int
883 reqchap()
884 {
885     lcp_wantoptions[0].neg_chap = 1;
886     auth_required = 1;
887     return (1);
888 }
889
890
891 /*
892  * setnovj - disable vj compression
893  */
894 static int
895 setnovj()
896 {
897     ipcp_wantoptions[0].neg_vj = 0;
898     ipcp_allowoptions[0].neg_vj = 0;
899     return (1);
900 }
901
902
903 /*
904  * setnovjccomp - disable VJ connection-ID compression
905  */
906 static int
907 setnovjccomp()
908 {
909     ipcp_wantoptions[0].cflag = 0;
910     ipcp_allowoptions[0].cflag = 0;
911 }
912
913
914 /*
915  * setvjslots - set maximum number of connection slots for VJ compression
916  */
917 static int
918 setvjslots(argv)
919     char **argv;
920 {
921     int value;
922
923     if (!int_option(*argv, &value))
924         return 0;
925     if (value < 2 || value > 16) {
926         fprintf(stderr, "pppd: vj-max-slots value must be between 2 and 16\n");
927         return 0;
928     }
929     ipcp_wantoptions [0].maxslotindex =
930         ipcp_allowoptions[0].maxslotindex = value - 1;
931     return 1;
932 }
933
934
935 /*
936  * setconnector - Set a program to connect to a serial line
937  */
938 static int
939 setconnector(argv)
940     char **argv;
941 {
942     connector = strdup(*argv);
943     if (connector == NULL)
944         novm("connector string");
945   
946     return (1);
947 }
948
949 /*
950  * setdisconnector - Set a program to disconnect from the serial line
951  */
952 static int
953 setdisconnector(argv)
954     char **argv;
955 {
956     disconnector = strdup(*argv);
957     if (disconnector == NULL)
958         novm("disconnector string");
959   
960     return (1);
961 }
962
963
964 /*
965  * setdomain - Set domain name to append to hostname 
966  */
967 static int
968 setdomain(argv)
969     char **argv;
970 {
971     strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
972     hostname[MAXNAMELEN-1] = 0;
973     return (1);
974 }
975
976
977 /*
978  * setasyncmap - add bits to asyncmap (what we request peer to escape).
979  */
980 static int
981 setasyncmap(argv)
982     char **argv;
983 {
984     long asyncmap;
985
986     if (!number_option(*argv, &asyncmap, 16))
987         return 0;
988     lcp_wantoptions[0].asyncmap |= asyncmap;
989     lcp_wantoptions[0].neg_asyncmap = 1;
990     return(1);
991 }
992
993
994 /*
995  * setescape - add chars to the set we escape on transmission.
996  */
997 static int
998 setescape(argv)
999     char **argv;
1000 {
1001     int n, ret;
1002     char *p, *endp;
1003
1004     p = *argv;
1005     ret = 1;
1006     while (*p) {
1007         n = strtol(p, &endp, 16);
1008         if (p == endp) {
1009             fprintf(stderr, "%s: invalid hex number: %s\n", progname, p);
1010             return 0;
1011         }
1012         p = endp;
1013         if (n < 0 || 0x20 <= n && n <= 0x3F || n == 0x5E || n > 0xFF) {
1014             fprintf(stderr, "%s: can't escape character 0x%x\n", n);
1015             ret = 0;
1016         } else
1017             xmit_accm[0][n >> 5] |= 1 << (n & 0x1F);
1018         while (*p == ',' || *p == ' ')
1019             ++p;
1020     }
1021     return ret;
1022 }
1023
1024
1025 /*
1026  * setspeed - Set the speed.
1027  */
1028 static int
1029 setspeed(arg)
1030     char *arg;
1031 {
1032     char *ptr;
1033     int spd;
1034
1035     spd = strtol(arg, &ptr, 0);
1036     if (ptr == arg || *ptr != 0 || spd == 0)
1037         return 0;
1038     inspeed = spd;
1039     return 1;
1040 }
1041
1042
1043 /*
1044  * setdevname - Set the device name.
1045  */
1046 int
1047 setdevname(cp)
1048     char *cp;
1049 {
1050     struct stat statbuf;
1051     char *tty, *ttyname();
1052     char dev[MAXPATHLEN];
1053   
1054     if (strncmp("/dev/", cp, 5) != 0) {
1055         strcpy(dev, "/dev/");
1056         strncat(dev, cp, MAXPATHLEN - 5);
1057         dev[MAXPATHLEN-1] = 0;
1058         cp = dev;
1059     }
1060
1061     /*
1062      * Check if there is a device by this name.
1063      */
1064     if (stat(cp, &statbuf) < 0) {
1065         if (errno == ENOENT)
1066             return 0;
1067         syslog(LOG_ERR, cp);
1068         return -1;
1069     }
1070   
1071     (void) strncpy(devnam, cp, MAXPATHLEN);
1072     devnam[MAXPATHLEN-1] = 0;
1073     default_device = FALSE;
1074   
1075     return 1;
1076 }
1077
1078
1079 /*
1080  * setipaddr - Set the IP address
1081  */
1082 int
1083 setipaddr(arg)
1084     char *arg;
1085 {
1086     struct hostent *hp;
1087     char *colon, *index();
1088     u_int32_t local, remote;
1089     ipcp_options *wo = &ipcp_wantoptions[0];
1090   
1091     /*
1092      * IP address pair separated by ":".
1093      */
1094     if ((colon = index(arg, ':')) == NULL)
1095         return 0;
1096   
1097     /*
1098      * If colon first character, then no local addr.
1099      */
1100     if (colon != arg) {
1101         *colon = '\0';
1102         if ((local = inet_addr(arg)) == -1) {
1103             if ((hp = gethostbyname(arg)) == NULL) {
1104                 fprintf(stderr, "unknown host: %s\n", arg);
1105                 return -1;
1106             } else {
1107                 local = *(long *)hp->h_addr;
1108                 if (our_name[0] == 0) {
1109                     strncpy(our_name, arg, MAXNAMELEN);
1110                     our_name[MAXNAMELEN-1] = 0;
1111                 }
1112             }
1113         }
1114         if (bad_ip_adrs(local)) {
1115             fprintf(stderr, "bad local IP address %s\n", ip_ntoa(local));
1116             return -1;
1117         }
1118         if (local != 0)
1119             wo->ouraddr = local;
1120         *colon = ':';
1121     }
1122   
1123     /*
1124      * If colon last character, then no remote addr.
1125      */
1126     if (*++colon != '\0') {
1127         if ((remote = inet_addr(colon)) == -1) {
1128             if ((hp = gethostbyname(colon)) == NULL) {
1129                 fprintf(stderr, "unknown host: %s\n", colon);
1130                 return -1;
1131             } else {
1132                 remote = *(long *)hp->h_addr;
1133                 if (remote_name[0] == 0) {
1134                     strncpy(remote_name, colon, MAXNAMELEN);
1135                     remote_name[MAXNAMELEN-1] = 0;
1136                 }
1137             }
1138         }
1139         if (bad_ip_adrs(remote)) {
1140             fprintf(stderr, "bad remote IP address %s\n", ip_ntoa(remote));
1141             return -1;
1142         }
1143         if (remote != 0)
1144             wo->hisaddr = remote;
1145     }
1146
1147     return 1;
1148 }
1149
1150
1151 /*
1152  * setnoipdflt - disable setipdefault()
1153  */
1154 static int
1155 setnoipdflt()
1156 {
1157     disable_defaultip = 1;
1158     return 1;
1159 }
1160
1161
1162 /*
1163  * setipcpaccl - accept peer's idea of our address
1164  */
1165 static int
1166 setipcpaccl()
1167 {
1168     ipcp_wantoptions[0].accept_local = 1;
1169     return 1;
1170 }
1171
1172
1173 /*
1174  * setipcpaccr - accept peer's idea of its address
1175  */
1176 static int
1177 setipcpaccr()
1178 {
1179     ipcp_wantoptions[0].accept_remote = 1;
1180     return 1;
1181 }
1182
1183
1184 /*
1185  * setipdefault - default our local IP address based on our hostname.
1186  */
1187 void
1188 setipdefault()
1189 {
1190     struct hostent *hp;
1191     u_int32_t local;
1192     ipcp_options *wo = &ipcp_wantoptions[0];
1193
1194     /*
1195      * If local IP address already given, don't bother.
1196      */
1197     if (wo->ouraddr != 0 || disable_defaultip)
1198         return;
1199
1200     /*
1201      * Look up our hostname (possibly with domain name appended)
1202      * and take the first IP address as our local IP address.
1203      * If there isn't an IP address for our hostname, too bad.
1204      */
1205     wo->accept_local = 1;       /* don't insist on this default value */
1206     if ((hp = gethostbyname(hostname)) == NULL)
1207         return;
1208     local = *(long *)hp->h_addr;
1209     if (local != 0 && !bad_ip_adrs(local))
1210         wo->ouraddr = local;
1211 }
1212
1213
1214 /*
1215  * setnetmask - set the netmask to be used on the interface.
1216  */
1217 static int
1218 setnetmask(argv)
1219     char **argv;
1220 {
1221     u_int32_t mask;
1222
1223     if ((mask = inet_addr(*argv)) == -1 || (netmask & ~mask) != 0) {
1224         fprintf(stderr, "Invalid netmask %s\n", *argv);
1225         return 0;
1226     }
1227
1228     netmask = mask;
1229     return (1);
1230 }
1231
1232 /*
1233  * Return user specified netmask. A value of zero means no netmask has
1234  * been set. 
1235  */
1236 /* ARGSUSED */
1237 u_int32_t
1238 GetMask(addr)
1239     u_int32_t addr;
1240 {
1241     return(netmask);
1242 }
1243
1244
1245 static int
1246 setcrtscts()
1247 {
1248     crtscts = 1;
1249     return (1);
1250 }
1251
1252 static int
1253 setnocrtscts()
1254 {
1255     crtscts = -1;
1256     return (1);
1257 }
1258
1259 static int
1260 setxonxoff()
1261 {
1262     crtscts = 2;
1263     return (1);
1264 }
1265
1266 static int
1267 setnodetach()
1268 {
1269     nodetach = 1;
1270     return (1);
1271 }
1272
1273 static int
1274 setmodem()
1275 {
1276     modem = 1;
1277     return 1;
1278 }
1279
1280 static int
1281 setlocal()
1282 {
1283     modem = 0;
1284     return 1;
1285 }
1286
1287 static int
1288 setlock()
1289 {
1290     lockflag = 1;
1291     return 1;
1292 }
1293
1294 static int
1295 setusehostname()
1296 {
1297     usehostname = 1;
1298     return 1;
1299 }
1300
1301 static int
1302 setname(argv)
1303     char **argv;
1304 {
1305     if (our_name[0] == 0) {
1306         strncpy(our_name, argv[0], MAXNAMELEN);
1307         our_name[MAXNAMELEN-1] = 0;
1308     }
1309     return 1;
1310 }
1311
1312 static int
1313 setuser(argv)
1314     char **argv;
1315 {
1316     strncpy(user, argv[0], MAXNAMELEN);
1317     user[MAXNAMELEN-1] = 0;
1318     return 1;
1319 }
1320
1321 static int
1322 setremote(argv)
1323     char **argv;
1324 {
1325     strncpy(remote_name, argv[0], MAXNAMELEN);
1326     remote_name[MAXNAMELEN-1] = 0;
1327     return 1;
1328 }
1329
1330 static int
1331 setauth()
1332 {
1333     auth_required = 1;
1334     return 1;
1335 }
1336
1337 static int
1338 setdefaultroute()
1339 {
1340     ipcp_wantoptions[0].default_route = 1;
1341     return 1;
1342 }
1343
1344 static int
1345 setproxyarp()
1346 {
1347     ipcp_wantoptions[0].proxy_arp = 1;
1348     return 1;
1349 }
1350
1351 static int
1352 setpersist()
1353 {
1354     persist = 1;
1355     return 1;
1356 }
1357
1358 static int
1359 setdologin()
1360 {
1361     uselogin = 1;
1362     return 1;
1363 }
1364
1365 /*
1366  * Functions to set the echo interval for modem-less monitors
1367  */
1368
1369 static int
1370 setlcpechointv(argv)
1371     char **argv;
1372 {
1373     return int_option(*argv, &lcp_echo_interval);
1374 }
1375
1376 static int
1377 setlcpechofails(argv)
1378     char **argv;
1379 {
1380     return int_option(*argv, &lcp_echo_fails);
1381 }
1382
1383 /*
1384  * Functions to set timeouts, max transmits, etc.
1385  */
1386 static int
1387 setlcptimeout(argv)
1388     char **argv;
1389 {
1390     return int_option(*argv, &lcp_fsm[0].timeouttime);
1391 }
1392
1393 static int setlcpterm(argv)
1394     char **argv;
1395 {
1396     return int_option(*argv, &lcp_fsm[0].maxtermtransmits);
1397 }
1398
1399 static int setlcpconf(argv)
1400     char **argv;
1401 {
1402     return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits);
1403 }
1404
1405 static int setlcpfails(argv)
1406     char **argv;
1407 {
1408     return int_option(*argv, &lcp_fsm[0].maxnakloops);
1409 }
1410
1411 static int setipcptimeout(argv)
1412     char **argv;
1413 {
1414     return int_option(*argv, &ipcp_fsm[0].timeouttime);
1415 }
1416
1417 static int setipcpterm(argv)
1418     char **argv;
1419 {
1420     return int_option(*argv, &ipcp_fsm[0].maxtermtransmits);
1421 }
1422
1423 static int setipcpconf(argv)
1424     char **argv;
1425 {
1426     return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits);
1427 }
1428
1429 static int setipcpfails(argv)
1430     char **argv;
1431 {
1432     return int_option(*argv, &lcp_fsm[0].maxnakloops);
1433 }
1434
1435 static int setpaptimeout(argv)
1436     char **argv;
1437 {
1438     return int_option(*argv, &upap[0].us_timeouttime);
1439 }
1440
1441 static int setpapreqs(argv)
1442     char **argv;
1443 {
1444     return int_option(*argv, &upap[0].us_maxtransmits);
1445 }
1446
1447 static int setchaptimeout(argv)
1448     char **argv;
1449 {
1450     return int_option(*argv, &chap[0].timeouttime);
1451 }
1452
1453 static int setchapchal(argv)
1454     char **argv;
1455 {
1456     return int_option(*argv, &chap[0].max_transmits);
1457 }
1458
1459 static int setchapintv(argv)
1460     char **argv;
1461 {
1462     return int_option(*argv, &chap[0].chal_interval);
1463 }
1464
1465 static int setbsdcomp(argv)
1466     char **argv;
1467 {
1468     int rbits, abits;
1469     char *str, *endp;
1470
1471     str = *argv;
1472     abits = rbits = strtol(str, &endp, 0);
1473     if (endp != str && *endp == ',') {
1474         str = endp + 1;
1475         abits = strtol(str, &endp, 0);
1476     }
1477     if (*endp != 0 || endp == str) {
1478         fprintf(stderr, "%s: invalid argument format for bsdcomp option\n",
1479                 progname);
1480         return 0;
1481     }
1482     if (rbits != 0 && (rbits < MIN_BSD_BITS || rbits > MAX_BSD_BITS)
1483         || abits != 0 && (abits < MIN_BSD_BITS || abits > MAX_BSD_BITS)) {
1484         fprintf(stderr, "%s: bsdcomp option values must be 0 or %d .. %d\n",
1485                 progname, MIN_BSD_BITS, MAX_BSD_BITS);
1486         return 0;
1487     }
1488     if (rbits > 0) {
1489         ccp_wantoptions[0].bsd_compress = 1;
1490         ccp_wantoptions[0].bsd_bits = rbits;
1491     } else
1492         ccp_wantoptions[0].bsd_compress = 0;
1493     if (abits > 0) {
1494         ccp_allowoptions[0].bsd_compress = 1;
1495         ccp_allowoptions[0].bsd_bits = abits;
1496     } else
1497         ccp_allowoptions[0].bsd_compress = 0;
1498     return 1;
1499 }
1500
1501 static int setnobsdcomp()
1502 {
1503     ccp_wantoptions[0].bsd_compress = 0;
1504     ccp_allowoptions[0].bsd_compress = 0;
1505 }