comparison mod_delegation/mod_delegation.lua @ 1720:48b7e8021afa

mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
author Goffi <goffi@goffi.org>
date Sun, 03 May 2015 17:46:49 +0200
parents 3938496cd4f8
children f49359330493
comparison
equal deleted inserted replaced
1719:3938496cd4f8 1720:48b7e8021afa
25 25
26 local _DELEGATION_NS = 'urn:xmpp:delegation:1' 26 local _DELEGATION_NS = 'urn:xmpp:delegation:1'
27 local _FORWARDED_NS = 'urn:xmpp:forward:0' 27 local _FORWARDED_NS = 'urn:xmpp:forward:0'
28 local _DISCO_NS = 'http://jabber.org/protocol/disco#info' 28 local _DISCO_NS = 'http://jabber.org/protocol/disco#info'
29 local _DATA_NS = 'jabber:x:data' 29 local _DATA_NS = 'jabber:x:data'
30 local _ORI_ID_PREFIX = "IQ_RESULT_"
31 30
32 local _MAIN_SEP = '::' 31 local _MAIN_SEP = '::'
33 local _BARE_SEP = ':bare:' 32 local _BARE_SEP = ':bare:'
34 local _MAIN_PREFIX = _DELEGATION_NS.._MAIN_SEP 33 local _MAIN_PREFIX = _DELEGATION_NS.._MAIN_SEP
35 local _BARE_PREFIX = _DELEGATION_NS.._BARE_SEP 34 local _BARE_PREFIX = _DELEGATION_NS.._BARE_SEP
149 module:hook('presence/initial', on_presence) 148 module:hook('presence/initial', on_presence)
150 149
151 150
152 --> delegated namespaces hook <-- 151 --> delegated namespaces hook <--
153 152
153 local stanza_cache = {} -- we cache original stanza to build reply
154 local function managing_ent_result(event) 154 local function managing_ent_result(event)
155 -- this function manage iq results from the managing entity 155 -- this function manage iq results from the managing entity
156 -- it do a couple of security check before sending the 156 -- it do a couple of security check before sending the
157 -- result to the managed entity 157 -- result to the managed entity
158 local stanza = event.stanza 158 local stanza = event.stanza
165 -- lot of checks to do... 165 -- lot of checks to do...
166 local delegation = stanza.tags[1] 166 local delegation = stanza.tags[1]
167 if #stanza ~= 1 or delegation.name ~= "delegation" or 167 if #stanza ~= 1 or delegation.name ~= "delegation" or
168 delegation.attr.xmlns ~= _DELEGATION_NS then 168 delegation.attr.xmlns ~= _DELEGATION_NS then
169 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from) 169 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
170 stanza_cache[stanza.attr.from][stanza.attr.id] = nil
170 return true 171 return true
171 end 172 end
172 173
173 local forwarded = delegation.tags[1] 174 local forwarded = delegation.tags[1]
174 if #delegation ~= 1 or forwarded.name ~= "forwarded" or 175 if #delegation ~= 1 or forwarded.name ~= "forwarded" or
175 forwarded.attr.xmlns ~= _FORWARDED_NS then 176 forwarded.attr.xmlns ~= _FORWARDED_NS then
176 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from) 177 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
178 stanza_cache[stanza.attr.from][stanza.attr.id] = nil
177 return true 179 return true
178 end 180 end
179 181
180 local iq = forwarded.tags[1] 182 local iq = forwarded.tags[1]
181 if #forwarded ~= 1 or iq.name ~= "iq" or #iq ~= 1 then 183 if #forwarded ~= 1 or iq.name ~= "iq" or #iq ~= 1 then
182 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from) 184 module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
185 stanza_cache[stanza.attr.from][stanza.attr.id] = nil
183 return true 186 return true
184 end 187 end
185 188
186 local namespace = iq.tags[1].xmlns 189 local namespace = iq.tags[1].xmlns
187 local ns_data = ns_delegations[namespace] 190 local ns_data = ns_delegations[namespace]
188 local original = ns_data[_ORI_ID_PREFIX..stanza.attr.id] 191 local original = stanza_cache[stanza.attr.from][stanza.attr.id]
192 stanza_cache[stanza.attr.from][stanza.attr.id] = nil
189 193
190 if stanza.attr.from ~= ns_data.connected or iq.attr.type ~= "result" or 194 if stanza.attr.from ~= ns_data.connected or iq.attr.type ~= "result" or
191 iq.attr.id ~= original.attr.id or iq.attr.to ~= original.attr.from then 195 iq.attr.id ~= original.attr.id or iq.attr.to ~= original.attr.from then
192 module:log("warn", "ignoring forbidden iq result from managing entity %s, please check that the component is no trying to do something bad (stanza: %s)", stanza.attr.from, tostring(stanza)) 196 module:log("warn", "ignoring forbidden iq result from managing entity %s, please check that the component is no trying to do something bad (stanza: %s)", stanza.attr.from, tostring(stanza))
193 module:send(st.error_reply(original, 'cancel', 'service-unavailable')) 197 module:send(st.error_reply(original, 'cancel', 'service-unavailable'))
205 :tag("delegation", { xmlns=_DELEGATION_NS }) 209 :tag("delegation", { xmlns=_DELEGATION_NS })
206 :tag("forwarded", { xmlns=_FORWARDED_NS }) 210 :tag("forwarded", { xmlns=_FORWARDED_NS })
207 :add_child(stanza) 211 :add_child(stanza)
208 local iq_id = iq_stanza.attr.id 212 local iq_id = iq_stanza.attr.id
209 -- we save the original stanza to check the managing entity result 213 -- we save the original stanza to check the managing entity result
210 ns_data[_ORI_ID_PREFIX..iq_id] = stanza 214 if not stanza_cache[to_jid] then stanza_cache[to_jid] = {} end
215 stanza_cache[to_jid][iq_id] = stanza
211 module:hook("iq-result/host/"..iq_id, managing_ent_result) 216 module:hook("iq-result/host/"..iq_id, managing_ent_result)
212 module:send(iq_stanza) 217 module:send(iq_stanza)
213 end 218 end
214 219
215 local function iq_hook(event) 220 local function iq_hook(event)