]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
roothints: various changes
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 11 Sep 2017 16:38:42 +0000 (18:38 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 11 Sep 2017 16:46:15 +0000 (18:46 +0200)
- expose the function as hints.root_file
- use the same filename as Debian
- remove the unneeded script
- docs and some nitpicks

NEWS
config.mk
daemon/daemon.mk
daemon/engine.c
daemon/engine.h
daemon/lua/config.lua [moved from daemon/lua/config.lua.in with 94% similarity]
etc/etc.mk
etc/root.hints [moved from etc/hints.zone with 100% similarity]
modules/hints/README.rst
modules/hints/hints.c
scripts/gen-root-hints.sh [deleted file]

diff --git a/NEWS b/NEWS
index 7b577935a96c9edffcb50062ab606f681b7840da..89d0e1646d664198191dc3f6aa21b3a49eb822c6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@ Incompatible changes
 Improvements
 ------------
 - policy.suffix: update the aho-corasick code (#200)
+- root hints are now loaded from a zonefile; exposed as hints.root_file().
+  You can override the path by defining ROOTHINTS during compilation.
 
 
 Knot Resolver 1.3.3 (2017-08-09)
index 7ed32b191f785933105d7b5d018fc1db0ec56292..820166ac404dad7482affa08cc45499dee0d5e6b 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -19,7 +19,7 @@ MANDIR ?= $(PREFIX)/share/man
 INCLUDEDIR ?= $(PREFIX)/include
 MODULEDIR ?= $(LIBDIR)/kdns_modules
 ETCDIR ?= $(PREFIX)/etc/kresd
-ROOTHINTS ?= $(ETCDIR)/hints.zone
+ROOTHINTS ?= $(ETCDIR)/root.hints
 
 # Tools
 CC      ?= cc
index 9fe5b6af213b8797e111f62d94bff22b31fbdf62..12ccc63036f11505ad5735b6b155dbb5aa5fe277 100644 (file)
@@ -31,6 +31,7 @@ LUA_HAS_SETFUNCS := \
 kresd_CFLAGS := -fPIE \
                -Dlibknot_SONAME=\"$(libknot_SONAME)\" \
                -Dlibzscanner_SONAME=\"$(libzscanner_SONAME)\" \
+               -DROOTHINTS=\"$(ROOTHINTS)\" \
                -DLUA_HAS_SETFUNCS="$(LUA_HAS_SETFUNCS)"
 kresd_DEPEND := $(libkres) $(contrib)
 kresd_LIBS := $(libkres_TARGET) $(contrib_TARGET) $(libknot_LIBS) \
@@ -59,7 +60,7 @@ ifneq ($(SED),)
 endif
 daemon-clean: kresd-clean
        @$(RM) daemon/lua/*.inc daemon/lua/kres.lua daemon/lua/trust_anchors.lua \
-               daemon/lua/zonefile.lua daemon/lua/config.lua
+               daemon/lua/zonefile.lua
 
 KNOT_RRSET_TXT_DUMP := \
        $(shell pkg-config libknot --atleast-version=2.4.0 && echo true || echo false)
@@ -69,9 +70,6 @@ daemon/lua/kres.lua: daemon/lua/kres.lua.in
 daemon/lua/trust_anchors.lua: daemon/lua/trust_anchors.lua.in
        @$(call quiet,SED,$<) -e "s|@ETCDIR@|$(ETCDIR)|g" $< > $@
 
-daemon/lua/config.lua: daemon/lua/config.lua.in
-       @$(call quiet,SED,$<) -e "s|@ROOTHINTS@|$(ROOTHINTS)|g" $< > $@
-
 LIBZSCANNER_COMMENTS := \
        $(shell pkg-config libzscanner --atleast-version=2.4.2 && echo true || echo false)
 daemon/lua/zonefile.lua: daemon/lua/zonefile.lua.in
index 5ba24796659dbebd54ef3e415aeef94a53477643..d18c6e6d115e2f0ec833d8436b5d57254d2f3706 100644 (file)
@@ -320,52 +320,61 @@ static int l_trustanchor(lua_State *L)
        return 1;
 }
 
-/** @internal for l_roothints */
+/** @internal for l_hints_root_file */
 static void roothints_add(zs_scanner_t *zs)
 {
        struct kr_zonecut *hints = zs->process.data;
        if (!hints) {
                return;
        }
-       if(zs->r_type == KNOT_RRTYPE_A || zs->r_type == KNOT_RRTYPE_AAAA) {
+       if (zs->r_type == KNOT_RRTYPE_A || zs->r_type == KNOT_RRTYPE_AAAA) {
                knot_rdata_t rdata[RDATA_ARR_MAX];
                knot_rdata_init(rdata, zs->r_data_length, zs->r_data, zs->r_ttl);
-               kr_zonecut_add(hints,zs->r_owner, rdata);
-       }               
+               kr_zonecut_add(hints, zs->r_owner, rdata);
+       }
 }
 
 /** Load root hints from zonefile. */
-static int l_roothints(lua_State *L)
+static int l_hint_root_file(lua_State *L)
 {
        struct engine *engine = engine_luaget(L);
        struct kr_context *ctx = &engine->resolver;
-       struct kr_zonecut *root_hints = &ctx->root_hints;
        const char *file = lua_tostring(L, 1);
-       if (!file || strlen(file) == 0) {
-               return 0;
-       }
 
-       zs_scanner_t *zs = malloc(sizeof(*zs));
-       if (!zs || zs_init(zs, ".", 1, 0) != 0) {
-               free(zs);
-               lua_pushstring(L, "not enough memory");
+       const char *err = lua_hint_root_file(ctx, file);
+       if (err) {
+               lua_pushstring(L, err);
                lua_error(L);
+       } else {
+               lua_pushboolean(L, true);
+               return 1;
        }
-               
-       if (zs_set_input_file(zs, file) != 0) {
-               free(zs);
-               lua_pushstring(L, "failed to open root hints file");
-               lua_error(L);
+}
+
+const char* lua_hint_root_file(struct kr_context *ctx, const char *file)
+{
+       if (!file) {
+               file = ROOTHINTS;
+       }
+       if (strlen(file) == 0 || !ctx) {
+               return "invalid parameters";
+       }
+       struct kr_zonecut *root_hints = &ctx->root_hints;
+
+       zs_scanner_t zs;
+       if (zs_init(&zs, ".", 1, 0) != 0) {
+               return "not enough memory";
        }
-       
+       if (zs_set_input_file(&zs, file) != 0) {
+               return "failed to open root hints file";
+       }
+
        kr_zonecut_set(root_hints, (const uint8_t *)"");
-       zs_set_processing(zs, roothints_add, NULL, root_hints);
-       zs_parse_all(zs);
-       
-       lua_pushboolean(L, true);
-       free(zs);
-       return 1;
+       zs_set_processing(&zs, roothints_add, NULL, root_hints);
+       zs_parse_all(&zs);
+       return NULL;
 }
+
 /** Unpack JSON object to table */
 static void l_unpack_json(lua_State *L, JsonNode *table)
 {
@@ -622,8 +631,8 @@ static int init_state(struct engine *engine)
        lua_setglobal(engine->L, "user");
        lua_pushcfunction(engine->L, l_trustanchor);
        lua_setglobal(engine->L, "trustanchor");
-       lua_pushcfunction(engine->L, l_roothints);
-       lua_setglobal(engine->L, "roothints");
+       lua_pushcfunction(engine->L, l_hint_root_file);
+       lua_setglobal(engine->L, "_hint_root_file");
        lua_pushliteral(engine->L, libknot_SONAME);
        lua_setglobal(engine->L, "libknot_SONAME");
        lua_pushliteral(engine->L, libzscanner_SONAME);
index 3497962abccb5aaaafa081559469425e22c99722..b85bea3b2242c037a5ea3ef0031ec86129e23ea3 100644 (file)
@@ -101,3 +101,10 @@ int engine_set_hostname(struct engine *engine, const char *hostname);
 /** Set/get the per engine moduledir */
 char *engine_get_moduledir(struct engine *engine);
 int engine_set_moduledir(struct engine *engine, const char *moduledir);
+
+/** Load root hints from a zonefile (or config-time default if NULL).
+ *
+ * @return error message or NULL (statically allocated)
+ */
+const char* lua_hint_root_file(struct kr_context *ctx, const char *file);
+
similarity index 94%
rename from daemon/lua/config.lua.in
rename to daemon/lua/config.lua
index b201a466882e8402d31a4006118d5986721ebe50..91babeb5b503f837e2b60c39ab9935a624d1b166 100644 (file)
@@ -16,5 +16,5 @@ if not cache.current_size then
 end
 
 if kres.context().root_hints.nsset.root == nil then
-       roothints('@ROOTHINTS@')
+       _hint_root_file()
 end
index 04d484bb18080645ce27de7e75a67a657f7b4518..7aa6cb6c16ec5356c1c2b2acfecd7c71f82c4cb9 100644 (file)
@@ -3,14 +3,14 @@ etc_SOURCES := icann-ca.pem \
        config.isp \
        config.personal \
        config.splitview \
-       hints.zone
+       root.hints
 
 etc-install: $(DESTDIR)$(ETCDIR)
        $(INSTALL) -m 0640 $(addprefix etc/,$(etc_SOURCES)) $(DESTDIR)$(ETCDIR)
 
-etc: etc/hints.zone
+etc: etc/root.hints
 
-etc/hints.zone:
+etc/root.hints:
        wget -O $@  https://www.internic.net/domain/named.root
 
 .PHONY: etc-install
similarity index 100%
rename from etc/hints.zone
rename to etc/root.hints
index 31be9bc6a410f4c36be89a5096818a8bfcfccc66..e37b1580f5fca43cd040c87319122ef8b289440c 100644 (file)
@@ -79,6 +79,10 @@ Properties
 
   .. tip:: If no parameters are passed, returns current root hints set.
 
+.. function:: hints.root_file(path)
+
+  Replace current root hints from a zonefile.  If the path is omitted, the compiled-in path is used, i.e. the root hints are reset to the default.
+
 .. function:: hints.root(root_hints)
 
   :param table root_hints: new set of root hints i.e. ``{['name'] = 'addr', ...}``
index 31969c1a5e93a071bea80569fec9caf2cbd389cf..c20f1c8dfd899fe696eb2ae0d689afc3934d80af 100644 (file)
@@ -565,6 +565,13 @@ static char* hint_root(void *env, struct kr_module *module, const char *args)
        return pack_hints(root_hints);
 }
 
+static char* hint_root_file(void *env, struct kr_module *module, const char *args)
+{
+       struct engine *engine = env;
+       struct kr_context *ctx = &engine->resolver;
+       return (char*)lua_hint_root_file(ctx, args);
+}
+
 /*
  * Module implementation.
  */
@@ -650,6 +657,7 @@ struct kr_prop *hints_props(void)
            { &hint_get,    "get", "Retrieve hint for given name.", },
            { &hint_add_hosts, "add_hosts", "Load a file with hosts-like formatting and add contents into hints.", },
            { &hint_root,   "root", "Replace root hints set (empty value to return current list).", },
+           { &hint_root_file, "root_file", "Replace root hints set from a zonefile.", },
            { NULL, NULL, NULL }
        };
        return prop_list;
diff --git a/scripts/gen-root-hints.sh b/scripts/gen-root-hints.sh
deleted file mode 100755 (executable)
index fb78356..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/sh -e
-
-echo "/* generated root hints */"
-
-for atype in A AAAA; do
-       # address length when using \xNN escapes
-       if [ "$atype" = A ]; then
-               alen=16
-       elif [ "$atype" = AAAA ]; then
-               alen=64
-       else
-               exit 1
-       fi
-
-       for n in a b c d e f g h i j k l m; do
-               ip="$(kdig "$atype" "$n.root-servers.net." +dnssec +short)"
-               ip_hex="$("$(dirname "$0")"/inet_pton.py "$ip")"
-               [ "$(printf "%s" "$ip_hex" | wc -c)" = "$alen" ] || exit 1
-               echo "#define HINT_${n}_${atype} \"$ip_hex\""
-       done
-done
-