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