changeset 312:31deafcbf623

mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
author Matthew Wild <mwild1@gmail.com>
date Sat, 08 Jan 2011 17:57:03 +0000
parents f663ea45436f
children 524f22ef2c2b
files mod_auth_dovecot/mod_auth_dovecot.lua
diffstat 1 files changed, 41 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/mod_auth_dovecot/mod_auth_dovecot.lua	Sat Jan 08 17:08:20 2011 +0000
+++ b/mod_auth_dovecot/mod_auth_dovecot.lua	Sat Jan 08 17:57:03 2011 +0000
@@ -7,16 +7,17 @@
 
 local socket_unix = require "socket.unix";
 local datamanager = require "util.datamanager";
+local usermanager = require "core.usermanager";
 local log = require "util.logger".init("auth_dovecot");
 local new_sasl = require "util.sasl".new;
 local nodeprep = require "util.encodings".stringprep.nodeprep;
 local base64 = require "util.encodings".base64;
 local pposix = require "util.pposix";
 
-local prosody = _G.prosody;
+local prosody = prosody;
 local socket_path = module:get_option_string("dovecot_auth_socket", "/var/run/dovecot/auth-login");
 
-function new_default_provider(host)
+function new_provider(host)
 	local provider = { name = "dovecot", request_id = 0 };
 	log("debug", "initializing dovecot authentication provider for host '%s'", host);
 	
@@ -39,9 +40,9 @@
 		
 		-- Create a connection to dovecot socket
 		log("debug", "connecting to dovecot socket at '%s'", socket_path);
-		local r, e = conn:connect(socket_path);
-		if (not r) then
-			log("warn", "error connecting to dovecot socket at '%s'. error was '%s'. check permissions", socket_path, e);
+		local ok, err = conn:connect(socket_path);
+		if not ok then
+			log("error", "error connecting to dovecot socket at '%s'. error was '%s'. check permissions", socket_path, err);
 			provider:close();
 			return false;
 		end
@@ -52,22 +53,22 @@
 		if not provider:send("VERSION\t1\t1\n") then
 			return false
 		end
-		if (not provider:send("CPID\t" .. pid .. "\n")) then
+		if not provider:send("CPID\t" .. pid .. "\n") then
 			return false
 		end
 		
 		-- Parse Dovecot's handshake
 		local done = false;
 		while (not done) do
-			local l = provider:receive();
-			if (not l) then
+			local line = provider:receive();
+			if not line then
 				return false;
 			end
 			
-			log("debug", "dovecot handshake: '%s'", l);
-			parts = string.gmatch(l, "[^\t]+");
-			first = parts();
-			if (first == "VERSION") then
+			log("debug", "dovecot handshake: '%s'", line);
+			local parts = line:gmatch("[^\t]+");
+			local first = parts();
+			if first == "VERSION" then
 				-- Version should be 1.1
 				local major_version = parts();
 				
@@ -76,20 +77,20 @@
 					provider:close();
 					return false;
 				end
-			elseif (first == "MECH") then
+			elseif first == "MECH" then
 				-- Mechanisms should include PLAIN
 				local ok = false;
-				for p in parts do
-					if p == "PLAIN" then
+				for part in parts do
+					if part == "PLAIN" then
 						ok = true;
 					end
 				end
-				if (not ok) then
-					log("warn", "server doesn't support PLAIN mechanism. It supports '%s'", l);
+				if not ok then
+					log("warn", "server doesn't support PLAIN mechanism. It supports '%s'", line);
 					provider:close();
 					return false;
 				end
-			elseif (first == "DONE") then
+			elseif first == "DONE" then
 				done = true;
 			end
 		end
@@ -98,9 +99,9 @@
 	
 	-- Wrapper for send(). Handles errors
 	function provider.send(self, data)
-		local r, e = conn:send(data);
-		if (not r) then
-			log("warn", "error sending '%s' to dovecot. error was '%s'", data, e);
+		local ok, err = conn:send(data);
+		if not ok then
+			log("error", "error sending '%s' to dovecot. error was '%s'", data, err);
 			provider:close();
 			return false;
 		end
@@ -109,13 +110,13 @@
 	
 	-- Wrapper for receive(). Handles errors
 	function provider.receive(self)
-		local r, e = conn:receive();
-		if (not r) then
-			log("warn", "error receiving data from dovecot. error was '%s'", socket, e);
+		local line, err = conn:receive();
+		if not line then
+			log("error", "error receiving data from dovecot. error was '%s'", err);
 			provider:close();
 			return false;
 		end
-		return r;
+		return line;
 	end
 	
 	function provider.send_auth_request(self, username, password)
@@ -132,24 +133,24 @@
 		
 		local msg = "AUTH\t" .. provider.request_id .. "\tPLAIN\tservice=XMPP\tresp=" .. b64;
 		log("debug", "sending auth request for '%s' with password '%s': '%s'", username, password, msg);
-		if (not provider:send(msg .. "\n")) then
+		if not provider:send(msg .. "\n") then
 			return nil, "Auth failed. Dovecot communications error";
 		end
 		
 		
 		-- Get response
-		local l = provider:receive();
-		log("debug", "got auth response: '%s'", l);
-		if (not l) then
+		local line = provider:receive();
+		log("debug", "got auth response: '%s'", line);
+		if not line then
 			return nil, "Auth failed. Dovecot communications error";
 		end
-		local parts = string.gmatch(l, "[^\t]+");
+		local parts = line:gmatch("[^\t]+");
 		
 		-- Check response
 		local status = parts();
 		local resp_id = tonumber(parts());
 		
-		if (resp_id  ~= provider.request_id) then
+		if resp_id  ~= provider.request_id then
 			log("warn", "dovecot response_id(%s) doesn't match request_id(%s)", resp_id, provider.request_id);
 			provider:close();
 			return nil, "Auth failed. Dovecot communications error";
@@ -163,7 +164,7 @@
 		
 		local status, extra = provider:send_auth_request(username, password);
 		
-		if (status == "OK") then
+		if status == "OK" then
 			log("info", "login ok for '%s'", username);
 			return true;
 		else
@@ -188,19 +189,19 @@
 		local status, extra = provider:send_auth_request(username, "");
 		
 		local param = extra();
-		while (param) do
-			parts = string.gmatch(param, "[^=]+");
-			name = parts();
-			value = parts();
-			if (name == "user") then
-				log("info", "user '%s' exists", username);
+		while param do
+			local parts = param:gmatch("[^=]+");
+			local name = parts();
+			local value = parts();
+			if name == "user" then
+				log("debug", "user '%s' exists", username);
 				return true;
 			end
 			
 			param = extra();
 		end
 		
-		log("info", "user '%s' does not exists (or dovecot didn't send user=<username> parameter)", username);
+		log("debug", "user '%s' does not exists (or dovecot didn't send user=<username> parameter)", username);
 		return false;
 	end
 
@@ -226,4 +227,4 @@
 	return provider;
 end
 
-module:add_item("auth-provider", new_default_provider(module.host));
+module:add_item("auth-provider", new_provider(module.host));