From 4851756ca45d3e9dc2d8add552528db9423332da Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 1 Nov 2008 21:07:14 +0000 Subject: Fix s2s once and for all - Moved dialback to the new mod_dialback (mostly). - Modules can now supply a list of origins to handle to add_handler - Modules can now handle and process any stanza, overriding the core - Modules handle non-jabber:client/jabber:server xmlns'd stanzas --- core/modulemanager.lua | 21 +++++++++++---- core/s2smanager.lua | 1 - core/stanza_router.lua | 62 +++++--------------------------------------- plugins/mod_dialback.lua | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 plugins/mod_dialback.lua diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 29139ad7..b65494e7 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -31,17 +31,27 @@ function modulehelpers.add_iq_handler(origin_type, xmlns, handler) end end -function modulehelpers.add_handler(origin_type, tag, xmlns, handler) - if not (origin_type and tag and xmlns and handler) then return false; end +local function _add_handler(module, origin_type, tag, xmlns, handler) handlers[origin_type] = handlers[origin_type] or {}; if not handlers[origin_type][tag] then handlers[origin_type][tag] = handlers[origin_type][tag] or {}; handlers[origin_type][tag][xmlns]= handler; - handler_info[handler] = getfenv(2).module; - log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag); + handler_info[handler] = module; + log("debug", "mod_%s now handles tag '%s'", module.name, tag); elseif handler_info[handlers[origin_type][tag]] then - log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name); + log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name); + end +end + +function modulehelpers.add_handler(origin_type, tag, xmlns, handler) + if not (origin_type and tag and xmlns and handler) then return false; end + if type(origin_type) == "table" then + for _, origin_type in ipairs(origin_type) do + _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); + end + return; end + _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler); end function loadall() @@ -53,6 +63,7 @@ function loadall() load("vcard"); load("private"); load("version"); + load("dialback"); end function load(name) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index e7436a72..b2e498bd 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -188,7 +188,6 @@ function mark_connected(session) if session.direction == "outgoing" then - hosts[to] = session; if sendq then session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host); for i, data in ipairs(sendq) do diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 3333354e..fc8cfa92 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -16,6 +16,9 @@ local sessionmanager = require "core.sessionmanager"; 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 format = string.format; local tostring = tostring; @@ -57,6 +60,8 @@ function core_process_stanza(origin, stanza) core_handle_stanza(origin, stanza); elseif stanza.name == "iq" and not select(3, jid_split(to)) then core_handle_stanza(origin, stanza); + elseif 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); end @@ -66,6 +71,7 @@ end -- that is, they are handled by this server function core_handle_stanza(origin, stanza) -- Handlers + if modules_handle_stanza(origin, stanza) then return; end if origin.type == "c2s" or origin.type == "c2s_unauthed" then local session = origin; @@ -108,62 +114,6 @@ function core_handle_stanza(origin, stanza) else -- TODO error, bad type end - else - log("debug", "Routing stanza to local"); - handle_stanza(session, stanza); - end - elseif origin.type == "s2sin_unauthed" or origin.type == "s2sin" then - if stanza.attr.xmlns == "jabber:server:dialback" then - if stanza.name == "verify" then - -- We are being asked to verify the key, to ensure it was generated by us - log("debug", "verifying dialback key..."); - local attr = stanza.attr; - print(tostring(attr.to), tostring(attr.from)) - print(tostring(origin.to_host), tostring(origin.from_host)) - -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34 - --if attr.from ~= origin.to_host then error("invalid-from"); end - local type; - if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then - type = "valid" - else - type = "invalid" - log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to); - end - origin.sends2s(format("%s", attr.to, attr.from, attr.id, type, stanza[1])); - elseif stanza.name == "result" and origin.type == "s2sin_unauthed" then - -- he wants to be identified through dialback - -- We need to check the key with the Authoritative server - local attr = stanza.attr; - origin.from_host = attr.from; - origin.to_host = attr.to; - origin.dialback_key = stanza[1]; - log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key); - send_s2s(origin.to_host, origin.from_host, format("%s", origin.to_host, origin.from_host, origin.streamid, origin.dialback_key)); - hosts[origin.from_host].dialback_verifying = origin; - end - end - elseif origin.type == "s2sout_unauthed" or origin.type == "s2sout" then - if stanza.attr.xmlns == "jabber:server:dialback" then - if stanza.name == "result" then - if stanza.attr.type == "valid" then - s2s_make_authenticated(origin); - else - -- FIXME - error("dialback failed!"); - end - elseif stanza.name == "verify" and origin.dialback_verifying then - local valid; - local attr = stanza.attr; - if attr.type == "valid" then - s2s_make_authenticated(origin.dialback_verifying); - valid = "valid"; - else - -- Warn the original connection that is was not verified successfully - log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed"); - valid = "invalid"; - end - origin.dialback_verifying.sends2s(format("%s", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key)); - end end else log("warn", "Unhandled origin: %s", origin.type); diff --git a/plugins/mod_dialback.lua b/plugins/mod_dialback.lua new file mode 100644 index 00000000..9ff181b7 --- /dev/null +++ b/plugins/mod_dialback.lua @@ -0,0 +1,67 @@ + +local format = string.format; +local send_s2s = require "core.s2smanager".send_to_host; +local s2s_make_authenticated = require "core.s2smanager".make_authenticated; +local s2s_verify_dialback = require "core.s2smanager".verify_dialback; + +local log = require "util.logger".init("mod_dialback"); + +local xmlns_dialback = "jabber:server:dialback"; + +add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback, + function (origin, stanza) + -- We are being asked to verify the key, to ensure it was generated by us + log("debug", "verifying dialback key..."); + local attr = stanza.attr; + -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34 + --if attr.from ~= origin.to_host then error("invalid-from"); end + local type; + if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then + type = "valid" + else + type = "invalid" + log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to); + end + origin.sends2s(format("%s", attr.to, attr.from, attr.id, type, stanza[1])); + end); + +add_handler("s2sin_unauthed", "result", xmlns_dialback, + function (origin, stanza) + -- he wants to be identified through dialback + -- We need to check the key with the Authoritative server + local attr = stanza.attr; + local attr = stanza.attr; + origin.from_host = attr.from; + origin.to_host = attr.to; + origin.dialback_key = stanza[1]; + log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key); + send_s2s(origin.to_host, origin.from_host, format("%s", origin.to_host, origin.from_host, origin.streamid, origin.dialback_key)); + hosts[origin.from_host].dialback_verifying = origin; + end); + +add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback, + function (origin, stanza) + if origin.dialback_verifying then + local valid; + local attr = stanza.attr; + if attr.type == "valid" then + s2s_make_authenticated(origin.dialback_verifying); + valid = "valid"; + else + -- Warn the original connection that is was not verified successfully + log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed"); + valid = "invalid"; + end + origin.dialback_verifying.sends2s(format("%s", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key)); + end + end); + +add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback, + function (origin, stanza) + if stanza.attr.type == "valid" then + s2s_make_authenticated(origin); + else + -- FIXME + error("dialback failed!"); + end + end); \ No newline at end of file -- cgit v1.2.3