Search in sources :

Example 11 with SearchLdapOptions

use of com.zimbra.cs.ldap.SearchLdapOptions 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)

Example 12 with SearchLdapOptions

use of com.zimbra.cs.ldap.SearchLdapOptions 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 13 with SearchLdapOptions

use of com.zimbra.cs.ldap.SearchLdapOptions 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)

Example 14 with SearchLdapOptions

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

the class BUG_57866 method upgradeWikiAccounts.

private void upgradeWikiAccounts(ZLdapContext zlc) throws ServiceException {
    // global wiki account
    String acctName = prov.getConfig().getNotebookAccount();
    if (acctName != null) {
        printer.format("Checking global wiki account %s\n", acctName);
        Account acct = prov.get(AccountBy.name, acctName);
        setIsSystemAccount(zlc, acct);
    }
    // domain wiki accounts
    LdapDIT dit = prov.getDIT();
    String[] returnAttrs = new String[] { Provisioning.A_zimbraNotebookAccount };
    String base = dit.mailBranchBaseDN();
    String query = "(&(objectclass=zimbraDomain)(zimbraNotebookAccount=*))";
    final Set<String> wikiAcctNames = new HashSet<String>();
    SearchLdapVisitor visitor = new SearchLdapVisitor(false) {

        @Override
        public void visit(String dn, IAttributes ldapAttrs) throws StopIteratingException {
            try {
                String acctName;
                acctName = ldapAttrs.getAttrString(Provisioning.A_zimbraNotebookAccount);
                if (acctName != null) {
                    wikiAcctNames.add(acctName);
                }
            } catch (ServiceException e) {
                printer.printStackTrace("unsble to search domains for wiki accounts", e);
            }
        }
    };
    SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
    zlc.searchPaged(searchOpts);
    for (String wikiAcctName : wikiAcctNames) {
        printer.format("Checking domain wiki account %s\n", wikiAcctName);
        Account acct = prov.get(AccountBy.name, wikiAcctName);
        setIsSystemAccount(zlc, acct);
    }
}
Also used : Account(com.zimbra.cs.account.Account) LdapDIT(com.zimbra.cs.account.ldap.LdapDIT) SearchLdapVisitor(com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor) ServiceException(com.zimbra.common.service.ServiceException) IAttributes(com.zimbra.cs.ldap.IAttributes) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions) HashSet(java.util.HashSet)

Example 15 with SearchLdapOptions

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

the class BUG_22033 method doUpgrade.

@Override
void doUpgrade() throws ServiceException {
    LdapDIT dit = prov.getDIT();
    String base;
    String query;
    String[] returnAttrs = new String[] { Provisioning.A_objectClass, Provisioning.A_zimbraCreateTimestamp, "createTimestamp" };
    if (mType == null) {
        printer.println("Checking all objects\n");
        base = dit.zimbraBaseDN();
        query = "(|" + "(objectclass=zimbraAccount)" + "(objectclass=zimbraAlias)" + "(objectclass=zimbraCalendarResource)" + "(objectclass=zimbraGlobalConfig)" + "(objectclass=zimbraCOS)" + "(objectclass=zimbraDataSource)" + "(objectclass=zimbraDistributionList)" + "(objectclass=zimbraDomain)" + "(objectclass=zimbraIdentity)" + "(objectclass=zimbraServer)" + "(objectclass=zimbraSignature)" + "(objectclass=zimbraXMPPComponent)" + "(objectclass=zimbraZimletEntry)" + ")";
    } else {
        printer.println("Checking " + mType.name() + " objects...\n");
        switch(mType) {
            case account:
                base = dit.mailBranchBaseDN();
                query = "(&(objectclass=zimbraAccount)(!(objectclass=zimbraCalendarResource)))";
                break;
            case alias:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraAlias)";
                break;
            case calendarresource:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraCalendarResource)";
                break;
            case config:
                base = dit.configDN();
                query = "(objectclass=zimbraGlobalConfig)";
                break;
            case cos:
                base = dit.cosBaseDN();
                query = "(objectclass=zimbraCOS)";
                break;
            case datasource:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraDataSource)";
                break;
            case distributionlist:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraDistributionList)";
                break;
            case domain:
                base = dit.domainBaseDN();
                query = "(objectclass=zimbraDomain)";
                break;
            case identity:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraIdentity)";
                break;
            case server:
                base = dit.serverBaseDN();
                query = "(objectclass=zimbraServer)";
                break;
            case signature:
                base = dit.mailBranchBaseDN();
                query = "(objectclass=zimbraSignature)";
                break;
            case xmppcomponent:
                base = dit.xmppcomponentBaseDN();
                query = "(objectclass=zimbraXMPPComponent)";
                break;
            case zimlet:
                base = dit.zimletBaseDN();
                query = "(objectclass=zimbraZimletEntry)";
                break;
            default:
                throw ServiceException.FAILURE("", null);
        }
    }
    query = "(&" + "(!(zimbraCreateTimestamp=*))" + query + ")";
    ZLdapContext zlc = null;
    ZLdapContext modZlc = null;
    Bug22033Visitor visitor = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
        modZlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
        visitor = new Bug22033Visitor(this, modZlc);
        SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
        zlc.searchPaged(searchOpts);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to list all objects", e);
    } finally {
        LdapClient.closeContext(zlc);
        LdapClient.closeContext(modZlc);
        if (visitor != null) {
            printer.println("\nModified " + visitor.getNumModified() + " objects");
        }
    }
}
Also used : LdapDIT(com.zimbra.cs.account.ldap.LdapDIT) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ServiceException(com.zimbra.common.service.ServiceException) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions)

Aggregations

SearchLdapOptions (com.zimbra.cs.ldap.SearchLdapOptions)17 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)11 ServiceException (com.zimbra.common.service.ServiceException)9 IAttributes (com.zimbra.cs.ldap.IAttributes)6 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)5 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)5 AccountServiceException (com.zimbra.cs.account.AccountServiceException)4 LdapDIT (com.zimbra.cs.account.ldap.LdapDIT)4 SearchLdapVisitor (com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor)4 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Account (com.zimbra.cs.account.Account)2 LdapInvalidAttrValueException (com.zimbra.cs.ldap.LdapException.LdapInvalidAttrValueException)2 HashMap (java.util.HashMap)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 Control (com.unboundid.ldap.sdk.Control)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)1