Skip to content

Commit c53aadb

Browse files
author
Côme Bernigaud
committed
Merge branch 'pull-request/1955'
* pull-request/1955: Add parenthesis to if statements Correctly add to the length of the final string Correctly add to the length of the final string Fix minor spacing issue Make LDAP_ESCAPE_DN compliant with RFC 4514
2 parents 5e4a5cf + b1d8260 commit c53aadb

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ext/ldap/ldap.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,7 +2826,7 @@ PHP_FUNCTION(ldap_set_rebind_proc)
28262826
/* }}} */
28272827
#endif
28282828

2829-
static zend_string* php_ldap_do_escape(const zend_bool *map, const char *value, size_t valuelen)
2829+
static zend_string* php_ldap_do_escape(const zend_bool *map, const char *value, size_t valuelen, zend_long flags)
28302830
{
28312831
char hex[] = "0123456789abcdef";
28322832
size_t i, p = 0;
@@ -2836,13 +2836,20 @@ static zend_string* php_ldap_do_escape(const zend_bool *map, const char *value,
28362836
for (i = 0; i < valuelen; i++) {
28372837
len += (map[(unsigned char) value[i]]) ? 3 : 1;
28382838
}
2839+
/* Per RFC 4514, a leading and trailing space must be escaped */
2840+
if ((flags & PHP_LDAP_ESCAPE_DN) && (value[0] == ' ')) {
2841+
len += 2;
2842+
}
2843+
if ((flags & PHP_LDAP_ESCAPE_DN) && ((valuelen > 1) && (value[valuelen - 1] == ' '))) {
2844+
len += 2;
2845+
}
28392846

28402847
ret = zend_string_alloc(len, 0);
28412848

28422849
for (i = 0; i < valuelen; i++) {
28432850
unsigned char v = (unsigned char) value[i];
28442851

2845-
if (map[v]) {
2852+
if (map[v] || ((flags & PHP_LDAP_ESCAPE_DN) && ((i == 0) || (i + 1 == valuelen)) && (v == ' '))) {
28462853
ZSTR_VAL(ret)[p++] = '\\';
28472854
ZSTR_VAL(ret)[p++] = hex[v >> 4];
28482855
ZSTR_VAL(ret)[p++] = hex[v & 0x0f];
@@ -2887,7 +2894,7 @@ PHP_FUNCTION(ldap_escape)
28872894

28882895
if (flags & PHP_LDAP_ESCAPE_DN) {
28892896
havecharlist = 1;
2890-
php_ldap_escape_map_set_chars(map, "\\,=+<>;\"#", sizeof("\\,=+<>;\"#") - 1, 1);
2897+
php_ldap_escape_map_set_chars(map, "\\,=+<>;\"#\r", sizeof("\\,=+<>;\"#\r") - 1, 1);
28912898
}
28922899

28932900
if (!havecharlist) {
@@ -2900,7 +2907,7 @@ PHP_FUNCTION(ldap_escape)
29002907
php_ldap_escape_map_set_chars(map, ignores, ignoreslen, 0);
29012908
}
29022909

2903-
RETURN_NEW_STR(php_ldap_do_escape(map, value, valuelen));
2910+
RETURN_NEW_STR(php_ldap_do_escape(map, value, valuelen, flags));
29042911
}
29052912

29062913
#ifdef STR_TRANSLATION

ext/ldap/tests/bug72021.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug #72021 (ldap_escape() with DN flag is not RFC compliant)
3+
--CREDITS--
4+
Chad Sikorra <Chad.Sikorra@gmail.com>
5+
--SKIPIF--
6+
<?php require_once('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
$subject = " Joe,= \rSmith ";
10+
11+
var_dump(ldap_escape($subject, null, LDAP_ESCAPE_DN));
12+
?>
13+
--EXPECT--
14+
string(24) "\20Joe\2c\3d \0dSmith\20"

0 commit comments

Comments
 (0)