Revert to stable version 3.5.4
[friendica-addons.git] / ldapauth / ldapauth.php
1 <?php
2 /**
3  * Name: LDAP Authenticate
4  * Description: Authenticate a user against an LDAP directory
5  * Version: 1.1
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * Author: aymhce
8  */
9  
10 /**
11  * Friendica addon
12  * 
13  * Module: LDAP Authenticate
14  *
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.
18  *
19  * Optionally authenticates only if a member of a given group in the directory.
20  *
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.
24  *
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. 
27  * 
28  * The configuration options for this module may be set in the .htconfig.php file
29  * e.g.:
30  *
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';
37  *
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';
42  *
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';
51  *
52  * ...etc.
53  */
54
55 require_once('include/user.php');
56
57
58 function ldapauth_install() {
59         register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
60 }
61
62
63 function ldapauth_uninstall() {
64         unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
65 }
66
67
68 function ldapauth_hook_authenticate($a,&$b) {
69     if(ldapauth_authenticate($b['username'],$b['password'])) {
70         $results = get_existing_account($b['username']);
71         if(! empty($results)){
72             $b['user_record'] = $results[0];
73             $b['authenticated'] = 1;
74         }
75     }
76     return;
77 }
78
79 function ldapauth_authenticate($username,$password) {
80
81     $ldap_server   = get_config('ldapauth','ldap_server');
82     $ldap_binddn   = get_config('ldapauth','ldap_binddn');
83     $ldap_bindpw   = get_config('ldapauth','ldap_bindpw');
84     $ldap_searchdn = get_config('ldapauth','ldap_searchdn');
85     $ldap_userattr = get_config('ldapauth','ldap_userattr');
86     $ldap_group    = get_config('ldapauth','ldap_group');
87     $ldap_autocreateaccount = get_config('ldapauth','ldap_autocreateaccount');
88     $ldap_autocreateaccount_emailattribute = get_config('ldapauth','ldap_autocreateaccount_emailattribute');
89     $ldap_autocreateaccount_nameattribute = get_config('ldapauth','ldap_autocreateaccount_nameattribute');
90         
91     if(! ((strlen($password))
92             && (function_exists('ldap_connect'))
93             && (strlen($ldap_server)))) {
94             logger("ldapauth: not configured or missing php-ldap module");
95             return false;
96     }
97
98     $connect = @ldap_connect($ldap_server);
99
100     if($connect === false) {
101         logger("ldapauth: could not connect to $ldap_server");
102         return false;
103     }
104
105     @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION,3);
106     @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
107     if((@ldap_bind($connect,$ldap_binddn,$ldap_bindpw)) === false) {
108         logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
109         return false;
110     }
111
112     $res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username);
113
114     if(! $res) {
115         logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
116         return false;
117     }
118
119     $id = @ldap_first_entry($connect,$res);
120
121     if(! $id) {
122         return false;
123     }
124
125     $dn = @ldap_get_dn($connect,$id);
126
127     if(! @ldap_bind($connect,$dn,$password))
128         return false;
129     
130     $emailarray = [];
131     $namearray = [];
132     if($ldap_autocreateaccount == "true"){
133         if(! strlen($ldap_autocreateaccount_emailattribute))
134             $ldap_autocreateaccount_emailattribute = "mail";
135         if(! strlen($ldap_autocreateaccount_nameattribute))
136             $ldap_autocreateaccount_nameattribute = "givenName";
137         $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
138         $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
139     }
140
141     if(! strlen($ldap_group)){
142         ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
143         return true;
144     }
145
146     $r = @ldap_compare($connect,$ldap_group,'member',$dn);
147     if ($r === -1) {
148         $err = @ldap_error($connect);
149         $eno = @ldap_errno($connect);
150         @ldap_close($connect);
151
152         if ($eno === 32) {
153             logger("ldapauth: access control group Does Not Exist");
154             return false;
155         }
156         elseif ($eno === 16) {
157             logger('ldapauth: membership attribute does not exist in access control group');
158             return false;
159         }
160         else {
161             logger('ldapauth: error: ' . $err);
162             return false;
163         }
164     }
165     elseif ($r === false) {
166         @ldap_close($connect);
167         return false;
168     }
169
170     ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
171     return true;
172 }
173
174 function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) {
175     if($ldap_autocreateaccount == "true"){
176         $results = get_existing_account($username);
177         if(empty($results)){
178             if (strlen($email) > 0 && strlen($name) > 0){
179                 $arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
180                 $result = create_user($arr);
181                 if ($result['success']){
182                     logger("ldapauth: account " . $username . " created");
183                 }else{
184                     logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
185                 }
186             }else{
187                 logger("ldapauth: unable to create account, no email or nickname found");
188             }
189         }
190     }
191 }
192
193 function get_existing_account($username){
194     return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username);
195 }