xref: /dragonfly/sbin/routed/parms.c (revision 1de703da)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgment:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sbin/routed/parms.c,v 1.7.2.1 2000/08/14 17:00:03 sheldonh Exp $
34  * $DragonFly: src/sbin/routed/parms.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
35  */
36 
37 #include "defs.h"
38 #include "pathnames.h"
39 #include <sys/stat.h>
40 
41 #if !defined(sgi) && !defined(__NetBSD__)
42 static char sccsid[] __attribute__((unused)) = "@(#)if.c	8.1 (Berkeley) 6/5/93";
43 #elif defined(__NetBSD__)
44 __RCSID("$NetBSD$");
45 #endif
46 #ident "$FreeBSD: src/sbin/routed/parms.c,v 1.7.2.1 2000/08/14 17:00:03 sheldonh Exp $"
47 
48 
49 struct parm *parms;
50 struct intnet *intnets;
51 struct r1net *r1nets;
52 struct tgate *tgates;
53 
54 
55 /* use configured parameters
56  */
57 void
58 get_parms(struct interface *ifp)
59 {
60 	static int warned_auth_in, warned_auth_out;
61 	struct parm *parmp;
62 	int i, num_passwds = 0;
63 
64 	/* get all relevant parameters
65 	 */
66 	for (parmp = parms; parmp != 0; parmp = parmp->parm_next) {
67 		if (parmp->parm_name[0] == '\0'
68 		    || !strcmp(ifp->int_name, parmp->parm_name)
69 		    || (parmp->parm_name[0] == '\n'
70 			&& on_net(ifp->int_addr,
71 				  parmp->parm_net, parmp->parm_mask))) {
72 
73 			/* This group of parameters is relevant,
74 			 * so get its settings
75 			 */
76 			ifp->int_state |= parmp->parm_int_state;
77 			for (i = 0; i < MAX_AUTH_KEYS; i++) {
78 				if (parmp->parm_auth[0].type == RIP_AUTH_NONE
79 				    || num_passwds >= MAX_AUTH_KEYS)
80 					break;
81 				memcpy(&ifp->int_auth[num_passwds++],
82 				       &parmp->parm_auth[i],
83 				       sizeof(ifp->int_auth[0]));
84 			}
85 			if (parmp->parm_rdisc_pref != 0)
86 				ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
87 			if (parmp->parm_rdisc_int != 0)
88 				ifp->int_rdisc_int = parmp->parm_rdisc_int;
89 			if (parmp->parm_d_metric != 0)
90 				ifp->int_d_metric = parmp->parm_d_metric;
91 		}
92 	}
93 
94 	/* Set general defaults.
95 	 *
96 	 * Default poor-man's router discovery to a metric that will
97 	 * be heard by old versions of `routed`.  They ignored received
98 	 * routes with metric 15.
99 	 */
100 	if ((ifp->int_state & IS_PM_RDISC)
101 	    && ifp->int_d_metric == 0)
102 		ifp->int_d_metric = FAKE_METRIC;
103 
104 	if (ifp->int_rdisc_int == 0)
105 		ifp->int_rdisc_int = DefMaxAdvertiseInterval;
106 
107 	if (!(ifp->int_if_flags & IFF_MULTICAST)
108 	    && !(ifp->int_state & IS_REMOTE))
109 		ifp->int_state |= IS_BCAST_RDISC;
110 
111 	if (ifp->int_if_flags & IFF_POINTOPOINT) {
112 		ifp->int_state |= IS_BCAST_RDISC;
113 		/* By default, point-to-point links should be passive
114 		 * about router-discovery for the sake of demand-dialing.
115 		 */
116 		if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
117 			ifp->int_state |= IS_NO_SOL_OUT;
118 		if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
119 			ifp->int_state |= IS_NO_ADV_OUT;
120 	}
121 
122 	if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
123 		ifp->int_state |= IS_NO_RDISC;
124 	if (ifp->int_state & IS_PASSIVE)
125 		ifp->int_state |= IS_NO_RIP;
126 
127 	if (!IS_RIP_IN_OFF(ifp->int_state)
128 	    && ifp->int_auth[0].type != RIP_AUTH_NONE
129 	    && !(ifp->int_state & IS_NO_RIPV1_IN)
130 	    && !warned_auth_in) {
131 		msglog("Warning: RIPv1 input via %s"
132 		       " will be accepted without authentication",
133 		       ifp->int_name);
134 		warned_auth_in = 1;
135 	}
136 	if (!IS_RIP_OUT_OFF(ifp->int_state)
137 	    && ifp->int_auth[0].type != RIP_AUTH_NONE
138 	    && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
139 		if (!warned_auth_out) {
140 			msglog("Warning: RIPv1 output via %s"
141 			       " will be sent without authentication",
142 			       ifp->int_name);
143 			warned_auth_out = 1;
144 		}
145 	}
146 }
147 
148 
149 /* Read a list of gateways from /etc/gateways and add them to our tables.
150  *
151  * This file contains a list of "remote" gateways.  That is usually
152  * a gateway which we cannot immediately determine if it is present or
153  * not as we can do for those provided by directly connected hardware.
154  *
155  * If a gateway is marked "passive" in the file, then we assume it
156  * does not understand RIP and assume it is always present.  Those
157  * not marked passive are treated as if they were directly connected
158  * and assumed to be broken if they do not send us advertisements.
159  * All remote interfaces are added to our list, and those not marked
160  * passive are sent routing updates.
161  *
162  * A passive interface can also be local, hardware interface exempt
163  * from RIP.
164  */
165 void
166 gwkludge(void)
167 {
168 	FILE *fp;
169 	char *p, *lptr;
170 	const char *cp;
171 	char lbuf[200], net_host[5], dname[64+1+64+1];
172 	char gname[GNAME_LEN+1], qual[9];
173 	struct interface *ifp;
174 	naddr dst, netmask, gate;
175 	int metric, n, lnum;
176 	struct stat sb;
177 	u_int state;
178 	const char *type;
179 
180 
181 	fp = fopen(_PATH_GATEWAYS, "r");
182 	if (fp == 0)
183 		return;
184 
185 	if (0 > fstat(fileno(fp), &sb)) {
186 		msglog("could not stat() "_PATH_GATEWAYS);
187 		(void)fclose(fp);
188 		return;
189 	}
190 
191 	for (lnum = 1; ; lnum++) {
192 		if (0 == fgets(lbuf, sizeof(lbuf), fp))
193 			break;
194 		lptr = lbuf;
195 		while (*lptr == ' ')
196 			lptr++;
197 		p = lptr+strlen(lptr)-1;
198 		while (*p == '\n'
199 		       || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
200 			*p-- = '\0';
201 		if (*lptr == '\0'	/* ignore null and comment lines */
202 		    || *lptr == '#')
203 			continue;
204 
205 		/* notice newfangled parameter lines
206 		 */
207 		if (strncasecmp("net", lptr, 3)
208 		    && strncasecmp("host", lptr, 4)) {
209 			cp = parse_parms(lptr,
210 					 (sb.st_uid == 0
211 					  && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
212 			if (cp != 0)
213 				msglog("%s in line %d of "_PATH_GATEWAYS,
214 				       cp, lnum);
215 			continue;
216 		}
217 
218 /*  {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
219 		qual[0] = '\0';
220 		/* the '64' here must be GNAME_LEN */
221 		n = sscanf(lptr, "%4s %129[^ \t] gateway"
222 			   " %64[^ / \t] metric %u %8s\n",
223 			   net_host, dname, gname, &metric, qual);
224 		if (n != 4 && n != 5) {
225 			msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
226 			       lptr, n);
227 			continue;
228 		}
229 		if (metric >= HOPCNT_INFINITY) {
230 			msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
231 			       lptr);
232 			continue;
233 		}
234 		if (!strcasecmp(net_host, "host")) {
235 			if (!gethost(dname, &dst)) {
236 				msglog("bad host \"%s\" in "_PATH_GATEWAYS
237 				       " entry \"%s\"", dname, lptr);
238 				continue;
239 			}
240 			netmask = HOST_MASK;
241 		} else if (!strcasecmp(net_host, "net")) {
242 			if (!getnet(dname, &dst, &netmask)) {
243 				msglog("bad net \"%s\" in "_PATH_GATEWAYS
244 				       " entry \"%s\"", dname, lptr);
245 				continue;
246 			}
247 			if (dst == RIP_DEFAULT) {
248 				msglog("bad net \"%s\" in "_PATH_GATEWAYS
249 				       " entry \"%s\"--cannot be default",
250 				       dname, lptr);
251 				continue;
252 			}
253 			HTONL(dst);	/* make network # into IP address */
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,0);
318 		if (ifp != 0) {
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 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
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 != 0)
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 #if defined(sgi) || defined(__NetBSD__)
441 	char *ptr;
442 #endif
443 
444 	if (0 > parse_quote(valp, "| ,\n\r", delimp,
445 			    buf,bufsize)
446 	    || buf[bufsize-1] != '\0'
447 	    || buf[bufsize-2] != '\0') {
448 		sprintf(buf,"bad timestamp %.25s", val0);
449 		return buf;
450 	}
451 	strcat(buf,"\n");
452 	memset(&tm, 0, sizeof(tm));
453 #if defined(sgi) || defined(__NetBSD__)
454 	ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
455 	if (ptr == NULL || *ptr != '\0') {
456 		sprintf(buf,"bad timestamp %.25s", val0);
457 		return buf;
458 	}
459 #else
460 	if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
461 			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
462 			&tm.tm_hour, &tm.tm_min)
463 	    || tm.tm_mon < 1 || tm.tm_mon > 12
464 	    || tm.tm_mday < 1 || tm.tm_mday > 31) {
465 		sprintf(buf,"bad timestamp %.25s", val0);
466 		return buf;
467 	}
468 	tm.tm_mon--;
469 	if (tm.tm_year <= 37)		/* assume small years are in the */
470 		tm.tm_year += 100;	/* 3rd millenium */
471 #endif
472 
473 	if ((*tp = mktime(&tm)) == -1) {
474 		sprintf(buf,"bad timestamp %.25s", val0);
475 		return buf;
476 	}
477 
478 	return 0;
479 }
480 
481 
482 /* Get a password, key ID, and expiration date in the format
483  *	passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
484  */
485 static const char *			/* 0 or error message */
486 get_passwd(char *tgt,
487 	   char *val,
488 	   struct parm *parmp,
489 	   u_int16_t type,
490 	   int safe)			/* 1=from secure file */
491 {
492 	static char buf[80];
493 	char *val0, *p, delim;
494 	struct auth k, *ap, *ap2;
495 	int i;
496 	u_long l;
497 
498 
499 	if (!safe)
500 		return "ignore unsafe password";
501 
502 	for (ap = parmp->parm_auth, i = 0;
503 	     ap->type != RIP_AUTH_NONE; i++, ap++) {
504 		if (i >= MAX_AUTH_KEYS)
505 			return "too many passwords";
506 	}
507 
508 	memset(&k, 0, sizeof(k));
509 	k.type = type;
510 	k.end = -1-DAY;
511 
512 	val0 = val;
513 	if (0 > parse_quote(&val, "| ,\n\r", &delim,
514 			    (char *)k.key, sizeof(k.key)))
515 		return tgt;
516 
517 	if (delim != '|') {
518 		if (type == RIP_AUTH_MD5)
519 			return "missing Keyid";
520 	} else {
521 		val0 = ++val;
522 		buf[sizeof(buf)-1] = '\0';
523 		if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
524 		    || buf[sizeof(buf)-1] != '\0'
525 		    || (l = strtoul(buf,&p,0)) > 255
526 		    || *p != '\0') {
527 			sprintf(buf,"bad KeyID \"%.20s\"", val0);
528 			return buf;
529 		}
530 		for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
531 			if (ap2->keyid == l) {
532 				sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
533 				return buf;
534 			}
535 		}
536 		k.keyid = (int)l;
537 
538 		if (delim == '|') {
539 			val0 = ++val;
540 			if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
541 					       buf,sizeof(buf))))
542 				return p;
543 			if (delim != '|')
544 				return "missing second timestamp";
545 			val0 = ++val;
546 			if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
547 					       buf,sizeof(buf))))
548 				return p;
549 			if ((u_long)k.start > (u_long)k.end) {
550 				sprintf(buf,"out of order timestamp %.30s",
551 					val0);
552 				return buf;
553 			}
554 		}
555 	}
556 	if (delim != '\0')
557 		return tgt;
558 
559 	memmove(ap, &k, sizeof(*ap));
560 	return 0;
561 }
562 
563 
564 static const char *
565 bad_str(const char *estr)
566 {
567 	static char buf[100+8];
568 
569 	sprintf(buf, "bad \"%.100s\"", estr);
570 	return buf;
571 }
572 
573 
574 /* Parse a set of parameters for an interface.
575  */
576 const char *					/* 0 or error message */
577 parse_parms(char *line,
578 	    int safe)			/* 1=from secure file */
579 {
580 #define PARS(str) (!strcasecmp(tgt, str))
581 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
582 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break;	\
583 	parm.parm_int_state |= (b);}
584 	struct parm parm;
585 	struct intnet *intnetp;
586 	struct r1net *r1netp;
587 	struct tgate *tg;
588 	naddr addr, mask;
589 	char delim, *val0 = 0, *tgt, *val, *p;
590 	const char *msg;
591 	char buf[BUFSIZ], buf2[BUFSIZ];
592 	int i;
593 
594 
595 	/* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
596 	if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
597 	    && *(val = &line[sizeof("subnet=")-1]) != '\0') {
598 		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
599 			return bad_str(line);
600 		intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
601 						   "parse_parms subnet");
602 		intnetp->intnet_metric = 1;
603 		if (delim == ',') {
604 			intnetp->intnet_metric = (int)strtol(val+1,&p,0);
605 			if (*p != '\0'
606 			    || intnetp->intnet_metric <= 0
607 			    || intnetp->intnet_metric >= HOPCNT_INFINITY)
608 				return bad_str(line);
609 		}
610 		if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
611 		    || intnetp->intnet_mask == HOST_MASK
612 		    || intnetp->intnet_addr == RIP_DEFAULT) {
613 			free(intnetp);
614 			return bad_str(line);
615 		}
616 		HTONL(intnetp->intnet_addr);
617 		intnetp->intnet_next = intnets;
618 		intnets = intnetp;
619 		return 0;
620 	}
621 
622 	/* "ripv1_mask=x.y.z.u/mask1,mask2" must be alone on the line.
623 	 * This requires that x.y.z.u/mask1 be considered a subnet of
624 	 * x.y.z.u/mask2, as if x.y.z.u/mask2 were a class-full network.
625 	 */
626 	if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
627 	    && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
628 		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
629 		    || delim == '\0')
630 			return bad_str(line);
631 		if ((i = (int)strtol(val+1, &p, 0)) <= 0
632 		    || i > 32 || *p != '\0')
633 			return bad_str(line);
634 		r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
635 						  "parse_parms ripv1_mask");
636 		r1netp->r1net_mask = HOST_MASK << (32-i);
637 		if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
638 		    || r1netp->r1net_net == RIP_DEFAULT
639 		    || r1netp->r1net_mask > r1netp->r1net_match) {
640 			free(r1netp);
641 			return bad_str(line);
642 		}
643 		r1netp->r1net_next = r1nets;
644 		r1nets = r1netp;
645 		return 0;
646 	}
647 
648 	memset(&parm, 0, sizeof(parm));
649 
650 	for (;;) {
651 		tgt = line + strspn(line, " ,\n\r");
652 		if (*tgt == '\0' || *tgt == '#')
653 			break;
654 		line = tgt+strcspn(tgt, "= #,\n\r");
655 		delim = *line;
656 		if (delim == '=') {
657 			val0 = ++line;
658 			if (0 > parse_quote(&line, " #,\n\r",&delim,
659 					    buf,sizeof(buf)))
660 				return bad_str(tgt);
661 		}
662 		if (delim != '\0') {
663 			for (;;) {
664 				*line = '\0';
665 				if (delim == '#')
666 					break;
667 				++line;
668 				if (delim != ' '
669 				    || (delim = *line) != ' ')
670 					break;
671 			}
672 		}
673 
674 		if (PARSEQ("if")) {
675 			if (parm.parm_name[0] != '\0'
676 			    || strlen(buf) > IF_NAME_LEN)
677 				return bad_str(tgt);
678 			strcpy(parm.parm_name, buf);
679 
680 		} else if (PARSEQ("addr")) {
681 			/* This is a bad idea, because the address based
682 			 * sets of parameters cannot be checked for
683 			 * consistency with the interface name parameters.
684 			 * The parm_net stuff is needed to allow several
685 			 * -F settings.
686 			 */
687 			if (!getnet(val0, &addr, &mask)
688 			    || parm.parm_name[0] != '\0')
689 				return bad_str(tgt);
690 			parm.parm_net = addr;
691 			parm.parm_mask = mask;
692 			parm.parm_name[0] = '\n';
693 
694 		} else if (PARSEQ("passwd")) {
695 			/* since cleartext passwords are so weak allow
696 			 * them anywhere
697 			 */
698 			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
699 			if (msg) {
700 				*val0 = '\0';
701 				return bad_str(msg);
702 			}
703 
704 		} else if (PARSEQ("md5_passwd")) {
705 			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
706 			if (msg) {
707 				*val0 = '\0';
708 				return bad_str(msg);
709 			}
710 
711 		} else if (PARS("no_ag")) {
712 			parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
713 
714 		} else if (PARS("no_super_ag")) {
715 			parm.parm_int_state |= IS_NO_SUPER_AG;
716 
717 		} else if (PARS("no_ripv1_in")) {
718 			parm.parm_int_state |= IS_NO_RIPV1_IN;
719 
720 		} else if (PARS("no_ripv2_in")) {
721 			parm.parm_int_state |= IS_NO_RIPV2_IN;
722 
723 		} else if (PARS("ripv2_out")) {
724 			if (parm.parm_int_state & IS_NO_RIPV2_OUT)
725 				return bad_str(tgt);
726 			parm.parm_int_state |= IS_NO_RIPV1_OUT;
727 
728 		} else if (PARS("ripv2")) {
729 			if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
730 			    || (parm.parm_int_state & IS_NO_RIPV2_IN))
731 				return bad_str(tgt);
732 			parm.parm_int_state |= (IS_NO_RIPV1_IN
733 						| IS_NO_RIPV1_OUT);
734 
735 		} else if (PARS("no_rip")) {
736 			CKF(IS_PM_RDISC, IS_NO_RIP);
737 
738 		} else if (PARS("no_rip_mcast")) {
739 			parm.parm_int_state |= IS_NO_RIP_MCAST;
740 
741 		} else if (PARS("no_rdisc")) {
742 			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
743 
744 		} else if (PARS("no_solicit")) {
745 			CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
746 
747 		} else if (PARS("send_solicit")) {
748 			CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
749 
750 		} else if (PARS("no_rdisc_adv")) {
751 			CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
752 
753 		} else if (PARS("rdisc_adv")) {
754 			CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
755 
756 		} else if (PARS("bcast_rdisc")) {
757 			parm.parm_int_state |= IS_BCAST_RDISC;
758 
759 		} else if (PARS("passive")) {
760 			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
761 			parm.parm_int_state |= IS_NO_RIP| IS_PASSIVE;
762 
763 		} else if (PARSEQ("rdisc_pref")) {
764 			if (parm.parm_rdisc_pref != 0
765 			    || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
766 				*p != '\0'))
767 				return bad_str(tgt);
768 
769 		} else if (PARS("pm_rdisc")) {
770 			if (IS_RIP_OUT_OFF(parm.parm_int_state))
771 				return bad_str(tgt);
772 			parm.parm_int_state |= IS_PM_RDISC;
773 
774 		} else if (PARSEQ("rdisc_interval")) {
775 			if (parm.parm_rdisc_int != 0
776 			    || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
777 				*p != '\0')
778 			    || parm.parm_rdisc_int < MinMaxAdvertiseInterval
779 			    || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
780 				return bad_str(tgt);
781 
782 		} else if (PARSEQ("fake_default")) {
783 			if (parm.parm_d_metric != 0
784 			    || IS_RIP_OUT_OFF(parm.parm_int_state)
785 			    || (parm.parm_d_metric = (int)strtoul(buf,&p,0),
786 				*p != '\0')
787 			    || parm.parm_d_metric > HOPCNT_INFINITY-1)
788 				return bad_str(tgt);
789 
790 		} else if (PARSEQ("trust_gateway")) {
791 			/* look for trust_gateway=x.y.z|net/mask|...) */
792 			p = buf;
793 			if (0 > parse_quote(&p, "|", &delim,
794 					    buf2, sizeof(buf2))
795 			    || !gethost(buf2,&addr))
796 				return bad_str(tgt);
797 			tg = (struct tgate *)rtmalloc(sizeof(*tg),
798 						      "parse_parms"
799 						      "trust_gateway");
800 			memset(tg, 0, sizeof(*tg));
801 			tg->tgate_addr = addr;
802 			i = 0;
803 			/* The default is to trust all routes. */
804 			while (delim == '|') {
805 				p++;
806 				if (i >= MAX_TGATE_NETS
807 				    || 0 > parse_quote(&p, "|", &delim,
808 						       buf2, sizeof(buf2))
809 				    || !getnet(buf2, &tg->tgate_nets[i].net,
810 					       &tg->tgate_nets[i].mask)
811 				    || tg->tgate_nets[i].net == RIP_DEFAULT
812 				    || tg->tgate_nets[i].mask == 0)
813 					return bad_str(tgt);
814 				i++;
815 			}
816 			tg->tgate_next = tgates;
817 			tgates = tg;
818 			parm.parm_int_state |= IS_DISTRUST;
819 
820 		} else if (PARS("redirect_ok")) {
821 			parm.parm_int_state |= IS_REDIRECT_OK;
822 
823 		} else {
824 			return bad_str(tgt);	/* error */
825 		}
826 	}
827 
828 	return check_parms(&parm);
829 #undef PARS
830 #undef PARSEQ
831 }
832 
833 
834 /* check for duplicate parameter specifications */
835 const char *				/* 0 or error message */
836 check_parms(struct parm *new)
837 {
838 	struct parm *parmp, **parmpp;
839 	int i, num_passwds;
840 
841 	/* set implicit values
842 	 */
843 	if (new->parm_int_state & IS_NO_ADV_IN)
844 		new->parm_int_state |= IS_NO_SOL_OUT;
845 	if (new->parm_int_state & IS_NO_SOL_OUT)
846 		new->parm_int_state |= IS_NO_ADV_IN;
847 
848 	for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
849 		if (new->parm_auth[i].type != RIP_AUTH_NONE)
850 			num_passwds++;
851 	}
852 
853 	/* compare with existing sets of parameters
854 	 */
855 	for (parmpp = &parms;
856 	     (parmp = *parmpp) != 0;
857 	     parmpp = &parmp->parm_next) {
858 		if (strcmp(new->parm_name, parmp->parm_name))
859 			continue;
860 		if (!on_net(htonl(parmp->parm_net),
861 			    new->parm_net, new->parm_mask)
862 		    && !on_net(htonl(new->parm_net),
863 			       parmp->parm_net, parmp->parm_mask))
864 			continue;
865 
866 		for (i = 0; i < MAX_AUTH_KEYS; i++) {
867 			if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
868 				num_passwds++;
869 		}
870 		if (num_passwds > MAX_AUTH_KEYS)
871 			return "too many conflicting passwords";
872 
873 		if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
874 		     && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
875 		     && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
876 			      && GROUP_IS_SOL_OUT))
877 		    || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
878 			&& 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
879 			&& 0 != ((new->parm_int_state ^ parmp->parm_int_state)
880 				 && GROUP_IS_ADV_OUT))
881 		    || (new->parm_rdisc_pref != 0
882 			&& parmp->parm_rdisc_pref != 0
883 			&& new->parm_rdisc_pref != parmp->parm_rdisc_pref)
884 		    || (new->parm_rdisc_int != 0
885 			&& parmp->parm_rdisc_int != 0
886 			&& new->parm_rdisc_int != parmp->parm_rdisc_int)) {
887 			return ("conflicting, duplicate router discovery"
888 				" parameters");
889 
890 		}
891 
892 		if (new->parm_d_metric != 0
893 		     && parmp->parm_d_metric != 0
894 		     && new->parm_d_metric != parmp->parm_d_metric) {
895 			return ("conflicting, duplicate poor man's router"
896 				" discovery or fake default metric");
897 		}
898 	}
899 
900 	/* link new entry on the so that when the entries are scanned,
901 	 * they affect the result in the order the operator specified.
902 	 */
903 	parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
904 	memcpy(parmp, new, sizeof(*parmp));
905 	*parmpp = parmp;
906 
907 	return 0;
908 }
909 
910 
911 /* get a network number as a name or a number, with an optional "/xx"
912  * netmask.
913  */
914 int					/* 0=bad */
915 getnet(char *name,
916        naddr *netp,			/* network in host byte order */
917        naddr *maskp)			/* masks are always in host order */
918 {
919 	int i;
920 	struct netent *np;
921 	naddr mask;			/* in host byte order */
922 	struct in_addr in;		/* a network and so host byte order */
923 	char hname[MAXHOSTNAMELEN+1];
924 	char *mname, *p;
925 
926 
927 	/* Detect and separate "1.2.3.4/24"
928 	 */
929 	if (0 != (mname = strrchr(name,'/'))) {
930 		i = (int)(mname - name);
931 		if (i > (int)sizeof(hname)-1)	/* name too long */
932 			return 0;
933 		memmove(hname, name, i);
934 		hname[i] = '\0';
935 		mname++;
936 		name = hname;
937 	}
938 
939 	np = getnetbyname(name);
940 	if (np != 0) {
941 		in.s_addr = (naddr)np->n_net;
942 		if (0 == (in.s_addr & 0xff000000))
943 			in.s_addr <<= 8;
944 		if (0 == (in.s_addr & 0xff000000))
945 			in.s_addr <<= 8;
946 		if (0 == (in.s_addr & 0xff000000))
947 			in.s_addr <<= 8;
948 	} else if (inet_aton(name, &in) == 1) {
949 		NTOHL(in.s_addr);
950 	} else if (!mname && !strcasecmp(name,"default")) {
951 		in.s_addr = RIP_DEFAULT;
952 	} else {
953 		return 0;
954 	}
955 
956 	if (!mname) {
957 		/* we cannot use the interfaces here because we have not
958 		 * looked at them yet.
959 		 */
960 		mask = std_mask(htonl(in.s_addr));
961 		if ((~mask & in.s_addr) != 0)
962 			mask = HOST_MASK;
963 	} else {
964 		mask = (naddr)strtoul(mname, &p, 0);
965 		if (*p != '\0' || mask > 32)
966 			return 0;
967 		if (mask != 0)
968 			mask = HOST_MASK << (32-mask);
969 	}
970 
971 	/* must have mask of 0 with default */
972 	if (mask != 0 && in.s_addr == RIP_DEFAULT)
973 		return 0;
974 	/* no host bits allowed in a network number */
975 	if ((~mask & in.s_addr) != 0)
976 		return 0;
977 	/* require non-zero network number */
978 	if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
979 		return 0;
980 	if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
981 		return 0;
982 	if (in.s_addr>>24 == 0xff)
983 		return 0;
984 
985 	*netp = in.s_addr;
986 	*maskp = mask;
987 	return 1;
988 }
989 
990 
991 int					/* 0=bad */
992 gethost(char *name,
993 	naddr *addrp)
994 {
995 	struct hostent *hp;
996 	struct in_addr in;
997 
998 
999 	/* Try for a number first, even in IRIX where gethostbyname()
1000 	 * is smart.  This avoids hitting the name server which
1001 	 * might be sick because routing is.
1002 	 */
1003 	if (inet_aton(name, &in) == 1) {
1004 		/* get a good number, but check that it it makes some
1005 		 * sense.
1006 		 */
1007 		if (ntohl(in.s_addr)>>24 == 0
1008 		    || ntohl(in.s_addr)>>24 == 0xff)
1009 			return 0;
1010 		*addrp = in.s_addr;
1011 		return 1;
1012 	}
1013 
1014 	hp = gethostbyname(name);
1015 	if (hp) {
1016 		memcpy(addrp, hp->h_addr, sizeof(*addrp));
1017 		return 1;
1018 	}
1019 
1020 	return 0;
1021 }
1022