From: Grégory Oestreicher Date: Wed, 14 Sep 2016 16:52:38 +0000 (+0200) Subject: Refactor the LDAP backend. X-Git-Tag: rec-4.1.0-alpha1~170^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5c80fdad6494868bf289deeab7b51f2ae0d0b17;p=thirdparty%2Fpdns.git Refactor the LDAP backend. This prepares the future work on this backend. --- diff --git a/modules/ldapbackend/Makefile.am b/modules/ldapbackend/Makefile.am index 6b76a215e8..e6f9cc6664 100644 --- a/modules/ldapbackend/Makefile.am +++ b/modules/ldapbackend/Makefile.am @@ -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) diff --git a/modules/ldapbackend/OBJECTFILES b/modules/ldapbackend/OBJECTFILES index 59f4773cd7..faad6e569b 100644 --- a/modules/ldapbackend/OBJECTFILES +++ b/modules/ldapbackend/OBJECTFILES @@ -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 index 0000000000..dfbf675b65 --- /dev/null +++ b/modules/ldapbackend/exceptions.hh @@ -0,0 +1,39 @@ +/* + * PowerDNS LDAP Connector + * By PowerDNS.COM BV + * By Norbert Sendetzky (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 +#include +#include + +#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 diff --git a/modules/ldapbackend/ldapbackend.cc b/modules/ldapbackend/ldapbackend.cc index 5c90b49c67..e920cafb76 100644 --- a/modules/ldapbackend/ldapbackend.cc +++ b/modules/ldapbackend/ldapbackend.cc @@ -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 index 0000000000..35ee0c36bc --- /dev/null +++ b/modules/ldapbackend/ldaputils.cc @@ -0,0 +1,54 @@ +#include "ldaputils.hh" +#include + +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 index 0000000000..ff26af391e --- /dev/null +++ b/modules/ldapbackend/ldaputils.hh @@ -0,0 +1,36 @@ +/* + * PowerDNS LDAP Connector + * By PowerDNS.COM BV + * By Norbert Sendetzky (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 +#include + +#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 diff --git a/modules/ldapbackend/powerldap.cc b/modules/ldapbackend/powerldap.cc index 5b184e137e..045702f086 100644 --- a/modules/ldapbackend/powerldap.cc +++ b/modules/ldapbackend/powerldap.cc @@ -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 @@ -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 ); } diff --git a/modules/ldapbackend/powerldap.hh b/modules/ldapbackend/powerldap.hh index 18cc25c996..c7f0abfa31 100644 --- a/modules/ldapbackend/powerldap.hh +++ b/modules/ldapbackend/powerldap.hh @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -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;