Search in sources :

Example 51 with Connection

use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.

the class Bootstrap method getConfiguration.

/**
     * Returns System Property with an URL.
     *
     * @param bootstrapData an URL that contains information on how to
     *        fetch the server configuration properties.
     * @param reinit <code>true</code> to re initialize the system.
     * @throws Exception if properties cannot be loaded.
     */
private static Properties getConfiguration(BootstrapData bootstrapData, boolean reinit, boolean bStartDS) throws Exception {
    Properties properties = null;
    bootstrapData.initSMS(bStartDS);
    if (reinit) {
        AdminUtils.initialize();
        SMSAuthModule.initialize();
    }
    DSConfigMgr dsCfg = DSConfigMgr.getDSConfigMgr();
    ServerGroup sg = dsCfg.getServerGroup("sms");
    if (sg == null) {
        return null;
    }
    try (ConnectionFactory factory = dsCfg.getNewConnectionFactory("sms", LDAPUser.Type.AUTH_ADMIN);
        Connection conn = factory.getConnection()) {
    // Success case. Managed to get connection
    } catch (LDAPServiceException e) {
        // ignore, DS is down
        return null;
    }
    String dsbasedn = bootstrapData.getUserBaseDN();
    String pwd = bootstrapData.getDsameUserPassword();
    String dsameUser = "cn=dsameuser,ou=DSAME Users," + dsbasedn;
    String instanceName = bootstrapData.getInstanceName();
    SSOToken ssoToken = getSSOToken(dsbasedn, dsameUser, JCECrypt.decode(pwd));
    try {
        properties = ServerConfiguration.getServerInstance(ssoToken, instanceName);
        if (properties != null) {
            // set debug level to error because debug.message in
            // SMSEntry.initializedClass won't work and will print out
            // error message. Save the debug level and will be restored
            // after SMSEntry.initializedClass.
            String debugLevel = (String) properties.get(Constants.SERVICES_DEBUG_LEVEL);
            boolean debugSetAtDefault = false;
            if (debugLevel == null) {
                debugSetAtDefault = true;
            }
            properties.setProperty(Constants.SERVICES_DEBUG_LEVEL, Debug.STR_ERROR);
            SystemProperties.initializeProperties(properties, true, false);
            DebugPropertiesObserver debugPO = DebugPropertiesObserver.getInstance();
            String serverConfigXML = ServerConfiguration.getServerConfigXML(ssoToken, instanceName);
            Crypt.reinitialize();
            BootstrapData.loadServerConfigXML(serverConfigXML);
            SMSEntry.initializeClass();
            if (debugSetAtDefault) {
                properties.remove(Constants.SERVICES_DEBUG_LEVEL);
            } else {
                properties.setProperty(Constants.SERVICES_DEBUG_LEVEL, debugLevel);
            }
            SystemProperties.initializeProperties(properties, true, true);
            String defaultDebugLevel = SystemProperties.getProperties().getProperty(Constants.SERVICES_DEBUG_LEVEL);
            if (debugSetAtDefault) {
                properties.setProperty(Constants.SERVICES_DEBUG_LEVEL, defaultDebugLevel);
                SystemProperties.initializeProperties(properties, true, true);
            }
            AdminUtils.initialize();
            SMSAuthModule.initialize();
            debugPO.notifyChanges();
            SMSPropertiesObserver.getInstance().notifyChanges();
            SystemProperties.setServerInstanceName(instanceName);
        // ConfigurationObserver is already added when 
        // DebugPropertiesObserver.getInstance().notifyChanges();
        // is called. Adding again causes 2 notification events
        // to be sent.
        // ServiceConfigManager scm = new ServiceConfigManager(
        //    Constants.SVC_NAME_PLATFORM, (SSOToken)
        //        AccessController.doPrivileged(
        //        AdminTokenAction.getInstance()));
        // scm.addListener(ConfigurationObserver.getInstance());
        }
    } catch (SMSException e) {
        //ignore. product is not configured yet.
        System.out.println("Bootstrap.getConfiguration :" + e);
        properties = null;
    }
    return properties;
}
Also used : ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) ServerGroup(com.iplanet.services.ldap.ServerGroup) SSOToken(com.iplanet.sso.SSOToken) DebugPropertiesObserver(com.sun.identity.common.DebugPropertiesObserver) SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) DSConfigMgr(com.iplanet.services.ldap.DSConfigMgr) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) SystemProperties(com.iplanet.am.util.SystemProperties) Properties(java.util.Properties)

Example 52 with Connection

use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.

the class AMSetupDSConfig method isDITLoaded.

/**
     * Check if DS is loaded with OpenAM entries
     *
     * @param ssl <code>true</code> of directory server is running on LDAPS.
     * @return <code>true</code> if Service Schema is loaded into
     *         Directory Server.
     */
String isDITLoaded(boolean ssl) {
    String baseDN = "ou=services," + suffix;
    String filter = "(|(ou=DAI)(ou=sunIdentityRepositoryService))";
    try (Connection conn = getLDAPConnection(ssl)) {
        ConnectionEntryReader results = conn.search(LDAPRequests.newSearchRequest(baseDN, SearchScope.WHOLE_SUBTREE, filter, "dn"));
        return Boolean.toString(results.hasNext());
    } catch (LdapException e) {
        if (Debug.getInstance(SetupConstants.DEBUG_NAME).messageEnabled()) {
            Debug.getInstance(SetupConstants.DEBUG_NAME).message("AMSetupDSConfig.isDITLoaded: LDAP Operation return code: " + e.getResult().getResultCode());
        }
        return "false";
    }
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) Connection(org.forgerock.opendj.ldap.Connection) LdapException(org.forgerock.opendj.ldap.LdapException)

