xref: /freebsd/sbin/routed/parms.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "defs.h"
33 #include "pathnames.h"
34 #include <sys/stat.h>
35 
36 #ifdef __NetBSD__
37 __RCSID("$NetBSD$");
38 #elif defined(__FreeBSD__)
39 __RCSID("$FreeBSD$");
40 #else
41 __RCSID("$Revision: 2.26 $");
42 #ident "$Revision: 2.26 $"
43 #endif
44 
45 
46 static struct parm *parms;
47 struct intnet *intnets;
48 struct r1net *r1nets;
49 struct tgate *tgates;
50 
51 
52 /* use configured parameters
53  */
54 void
55 get_parms(struct interface *ifp)
56 {
57 	static int warned_auth_in, warned_auth_out;
58 	struct parm *parmp;
59 	int i, num_passwds = 0;
60 
61 	/* get all relevant parameters
62 	 */
63 	for (parmp = parms; parmp != NULL; parmp = parmp->parm_next) {
64 		if (parmp->parm_name[0] == '\0'
65 		    || !strcmp(ifp->int_name, parmp->parm_name)
66 		    || (parmp->parm_name[0] == '\n'
67 			&& on_net(ifp->int_addr,
68 				  parmp->parm_net, parmp->parm_mask))) {
69 
70 			/* This group of parameters is relevant,
71 			 * so get its settings
72 			 */
73 			ifp->int_state |= parmp->parm_int_state;
74 			for (i = 0; i < MAX_AUTH_KEYS; i++) {
75 				if (parmp->parm_auth[0].type == RIP_AUTH_NONE
76 				    || num_passwds >= MAX_AUTH_KEYS)
77 					break;
78 				memcpy(&ifp->int_auth[num_passwds++],
79 				       &parmp->parm_auth[i],
80 				       sizeof(ifp->int_auth[0]));
81 			}
82 			if (parmp->parm_rdisc_pref != 0)
83 				ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
84 			if (parmp->parm_rdisc_int != 0)
85 				ifp->int_rdisc_int = parmp->parm_rdisc_int;
86 			if (parmp->parm_adj_inmetric != 0)
87 			    ifp->int_adj_inmetric = parmp->parm_adj_inmetric;
88 			if (parmp->parm_adj_outmetric != 0)
89 			    ifp->int_adj_outmetric = parmp->parm_adj_outmetric;
90 		}
91 	}
92 
93 	/* Set general defaults.
94 	 *
95 	 * Default poor-man's router discovery to a metric that will
96 	 * be heard by old versions of `routed`.  They ignored received
97 	 * routes with metric 15.
98 	 */
99 	if ((ifp->int_state & IS_PM_RDISC)
100 	    && ifp->int_d_metric == 0)
101 		ifp->int_d_metric = FAKE_METRIC;
102 
103 	if (ifp->int_rdisc_int == 0)
104 		ifp->int_rdisc_int = DefMaxAdvertiseInterval;
105 
106 	if (!(ifp->int_if_flags & IFF_MULTICAST)
107 	    && !(ifp->int_state & IS_REMOTE))
108 		ifp->int_state |= IS_BCAST_RDISC;
109 
110 	if (ifp->int_if_flags & IFF_POINTOPOINT) {
111 		ifp->int_state |= IS_BCAST_RDISC;
112 		/* By default, point-to-point links should be passive
113 		 * about router-discovery for the sake of demand-dialing.
114 		 */
115 		if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
116 			ifp->int_state |= IS_NO_SOL_OUT;
117 		if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
118 			ifp->int_state |= IS_NO_ADV_OUT;
119 	}
120 
121 	if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
122 		ifp->int_state |= IS_NO_RDISC;
123 	if (ifp->int_state & IS_PASSIVE)
124 		ifp->int_state |= IS_NO_RIP;
125 
126 	if (!IS_RIP_IN_OFF(ifp->int_state)
127 	    && ifp->int_auth[0].type != RIP_AUTH_NONE
128 	    && !(ifp->int_state & IS_NO_RIPV1_IN)
129 	    && !warned_auth_in) {
130 		msglog("Warning: RIPv1 input via %s"
131 		       " will be accepted without authentication",
132 		       ifp->int_name);
133 		warned_auth_in = 1;
134 	}
135 	if (!IS_RIP_OUT_OFF(ifp->int_state)
136 	    && ifp->int_auth[0].type != RIP_AUTH_NONE
137 	    && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
138 		if (!warned_auth_out) {
139 			msglog("Warning: RIPv1 output via %s"
140 			       " will be sent without authentication",
141 			       ifp->int_name);
142 			warned_auth_out = 1;
143 		}
144 	}
145 }
146 
147 
148 /* Read a list of gateways from /etc/gateways and add them to our tables.
149  *
150  * This file contains a list of "remote" gateways.  That is usually
151  * a gateway which we cannot immediately determine if it is present or
152  * not as we can do for those provided by directly connected hardware.
153  *
154  * If a gateway is marked "passive" in the file, then we assume it
155  * does not understand RIP and assume it is always present.  Those
156  * not marked passive are treated as if they were directly connected
157  * and assumed to be broken if they do not send us advertisements.
158  * All remote interfaces are added to our list, and those not marked
159  * passive are sent routing updates.
160  *
161  * A passive interface can also be local, hardware interface exempt
162  * from RIP.
163  */
164 void
165 gwkludge(void)
166 {
167 	FILE *fp;
168 	char *p, *lptr;
169 	const char *cp;
170 	char lbuf[200], net_host[5], dname[64+1+64+1];
171 	char gname[GNAME_LEN+1], qual[9];
172 	struct interface *ifp;
173 	naddr dst, netmask, gate;
174 	int metric, n, lnum;
175 	struct stat sb;
176 	u_int state;
177 	const char *type;
178 
179 
180 	fp = fopen(_PATH_GATEWAYS, "r");
181 	if (fp == NULL)
182 		return;
183 
184 	if (0 > fstat(fileno(fp), &sb)) {
185 		msglog("could not stat() "_PATH_GATEWAYS);
186 		(void)fclose(fp);
187 		return;
188 	}
189 
190 	for (lnum = 1; ; lnum++) {
191 		if (fgets(lbuf, sizeof(lbuf), fp) == NULL)
192 			break;
193 		lptr = lbuf;
194 		while (*lptr == ' ')
195 			lptr++;
196 		p = lptr+strlen(lptr)-1;
197 		while (*p == '\n'
198 		       || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
199 			*p-- = '\0';
200 		if (*lptr == '\0'	/* ignore null and comment lines */
201 		    || *lptr == '#')
202 			continue;
203 
204 		/* notice newfangled parameter lines
205 		 */
206 		if (strncasecmp("net", lptr, 3)
207 		    && strncasecmp("host", lptr, 4)) {
208 			cp = parse_parms(lptr,
209 					 (sb.st_uid == 0
210 					  && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
211 			if (cp != NULL)
212 				msglog("%s in line %d of "_PATH_GATEWAYS,
213 				       cp, lnum);
214 			continue;
215 		}
216 
217 /*  {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
218 		qual[0] = '\0';
219 		/* the '64' here must be GNAME_LEN */
220 		n = sscanf(lptr, "%4s %129[^ \t] gateway"
221 			   " %64[^ / \t] metric %u %8s\n",
222 			   net_host, dname, gname, &metric, qual);
223 		if (n != 4 && n != 5) {
224 			msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
225 			       lptr, n);
226 			continue;
227 		}
228 		if (metric >= HOPCNT_INFINITY) {
229 			msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
230 			       lptr);
231 			continue;
232 		}
233 		if (!strcasecmp(net_host, "host")) {
234 			if (!gethost(dname, &dst)) {
235 				msglog("bad host \"%s\" in "_PATH_GATEWAYS
236 				       " entry \"%s\"", dname, lptr);
237 				continue;
238 			}
239 			netmask = HOST_MASK;
240 		} else if (!strcasecmp(net_host, "net")) {
241 			if (!getnet(dname, &dst, &netmask)) {
242 				msglog("bad net \"%s\" in "_PATH_GATEWAYS
243 				       " entry \"%s\"", dname, lptr);
244 				continue;
245 			}
246 			if (dst == RIP_DEFAULT) {
247 				msglog("bad net \"%s\" in "_PATH_GATEWAYS
248 				       " entry \"%s\"--cannot be default",
249 				       dname, lptr);
250 				continue;
251 			}
252 			/* Turn network # into IP address. */
253 			dst = htonl(dst);
254 		} else {
255 			msglog("bad \"%s\" in "_PATH_GATEWAYS
256 			       " entry \"%s\"", net_host, lptr);
257 			continue;
258 		}
259 
260 		if (!gethost(gname, &gate)) {
261 			msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
262 			       " entry \"%s\"", gname, lptr);
263 			continue;
264 		}
265 
266 		if (!strcasecmp(qual, type = "passive")) {
267 			/* Passive entries are not placed in our tables,
268 			 * only the kernel's, so we don't copy all of the
269 			 * external routing information within a net.
270 			 * Internal machines should use the default
271 			 * route to a suitable gateway (like us).
272 			 */
273 			state = IS_REMOTE | IS_PASSIVE;
274 			if (metric == 0)
275 				metric = 1;
276 
277 		} else if (!strcasecmp(qual, type = "external")) {
278 			/* External entries are handled by other means
279 			 * such as EGP, and are placed only in the daemon
280 			 * tables to prevent overriding them with something
281 			 * else.
282 			 */
283 			strcpy(qual,"external");
284 			state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
285 			if (metric == 0)
286 				metric = 1;
287 
288 		} else if (!strcasecmp(qual, "active")
289 			   || qual[0] == '\0') {
290 			if (metric != 0) {
291 				/* Entries that are neither "passive" nor
292 				 * "external" are "remote" and must behave
293 				 * like physical interfaces.  If they are not
294 				 * heard from regularly, they are deleted.
295 				 */
296 				state = IS_REMOTE;
297 				type = "remote";
298 			} else {
299 				/* "remote" entries with a metric of 0
300 				 * are aliases for our own interfaces
301 				 */
302 				state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
303 				type = "alias";
304 			}
305 
306 		} else {
307 			msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
308 			       " unknown type %s", lptr, qual);
309 			continue;
310 		}
311 
312 		if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
313 			state |= IS_NO_RDISC;
314 		if (state & IS_PASSIVE)
315 			state |= IS_NO_RIP;
316 
317 		ifp = check_dup(gate,dst,netmask,state);
318 		if (ifp != NULL) {
319 			msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
320 			continue;
321 		}
322 
323 		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
324 		memset(ifp, 0, sizeof(*ifp));
325 
326 		ifp->int_state = state;
327 		if (netmask == HOST_MASK)
328 			ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
329 		else
330 			ifp->int_if_flags = IFF_UP;
331 		ifp->int_act_time = NEVER;
332 		ifp->int_addr = gate;
333 		ifp->int_dstaddr = dst;
334 		ifp->int_mask = netmask;
335 		ifp->int_ripv1_mask = netmask;
336 		ifp->int_std_mask = std_mask(gate);
337 		ifp->int_net = ntohl(dst);
338 		ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
339 		ifp->int_std_addr = htonl(ifp->int_std_net);
340 		ifp->int_metric = metric;
341 		if (!(state & IS_EXTERNAL)
342 		    && ifp->int_mask != ifp->int_std_mask)
343 			ifp->int_state |= IS_SUBNET;
344 		(void)sprintf(ifp->int_name, "%s(%s)", type, gname);
345 		ifp->int_index = -1;
346 
347 		if_link(ifp);
348 	}
349 
350 	/* After all of the parameter lines have been read,
351 	 * apply them to any remote interfaces.
352 	 */
353 	LIST_FOREACH(ifp, &ifnet, int_list) {
354 		get_parms(ifp);
355 
356 		tot_interfaces++;
357 		if (!IS_RIP_OFF(ifp->int_state))
358 			rip_interfaces++;
359 
360 		trace_if("Add", ifp);
361 	}
362 
363 	(void)fclose(fp);
364 }
365 
366 
367 /* like strtok(), but honoring backslash and not changing the source string
368  */
369 static int				/* 0=ok, -1=bad */
370 parse_quote(char **linep,		/* look here */
371 	    const char *delims,		/* for these delimiters */
372 	    char *delimp,		/* 0 or put found delimiter here */
373 	    char *buf,			/* copy token to here */
374 	    int	lim)			/* at most this many bytes */
375 {
376 	char c = '\0', *pc;
377 	const char *p;
378 
379 
380 	pc = *linep;
381 	if (*pc == '\0')
382 		return -1;
383 
384 	while (lim != 0) {
385 		c = *pc++;
386 		if (c == '\0')
387 			break;
388 
389 		if (c == '\\' && *pc != '\0') {
390 			if ((c = *pc++) == 'n') {
391 				c = '\n';
392 			} else if (c == 'r') {
393 				c = '\r';
394 			} else if (c == 't') {
395 				c = '\t';
396 			} else if (c == 'b') {
397 				c = '\b';
398 			} else if (c >= '0' && c <= '7') {
399 				c -= '0';
400 				if (*pc >= '0' && *pc <= '7') {
401 					c = (c<<3)+(*pc++ - '0');
402 					if (*pc >= '0' && *pc <= '7')
403 					    c = (c<<3)+(*pc++ - '0');
404 				}
405 			}
406 
407 		} else {
408 			for (p = delims; *p != '\0'; ++p) {
409 				if (*p == c)
410 					goto exit;
411 			}
412 		}
413 
414 		*buf++ = c;
415 		--lim;
416 	}
417 exit:
418 	if (lim == 0)
419 		return -1;
420 
421 	*buf = '\0';			/* terminate copy of token */
422 	if (delimp != NULL)
423 		*delimp = c;		/* return delimiter */
424 	*linep = pc-1;			/* say where we ended */
425 	return 0;
426 }
427 
428 
429 /* Parse password timestamp
430  */
431 static char *
432 parse_ts(time_t *tp,
433 	 char **valp,
434 	 char *val0,
435 	 char *delimp,
436 	 char *buf,
437 	 u_int bufsize)
438 {
439 	struct tm tm;
440 	char *ptr;
441 
442 	if (0 > parse_quote(valp, "| ,\n\r", delimp,
443 			    buf,bufsize)
444 	    || buf[bufsize-1] != '\0'
445 	    || buf[bufsize-2] != '\0') {
446 		sprintf(buf,"bad timestamp %.25s", val0);
447 		return buf;
448 	}
449 	strcat(buf,"\n");
450 	memset(&tm, 0, sizeof(tm));
451 	ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
452 	if (ptr == NULL || *ptr != '\0') {
453 		sprintf(buf,"bad timestamp %.25s", val0);
454 		return buf;
455 	}
456 
457 	if ((*tp = mktime(&tm)) == -1) {
458 		sprintf(buf,"bad timestamp %.25s", val0);
459 		return buf;
460 	}
461 
462 	return 0;
463 }
464 
465 
466 /* Get a password, key ID, and expiration date in the format
467  *	passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
468  */
469 static const char *			/* 0 or error message */
470 get_passwd(char *tgt,
471 	   char *val,
472 	   struct parm *parmp,
473 	   u_int16_t type,
474 	   int safe)			/* 1=from secure file */
475 {
476 	static char buf[80];
477 	char *val0, *p, delim;
478 	struct auth k, *ap, *ap2;
479 	int i;
480 	u_long l;
481 
482 	assert(val != NULL);
483 	if (!safe)
484 		return "ignore unsafe password";
485 
486 	for (ap = parmp->parm_auth, i = 0;
487 	     ap->type != RIP_AUTH_NONE; i++, ap++) {
488 		if (i >= MAX_AUTH_KEYS)
489 			return "too many passwords";
490 	}
491 
492 	memset(&k, 0, sizeof(k));
493 	k.type = type;
494 	k.end = -1-DAY;
495 
496 	val0 = val;
497 	if (0 > parse_quote(&val, "| ,\n\r", &delim,
498 			    (char *)k.key, sizeof(k.key)))
499 		return tgt;
500 
501 	if (delim != '|') {
502 		if (type == RIP_AUTH_MD5)
503 			return "missing Keyid";
504 	} else {
505 		val0 = ++val;
506 		buf[sizeof(buf)-1] = '\0';
507 		if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
508 		    || buf[sizeof(buf)-1] != '\0'
509 		    || (l = strtoul(buf,&p,0)) > 255
510 		    || *p != '\0') {
511 			sprintf(buf,"bad KeyID \"%.20s\"", val0);
512 			return buf;
513 		}
514 		for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
515 			if (ap2->keyid == l) {
516 				sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
517 				return buf;
518 			}
519 		}
520 		k.keyid = (int)l;
521 
522 		if (delim == '|') {
523 			val0 = ++val;
524 			if (NULL != (p = parse_ts(&k.start,&val,val0,&delim,
525 						  buf,sizeof(buf))))
526 				return p;
527 			if (delim != '|')
528 				return "missing second timestamp";
529 			val0 = ++val;
530 			if (NULL != (p = parse_ts(&k.end,&val,val0,&delim,
531 						  buf,sizeof(buf))))
532 				return p;
533 			if ((u_long)k.start > (u_long)k.end) {
534 				sprintf(buf,"out of order timestamp %.30s",
535 					val0);
536 				return buf;
537 			}
538 		}
539 	}
540 	if (delim != '\0')
541 		return tgt;
542 
543 	memmove(ap, &k, sizeof(*ap));
544 	return 0;
545 }
546 
547 
548 static const char *
549 bad_str(const char *estr)
550 {
551 	static char buf[100+8];
552 
553 	sprintf(buf, "bad \"%.100s\"", estr);
554 	return buf;
555 }
556 
557 
558 /* Parse a set of parameters for an interface.
559  */
560 const char *					/* 0 or error message */
561 parse_parms(char *line,
562 	    int safe)			/* 1=from secure file */
563 {
564 #define PARS(str) (!strcasecmp(tgt, str))
565 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
566 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break;	\
567 	parm.parm_int_state |= (b);}
568 	struct parm parm;
569 	struct intnet *intnetp;
570 	struct r1net *r1netp;
571 	struct tgate *tg;
572 	naddr addr, mask;
573 	char delim, *val0 = NULL, *tgt, *val, *p;
574 	const char *msg;
575 	char buf[BUFSIZ], buf2[BUFSIZ];
576 	int i;
577 
578 
579 	/* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
580 	if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
581 	    && *(val = &line[sizeof("subnet=")-1]) != '\0') {
582 		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
583 			return bad_str(line);
584 		intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
585 						   "parse_parms subnet");
586 		intnetp->intnet_metric = 1;
587 		if (delim == ',') {
588 			intnetp->intnet_metric = (int)strtol(val+1,&p,0);
589 			if (*p != '\0'
590 			    || intnetp->intnet_metric <= 0
591 			    || intnetp->intnet_metric >= HOPCNT_INFINITY) {
592 				free(intnetp);
593 				return bad_str(line);
594 			}
595 		}
596 		if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
597 		    || intnetp->intnet_mask == HOST_MASK
598 		    || intnetp->intnet_addr == RIP_DEFAULT) {
599 			free(intnetp);
600 			return bad_str(line);
601 		}
602 		intnetp->intnet_addr = htonl(intnetp->intnet_addr);
603 		intnetp->intnet_next = intnets;
604 		intnets = intnetp;
605 		return 0;
606 	}
607 
608 	/* "ripv1_mask=x.y.z.u/mask1,mask2" must be alone on the line.
609 	 * This requires that x.y.z.u/mask1 be considered a subnet of
610 	 * x.y.z.u/mask2, as if x.y.z.u/mask2 were a class-full network.
611 	 */
612 	if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
613 	    && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
614 		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
615 		    || delim == '\0')
616 			return bad_str(line);
617 		if ((i = (int)strtol(val+1, &p, 0)) <= 0
618 		    || i > 32 || *p != '\0')
619 			return bad_str(line);
620 		r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
621 						  "parse_parms ripv1_mask");
622 		r1netp->r1net_mask = HOST_MASK << (32-i);
623 		if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
624 		    || r1netp->r1net_net == RIP_DEFAULT
625 		    || r1netp->r1net_mask > r1netp->r1net_match) {
626 			free(r1netp);
627 			return bad_str(line);
628 		}
629 		r1netp->r1net_next = r1nets;
630 		r1nets = r1netp;
631 		return 0;
632 	}
633 
634 	memset(&parm, 0, sizeof(parm));
635 
636 	for (;;) {
637 		tgt = line + strspn(line, " ,\n\r");
638 		if (*tgt == '\0' || *tgt == '#')
639 			break;
640 		line = tgt+strcspn(tgt, "= #,\n\r");
641 		delim = *line;
642 		if (delim == '=') {
643 			val0 = ++line;
644 			if (0 > parse_quote(&line, " #,\n\r",&delim,
645 					    buf,sizeof(buf)))
646 				return bad_str(tgt);
647 		} else {
648 			val0 = NULL;
649 		}
650 		if (delim != '\0') {
651 			for (;;) {
652 				*line = '\0';
653 				if (delim == '#')
654 					break;
655 				++line;
656 				if (delim != ' '
657 				    || (delim = *line) != ' ')
658 					break;
659 			}
660 		}
661 
662 		if (PARSEQ("if")) {
663 			if (parm.parm_name[0] != '\0'
664 			    || strlen(buf) > IF_NAME_LEN)
665 				return bad_str(tgt);
666 			strcpy(parm.parm_name, buf);
667 
668 		} else if (PARSEQ("addr")) {
669 			/* This is a bad idea, because the address based
670 			 * sets of parameters cannot be checked for
671 			 * consistency with the interface name parameters.
672 			 * The parm_net stuff is needed to allow several
673 			 * -F settings.
674 			 */
675 			if (val0 == NULL || !getnet(val0, &addr, &mask)
676 			    || parm.parm_name[0] != '\0')
677 				return bad_str(tgt);
678 			parm.parm_net = addr;
679 			parm.parm_mask = mask;
680 			parm.parm_name[0] = '\n';
681 
682 		} else if (PARSEQ("passwd")) {
683 			/* since cleartext passwords are so weak allow
684 			 * them anywhere
685 			 */
686 			if (val0 == NULL)
687 				return bad_str("no passwd");
688 			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
689 			if (msg) {
690 				*val0 = '\0';
691 				return bad_str(msg);
692 			}
693 
694 		} else if (PARSEQ("md5_passwd")) {
695 			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
696 			if (msg) {
697 				*val0 = '\0';
698 				return bad_str(msg);
699 			}
700 
701 		} else if (PARS("no_ag")) {
702 			parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
703 
704 		} else if (PARS("no_super_ag")) {
705 			parm.parm_int_state |= IS_NO_SUPER_AG;
706 
707 		} else if (PARS("no_rip_out")) {
708 			parm.parm_int_state |= IS_NO_RIP_OUT;
709 
710 		} else if (PARS("no_ripv1_in")) {
711 			parm.parm_int_state |= IS_NO_RIPV1_IN;
712 
713 		} else if (PARS("no_ripv2_in")) {
714 			parm.parm_int_state |= IS_NO_RIPV2_IN;
715 
716 		} else if (PARS("ripv2_out")) {
717 			if (parm.parm_int_state & IS_NO_RIPV2_OUT)
718 				return bad_str(tgt);
719 			parm.parm_int_state |= IS_NO_RIPV1_OUT;
720 
721 		} else if (PARS("ripv2")) {
722 			if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
723 			    || (parm.parm_int_state & IS_NO_RIPV2_IN))
724 				return bad_str(tgt);
725 			parm.parm_int_state |= (IS_NO_RIPV1_IN
726 						| IS_NO_RIPV1_OUT);
727 
728 		} else if (PARS("no_rip")) {
729 			CKF(IS_PM_RDISC, IS_NO_RIP);
730 
731 		} else if (PARS("no_rip_mcast")) {
732 			parm.parm_int_state |= IS_NO_RIP_MCAST;
733 
734 		} else if (PARS("no_rdisc")) {
735 			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
736 
737 		} else if (PARS("no_solicit")) {
738 			CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
739 
740 		} else if (PARS("send_solicit")) {
741 			CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
742 
743 		} else if (PARS("no_rdisc_adv")) {
744 			CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
745 
746 		} else if (PARS("rdisc_adv")) {
747 			CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
748 
749 		} else if (PARS("bcast_rdisc")) {
750 			parm.parm_int_state |= IS_BCAST_RDISC;
751 
752 		} else if (PARS("passive")) {
753 			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
754 			parm.parm_int_state |= IS_NO_RIP | IS_PASSIVE;
755 
756 		} else if (PARSEQ("rdisc_pref")) {
757 			if (parm.parm_rdisc_pref != 0
758 			    || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
759 				*p != '\0'))
760 				return bad_str(tgt);
761 
762 		} else if (PARS("pm_rdisc")) {
763 			if (IS_RIP_OUT_OFF(parm.parm_int_state))
764 				return bad_str(tgt);
765 			parm.parm_int_state |= IS_PM_RDISC;
766 
767 		} else if (PARSEQ("rdisc_interval")) {
768 			if (parm.parm_rdisc_int != 0
769 			    || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
770 				*p != '\0')
771 			    || parm.parm_rdisc_int < MinMaxAdvertiseInterval
772 			    || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
773 				return bad_str(tgt);
774 
775 		} else if (PARSEQ("fake_default")) {
776 			if (parm.parm_d_metric != 0
777 			    || IS_RIP_OUT_OFF(parm.parm_int_state)
778 			    || (i = strtoul(buf,&p,0), *p != '\0')
779 			    || i > HOPCNT_INFINITY-1)
780 				return bad_str(tgt);
781 			parm.parm_d_metric = i;
782 
783 		} else if (PARSEQ("adj_inmetric")) {
784 			if (parm.parm_adj_inmetric != 0
785 			    || (i = strtoul(buf,&p,0), *p != '\0')
786 			    || i > HOPCNT_INFINITY-1)
787 				return bad_str(tgt);
788 			parm.parm_adj_inmetric = i;
789 
790 		} else if (PARSEQ("adj_outmetric")) {
791 			if (parm.parm_adj_outmetric != 0
792 			    || (i = strtoul(buf,&p,0), *p != '\0')
793 			    || i > HOPCNT_INFINITY-1)
794 				return bad_str(tgt);
795 			parm.parm_adj_outmetric = i;
796 
797 		} else if (PARSEQ("trust_gateway")) {
798 			/* look for trust_gateway=x.y.z|net/mask|...) */
799 			p = buf;
800 			if (0 > parse_quote(&p, "|", &delim,
801 					    buf2, sizeof(buf2))
802 			    || !gethost(buf2,&addr))
803 				return bad_str(tgt);
804 			tg = (struct tgate *)rtmalloc(sizeof(*tg),
805 						      "parse_parms"
806 						      "trust_gateway");
807 			memset(tg, 0, sizeof(*tg));
808 			tg->tgate_addr = addr;
809 			i = 0;
810 			/* The default is to trust all routes. */
811 			while (delim == '|') {
812 				p++;
813 				if (i >= MAX_TGATE_NETS
814 				    || 0 > parse_quote(&p, "|", &delim,
815 						       buf2, sizeof(buf2))
816 				    || !getnet(buf2, &tg->tgate_nets[i].net,
817 					       &tg->tgate_nets[i].mask)
818 				    || tg->tgate_nets[i].net == RIP_DEFAULT
819 				    || tg->tgate_nets[i].mask == 0) {
820 					free(tg);
821 					return bad_str(tgt);
822 				}
823 				i++;
824 			}
825 			tg->tgate_next = tgates;
826 			tgates = tg;
827 			parm.parm_int_state |= IS_DISTRUST;
828 
829 		} else if (PARS("redirect_ok")) {
830 			parm.parm_int_state |= IS_REDIRECT_OK;
831 
832 		} else {
833 			return bad_str(tgt);	/* error */
834 		}
835 	}
836 
837 	return check_parms(&parm);
838 #undef PARS
839 #undef PARSEQ
840 }
841 
842 
843 /* check for duplicate parameter specifications */
844 const char *				/* 0 or error message */
845 check_parms(struct parm *new)
846 {
847 	struct parm *parmp, **parmpp;
848 	int i, num_passwds;
849 
850 	/* set implicit values
851 	 */
852 	if (new->parm_int_state & IS_NO_ADV_IN)
853 		new->parm_int_state |= IS_NO_SOL_OUT;
854 	if (new->parm_int_state & IS_NO_SOL_OUT)
855 		new->parm_int_state |= IS_NO_ADV_IN;
856 
857 	for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
858 		if (new->parm_auth[i].type != RIP_AUTH_NONE)
859 			num_passwds++;
860 	}
861 
862 	/* compare with existing sets of parameters
863 	 */
864 	for (parmpp = &parms;
865 	     (parmp = *parmpp) != NULL;
866 	     parmpp = &parmp->parm_next) {
867 		if (strcmp(new->parm_name, parmp->parm_name))
868 			continue;
869 		if (!on_net(htonl(parmp->parm_net),
870 			    new->parm_net, new->parm_mask)
871 		    && !on_net(htonl(new->parm_net),
872 			       parmp->parm_net, parmp->parm_mask))
873 			continue;
874 
875 		for (i = 0; i < MAX_AUTH_KEYS; i++) {
876 			if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
877 				num_passwds++;
878 		}
879 		if (num_passwds > MAX_AUTH_KEYS)
880 			return "too many conflicting passwords";
881 
882 		if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
883 		     && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
884 		     && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
885 			      & GROUP_IS_SOL_OUT))
886 		    || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
887 			&& 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
888 			&& 0 != ((new->parm_int_state ^ parmp->parm_int_state)
889 				 & GROUP_IS_ADV_OUT))
890 		    || (new->parm_rdisc_pref != 0
891 			&& parmp->parm_rdisc_pref != 0
892 			&& new->parm_rdisc_pref != parmp->parm_rdisc_pref)
893 		    || (new->parm_rdisc_int != 0
894 			&& parmp->parm_rdisc_int != 0
895 			&& new->parm_rdisc_int != parmp->parm_rdisc_int)) {
896 			return ("conflicting, duplicate router discovery"
897 				" parameters");
898 
899 		}
900 
901 		if (new->parm_d_metric != 0
902 		     && parmp->parm_d_metric != 0
903 		     && new->parm_d_metric != parmp->parm_d_metric) {
904 			return ("conflicting, duplicate poor man's router"
905 				" discovery or fake default metric");
906 		}
907 
908 		if (new->parm_adj_inmetric != 0
909 		    && parmp->parm_adj_inmetric != 0
910 		    && new->parm_adj_inmetric != parmp->parm_adj_inmetric) {
911 			return ("conflicting interface input "
912 				"metric adjustments");
913 		}
914 
915 		if (new->parm_adj_outmetric != 0
916 		    && parmp->parm_adj_outmetric != 0
917 		    && new->parm_adj_outmetric != parmp->parm_adj_outmetric) {
918 			return ("conflicting interface output "
919 				"metric adjustments");
920 		}
921 	}
922 
923 	/* link new entry on the list so that when the entries are scanned,
924 	 * they affect the result in the order the operator specified.
925 	 */
926 	parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
927 	memcpy(parmp, new, sizeof(*parmp));
928 	*parmpp = parmp;
929 
930 	return 0;
931 }
932 
933 
934 /* get a network number as a name or a number, with an optional "/xx"
935  * netmask.
936  */
937 int					/* 0=bad */
938 getnet(char *name,
939        naddr *netp,			/* network in host byte order */
940        naddr *maskp)			/* masks are always in host order */
941 {
942 	int i;
943 	struct netent *np;
944 	naddr mask;			/* in host byte order */
945 	struct in_addr in;		/* a network and so host byte order */
946 	char hname[MAXHOSTNAMELEN+1];
947 	char *mname, *p;
948 
949 
950 	/* Detect and separate "1.2.3.4/24"
951 	 */
952 	if (NULL != (mname = strrchr(name,'/'))) {
953 		i = (int)(mname - name);
954 		if (i > (int)sizeof(hname)-1)	/* name too long */
955 			return 0;
956 		memmove(hname, name, i);
957 		hname[i] = '\0';
958 		mname++;
959 		name = hname;
960 	}
961 
962 	np = getnetbyname(name);
963 	if (np != NULL) {
964 		in.s_addr = (naddr)np->n_net;
965 		if (0 == (in.s_addr & 0xff000000))
966 			in.s_addr <<= 8;
967 		if (0 == (in.s_addr & 0xff000000))
968 			in.s_addr <<= 8;
969 		if (0 == (in.s_addr & 0xff000000))
970 			in.s_addr <<= 8;
971 	} else if (inet_aton(name, &in) == 1) {
972 		in.s_addr = ntohl(in.s_addr);
973 	} else if (!mname && !strcasecmp(name,"default")) {
974 		in.s_addr = RIP_DEFAULT;
975 	} else {
976 		return 0;
977 	}
978 
979 	if (!mname) {
980 		/* we cannot use the interfaces here because we have not
981 		 * looked at them yet.
982 		 */
983 		mask = std_mask(htonl(in.s_addr));
984 		if ((~mask & in.s_addr) != 0)
985 			mask = HOST_MASK;
986 	} else {
987 		mask = (naddr)strtoul(mname, &p, 0);
988 		if (*p != '\0' || mask > 32)
989 			return 0;
990 		if (mask != 0)
991 			mask = HOST_MASK << (32-mask);
992 	}
993 
994 	/* must have mask of 0 with default */
995 	if (mask != 0 && in.s_addr == RIP_DEFAULT)
996 		return 0;
997 	/* no host bits allowed in a network number */
998 	if ((~mask & in.s_addr) != 0)
999 		return 0;
1000 	/* require non-zero network number */
1001 	if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
1002 		return 0;
1003 	if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
1004 		return 0;
1005 	if (in.s_addr>>24 == 0xff)
1006 		return 0;
1007 
1008 	*netp = in.s_addr;
1009 	*maskp = mask;
1010 	return 1;
1011 }
1012 
1013 
1014 int					/* 0=bad */
1015 gethost(char *name,
1016 	naddr *addrp)
1017 {
1018 	struct hostent *hp;
1019 	struct in_addr in;
1020 
1021 
1022 	/* Try for a number first, even in IRIX where gethostbyname()
1023 	 * is smart.  This avoids hitting the name server which
1024 	 * might be sick because routing is.
1025 	 */
1026 	if (inet_aton(name, &in) == 1) {
1027 		/* get a good number, but check that it it makes some
1028 		 * sense.
1029 		 */
1030 		if (ntohl(in.s_addr)>>24 == 0
1031 		    || ntohl(in.s_addr)>>24 == 0xff)
1032 			return 0;
1033 		*addrp = in.s_addr;
1034 		return 1;
1035 	}
1036 
1037 	hp = gethostbyname(name);
1038 	if (hp) {
1039 		memcpy(addrp, hp->h_addr, sizeof(*addrp));
1040 		return 1;
1041 	}
1042 
1043 	return 0;
1044 }
1045