21 #include "ldapconnection.h"
23 #include "kldap_config.h"
26 #include <klocalizedstring.h>
30 #include <sasl/sasl.h>
31 static sasl_callback_t callbacks[] = {
32 { SASL_CB_ECHOPROMPT, NULL, NULL },
33 { SASL_CB_NOECHOPROMPT, NULL, NULL },
34 { SASL_CB_GETREALM, NULL, NULL },
35 { SASL_CB_USER, NULL, NULL },
36 { SASL_CB_AUTHNAME, NULL, NULL },
37 { SASL_CB_PASS, NULL, NULL },
38 { SASL_CB_CANON_USER, NULL, NULL },
39 { SASL_CB_LIST_END, NULL, NULL }
42 static bool ldapoperation_sasl_initialized =
false;
46 # ifndef HAVE_WINLDAP_H
50 # include <w32-ldap-help.h>
51 #endif // HAVE_WINLDAP_H
53 #ifndef LDAP_OPT_SUCCESS
54 #define LDAP_OPT_SUCCESS 0
59 using namespace KLDAP;
61 class LdapConnection::LdapConnectionPrivate
64 LdapConnectionPrivate();
74 sasl_conn_t *mSASLconn;
81 LdapConnection::LdapConnectionPrivate::LdapConnectionPrivate()
85 if ( !ldapoperation_sasl_initialized ) {
86 sasl_client_init( NULL );
87 ldapoperation_sasl_initialized =
true;
93 : d( new LdapConnectionPrivate )
99 : d( new LdapConnectionPrivate )
106 : d( new LdapConnectionPrivate )
112 LdapConnection::~LdapConnection()
120 d->mServer.setUrl( url );
135 return (
void *)d->mLDAP;
140 return (
void *)d->mSASLconn;
149 case LDAP_OPERATIONS_ERROR:
150 return i18n(
"LDAP Operations error" );
155 return i18n(
"No LDAP Support..." );
163 str = sasl_errdetail( d->mSASLconn );
166 return i18n(
"SASL support is not available. Please recompile libkldap with the "
167 "Cyrus-SASL (or compatible) client libraries, or complain to your "
168 "distribution packagers." );
174 return d->mConnectionError;
180 Q_ASSERT( d->mLDAP );
181 return ldap_get_option( d->mLDAP, option, value );
186 Q_ASSERT( d->mLDAP );
187 return ldap_set_option( d->mLDAP, option, value );
192 Q_ASSERT( d->mLDAP );
194 ldap_get_option( d->mLDAP, LDAP_OPT_ERROR_NUMBER, &err );
200 Q_ASSERT( d->mLDAP );
202 ldap_get_option( d->mLDAP, LDAP_OPT_ERROR_STRING, &errmsg );
210 Q_ASSERT( d->mLDAP );
211 kDebug() <<
"sizelimit:" << sizelimit;
212 if (
setOption( LDAP_OPT_SIZELIMIT, &sizelimit ) != LDAP_OPT_SUCCESS ) {
220 Q_ASSERT( d->mLDAP );
222 if (
getOption( LDAP_OPT_SIZELIMIT, &sizelimit ) != LDAP_OPT_SUCCESS ) {
230 Q_ASSERT( d->mLDAP );
231 kDebug() <<
"timelimit:" << timelimit;
232 if (
setOption( LDAP_OPT_TIMELIMIT, &timelimit ) != LDAP_OPT_SUCCESS ) {
240 Q_ASSERT( d->mLDAP );
242 if (
getOption( LDAP_OPT_TIMELIMIT, &timelimit ) != LDAP_OPT_SUCCESS ) {
256 int version = d->mServer.version();
257 int timeout = d->mServer.timeout();
260 url += QLatin1String(
"://");
261 url += d->mServer.host();
264 kDebug() <<
"ldap url:" << url;
265 #ifdef HAVE_LDAP_INITIALIZE
266 ret = ldap_initialize( &d->mLDAP, url.toLatin1() );
268 d->mLDAP = ldap_init( d->mServer.host().toLatin1().data(), d->mServer.port() );
269 if ( d->mLDAP == 0 ) {
275 if ( ret != LDAP_SUCCESS ) {
276 d->mConnectionError = i18n(
"An error occurred during the connection initialization phase." );
280 kDebug() <<
"setting version to:" << version;
281 if (
setOption( LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS ) {
283 d->mConnectionError = i18n(
"Cannot set protocol version to %1.", version );
288 #if defined(LDAP_OPT_TIMEOUT)
289 kDebug() <<
"setting timeout to:" << timeout;
292 if (
setOption( LDAP_OPT_TIMEOUT, &timeout ) != LDAP_OPT_SUCCESS ) {
294 d->mConnectionError = i18np(
"Cannot set timeout to %1 second.",
295 "Cannot set timeout to %1 seconds.",
304 kDebug() <<
"setting security to:" << d->mServer.security();
306 kDebug() <<
"start TLS";
307 #ifdef HAVE_LDAP_START_TLS_S
308 if ( ( ret = ldap_start_tls_s( d->mLDAP, NULL, NULL ) ) != LDAP_SUCCESS ) {
315 d->mConnectionError = i18n(
"TLS support not available in the LDAP client libraries." );
320 kDebug() <<
"setting sizelimit to:" << d->mServer.sizeLimit();
321 if ( d->mServer.sizeLimit() ) {
325 d->mConnectionError = i18n(
"Cannot set size limit." );
330 kDebug() <<
"setting timelimit to:" << d->mServer.timeLimit();
331 if ( d->mServer.timeLimit() ) {
335 d->mConnectionError = i18n(
"Cannot set time limit." );
341 kDebug() <<
"initializing SASL client";
342 int saslresult = sasl_client_new(
"ldap", d->mServer.host().toLatin1(),
343 0, 0, callbacks, 0, &d->mSASLconn );
344 if ( saslresult != SASL_OK ) {
345 d->mConnectionError = i18n(
"Cannot initialize the SASL client." );
346 return KLDAP_SASL_ERROR;
356 #ifdef HAVE_LDAP_UNBIND_EXT
357 ldap_unbind_ext( d->mLDAP, 0, 0 );
359 ldap_unbind( d->mLDAP );
364 if ( d->mSASLconn ) {
365 sasl_dispose( &d->mSASLconn );
369 kDebug() <<
"connection closed!";
375 kError() <<
"No LDAP support...";
381 kError() <<
"No LDAP support...";
387 kError() <<
"No LDAP support...";
393 kError() <<
"No LDAP support...";
399 kError() <<
"No LDAP support...";
405 kError() <<
"No LDAP support...";
411 kError() <<
"No LDAP support...";
417 kError() <<
"No LDAP support...";
423 d->mConnectionError =
424 i18n(
"LDAP support not compiled in. Please recompile libkldap with the "
425 "OpenLDAP (or compatible) client libraries, or complain to your "
426 "distribution packagers." );
427 kError() <<
"No LDAP support...";
433 kError() <<
"No LDAP support...";
const LdapServer & server() const
Returns the connection parameters which was specified with an LDAP Url or a LdapServer structure...
int timeLimit() const
Returns the current time limit.
int sizeLimit() const
Returns the current size limit.
void * saslHandle() const
Returns the opaqe sasl-library specific SASL object.
A special url class for LDAP.
void * handle() const
Returns the opaqe client-library specific LDAP object.
int ldapErrorCode() const
Returns the LDAP error code from the last operation.
QString ldapErrorString() const
Returns the LDAP error string from the last operation.
QString number(int n, int base)
QString fromLocal8Bit(const char *str, int size)
QString fromUtf8(const char *str, int size)
void setServer(const LdapServer &server)
Sets the connection parameters via the specified server structure.
void setUrl(const LdapUrl &url)
Sets the connection parameters via the specified url.
A class that contains LDAP server connection settings.
int connect()
Sets up the connection parameters with creating a handle to the LDAP server.
QString saslErrorString() const
Returns the SASL error string from the last SASL operation.
bool setSizeLimit(int sizelimit)
Sets the size limit for the connection.
static QString errorString(int code)
Returns a translated error message from the specified LDAP error code.
int getOption(int option, void *value) const
Gets an option from the connection.
bool setTimeLimit(int timelimit)
Sets the time limit for the connection.
QString connectionError() const
Returns a translated error string if connect() failed.
void close()
Closes the LDAP connection.
LdapConnection()
Constructs an LdapConnection object.
int setOption(int option, void *value)
Sets an option in the connection.