Merge 0.9->0.10
[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                 session.notopen = nil;
351         elseif session.direction == "outgoing" then
352                 session.notopen = nil;
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 end
387
388 function stream_callbacks.streamclosed(session)
389         (session.log or log)("debug", "Received </stream:stream>");
390         session:close(false);
391 end
392
393 function stream_callbacks.error(session, error, data)
394         if error == "no-stream" then
395                 session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}")));
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.streamid = nil;
507                 session.stream:reset();
508         end
509
510         session.stream_attrs = session_stream_attrs;
511
512         local filter = initialize_filters(session);
513         local conn = session.conn;
514         local w = conn.write;
515
516         function session.sends2s(t)
517                 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
518                 if t.name then
519                         t = filter("stanzas/out", t);
520                 end
521                 if t then
522                         t = filter("bytes/out", tostring(t));
523                         if t then
524                                 return w(conn, t);
525                         end
526                 end
527         end
528
529         function session.data(data)
530                 data = filter("bytes/in", data);
531                 if data then
532                         local ok, err = stream:feed(data);
533                         if ok then return; end
534                         log("warn", "Received invalid XML: %s", data);
535                         log("warn", "Problem was: %s", err);
536                         session:close("not-well-formed");
537                 end
538         end
539
540         session.close = session_close;
541
542         local handlestanza = stream_callbacks.handlestanza;
543         function session.dispatch_stanza(session, stanza)
544                 return handlestanza(session, stanza);
545         end
546
547         module:fire_event("s2s-created", { session = session });
548
549         add_task(connect_timeout, function ()
550                 if session.type == "s2sin" or session.type == "s2sout" then
551                         return; -- Ok, we're connected
552                 elseif session.type == "s2s_destroyed" then
553                         return; -- Session already destroyed
554                 end
555                 -- Not connected, need to close session and clean up
556                 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity",
557                 session.from_host or "(unknown)", session.to_host or "(unknown)");
558                 session:close("connection-timeout");
559         end);
560 end
561
562 function listener.onconnect(conn)
563         conn:setoption("keepalive", opt_keepalives);
564         local session = sessions[conn];
565         if not session then -- New incoming connection
566                 session = s2s_new_incoming(conn);
567                 sessions[conn] = session;
568                 session.log("debug", "Incoming s2s connection");
569                 initialize_session(session);
570         else -- Outgoing session connected
571                 session:open_stream(session.from_host, session.to_host);
572         end
573         session.ip = conn:ip();
574 end
575
576 function listener.onincoming(conn, data)
577         local session = sessions[conn];
578         if session then
579                 session.data(data);
580         end
581 end
582
583 function listener.onstatus(conn, status)
584         if status == "ssl-handshake-complete" then
585                 local session = sessions[conn];
586                 if session and session.direction == "outgoing" then
587                         session.log("debug", "Sending stream header...");
588                         session:open_stream(session.from_host, session.to_host);
589                 end
590         end
591 end
592
593 function listener.ondisconnect(conn, err)
594         local session = sessions[conn];
595         if session then
596                 sessions[conn] = nil;
597                 if err and session.direction == "outgoing" and session.notopen then
598                         (session.log or log)("debug", "s2s connection attempt failed: %s", err);
599                         if s2sout.attempt_connection(session, err) then
600                                 return; -- Session lives for now
601                         end
602                 end
603                 (session.log or log)("debug", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "connection closed"));
604                 s2s_destroy_session(session, err);
605         end
606 end
607
608 function listener.onreadtimeout(conn)
609         local session = sessions[conn];
610         if session then
611                 return (hosts[session.host] or prosody).events.fire_event("s2s-read-timeout", { session = session });
612         end
613 end
614
615 function listener.register_outgoing(conn, session)
616         sessions[conn] = session;
617         initialize_session(session);
618 end
619
620 function check_auth_policy(event)
621         local host, session = event.host, event.session;
622         local must_secure = secure_auth;
623
624         if not must_secure and secure_domains[host] then
625                 must_secure = true;
626         elseif must_secure and insecure_domains[host] then
627                 must_secure = false;
628         end
629
630         if must_secure and (session.cert_chain_status ~= "valid" or session.cert_identity_status ~= "valid") then
631                 module:log("warn", "Forbidding insecure connection to/from %s", host or session.ip or "(unknown host)");
632                 if session.direction == "incoming" then
633                         session:close({ condition = "not-authorized", text = "Your server's certificate is invalid, expired, or not trusted by "..session.to_host });
634                 else -- Close outgoing connections without warning
635                         session:close(false);
636                 end
637                 return false;
638         end
639 end
640
641 module:hook("s2s-check-certificate", check_auth_policy, -1);
642
643 s2sout.set_listener(listener);
644
645 module:hook("server-stopping", function(event)
646         local reason = event.reason;
647         for _, session in pairs(sessions) do
648                 session:close{ condition = "system-shutdown", text = reason };
649         end
650 end,500);
651
652
653
654 module:provides("net", {
655         name = "s2s";
656         listener = listener;
657         default_port = 5269;
658         encryption = "starttls";
659         multiplex = {
660                 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
661         };
662 });
663