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