~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Eric Day
 *
 *  This program 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; version 2 of the License.
 *
 *  This program 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

/* This is needed for simple auth, we're not ready for SASL yet. */
#define LDAP_DEPRECATED 1

#include <ldap.h>
#include <pthread.h>
#include <sys/time.h>
#include <map>
#include <string>

#include "drizzled/plugin/authentication.h"
#include "drizzled/security_context.h"
#include "drizzled/util/convert.h"
#include "drizzled/algorithm/sha1.h"

using namespace std;
using namespace drizzled;

namespace auth_ldap
{

static char *uri= NULL;
static const char DEFAULT_URI[]= "ldap://127.0.0.1/";
static char *bind_dn= NULL;
static char *bind_password= NULL;
static char *base_dn= NULL;
static char *password_attribute= NULL;
static const char DEFAULT_PASSWORD_ATTRIBUTE[]= "userPassword";
static char *mysql_password_attribute= NULL;
static const char DEFAULT_MYSQL_PASSWORD_ATTRIBUTE[]= "mysqlUserPassword";
static int cache_timeout= 0;
static const int DEFAULT_CACHE_TIMEOUT= 600;

class AuthLDAP: public plugin::Authentication
{
public:

  AuthLDAP(string name_arg);
  ~AuthLDAP();

  /**
   * Initialize the LDAP connection.
   *
   * @return True on success, false otherwise.
   */
  bool initialize(void);

  /**
   * Connect to the LDAP server.
   *
   * @return True on success, false otherwise.
   */
  bool connect(void);

  /**
   * Retrieve the last error encountered in the class.
   */
  string& getError(void);

private:

  typedef enum
  {
    NOT_FOUND,
    PLAIN_TEXT,
    MYSQL_HASH
  } PasswordType;

  typedef pair<PasswordType, string> PasswordEntry;
  typedef pair<string, PasswordEntry> UserEntry;
  typedef map<string, PasswordEntry> UserCache;

  /**
   * Base class method to check authentication for a user.
   */
  bool authenticate(const SecurityContext &sctx, const string &password);

  /**
   * Lookup a user in LDAP.
   *
   * @param[in] Username to lookup.
   */
  void lookupUser(const string& user);

  /**
   * Verify the local and remote scrambled password match using the MySQL
   * hashing algorithm.
   *
   * @param[in] password Plain text password that is stored locally.
   * @param[in] scramble_bytes The random bytes that the server sent to the
   *  client for scrambling the password.
   * @param[in] scrambled_password The result of the client scrambling the
   *  password remotely.
   * @return True if the password matched, false if not.
   */
  bool verifyMySQLHash(const PasswordEntry &password,
                       const string &scramble_bytes,
                       const string &scrambled_password);

  time_t next_cache_expiration;
  LDAP *ldap;
  string error;
  UserCache users;
  pthread_rwlock_t lock;
};

AuthLDAP::AuthLDAP(string name_arg):
  plugin::Authentication(name_arg),
  next_cache_expiration(),
  ldap(),
  error(),
  users()
{
}

AuthLDAP::~AuthLDAP()
{
  pthread_rwlock_destroy(&lock);
  if (ldap != NULL)
    ldap_unbind(ldap);
}

bool AuthLDAP::initialize(void)
{
  int return_code= pthread_rwlock_init(&lock, NULL);
  if (return_code != 0)
  {
    error= "pthread_rwlock_init failed";
    return false;
  }

  return connect();
}

bool AuthLDAP::connect(void)
{
  int return_code= ldap_initialize(&ldap, uri);
  if (return_code != LDAP_SUCCESS)
  {
    error= "ldap_initialize failed: ";
    error+= ldap_err2string(return_code);
    return false;
  }

  int version= 3;
  return_code= ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
  if (return_code != LDAP_SUCCESS)
  {
    ldap_unbind(ldap);
    ldap= NULL;
    error= "ldap_set_option failed: ";
    error+= ldap_err2string(return_code);
    return false;
  }

  if (bind_dn != NULL)
  {
    return_code= ldap_simple_bind_s(ldap, bind_dn, bind_password);
    if (return_code != LDAP_SUCCESS)
    {
      ldap_unbind(ldap);
      ldap= NULL;
      error= "ldap_simple_bind_s failed: ";
      error+= ldap_err2string(return_code);
      return false;
    }
  }

  return true;
}

string& AuthLDAP::getError(void)
{
  return error;
}

bool AuthLDAP::authenticate(const SecurityContext &sctx, const string &password)
{
  /* See if cache should be emptied. */
  if (cache_timeout > 0)
  {
    struct timeval current_time;
    gettimeofday(&current_time, NULL);
    if (current_time.tv_sec > next_cache_expiration)
    {
      pthread_rwlock_wrlock(&lock);
      /* Make sure another thread didn't already clear it. */
      if (current_time.tv_sec > next_cache_expiration)
      {
        users.clear();
        next_cache_expiration= current_time.tv_sec + cache_timeout;
      }
      pthread_rwlock_unlock(&lock);
    }
  }

  pthread_rwlock_rdlock(&lock);

  AuthLDAP::UserCache::const_iterator user= users.find(sctx.getUser());
  if (user == users.end())
  {
    pthread_rwlock_unlock(&lock);

    pthread_rwlock_wrlock(&lock);

    /* Make sure the user was not added while we unlocked. */
    user= users.find(sctx.getUser());
    if (user == users.end())
      lookupUser(sctx.getUser());

    pthread_rwlock_unlock(&lock);

    pthread_rwlock_rdlock(&lock);

    /* Get user again because map may have changed while unlocked. */
    user= users.find(sctx.getUser());
    if (user == users.end())
    {
      pthread_rwlock_unlock(&lock);
      return false;
    }
  }

  if (user->second.first == NOT_FOUND)
  {
    pthread_rwlock_unlock(&lock);
    return false;
  }

  if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH)
  {
    bool allow= verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
    pthread_rwlock_unlock(&lock);
    return allow;
  }

