Search in sources :

Example 16 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.

the class LdapExample method getAttributes.

public void getAttributes() throws Exception {
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
        /*
             * get attributes zimbraId, cn and description on DN "cn=default,cn=cos,cn=zimbra"
             */
        ZAttributes attrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", new String[] { "zimbraId", "cn", "description" });
        String zimbraId = attrs.getAttrString("zimbraId");
        /*
             * get all attributes on DN "cn=default,cn=cos,cn=zimbra"
             */
        ZAttributes allAttrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", null);
    } finally {
        // Note: this is important!! 
        LdapClient.closeContext(zlc);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 17 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.

the class LdapGalSearch method getGalEntryByDn.

public static void getGalEntryByDn(ZLdapContext zlc, GalSearchConfig.GalType galType, String dn, LdapGalMapRules rules, SearchGalResult result) throws ServiceException {
    String[] reqAttrs = rules.getLdapAttrs();
    if (ZimbraLog.gal.isDebugEnabled()) {
        StringBuffer returnAttrs = new StringBuffer();
        for (String a : reqAttrs) {
            returnAttrs.append(a + ",");
        }
        zlc.debug();
        ZimbraLog.gal.debug("getGalEntryByDn: " + ", dn=" + dn + ", attrs=" + returnAttrs);
    }
    SearhcGalVisitor visitor = new SearhcGalVisitor(zlc, galType, null, rules, result);
    try {
        ZAttributes attrs = zlc.getAttributes(dn, reqAttrs);
        visitor.visit(dn, attrs);
    } catch (LdapEntryNotFoundException e) {
        ZimbraLog.gal.debug("getGalEntryByDn: no such dn: " + dn, e);
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to search gal", e);
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 18 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.

the class TestLdapHelper method getAttributes.

@Test
public void getAttributes() throws Exception {
    String dn = prov.getDIT().configDN();
    ZAttributes attrs = ldapHelper.getAttributes(LdapUsage.UNITTEST, dn);
    assertEquals("config", attrs.getAttrString(Provisioning.A_cn));
}
Also used : ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 19 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.

the class LdapProvisioning method removeAliasInternal.

/*
     * 1. remove alias from mail and zimbraMailAlias attributes of the entry
     * 2. remove alias from all distribution lists
     * 3. delete the alias entry
     *
     * A. entry exists, alias exists
     *    - if alias points to the entry:            do 1, 2, 3
     *    - if alias points to other existing entry: do 1, and then throw NO_SUCH_ALIAS
     *    - if alias points to a non-existing entry: do 1, 2, 3, and then throw NO_SUCH_ALIAS
     *
     * B. entry exists, alias does not exist:  do 1, 2, and then throw NO_SUCH_ALIAS
     *
     * C. entry does not exist, alias exists:
     *    - if alias points to other existing entry: do nothing (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
     *    - if alias points to a non-existing entry: do 2, 3 (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
     *
     * D. entry does not exist, alias does not exist:  do 2 (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
     *
     *
     */
private void removeAliasInternal(NamedEntry entry, String alias) throws ServiceException {
    LdapUsage ldapUsage = null;
    if (entry instanceof Account) {
        ldapUsage = LdapUsage.REMOVE_ALIAS_ACCOUNT;
    } else if (entry instanceof Group) {
        ldapUsage = LdapUsage.REMOVE_ALIAS_DL;
    } else {
        ldapUsage = LdapUsage.REMOVE_ALIAS;
    }
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, ldapUsage);
        alias = alias.toLowerCase();
        alias = IDNUtil.toAsciiEmail(alias);
        String[] parts = alias.split("@");
        String aliasName = parts[0];
        String aliasDomain = parts[1];
        Domain domain = getDomainByAsciiName(aliasDomain, zlc);
        if (domain == null)
            throw AccountServiceException.NO_SUCH_DOMAIN(aliasDomain);
        String targetDn = (entry == null) ? null : ((LdapEntry) entry).getDN();
        String targetDomainName = null;
        if (entry != null) {
            if (entry instanceof Account) {
                targetDomainName = ((Account) entry).getDomainName();
            } else if (entry instanceof Group) {
                targetDomainName = ((Group) entry).getDomainName();
            } else {
                throw ServiceException.INVALID_REQUEST("invalid entry type for alias", null);
            }
        }
        String aliasDn = mDIT.aliasDN(targetDn, targetDomainName, aliasName, aliasDomain);
        ZAttributes aliasAttrs = null;
        Alias aliasEntry = null;
        try {
            aliasAttrs = helper.getAttributes(zlc, aliasDn);
            // see if the entry is an alias
            if (!isEntryAlias(aliasAttrs))
                throw AccountServiceException.NO_SUCH_ALIAS(alias);
            aliasEntry = makeAlias(aliasDn, aliasAttrs);
        } catch (ServiceException e) {
            ZimbraLog.account.warn("alias " + alias + " does not exist");
        }
        NamedEntry targetEntry = null;
        if (aliasEntry != null)
            targetEntry = searchAliasTarget(aliasEntry, false);
        boolean aliasPointsToEntry = ((entry != null) && (aliasEntry != null) && entry.getId().equals(aliasEntry.getAttr(Provisioning.A_zimbraAliasTargetId)));
        boolean aliasPointsToOtherExistingEntry = ((aliasEntry != null) && (targetEntry != null) && ((entry == null) || (!entry.getId().equals(targetEntry.getId()))));
        boolean aliasPointsToNonExistingEntry = ((aliasEntry != null) && (targetEntry == null));
        // 1. remove alias from mail/zimbraMailAlias attrs
        if (entry != null) {
            try {
                HashMap<String, String> attrs = new HashMap<String, String>();
                attrs.put("-" + Provisioning.A_mail, alias);
                attrs.put("-" + Provisioning.A_zimbraMailAlias, alias);
                modifyAttrsInternal(entry, zlc, attrs);
            } catch (ServiceException e) {
                ZimbraLog.account.warn("unable to remove zimbraMailAlias/mail attrs: " + alias);
            }
        }
        // 2. remove address from all DLs
        if (!aliasPointsToOtherExistingEntry) {
            removeAddressFromAllDistributionLists(alias);
        }
        // 3. remove the alias entry
        if (aliasPointsToEntry || aliasPointsToNonExistingEntry) {
            try {
                zlc.deleteEntry(aliasDn);
            } catch (ServiceException e) {
                // should not happen, log it
                ZimbraLog.account.warn("unable to remove alias entry at : " + aliasDn);
            }
        }
        // throw NO_SUCH_ALIAS if necessary
        if (((entry != null) && (aliasEntry == null)) || ((entry != null) && (aliasEntry != null) && !aliasPointsToEntry))
            throw AccountServiceException.NO_SUCH_ALIAS(alias);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) Group(com.zimbra.cs.account.Group) DynamicGroup(com.zimbra.cs.account.DynamicGroup) LdapDynamicGroup(com.zimbra.cs.account.ldap.entry.LdapDynamicGroup) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) HashMap(java.util.HashMap) NamedEntry(com.zimbra.cs.account.NamedEntry) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) LdapAlias(com.zimbra.cs.account.ldap.entry.LdapAlias) Alias(com.zimbra.cs.account.Alias) ZAttributes(com.zimbra.cs.ldap.ZAttributes) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain) LdapUsage(com.zimbra.cs.ldap.LdapUsage)

