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.5 1994/04/18 04:07:48 paulus Exp $";
32 #include <sys/types.h>
37 #include "pathnames.h"
38 #include "patchlevel.h"
49 char *strdup __ARGS((char *));
55 static int setdebug __ARGS((void));
56 static int setkdebug __ARGS((char **));
57 static int setpassive __ARGS((void));
58 static int setsilent __ARGS((void));
59 static int noopt __ARGS((void));
60 static int setnovj __ARGS((void));
61 static int reqpap __ARGS((void));
62 static int nopap __ARGS((void));
63 static int setupapfile __ARGS((char **));
64 static int nochap __ARGS((void));
65 static int reqchap __ARGS((void));
66 static int setspeed __ARGS((char *));
67 static int noaccomp __ARGS((void));
68 static int noasyncmap __ARGS((void));
69 static int noipaddr __ARGS((void));
70 static int nomagicnumber __ARGS((void));
71 static int setasyncmap __ARGS((char **));
72 static int setescape __ARGS((char **));
73 static int setmru __ARGS((char **));
74 static int setmtu __ARGS((char **));
75 static int nomru __ARGS((void));
76 static int nopcomp __ARGS((void));
77 static int setconnector __ARGS((char **));
78 static int setdisconnector __ARGS((char **));
79 static int setdomain __ARGS((char **));
80 static int setnetmask __ARGS((char **));
81 static int setcrtscts __ARGS((void));
82 static int setnodetach __ARGS((void));
83 static int setmodem __ARGS((void));
84 static int setlocal __ARGS((void));
85 static int setlock __ARGS((void));
86 static int setname __ARGS((char **));
87 static int setuser __ARGS((char **));
88 static int setremote __ARGS((char **));
89 static int setauth __ARGS((void));
90 static int readfile __ARGS((char **));
91 static int setdefaultroute __ARGS((void));
92 static int setproxyarp __ARGS((void));
93 static int setpersist __ARGS((void));
94 static int setdologin __ARGS((void));
95 static int setusehostname __ARGS((void));
96 static int setnoipdflt __ARGS((void));
97 static int setlcptimeout __ARGS((char **));
98 static int setlcpterm __ARGS((char **));
99 static int setlcpconf __ARGS((char **));
100 static int setlcpfails __ARGS((char **));
101 static int setipcptimeout __ARGS((char **));
102 static int setipcpterm __ARGS((char **));
103 static int setipcpconf __ARGS((char **));
104 static int setipcpfails __ARGS((char **));
105 static int setpaptimeout __ARGS((char **));
106 static int setpapreqs __ARGS((char **));
107 static int setchaptimeout __ARGS((char **));
108 static int setchapchal __ARGS((char **));
109 static int setchapintv __ARGS((char **));
110 static int setipcpaccl __ARGS((void));
111 static int setipcpaccr __ARGS((void));
113 static int number_option __ARGS((char *, long *, int));
119 extern char *progname;
121 extern int kdebugflag;
126 extern char *connector;
127 extern char *disconnector;
129 extern char devname[];
130 extern int default_device;
131 extern u_long netmask;
134 extern char passwd[];
135 extern int auth_required;
139 extern char our_name[];
140 extern char remote_name[];
142 int disable_defaultip;
152 "-all", 0, noopt, /* Don't request/allow any options */
153 "-ac", 0, noaccomp, /* Disable Address/Control compress */
154 "-am", 0, noasyncmap, /* Disable asyncmap negotiation */
155 "-as", 1, setasyncmap, /* set the desired async map */
156 "-d", 0, setdebug, /* Increase debugging level */
157 "-detach", 0, setnodetach, /* don't fork */
158 "-ip", 0, noipaddr, /* Disable IP address negotiation */
159 "-mn", 0, nomagicnumber, /* Disable magic number negotiation */
160 "-mru", 0, nomru, /* Disable mru negotiation */
161 "-p", 0, setpassive, /* Set passive mode */
162 "-pc", 0, nopcomp, /* Disable protocol field compress */
163 "+ua", 1, setupapfile, /* Get PAP user and password from file */
164 "+pap", 0, reqpap, /* Require PAP auth from peer */
165 "-pap", 0, nopap, /* Don't allow UPAP authentication with peer */
166 "+chap", 0, reqchap, /* Require CHAP authentication from peer */
167 "-chap", 0, nochap, /* Don't allow CHAP authentication with peer */
168 "-vj", 0, setnovj, /* disable VJ compression */
169 "asyncmap", 1, setasyncmap, /* set the desired async map */
170 "escape", 1, setescape, /* set chars to escape on transmission */
171 "connect", 1, setconnector, /* A program to set up a connection */
172 "disconnect", 1, setdisconnector, /* program to disconnect serial dev. */
173 "crtscts", 0, setcrtscts, /* set h/w flow control */
174 "debug", 0, setdebug, /* Increase debugging level */
175 "kdebug", 1, setkdebug, /* Enable kernel-level debugging */
176 "domain", 1, setdomain, /* Add given domain name to hostname*/
177 "mru", 1, setmru, /* Set MRU value for negotiation */
178 "mtu", 1, setmtu, /* Set our MTU */
179 "netmask", 1, setnetmask, /* set netmask */
180 "passive", 0, setpassive, /* Set passive mode */
181 "silent", 0, setsilent, /* Set silent mode */
182 "modem", 0, setmodem, /* Use modem control lines */
183 "local", 0, setlocal, /* Don't use modem control lines */
184 "lock", 0, setlock, /* Lock serial device (with lock file) */
185 "name", 1, setname, /* Set local name for authentication */
186 "user", 1, setuser, /* Set username for PAP auth with peer */
187 "usehostname", 0, setusehostname, /* Must use hostname for auth. */
188 "remotename", 1, setremote, /* Set remote name for authentication */
189 "auth", 0, setauth, /* Require authentication from peer */
190 "file", 1, readfile, /* Take options from a file */
191 "defaultroute", 0, setdefaultroute, /* Add default route */
192 "proxyarp", 0, setproxyarp, /* Add proxy ARP entry */
193 "persist", 0, setpersist, /* Keep on reopening connection after close */
194 "login", 0, setdologin, /* Use system password database for UPAP */
195 "noipdefault", 0, setnoipdflt, /* Don't use name for default IP adrs */
196 "lcp-restart", 1, setlcptimeout, /* Set timeout for LCP */
197 "lcp-max-terminate", 1, setlcpterm, /* Set max #xmits for term-reqs */
198 "lcp-max-configure", 1, setlcpconf, /* Set max #xmits for conf-reqs */
199 "lcp-max-failure", 1, setlcpfails, /* Set max #conf-naks for LCP */
200 "ipcp-restart", 1, setipcptimeout, /* Set timeout for IPCP */
201 "ipcp-max-terminate", 1, setipcpterm, /* Set max #xmits for term-reqs */
202 "ipcp-max-configure", 1, setipcpconf, /* Set max #xmits for conf-reqs */
203 "ipcp-max-failure", 1, setipcpfails, /* Set max #conf-naks for IPCP */
204 "pap-restart", 1, setpaptimeout, /* Set timeout for UPAP */
205 "pap-max-authreq", 1, setpapreqs, /* Set max #xmits for auth-reqs */
206 "chap-restart", 1, setchaptimeout, /* Set timeout for CHAP */
207 "chap-max-challenge", 1, setchapchal, /* Set max #xmits for challenge */
208 "chap-interval", 1, setchapintv, /* Set interval for rechallenge */
209 "ipcp-accept-local", 0, setipcpaccl, /* Accept peer's address for us */
210 "ipcp-accept-remote", 0, setipcpaccr, /* Accept peer's address for it */
215 static char *usage_string = "\
216 pppd version %s patch level %d\n\
217 Usage: %s [ arguments ], where arguments are:\n\
218 <device> Communicate over the named device\n\
219 <speed> Set the baud rate to <speed>\n\
220 <loc>:<rem> Set the local and/or remote interface IP\n\
221 addresses. Either one may be omitted.\n\
222 asyncmap <n> Set the desired async map to hex <n>\n\
223 auth Require authentication from peer\n\
224 connect <p> Invoke shell command <p> to set up the serial line\n\
225 crtscts Use hardware RTS/CTS flow control\n\
226 defaultroute Add default route through interface\n\
227 file <f> Take options from file <f>\n\
228 modem Use modem control lines\n\
229 mru <n> Set MRU value to <n> for negotiation\n\
230 netmask <n> Set interface netmask to <n>\n\
231 See pppd(8) for more options.\n\
236 * parse_args - parse a string of arguments, from the command
237 * line or from a file.
240 parse_args(argc, argv)
252 * First see if it's a command.
254 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
255 if (!strcmp(arg, cmdp->cmd_name))
258 if (cmdp->cmd_name != NULL) {
259 if (argc < cmdp->num_args) {
260 fprintf(stderr, "Too few parameters for command %s\n", arg);
263 if (!(*cmdp->cmd_func)(argv))
265 argc -= cmdp->num_args;
266 argv += cmdp->num_args;
270 * Maybe a tty name, speed or IP address?
272 if (!setdevname(arg) && !setspeed(arg) && !setipaddr(arg)) {
273 fprintf(stderr, "%s: unrecognized command\n", arg);
283 * usage - print out a message telling how to use the program.
287 fprintf(stderr, usage_string, VERSION, PATCHLEVEL, progname);
291 * options_from_file - Read a string of options from a file,
292 * and interpret them.
295 options_from_file(filename, must_exist)
303 char args[MAXARGS][MAXWORDLEN];
304 char cmd[MAXWORDLEN];
306 if ((f = fopen(filename, "r")) == NULL) {
307 if (!must_exist && errno == ENOENT)
312 while (getword(f, cmd, &newline, filename)) {
314 * First see if it's a command.
316 for (cmdp = cmds; cmdp->cmd_name; cmdp++)
317 if (!strcmp(cmd, cmdp->cmd_name))
320 if (cmdp->cmd_name != NULL) {
321 for (i = 0; i < cmdp->num_args; ++i) {
322 if (!getword(f, args[i], &newline, filename)) {
324 "In file %s: too few parameters for command %s\n",
331 if (!(*cmdp->cmd_func)(argv)) {
338 * Maybe a tty name, speed or IP address?
340 if (!setdevname(cmd) && !setspeed(cmd) && !setipaddr(cmd)) {
341 fprintf(stderr, "In file %s: unrecognized command %s\n",
352 * options_from_user - See if the use has a ~/.ppprc file,
353 * and if so, interpret options from it.
358 char *user, *path, *file;
361 if ((user = getenv("HOME")) == NULL)
364 path = malloc(strlen(user) + strlen(file) + 1);
366 novm("init file name");
369 ret = options_from_file(path, 0);
375 * Read a word from a file.
376 * Words are delimited by white-space or by quotes (").
377 * Quotes, white-space and \ may be escaped with \.
378 * \<newline> is ignored.
381 getword(f, word, newlinep, filename)
396 * First skip white-space and comments
398 while ((c = getc(f)) != EOF) {
401 * \<newline> is ignored; \ followed by anything else
404 if ((c = getc(f)) == '\n')
411 *newlinep = 1; /* next word starts a line */
413 /* comment - ignore until EOF or \n */
414 while ((c = getc(f)) != EOF && c != '\n')
419 } else if (!isspace(c))
424 * End of file or error - fail
436 * Is this character escaped by \ ?
440 --len; /* ignore \<newline> */
441 else if (c == '"' || isspace(c) || c == '\\')
442 word[len-1] = c; /* put special char in word */
444 if (len < MAXWORDLEN-1)
449 } else if (c == '"') {
451 } else if (!quoted && (isspace(c) || c == '#')) {
455 if (len < MAXWORDLEN-1)
461 if ((c = getc(f)) == EOF)
470 if (len >= MAXWORDLEN) {
471 word[MAXWORDLEN-1] = 0;
472 fprintf(stderr, "%s: warning: word in file %s too long (%.20s...)\n",
473 progname, filename, word);
481 * number_option - parse a numeric parameter for an option
484 number_option(str, valp, base)
491 *valp = strtol(str, &ptr, base);
493 fprintf(stderr, "%s: invalid number: %s\n", progname, str);
501 * int_option - like number_option, but valp is int *,
502 * the base is assumed to be 0, and *valp is not changed
503 * if there is an error.
506 int_option(str, valp)
512 if (!number_option(str, &v, 0))
520 * The following procedures execute commands.
524 * readfile - take commands from a file.
530 return options_from_file(*argv, 1);
534 * setdebug - Set debug (command line argument).
544 * setkdebug - Set kernel debugging level.
550 return int_option(*argv, &kdebugflag);
554 * noopt - Disable all options.
559 BZERO((char *) &lcp_wantoptions[0], sizeof (struct lcp_options));
560 BZERO((char *) &lcp_allowoptions[0], sizeof (struct lcp_options));
561 BZERO((char *) &ipcp_wantoptions[0], sizeof (struct ipcp_options));
562 BZERO((char *) &ipcp_allowoptions[0], sizeof (struct ipcp_options));
567 * noaccomp - Disable Address/Control field compression negotiation.
572 lcp_wantoptions[0].neg_accompression = 0;
573 lcp_allowoptions[0].neg_accompression = 0;
579 * noasyncmap - Disable async map negotiation.
584 lcp_wantoptions[0].neg_asyncmap = 0;
585 lcp_allowoptions[0].neg_asyncmap = 0;
591 * noipaddr - Disable IP address negotiation.
596 ipcp_wantoptions[0].neg_addr = 0;
597 ipcp_allowoptions[0].neg_addr = 0;
603 * nomagicnumber - Disable magic number negotiation.
608 lcp_wantoptions[0].neg_magicnumber = 0;
609 lcp_allowoptions[0].neg_magicnumber = 0;
615 * nomru - Disable mru negotiation.
620 lcp_wantoptions[0].neg_mru = 0;
621 lcp_allowoptions[0].neg_mru = 0;
627 * setmru - Set MRU for negotiation.
635 if (!number_option(*argv, &mru, 0))
637 lcp_wantoptions[0].mru = mru;
638 lcp_wantoptions[0].neg_mru = 1;
644 * setmru - Set the largest MTU we'll use.
652 if (!number_option(*argv, &mtu, 0))
654 if (mtu < MINMRU || mtu > MAXMRU) {
655 fprintf(stderr, "mtu option value of %d is too %s\n", mtu,
656 (mtu < MINMRU? "small": "large"));
659 lcp_allowoptions[0].mru = mtu;
665 * nopcomp - Disable Protocol field compression negotiation.
670 lcp_wantoptions[0].neg_pcompression = 0;
671 lcp_allowoptions[0].neg_pcompression = 0;
677 * setpassive - Set passive mode (don't give up if we time out sending
678 * LCP configure-requests).
683 lcp_wantoptions[0].passive = 1;
689 * setsilent - Set silent mode (don't start sending LCP configure-requests
690 * until we get one from the peer).
695 lcp_wantoptions[0].silent = 1;
701 * nopap - Disable PAP authentication with peer.
706 lcp_allowoptions[0].neg_upap = 0;
712 * reqpap - Require PAP authentication from peer.
717 lcp_wantoptions[0].neg_upap = 1;
723 * setupapfile - specifies UPAP info for authenticating with peer.
732 lcp_allowoptions[0].neg_upap = 1;
734 /* open user info file */
735 if ((ufile = fopen(*argv, "r")) == NULL) {
736 fprintf(stderr, "unable to open user login data file %s\n", *argv);
739 check_access(ufile, *argv);
742 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
743 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
744 fprintf(stderr, "Unable to read user login data file %s.\n", *argv);
749 /* get rid of newlines */
751 if (l > 0 && user[l-1] == '\n')
754 if (l > 0 && passwd[l-1] == '\n')
762 * nochap - Disable CHAP authentication with peer.
767 lcp_allowoptions[0].neg_chap = 0;
773 * reqchap - Require CHAP authentication from peer.
778 lcp_wantoptions[0].neg_chap = 1;
785 * setnovj - diable vj compression
790 ipcp_wantoptions[0].neg_vj = 0;
791 ipcp_allowoptions[0].neg_vj = 0;
796 * setconnector - Set a program to connect to a serial line
802 connector = strdup(*argv);
803 if (connector == NULL)
804 novm("connector string");
810 * setdisconnector - Set a program to disconnect from the serial line
813 setdisconnector(argv)
816 disconnector = strdup(*argv);
817 if (disconnector == NULL)
818 novm("disconnector string");
825 * setdomain - Set domain name to append to hostname
831 strncat(hostname, *argv, MAXNAMELEN - strlen(hostname));
832 hostname[MAXNAMELEN-1] = 0;
838 * setasyncmap - add bits to asyncmap (what we request peer to escape).
846 if (!number_option(*argv, &asyncmap, 16))
848 lcp_wantoptions[0].asyncmap |= asyncmap;
849 lcp_wantoptions[0].neg_asyncmap = 1;
855 * setescape - add chars to the set we escape on transmission.
867 n = strtol(p, &endp, 16);
869 fprintf(stderr, "%s: invalid hex number: %s\n", progname, p);
873 if (n < 0 || 0x20 <= n && n <= 0x3F || n == 0x5E || n > 0xFF) {
874 fprintf(stderr, "%s: can't escape character 0x%x\n", n);
877 xmit_accm[0][n >> 5] |= 1 << (n & 0x1F);
878 while (*p == ',' || *p == ' ')
886 * setspeed - Set the speed.
895 spd = strtol(arg, &ptr, 0);
896 if (ptr == arg || *ptr != 0 || spd == 0)
904 * setdevname - Set the device name.
911 char *tty, *ttyname();
912 char dev[MAXPATHLEN];
914 if (strncmp("/dev/", cp, 5) != 0) {
915 strcpy(dev, "/dev/");
916 strncat(dev, cp, MAXPATHLEN - 5);
917 dev[MAXPATHLEN-1] = 0;
922 * Check if there is a device by this name.
924 if (stat(cp, &statbuf) < 0) {
931 (void) strncpy(devname, cp, MAXPATHLEN);
932 devname[MAXPATHLEN-1] = 0;
933 default_device = FALSE;
940 * setipaddr - Set the IP address
947 char *colon, *index();
948 u_long local, remote;
949 ipcp_options *wo = &ipcp_wantoptions[0];
952 * IP address pair separated by ":".
954 if ((colon = index(arg, ':')) == NULL)
958 * If colon first character, then no local addr.
962 if ((local = inet_addr(arg)) == -1) {
963 if ((hp = gethostbyname(arg)) == NULL) {
964 fprintf(stderr, "unknown host: %s", arg);
967 local = *(long *)hp->h_addr;
968 if (our_name[0] == 0) {
969 strncpy(our_name, arg, MAXNAMELEN);
970 our_name[MAXNAMELEN-1] = 0;
980 * If colon last character, then no remote addr.
982 if (*++colon != '\0') {
983 if ((remote = inet_addr(colon)) == -1) {
984 if ((hp = gethostbyname(colon)) == NULL) {
985 fprintf(stderr, "unknown host: %s", colon);
988 remote = *(long *)hp->h_addr;
989 if (remote_name[0] == 0) {
990 strncpy(remote_name, colon, MAXNAMELEN);
991 remote_name[MAXNAMELEN-1] = 0;
996 wo->hisaddr = remote;
1004 * setnoipdflt - disable setipdefault()
1009 disable_defaultip = 1;
1015 * setipcpaccl - accept peer's idea of our address
1020 ipcp_wantoptions[0].accept_local = 1;
1026 * setipcpaccr - accept peer's idea of its address
1031 ipcp_wantoptions[0].accept_remote = 1;
1037 * setipdefault - default our local IP address based on our hostname.
1044 ipcp_options *wo = &ipcp_wantoptions[0];
1047 * If local IP address already given, don't bother.
1049 if (wo->ouraddr != 0 || disable_defaultip)
1053 * Look up our hostname (possibly with domain name appended)
1054 * and take the first IP address as our local IP address.
1055 * If there isn't an IP address for our hostname, too bad.
1057 wo->accept_local = 1; /* don't insist on this default value */
1058 if ((hp = gethostbyname(hostname)) == NULL)
1060 local = *(long *)hp->h_addr;
1062 wo->ouraddr = local;
1067 * setnetmask - set the netmask to be used on the interface.
1075 if ((mask = inet_addr(*argv)) == -1) {
1076 fprintf(stderr, "Invalid netmask %s\n", *argv);
1085 * Return user specified netmask. A value of zero means no netmask has
1143 if (our_name[0] == 0) {
1144 strncpy(our_name, argv[0], MAXNAMELEN);
1145 our_name[MAXNAMELEN-1] = 0;
1154 strncpy(user, argv[0], MAXNAMELEN);
1155 user[MAXNAMELEN-1] = 0;
1163 strncpy(remote_name, argv[0], MAXNAMELEN);
1164 remote_name[MAXNAMELEN-1] = 0;
1178 ipcp_wantoptions[0].default_route = 1;
1185 ipcp_wantoptions[0].proxy_arp = 1;
1204 * Functions to set timeouts, max transmits, etc.
1210 return int_option(*argv, &lcp_fsm[0].timeouttime, 0);
1213 static int setlcpterm(argv)
1216 return int_option(*argv, &lcp_fsm[0].maxtermtransmits, 0);
1219 static int setlcpconf(argv)
1222 return int_option(*argv, &lcp_fsm[0].maxconfreqtransmits, 0);
1225 static int setlcpfails(argv)
1228 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1231 static int setipcptimeout(argv)
1234 return int_option(*argv, &ipcp_fsm[0].timeouttime, 0);
1237 static int setipcpterm(argv)
1240 return int_option(*argv, &ipcp_fsm[0].maxtermtransmits, 0);
1243 static int setipcpconf(argv)
1246 return int_option(*argv, &ipcp_fsm[0].maxconfreqtransmits, 0);
1249 static int setipcpfails(argv)
1252 return int_option(*argv, &lcp_fsm[0].maxnakloops, 0);
1255 static int setpaptimeout(argv)
1258 return int_option(*argv, &upap[0].us_timeouttime, 0);
1261 static int setpapreqs(argv)
1264 return int_option(*argv, &upap[0].us_maxtransmits, 0);
1267 static int setchaptimeout(argv)
1270 return int_option(*argv, &chap[0].timeouttime, 0);
1273 static int setchapchal(argv)
1276 return int_option(*argv, &chap[0].max_transmits, 0);
1279 static int setchapintv(argv)
1282 return int_option(*argv, &chap[0].chal_interval, 0);