parms.c revision 1.13 1 /* $NetBSD: parms.c,v 1.13 1999/02/23 10:47:40 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 acknowledgment:
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[] __attribute__((unused)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
38 #elif defined(__NetBSD__)
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: parms.c,v 1.13 1999/02/23 10:47:40 christos Exp $");
41 #endif
42
43 #include "defs.h"
44 #include "pathnames.h"
45 #include <sys/stat.h>
46
47
48 struct parm *parms;
49 struct intnet *intnets;
50 struct r1net *r1nets;
51 struct tgate *tgates;
52
53
54 /* use configured parameters
55 */
56 void
57 get_parms(struct interface *ifp)
58 {
59 static int warned_auth_in, warned_auth_out;
60 struct parm *parmp;
61 int i, num_passwds = 0;
62
63 /* get all relevant parameters
64 */
65 for (parmp = parms; parmp != 0; parmp = parmp->parm_next) {
66 if (parmp->parm_name[0] == '\0'
67 || !strcmp(ifp->int_name, parmp->parm_name)
68 || (parmp->parm_name[0] == '\n'
69 && on_net(ifp->int_addr,
70 parmp->parm_net, parmp->parm_mask))) {
71
72 /* This group of parameters is relevant,
73 * so get its settings
74 */
75 ifp->int_state |= parmp->parm_int_state;
76 for (i = 0; i < MAX_AUTH_KEYS; i++) {
77 if (parmp->parm_auth[0].type == RIP_AUTH_NONE
78 || num_passwds >= MAX_AUTH_KEYS)
79 break;
80 memcpy(&ifp->int_auth[num_passwds++],
81 &parmp->parm_auth[i],
82 sizeof(ifp->int_auth[0]));
83 }
84 if (parmp->parm_rdisc_pref != 0)
85 ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
86 if (parmp->parm_rdisc_int != 0)
87 ifp->int_rdisc_int = parmp->parm_rdisc_int;
88 if (parmp->parm_d_metric != 0)
89 ifp->int_d_metric = parmp->parm_d_metric;
90 }
91 }
92
93 /* Set general defaults.
94 *
95 * Default poor-man's router discovery to a metric that will
96 * be heard by old versions of `routed`. They ignored received
97 * routes with metric 15.
98 */
99 if ((ifp->int_state & IS_PM_RDISC)
100 && ifp->int_d_metric == 0)
101 ifp->int_d_metric = FAKE_METRIC;
102
103 if (ifp->int_rdisc_int == 0)
104 ifp->int_rdisc_int = DefMaxAdvertiseInterval;
105
106 if (!(ifp->int_if_flags & IFF_MULTICAST)
107 && !(ifp->int_state & IS_REMOTE))
108 ifp->int_state |= IS_BCAST_RDISC;
109
110 if (ifp->int_if_flags & IFF_POINTOPOINT) {
111 ifp->int_state |= IS_BCAST_RDISC;
112 /* By default, point-to-point links should be passive
113 * about router-discovery for the sake of demand-dialing.
114 */
115 if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
116 ifp->int_state |= IS_NO_SOL_OUT;
117 if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
118 ifp->int_state |= IS_NO_ADV_OUT;
119 }
120
121 if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
122 ifp->int_state |= IS_NO_RDISC;
123 if (ifp->int_state & IS_PASSIVE)
124 ifp->int_state |= IS_NO_RIP;
125
126 if (!IS_RIP_IN_OFF(ifp->int_state)
127 && ifp->int_auth[0].type != RIP_AUTH_NONE
128 && !(ifp->int_state & IS_NO_RIPV1_IN)
129 && !warned_auth_in) {
130 msglog("Warning: RIPv1 input via %s"
131 " will be accepted without authentication",
132 ifp->int_name);
133 warned_auth_in = 1;
134 }
135 if (!IS_RIP_OUT_OFF(ifp->int_state)
136 && ifp->int_auth[0].type != RIP_AUTH_NONE
137 && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
138 if (!warned_auth_out) {
139 msglog("Warning: RIPv1 output via %s"
140 " will be sent without authentication",
141 ifp->int_name);
142 warned_auth_out = 1;
143 }
144 }
145 }
146
147
148 /* Read a list of gateways from /etc/gateways and add them to our tables.
149 *
150 * This file contains a list of "remote" gateways. That is usually
151 * a gateway which we cannot immediately determine if it is present or
152 * not as we can do for those provided by directly connected hardware.
153 *
154 * If a gateway is marked "passive" in the file, then we assume it
155 * does not understand RIP and assume it is always present. Those
156 * not marked passive are treated as if they were directly connected
157 * and assumed to be broken if they do not send us advertisements.
158 * All remote interfaces are added to our list, and those not marked
159 * passive are sent routing updates.
160 *
161 * A passive interface can also be local, hardware interface exempt
162 * from RIP.
163 */
164 void
165 gwkludge(void)
166 {
167 FILE *fp;
168 char *p, *lptr;
169 const char *cp;
170 char lbuf[200], net_host[5], dname[64+1+64+1];
171 char gname[GNAME_LEN+1], qual[9];
172 struct interface *ifp;
173 naddr dst, netmask, gate;
174 int metric, n, lnum;
175 struct stat sb;
176 u_int state;
177 const char *type;
178
179
180 fp = fopen(_PATH_GATEWAYS, "r");
181 if (fp == 0)
182 return;
183
184 if (0 > fstat(fileno(fp), &sb)) {
185 msglog("could not stat() "_PATH_GATEWAYS);
186 (void)fclose(fp);
187 return;
188 }
189
190 for (lnum = 1; ; lnum++) {
191 if (0 == fgets(lbuf, sizeof(lbuf), fp))
192 break;
193 lptr = lbuf;
194 while (*lptr == ' ')
195 lptr++;
196 p = lptr+strlen(lptr)-1;
197 while (*p == '\n'
198 || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
199 *p-- = '\0';
200 if (*lptr == '\0' /* ignore null and comment lines */
201 || *lptr == '#')
202 continue;
203
204 /* notice newfangled parameter lines
205 */
206 if (strncasecmp("net", lptr, 3)
207 && strncasecmp("host", lptr, 4)) {
208 cp = parse_parms(lptr,
209 (sb.st_uid == 0
210 && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
211 if (cp != 0)
212 msglog("%s in line %d of "_PATH_GATEWAYS,
213 cp, lnum);
214 continue;
215 }
216
217 /* {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
218 qual[0] = '\0';
219 /* the '64' here must be GNAME_LEN */
220 n = sscanf(lptr, "%4s %129[^ \t] gateway"
221 " %64[^ / \t] metric %u %8s\n",
222 net_host, dname, gname, &metric, qual);
223 if (n != 4 && n != 5) {
224 msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
225 lptr, n);
226 continue;
227 }
228 if (metric >= HOPCNT_INFINITY) {
229 msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
230 lptr);
231 continue;
232 }
233 if (!strcasecmp(net_host, "host")) {
234 if (!gethost(dname, &dst)) {
235 msglog("bad host \"%s\" in "_PATH_GATEWAYS
236 " entry \"%s\"", dname, lptr);
237 continue;
238 }
239 netmask = HOST_MASK;
240 } else if (!strcasecmp(net_host, "net")) {
241 if (!getnet(dname, &dst, &netmask)) {
242 msglog("bad net \"%s\" in "_PATH_GATEWAYS
243 " entry \"%s\"", dname, lptr);
244 continue;
245 }
246 if (dst == RIP_DEFAULT) {
247 msglog("bad net \"%s\" in "_PATH_GATEWAYS
248 " entry \"%s\"--cannot be default",
249 dname, lptr);
250 continue;
251 }
252 HTONL(dst); /* make network # into IP address */
253 } else {
254 msglog("bad \"%s\" in "_PATH_GATEWAYS
255 " entry \"%s\"", net_host, lptr);
256 continue;
257 }
258
259 if (!gethost(gname, &gate)) {
260 msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
261 " entry \"%s\"", gname, lptr);
262 continue;
263 }
264
265 if (!strcasecmp(qual, type = "passive")) {
266 /* Passive entries are not placed in our tables,
267 * only the kernel's, so we don't copy all of the
268 * external routing information within a net.
269 * Internal machines should use the default
270 * route to a suitable gateway (like us).
271 */
272 state = IS_REMOTE | IS_PASSIVE;
273 if (metric == 0)
274 metric = 1;
275
276 } else if (!strcasecmp(qual, type = "external")) {
277 /* External entries are handled by other means
278 * such as EGP, and are placed only in the daemon
279 * tables to prevent overriding them with something
280 * else.
281 */
282 strcpy(qual,"external");
283 state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
284 if (metric == 0)
285 metric = 1;
286
287 } else if (!strcasecmp(qual, "active")
288 || qual[0] == '\0') {
289 if (metric != 0) {
290 /* Entries that are neither "passive" nor
291 * "external" are "remote" and must behave
292 * like physical interfaces. If they are not
293 * heard from regularly, they are deleted.
294 */
295 state = IS_REMOTE;
296 type = "remote";
297 } else {
298 /* "remote" entries with a metric of 0
299 * are aliases for our own interfaces
300 */
301 state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
302 type = "alias";
303 }
304
305 } else {
306 msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
307 " unknown type %s", lptr, qual);
308 continue;
309 }
310
311 if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
312 state |= IS_NO_RDISC;
313 if (state & IS_PASSIVE)
314 state |= IS_NO_RIP;
315
316 ifp = check_dup(gate,dst,netmask,0);
317 if (ifp != 0) {
318 msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
319 continue;
320 }
321
322 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
323 memset(ifp, 0, sizeof(*ifp));
324
325 ifp->int_state = state;
326 if (netmask == HOST_MASK)
327 ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
328 else
329 ifp->int_if_flags = IFF_UP;
330 ifp->int_act_time = NEVER;
331 ifp->int_addr = gate;
332 ifp->int_dstaddr = dst;
333 ifp->int_mask = netmask;
334 ifp->int_ripv1_mask = netmask;
335 ifp->int_std_mask = std_mask(gate);
336 ifp->int_net = ntohl(dst);
337 ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
338 ifp->int_std_addr = htonl(ifp->int_std_net);
339 ifp->int_metric = metric;
340 if (!(state & IS_EXTERNAL)
341 && ifp->int_mask != ifp->int_std_mask)
342 ifp->int_state |= IS_SUBNET;
343 (void)sprintf(ifp->int_name, "%s(%s)", type, gname);
344 ifp->int_index = -1;
345
346 if_link(ifp);
347 }
348
349 /* After all of the parameter lines have been read,
350 * apply them to any remote interfaces.
351 */
352 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
353 get_parms(ifp);
354
355 tot_interfaces++;
356 if (!IS_RIP_OFF(ifp->int_state))
357 rip_interfaces++;
358
359 trace_if("Add", ifp);
360 }
361
362 (void)fclose(fp);
363 }
364
365
366 /* like strtok(), but honoring backslash and not changing the source string
367 */
368 static int /* 0=ok, -1=bad */
369 parse_quote(char **linep, /* look here */
370 const char *delims, /* for these delimiters */
371 char *delimp, /* 0 or put found delimiter here */
372 char *buf, /* copy token to here */
373 int lim) /* at most this many bytes */
374 {
375 char c = '\0', *pc;
376 const char *p;
377
378
379 pc = *linep;
380 if (*pc == '\0')
381 return -1;
382
383 while (lim != 0) {
384 c = *pc++;
385 if (c == '\0')
386 break;
387
388 if (c == '\\' && *pc != '\0') {
389 if ((c = *pc++) == 'n') {
390 c = '\n';
391 } else if (c == 'r') {
392 c = '\r';
393 } else if (c == 't') {
394 c = '\t';
395 } else if (c == 'b') {
396 c = '\b';
397 } else if (c >= '0' && c <= '7') {
398 c -= '0';
399 if (*pc >= '0' && *pc <= '7') {
400 c = (c<<3)+(*pc++ - '0');
401 if (*pc >= '0' && *pc <= '7')
402 c = (c<<3)+(*pc++ - '0');
403 }
404 }
405
406 } else {
407 for (p = delims; *p != '\0'; ++p) {
408 if (*p == c)
409 goto exit;
410 }
411 }
412
413 *buf++ = c;
414 --lim;
415 }
416 exit:
417 if (lim == 0)
418 return -1;
419
420 *buf = '\0'; /* terminate copy of token */
421 if (delimp != 0)
422 *delimp = c; /* return delimiter */
423 *linep = pc-1; /* say where we ended */
424 return 0;
425 }
426
427
428 /* Parse password timestamp
429 */
430 static char *
431 parse_ts(time_t *tp,
432 char **valp,
433 char *val0,
434 char *delimp,
435 char *buf,
436 u_int bufsize)
437 {
438 struct tm tm;
439 #if defined(sgi) || defined(__NetBSD__)
440 char *ptr;
441 #endif
442
443 if (0 > parse_quote(valp, "| ,\n\r", delimp,
444 buf,bufsize)
445 || buf[bufsize-1] != '\0'
446 || buf[bufsize-2] != '\0') {
447 sprintf(buf,"bad timestamp %.25s", val0);
448 return buf;
449 }
450 strcat(buf,"\n");
451 memset(&tm, 0, sizeof(tm));
452 #if defined(sgi) || defined(__NetBSD__)
453 ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
454 if (ptr == NULL || *ptr != '\0') {
455 sprintf(buf,"bad timestamp %.25s", val0);
456 return buf;
457 }
458 #else
459 if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
460 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
461 &tm.tm_hour, &tm.tm_min)
462 || tm.tm_mon < 1 || tm.tm_mon > 12
463 || tm.tm_mday < 1 || tm.tm_mday > 31) {
464 sprintf(buf,"bad timestamp %.25s", val0);
465 return buf;
466 }
467 tm.tm_mon--;
468 if (tm.tm_year <= 37) /* assume small years are in the */
469 tm.tm_year += 100; /* 3rd millenium */
470 #endif
471
472 if ((*tp = mktime(&tm)) == -1) {
473 sprintf(buf,"bad timestamp %.25s", val0);
474 return buf;
475 }
476
477 return 0;
478 }
479
480
481 /* Get a password, key ID, and expiration date in the format
482 * passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
483 */
484 static const char * /* 0 or error message */
485 get_passwd(char *tgt,
486 char *val,
487 struct parm *parmp,
488 u_int16_t type,
489 int safe) /* 1=from secure file */
490 {
491 static char buf[80];
492 char *val0, *p, delim;
493 struct auth k, *ap, *ap2;
494 int i;
495 u_long l;
496
497
498 if (!safe)
499 return "ignore unsafe password";
500
501 for (ap = parmp->parm_auth, i = 0;
502 ap->type != RIP_AUTH_NONE; i++, ap++) {
503 if (i >= MAX_AUTH_KEYS)
504 return "too many passwords";
505 }
506
507 memset(&k, 0, sizeof(k));
508 k.type = type;
509 k.end = -1-DAY;
510
511 val0 = val;
512 if (0 > parse_quote(&val, "| ,\n\r", &delim,
513 (char *)k.key, sizeof(k.key)))
514 return tgt;
515
516 if (delim != '|') {
517 if (type == RIP_AUTH_MD5)
518 return "missing Keyid";
519 } else {
520 val0 = ++val;
521 buf[sizeof(buf)-1] = '\0';
522 if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
523 || buf[sizeof(buf)-1] != '\0'
524 || (l = strtoul(buf,&p,0)) > 255
525 || *p != '\0') {
526 sprintf(buf,"bad KeyID \"%.20s\"", val0);
527 return buf;
528 }
529 for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
530 if (ap2->keyid == l) {
531 sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
532 return buf;
533 }
534 }
535 k.keyid = (int)l;
536
537 if (delim == '|') {
538 val0 = ++val;
539 if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
540 buf,sizeof(buf))))
541 return p;
542 if (delim != '|')
543 return "missing second timestamp";
544 val0 = ++val;
545 if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
546 buf,sizeof(buf))))
547 return p;
548 if ((u_long)k.start > (u_long)k.end) {
549 sprintf(buf,"out of order timestamp %.30s",
550 val0);
551 return buf;
552 }
553 }
554 }
555 if (delim != '\0')
556 return tgt;
557
558 memmove(ap, &k, sizeof(*ap));
559 return 0;
560 }
561
562
563 static const char *
564 bad_str(const char *estr)
565 {
566 static char buf[100+8];
567
568 sprintf(buf, "bad \"%.100s\"", estr);
569 return buf;
570 }
571
572
573 /* Parse a set of parameters for an interface.
574 */
575 const char * /* 0 or error message */
576 parse_parms(char *line,
577 int safe) /* 1=from secure file */
578 {
579 #define PARS(str) (!strcasecmp(tgt, str))
580 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
581 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break; \
582 parm.parm_int_state |= (b);}
583 struct parm parm;
584 struct intnet *intnetp;
585 struct r1net *r1netp;
586 struct tgate *tg;
587 naddr addr, mask;
588 char delim, *val0 = 0, *tgt, *val, *p;
589 const char *msg;
590 char buf[BUFSIZ], buf2[BUFSIZ];
591 int i;
592
593
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 */
834 const char * /* 0 or error message */
835 check_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 */
913 int /* 0=bad */
914 getnet(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
990 int /* 0=bad */
991 gethost(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