]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
utils/upgrade-4-to-5: socket->net_listen conversion script
authorTomas Krizek <tomas.krizek@nic.cz>
Mon, 25 Nov 2019 15:49:01 +0000 (16:49 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Fri, 24 Jan 2020 12:19:04 +0000 (13:19 +0100)
utils/meson.build
utils/upgrade/meson.build [new file with mode: 0644]
utils/upgrade/upgrade-4-to-5.lua.in [new file with mode: 0644]

index fbf8f33c9e4935737b29a1bd6e89fb90ec5cc49c..6ef9c376c50ac93544f15ba6a2fd0f1f03a84ec6 100644 (file)
@@ -4,3 +4,4 @@ build_utils = get_option('utils') != 'disabled'
 
 subdir('client')
 subdir('cache_gc')
+subdir('upgrade')
diff --git a/utils/upgrade/meson.build b/utils/upgrade/meson.build
new file mode 100644 (file)
index 0000000..19cc420
--- /dev/null
@@ -0,0 +1,11 @@
+## utils/upgrade
+
+upgrade_config = configuration_data()
+upgrade_config.set('etc_dir', etc_dir)
+
+configure_file(
+  input: 'upgrade-4-to-5.lua.in',
+  output: 'upgrade-4-to-5.lua',
+  configuration: upgrade_config,
+  install_dir: lib_dir
+)
diff --git a/utils/upgrade/upgrade-4-to-5.lua.in b/utils/upgrade/upgrade-4-to-5.lua.in
new file mode 100644 (file)
index 0000000..ad6c621
--- /dev/null
@@ -0,0 +1,85 @@
+local upg_dir = '@etc_dir@/.upgrade-4-to-5'
+local out = '@etc_dir@/kresd.conf'
+local sockets = {
+       { file='kresd.socket', kind='dns' },
+       { file='kresd-tls.socket', kind='tls' },
+       { file='kresd-doh.socket', kind='doh' },
+       { file='kresd-webmgmt.socket', kind='webmgmt' },
+}
+
+-- globals
+addr_port = {}
+outfile = io.open(out, 'a')
+
+if outfile == nil then
+       -- this is technicaly an error, but upgrade script should fail in scriptlets
+       os.exit(0)  -- make no changes and exit
+end
+
+outfile:write("\n-- START OF AUTOGENERATED BLOCK (upgrade-4-to-5)\n")
+outfile:write("-- This block was automatically created during upgrade\n")
+outfile:write("-- Please verify these settings match your desired configuration\n")
+outfile:write("-- See https://knot-resolver.readthedocs.io/en/stable/upgrading.html\n")
+
+local function write_net_listen(addr, port, kind)
+       -- make sure (addr, port) combination is unique
+       for _, val in ipairs(addr_port) do
+               if val.addr == addr and val.port == port then
+                       return
+               end
+       end
+
+       table.insert(addr_port, { addr=addr, port=port })
+       outfile:write(
+               "net.listen('"..addr.."', "..tostring(port)..
+               ", { kind = '"..kind.."', freebind = true })\n")
+end
+
+local function convert(line, kind, ipv6only)
+       local patterns = {
+               '^[^=]+=(%d+%.%d+%.%d+%.%d+):(%d+)',  -- IPv4
+               '^[^=]+=%[([^%]]+)%]:(%d+)',  -- IPv6
+               '^[^=]+=(/.*)',  -- UNIX
+       }
+
+       -- Datagram is either implied (dns) or unsupported (tls/doh/webmgmt)
+       if not line:match('^Listen.*Stream') then
+               return
+       end
+
+       for _, pattern in ipairs(patterns) do
+               local addr, port = line:match(pattern)
+               if addr ~= nil then
+                       write_net_listen(addr, port, kind)
+                       if not ipv6only then
+                               if addr:match('^::$') then
+                                       write_net_listen('0.0.0.0', port, kind)
+                               end
+                               if addr:match('^::1$') then
+                                       write_net_listen('127.0.0.1', port, kind)
+                               end
+                       end
+               end
+       end
+       return
+end
+
+for _, socket in pairs(sockets) do
+       local ipv6only = false
+       local ipv6only_f = io.open(upg_dir..'/'..socket.file..'.v6only', 'r')
+       if ipv6only_f ~= nil then
+               ipv6only = true
+               io.close(ipv6only_f)
+       end
+       local sockinfo = io.open(upg_dir..'/'..socket.file, 'r')
+       if sockinfo ~= nil then
+               for line in sockinfo:lines() do
+                       convert(line, socket.kind, ipv6only)
+               end
+       end
+end
+
+outfile:write("-- END OF AUTOGENERATED BLOCK (upgrade-4-to-5)\n")
+
+io.close(outfile)
+os.exit(0)