Search in sources :

Example 1 with SearchLdapOptions

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

the class LdapProvisioning method deleteDomainInternal.

public void deleteDomainInternal(ZLdapContext zlc, String zimbraId) throws ServiceException {
    // TODO: should only allow a domain delete to succeed if there are no people
    // if there aren't, we need to delete the people trees first, then delete the domain.
    LdapDomain domain = null;
    String acctBaseDn = null;
    String dynGroupsBaseDn = null;
    try {
        // bypass the cached Domain data, in case a subdomain exists and deletion
        // of the domain transforms into attribute removal, in which case attributes
        // might be missing from a stale cached Domain object
        domain = (LdapDomain) getDomainByIdInternal(zimbraId, zlc, GetFromDomainCacheOption.NEGATIVE);
        if (domain == null) {
            throw AccountServiceException.NO_SUCH_DOMAIN(zimbraId);
        }
        String name = domain.getName();
        // delete account base DN
        acctBaseDn = mDIT.domainDNToAccountBaseDN(domain.getDN());
        if (!acctBaseDn.equals(domain.getDN())) {
            try {
                zlc.deleteEntry(acctBaseDn);
            } catch (LdapEntryNotFoundException e) {
                ZimbraLog.account.info("entry %s not found", acctBaseDn);
            }
        }
        // delete dynamic groups base DN
        dynGroupsBaseDn = mDIT.domainDNToDynamicGroupsBaseDN(domain.getDN());
        if (!dynGroupsBaseDn.equals(domain.getDN())) {
            try {
                zlc.deleteEntry(dynGroupsBaseDn);
            } catch (LdapEntryNotFoundException e) {
                ZimbraLog.account.info("entry %s not found", dynGroupsBaseDn);
            }
        }
        try {
            zlc.deleteEntry(domain.getDN());
            domainCache.remove(domain);
        } catch (LdapContextNotEmptyException e) {
            // remove from cache before nuking all attrs
            domainCache.remove(domain);
            // assume subdomains exist and turn into plain dc object
            Map<String, Object> attrs = new HashMap<String, Object>();
            List<String> objClasses = new ArrayList<String>();
            objClasses.addAll(Arrays.asList("zimbraDomain", "amavisAccount", "DKIM"));
            attrs.put("-" + A_objectClass, objClasses);
            // remove all zimbra attrs
            for (String key : domain.getAttrs(false).keySet()) {
                if (key.startsWith("zimbra") || key.startsWith("amavis") || key.startsWith("DKIM"))
                    attrs.put(key, "");
            }
            // cannot invoke callback here.  If another domain attr is added in a callback,
            // e.g. zimbraDomainStatus would add zimbraMailStatus, then we will get a LDAP
            // schema violation naming error(zimbraDomain is removed, thus there cannot be
            // any zimbraAttrs left) and the modify will fail.
            modifyAttrs(domain, attrs, false, false);
            // necessary to remove the cached object re-created/refreshed by
            // refreshEntry() down the line from modifyAttrs()?
            domainCache.remove(domain);
        }
        String defaultDomain = getConfig().getAttr(A_zimbraDefaultDomainName, null);
        if (name.equalsIgnoreCase(defaultDomain)) {
            try {
                Map<String, String> attrs = new HashMap<String, String>();
                attrs.put(A_zimbraDefaultDomainName, "");
                modifyAttrs(getConfig(), attrs);
            } catch (Exception e) {
                ZimbraLog.account.warn("unable to remove config attr:" + A_zimbraDefaultDomainName, e);
            }
        }
    } catch (LdapContextNotEmptyException e) {
        // get a few entries to include in the error message
        int maxEntriesToGet = 5;
        final String doNotReportThisDN = acctBaseDn;
        final StringBuilder sb = new StringBuilder();
        sb.append(" (remaining entries: ");
        SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor() {

            @Override
            public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) {
                if (!dn.equals(doNotReportThisDN)) {
                    sb.append("[" + dn + "] ");
                }
            }
        };
        SearchLdapOptions searchOptions = new SearchLdapOptions(acctBaseDn, filterFactory.anyEntry(), new String[] { Provisioning.A_objectClass }, maxEntriesToGet, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
        try {
            zlc.searchPaged(searchOptions);
        } catch (LdapSizeLimitExceededException lslee) {
        // quietly ignore
        } catch (ServiceException se) {
            ZimbraLog.account.warn("unable to get sample entries in non-empty domain " + domain.getName() + " for reporting", se);
        }
        sb.append("...)");
        throw AccountServiceException.DOMAIN_NOT_EMPTY(domain.getName() + sb.toString(), e);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to purge domain: " + zimbraId, e);
    }
}
Also used : HashMap(java.util.HashMap) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions) LdapContextNotEmptyException(com.zimbra.cs.ldap.LdapException.LdapContextNotEmptyException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ZimletException(com.zimbra.cs.zimlet.ZimletException) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) IOException(java.io.IOException) LdapInvalidSearchFilterException(com.zimbra.cs.ldap.LdapException.LdapInvalidSearchFilterException) StopIteratingException(com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException) LdapInvalidAttrValueException(com.zimbra.cs.ldap.LdapException.LdapInvalidAttrValueException) LdapContextNotEmptyException(com.zimbra.cs.ldap.LdapException.LdapContextNotEmptyException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) ServiceException(com.zimbra.common.service.ServiceException) LdapException(com.zimbra.cs.ldap.LdapException) LdapMultipleEntriesMatchedException(com.zimbra.cs.ldap.LdapException.LdapMultipleEntriesMatchedException) PatternSyntaxException(java.util.regex.PatternSyntaxException) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) LdapInvalidAttrNameException(com.zimbra.cs.ldap.LdapException.LdapInvalidAttrNameException) SearchLdapVisitor(com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor) 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) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) IAttributes(com.zimbra.cs.ldap.IAttributes) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) ArrayList(java.util.ArrayList) List(java.util.List) AddressList(com.zimbra.cs.account.AddressList) DistributionList(com.zimbra.cs.account.DistributionList) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 2 with SearchLdapOptions

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

