xref: /onnv-gate/usr/src/cmd/sendmail/src/domain.c (revision 3544:8dfb1c11a5d7)
10Sstevel@tonic-gate /*
22197Sjbeck  * Copyright (c) 1998-2004, 2006 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate  *	All rights reserved.
40Sstevel@tonic-gate  * Copyright (c) 1986, 1995-1997 Eric P. Allman.  All rights reserved.
50Sstevel@tonic-gate  * Copyright (c) 1988, 1993
60Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
90Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
100Sstevel@tonic-gate  * the sendmail distribution.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
150Sstevel@tonic-gate 
160Sstevel@tonic-gate #include <sendmail.h>
17*3544Sjbeck #include "map.h"
180Sstevel@tonic-gate 
190Sstevel@tonic-gate #if NAMED_BIND
20*3544Sjbeck SM_RCSID("@(#)$Id: domain.c,v 8.202 2006/12/19 01:15:07 ca Exp $ (with name server)")
210Sstevel@tonic-gate #else /* NAMED_BIND */
22*3544Sjbeck SM_RCSID("@(#)$Id: domain.c,v 8.202 2006/12/19 01:15:07 ca Exp $ (without name server)")
230Sstevel@tonic-gate #endif /* NAMED_BIND */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #if NAMED_BIND
260Sstevel@tonic-gate 
270Sstevel@tonic-gate # include <arpa/inet.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate **  The standard udp packet size PACKETSZ (512) is not sufficient for some
320Sstevel@tonic-gate **  nameserver answers containing very many resource records. The resolver
330Sstevel@tonic-gate **  may switch to tcp and retry if it detects udp packet overflow.
340Sstevel@tonic-gate **  Also note that the resolver routines res_query and res_search return
350Sstevel@tonic-gate **  the size of the *un*truncated answer in case the supplied answer buffer
360Sstevel@tonic-gate **  it not big enough to accommodate the entire answer.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate # ifndef MAXPACKET
400Sstevel@tonic-gate #  define MAXPACKET 8192	/* max packet size used internally by BIND */
410Sstevel@tonic-gate # endif /* ! MAXPACKET */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate typedef union
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	HEADER		qb1;
460Sstevel@tonic-gate 	unsigned char	qb2[MAXPACKET];
470Sstevel@tonic-gate } querybuf;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate # ifndef MXHOSTBUFSIZE
500Sstevel@tonic-gate #  define MXHOSTBUFSIZE	(128 * MAXMXHOSTS)
510Sstevel@tonic-gate # endif /* ! MXHOSTBUFSIZE */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static char	MXHostBuf[MXHOSTBUFSIZE];
540Sstevel@tonic-gate #if (MXHOSTBUFSIZE < 2) || (MXHOSTBUFSIZE >= INT_MAX/2)
550Sstevel@tonic-gate 	ERROR: _MXHOSTBUFSIZE is out of range
560Sstevel@tonic-gate #endif /* (MXHOSTBUFSIZE < 2) || (MXHOSTBUFSIZE >= INT_MAX/2) */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate # ifndef MAXDNSRCH
590Sstevel@tonic-gate #  define MAXDNSRCH	6	/* number of possible domains to search */
600Sstevel@tonic-gate # endif /* ! MAXDNSRCH */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate # ifndef RES_DNSRCH_VARIABLE
630Sstevel@tonic-gate #  define RES_DNSRCH_VARIABLE	_res.dnsrch
640Sstevel@tonic-gate # endif /* ! RES_DNSRCH_VARIABLE */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate # ifndef NO_DATA
670Sstevel@tonic-gate #  define NO_DATA	NO_ADDRESS
680Sstevel@tonic-gate # endif /* ! NO_DATA */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate # ifndef HFIXEDSZ
710Sstevel@tonic-gate #  define HFIXEDSZ	12	/* sizeof(HEADER) */
720Sstevel@tonic-gate # endif /* ! HFIXEDSZ */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate # define MAXCNAMEDEPTH	10	/* maximum depth of CNAME recursion */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate # if defined(__RES) && (__RES >= 19940415)
770Sstevel@tonic-gate #  define RES_UNC_T	char *
780Sstevel@tonic-gate # else /* defined(__RES) && (__RES >= 19940415) */
790Sstevel@tonic-gate #  define RES_UNC_T	unsigned char *
800Sstevel@tonic-gate # endif /* defined(__RES) && (__RES >= 19940415) */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int	mxrand __P((char *));
830Sstevel@tonic-gate static int	fallbackmxrr __P((int, unsigned short *, char **));
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate **  GETFALLBACKMXRR -- get MX resource records for fallback MX host.
870Sstevel@tonic-gate **
880Sstevel@tonic-gate **	We have to initialize this once before doing anything else.
890Sstevel@tonic-gate **	Moreover, we have to repeat this from time to time to avoid
900Sstevel@tonic-gate **	stale data, e.g., in persistent queue runners.
910Sstevel@tonic-gate **	This should be done in a parent process so the child
920Sstevel@tonic-gate **	processes have the right data.
930Sstevel@tonic-gate **
940Sstevel@tonic-gate **	Parameters:
950Sstevel@tonic-gate **		host -- the name of the fallback MX host.
960Sstevel@tonic-gate **
970Sstevel@tonic-gate **	Returns:
980Sstevel@tonic-gate **		number of MX records.
990Sstevel@tonic-gate **
1000Sstevel@tonic-gate **	Side Effects:
1010Sstevel@tonic-gate **		Populates NumFallbackMXHosts and fbhosts.
1020Sstevel@tonic-gate **		Sets renewal time (based on TTL).
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int NumFallbackMXHosts = 0;	/* Number of fallback MX hosts (after MX expansion) */
1060Sstevel@tonic-gate static char *fbhosts[MAXMXHOSTS + 1];
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate int
getfallbackmxrr(host)1090Sstevel@tonic-gate getfallbackmxrr(host)
1100Sstevel@tonic-gate 	char *host;
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	int i, rcode;
1130Sstevel@tonic-gate 	int ttl;
1140Sstevel@tonic-gate 	static time_t renew = 0;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #if 0
1170Sstevel@tonic-gate 	/* This is currently done before this function is called. */
1180Sstevel@tonic-gate 	if (host == NULL || *host == '\0')
1190Sstevel@tonic-gate 		return 0;
1200Sstevel@tonic-gate #endif /* 0 */
1210Sstevel@tonic-gate 	if (NumFallbackMXHosts > 0 && renew > curtime())
1220Sstevel@tonic-gate 		return NumFallbackMXHosts;
1230Sstevel@tonic-gate 	if (host[0] == '[')
1240Sstevel@tonic-gate 	{
1250Sstevel@tonic-gate 		fbhosts[0] = host;
1260Sstevel@tonic-gate 		NumFallbackMXHosts = 1;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	else
1290Sstevel@tonic-gate 	{
1300Sstevel@tonic-gate 		/* free old data */
1310Sstevel@tonic-gate 		for (i = 0; i < NumFallbackMXHosts; i++)
1320Sstevel@tonic-gate 			sm_free(fbhosts[i]);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		/* get new data */
1350Sstevel@tonic-gate 		NumFallbackMXHosts = getmxrr(host, fbhosts, NULL, false,
1360Sstevel@tonic-gate 					     &rcode, false, &ttl);
1370Sstevel@tonic-gate 		renew = curtime() + ttl;
1380Sstevel@tonic-gate 		for (i = 0; i < NumFallbackMXHosts; i++)
1390Sstevel@tonic-gate 			fbhosts[i] = newstr(fbhosts[i]);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	return NumFallbackMXHosts;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate **  FALLBACKMXRR -- add MX resource records for fallback MX host to list.
1460Sstevel@tonic-gate **
1470Sstevel@tonic-gate **	Parameters:
1480Sstevel@tonic-gate **		nmx -- current number of MX records.
1490Sstevel@tonic-gate **		prefs -- array of preferences.
1500Sstevel@tonic-gate **		mxhosts -- array of MX hosts (maximum size: MAXMXHOSTS)
1510Sstevel@tonic-gate **
1520Sstevel@tonic-gate **	Returns:
1530Sstevel@tonic-gate **		new number of MX records.
1540Sstevel@tonic-gate **
1550Sstevel@tonic-gate **	Side Effects:
1560Sstevel@tonic-gate **		If FallbackMX was set, it appends the MX records for
1570Sstevel@tonic-gate **		that host to mxhosts (and modifies prefs accordingly).
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static int
fallbackmxrr(nmx,prefs,mxhosts)1610Sstevel@tonic-gate fallbackmxrr(nmx, prefs, mxhosts)
1620Sstevel@tonic-gate 	int nmx;
1630Sstevel@tonic-gate 	unsigned short *prefs;
1640Sstevel@tonic-gate 	char **mxhosts;
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	int i;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	for (i = 0; i < NumFallbackMXHosts && nmx < MAXMXHOSTS; i++)
1690Sstevel@tonic-gate 	{
1700Sstevel@tonic-gate 		if (nmx > 0)
1710Sstevel@tonic-gate 			prefs[nmx] = prefs[nmx - 1] + 1;
1720Sstevel@tonic-gate 		else
1730Sstevel@tonic-gate 			prefs[nmx] = 0;
1740Sstevel@tonic-gate 		mxhosts[nmx++] = fbhosts[i];
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	return nmx;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate **  GETMXRR -- get MX resource records for a domain
1810Sstevel@tonic-gate **
1820Sstevel@tonic-gate **	Parameters:
1830Sstevel@tonic-gate **		host -- the name of the host to MX.
1840Sstevel@tonic-gate **		mxhosts -- a pointer to a return buffer of MX records.
1850Sstevel@tonic-gate **		mxprefs -- a pointer to a return buffer of MX preferences.
1860Sstevel@tonic-gate **			If NULL, don't try to populate.
1870Sstevel@tonic-gate **		droplocalhost -- If true, all MX records less preferred
1880Sstevel@tonic-gate **			than the local host (as determined by $=w) will
1890Sstevel@tonic-gate **			be discarded.
1900Sstevel@tonic-gate **		rcode -- a pointer to an EX_ status code.
1910Sstevel@tonic-gate **		tryfallback -- add also fallback MX host?
1920Sstevel@tonic-gate **		pttl -- pointer to return TTL (can be NULL).
1930Sstevel@tonic-gate **
1940Sstevel@tonic-gate **	Returns:
1950Sstevel@tonic-gate **		The number of MX records found.
1960Sstevel@tonic-gate **		-1 if there is an internal failure.
1970Sstevel@tonic-gate **		If no MX records are found, mxhosts[0] is set to host
1980Sstevel@tonic-gate **			and 1 is returned.
1990Sstevel@tonic-gate **
2000Sstevel@tonic-gate **	Side Effects:
2010Sstevel@tonic-gate **		The entries made for mxhosts point to a static array
2020Sstevel@tonic-gate **		MXHostBuf[MXHOSTBUFSIZE], so the data needs to be copied,
2030Sstevel@tonic-gate **		if it must be preserved across calls to this function.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int
getmxrr(host,mxhosts,mxprefs,droplocalhost,rcode,tryfallback,pttl)2070Sstevel@tonic-gate getmxrr(host, mxhosts, mxprefs, droplocalhost, rcode, tryfallback, pttl)
2080Sstevel@tonic-gate 	char *host;
2090Sstevel@tonic-gate 	char **mxhosts;
2100Sstevel@tonic-gate 	unsigned short *mxprefs;
2110Sstevel@tonic-gate 	bool droplocalhost;
2120Sstevel@tonic-gate 	int *rcode;
2130Sstevel@tonic-gate 	bool tryfallback;
2140Sstevel@tonic-gate 	int *pttl;
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	register unsigned char *eom, *cp;
2170Sstevel@tonic-gate 	register int i, j, n;
2180Sstevel@tonic-gate 	int nmx = 0;
2190Sstevel@tonic-gate 	register char *bp;
2200Sstevel@tonic-gate 	HEADER *hp;
2210Sstevel@tonic-gate 	querybuf answer;
2220Sstevel@tonic-gate 	int ancount, qdcount, buflen;
2230Sstevel@tonic-gate 	bool seenlocal = false;
2240Sstevel@tonic-gate 	unsigned short pref, type;
2250Sstevel@tonic-gate 	unsigned short localpref = 256;
2260Sstevel@tonic-gate 	char *fallbackMX = FallbackMX;
2270Sstevel@tonic-gate 	bool trycanon = false;
2280Sstevel@tonic-gate 	unsigned short *prefs;
2290Sstevel@tonic-gate 	int (*resfunc) __P((const char *, int, int, u_char *, int));
2300Sstevel@tonic-gate 	unsigned short prefer[MAXMXHOSTS];
2310Sstevel@tonic-gate 	int weight[MAXMXHOSTS];
2320Sstevel@tonic-gate 	int ttl = 0;
2330Sstevel@tonic-gate 	extern int res_query(), res_search();
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (tTd(8, 2))
2360Sstevel@tonic-gate 		sm_dprintf("getmxrr(%s, droplocalhost=%d)\n",
2370Sstevel@tonic-gate 			   host, droplocalhost);
2380Sstevel@tonic-gate 	*rcode = EX_OK;
2390Sstevel@tonic-gate 	if (pttl != NULL)
2400Sstevel@tonic-gate 		*pttl = SM_DEFAULT_TTL;
2410Sstevel@tonic-gate 	if (*host == '\0')
2420Sstevel@tonic-gate 		return 0;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if ((fallbackMX != NULL && droplocalhost &&
2450Sstevel@tonic-gate 	     wordinclass(fallbackMX, 'w')) || !tryfallback)
2460Sstevel@tonic-gate 	{
2470Sstevel@tonic-gate 		/* don't use fallback for this pass */
2480Sstevel@tonic-gate 		fallbackMX = NULL;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (mxprefs != NULL)
2520Sstevel@tonic-gate 		prefs = mxprefs;
2530Sstevel@tonic-gate 	else
2540Sstevel@tonic-gate 		prefs = prefer;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* efficiency hack -- numeric or non-MX lookups */
2570Sstevel@tonic-gate 	if (host[0] == '[')
2580Sstevel@tonic-gate 		goto punt;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	**  If we don't have MX records in our host switch, don't
2620Sstevel@tonic-gate 	**  try for MX records.  Note that this really isn't "right",
2630Sstevel@tonic-gate 	**  since we might be set up to try NIS first and then DNS;
2640Sstevel@tonic-gate 	**  if the host is found in NIS we really shouldn't be doing
2650Sstevel@tonic-gate 	**  MX lookups.  However, that should be a degenerate case.
2660Sstevel@tonic-gate 	*/
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (!UseNameServer)
2690Sstevel@tonic-gate 		goto punt;
2700Sstevel@tonic-gate 	if (HasWildcardMX && ConfigLevel >= 6)
2710Sstevel@tonic-gate 		resfunc = res_query;
2720Sstevel@tonic-gate 	else
2730Sstevel@tonic-gate 		resfunc = res_search;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	errno = 0;
2760Sstevel@tonic-gate 	n = (*resfunc)(host, C_IN, T_MX, (unsigned char *) &answer,
2770Sstevel@tonic-gate 		       sizeof(answer));
2780Sstevel@tonic-gate 	if (n < 0)
2790Sstevel@tonic-gate 	{
2800Sstevel@tonic-gate 		if (tTd(8, 1))
2810Sstevel@tonic-gate 			sm_dprintf("getmxrr: res_search(%s) failed (errno=%d, h_errno=%d)\n",
282*3544Sjbeck 				host, errno, h_errno);
2830Sstevel@tonic-gate 		switch (h_errno)
2840Sstevel@tonic-gate 		{
2850Sstevel@tonic-gate 		  case NO_DATA:
2860Sstevel@tonic-gate 			trycanon = true;
2870Sstevel@tonic-gate 			/* FALLTHROUGH */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		  case NO_RECOVERY:
2900Sstevel@tonic-gate 			/* no MX data on this host */
2910Sstevel@tonic-gate 			goto punt;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		  case HOST_NOT_FOUND:
2940Sstevel@tonic-gate # if BROKEN_RES_SEARCH
2950Sstevel@tonic-gate 		  case 0:	/* Ultrix resolver retns failure w/ h_errno=0 */
2960Sstevel@tonic-gate # endif /* BROKEN_RES_SEARCH */
2970Sstevel@tonic-gate 			/* host doesn't exist in DNS; might be in /etc/hosts */
2980Sstevel@tonic-gate 			trycanon = true;
2990Sstevel@tonic-gate 			*rcode = EX_NOHOST;
3000Sstevel@tonic-gate 			goto punt;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		  case TRY_AGAIN:
3030Sstevel@tonic-gate 		  case -1:
3040Sstevel@tonic-gate 			/* couldn't connect to the name server */
3050Sstevel@tonic-gate 			if (fallbackMX != NULL)
3060Sstevel@tonic-gate 			{
3070Sstevel@tonic-gate 				/* name server is hosed -- push to fallback */
3080Sstevel@tonic-gate 				return fallbackmxrr(nmx, prefs, mxhosts);
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 			/* it might come up later; better queue it up */
3110Sstevel@tonic-gate 			*rcode = EX_TEMPFAIL;
3120Sstevel@tonic-gate 			break;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		  default:
3150Sstevel@tonic-gate 			syserr("getmxrr: res_search (%s) failed with impossible h_errno (%d)",
3160Sstevel@tonic-gate 				host, h_errno);
3170Sstevel@tonic-gate 			*rcode = EX_OSERR;
3180Sstevel@tonic-gate 			break;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		/* irreconcilable differences */
3220Sstevel@tonic-gate 		return -1;
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/* avoid problems after truncation in tcp packets */
3260Sstevel@tonic-gate 	if (n > sizeof(answer))
3270Sstevel@tonic-gate 		n = sizeof(answer);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/* find first satisfactory answer */
3300Sstevel@tonic-gate 	hp = (HEADER *)&answer;
3310Sstevel@tonic-gate 	cp = (unsigned char *)&answer + HFIXEDSZ;
3320Sstevel@tonic-gate 	eom = (unsigned char *)&answer + n;
3330Sstevel@tonic-gate 	for (qdcount = ntohs((unsigned short) hp->qdcount);
3340Sstevel@tonic-gate 	     qdcount--;
3350Sstevel@tonic-gate 	     cp += n + QFIXEDSZ)
3360Sstevel@tonic-gate 	{
3370Sstevel@tonic-gate 		if ((n = dn_skipname(cp, eom)) < 0)
3380Sstevel@tonic-gate 			goto punt;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/* NOTE: see definition of MXHostBuf! */
3420Sstevel@tonic-gate 	buflen = sizeof(MXHostBuf) - 1;
3430Sstevel@tonic-gate 	SM_ASSERT(buflen > 0);
3440Sstevel@tonic-gate 	bp = MXHostBuf;
3450Sstevel@tonic-gate 	ancount = ntohs((unsigned short) hp->ancount);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/* See RFC 1035 for layout of RRs. */
3480Sstevel@tonic-gate 	/* XXX leave room for FallbackMX ? */
3490Sstevel@tonic-gate 	while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS - 1)
3500Sstevel@tonic-gate 	{
3510Sstevel@tonic-gate 		if ((n = dn_expand((unsigned char *)&answer, eom, cp,
3520Sstevel@tonic-gate 				   (RES_UNC_T) bp, buflen)) < 0)
3530Sstevel@tonic-gate 			break;
3540Sstevel@tonic-gate 		cp += n;
3550Sstevel@tonic-gate 		GETSHORT(type, cp);
3560Sstevel@tonic-gate 		cp += INT16SZ;		/* skip over class */
3570Sstevel@tonic-gate 		GETLONG(ttl, cp);
3580Sstevel@tonic-gate 		GETSHORT(n, cp);	/* rdlength */
3590Sstevel@tonic-gate 		if (type != T_MX)
3600Sstevel@tonic-gate 		{
3610Sstevel@tonic-gate 			if (tTd(8, 8) || _res.options & RES_DEBUG)
3620Sstevel@tonic-gate 				sm_dprintf("unexpected answer type %d, size %d\n",
3630Sstevel@tonic-gate 					type, n);
3640Sstevel@tonic-gate 			cp += n;
3650Sstevel@tonic-gate 			continue;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 		GETSHORT(pref, cp);
3680Sstevel@tonic-gate 		if ((n = dn_expand((unsigned char *)&answer, eom, cp,
3690Sstevel@tonic-gate 				   (RES_UNC_T) bp, buflen)) < 0)
3700Sstevel@tonic-gate 			break;
3710Sstevel@tonic-gate 		cp += n;
3720Sstevel@tonic-gate 		n = strlen(bp);
3730Sstevel@tonic-gate # if 0
3740Sstevel@tonic-gate 		/* Can this happen? */
3750Sstevel@tonic-gate 		if (n == 0)
3760Sstevel@tonic-gate 		{
3770Sstevel@tonic-gate 			if (LogLevel > 4)
3780Sstevel@tonic-gate 				sm_syslog(LOG_ERR, NOQID,
3790Sstevel@tonic-gate 					  "MX records for %s contain empty string",
3800Sstevel@tonic-gate 					  host);
3810Sstevel@tonic-gate 			continue;
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate # endif /* 0 */
3840Sstevel@tonic-gate 		if (wordinclass(bp, 'w'))
3850Sstevel@tonic-gate 		{
3860Sstevel@tonic-gate 			if (tTd(8, 3))
3870Sstevel@tonic-gate 				sm_dprintf("found localhost (%s) in MX list, pref=%d\n",
3880Sstevel@tonic-gate 					bp, pref);
3890Sstevel@tonic-gate 			if (droplocalhost)
3900Sstevel@tonic-gate 			{
3910Sstevel@tonic-gate 				if (!seenlocal || pref < localpref)
3920Sstevel@tonic-gate 					localpref = pref;
3930Sstevel@tonic-gate 				seenlocal = true;
3940Sstevel@tonic-gate 				continue;
3950Sstevel@tonic-gate 			}
3960Sstevel@tonic-gate 			weight[nmx] = 0;
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		else
3990Sstevel@tonic-gate 			weight[nmx] = mxrand(bp);
4000Sstevel@tonic-gate 		prefs[nmx] = pref;
4010Sstevel@tonic-gate 		mxhosts[nmx++] = bp;
4020Sstevel@tonic-gate 		bp += n;
4030Sstevel@tonic-gate 		if (bp[-1] != '.')
4040Sstevel@tonic-gate 		{
4050Sstevel@tonic-gate 			*bp++ = '.';
4060Sstevel@tonic-gate 			n++;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 		*bp++ = '\0';
4090Sstevel@tonic-gate 		if (buflen < n + 1)
4100Sstevel@tonic-gate 		{
4110Sstevel@tonic-gate 			/* don't want to wrap buflen */
4120Sstevel@tonic-gate 			break;
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		buflen -= n + 1;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/* return only one TTL entry, that should be sufficient */
4180Sstevel@tonic-gate 	if (ttl > 0 && pttl != NULL)
4190Sstevel@tonic-gate 		*pttl = ttl;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/* sort the records */
4220Sstevel@tonic-gate 	for (i = 0; i < nmx; i++)
4230Sstevel@tonic-gate 	{
4240Sstevel@tonic-gate 		for (j = i + 1; j < nmx; j++)
4250Sstevel@tonic-gate 		{
4260Sstevel@tonic-gate 			if (prefs[i] > prefs[j] ||
4270Sstevel@tonic-gate 			    (prefs[i] == prefs[j] && weight[i] > weight[j]))
4280Sstevel@tonic-gate 			{
4290Sstevel@tonic-gate 				register int temp;
4300Sstevel@tonic-gate 				register char *temp1;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 				temp = prefs[i];
4330Sstevel@tonic-gate 				prefs[i] = prefs[j];
4340Sstevel@tonic-gate 				prefs[j] = temp;
4350Sstevel@tonic-gate 				temp1 = mxhosts[i];
4360Sstevel@tonic-gate 				mxhosts[i] = mxhosts[j];
4370Sstevel@tonic-gate 				mxhosts[j] = temp1;
4380Sstevel@tonic-gate 				temp = weight[i];
4390Sstevel@tonic-gate 				weight[i] = weight[j];
4400Sstevel@tonic-gate 				weight[j] = temp;
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		if (seenlocal && prefs[i] >= localpref)
4440Sstevel@tonic-gate 		{
4450Sstevel@tonic-gate 			/* truncate higher preference part of list */
4460Sstevel@tonic-gate 			nmx = i;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/* delete duplicates from list (yes, some bozos have duplicates) */
4510Sstevel@tonic-gate 	for (i = 0; i < nmx - 1; )
4520Sstevel@tonic-gate 	{
4530Sstevel@tonic-gate 		if (sm_strcasecmp(mxhosts[i], mxhosts[i + 1]) != 0)
4540Sstevel@tonic-gate 			i++;
4550Sstevel@tonic-gate 		else
4560Sstevel@tonic-gate 		{
4570Sstevel@tonic-gate 			/* compress out duplicate */
4580Sstevel@tonic-gate 			for (j = i + 1; j < nmx; j++)
4590Sstevel@tonic-gate 			{
4600Sstevel@tonic-gate 				mxhosts[j] = mxhosts[j + 1];
4610Sstevel@tonic-gate 				prefs[j] = prefs[j + 1];
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 			nmx--;
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (nmx == 0)
4680Sstevel@tonic-gate 	{
4690Sstevel@tonic-gate punt:
4700Sstevel@tonic-gate 		if (seenlocal)
4710Sstevel@tonic-gate 		{
4720Sstevel@tonic-gate 			struct hostent *h = NULL;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 			/*
4750Sstevel@tonic-gate 			**  If we have deleted all MX entries, this is
4760Sstevel@tonic-gate 			**  an error -- we should NEVER send to a host that
4770Sstevel@tonic-gate 			**  has an MX, and this should have been caught
4780Sstevel@tonic-gate 			**  earlier in the config file.
4790Sstevel@tonic-gate 			**
4800Sstevel@tonic-gate 			**  Some sites prefer to go ahead and try the
4810Sstevel@tonic-gate 			**  A record anyway; that case is handled by
4820Sstevel@tonic-gate 			**  setting TryNullMXList.  I believe this is a
4830Sstevel@tonic-gate 			**  bad idea, but it's up to you....
4840Sstevel@tonic-gate 			*/
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 			if (TryNullMXList)
4870Sstevel@tonic-gate 			{
4880Sstevel@tonic-gate 				SM_SET_H_ERRNO(0);
4890Sstevel@tonic-gate 				errno = 0;
4900Sstevel@tonic-gate 				h = sm_gethostbyname(host, AF_INET);
4910Sstevel@tonic-gate 				if (h == NULL)
4920Sstevel@tonic-gate 				{
4930Sstevel@tonic-gate 					if (errno == ETIMEDOUT ||
4940Sstevel@tonic-gate 					    h_errno == TRY_AGAIN ||
4950Sstevel@tonic-gate 					    (errno == ECONNREFUSED &&
4960Sstevel@tonic-gate 					     UseNameServer))
4970Sstevel@tonic-gate 					{
4980Sstevel@tonic-gate 						*rcode = EX_TEMPFAIL;
4990Sstevel@tonic-gate 						return -1;
5000Sstevel@tonic-gate 					}
5010Sstevel@tonic-gate # if NETINET6
5020Sstevel@tonic-gate 					SM_SET_H_ERRNO(0);
5030Sstevel@tonic-gate 					errno = 0;
5040Sstevel@tonic-gate 					h = sm_gethostbyname(host, AF_INET6);
5050Sstevel@tonic-gate 					if (h == NULL &&
5060Sstevel@tonic-gate 					    (errno == ETIMEDOUT ||
5070Sstevel@tonic-gate 					     h_errno == TRY_AGAIN ||
5080Sstevel@tonic-gate 					     (errno == ECONNREFUSED &&
5090Sstevel@tonic-gate 					      UseNameServer)))
5100Sstevel@tonic-gate 					{
5110Sstevel@tonic-gate 						*rcode = EX_TEMPFAIL;
5120Sstevel@tonic-gate 						return -1;
5130Sstevel@tonic-gate 					}
5140Sstevel@tonic-gate # endif /* NETINET6 */
5150Sstevel@tonic-gate 				}
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 			if (h == NULL)
5190Sstevel@tonic-gate 			{
5200Sstevel@tonic-gate 				*rcode = EX_CONFIG;
5210Sstevel@tonic-gate 				syserr("MX list for %s points back to %s",
5220Sstevel@tonic-gate 				       host, MyHostName);
5230Sstevel@tonic-gate 				return -1;
5240Sstevel@tonic-gate 			}
5250Sstevel@tonic-gate # if NETINET6
5260Sstevel@tonic-gate 			freehostent(h);
5272197Sjbeck 			h = NULL;
5280Sstevel@tonic-gate # endif /* NETINET6 */
5290Sstevel@tonic-gate 		}
530*3544Sjbeck 		if (strlen(host) >= sizeof(MXHostBuf))
5310Sstevel@tonic-gate 		{
5320Sstevel@tonic-gate 			*rcode = EX_CONFIG;
5330Sstevel@tonic-gate 			syserr("Host name %s too long",
5340Sstevel@tonic-gate 			       shortenstring(host, MAXSHORTSTR));
5350Sstevel@tonic-gate 			return -1;
5360Sstevel@tonic-gate 		}
537*3544Sjbeck 		(void) sm_strlcpy(MXHostBuf, host, sizeof(MXHostBuf));
5380Sstevel@tonic-gate 		mxhosts[0] = MXHostBuf;
5390Sstevel@tonic-gate 		prefs[0] = 0;
5400Sstevel@tonic-gate 		if (host[0] == '[')
5410Sstevel@tonic-gate 		{
5420Sstevel@tonic-gate 			register char *p;
5430Sstevel@tonic-gate # if NETINET6
5440Sstevel@tonic-gate 			struct sockaddr_in6 tmp6;
5450Sstevel@tonic-gate # endif /* NETINET6 */
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 			/* this may be an MX suppression-style address */
5480Sstevel@tonic-gate 			p = strchr(MXHostBuf, ']');
5490Sstevel@tonic-gate 			if (p != NULL)
5500Sstevel@tonic-gate 			{
5510Sstevel@tonic-gate 				*p = '\0';
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 				if (inet_addr(&MXHostBuf[1]) != INADDR_NONE)
5540Sstevel@tonic-gate 				{
5550Sstevel@tonic-gate 					nmx++;
5560Sstevel@tonic-gate 					*p = ']';
5570Sstevel@tonic-gate 				}
5580Sstevel@tonic-gate # if NETINET6
5590Sstevel@tonic-gate 				else if (anynet_pton(AF_INET6, &MXHostBuf[1],
5600Sstevel@tonic-gate 						     &tmp6.sin6_addr) == 1)
5610Sstevel@tonic-gate 				{
5620Sstevel@tonic-gate 					nmx++;
5630Sstevel@tonic-gate 					*p = ']';
5640Sstevel@tonic-gate 				}
5650Sstevel@tonic-gate # endif /* NETINET6 */
5660Sstevel@tonic-gate 				else
5670Sstevel@tonic-gate 				{
5680Sstevel@tonic-gate 					trycanon = true;
5690Sstevel@tonic-gate 					mxhosts[0]++;
5700Sstevel@tonic-gate 				}
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 		if (trycanon &&
574*3544Sjbeck 		    getcanonname(mxhosts[0], sizeof(MXHostBuf) - 2, false, pttl))
5750Sstevel@tonic-gate 		{
5760Sstevel@tonic-gate 			/* XXX MXHostBuf == "" ?  is that possible? */
5770Sstevel@tonic-gate 			bp = &MXHostBuf[strlen(MXHostBuf)];
5780Sstevel@tonic-gate 			if (bp[-1] != '.')
5790Sstevel@tonic-gate 			{
5800Sstevel@tonic-gate 				*bp++ = '.';
5810Sstevel@tonic-gate 				*bp = '\0';
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 			nmx = 1;
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/* if we have a default lowest preference, include that */
5880Sstevel@tonic-gate 	if (fallbackMX != NULL && !seenlocal)
5890Sstevel@tonic-gate 	{
5900Sstevel@tonic-gate 		nmx = fallbackmxrr(nmx, prefs, mxhosts);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	return nmx;
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate **  MXRAND -- create a randomizer for equal MX preferences
5960Sstevel@tonic-gate **
5970Sstevel@tonic-gate **	If two MX hosts have equal preferences we want to randomize
5980Sstevel@tonic-gate **	the selection.  But in order for signatures to be the same,
5990Sstevel@tonic-gate **	we need to randomize the same way each time.  This function
6000Sstevel@tonic-gate **	computes a pseudo-random hash function from the host name.
6010Sstevel@tonic-gate **
6020Sstevel@tonic-gate **	Parameters:
6030Sstevel@tonic-gate **		host -- the name of the host.
6040Sstevel@tonic-gate **
6050Sstevel@tonic-gate **	Returns:
6060Sstevel@tonic-gate **		A random but repeatable value based on the host name.
6070Sstevel@tonic-gate */
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate static int
mxrand(host)6100Sstevel@tonic-gate mxrand(host)
6110Sstevel@tonic-gate 	register char *host;
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	int hfunc;
6140Sstevel@tonic-gate 	static unsigned int seed;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (seed == 0)
6170Sstevel@tonic-gate 	{
6180Sstevel@tonic-gate 		seed = (int) curtime() & 0xffff;
6190Sstevel@tonic-gate 		if (seed == 0)
6200Sstevel@tonic-gate 			seed++;
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (tTd(17, 9))
6240Sstevel@tonic-gate 		sm_dprintf("mxrand(%s)", host);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	hfunc = seed;
6270Sstevel@tonic-gate 	while (*host != '\0')
6280Sstevel@tonic-gate 	{
6290Sstevel@tonic-gate 		int c = *host++;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		if (isascii(c) && isupper(c))
6320Sstevel@tonic-gate 			c = tolower(c);
6330Sstevel@tonic-gate 		hfunc = ((hfunc << 1) ^ c) % 2003;
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	hfunc &= 0xff;
6370Sstevel@tonic-gate 	hfunc++;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (tTd(17, 9))
6400Sstevel@tonic-gate 		sm_dprintf(" = %d\n", hfunc);
6410Sstevel@tonic-gate 	return hfunc;
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate **  BESTMX -- find the best MX for a name
6450Sstevel@tonic-gate **
6460Sstevel@tonic-gate **	This is really a hack, but I don't see any obvious way
6470Sstevel@tonic-gate **	to generalize it at the moment.
6480Sstevel@tonic-gate */
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /* ARGSUSED3 */
6510Sstevel@tonic-gate char *
bestmx_map_lookup(map,name,av,statp)6520Sstevel@tonic-gate bestmx_map_lookup(map, name, av, statp)
6530Sstevel@tonic-gate 	MAP *map;
6540Sstevel@tonic-gate 	char *name;
6550Sstevel@tonic-gate 	char **av;
6560Sstevel@tonic-gate 	int *statp;
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	int nmx;
6590Sstevel@tonic-gate 	int saveopts = _res.options;
6600Sstevel@tonic-gate 	int i;
6610Sstevel@tonic-gate 	ssize_t len = 0;
6620Sstevel@tonic-gate 	char *result;
6630Sstevel@tonic-gate 	char *mxhosts[MAXMXHOSTS + 1];
6640Sstevel@tonic-gate #if _FFR_BESTMX_BETTER_TRUNCATION
6650Sstevel@tonic-gate 	char *buf;
6660Sstevel@tonic-gate #else /* _FFR_BESTMX_BETTER_TRUNCATION */
6670Sstevel@tonic-gate 	char *p;
6680Sstevel@tonic-gate 	char buf[PSBUFSIZE / 2];
6690Sstevel@tonic-gate #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	_res.options &= ~(RES_DNSRCH|RES_DEFNAMES);
6720Sstevel@tonic-gate 	nmx = getmxrr(name, mxhosts, NULL, false, statp, false, NULL);
6730Sstevel@tonic-gate 	_res.options = saveopts;
6740Sstevel@tonic-gate 	if (nmx <= 0)
6750Sstevel@tonic-gate 		return NULL;
6760Sstevel@tonic-gate 	if (bitset(MF_MATCHONLY, map->map_mflags))
6770Sstevel@tonic-gate 		return map_rewrite(map, name, strlen(name), NULL);
6780Sstevel@tonic-gate 	if ((map->map_coldelim == '\0') || (nmx == 1))
6790Sstevel@tonic-gate 		return map_rewrite(map, mxhosts[0], strlen(mxhosts[0]), av);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	**  We were given a -z flag (return all MXs) and there are multiple
6830Sstevel@tonic-gate 	**  ones.  We need to build them all into a list.
6840Sstevel@tonic-gate 	*/
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate #if _FFR_BESTMX_BETTER_TRUNCATION
6870Sstevel@tonic-gate 	for (i = 0; i < nmx; i++)
6880Sstevel@tonic-gate 	{
6890Sstevel@tonic-gate 		if (strchr(mxhosts[i], map->map_coldelim) != NULL)
6900Sstevel@tonic-gate 		{
6910Sstevel@tonic-gate 			syserr("bestmx_map_lookup: MX host %.64s includes map delimiter character 0x%02X",
6920Sstevel@tonic-gate 			       mxhosts[i], map->map_coldelim);
6930Sstevel@tonic-gate 			return NULL;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		len += strlen(mxhosts[i]) + 1;
6960Sstevel@tonic-gate 		if (len < 0)
6970Sstevel@tonic-gate 		{
6980Sstevel@tonic-gate 			len -= strlen(mxhosts[i]) + 1;
6990Sstevel@tonic-gate 			break;
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	buf = (char *) sm_malloc(len);
7030Sstevel@tonic-gate 	if (buf == NULL)
7040Sstevel@tonic-gate 	{
7050Sstevel@tonic-gate 		*statp = EX_UNAVAILABLE;
7060Sstevel@tonic-gate 		return NULL;
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 	*buf = '\0';
7090Sstevel@tonic-gate 	for (i = 0; i < nmx; i++)
7100Sstevel@tonic-gate 	{
7110Sstevel@tonic-gate 		int end;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 		end = sm_strlcat(buf, mxhosts[i], len);
7140Sstevel@tonic-gate 		if (i != nmx && end + 1 < len)
7150Sstevel@tonic-gate 		{
7160Sstevel@tonic-gate 			buf[end] = map->map_coldelim;
7170Sstevel@tonic-gate 			buf[end + 1] = '\0';
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* Cleanly truncate for rulesets */
7220Sstevel@tonic-gate 	truncate_at_delim(buf, PSBUFSIZE / 2, map->map_coldelim);
7230Sstevel@tonic-gate #else /* _FFR_BESTMX_BETTER_TRUNCATION */
7240Sstevel@tonic-gate 	p = buf;
7250Sstevel@tonic-gate 	for (i = 0; i < nmx; i++)
7260Sstevel@tonic-gate 	{
7270Sstevel@tonic-gate 		size_t slen;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (strchr(mxhosts[i], map->map_coldelim) != NULL)
7300Sstevel@tonic-gate 		{
7310Sstevel@tonic-gate 			syserr("bestmx_map_lookup: MX host %.64s includes map delimiter character 0x%02X",
7320Sstevel@tonic-gate 			       mxhosts[i], map->map_coldelim);
7330Sstevel@tonic-gate 			return NULL;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 		slen = strlen(mxhosts[i]);
736*3544Sjbeck 		if (len + slen + 2 > sizeof(buf))
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		if (i > 0)
7390Sstevel@tonic-gate 		{
7400Sstevel@tonic-gate 			*p++ = map->map_coldelim;
7410Sstevel@tonic-gate 			len++;
7420Sstevel@tonic-gate 		}
743*3544Sjbeck 		(void) sm_strlcpy(p, mxhosts[i], sizeof(buf) - len);
7440Sstevel@tonic-gate 		p += slen;
7450Sstevel@tonic-gate 		len += slen;
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	result = map_rewrite(map, buf, len, av);
7500Sstevel@tonic-gate #if _FFR_BESTMX_BETTER_TRUNCATION
7510Sstevel@tonic-gate 	sm_free(buf);
7520Sstevel@tonic-gate #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
7530Sstevel@tonic-gate 	return result;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate **  DNS_GETCANONNAME -- get the canonical name for named host using DNS
7570Sstevel@tonic-gate **
7580Sstevel@tonic-gate **	This algorithm tries to be smart about wildcard MX records.
7590Sstevel@tonic-gate **	This is hard to do because DNS doesn't tell is if we matched
7600Sstevel@tonic-gate **	against a wildcard or a specific MX.
7610Sstevel@tonic-gate **
7620Sstevel@tonic-gate **	We always prefer A & CNAME records, since these are presumed
7630Sstevel@tonic-gate **	to be specific.
7640Sstevel@tonic-gate **
7650Sstevel@tonic-gate **	If we match an MX in one pass and lose it in the next, we use
7660Sstevel@tonic-gate **	the old one.  For example, consider an MX matching *.FOO.BAR.COM.
7670Sstevel@tonic-gate **	A hostname bletch.foo.bar.com will match against this MX, but
7680Sstevel@tonic-gate **	will stop matching when we try bletch.bar.com -- so we know
7690Sstevel@tonic-gate **	that bletch.foo.bar.com must have been right.  This fails if
7700Sstevel@tonic-gate **	there was also an MX record matching *.BAR.COM, but there are
7710Sstevel@tonic-gate **	some things that just can't be fixed.
7720Sstevel@tonic-gate **
7730Sstevel@tonic-gate **	Parameters:
7740Sstevel@tonic-gate **		host -- a buffer containing the name of the host.
7750Sstevel@tonic-gate **			This is a value-result parameter.
7760Sstevel@tonic-gate **		hbsize -- the size of the host buffer.
7770Sstevel@tonic-gate **		trymx -- if set, try MX records as well as A and CNAME.
7780Sstevel@tonic-gate **		statp -- pointer to place to store status.
7790Sstevel@tonic-gate **		pttl -- pointer to return TTL (can be NULL).
7800Sstevel@tonic-gate **
7810Sstevel@tonic-gate **	Returns:
7820Sstevel@tonic-gate **		true -- if the host matched.
7830Sstevel@tonic-gate **		false -- otherwise.
7840Sstevel@tonic-gate */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate bool
dns_getcanonname(host,hbsize,trymx,statp,pttl)7870Sstevel@tonic-gate dns_getcanonname(host, hbsize, trymx, statp, pttl)
7880Sstevel@tonic-gate 	char *host;
7890Sstevel@tonic-gate 	int hbsize;
7900Sstevel@tonic-gate 	bool trymx;
7910Sstevel@tonic-gate 	int *statp;
7920Sstevel@tonic-gate 	int *pttl;
7930Sstevel@tonic-gate {
7940Sstevel@tonic-gate 	register unsigned char *eom, *ap;
7950Sstevel@tonic-gate 	register char *cp;
7960Sstevel@tonic-gate 	register int n;
7970Sstevel@tonic-gate 	HEADER *hp;
7980Sstevel@tonic-gate 	querybuf answer;
7990Sstevel@tonic-gate 	int ancount, qdcount;
8000Sstevel@tonic-gate 	int ret;
8010Sstevel@tonic-gate 	char **domain;
8020Sstevel@tonic-gate 	int type;
8030Sstevel@tonic-gate 	int ttl = 0;
8040Sstevel@tonic-gate 	char **dp;
8050Sstevel@tonic-gate 	char *mxmatch;
8060Sstevel@tonic-gate 	bool amatch;
8070Sstevel@tonic-gate 	bool gotmx = false;
8080Sstevel@tonic-gate 	int qtype;
8090Sstevel@tonic-gate 	int initial;
8100Sstevel@tonic-gate 	int loopcnt;
8110Sstevel@tonic-gate 	char nbuf[SM_MAX(MAXPACKET, MAXDNAME*2+2)];
8120Sstevel@tonic-gate 	char *searchlist[MAXDNSRCH + 2];
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (tTd(8, 2))
8150Sstevel@tonic-gate 		sm_dprintf("dns_getcanonname(%s, trymx=%d)\n", host, trymx);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
8180Sstevel@tonic-gate 	{
8190Sstevel@tonic-gate 		*statp = EX_UNAVAILABLE;
8200Sstevel@tonic-gate 		return false;
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	*statp = EX_OK;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	**  Initialize domain search list.  If there is at least one
8270Sstevel@tonic-gate 	**  dot in the name, search the unmodified name first so we
8280Sstevel@tonic-gate 	**  find "vse.CS" in Czechoslovakia instead of in the local
8290Sstevel@tonic-gate 	**  domain (e.g., vse.CS.Berkeley.EDU).  Note that there is no
8300Sstevel@tonic-gate 	**  longer a country named Czechoslovakia but this type of problem
8310Sstevel@tonic-gate 	**  is still present.
8320Sstevel@tonic-gate 	**
8330Sstevel@tonic-gate 	**  Older versions of the resolver could create this
8340Sstevel@tonic-gate 	**  list by tearing apart the host name.
8350Sstevel@tonic-gate 	*/
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	loopcnt = 0;
8380Sstevel@tonic-gate cnameloop:
8390Sstevel@tonic-gate 	/* Check for dots in the name */
8400Sstevel@tonic-gate 	for (cp = host, n = 0; *cp != '\0'; cp++)
8410Sstevel@tonic-gate 		if (*cp == '.')
8420Sstevel@tonic-gate 			n++;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/*
8450Sstevel@tonic-gate 	**  Build the search list.
8460Sstevel@tonic-gate 	**	If there is at least one dot in name, start with a null
8470Sstevel@tonic-gate 	**	domain to search the unmodified name first.
8480Sstevel@tonic-gate 	**	If name does not end with a dot and search up local domain
8490Sstevel@tonic-gate 	**	tree desired, append each local domain component to the
8500Sstevel@tonic-gate 	**	search list; if name contains no dots and default domain
8510Sstevel@tonic-gate 	**	name is desired, append default domain name to search list;
8520Sstevel@tonic-gate 	**	else if name ends in a dot, remove that dot.
8530Sstevel@tonic-gate 	*/
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	dp = searchlist;
8560Sstevel@tonic-gate 	if (n > 0)
8570Sstevel@tonic-gate 		*dp++ = "";
8580Sstevel@tonic-gate 	if (n >= 0 && *--cp != '.' && bitset(RES_DNSRCH, _res.options))
8590Sstevel@tonic-gate 	{
8600Sstevel@tonic-gate 		/* make sure there are less than MAXDNSRCH domains */
8610Sstevel@tonic-gate 		for (domain = RES_DNSRCH_VARIABLE, ret = 0;
8620Sstevel@tonic-gate 		     *domain != NULL && ret < MAXDNSRCH;
8630Sstevel@tonic-gate 		     ret++)
8640Sstevel@tonic-gate 			*dp++ = *domain++;
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 	else if (n == 0 && bitset(RES_DEFNAMES, _res.options))
8670Sstevel@tonic-gate 	{
8680Sstevel@tonic-gate 		*dp++ = _res.defdname;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	else if (*cp == '.')
8710Sstevel@tonic-gate 	{
8720Sstevel@tonic-gate 		*cp = '\0';
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	*dp = NULL;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	**  Now loop through the search list, appending each domain in turn
8780Sstevel@tonic-gate 	**  name and searching for a match.
8790Sstevel@tonic-gate 	*/
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	mxmatch = NULL;
8820Sstevel@tonic-gate 	initial = T_A;
8830Sstevel@tonic-gate # if NETINET6
8840Sstevel@tonic-gate 	if (InetMode == AF_INET6)
8850Sstevel@tonic-gate 		initial = T_AAAA;
8860Sstevel@tonic-gate # endif /* NETINET6 */
8870Sstevel@tonic-gate 	qtype = initial;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	for (dp = searchlist; *dp != NULL; )
8900Sstevel@tonic-gate 	{
8910Sstevel@tonic-gate 		if (qtype == initial)
8920Sstevel@tonic-gate 			gotmx = false;
8930Sstevel@tonic-gate 		if (tTd(8, 5))
8940Sstevel@tonic-gate 			sm_dprintf("dns_getcanonname: trying %s.%s (%s)\n",
8950Sstevel@tonic-gate 				host, *dp,
8960Sstevel@tonic-gate # if NETINET6
8970Sstevel@tonic-gate 				qtype == T_AAAA ? "AAAA" :
8980Sstevel@tonic-gate # endif /* NETINET6 */
8990Sstevel@tonic-gate 				qtype == T_A ? "A" :
9000Sstevel@tonic-gate 				qtype == T_MX ? "MX" :
9010Sstevel@tonic-gate 				"???");
9020Sstevel@tonic-gate 		errno = 0;
9030Sstevel@tonic-gate 		ret = res_querydomain(host, *dp, C_IN, qtype,
9040Sstevel@tonic-gate 				      answer.qb2, sizeof(answer.qb2));
9050Sstevel@tonic-gate 		if (ret <= 0)
9060Sstevel@tonic-gate 		{
9070Sstevel@tonic-gate 			int save_errno = errno;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 			if (tTd(8, 7))
9100Sstevel@tonic-gate 				sm_dprintf("\tNO: errno=%d, h_errno=%d\n",
9110Sstevel@tonic-gate 					   save_errno, h_errno);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 			if (save_errno == ECONNREFUSED || h_errno == TRY_AGAIN)
9140Sstevel@tonic-gate 			{
9150Sstevel@tonic-gate 				/*
9160Sstevel@tonic-gate 				**  the name server seems to be down or broken.
9170Sstevel@tonic-gate 				*/
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 				SM_SET_H_ERRNO(TRY_AGAIN);
9200Sstevel@tonic-gate 				if (**dp == '\0')
9210Sstevel@tonic-gate 				{
9220Sstevel@tonic-gate 					if (*statp == EX_OK)
9230Sstevel@tonic-gate 						*statp = EX_TEMPFAIL;
9240Sstevel@tonic-gate 					goto nexttype;
9250Sstevel@tonic-gate 				}
9260Sstevel@tonic-gate 				*statp = EX_TEMPFAIL;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 				if (WorkAroundBrokenAAAA)
9290Sstevel@tonic-gate 				{
9300Sstevel@tonic-gate 					/*
9310Sstevel@tonic-gate 					**  Only return if not TRY_AGAIN as an
9320Sstevel@tonic-gate 					**  attempt with a different qtype may
9330Sstevel@tonic-gate 					**  succeed (res_querydomain() calls
9340Sstevel@tonic-gate 					**  res_query() calls res_send() which
9350Sstevel@tonic-gate 					**  sets errno to ETIMEDOUT if the
9360Sstevel@tonic-gate 					**  nameservers could be contacted but
9370Sstevel@tonic-gate 					**  didn't give an answer).
9380Sstevel@tonic-gate 					*/
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 					if (save_errno != ETIMEDOUT)
9410Sstevel@tonic-gate 						return false;
9420Sstevel@tonic-gate 				}
9430Sstevel@tonic-gate 				else
9440Sstevel@tonic-gate 					return false;
9450Sstevel@tonic-gate 			}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate nexttype:
9480Sstevel@tonic-gate 			if (h_errno != HOST_NOT_FOUND)
9490Sstevel@tonic-gate 			{
9500Sstevel@tonic-gate 				/* might have another type of interest */
9510Sstevel@tonic-gate # if NETINET6
9520Sstevel@tonic-gate 				if (qtype == T_AAAA)
9530Sstevel@tonic-gate 				{
9540Sstevel@tonic-gate 					qtype = T_A;
9550Sstevel@tonic-gate 					continue;
9560Sstevel@tonic-gate 				}
9570Sstevel@tonic-gate 				else
9580Sstevel@tonic-gate # endif /* NETINET6 */
9590Sstevel@tonic-gate 				if (qtype == T_A && !gotmx &&
9600Sstevel@tonic-gate 				    (trymx || **dp == '\0'))
9610Sstevel@tonic-gate 				{
9620Sstevel@tonic-gate 					qtype = T_MX;
9630Sstevel@tonic-gate 					continue;
9640Sstevel@tonic-gate 				}
9650Sstevel@tonic-gate 			}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 			/* definite no -- try the next domain */
9680Sstevel@tonic-gate 			dp++;
9690Sstevel@tonic-gate 			qtype = initial;
9700Sstevel@tonic-gate 			continue;
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 		else if (tTd(8, 7))
9730Sstevel@tonic-gate 			sm_dprintf("\tYES\n");
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		/* avoid problems after truncation in tcp packets */
9760Sstevel@tonic-gate 		if (ret > sizeof(answer))
9770Sstevel@tonic-gate 			ret = sizeof(answer);
9782197Sjbeck 		SM_ASSERT(ret >= 0);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		/*
9810Sstevel@tonic-gate 		**  Appear to have a match.  Confirm it by searching for A or
9820Sstevel@tonic-gate 		**  CNAME records.  If we don't have a local domain
9830Sstevel@tonic-gate 		**  wild card MX record, we will accept MX as well.
9840Sstevel@tonic-gate 		*/
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		hp = (HEADER *) &answer;
9870Sstevel@tonic-gate 		ap = (unsigned char *) &answer + HFIXEDSZ;
9880Sstevel@tonic-gate 		eom = (unsigned char *) &answer + ret;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 		/* skip question part of response -- we know what we asked */
9910Sstevel@tonic-gate 		for (qdcount = ntohs((unsigned short) hp->qdcount);
9920Sstevel@tonic-gate 		     qdcount--;
9930Sstevel@tonic-gate 		     ap += ret + QFIXEDSZ)
9940Sstevel@tonic-gate 		{
9950Sstevel@tonic-gate 			if ((ret = dn_skipname(ap, eom)) < 0)
9960Sstevel@tonic-gate 			{
9970Sstevel@tonic-gate 				if (tTd(8, 20))
9980Sstevel@tonic-gate 					sm_dprintf("qdcount failure (%d)\n",
9990Sstevel@tonic-gate 						ntohs((unsigned short) hp->qdcount));
10000Sstevel@tonic-gate 				*statp = EX_SOFTWARE;
10010Sstevel@tonic-gate 				return false;		/* ???XXX??? */
10020Sstevel@tonic-gate 			}
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		amatch = false;
10060Sstevel@tonic-gate 		for (ancount = ntohs((unsigned short) hp->ancount);
10070Sstevel@tonic-gate 		     --ancount >= 0 && ap < eom;
10080Sstevel@tonic-gate 		     ap += n)
10090Sstevel@tonic-gate 		{
10100Sstevel@tonic-gate 			n = dn_expand((unsigned char *) &answer, eom, ap,
1011*3544Sjbeck 				      (RES_UNC_T) nbuf, sizeof(nbuf));
10120Sstevel@tonic-gate 			if (n < 0)
10130Sstevel@tonic-gate 				break;
10140Sstevel@tonic-gate 			ap += n;
10150Sstevel@tonic-gate 			GETSHORT(type, ap);
10160Sstevel@tonic-gate 			ap += INT16SZ;		/* skip over class */
10170Sstevel@tonic-gate 			GETLONG(ttl, ap);
10180Sstevel@tonic-gate 			GETSHORT(n, ap);	/* rdlength */
10190Sstevel@tonic-gate 			switch (type)
10200Sstevel@tonic-gate 			{
10210Sstevel@tonic-gate 			  case T_MX:
10220Sstevel@tonic-gate 				gotmx = true;
10230Sstevel@tonic-gate 				if (**dp != '\0' && HasWildcardMX)
10240Sstevel@tonic-gate 				{
10250Sstevel@tonic-gate 					/*
10260Sstevel@tonic-gate 					**  If we are using MX matches and have
10270Sstevel@tonic-gate 					**  not yet gotten one, save this one
10280Sstevel@tonic-gate 					**  but keep searching for an A or
10290Sstevel@tonic-gate 					**  CNAME match.
10300Sstevel@tonic-gate 					*/
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 					if (trymx && mxmatch == NULL)
10330Sstevel@tonic-gate 						mxmatch = *dp;
10340Sstevel@tonic-gate 					continue;
10350Sstevel@tonic-gate 				}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 				/*
10380Sstevel@tonic-gate 				**  If we did not append a domain name, this
10390Sstevel@tonic-gate 				**  must have been a canonical name to start
10400Sstevel@tonic-gate 				**  with.  Even if we did append a domain name,
10410Sstevel@tonic-gate 				**  in the absence of a wildcard MX this must
10420Sstevel@tonic-gate 				**  still be a real MX match.
10430Sstevel@tonic-gate 				**  Such MX matches are as good as an A match,
10440Sstevel@tonic-gate 				**  fall through.
10450Sstevel@tonic-gate 				*/
10460Sstevel@tonic-gate 				/* FALLTHROUGH */
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate # if NETINET6
10490Sstevel@tonic-gate 			  case T_AAAA:
10500Sstevel@tonic-gate # endif /* NETINET6 */
10510Sstevel@tonic-gate 			  case T_A:
10520Sstevel@tonic-gate 				/* Flag that a good match was found */
10530Sstevel@tonic-gate 				amatch = true;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 				/* continue in case a CNAME also exists */
10560Sstevel@tonic-gate 				continue;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 			  case T_CNAME:
10590Sstevel@tonic-gate 				if (DontExpandCnames)
10600Sstevel@tonic-gate 				{
10610Sstevel@tonic-gate 					/* got CNAME -- guaranteed canonical */
10620Sstevel@tonic-gate 					amatch = true;
10630Sstevel@tonic-gate 					break;
10640Sstevel@tonic-gate 				}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 				if (loopcnt++ > MAXCNAMEDEPTH)
10670Sstevel@tonic-gate 				{
10680Sstevel@tonic-gate 					/*XXX should notify postmaster XXX*/
10690Sstevel@tonic-gate 					message("DNS failure: CNAME loop for %s",
10700Sstevel@tonic-gate 						host);
10710Sstevel@tonic-gate 					if (CurEnv->e_message == NULL)
10720Sstevel@tonic-gate 					{
10730Sstevel@tonic-gate 						char ebuf[MAXLINE];
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 						(void) sm_snprintf(ebuf,
1076*3544Sjbeck 							sizeof(ebuf),
10770Sstevel@tonic-gate 							"Deferred: DNS failure: CNAME loop for %.100s",
10780Sstevel@tonic-gate 							host);
10790Sstevel@tonic-gate 						CurEnv->e_message =
10800Sstevel@tonic-gate 						    sm_rpool_strdup_x(
10810Sstevel@tonic-gate 							CurEnv->e_rpool, ebuf);
10820Sstevel@tonic-gate 					}
10830Sstevel@tonic-gate 					SM_SET_H_ERRNO(NO_RECOVERY);
10840Sstevel@tonic-gate 					*statp = EX_CONFIG;
10850Sstevel@tonic-gate 					return false;
10860Sstevel@tonic-gate 				}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 				/* value points at name */
10890Sstevel@tonic-gate 				if ((ret = dn_expand((unsigned char *)&answer,
10900Sstevel@tonic-gate 						     eom, ap, (RES_UNC_T) nbuf,
10910Sstevel@tonic-gate 						     sizeof(nbuf))) < 0)
10920Sstevel@tonic-gate 					break;
10930Sstevel@tonic-gate 				(void) sm_strlcpy(host, nbuf, hbsize);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 				/*
10960Sstevel@tonic-gate 				**  RFC 1034 section 3.6 specifies that CNAME
10970Sstevel@tonic-gate 				**  should point at the canonical name -- but
10980Sstevel@tonic-gate 				**  urges software to try again anyway.
10990Sstevel@tonic-gate 				*/
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 				goto cnameloop;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 			  default:
11040Sstevel@tonic-gate 				/* not a record of interest */
11050Sstevel@tonic-gate 				continue;
11060Sstevel@tonic-gate 			}
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		if (amatch)
11100Sstevel@tonic-gate 		{
11110Sstevel@tonic-gate 			/*
11120Sstevel@tonic-gate 			**  Got a good match -- either an A, CNAME, or an
11130Sstevel@tonic-gate 			**  exact MX record.  Save it and get out of here.
11140Sstevel@tonic-gate 			*/
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 			mxmatch = *dp;
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		/*
11210Sstevel@tonic-gate 		**  Nothing definitive yet.
11220Sstevel@tonic-gate 		**	If this was a T_A query and we haven't yet found a MX
11230Sstevel@tonic-gate 		**		match, try T_MX if allowed to do so.
11240Sstevel@tonic-gate 		**	Otherwise, try the next domain.
11250Sstevel@tonic-gate 		*/
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate # if NETINET6
11280Sstevel@tonic-gate 		if (qtype == T_AAAA)
11290Sstevel@tonic-gate 			qtype = T_A;
11300Sstevel@tonic-gate 		else
11310Sstevel@tonic-gate # endif /* NETINET6 */
11320Sstevel@tonic-gate 		if (qtype == T_A && !gotmx && (trymx || **dp == '\0'))
11330Sstevel@tonic-gate 			qtype = T_MX;
11340Sstevel@tonic-gate 		else
11350Sstevel@tonic-gate 		{
11360Sstevel@tonic-gate 			qtype = initial;
11370Sstevel@tonic-gate 			dp++;
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	/* if nothing was found, we are done */
11420Sstevel@tonic-gate 	if (mxmatch == NULL)
11430Sstevel@tonic-gate 	{
11440Sstevel@tonic-gate 		if (*statp == EX_OK)
11450Sstevel@tonic-gate 			*statp = EX_NOHOST;
11460Sstevel@tonic-gate 		return false;
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/*
11500Sstevel@tonic-gate 	**  Create canonical name and return.
11510Sstevel@tonic-gate 	**  If saved domain name is null, name was already canonical.
11520Sstevel@tonic-gate 	**  Otherwise append the saved domain name.
11530Sstevel@tonic-gate 	*/
11540Sstevel@tonic-gate 
1155*3544Sjbeck 	(void) sm_snprintf(nbuf, sizeof(nbuf), "%.*s%s%.*s", MAXDNAME, host,
11560Sstevel@tonic-gate 			   *mxmatch == '\0' ? "" : ".",
11570Sstevel@tonic-gate 			   MAXDNAME, mxmatch);
11580Sstevel@tonic-gate 	(void) sm_strlcpy(host, nbuf, hbsize);
11590Sstevel@tonic-gate 	if (tTd(8, 5))
11600Sstevel@tonic-gate 		sm_dprintf("dns_getcanonname: %s\n", host);
11610Sstevel@tonic-gate 	*statp = EX_OK;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	/* return only one TTL entry, that should be sufficient */
11640Sstevel@tonic-gate 	if (ttl > 0 && pttl != NULL)
11650Sstevel@tonic-gate 		*pttl = ttl;
11660Sstevel@tonic-gate 	return true;
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate #endif /* NAMED_BIND */
1169