~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

« back to all changes in this revision

Viewing changes to kldap/ldapconnection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-09-06 22:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20060906224539-fiq8t03qdbqu7z3i
Tags: upstream-3.80.1
ImportĀ upstreamĀ versionĀ 3.80.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "ldapconnection.h"
 
22
 
 
23
#include <malloc.h>
 
24
 
 
25
#include <klocale.h>
 
26
#include <kdebug.h>
 
27
 
 
28
#include <kldap_config.h>
 
29
 
 
30
#ifdef SASL2_FOUND
 
31
#include <sasl/sasl.h>
 
32
#endif
 
33
 
 
34
#ifdef LDAP_FOUND
 
35
#define LDAP_DEPRECATED 1 //for ldap_simple_bind_s
 
36
#include <ldap.h>
 
37
#include <lber.h>
 
38
#endif
 
39
 
 
40
using namespace KLDAP;
 
41
 
 
42
 
 
43
class LdapConnection::LdapConnectionPrivate 
 
44
{
 
45
  public:
 
46
    LdapServer mServer;
 
47
    QString mConnectionError;
 
48
          
 
49
#ifdef LDAP_FOUND
 
50
    LDAP *mLDAP;
 
51
#else
 
52
    void *mLDAP;
 
53
#endif
 
54
};
 
55
 
 
56
LdapConnection::LdapConnection()
 
57
  : d( new LdapConnectionPrivate )
 
58
{
 
59
  d->mLDAP = 0;
 
60
}
 
61
 
 
62
LdapConnection::LdapConnection( const LdapUrl &url )
 
63
  : d( new LdapConnectionPrivate )
 
64
{
 
65
  d->mLDAP = 0;
 
66
  setUrl( url );
 
67
}
 
68
 
 
69
LdapConnection::LdapConnection( const LdapServer &server )
 
70
  : d( new LdapConnectionPrivate )
 
71
{
 
72
  d->mLDAP = 0;
 
73
  setServer( server );
 
74
}
 
75
                  
 
76
LdapConnection::~LdapConnection()
 
77
{
 
78
  delete d;
 
79
  close();
 
80
}
 
81
 
 
82
void LdapConnection::setUrl( const LdapUrl &url )
 
83
{
 
84
  d->mServer.setUrl( url );
 
85
}
 
86
 
 
87
void LdapConnection::setServer( const LdapServer &server )
 
88
{
 
89
  d->mServer = server;
 
90
}
 
91
 
 
92
void *LdapConnection::handle() const
 
93
{
 
94
  return (void*) d->mLDAP;
 
95
}
 
96
 
 
97
QString LdapConnection::errorString( int code )
 
98
{
 
99
  //No translated error messages yet
 
100
#ifdef LDAP_FOUND
 
101
  return QString::fromUtf8( ldap_err2string( code ) );
 
102
  switch ( code ) {
 
103
    case LDAP_OPERATIONS_ERROR: return i18n("LDAP Operations error");
 
104
    //FIXME:
 
105
    /* add the LDAP error codes */
 
106
  }
 
107
#else
 
108
  return i18n("No LDAP Support...");
 
109
#endif
 
110
}
 
111
 
 
112
QString LdapConnection::connectionError() const
 
113
 
114
  return d->mConnectionError; 
 
115
}      
 
116
 
 
117
#ifdef LDAP_FOUND
 
118
int LdapConnection::getOption( int option, void *value ) const
 
119
{
 
120
  Q_ASSERT( d->mLDAP );
 
121
  return ldap_get_option( d->mLDAP, option, value );
 
122
}
 
123
 
 
124
int LdapConnection::setOption( int option, void *value )
 
125
{
 
126
  Q_ASSERT( d->mLDAP );
 
127
  return ldap_set_option( d->mLDAP, option, value );
 
128
}
 
129
 
 
130
int LdapConnection::ldapErrorCode() const
 
131
{
 
132
  Q_ASSERT( d->mLDAP );
 
133
  int err;
 
134
  ldap_get_option( d->mLDAP, LDAP_OPT_ERROR_NUMBER, &err );
 
135
  return err;
 
136
}
 
