Projet

Général

Profil

Paste
Télécharger (67,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_servers / LdapServer.class.php @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines server classes and related functions.
6
 *
7
 */
8

    
9
/**
10
 * TODO check if this already exists or find a better place for this function
11
 *
12
 * Formats a ldap-entry ready to be printed on console.
13
 * TODO describe preconditions for ldap_entry
14
 */
15
function pretty_print_ldap_entry($ldap_entry) {
16
  $m=array();
17
  for ($i=0; $i < $ldap_entry['count']; $i++) {
18
    $k=$ldap_entry[$i];
19
    $v=$ldap_entry[$k];
20
    if(is_array($v)) {
21
      $m2=array();
22
      $max=$v['count']>3 ? 3 : $v['count'];
23
      for ($j=0; $j < $max; $j++) {
24
        $m2[] = $v[$j];
25
      }
26
      $v="(".join(", ", $m2).")";
27
    }
28
    $m[] = $k . ": " . $v;
29
  }
30
  return join(", ", $m);
31
}
32

    
33
/**
34
 * LDAP Server Class
35
 *
36
 *  This class is used to create, work with, and eventually destroy ldap_server
37
 * objects.
38
 *
39
 * @todo make bindpw protected
40
 */
41
class LdapServer {
42
  // LDAP Settings
43

    
44
  const LDAP_CONNECT_ERROR = 0x5b;
45
  const LDAP_SUCCESS = 0x00;
46
  const LDAP_OPERATIONS_ERROR = 0x01;
47
  const LDAP_PROTOCOL_ERROR = 0x02;
48

    
49
  public $sid;
50
  public $numericSid;
51
  public $name;
52
  public $status;
53
  public $ldap_type;
54
  public $address;
55
  public $port = 389;
56
  public $tls = FALSE;
57
  public $followrefs = FALSE;
58
  public $bind_method = 0;
59
  public $basedn = array();
60
  public $binddn = FALSE; // Default to an anonymous bind.
61
  public $bindpw = FALSE; // Default to an anonymous bind.
62
  public $user_dn_expression;
63
  public $user_attr;
64
  public $account_name_attr; //lowercase
65
  public $mail_attr; //lowercase
66
  public $mail_template;
67
  public $picture_attr;
68
  public $unique_persistent_attr; //lowercase
69
  public $unique_persistent_attr_binary = FALSE;
70
  public $ldapToDrupalUserPhp;
71
  public $testingDrupalUsername;
72
  public $testingDrupalUserDn;
73
  public $detailed_watchdog_log;
74
  public $editPath;
75
  public $queriableWithoutUserCredentials = FALSE; // can this server be queried without user credentials provided?
76
  public $userAttributeNeededCache = array(); // array of attributes needed keyed on $op such as 'user_update'
77

    
78
  public $groupFunctionalityUnused = 0;
79
  public $groupObjectClass;
80
  public $groupNested = 0; // 1 | 0
81
  public $groupDeriveFromDn = FALSE;
82
  public $groupDeriveFromDnAttr = NULL; //lowercase
83
  public $groupUserMembershipsAttrExists = FALSE; // does a user attribute containing groups exist?
84
  public $groupUserMembershipsAttr = NULL;   //lowercase     // name of user attribute containing groups
85
  public $groupUserMembershipsConfigured = FALSE; // user attribute containing memberships is configured enough to use
86

    
87
  public $groupMembershipsAttr = NULL;  //lowercase // members, uniquemember, memberUid
88
  public $groupMembershipsAttrMatchingUserAttr = NULL; //lowercase // dn, cn, etc contained in groupMembershipsAttr
89
  public $groupGroupEntryMembershipsConfigured = FALSE; // are groupMembershipsAttrMatchingUserAttr and groupGroupEntryMembershipsConfigured populated
90

    
91
  public $groupTestGroupDn = NULL;
92
  public $groupTestGroupDnWriteable = NULL;
93

    
94
  private $group_properties = array(
95
    'groupObjectClass', 'groupNested', 'groupDeriveFromDn', 'groupDeriveFromDnAttr', 'groupUserMembershipsAttrExists',
96
    'groupUserMembershipsAttr', 'groupMembershipsAttrMatchingUserAttr', 'groupTestGroupDn', 'groupTestGroupDnWriteable'
97
  );
98

    
99
  public $paginationEnabled = FALSE; // (boolean)(function_exists('ldap_control_paged_result_response') && function_exists('ldap_control_paged_result'));
100
  public $searchPagination = FALSE;
101
  public $searchPageSize = 1000;
102
  public $searchPageStart = 0;
103
  public $searchPageEnd = NULL;
104

    
105
  public $inDatabase = FALSE;
106
  public $connection;
107

    
108

    
109

    
110

    
111
  // direct mapping of db to object properties
112
  public static function field_to_properties_map() {
113
    return array(
114
    'sid' => 'sid',
115
    'numeric_sid' => 'numericSid',
116
    'name'  => 'name' ,
117
    'status'  => 'status',
118
    'ldap_type'  => 'ldap_type',
119
    'address'  => 'address',
120
    'port'  => 'port',
121
    'tls'  => 'tls',
122
    'followrefs'  => 'followrefs',
123
    'bind_method' => 'bind_method',
124
    'basedn'  => 'basedn',
125
    'binddn'  => 'binddn',
126
    'user_dn_expression' => 'user_dn_expression',
127
    'user_attr'  => 'user_attr',
128
    'account_name_attr'  => 'account_name_attr',
129
    'mail_attr'  => 'mail_attr',
130
    'mail_template'  => 'mail_template',
131
    'picture_attr'  => 'picture_attr',
132
    'unique_persistent_attr' => 'unique_persistent_attr',
133
    'unique_persistent_attr_binary' => 'unique_persistent_attr_binary',
134
    'ldap_to_drupal_user'  => 'ldapToDrupalUserPhp',
135
    'testing_drupal_username'  => 'testingDrupalUsername',
136
    'testing_drupal_user_dn'  => 'testingDrupalUserDn',
137

    
138
    'grp_unused' => 'groupFunctionalityUnused',
139
    'grp_object_cat' => 'groupObjectClass',
140
    'grp_nested' => 'groupNested',
141
    'grp_user_memb_attr_exists' => 'groupUserMembershipsAttrExists',
142
    'grp_user_memb_attr' => 'groupUserMembershipsAttr',
143
    'grp_memb_attr' => 'groupMembershipsAttr',
144
    'grp_memb_attr_match_user_attr' => 'groupMembershipsAttrMatchingUserAttr',
145
    'grp_derive_from_dn' => 'groupDeriveFromDn',
146
    'grp_derive_from_dn_attr' => 'groupDeriveFromDnAttr',
147
    'grp_test_grp_dn' =>  'groupTestGroupDn',
148
    'grp_test_grp_dn_writeable' => 'groupTestGroupDnWriteable',
149

    
150
    'search_pagination' => 'searchPagination',
151
    'search_page_size' => 'searchPageSize',
152

    
153
    );
154

    
155
  }
156

    
157
  /**
158
   * Constructor Method
159
   */
160
  function __construct($sid) {
161
    if (!is_scalar($sid)) {
162
      return;
163
    }
164
    $this->detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
165
    $server_record = FALSE;
166
    if (module_exists('ctools')) {
167
      ctools_include('export');
168
      $result = ctools_export_load_object('ldap_servers', 'names', array($sid));
169
      if (isset($result[$sid])) {
170
        $server_record = new stdClass();
171
        foreach ($result[$sid] as $db_field_name => $value) {
172
          $server_record->{$db_field_name} = $value;
173
        }
174
      }
175
      //debug('ctools record'); debug($server_record);
176
    }
177
    else {
178
      $select = db_select('ldap_servers')
179
        ->fields('ldap_servers')
180
        ->condition('ldap_servers.sid',  $sid)
181
        ->execute();
182
      foreach ($select as $record) {
183
        if ($record->sid == $sid) {
184
          $server_record = $record;
185
        }
186
      }
187
    }
188

    
189
    $server_record_bindpw = NULL;
190
    if (!$server_record) {
191
      $this->inDatabase = FALSE;
192
    }
193
    else {
194
      $this->inDatabase = TRUE;
195
      $this->sid = $sid;
196
      $this->detailedWatchdogLog = variable_get('ldap_help_watchdog_detail', 0);
197
      foreach ($this->field_to_properties_map() as $db_field_name => $property_name ) {
198
        if (isset($server_record->$db_field_name)) {
199
          $this->{$property_name} = $server_record->$db_field_name;
200
        }
201
      }
202
      $server_record_bindpw = property_exists($server_record, 'bindpw') ? $server_record->bindpw : '';
203
    }
204
    $this->initDerivedProperties($server_record_bindpw);
205
  }
206

    
207
  /**
208
   * this method sets properties that don't directly map from db record.  it is split out so it can be shared with ldapServerTest.class.php
209
   */
210
  protected function initDerivedProperties($bindpw) {
211

    
212
    // get this->basedn in array format
213
    if (!$this->basedn) {
214
      $this->basedn = array();
215
    }
216
    elseif (is_array($this->basedn)) { // do nothing
217
    }
218
    else {
219
      $basedn_unserialized = @unserialize($this->basedn);
220
      if (is_array($basedn_unserialized)) {
221
        $this->basedn = $basedn_unserialized;
222
      }
223
      else {
224
        $this->basedn = array();
225
        $token = is_scalar($basedn_unserialized) ? $basedn_unserialized : print_r($basedn_unserialized, TRUE);
226
        debug("basednb desearialization error". $token);
227
        watchdog('ldap_server', 'Failed to deserialize LdapServer::basedn of !basedn', array('!basedn' => $token), WATCHDOG_ERROR);
228
      }
229

    
230
    }
231

    
232
    if ($this->followrefs && !function_exists('ldap_set_rebind_proc')) {
233
      $this->followrefs = FALSE;
234
    }
235

    
236
    if ($bindpw) {
237
      $this->bindpw = ($bindpw == '') ? '' : ldap_servers_decrypt($bindpw);
238
    }
239

    
240
    $this->paginationEnabled = (boolean)(ldap_servers_php_supports_pagination() && $this->searchPagination);
241

    
242
    $this->queriableWithoutUserCredentials = (boolean)(
243
      $this->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT ||
244
      $this->bind_method == LDAP_SERVERS_BIND_METHOD_ANON_USER
245
    );
246
    $this->editPath = (!$this->sid) ? '' : 'admin/config/people/ldap/servers/edit/' . $this->sid;
247

    
248
    $this->groupGroupEntryMembershipsConfigured = ($this->groupMembershipsAttrMatchingUserAttr && $this->groupMembershipsAttr);
249
    $this->groupUserMembershipsConfigured = ($this->groupUserMembershipsAttrExists && $this->groupUserMembershipsAttr);
250
  }
251
  /**
252
   * Destructor Method
253
   */
254
  function __destruct() {
255
    // Close the server connection to be sure.
256
    $this->disconnect();
257
  }
258

    
259

    
260
  /**
261
   * Invoke Method
262
   */
263
  function __invoke() {
264
    $this->connect();
265
    $this->bind();
266
  }
267

    
268

    
269

    
270
  /**
271
   * Connect Method
272
   */
273
  function connect() {
274

    
275
    if (!$con = ldap_connect($this->address, $this->port)) {
276
      watchdog('user', 'LDAP Connect failure to ' . $this->address . ':' . $this->port);
277
      return LDAP_CONNECT_ERROR;
278
    }
279

    
280
    ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
281
    ldap_set_option($con, LDAP_OPT_REFERRALS, (int)$this->followrefs);
282

    
283
    // Use TLS if we are configured and able to.
284
    if ($this->tls) {
285
      ldap_get_option($con, LDAP_OPT_PROTOCOL_VERSION, $vers);
286
      if ($vers == -1) {
287
        watchdog('user', 'Could not get LDAP protocol version.');
288
        return LDAP_PROTOCOL_ERROR;
289
      }
290
      if ($vers != 3) {
291
        watchdog('user', 'Could not start TLS, only supported by LDAP v3.');
292
        return LDAP_CONNECT_ERROR;
293
      }
294
      elseif (!function_exists('ldap_start_tls')) {
295
        watchdog('user', 'Could not start TLS. It does not seem to be supported by this PHP setup.');
296
        return LDAP_CONNECT_ERROR;
297
      }
298
      elseif (!ldap_start_tls($con)) {
299
        $msg =  t("Could not start TLS. (Error %errno: %error).", array('%errno' => ldap_errno($con), '%error' => ldap_error($con)));
300
        watchdog('user', $msg);
301
        return LDAP_CONNECT_ERROR;
302
      }
303
    }
304

    
305
  // Store the resulting resource
306
  $this->connection = $con;
307
  return LDAP_SUCCESS;
308
  }
309

    
310

    
311
  /**
312
         * Bind (authenticate) against an active LDAP database.
313
         *
314
         * @param $userdn
315
         *   The DN to bind against. If NULL, we use $this->binddn
316
         * @param $pass
317
         *   The password search base. If NULL, we use $this->bindpw
318
   *
319
   * @return
320
   *   Result of bind; TRUE if successful, FALSE otherwise.
321
   */
322
  function bind($userdn = NULL, $pass = NULL, $anon_bind = FALSE) {
323

    
324
    // Ensure that we have an active server connection.
325
    if (!$this->connection) {
326
      watchdog('ldap', "LDAP bind failure for user %user. Not connected to LDAP server.", array('%user' => $userdn));
327
      return LDAP_CONNECT_ERROR;
328
    }
329

    
330
    if ($anon_bind !== FALSE && $userdn === NULL && $pass === NULL && $this->bind_method == LDAP_SERVERS_BIND_METHOD_ANON) {
331
      $anon_bind = TRUE;
332
    }
333
    if ($anon_bind === TRUE) {
334
      if (@!ldap_bind($this->connection)) {
335
        if ($this->detailedWatchdogLog) {
336
          watchdog('ldap', "LDAP anonymous bind error. Error %errno: %error", array('%errno' => ldap_errno($this->connection), '%error' => ldap_error($this->connection)));
337
        }
338
        return ldap_errno($this->connection);
339
      }
340
    }
341
    else {
342
      $userdn = ($userdn != NULL) ? $userdn : $this->binddn;
343
      $pass = ($pass != NULL) ? $pass : $this->bindpw;
344

    
345
      if ($this->followrefs) {
346
        $rebHandler = new LdapServersRebindHandler($userdn, $pass);
347
        ldap_set_rebind_proc($this->connection, array($rebHandler, 'rebind_callback'));
348
      }
349

    
350
      if (drupal_strlen($pass) == 0 || drupal_strlen($userdn) == 0) {
351
        watchdog('ldap', "LDAP bind failure for user userdn=%userdn, pass=%pass.", array('%userdn' => $userdn, '%pass' => $pass));
352
        return LDAP_LOCAL_ERROR;
353
      }
354
      if (@!ldap_bind($this->connection, $userdn, $pass)) {
355
        if ($this->detailedWatchdogLog) {
356
          watchdog('ldap', "LDAP bind failure for user %user. Error %errno: %error", array('%user' => $userdn, '%errno' => ldap_errno($this->connection), '%error' => ldap_error($this->connection)));
357
        }
358
        return ldap_errno($this->connection);
359
      }
360
    }
361

    
362
    return LDAP_SUCCESS;
363
  }
364

    
365
  /**
366
   * Disconnect (unbind) from an active LDAP server.
367
   */
368
  function disconnect() {
369
    if (!$this->connection) {
370
      // never bound or not currently bound, so no need to disconnect
371
      //watchdog('ldap', 'LDAP disconnect failure from '. $this->server_addr . ':' . $this->port);
372
    }
373
    else {
374
      ldap_unbind($this->connection);
375
      $this->connection = NULL;
376
    }
377
  }
378

    
379
  public function connectAndBindIfNotAlready() {
380
    if (! $this->connection) {
381
      $this->connect();
382
      $this->bind();
383
    }
384
  }
385

    
386
/**
387
 * does dn exist for this server?
388
 * [ ] Finished
389
 * [ ] Test Coverage.  Test ID:
390
 * [ ] Case insensitive
391
 *
392
 * @param string $dn
393
 * @param enum $return = 'boolean' or 'ldap_entry'
394
 * @param array $attributes in same form as ldap_read $attributes parameter
395
 *
396
 * @param return FALSE or ldap entry array
397
 */
398
  function dnExists($dn, $return = 'boolean', $attributes = NULL) {
399

    
400
    $params = array(
401
      'base_dn' => $dn,
402
      'attributes' => $attributes,
403
      'attrsonly' => FALSE,
404
      'filter' => '(objectclass=*)',
405
      'sizelimit' => 0,
406
      'timelimit' => 0,
407
      'deref' => NULL,
408
    );
409

    
410
    if ($return == 'boolean' || !is_array($attributes)) {
411
      $params['attributes'] = array('objectclass');
412
    }
413
    else {
414
      $params['attributes'] = $attributes;
415
    }
416

    
417
    $result = $this->ldapQuery(LDAP_SCOPE_BASE, $params);
418
    if ($result !== FALSE) {
419
      $entries = @ldap_get_entries($this->connection, $result);
420
      if ($entries !== FALSE && $entries['count'] > 0) {
421
        return ($return == 'boolean') ? TRUE : $entries[0];
422
      }
423
    }
424

    
425
    return FALSE;
426

    
427
  }
428

    
429
  /**
430
   * @param $ldap_result as ldap link identifier
431
   *
432
   * @return FALSE on error or number of entries.
433
   *   (if 0 entries will return 0)
434
   */
435
  public function countEntries($ldap_result) {
436
    return ldap_count_entries($this->connection, $ldap_result);
437
  }
438

    
439

    
440

    
441
  /**
442
   * create ldap entry.
443
   *
444
   * @param array $attributes should follow the structure of ldap_add functions
445
   *   entry array: http://us.php.net/manual/en/function.ldap-add.php
446
        $attributes["attribute1"] = "value";
447
        $attributes["attribute2"][0] = "value1";
448
        $attributes["attribute2"][1] = "value2";
449
   * @return boolean result
450
   */
451

    
452
  public function createLdapEntry($attributes, $dn = NULL) {
453

    
454
    if (!$this->connection) {
455
      $this->connect();
456
      $this->bind();
457
    }
458
    if (isset($attributes['dn'])) {
459
      $dn = $attributes['dn'];
460
      unset($attributes['dn']);
461
    }
462
    elseif (!$dn) {
463
      return FALSE;
464
    }
465

    
466
    $result = @ldap_add($this->connection, $dn, $attributes);
467
    if (!$result) {
468
      $error = "LDAP Server ldap_add(%dn) Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
469
      $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
470
      debug(t($error, $tokens));
471
      watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
472
    }
473

    
474
    return $result;
475
  }
476

    
477

    
478

    
479
/**
480
 * given 2 ldap entries, old and new, removed unchanged values to avoid security errors and incorrect date modifieds
481
 *
482
 * @param ldap entry array $new_entry in form <attribute> => <value>
483
 * @param ldap entry array $old_entry in form <attribute> => array('count' => N, array(<value>,...<value>
484
 *
485
 * @return ldap array with no values that have NOT changed
486
 */
487

    
488
  static public function removeUnchangedAttributes($new_entry, $old_entry) {
489

    
490
    foreach ($new_entry as $key => $new_val) {
491
      $old_value = FALSE;
492
      $old_value_is_scalar = FALSE;
493
      $key_lcase = drupal_strtolower($key);
494
      if (isset($old_entry[$key_lcase])) {
495
        if ($old_entry[$key_lcase]['count'] == 1) {
496
          $old_value = $old_entry[$key_lcase][0];
497
          $old_value_is_scalar = TRUE;
498
        }
499
        else {
500
          unset($old_entry[$key_lcase]['count']);
501
          $old_value = $old_entry[$key_lcase];
502
          $old_value_is_scalar = FALSE;
503
        }
504
      }
505

    
506
      // identical multivalued attributes
507
      if (is_array($new_val) && is_array($old_value) && count(array_diff($new_val, $old_value)) == 0) {
508
        unset($new_entry[$key]);
509
      }
510
      elseif ($old_value_is_scalar && !is_array($new_val) && drupal_strtolower($old_value) == drupal_strtolower($new_val)) {
511
        unset($new_entry[$key]); // don't change values that aren't changing to avoid false permission constraints
512
      }
513
    }
514
    return $new_entry;
515
  }
516

    
517

    
518

    
519

    
520

    
521
  /**
522
   * modify attributes of ldap entry
523
   *
524
   * @param string $dn DN of entry
525
   * @param array $attributes should follow the structure of ldap_add functions
526
   *   entry array: http://us.php.net/manual/en/function.ldap-add.php
527
        $attributes["attribute1"] = "value";
528
        $attributes["attribute2"][0] = "value1";
529
        $attributes["attribute2"][1] = "value2";
530

531
    @return TRUE on success FALSE on error
532
   */
533

    
534
  function modifyLdapEntry($dn, $attributes = array(), $old_attributes = FALSE) {
535

    
536
    $this->connectAndBindIfNotAlready();
537

    
538
    if (!$old_attributes) {
539
      $result = @ldap_read($this->connection, $dn, 'objectClass=*');
540
      if (!$result) {
541
        $error = "LDAP Server ldap_read(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
542
        $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
543
        watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
544
        return FALSE;
545
      }
546

    
547
      $entries = ldap_get_entries($this->connection, $result);
548
      if (is_array($entries) && $entries['count'] == 1) {
549
        $old_attributes =  $entries[0];
550
      }
551
    }
552
    $attributes = $this->removeUnchangedAttributes($attributes, $old_attributes);
553

    
554
    foreach ($attributes as $key => $cur_val) {
555
      $old_value = FALSE;
556
      $key_lcase = drupal_strtolower($key);
557
      if (isset($old_attributes[$key_lcase])) {
558
        if ($old_attributes[$key_lcase]['count'] == 1) {
559
          $old_value = $old_attributes[$key_lcase][0];
560
        }
561
        else {
562
          unset($old_attributes[$key_lcase]['count']);
563
          $old_value = $old_attributes[$key_lcase];
564
        }
565
      }
566

    
567
      if ($cur_val == '' && $old_value != '') { // remove enpty attributes
568
        unset($attributes[$key]);
569
        $result = @ldap_mod_del($this->connection, $dn, array($key_lcase => $old_value));
570
        if (!$result) {
571
          $error = "LDAP Server ldap_mod_del(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
572
          $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
573
          watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
574
          return FALSE;
575
        }
576
      }
577
      elseif (is_array($cur_val)) {
578
        foreach ($cur_val as $mv_key => $mv_cur_val) {
579
          if ($mv_cur_val == '') {
580
            unset($attributes[$key][$mv_key]); // remove empty values in multivalues attributes
581
          }
582
          else {
583
            $attributes[$key][$mv_key] = $mv_cur_val;
584
          }
585
        }
586
      }
587
    }
588

    
589
    if (count($attributes) > 0) {
590
      $result = @ldap_modify($this->connection, $dn, $attributes);
591
      if (!$result) {
592
        $error = "LDAP Server ldap_modify(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
593
        $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
594
        watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
595
        return FALSE;
596
      }
597
    }
598

    
599
    return TRUE;
600

    
601
  }
602

    
603
  /**
604
   * Perform an LDAP delete.
605
   *
606
   * @param string $dn
607
   *
608
   * @return boolean result per ldap_delete
609
   */
610

    
611
  public function delete($dn) {
612
    if (!$this->connection) {
613
      $this->connect();
614
      $this->bind();
615
    }
616
    $result = @ldap_delete($this->connection, $dn);
617
    if (!$result) {
618
      $error = "LDAP Server delete(%dn) in LdapServer::delete() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
619
      $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
620
      watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
621
    }
622
    return $result;
623
  }
624

    
625
  /**
626
   * Perform an LDAP search on all base dns and aggregate into one result
627
   *
628
   * @param string $filter
629
   *   The search filter. such as sAMAccountName=jbarclay.  attribute values (e.g. jbarclay) should be esacaped before calling
630

631
   * @param array $attributes
632
   *   List of desired attributes. If omitted, we only return "dn".
633
   *
634
   * @remaining params mimick ldap_search() function params
635
   *
636
   * @return
637
   *   An array of matching entries->attributes (will have 0
638
   *   elements if search returns no results),
639
   *   or FALSE on error on any of the basedn queries
640
   */
641

    
642
  public function searchAllBaseDns(
643
    $filter,
644
    $attributes = array(),
645
    $attrsonly = 0,
646
    $sizelimit = 0,
647
    $timelimit = 0,
648
    $deref = NULL,
649
    $scope = LDAP_SCOPE_SUBTREE
650
    ) {
651
    $all_entries = array();
652
    foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
653
      $entries = $this->search($base_dn, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref, $scope);  // no attributes, just dns needed
654
      if ($entries === FALSE) { // if error in any search, return false
655
        return FALSE;
656
      }
657
      if (count($all_entries) == 0) {
658
        $all_entries = $entries;
659
        unset($all_entries['count']);
660
      }
661
      else {
662
        $existing_count = count($all_entries);
663
        unset($entries['count']);
664
        foreach ($entries as $i => $entry) {
665
          $all_entries[$existing_count + $i] = $entry;
666
        }
667
      }
668
    }
669
    $all_entries['count'] = count($all_entries);
670
    return $all_entries;
671

    
672
  }
673

    
674

    
675
  /**
676
   * Perform an LDAP search.
677
   * @param string $basedn
678
   *   The search base. If NULL, we use $this->basedn. should not be esacaped
679
   *
680
   * @param string $filter
681
   *   The search filter. such as sAMAccountName=jbarclay.  attribute values (e.g. jbarclay) should be esacaped before calling
682

683
   * @param array $attributes
684
   *   List of desired attributes. If omitted, we only return "dn".
685
   *
686
   * @remaining params mimick ldap_search() function params
687
   *
688
   * @return
689
   *   An array of matching entries->attributes (will have 0
690
   *   elements if search returns no results),
691
   *   or FALSE on error.
692
   */
693

    
694
  function search($base_dn = NULL, $filter, $attributes = array(),
695
    $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = NULL, $scope = LDAP_SCOPE_SUBTREE) {
696

    
697
     /**
698
      * pagingation issues:
699
      * -- see documentation queue: http://markmail.org/message/52w24iae3g43ikix#query:+page:1+mid:bez5vpl6smgzmymy+state:results
700
      * -- wait for php 5.4? https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC6/NEWS (ldap_control_paged_result
701
      * -- http://sgehrig.wordpress.com/2009/11/06/reading-paged-ldap-results-with-php-is-a-show-stopper/
702
      */
703

    
704

    
705
    if ($base_dn == NULL) {
706
      if (count($this->basedn) == 1) {
707
        $base_dn = $this->basedn[0];
708
      }
709
      else {
710
        return FALSE;
711
      }
712
    }
713

    
714
    $attr_display =  is_array($attributes) ? join(',', $attributes) : 'none';
715
    $query = 'ldap_search() call: ' . join(",\n", array(
716
      'base_dn: ' . $base_dn,
717
      'filter = ' . $filter,
718
      'attributes: ' . $attr_display,
719
      'attrsonly = ' . $attrsonly,
720
      'sizelimit = ' . $sizelimit,
721
      'timelimit = ' . $timelimit,
722
      'deref = ' . $deref,
723
      'scope = ' . $scope,
724
      )
725
    );
726
    if ($this->detailed_watchdog_log) {
727
      watchdog('ldap_server', $query, array());
728
    }
729

    
730
    // When checking multiple servers, there's a chance we might not be connected yet.
731
    if (! $this->connection) {
732
      $this->connect();
733
      $this->bind();
734
    }
735

    
736
    $ldap_query_params = array(
737
      'connection' => $this->connection,
738
      'base_dn' => $base_dn,
739
      'filter' => $filter,
740
      'attributes' => $attributes,
741
      'attrsonly' => $attrsonly,
742
      'sizelimit' => $sizelimit,
743
      'timelimit' => $timelimit,
744
      'deref' => $deref,
745
      'query_display' => $query,
746
      'scope' => $scope,
747
    );
748

    
749
    if ($this->searchPagination && $this->paginationEnabled) {
750
      $aggregated_entries = $this->pagedLdapQuery($ldap_query_params);
751
      return $aggregated_entries;
752
    }
753
    else {
754
      $result = $this->ldapQuery($scope, $ldap_query_params);
755
      if ($result && ($this->countEntries($result) !== FALSE) ) {
756
        $entries = ldap_get_entries($this->connection, $result);
757
        drupal_alter('ldap_server_search_results', $entries, $ldap_query_params);
758
        return (is_array($entries)) ? $entries : FALSE;
759
      }
760
      elseif ($this->ldapErrorNumber()) {
761
        $watchdog_tokens =  array('%basedn' => $ldap_query_params['base_dn'], '%filter' => $ldap_query_params['filter'],
762
          '%attributes' => print_r($ldap_query_params['attributes'], TRUE), '%errmsg' => $this->errorMsg('ldap'),
763
          '%errno' => $this->ldapErrorNumber());
764
        watchdog('ldap', "LDAP ldap_search error. basedn: %basedn| filter: %filter| attributes:
765
          %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
766
        return FALSE;
767
      }
768
      else {
769
        return FALSE;
770
      }
771
    }
772
  }
773

    
774

    
775
  /**
776
   * execute a paged ldap query and return entries as one aggregated array
777
   *
778
   * $this->searchPageStart and $this->searchPageEnd should be set before calling if
779
   *   a particular set of pages is desired
780
   *
781
   * @param array $ldap_query_params of form:
782
      'base_dn' => base_dn,
783
      'filter' =>  filter,
784
      'attributes' => attributes,
785
      'attrsonly' => attrsonly,
786
      'sizelimit' => sizelimit,
787
      'timelimit' => timelimit,
788
      'deref' => deref,
789
      'scope' => scope,
790

791
      (this array of parameters is primarily passed on to ldapQuery() method)
792
   *
793
   * @return array of ldap entries or FALSE on error.
794
   *
795
   */
796
  public function pagedLdapQuery($ldap_query_params) {
797

    
798
    if (!($this->searchPagination && $this->paginationEnabled)) {
799
      watchdog('ldap', "LDAP server pagedLdapQuery() called when functionality not available in php install or
800
        not enabled in ldap server configuration.  error. basedn: %basedn| filter: %filter| attributes:
801
         %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
802
      RETURN FALSE;
803
    }
804

    
805
    $paged_entries = array();
806
    $page_token = '';
807
    $page = 0;
808
    $estimated_entries = 0;
809
    $aggregated_entries = array();
810
    $aggregated_entries_count = 0;
811
    $has_page_results = FALSE;
812

    
813
    do {
814
      ldap_control_paged_result($this->connection, $this->searchPageSize, TRUE, $page_token);
815
      $result = $this->ldapQuery($ldap_query_params['scope'], $ldap_query_params);
816

    
817
      if ($page >= $this->searchPageStart) {
818
        $skipped_page = FALSE;
819
        if ($result && ($this->countEntries($result) !== FALSE) ) {
820
          $page_entries = ldap_get_entries($this->connection, $result);
821
          unset($page_entries['count']);
822
          $has_page_results = (is_array($page_entries) && count($page_entries) > 0);
823
          $aggregated_entries = array_merge($aggregated_entries, $page_entries);
824
          $aggregated_entries_count = count($aggregated_entries);
825
        }
826
        elseif ($this->ldapErrorNumber()) {
827
          $watchdog_tokens =  array('%basedn' => $ldap_query_params['base_dn'], '%filter' => $ldap_query_params['filter'],
828
            '%attributes' => print_r($ldap_query_params['attributes'], TRUE), '%errmsg' => $this->errorMsg('ldap'),
829
            '%errno' => $this->ldapErrorNumber());
830
          watchdog('ldap', "LDAP ldap_search error. basedn: %basedn| filter: %filter| attributes:
831
            %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
832
          RETURN FALSE;
833
        }
834
        else {
835
          return FALSE;
836
        }
837
      }
838
      else {
839
        $skipped_page = TRUE;
840
      }
841
      @ldap_control_paged_result_response($this->connection, $result, $page_token, $estimated_entries);
842
      if ($ldap_query_params['sizelimit'] && $this->ldapErrorNumber() == LDAP_SIZELIMIT_EXCEEDED) {
843
        // false positive error thrown.  do not set result limit error when $sizelimit specified
844
      }
845
      elseif ($this->hasError()) {
846
        watchdog('ldap_server', 'ldap_control_paged_result_response() function error. LDAP Error: %message, ldap_list() parameters: %query',
847
          array('%message' => $this->errorMsg('ldap'), '%query' => $ldap_query_params['query_display']),
848
          WATCHDOG_ERROR);
849
      }
850

    
851
      if (isset($ldap_query_params['sizelimit']) && $ldap_query_params['sizelimit'] && $aggregated_entries_count >= $ldap_query_params['sizelimit']) {
852
        $discarded_entries = array_splice($aggregated_entries, $ldap_query_params['sizelimit']);
853
        break;
854
      }
855
      elseif ($this->searchPageEnd !== NULL && $page >= $this->searchPageEnd) { // user defined pagination has run out
856
        break;
857
      }
858
      elseif ($page_token === NULL || $page_token == '') { // ldap reference pagination has run out
859
        break;
860
      }
861
      $page++;
862
    } while ($skipped_page || $has_page_results);
863

    
864
    $aggregated_entries['count'] = count($aggregated_entries);
865
    return $aggregated_entries;
866
  }
867

    
868
  /**
869
   * execute ldap query and return ldap records
870
   *
871
   * @param scope
872
   * @params see pagedLdapQuery $params
873
   *
874
   * @return array of ldap entries
875
   */
876
  function ldapQuery($scope, $params) {
877

    
878
    $this->connectAndBindIfNotAlready();
879

    
880
    switch ($scope) {
881
      case LDAP_SCOPE_SUBTREE:
882
        $result = @ldap_search($this->connection, $params['base_dn'], $params['filter'], $params['attributes'], $params['attrsonly'],
883
          $params['sizelimit'], $params['timelimit'], $params['deref']);
884
        if ($params['sizelimit'] && $this->ldapErrorNumber() == LDAP_SIZELIMIT_EXCEEDED) {
885
          // false positive error thrown.  do not return result limit error when $sizelimit specified
886
        }
887
        elseif ($this->hasError()) {
888
          watchdog('ldap_server', 'ldap_search() function error. LDAP Error: %message, ldap_search() parameters: %query',
889
            array('%message' => $this->errorMsg('ldap'), '%query' => $params['query_display']),
890
            WATCHDOG_ERROR);
891
        }
892
        break;
893

    
894
      case LDAP_SCOPE_BASE:
895
        $result = @ldap_read($this->connection, $params['base_dn'], $params['filter'], $params['attributes'], $params['attrsonly'],
896
          $params['sizelimit'], $params['timelimit'], $params['deref']);
897
        if ($params['sizelimit'] && $this->ldapErrorNumber() == LDAP_SIZELIMIT_EXCEEDED) {
898
          // false positive error thrown.  do not result limit error when $sizelimit specified
899
        }
900
        elseif ($this->hasError()) {
901
          watchdog('ldap_server', 'ldap_read() function error.  LDAP Error: %message, ldap_read() parameters: %query',
902
            array('%message' => $this->errorMsg('ldap'), '%query' => @$params['query_display']),
903
            WATCHDOG_ERROR);
904
        }
905
        break;
906

    
907
      case LDAP_SCOPE_ONELEVEL:
908
        $result = @ldap_list($this->connection, $params['base_dn'], $params['filter'], $params['attributes'], $params['attrsonly'],
909
          $params['sizelimit'], $params['timelimit'], $params['deref']);
910
        if ($params['sizelimit'] && $this->ldapErrorNumber() == LDAP_SIZELIMIT_EXCEEDED) {
911
          // false positive error thrown.  do not result limit error when $sizelimit specified
912
        }
913
        elseif ($this->hasError()) {
914
          watchdog('ldap_server', 'ldap_list() function error. LDAP Error: %message, ldap_list() parameters: %query',
915
            array('%message' => $this->errorMsg('ldap'), '%query' => $params['query_display']),
916
            WATCHDOG_ERROR);
917
        }
918
        break;
919
    }
920
    return $result;
921
  }
922

    
923
  /**
924
   * @param array $dns Mixed Case
925
   * @return array $dns Lower Case
926
   */
927

    
928
  public function dnArrayToLowerCase($dns) {
929
    return array_keys(array_change_key_case(array_flip($dns), CASE_LOWER));
930
  }
931

    
932
  /**
933
   * @param binary or string $puid as returned from ldap_read or other ldap function
934
   *
935
   */
936
  public function userUserEntityFromPuid($puid) {
937

    
938
    $query = new EntityFieldQuery();
939
    $query->entityCondition('entity_type', 'user')
940
    ->fieldCondition('ldap_user_puid_sid', 'value', $this->sid, '=')
941
    ->fieldCondition('ldap_user_puid', 'value', $puid, '=')
942
    ->fieldCondition('ldap_user_puid_property', 'value', $this->unique_persistent_attr, '=')
943
    ->addMetaData('account', user_load(1)); // run the query as user 1
944

    
945
    $result = $query->execute();
946

    
947
    if (isset($result['user'])) {
948
      $uids = array_keys($result['user']);
949
      if (count($uids) == 1) {
950
        $user = entity_load('user', array_keys($result['user']));
951
        return $user[$uids[0]];
952
      }
953
      else {
954
        $uids = join(',', $uids);
955
        $tokens = array('%uids' => $uids, '%puid' => $puid, '%sid' =>  $this->sid, '%ldap_user_puid_property' =>  $this->unique_persistent_attr);
956
        watchdog('ldap_server', 'multiple users (uids: %uids) with same puid (puid=%puid, sid=%sid, ldap_user_puid_property=%ldap_user_puid_property)', $tokens, WATCHDOG_ERROR);
957
        return FALSE;
958
      }
959
    }
960
    else {
961
      return FALSE;
962
    }
963

    
964
  }
965

    
966
  function userUsernameToLdapNameTransform($drupal_username, &$watchdog_tokens) {
967
    if ($this->ldapToDrupalUserPhp && module_exists('php')) {
968
      global $name;
969
      $old_name_value = $name;
970
      $name = $drupal_username;
971
      $code = "<?php global \$name; \n" . $this->ldapToDrupalUserPhp . "; \n ?>";
972
      $watchdog_tokens['%code'] = $this->ldapToDrupalUserPhp;
973
      $code_result = php_eval($code);
974
      $watchdog_tokens['%code_result'] = $code_result;
975
      $ldap_username = $code_result;
976
      $watchdog_tokens['%ldap_username'] = $ldap_username;
977
      $name = $old_name_value;  // important because of global scope of $name
978
      if ($this->detailedWatchdogLog) {
979
        watchdog('ldap_server', '%drupal_user_name tansformed to %ldap_username by applying code <code>%code</code>', $watchdog_tokens, WATCHDOG_DEBUG);
980
      }
981
    }
982
    else {
983
      $ldap_username = $drupal_username;
984
    }
985

    
986
    // Let other modules alter the ldap name
987
    $context = array(
988
      'ldap_server' => $this,
989
    );
990
    drupal_alter('ldap_servers_username_to_ldapname', $ldap_username, $drupal_username, $context);
991

    
992
    return $ldap_username;
993

    
994
  }
995

    
996

    
997
 /**
998
   * @param ldap entry array $ldap_entry
999
   *
1000
   * @return string user's username value
1001
   */
1002
  public function userUsernameFromLdapEntry($ldap_entry) {
1003

    
1004

    
1005
    if ($this->account_name_attr) {
1006
      $accountname = (empty($ldap_entry[$this->account_name_attr][0])) ? FALSE : $ldap_entry[$this->account_name_attr][0];
1007
    }
1008
    elseif ($this->user_attr)  {
1009
      $accountname = (empty($ldap_entry[$this->user_attr][0])) ? FALSE : $ldap_entry[$this->user_attr][0];
1010
    }
1011
    else {
1012
      $accountname = FALSE;
1013
    }
1014

    
1015
    return $accountname;
1016
  }
1017

    
1018
 /**
1019
   * @param string $dn ldap dn
1020
   *
1021
   * @return mixed string user's username value of FALSE
1022
   */
1023
  public function userUsernameFromDn($dn) {
1024

    
1025
    $ldap_entry = @$this->dnExists($dn, 'ldap_entry', array());
1026
    if (!$ldap_entry || !is_array($ldap_entry)) {
1027
      return FALSE;
1028
    }
1029
    else {
1030
      return $this->userUsernameFromLdapEntry($ldap_entry);
1031
    }
1032

    
1033
  }
1034

    
1035
  /**
1036
   * @param ldap entry array $ldap_entry
1037
   *
1038
   * @return string user's mail value or FALSE if none present
1039
   */
1040
  public function userEmailFromLdapEntry($ldap_entry) {
1041

    
1042
    if ($ldap_entry && $this->mail_attr) { // not using template
1043
      $mail = isset($ldap_entry[$this->mail_attr][0]) ? $ldap_entry[$this->mail_attr][0] : FALSE;
1044
      return $mail;
1045
    }
1046
    elseif ($ldap_entry && $this->mail_template) {  // template is of form [cn]@illinois.edu
1047
      ldap_servers_module_load_include('inc', 'ldap_servers', 'ldap_servers.functions');
1048
      return ldap_servers_token_replace($ldap_entry, $this->mail_template, 'ldap_entry');
1049
    }
1050
    else {
1051
      return FALSE;
1052
    }
1053
  }
1054

    
1055
        /**
1056
         * @param ldap entry array $ldap_entry
1057
         *
1058
         * @return drupal file object image user's thumbnail or FALSE if none present or ERROR happens.
1059
         */
1060
        public function userPictureFromLdapEntry($ldap_entry, $drupal_username = FALSE) {
1061
                if ($ldap_entry && $this->picture_attr) {
1062
                        //Check if ldap entry has been provisioned.
1063

    
1064
                        $thumb = isset($ldap_entry[$this->picture_attr][0]) ? $ldap_entry[$this->picture_attr][0] : FALSE;
1065
                        if(!$thumb){
1066
                                return FALSE;
1067
                        }
1068

    
1069
                        //Create md5 check.
1070
                        $md5thumb = md5($thumb);
1071

    
1072
                        /**
1073
                         * If existing account already has picture check if it has changed if so remove old file and create the new one
1074
                   * If picture is not set but account has md5 something is wrong exit.
1075
                         */
1076
                        if ($drupal_username && $account = user_load_by_name($drupal_username)) {
1077
        if ($account->uid == 0 || $account->uid == 1){
1078
          return FALSE;
1079
        }
1080
        if (isset($account->picture)){
1081
          // Check if image has changed
1082
          if (isset($account->data['ldap_user']['init']['thumb5md']) && $md5thumb === $account->data['ldap_user']['init']['thumb5md']){
1083
            //No change return same image
1084
            return $account->picture;
1085
          }
1086
          else {
1087
            //Image is different check wether is obj/str and remove fileobject
1088
            if (is_object($account->picture)){
1089
              file_delete($account->picture, TRUE);
1090
            }
1091
            elseif (is_string($account->picture)){
1092
              $file = file_load(intval($account->picture));
1093
              file_delete($file, TRUE);
1094
            }
1095
          }
1096
        }
1097
        elseif (isset($account->data['ldap_user']['init']['thumb5md'])) {
1098
          watchdog('ldap_server', "Some error happened during thumbnailPhoto sync");
1099
          return FALSE;
1100
        }
1101
      }
1102
                        //Create tmp file to get image format.
1103
                        $filename = uniqid();
1104
                        $fileuri = file_directory_temp() .'/'. $filename;
1105
                        $size = file_put_contents($fileuri, $thumb);
1106
                        $info = image_get_info($fileuri);
1107
                        unlink($fileuri);
1108
                        // create file object
1109
                        $file = file_save_data($thumb, 'public://' . variable_get('user_picture_path') .'/'. $filename .'.'. $info['extension']);
1110
                        $file->md5Sum = $md5thumb;
1111
                        // standard Drupal validators for user pictures
1112
                        $validators = array(
1113
                                        'file_validate_is_image' => array(),
1114
                                        'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
1115
                                        'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
1116
                        );
1117
                        $errors = file_validate($file ,$validators);
1118
                        if (empty($errors)) {
1119
                                return $file;
1120
                        }
1121
      else {
1122
                                foreach ($errors as $err => $err_val){
1123
                                        watchdog('ldap_server', "Error storing picture: %$err", array("%$err" => $err_val), WATCHDOG_ERROR);
1124
                                }
1125
                                return FALSE;
1126
                        }
1127
                }
1128
        }
1129

    
1130

    
1131
  /**
1132
   * @param ldap entry array $ldap_entry
1133
   *
1134
   * @return string user's PUID or permanent user id (within ldap), converted from binary, if applicable
1135
   */
1136
  public function userPuidFromLdapEntry($ldap_entry) {
1137

    
1138
    if ($this->unique_persistent_attr
1139
        && isset($ldap_entry[$this->unique_persistent_attr][0])
1140
        && is_scalar($ldap_entry[$this->unique_persistent_attr][0])
1141
        ) {
1142
      $puid = $ldap_entry[$this->unique_persistent_attr][0];
1143
      return ($this->unique_persistent_attr_binary) ? ldap_servers_binary($puid) : $puid;
1144
    }
1145
    else {
1146
      return FALSE;
1147
    }
1148
  }
1149

    
1150
   /**
1151
   *  @param mixed $user
1152
   *    - drupal user object (stdClass Object)
1153
   *    - ldap entry of user (array)
1154
   *    - ldap dn of user (string)
1155
   *    - drupal username of user (string)
1156
   *
1157
   *  @return array $ldap_user_entry (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1158
  */
1159
  public function user_lookup($user) {
1160
    return $this->userUserToExistingLdapEntry($user);
1161
  }
1162
  public function userUserToExistingLdapEntry($user) {
1163

    
1164
    if (is_object($user)) {
1165
      $user_ldap_entry = $this->userUserNameToExistingLdapEntry($user->name);
1166
    }
1167
    elseif (is_array($user)) {
1168
      $user_ldap_entry = $user;
1169
    }
1170
    elseif (is_scalar($user)) {
1171
      if (strpos($user, '=') === FALSE) { // username
1172
        $user_ldap_entry = $this->userUserNameToExistingLdapEntry($user);
1173
      }
1174
      else {
1175
        $user_ldap_entry = $this->dnExists($user, 'ldap_entry');
1176
      }
1177
    }
1178
    return $user_ldap_entry;
1179
  }
1180

    
1181
  /**
1182
   * Queries LDAP server for the user.
1183
   *
1184
   * @param string $drupal_user_name
1185
   *
1186
   * @param string or int $prov_event
1187
   *   This could be anything, particularly when used by other modules.  Other modules should use string like 'mymodule_myevent'
1188
   *   LDAP_USER_EVENT_ALL signifies get all attributes needed by all other contexts/ops
1189
   *
1190
   * @return associative array representing ldap data of a user.  for example of returned value.
1191
   *   'sid' => ldap server id
1192
   *   'mail' => derived from ldap mail (not always populated).
1193
   *   'dn'   => dn of user
1194
   *   'attr' => single ldap entry array in form returned from ldap_search() extension, e.g.
1195
   *   'dn' => dn of entry
1196
   */
1197
  function userUserNameToExistingLdapEntry($drupal_user_name, $ldap_context = NULL) {
1198

    
1199
    $watchdog_tokens = array('%drupal_user_name' => $drupal_user_name);
1200
    $ldap_username = $this->userUsernameToLdapNameTransform($drupal_user_name, $watchdog_tokens);
1201
    if (!$ldap_username) {
1202
      return FALSE;
1203
    }
1204
    if (!$ldap_context) {
1205
      $attributes = array();
1206
    }
1207
    else {
1208
      $attribute_maps = ldap_servers_attributes_needed($this->sid, $ldap_context);
1209
      $attributes = array_keys($attribute_maps);
1210
    }
1211

    
1212
    foreach ($this->basedn as $basedn) {
1213
      if (empty($basedn)) continue;
1214
      $filter = '(' . $this->user_attr . '=' . ldap_server_massage_text($ldap_username, 'attr_value', LDAP_SERVER_MASSAGE_QUERY_LDAP) . ')';
1215
      $result = $this->search($basedn, $filter, $attributes);
1216
      if (!$result || !isset($result['count']) || !$result['count']) continue;
1217

    
1218
      // Must find exactly one user for authentication to work.
1219

    
1220
      if ($result['count'] != 1) {
1221
        $count = $result['count'];
1222
        watchdog('ldap_servers', "Error: !count users found with $filter under $basedn.", array('!count' => $count), WATCHDOG_ERROR);
1223
        continue;
1224
      }
1225
      $match = $result[0];
1226
      // These lines serve to fix the attribute name in case a
1227
      // naughty server (i.e.: MS Active Directory) is messing the
1228
      // characters' case.
1229
      // This was contributed by Dan "Gribnif" Wilga, and described
1230
      // here: http://drupal.org/node/87833
1231
      $name_attr = $this->user_attr;
1232

    
1233
      if (isset($match[$name_attr][0])) {
1234
        // leave name
1235
      }
1236
      elseif (isset($match[drupal_strtolower($name_attr)][0])) {
1237
        $name_attr = drupal_strtolower($name_attr);
1238

    
1239
      }
1240
      else {
1241
        if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_ANON_USER) {
1242
          $result = array(
1243
            'dn' =>  $match['dn'],
1244
            'mail' => $this->userEmailFromLdapEntry($match),
1245
            'attr' => $match,
1246
            'sid' => $this->sid,
1247
            );
1248
          return $result;
1249
        }
1250
        else {
1251
          continue;
1252
        }
1253
      }
1254

    
1255
      // Finally, we must filter out results with spaces added before
1256
      // or after, which are considered OK by LDAP but are no good for us
1257
      // We allow lettercase independence, as requested by Marc Galera
1258
      // on http://drupal.org/node/97728
1259
      //
1260
      // Some setups have multiple $name_attr per entry, as pointed out by
1261
      // Clarence "sparr" Risher on http://drupal.org/node/102008, so we
1262
      // loop through all possible options.
1263
      foreach ($match[$name_attr] as $value) {
1264
        if (drupal_strtolower(trim($value)) == drupal_strtolower($ldap_username)) {
1265
          $result = array(
1266
            'dn' =>  $match['dn'],
1267
            'mail' => $this->userEmailFromLdapEntry($match),
1268
            'attr' => $match,
1269
            'sid' => $this->sid,
1270
          );
1271
          return $result;
1272
        }
1273
      }
1274
    }
1275
  }
1276

    
1277
  /**
1278
   * Is a user a member of group?
1279
   *
1280
   * @param string $group_dn MIXED CASE
1281
   * @param mixed $user
1282
   *    - drupal user object (stdClass Object)
1283
   *    - ldap entry of user (array)
1284
   *    - ldap dn of user (array)
1285
   *    - drupal user name (string)
1286
   * @param enum $nested = NULL (default to server configuration), TRUE, or FALSE indicating to test for nested groups
1287
   */
1288
  public function groupIsMember($group_dn, $user, $nested = NULL) {
1289

    
1290
    $nested = ($nested === TRUE || $nested === FALSE) ? $nested : $this->groupNested;
1291
    $group_dns = $this->groupMembershipsFromUser($user, 'group_dns', $nested);
1292
    // while list of group dns is going to be in correct mixed case, $group_dn may not since it may be derived from user entered values
1293
    // so make sure in_array() is case insensitive
1294
    return (is_array($group_dns) && in_array(drupal_strtolower($group_dn), $this->dnArrayToLowerCase($group_dns)));
1295
  }
1296

    
1297

    
1298

    
1299
  /**
1300
   * NOT TESTED
1301
   * add a group entry
1302
   *
1303
   * @param string $group_dn as ldap dn
1304
   * @param array $attributes in key value form
1305
   *    $attributes = array(
1306
   *      "attribute1" = "value",
1307
   *      "attribute2" = array("value1", "value2"),
1308
   *      )
1309
   * @return boolean success
1310
   */
1311
  public function groupAddGroup($group_dn, $attributes = array()) {
1312

    
1313
    //debug("this->dnExists(   $group_dn, boolean)"); debug($this->dnExists($group_dn, 'boolean'));
1314
   // debug("this->dnExists(   $group_dn, boolean)"); debug($this->dnExists($group_dn));
1315
    if ($this->dnExists($group_dn, 'boolean')) {
1316
      return FALSE;
1317
    }
1318

    
1319
    $attributes = array_change_key_case($attributes, CASE_LOWER);
1320
    $objectclass = (empty($attributes['objectclass'])) ? $this->groupObjectClass : $attributes['objectclass'];
1321
    $attributes['objectclass'] = $objectclass;
1322

    
1323
    /**
1324
     * 2. give other modules a chance to add or alter attributes
1325
     */
1326
    $context = array(
1327
      'action' => 'add',
1328
      'corresponding_drupal_data' => array($group_dn => $attributes),
1329
      'corresponding_drupal_data_type' => 'group',
1330
    );
1331
    $ldap_entries = array($group_dn => $attributes);
1332
    drupal_alter('ldap_entry_pre_provision', $ldap_entries, $this, $context);
1333
    $attributes = $ldap_entries[$group_dn];
1334

    
1335

    
1336
     /**
1337
     * 4. provision ldap entry
1338
     *   @todo how is error handling done here?
1339
     */
1340
    $ldap_entry_created = $this->createLdapEntry($attributes, $group_dn);
1341

    
1342

    
1343
     /**
1344
     * 5. allow other modules to react to provisioned ldap entry
1345
     *   @todo how is error handling done here?
1346
     */
1347
    if ($ldap_entry_created) {
1348
      module_invoke_all('ldap_entry_post_provision', $ldap_entries, $this, $context);
1349
      return TRUE;
1350
    }
1351
    else {
1352
      return FALSE;
1353
    }
1354

    
1355
  }
1356

    
1357
  /**
1358
   * NOT TESTED
1359
   * remove a group entry
1360
   *
1361
   * @param string $group_dn as ldap dn
1362
   * @param boolean $only_if_group_empty
1363
   *   TRUE = group should not be removed if not empty
1364
   *   FALSE = groups should be deleted regardless of members
1365
   */
1366
  public function groupRemoveGroup($group_dn, $only_if_group_empty = TRUE) {
1367

    
1368
    if ($only_if_group_empty) {
1369
      $members = $this->groupAllMembers($group_dn);
1370
      if (is_array($members) && count($members) > 0) {
1371
        return FALSE;
1372
      }
1373
    }
1374

    
1375
    return $this->delete($group_dn);
1376

    
1377
  }
1378

    
1379
  /**
1380
   * NOT TESTED
1381
   * add a member to a group
1382
   *
1383
   * @param string $ldap_user_dn as ldap dn
1384
   * @param mixed $user
1385
   *    - drupal user object (stdClass Object)
1386
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1387
   *    - ldap dn of user (array)
1388
   *    - drupal username of user (string)
1389
   */
1390
  public function groupAddMember($group_dn, $user) {
1391

    
1392
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1393
    $result = FALSE;
1394
    if ($user_ldap_entry && $this->groupGroupEntryMembershipsConfigured) {
1395
      $add = array();
1396
      $add[$this->groupMembershipsAttr] = $user_ldap_entry['dn'];
1397
      $this->connectAndBindIfNotAlready();
1398
      $result = @ldap_mod_add($this->connection, $group_dn, $add);
1399
    }
1400

    
1401
    return $result;
1402
  }
1403

    
1404
  /**
1405
   * NOT TESTED
1406
   * remove a member from a group
1407
   *
1408
   * @param string $group_dn as ldap dn
1409
   * @param mixed $user
1410
   *    - drupal user object (stdClass Object)
1411
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1412
   *    - ldap dn of user (array)
1413
   *    - drupal username of user (string)
1414
   */
1415
  public function groupRemoveMember($group_dn, $user) {
1416

    
1417
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1418
    $result = FALSE;
1419
    if ($user_ldap_entry && $this->groupGroupEntryMembershipsConfigured) {
1420
      $del = array();
1421
      $del[$this->groupMembershipsAttr] = $user_ldap_entry['dn'];
1422
      $this->connectAndBindIfNotAlready();
1423
      $result = @ldap_mod_del($this->connection, $group_dn, $del);
1424
    }
1425
    return $result;
1426
  }
1427

    
1428

    
1429
  /**
1430
   *
1431
   * @todo: NOT IMPLEMENTED: nested groups
1432
   *
1433
   * get all members of a group
1434
   *
1435
   * @param string $group_dn as ldap dn
1436
   *
1437
   * @return FALSE on error otherwise array of group members (could be users or groups)
1438
   */
1439
  public function groupAllMembers($group_dn) {
1440
   // debug("groupAllMembers $group_dn, this->groupMembershipsAttr=". $this->groupMembershipsAttr . 'this->groupGroupEntryMembershipsConfigured=' . $this->groupGroupEntryMembershipsConfigured);
1441
    if (!$this->groupGroupEntryMembershipsConfigured) {
1442
      return FALSE;
1443
    }
1444
    $attributes = array($this->groupMembershipsAttr, 'cn');
1445
    $group_entry = $this->dnExists($group_dn, 'ldap_entry', $attributes);
1446
    if (!$group_entry) {
1447
      return FALSE;
1448
    }
1449
    else {
1450
      if (empty($group_entry['cn'])) { // if attributes weren't returned, don't give false  empty group
1451
        return FALSE;
1452
      }
1453
      if (empty($group_entry[$this->groupMembershipsAttr])) {
1454
        return array(); // if no attribute returned, no members
1455
      }
1456
      $members = $group_entry[$this->groupMembershipsAttr];
1457
      if (isset($members['count'])) {
1458
        unset($members['count']);
1459
      }
1460
      return $members;
1461
    }
1462

    
1463
    $this->groupMembersResursive($current_group_entries, $all_group_dns, $tested_group_ids, 0, $max_levels, $object_classes);
1464

    
1465
    return $all_group_dns;
1466

    
1467
  }
1468

    
1469
/**
1470
   *   NOT IMPLEMENTED
1471
   * recurse through all child groups and add members.
1472
   *
1473
   * @param array $current_group_entries of ldap group entries that are starting point.  should include at least 1 entry.
1474
   * @param array $all_group_dns as array of all groups user is a member of.  MIXED CASE VALUES
1475
   * @param array $tested_group_ids as array of tested group dn, cn, uid, etc.  MIXED CASE VALUES
1476
   *   whether these value are dn, cn, uid, etc depends on what attribute members, uniquemember, memberUid contains
1477
   *   whatever attribute is in $this->$tested_group_ids to avoid redundant recursing
1478
   * @param int $level of recursion
1479
   * @param int $max_levels as max recursion allowed
1480
   *
1481
   */
1482

    
1483
  public function groupMembersResursive($current_member_entries, &$all_member_dns, &$tested_group_ids, $level, $max_levels, $object_classes = FALSE) {
1484

    
1485
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_member_entries) || count($current_member_entries) == 0) {
1486
      return FALSE;
1487
    }
1488
    if (isset($current_member_entries['count'])) {
1489
      unset($current_member_entries['count']);
1490
    };
1491

    
1492
    foreach ($current_member_entries as $i => $member_entry) {
1493
      //dpm("groupMembersResursive:member_entry $i, level=$level < max_levels=$max_levels"); dpm($member_entry);
1494
      // 1.  Add entry itself if of the correct type to $all_member_dns
1495
      $objectClassMatch = (!$object_classes || (count(array_intersect(array_values($member_entry['objectclass']), $object_classes)) > 0));
1496
      $objectIsGroup = in_array($this->groupObjectClass, array_values($member_entry['objectclass']));
1497
      if ($objectClassMatch && !in_array($member_entry['dn'], $all_member_dns)) { // add member
1498
        $all_member_dns[] = $member_entry['dn'];
1499
      }
1500

    
1501
      // 2. If its a group, keep recurse the group for descendants
1502
      if ($objectIsGroup && $level < $max_levels) {
1503
        if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1504
          $group_id = $member_entry['dn'];
1505
        }
1506
        else {
1507
          $group_id = $member_entry[$this->groupMembershipsAttrMatchingUserAttr][0];
1508
        }
1509
        // 3. skip any groups that have already been tested
1510
        if (!in_array($group_id, $tested_group_ids)) {
1511
          $tested_group_ids[] = $group_id;
1512
          $member_ids = $member_entry[$this->groupMembershipsAttr];
1513
          if (isset($member_ids['count'])) {
1514
            unset($member_ids['count']);
1515
          };
1516
          $ors = array();
1517
          foreach ($member_ids as $i => $member_id) {
1518
            $ors[] =  $this->groupMembershipsAttr . '=' . ldap_pear_escape_filter_value($member_id); // @todo this would be replaced by query template
1519
          }
1520

    
1521
          if (count($ors)) {
1522
            $query_for_child_members = '(|(' . join(")(", $ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1523
            if (count($object_classes)) { // add or on object classe, otherwise get all object classes
1524
              $object_classes_ors = array('(objectClass=' . $this->groupObjectClass . ')');
1525
              foreach ($object_classes as $object_class) {
1526
                $object_classes_ors[] = '(objectClass=' . $object_class . ')';
1527
              }
1528
              $query_for_child_members = '&(|' . join($object_classes_ors) . ')(' . $query_for_child_members . ')';
1529
            }
1530
            foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1531
              $child_member_entries = $this->search($base_dn, $query_for_child_members, array('objectclass', $this->groupMembershipsAttr, $this->groupMembershipsAttrMatchingUserAttr));
1532
              if ($child_member_entries !== FALSE) {
1533
                $this->groupMembersResursive($child_member_entries, $all_member_dns, $tested_group_ids, $level + 1, $max_levels, $object_classes);
1534
              }
1535
            }
1536
          }
1537
        }
1538
      }
1539
    }
1540
  }
