Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ldap / ldap_servers / LdapServer.class.php @ 59ae487e

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
    }
176
    else {
177
      $select = db_select('ldap_servers')
178
        ->fields('ldap_servers')
179
        ->condition('ldap_servers.sid',  $sid)
180
        ->execute();
181
      foreach ($select as $record) {
182
        if ($record->sid == $sid) {
183
          $server_record = $record;
184
        }
185
      }
186
    }
187

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

    
206
  /**
207
   * 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
208
   */
209
  protected function initDerivedProperties($bindpw) {
210

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

    
229
    }
230

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

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

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

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

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

    
258

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

    
267

    
268

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

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

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

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

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

    
309

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

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

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

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

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

    
361
    return LDAP_SUCCESS;
362
  }
363

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

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

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

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

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

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

    
424
    return FALSE;
425

    
426
  }
427

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

    
438

    
439

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

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

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

    
465
    if (!empty($attributes['unicodePwd']) && ($this->ldap_type == 'ad')) {
466
      $attributes['unicodePwd'] = ldap_servers_convert_password_for_active_directory_unicodePwd($attributes['unicodePwd']);
467
    }
468

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

    
476
    return $result;
477
  }
478

    
479

    
480

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

    
490
  static public function removeUnchangedAttributes($new_entry, $old_entry) {
491

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

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

    
519

    
520

    
521

    
522

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

533
    @return TRUE on success FALSE on error
534
   */
535

    
536
  function modifyLdapEntry($dn, $attributes = array(), $old_attributes = FALSE) {
537

    
538
    $this->connectAndBindIfNotAlready();
539

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

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

    
555
    if (!empty($attributes['unicodePwd']) && ($this->ldap_type == 'ad')) {
556
      $attributes['unicodePwd'] = ldap_servers_convert_password_for_active_directory_unicodePwd($attributes['unicodePwd']);
557
    }
558

    
559
    $attributes = $this->removeUnchangedAttributes($attributes, $old_attributes);
560

    
561
    foreach ($attributes as $key => $cur_val) {
562
      $old_value = FALSE;
563
      $key_lcase = drupal_strtolower($key);
564
      if (isset($old_attributes[$key_lcase])) {
565
        if ($old_attributes[$key_lcase]['count'] == 1) {
566
          $old_value = $old_attributes[$key_lcase][0];
567
        }
568
        else {
569
          unset($old_attributes[$key_lcase]['count']);
570
          $old_value = $old_attributes[$key_lcase];
571
        }
572
      }
573

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

    
596
    if (count($attributes) > 0) {
597
      $result = @ldap_modify($this->connection, $dn, $attributes);
598
      if (!$result) {
599
        $error = "LDAP Server ldap_modify(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
600
        $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
601
        watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
602
        return FALSE;
603
      }
604
    }
605

    
606
    return TRUE;
607

    
608
  }
609

    
610
  /**
611
   * Perform an LDAP delete.
612
   *
613
   * @param string $dn
614
   *
615
   * @return boolean result per ldap_delete
616
   */
617

    
618
  public function delete($dn) {
619
    if (!$this->connection) {
620
      $this->connect();
621
      $this->bind();
622
    }
623
    $result = @ldap_delete($this->connection, $dn);
624
    if (!$result) {
625
      $error = "LDAP Server delete(%dn) in LdapServer::delete() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
626
      $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
627
      watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
628
    }
629
    return $result;
630
  }
631

    
632
  /**
633
   * Perform an LDAP search on all base dns and aggregate into one result
634
   *
635
   * @param string $filter
636
   *   The search filter. such as sAMAccountName=jbarclay.  attribute values (e.g. jbarclay) should be esacaped before calling
637

638
   * @param array $attributes
639
   *   List of desired attributes. If omitted, we only return "dn".
640
   *
641
   * @remaining params mimick ldap_search() function params
642
   *
643
   * @return
644
   *   An array of matching entries->attributes (will have 0
645
   *   elements if search returns no results),
646
   *   or FALSE on error on any of the basedn queries
647
   */
648

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

    
679
  }
680

    
681

    
682
  /**
683
   * Perform an LDAP search.
684
   * @param string $basedn
685
   *   The search base. If NULL, we use $this->basedn. should not be esacaped
686
   *
687
   * @param string $filter
688
   *   The search filter. such as sAMAccountName=jbarclay.  attribute values (e.g. jbarclay) should be esacaped before calling
689

690
   * @param array $attributes
691
   *   List of desired attributes. If omitted, we only return "dn".
692
   *
693
   * @remaining params mimick ldap_search() function params
694
   *
695
   * @return
696
   *   An array of matching entries->attributes (will have 0
697
   *   elements if search returns no results),
698
   *   or FALSE on error.
699
   */
700

    
701
  function search($base_dn = NULL, $filter, $attributes = array(),
702
    $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = NULL, $scope = LDAP_SCOPE_SUBTREE) {
703

    
704
     /**
705
      * pagingation issues:
706
      * -- see documentation queue: http://markmail.org/message/52w24iae3g43ikix#query:+page:1+mid:bez5vpl6smgzmymy+state:results
707
      * -- wait for php 5.4? https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC6/NEWS (ldap_control_paged_result
708
      * -- http://sgehrig.wordpress.com/2009/11/06/reading-paged-ldap-results-with-php-is-a-show-stopper/
709
      */
710

    
711

    
712
    if ($base_dn == NULL) {
713
      if (count($this->basedn) == 1) {
714
        $base_dn = $this->basedn[0];
715
      }
716
      else {
717
        return FALSE;
718
      }
719
    }
720

    
721
    $attr_display =  is_array($attributes) ? join(',', $attributes) : 'none';
722
    $query = 'ldap_search() call: ' . join(",\n", array(
723
      'base_dn: ' . $base_dn,
724
      'filter = ' . $filter,
725
      'attributes: ' . $attr_display,
726
      'attrsonly = ' . $attrsonly,
727
      'sizelimit = ' . $sizelimit,
728
      'timelimit = ' . $timelimit,
729
      'deref = ' . $deref,
730
      'scope = ' . $scope,
731
      )
732
    );
733
    if ($this->detailed_watchdog_log) {
734
      watchdog('ldap_server', $query, array());
735
    }
736

    
737
    // When checking multiple servers, there's a chance we might not be connected yet.
738
    if (! $this->connection) {
739
      $this->connect();
740
      $this->bind();
741
    }
742

    
743
    $ldap_query_params = array(
744
      'connection' => $this->connection,
745
      'base_dn' => $base_dn,
746
      'filter' => $filter,
747
      'attributes' => $attributes,
748
      'attrsonly' => $attrsonly,
749
      'sizelimit' => $sizelimit,
750
      'timelimit' => $timelimit,
751
      'deref' => $deref,
752
      'query_display' => $query,
753
      'scope' => $scope,
754
    );
755

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

    
781

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

798
      (this array of parameters is primarily passed on to ldapQuery() method)
799
   *
800
   * @return array of ldap entries or FALSE on error.
801
   *
802
   */
803
  public function pagedLdapQuery($ldap_query_params) {
804

    
805
    if (!($this->searchPagination && $this->paginationEnabled)) {
806
      watchdog('ldap', "LDAP server pagedLdapQuery() called when functionality not available in php install or
807
        not enabled in ldap server configuration.  error. basedn: %basedn| filter: %filter| attributes:
808
         %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
809
      RETURN FALSE;
810
    }
811

    
812
    $paged_entries = array();
813
    $page_token = '';
814
    $page = 0;
815
    $estimated_entries = 0;
816
    $aggregated_entries = array();
817
    $aggregated_entries_count = 0;
818
    $has_page_results = FALSE;
819

    
820
    do {
821
      ldap_control_paged_result($this->connection, $this->searchPageSize, TRUE, $page_token);
822
      $result = $this->ldapQuery($ldap_query_params['scope'], $ldap_query_params);
823

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

    
858
      if (isset($ldap_query_params['sizelimit']) && $ldap_query_params['sizelimit'] && $aggregated_entries_count >= $ldap_query_params['sizelimit']) {
859
        $discarded_entries = array_splice($aggregated_entries, $ldap_query_params['sizelimit']);
860
        break;
861
      }
862
      elseif ($this->searchPageEnd !== NULL && $page >= $this->searchPageEnd) { // user defined pagination has run out
863
        break;
864
      }
865
      elseif ($page_token === NULL || $page_token == '') { // ldap reference pagination has run out
866
        break;
867
      }
868
      $page++;
869
    } while ($skipped_page || $has_page_results);
870

    
871
    $aggregated_entries['count'] = count($aggregated_entries);
872
    return $aggregated_entries;
873
  }
874

    
875
  /**
876
   * execute ldap query and return ldap records
877
   *
878
   * @param scope
879
   * @params see pagedLdapQuery $params
880
   *
881
   * @return array of ldap entries
882
   */
883
  function ldapQuery($scope, $params) {
884

    
885
    $this->connectAndBindIfNotAlready();
886

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

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

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

    
930
  /**
931
   * @param array $dns Mixed Case
932
   * @return array $dns Lower Case
933
   */
934

    
935
  public function dnArrayToLowerCase($dns) {
936
    return array_keys(array_change_key_case(array_flip($dns), CASE_LOWER));
937
  }
938

    
939
  /**
940
   * @param binary or string $puid as returned from ldap_read or other ldap function
941
   *
942
   */
943
  public function userUserEntityFromPuid($puid) {
944

    
945
    $query = new EntityFieldQuery();
946
    $query->entityCondition('entity_type', 'user')
947
    ->fieldCondition('ldap_user_puid_sid', 'value', $this->sid, '=')
948
    ->fieldCondition('ldap_user_puid', 'value', $puid, '=')
949
    ->fieldCondition('ldap_user_puid_property', 'value', $this->unique_persistent_attr, '=')
950
    ->addMetaData('account', user_load(1)); // run the query as user 1
951

    
952
    $result = $query->execute();
953

    
954
    if (isset($result['user'])) {
955
      $uids = array_keys($result['user']);
956
      if (count($uids) == 1) {
957
        $user = entity_load('user', array_keys($result['user']));
958
        return $user[$uids[0]];
959
      }
960
      else {
961
        $uids = join(',', $uids);
962
        $tokens = array('%uids' => $uids, '%puid' => $puid, '%sid' =>  $this->sid, '%ldap_user_puid_property' =>  $this->unique_persistent_attr);
963
        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);
964
        return FALSE;
965
      }
966
    }
967
    else {
968
      return FALSE;
969
    }
970

    
971
  }
972

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

    
993
    // Let other modules alter the ldap name
994
    $context = array(
995
      'ldap_server' => $this,
996
    );
997
    drupal_alter('ldap_servers_username_to_ldapname', $ldap_username, $drupal_username, $context);
998

    
999
    return $ldap_username;
1000

    
1001
  }
1002

    
1003

    
1004
 /**
1005
   * @param ldap entry array $ldap_entry
1006
   *
1007
   * @return string user's username value
1008
   */
1009
  public function userUsernameFromLdapEntry($ldap_entry) {
1010

    
1011

    
1012
    if ($this->account_name_attr) {
1013
      $accountname = (empty($ldap_entry[$this->account_name_attr][0])) ? FALSE : $ldap_entry[$this->account_name_attr][0];
1014
    }
1015
    elseif ($this->user_attr)  {
1016
      $accountname = (empty($ldap_entry[$this->user_attr][0])) ? FALSE : $ldap_entry[$this->user_attr][0];
1017
    }
1018
    else {
1019
      $accountname = FALSE;
1020
    }
1021

    
1022
    return $accountname;
1023
  }
1024

    
1025
 /**
1026
   * @param string $dn ldap dn
1027
   *
1028
   * @return mixed string user's username value of FALSE
1029
   */
1030
  public function userUsernameFromDn($dn) {
1031

    
1032
    $ldap_entry = @$this->dnExists($dn, 'ldap_entry', array());
1033
    if (!$ldap_entry || !is_array($ldap_entry)) {
1034
      return FALSE;
1035
    }
1036
    else {
1037
      return $this->userUsernameFromLdapEntry($ldap_entry);
1038
    }
1039

    
1040
  }
1041

    
1042
  /**
1043
   * @param ldap entry array $ldap_entry
1044
   *
1045
   * @return string user's mail value or FALSE if none present
1046
   */
1047
  public function userEmailFromLdapEntry($ldap_entry) {
1048

    
1049
    if ($ldap_entry && $this->mail_attr) { // not using template
1050
      $mail = isset($ldap_entry[$this->mail_attr][0]) ? $ldap_entry[$this->mail_attr][0] : FALSE;
1051
      return $mail;
1052
    }
1053
    elseif ($ldap_entry && $this->mail_template) {  // template is of form [cn]@illinois.edu
1054
      ldap_servers_module_load_include('inc', 'ldap_servers', 'ldap_servers.functions');
1055
      return ldap_servers_token_replace($ldap_entry, $this->mail_template, 'ldap_entry');
1056
    }
1057
    else {
1058
      return FALSE;
1059
    }
1060
  }
1061

    
1062
        /**
1063
         * @param ldap entry array $ldap_entry
1064
         *
1065
         * @return drupal file object image user's thumbnail or FALSE if none present or ERROR happens.
1066
         */
1067
        public function userPictureFromLdapEntry($ldap_entry, $drupal_username = FALSE) {
1068
                if ($ldap_entry && $this->picture_attr) {
1069
                        //Check if ldap entry has been provisioned.
1070

    
1071
                        $thumb = isset($ldap_entry[$this->picture_attr][0]) ? $ldap_entry[$this->picture_attr][0] : FALSE;
1072
                        if(!$thumb){
1073
                                return FALSE;
1074
                        }
1075

    
1076
                        //Create md5 check.
1077
                        $md5thumb = md5($thumb);
1078

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

    
1137

    
1138
  /**
1139
   * @param ldap entry array $ldap_entry
1140
   *
1141
   * @return string user's PUID or permanent user id (within ldap), converted from binary, if applicable
1142
   */
1143
  public function userPuidFromLdapEntry($ldap_entry) {
1144

    
1145
    if ($this->unique_persistent_attr
1146
        && isset($ldap_entry[$this->unique_persistent_attr][0])
1147
        && is_scalar($ldap_entry[$this->unique_persistent_attr][0])
1148
        ) {
1149
      if (is_array($ldap_entry[$this->unique_persistent_attr])) {
1150
        $puid = $ldap_entry[$this->unique_persistent_attr][0];
1151
      }
1152
      else {
1153
        $puid = $ldap_entry[$this->unique_persistent_attr];
1154
      }
1155
      return ($this->unique_persistent_attr_binary) ? ldap_servers_binary($puid) : $puid;
1156
    }
1157
    else {
1158
      return FALSE;
1159
    }
1160
  }
1161

    
1162
   /**
1163
   *  @param mixed $user
1164
   *    - drupal user object (stdClass Object)
1165
   *    - ldap entry of user (array)
1166
   *    - ldap dn of user (string)
1167
   *    - drupal username of user (string)
1168
   *
1169
   *  @return array $ldap_user_entry (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1170
  */
1171
  public function user_lookup($user) {
1172
    return $this->userUserToExistingLdapEntry($user);
1173
  }
1174
  public function userUserToExistingLdapEntry($user) {
1175

    
1176
    if (is_object($user)) {
1177
      $user_ldap_entry = $this->userUserNameToExistingLdapEntry($user->name);
1178
    }
1179
    elseif (is_array($user)) {
1180
      $user_ldap_entry = $user;
1181
    }
1182
    elseif (is_scalar($user)) {
1183
      if (strpos($user, '=') === FALSE) { // username
1184
        $user_ldap_entry = $this->userUserNameToExistingLdapEntry($user);
1185
      }
1186
      else {
1187
        $user_ldap_entry = $this->dnExists($user, 'ldap_entry');
1188
      }
1189
    }
1190
    return $user_ldap_entry;
1191
  }
1192

    
1193
  /**
1194
   * Queries LDAP server for the user.
1195
   *
1196
   * @param string $drupal_user_name
1197
   *
1198
   * @param string or int $prov_event
1199
   *   This could be anything, particularly when used by other modules.  Other modules should use string like 'mymodule_myevent'
1200
   *   LDAP_USER_EVENT_ALL signifies get all attributes needed by all other contexts/ops
1201
   *
1202
   * @return associative array representing ldap data of a user.  for example of returned value.
1203
   *   'sid' => ldap server id
1204
   *   'mail' => derived from ldap mail (not always populated).
1205
   *   'dn'   => dn of user
1206
   *   'attr' => single ldap entry array in form returned from ldap_search() extension, e.g.
1207
   *   'dn' => dn of entry
1208
   */
1209
  function userUserNameToExistingLdapEntry($drupal_user_name, $ldap_context = NULL) {
1210

    
1211
    $watchdog_tokens = array('%drupal_user_name' => $drupal_user_name);
1212
    $ldap_username = $this->userUsernameToLdapNameTransform($drupal_user_name, $watchdog_tokens);
1213
    if (!$ldap_username) {
1214
      return FALSE;
1215
    }
1216
    if (!$ldap_context) {
1217
      $attributes = array();
1218
    }
1219
    else {
1220
      $attribute_maps = ldap_servers_attributes_needed($this->sid, $ldap_context);
1221
      $attributes = array_keys($attribute_maps);
1222
    }
1223

    
1224
    foreach ($this->basedn as $basedn) {
1225
      if (empty($basedn)) continue;
1226
      $filter = '(' . $this->user_attr . '=' . ldap_server_massage_text($ldap_username, 'attr_value', LDAP_SERVER_MASSAGE_QUERY_LDAP) . ')';
1227
      $result = $this->search($basedn, $filter, $attributes);
1228
      if (!$result || !isset($result['count']) || !$result['count']) continue;
1229

    
1230
      // Must find exactly one user for authentication to work.
1231

    
1232
      if ($result['count'] != 1) {
1233
        $count = $result['count'];
1234
        watchdog('ldap_servers', "Error: !count users found with $filter under $basedn.", array('!count' => $count), WATCHDOG_ERROR);
1235
        continue;
1236
      }
1237
      $match = $result[0];
1238
      // These lines serve to fix the attribute name in case a
1239
      // naughty server (i.e.: MS Active Directory) is messing the
1240
      // characters' case.
1241
      // This was contributed by Dan "Gribnif" Wilga, and described
1242
      // here: http://drupal.org/node/87833
1243
      $name_attr = $this->user_attr;
1244

    
1245
      if (isset($match[$name_attr][0])) {
1246
        // leave name
1247
      }
1248
      elseif (isset($match[drupal_strtolower($name_attr)][0])) {
1249
        $name_attr = drupal_strtolower($name_attr);
1250

    
1251
      }
1252
      else {
1253
        if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_ANON_USER) {
1254
          $result = array(
1255
            'dn' =>  $match['dn'],
1256
            'mail' => $this->userEmailFromLdapEntry($match),
1257
            'attr' => $match,
1258
            'sid' => $this->sid,
1259
            );
1260
          return $result;
1261
        }
1262
        else {
1263
          continue;
1264
        }
1265
      }
1266

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

    
1289
  /**
1290
   * Is a user a member of group?
1291
   *
1292
   * @param string $group_dn MIXED CASE
1293
   * @param mixed $user
1294
   *    - drupal user object (stdClass Object)
1295
   *    - ldap entry of user (array)
1296
   *    - ldap dn of user (array)
1297
   *    - drupal user name (string)
1298
   * @param enum $nested = NULL (default to server configuration), TRUE, or FALSE indicating to test for nested groups
1299
   */
1300
  public function groupIsMember($group_dn, $user, $nested = NULL) {
1301

    
1302
    $nested = ($nested === TRUE || $nested === FALSE) ? $nested : $this->groupNested;
1303
    $group_dns = $this->groupMembershipsFromUser($user, 'group_dns', $nested);
1304
    // 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
1305
    // so make sure in_array() is case insensitive
1306
    return (is_array($group_dns) && in_array(drupal_strtolower($group_dn), $this->dnArrayToLowerCase($group_dns)));
1307
  }
1308

    
1309

    
1310

    
1311
  /**
1312
   * NOT TESTED
1313
   * add a group entry
1314
   *
1315
   * @param string $group_dn as ldap dn
1316
   * @param array $attributes in key value form
1317
   *    $attributes = array(
1318
   *      "attribute1" = "value",
1319
   *      "attribute2" = array("value1", "value2"),
1320
   *      )
1321
   * @return boolean success
1322
   */
1323
  public function groupAddGroup($group_dn, $attributes = array()) {
1324

    
1325
    if ($this->dnExists($group_dn, 'boolean')) {
1326
      return FALSE;
1327
    }
1328

    
1329
    $attributes = array_change_key_case($attributes, CASE_LOWER);
1330
    $objectclass = (empty($attributes['objectclass'])) ? $this->groupObjectClass : $attributes['objectclass'];
1331
    $attributes['objectclass'] = $objectclass;
1332

    
1333
    /**
1334
     * 2. give other modules a chance to add or alter attributes
1335
     */
1336
    $context = array(
1337
      'action' => 'add',
1338
      'corresponding_drupal_data' => array($group_dn => $attributes),
1339
      'corresponding_drupal_data_type' => 'group',
1340
    );
1341
    $ldap_entries = array($group_dn => $attributes);
1342
    drupal_alter('ldap_entry_pre_provision', $ldap_entries, $this, $context);
1343
    $attributes = $ldap_entries[$group_dn];
1344

    
1345

    
1346
     /**
1347
     * 4. provision ldap entry
1348
     *   @todo how is error handling done here?
1349
     */
1350
    $ldap_entry_created = $this->createLdapEntry($attributes, $group_dn);
1351

    
1352

    
1353
     /**
1354
     * 5. allow other modules to react to provisioned ldap entry
1355
     *   @todo how is error handling done here?
1356
     */
1357
    if ($ldap_entry_created) {
1358
      module_invoke_all('ldap_entry_post_provision', $ldap_entries, $this, $context);
1359
      return TRUE;
1360
    }
1361
    else {
1362
      return FALSE;
1363
    }
1364

    
1365
  }
1366

    
1367
  /**
1368
   * NOT TESTED
1369
   * remove a group entry
1370
   *
1371
   * @param string $group_dn as ldap dn
1372
   * @param boolean $only_if_group_empty
1373
   *   TRUE = group should not be removed if not empty
1374
   *   FALSE = groups should be deleted regardless of members
1375
   */
1376
  public function groupRemoveGroup($group_dn, $only_if_group_empty = TRUE) {
1377

    
1378
    if ($only_if_group_empty) {
1379
      $members = $this->groupAllMembers($group_dn);
1380
      if (is_array($members) && count($members) > 0) {
1381
        return FALSE;
1382
      }
1383
    }
1384

    
1385
    return $this->delete($group_dn);
1386

    
1387
  }
1388

    
1389
  /**
1390
   * NOT TESTED
1391
   * add a member to a group
1392
   *
1393
   * @param string $ldap_user_dn as ldap dn
1394
   * @param mixed $user
1395
   *    - drupal user object (stdClass Object)
1396
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1397
   *    - ldap dn of user (array)
1398
   *    - drupal username of user (string)
1399
   */
1400
  public function groupAddMember($group_dn, $user) {
1401

    
1402
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1403
    $result = FALSE;
1404
    if ($user_ldap_entry && $this->groupGroupEntryMembershipsConfigured) {
1405
      $add = array();
1406
      $add[$this->groupMembershipsAttr] = $user_ldap_entry['dn'];
1407
      $this->connectAndBindIfNotAlready();
1408
      $result = @ldap_mod_add($this->connection, $group_dn, $add);
1409
    }
1410

    
1411
    return $result;
1412
  }
1413

    
1414
  /**
1415
   * NOT TESTED
1416
   * remove a member from a group
1417
   *
1418
   * @param string $group_dn as ldap dn
1419
   * @param mixed $user
1420
   *    - drupal user object (stdClass Object)
1421
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1422
   *    - ldap dn of user (array)
1423
   *    - drupal username of user (string)
1424
   */
1425
  public function groupRemoveMember($group_dn, $user) {
1426

    
1427
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1428
    $result = FALSE;
1429
    if ($user_ldap_entry && $this->groupGroupEntryMembershipsConfigured) {
1430
      $del = array();
1431
      $del[$this->groupMembershipsAttr] = $user_ldap_entry['dn'];
1432
      $this->connectAndBindIfNotAlready();
1433
      $result = @ldap_mod_del($this->connection, $group_dn, $del);
1434
    }
1435
    return $result;
1436
  }
1437

    
1438

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

    
1472
    $this->groupMembersResursive($current_group_entries, $all_group_dns, $tested_group_ids, 0, $max_levels, $object_classes);
1473

    
1474
    return $all_group_dns;
1475

    
1476
  }
1477

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

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

    
1494
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_member_entries) || count($current_member_entries) == 0) {
1495
      return FALSE;
1496
    }
1497
    if (isset($current_member_entries['count'])) {
1498
      unset($current_member_entries['count']);
1499
    };
1500

    
1501
    foreach ($current_member_entries as $i => $member_entry) {
1502
      // 1.  Add entry itself if of the correct type to $all_member_dns
1503
      $objectClassMatch = (!$object_classes || (count(array_intersect(array_values($member_entry['objectclass']), $object_classes)) > 0));
1504
      $objectIsGroup = in_array($this->groupObjectClass, array_values($member_entry['objectclass']));
1505
      if ($objectClassMatch && !in_array($member_entry['dn'], $all_member_dns)) { // add member
1506
        $all_member_dns[] = $member_entry['dn'];
1507
      }
1508

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

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

    
1550

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

    
1573
  public function groupMembershipsFromUser($user, $return = 'group_dns', $nested = NULL) {
1574

    
1575
    $group_dns = FALSE;
1576
    $user_ldap_entry = @$this->userUserToExistingLdapEntry($user);
1577
    if (!$user_ldap_entry || $this->groupFunctionalityUnused) {
1578
      return FALSE;
1579
    }
1580
    if ($nested === NULL) {
1581
      $nested = $this->groupNested;
1582
    }
1583

    
1584
    if ($this->groupUserMembershipsConfigured) { // preferred method
1585
      $group_dns = $this->groupUserMembershipsFromUserAttr($user_ldap_entry, $nested);
1586
    }
1587
    elseif ($this->groupGroupEntryMembershipsConfigured) {
1588
      $group_dns = $this->groupUserMembershipsFromEntry($user_ldap_entry, $nested);
1589
    }
1590
    else {
1591
      watchdog('ldap_servers', 'groupMembershipsFromUser: Group memberships for server have not been configured.', array(), WATCHDOG_WARNING);
1592
      return FALSE;
1593
    }
1594
    if ($return == 'group_dns') {
1595
      return $group_dns;
1596
    }
1597

    
1598
  }
1599

    
1600

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

    
1622
  public function groupUserMembershipsFromUserAttr($user, $nested = NULL) {
1623

    
1624
    if (!$this->groupUserMembershipsConfigured) {
1625
      return FALSE;
1626
    }
1627
    if ($nested === NULL) {
1628
      $nested = $this->groupNested;
1629
    }
1630

    
1631
    $not_user_ldap_entry = empty($user['attr'][$this->groupUserMembershipsAttr]);
1632
    if ($not_user_ldap_entry) { // if drupal user passed in, try to get user_ldap_entry
1633
      $user = $this->userUserToExistingLdapEntry($user);
1634
      $not_user_ldap_entry = empty($user['attr'][$this->groupUserMembershipsAttr]);
1635
      if ($not_user_ldap_entry) {
1636
        return FALSE; // user's membership attribute is not present.  either misconfigured or query failed
1637
      }
1638
    }
1639
    // if not exited yet, $user must be user_ldap_entry.
1640
    $user_ldap_entry = $user;
1641
    $all_group_dns = array();
1642
    $tested_group_ids = array();
1643
    $level = 0;
1644

    
1645
    $member_group_dns = $user_ldap_entry['attr'][$this->groupUserMembershipsAttr];
1646
    if (isset($member_group_dns['count'])) {
1647
      unset($member_group_dns['count']);
1648
    }
1649
    $ors = array();
1650
    foreach ($member_group_dns as $i => $member_group_dn) {
1651
      $all_group_dns[] = $member_group_dn;
1652
      if ($nested) {
1653
        if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1654
          $member_value = $member_group_dn;
1655
        }
1656
        else {
1657
          $member_value = ldap_servers_get_first_rdn_value_from_dn($member_group_dn, $this->groupMembershipsAttrMatchingUserAttr);
1658
        }
1659
        $ors[] =  $this->groupMembershipsAttr . '=' . ldap_pear_escape_filter_value($member_value);
1660
      }
1661
    }
1662

    
1663
    if ($nested && count($ors)) {
1664
      $count = count($ors);
1665
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1666
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1667
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1668
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1669

    
1670
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1671
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1672
          if ($group_entries !== FALSE  && $level < LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT) {
1673
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT);
1674
          }
1675
        }
1676
      }
1677
    }
