21 #include "ldapsearch.h"
25 #include <QtCore/QEventLoop>
26 #include <QtCore/QTimer>
29 #include <KLocalizedString>
30 using namespace KLDAP;
33 #define LDAPSEARCH_BLOCKING_TIMEOUT 10
35 class LdapSearch::Private
45 void closeConnection();
48 int pagesize,
int count );
53 bool mOwnConnection, mAbandoned;
62 int mCount, mMaxCount;
66 void LdapSearch::Private::result()
72 int res = mOp.waitForResult( mId, LDAPSEARCH_BLOCKING_TIMEOUT );
74 kDebug() <<
"LDAP result:" << res;
78 ( mConn->ldapErrorCode() != KLDAP_SUCCESS &&
79 mConn->ldapErrorCode() != KLDAP_SASL_BIND_IN_PROGRESS ) ) ) {
81 mError = mConn->ldapErrorCode();
82 mErrorString = mConn->ldapErrorString();
83 emit mParent->result( mParent );
88 if ( res == LdapOperation::RES_BIND ) {
91 servercc = mOp.serverCred();
93 kDebug() <<
"LdapSearch RES_BIND";
94 if ( mConn->ldapErrorCode() == KLDAP_SUCCESS ) {
95 kDebug() <<
"bind succeeded";
100 mOp.setServerControls( ctrls );
103 mId = mOp.search( mBase, mScope, mFilter, mAttributes );
104 mOp.setServerControls( savedctrls );
106 kDebug() <<
"bind next step";
107 mId = mOp.bind( servercc );
110 if ( mId == KLDAP_SASL_ERROR ) {
112 mErrorString = mConn->saslErrorString();
114 mError = mConn->ldapErrorCode();
115 mErrorString = mConn->ldapErrorString();
117 emit mParent->result( mParent );
125 if ( res == LdapOperation::RES_SEARCH_RESULT ) {
129 const int numberOfControls( mOp.controls().count() );
130 for (
int i = 0; i < numberOfControls; ++i ) {
131 estsize = mOp.controls()[i].parsePageControl( cookie );
132 if ( estsize != -1 ) {
136 kDebug() <<
" estimated size:" << estsize;
137 if ( estsize != -1 && !cookie.
isEmpty() ) {
139 savedctrls = mOp.serverControls();
142 mOp.setServerControls( ctrls );
143 mId = mOp.search( mBase, mScope, mFilter, mAttributes );
144 mOp.setServerControls( savedctrls );
146 mError = mConn->ldapErrorCode();
147 mErrorString = mConn->ldapErrorString();
148 emit mParent->result( mParent );
157 emit mParent->result( mParent );
162 if ( res == LdapOperation::RES_SEARCH_ENTRY ) {
163 emit mParent->data( mParent, mOp.object() );
168 if ( mMaxCount <= 0 || mCount < mMaxCount ) {
172 if ( mMaxCount > 0 && mCount == mMaxCount ) {
173 kDebug() << mCount <<
" entries reached";
174 emit mParent->result( mParent );
178 bool LdapSearch::Private::connect()
180 int ret = mConn->connect();
181 if ( ret != KLDAP_SUCCESS ) {
183 mErrorString = mConn->connectionError();
190 void LdapSearch::Private::closeConnection()
192 if ( mOwnConnection && mConn ) {
199 bool LdapSearch::Private::startSearch(
const LdapDN &base,
LdapUrl::Scope scope,
201 const QStringList &attributes,
int pagesize,
int count )
203 kDebug() <<
"search: base=" << base.toString() <<
"scope=" << (int)scope
204 <<
"filter=" << filter <<
"attributes=" << attributes
205 <<
"pagesize=" << pagesize;
208 mErrorString.
clear();
209 mOp.setConnection( *mConn );
210 mPageSize = pagesize;
214 mAttributes = attributes;
222 mConn->setOption( 0x0008, NULL );
224 mOp.setServerControls( ctrls );
229 if ( mId == KLDAP_SASL_ERROR ) {
231 mErrorString = mConn->saslErrorString();
233 mError = mConn->ldapErrorCode();
234 mErrorString = mConn->ldapErrorString();
235 if ( mError == -1 && mErrorString.isEmpty() ) {
236 mErrorString = i18n(
"Cannot access to server. Please reconfigure it." );
241 kDebug() <<
"startSearch msg id=" << mId;
252 : d( new Private( this ) )
254 d->mOwnConnection =
true;
259 : d( new Private( this ) )
261 d->mOwnConnection =
false;
262 d->mConn = &connection;
265 LdapSearch::~LdapSearch()
267 d->closeConnection();
273 d->closeConnection();
274 d->mOwnConnection =
false;
275 d->mConn = &connection;
280 d->mOp.setClientControls( ctrls );
285 d->mOp.setServerControls( ctrls );
291 if ( d->mOwnConnection ) {
292 d->closeConnection();
294 if ( !d->connect() ) {
299 attributes, server.
pageSize(), count );
304 if ( d->mOwnConnection ) {
305 d->closeConnection();
307 if ( !d->connect() ) {
311 bool critical =
true;
319 int pagesize,
int count )
321 Q_ASSERT( !d->mOwnConnection );
322 return d->startSearch( base, scope, filter, attributes, pagesize, count );
327 Q_ASSERT( !d->mFinished );
339 d->mAbandoned =
true;
349 return d->mErrorString;
352 #include "moc_ldapsearch.cpp"
LdapDN baseDn() const
Returns the baseDn of the LDAP connection.
void setServerControls(const LdapControls &ctrls)
Sets the server controls which will sent with each operation.
void setConnection(LdapConnection &connection)
Sets the connection for this object to use for searches from now onwards, regardless of the LDAP Url ...
bool search(const LdapServer &server, const QStringList &attributes=QStringList(), int count=0)
Starts a search operation on the LDAP server.
void abandon()
Tries to abandon the search.
QString filter() const
Returns the filter part of the LDAP url.
QStringList attributes() const
Returns the attributes part of the LDAP url.
static LdapControl createPageControl(int pagesize, const QByteArray &cookie=QByteArray())
Creates a paging search control.
LdapSearch()
Constructs an LdapSearch object.
A special url class for LDAP.
void continueSearch()
Continues the search (if you set count to non-zero in search(), and isFinished() is false) ...
QString errorString() const
Returns the error description of the search operation.
Scope
Describes the scope of the LDAP url.
int pageSize() const
Returns the page size of the LDAP connection.
Scope scope() const
Returns the scope part of the LDAP url.
LdapUrl::Scope scope() const
Returns the search scope of the LDAP connection.
A class that contains LDAP server connection settings.
This class represents a connection to an LDAP server.
void result(KLDAP::LdapSearch *search)
Emitted when the searching finished.
This class starts a search operation on a LDAP server and returns the search values via a Qt signal...
void setClientControls(const LdapControls &ctrls)
Sets the client controls which will sent with each operation.
LdapDN dn() const
Returns the dn part of the LDAP url.
This class allows sending an ldap operation (search, rename, modify, delete, compare, exop) to an LDAP server.
bool isFinished()
Returns true if the search is finished else returns false.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Extension extension(const QString &extension) const
Returns the specified extension.
static void insert(LdapControls &list, const LdapControl &ctrl)
Inserts a unique control against a list of controls.
int error() const
Returns the error code of the search operation (0 if no error).
QString filter() const
Returns the filter string of the LDAP connection.