  if (user->second.first == PLAIN_TEXT && password == user->second.second)
  {
    pthread_rwlock_unlock(&lock);
    return true;
  }

  pthread_rwlock_unlock(&lock);
  return false;
}

void AuthLDAP::lookupUser(const string& user)
{
  string filter("(cn=" + user + ")");
  const char *attributes[3]=
  {
    password_attribute,
    mysql_password_attribute,
    NULL
  };
  LDAPMessage *result;
  bool try_reconnect= true;

  while (true)
  {
    if (ldap == NULL)
    {
      if (! connect())
      {
        errmsg_printf(ERRMSG_LVL_ERROR, _("Reconnect failed: %s\n"),
                      getError().c_str());
        return;
      }
    }

    int return_code= ldap_search_ext_s(ldap,
                                       base_dn,
                                       LDAP_SCOPE_ONELEVEL,
                                       filter.c_str(),
                                       const_cast<char **>(attributes),
                                       0,
                                       NULL,
                                       NULL,
                                       NULL,
                                       1,
                                       &result);
    if (return_code != LDAP_SUCCESS)
    {
      errmsg_printf(ERRMSG_LVL_ERROR, _("ldap_search_ext_s failed: %s\n"),
                    ldap_err2string(return_code));

      /* Only try one reconnect per request. */
      if (try_reconnect)
      {
        try_reconnect= false;
        ldap_unbind(ldap);
        ldap= NULL;
        continue;
      }

      return;
    }

    break;
  }

  LDAPMessage *entry= ldap_first_entry(ldap, result);
  AuthLDAP::PasswordEntry new_password;
  if (entry == NULL)
    new_password= AuthLDAP::PasswordEntry(NOT_FOUND, "");
  else
  {
    char **values= ldap_get_values(ldap, entry, mysql_password_attribute);
    if (values == NULL)
    {
      values= ldap_get_values(ldap, entry, password_attribute);
      if (values == NULL)
        new_password= AuthLDAP::PasswordEntry(NOT_FOUND, "");
      else
      {
        new_password= AuthLDAP::PasswordEntry(PLAIN_TEXT, values[0]);
        ldap_value_free(values);
      }
    }
    else
    {
      new_password= AuthLDAP::PasswordEntry(MYSQL_HASH, values[0]);
      ldap_value_free(values);
    }
  }

  users.insert(AuthLDAP::UserEntry(user, new_password));
}