Example 53 with Connection

use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.

the class UserIdRepo method loadSchema.

private void loadSchema(Map userRepo, String basedir, ServletContext servletCtx, String strFiles, String type) throws Exception {
    try (Connection conn = getLDAPConnection(userRepo)) {
        String dbName = getDBName(userRepo, conn);
        for (String file : writeSchemaFiles(basedir, dbName, servletCtx, strFiles, userRepo, type)) {
            Object[] params = { file };
            SetupProgress.reportStart("emb.loadingschema", params);
            LdifUtils.createSchemaFromLDIF(file, conn);
            SetupProgress.reportEnd("emb.success", null);
            File f = new File(file);
            f.delete();
        }
    }
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) File(java.io.File)

Example 54 with Connection

use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.

the class SMSLdapObject method modify.

/**
     * Save the entry using the token provided. The principal provided will be
     * used to get the proxy connection.
     */
public void modify(SSOToken token, String dn, ModificationItem[] mods) throws SMSException, SSOException {
    int retry = 0;
    ModifyRequest request = copyModItemsToModifyRequest(DN.valueOf(dn), mods);
    while (retry <= connNumRetry) {
        debug.message("SMSLdapObject.modify() retry: {}", retry);
        try (Connection conn = getConnection(token.getPrincipal())) {
            conn.modify(request);
            debug.message("SMSLdapObject.modify(): Successfully modified entry: {}", dn);
            break;
        } catch (LdapException e) {
            ResultCode errorCode = e.getResult().getResultCode();
            if (!retryErrorCodes.contains(errorCode) || retry == connNumRetry) {
                debug.error("SMSLdapObject.modify(): Error modifying: {} By Principal {}", dn, token.getPrincipal().getName(), e);
                throw new SMSException(e, "sms-entry-cannot-modify");
            }
            retry++;
            try {
                Thread.sleep(connRetryInterval);
            } catch (InterruptedException ex) {
            // ignored
            }
        }
    }
}
Also used : SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 55 with Connection

use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.

the class SMSLdapObject method getSubEntries.

private Set<String> getSubEntries(SSOToken token, String dn, String filter, int numOfEntries, boolean sortResults, boolean ascendingOrder) throws SMSException, SSOException {
    SearchRequest request = getSearchRequest(dn, filter, SearchScope.SINGLE_LEVEL, numOfEntries, 0, sortResults, ascendingOrder, getNamingAttribute(), O_ATTR);
    int retry = 0;
    Set<String> answer = new LinkedHashSet<>();
    ConnectionEntryReader results;
    while (retry <= connNumRetry) {
        debug.message("SMSLdapObject.subEntries() retry: {}", retry);
        try (Connection conn = getConnection(token.getPrincipal())) {
            // Get the sub entries
            ConnectionEntryReader iterResults = conn.search(request);
            iterResults.hasNext();
            results = iterResults;
            // Construct the results and return
            try {
                while (results != null && results.hasNext()) {
                    try {
                        if (results.isReference()) {
                            debug.warning("Skipping reference result: {}", results.readReference());
                            continue;
                        }
                        SearchResultEntry entry = results.readEntry();
                        // Workaround for 3823, where (objectClass=*) is used
                        if (entry.getName().toString().toLowerCase().startsWith("ou=")) {
                            answer.add(entry.getName().rdn().getFirstAVA().getAttributeValue().toString());
                        }
                    } catch (SearchResultReferenceIOException e) {
                        debug.error("SMSLdapObject.subEntries: Reference should be handled already for dn {}", dn, e);
                    }
                }
            } catch (LdapException e) {
                debug.warning("SMSLdapObject.subEntries: Error in obtaining sub-entries: {}", dn, e);
                throw new SMSException(e, "sms-entry-cannot-obtain");
            }
            break;
        } catch (LdapException e) {
            ResultCode errorCode = e.getResult().getResultCode();
            if (errorCode.equals(ResultCode.NO_SUCH_OBJECT)) {
                debug.message("SMSLdapObject.subEntries(): entry not present: {}", dn);
                break;
            }
            if (!retryErrorCodes.contains(errorCode) || retry >= connNumRetry) {
                debug.warning("SMSLdapObject.subEntries: Unable to search for sub-entries: {}", dn, e);
                throw new SMSException(e, "sms-entry-cannot-search");
            }
            retry++;
            try {
                Thread.sleep(connRetryInterval);
            } catch (InterruptedException ex) {
            // ignored
            }
        }
    }
    debug.message("SMSLdapObject.subEntries: Successfully obtained sub-entries for {}", dn);
    return answer;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Aggregations

Connection (org.forgerock.opendj.ldap.Connection)88 LdapException (org.forgerock.opendj.ldap.LdapException)70 ByteString (org.forgerock.opendj.ldap.ByteString)45 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)42 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)35 ResultCode (org.forgerock.opendj.ldap.ResultCode)29 Attribute (org.forgerock.opendj.ldap.Attribute)25 HashSet (java.util.HashSet)23 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)20 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)17 IOException (java.io.IOException)16 SSOException (com.iplanet.sso.SSOException)15 PolicyException (com.sun.identity.policy.PolicyException)14 SMSException (com.sun.identity.sm.SMSException)13 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)12 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)11 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)10 InvalidNameException (com.sun.identity.policy.InvalidNameException)10 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)10 LinkedHashSet (java.util.LinkedHashSet)10