]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Refactor the LDAP backend.
authorGrégory Oestreicher <greg@kamago.net>
Wed, 14 Sep 2016 16:52:38 +0000 (18:52 +0200)
committerGrégory Oestreicher <greg@kamago.net>
Tue, 28 Feb 2017 21:32:44 +0000 (22:32 +0100)
This prepares the future work on this backend.

modules/ldapbackend/Makefile.am
modules/ldapbackend/OBJECTFILES
modules/ldapbackend/exceptions.hh [new file with mode: 0644]
modules/ldapbackend/ldapbackend.cc
modules/ldapbackend/ldaputils.cc [new file with mode: 0644]
modules/ldapbackend/ldaputils.hh [new file with mode: 0644]
modules/ldapbackend/powerldap.cc
modules/ldapbackend/powerldap.hh

index 6b76a215e8ab3a8b6021ae17915410446fb0af77..e6f9cc66645831851c467c3d98be3c1de2f70ebd 100644 (file)
@@ -5,7 +5,8 @@ EXTRA_DIST = OBJECTFILES OBJECTLIBS
 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)
index 59f4773cd75440ff50a92e467d6b9187155c1d0d..faad6e569b881d15d8edabcfc02faaa783543636 100644 (file)
@@ -1 +1 @@
-ldapbackend.lo powerldap.lo
+ldapbackend.lo powerldap.lo ldaputils.lo
diff --git a/modules/ldapbackend/exceptions.hh b/modules/ldapbackend/exceptions.hh
new file mode 100644 (file)
index 0000000..dfbf675
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *  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
index 5c90b49c67453545765ef57c3619061acf323ef0..e920cafb76aad6ce4ed3e16388f1b846f6b2a040 100644 (file)
@@ -23,6 +23,7 @@
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#include "exceptions.hh"
 #include "ldapbackend.hh"
 
 unsigned int ldap_host_index = 0;
diff --git a/modules/ldapbackend/ldaputils.cc b/modules/ldapbackend/ldaputils.cc
new file mode 100644 (file)
index 0000000..35ee0c3
--- /dev/null
@@ -0,0 +1,54 @@
+#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;
+}
diff --git a/modules/ldapbackend/ldaputils.hh b/modules/ldapbackend/ldaputils.hh
new file mode 100644 (file)
index 0000000..ff26af3
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *  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
index 5b184e137ee186f14bf3145997164951e0151048..045702f086269d22c2192ad67df8d7bc137aaba8 100644 (file)
@@ -23,6 +23,8 @@
 #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>
@@ -97,19 +99,13 @@ PowerLDAP::~PowerLDAP()
 
 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 );
 }
 
 
@@ -170,32 +166,13 @@ int PowerLDAP::search( const string& base, int scope, const string& filter, cons
 
 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;
 }
 
 
@@ -279,9 +256,7 @@ void PowerLDAP::getSearchResults( int msgid, sresult_t& result, bool dn, int tim
 
 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 );
 }
 
 
index 18cc25c9966b421bb0b07652bf47d8f34435a1dd..c7f0abfa3118ca277cbb7c0189f0cc4613b7c12b 100644 (file)
@@ -23,7 +23,6 @@
 #include <map>
 #include <string>
 #include <vector>
-#include <exception>
 #include <stdexcept>
 #include <inttypes.h>
 #include <errno.h>
@@ -40,18 +39,6 @@ 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;