Search in sources :

Example 21 with ZLdapContext

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

the class LdapProvisioning method getAttrsInOCs.

@Override
public void getAttrsInOCs(String[] ocs, Set<String> attrsInOCs) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.GET_SCHEMA);
        ZLdapSchema schema = zlc.getSchema();
        for (String oc : ocs) {
            try {
                ZLdapSchema.ZObjectClassDefinition ocSchema = schema.getObjectClass(oc);
                if (ocSchema != null) {
                    List<String> optAttrs = ocSchema.getOptionalAttributes();
                    for (String attr : optAttrs) {
                        attrsInOCs.add(attr);
                    }
                    List<String> reqAttrs = ocSchema.getRequiredAttributes();
                    for (String attr : reqAttrs) {
                        attrsInOCs.add(attr);
                    }
                }
            } catch (ServiceException e) {
                ZimbraLog.account.debug("unable to lookup attributes for objectclass: " + oc, e);
            }
        }
    } catch (ServiceException e) {
        ZimbraLog.account.warn("unable to get LDAP schema", e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapSchema(com.zimbra.cs.ldap.ZLdapSchema) 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)

Example 22 with ZLdapContext

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

the class LdapProvisioning method createShareLocator.

@Override
public ShareLocator createShareLocator(String id, Map<String, Object> attrs) throws ServiceException {
    CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
    AttributeManager.getInstance().preModify(attrs, null, callbackContext, true);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SHARELOCATOR);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.mapToAttrs(attrs);
        Set<String> ocs = LdapObjectClass.getShareLocatorObjectClasses(this);
        entry.addAttr(A_objectClass, ocs);
        entry.setAttr(A_cn, id);
        String dn = mDIT.shareLocatorIdToDN(id);
        entry.setDN(dn);
        zlc.createEntry(entry);
        ShareLocator shloc = getShareLocatorById(id, zlc, true);
        AttributeManager.getInstance().postModify(attrs, shloc, callbackContext);
        return shloc;
    } catch (LdapEntryAlreadyExistException nabe) {
        throw AccountServiceException.SHARE_LOCATOR_EXISTS(id);
    } catch (LdapException e) {
        throw e;
    } catch (AccountServiceException e) {
        throw e;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to create share locator: " + id, e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) 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) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) LdapException(com.zimbra.cs.ldap.LdapException) LdapShareLocator(com.zimbra.cs.account.ldap.entry.LdapShareLocator) ShareLocator(com.zimbra.cs.account.ShareLocator)

Example 23 with ZLdapContext

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

the class LdapProvisioning method deleteDomain.

private void deleteDomain(String zimbraId, boolean deleteDomainAliases) throws ServiceException {
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_DOMAIN);
        Domain domain = getDomainById(zimbraId, zlc);
        if (domain == null) {
            throw AccountServiceException.NO_SUCH_DOMAIN(zimbraId);
        }
        if (deleteDomainAliases) {
            List<String> aliasDomainIds = null;
            if (domain.isLocal()) {
                aliasDomainIds = getEmptyAliasDomainIds(zlc, domain, true);
            }
            // delete all alias domains if any
            if (aliasDomainIds != null) {
                for (String aliasDomainId : aliasDomainIds) {
                    deleteDomainInternal(zlc, aliasDomainId);
                }
            }
        }
        // delete the domain;
        deleteDomainInternal(zlc, zimbraId);
    } catch (ServiceException e) {
        throw 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) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain)

Example 24 with ZLdapContext

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

the class LdapProvisioning method deleteIdentity.

@Override
public void deleteIdentity(Account account, String identityName) throws ServiceException {
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    if (identityName.equalsIgnoreCase(ProvisioningConstants.DEFAULT_IDENTITY_NAME))
        throw ServiceException.INVALID_REQUEST("can't delete default identity", null);
    account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_IDENTITY);
        Identity identity = getIdentityByName(ldapEntry, identityName, zlc);
        if (identity == null)
            throw AccountServiceException.NO_SUCH_IDENTITY(identityName);
        String dn = getIdentityDn(ldapEntry, identityName);
        zlc.deleteEntry(dn);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to delete identity: " + identityName, 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) LdapIdentity(com.zimbra.cs.account.ldap.entry.LdapIdentity) Identity(com.zimbra.cs.account.Identity)

