rwrap: fix resolv wrapper with ipv6 addresses and old glibc versions
[resolv_wrapper.git] / src / resolv_wrapper.c
index 77baa452ab79b1ada5c344de714b0b33ce67965e..20ba2d1a899536977daaa20ac6bd9d5dacbf7272 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Copyright (c) 2014      Andreas Schneider <asn@samba.org>
- * Copyright (c) 2014      Jakub Hrozek <jakub.hrozek@posteo.se>
+ * Copyright (c) 2014-2018 Andreas Schneider <asn@samba.org>
+ * Copyright (c) 2014-2016 Jakub Hrozek <jakub.hrozek@posteo.se>
  *
  * All rights reserved.
  *
 
 #include <errno.h>
 #include <arpa/inet.h>
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif /* HAVE_ARPA_NAMESER_H */
 #include <netinet/in.h>
+#include <sys/socket.h>
 #include <sys/types.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #define ns_name_compress dn_comp
 #endif
 
+#define ns_t_uri 256
+
 enum rwrap_dbglvl_e {
        RWRAP_LOG_ERROR = 0,
        RWRAP_LOG_WARN,
+       RWRAP_LOG_NOTICE,
        RWRAP_LOG_DEBUG,
        RWRAP_LOG_TRACE
 };
 
-#ifdef NDEBUG
-# define RWRAP_LOG(...)
-#else /* NDEBUG */
+#ifndef HAVE_GETPROGNAME
+static const char *getprogname(void)
+{
+#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
+       return program_invocation_short_name;
+#elif defined(HAVE_GETEXECNAME)
+       return getexecname();
+#else
+       return NULL;
+#endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
+}
+#endif /* HAVE_GETPROGNAME */
 
 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
@@ -91,43 +107,53 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
        va_list va;
        const char *d;
        unsigned int lvl = 0;
-       int pid = getpid();
+       const char *prefix = NULL;
+       const char *progname = NULL;
 
        d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
        if (d != NULL) {
                lvl = atoi(d);
        }
 
+       if (lvl < dbglvl) {
+               return;
+       }
+
        va_start(va, format);
        vsnprintf(buffer, sizeof(buffer), format, va);
        va_end(va);
 
-       if (lvl >= dbglvl) {
-               switch (dbglvl) {
-                       case RWRAP_LOG_ERROR:
-                               fprintf(stderr,
-                                       "RWRAP_ERROR(%d) - %s: %s\n",
-                                       pid, func, buffer);
-                               break;
-                       case RWRAP_LOG_WARN:
-                               fprintf(stderr,
-                                       "RWRAP_WARN(%d) - %s: %s\n",
-                                       pid, func, buffer);
-                               break;
-                       case RWRAP_LOG_DEBUG:
-                               fprintf(stderr,
-                                       "RWRAP_DEBUG(%d) - %s: %s\n",
-                                       pid, func, buffer);
-                               break;
-                       case RWRAP_LOG_TRACE:
-                               fprintf(stderr,
-                                       "RWRAP_TRACE(%d) - %s: %s\n",
-                                       pid, func, buffer);
-                               break;
-               }
+       switch (dbglvl) {
+               case RWRAP_LOG_ERROR:
+                       prefix = "RWRAP_ERROR";
+                       break;
+               case RWRAP_LOG_WARN:
+                       prefix = "RWRAP_WARN";
+                       break;
+               case RWRAP_LOG_NOTICE:
+                       prefix = "RWRAP_NOTICE";
+                       break;
+               case RWRAP_LOG_DEBUG:
+                       prefix = "RWRAP_DEBUG";
+                       break;
+               case RWRAP_LOG_TRACE:
+                       prefix = "RWRAP_TRACE";
+                       break;
        }
+
+       progname = getprogname();
+       if (progname == NULL) {
+               progname = "<unknown>";
+       }
+
+       fprintf(stderr,
+               "%s[%s (%u)] - %s: %s\n",
+               prefix,
+               progname,
+               (unsigned int)getpid(),
+               func,
+               buffer);
 }
-#endif /* NDEBUG RWRAP_LOG */
 
 #ifndef SAFE_FREE
 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
