| | |
| | | */ |
| | | package org.opends.server.admin; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | import org.forgerock.i18n.slf4j.LocalizedLogger; |
| | | import org.forgerock.opendj.ldap.ModificationType; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | | import org.forgerock.opendj.ldap.SearchScope; |
| | |
| | | */ |
| | | public final class AdministrationDataSync |
| | | { |
| | | private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); |
| | | |
| | | /** |
| | | * The root connection. |
| | | */ |
| | | /** The root connection. */ |
| | | private InternalClientConnection internalConnection; |
| | | |
| | | /** |
| | | * The attribute name used to store the port. TODO Use the default |
| | | * one. |
| | | */ |
| | | /** 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. |
| | | * Create an object that will synchronize configuration and the admin data. |
| | | * |
| | | * @param internalConnection |
| | | * The root connection. |
| | |
| | | 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 |
| | | * cn=config. If and inconsistency is detected, we log a warning |
| | | * message and update "cn=admin data" |
| | | */ |
| | | public void synchronize() |
| | |
| | | checkAdminConnector(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Check if the admin connector is in sync. The desynchronization |
| | | * could occurs after the upgrade from 1.0. |
| | |
| | | } |
| | | |
| | | // Get the admin port |
| | | String adminPort = getAttr("cn=Administration Connector,cn=config", |
| | | LDAP_PORT); |
| | | String adminPort = getAttr("cn=Administration Connector,cn=config", LDAP_PORT); |
| | | if (adminPort == null) |
| | | { |
| | | // best effort. |
| | | return; |
| | | } |
| | | |
| | | LinkedList<Modification> mods = new LinkedList<>(); |
| | | // 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))); |
| | | AttributeType attrType1 = DirectoryServer.getAttributeType("adminport".toLowerCase(), true); |
| | | AttributeType attrType2 = DirectoryServer.getAttributeType("adminEnabled".toLowerCase(), true); |
| | | |
| | | // 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"))); |
| | | LinkedList<Modification> mods = new LinkedList<>(); |
| | | mods.add(new Modification(ModificationType.REPLACE, Attributes.create(attrType1, adminPort))); |
| | | mods.add(new Modification(ModificationType.REPLACE, Attributes.create(attrType2, "true"))); |
| | | |
| | | // Process modification |
| | | internalConnection.processModify(serverEntryDN, mods); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Look for the DN of the local register server. Assumption: default |
| | | * Connection Handler naming is used. |
| | |
| | | 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); |
| | | 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"); |
| | | String val = getAttr("cn=LDAPS Connection Handler,cn=Connection Handlers,cn=config", "ds-cfg-enabled"); |
| | | if (val != null) |
| | | { |
| | | ldapsPortEnable = val.toLowerCase().equals("true"); |
| | | ldapsPortEnable = "true".equals(val.toLowerCase()); |
| | | } |
| | | if ((ldapPort == null) && (ldapsPort == null)) |
| | | if (ldapPort == null && ldapsPort == null) |
| | | { |
| | | // best effort (see assumption) |
| | | return null; |
| | |
| | | String hostName; |
| | | try |
| | | { |
| | | hostName = java.net.InetAddress.getLocalHost().getCanonicalHostName(); |
| | | hostName = InetAddress.getLocalHost().getCanonicalHostName(); |
| | | } |
| | | catch (Throwable t) |
| | | { |
| | |
| | | InternalSearchOperation op = internalConnection.processSearch(request); |
| | | if (op.getResultCode() == ResultCode.SUCCESS) |
| | | { |
| | | Entry entry = null; |
| | | for (Entry currentEntry : op.getSearchEntries()) |
| | | { |
| | | String currentHostname = |
| | | currentEntry.parseAttribute("hostname").asString(); |
| | | try |
| | | { |
| | | String currentIPAddress = java.net.InetAddress.getByName( |
| | | currentHostname).getCanonicalHostName(); |
| | | if (currentIPAddress.equals(hostName)) |
| | | { |
| | | // Check if one of the port match |
| | | String currentport = |
| | | currentEntry.parseAttribute("ldapport").asString(); |
| | | if (currentport.equals(ldapPort)) |
| | | { |
| | | entry = currentEntry; |
| | | break; |
| | | } |
| | | if (ldapsPortEnable) |
| | | { |
| | | currentport = |
| | | currentEntry.parseAttribute("ldapsport").asString(); |
| | | if (currentport.equals(ldapsPort)) |
| | | { |
| | | entry = currentEntry; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // best effort. |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | Entry entry = findSameHostAndPort(op.getSearchEntries(), hostName, ldapPort, ldapsPortEnable, ldapsPort); |
| | | if (entry != null) |
| | | { |
| | | returnDN = entry.getName(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | catch (DirectoryException e) |
| | | { |
| | |
| | | return returnDN; |
| | | } |
| | | |
| | | |
| | | private Entry findSameHostAndPort(LinkedList<SearchResultEntry> searchResultEntries, |
| | | String hostName, String ldapPort, boolean ldapsPortEnable, String ldapsPort) |
| | | { |
| | | for (Entry currentEntry : searchResultEntries) |
| | | { |
| | | String currentHostname = currentEntry.parseAttribute("hostname").asString(); |
| | | try |
| | | { |
| | | String currentIPAddress = InetAddress.getByName(currentHostname).getCanonicalHostName(); |
| | | if (currentIPAddress.equals(hostName)) |
| | | { |
| | | // Check if one of the port match |
| | | String currentport = currentEntry.parseAttribute("ldapport").asString(); |
| | | if (currentport.equals(ldapPort)) |
| | | { |
| | | return currentEntry; |
| | | } |
| | | if (ldapsPortEnable) |
| | | { |
| | | currentport = currentEntry.parseAttribute("ldapsport").asString(); |
| | | if (currentport.equals(ldapsPort)) |
| | | { |
| | | return currentEntry; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // best effort. |
| | | continue; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * Gets an attribute value from an entry. |
| | |
| | | { |
| | | // can not happen |
| | | // best effort. |
| | | // TODO Log an Error. |
| | | logger.traceException(e); |
| | | return null; |
| | | } |
| | | |
| | |
| | | adminConnectorEntry = result.getFirst(); |
| | | } |
| | | |
| | | AttributeType attrType = DirectoryServer.getAttributeType(attrName); |
| | | if (attrType == null) |
| | | { |
| | | attrType = DirectoryServer.getDefaultAttributeType(attrName); |
| | | } |
| | | |
| | | AttributeType attrType = DirectoryServer.getAttributeType(attrName, true); |
| | | List<Attribute> attrs = adminConnectorEntry.getAttribute(attrType); |
| | | if (attrs == null) |
| | | 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(); |
| | | } |
| | | |
| | | // Get the attribute value |
| | | return attrs.get(0).iterator().next().toString(); |
| | | // can not happen |
| | | // best effort. |
| | | // TODO Log an Error. |
| | | return null; |
| | | } |
| | | |
| | | } |