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

Jean-Noel Rouvignac
20.06.2014 857b05043604e23dc55b0145ec724e87106f7058
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
/*
 * 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 2007-2008 Sun Microsystems, Inc.
 */
 
package org.opends.server.core;
 
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.ResultCode;
import org.opends.server.api.Backend;
import static org.opends.server.util.Validator.ensureNotNull;
import org.opends.messages.Message;
import static org.opends.messages.CoreMessages.*;
 
import java.util.TreeMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
 
/**
 * Registry for maintaining the set of registered base DN's, assocated
 * backends and naming context information.
 */
public class BaseDnRegistry {
 
  // The set of base DNs registered with the server.
  private TreeMap<DN,Backend> baseDNs;
 
  // The set of private naming contexts registered with the server.
  private TreeMap<DN,Backend> privateNamingContexts;
 
  // The set of public naming contexts registered with the server.
  private TreeMap<DN,Backend> publicNamingContexts;
 
  // Indicates whether or not this base DN registry is in test mode.
  // A registry instance that is in test mode will not modify backend
  // objects referred to in the above maps.
  private boolean testOnly;
 
  /**
   * Registers a base DN with this registry.
   *
   * @param  baseDN to register
   * @param  backend with which the base DN is assocated
   * @param  isPrivate indicates whether or not this base DN is private
   * @return list of error messages generated by registering the base DN
   *         that should be logged if the changes to this registry are
   *         committed to the server
   * @throws DirectoryException if the base DN cannot be registered
   */
  public List<Message> registerBaseDN(DN baseDN, Backend backend,
                                      boolean isPrivate)
          throws DirectoryException
  {
 
    List<Message> errors = new LinkedList<Message>();
 
    // Check to see if the base DN is already registered with the server.
    Backend existingBackend = baseDNs.get(baseDN);
    if (existingBackend != null)
    {
      Message message = ERR_REGISTER_BASEDN_ALREADY_EXISTS.
          get(String.valueOf(baseDN), backend.getBackendID(),
              existingBackend.getBackendID());
      throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
    }
 
 
    // Check to see if the backend is already registered with the server for
    // any other base DN(s).  The new base DN must not have any hierarchical
    // relationship with any other base Dns for the same backend.
    LinkedList<DN> otherBaseDNs = new LinkedList<DN>();
    for (DN dn : baseDNs.keySet())
    {
      Backend b = baseDNs.get(dn);
      if (b.equals(backend))
      {
        otherBaseDNs.add(dn);
 
        if (baseDN.isAncestorOf(dn) || baseDN.isDescendantOf(dn))
        {
          Message message = ERR_REGISTER_BASEDN_HIERARCHY_CONFLICT.
              get(String.valueOf(baseDN), backend.getBackendID(),
                  String.valueOf(dn));
          throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                                       message);
        }
      }
    }
 
 
    // Check to see if the new base DN is subordinate to any other base DN
    // already defined.  If it is, then any other base DN(s) for the same
    // backend must also be subordinate to the same base DN.
    Backend superiorBackend = null;
    DN      superiorBaseDN        ;
    DN      parentDN        = baseDN.getParent();
    while (parentDN != null)
    {
      if (baseDNs.containsKey(parentDN))
      {
        superiorBaseDN  = parentDN;
        superiorBackend = baseDNs.get(parentDN);
 
        for (DN dn : otherBaseDNs)
        {
          if (! dn.isDescendantOf(superiorBaseDN))
          {
            Message message = ERR_REGISTER_BASEDN_DIFFERENT_PARENT_BASES.
                get(String.valueOf(baseDN), backend.getBackendID(),
                    String.valueOf(dn));
            throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                                         message);
          }
        }
 
        break;
      }
 
