php-src icon indicating copy to clipboard operation
php-src copied to clipboard

ldap_search not working with base=NULL after php7.1

Open dab18 opened this issue 4 years ago • 8 comments

Description

The following code:

<?php
$ds = ldap_connect(); // <-- URI from /etc/openldap/ldap.conf
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

$sr = ldap_search($ds, NULL, "(uid=dab)", array("dn"));  // <-- BASE from /etc/openldap/ldap.conf
$entry = ldap_first_entry($ds, $sr);
var_dump($entry);

Resulted in this output:

Warning: ldap_search(): Search: No such object in test.php on line 5

But I expected this output instead:

resource(6) of type (ldap result entry)

/etc/openldap/ldap.conf: BASE c=ru URI ldap://localhost

slapd.log: version <=php7.1 slapd[14091]: conn=4230904 op=0 SRCH base="c=ru" scope=2 deref=0 filter="(uid=dab)" version >php7.1 slapd[14091]: conn=4230791 op=0 SRCH base="" scope=2 deref=0 filter="(uid=dab)"

related bug&commit: https://bugs.php.net/bug.php?id=77958 https://github.com/php/php-src/commit/5d2fe48785b6d24102ff53e78631cba7f2aefbef

PHP Version

PHP 8.0.12

Operating System

gentoo

dab18 avatar Jan 25 '22 07:01 dab18

It seems that behavioral change happened to due to fixing https://bugs.php.net/bug.php?id=77958. It might be a good idea to make the $base parameter nullable, to at least partially restore the former behavior.

@MCMic, what do you think?

cmb69 avatar Jan 25 '22 17:01 cmb69

Sounds like something that worked by chance, was it ever documented anywhere? It seems normal to expect a base when searching, and 7.1 is quite old so this has been like this for a lot of versions.

MCMic avatar Jan 27 '22 17:01 MCMic

I don't think that it was ever documented. The latest relevant change to the ldap_search fixed the parameter types from string to array|string.

cmb69 avatar Jan 27 '22 22:01 cmb69

but it worked and it's logical with default options from system configuration files another option: parse configs?

dab18 avatar Jan 28 '22 05:01 dab18

from openldap: man 5 ldap.conf The ldap.conf configuration file is used to set system-wide defaults to be applied when running ldap clients. BASE Specifies the default base DN to use when performing ldap operations. The base must be specified as a Distinguished Name in LDAP format.

code from openldap/libraries/libldap/search.c: in function ldap_build_search_req: if ( base == NULL ) { /* no base provided, use session default base */ base = ld->ld_options.ldo_defbase;

"session default base" initialized in init.c from config files

it's this behavior openldap-specific only?

dab18 avatar Jan 31 '22 08:01 dab18

Yes I think it is openldap specific.

come-nc avatar Feb 01 '22 09:02 come-nc

So should we transfer this issue to doc-en?

cmb69 avatar Feb 07 '22 12:02 cmb69

for ldap_search() only? or ldap_connect() also? ldap_connect(null) also not fully documented (what uri=null means?), but gets default uri from /etc/openldap/ldap.conf

dab18 avatar Feb 07 '22 13:02 dab18