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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations