07097dc1b97a81d11eaee2bc1e1405b234b5a9b0
[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 local prosody = _G.prosody;
22
23 module "usermanager"
24
25 local new_default_provider;
26
27 local function host_handler(host)
28         local host_session = hosts[host];
29         host_session.events.add_handler("item-added/auth-provider", function (provider)
30                 if config.get(host, "core", "authentication") == provider.name then
31                         host_session.users = provider;
32                 end
33         end);
34         host_session.events.add_handler("item-removed/auth-provider", function (provider)
35                 if host_session.users == provider then
36                         host_session.users = new_default_provider(host);
37                 end
38         end);
39         host_session.users = new_default_provider(host); -- Start with the default usermanager provider
40 end
41 prosody.events.add_handler("host-activated", host_handler);
42 prosody.events.add_handler("component-activated", host_handler);
43
44 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
45
46 function new_default_provider(host)
47         local provider = { name = "default" };
48         
49         function provider:test_password(username, password)
50                 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
51                 local credentials = datamanager.load(username, host, "accounts") or {};
52         
53                 if password == credentials.password then
54                         return true;
55                 else
56                         return nil, "Auth failed. Invalid username or password.";
57                 end
58         end
59
60         function provider:get_password(username)
61                 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
62                 return (datamanager.load(username, host, "accounts") or {}).password;
63         end
64         
65         function provider:set_password(username, password)
66                 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
67                 local account = datamanager.load(username, host, "accounts");
68                 if account then
69                         account.password = password;
70                         return datamanager.store(username, host, "accounts", account);
71                 end
72                 return nil, "Account not available.";
73         end
74
75         function provider:user_exists(username)
76                 if not(require_provisioning) and is_cyrus(host) then return true; end
77                 local account, err = datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
78                 return (account or err) ~= nil; -- FIXME also check for empty credentials
79         end
80
81         function provider:create_user(username, password)
82                 if not(require_provisioning) and is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
83                 return datamanager.store(username, host, "accounts", {password = password});
84         end
85
86         function provider:get_supported_methods()
87                 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
88         end
89
90         function provider:is_admin(jid)
91                 local admins = config.get(host, "core", "admins");
92                 if admins ~= config.get("*", "core", "admins") then
93                         if type(admins) == "table" then
94                                 jid = jid_bare(jid);
95                                 for _,admin in ipairs(admins) do
96                                         if admin == jid then return true; end
97                                 end
98                         elseif admins then
99                                 log("error", "Option 'admins' for host '%s' is not a table", host);
100                         end
101                 end
102                 return is_admin(jid); -- Test whether it's a global admin instead
103         end
104         return provider;
105 end
106
107 function validate_credentials(host, username, password, method)
108         return hosts[host].users:test_password(username, password);
109 end
110
111 function get_password(username, host)
112         return hosts[host].users:get_password(username);
113 end
114
115 function set_password(username, host, password)
116         return hosts[host].users:set_password(username, password);
117 end
118
119 function user_exists(username, host)
120         return hosts[host].users:user_exists(username);
121 end
122
123 function create_user(username, password, host)
124         return hosts[host].users:create_user(username, password);
125 end
126
127 function get_supported_methods(host)
128         return hosts[host].users:get_supported_methods();
129 end
130
131 function is_admin(jid, host)
132         if host and host ~= "*" then
133                 return hosts[host].users:is_admin(jid);
134         else -- Test only whether this JID is a global admin
135                 local admins = config.get("*", "core", "admins");
136                 if type(admins) == "table" then
137                         jid = jid_bare(jid);
138                         for _,admin in ipairs(admins) do
139                                 if admin == jid then return true; end
140                         end
141                 elseif admins then
142                         log("error", "Option 'admins' for host '%s' is not a table", host);
143                 end
144                 return nil;
145         end
146 end
147
148 _M.new_default_provider = new_default_provider;
149
150 return _M;