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