Search in sources :

Example 1 with LdapEntry

use of com.zimbra.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.

the class LdapProvisioning method refreshEntry.

void refreshEntry(Entry entry, ZLdapContext initZlc) throws ServiceException {
    try {
        String dn = ((LdapEntry) entry).getDN();
        ZAttributes attributes = helper.getAttributes(initZlc, dn);
        Map<String, Object> attrs = attributes.getAttrs();
        Map<String, Object> defaults = null;
        Map<String, Object> secondaryDefaults = null;
        Map<String, Object> overrideDefaults = null;
        if (entry instanceof Account) {
            // 
            // We can get here from either modifyAttrsInternal or reload path.
            // 
            // If we got here from modifyAttrsInternal, zimbraCOSId on account
            // might have been changed, added, removed, but entry now still contains
            // the old attrs.  Create a temp Account object from the new attrs, and then
            // use the same cos of the temp Account object for our entry object.
            // 
            // If we got here from reload, attrs are likely not changed, the callsites
            // just want a refreshed object.  For this case it's best if we still
            // always resolve the COS correctly.  makeAccount is a cheap call and won't
            // add any overhead like loading cos/domain from LDAP: even if cos/domain
            // has to be loaded (because not in cache) in the getCOS(temp) call, it's
            // just the same as calling (buggy) getCOS(entry) before.
            // 
            // We only need the temp object for the getCOS call, don't need to setup
            // primary/secondary defaults on the temp object because:
            // zimbraCOSId is only on account(of course), and that's all needed
            // for determining the COS for the account in the getCOS call: if
            // zimbraCOSId is not set on account, it will fallback to the domain
            // default COS, then fallback to the system default COS.
            // 
            Account temp = makeAccountNoDefaults(dn, attributes);
            Cos cos = getCOS(temp);
            if (cos != null)
                defaults = cos.getAccountDefaults();
            Domain domain = getDomain((Account) entry);
            if (domain != null)
                secondaryDefaults = domain.getAccountDefaults();
        } else if (entry instanceof Domain) {
            defaults = getConfig().getDomainDefaults();
        } else if (entry instanceof Server) {
            defaults = getConfig().getServerDefaults();
            AlwaysOnCluster aoc = getAlwaysOnCluster((Server) entry);
            if (aoc != null) {
                overrideDefaults = aoc.getServerOverrides();
            }
        }
        if (defaults == null && secondaryDefaults == null)
            entry.setAttrs(attrs);
        else
            entry.setAttrs(attrs, defaults, secondaryDefaults, overrideDefaults);
        extendLifeInCacheOrFlush(entry);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to refresh entry", e);
    }
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) Server(com.zimbra.cs.account.Server) LdapAlwaysOnCluster(com.zimbra.cs.account.ldap.entry.LdapAlwaysOnCluster) AlwaysOnCluster(com.zimbra.cs.account.AlwaysOnCluster) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) LdapCos(com.zimbra.cs.account.ldap.entry.LdapCos) Cos(com.zimbra.cs.account.Cos) ZAttributes(com.zimbra.cs.ldap.ZAttributes) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain)

Example 2 with LdapEntry

use of com.zimbra.cs.account.ldap.entry.LdapEntry 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();
    }
    // 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)

Example 3 with LdapEntry

use of com.zimbra.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.

the class LdapProvisioning method modifyDataSource.

