util.sasl.*, mod_auth_*, mod_saslauth: Pass SASL handler as first parameter to SASL...
[prosody.git] / plugins / mod_auth_anonymous.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_anonymous");
10 local new_sasl = require "util.sasl".new;
11 local datamanager = require "util.datamanager";
12
13 function new_default_provider(host)
14         local provider = { name = "anonymous" };
15
16         function provider.test_password(username, password)
17                 return nil, "Password based auth not supported.";
18         end
19
20         function provider.get_password(username)
21                 return nil, "Password not available.";
22         end
23
24         function provider.set_password(username, password)
25                 return nil, "Password based auth not supported.";
26         end
27
28         function provider.user_exists(username)
29                 return nil, "Only anonymous users are supported."; -- FIXME check if anonymous user is connected?
30         end
31
32         function provider.create_user(username, password)
33                 return nil, "Account creation/modification not supported.";
34         end
35
36         function provider.get_sasl_handler()
37                 local realm = module:get_option("sasl_realm") or module.host;
38                 local anonymous_authentication_profile = {
39                         anonymous = function(sasl, username, realm)
40                                 return true; -- for normal usage you should always return true here
41                         end
42                 };
43                 return new_sasl(realm, anonymous_authentication_profile);
44         end
45
46         return provider;
47 end
48
49 local function dm_callback(username, host, datastore, data)
50         if host == module.host then
51                 return false;
52         end
53         return username, host, datastore, data;
54 end
55 local host = hosts[module.host];
56 local _saved_disallow_s2s = host.disallow_s2s;
57 function module.load()
58         _saved_disallow_s2s = host.disallow_s2s;
59         host.disallow_s2s = module:get_option("disallow_s2s") ~= false;
60         datamanager.add_callback(dm_callback);
61 end
62 function module.unload()
63         host.disallow_s2s = _saved_disallow_s2s;
64         datamanager.remove_callback(dm_callback);
65 end
66
67 module:add_item("auth-provider", new_default_provider(module.host));
68