4e55458494a40a346032db6bcac7992a0ce3b2c3
[prosody.git] / plugins / mod_auth_internal_hashed.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2010 Jeff Mitchell
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local datamanager = require "util.datamanager";
11 local log = require "util.logger".init("usermanager");
12 local type = type;
13 local error = error;
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;
23 local hosts = hosts;
24
25 -- TODO: remove these two lines in near future
26 local hmac_sha1 = require "util.hmac".sha1;
27 local sha1 = require "util.hashes".sha1;
28
29 local prosody = _G.prosody;
30
31 local is_cyrus = usermanager.is_cyrus;
32
33 -- Default; can be set per-user
34 local iteration_count = 4096;
35
36 function new_hashpass_provider(host)
37         local provider = { name = "internal_hashed" };
38         log("debug", "initializing hashpass authentication provider for host '%s'", host);
39
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 {};
43         
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.";
47                         end
48
49                         if provider.set_password(username, credentials.password) == nil then
50                                 return nil, "Auth failed. Could not set hashed password from plaintext.";
51                         else
52                                 return true;
53                         end
54                 end
55
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.";
58                 end
59                 
60                 local valid, stored_key, server_key
61                 
62                 if credentials.hexpass then
63                         -- convert hexpass to stored_key and server_key
64                         -- TODO: remove this in near future
65                         valid = true;
66                         local salted_password = credentials.hexpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
67                         
68                         stored_key = sha1(hmac_sha1(salted_password, "Client Key"))
69                         server_key = hmac_sha1(salted_password, "Server Key");
70                 else
71                         valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
72                 end
73                 
74                 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
75                 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
76                 
77                 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key_hex then
78                         return true;
79                 else
80                         return nil, "Auth failed. Invalid username, password, or password hash information.";
81                 end
82         end
83
84         function provider.set_password(username, password)
85                 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
86                 local account = datamanager.load(username, host, "accounts");
87                 if account then
88                         if account.iteration_count == nil then
89                                 account.iteration_count = iteration_count;
90                         end
91
92                         if account.salt == nil then
93                                 account.salt = generate_uuid();
94                         end
95                         
96                         local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
97                         local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
98                         local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
99                         
100                         account.stored_key = stored_key_hex
101                         account.server_key = server_key_hex
102
103                         account.password = nil;
104                         return datamanager.store(username, host, "accounts", account);
105                 end
106                 return nil, "Account not available.";
107         end
108
109         function provider.user_exists(username)
110                 if is_cyrus(host) then return true; end
111                 local account = datamanager.load(username, host, "accounts");
112                 if not account then
113                         log("debug", "account not found for username '%s' at host '%s'", username, module.host);
114                         return nil, "Auth failed. Invalid username";
115                 end
116                 if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
117                         log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
118                         return nil, "Auth failed. Password invalid.";
119                 end
120                 return true;
121         end
122
123         function provider.create_user(username, password)
124                 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
125                 local salt = generate_uuid();
126                 local valid, stored_key, server_key = saltedPasswordSHA1(password, salt, iteration_count);
127                 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
128                 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
129                 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
130         end
131
132         function provider.get_sasl_handler()
133                 local realm = module:get_option("sasl_realm") or module.host;
134                 local testpass_authentication_profile = {
135                         plain_test = function(username, password, realm)
136                                 local prepped_username = nodeprep(username);
137                                 if not prepped_username then
138                                         log("debug", "NODEprep failed on username: %s", username);
139                                         return "", nil;
140                                 end
141                                 return usermanager.test_password(prepped_username, password, realm), true;
142                         end,
143                         scram_sha_1 = function(username, realm)
144                                 local credentials = datamanager.load(username, host, "accounts") or {};
145                                 if credentials.password then
146                                         usermanager.set_password(username, credentials.password);
147                                         credentials = datamanager.load(username, host, "accounts") or {};
148                                 end
149                                 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
150                                 stored_key = stored_key and stored_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
151                                 server_key = server_key and server_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
152                                 return stored_key, server_key, iteration_count, salt, true;
153                         end
154                 };
155                 return new_sasl(realm, testpass_authentication_profile);
156         end
157
158         function provider.is_admin(jid)
159                 local admins = config.get(host, "core", "admins");
160                 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
161                         jid = jid_bare(jid);
162                         for _,admin in ipairs(admins) do
163                                 if admin == jid then return true; end
164                         end
165                 elseif admins then
166                         log("error", "Option 'admins' for host '%s' is not a table", host);
167                 end
168                 return is_admin(jid); -- Test whether it's a global admin instead
169         end
170         return provider;
171 end
172
173 module:add_item("auth-provider", new_hashpass_provider(module.host));
174