mod_console: Override dofile() in the console environment (this lets print() print...
[prosody.git] / core / s2smanager.lua
index f0b802d87e50c164b0e84e9fd7a24a5d6e302178..e5fb699bacfc648ae8a3de869fe98dafd75a2061 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -23,6 +23,7 @@ local tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber,
 
 local idna_to_ascii = require "util.encodings".idna.to_ascii;
 local connlisteners_get = require "net.connlisteners".get;
+local initialize_filters = require "util.filters".initialize;
 local wrapclient = require "net.server".wrapclient;
 local modulemanager = require "core.modulemanager";
 local st = require "stanza";
@@ -41,9 +42,11 @@ local sha256_hash = require "util.hashes".sha256;
 local adns, dns = require "net.adns", require "net.dns";
 local config = require "core.configmanager";
 local connect_timeout = config.get("*", "core", "s2s_timeout") or 60;
-local dns_timeout = config.get("*", "core", "dns_timeout") or 60;
+local dns_timeout = config.get("*", "core", "dns_timeout") or 15;
 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
 
+dns.settimeout(dns_timeout);
+
 incoming_s2s = {};
 _G.prosody.incoming_s2s = incoming_s2s;
 local incoming_s2s = incoming_s2s;
@@ -68,7 +71,7 @@ local function bounce_sendq(session, reason)
                for i, data in ipairs(sendq) do
                        local reply = data[2];
                        local xmlns = reply.attr.xmlns;
-                       if not xmlns or xmlns == "jabber:client" or xmlns == "jabber:server" then
+                       if not xmlns then
                                reply.attr.type = "error";
                                reply:tag("error", {type = "cancel"})
                                        :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up();
@@ -91,7 +94,7 @@ function send_to_host(from_host, to_host, data)
        local host = hosts[from_host].s2sout[to_host];
        if host then
                -- We have a connection to this host already
-               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
+               if host.type == "s2sout_unauthed" and (data.name ~= "db:verify" or not host.dialback_key) then
                        (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host);
                        
                        -- Queue stanza until we are able to send it
@@ -137,7 +140,19 @@ function new_incoming(conn)
        open_sessions = open_sessions + 1;
        local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
        session.log = log;
-       session.sends2s = function (t) log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)")); w(conn, tostring(t)); end
+       local filter = initialize_filters(session);
+       session.sends2s = function (t)
+               log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)"));
+               if t.name then
+                       t = filter("stanzas/out", t);
+               end
+               if t then
+                       t = filter("bytes/out", tostring(t));
+                       if t then
+                               return w(conn, t);
+                       end
+               end
+       end
        incoming_s2s[session] = true;
        add_task(connect_timeout, function ()
                if session.conn ~= conn or
@@ -166,6 +181,8 @@ function new_outgoing(from_host, to_host, connect)
                        host_session.log = log;
                end
                
+               initialize_filters(host_session);
+               
                if connect ~= false then
                        -- Kick the connection attempting machine into life
                        attempt_connection(host_session);
@@ -193,7 +210,7 @@ end
 
 function attempt_connection(host_session, err)
        local from_host, to_host = host_session.from_host, host_session.to_host;
-       local connect_host, connect_port = idna_to_ascii(to_host), 5269;
+       local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
        
        if not connect_host then
                return false;
@@ -234,14 +251,6 @@ function attempt_connection(host_session, err)
                        end
                end, "_xmpp-server._tcp."..connect_host..".", "SRV");
                
-               -- Set handler for DNS timeout
-               add_task(dns_timeout, function ()
-                       if handle then
-                               adns.cancel(handle, true);
-                       end
-               end);
-               
-               log("debug", "DNS lookup for %s sent, waiting for response before we can connect", to_host);
                return true; -- Attempt in progress
        elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV
                host_session.srv_choice = host_session.srv_choice + 1;
@@ -294,13 +303,6 @@ function try_connect(host_session, connect_host, connect_port)
                end
        end, connect_host, "A", "IN");
 
-       -- Set handler for DNS timeout
-       add_task(dns_timeout, function ()
-               if handle then
-                       adns.cancel(handle, true);
-               end
-       end);
-       
        return true;
 end
 
@@ -328,13 +330,25 @@ function make_connect(host_session, connect_host, connect_port)
        conn = wrapclient(conn, connect_host, connect_port, cl, cl.default_mode or 1 );
        host_session.conn = conn;
        
+       local filter = initialize_filters(host_session);
+       local w, log = conn.write, host_session.log;
+       host_session.sends2s = function (t)
+               log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?"));
+               if t.name then
+                       t = filter("stanzas/out", t);
+               end
+               if t then
+                       t = filter("bytes/out", tostring(t));
+                       if t then
+                               return w(conn, tostring(t));
+                       end
+               end
+       end
+       
        -- Register this outgoing connection so that xmppserver_listener knows about it
        -- otherwise it will assume it is a new incoming connection
        cl.register_outgoing(conn, host_session);
        
-       local w, log = conn.write, host_session.log;
-       host_session.sends2s = function (t) log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?")); w(conn, tostring(t)); end
-       
        host_session:open_stream(from_host, to_host);
        
        log("debug", "Connection attempt in progress...");
@@ -376,10 +390,22 @@ function streamopened(session, attr)
        
                session.streamid = uuid_gen();
                (session.log or log)("debug", "incoming s2s received <stream:stream>");
-               if session.to_host and not hosts[session.to_host] then
-                       -- Attempting to connect to a host we don't serve
-                       session:close({ condition = "host-unknown"; text = "This host does not serve "..session.to_host });
-                       return;
+               if session.to_host then
+                       if not hosts[session.to_host] then
+                               -- Attempting to connect to a host we don't serve
+                               session:close({
+                                       condition = "host-unknown";
+                                       text = "This host does not serve "..session.to_host
+                               });
+                               return;
+                       elseif hosts[session.to_host].disallow_s2s then
+                               -- Attempting to connect to a host that disallows s2s
+                               session:close({
+                                       condition = "policy-violation";
+                                       text = "Server-to-server communication is not allowed to this host";
+                               });
+                               return;
+                       end
                end
                send("<?xml version='1.0'?>");
                send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
@@ -506,6 +532,7 @@ end
 
 local resting_session = { -- Resting, not dead
                destroyed = true;
+               type = "s2s_destroyed";
                open_stream = function (session)
                        session.log("debug", "Attempt to open stream on resting session");
                end;