From 8fa8f214f8354f55ffb21277d7dc8a5c29a8ea8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Mon, 11 Sep 2017 18:38:42 +0200 Subject: [PATCH] roothints: various changes - expose the function as hints.root_file - use the same filename as Debian - remove the unneeded script - docs and some nitpicks --- NEWS | 2 + config.mk | 2 +- daemon/daemon.mk | 6 +-- daemon/engine.c | 63 ++++++++++++++---------- daemon/engine.h | 7 +++ daemon/lua/{config.lua.in => config.lua} | 2 +- etc/etc.mk | 6 +-- etc/{hints.zone => root.hints} | 0 modules/hints/README.rst | 4 ++ modules/hints/hints.c | 8 +++ scripts/gen-root-hints.sh | 22 --------- 11 files changed, 64 insertions(+), 58 deletions(-) rename daemon/lua/{config.lua.in => config.lua} (94%) rename etc/{hints.zone => root.hints} (100%) delete mode 100755 scripts/gen-root-hints.sh diff --git a/NEWS b/NEWS index 7b577935a..89d0e1646 100644 --- 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) diff --git a/config.mk b/config.mk index 7ed32b191..820166ac4 100644 --- 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 diff --git a/daemon/daemon.mk b/daemon/daemon.mk index 9fe5b6af2..12ccc6303 100644 --- a/daemon/daemon.mk +++ b/daemon/daemon.mk @@ -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 diff --git a/daemon/engine.c b/daemon/engine.c index 5ba247966..d18c6e6d1 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -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); diff --git a/daemon/engine.h b/daemon/engine.h index 3497962ab..b85bea3b2 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -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); + diff --git a/daemon/lua/config.lua.in b/daemon/lua/config.lua similarity index 94% rename from daemon/lua/config.lua.in rename to daemon/lua/config.lua index b201a4668..91babeb5b 100644 --- a/daemon/lua/config.lua.in +++ b/daemon/lua/config.lua @@ -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 diff --git a/etc/etc.mk b/etc/etc.mk index 04d484bb1..7aa6cb6c1 100644 --- a/etc/etc.mk +++ b/etc/etc.mk @@ -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 diff --git a/etc/hints.zone b/etc/root.hints similarity index 100% rename from etc/hints.zone rename to etc/root.hints diff --git a/modules/hints/README.rst b/modules/hints/README.rst index 31be9bc6a..e37b1580f 100644 --- a/modules/hints/README.rst +++ b/modules/hints/README.rst @@ -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', ...}`` diff --git a/modules/hints/hints.c b/modules/hints/hints.c index 31969c1a5..c20f1c8df 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -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 index fb7835668..000000000 --- a/scripts/gen-root-hints.sh +++ /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 - -- 2.47.2