Search in sources :

Example 71 with ZLdapContext

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

the class TestLdapZLdapContext method searchPaged.

@Test
public void searchPaged() throws Exception {
    int SIZE_LIMIT = 5;
    String base = LdapConstants.DN_ROOT_DSE;
    ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
    String[] returnAttrs = new String[] { "objectClass" };
    final List<String> result = new ArrayList<String>();
    SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor() {

        @Override
        public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) {
            result.add(dn);
        }
    };
    SearchLdapOptions searchOptions = new SearchLdapOptions(base, filter, returnAttrs, SIZE_LIMIT, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
    boolean caughtException = false;
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapUsage.UNITTEST);
        zlc.searchPaged(searchOptions);
    } catch (LdapSizeLimitExceededException e) {
        caughtException = true;
    } finally {
        LdapClient.closeContext(zlc);
    }
    assertTrue(caughtException);
    assertEquals(SIZE_LIMIT, result.size());
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ArrayList(java.util.ArrayList) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions) ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) IAttributes(com.zimbra.cs.ldap.IAttributes) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) Map(java.util.Map)

Example 72 with ZLdapContext

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

the class TestLdapZLdapContext method searchDir.

@Test
public void searchDir() throws Exception {
    int SIZE_LIMIT = 5;
    String base = LdapConstants.DN_ROOT_DSE;
    ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
    String[] returnAttrs = new String[] { "objectClass" };
    ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, SIZE_LIMIT, returnAttrs);
    int numFound = 0;
    boolean caughtException = false;
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapUsage.UNITTEST);
        ZSearchResultEnumeration ne = zlc.searchDir(base, filter, searchControls);
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            numFound++;
        }
        ne.close();
    } catch (LdapSizeLimitExceededException e) {
        caughtException = true;
    } finally {
        LdapClient.closeContext(zlc);
    }
    assertTrue(caughtException);
/*
        // unboundid does not return entries if LdapSizeLimitExceededException
        // is thrown,  See commons on ZLdapContext.searchDir().
        if (testConfig != TestLdap.TestConfig.UBID) {
            assertEquals(SIZE_LIMIT, numFound);
        }
        */
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 73 with ZLdapContext

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

the class LdapProvisioning method deleteHabOrgUnit.

