f8e9b00cf56f6e469424b35616c141c62febdd82
[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 -- Default; can be set per-user
32 local iteration_count = 4096;
33
34 function new_hashpass_provider(host)
35         local provider = { name = "internal_hashed" };
36         log("debug", "initializing hashpass authentication provider for host '%s'", host);
37
38         function provider.test_password(username, password)
39                 local credentials = datamanager.load(username, host, "accounts") or {};
40         
41                 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
42                         if credentials.password ~= password then
43                                 return nil, "Auth failed. Provided password is incorrect.";
44                         end
45
46                         if provider.set_password(username, credentials.password) == nil then
47                                 return nil, "Auth failed. Could not set hashed password from plaintext.";
48                         else
49                                 return true;
50                         end
51                 end
52
53                 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
54                         return nil, "Auth failed. Stored salt and iteration count information is not complete.";
55                 end
56                 
57                 -- convert hexpass to stored_key and server_key
58                 -- TODO: remove this in near future
59                 if credentials.hashpass then
60                         local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
61                         credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
62                         credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
63                         credentials.hashpass = nil
64                         datamanager.store(username, host, "accounts", credentials);
65                 end
66                 
67                 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
68                 
69                 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
70                 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
71                 
72                 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
73                         return true;
74                 else
75                         return nil, "Auth failed. Invalid username, password, or password hash information.";
76                 end
77         end
78
79         function provider.set_password(username, password)
80                 local account = datamanager.load(username, host, "accounts");
81                 if account then
82                         account.salt = account.salt or generate_uuid();
83                         account.iteration_count = account.iteration_count or iteration_count;
84                         local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
85                         local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
86                         local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
87                         
88                         account.stored_key = stored_key_hex
89                         account.server_key = server_key_hex
90
91                         account.password = nil;
92                         return datamanager.store(username, host, "accounts", account);
93                 end
94                 return nil, "Account not available.";
95         end
96
97         function provider.user_exists(username)
98                 local account = datamanager.load(username, host, "accounts");
99                 if not account then
100                         log("debug", "account not found for username '%s' at host '%s'", username, module.host);
101                         return nil, "Auth failed. Invalid username";
102                 end
103                 if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
104                         log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
105                         return nil, "Auth failed. Password invalid.";
106                 end
107                 return true;
108         end
109
110         function provider.create_user(username, password)
111                 local salt = generate_uuid();
112                 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
113                 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
114                 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
115                 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
116         end
117
118         function provider.get_sasl_handler()
119                 local realm = module:get_option("sasl_realm") or module.host;
120                 local testpass_authentication_profile = {
121                         plain_test = function(username, password, realm)
122                                 local prepped_username = nodeprep(username);
123                                 if not prepped_username then
124                                         log("debug", "NODEprep failed on username: %s", username);
125                                         return "", nil;
126                                 end
127                                 return usermanager.test_password(prepped_username, password, realm), true;
128                         end,
129                         scram_sha_1 = function(username, realm)
130                                 local credentials = datamanager.load(username, host, "accounts") or {};
131                                 if credentials.password then
132                                         usermanager.set_password(username, credentials.password, host);
133                                         credentials = datamanager.load(username, host, "accounts") or {};
134                                 end
135                                 
136                                 -- convert hexpass to stored_key and server_key
137                                 -- TODO: remove this in near future
138                                 if credentials.hashpass then
139                                         local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
140                                         credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
141                                         credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
142                                         credentials.hashpass = nil
143                                         datamanager.store(username, host, "accounts", credentials);
144                                 end
145                                 
146                                 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
147                                 stored_key = stored_key and stored_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
148                                 server_key = server_key and server_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
149                                 return stored_key, server_key, iteration_count, salt, true;
150                         end
151                 };
152                 return new_sasl(realm, testpass_authentication_profile);
153         end
154
155         function provider.is_admin(jid)
156                 local admins = module:get_option_array("admins");
157                 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
158                         jid = jid_bare(jid);
159                         for _,admin in ipairs(admins) do
160                                 if admin == jid then return true; end
161                         end
162                 end
163         end
164         return provider;
165 end
166
167 module:add_item("auth-provider", new_hashpass_provider(module.host));
168