2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2010 Jeff Mitchell
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local datamanager = require "util.datamanager";
11 local log = require "util.logger".init("usermanager");
14 local ipairs = ipairs;
15 local hashes = require "util.hashes";
16 local jid_bare = require "util.jid".bare;
17 local getAuthenticationDatabaseSHA1 = require "util.sasl.scram".getAuthenticationDatabaseSHA1;
18 local config = require "core.configmanager";
19 local usermanager = require "core.usermanager";
20 local generate_uuid = require "util.uuid".generate;
21 local new_sasl = require "util.sasl".new;
22 local nodeprep = require "util.encodings".stringprep.nodeprep;
25 -- TODO: remove these two lines in near future
26 local hmac_sha1 = require "util.hmac".sha1;
27 local sha1 = require "util.hashes".sha1;
29 local prosody = _G.prosody;
31 local is_cyrus = usermanager.is_cyrus;
33 -- Default; can be set per-user
34 local iteration_count = 4096;
36 function new_hashpass_provider(host)
37 local provider = { name = "internal_hashed" };
38 log("debug", "initializing hashpass authentication provider for host '%s'", host);
40 function provider.test_password(username, password)
41 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
42 local credentials = datamanager.load(username, host, "accounts") or {};
44 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
45 if credentials.password ~= password then
46 return nil, "Auth failed. Provided password is incorrect.";
49 if provider.set_password(username, credentials.password) == nil then
50 return nil, "Auth failed. Could not set hashed password from plaintext.";
56 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
57 return nil, "Auth failed. Stored salt and iteration count information is not complete.";
60 local valid, stored_key, server_key
62 -- convert hexpass to stored_key and server_key
63 -- TODO: remove this in near future
64 if credentials.hashpass then
66 local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
67 credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
68 credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
69 credentials.hashpass = nil
70 datamanager.store(username, host, "accounts", credentials);
73 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
75 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
76 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
78 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
81 return nil, "Auth failed. Invalid username, password, or password hash information.";
85 function provider.set_password(username, password)
86 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
87 local account = datamanager.load(username, host, "accounts");
89 account.salt = account.salt or generate_uuid();
90 account.iteration_count = account.iteration_count or iteration_count;
91 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
92 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
93 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
95 account.stored_key = stored_key_hex
96 account.server_key = server_key_hex
98 account.password = nil;
99 return datamanager.store(username, host, "accounts", account);
101 return nil, "Account not available.";
104 function provider.user_exists(username)
105 if is_cyrus(host) then return true; end
106 local account = datamanager.load(username, host, "accounts");
108 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
109 return nil, "Auth failed. Invalid username";
111 if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
112 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
113 return nil, "Auth failed. Password invalid.";
118 function provider.create_user(username, password)
119 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
120 local salt = generate_uuid();
121 local valid, stored_key, server_key = saltedPasswordSHA1(password, salt, iteration_count);
122 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
123 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
124 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
127 function provider.get_sasl_handler()
128 local realm = module:get_option("sasl_realm") or module.host;
129 local testpass_authentication_profile = {
130 plain_test = function(username, password, realm)
131 local prepped_username = nodeprep(username);
132 if not prepped_username then
133 log("debug", "NODEprep failed on username: %s", username);
136 return usermanager.test_password(prepped_username, password, realm), true;
138 scram_sha_1 = function(username, realm)
139 local credentials = datamanager.load(username, host, "accounts") or {};
140 if credentials.password then
141 usermanager.set_password(username, credentials.password);
142 credentials = datamanager.load(username, host, "accounts") or {};
145 -- convert hexpass to stored_key and server_key
146 -- TODO: remove this in near future
147 if credentials.hashpass then
148 local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
149 credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
150 credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
151 credentials.hashpass = nil
152 datamanager.store(username, host, "accounts", credentials);
155 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
156 stored_key = stored_key and stored_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
157 server_key = server_key and server_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
158 return stored_key, server_key, iteration_count, salt, true;
161 return new_sasl(realm, testpass_authentication_profile);
164 function provider.is_admin(jid)
165 local admins = config.get(host, "core", "admins");
166 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
168 for _,admin in ipairs(admins) do
169 if admin == jid then return true; end
172 log("error", "Option 'admins' for host '%s' is not a table", host);
174 return is_admin(jid); -- Test whether it's a global admin instead
179 module:add_item("auth-provider", new_hashpass_provider(module.host));