@@ -145,13 +171,15 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
        }                                                       \
 } while(0);
 
-#define RWRAP_MAX_RECURSION 5
+#define RWRAP_MAX_RECURSION 64
 
 /* Priority and weight can be omitted from the hosts file, but need to be part
  * of the output
  */
 #define DFL_SRV_PRIO   1
 #define DFL_SRV_WEIGHT 100
+#define DFL_URI_PRIO   1
+#define DFL_URI_WEIGHT 100
 
 struct rwrap_srv_rrdata {
        uint16_t port;
@@ -160,6 +188,12 @@ struct rwrap_srv_rrdata {
        char hostname[MAXDNAME];
 };
 
+struct rwrap_uri_rrdata {
+       uint16_t prio;
+       uint16_t weight;
+       char uri[MAXDNAME];
+};
+
 struct rwrap_soa_rrdata {
        uint32_t serial;
        uint32_t refresh;
@@ -175,8 +209,11 @@ struct rwrap_fake_rr {
                struct in_addr a_rec;
                struct in6_addr aaaa_rec;
                struct rwrap_srv_rrdata srv_rec;
+               struct rwrap_uri_rrdata uri_rec;
                struct rwrap_soa_rrdata soa_rec;
                char cname_rec[MAXDNAME];
+               char ptr_rec[MAXDNAME];
+               char txt_rec[MAXDNAME];
        } rrdata;
 
        char key[MAXDNAME];
@@ -227,6 +264,15 @@ static int rwrap_create_fake_aaaa_rr(const char *key,
        rr->type = ns_t_aaaa;
        return 0;
 }
+static int rwrap_create_fake_ns_rr(const char *key,
+                                  const char *value,
+                                  struct rwrap_fake_rr *rr)
+{
+       memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1);
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_ns;
+       return 0;
+}
 
 static int rwrap_create_fake_srv_rr(const char *key,
                                    const char *value,
@@ -267,6 +313,53 @@ static int rwrap_create_fake_srv_rr(const char *key,
        return 0;
 }
 
+static int rwrap_create_fake_uri_rr(const char *key,
+                                   const char *value,
+                                   struct rwrap_fake_rr *rr)
+{
+       char *str_prio;
+       char *str_weight;
+       const char *uri;
+
+       /* parse the value into priority, weight, and uri
+        * and check the validity */
+       uri = value;
+       NEXT_KEY(uri, str_prio);
+       NEXT_KEY(str_prio, str_weight);
+       if (uri == NULL) {
+               RWRAP_LOG(RWRAP_LOG_ERROR,
+                         "Malformed URI entry [<null>]\n");
+               return -1;
+       }
+
+       if (str_prio) {
+               rr->rrdata.uri_rec.prio = atoi(str_prio);
+       } else {
+               rr->rrdata.uri_rec.prio = DFL_URI_PRIO;
+       }
+       if (str_weight) {
+               rr->rrdata.uri_rec.weight = atoi(str_weight);
+       } else {
+               rr->rrdata.uri_rec.weight = DFL_URI_WEIGHT;
+       }
+       memcpy(rr->rrdata.uri_rec.uri, uri, strlen(uri) + 1);
+
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_uri;
+       return 0;
+}
+
+static int rwrap_create_fake_txt_rr(const char *key,
+                                   const char *value,
+                                   struct rwrap_fake_rr *rr)
+{
+       memcpy(rr->rrdata.txt_rec, value, strlen(value) + 1);
+
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_txt;
+       return 0;
+}
+
 static int rwrap_create_fake_soa_rr(const char *key,
                                    const char *value,
                                    struct rwrap_fake_rr *rr)
@@ -321,33 +414,44 @@ static int rwrap_create_fake_cname_rr(const char *key,
        return 0;
 }
 
+static int rwrap_create_fake_ptr_rr(const char *key,
+                                   const char *value,
+                                   struct rwrap_fake_rr *rr)
+{
+       memcpy(rr->rrdata.ptr_rec , value, strlen(value) + 1);
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_ptr;
+       return 0;
+}
+
 /* Prepares a fake header with a single response. Advances header_blob */
 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
                                 size_t ancount, size_t arcount)
 {
-       uint8_t *hb;
-       HEADER *h;
+       union {
+               uint8_t *blob;
+               HEADER *header;
+       } h;
 
        if (remaining < NS_HFIXEDSZ) {
                RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
                return -1;
        }
 
-       hb = *header_blob;
-       memset(hb, 0, NS_HFIXEDSZ);
+       h.blob = *header_blob;
+       memset(h.blob, 0, NS_HFIXEDSZ);
 
-       h = (HEADER *) hb;
-       h->id = res_randomid();         /* random query ID */
-       h->qr = 1;                      /* response flag */
-       h->rd = 1;                      /* recursion desired */
-       h->ra = 1;                      /* recursion available */
+       h.header->id = res_randomid();          /* random query ID */
+       h.header->qr = 1;                       /* response flag */
+       h.header->rd = 1;                       /* recursion desired */
+       h.header->ra = 1;                       /* recursion available */
 
-       h->qdcount = htons(1);          /* no. of questions */
-       h->ancount = htons(ancount);    /* no. of answers */
-       h->arcount = htons(arcount);    /* no. of add'tl records */
+       h.header->qdcount = htons(1);           /* no. of questions */
+       h.header->ancount = htons(ancount);     /* no. of answers */
+       h.header->arcount = htons(arcount);     /* no. of add'tl records */
 
-       hb += NS_HFIXEDSZ;              /* move past the header */
-       *header_blob = hb;
+       /* move past the header */
+       *header_blob = h.blob += NS_HFIXEDSZ;
 
        return NS_HFIXEDSZ;
 }
@@ -426,9 +530,8 @@ static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
        uint8_t *a = answer_ptr;
        ssize_t resp_size;
 
-       if (rr == NULL || rr->type != ns_t_a) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_a) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
        RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
