You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
428 lines
10 KiB
428 lines
10 KiB
15 years ago
|
/* kldapclient.cpp - LDAP access
|
||
|
* Copyright (C) 2002 Klarälvdalens Datakonsult AB
|
||
|
*
|
||
|
* Author: Steffen Hansen <hansen@kde.org>
|
||
|
*
|
||
|
* Ported to KABC by Daniel Molkentin <molkentin@kde.org>
|
||
|
*
|
||
|
* This file is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 2 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This file 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 Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||
|
*/
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqimage.h>
|
||
|
#include <tqlabel.h>
|
||
|
#include <tqpixmap.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <tqurl.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kprotocolinfo.h>
|
||
|
|
||
|
#include "ldapclient.h"
|
||
|
#include "ldif.h"
|
||
|
#include "ldapurl.h"
|
||
|
|
||
|
using namespace KABC;
|
||
|
|
||
|
class LdapClient::LdapClientPrivate{
|
||
|
public:
|
||
15 years ago
|
TQString bindDN;
|
||
|
TQString pwdBindDN;
|
||
15 years ago
|
LDIF ldif;
|
||
|
};
|
||
|
|
||
15 years ago
|
TQString LdapObject::toString() const
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result = TQString::fromLatin1( "\ndn: %1\n" ).arg( dn );
|
||
15 years ago
|
for ( LdapAttrMap::ConstIterator it = attrs.begin(); it != attrs.end(); ++it ) {
|
||
15 years ago
|
TQString attr = it.key();
|
||
15 years ago
|
for ( LdapAttrValue::ConstIterator it2 = (*it).begin(); it2 != (*it).end(); ++it2 ) {
|
||
15 years ago
|
result += TQString::fromUtf8( LDIF::assembleLine( attr, *it2, 76 ) ) + "\n";
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
void LdapObject::clear()
|
||
|
{
|
||
15 years ago
|
dn = TQString::null;
|
||
15 years ago
|
attrs.clear();
|
||
|
}
|
||
|
|
||
|
void LdapObject::assign( const LdapObject& that )
|
||
|
{
|
||
|
if ( &that != this ) {
|
||
|
dn = that.dn;
|
||
|
attrs = that.attrs;
|
||
|
client = that.client;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
LdapClient::LdapClient( TQObject* parent, const char* name )
|
||
|
: TQObject( parent, name ), mJob( 0 ), mActive( false )
|
||
15 years ago
|
{
|
||
|
d = new LdapClientPrivate;
|
||
|
}
|
||
|
|
||
|
LdapClient::~LdapClient()
|
||
|
{
|
||
|
cancelQuery();
|
||
|
delete d; d = 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setHost( const TQString& host )
|
||
15 years ago
|
{
|
||
|
mHost = host;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setPort( const TQString& port )
|
||
15 years ago
|
{
|
||
|
mPort = port;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setBase( const TQString& base )
|
||
15 years ago
|
{
|
||
|
mBase = base;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setBindDN( const TQString& bindDN )
|
||
15 years ago
|
{
|
||
|
d->bindDN = bindDN;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setPwdBindDN( const TQString& pwdBindDN )
|
||
15 years ago
|
{
|
||
|
d->pwdBindDN = pwdBindDN;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::setAttrs( const TQStringList& attrs )
|
||
15 years ago
|
{
|
||
|
mAttrs = attrs;
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::startQuery( const TQString& filter )
|
||
15 years ago
|
{
|
||
|
cancelQuery();
|
||
|
LDAPUrl url;
|
||
|
|
||
|
url.setProtocol( "ldap" );
|
||
|
url.setUser( d->bindDN );
|
||
|
url.setPass( d->pwdBindDN );
|
||
|
url.setHost( mHost );
|
||
|
url.setPort( mPort.toUInt() );
|
||
|
url.setDn( mBase );
|
||
|
url.setAttributes( mAttrs );
|
||
|
url.setScope( mScope == "one" ? LDAPUrl::One : LDAPUrl::Sub );
|
||
|
url.setFilter( "("+filter+")" );
|
||
|
|
||
|
kdDebug(5700) << "Doing query: " << url.prettyURL() << endl;
|
||
|
|
||
|
startParseLDIF();
|
||
|
mActive = true;
|
||
12 years ago
|
mJob = TDEIO::get( url, false, false );
|
||
|
connect( mJob, TQT_SIGNAL( data( TDEIO::Job*, const TQByteArray& ) ),
|
||
|
this, TQT_SLOT( slotData( TDEIO::Job*, const TQByteArray& ) ) );
|
||
|
connect( mJob, TQT_SIGNAL( infoMessage( TDEIO::Job*, const TQString& ) ),
|
||
|
this, TQT_SLOT( slotInfoMessage( TDEIO::Job*, const TQString& ) ) );
|
||
|
connect( mJob, TQT_SIGNAL( result( TDEIO::Job* ) ),
|
||
15 years ago
|
this, TQT_SLOT( slotDone() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void LdapClient::cancelQuery()
|
||
|
{
|
||
|
if ( mJob ) {
|
||
|
mJob->kill();
|
||
|
mJob = 0;
|
||
|
}
|
||
|
|
||
|
mActive = false;
|
||
|
}
|
||
|
|
||
12 years ago
|
void LdapClient::slotData( TDEIO::Job*, const TQByteArray& data )
|
||
15 years ago
|
{
|
||
|
#ifndef NDEBUG // don't create the QString
|
||
15 years ago
|
// TQString str( data );
|
||
15 years ago
|
// kdDebug(5700) << "LdapClient: Got \"" << str << "\"\n";
|
||
|
#endif
|
||
|
parseLDIF( data );
|
||
|
}
|
||
|
|
||
12 years ago
|
void LdapClient::slotInfoMessage( TDEIO::Job*, const TQString & )
|
||
15 years ago
|
{
|
||
13 years ago
|
//tqDebug("Job said \"%s\"", info.latin1());
|
||
15 years ago
|
}
|
||
|
|
||
|
void LdapClient::slotDone()
|
||
|
{
|
||
|
endParseLDIF();
|
||
|
mActive = false;
|
||
|
#if 0
|
||
15 years ago
|
for ( TQValueList<LdapObject>::Iterator it = mObjects.begin(); it != mObjects.end(); ++it ) {
|
||
13 years ago
|
tqDebug( (*it).toString().latin1() );
|
||
15 years ago
|
}
|
||
|
#endif
|
||
|
int err = mJob->error();
|
||
12 years ago
|
if ( err && err != TDEIO::ERR_USER_CANCELED ) {
|
||
|
emit error( TDEIO::buildErrorString( err, TQString("%1:%2").arg( mHost ).arg( mPort ) ) );
|
||
15 years ago
|
}
|
||
|
emit done();
|
||
|
}
|
||
|
|
||
|
void LdapClient::startParseLDIF()
|
||
|
{
|
||
|
mCurrentObject.clear();
|
||
|
mLastAttrName = 0;
|
||
|
mLastAttrValue = 0;
|
||
|
mIsBase64 = false;
|
||
|
d->ldif.startParsing();
|
||
|
}
|
||
|
|
||
|
void LdapClient::endParseLDIF()
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapClient::parseLDIF( const TQByteArray& data )
|
||
15 years ago
|
{
|
||
|
if ( data.size() ) {
|
||
|
d->ldif.setLDIF( data );
|
||
|
} else {
|
||
|
d->ldif.endLDIF();
|
||
|
}
|
||
|
|
||
|
LDIF::ParseVal ret;
|
||
15 years ago
|
TQString name;
|
||
15 years ago
|
do {
|
||
|
ret = d->ldif.nextItem();
|
||
|
switch ( ret ) {
|
||
|
case LDIF::Item:
|
||
|
{
|
||
|
name = d->ldif.attr();
|
||
15 years ago
|
// Must make a copy! TQByteArray is explicitely shared
|
||
|
TQByteArray value = d->ldif.val().copy();
|
||
15 years ago
|
mCurrentObject.attrs[ name ].append( value );
|
||
|
break;
|
||
|
}
|
||
|
case LDIF::EndEntry:
|
||
|
mCurrentObject.dn = d->ldif.dn();
|
||
|
mCurrentObject.client = this;
|
||
|
emit result( mCurrentObject );
|
||
|
mCurrentObject.clear();
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
} while ( ret != LDIF::MoreData );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LdapClient::bindDN() const
|
||
15 years ago
|
{
|
||
|
return d->bindDN;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LdapClient::pwdBindDN() const
|
||
15 years ago
|
{
|
||
|
return d->pwdBindDN;
|
||
|
}
|
||
|
|
||
|
LdapSearch::LdapSearch()
|
||
|
: mActiveClients( 0 ), mNoLDAPLookup( false )
|
||
|
{
|
||
|
if ( !KProtocolInfo::isKnownProtocol( KURL("ldap://localhost") ) ) {
|
||
|
mNoLDAPLookup = true;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// stolen from KAddressBook
|
||
12 years ago
|
TDEConfig config( "kabldaprc", true );
|
||
15 years ago
|
config.setGroup( "LDAP" );
|
||
|
int numHosts = config.readUnsignedNumEntry( "NumSelectedHosts");
|
||
|
if ( !numHosts ) {
|
||
|
mNoLDAPLookup = true;
|
||
|
return;
|
||
|
} else {
|
||
|
for ( int j = 0; j < numHosts; j++ ) {
|
||
|
LdapClient* ldapClient = new LdapClient( this );
|
||
|
|
||
15 years ago
|
TQString host = config.readEntry( TQString( "SelectedHost%1" ).arg( j ), "" ).stripWhiteSpace();
|
||
15 years ago
|
if ( !host.isEmpty() )
|
||
|
ldapClient->setHost( host );
|
||
|
|
||
15 years ago
|
TQString port = TQString::number( config.readUnsignedNumEntry( TQString( "SelectedPort%1" ).arg( j ) ) );
|
||
15 years ago
|
if ( !port.isEmpty() )
|
||
|
ldapClient->setPort( port );
|
||
|
|
||
15 years ago
|
TQString base = config.readEntry( TQString( "SelectedBase%1" ).arg( j ), "" ).stripWhiteSpace();
|
||
15 years ago
|
if ( !base.isEmpty() )
|
||
|
ldapClient->setBase( base );
|
||
|
|
||
15 years ago
|
TQString bindDN = config.readEntry( TQString( "SelectedBind%1" ).arg( j ) ).stripWhiteSpace();
|
||
15 years ago
|
if ( !bindDN.isEmpty() )
|
||
|
ldapClient->setBindDN( bindDN );
|
||
|
|
||
15 years ago
|
TQString pwdBindDN = config.readEntry( TQString( "SelectedPwdBind%1" ).arg( j ) );
|
||
15 years ago
|
if ( !pwdBindDN.isEmpty() )
|
||
|
ldapClient->setPwdBindDN( pwdBindDN );
|
||
|
|
||
15 years ago
|
TQStringList attrs;
|
||
15 years ago
|
attrs << "cn" << "mail" << "givenname" << "sn";
|
||
|
ldapClient->setAttrs( attrs );
|
||
|
|
||
15 years ago
|
connect( ldapClient, TQT_SIGNAL( result( const KABC::LdapObject& ) ),
|
||
|
this, TQT_SLOT( slotLDAPResult( const KABC::LdapObject& ) ) );
|
||
|
connect( ldapClient, TQT_SIGNAL( done() ),
|
||
|
this, TQT_SLOT( slotLDAPDone() ) );
|
||
|
connect( ldapClient, TQT_SIGNAL( error( const TQString& ) ),
|
||
|
this, TQT_SLOT( slotLDAPError( const TQString& ) ) );
|
||
15 years ago
|
|
||
|
mClients.append( ldapClient );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
connect( &mDataTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotDataTimer() ) );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void LdapSearch::startSearch( const TQString& txt )
|
||
15 years ago
|
{
|
||
|
if ( mNoLDAPLookup )
|
||
|
return;
|
||
|
|
||
|
cancelSearch();
|
||
|
|
||
13 years ago
|
int pos = txt.find( '\"' );
|
||
15 years ago
|
if( pos >= 0 )
|
||
|
{
|
||
|
++pos;
|
||
13 years ago
|
int pos2 = txt.find( '\"', pos );
|
||
15 years ago
|
if( pos2 >= 0 )
|
||
|
mSearchText = txt.mid( pos , pos2 - pos );
|
||
|
else
|
||
|
mSearchText = txt.mid( pos );
|
||
|
} else
|
||
|
mSearchText = txt;
|
||
|
|
||
15 years ago
|
TQString filter = TQString( "|(cn=%1*)(mail=%2*)(givenName=%3*)(sn=%4*)" )
|
||
15 years ago
|
.arg( mSearchText ).arg( mSearchText ).arg( mSearchText ).arg( mSearchText );
|
||
|
|
||
15 years ago
|
TQValueList< LdapClient* >::Iterator it;
|
||
15 years ago
|
for ( it = mClients.begin(); it != mClients.end(); ++it ) {
|
||
|
(*it)->startQuery( filter );
|
||
|
++mActiveClients;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void LdapSearch::cancelSearch()
|
||
|
{
|
||
15 years ago
|
TQValueList< LdapClient* >::Iterator it;
|
||
15 years ago
|
for ( it = mClients.begin(); it != mClients.end(); ++it )
|
||
|
(*it)->cancelQuery();
|
||
|
|
||
|
mActiveClients = 0;
|
||
|
mResults.clear();
|
||
|
}
|
||
|
|
||
|
void LdapSearch::slotLDAPResult( const KABC::LdapObject& obj )
|
||
|
{
|
||
|
mResults.append( obj );
|
||
|
if ( !mDataTimer.isActive() )
|
||
|
mDataTimer.start( 500, true );
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapSearch::slotLDAPError( const TQString& )
|
||
15 years ago
|
{
|
||
|
slotLDAPDone();
|
||
|
}
|
||
|
|
||
|
void LdapSearch::slotLDAPDone()
|
||
|
{
|
||
|
if ( --mActiveClients > 0 )
|
||
|
return;
|
||
|
|
||
|
finish();
|
||
|
}
|
||
|
|
||
|
void LdapSearch::slotDataTimer()
|
||
|
{
|
||
15 years ago
|
TQStringList lst;
|
||
15 years ago
|
LdapResultList reslist;
|
||
|
makeSearchData( lst, reslist );
|
||
|
if ( !lst.isEmpty() )
|
||
|
emit searchData( lst );
|
||
|
if ( !reslist.isEmpty() )
|
||
|
emit searchData( reslist );
|
||
|
}
|
||
|
|
||
|
void LdapSearch::finish()
|
||
|
{
|
||
|
mDataTimer.stop();
|
||
|
|
||
|
slotDataTimer(); // emit final bunch of data
|
||
|
emit searchDone();
|
||
|
}
|
||
|
|
||
15 years ago
|
void LdapSearch::makeSearchData( TQStringList& ret, LdapResultList& resList )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString search_text_upper = mSearchText.upper();
|
||
15 years ago
|
|
||
15 years ago
|
TQValueList< KABC::LdapObject >::ConstIterator it1;
|
||
15 years ago
|
for ( it1 = mResults.begin(); it1 != mResults.end(); ++it1 ) {
|
||
15 years ago
|
TQString name, mail, givenname, sn;
|
||
15 years ago
|
|
||
|
LdapAttrMap::ConstIterator it2;
|
||
|
for ( it2 = (*it1).attrs.begin(); it2 != (*it1).attrs.end(); ++it2 ) {
|
||
15 years ago
|
TQString tmp = TQString::fromUtf8( (*it2).first(), (*it2).first().size() );
|
||
15 years ago
|
if ( it2.key() == "cn" )
|
||
|
name = tmp; // TODO loop?
|
||
|
else if( it2.key() == "mail" )
|
||
|
mail = tmp;
|
||
|
else if( it2.key() == "givenName" )
|
||
|
givenname = tmp;
|
||
|
else if( it2.key() == "sn" )
|
||
|
sn = tmp;
|
||
|
}
|
||
|
|
||
|
if( mail.isEmpty())
|
||
|
continue; // nothing, bad entry
|
||
|
else if ( name.isEmpty() )
|
||
|
ret.append( mail );
|
||
|
else {
|
||
|
kdDebug(5700) << "<" << name << "><" << mail << ">" << endl;
|
||
15 years ago
|
ret.append( TQString( "%1 <%2>" ).arg( name ).arg( mail ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
LdapResult sr;
|
||
13 years ago
|
sr.clientNumber = mClients.findIndex( (*it1).client );
|
||
15 years ago
|
sr.name = name;
|
||
|
sr.email = mail;
|
||
|
resList.append( sr );
|
||
|
}
|
||
|
|
||
|
mResults.clear();
|
||
|
}
|
||
|
|
||
|
bool LdapSearch::isAvailable() const
|
||
|
{
|
||
|
return !mNoLDAPLookup;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#include "ldapclient.moc"
|