df201e9ef55e6d897aab417b9660fa39d84e4b53
[prosody.git] / s2smanager.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
10
11 local hosts = hosts;
12 local sessions = sessions;
13 local core_process_stanza = function(a, b) core_process_stanza(a, b); end
14 local add_task = require "util.timer".add_task;
15 local socket = require "socket";
16 local format = string.format;
17 local t_insert, t_sort = table.insert, table.sort;
18 local get_traceback = debug.traceback;
19 local tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber, setmetatable
20     = tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber, setmetatable;
21
22 local idna_to_ascii = require "util.encodings".idna.to_ascii;
23 local connlisteners_get = require "net.connlisteners".get;
24 local initialize_filters = require "util.filters".initialize;
25 local wrapclient = require "net.server".wrapclient;
26 local modulemanager = require "core.modulemanager";
27 local st = require "stanza";
28 local stanza = st.stanza;
29 local nameprep = require "util.encodings".stringprep.nameprep;
30 local cert_verify_identity = require "util.x509".verify_identity;
31
32 local fire_event = prosody.events.fire_event;
33 local uuid_gen = require "util.uuid".generate;
34
35 local logger_init = require "util.logger".init;
36
37 local log = logger_init("s2smanager");
38
39 local sha256_hash = require "util.hashes".sha256;
40
41 local adns, dns = require "net.adns", require "net.dns";
42 local config = require "core.configmanager";
43 local connect_timeout = config.get("*", "core", "s2s_timeout") or 60;
44 local dns_timeout = config.get("*", "core", "dns_timeout") or 15;
45 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
46
47 dns.settimeout(dns_timeout);
48
49 local prosody = _G.prosody;
50 incoming_s2s = {};
51 prosody.incoming_s2s = incoming_s2s;
52 local incoming_s2s = incoming_s2s;
53
54 module "s2smanager"
55
56 function compare_srv_priorities(a,b)
57         return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
58 end
59
60 local bouncy_stanzas = { message = true, presence = true, iq = true };
61 local function bounce_sendq(session, reason)
62         local sendq = session.sendq;
63         if sendq then
64                 session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host));
65                 local dummy = {
66                         type = "s2sin";
67                         send = function(s)
68                                 (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", get_traceback());
69                         end;
70                         dummy = true;
71                 };
72                 for i, data in ipairs(sendq) do
73                         local reply = data[2];
74                         if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then
75                                 reply.attr.type = "error";
76                                 reply:tag("error", {type = "cancel"})
77                                         :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
78                                 if reason then
79                                         reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):text("Connection failed: "..reason):up();
80                                 end
81                                 core_process_stanza(dummy, reply);
82                         end
83                         sendq[i] = nil;
84                 end
85                 session.sendq = nil;
86         end
87 end
88
89 function send_to_host(from_host, to_host, data)
90         if not hosts[from_host] then
91                 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host);
92                 return false;
93         end
94         local host = hosts[from_host].s2sout[to_host];
95         if host then
96                 -- We have a connection to this host already
97                 if host.type == "s2sout_unauthed" and (data.name ~= "db:verify" or not host.dialback_key) then
98                         (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
99                         
100                         -- Queue stanza until we are able to send it
101                         if host.sendq then t_insert(host.sendq, {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)});
102                         else host.sendq = { {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)} }; end
103                         host.log("debug", "stanza [%s] queued ", data.name);
104                 elseif host.type == "local" or host.type == "component" then
105                         log("error", "Trying to send a stanza to ourselves??")
106                         log("error", "Traceback: %s", get_traceback());
107                         log("error", "Stanza: %s", tostring(data));
108                         return false;
109                 else
110                         (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
111                         -- FIXME
112                         if host.from_host ~= from_host then
113                                 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
114                                 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host));
115                         end
116                         host.sends2s(data);
117                         host.log("debug", "stanza sent over "..host.type);
118                 end
119         else
120                 log("debug", "opening a new outgoing connection for this stanza");
121                 local host_session = new_outgoing(from_host, to_host);
122
123                 -- Store in buffer
124                 host_session.sendq = { {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)} };
125                 log("debug", "stanza [%s] queued until connection complete", tostring(data.name));
126                 if (not host_session.connecting) and (not host_session.conn) then
127                         log("warn", "Connection to %s failed already, destroying session...", to_host);
128                         if not destroy_session(host_session, "Connection failed") then
129                                 -- Already destroyed, we need to bounce our stanza
130                                 bounce_sendq(host_session, host_session.destruction_reason);
131                         end
132                         return false;
133                 end
134         end
135         return true;
136 end
137
138 local open_sessions = 0;
139
140 function new_incoming(conn)
141         local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
142         if true then
143                 session.trace = newproxy(true);
144                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
145         end
146         open_sessions = open_sessions + 1;
147         local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
148         session.log = log;
149         local filter = initialize_filters(session);
150         session.sends2s = function (t)
151                 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)"));
152                 if t.name then
153                         t = filter("stanzas/out", t);
154                 end
155                 if t then
156                         t = filter("bytes/out", tostring(t));
157                         if t then
158                                 return w(conn, t);
159                         end
160                 end
161         end
162         incoming_s2s[session] = true;
163         add_task(connect_timeout, function ()
164                 if session.conn ~= conn or
165                    session.type == "s2sin" then
166                         return; -- Ok, we're connect[ed|ing]
167                 end
168                 -- Not connected, need to close session and clean up
169                 (session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity",
170                     session.from_host or "(unknown)", session.to_host or "(unknown)");
171                 session:close("connection-timeout");
172         end);
173         return session;
174 end
175
176 function new_outgoing(from_host, to_host, connect)
177                 local host_session = { to_host = to_host, from_host = from_host, host = from_host,
178                                        notopen = true, type = "s2sout_unauthed", direction = "outgoing",
179                                        open_stream = session_open_stream };
180                 
181                 hosts[from_host].s2sout[to_host] = host_session;
182                 
183                 host_session.close = destroy_session; -- This gets replaced by xmppserver_listener later
184                 
185                 local log;
186                 do
187                         local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
188                         log = logger_init(conn_name);
189                         host_session.log = log;
190                 end
191                 
192                 initialize_filters(host_session);
193                 
194                 if connect ~= false then
195                         -- Kick the connection attempting machine into life
196                         if not attempt_connection(host_session) then
197                                 -- Intentionally not returning here, the
198                                 -- session is needed, connected or not
199                                 destroy_session(host_session);
200                         end
201                 end
202                 
203                 if not host_session.sends2s then
204                         -- A sends2s which buffers data (until the stream is opened)
205                         -- note that data in this buffer will be sent before the stream is authed
206                         -- and will not be ack'd in any way, successful or otherwise
207                         local buffer;
208                         function host_session.sends2s(data)
209                                 if not buffer then
210                                         buffer = {};
211                                         host_session.send_buffer = buffer;
212                                 end
213                                 log("debug", "Buffering data on unconnected s2sout to %s", to_host);
214                                 buffer[#buffer+1] = data;
215                                 log("debug", "Buffered item %d: %s", #buffer, tostring(data));
216                         end
217                 end
218
219                 return host_session;
220 end
221
222
223 function attempt_connection(host_session, err)
224         local from_host, to_host = host_session.from_host, host_session.to_host;
225         local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
226         
227         if not connect_host then
228                 return false;
229         end
230         
231         if not err then -- This is our first attempt
232                 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host);
233                 host_session.connecting = true;
234                 local handle;
235                 handle = adns.lookup(function (answer)
236                         handle = nil;
237                         host_session.connecting = nil;
238                         if answer then
239                                 log("debug", to_host.." has SRV records, handling...");
240                                 local srv_hosts = {};
241                                 host_session.srv_hosts = srv_hosts;
242                                 for _, record in ipairs(answer) do
243                                         t_insert(srv_hosts, record.srv);
244                                 end
245                                 t_sort(srv_hosts, compare_srv_priorities);
246                                 
247                                 local srv_choice = srv_hosts[1];
248                                 host_session.srv_choice = 1;
249                                 if srv_choice then
250                                         connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
251                                         log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
252                                 end
253                         else
254                                 log("debug", to_host.." has no SRV records, falling back to A");
255                         end
256                         -- Try with SRV, or just the plain hostname if no SRV
257                         local ok, err = try_connect(host_session, connect_host, connect_port);
258                         if not ok then
259                                 if not attempt_connection(host_session, err) then
260                                         -- No more attempts will be made
261                                         destroy_session(host_session, err);
262                                 end
263                         end
264                 end, "_xmpp-server._tcp."..connect_host..".", "SRV");
265                 
266                 return true; -- Attempt in progress
267         elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV
268                 host_session.srv_choice = host_session.srv_choice + 1;
269                 local srv_choice = host_session.srv_hosts[host_session.srv_choice];
270                 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
271                 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port);
272         else
273                 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host));
274                 -- We're out of options
275                 return false;
276         end
277         
278         if not (connect_host and connect_port) then
279                 -- Likely we couldn't resolve DNS
280                 log("warn", "Hmm, we're without a host (%s) and port (%s) to connect to for %s, giving up :(", tostring(connect_host), tostring(connect_port), tostring(to_host));
281                 return false;
282         end
283         
284         return try_connect(host_session, connect_host, connect_port);
285 end
286
287 function try_connect(host_session, connect_host, connect_port)
288         host_session.connecting = true;
289         local handle;
290         handle = adns.lookup(function (reply, err)
291                 handle = nil;
292                 host_session.connecting = nil;
293                 
294                 -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
295                 if not (reply and reply[#reply] and reply[#reply].a) then
296                         local count = max_dns_depth;
297                         reply = dns.peek(connect_host, "CNAME", "IN");
298                         while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
299                                 log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
300                                 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
301                                 count = count - 1;
302                         end
303                 end
304                 -- end of CNAME resolving
305                 
306                 if reply and reply[#reply] and reply[#reply].a then
307                         log("debug", "DNS reply for %s gives us %s", connect_host, reply[#reply].a);
308                         local ok, err = make_connect(host_session, reply[#reply].a, connect_port);
309                         if not ok then
310                                 if not attempt_connection(host_session, err or "closed") then
311                                         err = err and (": "..err) or "";
312                                         destroy_session(host_session, "Connection failed"..err);
313                                 end
314                         end
315                 else
316                         log("debug", "DNS lookup failed to get a response for %s", connect_host);
317                         if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can
318                                 log("debug", "No other records to try for %s - destroying", host_session.to_host);
319                                 err = err and (": "..err) or "";
320                                 destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
321                         end
322                 end
323         end, connect_host, "A", "IN");
324
325         return true;
326 end
327
328 function make_connect(host_session, connect_host, connect_port)
329         (host_session.log or log)("info", "Beginning new connection attempt to %s (%s:%d)", host_session.to_host, connect_host, connect_port);
330         -- Ok, we're going to try to connect
331         
332         local from_host, to_host = host_session.from_host, host_session.to_host;
333         
334         local conn, handler = socket.tcp();
335         
336         if not conn then
337                 log("warn", "Failed to create outgoing connection, system error: %s", handler);
338                 return false, handler;
339         end
340
341         conn:settimeout(0);
342         local success, err = conn:connect(connect_host, connect_port);
343         if not success and err ~= "timeout" then
344                 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host, connect_port, err);
345                 return false, err;
346         end
347         
348         local cl = connlisteners_get("xmppserver");
349         conn = wrapclient(conn, connect_host, connect_port, cl, cl.default_mode or 1 );
350         host_session.conn = conn;
351         
352         local filter = initialize_filters(host_session);
353         local w, log = conn.write, host_session.log;
354         host_session.sends2s = function (t)
355                 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?"));
356                 if t.name then
357                         t = filter("stanzas/out", t);
358                 end
359                 if t then
360                         t = filter("bytes/out", tostring(t));
361                         if t then
362                                 return w(conn, tostring(t));
363                         end
364                 end
365         end
366         
367         -- Register this outgoing connection so that xmppserver_listener knows about it
368         -- otherwise it will assume it is a new incoming connection
369         cl.register_outgoing(conn, host_session);
370         
371         host_session:open_stream(from_host, to_host);
372         
373         log("debug", "Connection attempt in progress...");
374         add_task(connect_timeout, function ()
375                 if host_session.conn ~= conn or
376                    host_session.type == "s2sout" or
377                    host_session.connecting then
378                         return; -- Ok, we're connect[ed|ing]
379                 end
380                 -- Not connected, need to close session and clean up
381                 (host_session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity",
382                     host_session.from_host or "(unknown)", host_session.to_host or "(unknown)");
383                 host_session:close("connection-timeout");
384         end);
385         return true;
386 end
387
388 function session_open_stream(session, from, to)
389         session.sends2s(st.stanza("stream:stream", {
390                 xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
391                 ["xmlns:stream"]='http://etherx.jabber.org/streams',
392                 from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag());
393 end
394
395 local function check_cert_status(session)
396         local conn = session.conn:socket()
397         local cert
398         if conn.getpeercertificate then
399                 cert = conn:getpeercertificate()
400         end
401
402         if cert then
403                 local chain_valid, err = conn:getpeerchainvalid()
404                 if not chain_valid then
405                         session.cert_chain_status = "invalid";
406                         (session.log or log)("debug", "certificate chain validation result: %s", err);
407                 else
408                         session.cert_chain_status = "valid";
409
410                         local host = session.direction == "incoming" and session.from_host or session.to_host
411
412                         -- We'll go ahead and verify the asserted identity if the
413                         -- connecting server specified one.
414                         if host then
415                                 if cert_verify_identity(host, "xmpp-server", cert) then
416                                         session.cert_identity_status = "valid"
417                                 else
418                                         session.cert_identity_status = "invalid"
419                                 end
420                         end
421                 end
422         end
423 end
424
425 function streamopened(session, attr)
426         local send = session.sends2s;
427         
428         -- TODO: #29: SASL/TLS on s2s streams
429         session.version = tonumber(attr.version) or 0;
430         
431         -- TODO: Rename session.secure to session.encrypted
432         if session.secure == false then
433                 session.secure = true;
434         end
435
436         if session.direction == "incoming" then
437                 -- Send a reply stream header
438                 session.to_host = attr.to and nameprep(attr.to);
439                 session.from_host = attr.from and nameprep(attr.from);
440         
441                 session.streamid = uuid_gen();
442                 (session.log or log)("debug", "incoming s2s received <stream:stream>");
443                 if session.to_host then
444                         if not hosts[session.to_host] then
445                                 -- Attempting to connect to a host we don't serve
446                                 session:close({
447                                         condition = "host-unknown";
448                                         text = "This host does not serve "..session.to_host
449                                 });
450                                 return;
451                         elseif hosts[session.to_host].disallow_s2s then
452                                 -- Attempting to connect to a host that disallows s2s
453                                 session:close({
454                                         condition = "policy-violation";
455                                         text = "Server-to-server communication is not allowed to this host";
456                                 });
457                                 return;
458                         end
459                 end
460
461                 if session.secure and not session.cert_chain_status then check_cert_status(session); end
462
463                 send("<?xml version='1.0'?>");
464                 send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
465                                 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host, to=session.from_host, version=(session.version > 0 and "1.0" or nil) }):top_tag());
466                 if session.version >= 1.0 then
467                         local features = st.stanza("stream:features");
468                         
469                         if session.to_host then
470                                 hosts[session.to_host].events.fire_event("s2s-stream-features", { origin = session, features = features });
471                         else
472                                 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", session.from_host or "unknown host");
473                         end
474                         
475                         log("debug", "Sending stream features: %s", tostring(features));
476                         send(features);
477                 end
478         elseif session.direction == "outgoing" then
479                 -- If we are just using the connection for verifying dialback keys, we won't try and auth it
480                 if not attr.id then error("stream response did not give us a streamid!!!"); end
481                 session.streamid = attr.id;
482
483                 if session.secure and not session.cert_chain_status then check_cert_status(session); end
484
485                 -- Send unauthed buffer
486                 -- (stanzas which are fine to send before dialback)
487                 -- Note that this is *not* the stanza queue (which
488                 -- we can only send if auth succeeds) :)
489                 local send_buffer = session.send_buffer;
490                 if send_buffer and #send_buffer > 0 then
491                         log("debug", "Sending s2s send_buffer now...");
492                         for i, data in ipairs(send_buffer) do
493                                 session.sends2s(tostring(data));
494                                 send_buffer[i] = nil;
495                         end
496                 end
497                 session.send_buffer = nil;
498         
499                 -- If server is pre-1.0, don't wait for features, just do dialback
500                 if session.version < 1.0 then
501                         if not session.dialback_verifying then
502                                 log("debug", "Initiating dialback...");
503                                 initiate_dialback(session);
504                         else
505                                 mark_connected(session);
506                         end
507                 end
508         end
509         session.notopen = nil;
510 end
511
512 function streamclosed(session)
513         (session.log or log)("debug", "Received </stream:stream>");
514         session:close();
515 end
516
517 function initiate_dialback(session)
518         -- generate dialback key
519         session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
520         session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
521         session.log("info", "sent dialback key on outgoing s2s stream");
522 end
523
524 function generate_dialback(id, to, from)
525         return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
526 end
527
528 function verify_dialback(id, to, from, key)
529         return key == generate_dialback(id, to, from);
530 end
531
532 function make_authenticated(session, host)
533         if not session.secure then
534                 local local_host = session.direction == "incoming" and session.to_host or session.from_host;
535                 if config.get(local_host, "core", "s2s_require_encryption") then
536                         session:close({
537                                 condition = "policy-violation",
538                                 text = "Encrypted server-to-server communication is required but was not "
539                                        ..((session.direction == "outgoing" and "offered") or "used")
540                         });
541                 end
542         end
543         if session.type == "s2sout_unauthed" then
544                 session.type = "s2sout";
545         elseif session.type == "s2sin_unauthed" then
546                 session.type = "s2sin";
547                 if host then
548                         if not session.hosts[host] then session.hosts[host] = {}; end
549                         session.hosts[host].authed = true;
550                 end
551         elseif session.type == "s2sin" and host then
552                 if not session.hosts[host] then session.hosts[host] = {}; end
553                 session.hosts[host].authed = true;
554         else
555                 return false;
556         end
557         session.log("debug", "connection %s->%s is now authenticated", session.from_host or "(unknown)", session.to_host or "(unknown)");
558         
559         mark_connected(session);
560         
561         return true;
562 end
563
564 -- Stream is authorised, and ready for normal stanzas
565 function mark_connected(session)
566         local sendq, send = session.sendq, session.sends2s;
567         
568         local from, to = session.from_host, session.to_host;
569         
570         session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete");
571         
572         local send_to_host = send_to_host;
573         function session.send(data) return send_to_host(to, from, data); end
574         
575         local event_data = { session = session };
576         if session.type == "s2sout" then
577                 prosody.events.fire_event("s2sout-established", event_data);
578                 hosts[session.from_host].events.fire_event("s2sout-established", event_data);
579         else
580                 prosody.events.fire_event("s2sin-established", event_data);
581                 hosts[session.to_host].events.fire_event("s2sin-established", event_data);
582         end
583         
584         if session.direction == "outgoing" then
585                 if sendq then
586                         session.log("debug", "sending "..#sendq.." queued stanzas across new outgoing connection to "..session.to_host);
587                         for i, data in ipairs(sendq) do
588                                 send(data[1]);
589                                 sendq[i] = nil;
590                         end
591                         session.sendq = nil;
592                 end
593                 
594                 session.srv_hosts = nil;
595         end
596 end
597
598 local resting_session = { -- Resting, not dead
599                 destroyed = true;
600                 type = "s2s_destroyed";
601                 open_stream = function (session)
602                         session.log("debug", "Attempt to open stream on resting session");
603                 end;
604                 close = function (session)
605                         session.log("debug", "Attempt to close already-closed session");
606                 end;
607                 filter = function (type, data) return data; end;
608         }; resting_session.__index = resting_session;
609
610 function retire_session(session, reason)
611         local log = session.log or log;
612         for k in pairs(session) do
613                 if k ~= "trace" and k ~= "log" and k ~= "id" then
614                         session[k] = nil;
615                 end
616         end
617
618         session.destruction_reason = reason;
619
620         function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
621         function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
622         return setmetatable(session, resting_session);
623 end
624
625 function destroy_session(session, reason)
626         if session.destroyed then return; end
627         (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host));
628         
629         if session.direction == "outgoing" then
630                 hosts[session.from_host].s2sout[session.to_host] = nil;
631                 bounce_sendq(session, reason);
632         elseif session.direction == "incoming" then
633                 incoming_s2s[session] = nil;
634         end
635         
636         local event_data = { session = session, reason = reason };
637         if session.type == "s2sout" then
638                 prosody.events.fire_event("s2sout-destroyed", event_data);
639                 if hosts[session.from_host] then
640                         hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
641                 end
642         elseif session.type == "s2sin" then
643                 prosody.events.fire_event("s2sin-destroyed", event_data);
644                 if hosts[session.to_host] then
645                         hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
646                 end
647         end
648         
649         retire_session(session, reason); -- Clean session until it is GC'd
650         return true;
651 end
652
653 return _M;