mod_dialback, mod_saslauth: Remove broken fallback to dialback on SASL EXTERNAL failure
[prosody.git] / plugins / mod_s2s / s2sout.lib.lua
index d2c6023e65c556418fe45d2c665f15f7f964d83e..942a618d6244b86ae09b705b894810ca4fc3483f 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- 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.
 --
@@ -13,12 +13,12 @@ local wrapclient = require "net.server".wrapclient;
 local initialize_filters = require "util.filters".initialize;
 local idna_to_ascii = require "util.encodings".idna.to_ascii;
 local new_ip = require "util.ip".new_ip;
-local rfc3484_dest = require "util.rfc3484".destination;
+local rfc6724_dest = require "util.rfc6724".destination;
 local socket = require "socket";
 local adns = require "net.adns";
 local dns = require "net.dns";
 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs;
-local st = require "util.stanza";
+local local_addresses = require "util.net".local_addresses;
 
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 
@@ -44,24 +44,17 @@ local function compare_srv_priorities(a,b)
        return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
 end
 
-local function session_open_stream(session, from, to)
-       session.sends2s(st.stanza("stream:stream", {
-               xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
-               ["xmlns:stream"]='http://etherx.jabber.org/streams',
-               from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag());
-end
-
 function s2sout.initiate_connection(host_session)
        initialize_filters(host_session);
-       host_session.open_stream = session_open_stream;
-       
+       host_session.version = 1;
+
        -- Kick the connection attempting machine into life
        if not s2sout.attempt_connection(host_session) then
                -- Intentionally not returning here, the
                -- session is needed, connected or not
                s2s_destroy_session(host_session);
        end
-       
+
        if not host_session.sends2s then
                -- A sends2s which buffers data (until the stream is opened)
                -- note that data in this buffer will be sent before the stream is authed
@@ -80,13 +73,13 @@ function s2sout.initiate_connection(host_session)
 end
 
 function s2sout.attempt_connection(host_session, err)
-       local from_host, to_host = host_session.from_host, host_session.to_host;
+       local to_host = host_session.to_host;
        local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
-       
+
        if not connect_host then
                return false;
        end
-       
+
        if not err then -- This is our first attempt
                log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host);
                host_session.connecting = true;
@@ -94,20 +87,20 @@ function s2sout.attempt_connection(host_session, err)
                handle = adns.lookup(function (answer)
                        handle = nil;
                        host_session.connecting = nil;
-                       if answer then
-                               log("debug", to_host.." has SRV records, handling...");
-                               local srv_hosts = {};
+                       if answer and #answer > 0 then
+                               log("debug", "%s has SRV records, handling...", to_host);
+                               local srv_hosts = { answer = answer };
                                host_session.srv_hosts = srv_hosts;
                                for _, record in ipairs(answer) do
                                        t_insert(srv_hosts, record.srv);
                                end
                                if #srv_hosts == 1 and srv_hosts[1].target == "." then
-                                       log("debug", to_host.." does not provide a XMPP service");
+                                       log("debug", "%s does not provide a XMPP service", to_host);
                                        s2s_destroy_session(host_session, err); -- Nothing to see here
                                        return;
                                end
                                t_sort(srv_hosts, compare_srv_priorities);
-                               
+
                                local srv_choice = srv_hosts[1];
                                host_session.srv_choice = 1;
                                if srv_choice then
@@ -115,7 +108,7 @@ function s2sout.attempt_connection(host_session, err)
                                        log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
                                end
                        else
-                               log("debug", to_host.." has no SRV records, falling back to A/AAAA");
+                               log("debug", "%s has no SRV records, falling back to A/AAAA", to_host);
                        end
                        -- Try with SRV, or just the plain hostname if no SRV
                        local ok, err = s2sout.try_connect(host_session, connect_host, connect_port);
@@ -126,7 +119,7 @@ function s2sout.attempt_connection(host_session, err)
                                end
                        end
                end, "_xmpp-server._tcp."..connect_host..".", "SRV");
-               
+
                return true; -- Attempt in progress
        elseif host_session.ip_hosts then
                return s2sout.try_connect(host_session, connect_host, connect_port, err);
@@ -136,11 +129,11 @@ function s2sout.attempt_connection(host_session, err)
                connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
                host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port);
        else
