use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllServers.
@Override
public List<Server> getAllServers(String service) throws ServiceException {
List<Server> result = new ArrayList<Server>();
ZLdapFilter filter;
if (service != null) {
filter = filterFactory.serverByService(service);
} else {
filter = filterFactory.allServers();
}
try {
Map<String, Object> serverDefaults = getConfig().getServerDefaults();
ZSearchResultEnumeration ne = helper.searchDir(mDIT.serverBaseDN(), filter, ZSearchControls.SEARCH_CTLS_SUBTREE());
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
LdapServer s = new LdapServer(sr.getDN(), sr.getAttributes(), serverDefaults, this);
result.add(s);
}
ne.close();
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to list all servers", e);
}
if (result.size() > 0)
serverCache.put(result, true);
Collections.sort(result);
return result;
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method deleteServer.
@Override
public void deleteServer(String zimbraId) throws ServiceException {
LdapServer server = (LdapServer) getServerByIdInternal(zimbraId);
if (server == null)
throw AccountServiceException.NO_SUCH_SERVER(zimbraId);
// check that no account is still on this server
long numAcctsOnServer = getNumAccountsOnServer(server);
if (numAcctsOnServer != 0) {
throw ServiceException.INVALID_REQUEST("There are " + numAcctsOnServer + " account(s) on this server.", null);
}
String monitorHost = getConfig().getAttr(Provisioning.A_zimbraLogHostname);
String serverName = server.getName();
if (monitorHost != null && monitorHost.trim().equals(serverName)) {
throw ServiceException.INVALID_REQUEST("zimbraLogHostname is set to " + serverName, null);
}
String[] attributesToRemove = { Provisioning.A_zimbraReverseProxyAvailableLookupTargets, Provisioning.A_zimbraReverseProxyUpstreamEwsServers, Provisioning.A_zimbraReverseProxyUpstreamLoginServers };
removeAttrsForServer(attributesToRemove, server.getName());
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_SERVER);
removeServerFromAllCOSes(zimbraId, server.getName(), zlc);
zlc.deleteEntry(server.getDN());
serverCache.remove(server);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to purge server: " + zimbraId, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllAddressesOfEntry.
//
// returns the primary address and all aliases of the named account or DL
//
private AddrsOfEntry getAllAddressesOfEntry(String name) {
String primary = null;
String[] aliases = null;
AddrsOfEntry addrs = new AddrsOfEntry();
try {
// bug 56621. Do not count implicit aliases (aliases resolved by alias domain)
// when dealing with distribution list members.
Account acct = getAccountByName(name, false, false);
if (acct != null) {
addrs.setIsAccount(true);
primary = acct.getName();
aliases = acct.getMailAlias();
} else {
DistributionList dl = get(Key.DistributionListBy.name, name);
if (dl != null) {
primary = dl.getName();
aliases = dl.getAliases();
}
}
} catch (ServiceException se) {
// swallow any exception and go on
}
if (primary != null)
addrs.setPrimary(primary);
if (aliases != null)
addrs.addAll(aliases);
return addrs;
}
use of com.zimbra.common.service.ServiceException 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 {
domain = (LdapDomain) getDomainById(zimbraId, zlc);
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, String> attrs = new HashMap<String, String>();
attrs.put("-" + A_objectClass, "zimbraDomain");
// remove all zimbra attrs
for (String key : domain.getAttrs(false).keySet()) {
if (key.startsWith("zimbra"))
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);
}
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);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method autoProvZMGProxyAccount.
@Override
public Pair<Account, Boolean> autoProvZMGProxyAccount(String emailAddr, String password) throws ServiceException {
Account acct = getAccountByForeignPrincipal(ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
if (acct != null) {
return new Pair<>(acct, false);
}
String domainId = getConfig().getMobileGatewayDefaultProxyAccountDomainId();
if (domainId == null) {
return new Pair<>(null, false);
}
Domain domain = getDomainById(domainId);
if (domain == null) {
ZimbraLog.account.error("Domain corresponding to" + A_zimbraMobileGatewayDefaultProxyAccountDomainId + "not found ");
throw ServiceException.FAILURE("Missing server configuration", null);
}
String testDsId = "TestId";
Map<String, Object> testAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, true, testDsId);
DataSource dsToTest = new DataSource(null, DataSourceType.imap, "Test", testDsId, testAttrs, this);
try {
DataSourceManager.test(dsToTest);
} catch (ServiceException e) {
ZimbraLog.account.debug("ZMG Proxy account auto provisioning failed", e);
throw AuthFailedServiceException.AUTH_FAILED(emailAddr, SystemUtil.getInnermostException(e).getMessage(), e);
}
Map<String, Object> acctAttrs = new HashMap<String, Object>();
acctAttrs.put(Provisioning.A_zimbraIsMobileGatewayProxyAccount, ProvisioningConstants.TRUE);
acctAttrs.put(Provisioning.A_zimbraForeignPrincipal, ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
acctAttrs.put(Provisioning.A_zimbraHideInGal, ProvisioningConstants.TRUE);
acctAttrs.put(Provisioning.A_zimbraMailStatus, Provisioning.MailStatus.disabled.toString());
acctAttrs.put(Provisioning.A_zimbraMailHost, getLocalServer().getServiceHostname());
acct = createDummyAccount(acctAttrs, password, domain);
DataSource ds;
try {
Map<String, Object> dsAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, false, null);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, Integer.toString(mbox.createFolder(null, "/" + emailAddr, new Folder.FolderOptions()).getId()));
ds = createDataSource(acct, DataSourceType.imap, emailAddr, dsAttrs);
} catch (ServiceException e) {
try {
deleteAccount(acct.getId());
} catch (ServiceException e1) {
if (!AccountServiceException.NO_SUCH_ACCOUNT.equals(e1.getCode())) {
throw e1;
}
}
throw e;
}
DataSourceManager.asyncImportData(ds);
return new Pair<>(acct, true);
}
Aggregations