Search in sources :

Example 61 with ZLdapContext

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

the class LdapGalSearch method searchLdapGal.

private static void searchLdapGal(GalParams.ExternalGalParams galParams, GalOp galOp, String query, int maxResults, LdapGalMapRules rules, String token, SearchGalResult result) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        LdapGalCredential credential = galParams.credential();
        ExternalLdapConfig ldapConfig = new ExternalLdapConfig(galParams.url(), galParams.requireStartTLS(), credential.getAuthMech(), credential.getBindDn(), credential.getBindPassword(), rules.getBinaryLdapAttrs(), "external GAL");
        zlc = LdapClient.getExternalContext(ldapConfig, LdapUsage.fromGalOpLegacy(galOp));
        searchGal(zlc, GalSearchConfig.GalType.ldap, galParams.pageSize(), galParams.searchBase(), query, maxResults, rules, token, result);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig)

Example 62 with ZLdapContext

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

the class LdapGalSearch method doGalSearch.

private static void doGalSearch(GalSearchParams params) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        GalSearchConfig cfg = params.getConfig();
        GalSearchConfig.GalType galType = params.getConfig().getGalType();
        if (galType == GalSearchConfig.GalType.zimbra) {
            zlc = LdapClient.getContext(LdapUsage.fromGalOp(params.getOp()));
        } else {
            ExternalLdapConfig ldapConfig = new ExternalLdapConfig(cfg.getUrl(), cfg.getStartTlsEnabled(), cfg.getAuthMech(), cfg.getBindDn(), cfg.getBindPassword(), cfg.getRules().getBinaryLdapAttrs(), "external GAL");
            zlc = LdapClient.getExternalContext(ldapConfig, LdapUsage.fromGalOp(params.getOp()));
        }
        String fetchEntryByDn = params.getSearchEntryByDn();
        if (fetchEntryByDn == null) {
            SearchGalResult sgr = params.getResult();
            if (sgr != null && GalOp.sync.equals(params.getOp())) {
                sgr.setLdapTimeStamp(params.getLdapTimeStamp());
                sgr.setLdapMatchCount(params.getLdapMatchCount());
                sgr.setHadMore(params.ldapHasMore());
                sgr.setMaxLdapTimeStamp(params.getMaxLdapTimeStamp());
            }
            if (params.isExpandQuery()) {
                searchGal(zlc, galType, cfg.getPageSize(), cfg.getSearchBase(), params.generateLdapQuery(), params.getLimit(), cfg.getRules(), params.getSyncToken(), params.getResult(), params.getOp());
            } else {
                searchGal(zlc, galType, cfg.getPageSize(), cfg.getSearchBase(), params.getQuery(), params.getLimit(), cfg.getRules(), params.getSyncToken(), params.getResult(), params.getOp());
            }
        } else {
            getGalEntryByDn(zlc, galType, fetchEntryByDn, cfg.getRules(), params.getResult());
        }
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) GalSearchConfig(com.zimbra.cs.gal.GalSearchConfig) SearchGalResult(com.zimbra.cs.account.Provisioning.SearchGalResult)

Example 63 with ZLdapContext

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

the class LdapHelper method countEntries.

