]> granicus.if.org Git - pdns/commitdiff
very simpleminded ldap reconnect fix
authorBert Hubert <bert.hubert@netherlabs.nl>
Thu, 14 Jul 2011 18:42:05 +0000 (18:42 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Thu, 14 Jul 2011 18:42:05 +0000 (18:42 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2235 d19b8d6e-7fed-0310-83ef-9ca221ded41b

modules/ldapbackend/powerldap.cc
modules/ldapbackend/powerldap.hh

index 8ac36b83f9493a85638147941e4e654766ea06f5..ba8932d0ab221ab0efde9cae9d7f548344111694 100644 (file)
@@ -5,15 +5,28 @@
 
 
 PowerLDAP::PowerLDAP( const string& hosts, uint16_t port, bool tls )
+{
+        d_ld = 0;
+        d_hosts = hosts;
+        d_port = port;
+        d_tls = tls;
+        ensureConnect();
+}
+
+void PowerLDAP::ensureConnect()
 {
         int err;
 
+        if(d_ld) {
+          ldap_unbind_ext( d_ld, NULL, NULL );
+        }
+
 #ifdef HAVE_LDAP_INITIALIZE
-        if( ( err = ldap_initialize( &d_ld, hosts.c_str() ) ) != LDAP_SUCCESS )
+        if( ( err = ldap_initialize( &d_ld, d_hosts.c_str() ) ) != LDAP_SUCCESS )
         {
                string ldapuris;
                vector<string> uris;
-               stringtok( uris, hosts );
+               stringtok( uris, d_hosts );
 
                for( size_t i = 0; i < uris.size(); i++ )
                {
@@ -26,9 +39,9 @@ PowerLDAP::PowerLDAP( const string& hosts, uint16_t port, bool tls )
                }
         }
 #else
-        if( ( d_ld = ldap_init( hosts.c_str(), port ) ) == NULL )
+        if( ( d_ld = ldap_init( d_hosts.c_str(), d_port ) ) == NULL )
         {
-               throw LDAPException( "Error initializing LDAP connection to '" + hosts + "': " + string( strerror( errno ) ) );
+               throw LDAPException( "Error initializing LDAP connection to '" + d_hosts + "': " + string( strerror( errno ) ) );
         }
 #endif
 
@@ -43,7 +56,7 @@ PowerLDAP::PowerLDAP( const string& hosts, uint16_t port, bool tls )
                }
         }
 
-        if( tls && ( err = ldap_start_tls_s( d_ld, NULL, NULL ) ) != LDAP_SUCCESS )
+        if( d_tls && ( err = ldap_start_tls_s( d_ld, NULL, NULL ) ) != LDAP_SUCCESS )
         {
                ldap_unbind_ext( d_ld, NULL, NULL );
                throw LDAPException( "Couldn't perform STARTTLS: " + getError( err ) );
@@ -135,15 +148,16 @@ int PowerLDAP::waitResult( int msgid, int timeout, LDAPMessage** result )
         struct timeval tv;
         LDAPMessage* res;
 
-
         tv.tv_sec = timeout;
         tv.tv_usec = 0;
-
-        int rc = ldap_result( d_ld, msgid, LDAP_MSG_ONE, &tv, &res );
+        int rc;
+      
+        rc = ldap_result( d_ld, msgid, LDAP_MSG_ONE, &tv, &res );
 
         switch( rc )
         {
                case -1:
+            ensureConnect();
                        throw LDAPException( "Error waiting for LDAP result: " + getError() );
                case 0:
                        throw LDAPTimeout();
index f793df2a8fee69c5c3f777be94c5e22ff9220492..a2ae07987d34d50c2f01451ac932fb3eecdaafb5 100644 (file)
 #ifndef POWERLDAP_HH
 #define POWERLDAP_HH
 
-
-
 using std::map;
 using std::string;
 using std::vector;
 
-
-
 class LDAPException : public std::runtime_error
 {
 public:
         explicit LDAPException( const string &str ) : std::runtime_error( str ) {}
 };
 
-
-
 class LDAPTimeout : public LDAPException
 {
 public:
         explicit LDAPTimeout() : LDAPException( "Timeout" ) {}
 };
 
-
-
 class PowerLDAP
 {
         LDAP* d_ld;
+        string d_hosts;
+        int d_port;
+        bool d_tls;
 
         const string getError( int rc = -1 );
         int waitResult( int msgid = LDAP_RES_ANY, int timeout = 0, LDAPMessage** result = NULL );
-
+        void ensureConnect();
+        
 public:
         typedef map<string, vector<string> > sentry_t;
         typedef vector<sentry_t> sresult_t;