@@ -451,9 +554,8 @@ static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
        uint8_t *a = answer;
        ssize_t resp_size;
 
-       if (rr == NULL || rr->type != ns_t_aaaa) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_aaaa) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
        RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
@@ -469,6 +571,46 @@ static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
        return resp_size;
 }
 
+static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr,
+                            uint8_t *answer,
+                           size_t anslen)
+{
+       uint8_t *a = answer;
+       ssize_t resp_size = 0;
+       size_t rdata_size;
+       unsigned char hostname_compressed[MAXDNAME];
+       ssize_t compressed_len;
+
+       if (rr->type != ns_t_ns) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR");
+
+       /* Prepare the data to write */
+       compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
+                                         hostname_compressed,
+                                         MAXDNAME,
+                                         NULL,
+                                         NULL);
+       if (compressed_len < 0) {
+               return -1;
+       }
+
+       /* Is this enough? */
+       rdata_size = compressed_len;
+
+       resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size,
+                                           rr->key, anslen, &a);
+       if (resp_size < 0) {
+               return -1;
+       }
+
+       memcpy(a, hostname_compressed, compressed_len);
+
+       return resp_size;
+}
+
 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
                              uint8_t *answer,
                              size_t anslen)
@@ -479,9 +621,8 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
        unsigned char hostname_compressed[MAXDNAME];
        ssize_t compressed_len;
 
-       if (rr == NULL || rr->type != ns_t_srv) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_srv) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
        RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
@@ -510,6 +651,65 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
        return resp_size;
 }
 
