2 * Copyright (c) 2000-2004, 2010 Sendmail, Inc. and its suppliers.
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.
12 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
13 * (Royal Institute of Technology, Stockholm, Sweden).
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
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.
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.
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
48 # include <netinet/in_systm.h>
49 # include <netinet/ip.h>
51 # include "sm_resolve.h"
53 SM_RCSID("$Id: sm_resolve.c,v 8.39 2010/06/29 15:35:33 ca Exp $")
66 # endif /* NETINET6 */
77 static DNS_REPLY_T *parse_dns_reply __P((unsigned char *, int));
80 ** DNS_STRING_TO_TYPE -- convert resource record name into type
83 ** name -- name of resource record type
91 dns_string_to_type(name)
94 struct stot *p = stot;
96 for (p = stot; p->st_name != NULL; p++)
97 if (sm_strcasecmp(name, p->st_name) == 0)
103 ** DNS_TYPE_TO_STRING -- convert resource record type into name
106 ** type -- resource record type
109 ** name if succeeded.
114 dns_type_to_string(type)
117 struct stot *p = stot;
119 for (p = stot; p->st_name != NULL; p++)
120 if (type == p->st_type)
126 ** DNS_FREE_DATA -- free all components of a DNS_REPLY_T
129 ** r -- pointer to DNS_REPLY_T
139 RESOURCE_RECORD_T *rr;
141 if (r->dns_r_q.dns_q_domain != NULL)
142 sm_free(r->dns_r_q.dns_q_domain);
143 for (rr = r->dns_r_head; rr != NULL; )
145 RESOURCE_RECORD_T *tmp = rr;
147 if (rr->rr_domain != NULL)
148 sm_free(rr->rr_domain);
149 if (rr->rr_u.rr_data != NULL)
150 sm_free(rr->rr_u.rr_data);
158 ** PARSE_DNS_REPLY -- parse DNS reply data.
161 ** data -- pointer to dns data
162 ** len -- len of data
165 ** pointer to DNS_REPLY_T if succeeded.
170 parse_dns_reply(data, len)
175 unsigned short ans_cnt, ui;
178 char host[MAXHOSTNAMELEN];
180 RESOURCE_RECORD_T **rr;
182 r = (DNS_REPLY_T *) sm_malloc(sizeof(*r));
185 memset(r, 0, sizeof(*r));
189 /* doesn't work on Crays? */
190 memcpy(&r->dns_r_h, p, sizeof(r->dns_r_h));
191 p += sizeof(r->dns_r_h);
192 status = dn_expand(data, data + len, p, host, sizeof(host));
198 r->dns_r_q.dns_q_domain = sm_strdup(host);
199 if (r->dns_r_q.dns_q_domain == NULL)
205 ans_cnt = ntohs((unsigned short) r->dns_r_h.ancount);
208 GETSHORT(r->dns_r_q.dns_q_type, p);
209 GETSHORT(r->dns_r_q.dns_q_class, p);
212 while (p < data + len && ui < ans_cnt)
214 int type, class, ttl, size, txtlen;
216 status = dn_expand(data, data + len, p, host, sizeof(host));
228 if (p + size > data + len)
231 ** announced size of data exceeds length of
232 ** data paket: someone is cheating.
236 sm_syslog(LOG_WARNING, NOQID,
237 "ERROR: DNS RDLENGTH=%d > data len=%d",
238 size, len - (p - data));
242 *rr = (RESOURCE_RECORD_T *) sm_malloc(sizeof(**rr));
248 memset(*rr, 0, sizeof(**rr));
249 (*rr)->rr_domain = sm_strdup(host);
250 if ((*rr)->rr_domain == NULL)
255 (*rr)->rr_type = type;
256 (*rr)->rr_class = class;
258 (*rr)->rr_size = size;
264 status = dn_expand(data, data + len, p, host,
271 (*rr)->rr_u.rr_txt = sm_strdup(host);
272 if ((*rr)->rr_u.rr_txt == NULL)
281 status = dn_expand(data, data + len, p + 2, host,
288 l = strlen(host) + 1;
289 (*rr)->rr_u.rr_mx = (MX_RECORD_T *)
290 sm_malloc(sizeof(*((*rr)->rr_u.rr_mx)) + l);
291 if ((*rr)->rr_u.rr_mx == NULL)
296 (*rr)->rr_u.rr_mx->mx_r_preference = (p[0] << 8) | p[1];
297 (void) sm_strlcpy((*rr)->rr_u.rr_mx->mx_r_domain,
302 status = dn_expand(data, data + len, p + 6, host,
309 l = strlen(host) + 1;
310 (*rr)->rr_u.rr_srv = (SRV_RECORDT_T*)
311 sm_malloc(sizeof(*((*rr)->rr_u.rr_srv)) + l);
312 if ((*rr)->rr_u.rr_srv == NULL)
317 (*rr)->rr_u.rr_srv->srv_r_priority = (p[0] << 8) | p[1];
318 (*rr)->rr_u.rr_srv->srv_r_weight = (p[2] << 8) | p[3];
319 (*rr)->rr_u.rr_srv->srv_r_port = (p[4] << 8) | p[5];
320 (void) sm_strlcpy((*rr)->rr_u.rr_srv->srv_r_target,
327 ** The TXT record contains the length as
328 ** leading byte, hence the value is restricted
329 ** to 255, which is less than the maximum value
330 ** of RDLENGTH (size). Nevertheless, txtlen
331 ** must be less than size because the latter
332 ** specifies the length of the entire TXT
340 sm_syslog(LOG_WARNING, NOQID,
341 "ERROR: DNS TXT record size=%d <= text len=%d",
346 (*rr)->rr_u.rr_txt = (char *) sm_malloc(txtlen + 1);
347 if ((*rr)->rr_u.rr_txt == NULL)
352 (void) sm_strlcpy((*rr)->rr_u.rr_txt, (char*) p + 1,
357 (*rr)->rr_u.rr_data = (unsigned char*) sm_malloc(size);
358 if ((*rr)->rr_u.rr_data == NULL)
363 (void) memcpy((*rr)->rr_u.rr_data, p, size);
367 rr = &(*rr)->rr_next;
374 ** DNS_LOOKUP_INT -- perform dns map lookup (internal helper routine)
377 ** domain -- name to lookup
378 ** rr_class -- resource record class
379 ** rr_type -- resource record type
380 ** retrans -- retransmission timeout
381 ** retry -- number of retries
384 ** result of lookup if succeeded.
389 dns_lookup_int(domain, rr_class, rr_type, retrans, retry)
397 unsigned long old_options = 0;
398 time_t save_retrans = 0;
400 DNS_REPLY_T *r = NULL;
402 unsigned char *reply;
404 #define SMRBSIZE sizeof(reply_buf)
406 # define IP_MAXPACKET 65535
411 old_options = _res.options;
412 _res.options |= RES_DEBUG;
413 sm_dprintf("dns_lookup(%s, %d, %s)\n", domain,
414 rr_class, dns_type_to_string(rr_type));
418 save_retrans = _res.retrans;
419 _res.retrans = retrans;
423 save_retry = _res.retry;
428 reply = (unsigned char *)&reply_buf;
429 len = res_search(domain, rr_class, rr_type, reply, SMRBSIZE);
432 if (len >= IP_MAXPACKET)
435 sm_dprintf("dns_lookup: domain=%s, length=%d, default_size=%d, max=%d, status=response too long\n",
436 domain, len, (int) SMRBSIZE,
442 sm_dprintf("dns_lookup: domain=%s, length=%d, default_size=%d, max=%d, status=response longer than default size, resizing\n",
443 domain, len, (int) SMRBSIZE,
445 reply = (unsigned char *)sm_malloc(IP_MAXPACKET);
447 SM_SET_H_ERRNO(TRY_AGAIN);
449 len = res_search(domain, rr_class, rr_type,
450 reply, IP_MAXPACKET);
455 _res.options = old_options;
456 sm_dprintf("dns_lookup(%s, %d, %s) --> %d\n",
457 domain, rr_class, dns_type_to_string(rr_type), len);
459 if (len >= 0 && len < IP_MAXPACKET && reply != NULL)
460 r = parse_dns_reply(reply, len);
461 if (reply != (unsigned char *)&reply_buf && reply != NULL)
467 _res.retrans = save_retrans;
469 _res.retry = save_retry;
475 dns_lookup(domain, type_name, retrans, retry)
477 const char *type_name;
483 type = dns_string_to_type(type_name);
487 sm_dprintf("dns_lookup: unknown resource type: `%s'\n",
491 return dns_lookup_int(domain, C_IN, type, retrans, retry);
494 # endif /* NAMED_BIND */