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