From 6d95f2fdb21f732e6bcd5035939692e9b6e8a93e Mon Sep 17 00:00:00 2001 From: Miod Vallat Date: Thu, 9 Oct 2025 10:40:52 +0200 Subject: [PATCH] Correctly handle reconnection in several routines. If the search failed with a LDAPNoConnection exception and reconnection is successful, we would recurse to reiterate the operation, but then would proceed with the exist logic operating on uninitialized data. Signed-off-by: Miod Vallat --- modules/ldapbackend/native.cc | 23 ++++++++++++----------- modules/ldapbackend/primary.cc | 22 ++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/modules/ldapbackend/native.cc b/modules/ldapbackend/native.cc index f56496906a..b971d3c2b3 100644 --- a/modules/ldapbackend/native.cc +++ b/modules/ldapbackend/native.cc @@ -100,10 +100,10 @@ bool LdapBackend::list(const ZoneName& target, domainid_t domain_id, bool /* inc } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) - this->list(target, domain_id); - else - throw PDNSException("Failed to reconnect to LDAP server"); + if (reconnect()) { + return this->list(target, domain_id); + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to get zone " << target << " from LDAP directory: " << le.what() << endl; @@ -183,10 +183,11 @@ void LdapBackend::lookup(const QType& qtype, const DNSName& qname, domainid_t zo } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) + if (reconnect()) { this->lookup(qtype, qname, zoneid, dnspkt); - else - throw PDNSException("Failed to reconnect to LDAP server"); + return; + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to search LDAP directory: " << le.what() << endl; @@ -413,10 +414,10 @@ bool LdapBackend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool / } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) - this->getDomainInfo(domain, info); - else - throw PDNSException("Failed to reconnect to LDAP server"); + if (reconnect()) { + return this->getDomainInfo(domain, info); + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to search LDAP directory: " << le.what() << endl; diff --git a/modules/ldapbackend/primary.cc b/modules/ldapbackend/primary.cc index 14d8bda044..d53af02b5a 100644 --- a/modules/ldapbackend/primary.cc +++ b/modules/ldapbackend/primary.cc @@ -44,10 +44,10 @@ void LdapBackend::getUpdatedPrimaries(vector& domains, std::unordere } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) - this->getUpdatedPrimaries(domains, catalogs, catalogHashes); - else - throw PDNSException("Failed to reconnect to LDAP server"); + if (reconnect()) { + return this->getUpdatedPrimaries(domains, catalogs, catalogHashes); + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to search LDAP directory: " << le.what() << endl; @@ -92,10 +92,11 @@ void LdapBackend::setNotified(domainid_t id, uint32_t serial) } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) + if (reconnect()) { this->setNotified(id, serial); - else - throw PDNSException("Failed to reconnect to LDAP server"); + return; + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to search LDAP directory: " << le.what() << endl; @@ -129,10 +130,11 @@ void LdapBackend::setNotified(domainid_t id, uint32_t serial) } catch (LDAPNoConnection& lnc) { g_log << Logger::Warning << d_myname << " Connection to LDAP lost, trying to reconnect" << endl; - if (reconnect()) + if (reconnect()) { this->setNotified(id, serial); - else - throw PDNSException("Failed to reconnect to LDAP server"); + return; + } + throw PDNSException("Failed to reconnect to LDAP server"); } catch (LDAPException& le) { g_log << Logger::Error << d_myname << " Unable to search LDAP directory: " << le.what() << endl; -- 2.47.3