129432Sbloom /* 234921Sbostic * Copyright (c) 1986 Eric P. Allman 362524Sbostic * Copyright (c) 1988, 1993 462524Sbostic * The Regents of the University of California. All rights reserved. 533778Sbostic * 642826Sbostic * %sccs.include.redist.c% 733778Sbostic */ 829432Sbloom 940961Sbostic #include "sendmail.h" 1035653Seric 1129432Sbloom #ifndef lint 1235653Seric #ifdef NAMED_BIND 13*66145Seric static char sccsid[] = "@(#)domain.c 8.14 (Berkeley) 02/17/94 (with name server)"; 1435653Seric #else 15*66145Seric static char sccsid[] = "@(#)domain.c 8.14 (Berkeley) 02/17/94 (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 3259075Seric static char MXHostBuf[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 4263969Seric #ifndef NO_DATA 4363969Seric # define NO_DATA NO_ADDRESS 4463969Seric #endif 4563969Seric 4665751Seric #ifndef HEADERSZ 4765751Seric # define HEADERSZ sizeof(HEADER) 4865751Seric #endif 4965751Seric 5058248Seric /* don't use sizeof because sizeof(long) is different on 64-bit machines */ 5158248Seric #define SHORTSIZE 2 /* size of a short (really, must be 2) */ 5258248Seric #define LONGSIZE 4 /* size of a long (really, must be 4) */ 5362524Sbostic 5462524Sbostic #define MAXCNAMEDEPTH 10 /* maximum depth of CNAME recursion */ 5558248Seric /* 5658248Seric ** GETMXRR -- get MX resource records for a domain 5758248Seric ** 5858248Seric ** Parameters: 5958248Seric ** host -- the name of the host to MX. 6058248Seric ** mxhosts -- a pointer to a return buffer of MX records. 6159273Seric ** droplocalhost -- If TRUE, all MX records less preferred 6259273Seric ** than the local host (as determined by $=w) will 6359273Seric ** be discarded. 6458248Seric ** rcode -- a pointer to an EX_ status code. 6558248Seric ** 6658248Seric ** Returns: 6758248Seric ** The number of MX records found. 6858248Seric ** -1 if there is an internal failure. 6958248Seric ** If no MX records are found, mxhosts[0] is set to host 7058248Seric ** and 1 is returned. 7158248Seric */ 7258248Seric 7359273Seric getmxrr(host, mxhosts, droplocalhost, rcode) 7459075Seric char *host; 7559075Seric char **mxhosts; 7659273Seric bool droplocalhost; 7733929Sbostic int *rcode; 7829432Sbloom { 7933929Sbostic extern int h_errno; 8033929Sbostic register u_char *eom, *cp; 8163840Seric register int i, j, n; 8263840Seric int nmx = 0; 8333929Sbostic register char *bp; 8429432Sbloom HEADER *hp; 8533929Sbostic querybuf answer; 8658857Seric int ancount, qdcount, buflen; 8763840Seric bool seenlocal = FALSE; 8858891Seric u_short pref, localpref, type; 8958891Seric char *fallbackMX = FallBackMX; 9058891Seric static bool firsttime = TRUE; 9159273Seric STAB *st; 9264015Seric bool trycanon = FALSE; 9358891Seric u_short prefer[MAXMXHOSTS]; 9457454Seric int weight[MAXMXHOSTS]; 9563840Seric extern bool getcanonname(); 9629432Sbloom 97*66145Seric if (tTd(8, 2)) 98*66145Seric printf("getmxrr(%s, droplocalhost=%d)\n", host, droplocalhost); 99*66145Seric 10058891Seric if (fallbackMX != NULL) 10158891Seric { 10258891Seric if (firsttime && res_query(FallBackMX, C_IN, T_A, 10358891Seric (char *) &answer, sizeof answer) < 0) 10458891Seric { 10558891Seric /* this entry is bogus */ 10658891Seric fallbackMX = FallBackMX = NULL; 10758891Seric } 10859273Seric else if (droplocalhost && 10959273Seric (st = stab(fallbackMX, ST_CLASS, ST_FIND)) != NULL && 11059273Seric bitnset('w', st->s_class)) 11158891Seric { 11258891Seric /* don't use fallback for this pass */ 11358891Seric fallbackMX = NULL; 11458891Seric } 11558891Seric firsttime = FALSE; 11658891Seric } 11758891Seric 11864676Seric /* efficiency hack -- numeric or non-MX lookups */ 11964676Seric if (host[0] == '[') 12064676Seric goto punt; 12164676Seric 12236483Sbostic errno = 0; 12333929Sbostic n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer)); 12435653Seric if (n < 0) 12535653Seric { 12633929Sbostic if (tTd(8, 1)) 12752852Seric printf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n", 12852852Seric (host == NULL) ? "<NULL>" : host, errno, h_errno); 12935653Seric switch (h_errno) 13035653Seric { 13135653Seric case NO_DATA: 13264015Seric trycanon = TRUE; 13364015Seric /* fall through */ 13464015Seric 13535653Seric case NO_RECOVERY: 13635653Seric /* no MX data on this host */ 13733929Sbostic goto punt; 13835653Seric 13935653Seric case HOST_NOT_FOUND: 14035653Seric /* the host just doesn't exist */ 14133929Sbostic *rcode = EX_NOHOST; 14264950Seric 14364950Seric if (!UseNameServer) 14464950Seric { 14564950Seric /* might exist in /etc/hosts */ 14664950Seric goto punt; 14764950Seric } 14833929Sbostic break; 14935653Seric 15035653Seric case TRY_AGAIN: 15135653Seric /* couldn't connect to the name server */ 15235653Seric if (!UseNameServer && errno == ECONNREFUSED) 15335653Seric goto punt; 15435653Seric 15535653Seric /* it might come up later; better queue it up */ 15633929Sbostic *rcode = EX_TEMPFAIL; 15733929Sbostic break; 15829432Sbloom } 15935653Seric 16035653Seric /* irreconcilable differences */ 16135653Seric return (-1); 16229432Sbloom } 16333929Sbostic 16433929Sbostic /* find first satisfactory answer */ 16533929Sbostic hp = (HEADER *)&answer; 16665751Seric cp = (u_char *)&answer + HEADERSZ; 16733929Sbostic eom = (u_char *)&answer + n; 16833929Sbostic for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ) 16950957Skarels if ((n = dn_skipname(cp, eom)) < 0) 17033929Sbostic goto punt; 17159075Seric buflen = sizeof(MXHostBuf) - 1; 17259075Seric bp = MXHostBuf; 17333929Sbostic ancount = ntohs(hp->ancount); 17458848Seric while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1) 17556336Seric { 17646928Sbostic if ((n = dn_expand((u_char *)&answer, 17746928Sbostic eom, cp, (u_char *)bp, buflen)) < 0) 17829432Sbloom break; 17929432Sbloom cp += n; 18033929Sbostic GETSHORT(type, cp); 18158248Seric cp += SHORTSIZE + LONGSIZE; 18233929Sbostic GETSHORT(n, cp); 18356336Seric if (type != T_MX) 18456336Seric { 18557943Seric if (tTd(8, 8) || _res.options & RES_DEBUG) 18629432Sbloom printf("unexpected answer type %d, size %d\n", 18733929Sbostic type, n); 18829432Sbloom cp += n; 18929432Sbloom continue; 19029432Sbloom } 19133929Sbostic GETSHORT(pref, cp); 19256336Seric if ((n = dn_expand((u_char *)&answer, eom, cp, 19356336Seric (u_char *)bp, buflen)) < 0) 19429432Sbloom break; 19529551Sbloom cp += n; 19659273Seric if (droplocalhost && 19759273Seric (st = stab(bp, ST_CLASS, ST_FIND)) != NULL && 19859273Seric bitnset('w', st->s_class)) 19956336Seric { 200*66145Seric if (tTd(8, 3)) 201*66145Seric printf("found localhost (%s) in MX list, pref=%d\n", 202*66145Seric bp, pref); 20358857Seric if (!seenlocal || pref < localpref) 20433929Sbostic localpref = pref; 20558857Seric seenlocal = TRUE; 20629551Sbloom continue; 20729551Sbloom } 20857454Seric weight[nmx] = mxrand(bp); 20929432Sbloom prefer[nmx] = pref; 21029432Sbloom mxhosts[nmx++] = bp; 21156336Seric n = strlen(bp); 21233929Sbostic bp += n; 21356336Seric if (bp[-1] != '.') 21456336Seric { 21556336Seric *bp++ = '.'; 21656336Seric n++; 21756336Seric } 21856336Seric *bp++ = '\0'; 21956336Seric buflen -= n + 1; 22029432Sbloom } 22163840Seric 22263840Seric /* sort the records */ 22363840Seric for (i = 0; i < nmx; i++) 22457454Seric { 22563840Seric for (j = i + 1; j < nmx; j++) 22658668Seric { 22763840Seric if (prefer[i] > prefer[j] || 22863840Seric (prefer[i] == prefer[j] && weight[i] > weight[j])) 22963840Seric { 23063840Seric register int temp; 23163840Seric register char *temp1; 23263840Seric 23363840Seric temp = prefer[i]; 23463840Seric prefer[i] = prefer[j]; 23563840Seric prefer[j] = temp; 23663840Seric temp1 = mxhosts[i]; 23763840Seric mxhosts[i] = mxhosts[j]; 23863840Seric mxhosts[j] = temp1; 23963840Seric temp = weight[i]; 24063840Seric weight[i] = weight[j]; 24163840Seric weight[j] = temp; 24263840Seric } 24358668Seric } 24463840Seric if (seenlocal && prefer[i] >= localpref) 24563840Seric { 24663840Seric /* truncate higher preference part of list */ 24763840Seric nmx = i; 24863840Seric } 24929551Sbloom } 25063840Seric 25163840Seric if (nmx == 0) 25257454Seric { 25363840Seric punt: 25463840Seric if (seenlocal && 25563840Seric (!TryNullMXList || gethostbyname(host) == NULL)) 25657454Seric { 25763840Seric /* 25863840Seric ** If we have deleted all MX entries, this is 25963840Seric ** an error -- we should NEVER send to a host that 26063840Seric ** has an MX, and this should have been caught 26163840Seric ** earlier in the config file. 26263840Seric ** 26363840Seric ** Some sites prefer to go ahead and try the 26463840Seric ** A record anyway; that case is handled by 26563840Seric ** setting TryNullMXList. I believe this is a 26663840Seric ** bad idea, but it's up to you.... 26763840Seric */ 26829432Sbloom 26963840Seric *rcode = EX_CONFIG; 27063840Seric return -1; 27163840Seric } 27264676Seric strcpy(MXHostBuf, host); 27364676Seric mxhosts[0] = MXHostBuf; 27464676Seric if (host[0] == '[') 27564676Seric { 27664676Seric register char *p; 27764676Seric 27864676Seric /* this may be an MX suppression-style address */ 27964676Seric p = strchr(MXHostBuf, ']'); 28064676Seric if (p != NULL) 28164676Seric { 28264676Seric *p = '\0'; 28364676Seric if (inet_addr(&MXHostBuf[1]) != -1) 28464676Seric *p = ']'; 28564676Seric else 28664677Seric { 28764677Seric trycanon = TRUE; 28864676Seric mxhosts[0]++; 28964677Seric } 29064676Seric } 29164676Seric } 29264677Seric if (trycanon && 29364677Seric getcanonname(mxhosts[0], sizeof MXHostBuf - 2, FALSE)) 29463840Seric { 29564950Seric bp = &MXHostBuf[strlen(MXHostBuf)]; 29663840Seric if (bp[-1] != '.') 29757454Seric { 29863840Seric *bp++ = '.'; 29963840Seric *bp = '\0'; 30029551Sbloom } 30129551Sbloom } 30263840Seric nmx = 1; 30329432Sbloom } 30458848Seric 30558848Seric /* if we have a default lowest preference, include that */ 30664950Seric if (fallbackMX != NULL && !seenlocal) 30764950Seric mxhosts[nmx++] = fallbackMX; 30858848Seric 30957454Seric return (nmx); 31029432Sbloom } 31157135Seric /* 31257454Seric ** MXRAND -- create a randomizer for equal MX preferences 31357454Seric ** 31457454Seric ** If two MX hosts have equal preferences we want to randomize 31557454Seric ** the selection. But in order for signatures to be the same, 31657454Seric ** we need to randomize the same way each time. This function 31757454Seric ** computes a pseudo-random hash function from the host name. 31857454Seric ** 31957454Seric ** Parameters: 32057454Seric ** host -- the name of the host. 32157454Seric ** 32257454Seric ** Returns: 32357454Seric ** A random but repeatable value based on the host name. 32457454Seric ** 32557454Seric ** Side Effects: 32657454Seric ** none. 32757454Seric */ 32857454Seric 32957454Seric mxrand(host) 33057454Seric register char *host; 33157454Seric { 33257454Seric int hfunc; 33357454Seric static unsigned int seed; 33457454Seric 33557454Seric if (seed == 0) 33657454Seric { 33757454Seric seed = (int) curtime() & 0xffff; 33857454Seric if (seed == 0) 33957454Seric seed++; 34057454Seric } 34157454Seric 34257454Seric if (tTd(17, 9)) 34357454Seric printf("mxrand(%s)", host); 34457454Seric 34557454Seric hfunc = seed; 34657454Seric while (*host != '\0') 34757454Seric { 34857454Seric int c = *host++; 34957454Seric 35057454Seric if (isascii(c) && isupper(c)) 35157454Seric c = tolower(c); 35265952Seric hfunc = ((hfunc << 1) ^ c) % 2003; 35357454Seric } 35457454Seric 35557454Seric hfunc &= 0xff; 35657454Seric 35757454Seric if (tTd(17, 9)) 35857454Seric printf(" = %d\n", hfunc); 35957454Seric return hfunc; 36057454Seric } 36157454Seric /* 36257135Seric ** GETCANONNAME -- get the canonical name for named host 36357135Seric ** 36457943Seric ** This algorithm tries to be smart about wildcard MX records. 36557943Seric ** This is hard to do because DNS doesn't tell is if we matched 36657943Seric ** against a wildcard or a specific MX. 36757943Seric ** 36857943Seric ** We always prefer A & CNAME records, since these are presumed 36957943Seric ** to be specific. 37057943Seric ** 37157943Seric ** If we match an MX in one pass and lose it in the next, we use 37257943Seric ** the old one. For example, consider an MX matching *.FOO.BAR.COM. 37357943Seric ** A hostname bletch.foo.bar.com will match against this MX, but 37457943Seric ** will stop matching when we try bletch.bar.com -- so we know 37557943Seric ** that bletch.foo.bar.com must have been right. This fails if 37657943Seric ** there was also an MX record matching *.BAR.COM, but there are 37757943Seric ** some things that just can't be fixed. 37857943Seric ** 37957135Seric ** Parameters: 38057135Seric ** host -- a buffer containing the name of the host. 38157135Seric ** This is a value-result parameter. 38257135Seric ** hbsize -- the size of the host buffer. 38363840Seric ** trymx -- if set, try MX records as well as A and CNAME. 38457135Seric ** 38557135Seric ** Returns: 38657135Seric ** TRUE -- if the host matched. 38757135Seric ** FALSE -- otherwise. 38857135Seric */ 38929653Sbloom 39051314Seric bool 39163840Seric getcanonname(host, hbsize, trymx) 39229653Sbloom char *host; 39329653Sbloom int hbsize; 39463840Seric bool trymx; 39529653Sbloom { 39640277Sbostic extern int h_errno; 39751324Seric register u_char *eom, *ap; 39851324Seric register char *cp; 39933929Sbostic register int n; 40029653Sbloom HEADER *hp; 40133929Sbostic querybuf answer; 40261707Seric int ancount, qdcount; 40351324Seric int ret; 40451324Seric char **domain; 40551324Seric int type; 40657943Seric char **dp; 40757943Seric char *mxmatch; 40857943Seric bool amatch; 40958411Seric bool gotmx; 41058404Seric int qtype; 41162524Sbostic int loopcnt; 41265193Seric char *xp; 41358010Seric char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)]; 41458039Seric char *searchlist[MAXDNSRCH+2]; 41565193Seric extern char *gethostalias(); 41629653Sbloom 41751324Seric if (tTd(8, 2)) 41851324Seric printf("getcanonname(%s)\n", host); 41951324Seric 42051324Seric if ((_res.options & RES_INIT) == 0 && res_init() == -1) 42151324Seric return (FALSE); 42251324Seric 42336483Sbostic /* 42457943Seric ** Initialize domain search list. If there is at least one 42557943Seric ** dot in the name, search the unmodified name first so we 42657943Seric ** find "vse.CS" in Czechoslovakia instead of in the local 42757943Seric ** domain (e.g., vse.CS.Berkeley.EDU). 42857943Seric ** 42957943Seric ** Older versions of the resolver could create this 43057943Seric ** list by tearing apart the host name. 43157205Seric */ 43257205Seric 43362524Sbostic loopcnt = 0; 43459268Seric cnameloop: 43559268Seric for (cp = host, n = 0; *cp; cp++) 43659268Seric if (*cp == '.') 43759268Seric n++; 43859268Seric 43965193Seric if (n == 0 && (xp = gethostalias(host)) != NULL) 44065193Seric { 44165193Seric if (loopcnt++ > MAXCNAMEDEPTH) 44265193Seric { 44365193Seric syserr("loop in ${HOSTALIASES} file"); 44465193Seric } 44565193Seric else 44665193Seric { 44765193Seric strncpy(host, xp, hbsize); 44865193Seric host[hbsize - 1] = '\0'; 44965193Seric goto cnameloop; 45065193Seric } 45165193Seric } 45265193Seric 45357943Seric dp = searchlist; 45457943Seric if (n > 0) 45557943Seric *dp++ = ""; 45658411Seric if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options)) 45751324Seric { 45857943Seric for (domain = _res.dnsrch; *domain != NULL; ) 45957943Seric *dp++ = *domain++; 46057205Seric } 46158411Seric else if (n == 0 && bitset(RES_DEFNAMES, _res.options)) 46258411Seric { 46358411Seric *dp++ = _res.defdname; 46458411Seric } 46566040Seric else if (*cp == '.') 46666040Seric { 46766040Seric *cp = '\0'; 46866040Seric } 46957943Seric *dp = NULL; 47057205Seric 47157205Seric /* 47257943Seric ** Now run through the search list for the name in question. 47357205Seric */ 47457205Seric 47557943Seric mxmatch = NULL; 47658404Seric qtype = T_ANY; 47757943Seric 47858404Seric for (dp = searchlist; *dp != NULL; ) 47957205Seric { 48058411Seric if (qtype == T_ANY) 48158411Seric gotmx = FALSE; 48257943Seric if (tTd(8, 5)) 48358508Seric printf("getcanonname: trying %s.%s (%s)\n", host, *dp, 48458508Seric qtype == T_ANY ? "ANY" : qtype == T_A ? "A" : 48558508Seric qtype == T_MX ? "MX" : "???"); 48658404Seric ret = res_querydomain(host, *dp, C_IN, qtype, 48758010Seric &answer, sizeof(answer)); 48857943Seric if (ret <= 0) 48951324Seric { 49058796Seric if (tTd(8, 7)) 49158082Seric printf("\tNO: errno=%d, h_errno=%d\n", 49258082Seric errno, h_errno); 49351324Seric 49458082Seric if (errno == ECONNREFUSED || h_errno == TRY_AGAIN) 49557205Seric { 49657943Seric /* the name server seems to be down */ 49751324Seric h_errno = TRY_AGAIN; 49851910Seric return FALSE; 49951324Seric } 50057943Seric 50158501Seric if (h_errno != HOST_NOT_FOUND) 50258404Seric { 50358501Seric /* might have another type of interest */ 50458501Seric if (qtype == T_ANY) 50558501Seric { 50658501Seric qtype = T_A; 50758501Seric continue; 50858501Seric } 50963840Seric else if (qtype == T_A && !gotmx && trymx) 51058501Seric { 51158501Seric qtype = T_MX; 51258501Seric continue; 51358501Seric } 51458404Seric } 51558404Seric 51657943Seric if (mxmatch != NULL) 51751324Seric { 51857943Seric /* we matched before -- use that one */ 51951324Seric break; 52051324Seric } 52158501Seric 52258501Seric /* otherwise, try the next name */ 52358501Seric dp++; 52458501Seric qtype = T_ANY; 52557943Seric continue; 52651324Seric } 52758796Seric else if (tTd(8, 7)) 52857943Seric printf("\tYES\n"); 52957943Seric 53051910Seric /* 53157943Seric ** This might be a bogus match. Search for A or 53257943Seric ** CNAME records. If we don't have a matching 53357943Seric ** wild card MX record, we will accept MX as well. 53451910Seric */ 53551910Seric 53657943Seric hp = (HEADER *) &answer; 53765751Seric ap = (u_char *) &answer + HEADERSZ; 53857943Seric eom = (u_char *) &answer + ret; 53957943Seric 54057943Seric /* skip question part of response -- we know what we asked */ 54157943Seric for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ) 54251324Seric { 54357943Seric if ((ret = dn_skipname(ap, eom)) < 0) 54457943Seric { 54557943Seric if (tTd(8, 20)) 54657943Seric printf("qdcount failure (%d)\n", 54757943Seric ntohs(hp->qdcount)); 54857943Seric return FALSE; /* ???XXX??? */ 54957943Seric } 55051324Seric } 55157943Seric 55257943Seric amatch = FALSE; 55357943Seric for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n) 55451324Seric { 55557943Seric n = dn_expand((u_char *) &answer, eom, ap, 55657943Seric (u_char *) nbuf, sizeof nbuf); 55757943Seric if (n < 0) 55857943Seric break; 55957943Seric ap += n; 56057943Seric GETSHORT(type, ap); 56158248Seric ap += SHORTSIZE + LONGSIZE; 56257943Seric GETSHORT(n, ap); 56357943Seric switch (type) 56457943Seric { 56557943Seric case T_MX: 56658411Seric gotmx = TRUE; 56764030Seric if (**dp != '\0') 56857943Seric { 56957943Seric /* got a match -- save that info */ 57064030Seric if (trymx && mxmatch == NULL) 57157943Seric mxmatch = *dp; 57257943Seric continue; 57357943Seric } 57433929Sbostic 57557943Seric /* exact MX matches are as good as an A match */ 57657943Seric /* fall through */ 57757205Seric 57857943Seric case T_A: 57957943Seric /* good show */ 58057943Seric amatch = TRUE; 58133929Sbostic 58257943Seric /* continue in case a CNAME also exists */ 58357943Seric continue; 58457943Seric 58557943Seric case T_CNAME: 58662524Sbostic if (loopcnt++ > MAXCNAMEDEPTH) 58762524Sbostic { 58862524Sbostic syserr("DNS failure: CNAME loop for %s", 58962524Sbostic host); 59062524Sbostic continue; 59162524Sbostic } 59262524Sbostic 59357943Seric /* value points at name */ 59457943Seric if ((ret = dn_expand((u_char *)&answer, 59557943Seric eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0) 59657943Seric break; 59757943Seric (void)strncpy(host, nbuf, hbsize); /* XXX */ 59857943Seric host[hbsize - 1] = '\0'; 59957943Seric 60058844Seric /* 60158844Seric ** RFC 1034 section 3.6 specifies that CNAME 60258844Seric ** should point at the canonical name -- but 60358844Seric ** urges software to try again anyway. 60458844Seric */ 60558844Seric 60658844Seric goto cnameloop; 60758844Seric 60857943Seric default: 60957943Seric /* not a record of interest */ 61057943Seric continue; 61157943Seric } 61251324Seric } 61333929Sbostic 61457943Seric if (amatch) 61557943Seric { 61657943Seric /* got an A record and no CNAME */ 61757943Seric mxmatch = *dp; 61829653Sbloom break; 61929653Sbloom } 62058404Seric 62158404Seric /* 62258404Seric ** If this was a T_ANY query, we may have the info but 62358404Seric ** need an explicit query. Try T_A, then T_MX. 62458404Seric */ 62558404Seric 62658404Seric if (qtype == T_ANY) 62758404Seric qtype = T_A; 62863840Seric else if (qtype == T_A && !gotmx && trymx) 62958404Seric qtype = T_MX; 63058404Seric else 63158404Seric { 63258404Seric /* really nothing in this domain; try the next */ 63358404Seric qtype = T_ANY; 63458404Seric dp++; 63558404Seric } 63629653Sbloom } 63757943Seric 63857943Seric if (mxmatch == NULL) 63957943Seric return FALSE; 64057943Seric 64157943Seric /* create matching name and return */ 64257943Seric (void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host, 64357943Seric *mxmatch == '\0' ? "" : ".", 64457943Seric MAXDNAME, mxmatch); 64557943Seric strncpy(host, nbuf, hbsize); 64657943Seric host[hbsize - 1] = '\0'; 64757943Seric return TRUE; 64829653Sbloom } 64935653Seric 65065193Seric 65165193Seric char * 65265193Seric gethostalias(host) 65365193Seric char *host; 65465193Seric { 65565193Seric char *fname; 65665193Seric FILE *fp; 65765193Seric register char *p; 65865193Seric char buf[MAXLINE]; 65965193Seric static char hbuf[MAXDNAME]; 66065193Seric 66165193Seric fname = getenv("HOSTALIASES"); 66265193Seric if (fname == NULL || (fp = fopen(fname, "r")) == NULL) 66365193Seric return NULL; 66465193Seric while (fgets(buf, sizeof buf, fp) != NULL) 66565193Seric { 66665193Seric for (p = buf; p != '\0' && !(isascii(*p) && isspace(*p)); p++) 66765193Seric continue; 66865193Seric if (*p == 0) 66965193Seric { 67065193Seric /* syntax error */ 67165193Seric continue; 67265193Seric } 67365193Seric *p++ = '\0'; 67465193Seric if (strcasecmp(buf, host) == 0) 67565193Seric break; 67665193Seric } 67765193Seric 67865193Seric if (feof(fp)) 67965193Seric { 68065193Seric /* no match */ 68165193Seric fclose(fp); 68265193Seric return NULL; 68365193Seric } 68465193Seric 68565193Seric /* got a match; extract the equivalent name */ 68665193Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 68765193Seric p++; 68865193Seric host = p; 68965193Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 69065193Seric p++; 69165193Seric *p = '\0'; 69265193Seric strncpy(hbuf, host, sizeof hbuf - 1); 69365193Seric hbuf[sizeof hbuf - 1] = '\0'; 69465193Seric return hbuf; 69565193Seric } 69665193Seric 69765193Seric 69836494Sphil #else /* not NAMED_BIND */ 69936494Sphil 70036494Sphil #include <netdb.h> 70136494Sphil 70251314Seric bool 70363840Seric getcanonname(host, hbsize, trymx) 70436494Sphil char *host; 70536494Sphil int hbsize; 70663840Seric bool trymx; 70736494Sphil { 70836494Sphil struct hostent *hp; 70936494Sphil 71036494Sphil hp = gethostbyname(host); 71136494Sphil if (hp == NULL) 71251314Seric return (FALSE); 71336494Sphil 71436494Sphil if (strlen(hp->h_name) >= hbsize) 71551314Seric return (FALSE); 71636494Sphil 71736494Sphil (void) strcpy(host, hp->h_name); 71851314Seric return (TRUE); 71936494Sphil } 72036494Sphil 72136494Sphil #endif /* not NAMED_BIND */ 722