mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
27.02.2015 0fd845d18cfaf23fc9f54e92775c735955e4e859
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2012-2015 ForgeRock AS
 */
package org.opends.server.backends.jeb;
 
import static com.sleepycat.je.LockMode.*;
import static com.sleepycat.je.OperationStatus.*;
 
import static org.opends.messages.BackendMessages.*;
import static org.opends.server.backends.jeb.JebFormat.*;
import static org.opends.server.util.ServerConstants.*;
 
import java.util.*;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.ByteSequenceReader;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ByteStringBuilder;
import org.forgerock.opendj.ldap.ConditionResult;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.util.Pair;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.SearchOperation;
import org.opends.server.types.*;
import org.opends.server.util.StaticUtils;
 
import com.sleepycat.je.*;
 
/**
 * This class represents the referral database which contains URIs from referral
 * entries.
 * <p>
 * The key is the DN of the referral entry and the value is that of a pair
 * (labeled URI in the ref attribute for that entry, DN). The DN must be
 * duplicated in the value because the key is suitable for comparisons but is
 * not reversible to a valid DN. Duplicate keys are permitted since a referral
 * entry can contain multiple values of the ref attribute. Key order is the same
 * as in the DN database so that all referrals in a subtree can be retrieved by
 * cursoring through a range of the records.
 */
