xref: /csrg-svn/usr.sbin/sendmail/src/domain.c (revision 58857)
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*58857Seric static char sccsid[] = "@(#)domain.c	6.16 (Berkeley) 03/29/93 (with name server)";
1435653Seric #else
15*58857Seric static char sccsid[] = "@(#)domain.c	6.16 (Berkeley) 03/29/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;
72*58857Seric 	int ancount, qdcount, buflen;
73*58857Seric 	bool seenlocal;
7433929Sbostic 	u_short pref, localpref, type, prefer[MAXMXHOSTS];
7557454Seric 	int weight[MAXMXHOSTS];
7629432Sbloom 
7736483Sbostic 	errno = 0;
7833929Sbostic 	n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer));
7935653Seric 	if (n < 0)
8035653Seric 	{
8133929Sbostic 		if (tTd(8, 1))
8252852Seric 			printf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n",
8352852Seric 			    (host == NULL) ? "<NULL>" : host, errno, h_errno);
8435653Seric 		switch (h_errno)
8535653Seric 		{
8635653Seric 		  case NO_DATA:
8735653Seric 		  case NO_RECOVERY:
8835653Seric 			/* no MX data on this host */
8933929Sbostic 			goto punt;
9035653Seric 
9135653Seric 		  case HOST_NOT_FOUND:
9235653Seric 			/* the host just doesn't exist */
9333929Sbostic 			*rcode = EX_NOHOST;
9433929Sbostic 			break;
9535653Seric 
9635653Seric 		  case TRY_AGAIN:
9735653Seric 			/* couldn't connect to the name server */
9835653Seric 			if (!UseNameServer && errno == ECONNREFUSED)
9935653Seric 				goto punt;
10035653Seric 
10135653Seric 			/* it might come up later; better queue it up */
10233929Sbostic 			*rcode = EX_TEMPFAIL;
10333929Sbostic 			break;
10429432Sbloom 		}
10535653Seric 
10635653Seric 		/* irreconcilable differences */
10735653Seric 		return (-1);
10829432Sbloom 	}
10933929Sbostic 
11033929Sbostic 	/* find first satisfactory answer */
11133929Sbostic 	hp = (HEADER *)&answer;
11233929Sbostic 	cp = (u_char *)&answer + sizeof(HEADER);
11333929Sbostic 	eom = (u_char *)&answer + n;
11433929Sbostic 	for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
11550957Skarels 		if ((n = dn_skipname(cp, eom)) < 0)
11633929Sbostic 			goto punt;
11729432Sbloom 	nmx = 0;
118*58857Seric 	seenlocal = FALSE;
11956336Seric 	buflen = sizeof(hostbuf) - 1;
12033929Sbostic 	bp = hostbuf;
12133929Sbostic 	ancount = ntohs(hp->ancount);
12258848Seric 	while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1)
12356336Seric 	{
12446928Sbostic 		if ((n = dn_expand((u_char *)&answer,
12546928Sbostic 		    eom, cp, (u_char *)bp, buflen)) < 0)
12629432Sbloom 			break;
12729432Sbloom 		cp += n;
12833929Sbostic 		GETSHORT(type, cp);
12958248Seric  		cp += SHORTSIZE + LONGSIZE;
13033929Sbostic 		GETSHORT(n, cp);
13156336Seric 		if (type != T_MX)
13256336Seric 		{
13357943Seric 			if (tTd(8, 8) || _res.options & RES_DEBUG)
13429432Sbloom 				printf("unexpected answer type %d, size %d\n",
13533929Sbostic 				    type, n);
13629432Sbloom 			cp += n;
13729432Sbloom 			continue;
13829432Sbloom 		}
13933929Sbostic 		GETSHORT(pref, cp);
14056336Seric 		if ((n = dn_expand((u_char *)&answer, eom, cp,
14156336Seric 				   (u_char *)bp, buflen)) < 0)
14229432Sbloom 			break;
14329551Sbloom 		cp += n;
144*58857Seric 		if (strcasecmp(bp, localhost) == 0)
14556336Seric 		{
146*58857Seric 			if (!seenlocal || pref < localpref)
14733929Sbostic 				localpref = pref;
148*58857Seric 			seenlocal = TRUE;
14929551Sbloom 			continue;
15029551Sbloom 		}
15157454Seric 		weight[nmx] = mxrand(bp);
15229432Sbloom 		prefer[nmx] = pref;
15329432Sbloom 		mxhosts[nmx++] = bp;
15456336Seric 		n = strlen(bp);
15533929Sbostic 		bp += n;
15656336Seric 		if (bp[-1] != '.')
15756336Seric 		{
15856336Seric 			*bp++ = '.';
15956336Seric 			n++;
16056336Seric 		}
16156336Seric 		*bp++ = '\0';
16256336Seric 		buflen -= n + 1;
16329432Sbloom 	}
16457454Seric 	if (nmx == 0)
16557454Seric 	{
16658668Seric punt:
16758668Seric 		mxhosts[0] = strcpy(hostbuf, host);
16858668Seric 		bp = &hostbuf[strlen(hostbuf)];
16958668Seric 		if (bp[-1] != '.')
17058668Seric 		{
17158668Seric 			*bp++ = '.';
17258668Seric 			*bp = '\0';
17358668Seric 		}
17458848Seric 		nmx = 1;
175*58857Seric 		prefer[0] = 0;
176*58857Seric 		weight[0] = 0;
17729551Sbloom 	}
17833929Sbostic 
17929432Sbloom 	/* sort the records */
18057454Seric 	for (i = 0; i < nmx; i++)
18157454Seric 	{
18257454Seric 		for (j = i + 1; j < nmx; j++)
18357454Seric 		{
18436483Sbostic 			if (prefer[i] > prefer[j] ||
18557454Seric 			    (prefer[i] == prefer[j] && weight[i] > weight[j]))
18657454Seric 			{
18733929Sbostic 				register int temp;
18833929Sbostic 				register char *temp1;
18929432Sbloom 
19029432Sbloom 				temp = prefer[i];
19129432Sbloom 				prefer[i] = prefer[j];
19229432Sbloom 				prefer[j] = temp;
19329432Sbloom 				temp1 = mxhosts[i];
19429432Sbloom 				mxhosts[i] = mxhosts[j];
19529432Sbloom 				mxhosts[j] = temp1;
19657454Seric 				temp = weight[i];
19757454Seric 				weight[i] = weight[j];
19857454Seric 				weight[j] = temp;
19929432Sbloom 			}
20029432Sbloom 		}
20157454Seric 		if (seenlocal && prefer[i] >= localpref)
20257454Seric 		{
20329551Sbloom 			/*
20433929Sbostic 			 * truncate higher pref part of list; if we're
20533929Sbostic 			 * the best choice left, we should have realized
20633929Sbostic 			 * awhile ago that this was a local delivery.
20729551Sbloom 			 */
20857454Seric 			if (i == 0)
20957454Seric 			{
21033929Sbostic 				*rcode = EX_CONFIG;
21157454Seric 				return (-1);
21229551Sbloom 			}
21333929Sbostic 			nmx = i;
21429551Sbloom 			break;
21529551Sbloom 		}
21629432Sbloom 	}
21758848Seric 
21858848Seric 	/* if we have a default lowest preference, include that */
21958848Seric 	if (FallBackMX != NULL)
22058848Seric 		mxhosts[nmx++] = FallBackMX;
22158848Seric 
22257454Seric 	return (nmx);
22329432Sbloom }
22457135Seric /*
22557454Seric **  MXRAND -- create a randomizer for equal MX preferences
22657454Seric **
22757454Seric **	If two MX hosts have equal preferences we want to randomize
22857454Seric **	the selection.  But in order for signatures to be the same,
22957454Seric **	we need to randomize the same way each time.  This function
23057454Seric **	computes a pseudo-random hash function from the host name.
23157454Seric **
23257454Seric **	Parameters:
23357454Seric **		host -- the name of the host.
23457454Seric **
23557454Seric **	Returns:
23657454Seric **		A random but repeatable value based on the host name.
23757454Seric **
23857454Seric **	Side Effects:
23957454Seric **		none.
24057454Seric */
24157454Seric 
24257454Seric mxrand(host)
24357454Seric 	register char *host;
24457454Seric {
24557454Seric 	int hfunc;
24657454Seric 	static unsigned int seed;
24757454Seric 
24857454Seric 	if (seed == 0)
24957454Seric 	{
25057454Seric 		seed = (int) curtime() & 0xffff;
25157454Seric 		if (seed == 0)
25257454Seric 			seed++;
25357454Seric 	}
25457454Seric 
25557454Seric 	if (tTd(17, 9))
25657454Seric 		printf("mxrand(%s)", host);
25757454Seric 
25857454Seric 	hfunc = seed;
25957454Seric 	while (*host != '\0')
26057454Seric 	{
26157454Seric 		int c = *host++;
26257454Seric 
26357454Seric 		if (isascii(c) && isupper(c))
26457454Seric 			c = tolower(c);
26557454Seric 		hfunc = ((hfunc << 1) + c) % 2003;
26657454Seric 	}
26757454Seric 
26857454Seric 	hfunc &= 0xff;
26957454Seric 
27057454Seric 	if (tTd(17, 9))
27157454Seric 		printf(" = %d\n", hfunc);
27257454Seric 	return hfunc;
27357454Seric }
27457454Seric /*
27557135Seric **  GETCANONNAME -- get the canonical name for named host
27657135Seric **
27757943Seric **	This algorithm tries to be smart about wildcard MX records.
27857943Seric **	This is hard to do because DNS doesn't tell is if we matched
27957943Seric **	against a wildcard or a specific MX.
28057943Seric **
28157943Seric **	We always prefer A & CNAME records, since these are presumed
28257943Seric **	to be specific.
28357943Seric **
28457943Seric **	If we match an MX in one pass and lose it in the next, we use
28557943Seric **	the old one.  For example, consider an MX matching *.FOO.BAR.COM.
28657943Seric **	A hostname bletch.foo.bar.com will match against this MX, but
28757943Seric **	will stop matching when we try bletch.bar.com -- so we know
28857943Seric **	that bletch.foo.bar.com must have been right.  This fails if
28957943Seric **	there was also an MX record matching *.BAR.COM, but there are
29057943Seric **	some things that just can't be fixed.
29157943Seric **
29257135Seric **	Parameters:
29357135Seric **		host -- a buffer containing the name of the host.
29457135Seric **			This is a value-result parameter.
29557135Seric **		hbsize -- the size of the host buffer.
29657135Seric **
29757135Seric **	Returns:
29857135Seric **		TRUE -- if the host matched.
29957135Seric **		FALSE -- otherwise.
30057135Seric */
30129653Sbloom 
30251314Seric bool
30329653Sbloom getcanonname(host, hbsize)
30429653Sbloom 	char *host;
30529653Sbloom 	int hbsize;
30629653Sbloom {
30740277Sbostic 	extern int h_errno;
30851324Seric 	register u_char *eom, *ap;
30951324Seric 	register char *cp;
31033929Sbostic 	register int n;
31129653Sbloom 	HEADER *hp;
31233929Sbostic 	querybuf answer;
31357943Seric 	int first, ancount, qdcount;
31451324Seric 	int ret;
31551324Seric 	char **domain;
31651324Seric 	int type;
31757943Seric 	char **dp;
31857943Seric 	char *mxmatch;
31957943Seric 	bool amatch;
32058411Seric 	bool gotmx;
32158404Seric 	int qtype;
32258010Seric 	char nbuf[MAX(PACKETSZ, MAXDNAME*2+2)];
32358039Seric 	char *searchlist[MAXDNSRCH+2];
32429653Sbloom 
32551324Seric 	if (tTd(8, 2))
32651324Seric 		printf("getcanonname(%s)\n", host);
32751324Seric 
32851324Seric 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
32951324Seric 		return (FALSE);
33051324Seric 
33151324Seric 	for (cp = host, n = 0; *cp; cp++)
33251324Seric 		if (*cp == '.')
33351324Seric 			n++;
33451324Seric 
33536483Sbostic 	/*
33657943Seric 	**  Initialize domain search list.  If there is at least one
33757943Seric 	**  dot in the name, search the unmodified name first so we
33857943Seric 	**  find "vse.CS" in Czechoslovakia instead of in the local
33957943Seric 	**  domain (e.g., vse.CS.Berkeley.EDU).
34057943Seric 	**
34157943Seric 	**  Older versions of the resolver could create this
34257943Seric 	**  list by tearing apart the host name.
34357205Seric 	*/
34457205Seric 
34557943Seric 	dp = searchlist;
34657943Seric 	if (n > 0)
34757943Seric 		*dp++ = "";
34858411Seric 	if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options))
34951324Seric 	{
35057943Seric 		for (domain = _res.dnsrch; *domain != NULL; )
35157943Seric 			*dp++ = *domain++;
35257205Seric 	}
35358411Seric 	else if (n == 0 && bitset(RES_DEFNAMES, _res.options))
35458411Seric 	{
35558411Seric 		*dp++ = _res.defdname;
35658411Seric 	}
35757943Seric 	*dp = NULL;
35857205Seric 
35957205Seric 	/*
36057943Seric 	**  Now run through the search list for the name in question.
36157205Seric 	*/
36257205Seric 
36358844Seric cnameloop:
36457943Seric 	dp = searchlist;
36557943Seric 	mxmatch = NULL;
36658404Seric 	qtype = T_ANY;
36757943Seric 
36858404Seric 	for (dp = searchlist; *dp != NULL; )
36957205Seric 	{
37058411Seric 		if (qtype == T_ANY)
37158411Seric 			gotmx = FALSE;
37257943Seric 		if (tTd(8, 5))
37358508Seric 			printf("getcanonname: trying %s.%s (%s)\n", host, *dp,
37458508Seric 				qtype == T_ANY ? "ANY" : qtype == T_A ? "A" :
37558508Seric 				qtype == T_MX ? "MX" : "???");
37658404Seric 		ret = res_querydomain(host, *dp, C_IN, qtype,
37758010Seric 				      &answer, sizeof(answer));
37857943Seric 		if (ret <= 0)
37951324Seric 		{
38058796Seric 			if (tTd(8, 7))
38158082Seric 				printf("\tNO: errno=%d, h_errno=%d\n",
38258082Seric 					errno, h_errno);
38351324Seric 
38458082Seric 			if (errno == ECONNREFUSED || h_errno == TRY_AGAIN)
38557205Seric 			{
38657943Seric 				/* the name server seems to be down */
38751324Seric 				h_errno = TRY_AGAIN;
38851910Seric 				return FALSE;
38951324Seric 			}
39057943Seric 
39158501Seric 			if (h_errno != HOST_NOT_FOUND)
39258404Seric 			{
39358501Seric 				/* might have another type of interest */
39458501Seric 				if (qtype == T_ANY)
39558501Seric 				{
39658501Seric 					qtype = T_A;
39758501Seric 					continue;
39858501Seric 				}
39958501Seric 				else if (qtype == T_A && !gotmx)
40058501Seric 				{
40158501Seric 					qtype = T_MX;
40258501Seric 					continue;
40358501Seric 				}
40458404Seric 			}
40558404Seric 
40657943Seric 			if (mxmatch != NULL)
40751324Seric 			{
40857943Seric 				/* we matched before -- use that one */
40951324Seric 				break;
41051324Seric 			}
41158501Seric 
41258501Seric 			/* otherwise, try the next name */
41358501Seric 			dp++;
41458501Seric 			qtype = T_ANY;
41557943Seric 			continue;
41651324Seric 		}
41758796Seric 		else if (tTd(8, 7))
41857943Seric 			printf("\tYES\n");
41957943Seric 
42051910Seric 		/*
42157943Seric 		**  This might be a bogus match.  Search for A or
42257943Seric 		**  CNAME records.  If we don't have a matching
42357943Seric 		**  wild card MX record, we will accept MX as well.
42451910Seric 		*/
42551910Seric 
42657943Seric 		hp = (HEADER *) &answer;
42757943Seric 		ap = (u_char *) &answer + sizeof(HEADER);
42857943Seric 		eom = (u_char *) &answer + ret;
42957943Seric 
43057943Seric 		/* skip question part of response -- we know what we asked */
43157943Seric 		for (qdcount = ntohs(hp->qdcount); qdcount--; ap += ret + QFIXEDSZ)
43251324Seric 		{
43357943Seric 			if ((ret = dn_skipname(ap, eom)) < 0)
43457943Seric 			{
43557943Seric 				if (tTd(8, 20))
43657943Seric 					printf("qdcount failure (%d)\n",
43757943Seric 						ntohs(hp->qdcount));
43857943Seric 				return FALSE;		/* ???XXX??? */
43957943Seric 			}
44051324Seric 		}
44157943Seric 
44257943Seric 		amatch = FALSE;
44357943Seric 		for (ancount = ntohs(hp->ancount); --ancount >= 0 && ap < eom; ap += n)
44451324Seric 		{
44557943Seric 			n = dn_expand((u_char *) &answer, eom, ap,
44657943Seric 				      (u_char *) nbuf, sizeof nbuf);
44757943Seric 			if (n < 0)
44857943Seric 				break;
44957943Seric 			ap += n;
45057943Seric 			GETSHORT(type, ap);
45158248Seric 			ap += SHORTSIZE + LONGSIZE;
45257943Seric 			GETSHORT(n, ap);
45357943Seric 			switch (type)
45457943Seric 			{
45557943Seric 			  case T_MX:
45658411Seric 				gotmx = TRUE;
45757943Seric 				if (**dp != '\0')
45857943Seric 				{
45957943Seric 					/* got a match -- save that info */
46057943Seric 					if (mxmatch == NULL)
46157943Seric 						mxmatch = *dp;
46257943Seric 					continue;
46357943Seric 				}
46433929Sbostic 
46557943Seric 				/* exact MX matches are as good as an A match */
46657943Seric 				/* fall through */
46757205Seric 
46857943Seric 			  case T_A:
46957943Seric 				/* good show */
47057943Seric 				amatch = TRUE;
47133929Sbostic 
47257943Seric 				/* continue in case a CNAME also exists */
47357943Seric 				continue;
47457943Seric 
47557943Seric 			  case T_CNAME:
47657943Seric 				/* value points at name */
47757943Seric 				if ((ret = dn_expand((u_char *)&answer,
47857943Seric 				    eom, ap, (u_char *)nbuf, sizeof(nbuf))) < 0)
47957943Seric 					break;
48057943Seric 				(void)strncpy(host, nbuf, hbsize); /* XXX */
48157943Seric 				host[hbsize - 1] = '\0';
48257943Seric 
48358844Seric 				/*
48458844Seric 				**  RFC 1034 section 3.6 specifies that CNAME
48558844Seric 				**  should point at the canonical name -- but
48658844Seric 				**  urges software to try again anyway.
48758844Seric 				*/
48858844Seric 
48958844Seric 				goto cnameloop;
49058844Seric 
49157943Seric 			  default:
49257943Seric 				/* not a record of interest */
49357943Seric 				continue;
49457943Seric 			}
49551324Seric 		}
49633929Sbostic 
49757943Seric 		if (amatch)
49857943Seric 		{
49957943Seric 			/* got an A record and no CNAME */
50057943Seric 			mxmatch = *dp;
50129653Sbloom 			break;
50229653Sbloom 		}
50358404Seric 
50458404Seric 		/*
50558404Seric 		**  If this was a T_ANY query, we may have the info but
50658404Seric 		**  need an explicit query.  Try T_A, then T_MX.
50758404Seric 		*/
50858404Seric 
50958404Seric 		if (qtype == T_ANY)
51058404Seric 			qtype = T_A;
51158411Seric 		else if (qtype == T_A && !gotmx)
51258404Seric 			qtype = T_MX;
51358404Seric 		else
51458404Seric 		{
51558404Seric 			/* really nothing in this domain; try the next */
51658404Seric 			qtype = T_ANY;
51758404Seric 			dp++;
51858404Seric 		}
51929653Sbloom 	}
52057943Seric 
52157943Seric 	if (mxmatch == NULL)
52257943Seric 		return FALSE;
52357943Seric 
52457943Seric 	/* create matching name and return */
52557943Seric 	(void) sprintf(nbuf, "%.*s%s%.*s", MAXDNAME, host,
52657943Seric 			*mxmatch == '\0' ? "" : ".",
52757943Seric 			MAXDNAME, mxmatch);
52857943Seric 	strncpy(host, nbuf, hbsize);
52957943Seric 	host[hbsize - 1] = '\0';
53057943Seric 	return TRUE;
53129653Sbloom }
53235653Seric 
53336494Sphil #else /* not NAMED_BIND */
53436494Sphil 
53536494Sphil #include <netdb.h>
53636494Sphil 
53751314Seric bool
53836494Sphil getcanonname(host, hbsize)
53936494Sphil 	char *host;
54036494Sphil 	int hbsize;
54136494Sphil {
54236494Sphil 	struct hostent *hp;
54336494Sphil 
54436494Sphil 	hp = gethostbyname(host);
54536494Sphil 	if (hp == NULL)
54651314Seric 		return (FALSE);
54736494Sphil 
54836494Sphil 	if (strlen(hp->h_name) >= hbsize)
54951314Seric 		return (FALSE);
55036494Sphil 
55136494Sphil 	(void) strcpy(host, hp->h_name);
55251314Seric 	return (TRUE);
55336494Sphil }
55436494Sphil 
55536494Sphil #endif /* not NAMED_BIND */
556