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