4e29b3349a79079cbcfe617eeb149eb1482de440
[prosody.git] / plugins / mod_dialback.lua
1 -- Prosody IM v0.2
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 local dialback_requests = setmetatable({}, { __mode = 'v' });
33
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
41                 local type;
42                 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
43                         type = "valid"
44                 else
45                         type = "invalid"
46                         log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
47                 end
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]));
50         end);
51
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] };
58                 
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");
63                         return;
64                 end
65                 
66                 dialback_requests[attr.from] = origin;
67                 
68                 if not origin.from_host then
69                         -- Just used for friendlier logging
70                         origin.from_host = attr.from;
71                 end
72                 if not origin.to_host then
73                         -- Just used for friendlier logging
74                         origin.to_host = attr.to;
75                 end
76                 
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]));
80         end);
81
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
87                         local valid;
88                         if attr.type == "valid" then
89                                 s2s_make_authenticated(dialback_verifying, attr.from);
90                                 valid = "valid";
91                         else
92                                 -- Warn the original connection that is was not verified successfully
93                                 log("warn", "authoritative server for "..(attr.from or "(unknown)").." denied the key");
94                                 valid = "invalid";
95                         end
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+$"));
98                         else
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));
102                         end
103                         dialback_requests[attr.from] = nil;
104                 end
105         end);
106
107 module:add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
108         function (origin, stanza)
109                 -- Remote server is telling us whether we passed dialback
110                 
111                 local attr = stanza.attr;
112                 if not hosts[attr.to] then
113                         origin:close("host-unknown");
114                         return;
115                 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
116                         -- This isn't right
117                         origin:close("invalid-id");
118                         return;
119                 end
120                 if stanza.attr.type == "valid" then
121                         s2s_make_authenticated(origin, attr.from);
122                 else
123                         -- FIXME: Waiting on #33
124                         error("dialback failed!");
125                 end
126         end);