Merge 0.10->trunk
[prosody.git] / plugins / mod_auth_cyrus.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 log = require "util.logger".init("auth_cyrus");
10
11 local usermanager_user_exists = require "core.usermanager".user_exists;
12
13 local cyrus_service_realm = module:get_option("cyrus_service_realm");
14 local cyrus_service_name = module:get_option("cyrus_service_name");
15 local cyrus_application_name = module:get_option("cyrus_application_name");
16 local require_provisioning = module:get_option("cyrus_require_provisioning") or false;
17 local host_fqdn = module:get_option("cyrus_server_fqdn");
18
19 prosody.unlock_globals(); --FIXME: Figure out why this is needed and
20                                                   -- why cyrussasl isn't caught by the sandbox
21 local cyrus_new = require "util.sasl_cyrus".new;
22 prosody.lock_globals();
23 local new_sasl = function(realm)
24         return cyrus_new(
25                 cyrus_service_realm or realm,
26                 cyrus_service_name or "xmpp",
27                 cyrus_application_name or "prosody",
28                 host_fqdn
29         );
30 end
31
32 do -- diagnostic
33         local list;
34         for mechanism in pairs(new_sasl(module.host):mechanisms()) do
35                 list = (not(list) and mechanism) or (list..", "..mechanism);
36         end
37         if not list then
38                 module:log("error", "No Cyrus SASL mechanisms available");
39         else
40                 module:log("debug", "Available Cyrus SASL mechanisms: %s", list);
41         end
42 end
43
44 local host = module.host;
45
46 -- define auth provider
47 local provider = {};
48 log("debug", "initializing default authentication provider for host '%s'", host);
49
50 function provider.test_password(username, password)
51         return nil, "Legacy auth not supported with Cyrus SASL.";
52 end
53
54 function provider.get_password(username)
55         return nil, "Passwords unavailable for Cyrus SASL.";
56 end
57
58 function provider.set_password(username, password)
59         return nil, "Passwords unavailable for Cyrus SASL.";
60 end
61
62 function provider.user_exists(username)
63         if require_provisioning then
64                 return usermanager_user_exists(username, host);
65         end
66         return true;
67 end
68
69 function provider.create_user(username, password)
70         return nil, "Account creation/modification not available with Cyrus SASL.";
71 end
72
73 function provider.get_sasl_handler()
74         local handler = new_sasl(host);
75         if require_provisioning then
76                 function handler.require_provisioning(username)
77                         return usermanager_user_exists(username, host);
78                 end
79         end
80         return handler;
81 end
82
83 module:provides("auth", provider);
84