@Override
public void modifyDataSource(Account account, String dataSourceId, Map<String, Object> attrs) throws ServiceException {
    removeAttrIgnoreCase("objectclass", attrs);
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    LdapDataSource ds = (LdapDataSource) getDataSourceById(ldapEntry, dataSourceId, null);
    if (ds == null)
        throw AccountServiceException.NO_SUCH_DATA_SOURCE(dataSourceId);
    account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
    attrs.remove(A_zimbraDataSourceId);
    String name = (String) attrs.get(A_zimbraDataSourceName);
    boolean newName = (name != null && !name.equals(ds.getName()));
    if (newName)
        attrs.remove(A_zimbraDataSourceName);
    String password = (String) attrs.get(A_zimbraDataSourcePassword);
    if (password != null) {
        attrs.put(A_zimbraDataSourcePassword, DataSource.encryptData(ds.getId(), password));
    }
    String oauthToken = (String) attrs.get(A_zimbraDataSourceOAuthToken);
    if (oauthToken != null) {
        attrs.put(A_zimbraDataSourceOAuthToken, DataSource.encryptData(ds.getId(), oauthToken));
    }
    String clientSecret = (String) attrs.get(A_zimbraDataSourceOAuthClientSecret);
    if (clientSecret != null) {
        attrs.put(A_zimbraDataSourceOAuthClientSecret, DataSource.encryptData(ds.getId(), clientSecret));
    }
    String smtpPassword = (String) attrs.get(A_zimbraDataSourceSmtpAuthPassword);
    if (smtpPassword != null) {
        attrs.put(A_zimbraDataSourceSmtpAuthPassword, DataSource.encryptData(ds.getId(), smtpPassword));
    }
    modifyAttrs(ds, attrs, true);
    if (newName) {
        // the datasoruce cache could've been loaded again if getAllDataSources were called in pre/poseModify callback, so we clear it again
        account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
        ZLdapContext zlc = null;
        try {
            zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.RENAME_DATASOURCE);
            String newDn = getDataSourceDn(ldapEntry, name);
            zlc.renameEntry(ds.getDN(), newDn);
        } catch (ServiceException e) {
            throw ServiceException.FAILURE("unable to rename datasource: " + name, e);
        } finally {
            LdapClient.closeContext(zlc);
        }
    }
}
Also used : LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource) 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 4 with LdapEntry

use of com.zimbra.cs.account.ldap.entry.LdapEntry 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 5 with LdapEntry

use of com.zimbra.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.

the class LdapProvisioning method modifyIdentity.

@Override
public void modifyIdentity(Account account, String identityName, Map<String, Object> identityAttrs) throws ServiceException {
    removeAttrIgnoreCase("objectclass", identityAttrs);
    validateIdentityAttrs(identityAttrs);
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    // clear cache
    account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
    if (identityName.equalsIgnoreCase(ProvisioningConstants.DEFAULT_IDENTITY_NAME)) {
        modifyAttrs(account, identityAttrs);
    } else {
        LdapIdentity identity = (LdapIdentity) getIdentityByName(ldapEntry, identityName, null);
        if (identity == null)
            throw AccountServiceException.NO_SUCH_IDENTITY(identityName);
        String name = (String) identityAttrs.get(A_zimbraPrefIdentityName);
        boolean newName = (name != null && !name.equals(identityName));
        if (newName)
            identityAttrs.remove(A_zimbraPrefIdentityName);
        modifyAttrs(identity, identityAttrs, true);
        if (newName) {
            // the identity cache could've been loaded again if getAllIdentities were called in pre/poseModify callback, so we clear it again
            account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
            renameIdentity(ldapEntry, identity, name);
        }
    }
}
Also used : LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) LdapIdentity(com.zimbra.cs.account.ldap.entry.LdapIdentity)

Aggregations

LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)29 ServiceException (com.zimbra.common.service.ServiceException)20 AccountServiceException (com.zimbra.cs.account.AccountServiceException)20 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)17 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)15 Account (com.zimbra.cs.account.Account)10 DistributionList (com.zimbra.cs.account.DistributionList)9 Domain (com.zimbra.cs.account.Domain)9 HashMap (java.util.HashMap)9 NamedEntry (com.zimbra.cs.account.NamedEntry)7 List (java.util.List)7 LdapProv (com.zimbra.cs.account.ldap.LdapProv)6 LdapException (com.zimbra.cs.ldap.LdapException)5 LdapEntryAlreadyExistException (com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException)5 GuestAccount (com.zimbra.cs.account.GuestAccount)4 Signature (com.zimbra.cs.account.Signature)4 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)4 LdapDataSource (com.zimbra.cs.account.ldap.entry.LdapDataSource)4 LdapIdentity (com.zimbra.cs.account.ldap.entry.LdapIdentity)4 LdapSignature (com.zimbra.cs.account.ldap.entry.LdapSignature)4