From 0699ab44aaa99853e7efb116ae041e83c00fdaba Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 24 Feb 2012 15:14:07 +0000
Subject: modulemanager: include mod_c2s and mod_s2s into autoloaded modules.

---
 core/modulemanager.lua | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/core/modulemanager.lua b/core/modulemanager.lua
index a192e637..0ca37105 100644
--- a/core/modulemanager.lua
+++ b/core/modulemanager.lua
@@ -34,7 +34,7 @@ end
 
 local array, set = require "util.array", require "util.set";
 
-local autoload_modules = {"presence", "message", "iq", "offline"};
+local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
 local component_inheritable_modules = {"tls", "dialback", "iq"};
 
 -- We need this to let modules access the real global namespace
-- 
cgit v1.2.3


From 0c2a6fec148390fc52308086e099f253b762310f Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 24 Feb 2012 15:15:43 +0000
Subject: s2smanager: remove send_to_host.

---
 core/s2smanager.lua | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/core/s2smanager.lua b/core/s2smanager.lua
index e61aaccb..6877ee18 100644
--- a/core/s2smanager.lua
+++ b/core/s2smanager.lua
@@ -114,10 +114,7 @@ function mark_connected(session)
 	local from, to = session.from_host, session.to_host;
 	
 	session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete");
-	
-	local send_to_host = send_to_host;
-	function session.send(data) return send_to_host(to, from, data); end
-	
+
 	local event_data = { session = session };
 	if session.type == "s2sout" then
 		prosody.events.fire_event("s2sout-established", event_data);
-- 
cgit v1.2.3


From 61564a3ca0e754fc10b343d92555818f57aa8769 Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 24 Feb 2012 15:20:03 +0000
Subject: mod_dialback: import util.hashes and functionality once in
 s2smanager.

---
 plugins/mod_dialback.lua | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/plugins/mod_dialback.lua b/plugins/mod_dialback.lua
index e27f8657..977e58c3 100644
--- a/plugins/mod_dialback.lua
+++ b/plugins/mod_dialback.lua
@@ -6,22 +6,37 @@
 -- COPYING file in the source package for more information.
 --
 
+local format = string.format;
 
 local hosts = _G.hosts;
 local send_s2s = require "core.s2smanager".send_to_host;
 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
-local s2s_initiate_dialback = require "core.s2smanager".initiate_dialback;
-local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
 
 local log = module._log;
 
 local st = require "util.stanza";
+local sha256_hash = require "util.hashes".sha256;
 
 local xmlns_stream = "http://etherx.jabber.org/streams";
 local xmlns_dialback = "jabber:server:dialback";
 
 local dialback_requests = setmetatable({}, { __mode = 'v' });
 
