1 /* 2 * Copyright (c) 1986 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * 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 6.11 (Berkeley) 03/06/93 (with name server)"; 14 #else 15 static char sccsid[] = "@(#)domain.c 6.11 (Berkeley) 03/06/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 hostbuf[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 /* don't use sizeof because sizeof(long) is different on 64-bit machines */ 43 #define SHORTSIZE 2 /* size of a short (really, must be 2) */ 44 #define LONGSIZE 4 /* size of a long (really, must be 4) */ 45 /* 46 ** GETMXRR -- get MX resource records for a domain 47 ** 48 ** Parameters: 49 ** host -- the name of the host to MX. 50 ** mxhosts -- a pointer to a return buffer of MX records. 51 ** localhost -- the name of the local host. All MX records 52 ** less preferred than this one will be discarded. 53 ** rcode -- a pointer to an EX_ status code. 54 ** 55 ** Returns: 56 ** The number of MX records found. 57 ** -1 if there is an internal failure. 58 ** If no MX records are found, mxhosts[0] is set to host 59 ** and 1 is returned. 60 */ 61 62 getmxrr(host, mxhosts, localhost, rcode) 63 char *host, **mxhosts, *localhost; 64 int *rcode; 65 { 66 extern int h_errno; 67 register u_char *eom, *cp; 68 register int i, j, n, nmx; 69 register char *bp; 70 HEADER *hp; 71 querybuf answer; 72 int ancount, qdcount, buflen, seenlocal; 73 u_short pref, localpref, type, prefer[MAXMXHOSTS]; 74 int weight[MAXMXHOSTS]; 75 76 errno = 0; 77 n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer)); 78 if (n < 0) 79 { 80 if (tTd(8, 1)) 81 printf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n", 82 (host == NULL) ? "<NULL>" : host, errno, h_errno); 83 switch (h_errno) 84 { 85 case NO_DATA: 86 case NO_RECOVERY: 87 /* no MX data on this host */ 88 goto punt; 89 90 case HOST_NOT_FOUND: 91 /* the host just doesn't exist */ 92 *rcode = EX_NOHOST; 93 break; 94 95 case TRY_AGAIN: 96 /* couldn't connect to the name server */ 97 if (!UseNameServer && errno == ECONNREFUSED) 98 goto punt; 99 100 /* it might come up later; better queue it up */ 101 *rcode = EX_TEMPFAIL; 102 break; 103 } 104 105 /* irreconcilable differences */ 106 return (-1); 107 } 108 109 /* find first satisfactory answer */ 110 hp = (HEADER *)&answer; 111 cp = (u_char *)&answer + sizeof(HEADER); 112 eom = (u_char *)&answer + n; 113 for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ) 114 if ((n = dn_skipname(cp, eom)) < 0) 115 goto punt; 116 nmx = 0; 117 seenlocal = 0; 118 buflen = sizeof(hostbuf) - 1; 119 bp = hostbuf; 120 ancount = ntohs(hp->ancount); 121 while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS) 122 { 123 if ((n = dn_expand((u_char *)&answer, 124 eom, cp, (u_char *)bp, buflen)) < 0) 125 break; 126 cp += n; 127 GETSHORT(type, cp); 128 cp += SHORTSIZE + LONGSIZE; 129 GETSHORT(n, cp); 130 if (type != T_MX) 131 { 132 if (tTd(8, 8) || _res.options & RES_DEBUG) 133 printf("unexpected answer type %d, size %d\n", 134 type, n); 135 cp += n; 136 continue; 137 } 138 GETSHORT(pref, cp); 139 if ((n = dn_expand((u_char *)&answer, eom, cp, 140 (u_char *)bp, buflen)) < 0) 141 break; 142 cp += n; 143 if (!strcasecmp(bp, localhost)) 144 { 145 if (seenlocal == 0 || pref < localpref) 146 localpref = pref; 147 seenlocal = 1; 148 continue; 149 } 150 weight[nmx] = mxrand(bp); 151 prefer[nmx] = pref; 152 mxhosts[nmx++] = bp; 153 n = strlen(bp); 154 bp += n; 155 if (bp[-1] != '.') 156 { 157 *bp++ = '.'; 158 n++; 159 } 160 *bp++ = '\0'; 161 buflen -= n + 1; 162 } 163 if (nmx == 0) 164 { 165 punt: mxhosts[0] = strcpy(hostbuf, host); 166 return (1); 167 } 168 169 /* sort the records */ 170 for (i = 0; i < nmx; i++) 171 { 172 for (j = i + 1; j < nmx; j++) 173 { 174 if (prefer[i] > prefer[j] || 175 (prefer[i] == prefer[j] && weight[i] > weight[j])) 176 { 177 register int temp; 178 register char *temp1; 179 180 temp = prefer[i]; 181 prefer[i] = prefer[j]; 182 prefer[j] = temp; 183 temp1 = mxhosts[i]; 184 mxhosts[i] = mxhosts[j]; 185 mxhosts[j] = temp1; 186 temp = weight[i]; 187 weight[i] = weight[j]; 188 weight[j] = temp; 189 } 190 } 191 if (seenlocal && prefer[i] >= localpref) 192 { 193 /* 194 * truncate higher pref part of list; if we're 195 * the best choice left, we should have realized 196 * awhile ago that this was a local delivery. 197 */ 198 if (i == 0) 199 { 200 *rcode = EX_CONFIG; 201 return (-1); 202 } 203 nmx = i; 204 break; 205 } 206 } 207 return (nmx); 208 } 209 /* 210 ** MXRAND -- create a randomizer for equal MX preferences 211 ** 212 ** If two MX hosts have equal preferences we want to randomize 213 ** the selection. But in order for signatures to be the same, 214 ** we need to randomize the same way each time. This function 215 ** computes a pseudo-random hash function from the host name. 216 ** 217 ** Parameters: 218 ** host -- the name of the host. 219 ** 220 ** Returns: 221 ** A random but repeatable value based on the host name. 222 ** 223 ** Side Effects: 224 ** none. 225 */ 226 227 mxrand(host) 228 register char *host; 229 { 230 int hfunc; 231 static unsigned int seed; 232 233 if (seed == 0) 234 { 235 seed = (int) curtime() & 0xffff; 236 if (seed == 0) 237 seed++; 238 } 239 240 if (tTd(17, 9)) 241 printf("mxrand(%s)", host); 242 243 hfunc = seed; 244 while (*host != '\0') 245 { 246 int c = *host++; 247 248 if (isascii(c) && isupper(c)) 249 c = tolower(c); 250 hfunc = ((hfunc << 1) + c) % 2003; 251 } 252 253 hfunc &= 0xff; 254 255 if (tTd(17, 9)) 256 printf(" = %d\n", hfunc); 257 return hfunc; 258 } 259 /* 260 ** GETCANONNAME -- get the canonical name for named host 261 ** 262 ** This algorithm tries to be smart about wildcard MX records. 263 ** This is hard to do because DNS doesn't tell is if we matched 264 ** against a wildcard or a specific MX. 265 ** 266 ** We always prefer A & CNAME records, since these are presumed 267 ** to be specific. 268 ** 269 ** If we match an MX in one pass and lose it in the next, we use 270 ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 271 ** A hostname bletch.foo.bar.com will match against this MX, but 272 ** will stop matching when we try bletch.bar.com -- so we know 273 ** that bletch.foo.bar.com must have been right. This fails if 274 ** there was also an MX record matching *.BAR.COM, but there are 275 ** some things that just can't be fixed. 276 ** 277 ** Parameters: 278 ** host -- a buffer containing the name of the host. 279 ** This is a value-result parameter. 280 ** hbsize -- the size of the host buffer. 281 ** 282 ** Returns: 283 ** TRUE -- if the host matched. 284 ** FALSE -- otherwise. 285 */ 286 287 bool 288 getcanonname(host, hbsize) 289 char *host; 290 int hbsize; 291 { 292 extern int h_errno; 293 register u_char *eom, *ap; 294 register char *cp; 295 register int n; 296 HEADER *hp; 297 querybuf answer; 298 int first, ancount, qdcount; 299 int ret; 300 char **domain; 301 int type; 302 char **dp; 303 char *mxmatch; 304 bool amatch; 305 bool gotmx; 306 int qtype; 307 char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 308 char *searchlist[MAXDNSRCH+2]; 309 310 if (tTd(8, 2)) 311 printf("getcanonname(%s)\n", host); 312 313 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 314 return (FALSE); 315 316 for (cp = host, n = 0; *cp; cp++) 317 if (*cp == '.') 318 n++; 319 320 /* 321 ** Initialize domain search list. If there is at least one 322 ** dot in the name, search the unmodified name first so we 323 ** find "vse.CS" in Czechoslovakia instead of in the local 324 ** domain (e.g., vse.CS.Berkeley.EDU). 325 ** 326 ** Older versions of the resolver could create this 327 ** list by tearing apart the host name. 328 */ 329 330 dp = searchlist; 331 if (n > 0) 332 *dp++ = ""; 333 if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 334 { 335 for (domain = _res.dnsrch; *domain != NULL; ) 336 *dp++ = *domain++; 337 } 338 else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 339 { 340 *dp++ = _res.defdname; 341 } 342 *dp = NULL; 343 344 /* 345 ** Now run through the search list for the name in question. 346 */ 347 348 dp = searchlist; 349 mxmatch = NULL; 350 qtype = T_ANY; 351 352 for (dp = searchlist; *dp != NULL; ) 353 { 354 if (qtype == T_ANY) 355 gotmx = FALSE; 356 if (tTd(8, 5)) 357 printf("getcanonname: trying %s.%s (%s)\n", host, *dp, 358 qtype == T_ANY ? "ANY" : qtype == T_A ? "A" : 359 qtype == T_MX ? "MX" : "???"); 360 ret = res_querydomain(host, *dp, C_IN, qtype, 361 &answer, sizeof(answer)); 362 if (ret <= 0) 363 { 364 if (tTd(8, 8)) 365 printf("\tNO: errno=%d, h_errno=%d\n", 366 errno, h_errno); 367 368 if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 369 { 370 /* the name server seems to be down */ 371 h_errno = TRY_AGAIN; 372 return FALSE; 373 } 374 375 if (h_errno != HOST_NOT_FOUND) 376 { 377 /* might have another type of interest */ 378 if (qtype == T_ANY) 379 { 380 qtype = T_A; 381 continue; 382 } 383 else if (qtype == T_A && !gotmx) 384 { 385 qtype = T_MX; 386 continue; 387 } 388 } 389 390 if (mxmatch != NULL) 391 { 392 /* we matched before -- use that one */ 393 break; 394 } 395 396 /* otherwise, try the next name */ 397 dp++; 398 qtype = T_ANY; 399 continue; 400 } 401 else if (tTd(8, 8)) 402 printf("\tYES\n"); 403 404 /* 405 ** This might be a bogus match. Search for A or 406 ** CNAME records. If we don't have a matching 407 ** wild card MX record, we will accept MX as well. 408 */ 409 410 hp = (HEADER *) &answer; 411 ap = (u_char *) &answer + sizeof(HEADER); 412 eom = (u_char *) &answer + ret; 413 414 /* skip question part of response -- we know what we asked */ 415 for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 416 { 417 if ((ret = dn_skipname(ap, eom)) < 0) 418 { 419 if (tTd(8, 20)) 420 printf("qdcount failure (%d)\n", 421 ntohs(hp->qdcount)); 422 return FALSE; /* ???XXX??? */ 423 } 424 } 425 426 amatch = FALSE; 427 for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 428 { 429 n = dn_expand((u_char *) &answer, eom, ap, 430 (u_char *) nbuf, sizeof nbuf); 431 if (n < 0) 432 break; 433 ap += n; 434 GETSHORT(type, ap); 435 ap += SHORTSIZE + LONGSIZE; 436 GETSHORT(n, ap); 437 switch (type) 438 { 439 case T_MX: 440 gotmx = TRUE; 441 if (**dp != '\0') 442 { 443 /* got a match -- save that info */ 444 if (mxmatch == NULL) 445 mxmatch = *dp; 446 continue; 447 } 448 449 /* exact MX matches are as good as an A match */ 450 /* fall through */ 451 452 case T_A: 453 /* good show */ 454 amatch = TRUE; 455 456 /* continue in case a CNAME also exists */ 457 continue; 458 459 case T_CNAME: 460 /* value points at name */ 461 if ((ret = dn_expand((u_char *)&answer, 462 eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0) 463 break; 464 (void)strncpy(host, nbuf, hbsize); /* XXX */ 465 host[hbsize - 1] = '\0'; 466 return TRUE; 467 468 default: 469 /* not a record of interest */ 470 continue; 471 } 472 } 473 474 if (amatch) 475 { 476 /* got an A record and no CNAME */ 477 mxmatch = *dp; 478 break; 479 } 480 481 /* 482 ** If this was a T_ANY query, we may have the info but 483 ** need an explicit query. Try T_A, then T_MX. 484 */ 485 486 if (qtype == T_ANY) 487 qtype = T_A; 488 else if (qtype == T_A && !gotmx) 489 qtype = T_MX; 490 else 491 { 492 /* really nothing in this domain; try the next */ 493 qtype = T_ANY; 494 dp++; 495 } 496 } 497 498 if (mxmatch == NULL) 499 return FALSE; 500 501 /* create matching name and return */ 502 (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 503 *mxmatch == '\0' ? "" : ".", 504 MAXDNAME, mxmatch); 505 strncpy(host, nbuf, hbsize); 506 host[hbsize - 1] = '\0'; 507 return TRUE; 508 } 509 510 #else /* not NAMED_BIND */ 511 512 #include <netdb.h> 513 514 bool 515 getcanonname(host, hbsize) 516 char *host; 517 int hbsize; 518 { 519 struct hostent *hp; 520 521 hp = gethostbyname(host); 522 if (hp == NULL) 523 return (FALSE); 524 525 if (strlen(hp->h_name) >= hbsize) 526 return (FALSE); 527 528 (void) strcpy(host, hp->h_name); 529 return (TRUE); 530 } 531 532 #endif /* not NAMED_BIND */ 533