1541

    
1542

    
1543
 /**
1544
  /**
1545
   *  get list of all groups that a user is a member of.
1546
   *
1547
   *    If $nested = TRUE,
1548
   *    list will include all parent group.  That is if user is a member of "programmer" group
1549
   *    and "programmer" group is a member of "it" group, user is a member of
1550
   *    both "programmer" and "it" groups.
1551
   *
1552
   *    If $nested = FALSE, list will only include groups user is in directly.
1553
   *
1554
   *  @param mixed
1555
   *    - drupal user object (stdClass Object)
1556
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1557
   *    - ldap dn of user (array)
1558
   *    - drupal username of user (string)
1559
   *  @param enum $return = 'group_dns'
1560
   *  @param boolean $nested if groups should be recursed or not.
1561
   *
1562
   *  @return array of groups dns in mixed case or FALSE on error
1563
   */
1564

    
1565
  public function groupMembershipsFromUser($user, $return = 'group_dns', $nested = NULL) {
1566

    
1567
    $group_dns = FALSE;
1568
    $user_ldap_entry = @$this->userUserToExistingLdapEntry($user);
1569
    if (!$user_ldap_entry || $this->groupFunctionalityUnused) {
1570
      return FALSE;
1571
    }
1572
    if ($nested === NULL) {
1573
      $nested = $this->groupNested;
1574
    }
1575

    
1576
    if ($this->groupUserMembershipsConfigured) { // preferred method
1577
      $group_dns = $this->groupUserMembershipsFromUserAttr($user_ldap_entry, $nested);
1578
    }
1579
    elseif ($this->groupGroupEntryMembershipsConfigured) {
1580
      $group_dns = $this->groupUserMembershipsFromEntry($user_ldap_entry, $nested);
1581
    }
1582
    else {
1583
      watchdog('ldap_servers', 'groupMembershipsFromUser: Group memberships for server have not been configured.', array(), WATCHDOG_WARNING);
1584
      return FALSE;
1585
    }
1586
    if ($return == 'group_dns') {
1587
      return $group_dns;
1588
    }
1589

    
1590
  }
