Changeset

5119:d868ce990838

Merge 0.9->trunk
author Waqas Hussain <waqas20@gmail.com>
date Wed, 12 Sep 2012 22:03:57 +0500
parents 5114:4c2c04a49938 (current diff) 5118:0dc9e6c128c3 (diff)
children 5120:bcabea740c00
files
diffstat 5 files changed, 223 insertions(+), 235 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_auth_anonymous.lua	Mon Sep 10 23:17:06 2012 +0100
+++ b/plugins/mod_auth_anonymous.lua	Wed Sep 12 22:03:57 2012 +0500
@@ -9,41 +9,39 @@
 local new_sasl = require "util.sasl".new;
 local datamanager = require "util.datamanager";
 
-function new_default_provider(host)
-	local provider = { name = "anonymous" };
-
-	function provider.test_password(username, password)
-		return nil, "Password based auth not supported.";
-	end
+-- define auth provider
+local provider = {};
 
-	function provider.get_password(username)
-		return nil, "Password not available.";
-	end
-
-	function provider.set_password(username, password)
-		return nil, "Password based auth not supported.";
-	end
+function provider.test_password(username, password)
+	return nil, "Password based auth not supported.";
+end
 
-	function provider.user_exists(username)
-		return nil, "Only anonymous users are supported."; -- FIXME check if anonymous user is connected?
-	end
-
-	function provider.create_user(username, password)
-		return nil, "Account creation/modification not supported.";
-	end
+function provider.get_password(username)
+	return nil, "Password not available.";
+end
 
-	function provider.get_sasl_handler()
-		local anonymous_authentication_profile = {
-			anonymous = function(sasl, username, realm)
-				return true; -- for normal usage you should always return true here
-			end
-		};
-		return new_sasl(module.host, anonymous_authentication_profile);
-	end
-
-	return provider;
+function provider.set_password(username, password)
+	return nil, "Password based auth not supported.";
 end
 
+function provider.user_exists(username)
+	return nil, "Only anonymous users are supported."; -- FIXME check if anonymous user is connected?
+end
+
+function provider.create_user(username, password)
+	return nil, "Account creation/modification not supported.";
+end
+
+function provider.get_sasl_handler()
+	local anonymous_authentication_profile = {
+		anonymous = function(sasl, username, realm)
+			return true; -- for normal usage you should always return true here
+		end
+	};
+	return new_sasl(module.host, anonymous_authentication_profile);
+end
+
+-- datamanager callback to disable writes
 local function dm_callback(username, host, datastore, data)
 	if host == module.host then
 		return false;
@@ -64,5 +62,5 @@
 	datamanager.remove_callback(dm_callback);
 end
 
-module:add_item("auth-provider", new_default_provider(module.host));
+module:provides("auth", provider);
 
--- a/plugins/mod_auth_cyrus.lua	Mon Sep 10 23:17:06 2012 +0100
+++ b/plugins/mod_auth_cyrus.lua	Wed Sep 12 22:03:57 2012 +0500
@@ -41,45 +41,44 @@
 	end
 end
 
-function new_default_provider(host)
-	local provider = { name = "cyrus" };
-	log("debug", "initializing default authentication provider for host '%s'", host);
-
-	function provider.test_password(username, password)
-		return nil, "Legacy auth not supported with Cyrus SASL.";
-	end
+local host = module.host;
 
-	function provider.get_password(username)
-		return nil, "Passwords unavailable for Cyrus SASL.";
-	end
-	
-	function provider.set_password(username, password)
-		return nil, "Passwords unavailable for Cyrus SASL.";
-	end
+-- define auth provider
+local provider = {};
+log("debug", "initializing default authentication provider for host '%s'", host);
 
-	function provider.user_exists(username)
-		if require_provisioning then
-			return usermanager_user_exists(username, module.host);
-		end
-		return true;
-	end
-
-	function provider.create_user(username, password)
-		return nil, "Account creation/modification not available with Cyrus SASL.";
-	end
+function provider.test_password(username, password)
+	return nil, "Legacy auth not supported with Cyrus SASL.";
+end
 
