diff --git a/Makefile b/Makefile index da381780206ae3b51a846e929c75dfd5e5c20f88..5f9d7ffc84fa7926477c0d19be38c657093fa2ce 100644 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ endif # Overview info: - $(info Target: Knot DNS Resolver $(MAJOR).$(MINOR).$(PATCH)-$(PLATFORM)) + $(info Target: Knot DNS Resolver $(VERSION)-$(PLATFORM)) $(info Compiler: $(CC) $(BUILD_CFLAGS)) $(info ) $(info Variables) diff --git a/config.mk b/config.mk index 9a63fe85abc2b2fc3a6009bcc10a02bd903b2437..fbed46eaa1ec456d94fa408a9a5772da52cb35d5 100644 --- a/config.mk +++ b/config.mk @@ -6,6 +6,8 @@ ABIVER := 1 BUILDMODE := dynamic HARDENING := yes +VERSION := $(MAJOR).$(MINOR).$(PATCH) + # Paths PREFIX ?= /usr/local BINDIR ?= $(PREFIX)/bin @@ -27,7 +29,7 @@ INSTALL := install # Flags BUILD_LDFLAGS += $(LDFLAGS) BUILD_CFLAGS := $(CFLAGS) -std=c99 -D_GNU_SOURCE -Wno-unused -Wtype-limits -Wformat -Wformat-security -Wall -I$(abspath .) -I$(abspath lib/generic) -I$(abspath contrib) -I$(abspath contrib/lmdb) -BUILD_CFLAGS += -DPACKAGE_VERSION="\"$(MAJOR).$(MINOR).$(PATCH)\"" -DPREFIX="\"$(PREFIX)\"" -DMODULEDIR="\"$(MODULEDIR)\"" -DETCDIR="\"$(ETCDIR)\"" +BUILD_CFLAGS += -DPACKAGE_VERSION="\"$(VERSION)\"" -DPREFIX="\"$(PREFIX)\"" -DMODULEDIR="\"$(MODULEDIR)\"" -DETCDIR="\"$(ETCDIR)\"" ifeq (,$(findstring -O,$(CFLAGS))) BUILD_CFLAGS += -O2 endif diff --git a/daemon/daemon.mk b/daemon/daemon.mk index 10bb69781e8ccac17748a2b402df96481e860a4b..24c196a0f4aecf5db2387ec9a572881568f90c42 100644 --- a/daemon/daemon.mk +++ b/daemon/daemon.mk @@ -45,7 +45,7 @@ date := $(shell date +%F) daemon: $(kresd) daemon-install: kresd-install bindings-install ifneq ($(SED),) - $(SED) -e "s/@VERSION@/$(MAJOR).$(MINOR).$(PATCH)/" -e "s/@DATE@/$(date)/" doc/kresd.8.in > doc/kresd.8 + $(SED) -e "s/@VERSION@/$(VERSION)/" -e "s/@DATE@/$(date)/" doc/kresd.8.in > doc/kresd.8 $(INSTALL) -d -m 0755 $(DESTDIR)$(MANDIR)/man8/ $(INSTALL) -m 0644 doc/kresd.8 $(DESTDIR)$(MANDIR)/man8/ endif diff --git a/doc/modules.rst b/doc/modules.rst index 2b92c438826c9e3359d65592ec44a214f646a004..fb0600901b70034996358faa0b92f0751b6f9bfc 100644 --- a/doc/modules.rst +++ b/doc/modules.rst @@ -22,3 +22,4 @@ Knot DNS Resolver modules .. include:: ../modules/dns64/README.rst .. include:: ../modules/renumber/README.rst .. include:: ../modules/cookies/README.rst +.. include:: ../modules/version/README.rst diff --git a/lib/lib.mk b/lib/lib.mk index ae72f5ddd14a682b1a33d89fbd20b42aa8f0317a..01857b8ec032f92bb6411e9ee79ec33925040582 100644 --- a/lib/lib.mk +++ b/lib/lib.mk @@ -79,7 +79,7 @@ libkres.pc: @echo 'Name: libkres' >> $@ @echo 'Description: Knot DNS Resolver library' >> $@ @echo 'URL: https://www.knot-resolver.cz' >> $@ - @echo 'Version: $(MAJOR).$(MINOR).$(PATCH)' >> $@ + @echo 'Version: $(VERSION)' >> $@ @echo 'Libs: -L$${libdir} -lkres' >> $@ @echo 'Cflags: -I$${includedir}' >> $@ libkres-pcinstall: libkres.pc libkres-install diff --git a/modules/modules.mk b/modules/modules.mk index 2a55751098e64ac3876bddd8bef862c26106745a..d6077ebc713c2496163c71970cb0f35c4e651cb0 100644 --- a/modules/modules.mk +++ b/modules/modules.mk @@ -26,7 +26,8 @@ modules_TARGETS += ketcd \ dns64 \ renumber \ http \ - daf + daf \ + version endif # Make C module diff --git a/modules/version/README.rst b/modules/version/README.rst new file mode 100644 index 0000000000000000000000000000000000000000..152d1e7aa1800620e3504da67c9e83c91398d9bc --- /dev/null +++ b/modules/version/README.rst @@ -0,0 +1,22 @@ +.. _mod-version: + +Version +------- + +Module checks for new version and CVE_, and issues warning messages. + +Configuration +^^^^^^^^^^^^^ +.. code-block:: lua + + version.config(2*day) + -- configure period of check (defaults to 1*day) + +Running +^^^^^^^ + +.. code-block:: lua + + modules.load("version") + +.. _cve: https://cve.mitre.org/ diff --git a/modules/version/version.lua.in b/modules/version/version.lua.in new file mode 100644 index 0000000000000000000000000000000000000000..461d1932911b448f02bd0fba7ef2baae3db6d28e --- /dev/null +++ b/modules/version/version.lua.in @@ -0,0 +1,107 @@ +local M = {} + +local function getLastWord(str) + local space = 1 + for i=#str, 1, -1 do + if str:sub(i,i) == " " then + space = i + break + end + end + return str:sub(space+1, #str) +end + +--Converts string of HEX digits to string +local function hex2string(hex) + local str = "" + for i=1, #hex, 2 do + local ascii = tonumber(hex:sub(i,i+1), 16) + str = str .. string.char(ascii) + end + return str +end + +local function parseCVE(str) + local first + local last + first, last = str:find("CVE") + local position = last+2 + return str:sub(position,-1) +end + +local function parseVersion(str) + local branch = "stable" + local first + local last + first, last = str:find(branch) + local position = last+3 + local delimiter = #str + if str:find("|",position) then + delimiter = str:find("|",position)-1 + end + return str:sub(position, delimiter) +end + +--Parses version from server and compares it to the installed one +local function parse(record) + local output = "" + local str = getLastWord(kres.rr2str(record)) + str = hex2string(str) + local CVE = parseCVE(str) + local version = parseVersion(str) + local localVersion = '@VERSION@' + if version ~= localVersion then + output = output .. string.format( + "[version] Current version of Knot DNS Resolver is different from the latest stable one available." + .. " (Current: %s, Latest stable: %s)\n", + localVersion, version) + if CVE ~= "N/A" then + output = output .. string.format("[version] CVE: %s\n", CVE) + end + end + io.write(output) +end + +--Parses record from answer +local function request (answer) + local pkt = kres.pkt_t(answer) + if pkt:rcode() == kres.rcode.NOERROR then + parse(pkt:section(kres.section.ANSWER)[1]) + else + print ('Request for version ended with rcode: ', pkt:rcode()) + return + end +end + +local function callhome() + resolve('et.knot-resolver.cz', kres.type.TXT, kres.class.IN, 0, request) +end + +function M.config(period) + if period == nil then + print("Expected number of miliseconds. Using default version.config(1*day)") + return + end + if type(period) ~= "number" then + print("Expected number of miliseconds. Using default version.config(1*day)") + return + end + version.period = period + print(period) + if M.ev then event.cancel(M.ev) end + M.ev = event.recurrent(M.period, callhome) +end + + +function M.init() + if period == nil then + M.period = 1*day + end + M.ev = event.recurrent(M.period, callhome) +end + +function M.deinit() + if M.ev then event.cancel(M.ev) end +end + +return M \ No newline at end of file diff --git a/modules/version/version.mk b/modules/version/version.mk new file mode 100644 index 0000000000000000000000000000000000000000..624c7c027fe8f6a25b76019a2749bd473865efdc --- /dev/null +++ b/modules/version/version.mk @@ -0,0 +1,6 @@ +version_SOURCES := version.lua + +modules/version/version.lua: modules/version/version.lua.in + $(SED) -e "s/@VERSION@/$(VERSION)/" < "$<" > "$@" + +$(call make_lua_module,version) diff --git a/scripts/kresd-query.lua b/scripts/kresd-query.lua index 4f28390ac05754956326147e13e072277088aba2..c2d54689408b886bbe834dd2e56f8319c4c42c13 100755 --- a/scripts/kresd-query.lua +++ b/scripts/kresd-query.lua @@ -23,7 +23,7 @@ local function help() name = 'kresd-query.lua' print(string.format('Usage: %s [-t type] [-c class] [-C config] <name> <script>', name)) print('Execute a single-shot query and run a script on the result.') - print('There are two variable available: pkt (kres.pkt_t), req (kres.request_t)') + print('There are two variables available: pkt (kres.pkt_t), req (kres.request_t)') print('See modules README to learn about their APIs.') print('') print('Options:')