1/*
2 This file is part of libkldap.
3 Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "ldapserver.h"
22
23#include <kdebug.h>
24
25using namespace KLDAP;
26
27class LdapServer::LdapServerPrivate
28{
29 public:
30 QString mHost;
31 int mPort;
32 LdapDN mBaseDn;
33 QString mUser;
34 QString mBindDn;
35 QString mRealm;
36 QString mPassword;
37 QString mMech;
38 QString mFilter;
39 int mTimeLimit, mSizeLimit, mVersion, mPageSize, mTimeout;
40 Security mSecurity;
41 Auth mAuth;
42 LdapUrl::Scope mScope;
43};
44
45LdapServer::LdapServer()
46 : d( new LdapServerPrivate )
47{
48 clear();
49}
50
51LdapServer::LdapServer( const LdapUrl &url )
52 : d( new LdapServerPrivate )
53{
54 clear();
55
56 setUrl( url );
57}
58
59LdapServer::LdapServer( const LdapServer &that )
60 : d( new LdapServerPrivate )
61{
62 *d = *that.d;
63}
64
65LdapServer &LdapServer::operator= ( const LdapServer &that )
66{
67 if ( this == &that ) {
68 return *this;
69 }
70
71 *d = *that.d;
72
73 return *this;
74}
75
76LdapServer::~LdapServer()
77{
78 delete d;
79}
80
81void LdapServer::clear()
82{
83 d->mPort = 389;
84 d->mHost.clear();
85 d->mUser.clear();
86 d->mBindDn.clear();
87 d->mMech.clear();
88 d->mPassword.clear();
89 d->mSecurity = None;
90 d->mAuth = Anonymous;
91 d->mVersion = 3;
92 d->mTimeout = 0;
93 d->mSizeLimit = d->mTimeLimit = d->mPageSize = 0;
94}
95
96QString LdapServer::host() const
97{
98 return d->mHost;
99}
100
101int LdapServer::port() const
102{
103 return d->mPort;
104}
105
106LdapDN LdapServer::baseDn() const
107{
108 return d->mBaseDn;
109}
110
111QString LdapServer::user() const
112{
113 return d->mUser;
114}
115
116QString LdapServer::bindDn() const
117{
118 return d->mBindDn;
119}
120
121QString LdapServer::realm() const
122{
123 return d->mRealm;
124}
125
126QString LdapServer::password() const
127{
128 return d->mPassword;
129}
130
131QString LdapServer::filter() const
132{
133 return d->mFilter;
134}
135
136LdapUrl::Scope LdapServer::scope() const
137{
138 return d->mScope;
139}
140
141int LdapServer::timeLimit() const
142{
143 return d->mTimeLimit;
144}
145
146int LdapServer::sizeLimit() const
147{
148 return d->mSizeLimit;
149}
150
151int LdapServer::pageSize() const
152{
153 return d->mPageSize;
154}
155
156int LdapServer::version() const
157{
158 return d->mVersion;
159}
160
161LdapServer::Security LdapServer::security() const
162{
163 return d->mSecurity;
164}
165
166LdapServer::Auth LdapServer::auth() const
167{
168 return d->mAuth;
169}
170
171QString LdapServer::mech() const
172{
173 return d->mMech;
174}
175
176int LdapServer::timeout() const
177{
178 return d->mTimeout;
179}
180
181void LdapServer::setHost( const QString &host )
182{
183 d->mHost = host;
184}
185
186void LdapServer::setPort( int port )
187{
188 d->mPort = port;
189}
190
191void LdapServer::setBaseDn( const LdapDN &baseDn )
192{
193 d->mBaseDn = baseDn;
194}
195
196void LdapServer::setUser( const QString &user )
197{
198 d->mUser = user;
199}
200
201void LdapServer::setBindDn( const QString &bindDn )
202{
203 d->mBindDn = bindDn;
204}
205
206void LdapServer::setRealm( const QString &realm )
207{
208 d->mRealm = realm;
209}
210
211void LdapServer::setPassword( const QString &password )
212{
213 d->mPassword = password;
214}
215
216void LdapServer::setTimeLimit( int timelimit )
217{
218 d->mTimeLimit = timelimit;
219}
220
221void LdapServer::setSizeLimit( int sizelimit )
222{
223 d->mSizeLimit = sizelimit;
224}
225
226void LdapServer::setPageSize( int pagesize )
227{
228 d->mPageSize = pagesize;
229}
230
231void LdapServer::setFilter( const QString &filter )
232{
233 d->mFilter = filter;
234}
235
236void LdapServer::setScope( LdapUrl::Scope scope )
237{
238 d->mScope = scope;
239}
240
241void LdapServer::setVersion( int version )
242{
243 d->mVersion = version;
244}
245
246void LdapServer::setSecurity( Security security )
247{
248 d->mSecurity = security;
249}
250
251void LdapServer::setAuth( Auth auth )
252{
253 d->mAuth = auth;
254}
255
256void LdapServer::setMech( const QString &mech )
257{
258 d->mMech = mech;
259}
260
261void LdapServer::setTimeout( int timeout )
262{
263 d->mTimeout = timeout;
264}
265
266void LdapServer::setUrl( const LdapUrl &url )
267{
268 bool critical = true;
269
270 d->mHost = url.host();
271 int port = url.port();
272 if ( port <= 0 ) {
273 d->mPort = 389;
274 } else {
275 d->mPort = port;
276 }
277 d->mBaseDn = url.dn();
278 d->mScope = url.scope();
279
280 d->mFilter = url.filter();
281
282 d->mSecurity = None;
283 if ( url.protocol() == QLatin1String("ldaps") ) {
284 d->mSecurity = SSL;
285 } else if ( url.hasExtension( QLatin1String("x-tls") ) ) {
286 d->mSecurity = TLS;
287 }
288 kDebug() << "security:" << d->mSecurity;
289
290 d->mMech.clear();
291 d->mUser.clear();
292 d->mBindDn.clear();
293 if ( url.hasExtension(QLatin1String( "x-sasl") ) ) {
294 d->mAuth = SASL;
295 if ( url.hasExtension( QLatin1String("x-mech") ) ) {
296 d->mMech = url.extension( QLatin1String("x-mech"), critical );
297 }
298 if ( url.hasExtension( QLatin1String("x-realm") ) ) {
299 d->mRealm = url.extension( QLatin1String("x-realm"), critical );
300 }
301 if ( url.hasExtension( QLatin1String("bindname") ) ) {
302 d->mBindDn = url.extension( QLatin1String("bindname"), critical );
303 }
304 d->mUser = url.user();
305 } else if ( url.hasExtension( QLatin1String("bindname") ) ) {
306 d->mAuth = Simple;
307 d->mBindDn = url.extension( QLatin1String("bindname"), critical );
308 } else {
309 QString user = url.user();
310 if ( user.isEmpty() ) {
311 d->mAuth = Anonymous;
312 } else {
313 d->mAuth = Simple;
314 d->mBindDn = user;
315 }
316 }
317 d->mPassword = url.password();
318 if ( url.hasExtension( QLatin1String("x-version") ) ) {
319 d->mVersion = url.extension( QLatin1String("x-version"), critical ).toInt();
320 } else {
321 d->mVersion = 3;
322 }
323
324 if ( url.hasExtension( QLatin1String("x-timeout") ) ) {
325 d->mTimeout = url.extension( QLatin1String("x-timeout"), critical ).toInt();
326 } else {
327 d->mTimeout = 0;
328 }
329
330 if ( url.hasExtension( QLatin1String("x-timelimit") ) ) {
331 d->mTimeLimit = url.extension( QLatin1String("x-timelimit"), critical ).toInt();
332 } else {
333 d->mTimeLimit = 0;
334 }
335
336 if ( url.hasExtension( QLatin1String("x-sizelimit") ) ) {
337 d->mSizeLimit = url.extension( QLatin1String("x-sizelimit"), critical ).toInt();
338 } else {
339 d->mSizeLimit = 0;
340 }
341
342 if ( url.hasExtension( QLatin1String("x-pagesize") ) ) {
343 d->mPageSize = url.extension( QLatin1String("x-pagesize"), critical ).toInt();
344 } else {
345 d->mPageSize = 0;
346 }
347}
348
349LdapUrl LdapServer::url() const
350{
351 LdapUrl url;
352 url.setProtocol( d->mSecurity == SSL ? QLatin1String("ldaps") : QLatin1String("ldap") );
353 url.setPort( d->mPort );
354 url.setHost( d->mHost );
355 url.setDn( d->mBaseDn );
356 url.setFilter( d->mFilter );
357 url.setScope( d->mScope );
358 if ( d->mAuth == SASL ) {
359 url.setUser( d->mUser );
360 url.setPassword( d->mPassword );
361 url.setExtension( QLatin1String("bindname"), d->mBindDn, true );
362 url.setExtension( QLatin1String("x-sasl"), QString() );
363 if ( !d->mMech.isEmpty() ) {
364 url.setExtension( QLatin1String("x-mech"), d->mMech );
365 }
366 if ( !d->mRealm.isEmpty() ) {
367 url.setExtension( QLatin1String("x-realm"), d->mRealm );
368 }
369 } else if (d->mAuth == Simple ) {
370 url.setUser( d->mBindDn );
371 url.setPassword( d->mPassword );
372 }
373 if ( d->mVersion == 2 ) {
374 url.setExtension( QLatin1String("x-version"), d->mVersion );
375 }
376 if ( d->mTimeout ) {
377 url.setExtension( QLatin1String("x-timeout"), d->mTimeout );
378 }
379 if ( d->mTimeLimit != 0 ) {
380 url.setExtension( QLatin1String("x-timelimit"), d->mTimeLimit );
381 }
382 if ( d->mSizeLimit != 0 ) {
383 url.setExtension( QLatin1String("x-sizelimit"), d->mSizeLimit );
384 }
385 if ( d->mPageSize != 0 ) {
386 url.setExtension( QLatin1String("x-pagesize"), d->mPageSize );
387 }
388 if ( d->mSecurity == TLS ) {
389 url.setExtension( QLatin1String("x-tls"), 1, true );
390 }
391
392 return url;
393}
394