@@ -59,7 +59,7 @@ public function __construct()
59
59
}
60
60
61
61
// Thanks to: https://www.php.net/manual/de/function.ldap-connect.php#115662
62
- private function serviceping ($ host , $ port = 389 , $ timeout = 3 )
62
+ private function serviceping ($ host , $ port = 389 , $ timeout = 5 )
63
63
{
64
64
if ($ port === null ) {
65
65
$ port = 389 ;
@@ -70,7 +70,7 @@ private function serviceping($host, $port = 389, $timeout = 3)
70
70
try {
71
71
$ op = fsockopen ($ host , $ port , $ errno , $ errstr , $ timeout );
72
72
} catch (ErrorException $ e ) {
73
- Yii::error ('fsockopen failure! ' );
73
+ Yii::error ('fsockopen failure! ' , __METHOD__ );
74
74
return false ;
75
75
}
76
76
if (!$ op ) return false ; //DC is N/A
@@ -119,13 +119,13 @@ public function autoDetect($overrideIp = false)
119
119
public function login ($ username , $ password , $ domainKey )
120
120
{
121
121
122
- Yii::debug ('Hello! :) Trying to log you in via LDAP! ' );
122
+ Yii::debug ('Hello! :) Trying to log you in via LDAP! ' , __METHOD__ );
123
123
124
124
125
125
$ domainData = $ this ->domains [$ domainKey ];
126
126
127
127
$ ssl = isset ($ domainData ['useSSL ' ]) && $ domainData ['useSSL ' ];
128
- Yii::debug ('Use SSL here? ' . ($ ssl ? 'Yes ' : 'No ' ));
128
+ Yii::debug ('Use SSL here? ' . ($ ssl ? 'Yes ' : 'No ' ), __METHOD__ );
129
129
130
130
if ($ ssl ) {
131
131
// When using SSL, we have to set some env variables and create an ldap controlfile - otherwirse a connect with non valid certificat will fail!
@@ -140,22 +140,22 @@ public function login($username, $password, $domainKey)
140
140
if (!file_exists ($ ldaprcfile )) {
141
141
// Try to create the file
142
142
if (!@file_put_contents ($ ldaprcfile , 'TLS_REQCERT allow ' )) {
143
- Yii::error ('Cannot create required .ldaprc control file! ' );
143
+ Yii::error ('Cannot create required .ldaprc control file! ' , __METHOD__ );
144
144
return false ;
145
145
}
146
146
} else {
147
- Yii::debug ('.ldaprc file exists! ' );
147
+ Yii::debug ('.ldaprc file exists! ' , __METHOD__ );
148
148
}
149
149
150
150
putenv ('LDAPCONF= ' . $ ldaprcfile );
151
151
putenv ('LDAPTLS_REQCERT=allow ' );
152
152
putenv ('TLS_REQCERT=allow ' );
153
153
}
154
154
155
- Yii::debug ('Trying to connect to Domain # ' . $ domainKey . ' ( ' . $ domainData ['hostname ' ] . ') ' );
155
+ Yii::debug ('Trying to connect to Domain # ' . $ domainKey . ' ( ' . $ domainData ['hostname ' ] . ') ' , __METHOD__ );
156
156
157
157
if (!self ::serviceping ($ domainData ['hostname ' ], $ ssl ? 636 : null )) {
158
- Yii::error ('Connection failed! ' );
158
+ Yii::error ('Connection failed! ' , __METHOD__ );
159
159
return false ;
160
160
}
161
161
@@ -166,7 +166,7 @@ public function login($username, $password, $domainKey)
166
166
167
167
$ l = @ldap_connect ($ hostPrefix , $ port );
168
168
if (!$ l ) {
169
- Yii::warning ('Connect failed! ' . ldap_error ($ l ), ' ldapAuth ' );
169
+ Yii::warning ('Connect failed! ' . ldap_error ($ l ), __METHOD__ );
170
170
return false ;
171
171
}
172
172
@@ -176,12 +176,12 @@ public function login($username, $password, $domainKey)
176
176
177
177
$ bind_dn = strpos ($ username , '@ ' ) === false ? $ username . '@ ' . $ domainData ['name ' ] : $ username ;
178
178
179
- Yii::debug ('Trying to authenticate with DN ' . $ bind_dn );
179
+ Yii::debug ('Trying to authenticate with DN ' . $ bind_dn, __METHOD__ );
180
180
181
181
$ b = @ldap_bind ($ l , $ bind_dn , $ password );
182
182
183
183
if (!$ b ) {
184
- Yii::warning ('Bind failed! ' . ldap_error ($ l ), ' ldapAuth ' );
184
+ Yii::warning ('Bind failed! ' . ldap_error ($ l ), __METHOD__ );
185
185
return false ;
186
186
}
187
187
@@ -261,14 +261,18 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
261
261
262
262
$ return = [];
263
263
foreach ($ domains as $ domain ) {
264
- Yii::debug ($ domain , ' ldapAuth ' );
264
+ Yii::debug ($ domain , __METHOD__ );
265
265
if (!$ this ->login ($ domain ['publicSearchUser ' ], $ domain ['publicSearchUserPassword ' ], $ i )) {
266
- throw new ErrorException ('LDAP Connect or Bind error ( ' . ldap_errno ($ this ->_l ) . ' - ' . ldap_error ($ this ->_l ) . ') on ' . $ domain ['hostname ' ]);
266
+ if (empty ($ this ->_l )) {
267
+ throw new ErrorException ('LDAP Connect or Bind error on ' . $ domain ['hostname ' ]);
268
+ } else {
269
+ throw new ErrorException ('LDAP Connect or Bind error ( ' . ldap_errno ($ this ->_l ) . ' - ' . ldap_error ($ this ->_l ) . ') on ' . $ domain ['hostname ' ]);
270
+ }
267
271
}
268
272
269
273
$ searchFilter = str_replace ("%searchFor% " , addslashes ($ searchFor ), $ searchFilter );
270
274
271
- Yii::debug ('Search-Filter: ' . $ searchFilter );
275
+ Yii::debug ('Search-Filter: ' . $ searchFilter, __METHOD__ );
272
276
273
277
$ result = ldap_search ($ this ->_l , $ this ->_ldapBaseDn , $ searchFilter , $ attributes );
274
278
@@ -279,7 +283,7 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
279
283
continue ;
280
284
}
281
285
if (!isset ($ entry ['objectsid ' ])) {
282
- Yii::warning ('No objectsid! ignoring! ' );
286
+ Yii::warning ('No objectsid! ignoring! ' , __METHOD__ );
283
287
continue ;
284
288
}
285
289
$ sid = self ::SIDtoString ($ entry ['objectsid ' ])[0 ];
@@ -290,15 +294,15 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
290
294
// Check if this user is maybe already listed in the results - ifo so, determine which one is newer
291
295
foreach ($ return as $ _sid => $ _data ) {
292
296
if (!empty ($ _data ['sidhistory ' ]) && in_array ($ sid , $ _data ['sidhistory ' ])) {
293
- Yii::debug ('This user is listed in another users history - skipping ' );
297
+ Yii::debug ('This user is listed in another users history - skipping ' , __METHOD__ );
294
298
continue 2 ;
295
299
}
296
300
}
297
301
298
302
if ($ sidHistory ) {
299
303
foreach ($ sidHistory as $ item ) {
300
304
if (array_key_exists ($ item , $ return )) {
301
- Yii::debug ('User already exists with its sidhistory in results! Unsetting the old entry... ' );
305
+ Yii::debug ('User already exists with its sidhistory in results! Unsetting the old entry... ' , __METHOD__ );
302
306
unset($ return [$ item ]);
303
307
}
304
308
}
@@ -315,6 +319,10 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
315
319
}
316
320
}
317
321
$ i ++;
322
+
323
+ // Reset LDAP Link
324
+ ldap_close ($ this ->_l );
325
+ $ this ->_l = null ;
318
326
}
319
327
320
328
return empty ($ return ) ? [] : $ return ;
0 commit comments