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 
447c478bd9Sstevel@tonic-gate #include <sendmail.h>
457c478bd9Sstevel@tonic-gate #if DNSMAP
467c478bd9Sstevel@tonic-gate # if NAMED_BIND
477c478bd9Sstevel@tonic-gate #  include "sm_resolve.h"
487c478bd9Sstevel@tonic-gate 
49*d4660949Sjbeck SM_RCSID("$Id: sm_resolve.c,v 8.36 2008/02/11 23:04:16 ca Exp $")
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static struct stot
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	const char	*st_name;
547c478bd9Sstevel@tonic-gate 	int		st_type;
557c478bd9Sstevel@tonic-gate } stot[] =
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate #  if NETINET
587c478bd9Sstevel@tonic-gate 	{	"A",		T_A		},
597c478bd9Sstevel@tonic-gate #  endif /* NETINET */
607c478bd9Sstevel@tonic-gate #  if NETINET6
617c478bd9Sstevel@tonic-gate 	{	"AAAA",		T_AAAA		},
627c478bd9Sstevel@tonic-gate #  endif /* NETINET6 */
637c478bd9Sstevel@tonic-gate 	{	"NS",		T_NS		},
647c478bd9Sstevel@tonic-gate 	{	"CNAME",	T_CNAME		},
657c478bd9Sstevel@tonic-gate 	{	"PTR",		T_PTR		},
667c478bd9Sstevel@tonic-gate 	{	"MX",		T_MX		},
677c478bd9Sstevel@tonic-gate 	{	"TXT",		T_TXT		},
687c478bd9Sstevel@tonic-gate 	{	"AFSDB",	T_AFSDB		},
697c478bd9Sstevel@tonic-gate 	{	"SRV",		T_SRV		},
707c478bd9Sstevel@tonic-gate 	{	NULL,		0		}
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static DNS_REPLY_T *parse_dns_reply __P((unsigned char *, int));
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate **  DNS_STRING_TO_TYPE -- convert resource record name into type
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	Parameters:
797c478bd9Sstevel@tonic-gate **		name -- name of resource record type
807c478bd9Sstevel@tonic-gate **
817c478bd9Sstevel@tonic-gate **	Returns:
827c478bd9Sstevel@tonic-gate **		type if succeeded.
837c478bd9Sstevel@tonic-gate **		-1 otherwise.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate int
dns_string_to_type(name)877c478bd9Sstevel@tonic-gate dns_string_to_type(name)
887c478bd9Sstevel@tonic-gate 	const char *name;
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	struct stot *p = stot;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	for (p = stot; p->st_name != NULL; p++)
937c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(name, p->st_name) == 0)
947c478bd9Sstevel@tonic-gate 			return p->st_type;
957c478bd9Sstevel@tonic-gate 	return -1;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate **  DNS_TYPE_TO_STRING -- convert resource record type into name
1007c478bd9Sstevel@tonic-gate **
1017c478bd9Sstevel@tonic-gate **	Parameters:
1027c478bd9Sstevel@tonic-gate **		type -- resource record type
1037c478bd9Sstevel@tonic-gate **
1047c478bd9Sstevel@tonic-gate **	Returns:
1057c478bd9Sstevel@tonic-gate **		name if succeeded.
1067c478bd9Sstevel@tonic-gate **		NULL otherwise.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate const char *
dns_type_to_string(type)1107c478bd9Sstevel@tonic-gate dns_type_to_string(type)
1117c478bd9Sstevel@tonic-gate 	int type;
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	struct stot *p = stot;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	for (p = stot; p->st_name != NULL; p++)
1167c478bd9Sstevel@tonic-gate 		if (type == p->st_type)
1177c478bd9Sstevel@tonic-gate 			return p->st_name;
1187c478bd9Sstevel@tonic-gate 	return NULL;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate **  DNS_FREE_DATA -- free all components of a DNS_REPLY_T
1237c478bd9Sstevel@tonic-gate **
1247c478bd9Sstevel@tonic-gate **	Parameters:
1257c478bd9Sstevel@tonic-gate **		r -- pointer to DNS_REPLY_T
1267c478bd9Sstevel@tonic-gate **
1277c478bd9Sstevel@tonic-gate **	Returns:
1287c478bd9Sstevel@tonic-gate **		none.
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate void
dns_free_data(r)1327c478bd9Sstevel@tonic-gate dns_free_data(r)
1337c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r;
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	RESOURCE_RECORD_T *rr;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (r->dns_r_q.dns_q_domain != NULL)
1387c478bd9Sstevel@tonic-gate 		sm_free(r->dns_r_q.dns_q_domain);
1397c478bd9Sstevel@tonic-gate 	for (rr = r->dns_r_head; rr != NULL; )
1407c478bd9Sstevel@tonic-gate 	{
1417c478bd9Sstevel@tonic-gate 		RESOURCE_RECORD_T *tmp = rr;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if (rr->rr_domain != NULL)
1447c478bd9Sstevel@tonic-gate 			sm_free(rr->rr_domain);
1457c478bd9Sstevel@tonic-gate 		if (rr->rr_u.rr_data != NULL)
1467c478bd9Sstevel@tonic-gate 			sm_free(rr->rr_u.rr_data);
1477c478bd9Sstevel@tonic-gate 		rr = rr->rr_next;
1487c478bd9Sstevel@tonic-gate 		sm_free(tmp);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	sm_free(r);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate **  PARSE_DNS_REPLY -- parse DNS reply data.
1557c478bd9Sstevel@tonic-gate **
1567c478bd9Sstevel@tonic-gate **	Parameters:
1577c478bd9Sstevel@tonic-gate **		data -- pointer to dns data
1587c478bd9Sstevel@tonic-gate **		len -- len of data
1597c478bd9Sstevel@tonic-gate **
1607c478bd9Sstevel@tonic-gate **	Returns:
1617c478bd9Sstevel@tonic-gate **		pointer to DNS_REPLY_T if succeeded.
1627c478bd9Sstevel@tonic-gate **		NULL otherwise.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static DNS_REPLY_T *
parse_dns_reply(data,len)1667c478bd9Sstevel@tonic-gate parse_dns_reply(data, len)
1677c478bd9Sstevel@tonic-gate 	unsigned char *data;
1687c478bd9Sstevel@tonic-gate 	int len;
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	unsigned char *p;
171*d4660949Sjbeck 	unsigned short ans_cnt, ui;
1727c478bd9Sstevel@tonic-gate 	int status;
1737c478bd9Sstevel@tonic-gate 	size_t l;
1747c478bd9Sstevel@tonic-gate 	char host[MAXHOSTNAMELEN];
1757c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r;
1767c478bd9Sstevel@tonic-gate 	RESOURCE_RECORD_T **rr;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	r = (DNS_REPLY_T *) sm_malloc(sizeof(*r));
1797c478bd9Sstevel@tonic-gate 	if (r == NULL)
1807c478bd9Sstevel@tonic-gate 		return NULL;
1817c478bd9Sstevel@tonic-gate 	memset(r, 0, sizeof(*r));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	p = data;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* doesn't work on Crays? */
1867c478bd9Sstevel@tonic-gate 	memcpy(&r->dns_r_h, p, sizeof(r->dns_r_h));
1877c478bd9Sstevel@tonic-gate 	p += sizeof(r->dns_r_h);
188058561cbSjbeck 	status = dn_expand(data, data + len, p, host, sizeof(host));
1897c478bd9Sstevel@tonic-gate 	if (status < 0)
1907c478bd9Sstevel@tonic-gate 	{
1917c478bd9Sstevel@tonic-gate 		dns_free_data(r);
1927c478bd9Sstevel@tonic-gate 		return NULL;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	r->dns_r_q.dns_q_domain = sm_strdup(host);
1957c478bd9Sstevel@tonic-gate 	if (r->dns_r_q.dns_q_domain == NULL)
1967c478bd9Sstevel@tonic-gate 	{
1977c478bd9Sstevel@tonic-gate 		dns_free_data(r);
1987c478bd9Sstevel@tonic-gate 		return NULL;
1997c478bd9Sstevel@tonic-gate 	}
2007800901eSjbeck 
201*d4660949Sjbeck 	ans_cnt = ntohs((unsigned short) r->dns_r_h.ancount);
2027800901eSjbeck 
2037c478bd9Sstevel@tonic-gate 	p += status;
2047c478bd9Sstevel@tonic-gate 	GETSHORT(r->dns_r_q.dns_q_type, p);
2057c478bd9Sstevel@tonic-gate 	GETSHORT(r->dns_r_q.dns_q_class, p);
2067c478bd9Sstevel@tonic-gate 	rr = &r->dns_r_head;
2077800901eSjbeck 	ui = 0;
2087800901eSjbeck 	while (p < data + len && ui < ans_cnt)
2097c478bd9Sstevel@tonic-gate 	{
2107c478bd9Sstevel@tonic-gate 		int type, class, ttl, size, txtlen;
2117c478bd9Sstevel@tonic-gate 
212058561cbSjbeck 		status = dn_expand(data, data + len, p, host, sizeof(host));
2137c478bd9Sstevel@tonic-gate 		if (status < 0)
2147c478bd9Sstevel@tonic-gate 		{
2157c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2167c478bd9Sstevel@tonic-gate 			return NULL;
2177c478bd9Sstevel@tonic-gate 		}
2187800901eSjbeck 		++ui;
2197c478bd9Sstevel@tonic-gate 		p += status;
2207c478bd9Sstevel@tonic-gate 		GETSHORT(type, p);
2217c478bd9Sstevel@tonic-gate 		GETSHORT(class, p);
2227c478bd9Sstevel@tonic-gate 		GETLONG(ttl, p);
2237c478bd9Sstevel@tonic-gate 		GETSHORT(size, p);
2247c478bd9Sstevel@tonic-gate 		if (p + size > data + len)
2257c478bd9Sstevel@tonic-gate 		{
2267c478bd9Sstevel@tonic-gate 			/*
2277c478bd9Sstevel@tonic-gate 			**  announced size of data exceeds length of
2287c478bd9Sstevel@tonic-gate 			**  data paket: someone is cheating.
2297c478bd9Sstevel@tonic-gate 			*/
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			if (LogLevel > 5)
2327c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
2337c478bd9Sstevel@tonic-gate 					  "ERROR: DNS RDLENGTH=%d > data len=%d",
2347c478bd9Sstevel@tonic-gate 					  size, len - (p - data));
2357c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2367c478bd9Sstevel@tonic-gate 			return NULL;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 		*rr = (RESOURCE_RECORD_T *) sm_malloc(sizeof(**rr));
2397c478bd9Sstevel@tonic-gate 		if (*rr == NULL)
2407c478bd9Sstevel@tonic-gate 		{
2417c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2427c478bd9Sstevel@tonic-gate 			return NULL;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		memset(*rr, 0, sizeof(**rr));
2457c478bd9Sstevel@tonic-gate 		(*rr)->rr_domain = sm_strdup(host);
2467c478bd9Sstevel@tonic-gate 		if ((*rr)->rr_domain == NULL)
2477c478bd9Sstevel@tonic-gate 		{
2487c478bd9Sstevel@tonic-gate 			dns_free_data(r);
2497c478bd9Sstevel@tonic-gate 			return NULL;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		(*rr)->rr_type = type;
2527c478bd9Sstevel@tonic-gate 		(*rr)->rr_class = class;
2537c478bd9Sstevel@tonic-gate 		(*rr)->rr_ttl = ttl;
2547c478bd9Sstevel@tonic-gate 		(*rr)->rr_size = size;
2557c478bd9Sstevel@tonic-gate 		switch (type)
2567c478bd9Sstevel@tonic-gate 		{
2577c478bd9Sstevel@tonic-gate 		  case T_NS:
2587c478bd9Sstevel@tonic-gate 		  case T_CNAME:
2597c478bd9Sstevel@tonic-gate 		  case T_PTR:
2607c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p, host,
261058561cbSjbeck 					   sizeof(host));
2627c478bd9Sstevel@tonic-gate 			if (status < 0)
2637c478bd9Sstevel@tonic-gate 			{
2647c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2657c478bd9Sstevel@tonic-gate 				return NULL;
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_txt = sm_strdup(host);
2687c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_txt == NULL)
2697c478bd9Sstevel@tonic-gate 			{
2707c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2717c478bd9Sstevel@tonic-gate 				return NULL;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		  case T_MX:
2767c478bd9Sstevel@tonic-gate 		  case T_AFSDB:
2777c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p + 2, host,
278058561cbSjbeck 					   sizeof(host));
2797c478bd9Sstevel@tonic-gate 			if (status < 0)
2807c478bd9Sstevel@tonic-gate 			{
2817c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2827c478bd9Sstevel@tonic-gate 				return NULL;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			l = strlen(host) + 1;
2857c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_mx = (MX_RECORD_T *)
2867c478bd9Sstevel@tonic-gate 				sm_malloc(sizeof(*((*rr)->rr_u.rr_mx)) + l);
2877c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_mx == NULL)
2887c478bd9Sstevel@tonic-gate 			{
2897c478bd9Sstevel@tonic-gate 				dns_free_data(r);
2907c478bd9Sstevel@tonic-gate 				return NULL;
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_mx->mx_r_preference = (p[0] << 8) | p[1];
2937c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_mx->mx_r_domain,
2947c478bd9Sstevel@tonic-gate 					  host, l);
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		  case T_SRV:
2987c478bd9Sstevel@tonic-gate 			status = dn_expand(data, data + len, p + 6, host,
299058561cbSjbeck 					   sizeof(host));
3007c478bd9Sstevel@tonic-gate 			if (status < 0)
3017c478bd9Sstevel@tonic-gate 			{
3027c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3037c478bd9Sstevel@tonic-gate 				return NULL;
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 			l = strlen(host) + 1;
3067c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv = (SRV_RECORDT_T*)
3077c478bd9Sstevel@tonic-gate 				sm_malloc(sizeof(*((*rr)->rr_u.rr_srv)) + l);
3087c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_srv == NULL)
3097c478bd9Sstevel@tonic-gate 			{
3107c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3117c478bd9Sstevel@tonic-gate 				return NULL;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_priority = (p[0] << 8) | p[1];
3147c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_weight = (p[2] << 8) | p[3];
3157c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_srv->srv_r_port = (p[4] << 8) | p[5];
3167c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_srv->srv_r_target,
3177c478bd9Sstevel@tonic-gate 					  host, l);
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		  case T_TXT:
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			/*
3237c478bd9Sstevel@tonic-gate 			**  The TXT record contains the length as
3247c478bd9Sstevel@tonic-gate 			**  leading byte, hence the value is restricted
3257c478bd9Sstevel@tonic-gate 			**  to 255, which is less than the maximum value
3267c478bd9Sstevel@tonic-gate 			**  of RDLENGTH (size). Nevertheless, txtlen
3277c478bd9Sstevel@tonic-gate 			**  must be less than size because the latter
3287c478bd9Sstevel@tonic-gate 			**  specifies the length of the entire TXT
3297c478bd9Sstevel@tonic-gate 			**  record.
3307c478bd9Sstevel@tonic-gate 			*/
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 			txtlen = *p;
3337c478bd9Sstevel@tonic-gate 			if (txtlen >= size)
3347c478bd9Sstevel@tonic-gate 			{
3357c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
3367c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
3377c478bd9Sstevel@tonic-gate 						  "ERROR: DNS TXT record size=%d <= text len=%d",
3387c478bd9Sstevel@tonic-gate 						  size, txtlen);
3397c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3407c478bd9Sstevel@tonic-gate 				return NULL;
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_txt = (char *) sm_malloc(txtlen + 1);
3437c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_txt == NULL)
3447c478bd9Sstevel@tonic-gate 			{
3457c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3467c478bd9Sstevel@tonic-gate 				return NULL;
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy((*rr)->rr_u.rr_txt, (char*) p + 1,
3497c478bd9Sstevel@tonic-gate 					  txtlen + 1);
3507c478bd9Sstevel@tonic-gate 			break;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		  default:
3537c478bd9Sstevel@tonic-gate 			(*rr)->rr_u.rr_data = (unsigned char*) sm_malloc(size);
3547c478bd9Sstevel@tonic-gate 			if ((*rr)->rr_u.rr_data == NULL)
3557c478bd9Sstevel@tonic-gate 			{
3567c478bd9Sstevel@tonic-gate 				dns_free_data(r);
3577c478bd9Sstevel@tonic-gate 				return NULL;
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 			(void) memcpy((*rr)->rr_u.rr_data, p, size);
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		p += size;
3637c478bd9Sstevel@tonic-gate 		rr = &(*rr)->rr_next;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	*rr = NULL;
3667c478bd9Sstevel@tonic-gate 	return r;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate **  DNS_LOOKUP_INT -- perform dns map lookup (internal helper routine)
3717c478bd9Sstevel@tonic-gate **
3727c478bd9Sstevel@tonic-gate **	Parameters:
3737c478bd9Sstevel@tonic-gate **		domain -- name to lookup
3747c478bd9Sstevel@tonic-gate **		rr_class -- resource record class
3757c478bd9Sstevel@tonic-gate **		rr_type -- resource record type
3767c478bd9Sstevel@tonic-gate **		retrans -- retransmission timeout
3777c478bd9Sstevel@tonic-gate **		retry -- number of retries
3787c478bd9Sstevel@tonic-gate **
3797c478bd9Sstevel@tonic-gate **	Returns:
3807c478bd9Sstevel@tonic-gate **		result of lookup if succeeded.
3817c478bd9Sstevel@tonic-gate **		NULL otherwise.
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate DNS_REPLY_T *
dns_lookup_int(domain,rr_class,rr_type,retrans,retry)3857c478bd9Sstevel@tonic-gate dns_lookup_int(domain, rr_class, rr_type, retrans, retry)
3867c478bd9Sstevel@tonic-gate 	const char *domain;
3877c478bd9Sstevel@tonic-gate 	int rr_class;
3887c478bd9Sstevel@tonic-gate 	int rr_type;
3897c478bd9Sstevel@tonic-gate 	time_t retrans;
3907c478bd9Sstevel@tonic-gate 	int retry;
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	int len;
3937c478bd9Sstevel@tonic-gate 	unsigned long old_options = 0;
3947c478bd9Sstevel@tonic-gate 	time_t save_retrans = 0;
3957c478bd9Sstevel@tonic-gate 	int save_retry = 0;
3967c478bd9Sstevel@tonic-gate 	DNS_REPLY_T *r = NULL;
3977c478bd9Sstevel@tonic-gate 	unsigned char reply[1024];
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (tTd(8, 16))
4007c478bd9Sstevel@tonic-gate 	{
4017c478bd9Sstevel@tonic-gate 		old_options = _res.options;
4027c478bd9Sstevel@tonic-gate 		_res.options |= RES_DEBUG;
4037c478bd9Sstevel@tonic-gate 		sm_dprintf("dns_lookup(%s, %d, %s)\n", domain,
4047c478bd9Sstevel@tonic-gate 			   rr_class, dns_type_to_string(rr_type));
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	if (retrans > 0)
4077c478bd9Sstevel@tonic-gate 	{
4087c478bd9Sstevel@tonic-gate 		save_retrans = _res.retrans;
4097c478bd9Sstevel@tonic-gate 		_res.retrans = retrans;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	if (retry > 0)
4127c478bd9Sstevel@tonic-gate 	{
4137c478bd9Sstevel@tonic-gate 		save_retry = _res.retry;
4147c478bd9Sstevel@tonic-gate 		_res.retry = retry;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	errno = 0;
4177c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(0);
418058561cbSjbeck 	len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
4197c478bd9Sstevel@tonic-gate 	if (tTd(8, 16))
4207c478bd9Sstevel@tonic-gate 	{
4217c478bd9Sstevel@tonic-gate 		_res.options = old_options;
4227c478bd9Sstevel@tonic-gate 		sm_dprintf("dns_lookup(%s, %d, %s) --> %d\n",
4237c478bd9Sstevel@tonic-gate 			   domain, rr_class, dns_type_to_string(rr_type), len);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	if (len >= 0)
4267c478bd9Sstevel@tonic-gate 		r = parse_dns_reply(reply, len);
4277c478bd9Sstevel@tonic-gate 	if (retrans > 0)
4287c478bd9Sstevel@tonic-gate 		_res.retrans = save_retrans;
4297c478bd9Sstevel@tonic-gate 	if (retry > 0)
4307c478bd9Sstevel@tonic-gate 		_res.retry = save_retry;
4317c478bd9Sstevel@tonic-gate 	return r;
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate #  if 0
4357c478bd9Sstevel@tonic-gate DNS_REPLY_T *
4367c478bd9Sstevel@tonic-gate dns_lookup(domain, type_name, retrans, retry)
4377c478bd9Sstevel@tonic-gate 	const char *domain;
4387c478bd9Sstevel@tonic-gate 	const char *type_name;
4397c478bd9Sstevel@tonic-gate 	time_t retrans;
4407c478bd9Sstevel@tonic-gate 	int retry;
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	int type;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	type = dns_string_to_type(type_name);
4457c478bd9Sstevel@tonic-gate 	if (type == -1)
4467c478bd9Sstevel@tonic-gate 	{
4477c478bd9Sstevel@tonic-gate 		if (tTd(8, 16))
4487c478bd9Sstevel@tonic-gate 			sm_dprintf("dns_lookup: unknown resource type: `%s'\n",
4497c478bd9Sstevel@tonic-gate 				type_name);
4507c478bd9Sstevel@tonic-gate 		return NULL;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	return dns_lookup_int(domain, C_IN, type, retrans, retry);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate #  endif /* 0 */
4557c478bd9Sstevel@tonic-gate # endif /* NAMED_BIND */
4567c478bd9Sstevel@tonic-gate #endif /* DNSMAP */
457