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