1678

    
1679
    return $all_group_dns;
1680
  }
1681

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

    
1705
    if (!$this->groupGroupEntryMembershipsConfigured) {
1706
      return FALSE;
1707
    }
1708
    if ($nested === NULL) {
1709
      $nested = $this->groupNested;
1710
    }
1711

    
1712
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1713

    
1714
    $all_group_dns = array(); // MIXED CASE VALUES
1715
    $tested_group_ids = array(); // array of dns already tested to avoid excess queries MIXED CASE VALUES
1716
    $level = 0;
1717

    
1718
    if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1719
      $member_value = $user_ldap_entry['dn'];
1720
    }
1721
    else {
1722
      $member_value = $user_ldap_entry['attr'][$this->groupMembershipsAttrMatchingUserAttr][0];
1723
    }
1724
    $member_value = ldap_pear_escape_filter_value($member_value);
1725
    if ($this->groupObjectClass == '') {
1726
      $group_query = '(' . $this->groupMembershipsAttr . "=$member_value)";
1727
    }
1728
    else {
1729
      $group_query = '(&(objectClass=' . $this->groupObjectClass . ')(' . $this->groupMembershipsAttr . "=$member_value))";
1730
    }
1731

    
1732
    foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1733
      $group_entries = $this->search($base_dn, $group_query, array()); // only need dn, so empty array forces return of no attributes