1591

    
1592

    
1593
  /**
1594
   *  get list of all groups that a user is a member of by using memberOf attribute first,
1595
   *    then if nesting is true, using group entries to find parent groups
1596
   *
1597
   *    If $nested = TRUE,
1598
   *    list will include all parent group.  That is if user is a member of "programmer" group
1599
   *    and "programmer" group is a member of "it" group, user is a member of
1600
   *    both "programmer" and "it" groups.
1601
   *
1602
   *    If $nested = FALSE, list will only include groups user is in directly.
1603
   *
1604
   *  @param mixed
1605
   *    - drupal user object (stdClass Object)
1606
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1607
   *    - ldap dn of user (array)
1608
   *    - drupal username of user (string)
1609
   *  @param boolean $nested if groups should be recursed or not.
1610
   *
1611
   *  @return array of group dns
1612
   */
1613

    
1614
  public function groupUserMembershipsFromUserAttr($user, $nested = NULL) {
1615

    
1616
    if (!$this->groupUserMembershipsConfigured) {
1617
      return FALSE;
1618
    }
1619
    if ($nested === NULL) {
1620
      $nested = $this->groupNested;
1621
    }
1622

    
1623
    $not_user_ldap_entry = empty($user['attr'][$this->groupUserMembershipsAttr]);
1624
    if ($not_user_ldap_entry) { // if drupal user passed in, try to get user_ldap_entry
1625
      $user = $this->userUserToExistingLdapEntry($user);
1626
      $not_user_ldap_entry = empty($user['attr'][$this->groupUserMembershipsAttr]);
1627
      if ($not_user_ldap_entry) {
1628
        return FALSE; // user's membership attribute is not present.  either misconfigured or query failed
1629
      }
1630
    }
1631
    // if not exited yet, $user must be user_ldap_entry.
1632
    $user_ldap_entry = $user;
1633
    $all_group_dns = array();
1634
    $tested_group_ids = array();
1635
    $level = 0;
1636

    
1637
    $member_group_dns = $user_ldap_entry['attr'][$this->groupUserMembershipsAttr];
1638
    if (isset($member_group_dns['count'])) {
1639
      unset($member_group_dns['count']);
1640
    }
1641
    $ors = array();
1642
    foreach ($member_group_dns as $i => $member_group_dn) {
1643
      $all_group_dns[] = $member_group_dn;
1644
      if ($nested) {
1645
        if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1646
          $member_value = $member_group_dn;
1647
        }
1648
        else {
1649
          $member_value = ldap_servers_get_first_rdn_value_from_dn($member_group_dn, $this->groupMembershipsAttrMatchingUserAttr);
1650
        }
1651
        $ors[] =  $this->groupMembershipsAttr . '=' . ldap_pear_escape_filter_value($member_value);
1652
      }
1653
    }
