portmanager: Import ipairs (thanks Maranda)
[prosody.git] / plugins / mod_dialback.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local format = string.format;
10
11 local hosts = _G.hosts;
12 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
13
14 local log = module._log;
15
16 local st = require "util.stanza";
17 local sha256_hash = require "util.hashes".sha256;
18
19 local xmlns_stream = "http://etherx.jabber.org/streams";
20
21 local dialback_requests = setmetatable({}, { __mode = 'v' });
22
23 function generate_dialback(id, to, from)
24         return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
25 end
26
27 function initiate_dialback(session)
28         -- generate dialback key
29         session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
30         session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
31         session.log("info", "sent dialback key on outgoing s2s stream");
32 end
33
34 function verify_dialback(id, to, from, key)
35         return key == generate_dialback(id, to, from);
36 end
37
38 module:hook("stanza/jabber:server:dialback:verify", function(event)
39         local origin, stanza = event.origin, event.stanza;
40         
41         if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
42                 -- We are being asked to verify the key, to ensure it was generated by us
43                 origin.log("debug", "verifying that dialback key is ours...");
44                 local attr = stanza.attr;
45                 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
46                 --if attr.from ~= origin.to_host then error("invalid-from"); end
47                 local type;
48                 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
49                         type = "valid"
50                 else
51                         type = "invalid"
52                         origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
53                 end
54                 origin.log("debug", "verified dialback key... it is %s", type);
55                 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
56                 return true;
57         end
58 end);
59
60 module:hook("stanza/jabber:server:dialback:result", function(event)
61         local origin, stanza = event.origin, event.stanza;
62         
63         if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
64                 -- he wants to be identified through dialback
65                 -- We need to check the key with the Authoritative server
66                 local attr = stanza.attr;
67                 origin.hosts[attr.from] = { dialback_key = stanza[1] };
68                 
69                 if not hosts[attr.to] then
70                         -- Not a host that we serve
71                         origin.log("info", "%s tried to connect to %s, which we don't serve", attr.from, attr.to);
72                         origin:close("host-unknown");
73                         return true;
74                 end
75                 
76                 dialback_requests[attr.from.."/"..origin.streamid] = origin;
77                 
78                 if not origin.from_host then
79                         -- Just used for friendlier logging
80                         origin.from_host = attr.from;
81                 end
82                 if not origin.to_host then
83                         -- Just used for friendlier logging
84                         origin.to_host = attr.to;
85                 end
86                 
87                 origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
88                 origin.send(st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
89                 return true;
90         end
91 end);
92
93 module:hook("stanza/jabber:server:dialback:verify", function(event)
94         local origin, stanza = event.origin, event.stanza;
95         
96         if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
97                 local attr = stanza.attr;
98                 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
99                 module:log("debug", tostring(dialback_verifying).." "..attr.from.." "..origin.to_host);
100                 if dialback_verifying and attr.from == origin.to_host then
101                         local valid;
102                         if attr.type == "valid" then
103                                 s2s_make_authenticated(dialback_verifying, attr.from);
104                                 valid = "valid";
105                         else
106                                 -- Warn the original connection that is was not verified successfully
107                                 log("warn", "authoritative server for "..(attr.from or "(unknown)").." denied the key");
108                                 valid = "invalid";
109                         end
110                         if not dialback_verifying.sends2s then
111                                 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+$"));
112                         else
113                                 dialback_verifying.sends2s(
114                                                 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
115                                                                 :text(dialback_verifying.hosts[attr.from].dialback_key));
116                         end
117                         dialback_requests[attr.from.."/"..(attr.id or "")] = nil;
118                 end
119                 return true;
120         end
121 end);
122
123 module:hook("stanza/jabber:server:dialback:result", function(event)
124         local origin, stanza = event.origin, event.stanza;
125         
126         if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
127                 -- Remote server is telling us whether we passed dialback
128                 
129                 local attr = stanza.attr;
130                 if not hosts[attr.to] then
131                         origin:close("host-unknown");
132                         return true;
133                 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
134                         -- This isn't right
135                         origin:close("invalid-id");
136                         return true;
137                 end
138                 if stanza.attr.type == "valid" then
139                         s2s_make_authenticated(origin, attr.from);
140                 else
141                         origin:close("not-authorized", "dialback authentication failed");
142                 end
143                 return true;
144         end
145 end);
146
147 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
148         if origin.external_auth == "failed" then
149                 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
150                 initiate_dialback(origin);
151                 return true;
152         end
153 end, 100);
154
155 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
156         if not origin.external_auth or origin.external_auth == "failed" then
157                 module:log("debug", "Initiating dialback...");
158                 initiate_dialback(origin);
159                 return true;
160         end
161 end, 100);
162
163 module:hook("s2s-authenticate-legacy", function (event)
164         module:log("debug", "Initiating dialback...");
165         initiate_dialback(event.origin);
166         return true;
167 end, 100);
168
169 -- Offer dialback to incoming hosts
170 module:hook("s2s-stream-features", function (data)
171         data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();
172 end);