diff --git a/include/class_ldap.inc b/include/class_ldap.inc index 6628f0b5e5792120fd7af076c13559854a904c0f..8fab363eb6a600e688bfd26b7dea579e40e1337b 100755 --- a/include/class_ldap.inc +++ b/include/class_ldap.inc @@ -327,7 +327,7 @@ class LDAP * * \param string $scope Scope of the search: subtree/base/one */ - function search ($srp, $filter, $attrs = [], $scope = 'subtree', array $controls = NULL) + function search($srp, $filter, $attrs = [], $scope = 'subtree', array $controls = NULL) { if ($this->hascon) { if ($this->reconnect) { @@ -336,51 +336,68 @@ class LDAP $startTime = microtime(TRUE); $this->clearResult($srp); - switch (strtolower((string) $scope)) { + + switch (strtolower((string)$scope)) { case 'base': if (isset($controls)) { - $this->sr[$srp] = @ldap_read($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); + $this->sr[$srp] = ldap_read($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); } else { - $this->sr[$srp] = @ldap_read($this->cid, $this->basedn, $filter, $attrs); + $this->sr[$srp] = ldap_read($this->cid, $this->basedn, $filter, $attrs); } break; + case 'one': if (isset($controls)) { - $this->sr[$srp] = @ldap_list($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); + $this->sr[$srp] = ldap_list($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); } else { - $this->sr[$srp] = @ldap_list($this->cid, $this->basedn, $filter, $attrs); + $this->sr[$srp] = ldap_list($this->cid, $this->basedn, $filter, $attrs); } break; + case 'subtree': default: if (isset($controls)) { - $this->sr[$srp] = @ldap_search($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); + $this->sr[$srp] = ldap_search($this->cid, $this->basedn, $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER, $controls); } else { - $this->sr[$srp] = @ldap_search($this->cid, $this->basedn, $filter, $attrs); + $this->sr[$srp] = ldap_search($this->cid, $this->basedn, $filter, $attrs); } break; } - $this->error = @ldap_error($this->cid); + + // Check if the LDAP operation was successful + if ($this->sr[$srp] === false) { + // If it failed, log the error and handle it properly + $this->error = ldap_error($this->cid); + logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'LDAP search error'); + $this->resetResult($srp); + $this->hasres[$srp] = FALSE; // Indicate that there was no result + return false; // Return false to indicate the search failed + } + $this->resetResult($srp); $this->hasres[$srp] = TRUE; - /* Check if query took longer as specified in max_ldap_query_time */ + // Check if query took longer than specified in max_ldap_query_time $diff = microtime(TRUE) - $startTime; if ($this->max_ldap_query_time && ($diff > $this->max_ldap_query_time)) { $warning = new FusionDirectoryWarning(htmlescape(sprintf(_('LDAP performance is poor: last query took about %.2fs!'), $diff))); $warning->display(); } + // Log the LDAP operation $this->log("LDAP operation: time=".$diff." operation=search('".$this->basedn."', '$filter')"); logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'search(base="'.$this->basedn.'",scope="'.$scope.'",filter="'.$filter.'")'); + return $this->sr[$srp]; } else { + // Handle case where the connection is not established $this->error = "Could not connect to LDAP server"; logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'search(base="'.$this->basedn.'",scope="'.$scope.'",filter="'.$filter.'")'); - return ""; + return false; } } + /*! * \brief Parse last result * @@ -428,24 +445,53 @@ class LDAP * * \param string $filter Initialized at "(objectclass=*)" */ - function cat ($srp, $dn, $attrs = ["*"], $filter = "(objectclass=*)") - { + function cat($srp, $dn, $attrs = ["*"], $filter = "(objectclass=*)") { if ($this->hascon) { if ($this->reconnect) { $this->connect(); } $this->clearResult($srp); + + // Use @ to suppress any warnings or errors from ldap_read $this->sr[$srp] = @ldap_read($this->cid, $dn, $filter, $attrs); - $this->error = @ldap_error($this->cid); + + // Check if ldap_read() failed, but do not log as an error if the DN is missing + if ($this->sr[$srp] === false) { + // Get the LDAP error message + $this->error = ldap_error($this->cid); + + // Check if the error is "No such object", which means the DN does not exist + if (strpos($this->error, 'No such object') !== false) { + // Expected behavior: DN not found + logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, + 'No such object for dn="'.$dn.'" - Expected absence'); + return null; // Return null to gracefully handle the expected absence of the DN + } else { + // If the error is not "No such object", log it as a real error + logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, + 'LDAP read failed for dn="'.$dn.'", filter="'.$filter.'" Error: '.$this->error); + return false; // Return false to indicate an unexpected error + } + } + + // If no error, reset the result and mark as having results + $this->error = ldap_error($this->cid); // Capture any last error $this->resetResult($srp); - $this->hasres[$srp] = TRUE; - logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'cat(dn="'.$dn.'",filter="'.$filter.'")'); + $this->hasres[$srp] = true; + + // Log the successful operation + logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, 'No error', + 'cat(dn="'.$dn.'", filter="'.$filter.'")'); + + // Return the LDAP result return $this->sr[$srp]; } else { + // Handle the case when there's no connection $this->error = "Could not connect to LDAP server"; - logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'cat(dn="'.$dn.'",filter="'.$filter.'")'); - return ""; + logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, + 'cat(dn="'.$dn.'", filter="'.$filter.'")'); + return false; // Return false instead of an empty string } } diff --git a/plugins/personal/generic/class_user.inc b/plugins/personal/generic/class_user.inc index ec2059d81596ba9191aed9c605df95d4701fb71f..00815d13c032a4dc3e9d56178fdeaa98e362a2cc 100755 --- a/plugins/personal/generic/class_user.inc +++ b/plugins/personal/generic/class_user.inc @@ -485,10 +485,12 @@ class user extends simplePlugin $check_length = ($config->get_cfg_value('passwordMinLength') != ''); $length = $config->get_cfg_value('passwordMinLength', 0); - try { - list($policy, $attrs) = static::fetchPpolicy($user); - } catch (NonExistingLdapNodeException $e) { - return $e->getMessage(); + if (class_available('ppolicyAccount')) { + try { + list($policy, $attrs) = static::fetchPpolicy($user); + } catch (NonExistingLdapNodeException $e) { + return $e->getMessage(); + } } if (isset($policy)) {