2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local send_s2s = require "core.s2smanager".send_to_host;
23 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
24 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
26 local st = require "util.stanza";
28 local log = require "util.logger".init("mod_dialback");
30 local xmlns_dialback = "jabber:server:dialback";
32 local dialback_requests = setmetatable({}, { __mode = 'v' });
34 module:add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback,
35 function (origin, stanza)
36 -- We are being asked to verify the key, to ensure it was generated by us
37 log("debug", "verifying dialback key...");
38 local attr = stanza.attr;
39 -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
40 --if attr.from ~= origin.to_host then error("invalid-from"); end
42 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
46 log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
48 log("debug", "verified dialback key... it is %s", type);
49 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
52 module:add_handler({ "s2sin_unauthed", "s2sin" }, "result", xmlns_dialback,
53 function (origin, stanza)
54 -- he wants to be identified through dialback
55 -- We need to check the key with the Authoritative server
56 local attr = stanza.attr;
57 origin.hosts[attr.from] = { dialback_key = stanza[1] };
59 if not hosts[attr.to] then
60 -- Not a host that we serve
61 log("info", "%s tried to connect to %s, which we don't serve", attr.from, attr.to);
62 origin:close("host-unknown");
66 dialback_requests[attr.from] = origin;
68 if not origin.from_host then
69 -- Just used for friendlier logging
70 origin.from_host = attr.from;
72 if not origin.to_host then
73 -- Just used for friendlier logging
74 origin.to_host = attr.to;
77 log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
78 send_s2s(attr.to, attr.from,
79 st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
82 module:add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback,
83 function (origin, stanza)
84 local attr = stanza.attr;
85 local dialback_verifying = dialback_requests[attr.from];
86 if dialback_verifying then
88 if attr.type == "valid" then
89 s2s_make_authenticated(dialback_verifying, attr.from);
92 -- Warn the original connection that is was not verified successfully
93 log("warn", "authoritative server for "..(attr.from or "(unknown)").." denied the key");
96 if not dialback_verifying.sends2s then
97 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+$"));
99 dialback_verifying.sends2s(
100 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
101 :text(dialback_verifying.hosts[attr.from].dialback_key));
103 dialback_requests[attr.from] = nil;
107 module:add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
108 function (origin, stanza)
109 -- Remote server is telling us whether we passed dialback
111 local attr = stanza.attr;
112 if not hosts[attr.to] then
113 origin:close("host-unknown");
115 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
117 origin:close("invalid-id");
120 if stanza.attr.type == "valid" then
121 s2s_make_authenticated(origin, attr.from);
123 -- FIXME: Waiting on #33
124 error("dialback failed!");