Projet

Général

Profil

Paste
Télécharger (68,2 ko) Statistiques
| Branche: | Révision:

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

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
    if (!empty($attributes['unicodePwd']) && ($this->ldap_type == 'ad')) {
467
      $attributes['unicodePwd'] = ldap_servers_convert_password_for_active_directory_unicodePwd($attributes['unicodePwd']);
468
    }
469

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

    
478
    return $result;
479
  }
480

    
481

    
482

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

    
492
  static public function removeUnchangedAttributes($new_entry, $old_entry) {
493

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

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

    
521

    
522

    
523

    
524

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

535
    @return TRUE on success FALSE on error
536
   */
537

    
538
  function modifyLdapEntry($dn, $attributes = array(), $old_attributes = FALSE) {
539

    
540
    $this->connectAndBindIfNotAlready();
541

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

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

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

    
561
    $attributes = $this->removeUnchangedAttributes($attributes, $old_attributes);
562

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

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

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

    
608
    return TRUE;
609

    
610
  }
611

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

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

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

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

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

    
681
  }
682

    
683

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

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

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

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

    
713

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

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

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

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

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

    
783

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

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

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

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

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

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

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

    
873
    $aggregated_entries['count'] = count($aggregated_entries);
874
    return $aggregated_entries;
875
  }
876

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

    
887
    $this->connectAndBindIfNotAlready();
888

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

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

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

    
932
  /**
933
   * @param array $dns Mixed Case
934
   * @return array $dns Lower Case
935
   */
936

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

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

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

    
954
    $result = $query->execute();
955

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

    
973
  }
974

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

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

    
1001
    return $ldap_username;
1002

    
1003
  }
1004

    
1005

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

    
1013

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

    
1024
    return $accountname;
1025
  }
1026

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

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

    
1042
  }
1043

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

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

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

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

    
1078
                        //Create md5 check.
1079
                        $md5thumb = md5($thumb);
1080

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

    
1139

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

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

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

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

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

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

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

    
1232
      // Must find exactly one user for authentication to work.
1233

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

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

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

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

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

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

    
1311

    
1312

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

    
1327
    //debug("this->dnExists(   $group_dn, boolean)"); debug($this->dnExists($group_dn, 'boolean'));
1328
   // debug("this->dnExists(   $group_dn, boolean)"); debug($this->dnExists($group_dn));
1329
    if ($this->dnExists($group_dn, 'boolean')) {
1330
      return FALSE;
1331
    }
1332

    
1333
    $attributes = array_change_key_case($attributes, CASE_LOWER);
1334
    $objectclass = (empty($attributes['objectclass'])) ? $this->groupObjectClass : $attributes['objectclass'];
1335
    $attributes['objectclass'] = $objectclass;
1336

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

    
1349

    
1350
     /**
1351
     * 4. provision ldap entry
1352
     *   @todo how is error handling done here?
1353
     */
1354
    $ldap_entry_created = $this->createLdapEntry($attributes, $group_dn);
1355

    
1356

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

    
1369
  }
1370

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

    
1382
    if ($only_if_group_empty) {
1383
      $members = $this->groupAllMembers($group_dn);
1384
      if (is_array($members) && count($members) > 0) {
1385
        return FALSE;
1386
      }
1387
    }
1388

    
1389
    return $this->delete($group_dn);
1390

    
1391
  }
1392

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

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

    
1415
    return $result;
1416
  }
1417

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

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

    
1442

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

    
1477
    $this->groupMembersResursive($current_group_entries, $all_group_dns, $tested_group_ids, 0, $max_levels, $object_classes);
1478

    
1479
    return $all_group_dns;
1480

    
1481
  }
1482

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

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

    
1499
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_member_entries) || count($current_member_entries) == 0) {
1500
      return FALSE;
1501
    }
1502
    if (isset($current_member_entries['count'])) {
1503
      unset($current_member_entries['count']);
1504
    };
