3 * Name: LDAP Authenticate
4 * Description: Authenticate a user against an LDAP directory
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
13 * Module: LDAP Authenticate
15 * Authenticate a user against an LDAP directory
16 * Useful for Windows Active Directory and other LDAP-based organisations
17 * to maintain a single password across the organisation.
19 * Optionally authenticates only if a member of a given group in the directory.
21 * By default, the person must have registered with Friendica using the normal registration
22 * procedures in order to have a Friendica user record, contact, and profile.
23 * However, it's possible with an option to automate the creation of a Friendica basic account.
25 * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
26 * ldap.conf file to the signing cert for your LDAP server.
28 * The configuration options for this module may be set in the .htconfig.php file
31 * // ldap hostname server - required
32 * $a->config['ldapauth']['ldap_server'] = 'host.example.com';
33 * // dn to search users - required
34 * $a->config['ldapauth']['ldap_searchdn'] = 'ou=users,dc=example,dc=com';
35 * // attribute to find username - required
36 * $a->config['ldapauth']['ldap_userattr'] = 'uid';
38 * // admin dn - optional - only if ldap server dont have anonymous access
39 * $a->config['ldapauth']['ldap_binddn'] = 'cn=admin,dc=example,dc=com';
40 * // admin password - optional - only if ldap server dont have anonymous access
41 * $a->config['ldapauth']['ldap_bindpw'] = 'password';
43 * // for create Friendica account if user exist in ldap
44 * // required an email and a simple (beautiful) nickname on user ldap object
45 * // active account creation - optional - default none
46 * $a->config['ldapauth']['ldap_autocreateaccount'] = 'true';
47 * // attribute to get email - optional - default : 'mail'
48 * $a->config['ldapauth']['ldap_autocreateaccount_emailattribute'] = 'mail';
49 * // attribute to get nickname - optional - default : 'givenName'
50 * $a->config['ldapauth']['ldap_autocreateaccount_nameattribute'] = 'cn';
54 use Friendica\Core\Addon;
55 use Friendica\Core\Config;
56 use Friendica\Model\User;
58 function ldapauth_install()
60 Addon::registerHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
63 function ldapauth_uninstall()
65 Addon::unregisterHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
69 function ldapauth_hook_authenticate($a,&$b) {
70 if(ldapauth_authenticate($b['username'],$b['password'])) {
71 $results = get_existing_account($b['username']);
72 if(! empty($results)){
73 $b['user_record'] = $results[0];
74 $b['authenticated'] = 1;
80 function ldapauth_authenticate($username,$password) {
82 $ldap_server = get_config('ldapauth','ldap_server');
83 $ldap_binddn = get_config('ldapauth','ldap_binddn');
84 $ldap_bindpw = get_config('ldapauth','ldap_bindpw');
85 $ldap_searchdn = get_config('ldapauth','ldap_searchdn');
86 $ldap_userattr = get_config('ldapauth','ldap_userattr');
87 $ldap_group = get_config('ldapauth','ldap_group');
88 $ldap_autocreateaccount = get_config('ldapauth','ldap_autocreateaccount');
89 $ldap_autocreateaccount_emailattribute = get_config('ldapauth','ldap_autocreateaccount_emailattribute');
90 $ldap_autocreateaccount_nameattribute = get_config('ldapauth','ldap_autocreateaccount_nameattribute');
92 if(! ((strlen($password))
93 && (function_exists('ldap_connect'))
94 && (strlen($ldap_server)))) {
95 logger("ldapauth: not configured or missing php-ldap module");
99 $connect = @ldap_connect($ldap_server);
101 if($connect === false) {
102 logger("ldapauth: could not connect to $ldap_server");
106 @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION,3);
107 @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
108 if((@ldap_bind($connect,$ldap_binddn,$ldap_bindpw)) === false) {
109 logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
113 $res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username);
116 logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
120 $id = @ldap_first_entry($connect,$res);
126 $dn = @ldap_get_dn($connect,$id);
128 if(! @ldap_bind($connect,$dn,$password))
133 if($ldap_autocreateaccount == "true"){
134 if(! strlen($ldap_autocreateaccount_emailattribute))
135 $ldap_autocreateaccount_emailattribute = "mail";
136 if(! strlen($ldap_autocreateaccount_nameattribute))
137 $ldap_autocreateaccount_nameattribute = "givenName";
138 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
139 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
142 if(! strlen($ldap_group)){
143 ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
147 $r = @ldap_compare($connect,$ldap_group,'member',$dn);
149 $err = @ldap_error($connect);
150 $eno = @ldap_errno($connect);
151 @ldap_close($connect);
154 logger("ldapauth: access control group Does Not Exist");
157 elseif ($eno === 16) {
158 logger('ldapauth: membership attribute does not exist in access control group');
162 logger('ldapauth: error: ' . $err);
166 elseif ($r === false) {
167 @ldap_close($connect);
171 ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
175 function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) {
176 if($ldap_autocreateaccount == "true"){
177 $results = get_existing_account($username);
179 if (strlen($email) > 0 && strlen($name) > 0){
180 $arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
181 $result = create_user($arr);
182 if ($result['success']){
183 logger("ldapauth: account " . $username . " created");
185 logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
188 logger("ldapauth: unable to create account, no email or nickname found");
194 function get_existing_account($username){
195 return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username);