2 * options.c - handles option processing for PPP.
4 * Copyright (c) 1989 Carnegie Mellon University.
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.
21 static char rcsid[] = "$Id: options.c,v 1.2 1993/11/23 23:40:53 paulus Exp $";
32 #include <sys/types.h>
37 #include "pathnames.h"
38 #include "patchlevel.h"
52 static int setdebug __ARGS((void));
53 static int setpassive __ARGS((void));
54 static int setsilent __ARGS((void));
55 static int noopt __ARGS((void));
56 static int setnovj __ARGS((void));
57 static int reqpap __ARGS((void));
58 static int nopap __ARGS((void));
59 static int setupapfile __ARGS((char **));
60 static int nochap __ARGS((void));
61 static int reqchap __ARGS((void));
62 static int setspeed __ARGS((char *));
63 static int noaccomp __ARGS((void));
64 static int noasyncmap __ARGS((void));
65 static int noipaddr __ARGS((void));
66 static int nomagicnumber __ARGS((void));
67 static int setasyncmap __ARGS((char **));
68 static int setmru __ARGS((char **));
69 static int nomru __ARGS((void));
70 static int nopcomp __ARGS((void));
71 static int setconnector __ARGS((char **));
72 static int setdomain __ARGS((char **));
73 static int setnetmask __ARGS((char **));
74 static int setcrtscts __ARGS((void));
75 static int setnodetach __ARGS((void));
76 static int setmodem __ARGS((void));
77 static int setlocal __ARGS((void));
78 static int setname __ARGS((char **));
79 static int setuser __ARGS((char **));
80 static int setremote __ARGS((char **));
81 static int setauth __ARGS((void));
82 static int readfile __ARGS((char **));
83 static int setdefaultroute __ARGS((void));
84 static int setproxyarp __ARGS((void));
85 static int setpersist __ARGS((void));
86 static int setdologin __ARGS((void));
87 static int setusehostname __ARGS((void));
88 static int setlcptimeout __ARGS((char **));
89 static int setlcpterm __ARGS((char **));
90 static int setlcpconf __ARGS((char **));
91 static int setlcpfails __ARGS((char **));
92 static int setipcptimeout __ARGS((char **));
93 static int setipcpterm __ARGS((char **));
94 static int setipcpconf __ARGS((char **));
95 static int setipcpfails __ARGS((char **));
96 static int setpaptimeout __ARGS((char **));
97 static int setpapreqs __ARGS((char **));
98 static int setchaptimeout __ARGS((char **));
99 static int setchapchal __ARGS((char **));
100 static int setchapintv __ARGS((char **));
102 static int number_option __ARGS((char *, long *, int));
108 extern char *progname;
113 extern char *connector;
115 extern char devname[];
116 extern int default_device;
117 extern u_long netmask;
120 extern char passwd[];
121 extern int auth_required;
125 extern char our_name[];
126 extern char remote_name[];
137 "-all", 0, noopt, /* Don't request/allow any options */
138 "-ac", 0, noaccomp, /* Disable Address/Control compress */
139 "-am", 0, noasyncmap, /* Disable asyncmap negotiation */
140 "-as", 1, setasyncmap, /* set the desired async map */
141 "-d", 0, setdebug, /* Increase debugging level */
142 "-detach", 0, setnodetach, /* don't fork */
143 "-ip", 0, noipaddr, /* Disable IP address negotiation */
144 "-mn", 0, nomagicnumber, /* Disable magic number negotiation */
145 "-mru", 0, nomru, /* Disable mru negotiation */
146 "-p", 0, setpassive, /* Set passive mode */
147 "-pc", 0, nopcomp, /* Disable protocol field compress */
148 "+ua", 1, setupapfile, /* Get PAP user and password from file */
149 "+pap", 0, reqpap, /* Require PAP auth from peer */
150 "-pap", 0, nopap, /* Don't allow UPAP authentication with peer */
151 "+chap", 0, reqchap, /* Require CHAP authentication from peer */
152 "-chap", 0, nochap, /* Don't allow CHAP authentication with peer */
153 "-vj", 0, setnovj, /* disable VJ compression */
154 "asyncmap", 1, setasyncmap, /* set the desired async map */
155 "connect", 1, setconnector, /* A program to set up a connection */
156 "crtscts", 0, setcrtscts, /* set h/w flow control */
157 "debug", 0, setdebug, /* Increase debugging level */
158 "domain", 1, setdomain, /* Add given domain name to hostname*/
159 "mru", 1, setmru, /* Set MRU value for negotiation */
160 "netmask", 1, setnetmask, /* set netmask */
161 "passive", 0, setpassive, /* Set passive mode */
162 "silent", 0, setsilent, /* Set silent mode */
163 "modem", 0, setmodem, /* Use modem control lines */
164 "local", 0, setlocal, /* Don't use modem control lines */
165 "name", 1, setname, /* Set local name for authentication */
166 "user", 1, setuser, /* Set username for PAP auth with peer */
167 "usehostname", 0, setusehostname, /* Must use hostname for auth. */
168 "remotename", 1, setremote, /* Set remote name for authentication */
169 "auth", 0, setauth, /* Require authentication from peer */
170 "file", 1, readfile, /* Take options from a file */
171 "defaultroute", 0, setdefaultroute, /* Add default route */
172 "proxyarp", 0, setproxyarp, /* Add proxy ARP entry */
173 "persist", 0, setpersist, /* Keep on reopening connection after close */
174 "login", 0, setdologin, /* Use system password database for UPAP */
175 "lcp-restart", 1, setlcptimeout, /* Set timeout for LCP */
176 "lcp-max-terminate", 1, setlcpterm, /* Set max #xmits for term-reqs */
177 "lcp-max-configure", 1, setlcpconf, /* Set max #xmits for conf-reqs */
178 "lcp-max-failure", 1, setlcpfails, /* Set max #conf-naks for LCP */
179 "ipcp-restart", 1, setipcptimeout, /* Set timeout for IPCP */
180 "ipcp-max-terminate", 1, setipcpterm, /* Set max #xmits for term-reqs */
181 "ipcp-max-configure", 1, setipcpconf, /* Set max #xmits for conf-reqs */
182 "ipcp-max-failure", 1, setipcpfails, /* Set max #conf-naks for IPCP */
183 "pap-restart", 1, setpaptimeout, /* Set timeout for UPAP */
184 "pap-max-authreq", 1, setpapreqs, /* Set max #xmits for auth-reqs */
185 "chap-restart", 1, setchaptimeout, /* Set timeout for CHAP */
186 "chap-max-challenge", 1, setchapchal, /* Set max #xmits for challenge */
187 "chap-interval", 1, setchapintv, /* Set interval for rechallenge */
192 static char *usage_string = "\
193 pppd version %s patch level %d\n\
194 Usage: %s [ arguments ], where arguments are:\n\
195 <device> Communicate over the named device\n\
196 <speed> Set the baud rate to <speed>\n\
197 <loc>:<rem> Set the local and/or remote interface IP\n\
198 addresses. Either one may be omitted.\n\
199 asyncmap <n> Set the desired async map to hex <n>\n\
200 auth Require authentication from peer\n\
201 connect <p> Invoke shell command <p> to set up the serial line\n\
202 crtscts Use hardware RTS/CTS flow control\n\
203 defaultroute Add default route through interface\n\
204 file <f> Take options from file <f>\n\
205 modem Use modem control lines\n\
206 mru <n> Set MRU value to <n> for negotiation\n\
207 netmask <n> Set interface netmask to <n>\n\
208 See pppd(8) for more options.\n\
213 -all Don't request/allow any options\n\
214 -ac Disable Address/Control compression\n\
215 -am Disable asyncmap negotiation\n\
216 -as <n> Set the desired async map to hex <n>\n\
217 -d Increase debugging level\n\
218 -detach Don't fork to background\n\
219 -ip Disable IP address negotiation\n\
220 -mn Disable magic number negotiation\n\
221 -mru Disable mru negotiation\n\
222 -p Set passive mode\n\
223 -pc Disable protocol field compression\n\
224 +ua <f> Get username and password for authenticating\n\
225 with peer using PAP from file <f>\n\
226 +pap Require PAP authentication from peer\n\
227 -pap Don't agree to authenticating with peer using PAP\n\
228 +chap Require CHAP authentication from peer\n\
229 -chap Don't agree to authenticating with peer using CHAP\n\
230 -vj disable VJ compression\n\
231 -auth Don't agree to authenticate with peer\n\
232 debug Increase debugging level\n\
233 domain <d> Append domain name <d> to hostname for authentication\n\
234 passive Set passive mode\n\
235 local Don't use modem control lines\n\
236 proxyarp Add proxy ARP entry\n\
241 * parse_args - parse a string of arguments, from the command
242 * line or from a file.
245 parse_args(argc, argv)
257 * First see if it's a command.
259 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
260 if (!strcmp(arg, cmdp->cmd_name))
263 if (cmdp->cmd_name != NULL) {
264 if (argc < cmdp->num_args) {
265 fprintf(stderr, "Too few parameters for command %s\n", arg);
268 if (!(*cmdp->cmd_func)(argv))
270 argc -= cmdp->num_args;
271 argv += cmdp->num_args;
275 * Maybe a tty name, speed or IP address?
277 if (!setdevname(arg) && !setspeed(arg) && !setipaddr(arg)) {
278 fprintf(stderr, "%s: unrecognized command\n", arg);
288 * usage - print out a message telling how to use the program.
292 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, progname);
296 * options_from_file - Read a string of options from a file,
297 * and interpret them.
300 options_from_file(filename, must_exist)
308 char args[MAXARGS][MAXWORDLEN];
309 char cmd[MAXWORDLEN];
311 if ((f = fopen(filename, "r")) == NULL) {
312 if (!must_exist && errno == ENOENT)
317 while (getword(f, cmd, &newline, filename)) {
319 * First see if it's a command.
321 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
322 if (!strcmp(cmd, cmdp->cmd_name))
325 if (cmdp->cmd_name != NULL) {
326 for (i = 0; i < cmdp->num_args; ++i) {
327 if (!getword(f, args[i], &newline, filename)) {
329 "In file %s: too few parameters for command %s\n",
336 if (!(*cmdp->cmd_func)(argv)) {
343 * Maybe a tty name, speed or IP address?
345 if (!setdevname(cmd) && !setspeed(cmd) && !setipaddr(cmd)) {
346 fprintf(stderr, "In file %s: unrecognized command %s\n",
357 * options_from_user - See if the use has a ~/.ppprc file,
358 * and if so, interpret options from it.
363 char *user, *path, *file;
366 if ((user = getenv("HOME")) == NULL)
369 path = malloc(strlen(user) + strlen(file) + 1);
371 novm("init file name");
374 ret = options_from_file(path, 0);
380 * Read a word from a file.
381 * Words are delimited by white-space or by quotes (").
382 * Quotes, white-space and \ may be escaped with \.
383 * \<newline> is ignored.
386 getword(f, word, newlinep, filename)
401 * First skip white-space and comments
403 while ((c = getc(f)) != EOF) {
406 * \<newline> is ignored; \ followed by anything else
409 if ((c = getc(f)) == '\n')
416 *newlinep = 1; /* next word starts a line */
418 /* comment - ignore until EOF or \n */
419 while ((c = getc(f)) != EOF && c != '\n')
424 } else if (!isspace(c))
429 * End of file or error - fail
441 * Is this character escaped by \ ?
445 --len; /* ignore \<newline> */
446 else if (c == '"' || isspace(c) || c == '\\')
447 word[len-1] = c; /* put special char in word */
449 if (len < MAXWORDLEN-1)
454 } else if (c == '"') {
456 } else if (!quoted && (isspace(c) || c == '#')) {
460 if (len < MAXWORDLEN-1)
466 if ((c = getc(f)) == EOF)
475 if (len >= MAXWORDLEN) {
476 word[MAXWORDLEN-1] = 0;
477 fprintf(stderr, "%s: warning: word in file %s too long (%.20s...)\n",
478 progname, filename, word);
486 * number_option - parse a numeric parameter for an option
489 number_option(str, valp, base)
496 *valp = strtol(str, &ptr, base);
498 fprintf(stderr, "%s: invalid number: %s\n", progname, str);
506 * int_option - like number_option, but valp is int *,
507 * the base is assumed to be 0, and *valp is not changed
508 * if there is an error.
511 int_option(str, valp)
517 if (!number_option(str, &v, 0))
525 * The following procedures execute commands.
529 * readfile - take commands from a file.
535 return options_from_file(*argv, 1);
539 * setdebug - Set debug (command line argument).
545 setlogmask(LOG_UPTO(LOG_DEBUG));
550 * noopt - Disable all options.
555 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
556 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
557 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
558 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
563 * noaccomp - Disable Address/Control field compression negotiation.
568 lcp_wantoptions[0].neg_accompression = 0;
569 lcp_allowoptions[0].neg_accompression = 0;
575 * noasyncmap - Disable async map negotiation.
580 lcp_wantoptions[0].neg_asyncmap = 0;
581 lcp_allowoptions[0].neg_asyncmap = 0;
587 * noipaddr - Disable IP address negotiation.
592 ipcp_wantoptions[0].neg_addr = 0;
593 ipcp_allowoptions[0].neg_addr = 0;
599 * nomagicnumber - Disable magic number negotiation.
604 lcp_wantoptions[0].neg_magicnumber = 0;
605 lcp_allowoptions[0].neg_magicnumber = 0;
611 * nomru - Disable mru negotiation.
616 lcp_wantoptions[0].neg_mru = 0;
617 lcp_allowoptions[0].neg_mru = 0;
623 * setmru - Set MRU for negotiation.
631 if (!number_option(*argv, &mru, 0))
633 lcp_wantoptions[0].mru = mru;
634 lcp_wantoptions[0].neg_mru = 1;
640 * nopcomp - Disable Protocol field compression negotiation.
645 lcp_wantoptions[0].neg_pcompression = 0;
646 lcp_allowoptions[0].neg_pcompression = 0;
652 * setpassive - Set passive mode (don't give up if we time out sending
653 * LCP configure-requests).
658 lcp_wantoptions[0].passive = 1;
664 * setsilent - Set silent mode (don't start sending LCP configure-requests
665 * until we get one from the peer).
670 lcp_wantoptions[0].silent = 1;
676 * nopap - Disable PAP authentication with peer.
681 lcp_allowoptions[0].neg_upap = 0;
687 * reqpap - Require PAP authentication from peer.
692 lcp_wantoptions[0].neg_upap = 1;
698 * setupapfile - specifies UPAP info for authenticating with peer.
707 lcp_allowoptions[0].neg_upap = 1;
709 /* open user info file */
710 if ((ufile = fopen(*argv, "r")) == NULL) {
711 fprintf(stderr, "unable to open user login data file %s\n", *argv);
714 check_access(ufile, *argv);
717 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
718 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
719 fprintf(stderr, "Unable to read user login data file %s.\n", *argv);
724 /* get rid of newlines */
726 if (l > 0 && user[l-1] == '\n')
729 if (l > 0 && passwd[l-1] == '\n')
737 * nochap - Disable CHAP authentication with peer.
742 lcp_allowoptions[0].neg_chap = 0;
748 * reqchap - Require CHAP authentication from peer.
753 lcp_wantoptions[0].neg_chap = 1;
760 * setnovj - diable vj compression
765 ipcp_wantoptions[0].neg_vj = 0;
766 ipcp_allowoptions[0].neg_vj = 0;
771 * setconnector - Set a program to connect to a serial line
777 connector = strdup(*argv);
778 if (connector == NULL)
779 novm("connector string");
786 * setdomain - Set domain name to append to hostname
792 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
793 hostname[MAXNAMELEN-1] = 0;
803 if (!number_option(*argv, &asyncmap, 16))
805 lcp_wantoptions[0].asyncmap |= asyncmap;
806 lcp_wantoptions[0].neg_asyncmap = 1;
811 * setspeed - Set the speed.
820 spd = strtol(arg, &ptr, 0);
821 if (ptr == arg || *ptr != 0 || spd == 0)
829 * setdevname - Set the device name.
836 char *tty, *ttyname();
837 char dev[MAXPATHLEN];
839 if (strncmp("/dev/", cp, 5) != 0) {
840 strcpy(dev, "/dev/");
841 strncat(dev, cp, MAXPATHLEN - 5);
842 dev[MAXPATHLEN-1] = 0;
847 * Check if there is a device by this name.
849 if (stat(cp, &statbuf) < 0) {
856 (void) strncpy(devname, cp, MAXPATHLEN);
857 devname[MAXPATHLEN-1] = 0;
858 default_device = FALSE;
865 * setipaddr - Set the IP address
872 char *colon, *index();
873 u_long local, remote;
874 ipcp_options *wo = &ipcp_wantoptions[0];
877 * IP address pair separated by ":".
879 if ((colon = index(arg, ':')) == NULL)
883 * If colon first character, then no local addr.
887 if ((local = inet_addr(arg)) == -1) {
888 if ((hp = gethostbyname(arg)) == NULL) {
889 fprintf(stderr, "unknown host: %s", arg);
892 local = *(long *)hp->h_addr;
893 if (our_name[0] == 0) {
894 strncpy(our_name, arg, MAXNAMELEN);
895 our_name[MAXNAMELEN-1] = 0;
905 * If colon last character, then no remote addr.
907 if (*++colon != '\0') {
908 if ((remote = inet_addr(colon)) == -1) {
909 if ((hp = gethostbyname(colon)) == NULL) {
910 fprintf(stderr, "unknown host: %s", colon);
913 remote = *(long *)hp->h_addr;
914 if (remote_name[0] == 0) {
915 strncpy(remote_name, colon, MAXNAMELEN);
916 remote_name[MAXNAMELEN-1] = 0;
921 wo->hisaddr = remote;
929 * setipdefault - default our local IP address based on our hostname.
936 ipcp_options *wo = &ipcp_wantoptions[0];
939 * If local IP address already given, don't bother.
941 if (wo->ouraddr != 0)
945 * Look up our hostname (possibly with domain name appended)
946 * and take the first IP address as our local IP address.
947 * If there isn't an IP address for our hostname, too bad.
949 if ((hp = gethostbyname(hostname)) == NULL)
951 local = *(long *)hp->h_addr;
958 * setnetmask - set the netmask to be used on the interface.
966 if ((mask = inet_addr(*argv)) == -1) {
967 fprintf(stderr, "Invalid netmask %s\n", *argv);
1014 if (our_name[0] == 0) {
1015 strncpy(our_name, argv[0], MAXNAMELEN);
1016 our_name[MAXNAMELEN-1] = 0;
1025 strncpy(user, argv[0], MAXNAMELEN);
1026 user[MAXNAMELEN-1] = 0;
1034 strncpy(remote_name, argv[0], MAXNAMELEN);
1035 remote_name[MAXNAMELEN-1] = 0;
1049 ipcp_wantoptions[0].default_route = 1;
1056 ipcp_wantoptions[0].proxy_arp = 1;
1075 * Functions to set timeouts, max transmits, etc.
1081 return int_option(*argv, &lcp_fsm[0].timeouttime, 0);
1084 static int setlcpterm(argv)
1087 return int_option(*argv, &lcp_fsm[0].maxtermtransmits, 0);
1090 static int setlcpconf(argv)
1093 return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits, 0);
1096 static int setlcpfails(argv)
1099 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1102 static int setipcptimeout(argv)
1105 return int_option(*argv, &ipcp_fsm[0].timeouttime, 0);
1108 static int setipcpterm(argv)
1111 return int_option(*argv, &ipcp_fsm[0].maxtermtransmits, 0);
1114 static int setipcpconf(argv)
1117 return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits, 0);
1120 static int setipcpfails(argv)
1123 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1126 static int setpaptimeout(argv)
1129 return int_option(*argv, &upap[0].us_timeouttime, 0);
1132 static int setpapreqs(argv)
1135 return int_option(*argv, &upap[0].us_maxtransmits, 0);
1138 static int setchaptimeout(argv)
1141 return int_option(*argv, &chap[0].timeouttime, 0);
1144 static int setchapchal(argv)
1147 return int_option(*argv, &chap[0].max_transmits, 0);
1150 static int setchapintv(argv)
1153 return int_option(*argv, &chap[0].chal_interval, 0);