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

Nicolas Capponi
04.13.2013 e538344449d345daa6e5ecb9b05ceba5427408e9
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
/*
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  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-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2012 ForgeRock AS
 */
package org.opends.server.admin;
 
 
 
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
 
import org.opends.server.core.DirectoryServer;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.protocols.internal.InternalSearchOperation;
import org.opends.server.protocols.ldap.LDAPFilter;
import org.opends.server.schema.DirectoryStringSyntax;
import org.opends.server.types.Attribute;
import org.opends.server.types.AttributeType;
import org.opends.server.types.Attributes;
import org.opends.server.types.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.opends.server.types.DereferencePolicy;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.Entry;
import org.opends.server.types.LDAPException;
import org.opends.server.types.Modification;
import org.opends.server.types.ModificationType;
import org.opends.server.types.ResultCode;
import org.opends.server.types.SearchResultEntry;
import org.opends.server.types.SearchScope;
 
 
 
/**
 * Check if information found in "cn=admin data" is coherent with
 * cn=config. If and inconsistency is detected, we log a warning
 * message and update "cn=admin data"
 */
public final class AdministrationDataSync
{
 
  /**
   * The root connection.
   */
  private InternalClientConnection internalConnection;
 
  /**
   * The attribute name used to store the port. TODO Use the default
   * one.
   */
  private static final String LDAP_PORT = "ds-cfg-listen-port";
 
 
 
  /**
   * Create an object that will syncrhonize configuration and the
   * admin data.
   *
   * @param internalConnection
   *          The root connection.
   */
  public AdministrationDataSync(InternalClientConnection internalConnection)
  {
    this.internalConnection = internalConnection;
  }
 
 
 
  /**
   * Check if information found in "cn=admin data" is coherent with
   * cn=config. If and inconsistancy is detected, we log a warning
   * message and update "cn=admin data"
   */
  public void synchronize()
  {
    // Check if the admin connector is in sync
    checkAdminConnector();
  }
 
 
 
  /**
   * Check if the admin connector is in sync. The desynchronization
   * could occurs after the upgrade from 1.0.
   */
  private void checkAdminConnector()
  {
    // Look for the server registration in "cn=admin data"
    DN serverEntryDN = searchServerEntry();
    if (serverEntryDN == null)
    {
      // Nothing to do
      return;
    }
 
    // Get the admin port
    String adminPort = getAttr("cn=Administration Connector,cn=config",
        LDAP_PORT);
    if (adminPort == null)
    {
      // best effort.
      return;
    }
 
    LinkedList<Modification> mods = new LinkedList<Modification>();
    // adminport
    String attName = "adminport";
    AttributeType attrType = DirectoryServer.getAttributeType(attName
        .toLowerCase());
    if (attrType == null)
    {
      attrType = DirectoryServer.getDefaultAttributeType(attName.toLowerCase());
    }
    mods.add(new Modification(ModificationType.REPLACE, Attributes.create(
        attrType, adminPort)));
 
    // adminEnabled
    attName = "adminEnabled";
    attrType = DirectoryServer.getAttributeType(attName.toLowerCase());
    if (attrType == null)
    {
      attrType = DirectoryServer.getDefaultAttributeType(attName.toLowerCase());
    }
    mods.add(new Modification(ModificationType.REPLACE, Attributes.create(
        attrType, "true")));
 
    // Process modification
    internalConnection.processModify(serverEntryDN, mods);
  }
 
 
 
