Changeset

3179:99c5288a26e4

Merge trunk/MattJ with trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 04 Jun 2010 14:08:40 +0100
parents 3178:46f5ed897beb (diff) 3157:174f3cfe86ff (current diff)
children 3180:99be525bcfb4 3181:1f73b3a960cf
files util/sasl/plain.lua
diffstat 11 files changed, 321 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/core/modulemanager.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/core/modulemanager.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -39,7 +39,7 @@
 
 local array, set = require "util.array", require "util.set";
 
-local autoload_modules = {"presence", "message", "iq"};
+local autoload_modules = {"presence", "message", "iq", "defaultauth"};
 
 -- We need this to let modules access the real global namespace
 local _G = _G;
--- a/core/usermanager.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/core/usermanager.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -20,117 +20,72 @@
 
 local prosody = _G.prosody;
 
+local setmetatable = setmetatable;
+
 module "usermanager"
 
-local new_default_provider;
+function new_null_provider()
+	local function dummy() end;
+	return setmetatable({name = "null"}, { __index = function() return dummy; end });
+end
 
 local function host_handler(host)
 	local host_session = hosts[host];
-	host_session.events.add_handler("item-added/auth-provider", function (provider)
-		if config.get(host, "core", "authentication") == provider.name then
+	host_session.events.add_handler("item-added/auth-provider", function (event)
+		local provider = event.item;
+		if config.get(host, "core", "authentication") == nil and provider.name == "default" then
+			host_session.users = provider;
+		elseif config.get(host, "core", "authentication") == provider.name then
 			host_session.users = provider;
 		end
-	end);
-	host_session.events.add_handler("item-removed/auth-provider", function (provider)
-		if host_session.users == provider then
-			host_session.users = new_default_provider(host);
+		if host_session.users ~= nil and host_session.users.name ~= nil then
+			log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
 		end
 	end);
-	host_session.users = new_default_provider(host); -- Start with the default usermanager provider
-end
-prosody.events.add_handler("host-activated", host_handler);
-prosody.events.add_handler("component-activated", host_handler);
-
-local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
-
-function new_default_provider(host)
-	local provider = { name = "default" };
-	
-	function provider:test_password(username, password)
-		if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
-		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
-
-	function provider:get_password(username)
-		if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
-		return (datamanager.load(username, host, "accounts") or {}).password;
-	end
-	
-	function provider:set_password(username, password)
-		if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
-		local account = datamanager.load(username, host, "accounts");
-		if account then
-			account.password = password;
-			return datamanager.store(username, host, "accounts", account);
+	host_session.events.add_handler("item-removed/auth-provider", function (event)
+		local provider = event.item;
+		if host_session.users == provider then
+			host_session.users = new_null_provider();
 		end
-		return nil, "Account not available.";
-	end
-
-	function provider:user_exists(username)
-		if not(require_provisioning) and is_cyrus(host) then return true; end
-		local account, err = datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
-		return (account or err) ~= nil; -- FIXME also check for empty credentials
-	end
-
-	function provider:create_user(username, password)
-		if not(require_provisioning) and is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
-		return datamanager.store(username, host, "accounts", {password = password});
-	end
-
-	function provider:get_supported_methods()
-		return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
-	end
+	end);
+   	host_session.users = new_null_provider(); -- Start with the default usermanager provider
+end;
+prosody.events.add_handler("host-activated", host_handler, 100);
+prosody.events.add_handler("component-activated", host_handler, 100);
 
-	function provider:is_admin(jid)
-		local admins = config.get(host, "core", "admins");
-		if admins ~= config.get("*", "core", "admins") then
-			if type(admins) == "table" then
-				jid = jid_bare(jid);
-				for _,admin in ipairs(admins) do
-					if admin == jid then return true; end
-				end
-			elseif admins then
-				log("error", "Option 'admins' for host '%s' is not a table", host);
-			end
-		end
-		return is_admin(jid); -- Test whether it's a global admin instead
-	end
-	return provider;
-end
+function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
 
-function validate_credentials(host, username, password, method)
-	return hosts[host].users:test_password(username, password);
+function test_password(username, password, host)
+	return hosts[host].users.test_password(username, password);
 end
 
 function get_password(username, host)
-	return hosts[host].users:get_password(username);
+	return hosts[host].users.get_password(username);
 end
 
-function set_password(username, host, password)
-	return hosts[host].users:set_password(username, password);
+function set_password(username, password, host)
+	return hosts[host].users.set_password(username, password);
 end
 
 function user_exists(username, host)
-	return hosts[host].users:user_exists(username);
+	return hosts[host].users.user_exists(username);
 end
 
 function create_user(username, password, host)
-	return hosts[host].users:create_user(username, password);
+	return hosts[host].users.create_user(username, password);
 end
 
 function get_supported_methods(host)
-	return hosts[host].users:get_supported_methods();
+	return hosts[host].users.get_supported_methods();
+end
+
+function get_provider(host)
+	return hosts[host].users;
 end
 
 function is_admin(jid, host)
 	if host and host ~= "*" then
-		return hosts[host].users:is_admin(jid);
+		return hosts[host].users.is_admin(jid);
 	else -- Test only whether this JID is a global admin
 		local admins = config.get("*", "core", "admins");
 		if type(admins) == "table" then
@@ -145,6 +100,4 @@
 	end
 end
 
-_M.new_default_provider = new_default_provider;
-
 return _M;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_defaultauth.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -0,0 +1,96 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2010 Jeff Mitchell
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local datamanager = require "util.datamanager";
+local log = require "util.logger".init("usermanager");
+local type = type;
+local error = error;
+local ipairs = ipairs;
+local hashes = require "util.hashes";
+local jid_bare = require "util.jid".bare;
+local config = require "core.configmanager";
+local usermanager = require "core.usermanager";
+local hosts = hosts;
+
+local prosody = _G.prosody;
+
+local is_cyrus = usermanager.is_cyrus;
+
+function new_default_provider(host)
+	local provider = { name = "default" };
+	log("debug", "initializing default authentication provider for host '%s'", host);
+
+	function provider.test_password(username, password)
+		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
+		if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
+		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
+
+	function provider.get_password(username)
+		log("debug", "get_password for username '%s' at host '%s'", username, module.host);
+		if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
+		return (datamanager.load(username, host, "accounts") or {}).password;
+	end
+	
+	function provider.set_password(username, password)
+		if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
+		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.user_exists(username)
+		if is_cyrus(host) then return true; end
+		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 account.password == nil or string.len(account.password) == 0 then
+			log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
+			return nil, "Auth failed. Password invalid.";
+		end
+		return true;
+	end
+
+	function provider.create_user(username, password)
+		if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
+		return datamanager.store(username, host, "accounts", {password = password});
+	end
+
+	function provider.get_supported_methods()
+		return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
+	end
+
+	function provider.is_admin(jid)
+		local admins = config.get(host, "core", "admins");
+		if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
+			jid = jid_bare(jid);
+			for _,admin in ipairs(admins) do
+				if admin == jid then return true; end
+			end
+		elseif admins then
+			log("error", "Option 'admins' for host '%s' is not a table", host);
+		end
+		return is_admin(jid); -- Test whether it's a global admin instead
+	end
+	return provider;
+end
+
+module:add_item("auth-provider", new_default_provider(module.host));
+
--- a/plugins/mod_groups.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/plugins/mod_groups.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -29,6 +29,9 @@
 			if jid ~= bare_jid then
 				if not roster[jid] then roster[jid] = {}; end
 				roster[jid].subscription = "both";
+				if groups[group_name][jid] then
+					roster[jid].name = groups[group_name][jid];
+				end
 				if not roster[jid].groups then
 					roster[jid].groups = { [group_name] = true };
 				end
@@ -100,10 +103,13 @@
 			groups[curr_group] = groups[curr_group] or {};
 		else
 			-- Add JID
-			local jid = jid_prep(line:match("%S+"));
+			local entryjid, name = line:match("([^=]*)=?(.*)");
+			module:log("debug", "entryjid = '%s', name = '%s'", entryjid, name);
+			local jid;
+			jid = jid_prep(entryjid:match("%S+"));
 			if jid then
 				module:log("debug", "New member of %s: %s", tostring(curr_group), tostring(jid));
-				groups[curr_group][jid] = true;
+				groups[curr_group][jid] = name or false;
 				members[jid] = members[jid] or {};
 				members[jid][#members[jid]+1] = curr_group;
 			end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_hashpassauth.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -0,0 +1,128 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2010 Jeff Mitchell
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local datamanager = require "util.datamanager";
+local log = require "util.logger".init("usermanager");
+local type = type;
+local error = error;
+local ipairs = ipairs;
+local hashes = require "util.hashes";
+local jid_bare = require "util.jid".bare;
+local saltedPasswordSHA1 = require "util.sasl.scram".saltedPasswordSHA1;
+local config = require "core.configmanager";
+local usermanager = require "core.usermanager";
+local generate_uuid = require "util.uuid".generate;
+local hosts = hosts;
+
+local prosody = _G.prosody;
+
+local is_cyrus = usermanager.is_cyrus;
+
+-- Default; can be set per-user
+local iteration_count = 4096;
+
+function new_hashpass_provider(host)
+	local provider = { name = "hashpass" };
+	log("debug", "initializing hashpass authentication provider for host '%s'", host);
+
+	function provider.test_password(username, password)
+		if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
+		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
+
+			if provider.set_password(username, credentials.password) == nil then
+				return nil, "Auth failed. Could not set hashed password from plaintext.";
+			else
+				return true;
+			end
+		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, binpass = saltedPasswordSHA1(password, credentials.salt, credentials.iteration_count);
+		local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
+
+		if valid and hexpass == credentials.hashpass then
+			return true;
+		else
+			return nil, "Auth failed. Invalid username, password, or password hash information.";
+		end
+	end
+
+	function provider.set_password(username, password)
+		if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
+		local account = datamanager.load(username, host, "accounts");
+		if account then
+			if account.iteration_count == nil then
+				account.iteration_count = iteration_count;
+			end
+
+			if account.salt == nil then
+				account.salt = generate_uuid();
+			end
+
+			local valid, binpass = saltedPasswordSHA1(password, account.salt, account.iteration_count);
+			local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
+			account.hashpass = hexpass;
+
+			account.password = nil;
+			return datamanager.store(username, host, "accounts", account);
+		end
+		return nil, "Account not available.";
+	end
+
+	function provider.user_exists(username)
+		if is_cyrus(host) then return true; end
+		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 (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
+			log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
+			return nil, "Auth failed. Password invalid.";
+		end
+		return true;
+	end
+
+	function provider.create_user(username, password)
+		if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
+		local salt = generate_uuid();
+		local valid, binpass = saltedPasswordSHA1(password, salt, iteration_count);
+		local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
+		return datamanager.store(username, host, "accounts", {hashpass = hexpass, salt = salt, iteration_count = iteration_count});
+	end
+
+	function provider.get_supported_methods()
+		return {["PLAIN"] = true}; -- TODO this should be taken from the config
+	end
+
+	function provider.is_admin(jid)
+		local admins = config.get(host, "core", "admins");
+		if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
+			jid = jid_bare(jid);
+			for _,admin in ipairs(admins) do
+				if admin == jid then return true; end
+			end
+		elseif admins then
+			log("error", "Option 'admins' for host '%s' is not a table", host);
+		end
+		return is_admin(jid); -- Test whether it's a global admin instead
+	end
+	return provider;
+end
+
+module:add_item("auth-provider", new_hashpass_provider(module.host));
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_motd.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -0,0 +1,25 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2010 Jeff Mitchell
+-- 
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local host = module:get_host();
+local motd_text = module:get_option("motd_text") or "MOTD: (blank)";
+local motd_jid = module:get_option("motd_jid") or host;
+
+local st = require "util.stanza";
+
+module:hook("resource-bind",
+	function (event)
+		local session = event.session;
+		local motd_stanza =
+			st.message({ to = session.username..'@'..session.host, from = motd_jid })
+				:tag("body"):text(motd_text);
+		core_route_stanza(hosts[host], motd_stanza);
+		module:log("debug", "MOTD send to user %s@%s", session.username, session.host);
+
+end);
--- a/plugins/mod_register.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/plugins/mod_register.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -35,7 +35,7 @@
 				local username, host = session.username, session.host;
 				--session.send(st.error_reply(stanza, "cancel", "not-allowed"));
 				--return;
-				usermanager_set_password(username, host, nil); -- Disable account
+				usermanager_set_password(username, nil, host); -- Disable account
 				-- FIXME the disabling currently allows a different user to recreate the account
 				-- we should add an in-memory account block mode when we have threading
 				session.send(st.reply(stanza));
@@ -70,7 +70,7 @@
 					username = nodeprep(table.concat(username));
 					password = table.concat(password);
 					if username == session.username then
-						if usermanager_set_password(username, session.host, password) then
+						if usermanager_set_password(username, password, session.host) then
 							session.send(st.reply(stanza));
 						else
 							-- TODO unable to write file, file may be locked, etc, what's the correct error?
--- a/plugins/mod_saslauth.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/plugins/mod_saslauth.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -15,10 +15,11 @@
 
 local nodeprep = require "util.encodings".stringprep.nodeprep;
 local datamanager_load = require "util.datamanager".load;
-local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
+local usermanager_get_provider = require "core.usermanager".get_provider;
 local usermanager_get_supported_methods = require "core.usermanager".get_supported_methods;
 local usermanager_user_exists = require "core.usermanager".user_exists;
 local usermanager_get_password = require "core.usermanager".get_password;
+local usermanager_test_password = require "core.usermanager".test_password;
 local t_concat, t_insert = table.concat, table.insert;
 local tostring = tostring;
 local jid_split = require "util.jid".split;
@@ -66,7 +67,7 @@
 	error("Unknown SASL backend");
 end
 
-local default_authentication_profile = {
+local getpass_authentication_profile = {
 	plain = function(username, realm)
 		local prepped_username = nodeprep(username);
 		if not prepped_username then
@@ -81,6 +82,17 @@
 	end
 };
 
+local testpass_authentication_profile = {
+	plain_test = 	function(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, password, realm), true;
+			end
+};
+
 local anonymous_authentication_profile = {
 	anonymous = function(username, realm)
 		return true; -- for normal usage you should always return true here
@@ -183,7 +195,13 @@
 		if module:get_option("anonymous_login") then
 			origin.sasl_handler = new_sasl(realm, anonymous_authentication_profile);
 		else
-			origin.sasl_handler = new_sasl(realm, default_authentication_profile);
+			if usermanager_get_provider(realm).get_password then
+				origin.sasl_handler = new_sasl(realm, getpass_authentication_profile);
+			elseif usermanager_get_provider(realm).test_password then
+				origin.sasl_handler = new_sasl(realm, testpass_authentication_profile);
+			else
+				log("warn", "AUTH: Could not load an authentication profile for the given provider.");
+			end
 			if not (module:get_option("allow_unencrypted_plain_auth")) and not origin.secure then
 				origin.sasl_handler:forbidden({"PLAIN"});
 			end
--- a/prosody.cfg.lua.dist	Thu Jun 03 18:09:02 2010 +0500
+++ b/prosody.cfg.lua.dist	Fri Jun 04 14:08:40 2010 +0100
@@ -71,6 +71,7 @@
 	-- "presence";
 	-- "message";
 	-- "iq";
+	-- "defaultauth";
 };
 
 -- Disable account creation by default, for security
--- a/prosodyctl	Thu Jun 03 18:09:02 2010 +0500
+++ b/prosodyctl	Fri Jun 04 14:08:40 2010 +0100
@@ -123,7 +123,7 @@
 hosts = prosody.hosts;
 
 local function make_host(hostname)
-	return { events = prosody.events, users = require "core.usermanager".new_default_provider(hostname) };
+	return { events = prosody.events, users = require "core.usermanager".new_null_provider(hostname) };
 end
 
 for hostname, config in pairs(config.getconfig()) do
--- a/util/sasl/plain.lua	Thu Jun 03 18:09:02 2010 +0500
+++ b/util/sasl/plain.lua	Fri Jun 04 14:08:40 2010 +0100
@@ -29,7 +29,7 @@
 	end
 
 plain_test:
-	function(username, realm, password)
+	function(username, password, realm)
 		return true or false, state;
 	end
 ]]
@@ -60,7 +60,7 @@
 		correct_password, state = self.profile.plain(authentication, self.realm);
 		correct = (correct_password == password);
 	elseif self.profile.plain_test then
-		correct, state = self.profile.plain_test(authentication, self.realm, password);
+		correct, state = self.profile.plain_test(authentication, password, self.realm);
 	end
 
 	self.username = authentication