1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Collection of functions that don't belong in server object.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Modify an LDAP Entry.
|
10
|
*
|
11
|
* @deprecated This has been deprecated in favor of
|
12
|
* `$ldap_server->modifyLdapEntry($dn, $attributes)` which handles empty
|
13
|
* attributes better.
|
14
|
*/
|
15
|
function ldap_user_modify($dn, $attributes, $ldap_server) {
|
16
|
$result = ldap_modify($ldap_server->connection, $dn, $attributes);
|
17
|
if (!$result) {
|
18
|
$error = "LDAP Server ldap_modify(%dn) in ldap_user_modify(), LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
|
19
|
$tokens = ['%dn' => $dn, '%ldap_errno' => ldap_errno($ldap_server->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($ldap_server->connection))];
|
20
|
watchdog('ldap_servers', $error, $tokens, WATCHDOG_ERROR);
|
21
|
}
|
22
|
return $result;
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Modify a password.
|
27
|
*/
|
28
|
function ldap_password_modify($userdn, $new_password, $ldap_server) {
|
29
|
|
30
|
$new_password = "\"" . $new_password . "\"";
|
31
|
$len = drupal_strlen($new_password);
|
32
|
$new_pass = NULL;
|
33
|
for ($i = 0; $i < $len; $i++) {
|
34
|
$new_pass .= "{$new_password[$i]}\000";
|
35
|
}
|
36
|
|
37
|
$status = ldap_mod_replace($ldap_server->connection, $userdn, ['unicodePwd' => $new_pass]);
|
38
|
if (!$status) {
|
39
|
watchdog(
|
40
|
'ldap_servers',
|
41
|
'Error: password_modify() failed to modify ldap password w/ base DN "!dn"',
|
42
|
['!dn' => $userdn],
|
43
|
WATCHDOG_ERROR
|
44
|
);
|
45
|
}
|
46
|
|
47
|
return $status;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Converts a password to the format that Active Directory supports (for the
|
52
|
* purpose of changing or setting). Note that AD needs the field to be called
|
53
|
* unicodePwd (as opposed to userPassword)
|
54
|
*
|
55
|
* @param string $password
|
56
|
* The password that is being formatted for Active Directory unicodePwd field.
|
57
|
*
|
58
|
* @return string
|
59
|
* $password surrounded with quotes and in UTF-16LE encoding
|
60
|
*/
|
61
|
function ldap_servers_convert_password_for_active_directory_unicodePwd($password) {
|
62
|
// This function can be called with $attributes['unicodePwd'] as an array.
|
63
|
if (!is_array($password)) {
|
64
|
return mb_convert_encoding("\"{$password}\"", "UTF-16LE");
|
65
|
}
|
66
|
else {
|
67
|
// Presumably there is no use case for there being more than one password in
|
68
|
// the $attributes array, hence it will be at index 0 and we return in kind.
|
69
|
return [mb_convert_encoding("\"{$password[0]}\"", "UTF-16LE")];
|
70
|
}
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* This attempts to find bad dns, but should only be used as warnings
|
75
|
* as the ldap spec allows for any old character to be escaped and ldap
|
76
|
* implementations may not follow the spec.
|
77
|
*
|
78
|
* Http://www.ietf.org/rfc/rfc2253.txt.
|
79
|
*/
|
80
|
function ldap_baddn($dn, $dn_name) {
|
81
|
$result = [];
|
82
|
$valid_attr_name = '[_a-zA-Z\d\s]';
|
83
|
$valid_attr_values = '[_\-a-zA-Z\d\s]';
|
84
|
$regex = '/^(' . $valid_attr_name . '*\=' . $valid_attr_values . '*[,]{1})*(' . $valid_attr_name . '*\=' . $valid_attr_values . '*){1}$/';
|
85
|
$match = (preg_match($regex, $dn)) ? TRUE : FALSE;
|
86
|
$result['boolean'] = $match;
|
87
|
if (!$match) {
|
88
|
$tokens = ['%dn' => htmlspecialchars($dn), '%dn_name' => $dn_name];
|
89
|
$result['text'] = t('Possible invalid format for:', $tokens)
|
90
|
. '<em>' . $tokens['%dn'] . '</em>.<br/> ' .
|
91
|
t('The format may be correct for your ldap, but please double check.', $tokens);
|
92
|
}
|
93
|
return $result;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* This attempts to find bad dns, but should only be used as warningswe
|
98
|
* as the ldap spec allows for any old character to be escaped and ldap
|
99
|
* implementations may not follow the spec.
|
100
|
*
|
101
|
* Http://www.ietf.org/rfc/rfc2253.txt.
|
102
|
*/
|
103
|
function ldap_badattr($attr, $attr_name) {
|
104
|
$result = [];
|
105
|
$valid_attr_name = '[_a-zA-Z\d\s]';
|
106
|
$regex = '/^(' . $valid_attr_name . '){1,}$/';
|
107
|
$match = (preg_match($regex, $attr)) ? TRUE : FALSE;
|
108
|
$result['boolean'] = $match;
|
109
|
if (!$match) {
|
110
|
$tokens = ['%attr' => htmlspecialchars($attr), '%attr_name' => $attr_name];
|
111
|
$result['text'] = t('Possible invalid format for %attr_name:', $tokens) . ' <code><em>' . $tokens['%attr']
|
112
|
. '</em></code><br/>' . t('The format may be correct for your ldap, but please double check.', $tokens);
|
113
|
}
|
114
|
return $result;
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Get array of required attributes for an ldap query.
|
119
|
*
|
120
|
* @param string $sid
|
121
|
* server id or ldap server object being used.
|
122
|
* @param string $direction
|
123
|
* LDAP_USER_PROV_DIRECTION_* constant.
|
124
|
* @param string $ldap_context
|
125
|
* @param bool $reset
|
126
|
* cache.
|
127
|
*/
|
128
|
function ldap_servers_attributes_needed($sid, $ldap_context = 'all', $reset = TRUE) {
|
129
|
|
130
|
static $attributes;
|
131
|
$sid = is_object($sid) ? $sid->sid : $sid;
|
132
|
$static_cache_id = ($sid) ? $ldap_context . '__' . $sid : $ldap_context;
|
133
|
if (!is_array($attributes)) {
|
134
|
$attributes = [];
|
135
|
}
|
136
|
if (!isset($attributes[$static_cache_id]) || $reset) {
|
137
|
$params = [
|
138
|
'sid' => $sid,
|
139
|
'ldap_context' => $ldap_context,
|
140
|
];
|
141
|
drupal_alter('ldap_attributes_needed', $attributes[$static_cache_id], $params);
|
142
|
}
|
143
|
$return = $attributes[$static_cache_id];
|
144
|
|
145
|
return $return;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Function to massage (change case, escape, unescape) ldap attribute names
|
150
|
* and values. The primary purpose of this is to articulate and ensure
|
151
|
* consistency across ldap modules.
|
152
|
*
|
153
|
* @param mixed $value
|
154
|
* to be massaged.
|
155
|
* @param string $value_type
|
156
|
* = 'attr_name' or 'attr_value;.
|
157
|
* @param enum $context
|
158
|
* ...see LDAP_SERVER_MASSAGE_* constants
|
159
|
*
|
160
|
* .e.g. ldap_server_massage_text($value, 'attr_value',
|
161
|
* LDAP_SERVER_MASSAGE_QUERY_LDAP)
|
162
|
*
|
163
|
* @return array|mixed|string
|
164
|
*/
|
165
|
function ldap_server_massage_text($value, $value_type, $context) {
|
166
|
|
167
|
$scalar = is_scalar($value);
|
168
|
|
169
|
if ($value_type == 'attr_value') {
|
170
|
|
171
|
if ($context == LDAP_SERVER_MASSAGE_QUERY_LDAP) {
|
172
|
$value = ldap_pear_escape_filter_value($value);
|
173
|
}
|
174
|
elseif ($context == LDAP_SERVER_MASSAGE_STORE_LDAP) {
|
175
|
$value = ldap_pear_escape_dn_value($value);
|
176
|
}
|
177
|
|
178
|
switch ($context) {
|
179
|
|
180
|
case LDAP_SERVER_MASSAGE_DISPLAY:
|
181
|
case LDAP_SERVER_MASSAGE_TOKEN_REPLACE:
|
182
|
|
183
|
case LDAP_SERVER_MASSAGE_QUERY_LDAP:
|
184
|
case LDAP_SERVER_MASSAGE_QUERY_DB:
|
185
|
case LDAP_SERVER_MASSAGE_QUERY_ARRAY:
|
186
|
case LDAP_SERVER_MASSAGE_QUERY_PROPERTY:
|
187
|
|
188
|
case LDAP_SERVER_MASSAGE_STORE_LDAP:
|
189
|
case LDAP_SERVER_MASSAGE_STORE_DB:
|
190
|
case LDAP_SERVER_MASSAGE_STORE_ARRAY:
|
191
|
case LDAP_SERVER_MASSAGE_STORE_PROPERTY:
|
192
|
|
193
|
break;
|
194
|
|
195
|
}
|
196
|
}
|
197
|
// attr_name.
|
198
|
elseif ($value_type == 'attr_name') {
|
199
|
switch ($context) {
|
200
|
|
201
|
case LDAP_SERVER_MASSAGE_DISPLAY:
|
202
|
break;
|
203
|
|
204
|
case LDAP_SERVER_MASSAGE_TOKEN_REPLACE:
|
205
|
|
206
|
case LDAP_SERVER_MASSAGE_QUERY_LDAP:
|
207
|
case LDAP_SERVER_MASSAGE_QUERY_DB:
|
208
|
case LDAP_SERVER_MASSAGE_QUERY_ARRAY:
|
209
|
case LDAP_SERVER_MASSAGE_QUERY_PROPERTY:
|
210
|
|
211
|
case LDAP_SERVER_MASSAGE_STORE_LDAP:
|
212
|
case LDAP_SERVER_MASSAGE_STORE_DB:
|
213
|
case LDAP_SERVER_MASSAGE_STORE_ARRAY:
|
214
|
case LDAP_SERVER_MASSAGE_STORE_PROPERTY:
|
215
|
if ($scalar) {
|
216
|
$value = drupal_strtolower($value);
|
217
|
}
|
218
|
elseif (is_array($value)) {
|
219
|
foreach ($value as $i => $val) {
|
220
|
$value[$i] = drupal_strtolower($val);
|
221
|
}
|
222
|
}
|
223
|
else {
|
224
|
// Neither scalar nor array $value.
|
225
|
}
|
226
|
break;
|
227
|
|
228
|
}
|
229
|
}
|
230
|
|
231
|
return $value;
|
232
|
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* From pear net_ldap2-2.0.11.
|
237
|
*
|
238
|
* Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
|
239
|
*
|
240
|
* Any control characters with an ACII code < 32 as well as the characters with special meaning in
|
241
|
* LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
|
242
|
* backslash followed by two hex digits representing the hexadecimal value of the character.
|
243
|
*
|
244
|
* @param array $values
|
245
|
* Array of values to escape.
|
246
|
*
|
247
|
* @static
|
248
|
*
|
249
|
* @return array Array $values, but escaped
|
250
|
*/
|
251
|
function ldap_pear_escape_filter_value($values = []) {
|
252
|
// Parameter validation.
|
253
|
$is_scalar = is_scalar($values);
|
254
|
if ($is_scalar) {
|
255
|
$values = [$values];
|
256
|
}
|
257
|
if ($values === NULL) {
|
258
|
return NULL;
|
259
|
}
|
260
|
|
261
|
foreach ($values as $key => $val) {
|
262
|
// Escaping of filter meta characters.
|
263
|
$val = str_replace('\\', '\5c', $val);
|
264
|
$val = str_replace('*', '\2a', $val);
|
265
|
$val = str_replace('(', '\28', $val);
|
266
|
$val = str_replace(')', '\29', $val);
|
267
|
|
268
|
// ASCII < 32 escaping.
|
269
|
$val = ldap_pear_asc2hex32($val);
|
270
|
|
271
|
if (NULL === $val) {
|
272
|
$val = '\0';
|
273
|
} // apply escaped "null" if string is empty
|
274
|
|
275
|
$values[$key] = $val;
|
276
|
}
|
277
|
|
278
|
return ($is_scalar) ? $values[0] : $values;
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Undoes the conversion done by {@link escape_filter_value()}.
|
283
|
*
|
284
|
* Converts any sequences of a backslash followed by two hex digits into the corresponding character.
|
285
|
*
|
286
|
* @param array $values
|
287
|
* Array of values to escape.
|
288
|
*
|
289
|
* @static
|
290
|
*
|
291
|
* @return array Array $values, but unescaped
|
292
|
*/
|
293
|
function ldap_pear_unescape_filter_value($values = []) {
|
294
|
// Parameter validation.
|
295
|
$is_scalar = is_scalar($values);
|
296
|
if (!is_array($values)) {
|
297
|
$values = [$values];
|
298
|
}
|
299
|
|
300
|
foreach ($values as $key => $value) {
|
301
|
// Translate hex code into ascii.
|
302
|
$values[$key] = ldap_pear_hex2asc($value);
|
303
|
}
|
304
|
|
305
|
return ($is_scalar) ? $values[0] : $values;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Escapes a DN value according to RFC 2253.
|
310
|
*
|
311
|
* Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs.
|
312
|
* The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252
|
313
|
* are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair.
|
314
|
* Finally all leading and trailing spaces are converted to sequences of \20.
|
315
|
*
|
316
|
* @param array $values
|
317
|
* An array containing the DN values that should be escaped.
|
318
|
*
|
319
|
* @static
|
320
|
*
|
321
|
* @return array The array $values, but escaped
|
322
|
*/
|
323
|
function ldap_pear_escape_dn_value($values = []) {
|
324
|
// Parameter validation.
|
325
|
$is_scalar = is_scalar($values);
|
326
|
|
327
|
if (!is_array($values)) {
|
328
|
$values = [$values];
|
329
|
}
|
330
|
|
331
|
foreach ($values as $key => $val) {
|
332
|
// Escaping of filter meta characters.
|
333
|
$val = str_replace('\\', '\\\\', $val);
|
334
|
$val = str_replace(',', '\,', $val);
|
335
|
$val = str_replace('+', '\+', $val);
|
336
|
$val = str_replace('"', '\"', $val);
|
337
|
$val = str_replace('<', '\<', $val);
|
338
|
$val = str_replace('>', '\>', $val);
|
339
|
$val = str_replace(';', '\;', $val);
|
340
|
$val = str_replace('#', '\#', $val);
|
341
|
$val = str_replace('=', '\=', $val);
|
342
|
|
343
|
// ASCII < 32 escaping.
|
344
|
$val = ldap_pear_asc2hex32($val);
|
345
|
|
346
|
// Convert all leading and trailing spaces to sequences of \20.
|
347
|
if (preg_match('/^(\s*)(.+?)(\s*)$/', $val, $matches)) {
|
348
|
$val = $matches[2];
|
349
|
for ($i = 0; $i < strlen($matches[1]); $i++) {
|
350
|
$val = '\20' . $val;
|
351
|
}
|
352
|
for ($i = 0; $i < strlen($matches[3]); $i++) {
|
353
|
$val = $val . '\20';
|
354
|
}
|
355
|
}
|
356
|
|
357
|
// Apply escaped "null" if string is empty.
|
358
|
if (NULL === $val) {
|
359
|
$val = '\0';
|
360
|
}
|
361
|
|
362
|
$values[$key] = $val;
|
363
|
}
|
364
|
|
365
|
return ($is_scalar) ? $values[0] : $values;
|
366
|
}
|
367
|
|
368
|
/**
|
369
|
* Undoes the conversion done by escape_dn_value().
|
370
|
*
|
371
|
* Any escape sequence starting with a baskslash - hexpair or special character -
|
372
|
* will be transformed back to the corresponding character.
|
373
|
*
|
374
|
* @param array $values
|
375
|
* Array of DN Values.
|
376
|
*
|
377
|
* @return array Same as $values, but unescaped
|
378
|
*
|
379
|
* @static
|
380
|
*/
|
381
|
function ldap_pear_unescape_dn_value($values = []) {
|
382
|
$is_scalar = is_scalar($values);
|
383
|
|
384
|
// Parameter validation.
|
385
|
if (!is_array($values)) {
|
386
|
$values = [$values];
|
387
|
}
|
388
|
|
389
|
foreach ($values as $key => $val) {
|
390
|
// Strip slashes from special chars.
|
391
|
$val = str_replace('\\\\', '\\', $val);
|
392
|
$val = str_replace('\,', ',', $val);
|
393
|
$val = str_replace('\+', '+', $val);
|
394
|
$val = str_replace('\"', '"', $val);
|
395
|
$val = str_replace('\<', '<', $val);
|
396
|
$val = str_replace('\>', '>', $val);
|
397
|
$val = str_replace('\;', ';', $val);
|
398
|
$val = str_replace('\#', '#', $val);
|
399
|
$val = str_replace('\=', '=', $val);
|
400
|
|
401
|
// Translate hex code into ascii.
|
402
|
$values[$key] = ldap_pear_hex2asc($val);
|
403
|
}
|
404
|
|
405
|
return ($is_scalar) ? $values[0] : $values;
|
406
|
}
|
407
|
|
408
|
/**
|
409
|
* Converts all Hex expressions ("\HEX") to their original ASCII characters.
|
410
|
*
|
411
|
* @param string $string
|
412
|
* String to convert.
|
413
|
*
|
414
|
* @static
|
415
|
* @author beni@php.net, heavily based on work from DavidSmith@byu.net
|
416
|
*
|
417
|
* @return string
|
418
|
*/
|
419
|
function ldap_pear_hex2asc($string) {
|
420
|
$string = preg_replace_callback(
|
421
|
"/\\\([0-9A-Fa-f]{2})/",
|
422
|
function ($m) {
|
423
|
return chr(hexdec($m[1]));
|
424
|
},
|
425
|
$string
|
426
|
);
|
427
|
return $string;
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* Converts all ASCII chars < 32 to "\HEX".
|
432
|
*
|
433
|
* @param string $string
|
434
|
* String to convert.
|
435
|
*
|
436
|
* @static
|
437
|
*
|
438
|
* @return string
|
439
|
*/
|
440
|
function ldap_pear_asc2hex32($string) {
|
441
|
for ($i = 0; $i < strlen($string); $i++) {
|
442
|
$char = substr($string, $i, 1);
|
443
|
if (ord($char) < 32) {
|
444
|
$hex = dechex(ord($char));
|
445
|
if (strlen($hex) == 1) {
|
446
|
$hex = '0' . $hex;
|
447
|
}
|
448
|
$string = str_replace($char, '\\' . $hex, $string);
|
449
|
}
|
450
|
}
|
451
|
return $string;
|
452
|
}
|