1734
      if ($group_entries !== FALSE) {
1735
        $max_levels = ($nested) ? LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT : 0;
1736
        $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level, $max_levels);
1737
      }
1738
    }
1739

    
1740
    return $all_group_dns;
1741
  }
1742

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

    
1762
  public function groupMembershipsFromEntryRecursive($current_group_entries, &$all_group_dns, &$tested_group_ids, $level, $max_levels) {
1763

    
1764
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_group_entries) || count($current_group_entries) == 0) {
1765
      return FALSE;
1766
    }
1767
    if (isset($current_group_entries['count'])) {
1768
      unset($current_group_entries['count']);
1769
    };
1770

    
1771
    $ors = array();
1772
    foreach ($current_group_entries as $i => $group_entry) {
1773
      if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1774
        $member_id = $group_entry['dn'];
1775
      }
1776
      else {// maybe cn, uid, etc is held
1777
        $member_id = ldap_servers_get_first_rdn_value_from_dn($group_entry['dn'], $this->groupMembershipsAttrMatchingUserAttr);
1778
        if(!$member_id) {
1779
          if ($this->detailed_watchdog_log) {
1780
             watchdog('ldap_server', 'group_entry: %ge', array('%ge'=>pretty_print_ldap_entry($group_entry)));
1781
          }
1782
          // group not identified by simple checks yet!
1783

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

    
1802
      if ($member_id && !in_array($member_id, $tested_group_ids)) {
1803
        $tested_group_ids[] = $member_id;
1804
        $all_group_dns[] = $group_entry['dn'];
1805
        // add $group_id (dn, cn, uid) to query
1806
        $ors[] =  $this->groupMembershipsAttr . '=' .  ldap_pear_escape_filter_value($member_id);
1807
      }
1808
    }
1809

    
1810
    if ($level < $max_levels && count($ors)) {
1811
      $count = count($ors);
1812
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1813
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1814
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1815
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1816

    
1817
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1818
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1819
          if ($group_entries !== FALSE) {
1820
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, $max_levels);
1821
          }
1822
        }