-	function provider.get_sasl_handler()
-		local handler = new_sasl(module.host);
-		if require_provisioning then
-			function handler.require_provisioning(username)
-				return usermanager_user_exists(username, module.host);
-			end
-		end
-		return handler;
-	end
+function provider.get_password(username)
+	return nil, "Passwords unavailable for Cyrus SASL.";
+end
 
-	return provider;
+function provider.set_password(username, password)
+	return nil, "Passwords unavailable for Cyrus SASL.";
 end
 
-module:add_item("auth-provider", new_default_provider(module.host));
+function provider.user_exists(username)
+	if require_provisioning then
+		return usermanager_user_exists(username, host);
+	end
+	return true;
+end
+
+function provider.create_user(username, password)
+	return nil, "Account creation/modification not available with Cyrus SASL.";
+end
 
+function provider.get_sasl_handler()
+	local handler = new_sasl(host);
+	if require_provisioning then
+		function handler.require_provisioning(username)
+			return usermanager_user_exists(username, host);
+		end
+	end
+	return handler;
+end
+
+module:provides("auth", provider);
+
--- a/plugins/mod_auth_internal_hashed.lua	Mon Sep 10 23:17:06 2012 +0100
+++ b/plugins/mod_auth_internal_hashed.lua	Wed Sep 12 22:03:57 2012 +0500
@@ -39,113 +39,111 @@
 -- Default; can be set per-user
 local iteration_count = 4096;
 
-function new_hashpass_provider(host)
-	local provider = { name = "internal_hashed" };
-	log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
+local host = module.host;
+-- define auth provider
+local provider = {};
+log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
 
-	function provider.test_password(username, password)
-		local credentials = datamanager.load(username, host, "accounts") or {};
-	
-		if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
-			if credentials.password ~= password then
-				return nil, "Auth failed. Provided password is incorrect.";
-			end
+function provider.test_password(username, password)
+	local credentials = datamanager.load(username, host, "accounts") or {};
 
-			if provider.set_password(username, credentials.password) == nil then
-				return nil, "Auth failed. Could not set hashed password from plaintext.";
-			else
-				return true;
-			end
+	if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
+		if credentials.password ~= password then
+			return nil, "Auth failed. Provided password is incorrect.";
 		end
 
-		if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
-			return nil, "Auth failed. Stored salt and iteration count information is not complete.";
-		end
-		
-		local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
-		
-		local stored_key_hex = to_hex(stored_key);
-		local server_key_hex = to_hex(server_key);
-		
-		if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
+		if provider.set_password(username, credentials.password) == nil then
+			return nil, "Auth failed. Could not set hashed password from plaintext.";
+		else
 			return true;
-		else
-			return nil, "Auth failed. Invalid username, password, or password hash information.";
 		end
 	end
 
-	function provider.set_password(username, password)
-		local account = datamanager.load(username, host, "accounts");
-		if account then
-			account.salt = account.salt or generate_uuid();
-			account.iteration_count = account.iteration_count or iteration_count;
-			local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
-			local stored_key_hex = to_hex(stored_key);
-			local server_key_hex = to_hex(server_key);
-			
-			account.stored_key = stored_key_hex
-			account.server_key = server_key_hex
-
-			account.password = nil;
-			return datamanager.store(username, host, "accounts", account);
-		end
-		return nil, "Account not available.";
+	if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
+		return nil, "Auth failed. Stored salt and iteration count information is not complete.";
 	end
-
-	function provider.user_exists(username)
-		local account = datamanager.load(username, host, "accounts");
-		if not account then
-			log("debug", "account not found for username '%s' at host '%s'", username, module.host);
-			return nil, "Auth failed. Invalid username";
-		end
+	
+	local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
+	
+	local stored_key_hex = to_hex(stored_key);
+	local server_key_hex = to_hex(server_key);
+	
+	if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
 		return true;
+	else
+		return nil, "Auth failed. Invalid username, password, or password hash information.";
 	end
+end
 
-	function provider.create_user(username, password)
-		if password == nil then
-			return datamanager.store(username, host, "accounts", {});
-		end
-		local salt = generate_uuid();
-		local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
+function provider.set_password(username, password)
+	local account = datamanager.load(username, host, "accounts");
+	if account then
+		account.salt = account.salt or generate_uuid();
+		account.iteration_count = account.iteration_count or iteration_count;
+		local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
 		local stored_key_hex = to_hex(stored_key);
 		local server_key_hex = to_hex(server_key);