public long countEntries(String baseDN, ZLdapFilter filter, ZSearchControls searchControls, ZLdapContext initZlc, LdapServerType ldapServerType) throws ServiceException {
    boolean noopSearchSupported = !InMemoryLdapServer.isOn() && DebugConfig.ldapNoopSearchSupported;
    if (noopSearchSupported) {
        return countEntriesByNoopSearch(baseDN, filter, searchControls, initZlc, ldapServerType);
    } else {
        CountObjectsVisitor visitor = new CountObjectsVisitor();
        SearchLdapOptions searchOptions = new SearchLdapOptions(baseDN, filter, null, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
        ZLdapContext zlc = initZlc;
        try {
            if (zlc == null) {
                zlc = LdapClient.getContext(ldapServerType, LdapUsage.SEARCH);
            }
            zlc.searchPaged(searchOptions);
        } finally {
            if (initZlc == null) {
                LdapClient.closeContext(zlc);
            }
        }
        return visitor.getCount();
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions)

Example 64 with ZLdapContext

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

the class ADGroupHandler method getDelegatedAdminGroups.

private List<String> getDelegatedAdminGroups(Account acct, boolean asAdmin) throws ServiceException {
    LdapProv prov = LdapProv.getInst();
    Domain domain = prov.getDomain(acct);
    if (domain == null) {
        throw ServiceException.FAILURE("unable to get domain for account " + acct.getName(), null);
    }
    // try explicit external DN on account first
    String extDN = acct.getAuthLdapExternalDn();
    if (extDN == null) {
        // then try bind DN template on domain
        // note: for AD auth, zimbraAuthLdapSearchFilter is not used, so we
        // skip that. See LdapProvisioning.externalLdapAuth
        String dnTemplate = domain.getAuthLdapBindDn();
        if (dnTemplate != null) {
            extDN = LdapUtil.computeDn(acct.getName(), dnTemplate);
        }
    }
    if (extDN == null) {
        throw ServiceException.FAILURE("unable to get external DN for account " + acct.getName(), null);
    }
    ZLdapContext zlc = null;
    try {
        zlc = getExternalDelegatedAdminGroupsLdapContext(domain, asAdmin);
        ZAttributes attrs = prov.getHelper().getAttributes(zlc, extDN, new String[] { MEMBER_OF_ATTR });
        return attrs.getMultiAttrStringAsList(MEMBER_OF_ATTR, CheckBinary.NOCHECK);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZAttributes(com.zimbra.cs.ldap.ZAttributes) Domain(com.zimbra.cs.account.Domain) LdapProv(com.zimbra.cs.account.ldap.LdapProv)

Example 65 with ZLdapContext

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

the class TestLdapProvDomain method verifyAllDomains.

private void verifyAllDomains(List<Domain> allDomains) throws Exception {
    // domains created by r-t-w
    // TODO: this verification is very fragile
    Set<String> expectedDomains = new HashSet<String>();
    String defaultDomainName = prov.getInstance().getConfig().getDefaultDomainName();
    expectedDomains.add(defaultDomainName);
    expectedDomains.add("example.com");
    assertEquals(expectedDomains.size(), allDomains.size());
    for (Domain domain : allDomains) {
        assertTrue(expectedDomains.contains(domain.getName()));
    }
    // 
    // another verification
    // 
    LdapHelper ldapHelper = ((LdapProv) prov).getHelper();
    final List<String> /* zimbraId */
    domainIds = new ArrayList<String>();
    SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor() {

        @Override
        public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) {
            try {
                domainIds.add(ldapAttrs.getAttrString(Provisioning.A_zimbraId));
            } catch (ServiceException e) {
                fail();
            }
        }
    };
    SearchLdapOptions searchOpts = new SearchLdapOptions(LdapConstants.DN_ROOT_DSE, ZLdapFilterFactory.getInstance().fromFilterString(FilterId.UNITTEST, "(objectclass=zimbraDomain)"), new String[] { Provisioning.A_zimbraId }, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapUsage.UNITTEST);
        ldapHelper.searchLdap(zlc, searchOpts);
    } finally {
        LdapClient.closeContext(zlc);
    }
    assertEquals(domainIds.size(), allDomains.size());
    for (Domain domain : allDomains) {
        assertTrue(domainIds.contains(domain.getId()));
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) LdapHelper(com.zimbra.cs.account.ldap.LdapHelper) ArrayList(java.util.ArrayList) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions) LdapProv(com.zimbra.cs.account.ldap.LdapProv) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) IAttributes(com.zimbra.cs.ldap.IAttributes) Domain(com.zimbra.cs.account.Domain) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)126 ServiceException (com.zimbra.common.service.ServiceException)65 AccountServiceException (com.zimbra.cs.account.AccountServiceException)62 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)60 LdapEntryAlreadyExistException (com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException)23 LdapException (com.zimbra.cs.ldap.LdapException)22 ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)21 Domain (com.zimbra.cs.account.Domain)19 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)18 CallbackContext (com.zimbra.cs.account.callback.CallbackContext)16 Date (java.util.Date)16 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)14 HashMap (java.util.HashMap)14 SearchLdapOptions (com.zimbra.cs.ldap.SearchLdapOptions)13 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)12 Account (com.zimbra.cs.account.Account)11 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)11 ZAttributes (com.zimbra.cs.ldap.ZAttributes)10 HashSet (java.util.HashSet)10 GuestAccount (com.zimbra.cs.account.GuestAccount)9