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