@Override
public void deleteHabOrgUnit(Domain domain, String habOrgUnitName) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        String domainDn = ((LdapEntry) domain).getDN();
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_OU);
        if (isEmptyOu(habOrgUnitName, domainDn)) {
            zlc.deleteEntry(createOuDn(habOrgUnitName, domainDn));
        } else {
            throw ServiceException.FAILURE(String.format("HabOrgUnit: %s" + " of doamin:%s  is not empty", habOrgUnitName, domainDn), null);
        }
    } catch (ServiceException e) {
        throw ServiceException.FAILURE(String.format("Unable to delete HAB org unit: %s for domain=%s", habOrgUnitName, domain.getName()), e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry)

Example 74 with ZLdapContext

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

the class LdapProvisioning method createSignature.

private Signature createSignature(Account account, String signatureName, Map<String, Object> signatureAttrs, boolean restoring) throws ServiceException {
    signatureName = signatureName.trim();
    removeAttrIgnoreCase("objectclass", signatureAttrs);
    validateSignatureAttrs(signatureAttrs);
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    /*
         * check if the signature name already exists
         *
         * We check if the signatureName is the same as the signature on the account.
         * For signatures that are in the signature LDAP entries, JNDI will throw
         * NameAlreadyBoundException for duplicate names.
         *
         */
    Signature acctSig = LdapSignature.getAccountSignature(this, account);
    if (acctSig != null && signatureName.equalsIgnoreCase(acctSig.getName()))
        throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
    boolean setAsDefault = false;
    List<Signature> existing = getAllSignatures(account);
    // If the signature id is supplied with the request, check that it
    // is not associated with an existing signature
    String signatureId = (String) signatureAttrs.get(Provisioning.A_zimbraSignatureId);
    if (signatureId != null) {
        for (Signature signature : existing) {
            if (signatureId.equals(signature.getAttr(Provisioning.A_zimbraSignatureId))) {
                throw AccountServiceException.SIGNATURE_EXISTS(signatureId);
            }
        }
    }
    int numSigs = existing.size();
    if (numSigs >= account.getLongAttr(A_zimbraSignatureMaxNumEntries, 20))
        throw AccountServiceException.TOO_MANY_SIGNATURES();
    else if (numSigs == 0)
        setAsDefault = true;
    account.setCachedData(SIGNATURE_LIST_CACHE_KEY, null);
    boolean checkImmutable = !restoring;
    CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
    callbackContext.setData(DataKey.MAX_SIGNATURE_LEN, String.valueOf(account.getMailSignatureMaxLength()));
    AttributeManager.getInstance().preModify(signatureAttrs, null, callbackContext, checkImmutable);
    if (signatureId == null) {
        signatureId = LdapUtil.generateUUID();
        signatureAttrs.put(Provisioning.A_zimbraSignatureId, signatureId);
    }
    if (acctSig == null) {
        // the slot on the account is not occupied, use it
        signatureAttrs.put(Provisioning.A_zimbraSignatureName, signatureName);
        // pass in setAsDefault as an optimization, since we are updating the account
        // entry, we can update the default attr in one LDAP write
        LdapSignature.createAccountSignature(this, account, signatureAttrs, setAsDefault);
        return LdapSignature.getAccountSignature(this, account);
    }
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SIGNATURE);
        String dn = getSignatureDn(ldapEntry, signatureName);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.mapToAttrs(signatureAttrs);
        entry.setAttr(A_objectClass, "zimbraSignature");
        entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
        entry.setDN(dn);
        zlc.createEntry(entry);
        Signature signature = getSignatureById(account, ldapEntry, signatureId, zlc);
        AttributeManager.getInstance().postModify(signatureAttrs, signature, callbackContext);
        if (setAsDefault)
            setDefaultSignature(account, signatureId);
        return signature;
    } catch (LdapEntryAlreadyExistException nabe) {
        throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
    } catch (LdapException e) {
        throw e;
    } catch (AccountServiceException e) {
        throw e;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to create signature: " + signatureName, e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) Date(java.util.Date) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) Signature(com.zimbra.cs.account.Signature) LdapSignature(com.zimbra.cs.account.ldap.entry.LdapSignature) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) LdapException(com.zimbra.cs.ldap.LdapException)

Example 75 with ZLdapContext

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

the class LdapProvisioning method searchContainingDynamicGroupIdsForExternalAddress.

/*
     * returns zimbraId of dynamic groups containing addr as an external member.
     */
private Set<String> searchContainingDynamicGroupIdsForExternalAddress(String addr, ZLdapContext initZlc) {
    final Set<String> groupIds = Sets.newHashSet();
    SearchLdapVisitor visitor = new SearchLdapVisitor(false) {

        @Override
        public void visit(String dn, IAttributes ldapAttrs) throws StopIteratingException {
            String groupId = null;
            try {
                groupId = ldapAttrs.getAttrString(A_zimbraGroupId);
            } catch (ServiceException e) {
                ZimbraLog.account.warn("unable to get attr", e);
            }
            if (groupId != null) {
                groupIds.add(groupId);
            }
        }
    };
    ZLdapContext zlc = initZlc;
    try {
        if (zlc == null) {
            zlc = LdapClient.getContext(LdapServerType.REPLICA, LdapUsage.SEARCH);
        }
        String base = mDIT.mailBranchBaseDN();
        ZLdapFilter filter = filterFactory.dynamicGroupsStaticUnitByMemberAddr(addr);
        SearchLdapOptions searchOptions = new SearchLdapOptions(base, filter, new String[] { A_zimbraGroupId }, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
        zlc.searchPaged(searchOptions);
    } catch (ServiceException e) {
        ZimbraLog.account.warn("unable to search dynamic groups for guest acct", e);
    } finally {
        if (initZlc == null) {
            LdapClient.closeContext(zlc);
        }
    }
    return groupIds;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) SearchLdapVisitor(com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) IAttributes(com.zimbra.cs.ldap.IAttributes) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions)

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