Search in sources :

Example 16 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project openmeetings by apache.

the class LdapLoginManager method importUsers.

public void importUsers(Long domainId, boolean print) throws OmException {
    try (LdapWorker w = new LdapWorker(domainId)) {
        bindAdmin(w.conn, w.options);
        Dn baseDn = new Dn(w.options.searchBase);
        try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(w.options.importQuery).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
            while (cursor.next()) {
                try {
                    Entry e = cursor.get();
                    User u = userDao.getByLogin(getLogin(w.config, e), Type.ldap, domainId);
                    u = w.getUser(e, u);
                    if (print) {
                        log.info("Going to import user: {}", u);
                    } else {
                        userDao.update(u, null);
                        log.info("User {}, was imported", u);
                    }
                } catch (CursorLdapReferralException cle) {
                    log.warn("Referral LDAP entry found, ignore it");
                }
            }
        }
    } catch (LdapAuthenticationException ae) {
        log.error("Not authenticated.", ae);
        throw BAD_CREDENTIALS;
    } catch (OmException e) {
        throw e;
    } catch (Exception e) {
        log.error("Unexpected exception.", e);
        throw new OmException(e);
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) EntryCursorImpl(org.apache.directory.ldap.client.api.EntryCursorImpl) Entry(org.apache.directory.api.ldap.model.entry.Entry) User(org.apache.openmeetings.db.entity.user.User) GroupUser(org.apache.openmeetings.db.entity.user.GroupUser) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) OmException(org.apache.openmeetings.util.OmException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) OmException(org.apache.openmeetings.util.OmException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 17 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project account-identity by cryptofiat.

the class LdapService method lookupIdCode.

public LdapResponse lookupIdCode(long idCode) {
    LdapResponse lResponse = LdapResponse.builder().build();
    lResponse = tryLocalCache(idCode);
    if (lResponse != null && lResponse.getIdCode() > 0) {
        return lResponse;
    }
    LdapNetworkConnection connection = new LdapNetworkConnection("ldap.sk.ee");
    try {
        connection.bind();
        EntryCursor cursor = connection.search("c=EE", "(serialNumber=" + String.valueOf(idCode) + ")", SearchScope.SUBTREE, "*");
        while (cursor.next()) {
            Entry entry = cursor.get();
            log.info("got an entry: " + entry.toString());
            String cn = entry.get("cn").getString();
            lResponse = LdapResponse.builder().idCode(Long.valueOf(idCode)).firstName(cn.split(",")[1]).lastName(cn.split(",")[0]).build();
        }
        connection.unBind();
        connection.close();
    } catch (Exception e) {
        log.error("Exception trying LDAP " + e.toString());
    }
    if (lResponse != null && lResponse.getIdCode() > 0) {
        storeLocalCache(lResponse);
        return lResponse;
    } else {
        return null;
    }
}
Also used : LdapResponse(eu.cryptoeuro.accountmapper.response.LdapResponse) EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 18 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project directory-ldap-api by apache.

the class LdapNetworkConnection method fetchRootDSE.

/**
 * fetches the rootDSE from the server
 * @throws LdapException
 */
private void fetchRootDSE(String... explicitAttributes) throws LdapException {
    EntryCursor cursor = null;
    String[] attributes = explicitAttributes;
    if (attributes.length == 0) {
        attributes = new String[] { SchemaConstants.ALL_USER_ATTRIBUTES, SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES };
    }
    try {
        cursor = search("", LdapConstants.OBJECT_CLASS_STAR, SearchScope.OBJECT, attributes);
        if (cursor.next()) {
            rootDse = cursor.get();
        } else {
            throw new LdapException("Search for root DSE returned no entry");
        }
    } catch (Exception e) {
        String msg = "Failed to fetch the RootDSE";
        LOG.error(msg);
        throw new LdapException(msg, e);
    } finally {
        if (cursor != null) {
            try {
                cursor.close();
            } catch (Exception e) {
                LOG.error(I18n.err(I18n.ERR_03201_CURSOR_CLOSE_FAIL), e);
            }
        }
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 19 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project gpconnect-demonstrator by nhsconnect.

the class EndpointResolver method ldapQueryRequest.

private List<Collection<Attribute>> ldapQueryRequest(String queryBase, String queryFilter) throws IOException {
    String uuid = java.util.UUID.randomUUID().toString();
    List<Collection<Attribute>> returnList = new ArrayList<>();
    LdapNetworkConnection connection = null;
    LOG.debug(uuid + " ldapSDSQuery (Base:" + queryBase + " Filter:" + queryFilter + ")");
    try {
        connection = new LdapNetworkConnection(ldapUrl, ldapPort, ldapUseSSL);
        if (serverKeyManager == null && trustManager == null) {
            // Create Key Manager
            try (FileInputStream keystoreInputStream = new FileInputStream(configPath + keystoreFilename)) {
                KeyStore serverKeys = KeyStore.getInstance(keystoreType);
                serverKeys.load(keystoreInputStream, keystorePassword.toCharArray());
                serverKeyManager = KeyManagerFactory.getInstance("SunX509");
                serverKeyManager.init(serverKeys, keystorePassword.toCharArray());
            }
            // Create New Trust Store
            try (FileInputStream keystoreInputStream = new FileInputStream(configPath + keystoreFilename)) {
                KeyStore serverTrustStore = KeyStore.getInstance(keystoreType);
                serverTrustStore.load(keystoreInputStream, keystorePassword.toCharArray());
                trustManager = TrustManagerFactory.getInstance("SunX509");
                trustManager.init(serverTrustStore);
            }
        }
        // Set SSL Trust and Key stores in the config
        connection.getConfig().setKeyManagers(serverKeyManager.getKeyManagers());
        connection.getConfig().setTrustManagers(trustManager.getTrustManagers());
        connection.bind();
        EntryCursor cursor = connection.search(queryBase, queryFilter, SearchScope.SUBTREE);
        while (cursor.next()) {
            returnList.add(cursor.get().getAttributes());
            for (Attribute attribute : cursor.get().getAttributes()) {
                LOG.debug(attribute.getId() + ":" + attribute.getString());
            }
        }
        connection.unBind();
    } catch (Exception e) {
        LOG.error(uuid + " Error - " + e.getMessage());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return returnList;
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ArrayList(java.util.ArrayList) Collection(java.util.Collection) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Aggregations

EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)19 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)14 IOException (java.io.IOException)11 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)10 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)7 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)5 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)5 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)5 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)4 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)3 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)3 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)3 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)2 InternalServerError (io.kamax.mxisd.exception.InternalServerError)2 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)2 SearchRequestImpl (org.apache.directory.api.ldap.model.message.SearchRequestImpl)2