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