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