2 * Copyright (c) 2014 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014 Jakub Hrozek <jakub.hrozek@posteo.se>
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 #ifdef HAVE_ARPA_NAMESER_H
40 #include <arpa/nameser.h>
41 #endif /* HAVE_ARPA_NAMESER_H */
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
55 /* GCC has printf type attribute check. */
56 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
57 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
59 #define PRINTF_ATTRIBUTE(a,b)
60 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
62 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
63 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
65 #define DESTRUCTOR_ATTRIBUTE
66 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
68 #ifndef RWRAP_DEFAULT_FAKE_TTL
69 #define RWRAP_DEFAULT_FAKE_TTL 600
70 #endif /* RWRAP_DEFAULT_FAKE_TTL */
72 #ifndef HAVE_NS_NAME_COMPRESS
73 #define ns_name_compress dn_comp
86 # define RWRAP_LOG(...)
89 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
90 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
92 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
94 const char *format, ...)
102 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
107 va_start(va, format);
108 vsnprintf(buffer, sizeof(buffer), format, va);
113 case RWRAP_LOG_ERROR:
115 "RWRAP_ERROR(%d) - %s: %s\n",
120 "RWRAP_WARN(%d) - %s: %s\n",
123 case RWRAP_LOG_DEBUG:
125 "RWRAP_DEBUG(%d) - %s: %s\n",
128 case RWRAP_LOG_TRACE:
130 "RWRAP_TRACE(%d) - %s: %s\n",
136 #endif /* NDEBUG RWRAP_LOG */
139 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
142 #define NEXT_KEY(buf, key) do { \
143 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
144 if ((key) != NULL) { \
148 while ((key) != NULL \
149 && (isblank((int)(key)[0]))) { \
154 #define RWRAP_MAX_RECURSION 5
156 /* Priority and weight can be omitted from the hosts file, but need to be part
159 #define DFL_SRV_PRIO 1
160 #define DFL_SRV_WEIGHT 100
161 #define DFL_URI_PRIO 1
162 #define DFL_URI_WEIGHT 100
164 struct rwrap_srv_rrdata {
168 char hostname[MAXDNAME];
171 struct rwrap_uri_rrdata {
177 struct rwrap_soa_rrdata {
183 char nameserver[MAXDNAME];
184 char mailbox[MAXDNAME];
187 struct rwrap_fake_rr {
189 struct in_addr a_rec;
190 struct in6_addr aaaa_rec;
191 struct rwrap_srv_rrdata srv_rec;
192 struct rwrap_uri_rrdata uri_rec;
193 struct rwrap_soa_rrdata soa_rec;
194 char cname_rec[MAXDNAME];
198 int type; /* ns_t_* */
201 static void rwrap_fake_rr_init(struct rwrap_fake_rr *rr, size_t len)
205 for (i = 0; i < len; i++) {
206 rr[i].type = ns_t_invalid;
210 static int rwrap_create_fake_a_rr(const char *key,
212 struct rwrap_fake_rr *rr)
216 ok = inet_pton(AF_INET, value, &rr->rrdata.a_rec);
218 RWRAP_LOG(RWRAP_LOG_ERROR,
219 "Failed to convert [%s] to binary\n", value);
223 memcpy(rr->key, key, strlen(key) + 1);
228 static int rwrap_create_fake_aaaa_rr(const char *key,
230 struct rwrap_fake_rr *rr)
234 ok = inet_pton(AF_INET6, value, &rr->rrdata.aaaa_rec);
236 RWRAP_LOG(RWRAP_LOG_ERROR,
237 "Failed to convert [%s] to binary\n", value);
241 memcpy(rr->key, key, strlen(key) + 1);
242 rr->type = ns_t_aaaa;
245 static int rwrap_create_fake_ns_rr(const char *key,
247 struct rwrap_fake_rr *rr)
249 memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1);
250 memcpy(rr->key, key, strlen(key) + 1);
255 static int rwrap_create_fake_srv_rr(const char *key,
257 struct rwrap_fake_rr *rr)
262 const char *hostname;
264 /* parse the value into priority, weight, port and hostname
265 * and check the validity */
267 NEXT_KEY(hostname, str_port);
268 NEXT_KEY(str_port, str_prio);
269 NEXT_KEY(str_prio, str_weight);
270 if (str_port == NULL || hostname == NULL) {
271 RWRAP_LOG(RWRAP_LOG_ERROR,
272 "Malformed SRV entry [%s]\n", value);
277 rr->rrdata.srv_rec.prio = atoi(str_prio);
279 rr->rrdata.srv_rec.prio = DFL_SRV_PRIO;
282 rr->rrdata.srv_rec.weight = atoi(str_weight);
284 rr->rrdata.srv_rec.weight = DFL_SRV_WEIGHT;
286 rr->rrdata.srv_rec.port = atoi(str_port);
287 memcpy(rr->rrdata.srv_rec.hostname , hostname, strlen(hostname) + 1);
289 memcpy(rr->key, key, strlen(key) + 1);
294 static int rwrap_create_fake_uri_rr(const char *key,
296 struct rwrap_fake_rr *rr)
302 /* parse the value into priority, weight, and uri
303 * and check the validity */
305 NEXT_KEY(uri, str_prio);
306 NEXT_KEY(str_prio, str_weight);
308 RWRAP_LOG(RWRAP_LOG_ERROR,
309 "Malformed URI entry [%s]\n", value);
314 rr->rrdata.uri_rec.prio = atoi(str_prio);
316 rr->rrdata.uri_rec.prio = DFL_URI_PRIO;
319 rr->rrdata.uri_rec.weight = atoi(str_weight);
321 rr->rrdata.uri_rec.weight = DFL_URI_WEIGHT;
323 memcpy(rr->rrdata.uri_rec.uri, uri, strlen(uri) + 1);
325 memcpy(rr->key, key, strlen(key) + 1);
330 static int rwrap_create_fake_soa_rr(const char *key,
332 struct rwrap_fake_rr *rr)
334 const char *nameserver;
342 /* parse the value into nameserver, mailbox, serial, refresh,
343 * retry, expire, minimum and check the validity
346 NEXT_KEY(nameserver, mailbox);
347 NEXT_KEY(mailbox, str_serial);
348 NEXT_KEY(str_serial, str_refresh);
349 NEXT_KEY(str_refresh, str_retry);
350 NEXT_KEY(str_retry, str_expire);
351 NEXT_KEY(str_expire, str_minimum);
352 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
353 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
354 str_minimum == NULL) {
355 RWRAP_LOG(RWRAP_LOG_ERROR,
356 "Malformed SOA entry [%s]\n", value);
360 memcpy(rr->rrdata.soa_rec.nameserver, nameserver, strlen(nameserver)+1);
361 memcpy(rr->rrdata.soa_rec.mailbox, mailbox, strlen(mailbox)+1);
363 rr->rrdata.soa_rec.serial = atoi(str_serial);
364 rr->rrdata.soa_rec.refresh = atoi(str_refresh);
365 rr->rrdata.soa_rec.retry = atoi(str_retry);
366 rr->rrdata.soa_rec.expire = atoi(str_expire);
367 rr->rrdata.soa_rec.minimum = atoi(str_minimum);
369 memcpy(rr->key, key, strlen(key) + 1);
374 static int rwrap_create_fake_cname_rr(const char *key,
376 struct rwrap_fake_rr *rr)
378 memcpy(rr->rrdata.cname_rec , value, strlen(value) + 1);
379 memcpy(rr->key, key, strlen(key) + 1);
380 rr->type = ns_t_cname;
384 /* Prepares a fake header with a single response. Advances header_blob */
385 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
386 size_t ancount, size_t arcount)
391 if (remaining < NS_HFIXEDSZ) {
392 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
397 memset(hb, 0, NS_HFIXEDSZ);
400 h->id = res_randomid(); /* random query ID */
401 h->qr = 1; /* response flag */
402 h->rd = 1; /* recursion desired */
403 h->ra = 1; /* recursion available */
405 h->qdcount = htons(1); /* no. of questions */
406 h->ancount = htons(ancount); /* no. of answers */
407 h->arcount = htons(arcount); /* no. of add'tl records */
409 hb += NS_HFIXEDSZ; /* move past the header */
415 static ssize_t rwrap_fake_question(const char *question,
417 uint8_t **question_ptr,
420 uint8_t *qb = *question_ptr;
423 n = ns_name_compress(question, qb, remaining, NULL, NULL);
425 RWRAP_LOG(RWRAP_LOG_ERROR,
426 "Failed to compress [%s]\n", question);
433 if (remaining < 2 * sizeof(uint16_t)) {
434 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
439 NS_PUT16(ns_c_in, qb);
442 return n + 2 * sizeof(uint16_t);
445 static ssize_t rwrap_fake_rdata_common(uint16_t type,
451 uint8_t *rd = *rdata_ptr;
454 written = ns_name_compress(key, rd, remaining, NULL, NULL);
456 RWRAP_LOG(RWRAP_LOG_ERROR,
457 "Failed to compress [%s]\n", key);
461 remaining -= written;
463 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
464 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
469 NS_PUT16(ns_c_in, rd);
470 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
471 NS_PUT16(rdata_size, rd);
473 if (remaining < rdata_size) {
474 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
479 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
482 static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
486 uint8_t *a = answer_ptr;
489 if (rr == NULL || rr->type != ns_t_a) {
490 RWRAP_LOG(RWRAP_LOG_ERROR,
491 "Malformed record, no or wrong value!\n");
494 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
496 resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
502 memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
507 static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
514 if (rr == NULL || rr->type != ns_t_aaaa) {
515 RWRAP_LOG(RWRAP_LOG_ERROR,
516 "Malformed record, no or wrong value!\n");
519 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
521 resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
522 rr->key, anslen, &a);
527 memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
532 static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr,
537 ssize_t resp_size = 0;
539 unsigned char hostname_compressed[MAXDNAME];
540 ssize_t compressed_len;
542 if (rr == NULL || rr->type != ns_t_ns) {
543 RWRAP_LOG(RWRAP_LOG_ERROR,
544 "Malformed record, no or wrong value!\n");
547 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR");
549 /* Prepare the data to write */
550 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
555 if (compressed_len < 0) {
559 /* Is this enough? */
560 rdata_size = compressed_len;
562 resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size,
563 rr->key, anslen, &a);
568 memcpy(a, hostname_compressed, compressed_len);
573 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
580 unsigned char hostname_compressed[MAXDNAME];
581 ssize_t compressed_len;
583 if (rr == NULL || rr->type != ns_t_srv) {
584 RWRAP_LOG(RWRAP_LOG_ERROR,
585 "Malformed record, no or wrong value!\n");
588 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
589 rdata_size = 3 * sizeof(uint16_t);
591 /* Prepare the data to write */
592 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
593 hostname_compressed, MAXDNAME,
595 if (compressed_len < 0) {
598 rdata_size += compressed_len;
600 resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
601 rr->key, anslen, &a);
606 NS_PUT16(rr->rrdata.srv_rec.prio, a);
607 NS_PUT16(rr->rrdata.srv_rec.weight, a);
608 NS_PUT16(rr->rrdata.srv_rec.port, a);
609 memcpy(a, hostname_compressed, compressed_len);
614 static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
621 unsigned char uri_compressed[MAXDNAME];
622 ssize_t compressed_len;
624 if (rr == NULL || rr->type != ns_t_uri) {
625 RWRAP_LOG(RWRAP_LOG_ERROR,
626 "Malformed record, no or wrong value!\n");
629 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR");
630 rdata_size = 3 * sizeof(uint16_t);
632 /* Prepare the data to write */
633 compressed_len = ns_name_compress(rr->rrdata.uri_rec.uri,
634 uri_compressed, MAXDNAME,
636 if (compressed_len < 0) {
639 rdata_size += compressed_len;
641 resp_size = rwrap_fake_rdata_common(ns_t_uri, rdata_size,
642 rr->key, anslen, &a);
647 NS_PUT16(rr->rrdata.uri_rec.prio, a);
648 NS_PUT16(rr->rrdata.uri_rec.weight, a);
649 memcpy(a, uri_compressed, compressed_len);
654 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
661 unsigned char nameser_compressed[MAXDNAME];
662 ssize_t compressed_ns_len;
663 unsigned char mailbox_compressed[MAXDNAME];
664 ssize_t compressed_mb_len;
666 if (rr == NULL || rr->type != ns_t_soa) {
667 RWRAP_LOG(RWRAP_LOG_ERROR,
668 "Malformed record, no or wrong value!\n");
671 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
672 rdata_size = 5 * sizeof(uint16_t);
674 compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
676 MAXDNAME, NULL, NULL);
677 if (compressed_ns_len < 0) {
680 rdata_size += compressed_ns_len;
682 compressed_mb_len = ns_name_compress(rr->rrdata.soa_rec.mailbox,
684 MAXDNAME, NULL, NULL);
685 if (compressed_mb_len < 0) {
688 rdata_size += compressed_mb_len;
690 resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
691 rr->key, anslen, &a);
696 memcpy(a, nameser_compressed, compressed_ns_len);
697 a += compressed_ns_len;
698 memcpy(a, mailbox_compressed, compressed_mb_len);
699 a += compressed_mb_len;
700 NS_PUT32(rr->rrdata.soa_rec.serial, a);
701 NS_PUT32(rr->rrdata.soa_rec.refresh, a);
702 NS_PUT32(rr->rrdata.soa_rec.retry, a);
703 NS_PUT32(rr->rrdata.soa_rec.expire, a);
704 NS_PUT32(rr->rrdata.soa_rec.minimum, a);
709 static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
715 unsigned char hostname_compressed[MAXDNAME];
718 if (rr == NULL || rr->type != ns_t_cname) {
719 RWRAP_LOG(RWRAP_LOG_ERROR,
720 "Malformed record, no or wrong value!\n");
723 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
725 /* Prepare the data to write */
726 rdata_size = ns_name_compress(rr->rrdata.cname_rec,
727 hostname_compressed, MAXDNAME,
729 if (rdata_size < 0) {
733 resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
734 rr->key, anslen, &a);
739 memcpy(a, hostname_compressed, rdata_size);
744 #define RESOLV_MATCH(line, name) \
745 (strncmp(line, name, sizeof(name) - 1) == 0 && \
746 (line[sizeof(name) - 1] == ' ' || \
747 line[sizeof(name) - 1] == '\t'))
749 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
750 ((type) == (ns_type) && \
751 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
752 (strcasecmp(key, query)) == 0)
755 static int rwrap_get_record(const char *hostfile, unsigned recursion,
756 const char *query, int type,
757 struct rwrap_fake_rr *rr);
759 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
760 const char *query, struct rwrap_fake_rr *rr)
764 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
765 if (rc == 0) return 0;
767 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
768 if (rc == ENOENT) rc = 0;
773 static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
774 const char *query, struct rwrap_fake_rr *rr)
778 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
779 if (rc == 0) return 0;
781 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
782 if (rc == 0) return 0;
784 rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
785 if (rc == ENOENT) rc = 0;
790 static int rwrap_get_record(const char *hostfile, unsigned recursion,
791 const char *query, int type,
792 struct rwrap_fake_rr *rr)
800 if (recursion >= RWRAP_MAX_RECURSION) {
801 RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
805 RWRAP_LOG(RWRAP_LOG_TRACE,
806 "Searching in fake hosts file %s for %s:%d\n", hostfile,
809 fp = fopen(hostfile, "r");
811 RWRAP_LOG(RWRAP_LOG_ERROR,
812 "Opening %s failed: %s",
813 hostfile, strerror(errno));
817 while (fgets(buf, sizeof(buf), fp) != NULL) {
824 NEXT_KEY(rec_type, key);
825 NEXT_KEY(key, value);
827 if (key == NULL || value == NULL) {
828 RWRAP_LOG(RWRAP_LOG_WARN,
829 "Malformed line: not enough parts, use \"rec_type key data\n"
830 "For example \"A cwrap.org 10.10.10.10\"");
835 while(q[0] != '\n' && q[0] != '\0') {
840 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
841 rc = rwrap_create_fake_a_rr(key, value, rr);
843 } else if (TYPE_MATCH(type, ns_t_aaaa,
844 rec_type, "AAAA", key, query)) {
845 rc = rwrap_create_fake_aaaa_rr(key, value, rr);
847 } else if (TYPE_MATCH(type, ns_t_ns,
848 rec_type, "NS", key, query)) {
849 rc = rwrap_create_fake_ns_rr(key, value, rr);
851 } else if (TYPE_MATCH(type, ns_t_srv,
852 rec_type, "SRV", key, query)) {
853 rc = rwrap_create_fake_srv_rr(key, value, rr);
855 rc = rwrap_srv_recurse(hostfile, recursion+1,
856 rr->rrdata.srv_rec.hostname,
860 } else if (TYPE_MATCH(type, ns_t_uri,
861 rec_type, "URI", key, query)) {
862 rc = rwrap_create_fake_uri_rr(key, value, rr);
864 } else if (TYPE_MATCH(type, ns_t_soa,
865 rec_type, "SOA", key, query)) {
866 rc = rwrap_create_fake_soa_rr(key, value, rr);
868 } else if (TYPE_MATCH(type, ns_t_cname,
869 rec_type, "CNAME", key, query)) {
870 rc = rwrap_create_fake_cname_rr(key, value, rr);
872 rc = rwrap_cname_recurse(hostfile, recursion+1,
876 } else if (TYPE_MATCH(type, ns_t_a, rec_type, "CNAME", key, query)) {
877 rc = rwrap_create_fake_cname_rr(key, value, rr);
879 rc = rwrap_cname_recurse(hostfile, recursion+1,
886 if (rc == ENOENT && recursion == 0 && key != NULL) {
887 RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
888 memcpy(rr->key, key, strlen(key) + 1);
895 static ssize_t rwrap_fake_empty(int type,
896 const char *question,
901 size_t remaining = anslen;
903 resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
907 remaining -= resp_data;
909 resp_data += rwrap_fake_question(question, type, &answer, remaining);
913 remaining -= resp_data;
915 resp_data += rwrap_fake_rdata_common(type, 0, question,
924 static inline bool rwrap_known_type(int type)
940 static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
945 /* Include all RRs in the stack until the sought type
946 * in the answer section. This is the case i.e. when looking
947 * up an A record but the name points to a CNAME
949 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
952 if (rwrap_known_type(rrs[i].type) &&
953 rrs[i].type == qtype) {
958 /* Return 0 records if the sought type wasn't in the stack */
959 return i < RWRAP_MAX_RECURSION ? ancount : 0;
962 static int rwrap_arcount(struct rwrap_fake_rr *rrs, int ancount)
967 /* start from index ancount */
968 for (i = ancount; i < RWRAP_MAX_RECURSION; i++) {
969 if (rwrap_known_type(rrs[i].type)) {
977 static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
985 resp_data = rwrap_fake_a(rr, answer, anslen);
988 resp_data = rwrap_fake_aaaa(rr, answer, anslen);
991 resp_data = rwrap_fake_ns(rr, answer, anslen);
994 resp_data = rwrap_fake_srv(rr, answer, anslen);
997 resp_data = rwrap_fake_uri(rr, answer, anslen);
1000 resp_data = rwrap_fake_soa(rr, answer, anslen);
1003 resp_data = rwrap_fake_cname(rr, answer, anslen);
1012 static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
1020 size_t remaining = anslen;
1025 ancount = rwrap_ancount(rrs, type);
1026 arcount = rwrap_arcount(rrs, ancount);
1027 RWRAP_LOG(RWRAP_LOG_TRACE,
1028 "Got %d answers and %d additional records\n", ancount, arcount);
1030 resp_data = rwrap_fake_header(&answer, remaining, ancount, arcount);
1031 if (resp_data < 0) {
1034 remaining -= resp_data;
1036 resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
1037 if (resp_data < 0) {
1040 remaining -= resp_data;
1043 for (i = 0; i < ancount; i++) {
1044 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1053 /* add authoritative NS here? */
1055 /* additional records */
1056 for (i = ancount; i < ancount + arcount; i++) {
1057 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1069 /* Reads in a file in the following format:
1072 * Malformed entries are silently skipped.
1073 * Allocates answer buffer of size anslen that has to be freed after use.
1075 static int rwrap_res_fake_hosts(const char *hostfile,
1078 unsigned char *answer,
1082 char *query_name = NULL;
1083 size_t qlen = strlen(query);
1084 struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
1087 RWRAP_LOG(RWRAP_LOG_TRACE,
1088 "Searching in fake hosts file %s\n", hostfile);
1090 if (qlen > 0 && query[qlen-1] == '.') {
1094 query_name = strndup(query, qlen);
1095 if (query_name == NULL) {
1099 rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
1101 rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
1104 RWRAP_LOG(RWRAP_LOG_TRACE,
1105 "Found record for [%s]\n", query_name);
1106 resp_size = rwrap_fake_answer(rrs, type, answer, anslen);
1109 RWRAP_LOG(RWRAP_LOG_TRACE,
1110 "No record for [%s]\n", query_name);
1111 resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
1114 RWRAP_LOG(RWRAP_LOG_ERROR,
1115 "Error searching for [%s]\n", query_name);
1120 switch (resp_size) {
1122 RWRAP_LOG(RWRAP_LOG_ERROR,
1123 "Error faking answer for [%s]\n", query_name);
1126 RWRAP_LOG(RWRAP_LOG_TRACE,
1127 "Successfully faked answer for [%s]\n",
1136 /*********************************************************
1137 * RWRAP LOADING LIBC FUNCTIONS
1138 *********************************************************/
1142 typedef int (*__libc_res_ninit)(struct __res_state *state);
1143 typedef int (*__libc___res_ninit)(struct __res_state *state);
1144 typedef void (*__libc_res_nclose)(struct __res_state *state);
1145 typedef void (*__libc___res_nclose)(struct __res_state *state);
1146 typedef int (*__libc_res_nquery)(struct __res_state *state,
1150 unsigned char *answer,
1152 typedef int (*__libc___res_nquery)(struct __res_state *state,
1156 unsigned char *answer,
1158 typedef int (*__libc_res_nsearch)(struct __res_state *state,
1162 unsigned char *answer,
1164 typedef int (*__libc___res_nsearch)(struct __res_state *state,
1168 unsigned char *answer,
1171 #define RWRAP_SYMBOL_ENTRY(i) \
1177 struct rwrap_libc_symbols {
1178 RWRAP_SYMBOL_ENTRY(res_ninit);
1179 RWRAP_SYMBOL_ENTRY(__res_ninit);
1180 RWRAP_SYMBOL_ENTRY(res_nclose);
1181 RWRAP_SYMBOL_ENTRY(__res_nclose);
1182 RWRAP_SYMBOL_ENTRY(res_nquery);
1183 RWRAP_SYMBOL_ENTRY(__res_nquery);
1184 RWRAP_SYMBOL_ENTRY(res_nsearch);
1185 RWRAP_SYMBOL_ENTRY(__res_nsearch);
1187 #undef RWRAP_SYMBOL_ENTRY
1192 struct rwrap_libc_symbols symbols;
1197 struct rwrap_libc_symbols symbols;
1206 static struct rwrap rwrap;
1214 static const char *rwrap_str_lib(enum rwrap_lib lib)
1219 case RWRAP_LIBRESOLV:
1223 /* Compiler would warn us about unhandled enum value if we get here */
1228 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
1230 int flags = RTLD_LAZY;
1231 void *handle = NULL;
1234 #ifdef RTLD_DEEPBIND
1235 flags |= RTLD_DEEPBIND;
1239 case RWRAP_LIBRESOLV:
1240 #ifdef HAVE_LIBRESOLV
1241 handle = rwrap.libresolv.handle;
1242 if (handle == NULL) {
1243 for (i = 10; i >= 0; i--) {
1244 char soname[256] = {0};
1246 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
1247 handle = dlopen(soname, flags);
1248 if (handle != NULL) {
1253 rwrap.libresolv.handle = handle;
1259 handle = rwrap.libc.handle;
1261 if (handle == NULL) {
1262 handle = dlopen(LIBC_SO, flags);
1264 rwrap.libc.handle = handle;
1267 if (handle == NULL) {
1268 for (i = 10; i >= 0; i--) {
1269 char soname[256] = {0};
1271 snprintf(soname, sizeof(soname), "libc.so.%d", i);
1272 handle = dlopen(soname, flags);
1273 if (handle != NULL) {
1278 rwrap.libc.handle = handle;
1283 if (handle == NULL) {
1285 handle = rwrap.libc.handle = rwrap.libresolv.handle = RTLD_NEXT;
1287 RWRAP_LOG(RWRAP_LOG_ERROR,
1288 "Failed to dlopen library: %s\n",
1297 static void *_rwrap_bind_symbol(enum rwrap_lib lib, const char *fn_name)
1302 handle = rwrap_load_lib_handle(lib);
1304 func = dlsym(handle, fn_name);
1306 RWRAP_LOG(RWRAP_LOG_ERROR,
1307 "Failed to find %s: %s\n",
1308 fn_name, dlerror());
1312 RWRAP_LOG(RWRAP_LOG_TRACE,
1313 "Loaded %s from %s",
1314 fn_name, rwrap_str_lib(lib));
1318 #define rwrap_bind_symbol_libc(sym_name) \
1319 if (rwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
1320 rwrap.libc.symbols._libc_##sym_name.obj = \
1321 _rwrap_bind_symbol(RWRAP_LIBC, #sym_name); \
1324 #define rwrap_bind_symbol_libresolv(sym_name) \
1325 if (rwrap.libresolv.symbols._libc_##sym_name.obj == NULL) { \
1326 rwrap.libresolv.symbols._libc_##sym_name.obj = \
1327 _rwrap_bind_symbol(RWRAP_LIBRESOLV, #sym_name); \
1333 * Functions especially from libc need to be loaded individually, you can't load
1334 * all at once or gdb will segfault at startup. The same applies to valgrind and
1335 * has probably something todo with with the linker.
1336 * So we need load each function at the point it is called the first time.
1339 static int libc_res_ninit(struct __res_state *state)
1341 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1343 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1344 rwrap_bind_symbol_libresolv(res_ninit);
1346 return rwrap.libresolv.symbols._libc_res_ninit.f(state);
1347 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1348 rwrap_bind_symbol_libc(res_ninit);
1350 return rwrap.libc.symbols._libc_res_ninit.f(state);
1351 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1353 #elif defined(HAVE___RES_NINIT)
1354 rwrap_bind_symbol_libc(__res_ninit);
1356 return rwrap.libc.symbols._libc___res_ninit.f(state);
1358 #error "No res_ninit function"
1362 static void libc_res_nclose(struct __res_state *state)
1364 #if !defined(res_close) && defined(HAVE_RES_NCLOSE)
1366 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1367 rwrap_bind_symbol_libresolv(res_nclose);
1369 rwrap.libresolv.symbols._libc_res_nclose.f(state);
1371 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1372 rwrap_bind_symbol_libc(res_nclose);
1374 rwrap.libc.symbols._libc_res_nclose.f(state);
1376 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1378 #elif defined(HAVE___RES_NCLOSE)
1379 rwrap_bind_symbol_libc(__res_nclose);
1381 rwrap.libc.symbols._libc___res_nclose.f(state);
1383 #error "No res_nclose function"
1387 static int libc_res_nquery(struct __res_state *state,
1391 unsigned char *answer,
1394 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1395 rwrap_bind_symbol_libresolv(res_nquery);
1397 return rwrap.libresolv.symbols._libc_res_nquery.f(state,
1403 #elif defined(HAVE___RES_NQUERY)
1404 rwrap_bind_symbol_libresolv(__res_nquery);
1406 return rwrap.libresolv.symbols._libc___res_nquery.f(state,
1413 #error "No res_nquery function"
1417 static int libc_res_nsearch(struct __res_state *state,
1421 unsigned char *answer,
1424 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1425 rwrap_bind_symbol_libresolv(res_nsearch);
1427 return rwrap.libresolv.symbols._libc_res_nsearch.f(state,
1433 #elif defined(HAVE___RES_NSEARCH)
1434 rwrap_bind_symbol_libresolv(__res_nsearch);
1436 return rwrap.libresolv.symbols._libc___res_nsearch.f(state,
1443 #error "No res_nsearch function"
1447 /****************************************************************************
1449 ***************************************************************************/
1451 static int rwrap_parse_resolv_conf(struct __res_state *state,
1452 const char *resolv_conf)
1458 fp = fopen(resolv_conf, "r");
1460 RWRAP_LOG(RWRAP_LOG_ERROR,
1461 "Opening %s failed: %s",
1462 resolv_conf, strerror(errno));
1466 while(fgets(buf, sizeof(buf), fp) != NULL) {
1469 /* Ignore comments */
1470 if (buf[0] == '#' || buf[0] == ';') {
1474 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
1479 p = buf + strlen("nameserver");
1481 /* Skip spaces and tabs */
1482 while(isblank((int)p[0])) {
1487 while(q[0] != '\n' && q[0] != '\0') {
1492 ok = inet_pton(AF_INET, p, &a);
1494 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
1495 .sin_family = AF_INET,
1497 .sin_port = htons(53),
1504 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1507 ok = inet_pton(AF_INET6, p, &a6);
1509 struct sockaddr_in6 *sa6;
1511 sa6 = malloc(sizeof(*sa6));
1517 sa6->sin6_family = AF_INET6;
1518 sa6->sin6_port = htons(53);
1519 sa6->sin6_flowinfo = 0;
1520 sa6->sin6_addr = a6;
1522 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
1523 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
1524 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1526 state->_u._ext.nscount++;
1529 RWRAP_LOG(RWRAP_LOG_ERROR,
1530 "Malformed DNS server");
1533 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1535 * BSD uses an opaque structure to store the
1536 * IPv6 addresses. So we can not simply store
1537 * these addresses the same way as above.
1539 RWRAP_LOG(RWRAP_LOG_WARN,
1540 "resolve_wrapper does not support "
1541 "IPv6 on this platform");
1546 } /* TODO: match other keywords */
1550 RWRAP_LOG(RWRAP_LOG_ERROR,
1551 "Reading from %s failed",
1561 /****************************************************************************
1563 ***************************************************************************/
1565 static int rwrap_res_ninit(struct __res_state *state)
1569 rc = libc_res_ninit(state);
1571 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1573 if (resolv_conf != NULL) {
1576 (void)i; /* maybe unused */
1578 /* Delete name servers */
1580 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1582 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1583 state->_u._ext.nscount = 0;
1584 for (i = 0; i < state->_u._ext.nscount; i++) {
1585 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1589 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1596 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1597 int res_ninit(struct __res_state *state)
1598 #elif defined(HAVE___RES_NINIT)
1599 int __res_ninit(struct __res_state *state)
1602 return rwrap_res_ninit(state);
1605 /****************************************************************************
1607 ***************************************************************************/
1609 static struct __res_state rwrap_res_state;
1611 static int rwrap_res_init(void)
1615 rc = rwrap_res_ninit(&rwrap_res_state);
1620 #if !defined(res_ninit) && defined(HAVE_RES_INIT)
1622 #elif defined(HAVE___RES_INIT)
1623 int __res_init(void)
1626 return rwrap_res_init();
1629 /****************************************************************************
1631 ***************************************************************************/
1633 static void rwrap_res_nclose(struct __res_state *state)
1635 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1639 libc_res_nclose(state);
1641 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1642 if (state != NULL) {
1643 for (i = 0; i < state->_u._ext.nscount; i++) {
1644 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1650 #if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)
1651 void res_nclose(struct __res_state *state)
1652 #elif defined(HAVE___RES_NCLOSE)
1653 void __res_nclose(struct __res_state *state)
1656 rwrap_res_nclose(state);
1659 /****************************************************************************
1661 ***************************************************************************/
1663 static void rwrap_res_close(void)
1665 rwrap_res_nclose(&rwrap_res_state);
1668 #if defined(HAVE_RES_CLOSE)
1669 void res_close(void)
1670 #elif defined(HAVE___RES_CLOSE)
1671 void __res_close(void)
1677 /****************************************************************************
1679 ***************************************************************************/
1681 static int rwrap_res_nquery(struct __res_state *state,
1685 unsigned char *answer,
1689 const char *fake_hosts;
1694 RWRAP_LOG(RWRAP_LOG_TRACE,
1695 "Resolve the domain name [%s] - class=%d, type=%d",
1696 dname, class, type);
1698 for (i = 0; i < state->nscount; i++) {
1699 char ip[INET6_ADDRSTRLEN];
1701 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1702 RWRAP_LOG(RWRAP_LOG_TRACE,
1708 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1709 if (fake_hosts != NULL) {
1710 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1712 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1716 RWRAP_LOG(RWRAP_LOG_TRACE,
1717 "The returned response length is: %d",
1723 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1724 int res_nquery(struct __res_state *state,
1728 unsigned char *answer,
1730 #elif defined(HAVE___RES_NQUERY)
1731 int __res_nquery(struct __res_state *state,
1735 unsigned char *answer,
1739 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1742 /****************************************************************************
1744 ***************************************************************************/
1746 static int rwrap_res_query(const char *dname,
1749 unsigned char *answer,
1754 rc = rwrap_res_ninit(&rwrap_res_state);
1759 rc = rwrap_res_nquery(&rwrap_res_state,
1769 #if !defined(res_query) && defined(HAVE_RES_QUERY)
1770 int res_query(const char *dname,
1773 unsigned char *answer,
1775 #elif defined(HAVE___RES_QUERY)
1776 int __res_query(const char *dname,
1779 unsigned char *answer,
1783 return rwrap_res_query(dname, class, type, answer, anslen);
1786 /****************************************************************************
1788 ***************************************************************************/
1790 static int rwrap_res_nsearch(struct __res_state *state,
1794 unsigned char *answer,
1798 const char *fake_hosts;
1803 RWRAP_LOG(RWRAP_LOG_TRACE,
1804 "Resolve the domain name [%s] - class=%d, type=%d",
1805 dname, class, type);
1807 for (i = 0; i < state->nscount; i++) {
1808 char ip[INET6_ADDRSTRLEN];
1810 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1811 RWRAP_LOG(RWRAP_LOG_TRACE,
1817 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1818 if (fake_hosts != NULL) {
1819 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1821 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1824 RWRAP_LOG(RWRAP_LOG_TRACE,
1825 "The returned response length is: %d",
1831 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1832 int res_nsearch(struct __res_state *state,
1836 unsigned char *answer,
1838 #elif defined(HAVE___RES_NSEARCH)
1839 int __res_nsearch(struct __res_state *state,
1843 unsigned char *answer,
1847 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1850 /****************************************************************************
1852 ***************************************************************************/
1854 static int rwrap_res_search(const char *dname,
1857 unsigned char *answer,
1862 rc = rwrap_res_ninit(&rwrap_res_state);
1867 rc = rwrap_res_nsearch(&rwrap_res_state,
1877 #if !defined(res_search) && defined(HAVE_RES_SEARCH)
1878 int res_search(const char *dname,
1881 unsigned char *answer,
1883 #elif defined(HAVE___RES_SEARCH)
1884 int __res_search(const char *dname,
1887 unsigned char *answer,
1891 return rwrap_res_search(dname, class, type, answer, anslen);