1654

    
1655
    if ($nested && count($ors)) {
1656
      $count = count($ors);
1657
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1658
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1659
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1660
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1661

    
1662
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1663
          // debug("query for parent groups, base_dn=$base_dn, $query_for_parent_groups");
1664
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1665
          if ($group_entries !== FALSE  && $level < LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT) {
1666
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT);
1667
          }
1668
        }
1669
      }
1670
    }
1671

    
1672
    return $all_group_dns;
1673
  }
1674

    
1675
  /**
1676
   *  get list of all groups that a user is a member of by querying groups
1677
   *
1678
   *    If $nested = TRUE,
1679
   *    list will include all parent group.  That is if user is a member of "programmer" group
1680
   *    and "programmer" group is a member of "it" group, user is a member of
1681
   *    both "programmer" and "it" groups.
1682
   *
1683
   *    If $nested = FALSE, list will only include groups user is in directly.
1684
   *
1685
   *  @param mixed
1686
   *    - drupal user object (stdClass Object)
1687
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1688
   *    - ldap dn of user (array)
1689
   *    - drupal username of user (string)
1690
   *  @param boolean $nested if groups should be recursed or not.
1691
   *
1692
   *  @return array of group dns MIXED CASE VALUES
1693
   *
1694
   *  @see tests/DeriveFromEntry/ldap_servers.inc for fuller notes and test example
1695
   */
