comparison plugins/mod_dialback.lua @ 4797:e239668aa6d2

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Sun, 29 Apr 2012 02:10:55 +0100
parents 178f252c31b0
children 5ef05f32bc42 751510cd558d
comparison
equal deleted inserted replaced
4796:04a34287dc12 4797:e239668aa6d2
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";
22 20
23 local dialback_requests = setmetatable({}, { __mode = 'v' }); 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
24 37
25 module:hook("stanza/jabber:server:dialback:verify", function(event) 38 module:hook("stanza/jabber:server:dialback:verify", function(event)
26 local origin, stanza = event.origin, event.stanza; 39 local origin, stanza = event.origin, event.stanza;
27 40
28 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then 41 if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
30 origin.log("debug", "verifying that dialback key is ours..."); 43 origin.log("debug", "verifying that dialback key is ours...");
31 local attr = stanza.attr; 44 local attr = stanza.attr;
32 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34 45 -- 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 46 --if attr.from ~= origin.to_host then error("invalid-from"); end
34 local type; 47 local type;
35 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then 48 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
36 type = "valid" 49 type = "valid"
37 else 50 else
38 type = "invalid" 51 type = "invalid"
39 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to); 52 origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
40 end 53 end
70 -- Just used for friendlier logging 83 -- Just used for friendlier logging
71 origin.to_host = attr.to; 84 origin.to_host = attr.to;
72 end 85 end
73 86
74 origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]); 87 origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
75 send_s2s(attr.to, attr.from, 88 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; 89 return true;
78 end 90 end
79 end); 91 end);
80 92
81 module:hook("stanza/jabber:server:dialback:verify", function(event) 93 module:hook("stanza/jabber:server:dialback:verify", function(event)
82 local origin, stanza = event.origin, event.stanza; 94 local origin, stanza = event.origin, event.stanza;
83 95
84 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then 96 if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
85 local attr = stanza.attr; 97 local attr = stanza.attr;
86 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")]; 98 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
99 module:log("debug", tostring(dialback_verifying).." "..attr.from.." "..origin.to_host);
87 if dialback_verifying and attr.from == origin.to_host then 100 if dialback_verifying and attr.from == origin.to_host then
88 local valid; 101 local valid;
89 if attr.type == "valid" then 102 if attr.type == "valid" then
90 s2s_make_authenticated(dialback_verifying, attr.from); 103 s2s_make_authenticated(dialback_verifying, attr.from);
91 valid = "valid"; 104 valid = "valid";
132 end); 145 end);
133 146
134 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza) 147 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
135 if origin.external_auth == "failed" then 148 if origin.external_auth == "failed" then
136 module:log("debug", "SASL EXTERNAL failed, falling back to dialback"); 149 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
137 s2s_initiate_dialback(origin); 150 initiate_dialback(origin);
138 return true; 151 return true;
139 end 152 end
140 end, 100); 153 end, 100);
141 154
142 module:hook_stanza(xmlns_stream, "features", function (origin, stanza) 155 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
143 if not origin.external_auth or origin.external_auth == "failed" then 156 if not origin.external_auth or origin.external_auth == "failed" then
144 s2s_initiate_dialback(origin); 157 module:log("debug", "Initiating dialback...");
158 initiate_dialback(origin);
145 return true; 159 return true;
146 end 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;
147 end, 100); 167 end, 100);
148 168
149 -- Offer dialback to incoming hosts 169 -- Offer dialback to incoming hosts
150 module:hook("s2s-stream-features", function (data) 170 module:hook("s2s-stream-features", function (data)
151 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up(); 171 data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();