diff --git a/library/Icinga/Protocol/Ldap/LdapConnection.php b/library/Icinga/Protocol/Ldap/LdapConnection.php index 5f1a7b728..3d30f9852 100644 --- a/library/Icinga/Protocol/Ldap/LdapConnection.php +++ b/library/Icinga/Protocol/Ldap/LdapConnection.php @@ -358,10 +358,30 @@ class LdapConnection implements Selectable, Inspectable */ public function count(LdapQuery $query) { + $ds = $this->getConnection(); $this->bind(); - $res = $this->runQuery($query, array()); - return count($res); + $results = @ldap_search( + $ds, + $query->getBase() ?: $this->getDn(), + (string) $query, + array('dn'), + 0, + 0 + ); + + if ($results === false) { + if (ldap_errno($ds) !== self::LDAP_NO_SUCH_OBJECT) { + throw new LdapException( + 'LDAP count query "%s" (base %s) failed: %s', + (string) $query, + $query->getBase() ?: $this->getDn(), + ldap_error($ds) + ); + } + } + + return ldap_count_entries($ds, $results); } /**