use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class LdapProvisioning method ldapAuthenticate.
/*
* search for the auth DN for the user, authneticate to the result DN
*/
private void ldapAuthenticate(String[] url, boolean wantStartTLS, String password, String searchBase, String searchFilter, String searchDn, String searchPassword) throws ServiceException {
if (password == null || password.equals("")) {
throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("empty password");
}
ExternalLdapConfig config = new ExternalLdapConfig(url, wantStartTLS, null, searchDn, searchPassword, null, "external LDAP auth");
String resultDn = null;
String tooMany = null;
ZLdapContext zlc = null;
try {
zlc = LdapClient.getExternalContext(config, LdapUsage.LDAP_AUTH_EXTERNAL);
ZSearchResultEnumeration ne = zlc.searchDir(searchBase, filterFactory.fromFilterString(FilterId.LDAP_AUTHENTICATE, searchFilter), ZSearchControls.SEARCH_CTLS_SUBTREE());
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
if (resultDn == null) {
resultDn = sr.getDN();
} else {
tooMany = sr.getDN();
break;
}
}
ne.close();
} finally {
LdapClient.closeContext(zlc);
}
if (tooMany != null) {
ZimbraLog.account.warn(String.format("ldapAuthenticate searchFilter returned more then one result: (dn1=%s, dn2=%s, filter=%s)", resultDn, tooMany, searchFilter));
throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("too many results from search filter!");
} else if (resultDn == null) {
throw AccountServiceException.AuthFailedServiceException.AUTH_FAILED("empty search");
}
if (ZimbraLog.account.isDebugEnabled())
ZimbraLog.account.debug("search filter matched: " + resultDn);
ldapAuthenticate(url, wantStartTLS, resultDn, password);
}
use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class LdapProvisioning method getDistributionListByQuery.
private DistributionList getDistributionListByQuery(String base, ZLdapFilter filter, ZLdapContext initZlc, boolean basicAttrsOnly) throws ServiceException {
String[] returnAttrs = basicAttrsOnly ? BASIC_DL_ATTRS : null;
DistributionList dl = null;
try {
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration ne = helper.searchDir(base, filter, searchControls, initZlc, LdapServerType.REPLICA);
if (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
dl = makeDistributionList(sr.getDN(), sr.getAttributes(), basicAttrsOnly);
}
ne.close();
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to lookup distribution list via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
}
return dl;
}
Aggregations