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