129432Sbloom /* 234921Sbostic * Copyright (c) 1986 Eric P. Allman 333778Sbostic * Copyright (c) 1988 Regents of the University of California. 433778Sbostic * All rights reserved. 533778Sbostic * 642826Sbostic * %sccs.include.redist.c% 733778Sbostic */ 829432Sbloom 940961Sbostic #include "sendmail.h" 1035653Seric 1129432Sbloom #ifndef lint 1235653Seric #ifdef NAMED_BIND 13*58404Seric static char sccsid[] = "@(#)domain.c 6.8 (Berkeley) 03/03/93 (with name server)"; 1435653Seric #else 15*58404Seric static char sccsid[] = "@(#)domain.c 6.8 (Berkeley) 03/03/93 (without name server)"; 1635653Seric #endif 1733778Sbostic #endif /* not lint */ 1829432Sbloom 1935653Seric #ifdef NAMED_BIND 2035653Seric 2135653Seric #include <errno.h> 2233929Sbostic #include <arpa/nameser.h> 2333929Sbostic #include <resolv.h> 2433929Sbostic #include <netdb.h> 2529432Sbloom 2657454Seric typedef union 2757454Seric { 2857454Seric HEADER qb1; 2957454Seric char qb2[PACKETSZ]; 3029432Sbloom } querybuf; 3129432Sbloom 3257454Seric static char hostbuf[MAXMXHOSTS*PACKETSZ]; 3329432Sbloom 3457943Seric #ifndef MAXDNSRCH 3557943Seric #define MAXDNSRCH 6 /* number of possible domains to search */ 3657943Seric #endif 3757943Seric 3858010Seric #ifndef MAX 3958010Seric #define MAX(a, b) ((a) > (b) ? (a) : (b)) 4058010Seric #endif 4158010Seric 4258248Seric /* don't use sizeof because sizeof(long) is different on 64-bit machines */ 4358248Seric #define SHORTSIZE 2 /* size of a short (really, must be 2) */ 4458248Seric #define LONGSIZE 4 /* size of a long (really, must be 4) */ 4558248Seric /* 4658248Seric ** GETMXRR -- get MX resource records for a domain 4758248Seric ** 4858248Seric ** Parameters: 4958248Seric ** host -- the name of the host to MX. 5058248Seric ** mxhosts -- a pointer to a return buffer of MX records. 5158248Seric ** localhost -- the name of the local host. All MX records 5258248Seric ** less preferred than this one will be discarded. 5358248Seric ** rcode -- a pointer to an EX_ status code. 5458248Seric ** 5558248Seric ** Returns: 5658248Seric ** The number of MX records found. 5758248Seric ** -1 if there is an internal failure. 5858248Seric ** If no MX records are found, mxhosts[0] is set to host 5958248Seric ** and 1 is returned. 6058248Seric */ 6158248Seric 6233929Sbostic getmxrr(host, mxhosts, localhost, rcode) 6333929Sbostic char *host, **mxhosts, *localhost; 6433929Sbostic int *rcode; 6529432Sbloom { 6633929Sbostic extern int h_errno; 6733929Sbostic register u_char *eom, *cp; 6833929Sbostic register int i, j, n, nmx; 6933929Sbostic register char *bp; 7029432Sbloom HEADER *hp; 7133929Sbostic querybuf answer; 7233929Sbostic int ancount, qdcount, buflen, seenlocal; 7333929Sbostic u_short pref, localpref, type, prefer[MAXMXHOSTS]; 7457454Seric int weight[MAXMXHOSTS]; 7529432Sbloom 7636483Sbostic errno = 0; 7733929Sbostic n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer)); 7835653Seric if (n < 0) 7935653Seric { 8033929Sbostic if (tTd(8, 1)) 8152852Seric printf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n", 8252852Seric (host == NULL) ? "<NULL>" : host, errno, h_errno); 8335653Seric switch (h_errno) 8435653Seric { 8535653Seric case NO_DATA: 8635653Seric case NO_RECOVERY: 8735653Seric /* no MX data on this host */ 8833929Sbostic goto punt; 8935653Seric 9035653Seric case HOST_NOT_FOUND: 9135653Seric /* the host just doesn't exist */ 9233929Sbostic *rcode = EX_NOHOST; 9333929Sbostic break; 9435653Seric 9535653Seric case TRY_AGAIN: 9635653Seric /* couldn't connect to the name server */ 9735653Seric if (!UseNameServer && errno == ECONNREFUSED) 9835653Seric goto punt; 9935653Seric 10035653Seric /* it might come up later; better queue it up */ 10133929Sbostic *rcode = EX_TEMPFAIL; 10233929Sbostic break; 10329432Sbloom } 10435653Seric 10535653Seric /* irreconcilable differences */ 10635653Seric return (-1); 10729432Sbloom } 10833929Sbostic 10933929Sbostic /* find first satisfactory answer */ 11033929Sbostic hp = (HEADER *)&answer; 11133929Sbostic cp = (u_char *)&answer + sizeof(HEADER); 11233929Sbostic eom = (u_char *)&answer + n; 11333929Sbostic for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ) 11450957Skarels if ((n = dn_skipname(cp, eom)) < 0) 11533929Sbostic goto punt; 11629432Sbloom nmx = 0; 11729551Sbloom seenlocal = 0; 11856336Seric buflen = sizeof(hostbuf) - 1; 11933929Sbostic bp = hostbuf; 12033929Sbostic ancount = ntohs(hp->ancount); 12156336Seric while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS) 12256336Seric { 12346928Sbostic if ((n = dn_expand((u_char *)&answer, 12446928Sbostic eom, cp, (u_char *)bp, buflen)) < 0) 12529432Sbloom break; 12629432Sbloom cp += n; 12733929Sbostic GETSHORT(type, cp); 12858248Seric cp += SHORTSIZE + LONGSIZE; 12933929Sbostic GETSHORT(n, cp); 13056336Seric if (type != T_MX) 13156336Seric { 13257943Seric if (tTd(8, 8) || _res.options & RES_DEBUG) 13329432Sbloom printf("unexpected answer type %d, size %d\n", 13433929Sbostic type, n); 13529432Sbloom cp += n; 13629432Sbloom continue; 13729432Sbloom } 13833929Sbostic GETSHORT(pref, cp); 13956336Seric if ((n = dn_expand((u_char *)&answer, eom, cp, 14056336Seric (u_char *)bp, buflen)) < 0) 14129432Sbloom break; 14229551Sbloom cp += n; 14356336Seric if (!strcasecmp(bp, localhost)) 14456336Seric { 14533929Sbostic if (seenlocal == 0 || pref < localpref) 14633929Sbostic localpref = pref; 14729551Sbloom seenlocal = 1; 14829551Sbloom continue; 14929551Sbloom } 15057454Seric weight[nmx] = mxrand(bp); 15129432Sbloom prefer[nmx] = pref; 15229432Sbloom mxhosts[nmx++] = bp; 15356336Seric n = strlen(bp); 15433929Sbostic bp += n; 15556336Seric if (bp[-1] != '.') 15656336Seric { 15756336Seric *bp++ = '.'; 15856336Seric n++; 15956336Seric } 16056336Seric *bp++ = '\0'; 16156336Seric buflen -= n + 1; 16229432Sbloom } 16357454Seric if (nmx == 0) 16457454Seric { 16533929Sbostic punt: mxhosts[0] = strcpy(hostbuf, host); 16657454Seric return (1); 16729551Sbloom } 16833929Sbostic 16929432Sbloom /* sort the records */ 17057454Seric for (i = 0; i < nmx; i++) 17157454Seric { 17257454Seric for (j = i + 1; j < nmx; j++) 17357454Seric { 17436483Sbostic if (prefer[i] > prefer[j] || 17557454Seric (prefer[i] == prefer[j] && weight[i] > weight[j])) 17657454Seric { 17733929Sbostic register int temp; 17833929Sbostic register char *temp1; 17929432Sbloom 18029432Sbloom temp = prefer[i]; 18129432Sbloom prefer[i] = prefer[j]; 18229432Sbloom prefer[j] = temp; 18329432Sbloom temp1 = mxhosts[i]; 18429432Sbloom mxhosts[i] = mxhosts[j]; 18529432Sbloom mxhosts[j] = temp1; 18657454Seric temp = weight[i]; 18757454Seric weight[i] = weight[j]; 18857454Seric weight[j] = temp; 18929432Sbloom } 19029432Sbloom } 19157454Seric if (seenlocal && prefer[i] >= localpref) 19257454Seric { 19329551Sbloom /* 19433929Sbostic * truncate higher pref part of list; if we're 19533929Sbostic * the best choice left, we should have realized 19633929Sbostic * awhile ago that this was a local delivery. 19729551Sbloom */ 19857454Seric if (i == 0) 19957454Seric { 20033929Sbostic *rcode = EX_CONFIG; 20157454Seric return (-1); 20229551Sbloom } 20333929Sbostic nmx = i; 20429551Sbloom break; 20529551Sbloom } 20629432Sbloom } 20757454Seric return (nmx); 20829432Sbloom } 20957135Seric /* 21057454Seric ** MXRAND -- create a randomizer for equal MX preferences 21157454Seric ** 21257454Seric ** If two MX hosts have equal preferences we want to randomize 21357454Seric ** the selection. But in order for signatures to be the same, 21457454Seric ** we need to randomize the same way each time. This function 21557454Seric ** computes a pseudo-random hash function from the host name. 21657454Seric ** 21757454Seric ** Parameters: 21857454Seric ** host -- the name of the host. 21957454Seric ** 22057454Seric ** Returns: 22157454Seric ** A random but repeatable value based on the host name. 22257454Seric ** 22357454Seric ** Side Effects: 22457454Seric ** none. 22557454Seric */ 22657454Seric 22757454Seric mxrand(host) 22857454Seric register char *host; 22957454Seric { 23057454Seric int hfunc; 23157454Seric static unsigned int seed; 23257454Seric 23357454Seric if (seed == 0) 23457454Seric { 23557454Seric seed = (int) curtime() & 0xffff; 23657454Seric if (seed == 0) 23757454Seric seed++; 23857454Seric } 23957454Seric 24057454Seric if (tTd(17, 9)) 24157454Seric printf("mxrand(%s)", host); 24257454Seric 24357454Seric hfunc = seed; 24457454Seric while (*host != '\0') 24557454Seric { 24657454Seric int c = *host++; 24757454Seric 24857454Seric if (isascii(c) && isupper(c)) 24957454Seric c = tolower(c); 25057454Seric hfunc = ((hfunc << 1) + c) % 2003; 25157454Seric } 25257454Seric 25357454Seric hfunc &= 0xff; 25457454Seric 25557454Seric if (tTd(17, 9)) 25657454Seric printf(" = %d\n", hfunc); 25757454Seric return hfunc; 25857454Seric } 25957454Seric /* 26057135Seric ** GETCANONNAME -- get the canonical name for named host 26157135Seric ** 26257943Seric ** This algorithm tries to be smart about wildcard MX records. 26357943Seric ** This is hard to do because DNS doesn't tell is if we matched 26457943Seric ** against a wildcard or a specific MX. 26557943Seric ** 26657943Seric ** We always prefer A & CNAME records, since these are presumed 26757943Seric ** to be specific. 26857943Seric ** 26957943Seric ** If we match an MX in one pass and lose it in the next, we use 27057943Seric ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 27157943Seric ** A hostname bletch.foo.bar.com will match against this MX, but 27257943Seric ** will stop matching when we try bletch.bar.com -- so we know 27357943Seric ** that bletch.foo.bar.com must have been right. This fails if 27457943Seric ** there was also an MX record matching *.BAR.COM, but there are 27557943Seric ** some things that just can't be fixed. 27657943Seric ** 27757135Seric ** Parameters: 27857135Seric ** host -- a buffer containing the name of the host. 27957135Seric ** This is a value-result parameter. 28057135Seric ** hbsize -- the size of the host buffer. 28157135Seric ** 28257135Seric ** Returns: 28357135Seric ** TRUE -- if the host matched. 28457135Seric ** FALSE -- otherwise. 28557135Seric */ 28629653Sbloom 28751314Seric bool 28829653Sbloom getcanonname(host, hbsize) 28929653Sbloom char *host; 29029653Sbloom int hbsize; 29129653Sbloom { 29240277Sbostic extern int h_errno; 29351324Seric register u_char *eom, *ap; 29451324Seric register char *cp; 29533929Sbostic register int n; 29629653Sbloom HEADER *hp; 29733929Sbostic querybuf answer; 29857943Seric int first, ancount, qdcount; 29951324Seric int ret; 30051324Seric char **domain; 30151324Seric int type; 30257943Seric char **dp; 30357943Seric char *mxmatch; 30457943Seric bool amatch; 305*58404Seric int qtype; 30658010Seric char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 30758039Seric char *searchlist[MAXDNSRCH+2]; 30829653Sbloom 30951324Seric if (tTd(8, 2)) 31051324Seric printf("getcanonname(%s)\n", host); 31151324Seric 31251324Seric if ((_res.options & RES_INIT) == 0 && res_init() == -1) 31351324Seric return (FALSE); 31451324Seric 31551324Seric for (cp = host, n = 0; *cp; cp++) 31651324Seric if (*cp == '.') 31751324Seric n++; 31851324Seric 31936483Sbostic /* 32057943Seric ** Initialize domain search list. If there is at least one 32157943Seric ** dot in the name, search the unmodified name first so we 32257943Seric ** find "vse.CS" in Czechoslovakia instead of in the local 32357943Seric ** domain (e.g., vse.CS.Berkeley.EDU). 32457943Seric ** 32557943Seric ** Older versions of the resolver could create this 32657943Seric ** list by tearing apart the host name. 32757205Seric */ 32857205Seric 32957943Seric dp = searchlist; 33057943Seric if (n > 0) 33157943Seric *dp++ = ""; 33257943Seric if (n == 0 || n > 0 && *--cp != '.') 33351324Seric { 33457943Seric for (domain = _res.dnsrch; *domain != NULL; ) 33557943Seric *dp++ = *domain++; 33657205Seric } 33757943Seric *dp = NULL; 33857205Seric 33957205Seric /* 34057943Seric ** Now run through the search list for the name in question. 34157205Seric */ 34257205Seric 34357943Seric dp = searchlist; 34457943Seric mxmatch = NULL; 345*58404Seric qtype = T_ANY; 34657943Seric 347*58404Seric for (dp = searchlist; *dp != NULL; ) 34857205Seric { 34957943Seric if (tTd(8, 5)) 35058010Seric printf("getcanonname: trying %s.%s\n", host, *dp); 351*58404Seric ret = res_querydomain(host, *dp, C_IN, qtype, 35258010Seric &answer, sizeof(answer)); 35357943Seric if (ret <= 0) 35451324Seric { 35557943Seric if (tTd(8, 8)) 35658082Seric printf("\tNO: errno=%d, h_errno=%d\n", 35758082Seric errno, h_errno); 35851324Seric 35958082Seric if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 36057205Seric { 36157943Seric /* the name server seems to be down */ 36251324Seric h_errno = TRY_AGAIN; 36351910Seric return FALSE; 36451324Seric } 36557943Seric 366*58404Seric if (h_errno == HOST_NOT_FOUND) 367*58404Seric { 368*58404Seric /* definitely no data for this address */ 369*58404Seric dp++; 370*58404Seric qtype = T_ANY; /* just in case */ 371*58404Seric continue; 372*58404Seric } 373*58404Seric 37457943Seric if (mxmatch != NULL) 37551324Seric { 37657943Seric /* we matched before -- use that one */ 37751324Seric break; 37851324Seric } 37957943Seric continue; 38051324Seric } 381*58404Seric else if (tTd(8, 8)) 38257943Seric printf("\tYES\n"); 38357943Seric 38451910Seric /* 38557943Seric ** This might be a bogus match. Search for A or 38657943Seric ** CNAME records. If we don't have a matching 38757943Seric ** wild card MX record, we will accept MX as well. 38851910Seric */ 38951910Seric 39057943Seric hp = (HEADER *) &answer; 39157943Seric ap = (u_char *) &answer + sizeof(HEADER); 39257943Seric eom = (u_char *) &answer + ret; 39357943Seric 39457943Seric /* skip question part of response -- we know what we asked */ 39557943Seric for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 39651324Seric { 39757943Seric if ((ret = dn_skipname(ap, eom)) < 0) 39857943Seric { 39957943Seric if (tTd(8, 20)) 40057943Seric printf("qdcount failure (%d)\n", 40157943Seric ntohs(hp->qdcount)); 40257943Seric return FALSE; /* ???XXX??? */ 40357943Seric } 40451324Seric } 40557943Seric 40657943Seric amatch = FALSE; 40757943Seric for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 40851324Seric { 40957943Seric n = dn_expand((u_char *) &answer, eom, ap, 41057943Seric (u_char *) nbuf, sizeof nbuf); 41157943Seric if (n < 0) 41257943Seric break; 41357943Seric ap += n; 41457943Seric GETSHORT(type, ap); 41558248Seric ap += SHORTSIZE + LONGSIZE; 41657943Seric GETSHORT(n, ap); 41757943Seric switch (type) 41857943Seric { 41957943Seric case T_MX: 42057943Seric if (**dp != '\0') 42157943Seric { 42257943Seric /* got a match -- save that info */ 42357943Seric if (mxmatch == NULL) 42457943Seric mxmatch = *dp; 42557943Seric continue; 42657943Seric } 42733929Sbostic 42857943Seric /* exact MX matches are as good as an A match */ 42957943Seric /* fall through */ 43057205Seric 43157943Seric case T_A: 43257943Seric /* good show */ 43357943Seric amatch = TRUE; 43433929Sbostic 43557943Seric /* continue in case a CNAME also exists */ 43657943Seric continue; 43757943Seric 43857943Seric case T_CNAME: 43957943Seric /* value points at name */ 44057943Seric if ((ret = dn_expand((u_char *)&answer, 44157943Seric eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0) 44257943Seric break; 44357943Seric (void)strncpy(host, nbuf, hbsize); /* XXX */ 44457943Seric host[hbsize - 1] = '\0'; 44557943Seric return TRUE; 44657943Seric 44757943Seric default: 44857943Seric /* not a record of interest */ 44957943Seric continue; 45057943Seric } 45151324Seric } 45233929Sbostic 45357943Seric if (amatch) 45457943Seric { 45557943Seric /* got an A record and no CNAME */ 45657943Seric mxmatch = *dp; 45729653Sbloom break; 45829653Sbloom } 459*58404Seric 460*58404Seric /* 461*58404Seric ** If this was a T_ANY query, we may have the info but 462*58404Seric ** need an explicit query. Try T_A, then T_MX. 463*58404Seric */ 464*58404Seric 465*58404Seric if (qtype == T_ANY) 466*58404Seric qtype = T_A; 467*58404Seric else if (qtype = T_A) 468*58404Seric qtype = T_MX; 469*58404Seric else 470*58404Seric { 471*58404Seric /* really nothing in this domain; try the next */ 472*58404Seric qtype = T_ANY; 473*58404Seric dp++; 474*58404Seric } 47529653Sbloom } 47657943Seric 47757943Seric if (mxmatch == NULL) 47857943Seric return FALSE; 47957943Seric 48057943Seric /* create matching name and return */ 48157943Seric (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 48257943Seric *mxmatch == '\0' ? "" : ".", 48357943Seric MAXDNAME, mxmatch); 48457943Seric strncpy(host, nbuf, hbsize); 48557943Seric host[hbsize - 1] = '\0'; 48657943Seric return TRUE; 48729653Sbloom } 48835653Seric 48936494Sphil #else /* not NAMED_BIND */ 49036494Sphil 49136494Sphil #include <netdb.h> 49236494Sphil 49351314Seric bool 49436494Sphil getcanonname(host, hbsize) 49536494Sphil char *host; 49636494Sphil int hbsize; 49736494Sphil { 49836494Sphil struct hostent *hp; 49936494Sphil 50036494Sphil hp = gethostbyname(host); 50136494Sphil if (hp == NULL) 50251314Seric return (FALSE); 50336494Sphil 50436494Sphil if (strlen(hp->h_name) >= hbsize) 50551314Seric return (FALSE); 50636494Sphil 50736494Sphil (void) strcpy(host, hp->h_name); 50851314Seric return (TRUE); 50936494Sphil } 51036494Sphil 51136494Sphil #endif /* not NAMED_BIND */ 512