diff options
-rw-r--r-- | core/sessionmanager.lua | 2 | ||||
-rw-r--r-- | core/stanza_router.lua | 79 | ||||
-rw-r--r-- | core/xmlhandlers.lua | 42 | ||||
-rw-r--r-- | main.lua | 1 | ||||
-rw-r--r-- | net/connlisteners.lua | 1 | ||||
-rw-r--r-- | util/logger.lua | 2 | ||||
-rw-r--r-- | util/stanza.lua | 7 | ||||
-rw-r--r-- | util/uuid.lua | 2 |
8 files changed, 113 insertions, 23 deletions
diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua index fd9b1272..2b7659d2 100644 --- a/core/sessionmanager.lua +++ b/core/sessionmanager.lua @@ -11,7 +11,7 @@ local sessions = sessions; local modulemanager = require "core.modulemanager"; local log = require "util.logger".init("sessionmanager"); local error = error; -local uuid_generate = require "util.uuid".uuid_generate; +local uuid_generate = require "util.uuid".generate; local rm_load_roster = require "core.rostermanager".load_roster; local newproxy = newproxy; diff --git a/core/stanza_router.lua b/core/stanza_router.lua index d139bc29..5062bc23 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -9,9 +9,14 @@ local log = require "util.logger".init("stanzarouter") local st = require "util.stanza"; local send = require "core.sessionmanager".send_to_session; --- local send_s2s = require "core.s2smanager".send_to_host; +local send_s2s = require "core.s2smanager".send_to_host; local user_exists = require "core.usermanager".user_exists; +local s2s_verify_dialback = require "core.s2smanager".verify_dialback; +local s2s_make_authenticated = require "core.s2smanager".make_authenticated; +local format = string.format; +local tostring = tostring; + local jid_split = require "util.jid".split; local print = print; @@ -33,17 +38,18 @@ function core_process_stanza(origin, stanza) end local to = stanza.attr.to; - stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) -- 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) + end - -- TODO presence subscriptions if not to then core_handle_stanza(origin, stanza); 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 core_handle_stanza(origin, stanza); - elseif origin.type == "c2s" then + elseif origin.type == "c2s" or origin.type == "s2sin" then core_route_stanza(origin, stanza); end end @@ -90,6 +96,58 @@ function core_handle_stanza(origin, stanza) 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 = "invalid"; + if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then + type = "valid" + end + origin.send(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", 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", attr.from, stanza[1]); + send_s2s(attr.to, attr.from, format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", attr.to, attr.from, origin.streamid, stanza[1])); + hosts[attr.from].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.send(format("<db:result from='%s' to='%s' id='%s' type='%s'>%s</db:result>", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key)); + end + end + else + log("warn", "Unhandled origin: %s", origin.type); end end @@ -184,13 +242,14 @@ function core_route_stanza(origin, stanza) end end end - else + elseif origin.type == "c2s" then -- Remote host - if host_session then - -- Send to session - else - -- Need to establish the connection - end + --stanza.attr.xmlns = "jabber:server"; + stanza.attr.xmlns = nil; + log("debug", "sending s2s stanza: %s", tostring(stanza)); + send_s2s(origin.host, host, stanza); + else + log("warn", "received stanza from unhandled connection type: %s", origin.type); end stanza.attr.to = to; -- reset end diff --git a/core/xmlhandlers.lua b/core/xmlhandlers.lua index a6b1c18a..3037a848 100644 --- a/core/xmlhandlers.lua +++ b/core/xmlhandlers.lua @@ -15,6 +15,8 @@ local t_concat = table.concat; local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end local sm_destroy_session = import("core.sessionmanager", "destroy_session"); +local default_log = require "util.logger".init("xmlhandlers"); + local error = error; module "xmlhandlers" @@ -29,8 +31,8 @@ function init_xmlhandlers(session, streamopened) local curr_tag; local chardata = {}; local xml_handlers = {}; - local log = session.log; - local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end + local log = session.log or default_log; + --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end local send = session.send; @@ -41,8 +43,27 @@ function init_xmlhandlers(session, streamopened) stanza:text(t_concat(chardata)); chardata = {}; end - curr_ns,name = name:match("^(.+):(%w+)$"); - if not stanza then + curr_ns,name = name:match("^(.+)|([%w%-]+)$"); + if curr_ns ~= "jabber:server" then + attr.xmlns = curr_ns; + end + + -- FIXME !!!!! + for i, k in ipairs(attr) do + if type(k) == "string" then + local ns, nm = k:match("^([^|]+)|?([^|]-)$") + if ns and nm then + ns = ns_prefixes[ns]; + if ns then + attr[ns..":"..nm] = attr[k]; + attr[i] = ns..":"..nm; + attr[k] = nil; + end + end + end + end + + if not stanza then --if we are not currently inside a stanza if session.notopen then if name == "stream" then streamopened(session, attr); @@ -53,11 +74,14 @@ function init_xmlhandlers(session, streamopened) if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then error("Client sent invalid top-level stanza"); end - attr.xmlns = curr_ns; + stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns }); curr_tag = stanza; - else - attr.xmlns = curr_ns; + else -- we are inside a stanza, so add a tag + attr.xmlns = nil; + if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then + attr.xmlns = curr_ns; + end stanza:tag(name, attr); end end @@ -67,12 +91,14 @@ function init_xmlhandlers(session, streamopened) end end function xml_handlers:EndElement(name) - curr_ns,name = name:match("^(.+):(%w+)$"); + curr_ns,name = name:match("^(.+)|([%w%-]+)$"); if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then if name == "stream" then log("debug", "Stream closed"); sm_destroy_session(session); return; + elseif name == "error" then + error("Stream error: "..tostring(name)..": "..tostring(stanza)); else error("XML parse error in client stream"); end @@ -54,5 +54,6 @@ local protected_handler = function (conn, data, err) local success, ret = pcall( local protected_disconnect = function (conn, err) local success, ret = pcall(disconnect, conn, err); if not success then print("ERROR on "..tostring(conn).." disconnect: "..ret); conn:close(); end end; start("xmppclient", { ssl = ssl_ctx }) +start("xmppserver", { ssl = ssl_ctx }) server.loop(); diff --git a/net/connlisteners.lua b/net/connlisteners.lua index f7321cdc..431d8717 100644 --- a/net/connlisteners.lua +++ b/net/connlisteners.lua @@ -28,7 +28,6 @@ function get(name) if not h then pcall(dofile, "net/"..name:gsub("[^%w%-]", "_").."_listener.lua"); h = listeners[name]; - end return h; end diff --git a/util/logger.lua b/util/logger.lua index 623ceb67..8d983605 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -6,7 +6,7 @@ local tostring = tostring; module "logger" function init(name) - name = nil; -- While this line is not commented, will automatically fill in file/line number info + --name = nil; -- While this line is not commented, will automatically fill in file/line number info return function (level, message, ...) if not name then local inf = debug.getinfo(3, 'Snl'); diff --git a/util/stanza.lua b/util/stanza.lua index 6bc70ab9..95a19fbd 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -6,8 +6,14 @@ local setmetatable = setmetatable; local pairs = pairs; local ipairs = ipairs; local type = type; +local next = next; +local print = print; local unpack = unpack; local s_gsub = string.gsub; + +local debug = debug; +local log = require "util.logger".init("stanza"); + module "stanza" stanza_mt = {}; @@ -91,7 +97,6 @@ function stanza_mt.__tostring(t) if t.attr then for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, tostring(v)); end end end - return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name); end diff --git a/util/uuid.lua b/util/uuid.lua index 489522aa..e1c02c91 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -2,7 +2,7 @@ local m_random = math.random; module "uuid" -function uuid_generate() +function generate() return m_random(0, 99999999); end |