1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include "powerldap.hh"
#include <pdns/misc.hh>
#include <sys/time.h>
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, d_hosts.c_str() ) ) != LDAP_SUCCESS )
{
string ldapuris;
vector<string> uris;
stringtok( uris, d_hosts );
for( size_t i = 0; i < uris.size(); i++ )
{
ldapuris += " ldap://" + uris[i];
}
if( ( err = ldap_initialize( &d_ld, ldapuris.c_str() ) ) != LDAP_SUCCESS )
{
throw LDAPException( "Error initializing LDAP connection to '" + ldapuris + ": " + getError( err ) );
}
}
#else
if( ( d_ld = ldap_init( d_hosts.c_str(), d_port ) ) == NULL )
{
throw LDAPException( "Error initializing LDAP connection to '" + d_hosts + "': " + string( strerror( errno ) ) );
}
#endif
int protocol = LDAP_VERSION3;
if( ldap_set_option( d_ld, LDAP_OPT_PROTOCOL_VERSION, &protocol ) != LDAP_OPT_SUCCESS )
{
protocol = LDAP_VERSION2;
if( ldap_set_option( d_ld, LDAP_OPT_PROTOCOL_VERSION, &protocol ) != LDAP_OPT_SUCCESS )
{
ldap_unbind_ext( d_ld, NULL, NULL );
throw LDAPException( "Couldn't set protocol version to LDAPv3 or LDAPv2" );
}
}
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 ) );
}
}
PowerLDAP::~PowerLDAP()
{
ldap_unbind_ext( d_ld, NULL, NULL );
}
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" ) );
}
}
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" ) );
}
}
void PowerLDAP::bind( const string& ldapbinddn, const string& ldapsecret, int method, int timeout )
{
int msgid;
#ifdef HAVE_LDAP_SASL_BIND
int rc;
struct berval passwd;
passwd.bv_val = (char *)ldapsecret.c_str();
passwd.bv_len = strlen( passwd.bv_val );
if( ( rc = ldap_sasl_bind( d_ld, ldapbinddn.c_str(), LDAP_SASL_SIMPLE, &passwd, NULL, NULL, &msgid ) ) != LDAP_SUCCESS )
{
throw LDAPException( "Failed to bind to LDAP server: " + getError( rc ) );
}
#else
if( ( msgid = ldap_bind( d_ld, ldapbinddn.c_str(), ldapsecret.c_str(), method ) ) == -1 )
{
throw LDAPException( "Failed to bind to LDAP server: " + getError( msgid ) );
}
#endif
waitResult( msgid, timeout, NULL );
}
/**
* Deprecated, use PowerLDAP::bind() instead
*/
void PowerLDAP::simpleBind( const string& ldapbinddn, const string& ldapsecret )
{
this->bind( ldapbinddn, ldapsecret, LDAP_AUTH_SIMPLE, 30 );
}
int PowerLDAP::search( const string& base, int scope, const string& filter, const char** attr )
{
int msgid, rc;
if( ( rc = ldap_search_ext( d_ld, base.c_str(), scope, filter.c_str(), const_cast<char**> (attr), 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &msgid ) ) != LDAP_SUCCESS )
{
throw LDAPException( "Starting LDAP search: " + getError( rc ) );
}
return msgid;
}
/**
* Function waits for a result, returns its type and optionally stores the result.
* If the result is returned, the caller is responsible for freeing it with
* ldap_msgfree!
*/
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();
}
if( result == NULL )
{
ldap_msgfree( res );
return rc;
}
*result = res;
return rc;
}
bool PowerLDAP::getSearchEntry( int msgid, sentry_t& entry, bool dn, int timeout )
{
int i;
char* attr;
BerElement* ber;
struct berval** berval;
vector<string> values;
LDAPMessage* result;
LDAPMessage* object;
if( ( i = waitResult( msgid, timeout, &result ) ) == LDAP_RES_SEARCH_RESULT )
{
ldap_msgfree( result );
return false;
}
if( i != LDAP_RES_SEARCH_ENTRY )
{
ldap_msgfree( result );
throw LDAPException( "Search returned an unexpected result" );
}
if( ( object = ldap_first_entry( d_ld, result ) ) == NULL )
{
ldap_msgfree( result );
throw LDAPException( "Couldn't get first result entry: " + getError() );
}
entry.clear();
if( dn )
{
attr = ldap_get_dn( d_ld, object );
values.push_back( string( attr ) );
ldap_memfree( attr );
entry["dn"] = values;
}
if( ( attr = ldap_first_attribute( d_ld, object, &ber ) ) != NULL )
{
do
{
if( ( berval = ldap_get_values_len( d_ld, object, attr ) ) != NULL )
{
values.clear();
for( i = 0; i < ldap_count_values_len( berval ); i++ )
{
values.push_back( berval[i]->bv_val ); // use berval[i]->bv_len for non string values?
}
entry[attr] = values;
ldap_value_free_len( berval );
}
ldap_memfree( attr );
}
while( ( attr = ldap_next_attribute( d_ld, object, ber ) ) != NULL );
ber_free( ber, 0 );
}
ldap_msgfree( result );
return true;
}
void PowerLDAP::getSearchResults( int msgid, sresult_t& result, bool dn, int timeout )
{
sentry_t entry;
result.clear();
while( getSearchEntry( msgid, entry, dn, timeout ) )
{
result.push_back( entry );
}
}
const string PowerLDAP::getError( int rc )
{
if( rc == -1 ) { getOption( LDAP_OPT_ERROR_NUMBER, &rc ); }
return string( ldap_err2string( rc ) );;
}
const string PowerLDAP::escape( const string& str )
{
string a;
string::const_iterator i;
for( i = str.begin(); i != str.end(); i++ )
{
if( *i == '*' || *i == '\\' ) {
a += '\\';
}
a += *i;
}
return a;
}
|