From 564fb0420a1e83294462f83cfe735fac3aae7875 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 12 Nov 2008 19:26:08 +0000 Subject: Some s2s fixes. Now connect() does not block, and stanzas are not lost when connection is slow --- core/s2smanager.lua | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index aed10753..1cfba592 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -31,18 +31,23 @@ end function send_to_host(from_host, to_host, data) local host = hosts[to_host]; if host then - -- Write to connection - if host.type == "s2sout_unauthed" and not host.notopen and not host.dialback_key then - log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now..."); - initiate_dialback(host); - if not host.sendq then host.sendq = { data }; - else t_insert(host.sendq, data); end + -- We have a connection to this host already + if host.type == "s2sout_unauthed" then + host.log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now..."); + if not host.notopen and not host.dialback_key then + host.log("debug", "dialback had not been initiated"); + initiate_dialback(host); + end + + -- Queue stanza until we are able to send it + if host.sendq then t_insert(host.sendq, data); + else host.sendq = { data }; end else - log("debug", "going to send stanza to "..to_host.." from "..from_host); + host.log("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME if hosts[to_host].from_host ~= from_host then log("error", "WARNING! This might, possibly, be a bug, but it might not..."); end hosts[to_host].sends2s(data); - log("debug", "stanza sent over "..hosts[to_host].type); + host.log("debug", "stanza sent over "..hosts[to_host].type); end else log("debug", "opening a new outgoing connection for this stanza"); @@ -77,18 +82,22 @@ function new_outgoing(from_host, to_host) local conn, handler = socket.tcp() - - -- Register this outgoing connection so that xmppserver_listener knows about it - -- otherwise it will assume it is a new incoming connection - cl.register_outgoing(conn, host_session); - --FIXME: Below parameters (ports/ip) are incorrect (use SRV) to_host = srvmap[to_host] or to_host; - conn:settimeout(0.1); - conn:connect(to_host, 5269); + + conn:settimeout(0); + local success, err = conn:connect(to_host, 5269); + if not success then + log("warn", "s2s connect() failed: %s", err); + end + conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx ); host_session.conn = conn; - + + -- Register this outgoing connection so that xmppserver_listener knows about it + -- otherwise it will assume it is a new incoming connection + cl.register_outgoing(conn, host_session); + do local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); host_session.log = logger_init(conn_name); -- cgit v1.2.3 From 6bb0a34965cfb0f57f3e2a19ba8f4584f1e799ed Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 03:47:44 +0000 Subject: Add new top_tag() method to stanzas --- util/stanza.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..5339b91e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -105,6 +105,14 @@ function stanza_mt.__tostring(t) return s_format("<%s%s>%s", t.name, attr_string, children_text, t.name); end +function stanza_mt.top_tag(t) + local attr_string = ""; + 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>", t.name, attr_string); +end + function stanza_mt.__add(s1, s2) return s1:add_direct_child(s2); end -- cgit v1.2.3 From a37d7db54f875e07c1bcc813265aaf112130b313 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 03:48:20 +0000 Subject: Change sending reply stream header to use top_tag() --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 1cfba592..8a6d5b45 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -133,7 +133,7 @@ function streamopened(session, attr) session.streamid = uuid_gen(); print(session, session.from_host, "incoming s2s stream opened"); send(""); - send(format("", session.streamid, session.to_host)); + send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host })); elseif session.direction == "outgoing" then -- If we are just using the connection for verifying dialback keys, we won't try and auth it if not attr.id then error("stream response did not give us a streamid!!!"); end -- cgit v1.2.3 From 2a2cad9354e899fa367d8e5b8637e83400a3877c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 03:56:22 +0000 Subject: Missed importing a function in last commit --- core/s2smanager.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 8a6d5b45..276873cd 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -10,6 +10,8 @@ local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber local connlisteners_get = require "net.connlisteners".get; local wraptlsclient = require "net.server".wraptlsclient; local modulemanager = require "core.modulemanager"; +local st = require "stanza"; +local stanza = st.stanza; local uuid_gen = require "util.uuid".generate; -- cgit v1.2.3 From a3018d64216b7df0b18deaa5bb0bc1625c1ed670 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:07:53 +0500 Subject: Fixed stanza deserialization --- util/stanza.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..79d3167e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -149,6 +149,9 @@ function deserialize(stanza) end stanza.tags = tags; end + if not stanza.last_add then + stanza.last_add = {}; + end end return stanza; -- cgit v1.2.3 From 6c0003661af9cee25d27dcee5b7cd4520c105cdd Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:10:42 +0500 Subject: Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages) --- util/datamanager.lua | 69 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/util/datamanager.lua b/util/datamanager.lua index aad370d1..0f00da1b 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -1,6 +1,6 @@ local format = string.format; local setmetatable, type = setmetatable, type; -local pairs = pairs; +local pairs, ipairs = pairs, ipairs; local char = string.char; local loadfile, setfenv, pcall = loadfile, setfenv, pcall; local log = log; @@ -9,6 +9,7 @@ local os_remove = os.remove; local tostring = tostring; local error = error; local next = next; +local t_insert = table.insert; local indent = function(f, i) for n = 1, i do @@ -69,13 +70,14 @@ end ------- API ------------- -function getpath(username, host, datastore) +function getpath(username, host, datastore, ext) + ext = ext or "dat"; if username then - return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username)); + return format("data/%s/%s/%s.%s", encode(host), datastore, encode(username), ext); elseif host then - return format("data/%s/%s.dat", encode(host), datastore); + return format("data/%s/%s.%s", encode(host), datastore, ext); else - return format("data/%s.dat", datastore); + return format("data/%s.%s", datastore, ext); end end @@ -115,4 +117,59 @@ function store(username, host, datastore, data) return true; end -return _M; \ No newline at end of file +function list_append(username, host, datastore, data) + if not data then return; end + -- save the datastore + local f, msg = io_open(getpath(username, host, datastore, "list"), "a+"); + if not f then + log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); + return; + end + f:write("item("); + simplesave(f, data, 1); + f:write(");\n"); + f:close(); + return true; +end + +function list_store(username, host, datastore, data) + if not data then + data = {}; + end + -- save the datastore + local f, msg = io_open(getpath(username, host, datastore, "list"), "w+"); + if not f then + log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); + return; + end + for _, d in ipairs(data) do + f:write("item("); + simplesave(f, d, 1); + f:write(");\n"); + end + f:close(); + if not next(data) then -- try to delete empty datastore + os_remove(getpath(username, host, datastore, "list")); + end + -- we write data even when we are deleting because lua doesn't have a + -- platform independent way of checking for non-exisitng files + return true; +end + +function list_load(username, host, datastore) + local data, ret = loadfile(getpath(username, host, datastore, "list")); + if not data then + log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); + return nil; + end + local items = {}; + setfenv(data, {item = function(i) t_insert(items, i); end}); + local success, ret = pcall(data); + if not success then + log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); + return nil; + end + return items; +end + +return _M; -- cgit v1.2.3 From 5358129300fc848b130811999adb945773f171fb Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:12:19 +0500 Subject: Added util.datetime: Utility methods to support XEP-0082: XMPP Date and Time Profiles --- util/datetime.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 util/datetime.lua diff --git a/util/datetime.lua b/util/datetime.lua new file mode 100644 index 00000000..077cbb67 --- /dev/null +++ b/util/datetime.lua @@ -0,0 +1,28 @@ +-- XEP-0082: XMPP Date and Time Profiles + +local os_date = os.date; +local error = error; + +module "datetime" + +function date() + return os_date("!%Y-%m-%d"); +end + +function datetime() + return os_date("!%Y-%m-%dT%H:%M:%SZ"); +end + +function time() + return os_date("!%H:%M:%S"); +end + +function legacy() + return os_date("!%Y%m%dT%H:%M:%S"); +end + +function parse(s) + error("datetime.parse: Not implemented"); -- TODO +end + +return _M; -- cgit v1.2.3 From 093cf3e0172ee565202910a324261883d6ff5a5a Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:13:13 +0500 Subject: Added basic offline message support --- core/offlinemanager.lua | 32 ++++++++++++++++++++++++++++++++ core/stanza_router.lua | 18 +++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 core/offlinemanager.lua diff --git a/core/offlinemanager.lua b/core/offlinemanager.lua new file mode 100644 index 00000000..283de5e3 --- /dev/null +++ b/core/offlinemanager.lua @@ -0,0 +1,32 @@ + +local datamanager = require "util.datamanager"; +local st = require "util.stanza"; +local datetime = require "util.datetime"; +local ipairs = ipairs; + +module "offlinemanager" + +function store(node, host, stanza) + stanza.attr.stamp = datetime.datetime(); + stanza.attr.stamp_legacy = datetime.legacy(); + return datamanager.list_append(node, host, "offline", st.preserialize(stanza)); +end + +function load(node, host) + local data = datamanager.list_load(node, host, "offline"); + if not data then return; end + for k, v in ipairs(data) do + stanza = st.deserialize(v); + stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = stanza.attr.stamp}):up(); -- XEP-0203 + stanza:tag("x", {xmlns = "jabber:x:delay", from = host, stamp = stanza.attr.stamp_legacy}):up(); -- XEP-0091 (deprecated) + stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil; + data[k] = stanza; + end + return data; +end + +function deleteAll(node, host) + return datamanager.list_store(node, host, "offline", nil); +end + +return _M; diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 9ae98f1c..5d6358ec 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -13,6 +13,7 @@ local user_exists = require "core.usermanager".user_exists; local rostermanager = require "core.rostermanager"; local sessionmanager = require "core.sessionmanager"; +local offlinemanager = require "core.offlinemanager"; local s2s_verify_dialback = require "core.s2smanager".verify_dialback; local s2s_make_authenticated = require "core.s2smanager".make_authenticated; @@ -149,6 +150,10 @@ function core_handle_stanza(origin, stanza) core_route_stanza(origin, request); end end + for _, msg in ipairs(offlinemanager.load(node, host) or {}) do + origin.send(msg); -- FIXME do we need to modify to/from in any way? + end + offlinemanager.deleteAll(node, host); end origin.priority = 0; if stanza.attr.type == "unavailable" then @@ -328,8 +333,14 @@ function core_route_stanza(origin, stanza) t_insert(recipients, session); end end + local count = 0; for _, session in pairs(recipients) do session.send(stanza); + count = count + 1; + end + if count == 0 then + offlinemanager.store(node, host, stanza); + -- TODO deal with storage errors end else -- TODO send IQ error @@ -349,7 +360,12 @@ function core_route_stanza(origin, stanza) -- TODO send unavailable presence or unsubscribed end elseif stanza.name == "message" then - -- TODO send message error, or store offline messages + if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then + offlinemanager.store(node, host, stanza); + -- FIXME don't store messages with only chat state notifications + end + -- TODO allow configuration of offline storage + -- TODO send error if not storing offline elseif stanza.name == "iq" then -- TODO send IQ error end -- cgit v1.2.3 From a1740642f2a9af6bf42747fe0b4bf2148fc38449 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 19:14:31 +0500 Subject: Added error replies for unhandled stanzas --- core/stanza_router.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 5d6358ec..1793f547 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -173,11 +173,16 @@ function core_handle_stanza(origin, stanza) end stanza.attr.to = nil; -- reset it else - -- TODO error, bad type + log("warn", "Unhandled c2s presence: %s", tostring(stanza)); + origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? end + else + log("warn", "Unhandled c2s stanza: %s", tostring(stanza)); + origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? end -- TODO handle other stanzas else - log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error + log("warn", "Unhandled origin: %s", origin.type); + origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? end end -- cgit v1.2.3 From 22926813345e5f0046b846cd7817e9483ad4c8e6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 16:47:16 +0000 Subject: Add support for remote debugger --- main.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.lua b/main.lua index eac26fe5..36afa58e 100644 --- a/main.lua +++ b/main.lua @@ -32,6 +32,9 @@ require "core.usermanager" require "core.sessionmanager" require "core.stanza_router" +pcall(require, "remdebug.engine"); +if remdebug then remdebug.engine.start() end + local start = require "net.connlisteners".start; require "util.stanza" require "util.jid" -- cgit v1.2.3 From 3349550650708464ff95669a41003f8086181167 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:06:17 +0000 Subject: Mmm, s2s fixed :) --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 276873cd..fe43b181 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -135,7 +135,7 @@ function streamopened(session, attr) session.streamid = uuid_gen(); print(session, session.from_host, "incoming s2s stream opened"); send(""); - send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host })); + send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); elseif session.direction == "outgoing" then -- If we are just using the connection for verifying dialback keys, we won't try and auth it if not attr.id then error("stream response did not give us a streamid!!!"); end -- cgit v1.2.3 From e110538068747e6595facc504f775c3555d546b8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:09:20 +0000 Subject: Another small fix, for logging in s2smanager --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index fe43b181..f5ad12f9 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -45,7 +45,7 @@ function send_to_host(from_host, to_host, data) if host.sendq then t_insert(host.sendq, data); else host.sendq = { data }; end else - host.log("debug", "going to send stanza to "..to_host.." from "..from_host); + (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME if hosts[to_host].from_host ~= from_host then log("error", "WARNING! This might, possibly, be a bug, but it might not..."); end hosts[to_host].sends2s(data); -- cgit v1.2.3 From 9acd51fbef35196c355bf03a9d7751a064649d29 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:12:08 +0000 Subject: Another small fix, for logging in s2smanager --- core/s2smanager.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index f5ad12f9..e0a6f3ca 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -47,7 +47,10 @@ function send_to_host(from_host, to_host, data) else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME - if hosts[to_host].from_host ~= from_host then log("error", "WARNING! This might, possibly, be a bug, but it might not..."); end + if hosts[to_host].from_host ~= from_host then + log("error", "WARNING! This might, possibly, be a bug, but it might not..."); + log("error", "We are going to send from %s instead of %s", hosts[to_host].from_host, from_host); + end hosts[to_host].sends2s(data); host.log("debug", "stanza sent over "..hosts[to_host].type); end -- cgit v1.2.3 From 1b422e8f1d173e7f70db93cfec3ef499e0f9e37c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:20:46 +0000 Subject: Fix for detecting when we are routing a stanza to ourself (I'm sure this has something to do with you, waqas...) --- core/s2smanager.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index e0a6f3ca..16092ba8 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -4,6 +4,7 @@ local sessions = sessions; local socket = require "socket"; local format = string.format; local t_insert = table.insert; +local get_traceback = debug.traceback; local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; @@ -44,6 +45,9 @@ function send_to_host(from_host, to_host, data) -- Queue stanza until we are able to send it if host.sendq then t_insert(host.sendq, data); else host.sendq = { data }; end + elseif host.type == "local" or host.type == "component" then + log("error", "Trying to send a stanza to ourselves??") + log("error", "Traceback: "..get_traceback()); else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME -- cgit v1.2.3 From f980ba5734e2a81348f14905147519b26ef8d794 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:23:43 +0000 Subject: Print out the stanza also --- core/s2smanager.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 16092ba8..530e3087 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -47,7 +47,8 @@ function send_to_host(from_host, to_host, data) else host.sendq = { data }; end elseif host.type == "local" or host.type == "component" then log("error", "Trying to send a stanza to ourselves??") - log("error", "Traceback: "..get_traceback()); + log("error", "Traceback: %s", get_traceback()); + log("error", "Stanza: %s", stanza); else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME -- cgit v1.2.3 From c374478423b1fa6df31f2e133db77f89ceb91fac Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:33:20 +0000 Subject: I knew it ;) Fix sending error replies over s2s (though we shouldn't be error'ing on stream:features anyway) --- core/s2smanager.lua | 2 +- core/stanza_router.lua | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 530e3087..8b421f13 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -48,7 +48,7 @@ function send_to_host(from_host, to_host, data) elseif host.type == "local" or host.type == "component" then log("error", "Trying to send a stanza to ourselves??") log("error", "Traceback: %s", get_traceback()); - log("error", "Stanza: %s", stanza); + log("error", "Stanza: %s", data); else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 1793f547..6c117c25 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -182,7 +182,8 @@ function core_handle_stanza(origin, stanza) end -- TODO handle other stanzas else log("warn", "Unhandled origin: %s", origin.type); - origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + -- s2s stanzas can get here + (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? end end -- cgit v1.2.3 From 6d9485225a4c8c9b339ca9cea137ba3f2de6fd64 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:38:41 +0000 Subject: Yep, s2s definitely works now. This is just a small fix for logging... --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 8b421f13..4fa22957 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -48,7 +48,7 @@ function send_to_host(from_host, to_host, data) elseif host.type == "local" or host.type == "component" then log("error", "Trying to send a stanza to ourselves??") log("error", "Traceback: %s", get_traceback()); - log("error", "Stanza: %s", data); + log("error", "Stanza: %s", tostring(data)); else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME -- cgit v1.2.3 From 428fc65bbc595d80cb678dadcc9ea7cae055d5b9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:41:37 +0000 Subject: Some more logging fixes --- core/s2smanager.lua | 7 +++---- plugins/mod_dialback.lua | 1 + util/logger.lua | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 4fa22957..6d00ff6e 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -103,11 +103,11 @@ function new_outgoing(from_host, to_host) conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx ); host_session.conn = conn; - + -- Register this outgoing connection so that xmppserver_listener knows about it -- otherwise it will assume it is a new incoming connection cl.register_outgoing(conn, host_session); - + do local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); host_session.log = logger_init(conn_name); @@ -122,7 +122,6 @@ function new_outgoing(from_host, to_host) end function streamopened(session, attr) - session.log("debug", "s2s stream opened"); local send = session.sends2s; session.version = tonumber(attr.version) or 0; @@ -166,7 +165,7 @@ function streamopened(session, attr) end send("");]] - log("info", "s2s stream opened successfully"); + session.notopen = nil; end diff --git a/plugins/mod_dialback.lua b/plugins/mod_dialback.lua index dd9704f2..d0c1dd6f 100644 --- a/plugins/mod_dialback.lua +++ b/plugins/mod_dialback.lua @@ -22,6 +22,7 @@ add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback, type = "invalid" log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to); end + log("debug", "verifyied dialback key... it is %s", type); origin.sends2s(format("%s", attr.to, attr.from, attr.id, type, stanza[1])); end); diff --git a/util/logger.lua b/util/logger.lua index 8d983605..fd5d3d80 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -13,9 +13,9 @@ function init(name) level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; end if ... then - print(level, format(message, ...)); + print(name, level, format(message, ...)); else - print(level, message); + print(name, level, message); end end end -- cgit v1.2.3 From 22df06d27db30cae5c7d3361a4fe51ab9a89dcbc Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:54:56 +0000 Subject: Now outgoing s2s sessions are associated with their from_host, fixes #15 --- core/s2smanager.lua | 14 +++++++------- main.lua | 2 +- plugins/mod_dialback.lua | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 6d00ff6e..3f286d0e 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -32,7 +32,7 @@ function connect_host(from_host, to_host) end function send_to_host(from_host, to_host, data) - local host = hosts[to_host]; + local host = hosts[from_host].s2sout[to_host]; if host then -- We have a connection to this host already if host.type == "s2sout_unauthed" then @@ -52,12 +52,12 @@ function send_to_host(from_host, to_host, data) else (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); -- FIXME - if hosts[to_host].from_host ~= from_host then + if host.from_host ~= from_host then log("error", "WARNING! This might, possibly, be a bug, but it might not..."); - log("error", "We are going to send from %s instead of %s", hosts[to_host].from_host, from_host); + log("error", "We are going to send from %s instead of %s", host.from_host, from_host); end - hosts[to_host].sends2s(data); - host.log("debug", "stanza sent over "..hosts[to_host].type); + host.sends2s(data); + host.log("debug", "stanza sent over "..host.type); end else log("debug", "opening a new outgoing connection for this stanza"); @@ -87,7 +87,7 @@ end function new_outgoing(from_host, to_host) local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; - hosts[to_host] = host_session; + hosts[from_host].s2sout[to_host] = host_session; local cl = connlisteners_get("xmppserver"); local conn, handler = socket.tcp() @@ -225,7 +225,7 @@ end function destroy_session(session) (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)); if session.direction == "outgoing" then - hosts[session.to_host] = nil; + hosts[session.from_host].s2sout[session.to_host] = nil; end session.conn = nil; session.disconnect = nil; diff --git a/main.lua b/main.lua index 36afa58e..a9154d43 100644 --- a/main.lua +++ b/main.lua @@ -17,7 +17,7 @@ hosts = {}; if config.hosts and #config.hosts > 0 then for _, host in pairs(config.hosts) do - hosts[host] = {type = "local", connected = true, sessions = {}, host = host}; + hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} }; end else error("No hosts defined in the configuration file"); end diff --git a/plugins/mod_dialback.lua b/plugins/mod_dialback.lua index d0c1dd6f..c17cbcaf 100644 --- a/plugins/mod_dialback.lua +++ b/plugins/mod_dialback.lua @@ -39,7 +39,7 @@ add_handler("s2sin_unauthed", "result", xmlns_dialback, 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; + hosts[origin.to_host].s2sout[origin.from_host].dialback_verifying = origin; end); add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback, -- cgit v1.2.3 From be854cf8995560ecb4cf61e3d7f9823e970c0521 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 14:26:56 +0000 Subject: Fix outgoing s2s from components. Fixes #16 --- core/componentmanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/componentmanager.lua b/core/componentmanager.lua index 38718882..254abfe4 100644 --- a/core/componentmanager.lua +++ b/core/componentmanager.lua @@ -25,7 +25,7 @@ function register_component(host, component) if not hosts[host] then -- TODO check for host well-formedness components[host] = component; - hosts[host] = {type = "component", host = host, connected = true}; + hosts[host] = {type = "component", host = host, connected = true, s2sout = {} }; log("debug", "component added: "..host); else log("error", "Attempt to set component for existing host: "..host); -- cgit v1.2.3 From d67940a1d7700c0389341c42f845b91e8b32eea6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 16:03:33 +0000 Subject: A treat for Linux users ;) --- core/stanza_router.lua | 2 +- util/logger.lua | 17 +++++++++++++++-- util/stanza.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++---- util/termcolours.lua | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 util/termcolours.lua diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 6c117c25..16e6598f 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -32,7 +32,7 @@ local jid_split = require "util.jid".split; local print = print; function core_process_stanza(origin, stanza) - log("debug", "Received["..origin.type.."]: "..tostring(st.reply(st.reply(stanza)))) + log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag()) if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling. -- TODO verify validity of stanza (as well as JID validity) diff --git a/util/logger.lua b/util/logger.lua index fd5d3d80..f93cafc1 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -3,8 +3,21 @@ local format = string.format; local print = print; local debug = debug; local tostring = tostring; + +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; +local do_pretty_printing = not os.getenv("WINDIR"); + module "logger" +local logstyles = {}; + +--TODO: This should be done in config, but we don't have proper config yet +if do_pretty_printing then + logstyles["info"] = getstyle("bold"); + logstyles["warn"] = getstyle("bold", "yellow"); + logstyles["error"] = getstyle("bold", "red"); +end + function init(name) --name = nil; -- While this line is not commented, will automatically fill in file/line number info return function (level, message, ...) @@ -13,9 +26,9 @@ function init(name) level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; end if ... then - print(name, level, format(message, ...)); + print(name, getstring(logstyles[level], level), format(message, ...)); else - print(name, level, message); + print(name, getstring(logstyles[level], level), message); end end end diff --git a/util/stanza.lua b/util/stanza.lua index cfa33c5b..5a6ba8c5 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -10,8 +10,11 @@ local next = next; local print = print; local unpack = unpack; local s_gsub = string.gsub; +local os = os; + +local do_pretty_printing = not os.getenv("WINDIR"); +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; -local debug = debug; local log = require "util.logger".init("stanza"); module "stanza" @@ -157,9 +160,6 @@ function deserialize(stanza) end stanza.tags = tags; end - if not stanza.last_add then - stanza.last_add = {}; - end end return stanza; @@ -195,4 +195,44 @@ function presence(attr) return stanza("presence", attr); end +if do_pretty_printing then + local style_attrk = getstyle("yellow"); + local style_attrv = getstyle("red"); + local style_tagname = getstyle("red"); + local style_punc = getstyle("magenta"); + + local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); + local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); + --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, ""); + local tag_format = top_tag_format.."%s"..getstring(style_punc, ""); + function stanza_mt.pretty_print(t) + local children_text = ""; + for n, child in ipairs(t) do + if type(child) == "string" then + children_text = children_text .. xml_escape(child); + else + children_text = children_text .. child:pretty_print(); + end + end + + local attr_string = ""; + if t.attr then + for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end + end + return s_format(tag_format, t.name, attr_string, children_text, t.name); + end + + function stanza_mt.pretty_top_tag(t) + local attr_string = ""; + if t.attr then + for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end + end + return s_format(top_tag_format, t.name, attr_string); + end +else + -- Sorry, fresh out of colours for you guys ;) + stanza_mt.pretty_print = stanza_mt.__tostring; + stanza_mt.pretty_top_tag = stanza_mt.top_tag; +end + return _M; diff --git a/util/termcolours.lua b/util/termcolours.lua new file mode 100644 index 00000000..5cf5b555 --- /dev/null +++ b/util/termcolours.lua @@ -0,0 +1,33 @@ +local t_concat, t_insert = table.concat, table.insert; +local char, format = string.char, string.format; +local ipairs = ipairs; +module "termcolours" + +local stylemap = { + reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8; + black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37; + ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47; + bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0; + } + +local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m"; +function getstring(style, text) + if style then + return format(fmt_string, style, text); + else + return text; + end +end + +function getstyle(...) + local styles, result = { ... }, {}; + for i, style in ipairs(styles) do + style = stylemap[style]; + if style then + t_insert(result, style); + end + end + return t_concat(result, ";"); +end + +return _M; -- cgit v1.2.3 From 44701835b153e9497a8a5f634ece17ffda41c5ca Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 17:37:45 +0000 Subject: Update rostermanager to use new logger --- core/rostermanager.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/rostermanager.lua b/core/rostermanager.lua index 9a203337..235f2aee 100644 --- a/core/rostermanager.lua +++ b/core/rostermanager.lua @@ -1,8 +1,6 @@ -local mainlog = log; -local function log(type, message) - mainlog(type, "rostermanager", message); -end + +local log = require "util.logger".init("rostermanager"); local setmetatable = setmetatable; local format = string.format; -- cgit v1.2.3 From 50e328e87fb21ffca2780128ed5da9c0e011eb5f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 18:46:00 +0000 Subject: Only reply with errors if the stanza is not an error or a result (don't know how much bandwidth this just cost me :) ) --- core/stanza_router.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 16e6598f..c1819651 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -174,16 +174,22 @@ function core_handle_stanza(origin, stanza) stanza.attr.to = nil; -- reset it else log("warn", "Unhandled c2s presence: %s", tostring(stanza)); - origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + if stanza.attr.type ~= "error" then + origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + end end else log("warn", "Unhandled c2s stanza: %s", tostring(stanza)); - origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then + origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + end end -- TODO handle other stanzas else log("warn", "Unhandled origin: %s", origin.type); - -- s2s stanzas can get here - (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then + -- s2s stanzas can get here + (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? + end end end -- cgit v1.2.3 From b06c5faec463339f95023abb0b1e2aa09dbefd7c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 18:54:19 +0000 Subject: Remove some old unused (and empty) functions from s2smanager --- core/s2smanager.lua | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 3f286d0e..490dfec4 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -28,9 +28,6 @@ local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.c module "s2smanager" -function connect_host(from_host, to_host) -end - function send_to_host(from_host, to_host, data) local host = hosts[from_host].s2sout[to_host]; if host then @@ -67,10 +64,6 @@ function send_to_host(from_host, to_host, data) end end -function disconnect_host(host) - -end - local open_sessions = 0; function new_incoming(conn) -- cgit v1.2.3 From 1c704dcef79c640ffdbc259af63c29ac47599e89 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 20:30:24 +0000 Subject: Update hostname for identi.ca --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 490dfec4..bec1c29b 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -24,7 +24,7 @@ local md5_hash = require "util.hashes".md5; local dialback_secret = "This is very secret!!! Ha!"; -local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" }; +local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "hampton.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" }; module "s2smanager" -- cgit v1.2.3 From fc57c3a09a9ca234f45bb3a1e9b3d8fd8d48d2e0 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 15 Nov 2008 05:33:14 +0500 Subject: Some bugs fixed --- core/offlinemanager.lua | 2 +- core/rostermanager.lua | 2 +- net/xmppclient_listener.lua | 2 +- util/datamanager.lua | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/offlinemanager.lua b/core/offlinemanager.lua index 283de5e3..e71da446 100644 --- a/core/offlinemanager.lua +++ b/core/offlinemanager.lua @@ -16,7 +16,7 @@ function load(node, host) local data = datamanager.list_load(node, host, "offline"); if not data then return; end for k, v in ipairs(data) do - stanza = st.deserialize(v); + local stanza = st.deserialize(v); stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = stanza.attr.stamp}):up(); -- XEP-0203 stanza:tag("x", {xmlns = "jabber:x:delay", from = host, stamp = stanza.attr.stamp_legacy}):up(); -- XEP-0091 (deprecated) stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil; diff --git a/core/rostermanager.lua b/core/rostermanager.lua index 9a203337..504d9961 100644 --- a/core/rostermanager.lua +++ b/core/rostermanager.lua @@ -234,7 +234,7 @@ function unsubscribed(username, host, jid) if item.subscription == "from" then item.subscription = "none"; changed = true; - elseif item.subscription == both then + elseif item.subscription == "both" then item.subscription = "to"; changed = true; end diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index ca4cbbb0..914dd78e 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -78,7 +78,7 @@ function xmppclient.disconnect(conn) if session then if session.last_presence and session.last_presence.attr.type ~= "unavailable" then local pres = st.presence{ type = "unavailable" }; - if err == "closed" then err = "connection closed"; end + if err == "closed" then err = "connection closed"; end --FIXME where did err come from? pres:tag("status"):text("Disconnected: "..err); session.stanza_dispatch(pres); end diff --git a/util/datamanager.lua b/util/datamanager.lua index 0f00da1b..80b35733 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -6,7 +6,7 @@ local loadfile, setfenv, pcall = loadfile, setfenv, pcall; local log = log; local io_open = io.open; local os_remove = os.remove; -local tostring = tostring; +local tostring, tonumber = tostring, tonumber; local error = error; local next = next; local t_insert = table.insert; -- cgit v1.2.3 From 9318711f5f25a04aea6fe2fc9caa79a322a004b3 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 15 Nov 2008 08:38:25 +0500 Subject: Log how many queued stanzas we send --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/s2smanager.lua b/core/s2smanager.lua index bec1c29b..c3d9bdb4 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -205,7 +205,7 @@ function mark_connected(session) if session.direction == "outgoing" then if sendq then - session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host); + session.log("debug", "sending "..#sendq.." queued stanzas across new outgoing connection to "..session.to_host); for i, data in ipairs(sendq) do send(data); sendq[i] = nil; -- cgit v1.2.3 From 32a4306f7afef43e355d5f1a82f9ae23ecba363b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 15 Nov 2008 04:28:41 +0000 Subject: Return registered host table when registering a component --- core/componentmanager.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/core/componentmanager.lua b/core/componentmanager.lua index 254abfe4..5b655435 100644 --- a/core/componentmanager.lua +++ b/core/componentmanager.lua @@ -27,6 +27,7 @@ function register_component(host, component) components[host] = component; hosts[host] = {type = "component", host = host, connected = true, s2sout = {} }; log("debug", "component added: "..host); + return hosts[host]; else log("error", "Attempt to set component for existing host: "..host); end -- cgit v1.2.3 From aefcb845c34c7bf15a370812b28b5da27fbc983b Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 15 Nov 2008 12:21:04 +0500 Subject: mod_saslauth updated for digest-md5 --- plugins/mod_saslauth.lua | 63 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/plugins/mod_saslauth.lua b/plugins/mod_saslauth.lua index 24c82a1c..d8e27c4f 100644 --- a/plugins/mod_saslauth.lua +++ b/plugins/mod_saslauth.lua @@ -2,6 +2,7 @@ local st = require "util.stanza"; local send = require "core.sessionmanager".send_to_session; local sm_bind_resource = require "core.sessionmanager".bind_resource; +local jid local usermanager_validate_credentials = require "core.usermanager".validate_credentials; local t_concat, t_insert = table.concat, table.insert; @@ -15,10 +16,51 @@ local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas'; local new_sasl = require "util.sasl".new; +local function build_reply(status, ret) + local reply = st.stanza(status, {xmlns = xmlns_sasl}); + if status == "challenge" then + reply:text(ret or ""); + elseif status == "failure" then + reply:tag(ret):up(); + elseif status == "success" then + reply:text(ret or ""); + else + error("Unknown sasl status: "..status); + end + return reply; +end + +local function handle_status(session, status) + if status == "failure" then + session.sasl_handler = nil; + elseif status == "success" then + session.sasl_handler = nil; + session:reset_stream(); + end +end + +local function password_callback(jid, mechanism) + local node, host = jid_split(jid); + local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords + local func = function(x) return x; end; + if password then + if mechanism == "PLAIN" then + return func, password; + elseif mechanism == "DIGEST-MD5" then + return func, require "hashes".md5(node.."::"..password); + end + end + return func, nil; +end + add_handler("c2s_unauthed", "auth", xmlns_sasl, function (session, stanza) if not session.sasl_handler then - session.sasl_handler = new_sasl(stanza.attr.mechanism, + session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback); + local status, ret = session.sasl_handler:feed(stanza[1]); + handle_status(session, status); + session.send(build_reply(status, ret)); + --[[session.sasl_handler = new_sasl(stanza.attr.mechanism, function (username, password) -- onAuth require "core.usermanager" @@ -47,12 +89,27 @@ add_handler("c2s_unauthed", "auth", xmlns_sasl, send(session, stanza); end ); - session.sasl_handler:feed(stanza); + session.sasl_handler:feed(stanza); ]] else error("Client tried to negotiate SASL again", 0); end - end); + +add_handler("c2s_unauthed", "abort", xmlns_sasl, + function(session, stanza) + if not session.sasl_handler then error("Attempt to abort when sasl has not started"); end + local status, ret = session.sasl_handler:feed(stanza[1]); + handle_status(session, status); + session.send(build_reply(status, ret)); + end); + +add_handler("c2s_unauthed", "response", xmlns_sasl, + function(session, stanza) + if not session.sasl_handler then error("Attempt to respond when sasl has not started"); end + local status, ret = session.sasl_handler:feed(stanza[1]); + handle_status(session, status); + session.send(build_reply(status, ret)); + end); add_event_hook("stream-features", function (session, features) -- cgit v1.2.3