2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local hosts = _G.hosts;
11 local log = module._log;
13 local st = require "util.stanza";
14 local sha256_hash = require "util.hashes".sha256;
15 local nameprep = require "util.encodings".stringprep.nameprep;
16 local uuid_gen = require"util.uuid".generate;
18 local xmlns_stream = "http://etherx.jabber.org/streams";
20 local dialback_requests = setmetatable({}, { __mode = 'v' });
22 local dialback_secret = module.host .. module:get_option_string("dialback_secret", uuid_gen());
24 function module.save()
25 return { dialback_secret = dialback_secret };
28 function module.restore(state)
29 dialback_secret = state.dialback_secret;
32 function generate_dialback(id, to, from)
33 return sha256_hash(id..to..dialback_secret, true);
36 function initiate_dialback(session)
37 -- generate dialback key
38 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
39 session.sends2s(st.stanza("db:result", { from = session.from_host, to = session.to_host }):text(session.dialback_key));
40 session.log("debug", "sent dialback key on outgoing s2s stream");
43 function verify_dialback(id, to, from, key)
44 return key == generate_dialback(id, to, from);
47 module:hook("stanza/jabber:server:dialback:verify", function(event)
48 local origin, stanza = event.origin, event.stanza;
50 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
51 -- We are being asked to verify the key, to ensure it was generated by us
52 origin.log("debug", "verifying that dialback key is ours...");
53 local attr = stanza.attr;
55 module:log("warn", "Ignoring incoming session from %s claiming a dialback key for %s is %s",
56 origin.from_host or "(unknown)", attr.from or "(unknown)", attr.type);
59 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
60 --if attr.from ~= origin.to_host then error("invalid-from"); end
62 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
66 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
68 origin.log("debug", "verified dialback key... it is %s", type);
69 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
74 module:hook("stanza/jabber:server:dialback:result", function(event)
75 local origin, stanza = event.origin, event.stanza;
77 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
78 -- he wants to be identified through dialback
79 -- We need to check the key with the Authoritative server
80 local attr = stanza.attr;
81 local to, from = nameprep(attr.to), nameprep(attr.from);
84 -- Not a host that we serve
85 origin.log("warn", "%s tried to connect to %s, which we don't serve", from, to);
86 origin:close("host-unknown");
89 origin:close("improper-addressing");
92 origin.hosts[from] = { dialback_key = stanza[1] };
94 dialback_requests[from.."/"..origin.streamid] = origin;
96 -- COMPAT: ejabberd, gmail and perhaps others do not always set 'to' and 'from'
97 -- on streams. We fill in the session's to/from here instead.
98 if not origin.from_host then
99 origin.from_host = from;
101 if not origin.to_host then
105 origin.log("debug", "asking %s if key %s belongs to them", from, stanza[1]);
106 module:fire_event("route/remote", {
107 from_host = to, to_host = from;
108 stanza = st.stanza("db:verify", { from = to, to = from, id = origin.streamid }):text(stanza[1]);
114 module:hook("stanza/jabber:server:dialback:verify", function(event)
115 local origin, stanza = event.origin, event.stanza;
117 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
118 local attr = stanza.attr;
119 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
120 if dialback_verifying and attr.from == origin.to_host then
122 if attr.type == "valid" then
123 module:fire_event("s2s-authenticated", { session = dialback_verifying, host = attr.from });
126 -- Warn the original connection that is was not verified successfully
127 log("warn", "authoritative server for %s denied the key", attr.from or "(unknown)");
130 if dialback_verifying.destroyed then
131 log("warn", "Incoming s2s session %s was closed in the meantime, so we can't notify it of the db result", tostring(dialback_verifying):match("%w+$"));
133 dialback_verifying.sends2s(
134 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
135 :text(dialback_verifying.hosts[attr.from].dialback_key));
137 dialback_requests[attr.from.."/"..(attr.id or "")] = nil;
143 module:hook("stanza/jabber:server:dialback:result", function(event)
144 local origin, stanza = event.origin, event.stanza;
146 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
147 -- Remote server is telling us whether we passed dialback
149 local attr = stanza.attr;
150 if not hosts[attr.to] then
151 origin:close("host-unknown");
153 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
155 origin:close("invalid-id");
158 if stanza.attr.type == "valid" then
159 module:fire_event("s2s-authenticated", { session = origin, host = attr.from });
161 origin:close("not-authorized", "dialback authentication failed");
167 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
168 if origin.external_auth == "failed" then
169 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
170 initiate_dialback(origin);
175 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
176 if not origin.external_auth or origin.external_auth == "failed" then
177 module:log("debug", "Initiating dialback...");
178 initiate_dialback(origin);
183 module:hook("s2sout-authenticate-legacy", function (event)
184 module:log("debug", "Initiating dialback...");
185 initiate_dialback(event.origin);
189 -- Offer dialback to incoming hosts
190 module:hook("s2s-stream-features", function (data)
191 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();