Merging more s2s
authorMatthew Wild <mwild1@gmail.com>
Fri, 24 Oct 2008 07:36:48 +0100
changeset 150 d09b8a1ab046
parent 149 40e443eacbbd (current diff)
parent 148 4c0dcd245d34 (diff)
child 153 8310bfddaba8
Merging more s2s
core/sessionmanager.lua
core/stanza_router.lua
core/xmlhandlers.lua
main.lua
--- a/core/sessionmanager.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/core/sessionmanager.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -11,7 +11,7 @@
 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;
--- a/core/stanza_router.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/core/stanza_router.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -9,9 +9,14 @@
 
 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 @@
 	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 @@
 			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 @@
 				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
--- a/core/xmlhandlers.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/core/xmlhandlers.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -15,6 +15,8 @@
 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 @@
 		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 @@
 				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 @@
 				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 @@
 			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
--- a/main.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/main.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -54,5 +54,6 @@
 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();
--- a/net/connlisteners.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/net/connlisteners.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -28,7 +28,6 @@
 	if not h then
 		pcall(dofile, "net/"..name:gsub("[^%w%-]", "_").."_listener.lua");
 		h = listeners[name];
-		
 	end
 	return h;
 end
--- a/util/logger.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/util/logger.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -6,7 +6,7 @@
 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');
--- a/util/stanza.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/util/stanza.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -6,8 +6,14 @@
 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 @@
 	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
 
--- a/util/uuid.lua	Fri Oct 24 07:34:13 2008 +0100
+++ b/util/uuid.lua	Fri Oct 24 07:36:48 2008 +0100
@@ -2,7 +2,7 @@
 local m_random = math.random;
 module "uuid"
 
-function uuid_generate()
+function generate()
 	return m_random(0, 99999999);
 end