Example 25 with ZLdapContext

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

the class LdapProvisioning method createDataSource.

private DataSource createDataSource(Account account, DataSourceType dsType, String dsName, Map<String, Object> dataSourceAttrs, boolean passwdAlreadyEncrypted, boolean restoring) throws ServiceException {
    removeAttrIgnoreCase("objectclass", dataSourceAttrs);
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null) {
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    }
    List<DataSource> existing = getAllDataSources(account);
    if (existing.size() >= account.getLongAttr(A_zimbraDataSourceMaxNumEntries, 20)) {
        throw AccountServiceException.TOO_MANY_DATA_SOURCES();
    }
    String dsEmailAddr = (String) dataSourceAttrs.get(A_zimbraDataSourceEmailAddress);
    if (!StringUtil.isNullOrEmpty(dsEmailAddr)) {
        for (DataSource ds : existing) {
            if (dsEmailAddr.equals(ds.getEmailAddress())) {
                throw AccountServiceException.DATA_SOURCE_EXISTS(dsEmailAddr);
            }
        }
    }
    // must be the same
    dataSourceAttrs.put(A_zimbraDataSourceName, dsName);
    dataSourceAttrs.put(Provisioning.A_zimbraDataSourceType, dsType.toString());
    account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
    boolean checkImmutable = !restoring;
    CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
    AttributeManager.getInstance().preModify(dataSourceAttrs, null, callbackContext, checkImmutable);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_DATASOURCE);
        String dn = getDataSourceDn(ldapEntry, dsName);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.setDN(dn);
        entry.mapToAttrs(dataSourceAttrs);
        entry.setAttr(A_objectClass, "zimbraDataSource");
        String extraOc = LdapDataSource.getObjectClass(dsType);
        if (extraOc != null) {
            entry.addAttr(A_objectClass, Sets.newHashSet(extraOc));
        }
        String dsId = entry.getAttrString(A_zimbraDataSourceId);
        if (dsId == null) {
            dsId = LdapUtil.generateUUID();
            entry.setAttr(A_zimbraDataSourceId, dsId);
        }
        String password = entry.getAttrString(A_zimbraDataSourcePassword);
        if (password != null) {
            String encrypted = passwdAlreadyEncrypted ? password : DataSource.encryptData(dsId, password);
            entry.setAttr(A_zimbraDataSourcePassword, encrypted);
        }
        String oauthToken = entry.getAttrString(A_zimbraDataSourceOAuthToken);
        if (oauthToken != null) {
            String encrypted = passwdAlreadyEncrypted ? oauthToken : DataSource.encryptData(dsId, oauthToken);
            entry.setAttr(A_zimbraDataSourceOAuthToken, encrypted);
        }
        String clientSecret = entry.getAttrString(A_zimbraDataSourceOAuthClientSecret);
        if (clientSecret != null) {
            String encrypted = passwdAlreadyEncrypted ? clientSecret : DataSource.encryptData(dsId, clientSecret);
            entry.setAttr(A_zimbraDataSourceOAuthClientSecret, encrypted);
        }
        String smtpPassword = entry.getAttrString(A_zimbraDataSourceSmtpAuthPassword);
        if (smtpPassword != null) {
            String encrypted = passwdAlreadyEncrypted ? smtpPassword : DataSource.encryptData(dsId, smtpPassword);
            entry.setAttr(A_zimbraDataSourceSmtpAuthPassword, encrypted);
        }
        entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
        zlc.createEntry(entry);
        DataSource ds = getDataSourceById(ldapEntry, dsId, zlc);
        AttributeManager.getInstance().postModify(dataSourceAttrs, ds, callbackContext);
        return ds;
    } catch (LdapEntryAlreadyExistException nabe) {
        throw AccountServiceException.DATA_SOURCE_EXISTS(dsName);
    } catch (LdapException e) {
        throw e;
    } catch (AccountServiceException e) {
        throw e;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to create data source: " + dsName, 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) DataSource(com.zimbra.cs.account.DataSource) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource) 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) LdapException(com.zimbra.cs.ldap.LdapException)

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