2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local prosody = prosody;
12 local hosts = prosody.hosts;
13 local core_process_stanza = prosody.core_process_stanza;
15 local tostring, type = tostring, type;
16 local t_insert = table.insert;
17 local xpcall, traceback = xpcall, debug.traceback;
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 uuid_gen = require "util.uuid".generate;
28 local fire_global_event = prosody.events.fire_event;
29 local runner = require "util.async".runner;
31 local s2sout = module:require("s2sout");
33 local connect_timeout = module:get_option_number("s2s_timeout", 90);
34 local stream_close_timeout = module:get_option_number("s2s_close_timeout", 5);
35 local opt_keepalives = module:get_option_boolean("s2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
36 local secure_auth = module:get_option_boolean("s2s_secure_auth", false); -- One day...
37 local secure_domains, insecure_domains =
38 module:get_option_set("s2s_secure_domains", {})._items, module:get_option_set("s2s_insecure_domains", {})._items;
39 local require_encryption = module:get_option_boolean("s2s_require_encryption", false);
41 local measure_connections = module:measure("connections", "counter");
43 local sessions = module:shared("sessions");
45 local runner_callbacks = {};
47 local log = module._log;
50 -- Connection counter resets to 0 on load and reload
51 -- Bump it up to current value
53 for _ in pairs(sessions) do
56 measure_connections(count);
59 --- Handle stanzas to remote domains
61 local bouncy_stanzas = { message = true, presence = true, iq = true };
62 local function bounce_sendq(session, reason)
63 local sendq = session.sendq;
64 if not sendq then return; end
65 session.log("info", "Sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host));
69 (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback());
73 (session.log or log)("error", "Attempting to close the dummy origin of s2s error replies, please report this! Traceback: %s", traceback());
76 for i, data in ipairs(sendq) do
77 local reply = data[2];
78 if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then
79 reply.attr.type = "error";
80 reply:tag("error", {type = "cancel"})
81 :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
83 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})
84 :text("Server-to-server connection failed: "..reason):up();
86 core_process_stanza(dummy, reply);
93 -- Handles stanzas to existing s2s sessions
94 function route_to_existing_session(event)
95 local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
96 if not hosts[from_host] then
97 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host);
100 if hosts[to_host] then
101 log("warn", "Attempt to route stanza to a remote %s - a host we do serve?!", from_host);
104 local host = hosts[from_host].s2sout[to_host];
106 -- We have a connection to this host already
107 if host.type == "s2sout_unauthed" and (stanza.name ~= "db:verify" or not host.dialback_key) then
108 (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
110 -- Queue stanza until we are able to send it
111 if host.sendq then t_insert(host.sendq, {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)});
112 else host.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; end
113 host.log("debug", "stanza [%s] queued ", stanza.name);
115 elseif host.type == "local" or host.type == "component" then
116 log("error", "Trying to send a stanza to ourselves??")
117 log("error", "Traceback: %s", traceback());
118 log("error", "Stanza: %s", tostring(stanza));
121 (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
123 if host.from_host ~= from_host then
124 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
125 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host));
127 if host.sends2s(stanza) then
128 host.log("debug", "stanza sent over %s", host.type);
135 -- Create a new outgoing session for a stanza
136 function route_to_new_session(event)
137 local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
138 log("debug", "opening a new outgoing connection for this stanza");
139 local host_session = s2s_new_outgoing(from_host, to_host);
142 host_session.bounce_sendq = bounce_sendq;
143 host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
144 log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name));
145 s2sout.initiate_connection(host_session);
146 if (not host_session.connecting) and (not host_session.conn) then
147 log("warn", "Connection to %s failed already, destroying session...", to_host);
148 s2s_destroy_session(host_session, "Connection failed");
154 local function keepalive(event)
155 return event.session.sends2s(' ');
158 module:hook("s2s-read-timeout", keepalive, -1);
160 function module.add_host(module)
161 if module:get_option_boolean("disallow_s2s", false) then
162 module:log("warn", "The 'disallow_s2s' config option is deprecated, please see https://prosody.im/doc/s2s#disabling");
163 return nil, "This host has disallow_s2s set";
165 module:hook("route/remote", route_to_existing_session, -1);
166 module:hook("route/remote", route_to_new_session, -10);
167 module:hook("s2s-authenticated", make_authenticated, -1);
168 module:hook("s2s-read-timeout", keepalive, -1);
169 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
170 if session.type == "s2sout" then
171 -- Stream is authenticated and we are seem to be done with feature negotiation,
172 -- so the stream is ready for stanzas. RFC 6120 Section 4.3
173 mark_connected(session);
175 elseif not session.dialback_verifying then
176 session.log("warn", "No SASL EXTERNAL offer and Dialback doesn't seem to be enabled, giving up");
183 -- Stream is authorised, and ready for normal stanzas
184 function mark_connected(session)
185 local sendq = session.sendq;
187 local from, to = session.from_host, session.to_host;
189 session.log("info", "%s s2s connection %s->%s complete", session.direction:gsub("^.", string.upper), from, to);
191 local event_data = { session = session };
192 if session.type == "s2sout" then
193 fire_global_event("s2sout-established", event_data);
194 hosts[from].events.fire_event("s2sout-established", event_data);
196 local host_session = hosts[to];
197 session.send = function(stanza)
198 return host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
201 fire_global_event("s2sin-established", event_data);
202 hosts[to].events.fire_event("s2sin-established", event_data);
205 if session.direction == "outgoing" then
207 session.log("debug", "sending %d queued stanzas across new outgoing connection to %s", #sendq, session.to_host);
208 local send = session.sends2s;
209 for i, data in ipairs(sendq) do
216 session.ip_hosts = nil;
217 session.srv_hosts = nil;
221 function make_authenticated(event)
222 local session, host = event.session, event.host;
223 if not session.secure then
224 if require_encryption or (secure_auth and not(insecure_domains[host])) or secure_domains[host] then
226 condition = "policy-violation",
227 text = "Encrypted server-to-server communication is required but was not "
228 ..((session.direction == "outgoing" and "offered") or "used")
233 session:close({ condition = "undefined-condition", text = "Attempt to authenticate as a host we serve" });
235 if session.type == "s2sout_unauthed" then
236 session.type = "s2sout";
237 elseif session.type == "s2sin_unauthed" then
238 session.type = "s2sin";
240 if not session.hosts[host] then session.hosts[host] = {}; end
241 session.hosts[host].authed = true;
243 elseif session.type == "s2sin" and host then
244 if not session.hosts[host] then session.hosts[host] = {}; end
245 session.hosts[host].authed = true;
249 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
251 if (session.type == "s2sout" and session.external_auth ~= "succeeded") or session.type == "s2sin" then
252 -- Stream either used dialback for authentication or is an incoming stream.
253 mark_connected(session);
259 --- Helper to check that a session peer's certificate is valid
260 function check_cert_status(session)
261 local host = session.direction == "outgoing" and session.to_host or session.from_host
262 local conn = session.conn:socket()
264 if conn.getpeercertificate then
265 cert = conn:getpeercertificate()
268 return module:fire_event("s2s-check-certificate", { host = host, session = session, cert = cert });
271 --- XMPP stream event handlers
273 local stream_callbacks = { default_ns = "jabber:server" };
275 function stream_callbacks.handlestanza(session, stanza)
276 stanza = session.filter("stanzas/in", stanza);
277 session.thread:run(stanza);
280 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
282 function stream_callbacks.streamopened(session, attr)
283 -- run _streamopened in async context
284 session.thread:run({ attr = attr });
287 function stream_callbacks._streamopened(session, attr)
288 session.version = tonumber(attr.version) or 0;
290 -- TODO: Rename session.secure to session.encrypted
291 if session.secure == false then
292 session.secure = true;
293 session.encrypted = true;
295 local sock = session.conn:socket();
297 local info = sock:info();
298 (session.log or log)("info", "Stream encrypted (%s with %s)", info.protocol, info.cipher);
299 session.compressed = info.compression;
301 (session.log or log)("info", "Stream encrypted");
302 session.compressed = sock.compression and sock:compression(); --COMPAT mw/luasec-hg
306 if session.direction == "incoming" then
307 -- Send a reply stream header
310 local to, from = nameprep(attr.to), nameprep(attr.from);
311 if not to and attr.to then -- COMPAT: Some servers do not reliably set 'to' (especially on stream restarts)
312 session:close({ condition = "improper-addressing", text = "Invalid 'to' address" });
315 if not from and attr.from then -- COMPAT: Some servers do not reliably set 'from' (especially on stream restarts)
316 session:close({ condition = "improper-addressing", text = "Invalid 'from' address" });
320 -- Set session.[from/to]_host if they have not been set already and if
321 -- this session isn't already authenticated
322 if session.type == "s2sin_unauthed" and from and not session.from_host then
323 session.from_host = from;
324 elseif from ~= session.from_host then
325 session:close({ condition = "improper-addressing", text = "New stream 'from' attribute does not match original" });
328 if session.type == "s2sin_unauthed" and to and not session.to_host then
329 session.to_host = to;
330 elseif to ~= session.to_host then
331 session:close({ condition = "improper-addressing", text = "New stream 'to' attribute does not match original" });
335 -- For convenience we'll put the sanitised values into these variables
336 to, from = session.to_host, session.from_host;
338 session.streamid = uuid_gen();
339 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag());
341 if not hosts[to] then
342 -- Attempting to connect to a host we don't serve
344 condition = "host-unknown";
345 text = "This host does not serve "..to
348 elseif not hosts[to].modules.s2s then
349 -- Attempting to connect to a host that disallows s2s
351 condition = "policy-violation";
352 text = "Server-to-server communication is disabled for this host";
359 session:close({ condition = "undefined-condition", text = "Attempt to connect from a host we serve" });
363 if session.secure and not session.cert_chain_status then
364 if check_cert_status(session) == false then
369 session:open_stream(session.to_host, session.from_host)
370 session.notopen = nil;
371 if session.version >= 1.0 then
372 local features = st.stanza("stream:features");
375 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features });
377 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", from or session.ip or "unknown host");
378 fire_global_event("s2s-stream-features-legacy", { origin = session, features = features });
381 if ( session.type == "s2sin" or session.type == "s2sout" ) or features.tags[1] then
382 log("debug", "Sending stream features: %s", tostring(features));
383 session.sends2s(features);
385 (session.log or log)("warn", "No features to offer, giving up");
386 session:close({ condition = "undefined-condition", text = "No features to offer" });
389 elseif session.direction == "outgoing" then
390 session.notopen = nil;
392 log("error", "Stream response from %s did not give us a stream id!", session.to_host);
393 session:close({ condition = "undefined-condition", text = "Missing stream ID" });
396 session.streamid = attr.id;
398 if session.secure and not session.cert_chain_status then
399 if check_cert_status(session) == false then
404 -- Send unauthed buffer
405 -- (stanzas which are fine to send before dialback)
406 -- Note that this is *not* the stanza queue (which
407 -- we can only send if auth succeeds) :)
408 local send_buffer = session.send_buffer;
409 if send_buffer and #send_buffer > 0 then
410 log("debug", "Sending s2s send_buffer now...");
411 for i, data in ipairs(send_buffer) do
412 session.sends2s(tostring(data));
413 send_buffer[i] = nil;
416 session.send_buffer = nil;
418 -- If server is pre-1.0, don't wait for features, just do dialback
419 if session.version < 1.0 then
420 if not session.dialback_verifying then
421 hosts[session.from_host].events.fire_event("s2sout-authenticate-legacy", { origin = session });
423 mark_connected(session);
429 function stream_callbacks.streamclosed(session)
430 (session.log or log)("debug", "Received </stream:stream>");
431 session:close(false);
434 function stream_callbacks.error(session, error, data)
435 if error == "no-stream" then
436 session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}")));
437 session:close("invalid-namespace");
438 elseif error == "parse-error" then
439 session.log("debug", "Server-to-server XML parse error: %s", tostring(error));
440 session:close("not-well-formed");
441 elseif error == "stream-error" then
442 local condition, text = "undefined-condition";
443 for child in data:children() do
444 if child.attr.xmlns == xmlns_xmpp_streams then
445 if child.name ~= "text" then
446 condition = child.name;
448 text = child:get_text();
450 if condition ~= "undefined-condition" and text then
455 text = condition .. (text and (" ("..text..")") or "");
456 session.log("info", "Session closed by remote with error: %s", text);
457 session:close(nil, text);
464 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
465 local function session_close(session, reason, remote_reason)
466 local log = session.log or log;
468 if session.notopen then
469 if session.direction == "incoming" then
470 session:open_stream(session.to_host, session.from_host);
472 session:open_stream(session.from_host, session.to_host);
475 if reason then -- nil == no err, initiated by us, false == initiated by remote
476 if type(reason) == "string" then -- assume stream error
477 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or session.ip or "(unknown host)", session.type, reason);
478 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
479 elseif type(reason) == "table" then
480 if reason.condition then
481 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
483 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
486 stanza:add_child(reason.extra);
488 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or session.ip or "(unknown host)", session.type, tostring(stanza));
489 session.sends2s(stanza);
490 elseif reason.name then -- a stanza
491 log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s", session.from_host or "(unknown host)", session.to_host or "(unknown host)", session.type, tostring(reason));
492 session.sends2s(reason);
497 session.sends2s("</stream:stream>");
498 function session.sends2s() return false; end
500 local reason = remote_reason or (reason and (reason.text or reason.condition)) or reason;
501 session.log("info", "%s s2s stream %s->%s closed: %s", session.direction:gsub("^.", string.upper), session.from_host or "(unknown host)", session.to_host or "(unknown host)", reason or "stream closed");
503 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
504 local conn = session.conn;
505 if reason == nil and not session.notopen and session.type == "s2sin" then
506 add_task(stream_close_timeout, function ()
507 if not session.destroyed then
508 session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
509 s2s_destroy_session(session, reason);
514 s2s_destroy_session(session, reason);
515 conn:close(); -- Close immediately, as this is an outgoing connection or is not authed
520 function session_stream_attrs(session, from, to, attr)
521 if not from or (hosts[from] and hosts[from].modules.dialback) then
522 attr["xmlns:db"] = 'jabber:server:dialback';
532 -- Session initialization logic shared by incoming and outgoing
533 local function initialize_session(session)
534 local stream = new_xmpp_stream(session, stream_callbacks);
536 session.thread = runner(function (stanza)
537 if stanza.name == nil then
538 stream_callbacks._streamopened(session, stanza.attr);
540 core_process_stanza(session, stanza);
542 end, runner_callbacks, session);
544 local log = session.log or log;
545 session.stream = stream;
547 session.notopen = true;
549 function session.reset_stream()
550 session.notopen = true;
551 session.streamid = nil;
552 session.stream:reset();
555 session.stream_attrs = session_stream_attrs;
557 local filter = initialize_filters(session);
558 local conn = session.conn;
559 local w = conn.write;
561 function session.sends2s(t)
562 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
564 t = filter("stanzas/out", t);
567 t = filter("bytes/out", tostring(t));
574 function session.data(data)
575 data = filter("bytes/in", data);
577 local ok, err = stream:feed(data);
578 if ok then return; end
579 log("warn", "Received invalid XML: %s", data);
580 log("warn", "Problem was: %s", err);
581 session:close("not-well-formed");
585 session.close = session_close;
587 local handlestanza = stream_callbacks.handlestanza;
588 function session.dispatch_stanza(session, stanza)
589 return handlestanza(session, stanza);
592 module:fire_event("s2s-created", { session = session });
594 add_task(connect_timeout, function ()
595 if session.type == "s2sin" or session.type == "s2sout" then
596 return; -- Ok, we're connected
597 elseif session.type == "s2s_destroyed" then
598 return; -- Session already destroyed
600 -- Not connected, need to close session and clean up
601 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity",
602 session.from_host or "(unknown)", session.to_host or "(unknown)");
603 session:close("connection-timeout");
607 function runner_callbacks:ready()
608 self.data.log("debug", "Runner %s ready (%s)", self.thread, coroutine.status(self.thread));
609 self.data.conn:resume();
612 function runner_callbacks:waiting()
613 self.data.log("debug", "Runner %s waiting (%s)", self.thread, coroutine.status(self.thread));
614 self.data.conn:pause();
617 function runner_callbacks:error(err)
618 (self.data.log or log)("error", "Traceback[s2s]: %s", err);
621 function listener.onconnect(conn)
622 measure_connections(1);
623 conn:setoption("keepalive", opt_keepalives);
624 local session = sessions[conn];
625 if not session then -- New incoming connection
626 session = s2s_new_incoming(conn);
627 sessions[conn] = session;
628 session.log("debug", "Incoming s2s connection");
629 initialize_session(session);
630 else -- Outgoing session connected
631 session:open_stream(session.from_host, session.to_host);
633 session.ip = conn:ip();
636 function listener.onincoming(conn, data)
637 local session = sessions[conn];
643 function listener.onstatus(conn, status)
644 if status == "ssl-handshake-complete" then
645 local session = sessions[conn];
646 if session and session.direction == "outgoing" then
647 session.log("debug", "Sending stream header...");
648 session:open_stream(session.from_host, session.to_host);
653 function listener.ontimeout(conn)
654 -- Called instead of onconnect when the connection times out
655 measure_connections(1);
658 function listener.ondisconnect(conn, err)
659 measure_connections(-1);
660 local session = sessions[conn];
662 sessions[conn] = nil;
663 if err and session.direction == "outgoing" and session.notopen then
664 (session.log or log)("debug", "s2s connection attempt failed: %s", err);
665 if s2sout.attempt_connection(session, err) then
666 return; -- Session lives for now
669 (session.log or log)("debug", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "connection closed"));
670 s2s_destroy_session(session, err);
674 function listener.onreadtimeout(conn)
675 local session = sessions[conn];
676 local host = session.host or session.to_host;
678 return (hosts[host] or prosody).events.fire_event("s2s-read-timeout", { session = session });
682 function listener.register_outgoing(conn, session)
683 sessions[conn] = session;
684 initialize_session(session);
687 function listener.ondetach(conn)
688 sessions[conn] = nil;
691 function check_auth_policy(event)
692 local host, session = event.host, event.session;
693 local must_secure = secure_auth;
695 if not must_secure and secure_domains[host] then
697 elseif must_secure and insecure_domains[host] then
701 if must_secure and (session.cert_chain_status ~= "valid" or session.cert_identity_status ~= "valid") then
702 module:log("warn", "Forbidding insecure connection to/from %s", host or session.ip or "(unknown host)");
703 if session.direction == "incoming" then
704 session:close({ condition = "not-authorized", text = "Your server's certificate is invalid, expired, or not trusted by "..session.to_host });
705 else -- Close outgoing connections without warning
706 session:close(false);
712 module:hook("s2s-check-certificate", check_auth_policy, -1);
714 s2sout.set_listener(listener);
716 module:hook("server-stopping", function(event)
717 local reason = event.reason;
718 for _, session in pairs(sessions) do
719 session:close{ condition = "system-shutdown", text = reason };
725 module:provides("net", {
729 encryption = "starttls";
731 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";