mod_s2s, mod_saslauth, mod_compression: Refactor to have common code for opening...
[prosody.git] / plugins / mod_s2s / mod_s2s.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 module:set_global();
10
11 local prosody = prosody;
12 local hosts = prosody.hosts;
13 local core_process_stanza = prosody.core_process_stanza;
14
15 local tostring, type = tostring, type;
16 local t_insert = table.insert;
17 local xpcall, traceback = xpcall, debug.traceback;
18
19 local add_task = require "util.timer".add_task;
20 local st = require "util.stanza";
21 local initialize_filters = require "util.filters".initialize;
22 local nameprep = require "util.encodings".stringprep.nameprep;
23 local new_xmpp_stream = require "util.xmppstream".new;
24 local s2s_new_incoming = require "core.s2smanager".new_incoming;
25 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
26 local s2s_destroy_session = require "core.s2smanager".destroy_session;
27 local s2s_mark_connected = require "core.s2smanager".mark_connected;
28 local uuid_gen = require "util.uuid".generate;
29 local cert_verify_identity = require "util.x509".verify_identity;
30
31 local s2sout = module:require("s2sout");
32
33 local connect_timeout = module:get_option_number("s2s_timeout", 90);
34 local stream_close_timeout = module:get_option_number("s2s_close_timeout", 5);
35
36 local sessions = module:shared("sessions");
37
38 local log = module._log;
39
40 --- Handle stanzas to remote domains
41
42 local bouncy_stanzas = { message = true, presence = true, iq = true };
43 local function bounce_sendq(session, reason)
44         local sendq = session.sendq;
45         if not sendq then return; end
46         session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host));
47         local dummy = {
48                 type = "s2sin";
49                 send = function(s)
50                         (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback());
51                 end;
52                 dummy = true;
53         };
54         for i, data in ipairs(sendq) do
55                 local reply = data[2];
56                 if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then
57                         reply.attr.type = "error";
58                         reply:tag("error", {type = "cancel"})
59                                 :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
60                         if reason then
61                                 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})
62                                         :text("Server-to-server connection failed: "..reason):up();
63                         end
64                         core_process_stanza(dummy, reply);
65                 end
66                 sendq[i] = nil;
67         end
68         session.sendq = nil;
69 end
70
71 -- Handles stanzas to existing s2s sessions
72 function route_to_existing_session(event)
73         local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
74         if not hosts[from_host] then
75                 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host);
76                 return false;
77         end
78         local host = hosts[from_host].s2sout[to_host];
79         if host then
80                 -- We have a connection to this host already
81                 if host.type == "s2sout_unauthed" and (stanza.name ~= "db:verify" or not host.dialback_key) then
82                         (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
83
84                         -- Queue stanza until we are able to send it
85                         if host.sendq then t_insert(host.sendq, {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)});
86                         else host.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} }; end
87                         host.log("debug", "stanza [%s] queued ", stanza.name);
88                         return true;
89                 elseif host.type == "local" or host.type == "component" then
90                         log("error", "Trying to send a stanza to ourselves??")
91                         log("error", "Traceback: %s", traceback());
92                         log("error", "Stanza: %s", tostring(stanza));
93                         return false;
94                 else
95                         (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host);
96                         -- FIXME
97                         if host.from_host ~= from_host then
98                                 log("error", "WARNING! This might, possibly, be a bug, but it might not...");
99                                 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host));
100                         end
101                         if host.sends2s(stanza) then
102                                 host.log("debug", "stanza sent over %s", host.type);
103                                 return true;
104                         end
105                 end
106         end
107 end
108
109 -- Create a new outgoing session for a stanza
110 function route_to_new_session(event)
111         local from_host, to_host, stanza = event.from_host, event.to_host, event.stanza;
112         log("debug", "opening a new outgoing connection for this stanza");
113         local host_session = s2s_new_outgoing(from_host, to_host);
114
115         -- Store in buffer
116         host_session.bounce_sendq = bounce_sendq;
117         host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
118         log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name));
119         s2sout.initiate_connection(host_session);
120         if (not host_session.connecting) and (not host_session.conn) then
121                 log("warn", "Connection to %s failed already, destroying session...", to_host);
122                 s2s_destroy_session(host_session, "Connection failed");
123                 return false;
124         end
125         return true;
126 end
127
128 function module.add_host(module)
129         if module:get_option_boolean("disallow_s2s", false) then
130                 module:log("warn", "The 'disallow_s2s' config option is deprecated, please see http://prosody.im/doc/s2s#disabling");
131                 return nil, "This host has disallow_s2s set";
132         end
133         module:hook("route/remote", route_to_existing_session, 200);
134         module:hook("route/remote", route_to_new_session, 100);
135 end
136
137 --- Helper to check that a session peer's certificate is valid
138 local function check_cert_status(session)
139         local host = session.direction == "incoming" and session.from_host or session.to_host
140         local conn = session.conn:socket()
141         local cert
142         if conn.getpeercertificate then
143                 cert = conn:getpeercertificate()
144         end
145
146         if cert then
147                 local chain_valid, errors = conn:getpeerverification()
148                 -- Is there any interest in printing out all/the number of errors here?
149                 if not chain_valid then
150                         (session.log or log)("debug", "certificate chain validation result: invalid");
151                         for depth, t in ipairs(errors) do
152                                 (session.log or log)("debug", "certificate error(s) at depth %d: %s", depth-1, table.concat(t, ", "))
153                         end
154                         session.cert_chain_status = "invalid";
155                 else
156                         (session.log or log)("debug", "certificate chain validation result: valid");
157                         session.cert_chain_status = "valid";
158
159                         -- We'll go ahead and verify the asserted identity if the
160                         -- connecting server specified one.
161                         if host then
162                                 if cert_verify_identity(host, "xmpp-server", cert) then
163                                         session.cert_identity_status = "valid"
164                                 else
165                                         session.cert_identity_status = "invalid"
166                                 end
167                         end
168                 end
169         end
170         module:fire_event("s2s-check-certificate", { host = host, session = session, cert = cert });
171 end
172
173 --- XMPP stream event handlers
174
175 local stream_callbacks = { default_ns = "jabber:server", handlestanza =  core_process_stanza };
176
177 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
178
179 function stream_callbacks.streamopened(session, attr)
180         local send = session.sends2s;
181         
182         session.version = tonumber(attr.version) or 0;
183         
184         -- TODO: Rename session.secure to session.encrypted
185         if session.secure == false then
186                 session.secure = true;
187
188                 -- Check if TLS compression is used
189                 local sock = session.conn:socket();
190                 if sock.info then
191                         session.compressed = sock:info"compression";
192                 elseif sock.compression then
193                         session.compressed = sock:compression(); --COMPAT mw/luasec-hg
194                 end
195         end
196
197         if session.direction == "incoming" then
198                 -- Send a reply stream header
199                 
200                 -- Validate to/from
201                 local to, from = nameprep(attr.to), nameprep(attr.from);
202                 if not to and attr.to then -- COMPAT: Some servers do not reliably set 'to' (especially on stream restarts)
203                         session:close({ condition = "improper-addressing", text = "Invalid 'to' address" });
204                         return;
205                 end
206                 if not from and attr.from then -- COMPAT: Some servers do not reliably set 'from' (especially on stream restarts)
207                         session:close({ condition = "improper-addressing", text = "Invalid 'from' address" });
208                         return;
209                 end
210                 
211                 -- Set session.[from/to]_host if they have not been set already and if
212                 -- this session isn't already authenticated
213                 if session.type == "s2sin_unauthed" and from and not session.from_host then
214                         session.from_host = from;
215                 elseif from ~= session.from_host then
216                         session:close({ condition = "improper-addressing", text = "New stream 'from' attribute does not match original" });
217                         return;
218                 end
219                 if session.type == "s2sin_unauthed" and to and not session.to_host then
220                         session.to_host = to;
221                 elseif to ~= session.to_host then
222                         session:close({ condition = "improper-addressing", text = "New stream 'to' attribute does not match original" });
223                         return;
224                 end
225                 
226                 -- For convenience we'll put the sanitised values into these variables
227                 to, from = session.to_host, session.from_host;
228                 
229                 session.streamid = uuid_gen();
230                 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag());
231                 if to then
232                         if not hosts[to] then
233                                 -- Attempting to connect to a host we don't serve
234                                 session:close({
235                                         condition = "host-unknown";
236                                         text = "This host does not serve "..to
237                                 });
238                                 return;
239                         elseif not hosts[to].modules.s2s then
240                                 -- Attempting to connect to a host that disallows s2s
241                                 session:close({
242                                         condition = "policy-violation";
243                                         text = "Server-to-server communication is disabled for this host";
244                                 });
245                                 return;
246                         end
247                 end
248
249                 if session.secure and not session.cert_chain_status then check_cert_status(session); end
250
251                 session:open_stream()
252                 if session.version >= 1.0 then
253                         local features = st.stanza("stream:features");
254                         
255                         if to then
256                                 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features });
257                         else
258                                 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", from or "unknown host");
259                         end
260                         
261                         log("debug", "Sending stream features: %s", tostring(features));
262                         send(features);
263                 end
264         elseif session.direction == "outgoing" then
265                 -- If we are just using the connection for verifying dialback keys, we won't try and auth it
266                 if not attr.id then error("stream response did not give us a streamid!!!"); end
267                 session.streamid = attr.id;
268
269                 if session.secure and not session.cert_chain_status then check_cert_status(session); end
270
271                 -- Send unauthed buffer
272                 -- (stanzas which are fine to send before dialback)
273                 -- Note that this is *not* the stanza queue (which
274                 -- we can only send if auth succeeds) :)
275                 local send_buffer = session.send_buffer;
276                 if send_buffer and #send_buffer > 0 then
277                         log("debug", "Sending s2s send_buffer now...");
278                         for i, data in ipairs(send_buffer) do
279                                 session.sends2s(tostring(data));
280                                 send_buffer[i] = nil;
281                         end
282                 end
283                 session.send_buffer = nil;
284         
285                 -- If server is pre-1.0, don't wait for features, just do dialback
286                 if session.version < 1.0 then
287                         if not session.dialback_verifying then
288                                 hosts[session.from_host].events.fire_event("s2sout-authenticate-legacy", { origin = session });
289                         else
290                                 s2s_mark_connected(session);
291                         end
292                 end
293         end
294         session.notopen = nil;
295 end
296
297 function stream_callbacks.streamclosed(session)
298         (session.log or log)("debug", "Received </stream:stream>");
299         session:close(false);
300 end
301
302 function stream_callbacks.error(session, error, data)
303         if error == "no-stream" then
304                 session:close("invalid-namespace");
305         elseif error == "parse-error" then
306                 session.log("debug", "Server-to-server XML parse error: %s", tostring(error));
307                 session:close("not-well-formed");
308         elseif error == "stream-error" then
309                 local condition, text = "undefined-condition";
310                 for child in data:children() do
311                         if child.attr.xmlns == xmlns_xmpp_streams then
312                                 if child.name ~= "text" then
313                                         condition = child.name;
314                                 else
315                                         text = child:get_text();
316                                 end
317                                 if condition ~= "undefined-condition" and text then
318                                         break;
319                                 end
320                         end
321                 end
322                 text = condition .. (text and (" ("..text..")") or "");
323                 session.log("info", "Session closed by remote with error: %s", text);
324                 session:close(nil, text);
325         end
326 end
327
328 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
329 function stream_callbacks.handlestanza(session, stanza)
330         if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
331                 stanza.attr.xmlns = nil;
332         end
333         stanza = session.filter("stanzas/in", stanza);
334         if stanza then
335                 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
336         end
337 end
338
339 local listener = {};
340
341 --- Session methods
342 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
343 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
344 local function session_close(session, reason, remote_reason)
345         local log = session.log or log;
346         if session.conn then
347                 if session.notopen then
348                         session:open_stream()
349                 end
350                 if reason then -- nil == no err, initiated by us, false == initiated by remote
351                         if type(reason) == "string" then -- assume stream error
352                                 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason);
353                                 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
354                         elseif type(reason) == "table" then
355                                 if reason.condition then
356                                         local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
357                                         if reason.text then
358                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
359                                         end
360                                         if reason.extra then
361                                                 stanza:add_child(reason.extra);
362                                         end
363                                         log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza));
364                                         session.sends2s(stanza);
365                                 elseif reason.name then -- a stanza
366                                         log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s", session.from_host or "(unknown host)", session.to_host or "(unknown host)", session.type, tostring(reason));
367                                         session.sends2s(reason);
368                                 end
369                         end
370                 end
371
372                 session.sends2s("</stream:stream>");
373                 function session.sends2s() return false; end
374                 
375                 local reason = remote_reason or (reason and (reason.text or reason.condition)) or reason;
376                 session.log("info", "%s s2s stream %s->%s closed: %s", session.direction, session.from_host or "(unknown host)", session.to_host or "(unknown host)", reason or "stream closed");
377                 
378                 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
379                 local conn = session.conn;
380                 if reason == nil and not session.notopen and session.type == "s2sin" then
381                         add_task(stream_close_timeout, function ()
382                                 if not session.destroyed then
383                                         session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
384                                         s2s_destroy_session(session, reason);
385                                         conn:close();
386                                 end
387                         end);
388                 else
389                         s2s_destroy_session(session, reason);
390                         conn:close(); -- Close immediately, as this is an outgoing connection or is not authed
391                 end
392         end
393 end
394
395 function session_open_stream(session, from, to)
396         local from = from or session.from_host;
397         local to = to or session.to_host;
398         local attr = {
399                 ["xmlns:stream"] = 'http://etherx.jabber.org/streams',
400                 xmlns = 'jabber:server',
401                 version = session.version and (session.version > 0 and "1.0" or nil),
402                 ["xml:lang"] = 'en',
403                 id = session.streamid,
404                 from = from, to = to,
405         }
406         local local_host = session.direction == "outgoing" and from or to;
407         if not local_host or hosts[local_host].modules.dialback then
408                 attr["xmlns:db"] = 'jabber:server:dialback';
409         end
410
411         session.sends2s("<?xml version='1.0'?>");
412         session.sends2s(st.stanza("stream:stream", attr):top_tag());
413         return true;
414 end
415
416 -- Session initialization logic shared by incoming and outgoing
417 local function initialize_session(session)
418         local stream = new_xmpp_stream(session, stream_callbacks);
419         session.stream = stream;
420         
421         session.notopen = true;
422                 
423         function session.reset_stream()
424                 session.notopen = true;
425                 session.stream:reset();
426         end
427
428         session.open_stream = session_open_stream;
429         
430         local filter = session.filter;
431         function session.data(data)
432                 data = filter("bytes/in", data);
433                 if data then
434                         local ok, err = stream:feed(data);
435                         if ok then return; end
436                         (session.log or log)("warn", "Received invalid XML: %s", data);
437                         (session.log or log)("warn", "Problem was: %s", err);
438                         session:close("not-well-formed");
439                 end
440         end
441
442         session.close = session_close;
443
444         local handlestanza = stream_callbacks.handlestanza;
445         function session.dispatch_stanza(session, stanza)
446                 return handlestanza(session, stanza);
447         end
448
449         add_task(connect_timeout, function ()
450                 if session.type == "s2sin" or session.type == "s2sout" then
451                         return; -- Ok, we're connected
452                 elseif session.type == "s2s_destroyed" then
453                         return; -- Session already destroyed
454                 end
455                 -- Not connected, need to close session and clean up
456                 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity",
457                 session.from_host or "(unknown)", session.to_host or "(unknown)");
458                 session:close("connection-timeout");
459         end);
460 end
461
462 function listener.onconnect(conn)
463         local session = sessions[conn];
464         if not session then -- New incoming connection
465                 session = s2s_new_incoming(conn);
466                 sessions[conn] = session;
467                 session.log("debug", "Incoming s2s connection");
468
469                 local filter = initialize_filters(session);
470                 local w = conn.write;
471                 session.sends2s = function (t)
472                         log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)"));
473                         if t.name then
474                                 t = filter("stanzas/out", t);
475                         end
476                         if t then
477                                 t = filter("bytes/out", tostring(t));
478                                 if t then
479                                         return w(conn, t);
480                                 end
481                         end
482                 end
483         
484                 initialize_session(session);
485         else -- Outgoing session connected
486                 session:open_stream(session.from_host, session.to_host);
487         end
488 end
489
490 function listener.onincoming(conn, data)
491         local session = sessions[conn];
492         if session then
493                 session.data(data);
494         end
495 end
496         
497 function listener.onstatus(conn, status)
498         if status == "ssl-handshake-complete" then
499                 local session = sessions[conn];
500                 if session and session.direction == "outgoing" then
501                         session.log("debug", "Sending stream header...");
502                         session:open_stream(session.from_host, session.to_host);
503                 end
504         end
505 end
506
507 function listener.ondisconnect(conn, err)
508         local session = sessions[conn];
509         if session then
510                 sessions[conn] = nil;
511                 if err and session.direction == "outgoing" and session.notopen then
512                         (session.log or log)("debug", "s2s connection attempt failed: %s", err);
513                         if s2sout.attempt_connection(session, err) then
514                                 (session.log or log)("debug", "...so we're going to try another target");
515                                 return; -- Session lives for now
516                         end
517                 end
518                 (session.log or log)("debug", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "connection closed"));
519                 s2s_destroy_session(session, err);
520         end
521 end
522
523 function listener.register_outgoing(conn, session)
524         session.direction = "outgoing";
525         sessions[conn] = session;
526         initialize_session(session);
527 end
528
529 s2sout.set_listener(listener);
530
531 module:hook("server-stopping", function(event)
532         local reason = event.reason;
533         for _, session in pairs(sessions) do
534                 session:close{ condition = "system-shutdown", text = reason };
535         end
536 end,500);
537
538
539
540 module:provides("net", {
541         name = "s2s";
542         listener = listener;
543         default_port = 5269;
544         encryption = "starttls";
545         multiplex = {
546                 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
547         };
548 });
549