1696
  public function groupUserMembershipsFromEntry($user, $nested = NULL) {
1697

    
1698
    if (!$this->groupGroupEntryMembershipsConfigured) {
1699
      return FALSE;
1700
    }
1701
    if ($nested === NULL) {
1702
      $nested = $this->groupNested;
1703
    }
1704

    
1705
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1706

    
1707
    $all_group_dns = array(); // MIXED CASE VALUES
1708
    $tested_group_ids = array(); // array of dns already tested to avoid excess queries MIXED CASE VALUES
1709
    $level = 0;
1710

    
1711
    if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1712
      $member_value = $user_ldap_entry['dn'];
1713
    }
1714
    else {
1715
      $member_value = $user_ldap_entry['attr'][$this->groupMembershipsAttrMatchingUserAttr][0];
1716
    }
1717
    $member_value = ldap_pear_escape_filter_value($member_value);
1718
    if ($this->groupObjectClass == '') {
1719
      $group_query = '(' . $this->groupMembershipsAttr . "=$member_value)";
1720
    }
1721
    else {
1722
      $group_query = '(&(objectClass=' . $this->groupObjectClass . ')(' . $this->groupMembershipsAttr . "=$member_value))";
1723
    }
1724

    
1725
    foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1726
      $group_entries = $this->search($base_dn, $group_query, array()); // only need dn, so empty array forces return of no attributes