1823
      }
1824
    }
1825

    
1826
    return TRUE;
1827
  }
1828

    
1829

    
1830
 /**
1831
   *  get "groups" from derived from DN.  Has limited usefulness
1832
   *
1833
   *  @param mixed
1834
   *    - drupal user object (stdClass Object)
1835
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1836
   *    - ldap dn of user (array)
1837
   *    - drupal username of user (string)
1838
   *
1839
   *  @return array of group strings
1840
   */
1841
  public function groupUserMembershipsFromDn($user) {
1842

    
1843
    if (!$this->groupDeriveFromDn || !$this->groupDeriveFromDnAttr) {
1844
      return FALSE;
1845
    }
1846
    elseif ($user_ldap_entry = $this->userUserToExistingLdapEntry($user)) {
1847
      return ldap_servers_get_all_rdn_values_from_dn($user_ldap_entry['dn'], $this->groupDeriveFromDnAttr);
1848
    }
1849
    else {
1850
      return FALSE;
1851
    }
1852

    
1853
  }
1854
  /**
1855
   * Error methods and properties.
1856
   */
1857

    
1858
  public $detailedWatchdogLog = FALSE;
1859
  protected $_errorMsg = NULL;
1860
  protected $_hasError = FALSE;
1861
  protected $_errorName = NULL;