-		return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
-	end
+		
+		account.stored_key = stored_key_hex
+		account.server_key = server_key_hex
 
-	function provider.delete_user(username)
-		return datamanager.store(username, host, "accounts", nil);
+		account.password = nil;
+		return datamanager.store(username, host, "accounts", account);
 	end
+	return nil, "Account not available.";
+end
 
-	function provider.get_sasl_handler()
-		local testpass_authentication_profile = {
-			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
-				return usermanager.test_password(prepped_username, realm, password), true;
-			end,
-			scram_sha_1 = function(sasl, username, realm)
-				local credentials = datamanager.load(username, host, "accounts");
-				if not credentials then return; end
-				if credentials.password then
-					usermanager.set_password(username, credentials.password, host);
-					credentials = datamanager.load(username, host, "accounts");
-					if not credentials then return; end
-				end
-				
-				local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
-				stored_key = stored_key and from_hex(stored_key);
-				server_key = server_key and from_hex(server_key);
-				return stored_key, server_key, iteration_count, salt, true;
-			end
-		};
-		return new_sasl(module.host, testpass_authentication_profile);
+function provider.user_exists(username)
+	local account = datamanager.load(username, host, "accounts");
+	if not account then
+		log("debug", "account not found for username '%s' at host '%s'", username, host);
+		return nil, "Auth failed. Invalid username";
 	end
-	
-	return provider;
+	return true;
+end
+
+function provider.create_user(username, password)
+	if password == nil then
+		return datamanager.store(username, host, "accounts", {});
+	end
+	local salt = generate_uuid();
+	local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
+	local stored_key_hex = to_hex(stored_key);
+	local server_key_hex = to_hex(server_key);
+	return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
 end
 
-module:add_item("auth-provider", new_hashpass_provider(module.host));
+function provider.delete_user(username)
+	return datamanager.store(username, host, "accounts", nil);
+end
 
+function provider.get_sasl_handler()
+	local testpass_authentication_profile = {
+		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
+			return usermanager.test_password(prepped_username, realm, password), true;
+		end,
+		scram_sha_1 = function(sasl, username, realm)
+			local credentials = datamanager.load(username, host, "accounts");
+			if not credentials then return; end
+			if credentials.password then
+				usermanager.set_password(username, credentials.password, host);
+				credentials = datamanager.load(username, host, "accounts");
+				if not credentials then return; end
+			end
+			
+			local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
+			stored_key = stored_key and from_hex(stored_key);
+			server_key = server_key and from_hex(server_key);
+			return stored_key, server_key, iteration_count, salt, true;
+		end
+	};
+	return new_sasl(host, testpass_authentication_profile);
+end
+	
+module:provides("auth", provider);
+
--- a/plugins/mod_auth_internal_plain.lua	Mon Sep 10 23:17:06 2012 +0100
+++ b/plugins/mod_auth_internal_plain.lua	Wed Sep 12 22:03:57 2012 +0500
@@ -12,73 +12,71 @@
 local nodeprep = require "util.encodings".stringprep.nodeprep;
 
 local log = module._log;
-
-function new_default_provider(host)
-	local provider = { name = "internal_plain" };
-	log("debug", "initializing internal_plain authentication provider for host '%s'", host);
+local host = module.host;
 
-	function provider.test_password(username, password)
-		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
-		local credentials = datamanager.load(username, host, "accounts") or {};
-	
-		if password == credentials.password then
-			return true;
-		else
-			return nil, "Auth failed. Invalid username or password.";
-		end
-	end
+-- define auth provider
+local provider = {};
+log("debug", "initializing internal_plain authentication provider for host '%s'", host);
 
-	function provider.get_password(username)
-		log("debug", "get_password for username '%s' at host '%s'", username, module.host);
-		return (datamanager.load(username, host, "accounts") or {}).password;
-	end
-	
-	function provider.set_password(username, password)
-		local account = datamanager.load(username, host, "accounts");
-		if account then
-			account.password = password;
-			return datamanager.store(username, host, "accounts", account);
-		end
-		return nil, "Account not available.";
-	end
+function provider.test_password(username, password)
+	log("debug", "test password '%s' for user %s at host %s", password, username, host);
+	local credentials = datamanager.load(username, host, "accounts") or {};
 