1505

    
1506
    foreach ($current_member_entries as $i => $member_entry) {
1507
      //dpm("groupMembersResursive:member_entry $i, level=$level < max_levels=$max_levels"); dpm($member_entry);
1508
      // 1.  Add entry itself if of the correct type to $all_member_dns
1509
      $objectClassMatch = (!$object_classes || (count(array_intersect(array_values($member_entry['objectclass']), $object_classes)) > 0));
1510
      $objectIsGroup = in_array($this->groupObjectClass, array_values($member_entry['objectclass']));
1511
      if ($objectClassMatch && !in_array($member_entry['dn'], $all_member_dns)) { // add member
1512
        $all_member_dns[] = $member_entry['dn'];
1513
      }
1514

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

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

    
1556

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

    
1579
  public function groupMembershipsFromUser($user, $return = 'group_dns', $nested = NULL) {
1580

    
1581
    $group_dns = FALSE;
1582
    $user_ldap_entry = @$this->userUserToExistingLdapEntry($user);
1583
    if (!$user_ldap_entry || $this->groupFunctionalityUnused) {
1584
      return FALSE;
1585
    }
1586
    if ($nested === NULL) {
1587
      $nested = $this->groupNested;
1588
    }
1589

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

    
1604
  }
1605

    
1606

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

    
1628
  public function groupUserMembershipsFromUserAttr($user, $nested = NULL) {
1629

    
1630
    if (!$this->groupUserMembershipsConfigured) {
1631
      return FALSE;
1632
    }
1633
    if ($nested === NULL) {
1634
      $nested = $this->groupNested;
1635
    }
1636

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

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

    
1669
    if ($nested && count($ors)) {
1670
      $count = count($ors);
1671
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1672
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1673
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1674
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1675

    
1676
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1677
          // debug("query for parent groups, base_dn=$base_dn, $query_for_parent_groups");
1678
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1679
          if ($group_entries !== FALSE  && $level < LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT) {
1680
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT);
1681
          }
1682
        }
1683
      }
1684
    }
1685

    
1686
    return $all_group_dns;
1687
  }
1688

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

    
1712
    if (!$this->groupGroupEntryMembershipsConfigured) {
1713
      return FALSE;
1714
    }
1715
    if ($nested === NULL) {
1716
      $nested = $this->groupNested;
1717
    }
1718

    
1719
    $user_ldap_entry = $this->userUserToExistingLdapEntry($user);
1720

    
1721
    $all_group_dns = array(); // MIXED CASE VALUES
1722
    $tested_group_ids = array(); // array of dns already tested to avoid excess queries MIXED CASE VALUES
1723
    $level = 0;
1724

    
1725
    if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1726
      $member_value = $user_ldap_entry['dn'];
1727
    }
1728
    else {
1729
      $member_value = $user_ldap_entry['attr'][$this->groupMembershipsAttrMatchingUserAttr][0];
1730
    }
1731
    $member_value = ldap_pear_escape_filter_value($member_value);
1732
    if ($this->groupObjectClass == '') {
1733
      $group_query = '(' . $this->groupMembershipsAttr . "=$member_value)";
1734
    }
1735
    else {
1736
      $group_query = '(&(objectClass=' . $this->groupObjectClass . ')(' . $this->groupMembershipsAttr . "=$member_value))";
1737
    }
1738

    
1739
    foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1740
      $group_entries = $this->search($base_dn, $group_query, array()); // only need dn, so empty array forces return of no attributes
1741
      if ($group_entries !== FALSE) {
1742
        $max_levels = ($nested) ? LDAP_SERVER_LDAP_QUERY_RECURSION_LIMIT : 0;
1743
        $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level, $max_levels);
1744
      }
1745
    }
1746

    
1747
    return $all_group_dns;
1748
  }
