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++ )
{
}
}
#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
}
}
- 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 ) );
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();
#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;