configure: Replaces tabs with spaces in --help
[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 cert_verify_identity = require "util.x509".verify_identity;
29 local fire_global_event = prosody.events.fire_event;
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
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", secure_auth);
40
41 local sessions = module:shared("sessions");
42
43 local log = module._log;
44
45 --- Handle stanzas to remote domains
46
47 local bouncy_stanzas = { message = true, presence = true, iq = true };
48 local function bounce_sendq(session, reason)
49         local sendq = session.sendq;
50         if not sendq then return; end
51         session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host));
52         local dummy = {
53                 type = "s2sin";
54                 send = function(s)
55                         (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback());
56                 end;
57                 dummy = true;
58         };
59         for i, data in ipairs(sendq) do
60                 local reply = data[2];
61                 if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then
62                         reply.attr.type = "error";
63                         reply:tag("error", {type = "cancel"})
64                                 :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
65                         if reason then
66                                 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})
67                                         :text("Server-to-server connection failed: "..reason):up();
68                         end
69                         core_process_stanza(dummy, reply);
70                 end
71                 sendq[i] = nil;
72         end
73         session.sendq = nil;
74 end
75
76 -- Handles stanzas to existing s2s sessions
77 function route_to_existing_session(event)
78         local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
79         if not hosts[from_host] then
80                 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host);
81                 return false;
82         end
83         if hosts[to_host] then
84                 log("warn", "Attempt to route stanza to a remote %s - a host we do serve?!", from_host);
85                 return false;
86         end
87         local host = hosts[from_host].s2sout[to_host];
88         if host then
89                 -- We have a connection to this host already
90                 if host.type == "s2sout_unauthed" and (stanza.name ~= "db:verify" or not host.dialback_key) then
91                         (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
92
93                         -- Queue stanza until we are able to send it
94                         if host.sendq then t_insert(host.sendq, {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)});
95                         else host.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; end
96                         host.log("debug", "stanza [%s] queued ", stanza.name);
97                         return true;
98                 elseif host.type == "local" or host.type == "component" then
99                         log("error", "Trying to send a stanza to ourselves??")
100                         log("error", "Traceback: %s", traceback());
101                         log("error", "Stanza: %s", tostring(stanza));
102                         return false;
103                 else
104                         (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
105                         -- FIXME
106                         if host.from_host ~= from_host then
107                                 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
108                                 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host));
109                         end
110                         if host.sends2s(stanza) then
111                                 host.log("debug", "stanza sent over %s", host.type);
112                                 return true;
113                         end
114                 end
115         end
116 end
117
118 -- Create a new outgoing session for a stanza
119 function route_to_new_session(event)
120         local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
121         log("debug", "opening a new outgoing connection for this stanza");
122         local host_session = s2s_new_outgoing(from_host, to_host);
123
124         -- Store in buffer
125         host_session.bounce_sendq = bounce_sendq;
126         host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
127         log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name));
128         s2sout.initiate_connection(host_session);
129         if (not host_session.connecting) and (not host_session.conn) then
130                 log("warn", "Connection to %s failed already, destroying session...", to_host);
131                 s2s_destroy_session(host_session, "Connection failed");
132                 return false;
133         end
134         return true;
135 end
136
137 function module.add_host(module)
138         if module:get_option_boolean("disallow_s2s", false) then
139                 module:log("warn", "The 'disallow_s2s' config option is deprecated, please see http://prosody.im/doc/s2s#disabling");
140                 return nil, "This host has disallow_s2s set";
141         end
142         module:hook("route/remote", route_to_existing_session, 200);
143         module:hook("route/remote", route_to_new_session, 100);
144         module:hook("s2s-authenticated", make_authenticated, -1);
145 end
146
147 -- Stream is authorised, and ready for normal stanzas
148 function mark_connected(session)
149         local sendq, send = session.sendq, session.sends2s;
150         
151         local from, to = session.from_host, session.to_host;
152         
153         session.log("info", "%s s2s connection %s->%s complete", session.direction, from, to);
154
155         local event_data = { session = session };
156         if session.type == "s2sout" then
157                 fire_global_event("s2sout-established", event_data);
158                 hosts[from].events.fire_event("s2sout-established", event_data);
159         else
160                 local host_session = hosts[to];
161                 session.send = function(stanza)
162                         return host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
163                 end;
164
165                 fire_global_event("s2sin-established", event_data);
166                 hosts[to].events.fire_event("s2sin-established", event_data);
167         end
168         
169         if session.direction == "outgoing" then
170                 if sendq then
171                         session.log("debug", "sending %d queued stanzas across new outgoing connection to %s", #sendq, session.to_host);
172                         for i, data in ipairs(sendq) do
173                                 send(data[1]);
174                                 sendq[i] = nil;
175                         end
176                         session.sendq = nil;
177                 end
178                 
179                 session.ip_hosts = nil;
180                 session.srv_hosts = nil;
181         end
182 end
183
184 function make_authenticated(event)
185         local session, host = event.session, event.host;
186         if not session.secure then
187                 if require_encryption or secure_auth or secure_domains[host] then
188                         session:close({
189                                 condition = "policy-violation",
190                                 text = "Encrypted server-to-server communication is required but was not "
191                                        ..((session.direction == "outgoing" and "offered") or "used")
192                         });
193                 end
194         end
195         if hosts[host] then
196                 session:close({ condition = "undefined-condition", text = "Attempt to authenticate as a host we serve" });
197         end
198         if session.type == "s2sout_unauthed" then
199                 session.type = "s2sout";
200         elseif session.type == "s2sin_unauthed" then
201                 session.type = "s2sin";
202                 if host then
203                         if not session.hosts[host] then session.hosts[host] = {}; end
204                         session.hosts[host].authed = true;
205                 end
206         elseif session.type == "s2sin" and host then
207                 if not session.hosts[host] then session.hosts[host] = {}; end
208                 session.hosts[host].authed = true;
209         else
210                 return false;
211         end
212         session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
213         
214         mark_connected(session);
215         
216         return true;
217 end
218
219 --- Helper to check that a session peer's certificate is valid
220 local function check_cert_status(session)
221         local host = session.direction == "outgoing" and session.to_host or session.from_host
222         local conn = session.conn:socket()
223         local cert
224         if conn.getpeercertificate then
225                 cert = conn:getpeercertificate()
226         end
227
228         if cert then
229                 local chain_valid, errors = conn:getpeerverification()
230                 -- Is there any interest in printing out all/the number of errors here?
231                 if not chain_valid then
232                         (session.log or log)("debug", "certificate chain validation result: invalid");
233                         for depth, t in ipairs(errors) do
234                                 (session.log or log)("debug", "certificate error(s) at depth %d: %s", depth-1, table.concat(t, ", "))
235                         end
236                         session.cert_chain_status = "invalid";
237                 else
238                         (session.log or log)("debug", "certificate chain validation result: valid");
239                         session.cert_chain_status = "valid";
240
241                         -- We'll go ahead and verify the asserted identity if the
242                         -- connecting server specified one.
243                         if host then
244                                 if cert_verify_identity(host, "xmpp-server", cert) then
245                                         session.cert_identity_status = "valid"
246                                 else
247                                         session.cert_identity_status = "invalid"
248                                 end
249                         end
250                 end
251         end
252         return module:fire_event("s2s-check-certificate", { host = host, session = session, cert = cert });
253 end
254
255 --- XMPP stream event handlers
256
257 local stream_callbacks = { default_ns = "jabber:server", handlestanza =  core_process_stanza };
258
259 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
260
261 function stream_callbacks.streamopened(session, attr)
262         local send = session.sends2s;
263         
264         session.version = tonumber(attr.version) or 0;
265         
266         -- TODO: Rename session.secure to session.encrypted
267         if session.secure == false then
268                 session.secure = true;
269
270                 -- Check if TLS compression is used
271                 local sock = session.conn:socket();
272                 if sock.info then
273                         session.compressed = sock:info"compression";
274                 elseif sock.compression then
275                         session.compressed = sock:compression(); --COMPAT mw/luasec-hg
276                 end
277         end
278
279         if session.direction == "incoming" then
280                 -- Send a reply stream header
281                 
282                 -- Validate to/from
283                 local to, from = nameprep(attr.to), nameprep(attr.from);
284                 if not to and attr.to then -- COMPAT: Some servers do not reliably set 'to' (especially on stream restarts)
285                         session:close({ condition = "improper-addressing", text = "Invalid 'to' address" });
286                         return;
287                 end
288                 if not from and attr.from then -- COMPAT: Some servers do not reliably set 'from' (especially on stream restarts)
289                         session:close({ condition = "improper-addressing", text = "Invalid 'from' address" });
290                         return;
291                 end
292                 
293                 -- Set session.[from/to]_host if they have not been set already and if
294                 -- this session isn't already authenticated
295                 if session.type == "s2sin_unauthed" and from and not session.from_host then
296                         session.from_host = from;
297                 elseif from ~= session.from_host then
298                         session:close({ condition = "improper-addressing", text = "New stream 'from' attribute does not match original" });
299                         return;
300                 end
301                 if session.type == "s2sin_unauthed" and to and not session.to_host then
302                         session.to_host = to;
303                 elseif to ~= session.to_host then
304                         session:close({ condition = "improper-addressing", text = "New stream 'to' attribute does not match original" });
305                         return;
306                 end
307                 
308                 -- For convenience we'll put the sanitised values into these variables
309                 to, from = session.to_host, session.from_host;
310                 
311                 session.streamid = uuid_gen();
312                 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag());
313                 if to then
314                         if not hosts[to] then
315                                 -- Attempting to connect to a host we don't serve
316                                 session:close({
317                                         condition = "host-unknown";
318                                         text = "This host does not serve "..to
319                                 });
320                                 return;
321                         elseif not hosts[to].modules.s2s then
322                                 -- Attempting to connect to a host that disallows s2s
323                                 session:close({
324                                         condition = "policy-violation";
325                                         text = "Server-to-server communication is disabled for this host";
326                                 });
327                                 return;
328                         end
329                 end
330
331                 if hosts[from] then
332                         session:close({ condition = "undefined-condition", text = "Attempt to connect from a host we serve" });
333                         return;
334                 end
335
336                 if session.secure and not session.cert_chain_status then
337                         if check_cert_status(session) == false then
338                                 return;
339                         end
340                 end
341
342                 session:open_stream()
343                 if session.version >= 1.0 then
344                         local features = st.stanza("stream:features");
345                         
346                         if to then
347                                 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features });
348                         else
349                                 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", from or "unknown host");
350                         end
351                         
352                         log("debug", "Sending stream features: %s", tostring(features));
353                         send(features);
354                 end
355         elseif session.direction == "outgoing" then
356                 -- If we are just using the connection for verifying dialback keys, we won't try and auth it
357                 if not attr.id then error("stream response did not give us a streamid!!!"); end
358                 session.streamid = attr.id;
359
360                 if session.secure and not session.cert_chain_status then
361                         if check_cert_status(session) == false then
362                                 return;
363                         end
364                 end
365
366                 -- Send unauthed buffer
367                 -- (stanzas which are fine to send before dialback)
368                 -- Note that this is *not* the stanza queue (which
369                 -- we can only send if auth succeeds) :)
370                 local send_buffer = session.send_buffer;
371                 if send_buffer and #send_buffer > 0 then
372                         log("debug", "Sending s2s send_buffer now...");
373                         for i, data in ipairs(send_buffer) do
374                                 session.sends2s(tostring(data));
375                                 send_buffer[i] = nil;
376                         end
377                 end
378                 session.send_buffer = nil;
379         
380                 -- If server is pre-1.0, don't wait for features, just do dialback
381                 if session.version < 1.0 then
382                         if not session.dialback_verifying then
383                                 hosts[session.from_host].events.fire_event("s2sout-authenticate-legacy", { origin = session });
384                         else
385                                 mark_connected(session);
386                         end
387                 end
388         end
389         session.notopen = nil;
390 end
391
392 function stream_callbacks.streamclosed(session)
393         (session.log or log)("debug", "Received </stream:stream>");
394         session:close(false);
395 end
396
397 function stream_callbacks.error(session, error, data)
398         if error == "no-stream" then
399                 session:close("invalid-namespace");
400         elseif error == "parse-error" then
401                 session.log("debug", "Server-to-server XML parse error: %s", tostring(error));
402                 session:close("not-well-formed");
403         elseif error == "stream-error" then
404                 local condition, text = "undefined-condition";
405                 for child in data:children() do
406                         if child.attr.xmlns == xmlns_xmpp_streams then
407                                 if child.name ~= "text" then
408                                         condition = child.name;
409                                 else
410                                         text = child:get_text();
411                                 end
412                                 if condition ~= "undefined-condition" and text then
413                                         break;
414                                 end
415                         end
416                 end
417                 text = condition .. (text and (" ("..text..")") or "");
418                 session.log("info", "Session closed by remote with error: %s", text);
419                 session:close(nil, text);
420         end
421 end
422
423 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
424 function stream_callbacks.handlestanza(session, stanza)
425         if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
426                 stanza.attr.xmlns = nil;
427         end
428         stanza = session.filter("stanzas/in", stanza);
429         if stanza then
430                 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
431         end
432 end
433
434 local listener = {};
435
436 --- Session methods
437 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
438 local function session_close(session, reason, remote_reason)
439         local log = session.log or log;
440         if session.conn then
441                 if session.notopen then
442                         session:open_stream()
443                 end
444                 if reason then -- nil == no err, initiated by us, false == initiated by remote
445                         if type(reason) == "string" then -- assume stream error
446                                 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason);
447                                 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
448                         elseif type(reason) == "table" then
449                                 if reason.condition then
450                                         local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
451                                         if reason.text then
452                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
453                                         end
454                                         if reason.extra then
455                                                 stanza:add_child(reason.extra);
456                                         end
457                                         log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza));
458                                         session.sends2s(stanza);
459                                 elseif reason.name then -- a stanza
460                                         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));
461                                         session.sends2s(reason);
462                                 end
463                         end
464                 end
465
466                 session.sends2s("</stream:stream>");
467                 function session.sends2s() return false; end
468                 
469                 local reason = remote_reason or (reason and (reason.text or reason.condition)) or reason;
470                 session.log("info", "%s s2s stream %s->%s closed: %s", session.direction, session.from_host or "(unknown host)", session.to_host or "(unknown host)", reason or "stream closed");
471                 
472                 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
473                 local conn = session.conn;
474                 if reason == nil and not session.notopen and session.type == "s2sin" then
475                         add_task(stream_close_timeout, function ()
476                                 if not session.destroyed then
477                                         session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
478                                         s2s_destroy_session(session, reason);
479                                         conn:close();
480                                 end
481                         end);
482                 else
483                         s2s_destroy_session(session, reason);
484                         conn:close(); -- Close immediately, as this is an outgoing connection or is not authed
485                 end
486         end
487 end
488
489 function session_open_stream(session, from, to)
490         local from = from or session.from_host;
491         local to = to or session.to_host;
492         local attr = {
493                 ["xmlns:stream"] = 'http://etherx.jabber.org/streams',
494                 xmlns = 'jabber:server',
495                 version = session.version and (session.version > 0 and "1.0" or nil),
496                 ["xml:lang"] = 'en',
497                 id = session.streamid,
498                 from = from, to = to,
499         }
500         local local_host = session.direction == "outgoing" and from or to;
501         if not local_host or (hosts[local_host] and hosts[local_host].modules.dialback) then
502                 attr["xmlns:db"] = 'jabber:server:dialback';
503         end
504
505         session.sends2s("<?xml version='1.0'?>");
506         session.sends2s(st.stanza("stream:stream", attr):top_tag());
507         return true;
508 end
509
510 -- Session initialization logic shared by incoming and outgoing
511 local function initialize_session(session)
512         local stream = new_xmpp_stream(session, stream_callbacks);
513         session.stream = stream;
514         
515         session.notopen = true;
516                 
517         function session.reset_stream()
518                 session.notopen = true;
519                 session.stream:reset();
520         end
521
522         session.open_stream = session_open_stream;
523         
524         local filter = session.filter;
525         function session.data(data)
526                 data = filter("bytes/in", data);
527                 if data then
528                         local ok, err = stream:feed(data);
529                         if ok then return; end
530                         (session.log or log)("warn", "Received invalid XML: %s", data);
531                         (session.log or log)("warn", "Problem was: %s", err);
532                         session:close("not-well-formed");
533                 end
534         end
535
536         session.close = session_close;
537
538         local handlestanza = stream_callbacks.handlestanza;
539         function session.dispatch_stanza(session, stanza)
540                 return handlestanza(session, stanza);
541         end
542
543         add_task(connect_timeout, function ()
544                 if session.type == "s2sin" or session.type == "s2sout" then
545                         return; -- Ok, we're connected
546                 elseif session.type == "s2s_destroyed" then
547                         return; -- Session already destroyed
548                 end
549                 -- Not connected, need to close session and clean up
550                 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity",
551                 session.from_host or "(unknown)", session.to_host or "(unknown)");
552                 session:close("connection-timeout");
553         end);
554 end
555
556 function listener.onconnect(conn)
557         local session = sessions[conn];
558         if not session then -- New incoming connection
559                 session = s2s_new_incoming(conn);
560                 sessions[conn] = session;
561                 session.log("debug", "Incoming s2s connection");
562
563                 local filter = initialize_filters(session);
564                 local w = conn.write;
565                 session.sends2s = function (t)
566                         log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)"));
567                         if t.name then
568                                 t = filter("stanzas/out", t);
569                         end
570                         if t then
571                                 t = filter("bytes/out", tostring(t));
572                                 if t then
573                                         return w(conn, t);
574                                 end
575                         end
576                 end
577         
578                 initialize_session(session);
579         else -- Outgoing session connected
580                 session:open_stream(session.from_host, session.to_host);
581         end
582 end
583
584 function listener.onincoming(conn, data)
585         local session = sessions[conn];
586         if session then
587                 session.data(data);
588         end
589 end
590         
591 function listener.onstatus(conn, status)
592         if status == "ssl-handshake-complete" then
593                 local session = sessions[conn];
594                 if session and session.direction == "outgoing" then
595                         session.log("debug", "Sending stream header...");
596                         session:open_stream(session.from_host, session.to_host);
597                 end
598         end
599 end
600
601 function listener.ondisconnect(conn, err)
602         local session = sessions[conn];
603         if session then
604                 sessions[conn] = nil;
605                 if err and session.direction == "outgoing" and session.notopen then
606                         (session.log or log)("debug", "s2s connection attempt failed: %s", err);
607                         if s2sout.attempt_connection(session, err) then
608                                 (session.log or log)("debug", "...so we're going to try another target");
609                                 return; -- Session lives for now
610                         end
611                 end
612                 (session.log or log)("debug", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "connection closed"));
613                 s2s_destroy_session(session, err);
614         end
615 end
616
617 function listener.register_outgoing(conn, session)
618         session.direction = "outgoing";
619         sessions[conn] = session;
620         initialize_session(session);
621 end
622
623 function check_auth_policy(event)
624         local host, session = event.host, event.session;
625         local must_secure = secure_auth;
626
627         if not must_secure and secure_domains[host] then
628                 must_secure = true;
629         elseif must_secure and insecure_domains[host] then
630                 must_secure = false;
631         end
632         
633         if must_secure and not session.cert_identity_status then
634                 module:log("warn", "Forbidding insecure connection to/from %s", host);
635                 if session.direction == "incoming" then
636                         session:close({ condition = "not-authorized", text = "Your server's certificate is invalid, expired, or not trusted by"..session.to_host });
637                 else -- Close outgoing connections without warning
638                         session:close(false);
639                 end
640                 return false;
641         end
642 end
643
644 module:hook("s2s-check-certificate", check_auth_policy, -1);
645
646 s2sout.set_listener(listener);
647
648 module:hook("server-stopping", function(event)
649         local reason = event.reason;
650         for _, session in pairs(sessions) do
651                 session:close{ condition = "system-shutdown", text = reason };
652         end
653 end,500);
654
655
656
657 module:provides("net", {
658         name = "s2s";
659         listener = listener;
660         default_port = 5269;
661         encryption = "starttls";
662         multiplex = {
663                 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
664         };
665 });
666