+function generate_dialback(id, to, from)
+	return sha256_hash(id..to..from..hosts[from].dialback_secret, true);
+end
+
+function initiate_dialback(session)
+	-- generate dialback key
+	session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
+	session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key));
+	session.log("info", "sent dialback key on outgoing s2s stream");
+end
+
+function verify_dialback(id, to, from, key)
+	return key == generate_dialback(id, to, from);
+end
+
 module:hook("stanza/jabber:server:dialback:verify", function(event)
 	local origin, stanza = event.origin, event.stanza;
 	
@@ -32,7 +47,7 @@ module:hook("stanza/jabber:server:dialback:verify", function(event)
 		-- COMPAT: 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
+		if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
 			type = "valid"
 		else
 			type = "invalid"
@@ -72,8 +87,8 @@ module:hook("stanza/jabber:server:dialback:result", function(event)
 		end
 		
 		origin.log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
-		send_s2s(attr.to, attr.from,
-			st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
+		--send_s2s(attr.to, attr.from,
+		origin.send(st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
 		return true;
 	end
 end);
@@ -84,6 +99,7 @@ module:hook("stanza/jabber:server:dialback:verify", function(event)
 	if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
 		local attr = stanza.attr;
 		local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
+		module:log("debug", tostring(dialback_verifying).." "..attr.from.." "..origin.to_host);
 		if dialback_verifying and attr.from == origin.to_host then
 			local valid;
 			if attr.type == "valid" then
@@ -134,14 +150,14 @@ end);
 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
 	if origin.external_auth == "failed" then
 		module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
-		s2s_initiate_dialback(origin);
+		initiate_dialback(origin);
 		return true;
 	end
 end, 100);
 
 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
 	if not origin.external_auth or origin.external_auth == "failed" then
-		s2s_initiate_dialback(origin);
+		initiate_dialback(origin);
 		return true;
 	end
 end, 100);
-- 
cgit v1.2.3


From 48f64ab12c78907e4f85bb1c6dc02c297999cc55 Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 24 Feb 2012 15:21:21 +0000
Subject: mod_s2s: port functionality once in s2smanager.

---
 plugins/s2s/mod_s2s.lua | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/plugins/s2s/mod_s2s.lua b/plugins/s2s/mod_s2s.lua
index 5be4d84b..0de821b2 100644
--- a/plugins/s2s/mod_s2s.lua
+++ b/plugins/s2s/mod_s2s.lua
@@ -14,10 +14,12 @@ local xpcall, traceback = xpcall, debug.traceback;
 local add_task = require "util.timer".add_task;
 local st = require "util.stanza";
 local initialize_filters = require "util.filters".initialize;
+local nameprep = require "util.encodings".stringprep.nameprep;
 local new_xmpp_stream = require "util.xmppstream".new;
 local s2s_new_incoming = require "core.s2smanager".new_incoming;
 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
+local uuid_gen = require "util.uuid".generate;
 
 local s2sout = module:require("s2sout");
 
@@ -94,6 +96,7 @@ function send_to_host(from_host, to_host, stanza)
 		host_session.bounce_sendq = bounce_sendq;
 		host_session.sendq = { {tostring(stanza), stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
 		log("debug", "stanza [%s] queued until connection complete", tostring(stanza.name));
+		s2sout.initiate_connection(host_session);
 		if (not host_session.connecting) and (not host_session.conn) then
 			log("warn", "Connection to %s failed already, destroying session...", to_host);
 			if not s2s_destroy_session(host_session, "Connection failed") then
@@ -102,7 +105,6 @@ function send_to_host(from_host, to_host, stanza)
 			end
 			return false;
 		end
-		s2sout.initiate_connection(host_session);
 	end
 	return true;
 end
@@ -189,7 +191,7 @@ function stream_callbacks.streamopened(session, attr)
 		if session.secure and not session.cert_chain_status then check_cert_status(session); end
 
 		send("<?xml version='1.0'?>");
-		send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
+		send(stanza.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, to=session.from_host, version=(session.version > 0 and "1.0" or nil) }):top_tag());
 		if session.version >= 1.0 then
 			local features = st.stanza("stream:features");
@@ -235,6 +237,7 @@ function stream_callbacks.streamopened(session, attr)
 		end
 	end
 	session.notopen = nil;
+	session.send = function(stanza) send_to_host(session.to_host, session.from_host, stanza); end;
 end
 
 function stream_callbacks.streamclosed(session)
-- 
cgit v1.2.3


From 3552b31a4fac765a6b5bca81f869423079c2073f Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 24 Feb 2012 15:24:10 +0000
Subject: s2sout.lib: import utils/functionality once in s2smanager.

---
 plugins/s2s/s2sout.lib.lua | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/plugins/s2s/s2sout.lib.lua b/plugins/s2s/s2sout.lib.lua
index 094126e7..12669d97 100644
--- a/plugins/s2s/s2sout.lib.lua
+++ b/plugins/s2s/s2sout.lib.lua
@@ -8,13 +8,24 @@
 
 --- Module containing all the logic for connecting to a remote server
 
+local t_insert = table.insert;
+local t_sort = table.sort;
+local ipairs = ipairs;
+
+local wrapclient = require "net.server".wrapclient;
 local initialize_filters = require "util.filters".initialize;
 local idna_to_ascii = require "util.encodings".idna.to_ascii;
 local add_task = require "util.timer".add_task;
+local st = require "util.stanza";
+local new_ip = require "util.ip".new_ip;
+local rfc3484_dest = require "util.rfc3484".destination;
 local socket = require "socket";
 
+local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 
+local cfg_sources = config.get("*", "core", "s2s_interfaces") or socket.local_addresses();
+
 local s2sout = {};
 
 local s2s_listener;
@@ -36,7 +47,7 @@ end
 
 function s2sout.initiate_connection(host_session)
 	initialize_filters(host_session);
-	session.open_stream = session_open_stream;
+	host_session.open_stream = session_open_stream;
 	
 	-- Kick the connection attempting machine into life
 	if not s2sout.attempt_connection(host_session) then
@@ -147,6 +158,7 @@ function s2sout.try_next_ip(host_session)
 end
 
 function s2sout.try_connect(host_session, connect_host, connect_port, err)
+	local sources;
 	host_session.connecting = true;
 
 	if not err then
@@ -156,7 +168,7 @@ function s2sout.try_connect(host_session, connect_host, connect_port, err)
 		local has_other = false;
 
 		if not sources then
-			sources =  {};
+			sources = {};
 			for i, source in ipairs(cfg_sources) do
 				if source == "*" then
 					sources[i] = new_ip("0.0.0.0", "IPv4");
-- 
cgit v1.2.3


From e69011927edb0b979b71b6649be70f0191655616 Mon Sep 17 00:00:00 2001
From: Marco Cirillo <maranda@lightwitch.org>
Date: Fri, 24 Feb 2012 15:34:25 +0000
Subject: mod_s2s, s2sout.lib: import cert verify and add another fallback
 method in case socket.local_addresses isn't there.

---
 plugins/s2s/mod_s2s.lua    | 1 +
 plugins/s2s/s2sout.lib.lua | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/plugins/s2s/mod_s2s.lua b/plugins/s2s/mod_s2s.lua
index 0de821b2..88e8cded 100644
--- a/plugins/s2s/mod_s2s.lua
+++ b/plugins/s2s/mod_s2s.lua
@@ -20,6 +20,7 @@ local s2s_new_incoming = require "core.s2smanager".new_incoming;
 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 local uuid_gen = require "util.uuid".generate;
+local cert_verify_identity = require "util.x509".verify_identity;
 
 local s2sout = module:require("s2sout");
 
diff --git a/plugins/s2s/s2sout.lib.lua b/plugins/s2s/s2sout.lib.lua
index 12669d97..2856a15c 100644
--- a/plugins/s2s/s2sout.lib.lua
+++ b/plugins/s2s/s2sout.lib.lua
@@ -24,7 +24,7 @@ local socket = require "socket";
 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 
-local cfg_sources = config.get("*", "core", "s2s_interfaces") or socket.local_addresses();
+local cfg_sources = config.get("*", "core", "s2s_interfaces") or socket.local_addresses and socket.local_addresses() or { "*" };
 
 local s2sout = {};
 
-- 
cgit v1.2.3


From b6a8fd9f6b060324897dc9f4f6a80c706e4ba399 Mon Sep 17 00:00:00 2001
From: Marco Cirillo <maranda@lightwitch.org>
Date: Fri, 24 Feb 2012 15:35:04 +0000
Subject: mod_admin_telnet: make service private.

---
 plugins/mod_admin_telnet.lua | 1 +
 1 file changed, 1 insertion(+)

diff --git a/plugins/mod_admin_telnet.lua b/plugins/mod_admin_telnet.lua
index 9e9065bb..d3f5543e 100644
--- a/plugins/mod_admin_telnet.lua
+++ b/plugins/mod_admin_telnet.lua
@@ -776,4 +776,5 @@ end
 require "core.portmanager".register_service("console", {
 	listener = console_listener;
 	default_port = 5582;
+	private = true;
 });
-- 
cgit v1.2.3


From 39a677bc8af8e6e97e896afb72e986ae7cbb9a7e Mon Sep 17 00:00:00 2001
From: Marco Cirillo <maranda@lightwitch.org>
Date: Fri, 24 Feb 2012 15:36:36 +0000
Subject: mod_s2s: prevent attempting to reconnect when the stream is
 gracefully closed and fix TB by checking session.conn is set (racy racy?)

---
 plugins/s2s/mod_s2s.lua | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/plugins/s2s/mod_s2s.lua b/plugins/s2s/mod_s2s.lua
index 88e8cded..d1fdedb3 100644
--- a/plugins/s2s/mod_s2s.lua
+++ b/plugins/s2s/mod_s2s.lua
@@ -247,7 +247,7 @@ function stream_callbacks.streamclosed(session)
 end
 
 function stream_callbacks.streamdisconnected(session, err)
-	if err and err ~= "closed" then
+	if err and err ~= "stream closed" then
 		(session.log or log)("debug", "s2s connection attempt failed: %s", err);
 		if s2sout.attempt_connection(session, err) then
 			(session.log or log)("debug", "...so we're going to try another target");
@@ -255,7 +255,7 @@ function stream_callbacks.streamdisconnected(session, err)
 		end
 	end
 	(session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err or "closed"));
-	sessions[session.conn]  = nil;
+	if session.con then sessions[session.conn] = nil; else (session.log or log)("debug", "stale session's connection already closed"); end
 	s2s_destroy_session(session, err);
 end
 
-- 
cgit v1.2.3