Annotate

plugins/mod_auth_ldap.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 13224:71c28b36923f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11837
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_auth_ldap
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
3 local new_sasl = require "prosody.util.sasl".new;
11837
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local lualdap = require "lualdap";
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local function ldap_filter_escape(s)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 return (s:gsub("[*()\\%z]", function(c) return ("\\%02x"):format(c:byte()) end));
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- Config options
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local ldap_server = module:get_option_string("ldap_server", "localhost");
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local ldap_rootdn = module:get_option_string("ldap_rootdn", "");
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local ldap_password = module:get_option_string("ldap_password", "");
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local ldap_tls = module:get_option_boolean("ldap_tls");
13224
71c28b36923f mod_auth_ldap: Use enum option method
Kim Alvefur <zash@zash.se>
parents: 12977
diff changeset
15 local ldap_scope = module:get_option_enum("ldap_scope", "subtree", "base", "onelevel");
11837
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local ldap_filter = module:get_option_string("ldap_filter", "(uid=$user)"):gsub("%%s", "$user", 1);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local ldap_base = assert(module:get_option_string("ldap_base"), "ldap_base is a required option for ldap");
13224
71c28b36923f mod_auth_ldap: Use enum option method
Kim Alvefur <zash@zash.se>
parents: 12977
diff changeset
18 local ldap_mode = module:get_option_enum("ldap_mode", "bind", "getpasswd");
11837
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local ldap_admins = module:get_option_string("ldap_admin_filter",
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:get_option_string("ldap_admins")); -- COMPAT with mistake in documentation
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local host = ldap_filter_escape(module:get_option_string("realm", module.host));
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
12642
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
23 if ldap_admins then
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
24 module:log("error", "The 'ldap_admin_filter' option has been deprecated, "..
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
25 "and will be ignored. Equivalent functionality may be added in "..
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
26 "the future if there is demand."
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
27 );
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
28 end
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 11837
diff changeset
29
11837
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 -- Initiate connection
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local ld = nil;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 module.unload = function() if ld then pcall(ld, ld.close); end end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 function ldap_do_once(method, ...)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if ld == nil then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local err;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 ld, err = lualdap.open_simple(ldap_server, ldap_rootdn, ldap_password, ldap_tls);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 if not ld then return nil, err, "reconnect"; end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 -- luacheck: ignore 411/success
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local success, iterator, invariant, initial = pcall(ld[method], ld, ...);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if not success then ld = nil; return nil, iterator, "search"; end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local success, dn, attr = pcall(iterator, invariant, initial);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if not success then ld = nil; return success, dn, "iter"; end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return dn, attr, "return";
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 function ldap_do(method, retry_count, ...)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local dn, attr, where;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 for _=1,1+retry_count do
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 dn, attr, where = ldap_do_once(method, ...);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 if dn or not(attr) then break; end -- nothing or something found
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 module:log("warn", "LDAP: %s %s (in %s)", tostring(dn), tostring(attr), where);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 -- otherwise retry
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if not dn and attr then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 module:log("error", "LDAP: %s", tostring(attr));
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 return dn, attr;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 function get_user(username)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 module:log("debug", "get_user(%q)", username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return ldap_do("search", 2, {
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 base = ldap_base;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 scope = ldap_scope;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 sizelimit = 1;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 filter = ldap_filter:gsub("%$(%a+)", {
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 user = ldap_filter_escape(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 host = host;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 });
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 });
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local provider = {};
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 function provider.create_user(username, password) -- luacheck: ignore 212
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 return nil, "Account creation not available with LDAP.";
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 function provider.user_exists(username)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 return not not get_user(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 function provider.set_password(username, password)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 local dn, attr = get_user(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 if not dn then return nil, attr end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if attr.userPassword == password then return true end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 return ldap_do("modify", 2, dn, { '=', userPassword = password });
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 if ldap_mode == "getpasswd" then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 function provider.get_password(username)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 local dn, attr = get_user(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 if dn and attr then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return attr.userPassword;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 function provider.test_password(username, password)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 return provider.get_password(username) == password;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 function provider.get_sasl_handler()
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 return new_sasl(module.host, {
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 plain = function(sasl, username) -- luacheck: ignore 212/sasl
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 local password = provider.get_password(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 if not password then return "", nil; end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return password, true;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 });
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 elseif ldap_mode == "bind" then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 local function test_password(userdn, password)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 local ok, err = lualdap.open_simple(ldap_server, userdn, password, ldap_tls);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 if not ok then
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 module:log("debug", "ldap open_simple error: %s", err);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 return not not ok;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 function provider.test_password(username, password)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 local dn = get_user(username);
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 if not dn then return end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 return test_password(dn, password)
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 function provider.get_sasl_handler()
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 return new_sasl(module.host, {
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 plain_test = function(sasl, username, password) -- luacheck: ignore 212/sasl
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 return provider.test_password(username, password), true;
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 });
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 else
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 module:log("error", "Unsupported ldap_mode %s", tostring(ldap_mode));
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141
a37bf4497280 mod_auth_ldap: Import from prosody-modules rev f52452911187
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 module:provides("auth", provider);