Comparison

plugins/mod_dialback.lua @ 4684:dc70c4ffb66d

Merge timber->trunk - thanks everyone!
author Matthew Wild <mwild1@gmail.com>
date Tue, 24 Apr 2012 21:59:20 +0100
parent 4587:93a84314c296
child 4761:178f252c31b0
comparison
equal deleted inserted replaced
4529:12621337471f 4684:dc70c4ffb66d
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local format = string.format;
9 10
10 local hosts = _G.hosts; 11 local hosts = _G.hosts;
11 local send_s2s = require "core.s2smanager".send_to_host;
12 local s2s_make_authenticated = require "core.s2smanager".make_authenticated; 12 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
13 local s2s_initiate_dialback = require "core.s2smanager".initiate_dialback;
14 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
15 13
16 local log = module._log; 14 local log = module._log;
17 15
18 local st = require "util.stanza"; 16 local st = require "util.stanza";
17 local sha256_hash = require "util.hashes".sha256;
19 18
20 local xmlns_stream = "http://etherx.jabber.org/streams"; 19 local xmlns_stream = "http://etherx.jabber.org/streams";
21 local xmlns_dialback = "jabber:server:dialback"; 20 local xmlns_dialback = "jabber:server:dialback";
22 21
23 local dialback_requests = setmetatable({}, { __mode = 'v' }); 22 local dialback_requests = setmetatable({}, { __mode = 'v' });
23
24 function generate_dialback(id, to, from)
25 return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
26 end
27
28 function initiate_dialback(session)
29 -- generate dialback key
30 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
31 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
32 session.log("info", "sent dialback key on outgoing s2s stream");
33 end
34
35 function verify_dialback(id, to, from, key)
36 return key == generate_dialback(id, to, from);
37 end
24 38
25 module:hook("stanza/jabber:server:dialback:verify", function(event) 39 module:hook("stanza/jabber:server:dialback:verify", function(event)
26 local origin, stanza = event.origin, event.stanza; 40 local origin, stanza = event.origin, event.stanza;
27 41
28 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then 42 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
30 origin.log("debug", "verifying that dialback key is ours..."); 44 origin.log("debug", "verifying that dialback key is ours...");
31 local attr = stanza.attr; 45 local attr = stanza.attr;
32 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34 46 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
33 --if attr.from ~= origin.to_host then error("invalid-from"); end 47 --if attr.from ~= origin.to_host then error("invalid-from"); end
34 local type; 48 local type;
35 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then 49 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
36 type = "valid" 50 type = "valid"
37 else 51 else
38 type = "invalid" 52 type = "invalid"
39 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to); 53 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
40 end 54 end
70 -- Just used for friendlier logging 84 -- Just used for friendlier logging
71 origin.to_host = attr.to; 85 origin.to_host = attr.to;
72 end 86 end
73 87
74 origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]); 88 origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
75 send_s2s(attr.to, attr.from, 89 origin.send(st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
76 st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
77 return true; 90 return true;
78 end 91 end
79 end); 92 end);
80 93
81 module:hook("stanza/jabber:server:dialback:verify", function(event) 94 module:hook("stanza/jabber:server:dialback:verify", function(event)
82 local origin, stanza = event.origin, event.stanza; 95 local origin, stanza = event.origin, event.stanza;
83 96
84 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then 97 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
85 local attr = stanza.attr; 98 local attr = stanza.attr;
86 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")]; 99 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
100 module:log("debug", tostring(dialback_verifying).." "..attr.from.." "..origin.to_host);
87 if dialback_verifying and attr.from == origin.to_host then 101 if dialback_verifying and attr.from == origin.to_host then
88 local valid; 102 local valid;
89 if attr.type == "valid" then 103 if attr.type == "valid" then
90 s2s_make_authenticated(dialback_verifying, attr.from); 104 s2s_make_authenticated(dialback_verifying, attr.from);
91 valid = "valid"; 105 valid = "valid";
132 end); 146 end);
133 147
134 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza) 148 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
135 if origin.external_auth == "failed" then 149 if origin.external_auth == "failed" then
136 module:log("debug", "SASL EXTERNAL failed, falling back to dialback"); 150 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
137 s2s_initiate_dialback(origin); 151 initiate_dialback(origin);
138 return true; 152 return true;
139 end 153 end
140 end, 100); 154 end, 100);
141 155
142 module:hook_stanza(xmlns_stream, "features", function (origin, stanza) 156 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
143 if not origin.external_auth or origin.external_auth == "failed" then 157 if not origin.external_auth or origin.external_auth == "failed" then
144 s2s_initiate_dialback(origin); 158 module:log("debug", "Initiating dialback...");
159 initiate_dialback(origin);
145 return true; 160 return true;
146 end 161 end
162 end, 100);
163
164 module:hook("s2s-authenticate-legacy", function (event)
165 module:log("debug", "Initiating dialback...");
166 initiate_dialback(event.origin);
167 return true;
147 end, 100); 168 end, 100);
148 169
149 -- Offer dialback to incoming hosts 170 -- Offer dialback to incoming hosts
150 module:hook("s2s-stream-features", function (data) 171 module:hook("s2s-stream-features", function (data)
151 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up(); 172 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();