]> git.mxchange.org Git - friendica-addons.git/blob - ldapauth/ldapauth.php
Issue 3873
[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 use Friendica\Core\Config;
58
59
60 function ldapauth_install() {
61         register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
62 }
63
64
65 function ldapauth_uninstall() {
66         unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
67 }
68
69
70 function ldapauth_hook_authenticate($a,&$b) {
71     if(ldapauth_authenticate($b['username'],$b['password'])) {
72         $results = get_existing_account($b['username']);
73         if(! empty($results)){
74             $b['user_record'] = $results[0];
75             $b['authenticated'] = 1;
76         }
77     }
78     return;
79 }
80
81 function ldapauth_authenticate($username,$password) {
82
83     $ldap_server   = Config::get('ldapauth','ldap_server');
84     $ldap_binddn   = Config::get('ldapauth','ldap_binddn');
85     $ldap_bindpw   = Config::get('ldapauth','ldap_bindpw');
86     $ldap_searchdn = Config::get('ldapauth','ldap_searchdn');
87     $ldap_userattr = Config::get('ldapauth','ldap_userattr');
88     $ldap_group    = Config::get('ldapauth','ldap_group');
89     $ldap_autocreateaccount = Config::get('ldapauth','ldap_autocreateaccount');
90     $ldap_autocreateaccount_emailattribute = Config::get('ldapauth','ldap_autocreateaccount_emailattribute');
91     $ldap_autocreateaccount_nameattribute = Config::get('ldapauth','ldap_autocreateaccount_nameattribute');
92         
93     if(! ((strlen($password))
94             && (function_exists('ldap_connect'))
95             && (strlen($ldap_server)))) {
96             logger("ldapauth: not configured or missing php-ldap module");
97             return false;
98     }
99
100     $connect = @ldap_connect($ldap_server);
101
102     if($connect === false) {
103         logger("ldapauth: could not connect to $ldap_server");
104         return false;
105     }
106
107     @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION,3);
108     @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
109     if((@ldap_bind($connect,$ldap_binddn,$ldap_bindpw)) === false) {
110         logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
111         return false;
112     }
113
114     $res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username);
115
116     if(! $res) {
117         logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
118         return false;
119     }
120
121     $id = @ldap_first_entry($connect,$res);
122
123     if(! $id) {
124         return false;
125     }
126
127     $dn = @ldap_get_dn($connect,$id);
128
129     if(! @ldap_bind($connect,$dn,$password))
130         return false;
131     
132     $emailarray = [];
133     $namearray = [];
134     if($ldap_autocreateaccount == "true"){
135         if(! strlen($ldap_autocreateaccount_emailattribute))
136             $ldap_autocreateaccount_emailattribute = "mail";
137         if(! strlen($ldap_autocreateaccount_nameattribute))
138             $ldap_autocreateaccount_nameattribute = "givenName";
139         $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
140         $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
141     }
142
143     if(! strlen($ldap_group)){
144         ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
145         return true;
146     }
147
148     $r = @ldap_compare($connect,$ldap_group,'member',$dn);
149     if ($r === -1) {
150         $err = @ldap_error($connect);
151         $eno = @ldap_errno($connect);
152         @ldap_close($connect);
153
154         if ($eno === 32) {
155             logger("ldapauth: access control group Does Not Exist");
156             return false;
157         }
158         elseif ($eno === 16) {
159             logger('ldapauth: membership attribute does not exist in access control group');
160             return false;
161         }
162         else {
163             logger('ldapauth: error: ' . $err);
164             return false;
165         }
166     }
167     elseif ($r === false) {
168         @ldap_close($connect);
169         return false;
170     }
171
172     ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
173     return true;
174 }
175
176 function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) {
177     if($ldap_autocreateaccount == "true"){
178         $results = get_existing_account($username);
179         if(empty($results)){
180             if (strlen($email) > 0 && strlen($name) > 0){
181                 $arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
182                 $result = create_user($arr);
183                 if ($result['success']){
184                     logger("ldapauth: account " . $username . " created");
185                 }else{
186                     logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
187                 }
188             }else{
189                 logger("ldapauth: unable to create account, no email or nickname found");
190             }
191         }
192     }
193 }
194
195 function get_existing_account($username){
196     return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username);
197 }