public class DN2URI extends DatabaseContainer
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  private static final byte STRING_SEPARATOR = 0x00;
 
  /**
   * The key comparator used for the DN database.
   */
  private final Comparator<byte[]> dn2uriComparator;
 
 
  private final int prefixRDNComponents;
 
 
  /**
   * The standard attribute type that is used to specify the set of referral
   * URLs in a referral entry.
   */
  private final AttributeType referralType =
       DirectoryServer.getAttributeType(ATTR_REFERRAL_URL);
 
  /**
   * A flag that indicates whether there are any referrals contained in this
   * database.  It should only be set to {@code false} when it is known that
   * there are no referrals.
   */
  private volatile ConditionResult containsReferrals =
       ConditionResult.UNDEFINED;
 
 
  /**
   * Create a new object representing a referral database in a given
   * entryContainer.
   *
   * @param name The name of the referral database.
   * @param env The JE environment.
   * @param entryContainer The entryContainer of the DN database.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  @SuppressWarnings("unchecked")
  DN2URI(String name, Environment env,
        EntryContainer entryContainer)
      throws DatabaseException
  {
    super(name, env, entryContainer);
 
    dn2uriComparator = new AttributeIndex.KeyComparator();
    prefixRDNComponents = entryContainer.getBaseDN().size();
 
    this.dbConfig = JEBUtils.toDatabaseConfigAllowDuplicates(env);
    this.dbConfig.setBtreeComparator((Class<? extends Comparator<byte[]>>)
                                  dn2uriComparator.getClass());
  }
 
  /**
   * Insert a URI value in the referral database.
   *
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param dn The DN of the referral entry.
   * @param labeledURI The labeled URI value of the ref attribute.
   * @return true if the record was inserted, false if it was not.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  private boolean insert(Transaction txn, DN dn, String labeledURI)
       throws DatabaseException
  {
    byte[] normDN = JebFormat.dnToDNKey(dn, prefixRDNComponents);
    DatabaseEntry key = new DatabaseEntry(normDN);
    DatabaseEntry data = new DatabaseEntry(encodeURIAndDN(labeledURI, dn));
 
    // The JE insert method does not permit duplicate keys so we must use the
    // put method.
    if (put(txn, key, data) == SUCCESS)
    {
      containsReferrals = ConditionResult.TRUE;
      return true;
    }
    return false;
  }
 
  private byte[] encodeURIAndDN(String labeledURI, DN dn)
  {
    return new ByteStringBuilder()
      .append(labeledURI)
      .append(STRING_SEPARATOR)
      .append(dn.toString())
      .toByteArray();
  }
 
  private Pair<String, DN> decodeURIAndDN(byte[] data) throws DirectoryException {
    try {
      final ByteSequenceReader reader = ByteString.valueOf(data).asReader();
      final String labeledURI = reader.getString(getNextStringLength(reader));
      // skip the string separator
      reader.skip(1);
      final DN dn = DN.valueOf(reader.getString(reader.remaining()));
      return Pair.of(labeledURI, dn);
    }
    catch (Exception e) {
       throw new DirectoryException(ResultCode.OPERATIONS_ERROR, ERR_DATABASE_EXCEPTION.get(e));
    }
  }
 
  /** Returns the length of next string by looking for the zero byte used as separator. */
  private int getNextStringLength(ByteSequenceReader reader)
  {
    int length = 0;
    while (reader.peek(length) != STRING_SEPARATOR)
    {
      length++;
    }
    return length;
  }
 
  /**
   * Delete URI values for a given referral entry from the referral database.
   *
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param dn The DN of the referral entry for which URI values are to be
   * deleted.
   * @return true if the values were deleted, false if not.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  boolean delete(Transaction txn, DN dn) throws DatabaseException
  {
    byte[] normDN = JebFormat.dnToDNKey(dn, prefixRDNComponents);
    DatabaseEntry key = new DatabaseEntry(normDN);
 
    if (delete(txn, key) == SUCCESS)
    {
      containsReferrals = containsReferrals(txn);
      return true;
    }
    return false;
  }
 
  /**
   * Delete a single URI value from the referral database.
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param dn The DN of the referral entry.
   * @param labeledURI The URI value to be deleted.
   * @return true if the value was deleted, false if not.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  private boolean delete(Transaction txn, DN dn, String labeledURI) throws DatabaseException
  {
    CursorConfig cursorConfig = null;
    byte[] normDN = JebFormat.dnToDNKey(dn, prefixRDNComponents);
    byte[] URIBytes = StaticUtils.getBytes(labeledURI);
    DatabaseEntry key = new DatabaseEntry(normDN);
    DatabaseEntry data = new DatabaseEntry(URIBytes);
 
    Cursor cursor = openCursor(txn, cursorConfig);
    try
    {
      OperationStatus status = cursor.getSearchBoth(key, data, null);
      if (status == OperationStatus.SUCCESS)
      {
        status = cursor.delete();
      }
 
      if (status == OperationStatus.SUCCESS)
      {
        containsReferrals = containsReferrals(txn);
        return true;
      }
      return false;
    }
    finally
    {
      cursor.close();
    }
  }
 
  /**
   * Indicates whether the underlying database contains any referrals.
   *
   * @param  txn  The transaction to use when making the determination.
   *
   * @return  {@code true} if it is believed that the underlying database may
   *          contain at least one referral, or {@code false} if it is certain
   *          that it doesn't.
   */
  private ConditionResult containsReferrals(Transaction txn)
  {
    try
    {
      Cursor cursor = openCursor(txn, null);
      DatabaseEntry key  = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
 
      OperationStatus status = cursor.getFirst(key, data, null);
      cursor.close();
 
      if (status == OperationStatus.SUCCESS)
      {
        return ConditionResult.TRUE;
      }
      else if (status == OperationStatus.NOTFOUND)
      {
        return ConditionResult.FALSE;
      }
      else
      {
        return ConditionResult.UNDEFINED;
      }
    }
    catch (Exception e)
    {
      logger.traceException(e);
 
      return ConditionResult.UNDEFINED;
    }
  }
 
  /**
   * Update the referral database for an entry that has been modified.  Does
   * not do anything unless the entry before the modification or the entry after
   * the modification is a referral entry.
   *
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param before The entry before the modifications have been applied.
   * @param after The entry after the modifications have been applied.
   * @param mods The sequence of modifications made to the entry.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  void modifyEntry(Transaction txn, Entry before, Entry after, List<Modification> mods)
       throws DatabaseException
  {
    DN entryDN = before.getName();
    for (Modification mod : mods)
    {
      Attribute modAttr = mod.getAttribute();
      AttributeType modAttrType = modAttr.getAttributeType();
      if (modAttrType.equals(referralType))
      {
        Attribute a = mod.getAttribute();
        switch (mod.getModificationType().asEnum())
        {
          case ADD:
            if (a != null)
            {
              for (ByteString v : a)
              {
                insert(txn, entryDN, v.toString());
              }
            }
            break;
 
          case DELETE:
            if (a == null || a.isEmpty())
            {
              delete(txn, entryDN);
            }
            else
            {
              for (ByteString v : a)
              {
                delete(txn, entryDN, v.toString());
              }
            }
            break;
 
          case INCREMENT:
            // Nonsensical.
            break;
 
          case REPLACE:
            delete(txn, entryDN);
            if (a != null)
            {
              for (ByteString v : a)
              {
                insert(txn, entryDN, v.toString());
              }
            }
            break;
        }
      }
    }
  }
 
  /**
   * Update the referral database for an entry that has been replaced.  Does
   * not do anything unless the entry before it was replaced or the entry after
   * it was replaced is a referral entry.
   *
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param before The entry before it was replaced.
   * @param after The entry after it was replaced.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  public void replaceEntry(Transaction txn, Entry before, Entry after)
       throws DatabaseException
  {
    deleteEntry(txn, before);
    addEntry(txn, after);
  }
 
  /**
   * Update the referral database for a new entry. Does nothing if the entry
   * is not a referral entry.
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param entry The entry to be added.
   * @return True if the entry was added successfully or False otherwise.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  public boolean addEntry(Transaction txn, Entry entry)
       throws DatabaseException
  {
    boolean success = true;
    Set<String> labeledURIs = entry.getReferralURLs();
    if (labeledURIs != null)
    {
      DN dn = entry.getName();
      for (String labeledURI : labeledURIs)
      {
        if(!insert(txn, dn, labeledURI))
        {
          success = false;
        }
      }
    }
    return success;
  }
 
  /**
   * Update the referral database for a deleted entry. Does nothing if the entry
   * was not a referral entry.
   * @param txn A database transaction used for the update, or null if none is
   * required.
   * @param entry The entry to be deleted.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  void deleteEntry(Transaction txn, Entry entry) throws DatabaseException
  {
    Set<String> labeledURIs = entry.getReferralURLs();
    if (labeledURIs != null)
    {
      delete(txn, entry.getName());
    }
  }
 
  /**
   * Checks whether the target of an operation is a referral entry and throws
   * a Directory referral exception if it is.
   * @param entry The target entry of the operation, or the base entry of a
   * search operation.
   * @param searchScope The scope of the search operation, or null if the
   * operation is not a search operation.
   * @throws DirectoryException If a referral is found at or above the target
   * DN.  The referral URLs will be set appropriately for the references found
   * in the referral entry.
   */
  void checkTargetForReferral(Entry entry, SearchScope searchScope) throws DirectoryException
  {
    Set<String> referralURLs = entry.getReferralURLs();
    if (referralURLs != null)
    {
      throwReferralException(entry.getName(), entry.getName(), referralURLs,
                             searchScope);
    }
  }
 
  /**
   * Throws a Directory referral exception for the case where a referral entry
   * exists at or above the target DN of an operation.
   * @param targetDN The target DN of the operation, or the base object of a
   * search operation.
   * @param referralDN The DN of the referral entry.
   * @param labeledURIs The set of labeled URIs in the referral entry.
   * @param searchScope The scope of the search operation, or null if the
   * operation is not a search operation.
   * @throws DirectoryException If a referral is found at or above the target
   * DN.  The referral URLs will be set appropriately for the references found
   * in the referral entry.
   */
  private void throwReferralException(DN targetDN, DN referralDN, Set<String> labeledURIs, SearchScope searchScope)
       throws DirectoryException
  {
    ArrayList<String> URIList = new ArrayList<>(labeledURIs.size());
    for (String labeledURI : labeledURIs)
    {
      // Remove the label part of the labeled URI if there is a label.
      String uri = labeledURI;
      int i = labeledURI.indexOf(' ');
      if (i != -1)
      {
        uri = labeledURI.substring(0, i);
      }
 
      try
      {
        LDAPURL ldapurl = LDAPURL.decode(uri, false);
 
        if ("ldap".equalsIgnoreCase(ldapurl.getScheme()))
        {
          DN urlBaseDN = targetDN;
          if (!referralDN.equals(ldapurl.getBaseDN()))
          {
            urlBaseDN =
                 EntryContainer.modDN(targetDN,
                                      referralDN.size(),
                                      ldapurl.getBaseDN());
          }
          ldapurl.setBaseDN(urlBaseDN);
          if (searchScope == null)
          {
            // RFC 3296, 5.2.  Target Object Considerations:
            // In cases where the URI to be returned is a LDAP URL, the server
            // SHOULD trim any present scope, filter, or attribute list from the
            // URI before returning it.  Critical extensions MUST NOT be trimmed
            // or modified.
            StringBuilder builder = new StringBuilder(uri.length());
            ldapurl.toString(builder, true);
            uri = builder.toString();
          }
          else
          {
            // RFC 3296, 5.3.  Base Object Considerations:
            // In cases where the URI to be returned is a LDAP URL, the server
            // MUST provide an explicit scope specifier from the LDAP URL prior
            // to returning it.
            ldapurl.getAttributes().clear();
            ldapurl.setScope(searchScope);
            ldapurl.setFilter(null);
            uri = ldapurl.toString();
          }
        }
      }
      catch (DirectoryException e)
      {
        logger.traceException(e);
        // Return the non-LDAP URI as is.
      }
 
      URIList.add(uri);
    }
 
    // Throw a directory referral exception containing the URIs.
    LocalizableMessage msg = NOTE_REFERRAL_RESULT_MESSAGE.get(referralDN);
    throw new DirectoryException(
            ResultCode.REFERRAL, msg, referralDN, URIList, null);
  }
 
  /**
   * Process referral entries that are above the target DN of an operation.
   * @param targetDN The target DN of the operation, or the base object of a
   * search operation.
   * @param searchScope The scope of the search operation, or null if the
   * operation is not a search operation.
   * @throws DirectoryException If a referral is found at or above the target
   * DN.  The referral URLs will be set appropriately for the references found
   * in the referral entry.
   */
  void targetEntryReferrals(DN targetDN, SearchScope searchScope) throws DirectoryException
  {
    if (containsReferrals == ConditionResult.UNDEFINED)
    {
      containsReferrals = containsReferrals(null);
    }
 
    if (containsReferrals == ConditionResult.FALSE)
    {
      return;
    }
 
    Transaction txn = null;
    CursorConfig cursorConfig = null;
 
    try
    {
      Cursor cursor = openCursor(txn, cursorConfig);
      try
      {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
 
        // Go up through the DIT hierarchy until we find a referral.
        for (DN dn = entryContainer.getParentWithinBase(targetDN); dn != null;
             dn = entryContainer.getParentWithinBase(dn))
        {
          // Look for a record whose key matches the current DN.
          key.setData(JebFormat.dnToDNKey(dn, prefixRDNComponents));
          OperationStatus status = cursor.getSearchKey(key, data, DEFAULT);
          if (status == OperationStatus.SUCCESS)
          {
            // Construct a set of all the labeled URIs in the referral.
            Set<String> labeledURIs = new LinkedHashSet<>(cursor.count());
            do
            {
              final Pair<String, DN> uriAndDN = decodeURIAndDN(data.getData());
              final String labeledURI = uriAndDN.getFirst();
              labeledURIs.add(labeledURI);
              status = cursor.getNextDup(key, data, DEFAULT);
            } while (status == OperationStatus.SUCCESS);
 
            throwReferralException(targetDN, dn, labeledURIs, searchScope);
          }
        }
      }
      finally
      {
        cursor.close();
      }
    }
    catch (DatabaseException e)
    {
      logger.traceException(e);
    }
  }
 
  /**
   * Return search result references for a search operation using the referral
   * database to find all referral entries within scope of the search.
   * @param searchOp The search operation for which search result references
   * should be returned.
   * @return  <CODE>true</CODE> if the caller should continue processing the
   *          search request and sending additional entries and references, or
   *          <CODE>false</CODE> if not for some reason (e.g., the size limit
   *          has been reached or the search has been abandoned).
   * @throws DirectoryException If a Directory Server error occurs.
   */
  boolean returnSearchReferences(SearchOperation searchOp) throws DirectoryException
  {
    if (containsReferrals == ConditionResult.UNDEFINED)
    {
      containsReferrals = containsReferrals(null);
    }
 
    if (containsReferrals == ConditionResult.FALSE)
    {
      return true;
    }
 
    Transaction txn = null;
    CursorConfig cursorConfig = null;
 
    /*
     * We will iterate forwards through a range of the keys to
     * find subordinates of the base entry from the top of the tree
     * downwards.
     */
    byte[] baseDN = JebFormat.dnToDNKey(searchOp.getBaseDN(), prefixRDNComponents);
    final byte special = 0x00;
    byte[] suffix = Arrays.copyOf(baseDN, baseDN.length+1);
    suffix[suffix.length - 1] = special;
    byte[] end = Arrays.copyOf(suffix, suffix.length);
    end[end.length - 1] = special + 1;
 
    /*
     * Set the ending value to a value of equal length but slightly
     * greater than the suffix. Since keys are compared in
     * reverse order we must set the first byte (the comma).
     * No possibility of overflow here.
     */
 
    DatabaseEntry data = new DatabaseEntry();
    DatabaseEntry key = new DatabaseEntry(suffix);
 
    try
    {
      Cursor cursor = openCursor(txn, cursorConfig);
      try
      {
        // Initialize the cursor very close to the starting value then
        // step forward until we pass the ending value.
        for (OperationStatus status =
             cursor.getSearchKeyRange(key, data, DEFAULT);
             status == OperationStatus.SUCCESS;
             status = cursor.getNextNoDup(key, data, DEFAULT))
        {
          int cmp = dn2uriComparator.compare(key.getData(), end);
          if (cmp >= 0)
          {
            // We have gone past the ending value.
            break;
          }
 
          // We have found a subordinate referral.
          final Pair<String, DN> uriAndDN = decodeURIAndDN(data.getData());
          final String labeledURI = uriAndDN.getFirst();
          final DN dn = uriAndDN.getSecond();
 
          // Make sure the referral is within scope.
          if (searchOp.getScope() == SearchScope.SINGLE_LEVEL
              && findDNKeyParent(key.getData()) != baseDN.length)
          {
            continue;
          }
 
          // Construct a list of all the URIs in the referral.
          ArrayList<String> URIList = new ArrayList<>(cursor.count());
          do
          {
            // Remove the label part of the labeled URI if there is a label.
            String uri = labeledURI;
            int i = labeledURI.indexOf(' ');
            if (i != -1)
            {
              uri = labeledURI.substring(0, i);
            }
 
            // From RFC 3296 section 5.4:
            // If the URI component is not a LDAP URL, it should be returned as
            // is.  If the LDAP URL's DN part is absent or empty, the DN part
            // must be modified to contain the DN of the referral object.  If
            // the URI component is a LDAP URL, the URI SHOULD be modified to
            // add an explicit scope specifier.
            try
            {
              LDAPURL ldapurl = LDAPURL.decode(uri, false);
 
              if ("ldap".equalsIgnoreCase(ldapurl.getScheme()))
              {
                if (ldapurl.getBaseDN().isRootDN())
                {
                  ldapurl.setBaseDN(dn);
                }
                ldapurl.getAttributes().clear();
                if (searchOp.getScope() == SearchScope.SINGLE_LEVEL)
                {
                  ldapurl.setScope(SearchScope.BASE_OBJECT);
                }
                else
                {
                  ldapurl.setScope(SearchScope.WHOLE_SUBTREE);
                }
                ldapurl.setFilter(null);
                uri = ldapurl.toString();
              }
            }
            catch (DirectoryException e)
            {
              logger.traceException(e);
              // Return the non-LDAP URI as is.
            }
 
            URIList.add(uri);
            status = cursor.getNextDup(key, data, DEFAULT);
          } while (status == OperationStatus.SUCCESS);
 
          SearchResultReference reference = new SearchResultReference(URIList);
          if (!searchOp.returnReference(dn, reference))
          {
            return false;
          }
        }
      }
      finally
      {
        cursor.close();
      }
    }
    catch (DatabaseException e)
    {
      logger.traceException(e);
    }
 
    return true;
  }
}