      parentDN = parentDN.getParent();
    }
 
    if (superiorBackend == null)
    {
      if (backend.getParentBackend() != null)
      {
        Message message = ERR_REGISTER_BASEDN_NEW_BASE_NOT_SUBORDINATE.
            get(String.valueOf(baseDN), backend.getBackendID(),
                backend.getParentBackend().getBackendID());
        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                                     message);
      }
    }
 
 
    // Check to see if the new base DN should be the superior base DN for any
    // other base DN(s) already defined.
    LinkedList<Backend> subordinateBackends = new LinkedList<Backend>();
    LinkedList<DN>      subordinateBaseDNs  = new LinkedList<DN>();
    for (DN dn : baseDNs.keySet())
    {
      Backend b = baseDNs.get(dn);
      parentDN = dn.getParent();
      while (parentDN != null)
      {
        if (parentDN.equals(baseDN))
        {
          subordinateBaseDNs.add(dn);
          subordinateBackends.add(b);
          break;
        }
        else if (baseDNs.containsKey(parentDN))
        {
          break;
        }
 
        parentDN = parentDN.getParent();
      }
    }
 
 
    // If we've gotten here, then the new base DN is acceptable.  If we should
    // actually apply the changes then do so now.
 
    // Check to see if any of the registered backends already contain an
    // entry with the DN specified as the base DN.  This could happen if
    // we're creating a new subordinate backend in an existing directory
    // (e.g., moving the "ou=People,dc=example,dc=com" branch to its own
    // backend when that data already exists under the "dc=example,dc=com"
    // backend).  This condition shouldn't prevent the new base DN from
    // being registered, but it's definitely important enough that we let
    // the administrator know about it and remind them that the existing
    // backend will need to be reinitialized.
    if (superiorBackend != null)
    {
      if (superiorBackend.entryExists(baseDN))
      {
        Message message = WARN_REGISTER_BASEDN_ENTRIES_IN_MULTIPLE_BACKENDS.
            get(superiorBackend.getBackendID(), String.valueOf(baseDN),
                backend.getBackendID());
        errors.add(message);
      }
    }
 
 
    baseDNs.put(baseDN, backend);
 
    if (superiorBackend == null)
    {
      if (isPrivate)
      {
        if (!testOnly)
        {
          backend.setPrivateBackend(true);
        }
        privateNamingContexts.put(baseDN, backend);
      }
      else
      {
        if (!testOnly)
        {
          backend.setPrivateBackend(false);
        }
        publicNamingContexts.put(baseDN, backend);
      }
    }
    else if (otherBaseDNs.isEmpty())
    {
      if (!testOnly)
      {
        backend.setParentBackend(superiorBackend);
        superiorBackend.addSubordinateBackend(backend);
      }
    }
 
    if (!testOnly)
    {
      for (Backend b : subordinateBackends)
      {
        Backend oldParentBackend = b.getParentBackend();
        if (oldParentBackend != null)
        {
          oldParentBackend.removeSubordinateBackend(b);
        }
 
        b.setParentBackend(backend);
        backend.addSubordinateBackend(b);
      }
    }
 
    for (DN dn : subordinateBaseDNs)
    {
      publicNamingContexts.remove(dn);
      privateNamingContexts.remove(dn);
    }
 
    return errors;
  }
 
 
  /**
   * Deregisters a base DN with this registry.
   *
   * @param  baseDN to deregister
   * @return list of error messages generated by deregistering the base DN
   *         that should be logged if the changes to this registry are
   *         committed to the server
   * @throws DirectoryException if the base DN could not be deregistered
   */
  public List<Message> deregisterBaseDN(DN baseDN)
         throws DirectoryException
  {
    LinkedList<Message> errors = new LinkedList<Message>();
 
    ensureNotNull(baseDN);
 
    // Make sure that the Directory Server actually contains a backend with
    // the specified base DN.
    Backend backend = baseDNs.get(baseDN);
    if (backend == null)
    {
      Message message =
          ERR_DEREGISTER_BASEDN_NOT_REGISTERED.get(String.valueOf(baseDN));
      throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
    }
 
 
    // Check to see if the backend has a parent backend, and whether it has
    // any subordinates with base DNs that are below the base DN to remove.
    Backend             superiorBackend     = backend.getParentBackend();
    LinkedList<Backend> subordinateBackends = new LinkedList<Backend>();
    if (backend.getSubordinateBackends() != null)
    {
      for (Backend b : backend.getSubordinateBackends())
      {
        for (DN dn : b.getBaseDNs())
        {
          if (dn.isDescendantOf(baseDN))
          {
            subordinateBackends.add(b);
            break;
          }
        }
      }
    }
 
 
    // See if there are any other base DNs registered within the same backend.
    LinkedList<DN> otherBaseDNs = new LinkedList<DN>();
    for (DN dn : baseDNs.keySet())
    {
      if (dn.equals(baseDN))
      {
        continue;
      }
 
      Backend b = baseDNs.get(dn);
      if (backend.equals(b))
      {
        otherBaseDNs.add(dn);
      }
    }
 
 
    // If we've gotten here, then it's OK to make the changes.
 
    // Get rid of the references to this base DN in the mapping tree
    // information.
    baseDNs.remove(baseDN);
    publicNamingContexts.remove(baseDN);
    privateNamingContexts.remove(baseDN);
 
    if (superiorBackend == null)
    {
      // If there were any subordinate backends, then all of their base DNs
      // will now be promoted to naming contexts.
      for (Backend b : subordinateBackends)
      {
        if (!testOnly)
        {
          b.setParentBackend(null);
          backend.removeSubordinateBackend(b);
        }
 
        for (DN dn : b.getBaseDNs())
        {
          if (b.isPrivateBackend())
          {
            privateNamingContexts.put(dn, b);
          }
          else
          {
            publicNamingContexts.put(dn, b);
          }
        }
      }
    }
    else
    {
      // If there are no other base DNs for the associated backend, then
      // remove this backend as a subordinate of the parent backend.
      if (otherBaseDNs.isEmpty())
      {
        if (!testOnly)
        {
          superiorBackend.removeSubordinateBackend(backend);
        }
      }
 
 
      // If there are any subordinate backends, then they need to be made
      // subordinate to the parent backend.  Also, we should log a warning
      // message indicating that there may be inconsistent search results
      // because some of the structural entries will be missing.
      if (! subordinateBackends.isEmpty())
      {
        // Suppress this warning message on server shutdown.
        if (!DirectoryServer.getInstance().isShuttingDown()) {
          Message message = WARN_DEREGISTER_BASEDN_MISSING_HIERARCHY.get(
            String.valueOf(baseDN), backend.getBackendID());
          errors.add(message);
        }
 
        if (!testOnly)
        {
          for (Backend b : subordinateBackends)
          {
            backend.removeSubordinateBackend(b);
            superiorBackend.addSubordinateBackend(b);
            b.setParentBackend(superiorBackend);
          }
        }
      }
    }
    return errors;
  }
 
 
  /**
   * Creates a default instance.
   */
  BaseDnRegistry()
  {
    this(new TreeMap<DN,Backend>(), new TreeMap<DN,Backend>(),
         new TreeMap<DN,Backend>(), false);
  }
 
  /**
   * Returns a copy of this registry.
   *
   * @return copy of this registry
   */
  BaseDnRegistry copy()
  {
    return new BaseDnRegistry(
            new TreeMap<DN,Backend>(baseDNs),
            new TreeMap<DN,Backend>(publicNamingContexts),
            new TreeMap<DN,Backend>(privateNamingContexts),
            true);
  }
 
 
  /**
   * Creates a parameterized instance.
   *
   * @param baseDNs map
   * @param publicNamingContexts map
   * @param privateNamingContexts map
   * @param testOnly indicates whether this registry will be used for testing;
   *        when <code>true</code> this registry will not modify backends
   */
  private BaseDnRegistry(TreeMap<DN, Backend> baseDNs,
                         TreeMap<DN, Backend> publicNamingContexts,
                         TreeMap<DN, Backend> privateNamingContexts,
                         boolean testOnly)
  {
    this.baseDNs = baseDNs;
    this.publicNamingContexts = publicNamingContexts;
    this.privateNamingContexts = privateNamingContexts;
    this.testOnly = testOnly;
  }
 
 
  /**
   * Gets the mapping of registered base DNs to their associated backend.
   *
   * @return mapping from base DN to backend
   */
  Map<DN,Backend> getBaseDnMap() {
    return this.baseDNs;
  }
 
 
  /**
   * Gets the mapping of registered public naming contexts to their
   * associated backend.
   *
   * @return mapping from naming context to backend
   */
  Map<DN,Backend> getPublicNamingContextsMap() {
    return this.publicNamingContexts;
  }
 
 
  /**
   * Gets the mapping of registered private naming contexts to their
   * associated backend.
   *
   * @return mapping from naming context to backend
   */
  Map<DN,Backend> getPrivateNamingContextsMap() {
    return this.privateNamingContexts;
  }
 
 
 
 
  /**
   * Indicates whether the specified DN is contained in this registry as
   * a naming contexts.
   *
   * @param  dn  The DN for which to make the determination.
   *
   * @return  {@code true} if the specified DN is a naming context in this
   *          registry, or {@code false} if it is not.
   */
  boolean containsNamingContext(DN dn)
  {
    return (privateNamingContexts.containsKey(dn) ||
            publicNamingContexts.containsKey(dn));
  }
 
 
  /**
   * Clear and nullify this registry's internal state.
   */
  void clear() {
 
    if (baseDNs != null)
    {
      baseDNs.clear();
    }
 
    if (privateNamingContexts != null)
    {
      privateNamingContexts.clear();
    }
 
    if (publicNamingContexts != null)
    {
      publicNamingContexts.clear();
    }
 
  }
 
}