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*58796Seric static char sccsid[] = "@(#)domain.c 6.13 (Berkeley) 03/23/93 (with name server)"; 1435653Seric #else 15*58796Seric static char sccsid[] = "@(#)domain.c 6.13 (Berkeley) 03/23/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 { 16558668Seric punt: 16658668Seric mxhosts[0] = strcpy(hostbuf, host); 16758668Seric bp = &hostbuf[strlen(hostbuf)]; 16858668Seric if (bp[-1] != '.') 16958668Seric { 17058668Seric *bp++ = '.'; 17158668Seric *bp = '\0'; 17258668Seric } 17357454Seric return (1); 17429551Sbloom } 17533929Sbostic 17629432Sbloom /* sort the records */ 17757454Seric for (i = 0; i < nmx; i++) 17857454Seric { 17957454Seric for (j = i + 1; j < nmx; j++) 18057454Seric { 18136483Sbostic if (prefer[i] > prefer[j] || 18257454Seric (prefer[i] == prefer[j] && weight[i] > weight[j])) 18357454Seric { 18433929Sbostic register int temp; 18533929Sbostic register char *temp1; 18629432Sbloom 18729432Sbloom temp = prefer[i]; 18829432Sbloom prefer[i] = prefer[j]; 18929432Sbloom prefer[j] = temp; 19029432Sbloom temp1 = mxhosts[i]; 19129432Sbloom mxhosts[i] = mxhosts[j]; 19229432Sbloom mxhosts[j] = temp1; 19357454Seric temp = weight[i]; 19457454Seric weight[i] = weight[j]; 19557454Seric weight[j] = temp; 19629432Sbloom } 19729432Sbloom } 19857454Seric if (seenlocal && prefer[i] >= localpref) 19957454Seric { 20029551Sbloom /* 20133929Sbostic * truncate higher pref part of list; if we're 20233929Sbostic * the best choice left, we should have realized 20333929Sbostic * awhile ago that this was a local delivery. 20429551Sbloom */ 20557454Seric if (i == 0) 20657454Seric { 20733929Sbostic *rcode = EX_CONFIG; 20857454Seric return (-1); 20929551Sbloom } 21033929Sbostic nmx = i; 21129551Sbloom break; 21229551Sbloom } 21329432Sbloom } 21457454Seric return (nmx); 21529432Sbloom } 21657135Seric /* 21757454Seric ** MXRAND -- create a randomizer for equal MX preferences 21857454Seric ** 21957454Seric ** If two MX hosts have equal preferences we want to randomize 22057454Seric ** the selection. But in order for signatures to be the same, 22157454Seric ** we need to randomize the same way each time. This function 22257454Seric ** computes a pseudo-random hash function from the host name. 22357454Seric ** 22457454Seric ** Parameters: 22557454Seric ** host -- the name of the host. 22657454Seric ** 22757454Seric ** Returns: 22857454Seric ** A random but repeatable value based on the host name. 22957454Seric ** 23057454Seric ** Side Effects: 23157454Seric ** none. 23257454Seric */ 23357454Seric 23457454Seric mxrand(host) 23557454Seric register char *host; 23657454Seric { 23757454Seric int hfunc; 23857454Seric static unsigned int seed; 23957454Seric 24057454Seric if (seed == 0) 24157454Seric { 24257454Seric seed = (int) curtime() & 0xffff; 24357454Seric if (seed == 0) 24457454Seric seed++; 24557454Seric } 24657454Seric 24757454Seric if (tTd(17, 9)) 24857454Seric printf("mxrand(%s)", host); 24957454Seric 25057454Seric hfunc = seed; 25157454Seric while (*host != '\0') 25257454Seric { 25357454Seric int c = *host++; 25457454Seric 25557454Seric if (isascii(c) && isupper(c)) 25657454Seric c = tolower(c); 25757454Seric hfunc = ((hfunc << 1) + c) % 2003; 25857454Seric } 25957454Seric 26057454Seric hfunc &= 0xff; 26157454Seric 26257454Seric if (tTd(17, 9)) 26357454Seric printf(" = %d\n", hfunc); 26457454Seric return hfunc; 26557454Seric } 26657454Seric /* 26757135Seric ** GETCANONNAME -- get the canonical name for named host 26857135Seric ** 26957943Seric ** This algorithm tries to be smart about wildcard MX records. 27057943Seric ** This is hard to do because DNS doesn't tell is if we matched 27157943Seric ** against a wildcard or a specific MX. 27257943Seric ** 27357943Seric ** We always prefer A & CNAME records, since these are presumed 27457943Seric ** to be specific. 27557943Seric ** 27657943Seric ** If we match an MX in one pass and lose it in the next, we use 27757943Seric ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 27857943Seric ** A hostname bletch.foo.bar.com will match against this MX, but 27957943Seric ** will stop matching when we try bletch.bar.com -- so we know 28057943Seric ** that bletch.foo.bar.com must have been right. This fails if 28157943Seric ** there was also an MX record matching *.BAR.COM, but there are 28257943Seric ** some things that just can't be fixed. 28357943Seric ** 28457135Seric ** Parameters: 28557135Seric ** host -- a buffer containing the name of the host. 28657135Seric ** This is a value-result parameter. 28757135Seric ** hbsize -- the size of the host buffer. 28857135Seric ** 28957135Seric ** Returns: 29057135Seric ** TRUE -- if the host matched. 29157135Seric ** FALSE -- otherwise. 29257135Seric */ 29329653Sbloom 29451314Seric bool 29529653Sbloom getcanonname(host, hbsize) 29629653Sbloom char *host; 29729653Sbloom int hbsize; 29829653Sbloom { 29940277Sbostic extern int h_errno; 30051324Seric register u_char *eom, *ap; 30151324Seric register char *cp; 30233929Sbostic register int n; 30329653Sbloom HEADER *hp; 30433929Sbostic querybuf answer; 30557943Seric int first, ancount, qdcount; 30651324Seric int ret; 30751324Seric char **domain; 30851324Seric int type; 30957943Seric char **dp; 31057943Seric char *mxmatch; 31157943Seric bool amatch; 31258411Seric bool gotmx; 31358404Seric int qtype; 31458010Seric char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 31558039Seric char *searchlist[MAXDNSRCH+2]; 31629653Sbloom 31751324Seric if (tTd(8, 2)) 31851324Seric printf("getcanonname(%s)\n", host); 31951324Seric 32051324Seric if ((_res.options & RES_INIT) == 0 && res_init() == -1) 32151324Seric return (FALSE); 32251324Seric 32351324Seric for (cp = host, n = 0; *cp; cp++) 32451324Seric if (*cp == '.') 32551324Seric n++; 32651324Seric 32736483Sbostic /* 32857943Seric ** Initialize domain search list. If there is at least one 32957943Seric ** dot in the name, search the unmodified name first so we 33057943Seric ** find "vse.CS" in Czechoslovakia instead of in the local 33157943Seric ** domain (e.g., vse.CS.Berkeley.EDU). 33257943Seric ** 33357943Seric ** Older versions of the resolver could create this 33457943Seric ** list by tearing apart the host name. 33557205Seric */ 33657205Seric 33757943Seric dp = searchlist; 33857943Seric if (n > 0) 33957943Seric *dp++ = ""; 34058411Seric if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 34151324Seric { 34257943Seric for (domain = _res.dnsrch; *domain != NULL; ) 34357943Seric *dp++ = *domain++; 34457205Seric } 34558411Seric else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 34658411Seric { 34758411Seric *dp++ = _res.defdname; 34858411Seric } 34957943Seric *dp = NULL; 35057205Seric 35157205Seric /* 35257943Seric ** Now run through the search list for the name in question. 35357205Seric */ 35457205Seric 35557943Seric dp = searchlist; 35657943Seric mxmatch = NULL; 35758404Seric qtype = T_ANY; 35857943Seric 35958404Seric for (dp = searchlist; *dp != NULL; ) 36057205Seric { 36158411Seric if (qtype == T_ANY) 36258411Seric gotmx = FALSE; 36357943Seric if (tTd(8, 5)) 36458508Seric printf("getcanonname: trying %s.%s (%s)\n", host, *dp, 36558508Seric qtype == T_ANY ? "ANY" : qtype == T_A ? "A" : 36658508Seric qtype == T_MX ? "MX" : "???"); 36758404Seric ret = res_querydomain(host, *dp, C_IN, qtype, 36858010Seric &answer, sizeof(answer)); 36957943Seric if (ret <= 0) 37051324Seric { 371*58796Seric if (tTd(8, 7)) 37258082Seric printf("\tNO: errno=%d, h_errno=%d\n", 37358082Seric errno, h_errno); 37451324Seric 37558082Seric if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 37657205Seric { 37757943Seric /* the name server seems to be down */ 37851324Seric h_errno = TRY_AGAIN; 37951910Seric return FALSE; 38051324Seric } 38157943Seric 38258501Seric if (h_errno != HOST_NOT_FOUND) 38358404Seric { 38458501Seric /* might have another type of interest */ 38558501Seric if (qtype == T_ANY) 38658501Seric { 38758501Seric qtype = T_A; 38858501Seric continue; 38958501Seric } 39058501Seric else if (qtype == T_A && !gotmx) 39158501Seric { 39258501Seric qtype = T_MX; 39358501Seric continue; 39458501Seric } 39558404Seric } 39658404Seric 39757943Seric if (mxmatch != NULL) 39851324Seric { 39957943Seric /* we matched before -- use that one */ 40051324Seric break; 40151324Seric } 40258501Seric 40358501Seric /* otherwise, try the next name */ 40458501Seric dp++; 40558501Seric qtype = T_ANY; 40657943Seric continue; 40751324Seric } 408*58796Seric else if (tTd(8, 7)) 40957943Seric printf("\tYES\n"); 41057943Seric 41151910Seric /* 41257943Seric ** This might be a bogus match. Search for A or 41357943Seric ** CNAME records. If we don't have a matching 41457943Seric ** wild card MX record, we will accept MX as well. 41551910Seric */ 41651910Seric 41757943Seric hp = (HEADER *) &answer; 41857943Seric ap = (u_char *) &answer + sizeof(HEADER); 41957943Seric eom = (u_char *) &answer + ret; 42057943Seric 42157943Seric /* skip question part of response -- we know what we asked */ 42257943Seric for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 42351324Seric { 42457943Seric if ((ret = dn_skipname(ap, eom)) < 0) 42557943Seric { 42657943Seric if (tTd(8, 20)) 42757943Seric printf("qdcount failure (%d)\n", 42857943Seric ntohs(hp->qdcount)); 42957943Seric return FALSE; /* ???XXX??? */ 43057943Seric } 43151324Seric } 43257943Seric 43357943Seric amatch = FALSE; 43457943Seric for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 43551324Seric { 43657943Seric n = dn_expand((u_char *) &answer, eom, ap, 43757943Seric (u_char *) nbuf, sizeof nbuf); 43857943Seric if (n < 0) 43957943Seric break; 44057943Seric ap += n; 44157943Seric GETSHORT(type, ap); 44258248Seric ap += SHORTSIZE + LONGSIZE; 44357943Seric GETSHORT(n, ap); 44457943Seric switch (type) 44557943Seric { 44657943Seric case T_MX: 44758411Seric gotmx = TRUE; 44857943Seric if (**dp != '\0') 44957943Seric { 45057943Seric /* got a match -- save that info */ 45157943Seric if (mxmatch == NULL) 45257943Seric mxmatch = *dp; 45357943Seric continue; 45457943Seric } 45533929Sbostic 45657943Seric /* exact MX matches are as good as an A match */ 45757943Seric /* fall through */ 45857205Seric 45957943Seric case T_A: 46057943Seric /* good show */ 46157943Seric amatch = TRUE; 46233929Sbostic 46357943Seric /* continue in case a CNAME also exists */ 46457943Seric continue; 46557943Seric 46657943Seric case T_CNAME: 46757943Seric /* value points at name */ 46857943Seric if ((ret = dn_expand((u_char *)&answer, 46957943Seric eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0) 47057943Seric break; 47157943Seric (void)strncpy(host, nbuf, hbsize); /* XXX */ 47257943Seric host[hbsize - 1] = '\0'; 47357943Seric return TRUE; 47457943Seric 47557943Seric default: 47657943Seric /* not a record of interest */ 47757943Seric continue; 47857943Seric } 47951324Seric } 48033929Sbostic 48157943Seric if (amatch) 48257943Seric { 48357943Seric /* got an A record and no CNAME */ 48457943Seric mxmatch = *dp; 48529653Sbloom break; 48629653Sbloom } 48758404Seric 48858404Seric /* 48958404Seric ** If this was a T_ANY query, we may have the info but 49058404Seric ** need an explicit query. Try T_A, then T_MX. 49158404Seric */ 49258404Seric 49358404Seric if (qtype == T_ANY) 49458404Seric qtype = T_A; 49558411Seric else if (qtype == T_A && !gotmx) 49658404Seric qtype = T_MX; 49758404Seric else 49858404Seric { 49958404Seric /* really nothing in this domain; try the next */ 50058404Seric qtype = T_ANY; 50158404Seric dp++; 50258404Seric } 50329653Sbloom } 50457943Seric 50557943Seric if (mxmatch == NULL) 50657943Seric return FALSE; 50757943Seric 50857943Seric /* create matching name and return */ 50957943Seric (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 51057943Seric *mxmatch == '\0' ? "" : ".", 51157943Seric MAXDNAME, mxmatch); 51257943Seric strncpy(host, nbuf, hbsize); 51357943Seric host[hbsize - 1] = '\0'; 51457943Seric return TRUE; 51529653Sbloom } 51635653Seric 51736494Sphil #else /* not NAMED_BIND */ 51836494Sphil 51936494Sphil #include <netdb.h> 52036494Sphil 52151314Seric bool 52236494Sphil getcanonname(host, hbsize) 52336494Sphil char *host; 52436494Sphil int hbsize; 52536494Sphil { 52636494Sphil struct hostent *hp; 52736494Sphil 52836494Sphil hp = gethostbyname(host); 52936494Sphil if (hp == NULL) 53051314Seric return (FALSE); 53136494Sphil 53236494Sphil if (strlen(hp->h_name) >= hbsize) 53351314Seric return (FALSE); 53436494Sphil 53536494Sphil (void) strcpy(host, hp->h_name); 53651314Seric return (TRUE); 53736494Sphil } 53836494Sphil 53936494Sphil #endif /* not NAMED_BIND */ 540