1727
      if ($group_entries !== FALSE) {
1728
        $max_levels = ($nested) ? LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT : 0;
1729
        $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level, $max_levels);
1730
      }
1731
    }
1732

    
1733
    return $all_group_dns;
1734
  }
1735

    
1736
  /**
1737
   * recurse through all groups, adding parent groups to $all_group_dns array.
1738
   *
1739
   * @param array $current_group_entries of ldap group entries that are starting point.  should include at least 1 entry.
1740
   * @param array $all_group_dns as array of all groups user is a member of.  MIXED CASE VALUES
1741
   * @param array $tested_group_ids as array of tested group dn, cn, uid, etc.  MIXED CASE VALUES
1742
   *   whether these value are dn, cn, uid, etc depends on what attribute members, uniquemember, memberUid contains
1743
   *   whatever attribute is in $this->$tested_group_ids to avoid redundant recursing
1744
   * @param int $level of recursion
1745
   * @param int $max_levels as max recursion allowed
1746
   *
1747
   * given set of groups entries ($current_group_entries such as it, hr, accounting),
1748
   * find parent groups (such as staff, people, users) and add them to list of group memberships ($all_group_dns)
1749
   *
1750
   * (&(objectClass=[$this->groupObjectClass])(|([$this->groupMembershipsAttr]=groupid1)([$this->groupMembershipsAttr]=groupid2))
1751
   *
1752
   * @return FALSE for error or misconfiguration, otherwise TRUE.  results are passed by reference.
1753
   */
