xref: /titanic_44/usr/src/cmd/sendmail/src/sm_resolve.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1 /*
2  * Copyright (c) 2000-2004 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  */
10 
11 /*
12  * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H�gskolan
13  * (Royal Institute of Technology, Stockholm, Sweden).
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  *
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * 3. Neither the name of the Institute nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  */
43 
44 #pragma ident	"%Z%%M%	%I%	%E% SMI"
45 
46 #include <sendmail.h>
47 #if DNSMAP
48 # if NAMED_BIND
49 #  include "sm_resolve.h"
50 
51 SM_RCSID("$Id: sm_resolve.c,v 8.34 2006/08/15 23:24:58 ca Exp $")
52 
53 static struct stot
54 {
55 	const char	*st_name;
56 	int		st_type;
57 } stot[] =
58 {
59 #  if NETINET
60 	{	"A",		T_A		},
61 #  endif /* NETINET */
62 #  if NETINET6
63 	{	"AAAA",		T_AAAA		},
64 #  endif /* NETINET6 */
65 	{	"NS",		T_NS		},
66 	{	"CNAME",	T_CNAME		},
67 	{	"PTR",		T_PTR		},
68 	{	"MX",		T_MX		},
69 	{	"TXT",		T_TXT		},
70 	{	"AFSDB",	T_AFSDB		},
71 	{	"SRV",		T_SRV		},
72 	{	NULL,		0		}
73 };
74 
75 static DNS_REPLY_T *parse_dns_reply __P((unsigned char *, int));
76 
77 /*
78 **  DNS_STRING_TO_TYPE -- convert resource record name into type
79 **
80 **	Parameters:
81 **		name -- name of resource record type
82 **
83 **	Returns:
84 **		type if succeeded.
85 **		-1 otherwise.
86 */
87 
88 int
89 dns_string_to_type(name)
90 	const char *name;
91 {
92 	struct stot *p = stot;
93 
94 	for (p = stot; p->st_name != NULL; p++)
95 		if (sm_strcasecmp(name, p->st_name) == 0)
96 			return p->st_type;
97 	return -1;
98 }
99 
100 /*
101 **  DNS_TYPE_TO_STRING -- convert resource record type into name
102 **
103 **	Parameters:
104 **		type -- resource record type
105 **
106 **	Returns:
107 **		name if succeeded.
108 **		NULL otherwise.
109 */
110 
111 const char *
112 dns_type_to_string(type)
113 	int type;
114 {
115 	struct stot *p = stot;
116 
117 	for (p = stot; p->st_name != NULL; p++)
118 		if (type == p->st_type)
119 			return p->st_name;
120 	return NULL;
121 }
122 
123 /*
124 **  DNS_FREE_DATA -- free all components of a DNS_REPLY_T
125 **
126 **	Parameters:
127 **		r -- pointer to DNS_REPLY_T
128 **
129 **	Returns:
130 **		none.
131 */
132 
133 void
134 dns_free_data(r)
135 	DNS_REPLY_T *r;
136 {
137 	RESOURCE_RECORD_T *rr;
138 
139 	if (r->dns_r_q.dns_q_domain != NULL)
140 		sm_free(r->dns_r_q.dns_q_domain);
141 	for (rr = r->dns_r_head; rr != NULL; )
142 	{
143 		RESOURCE_RECORD_T *tmp = rr;
144 
145 		if (rr->rr_domain != NULL)
146 			sm_free(rr->rr_domain);
147 		if (rr->rr_u.rr_data != NULL)
148 			sm_free(rr->rr_u.rr_data);
149 		rr = rr->rr_next;
150 		sm_free(tmp);
151 	}
152 	sm_free(r);
153 }
154 
155 /*
156 **  PARSE_DNS_REPLY -- parse DNS reply data.
157 **
158 **	Parameters:
159 **		data -- pointer to dns data
160 **		len -- len of data
161 **
162 **	Returns:
163 **		pointer to DNS_REPLY_T if succeeded.
164 **		NULL otherwise.
165 */
166 
167 static DNS_REPLY_T *
168 parse_dns_reply(data, len)
169 	unsigned char *data;
170 	int len;
171 {
172 	unsigned char *p;
173 	int status;
174 	size_t l;
175 	char host[MAXHOSTNAMELEN];
176 	DNS_REPLY_T *r;
177 	RESOURCE_RECORD_T **rr;
178 
179 	r = (DNS_REPLY_T *) sm_malloc(sizeof(*r));
180 	if (r == NULL)
181 		return NULL;
182 	memset(r, 0, sizeof(*r));
183 
184 	p = data;
185 
186 	/* doesn't work on Crays? */
187 	memcpy(&r->dns_r_h, p, sizeof(r->dns_r_h));
188 	p += sizeof(r->dns_r_h);
189 	status = dn_expand(data, data + len, p, host, sizeof(host));
190 	if (status < 0)
191 	{
192 		dns_free_data(r);
193 		return NULL;
194 	}
195 	r->dns_r_q.dns_q_domain = sm_strdup(host);
196 	if (r->dns_r_q.dns_q_domain == NULL)
197 	{
198 		dns_free_data(r);
199 		return NULL;
200 	}
201 	p += status;
202 	GETSHORT(r->dns_r_q.dns_q_type, p);
203 	GETSHORT(r->dns_r_q.dns_q_class, p);
204 	rr = &r->dns_r_head;
205 	while (p < data + len)
206 	{
207 		int type, class, ttl, size, txtlen;
208 
209 		status = dn_expand(data, data + len, p, host, sizeof(host));
210 		if (status < 0)
211 		{
212 			dns_free_data(r);
213 			return NULL;
214 		}
215 		p += status;
216 		GETSHORT(type, p);
217 		GETSHORT(class, p);
218 		GETLONG(ttl, p);
219 		GETSHORT(size, p);
220 		if (p + size > data + len)
221 		{
222 			/*
223 			**  announced size of data exceeds length of
224 			**  data paket: someone is cheating.
225 			*/
226 
227 			if (LogLevel > 5)
228 				sm_syslog(LOG_WARNING, NOQID,
229 					  "ERROR: DNS RDLENGTH=%d > data len=%d",
230 					  size, len - (p - data));
231 			dns_free_data(r);
232 			return NULL;
233 		}
234 		*rr = (RESOURCE_RECORD_T *) sm_malloc(sizeof(**rr));
235 		if (*rr == NULL)
236 		{
237 			dns_free_data(r);
238 			return NULL;
239 		}
240 		memset(*rr, 0, sizeof(**rr));
241 		(*rr)->rr_domain = sm_strdup(host);
242 		if ((*rr)->rr_domain == NULL)
243 		{
244 			dns_free_data(r);
245 			return NULL;
246 		}
247 		(*rr)->rr_type = type;
248 		(*rr)->rr_class = class;
249 		(*rr)->rr_ttl = ttl;
250 		(*rr)->rr_size = size;
251 		switch (type)
252 		{
253 		  case T_NS:
254 		  case T_CNAME:
255 		  case T_PTR:
256 			status = dn_expand(data, data + len, p, host,
257 					   sizeof(host));
258 			if (status < 0)
259 			{
260 				dns_free_data(r);
261 				return NULL;
262 			}
263 			(*rr)->rr_u.rr_txt = sm_strdup(host);
264 			if ((*rr)->rr_u.rr_txt == NULL)
265 			{
266 				dns_free_data(r);
267 				return NULL;
268 			}
269 			break;
270 
271 		  case T_MX:
272 		  case T_AFSDB:
273 			status = dn_expand(data, data + len, p + 2, host,
274 					   sizeof(host));
275 			if (status < 0)
276 			{
277 				dns_free_data(r);
278 				return NULL;
279 			}
280 			l = strlen(host) + 1;
281 			(*rr)->rr_u.rr_mx = (MX_RECORD_T *)
282 				sm_malloc(sizeof(*((*rr)->rr_u.rr_mx)) + l);
283 			if ((*rr)->rr_u.rr_mx == NULL)
284 			{
285 				dns_free_data(r);
286 				return NULL;
287 			}
288 			(*rr)->rr_u.rr_mx->mx_r_preference = (p[0] << 8) | p[1];
289 			(void) sm_strlcpy((*rr)->rr_u.rr_mx->mx_r_domain,
290 					  host, l);
291 			break;
292 
293 		  case T_SRV:
294 			status = dn_expand(data, data + len, p + 6, host,
295 					   sizeof(host));
296 			if (status < 0)
297 			{
298 				dns_free_data(r);
299 				return NULL;
300 			}
301 			l = strlen(host) + 1;
302 			(*rr)->rr_u.rr_srv = (SRV_RECORDT_T*)
303 				sm_malloc(sizeof(*((*rr)->rr_u.rr_srv)) + l);
304 			if ((*rr)->rr_u.rr_srv == NULL)
305 			{
306 				dns_free_data(r);
307 				return NULL;
308 			}
309 			(*rr)->rr_u.rr_srv->srv_r_priority = (p[0] << 8) | p[1];
310 			(*rr)->rr_u.rr_srv->srv_r_weight = (p[2] << 8) | p[3];
311 			(*rr)->rr_u.rr_srv->srv_r_port = (p[4] << 8) | p[5];
312 			(void) sm_strlcpy((*rr)->rr_u.rr_srv->srv_r_target,
313 					  host, l);
314 			break;
315 
316 		  case T_TXT:
317 
318 			/*
319 			**  The TXT record contains the length as
320 			**  leading byte, hence the value is restricted
321 			**  to 255, which is less than the maximum value
322 			**  of RDLENGTH (size). Nevertheless, txtlen
323 			**  must be less than size because the latter
324 			**  specifies the length of the entire TXT
325 			**  record.
326 			*/
327 
328 			txtlen = *p;
329 			if (txtlen >= size)
330 			{
331 				if (LogLevel > 5)
332 					sm_syslog(LOG_WARNING, NOQID,
333 						  "ERROR: DNS TXT record size=%d <= text len=%d",
334 						  size, txtlen);
335 				dns_free_data(r);
336 				return NULL;
337 			}
338 			(*rr)->rr_u.rr_txt = (char *) sm_malloc(txtlen + 1);
339 			if ((*rr)->rr_u.rr_txt == NULL)
340 			{
341 				dns_free_data(r);
342 				return NULL;
343 			}
344 			(void) sm_strlcpy((*rr)->rr_u.rr_txt, (char*) p + 1,
345 					  txtlen + 1);
346 			break;
347 
348 		  default:
349 			(*rr)->rr_u.rr_data = (unsigned char*) sm_malloc(size);
350 			if ((*rr)->rr_u.rr_data == NULL)
351 			{
352 				dns_free_data(r);
353 				return NULL;
354 			}
355 			(void) memcpy((*rr)->rr_u.rr_data, p, size);
356 			break;
357 		}
358 		p += size;
359 		rr = &(*rr)->rr_next;
360 	}
361 	*rr = NULL;
362 	return r;
363 }
364 
365 /*
366 **  DNS_LOOKUP_INT -- perform dns map lookup (internal helper routine)
367 **
368 **	Parameters:
369 **		domain -- name to lookup
370 **		rr_class -- resource record class
371 **		rr_type -- resource record type
372 **		retrans -- retransmission timeout
373 **		retry -- number of retries
374 **
375 **	Returns:
376 **		result of lookup if succeeded.
377 **		NULL otherwise.
378 */
379 
380 DNS_REPLY_T *
381 dns_lookup_int(domain, rr_class, rr_type, retrans, retry)
382 	const char *domain;
383 	int rr_class;
384 	int rr_type;
385 	time_t retrans;
386 	int retry;
387 {
388 	int len;
389 	unsigned long old_options = 0;
390 	time_t save_retrans = 0;
391 	int save_retry = 0;
392 	DNS_REPLY_T *r = NULL;
393 	unsigned char reply[1024];
394 
395 	if (tTd(8, 16))
396 	{
397 		old_options = _res.options;
398 		_res.options |= RES_DEBUG;
399 		sm_dprintf("dns_lookup(%s, %d, %s)\n", domain,
400 			   rr_class, dns_type_to_string(rr_type));
401 	}
402 	if (retrans > 0)
403 	{
404 		save_retrans = _res.retrans;
405 		_res.retrans = retrans;
406 	}
407 	if (retry > 0)
408 	{
409 		save_retry = _res.retry;
410 		_res.retry = retry;
411 	}
412 	errno = 0;
413 	SM_SET_H_ERRNO(0);
414 	len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
415 	if (tTd(8, 16))
416 	{
417 		_res.options = old_options;
418 		sm_dprintf("dns_lookup(%s, %d, %s) --> %d\n",
419 			   domain, rr_class, dns_type_to_string(rr_type), len);
420 	}
421 	if (len >= 0)
422 		r = parse_dns_reply(reply, len);
423 	if (retrans > 0)
424 		_res.retrans = save_retrans;
425 	if (retry > 0)
426 		_res.retry = save_retry;
427 	return r;
428 }
429 
430 #  if 0
431 DNS_REPLY_T *
432 dns_lookup(domain, type_name, retrans, retry)
433 	const char *domain;
434 	const char *type_name;
435 	time_t retrans;
436 	int retry;
437 {
438 	int type;
439 
440 	type = dns_string_to_type(type_name);
441 	if (type == -1)
442 	{
443 		if (tTd(8, 16))
444 			sm_dprintf("dns_lookup: unknown resource type: `%s'\n",
445 				type_name);
446 		return NULL;
447 	}
448 	return dns_lookup_int(domain, C_IN, type, retrans, retry);
449 }
450 #  endif /* 0 */
451 # endif /* NAMED_BIND */
452 #endif /* DNSMAP */
453