Search in sources :

Example 6 with ResultCode

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

the class SMSEmbeddedLdapObject method internalCreate.

/**
     * Create an entry in the directory using the principal name
     */
private void internalCreate(SSOToken token, String dn, Map attrs) throws SMSException, SSOException {
    SMSAuditor auditor = newAuditor(token, dn, null);
    List attrList = copyMapToAttrList(attrs);
    AddOperation ao = icConn.processAdd(dn, attrList);
    ResultCode resultCode = ao.getResultCode();
    if (resultCode == ResultCode.SUCCESS) {
        if (debug.messageEnabled()) {
            debug.message("SMSEmbeddedLdapObject.create: Successfully created" + " entry: " + dn);
        }
        if (auditor != null) {
            auditor.auditCreate(attrs);
        }
    } else if (resultCode == ResultCode.ENTRY_ALREADY_EXISTS) {
        // During install time and other times,
        // this error gets throws due to unknown issue. Issue:
        // Hence mask it.
        debug.warning("SMSEmbeddedLdapObject.create: Entry " + "Already Exists Error for DN" + dn);
    } else {
        debug.error("SMSEmbeddedLdapObject.create: Error creating entry: " + dn + ", error code = " + resultCode);
        throw new SMSException("", "sms-entry-cannot-create");
    }
}
Also used : SMSAuditor(org.forgerock.openam.auditors.SMSAuditor) SMSException(com.sun.identity.sm.SMSException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AddOperation(org.opends.server.core.AddOperation) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 7 with ResultCode

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

the class SearchResultIterator method hasNext.

public boolean hasNext() {
    try {
        if (results.hasNext()) {
            if (current == null) {
                if (results.isReference()) {
                    debug.warning("SearchResultIterator: ignoring reference: {}", results.readReference());
                    return hasNext();
                }
                SearchResultEntry entry = results.readEntry();
                String dn = entry.getName().toString();
                if (hasExcludeDNs && excludeDNs.contains(dn)) {
                    return hasNext();
                }
                current = new SMSDataEntry(dn, SMSUtils.convertEntryToAttributesMap(entry));
            }
            return true;
        }
    } catch (LdapException e) {
        ResultCode errorCode = e.getResult().getResultCode();
        if (errorCode.equals(ResultCode.SIZE_LIMIT_EXCEEDED)) {
            debug.message("SearchResultIterator: size limit exceeded");
        } else {
            debug.error("SearchResultIterator.hasNext", e);
        }
    } catch (SearchResultReferenceIOException e) {
        debug.error("SearchResultIterator.hasNext: reference should be already handled", e);
        return hasNext();
    }
    conn.close();
    return false;
}
Also used : SMSDataEntry(com.sun.identity.sm.SMSDataEntry) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 8 with ResultCode

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

the class LDAPUsers method handleResultException.

private PolicyException handleResultException(LdapException e) {
    ResultCode ldapErrorCode = e.getResult().getResultCode();
    if (ldapErrorCode.equals(ResultCode.INVALID_CREDENTIALS)) {
        return new PolicyException(ResBundleUtils.rbName, "ldap_invalid_password", null, null);
    } else if (ldapErrorCode.equals(ResultCode.NO_SUCH_OBJECT)) {
        String[] objs = { baseDN };
        return new PolicyException(ResBundleUtils.rbName, "no_such_ldap_users_base_dn", objs, null);
    }
    String errorMsg = e.getResult().getDiagnosticMessage();
    String additionalMsg = e.getMessage();
    if (additionalMsg != null) {
        return new PolicyException(errorMsg + ": " + additionalMsg);
    } else {
        return new PolicyException(errorMsg);
    }
}
Also used : PolicyException(com.sun.identity.policy.PolicyException) ByteString(org.forgerock.opendj.ldap.ByteString) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 9 with ResultCode

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

the class LDAPUsers method getUserDN.

/**
     * Gets the DN for a user identified 
     * by the token. If the Directory server is locally installed to speed
     * up the search, no directoty search is performed and the DN obtained
     * from the token is returned. If the directory is remote
     * a LDAP search is performed to get the user DN.
     */
private DN getUserDN(SSOToken token) throws SSOException, PolicyException {
    Set<String> qualifiedUserDNs = new HashSet<>();
    String userLocalDN = token.getPrincipal().getName();
    DN userDN = null;
    if (localDS && !PolicyUtils.principalNameEqualsUuid(token)) {
        userDN = DN.valueOf(userLocalDN);
    } else {
        // try to figure out the user name from the local user DN
        int beginIndex = userLocalDN.indexOf("=");
        int endIndex = userLocalDN.indexOf(",");
        if ((beginIndex <= 0) || (endIndex <= 0) || (beginIndex >= endIndex)) {
            throw (new PolicyException(ResBundleUtils.rbName, "ldapusers_subject_invalid_local_user_dn", null, null));
        }
        String userName = userLocalDN.substring(beginIndex + 1, endIndex);
        String searchFilter = null;
        if ((userSearchFilter != null) && !(userSearchFilter.length() == 0)) {
            searchFilter = "(&" + userSearchFilter + PolicyUtils.constructUserFilter(token, userRDNAttrName, userName, aliasEnabled) + ")";
        } else {
            searchFilter = PolicyUtils.constructUserFilter(token, userRDNAttrName, userName, aliasEnabled);
        }
        if (debug.messageEnabled()) {
            debug.message("LDAPUsers.getUserDN(): search filter is: " + searchFilter);
        }
        String[] attrs = { userRDNAttrName };
        // search the remote ldap and find out the user DN
        try (Connection ld = connPool.getConnection()) {
            ConnectionEntryReader res = search(searchFilter, ld, attrs);
            while (res.hasNext()) {
                try {
                    SearchResultEntry entry = res.readEntry();
                    qualifiedUserDNs.add(entry.getName().toString());
                } catch (SearchResultReferenceIOException e) {
                    // ignore referrals
                    continue;
                } catch (LdapException e) {
                    String[] objs = { orgName };
                    ResultCode resultCode = e.getResult().getResultCode();
                    if (resultCode.equals(ResultCode.SIZE_LIMIT_EXCEEDED)) {
                        debug.warning("LDAPUsers.getUserDN(): exceeded the size limit");
                        throw new PolicyException(ResBundleUtils.rbName, "ldap_search_exceed_size_limit", objs, null);
                    } else if (resultCode.equals(ResultCode.TIME_LIMIT_EXCEEDED)) {
                        debug.warning("LDAPUsers.getUserDN(): exceeded the time limit");
                        throw new PolicyException(ResBundleUtils.rbName, "ldap_search_exceed_time_limit", objs, null);
                    } else {
                        throw new PolicyException(e);
                    }
                }
            }
        } catch (LdapException e) {
            throw handleResultException(e);
        } catch (Exception e) {
            throw new PolicyException(e);
        }
        // check if the user belongs to any of the selected users
        if (qualifiedUserDNs.size() > 0) {
            debug.message("LDAPUsers.getUserDN(): qualified users={}", qualifiedUserDNs);
            Iterator<String> iter = qualifiedUserDNs.iterator();
            // we only take the first qualified DN
            userDN = DN.valueOf(iter.next());
        }
    }
    return userDN;
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) DN(org.forgerock.opendj.ldap.DN) ByteString(org.forgerock.opendj.ldap.ByteString) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) LdapException(org.forgerock.opendj.ldap.LdapException) NameNotFoundException(com.sun.identity.policy.NameNotFoundException) PolicyException(com.sun.identity.policy.PolicyException) InvalidNameException(com.sun.identity.policy.InvalidNameException) SSOException(com.iplanet.sso.SSOException) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) PolicyException(com.sun.identity.policy.PolicyException) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) HashSet(java.util.HashSet) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 10 with ResultCode

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

