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