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.7 (Berkeley) 02/26/93 (with name server)"; 14 #else 15 static char sccsid[] = "@(#)domain.c 6.7 (Berkeley) 02/26/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 char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 306 char *searchlist[MAXDNSRCH+2]; 307 308 if (tTd(8, 2)) 309 printf("getcanonname(%s)\n", host); 310 311 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 312 return (FALSE); 313 314 for (cp = host, n = 0; *cp; cp++) 315 if (*cp == '.') 316 n++; 317 318 /* 319 ** Initialize domain search list. If there is at least one 320 ** dot in the name, search the unmodified name first so we 321 ** find "vse.CS" in Czechoslovakia instead of in the local 322 ** domain (e.g., vse.CS.Berkeley.EDU). 323 ** 324 ** Older versions of the resolver could create this 325 ** list by tearing apart the host name. 326 */ 327 328 dp = searchlist; 329 if (n > 0) 330 *dp++ = ""; 331 if (n == 0 || n > 0 && *--cp != '.') 332 { 333 for (domain = _res.dnsrch; *domain != NULL; ) 334 *dp++ = *domain++; 335 } 336 *dp = NULL; 337 338 /* 339 ** Now run through the search list for the name in question. 340 */ 341 342 dp = searchlist; 343 mxmatch = NULL; 344 345 for (dp = searchlist; *dp != NULL; dp++) 346 { 347 if (tTd(8, 5)) 348 printf("getcanonname: trying %s.%s\n", host, *dp); 349 ret = res_querydomain(host, *dp, C_IN, T_ANY, 350 &answer, sizeof(answer)); 351 if (ret <= 0) 352 { 353 if (tTd(8, 8)) 354 printf("\tNO: errno=%d, h_errno=%d\n", 355 errno, h_errno); 356 357 if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 358 { 359 /* the name server seems to be down */ 360 h_errno = TRY_AGAIN; 361 return FALSE; 362 } 363 364 if (mxmatch != NULL) 365 { 366 /* we matched before -- use that one */ 367 break; 368 } 369 continue; 370 } 371 if (tTd(8, 8)) 372 printf("\tYES\n"); 373 374 /* 375 ** This might be a bogus match. Search for A or 376 ** CNAME records. If we don't have a matching 377 ** wild card MX record, we will accept MX as well. 378 */ 379 380 hp = (HEADER *) &answer; 381 ap = (u_char *) &answer + sizeof(HEADER); 382 eom = (u_char *) &answer + ret; 383 384 /* skip question part of response -- we know what we asked */ 385 for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 386 { 387 if ((ret = dn_skipname(ap, eom)) < 0) 388 { 389 if (tTd(8, 20)) 390 printf("qdcount failure (%d)\n", 391 ntohs(hp->qdcount)); 392 return FALSE; /* ???XXX??? */ 393 } 394 } 395 396 amatch = FALSE; 397 for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 398 { 399 n = dn_expand((u_char *) &answer, eom, ap, 400 (u_char *) nbuf, sizeof nbuf); 401 if (n < 0) 402 break; 403 ap += n; 404 GETSHORT(type, ap); 405 ap += SHORTSIZE + LONGSIZE; 406 GETSHORT(n, ap); 407 switch (type) 408 { 409 case T_MX: 410 if (**dp != '\0') 411 { 412 /* got a match -- save that info */ 413 if (mxmatch == NULL) 414 mxmatch = *dp; 415 continue; 416 } 417 418 /* exact MX matches are as good as an A match */ 419 /* fall through */ 420 421 case T_A: 422 /* good show */ 423 amatch = TRUE; 424 425 /* continue in case a CNAME also exists */ 426 continue; 427 428 case T_CNAME: 429 /* value points at name */ 430 if ((ret = dn_expand((u_char *)&answer, 431 eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0) 432 break; 433 (void)strncpy(host, nbuf, hbsize); /* XXX */ 434 host[hbsize - 1] = '\0'; 435 return TRUE; 436 437 default: 438 /* not a record of interest */ 439 continue; 440 } 441 } 442 443 if (amatch) 444 { 445 /* got an A record and no CNAME */ 446 mxmatch = *dp; 447 break; 448 } 449 } 450 451 if (mxmatch == NULL) 452 return FALSE; 453 454 /* create matching name and return */ 455 (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 456 *mxmatch == '\0' ? "" : ".", 457 MAXDNAME, mxmatch); 458 strncpy(host, nbuf, hbsize); 459 host[hbsize - 1] = '\0'; 460 return TRUE; 461 } 462 463 #else /* not NAMED_BIND */ 464 465 #include <netdb.h> 466 467 bool 468 getcanonname(host, hbsize) 469 char *host; 470 int hbsize; 471 { 472 struct hostent *hp; 473 474 hp = gethostbyname(host); 475 if (hp == NULL) 476 return (FALSE); 477 478 if (strlen(hp->h_name) >= hbsize) 479 return (FALSE); 480 481 (void) strcpy(host, hp->h_name); 482 return (TRUE); 483 } 484 485 #endif /* not NAMED_BIND */ 486