2 * Copyright (c) 2000-2004, 2010, 2015, 2020 Proofpoint, 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
45 #include <sm/sendmail.h>
49 # include <netinet/in_systm.h>
50 # include <netinet/ip.h>
52 # if DNSSEC_TEST || _FFR_NAMESERVER
53 # define _DEFINE_SMR_GLOBALS 1
55 # include "sm_resolve.h"
58 #include <arpa/inet.h>
60 SM_RCSID("$Id: sm_resolve.c,v 8.40 2013-11-22 20:51:56 ca Exp $")
89 { "DNSKEY", T_DNSKEY },
95 static DNS_REPLY_T *parse_dns_reply __P((unsigned char *, int, unsigned int));
96 # if DNSSEC_TEST && defined(T_TLSA)
97 static char *hex2bin __P((const char *, int));
101 ** DNS_STRING_TO_TYPE -- convert resource record name into type
104 ** name -- name of resource record type
107 ** type if succeeded.
112 dns_string_to_type(name)
115 struct stot *p = stot;
117 for (p = stot; p->st_name != NULL; p++)
118 if (SM_STRCASEEQ(name, p->st_name))
124 ** DNS_TYPE_TO_STRING -- convert resource record type into name
127 ** type -- resource record type
130 ** name if succeeded.
135 dns_type_to_string(type)
138 struct stot *p = stot;
140 for (p = stot; p->st_name != NULL; p++)
141 if (type == p->st_type)
147 ** DNS_FREE_DATA -- free all components of a DNS_REPLY_T
150 ** dr -- pointer to DNS_REPLY_T
160 RESOURCE_RECORD_T *rr;
164 if (dr->dns_r_q.dns_q_domain != NULL)
165 sm_free(dr->dns_r_q.dns_q_domain);
166 for (rr = dr->dns_r_head; rr != NULL; )
168 RESOURCE_RECORD_T *tmp = rr;
170 if (rr->rr_domain != NULL)
171 sm_free(rr->rr_domain);
172 if (rr->rr_u.rr_data != NULL)
173 sm_free(rr->rr_u.rr_data);
181 ** BIN2HEX -- convert binary TLSA RR to hex string
184 ** tlsa -- pointer to result (allocated here)
185 ** p -- binary data (TLSA RR)
186 ** size -- length of p
187 ** min_size -- minimum expected size
190 ** >0: length of string (*tlsa)
194 static int bin2hex __P((char **, unsigned char *, int, int));
197 bin2hex(tlsa, p, size, min_size)
206 if (txtlen <= size || size < min_size)
209 sm_syslog(LOG_WARNING, NOQID,
210 "ERROR: bin2hex: size %d wrong", size);
213 *tlsa = (char *) sm_malloc(txtlen);
217 sm_dprintf("len=%d, rr_data=NULL\n", txtlen);
220 snprintf(*tlsa, txtlen,
221 "%02X %02X %02X", p[0], p[1], p[2]);
224 /* why isn't there a print function like strlcat? */
225 for (i = 3; i < size && pos < txtlen; i++, pos += 3)
226 snprintf(*tlsa + pos, txtlen - pos, "%c%02X",
227 (i == 3) ? ' ' : ':', p[i]);
233 ** PARSE_DNS_REPLY -- parse DNS reply data.
236 ** data -- pointer to dns data
237 ** len -- len of data
238 ** flags -- flags (RR_*)
241 ** pointer to DNS_REPLY_T if succeeded.
245 ** use dns_free_data() to free() the result when no longer needed.
249 parse_dns_reply(data, len, flags)
255 unsigned short ans_cnt, ui;
258 char host[MAXHOSTNAMELEN];
260 RESOURCE_RECORD_T **rr;
266 fp = fopen("dns.buffer", "w");
269 fwrite(data, 1, len, fp);
274 sm_dprintf("parse_dns_reply: fp=%p, e=%d\n",
278 dr = (DNS_REPLY_T *) sm_malloc(sizeof(*dr));
281 memset(dr, 0, sizeof(*dr));
285 /* doesn't work on Crays? */
286 memcpy(&dr->dns_r_h, p, sizeof(dr->dns_r_h));
287 p += sizeof(dr->dns_r_h);
288 status = dn_expand(data, data + len, p, host, sizeof(host));
291 dr->dns_r_q.dns_q_domain = sm_strdup(host);
292 if (dr->dns_r_q.dns_q_domain == NULL)
295 ans_cnt = ntohs((unsigned short) dr->dns_r_h.ancount);
297 sm_dprintf("parse_dns_reply: ac=%d, ad=%d\n", ans_cnt,
301 GETSHORT(dr->dns_r_q.dns_q_type, p);
302 GETSHORT(dr->dns_r_q.dns_q_class, p);
303 rr = &dr->dns_r_head;
305 while (p < data + len && ui < ans_cnt)
307 int type, class, ttl, size, txtlen;
309 status = dn_expand(data, data + len, p, host, sizeof(host));
318 if (p + size > data + len)
321 ** announced size of data exceeds length of
322 ** data paket: someone is cheating.
326 sm_syslog(LOG_WARNING, NOQID,
327 "ERROR: DNS RDLENGTH=%d > data len=%d",
328 size, len - (int)(p - data));
331 *rr = (RESOURCE_RECORD_T *) sm_malloc(sizeof(**rr));
334 memset(*rr, 0, sizeof(**rr));
335 (*rr)->rr_domain = sm_strdup(host);
336 if ((*rr)->rr_domain == NULL)
338 (*rr)->rr_type = type;
339 (*rr)->rr_class = class;
341 (*rr)->rr_size = size;
347 status = dn_expand(data, data + len, p, host,
352 sm_dprintf("parse_dns_reply: type=%s, host=%s\n",
353 dns_type_to_string(type), host);
354 (*rr)->rr_u.rr_txt = sm_strdup(host);
355 if ((*rr)->rr_u.rr_txt == NULL)
361 status = dn_expand(data, data + len, p + 2, host,
365 l = strlen(host) + 1;
366 (*rr)->rr_u.rr_mx = (MX_RECORD_T *)
367 sm_malloc(sizeof(*((*rr)->rr_u.rr_mx)) + l);
368 if ((*rr)->rr_u.rr_mx == NULL)
370 (*rr)->rr_u.rr_mx->mx_r_preference = (p[0] << 8) | p[1];
371 (void) sm_strlcpy((*rr)->rr_u.rr_mx->mx_r_domain,
374 sm_dprintf("mx=%s, pref=%d\n", host,
375 (*rr)->rr_u.rr_mx->mx_r_preference);
379 status = dn_expand(data, data + len, p + 6, host,
383 l = strlen(host) + 1;
384 (*rr)->rr_u.rr_srv = (SRV_RECORDT_T*)
385 sm_malloc(sizeof(*((*rr)->rr_u.rr_srv)) + l);
386 if ((*rr)->rr_u.rr_srv == NULL)
388 (*rr)->rr_u.rr_srv->srv_r_priority = (p[0] << 8) | p[1];
389 (*rr)->rr_u.rr_srv->srv_r_weight = (p[2] << 8) | p[3];
390 (*rr)->rr_u.rr_srv->srv_r_port = (p[4] << 8) | p[5];
391 (void) sm_strlcpy((*rr)->rr_u.rr_srv->srv_r_target,
398 ** The TXT record contains the length as
399 ** leading byte, hence the value is restricted
400 ** to 255, which is less than the maximum value
401 ** of RDLENGTH (size). Nevertheless, txtlen
402 ** must be less than size because the latter
403 ** specifies the length of the entire TXT
411 sm_syslog(LOG_WARNING, NOQID,
412 "ERROR: DNS TXT record size=%d <= text len=%d",
416 (*rr)->rr_u.rr_txt = (char *) sm_malloc(txtlen + 1);
417 if ((*rr)->rr_u.rr_txt == NULL)
419 (void) sm_strlcpy((*rr)->rr_u.rr_txt, (char*) p + 1,
426 sm_dprintf("parse_dns_reply: TLSA, size=%d, flags=%X\n",
428 if ((flags & RR_AS_TEXT) != 0)
430 txtlen = bin2hex((char **)&((*rr)->rr_u.rr_data),
437 /* return "raw" data for caller to use as it pleases */
441 (*rr)->rr_u.rr_data = (unsigned char*) sm_malloc(size);
442 if ((*rr)->rr_u.rr_data == NULL)
444 (void) memcpy((*rr)->rr_u.rr_data, p, size);
445 if (tTd(8, 61) && type == T_A)
449 (void) memcpy((void *)&addr.sin.sin_addr.s_addr, p, size);
450 sm_dprintf("parse_dns_reply: IPv4=%s\n",
451 inet_ntoa(addr.sin.sin_addr));
456 rr = &(*rr)->rr_next;
468 # include <arpa/nameser.h>
469 # if _FFR_8BITENVADDR
470 # include <sm/sendmail.h>
473 static int gen_dns_reply __P((unsigned char *, int, unsigned char *,
474 const char *, int, const char *, int, int, int, int,
475 const char *, int, int, int));
476 static int dnscrtrr __P((const char *, const char *, int, char *, int,
477 unsigned int, int *, int *, unsigned char *, int, unsigned char *));
480 ** HERRNO2TXT -- return error text for h_errno
486 ** DNS error text if available
500 return "HOST_NOT_FOUND";
504 return "NO_RECOVERY";
508 return "bogus h_errno";
512 ** GEN_DNS_REPLY -- generate DNS reply data.
515 ** buf -- buffer to which DNS data is written
516 ** buflen -- length of buffer
517 ** bufpos -- position in buffer where DNS RRs are appended
518 ** query -- name of query
519 ** qtype -- resource record type of query
520 ** domain -- name of domain which has been "found"
521 ** class -- resource record class
522 ** type -- resource record type
524 ** size -- size of data
526 ** txtlen -- length of text
527 ** pref -- MX preference
531 ** >0 length of buffer that has been used.
536 gen_dns_reply(buf, buflen, bufpos, query, qtype, domain, class, type, ttl, size, data, txtlen, pref, ad)
539 unsigned char *bufpos;
552 unsigned short ans_cnt;
554 unsigned char *cp, *ep;
556 static unsigned char *dnptrs[20], **dpp, **lastdnptr;
558 #define DN_COMP_CHK do \
563 sm_dprintf("gen_dns_reply: dn_comp=%d\n", n); \
568 SM_REQUIRE(NULL != buf);
569 SM_REQUIRE(buflen >= HFIXEDSZ);
570 SM_REQUIRE(query != NULL);
579 sm_dprintf("gen_dns_reply: query=%s, domain=%s, type=%s, size=%d, ad=%d\n",
580 query, domain, dns_type_to_string(type), size, ad);
584 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
586 memset(buf, 0, HFIXEDSZ);
587 hp->id = 0xdead; /* HACK */
590 hp->rd = 0; /* recursion desired? */
591 hp->rcode = 0; /* !!! */
592 /* hp->aa = ?; * !!! */
593 /* hp->tc = ?; * !!! */
594 /* hp->ra = ?; * !!! */
595 hp->qdcount = htons(1);
598 n = dn_comp(query, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr);
606 if (ep - cp < QFIXEDSZ)
609 sm_dprintf("gen_dns_reply: ep-cp=%ld\n",
613 n = dn_comp(domain, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr);
620 ans_cnt = ntohs((unsigned short) hp->ancount);
622 hp->ancount = htons((unsigned short) ans_cnt);
627 n = dn_comp(data, cp + 4, ep - cp - QFIXEDSZ, dnptrs, lastdnptr);
637 PUTSHORT(txtlen, cp);
638 (void) sm_strlcpy((char *)cp, data, txtlen + 1);
643 n = dn_comp(data, cp + 2, ep - cp - QFIXEDSZ, dnptrs, lastdnptr);
654 tlsa = hex2bin(data, size);
659 (void) memcpy(cp, tlsa, n);
667 (void) memcpy(cp, data, size);
676 ** SETHERRNOFROMSTRING -- set h_errno based on text
679 ** str -- string which might contain h_errno text
680 ** prc -- pointer to rcode (EX_*)
688 setherrnofromstring(str, prc)
693 if (SM_IS_EMPTY(str))
695 if (strstr(str, "herrno:") == NULL)
699 if (strstr(str, "host_not_found"))
700 SM_SET_H_ERRNO(HOST_NOT_FOUND);
701 else if (strstr(str, "try_again"))
703 SM_SET_H_ERRNO(TRY_AGAIN);
707 else if (strstr(str, "no_recovery"))
708 SM_SET_H_ERRNO(NO_RECOVERY);
709 else if (strstr(str, "no_data"))
710 SM_SET_H_ERRNO(NO_DATA);
712 SM_SET_H_ERRNO(NETDB_INTERNAL);
717 ** GETTTLFROMSTRING -- extract ttl from a string
720 ** str -- string which might contain ttl
728 getttlfromstring(str)
731 if (SM_IS_EMPTY(str))
733 #define TTL_PRE "ttl="
734 if (strstr(str, TTL_PRE) == NULL)
736 return strtoul(str + strlen(TTL_PRE), NULL, 10);
742 ** HEX2BIN -- convert hex string to binary TLSA RR
745 ** p -- hex representation of TLSA RR
746 ** size -- length of p
749 ** pointer to binary TLSA RR
762 if (txtlen * 2 == size)
765 sm_syslog(LOG_WARNING, NOQID,
766 "ERROR: hex2bin: size %d wrong", size);
769 tlsa = sm_malloc(txtlen + 1);
773 sm_dprintf("len=%d, tlsa=NULL\n", txtlen);
777 #define CHAR2INT(c) (((c) <= '9') ? ((c) - '0') : (toupper(c) - 'A' + 10))
778 for (i = 0, pos = 0; i + 1 < size && pos < txtlen; i += 2, pos++)
779 tlsa[pos] = CHAR2INT(p[i]) * 16 + CHAR2INT(p[i+1]);
810 ** DNSCRTRR -- create DNS RR
813 ** domain -- original query domain
814 ** query -- name of query
815 ** qtype -- resource record type of query
816 ** value -- (list of) data to set
817 ** rr_type -- resource record type
818 ** flags -- flags how to handle various lookups
819 ** herr -- (pointer to) h_errno (output if non-NULL)
820 ** adp -- (pointer to) ad flag
821 ** answer -- buffer for RRs
822 ** anslen -- size of answer
823 ** anspos -- current position in answer
826 ** >0: length of data in answer
827 ** <0: error, check *herr
831 dnscrtrr(domain, query, qtype, value, rr_type, flags, herr, adp, answer, anslen, anspos)
840 unsigned char *answer;
842 unsigned char *anspos;
847 char data[IN6ADDRSZ];
851 if (SM_IS_EMPTY(value))
853 SM_REQUIRE(adp != NULL);
854 (void) sm_strlcpy(rhs, value, sizeof(rhs));
856 if (setherrnofromstring(p, NULL) != 0)
861 sm_dprintf("dnscrtrr rhs=%s h_errno=%d (%s)\n",
862 p, h_errno, herrno2txt(h_errno));
868 for (token = p; token != NULL && *token != '\0'; token = p)
871 while (p != NULL && *p != '\0' && !SM_ISSPACE(*p))
875 sm_dprintf("dnscrtrr: token=%s\n", token);
876 if (strcmp(token, "ad") == 0)
880 adflag = (_res.options & RES_USE_DNSSEC) != 0;
882 /* maybe print this only for the final RR? */
884 sm_dprintf("dnscrtrr: ad=1, adp=%d, adflag=%d\n",
886 if (*adp != 0 && adflag)
893 if (ttl == 0 && (ttl = getttlfromstring(token)) > 0)
896 sm_dprintf("dnscrtrr: ttl=%d\n", ttl);
902 addr.sin.sin_addr.s_addr = inet_addr(token);
903 (void) memmove(data, (void *)&addr.sin.sin_addr.s_addr,
905 rlen = gen_dns_reply(answer, anslen, anspos,
906 query, qtype, domain, C_IN, rr_type, ttl,
907 INADDRSZ, data, 0, 0, ad);
911 if (rr_type == T_AAAA)
913 anynet_pton(AF_INET6, token, &addr.sin6.sin6_addr);
914 memmove(data, (void *)&addr.sin6.sin6_addr, IN6ADDRSZ);
915 rlen = gen_dns_reply(answer, anslen, anspos,
916 query, qtype, domain, C_IN, rr_type, ttl,
917 IN6ADDRSZ, data, 0, 0, ad);
919 # endif /* NETINET6 */
926 pref = (int) strtoul(token, &endptr, 10);
927 if (endptr == NULL || *endptr != ':')
930 rlen = gen_dns_reply(answer, anslen, anspos,
931 query, qtype, domain, C_IN, rr_type, ttl,
932 strlen(token) + 1, token, 0, pref, ad);
934 sm_dprintf("dnscrtrr: mx=%s, pref=%d, rlen=%d\n",
939 if (rr_type == T_TLSA)
940 rlen = gen_dns_reply(answer, anslen, anspos,
941 query, qtype, domain, C_IN, rr_type, ttl,
942 strlen(token) + 1, token, 0, 0, ad);
945 if (rr_type == T_CNAME)
946 rlen = gen_dns_reply(answer, anslen, anspos,
947 query, qtype, domain, C_IN, rr_type, ttl,
948 strlen(token), token, 0, 0, ad);
952 anspos = answer + rlen;
961 if (herr != NULL && 0 == *herr)
967 ** TSTDNS_SEARCH -- replacement for res_search() for testing
970 ** domain -- query domain
972 ** type -- resource record type
973 ** answer -- buffer for RRs
974 ** anslen -- size of answer
977 ** >0: length of data in answer
978 ** <0: error, check h_errno
982 tstdns_search(domain, class, type, answer, anslen)
986 unsigned char *answer;
989 int rlen, ad, maprcode, cnt, flags, herr;
996 # if _FFR_8BITENVADDR
997 char qbuf[MAXNAME_I];
1000 # define qdomain domain
1002 char key[MAXNAME_I + 16];
1004 unsigned char *anspos;
1010 if (SM_IS_EMPTY(domain))
1012 tag = rr_type2tag(type);
1018 # if _FFR_8BITENVADDR
1020 sm_dprintf("domain=%s\n", domain);
1021 (void) dequote_internal_chars((char *)domain, qbuf, sizeof(qbuf));
1025 sm_dprintf("qdomain=%s\n", qdomain);
1028 # endif /* _FFR_8BITENVADDR */
1031 map = stab("access", ST_MAP, ST_FIND);
1034 sm_dprintf("access map not found\n");
1037 if (!bitset(MF_OPEN, map->s_map.map_mflags) &&
1038 !openmap(&(map->s_map)))
1040 sm_dprintf("access map open failed\n");
1045 ** Look up tag:domain, if not found and domain does not end with a dot
1046 ** (and the proper debug level is selected), also try with trailing dot.
1049 #define SM_LOOKUP2(tag) \
1053 len = strlen(qdomain); \
1056 snprintf(key, sizeof(key), "%s:%s", tag, qdomain); \
1057 p = (*map->s_map.map_class->map_lookup)(&map->s_map, key, av, \
1061 if (!tTd(8, 112) || (len > 0 && '.' == qdomain[len - 1])) \
1063 snprintf(key, sizeof(key), "%s:%s.", tag, qdomain); \
1064 p = (*map->s_map.map_class->map_lookup)(&map->s_map, key, av, \
1069 found_cname = false;
1074 /* Should this try with/without trailing dot? */
1078 sm_dprintf("access map lookup key=%s, value=%s\n", key,
1082 if (NULL == p && (flags & RR_NO_CNAME) == 0)
1084 sm_dprintf("access map lookup failed key=%s, try cname\n",
1086 SM_LOOKUP2("cname");
1089 sm_dprintf("cname lookup key=%s, value=%s, ad=%d\n",
1091 rlen = dnscrtrr(qdomain, query, type, p, T_CNAME,
1092 flags, &herr, &ad, answer,
1097 anspos = answer + rlen;
1104 (void) sm_strlcpy(rhs, p, sizeof(rhs));
1107 /* skip (leading) ad/ttl: look for last ' ' */
1108 if ((last = strrchr(p, ' ')) != NULL && last[1] != '\0')
1117 char *tags[] = { "ipv4", "mx", "tlsa",
1124 for (t = 0; tags[t] != NULL; t++)
1126 if (strcmp(tag, tags[t]) == 0)
1128 SM_LOOKUP2(tags[t]);
1131 sm_dprintf("access map lookup failed key=%s:%s, but found key=%s\n",
1137 sm_dprintf("access map lookup failed key=%s\n", key);
1138 herr = HOST_NOT_FOUND;
1141 if (found_cname && (flags & RR_ONLY_CNAME) != 0)
1143 rlen = dnscrtrr(qdomain, query, type, p, type, flags, &herr, &ad,
1144 answer, anslen, anspos);
1152 SM_SET_H_ERRNO(herr);
1153 sm_dprintf("rlen=%d, herr=%d\n", rlen, herr);
1158 ** TSTDNS_QUERYDOMAIN -- replacement for res_querydomain() for testing
1161 ** name -- query name
1162 ** domain -- query domain
1164 ** type -- resource record type
1165 ** answer -- buffer for RRs
1166 ** anslen -- size of answer
1169 ** >0: length of data in answer
1170 ** <0: error, check h_errno
1174 tstdns_querydomain(name, domain, class, type, answer, anslen)
1179 unsigned char *answer;
1182 char query[MAXNAME_I];
1187 if (SM_IS_EMPTY(domain))
1188 return tstdns_search(name, class, type, answer, anslen);
1190 len = snprintf(query, sizeof(query), "%s.%s", name, domain);
1191 if (len >= (int)sizeof(query))
1193 return tstdns_search(query, class, type, answer, anslen);
1196 SM_SET_H_ERRNO(NO_RECOVERY);
1200 # endif /* DNSSEC_TEST */
1203 ** DNS_LOOKUP_INT -- perform DNS lookup
1206 ** domain -- name to look up
1207 ** rr_class -- resource record class
1208 ** rr_type -- resource record type
1209 ** retrans -- retransmission timeout
1210 ** retry -- number of retries
1211 ** options -- DNS resolver options
1212 ** flags -- currently only passed to parse_dns_reply()
1213 ** err -- (pointer to) errno (output if non-NULL)
1214 ** herr -- (pointer to) h_errno (output if non-NULL)
1217 ** result of lookup if succeeded.
1222 dns_lookup_int(domain, rr_class, rr_type, retrans, retry, options, flags, err, herr)
1228 unsigned int options;
1234 unsigned long old_options = 0;
1235 time_t save_retrans = 0;
1237 DNS_REPLY_T *dr = NULL;
1239 unsigned char *reply;
1240 int (*resfunc) __P((const char *, int, int, u_char *, int));
1242 # define SMRBSIZE ((int) sizeof(reply_buf))
1243 # ifndef IP_MAXPACKET
1244 # define IP_MAXPACKET 65535
1247 resfunc = res_search;
1250 resfunc = tstdns_search;
1253 old_options = _res.options;
1254 _res.options |= options;
1261 _res.options |= RES_DEBUG;
1262 sm_dprintf("dns_lookup_int(%s, %d, %s, %x)\n", domain,
1263 rr_class, dns_type_to_string(rr_type), options);
1267 sm_dprintf("NS=%s, port=%d\n",
1268 inet_ntoa(_res.nsaddr_list[0].sin_addr),
1269 ntohs(_res.nsaddr_list[0].sin_port));
1273 save_retrans = _res.retrans;
1274 _res.retrans = retrans;
1278 save_retry = _res.retry;
1283 reply = (unsigned char *)&reply_buf;
1284 len = (*resfunc)(domain, rr_class, rr_type, reply, SMRBSIZE);
1285 if (len >= SMRBSIZE)
1287 if (len >= IP_MAXPACKET)
1290 sm_dprintf("dns_lookup: domain=%s, length=%d, default_size=%d, max=%d, status=response too long\n",
1291 domain, len, SMRBSIZE, IP_MAXPACKET);
1296 sm_dprintf("dns_lookup: domain=%s, length=%d, default_size=%d, max=%d, status=response longer than default size, resizing\n",
1297 domain, len, SMRBSIZE, IP_MAXPACKET);
1298 reply = (unsigned char *)sm_malloc(IP_MAXPACKET);
1300 SM_SET_H_ERRNO(TRY_AGAIN);
1304 len = (*resfunc)(domain, rr_class, rr_type,
1305 reply, IP_MAXPACKET);
1309 _res.options = old_options;
1318 sm_dprintf("dns_lookup_int(%s, %d, %s, %x)=%d, errno=%d, h_errno=%d"
1323 domain, rr_class, dns_type_to_string(rr_type),
1324 options, len, errno, h_errno
1326 , herrno2txt(h_errno)
1331 else if (tTd(8, 16))
1333 sm_dprintf("dns_lookup_int(%s, %d, %s, %x)=%d\n",
1334 domain, rr_class, dns_type_to_string(rr_type),
1337 if (len >= 0 && len < IP_MAXPACKET && reply != NULL)
1338 dr = parse_dns_reply(reply, len, flags);
1339 if (reply != (unsigned char *)&reply_buf && reply != NULL)
1345 _res.retrans = save_retrans;
1347 _res.retry = save_retry;
1352 ** DNS_LOOKUP_MAP -- perform DNS map lookup
1355 ** domain -- name to look up
1356 ** rr_class -- resource record class
1357 ** rr_type -- resource record type
1358 ** retrans -- retransmission timeout
1359 ** retry -- number of retries
1360 ** options -- DNS resolver options
1363 ** result of lookup if succeeded.
1368 dns_lookup_map(domain, rr_class, rr_type, retrans, retry, options)
1374 unsigned int options;
1376 return dns_lookup_int(domain, rr_class, rr_type, retrans, retry,
1377 options, RR_AS_TEXT, NULL, NULL);
1382 ** DNS2HE -- convert DNS_REPLY_T list to hostent struct
1385 ** dr -- DNS lookup result
1386 ** family -- address family
1389 ** hostent struct if succeeded.
1393 ** this returns a pointer to a static struct!
1401 # define SM_MAX_ADDRS 256
1402 static struct hostent he;
1403 static char *he_aliases[1];
1404 static char *he_addr_list[SM_MAX_ADDRS];
1406 # define IN_ADDRSZ IN6ADDRSZ
1408 # define IN_ADDRSZ INADDRSZ
1410 static char he_addrs[SM_MAX_ADDRS * IN_ADDRSZ];
1411 static char he_name[MAXNAME_I];
1412 static bool he_init = false;
1416 # if NETINET6 && DNSSEC_TEST
1417 struct in6_addr ia6;
1418 char buf6[INET6_ADDRSTRLEN];
1420 RESOURCE_RECORD_T *rr;
1428 he_aliases[0] = NULL;
1429 he.h_aliases = he_aliases;
1430 he.h_addr_list = he_addr_list;
1431 he.h_name = he_name;
1434 h->h_addrtype = family;
1437 sm_dprintf("dns2he: ad=%d\n", dr->dns_r_h.ad);
1439 /* do we want/need to copy the name? */
1440 rr = dr->dns_r_head;
1441 if (rr != NULL && rr->rr_domain != NULL)
1442 sm_strlcpy(h->h_name, rr->rr_domain, sizeof(he_name));
1444 h->h_name[0] = '\0';
1448 if (family == AF_INET)
1452 if (family == AF_INET6)
1459 for (rr = dr->dns_r_head, i = 0; rr != NULL && i < SM_MAX_ADDRS - 1;
1462 h->h_addr_list[i] = he_addrs + i * h->h_length;
1463 switch (rr->rr_type)
1467 if (family != AF_INET)
1469 memmove(h->h_addr_list[i], rr->rr_u.rr_a, INADDRSZ);
1472 # endif /* NETINET */
1475 if (family != AF_INET6)
1477 memmove(h->h_addr_list[i], rr->rr_u.rr_aaaa, IN6ADDRSZ);
1480 # endif /* NETINET6 */
1484 sm_dprintf("dns2he: cname: %s ttl=%d\n",
1485 rr->rr_u.rr_txt, rr->rr_ttl);
1491 sm_dprintf("dns2he: mx: %d %s ttl=%d\n",
1492 rr->rr_u.rr_mx->mx_r_preference,
1493 rr->rr_u.rr_mx->mx_r_domain,
1498 # if defined(T_TLSA)
1506 len = bin2hex(&tlsa, rr->rr_u.rr_data,
1509 sm_dprintf("dns2he: tlsa: %s ttl=%d\n",
1514 # endif /* T_TLSA */
1518 /* complain if list is too long! */
1519 SM_ASSERT(i < SM_MAX_ADDRS);
1520 h->h_addr_list[i] = NULL;
1527 for (i = 0; h->h_addr_list[i] != NULL && i < SM_MAX_ADDRS; i++)
1533 if (h->h_addrtype == AF_INET6)
1535 memmove(&ia6, h->h_addr_list[i], IN6ADDRSZ);
1536 addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
1539 # endif /* NETINET6 */
1540 /* "else" in #if code above */
1542 memmove(&ia, h->h_addr_list[i], INADDRSZ);
1543 addr = (char *) inet_ntoa(ia);
1546 sm_dprintf("dns2he: addr[%d]: %s\n", i, addr);
1549 # endif /* DNSSEC_TEST */
1553 # endif /* DNSMAP || DANE */
1555 # if DNSSEC_TEST || _FFR_NAMESERVER
1557 ** DNS_ADDNS -- add one NS in resolver context
1560 ** ns -- (IPv4 address of) nameserver
1561 ** port -- nameserver port (host order)
1567 static void dns_addns __P((struct in_addr *, unsigned int));
1568 static int nsidx = 0;
1578 syserr("too many NameServers defined (%d max)", MAXNS);
1579 _res.nsaddr_list[nsidx].sin_family = AF_INET;
1580 _res.nsaddr_list[nsidx].sin_addr = *ns;
1582 _res.nsaddr_list[nsidx].sin_port = htons(port);
1583 _res.nscount = ++nsidx;
1585 sm_dprintf("dns_addns: nsidx=%d, ns=%s:%u\n",
1586 nsidx - 1, inet_ntoa(*ns), port);
1590 ** NSPORTIP -- parse port@IPv4 and set NS accordingly
1600 ** sets NS for DNS lookups
1604 ** There should be a generic function for this...
1605 ** milter_open(), socket_map_open(), others?
1614 unsigned short port;
1615 struct in_addr nsip;
1621 while (SM_ISSPACE(*p))
1629 if (isascii(*p) && isdigit(*p))
1637 r = inet_pton(AF_INET, p, &nsip);
1640 if ((_res.options & RES_INIT) == 0)
1642 dns_addns(&nsip, port);
1646 return r > 0 ? 0 : -1;
1648 # endif /* DNSSEC_TEST || _FFR_NAMESERVER */
1649 #endif /* NAMED_BIND */