]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
hints: do not load /etc/hosts by default
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 16 Jan 2017 13:16:42 +0000 (14:16 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 16 Jan 2017 13:25:41 +0000 (14:25 +0100)
And don't crash anymore if loaded without configuring.
Users can now avoid loading any file and instead specify hints
just via hints.set() calls.

It's perhaps still strange that hints.config(...) will drop any
hints set previously.

NEWS
modules/hints/README.rst
modules/hints/hints.c

diff --git a/NEWS b/NEWS
index 5ba6ed8eb5b673457739a19c580a31e7f44ed8f8..d666becb1cbc1744edf716ce8ca0326204b55acc 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,8 @@ Knot Resolver 1.2.0 (2016-12-yy)
 - Support CD flag on incoming requests.
 - It now requires knot >= 2.3.1 to link successfully.
 - The API+ABI for modules changed slightly.
+- hints module: previously /etc/hosts was loaded by default, but not anymore.
+  Users can now actually avoid loading any file.
 
 Knot Resolver 1.1.1 (2016-08-24)
 ================================
index 71f2e8eeb7da3b4a50e38487ac6b788589743993..88c259dd65451142114a3d421fcf1d62c1cf3bfc 100644 (file)
@@ -28,10 +28,11 @@ Properties
 
 .. function:: hints.config([path])
 
-  :param string path:  path to hosts file, default: ``"/etc/hosts"``
+  :param string path:  path to hosts file, default: no file
   :return: ``{ result: bool }``
 
-  Load specified hosts file.
+  Clear any configured hints and load specified hosts file.
+  (Root hints are not touched.)
 
 .. function:: hints.get(hostname)
 
index c1042adb38da1dd3bad1ad07f761e807f3d8d10c..0b9091d8f5f08e5d9e780d9c7d101133ce16896f 100644 (file)
@@ -34,7 +34,6 @@
 #include "lib/layer.h"
 
 /* Defaults */
-#define DEFAULT_FILE "/etc/hosts"
 #define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "hint",  fmt)
 
 /* Structure for reverse search (address to domain) */
@@ -301,7 +300,7 @@ static int load_map(struct kr_zonecut *hints, FILE *fp)
        return kr_ok();
 }
 
-static int load(struct kr_module *module, const char *path)
+static int load_file(struct kr_module *module, const char *path)
 {
        auto_fclose FILE *fp = fopen(path, "r");
        if (fp == NULL) {
@@ -311,21 +310,8 @@ static int load(struct kr_module *module, const char *path)
                VERBOSE_MSG(NULL, "reading '%s'\n", path);
        }
 
-       /* Create pool and copy itself */
-       knot_mm_t _pool = {
-               .ctx = mp_new(4096),
-               .alloc = (knot_mm_alloc_t) mp_alloc
-       };
-       knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool));
-       if (!pool) {
-               return kr_error(ENOMEM);
-       }
-       memcpy(pool, &_pool, sizeof(*pool));
-
        /* Load file to map */
-       struct kr_zonecut *hints = mm_alloc(pool, sizeof(*hints));
-       kr_zonecut_init(hints, (const uint8_t *)(""), pool);
-       module->data = hints;
+       struct kr_zonecut *hints = module->data;
        return load_map(hints, fp);
 }
 
@@ -349,6 +335,8 @@ static void unload(struct kr_module *module)
 static char* hint_set(void *env, struct kr_module *module, const char *args)
 {
        struct kr_zonecut *hints = module->data;
+       if (!args)
+               return NULL;
        auto_free char *args_copy = strdup(args);
 
        int ret = -1;
@@ -406,11 +394,15 @@ static char* pack_hints(struct kr_zonecut *hints);
  * Retrieve address hints, either for given name or for all names.
  *
  * Input:  name
- * Output: { address1, address2, ... }
+ * Output: NULL or "{ address1, address2, ... }"
  */
 static char* hint_get(void *env, struct kr_module *module, const char *args)
 {
        struct kr_zonecut *hints = module->data;
+       if (!hints) {
+               assert(false);
+               return NULL;
+       }
 
        if (!args) {
                return pack_hints(hints);
@@ -509,21 +501,51 @@ const kr_layer_api_t *hints_layer(struct kr_module *module)
        return &_layer;
 }
 
+
+/** Basic initialization: get a memory pool, etc. */
 KR_EXPORT
 int hints_init(struct kr_module *module)
 {
-       module->data = NULL;
-       return 0;
+       /* Create pool and copy itself */
+       knot_mm_t _pool = {
+               .ctx = mp_new(4096),
+               .alloc = (knot_mm_alloc_t) mp_alloc
+       };
+       knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool));
+       if (!pool) {
+               return kr_error(ENOMEM);
+       }
+       memcpy(pool, &_pool, sizeof(*pool));
+
+       struct kr_zonecut *hints = mm_alloc(pool, sizeof(*hints));
+       if (!hints) {
+               mp_delete(pool->ctx);
+               return kr_error(ENOMEM);
+       }
+       hints->pool = pool;
+       kr_zonecut_init(hints, (const uint8_t *)(""), pool);
+       module->data = hints;
+
+       return kr_ok();
 }
 
+/** Drop all hints, and load a hosts file if any was specified.
+ *
+ * It seems slightly strange to drop all, but keep doing that for now.
+ */
 KR_EXPORT
 int hints_config(struct kr_module *module, const char *conf)
 {
        unload(module);
-       if (!conf || strlen(conf) < 1) {
-               conf = DEFAULT_FILE;
+       int err = hints_init(module);
+       if (err != kr_ok()) {
+               return err;
        }
-       return load(module, conf);
+
+       if (conf && conf[0]) {
+               return load_file(module, conf);
+       }
+       return kr_ok();
 }
 
 KR_EXPORT