2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local core_process_stanza = function(a, b) core_process_stanza(a, b); end
13 local format = string.format;
14 local t_insert, t_sort = table.insert, table.sort;
15 local get_traceback = debug.traceback;
16 local tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable
17 = tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable;
19 local initialize_filters = require "util.filters".initialize;
20 local wrapclient = require "net.server".wrapclient;
21 local st = require "stanza";
22 local stanza = st.stanza;
23 local nameprep = require "util.encodings".stringprep.nameprep;
24 local cert_verify_identity = require "util.x509".verify_identity;
25 local new_ip = require "util.ip".new_ip;
26 local rfc3484_dest = require "util.rfc3484".destination;
28 local fire_event = prosody.events.fire_event;
29 local uuid_gen = require "util.uuid".generate;
31 local logger_init = require "util.logger".init;
33 local log = logger_init("s2smanager");
35 local sha256_hash = require "util.hashes".sha256;
37 local adns, dns = require "net.adns", require "net.dns";
38 local config = require "core.configmanager";
39 local dns_timeout = config.get("*", "core", "dns_timeout") or 15;
40 local cfg_sources = config.get("*", "core", "s2s_interface")
41 or config.get("*", "core", "interface");
44 --FIXME: s2sout should create its own resolver w/ timeout
45 dns.settimeout(dns_timeout);
47 local prosody = _G.prosody;
49 prosody.incoming_s2s = incoming_s2s;
50 local incoming_s2s = incoming_s2s;
54 local open_sessions = 0;
56 function new_incoming(conn)
57 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
59 session.trace = newproxy(true);
60 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
62 open_sessions = open_sessions + 1;
63 session.log = logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
64 incoming_s2s[session] = true;
68 function new_outgoing(from_host, to_host, connect)
69 local host_session = { to_host = to_host, from_host = from_host, host = from_host,
70 notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
71 hosts[from_host].s2sout[to_host] = host_session;
72 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
73 host_session.log = logger_init(conn_name);
77 function make_authenticated(session, host)
78 if not session.secure then
79 local local_host = session.direction == "incoming" and session.to_host or session.from_host;
80 if config.get(local_host, "core", "s2s_require_encryption") then
82 condition = "policy-violation",
83 text = "Encrypted server-to-server communication is required but was not "
84 ..((session.direction == "outgoing" and "offered") or "used")
88 if session.type == "s2sout_unauthed" then
89 session.type = "s2sout";
90 elseif session.type == "s2sin_unauthed" then
91 session.type = "s2sin";
93 if not session.hosts[host] then session.hosts[host] = {}; end
94 session.hosts[host].authed = true;
96 elseif session.type == "s2sin" and host then
97 if not session.hosts[host] then session.hosts[host] = {}; end
98 session.hosts[host].authed = true;
102 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host or "(unknown)", session.to_host or "(unknown)", host);
104 mark_connected(session);
109 -- Stream is authorised, and ready for normal stanzas
110 function mark_connected(session)
111 local sendq, send = session.sendq, session.sends2s;
113 local from, to = session.from_host, session.to_host;
115 session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete");
117 local event_data = { session = session };
118 if session.type == "s2sout" then
119 prosody.events.fire_event("s2sout-established", event_data);
120 hosts[from].events.fire_event("s2sout-established", event_data);
122 local host_session = hosts[to];
123 session.send = function(stanza)
124 host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
127 prosody.events.fire_event("s2sin-established", event_data);
128 hosts[to].events.fire_event("s2sin-established", event_data);
131 if session.direction == "outgoing" then
133 session.log("debug", "sending "..#sendq.." queued stanzas across new outgoing connection to "..session.to_host);
134 for i, data in ipairs(sendq) do
141 session.ip_hosts = nil;
142 session.srv_hosts = nil;
146 local resting_session = { -- Resting, not dead
148 type = "s2s_destroyed";
149 open_stream = function (session)
150 session.log("debug", "Attempt to open stream on resting session");
152 close = function (session)
153 session.log("debug", "Attempt to close already-closed session");
155 filter = function (type, data) return data; end;
156 }; resting_session.__index = resting_session;
158 function retire_session(session, reason)
159 local log = session.log or log;
160 for k in pairs(session) do
161 if k ~= "trace" and k ~= "log" and k ~= "id" then
166 session.destruction_reason = reason;
168 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
169 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
170 return setmetatable(session, resting_session);
173 function destroy_session(session, reason)
174 if session.destroyed then return; end
175 (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
177 if session.direction == "outgoing" then
178 hosts[session.from_host].s2sout[session.to_host] = nil;
179 session:bounce_sendq(reason);
180 elseif session.direction == "incoming" then
181 incoming_s2s[session] = nil;
184 local event_data = { session = session, reason = reason };
185 if session.type == "s2sout" then
186 prosody.events.fire_event("s2sout-destroyed", event_data);
187 if hosts[session.from_host] then
188 hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
190 elseif session.type == "s2sin" then
191 prosody.events.fire_event("s2sin-destroyed", event_data);
192 if hosts[session.to_host] then
193 hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
197 retire_session(session, reason); -- Clean session until it is GC'd