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 typedef int (*__libc_res_ninit
)(struct __res_state
*state
);
985 typedef int (*__libc___res_ninit
)(struct __res_state
*state
);
986 typedef void (*__libc_res_nclose
)(struct __res_state
*state
);
987 typedef void (*__libc___res_nclose
)(struct __res_state
*state
);
988 typedef int (*__libc_res_nquery
)(struct __res_state
*state
,
992 unsigned char *answer
,
994 typedef int (*__libc___res_nquery
)(struct __res_state
*state
,
998 unsigned char *answer
,
1000 typedef int (*__libc_res_nsearch
)(struct __res_state
*state
,
1004 unsigned char *answer
,
1006 typedef int (*__libc___res_nsearch
)(struct __res_state
*state
,
1010 unsigned char *answer
,
1013 #define RWRAP_SYMBOL_ENTRY(i) \
1019 struct rwrap_libc_symbols
{
1020 RWRAP_SYMBOL_ENTRY(res_ninit
);
1021 RWRAP_SYMBOL_ENTRY(__res_ninit
);
1022 RWRAP_SYMBOL_ENTRY(res_nclose
);
1023 RWRAP_SYMBOL_ENTRY(__res_nclose
);
1024 RWRAP_SYMBOL_ENTRY(res_nquery
);
1025 RWRAP_SYMBOL_ENTRY(__res_nquery
);
1026 RWRAP_SYMBOL_ENTRY(res_nsearch
);
1027 RWRAP_SYMBOL_ENTRY(__res_nsearch
);
1029 #undef RWRAP_SYMBOL_ENTRY
1034 struct rwrap_libc_symbols symbols
;
1039 struct rwrap_libc_symbols symbols
;
1048 static struct rwrap rwrap
;
1056 static const char *rwrap_str_lib(enum rwrap_lib lib
)
1061 case RWRAP_LIBRESOLV
:
1065 /* Compiler would warn us about unhandled enum value if we get here */
1070 static void *rwrap_load_lib_handle(enum rwrap_lib lib
)
1072 int flags
= RTLD_LAZY
;
1073 void *handle
= NULL
;
1076 #ifdef RTLD_DEEPBIND
1077 flags
|= RTLD_DEEPBIND
;
1081 case RWRAP_LIBRESOLV
:
1082 #ifdef HAVE_LIBRESOLV
1083 handle
= rwrap
.libresolv
.handle
;
1084 if (handle
== NULL
) {
1085 for (i
= 10; i
>= 0; i
--) {
1086 char soname
[256] = {0};
1088 snprintf(soname
, sizeof(soname
), "libresolv.so.%d", i
);
1089 handle
= dlopen(soname
, flags
);
1090 if (handle
!= NULL
) {
1095 rwrap
.libresolv
.handle
= handle
;
1101 handle
= rwrap
.libc
.handle
;
1103 if (handle
== NULL
) {
1104 handle
= dlopen(LIBC_SO
, flags
);
1106 rwrap
.libc
.handle
= handle
;
1109 if (handle
== NULL
) {
1110 for (i
= 10; i
>= 0; i
--) {
1111 char soname
[256] = {0};
1113 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
1114 handle
= dlopen(soname
, flags
);
1115 if (handle
!= NULL
) {
1120 rwrap
.libc
.handle
= handle
;
1125 if (handle
== NULL
) {
1127 handle
= rwrap
.libc
.handle
= rwrap
.libresolv
.handle
= RTLD_NEXT
;
1129 RWRAP_LOG(RWRAP_LOG_ERROR
,
1130 "Failed to dlopen library: %s\n",
1139 static void *_rwrap_bind_symbol(enum rwrap_lib lib
, const char *fn_name
)
1144 handle
= rwrap_load_lib_handle(lib
);
1146 func
= dlsym(handle
, fn_name
);
1148 RWRAP_LOG(RWRAP_LOG_ERROR
,
1149 "Failed to find %s: %s\n",
1150 fn_name
, dlerror());
1154 RWRAP_LOG(RWRAP_LOG_TRACE
,
1155 "Loaded %s from %s",
1156 fn_name
, rwrap_str_lib(lib
));
1160 #define rwrap_bind_symbol_libc(sym_name) \
1161 if (rwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
1162 rwrap.libc.symbols._libc_##sym_name.obj = \
1163 _rwrap_bind_symbol(RWRAP_LIBC, #sym_name); \
1166 #define rwrap_bind_symbol_libresolv(sym_name) \
1167 if (rwrap.libresolv.symbols._libc_##sym_name.obj == NULL) { \
1168 rwrap.libresolv.symbols._libc_##sym_name.obj = \
1169 _rwrap_bind_symbol(RWRAP_LIBRESOLV, #sym_name); \
1175 * Functions especially from libc need to be loaded individually, you can't load
1176 * all at once or gdb will segfault at startup. The same applies to valgrind and
1177 * has probably something todo with with the linker.
1178 * So we need load each function at the point it is called the first time.
1181 static int libc_res_ninit(struct __res_state
*state
)
1183 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1185 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1186 rwrap_bind_symbol_libresolv(res_ninit
);
1188 return rwrap
.libresolv
.symbols
._libc_res_ninit
.f(state
);
1189 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1190 rwrap_bind_symbol_libc(res_ninit
);
1192 return rwrap
.libc
.symbols
._libc_res_ninit
.f(state
);
1193 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1195 #elif defined(HAVE___RES_NINIT)
1196 rwrap_bind_symbol_libc(__res_ninit
);
1198 return rwrap
.libc
.symbols
._libc___res_ninit
.f(state
);
1200 #error "No res_ninit function"
1204 static void libc_res_nclose(struct __res_state
*state
)
1206 #if !defined(res_close) && defined(HAVE_RES_NCLOSE)
1208 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1209 rwrap_bind_symbol_libresolv(res_nclose
);
1211 rwrap
.libresolv
.symbols
._libc_res_nclose
.f(state
);
1213 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1214 rwrap_bind_symbol_libc(res_nclose
);
1216 rwrap
.libc
.symbols
._libc_res_nclose
.f(state
);
1218 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1220 #elif defined(HAVE___RES_NCLOSE)
1221 rwrap_bind_symbol_libc(__res_nclose
);
1223 rwrap
.libc
.symbols
._libc___res_nclose
.f(state
);
1225 #error "No res_nclose function"
1229 static int libc_res_nquery(struct __res_state
*state
,
1233 unsigned char *answer
,
1236 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1237 rwrap_bind_symbol_libresolv(res_nquery
);
1239 return rwrap
.libresolv
.symbols
._libc_res_nquery
.f(state
,
1245 #elif defined(HAVE___RES_NQUERY)
1246 rwrap_bind_symbol_libresolv(__res_nquery
);
1248 return rwrap
.libresolv
.symbols
._libc___res_nquery
.f(state
,
1255 #error "No res_nquery function"
1259 static int libc_res_nsearch(struct __res_state
*state
,
1263 unsigned char *answer
,
1266 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1267 rwrap_bind_symbol_libresolv(res_nsearch
);
1269 return rwrap
.libresolv
.symbols
._libc_res_nsearch
.f(state
,
1275 #elif defined(HAVE___RES_NSEARCH)
1276 rwrap_bind_symbol_libresolv(__res_nsearch
);
1278 return rwrap
.libresolv
.symbols
._libc___res_nsearch
.f(state
,
1285 #error "No res_nsearch function"
1289 /****************************************************************************
1291 ***************************************************************************/
1293 static int rwrap_parse_resolv_conf(struct __res_state
*state
,
1294 const char *resolv_conf
)
1300 fp
= fopen(resolv_conf
, "r");
1302 RWRAP_LOG(RWRAP_LOG_ERROR
,
1303 "Opening %s failed: %s",
1304 resolv_conf
, strerror(errno
));
1308 while(fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1311 /* Ignore comments */
1312 if (buf
[0] == '#' || buf
[0] == ';') {
1316 if (RESOLV_MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
1321 p
= buf
+ strlen("nameserver");
1323 /* Skip spaces and tabs */
1324 while(isblank((int)p
[0])) {
1329 while(q
[0] != '\n' && q
[0] != '\0') {
1334 ok
= inet_pton(AF_INET
, p
, &a
);
1336 state
->nsaddr_list
[state
->nscount
] = (struct sockaddr_in
) {
1337 .sin_family
= AF_INET
,
1339 .sin_port
= htons(53),
1346 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1349 ok
= inet_pton(AF_INET6
, p
, &a6
);
1351 struct sockaddr_in6
*sa6
;
1353 sa6
= malloc(sizeof(*sa6
));
1359 sa6
->sin6_family
= AF_INET6
;
1360 sa6
->sin6_port
= htons(53);
1361 sa6
->sin6_flowinfo
= 0;
1362 sa6
->sin6_addr
= a6
;
1364 state
->_u
._ext
.nsaddrs
[state
->_u
._ext
.nscount
] = sa6
;
1365 state
->_u
._ext
.nssocks
[state
->_u
._ext
.nscount
] = -1;
1366 state
->_u
._ext
.nsmap
[state
->_u
._ext
.nscount
] = MAXNS
+ 1;
1368 state
->_u
._ext
.nscount
++;
1371 RWRAP_LOG(RWRAP_LOG_ERROR
,
1372 "Malformed DNS server");
1375 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1377 * BSD uses an opaque structure to store the
1378 * IPv6 addresses. So we can not simply store
1379 * these addresses the same way as above.
1381 RWRAP_LOG(RWRAP_LOG_WARN
,
1382 "resolve_wrapper does not support "
1383 "IPv6 on this platform");
1388 } /* TODO: match other keywords */
1392 RWRAP_LOG(RWRAP_LOG_ERROR
,
1393 "Reading from %s failed",
1403 /****************************************************************************
1405 ***************************************************************************/
1407 static int rwrap_res_ninit(struct __res_state
*state
)
1411 rc
= libc_res_ninit(state
);
1413 const char *resolv_conf
= getenv("RESOLV_WRAPPER_CONF");
1415 if (resolv_conf
!= NULL
) {
1418 (void)i
; /* maybe unused */
1420 /* Delete name servers */
1422 memset(state
->nsaddr_list
, 0, sizeof(state
->nsaddr_list
));
1424 state
->_u
._ext
.nscount
= 0;
1425 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1426 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1427 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1431 rc
= rwrap_parse_resolv_conf(state
, resolv_conf
);
1438 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1439 int res_ninit(struct __res_state
*state
)
1440 #elif defined(HAVE___RES_NINIT)
1441 int __res_ninit(struct __res_state
*state
)
1444 return rwrap_res_ninit(state
);
1447 /****************************************************************************
1449 ***************************************************************************/
1451 static struct __res_state rwrap_res_state
;
1453 static int rwrap_res_init(void)
1457 rc
= rwrap_res_ninit(&rwrap_res_state
);
1462 #if !defined(res_ninit) && defined(HAVE_RES_INIT)
1464 #elif defined(HAVE___RES_INIT)
1465 int __res_init(void)
1468 return rwrap_res_init();
1471 /****************************************************************************
1473 ***************************************************************************/
1475 static void rwrap_res_nclose(struct __res_state
*state
)
1477 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1481 libc_res_nclose(state
);
1483 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1484 if (state
!= NULL
) {
1485 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1486 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1492 #if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)
1493 void res_nclose(struct __res_state
*state
)
1494 #elif defined(HAVE___RES_NCLOSE)
1495 void __res_nclose(struct __res_state
*state
)
1498 rwrap_res_nclose(state
);
1501 /****************************************************************************
1503 ***************************************************************************/
1505 static void rwrap_res_close(void)
1507 rwrap_res_nclose(&rwrap_res_state
);
1510 #if defined(HAVE_RES_CLOSE)
1511 void res_close(void)
1512 #elif defined(HAVE___RES_CLOSE)
1513 void __res_close(void)
1519 /****************************************************************************
1521 ***************************************************************************/
1523 static int rwrap_res_nquery(struct __res_state
*state
,
1527 unsigned char *answer
,
1531 const char *fake_hosts
;
1536 RWRAP_LOG(RWRAP_LOG_TRACE
,
1537 "Resolve the domain name [%s] - class=%d, type=%d",
1538 dname
, class, type
);
1540 for (i
= 0; i
< state
->nscount
; i
++) {
1541 char ip
[INET6_ADDRSTRLEN
];
1543 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1544 RWRAP_LOG(RWRAP_LOG_TRACE
,
1550 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1551 if (fake_hosts
!= NULL
) {
1552 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1554 rc
= libc_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1558 RWRAP_LOG(RWRAP_LOG_TRACE
,
1559 "The returned response length is: %d",
1565 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1566 int res_nquery(struct __res_state
*state
,
1570 unsigned char *answer
,
1572 #elif defined(HAVE___RES_NQUERY)
1573 int __res_nquery(struct __res_state
*state
,
1577 unsigned char *answer
,
1581 return rwrap_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1584 /****************************************************************************
1586 ***************************************************************************/
1588 static int rwrap_res_query(const char *dname
,
1591 unsigned char *answer
,
1596 rc
= rwrap_res_ninit(&rwrap_res_state
);
1601 rc
= rwrap_res_nquery(&rwrap_res_state
,
1611 #if !defined(res_query) && defined(HAVE_RES_QUERY)
1612 int res_query(const char *dname
,
1615 unsigned char *answer
,
1617 #elif defined(HAVE___RES_QUERY)
1618 int __res_query(const char *dname
,
1621 unsigned char *answer
,
1625 return rwrap_res_query(dname
, class, type
, answer
, anslen
);
1628 /****************************************************************************
1630 ***************************************************************************/
1632 static int rwrap_res_nsearch(struct __res_state
*state
,
1636 unsigned char *answer
,
1640 const char *fake_hosts
;
1645 RWRAP_LOG(RWRAP_LOG_TRACE
,
1646 "Resolve the domain name [%s] - class=%d, type=%d",
1647 dname
, class, type
);
1649 for (i
= 0; i
< state
->nscount
; i
++) {
1650 char ip
[INET6_ADDRSTRLEN
];
1652 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1653 RWRAP_LOG(RWRAP_LOG_TRACE
,
1659 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1660 if (fake_hosts
!= NULL
) {
1661 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1663 rc
= libc_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1666 RWRAP_LOG(RWRAP_LOG_TRACE
,
1667 "The returned response length is: %d",
1673 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1674 int res_nsearch(struct __res_state
*state
,
1678 unsigned char *answer
,
1680 #elif defined(HAVE___RES_NSEARCH)
1681 int __res_nsearch(struct __res_state
*state
,
1685 unsigned char *answer
,
1689 return rwrap_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1692 /****************************************************************************
1694 ***************************************************************************/
1696 static int rwrap_res_search(const char *dname
,
1699 unsigned char *answer
,
1704 rc
= rwrap_res_ninit(&rwrap_res_state
);
1709 rc
= rwrap_res_nsearch(&rwrap_res_state
,
1719 #if !defined(res_search) && defined(HAVE_RES_SEARCH)
1720 int res_search(const char *dname
,
1723 unsigned char *answer
,
1725 #elif defined(HAVE___RES_SEARCH)
1726 int __res_search(const char *dname
,
1729 unsigned char *answer
,
1733 return rwrap_res_search(dname
, class, type
, answer
, anslen
);