Merge 0.7->trunk
[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 module "usermanager"
20
21 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
22
23 function validate_credentials(host, username, password, method)
24         log("debug", "User '%s' is being validated", username);
25         if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
26         local credentials = datamanager.load(username, host, "accounts") or {};
27
28         if method == nil then method = "PLAIN"; end
29         if method == "PLAIN" and credentials.password then -- PLAIN, do directly
30                 if password == credentials.password then
31                         return true;
32                 else
33                         return nil, "Auth failed. Invalid username or password.";
34                 end
35   end
36         -- must do md5
37         -- make credentials md5
38         local pwd = credentials.password;
39         if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
40         -- make password md5
41         if method == "PLAIN" then
42                 password = hashes.md5(password or "", true);
43         elseif method ~= "DIGEST-MD5" then
44                 return nil, "Unsupported auth method";
45         end
46         -- compare
47         if password == pwd then
48                 return true;
49         else
50                 return nil, "Auth failed. Invalid username or password.";
51         end
52 end
53
54 function get_password(username, host)
55         if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
56         return (datamanager.load(username, host, "accounts") or {}).password
57 end
58 function set_password(username, host, password)
59         if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
60         local account = datamanager.load(username, host, "accounts");
61         if account then
62                 account.password = password;
63                 return datamanager.store(username, host, "accounts", account);
64         end
65         return nil, "Account not available.";
66 end
67
68 function user_exists(username, host)
69         if is_cyrus(host) then return true; end
70         return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
71 end
72
73 function create_user(username, password, host)
74         if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
75         return datamanager.store(username, host, "accounts", {password = password});
76 end
77
78 function get_supported_methods(host)
79         return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
80 end
81
82 function is_admin(jid, host)
83         host = host or "*";
84         local admins = config.get(host, "core", "admins");
85         if host ~= "*" and admins == config.get("*", "core", "admins") then
86                 return nil;
87         end
88         if type(admins) == "table" then
89                 jid = jid_bare(jid);
90                 for _,admin in ipairs(admins) do
91                         if admin == jid then return true; end
92                 end
93         elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end
94         return nil;
95 end
96
97 return _M;