the class LdapProvisioning method searchLdapObjects.

private void searchLdapObjects(String base, ZLdapFilter filter, String[] returnAttrs, SearchDirectoryOptions opts, NamedEntry.Visitor visitor) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.get(opts.getOnMaster()), opts.getUseConnPool(), LdapUsage.SEARCH);
        SearchObjectsVisitor searchObjectsVisitor = new SearchObjectsVisitor(this, zlc, visitor, opts.getMaxResults(), opts.getMakeObjectOpt(), returnAttrs);
        SearchLdapOptions searchObjectsOptions = new SearchLdapOptions(base, filter, returnAttrs, opts.getMaxResults(), null, ZSearchScope.SEARCH_SCOPE_SUBTREE, searchObjectsVisitor);
        searchObjectsOptions.setUseControl(opts.isUseControl());
        searchObjectsOptions.setManageDSAit(opts.isManageDSAit());
        zlc.searchPaged(searchObjectsOptions);
    } catch (LdapSizeLimitExceededException e) {
        throw AccountServiceException.TOO_MANY_SEARCH_RESULTS("too many search results returned", e);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to list all objects", 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) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions)

Example 3 with SearchLdapOptions

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

the class BUG_18277 method getAllDomainOrGlobalAdmins.

private void getAllDomainOrGlobalAdmins(Set<String> domainAdminIds, Set<String> globalAdminIds) throws ServiceException {
    LdapDIT dit = prov.getDIT();
    String[] returnAttrs = new String[] { Provisioning.A_objectClass, Provisioning.A_zimbraId, Provisioning.A_zimbraIsAdminAccount, Provisioning.A_zimbraIsDomainAdminAccount, Provisioning.A_zimbraIsDelegatedAdminAccount };
    String configBranchBaseDn = dit.configBranchBaseDN();
    String base = dit.mailBranchBaseDN();
    String query = "(&(objectclass=zimbraAccount)(|(zimbraIsDomainAdminAccount=TRUE)(zimbraIsAdminAccount=TRUE)))";
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
        Bug18277Visitor visitor = new Bug18277Visitor(this, configBranchBaseDn, domainAdminIds, globalAdminIds);
        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);
    }
}
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)

Example 4 with SearchLdapOptions

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

the class BUG_29978 method doUpgrade.

/**
 * for each domain, if domain has zimbraPublicServiceHostname, and that zPSH has a
 * corresponding zimbraServer, then set public service port/protocol on domain from
 * that zimbraServer.
 */
@Override
void doUpgrade() throws ServiceException {
    List<Server> servers = prov.getAllServers();
    String query = genQuery(servers);
    String[] bases = prov.getDIT().getSearchBases(Provisioning.SD_DOMAIN_FLAG);
    String[] attrs = new String[] { Provisioning.A_objectClass, Provisioning.A_zimbraId, Provisioning.A_zimbraDomainName, Provisioning.A_zimbraPublicServiceHostname, Provisioning.A_zimbraPublicServiceProtocol, Provisioning.A_zimbraPublicServicePort };
    ZLdapContext zlc = null;
    Bug29978Visitor visitor = new Bug29978Visitor(this, zlc, servers);
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
        for (String base : bases) {
            // should really have one base, but iterate thought the arrya anyway
            if (verbose) {
                printer.println("LDAP search base: " + base);
                printer.println("LDAP search query: " + query);
                printer.println();
            }
            SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), attrs, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
            zlc.searchPaged(searchOpts);
        }
    } finally {
        LdapClient.closeContext(zlc);
        visitor.reportStat();
    }
}
Also used : Server(com.zimbra.cs.account.Server) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions)

Example 5 with SearchLdapOptions

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

the class BUG_57866 method upgradeGalSyncAccounts.

private void upgradeGalSyncAccounts(ZLdapContext zlc) throws ServiceException {
    LdapDIT dit = prov.getDIT();
    String[] returnAttrs = new String[] { Provisioning.A_zimbraGalAccountId };
    String base = dit.mailBranchBaseDN();
    String query = "(&(objectclass=zimbraDomain)(zimbraGalAccountId=*))";
    final Set<String> galAcctIds = new HashSet<String>();
    SearchLdapVisitor visitor = new SearchLdapVisitor(false) {

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

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