Changeset

297:15b375870b40

Providing some human readable error messages and some fixes.
author Tobias Markmann <tm@ayena.de>
date Sat, 15 Nov 2008 22:36:22 +0100
parents 294:5d861d6e5bbd (current diff) 296:21835c4fc34f (diff)
children 298:985710ea308b 303:89e8f53b870e
files plugins/mod_saslauth.lua util/sasl.lua
diffstat 2 files changed, 38 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_saslauth.lua	Sat Nov 15 22:30:09 2008 +0100
+++ b/plugins/mod_saslauth.lua	Sat Nov 15 22:36:22 2008 +0100
@@ -51,13 +51,19 @@
 		if mechanism == "PLAIN" then
 			return func, password;
 		elseif mechanism == "DIGEST-MD5" then
-			return func, require "hashes".md5(node..":"..host..":"..password);
+			return func, require "md5".sum(node..":"..host..":"..password);
 		end
 	end
 	return func, nil;
 end
 
-function do_sasl(session, stanza)
+function sasl_handler(session, stanza)
+	if stanza.name == "auth" then
+		-- FIXME ignoring duplicates because ejabberd does
+		session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
+	elseif not session.sasl_handler then
+		return; -- FIXME ignoring out of order stanzas because ejabberd does
+	end
 	local text = stanza[1];
 	if text then
 		text = base64.decode(text);
@@ -74,27 +80,9 @@
 	session.send(s);
 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.host, password_callback);
-				do_sasl(session, 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
-		do_sasl(session, stanza);
-	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
-		do_sasl(session, stanza);
-	end);
+add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
+add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
+add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
 
 add_event_hook("stream-features", 
 					function (session, features)												
--- a/util/sasl.lua	Sat Nov 15 22:30:09 2008 +0100
+++ b/util/sasl.lua	Sat Nov 15 22:36:22 2008 +0100
@@ -16,30 +16,29 @@
 
 local function new_plain(realm, password_handler)
 	local object = { mechanism = "PLAIN", realm = realm, password_handler = password_handler}
-	object.feed = 	function(self, message)
-						--print(message:gsub("%W", function (c) return string.format("\\%d", string.byte(c)) end));
-
-						if message == "" or message == nil then return "failure", "malformed-request" end
-						local response = message
-						local authorization = s_match(response, "([^&%z]+)")
-						local authentication = s_match(response, "%z([^&%z]+)%z")
-						local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
-						
-						local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
-						
-						local claimed_password = ""
-						if password_encoding == nil then claimed_password = password
-						else claimed_password = password_encoding(password) end
-						
-						self.username = authentication
-						if claimed_password == correct_password then
-							log("debug", "success")
-							return "success"
-						else
-							log("debug", "failure")
-							return "failure", "not-authorized"
-						end
-					end
+	function object.feed(self, message)
+        
+		if message == "" or message == nil then return "failure", "malformed-request" end
+		local response = message
+		local authorization = s_match(response, "([^&%z]+)")
+		local authentication = s_match(response, "%z([^&%z]+)%z")
+		local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
+		
+		local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
+		
+		local claimed_password = ""
+		if password_encoding == nil then claimed_password = password
+		else claimed_password = password_encoding(password) end
+		
+		self.username = authentication
+		if claimed_password == correct_password then
+			log("debug", "success")
+			return "success"
+		else
+			log("debug", "failure")
+			return "failure", "not-authorized"
+		end
+	end
 	return object
 end
 
@@ -111,7 +110,7 @@
 				if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
 			end
 			
-			if not response["cnonce"] then return "failure", "malformed-request" end
+			if not response["cnonce"] then return "failure", "malformed-request", "Missing entry for cnonce in SASL message." end
 			if not response["qop"] then response["qop"] = "auth" end
 			
 			if response["realm"] == nil then response["realm"] = "" end
@@ -147,13 +146,14 @@
         
 				KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
 				local rspauth = md5.sumhexa(KD)
-				
+				self.authenticated = true
 				return "challenge", serialize({rspauth = rspauth})
 			else
 				return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
 			end							
 		elseif self.step == 3 then
-			return "success"
+			if self.authenticated ~= nil then return "success"
+			else return "failure", "malformed-request" end
 		end
 	end
 	return object