-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLdap.php
515 lines (440 loc) · 19.2 KB
/
Ldap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php namespace Ltb;
class Ldap {
// php ldap instance
public $ldap = null;
// ldap connection parameters
public $ldap_url = null;
public $ldap_starttls = null;
public $ldap_binddn = null;
public $ldap_bindpw = null;
public $ldap_network_timeout = null;
public $ldap_user_base = null;
public $ldap_size_limit = null;
public $ldap_krb5ccname = null;
public $ldap_page_size = 0;
public function __construct(
$ldap_url,
$ldap_starttls,
$ldap_binddn,
$ldap_bindpw,
$ldap_network_timeout,
$ldap_user_base,
$ldap_size_limit,
$ldap_krb5ccname,
$ldap_page_size = 0
)
{
$this->ldap_url = $ldap_url;
$this->ldap_starttls = $ldap_starttls;
$this->ldap_binddn = $ldap_binddn;
$this->ldap_bindpw = $ldap_bindpw;
$this->ldap_network_timeout = $ldap_network_timeout;
$this->ldap_user_base = $ldap_user_base;
$this->ldap_size_limit = $ldap_size_limit;
$this->ldap_krb5ccname = $ldap_krb5ccname;
$this->ldap_page_size = $ldap_page_size;
}
function connect() {
# Connect to LDAP
$ldap = \Ltb\PhpLDAP::ldap_connect($this->ldap_url);
\Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
\Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if ( isset($this->ldap_network_timeout) ) {
\Ltb\PhpLDAP::ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, $this->ldap_network_timeout);
}
if ( $this->ldap_starttls && !\Ltb\PhpLDAP::ldap_start_tls($ldap) ) {
error_log("LDAP - Unable to use StartTLS");
return array(false, "ldaperror");
}
# Bind
if ( !empty($this->ldap_binddn) ) {
$bind = \Ltb\PhpLDAP::ldap_bind($ldap, $this->ldap_binddn, $this->ldap_bindpw);
} elseif ( isset($this->ldap_krb5ccname) ) {
putenv("KRB5CCNAME=".$this->ldap_krb5ccname);
$bind = \Ltb\PhpLDAP::ldap_sasl_bind($ldap, NULL, NULL, 'GSSAPI') or error_log('LDAP - GSSAPI Bind failed');
} else {
$bind = \Ltb\PhpLDAP::ldap_bind($ldap);
}
if ( !$bind ) {
$errno = \Ltb\PhpLDAP::ldap_errno($ldap);
if ( $errno ) {
error_log("LDAP - Bind error $errno (".\Ltb\PhpLDAP::ldap_error($ldap).")");
} else {
error_log("LDAP - Bind error");
}
return array(false, "ldaperror");
}
$this->ldap = $ldap;
return array($ldap, false);
}
# Function that call ldap_search, ldap_read, or ldap_list
# depending on the given scope
# Expected arguments: scope + same list as ldap_search without ldap connection
# - string $scope
# - array|string $base,
# - array|string $filter,
# - array $attributes = [],
# - int $attributes_only = 0,
# - int $sizelimit = -1,
# - int $timelimit = -1,
# - int $deref = LDAP_DEREF_NEVER,
# - ?array $controls = null
function search_with_scope(string $scope = "sub", ...$searchargs)
{
switch($scope)
{
case "sub":
return \Ltb\PhpLDAP::ldap_search($this->ldap, ...$searchargs);
case "one":
return \Ltb\PhpLDAP::ldap_list($this->ldap, ...$searchargs);
case "base":
return \Ltb\PhpLDAP::ldap_read($this->ldap, ...$searchargs);
default:
error_log("search_with_scope: invalid scope $scope");
return false;
}
}
function search($ldap_filter,$attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items, $search_scope = "sub")
{
$result = "";
$nb_entries = 0;
$entries = array();
$size_limit_reached = false;
# Connect to LDAP
$ldap_connection = $this->connect();
$result = $ldap_connection[1];
if ($this->ldap) {
foreach( $search_result_items as $item ) {
$attributes[] = $attributes_map[$item]['attribute'];
}
$attributes[] = $attributes_map[$search_result_title]['attribute'];
$attributes[] = $attributes_map[$search_result_sortby]['attribute'];
$cookie = "";
do {
$controls = [];
if($this->ldap_page_size != 0)
{
$controls = [[
'oid' => LDAP_CONTROL_PAGEDRESULTS,
'value' => [
'size' => $this->ldap_page_size,
'cookie' => $cookie]
]];
}
# Search for users
$search = $this->search_with_scope($search_scope,
$this->ldap_user_base,
$ldap_filter,
$attributes,
0,
$this->ldap_size_limit,
-1,
LDAP_DEREF_NEVER,
$controls );
if($search == false)
{
# Error during search: compute the error code and stop the process
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
break;
}
$errno = null;
$matcheddn = null;
$errmsg = null;
$referrals = null;
\Ltb\PhpLDAP::ldap_parse_result($this->ldap, $search, $errno, $matcheddn, $errmsg, $referrals, $controls);
if($errno != 0)
{
# if any error occurs, stop the search loop and treat error
break;
}
$nb_entries += \Ltb\PhpLDAP::ldap_count_entries($this->ldap, $search);
$entries = array_merge($entries, \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search));
$entries["count"] = $nb_entries;
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
} else {
$cookie = "";
}
} while (!empty($cookie) && $this->ldap_page_size != 0);
if ( $errno == 4) {
$size_limit_reached = true;
}
if ( $errno != 0 and $errno !=4 ) {
$result = "ldaperror";
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {
if ($nb_entries === 0) {
$result = "noentriesfound";
} else {
# Sort entries
if (isset($search_result_sortby)) {
$sortby = $attributes_map[$search_result_sortby]['attribute'];
$this->ldapSort($entries, $sortby);
}
unset($entries["count"]);
}
}
}
return [$this->ldap,$result,$nb_entries,$entries,$size_limit_reached];
}
function get_list($ldap_base, $ldap_filter, $key, $value) {
$return = array();
if ($this->ldap != null) {
# Search entry
$search = \Ltb\PhpLDAP::ldap_search($this->ldap, $ldap_base, $ldap_filter, array($key, $value) );
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
if ( $errno ) {
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {
$entries = \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search);
for ($i=0; $i<$entries["count"]; $i++) {
if(isset($entries[$i][$key][0])) {
$return[$entries[$i][$key][0]] = isset($entries[$i][$value][0]) ? $entries[$i][$value][0] : $entries[$i][$key][0];
}
}
}
}
return $return;
}
# if key is not found in attributes, order of entries is preserved
function ldapSort(array &$entries, $key)
{
# 'count' is an additionnal attribute of ldap entries that will be preserved
# remove it since lost by usort ( changed to integer index )
$count=$entries['count'];
unset($entries['count']);
$sort_key=$key;
usort($entries,
fn($a, $b) =>
( is_array($a) and is_array($b) ) ?
( array_key_exists($sort_key,$a) ?
( array_key_exists($sort_key,$b) ? $a[$sort_key][0] <=> $b[$sort_key][0] : 1 )
: ( array_key_exists($sort_key,$b) ? -1 : 0 ))
: 0
);
# preserve count since sorting should not change number of elements.
$entries['count']=$count;
return true;
}
# ldap_search + ldap_sort combined done at server side if possible
# if not supported fallback on client sorting.
function sorted_search($ldap_base, $ldap_filter, $attributes, $sortby, $ldap_size_limit) {
if($this->ldap == null)
return array(null, null, null);
if (isset($sortby) and $sortby)
{
$check_attribute='supportedControl';
$check = \Ltb\PhpLDAP::ldap_read($this->ldap, '', '(objectClass=*)', [$check_attribute]);
$entries = \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $check);
if (in_array(LDAP_CONTROL_SORTREQUEST, $entries[0]['supportedcontrol'],true)) {
# server side sort
$controls=[['oid' => LDAP_CONTROL_SORTREQUEST, 'value' => [['attr'=>$sortby]]]];
# if $sortby is not in $attributes ? what to do ?
$ldap_result = \Ltb\PhpLDAP::ldap_search($this->ldap, $ldap_base, $ldap_filter, $attributes, 0, $ldap_size_limit, -1, LDAP_DEREF_NEVER, $controls );
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
if ( $errno === 0 )
{
$entries=\Ltb\PhpLDAP::ldap_get_entries($this->ldap, $ldap_result);
}
}
}
if (!isset($errno))
{
$ldap_result = \Ltb\PhpLDAP::ldap_search($this->ldap, $ldap_base, $ldap_filter, $attributes, 0, $ldap_size_limit);
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
if ( $errno === 0 )
{
$entries=\Ltb\PhpLDAP::ldap_get_entries($this->ldap, $ldap_result);
$this->ldapSort($entries,$sortby);
}
else {
var_dump($errno);
}
}
return array($ldap_result,$errno,$entries);
}
/**
* Gets the value of the LDAP attribute
* @param string $dn the dn of the user
* @param string $attribute the LDAP attribute
* @return array|false the array containing attribute values
*/
function get_attribute_values($dn, $attribute) {
$search = \Ltb\PhpLDAP::ldap_read($this->ldap, $dn, "(objectClass=*)", array($attribute));
if ($search) {
return \Ltb\PhpLDAP::ldap_get_values($this->ldap, \Ltb\PhpLDAP::ldap_first_entry($this->ldap, $search), $attribute);
}
return false;
}
/*
* Gets the value of the password attribute
* @param string $dn the dn of the user
* @param string $pwdattribute the Attribute that contains the password
* @return array|false the array containing attribute values
*/
function get_password_value($dn, $pwdattribute) {
$password_values = $this->get_attribute_values($dn, $pwdattribute);
if(isset($password_values[0])) {
return $password_values[0];
}
return false;
}
/**
* Changes the password of a user while binded as the user in an Active Directory
* @param string $dn the dn of the user
* @param string $oldpassword the old password
* @param string $password the new password
* @return array [$error_code, $error_msg]
*/
function change_ad_password_as_user($dn, $oldpassword, $password): array {
# The AD password change procedure is modifying the attribute unicodePwd by
# first deleting unicodePwd with the old password and them adding it with the
# the new password
$oldpassword_hashed = \Ltb\Password::make_ad_password($oldpassword);
$modifications = array(
array(
"attrib" => "unicodePwd",
"modtype" => LDAP_MODIFY_BATCH_REMOVE,
"values" => array($oldpassword_hashed),
),
array(
"attrib" => "unicodePwd",
"modtype" => LDAP_MODIFY_BATCH_ADD,
"values" => array($password),
)
);
\Ltb\PhpLDAP::ldap_modify_batch($this->ldap, $dn, $modifications);
$error_code = \Ltb\PhpLDAP::ldap_errno($this->ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($this->ldap);
return array($error_code, $error_msg);
}
protected function get_ppolicy_error_code($ctrls) {
if (isset($ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE])) {
$value = $ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE]['value'];
if (isset($value['error'])) {
$ppolicy_error_code = $value['error'];
error_log("LDAP - Ppolicy error code: $ppolicy_error_code");
return $ppolicy_error_code;
}
}
return false;
}
/**
* Changes the Password using extended password modification
* @param string $dn the dn of the user
* @param string $oldpassword the old password
* @param string $password the new password
* @param array $userdata
* @param bool $use_ppolicy_control
* @return array 0: error_code, 1: error_msg, 2: ppolicy_error_code
*/
function change_password_with_exop($dn, $oldpassword, $password, $use_ppolicy_control): array {
$ppolicy_error_code = false;
$exop_passwd = FALSE;
if ( $use_ppolicy_control ) {
$ctrls = array();
$exop_passwd = \Ltb\PhpLDAP::ldap_exop_passwd($this->ldap, $dn, $oldpassword, $password, $ctrls);
$error_code = \Ltb\PhpLDAP::ldap_errno($this->ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($this->ldap);
if (!$exop_passwd) {
$ppolicy_error_code = self::get_ppolicy_error_code($ctrls);
}
} else {
$exop_passwd = \Ltb\PhpLDAP::ldap_exop_passwd($this->ldap, $dn, $oldpassword, $password);
$error_code = \Ltb\PhpLDAP::ldap_errno($this->ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($this->ldap);
}
return array($error_code, $error_msg, $ppolicy_error_code);
}
/**
* Changes attributes (and possibly password) using Password Policy Control
* @param string $dn the dn of the user
* @param array $userdata the array, containing the modifications
* @return array 0: error_code, 1: error_msg, 2: ppolicy_error_code
*/
function modify_attributes_using_ppolicy($dn, $userdata): array {
$error_code = "";
$error_msg = "";
$matcheddn = null;
$referrals = array();
$ctrls = array();
$ppolicy_error_code = false;
$ppolicy_replace = \Ltb\PhpLDAP::ldap_mod_replace_ext($this->ldap, $dn, $userdata, [['oid' => LDAP_CONTROL_PASSWORDPOLICYREQUEST]]);
if (\Ltb\PhpLDAP::ldap_parse_result($this->ldap, $ppolicy_replace, $error_code, $matcheddn, $error_msg, $referrals, $ctrls)) {
$ppolicy_error_code = self::get_ppolicy_error_code($ctrls);
}
return array($error_code, $error_msg, $ppolicy_error_code);
}
/**
* Changes attributes (and password)
* @param string $dn the dn of the user
* @param array $userdata the array, containing the new (hashed) password
* @return array 0: error_code, 1: error_msg
*/
function modify_attributes($dn, $userdata): array {
\Ltb\PhpLDAP::ldap_mod_replace($this->ldap, $dn, $userdata);
$error_code = \Ltb\PhpLDAP::ldap_errno($this->ldap);
$error_msg = \Ltb\PhpLDAP::ldap_error($this->ldap);
return array($error_code, $error_msg);
}
const PPOLICY_ERROR_CODE_TO_RESULT_MAPPER = [
0 => "passwordExpired",
1 => "accountLocked",
2 => "changeAfterReset",
3 => "passwordModNotAllowed",
4 => "mustSupplyOldPassword",
5 => "badquality",
6 => "tooshort",
7 => "tooyoung",
8 => "inhistory"
];
/**
* get the first value of the first attribute in the first entry found
* @param string $ldap_base: the base search
* @param string $ldap_scope: the scope for the search
* @param string $ldap_filter: the filter for searching the entry
* @param string $attribute: a list of attributes, separated by ","
* @return string: the first value of the first attribute found in the first entry
*/
function get_first_value($ldap_base, $ldap_scope, $ldap_filter, $attribute): string {
$value = "";
if ($this->ldap) {
# Search entry
$search = $this->search_with_scope($ldap_scope, $ldap_base, $ldap_filter, explode(",", $attribute));
$errno = \Ltb\PhpLDAP::ldap_errno($this->ldap);
if ( $errno ) {
error_log("LDAP - Search error $errno (".\Ltb\PhpLDAP::ldap_error($this->ldap).")");
} else {
$entry = \Ltb\PhpLDAP::ldap_get_entries($this->ldap, $search);
# Loop over attribute
foreach ( explode(",", $attribute) as $ldap_attribute ) {
if ( isset ($entry[0][$ldap_attribute]) ) {
$value = $entry[0][$ldap_attribute][0];
break;
}
}
}
}
return $value;
}
/**
* test if a DN matches filter, base and scope
* @param string $dn: entry DN
* @param string $dnAttribute: attribute name containing the DN
* @param string $filter
* @param string $base
* @param string $scope
* @return bool: true if DN matches filter, base and scope
*/
public function matchDn($dn, $dnAttribute, $filter, $base, $scope): bool {
# Build filter
$dn_escape = ldap_escape($dn, "", LDAP_ESCAPE_FILTER);
$search_filter = '(&' . $filter . '(' . $dnAttribute . '=' . $dn_escape .'))';
# Search with scope
$search = $this->search_with_scope($scope, $base, $search_filter, ['1.1']);
$count = \Ltb\PhpLDAP::ldap_count_entries($this->ldap, $search);
if ( $count == 1) { return true; }
return false;
}
}
?>