4 * Name: LDAP Authenticate
5 * Description: Authenticate a user against an LDAP directory
7 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
14 * Module: LDAP Authenticate
16 * Authenticate a user against an LDAP directory
17 * Useful for Windows Active Directory and other LDAP-based organisations
18 * to maintain a single password across the organisation.
20 * Optionally authenticates only if a member of a given group in the directory.
22 * By default, the person must have registered with Friendica using the normal registration
23 * procedures in order to have a Friendica user record, contact, and profile.
24 * However, it's possible with an option to automate the creation of a Friendica basic account.
26 * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
27 * ldap.conf file to the signing cert for your LDAP server.
29 * The configuration options for this module may be set in the config/addon.config.php file
33 * ; ldap hostname server - required
34 * ldap_server = host.example.com
35 * ; dn to search users - required
36 * ldap_searchdn = ou=users,dc=example,dc=com
37 * ; attribute to find username - required
40 * ; admin dn - optional - only if ldap server dont have anonymous access
41 * ldap_binddn = cn=admin,dc=example,dc=com
42 * ; admin password - optional - only if ldap server dont have anonymous access
43 * ldap_bindpw = password
45 * ; for create Friendica account if user exist in ldap
46 * ; required an email and a simple (beautiful) nickname on user ldap object
47 * ; active account creation - optional - default none
48 * ldap_autocreateaccount = true
49 * ; attribute to get email - optional - default : 'mail'
50 * ldap_autocreateaccount_emailattribute = mail
51 * ; attribute to get nickname - optional - default : 'givenName'
52 * ldap_autocreateaccount_nameattribute = cn
57 use Friendica\Core\Hook;
58 use Friendica\Core\Logger;
59 use Friendica\Database\DBA;
61 use Friendica\Model\User;
62 use Friendica\Util\ConfigFileLoader;
64 function ldapauth_install()
66 Hook::register('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
67 Hook::register('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
70 function ldapauth_load_config(\Friendica\App $a, ConfigFileLoader $loader)
72 $a->getConfigCache()->load($loader->loadAddonConfig('ldapauth'));
75 function ldapauth_hook_authenticate($a, &$b)
77 $user = ldapauth_authenticate($b['username'], $b['password']);
78 if (!empty($user['uid'])) {
79 $b['user_record'] = User::getById($user['uid']);
80 $b['authenticated'] = 1;
84 function ldapauth_authenticate($username, $password)
86 $ldap_server = DI::config()->get('ldapauth', 'ldap_server');
87 $ldap_binddn = DI::config()->get('ldapauth', 'ldap_binddn');
88 $ldap_bindpw = DI::config()->get('ldapauth', 'ldap_bindpw');
89 $ldap_searchdn = DI::config()->get('ldapauth', 'ldap_searchdn');
90 $ldap_userattr = DI::config()->get('ldapauth', 'ldap_userattr');
91 $ldap_group = DI::config()->get('ldapauth', 'ldap_group');
92 $ldap_autocreateaccount = DI::config()->get('ldapauth', 'ldap_autocreateaccount');
93 $ldap_autocreateaccount_emailattribute = DI::config()->get('ldapauth', 'ldap_autocreateaccount_emailattribute');
94 $ldap_autocreateaccount_nameattribute = DI::config()->get('ldapauth', 'ldap_autocreateaccount_nameattribute');
96 if (!extension_loaded('ldap') || !strlen($ldap_server)) {
97 Logger::error('Addon not configured or missing php-ldap extension', ['extension_loaded' => extension_loaded('ldap'), 'server' => $ldap_server]);
101 if (!strlen($password)) {
102 Logger::error('Empty password disallowed', ['provided_password_length' => strlen($password)]);
106 $connect = @ldap_connect($ldap_server);
107 if ($connect === false) {
108 Logger::warning('Could not connect to LDAP server', ['server' => $ldap_server]);
112 @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
113 @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
114 if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
115 Logger::warning('Could not bind to LDAP server', ['server' => $ldap_server, 'binddn' => $ldap_binddn, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
119 $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
121 Logger::notice('LDAP user not found.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
125 $id = @ldap_first_entry($connect, $res);
127 Logger::notice('Could not retrieve first LDAP entry.', ['searchdn' => $ldap_searchdn, 'userattr' => $ldap_userattr, 'username' => $username, 'errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
131 $dn = @ldap_get_dn($connect, $id);
132 if (!@ldap_bind($connect, $dn, $password)) {
133 Logger::notice('Could not authenticate LDAP user with provided password', ['errno' => ldap_errno($connect), 'error' => ldap_error($connect)]);
139 if ($ldap_autocreateaccount == "true") {
140 if (!strlen($ldap_autocreateaccount_emailattribute)) {
141 $ldap_autocreateaccount_emailattribute = "mail";
143 if (!strlen($ldap_autocreateaccount_nameattribute)) {
144 $ldap_autocreateaccount_nameattribute = "givenName";
146 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
147 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
150 if (!strlen($ldap_group)) {
151 ldap_createaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
155 $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
157 $errno = @ldap_errno($connect);
159 Logger::notice('LDAP Access Control Group does not exist', ['errno' => $errno, 'error' => ldap_error($connect)]);
160 } elseif ($errno === 16) {
161 Logger::notice('LDAP membership attribute does not exist in access control group', ['errno' => $errno, 'error' => ldap_error($connect)]);
163 Logger::notice('Unexpected LDAP error', ['errno' => $errno, 'error' => ldap_error($connect)]);
166 @ldap_close($connect);
170 if ($ldap_autocreateaccount == "true" && !DBA::exists('user', ['nickname' => $username])) {
171 return ldap_createaccount($username, $password, $emailarray[0], $namearray[0]);
175 $authentication = User::getAuthenticationInfo($username);
176 return User::getById($authentication['uid']);
177 } catch (Exception $e) {
178 Logger::notice('LDAP authentication error: ' . $e->getMessage());
183 function ldap_createaccount($username, $password, $email, $name)
185 if (!strlen($email) || !strlen($name)) {
186 Logger::notice('Could not create local user from LDAP data, no email or nickname provided');
191 $user = User::create([
193 'nickname' => $username,
195 'password' => $password,
198 Logger::info('Local user created from LDAP data', ['username' => $username, 'name' => $name]);
200 } catch (Exception $ex) {
201 Logger::error('Could not create local user from LDAP data', ['username' => $username, 'exception' => $ex->getMessage()]);