the class LDAPUsers method getValidEntries.

/**
     * Returns a set of possible values that satisfy the <code>pattern</code>.
     * The returned <code>ValidValues</code> object contains a set of
     * map of user DN to a map of user's attribute name to a string array of
     * attribute values.
     *
     * @param token Single Sign On token for fetching the possible values.
     * @param pattern Search pattern of which possible values are matched to.
     * @param attributeNames Array of attribute names to be to returned.
     * @return a set of possible values that satify the <code>pattern</code>.
     * @throws SSOException if <code>SSOToken</code> is invalid.
     * @throws PolicyException if there are problems getting these values.
     */
public ValidValues getValidEntries(SSOToken token, String pattern, String[] attributeNames) throws SSOException, PolicyException {
    if (!initialized) {
        throw (new PolicyException(ResBundleUtils.rbName, "ldapusers_subject_not_yet_initialized", null, null));
    }
    Set<Map<String, Map<String, String[]>>> results = new HashSet<>();
    String searchFilter = getSearchFilter(pattern);
    int status = ValidValues.SUCCESS;
    try (Connection ld = connPool.getConnection()) {
        ConnectionEntryReader res = search(searchFilter, ld, attributeNames);
        Map<String, Map<String, String[]>> map = new HashMap<>();
        results.add(map);
        while (res.hasNext()) {
            try {
                SearchResultEntry entry = res.readEntry();
                if (entry != null) {
                    String userDN = entry.getName().toString();
                    map.put(userDN, getUserAttributeValues(entry, attributeNames));
                }
            } catch (SearchResultReferenceIOException lre) {
                // ignore referrals
                continue;
            } catch (LdapException e) {
                ResultCode resultCode = e.getResult().getResultCode();
                if (resultCode.equals(ResultCode.SIZE_LIMIT_EXCEEDED)) {
                    debug.warning("LDAPUsers.getValidEntries(): exceeded the size limit");
                    status = ValidValues.SIZE_LIMIT_EXCEEDED;
                } else if (resultCode.equals(ResultCode.TIME_LIMIT_EXCEEDED)) {
                    debug.warning("LDAPUsers.getValidEntries(): exceeded the time limit");
                    status = ValidValues.TIME_LIMIT_EXCEEDED;
                } else {
                    throw new PolicyException(e);
                }
            }
        }
    } catch (LdapException e) {
        throw handleResultException(e);
    } catch (Exception e) {
        throw new PolicyException(e);
    }
    return new ValidValues(status, results);
}
Also used : HashMap(java.util.HashMap) ValidValues(com.sun.identity.policy.ValidValues) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) LdapException(org.forgerock.opendj.ldap.LdapException) NameNotFoundException(com.sun.identity.policy.NameNotFoundException) PolicyException(com.sun.identity.policy.PolicyException) InvalidNameException(com.sun.identity.policy.InvalidNameException) SSOException(com.iplanet.sso.SSOException) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) PolicyException(com.sun.identity.policy.PolicyException) HashMap(java.util.HashMap) Map(java.util.Map) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) HashSet(java.util.HashSet) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Aggregations

ResultCode (org.forgerock.opendj.ldap.ResultCode)49 LdapException (org.forgerock.opendj.ldap.LdapException)37 Connection (org.forgerock.opendj.ldap.Connection)29 ByteString (org.forgerock.opendj.ldap.ByteString)18 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)18 SMSException (com.sun.identity.sm.SMSException)17 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)17 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)15 HashSet (java.util.HashSet)14 PolicyException (com.sun.identity.policy.PolicyException)13 SSOException (com.iplanet.sso.SSOException)9 InvalidNameException (com.sun.identity.policy.InvalidNameException)9 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)9 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)7 ValidValues (com.sun.identity.policy.ValidValues)6 LinkedHashSet (java.util.LinkedHashSet)6 InternalSearchOperation (org.opends.server.protocols.internal.InternalSearchOperation)6 AMException (com.iplanet.am.sdk.AMException)4 AMSearchResults (com.iplanet.am.sdk.AMSearchResults)4 IOException (java.io.IOException)4