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
85 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
86 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
88 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
90 const char *format, ...)
98 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
103 va_start(va, format);
104 vsnprintf(buffer, sizeof(buffer), format, va);
109 case RWRAP_LOG_ERROR:
111 "RWRAP_ERROR(%d) - %s: %s\n",
116 "RWRAP_WARN(%d) - %s: %s\n",
119 case RWRAP_LOG_DEBUG:
121 "RWRAP_DEBUG(%d) - %s: %s\n",
124 case RWRAP_LOG_TRACE:
126 "RWRAP_TRACE(%d) - %s: %s\n",
134 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
137 #define NEXT_KEY(buf, key) do { \
138 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
139 if ((key) != NULL) { \
143 while ((key) != NULL \
144 && (isblank((int)(key)[0]))) { \
149 #define RWRAP_MAX_RECURSION 64
151 /* Priority and weight can be omitted from the hosts file, but need to be part
154 #define DFL_SRV_PRIO 1
155 #define DFL_SRV_WEIGHT 100
156 #define DFL_URI_PRIO 1
157 #define DFL_URI_WEIGHT 100
159 struct rwrap_srv_rrdata {
163 char hostname[MAXDNAME];
166 struct rwrap_uri_rrdata {
172 struct rwrap_soa_rrdata {
178 char nameserver[MAXDNAME];
179 char mailbox[MAXDNAME];
182 struct rwrap_fake_rr {
184 struct in_addr a_rec;
185 struct in6_addr aaaa_rec;
186 struct rwrap_srv_rrdata srv_rec;
187 struct rwrap_uri_rrdata uri_rec;
188 struct rwrap_soa_rrdata soa_rec;
189 char cname_rec[MAXDNAME];
190 char ptr_rec[MAXDNAME];
194 int type; /* ns_t_* */
197 static void rwrap_fake_rr_init(struct rwrap_fake_rr *rr, size_t len)
201 for (i = 0; i < len; i++) {
202 rr[i].type = ns_t_invalid;
206 static int rwrap_create_fake_a_rr(const char *key,
208 struct rwrap_fake_rr *rr)
212 ok = inet_pton(AF_INET, value, &rr->rrdata.a_rec);
214 RWRAP_LOG(RWRAP_LOG_ERROR,
215 "Failed to convert [%s] to binary\n", value);
219 memcpy(rr->key, key, strlen(key) + 1);
224 static int rwrap_create_fake_aaaa_rr(const char *key,
226 struct rwrap_fake_rr *rr)
230 ok = inet_pton(AF_INET6, value, &rr->rrdata.aaaa_rec);
232 RWRAP_LOG(RWRAP_LOG_ERROR,
233 "Failed to convert [%s] to binary\n", value);
237 memcpy(rr->key, key, strlen(key) + 1);
238 rr->type = ns_t_aaaa;
241 static int rwrap_create_fake_ns_rr(const char *key,
243 struct rwrap_fake_rr *rr)
245 memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1);
246 memcpy(rr->key, key, strlen(key) + 1);
251 static int rwrap_create_fake_srv_rr(const char *key,
253 struct rwrap_fake_rr *rr)
258 const char *hostname;
260 /* parse the value into priority, weight, port and hostname
261 * and check the validity */
263 NEXT_KEY(hostname, str_port);
264 NEXT_KEY(str_port, str_prio);
265 NEXT_KEY(str_prio, str_weight);
266 if (str_port == NULL || hostname == NULL) {
267 RWRAP_LOG(RWRAP_LOG_ERROR,
268 "Malformed SRV entry [%s]\n", value);
273 rr->rrdata.srv_rec.prio = atoi(str_prio);
275 rr->rrdata.srv_rec.prio = DFL_SRV_PRIO;
278 rr->rrdata.srv_rec.weight = atoi(str_weight);
280 rr->rrdata.srv_rec.weight = DFL_SRV_WEIGHT;
282 rr->rrdata.srv_rec.port = atoi(str_port);
283 memcpy(rr->rrdata.srv_rec.hostname , hostname, strlen(hostname) + 1);
285 memcpy(rr->key, key, strlen(key) + 1);
290 static int rwrap_create_fake_uri_rr(const char *key,
292 struct rwrap_fake_rr *rr)
298 /* parse the value into priority, weight, and uri
299 * and check the validity */
301 NEXT_KEY(uri, str_prio);
302 NEXT_KEY(str_prio, str_weight);
304 RWRAP_LOG(RWRAP_LOG_ERROR,
305 "Malformed URI entry [%s]\n", value);
310 rr->rrdata.uri_rec.prio = atoi(str_prio);
312 rr->rrdata.uri_rec.prio = DFL_URI_PRIO;
315 rr->rrdata.uri_rec.weight = atoi(str_weight);
317 rr->rrdata.uri_rec.weight = DFL_URI_WEIGHT;
319 memcpy(rr->rrdata.uri_rec.uri, uri, strlen(uri) + 1);
321 memcpy(rr->key, key, strlen(key) + 1);
326 static int rwrap_create_fake_soa_rr(const char *key,
328 struct rwrap_fake_rr *rr)
330 const char *nameserver;
338 /* parse the value into nameserver, mailbox, serial, refresh,
339 * retry, expire, minimum and check the validity
342 NEXT_KEY(nameserver, mailbox);
343 NEXT_KEY(mailbox, str_serial);
344 NEXT_KEY(str_serial, str_refresh);
345 NEXT_KEY(str_refresh, str_retry);
346 NEXT_KEY(str_retry, str_expire);
347 NEXT_KEY(str_expire, str_minimum);
348 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
349 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
350 str_minimum == NULL) {
351 RWRAP_LOG(RWRAP_LOG_ERROR,
352 "Malformed SOA entry [%s]\n", value);
356 memcpy(rr->rrdata.soa_rec.nameserver, nameserver, strlen(nameserver)+1);
357 memcpy(rr->rrdata.soa_rec.mailbox, mailbox, strlen(mailbox)+1);
359 rr->rrdata.soa_rec.serial = atoi(str_serial);
360 rr->rrdata.soa_rec.refresh = atoi(str_refresh);
361 rr->rrdata.soa_rec.retry = atoi(str_retry);
362 rr->rrdata.soa_rec.expire = atoi(str_expire);
363 rr->rrdata.soa_rec.minimum = atoi(str_minimum);
365 memcpy(rr->key, key, strlen(key) + 1);
370 static int rwrap_create_fake_cname_rr(const char *key,
372 struct rwrap_fake_rr *rr)
374 memcpy(rr->rrdata.cname_rec , value, strlen(value) + 1);
375 memcpy(rr->key, key, strlen(key) + 1);
376 rr->type = ns_t_cname;
380 static int rwrap_create_fake_ptr_rr(const char *key,
382 struct rwrap_fake_rr *rr)
384 memcpy(rr->rrdata.ptr_rec , value, strlen(value) + 1);
385 memcpy(rr->key, key, strlen(key) + 1);
390 /* Prepares a fake header with a single response. Advances header_blob */
391 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
392 size_t ancount, size_t arcount)
397 if (remaining < NS_HFIXEDSZ) {
398 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
403 memset(hb, 0, NS_HFIXEDSZ);
406 h->id = res_randomid(); /* random query ID */
407 h->qr = 1; /* response flag */
408 h->rd = 1; /* recursion desired */
409 h->ra = 1; /* recursion available */
411 h->qdcount = htons(1); /* no. of questions */
412 h->ancount = htons(ancount); /* no. of answers */
413 h->arcount = htons(arcount); /* no. of add'tl records */
415 hb += NS_HFIXEDSZ; /* move past the header */
421 static ssize_t rwrap_fake_question(const char *question,
423 uint8_t **question_ptr,
426 uint8_t *qb = *question_ptr;
429 n = ns_name_compress(question, qb, remaining, NULL, NULL);
431 RWRAP_LOG(RWRAP_LOG_ERROR,
432 "Failed to compress [%s]\n", question);
439 if (remaining < 2 * sizeof(uint16_t)) {
440 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
445 NS_PUT16(ns_c_in, qb);
448 return n + 2 * sizeof(uint16_t);
451 static ssize_t rwrap_fake_rdata_common(uint16_t type,
457 uint8_t *rd = *rdata_ptr;
460 written = ns_name_compress(key, rd, remaining, NULL, NULL);
462 RWRAP_LOG(RWRAP_LOG_ERROR,
463 "Failed to compress [%s]\n", key);
467 remaining -= written;
469 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
470 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
475 NS_PUT16(ns_c_in, rd);
476 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
477 NS_PUT16(rdata_size, rd);
479 if (remaining < rdata_size) {
480 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
485 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
488 static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
492 uint8_t *a = answer_ptr;
495 if (rr->type != ns_t_a) {
496 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
499 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
501 resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
507 memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
512 static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
519 if (rr->type != ns_t_aaaa) {
520 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
523 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
525 resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
526 rr->key, anslen, &a);
531 memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
536 static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr,
541 ssize_t resp_size = 0;
543 unsigned char hostname_compressed[MAXDNAME];
544 ssize_t compressed_len;
546 if (rr->type != ns_t_ns) {
547 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
550 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR");
552 /* Prepare the data to write */
553 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
558 if (compressed_len < 0) {
562 /* Is this enough? */
563 rdata_size = compressed_len;
565 resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size,
566 rr->key, anslen, &a);
571 memcpy(a, hostname_compressed, compressed_len);
576 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
583 unsigned char hostname_compressed[MAXDNAME];
584 ssize_t compressed_len;
586 if (rr->type != ns_t_srv) {
587 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
590 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
591 rdata_size = 3 * sizeof(uint16_t);
593 /* Prepare the data to write */
594 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
595 hostname_compressed, MAXDNAME,
597 if (compressed_len < 0) {
600 rdata_size += compressed_len;
602 resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
603 rr->key, anslen, &a);
608 NS_PUT16(rr->rrdata.srv_rec.prio, a);
609 NS_PUT16(rr->rrdata.srv_rec.weight, a);
610 NS_PUT16(rr->rrdata.srv_rec.port, a);
611 memcpy(a, hostname_compressed, compressed_len);
616 static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
625 if (rr->type != ns_t_uri) {
626 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
629 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR");
630 rdata_size = 3 * sizeof(uint16_t);
631 uri_len = strlen(rr->rrdata.uri_rec.uri) + 1;
632 rdata_size += uri_len;
634 resp_size = rwrap_fake_rdata_common(ns_t_uri, rdata_size,
635 rr->key, anslen, &a);
640 NS_PUT16(rr->rrdata.uri_rec.prio, a);
641 NS_PUT16(rr->rrdata.uri_rec.weight, a);
642 memcpy(a, rr->rrdata.uri_rec.uri, uri_len);
647 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
654 unsigned char nameser_compressed[MAXDNAME];
655 ssize_t compressed_ns_len;
656 unsigned char mailbox_compressed[MAXDNAME];
657 ssize_t compressed_mb_len;
659 if (rr->type != ns_t_soa) {
660 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
663 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
664 rdata_size = 5 * sizeof(uint16_t);
666 compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
668 MAXDNAME, NULL, NULL);
669 if (compressed_ns_len < 0) {
672 rdata_size += compressed_ns_len;
674 compressed_mb_len = ns_name_compress(rr->rrdata.soa_rec.mailbox,
676 MAXDNAME, NULL, NULL);
677 if (compressed_mb_len < 0) {
680 rdata_size += compressed_mb_len;
682 resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
683 rr->key, anslen, &a);
688 memcpy(a, nameser_compressed, compressed_ns_len);
689 a += compressed_ns_len;
690 memcpy(a, mailbox_compressed, compressed_mb_len);
691 a += compressed_mb_len;
692 NS_PUT32(rr->rrdata.soa_rec.serial, a);
693 NS_PUT32(rr->rrdata.soa_rec.refresh, a);
694 NS_PUT32(rr->rrdata.soa_rec.retry, a);
695 NS_PUT32(rr->rrdata.soa_rec.expire, a);
696 NS_PUT32(rr->rrdata.soa_rec.minimum, a);
701 static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
707 unsigned char hostname_compressed[MAXDNAME];
710 if (rr->type != ns_t_cname) {
711 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
714 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
716 /* Prepare the data to write */
717 rdata_size = ns_name_compress(rr->rrdata.cname_rec,
718 hostname_compressed, MAXDNAME,
720 if (rdata_size < 0) {
724 resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
725 rr->key, anslen, &a);
730 memcpy(a, hostname_compressed, rdata_size);
735 static ssize_t rwrap_fake_ptr(struct rwrap_fake_rr *rr,
742 unsigned char hostname_compressed[MAXDNAME];
744 if (rr->type != ns_t_ptr) {
745 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
748 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding PTR RR");
750 /* Prepare the data to write */
751 rdata_size = ns_name_compress(rr->rrdata.ptr_rec,
752 hostname_compressed, MAXDNAME,
754 if (rdata_size < 0) {
758 resp_size = rwrap_fake_rdata_common(ns_t_ptr, rdata_size,
759 rr->key, anslen, &a);
764 memcpy(a, hostname_compressed, rdata_size);
769 #define RESOLV_MATCH(line, name) \
770 (strncmp(line, name, sizeof(name) - 1) == 0 && \
771 (line[sizeof(name) - 1] == ' ' || \
772 line[sizeof(name) - 1] == '\t'))
774 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
775 ((type) == (ns_type) && \
776 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
777 (strcasecmp(key, query)) == 0)
780 static int rwrap_get_record(const char *hostfile, unsigned recursion,
781 const char *query, int type,
782 struct rwrap_fake_rr *rr);
784 static int rwrap_uri_recurse(const char *hostfile, unsigned recursion,
785 const char *query, struct rwrap_fake_rr *rr)
789 rc = rwrap_get_record(hostfile, recursion, query, ns_t_uri, rr);
797 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
798 const char *query, struct rwrap_fake_rr *rr)
802 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
803 if (rc == 0) return 0;
805 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
806 if (rc == ENOENT) rc = 0;
811 static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
812 const char *query, struct rwrap_fake_rr *rr)
816 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
817 if (rc == 0) return 0;
819 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
820 if (rc == 0) return 0;
822 rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
823 if (rc == ENOENT) rc = 0;
828 static int rwrap_get_record(const char *hostfile, unsigned recursion,
829 const char *query, int type,
830 struct rwrap_fake_rr *rr)
837 unsigned num_uris = 0;
839 if (recursion >= RWRAP_MAX_RECURSION) {
840 RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
844 RWRAP_LOG(RWRAP_LOG_TRACE,
845 "Searching in fake hosts file %s for %s:%d\n", hostfile,
848 fp = fopen(hostfile, "r");
850 RWRAP_LOG(RWRAP_LOG_ERROR,
851 "Opening %s failed: %s",
852 hostfile, strerror(errno));
856 while (fgets(buf, sizeof(buf), fp) != NULL) {
863 NEXT_KEY(rec_type, key);
864 NEXT_KEY(key, value);
866 if (key == NULL || value == NULL) {
867 RWRAP_LOG(RWRAP_LOG_WARN,
868 "Malformed line: not enough parts, use \"rec_type key data\n"
869 "For example \"A cwrap.org 10.10.10.10\"");
874 while(q[0] != '\n' && q[0] != '\0') {
879 if (type == ns_t_uri && recursion > 0) {
880 /* Skip non-URI records. */
881 if (!TYPE_MATCH(type, ns_t_uri, rec_type, "URI", key, query)) {
884 /* Skip previous records based on the recurse depth. */
886 if (num_uris <= recursion) {
891 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
892 rc = rwrap_create_fake_a_rr(key, value, rr);
894 } else if (TYPE_MATCH(type, ns_t_aaaa,
895 rec_type, "AAAA", key, query)) {
896 rc = rwrap_create_fake_aaaa_rr(key, value, rr);
898 } else if (TYPE_MATCH(type, ns_t_ns,
899 rec_type, "NS", key, query)) {
900 rc = rwrap_create_fake_ns_rr(key, value, rr);
902 } else if (TYPE_MATCH(type, ns_t_srv,
903 rec_type, "SRV", key, query)) {
904 rc = rwrap_create_fake_srv_rr(key, value, rr);
906 rc = rwrap_srv_recurse(hostfile, recursion+1,
907 rr->rrdata.srv_rec.hostname,
911 } else if (TYPE_MATCH(type, ns_t_uri,
912 rec_type, "URI", key, query)) {
913 rc = rwrap_create_fake_uri_rr(key, value, rr);
915 /* Recurse to collect multiple URI answers under a single key. */
916 rc = rwrap_uri_recurse(hostfile, recursion + 1, key, rr + 1);
919 } else if (TYPE_MATCH(type, ns_t_soa,
920 rec_type, "SOA", key, query)) {
921 rc = rwrap_create_fake_soa_rr(key, value, rr);
923 } else if (TYPE_MATCH(type, ns_t_cname,
924 rec_type, "CNAME", key, query)) {
925 rc = rwrap_create_fake_cname_rr(key, value, rr);
927 rc = rwrap_cname_recurse(hostfile, recursion+1,
931 } else if (TYPE_MATCH(type, ns_t_a, rec_type, "CNAME", key, query)) {
932 rc = rwrap_create_fake_cname_rr(key, value, rr);
934 rc = rwrap_cname_recurse(hostfile, recursion+1,
938 } else if (TYPE_MATCH(type, ns_t_ptr,
939 rec_type, "PTR", key, query)) {
940 rc = rwrap_create_fake_ptr_rr(key, value, rr);
945 if (rc == ENOENT && recursion == 0 && key != NULL) {
946 RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
947 memcpy(rr->key, key, strlen(key) + 1);
954 static ssize_t rwrap_fake_empty(int type,
955 const char *question,
960 size_t remaining = anslen;
962 resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
966 remaining -= resp_data;
968 resp_data += rwrap_fake_question(question, type, &answer, remaining);
972 remaining -= resp_data;
974 resp_data += rwrap_fake_rdata_common(type, 0, question,
983 static inline bool rwrap_known_type(int type)
1000 static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
1005 /* For URI return the number of URIs. */
1006 if (qtype == ns_t_uri) {
1007 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
1008 if (rwrap_known_type(rrs[i].type) &&
1009 rrs[i].type == qtype) {
1016 /* Include all RRs in the stack until the sought type
1017 * in the answer section. This is the case i.e. when looking
1018 * up an A record but the name points to a CNAME
1020 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
1023 if (rwrap_known_type(rrs[i].type) &&
1024 rrs[i].type == qtype) {
1029 /* Return 0 records if the sought type wasn't in the stack */
1030 return i < RWRAP_MAX_RECURSION ? ancount : 0;
1033 static int rwrap_arcount(struct rwrap_fake_rr *rrs, int ancount)
1038 /* start from index ancount */
1039 for (i = ancount; i < RWRAP_MAX_RECURSION; i++) {
1040 if (rwrap_known_type(rrs[i].type)) {
1048 static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
1055 RWRAP_LOG(RWRAP_LOG_ERROR, "Internal error!\n");
1061 resp_data = rwrap_fake_a(rr, answer, anslen);
1064 resp_data = rwrap_fake_aaaa(rr, answer, anslen);
1067 resp_data = rwrap_fake_ns(rr, answer, anslen);
1070 resp_data = rwrap_fake_srv(rr, answer, anslen);
1073 resp_data = rwrap_fake_uri(rr, answer, anslen);
1076 resp_data = rwrap_fake_soa(rr, answer, anslen);
1079 resp_data = rwrap_fake_cname(rr, answer, anslen);
1082 resp_data = rwrap_fake_ptr(rr, answer, anslen);
1091 static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
1099 size_t remaining = anslen;
1104 ancount = rwrap_ancount(rrs, type);
1105 arcount = rwrap_arcount(rrs, ancount);
1106 RWRAP_LOG(RWRAP_LOG_TRACE,
1107 "Got %d answers and %d additional records\n", ancount, arcount);
1109 resp_data = rwrap_fake_header(&answer, remaining, ancount, arcount);
1110 if (resp_data < 0) {
1113 remaining -= resp_data;
1115 resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
1116 if (resp_data < 0) {
1119 remaining -= resp_data;
1122 for (i = 0; i < ancount; i++) {
1123 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1132 /* add authoritative NS here? */
1134 /* additional records */
1135 for (i = ancount; i < ancount + arcount; i++) {
1136 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1148 /* Reads in a file in the following format:
1151 * Malformed entries are silently skipped.
1152 * Allocates answer buffer of size anslen that has to be freed after use.
1154 static int rwrap_res_fake_hosts(const char *hostfile,
1157 unsigned char *answer,
1161 char *query_name = NULL;
1162 size_t qlen = strlen(query);
1163 struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
1166 RWRAP_LOG(RWRAP_LOG_TRACE,
1167 "Searching in fake hosts file %s\n", hostfile);
1169 if (qlen > 0 && query[qlen-1] == '.') {
1173 query_name = strndup(query, qlen);
1174 if (query_name == NULL) {
1178 rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
1180 rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
1183 RWRAP_LOG(RWRAP_LOG_TRACE,
1184 "Found record for [%s]\n", query_name);
1185 resp_size = rwrap_fake_answer(rrs, type, answer, anslen);
1188 RWRAP_LOG(RWRAP_LOG_TRACE,
1189 "No record for [%s]\n", query_name);
1190 resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
1193 RWRAP_LOG(RWRAP_LOG_ERROR,
1194 "Error searching for [%s]\n", query_name);
1199 switch (resp_size) {
1201 RWRAP_LOG(RWRAP_LOG_ERROR,
1202 "Error faking answer for [%s]\n", query_name);
1205 RWRAP_LOG(RWRAP_LOG_TRACE,
1206 "Successfully faked answer for [%s]\n",
1215 /*********************************************************
1216 * RWRAP LOADING LIBC FUNCTIONS
1217 *********************************************************/
1221 typedef int (*__libc_res_ninit)(struct __res_state *state);
1222 typedef int (*__libc___res_ninit)(struct __res_state *state);
1223 typedef void (*__libc_res_nclose)(struct __res_state *state);
1224 typedef void (*__libc___res_nclose)(struct __res_state *state);
1225 typedef int (*__libc_res_nquery)(struct __res_state *state,
1229 unsigned char *answer,
1231 typedef int (*__libc___res_nquery)(struct __res_state *state,
1235 unsigned char *answer,
1237 typedef int (*__libc_res_nsearch)(struct __res_state *state,
1241 unsigned char *answer,
1243 typedef int (*__libc___res_nsearch)(struct __res_state *state,
1247 unsigned char *answer,
1250 #define RWRAP_SYMBOL_ENTRY(i) \
1256 struct rwrap_libc_symbols {
1257 RWRAP_SYMBOL_ENTRY(res_ninit);
1258 RWRAP_SYMBOL_ENTRY(__res_ninit);
1259 RWRAP_SYMBOL_ENTRY(res_nclose);
1260 RWRAP_SYMBOL_ENTRY(__res_nclose);
1261 RWRAP_SYMBOL_ENTRY(res_nquery);
1262 RWRAP_SYMBOL_ENTRY(__res_nquery);
1263 RWRAP_SYMBOL_ENTRY(res_nsearch);
1264 RWRAP_SYMBOL_ENTRY(__res_nsearch);
1266 #undef RWRAP_SYMBOL_ENTRY
1271 struct rwrap_libc_symbols symbols;
1276 struct rwrap_libc_symbols symbols;
1285 static struct rwrap rwrap;
1293 static const char *rwrap_str_lib(enum rwrap_lib lib)
1298 case RWRAP_LIBRESOLV:
1302 /* Compiler would warn us about unhandled enum value if we get here */
1307 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
1309 int flags = RTLD_LAZY;
1310 void *handle = NULL;
1313 #ifdef RTLD_DEEPBIND
1314 flags |= RTLD_DEEPBIND;
1318 case RWRAP_LIBRESOLV:
1319 #ifdef HAVE_LIBRESOLV
1320 handle = rwrap.libresolv.handle;
1321 if (handle == NULL) {
1322 for (i = 10; i >= 0; i--) {
1323 char soname[256] = {0};
1325 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
1326 handle = dlopen(soname, flags);
1327 if (handle != NULL) {
1332 rwrap.libresolv.handle = handle;
1338 handle = rwrap.libc.handle;
1340 if (handle == NULL) {
1341 handle = dlopen(LIBC_SO, flags);
1343 rwrap.libc.handle = handle;
1346 if (handle == NULL) {
1347 for (i = 10; i >= 0; i--) {
1348 char soname[256] = {0};
1350 snprintf(soname, sizeof(soname), "libc.so.%d", i);
1351 handle = dlopen(soname, flags);
1352 if (handle != NULL) {
1357 rwrap.libc.handle = handle;
1362 if (handle == NULL) {
1364 handle = rwrap.libc.handle = rwrap.libresolv.handle = RTLD_NEXT;
1366 RWRAP_LOG(RWRAP_LOG_ERROR,
1367 "Failed to dlopen library: %s\n",
1376 static void *_rwrap_bind_symbol(enum rwrap_lib lib, const char *fn_name)
1381 handle = rwrap_load_lib_handle(lib);
1383 func = dlsym(handle, fn_name);
1385 RWRAP_LOG(RWRAP_LOG_ERROR,
1386 "Failed to find %s: %s\n",
1387 fn_name, dlerror());
1391 RWRAP_LOG(RWRAP_LOG_TRACE,
1392 "Loaded %s from %s",
1393 fn_name, rwrap_str_lib(lib));
1397 #define rwrap_bind_symbol_libc(sym_name) \
1398 if (rwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
1399 rwrap.libc.symbols._libc_##sym_name.obj = \
1400 _rwrap_bind_symbol(RWRAP_LIBC, #sym_name); \
1403 #define rwrap_bind_symbol_libresolv(sym_name) \
1404 if (rwrap.libresolv.symbols._libc_##sym_name.obj == NULL) { \
1405 rwrap.libresolv.symbols._libc_##sym_name.obj = \
1406 _rwrap_bind_symbol(RWRAP_LIBRESOLV, #sym_name); \
1412 * Functions especially from libc need to be loaded individually, you can't load
1413 * all at once or gdb will segfault at startup. The same applies to valgrind and
1414 * has probably something todo with with the linker.
1415 * So we need load each function at the point it is called the first time.
1418 static int libc_res_ninit(struct __res_state *state)
1420 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1422 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1423 rwrap_bind_symbol_libresolv(res_ninit);
1425 return rwrap.libresolv.symbols._libc_res_ninit.f(state);
1426 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1427 rwrap_bind_symbol_libc(res_ninit);
1429 return rwrap.libc.symbols._libc_res_ninit.f(state);
1430 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1432 #elif defined(HAVE___RES_NINIT)
1433 rwrap_bind_symbol_libc(__res_ninit);
1435 return rwrap.libc.symbols._libc___res_ninit.f(state);
1437 #error "No res_ninit function"
1441 static void libc_res_nclose(struct __res_state *state)
1443 #if !defined(res_close) && defined(HAVE_RES_NCLOSE)
1445 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1446 rwrap_bind_symbol_libresolv(res_nclose);
1448 rwrap.libresolv.symbols._libc_res_nclose.f(state);
1450 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1451 rwrap_bind_symbol_libc(res_nclose);
1453 rwrap.libc.symbols._libc_res_nclose.f(state);
1455 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1457 #elif defined(HAVE___RES_NCLOSE)
1458 rwrap_bind_symbol_libc(__res_nclose);
1460 rwrap.libc.symbols._libc___res_nclose.f(state);
1462 #error "No res_nclose function"
1466 static int libc_res_nquery(struct __res_state *state,
1470 unsigned char *answer,
1473 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1474 rwrap_bind_symbol_libresolv(res_nquery);
1476 return rwrap.libresolv.symbols._libc_res_nquery.f(state,
1482 #elif defined(HAVE___RES_NQUERY)
1483 rwrap_bind_symbol_libresolv(__res_nquery);
1485 return rwrap.libresolv.symbols._libc___res_nquery.f(state,
1492 #error "No res_nquery function"
1496 static int libc_res_nsearch(struct __res_state *state,
1500 unsigned char *answer,
1503 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1504 rwrap_bind_symbol_libresolv(res_nsearch);
1506 return rwrap.libresolv.symbols._libc_res_nsearch.f(state,
1512 #elif defined(HAVE___RES_NSEARCH)
1513 rwrap_bind_symbol_libresolv(__res_nsearch);
1515 return rwrap.libresolv.symbols._libc___res_nsearch.f(state,
1522 #error "No res_nsearch function"
1526 /****************************************************************************
1528 ***************************************************************************/
1530 static int rwrap_parse_resolv_conf(struct __res_state *state,
1531 const char *resolv_conf)
1537 fp = fopen(resolv_conf, "r");
1539 RWRAP_LOG(RWRAP_LOG_ERROR,
1540 "Opening %s failed: %s",
1541 resolv_conf, strerror(errno));
1545 while(fgets(buf, sizeof(buf), fp) != NULL) {
1548 /* Ignore comments */
1549 if (buf[0] == '#' || buf[0] == ';') {
1553 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
1558 p = buf + strlen("nameserver");
1560 /* Skip spaces and tabs */
1561 while(isblank((int)p[0])) {
1566 while(q[0] != '\n' && q[0] != '\0') {
1571 ok = inet_pton(AF_INET, p, &a);
1573 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
1574 .sin_family = AF_INET,
1576 .sin_port = htons(53),
1583 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1586 ok = inet_pton(AF_INET6, p, &a6);
1588 struct sockaddr_in6 *sa6;
1590 sa6 = malloc(sizeof(*sa6));
1596 sa6->sin6_family = AF_INET6;
1597 sa6->sin6_port = htons(53);
1598 sa6->sin6_flowinfo = 0;
1599 sa6->sin6_addr = a6;
1601 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
1602 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
1603 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1605 state->_u._ext.nscount++;
1608 RWRAP_LOG(RWRAP_LOG_ERROR,
1609 "Malformed DNS server");
1612 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1614 * BSD uses an opaque structure to store the
1615 * IPv6 addresses. So we can not simply store
1616 * these addresses the same way as above.
1618 RWRAP_LOG(RWRAP_LOG_WARN,
1619 "resolve_wrapper does not support "
1620 "IPv6 on this platform");
1625 } /* TODO: match other keywords */
1629 RWRAP_LOG(RWRAP_LOG_ERROR,
1630 "Reading from %s failed",
1640 /****************************************************************************
1642 ***************************************************************************/
1644 static int rwrap_res_ninit(struct __res_state *state)
1648 rc = libc_res_ninit(state);
1650 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1652 if (resolv_conf != NULL) {
1655 (void)i; /* maybe unused */
1657 /* Delete name servers */
1659 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1661 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1662 state->_u._ext.nscount = 0;
1663 for (i = 0; i < state->_u._ext.nscount; i++) {
1664 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1668 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1675 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1676 int res_ninit(struct __res_state *state)
1677 #elif defined(HAVE___RES_NINIT)
1678 int __res_ninit(struct __res_state *state)
1681 return rwrap_res_ninit(state);
1684 /****************************************************************************
1686 ***************************************************************************/
1688 static struct __res_state rwrap_res_state;
1690 static int rwrap_res_init(void)
1694 rc = rwrap_res_ninit(&rwrap_res_state);
1699 #if !defined(res_ninit) && defined(HAVE_RES_INIT)
1701 #elif defined(HAVE___RES_INIT)
1702 int __res_init(void)
1705 return rwrap_res_init();
1708 /****************************************************************************
1710 ***************************************************************************/
1712 static void rwrap_res_nclose(struct __res_state *state)
1714 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1718 libc_res_nclose(state);
1720 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1721 if (state != NULL) {
1722 for (i = 0; i < state->_u._ext.nscount; i++) {
1723 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1729 #if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)
1730 void res_nclose(struct __res_state *state)
1731 #elif defined(HAVE___RES_NCLOSE)
1732 void __res_nclose(struct __res_state *state)
1735 rwrap_res_nclose(state);
1738 /****************************************************************************
1740 ***************************************************************************/
1742 static void rwrap_res_close(void)
1744 rwrap_res_nclose(&rwrap_res_state);
1747 #if defined(HAVE_RES_CLOSE)
1748 void res_close(void)
1749 #elif defined(HAVE___RES_CLOSE)
1750 void __res_close(void)
1756 /****************************************************************************
1758 ***************************************************************************/
1760 static int rwrap_res_nquery(struct __res_state *state,
1764 unsigned char *answer,
1768 const char *fake_hosts;
1773 RWRAP_LOG(RWRAP_LOG_TRACE,
1774 "Resolve the domain name [%s] - class=%d, type=%d",
1775 dname, class, type);
1777 for (i = 0; i < state->nscount; i++) {
1778 char ip[INET6_ADDRSTRLEN];
1780 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1781 RWRAP_LOG(RWRAP_LOG_TRACE,
1787 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1788 if (fake_hosts != NULL) {
1789 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1791 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1795 RWRAP_LOG(RWRAP_LOG_TRACE,
1796 "The returned response length is: %d",
1802 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1803 int res_nquery(struct __res_state *state,
1807 unsigned char *answer,
1809 #elif defined(HAVE___RES_NQUERY)
1810 int __res_nquery(struct __res_state *state,
1814 unsigned char *answer,
1818 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1821 /****************************************************************************
1823 ***************************************************************************/
1825 static int rwrap_res_query(const char *dname,
1828 unsigned char *answer,
1833 rc = rwrap_res_ninit(&rwrap_res_state);
1838 rc = rwrap_res_nquery(&rwrap_res_state,
1848 #if !defined(res_query) && defined(HAVE_RES_QUERY)
1849 int res_query(const char *dname,
1852 unsigned char *answer,
1854 #elif defined(HAVE___RES_QUERY)
1855 int __res_query(const char *dname,
1858 unsigned char *answer,
1862 return rwrap_res_query(dname, class, type, answer, anslen);
1865 /****************************************************************************
1867 ***************************************************************************/
1869 static int rwrap_res_nsearch(struct __res_state *state,
1873 unsigned char *answer,
1877 const char *fake_hosts;
1882 RWRAP_LOG(RWRAP_LOG_TRACE,
1883 "Resolve the domain name [%s] - class=%d, type=%d",
1884 dname, class, type);
1886 for (i = 0; i < state->nscount; i++) {
1887 char ip[INET6_ADDRSTRLEN];
1889 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1890 RWRAP_LOG(RWRAP_LOG_TRACE,
1896 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1897 if (fake_hosts != NULL) {
1898 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1900 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1903 RWRAP_LOG(RWRAP_LOG_TRACE,
1904 "The returned response length is: %d",
1910 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1911 int res_nsearch(struct __res_state *state,
1915 unsigned char *answer,
1917 #elif defined(HAVE___RES_NSEARCH)
1918 int __res_nsearch(struct __res_state *state,
1922 unsigned char *answer,
1926 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1929 /****************************************************************************
1931 ***************************************************************************/
1933 static int rwrap_res_search(const char *dname,
1936 unsigned char *answer,
1941 rc = rwrap_res_ninit(&rwrap_res_state);
1946 rc = rwrap_res_nsearch(&rwrap_res_state,
1956 #if !defined(res_search) && defined(HAVE_RES_SEARCH)
1957 int res_search(const char *dname,
1960 unsigned char *answer,
1962 #elif defined(HAVE___RES_SEARCH)
1963 int __res_search(const char *dname,
1966 unsigned char *answer,
1970 return rwrap_res_search(dname, class, type, answer, anslen);