137
 
 
138
QString LdapConnection::ldapErrorString() const
 
139
{
 
140
  Q_ASSERT( d->mLDAP );
 
141
  char *errmsg;
 
142
  ldap_get_option( d->mLDAP, LDAP_OPT_ERROR_STRING, &errmsg );
 
143
  QString msg = QString::fromLocal8Bit( errmsg );
 
144
  free( errmsg );
 
145
  return msg;
 
146
}
 
147
 
 
148
bool LdapConnection::setSizeLimit( int sizelimit )
 
149
{
 
150
  Q_ASSERT( d->mLDAP );
 
151
  kDebug() << "sizelimit: " << sizelimit << endl;
 
152
  if ( setOption( LDAP_OPT_SIZELIMIT, &sizelimit ) != LDAP_OPT_SUCCESS ) {
 
153
    return false;
 
154
  }
 
155
  return true;
 
156
}
 
157
 
 
158
int LdapConnection::sizeLimit() const
 
159
{
 
160
  Q_ASSERT( d->mLDAP );
 
161
  int sizelimit;
 
162
  if ( getOption( LDAP_OPT_SIZELIMIT, &sizelimit ) != LDAP_OPT_SUCCESS ) {
 
163
    return -1;
 
164
  }
 
165
  return sizelimit;
 
166
}
 
167
 
 
168
bool LdapConnection::setTimeLimit( int timelimit )
 
169
{
 
170
  Q_ASSERT( d->mLDAP );
 
171
  kDebug() << "timelimit: " << timelimit << endl;
 
172
  if ( setOption( LDAP_OPT_TIMELIMIT, &timelimit ) != LDAP_OPT_SUCCESS ) {
 
173
    return false;
 
174
  }
 
175
  return true;
 
176
}
 
177
 
 
178
int LdapConnection::timeLimit() const
 
179
{
 
180
  Q_ASSERT( d->mLDAP );
 
181
  int timelimit;
 
182
  if ( getOption( LDAP_OPT_TIMELIMIT, &timelimit ) != LDAP_OPT_SUCCESS ) {
 
183
    return -1;
 
184
  }
 
185
  return timelimit;
 
186
}
 
187
 
 
188
static int kldap_sasl_interact( LDAP *, unsigned, void *defaults, void *in )
 
189
{
 
190
#ifdef SASL2_FOUND
 
191
  LdapConnection::SASL_Data *data = (LdapConnection::SASL_Data*) defaults;
 
192
  sasl_interact_t *interact = ( sasl_interact_t * ) in;
 
193
 
 
194
  if ( data->proc ) {
 
195
    for ( ; interact->id != SASL_CB_LIST_END; interact++ ) {
 
196
      switch ( interact->id ) {
 
197
        case SASL_CB_GETREALM:
 
198
          data->creds.fields |= LdapConnection::SASL_Realm;
 
199
          break;
 
200
        case SASL_CB_AUTHNAME:
 
201
          data->creds.fields |= LdapConnection::SASL_Authname;
 
202
          break;
 
203
        case SASL_CB_PASS:
 
204
          data->creds.fields |= LdapConnection::SASL_Password;
 
205
          break;
 
206
        case SASL_CB_USER:
 
207
          data->creds.fields |= LdapConnection::SASL_Authzid;
 
208
          break;
 
209
      }
 
210
    }
 
211
    int retval;
 
212
    if ( (retval = data->proc( data->creds, data->data )) ) return retval;
 
213
  }
 
214
 
 
215
  QString value;
 
216
  
 
217
  while( interact->id != SASL_CB_LIST_END ) {
 
218
    value = QString();
 
219
    switch( interact->id ) {
 
220
      case SASL_CB_GETREALM:
 
221
        value = data->creds.realm;
 
222
        kDebug() << "SASL_REALM=" << value << endl;
 
223
        break;
 
224
      case SASL_CB_AUTHNAME:
 
225
        value = data->creds.authname;
 
226
        kDebug() << "SASL_AUTHNAME=" << value << endl;
 
227
        break;
 
228
      case SASL_CB_PASS:
 
229
        value = data->creds.password;
 
230
        kDebug() << "SASL_PASSWD=[hidden]" << endl;
 
231
        break;
 
232
      case SASL_CB_USER:
 
233
        value = data->creds.authzid;
 
234
        kDebug() << "SASL_AUTHZID=" << value << endl;
 
235
        break;
 
236
    }
 
237
  }
 
238
  if ( value.isEmpty() ) {
 
239
    interact->result = NULL;
 
240
    interact->len = 0;
 
241
  } else {
 
242
    interact->result = strdup( value.toUtf8() );
 
243
    interact->len = strlen( (const char *) interact->result );
 
244
  }
 
245
  interact++;
 
246
#endif
 
247
  return LDAP_SUCCESS;
 
248
}
 
