From 7c92b4e9a1b3b59d3a9e5a96ec7a16968f46a314 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Mon, 17 Nov 2008 18:53:13 +0500 Subject: Fixed s2s manager's send queue to not store recieved stanza objects (as these can chnage after the send call) --- core/s2smanager.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index c3d9bdb4..dbfc2cb3 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -29,6 +29,7 @@ local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "hampton.con module "s2smanager" function send_to_host(from_host, to_host, data) + if data.name then data = tostring(data); end local host = hosts[from_host].s2sout[to_host]; if host then -- We have a connection to this host already -- cgit v1.2.3 From 01c770997f61259d6e5b8ae5018aab1ef6ac0ef8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 18 Nov 2008 17:52:33 +0000 Subject: Quite some changes, to: - Small logging fix for s2smanager - Send a stream error if an incoming s2s connection is to an unrecognised hostname (fixes #11) - init_xmlhandlers now takes a table of callbacks (includes changes to net/xmpp*_listener for this) - Move sending of unavailable presence to where it should be, sessionmanager.destroy_session - Fix sending of stream errors to wrong connection --- core/s2smanager.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index dbfc2cb3..afc4f145 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -52,7 +52,7 @@ function send_to_host(from_host, to_host, data) -- FIXME 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", host.from_host, from_host); + log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host)); end host.sends2s(data); host.log("debug", "stanza sent over "..host.type); @@ -137,6 +137,11 @@ function streamopened(session, attr) 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 }):top_tag()); + if session.to_host and not hosts[session.to_host] then + -- Attempting to connect to a host we don't serve + session:disconnect("host-unknown"); + return; + end 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 @@ -218,9 +223,13 @@ end function destroy_session(session) (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)); + + -- FIXME: Flush sendq here/report errors to originators + if session.direction == "outgoing" then hosts[session.from_host].s2sout[session.to_host] = nil; end + session.conn = nil; session.disconnect = nil; for k in pairs(session) do -- cgit v1.2.3 From 99b617b8b6839a496ee26033b1b613a9f11e8f62 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 18 Nov 2008 19:44:54 +0000 Subject: session:disconnect() -> session:close() for consistency with other Lua APIs --- core/s2smanager.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index afc4f145..1fc2715d 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -139,7 +139,7 @@ function streamopened(session, attr) 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()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve - session:disconnect("host-unknown"); + session:close("host-unknown"); return; end elseif session.direction == "outgoing" then @@ -230,8 +230,6 @@ function destroy_session(session) hosts[session.from_host].s2sout[session.to_host] = nil; end - session.conn = nil; - session.disconnect = nil; for k in pairs(session) do if k ~= "trace" then session[k] = nil; -- cgit v1.2.3 From 801e99fcbbfd667fb3d8779782a6d9fb214d1685 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 18 Nov 2008 22:41:04 +0000 Subject: We have SRV resolving \o/ --- core/s2smanager.lua | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 1fc2715d..d6ad2be1 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -3,7 +3,7 @@ local hosts = hosts; local sessions = sessions; local socket = require "socket"; local format = string.format; -local t_insert = table.insert; +local t_insert, t_sort = table.insert, table.sort; local get_traceback = debug.traceback; local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; @@ -24,17 +24,19 @@ local md5_hash = require "util.hashes".md5; local dialback_secret = "This is very secret!!! Ha!"; -local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "hampton.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" }; +local dns = require "net.dns"; module "s2smanager" +local function compare_srv_priorities(a,b) return a.priority < b.priority or a.weight < b.weight; end + function send_to_host(from_host, to_host, data) if data.name then data = tostring(data); end 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 - host.log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now..."); + (host.log or 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); @@ -87,11 +89,31 @@ function new_outgoing(from_host, to_host) local conn, handler = socket.tcp() --FIXME: Below parameters (ports/ip) are incorrect (use SRV) - to_host = srvmap[to_host] or to_host; + + local connect_host, connect_port = to_host, 5269; + + local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV"); + + if answer then + log("debug", to_host.." has SRV records, handling..."); + local srv_hosts = {}; + host_session.srv_hosts = srv_hosts; + for _, record in ipairs(answer) do + t_insert(srv_hosts, record.srv); + end + t_sort(srv_hosts, compare_srv_priorities); + + local srv_choice = srv_hosts[1]; + if srv_choice then + log("debug", "Best record found"); + connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; + log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); + end + end conn:settimeout(0); - local success, err = conn:connect(to_host, 5269); - if not success then + local success, err = conn:connect(connect_host, connect_port); + if not success and err ~= "timeout" then log("warn", "s2s connect() failed: %s", err); end -- cgit v1.2.3 From f1966ba5b72e6b3d49fee3405958a2b2f86239a4 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 05:11:37 +0000 Subject: Log sent s2s stanzas --- core/s2smanager.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index d6ad2be1..1a600db2 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -76,8 +76,8 @@ function new_incoming(conn) getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end; end open_sessions = open_sessions + 1; - local w = conn.write; - session.sends2s = function (t) w(tostring(t)); end + local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$")); + session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end return session; end @@ -124,13 +124,15 @@ function new_outgoing(from_host, to_host) -- otherwise it will assume it is a new incoming connection cl.register_outgoing(conn, host_session); + local log; do local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); - host_session.log = logger_init(conn_name); + log = logger_init(conn_name); + host_session.log = log; end local w = conn.write; - host_session.sends2s = function (t) w(tostring(t)); end + host_session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end conn.write(format([[]], from_host, to_host)); -- cgit v1.2.3 From 9ab64fc855ca26144480ba5a9d24c3653cd2fc4d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 05:12:21 +0000 Subject: Don't attempt to auth connection unless stanzas are being sent across it --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 1a600db2..7d07ad7a 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -35,7 +35,7 @@ function send_to_host(from_host, to_host, data) 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 + if host.type == "s2sout_unauthed" and ((not data.xmlns) or data.xmlns == "jabber:client" or data.xmlns == "jabber:server") then (host.log or 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"); -- cgit v1.2.3 From b27c56326695c39a06d9d6fb1b8982c4f23b82f7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 05:13:07 +0000 Subject: Send version=1.0 in s2s stream header. Send s2s stream:features when in 1.0 mode. --- core/s2smanager.lua | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 7d07ad7a..a61157c0 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -160,12 +160,28 @@ 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 }):top_tag()); + send(stanza("stream:stream", { version = '1.0', xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve session:close("host-unknown"); return; end + if session.version >= 1.0 then + send(st.stanza("stream:features") + :tag("dialback", { xmlns='urn:xmpp:features:dialback' }):tag("optional"):up():up()); + end + --[[ + local features = {}; + modulemanager.fire_event("stream-features-s2s", session, features); + + send(""); + + for _, feature in ipairs(features) do + send(tostring(feature)); + end + + send(""); + ]] 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 @@ -177,17 +193,6 @@ function streamopened(session, attr) mark_connected(session); end end - --[[ - local features = {}; - modulemanager.fire_event("stream-features-s2s", session, features); - - send(""); - - for _, feature in ipairs(features) do - send(tostring(feature)); - end - - send("");]] session.notopen = nil; end -- cgit v1.2.3 From 43c5342160551d24cb634ae9936398100f1072da Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 05:34:00 +0000 Subject: Don't send version 1.0. Who cares about Gmail? --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index a61157c0..05c64519 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -160,7 +160,7 @@ function streamopened(session, attr) session.streamid = uuid_gen(); print(session, session.from_host, "incoming s2s stream opened"); send(""); - send(stanza("stream:stream", { version = '1.0', xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); + 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()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve session:close("host-unknown"); -- cgit v1.2.3 From dd6a298f93413ec919c8f467438009b63e187b51 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 22:04:39 +0000 Subject: Fix for setting the correct host on the socket, seems to fix s2s with XCP --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 05c64519..2370e437 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -117,7 +117,7 @@ function new_outgoing(from_host, to_host) log("warn", "s2s connect() failed: %s", err); end - conn = wraptlsclient(cl, conn, to_host, 5269, 0, 1, hosts[from_host].ssl_ctx ); + conn = wraptlsclient(cl, conn, connect_host, connect_port, 0, 1, hosts[from_host].ssl_ctx ); host_session.conn = conn; -- Register this outgoing connection so that xmppserver_listener knows about it -- cgit v1.2.3 From 093c9c9fa0e01bee0db0f5866ae19b0278057ea5 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 22:05:44 +0000 Subject: Send version=1.0 again in s2s stream header --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 2370e437..4188e26b 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -160,7 +160,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 }):top_tag()); + send(stanza("stream:stream", { version = '1.0', xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve session:close("host-unknown"); -- cgit v1.2.3 From 5a46bff5b212336561c58ec6b374299abf54a069 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 22:50:37 +0000 Subject: Comments and logging fixes --- core/s2smanager.lua | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index 4188e26b..a8fe4d0f 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -105,7 +105,6 @@ function new_outgoing(from_host, to_host) local srv_choice = srv_hosts[1]; if srv_choice then - log("debug", "Best record found"); connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); end @@ -145,7 +144,6 @@ function streamopened(session, attr) session.version = tonumber(attr.version) or 0; if session.version >= 1.0 and not (attr.to and attr.from) then print("to: "..tostring(attr.to).." from: "..tostring(attr.from)); - --error(session.to_host.." failed to specify 'to' or 'from' hostname as per RFC"); log("warn", (session.to_host or "(unknown)").." failed to specify 'to' or 'from' hostname as per RFC"); end @@ -170,18 +168,6 @@ function streamopened(session, attr) send(st.stanza("stream:features") :tag("dialback", { xmlns='urn:xmpp:features:dialback' }):tag("optional"):up():up()); end - --[[ - local features = {}; - modulemanager.fire_event("stream-features-s2s", session, features); - - send(""); - - for _, feature in ipairs(features) do - send(tostring(feature)); - end - - send(""); - ]] 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 2cad7943e5380e36049f3f822e148b9c4da5d0aa Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Nov 2008 23:25:04 +0000 Subject: Remove an old FIXME comment --- core/s2smanager.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index a8fe4d0f..ec07ef5a 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -87,9 +87,7 @@ function new_outgoing(from_host, to_host) local cl = connlisteners_get("xmppserver"); local conn, handler = socket.tcp() - - --FIXME: Below parameters (ports/ip) are incorrect (use SRV) - + local connect_host, connect_port = to_host, 5269; local answer = dns.lookup("_xmpp-server._tcp."..to_host..".", "SRV"); -- cgit v1.2.3 From 57484427669ad05811f8f4110deb6dabb69af930 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 20 Nov 2008 01:14:31 +0000 Subject: Remove version=1.0 on s2s stream headers, again. --- core/s2smanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index ec07ef5a..cb650cfc 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -156,7 +156,7 @@ function streamopened(session, attr) session.streamid = uuid_gen(); print(session, session.from_host, "incoming s2s stream opened"); send(""); - send(stanza("stream:stream", { version = '1.0', xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host }):top_tag()); + 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()); if session.to_host and not hosts[session.to_host] then -- Attempting to connect to a host we don't serve session:close("host-unknown"); -- cgit v1.2.3 From 5efcee9d4d5fdf372fcdf4b580ce338889ff1339 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 20 Nov 2008 03:00:43 +0000 Subject: Remove or comment useless prints, or change them to log() --- core/s2smanager.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/s2smanager.lua') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index cb650cfc..6d8f3a00 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -141,20 +141,20 @@ function streamopened(session, attr) session.version = tonumber(attr.version) or 0; if session.version >= 1.0 and not (attr.to and attr.from) then - print("to: "..tostring(attr.to).." from: "..tostring(attr.from)); + --print("to: "..tostring(attr.to).." from: "..tostring(attr.from)); log("warn", (session.to_host or "(unknown)").." failed to specify 'to' or 'from' hostname as per RFC"); end if session.direction == "incoming" then -- Send a reply stream header - for k,v in pairs(attr) do print("", tostring(k), ":::", tostring(v)); end + --for k,v in pairs(attr) do print("", tostring(k), ":::", tostring(v)); end session.to_host = attr.to; session.from_host = attr.from; session.streamid = uuid_gen(); - print(session, session.from_host, "incoming s2s stream opened"); + (session.log or log)("debug", "incoming s2s received "); 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 }):top_tag()); if session.to_host and not hosts[session.to_host] then -- cgit v1.2.3