1754

    
1755
  public function groupMembershipsFromEntryRecursive($current_group_entries, &$all_group_dns, &$tested_group_ids, $level, $max_levels) {
1756

    
1757
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_group_entries) || count($current_group_entries) == 0) {
1758
      return FALSE;
1759
    }
1760
    if (isset($current_group_entries['count'])) {
1761
      unset($current_group_entries['count']);
1762
    };
1763

    
1764
    $ors = array();
1765
    foreach ($current_group_entries as $i => $group_entry) {
1766
      if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1767
        $member_id = $group_entry['dn'];
1768
      }
1769
      else {// maybe cn, uid, etc is held
1770
        $member_id = ldap_servers_get_first_rdn_value_from_dn($group_entry['dn'], $this->groupMembershipsAttrMatchingUserAttr);
1771
        if(!$member_id) {
1772
          if ($this->detailed_watchdog_log) {
1773
             watchdog('ldap_server', 'group_entry: %ge', array('%ge'=>pretty_print_ldap_entry($group_entry)));
1774
          }
1775
          // group not identified by simple checks yet!
1776

    
1777
          // examine the entry and see if it matches the configured groupObjectClass
1778
          $goc=$group_entry['objectclass']; // TODO do we need to ensure such entry is there?
1779
          if(is_array($goc)) {              // TODO is it always an array?
1780
            foreach($goc as $g) {
1781
              $g=drupal_strtolower($g);
1782
              if($g == $this->groupObjectClass) {
1783
                // found a group, current user must be member in it - so:
1784
                if ($this->detailed_watchdog_log) {
1785
                  watchdog('ldap_server', 'adding %mi', array('%mi'=>$member_id));
1786
                }
1787
                $member_id=$group_entry['dn'];
1788
                break;
1789
              }
1790
            }
1791
          }
1792
        }
1793
      }
