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.4 1994/02/08 23:48:50 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 setnoipdflt __ARGS((void));
89 static int setlcptimeout __ARGS((char **));
90 static int setlcpterm __ARGS((char **));
91 static int setlcpconf __ARGS((char **));
92 static int setlcpfails __ARGS((char **));
93 static int setipcptimeout __ARGS((char **));
94 static int setipcpterm __ARGS((char **));
95 static int setipcpconf __ARGS((char **));
96 static int setipcpfails __ARGS((char **));
97 static int setpaptimeout __ARGS((char **));
98 static int setpapreqs __ARGS((char **));
99 static int setchaptimeout __ARGS((char **));
100 static int setchapchal __ARGS((char **));
101 static int setchapintv __ARGS((char **));
102 static int setipcpaccl __ARGS((void));
103 static int setipcpaccr __ARGS((void));
105 static int number_option __ARGS((char *, long *, int));
111 extern char *progname;
116 extern char *connector;
118 extern char devname[];
119 extern int default_device;
120 extern u_long netmask;
123 extern char passwd[];
124 extern int auth_required;
128 extern char our_name[];
129 extern char remote_name[];
131 int disable_defaultip;
141 "-all", 0, noopt, /* Don't request/allow any options */
142 "-ac", 0, noaccomp, /* Disable Address/Control compress */
143 "-am", 0, noasyncmap, /* Disable asyncmap negotiation */
144 "-as", 1, setasyncmap, /* set the desired async map */
145 "-d", 0, setdebug, /* Increase debugging level */
146 "-detach", 0, setnodetach, /* don't fork */
147 "-ip", 0, noipaddr, /* Disable IP address negotiation */
148 "-mn", 0, nomagicnumber, /* Disable magic number negotiation */
149 "-mru", 0, nomru, /* Disable mru negotiation */
150 "-p", 0, setpassive, /* Set passive mode */
151 "-pc", 0, nopcomp, /* Disable protocol field compress */
152 "+ua", 1, setupapfile, /* Get PAP user and password from file */
153 "+pap", 0, reqpap, /* Require PAP auth from peer */
154 "-pap", 0, nopap, /* Don't allow UPAP authentication with peer */
155 "+chap", 0, reqchap, /* Require CHAP authentication from peer */
156 "-chap", 0, nochap, /* Don't allow CHAP authentication with peer */
157 "-vj", 0, setnovj, /* disable VJ compression */
158 "asyncmap", 1, setasyncmap, /* set the desired async map */
159 "connect", 1, setconnector, /* A program to set up a connection */
160 "crtscts", 0, setcrtscts, /* set h/w flow control */
161 "debug", 0, setdebug, /* Increase debugging level */
162 "domain", 1, setdomain, /* Add given domain name to hostname*/
163 "mru", 1, setmru, /* Set MRU value for negotiation */
164 "netmask", 1, setnetmask, /* set netmask */
165 "passive", 0, setpassive, /* Set passive mode */
166 "silent", 0, setsilent, /* Set silent mode */
167 "modem", 0, setmodem, /* Use modem control lines */
168 "local", 0, setlocal, /* Don't use modem control lines */
169 "name", 1, setname, /* Set local name for authentication */
170 "user", 1, setuser, /* Set username for PAP auth with peer */
171 "usehostname", 0, setusehostname, /* Must use hostname for auth. */
172 "remotename", 1, setremote, /* Set remote name for authentication */
173 "auth", 0, setauth, /* Require authentication from peer */
174 "file", 1, readfile, /* Take options from a file */
175 "defaultroute", 0, setdefaultroute, /* Add default route */
176 "proxyarp", 0, setproxyarp, /* Add proxy ARP entry */
177 "persist", 0, setpersist, /* Keep on reopening connection after close */
178 "login", 0, setdologin, /* Use system password database for UPAP */
179 "noipdefault", 0, setnoipdflt, /* Don't use name for default IP adrs */
180 "lcp-restart", 1, setlcptimeout, /* Set timeout for LCP */
181 "lcp-max-terminate", 1, setlcpterm, /* Set max #xmits for term-reqs */
182 "lcp-max-configure", 1, setlcpconf, /* Set max #xmits for conf-reqs */
183 "lcp-max-failure", 1, setlcpfails, /* Set max #conf-naks for LCP */
184 "ipcp-restart", 1, setipcptimeout, /* Set timeout for IPCP */
185 "ipcp-max-terminate", 1, setipcpterm, /* Set max #xmits for term-reqs */
186 "ipcp-max-configure", 1, setipcpconf, /* Set max #xmits for conf-reqs */
187 "ipcp-max-failure", 1, setipcpfails, /* Set max #conf-naks for IPCP */
188 "pap-restart", 1, setpaptimeout, /* Set timeout for UPAP */
189 "pap-max-authreq", 1, setpapreqs, /* Set max #xmits for auth-reqs */
190 "chap-restart", 1, setchaptimeout, /* Set timeout for CHAP */
191 "chap-max-challenge", 1, setchapchal, /* Set max #xmits for challenge */
192 "chap-interval", 1, setchapintv, /* Set interval for rechallenge */
193 "ipcp-accept-local", 0, setipcpaccl, /* Accept peer's address for us */
194 "ipcp-accept-remote", 0, setipcpaccr, /* Accept peer's address for it */
199 static char *usage_string = "\
200 pppd version %s patch level %d\n\
201 Usage: %s [ arguments ], where arguments are:\n\
202 <device> Communicate over the named device\n\
203 <speed> Set the baud rate to <speed>\n\
204 <loc>:<rem> Set the local and/or remote interface IP\n\
205 addresses. Either one may be omitted.\n\
206 asyncmap <n> Set the desired async map to hex <n>\n\
207 auth Require authentication from peer\n\
208 connect <p> Invoke shell command <p> to set up the serial line\n\
209 crtscts Use hardware RTS/CTS flow control\n\
210 defaultroute Add default route through interface\n\
211 file <f> Take options from file <f>\n\
212 modem Use modem control lines\n\
213 mru <n> Set MRU value to <n> for negotiation\n\
214 netmask <n> Set interface netmask to <n>\n\
215 See pppd(8) for more options.\n\
220 -all Don't request/allow any options\n\
221 -ac Disable Address/Control compression\n\
222 -am Disable asyncmap negotiation\n\
223 -as <n> Set the desired async map to hex <n>\n\
224 -d Increase debugging level\n\
225 -detach Don't fork to background\n\
226 -ip Disable IP address negotiation\n\
227 -mn Disable magic number negotiation\n\
228 -mru Disable mru negotiation\n\
229 -p Set passive mode\n\
230 -pc Disable protocol field compression\n\
231 +ua <f> Get username and password for authenticating\n\
232 with peer using PAP from file <f>\n\
233 +pap Require PAP authentication from peer\n\
234 -pap Don't agree to authenticating with peer using PAP\n\
235 +chap Require CHAP authentication from peer\n\
236 -chap Don't agree to authenticating with peer using CHAP\n\
237 -vj disable VJ compression\n\
238 -auth Don't agree to authenticate with peer\n\
239 debug Increase debugging level\n\
240 domain <d> Append domain name <d> to hostname for authentication\n\
241 passive Set passive mode\n\
242 local Don't use modem control lines\n\
243 proxyarp Add proxy ARP entry\n\
248 * parse_args - parse a string of arguments, from the command
249 * line or from a file.
252 parse_args(argc, argv)
264 * First see if it's a command.
266 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
267 if (!strcmp(arg, cmdp->cmd_name))
270 if (cmdp->cmd_name != NULL) {
271 if (argc < cmdp->num_args) {
272 fprintf(stderr, "Too few parameters for command %s\n", arg);
275 if (!(*cmdp->cmd_func)(argv))
277 argc -= cmdp->num_args;
278 argv += cmdp->num_args;
282 * Maybe a tty name, speed or IP address?
284 if (!setdevname(arg) && !setspeed(arg) && !setipaddr(arg)) {
285 fprintf(stderr, "%s: unrecognized command\n", arg);
295 * usage - print out a message telling how to use the program.
299 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, progname);
303 * options_from_file - Read a string of options from a file,
304 * and interpret them.
307 options_from_file(filename, must_exist)
315 char args[MAXARGS][MAXWORDLEN];
316 char cmd[MAXWORDLEN];
318 if ((f = fopen(filename, "r")) == NULL) {
319 if (!must_exist && errno == ENOENT)
324 while (getword(f, cmd, &newline, filename)) {
326 * First see if it's a command.
328 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
329 if (!strcmp(cmd, cmdp->cmd_name))
332 if (cmdp->cmd_name != NULL) {
333 for (i = 0; i < cmdp->num_args; ++i) {
334 if (!getword(f, args[i], &newline, filename)) {
336 "In file %s: too few parameters for command %s\n",
343 if (!(*cmdp->cmd_func)(argv)) {
350 * Maybe a tty name, speed or IP address?
352 if (!setdevname(cmd) && !setspeed(cmd) && !setipaddr(cmd)) {
353 fprintf(stderr, "In file %s: unrecognized command %s\n",
364 * options_from_user - See if the use has a ~/.ppprc file,
365 * and if so, interpret options from it.
370 char *user, *path, *file;
373 if ((user = getenv("HOME")) == NULL)
376 path = malloc(strlen(user) + strlen(file) + 1);
378 novm("init file name");
381 ret = options_from_file(path, 0);
387 * Read a word from a file.
388 * Words are delimited by white-space or by quotes (").
389 * Quotes, white-space and \ may be escaped with \.
390 * \<newline> is ignored.
393 getword(f, word, newlinep, filename)
408 * First skip white-space and comments
410 while ((c = getc(f)) != EOF) {
413 * \<newline> is ignored; \ followed by anything else
416 if ((c = getc(f)) == '\n')
423 *newlinep = 1; /* next word starts a line */
425 /* comment - ignore until EOF or \n */
426 while ((c = getc(f)) != EOF && c != '\n')
431 } else if (!isspace(c))
436 * End of file or error - fail
448 * Is this character escaped by \ ?
452 --len; /* ignore \<newline> */
453 else if (c == '"' || isspace(c) || c == '\\')
454 word[len-1] = c; /* put special char in word */
456 if (len < MAXWORDLEN-1)
461 } else if (c == '"') {
463 } else if (!quoted && (isspace(c) || c == '#')) {
467 if (len < MAXWORDLEN-1)
473 if ((c = getc(f)) == EOF)
482 if (len >= MAXWORDLEN) {
483 word[MAXWORDLEN-1] = 0;
484 fprintf(stderr, "%s: warning: word in file %s too long (%.20s...)\n",
485 progname, filename, word);
493 * number_option - parse a numeric parameter for an option
496 number_option(str, valp, base)
503 *valp = strtol(str, &ptr, base);
505 fprintf(stderr, "%s: invalid number: %s\n", progname, str);
513 * int_option - like number_option, but valp is int *,
514 * the base is assumed to be 0, and *valp is not changed
515 * if there is an error.
518 int_option(str, valp)
524 if (!number_option(str, &v, 0))
532 * The following procedures execute commands.
536 * readfile - take commands from a file.
542 return options_from_file(*argv, 1);
546 * setdebug - Set debug (command line argument).
552 setlogmask(LOG_UPTO(LOG_DEBUG));
557 * noopt - Disable all options.
562 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
563 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
564 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
565 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
570 * noaccomp - Disable Address/Control field compression negotiation.
575 lcp_wantoptions[0].neg_accompression = 0;
576 lcp_allowoptions[0].neg_accompression = 0;
582 * noasyncmap - Disable async map negotiation.
587 lcp_wantoptions[0].neg_asyncmap = 0;
588 lcp_allowoptions[0].neg_asyncmap = 0;
594 * noipaddr - Disable IP address negotiation.
599 ipcp_wantoptions[0].neg_addr = 0;
600 ipcp_allowoptions[0].neg_addr = 0;
606 * nomagicnumber - Disable magic number negotiation.
611 lcp_wantoptions[0].neg_magicnumber = 0;
612 lcp_allowoptions[0].neg_magicnumber = 0;
618 * nomru - Disable mru negotiation.
623 lcp_wantoptions[0].neg_mru = 0;
624 lcp_allowoptions[0].neg_mru = 0;
630 * setmru - Set MRU for negotiation.
638 if (!number_option(*argv, &mru, 0))
640 lcp_wantoptions[0].mru = mru;
641 lcp_wantoptions[0].neg_mru = 1;
647 * nopcomp - Disable Protocol field compression negotiation.
652 lcp_wantoptions[0].neg_pcompression = 0;
653 lcp_allowoptions[0].neg_pcompression = 0;
659 * setpassive - Set passive mode (don't give up if we time out sending
660 * LCP configure-requests).
665 lcp_wantoptions[0].passive = 1;
671 * setsilent - Set silent mode (don't start sending LCP configure-requests
672 * until we get one from the peer).
677 lcp_wantoptions[0].silent = 1;
683 * nopap - Disable PAP authentication with peer.
688 lcp_allowoptions[0].neg_upap = 0;
694 * reqpap - Require PAP authentication from peer.
699 lcp_wantoptions[0].neg_upap = 1;
705 * setupapfile - specifies UPAP info for authenticating with peer.
714 lcp_allowoptions[0].neg_upap = 1;
716 /* open user info file */
717 if ((ufile = fopen(*argv, "r")) == NULL) {
718 fprintf(stderr, "unable to open user login data file %s\n", *argv);
721 check_access(ufile, *argv);
724 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
725 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
726 fprintf(stderr, "Unable to read user login data file %s.\n", *argv);
731 /* get rid of newlines */
733 if (l > 0 && user[l-1] == '\n')
736 if (l > 0 && passwd[l-1] == '\n')
744 * nochap - Disable CHAP authentication with peer.
749 lcp_allowoptions[0].neg_chap = 0;
755 * reqchap - Require CHAP authentication from peer.
760 lcp_wantoptions[0].neg_chap = 1;
767 * setnovj - diable vj compression
772 ipcp_wantoptions[0].neg_vj = 0;
773 ipcp_allowoptions[0].neg_vj = 0;
778 * setconnector - Set a program to connect to a serial line
784 connector = strdup(*argv);
785 if (connector == NULL)
786 novm("connector string");
793 * setdomain - Set domain name to append to hostname
799 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
800 hostname[MAXNAMELEN-1] = 0;
810 if (!number_option(*argv, &asyncmap, 16))
812 lcp_wantoptions[0].asyncmap |= asyncmap;
813 lcp_wantoptions[0].neg_asyncmap = 1;
818 * setspeed - Set the speed.
827 spd = strtol(arg, &ptr, 0);
828 if (ptr == arg || *ptr != 0 || spd == 0)
836 * setdevname - Set the device name.
843 char *tty, *ttyname();
844 char dev[MAXPATHLEN];
846 if (strncmp("/dev/", cp, 5) != 0) {
847 strcpy(dev, "/dev/");
848 strncat(dev, cp, MAXPATHLEN - 5);
849 dev[MAXPATHLEN-1] = 0;
854 * Check if there is a device by this name.
856 if (stat(cp, &statbuf) < 0) {
863 (void) strncpy(devname, cp, MAXPATHLEN);
864 devname[MAXPATHLEN-1] = 0;
865 default_device = FALSE;
872 * setipaddr - Set the IP address
879 char *colon, *index();
880 u_long local, remote;
881 ipcp_options *wo = &ipcp_wantoptions[0];
884 * IP address pair separated by ":".
886 if ((colon = index(arg, ':')) == NULL)
890 * If colon first character, then no local addr.
894 if ((local = inet_addr(arg)) == -1) {
895 if ((hp = gethostbyname(arg)) == NULL) {
896 fprintf(stderr, "unknown host: %s", arg);
899 local = *(long *)hp->h_addr;
900 if (our_name[0] == 0) {
901 strncpy(our_name, arg, MAXNAMELEN);
902 our_name[MAXNAMELEN-1] = 0;
912 * If colon last character, then no remote addr.
914 if (*++colon != '\0') {
915 if ((remote = inet_addr(colon)) == -1) {
916 if ((hp = gethostbyname(colon)) == NULL) {
917 fprintf(stderr, "unknown host: %s", colon);
920 remote = *(long *)hp->h_addr;
921 if (remote_name[0] == 0) {
922 strncpy(remote_name, colon, MAXNAMELEN);
923 remote_name[MAXNAMELEN-1] = 0;
928 wo->hisaddr = remote;
936 * setnoipdflt - disable setipdefault()
941 disable_defaultip = 1;
947 * setipcpaccl - accept peer's idea of our address
952 ipcp_wantoptions[0].accept_local = 1;
958 * setipcpaccr - accept peer's idea of its address
963 ipcp_wantoptions[0].accept_remote = 1;
969 * setipdefault - default our local IP address based on our hostname.
976 ipcp_options *wo = &ipcp_wantoptions[0];
979 * If local IP address already given, don't bother.
981 if (wo->ouraddr != 0 || disable_defaultip)
985 * Look up our hostname (possibly with domain name appended)
986 * and take the first IP address as our local IP address.
987 * If there isn't an IP address for our hostname, too bad.
989 wo->accept_local = 1; /* don't insist on this default value */
990 if ((hp = gethostbyname(hostname)) == NULL)
992 local = *(long *)hp->h_addr;
999 * setnetmask - set the netmask to be used on the interface.
1007 if ((mask = inet_addr(*argv)) == -1) {
1008 fprintf(stderr, "Invalid netmask %s\n", *argv);
1055 if (our_name[0] == 0) {
1056 strncpy(our_name, argv[0], MAXNAMELEN);
1057 our_name[MAXNAMELEN-1] = 0;
1066 strncpy(user, argv[0], MAXNAMELEN);
1067 user[MAXNAMELEN-1] = 0;
1075 strncpy(remote_name, argv[0], MAXNAMELEN);
1076 remote_name[MAXNAMELEN-1] = 0;
1090 ipcp_wantoptions[0].default_route = 1;
1097 ipcp_wantoptions[0].proxy_arp = 1;
1116 * Functions to set timeouts, max transmits, etc.
1122 return int_option(*argv, &lcp_fsm[0].timeouttime, 0);
1125 static int setlcpterm(argv)
1128 return int_option(*argv, &lcp_fsm[0].maxtermtransmits, 0);
1131 static int setlcpconf(argv)
1134 return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits, 0);
1137 static int setlcpfails(argv)
1140 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1143 static int setipcptimeout(argv)
1146 return int_option(*argv, &ipcp_fsm[0].timeouttime, 0);
1149 static int setipcpterm(argv)
1152 return int_option(*argv, &ipcp_fsm[0].maxtermtransmits, 0);
1155 static int setipcpconf(argv)
1158 return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits, 0);
1161 static int setipcpfails(argv)
1164 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1167 static int setpaptimeout(argv)
1170 return int_option(*argv, &upap[0].us_timeouttime, 0);
1173 static int setpapreqs(argv)
1176 return int_option(*argv, &upap[0].us_maxtransmits, 0);
1179 static int setchaptimeout(argv)
1182 return int_option(*argv, &chap[0].timeouttime, 0);
1185 static int setchapchal(argv)
1188 return int_option(*argv, &chap[0].max_transmits, 0);
1191 static int setchapintv(argv)
1194 return int_option(*argv, &chap[0].chal_interval, 0);