Fix the last couple of places where we send strings from mod_dialback
[prosody.git] / plugins / mod_dialback.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
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;
25
26 local st = require "util.stanza";
27
28 local log = require "util.logger".init("mod_dialback");
29
30 local xmlns_dialback = "jabber:server:dialback";
31
32 module:add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback,
33         function (origin, stanza)
34                 -- We are being asked to verify the key, to ensure it was generated by us
35                 log("debug", "verifying dialback key...");
36                 local attr = stanza.attr;
37                 -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
38                 --if attr.from ~= origin.to_host then error("invalid-from"); end
39                 local type;
40                 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
41                         type = "valid"
42                 else
43                         type = "invalid"
44                         log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
45                 end
46                 log("debug", "verified dialback key... it is %s", type);
47                 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
48         end);
49
50 module:add_handler("s2sin_unauthed", "result", xmlns_dialback,
51         function (origin, stanza)
52                 -- he wants to be identified through dialback
53                 -- We need to check the key with the Authoritative server
54                 local attr = stanza.attr;
55                 local attr = stanza.attr;
56                 origin.from_host = attr.from;
57                 origin.to_host = attr.to;
58                 origin.dialback_key = stanza[1];
59                 log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key);
60                 send_s2s(origin.to_host, origin.from_host,
61                         st.stanza("db:verify", { from = origin.to_host, to = origin.from_host, id = origin.streamid }):text(origin.dialback_key));
62                 hosts[origin.to_host].s2sout[origin.from_host].dialback_verifying = origin;
63         end);
64
65 module:add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback,
66         function (origin, stanza)
67                 if origin.dialback_verifying then
68                         local valid;
69                         local attr = stanza.attr;
70                         if attr.type == "valid" then
71                                 s2s_make_authenticated(origin.dialback_verifying);
72                                 valid = "valid";
73                         else
74                                 -- Warn the original connection that is was not verified successfully
75                                 log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed");
76                                 valid = "invalid";
77                         end
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+$"));
80                         else
81                                 origin.dialback_verifying.sends2s(
82                                                 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
83                                                                 :text(origin.dialback_verifying.dialback_key));
84                         end
85                 end
86         end);
87
88 module:add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
89         function (origin, stanza)
90                 if stanza.attr.type == "valid" then
91                         s2s_make_authenticated(origin);
92                 else
93                         -- FIXME
94                         error("dialback failed!");
95                 end
96         end);