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 */
68 #ifndef HAVE_NS_NAME_COMPRESS
69 #define ns_name_compress dn_comp
80 # define RWRAP_LOG(...)
83 static void rwrap_log(enum rwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
84 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
86 static void rwrap_log(enum rwrap_dbglvl_e dbglvl
,
88 const char *format
, ...)
96 d
= getenv("RESOLV_WRAPPER_DEBUGLEVEL");
101 va_start(va
, format
);
102 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
107 case RWRAP_LOG_ERROR
:
109 "RWRAP_ERROR(%d) - %s: %s\n",
114 "RWRAP_WARN(%d) - %s: %s\n",
117 case RWRAP_LOG_DEBUG
:
119 "RWRAP_DEBUG(%d) - %s: %s\n",
122 case RWRAP_LOG_TRACE
:
124 "RWRAP_TRACE(%d) - %s: %s\n",
130 #endif /* NDEBUG RWRAP_LOG */
133 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
136 #define NEXT_KEY(buf, key) do { \
137 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
138 if ((key) != NULL) { \
142 while ((key) != NULL \
143 && (isblank((int)(key)[0]))) { \
148 #define RWRAP_MAX_RECURSION 5
150 /* Priority and weight can be omitted from the hosts file, but need to be part
153 #define DFL_SRV_PRIO 1
154 #define DFL_SRV_WEIGHT 100
156 struct rwrap_srv_rrdata
{
160 char hostname
[MAXDNAME
];
163 struct rwrap_soa_rrdata
{
169 char nameserver
[MAXDNAME
];
170 char mailbox
[MAXDNAME
];
173 struct rwrap_fake_rr
{
175 struct in_addr a_rec
;
176 struct in6_addr aaaa_rec
;
177 struct rwrap_srv_rrdata srv_rec
;
178 struct rwrap_soa_rrdata soa_rec
;
179 char cname_rec
[MAXDNAME
];
183 int type
; /* ns_t_* */
186 static void rwrap_fake_rr_init(struct rwrap_fake_rr
*rr
, size_t len
)
190 for (i
= 0; i
< len
; i
++) {
191 rr
[i
].type
= ns_t_invalid
;
195 static int rwrap_create_fake_a_rr(const char *key
,
197 struct rwrap_fake_rr
*rr
)
201 ok
= inet_pton(AF_INET
, value
, &rr
->rrdata
.a_rec
);
203 RWRAP_LOG(RWRAP_LOG_ERROR
,
204 "Failed to convert [%s] to binary\n", value
);
208 memcpy(rr
->key
, key
, strlen(key
) + 1);
213 static int rwrap_create_fake_aaaa_rr(const char *key
,
215 struct rwrap_fake_rr
*rr
)
219 ok
= inet_pton(AF_INET6
, value
, &rr
->rrdata
.aaaa_rec
);
221 RWRAP_LOG(RWRAP_LOG_ERROR
,
222 "Failed to convert [%s] to binary\n", value
);
226 memcpy(rr
->key
, key
, strlen(key
) + 1);
227 rr
->type
= ns_t_aaaa
;
231 static int rwrap_create_fake_srv_rr(const char *key
,
233 struct rwrap_fake_rr
*rr
)
238 const char *hostname
;
240 /* parse the value into priority, weight, port and hostname
241 * and check the validity */
243 NEXT_KEY(hostname
, str_port
);
244 NEXT_KEY(str_port
, str_prio
);
245 NEXT_KEY(str_prio
, str_weight
);
246 if (str_port
== NULL
|| hostname
== NULL
) {
247 RWRAP_LOG(RWRAP_LOG_ERROR
,
248 "Malformed SRV entry [%s]\n", value
);
253 rr
->rrdata
.srv_rec
.prio
= atoi(str_prio
);
255 rr
->rrdata
.srv_rec
.prio
= DFL_SRV_PRIO
;
258 rr
->rrdata
.srv_rec
.weight
= atoi(str_weight
);
260 rr
->rrdata
.srv_rec
.weight
= DFL_SRV_WEIGHT
;
262 rr
->rrdata
.srv_rec
.port
= atoi(str_port
);
263 memcpy(rr
->rrdata
.srv_rec
.hostname
, hostname
, strlen(hostname
) + 1);
265 memcpy(rr
->key
, key
, strlen(key
) + 1);
270 static int rwrap_create_fake_soa_rr(const char *key
,
272 struct rwrap_fake_rr
*rr
)
274 const char *nameserver
;
282 /* parse the value into nameserver, mailbox, serial, refresh,
283 * retry, expire, minimum and check the validity
286 NEXT_KEY(nameserver
, mailbox
);
287 NEXT_KEY(mailbox
, str_serial
);
288 NEXT_KEY(str_serial
, str_refresh
);
289 NEXT_KEY(str_refresh
, str_retry
);
290 NEXT_KEY(str_retry
, str_expire
);
291 NEXT_KEY(str_expire
, str_minimum
);
292 if (nameserver
== NULL
|| mailbox
== NULL
|| str_serial
== NULL
||
293 str_refresh
== NULL
|| str_retry
== NULL
|| str_expire
== NULL
||
294 str_minimum
== NULL
) {
295 RWRAP_LOG(RWRAP_LOG_ERROR
,
296 "Malformed SOA entry [%s]\n", value
);
300 memcpy(rr
->rrdata
.soa_rec
.nameserver
, nameserver
, strlen(nameserver
)+1);
301 memcpy(rr
->rrdata
.soa_rec
.mailbox
, mailbox
, strlen(mailbox
)+1);
303 rr
->rrdata
.soa_rec
.serial
= atoi(str_serial
);
304 rr
->rrdata
.soa_rec
.refresh
= atoi(str_refresh
);
305 rr
->rrdata
.soa_rec
.retry
= atoi(str_retry
);
306 rr
->rrdata
.soa_rec
.expire
= atoi(str_expire
);
307 rr
->rrdata
.soa_rec
.minimum
= atoi(str_minimum
);
309 memcpy(rr
->key
, key
, strlen(key
) + 1);
314 static int rwrap_create_fake_cname_rr(const char *key
,
316 struct rwrap_fake_rr
*rr
)
318 memcpy(rr
->rrdata
.cname_rec
, value
, strlen(value
) + 1);
319 memcpy(rr
->key
, key
, strlen(key
) + 1);
320 rr
->type
= ns_t_cname
;
324 /* Prepares a fake header with a single response. Advances header_blob */
325 static ssize_t
rwrap_fake_header(uint8_t **header_blob
, size_t remaining
,
326 size_t ancount
, size_t arcount
)
331 if (remaining
< NS_HFIXEDSZ
) {
332 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small!\n");
337 memset(hb
, 0, NS_HFIXEDSZ
);
340 h
->id
= res_randomid(); /* random query ID */
341 h
->qr
= 1; /* response flag */
342 h
->rd
= 1; /* recursion desired */
343 h
->ra
= 1; /* resursion available */
345 h
->qdcount
= htons(1); /* no. of questions */
346 h
->ancount
= htons(ancount
); /* no. of answers */
347 h
->arcount
= htons(arcount
); /* no. of add'tl records */
349 hb
+= NS_HFIXEDSZ
; /* move past the header */
355 static ssize_t
rwrap_fake_question(const char *question
,
357 uint8_t **question_ptr
,
360 uint8_t *qb
= *question_ptr
;
363 n
= ns_name_compress(question
, qb
, remaining
, NULL
, NULL
);
365 RWRAP_LOG(RWRAP_LOG_ERROR
,
366 "Failed to compress [%s]\n", question
);
373 if (remaining
< 2 * sizeof(uint16_t)) {
374 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small!\n");
379 NS_PUT16(ns_c_in
, qb
);
382 return n
+ 2 * sizeof(uint16_t);
385 static ssize_t
rwrap_fake_rdata_common(uint16_t type
,
391 uint8_t *rd
= *rdata_ptr
;
394 written
= ns_name_compress(key
, rd
, remaining
, NULL
, NULL
);
396 RWRAP_LOG(RWRAP_LOG_ERROR
,
397 "Failed to compress [%s]\n", key
);
401 remaining
-= written
;
403 if (remaining
< 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
404 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small\n");
409 NS_PUT16(ns_c_in
, rd
);
410 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL
, rd
);
411 NS_PUT16(rdata_size
, rd
);
413 if (remaining
< rdata_size
) {
414 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small\n");
419 return written
+ 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size
;
422 static ssize_t
rwrap_fake_a(struct rwrap_fake_rr
*rr
,
426 uint8_t *a
= answer_ptr
;
429 if (rr
== NULL
|| rr
->type
!= ns_t_a
) {
430 RWRAP_LOG(RWRAP_LOG_ERROR
,
431 "Malformed record, no or wrong value!\n");
434 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding A RR");
436 resp_size
= rwrap_fake_rdata_common(ns_t_a
, sizeof(struct in_addr
), rr
->key
,
442 memcpy(a
, &rr
->rrdata
.a_rec
, sizeof(struct in_addr
));
447 static ssize_t
rwrap_fake_aaaa(struct rwrap_fake_rr
*rr
,
454 if (rr
== NULL
|| rr
->type
!= ns_t_aaaa
) {
455 RWRAP_LOG(RWRAP_LOG_ERROR
,
456 "Malformed record, no or wrong value!\n");
459 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding AAAA RR");
461 resp_size
= rwrap_fake_rdata_common(ns_t_aaaa
, sizeof(struct in6_addr
),
462 rr
->key
, anslen
, &a
);
467 memcpy(a
, &rr
->rrdata
.aaaa_rec
, sizeof(struct in6_addr
));
472 static ssize_t
rwrap_fake_srv(struct rwrap_fake_rr
*rr
,
479 unsigned char hostname_compressed
[MAXDNAME
];
480 ssize_t compressed_len
;
482 if (rr
== NULL
|| rr
->type
!= ns_t_srv
) {
483 RWRAP_LOG(RWRAP_LOG_ERROR
,
484 "Malformed record, no or wrong value!\n");
487 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding SRV RR");
488 rdata_size
= 3 * sizeof(uint16_t);
490 /* Prepare the data to write */
491 compressed_len
= ns_name_compress(rr
->rrdata
.srv_rec
.hostname
,
492 hostname_compressed
, MAXDNAME
,
494 if (compressed_len
< 0) {
497 rdata_size
+= compressed_len
;
499 resp_size
= rwrap_fake_rdata_common(ns_t_srv
, rdata_size
,
500 rr
->key
, anslen
, &a
);
505 NS_PUT16(rr
->rrdata
.srv_rec
.prio
, a
);
506 NS_PUT16(rr
->rrdata
.srv_rec
.weight
, a
);
507 NS_PUT16(rr
->rrdata
.srv_rec
.port
, a
);
508 memcpy(a
, hostname_compressed
, compressed_len
);
513 static ssize_t
rwrap_fake_soa(struct rwrap_fake_rr
*rr
,
520 unsigned char nameser_compressed
[MAXDNAME
];
521 ssize_t compressed_ns_len
;
522 unsigned char mailbox_compressed
[MAXDNAME
];
523 ssize_t compressed_mb_len
;
525 if (rr
== NULL
|| rr
->type
!= ns_t_soa
) {
526 RWRAP_LOG(RWRAP_LOG_ERROR
,
527 "Malformed record, no or wrong value!\n");
530 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding SOA RR");
531 rdata_size
= 5 * sizeof(uint16_t);
533 compressed_ns_len
= ns_name_compress(rr
->rrdata
.soa_rec
.nameserver
,
535 MAXDNAME
, NULL
, NULL
);
536 if (compressed_ns_len
< 0) {
539 rdata_size
+= compressed_ns_len
;
541 compressed_mb_len
= ns_name_compress(rr
->rrdata
.soa_rec
.mailbox
,
543 MAXDNAME
, NULL
, NULL
);
544 if (compressed_mb_len
< 0) {
547 rdata_size
+= compressed_mb_len
;
549 resp_size
= rwrap_fake_rdata_common(ns_t_soa
, rdata_size
,
550 rr
->key
, anslen
, &a
);
555 memcpy(a
, nameser_compressed
, compressed_ns_len
);
556 a
+= compressed_ns_len
;
557 memcpy(a
, mailbox_compressed
, compressed_mb_len
);
558 a
+= compressed_mb_len
;
559 NS_PUT32(rr
->rrdata
.soa_rec
.serial
, a
);
560 NS_PUT32(rr
->rrdata
.soa_rec
.refresh
, a
);
561 NS_PUT32(rr
->rrdata
.soa_rec
.retry
, a
);
562 NS_PUT32(rr
->rrdata
.soa_rec
.expire
, a
);
563 NS_PUT32(rr
->rrdata
.soa_rec
.minimum
, a
);
568 static ssize_t
rwrap_fake_cname(struct rwrap_fake_rr
*rr
,
574 unsigned char hostname_compressed
[MAXDNAME
];
577 if (rr
== NULL
|| rr
->type
!= ns_t_cname
) {
578 RWRAP_LOG(RWRAP_LOG_ERROR
,
579 "Malformed record, no or wrong value!\n");
582 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding CNAME RR");
584 /* Prepare the data to write */
585 rdata_size
= ns_name_compress(rr
->rrdata
.cname_rec
,
586 hostname_compressed
, MAXDNAME
,
588 if (rdata_size
< 0) {
592 resp_size
= rwrap_fake_rdata_common(ns_t_cname
, rdata_size
,
593 rr
->key
, anslen
, &a
);
598 memcpy(a
, hostname_compressed
, rdata_size
);
603 #define RESOLV_MATCH(line, name) \
604 (strncmp(line, name, sizeof(name) - 1) == 0 && \
605 (line[sizeof(name) - 1] == ' ' || \
606 line[sizeof(name) - 1] == '\t'))
608 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
609 ((type) == (ns_type) && \
610 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
611 (strcasecmp(key, query)) == 0)
614 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
615 const char *query
, int type
,
616 struct rwrap_fake_rr
*rr
);
618 static int rwrap_srv_recurse(const char *hostfile
, unsigned recursion
,
619 const char *query
, struct rwrap_fake_rr
*rr
)
623 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
624 if (rc
== 0) return 0;
626 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
627 if (rc
== ENOENT
) rc
= 0;
632 static int rwrap_cname_recurse(const char *hostfile
, unsigned recursion
,
633 const char *query
, struct rwrap_fake_rr
*rr
)
637 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
638 if (rc
== 0) return 0;
640 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
641 if (rc
== 0) return 0;
643 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_cname
, rr
);
644 if (rc
== ENOENT
) rc
= 0;
649 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
650 const char *query
, int type
,
651 struct rwrap_fake_rr
*rr
)
659 if (recursion
>= RWRAP_MAX_RECURSION
) {
660 RWRAP_LOG(RWRAP_LOG_ERROR
, "Recursed too deep!\n");
664 RWRAP_LOG(RWRAP_LOG_TRACE
,
665 "Searching in fake hosts file %s\n", hostfile
);
667 fp
= fopen(hostfile
, "r");
669 RWRAP_LOG(RWRAP_LOG_ERROR
,
670 "Opening %s failed: %s",
671 hostfile
, strerror(errno
));
675 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
682 NEXT_KEY(rec_type
, key
);
683 NEXT_KEY(key
, value
);
685 if (key
== NULL
|| value
== NULL
) {
686 RWRAP_LOG(RWRAP_LOG_WARN
,
687 "Malformed line: not enough parts, use \"rec_type key data\n"
688 "For example \"A cwrap.org 10.10.10.10\"");
693 while(q
[0] != '\n' && q
[0] != '\0') {
698 if (TYPE_MATCH(type
, ns_t_a
, rec_type
, "A", key
, query
)) {
699 rc
= rwrap_create_fake_a_rr(key
, value
, rr
);
701 } else if (TYPE_MATCH(type
, ns_t_aaaa
,
702 rec_type
, "AAAA", key
, query
)) {
703 rc
= rwrap_create_fake_aaaa_rr(key
, value
, rr
);
705 } else if (TYPE_MATCH(type
, ns_t_srv
,
706 rec_type
, "SRV", key
, query
)) {
707 rc
= rwrap_create_fake_srv_rr(key
, value
, rr
);
709 rc
= rwrap_srv_recurse(hostfile
, recursion
+1,
710 rr
->rrdata
.srv_rec
.hostname
,
714 } else if (TYPE_MATCH(type
, ns_t_soa
,
715 rec_type
, "SOA", key
, query
)) {
716 rc
= rwrap_create_fake_soa_rr(key
, value
, rr
);
718 } else if (TYPE_MATCH(type
, ns_t_cname
,
719 rec_type
, "CNAME", key
, query
)) {
720 rc
= rwrap_create_fake_cname_rr(key
, value
, rr
);
722 rc
= rwrap_cname_recurse(hostfile
, recursion
+1,
726 } else if (TYPE_MATCH(type
, ns_t_a
, rec_type
, "CNAME", key
, query
)) {
727 rc
= rwrap_create_fake_cname_rr(key
, value
, rr
);
729 rc
= rwrap_cname_recurse(hostfile
, recursion
+1,
736 if (rc
== ENOENT
&& recursion
== 0 && key
!= NULL
) {
737 RWRAP_LOG(RWRAP_LOG_TRACE
, "Record for [%s] not found\n", query
);
738 memcpy(rr
->key
, key
, strlen(key
) + 1);
745 static ssize_t
rwrap_fake_empty(int type
,
746 const char *question
,
751 size_t remaining
= anslen
;
753 resp_data
= rwrap_fake_header(&answer
, remaining
, 0, 0);
757 remaining
-= resp_data
;
759 resp_data
+= rwrap_fake_question(question
, type
, &answer
, remaining
);
763 remaining
-= resp_data
;
765 resp_data
+= rwrap_fake_rdata_common(type
, 0, question
,
774 static inline bool rwrap_known_type(int type
)
788 static int rwrap_ancount(struct rwrap_fake_rr
*rrs
, int qtype
)
793 /* Include all RRs in the stack until the sought type
794 * in the answer section. This is the case i.e. when looking
795 * up an A record but the name points to a CNAME
797 for (i
= 0; i
< RWRAP_MAX_RECURSION
; i
++) {
800 if (rwrap_known_type(rrs
[i
].type
) &&
801 rrs
[i
].type
== qtype
) {
806 /* Return 0 records if the sought type wasn't in the stack */
807 return i
< RWRAP_MAX_RECURSION
? ancount
: 0;
810 static int rwrap_arcount(struct rwrap_fake_rr
*rrs
, int ancount
)
815 /* start from index ancount */
816 for (i
= ancount
; i
< RWRAP_MAX_RECURSION
; i
++) {
817 if (rwrap_known_type(rrs
[i
].type
)) {
825 static ssize_t
rwrap_add_rr(struct rwrap_fake_rr
*rr
,
833 resp_data
= rwrap_fake_a(rr
, answer
, anslen
);
836 resp_data
= rwrap_fake_aaaa(rr
, answer
, anslen
);
839 resp_data
= rwrap_fake_srv(rr
, answer
, anslen
);
842 resp_data
= rwrap_fake_soa(rr
, answer
, anslen
);
845 resp_data
= rwrap_fake_cname(rr
, answer
, anslen
);
854 static ssize_t
rwrap_fake_answer(struct rwrap_fake_rr
*rrs
,
862 size_t remaining
= anslen
;
867 ancount
= rwrap_ancount(rrs
, type
);
868 arcount
= rwrap_arcount(rrs
, ancount
);
869 RWRAP_LOG(RWRAP_LOG_TRACE
,
870 "Got %d answers and %d additional records\n", ancount
, arcount
);
872 resp_data
= rwrap_fake_header(&answer
, remaining
, ancount
, arcount
);
876 remaining
-= resp_data
;
878 resp_data
+= rwrap_fake_question(rrs
->key
, rrs
->type
, &answer
, remaining
);
882 remaining
-= resp_data
;
885 for (i
= 0; i
< ancount
; i
++) {
886 rrlen
= rwrap_add_rr(&rrs
[i
], answer
, remaining
);
895 /* add authoritative NS here? */
897 /* additional records */
898 for (i
= ancount
; i
< ancount
+ arcount
; i
++) {
899 rrlen
= rwrap_add_rr(&rrs
[i
], answer
, remaining
);
911 /* Reads in a file in the following format:
914 * Malformed entried are silently skipped.
915 * Allocates answer buffer of size anslen that has to be freed after use.
917 static int rwrap_res_fake_hosts(const char *hostfile
,
920 unsigned char *answer
,
924 char *query_name
= NULL
;
925 size_t qlen
= strlen(query
);
926 struct rwrap_fake_rr rrs
[RWRAP_MAX_RECURSION
];
929 RWRAP_LOG(RWRAP_LOG_TRACE
,
930 "Searching in fake hosts file %s\n", hostfile
);
932 if (qlen
> 0 && query
[qlen
-1] == '.') {
936 query_name
= strndup(query
, qlen
);
937 if (query_name
== NULL
) {
941 rwrap_fake_rr_init(rrs
, RWRAP_MAX_RECURSION
);
943 rc
= rwrap_get_record(hostfile
, 0, query_name
, type
, rrs
);
946 RWRAP_LOG(RWRAP_LOG_TRACE
,
947 "Found record for [%s]\n", query_name
);
948 resp_size
= rwrap_fake_answer(rrs
, type
, answer
, anslen
);
951 RWRAP_LOG(RWRAP_LOG_TRACE
,
952 "No record for [%s]\n", query_name
);
953 resp_size
= rwrap_fake_empty(type
, rrs
->key
, answer
, anslen
);
956 RWRAP_LOG(RWRAP_LOG_ERROR
,
957 "Error searching for [%s]\n", query_name
);
964 RWRAP_LOG(RWRAP_LOG_ERROR
,
965 "Error faking answer for [%s]\n", query_name
);
968 RWRAP_LOG(RWRAP_LOG_TRACE
,
969 "Successfully faked answer for [%s]\n",
978 /*********************************************************
979 * RWRAP LOADING LIBC FUNCTIONS
980 *********************************************************/
984 struct rwrap_libc_fns
{
985 int (*libc_res_init
)(void);
986 int (*libc___res_init
)(void);
987 int (*libc_res_ninit
)(struct __res_state
*state
);
988 int (*libc___res_ninit
)(struct __res_state
*state
);
989 void (*libc_res_nclose
)(struct __res_state
*state
);
990 void (*libc___res_nclose
)(struct __res_state
*state
);
991 void (*libc_res_close
)(void);
992 void (*libc___res_close
)(void);
993 int (*libc_res_nquery
)(struct __res_state
*state
,
997 unsigned char *answer
,
999 int (*libc___res_nquery
)(struct __res_state
*state
,
1003 unsigned char *answer
,
1005 int (*libc_res_nsearch
)(struct __res_state
*state
,
1009 unsigned char *answer
,
1011 int (*libc___res_nsearch
)(struct __res_state
*state
,
1015 unsigned char *answer
,
1021 void *libresolv_handle
;
1028 struct rwrap_libc_fns fns
;
1031 static struct rwrap rwrap
;
1039 static const char *rwrap_str_lib(enum rwrap_lib lib
)
1044 case RWRAP_LIBRESOLV
:
1048 /* Compiler would warn us about unhandled enum value if we get here */
1053 static void *rwrap_load_lib_handle(enum rwrap_lib lib
)
1055 int flags
= RTLD_LAZY
;
1056 void *handle
= NULL
;
1059 #ifdef RTLD_DEEPBIND
1060 flags
|= RTLD_DEEPBIND
;
1064 case RWRAP_LIBRESOLV
:
1065 #ifdef HAVE_LIBRESOLV
1066 handle
= rwrap
.libresolv_handle
;
1067 if (handle
== NULL
) {
1068 for (i
= 10; i
>= 0; i
--) {
1069 char soname
[256] = {0};
1071 snprintf(soname
, sizeof(soname
), "libresolv.so.%d", i
);
1072 handle
= dlopen(soname
, flags
);
1073 if (handle
!= NULL
) {
1078 rwrap
.libresolv_handle
= handle
;
1084 handle
= rwrap
.libc_handle
;
1086 if (handle
== NULL
) {
1087 handle
= dlopen(LIBC_SO
, flags
);
1089 rwrap
.libc_handle
= handle
;
1092 if (handle
== NULL
) {
1093 for (i
= 10; i
>= 0; i
--) {
1094 char soname
[256] = {0};
1096 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
1097 handle
= dlopen(soname
, flags
);
1098 if (handle
!= NULL
) {
1103 rwrap
.libc_handle
= handle
;
1108 if (handle
== NULL
) {
1110 handle
= rwrap
.libc_handle
= rwrap
.libresolv_handle
= RTLD_NEXT
;
1112 RWRAP_LOG(RWRAP_LOG_ERROR
,
1113 "Failed to dlopen library: %s\n",
1122 static void *_rwrap_load_lib_function(enum rwrap_lib lib
, const char *fn_name
)
1127 handle
= rwrap_load_lib_handle(lib
);
1129 func
= dlsym(handle
, fn_name
);
1131 RWRAP_LOG(RWRAP_LOG_ERROR
,
1132 "Failed to find %s: %s\n",
1133 fn_name
, dlerror());
1137 RWRAP_LOG(RWRAP_LOG_TRACE
,
1138 "Loaded %s from %s",
1139 fn_name
, rwrap_str_lib(lib
));
1143 #define rwrap_load_lib_function(lib, fn_name) \
1144 if (rwrap.fns.libc_##fn_name == NULL) { \
1145 *(void **) (&rwrap.fns.libc_##fn_name) = \
1146 _rwrap_load_lib_function(lib, #fn_name); \
1152 * Functions especially from libc need to be loaded individually, you can't load
1153 * all at once or gdb will segfault at startup. The same applies to valgrind and
1154 * has probably something todo with with the linker.
1155 * So we need load each function at the point it is called the first time.
1158 static int libc_res_init(void)
1160 #if defined(HAVE_RES_INIT)
1161 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_init
);
1163 return rwrap
.fns
.libc_res_init();
1164 #elif defined(HAVE___RES_INIT)
1165 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_init
);
1167 return rwrap
.fns
.libc___res_init();
1172 static int libc_res_ninit(struct __res_state
*state
)
1174 #if defined(HAVE_RES_NINIT)
1176 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1177 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_ninit
);
1178 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1179 rwrap_load_lib_function(RWRAP_LIBC
, res_ninit
);
1180 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1182 return rwrap
.fns
.libc_res_ninit(state
);
1183 #elif defined(HAVE___RES_NINIT)
1184 rwrap_load_lib_function(RWRAP_LIBC
, __res_ninit
);
1186 return rwrap
.fns
.libc___res_ninit(state
);
1188 #error "No res_ninit function"
1192 static void libc_res_nclose(struct __res_state
*state
)
1194 #if defined(HAVE_RES_NCLOSE)
1196 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1197 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nclose
);
1198 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1199 rwrap_load_lib_function(RWRAP_LIBC
, res_nclose
);
1200 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1202 rwrap
.fns
.libc_res_nclose(state
);
1203 #elif defined(HAVE___RES_NCLOSE)
1204 rwrap_load_lib_function(RWRAP_LIBC
, __res_nclose
);
1206 rwrap
.fns
.libc___res_nclose(state
);
1208 #error "No res_nclose function"
1212 static int libc_res_nquery(struct __res_state
*state
,
1216 unsigned char *answer
,
1219 #if defined(HAVE_RES_NQUERY)
1220 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nquery
);
1222 return rwrap
.fns
.libc_res_nquery(state
,
1228 #elif defined(HAVE___RES_NQUERY)
1229 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nquery
);
1231 return rwrap
.fns
.libc___res_nquery(state
,
1238 #error "No res_nquery function"
1242 static int libc_res_nsearch(struct __res_state
*state
,
1246 unsigned char *answer
,
1249 #if defined(HAVE_RES_NSEARCH)
1250 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nsearch
);
1252 return rwrap
.fns
.libc_res_nsearch(state
,
1258 #elif defined(HAVE___RES_NSEARCH)
1259 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nsearch
);
1261 return rwrap
.fns
.libc___res_nsearch(state
,
1268 #error "No res_nsearch function"
1272 /****************************************************************************
1274 ***************************************************************************/
1276 static int rwrap_parse_resolv_conf(struct __res_state
*state
,
1277 const char *resolv_conf
)
1283 fp
= fopen(resolv_conf
, "r");
1285 RWRAP_LOG(RWRAP_LOG_ERROR
,
1286 "Opening %s failed: %s",
1287 resolv_conf
, strerror(errno
));
1291 while(fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1294 /* Ignore comments */
1295 if (buf
[0] == '#' || buf
[0] == ';') {
1299 if (RESOLV_MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
1304 p
= buf
+ strlen("nameserver");
1306 /* Skip spaces and tabs */
1307 while(isblank((int)p
[0])) {
1312 while(q
[0] != '\n' && q
[0] != '\0') {
1317 ok
= inet_pton(AF_INET
, p
, &a
);
1319 state
->nsaddr_list
[state
->nscount
] = (struct sockaddr_in
) {
1320 .sin_family
= AF_INET
,
1322 .sin_port
= htons(53),
1329 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1332 ok
= inet_pton(AF_INET6
, p
, &a6
);
1334 struct sockaddr_in6
*sa6
;
1336 sa6
= malloc(sizeof(*sa6
));
1342 sa6
->sin6_family
= AF_INET6
;
1343 sa6
->sin6_port
= htons(53);
1344 sa6
->sin6_flowinfo
= 0;
1345 sa6
->sin6_addr
= a6
;
1347 state
->_u
._ext
.nsaddrs
[state
->_u
._ext
.nscount
] = sa6
;
1348 state
->_u
._ext
.nssocks
[state
->_u
._ext
.nscount
] = -1;
1349 state
->_u
._ext
.nsmap
[state
->_u
._ext
.nscount
] = MAXNS
+ 1;
1351 state
->_u
._ext
.nscount
++;
1354 RWRAP_LOG(RWRAP_LOG_ERROR
,
1355 "Malformed DNS server");
1358 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1360 * BSD uses an opaque structure to store the
1361 * IPv6 addresses. So we can not simply store
1362 * these addresses the same way as above.
1364 RWRAP_LOG(RWRAP_LOG_WARN
,
1365 "resolve_wrapper does not support "
1366 "IPv6 on this platform");
1371 } /* TODO: match other keywords */
1375 RWRAP_LOG(RWRAP_LOG_ERROR
,
1376 "Reading from %s failed",
1386 /****************************************************************************
1388 ***************************************************************************/
1390 static int rwrap_res_ninit(struct __res_state
*state
)
1394 rc
= libc_res_ninit(state
);
1396 const char *resolv_conf
= getenv("RESOLV_WRAPPER_CONF");
1398 if (resolv_conf
!= NULL
) {
1401 (void)i
; /* maybe unused */
1403 /* Delete name servers */
1405 memset(state
->nsaddr_list
, 0, sizeof(state
->nsaddr_list
));
1407 state
->_u
._ext
.nscount
= 0;
1408 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1409 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1410 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1414 rc
= rwrap_parse_resolv_conf(state
, resolv_conf
);
1421 #if defined(HAVE_RES_NINIT)
1422 int res_ninit(struct __res_state
*state
)
1423 #elif defined(HAVE___RES_NINIT)
1424 int __res_ninit(struct __res_state
*state
)
1427 return rwrap_res_ninit(state
);
1430 /****************************************************************************
1432 ***************************************************************************/
1434 static struct __res_state rwrap_res_state
;
1436 static int rwrap_res_init(void)
1440 rc
= rwrap_res_ninit(&rwrap_res_state
);
1445 #if defined(HAVE_RES_INIT)
1447 #elif defined(HAVE___RES_INIT)
1448 int __res_init(void)
1451 return rwrap_res_init();
1454 /****************************************************************************
1456 ***************************************************************************/
1458 static void rwrap_res_nclose(struct __res_state
*state
)
1460 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1464 libc_res_nclose(state
);
1466 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1467 if (state
!= NULL
) {
1468 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1469 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1475 #if defined(HAVE_RES_NCLOSE)
1476 void res_nclose(struct __res_state
*state
)
1477 #elif defined(HAVE___RES_NCLOSE)
1478 void __res_nclose(struct __res_state
*state
)
1481 rwrap_res_nclose(state
);
1484 /****************************************************************************
1486 ***************************************************************************/
1488 static void rwrap_res_close(void)
1490 rwrap_res_nclose(&rwrap_res_state
);
1493 #if defined(HAVE_RES_CLOSE)
1494 void res_close(void)
1495 #elif defined(HAVE___RES_CLOSE)
1496 void __res_close(void)
1502 /****************************************************************************
1504 ***************************************************************************/
1506 static int rwrap_res_nquery(struct __res_state
*state
,
1510 unsigned char *answer
,
1514 const char *fake_hosts
;
1519 RWRAP_LOG(RWRAP_LOG_TRACE
,
1520 "Resolve the domain name [%s] - class=%d, type=%d",
1521 dname
, class, type
);
1523 for (i
= 0; i
< state
->nscount
; i
++) {
1524 char ip
[INET6_ADDRSTRLEN
];
1526 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1527 RWRAP_LOG(RWRAP_LOG_TRACE
,
1533 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1534 if (fake_hosts
!= NULL
) {
1535 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1537 rc
= libc_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1541 RWRAP_LOG(RWRAP_LOG_TRACE
,
1542 "The returned response length is: %d",
1548 #if defined(HAVE_RES_NQUERY)
1549 int res_nquery(struct __res_state
*state
,
1553 unsigned char *answer
,
1555 #elif defined(HAVE___RES_NQUERY)
1556 int __res_nquery(struct __res_state
*state
,
1560 unsigned char *answer
,
1564 return rwrap_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1567 /****************************************************************************
1569 ***************************************************************************/
1571 static int rwrap_res_query(const char *dname
,
1574 unsigned char *answer
,
1579 rc
= rwrap_res_ninit(&rwrap_res_state
);
1584 rc
= rwrap_res_nquery(&rwrap_res_state
,
1594 #if defined(HAVE_RES_QUERY)
1595 int res_query(const char *dname
,
1598 unsigned char *answer
,
1600 #elif defined(HAVE___RES_QUERY)
1601 int __res_query(const char *dname
,
1604 unsigned char *answer
,
1608 return rwrap_res_query(dname
, class, type
, answer
, anslen
);
1611 /****************************************************************************
1613 ***************************************************************************/
1615 static int rwrap_res_nsearch(struct __res_state
*state
,
1619 unsigned char *answer
,
1623 const char *fake_hosts
;
1628 RWRAP_LOG(RWRAP_LOG_TRACE
,
1629 "Resolve the domain name [%s] - class=%d, type=%d",
1630 dname
, class, type
);
1632 for (i
= 0; i
< state
->nscount
; i
++) {
1633 char ip
[INET6_ADDRSTRLEN
];
1635 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1636 RWRAP_LOG(RWRAP_LOG_TRACE
,
1642 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1643 if (fake_hosts
!= NULL
) {
1644 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1646 rc
= libc_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1649 RWRAP_LOG(RWRAP_LOG_TRACE
,
1650 "The returned response length is: %d",
1656 #if defined(HAVE_RES_NSEARCH)
1657 int res_nsearch(struct __res_state
*state
,
1661 unsigned char *answer
,
1663 #elif defined(HAVE___RES_NSEARCH)
1664 int __res_nsearch(struct __res_state
*state
,
1668 unsigned char *answer
,
1672 return rwrap_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1675 /****************************************************************************
1677 ***************************************************************************/
1679 static int rwrap_res_search(const char *dname
,
1682 unsigned char *answer
,
1687 rc
= rwrap_res_ninit(&rwrap_res_state
);
1692 rc
= rwrap_res_nsearch(&rwrap_res_state
,
1702 #if defined(HAVE_RES_SEARCH)
1703 int res_search(const char *dname
,
1706 unsigned char *answer
,
1708 #elif defined(HAVE___RES_SEARCH)
1709 int __res_search(const char *dname
,
1712 unsigned char *answer
,
1716 return rwrap_res_search(dname
, class, type
, answer
, anslen
);