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.27 (Berkeley) 10/24/94 (with name server)"; 14 #else 15 static char sccsid[] = "@(#)domain.c 8.27 (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 short mapreturn[3]; 405 406 nmaps = switch_map_find("hosts", maptype, mapreturn); 407 hasmx = FALSE; 408 if (nmaps > 0 && nmaps <= MAXMAPSTACK) 409 { 410 register int mapno; 411 412 for (mapno = 0; mapno < nmaps && !hasmx; mapno++) 413 { 414 if (strcmp(maptype[mapno], "dns") == 0) 415 hasmx = TRUE; 416 } 417 } 418 firsttime = FALSE; 419 } 420 return hasmx; 421 } 422 /* 423 ** GETCANONNAME -- get the canonical name for named host 424 ** 425 ** This algorithm tries to be smart about wildcard MX records. 426 ** This is hard to do because DNS doesn't tell is if we matched 427 ** against a wildcard or a specific MX. 428 ** 429 ** We always prefer A & CNAME records, since these are presumed 430 ** to be specific. 431 ** 432 ** If we match an MX in one pass and lose it in the next, we use 433 ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 434 ** A hostname bletch.foo.bar.com will match against this MX, but 435 ** will stop matching when we try bletch.bar.com -- so we know 436 ** that bletch.foo.bar.com must have been right. This fails if 437 ** there was also an MX record matching *.BAR.COM, but there are 438 ** some things that just can't be fixed. 439 ** 440 ** Parameters: 441 ** host -- a buffer containing the name of the host. 442 ** This is a value-result parameter. 443 ** hbsize -- the size of the host buffer. 444 ** trymx -- if set, try MX records as well as A and CNAME. 445 ** 446 ** Returns: 447 ** TRUE -- if the host matched. 448 ** FALSE -- otherwise. 449 */ 450 451 bool 452 getcanonname(host, hbsize, trymx) 453 char *host; 454 int hbsize; 455 bool trymx; 456 { 457 extern int h_errno; 458 register u_char *eom, *ap; 459 register char *cp; 460 register int n; 461 HEADER *hp; 462 querybuf answer; 463 int ancount, qdcount; 464 int ret; 465 char **domain; 466 int type; 467 char **dp; 468 char *mxmatch; 469 bool amatch; 470 bool gotmx; 471 int qtype; 472 int loopcnt; 473 char *xp; 474 char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 475 char *searchlist[MAXDNSRCH+2]; 476 extern char *gethostalias(); 477 478 if (tTd(8, 2)) 479 printf("getcanonname(%s)\n", host); 480 481 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 482 return (FALSE); 483 484 /* 485 ** Initialize domain search list. If there is at least one 486 ** dot in the name, search the unmodified name first so we 487 ** find "vse.CS" in Czechoslovakia instead of in the local 488 ** domain (e.g., vse.CS.Berkeley.EDU). 489 ** 490 ** Older versions of the resolver could create this 491 ** list by tearing apart the host name. 492 */ 493 494 loopcnt = 0; 495 cnameloop: 496 for (cp = host, n = 0; *cp; cp++) 497 if (*cp == '.') 498 n++; 499 500 if (n == 0 && (xp = gethostalias(host)) != NULL) 501 { 502 if (loopcnt++ > MAXCNAMEDEPTH) 503 { 504 syserr("loop in ${HOSTALIASES} file"); 505 } 506 else 507 { 508 strncpy(host, xp, hbsize); 509 host[hbsize - 1] = '\0'; 510 goto cnameloop; 511 } 512 } 513 514 dp = searchlist; 515 if (n > 0) 516 *dp++ = ""; 517 if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 518 { 519 for (domain = _res.dnsrch; *domain != NULL; ) 520 *dp++ = *domain++; 521 } 522 else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 523 { 524 *dp++ = _res.defdname; 525 } 526 else if (*cp == '.') 527 { 528 *cp = '\0'; 529 } 530 *dp = NULL; 531 532 /* 533 ** Now run through the search list for the name in question. 534 */ 535 536 mxmatch = NULL; 537 qtype = T_ANY; 538 539 for (dp = searchlist; *dp != NULL; ) 540 { 541 if (qtype == T_ANY) 542 gotmx = FALSE; 543 if (tTd(8, 5)) 544 printf("getcanonname: trying %s.%s (%s)\n", host, *dp, 545 qtype == T_ANY ? "ANY" : qtype == T_A ? "A" : 546 qtype == T_MX ? "MX" : "???"); 547 ret = res_querydomain(host, *dp, C_IN, qtype, 548 answer.qb2, sizeof(answer.qb2)); 549 if (ret <= 0) 550 { 551 if (tTd(8, 7)) 552 printf("\tNO: errno=%d, h_errno=%d\n", 553 errno, h_errno); 554 555 if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 556 { 557 /* the name server seems to be down */ 558 h_errno = TRY_AGAIN; 559 return FALSE; 560 } 561 562 if (h_errno != HOST_NOT_FOUND) 563 { 564 /* might have another type of interest */ 565 if (qtype == T_ANY) 566 { 567 qtype = T_A; 568 continue; 569 } 570 else if (qtype == T_A && !gotmx && trymx) 571 { 572 qtype = T_MX; 573 continue; 574 } 575 } 576 577 if (mxmatch != NULL) 578 { 579 /* we matched before -- use that one */ 580 break; 581 } 582 583 /* otherwise, try the next name */ 584 dp++; 585 qtype = T_ANY; 586 continue; 587 } 588 else if (tTd(8, 7)) 589 printf("\tYES\n"); 590 591 /* 592 ** This might be a bogus match. Search for A or 593 ** CNAME records. If we don't have a matching 594 ** wild card MX record, we will accept MX as well. 595 */ 596 597 hp = (HEADER *) &answer; 598 ap = (u_char *) &answer + HFIXEDSZ; 599 eom = (u_char *) &answer + ret; 600 601 /* skip question part of response -- we know what we asked */ 602 for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 603 { 604 if ((ret = dn_skipname(ap, eom)) < 0) 605 { 606 if (tTd(8, 20)) 607 printf("qdcount failure (%d)\n", 608 ntohs(hp->qdcount)); 609 return FALSE; /* ???XXX??? */ 610 } 611 } 612 613 amatch = FALSE; 614 for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 615 { 616 n = dn_expand((u_char *) &answer, eom, ap, 617 (RES_UNC_T) nbuf, sizeof nbuf); 618 if (n < 0) 619 break; 620 ap += n; 621 GETSHORT(type, ap); 622 ap += INT16SZ + INT32SZ; 623 GETSHORT(n, ap); 624 switch (type) 625 { 626 case T_MX: 627 gotmx = TRUE; 628 if (**dp != '\0') 629 { 630 /* got a match -- save that info */ 631 if (trymx && mxmatch == NULL) 632 mxmatch = *dp; 633 continue; 634 } 635 636 /* exact MX matches are as good as an A match */ 637 /* fall through */ 638 639 case T_A: 640 /* good show */ 641 amatch = TRUE; 642 643 /* continue in case a CNAME also exists */ 644 continue; 645 646 case T_CNAME: 647 if (loopcnt++ > MAXCNAMEDEPTH) 648 { 649 /*XXX should notify postmaster XXX*/ 650 message("DNS failure: CNAME loop for %s", 651 host); 652 if (CurEnv->e_message == NULL) 653 { 654 char ebuf[MAXLINE]; 655 656 sprintf(ebuf, "Deferred: DNS failure: CNAME loop for %s", 657 host); 658 CurEnv->e_message = newstr(ebuf); 659 } 660 h_errno = NO_RECOVERY; 661 return FALSE; 662 } 663 664 /* value points at name */ 665 if ((ret = dn_expand((u_char *)&answer, 666 eom, ap, (RES_UNC_T) nbuf, sizeof(nbuf))) < 0) 667 break; 668 (void)strncpy(host, nbuf, hbsize); /* XXX */ 669 host[hbsize - 1] = '\0'; 670 671 /* 672 ** RFC 1034 section 3.6 specifies that CNAME 673 ** should point at the canonical name -- but 674 ** urges software to try again anyway. 675 */ 676 677 goto cnameloop; 678 679 default: 680 /* not a record of interest */ 681 continue; 682 } 683 } 684 685 if (amatch) 686 { 687 /* got an A record and no CNAME */ 688 mxmatch = *dp; 689 break; 690 } 691 692 /* 693 ** If this was a T_ANY query, we may have the info but 694 ** need an explicit query. Try T_A, then T_MX. 695 */ 696 697 if (qtype == T_ANY) 698 qtype = T_A; 699 else if (qtype == T_A && !gotmx && trymx) 700 qtype = T_MX; 701 else 702 { 703 /* really nothing in this domain; try the next */ 704 qtype = T_ANY; 705 dp++; 706 } 707 } 708 709 if (mxmatch == NULL) 710 return FALSE; 711 712 /* create matching name and return */ 713 (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 714 *mxmatch == '\0' ? "" : ".", 715 MAXDNAME, mxmatch); 716 strncpy(host, nbuf, hbsize); 717 host[hbsize - 1] = '\0'; 718 return TRUE; 719 } 720 721 722 char * 723 gethostalias(host) 724 char *host; 725 { 726 char *fname; 727 FILE *fp; 728 register char *p; 729 char buf[MAXLINE]; 730 static char hbuf[MAXDNAME]; 731 732 fname = getenv("HOSTALIASES"); 733 if (fname == NULL || (fp = fopen(fname, "r")) == NULL) 734 return NULL; 735 while (fgets(buf, sizeof buf, fp) != NULL) 736 { 737 for (p = buf; p != '\0' && !(isascii(*p) && isspace(*p)); p++) 738 continue; 739 if (*p == 0) 740 { 741 /* syntax error */ 742 continue; 743 } 744 *p++ = '\0'; 745 if (strcasecmp(buf, host) == 0) 746 break; 747 } 748 749 if (feof(fp)) 750 { 751 /* no match */ 752 fclose(fp); 753 return NULL; 754 } 755 756 /* got a match; extract the equivalent name */ 757 while (*p != '\0' && isascii(*p) && isspace(*p)) 758 p++; 759 host = p; 760 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 761 p++; 762 *p = '\0'; 763 strncpy(hbuf, host, sizeof hbuf - 1); 764 hbuf[sizeof hbuf - 1] = '\0'; 765 return hbuf; 766 } 767 768 769 #else /* not NAMED_BIND */ 770 771 #include <netdb.h> 772 773 bool 774 getcanonname(host, hbsize, trymx) 775 char *host; 776 int hbsize; 777 bool trymx; 778 { 779 struct hostent *hp; 780 char *p; 781 782 hp = gethostbyname(host); 783 if (hp == NULL) 784 return (FALSE); 785 p = hp->h_name; 786 if (strchr(p, '.') == NULL) 787 { 788 /* first word is a short name -- try to find a long one */ 789 char **ap; 790 791 for (ap = hp->h_aliases; *ap != NULL; ap++) 792 if (strchr(*ap, '.') != NULL) 793 break; 794 if (*ap != NULL) 795 p = *ap; 796 } 797 798 if (strlen(p) >= hbsize) 799 return (FALSE); 800 801 (void) strcpy(host, p); 802 return (TRUE); 803 } 804 805 #endif /* not NAMED_BIND */ 806