X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;ds=sidebyside;f=plugins%2Fmod_component.lua;h=11abab799e3b2be35979ad4ba3ccc87e287ca22f;hb=8551bf5a2ae3d1c8bc47f052f531902fe4f0d919;hp=c4237b45efb4999a51268df157a3ab1d3bee4bfb;hpb=6c7bd25678abe8b07be7595630d795603ca81c0d;p=prosody.git diff --git a/plugins/mod_component.lua b/plugins/mod_component.lua index c4237b45..11abab79 100644 --- a/plugins/mod_component.lua +++ b/plugins/mod_component.lua @@ -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,9 +20,13 @@ 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) @@ -43,7 +49,7 @@ function module.add_host(module) 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); @@ -76,12 +82,14 @@ function module.add_host(module) 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")); + module:fire_event("component-authenticated", { session = session }); 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) @@ -90,6 +98,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")); @@ -123,7 +143,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 @@ -168,8 +188,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 @@ -200,7 +219,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 @@ -244,12 +266,16 @@ 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"); @@ -294,11 +320,16 @@ function listener.ondisconnect(conn, err) end end -module:add_item("net-provider", { +function listener.ondetach(conn) + sessions[conn] = nil; +end + +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.*>"; }; });