mod_bosh: Store time to destroy session in inactive_sessions, removing dependency...
[prosody.git] / plugins / mod_dialback.lua
index 9a1e28f95a5b88d76e22ed7806d978191cf1748d..e27f8657b2a4b24b7fb90321cac49ae6ed5af7c3 100644 (file)
@@ -12,7 +12,6 @@ local send_s2s = require "core.s2smanager".send_to_host;
 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
 local s2s_initiate_dialback = require "core.s2smanager".initiate_dialback;
 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
-local s2s_destroy_session = require "core.s2smanager".destroy_session;
 
 local log = module._log;
 
@@ -61,7 +60,7 @@ module:hook("stanza/jabber:server:dialback:result", function(event)
                        return true;
                end
                
-               dialback_requests[attr.from] = origin;
+               dialback_requests[attr.from.."/"..origin.streamid] = origin;
                
                if not origin.from_host then
                        -- Just used for friendlier logging
@@ -84,8 +83,8 @@ module:hook("stanza/jabber:server:dialback:verify", function(event)
        
        if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
                local attr = stanza.attr;
-               local dialback_verifying = dialback_requests[attr.from];
-               if dialback_verifying then
+               local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
+               if dialback_verifying and attr.from == origin.to_host then
                        local valid;
                        if attr.type == "valid" then
                                s2s_make_authenticated(dialback_verifying, attr.from);
@@ -102,7 +101,7 @@ module:hook("stanza/jabber:server:dialback:verify", function(event)
                                                st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
                                                                :text(dialback_verifying.hosts[attr.from].dialback_key));
                        end
-                       dialback_requests[attr.from] = nil;
+                       dialback_requests[attr.from.."/"..(attr.id or "")] = nil;
                end
                return true;
        end
@@ -126,18 +125,28 @@ module:hook("stanza/jabber:server:dialback:result", function(event)
                if stanza.attr.type == "valid" then
                        s2s_make_authenticated(origin, attr.from);
                else
-                       s2s_destroy_session(origin)
+                       origin:close("not-authorized", "dialback authentication failed");
                end
                return true;
        end
 end);
 
+module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
+       if origin.external_auth == "failed" then
+               module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
+               s2s_initiate_dialback(origin);
+               return true;
+       end
+end, 100);
+
 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
+       if not origin.external_auth or origin.external_auth == "failed" then
                s2s_initiate_dialback(origin);
                return true;
-       end, 100);
+       end
+end, 100);
 
 -- Offer dialback to incoming hosts
 module:hook("s2s-stream-features", function (data)
-               data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):tag("optional"):up():up();
-       end);
+       data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();
+end);