+static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
+                             uint8_t *answer,
+                             size_t anslen)
+{
+       uint8_t *a = answer;
+       ssize_t resp_size;
+       size_t rdata_size;
+       size_t uri_len;
+
+       if (rr->type != ns_t_uri) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR");
+       rdata_size = 3 * sizeof(uint16_t);
+       uri_len = strlen(rr->rrdata.uri_rec.uri) + 1;
+       rdata_size += uri_len;
+
+       resp_size = rwrap_fake_rdata_common(ns_t_uri, rdata_size,
+                                           rr->key, anslen, &a);
+       if (resp_size < 0) {
+               return -1;
+       }
+
+       NS_PUT16(rr->rrdata.uri_rec.prio, a);
+       NS_PUT16(rr->rrdata.uri_rec.weight, a);
+       memcpy(a, rr->rrdata.uri_rec.uri, uri_len);
+
+       return resp_size;
+}
+
+static ssize_t rwrap_fake_txt(struct rwrap_fake_rr *rr,
+                             uint8_t *answer,
+                             size_t anslen)
+{
+       uint8_t *a = answer;
+       ssize_t resp_size;
+       size_t rdata_size;
+       size_t txt_len;
+
+       if (rr->type != ns_t_txt) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding TXT RR");
+       txt_len = strlen(rr->rrdata.txt_rec) + 1;
+       rdata_size = txt_len;
+
+       resp_size = rwrap_fake_rdata_common(ns_t_txt, rdata_size,
+                                           rr->key, anslen, &a);
+       if (resp_size < 0) {
+               return -1;
+       }
+
+       memcpy(a, rr->rrdata.txt_rec, txt_len);
+
+       return resp_size;
+}
+
 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
                              uint8_t *answer,
                              size_t anslen)
@@ -522,9 +722,8 @@ static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
        unsigned char mailbox_compressed[MAXDNAME];
        ssize_t compressed_mb_len;
 
-       if (rr == NULL || rr->type != ns_t_soa) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_soa) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
        RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
@@ -574,9 +773,8 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
        unsigned char hostname_compressed[MAXDNAME];
        ssize_t rdata_size;
 
-       if (rr == NULL || rr->type != ns_t_cname) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_cname) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
        RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
@@ -600,6 +798,40 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
        return resp_size;
 }
 
+static ssize_t rwrap_fake_ptr(struct rwrap_fake_rr *rr,
+                             uint8_t *answer,
+                             size_t anslen)
+{
+       uint8_t *a = answer;
+       ssize_t rdata_size;
+       ssize_t resp_size;
+       unsigned char hostname_compressed[MAXDNAME];
+
+       if (rr->type != ns_t_ptr) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding PTR RR");
+
+       /* Prepare the data to write */
+       rdata_size = ns_name_compress(rr->rrdata.ptr_rec,
+                                     hostname_compressed, MAXDNAME,
+                                     NULL, NULL);
+       if (rdata_size < 0) {
+               return -1;
+       }
+
+       resp_size = rwrap_fake_rdata_common(ns_t_ptr, rdata_size,
+                                           rr->key, anslen, &a);
+       if (resp_size < 0) {
+               return -1;
+       }
+
+       memcpy(a, hostname_compressed, rdata_size);
+
+       return resp_size;
+}
+
 #define RESOLV_MATCH(line, name) \
        (strncmp(line, name, sizeof(name) - 1) == 0 && \
        (line[sizeof(name) - 1] == ' ' || \
@@ -615,6 +847,19 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                            const char *query, int type,
                            struct rwrap_fake_rr *rr);
 
+static int rwrap_uri_recurse(const char *hostfile, unsigned recursion,
+                            const char *query, struct rwrap_fake_rr *rr)
+{
+       int rc;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_uri, rr);
+       if (rc == ENOENT) {
+               rc = 0;
+       }
+
+       return rc;
+}
+
 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
                             const char *query, struct rwrap_fake_rr *rr)
 {
@@ -655,6 +900,7 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
        char *key = NULL;
        char *value = NULL;
        int rc = ENOENT;
+       unsigned num_uris = 0;
 
        if (recursion >= RWRAP_MAX_RECURSION) {
                RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
@@ -662,11 +908,12 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
        }
 
        RWRAP_LOG(RWRAP_LOG_TRACE,
-                 "Searching in fake hosts file %s\n", hostfile);
+                 "Searching in fake hosts file %s for %s:%d\n", hostfile,
+                 query, type);
 
        fp = fopen(hostfile, "r");
        if (fp == NULL) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
+               RWRAP_LOG(RWRAP_LOG_WARN,
                          "Opening %s failed: %s",
                          hostfile, strerror(errno));
                return -1;