  /**
   * Look for the DN of the local register server. Assumption: default
   * Connection Handler naming is used.
   *
   * @return The DN of the local register server or null.
   */
  private DN searchServerEntry()
  {
    DN returnDN = null;
 
    // Get the LDAP and LDAPS port
    String ldapPort = getAttr(
        "cn=LDAP Connection Handler,cn=Connection Handlers,cn=config",
        LDAP_PORT);
    String ldapsPort = getAttr(
        "cn=LDAPS Connection Handler,cn=Connection Handlers,cn=config",
        LDAP_PORT);
    boolean ldapsPortEnable = false;
    String val = getAttr(
        "cn=LDAPS Connection Handler,cn=Connection Handlers,cn=config",
        "ds-cfg-enabled");
    if (val != null)
    {
      ldapsPortEnable = val.toLowerCase().equals("true");
    }
    if ((ldapPort == null) && (ldapsPort == null))
    {
      // best effort (see assumption)
      return null;
    }
 
    // Get the IP address of the local host.
    String hostName;
    try
    {
      hostName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
    }
    catch (Throwable t)
    {
      // best effort.
      return null;
    }
 
    // Look for a local server with the Ldap Port.
    String attrName = "hostname";
    AttributeType hostnameType = DirectoryServer.getAttributeType(attrName);
    if (hostnameType == null)
    {
      hostnameType = DirectoryServer.getDefaultAttributeType(attrName);
    }
    try
    {
      InternalSearchOperation op = internalConnection.processSearch(
          "cn=Servers,cn=admin data",
          SearchScope.SINGLE_LEVEL, "objectclass=*");
      if (op.getResultCode() == ResultCode.SUCCESS)
      {
        Entry entry = null;
        for (Entry currentEntry : op.getSearchEntries())
        {
          String currentHostname = currentEntry.getAttributeValue(hostnameType,
              DirectoryStringSyntax.DECODER);
          try
          {
            String currentIPAddress = java.net.InetAddress.getByName(
                currentHostname).getCanonicalHostName();
            if (currentIPAddress.equals(hostName))
            {
              // Check if one of the port match
              attrName = "ldapport";
              AttributeType portType = DirectoryServer
                  .getAttributeType(attrName);
              if (portType == null)
              {
                portType = DirectoryServer.getDefaultAttributeType(attrName);
              }
              String currentport = currentEntry.getAttributeValue(portType,
                  DirectoryStringSyntax.DECODER);
              if (currentport.equals(ldapPort))
              {
                entry = currentEntry;
                break;
              }
              if (ldapsPortEnable)
              {
                attrName = "ldapsport";
                portType = DirectoryServer.getAttributeType(attrName);
                if (portType == null)
                {
                  portType = DirectoryServer.getDefaultAttributeType(attrName);
                }
                currentport = currentEntry.getAttributeValue(portType,
                    DirectoryStringSyntax.DECODER);
                if (currentport.equals(ldapsPort))
                {
                  entry = currentEntry;
                  break;
                }
              }
            }
          }
          catch (Exception e)
          {
            // best effort.
            continue;
          }
        }
 
        if (entry != null)
        {
          returnDN = entry.getDN();
        }
      }
 
    }
    catch (DirectoryException e)
    {
      // never happens because the filter is always valid.
      return null;
    }
    return returnDN;
  }
 
 
 
  /**
   * Gets an attribute value from an entry.
   *
   * @param DN
   *          The DN of the entry.
   * @param attrName
   *          The attribute name.
   * @return The attribute value or {@code null} if the value could
   *         not be retrieved.
   */
  private String getAttr(String baseDN, String attrName)
  {
    // Prepare the ldap search
    LDAPFilter filter;
    try
    {
      filter = LDAPFilter.decode("objectclass=*");
    }
    catch (LDAPException e)
    {
      // can not happen
      // best effort.
      // TODO Log an Error.
      return null;
    }
 
    LinkedHashSet<String> attributes = new LinkedHashSet<String>(1);
    attributes.add(attrName);
    InternalSearchOperation search = internalConnection.processSearch(
        ByteString.valueOf(baseDN), SearchScope.BASE_OBJECT,
        DereferencePolicy.DEREF_ALWAYS, 0, 0, false, filter, attributes);
 
    if ((search.getResultCode() != ResultCode.SUCCESS))
    {
      // can not happen
      // best effort.
      // TODO Log an Error.
      return null;
    }
 
    SearchResultEntry adminConnectorEntry = null;
 
    /*
     * Read the port from the PORT attribute
     */
    LinkedList<SearchResultEntry> result = search.getSearchEntries();
    if (!result.isEmpty())
    {
      adminConnectorEntry = result.getFirst();
    }
 
    AttributeType attrType = DirectoryServer.getAttributeType(attrName);
    if (attrType == null)
    {
      attrType = DirectoryServer.getDefaultAttributeType(attrName);
    }
 
    List<Attribute> attrs = adminConnectorEntry.getAttribute(attrType);
 
    if (attrs == null)
    {
      // can not happen
      // best effort.
      // TODO Log an Error.
      return null;
    }
 
    // Get the attribute value
    return attrs.get(0).iterator().next().toString();
  }
 
}