rwrap: split out a rwrap_reset_nameservers() function
[resolv_wrapper.git] / src / resolv_wrapper.c
index 2b496967a1d3bd6cebba7f4a838e4cf67998f2c7..7c5bf81ab54ef6c95becaa8ebede45a0bdbd8285 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 <resolv.h>
 
+#ifdef HAVE_RES_STATE_U_EXT_NSADDRS
+#define HAVE_RESOLV_IPV6_NSADDRS 1
+#endif
+
 /* GCC has printf type attribute check. */
 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
@@ -83,6 +87,19 @@ enum rwrap_dbglvl_e {
        RWRAP_LOG_TRACE
 };
 
+#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__)
 
@@ -94,8 +111,8 @@ 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) {
@@ -128,10 +145,16 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
                        break;
        }
 
+       progname = getprogname();
+       if (progname == NULL) {
+               progname = "<unknown>";
+       }
+
        fprintf(stderr,
-               "%s(%d) - %s: %s\n",
+               "%s[%s (%u)] - %s: %s\n",
                prefix,
-               pid,
+               progname,
+               (unsigned int)getpid(),
                func,
                buffer);
 }
@@ -194,6 +217,7 @@ struct rwrap_fake_rr {
                struct rwrap_soa_rrdata soa_rec;
                char cname_rec[MAXDNAME];
                char ptr_rec[MAXDNAME];
+               char txt_rec[MAXDNAME];
        } rrdata;
 
        char key[MAXDNAME];
@@ -308,7 +332,7 @@ static int rwrap_create_fake_uri_rr(const char *key,
        NEXT_KEY(str_prio, str_weight);
        if (uri == NULL) {
                RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed URI entry [%s]\n", value);
+                         "Malformed URI entry [<null>]\n");
                return -1;
        }
 
@@ -329,6 +353,17 @@ static int rwrap_create_fake_uri_rr(const char *key,
        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)
@@ -397,29 +432,30 @@ static int rwrap_create_fake_ptr_rr(const char *key,
 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;
 }
@@ -650,6 +686,34 @@ static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
        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)
@@ -946,6 +1010,11 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion,
                        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;
+               }
        }
 
        if (rc == ENOENT && recursion == 0 && key != NULL) {
@@ -997,6 +1066,7 @@ static inline bool rwrap_known_type(int type)
        case ns_t_soa:
        case ns_t_cname:
        case ns_t_ptr:
+       case ns_t_txt:
                return true;
        }
 
@@ -1087,6 +1157,9 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
        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;
        }
@@ -1196,8 +1269,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;
        }
@@ -1295,7 +1369,6 @@ enum rwrap_lib {
     RWRAP_LIBRESOLV
 };
 
-#ifndef NDEBUG
 static const char *rwrap_str_lib(enum rwrap_lib lib)
 {
        switch (lib) {
@@ -1308,7 +1381,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)
 {
@@ -1317,7 +1389,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) {
@@ -1424,21 +1514,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
@@ -1447,23 +1529,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
@@ -1533,6 +1606,29 @@ static int libc_res_nsearch(struct __res_state *state,
  *   RES_HELPER
  ***************************************************************************/
 
+static void rwrap_reset_nameservers(struct __res_state *state)
+{
+#ifdef HAVE_RES_STATE_U_EXT_NSADDRS
+       size_t i;
+
+       for (i = 0; i < (size_t)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]);
+       }
+       memset(&state->_u._ext, 0, sizeof(state->_u._ext));
+       for (i = 0; i < MAXNS; i++) {
+               state->_u._ext.nssocks[i] = -1;
+               state->_u._ext.nsmap[i] = MAXNS + 1;
+       }
+       state->ipv6_unavail = false;
+#endif
+       memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
+       state->nscount = 0;
+}
+
 static int rwrap_parse_resolv_conf(struct __res_state *state,
                                   const char *resolv_conf)
 {
@@ -1540,6 +1636,8 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
        char buf[BUFSIZ];
        int nserv = 0;
 
+       rwrap_reset_nameservers(state);
+
        fp = fopen(resolv_conf, "r");
        if (fp == NULL) {
                RWRAP_LOG(RWRAP_LOG_ERROR,
@@ -1576,14 +1674,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
@@ -1604,11 +1701,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,
@@ -1631,6 +1728,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",
@@ -1656,21 +1760,6 @@ static int rwrap_res_ninit(struct __res_state *state)
                const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
 
                if (resolv_conf != NULL) {
-                       uint16_t i;
-
-                       (void)i; /* maybe unused */
-
-                       /* Delete name servers */
-                       state->nscount = 0;
-                       memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
-
-#ifdef HAVE_RESOLV_IPV6_NSADDRS
-                       state->_u._ext.nscount = 0;
-                       for (i = 0; i < state->_u._ext.nscount; i++) {
-                               SAFE_FREE(state->_u._ext.nsaddrs[i]);
-                       }
-#endif
-
                        rc = rwrap_parse_resolv_conf(state, resolv_conf);
                }
        }
@@ -1717,19 +1806,8 @@ int __res_init(void)
 
 static void rwrap_res_nclose(struct __res_state *state)
 {
-#ifdef HAVE_RESOLV_IPV6_NSADDRS
-       int i;
-#endif
-
+       rwrap_reset_nameservers(state);
        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)