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;
10 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
12 local log = module._log;
14 local st = require "util.stanza";
15 local sha256_hash = require "util.hashes".sha256;
16 local nameprep = require "util.encodings".stringprep.nameprep;
18 local xmlns_stream = "http://etherx.jabber.org/streams";
20 local dialback_requests = setmetatable({}, { __mode = 'v' });
22 function generate_dialback(id, to, from)
23 return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
26 function initiate_dialback(session)
27 -- generate dialback key
28 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
29 session.sends2s(st.stanza("db:result", { from = session.from_host, to = session.to_host }):text(session.dialback_key));
30 session.log("info", "sent dialback key on outgoing s2s stream");
33 function verify_dialback(id, to, from, key)
34 return key == generate_dialback(id, to, from);
37 module:hook("stanza/jabber:server:dialback:verify", function(event)
38 local origin, stanza = event.origin, event.stanza;
40 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
41 -- We are being asked to verify the key, to ensure it was generated by us
42 origin.log("debug", "verifying that dialback key is ours...");
43 local attr = stanza.attr;
45 module:log("warn", "Ignoring incoming session from %s claiming a dialback key for %s is %s",
46 origin.from_host or "(unknown)", attr.from or "(unknown)", attr.type);
49 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
50 --if attr.from ~= origin.to_host then error("invalid-from"); end
52 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
56 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
58 origin.log("debug", "verified dialback key... it is %s", type);
59 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
64 module:hook("stanza/jabber:server:dialback:result", function(event)
65 local origin, stanza = event.origin, event.stanza;
67 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
68 -- he wants to be identified through dialback
69 -- We need to check the key with the Authoritative server
70 local attr = stanza.attr;
71 local to, from = nameprep(attr.to), nameprep(attr.from);
74 -- Not a host that we serve
75 origin.log("info", "%s tried to connect to %s, which we don't serve", from, to);
76 origin:close("host-unknown");
79 origin:close("improper-addressing");
82 origin.hosts[from] = { dialback_key = stanza[1] };
84 dialback_requests[from.."/"..origin.streamid] = origin;
86 -- COMPAT: ejabberd, gmail and perhaps others do not always set 'to' and 'from'
87 -- on streams. We fill in the session's to/from here instead.
88 if not origin.from_host then
89 origin.from_host = from;
91 if not origin.to_host then
95 origin.log("debug", "asking %s if key %s belongs to them", from, stanza[1]);
96 module:fire_event("route/remote", {
97 from_host = to, to_host = from;
98 stanza = st.stanza("db:verify", { from = to, to = from, id = origin.streamid }):text(stanza[1]);
104 module:hook("stanza/jabber:server:dialback:verify", function(event)
105 local origin, stanza = event.origin, event.stanza;
107 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
108 local attr = stanza.attr;
109 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
110 if dialback_verifying and attr.from == origin.to_host then
112 if attr.type == "valid" then
113 s2s_make_authenticated(dialback_verifying, attr.from);
116 -- Warn the original connection that is was not verified successfully
117 log("warn", "authoritative server for %s denied the key", attr.from or "(unknown)");
120 if dialback_verifying.destroyed then
121 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+$"));
123 dialback_verifying.sends2s(
124 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
125 :text(dialback_verifying.hosts[attr.from].dialback_key));
127 dialback_requests[attr.from.."/"..(attr.id or "")] = nil;
133 module:hook("stanza/jabber:server:dialback:result", function(event)
134 local origin, stanza = event.origin, event.stanza;
136 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
137 -- Remote server is telling us whether we passed dialback
139 local attr = stanza.attr;
140 if not hosts[attr.to] then
141 origin:close("host-unknown");
143 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
145 origin:close("invalid-id");
148 if stanza.attr.type == "valid" then
149 s2s_make_authenticated(origin, attr.from);
151 origin:close("not-authorized", "dialback authentication failed");
157 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
158 if origin.external_auth == "failed" then
159 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
160 initiate_dialback(origin);
165 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
166 if not origin.external_auth or origin.external_auth == "failed" then
167 module:log("debug", "Initiating dialback...");
168 initiate_dialback(origin);
173 module:hook("s2s-authenticate-legacy", function (event)
174 module:log("debug", "Initiating dialback...");
175 initiate_dialback(event.origin);
179 -- Offer dialback to incoming hosts
180 module:hook("s2s-stream-features", function (data)
181 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();