Search in sources :

Example 26 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.

the class LdapProvisioning method ldapAuthenticate.

/*
     * search for the auth DN for the user, authneticate to the result DN
     */
private void ldapAuthenticate(String[] url, boolean wantStartTLS, String password, String searchBase, String searchFilter, String searchDn, String searchPassword) throws ServiceException {
    if (password == null || password.equals("")) {
        throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("empty password");
    }
    ExternalLdapConfig config = new ExternalLdapConfig(url, wantStartTLS, null, searchDn, searchPassword, null, "external LDAP auth");
    String resultDn = null;
    String tooMany = null;
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getExternalContext(config, LdapUsage.LDAP_AUTH_EXTERNAL);
        ZSearchResultEnumeration ne = zlc.searchDir(searchBase, filterFactory.fromFilterString(FilterId.LDAP_AUTHENTICATE, searchFilter), ZSearchControls.SEARCH_CTLS_SUBTREE());
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            if (resultDn == null) {
                resultDn = sr.getDN();
            } else {
                tooMany = sr.getDN();
                break;
            }
        }
        ne.close();
    } finally {
        LdapClient.closeContext(zlc);
    }
    if (tooMany != null) {
        ZimbraLog.account.warn(String.format("ldapAuthenticate searchFilter returned more then one result: (dn1=%s, dn2=%s, filter=%s)", resultDn, tooMany, searchFilter));
        throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("too many results from search filter!");
    } else if (resultDn == null) {
        throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("empty search");
    }
    if (ZimbraLog.account.isDebugEnabled())
        ZimbraLog.account.debug("search filter matched: " + resultDn);
    ldapAuthenticate(url, wantStartTLS, resultDn, password);
}
Also used : ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 27 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.

the class LdapProvisioning method getDistributionListByQuery.

private DistributionList getDistributionListByQuery(String base, ZLdapFilter filter, ZLdapContext initZlc, boolean basicAttrsOnly) throws ServiceException {
    String[] returnAttrs = basicAttrsOnly ? BASIC_DL_ATTRS : null;
    DistributionList dl = null;
    try {
        ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
        ZSearchResultEnumeration ne = helper.searchDir(base, filter, searchControls, initZlc, LdapServerType.REPLICA);
        if (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            dl = makeDistributionList(sr.getDN(), sr.getAttributes(), basicAttrsOnly);
        }
        ne.close();
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to lookup distribution list via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
    }
    return dl;
}
Also used : ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) DistributionList(com.zimbra.cs.account.DistributionList) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Aggregations

ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)27 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)25 ServiceException (com.zimbra.common.service.ServiceException)15 AccountServiceException (com.zimbra.cs.account.AccountServiceException)14 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)14 ArrayList (java.util.ArrayList)14 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)11 ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)11 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)4 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)3 Account (com.zimbra.cs.account.Account)2 Cos (com.zimbra.cs.account.Cos)2 GuestAccount (com.zimbra.cs.account.GuestAccount)2 LdapDIT (com.zimbra.cs.account.ldap.LdapDIT)2 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)2 LdapCos (com.zimbra.cs.account.ldap.entry.LdapCos)2 LdapMimeType (com.zimbra.cs.account.ldap.entry.LdapMimeType)2 MimeTypeInfo (com.zimbra.cs.mime.MimeTypeInfo)2 AlwaysOnCluster (com.zimbra.cs.account.AlwaysOnCluster)1 DataSource (com.zimbra.cs.account.DataSource)1