1862

    
1863
  public function setError($_errorName, $_errorMsgText = NULL) {
1864
    $this->_errorMsgText = $_errorMsgText;
1865
    $this->_errorName = $_errorName;
1866
    $this->_hasError = TRUE;
1867
  }
1868

    
1869
  public function clearError() {
1870
    $this->_hasError = FALSE;
1871
    $this->_errorMsg = NULL;
1872
    $this->_errorName = NULL;
1873
  }
1874

    
1875
  public function hasError() {
1876
    return ($this->_hasError || $this->ldapErrorNumber());
1877
  }
1878

    
1879
  public function errorMsg($type = NULL) {
1880
    if ($type == 'ldap' && $this->connection) {
1881
      return ldap_err2str(ldap_errno($this->connection));
1882
    }
1883
    elseif ($type == NULL) {
1884
      return $this->_errorMsg;
1885
    }
1886
    else {
1887
      return NULL;
1888
    }
1889
  }
1890

    
1891
  public function errorName($type = NULL) {
1892
    if ($type == 'ldap' && $this->connection) {
1893
      return "LDAP Error: " . ldap_error($this->connection);
1894
    }
1895
    elseif ($type == NULL) {
1896
      return $this->_errorName;
1897
    }
1898
    else {
1899
      return NULL;
1900
    }
1901
  }
