2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local datamanager = require "util.datamanager";
10 local log = require "util.logger".init("usermanager");
13 local ipairs = ipairs;
14 local hashes = require "util.hashes";
15 local jid_bare = require "util.jid".bare;
16 local config = require "core.configmanager";
19 local require_provisioning = config.get("*", "core", "cyrus_require_provisioning") or false;
23 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
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 {};
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
35 return nil, "Auth failed. Invalid username or password.";
39 -- make credentials md5
40 local pwd = credentials.password;
41 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
43 if method == "PLAIN" then
44 password = hashes.md5(password or "", true);
45 elseif method ~= "DIGEST-MD5" then
46 return nil, "Unsupported auth method";
49 if password == pwd then
52 return nil, "Auth failed. Invalid username or password.";
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
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");
64 account.password = password;
65 return datamanager.store(username, host, "accounts", account);
67 return nil, "Account not available.";
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
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});
81 function get_supported_methods(host)
82 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
85 function is_admin(jid, host)
87 local admins = config.get(host, "core", "admins");
88 if host ~= "*" and admins == config.get("*", "core", "admins") then
91 if type(admins) == "table" then
93 for _,admin in ipairs(admins) do
94 if admin == jid then return true; end
96 elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end