1749

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

    
1769
  public function groupMembershipsFromEntryRecursive($current_group_entries, &$all_group_dns, &$tested_group_ids, $level, $max_levels) {
1770

    
1771
    if (!$this->groupGroupEntryMembershipsConfigured || !is_array($current_group_entries) || count($current_group_entries) == 0) {
1772
      return FALSE;
1773
    }
1774
    if (isset($current_group_entries['count'])) {
1775
      unset($current_group_entries['count']);
1776
    };
1777

    
1778
    $ors = array();
1779
    foreach ($current_group_entries as $i => $group_entry) {
1780
      if ($this->groupMembershipsAttrMatchingUserAttr == 'dn') {
1781
        $member_id = $group_entry['dn'];
1782
      }
1783
      else {// maybe cn, uid, etc is held
1784
        $member_id = ldap_servers_get_first_rdn_value_from_dn($group_entry['dn'], $this->groupMembershipsAttrMatchingUserAttr);
1785
        if(!$member_id) {
1786
          if ($this->detailed_watchdog_log) {
1787
             watchdog('ldap_server', 'group_entry: %ge', array('%ge'=>pretty_print_ldap_entry($group_entry)));
1788
          }
1789
          // group not identified by simple checks yet!
1790

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

    
1809
      if ($member_id && !in_array($member_id, $tested_group_ids)) {
1810
        $tested_group_ids[] = $member_id;
1811
        $all_group_dns[] = $group_entry['dn'];
1812
        // add $group_id (dn, cn, uid) to query
1813
        $ors[] =  $this->groupMembershipsAttr . '=' .  ldap_pear_escape_filter_value($member_id);
1814
      }
1815
    }
1816

    
1817
    if ($level < $max_levels && count($ors)) {
1818
      $count = count($ors);
1819
      for ($i=0; $i < $count; $i=$i+LDAP_SERVER_LDAP_QUERY_CHUNK) { // only 50 or so per query
1820
        $current_ors = array_slice($ors, $i, LDAP_SERVER_LDAP_QUERY_CHUNK);
1821
        $or = '(|(' . join(")(", $current_ors) . '))';  // e.g. (|(cn=group1)(cn=group2)) or   (|(dn=cn=group1,ou=blah...)(dn=cn=group2,ou=blah...))
1822
        $query_for_parent_groups = '(&(objectClass=' . $this->groupObjectClass . ')' . $or . ')';
1823

    
1824
        foreach ($this->basedn as $base_dn) {  // need to search on all basedns one at a time
1825
          $group_entries = $this->search($base_dn, $query_for_parent_groups);  // no attributes, just dns needed
1826
          if ($group_entries !== FALSE) {
1827
            $this->groupMembershipsFromEntryRecursive($group_entries, $all_group_dns, $tested_group_ids, $level + 1, $max_levels);
1828
          }
1829
        }
1830
      }
1831
    }
1832

    
1833
    return TRUE;
1834
  }
1835

    
1836

    
1837
 /**
1838
   *  get "groups" from derived from DN.  Has limited usefulness
1839
   *
1840
   *  @param mixed
1841
   *    - drupal user object (stdClass Object)
1842
   *    - ldap entry of user (array) (with top level keys of 'dn', 'mail', 'sid' and 'attr' )
1843
   *    - ldap dn of user (array)
1844
   *    - drupal username of user (string)
1845
   *
1846
   *  @return array of group strings
1847
   */
1848
  public function groupUserMembershipsFromDn($user) {
1849

    
1850
    if (!$this->groupDeriveFromDn || !$this->groupDeriveFromDnAttr) {
1851
      return FALSE;
1852
    }
1853
    elseif ($user_ldap_entry = $this->userUserToExistingLdapEntry($user)) {
1854
      return ldap_servers_get_all_rdn_values_from_dn($user_ldap_entry['dn'], $this->groupDeriveFromDnAttr);
1855
    }
1856
    else {
1857
      return FALSE;
1858
    }
1859

    
1860
  }
1861
  /**
1862
   * Error methods and properties.
1863
   */
1864

    
1865
  public $detailedWatchdogLog = FALSE;
1866
  protected $_errorMsg = NULL;
1867
  protected $_hasError = FALSE;
1868
  protected $_errorName = NULL;
1869

    
1870
  public function setError($_errorName, $_errorMsgText = NULL) {
1871
    $this->_errorMsgText = $_errorMsgText;
1872
    $this->_errorName = $_errorName;
1873
    $this->_hasError = TRUE;
1874
  }
1875

    
1876
  public function clearError() {
1877
    $this->_hasError = FALSE;
1878
    $this->_errorMsg = NULL;
1879
    $this->_errorName = NULL;
1880
  }
1881

    
1882
  public function hasError() {
1883
    return ($this->_hasError || $this->ldapErrorNumber());
1884
  }
1885

    
1886
  public function errorMsg($type = NULL) {
1887
    if ($type == 'ldap' && $this->connection) {
1888
      return ldap_err2str(ldap_errno($this->connection));
1889
    }
1890
    elseif ($type == NULL) {
1891
      return $this->_errorMsg;
1892
    }
1893
    else {
1894
      return NULL;
1895
    }
1896
  }
1897

    
1898
  public function errorName($type = NULL) {
1899
    if ($type == 'ldap' && $this->connection) {
1900
      return "LDAP Error: " . ldap_error($this->connection);
1901
    }
1902
    elseif ($type == NULL) {
1903
      return $this->_errorName;
1904
    }
1905
    else {
1906
      return NULL;
1907
    }
1908
  }
1909

    
1910
  public function ldapErrorNumber() {
1911
    if ($this->connection && ldap_errno($this->connection)) {
1912
      return ldap_errno($this->connection);
1913
    }
1914
    else {
1915
      return FALSE;
1916
    }
1917
  }
1918

    
1919
}
1920

    
1921
/**
1922
 * Class for enabling rebind functionality for following referrrals.
1923
 */
1924
class LdapServersRebindHandler {
1925

    
1926
  private $bind_dn = 'Anonymous';
1927
  private $bind_passwd = '';
1928

    
1929
  public function __construct($bind_user_dn, $bind_user_passwd){
1930
    $this->bind_dn = $bind_user_dn;
1931
    $this->bind_passwd = $bind_user_passwd;
1932
  }
1933

    
1934
  public function rebind_callback($ldap, $referral){
1935
    // ldap options
1936
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
1937
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 1);
1938
    ldap_set_rebind_proc($ldap, array($this, 'rebind_callback'));
1939

    
1940
  // Bind to new host, assumes initial bind dn has access to the referred servers.
1941
    if (!ldap_bind($ldap, $this->bind_dn, $this->bind_passwd)) {
1942
      echo "Could not bind to referral server: $referral";
1943
      return 1;
1944
    }
1945
    return 0;
1946
  }
1947
}