698d2f10413676bc28f3779e94b0f5242fe73e78
[prosody.git] / core / usermanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local datamanager = require "util.datamanager";
10 local log = require "util.logger".init("usermanager");
11 local type = type;
12 local error = error;
13 local ipairs = ipairs;
14 local hashes = require "util.hashes";
15 local jid_bare = require "util.jid".bare;
16 local config = require "core.configmanager";
17 local hosts = hosts;
18
19 local require_provisioning = config.get("*", "core", "cyrus_require_provisioning") or false;
20
21 module "usermanager"
22
23 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
24
25 function validate_credentials(host, username, password, method)
26         log("debug", "User '%s' is being validated", username);
27         if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
28         local credentials = datamanager.load(username, host, "accounts") or {};
29
30         if method == nil then method = "PLAIN"; end
31         if method == "PLAIN" and credentials.password then -- PLAIN, do directly
32                 if password == credentials.password then
33                         return true;
34                 else
35                         return nil, "Auth failed. Invalid username or password.";
36                 end
37   end
38         -- must do md5
39         -- make credentials md5
40         local pwd = credentials.password;
41         if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
42         -- make password md5
43         if method == "PLAIN" then
44                 password = hashes.md5(password or "", true);
45         elseif method ~= "DIGEST-MD5" then
46                 return nil, "Unsupported auth method";
47         end
48         -- compare
49         if password == pwd then
50                 return true;
51         else
52                 return nil, "Auth failed. Invalid username or password.";
53         end
54 end
55
56 function get_password(username, host)
57         if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
58         return (datamanager.load(username, host, "accounts") or {}).password
59 end
60 function set_password(username, host, password)
61         if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
62         local account = datamanager.load(username, host, "accounts");
63         if account then
64                 account.password = password;
65                 return datamanager.store(username, host, "accounts", account);
66         end
67         return nil, "Account not available.";
68 end
69
70 function user_exists(username, host)
71         if not(require_provisioning) and is_cyrus(host) then return true; end
72         local account, err = datamanager.load(username, host, "accounts");
73         return (account or err) ~= nil; -- FIXME also check for empty credentials
74 end
75
76 function create_user(username, password, host)
77         if not(require_provisioning) and is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
78         return datamanager.store(username, host, "accounts", {password = password});
79 end
80
81 function get_supported_methods(host)
82         return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
83 end
84
85 function is_admin(jid, host)
86         host = host or "*";
87         local admins = config.get(host, "core", "admins");
88         if host ~= "*" and admins == config.get("*", "core", "admins") then
89                 return nil;
90         end
91         if type(admins) == "table" then
92                 jid = jid_bare(jid);
93                 for _,admin in ipairs(admins) do
94                         if admin == jid then return true; end
95                 end
96         elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end
97         return nil;
98 end
99
100 return _M;