00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ldapserver.h"
00022
00023 #include <kdebug.h>
00024
00025 using namespace KLDAP;
00026
00027 class LdapServer::LdapServerPrivate
00028 {
00029 public:
00030 QString mHost;
00031 int mPort;
00032 LdapDN mBaseDn;
00033 QString mUser;
00034 QString mBindDn;
00035 QString mRealm;
00036 QString mPassword;
00037 QString mMech;
00038 QString mFilter;
00039 int mTimeLimit, mSizeLimit, mVersion, mPageSize, mTimeout;
00040 Security mSecurity;
00041 Auth mAuth;
00042 LdapUrl::Scope mScope;
00043 };
00044
00045 LdapServer::LdapServer()
00046 : d( new LdapServerPrivate )
00047 {
00048 clear();
00049 }
00050
00051 LdapServer::LdapServer( const LdapUrl &url )
00052 : d( new LdapServerPrivate )
00053 {
00054 clear();
00055
00056 setUrl( url );
00057 }
00058
00059 LdapServer::LdapServer( const LdapServer &that )
00060 : d( new LdapServerPrivate )
00061 {
00062 *d = *that.d;
00063 }
00064
00065 LdapServer &LdapServer::operator= ( const LdapServer &that )
00066 {
00067 if ( this == &that ) {
00068 return *this;
00069 }
00070
00071 *d = *that.d;
00072
00073 return *this;
00074 }
00075
00076 LdapServer::~LdapServer()
00077 {
00078 delete d;
00079 }
00080
00081 void LdapServer::clear()
00082 {
00083 d->mPort = 389;
00084 d->mHost.clear();
00085 d->mUser.clear();
00086 d->mBindDn.clear();
00087 d->mMech.clear();
00088 d->mPassword.clear();
00089 d->mSecurity = None;
00090 d->mAuth = Anonymous;
00091 d->mVersion = 3;
00092 d->mTimeout = 0;
00093 d->mSizeLimit = d->mTimeLimit = d->mPageSize = 0;
00094 }
00095
00096 QString LdapServer::host() const
00097 {
00098 return d->mHost;
00099 }
00100
00101 int LdapServer::port() const
00102 {
00103 return d->mPort;
00104 }
00105
00106 LdapDN LdapServer::baseDn() const
00107 {
00108 return d->mBaseDn;
00109 }
00110
00111 QString LdapServer::user() const
00112 {
00113 return d->mUser;
00114 }
00115
00116 QString LdapServer::bindDn() const
00117 {
00118 return d->mBindDn;
00119 }
00120
00121 QString LdapServer::realm() const
00122 {
00123 return d->mRealm;
00124 }
00125
00126 QString LdapServer::password() const
00127 {
00128 return d->mPassword;
00129 }
00130
00131 QString LdapServer::filter() const
00132 {
00133 return d->mFilter;
00134 }
00135
00136 LdapUrl::Scope LdapServer::scope() const
00137 {
00138 return d->mScope;
00139 }
00140
00141 int LdapServer::timeLimit() const
00142 {
00143 return d->mTimeLimit;
00144 }
00145
00146 int LdapServer::sizeLimit() const
00147 {
00148 return d->mSizeLimit;
00149 }
00150
00151 int LdapServer::pageSize() const
00152 {
00153 return d->mPageSize;
00154 }
00155
00156 int LdapServer::version() const
00157 {
00158 return d->mVersion;
00159 }
00160
00161 LdapServer::Security LdapServer::security() const
00162 {
00163 return d->mSecurity;
00164 }
00165
00166 LdapServer::Auth LdapServer::auth() const
00167 {
00168 return d->mAuth;
00169 }
00170
00171 QString LdapServer::mech() const
00172 {
00173 return d->mMech;
00174 }
00175
00176 int LdapServer::timeout() const
00177 {
00178 return d->mTimeout;
00179 }
00180
00181 void LdapServer::setHost( const QString &host )
00182 {
00183 d->mHost = host;
00184 }
00185
00186 void LdapServer::setPort( int port )
00187 {
00188 d->mPort = port;
00189 }
00190
00191 void LdapServer::setBaseDn( const LdapDN &baseDn )
00192 {
00193 d->mBaseDn = baseDn;
00194 }
00195
00196 void LdapServer::setUser( const QString &user )
00197 {
00198 d->mUser = user;
00199 }
00200
00201 void LdapServer::setBindDn( const QString &bindDn )
00202 {
00203 d->mBindDn = bindDn;
00204 }
00205
00206 void LdapServer::setRealm( const QString &realm )
00207 {
00208 d->mRealm = realm;
00209 }
00210
00211 void LdapServer::setPassword( const QString &password )
00212 {
00213 d->mPassword = password;
00214 }
00215
00216 void LdapServer::setTimeLimit( int timelimit )
00217 {
00218 d->mTimeLimit = timelimit;
00219 }
00220
00221 void LdapServer::setSizeLimit( int sizelimit )
00222 {
00223 d->mSizeLimit = sizelimit;
00224 }
00225
00226 void LdapServer::setPageSize( int pagesize )
00227 {
00228 d->mPageSize = pagesize;
00229 }
00230
00231 void LdapServer::setFilter( const QString &filter )
00232 {
00233 d->mFilter = filter;
00234 }
00235
00236 void LdapServer::setScope( LdapUrl::Scope scope )
00237 {
00238 d->mScope = scope;
00239 }
00240
00241 void LdapServer::setVersion( int version )
00242 {
00243 d->mVersion = version;
00244 }
00245
00246 void LdapServer::setSecurity( Security security )
00247 {
00248 d->mSecurity = security;
00249 }
00250
00251 void LdapServer::setAuth( Auth auth )
00252 {
00253 d->mAuth = auth;
00254 }
00255
00256 void LdapServer::setMech( const QString &mech )
00257 {
00258 d->mMech = mech;
00259 }
00260
00261 void LdapServer::setTimeout( int timeout )
00262 {
00263 d->mTimeout = timeout;
00264 }
00265
00266 void LdapServer::setUrl( const LdapUrl &url )
00267 {
00268 bool critical;
00269
00270 d->mHost = url.host();
00271 int port = url.port();
00272 if ( port <= 0 ) {
00273 d->mPort = 389;
00274 } else {
00275 d->mPort = port;
00276 }
00277 d->mBaseDn = url.dn();
00278 d->mScope = url.scope();
00279
00280 d->mFilter = url.filter();
00281
00282 d->mSecurity = None;
00283 if ( url.protocol() == "ldaps" ) {
00284 d->mSecurity = SSL;
00285 } else if ( url.hasExtension( "x-tls" ) ) {
00286 d->mSecurity = TLS;
00287 }
00288 kDebug() << "security:" << d->mSecurity;
00289
00290 d->mMech.clear();
00291 d->mUser.clear();
00292 d->mBindDn.clear();
00293 if ( url.hasExtension( "x-sasl" ) ) {
00294 d->mAuth = SASL;
00295 if ( url.hasExtension( "x-mech" ) ) {
00296 d->mMech = url.extension( "x-mech", critical );
00297 }
00298 if ( url.hasExtension( "x-realm" ) ) {
00299 d->mRealm = url.extension( "x-realm", critical );
00300 }
00301 if ( url.hasExtension( "bindname" ) ) {
00302 d->mBindDn = url.extension( "bindname", critical );
00303 }
00304 d->mUser = url.user();
00305 } else if ( url.hasExtension( "bindname" ) ) {
00306 d->mAuth = Simple;
00307 d->mBindDn = url.extension( "bindname", critical );
00308 } else {
00309 QString user = url.user();
00310 if ( user.isEmpty() ) {
00311 d->mAuth = Anonymous;
00312 } else {
00313 d->mAuth = Simple;
00314 d->mBindDn = user;
00315 }
00316 }
00317 d->mPassword = url.password();
00318 if ( url.hasExtension( "x-version" ) ) {
00319 d->mVersion = url.extension( "x-version", critical ).toInt();
00320 } else {
00321 d->mVersion = 3;
00322 }
00323
00324 if ( url.hasExtension( "x-timeout" ) ) {
00325 d->mTimeout = url.extension( "x-timeout", critical ).toInt();
00326 } else {
00327 d->mTimeout = 0;
00328 }
00329
00330 if ( url.hasExtension( "x-timelimit" ) ) {
00331 d->mTimeLimit = url.extension( "x-timelimit", critical ).toInt();
00332 } else {
00333 d->mTimeLimit = 0;
00334 }
00335
00336 if ( url.hasExtension( "x-sizelimit" ) ) {
00337 d->mSizeLimit = url.extension( "x-sizelimit", critical ).toInt();
00338 } else {
00339 d->mSizeLimit = 0;
00340 }
00341
00342 if ( url.hasExtension( "x-pagesize" ) ) {
00343 d->mPageSize = url.extension( "x-pagesize", critical ).toInt();
00344 } else {
00345 d->mPageSize = 0;
00346 }
00347 }
00348
00349 LdapUrl LdapServer::url() const
00350 {
00351 LdapUrl url;
00352 url.setProtocol( d->mSecurity == SSL ? "ldaps" : "ldap" );
00353 url.setPort( d->mPort );
00354 url.setHost( d->mHost );
00355 url.setPassword( d->mPassword );
00356 url.setDn( d->mBaseDn );
00357 url.setFilter( d->mFilter );
00358 url.setScope( d->mScope );
00359 if ( d->mAuth == SASL ) {
00360 url.setUser( d->mUser );
00361 url.setExtension( "bindname", d->mBindDn, true );
00362 url.setExtension( "x-sasl", QString() );
00363 if ( !d->mMech.isEmpty() ) {
00364 url.setExtension( "x-mech", d->mMech );
00365 }
00366 if ( !d->mRealm.isEmpty() ) {
00367 url.setExtension( "x-realm", d->mRealm );
00368 }
00369 } else {
00370 url.setUser( d->mBindDn );
00371 }
00372 if ( d->mVersion == 2 ) {
00373 url.setExtension( "x-version", d->mVersion );
00374 }
00375 if ( d->mTimeout ) {
00376 url.setExtension( "x-timeout", d->mTimeout );
00377 }
00378 if ( d->mTimeLimit != 0 ) {
00379 url.setExtension( "x-timelimit", d->mTimeLimit );
00380 }
00381 if ( d->mSizeLimit != 0 ) {
00382 url.setExtension( "x-sizelimit", d->mSizeLimit );
00383 }
00384 if ( d->mPageSize != 0 ) {
00385 url.setExtension( "x-pagesize", d->mPageSize );
00386 }
00387 if ( d->mSecurity == TLS ) {
00388 url.setExtension( "x-tls", 1, true );
00389 }
00390
00391 return url;
00392 }