-               host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host));
+               host_session.log("info", "Failed in all attempts to connect to %s", tostring(host_session.to_host));
                -- We're out of options
                return false;
        end
-       
+
        if not (connect_host and connect_port) then
                -- Likely we couldn't resolve DNS
                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));
@@ -197,7 +190,7 @@ function s2sout.try_connect(host_session, connect_host, connect_port, err)
 
                                if have_other_result then
                                        if #IPs > 0 then
-                                               rfc3484_dest(host_session.ip_hosts, sources);
+                                               rfc6724_dest(host_session.ip_hosts, sources);
                                                for i = 1, #IPs do
                                                        IPs[i] = {ip = IPs[i], port = connect_port};
                                                end
@@ -233,7 +226,7 @@ function s2sout.try_connect(host_session, connect_host, connect_port, err)
 
                                if have_other_result then
                                        if #IPs > 0 then
-                                               rfc3484_dest(host_session.ip_hosts, sources);
+                                               rfc6724_dest(host_session.ip_hosts, sources);
                                                for i = 1, #IPs do
                                                        IPs[i] = {ip = IPs[i], port = connect_port};
                                                end
@@ -272,18 +265,23 @@ function s2sout.try_connect(host_session, connect_host, connect_port, err)
 end
 
 function s2sout.make_connect(host_session, connect_host, connect_port)
-       (host_session.log or log)("info", "Beginning new connection attempt to %s ([%s]:%d)", host_session.to_host, connect_host.addr, connect_port);
-       -- Ok, we're going to try to connect
-       
-       local from_host, to_host = host_session.from_host, host_session.to_host;
-       
+       (host_session.log or log)("debug", "Beginning new connection attempt to %s ([%s]:%d)", host_session.to_host, connect_host.addr, connect_port);
+
+       -- Reset secure flag in case this is another
+       -- connection attempt after a failed STARTTLS
+       host_session.secure = nil;
+       host_session.encrypted = nil;
+
        local conn, handler;
-       if connect_host.proto == "IPv4" then
+       local proto = connect_host.proto;
+       if proto == "IPv4" then
                conn, handler = socket.tcp();
-       else
+       elseif proto == "IPv6" and socket.tcp6 then
                conn, handler = socket.tcp6();
+       else
+               handler = "Unsupported protocol: "..tostring(proto);
        end
-       
+
        if not conn then
                log("warn", "Failed to create outgoing connection, system error: %s", handler);
                return false, handler;
@@ -295,29 +293,14 @@ function s2sout.make_connect(host_session, connect_host, connect_port)
                log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host.addr, connect_port, err);
                return false, err;
        end
-       
+
        conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, "*a");
        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
        s2s_listener.register_outgoing(conn, host_session);
-       
+
        log("debug", "Connection attempt in progress...");
        return true;
 end
@@ -332,20 +315,12 @@ module:hook_global("service-added", function (event)
        end
        for source, _ in pairs(s2s_sources) do
                if source == "*" or source == "0.0.0.0" then
-                       if not socket.local_addresses then
-                               sources[#sources + 1] = new_ip("0.0.0.0", "IPv4");
-                       else
-                               for _, addr in ipairs(socket.local_addresses("ipv4", true)) do
-                                       sources[#sources + 1] = new_ip(addr, "IPv4");
-                               end
+                       for _, addr in ipairs(local_addresses("ipv4", true)) do
+                               sources[#sources + 1] = new_ip(addr, "IPv4");
                        end
                elseif source == "::" then
-                       if not socket.local_addresses then
-                               sources[#sources + 1] = new_ip("::", "IPv6");
-                       else
-                               for _, addr in ipairs(socket.local_addresses("ipv6", true)) do
-                                       sources[#sources + 1] = new_ip(addr, "IPv6");
-                               end
+                       for _, addr in ipairs(local_addresses("ipv6", true)) do
+                               sources[#sources + 1] = new_ip(addr, "IPv6");
                        end
                else
                        sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
@@ -358,6 +333,9 @@ module:hook_global("service-added", function (event)
                        has_ipv4 = true;
                end
        end
+       if not (has_ipv4 or has_ipv6)  then
+               module:log("warn", "No local IPv4 or IPv6 addresses detected, outgoing connections may fail");
+       end
 end);
 
 return s2sout;