Skip to content

Commit fcbe20d

Browse files
megabugsmalyshev
authored andcommitted
Set an LDAP error code when failing ldap_bind due to null bytes
Some applications check a LDAP link's error code after seeing ldap_bind fail due to a null byte bind attempt and hence incorrectly receive the last set error code. Fix by setting an LDAP error code before returning in this case.
1 parent 2711948 commit fcbe20d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

ext/ldap/ldap.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,23 @@ static int _get_lderrno(LDAP *ldap)
385385
}
386386
/* }}} */
387387

388+
/* {{{ _set_lderrno
389+
*/
390+
static void _set_lderrno(LDAP *ldap, int lderr)
391+
{
392+
#if !HAVE_NSLDAP
393+
#if LDAP_API_VERSION > 2000 || HAVE_ORALDAP
394+
/* New versions of OpenLDAP do it this way */
395+
ldap_set_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
396+
#else
397+
ldap->ld_errno = lderr;
398+
#endif
399+
#else
400+
ldap_set_lderrno(ldap, lderr, NULL, NULL);
401+
#endif
402+
}
403+
/* }}} */
404+
388405
/* {{{ proto bool ldap_bind(resource link [, string dn [, string password]])
389406
Bind to LDAP directory */
390407
PHP_FUNCTION(ldap_bind)
@@ -399,18 +416,20 @@ PHP_FUNCTION(ldap_bind)
399416
RETURN_FALSE;
400417
}
401418

419+
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
420+
402421
if (ldap_bind_dn != NULL && memchr(ldap_bind_dn, '\0', ldap_bind_dnlen) != NULL) {
422+
_set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
403423
php_error_docref(NULL TSRMLS_CC, E_WARNING, "DN contains a null byte");
404424
RETURN_FALSE;
405425
}
406426

407427
if (ldap_bind_pw != NULL && memchr(ldap_bind_pw, '\0', ldap_bind_pwlen) != NULL) {
428+
_set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
408429
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Password contains a null byte");
409430
RETURN_FALSE;
410431
}
411432

412-
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
413-
414433
if ((rc = ldap_bind_s(ld->link, ldap_bind_dn, ldap_bind_pw, LDAP_AUTH_SIMPLE)) != LDAP_SUCCESS) {
415434
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
416435
RETURN_FALSE;

0 commit comments

Comments
 (0)