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 if (errorMessage.indexOf(PrefsPropsUtil.getString(
270 companyId, PropsKeys.LDAP_ERROR_USER_LOCKOUT))
271 != -1) {
272
273 throw new UserLockoutException();
274 }
275 else if (errorMessage.indexOf(PrefsPropsUtil.getString(
276 companyId, PropsKeys.LDAP_ERROR_PASSWORD_EXPIRED))
277 != -1) {
278
279 throw new PasswordExpiredException();
280 }
281 }
282
283 if (!ldapAuthResult.isAuthenticated()) {
284 return FAILURE;
285 }
286
287
288
289 User user = PortalLDAPImporterUtil.importLDAPUser(
290 ldapServerId, companyId, ldapContext, attributes, password);
291
292
293
294 String resultCode = ldapAuthResult.getResponseControl();
295
296 if (resultCode.equals(LDAPAuth.RESULT_PASSWORD_RESET)) {
297 UserLocalServiceUtil.updatePasswordReset(
298 user.getUserId(), true);
299 }
300 else if (
301 resultCode.equals(LDAPAuth.RESULT_PASSWORD_EXP_WARNING)) {
302
303 UserLocalServiceUtil.updatePasswordReset(
304 user.getUserId(), true);
305 }
306 }
307 else {
308 if (_log.isDebugEnabled()) {
309 _log.debug("Search filter did not return any results");
310 }
311
312 return DNE;
313 }
314
315 enu.close();
316 }
317 catch (Exception e) {
318 _log.error("Problem accessing LDAP server", e);
319
320 return FAILURE;
321 }
322 finally {
323 if (ldapContext != null) {
324 ldapContext.close();
325 }
326 }
327
328 return SUCCESS;
329 }
330
331 protected int authenticate(
332 long companyId, String emailAddress, String screenName, long userId,
333 String password)
334 throws Exception {
335
336 if (!LDAPSettingsUtil.isAuthEnabled(companyId)) {
337 if (_log.isDebugEnabled()) {
338 _log.debug("Authenticator is not enabled");
339 }
340
341 return SUCCESS;
342 }
343
344 if (_log.isDebugEnabled()) {
345 _log.debug("Authenticator is enabled");
346 }
347
348 long[] ldapServerIds = StringUtil.split(
349 PrefsPropsUtil.getString(companyId, "ldap.server.ids"), 0L);
350
351 if (ldapServerIds.length <= 0) {
352 ldapServerIds = new long[] {0};
353 }
354
355 for (long ldapServerId : ldapServerIds) {
356 int result = authenticate(
357 companyId, ldapServerId, emailAddress, screenName, userId,
358 password);
359
360 if (result == SUCCESS) {
361 return result;
362 }
363 }
364
365 return authenticateRequired(
366 companyId, userId, emailAddress, screenName, true, FAILURE);
367 }
368
369 protected int authenticateOmniadmin(
370 long companyId, String emailAddress, String screenName, long userId)
371 throws Exception {
372
373
374
375 if (PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
376 if (userId > 0) {
377 if (OmniadminUtil.isOmniadmin(userId)) {
378 return SUCCESS;
379 }
380 }
381 else if (Validator.isNotNull(emailAddress)) {
382 try {
383 User user = UserLocalServiceUtil.getUserByEmailAddress(
384 companyId, emailAddress);
385
386 if (OmniadminUtil.isOmniadmin(user.getUserId())) {
387 return SUCCESS;
388 }
389 }
390 catch (NoSuchUserException nsue) {
391 }
392 }
393 else if (Validator.isNotNull(screenName)) {
394 try {
395 User user = UserLocalServiceUtil.getUserByScreenName(
396 companyId, screenName);
397
398 if (OmniadminUtil.isOmniadmin(user.getUserId())) {
399 return SUCCESS;
400 }
401 }
402 catch (NoSuchUserException nsue) {
403 }
404 }
405 }
406
407 return FAILURE;
408 }
409
410 protected int authenticateRequired(
411 long companyId, long userId, String emailAddress, String screenName,
412 boolean allowOmniadmin, int failureCode)
413 throws Exception {
414
415
416
417
418 if (allowOmniadmin &&
419 (authenticateOmniadmin(
420 companyId, emailAddress, screenName, userId) == SUCCESS)) {
421
422 return SUCCESS;
423 }
424
425 if (PrefsPropsUtil.getBoolean(
426 companyId, PropsKeys.LDAP_AUTH_REQUIRED)) {
427
428 return failureCode;
429 }
430 else {
431 return SUCCESS;
432 }
433 }
434
435 private static Log _log = LogFactoryUtil.getLog(LDAPAuth.class);
436
437 }