1 /* 2 * Copyright (c) 1986 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #include "sendmail.h" 10 11 #ifndef lint 12 #if NAMED_BIND 13 static char sccsid[] = "@(#)domain.c 8.26 (Berkeley) 10/24/94 (with name server)"; 14 #else 15 static char sccsid[] = "@(#)domain.c 8.26 (Berkeley) 10/24/94 (without name server)"; 16 #endif 17 #endif /* not lint */ 18 19 #if NAMED_BIND 20 21 #include <errno.h> 22 #include <resolv.h> 23 #include <netdb.h> 24 25 typedef union 26 { 27 HEADER qb1; 28 u_char qb2[PACKETSZ]; 29 } querybuf; 30 31 static char MXHostBuf[MAXMXHOSTS*PACKETSZ]; 32 33 #ifndef MAXDNSRCH 34 #define MAXDNSRCH 6 /* number of possible domains to search */ 35 #endif 36 37 #ifndef MAX 38 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 39 #endif 40 41 #ifndef NO_DATA 42 # define NO_DATA NO_ADDRESS 43 #endif 44 45 #ifndef HFIXEDSZ 46 # define HFIXEDSZ 12 /* sizeof(HEADER) */ 47 #endif 48 49 #define MAXCNAMEDEPTH 10 /* maximum depth of CNAME recursion */ 50 51 #if defined(__RES) && (__RES >= 19940415) 52 # define RES_UNC_T char * 53 #else 54 # define RES_UNC_T u_char * 55 #endif 56 /* 57 ** GETMXRR -- get MX resource records for a domain 58 ** 59 ** Parameters: 60 ** host -- the name of the host to MX. 61 ** mxhosts -- a pointer to a return buffer of MX records. 62 ** droplocalhost -- If TRUE, all MX records less preferred 63 ** than the local host (as determined by $=w) will 64 ** be discarded. 65 ** rcode -- a pointer to an EX_ status code. 66 ** 67 ** Returns: 68 ** The number of MX records found. 69 ** -1 if there is an internal failure. 70 ** If no MX records are found, mxhosts[0] is set to host 71 ** and 1 is returned. 72 */ 73 74 getmxrr(host, mxhosts, droplocalhost, rcode) 75 char *host; 76 char **mxhosts; 77 bool droplocalhost; 78 int *rcode; 79 { 80 extern int h_errno; 81 register u_char *eom, *cp; 82 register int i, j, n; 83 int nmx = 0; 84 register char *bp; 85 HEADER *hp; 86 querybuf answer; 87 int ancount, qdcount, buflen; 88 bool seenlocal = FALSE; 89 u_short pref, localpref, type; 90 char *fallbackMX = FallBackMX; 91 static bool firsttime = TRUE; 92 STAB *st; 93 bool trycanon = FALSE; 94 u_short prefer[MAXMXHOSTS]; 95 int weight[MAXMXHOSTS]; 96 extern bool getcanonname(); 97 extern bool mx_enabled(); 98 99 if (tTd(8, 2)) 100 printf("getmxrr(%s, droplocalhost=%d)\n", host, droplocalhost); 101 102 if (fallbackMX != NULL) 103 { 104 if (firsttime && 105 res_query(FallBackMX, C_IN, T_A, 106 (u_char *) &answer, sizeof answer) < 0) 107 { 108 /* this entry is bogus */ 109 fallbackMX = FallBackMX = NULL; 110 } 111 else if (droplocalhost && 112 (st = stab(fallbackMX, ST_CLASS, ST_FIND)) != NULL && 113 bitnset('w', st->s_class)) 114 { 115 /* don't use fallback for this pass */ 116 fallbackMX = NULL; 117 } 118 firsttime = FALSE; 119 } 120 121 /* efficiency hack -- numeric or non-MX lookups */ 122 if (host[0] == '[') 123 goto punt; 124 125 /* 126 ** If we don't have MX records in our host switch, don't 127 ** try for MX records. Note that this really isn't "right", 128 ** since we might be set up to try NIS first and then DNS; 129 ** if the host is found in NIS we really shouldn't be doing 130 ** MX lookups. However, that should be a degenerate case. 131 */ 132 133 if (!mx_enabled()) 134 goto punt; 135 136 errno = 0; 137 n = res_search(host, C_IN, T_MX, (u_char *) &answer, sizeof(answer)); 138 if (n < 0) 139 { 140 if (tTd(8, 1)) 141 printf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n", 142 (host == NULL) ? "<NULL>" : host, errno, h_errno); 143 switch (h_errno) 144 { 145 case NO_DATA: 146 trycanon = TRUE; 147 /* fall through */ 148 149 case NO_RECOVERY: 150 /* no MX data on this host */ 151 goto punt; 152 153 case HOST_NOT_FOUND: 154 #ifdef BROKEN_RES_SEARCH 155 /* Ultrix resolver returns failure w/ h_errno=0 */ 156 case 0: 157 #endif 158 /* the host just doesn't exist */ 159 *rcode = EX_NOHOST; 160 161 if (!UseNameServer) 162 { 163 /* might exist in /etc/hosts */ 164 goto punt; 165 } 166 break; 167 168 case TRY_AGAIN: 169 /* couldn't connect to the name server */ 170 if (!UseNameServer && errno == ECONNREFUSED) 171 goto punt; 172 173 /* it might come up later; better queue it up */ 174 *rcode = EX_TEMPFAIL; 175 break; 176 177 default: 178 syserr("getmxrr: res_search (%s) failed with impossible h_errno (%d)\n", 179 host, h_errno); 180 *rcode = EX_OSERR; 181 break; 182 } 183 184 /* irreconcilable differences */ 185 return (-1); 186 } 187 188 /* find first satisfactory answer */ 189 hp = (HEADER *)&answer; 190 cp = (u_char *)&answer + HFIXEDSZ; 191 eom = (u_char *)&answer + n; 192 for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ) 193 if ((n = dn_skipname(cp, eom)) < 0) 194 goto punt; 195 buflen = sizeof(MXHostBuf) - 1; 196 bp = MXHostBuf; 197 ancount = ntohs(hp->ancount); 198 while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1) 199 { 200 if ((n = dn_expand((u_char *)&answer, 201 eom, cp, (RES_UNC_T) bp, buflen)) < 0) 202 break; 203 cp += n; 204 GETSHORT(type, cp); 205 cp += INT16SZ + INT32SZ; 206 GETSHORT(n, cp); 207 if (type != T_MX) 208 { 209 if (tTd(8, 8) || _res.options & RES_DEBUG) 210 printf("unexpected answer type %d, size %d\n", 211 type, n); 212 cp += n; 213 continue; 214 } 215 GETSHORT(pref, cp); 216 if ((n = dn_expand((u_char *)&answer, eom, cp, 217 (RES_UNC_T) bp, buflen)) < 0) 218 break; 219 cp += n; 220 if (droplocalhost && 221 (st = stab(bp, ST_CLASS, ST_FIND)) != NULL && 222 bitnset('w', st->s_class)) 223 { 224 if (tTd(8, 3)) 225 printf("found localhost (%s) in MX list, pref=%d\n", 226 bp, pref); 227 if (!seenlocal || pref < localpref) 228 localpref = pref; 229 seenlocal = TRUE; 230 continue; 231 } 232 weight[nmx] = mxrand(bp); 233 prefer[nmx] = pref; 234 mxhosts[nmx++] = bp; 235 n = strlen(bp); 236 bp += n; 237 if (bp[-1] != '.') 238 { 239 *bp++ = '.'; 240 n++; 241 } 242 *bp++ = '\0'; 243 buflen -= n + 1; 244 } 245 246 /* sort the records */ 247 for (i = 0; i < nmx; i++) 248 { 249 for (j = i + 1; j < nmx; j++) 250 { 251 if (prefer[i] > prefer[j] || 252 (prefer[i] == prefer[j] && weight[i] > weight[j])) 253 { 254 register int temp; 255 register char *temp1; 256 257 temp = prefer[i]; 258 prefer[i] = prefer[j]; 259 prefer[j] = temp; 260 temp1 = mxhosts[i]; 261 mxhosts[i] = mxhosts[j]; 262 mxhosts[j] = temp1; 263 temp = weight[i]; 264 weight[i] = weight[j]; 265 weight[j] = temp; 266 } 267 } 268 if (seenlocal && prefer[i] >= localpref) 269 { 270 /* truncate higher preference part of list */ 271 nmx = i; 272 } 273 } 274 275 if (nmx == 0) 276 { 277 punt: 278 if (seenlocal && 279 (!TryNullMXList || gethostbyname(host) == NULL)) 280 { 281 /* 282 ** If we have deleted all MX entries, this is 283 ** an error -- we should NEVER send to a host that 284 ** has an MX, and this should have been caught 285 ** earlier in the config file. 286 ** 287 ** Some sites prefer to go ahead and try the 288 ** A record anyway; that case is handled by 289 ** setting TryNullMXList. I believe this is a 290 ** bad idea, but it's up to you.... 291 */ 292 293 *rcode = EX_CONFIG; 294 syserr("MX list for %s points back to %s", 295 host, MyHostName); 296 return -1; 297 } 298 strcpy(MXHostBuf, host); 299 mxhosts[0] = MXHostBuf; 300 if (host[0] == '[') 301 { 302 register char *p; 303 304 /* this may be an MX suppression-style address */ 305 p = strchr(MXHostBuf, ']'); 306 if (p != NULL) 307 { 308 *p = '\0'; 309 if (inet_addr(&MXHostBuf[1]) != -1) 310 *p = ']'; 311 else 312 { 313 trycanon = TRUE; 314 mxhosts[0]++; 315 } 316 } 317 } 318 if (trycanon && 319 getcanonname(mxhosts[0], sizeof MXHostBuf - 2, FALSE)) 320 { 321 bp = &MXHostBuf[strlen(MXHostBuf)]; 322 if (bp[-1] != '.') 323 { 324 *bp++ = '.'; 325 *bp = '\0'; 326 } 327 } 328 nmx = 1; 329 } 330 331 /* if we have a default lowest preference, include that */ 332 if (fallbackMX != NULL && !seenlocal) 333 mxhosts[nmx++] = fallbackMX; 334 335 return (nmx); 336 } 337 /* 338 ** MXRAND -- create a randomizer for equal MX preferences 339 ** 340 ** If two MX hosts have equal preferences we want to randomize 341 ** the selection. But in order for signatures to be the same, 342 ** we need to randomize the same way each time. This function 343 ** computes a pseudo-random hash function from the host name. 344 ** 345 ** Parameters: 346 ** host -- the name of the host. 347 ** 348 ** Returns: 349 ** A random but repeatable value based on the host name. 350 ** 351 ** Side Effects: 352 ** none. 353 */ 354 355 mxrand(host) 356 register char *host; 357 { 358 int hfunc; 359 static unsigned int seed; 360 361 if (seed == 0) 362 { 363 seed = (int) curtime() & 0xffff; 364 if (seed == 0) 365 seed++; 366 } 367 368 if (tTd(17, 9)) 369 printf("mxrand(%s)", host); 370 371 hfunc = seed; 372 while (*host != '\0') 373 { 374 int c = *host++; 375 376 if (isascii(c) && isupper(c)) 377 c = tolower(c); 378 hfunc = ((hfunc << 1) ^ c) % 2003; 379 } 380 381 hfunc &= 0xff; 382 383 if (tTd(17, 9)) 384 printf(" = %d\n", hfunc); 385 return hfunc; 386 } 387 /* 388 ** MX_ENABLED -- check to see if MX records apply 389 ** 390 ** This is done by seeing if "dns" is listed in the hosts 391 ** service switch. 392 */ 393 394 bool 395 mx_enabled() 396 { 397 static bool firsttime = TRUE; 398 static bool hasmx; 399 char *maptype[MAXMAPSTACK]; 400 401 if (firsttime) 402 { 403 int nmaps; 404 405 nmaps = switch_map_find("hosts", maptype); 406 hasmx = FALSE; 407 if (nmaps > 0 && nmaps <= MAXMAPSTACK) 408 { 409 register int mapno; 410 411 for (mapno = 0; mapno < nmaps && !hasmx; mapno++) 412 { 413 if (strcmp(maptype[mapno], "dns") == 0) 414 hasmx = TRUE; 415 } 416 } 417 firsttime = FALSE; 418 } 419 return hasmx; 420 } 421 /* 422 ** GETCANONNAME -- get the canonical name for named host 423 ** 424 ** This algorithm tries to be smart about wildcard MX records. 425 ** This is hard to do because DNS doesn't tell is if we matched 426 ** against a wildcard or a specific MX. 427 ** 428 ** We always prefer A & CNAME records, since these are presumed 429 ** to be specific. 430 ** 431 ** If we match an MX in one pass and lose it in the next, we use 432 ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 433 ** A hostname bletch.foo.bar.com will match against this MX, but 434 ** will stop matching when we try bletch.bar.com -- so we know 435 ** that bletch.foo.bar.com must have been right. This fails if 436 ** there was also an MX record matching *.BAR.COM, but there are 437 ** some things that just can't be fixed. 438 ** 439 ** Parameters: 440 ** host -- a buffer containing the name of the host. 441 ** This is a value-result parameter. 442 ** hbsize -- the size of the host buffer. 443 ** trymx -- if set, try MX records as well as A and CNAME. 444 ** 445 ** Returns: 446 ** TRUE -- if the host matched. 447 ** FALSE -- otherwise. 448 */ 449 450 bool 451 getcanonname(host, hbsize, trymx) 452 char *host; 453 int hbsize; 454 bool trymx; 455 { 456 extern int h_errno; 457 register u_char *eom, *ap; 458 register char *cp; 459 register int n; 460 HEADER *hp; 461 querybuf answer; 462 int ancount, qdcount; 463 int ret; 464 char **domain; 465 int type; 466 char **dp; 467 char *mxmatch; 468 bool amatch; 469 bool gotmx; 470 int qtype; 471 int loopcnt; 472 char *xp; 473 char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 474 char *searchlist[MAXDNSRCH+2]; 475 extern char *gethostalias(); 476 477 if (tTd(8, 2)) 478 printf("getcanonname(%s)\n", host); 479 480 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 481 return (FALSE); 482 483 /* 484 ** Initialize domain search list. If there is at least one 485 ** dot in the name, search the unmodified name first so we 486 ** find "vse.CS" in Czechoslovakia instead of in the local 487 ** domain (e.g., vse.CS.Berkeley.EDU). 488 ** 489 ** Older versions of the resolver could create this 490 ** list by tearing apart the host name. 491 */ 492 493 loopcnt = 0; 494 cnameloop: 495 for (cp = host, n = 0; *cp; cp++) 496 if (*cp == '.') 497 n++; 498 499 if (n == 0 && (xp = gethostalias(host)) != NULL) 500 { 501 if (loopcnt++ > MAXCNAMEDEPTH) 502 { 503 syserr("loop in ${HOSTALIASES} file"); 504 } 505 else 506 { 507 strncpy(host, xp, hbsize); 508 host[hbsize - 1] = '\0'; 509 goto cnameloop; 510 } 511 } 512 513 dp = searchlist; 514 if (n > 0) 515 *dp++ = ""; 516 if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 517 { 518 for (domain = _res.dnsrch; *domain != NULL; ) 519 *dp++ = *domain++; 520 } 521 else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 522 { 523 *dp++ = _res.defdname; 524 } 525 else if (*cp == '.') 526 { 527 *cp = '\0'; 528 } 529 *dp = NULL; 530 531 /* 532 ** Now run through the search list for the name in question. 533 */ 534 535 mxmatch = NULL; 536 qtype = T_ANY; 537 538 for (dp = searchlist; *dp != NULL; ) 539 { 540 if (qtype == T_ANY) 541 gotmx = FALSE; 542 if (tTd(8, 5)) 543 printf("getcanonname: trying %s.%s (%s)\n", host, *dp, 544 qtype == T_ANY ? "ANY" : qtype == T_A ? "A" : 545 qtype == T_MX ? "MX" : "???"); 546 ret = res_querydomain(host, *dp, C_IN, qtype, 547 answer.qb2, sizeof(answer.qb2)); 548 if (ret <= 0) 549 { 550 if (tTd(8, 7)) 551 printf("\tNO: errno=%d, h_errno=%d\n", 552 errno, h_errno); 553 554 if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 555 { 556 /* the name server seems to be down */ 557 h_errno = TRY_AGAIN; 558 return FALSE; 559 } 560 561 if (h_errno != HOST_NOT_FOUND) 562 { 563 /* might have another type of interest */ 564 if (qtype == T_ANY) 565 { 566 qtype = T_A; 567 continue; 568 } 569 else if (qtype == T_A && !gotmx && trymx) 570 { 571 qtype = T_MX; 572 continue; 573 } 574 } 575 576 if (mxmatch != NULL) 577 { 578 /* we matched before -- use that one */ 579 break; 580 } 581 582 /* otherwise, try the next name */ 583 dp++; 584 qtype = T_ANY; 585 continue; 586 } 587 else if (tTd(8, 7)) 588 printf("\tYES\n"); 589 590 /* 591 ** This might be a bogus match. Search for A or 592 ** CNAME records. If we don't have a matching 593 ** wild card MX record, we will accept MX as well. 594 */ 595 596 hp = (HEADER *) &answer; 597 ap = (u_char *) &answer + HFIXEDSZ; 598 eom = (u_char *) &answer + ret; 599 600 /* skip question part of response -- we know what we asked */ 601 for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 602 { 603 if ((ret = dn_skipname(ap, eom)) < 0) 604 { 605 if (tTd(8, 20)) 606 printf("qdcount failure (%d)\n", 607 ntohs(hp->qdcount)); 608 return FALSE; /* ???XXX??? */ 609 } 610 } 611 612 amatch = FALSE; 613 for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 614 { 615 n = dn_expand((u_char *) &answer, eom, ap, 616 (RES_UNC_T) nbuf, sizeof nbuf); 617 if (n < 0) 618 break; 619 ap += n; 620 GETSHORT(type, ap); 621 ap += INT16SZ + INT32SZ; 622 GETSHORT(n, ap); 623 switch (type) 624 { 625 case T_MX: 626 gotmx = TRUE; 627 if (**dp != '\0') 628 { 629 /* got a match -- save that info */ 630 if (trymx && mxmatch == NULL) 631 mxmatch = *dp; 632 continue; 633 } 634 635 /* exact MX matches are as good as an A match */ 636 /* fall through */ 637 638 case T_A: 639 /* good show */ 640 amatch = TRUE; 641 642 /* continue in case a CNAME also exists */ 643 continue; 644 645 case T_CNAME: 646 if (loopcnt++ > MAXCNAMEDEPTH) 647 { 648 /*XXX should notify postmaster XXX*/ 649 message("DNS failure: CNAME loop for %s", 650 host); 651 if (CurEnv->e_message == NULL) 652 { 653 char ebuf[MAXLINE]; 654 655 sprintf(ebuf, "Deferred: DNS failure: CNAME loop for %s", 656 host); 657 CurEnv->e_message = newstr(ebuf); 658 } 659 h_errno = NO_RECOVERY; 660 return FALSE; 661 } 662 663 /* value points at name */ 664 if ((ret = dn_expand((u_char *)&answer, 665 eom, ap, (RES_UNC_T) nbuf, sizeof(nbuf))) < 0) 666 break; 667 (void)strncpy(host, nbuf, hbsize); /* XXX */ 668 host[hbsize - 1] = '\0'; 669 670 /* 671 ** RFC 1034 section 3.6 specifies that CNAME 672 ** should point at the canonical name -- but 673 ** urges software to try again anyway. 674 */ 675 676 goto cnameloop; 677 678 default: 679 /* not a record of interest */ 680 continue; 681 } 682 } 683 684 if (amatch) 685 { 686 /* got an A record and no CNAME */ 687 mxmatch = *dp; 688 break; 689 } 690 691 /* 692 ** If this was a T_ANY query, we may have the info but 693 ** need an explicit query. Try T_A, then T_MX. 694 */ 695 696 if (qtype == T_ANY) 697 qtype = T_A; 698 else if (qtype == T_A && !gotmx && trymx) 699 qtype = T_MX; 700 else 701 { 702 /* really nothing in this domain; try the next */ 703 qtype = T_ANY; 704 dp++; 705 } 706 } 707 708 if (mxmatch == NULL) 709 return FALSE; 710 711 /* create matching name and return */ 712 (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 713 *mxmatch == '\0' ? "" : ".", 714 MAXDNAME, mxmatch); 715 strncpy(host, nbuf, hbsize); 716 host[hbsize - 1] = '\0'; 717 return TRUE; 718 } 719 720 721 char * 722 gethostalias(host) 723 char *host; 724 { 725 char *fname; 726 FILE *fp; 727 register char *p; 728 char buf[MAXLINE]; 729 static char hbuf[MAXDNAME]; 730 731 fname = getenv("HOSTALIASES"); 732 if (fname == NULL || (fp = fopen(fname, "r")) == NULL) 733 return NULL; 734 while (fgets(buf, sizeof buf, fp) != NULL) 735 { 736 for (p = buf; p != '\0' && !(isascii(*p) && isspace(*p)); p++) 737 continue; 738 if (*p == 0) 739 { 740 /* syntax error */ 741 continue; 742 } 743 *p++ = '\0'; 744 if (strcasecmp(buf, host) == 0) 745 break; 746 } 747 748 if (feof(fp)) 749 { 750 /* no match */ 751 fclose(fp); 752 return NULL; 753 } 754 755 /* got a match; extract the equivalent name */ 756 while (*p != '\0' && isascii(*p) && isspace(*p)) 757 p++; 758 host = p; 759 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 760 p++; 761 *p = '\0'; 762 strncpy(hbuf, host, sizeof hbuf - 1); 763 hbuf[sizeof hbuf - 1] = '\0'; 764 return hbuf; 765 } 766 767 768 #else /* not NAMED_BIND */ 769 770 #include <netdb.h> 771 772 bool 773 getcanonname(host, hbsize, trymx) 774 char *host; 775 int hbsize; 776 bool trymx; 777 { 778 struct hostent *hp; 779 char *p; 780 781 hp = gethostbyname(host); 782 if (hp == NULL) 783 return (FALSE); 784 p = hp->h_name; 785 if (strchr(p, '.') == NULL) 786 { 787 /* first word is a short name -- try to find a long one */ 788 char **ap; 789 790 for (ap = hp->h_aliases; *ap != NULL; ap++) 791 if (strchr(*ap, '.') != NULL) 792 break; 793 if (*ap != NULL) 794 p = *ap; 795 } 796 797 if (strlen(p) >= hbsize) 798 return (FALSE); 799 800 (void) strcpy(host, p); 801 return (TRUE); 802 } 803 804 #endif /* not NAMED_BIND */ 805