Merge 0.10->trunk
[prosody.git] / plugins / mod_s2s / mod_s2s.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 module:set_global();
10
11 local prosody = prosody;
12 local hosts = prosody.hosts;
13 local core_process_stanza = prosody.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 uuid_gen = require "util.uuid".generate;
28 local fire_global_event = prosody.events.fire_event;
29 local runner = require "util.async".runner;
30
31 local s2sout = module:require("s2sout");
32
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);
40
41 local measure_connections = module:measure("connections", "counter");
42
43 local sessions = module:shared("sessions");
44
45 local runner_callbacks = {};
46
47 local log = module._log;
48
49 do
50         -- Connection counter resets to 0 on load and reload
51         -- Bump it up to current value
52         local count = 0;
53         for _ in pairs(sessions) do
54                 count = count + 1;
55         end
56         measure_connections(count);
57 end
58
59 --- Handle stanzas to remote domains
60
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));
66         local dummy = {
67                 type = "s2sin";
68                 send = function(s)
69                         (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback());
70                 end;
71                 dummy = true;
72                 close = function ()
73                         (session.log or log)("error", "Attempting to close the dummy origin of s2s error replies, please report this! Traceback: %s", traceback());
74                 end;
75         };
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();
82                         if reason then
83                                 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})
84                                         :text("Server-to-server connection failed: "..reason):up();
85                         end
86                         core_process_stanza(dummy, reply);
87                 end
88                 sendq[i] = nil;
89         end
90         session.sendq = nil;
91 end
92
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);
98                 return false;
99         end
100         if hosts[to_host] then
101                 log("warn", "Attempt to route stanza to a remote %s - a host we do serve?!", from_host);
102                 return false;
103         end
104         local host = hosts[from_host].s2sout[to_host];
105         if host then
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);
109
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);
114                         return true;
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));
119                         return false;
120                 else
121                         (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
122                         -- FIXME
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));
126                         end
127                         if host.sends2s(stanza) then
128                                 host.log("debug", "stanza sent over %s", host.type);
129                                 return true;
130                         end
131                 end
132         end
133 end
134
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);
140
141         -- Store in buffer
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");
149                 return false;
150         end
151         return true;
152 end
153
154 local function keepalive(event)
155         return event.session.sends2s(' ');
156 end
157
158 module:hook("s2s-read-timeout", keepalive, -1);
159
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";
164         end
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);
174                         return true;
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");
177                         session:close();
178                         return false;
179                 end
180         end, -1);
181 end
182
183 -- Stream is authorised, and ready for normal stanzas
184 function mark_connected(session)
185         local sendq = session.sendq;
186
187         local from, to = session.from_host, session.to_host;
188
189         session.log("info", "%s s2s connection %s->%s complete", session.direction:gsub("^.", string.upper), from, to);
190
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);
195         else
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 });
199                 end;
200
201                 fire_global_event("s2sin-established", event_data);
202                 hosts[to].events.fire_event("s2sin-established", event_data);
203         end
204
205         if session.direction == "outgoing" then
206                 if sendq 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
210                                 send(data[1]);
211                                 sendq[i] = nil;
212                         end
213                         session.sendq = nil;
214                 end
215
216                 session.ip_hosts = nil;
217                 session.srv_hosts = nil;
218         end
219 end
220
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
225                         session:close({
226                                 condition = "policy-violation",
227                                 text = "Encrypted server-to-server communication is required but was not "
228                                        ..((session.direction == "outgoing" and "offered") or "used")
229                         });
230                 end
231         end
232         if hosts[host] then
233                 session:close({ condition = "undefined-condition", text = "Attempt to authenticate as a host we serve" });
234         end
235         if session.type == "s2sout_unauthed" then
236                 session.type = "s2sout";
237         elseif session.type == "s2sin_unauthed" then
238                 session.type = "s2sin";
239                 if host then
240                         if not session.hosts[host] then session.hosts[host] = {}; end
241                         session.hosts[host].authed = true;
242                 end
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;
246         else
247                 return false;
248         end
249         session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
250
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);
254         end
255
256         return true;
257 end
258
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()
263         local cert
264         if conn.getpeercertificate then
265                 cert = conn:getpeercertificate()
266         end
267
268         return module:fire_event("s2s-check-certificate", { host = host, session = session, cert = cert });
269 end
270
271 --- XMPP stream event handlers
272
273 local stream_callbacks = { default_ns = "jabber:server" };
274
275 function stream_callbacks.handlestanza(session, stanza)
276         stanza = session.filter("stanzas/in", stanza);
277         session.thread:run(stanza);
278 end
279
280 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
281
282 function stream_callbacks.streamopened(session, attr)
283         -- run _streamopened in async context
284         session.thread:run({ attr = attr });
285 end
286
287 function stream_callbacks._streamopened(session, attr)
288         session.version = tonumber(attr.version) or 0;
289
290         -- TODO: Rename session.secure to session.encrypted
291         if session.secure == false then
292                 session.secure = true;
293                 session.encrypted = true;
294
295                 local sock = session.conn:socket();
296                 if sock.info then
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;
300                 else
301                         (session.log or log)("info", "Stream encrypted");
302                         session.compressed = sock.compression and sock:compression(); --COMPAT mw/luasec-hg
303                 end
304         end
305
306         if session.direction == "incoming" then
307                 -- Send a reply stream header
308
309                 -- Validate to/from
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" });
313                         return;
314                 end
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" });
317                         return;
318                 end
319
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" });
326                         return;
327                 end
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" });
332                         return;
333                 end
334
335                 -- For convenience we'll put the sanitised values into these variables
336                 to, from = session.to_host, session.from_host;
337
338                 session.streamid = uuid_gen();
339                 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag());
340                 if to then
341                         if not hosts[to] then
342                                 -- Attempting to connect to a host we don't serve
343                                 session:close({
344                                         condition = "host-unknown";
345                                         text = "This host does not serve "..to
346                                 });
347                                 return;
348                         elseif not hosts[to].modules.s2s then
349                                 -- Attempting to connect to a host that disallows s2s
350                                 session:close({
351                                         condition = "policy-violation";
352                                         text = "Server-to-server communication is disabled for this host";
353                                 });
354                                 return;
355                         end
356                 end
357
358                 if hosts[from] then
359                         session:close({ condition = "undefined-condition", text = "Attempt to connect from a host we serve" });
360                         return;
361                 end
362
363                 if session.secure and not session.cert_chain_status then
364                         if check_cert_status(session) == false then
365                                 return;
366                         end
367                 end
368
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");
373
374                         if to then
375                                 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features });
376                         else
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 });
379                         end
380
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);
384                         else
385                                 (session.log or log)("warn", "No features to offer, giving up");
386                                 session:close({ condition = "undefined-condition", text = "No features to offer" });
387                         end
388                 end
389         elseif session.direction == "outgoing" then
390                 session.notopen = nil;
391                 if not attr.id then
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" });
394                         return;
395                 end
396                 session.streamid = attr.id;
397
398                 if session.secure and not session.cert_chain_status then
399                         if check_cert_status(session) == false then
400                                 return;
401                         end
402                 end
403
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;
414                         end
415                 end
416                 session.send_buffer = nil;
417
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 });
422                         else
423                                 mark_connected(session);
424                         end
425                 end
426         end
427 end
428
429 function stream_callbacks.streamclosed(session)
430         (session.log or log)("debug", "Received </stream:stream>");
431         session:close(false);
432 end
433
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;
447                                 else
448                                         text = child:get_text();
449                                 end
450                                 if condition ~= "undefined-condition" and text then
451                                         break;
452                                 end
453                         end
454                 end
455                 text = condition .. (text and (" ("..text..")") or "");
456                 session.log("info", "Session closed by remote with error: %s", text);
457                 session:close(nil, text);
458         end
459 end
460
461 local listener = {};
462
463 --- Session methods
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;
467         if session.conn then
468                 if session.notopen then
469                         if session.direction == "incoming" then
470                                 session:open_stream(session.to_host, session.from_host);
471                         else
472                                 session:open_stream(session.from_host, session.to_host);
473                         end
474                 end
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();
482                                         if reason.text then
483                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
484                                         end
485                                         if reason.extra then
486                                                 stanza:add_child(reason.extra);
487                                         end
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);
493                                 end
494                         end
495                 end
496
497                 session.sends2s("</stream:stream>");
498                 function session.sends2s() return false; end
499
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");
502
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);
510                                         conn:close();
511                                 end
512                         end);
513                 else
514                         s2s_destroy_session(session, reason);
515                         conn:close(); -- Close immediately, as this is an outgoing connection or is not authed
516                 end
517         end
518 end
519
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';
523         end
524         if not from then
525                 attr.from = '';
526         end
527         if not to then
528                 attr.to = '';
529         end
530 end
531
532 -- Session initialization logic shared by incoming and outgoing
533 local function initialize_session(session)
534         local stream = new_xmpp_stream(session, stream_callbacks);
535
536         session.thread = runner(function (stanza)
537                 if stanza.name == nil then
538                         stream_callbacks._streamopened(session, stanza.attr);
539                 else
540                         core_process_stanza(session, stanza);
541                 end
542         end, runner_callbacks, session);
543
544         local log = session.log or log;
545         session.stream = stream;
546
547         session.notopen = true;
548
549         function session.reset_stream()
550                 session.notopen = true;
551                 session.streamid = nil;
552                 session.stream:reset();
553         end
554
555         session.stream_attrs = session_stream_attrs;
556
557         local filter = initialize_filters(session);
558         local conn = session.conn;
559         local w = conn.write;
560
561         function session.sends2s(t)
562                 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
563                 if t.name then
564                         t = filter("stanzas/out", t);
565                 end
566                 if t then
567                         t = filter("bytes/out", tostring(t));
568                         if t then
569                                 return w(conn, t);
570                         end
571                 end
572         end
573
574         function session.data(data)
575                 data = filter("bytes/in", data);
576                 if data then
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");
582                 end
583         end
584
585         session.close = session_close;
586
587         local handlestanza = stream_callbacks.handlestanza;
588         function session.dispatch_stanza(session, stanza)
589                 return handlestanza(session, stanza);
590         end
591
592         module:fire_event("s2s-created", { session = session });
593
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
599                 end
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");
604         end);
605 end
606
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();
610 end
611
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();
615 end
616
617 function runner_callbacks:error(err)
618         (self.data.log or log)("error", "Traceback[s2s]: %s", err);
619 end
620
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);
632         end
633         session.ip = conn:ip();
634 end
635
636 function listener.onincoming(conn, data)
637         local session = sessions[conn];
638         if session then
639                 session.data(data);
640         end
641 end
642
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);
649                 end
650         end
651 end
652
653 function listener.ontimeout(conn)
654         -- Called instead of onconnect when the connection times out
655         measure_connections(1);
656 end
657
658 function listener.ondisconnect(conn, err)
659         measure_connections(-1);
660         local session = sessions[conn];
661         if session then
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
667                         end
668                 end
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);
671         end
672 end
673
674 function listener.onreadtimeout(conn)
675         local session = sessions[conn];
676         local host = session.host or session.to_host;
677         if session then
678                 return (hosts[host] or prosody).events.fire_event("s2s-read-timeout", { session = session });
679         end
680 end
681
682 function listener.register_outgoing(conn, session)
683         sessions[conn] = session;
684         initialize_session(session);
685 end
686
687 function listener.ondetach(conn)
688         sessions[conn] = nil;
689 end
690
691 function check_auth_policy(event)
692         local host, session = event.host, event.session;
693         local must_secure = secure_auth;
694
695         if not must_secure and secure_domains[host] then
696                 must_secure = true;
697         elseif must_secure and insecure_domains[host] then
698                 must_secure = false;
699         end
700
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);
707                 end
708                 return false;
709         end
710 end
711
712 module:hook("s2s-check-certificate", check_auth_policy, -1);
713
714 s2sout.set_listener(listener);
715
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 };
720         end
721 end, -200);
722
723
724
725 module:provides("net", {
726         name = "s2s";
727         listener = listener;
728         default_port = 5269;
729         encryption = "starttls";
730         multiplex = {
731                 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
732         };
733 });
734