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