Search in sources :

Example 31 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class JWTBasedAuthTest method testAccountAndExpiresJWT.

@Test
public void testAccountAndExpiresJWT() {
    Account acct;
    try {
        acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        AuthToken at = AuthProvider.getAuthToken(acct, 0, TokenType.JWT);
        validateJWT(at, acct.getId());
    } catch (ServiceException e) {
        Assert.fail("testAccountAndExpiresJWT failed");
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) AuthToken(com.zimbra.cs.account.AuthToken) Test(org.junit.Test)

Example 32 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class AutoProvision method createAccount.

protected Account createAccount(String acctZimbraName, ExternalEntry externalEntry, String password, AutoProvMode mode) throws ServiceException {
    ZAttributes externalAttrs = externalEntry.getAttrs();
    Map<String, Object> zimbraAttrs = mapAttrs(externalAttrs);
    /*
        // TODO: should we do this?
        String zimbraPassword = RandomPassword.generate();
        zimbraAttrs.put(Provisioning.A_zimbraPasswordMustChange, Provisioning.TRUE);
        */
    // if password is provided, use it
    String zimbraPassword = null;
    if (password != null) {
        zimbraPassword = password;
        zimbraAttrs.remove(Provisioning.A_userPassword);
    }
    Account acct = null;
    try {
        acct = prov.createAccount(acctZimbraName, zimbraPassword, zimbraAttrs);
    } catch (ServiceException e) {
        if (AccountServiceException.ACCOUNT_EXISTS.equals(e.getCode())) {
            ZimbraLog.autoprov.debug("account %s already exists", acctZimbraName);
            // the account already exists, that's fine, just return null
            switch(mode) {
                case EAGER:
                    // that's fine, just return null
                    return null;
                case LAZY:
                case MANUAL:
                default:
                    throw e;
            }
        } else {
            throw e;
        }
    }
    ZimbraLog.autoprov.info("auto provisioned account: " + acctZimbraName);
    ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "auto provision Account", "name", acct.getName(), "id", acct.getId() }, zimbraAttrs));
    // send notification email
    try {
        sendNotifMessage(acct, zimbraPassword);
    } catch (ServiceException e) {
        // exception during sending notif email should not fail this method
        ZimbraLog.autoprov.warn("unable to send auto provision notification email", e);
    }
    // invoke post create listener if configured
    try {
        AutoProvisionListener listener = AutoProvisionCachedInfo.getInfo(domain).getListener();
        if (listener != null) {
            listener.postCreate(domain, acct, externalEntry.getDN());
        } else {
            // eager mode should configure Listener
            if (mode == AutoProvMode.EAGER) {
                ZimbraLog.autoprov.warn("EAGER mode should configure " + Provisioning.A_zimbraAutoProvListenerClass);
            }
        }
    } catch (ServiceException e) {
        // exception during the post create listener should not fail this method
        ZimbraLog.autoprov.warn("encountered error in post auto provision listener", e);
    }
    return acct;
}
Also used : Account(com.zimbra.cs.account.Account) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 33 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class AutoProvisionEager method createAccountBatch.

