]> git.ozlabs.org Git - ppp.git/blob - pppd/options.c
mention /etc/ppp/options.<ttyname>
[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.10 1994/05/27 00:43:34 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     if (!readable(fileno(ufile))) {
839         fprintf(stderr, "%s: access denied\n", *argv);
840         return 0;
841     }
842     check_access(ufile, *argv);
843
844     /* get username */
845     if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
846         || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
847         fprintf(stderr, "Unable to read user login data file %s.\n", *argv);
848         return 0;
849     }
850     fclose(ufile);
851
852     /* get rid of newlines */
853     l = strlen(user);
854     if (l > 0 && user[l-1] == '\n')
855         user[l-1] = 0;
856     l = strlen(passwd);
857     if (l > 0 && passwd[l-1] == '\n')
858         passwd[l-1] = 0;
859
860     return (1);
861 }
862
863
864 /*
865  * nochap - Disable CHAP authentication with peer.
866  */
867 static int
868 nochap()
869 {
870     lcp_allowoptions[0].neg_chap = 0;
871     return (1);
872 }
873
874
875 /*
876  * reqchap - Require CHAP authentication from peer.
877  */
878 static int
879 reqchap()
880 {
881     lcp_wantoptions[0].neg_chap = 1;
882     auth_required = 1;
883     return (1);
884 }
885
886
887 /*
888  * setnovj - disable vj compression
889  */
890 static int
891 setnovj()
892 {
893     ipcp_wantoptions[0].neg_vj = 0;
894     ipcp_allowoptions[0].neg_vj = 0;
895     return (1);
896 }
897
898
899 /*
900  * setnovjccomp - disable VJ connection-ID compression
901  */
902 static int
903 setnovjccomp()
904 {
905     ipcp_wantoptions[0].cflag = 0;
906     ipcp_allowoptions[0].cflag = 0;
907 }
908
909
910 /*
911  * setvjslots - set maximum number of connection slots for VJ compression
912  */
913 static int
914 setvjslots(argv)
915     char **argv;
916 {
917     int value;
918
919     if (!int_option(*argv, &value))
920         return 0;
921     if (value < 2 || value > 16) {
922         fprintf(stderr, "pppd: vj-max-slots value must be between 2 and 16\n");
923         return 0;
924     }
925     ipcp_wantoptions [0].maxslotindex =
926         ipcp_allowoptions[0].maxslotindex = value - 1;
927     return 1;
928 }
929
930
931 /*
932  * setconnector - Set a program to connect to a serial line
933  */
934 static int
935 setconnector(argv)
936     char **argv;
937 {
938     connector = strdup(*argv);
939     if (connector == NULL)
940         novm("connector string");
941   
942     return (1);
943 }
944
945 /*
946  * setdisconnector - Set a program to disconnect from the serial line
947  */
948 static int
949 setdisconnector(argv)
950     char **argv;
951 {
952     disconnector = strdup(*argv);
953     if (disconnector == NULL)
954         novm("disconnector string");
955   
956     return (1);
957 }
958
959
960 /*
961  * setdomain - Set domain name to append to hostname 
962  */
963 static int
964 setdomain(argv)
965     char **argv;
966 {
967     strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
968     hostname[MAXNAMELEN-1] = 0;
969     return (1);
970 }
971
972
973 /*
974  * setasyncmap - add bits to asyncmap (what we request peer to escape).
975  */
976 static int
977 setasyncmap(argv)
978     char **argv;
979 {
980     long asyncmap;
981
982     if (!number_option(*argv, &asyncmap, 16))
983         return 0;
984     lcp_wantoptions[0].asyncmap |= asyncmap;
985     lcp_wantoptions[0].neg_asyncmap = 1;
986     return(1);
987 }
988
989
990 /*
991  * setescape - add chars to the set we escape on transmission.
992  */
993 static int
994 setescape(argv)
995     char **argv;
996 {
997     int n, ret;
998     char *p, *endp;
999
1000     p = *argv;
1001     ret = 1;
1002     while (*p) {
1003         n = strtol(p, &endp, 16);
1004         if (p == endp) {
1005             fprintf(stderr, "%s: invalid hex number: %s\n", progname, p);
1006             return 0;
1007         }
1008         p = endp;
1009         if (n < 0 || 0x20 <= n && n <= 0x3F || n == 0x5E || n > 0xFF) {
1010             fprintf(stderr, "%s: can't escape character 0x%x\n", n);
1011             ret = 0;
1012         } else
1013             xmit_accm[0][n >> 5] |= 1 << (n & 0x1F);
1014         while (*p == ',' || *p == ' ')
1015             ++p;
1016     }
1017     return ret;
1018 }
1019
1020
1021 /*
1022  * setspeed - Set the speed.
1023  */
1024 static int
1025 setspeed(arg)
1026     char *arg;
1027 {
1028     char *ptr;
1029     int spd;
1030
1031     spd = strtol(arg, &ptr, 0);
1032     if (ptr == arg || *ptr != 0 || spd == 0)
1033         return 0;
1034     inspeed = spd;
1035     return 1;
1036 }
1037
1038
1039 /*
1040  * setdevname - Set the device name.
1041  */
1042 int
1043 setdevname(cp)
1044     char *cp;
1045 {
1046     struct stat statbuf;
1047     char *tty, *ttyname();
1048     char dev[MAXPATHLEN];
1049   
1050     if (strncmp("/dev/", cp, 5) != 0) {
1051         strcpy(dev, "/dev/");
1052         strncat(dev, cp, MAXPATHLEN - 5);
1053         dev[MAXPATHLEN-1] = 0;
1054         cp = dev;
1055     }
1056
1057     /*
1058      * Check if there is a device by this name.
1059      */
1060     if (stat(cp, &statbuf) < 0) {
1061         if (errno == ENOENT)
1062             return 0;
1063         syslog(LOG_ERR, cp);
1064         return -1;
1065     }
1066   
1067     (void) strncpy(devname, cp, MAXPATHLEN);
1068     devname[MAXPATHLEN-1] = 0;
1069     default_device = FALSE;
1070   
1071     return 1;
1072 }
1073
1074
1075 /*
1076  * setipaddr - Set the IP address
1077  */
1078 int
1079 setipaddr(arg)
1080     char *arg;
1081 {
1082     struct hostent *hp;
1083     char *colon, *index();
1084     u_long local, remote;
1085     ipcp_options *wo = &ipcp_wantoptions[0];
1086   
1087     /*
1088      * IP address pair separated by ":".
1089      */
1090     if ((colon = index(arg, ':')) == NULL)
1091         return 0;
1092   
1093     /*
1094      * If colon first character, then no local addr.
1095      */
1096     if (colon != arg) {
1097         *colon = '\0';
1098         if ((local = inet_addr(arg)) == -1) {
1099             if ((hp = gethostbyname(arg)) == NULL) {
1100                 fprintf(stderr, "unknown host: %s\n", arg);
1101                 return -1;
1102             } else {
1103                 local = *(long *)hp->h_addr;
1104                 if (our_name[0] == 0) {
1105                     strncpy(our_name, arg, MAXNAMELEN);
1106                     our_name[MAXNAMELEN-1] = 0;
1107                 }
1108             }
1109         }
1110         if (bad_ip_adrs(local)) {
1111             fprintf(stderr, "bad local IP address %s\n", ip_ntoa(local));
1112             return -1;
1113         }
1114         if (local != 0)
1115             wo->ouraddr = local;
1116         *colon = ':';
1117     }
1118   
1119     /*
1120      * If colon last character, then no remote addr.
1121      */
1122     if (*++colon != '\0') {
1123         if ((remote = inet_addr(colon)) == -1) {
1124             if ((hp = gethostbyname(colon)) == NULL) {
1125                 fprintf(stderr, "unknown host: %s\n", colon);
1126                 return -1;
1127             } else {
1128                 remote = *(long *)hp->h_addr;
1129                 if (remote_name[0] == 0) {
1130                     strncpy(remote_name, colon, MAXNAMELEN);
1131                     remote_name[MAXNAMELEN-1] = 0;
1132                 }
1133             }
1134         }
1135         if (bad_ip_adrs(remote)) {
1136             fprintf(stderr, "bad remote IP address %s\n", ip_ntoa(remote));
1137             return -1;
1138         }
1139         if (remote != 0)
1140             wo->hisaddr = remote;
1141     }
1142
1143     return 1;
1144 }
1145
1146
1147 /*
1148  * setnoipdflt - disable setipdefault()
1149  */
1150 static int
1151 setnoipdflt()
1152 {
1153     disable_defaultip = 1;
1154     return 1;
1155 }
1156
1157
1158 /*
1159  * setipcpaccl - accept peer's idea of our address
1160  */
1161 static int
1162 setipcpaccl()
1163 {
1164     ipcp_wantoptions[0].accept_local = 1;
1165     return 1;
1166 }
1167
1168
1169 /*
1170  * setipcpaccr - accept peer's idea of its address
1171  */
1172 static int
1173 setipcpaccr()
1174 {
1175     ipcp_wantoptions[0].accept_remote = 1;
1176     return 1;
1177 }
1178
1179
1180 /*
1181  * setipdefault - default our local IP address based on our hostname.
1182  */
1183 void
1184 setipdefault()
1185 {
1186     struct hostent *hp;
1187     u_long local;
1188     ipcp_options *wo = &ipcp_wantoptions[0];
1189
1190     /*
1191      * If local IP address already given, don't bother.
1192      */
1193     if (wo->ouraddr != 0 || disable_defaultip)
1194         return;
1195
1196     /*
1197      * Look up our hostname (possibly with domain name appended)
1198      * and take the first IP address as our local IP address.
1199      * If there isn't an IP address for our hostname, too bad.
1200      */
1201     wo->accept_local = 1;       /* don't insist on this default value */
1202     if ((hp = gethostbyname(hostname)) == NULL)
1203         return;
1204     local = *(long *)hp->h_addr;
1205     if (local != 0 && !bad_ip_adrs(local))
1206         wo->ouraddr = local;
1207 }
1208
1209
1210 /*
1211  * setnetmask - set the netmask to be used on the interface.
1212  */
1213 static int
1214 setnetmask(argv)
1215     char **argv;
1216 {
1217     u_long mask;
1218
1219     if ((mask = inet_addr(*argv)) == -1 || (netmask & ~mask) != 0) {
1220         fprintf(stderr, "Invalid netmask %s\n", *argv);
1221         return 0;
1222     }
1223
1224     netmask = mask;
1225     return (1);
1226 }
1227
1228 /*
1229  * Return user specified netmask. A value of zero means no netmask has
1230  * been set. 
1231  */
1232 /* ARGSUSED */
1233 u_long
1234 GetMask(addr)
1235     u_long addr;
1236 {
1237     return(netmask);
1238 }
1239
1240
1241 static int
1242 setcrtscts()
1243 {
1244     crtscts = 1;
1245     return (1);
1246 }
1247
1248 static int
1249 setxonxoff()
1250 {
1251     crtscts = 2;
1252     return (1);
1253 }
1254
1255 static int
1256 setnodetach()
1257 {
1258     nodetach = 1;
1259     return (1);
1260 }
1261
1262 static int
1263 setmodem()
1264 {
1265     modem = 1;
1266     return 1;
1267 }
1268
1269 static int
1270 setlocal()
1271 {
1272     modem = 0;
1273     return 1;
1274 }
1275
1276 static int
1277 setlock()
1278 {
1279     lockflag = 1;
1280     return 1;
1281 }
1282
1283 static int
1284 setusehostname()
1285 {
1286     usehostname = 1;
1287     return 1;
1288 }
1289
1290 static int
1291 setname(argv)
1292     char **argv;
1293 {
1294     if (our_name[0] == 0) {
1295         strncpy(our_name, argv[0], MAXNAMELEN);
1296         our_name[MAXNAMELEN-1] = 0;
1297     }
1298     return 1;
1299 }
1300
1301 static int
1302 setuser(argv)
1303     char **argv;
1304 {
1305     strncpy(user, argv[0], MAXNAMELEN);
1306     user[MAXNAMELEN-1] = 0;
1307     return 1;
1308 }
1309
1310 static int
1311 setremote(argv)
1312     char **argv;
1313 {
1314     strncpy(remote_name, argv[0], MAXNAMELEN);
1315     remote_name[MAXNAMELEN-1] = 0;
1316     return 1;
1317 }
1318
1319 static int
1320 setauth()
1321 {
1322     auth_required = 1;
1323     return 1;
1324 }
1325
1326 static int
1327 setdefaultroute()
1328 {
1329     ipcp_wantoptions[0].default_route = 1;
1330     return 1;
1331 }
1332
1333 static int
1334 setproxyarp()
1335 {
1336     ipcp_wantoptions[0].proxy_arp = 1;
1337     return 1;
1338 }
1339
1340 static int
1341 setpersist()
1342 {
1343     persist = 1;
1344     return 1;
1345 }
1346
1347 static int
1348 setdologin()
1349 {
1350     uselogin = 1;
1351     return 1;
1352 }
1353
1354 /*
1355  * Functions to set the echo interval for modem-less monitors
1356  */
1357
1358 static int
1359 setlcpechointv(argv)
1360     char **argv;
1361 {
1362     return int_option(*argv, &lcp_echo_interval);
1363 }
1364
1365 static int
1366 setlcpechofails(argv)
1367     char **argv;
1368 {
1369     return int_option(*argv, &lcp_echo_fails);
1370 }
1371
1372 /*
1373  * Functions to set timeouts, max transmits, etc.
1374  */
1375 static int
1376 setlcptimeout(argv)
1377     char **argv;
1378 {
1379     return int_option(*argv, &lcp_fsm[0].timeouttime);
1380 }
1381
1382 static int setlcpterm(argv)
1383     char **argv;
1384 {
1385     return int_option(*argv, &lcp_fsm[0].maxtermtransmits);
1386 }
1387
1388 static int setlcpconf(argv)
1389     char **argv;
1390 {
1391     return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits);
1392 }
1393
1394 static int setlcpfails(argv)
1395     char **argv;
1396 {
1397     return int_option(*argv, &lcp_fsm[0].maxnakloops);
1398 }
1399
1400 static int setipcptimeout(argv)
1401     char **argv;
1402 {
1403     return int_option(*argv, &ipcp_fsm[0].timeouttime);
1404 }
1405
1406 static int setipcpterm(argv)
1407     char **argv;
1408 {
1409     return int_option(*argv, &ipcp_fsm[0].maxtermtransmits);
1410 }
1411
1412 static int setipcpconf(argv)
1413     char **argv;
1414 {
1415     return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits);
1416 }
1417
1418 static int setipcpfails(argv)
1419     char **argv;
1420 {
1421     return int_option(*argv, &lcp_fsm[0].maxnakloops);
1422 }
1423
1424 static int setpaptimeout(argv)
1425     char **argv;
1426 {
1427     return int_option(*argv, &upap[0].us_timeouttime);
1428 }
1429
1430 static int setpapreqs(argv)
1431     char **argv;
1432 {
1433     return int_option(*argv, &upap[0].us_maxtransmits);
1434 }
1435
1436 static int setchaptimeout(argv)
1437     char **argv;
1438 {
1439     return int_option(*argv, &chap[0].timeouttime);
1440 }
1441
1442 static int setchapchal(argv)
1443     char **argv;
1444 {
1445     return int_option(*argv, &chap[0].max_transmits);
1446 }
1447
1448 static int setchapintv(argv)
1449     char **argv;
1450 {
1451     return int_option(*argv, &chap[0].chal_interval);
1452 }