1 /* 2 * Copyright (c) 1998-2000 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1986, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14 #include <sendmail.h> 15 16 #ifndef lint 17 # if NAMED_BIND 18 static char id[] = "@(#)$Id: domain.c,v 8.114.6.1.2.3 2000/06/13 18:00:08 gshapiro Exp $ (with name server)"; 19 # else /* NAMED_BIND */ 20 static char id[] = "@(#)$Id: domain.c,v 8.114.6.1.2.3 2000/06/13 18:00:08 gshapiro Exp $ (without name server)"; 21 # endif /* NAMED_BIND */ 22 #endif /* ! lint */ 23 24 25 #if NAMED_BIND 26 27 # include <arpa/inet.h> 28 29 /* 30 ** The standard udp packet size PACKETSZ (512) is not sufficient for some 31 ** nameserver answers containing very many resource records. The resolver 32 ** may switch to tcp and retry if it detects udp packet overflow. 33 ** Also note that the resolver routines res_query and res_search return 34 ** the size of the *un*truncated answer in case the supplied answer buffer 35 ** it not big enough to accommodate the entire answer. 36 */ 37 38 # ifndef MAXPACKET 39 # define MAXPACKET 8192 /* max packet size used internally by BIND */ 40 # endif /* ! MAXPACKET */ 41 42 typedef union 43 { 44 HEADER qb1; 45 u_char qb2[MAXPACKET]; 46 } querybuf; 47 48 # ifndef MXHOSTBUFSIZE 49 # define MXHOSTBUFSIZE (128 * MAXMXHOSTS) 50 # endif /* ! MXHOSTBUFSIZE */ 51 52 static char MXHostBuf[MXHOSTBUFSIZE]; 53 54 # ifndef MAXDNSRCH 55 # define MAXDNSRCH 6 /* number of possible domains to search */ 56 # endif /* ! MAXDNSRCH */ 57 58 # ifndef RES_DNSRCH_VARIABLE 59 # define RES_DNSRCH_VARIABLE _res.dnsrch 60 # endif /* ! RES_DNSRCH_VARIABLE */ 61 62 # ifndef MAX 63 # define MAX(a, b) ((a) > (b) ? (a) : (b)) 64 # endif /* ! MAX */ 65 66 # ifndef NO_DATA 67 # define NO_DATA NO_ADDRESS 68 # endif /* ! NO_DATA */ 69 70 # ifndef HFIXEDSZ 71 # define HFIXEDSZ 12 /* sizeof(HEADER) */ 72 # endif /* ! HFIXEDSZ */ 73 74 # define MAXCNAMEDEPTH 10 /* maximum depth of CNAME recursion */ 75 76 # if defined(__RES) && (__RES >= 19940415) 77 # define RES_UNC_T char * 78 # else /* defined(__RES) && (__RES >= 19940415) */ 79 # define RES_UNC_T u_char * 80 # endif /* defined(__RES) && (__RES >= 19940415) */ 81 82 static char *gethostalias __P((char *)); 83 static int mxrand __P((char *)); 84 85 /* 86 ** GETMXRR -- get MX resource records for a domain 87 ** 88 ** Parameters: 89 ** host -- the name of the host to MX. 90 ** mxhosts -- a pointer to a return buffer of MX records. 91 ** mxprefs -- a pointer to a return buffer of MX preferences. 92 ** If NULL, don't try to populate. 93 ** droplocalhost -- If TRUE, all MX records less preferred 94 ** than the local host (as determined by $=w) will 95 ** be discarded. 96 ** rcode -- a pointer to an EX_ status code. 97 ** 98 ** Returns: 99 ** The number of MX records found. 100 ** -1 if there is an internal failure. 101 ** If no MX records are found, mxhosts[0] is set to host 102 ** and 1 is returned. 103 */ 104 105 int 106 getmxrr(host, mxhosts, mxprefs, droplocalhost, rcode) 107 char *host; 108 char **mxhosts; 109 u_short *mxprefs; 110 bool droplocalhost; 111 int *rcode; 112 { 113 register u_char *eom, *cp; 114 register int i, j, n; 115 int nmx = 0; 116 register char *bp; 117 HEADER *hp; 118 querybuf answer; 119 int ancount, qdcount, buflen; 120 bool seenlocal = FALSE; 121 u_short pref, type; 122 u_short localpref = 256; 123 char *fallbackMX = FallBackMX; 124 bool trycanon = FALSE; 125 u_short *prefs; 126 int (*resfunc)(); 127 u_short prefer[MAXMXHOSTS]; 128 int weight[MAXMXHOSTS]; 129 extern int res_query(), res_search(); 130 131 if (tTd(8, 2)) 132 dprintf("getmxrr(%s, droplocalhost=%d)\n", 133 host, droplocalhost); 134 135 if (fallbackMX != NULL && droplocalhost && 136 wordinclass(fallbackMX, 'w')) 137 { 138 /* don't use fallback for this pass */ 139 fallbackMX = NULL; 140 } 141 142 *rcode = EX_OK; 143 144 if (mxprefs != NULL) 145 prefs = mxprefs; 146 else 147 prefs = prefer; 148 149 150 /* efficiency hack -- numeric or non-MX lookups */ 151 if (host[0] == '[') 152 goto punt; 153 154 /* 155 ** If we don't have MX records in our host switch, don't 156 ** try for MX records. Note that this really isn't "right", 157 ** since we might be set up to try NIS first and then DNS; 158 ** if the host is found in NIS we really shouldn't be doing 159 ** MX lookups. However, that should be a degenerate case. 160 */ 161 162 if (!UseNameServer) 163 goto punt; 164 if (HasWildcardMX && ConfigLevel >= 6) 165 resfunc = res_query; 166 else 167 resfunc = res_search; 168 169 errno = 0; 170 n = (*resfunc)(host, C_IN, T_MX, (u_char *) &answer, sizeof(answer)); 171 if (n < 0) 172 { 173 if (tTd(8, 1)) 174 dprintf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n", 175 (host == NULL) ? "<NULL>" : host, errno, h_errno); 176 switch (h_errno) 177 { 178 case NO_DATA: 179 trycanon = TRUE; 180 /* FALLTHROUGH */ 181 182 case NO_RECOVERY: 183 /* no MX data on this host */ 184 goto punt; 185 186 case HOST_NOT_FOUND: 187 # if BROKEN_RES_SEARCH 188 case 0: /* Ultrix resolver retns failure w/ h_errno=0 */ 189 # endif /* BROKEN_RES_SEARCH */ 190 /* host doesn't exist in DNS; might be in /etc/hosts */ 191 trycanon = TRUE; 192 *rcode = EX_NOHOST; 193 goto punt; 194 195 case TRY_AGAIN: 196 case -1: 197 /* couldn't connect to the name server */ 198 if (fallbackMX != NULL) 199 { 200 /* name server is hosed -- push to fallback */ 201 if (nmx > 0) 202 prefs[nmx] = prefs[nmx - 1] + 1; 203 else 204 prefs[nmx] = 0; 205 mxhosts[nmx++] = fallbackMX; 206 return nmx; 207 } 208 /* it might come up later; better queue it up */ 209 *rcode = EX_TEMPFAIL; 210 break; 211 212 default: 213 syserr("getmxrr: res_search (%s) failed with impossible h_errno (%d)\n", 214 host, h_errno); 215 *rcode = EX_OSERR; 216 break; 217 } 218 219 /* irreconcilable differences */ 220 return -1; 221 } 222 223 /* avoid problems after truncation in tcp packets */ 224 if (n > sizeof(answer)) 225 n = sizeof(answer); 226 227 /* find first satisfactory answer */ 228 hp = (HEADER *)&answer; 229 cp = (u_char *)&answer + HFIXEDSZ; 230 eom = (u_char *)&answer + n; 231 for (qdcount = ntohs((u_short)hp->qdcount); 232 qdcount--; 233 cp += n + QFIXEDSZ) 234 { 235 if ((n = dn_skipname(cp, eom)) < 0) 236 goto punt; 237 } 238 buflen = sizeof(MXHostBuf) - 1; 239 bp = MXHostBuf; 240 ancount = ntohs((u_short)hp->ancount); 241 while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1) 242 { 243 if ((n = dn_expand((u_char *)&answer, 244 eom, cp, (RES_UNC_T) bp, buflen)) < 0) 245 break; 246 cp += n; 247 GETSHORT(type, cp); 248 cp += INT16SZ + INT32SZ; 249 GETSHORT(n, cp); 250 if (type != T_MX) 251 { 252 if (tTd(8, 8) || _res.options & RES_DEBUG) 253 dprintf("unexpected answer type %d, size %d\n", 254 type, n); 255 cp += n; 256 continue; 257 } 258 GETSHORT(pref, cp); 259 if ((n = dn_expand((u_char *)&answer, eom, cp, 260 (RES_UNC_T) bp, buflen)) < 0) 261 break; 262 cp += n; 263 if (wordinclass(bp, 'w')) 264 { 265 if (tTd(8, 3)) 266 dprintf("found localhost (%s) in MX list, pref=%d\n", 267 bp, pref); 268 if (droplocalhost) 269 { 270 if (!seenlocal || pref < localpref) 271 localpref = pref; 272 seenlocal = TRUE; 273 continue; 274 } 275 weight[nmx] = 0; 276 } 277 else 278 weight[nmx] = mxrand(bp); 279 prefs[nmx] = pref; 280 mxhosts[nmx++] = bp; 281 n = strlen(bp); 282 bp += n; 283 if (bp[-1] != '.') 284 { 285 *bp++ = '.'; 286 n++; 287 } 288 *bp++ = '\0'; 289 buflen -= n + 1; 290 } 291 292 /* sort the records */ 293 for (i = 0; i < nmx; i++) 294 { 295 for (j = i + 1; j < nmx; j++) 296 { 297 if (prefs[i] > prefs[j] || 298 (prefs[i] == prefs[j] && weight[i] > weight[j])) 299 { 300 register int temp; 301 register char *temp1; 302 303 temp = prefs[i]; 304 prefs[i] = prefs[j]; 305 prefs[j] = temp; 306 temp1 = mxhosts[i]; 307 mxhosts[i] = mxhosts[j]; 308 mxhosts[j] = temp1; 309 temp = weight[i]; 310 weight[i] = weight[j]; 311 weight[j] = temp; 312 } 313 } 314 if (seenlocal && prefs[i] >= localpref) 315 { 316 /* truncate higher preference part of list */ 317 nmx = i; 318 } 319 } 320 321 /* delete duplicates from list (yes, some bozos have duplicates) */ 322 for (i = 0; i < nmx - 1; ) 323 { 324 if (strcasecmp(mxhosts[i], mxhosts[i + 1]) != 0) 325 i++; 326 else 327 { 328 /* compress out duplicate */ 329 for (j = i + 1; j < nmx; j++) 330 { 331 mxhosts[j] = mxhosts[j + 1]; 332 prefs[j] = prefs[j + 1]; 333 } 334 nmx--; 335 } 336 } 337 338 if (nmx == 0) 339 { 340 punt: 341 if (seenlocal) 342 { 343 struct hostent *h = NULL; 344 345 /* 346 ** If we have deleted all MX entries, this is 347 ** an error -- we should NEVER send to a host that 348 ** has an MX, and this should have been caught 349 ** earlier in the config file. 350 ** 351 ** Some sites prefer to go ahead and try the 352 ** A record anyway; that case is handled by 353 ** setting TryNullMXList. I believe this is a 354 ** bad idea, but it's up to you.... 355 */ 356 357 if (TryNullMXList) 358 { 359 h_errno = 0; 360 errno = 0; 361 h = sm_gethostbyname(host, AF_INET); 362 if (h == NULL) 363 { 364 if (errno == ETIMEDOUT || 365 h_errno == TRY_AGAIN || 366 (errno == ECONNREFUSED && 367 UseNameServer)) 368 { 369 *rcode = EX_TEMPFAIL; 370 return -1; 371 } 372 # if NETINET6 373 h_errno = 0; 374 errno = 0; 375 h = sm_gethostbyname(host, AF_INET6); 376 if (h == NULL && 377 (errno == ETIMEDOUT || 378 h_errno == TRY_AGAIN || 379 (errno == ECONNREFUSED && 380 UseNameServer))) 381 { 382 *rcode = EX_TEMPFAIL; 383 return -1; 384 } 385 # endif /* NETINET6 */ 386 } 387 } 388 389 if (h == NULL) 390 { 391 *rcode = EX_CONFIG; 392 syserr("MX list for %s points back to %s", 393 host, MyHostName); 394 return -1; 395 } 396 } 397 if (strlen(host) >= (SIZE_T) sizeof MXHostBuf) 398 { 399 *rcode = EX_CONFIG; 400 syserr("Host name %s too long", 401 shortenstring(host, MAXSHORTSTR)); 402 return -1; 403 } 404 snprintf(MXHostBuf, sizeof MXHostBuf, "%s", host); 405 mxhosts[0] = MXHostBuf; 406 prefs[0] = 0; 407 if (host[0] == '[') 408 { 409 register char *p; 410 # if NETINET6 411 struct sockaddr_in6 tmp6; 412 # endif /* NETINET6 */ 413 414 /* this may be an MX suppression-style address */ 415 p = strchr(MXHostBuf, ']'); 416 if (p != NULL) 417 { 418 *p = '\0'; 419 420 if (inet_addr(&MXHostBuf[1]) != INADDR_NONE) 421 { 422 nmx++; 423 *p = ']'; 424 } 425 # if NETINET6 426 else if (inet_pton(AF_INET6, &MXHostBuf[1], 427 &tmp6.sin6_addr) == 1) 428 { 429 nmx++; 430 *p = ']'; 431 } 432 # endif /* NETINET6 */ 433 else 434 { 435 trycanon = TRUE; 436 mxhosts[0]++; 437 } 438 } 439 } 440 if (trycanon && 441 getcanonname(mxhosts[0], sizeof MXHostBuf - 2, FALSE)) 442 { 443 bp = &MXHostBuf[strlen(MXHostBuf)]; 444 if (bp[-1] != '.') 445 { 446 *bp++ = '.'; 447 *bp = '\0'; 448 } 449 nmx = 1; 450 } 451 } 452 453 /* if we have a default lowest preference, include that */ 454 if (fallbackMX != NULL && !seenlocal) 455 { 456 if (nmx > 0) 457 prefs[nmx] = prefs[nmx - 1] + 1; 458 else 459 prefs[nmx] = 0; 460 mxhosts[nmx++] = fallbackMX; 461 } 462 463 return nmx; 464 } 465 /* 466 ** MXRAND -- create a randomizer for equal MX preferences 467 ** 468 ** If two MX hosts have equal preferences we want to randomize 469 ** the selection. But in order for signatures to be the same, 470 ** we need to randomize the same way each time. This function 471 ** computes a pseudo-random hash function from the host name. 472 ** 473 ** Parameters: 474 ** host -- the name of the host. 475 ** 476 ** Returns: 477 ** A random but repeatable value based on the host name. 478 ** 479 ** Side Effects: 480 ** none. 481 */ 482 483 static int 484 mxrand(host) 485 register char *host; 486 { 487 int hfunc; 488 static unsigned int seed; 489 490 if (seed == 0) 491 { 492 seed = (int) curtime() & 0xffff; 493 if (seed == 0) 494 seed++; 495 } 496 497 if (tTd(17, 9)) 498 dprintf("mxrand(%s)", host); 499 500 hfunc = seed; 501 while (*host != '\0') 502 { 503 int c = *host++; 504 505 if (isascii(c) && isupper(c)) 506 c = tolower(c); 507 hfunc = ((hfunc << 1) ^ c) % 2003; 508 } 509 510 hfunc &= 0xff; 511 hfunc++; 512 513 if (tTd(17, 9)) 514 dprintf(" = %d\n", hfunc); 515 return hfunc; 516 } 517 /* 518 ** BESTMX -- find the best MX for a name 519 ** 520 ** This is really a hack, but I don't see any obvious way 521 ** to generalize it at the moment. 522 */ 523 524 /* ARGSUSED3 */ 525 char * 526 bestmx_map_lookup(map, name, av, statp) 527 MAP *map; 528 char *name; 529 char **av; 530 int *statp; 531 { 532 int nmx; 533 int saveopts = _res.options; 534 int i, len = 0; 535 char *p; 536 char *mxhosts[MAXMXHOSTS + 1]; 537 char buf[PSBUFSIZE / 2]; 538 539 _res.options &= ~(RES_DNSRCH|RES_DEFNAMES); 540 nmx = getmxrr(name, mxhosts, NULL, FALSE, statp); 541 _res.options = saveopts; 542 if (nmx <= 0) 543 return NULL; 544 if (bitset(MF_MATCHONLY, map->map_mflags)) 545 return map_rewrite(map, name, strlen(name), NULL); 546 if ((map->map_coldelim == '\0') || (nmx == 1)) 547 return map_rewrite(map, mxhosts[0], strlen(mxhosts[0]), av); 548 549 /* 550 ** We were given a -z flag (return all MXs) and there are multiple 551 ** ones. We need to build them all into a list. 552 */ 553 p = buf; 554 for (i = 0; i < nmx; i++) 555 { 556 int slen; 557 558 if (strchr(mxhosts[i], map->map_coldelim) != NULL) 559 { 560 syserr("bestmx_map_lookup: MX host %.64s includes map delimiter character 0x%02X", 561 mxhosts[i], map->map_coldelim); 562 return NULL; 563 } 564 slen = strlen(mxhosts[i]); 565 if (len + slen + 2 > sizeof buf) 566 break; 567 if (i > 0) 568 { 569 *p++ = map->map_coldelim; 570 len++; 571 } 572 (void) strlcpy(p, mxhosts[i], sizeof buf - len); 573 p += slen; 574 len += slen; 575 } 576 return map_rewrite(map, buf, len, av); 577 } 578 /* 579 ** DNS_GETCANONNAME -- get the canonical name for named host using DNS 580 ** 581 ** This algorithm tries to be smart about wildcard MX records. 582 ** This is hard to do because DNS doesn't tell is if we matched 583 ** against a wildcard or a specific MX. 584 ** 585 ** We always prefer A & CNAME records, since these are presumed 586 ** to be specific. 587 ** 588 ** If we match an MX in one pass and lose it in the next, we use 589 ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 590 ** A hostname bletch.foo.bar.com will match against this MX, but 591 ** will stop matching when we try bletch.bar.com -- so we know 592 ** that bletch.foo.bar.com must have been right. This fails if 593 ** there was also an MX record matching *.BAR.COM, but there are 594 ** some things that just can't be fixed. 595 ** 596 ** Parameters: 597 ** host -- a buffer containing the name of the host. 598 ** This is a value-result parameter. 599 ** hbsize -- the size of the host buffer. 600 ** trymx -- if set, try MX records as well as A and CNAME. 601 ** statp -- pointer to place to store status. 602 ** 603 ** Returns: 604 ** TRUE -- if the host matched. 605 ** FALSE -- otherwise. 606 */ 607 608 bool 609 dns_getcanonname(host, hbsize, trymx, statp) 610 char *host; 611 int hbsize; 612 bool trymx; 613 int *statp; 614 { 615 register u_char *eom, *ap; 616 register char *cp; 617 register int n; 618 HEADER *hp; 619 querybuf answer; 620 int ancount, qdcount; 621 int ret; 622 char **domain; 623 int type; 624 char **dp; 625 char *mxmatch; 626 bool amatch; 627 bool gotmx = FALSE; 628 int qtype; 629 int loopcnt; 630 char *xp; 631 char nbuf[MAX(MAXPACKET, MAXDNAME*2+2)]; 632 char *searchlist[MAXDNSRCH+2]; 633 634 if (tTd(8, 2)) 635 dprintf("dns_getcanonname(%s, trymx=%d)\n", host, trymx); 636 637 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 638 { 639 *statp = EX_UNAVAILABLE; 640 return FALSE; 641 } 642 643 /* 644 ** Initialize domain search list. If there is at least one 645 ** dot in the name, search the unmodified name first so we 646 ** find "vse.CS" in Czechoslovakia instead of in the local 647 ** domain (e.g., vse.CS.Berkeley.EDU). Note that there is no 648 ** longer a country named Czechoslovakia but this type of problem 649 ** is still present. 650 ** 651 ** Older versions of the resolver could create this 652 ** list by tearing apart the host name. 653 */ 654 655 loopcnt = 0; 656 cnameloop: 657 /* Check for dots in the name */ 658 for (cp = host, n = 0; *cp != '\0'; cp++) 659 if (*cp == '.') 660 n++; 661 662 /* 663 ** If this is a simple name, determine whether it matches an 664 ** alias in the file defined by the environment variable HOSTALIASES. 665 */ 666 if (n == 0 && (xp = gethostalias(host)) != NULL) 667 { 668 if (loopcnt++ > MAXCNAMEDEPTH) 669 { 670 syserr("loop in ${HOSTALIASES} file"); 671 } 672 else 673 { 674 (void) strlcpy(host, xp, hbsize); 675 goto cnameloop; 676 } 677 } 678 679 /* 680 ** Build the search list. 681 ** If there is at least one dot in name, start with a null 682 ** domain to search the unmodified name first. 683 ** If name does not end with a dot and search up local domain 684 ** tree desired, append each local domain component to the 685 ** search list; if name contains no dots and default domain 686 ** name is desired, append default domain name to search list; 687 ** else if name ends in a dot, remove that dot. 688 */ 689 690 dp = searchlist; 691 if (n > 0) 692 *dp++ = ""; 693 if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 694 { 695 /* make sure there are less than MAXDNSRCH domains */ 696 for (domain = RES_DNSRCH_VARIABLE, ret = 0; 697 *domain != NULL && ret < MAXDNSRCH; 698 ret++) 699 *dp++ = *domain++; 700 } 701 else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 702 { 703 *dp++ = _res.defdname; 704 } 705 else if (*cp == '.') 706 { 707 *cp = '\0'; 708 } 709 *dp = NULL; 710 711 /* 712 ** Now loop through the search list, appending each domain in turn 713 ** name and searching for a match. 714 */ 715 716 mxmatch = NULL; 717 qtype = T_ANY; 718 719 for (dp = searchlist; *dp != NULL; ) 720 { 721 if (qtype == T_ANY) 722 gotmx = FALSE; 723 if (tTd(8, 5)) 724 dprintf("dns_getcanonname: trying %s.%s (%s)\n", 725 host, *dp, 726 qtype == T_ANY ? "ANY" : 727 # if NETINET6 728 qtype == T_AAAA ? "AAAA" : 729 # endif /* NETINET6 */ 730 qtype == T_A ? "A" : 731 qtype == T_MX ? "MX" : 732 "???"); 733 ret = res_querydomain(host, *dp, C_IN, qtype, 734 answer.qb2, sizeof(answer.qb2)); 735 if (ret <= 0) 736 { 737 if (tTd(8, 7)) 738 dprintf("\tNO: errno=%d, h_errno=%d\n", 739 errno, h_errno); 740 741 if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 742 { 743 /* the name server seems to be down */ 744 h_errno = TRY_AGAIN; 745 *statp = EX_TEMPFAIL; 746 747 /* 748 ** If the ANY query is larger than the 749 ** UDP packet size, the resolver will 750 ** fall back to TCP. However, some 751 ** misconfigured firewalls block 53/TCP 752 ** so the ANY lookup fails whereas an MX 753 ** or A record might work. Therefore, 754 ** don't fail on ANY queries. 755 ** 756 ** The ANY query is really meant to prime 757 ** the cache so this isn't dangerous. 758 */ 759 760 if (qtype != T_ANY) 761 return FALSE; 762 } 763 764 if (h_errno != HOST_NOT_FOUND) 765 { 766 /* might have another type of interest */ 767 if (qtype == T_ANY) 768 { 769 # if NETINET6 770 qtype = T_AAAA; 771 # else /* NETINET6 */ 772 qtype = T_A; 773 # endif /* NETINET6 */ 774 continue; 775 } 776 # if NETINET6 777 else if (qtype == T_AAAA) 778 { 779 qtype = T_A; 780 continue; 781 } 782 # endif /* NETINET6 */ 783 else if (qtype == T_A && !gotmx && 784 (trymx || **dp == '\0')) 785 { 786 qtype = T_MX; 787 continue; 788 } 789 } 790 791 /* definite no -- try the next domain */ 792 dp++; 793 qtype = T_ANY; 794 continue; 795 } 796 else if (tTd(8, 7)) 797 dprintf("\tYES\n"); 798 799 /* avoid problems after truncation in tcp packets */ 800 if (ret > sizeof(answer)) 801 ret = sizeof(answer); 802 803 /* 804 ** Appear to have a match. Confirm it by searching for A or 805 ** CNAME records. If we don't have a local domain 806 ** wild card MX record, we will accept MX as well. 807 */ 808 809 hp = (HEADER *) &answer; 810 ap = (u_char *) &answer + HFIXEDSZ; 811 eom = (u_char *) &answer + ret; 812 813 /* skip question part of response -- we know what we asked */ 814 for (qdcount = ntohs((u_short)hp->qdcount); 815 qdcount--; 816 ap += ret + QFIXEDSZ) 817 { 818 if ((ret = dn_skipname(ap, eom)) < 0) 819 { 820 if (tTd(8, 20)) 821 dprintf("qdcount failure (%d)\n", 822 ntohs((u_short)hp->qdcount)); 823 *statp = EX_SOFTWARE; 824 return FALSE; /* ???XXX??? */ 825 } 826 } 827 828 amatch = FALSE; 829 for (ancount = ntohs((u_short)hp->ancount); 830 --ancount >= 0 && ap < eom; 831 ap += n) 832 { 833 n = dn_expand((u_char *) &answer, eom, ap, 834 (RES_UNC_T) nbuf, sizeof nbuf); 835 if (n < 0) 836 break; 837 ap += n; 838 GETSHORT(type, ap); 839 ap += INT16SZ + INT32SZ; 840 GETSHORT(n, ap); 841 switch (type) 842 { 843 case T_MX: 844 gotmx = TRUE; 845 if (**dp != '\0' && HasWildcardMX) 846 { 847 /* 848 ** If we are using MX matches and have 849 ** not yet gotten one, save this one 850 ** but keep searching for an A or 851 ** CNAME match. 852 */ 853 854 if (trymx && mxmatch == NULL) 855 mxmatch = *dp; 856 continue; 857 } 858 859 /* 860 ** If we did not append a domain name, this 861 ** must have been a canonical name to start 862 ** with. Even if we did append a domain name, 863 ** in the absence of a wildcard MX this must 864 ** still be a real MX match. 865 ** Such MX matches are as good as an A match, 866 ** fall through. 867 */ 868 /* FALLTHROUGH */ 869 870 # if NETINET6 871 case T_AAAA: 872 /* Flag that a good match was found */ 873 amatch = TRUE; 874 875 /* continue in case a CNAME also exists */ 876 continue; 877 # endif /* NETINET6 */ 878 879 case T_A: 880 /* Flag that a good match was found */ 881 amatch = TRUE; 882 883 /* continue in case a CNAME also exists */ 884 continue; 885 886 case T_CNAME: 887 if (DontExpandCnames) 888 { 889 /* got CNAME -- guaranteed canonical */ 890 amatch = TRUE; 891 break; 892 } 893 894 if (loopcnt++ > MAXCNAMEDEPTH) 895 { 896 /*XXX should notify postmaster XXX*/ 897 message("DNS failure: CNAME loop for %s", 898 host); 899 if (CurEnv->e_message == NULL) 900 { 901 char ebuf[MAXLINE]; 902 903 snprintf(ebuf, sizeof ebuf, 904 "Deferred: DNS failure: CNAME loop for %.100s", 905 host); 906 CurEnv->e_message = newstr(ebuf); 907 } 908 h_errno = NO_RECOVERY; 909 *statp = EX_CONFIG; 910 return FALSE; 911 } 912 913 /* value points at name */ 914 if ((ret = dn_expand((u_char *)&answer, 915 eom, ap, (RES_UNC_T) nbuf, sizeof(nbuf))) < 0) 916 break; 917 (void)strlcpy(host, nbuf, hbsize); 918 919 /* 920 ** RFC 1034 section 3.6 specifies that CNAME 921 ** should point at the canonical name -- but 922 ** urges software to try again anyway. 923 */ 924 925 goto cnameloop; 926 927 default: 928 /* not a record of interest */ 929 continue; 930 } 931 } 932 933 if (amatch) 934 { 935 /* 936 ** Got a good match -- either an A, CNAME, or an 937 ** exact MX record. Save it and get out of here. 938 */ 939 940 mxmatch = *dp; 941 break; 942 } 943 944 /* 945 ** Nothing definitive yet. 946 ** If this was a T_ANY query, we don't really know what 947 ** was returned -- it might have been a T_NS, 948 ** for example. Try T_A to be more specific 949 ** during the next pass. 950 ** If this was a T_A query and we haven't yet found a MX 951 ** match, try T_MX if allowed to do so. 952 ** Otherwise, try the next domain. 953 */ 954 955 if (qtype == T_ANY) 956 { 957 # if NETINET6 958 qtype = T_AAAA; 959 # else /* NETINET6 */ 960 qtype = T_A; 961 # endif /* NETINET6 */ 962 } 963 # if NETINET6 964 else if (qtype == T_AAAA) 965 qtype = T_A; 966 # endif /* NETINET6 */ 967 else if (qtype == T_A && !gotmx && (trymx || **dp == '\0')) 968 qtype = T_MX; 969 else 970 { 971 qtype = T_ANY; 972 dp++; 973 } 974 } 975 976 /* if nothing was found, we are done */ 977 if (mxmatch == NULL) 978 { 979 *statp = EX_NOHOST; 980 return FALSE; 981 } 982 983 /* 984 ** Create canonical name and return. 985 ** If saved domain name is null, name was already canonical. 986 ** Otherwise append the saved domain name. 987 */ 988 989 (void) snprintf(nbuf, sizeof nbuf, "%.*s%s%.*s", MAXDNAME, host, 990 *mxmatch == '\0' ? "" : ".", 991 MAXDNAME, mxmatch); 992 (void) strlcpy(host, nbuf, hbsize); 993 if (tTd(8, 5)) 994 dprintf("dns_getcanonname: %s\n", host); 995 *statp = EX_OK; 996 return TRUE; 997 } 998 999 static char * 1000 gethostalias(host) 1001 char *host; 1002 { 1003 char *fname; 1004 FILE *fp; 1005 register char *p = NULL; 1006 long sff = SFF_REGONLY; 1007 char buf[MAXLINE]; 1008 static char hbuf[MAXDNAME]; 1009 1010 if (DontLockReadFiles) 1011 sff |= SFF_NOLOCK; 1012 fname = getenv("HOSTALIASES"); 1013 if (fname == NULL || 1014 (fp = safefopen(fname, O_RDONLY, 0, sff)) == NULL) 1015 return NULL; 1016 while (fgets(buf, sizeof buf, fp) != NULL) 1017 { 1018 for (p = buf; p != '\0' && !(isascii(*p) && isspace(*p)); p++) 1019 continue; 1020 if (*p == 0) 1021 { 1022 /* syntax error */ 1023 continue; 1024 } 1025 *p++ = '\0'; 1026 if (strcasecmp(buf, host) == 0) 1027 break; 1028 } 1029 1030 if (feof(fp)) 1031 { 1032 /* no match */ 1033 (void) fclose(fp); 1034 return NULL; 1035 } 1036 (void) fclose(fp); 1037 1038 /* got a match; extract the equivalent name */ 1039 while (*p != '\0' && isascii(*p) && isspace(*p)) 1040 p++; 1041 host = p; 1042 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 1043 p++; 1044 *p = '\0'; 1045 (void) strlcpy(hbuf, host, sizeof hbuf); 1046 return hbuf; 1047 } 1048 #endif /* NAMED_BIND */ 1049