d8ceb4efeb17ca58694a4d0a611cf6f39bd693ca
[prosody.git] / core / s2smanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
20     = tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber;
21
22 local idna_to_ascii = require "util.encodings".idna.to_ascii;
23 local connlisteners_get = require "net.connlisteners".get;
24 local wrapclient = require "net.server".wrapclient;
25 local modulemanager = require "core.modulemanager";
26 local st = require "stanza";
27 local stanza = st.stanza;
28 local nameprep = require "util.encodings".stringprep.nameprep;
29
30 local fire_event = require "core.eventmanager".fire_event;
31 local uuid_gen = require "util.uuid".generate;
32
33 local logger_init = require "util.logger".init;
34
35 local log = logger_init("s2smanager");
36
37 local sha256_hash = require "util.hashes".sha256;
38
39 local adns, dns = require "net.adns", require "net.dns";
40 local config = require "core.configmanager";
41 local connect_timeout = config.get("*", "core", "s2s_timeout") or 60;
42 local dns_timeout = config.get("*", "core", "dns_timeout") or 60;
43 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
44
45 incoming_s2s = {};
46 _G.prosody.incoming_s2s = incoming_s2s;
47 local incoming_s2s = incoming_s2s;
48
49 module "s2smanager"
50
51 local function compare_srv_priorities(a,b) return a.priority < b.priority or a.weight < b.weight; end
52
53 local function bounce_sendq(session, reason)
54         local sendq = session.sendq;
55         if sendq then
56                 session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host));
57                 local dummy = {
58                         type = "s2sin";
59                         send = function(s)
60                                 (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", get_traceback());
61                         end;
62                         dummy = true;
63                 };
64                 for i, data in ipairs(sendq) do
65                         local reply = data[2];
66                         local xmlns = reply.attr.xmlns;
67                         if not xmlns or xmlns == "jabber:client" or xmlns == "jabber:server" then
68                                 reply.attr.type = "error";
69                                 reply:tag("error", {type = "cancel"})
70                                         :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
71                                 if reason then
72                                         reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):text("Connection failed: "..reason):up();
73                                 end
74                                 core_process_stanza(dummy, reply);
75                         end
76                         sendq[i] = nil;
77                 end
78                 session.sendq = nil;
79         end
80 end
81
82 function send_to_host(from_host, to_host, data)
83         if not hosts[from_host] then
84                 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host);
85                 return false;
86         end
87         local host = hosts[from_host].s2sout[to_host];
88         if host then
89                 -- We have a connection to this host already
90                 if host.type == "s2sout_unauthed" and (data.name ~= "db:verify" or not host.dialback_key) and ((not data.xmlns) or data.xmlns == "jabber:client" or data.xmlns == "jabber:server") then
91                         (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
92                         
93                         -- Queue stanza until we are able to send it
94                         if host.sendq then t_insert(host.sendq, {tostring(data), st.reply(data)});
95                         else host.sendq = { {tostring(data), st.reply(data)} }; end
96                         host.log("debug", "stanza [%s] queued ", data.name);
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", get_traceback());
100                         log("error", "Stanza: %s", tostring(data));
101                 else
102                         (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
103                         -- FIXME
104                         if host.from_host ~= from_host then
105                                 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
106                                 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host));
107                         end
108                         host.sends2s(data);
109                         host.log("debug", "stanza sent over "..host.type);
110                 end
111         else
112                 log("debug", "opening a new outgoing connection for this stanza");
113                 local host_session = new_outgoing(from_host, to_host);
114
115                 -- Store in buffer
116                 host_session.sendq = { {tostring(data), st.reply(data)} };
117                 log("debug", "stanza [%s] queued until connection complete", tostring(data.name));
118                 if (not host_session.connecting) and (not host_session.conn) then
119                         log("warn", "Connection to %s failed already, destroying session...", to_host);
120                         destroy_session(host_session);
121                 end
122         end
123 end
124
125 local open_sessions = 0;
126
127 function new_incoming(conn)
128         local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
129         if true then
130                 session.trace = newproxy(true);
131                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
132         end
133         open_sessions = open_sessions + 1;
134         local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
135         session.log = log;
136         session.sends2s = function (t) log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)")); w(conn, tostring(t)); end
137         incoming_s2s[session] = true;
138         add_task(connect_timeout, function ()
139                 if session.conn ~= conn or
140                    session.type == "s2sin" then
141                         return; -- Ok, we're connect[ed|ing]
142                 end
143                 -- Not connected, need to close session and clean up
144                 (session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity",
145                     session.from_host or "(unknown)", session.to_host or "(unknown)");
146                 session:close("connection-timeout");
147         end);
148         return session;
149 end
150
151 function new_outgoing(from_host, to_host, connect)
152                 local host_session = { to_host = to_host, from_host = from_host, host = from_host,
153                                        notopen = true, type = "s2sout_unauthed", direction = "outgoing",
154                                        open_stream = session_open_stream };
155                 
156                 hosts[from_host].s2sout[to_host] = host_session;
157                 
158                 local log;
159                 do
160                         local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
161                         log = logger_init(conn_name);
162                         host_session.log = log;
163                 end
164                 
165                 if connect ~= false then
166                         -- Kick the connection attempting machine into life
167                         attempt_connection(host_session);
168                 end
169                 
170                 if not host_session.sends2s then
171                         -- A sends2s which buffers data (until the stream is opened)
172                         -- note that data in this buffer will be sent before the stream is authed
173                         -- and will not be ack'd in any way, successful or otherwise
174                         local buffer;
175                         function host_session.sends2s(data)
176                                 if not buffer then
177                                         buffer = {};
178                                         host_session.send_buffer = buffer;
179                                 end
180                                 log("debug", "Buffering data on unconnected s2sout to %s", to_host);
181                                 buffer[#buffer+1] = data;
182                                 log("debug", "Buffered item %d: %s", #buffer, tostring(data));
183                         end
184                 end
185
186                 return host_session;
187 end
188
189
190 function attempt_connection(host_session, err)
191         local from_host, to_host = host_session.from_host, host_session.to_host;
192         local connect_host, connect_port = idna_to_ascii(to_host), 5269;
193         
194         if not connect_host then
195                 return false;
196         end
197         
198         if not err then -- This is our first attempt
199                 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host);
200                 host_session.connecting = true;
201                 local handle;
202                 handle = adns.lookup(function (answer)
203                         handle = nil;
204                         host_session.connecting = nil;
205                         if answer then
206                                 log("debug", to_host.." has SRV records, handling...");
207                                 local srv_hosts = {};
208                                 host_session.srv_hosts = srv_hosts;
209                                 for _, record in ipairs(answer) do
210                                         t_insert(srv_hosts, record.srv);
211                                 end
212                                 t_sort(srv_hosts, compare_srv_priorities);
213                                 
214                                 local srv_choice = srv_hosts[1];
215                                 host_session.srv_choice = 1;
216                                 if srv_choice then
217                                         connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
218                                         log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
219                                 end
220                         else
221                                 log("debug", to_host.." has no SRV records, falling back to A");
222                         end
223                         -- Try with SRV, or just the plain hostname if no SRV
224                         local ok, err = try_connect(host_session, connect_host, connect_port);
225                         if not ok then
226                                 if not attempt_connection(host_session, err) then
227                                         -- No more attempts will be made
228                                         destroy_session(host_session, err);
229                                 end
230                         end
231                 end, "_xmpp-server._tcp."..connect_host..".", "SRV");
232                 
233                 -- Set handler for DNS timeout
234                 add_task(dns_timeout, function ()
235                         if handle then
236                                 adns.cancel(handle, true);
237                         end
238                 end);
239                 
240                 log("debug", "DNS lookup for %s sent, waiting for response before we can connect", to_host);
241                 return true; -- Attempt in progress
242         elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV
243                 host_session.srv_choice = host_session.srv_choice + 1;
244                 local srv_choice = host_session.srv_hosts[host_session.srv_choice];
245                 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
246                 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port);
247         else
248                 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host));
249                 -- We're out of options
250                 return false;
251         end
252         
253         if not (connect_host and connect_port) then
254                 -- Likely we couldn't resolve DNS
255                 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));
256                 return false;
257         end
258         
259         return try_connect(host_session, connect_host, connect_port);
260 end
261
262 function try_connect(host_session, connect_host, connect_port)
263         host_session.connecting = true;
264         local handle;
265         handle = adns.lookup(function (reply)
266                 handle = nil;
267                 host_session.connecting = nil;
268                 
269                 -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
270                 if not (reply and reply[#reply] and reply[#reply].a) then
271                         local count = max_dns_depth;
272                         reply = dns.peek(connect_host, "CNAME", "IN");
273                         while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
274                                 log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
275                                 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
276                                 count = count - 1;
277                         end
278                 end
279                 -- end of CNAME resolving
280                 
281                 if reply and reply[#reply] and reply[#reply].a then
282                         log("debug", "DNS reply for %s gives us %s", connect_host, reply[#reply].a);
283                         return make_connect(host_session, reply[#reply].a, connect_port);
284                 else
285                         log("debug", "DNS lookup failed to get a response for %s", connect_host);
286                         if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can
287                                 log("debug", "No other records to try for %s - destroying", host_session.to_host);
288                                 destroy_session(host_session, "DNS resolution failed"); -- End of the line, we can't
289                         end
290                 end
291         end, connect_host, "A", "IN");
292
293         -- Set handler for DNS timeout
294         add_task(dns_timeout, function ()
295                 if handle then
296                         adns.cancel(handle, true);
297                 end
298         end);
299         
300         return true;
301 end
302
303 function make_connect(host_session, connect_host, connect_port)
304         host_session.log("info", "Beginning new connection attempt to %s (%s:%d)", host_session.to_host, connect_host, connect_port);
305         -- Ok, we're going to try to connect
306         
307         local from_host, to_host = host_session.from_host, host_session.to_host;
308         
309         local conn, handler = socket.tcp()
310         
311         if not conn then
312                 log("warn", "Failed to create outgoing connection, system error: %s", handler);
313                 return false, handler;
314         end
315
316         conn:settimeout(0);
317         local success, err = conn:connect(connect_host, connect_port);
318         if not success and err ~= "timeout" then
319                 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host, connect_port, err);
320                 return false, err;
321         end
322         
323         local cl = connlisteners_get("xmppserver");
324         conn = wrapclient(conn, connect_host, connect_port, cl, cl.default_mode or 1 );
325         host_session.conn = conn;
326         
327         -- Register this outgoing connection so that xmppserver_listener knows about it
328         -- otherwise it will assume it is a new incoming connection
329         cl.register_outgoing(conn, host_session);
330         
331         local w, log = conn.write, host_session.log;
332         host_session.sends2s = function (t) log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?")); w(conn, tostring(t)); end
333         
334         host_session:open_stream(from_host, to_host);
335         
336         log("debug", "Connection attempt in progress...");
337         add_task(connect_timeout, function ()
338                 if host_session.conn ~= conn or
339                    host_session.type == "s2sout" or
340                    host_session.connecting then
341                         return; -- Ok, we're connect[ed|ing]
342                 end
343                 -- Not connected, need to close session and clean up
344                 (host_session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity",
345                     host_session.from_host or "(unknown)", host_session.to_host or "(unknown)");
346                 host_session:close("connection-timeout");
347         end);
348         return true;
349 end
350
351 function session_open_stream(session, from, to)
352         session.sends2s(st.stanza("stream:stream", {
353                 xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
354                 ["xmlns:stream"]='http://etherx.jabber.org/streams',
355                 from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag());
356 end
357
358 function streamopened(session, attr)
359         local send = session.sends2s;
360         
361         -- TODO: #29: SASL/TLS on s2s streams
362         session.version = tonumber(attr.version) or 0;
363         
364         if session.secure == false then
365                 session.secure = true;
366         end
367         
368         if session.version >= 1.0 and not (attr.to and attr.from) then
369                 (session.log or log)("warn", "Remote of stream "..(session.from_host or "(unknown)").."->"..(session.to_host or "(unknown)")
370                         .." failed to specify to (%s) and/or from (%s) hostname as per RFC", tostring(attr.to), tostring(attr.from));
371         end
372         
373         if session.direction == "incoming" then
374                 -- Send a reply stream header
375                 session.to_host = attr.to and nameprep(attr.to);
376                 session.from_host = attr.from and nameprep(attr.from);
377         
378                 session.streamid = uuid_gen();
379                 (session.log or log)("debug", "incoming s2s received <stream:stream>");
380                 if session.to_host and not hosts[session.to_host] then
381                         -- Attempting to connect to a host we don't serve
382                         session:close({ condition = "host-unknown"; text = "This host does not serve "..session.to_host });
383                         return;
384                 end
385                 send("<?xml version='1.0'?>");
386                 send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
387                                 ["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());
388                 if session.version >= 1.0 then
389                         local features = st.stanza("stream:features");
390                         
391                         if session.to_host then
392                                 hosts[session.to_host].events.fire_event("s2s-stream-features", { session = session, features = features });
393                         else
394                                 (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");
395                         end
396                         
397                         log("debug", "Sending stream features: %s", tostring(features));
398                         send(features);
399                 end
400         elseif session.direction == "outgoing" then
401                 -- If we are just using the connection for verifying dialback keys, we won't try and auth it
402                 if not attr.id then error("stream response did not give us a streamid!!!"); end
403                 session.streamid = attr.id;
404         
405                 -- Send unauthed buffer
406                 -- (stanzas which are fine to send before dialback)
407                 -- Note that this is *not* the stanza queue (which
408                 -- we can only send if auth succeeds) :)
409                 local send_buffer = session.send_buffer;
410                 if send_buffer and #send_buffer > 0 then
411                         log("debug", "Sending s2s send_buffer now...");
412                         for i, data in ipairs(send_buffer) do
413                                 session.sends2s(tostring(data));
414                                 send_buffer[i] = nil;
415                         end
416                 end
417                 session.send_buffer = nil;
418         
419                 -- If server is pre-1.0, don't wait for features, just do dialback
420                 if session.version < 1.0 then
421                         if not session.dialback_verifying then
422                                 log("debug", "Initiating dialback...");
423                                 initiate_dialback(session);
424                         else
425                                 mark_connected(session);
426                         end
427                 end
428         end
429         session.notopen = nil;
430 end
431
432 function streamclosed(session)
433         (session.log or log)("debug", "</stream:stream>");
434         if session.sends2s then
435                 session.sends2s("</stream:stream>");
436         end
437         session.notopen = true;
438 end
439
440 function initiate_dialback(session)
441         -- generate dialback key
442         session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
443         session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
444         session.log("info", "sent dialback key on outgoing s2s stream");
445 end
446
447 function generate_dialback(id, to, from)
448         return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
449 end
450
451 function verify_dialback(id, to, from, key)
452         return key == generate_dialback(id, to, from);
453 end
454
455 function make_authenticated(session, host)
456         if not session.secure then
457                 local local_host = session.direction == "incoming" and session.to_host or session.from_host;
458                 if config.get(local_host, "core", "s2s_require_encryption") then
459                         session:close({
460                                 condition = "policy-violation",
461                                 text = "Encrypted server-to-server communication is required but was not "
462                                        ..((session.direction == "outgoing" and "offered") or "used")
463                         });
464                 end
465         end
466         if session.type == "s2sout_unauthed" then
467                 session.type = "s2sout";
468         elseif session.type == "s2sin_unauthed" then
469                 session.type = "s2sin";
470                 if host then
471                         session.hosts[host].authed = true;
472                 end
473         elseif session.type == "s2sin" and host then
474                 session.hosts[host].authed = true;
475         else
476                 return false;
477         end
478         session.log("debug", "connection %s->%s is now authenticated", session.from_host or "(unknown)", session.to_host or "(unknown)");
479         
480         mark_connected(session);
481         
482         return true;
483 end
484
485 -- Stream is authorised, and ready for normal stanzas
486 function mark_connected(session)
487         local sendq, send = session.sendq, session.sends2s;
488         
489         local from, to = session.from_host, session.to_host;
490         
491         session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete");
492         
493         local send_to_host = send_to_host;
494         function session.send(data) send_to_host(to, from, data); end
495         
496         
497         if session.direction == "outgoing" then
498                 if sendq then
499                         session.log("debug", "sending "..#sendq.." queued stanzas across new outgoing connection to "..session.to_host);
500                         for i, data in ipairs(sendq) do
501                                 send(data[1]);
502                                 sendq[i] = nil;
503                         end
504                         session.sendq = nil;
505                 end
506                 
507                 session.srv_hosts = nil;
508         end
509 end
510
511 function destroy_session(session, reason)
512         (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host));
513         
514         if session.direction == "outgoing" then
515                 hosts[session.from_host].s2sout[session.to_host] = nil;
516                 bounce_sendq(session, reason);
517         elseif session.direction == "incoming" then
518                 incoming_s2s[session] = nil;
519         end
520         
521         for k in pairs(session) do
522                 if k ~= "trace" then
523                         session[k] = nil;
524                 end
525         end
526 end
527
528 return _M;