Mercurial > prosody-hg
comparison core/stanza_router.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 05 Nov 2020 22:31:25 +0100 |
| parents | 067ada779ea5 |
| children | e6e56e2dd996 |
comparison
equal
deleted
inserted
replaced
| 11199:6c7c50a4de32 | 11200:bf8f2da84007 |
|---|---|
| 10 | 10 |
| 11 local hosts = _G.prosody.hosts; | 11 local hosts = _G.prosody.hosts; |
| 12 local tostring = tostring; | 12 local tostring = tostring; |
| 13 local st = require "util.stanza"; | 13 local st = require "util.stanza"; |
| 14 local jid_split = require "util.jid".split; | 14 local jid_split = require "util.jid".split; |
| 15 local jid_host = require "util.jid".host; | |
| 15 local jid_prepped_split = require "util.jid".prepped_split; | 16 local jid_prepped_split = require "util.jid".prepped_split; |
| 16 | 17 |
| 17 local full_sessions = _G.prosody.full_sessions; | 18 local full_sessions = _G.prosody.full_sessions; |
| 18 local bare_sessions = _G.prosody.bare_sessions; | 19 local bare_sessions = _G.prosody.bare_sessions; |
| 19 | 20 |
| 25 if xmlns == "jabber:client" and valid_stanzas[name] then | 26 if xmlns == "jabber:client" and valid_stanzas[name] then |
| 26 -- A normal stanza | 27 -- A normal stanza |
| 27 local st_type = stanza.attr.type; | 28 local st_type = stanza.attr.type; |
| 28 if st_type == "error" or (name == "iq" and st_type == "result") then | 29 if st_type == "error" or (name == "iq" and st_type == "result") then |
| 29 if st_type == "error" then | 30 if st_type == "error" then |
| 30 local err_type, err_condition, err_message = stanza:get_error(); | 31 local err_type, err_condition, err_message = stanza:get_error(); -- luacheck: ignore 211/err_message |
| 31 log("debug", "Discarding unhandled error %s (%s, %s) from %s: %s", | 32 log("debug", "Discarding unhandled error %s (%s, %s) from %s: %s", |
| 32 name, err_type, err_condition or "unknown condition", origin_type, stanza:top_tag()); | 33 name, err_type, err_condition or "unknown condition", origin_type, stanza:top_tag()); |
| 33 else | 34 else |
| 34 log("debug", "Discarding %s from %s of type: %s", name, origin_type, st_type or '<nil>'); | 35 log("debug", "Discarding %s from %s of type: %s", name, origin_type, st_type or '<nil>'); |
| 35 end | 36 end |
| 79 local node, host, resource; | 80 local node, host, resource; |
| 80 local from_node, from_host, from_resource; | 81 local from_node, from_host, from_resource; |
| 81 local to_bare, from_bare; | 82 local to_bare, from_bare; |
| 82 if to then | 83 if to then |
| 83 if full_sessions[to] or bare_sessions[to] or hosts[to] then | 84 if full_sessions[to] or bare_sessions[to] or hosts[to] then |
| 84 node, host = jid_split(to); -- TODO only the host is needed, optimize | 85 host = jid_host(to); |
| 85 else | 86 else |
| 86 node, host, resource = jid_prepped_split(to); | 87 node, host, resource = jid_prepped_split(to); |
| 87 if not host then | 88 if not host then |
| 88 log("warn", "Received stanza with invalid destination JID: %s", to); | 89 log("warn", "Received stanza with invalid destination JID: %s", to); |
| 89 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then | 90 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
| 109 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID | 110 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID |
| 110 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end | 111 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end |
| 111 stanza.attr.from = from; | 112 stanza.attr.from = from; |
| 112 end | 113 end |
| 113 | 114 |
| 114 if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == nil then | 115 if (origin.type == "s2sin" or origin.type == "s2sout" or origin.type == "c2s" or origin.type == "component") and xmlns == nil then |
| 115 if origin.type == "s2sin" and not origin.dummy then | 116 if (origin.type == "s2sin" or origin.type == "s2sout") and not origin.dummy then |
| 116 local host_status = origin.hosts[from_host]; | 117 local host_status = origin.hosts[from_host]; |
| 117 if not host_status or not host_status.authed then -- remote server trying to impersonate some other server? | 118 if not host_status or not host_status.authed then -- remote server trying to impersonate some other server? |
| 118 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host); | 119 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host); |
| 119 origin:close("not-authorized"); | 120 origin:close("not-authorized"); |
| 120 return; | 121 return; |
| 169 to_type = '/bare'; | 170 to_type = '/bare'; |
| 170 to_self = true; | 171 to_self = true; |
| 171 end | 172 end |
| 172 end | 173 end |
| 173 | 174 |
| 174 local event_data = {origin=origin, stanza=stanza}; | 175 local event_data = {origin=origin, stanza=stanza, to_self=to_self}; |
| 176 | |
| 175 if preevents then -- c2s connection | 177 if preevents then -- c2s connection |
| 178 local result = hosts[origin.host].events.fire_event("pre-stanza", event_data); | |
| 179 if result ~= nil then | |
| 180 log("debug", "Stanza rejected by pre-stanza handler: %s", event_data.reason or "unknown reason"); | |
| 181 return; | |
| 182 end | |
| 183 | |
| 176 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing | 184 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing |
| 177 end | 185 end |
| 178 local h = hosts[to_bare] or hosts[host or origin.host]; | 186 local h = hosts[to_bare] or hosts[host or origin.host]; |
| 179 if h then | 187 if h then |
| 180 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing | 188 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing |
| 184 core_route_stanza(origin, stanza); | 192 core_route_stanza(origin, stanza); |
| 185 end | 193 end |
| 186 end | 194 end |
| 187 | 195 |
| 188 function core_route_stanza(origin, stanza) | 196 function core_route_stanza(origin, stanza) |
| 189 local node, host, resource = jid_split(stanza.attr.to); | 197 local host = jid_host(stanza.attr.to); |
| 190 local from_node, from_host, from_resource = jid_split(stanza.attr.from); | 198 local from_host = jid_host(stanza.attr.from); |
| 191 | 199 |
| 192 -- Auto-detect origin if not specified | 200 -- Auto-detect origin if not specified |
| 193 origin = origin or hosts[from_host]; | 201 origin = origin or hosts[from_host]; |
| 194 if not origin then return false; end | 202 if not origin then return false; end |
| 195 | 203 |
| 197 -- old stanza routing code removed | 205 -- old stanza routing code removed |
| 198 core_post_stanza(origin, stanza); | 206 core_post_stanza(origin, stanza); |
| 199 else | 207 else |
| 200 local host_session = hosts[from_host]; | 208 local host_session = hosts[from_host]; |
| 201 if not host_session then | 209 if not host_session then |
| 202 log("error", "No hosts[from_host] (please report): %s", tostring(stanza)); | 210 log("error", "No hosts[from_host] (please report): %s", stanza); |
| 203 else | 211 else |
| 204 local xmlns = stanza.attr.xmlns; | 212 local xmlns = stanza.attr.xmlns; |
| 205 stanza.attr.xmlns = nil; | 213 stanza.attr.xmlns = nil; |
| 206 local routed = host_session.events.fire_event("route/remote", { | 214 local routed = host_session.events.fire_event("route/remote", { |
| 207 origin = origin, stanza = stanza, from_host = from_host, to_host = host }); | 215 origin = origin, stanza = stanza, from_host = from_host, to_host = host }); |
