mod_console: Add host:* commands to help (thanks Zash)
[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 modulemanager = require "core.modulemanager";
11 local log = require "util.logger".init("usermanager");
12 local type = type;
13 local error = error;
14 local ipairs = ipairs;
15 local hashes = require "util.hashes";
16 local jid_bare = require "util.jid".bare;
17 local config = require "core.configmanager";
18 local hosts = hosts;
19 local sasl_new = require "util.sasl".new;
20
21 local require_provisioning = config.get("*", "core", "cyrus_require_provisioning") or false;
22
23 local prosody = _G.prosody;
24
25 local setmetatable = setmetatable;
26
27 local default_provider = "internal_plain";
28
29 module "usermanager"
30
31 function new_null_provider()
32         local function dummy() end;
33         local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
34         return setmetatable({name = "null", get_sasl_handler = dummy_get_sasl_handler}, { __index = function() return dummy; end });
35 end
36
37 function initialize_host(host)
38         local host_session = hosts[host];
39         host_session.events.add_handler("item-added/auth-provider", function (event)
40                 local provider = event.item;
41                 local auth_provider = config.get(host, "core", "authentication") or default_provider;
42                 if provider.name == auth_provider then
43                         host_session.users = provider;
44                 end
45                 if host_session.users ~= nil and host_session.users.name ~= nil then
46                         log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
47                 end
48         end);
49         host_session.events.add_handler("item-removed/auth-provider", function (event)
50                 local provider = event.item;
51                 if host_session.users == provider then
52                         host_session.users = new_null_provider();
53                 end
54         end);
55         host_session.users = new_null_provider(); -- Start with the default usermanager provider
56         local auth_provider = config.get(host, "core", "authentication") or default_provider;
57         if auth_provider ~= "null" then
58                 modulemanager.load(host, "auth_"..auth_provider);
59         end
60 end;
61 prosody.events.add_handler("host-activated", initialize_host, 100);
62 prosody.events.add_handler("component-activated", initialize_host, 100);
63
64 function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
65
66 function test_password(username, host, password)
67         return hosts[host].users.test_password(username, password);
68 end
69
70 function get_password(username, host)
71         return hosts[host].users.get_password(username);
72 end
73
74 function set_password(username, password, host)
75         return hosts[host].users.set_password(username, password);
76 end
77
78 function user_exists(username, host)
79         return hosts[host].users.user_exists(username);
80 end
81
82 function create_user(username, password, host)
83         return hosts[host].users.create_user(username, password);
84 end
85
86 function get_sasl_handler(host)
87         return hosts[host].users.get_sasl_handler();
88 end
89
90 function get_provider(host)
91         return hosts[host].users;
92 end
93
94 function is_admin(jid, host)
95         local is_admin;
96         jid = jid_bare(jid);
97         host = host or "*";
98         
99         local host_admins = config.get(host, "core", "admins");
100         local global_admins = config.get("*", "core", "admins");
101         
102         if host_admins and host_admins ~= global_admins then
103                 if type(host_admins) == "table" then
104                         for _,admin in ipairs(host_admins) do
105                                 if admin == jid then
106                                         is_admin = true;
107                                         break;
108                                 end
109                         end
110                 elseif host_admins then
111                         log("error", "Option 'admins' for host '%s' is not a list", host);
112                 end
113         end
114         
115         if not is_admin and global_admins then
116                 if type(global_admins) == "table" then
117                         for _,admin in ipairs(global_admins) do
118                                 if admin == jid then
119                                         is_admin = true;
120                                         break;
121                                 end
122                         end
123                 elseif global_admins then
124                         log("error", "Global option 'admins' is not a list");
125                 end
126         end
127         
128         -- Still not an admin, check with auth provider
129         if not is_admin and host ~= "*" and hosts[host].users.is_admin then
130                 is_admin = hosts[host].users.is_admin(jid);
131         end
132         return is_admin or false;
133 end
134
135 return _M;