bool AuthLDAP::verifyMySQLHash(const PasswordEntry &password,
                               const string &scramble_bytes,
                               const string &scrambled_password)
{
  if (scramble_bytes.size() != SHA1_DIGEST_LENGTH ||
      scrambled_password.size() != SHA1_DIGEST_LENGTH)
  {
    return false;
  }

  SHA1_CTX ctx;
  uint8_t local_scrambled_password[SHA1_DIGEST_LENGTH];
  uint8_t temp_hash[SHA1_DIGEST_LENGTH];
  uint8_t scrambled_password_check[SHA1_DIGEST_LENGTH];

  if (password.first == MYSQL_HASH)
  {
    /* Get the double-hashed password from the given hex string. */
    drizzled_hex_to_string(reinterpret_cast<char*>(local_scrambled_password),
                           password.second.c_str(), SHA1_DIGEST_LENGTH * 2);
  }
  else
  {
    /* Generate the double SHA1 hash for the password stored locally first. */
    SHA1Init(&ctx);
    SHA1Update(&ctx, reinterpret_cast<const uint8_t *>(password.second.c_str()),
               password.second.size());
    SHA1Final(temp_hash, &ctx);

    SHA1Init(&ctx);
    SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
    SHA1Final(local_scrambled_password, &ctx);
  }

  /* Hash the scramble that was sent to client with the local password. */
  SHA1Init(&ctx);
  SHA1Update(&ctx, reinterpret_cast<const uint8_t*>(scramble_bytes.c_str()),
             SHA1_DIGEST_LENGTH);
  SHA1Update(&ctx, local_scrambled_password, SHA1_DIGEST_LENGTH);
  SHA1Final(temp_hash, &ctx);

  /* Next, XOR the result with what the client sent to get the original
     single-hashed password. */
  for (int x= 0; x < SHA1_DIGEST_LENGTH; x++)
    temp_hash[x]= temp_hash[x] ^ scrambled_password[x];

  /* Hash this result once more to get the double-hashed password again. */
  SHA1Init(&ctx);
  SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
  SHA1Final(scrambled_password_check, &ctx);

  /* These should match for a successful auth. */
  return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
}

static int init(module::Context &context)
{
  AuthLDAP *auth_ldap= new AuthLDAP("auth_ldap");
  if (! auth_ldap->initialize())
  {
    errmsg_printf(ERRMSG_LVL_ERROR, _("Could not load auth ldap: %s\n"),
                  auth_ldap->getError().c_str());
    delete auth_ldap;
    return 1;
  }

  context.add(auth_ldap);
  return 0;
}

static DRIZZLE_SYSVAR_STR(uri,
                          uri,
                          PLUGIN_VAR_READONLY,
                          N_("URI of the LDAP server to contact"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          DEFAULT_URI);

static DRIZZLE_SYSVAR_STR(bind_dn,
                          bind_dn,
                          PLUGIN_VAR_READONLY,
                          N_("DN to use when binding to the LDAP server"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          NULL); /* default value */

static DRIZZLE_SYSVAR_STR(bind_password,
                          bind_password,
                          PLUGIN_VAR_READONLY,
                          N_("Password to use when binding the DN"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          NULL); /* default value */

static DRIZZLE_SYSVAR_STR(base_dn,
                          base_dn,
                          PLUGIN_VAR_READONLY,
                          N_("DN to use when searching"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          NULL); /* default value */

static DRIZZLE_SYSVAR_STR(password_attribute,
                          password_attribute,
                          PLUGIN_VAR_READONLY,
                          N_("Attribute in LDAP with plain text password"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          DEFAULT_PASSWORD_ATTRIBUTE);

static DRIZZLE_SYSVAR_STR(mysql_password_attribute,
                          mysql_password_attribute,
                          PLUGIN_VAR_READONLY,
                          N_("Attribute in LDAP with MySQL hashed password"),
                          NULL, /* check func */
                          NULL, /* update func*/
                          DEFAULT_MYSQL_PASSWORD_ATTRIBUTE);

static DRIZZLE_SYSVAR_INT(cache_timeout,
                          cache_timeout,
                          PLUGIN_VAR_READONLY,
                          N_("How often to empty the users cache, 0 to disable"),
                          NULL, /* check func */
                          NULL, /* update func */
                          DEFAULT_CACHE_TIMEOUT,
                          0,
                          2147483647,
                          0);

static drizzle_sys_var* sys_variables[]=
{
  DRIZZLE_SYSVAR(uri),
  DRIZZLE_SYSVAR(bind_dn),
  DRIZZLE_SYSVAR(bind_password),
  DRIZZLE_SYSVAR(base_dn),
  DRIZZLE_SYSVAR(password_attribute),
  DRIZZLE_SYSVAR(mysql_password_attribute),
  DRIZZLE_SYSVAR(cache_timeout),
  NULL
};

} /* namespace auth_ldap */

DRIZZLE_PLUGIN(auth_ldap::init, auth_ldap::sys_variables, NULL);