From 97853af1a890067177eea401614b0cc0de1486d7 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 4 Nov 2008 17:21:27 +0500 Subject: Foxed: Copy stanza type in reply for presence and message stanzas --- util/stanza.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/stanza.lua b/util/stanza.lua index 3fa8ba66..52f372cc 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -167,7 +167,7 @@ function iq(attr) end function reply(orig) - return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or nil) }); + return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or orig.attr.type) }); end function error_reply(orig, type, condition, message, clone) -- cgit v1.2.3 From 0ee447e39f7d8f52c3b6bf13f9023819c863c65a Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 4 Nov 2008 17:25:12 +0500 Subject: Internal component support --- core/componentmanager.lua | 35 +++++++++++++++++++++++++++++++++++ core/stanza_router.lua | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 core/componentmanager.lua diff --git a/core/componentmanager.lua b/core/componentmanager.lua new file mode 100644 index 00000000..18bf5c02 --- /dev/null +++ b/core/componentmanager.lua @@ -0,0 +1,35 @@ + + +local log = require "util.logger".init("componentmanager") +local jid_split = require "util.jid".split; +local hosts = hosts; + +local components = {}; + +module "componentmanager" + +function handle_stanza(origin, stanza) + local node, host = jid_split(stanza.attr.to); + local component = components[host]; + if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server + if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource + if component then + log("debug", "stanza being handled by component: "..host); + component(origin, stanza); + else + log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to); + end +end + +function register_component(host, component) + if not hosts[host] then + -- TODO check for host well-formedness + components[host] = component; + hosts[host] = {type = "component", connected = true}; + log("debug", "component added: "..host); + else + log("error", "Attempt to set component for existing host: "..host); + end +end + +return _M; \ No newline at end of file diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 4f6918d2..9cb6ff98 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -18,6 +18,7 @@ local s2s_verify_dialback = require "core.s2smanager".verify_dialback; local s2s_make_authenticated = require "core.s2smanager".make_authenticated; local modules_handle_stanza = require "core.modulemanager".handle_stanza; +local component_handle_stanza = require "core.componentmanager".handle_stanza; local format = string.format; local tostring = tostring; @@ -31,6 +32,7 @@ local print = print; function core_process_stanza(origin, stanza) log("debug", "Received["..origin.type.."]: "..tostring(stanza)) + -- TODO verify validity of stanza (as well as JID validity) if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then if stanza.attr.type == "set" or stanza.attr.type == "get" then @@ -46,28 +48,47 @@ function core_process_stanza(origin, stanza) error("Client MUST bind resource after auth"); end - local to = stanza.attr.to; -- TODO also, stazas should be returned to their original state before the function ends if origin.type == "c2s" then - stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) + stanza.attr.from = origin.full_jid; + end + local to = stanza.attr.to; + local node, host, resource = jid_split(to); + local to_bare = node and (node.."@"..host) or host; -- bare JID + local from = stanza.attr.from; + local from_node, from_host, from_resource = jid_split(from); + local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID + + if origin.type == "s2sin" then + if origin.host ~= from_host then -- remote server trying to impersonate some other server? + return; -- FIXME what should we do here? does this work with subdomains? + end + end + if to and not(hosts[to]) and not(hosts[to_bare]) and (not(hosts[host]) or hosts[host].type ~= "local") then -- not for us? + return; -- FIXME what should we do here? end + -- FIXME do stanzas not of jabber:client get handled by components? if not to then core_handle_stanza(origin, stanza); + elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server + core_handle_stanza(origin, stanza); + elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server + component_handle_stanza(origin, stanza); + elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource + component_handle_stanza(origin, stanza); + elseif hosts[host].type == "component" then -- directed at a component + component_handle_stanza(origin, stanza); elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then - local node, host = jid_split(stanza.attr.to); - local to_bare = node and (node.."@"..host) or host; -- bare JID - local from_node, from_host = jid_split(stanza.attr.from); - local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); - elseif hosts[to] and hosts[to].type == "local" then - core_handle_stanza(origin, stanza); - elseif stanza.name == "iq" and not select(3, jid_split(to)) then + elseif stanza.name == "iq" and not resource then -- directed at bare JID core_handle_stanza(origin, stanza); elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then modules_handle_stanza(origin, stanza); elseif origin.type == "c2s" or origin.type == "s2sin" then core_route_stanza(origin, stanza); + else + log("warn", "stanza not processed"); end end @@ -144,9 +165,9 @@ function core_handle_stanza(origin, stanza) else -- TODO error, bad type end - end + end -- TODO handle other stanzas else - log("warn", "Unhandled origin: %s", origin.type); + log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error end end -- cgit v1.2.3 From 6f9e33247cdaf5bace1a44f895d1e533c3f050f3 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 4 Nov 2008 17:43:48 +0500 Subject: Commented a buggy check --- core/stanza_router.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 9cb6ff98..01b6128a 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -61,12 +61,14 @@ function core_process_stanza(origin, stanza) if origin.type == "s2sin" then if origin.host ~= from_host then -- remote server trying to impersonate some other server? + log("warn", "origin.host ~= from_host"); return; -- FIXME what should we do here? does this work with subdomains? end end - if to and not(hosts[to]) and not(hosts[to_bare]) and (not(hosts[host]) or hosts[host].type ~= "local") then -- not for us? + --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us? + log("warn", "stanza recieved for a non-local server"); return; -- FIXME what should we do here? - end + end]] -- FIXME -- FIXME do stanzas not of jabber:client get handled by components? if not to then -- cgit v1.2.3