5e2e41fcd31c72a53afc2de9d6e6f901d38188ec
[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 modulemanager = require "core.modulemanager";
10 local log = require "util.logger".init("usermanager");
11 local type = type;
12 local ipairs = ipairs;
13 local jid_bare = require "util.jid".bare;
14 local config = require "core.configmanager";
15 local hosts = hosts;
16 local sasl_new = require "util.sasl".new;
17
18 local prosody = _G.prosody;
19
20 local setmetatable = setmetatable;
21
22 local default_provider = "internal_plain";
23
24 module "usermanager"
25
26 function new_null_provider()
27         local function dummy() return nil, "method not implemented"; end;
28         local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
29         return setmetatable({name = "null", get_sasl_handler = dummy_get_sasl_handler}, {
30                 __index = function(self, method) return dummy; end
31         });
32 end
33
34 local provider_mt = { __index = new_null_provider() };
35
36 function initialize_host(host)
37         local host_session = hosts[host];
38         if host_session.type ~= "local" then return; end
39         
40         host_session.events.add_handler("item-added/auth-provider", function (event)
41                 local provider = event.item;
42                 local auth_provider = config.get(host, "core", "authentication") or default_provider;
43                 if config.get(host, "core", "anonymous_login") then auth_provider = "anonymous"; end -- COMPAT 0.7
44                 if provider.name == auth_provider then
45                         host_session.users = setmetatable(provider, provider_mt);
46                 end
47                 if host_session.users ~= nil and host_session.users.name ~= nil then
48                         log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
49                 end
50         end);
51         host_session.events.add_handler("item-removed/auth-provider", function (event)
52                 local provider = event.item;
53                 if host_session.users == provider then
54                         host_session.users = new_null_provider();
55                 end
56         end);
57         host_session.users = new_null_provider(); -- Start with the default usermanager provider
58         local auth_provider = config.get(host, "core", "authentication") or default_provider;
59         if config.get(host, "core", "anonymous_login") then auth_provider = "anonymous"; end -- COMPAT 0.7
60         if auth_provider ~= "null" then
61                 modulemanager.load(host, "auth_"..auth_provider);
62         end
63 end;
64 prosody.events.add_handler("host-activated", initialize_host, 100);
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 delete_user(username, host)
87         return hosts[host].users.delete_user(username);
88 end
89
90 function get_sasl_handler(host)
91         return hosts[host].users.get_sasl_handler();
92 end
93
94 function get_provider(host)
95         return hosts[host].users;
96 end
97
98 function is_admin(jid, host)
99         local is_admin;
100         jid = jid_bare(jid);
101         host = host or "*";
102         
103         local host_admins = config.get(host, "core", "admins");
104         local global_admins = config.get("*", "core", "admins");
105         
106         if host_admins and host_admins ~= global_admins then
107                 if type(host_admins) == "table" then
108                         for _,admin in ipairs(host_admins) do
109                                 if admin == jid then
110                                         is_admin = true;
111                                         break;
112                                 end
113                         end
114                 elseif host_admins then
115                         log("error", "Option 'admins' for host '%s' is not a list", host);
116                 end
117         end
118         
119         if not is_admin and global_admins then
120                 if type(global_admins) == "table" then
121                         for _,admin in ipairs(global_admins) do
122                                 if admin == jid then
123                                         is_admin = true;
124                                         break;
125                                 end
126                         end
127                 elseif global_admins then
128                         log("error", "Global option 'admins' is not a list");
129                 end
130         end
131         
132         -- Still not an admin, check with auth provider
133         if not is_admin and host ~= "*" and hosts[host].users and hosts[host].users.is_admin then
134                 is_admin = hosts[host].users.is_admin(jid);
135         end
136         return is_admin or false;
137 end
138
139 return _M;