xref: /illumos-gate/usr/src/cmd/sendmail/src/sm_resolve.c (revision d4660949aa62dd6a963f4913b7120b383cf473c4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2004 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H�gskolan
137c478bd9Sstevel@tonic-gate  * (Royal Institute of Technology, Stockholm, Sweden).
147c478bd9Sstevel@tonic-gate  * All rights reserved.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
177c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
187c478bd9Sstevel@tonic-gate  * are met:
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
217c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
247c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
257c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * 3. Neither the name of the Institute nor the names of its contributors
287c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
297c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
327c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
337c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
347c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
357c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
367c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
377c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
387c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
397c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
407c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
417c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
44058561cbSjbeck #pragma ident	"%Z%%M%	%I%	%E% SMI"
45058561cbSjbeck 
467c478bd9Sstevel@tonic-gate #include <sendmail.h>
477c478bd9Sstevel@tonic-gate #if DNSMAP
487c478bd9Sstevel@tonic-gate # if NAMED_BIND
497c478bd9Sstevel@tonic-gate #  include "sm_resolve.h"
507c478bd9Sstevel@tonic-gate 
51*d4660949Sjbeck SM_RCSID("$Id: sm_resolve.c,v 8.36 2008/02/11 23:04:16 ca Exp $")
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static struct stot
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	const char	*st_name;
567c478bd9Sstevel@tonic-gate 	int		st_type;
577c478bd9Sstevel@tonic-gate } stot[] =
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate #  if NETINET
607c478bd9Sstevel@tonic-gate 	{	"A",		T_A		},
617c478bd9Sstevel@tonic-gate #  endif /* NETINET */
627c478bd9Sstevel@tonic-gate #  if NETINET6
637c478bd9Sstevel@tonic-gate 	{	"AAAA",		T_AAAA		},
647c478bd9Sstevel@tonic-gate #  endif /* NETINET6 */
657c478bd9Sstevel@tonic-gate 	{	"NS",		T_NS		},
667c478bd9Sstevel@tonic-gate 	{	"CNAME",	T_CNAME		},
677c478bd9Sstevel@tonic-gate 	{	"PTR",		T_PTR		},
687c478bd9Sstevel@tonic-gate 	{	"MX",		T_MX		},
697c478bd9Sstevel@tonic-gate 	{	"TXT",		T_TXT		},
707c478bd9Sstevel@tonic-gate 	{	"AFSDB",	T_AFSDB		},
717c478bd9Sstevel@tonic-gate 	{	"SRV",		T_SRV		},
727c478bd9Sstevel@tonic-gate 	{	NULL,		0		}
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static DNS_REPLY_T *parse_dns_reply __P((unsigned char *, int));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate **  DNS_STRING_TO_TYPE -- convert resource record name into type
797c478bd9Sstevel@tonic-gate **
807c478bd9Sstevel@tonic-gate **	Parameters:
817c478bd9Sstevel@tonic-gate **		name -- name of resource record type
827c478bd9Sstevel@tonic-gate **
837c478bd9Sstevel@tonic-gate **	Returns:
847c478bd9Sstevel@tonic-gate **		type if succeeded.
857c478bd9Sstevel@tonic-gate **		-1 otherwise.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
897c478bd9Sstevel@tonic-gate dns_string_to_type(name)
907c478bd9Sstevel@tonic-gate 	const char *name;
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	struct stot *p = stot;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	for (p = stot; p->st_name != NULL; p++)
957c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(name, p->st_name) == 0)
967c478bd9Sstevel@tonic-gate 			return p->st_type;
977c478bd9Sstevel@tonic-gate 	return -1;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate **  DNS_TYPE_TO_STRING -- convert resource record type into name
1027c478bd9Sstevel@tonic-gate **
1037c478bd9Sstevel@tonic-gate **	Parameters:
1047c478bd9Sstevel@tonic-gate **		type -- resource record type
1057c478bd9Sstevel@tonic-gate **
1067c478bd9Sstevel@tonic-gate **	Returns:
1077c478bd9Sstevel@tonic-gate **		name if succeeded.
1087c478bd9Sstevel@tonic-gate **		NULL otherwise.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate const char *
1127c478bd9Sstevel@tonic-gate dns_type_to_string(type)
1137c478bd9Sstevel@tonic-gate 	int type;
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	struct stot *p = stot;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	for (p = stot; p->st_name != NULL; p++)
1187c478bd9Sstevel@tonic-gate 		if (type == p->st_type)
1197c478bd9Sstevel@tonic-gate 			return p->st_name;
1207c478bd9Sstevel@tonic-gate 	return NULL;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate **  DNS_FREE_DATA -- free all components of a DNS_REPLY_T
1257c478bd9Sstevel@tonic-gate **
1267c478bd9Sstevel@tonic-gate **	Parameters:
1277c478bd9Sstevel@tonic-gate **		r -- pointer to DNS_REPLY_T
1287c478bd9Sstevel@tonic-gate **
1297c478bd9Sstevel@tonic-gate **	Returns:
1307c478bd9Sstevel@tonic-gate **		none.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate void
1347c478bd9Sstevel@tonic-gate dns_free_data(r)
1357c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r;
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	RESOURCE_RECORD_T *rr;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (r->dns_r_q.dns_q_domain != NULL)
1407c478bd9Sstevel@tonic-gate 		sm_free(r->dns_r_q.dns_q_domain);
1417c478bd9Sstevel@tonic-gate 	for (rr = r->dns_r_head; rr != NULL; )
1427c478bd9Sstevel@tonic-gate 	{
1437c478bd9Sstevel@tonic-gate 		RESOURCE_RECORD_T *tmp = rr;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if (rr->rr_domain != NULL)
1467c478bd9Sstevel@tonic-gate 			sm_free(rr->rr_domain);
1477c478bd9Sstevel@tonic-gate 		if (rr->rr_u.rr_data != NULL)
1487c478bd9Sstevel@tonic-gate 			sm_free(rr->rr_u.rr_data);
1497c478bd9Sstevel@tonic-gate 		rr = rr->rr_next;
1507c478bd9Sstevel@tonic-gate 		sm_free(tmp);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	sm_free(r);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate **  PARSE_DNS_REPLY -- parse DNS reply data.
1577c478bd9Sstevel@tonic-gate **
1587c478bd9Sstevel@tonic-gate **	Parameters:
1597c478bd9Sstevel@tonic-gate **		data -- pointer to dns data
1607c478bd9Sstevel@tonic-gate **		len -- len of data
1617c478bd9Sstevel@tonic-gate **
1627c478bd9Sstevel@tonic-gate **	Returns:
1637c478bd9Sstevel@tonic-gate **		pointer to DNS_REPLY_T if succeeded.
1647c478bd9Sstevel@tonic-gate **		NULL otherwise.
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static DNS_REPLY_T *
1687c478bd9Sstevel@tonic-gate parse_dns_reply(data, len)
1697c478bd9Sstevel@tonic-gate 	unsigned char *data;
1707c478bd9Sstevel@tonic-gate 	int len;
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	unsigned char *p;
173*d4660949Sjbeck 	unsigned short ans_cnt, ui;
1747c478bd9Sstevel@tonic-gate 	int status;
1757c478bd9Sstevel@tonic-gate 	size_t l;
1767c478bd9Sstevel@tonic-gate 	char host[MAXHOSTNAMELEN];
1777c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r;
1787c478bd9Sstevel@tonic-gate 	RESOURCE_RECORD_T **rr;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	r = (DNS_REPLY_T *) sm_malloc(sizeof(*r));
1817c478bd9Sstevel@tonic-gate 	if (r == NULL)
1827c478bd9Sstevel@tonic-gate 		return NULL;
1837c478bd9Sstevel@tonic-gate 	memset(r, 0, sizeof(*r));
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	p = data;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* doesn't work on Crays? */
1887c478bd9Sstevel@tonic-gate 	memcpy(&r->dns_r_h, p, sizeof(r->dns_r_h));
1897c478bd9Sstevel@tonic-gate 	p += sizeof(r->dns_r_h);
190058561cbSjbeck 	status = dn_expand(data, data + len, p, host, sizeof(host));
1917c478bd9Sstevel@tonic-gate 	if (status < 0)
1927c478bd9Sstevel@tonic-gate 	{
1937c478bd9Sstevel@tonic-gate 		dns_free_data(r);
1947c478bd9Sstevel@tonic-gate 		return NULL;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	r->dns_r_q.dns_q_domain = sm_strdup(host);
1977c478bd9Sstevel@tonic-gate 	if (r->dns_r_q.dns_q_domain == NULL)
1987c478bd9Sstevel@tonic-gate 	{
1997c478bd9Sstevel@tonic-gate 		dns_free_data(r);
2007c478bd9Sstevel@tonic-gate 		return NULL;
2017c478bd9Sstevel@tonic-gate 	}
2027800901eSjbeck 
203*d4660949Sjbeck 	ans_cnt = ntohs((unsigned short) r->dns_r_h.ancount);
2047800901eSjbeck 
2057c478bd9Sstevel@tonic-gate 	p += status;
2067c478bd9Sstevel@tonic-gate 	GETSHORT(r->dns_r_q.dns_q_type, p);
2077c478bd9Sstevel@tonic-gate 	GETSHORT(r->dns_r_q.dns_q_class, p);
2087c478bd9Sstevel@tonic-gate 	rr = &r->dns_r_head;
2097800901eSjbeck 	ui = 0;
2107800901eSjbeck 	while (p < data + len && ui < ans_cnt)
2117c478bd9Sstevel@tonic-gate 	{
2127c478bd9Sstevel@tonic-gate 		int type, class, ttl, size, txtlen;
2137c478bd9Sstevel@tonic-gate 
214058561cbSjbeck 		status = dn_expand(data, data + len, p, host, sizeof(host));
2157c478bd9Sstevel@tonic-gate 		if (status < 0)
2167c478bd9Sstevel@tonic-gate 		{
2177c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2187c478bd9Sstevel@tonic-gate 			return NULL;
2197c478bd9Sstevel@tonic-gate 		}
2207800901eSjbeck 		++ui;
2217c478bd9Sstevel@tonic-gate 		p += status;
2227c478bd9Sstevel@tonic-gate 		GETSHORT(type, p);
2237c478bd9Sstevel@tonic-gate 		GETSHORT(class, p);
2247c478bd9Sstevel@tonic-gate 		GETLONG(ttl, p);
2257c478bd9Sstevel@tonic-gate 		GETSHORT(size, p);
2267c478bd9Sstevel@tonic-gate 		if (p + size > data + len)
2277c478bd9Sstevel@tonic-gate 		{
2287c478bd9Sstevel@tonic-gate 			/*
2297c478bd9Sstevel@tonic-gate 			**  announced size of data exceeds length of
2307c478bd9Sstevel@tonic-gate 			**  data paket: someone is cheating.
2317c478bd9Sstevel@tonic-gate 			*/
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			if (LogLevel > 5)
2347c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
2357c478bd9Sstevel@tonic-gate 					  "ERROR: DNS RDLENGTH=%d > data len=%d",
2367c478bd9Sstevel@tonic-gate 					  size, len - (p - data));
2377c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2387c478bd9Sstevel@tonic-gate 			return NULL;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		*rr = (RESOURCE_RECORD_T *) sm_malloc(sizeof(**rr));
2417c478bd9Sstevel@tonic-gate 		if (*rr == NULL)
2427c478bd9Sstevel@tonic-gate 		{
2437c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2447c478bd9Sstevel@tonic-gate 			return NULL;
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 		memset(*rr, 0, sizeof(**rr));
2477c478bd9Sstevel@tonic-gate 		(*rr)->rr_domain = sm_strdup(host);
2487c478bd9Sstevel@tonic-gate 		if ((*rr)->rr_domain == NULL)
2497c478bd9Sstevel@tonic-gate 		{
2507c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2517c478bd9Sstevel@tonic-gate 			return NULL;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		(*rr)->rr_type = type;
2547c478bd9Sstevel@tonic-gate 		(*rr)->rr_class = class;
2557c478bd9Sstevel@tonic-gate 		(*rr)->rr_ttl = ttl;
2567c478bd9Sstevel@tonic-gate 		(*rr)->rr_size = size;
2577c478bd9Sstevel@tonic-gate 		switch (type)
2587c478bd9Sstevel@tonic-gate 		{
2597c478bd9Sstevel@tonic-gate 		  case T_NS:
2607c478bd9Sstevel@tonic-gate 		  case T_CNAME:
2617c478bd9Sstevel@tonic-gate 		  case T_PTR:
2627c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p, host,
263058561cbSjbeck 					   sizeof(host));
2647c478bd9Sstevel@tonic-gate 			if (status < 0)
2657c478bd9Sstevel@tonic-gate 			{
2667c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2677c478bd9Sstevel@tonic-gate 				return NULL;
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_txt = sm_strdup(host);
2707c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_txt == NULL)
2717c478bd9Sstevel@tonic-gate 			{
2727c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2737c478bd9Sstevel@tonic-gate 				return NULL;
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		  case T_MX:
2787c478bd9Sstevel@tonic-gate 		  case T_AFSDB:
2797c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p + 2, host,
280058561cbSjbeck 					   sizeof(host));
2817c478bd9Sstevel@tonic-gate 			if (status < 0)
2827c478bd9Sstevel@tonic-gate 			{
2837c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2847c478bd9Sstevel@tonic-gate 				return NULL;
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 			l = strlen(host) + 1;
2877c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_mx = (MX_RECORD_T *)
2887c478bd9Sstevel@tonic-gate 				sm_malloc(sizeof(*((*rr)->rr_u.rr_mx)) + l);
2897c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_mx == NULL)
2907c478bd9Sstevel@tonic-gate 			{
2917c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2927c478bd9Sstevel@tonic-gate 				return NULL;
2937c478bd9Sstevel@tonic-gate 			}
2947c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_mx->mx_r_preference = (p[0] << 8) | p[1];
2957c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_mx->mx_r_domain,
2967c478bd9Sstevel@tonic-gate 					  host, l);
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		  case T_SRV:
3007c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p + 6, host,
301058561cbSjbeck 					   sizeof(host));
3027c478bd9Sstevel@tonic-gate 			if (status < 0)
3037c478bd9Sstevel@tonic-gate 			{
3047c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3057c478bd9Sstevel@tonic-gate 				return NULL;
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 			l = strlen(host) + 1;
3087c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv = (SRV_RECORDT_T*)
3097c478bd9Sstevel@tonic-gate 				sm_malloc(sizeof(*((*rr)->rr_u.rr_srv)) + l);
3107c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_srv == NULL)
3117c478bd9Sstevel@tonic-gate 			{
3127c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3137c478bd9Sstevel@tonic-gate 				return NULL;
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_priority = (p[0] << 8) | p[1];
3167c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_weight = (p[2] << 8) | p[3];
3177c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_port = (p[4] << 8) | p[5];
3187c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_srv->srv_r_target,
3197c478bd9Sstevel@tonic-gate 					  host, l);
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		  case T_TXT:
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 			/*
3257c478bd9Sstevel@tonic-gate 			**  The TXT record contains the length as
3267c478bd9Sstevel@tonic-gate 			**  leading byte, hence the value is restricted
3277c478bd9Sstevel@tonic-gate 			**  to 255, which is less than the maximum value
3287c478bd9Sstevel@tonic-gate 			**  of RDLENGTH (size). Nevertheless, txtlen
3297c478bd9Sstevel@tonic-gate 			**  must be less than size because the latter
3307c478bd9Sstevel@tonic-gate 			**  specifies the length of the entire TXT
3317c478bd9Sstevel@tonic-gate 			**  record.
3327c478bd9Sstevel@tonic-gate 			*/
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 			txtlen = *p;
3357c478bd9Sstevel@tonic-gate 			if (txtlen >= size)
3367c478bd9Sstevel@tonic-gate 			{
3377c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
3387c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
3397c478bd9Sstevel@tonic-gate 						  "ERROR: DNS TXT record size=%d <= text len=%d",
3407c478bd9Sstevel@tonic-gate 						  size, txtlen);
3417c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3427c478bd9Sstevel@tonic-gate 				return NULL;
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_txt = (char *) sm_malloc(txtlen + 1);
3457c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_txt == NULL)
3467c478bd9Sstevel@tonic-gate 			{
3477c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3487c478bd9Sstevel@tonic-gate 				return NULL;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_txt, (char*) p + 1,
3517c478bd9Sstevel@tonic-gate 					  txtlen + 1);
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		  default:
3557c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_data = (unsigned char*) sm_malloc(size);
3567c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_data == NULL)
3577c478bd9Sstevel@tonic-gate 			{
3587c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3597c478bd9Sstevel@tonic-gate 				return NULL;
3607c478bd9Sstevel@tonic-gate 			}
3617c478bd9Sstevel@tonic-gate 			(void) memcpy((*rr)->rr_u.rr_data, p, size);
3627c478bd9Sstevel@tonic-gate 			break;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		p += size;
3657c478bd9Sstevel@tonic-gate 		rr = &(*rr)->rr_next;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	*rr = NULL;
3687c478bd9Sstevel@tonic-gate 	return r;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate **  DNS_LOOKUP_INT -- perform dns map lookup (internal helper routine)
3737c478bd9Sstevel@tonic-gate **
3747c478bd9Sstevel@tonic-gate **	Parameters:
3757c478bd9Sstevel@tonic-gate **		domain -- name to lookup
3767c478bd9Sstevel@tonic-gate **		rr_class -- resource record class
3777c478bd9Sstevel@tonic-gate **		rr_type -- resource record type
3787c478bd9Sstevel@tonic-gate **		retrans -- retransmission timeout
3797c478bd9Sstevel@tonic-gate **		retry -- number of retries
3807c478bd9Sstevel@tonic-gate **
3817c478bd9Sstevel@tonic-gate **	Returns:
3827c478bd9Sstevel@tonic-gate **		result of lookup if succeeded.
3837c478bd9Sstevel@tonic-gate **		NULL otherwise.
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate DNS_REPLY_T *
3877c478bd9Sstevel@tonic-gate dns_lookup_int(domain, rr_class, rr_type, retrans, retry)
3887c478bd9Sstevel@tonic-gate 	const char *domain;
3897c478bd9Sstevel@tonic-gate 	int rr_class;
3907c478bd9Sstevel@tonic-gate 	int rr_type;
3917c478bd9Sstevel@tonic-gate 	time_t retrans;
3927c478bd9Sstevel@tonic-gate 	int retry;
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	int len;
3957c478bd9Sstevel@tonic-gate 	unsigned long old_options = 0;
3967c478bd9Sstevel@tonic-gate 	time_t save_retrans = 0;
3977c478bd9Sstevel@tonic-gate 	int save_retry = 0;
3987c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r = NULL;
3997c478bd9Sstevel@tonic-gate 	unsigned char reply[1024];
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (tTd(8, 16))
4027c478bd9Sstevel@tonic-gate 	{
4037c478bd9Sstevel@tonic-gate 		old_options = _res.options;
4047c478bd9Sstevel@tonic-gate 		_res.options |= RES_DEBUG;
4057c478bd9Sstevel@tonic-gate 		sm_dprintf("dns_lookup(%s, %d, %s)\n", domain,
4067c478bd9Sstevel@tonic-gate 			   rr_class, dns_type_to_string(rr_type));
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	if (retrans > 0)
4097c478bd9Sstevel@tonic-gate 	{
4107c478bd9Sstevel@tonic-gate 		save_retrans = _res.retrans;
4117c478bd9Sstevel@tonic-gate 		_res.retrans = retrans;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	if (retry > 0)
4147c478bd9Sstevel@tonic-gate 	{
4157c478bd9Sstevel@tonic-gate 		save_retry = _res.retry;
4167c478bd9Sstevel@tonic-gate 		_res.retry = retry;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	errno = 0;
4197c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(0);
420058561cbSjbeck 	len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
4217c478bd9Sstevel@tonic-gate 	if (tTd(8, 16))
4227c478bd9Sstevel@tonic-gate 	{
4237c478bd9Sstevel@tonic-gate 		_res.options = old_options;
4247c478bd9Sstevel@tonic-gate 		sm_dprintf("dns_lookup(%s, %d, %s) --> %d\n",
4257c478bd9Sstevel@tonic-gate 			   domain, rr_class, dns_type_to_string(rr_type), len);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 	if (len >= 0)
4287c478bd9Sstevel@tonic-gate 		r = parse_dns_reply(reply, len);
4297c478bd9Sstevel@tonic-gate 	if (retrans > 0)
4307c478bd9Sstevel@tonic-gate 		_res.retrans = save_retrans;
4317c478bd9Sstevel@tonic-gate 	if (retry > 0)
4327c478bd9Sstevel@tonic-gate 		_res.retry = save_retry;
4337c478bd9Sstevel@tonic-gate 	return r;
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate #  if 0
4377c478bd9Sstevel@tonic-gate DNS_REPLY_T *
4387c478bd9Sstevel@tonic-gate dns_lookup(domain, type_name, retrans, retry)
4397c478bd9Sstevel@tonic-gate 	const char *domain;
4407c478bd9Sstevel@tonic-gate 	const char *type_name;
4417c478bd9Sstevel@tonic-gate 	time_t retrans;
4427c478bd9Sstevel@tonic-gate 	int retry;
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int type;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	type = dns_string_to_type(type_name);
4477c478bd9Sstevel@tonic-gate 	if (type == -1)
4487c478bd9Sstevel@tonic-gate 	{
4497c478bd9Sstevel@tonic-gate 		if (tTd(8, 16))
4507c478bd9Sstevel@tonic-gate 			sm_dprintf("dns_lookup: unknown resource type: `%s'\n",
4517c478bd9Sstevel@tonic-gate 				type_name);
4527c478bd9Sstevel@tonic-gate 		return NULL;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	return dns_lookup_int(domain, C_IN, type, retrans, retry);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate #  endif /* 0 */
4577c478bd9Sstevel@tonic-gate # endif /* NAMED_BIND */
4587c478bd9Sstevel@tonic-gate #endif /* DNSMAP */
459