@@ -695,6 +942,18 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                }
                q[0] = '\0';
 
+               if (type == ns_t_uri && recursion > 0) {
+                       /* Skip non-URI records. */
+                       if (!TYPE_MATCH(type, ns_t_uri, rec_type, "URI", key, query)) {
+                               continue;
+                       }
+                       /* Skip previous records based on the recurse depth. */
+                       num_uris++;
+                       if (num_uris <= recursion) {
+                               continue;
+                       }
+               }
+
                if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
                        rc = rwrap_create_fake_a_rr(key, value, rr);
                        break;
@@ -702,6 +961,10 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                                      rec_type, "AAAA", key, query)) {
                        rc = rwrap_create_fake_aaaa_rr(key, value, rr);
                        break;
+               } else if (TYPE_MATCH(type, ns_t_ns,
+                                     rec_type, "NS", key, query)) {
+                       rc = rwrap_create_fake_ns_rr(key, value, rr);
+                       break;
                } else if (TYPE_MATCH(type, ns_t_srv,
                                      rec_type, "SRV", key, query)) {
                        rc = rwrap_create_fake_srv_rr(key, value, rr);
@@ -711,6 +974,14 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                                                rr + 1);
                        }
                        break;
+               } else if (TYPE_MATCH(type, ns_t_uri,
+                                     rec_type, "URI", key, query)) {
+                       rc = rwrap_create_fake_uri_rr(key, value, rr);
+                       if (rc == 0) {
+                               /* Recurse to collect multiple URI answers under a single key. */
+                               rc = rwrap_uri_recurse(hostfile, recursion + 1, key, rr + 1);
+                       }
+                       break;
                } else if (TYPE_MATCH(type, ns_t_soa,
                                      rec_type, "SOA", key, query)) {
                        rc = rwrap_create_fake_soa_rr(key, value, rr);
@@ -730,6 +1001,15 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                                                         value, rr + 1);
                        }
                        break;
+               } else if (TYPE_MATCH(type, ns_t_ptr,
+                                     rec_type, "PTR", key, query)) {
+                       rc = rwrap_create_fake_ptr_rr(key, value, rr);
+                       break;
+               }
+               else if (TYPE_MATCH(type, ns_t_txt,
+                                     rec_type, "TXT", key, query)) {
+                       rc = rwrap_create_fake_txt_rr(key, value, rr);
+                       break;
                }
        }
 
@@ -776,9 +1056,13 @@ static inline bool rwrap_known_type(int type)
        switch (type) {
        case ns_t_a:
        case ns_t_aaaa:
+       case ns_t_ns:
        case ns_t_srv:
+       case ns_t_uri:
        case ns_t_soa:
        case ns_t_cname:
+       case ns_t_ptr:
+       case ns_t_txt:
                return true;
        }
 
@@ -790,6 +1074,17 @@ static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
        int i;
        int ancount = 0;
 
