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