Search in sources :

Example 81 with ServiceException

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;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) Server(com.zimbra.cs.account.Server) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ArrayList(java.util.ArrayList) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 82 with ServiceException

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);
    }
}
Also used : InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) 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 83 with ServiceException

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;
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) DistributionList(com.zimbra.cs.account.DistributionList)

Example 84 with ServiceException

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);
    }
}
Also used : HashMap(java.util.HashMap) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) SearchLdapOptions(com.zimbra.cs.ldap.SearchLdapOptions) LdapContextNotEmptyException(com.zimbra.cs.ldap.LdapException.LdapContextNotEmptyException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ZimletException(com.zimbra.cs.zimlet.ZimletException) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) IOException(java.io.IOException) LdapInvalidSearchFilterException(com.zimbra.cs.ldap.LdapException.LdapInvalidSearchFilterException) StopIteratingException(com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException) LdapInvalidAttrValueException(com.zimbra.cs.ldap.LdapException.LdapInvalidAttrValueException) LdapContextNotEmptyException(com.zimbra.cs.ldap.LdapException.LdapContextNotEmptyException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) ServiceException(com.zimbra.common.service.ServiceException) LdapException(com.zimbra.cs.ldap.LdapException) LdapMultipleEntriesMatchedException(com.zimbra.cs.ldap.LdapException.LdapMultipleEntriesMatchedException) PatternSyntaxException(java.util.regex.PatternSyntaxException) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) LdapInvalidAttrNameException(com.zimbra.cs.ldap.LdapException.LdapInvalidAttrNameException) SearchLdapVisitor(com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor) SearchLdapVisitor(com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) IAttributes(com.zimbra.cs.ldap.IAttributes) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 85 with ServiceException

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);
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain) Pair(com.zimbra.common.util.Pair) DataSource(com.zimbra.cs.account.DataSource) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)772 AccountServiceException (com.zimbra.cs.account.AccountServiceException)220 Account (com.zimbra.cs.account.Account)193 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)149 IOException (java.io.IOException)127 Mailbox (com.zimbra.cs.mailbox.Mailbox)122 ArrayList (java.util.ArrayList)107 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)100 Element (com.zimbra.common.soap.Element)97 HashMap (java.util.HashMap)93 Test (org.junit.Test)89 Provisioning (com.zimbra.cs.account.Provisioning)86 Domain (com.zimbra.cs.account.Domain)60 Folder (com.zimbra.cs.mailbox.Folder)54 Server (com.zimbra.cs.account.Server)53 ItemId (com.zimbra.cs.service.util.ItemId)52 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)51 ZMailbox (com.zimbra.client.ZMailbox)50 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)46 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)44