2 * Copyright (c) 2014 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014 Jakub Hrozek <jakub.hrozek@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <sys/types.h>
51 /* GCC has printf type attribute check. */
52 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
53 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
55 #define PRINTF_ATTRIBUTE(a,b)
56 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
58 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
59 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
61 #define DESTRUCTOR_ATTRIBUTE
62 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
64 #ifndef RWRAP_DEFAULT_FAKE_TTL
65 #define RWRAP_DEFAULT_FAKE_TTL 600
66 #endif /* RWRAP_DEFAULT_FAKE_TTL */
76 # define RWRAP_LOG(...)
79 static void rwrap_log(enum rwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
80 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
82 static void rwrap_log(enum rwrap_dbglvl_e dbglvl
,
84 const char *format
, ...)
92 d
= getenv("RESOLV_WRAPPER_DEBUGLEVEL");
98 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
103 case RWRAP_LOG_ERROR
:
105 "RWRAP_ERROR(%d) - %s: %s\n",
110 "RWRAP_WARN(%d) - %s: %s\n",
113 case RWRAP_LOG_DEBUG
:
115 "RWRAP_DEBUG(%d) - %s: %s\n",
118 case RWRAP_LOG_TRACE
:
120 "RWRAP_TRACE(%d) - %s: %s\n",
126 #endif /* NDEBUG RWRAP_LOG */
129 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
132 #define NEXT_KEY(buf, key) do { \
133 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
134 if ((key) != NULL) { \
138 while ((key) != NULL \
139 && (isblank((int)(key)[0]))) { \
144 #define RWRAP_MAX_RECURSION 5
146 /* Priority and weight can be omitted from the hosts file, but need to be part
149 #define DFL_SRV_PRIO 1
150 #define DFL_SRV_WEIGHT 100
152 struct rwrap_srv_rrdata
{
156 char hostname
[MAXDNAME
];
159 struct rwrap_soa_rrdata
{
165 char nameserver
[MAXDNAME
];
166 char mailbox
[MAXDNAME
];
169 struct rwrap_fake_rr
{
171 struct in_addr a_rec
;
172 struct in6_addr aaaa_rec
;
173 struct rwrap_srv_rrdata srv_rec
;
174 struct rwrap_soa_rrdata soa_rec
;
175 char cname_rec
[MAXDNAME
];
179 int type
; /* ns_t_* */
182 static void rwrap_fake_rr_init(struct rwrap_fake_rr
*rr
, size_t len
)
186 for (i
= 0; i
< len
; i
++) {
187 rr
[i
].type
= ns_t_invalid
;
191 static int rwrap_create_fake_a_rr(const char *key
,
193 struct rwrap_fake_rr
*rr
)
197 ok
= inet_pton(AF_INET
, value
, &rr
->rrdata
.a_rec
);
199 RWRAP_LOG(RWRAP_LOG_ERROR
,
200 "Failed to convert [%s] to binary\n", value
);
204 memcpy(rr
->key
, key
, strlen(key
) + 1);
209 static int rwrap_create_fake_aaaa_rr(const char *key
,
211 struct rwrap_fake_rr
*rr
)
215 ok
= inet_pton(AF_INET6
, value
, &rr
->rrdata
.aaaa_rec
);
217 RWRAP_LOG(RWRAP_LOG_ERROR
,
218 "Failed to convert [%s] to binary\n", value
);
222 memcpy(rr
->key
, key
, strlen(key
) + 1);
223 rr
->type
= ns_t_aaaa
;
227 static int rwrap_create_fake_srv_rr(const char *key
,
229 struct rwrap_fake_rr
*rr
)
234 const char *hostname
;
236 /* parse the value into priority, weight, port and hostname
237 * and check the validity */
239 NEXT_KEY(hostname
, str_port
);
240 NEXT_KEY(str_port
, str_prio
);
241 NEXT_KEY(str_prio
, str_weight
);
242 if (str_port
== NULL
|| hostname
== NULL
) {
243 RWRAP_LOG(RWRAP_LOG_ERROR
,
244 "Malformed SRV entry [%s]\n", value
);
249 rr
->rrdata
.srv_rec
.prio
= atoi(str_prio
);
251 rr
->rrdata
.srv_rec
.prio
= DFL_SRV_PRIO
;
254 rr
->rrdata
.srv_rec
.weight
= atoi(str_weight
);
256 rr
->rrdata
.srv_rec
.weight
= DFL_SRV_WEIGHT
;
258 rr
->rrdata
.srv_rec
.port
= atoi(str_port
);
259 memcpy(rr
->rrdata
.srv_rec
.hostname
, hostname
, strlen(hostname
) + 1);
261 memcpy(rr
->key
, key
, strlen(key
) + 1);
266 static int rwrap_create_fake_soa_rr(const char *key
,
268 struct rwrap_fake_rr
*rr
)
270 const char *nameserver
;
278 /* parse the value into nameserver, mailbox, serial, refresh,
279 * retry, expire, minimum and check the validity
282 NEXT_KEY(nameserver
, mailbox
);
283 NEXT_KEY(mailbox
, str_serial
);
284 NEXT_KEY(str_serial
, str_refresh
);
285 NEXT_KEY(str_refresh
, str_retry
);
286 NEXT_KEY(str_retry
, str_expire
);
287 NEXT_KEY(str_expire
, str_minimum
);
288 if (nameserver
== NULL
|| mailbox
== NULL
|| str_serial
== NULL
||
289 str_refresh
== NULL
|| str_retry
== NULL
|| str_expire
== NULL
||
290 str_minimum
== NULL
) {
291 RWRAP_LOG(RWRAP_LOG_ERROR
,
292 "Malformed SOA entry [%s]\n", value
);
296 memcpy(rr
->rrdata
.soa_rec
.nameserver
, nameserver
, strlen(nameserver
)+1);
297 memcpy(rr
->rrdata
.soa_rec
.mailbox
, mailbox
, strlen(mailbox
)+1);
299 rr
->rrdata
.soa_rec
.serial
= atoi(str_serial
);
300 rr
->rrdata
.soa_rec
.refresh
= atoi(str_refresh
);
301 rr
->rrdata
.soa_rec
.retry
= atoi(str_retry
);
302 rr
->rrdata
.soa_rec
.expire
= atoi(str_expire
);
303 rr
->rrdata
.soa_rec
.minimum
= atoi(str_minimum
);
305 memcpy(rr
->key
, key
, strlen(key
) + 1);
310 static int rwrap_create_fake_cname_rr(const char *key
,
312 struct rwrap_fake_rr
*rr
)
314 memcpy(rr
->rrdata
.cname_rec
, value
, strlen(value
) + 1);
315 memcpy(rr
->key
, key
, strlen(key
) + 1);
316 rr
->type
= ns_t_cname
;
320 /* Prepares a fake header with a single response. Advances header_blob */
321 static ssize_t
rwrap_fake_header(uint8_t **header_blob
, size_t remaining
,
322 size_t ancount
, size_t arcount
)
327 if (remaining
< NS_HFIXEDSZ
) {
328 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small!\n");
333 memset(hb
, 0, NS_HFIXEDSZ
);
336 h
->id
= res_randomid(); /* random query ID */
337 h
->qr
= 1; /* response flag */
338 h
->rd
= 1; /* recursion desired */
339 h
->ra
= 1; /* resursion available */
341 h
->qdcount
= htons(1); /* no. of questions */
342 h
->ancount
= htons(ancount
); /* no. of answers */
343 h
->arcount
= htons(arcount
); /* no. of add'tl records */
345 hb
+= NS_HFIXEDSZ
; /* move past the header */
351 static ssize_t
rwrap_fake_question(const char *question
,
353 uint8_t **question_ptr
,
356 uint8_t *qb
= *question_ptr
;
359 n
= ns_name_compress(question
, qb
, remaining
, NULL
, NULL
);
361 RWRAP_LOG(RWRAP_LOG_ERROR
,
362 "Failed to compress [%s]\n", question
);
369 if (remaining
< 2 * sizeof(uint16_t)) {
370 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small!\n");
375 NS_PUT16(ns_c_in
, qb
);
378 return n
+ 2 * sizeof(uint16_t);
381 static ssize_t
rwrap_fake_rdata_common(uint16_t type
,
387 uint8_t *rd
= *rdata_ptr
;
390 written
= ns_name_compress(key
, rd
, remaining
, NULL
, NULL
);
392 RWRAP_LOG(RWRAP_LOG_ERROR
,
393 "Failed to compress [%s]\n", key
);
397 remaining
-= written
;
399 if (remaining
< 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
400 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small\n");
405 NS_PUT16(ns_c_in
, rd
);
406 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL
, rd
);
407 NS_PUT16(rdata_size
, rd
);
409 if (remaining
< rdata_size
) {
410 RWRAP_LOG(RWRAP_LOG_ERROR
, "Buffer too small\n");
415 return written
+ 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size
;
418 static ssize_t
rwrap_fake_a(struct rwrap_fake_rr
*rr
,
422 uint8_t *a
= answer_ptr
;
425 if (rr
== NULL
|| rr
->type
!= ns_t_a
) {
426 RWRAP_LOG(RWRAP_LOG_ERROR
,
427 "Malformed record, no or wrong value!\n");
430 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding A RR");
432 resp_size
= rwrap_fake_rdata_common(ns_t_a
, sizeof(struct in_addr
), rr
->key
,
438 memcpy(a
, &rr
->rrdata
.a_rec
, sizeof(struct in_addr
));
443 static ssize_t
rwrap_fake_aaaa(struct rwrap_fake_rr
*rr
,
450 if (rr
== NULL
|| rr
->type
!= ns_t_aaaa
) {
451 RWRAP_LOG(RWRAP_LOG_ERROR
,
452 "Malformed record, no or wrong value!\n");
455 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding AAAA RR");
457 resp_size
= rwrap_fake_rdata_common(ns_t_aaaa
, sizeof(struct in6_addr
),
458 rr
->key
, anslen
, &a
);
463 memcpy(a
, &rr
->rrdata
.aaaa_rec
, sizeof(struct in6_addr
));
468 static ssize_t
rwrap_fake_srv(struct rwrap_fake_rr
*rr
,
475 unsigned char hostname_compressed
[MAXDNAME
];
476 ssize_t compressed_len
;
478 if (rr
== NULL
|| rr
->type
!= ns_t_srv
) {
479 RWRAP_LOG(RWRAP_LOG_ERROR
,
480 "Malformed record, no or wrong value!\n");
483 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding SRV RR");
484 rdata_size
= 3 * sizeof(uint16_t);
486 /* Prepare the data to write */
487 compressed_len
= ns_name_compress(rr
->rrdata
.srv_rec
.hostname
,
488 hostname_compressed
, MAXDNAME
,
490 if (compressed_len
< 0) {
493 rdata_size
+= compressed_len
;
495 resp_size
= rwrap_fake_rdata_common(ns_t_srv
, rdata_size
,
496 rr
->key
, anslen
, &a
);
501 NS_PUT16(rr
->rrdata
.srv_rec
.prio
, a
);
502 NS_PUT16(rr
->rrdata
.srv_rec
.weight
, a
);
503 NS_PUT16(rr
->rrdata
.srv_rec
.port
, a
);
504 memcpy(a
, hostname_compressed
, compressed_len
);
509 static ssize_t
rwrap_fake_soa(struct rwrap_fake_rr
*rr
,
516 unsigned char nameser_compressed
[MAXDNAME
];
517 ssize_t compressed_ns_len
;
518 unsigned char mailbox_compressed
[MAXDNAME
];
519 ssize_t compressed_mb_len
;
521 if (rr
== NULL
|| rr
->type
!= ns_t_soa
) {
522 RWRAP_LOG(RWRAP_LOG_ERROR
,
523 "Malformed record, no or wrong value!\n");
526 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding SOA RR");
527 rdata_size
= 5 * sizeof(uint16_t);
529 compressed_ns_len
= ns_name_compress(rr
->rrdata
.soa_rec
.nameserver
,
531 MAXDNAME
, NULL
, NULL
);
532 if (compressed_ns_len
< 0) {
535 rdata_size
+= compressed_ns_len
;
537 compressed_mb_len
= ns_name_compress(rr
->rrdata
.soa_rec
.mailbox
,
539 MAXDNAME
, NULL
, NULL
);
540 if (compressed_mb_len
< 0) {
543 rdata_size
+= compressed_mb_len
;
545 resp_size
= rwrap_fake_rdata_common(ns_t_soa
, rdata_size
,
546 rr
->key
, anslen
, &a
);
551 memcpy(a
, nameser_compressed
, compressed_ns_len
);
552 a
+= compressed_ns_len
;
553 memcpy(a
, mailbox_compressed
, compressed_mb_len
);
554 a
+= compressed_mb_len
;
555 NS_PUT32(rr
->rrdata
.soa_rec
.serial
, a
);
556 NS_PUT32(rr
->rrdata
.soa_rec
.refresh
, a
);
557 NS_PUT32(rr
->rrdata
.soa_rec
.retry
, a
);
558 NS_PUT32(rr
->rrdata
.soa_rec
.expire
, a
);
559 NS_PUT32(rr
->rrdata
.soa_rec
.minimum
, a
);
564 static ssize_t
rwrap_fake_cname(struct rwrap_fake_rr
*rr
,
570 unsigned char hostname_compressed
[MAXDNAME
];
573 if (rr
== NULL
|| rr
->type
!= ns_t_cname
) {
574 RWRAP_LOG(RWRAP_LOG_ERROR
,
575 "Malformed record, no or wrong value!\n");
578 RWRAP_LOG(RWRAP_LOG_TRACE
, "Adding CNAME RR");
580 /* Prepare the data to write */
581 rdata_size
= ns_name_compress(rr
->rrdata
.cname_rec
,
582 hostname_compressed
, MAXDNAME
,
584 if (rdata_size
< 0) {
588 resp_size
= rwrap_fake_rdata_common(ns_t_cname
, rdata_size
,
589 rr
->key
, anslen
, &a
);
594 memcpy(a
, hostname_compressed
, rdata_size
);
599 #define RESOLV_MATCH(line, name) \
600 (strncmp(line, name, sizeof(name) - 1) == 0 && \
601 (line[sizeof(name) - 1] == ' ' || \
602 line[sizeof(name) - 1] == '\t'))
604 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
605 ((type) == (ns_type) && \
606 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
607 (strcasecmp(key, query)) == 0)
610 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
611 const char *query
, int type
,
612 struct rwrap_fake_rr
*rr
);
614 static int rwrap_srv_recurse(const char *hostfile
, unsigned recursion
,
615 const char *query
, struct rwrap_fake_rr
*rr
)
619 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
620 if (rc
== 0) return 0;
622 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
623 if (rc
== ENOENT
) rc
= 0;
628 static int rwrap_cname_recurse(const char *hostfile
, unsigned recursion
,
629 const char *query
, struct rwrap_fake_rr
*rr
)
633 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_a
, rr
);
634 if (rc
== 0) return 0;
636 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_aaaa
, rr
);
637 if (rc
== 0) return 0;
639 rc
= rwrap_get_record(hostfile
, recursion
, query
, ns_t_cname
, rr
);
640 if (rc
== ENOENT
) rc
= 0;
645 static int rwrap_get_record(const char *hostfile
, unsigned recursion
,
646 const char *query
, int type
,
647 struct rwrap_fake_rr
*rr
)
655 if (recursion
>= RWRAP_MAX_RECURSION
) {
656 RWRAP_LOG(RWRAP_LOG_ERROR
, "Recursed too deep!\n");
660 RWRAP_LOG(RWRAP_LOG_TRACE
,
661 "Searching in fake hosts file %s\n", hostfile
);
663 fp
= fopen(hostfile
, "r");
665 RWRAP_LOG(RWRAP_LOG_ERROR
,
666 "Opening %s failed: %s",
667 hostfile
, strerror(errno
));
671 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
678 NEXT_KEY(rec_type
, key
);
679 NEXT_KEY(key
, value
);
681 if (key
== NULL
|| value
== NULL
) {
682 RWRAP_LOG(RWRAP_LOG_WARN
,
683 "Malformed line: not enough parts, use \"rec_type key data\n"
684 "For example \"A cwrap.org 10.10.10.10\"");
689 while(q
[0] != '\n' && q
[0] != '\0') {
694 if (TYPE_MATCH(type
, ns_t_a
, rec_type
, "A", key
, query
)) {
695 rc
= rwrap_create_fake_a_rr(key
, value
, rr
);
697 } else if (TYPE_MATCH(type
, ns_t_aaaa
,
698 rec_type
, "AAAA", key
, query
)) {
699 rc
= rwrap_create_fake_aaaa_rr(key
, value
, rr
);
701 } else if (TYPE_MATCH(type
, ns_t_srv
,
702 rec_type
, "SRV", key
, query
)) {
703 rc
= rwrap_create_fake_srv_rr(key
, value
, rr
);
705 rc
= rwrap_srv_recurse(hostfile
, recursion
+1,
706 rr
->rrdata
.srv_rec
.hostname
,
710 } else if (TYPE_MATCH(type
, ns_t_soa
,
711 rec_type
, "SOA", key
, query
)) {
712 rc
= rwrap_create_fake_soa_rr(key
, value
, rr
);
714 } else if (TYPE_MATCH(type
, ns_t_cname
,
715 rec_type
, "CNAME", key
, query
)) {
716 rc
= rwrap_create_fake_cname_rr(key
, value
, rr
);
718 rc
= rwrap_cname_recurse(hostfile
, recursion
+1,
722 } else if (TYPE_MATCH(type
, ns_t_a
, rec_type
, "CNAME", key
, query
)) {
723 rc
= rwrap_create_fake_cname_rr(key
, value
, rr
);
725 rc
= rwrap_cname_recurse(hostfile
, recursion
+1,
732 if (rc
== ENOENT
&& recursion
== 0) {
733 RWRAP_LOG(RWRAP_LOG_TRACE
, "Record for [%s] not found\n", query
);
734 memcpy(rr
->key
, key
, strlen(key
) + 1);
741 static ssize_t
rwrap_fake_empty(int type
,
742 const char *question
,
747 size_t remaining
= anslen
;
749 resp_data
= rwrap_fake_header(&answer
, remaining
, 0, 0);
753 remaining
-= resp_data
;
755 resp_data
+= rwrap_fake_question(question
, type
, &answer
, remaining
);
759 remaining
-= resp_data
;
761 resp_data
+= rwrap_fake_rdata_common(type
, 0, question
,
770 static inline bool rwrap_known_type(int type
)
784 static int rwrap_ancount(struct rwrap_fake_rr
*rrs
, int qtype
)
789 /* Include all RRs in the stack until the sought type
790 * in the answer section. This is the case i.e. when looking
791 * up an A record but the name points to a CNAME
793 for (i
= 0; i
< RWRAP_MAX_RECURSION
; i
++) {
796 if (rwrap_known_type(rrs
[i
].type
) &&
797 rrs
[i
].type
== qtype
) {
802 /* Return 0 records if the sought type wasn't in the stack */
803 return i
< RWRAP_MAX_RECURSION
? ancount
: 0;
806 static int rwrap_arcount(struct rwrap_fake_rr
*rrs
, int ancount
)
811 /* start from index ancount */
812 for (i
= ancount
; i
< RWRAP_MAX_RECURSION
; i
++) {
813 if (rwrap_known_type(rrs
[i
].type
)) {
821 static ssize_t
rwrap_add_rr(struct rwrap_fake_rr
*rr
,
829 resp_data
= rwrap_fake_a(rr
, answer
, anslen
);
832 resp_data
= rwrap_fake_aaaa(rr
, answer
, anslen
);
835 resp_data
= rwrap_fake_srv(rr
, answer
, anslen
);
838 resp_data
= rwrap_fake_soa(rr
, answer
, anslen
);
841 resp_data
= rwrap_fake_cname(rr
, answer
, anslen
);
850 static ssize_t
rwrap_fake_answer(struct rwrap_fake_rr
*rrs
,
858 size_t remaining
= anslen
;
863 ancount
= rwrap_ancount(rrs
, type
);
864 arcount
= rwrap_arcount(rrs
, ancount
);
865 RWRAP_LOG(RWRAP_LOG_TRACE
,
866 "Got %d answers and %d additional records\n", ancount
, arcount
);
868 resp_data
= rwrap_fake_header(&answer
, remaining
, ancount
, arcount
);
872 remaining
-= resp_data
;
874 resp_data
+= rwrap_fake_question(rrs
->key
, rrs
->type
, &answer
, remaining
);
878 remaining
-= resp_data
;
881 for (i
= 0; i
< ancount
; i
++) {
882 rrlen
= rwrap_add_rr(&rrs
[i
], answer
, remaining
);
891 /* add authoritative NS here? */
893 /* additional records */
894 for (i
= ancount
; i
< ancount
+ arcount
; i
++) {
895 rrlen
= rwrap_add_rr(&rrs
[i
], answer
, remaining
);
907 /* Reads in a file in the following format:
910 * Malformed entried are silently skipped.
911 * Allocates answer buffer of size anslen that has to be freed after use.
913 static int rwrap_res_fake_hosts(const char *hostfile
,
916 unsigned char *answer
,
920 char *query_name
= NULL
;
921 size_t qlen
= strlen(query
);
922 struct rwrap_fake_rr rrs
[RWRAP_MAX_RECURSION
];
925 RWRAP_LOG(RWRAP_LOG_TRACE
,
926 "Searching in fake hosts file %s\n", hostfile
);
928 if (qlen
> 0 && query
[qlen
-1] == '.') {
932 query_name
= strndup(query
, qlen
);
933 if (query_name
== NULL
) {
937 rwrap_fake_rr_init(rrs
, RWRAP_MAX_RECURSION
);
939 rc
= rwrap_get_record(hostfile
, 0, query_name
, type
, rrs
);
942 RWRAP_LOG(RWRAP_LOG_TRACE
,
943 "Found record for [%s]\n", query_name
);
944 resp_size
= rwrap_fake_answer(rrs
, type
, answer
, anslen
);
947 RWRAP_LOG(RWRAP_LOG_TRACE
,
948 "No record for [%s]\n", query_name
);
949 resp_size
= rwrap_fake_empty(type
, rrs
->key
, answer
, anslen
);
952 RWRAP_LOG(RWRAP_LOG_ERROR
,
953 "Error searching for [%s]\n", query_name
);
960 RWRAP_LOG(RWRAP_LOG_ERROR
,
961 "Error faking answer for [%s]\n", query_name
);
964 RWRAP_LOG(RWRAP_LOG_TRACE
,
965 "Successfully faked answer for [%s]\n",
974 /*********************************************************
975 * RWRAP LOADING LIBC FUNCTIONS
976 *********************************************************/
980 struct rwrap_libc_fns
{
981 int (*libc_res_init
)(void);
982 int (*libc___res_init
)(void);
983 int (*libc_res_ninit
)(struct __res_state
*state
);
984 int (*libc___res_ninit
)(struct __res_state
*state
);
985 void (*libc_res_nclose
)(struct __res_state
*state
);
986 void (*libc___res_nclose
)(struct __res_state
*state
);
987 void (*libc_res_close
)(void);
988 void (*libc___res_close
)(void);
989 int (*libc_res_nquery
)(struct __res_state
*state
,
993 unsigned char *answer
,
995 int (*libc___res_nquery
)(struct __res_state
*state
,
999 unsigned char *answer
,
1001 int (*libc_res_nsearch
)(struct __res_state
*state
,
1005 unsigned char *answer
,
1007 int (*libc___res_nsearch
)(struct __res_state
*state
,
1011 unsigned char *answer
,
1017 void *libresolv_handle
;
1024 struct rwrap_libc_fns fns
;
1027 static struct rwrap rwrap
;
1035 static const char *rwrap_str_lib(enum rwrap_lib lib
)
1040 case RWRAP_LIBRESOLV
:
1044 /* Compiler would warn us about unhandled enum value if we get here */
1049 static void *rwrap_load_lib_handle(enum rwrap_lib lib
)
1051 int flags
= RTLD_LAZY
;
1052 void *handle
= NULL
;
1055 #ifdef RTLD_DEEPBIND
1056 flags
|= RTLD_DEEPBIND
;
1060 case RWRAP_LIBRESOLV
:
1061 #ifdef HAVE_LIBRESOLV
1062 handle
= rwrap
.libresolv_handle
;
1063 if (handle
== NULL
) {
1064 for (i
= 10; i
>= 0; i
--) {
1065 char soname
[256] = {0};
1067 snprintf(soname
, sizeof(soname
), "libresolv.so.%d", i
);
1068 handle
= dlopen(soname
, flags
);
1069 if (handle
!= NULL
) {
1074 rwrap
.libresolv_handle
= handle
;
1080 handle
= rwrap
.libc_handle
;
1082 if (handle
== NULL
) {
1083 handle
= dlopen(LIBC_SO
, flags
);
1085 rwrap
.libc_handle
= handle
;
1088 if (handle
== NULL
) {
1089 for (i
= 10; i
>= 0; i
--) {
1090 char soname
[256] = {0};
1092 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
1093 handle
= dlopen(soname
, flags
);
1094 if (handle
!= NULL
) {
1099 rwrap
.libc_handle
= handle
;
1104 if (handle
== NULL
) {
1106 handle
= rwrap
.libc_handle
= rwrap
.libresolv_handle
= RTLD_NEXT
;
1108 RWRAP_LOG(RWRAP_LOG_ERROR
,
1109 "Failed to dlopen library: %s\n",
1118 static void *_rwrap_load_lib_function(enum rwrap_lib lib
, const char *fn_name
)
1123 handle
= rwrap_load_lib_handle(lib
);
1125 func
= dlsym(handle
, fn_name
);
1127 RWRAP_LOG(RWRAP_LOG_ERROR
,
1128 "Failed to find %s: %s\n",
1129 fn_name
, dlerror());
1133 RWRAP_LOG(RWRAP_LOG_TRACE
,
1134 "Loaded %s from %s",
1135 fn_name
, rwrap_str_lib(lib
));
1139 #define rwrap_load_lib_function(lib, fn_name) \
1140 if (rwrap.fns.libc_##fn_name == NULL) { \
1141 *(void **) (&rwrap.fns.libc_##fn_name) = \
1142 _rwrap_load_lib_function(lib, #fn_name); \
1148 * Functions especially from libc need to be loaded individually, you can't load
1149 * all at once or gdb will segfault at startup. The same applies to valgrind and
1150 * has probably something todo with with the linker.
1151 * So we need load each function at the point it is called the first time.
1154 static int libc_res_init(void)
1156 #if defined(HAVE_RES_INIT)
1157 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_init
);
1159 return rwrap
.fns
.libc_res_init();
1160 #elif defined(HAVE___RES_INIT)
1161 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_init
);
1163 return rwrap
.fns
.libc___res_init();
1168 static int libc_res_ninit(struct __res_state
*state
)
1170 #if defined(HAVE_RES_NINIT)
1172 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1173 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_ninit
);
1174 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1175 rwrap_load_lib_function(RWRAP_LIBC
, res_ninit
);
1176 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1178 return rwrap
.fns
.libc_res_ninit(state
);
1179 #elif defined(HAVE___RES_NINIT)
1180 rwrap_load_lib_function(RWRAP_LIBC
, __res_ninit
);
1182 return rwrap
.fns
.libc___res_ninit(state
);
1184 #error "No res_ninit function"
1188 static void libc_res_nclose(struct __res_state
*state
)
1190 #if defined(HAVE_RES_NCLOSE)
1192 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1193 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nclose
);
1194 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1195 rwrap_load_lib_function(RWRAP_LIBC
, res_nclose
);
1196 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1198 rwrap
.fns
.libc_res_nclose(state
);
1199 #elif defined(HAVE___RES_NCLOSE)
1200 rwrap_load_lib_function(RWRAP_LIBC
, __res_nclose
);
1202 rwrap
.fns
.libc___res_nclose(state
);
1204 #error "No res_nclose function"
1208 static int libc_res_nquery(struct __res_state
*state
,
1212 unsigned char *answer
,
1215 #if defined(HAVE_RES_NQUERY)
1216 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nquery
);
1218 return rwrap
.fns
.libc_res_nquery(state
,
1224 #elif defined(HAVE___RES_NQUERY)
1225 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nquery
);
1227 return rwrap
.fns
.libc___res_nquery(state
,
1234 #error "No res_nquery function"
1238 static int libc_res_nsearch(struct __res_state
*state
,
1242 unsigned char *answer
,
1245 #if defined(HAVE_RES_NSEARCH)
1246 rwrap_load_lib_function(RWRAP_LIBRESOLV
, res_nsearch
);
1248 return rwrap
.fns
.libc_res_nsearch(state
,
1254 #elif defined(HAVE___RES_NSEARCH)
1255 rwrap_load_lib_function(RWRAP_LIBRESOLV
, __res_nsearch
);
1257 return rwrap
.fns
.libc___res_nsearch(state
,
1264 #error "No res_nsearch function"
1268 /****************************************************************************
1270 ***************************************************************************/
1272 static int rwrap_parse_resolv_conf(struct __res_state
*state
,
1273 const char *resolv_conf
)
1279 fp
= fopen(resolv_conf
, "r");
1281 RWRAP_LOG(RWRAP_LOG_ERROR
,
1282 "Opening %s failed: %s",
1283 resolv_conf
, strerror(errno
));
1287 while(fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1290 /* Ignore comments */
1291 if (buf
[0] == '#' || buf
[0] == ';') {
1295 if (RESOLV_MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
1300 p
= buf
+ strlen("nameserver");
1302 /* Skip spaces and tabs */
1303 while(isblank((int)p
[0])) {
1308 while(q
[0] != '\n' && q
[0] != '\0') {
1313 ok
= inet_pton(AF_INET
, p
, &a
);
1315 state
->nsaddr_list
[state
->nscount
] = (struct sockaddr_in
) {
1316 .sin_family
= AF_INET
,
1318 .sin_port
= htons(53),
1325 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1328 ok
= inet_pton(AF_INET6
, p
, &a6
);
1330 struct sockaddr_in6
*sa6
;
1332 sa6
= malloc(sizeof(*sa6
));
1338 sa6
->sin6_family
= AF_INET6
;
1339 sa6
->sin6_port
= htons(53);
1340 sa6
->sin6_flowinfo
= 0;
1341 sa6
->sin6_addr
= a6
;
1343 state
->_u
._ext
.nsaddrs
[state
->_u
._ext
.nscount
] = sa6
;
1344 state
->_u
._ext
.nssocks
[state
->_u
._ext
.nscount
] = -1;
1345 state
->_u
._ext
.nsmap
[state
->_u
._ext
.nscount
] = MAXNS
+ 1;
1347 state
->_u
._ext
.nscount
++;
1350 RWRAP_LOG(RWRAP_LOG_ERROR
,
1351 "Malformed DNS server");
1354 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1356 * BSD uses an opaque structure to store the
1357 * IPv6 addresses. So we can not simply store
1358 * these addresses the same way as above.
1360 RWRAP_LOG(RWRAP_LOG_WARN
,
1361 "resolve_wrapper does not support "
1362 "IPv6 on this platform");
1367 } /* TODO: match other keywords */
1371 RWRAP_LOG(RWRAP_LOG_ERROR
,
1372 "Reading from %s failed",
1382 /****************************************************************************
1384 ***************************************************************************/
1386 static int rwrap_res_ninit(struct __res_state
*state
)
1390 rc
= libc_res_ninit(state
);
1392 const char *resolv_conf
= getenv("RESOLV_WRAPPER_CONF");
1394 if (resolv_conf
!= NULL
) {
1397 (void)i
; /* maybe unused */
1399 /* Delete name servers */
1401 memset(state
->nsaddr_list
, 0, sizeof(state
->nsaddr_list
));
1403 state
->_u
._ext
.nscount
= 0;
1404 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1405 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1406 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1410 rc
= rwrap_parse_resolv_conf(state
, resolv_conf
);
1417 #if defined(HAVE_RES_NINIT)
1418 int res_ninit(struct __res_state
*state
)
1419 #elif defined(HAVE___RES_NINIT)
1420 int __res_ninit(struct __res_state
*state
)
1423 return rwrap_res_ninit(state
);
1426 /****************************************************************************
1428 ***************************************************************************/
1430 static struct __res_state rwrap_res_state
;
1432 static int rwrap_res_init(void)
1436 rc
= rwrap_res_ninit(&rwrap_res_state
);
1441 #if defined(HAVE_RES_INIT)
1443 #elif defined(HAVE___RES_INIT)
1444 int __res_init(void)
1447 return rwrap_res_init();
1450 /****************************************************************************
1452 ***************************************************************************/
1454 static void rwrap_res_nclose(struct __res_state
*state
)
1456 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1460 libc_res_nclose(state
);
1462 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1463 if (state
!= NULL
) {
1464 for (i
= 0; i
< state
->_u
._ext
.nscount
; i
++) {
1465 SAFE_FREE(state
->_u
._ext
.nsaddrs
[i
]);
1471 #if defined(HAVE_RES_NCLOSE)
1472 void res_nclose(struct __res_state
*state
)
1473 #elif defined(HAVE___RES_NCLOSE)
1474 void __res_nclose(struct __res_state
*state
)
1477 rwrap_res_nclose(state
);
1480 /****************************************************************************
1482 ***************************************************************************/
1484 static void rwrap_res_close(void)
1486 rwrap_res_nclose(&rwrap_res_state
);
1489 #if defined(HAVE_RES_CLOSE)
1490 void res_close(void)
1491 #elif defined(HAVE___RES_CLOSE)
1492 void __res_close(void)
1498 /****************************************************************************
1500 ***************************************************************************/
1502 static int rwrap_res_nquery(struct __res_state
*state
,
1506 unsigned char *answer
,
1510 const char *fake_hosts
;
1515 RWRAP_LOG(RWRAP_LOG_TRACE
,
1516 "Resolve the domain name [%s] - class=%d, type=%d",
1517 dname
, class, type
);
1519 for (i
= 0; i
< state
->nscount
; i
++) {
1520 char ip
[INET6_ADDRSTRLEN
];
1522 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1523 RWRAP_LOG(RWRAP_LOG_TRACE
,
1529 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1530 if (fake_hosts
!= NULL
) {
1531 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1533 rc
= libc_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1537 RWRAP_LOG(RWRAP_LOG_TRACE
,
1538 "The returned response length is: %d",
1544 #if defined(HAVE_RES_NQUERY)
1545 int res_nquery(struct __res_state
*state
,
1549 unsigned char *answer
,
1551 #elif defined(HAVE___RES_NQUERY)
1552 int __res_nquery(struct __res_state
*state
,
1556 unsigned char *answer
,
1560 return rwrap_res_nquery(state
, dname
, class, type
, answer
, anslen
);
1563 /****************************************************************************
1565 ***************************************************************************/
1567 static int rwrap_res_query(const char *dname
,
1570 unsigned char *answer
,
1575 rc
= rwrap_res_ninit(&rwrap_res_state
);
1580 rc
= rwrap_res_nquery(&rwrap_res_state
,
1590 #if defined(HAVE_RES_QUERY)
1591 int res_query(const char *dname
,
1594 unsigned char *answer
,
1596 #elif defined(HAVE___RES_QUERY)
1597 int __res_query(const char *dname
,
1600 unsigned char *answer
,
1604 return rwrap_res_query(dname
, class, type
, answer
, anslen
);
1607 /****************************************************************************
1609 ***************************************************************************/
1611 static int rwrap_res_nsearch(struct __res_state
*state
,
1615 unsigned char *answer
,
1619 const char *fake_hosts
;
1624 RWRAP_LOG(RWRAP_LOG_TRACE
,
1625 "Resolve the domain name [%s] - class=%d, type=%d",
1626 dname
, class, type
);
1628 for (i
= 0; i
< state
->nscount
; i
++) {
1629 char ip
[INET6_ADDRSTRLEN
];
1631 inet_ntop(AF_INET
, &state
->nsaddr_list
[i
].sin_addr
, ip
, sizeof(ip
));
1632 RWRAP_LOG(RWRAP_LOG_TRACE
,
1638 fake_hosts
= getenv("RESOLV_WRAPPER_HOSTS");
1639 if (fake_hosts
!= NULL
) {
1640 rc
= rwrap_res_fake_hosts(fake_hosts
, dname
, type
, answer
, anslen
);
1642 rc
= libc_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1645 RWRAP_LOG(RWRAP_LOG_TRACE
,
1646 "The returned response length is: %d",
1652 #if defined(HAVE_RES_NSEARCH)
1653 int res_nsearch(struct __res_state
*state
,
1657 unsigned char *answer
,
1659 #elif defined(HAVE___RES_NSEARCH)
1660 int __res_nsearch(struct __res_state
*state
,
1664 unsigned char *answer
,
1668 return rwrap_res_nsearch(state
, dname
, class, type
, answer
, anslen
);
1671 /****************************************************************************
1673 ***************************************************************************/
1675 static int rwrap_res_search(const char *dname
,
1678 unsigned char *answer
,
1683 rc
= rwrap_res_ninit(&rwrap_res_state
);
1688 rc
= rwrap_res_nsearch(&rwrap_res_state
,
1698 #if defined(HAVE_RES_SEARCH)
1699 int res_search(const char *dname
,
1702 unsigned char *answer
,
1704 #elif defined(HAVE___RES_SEARCH)
1705 int __res_search(const char *dname
,
1708 unsigned char *answer
,
1712 return rwrap_res_search(dname
, class, type
, answer
, anslen
);