annotate mod_dnsupdate/mod_dnsupdate.lua @ 4764:a754f7e380b2

mod_dnsupdate: Rewrite port config vs DNS comparison code I'm not sure if it was correct, which means it was hard to understand and thus needed to be simplified. Hope this accomplishes that.
author Kim Alvefur <zash@zash.se>
date Mon, 08 Nov 2021 23:03:14 +0100
parents 91077c928c57
children 3632836f35b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4762
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 module:set_global();
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local config = require "core.configmanager";
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local argparse = require "util.argparse";
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local dns = require"net.adns".resolver();
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local async = require "util.async";
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local set = require "util.set";
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local nameprep = require"util.encodings".stringprep.nameprep;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local idna_to_ascii = require"util.encodings".idna.to_ascii;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local services = { "xmpp-client"; "xmpps-client"; "xmpp-server"; "xmpps-server" }
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local function validate_dnsname_option(options, option_name, default)
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local host = options[option_name];
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if host == nil then return default end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local normalized = nameprep(host);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 if not normalized then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 module:log("error", "--%s %q fails normalization");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 return;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local alabel = idna_to_ascii(normalized);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 if not alabel then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 module:log("error", "--%s %q fails IDNA");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 return;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return alabel;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 function module.command(arg)
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local opts = argparse.parse(arg, {
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 short_params = { d = "domain"; p = "primary"; t = "target"; l = "ttl"; h = "help"; ["?"] = "help" };
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 value_params = { domain = true; primary = true; target = true; ttl = true };
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 });
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if not arg[1] or arg[2] or not opts or opts.help or not opts.domain then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local out = opts.help and io.stdout or io.stderr;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 out:write("prosodyctl mod_dnsupdate [options] virtualhost\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 out:write("\t-d --domain\tbase domain name *required*\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 out:write("\t-p --primary\tprimary DNS name server\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 out:write("\t-t --target\ttarget hostname for SRV\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 out:write("\t-l --ttl\tTTL to use\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 out:write("\t--each\tremove and replace individual SRV records\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 out:write("\t--reset\tremove and replace all SRV records\n");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return opts and opts.help and 0 or 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local vhost = nameprep(arg[1]); -- TODO loop over arg[]?
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if not vhost then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 module:log("error", "Host %q fails normalization", arg[1]);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 return 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local ihost = idna_to_ascii(vhost);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if not ihost then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 module:log("error", "Host %q falis IDNA", vhost);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 if not config.get(vhost, "defined") then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 module:log("error", "Host %q is not defined in the config", vhost);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local domain = validate_dnsname_option(opts, "domain");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if not domain then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 module:log("error", "--domain is required");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 return 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local primary = validate_dnsname_option(opts, "primary")
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 or async.wait_for(dns:lookup_promise(domain, "SOA"):next(function(ret) return ret[1].soa.mname; end));
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if not primary then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 module:log("error", "Could not discover primary name server, specify it with --primary");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 return 1;
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local target = validate_dnsname_option(opts, "target", module:context(vhost):get_option_string("xmpp_host", ihost));
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 -- TODO validate that target has A/AAAA
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local configured_ports = {
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 ["xmpp-client"] = module:get_option_array("c2s_ports", { 5222 });
4763
91077c928c57 mod_dnsupdate: Fix to use correct port settings
Kim Alvefur <zash@zash.se>
parents: 4762
diff changeset
78 ["xmpp-server"] = module:get_option_array("s2s_ports", { 5269 });
4762
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 ["xmpps-client"] = module:get_option_array("c2s_direct_tls_ports", {});
4763
91077c928c57 mod_dnsupdate: Fix to use correct port settings
Kim Alvefur <zash@zash.se>
parents: 4762
diff changeset
80 ["xmpps-server"] = module:get_option_array("s2s_direct_tls_ports", {});
4762
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 };
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if opts.multiplex then
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 for opt, ports in pairs(configured_ports) do
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 ports:append(module:get_option_array(opt:sub(1, 5) == "xmpps" and "ssl_ports" or "ports", {}));
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 local existing_srv = {};
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 for _, service in ipairs(services) do
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 existing_srv[service] = dns:lookup_promise(("_%s._tcp.%s"):format(service, ihost), "SRV");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 print("zone", domain);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 print("server", primary);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 print("ttl " .. tostring(opts.ttl or 60 * 60));
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 for _, service in ipairs(services) do
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 local ports = set.new(configured_ports[service]);
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 local records = (async.wait_for(existing_srv[service]));
4764
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
101 if opts.reset then
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
102 print(("del _%s._tcp.%s IN SRV"):format(service, ihost));
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
103 else
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
104 for _, rr in ipairs(records) do
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
105 if ports:contains(rr.srv.port) and target == nameprep(rr.srv.target):gsub("%.$", "") then
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
106 ports:remove(rr.srv.port)
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
107 else
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
108 print(("del _%s._tcp.%s IN SRV %s"):format(service, ihost, rr));
4762
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
4764
a754f7e380b2 mod_dnsupdate: Rewrite port config vs DNS comparison code
Kim Alvefur <zash@zash.se>
parents: 4763
diff changeset
112 for port in ports do print(("add _%s._tcp.%s IN SRV 1 1 %d %s"):format(service, ihost, port, target)); end
4762
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 print("show");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 print("send");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 print("answer");
ba312cd7907f mod_dnsupdate: Use with nsupdate to update DNS records from config
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end