2 * Copyright (c) 2014 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014 Jakub Hrozek <jakub.hrozek@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <sys/types.h>
51 /* GCC has printf type attribute check. */
52 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
53 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
55 #define PRINTF_ATTRIBUTE(a,b)
56 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
58 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
59 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
61 #define DESTRUCTOR_ATTRIBUTE
62 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
64 #ifndef RWRAP_DEFAULT_FAKE_TTL
65 #define RWRAP_DEFAULT_FAKE_TTL 600
66 #endif /* RWRAP_DEFAULT_FAKE_TTL */
76 # define RWRAP_LOG(...)
79 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
80 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
82 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
84 const char *format, ...)
92 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
98 vsnprintf(buffer, sizeof(buffer), format, va);
103 case RWRAP_LOG_ERROR:
105 "RWRAP_ERROR(%d) - %s: %s\n",
110 "RWRAP_WARN(%d) - %s: %s\n",
113 case RWRAP_LOG_DEBUG:
115 "RWRAP_DEBUG(%d) - %s: %s\n",
118 case RWRAP_LOG_TRACE:
120 "RWRAP_TRACE(%d) - %s: %s\n",
126 #endif /* NDEBUG RWRAP_LOG */
129 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
132 #define NEXT_KEY(buf, key) do { \
133 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
134 if ((key) != NULL) { \
138 while ((key) != NULL \
139 && (isblank((int)(key)[0]))) { \
144 #define RWRAP_MAX_RECURSION 5
146 /* Priority and weight can be omitted from the hosts file, but need to be part
149 #define DFL_SRV_PRIO 1
150 #define DFL_SRV_WEIGHT 100
152 struct rwrap_srv_rrdata {
156 char hostname[MAXDNAME];
159 struct rwrap_soa_rrdata {
165 char nameserver[MAXDNAME];
166 char mailbox[MAXDNAME];
169 struct rwrap_fake_rr {
171 struct in_addr a_rec;
172 struct in6_addr aaaa_rec;
173 struct rwrap_srv_rrdata srv_rec;
174 struct rwrap_soa_rrdata soa_rec;
175 char cname_rec[MAXDNAME];
179 int type; /* ns_t_* */
182 static void rwrap_fake_rr_init(struct rwrap_fake_rr *rr, size_t len)
186 for (i = 0; i < len; i++) {
187 rr[i].type = ns_t_invalid;
191 static int rwrap_create_fake_a_rr(const char *key,
193 struct rwrap_fake_rr *rr)
197 ok = inet_pton(AF_INET, value, &rr->rrdata.a_rec);
199 RWRAP_LOG(RWRAP_LOG_ERROR,
200 "Failed to convert [%s] to binary\n", value);
204 memcpy(rr->key, key, strlen(key) + 1);
209 static int rwrap_create_fake_aaaa_rr(const char *key,
211 struct rwrap_fake_rr *rr)
215 ok = inet_pton(AF_INET6, value, &rr->rrdata.aaaa_rec);
217 RWRAP_LOG(RWRAP_LOG_ERROR,
218 "Failed to convert [%s] to binary\n", value);
222 memcpy(rr->key, key, strlen(key) + 1);
223 rr->type = ns_t_aaaa;
227 static int rwrap_create_fake_srv_rr(const char *key,
229 struct rwrap_fake_rr *rr)
234 const char *hostname;
236 /* parse the value into priority, weight, port and hostname
237 * and check the validity */
239 NEXT_KEY(hostname, str_port);
240 NEXT_KEY(str_port, str_prio);
241 NEXT_KEY(str_prio, str_weight);
242 if (str_port == NULL || hostname == NULL) {
243 RWRAP_LOG(RWRAP_LOG_ERROR,
244 "Malformed SRV entry [%s]\n", value);
249 rr->rrdata.srv_rec.prio = atoi(str_prio);
251 rr->rrdata.srv_rec.prio = DFL_SRV_PRIO;
254 rr->rrdata.srv_rec.weight = atoi(str_weight);
256 rr->rrdata.srv_rec.weight = DFL_SRV_WEIGHT;
258 rr->rrdata.srv_rec.port = atoi(str_port);
259 memcpy(rr->rrdata.srv_rec.hostname , hostname, strlen(hostname) + 1);
261 memcpy(rr->key, key, strlen(key) + 1);
266 static int rwrap_create_fake_soa_rr(const char *key,
268 struct rwrap_fake_rr *rr)
270 const char *nameserver;
278 /* parse the value into nameserver, mailbox, serial, refresh,
279 * retry, expire, minimum and check the validity
282 NEXT_KEY(nameserver, mailbox);
283 NEXT_KEY(mailbox, str_serial);
284 NEXT_KEY(str_serial, str_refresh);
285 NEXT_KEY(str_refresh, str_retry);
286 NEXT_KEY(str_retry, str_expire);
287 NEXT_KEY(str_expire, str_minimum);
288 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
289 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
290 str_minimum == NULL) {
291 RWRAP_LOG(RWRAP_LOG_ERROR,
292 "Malformed SOA entry [%s]\n", value);
296 memcpy(rr->rrdata.soa_rec.nameserver, nameserver, strlen(nameserver)+1);
297 memcpy(rr->rrdata.soa_rec.mailbox, mailbox, strlen(mailbox)+1);
299 rr->rrdata.soa_rec.serial = atoi(str_serial);
300 rr->rrdata.soa_rec.refresh = atoi(str_refresh);
301 rr->rrdata.soa_rec.retry = atoi(str_retry);
302 rr->rrdata.soa_rec.expire = atoi(str_expire);
303 rr->rrdata.soa_rec.minimum = atoi(str_minimum);
305 memcpy(rr->key, key, strlen(key) + 1);
310 static int rwrap_create_fake_cname_rr(const char *key,
312 struct rwrap_fake_rr *rr)
314 memcpy(rr->rrdata.cname_rec , value, strlen(value) + 1);
315 memcpy(rr->key, key, strlen(key) + 1);
316 rr->type = ns_t_cname;
320 /* Prepares a fake header with a single response. Advances header_blob */
321 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
322 size_t ancount, size_t arcount)
327 if (remaining < NS_HFIXEDSZ) {
328 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
333 memset(hb, 0, NS_HFIXEDSZ);
336 h->id = res_randomid(); /* random query ID */
337 h->qr = 1; /* response flag */
338 h->rd = 1; /* recursion desired */
339 h->ra = 1; /* resursion available */
341 h->qdcount = htons(1); /* no. of questions */
342 h->ancount = htons(ancount); /* no. of answers */
343 h->arcount = htons(arcount); /* no. of add'tl records */
345 hb += NS_HFIXEDSZ; /* move past the header */
351 static ssize_t rwrap_fake_question(const char *question,
353 uint8_t **question_ptr,
356 uint8_t *qb = *question_ptr;
359 n = ns_name_compress(question, qb, remaining, NULL, NULL);
361 RWRAP_LOG(RWRAP_LOG_ERROR,
362 "Failed to compress [%s]\n", question);
369 if (remaining < 2 * sizeof(uint16_t)) {
370 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
375 NS_PUT16(ns_c_in, qb);
378 return n + 2 * sizeof(uint16_t);
381 static ssize_t rwrap_fake_rdata_common(uint16_t type,
387 uint8_t *rd = *rdata_ptr;
390 written = ns_name_compress(key, rd, remaining, NULL, NULL);
392 RWRAP_LOG(RWRAP_LOG_ERROR,
393 "Failed to compress [%s]\n", key);
397 remaining -= written;
399 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
400 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
405 NS_PUT16(ns_c_in, rd);
406 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
407 NS_PUT16(rdata_size, rd);
409 if (remaining < rdata_size) {
410 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
415 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
418 static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
422 uint8_t *a = answer_ptr;
425 if (rr == NULL || rr->type != ns_t_a) {
426 RWRAP_LOG(RWRAP_LOG_ERROR,
427 "Malformed record, no or wrong value!\n");
430 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
432 resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
438 memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
443 static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
450 if (rr == NULL || rr->type != ns_t_aaaa) {
451 RWRAP_LOG(RWRAP_LOG_ERROR,
452 "Malformed record, no or wrong value!\n");
455 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
457 resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
458 rr->key, anslen, &a);
463 memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
468 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
475 unsigned char hostname_compressed[MAXDNAME];
476 ssize_t compressed_len;
478 if (rr == NULL || rr->type != ns_t_srv) {
479 RWRAP_LOG(RWRAP_LOG_ERROR,
480 "Malformed record, no or wrong value!\n");
483 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
484 rdata_size = 3 * sizeof(uint16_t);
486 /* Prepare the data to write */
487 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
488 hostname_compressed, MAXDNAME,
490 if (compressed_len < 0) {
493 rdata_size += compressed_len;
495 resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
496 rr->key, anslen, &a);
501 NS_PUT16(rr->rrdata.srv_rec.prio, a);
502 NS_PUT16(rr->rrdata.srv_rec.weight, a);
503 NS_PUT16(rr->rrdata.srv_rec.port, a);
504 memcpy(a, hostname_compressed, compressed_len);
509 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
516 unsigned char nameser_compressed[MAXDNAME];
517 ssize_t compressed_ns_len;
518 unsigned char mailbox_compressed[MAXDNAME];
519 ssize_t compressed_mb_len;
521 if (rr == NULL || rr->type != ns_t_soa) {
522 RWRAP_LOG(RWRAP_LOG_ERROR,
523 "Malformed record, no or wrong value!\n");
526 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
527 rdata_size = 5 * sizeof(uint16_t);
529 compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
531 MAXDNAME, NULL, NULL);
532 if (compressed_ns_len < 0) {
535 rdata_size += compressed_ns_len;
537 compressed_mb_len = ns_name_compress(rr->rrdata.soa_rec.mailbox,
539 MAXDNAME, NULL, NULL);
540 if (compressed_mb_len < 0) {
543 rdata_size += compressed_mb_len;
545 resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
546 rr->key, anslen, &a);
551 memcpy(a, nameser_compressed, compressed_ns_len);
552 a += compressed_ns_len;
553 memcpy(a, mailbox_compressed, compressed_mb_len);
554 a += compressed_mb_len;
555 NS_PUT32(rr->rrdata.soa_rec.serial, a);
556 NS_PUT32(rr->rrdata.soa_rec.refresh, a);
557 NS_PUT32(rr->rrdata.soa_rec.retry, a);
558 NS_PUT32(rr->rrdata.soa_rec.expire, a);
559 NS_PUT32(rr->rrdata.soa_rec.minimum, a);
564 static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
570 unsigned char hostname_compressed[MAXDNAME];
573 if (rr == NULL || rr->type != ns_t_cname) {
574 RWRAP_LOG(RWRAP_LOG_ERROR,
575 "Malformed record, no or wrong value!\n");
578 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
580 /* Prepare the data to write */
581 rdata_size = ns_name_compress(rr->rrdata.cname_rec,
582 hostname_compressed, MAXDNAME,
584 if (rdata_size < 0) {
588 resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
589 rr->key, anslen, &a);
594 memcpy(a, hostname_compressed, rdata_size);
599 #define RESOLV_MATCH(line, name) \
600 (strncmp(line, name, sizeof(name) - 1) == 0 && \
601 (line[sizeof(name) - 1] == ' ' || \
602 line[sizeof(name) - 1] == '\t'))
604 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
605 ((type) == (ns_type) && \
606 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
607 (strcasecmp(key, query)) == 0)
610 static int rwrap_get_record(const char *hostfile, unsigned recursion,
611 const char *query, int type,
612 struct rwrap_fake_rr *rr);
614 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
615 const char *query, struct rwrap_fake_rr *rr)
619 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
620 if (rc == 0) return 0;
622 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
623 if (rc == ENOENT) rc = 0;
628 static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
629 const char *query, struct rwrap_fake_rr *rr)
633 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
634 if (rc == 0) return 0;
636 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
637 if (rc == 0) return 0;
639 rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
640 if (rc == ENOENT) rc = 0;
645 static int rwrap_get_record(const char *hostfile, unsigned recursion,
646 const char *query, int type,
647 struct rwrap_fake_rr *rr)
655 if (recursion >= RWRAP_MAX_RECURSION) {
656 RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
660 RWRAP_LOG(RWRAP_LOG_TRACE,
661 "Searching in fake hosts file %s\n", hostfile);
663 fp = fopen(hostfile, "r");
665 RWRAP_LOG(RWRAP_LOG_ERROR,
666 "Opening %s failed: %s",
667 hostfile, strerror(errno));
671 while (fgets(buf, sizeof(buf), fp) != NULL) {
678 NEXT_KEY(rec_type, key);
679 NEXT_KEY(key, value);
682 while(q[0] != '\n' && q[0] != '\0') {
687 if (key == NULL || value == NULL) {
688 RWRAP_LOG(RWRAP_LOG_WARN,
689 "Malformed line: not enough parts, use \"rec_type key data\n"
690 "For example \"A cwrap.org 10.10.10.10\"");
694 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
695 rc = rwrap_create_fake_a_rr(key, value, rr);
697 } else if (TYPE_MATCH(type, ns_t_aaaa,
698 rec_type, "AAAA", key, query)) {
699 rc = rwrap_create_fake_aaaa_rr(key, value, rr);
701 } else if (TYPE_MATCH(type, ns_t_srv,
702 rec_type, "SRV", key, query)) {
703 rc = rwrap_create_fake_srv_rr(key, value, rr);
705 rc = rwrap_srv_recurse(hostfile, recursion+1,
706 rr->rrdata.srv_rec.hostname,
710 } else if (TYPE_MATCH(type, ns_t_soa,
711 rec_type, "SOA", key, query)) {
712 rc = rwrap_create_fake_soa_rr(key, value, rr);
714 } else if (TYPE_MATCH(type, ns_t_cname,
715 rec_type, "CNAME", key, query)) {
716 rc = rwrap_create_fake_cname_rr(key, value, rr);
718 rc = rwrap_cname_recurse(hostfile, recursion+1,
725 if (rc == ENOENT && recursion == 0) {
726 RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
727 memcpy(rr->key, key, strlen(key) + 1);
734 static ssize_t rwrap_fake_empty(int type,
735 const char *question,
740 size_t remaining = anslen;
742 resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
746 remaining -= resp_data;
748 resp_data += rwrap_fake_question(question, type, &answer, remaining);
752 remaining -= resp_data;
754 resp_data += rwrap_fake_rdata_common(type, 0, question,
763 static inline bool rwrap_known_type(int type)
777 static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
782 /* Include all RRs in the stack until the sought type
783 * in the answer section. This is the case i.e. when looking
784 * up an A record but the name points to a CNAME
786 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
789 if (rwrap_known_type(rrs[i].type) &&
790 rrs[i].type == qtype) {
795 /* Return 0 records if the sought type wasn't in the stack */
796 return i < RWRAP_MAX_RECURSION ? ancount : 0;
799 static int rwrap_arcount(struct rwrap_fake_rr *rrs, int ancount)
804 /* start from index ancount */
805 for (i = ancount; i < RWRAP_MAX_RECURSION; i++) {
806 if (rwrap_known_type(rrs[i].type)) {
814 static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
822 resp_data = rwrap_fake_a(rr, answer, anslen);
825 resp_data = rwrap_fake_aaaa(rr, answer, anslen);
828 resp_data = rwrap_fake_srv(rr, answer, anslen);
831 resp_data = rwrap_fake_soa(rr, answer, anslen);
834 resp_data = rwrap_fake_cname(rr, answer, anslen);
843 static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
851 size_t remaining = anslen;
856 ancount = rwrap_ancount(rrs, type);
857 arcount = rwrap_arcount(rrs, ancount);
858 RWRAP_LOG(RWRAP_LOG_TRACE,
859 "Got %d answers and %d additional records\n", ancount, arcount);
861 resp_data = rwrap_fake_header(&answer, remaining, ancount, arcount);
865 remaining -= resp_data;
867 resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
871 remaining -= resp_data;
874 for (i = 0; i < ancount; i++) {
875 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
884 /* add authoritative NS here? */
886 /* additional records */
887 for (i = ancount; i < ancount + arcount; i++) {
888 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
900 /* Reads in a file in the following format:
903 * Malformed entried are silently skipped.
904 * Allocates answer buffer of size anslen that has to be freed after use.
906 static int rwrap_res_fake_hosts(const char *hostfile,
909 unsigned char *answer,
913 char *query_name = NULL;
914 size_t qlen = strlen(query);
915 struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
918 RWRAP_LOG(RWRAP_LOG_TRACE,
919 "Searching in fake hosts file %s\n", hostfile);
921 if (qlen > 0 && query[qlen-1] == '.') {
925 query_name = strndup(query, qlen);
926 if (query_name == NULL) {
930 rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
932 rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
935 RWRAP_LOG(RWRAP_LOG_TRACE,
936 "Found record for [%s]\n", query_name);
937 resp_size = rwrap_fake_answer(rrs, type, answer, anslen);
940 RWRAP_LOG(RWRAP_LOG_TRACE,
941 "No record for [%s]\n", query_name);
942 resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
945 RWRAP_LOG(RWRAP_LOG_ERROR,
946 "Error searching for [%s]\n", query_name);
953 RWRAP_LOG(RWRAP_LOG_ERROR,
954 "Error faking answer for [%s]\n", query_name);
957 RWRAP_LOG(RWRAP_LOG_TRACE,
958 "Successfully faked answer for [%s]\n",
967 /*********************************************************
968 * RWRAP LOADING LIBC FUNCTIONS
969 *********************************************************/
973 struct rwrap_libc_fns {
974 int (*libc_res_init)(void);
975 int (*libc___res_init)(void);
976 int (*libc_res_ninit)(struct __res_state *state);
977 int (*libc___res_ninit)(struct __res_state *state);
978 void (*libc_res_nclose)(struct __res_state *state);
979 void (*libc___res_nclose)(struct __res_state *state);
980 void (*libc_res_close)(void);
981 void (*libc___res_close)(void);
982 int (*libc_res_nquery)(struct __res_state *state,
986 unsigned char *answer,
988 int (*libc___res_nquery)(struct __res_state *state,
992 unsigned char *answer,
994 int (*libc_res_nsearch)(struct __res_state *state,
998 unsigned char *answer,
1000 int (*libc___res_nsearch)(struct __res_state *state,
1004 unsigned char *answer,
1010 void *libresolv_handle;
1017 struct rwrap_libc_fns fns;
1020 static struct rwrap rwrap;
1028 static const char *rwrap_str_lib(enum rwrap_lib lib)
1033 case RWRAP_LIBRESOLV:
1037 /* Compiler would warn us about unhandled enum value if we get here */
1042 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
1044 int flags = RTLD_LAZY;
1045 void *handle = NULL;
1048 #ifdef RTLD_DEEPBIND
1049 flags |= RTLD_DEEPBIND;
1053 case RWRAP_LIBRESOLV:
1054 #ifdef HAVE_LIBRESOLV
1055 handle = rwrap.libresolv_handle;
1056 if (handle == NULL) {
1057 for (i = 10; i >= 0; i--) {
1058 char soname[256] = {0};
1060 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
1061 handle = dlopen(soname, flags);
1062 if (handle != NULL) {
1067 rwrap.libresolv_handle = handle;
1073 handle = rwrap.libc_handle;
1075 if (handle == NULL) {
1076 handle = dlopen(LIBC_SO, flags);
1078 rwrap.libc_handle = handle;
1081 if (handle == NULL) {
1082 for (i = 10; i >= 0; i--) {
1083 char soname[256] = {0};
1085 snprintf(soname, sizeof(soname), "libc.so.%d", i);
1086 handle = dlopen(soname, flags);
1087 if (handle != NULL) {
1092 rwrap.libc_handle = handle;
1097 if (handle == NULL) {
1099 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
1101 RWRAP_LOG(RWRAP_LOG_ERROR,
1102 "Failed to dlopen library: %s\n",
1111 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
1116 handle = rwrap_load_lib_handle(lib);
1118 func = dlsym(handle, fn_name);
1120 RWRAP_LOG(RWRAP_LOG_ERROR,
1121 "Failed to find %s: %s\n",
1122 fn_name, dlerror());
1126 RWRAP_LOG(RWRAP_LOG_TRACE,
1127 "Loaded %s from %s",
1128 fn_name, rwrap_str_lib(lib));
1132 #define rwrap_load_lib_function(lib, fn_name) \
1133 if (rwrap.fns.libc_##fn_name == NULL) { \
1134 *(void **) (&rwrap.fns.libc_##fn_name) = \
1135 _rwrap_load_lib_function(lib, #fn_name); \
1141 * Functions especially from libc need to be loaded individually, you can't load
1142 * all at once or gdb will segfault at startup. The same applies to valgrind and
1143 * has probably something todo with with the linker.
1144 * So we need load each function at the point it is called the first time.
1147 static int libc_res_init(void)
1149 #if defined(HAVE_RES_INIT)
1150 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
1152 return rwrap.fns.libc_res_init();
1153 #elif defined(HAVE___RES_INIT)
1154 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
1156 return rwrap.fns.libc___res_init();
1161 static int libc_res_ninit(struct __res_state *state)
1163 #if defined(HAVE_RES_NINIT)
1165 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1166 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_ninit);
1167 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1168 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
1169 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1171 return rwrap.fns.libc_res_ninit(state);
1172 #elif defined(HAVE___RES_NINIT)
1173 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
1175 return rwrap.fns.libc___res_ninit(state);
1177 #error "No res_ninit function"
1181 static void libc_res_nclose(struct __res_state *state)
1183 #if defined(HAVE_RES_NCLOSE)
1185 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1186 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nclose);
1187 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1188 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
1189 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1191 rwrap.fns.libc_res_nclose(state);
1192 #elif defined(HAVE___RES_NCLOSE)
1193 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
1195 rwrap.fns.libc___res_nclose(state);
1197 #error "No res_nclose function"
1201 static int libc_res_nquery(struct __res_state *state,
1205 unsigned char *answer,
1208 #if defined(HAVE_RES_NQUERY)
1209 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
1211 return rwrap.fns.libc_res_nquery(state,
1217 #elif defined(HAVE___RES_NQUERY)
1218 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
1220 return rwrap.fns.libc___res_nquery(state,
1227 #error "No res_nquery function"
1231 static int libc_res_nsearch(struct __res_state *state,
1235 unsigned char *answer,
1238 #if defined(HAVE_RES_NSEARCH)
1239 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
1241 return rwrap.fns.libc_res_nsearch(state,
1247 #elif defined(HAVE___RES_NSEARCH)
1248 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
1250 return rwrap.fns.libc___res_nsearch(state,
1257 #error "No res_nsearch function"
1261 /****************************************************************************
1263 ***************************************************************************/
1265 static int rwrap_parse_resolv_conf(struct __res_state *state,
1266 const char *resolv_conf)
1272 fp = fopen(resolv_conf, "r");
1274 RWRAP_LOG(RWRAP_LOG_ERROR,
1275 "Opening %s failed: %s",
1276 resolv_conf, strerror(errno));
1280 while(fgets(buf, sizeof(buf), fp) != NULL) {
1283 /* Ignore comments */
1284 if (buf[0] == '#' || buf[0] == ';') {
1288 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
1293 p = buf + strlen("nameserver");
1295 /* Skip spaces and tabs */
1296 while(isblank((int)p[0])) {
1301 while(q[0] != '\n' && q[0] != '\0') {
1306 ok = inet_pton(AF_INET, p, &a);
1308 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
1309 .sin_family = AF_INET,
1311 .sin_port = htons(53),
1318 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1321 ok = inet_pton(AF_INET6, p, &a6);
1323 struct sockaddr_in6 *sa6;
1325 sa6 = malloc(sizeof(*sa6));
1331 sa6->sin6_family = AF_INET6;
1332 sa6->sin6_port = htons(53);
1333 sa6->sin6_flowinfo = 0;
1334 sa6->sin6_addr = a6;
1336 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
1337 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
1338 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1340 state->_u._ext.nscount++;
1343 RWRAP_LOG(RWRAP_LOG_ERROR,
1344 "Malformed DNS server");
1347 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1349 * BSD uses an opaque structure to store the
1350 * IPv6 addresses. So we can not simply store
1351 * these addresses the same way as above.
1353 RWRAP_LOG(RWRAP_LOG_WARN,
1354 "resolve_wrapper does not support "
1355 "IPv6 on this platform");
1360 } /* TODO: match other keywords */
1364 RWRAP_LOG(RWRAP_LOG_ERROR,
1365 "Reading from %s failed",
1375 /****************************************************************************
1377 ***************************************************************************/
1379 static int rwrap_res_ninit(struct __res_state *state)
1383 rc = libc_res_ninit(state);
1385 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1387 if (resolv_conf != NULL) {
1390 (void)i; /* maybe unused */
1392 /* Delete name servers */
1394 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1396 state->_u._ext.nscount = 0;
1397 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1398 for (i = 0; i < state->_u._ext.nscount; i++) {
1399 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1403 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1410 #if defined(HAVE_RES_NINIT)
1411 int res_ninit(struct __res_state *state)
1412 #elif defined(HAVE___RES_NINIT)
1413 int __res_ninit(struct __res_state *state)
1416 return rwrap_res_ninit(state);
1419 /****************************************************************************
1421 ***************************************************************************/
1423 static struct __res_state rwrap_res_state;
1425 static int rwrap_res_init(void)
1429 rc = rwrap_res_ninit(&rwrap_res_state);
1434 #if defined(HAVE_RES_INIT)
1436 #elif defined(HAVE___RES_INIT)
1437 int __res_init(void)
1440 return rwrap_res_init();
1443 /****************************************************************************
1445 ***************************************************************************/
1447 static void rwrap_res_nclose(struct __res_state *state)
1449 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1453 libc_res_nclose(state);
1455 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1456 if (state != NULL) {
1457 for (i = 0; i < state->_u._ext.nscount; i++) {
1458 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1464 #if defined(HAVE_RES_NCLOSE)
1465 void res_nclose(struct __res_state *state)
1466 #elif defined(HAVE___RES_NCLOSE)
1467 void __res_nclose(struct __res_state *state)
1470 rwrap_res_nclose(state);
1473 /****************************************************************************
1475 ***************************************************************************/
1477 static void rwrap_res_close(void)
1479 rwrap_res_nclose(&rwrap_res_state);
1482 #if defined(HAVE_RES_CLOSE)
1483 void res_close(void)
1484 #elif defined(HAVE___RES_CLOSE)
1485 void __res_close(void)
1491 /****************************************************************************
1493 ***************************************************************************/
1495 static int rwrap_res_nquery(struct __res_state *state,
1499 unsigned char *answer,
1503 const char *fake_hosts;
1508 RWRAP_LOG(RWRAP_LOG_TRACE,
1509 "Resolve the domain name [%s] - class=%d, type=%d",
1510 dname, class, type);
1512 for (i = 0; i < state->nscount; i++) {
1513 char ip[INET6_ADDRSTRLEN];
1515 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1516 RWRAP_LOG(RWRAP_LOG_TRACE,
1522 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1523 if (fake_hosts != NULL) {
1524 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1526 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1530 RWRAP_LOG(RWRAP_LOG_TRACE,
1531 "The returned response length is: %d",
1537 #if defined(HAVE_RES_NQUERY)
1538 int res_nquery(struct __res_state *state,
1542 unsigned char *answer,
1544 #elif defined(HAVE___RES_NQUERY)
1545 int __res_nquery(struct __res_state *state,
1549 unsigned char *answer,
1553 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1556 /****************************************************************************
1558 ***************************************************************************/
1560 static int rwrap_res_query(const char *dname,
1563 unsigned char *answer,
1568 rc = rwrap_res_ninit(&rwrap_res_state);
1573 rc = rwrap_res_nquery(&rwrap_res_state,
1583 #if defined(HAVE_RES_QUERY)
1584 int res_query(const char *dname,
1587 unsigned char *answer,
1589 #elif defined(HAVE___RES_QUERY)
1590 int __res_query(const char *dname,
1593 unsigned char *answer,
1597 return rwrap_res_query(dname, class, type, answer, anslen);
1600 /****************************************************************************
1602 ***************************************************************************/
1604 static int rwrap_res_nsearch(struct __res_state *state,
1608 unsigned char *answer,
1612 const char *fake_hosts;
1617 RWRAP_LOG(RWRAP_LOG_TRACE,
1618 "Resolve the domain name [%s] - class=%d, type=%d",
1619 dname, class, type);
1621 for (i = 0; i < state->nscount; i++) {
1622 char ip[INET6_ADDRSTRLEN];
1624 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1625 RWRAP_LOG(RWRAP_LOG_TRACE,
1631 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1632 if (fake_hosts != NULL) {
1633 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1635 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1638 RWRAP_LOG(RWRAP_LOG_TRACE,
1639 "The returned response length is: %d",
1645 #if defined(HAVE_RES_NSEARCH)
1646 int res_nsearch(struct __res_state *state,
1650 unsigned char *answer,
1652 #elif defined(HAVE___RES_NSEARCH)
1653 int __res_nsearch(struct __res_state *state,
1657 unsigned char *answer,
1661 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1664 /****************************************************************************
1666 ***************************************************************************/
1668 static int rwrap_res_search(const char *dname,
1671 unsigned char *answer,
1676 rc = rwrap_res_ninit(&rwrap_res_state);
1681 rc = rwrap_res_nsearch(&rwrap_res_state,
1691 #if defined(HAVE_RES_SEARCH)
1692 int res_search(const char *dname,
1695 unsigned char *answer,
1697 #elif defined(HAVE___RES_SEARCH)
1698 int __res_search(const char *dname,
1701 unsigned char *answer,
1705 return rwrap_res_search(dname, class, type, answer, anslen);