This prepares the future work on this backend.
libldapbackend_la_SOURCES = \
ldapbackend.cc ldapbackend.hh \
powerldap.cc powerldap.hh \
- utils.hh
+ utils.hh exceptions.hh \
+ ldaputils.hh ldaputils.cc
libldapbackend_la_LDFLAGS = -module -avoid-version
libldapbackend_la_LIBADD = $(LDAP_LIBS)
-ldapbackend.lo powerldap.lo
+ldapbackend.lo powerldap.lo ldaputils.lo
--- /dev/null
+/*
+ * PowerDNS LDAP Connector
+ * By PowerDNS.COM BV
+ * By Norbert Sendetzky <norbert@linuxnetworks.de> (2003-2007)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <exception>
+#include <stdexcept>
+#include <string>
+
+#ifndef LDAPEXCEPTIONS_HH
+#define LDAPEXCEPTIONS_HH
+
+class LDAPException : public std::runtime_error
+{
+public:
+ explicit LDAPException( const std::string &str ) : std::runtime_error( str ) {}
+};
+
+class LDAPTimeout : public LDAPException
+{
+public:
+ explicit LDAPTimeout() : LDAPException( "Timeout" ) {}
+};
+
+#endif // LDAPEXCEPTIONS_HH
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#include "exceptions.hh"
#include "ldapbackend.hh"
unsigned int ldap_host_index = 0;
--- /dev/null
+#include "ldaputils.hh"
+#include <sys/time.h>
+
+void ldapSetOption( LDAP *conn, int option, void *value )
+{
+ if( ldap_set_option( conn, option, value ) != LDAP_OPT_SUCCESS )
+ {
+ throw( LDAPException( "Unable to set option" ) );
+ }
+}
+
+void ldapGetOption( LDAP *conn, int option, void *value )
+{
+ if( ldap_get_option( conn, option, value ) != LDAP_OPT_SUCCESS )
+ {
+ throw( LDAPException( "Unable to get option" ) );
+ }
+}
+
+std::string ldapGetError( LDAP *conn, int code )
+{
+ if ( code == -1 )
+ ldapGetOption( conn, LDAP_OPT_ERROR_NUMBER, &code );
+ return std::string( ldap_err2string( code ) );
+}
+
+int ldapWaitResult( LDAP *conn, int msgid, int timeout, LDAPMessage** result )
+{
+ struct timeval tv;
+ LDAPMessage* res;
+
+
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ int rc = ldap_result( conn, msgid, LDAP_MSG_ONE, &tv, &res );
+
+ switch( rc )
+ {
+ case -1:
+ throw LDAPException( "Error waiting for LDAP result: " + ldapGetError( conn, rc ) );
+ case 0:
+ throw LDAPTimeout();
+ }
+
+ if( result == NULL )
+ {
+ ldap_msgfree( res );
+ return rc;
+ }
+
+ *result = res;
+ return rc;
+}
--- /dev/null
+/*
+ * PowerDNS LDAP Connector
+ * By PowerDNS.COM BV
+ * By Norbert Sendetzky <norbert@linuxnetworks.de> (2003-2007)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "exceptions.hh"
+
+#include <ldap.h>
+#include <string>
+
+#ifndef LDAPUTILS_HH
+#define LDAPUTILS_HH
+
+void ldapSetOption( LDAP *conn, int option, void *value );
+
+void ldapGetOption( LDAP *conn, int option, void *value );
+
+std::string ldapGetError( LDAP *conn, int code );
+
+int ldapWaitResult( LDAP *conn, int msgid = LDAP_RES_ANY, int timeout = 0, LDAPMessage** result = NULL );
+
+#endif // LDAPUTILS_HH
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#include "exceptions.hh"
+#include "ldaputils.hh"
#include "powerldap.hh"
#include "pdns/misc.hh"
#include <sys/time.h>
void PowerLDAP::setOption( int option, int value )
{
- if( ldap_set_option( d_ld, option, (void*) &value ) != LDAP_OPT_SUCCESS )
- {
- throw( LDAPException( "Unable to set option" ) );
- }
+ ldapSetOption( d_ld, option, (void*) &value );
}
void PowerLDAP::getOption( int option, int *value )
{
- if( ldap_get_option( d_ld, option, (void*) value ) != LDAP_OPT_SUCCESS )
- {
- throw( LDAPException( "Unable to get option" ) );
- }
+ ldapGetOption( d_ld, option, (void*) value );
}
int PowerLDAP::waitResult( int msgid, int timeout, LDAPMessage** result )
{
- struct timeval tv;
- LDAPMessage* res;
-
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
- 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();
+ try {
+ ldapWaitResult( d_ld, msgid, timeout, result );
}
-
- if( result == NULL )
- {
- ldap_msgfree( res );
- return rc;
+ catch ( LDAPException &e ) {
+ ensureConnect();
+ throw; // Not sure why this was done, but the original behavior.
}
-
- *result = res;
- return rc;
}
const string PowerLDAP::getError( int rc )
{
- if( rc == -1 ) { getOption( LDAP_OPT_ERROR_NUMBER, &rc ); }
-
- return string( ldap_err2string( rc ) );;
+ return ldapGetError( d_ld, rc );
}
#include <map>
#include <string>
#include <vector>
-#include <exception>
#include <stdexcept>
#include <inttypes.h>
#include <errno.h>
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;