1794

    
1795
      if ($member_id && !in_array($member_id, $tested_group_ids)) {
1796
        $tested_group_ids[] = $member_id;
1797
        $all_group_dns[] = $group_entry['dn'];
1798
        // add $group_id (dn, cn, uid) to query
1799
        $ors[] =  $this->groupMembershipsAttr . '=' .  ldap_pear_escape_filter_value($member_id);
1800
      }
1801
    }
1802

    
1803
    if ($level < $max_levels && count($ors)) {
1804
      $count = count($ors);
1805
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1806
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1807
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1808
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1809

    
1810
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1811
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1812
          if ($group_entries !== FALSE) {
1813
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, $max_levels);
1814
          }
1815
        }
1816
      }
1817
    }
1818

    
1819
    return TRUE;
1820
  }
1821

    
1822

    
1823
 /**
1824
   *  get "groups" from derived from DN.  Has limited usefulness
1825
   *
1826
   *  @param mixed
1827
   *    - drupal user object (stdClass Object)
1828
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1829
   *    - ldap dn of user (array)
1830
   *    - drupal username of user (string)
1831
   *
1832
   *  @return array of group strings
1833
   */
1834
  public function groupUserMembershipsFromDn($user) {
1835

    
1836
    if (!$this->groupDeriveFromDn || !$this->groupDeriveFromDnAttr) {
1837
      return FALSE;
1838
    }
1839
    elseif ($user_ldap_entry = $this->userUserToExistingLdapEntry($user)) {
1840
      return ldap_servers_get_all_rdn_values_from_dn($user_ldap_entry['dn'], $this->groupDeriveFromDnAttr);
1841
    }
1842
    else {
1843
      return FALSE;
1844
    }
1845

    
1846
  }
1847
  /**
1848
   * Error methods and properties.
1849
   */
1850

    
1851
  public $detailedWatchdogLog = FALSE;
1852
  protected $_errorMsg = NULL;
1853
  protected $_hasError = FALSE;
1854
  protected $_errorName = NULL;
1855

    
1856
  public function setError($_errorName, $_errorMsgText = NULL) {
1857
    $this->_errorMsgText = $_errorMsgText;
1858
    $this->_errorName = $_errorName;
1859
    $this->_hasError = TRUE;
1860
  }
1861

    
1862
  public function clearError() {
1863
    $this->_hasError = FALSE;
1864
    $this->_errorMsg = NULL;
1865
    $this->_errorName = NULL;
1866
  }
1867

    
1868
  public function hasError() {
1869
    return ($this->_hasError || $this->ldapErrorNumber());
1870
  }
1871

    
1872
  public function errorMsg($type = NULL) {
1873
    if ($type == 'ldap' && $this->connection) {
1874
      return ldap_err2str(ldap_errno($this->connection));
1875
    }
1876
    elseif ($type == NULL) {
1877
      return $this->_errorMsg;
1878
    }
1879
    else {
1880
      return NULL;
1881
    }
1882
  }
1883

    
1884
  public function errorName($type = NULL) {
1885
    if ($type == 'ldap' && $this->connection) {
1886
      return "LDAP Error: " . ldap_error($this->connection);
1887
    }
1888
    elseif ($type == NULL) {
1889
      return $this->_errorName;
1890
    }
1891
    else {
1892
      return NULL;
1893
    }
1894
  }
1895

    
1896
  public function ldapErrorNumber() {
1897
    if ($this->connection && ldap_errno($this->connection)) {
1898
      return ldap_errno($this->connection);
1899
    }
1900
    else {
1901
      return FALSE;
1902
    }
1903
  }
1904

    
1905
}
1906

    
1907
/**
1908
 * Class for enabling rebind functionality for following referrrals.
1909
 */
1910
class LdapServersRebindHandler {
1911

    
1912
  private $bind_dn = 'Anonymous';
1913
  private $bind_passwd = '';
1914

    
1915
  public function __construct($bind_user_dn, $bind_user_passwd){
1916
    $this->bind_dn = $bind_user_dn;
1917
    $this->bind_passwd = $bind_user_passwd;
1918
  }
1919

    
1920
  public function rebind_callback($ldap, $referral){
1921
    // ldap options
1922
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
1923
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 1);
1924
    ldap_set_rebind_proc($ldap, array($this, 'rebind_callback'));
1925

    
1926
  // Bind to new host, assumes initial bind dn has access to the referred servers.
1927
    if (!ldap_bind($ldap, $this->bind_dn, $this->bind_passwd)) {
1928
      echo "Could not bind to referral server: $referral";
1929
      return 1;
1930
    }
1931
    return 0;
1932
  }
1933
}