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\Config;
58 use Friendica\Core\Hook;
59 use Friendica\Core\Logger;
60 use Friendica\Model\User;
62 function ldapauth_install()
64 Hook::register('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
65 Hook::register('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
68 function ldapauth_uninstall()
70 Hook::unregister('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
71 Hook::unregister('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
74 function ldapauth_load_config(\Friendica\App $a, Config\ConfigCacheLoader $loader)
76 $a->getConfig()->loadConfigArray($loader->loadConfigFile('ldapauth'));
79 function ldapauth_hook_authenticate($a, &$b)
81 if (ldapauth_authenticate($b['username'], $b['password'])) {
82 $results = get_existing_account($b['username']);
83 if (!empty($results)) {
84 $b['user_record'] = $results[0];
85 $b['authenticated'] = 1;
91 function ldapauth_authenticate($username, $password)
93 $ldap_server = Config::get('ldapauth', 'ldap_server');
94 $ldap_binddn = Config::get('ldapauth', 'ldap_binddn');
95 $ldap_bindpw = Config::get('ldapauth', 'ldap_bindpw');
96 $ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
97 $ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
98 $ldap_group = Config::get('ldapauth', 'ldap_group');
99 $ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
100 $ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
101 $ldap_autocreateaccount_nameattribute = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
103 if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
104 Logger::log("ldapauth: not configured or missing php-ldap module");
108 $connect = @ldap_connect($ldap_server);
110 if ($connect === false) {
111 Logger::log("ldapauth: could not connect to $ldap_server");
115 @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
116 @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
117 if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
118 Logger::log("ldapauth: could not bind $ldap_server as $ldap_binddn");
122 $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
125 Logger::log("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
129 $id = @ldap_first_entry($connect, $res);
135 $dn = @ldap_get_dn($connect, $id);
137 if (!@ldap_bind($connect, $dn, $password)) {
143 if ($ldap_autocreateaccount == "true") {
144 if (!strlen($ldap_autocreateaccount_emailattribute)) {
145 $ldap_autocreateaccount_emailattribute = "mail";
147 if (!strlen($ldap_autocreateaccount_nameattribute)) {
148 $ldap_autocreateaccount_nameattribute = "givenName";
150 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
151 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
154 if (!strlen($ldap_group)) {
155 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
159 $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
161 $err = @ldap_error($connect);
162 $eno = @ldap_errno($connect);
163 @ldap_close($connect);
166 Logger::log("ldapauth: access control group Does Not Exist");
168 } elseif ($eno === 16) {
169 Logger::log('ldapauth: membership attribute does not exist in access control group');
172 Logger::log('ldapauth: error: ' . $err);
175 } elseif ($r === false) {
176 @ldap_close($connect);
180 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
184 function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
186 if ($ldap_autocreateaccount == "true") {
187 $results = get_existing_account($username);
188 if (empty($results)) {
189 if (strlen($email) > 0 && strlen($name) > 0) {
190 $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
194 Logger::log("ldapauth: account " . $username . " created");
195 } catch (Exception $ex) {
196 Logger::log("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
199 Logger::log("ldapauth: unable to create account, no email or nickname found");
205 function get_existing_account($username)
207 return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);