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