+       /* For URI return the number of URIs. */
+       if (qtype == ns_t_uri) {
+               for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
+                       if (rwrap_known_type(rrs[i].type) &&
+                           rrs[i].type == qtype) {
+                               ancount++;
+                       }
+               }
+               return ancount;
+       }
+
        /* Include all RRs in the stack until the sought type
         * in the answer section. This is the case i.e. when looking
         * up an A record but the name points to a CNAME
@@ -828,6 +1123,11 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
 {
        ssize_t resp_data;
 
+       if (rr == NULL) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Internal error!\n");
+               return -1;
+       }
+
        switch (rr->type) {
        case ns_t_a:
                resp_data = rwrap_fake_a(rr, answer, anslen);
@@ -835,15 +1135,27 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
        case ns_t_aaaa:
                resp_data = rwrap_fake_aaaa(rr, answer, anslen);
                break;
+       case ns_t_ns:
+               resp_data = rwrap_fake_ns(rr, answer, anslen);
+               break;
        case ns_t_srv:
                resp_data = rwrap_fake_srv(rr, answer, anslen);
                break;
+       case ns_t_uri:
+               resp_data = rwrap_fake_uri(rr, answer, anslen);
+               break;
        case ns_t_soa:
                resp_data = rwrap_fake_soa(rr, answer, anslen);
                break;
        case ns_t_cname:
                resp_data = rwrap_fake_cname(rr, answer, anslen);
                break;
+       case ns_t_ptr:
+               resp_data = rwrap_fake_ptr(rr, answer, anslen);
+               break;
+       case ns_t_txt:
+               resp_data = rwrap_fake_txt(rr, answer, anslen);
+               break;
        default:
                return -1;
        }
@@ -953,8 +1265,9 @@ static int rwrap_res_fake_hosts(const char *hostfile,
                resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
                break;
        default:
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                               "Error searching for [%s]\n", query_name);
+               RWRAP_LOG(RWRAP_LOG_NOTICE,
+                         "Searching for [%s] did not return any results\n",
+                         query_name);
                free(query_name);
                return -1;
        }
@@ -1052,7 +1365,6 @@ enum rwrap_lib {
     RWRAP_LIBRESOLV
 };
 
-#ifndef NDEBUG
 static const char *rwrap_str_lib(enum rwrap_lib lib)
 {
        switch (lib) {
@@ -1065,7 +1377,6 @@ static const char *rwrap_str_lib(enum rwrap_lib lib)
        /* Compiler would warn us about unhandled enum value if we get here */
        return "unknown";
 }
