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;
60 use Friendica\Model\User;
61 use Friendica\Util\ConfigFileLoader;
63 function ldapauth_install()
65 Hook::register('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
66 Hook::register('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
69 function ldapauth_uninstall()
71 Hook::unregister('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
72 Hook::unregister('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
75 function ldapauth_load_config(\Friendica\App $a, ConfigFileLoader $loader)
77 $a->getConfigCache()->load($loader->loadAddonConfig('ldapauth'));
80 function ldapauth_hook_authenticate($a, &$b)
82 if (ldapauth_authenticate($b['username'], $b['password'])) {
83 $results = get_existing_account($b['username']);
84 if (!empty($results)) {
85 $b['user_record'] = $results[0];
86 $b['authenticated'] = 1;
92 function ldapauth_authenticate($username, $password)
94 $ldap_server = DI::config()->get('ldapauth', 'ldap_server');
95 $ldap_binddn = DI::config()->get('ldapauth', 'ldap_binddn');
96 $ldap_bindpw = DI::config()->get('ldapauth', 'ldap_bindpw');
97 $ldap_searchdn = DI::config()->get('ldapauth', 'ldap_searchdn');
98 $ldap_userattr = DI::config()->get('ldapauth', 'ldap_userattr');
99 $ldap_group = DI::config()->get('ldapauth', 'ldap_group');
100 $ldap_autocreateaccount = DI::config()->get('ldapauth', 'ldap_autocreateaccount');
101 $ldap_autocreateaccount_emailattribute = DI::config()->get('ldapauth', 'ldap_autocreateaccount_emailattribute');
102 $ldap_autocreateaccount_nameattribute = DI::config()->get('ldapauth', 'ldap_autocreateaccount_nameattribute');
104 if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
105 Logger::log("ldapauth: not configured or missing php-ldap module");
109 $connect = @ldap_connect($ldap_server);
111 if ($connect === false) {
112 Logger::log("ldapauth: could not connect to $ldap_server");
116 @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
117 @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
118 if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
119 Logger::log("ldapauth: could not bind $ldap_server as $ldap_binddn");
123 $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
126 Logger::log("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
130 $id = @ldap_first_entry($connect, $res);
136 $dn = @ldap_get_dn($connect, $id);
138 if (!@ldap_bind($connect, $dn, $password)) {
144 if ($ldap_autocreateaccount == "true") {
145 if (!strlen($ldap_autocreateaccount_emailattribute)) {
146 $ldap_autocreateaccount_emailattribute = "mail";
148 if (!strlen($ldap_autocreateaccount_nameattribute)) {
149 $ldap_autocreateaccount_nameattribute = "givenName";
151 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
152 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
155 if (!strlen($ldap_group)) {
156 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
160 $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
162 $err = @ldap_error($connect);
163 $eno = @ldap_errno($connect);
164 @ldap_close($connect);
167 Logger::log("ldapauth: access control group Does Not Exist");
169 } elseif ($eno === 16) {
170 Logger::log('ldapauth: membership attribute does not exist in access control group');
173 Logger::log('ldapauth: error: ' . $err);
176 } elseif ($r === false) {
177 @ldap_close($connect);
181 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
185 function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
187 if ($ldap_autocreateaccount == "true") {
188 $results = get_existing_account($username);
189 if (empty($results)) {
190 if (strlen($email) > 0 && strlen($name) > 0) {
191 $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
195 Logger::log("ldapauth: account " . $username . " created");
196 } catch (Exception $ex) {
197 Logger::log("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
200 Logger::log("ldapauth: unable to create account, no email or nickname found");
206 function get_existing_account($username)
208 return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);