Software /
code /
prosody
Comparison
plugins/mod_s2s/mod_s2s.lua @ 4814:474684c07a3a
Rename plugins/s2s/ to plugins/mod_s2s/
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 04 May 2012 00:05:15 +0100 |
parent | 4798:plugins/s2s/mod_s2s.lua@e8bd0a6f45e2 |
child | 4818:3bda6fc02652 |
comparison
equal
deleted
inserted
replaced
4813:77da9671ac39 | 4814:474684c07a3a |
---|---|
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 module:set_global(); | |
10 | |
11 local prosody = prosody; | |
12 local hosts = prosody.hosts; | |
13 local core_process_stanza = core_process_stanza; | |
14 | |
15 local tostring, type = tostring, type; | |
16 local t_insert = table.insert; | |
17 local xpcall, traceback = xpcall, debug.traceback; | |
18 | |
19 local add_task = require "util.timer".add_task; | |
20 local st = require "util.stanza"; | |
21 local initialize_filters = require "util.filters".initialize; | |
22 local nameprep = require "util.encodings".stringprep.nameprep; | |
23 local new_xmpp_stream = require "util.xmppstream".new; | |
24 local s2s_new_incoming = require "core.s2smanager".new_incoming; | |
25 local s2s_new_outgoing = require "core.s2smanager".new_outgoing; | |
26 local s2s_destroy_session = require "core.s2smanager".destroy_session; | |
27 local s2s_mark_connected = require "core.s2smanager".mark_connected; | |
28 local uuid_gen = require "util.uuid".generate; | |
29 local cert_verify_identity = require "util.x509".verify_identity; | |
30 | |
31 local s2sout = module:require("s2sout"); | |
32 | |
33 local connect_timeout = module:get_option_number("s2s_timeout", 60); | |
34 | |
35 local sessions = module:shared("sessions"); | |
36 | |
37 local log = module._log; | |
38 | |
39 --- Handle stanzas to remote domains | |
40 | |
41 local bouncy_stanzas = { message = true, presence = true, iq = true }; | |
42 local function bounce_sendq(session, reason) | |
43 local sendq = session.sendq; | |
44 if not sendq then return; end | |
45 session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host)); | |
46 local dummy = { | |
47 type = "s2sin"; | |
48 send = function(s) | |
49 (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback()); | |
50 end; | |
51 dummy = true; | |
52 }; | |
53 for i, data in ipairs(sendq) do | |
54 local reply = data[2]; | |
55 if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then | |
56 reply.attr.type = "error"; | |
57 reply:tag("error", {type = "cancel"}) | |
58 :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up(); | |
59 if reason then | |
60 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}) | |
61 :text("Server-to-server connection failed: "..reason):up(); | |
62 end | |
63 core_process_stanza(dummy, reply); | |
64 end | |
65 sendq[i] = nil; | |
66 end | |
67 session.sendq = nil; | |
68 end | |
69 | |
70 module:hook("route/remote", function (event) | |
71 local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza; | |
72 if not hosts[from_host] then | |
73 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host); | |
74 return false; | |
75 end | |
76 local host = hosts[from_host].s2sout[to_host]; | |
77 if host then | |
78 -- We have a connection to this host already | |
79 if host.type == "s2sout_unauthed" and (stanza.name ~= "db:verify" or not host.dialback_key) then | |
80 (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host); | |
81 | |
82 -- Queue stanza until we are able to send it | |
83 if host.sendq then t_insert(host.sendq, {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)}); | |
84 else host.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; end | |
85 host.log("debug", "stanza [%s] queued ", stanza.name); | |
86 return true; | |
87 elseif host.type == "local" or host.type == "component" then | |
88 log("error", "Trying to send a stanza to ourselves??") | |
89 log("error", "Traceback: %s", traceback()); | |
90 log("error", "Stanza: %s", tostring(stanza)); | |
91 return false; | |
92 else | |
93 (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); | |
94 -- FIXME | |
95 if host.from_host ~= from_host then | |
96 log("error", "WARNING! This might, possibly, be a bug, but it might not..."); | |
97 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host)); | |
98 end | |
99 host.sends2s(stanza); | |
100 host.log("debug", "stanza sent over "..host.type); | |
101 return true; | |
102 end | |
103 end | |
104 end, 200); | |
105 | |
106 module:hook("route/remote", function (event) | |
107 local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza; | |
108 log("debug", "opening a new outgoing connection for this stanza"); | |
109 local host_session = s2s_new_outgoing(from_host, to_host); | |
110 | |
111 -- Store in buffer | |
112 host_session.bounce_sendq = bounce_sendq; | |
113 host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; | |
114 log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name)); | |
115 s2sout.initiate_connection(host_session); | |
116 if (not host_session.connecting) and (not host_session.conn) then | |
117 log("warn", "Connection to %s failed already, destroying session...", to_host); | |
118 s2s_destroy_session(host_session, "Connection failed"); | |
119 return false; | |
120 end | |
121 return true; | |
122 end, 100); | |
123 | |
124 --- Helper to check that a session peer's certificate is valid | |
125 local function check_cert_status(session) | |
126 local conn = session.conn:socket() | |
127 local cert | |
128 if conn.getpeercertificate then | |
129 cert = conn:getpeercertificate() | |
130 end | |
131 | |
132 if cert then | |
133 local chain_valid, errors = conn:getpeerverification() | |
134 -- Is there any interest in printing out all/the number of errors here? | |
135 if not chain_valid then | |
136 (session.log or log)("debug", "certificate chain validation result: invalid"); | |
137 for depth, t in ipairs(errors) do | |
138 (session.log or log)("debug", "certificate error(s) at depth %d: %s", depth-1, table.concat(t, ", ")) | |
139 end | |
140 session.cert_chain_status = "invalid"; | |
141 else | |
142 (session.log or log)("debug", "certificate chain validation result: valid"); | |
143 session.cert_chain_status = "valid"; | |
144 | |
145 local host = session.direction == "incoming" and session.from_host or session.to_host | |
146 | |
147 -- We'll go ahead and verify the asserted identity if the | |
148 -- connecting server specified one. | |
149 if host then | |
150 if cert_verify_identity(host, "xmpp-server", cert) then | |
151 session.cert_identity_status = "valid" | |
152 else | |
153 session.cert_identity_status = "invalid" | |
154 end | |
155 end | |
156 end | |
157 end | |
158 end | |
159 | |
160 --- XMPP stream event handlers | |
161 | |
162 local stream_callbacks = { default_ns = "jabber:server", handlestanza = core_process_stanza }; | |
163 | |
164 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; | |
165 | |
166 function stream_callbacks.streamopened(session, attr) | |
167 local send = session.sends2s; | |
168 | |
169 -- TODO: #29: SASL/TLS on s2s streams | |
170 session.version = tonumber(attr.version) or 0; | |
171 | |
172 -- TODO: Rename session.secure to session.encrypted | |
173 if session.secure == false then | |
174 session.secure = true; | |
175 end | |
176 | |
177 if session.direction == "incoming" then | |
178 -- Send a reply stream header | |
179 | |
180 -- Validate to/from | |
181 local to, from = nameprep(attr.to), nameprep(attr.from); | |
182 if not to and attr.to then -- COMPAT: Some servers do not reliably set 'to' (especially on stream restarts) | |
183 session:close({ condition = "improper-addressing", text = "Invalid 'to' address" }); | |
184 return; | |
185 end | |
186 if not from and attr.from then -- COMPAT: Some servers do not reliably set 'from' (especially on stream restarts) | |
187 session:close({ condition = "improper-addressing", text = "Invalid 'from' address" }); | |
188 return; | |
189 end | |
190 | |
191 -- Set session.[from/to]_host if they have not been set already and if | |
192 -- this session isn't already authenticated | |
193 if session.type == "s2sin_unauthed" and from and not session.from_host then | |
194 session.from_host = from; | |
195 elseif from ~= session.from_host then | |
196 session:close({ condition = "improper-addressing", text = "New stream 'from' attribute does not match original" }); | |
197 return; | |
198 end | |
199 if session.type == "s2sin_unauthed" and to and not session.to_host then | |
200 session.to_host = to; | |
201 elseif to ~= session.to_host then | |
202 session:close({ condition = "improper-addressing", text = "New stream 'to' attribute does not match original" }); | |
203 return; | |
204 end | |
205 | |
206 session.streamid = uuid_gen(); | |
207 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag()); | |
208 if session.to_host then | |
209 if not hosts[session.to_host] then | |
210 -- Attempting to connect to a host we don't serve | |
211 session:close({ | |
212 condition = "host-unknown"; | |
213 text = "This host does not serve "..session.to_host | |
214 }); | |
215 return; | |
216 elseif hosts[session.to_host].disallow_s2s then | |
217 -- Attempting to connect to a host that disallows s2s | |
218 session:close({ | |
219 condition = "policy-violation"; | |
220 text = "Server-to-server communication is not allowed to this host"; | |
221 }); | |
222 return; | |
223 end | |
224 end | |
225 | |
226 if session.secure and not session.cert_chain_status then check_cert_status(session); end | |
227 | |
228 send("<?xml version='1.0'?>"); | |
229 send(st.stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', | |
230 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host, to=session.from_host, version=(session.version > 0 and "1.0" or nil) }):top_tag()); | |
231 if session.version >= 1.0 then | |
232 local features = st.stanza("stream:features"); | |
233 | |
234 if session.to_host then | |
235 hosts[session.to_host].events.fire_event("s2s-stream-features", { origin = session, features = features }); | |
236 else | |
237 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", session.from_host or "unknown host"); | |
238 end | |
239 | |
240 log("debug", "Sending stream features: %s", tostring(features)); | |
241 send(features); | |
242 end | |
243 elseif session.direction == "outgoing" then | |
244 -- If we are just using the connection for verifying dialback keys, we won't try and auth it | |
245 if not attr.id then error("stream response did not give us a streamid!!!"); end | |
246 session.streamid = attr.id; | |
247 | |
248 if session.secure and not session.cert_chain_status then check_cert_status(session); end | |
249 | |
250 -- Send unauthed buffer | |
251 -- (stanzas which are fine to send before dialback) | |
252 -- Note that this is *not* the stanza queue (which | |
253 -- we can only send if auth succeeds) :) | |
254 local send_buffer = session.send_buffer; | |
255 if send_buffer and #send_buffer > 0 then | |
256 log("debug", "Sending s2s send_buffer now..."); | |
257 for i, data in ipairs(send_buffer) do | |
258 session.sends2s(tostring(data)); | |
259 send_buffer[i] = nil; | |
260 end | |
261 end | |
262 session.send_buffer = nil; | |
263 | |
264 -- If server is pre-1.0, don't wait for features, just do dialback | |
265 if session.version < 1.0 then | |
266 if not session.dialback_verifying then | |
267 hosts[session.from_host].events.fire_event("s2s-authenticate-legacy", { origin = session }); | |
268 else | |
269 s2s_mark_connected(session); | |
270 end | |
271 end | |
272 end | |
273 session.notopen = nil; | |
274 session.send = function(stanza) prosody.events.fire_event("route/remote", { from_host = session.to_host, to_host = session.from_host, stanza = stanza}) end; | |
275 end | |
276 | |
277 function stream_callbacks.streamclosed(session) | |
278 (session.log or log)("debug", "Received </stream:stream>"); | |
279 session:close(); | |
280 end | |
281 | |
282 function stream_callbacks.streamdisconnected(session, err) | |
283 if err and err ~= "closed" and session.direction == "outgoing" then | |
284 (session.log or log)("debug", "s2s connection attempt failed: %s", err); | |
285 if s2sout.attempt_connection(session, err) then | |
286 (session.log or log)("debug", "...so we're going to try another target"); | |
287 return true; -- Session lives for now | |
288 end | |
289 end | |
290 (session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "closed")); | |
291 s2s_destroy_session(session, err); | |
292 end | |
293 | |
294 function stream_callbacks.error(session, error, data) | |
295 if error == "no-stream" then | |
296 session:close("invalid-namespace"); | |
297 elseif error == "parse-error" then | |
298 session.log("debug", "Server-to-server XML parse error: %s", tostring(error)); | |
299 session:close("not-well-formed"); | |
300 elseif error == "stream-error" then | |
301 local condition, text = "undefined-condition"; | |
302 for child in data:children() do | |
303 if child.attr.xmlns == xmlns_xmpp_streams then | |
304 if child.name ~= "text" then | |
305 condition = child.name; | |
306 else | |
307 text = child:get_text(); | |
308 end | |
309 if condition ~= "undefined-condition" and text then | |
310 break; | |
311 end | |
312 end | |
313 end | |
314 text = condition .. (text and (" ("..text..")") or ""); | |
315 session.log("info", "Session closed by remote with error: %s", text); | |
316 session:close(nil, text); | |
317 end | |
318 end | |
319 | |
320 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end | |
321 function stream_callbacks.handlestanza(session, stanza) | |
322 if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client | |
323 stanza.attr.xmlns = nil; | |
324 end | |
325 stanza = session.filter("stanzas/in", stanza); | |
326 if stanza then | |
327 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr); | |
328 end | |
329 end | |
330 | |
331 local listener = {}; | |
332 | |
333 --- Session methods | |
334 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; | |
335 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; | |
336 local function session_close(session, reason, remote_reason) | |
337 local log = session.log or log; | |
338 if session.conn then | |
339 if session.notopen then | |
340 session.sends2s("<?xml version='1.0'?>"); | |
341 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag()); | |
342 end | |
343 if reason then | |
344 if type(reason) == "string" then -- assume stream error | |
345 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason); | |
346 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); | |
347 elseif type(reason) == "table" then | |
348 if reason.condition then | |
349 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up(); | |
350 if reason.text then | |
351 stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); | |
352 end | |
353 if reason.extra then | |
354 stanza:add_child(reason.extra); | |
355 end | |
356 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza)); | |
357 session.sends2s(stanza); | |
358 elseif reason.name then -- a stanza | |
359 log("info", "Disconnecting %s->%s[%s], <stream:error> is: %s", session.from_host or "(unknown host)", session.to_host or "(unknown host)", session.type, tostring(reason)); | |
360 session.sends2s(reason); | |
361 end | |
362 end | |
363 end | |
364 session.sends2s("</stream:stream>"); | |
365 if session.notopen or not session.conn:close() then | |
366 session.conn:close(true); -- Force FIXME: timer? | |
367 end | |
368 session.conn:close(); | |
369 listener.ondisconnect(session.conn, remote_reason or (reason and (reason.text or reason.condition)) or reason or "stream closed"); | |
370 end | |
371 end | |
372 | |
373 -- Session initialization logic shared by incoming and outgoing | |
374 local function initialize_session(session) | |
375 local stream = new_xmpp_stream(session, stream_callbacks); | |
376 session.stream = stream; | |
377 | |
378 session.notopen = true; | |
379 | |
380 function session.reset_stream() | |
381 session.notopen = true; | |
382 session.stream:reset(); | |
383 end | |
384 | |
385 local filter = session.filter; | |
386 function session.data(data) | |
387 data = filter("bytes/in", data); | |
388 if data then | |
389 local ok, err = stream:feed(data); | |
390 if ok then return; end | |
391 (session.log or log)("warn", "Received invalid XML: %s", data); | |
392 (session.log or log)("warn", "Problem was: %s", err); | |
393 session:close("not-well-formed"); | |
394 end | |
395 end | |
396 | |
397 session.close = session_close; | |
398 | |
399 local handlestanza = stream_callbacks.handlestanza; | |
400 function session.dispatch_stanza(session, stanza) | |
401 return handlestanza(session, stanza); | |
402 end | |
403 | |
404 local conn = session.conn; | |
405 add_task(connect_timeout, function () | |
406 if session.conn ~= conn or session.connecting | |
407 or session.type == "s2sin" or session.type == "s2sout" then | |
408 return; -- Ok, we're connect[ed|ing] | |
409 end | |
410 -- Not connected, need to close session and clean up | |
411 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity", | |
412 session.from_host or "(unknown)", session.to_host or "(unknown)"); | |
413 session:close("connection-timeout"); | |
414 end); | |
415 end | |
416 | |
417 function listener.onconnect(conn) | |
418 if not sessions[conn] then -- May be an existing outgoing session | |
419 local session = s2s_new_incoming(conn); | |
420 sessions[conn] = session; | |
421 session.log("debug", "Incoming s2s connection"); | |
422 | |
423 local filter = initialize_filters(session); | |
424 local w = conn.write; | |
425 session.sends2s = function (t) | |
426 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)")); | |
427 if t.name then | |
428 t = filter("stanzas/out", t); | |
429 end | |
430 if t then | |
431 t = filter("bytes/out", tostring(t)); | |
432 if t then | |
433 return w(conn, t); | |
434 end | |
435 end | |
436 end | |
437 | |
438 initialize_session(session); | |
439 end | |
440 end | |
441 | |
442 function listener.onincoming(conn, data) | |
443 local session = sessions[conn]; | |
444 if session then | |
445 session.data(data); | |
446 end | |
447 end | |
448 | |
449 function listener.onstatus(conn, status) | |
450 if status == "ssl-handshake-complete" then | |
451 local session = sessions[conn]; | |
452 if session and session.direction == "outgoing" then | |
453 session.log("debug", "Sending stream header..."); | |
454 session:open_stream(session.from_host, session.to_host); | |
455 end | |
456 end | |
457 end | |
458 | |
459 function listener.ondisconnect(conn, err) | |
460 local session = sessions[conn]; | |
461 if session then | |
462 if stream_callbacks.streamdisconnected(session, err) then | |
463 return; -- Connection lives, for now | |
464 end | |
465 end | |
466 sessions[conn] = nil; | |
467 end | |
468 | |
469 function listener.register_outgoing(conn, session) | |
470 session.direction = "outgoing"; | |
471 sessions[conn] = session; | |
472 initialize_session(session); | |
473 end | |
474 | |
475 s2sout.set_listener(listener); | |
476 | |
477 module:add_item("net-provider", { | |
478 name = "s2s"; | |
479 listener = listener; | |
480 default_port = 5269; | |
481 encryption = "starttls"; | |
482 multiplex = { | |
483 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>"; | |
484 }; | |
485 }); | |
486 |