-#endif
 
 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
 {
@@ -1074,7 +1385,25 @@ static void *rwrap_load_lib_handle(enum rwrap_lib lib)
        int i;
 
 #ifdef RTLD_DEEPBIND
-       flags |= RTLD_DEEPBIND;
+       const char *env_preload = getenv("LD_PRELOAD");
+       const char *env_deepbind = getenv("RESOLV_WRAPPER_DISABLE_DEEPBIND");
+       bool enable_deepbind = true;
+
+       /* Don't do a deepbind if we run with libasan */
+       if (env_preload != NULL && strlen(env_preload) < 1024) {
+               const char *p = strstr(env_preload, "libasan.so");
+               if (p != NULL) {
+                       enable_deepbind = false;
+               }
+       }
+
+       if (env_deepbind != NULL && strlen(env_deepbind) >= 1) {
+               enable_deepbind = false;
+       }
+
+       if (enable_deepbind) {
+               flags |= RTLD_DEEPBIND;
+       }
 #endif
 
        switch (lib) {
@@ -1181,21 +1510,13 @@ static void *_rwrap_bind_symbol(enum rwrap_lib lib, const char *fn_name)
 static int libc_res_ninit(struct __res_state *state)
 {
 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
-
-#if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
        rwrap_bind_symbol_libresolv(res_ninit);
 
        return rwrap.libresolv.symbols._libc_res_ninit.f(state);
-#else /* HAVE_RES_NINIT_IN_LIBRESOLV */
-       rwrap_bind_symbol_libc(res_ninit);
-
-       return rwrap.libc.symbols._libc_res_ninit.f(state);
-#endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
-
 #elif defined(HAVE___RES_NINIT)
-       rwrap_bind_symbol_libc(__res_ninit);
+       rwrap_bind_symbol_libresolv(__res_ninit);
 
-       return rwrap.libc.symbols._libc___res_ninit.f(state);
+       return rwrap.libresolv.symbols._libc___res_ninit.f(state);
 #else
 #error "No res_ninit function"
 #endif
@@ -1204,23 +1525,14 @@ static int libc_res_ninit(struct __res_state *state)
 static void libc_res_nclose(struct __res_state *state)
 {
 #if !defined(res_close) && defined(HAVE_RES_NCLOSE)
-
-#if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
        rwrap_bind_symbol_libresolv(res_nclose);
 
        rwrap.libresolv.symbols._libc_res_nclose.f(state);
        return;
-#else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
-       rwrap_bind_symbol_libc(res_nclose);
-
-       rwrap.libc.symbols._libc_res_nclose.f(state);
-       return;
-#endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
-
 #elif defined(HAVE___RES_NCLOSE)
-       rwrap_bind_symbol_libc(__res_nclose);
+       rwrap_bind_symbol_libresolv(__res_nclose);
 
-       rwrap.libc.symbols._libc___res_nclose.f(state);
+       rwrap.libresolv.symbols._libc___res_nclose.f(state);
 #else
 #error "No res_nclose function"
 #endif
@@ -1333,14 +1645,13 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
 
                        ok = inet_pton(AF_INET, p, &a);
                        if (ok) {
-                               state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
+                               state->nsaddr_list[nserv] = (struct sockaddr_in) {
                                        .sin_family = AF_INET,
                                        .sin_addr = a,
                                        .sin_port = htons(53),
                                        .sin_zero = { 0 },
                                };
 
-                               state->nscount++;
                                nserv++;
                        } else {
 #ifdef HAVE_RESOLV_IPV6_NSADDRS
@@ -1361,11 +1672,11 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
                                        sa6->sin6_flowinfo = 0;
                                        sa6->sin6_addr = a6;
 
-                                       state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
-                                       state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
-                                       state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
+                                       state->_u._ext.nsaddrs[nserv] = sa6;
+                                       state->_u._ext.nssocks[nserv] = -1;
+                                       state->_u._ext.nsmap[nserv] = MAXNS + 1;
 
-                                       state->_u._ext.nscount++;
+                                       state->_u._ext.nscount6++;
                                        nserv++;
                                } else {
                                        RWRAP_LOG(RWRAP_LOG_ERROR,
@@ -1388,6 +1699,13 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
                } /* TODO: match other keywords */
        }
 
+       /*
+        * note that state->_u._ext.nscount is left as 0,
+        * this matches glibc and allows resolv wrapper
+        * to work with most (maybe all) glibc versions.
+        */
+       state->nscount = nserv;
+
        if (ferror(fp)) {
                RWRAP_LOG(RWRAP_LOG_ERROR,
                          "Reading from %s failed",
@@ -1413,21 +1731,36 @@ static int rwrap_res_ninit(struct __res_state *state)
                const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
 
                if (resolv_conf != NULL) {
+                       /* Delete name servers */
+#ifdef HAVE_RESOLV_IPV6_NSADDRS
                        uint16_t i;
 
-                       (void)i; /* maybe unused */
+                       for (i = 0; i < state->nscount; i++) {
+                               if (state->_u._ext.nssocks[i] != -1) {
+                                       close(state->_u._ext.nssocks[i]);
+                                       state->_u._ext.nssocks[i] = -1;
+                               }
+
+                               SAFE_FREE(state->_u._ext.nsaddrs[i]);
+                       }
+#endif
 
-                       /* Delete name servers */
                        state->nscount = 0;
                        memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
 
-                       state->_u._ext.nscount = 0;
 #ifdef HAVE_RESOLV_IPV6_NSADDRS
-                       for (i = 0; i < state->_u._ext.nscount; i++) {
-                               SAFE_FREE(state->_u._ext.nsaddrs[i]);
+                       state->ipv6_unavail = false;
+                       state->_u._ext.nsinit = 0;
+                       state->_u._ext.nscount = 0;
+                       state->_u._ext.nscount6 = 0;
+                       for (i = 0; i < MAXNS; i++) {
+                               state->_u._ext.nsaddrs[i] = NULL;
+                               state->_u._ext.nssocks[i] = -1;
+                               state->_u._ext.nsmap[i] = MAXNS;
                        }
 #endif
 
+                       /* And parse the new name servers */
                        rc = rwrap_parse_resolv_conf(state, resolv_conf);
                }
        }
@@ -1474,19 +1807,7 @@ int __res_init(void)
 
 static void rwrap_res_nclose(struct __res_state *state)
 {
-#ifdef HAVE_RESOLV_IPV6_NSADDRS
-       int i;
-#endif
-
        libc_res_nclose(state);
-
-#ifdef HAVE_RESOLV_IPV6_NSADDRS
-       if (state != NULL) {
-               for (i = 0; i < state->_u._ext.nscount; i++) {
-                       SAFE_FREE(state->_u._ext.nsaddrs[i]);
-               }
-       }
-#endif
 }
 
 #if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)