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