-	function provider.user_exists(username)
-		local account = datamanager.load(username, host, "accounts");
-		if not account then
-			log("debug", "account not found for username '%s' at host '%s'", username, module.host);
-			return nil, "Auth failed. Invalid username";
-		end
+	if password == credentials.password then
 		return true;
+	else
+		return nil, "Auth failed. Invalid username or password.";
 	end
-
-	function provider.create_user(username, password)
-		return datamanager.store(username, host, "accounts", {password = password});
-	end
-	
-	function provider.delete_user(username)
-		return datamanager.store(username, host, "accounts", nil);
-	end
+end
 
-	function provider.get_sasl_handler()
-		local getpass_authentication_profile = {
-			plain = function(sasl, username, realm)
-				local prepped_username = nodeprep(username);
-				if not prepped_username then
-					log("debug", "NODEprep failed on username: %s", username);
-					return "", nil;
-				end
-				local password = usermanager.get_password(prepped_username, realm);
-				if not password then
-					return "", nil;
-				end
-				return password, true;
-			end
-		};
-		return new_sasl(module.host, getpass_authentication_profile);
+function provider.get_password(username)
+	log("debug", "get_password for username '%s' at host '%s'", username, host);
+	return (datamanager.load(username, host, "accounts") or {}).password;
+end
+
+function provider.set_password(username, password)
+	local account = datamanager.load(username, host, "accounts");
+	if account then
+		account.password = password;
+		return datamanager.store(username, host, "accounts", account);
 	end
-	
-	return provider;
+	return nil, "Account not available.";
 end
 
-module:add_item("auth-provider", new_default_provider(module.host));
+function provider.user_exists(username)
+	local account = datamanager.load(username, host, "accounts");
+	if not account then
+		log("debug", "account not found for username '%s' at host '%s'", username, host);
+		return nil, "Auth failed. Invalid username";
+	end
+	return true;
+end
+
+function provider.create_user(username, password)
+	return datamanager.store(username, host, "accounts", {password = password});
+end
+
+function provider.delete_user(username)
+	return datamanager.store(username, host, "accounts", nil);
+end
 
+function provider.get_sasl_handler()
+	local getpass_authentication_profile = {
+		plain = function(sasl, username, realm)
+			local prepped_username = nodeprep(username);
+			if not prepped_username then
+				log("debug", "NODEprep failed on username: %s", username);
+				return "", nil;
+			end
+			local password = usermanager.get_password(prepped_username, realm);
+			if not password then
+				return "", nil;
+			end
+			return password, true;
+		end
+	};
+	return new_sasl(host, getpass_authentication_profile);
+end
+	
+module:provides("auth", provider);
+
--- a/util/datamanager.lua	Mon Sep 10 23:17:06 2012 +0100
+++ b/util/datamanager.lua	Wed Sep 12 22:03:57 2012 +0500
@@ -25,28 +25,23 @@
 local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua)
 local lfs = require "lfs";
 local prosody = prosody;
-local raw_mkdir;
-local fallocate;
 
-if prosody.platform == "posix" then
-	raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
-	fallocate = require "util.pposix".fallocate;
-else
-	raw_mkdir = lfs.mkdir;
-end
-
-if not fallocate then -- Fallback
-	function fallocate(f, offset, len)
-		-- This assumes that current position == offset
-		local fake_data = (" "):rep(len);
-		local ok, msg = f:write(fake_data);
-		if not ok then
-			return ok, msg;
-		end
-		f:seek("set", offset);
-		return true;
+local raw_mkdir = lfs.mkdir;
+local function fallocate(f, offset, len)
+	-- This assumes that current position == offset
+	local fake_data = (" "):rep(len);
+	local ok, msg = f:write(fake_data);
+	if not ok then
+		return ok, msg;
 	end
-end
+	f:seek("set", offset);
+	return true;
+end;
+pcall(function()
+	local pposix = require "util.pposix";
+	raw_mkdir = pposix.mkdir or raw_mkdir; -- Doesn't trample on umask
+	fallocate = pposix.fallocate or fallocate;
+end);
 
 module "datamanager"