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");
431 resp_size
= rwrap_fake_rdata_common(ns_t_a
, sizeof(struct in_addr
), rr
->key
,
437 memcpy(a
, &rr
->rrdata
.a_rec
, sizeof(struct in_addr
));
442 static ssize_t
rwrap_fake_aaaa(struct rwrap_fake_rr
*rr
,
449 if (rr
== NULL
|| rr
->type
!= ns_t_aaaa
) {
450 RWRAP_LOG(RWRAP_LOG_ERROR
,
451 "Malformed record, no or wrong value!\n");
455 resp_size
= rwrap_fake_rdata_common(ns_t_aaaa
, sizeof(struct in6_addr
),
456 rr
->key
, anslen
, &a
);
461 memcpy(a
, &rr
->rrdata
.aaaa_rec
, sizeof(struct in6_addr
));
466 static ssize_t
rwrap_fake_srv(struct rwrap_fake_rr
*rr
,
473 unsigned char hostname_compressed
[MAXDNAME
];
474 ssize_t compressed_len
;
476 if (rr
== NULL
|| rr
->type
!= ns_t_srv
) {
477 RWRAP_LOG(RWRAP_LOG_ERROR
,
478 "Malformed record, no or wrong value!\n");
481 rdata_size
= 3 * sizeof(uint16_t);
483 /* Prepare the data to write */
484 compressed_len
= ns_name_compress(rr
->rrdata
.srv_rec
.hostname
,
485 hostname_compressed
, MAXDNAME
,
487 if (compressed_len
< 0) {
490 rdata_size
+= compressed_len
;
492 resp_size
= rwrap_fake_rdata_common(ns_t_srv
, rdata_size
,
493 rr
->key
, anslen
, &a
);
498 NS_PUT16(rr
->rrdata
.srv_rec
.prio
, a
);
499 NS_PUT16(rr
->rrdata
.srv_rec
.weight
, a
);
500 NS_PUT16(rr
->rrdata
.srv_rec
.port
, a
);
501 memcpy(a
, hostname_compressed
, compressed_len
);
506 static ssize_t
rwrap_fake_soa(struct rwrap_fake_rr
*rr
,
513 unsigned char nameser_compressed
[MAXDNAME
];
514 ssize_t compressed_ns_len
;
515 unsigned char mailbox_compressed
[MAXDNAME
];
516 ssize_t compressed_mb_len
;
518 if (rr
== NULL
|| rr
->type
!= ns_t_soa
) {
519 RWRAP_LOG(RWRAP_LOG_ERROR
,
520 "Malformed record, no or wrong value!\n");
523 rdata_size
= 5 * sizeof(uint16_t);
525 compressed_ns_len
= ns_name_compress(rr
->rrdata
.soa_rec
.nameserver
,
527 MAXDNAME
, NULL
, NULL
);
528 if (compressed_ns_len
< 0) {
531 rdata_size
+= compressed_ns_len
;
533 compressed_mb_len
= ns_name_compress(rr
->rrdata
.soa_rec
.mailbox
,
535 MAXDNAME
, NULL
, NULL
);
536 if (compressed_mb_len
< 0) {
539 rdata_size
+= compressed_mb_len
;
541 resp_size
= rwrap_fake_rdata_common(ns_t_soa
, rdata_size
,
542 rr
->key
, anslen
, &a
);
547 memcpy(a
, nameser_compressed
, compressed_ns_len
);
548 a
+= compressed_ns_len
;
549 memcpy(a
, mailbox_compressed
, compressed_mb_len
);
550 a
+= compressed_mb_len
;
551 NS_PUT32(rr
->rrdata
.soa_rec
.serial
, a
);
552 NS_PUT32(rr
->rrdata
.soa_rec
.refresh
, a
);
553 NS_PUT32(rr
->rrdata
.soa_rec
.retry
, a
);
554 NS_PUT32(rr
->rrdata
.soa_rec
.expire
, a
);
555 NS_PUT32(rr
->rrdata
.soa_rec
.minimum
, a
);
560 static ssize_t
rwrap_fake_cname(struct rwrap_fake_rr
*rr
,
566 unsigned char hostname_compressed
[MAXDNAME
];
569 if (rr
== NULL
|| rr
->type
!= ns_t_cname
) {
570 RWRAP_LOG(RWRAP_LOG_ERROR
,
571 "Malformed record, no or wrong value!\n");
575 /* Prepare the data to write */
576 rdata_size
= ns_name_compress(rr
->rrdata
.cname_rec
,
577 hostname_compressed
, MAXDNAME
,
579 if (rdata_size
< 0) {
583 resp_size
= rwrap_fake_rdata_common(ns_t_cname
, rdata_size
,
584 rr
->key
, anslen
, &a
);
589 memcpy(a
, hostname_compressed
, rdata_size
);
594 #define RESOLV_MATCH(line, name) \
595 (strncmp(line, name, sizeof(name) - 1) == 0 && \
596 (line[sizeof(name) - 1] == ' ' || \
597 line[sizeof(name) - 1] == '\t'))
599 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
600 ((type) == (ns_type) && \
601 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
602 (strcasecmp(key, query)) == 0)
605 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
606 const char *query
, int type
,
607 struct rwrap_fake_rr
*rr
);
609 static int rwrap_srv_recurse(const char *hostfile
, unsigned recursion
,
610 const char *query
, struct rwrap_fake_rr
*rr
)
614 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
615 if (rc
== 0) return 0;
617 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
618 if (rc
== ENOENT
) rc
= 0;
623 static int rwrap_cname_recurse(const char *hostfile
, unsigned recursion
,
624 const char *query
, struct rwrap_fake_rr
*rr
)
628 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
629 if (rc
== 0) return 0;
631 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
632 if (rc
== 0) return 0;
634 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_cname
, rr
);
635 if (rc
== ENOENT
) rc
= 0;
640 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
641 const char *query
, int type
,
642 struct rwrap_fake_rr
*rr
)
650 if (recursion
>= RWRAP_MAX_RECURSION
) {
651 RWRAP_LOG(RWRAP_LOG_ERROR
, "Recursed too deep!\n");
655 RWRAP_LOG(RWRAP_LOG_TRACE
,
656 "Searching in fake hosts file %s\n", hostfile
);
658 fp
= fopen(hostfile
, "r");
660 RWRAP_LOG(RWRAP_LOG_ERROR
,
661 "Opening %s failed: %s",
662 hostfile
, strerror(errno
));
666 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
673 NEXT_KEY(rec_type
, key
);
674 NEXT_KEY(key
, value
);
677 while(q
[0] != '\n' && q
[0] != '\0') {
682 if (key
== NULL
|| value
== NULL
) {
683 RWRAP_LOG(RWRAP_LOG_WARN
,
684 "Malformed line: not enough parts, use \"rec_type key data\n"
685 "For example \"A cwrap.org 10.10.10.10\"");
689 if (TYPE_MATCH(type
, ns_t_a
, rec_type
, "A", key
, query
)) {
690 rc
= rwrap_create_fake_a_rr(key
, value
, rr
);
692 } else if (TYPE_MATCH(type
, ns_t_aaaa
,
693 rec_type
, "AAAA", key
, query
)) {
694 rc
= rwrap_create_fake_aaaa_rr(key
, value
, rr
);
696 } else if (TYPE_MATCH(type
, ns_t_srv
,
697 rec_type
, "SRV", key
, query
)) {
698 rc
= rwrap_create_fake_srv_rr(key
, value
, rr
);
700 rc
= rwrap_srv_recurse(hostfile
, recursion
+1,
701 rr
->rrdata
.srv_rec
.hostname
,
705 } else if (TYPE_MATCH(type
, ns_t_soa
,
706 rec_type
, "SOA", key
, query
)) {
707 rc
= rwrap_create_fake_soa_rr(key
, value
, rr
);
709 } else if (TYPE_MATCH(type
, ns_t_cname
,
710 rec_type
, "CNAME", key
, query
)) {
711 rc
= rwrap_create_fake_cname_rr(key
, value
, rr
);
713 rc
= rwrap_cname_recurse(hostfile
, recursion
+1,
720 if (rc
== ENOENT
&& recursion
== 0) {
721 RWRAP_LOG(RWRAP_LOG_TRACE
, "Record for [%s] not found\n", query
);
722 memcpy(rr
->key
, key
, strlen(key
) + 1);
729 static ssize_t
rwrap_fake_empty(int type
,
730 const char *question
,
735 size_t remaining
= anslen
;
737 resp_data
= rwrap_fake_header(&answer
, remaining
, 0, 0);
741 remaining
-= resp_data
;
743 resp_data
+= rwrap_fake_question(question
, type
, &answer
, remaining
);
747 remaining
-= resp_data
;
749 resp_data
+= rwrap_fake_rdata_common(type
, 0, question
,
758 static ssize_t
rwrap_fake_answer(struct rwrap_fake_rr
*rrs
,
764 size_t remaining
= anslen
;
766 resp_data
= rwrap_fake_header(&answer
, remaining
, 1, 0);
770 remaining
-= resp_data
;
772 resp_data
+= rwrap_fake_question(rrs
->key
, rrs
->type
, &answer
, remaining
);
776 remaining
-= resp_data
;
780 resp_data
+= rwrap_fake_a(rrs
, answer
, anslen
);
783 resp_data
+= rwrap_fake_aaaa(rrs
, answer
, anslen
);
786 resp_data
+= rwrap_fake_srv(rrs
, answer
, anslen
);
789 resp_data
+= rwrap_fake_soa(rrs
, answer
, anslen
);
792 resp_data
+= rwrap_fake_cname(rrs
, answer
, anslen
);
801 /* Reads in a file in the following format:
804 * Malformed entried are silently skipped.
805 * Allocates answer buffer of size anslen that has to be freed after use.
807 static int rwrap_res_fake_hosts(const char *hostfile
,
810 unsigned char *answer
,
814 char *query_name
= NULL
;
815 size_t qlen
= strlen(query
);
816 struct rwrap_fake_rr rrs
[RWRAP_MAX_RECURSION
];
819 RWRAP_LOG(RWRAP_LOG_TRACE
,
820 "Searching in fake hosts file %s\n", hostfile
);
822 if (qlen
> 0 && query
[qlen
-1] == '.') {
826 query_name
= strndup(query
, qlen
);
827 if (query_name
== NULL
) {
831 rwrap_fake_rr_init(rrs
, RWRAP_MAX_RECURSION
);
833 rc
= rwrap_get_record(hostfile
, 0, query_name
, type
, rrs
);
836 RWRAP_LOG(RWRAP_LOG_TRACE
,
837 "Found record for [%s]\n", query_name
);
838 resp_size
= rwrap_fake_answer(rrs
, answer
, anslen
);
841 RWRAP_LOG(RWRAP_LOG_TRACE
,
842 "No record for [%s]\n", query_name
);
843 resp_size
= rwrap_fake_empty(type
, rrs
->key
, answer
, anslen
);
846 RWRAP_LOG(RWRAP_LOG_ERROR
,
847 "Error searching for [%s]\n", query_name
);
854 RWRAP_LOG(RWRAP_LOG_ERROR
,
855 "Error faking answer for [%s]\n", query_name
);
858 RWRAP_LOG(RWRAP_LOG_TRACE
,
859 "Successfully faked answer for [%s]\n",
868 /*********************************************************
869 * RWRAP LOADING LIBC FUNCTIONS
870 *********************************************************/
874 struct rwrap_libc_fns
{
875 int (*libc_res_init
)(void);
876 int (*libc___res_init
)(void);
877 int (*libc_res_ninit
)(struct __res_state
*state
);
878 int (*libc___res_ninit
)(struct __res_state
*state
);
879 void (*libc_res_nclose
)(struct __res_state
*state
);
880 void (*libc___res_nclose
)(struct __res_state
*state
);
881 void (*libc_res_close
)(void);
882 void (*libc___res_close
)(void);
883 int (*libc_res_nquery
)(struct __res_state
*state
,
887 unsigned char *answer
,
889 int (*libc___res_nquery
)(struct __res_state
*state
,
893 unsigned char *answer
,
895 int (*libc_res_nsearch
)(struct __res_state
*state
,
899 unsigned char *answer
,
901 int (*libc___res_nsearch
)(struct __res_state
*state
,
905 unsigned char *answer
,
911 void *libresolv_handle
;
918 struct rwrap_libc_fns fns
;
921 static struct rwrap rwrap
;
929 static const char *rwrap_str_lib(enum rwrap_lib lib
)
934 case RWRAP_LIBRESOLV
:
938 /* Compiler would warn us about unhandled enum value if we get here */
943 static void *rwrap_load_lib_handle(enum rwrap_lib lib
)
945 int flags
= RTLD_LAZY
;
950 flags
|= RTLD_DEEPBIND
;
954 case RWRAP_LIBRESOLV
:
955 #ifdef HAVE_LIBRESOLV
956 handle
= rwrap
.libresolv_handle
;
957 if (handle
== NULL
) {
958 for (i
= 10; i
>= 0; i
--) {
959 char soname
[256] = {0};
961 snprintf(soname
, sizeof(soname
), "libresolv.so.%d", i
);
962 handle
= dlopen(soname
, flags
);
963 if (handle
!= NULL
) {
968 rwrap
.libresolv_handle
= handle
;
974 handle
= rwrap
.libc_handle
;
976 if (handle
== NULL
) {
977 handle
= dlopen(LIBC_SO
, flags
);
979 rwrap
.libc_handle
= handle
;
982 if (handle
== NULL
) {
983 for (i
= 10; i
>= 0; i
--) {
984 char soname
[256] = {0};
986 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
987 handle
= dlopen(soname
, flags
);
988 if (handle
!= NULL
) {
993 rwrap
.libc_handle
= handle
;
998 if (handle
== NULL
) {
1000 handle
= rwrap
.libc_handle
= rwrap
.libresolv_handle
= RTLD_NEXT
;
1002 RWRAP_LOG(RWRAP_LOG_ERROR
,
1003 "Failed to dlopen library: %s\n",
1012 static void *_rwrap_load_lib_function(enum rwrap_lib lib
, const char *fn_name
)
1017 handle
= rwrap_load_lib_handle(lib
);
1019 func
= dlsym(handle
, fn_name
);
1021 RWRAP_LOG(RWRAP_LOG_ERROR
,
1022 "Failed to find %s: %s\n",
1023 fn_name
, dlerror());
1027 RWRAP_LOG(RWRAP_LOG_TRACE
,
1028 "Loaded %s from %s",
1029 fn_name
, rwrap_str_lib(lib
));
1033 #define rwrap_load_lib_function(lib, fn_name) \
1034 if (rwrap.fns.libc_##fn_name == NULL) { \
1035 *(void **) (&rwrap.fns.libc_##fn_name) = \
1036 _rwrap_load_lib_function(lib, #fn_name); \
1042 * Functions especially from libc need to be loaded individually, you can't load
1043 * all at once or gdb will segfault at startup. The same applies to valgrind and
1044 * has probably something todo with with the linker.
1045 * So we need load each function at the point it is called the first time.
1048 static int libc_res_init(void)
1050 #if defined(HAVE_RES_INIT)
1051 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_init
);
1053 return rwrap
.fns
.libc_res_init();
1054 #elif defined(HAVE___RES_INIT)
1055 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_init
);
1057 return rwrap
.fns
.libc___res_init();
1062 static int libc_res_ninit(struct __res_state
*state
)
1064 #if defined(HAVE_RES_NINIT)
1066 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1067 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_ninit
);
1068 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1069 rwrap_load_lib_function(RWRAP_LIBC
, res_ninit
);
1070 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1072 return rwrap
.fns
.libc_res_ninit(state
);
1073 #elif defined(HAVE___RES_NINIT)
1074 rwrap_load_lib_function(RWRAP_LIBC
, __res_ninit
);
1076 return rwrap
.fns
.libc___res_ninit(state
);
1078 #error "No res_ninit function"
1082 static void libc_res_nclose(struct __res_state
*state
)
1084 #if defined(HAVE_RES_NCLOSE)
1086 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1087 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nclose
);
1088 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1089 rwrap_load_lib_function(RWRAP_LIBC
, res_nclose
);
1090 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1092 rwrap
.fns
.libc_res_nclose(state
);
1093 #elif defined(HAVE___RES_NCLOSE)
1094 rwrap_load_lib_function(RWRAP_LIBC
, __res_nclose
);
1096 rwrap
.fns
.libc___res_nclose(state
);
1098 #error "No res_nclose function"
1102 static int libc_res_nquery(struct __res_state
*state
,
1106 unsigned char *answer
,
1109 #if defined(HAVE_RES_NQUERY)
1110 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nquery
);
1112 return rwrap
.fns
.libc_res_nquery(state
,
1118 #elif defined(HAVE___RES_NQUERY)
1119 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nquery
);
1121 return rwrap
.fns
.libc___res_nquery(state
,
1128 #error "No res_nquery function"
1132 static int libc_res_nsearch(struct __res_state
*state
,
1136 unsigned char *answer
,
1139 #if defined(HAVE_RES_NSEARCH)
1140 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nsearch
);
1142 return rwrap
.fns
.libc_res_nsearch(state
,
1148 #elif defined(HAVE___RES_NSEARCH)
1149 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nsearch
);
1151 return rwrap
.fns
.libc___res_nsearch(state
,
1158 #error "No res_nsearch function"
1162 /****************************************************************************
1164 ***************************************************************************/
1166 static int rwrap_parse_resolv_conf(struct __res_state
*state
,
1167 const char *resolv_conf
)
1173 fp
= fopen(resolv_conf
, "r");
1175 RWRAP_LOG(RWRAP_LOG_ERROR
,
1176 "Opening %s failed: %s",
1177 resolv_conf
, strerror(errno
));
1181 while(fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1184 /* Ignore comments */
1185 if (buf
[0] == '#' || buf
[0] == ';') {
1189 if (RESOLV_MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
1194 p
= buf
+ strlen("nameserver");
1196 /* Skip spaces and tabs */
1197 while(isblank((int)p
[0])) {
1202 while(q
[0] != '\n' && q
[0] != '\0') {
1207 ok
= inet_pton(AF_INET
, p
, &a
);
1209 state
->nsaddr_list
[state
->nscount
] = (struct sockaddr_in
) {
1210 .sin_family
= AF_INET
,
1212 .sin_port
= htons(53),
1219 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1222 ok
= inet_pton(AF_INET6
, p
, &a6
);
1224 struct sockaddr_in6
*sa6
;
1226 sa6
= malloc(sizeof(*sa6
));
1232 sa6
->sin6_family
= AF_INET6
;
1233 sa6
->sin6_port
= htons(53);
1234 sa6
->sin6_flowinfo
= 0;
1235 sa6
->sin6_addr
= a6
;
1237 state
->_u
._ext
.nsaddrs
[state
->_u
._ext
.nscount
] = sa6
;
1238 state
->_u
._ext
.nssocks
[state
->_u
._ext
.nscount
] = -1;
1239 state
->_u
._ext
.nsmap
[state
->_u
._ext
.nscount
] = MAXNS
+ 1;
1241 state
->_u
._ext
.nscount
++;
1244 RWRAP_LOG(RWRAP_LOG_ERROR
,
1245 "Malformed DNS server");
1248 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1250 * BSD uses an opaque structure to store the
1251 * IPv6 addresses. So we can not simply store
1252 * these addresses the same way as above.
1254 RWRAP_LOG(RWRAP_LOG_WARN
,
1255 "resolve_wrapper does not support "
1256 "IPv6 on this platform");
1261 } /* TODO: match other keywords */
1265 RWRAP_LOG(RWRAP_LOG_ERROR
,
1266 "Reading from %s failed",
1276 /****************************************************************************
1278 ***************************************************************************/
1280 static int rwrap_res_ninit(struct __res_state
*state
)
1284 rc
= libc_res_ninit(state
);
1286 const char *resolv_conf
= getenv("RESOLV_WRAPPER_CONF");
1288 if (resolv_conf
!= NULL
) {
1291 (void)i
; /* maybe unused */
1293 /* Delete name servers */
1295 memset(state
->nsaddr_list
, 0, sizeof(state
->nsaddr_list
));
1297 state
->_u
._ext
.nscount
= 0;
1298 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1299 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1300 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1304 rc
= rwrap_parse_resolv_conf(state
, resolv_conf
);
1311 #if defined(HAVE_RES_NINIT)
1312 int res_ninit(struct __res_state
*state
)
1313 #elif defined(HAVE___RES_NINIT)
1314 int __res_ninit(struct __res_state
*state
)
1317 return rwrap_res_ninit(state
);
1320 /****************************************************************************
1322 ***************************************************************************/
1324 static struct __res_state rwrap_res_state
;
1326 static int rwrap_res_init(void)
1330 rc
= rwrap_res_ninit(&rwrap_res_state
);
1335 #if defined(HAVE_RES_INIT)
1337 #elif defined(HAVE___RES_INIT)
1338 int __res_init(void)
1341 return rwrap_res_init();
1344 /****************************************************************************
1346 ***************************************************************************/
1348 static void rwrap_res_nclose(struct __res_state
*state
)
1350 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1354 libc_res_nclose(state
);
1356 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1357 if (state
!= NULL
) {
1358 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1359 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1365 #if defined(HAVE_RES_NCLOSE)
1366 void res_nclose(struct __res_state
*state
)
1367 #elif defined(HAVE___RES_NCLOSE)
1368 void __res_nclose(struct __res_state
*state
)
1371 rwrap_res_nclose(state
);
1374 /****************************************************************************
1376 ***************************************************************************/
1378 static void rwrap_res_close(void)
1380 rwrap_res_nclose(&rwrap_res_state
);
1383 #if defined(HAVE_RES_CLOSE)
1384 void res_close(void)
1385 #elif defined(HAVE___RES_CLOSE)
1386 void __res_close(void)
1392 /****************************************************************************
1394 ***************************************************************************/
1396 static int rwrap_res_nquery(struct __res_state
*state
,
1400 unsigned char *answer
,
1404 const char *fake_hosts
;
1409 RWRAP_LOG(RWRAP_LOG_TRACE
,
1410 "Resolve the domain name [%s] - class=%d, type=%d",
1411 dname
, class, type
);
1413 for (i
= 0; i
< state
->nscount
; i
++) {
1414 char ip
[INET6_ADDRSTRLEN
];
1416 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1417 RWRAP_LOG(RWRAP_LOG_TRACE
,
1423 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1424 if (fake_hosts
!= NULL
) {
1425 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1427 rc
= libc_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1431 RWRAP_LOG(RWRAP_LOG_TRACE
,
1432 "The returned response length is: %d",
1438 #if defined(HAVE_RES_NQUERY)
1439 int res_nquery(struct __res_state
*state
,
1443 unsigned char *answer
,
1445 #elif defined(HAVE___RES_NQUERY)
1446 int __res_nquery(struct __res_state
*state
,
1450 unsigned char *answer
,
1454 return rwrap_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1457 /****************************************************************************
1459 ***************************************************************************/
1461 static int rwrap_res_query(const char *dname
,
1464 unsigned char *answer
,
1469 rc
= rwrap_res_ninit(&rwrap_res_state
);
1474 rc
= rwrap_res_nquery(&rwrap_res_state
,
1484 #if defined(HAVE_RES_QUERY)
1485 int res_query(const char *dname
,
1488 unsigned char *answer
,
1490 #elif defined(HAVE___RES_QUERY)
1491 int __res_query(const char *dname
,
1494 unsigned char *answer
,
1498 return rwrap_res_query(dname
, class, type
, answer
, anslen
);
1501 /****************************************************************************
1503 ***************************************************************************/
1505 static int rwrap_res_nsearch(struct __res_state
*state
,
1509 unsigned char *answer
,
1513 const char *fake_hosts
;
1518 RWRAP_LOG(RWRAP_LOG_TRACE
,
1519 "Resolve the domain name [%s] - class=%d, type=%d",
1520 dname
, class, type
);
1522 for (i
= 0; i
< state
->nscount
; i
++) {
1523 char ip
[INET6_ADDRSTRLEN
];
1525 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1526 RWRAP_LOG(RWRAP_LOG_TRACE
,
1532 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1533 if (fake_hosts
!= NULL
) {
1534 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1536 rc
= libc_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1539 RWRAP_LOG(RWRAP_LOG_TRACE
,
1540 "The returned response length is: %d",
1546 #if defined(HAVE_RES_NSEARCH)
1547 int res_nsearch(struct __res_state
*state
,
1551 unsigned char *answer
,
1553 #elif defined(HAVE___RES_NSEARCH)
1554 int __res_nsearch(struct __res_state
*state
,
1558 unsigned char *answer
,
1562 return rwrap_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1565 /****************************************************************************
1567 ***************************************************************************/
1569 static int rwrap_res_search(const char *dname
,
1572 unsigned char *answer
,
1577 rc
= rwrap_res_ninit(&rwrap_res_state
);
1582 rc
= rwrap_res_nsearch(&rwrap_res_state
,
1592 #if defined(HAVE_RES_SEARCH)
1593 int res_search(const char *dname
,
1596 unsigned char *answer
,
1598 #elif defined(HAVE___RES_SEARCH)
1599 int __res_search(const char *dname
,
1602 unsigned char *answer
,
1606 return rwrap_res_search(dname
, class, type
, answer
, anslen
);