mod_dialback: Move d-w-d after to/from validation
[prosody.git] / plugins / mod_component.lua
index 5b1eefc767537797769866a9e8cb6657a3745299..297609d830d1a55ffab5c797477cef2c9e83fd19 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.
 --
@@ -9,6 +9,8 @@
 module:set_global();
 
 local t_concat = table.concat;
+local xpcall, tostring, type = xpcall, tostring, type;
+local traceback = debug.traceback;
 
 local logger = require "util.logger";
 local sha1 = require "util.hashes".sha1;
@@ -18,16 +20,20 @@ local jid_split = require "util.jid".split;
 local new_xmpp_stream = require "util.xmppstream".new;
 local uuid_gen = require "util.uuid".generate;
 
+local core_process_stanza = prosody.core_process_stanza;
+local hosts = prosody.hosts;
 
 local log = module._log;
 
+local opt_keepalives = module:get_option_boolean("component_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
+
 local sessions = module:shared("sessions");
 
 function module.add_host(module)
        if module:get_host_type() ~= "component" then
                error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
        end
-       
+
        local env = module.environment;
        env.connected = false;
 
@@ -38,26 +44,26 @@ function module.add_host(module)
                send = nil;
                session.on_destroy = nil;
        end
-       
+
        -- Handle authentication attempts by component
        local function handle_component_auth(event)
                local session, stanza = event.origin, event.stanza;
-               
-               if session.type ~= "component" then return; end
-       
+
+               if session.type ~= "component_unauthed" then return; end
+
                if (not session.host) or #stanza.tags > 0 then
                        (session.log or log)("warn", "Invalid component handshake for host: %s", session.host);
                        session:close("not-authorized");
                        return true;
                end
-               
+
                local secret = module:get_option("component_secret");
                if not secret then
                        (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
                        session:close("not-authorized");
                        return true;
                end
-               
+
                local supplied_token = t_concat(stanza);
                local calculated_token = sha1(session.streamid..secret, true);
                if supplied_token:lower() ~= calculated_token:lower() then
@@ -65,22 +71,24 @@ function module.add_host(module)
                        session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
                        return true;
                end
-               
+
                if env.connected then
                        module:log("error", "Second component attempted to connect, denying connection");
                        session:close{ condition = "conflict", text = "Component already connected" };
+                       return true;
                end
-               
+
                env.connected = true;
                send = session.send;
                session.on_destroy = on_destroy;
                session.component_validate_from = module:get_option_boolean("validate_from_addresses", true);
+               session.type = "component";
                module:log("info", "External component successfully authenticated");
                session.send(st.stanza("handshake"));
-       
+
                return true;
        end
-       module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);
+       module:hook("stanza/jabber:component:accept:handshake", handle_component_auth, -1);
 
        -- Handle stanzas addressed to this component
        local function handle_stanza(event)
@@ -89,6 +97,18 @@ function module.add_host(module)
                        stanza.attr.xmlns = nil;
                        send(stanza);
                else
+                       if stanza.name == "iq" and stanza.attr.type == "get" and stanza.attr.to == module.host then
+                               local query = stanza.tags[1];
+                               local node = query.attr.node;
+                               if query.name == "query" and query.attr.xmlns == "http://jabber.org/protocol/disco#info" and (not node or node == "") then
+                                       local name = module:get_option_string("name");
+                                       if name then
+                                               event.origin.send(st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
+                                                       :tag("identity", { category = "component", type = "generic", name = module:get_option_string("name", "Prosody") }))
+                                               return true;
+                                       end
+                               end
+                       end
                        module:log("warn", "Component not connected, bouncing error for: %s", stanza:top_tag());
                        if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                                event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
@@ -96,7 +116,7 @@ function module.add_host(module)
                end
                return true;
        end
-       
+
        module:hook("iq/bare", handle_stanza, -1);
        module:hook("message/bare", handle_stanza, -1);
        module:hook("presence/bare", handle_stanza, -1);
@@ -122,7 +142,7 @@ local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
 
 function stream_callbacks.error(session, error, data, data2)
        if session.destroyed then return; end
-       module:log("warn", "Error processing component stream: "..tostring(error));
+       module:log("warn", "Error processing component stream: %s", tostring(error));
        if error == "no-stream" then
                session:close("invalid-namespace");
        elseif error == "parse-error" then
@@ -157,8 +177,7 @@ function stream_callbacks.streamopened(session, attr)
        session.streamid = uuid_gen();
        session.notopen = nil;
        -- Return stream header
-       session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
-                       ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
+       session:open_stream();
 end
 
 function stream_callbacks.streamclosed(session)
@@ -166,8 +185,7 @@ function stream_callbacks.streamclosed(session)
        session:close();
 end
 
-local core_process_stanza = core_process_stanza;
-
+local function handleerr(err) log("error", "Traceback[component]: %s", traceback(tostring(err), 2)); end
 function stream_callbacks.handlestanza(session, stanza)
        -- Namespaces are icky.
        if not stanza.attr.xmlns and stanza.name == "handshake" then
@@ -198,7 +216,10 @@ function stream_callbacks.handlestanza(session, stanza)
                        return;
                end
        end
-       return core_process_stanza(session, stanza);
+
+       if stanza then
+               return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
+       end
 end
 
 --- Closing a component connection
@@ -206,7 +227,6 @@ local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
 local function session_close(session, reason)
        if session.destroyed then return; end
-       local log = session.log or log;
        if session.conn then
                if session.notopen then
                        session.send("<?xml version='1.0'?>");
@@ -243,20 +263,24 @@ end
 
 function listener.onconnect(conn)
        local _send = conn.write;
-       local session = { type = "component", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
+       local session = { type = "component_unauthed", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
 
        -- Logging functions --
-       local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
+       local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$");
        session.log = logger.init(conn_name);
        session.close = session_close;
-       
+
+       if opt_keepalives then
+               conn:setoption("keepalive", opt_keepalives);
+       end
+
        session.log("info", "Incoming Jabber component connection");
-       
+
        local stream = new_xmpp_stream(session, stream_callbacks);
        session.stream = stream;
-       
+
        session.notopen = true;
-       
+
        function session.reset_stream()
                session.notopen = true;
                session.stream:reset();
@@ -268,7 +292,7 @@ function listener.onconnect(conn)
                module:log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
                session:close("not-well-formed");
        end
-       
+
        session.dispatch_stanza = stream_callbacks.handlestanza;
 
        sessions[conn] = session;
@@ -293,11 +317,12 @@ function listener.ondisconnect(conn, err)
        end
 end
 
-module:add_item("net-provider", {
+module:provides("net", {
        name = "component";
+       private = true;
        listener = listener;
        default_port = 5347;
        multiplex = {
-               pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:component%1.*>";
+               pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:component:accept%1.*>";
        };
 });