001
014
015 package com.liferay.portal.security.auth;
016
017 import com.liferay.portal.NoSuchUserException;
018 import com.liferay.portal.PasswordExpiredException;
019 import com.liferay.portal.UserLockoutException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
029 import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
030 import com.liferay.portal.security.ldap.PortalLDAPUtil;
031 import com.liferay.portal.security.pwd.PwdEncryptor;
032 import com.liferay.portal.service.UserLocalServiceUtil;
033 import com.liferay.portal.util.PrefsPropsUtil;
034 import com.liferay.portal.util.PropsValues;
035 import com.liferay.portlet.admin.util.OmniadminUtil;
036
037 import java.util.Hashtable;
038 import java.util.Map;
039 import java.util.Properties;
040
041 import javax.naming.Context;
042 import javax.naming.NamingEnumeration;
043 import javax.naming.directory.Attribute;
044 import javax.naming.directory.Attributes;
045 import javax.naming.directory.SearchControls;
046 import javax.naming.directory.SearchResult;
047 import javax.naming.ldap.Control;
048 import javax.naming.ldap.InitialLdapContext;
049 import javax.naming.ldap.LdapContext;
050
051
055 public class LDAPAuth implements Authenticator {
056
057 public static final String AUTH_METHOD_BIND = "bind";
058
059 public static final String AUTH_METHOD_PASSWORD_COMPARE =
060 "password-compare";
061
062 public static final String RESULT_PASSWORD_EXP_WARNING =
063 "2.16.840.1.113730.3.4.5";
064
065 public static final String RESULT_PASSWORD_RESET =
066 "2.16.840.1.113730.3.4.4";
067
068 public int authenticateByEmailAddress(
069 long companyId, String emailAddress, String password,
070 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
071 throws AuthException {
072
073 try {
074 return authenticate(
075 companyId, emailAddress, StringPool.BLANK, 0, password);
076 }
077 catch (Exception e) {
078 _log.error(e, e);
079
080 throw new AuthException(e);
081 }
082 }
083
084 public int authenticateByScreenName(
085 long companyId, String screenName, String password,
086 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
087 throws AuthException {
088
089 try {
090 return authenticate(
091 companyId, StringPool.BLANK, screenName, 0, password);
092 }
093 catch (Exception e) {
094 _log.error(e, e);
095
096 throw new AuthException(e);
097 }
098 }
099
100 public int authenticateByUserId(
101 long companyId, long userId, String password,
102 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
103 throws AuthException {
104
105 try {
106 return authenticate(
107 companyId, StringPool.BLANK, StringPool.BLANK, userId,
108 password);
109 }
110 catch (Exception e) {
111 _log.error(e, e);
112
113 throw new AuthException(e);
114 }
115 }
116
117 protected LDAPAuthResult authenticate(
118 LdapContext ctx, long companyId, Attributes attributes,
119 String userDN, String password)
120 throws Exception {
121
122 LDAPAuthResult ldapAuthResult = new LDAPAuthResult();
123
124
125
126
127
128 String authMethod = PrefsPropsUtil.getString(
129 companyId, PropsKeys.LDAP_AUTH_METHOD);
130 InitialLdapContext innerCtx = null;
131
132 if (authMethod.equals(AUTH_METHOD_BIND)) {
133 try {
134 Hashtable<String, Object> env =
135 (Hashtable<String, Object>)ctx.getEnvironment();
136
137 env.put(Context.SECURITY_PRINCIPAL, userDN);
138 env.put(Context.SECURITY_CREDENTIALS, password);
139 env.put(
140 Context.REFERRAL,
141 PrefsPropsUtil.getString(
142 companyId, PropsKeys.LDAP_REFERRAL));
143
144
145
146 env.put("com.sun.jndi.ldap.connect.pool", "false");
147
148 innerCtx = new InitialLdapContext(env, null);
149
150
151
152 Control[] responseControls = innerCtx.getResponseControls();
153
154 ldapAuthResult.setAuthenticated(true);
155 ldapAuthResult.setResponseControl(responseControls);
156 }
157 catch (Exception e) {
158 if (_log.isDebugEnabled()) {
159 _log.debug(
160 "Failed to bind to the LDAP server with userDN "
161 + userDN + " and password " + password);
162 }
163
164 _log.error("Failed to bind to the LDAP server", e);
165
166 ldapAuthResult.setAuthenticated(false);
167 ldapAuthResult.setErrorMessage(e.getMessage());
168 }
169 finally {
170 if (innerCtx != null) {
171 innerCtx.close();
172 }
173 }
174 }
175 else if (authMethod.equals(AUTH_METHOD_PASSWORD_COMPARE)) {
176 Attribute userPassword = attributes.get("userPassword");
177
178 if (userPassword != null) {
179 String ldapPassword = new String((byte[])userPassword.get());
180
181 String encryptedPassword = password;
182
183 String algorithm = PrefsPropsUtil.getString(
184 companyId,
185 PropsKeys.LDAP_AUTH_PASSWORD_ENCRYPTION_ALGORITHM);
186
187 if (Validator.isNotNull(algorithm)) {
188 encryptedPassword =
189 "{" + algorithm + "}" +
190 PwdEncryptor.encrypt(
191 algorithm, password, ldapPassword);
192 }
193
194 if (ldapPassword.equals(encryptedPassword)) {
195 ldapAuthResult.setAuthenticated(true);
196 }
197 else {
198 ldapAuthResult.setAuthenticated(false);
199
200 if (_log.isWarnEnabled()) {
201 _log.warn(
202 "Passwords do not match for userDN " + userDN);
203 }
204 }
205 }
206 }
207
208 return ldapAuthResult;
209 }
210
211 protected int authenticate(
212 long companyId, long ldapServerId, String emailAddress,
213 String screenName, long userId, String password)
214 throws Exception {
215
216 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
217
218 LdapContext ldapContext = PortalLDAPUtil.getContext(
219 ldapServerId, companyId);
220
221 if (ldapContext == null) {
222 return FAILURE;
223 }
224
225 try {
226 String baseDN = PrefsPropsUtil.getString(
227 companyId, PropsKeys.LDAP_BASE_DN + postfix);
228
229
230
231 String filter = LDAPSettingsUtil.getAuthSearchFilter(
232 ldapServerId, companyId, emailAddress, screenName,
233 String.valueOf(userId));
234
235 Properties userMappings = LDAPSettingsUtil.getUserMappings(
236 ldapServerId, companyId);
237
238 String userMappingsScreenName = GetterUtil.getString(
239 userMappings.getProperty("screenName")).toLowerCase();
240
241 SearchControls searchControls = new SearchControls(
242 SearchControls.SUBTREE_SCOPE, 1, 0,
243 new String[] {userMappingsScreenName}, false, false);
244
245 NamingEnumeration<SearchResult> enu = ldapContext.search(
246 baseDN, filter, searchControls);
247
248 if (enu.hasMoreElements()) {
249 if (_log.isDebugEnabled()) {
250 _log.debug("Search filter returned at least one result");
251 }
252
253 SearchResult result = enu.nextElement();
254
255 String fullUserDN = PortalLDAPUtil.getNameInNamespace(
256 ldapServerId, companyId, result);
257
258 Attributes attributes = PortalLDAPUtil.getUserAttributes(
259 ldapServerId, companyId, ldapContext, fullUserDN);
260
261 LDAPAuthResult ldapAuthResult = authenticate(
262 ldapContext, companyId, attributes, fullUserDN, password);
263
264
265
266 String errorMessage = ldapAuthResult.getErrorMessage();
267
268 if (errorMessage != null) {
269 int pos = errorMessage.indexOf(
270 PrefsPropsUtil.getString(
271 companyId, PropsKeys.LDAP_ERROR_USER_LOCKOUT));
272
273 if (pos != -1) {
274 throw new UserLockoutException();
275 }
276
277 pos = errorMessage.indexOf(
278 PrefsPropsUtil.getString(
279 companyId, PropsKeys.LDAP_ERROR_PASSWORD_EXPIRED));
280
281 if (pos != -1) {
282 throw new PasswordExpiredException();
283 }
284 }
285
286 if (!ldapAuthResult.isAuthenticated()) {
287 return FAILURE;
288 }
289
290
291
292 User user = PortalLDAPImporterUtil.importLDAPUser(
293 ldapServerId, companyId, ldapContext, attributes, password);
294
295
296
297 String resultCode = ldapAuthResult.getResponseControl();
298
299 if (resultCode.equals(LDAPAuth.RESULT_PASSWORD_RESET)) {
300 UserLocalServiceUtil.updatePasswordReset(
301 user.getUserId(), true);
302 }
303 }
304 else {
305 if (_log.isDebugEnabled()) {
306 _log.debug("Search filter did not return any results");
307 }
308
309 return DNE;
310 }
311
312 enu.close();
313 }
314 catch (Exception e) {
315 if (e instanceof PasswordExpiredException ||
316 e instanceof UserLockoutException) {
317
318 throw e;
319 }
320
321 _log.error("Problem accessing LDAP server", e);
322
323 return FAILURE;
324 }
325 finally {
326 if (ldapContext != null) {
327 ldapContext.close();
328 }
329 }
330
331 return SUCCESS;
332 }
333
334 protected int authenticate(
335 long companyId, String emailAddress, String screenName, long userId,
336 String password)
337 throws Exception {
338
339 if (!AuthSettingsUtil.isLDAPAuthEnabled(companyId)) {
340 if (_log.isDebugEnabled()) {
341 _log.debug("Authenticator is not enabled");
342 }
343
344 return SUCCESS;
345 }
346
347 if (_log.isDebugEnabled()) {
348 _log.debug("Authenticator is enabled");
349 }
350
351 long[] ldapServerIds = StringUtil.split(
352 PrefsPropsUtil.getString(companyId, "ldap.server.ids"), 0L);
353
354 for (long ldapServerId : ldapServerIds) {
355 int result = authenticate(
356 companyId, ldapServerId, emailAddress, screenName, userId,
357 password);
358
359 if (result == SUCCESS) {
360 return result;
361 }
362 }
363
364 for (int ldapServerId = 0;; ldapServerId++) {
365 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
366
367 String providerUrl = PrefsPropsUtil.getString(
368 companyId, PropsKeys.LDAP_BASE_PROVIDER_URL + postfix);
369
370 if (Validator.isNull(providerUrl)) {
371 break;
372 }
373
374 int result = authenticate(
375 companyId, ldapServerId, emailAddress, screenName, userId,
376 password);
377
378 if (result == SUCCESS) {
379 return result;
380 }
381 }
382
383 return authenticateRequired(
384 companyId, userId, emailAddress, screenName, true, FAILURE);
385 }
386
387 protected int authenticateOmniadmin(
388 long companyId, String emailAddress, String screenName, long userId)
389 throws Exception {
390
391
392
393 if (PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
394 if (userId > 0) {
395 if (OmniadminUtil.isOmniadmin(userId)) {
396 return SUCCESS;
397 }
398 }
399 else if (Validator.isNotNull(emailAddress)) {
400 try {
401 User user = UserLocalServiceUtil.getUserByEmailAddress(
402 companyId, emailAddress);
403
404 if (OmniadminUtil.isOmniadmin(user.getUserId())) {
405 return SUCCESS;
406 }
407 }
408 catch (NoSuchUserException nsue) {
409 }
410 }
411 else if (Validator.isNotNull(screenName)) {
412 try {
413 User user = UserLocalServiceUtil.getUserByScreenName(
414 companyId, screenName);
415
416 if (OmniadminUtil.isOmniadmin(user.getUserId())) {
417 return SUCCESS;
418 }
419 }
420 catch (NoSuchUserException nsue) {
421 }
422 }
423 }
424
425 return FAILURE;
426 }
427
428 protected int authenticateRequired(
429 long companyId, long userId, String emailAddress, String screenName,
430 boolean allowOmniadmin, int failureCode)
431 throws Exception {
432
433
434
435
436 if (allowOmniadmin &&
437 (authenticateOmniadmin(
438 companyId, emailAddress, screenName, userId) == SUCCESS)) {
439
440 return SUCCESS;
441 }
442
443 if (PrefsPropsUtil.getBoolean(
444 companyId, PropsKeys.LDAP_AUTH_REQUIRED)) {
445
446 return failureCode;
447 }
448 else {
449 return SUCCESS;
450 }
451 }
452
453 private static Log _log = LogFactoryUtil.getLog(LDAPAuth.class);
454
455 }