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