Changeset

700:0c130c45b7c1

mod_auth_dovecot: Old forgotten changes. Testing appreciated.
author Kim Alvefur <zash@zash.se>
date Thu, 07 Jun 2012 23:41:25 +0200
parents 699:7c88e09a07e7
children 701:cc5805f83583
files mod_auth_dovecot/auth_dovecot/mod_auth_dovecot.lua mod_auth_dovecot/auth_dovecot/sasl_dovecot.lib.lua
diffstat 2 files changed, 29 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/mod_auth_dovecot/auth_dovecot/mod_auth_dovecot.lua	Tue Jun 05 22:47:12 2012 +0200
+++ b/mod_auth_dovecot/auth_dovecot/mod_auth_dovecot.lua	Thu Jun 07 23:41:25 2012 +0200
@@ -14,6 +14,7 @@
 local service_realm = module:get_option("realm");
 local service_name = module:get_option("service_name");
 local append_host = module:get_option_boolean("auth_append_host");
+--assert(not append_host, "auth_append_host does not work");
 local validate_domain = module:get_option_boolean("validate_append_host");
 local handle_appended = module:get_option_string("handle_appended");
 local util_sasl_new = require "util.sasl".new;
@@ -25,7 +26,7 @@
 		service_realm or realm,
 		service_name or "xmpp",
 
-		socket_port and { socket_path, socket_port } or socket_path,
+		socket_port and { socket_host, socket_port } or socket_path,
 
 		{ --config
 			handle_domain = handle_appended or
@@ -55,22 +56,6 @@
 	return new_sasl(module.host):plain_test(username, password);
 end
 
-if append_host then
-	new_sasl = function(realm)
-		return util_sasl_new(realm, {
-			plain_test = function(sasl, username, password, realm)
-				local prepped_username = nodeprep(username);
-				if not prepped_username then
-					log("debug", "NODEprep failed on username: %s", username);
-					return "", nil;
-				end
-				prepped_username = prepped_username .. "@" .. module.host;
-				return provider.test_password(prepped_username, password), true;
-			end,
-		});
-	end
-end
-
 function provider.get_password(username)
 	return nil, "Passwords unavailable for "..name;
 end
@@ -80,10 +65,13 @@
 end
 
 function provider.user_exists(username)
+	return true -- FIXME
+--[[ This, sadly, doesn't work.
 	local user_test = new_sasl(module.host);
 	user_test:select("PLAIN");
 	user_test:process(("\0%s\0"):format(username));
 	return user_test.username == username;
+--]]
 end
 
 function provider.create_user(username, password)
@@ -94,5 +82,13 @@
 	return new_sasl(module.host);
 end
 
+if append_host then
+	function provider.test_password(username, password)
+		return new_sasl(module.host):plain_test(username .. "@".. (service_realm or module.host), password);
+	end
+
+	provider.get_sasl_handler = nil
+end
+
 module:add_item("auth-provider", provider);
 
--- a/mod_auth_dovecot/auth_dovecot/sasl_dovecot.lib.lua	Tue Jun 05 22:47:12 2012 +0200
+++ b/mod_auth_dovecot/auth_dovecot/sasl_dovecot.lib.lua	Thu Jun 07 23:41:25 2012 +0200
@@ -44,17 +44,17 @@
 local function connect(socket_info)
 	--log("debug", "connect(%q)", socket_path);
 	if conn then conn:close(); pid = nil; end
-	if not pid then pid = tonumber(tostring(conn):match("0x%x*$")) end
 
 	local socket_type = (type(socket_info) == "string") and "UNIX" or "TCP";
 
-	local ok, err;
+	local ok, err, socket_path;
 	if socket_type == "TCP" then
 		local socket_host, socket_port = unpack(socket_info);
 		conn = socket.tcp();
 		ok, err = conn:connect(socket_host, socket_port);
 		socket_path = ("%s:%d"):format(socket_host, socket_port);
 	elseif socket.unix then
+		socket_path = socket_info;
 		conn = socket.unix();
 		ok, err = conn:connect(socket_path);
 	else
@@ -62,11 +62,12 @@
 	end
 
 	if not ok then
-		log("error", "error connecting to dovecot %s socket at '%s'. error was '%s'", socket_type, socket_path, err);
+		log("error", "error connecting to dovecot %s socket at '%s'. error was '%s'", socket_type, socket_path or socket_info, err);
 		return false;
 	end
 
 	-- Send our handshake
+	pid = tonumber(tostring(conn):match("0x%x*$"));
 	log("debug", "sending handshake to dovecot. version 1.1, cpid '%d'", pid);
 	if not conn:send("VERSION\t1\t1\n") then
 		return false
@@ -127,7 +128,11 @@
 -- [[
 function method:send(...)
 	local msg = t_concat({...}, "\t");
-	local ok, err = self.conn:send(authmsg.."\n");
+	if msg:sub(-1) ~= "\n" then
+		msg = msg .. "\n"
+	end
+	module:log("debug", "sending %q", msg:sub(1,-2));
+	local ok, err = self.conn:send(msg);
 	if not ok then
 		log("error", "Could not write to socket: %s", err);
 		return nil, err;
@@ -136,13 +141,13 @@
 end
 
 function method:recv()
-	local line, err = self.conn:receive();
 	--log("debug", "Sent %d bytes to socket", ok);
 	local line, err = self.conn:receive();
 	if not line then
 		log("error", "Could not read from socket: %s", err);
 		return nil, err;
 	end
+	module:log("debug", "received %q", line);
 	return line;
 end
 -- ]]
@@ -183,30 +188,30 @@
 	--end
 	local request_id = self.request_id;
 	local authmsg;
+	local ok, err;
 	if not self.started then
 		self.started = true;
-		authmsg = t_concat({
+		ok, err = self:send(
 			"AUTH",
 			request_id,
 			self.selected,
 			"service="..self.service_name,
 			"resp="..(message and b64(message) or "=")
-		}, "\t");
+		);
 	else
-		authmsg = t_concat({
+		ok, err = self:send(
 			"CONT",
 			request_id,
 			(message and b64(message) or "=")
-		}, "\t");
+		);
 	end
 	--log("debug", "Sending %d bytes: %q", #authmsg, authmsg);
-	local ok, err = self.conn:send(authmsg.."\n");
 	if not ok then
 		log("error", "Could not write to socket: %s", err);
 		return "failure", "internal-server-error", err
 	end
 	--log("debug", "Sent %d bytes to socket", ok);
-	local line, err = self.conn:receive();
+	local line, err = self:recv();
 	if not line then
 		log("error", "Could not read from socket: %s", err);
 		return "failure", "internal-server-error", err