9cffcc6e3874a7e9d0435f009ceb97e199267f97
[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 saltedPasswordSHA1 = require "util.sasl.scram".saltedPasswordSHA1;
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 local prosody = _G.prosody;
26
27 local is_cyrus = usermanager.is_cyrus;
28
29 -- Default; can be set per-user
30 local iteration_count = 4096;
31
32 function new_hashpass_provider(host)
33         local provider = { name = "internal_hashed" };
34         log("debug", "initializing hashpass authentication provider for host '%s'", host);
35
36         function provider.test_password(username, password)
37                 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
38                 local credentials = datamanager.load(username, host, "accounts") or {};
39         
40                 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
41                         if credentials.password ~= password then
42                                 return nil, "Auth failed. Provided password is incorrect.";
43                         end
44
45                         if provider.set_password(username, credentials.password) == nil then
46                                 return nil, "Auth failed. Could not set hashed password from plaintext.";
47                         else
48                                 return true;
49                         end
50                 end
51
52                 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
53                         return nil, "Auth failed. Stored salt and iteration count information is not complete.";
54                 end
55
56                 local valid, binpass = saltedPasswordSHA1(password, credentials.salt, credentials.iteration_count);
57                 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
58
59                 if valid and hexpass == credentials.hashpass then
60                         return true;
61                 else
62                         return nil, "Auth failed. Invalid username, password, or password hash information.";
63                 end
64         end
65
66         function provider.set_password(username, password)
67                 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
68                 local account = datamanager.load(username, host, "accounts");
69                 if account then
70                         if account.iteration_count == nil then
71                                 account.iteration_count = iteration_count;
72                         end
73
74                         if account.salt == nil then
75                                 account.salt = generate_uuid();
76                         end
77
78                         local valid, binpass = saltedPasswordSHA1(password, account.salt, account.iteration_count);
79                         local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
80                         account.hashpass = hexpass;
81
82                         account.password = nil;
83                         return datamanager.store(username, host, "accounts", account);
84                 end
85                 return nil, "Account not available.";
86         end
87
88         function provider.user_exists(username)
89                 if is_cyrus(host) then return true; end
90                 local account = datamanager.load(username, host, "accounts");
91                 if not account then
92                         log("debug", "account not found for username '%s' at host '%s'", username, module.host);
93                         return nil, "Auth failed. Invalid username";
94                 end
95                 if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
96                         log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
97                         return nil, "Auth failed. Password invalid.";
98                 end
99                 return true;
100         end
101
102         function provider.create_user(username, password)
103                 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
104                 local salt = generate_uuid();
105                 local valid, binpass = saltedPasswordSHA1(password, salt, iteration_count);
106                 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
107                 return datamanager.store(username, host, "accounts", {hashpass = hexpass, salt = salt, iteration_count = iteration_count});
108         end
109
110         function provider.get_sasl_handler()
111                 local realm = module:get_option("sasl_realm") or module.host;
112                 local testpass_authentication_profile = {
113                         plain_test = function(username, password, realm)
114                                 local prepped_username = nodeprep(username);
115                                 if not prepped_username then
116                                         log("debug", "NODEprep failed on username: %s", username);
117                                         return "", nil;
118                                 end
119                                 return usermanager.test_password(prepped_username, password, realm), true;
120                         end,
121                         scram_sha_1 = function(username, realm)
122                                 local credentials = datamanager.load(username, host, "accounts") or {};
123                                 if credentials.password then
124                                         usermanager.set_password(username, credentials.password);
125                                         credentials = datamanager.load(username, host, "accounts") or {};
126                                 end
127                                 local salted_password, iteration_count, salt = credentials.hashpass, credentials.iteration_count, credentials.salt;
128                                 salted_password = salted_password and salted_password:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
129                                 return salted_password, iteration_count, salt, true;
130                         end
131                 };
132                 return new_sasl(realm, testpass_authentication_profile);
133         end
134
135         function provider.is_admin(jid)
136                 local admins = config.get(host, "core", "admins");
137                 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
138                         jid = jid_bare(jid);
139                         for _,admin in ipairs(admins) do
140                                 if admin == jid then return true; end
141                         end
142                 elseif admins then
143                         log("error", "Option 'admins' for host '%s' is not a table", host);
144                 end
145                 return is_admin(jid); -- Test whether it's a global admin instead
146         end
147         return provider;
148 end
149
150 module:add_item("auth-provider", new_hashpass_provider(module.host));
151