249
 
 
250
int LdapConnection::connect()
 
251
{
 
252
  int ret;
 
253
  QString url;
 
254
  if ( d->mLDAP ) close();
 
255
  
 
256
  int version = d->mServer.version();
 
257
 
 
258
  url = d->mServer.security() == LdapServer::SSL ? "ldaps" : "ldap";
 
259
  url += "://";
 
260
  url += d->mServer.host();
 
261
  url += ':';
 
262
  url += QString::number( d->mServer.port() );
 
263
  kDebug() << "ldap url: " << url << endl;
 
264
  ret = ldap_initialize( &d->mLDAP, url.toLatin1() );
 
265
  if ( ret != LDAP_SUCCESS ) {
 
266
    d->mConnectionError = i18n("An error occurred during the connection initialization phase.");
 
267
    return ret;
 
268
  }
 
269
 
 
270
  kDebug() << "setting version to: " << version << endl;
 
271
  if ( (setOption( LDAP_OPT_PROTOCOL_VERSION, &version )) != LDAP_OPT_SUCCESS ) {
 
272
    ret = ldapErrorCode();
 
273
      d->mConnectionError = i18n("Cannot set protocol version to %1.").arg(version);
 
274
    close();
 
275
    return ret;
 
276
  }
 
277
 
 
278
  //FIXME: accessing to certificate handling would be good
 
279
  kDebug() << "setting security to: " << d->mServer.security() << endl;
 
280
  if ( d->mServer.security() == LdapServer::TLS ) {
 
281
    kDebug() << "start TLS" << endl;
 
282
    if ( ( ret = ldap_start_tls_s( d->mLDAP, NULL, NULL ) ) != LDAP_SUCCESS ) {
 
283
      close();
 
284
      d->mConnectionError = i18n("Cannot start TLS.");
 
285
      return ret;
 
286
    }
 
287
  }
 
288
  
 
289
  kDebug() << "setting sizelimit to: " << d->mServer.sizeLimit() << endl;
 
290
  if ( d->mServer.sizeLimit() ) {
 
291
    if ( !setSizeLimit( d->mServer.sizeLimit() ) ) {
 
292
      ret = ldapErrorCode();
 
293
      close();
 
294
      d->mConnectionError = i18n("Cannot set size limit.");
 
295
      return ret;
 
296
    }
 
297
  }
 
298
 
 
299
  kDebug() << "setting timelimit to: " << d->mServer.timeLimit() << endl;
 
300
  if ( d->mServer.timeLimit() ) {
 
301
    if ( !setTimeLimit( d->mServer.timeLimit() ) ) {
 
302
      ret = ldapErrorCode();
 
303
      close();
 
304
      d->mConnectionError = i18n("Cannot set time limit.");
 
305
      return ret;
 
306
    }
 
307
  }
 
308
  return 0;
 
309
}
 
310
 
 
311
int LdapConnection::bind( SASL_Callback_Proc *saslproc, void *data )
 