Example 20 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.

the class LdapProvisioning method getServerByName.

private Server getServerByName(String name, boolean nocache) throws ServiceException {
    if (!nocache) {
        Server s = serverCache.getByName(name);
        if (s != null)
            return s;
    }
    try {
        String dn = mDIT.serverNameToDN(name);
        ZAttributes attrs = helper.getAttributes(LdapUsage.GET_SERVER, dn);
        LdapServer s = new LdapServer(dn, attrs, getConfig().getServerDefaults(), this);
        serverCache.put(s);
        return s;
    } catch (LdapEntryNotFoundException e) {
        return null;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to lookup server by name: " + name + " message: " + e.getMessage(), e);
    }
}
Also used : 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) LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Aggregations

ZAttributes (com.zimbra.cs.ldap.ZAttributes)30 ServiceException (com.zimbra.common.service.ServiceException)18 AccountServiceException (com.zimbra.cs.account.AccountServiceException)16 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)15 LdapEntryNotFoundException (com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException)8 Account (com.zimbra.cs.account.Account)6 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)6 Domain (com.zimbra.cs.account.Domain)4 GuestAccount (com.zimbra.cs.account.GuestAccount)4 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)4 HashMap (java.util.HashMap)4 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)3 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)3 ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)3 Alias (com.zimbra.cs.account.Alias)2 AlwaysOnCluster (com.zimbra.cs.account.AlwaysOnCluster)2 DynamicGroup (com.zimbra.cs.account.DynamicGroup)2 Group (com.zimbra.cs.account.Group)2 NamedEntry (com.zimbra.cs.account.NamedEntry)2 Server (com.zimbra.cs.account.Server)2