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