X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=core%2Fstanza_router.lua;h=a2c7b3963abfba49273e12de367aebb864e28ade;hb=03406f6c1978bd832e7656573b708946264fb32d;hp=4c241c270f667ad1b6f8e00b44b9e227ab727fbd;hpb=7c1b7be64227acae6e2d592dab4821e30b02e456;p=prosody.git diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 4c241c27..a2c7b396 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -17,24 +17,37 @@ local jid_prepped_split = require "util.jid".prepped_split; local full_sessions = _G.prosody.full_sessions; local bare_sessions = _G.prosody.bare_sessions; +local core_post_stanza, core_process_stanza, core_route_stanza; + +function deprecated_warning(f) + _G[f] = function(...) + log("warn", "Using the global %s() is deprecated, use module:send() or prosody.%s(). %s", f, f, debug.traceback()); + return prosody[f](...); + end +end +deprecated_warning"core_post_stanza"; +deprecated_warning"core_process_stanza"; +deprecated_warning"core_route_stanza"; + +local valid_stanzas = { message = true, presence = true, iq = true }; local function handle_unhandled_stanza(host, origin, stanza) local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type; - if name == "iq" and xmlns == "jabber:client" then - if stanza.attr.type == "get" or stanza.attr.type == "set" then + if xmlns == "jabber:client" and valid_stanzas[name] then + -- A normal stanza + local st_type = stanza.attr.type; + if st_type == "error" or (name == "iq" and st_type == "result") then + log("debug", "Discarding %s from %s of type: %s", name, origin_type, st_type or ''); + return; + end + if name == "iq" and (st_type == "get" or st_type == "set") and stanza.tags[1] then xmlns = stanza.tags[1].attr.xmlns or "jabber:client"; - log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns); - else - log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type); - return true; end - end - if stanza.attr.xmlns == nil then - log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it - if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then + log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin_type, name, xmlns); + if origin.send then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); end elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features - log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it + log("warn", "Unhandled %s stream element or stanza: %s; xmlns=%s: %s", origin_type, name, xmlns, tostring(stanza)); -- we didn't handle it origin:close("unsupported-stanza-type"); end end @@ -43,19 +56,21 @@ local iq_types = { set=true, get=true, result=true, error=true }; function core_process_stanza(origin, stanza) (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag()) - -- TODO verify validity of stanza (as well as JID validity) - if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log - if stanza.name == "iq" then - if not stanza.attr.id then stanza.attr.id = ""; end -- COMPAT Jabiru doesn't send the id attribute on roster requests - if not iq_types[stanza.attr.type] or ((stanza.attr.type == "set" or stanza.attr.type == "get") and (#stanza.tags ~= 1)) then - origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid IQ type or incorrect number of children")); - return; + if origin.type == "c2s" and not stanza.attr.xmlns then + local name, st_type = stanza.name, stanza.attr.type; + if st_type == "error" and #stanza.tags == 0 then + return handle_unhandled_stanza(origin.host, origin, stanza); + end + if name == "iq" then + if not stanza.attr.id then stanza.attr.id = ""; end -- COMPAT Jabiru doesn't send the id attribute on roster requests + if not iq_types[st_type] or ((st_type == "set" or st_type == "get") and (#stanza.tags ~= 1)) then + origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid IQ type or incorrect number of children")); + return; + end end - end - if origin.type == "c2s" and not stanza.attr.xmlns then if not origin.full_jid - and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind" + and not(name == "iq" and st_type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind" and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then -- authenticated client isn't bound and current stanza is not a bind request if stanza.attr.type ~= "result" and stanza.attr.type ~= "error" then @@ -111,8 +126,8 @@ function core_process_stanza(origin, stanza) log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host); origin:close("not-authorized"); return; - elseif not hosts[to_host] then - log("warn", "Remote server %s sent us a stanza for %s, closing stream", origin.from_host, to_host); + elseif not hosts[host] then + log("warn", "Remote server %s sent us a stanza for %s, closing stream", origin.from_host, host); origin:close("host-unknown"); return; end @@ -199,8 +214,13 @@ function core_route_stanza(origin, stanza) local routed = host_session.events.fire_event("route/remote", { origin = origin, stanza = stanza, from_host = from_host, to_host = host }); stanza.attr.xmlns = xmlns; -- reset if not routed then + log("debug", "... no, just kidding."); + if stanza.attr.type == "error" or (stanza.name == "iq" and stanza.attr.type == "result") then return; end core_route_stanza(host_session, st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote domains is not enabled")); end end end end +prosody.core_process_stanza = core_process_stanza; +prosody.core_post_stanza = core_post_stanza; +prosody.core_route_stanza = core_route_stanza;