3 local sessions = sessions;
4 local socket = require "socket";
5 local format = string.format;
6 local t_insert = table.insert;
7 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber
8 = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber;
10 local connlisteners_get = require "net.connlisteners".get;
11 local wraptlsclient = require "net.server".wraptlsclient;
12 local modulemanager = require "core.modulemanager";
13 local st = require "stanza";
14 local stanza = st.stanza;
16 local uuid_gen = require "util.uuid".generate;
18 local logger_init = require "util.logger".init;
20 local log = logger_init("s2smanager");
22 local md5_hash = require "util.hashes".md5;
24 local dialback_secret = "This is very secret!!! Ha!";
26 local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" };
30 function connect_host(from_host, to_host)
33 function send_to_host(from_host, to_host, data)
34 local host = hosts[to_host];
36 -- We have a connection to this host already
37 if host.type == "s2sout_unauthed" then
38 host.log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now...");
39 if not host.notopen and not host.dialback_key then
40 host.log("debug", "dialback had not been initiated");
41 initiate_dialback(host);
44 -- Queue stanza until we are able to send it
45 if host.sendq then t_insert(host.sendq, data);
46 else host.sendq = { data }; end
48 (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
50 if hosts[to_host].from_host ~= from_host then
51 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
52 log("error", "We are going to send from %s instead of %s", hosts[to_host].from_host, from_host);
54 hosts[to_host].sends2s(data);
55 host.log("debug", "stanza sent over "..hosts[to_host].type);
58 log("debug", "opening a new outgoing connection for this stanza");
59 local host_session = new_outgoing(from_host, to_host);
61 host_session.sendq = { data };
65 function disconnect_host(host)
69 local open_sessions = 0;
71 function new_incoming(conn)
72 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming" };
74 session.trace = newproxy(true);
75 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end;
77 open_sessions = open_sessions + 1;
79 session.sends2s = function (t) w(tostring(t)); end
83 function new_outgoing(from_host, to_host)
84 local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
85 hosts[to_host] = host_session;
86 local cl = connlisteners_get("xmppserver");
88 local conn, handler = socket.tcp()
90 --FIXME: Below parameters (ports/ip) are incorrect (use SRV)
91 to_host = srvmap[to_host] or to_host;
94 local success, err = conn:connect(to_host, 5269);
96 log("warn", "s2s connect() failed: %s", err);
99 conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx );
100 host_session.conn = conn;
102 -- Register this outgoing connection so that xmppserver_listener knows about it
103 -- otherwise it will assume it is a new incoming connection
104 cl.register_outgoing(conn, host_session);
107 local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$");
108 host_session.log = logger_init(conn_name);
111 local w = conn.write;
112 host_session.sends2s = function (t) w(tostring(t)); end
114 conn.write(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host));
119 function streamopened(session, attr)
120 session.log("debug", "s2s stream opened");
121 local send = session.sends2s;
123 session.version = tonumber(attr.version) or 0;
124 if session.version >= 1.0 and not (attr.to and attr.from) then
125 print("to: "..tostring(attr.to).." from: "..tostring(attr.from));
126 --error(session.to_host.." failed to specify 'to' or 'from' hostname as per RFC");
127 log("warn", (session.to_host or "(unknown)").." failed to specify 'to' or 'from' hostname as per RFC");
130 if session.direction == "incoming" then
131 -- Send a reply stream header
133 for k,v in pairs(attr) do print("", tostring(k), ":::", tostring(v)); end
135 session.to_host = attr.to;
136 session.from_host = attr.from;
138 session.streamid = uuid_gen();
139 print(session, session.from_host, "incoming s2s stream opened");
140 send("<?xml version='1.0'?>");
141 send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag());
142 elseif session.direction == "outgoing" then
143 -- If we are just using the connection for verifying dialback keys, we won't try and auth it
144 if not attr.id then error("stream response did not give us a streamid!!!"); end
145 session.streamid = attr.id;
147 if not session.dialback_verifying then
148 initiate_dialback(session);
150 mark_connected(session);
155 modulemanager.fire_event("stream-features-s2s", session, features);
157 send("<stream:features>");
159 for _, feature in ipairs(features) do
160 send(tostring(feature));
163 send("</stream:features>");]]
164 log("info", "s2s stream opened successfully");
165 session.notopen = nil;
168 function initiate_dialback(session)
169 -- generate dialback key
170 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
171 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
172 session.log("info", "sent dialback key on outgoing s2s stream");
175 function generate_dialback(id, to, from)
176 return md5_hash(id..to..from..dialback_secret); -- FIXME: See XEP-185 and XEP-220
179 function verify_dialback(id, to, from, key)
180 return key == generate_dialback(id, to, from);
183 function make_authenticated(session)
184 if session.type == "s2sout_unauthed" then
185 session.type = "s2sout";
186 elseif session.type == "s2sin_unauthed" then
187 session.type = "s2sin";
191 session.log("info", "connection is now authenticated");
193 mark_connected(session);
198 function mark_connected(session)
199 local sendq, send = session.sendq, session.sends2s;
201 local from, to = session.from_host, session.to_host;
203 session.log("debug", session.direction.." s2s connection "..from.."->"..to.." is now complete");
205 local send_to_host = send_to_host;
206 function session.send(data) send_to_host(to, from, data); end
209 if session.direction == "outgoing" then
211 session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host);
212 for i, data in ipairs(sendq) do
221 function destroy_session(session)
222 (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host));
223 if session.direction == "outgoing" then
224 hosts[session.to_host] = nil;
227 session.disconnect = nil;
228 for k in pairs(session) do