312
{
 
313
  int ret;
 
314
  
 
315
  if ( d->mServer.auth() == LdapServer::SASL ) {
 
316
#ifdef SASL2_FOUND
 
317
    QString mech = d->mServer.mech();
 
318
    if ( mech.isEmpty() ) mech = "DIGEST-MD5";
 
319
 
 
320
    SASL_Data sasldata;
 
321
    sasldata.proc = saslproc;
 
322
    sasldata.data = data;
 
323
    sasldata.creds.fields = 0;
 
324
    sasldata.creds.realm = d->mServer.realm();
 
325
    sasldata.creds.authname = d->mServer.user();
 
326
    sasldata.creds.authzid = d->mServer.bindDn();
 
327
    sasldata.creds.password = d->mServer.password();
 
328
    
 
329
    ret = ldap_sasl_interactive_bind_s( d->mLDAP, 0, mech.toLatin1(), 0, 0,
 
330
      LDAP_SASL_INTERACTIVE, &kldap_sasl_interact, &sasldata );
 
331
#else
 
332
    return -0xff;
 
333
#endif
 
334
  } else {
 
335
    QString bindname, pass;
 
336
    if ( d->mServer.auth() == LdapServer::Simple ) {
 
337
      bindname = d->mServer.bindDn();
 
338
      pass = d->mServer.password();
 
339
    }
 
340
    kDebug() << "binding to server, bindname: " << bindname << " password: *****" << endl;
 
341
    ret = ldap_simple_bind_s( d->mLDAP, bindname.toUtf8(), pass.toUtf8() );
 
342
  }
 
343
  return ret;
 
344
}
 
345
 
 
346
void LdapConnection::close()
 
347
{
 
348
  if ( d->mLDAP ) ldap_unbind_ext_s( d->mLDAP, 0, 0 );
 
349
  d->mLDAP = 0;
 
350
  kDebug() << "connection closed!" << endl;
 
351
}
 
352
#else //LDAP_FOUND
 
353
 
 
354
int LdapConnection::getOption( int option, void *value ) const
 
355
{
 
356
  kError() << "No LDAP support..." << endl;
 
357
  return -1;
 
358
}
 
359
 
 
360
int LdapConnection::setOption( int option, void *value )
 
361
{
 
362
  kError() << "No LDAP support..." << endl;
 
363
  return -1;
 
364
}
 
365
 
 
366
int LdapConnection::ldapErrorCode() const
 
367
{
 
368
  kError() << "No LDAP support..." << endl;
 
369
  return -1;
 
370
}
 
371
 
 
372
QString LdapConnection::ldapErrorString() const
 
373
{
 
374
  kError() << "No LDAP support..." << endl;
 
375
  return QString();
 
376
}
 
377
 
 
378
bool LdapConnection::setSizeLimit( int sizelimit )
 
379
{
 
380
  kError() << "No LDAP support..." << endl;
 
381
  return false;
 
382
}
 
383
 
 
384
int LdapConnection::sizeLimit() const
 
385
{
 
386
  kError() << "No LDAP support..." << endl;
 
387
  return -1;
 
388
}
 
389
 
 
390
bool LdapConnection::setTimeLimit( int timelimit )
 
391
{
 
392
  kError() << "No LDAP support..." << endl;
 
393
  return false;
 
394
}
 
395
 
 
396
int LdapConnection::timeLimit() const
 
397
{
 
398
  kError() << "No LDAP support..." << endl;
 
399
  return -1;
 
400
}
 
401
 
 
402
int LdapConnection::connect( )
 
403
{
 
404
  d->mConnectionError = i18n("LDAP support not compiled in. Please recompile libkldap with the OpenLDAP (or compatible) client libraries, or complain to your distribution packagers.");
 
405
  kError() << "No LDAP support..." << endl;
 
406
  return -1;
 
407
}
 
408
 
 
409
int LdapConnection::bind( SASL_Callback_Proc *saslproc, void *data )
 
410
{
 
411
  kError() << "No LDAP support..." << endl;
 
412
  return -1;
 
413
}
 
414
 
 
415
void LdapConnection::close()
 
416
{
 
417
  kError() << "No LDAP support..." << endl;
 
418
}
 
419
 
 
420
 
 
421
#endif