Search in sources :

Example 41 with Connection

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

the class SMSRepositoryMig method migrate.

private static void migrate(ConnectionFactory factory, String host, int port, String binddn, String pw, String basedn, String flatfiledir) throws Exception {
    // check args
    if (port < 0 || binddn == null || binddn.length() == 0 || pw == null || pw.length() == 0 || basedn == null || basedn.length() == 0 || flatfiledir == null || flatfiledir.length() == 0) {
        throw new IllegalArgumentException("SMSRepositoryMig: One or more invalid " + "arguments in constructor");
    }
    // Create the SMSFlatFileObject
    SMSFlatFileObject smsFlatFileObject = new SMSFlatFileObject();
    try (Connection conn = factory.getConnection()) {
        // Loop through LDAP attributes, create SMS object for each.
        ConnectionEntryReader res = conn.search(LDAPRequests.newSearchRequest("ou=services," + basedn, SearchScope.BASE_OBJECT, "(objectclass=*)", "*"));
        while (res.hasNext()) {
            if (res.isReference()) {
                //ignore
                res.readReference();
                System.out.println("ERROR: LDAP Referral not supported.");
                System.out.println("LDAPReferralException received");
            } else {
                SearchResultEntry entry;
                try {
                    entry = res.readEntry();
                    createSMSEntry(smsFlatFileObject, entry.getName().toString(), entry.getAllAttributes());
                } catch (LdapException e) {
                    System.out.println("ERROR: LDAP Exception encountered: " + e.toString());
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) Connection(org.forgerock.opendj.ldap.Connection) LdapException(org.forgerock.opendj.ldap.LdapException) SMSFlatFileObject(com.sun.identity.sm.flatfile.SMSFlatFileObject) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 42 with Connection

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

the class ImportServiceConfiguration method getLDAPConnection.

private Connection getLDAPConnection() throws CLIException {
    IOutput outputWriter = getOutputWriter();
    if (isVerbose()) {
        outputWriter.printlnMessage(getResourceString("import-service-configuration-connecting-to-ds"));
    }
    try {
        Connection conn;
        DSConfigMgr dsCfg = DSConfigMgr.getDSConfigMgr();
        ServerGroup sg = dsCfg.getServerGroup("sms");
        if (sg != null) {
            conn = dsCfg.getNewConnectionFactory("sms", LDAPUser.Type.AUTH_ADMIN).getConnection();
        } else {
            throw new CLIException(getResourceString("import-service-configuration-not-connect-to-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED, null);
        }
        if (isVerbose()) {
            outputWriter.printlnMessage(getResourceString("import-service-configuration-connected-to-ds"));
        }
        return conn;
    } catch (LDAPServiceException | LdapException e) {
        throw new CLIException(getResourceString("import-service-configuration-not-connect-to-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED, null);
    }
}
Also used : ServerGroup(com.iplanet.services.ldap.ServerGroup) IOutput(com.sun.identity.cli.IOutput) Connection(org.forgerock.opendj.ldap.Connection) DSConfigMgr(com.iplanet.services.ldap.DSConfigMgr) CLIException(com.sun.identity.cli.CLIException) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) LdapException(org.forgerock.opendj.ldap.LdapException)

Example 43 with Connection

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

the class ImportServiceConfiguration method handleRequest.

/**
     * Services a Commandline Request.
     *
     * @param rc Request Context.
     * @throws CLIException if the request cannot serviced.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    String xmlFile = getStringOptionValue(IArgument.XML_FILE);
    String encryptSecret = getStringOptionValue(IArgument.ENCRYPT_SECRET);
    try {
        encryptSecret = CLIUtil.getFileContent(getCommandManager(), encryptSecret).trim();
    } catch (CLIException clie) {
    //There is no encryptSecret file
    }
    validateEncryptSecret(xmlFile, encryptSecret);
    // disable notification
    SystemProperties.initializeProperties(Constants.SMS_ENABLE_DB_NOTIFICATION, "true");
    SystemProperties.initializeProperties("com.sun.am.event.connection.disable.list", "sm,aci,um");
    // disable error debug messsage
    SystemProperties.initializeProperties(Constants.SYS_PROPERTY_INSTALL_TIME, "true");
    IOutput outputWriter = getOutputWriter();
    try (Connection ldConnection = getLDAPConnection()) {
        InitializeSystem initSys = CommandManager.initSys;
        SSOToken ssoToken = initSys.getSSOToken(getAdminPassword());
        DirectoryServerVendor.Vendor vendor = DirectoryServerVendor.getInstance().query(ldConnection);
        if (!vendor.name.equals(DirectoryServerVendor.OPENDJ) && !vendor.name.equals(DirectoryServerVendor.OPENDS) && !vendor.name.equals(DirectoryServerVendor.ODSEE)) {
            throw new CLIException(getResourceString("import-service-configuration-unknown-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
        }
        loadLDIF(vendor, ldConnection);
        String ouServices = "ou=services," + initSys.getRootSuffix();
        if (this.isOuServicesExists(ssoToken, ouServices)) {
            System.out.print(getResourceString("import-service-configuration-prompt-delete") + " ");
            String value = (new BufferedReader(new InputStreamReader(System.in))).readLine();
            value = value.trim();
            if (value.equalsIgnoreCase("y") || value.equalsIgnoreCase("yes")) {
                outputWriter.printlnMessage(getResourceString("import-service-configuration-processing"));
                deleteOuServicesDescendents(ssoToken, ouServices);
                importData(xmlFile, encryptSecret, ssoToken);
            }
        } else {
            outputWriter.printlnMessage(getResourceString("import-service-configuration-processing"));
            importData(xmlFile, encryptSecret, ssoToken);
        }
    } catch (SMSException e) {
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (LdapException e) {
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IOException e) {
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (LoginException e) {
        throw new CLIException(getCommandManager().getResourceBundle().getString("exception-LDAP-login-failed"), ExitCodes.LDAP_LOGIN_FAILED);
    } catch (InvalidAuthContextException e) {
        throw new CLIException(getCommandManager().getResourceBundle().getString("exception-LDAP-login-failed"), ExitCodes.LDAP_LOGIN_FAILED);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) InputStreamReader(java.io.InputStreamReader) SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) SSOException(com.iplanet.sso.SSOException) IOException(java.io.IOException) InitializeSystem(com.sun.identity.cli.InitializeSystem) IOutput(com.sun.identity.cli.IOutput) BufferedReader(java.io.BufferedReader) CLIException(com.sun.identity.cli.CLIException) DirectoryServerVendor(com.sun.identity.sm.DirectoryServerVendor) LoginException(javax.security.auth.login.LoginException) LdapException(org.forgerock.opendj.ldap.LdapException) InvalidAuthContextException(com.sun.identity.authentication.internal.InvalidAuthContextException)

Example 44 with Connection

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

the class AddAMSDKIdRepoPlugin method loadLDIFs.

private void loadLDIFs() throws Exception {
    CommandManager mgr = getCommandManager();
    List ldifs = getLDIFs();
    for (Iterator i = directoryServers.iterator(); i.hasNext(); ) {
        String dshost = (String) i.next();
        try (ConnectionFactory factory = getLDAPConnection(new DSEntry(dshost));
            Connection ld = factory.getConnection()) {
            String dbName = LDAPUtils.getDBName(basedn, ld);
            for (Iterator j = ldifs.iterator(); j.hasNext(); ) {
                String file = (String) j.next();
                String content = CLIUtil.getFileContent(mgr, file);
                String swapped = tagswap(content, dbName);
                loadLDIF(ld, swapped);
            }
        }
    }
}
Also used : ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) CommandManager(com.sun.identity.cli.CommandManager) Iterator(java.util.Iterator) Connection(org.forgerock.opendj.ldap.Connection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 45 with Connection

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

the class AMCertStore method getCertificate.

/**
     * Return matched certificate from ldap certificate store 
     */
public X509Certificate getCertificate() {
    /*
         * Lookup the certificate in the LDAP certificate
         * directory and compare the values.
         */
    try (Connection ldc = getConnection()) {
        if (ldc == null) {
            return null;
        }
        ConnectionEntryReader results = getSearchResults(ldc, USERCERTIFICATE, USERCERTIFICATE_BINARY, CACERTIFICATE, CACERTIFICATE_BINARY);
        while (results != null && results.hasNext()) {
            // "Found search results for: " + cn , 2);
            if (results.isEntry()) {
                SearchResultEntry entry = results.readEntry();
                /*
                     * Retrieve the certificate from the store
                     */
                Attribute certAttribute = entry.getAttribute(USERCERTIFICATE);
                if (certAttribute == null) {
                    certAttribute = entry.getAttribute(USERCERTIFICATE_BINARY);
                    if (certAttribute == null) {
                        // an end-entity certificate can be a CA certificate
                        certAttribute = entry.getAttribute(CACERTIFICATE);
                        if (certAttribute == null) {
                            certAttribute = entry.getAttribute(CACERTIFICATE_BINARY);
                        }
                        if (certAttribute == null) {
                            debug.message("AMCertStore.getCertificate: Certificate - get usercertificate is null ");
                            continue;
                        }
                    }
                }
                for (ByteString value : certAttribute) {
                    byte[] bytes = value.toByteArray();
                    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
                    X509Certificate c = null;
                    try {
                        c = (X509Certificate) cf.generateCertificate(bis);
                    } catch (CertificateParsingException e) {
                        debug.error("AMCertStore.getCertificate : " + "Error in Certificate parsing : ", e);
                    }
                    if (c != null) {
                        return c;
                    }
                }
            // inner while
            } else {
                SearchResultReference reference = results.readReference();
                debug.warning("Got an LDAP reference - only expected entries. Ignoring: {}", reference);
            }
        }
    // outer while  
    } catch (Exception e) {
        debug.error("AMCertStore.getCertificate : " + "Certificate - Error finding registered certificate = ", e);
    }
    return null;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) CertificateParsingException(java.security.cert.CertificateParsingException) Attribute(org.forgerock.opendj.ldap.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteString(org.forgerock.opendj.ldap.ByteString) Connection(org.forgerock.opendj.ldap.Connection) SearchResultReference(org.forgerock.opendj.ldap.responses.SearchResultReference) X509Certificate(java.security.cert.X509Certificate) CertificateParsingException(java.security.cert.CertificateParsingException) LdapException(org.forgerock.opendj.ldap.LdapException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Aggregations

Connection (org.forgerock.opendj.ldap.Connection)94 LdapException (org.forgerock.opendj.ldap.LdapException)72 ByteString (org.forgerock.opendj.ldap.ByteString)47 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)46 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)39 ResultCode (org.forgerock.opendj.ldap.ResultCode)29 Attribute (org.forgerock.opendj.ldap.Attribute)27 HashSet (java.util.HashSet)26 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)20 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)19 IOException (java.io.IOException)18 SSOException (com.iplanet.sso.SSOException)15 PolicyException (com.sun.identity.policy.PolicyException)14 SMSException (com.sun.identity.sm.SMSException)13 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)13 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)12 BindResult (org.forgerock.opendj.ldap.responses.BindResult)12 DN (org.forgerock.opendj.ldap.DN)11 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)10 InvalidNameException (com.sun.identity.policy.InvalidNameException)10