private void createAccountBatch() throws ServiceException {
    long polledAt = System.currentTimeMillis();
    List<ExternalEntry> entries = new ArrayList<ExternalEntry>();
    boolean hitSizeLimitExceededException = searchAccounts(entries, domain.getAutoProvBatchSize());
    ZimbraLog.autoprov.info("%d external LDAP entries returned as search result", entries.size());
    int stuckAcctNum = 0;
    for (ExternalEntry entry : entries) {
        if (scheduler.isShutDownRequested()) {
            ZimbraLog.autoprov.info("eager auto provision aborted");
            return;
        }
        try {
            ZAttributes externalAttrs = entry.getAttrs();
            String acctZimbraName = mapName(externalAttrs, null);
            ZimbraLog.autoprov.info("auto creating account in EAGER mode: " + acctZimbraName + ", dn=\"" + entry.getDN() + "\"");
            Account acct = createAccount(acctZimbraName, entry, null, AutoProvMode.EAGER);
            if (acct == null) {
                stuckAcctNum++;
            }
        } catch (ServiceException e) {
            // log and continue with next entry
            ZimbraLog.autoprov.warn("unable to auto create account, dn=\"" + entry.getDN() + "\"", e);
            stuckAcctNum++;
        }
    }
    // in the last batch we won't hit size limit, then the last polled timstamp will be set, we can forget about the stuck ones
    if (hitSizeLimitExceededException && entries.size() == stuckAcctNum) {
        ZimbraLog.autoprov.info("search result contains unsuccessful external entries, increasing batch size by %d", stuckAcctNum);
        int currentBatchSize = domain.getAutoProvBatchSize();
        domain.setAutoProvBatchSize(currentBatchSize + stuckAcctNum);
        ZimbraLog.autoprov.info("batch size is %d now", domain.getAutoProvBatchSize());
    }
    // 
    if (!hitSizeLimitExceededException) {
        String lastPolledAt = LdapDateUtil.toGeneralizedTimeWithMs(new Date(polledAt));
        ZimbraLog.autoprov.info("Auto Provisioning has finished for now, setting last polled timestamp: " + lastPolledAt);
        domain.setAutoProvLastPolledTimestampAsString(lastPolledAt);
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) ZAttributes(com.zimbra.cs.ldap.ZAttributes) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 34 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class TrustedSenderList method preModify.

@SuppressWarnings("unchecked")
@Override
public void preModify(CallbackContext context, String name, Object value, @SuppressWarnings("rawtypes") Map mod, Entry entry) throws ServiceException {
    if (context.isCreate() || !(entry instanceof Account)) {
        return;
    }
    // Skip if already processed.
    if (context.isDoneAndSetIfNot(TrustedSenderList.class)) {
        return;
    }
    Account account = (Account) entry;
    int max = account.getMailTrustedSenderListMaxNumEntries();
    Object replace = mod.get(name);
    Object add = mod.get("+" + name);
    Object remove = mod.get("-" + name);
    if (replace != null) {
        Set<String> set = getMultiValueSet(replace);
        if (set.size() > max) {
            throw AccountServiceException.TOO_MANY_TRUSTED_SENDERS(set.size() + " > " + max);
        }
    } else {
        String[] current = account.getMultiAttr(name, false);
        if (add != null) {
            Set<String> set = getMultiValueSet(add);
            if (current.length > 0) {
                if (current.length + set.size() > max) {
                    throw AccountServiceException.TOO_MANY_TRUSTED_SENDERS(current.length + " + " + set.size() + " > " + max);
                }
            } else {
                // copy CoS values
                List<String> def = getMultiValue(account.getAttrDefault(name));
                if (def.size() + set.size() > max) {
                    throw AccountServiceException.TOO_MANY_TRUSTED_SENDERS(def.size() + " + " + set.size() + " > " + max);
                }
                set.addAll(def);
                mod.put("+" + name, set);
            }
        }
        if (remove != null) {
            if (current.length == 0) {
                // copy CoS values
                Set<String> def;
                if (add == null) {
                    def = getMultiValueSet(account.getAttrDefault(name));
                } else {
                    // honor the result from "add"
                    def = (Set<String>) mod.get("+" + name);
                    assert (def != null);
                }
                def.removeAll(getMultiValueSet(remove));
                mod.remove("-" + name);
                mod.put("+" + name, def);
            }
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account)

Example 35 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class DataSourceCallback method scheduleCos.

/**
 * Updates data source schedules for all accounts that are on the current server
 * and in the given COS.
 */
private void scheduleCos(Cos cos) throws ServiceException {
    ZimbraLog.datasource.info("Updating schedule for all DataSources for all accounts in COS %s.", cos.getName());
    List<Account> accts;
    Provisioning prov = Provisioning.getInstance();
    // Look up all account id's for this server
    if (prov instanceof LdapProv)
        accts = lookupAccountsFromLDAP(prov, cos.getId());
    else
        accts = lookupAccountsFromDB(prov);
    // Update schedules for all data sources on this server
    for (Account account : accts) {
        if (account != null && Provisioning.ACCOUNT_STATUS_ACTIVE.equals(account.getAccountStatus(prov))) {
            Cos accountCos = prov.getCOS(account);
            if (accountCos != null && cos.getId().equals(accountCos.getId())) {
                scheduleAccount(account);
            }
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) Cos(com.zimbra.cs.account.Cos) Provisioning(com.zimbra.cs.account.Provisioning) LdapProv(com.zimbra.cs.account.ldap.LdapProv)

Aggregations

Account (com.zimbra.cs.account.Account)1444 Test (org.junit.Test)691 Mailbox (com.zimbra.cs.mailbox.Mailbox)468 OperationContext (com.zimbra.cs.mailbox.OperationContext)354 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)331 Message (com.zimbra.cs.mailbox.Message)315 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)294 Element (com.zimbra.common.soap.Element)280 ServiceException (com.zimbra.common.service.ServiceException)261 ItemId (com.zimbra.cs.service.util.ItemId)248 Provisioning (com.zimbra.cs.account.Provisioning)234 HashMap (java.util.HashMap)179 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)175 Domain (com.zimbra.cs.account.Domain)149 MimeMessage (javax.mail.internet.MimeMessage)125 GuestAccount (com.zimbra.cs.account.GuestAccount)107 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)100 SyntaxException (org.apache.jsieve.exception.SyntaxException)90 AccountServiceException (com.zimbra.cs.account.AccountServiceException)87 Header (javax.mail.Header)84