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 format = string.format;
23 local send_s2s = require "core.s2smanager".send_to_host;
24 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
25 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
27 local log = require "util.logger".init("mod_dialback");
29 local xmlns_dialback = "jabber:server:dialback";
31 module:add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback,
32 function (origin, stanza)
33 -- We are being asked to verify the key, to ensure it was generated by us
34 log("debug", "verifying dialback key...");
35 local attr = stanza.attr;
36 -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
37 --if attr.from ~= origin.to_host then error("invalid-from"); end
39 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
43 log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
45 log("debug", "verifyied dialback key... it is %s", type);
46 origin.sends2s(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
49 module:add_handler("s2sin_unauthed", "result", xmlns_dialback,
50 function (origin, stanza)
51 -- he wants to be identified through dialback
52 -- We need to check the key with the Authoritative server
53 local attr = stanza.attr;
54 local attr = stanza.attr;
55 origin.from_host = attr.from;
56 origin.to_host = attr.to;
57 origin.dialback_key = stanza[1];
58 log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key);
59 send_s2s(origin.to_host, origin.from_host,
60 format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", origin.to_host, origin.from_host,
61 origin.streamid, origin.dialback_key));
62 hosts[origin.to_host].s2sout[origin.from_host].dialback_verifying = origin;
65 module:add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback,
66 function (origin, stanza)
67 if origin.dialback_verifying then
69 local attr = stanza.attr;
70 if attr.type == "valid" then
71 s2s_make_authenticated(origin.dialback_verifying);
74 -- Warn the original connection that is was not verified successfully
75 log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed");
78 if not origin.dialback_verifying.sends2s then
79 log("warn", "Incoming s2s session %s was closed in the meantime, so we can't notify it of the db result", tostring(origin.dialback_verifying):match("%w+$"));
81 origin.dialback_verifying.sends2s(format("<db:result from='%s' to='%s' id='%s' type='%s'>%s</db:result>",
82 attr.to, attr.from, attr.id, valid, origin.dialback_verifying.dialback_key));
87 module:add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
88 function (origin, stanza)
89 if stanza.attr.type == "valid" then
90 s2s_make_authenticated(origin);
93 error("dialback failed!");