1902

    
1903
  public function ldapErrorNumber() {
1904
    if ($this->connection && ldap_errno($this->connection)) {
1905
      return ldap_errno($this->connection);
1906
    }
1907
    else {
1908
      return FALSE;
1909
    }
1910
  }
1911

    
1912
}
1913

    
1914
/**
1915
 * Class for enabling rebind functionality for following referrrals.
1916
 */
1917
class LdapServersRebindHandler {
1918

    
1919
  private $bind_dn = 'Anonymous';
1920
  private $bind_passwd = '';
1921

    
1922
  public function __construct($bind_user_dn, $bind_user_passwd){
1923
    $this->bind_dn = $bind_user_dn;
1924
    $this->bind_passwd = $bind_user_passwd;
1925
  }
1926

    
1927
  public function rebind_callback($ldap, $referral){
1928
    // ldap options
1929
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
1930
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 1);
1931
    ldap_set_rebind_proc($ldap, array($this, 'rebind_callback'));
1932

    
1933
  // Bind to new host, assumes initial bind dn has access to the referred servers.
1934
    if (!ldap_bind($ldap, $this->bind_dn, $this->bind_passwd)) {
1935
      echo "Could not bind to referral server: $referral";
1936
      return 1;
1937
    }
1938
    return 0;
1939
  }
1940
}