Home | History | Annotate | Line # | Download | only in routed
parms.c revision 1.8
      1 /*	$NetBSD: parms.c,v 1.8 1997/09/19 16:25:49 christos 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.8 1997/09/19 16:25:49 christos 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 				memmove(&ifp->int_auth[num_passwds++],
     80 				    &parmp->parm_auth[i],
     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 = '\0';
    370 	char *pc, *p;
    371 
    372 
    373 	pc = *linep;
    374 	if (*pc == '\0')
    375 		return -1;
    376 
    377 	while (lim != 0) {
    378 		c = *pc++;
    379 		if (c == '\0')
    380 			break;
    381 
    382 		if (c == '\\' && pc != '\0') {
    383 			if ((c = *pc++) == 'n') {
    384 				c = '\n';
    385 			} else if (c == 'r') {
    386 				c = '\r';
    387 			} else if (c == 't') {
    388 				c = '\t';
    389 			} else if (c == 'b') {
    390 				c = '\b';
    391 			} else if (c >= '0' && c <= '7') {
    392 				c -= '0';
    393 				if (*pc >= '0' && *pc <= '7') {
    394 					c = (c<<3)+(*pc++ - '0');
    395 					if (*pc >= '0' && *pc <= '7')
    396 					    c = (c<<3)+(*pc++ - '0');
    397 				}
    398 			}
    399 
    400 		} else {
    401 			for (p = delims; *p != '\0'; ++p) {
    402 				if (*p == c)
    403 					goto exit;
    404 			}
    405 		}
    406 
    407 		*buf++ = c;
    408 		--lim;
    409 	}
    410 exit:
    411 	if (lim == 0)
    412 		return -1;
    413 
    414 	*buf = '\0';
    415 	if (delimp != 0)
    416 		*delimp = c;
    417 	*linep = pc-1;
    418 	return 0;
    419 }
    420 
    421 
    422 /* Parse password timestamp
    423  */
    424 static char *
    425 parse_ts(time_t *tp,
    426 	 char **valp,
    427 	 char *val0,
    428 	 char *delimp,
    429 	 char *buf,
    430 	 u_int bufsize)
    431 {
    432 	struct tm tm;
    433 
    434 	if (0 > parse_quote(valp, "| ,\n\r", delimp,
    435 			    buf,bufsize)
    436 	    || buf[bufsize-1] != '\0'
    437 	    || buf[bufsize-2] != '\0') {
    438 		sprintf(buf,"bad timestamp %.25s", val0);
    439 		return buf;
    440 	}
    441 	strcat(buf,"\n");
    442 	memset(&tm, 0, sizeof(tm));
    443 	if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
    444 			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
    445 			&tm.tm_hour, &tm.tm_min)) {
    446 		sprintf(buf,"bad timestamp %.25s", val0);
    447 		return buf;
    448 	}
    449 	if (tm.tm_year <= 37)
    450 		tm.tm_year += 100;
    451 
    452 	if ((*tp = mktime(&tm)) == -1) {
    453 		sprintf(buf,"bad timestamp %.25s", val0);
    454 		return buf;
    455 	}
    456 
    457 	return 0;
    458 }
    459 
    460 
    461 /* Get a password, key ID, and expiration date in the format
    462  *	passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
    463  */
    464 static char *				/* 0 or error message */
    465 get_passwd(char *tgt,
    466 	   char *val,
    467 	   struct parm *parmp,
    468 	   u_char type,
    469 	   int safe)			/* 1=from secure file */
    470 {
    471 	static char buf[80];
    472 	char *val0, *p, delim;
    473 	struct auth k, *ap, *ap2;
    474 	int i;
    475 	u_long l;
    476 
    477 
    478 	if (!safe)
    479 		return "ignore unsafe password";
    480 
    481 	for (ap = parmp->parm_auth, i = 0;
    482 	     ap->type != RIP_AUTH_NONE; i++, ap++) {
    483 		if (i >= MAX_AUTH_KEYS)
    484 			return "too many passwords";
    485 	}
    486 
    487 	memset(&k, 0, sizeof(k));
    488 	k.type = type;
    489 	k.end = -1-DAY;
    490 
    491 	val0 = val;
    492 	if (0 > parse_quote(&val, "| ,\n\r", &delim,
    493 			    (char *)k.key, sizeof(k.key)))
    494 		return tgt;
    495 
    496 	if (delim != '|') {
    497 		if (type == RIP_AUTH_MD5)
    498 			return "missing Keyid";
    499 	} else {
    500 		val0 = ++val;
    501 		buf[sizeof(buf)-1] = '\0';
    502 		if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
    503 		    || buf[sizeof(buf)-1] != '\0'
    504 		    || (l = strtoul(buf,&p,0)) > 255
    505 		    || *p != '\0') {
    506 			sprintf(buf,"bad KeyID \"%.20s\"", val0);
    507 			return buf;
    508 		}
    509 		for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
    510 			if (ap2->keyid == l) {
    511 				sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
    512 				return buf;
    513 			}
    514 		}
    515 		k.keyid = (int)l;
    516 
    517 		if (delim == '|') {
    518 			val0 = ++val;
    519 			if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
    520 					       buf,sizeof(buf))))
    521 				return p;
    522 			if (delim != '|')
    523 				return "missing second timestamp";
    524 			val0 = ++val;
    525 			if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
    526 					       buf,sizeof(buf))))
    527 				return p;
    528 			if ((u_long)k.start > (u_long)k.end) {
    529 				sprintf(buf,"out of order timestamp %.30s",
    530 					val0);
    531 				return buf;
    532 			}
    533 		}
    534 	}
    535 	if (delim != '\0')
    536 		return tgt;
    537 
    538 	memmove(ap, &k, sizeof(*ap));
    539 	return 0;
    540 }
    541 
    542 
    543 /* Parse a set of parameters for an interface.
    544  */
    545 char *					/* 0 or error message */
    546 parse_parms(char *line,
    547 	    int safe)			/* 1=from secure file */
    548 {
    549 #define PARS(str) (!strcasecmp(tgt, str))
    550 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
    551 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break;	\
    552 	parm.parm_int_state |= (b);}
    553 	struct parm parm;
    554 	struct intnet *intnetp;
    555 	struct tgate *tg;
    556 	naddr addr, mask;
    557 	char delim, *val0 = NULL, *tgt, *val, *p;
    558 	char buf[64];
    559 
    560 
    561 	/* "subnet=x.y.z.u/mask,metric" must be alone on the line */
    562 	if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
    563 	    && *(val = &line[sizeof("subnet=")]) != '\0') {
    564 		intnetp = (struct intnet*)malloc(sizeof(*intnetp));
    565 		intnetp->intnet_metric = 1;
    566 		if ((p = strrchr(val,','))) {
    567 			*p++ = '\0';
    568 			intnetp->intnet_metric = (int)strtol(p,&p,0);
    569 			if (*p != '\0'
    570 			    || intnetp->intnet_metric <= 0
    571 			    || intnetp->intnet_metric >= HOPCNT_INFINITY)
    572 				return line;
    573 		}
    574 		if (!getnet(val, &intnetp->intnet_addr, &intnetp->intnet_mask)
    575 		    || intnetp->intnet_mask == HOST_MASK
    576 		    || intnetp->intnet_addr == RIP_DEFAULT) {
    577 			free(intnetp);
    578 			return line;
    579 		}
    580 		HTONL(intnetp->intnet_addr);
    581 		intnetp->intnet_next = intnets;
    582 		intnets = intnetp;
    583 		return 0;
    584 	}
    585 
    586 	memset(&parm, 0, sizeof(parm));
    587 
    588 	tgt = "null";
    589 	for (;;) {
    590 		tgt = line + strspn(line, " ,\n\r");
    591 		if (*tgt == '\0')
    592 			break;
    593 
    594 		line += strcspn(tgt, "= ,\n\r");
    595 		delim = *line;
    596 		if (delim == '=') {
    597 			val0 = ++line;
    598 			if (0 > parse_quote(&line," ,\n\r",&delim,
    599 					    buf,sizeof(buf)))
    600 				return tgt;
    601 		}
    602 		if (delim != '\0')
    603 			*line++ = '\0';
    604 
    605 		if (PARSEQ("if")) {
    606 			if (parm.parm_name[0] != '\0'
    607 			    || strlen(buf) > IFNAMSIZ)
    608 				return tgt;
    609 			strcpy(parm.parm_name, buf);
    610 
    611 		} else if (PARSEQ("addr")) {
    612 			/* This is a bad idea, because the address based
    613 			 * sets of parameters cannot be checked for
    614 			 * consistency with the interface name parameters.
    615 			 * The parm_net stuff is needed to allow several
    616 			 * -F settings.
    617 			 */
    618 			if (!getnet(val0, &addr, &mask)
    619 			    || parm.parm_name[0] != '\0')
    620 				return tgt;
    621 			parm.parm_net = addr;
    622 			parm.parm_mask = mask;
    623 			parm.parm_name[0] = '\n';
    624 
    625 		} else if (PARSEQ("passwd")) {
    626 			/* since cleartext passwords are so weak allow
    627 			 * them anywhere
    628 			 */
    629 			tgt = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
    630 			if (tgt) {
    631 				*val0 = '\0';
    632 				return tgt;
    633 			}
    634 
    635 		} else if (PARSEQ("md5_passwd")) {
    636 			tgt = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
    637 			if (tgt) {
    638 				*val0 = '\0';
    639 				return tgt;
    640 			}
    641 
    642 		} else if (PARS("no_ag")) {
    643 			parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
    644 
    645 		} else if (PARS("no_super_ag")) {
    646 			parm.parm_int_state |= IS_NO_SUPER_AG;
    647 
    648 		} else if (PARS("no_ripv1_in")) {
    649 			parm.parm_int_state |= IS_NO_RIPV1_IN;
    650 
    651 		} else if (PARS("no_ripv2_in")) {
    652 			parm.parm_int_state |= IS_NO_RIPV2_IN;
    653 
    654 		} else if (PARS("ripv2_out")) {
    655 			if (parm.parm_int_state & IS_NO_RIPV2_OUT)
    656 				return tgt;
    657 			parm.parm_int_state |= IS_NO_RIPV1_OUT;
    658 
    659 		} else if (PARS("ripv2")) {
    660 			if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
    661 			    || (parm.parm_int_state & IS_NO_RIPV2_IN))
    662 				return tgt;
    663 			parm.parm_int_state |= (IS_NO_RIPV1_IN
    664 						| IS_NO_RIPV1_OUT);
    665 
    666 		} else if (PARS("no_rip")) {
    667 			CKF(IS_PM_RDISC, IS_NO_RIP);
    668 
    669 		} else if (PARS("no_rdisc")) {
    670 			CKF((GROUP_IS_SOL|GROUP_IS_ADV), IS_NO_RDISC);
    671 
    672 		} else if (PARS("no_solicit")) {
    673 			CKF(GROUP_IS_SOL, IS_NO_SOL_OUT);
    674 
    675 		} else if (PARS("send_solicit")) {
    676 			CKF(GROUP_IS_SOL, IS_SOL_OUT);
    677 
    678 		} else if (PARS("no_rdisc_adv")) {
    679 			CKF(GROUP_IS_ADV, IS_NO_ADV_OUT);
    680 
    681 		} else if (PARS("rdisc_adv")) {
    682 			CKF(GROUP_IS_ADV, IS_ADV_OUT);
    683 
    684 		} else if (PARS("bcast_rdisc")) {
    685 			parm.parm_int_state |= IS_BCAST_RDISC;
    686 
    687 		} else if (PARS("passive")) {
    688 			CKF((GROUP_IS_SOL|GROUP_IS_ADV), IS_NO_RDISC);
    689 			parm.parm_int_state |= IS_NO_RIP;
    690 
    691 		} else if (PARSEQ("rdisc_pref")) {
    692 			if (parm.parm_rdisc_pref != 0
    693 			    || (parm.parm_rdisc_pref = (int)strtoul(buf, &p,0),
    694 				*p != '\0'))
    695 				return tgt;
    696 
    697 		} else if (PARS("pm_rdisc")) {
    698 			if (IS_RIP_OUT_OFF(parm.parm_int_state))
    699 				return tgt;
    700 			parm.parm_int_state |= IS_PM_RDISC;
    701 
    702 		} else if (PARSEQ("rdisc_interval")) {
    703 			if (parm.parm_rdisc_int != 0
    704 			    || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
    705 				*p != '\0')
    706 			    || parm.parm_rdisc_int < MinMaxAdvertiseInterval
    707 			    || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
    708 				return tgt;
    709 
    710 		} else if (PARSEQ("fake_default")) {
    711 			if (parm.parm_d_metric != 0
    712 			    || IS_RIP_OUT_OFF(parm.parm_int_state)
    713 			    || (parm.parm_d_metric = (int)strtoul(buf,&p,0),
    714 				*p != '\0')
    715 			    || parm.parm_d_metric > HOPCNT_INFINITY-1)
    716 				return tgt;
    717 
    718 		} else if (PARSEQ("trust_gateway")) {
    719 			if (!gethost(buf,&addr))
    720 				return tgt;
    721 			tg = (struct tgate *)malloc(sizeof(*tg));
    722 			tg->tgate_next = tgates;
    723 			tg->tgate_addr = addr;
    724 			tgates = tg;
    725 			parm.parm_int_state |= IS_DISTRUST;
    726 
    727 		} else if (PARS("redirect_ok")) {
    728 			parm.parm_int_state |= IS_REDIRECT_OK;
    729 
    730 		} else {
    731 			return tgt;	/* error */
    732 		}
    733 	}
    734 
    735 	return check_parms(&parm);
    736 #undef PARS
    737 #undef PARSEQ
    738 }
    739 
    740 
    741 /* check for duplicate parameter specifications */
    742 char *					/* 0 or error message */
    743 check_parms(struct parm *new)
    744 {
    745 	struct parm *parmp, **parmpp;
    746 	int i, num_passwds;
    747 
    748 	/* set implicit values
    749 	 */
    750 	if (new->parm_int_state & IS_NO_ADV_IN)
    751 		new->parm_int_state |= IS_NO_SOL_OUT;
    752 
    753 	for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
    754 		if (new->parm_auth[i].type != RIP_AUTH_NONE)
    755 			num_passwds++;
    756 	}
    757 
    758 	/* compare with existing sets of parameters
    759 	 */
    760 	for (parmpp = &parms;
    761 	     (parmp = *parmpp) != 0;
    762 	     parmpp = &parmp->parm_next) {
    763 		if (strcmp(new->parm_name, parmp->parm_name))
    764 			continue;
    765 		if (!on_net(htonl(parmp->parm_net),
    766 			    new->parm_net, new->parm_mask)
    767 		    && !on_net(htonl(new->parm_net),
    768 			       parmp->parm_net, parmp->parm_mask))
    769 			continue;
    770 
    771 		for (i = 0; i < MAX_AUTH_KEYS; i++) {
    772 			if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
    773 				num_passwds++;
    774 		}
    775 		if (num_passwds > MAX_AUTH_KEYS)
    776 			return "too many conflicting passwords";
    777 
    778 		if ((0 != (new->parm_int_state & GROUP_IS_SOL)
    779 		     && 0 != (parmp->parm_int_state & GROUP_IS_SOL)
    780 		     && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
    781 			      && GROUP_IS_SOL))
    782 		    || (0 != (new->parm_int_state & GROUP_IS_ADV)
    783 			&& 0 != (parmp->parm_int_state & GROUP_IS_ADV)
    784 			&& 0 != ((new->parm_int_state ^ parmp->parm_int_state)
    785 				 && GROUP_IS_ADV))
    786 		    || (new->parm_rdisc_pref != 0
    787 			&& parmp->parm_rdisc_pref != 0
    788 			&& new->parm_rdisc_pref != parmp->parm_rdisc_pref)
    789 		    || (new->parm_rdisc_int != 0
    790 			&& parmp->parm_rdisc_int != 0
    791 			&& new->parm_rdisc_int != parmp->parm_rdisc_int)) {
    792 			return ("conflicting, duplicate router discovery"
    793 				" parameters");
    794 
    795 		}
    796 
    797 		if (new->parm_d_metric != 0
    798 		     && parmp->parm_d_metric != 0
    799 		     && new->parm_d_metric != parmp->parm_d_metric) {
    800 			return ("conflicting, duplicate poor man's router"
    801 				" discovery or fake default metric");
    802 		}
    803 	}
    804 
    805 	/* link new entry on the so that when the entries are scanned,
    806 	 * they affect the result in the order the operator specified.
    807 	 */
    808 	parmp = (struct parm*)malloc(sizeof(*parmp));
    809 	memmove(parmp, new, sizeof(*parmp));
    810 	*parmpp = parmp;
    811 
    812 	return 0;
    813 }
    814 
    815 
    816 /* get a network number as a name or a number, with an optional "/xx"
    817  * netmask.
    818  */
    819 int					/* 0=bad */
    820 getnet(char *name,
    821        naddr *netp,			/* a network so host byte order */
    822        naddr *maskp)			/* masks are always in host order */
    823 {
    824 	int i;
    825 	struct netent *np;
    826 	naddr mask;			/* in host byte order */
    827 	struct in_addr in;		/* a network and so host byte order */
    828 	char hname[MAXHOSTNAMELEN+1];
    829 	char *mname, *p;
    830 
    831 
    832 	/* Detect and separate "1.2.3.4/24"
    833 	 */
    834 	if (0 != (mname = strrchr(name,'/'))) {
    835 		i = (int)(mname - name);
    836 		if (i > sizeof(hname)-1)	/* name too long */
    837 			return 0;
    838 		memmove(hname, name, i);
    839 		hname[i] = '\0';
    840 		mname++;
    841 		name = hname;
    842 	}
    843 
    844 	np = getnetbyname(name);
    845 	if (np != 0) {
    846 		in.s_addr = (naddr)np->n_net;
    847 	} else if (inet_aton(name, &in) == 1) {
    848 		NTOHL(in.s_addr);
    849 	} else if (!mname && !strcasecmp(name,"default")) {
    850 		in.s_addr = RIP_DEFAULT;
    851 	} else {
    852 		return 0;
    853 	}
    854 
    855 	if (!mname) {
    856 		/* we cannot use the interfaces here because we have not
    857 		 * looked at them yet.
    858 		 */
    859 		mask = std_mask(htonl(in.s_addr));
    860 		if ((~mask & in.s_addr) != 0)
    861 			mask = HOST_MASK;
    862 	} else {
    863 		mask = (naddr)strtoul(mname, &p, 0);
    864 		if (*p != '\0' || mask > 32)
    865 			return 0;
    866 		if (mask != 0)
    867 			mask = HOST_MASK << (32-mask);
    868 	}
    869 
    870 	/* must have mask of 0 with default */
    871 	if (mask != 0 && in.s_addr == RIP_DEFAULT)
    872 		return 0;
    873 	/* no host bits allowed in a network number */
    874 	if ((~mask & in.s_addr) != 0)
    875 		return 0;
    876 	/* require non-zero network number */
    877 	if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
    878 		return 0;
    879 	if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
    880 		return 0;
    881 	if (in.s_addr>>24 == 0xff)
    882 		return 0;
    883 
    884 	*netp = in.s_addr;
    885 	*maskp = mask;
    886 	return 1;
    887 }
    888 
    889 
    890 int					/* 0=bad */
    891 gethost(char *name,
    892 	naddr *addrp)
    893 {
    894 	struct hostent *hp;
    895 	struct in_addr in;
    896 
    897 
    898 	/* Try for a number first, even in IRIX where gethostbyname()
    899 	 * is smart.  This avoids hitting the name server which
    900 	 * might be sick because routing is.
    901 	 */
    902 	if (inet_aton(name, &in) == 1) {
    903 		/* get a good number, but check that it it makes some
    904 		 * sense.
    905 		 */
    906 		if (ntohl(in.s_addr)>>24 == 0
    907 		    || ntohl(in.s_addr)>>24 == 0xff)
    908 			return 0;
    909 		*addrp = in.s_addr;
    910 		return 1;
    911 	}
    912 
    913 	hp = gethostbyname(name);
    914 	if (hp) {
    915 		memmove(addrp, hp->h_addr, sizeof(*addrp));
    916 		return 1;
    917 	}
    918 
    919 	return 0;
    920 }
    921