Search in sources :

Example 6 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 7 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 8 with ZLdapContext

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

the class LdapProvisioning method copyCos.

private Cos copyCos(String srcCosId, String destCosName, Map<String, Object> cosAttrs) throws ServiceException {
    destCosName = destCosName.toLowerCase().trim();
    Cos srcCos = getCosById(srcCosId, null);
    if (srcCos == null)
        throw AccountServiceException.NO_SUCH_COS(srcCosId);
    // bug 67716, use a case insensitive map because provided attr names may not be
    // the canonical name and that will cause multiple entries in the map
    Map<String, Object> allAttrs = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
    allAttrs.putAll(srcCos.getAttrs());
    allAttrs.remove(Provisioning.A_objectClass);
    allAttrs.remove(Provisioning.A_zimbraId);
    allAttrs.remove(Provisioning.A_zimbraCreateTimestamp);
    allAttrs.remove(Provisioning.A_zimbraACE);
    allAttrs.remove(Provisioning.A_cn);
    allAttrs.remove(Provisioning.A_description);
    if (cosAttrs != null) {
        for (Map.Entry<String, Object> e : cosAttrs.entrySet()) {
            String attr = e.getKey();
            Object value = e.getValue();
            if (value instanceof String && Strings.isNullOrEmpty((String) value)) {
                allAttrs.remove(attr);
            } else {
                allAttrs.put(attr, value);
            }
        }
    }
    CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
    //get rid of deprecated attrs
    Map<String, Object> allNewAttrs = new HashMap<String, Object>(allAttrs);
    for (String attr : allAttrs.keySet()) {
        AttributeInfo info = AttributeManager.getInstance().getAttributeInfo(attr);
        if (info != null && info.isDeprecated()) {
            allNewAttrs.remove(attr);
        }
    }
    allAttrs = allNewAttrs;
    AttributeManager.getInstance().preModify(allAttrs, null, callbackContext, true);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_COS);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.mapToAttrs(allAttrs);
        Set<String> ocs = LdapObjectClass.getCosObjectClasses(this);
        entry.addAttr(A_objectClass, ocs);
        String zimbraIdStr = LdapUtil.generateUUID();
        entry.setAttr(A_zimbraId, zimbraIdStr);
        entry.setAttr(A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
        entry.setAttr(A_cn, destCosName);
        String dn = mDIT.cosNametoDN(destCosName);
        entry.setDN(dn);
        zlc.createEntry(entry);
        Cos cos = getCosById(zimbraIdStr, zlc);
        AttributeManager.getInstance().postModify(allAttrs, cos, callbackContext);
        return cos;
    } catch (LdapEntryAlreadyExistException nabe) {
        throw AccountServiceException.COS_EXISTS(destCosName);
    } catch (LdapException e) {
        throw e;
    } catch (AccountServiceException e) {
        throw e;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to create cos: " + destCosName, 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) HashMap(java.util.HashMap) LdapCos(com.zimbra.cs.account.ldap.entry.LdapCos) Cos(com.zimbra.cs.account.Cos) TreeMap(java.util.TreeMap) Date(java.util.Date) AttributeInfo(com.zimbra.cs.account.AttributeInfo) 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) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) LdapException(com.zimbra.cs.ldap.LdapException)

Example 9 with ZLdapContext

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

the class LdapProvisioning method deleteDistributionList.

private void deleteDistributionList(LdapDistributionList dl) throws ServiceException {
    String zimbraId = dl.getId();
    // make a copy of all addrs of this DL, after the delete all aliases on this dl
    // object will be gone, but we need to remove them from the allgroups cache after the DL is deleted
    Set<String> addrs = new HashSet<String>(dl.getMultiAttrSet(Provisioning.A_mail));
    // remove the DL from all DLs
    // this doesn't throw any exceptions
    removeAddressFromAllDistributionLists(dl.getName());
    // delete all aliases of the DL
    String[] aliases = dl.getAliases();
    if (aliases != null) {
        String dlName = dl.getName();
        for (int i = 0; i < aliases.length; i++) {
            // this "alias" if it is the primary name, the entire entry will be deleted anyway.
            if (!dlName.equalsIgnoreCase(aliases[i])) {
                // this also removes each alias from any DLs
                removeAlias(dl, aliases[i]);
            }
        }
    }
    // delete all grants granted to the DL
    try {
        RightCommand.revokeAllRights(this, GranteeType.GT_GROUP, zimbraId);
    } catch (ServiceException e) {
        // eat the exception and continue
        ZimbraLog.account.warn("cannot revoke grants", e);
    }
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_DISTRIBUTIONLIST);
        zlc.deleteEntry(dl.getDN());
        groupCache.remove(dl);
        allDLs.removeGroup(addrs);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to purge distribution list: " + zimbraId, e);
    } finally {
        LdapClient.closeContext(zlc);
    }
    PermissionCache.invalidateCache();
}
Also used : 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) HashSet(java.util.HashSet)

Example 10 with ZLdapContext

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

the class LdapProvisioning method deleteAlwaysOnCluster.

@Override
public void deleteAlwaysOnCluster(String zimbraId) throws ServiceException {
    LdapAlwaysOnCluster cluster = (LdapAlwaysOnCluster) getAlwaysOnClusterByIdInternal(zimbraId);
    if (cluster == null)
        throw AccountServiceException.NO_SUCH_ALWAYSONCLUSTER(zimbraId);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_ALWAYSONCLUSTER);
        zlc.deleteEntry(cluster.getDN());
        alwaysOnClusterCache.remove(cluster);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to purge alwaysOnCluster: " + zimbraId, e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : LdapAlwaysOnCluster(com.zimbra.cs.account.ldap.entry.LdapAlwaysOnCluster) 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)

Aggregations

ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)112 ServiceException (com.zimbra.common.service.ServiceException)51 AccountServiceException (com.zimbra.cs.account.AccountServiceException)48 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)46 LdapEntryAlreadyExistException (com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException)21 LdapException (com.zimbra.cs.ldap.LdapException)20 ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)18 Domain (com.zimbra.cs.account.Domain)17 CallbackContext (com.zimbra.cs.account.callback.CallbackContext)14 Date (java.util.Date)14 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)12 HashMap (java.util.HashMap)12 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)11 SearchLdapOptions (com.zimbra.cs.ldap.SearchLdapOptions)11 Account (com.zimbra.cs.account.Account)9 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)8 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)8 GuestAccount (com.zimbra.cs.account.GuestAccount)7 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)7 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)7