parms.c revision 50476
10SN/A/*
22362SN/A * Copyright (c) 1983, 1993
30SN/A *	The Regents of the University of California.  All rights reserved.
40SN/A *
50SN/A * Redistribution and use in source and binary forms, with or without
60SN/A * modification, are permitted provided that the following conditions
72362SN/A * are met:
80SN/A * 1. Redistributions of source code must retain the above copyright
92362SN/A *    notice, this list of conditions and the following disclaimer.
100SN/A * 2. Redistributions in binary form must reproduce the above copyright
110SN/A *    notice, this list of conditions and the following disclaimer in the
120SN/A *    documentation and/or other materials provided with the distribution.
130SN/A * 3. All advertising materials mentioning features or use of this software
140SN/A *    must display the following acknowledgment:
150SN/A *	This product includes software developed by the University of
160SN/A *	California, Berkeley and its contributors.
170SN/A * 4. Neither the name of the University nor the names of its contributors
180SN/A *    may be used to endorse or promote products derived from this software
190SN/A *    without specific prior written permission.
200SN/A *
212362SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
222362SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
232362SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310SN/A * SUCH DAMAGE.
320SN/A *
330SN/A * $FreeBSD: head/sbin/routed/parms.c 50476 1999-08-28 00:22:10Z peter $
340SN/A */
350SN/A
360SN/A#include "defs.h"
370SN/A#include "pathnames.h"
380SN/A#include <sys/stat.h>
390SN/A
400SN/A#if !defined(sgi) && !defined(__NetBSD__)
410SN/Astatic char sccsid[] __attribute__((unused)) = "@(#)if.c	8.1 (Berkeley) 6/5/93";
420SN/A#elif defined(__NetBSD__)
430SN/A__RCSID("$NetBSD$");
440SN/A#endif
450SN/A#ident "$Revision: 1.5 $"
460SN/A
470SN/A
480SN/Astruct parm *parms;
490SN/Astruct intnet *intnets;
500SN/Astruct r1net *r1nets;
510SN/Astruct tgate *tgates;
520SN/A
530SN/A
540SN/A/* use configured parameters
550SN/A */
560SN/Avoid
570SN/Aget_parms(struct interface *ifp)
580SN/A{
590SN/A	static int warned_auth_in, warned_auth_out;
600SN/A	struct parm *parmp;
610SN/A	int i, num_passwds = 0;
620SN/A
630SN/A	/* get all relevant parameters
640SN/A	 */
650SN/A	for (parmp = parms; parmp != 0; parmp = parmp->parm_next) {
660SN/A		if (parmp->parm_name[0] == '\0'
670SN/A		    || !strcmp(ifp->int_name, parmp->parm_name)
680SN/A		    || (parmp->parm_name[0] == '\n'
690SN/A			&& on_net(ifp->int_addr,
700SN/A				  parmp->parm_net, parmp->parm_mask))) {
710SN/A
720SN/A			/* This group of parameters is relevant,
730SN/A			 * so get its settings
740SN/A			 */
750SN/A			ifp->int_state |= parmp->parm_int_state;
760SN/A			for (i = 0; i < MAX_AUTH_KEYS; i++) {
770SN/A				if (parmp->parm_auth[0].type == RIP_AUTH_NONE
780SN/A				    || num_passwds >= MAX_AUTH_KEYS)
790SN/A					break;
800SN/A				memcpy(&ifp->int_auth[num_passwds++],
810SN/A				       &parmp->parm_auth[i],
820SN/A				       sizeof(ifp->int_auth[0]));
830SN/A			}
840SN/A			if (parmp->parm_rdisc_pref != 0)
850SN/A				ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
860SN/A			if (parmp->parm_rdisc_int != 0)
870SN/A				ifp->int_rdisc_int = parmp->parm_rdisc_int;
880SN/A			if (parmp->parm_d_metric != 0)
890SN/A				ifp->int_d_metric = parmp->parm_d_metric;
900SN/A		}
910SN/A	}
920SN/A
930SN/A	/* Set general defaults.
940SN/A	 *
950SN/A	 * Default poor-man's router discovery to a metric that will
960SN/A	 * be heard by old versions of `routed`.  They ignored received
970SN/A	 * routes with metric 15.
980SN/A	 */
990SN/A	if ((ifp->int_state & IS_PM_RDISC)
1000SN/A	    && ifp->int_d_metric == 0)
1010SN/A		ifp->int_d_metric = FAKE_METRIC;
1020SN/A
1030SN/A	if (ifp->int_rdisc_int == 0)
1040SN/A		ifp->int_rdisc_int = DefMaxAdvertiseInterval;
1050SN/A
1060SN/A	if (!(ifp->int_if_flags & IFF_MULTICAST)
1070SN/A	    && !(ifp->int_state & IS_REMOTE))
1080SN/A		ifp->int_state |= IS_BCAST_RDISC;
1090SN/A
1100SN/A	if (ifp->int_if_flags & IFF_POINTOPOINT) {
1110SN/A		ifp->int_state |= IS_BCAST_RDISC;
1120SN/A		/* By default, point-to-point links should be passive
1130SN/A		 * about router-discovery for the sake of demand-dialing.
1140SN/A		 */
1150SN/A		if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
1160SN/A			ifp->int_state |= IS_NO_SOL_OUT;
1170SN/A		if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
1180SN/A			ifp->int_state |= IS_NO_ADV_OUT;
1190SN/A	}
1200SN/A
1210SN/A	if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
1220SN/A		ifp->int_state |= IS_NO_RDISC;
1230SN/A	if (ifp->int_state & IS_PASSIVE)
1240SN/A		ifp->int_state |= IS_NO_RIP;
1250SN/A
1260SN/A	if (!IS_RIP_IN_OFF(ifp->int_state)
1270SN/A	    && ifp->int_auth[0].type != RIP_AUTH_NONE
1280SN/A	    && !(ifp->int_state & IS_NO_RIPV1_IN)
1290SN/A	    && !warned_auth_in) {
1300SN/A		msglog("Warning: RIPv1 input via %s"
1310SN/A		       " will be accepted without authentication",
1320SN/A		       ifp->int_name);
1330SN/A		warned_auth_in = 1;
1340SN/A	}
1350SN/A	if (!IS_RIP_OUT_OFF(ifp->int_state)
1360SN/A	    && ifp->int_auth[0].type != RIP_AUTH_NONE
1370SN/A	    && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
1380SN/A		if (!warned_auth_out) {
1390SN/A			msglog("Warning: RIPv1 output via %s"
1400SN/A			       " will be sent without authentication",
1410SN/A			       ifp->int_name);
1420SN/A			warned_auth_out = 1;
1430SN/A		}
1440SN/A	}
1450SN/A}
1460SN/A
1470SN/A
1480SN/A/* Read a list of gateways from /etc/gateways and add them to our tables.
1490SN/A *
1500SN/A * This file contains a list of "remote" gateways.  That is usually
1510SN/A * a gateway which we cannot immediately determine if it is present or
1520SN/A * not as we can do for those provided by directly connected hardware.
1530SN/A *
1540SN/A * If a gateway is marked "passive" in the file, then we assume it
1550SN/A * does not understand RIP and assume it is always present.  Those
1560SN/A * not marked passive are treated as if they were directly connected
1570SN/A * and assumed to be broken if they do not send us advertisements.
1580SN/A * All remote interfaces are added to our list, and those not marked
1590SN/A * passive are sent routing updates.
1600SN/A *
1610SN/A * A passive interface can also be local, hardware interface exempt
1620SN/A * from RIP.
1630SN/A */
1640SN/Avoid
1650SN/Agwkludge(void)
1660SN/A{
1670SN/A	FILE *fp;
1680SN/A	char *p, *lptr;
1690SN/A	const char *cp;
1700SN/A	char lbuf[200], net_host[5], dname[64+1+64+1];
1710SN/A	char gname[GNAME_LEN+1], qual[9];
1720SN/A	struct interface *ifp;
1730SN/A	naddr dst, netmask, gate;
1740SN/A	int metric, n, lnum;
1750SN/A	struct stat sb;
1760SN/A	u_int state;
1770SN/A	const char *type;
1780SN/A
1790SN/A
1800SN/A	fp = fopen(_PATH_GATEWAYS, "r");
1810SN/A	if (fp == 0)
1820SN/A		return;
1830SN/A
1840SN/A	if (0 > fstat(fileno(fp), &sb)) {
1850SN/A		msglog("could not stat() "_PATH_GATEWAYS);
1860SN/A		(void)fclose(fp);
1870SN/A		return;
1880SN/A	}
1890SN/A
1900SN/A	for (lnum = 1; ; lnum++) {
1910SN/A		if (0 == fgets(lbuf, sizeof(lbuf), fp))
1920SN/A			break;
1930SN/A		lptr = lbuf;
1940SN/A		while (*lptr == ' ')
1950SN/A			lptr++;
1960SN/A		p = lptr+strlen(lptr)-1;
1970SN/A		while (*p == '\n'
1980SN/A		       || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
1990SN/A			*p-- = '\0';
2000SN/A		if (*lptr == '\0'	/* ignore null and comment lines */
2010SN/A		    || *lptr == '#')
2020SN/A			continue;
2030SN/A
2040SN/A		/* notice newfangled parameter lines
2050SN/A		 */
2060SN/A		if (strncasecmp("net", lptr, 3)
2070SN/A		    && strncasecmp("host", lptr, 4)) {
2080SN/A			cp = parse_parms(lptr,
2090SN/A					 (sb.st_uid == 0
2100SN/A					  && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
2110SN/A			if (cp != 0)
2120SN/A				msglog("%s in line %d of "_PATH_GATEWAYS,
2130SN/A				       cp, lnum);
2140SN/A			continue;
2150SN/A		}
2160SN/A
2170SN/A/*  {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
2180SN/A		qual[0] = '\0';
2190SN/A		/* the '64' here must be GNAME_LEN */
2200SN/A		n = sscanf(lptr, "%4s %129[^ \t] gateway"
2210SN/A			   " %64[^ / \t] metric %u %8s\n",
2220SN/A			   net_host, dname, gname, &metric, qual);
2230SN/A		if (n != 4 && n != 5) {
2240SN/A			msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
2250SN/A			       lptr, n);
2260SN/A			continue;
2270SN/A		}
2280SN/A		if (metric >= HOPCNT_INFINITY) {
2290SN/A			msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
2300SN/A			       lptr);
2310SN/A			continue;
2320SN/A		}
2330SN/A		if (!strcasecmp(net_host, "host")) {
2340SN/A			if (!gethost(dname, &dst)) {
2350SN/A				msglog("bad host \"%s\" in "_PATH_GATEWAYS
2360SN/A				       " entry \"%s\"", dname, lptr);
2370SN/A				continue;
2380SN/A			}
2390SN/A			netmask = HOST_MASK;
2400SN/A		} else if (!strcasecmp(net_host, "net")) {
2410SN/A			if (!getnet(dname, &dst, &netmask)) {
2420SN/A				msglog("bad net \"%s\" in "_PATH_GATEWAYS
2430SN/A				       " entry \"%s\"", dname, lptr);
2440SN/A				continue;
2450SN/A			}
2460SN/A			if (dst == RIP_DEFAULT) {
2470SN/A				msglog("bad net \"%s\" in "_PATH_GATEWAYS
2480SN/A				       " entry \"%s\"--cannot be default",
2490SN/A				       dname, lptr);
2500SN/A				continue;
2510SN/A			}
2520SN/A			HTONL(dst);	/* make network # into IP address */
2530SN/A		} else {
2540SN/A			msglog("bad \"%s\" in "_PATH_GATEWAYS
2550SN/A			       " entry \"%s\"", net_host, lptr);
2560SN/A			continue;
2570SN/A		}
2580SN/A
2590SN/A		if (!gethost(gname, &gate)) {
2600SN/A			msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
2610SN/A			       " entry \"%s\"", gname, lptr);
2620SN/A			continue;
2630SN/A		}
2640SN/A
2650SN/A		if (!strcasecmp(qual, type = "passive")) {
2660SN/A			/* Passive entries are not placed in our tables,
2670SN/A			 * only the kernel's, so we don't copy all of the
2680SN/A			 * external routing information within a net.
2690SN/A			 * Internal machines should use the default
2700SN/A			 * route to a suitable gateway (like us).
2710SN/A			 */
2720SN/A			state = IS_REMOTE | IS_PASSIVE;
2730SN/A			if (metric == 0)
2740SN/A				metric = 1;
2750SN/A
2760SN/A		} else if (!strcasecmp(qual, type = "external")) {
2770SN/A			/* External entries are handled by other means
2780SN/A			 * such as EGP, and are placed only in the daemon
2790SN/A			 * tables to prevent overriding them with something
2800SN/A			 * else.
2810SN/A			 */
2820SN/A			strcpy(qual,"external");
2830SN/A			state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
2840SN/A			if (metric == 0)
2850SN/A				metric = 1;
2860SN/A
2870SN/A		} else if (!strcasecmp(qual, "active")
2880SN/A			   || qual[0] == '\0') {
2890SN/A			if (metric != 0) {
2900SN/A				/* Entries that are neither "passive" nor
2910SN/A				 * "external" are "remote" and must behave
2920SN/A				 * like physical interfaces.  If they are not
2930SN/A				 * heard from regularly, they are deleted.
2940SN/A				 */
2950SN/A				state = IS_REMOTE;
2960SN/A				type = "remote";
2970SN/A			} else {
2980SN/A				/* "remote" entries with a metric of 0
2990SN/A				 * are aliases for our own interfaces
3000SN/A				 */
3010SN/A				state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
3020SN/A				type = "alias";
3030SN/A			}
3040SN/A
3050SN/A		} else {
3060SN/A			msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
3070SN/A			       " unknown type %s", lptr, qual);
3080SN/A			continue;
3090SN/A		}
3100SN/A
3110SN/A		if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
3120SN/A			state |= IS_NO_RDISC;
3130SN/A		if (state & IS_PASSIVE)
3140SN/A			state |= IS_NO_RIP;
3150SN/A
3160SN/A		ifp = check_dup(gate,dst,netmask,0);
3170SN/A		if (ifp != 0) {
3180SN/A			msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
3190SN/A			continue;
3200SN/A		}
3210SN/A
3220SN/A		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
3230SN/A		memset(ifp, 0, sizeof(*ifp));
3240SN/A
3250SN/A		ifp->int_state = state;
3260SN/A		if (netmask == HOST_MASK)
3270SN/A			ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
3280SN/A		else
3290SN/A			ifp->int_if_flags = IFF_UP;
3300SN/A		ifp->int_act_time = NEVER;
3310SN/A		ifp->int_addr = gate;
3320SN/A		ifp->int_dstaddr = dst;
3330SN/A		ifp->int_mask = netmask;
3340SN/A		ifp->int_ripv1_mask = netmask;
3350SN/A		ifp->int_std_mask = std_mask(gate);
3360SN/A		ifp->int_net = ntohl(dst);
3370SN/A		ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
3380SN/A		ifp->int_std_addr = htonl(ifp->int_std_net);
3390SN/A		ifp->int_metric = metric;
3400SN/A		if (!(state & IS_EXTERNAL)
3410SN/A		    && ifp->int_mask != ifp->int_std_mask)
3420SN/A			ifp->int_state |= IS_SUBNET;
3430SN/A		(void)sprintf(ifp->int_name, "%s(%s)", type, gname);
3440SN/A		ifp->int_index = -1;
3450SN/A
3460SN/A		if_link(ifp);
3470SN/A	}
3480SN/A
3490SN/A	/* After all of the parameter lines have been read,
3500SN/A	 * apply them to any remote interfaces.
3510SN/A	 */
3520SN/A	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
3530SN/A		get_parms(ifp);
3540SN/A
3550SN/A		tot_interfaces++;
3560SN/A		if (!IS_RIP_OFF(ifp->int_state))
3570SN/A			rip_interfaces++;
3580SN/A
3590SN/A		trace_if("Add", ifp);
3600SN/A	}
3610SN/A
3620SN/A	(void)fclose(fp);
3630SN/A}
3640SN/A
3650SN/A
3660SN/A/* like strtok(), but honoring backslash and not changing the source string
3670SN/A */
3680SN/Astatic int				/* 0=ok, -1=bad */
3690SN/Aparse_quote(char **linep,		/* look here */
3700SN/A	    const char *delims,		/* for these delimiters */
3710SN/A	    char *delimp,		/* 0 or put found delimiter here */
3720SN/A	    char *buf,			/* copy token to here */
3730SN/A	    int	lim)			/* at most this many bytes */
3740SN/A{
3750SN/A	char c = '\0', *pc;
3760SN/A	const char *p;
3770SN/A
3780SN/A
3790SN/A	pc = *linep;
3800SN/A	if (*pc == '\0')
3810SN/A		return -1;
3820SN/A
3830SN/A	while (lim != 0) {
3840SN/A		c = *pc++;
3850SN/A		if (c == '\0')
3860SN/A			break;
3870SN/A
3880SN/A		if (c == '\\' && *pc != '\0') {
3890SN/A			if ((c = *pc++) == 'n') {
3900SN/A				c = '\n';
3910SN/A			} else if (c == 'r') {
3920SN/A				c = '\r';
3930SN/A			} else if (c == 't') {
3940SN/A				c = '\t';
3950SN/A			} else if (c == 'b') {
3960SN/A				c = '\b';
3970SN/A			} else if (c >= '0' && c <= '7') {
3980SN/A				c -= '0';
3990SN/A				if (*pc >= '0' && *pc <= '7') {
4000SN/A					c = (c<<3)+(*pc++ - '0');
4010SN/A					if (*pc >= '0' && *pc <= '7')
4020SN/A					    c = (c<<3)+(*pc++ - '0');
4030SN/A				}
4040SN/A			}
4050SN/A
4060SN/A		} else {
4070SN/A			for (p = delims; *p != '\0'; ++p) {
4080SN/A				if (*p == c)
4090SN/A					goto exit;
4100SN/A			}
4110SN/A		}
4120SN/A
4130SN/A		*buf++ = c;
4140SN/A		--lim;
4150SN/A	}
4160SN/Aexit:
4170SN/A	if (lim == 0)
4180SN/A		return -1;
4190SN/A
4200SN/A	*buf = '\0';			/* terminate copy of token */
4210SN/A	if (delimp != 0)
4220SN/A		*delimp = c;		/* return delimiter */
4230SN/A	*linep = pc-1;			/* say where we ended */
4240SN/A	return 0;
4250SN/A}
4260SN/A
4270SN/A
4280SN/A/* Parse password timestamp
4290SN/A */
4300SN/Astatic char *
4310SN/Aparse_ts(time_t *tp,
4320SN/A	 char **valp,
4330SN/A	 char *val0,
4340SN/A	 char *delimp,
4350SN/A	 char *buf,
4360SN/A	 u_int bufsize)
4370SN/A{
4380SN/A	struct tm tm;
4390SN/A#if defined(sgi) || defined(__NetBSD__)
4400SN/A	char *ptr;
4410SN/A#endif
4420SN/A
4430SN/A	if (0 > parse_quote(valp, "| ,\n\r", delimp,
4440SN/A			    buf,bufsize)
4450SN/A	    || buf[bufsize-1] != '\0'
4460SN/A	    || buf[bufsize-2] != '\0') {
4470SN/A		sprintf(buf,"bad timestamp %.25s", val0);
4480SN/A		return buf;
4490SN/A	}
4500SN/A	strcat(buf,"\n");
4510SN/A	memset(&tm, 0, sizeof(tm));
4520SN/A#if defined(sgi) || defined(__NetBSD__)
4530SN/A	ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
4540SN/A	if (ptr == NULL || *ptr != '\0') {
4550SN/A		sprintf(buf,"bad timestamp %.25s", val0);
4560SN/A		return buf;
4570SN/A	}
4580SN/A#else
4590SN/A	if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
4600SN/A			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
4610SN/A			&tm.tm_hour, &tm.tm_min)
4620SN/A	    || tm.tm_mon < 1 || tm.tm_mon > 12
4630SN/A	    || tm.tm_mday < 1 || tm.tm_mday > 31) {
4640SN/A		sprintf(buf,"bad timestamp %.25s", val0);
4650SN/A		return buf;
4660SN/A	}
4670SN/A	tm.tm_mon--;
4680SN/A	if (tm.tm_year <= 37)		/* assume small years are in the */
4690SN/A		tm.tm_year += 100;	/* 3rd millenium */
4700SN/A#endif
4710SN/A
4720SN/A	if ((*tp = mktime(&tm)) == -1) {
4730SN/A		sprintf(buf,"bad timestamp %.25s", val0);
4740SN/A		return buf;
4750SN/A	}
4760SN/A
4770SN/A	return 0;
4780SN/A}
4790SN/A
4800SN/A
4810SN/A/* Get a password, key ID, and expiration date in the format
4820SN/A *	passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
4830SN/A */
4840SN/Astatic const char *			/* 0 or error message */
4850SN/Aget_passwd(char *tgt,
4860SN/A	   char *val,
4870SN/A	   struct parm *parmp,
4880SN/A	   u_int16_t type,
4890SN/A	   int safe)			/* 1=from secure file */
4900SN/A{
4910SN/A	static char buf[80];
4920SN/A	char *val0, *p, delim;
4930SN/A	struct auth k, *ap, *ap2;
4940SN/A	int i;
4950SN/A	u_long l;
4960SN/A
4970SN/A
4980SN/A	if (!safe)
4990SN/A		return "ignore unsafe password";
5000SN/A
5010SN/A	for (ap = parmp->parm_auth, i = 0;
5020SN/A	     ap->type != RIP_AUTH_NONE; i++, ap++) {
5030SN/A		if (i >= MAX_AUTH_KEYS)
5040SN/A			return "too many passwords";
5050SN/A	}
5060SN/A
5070SN/A	memset(&k, 0, sizeof(k));
5080SN/A	k.type = type;
5090SN/A	k.end = -1-DAY;
5100SN/A
5110SN/A	val0 = val;
5120SN/A	if (0 > parse_quote(&val, "| ,\n\r", &delim,
5130SN/A			    (char *)k.key, sizeof(k.key)))
5140SN/A		return tgt;
5150SN/A
5160SN/A	if (delim != '|') {
5170SN/A		if (type == RIP_AUTH_MD5)
5180SN/A			return "missing Keyid";
5190SN/A	} else {
5200SN/A		val0 = ++val;
5210SN/A		buf[sizeof(buf)-1] = '\0';
5220SN/A		if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
5230SN/A		    || buf[sizeof(buf)-1] != '\0'
5240SN/A		    || (l = strtoul(buf,&p,0)) > 255
5250SN/A		    || *p != '\0') {
5260SN/A			sprintf(buf,"bad KeyID \"%.20s\"", val0);
5270SN/A			return buf;
5280SN/A		}
5290SN/A		for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
5300SN/A			if (ap2->keyid == l) {
5310SN/A				sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
5320SN/A				return buf;
5330SN/A			}
5340SN/A		}
5350SN/A		k.keyid = (int)l;
5360SN/A
5370SN/A		if (delim == '|') {
5380SN/A			val0 = ++val;
5390SN/A			if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
5400SN/A					       buf,sizeof(buf))))
5410SN/A				return p;
5420SN/A			if (delim != '|')
5430SN/A				return "missing second timestamp";
5440SN/A			val0 = ++val;
5450SN/A			if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
5460SN/A					       buf,sizeof(buf))))
5470SN/A				return p;
5480SN/A			if ((u_long)k.start > (u_long)k.end) {
5490SN/A				sprintf(buf,"out of order timestamp %.30s",
5500SN/A					val0);
5510SN/A				return buf;
5520SN/A			}
5530SN/A		}
5540SN/A	}
5550SN/A	if (delim != '\0')
5560SN/A		return tgt;
5570SN/A
5580SN/A	memmove(ap, &k, sizeof(*ap));
5590SN/A	return 0;
5600SN/A}
5610SN/A
5620SN/A
5630SN/Astatic const char *
5640SN/Abad_str(const char *estr)
5650SN/A{
5660SN/A	static char buf[100+8];
5670SN/A
5680SN/A	sprintf(buf, "bad \"%.100s\"", estr);
5690SN/A	return buf;
5700SN/A}
5710SN/A
5720SN/A
5730SN/A/* Parse a set of parameters for an interface.
5740SN/A */
5750SN/Aconst char *					/* 0 or error message */
5760SN/Aparse_parms(char *line,
5770SN/A	    int safe)			/* 1=from secure file */
5780SN/A{
5790SN/A#define PARS(str) (!strcasecmp(tgt, str))
5800SN/A#define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
5810SN/A#define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break;	\
5820SN/A	parm.parm_int_state |= (b);}
5830SN/A	struct parm parm;
5840SN/A	struct intnet *intnetp;
5850SN/A	struct r1net *r1netp;
5860SN/A	struct tgate *tg;
5870SN/A	naddr addr, mask;
5880SN/A	char delim, *val0 = 0, *tgt, *val, *p;
5890SN/A	const char *msg;
5900SN/A	char buf[BUFSIZ], buf2[BUFSIZ];
5910SN/A	int i;
5920SN/A
5930SN/A
594	/* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
595	if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
596	    && *(val = &line[sizeof("subnet=")-1]) != '\0') {
597		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
598			return bad_str(line);
599		intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
600						   "parse_parms subnet");
601		intnetp->intnet_metric = 1;
602		if (delim == ',') {
603			intnetp->intnet_metric = (int)strtol(val+1,&p,0);
604			if (*p != '\0'
605			    || intnetp->intnet_metric <= 0
606			    || intnetp->intnet_metric >= HOPCNT_INFINITY)
607				return bad_str(line);
608		}
609		if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
610		    || intnetp->intnet_mask == HOST_MASK
611		    || intnetp->intnet_addr == RIP_DEFAULT) {
612			free(intnetp);
613			return bad_str(line);
614		}
615		HTONL(intnetp->intnet_addr);
616		intnetp->intnet_next = intnets;
617		intnets = intnetp;
618		return 0;
619	}
620
621	/* "ripv1_mask=x.y.z.u/mask1,mask2" must be alone on the line.
622	 * This requires that x.y.z.u/mask1 be considered a subnet of
623	 * x.y.z.u/mask2, as if x.y.z.u/mask2 were a class-full network.
624	 */
625	if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
626	    && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
627		if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
628		    || delim == '\0')
629			return bad_str(line);
630		if ((i = (int)strtol(val+1, &p, 0)) <= 0
631		    || i > 32 || *p != '\0')
632			return bad_str(line);
633		r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
634						  "parse_parms ripv1_mask");
635		r1netp->r1net_mask = HOST_MASK << (32-i);
636		if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
637		    || r1netp->r1net_net == RIP_DEFAULT
638		    || r1netp->r1net_mask > r1netp->r1net_match) {
639			free(r1netp);
640			return bad_str(line);
641		}
642		r1netp->r1net_next = r1nets;
643		r1nets = r1netp;
644		return 0;
645	}
646
647	memset(&parm, 0, sizeof(parm));
648
649	for (;;) {
650		tgt = line + strspn(line, " ,\n\r");
651		if (*tgt == '\0' || *tgt == '#')
652			break;
653		line = tgt+strcspn(tgt, "= #,\n\r");
654		delim = *line;
655		if (delim == '=') {
656			val0 = ++line;
657			if (0 > parse_quote(&line, " #,\n\r",&delim,
658					    buf,sizeof(buf)))
659				return bad_str(tgt);
660		}
661		if (delim != '\0') {
662			for (;;) {
663				*line = '\0';
664				if (delim == '#')
665					break;
666				++line;
667				if (delim != ' '
668				    || (delim = *line) != ' ')
669					break;
670			}
671		}
672
673		if (PARSEQ("if")) {
674			if (parm.parm_name[0] != '\0'
675			    || strlen(buf) > IF_NAME_LEN)
676				return bad_str(tgt);
677			strcpy(parm.parm_name, buf);
678
679		} else if (PARSEQ("addr")) {
680			/* This is a bad idea, because the address based
681			 * sets of parameters cannot be checked for
682			 * consistency with the interface name parameters.
683			 * The parm_net stuff is needed to allow several
684			 * -F settings.
685			 */
686			if (!getnet(val0, &addr, &mask)
687			    || parm.parm_name[0] != '\0')
688				return bad_str(tgt);
689			parm.parm_net = addr;
690			parm.parm_mask = mask;
691			parm.parm_name[0] = '\n';
692
693		} else if (PARSEQ("passwd")) {
694			/* since cleartext passwords are so weak allow
695			 * them anywhere
696			 */
697			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
698			if (msg) {
699				*val0 = '\0';
700				return bad_str(msg);
701			}
702
703		} else if (PARSEQ("md5_passwd")) {
704			msg = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
705			if (msg) {
706				*val0 = '\0';
707				return bad_str(msg);
708			}
709
710		} else if (PARS("no_ag")) {
711			parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
712
713		} else if (PARS("no_super_ag")) {
714			parm.parm_int_state |= IS_NO_SUPER_AG;
715
716		} else if (PARS("no_ripv1_in")) {
717			parm.parm_int_state |= IS_NO_RIPV1_IN;
718
719		} else if (PARS("no_ripv2_in")) {
720			parm.parm_int_state |= IS_NO_RIPV2_IN;
721
722		} else if (PARS("ripv2_out")) {
723			if (parm.parm_int_state & IS_NO_RIPV2_OUT)
724				return bad_str(tgt);
725			parm.parm_int_state |= IS_NO_RIPV1_OUT;
726
727		} else if (PARS("ripv2")) {
728			if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
729			    || (parm.parm_int_state & IS_NO_RIPV2_IN))
730				return bad_str(tgt);
731			parm.parm_int_state |= (IS_NO_RIPV1_IN
732						| IS_NO_RIPV1_OUT);
733
734		} else if (PARS("no_rip")) {
735			CKF(IS_PM_RDISC, IS_NO_RIP);
736
737		} else if (PARS("no_rip_mcast")) {
738			parm.parm_int_state |= IS_NO_RIP_MCAST;
739
740		} else if (PARS("no_rdisc")) {
741			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
742
743		} else if (PARS("no_solicit")) {
744			CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
745
746		} else if (PARS("send_solicit")) {
747			CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
748
749		} else if (PARS("no_rdisc_adv")) {
750			CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
751
752		} else if (PARS("rdisc_adv")) {
753			CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
754
755		} else if (PARS("bcast_rdisc")) {
756			parm.parm_int_state |= IS_BCAST_RDISC;
757
758		} else if (PARS("passive")) {
759			CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
760			parm.parm_int_state |= IS_NO_RIP;
761
762		} else if (PARSEQ("rdisc_pref")) {
763			if (parm.parm_rdisc_pref != 0
764			    || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
765				*p != '\0'))
766				return bad_str(tgt);
767
768		} else if (PARS("pm_rdisc")) {
769			if (IS_RIP_OUT_OFF(parm.parm_int_state))
770				return bad_str(tgt);
771			parm.parm_int_state |= IS_PM_RDISC;
772
773		} else if (PARSEQ("rdisc_interval")) {
774			if (parm.parm_rdisc_int != 0
775			    || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
776				*p != '\0')
777			    || parm.parm_rdisc_int < MinMaxAdvertiseInterval
778			    || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
779				return bad_str(tgt);
780
781		} else if (PARSEQ("fake_default")) {
782			if (parm.parm_d_metric != 0
783			    || IS_RIP_OUT_OFF(parm.parm_int_state)
784			    || (parm.parm_d_metric = (int)strtoul(buf,&p,0),
785				*p != '\0')
786			    || parm.parm_d_metric > HOPCNT_INFINITY-1)
787				return bad_str(tgt);
788
789		} else if (PARSEQ("trust_gateway")) {
790			/* look for trust_gateway=x.y.z|net/mask|...) */
791			p = buf;
792			if (0 > parse_quote(&p, "|", &delim,
793					    buf2, sizeof(buf2))
794			    || !gethost(buf2,&addr))
795				return bad_str(tgt);
796			tg = (struct tgate *)rtmalloc(sizeof(*tg),
797						      "parse_parms"
798						      "trust_gateway");
799			memset(tg, 0, sizeof(*tg));
800			tg->tgate_addr = addr;
801			i = 0;
802			/* The default is to trust all routes. */
803			while (delim == '|') {
804				p++;
805				if (i >= MAX_TGATE_NETS
806				    || 0 > parse_quote(&p, "|", &delim,
807						       buf2, sizeof(buf2))
808				    || !getnet(buf2, &tg->tgate_nets[i].net,
809					       &tg->tgate_nets[i].mask)
810				    || tg->tgate_nets[i].net == RIP_DEFAULT
811				    || tg->tgate_nets[i].mask == 0)
812					return bad_str(tgt);
813				i++;
814			}
815			tg->tgate_next = tgates;
816			tgates = tg;
817			parm.parm_int_state |= IS_DISTRUST;
818
819		} else if (PARS("redirect_ok")) {
820			parm.parm_int_state |= IS_REDIRECT_OK;
821
822		} else {
823			return bad_str(tgt);	/* error */
824		}
825	}
826
827	return check_parms(&parm);
828#undef PARS
829#undef PARSEQ
830}
831
832
833/* check for duplicate parameter specifications */
834const char *				/* 0 or error message */
835check_parms(struct parm *new)
836{
837	struct parm *parmp, **parmpp;
838	int i, num_passwds;
839
840	/* set implicit values
841	 */
842	if (new->parm_int_state & IS_NO_ADV_IN)
843		new->parm_int_state |= IS_NO_SOL_OUT;
844	if (new->parm_int_state & IS_NO_SOL_OUT)
845		new->parm_int_state |= IS_NO_ADV_IN;
846
847	for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
848		if (new->parm_auth[i].type != RIP_AUTH_NONE)
849			num_passwds++;
850	}
851
852	/* compare with existing sets of parameters
853	 */
854	for (parmpp = &parms;
855	     (parmp = *parmpp) != 0;
856	     parmpp = &parmp->parm_next) {
857		if (strcmp(new->parm_name, parmp->parm_name))
858			continue;
859		if (!on_net(htonl(parmp->parm_net),
860			    new->parm_net, new->parm_mask)
861		    && !on_net(htonl(new->parm_net),
862			       parmp->parm_net, parmp->parm_mask))
863			continue;
864
865		for (i = 0; i < MAX_AUTH_KEYS; i++) {
866			if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
867				num_passwds++;
868		}
869		if (num_passwds > MAX_AUTH_KEYS)
870			return "too many conflicting passwords";
871
872		if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
873		     && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
874		     && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
875			      && GROUP_IS_SOL_OUT))
876		    || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
877			&& 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
878			&& 0 != ((new->parm_int_state ^ parmp->parm_int_state)
879				 && GROUP_IS_ADV_OUT))
880		    || (new->parm_rdisc_pref != 0
881			&& parmp->parm_rdisc_pref != 0
882			&& new->parm_rdisc_pref != parmp->parm_rdisc_pref)
883		    || (new->parm_rdisc_int != 0
884			&& parmp->parm_rdisc_int != 0
885			&& new->parm_rdisc_int != parmp->parm_rdisc_int)) {
886			return ("conflicting, duplicate router discovery"
887				" parameters");
888
889		}
890
891		if (new->parm_d_metric != 0
892		     && parmp->parm_d_metric != 0
893		     && new->parm_d_metric != parmp->parm_d_metric) {
894			return ("conflicting, duplicate poor man's router"
895				" discovery or fake default metric");
896		}
897	}
898
899	/* link new entry on the so that when the entries are scanned,
900	 * they affect the result in the order the operator specified.
901	 */
902	parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
903	memcpy(parmp, new, sizeof(*parmp));
904	*parmpp = parmp;
905
906	return 0;
907}
908
909
910/* get a network number as a name or a number, with an optional "/xx"
911 * netmask.
912 */
913int					/* 0=bad */
914getnet(char *name,
915       naddr *netp,			/* network in host byte order */
916       naddr *maskp)			/* masks are always in host order */
917{
918	int i;
919	struct netent *np;
920	naddr mask;			/* in host byte order */
921	struct in_addr in;		/* a network and so host byte order */
922	char hname[MAXHOSTNAMELEN+1];
923	char *mname, *p;
924
925
926	/* Detect and separate "1.2.3.4/24"
927	 */
928	if (0 != (mname = strrchr(name,'/'))) {
929		i = (int)(mname - name);
930		if (i > (int)sizeof(hname)-1)	/* name too long */
931			return 0;
932		memmove(hname, name, i);
933		hname[i] = '\0';
934		mname++;
935		name = hname;
936	}
937
938	np = getnetbyname(name);
939	if (np != 0) {
940		in.s_addr = (naddr)np->n_net;
941		if (0 == (in.s_addr & 0xff000000))
942			in.s_addr <<= 8;
943		if (0 == (in.s_addr & 0xff000000))
944			in.s_addr <<= 8;
945		if (0 == (in.s_addr & 0xff000000))
946			in.s_addr <<= 8;
947	} else if (inet_aton(name, &in) == 1) {
948		NTOHL(in.s_addr);
949	} else if (!mname && !strcasecmp(name,"default")) {
950		in.s_addr = RIP_DEFAULT;
951	} else {
952		return 0;
953	}
954
955	if (!mname) {
956		/* we cannot use the interfaces here because we have not
957		 * looked at them yet.
958		 */
959		mask = std_mask(htonl(in.s_addr));
960		if ((~mask & in.s_addr) != 0)
961			mask = HOST_MASK;
962	} else {
963		mask = (naddr)strtoul(mname, &p, 0);
964		if (*p != '\0' || mask > 32)
965			return 0;
966		if (mask != 0)
967			mask = HOST_MASK << (32-mask);
968	}
969
970	/* must have mask of 0 with default */
971	if (mask != 0 && in.s_addr == RIP_DEFAULT)
972		return 0;
973	/* no host bits allowed in a network number */
974	if ((~mask & in.s_addr) != 0)
975		return 0;
976	/* require non-zero network number */
977	if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
978		return 0;
979	if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
980		return 0;
981	if (in.s_addr>>24 == 0xff)
982		return 0;
983
984	*netp = in.s_addr;
985	*maskp = mask;
986	return 1;
987}
988
989
990int					/* 0=bad */
991gethost(char *name,
992	naddr *addrp)
993{
994	struct hostent *hp;
995	struct in_addr in;
996
997
998	/* Try for a number first, even in IRIX where gethostbyname()
999	 * is smart.  This avoids hitting the name server which
1000	 * might be sick because routing is.
1001	 */
1002	if (inet_aton(name, &in) == 1) {
1003		/* get a good number, but check that it it makes some
1004		 * sense.
1005		 */
1006		if (ntohl(in.s_addr)>>24 == 0
1007		    || ntohl(in.s_addr)>>24 == 0xff)
1008			return 0;
1009		*addrp = in.s_addr;
1010		return 1;
1011	}
1012
1013	hp = gethostbyname(name);
1014	if (hp) {
1015		memcpy(addrp, hp->h_addr, sizeof(*addrp));
1016		return 1;
1017	}
1018
1019	return 0;
1020}
1021