1
22
23 package com.liferay.portal.security.auth;
24
25 import com.liferay.portal.NoSuchUserException;
26 import com.liferay.portal.PasswordExpiredException;
27 import com.liferay.portal.UserLockoutException;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.security.ldap.PortalLDAPUtil;
34 import com.liferay.portal.security.pwd.PwdEncryptor;
35 import com.liferay.portal.service.UserLocalServiceUtil;
36 import com.liferay.portal.util.PrefsPropsUtil;
37 import com.liferay.portal.util.PropsKeys;
38 import com.liferay.portal.util.PropsValues;
39 import com.liferay.portlet.admin.util.OmniadminUtil;
40
41 import java.util.Hashtable;
42 import java.util.Map;
43
44 import javax.naming.Context;
45 import javax.naming.NamingEnumeration;
46 import javax.naming.directory.Attribute;
47 import javax.naming.directory.Attributes;
48 import javax.naming.directory.SearchControls;
49 import javax.naming.directory.SearchResult;
50 import javax.naming.ldap.Control;
51 import javax.naming.ldap.InitialLdapContext;
52 import javax.naming.ldap.LdapContext;
53
54
61 public class LDAPAuth implements Authenticator {
62
63 public static final String AUTH_METHOD_BIND = "bind";
64
65 public static final String AUTH_METHOD_PASSWORD_COMPARE =
66 "password-compare";
67
68 public static final String RESULT_PASSWORD_RESET =
69 "2.16.840.1.113730.3.4.4";
70
71 public static final String RESULT_PASSWORD_EXP_WARNING =
72 "2.16.840.1.113730.3.4.5";
73
74 public int authenticateByEmailAddress(
75 long companyId, String emailAddress, String password,
76 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
77 throws AuthException {
78
79 try {
80 return authenticate(
81 companyId, emailAddress, StringPool.BLANK, 0, password);
82 }
83 catch (Exception e) {
84 _log.error(e, e);
85
86 throw new AuthException(e);
87 }
88 }
89
90 public int authenticateByScreenName(
91 long companyId, String screenName, String password,
92 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
93 throws AuthException {
94
95 try {
96 return authenticate(
97 companyId, StringPool.BLANK, screenName, 0, password);
98 }
99 catch (Exception e) {
100 _log.error(e, e);
101
102 throw new AuthException(e);
103 }
104 }
105
106 public int authenticateByUserId(
107 long companyId, long userId, String password,
108 Map<String, String[]> headerMap, Map<String, String[]> parameterMap)
109 throws AuthException {
110
111 try {
112 return authenticate(
113 companyId, StringPool.BLANK, StringPool.BLANK, userId,
114 password);
115 }
116 catch (Exception e) {
117 _log.error(e, e);
118
119 throw new AuthException(e);
120 }
121 }
122
123 protected int authenticate(
124 long companyId, String emailAddress, String screenName, long userId,
125 String password)
126 throws Exception {
127
128 if (!PortalLDAPUtil.isAuthEnabled(companyId)) {
129 if (_log.isDebugEnabled()) {
130 _log.debug("Authenticator is not enabled");
131 }
132
133 return SUCCESS;
134 }
135
136 if (_log.isDebugEnabled()) {
137 _log.debug("Authenticator is enabled");
138 }
139
140
143 if (authenticateOmniadmin(companyId, emailAddress, userId) == SUCCESS) {
144 return SUCCESS;
145 }
146
147 String baseDN = PrefsPropsUtil.getString(
148 companyId, PropsKeys.LDAP_BASE_DN);
149
150 LdapContext ctx = PortalLDAPUtil.getContext(companyId);
151
152 if (ctx == null) {
153 return authenticateRequired(
154 companyId, userId, emailAddress, FAILURE);
155 }
156
157
159 String filter = PortalLDAPUtil.getAuthSearchFilter(
160 companyId, emailAddress, screenName, String.valueOf(userId));
161
162 try {
163 SearchControls cons = new SearchControls(
164 SearchControls.SUBTREE_SCOPE, 1, 0, null, false, false);
165
166 NamingEnumeration<SearchResult> enu = ctx.search(
167 baseDN, filter, cons);
168
169 if (enu.hasMoreElements()) {
170 if (_log.isDebugEnabled()) {
171 _log.debug("Search filter returned at least one result");
172 }
173
174 SearchResult result = enu.nextElement();
175
176 String fullUserDN = PortalLDAPUtil.getNameInNamespace(
177 companyId, result);
178
179 Attributes attrs = PortalLDAPUtil.getAttributes(
180 ctx, fullUserDN);
181
182 LDAPAuthResult ldapAuthResult = authenticate(
183 ctx, companyId, attrs, fullUserDN, password);
184
185
187 String errorMessage = ldapAuthResult.getErrorMessage();
188
189 if (errorMessage != null) {
190 if (errorMessage.indexOf(PrefsPropsUtil.getString(
191 companyId, PropsKeys.LDAP_ERROR_USER_LOCKOUT))
192 != -1) {
193
194 throw new UserLockoutException();
195 }
196 else if (errorMessage.indexOf(PrefsPropsUtil.getString(
197 companyId, PropsKeys.LDAP_ERROR_PASSWORD_EXPIRED))
198 != -1) {
199
200 throw new PasswordExpiredException();
201 }
202 }
203
204 if (!ldapAuthResult.isAuthenticated()) {
205 return authenticateRequired(
206 companyId, userId, emailAddress, FAILURE);
207 }
208
209
211 User user = PortalLDAPUtil.importLDAPUser(
212 companyId, ctx, attrs, password, true);
213
214
216 String resultCode = ldapAuthResult.getResponseControl();
217
218 if (resultCode.equals(LDAPAuth.RESULT_PASSWORD_RESET)) {
219 UserLocalServiceUtil.updatePasswordReset(
220 user.getUserId(), true);
221 }
222 else if (
223 resultCode.equals(LDAPAuth.RESULT_PASSWORD_EXP_WARNING)) {
224
225 UserLocalServiceUtil.updatePasswordReset(
226 user.getUserId(), true);
227 }
228 }
229 else {
230 if (_log.isDebugEnabled()) {
231 _log.debug("Search filter did not return any results");
232 }
233
234 return authenticateRequired(
235 companyId, userId, emailAddress, DNE);
236 }
237 }
238 catch (Exception e) {
239 _log.error("Problem accessing LDAP server: " + e.getMessage());
240
241 if (authenticateRequired(
242 companyId, userId, emailAddress, FAILURE) == FAILURE) {
243
244 throw e;
245 }
246 }
247 finally {
248 ctx.close();
249 }
250
251 return SUCCESS;
252 }
253
254 protected LDAPAuthResult authenticate(
255 LdapContext ctx, long companyId, Attributes attrs, String userDN,
256 String password)
257 throws Exception {
258
259 LDAPAuthResult ldapAuthResult = new LDAPAuthResult();
260
261
265 String authMethod = PrefsPropsUtil.getString(
266 companyId, PropsKeys.LDAP_AUTH_METHOD);
267
268 if (authMethod.equals(AUTH_METHOD_BIND)) {
269 try {
270 Hashtable<String, String> env =
271 (Hashtable<String, String>)ctx.getEnvironment();
272
273 env.put(Context.SECURITY_PRINCIPAL, userDN);
274 env.put(Context.SECURITY_CREDENTIALS, password);
275 env.put(
276 Context.REFERRAL,
277 PrefsPropsUtil.getString(
278 companyId, PropsKeys.LDAP_REFERRAL));
279
280 ctx = new InitialLdapContext(env, null);
281
282
284 Control[] responseControls = ctx.getResponseControls();
285
286 ldapAuthResult.setAuthenticated(true);
287 ldapAuthResult.setResponseControl(responseControls);
288 }
289 catch (Exception e) {
290 if (_log.isDebugEnabled()) {
291 _log.debug(
292 "Failed to bind to the LDAP server with userDN "
293 + userDN + " and password " + password);
294 }
295
296 _log.error(
297 "Failed to bind to the LDAP server: " + e.getMessage());
298
299 ldapAuthResult.setAuthenticated(false);
300 ldapAuthResult.setErrorMessage(e.getMessage());
301 }
302 }
303 else if (authMethod.equals(AUTH_METHOD_PASSWORD_COMPARE)) {
304 Attribute userPassword = attrs.get("userPassword");
305
306 if (userPassword != null) {
307 String ldapPassword = new String((byte[])userPassword.get());
308
309 String encryptedPassword = password;
310
311 String algorithm = PrefsPropsUtil.getString(
312 companyId,
313 PropsKeys.LDAP_AUTH_PASSWORD_ENCRYPTION_ALGORITHM);
314
315 if (Validator.isNotNull(algorithm)) {
316 encryptedPassword =
317 "{" + algorithm + "}" +
318 PwdEncryptor.encrypt(
319 algorithm, password, ldapPassword);
320 }
321
322 if (ldapPassword.equals(encryptedPassword)) {
323 ldapAuthResult.setAuthenticated(true);
324 }
325 else {
326 ldapAuthResult.setAuthenticated(false);
327
328 if (_log.isWarnEnabled()) {
329 _log.warn(
330 "Passwords do not match for userDN " + userDN);
331 }
332 }
333 }
334 }
335
336 return ldapAuthResult;
337 }
338
339 protected int authenticateOmniadmin(
340 long companyId, String emailAddress, long userId)
341 throws Exception {
342
343
345 if (PropsValues.AUTH_PIPELINE_ENABLE_LIFERAY_CHECK) {
346 if (userId > 0) {
347 if (OmniadminUtil.isOmniadmin(userId)) {
348 return SUCCESS;
349 }
350 }
351 else if (Validator.isNotNull(emailAddress)) {
352 try {
353 User user = UserLocalServiceUtil.getUserByEmailAddress(
354 companyId, emailAddress);
355
356 if (OmniadminUtil.isOmniadmin(user.getUserId())) {
357 return SUCCESS;
358 }
359 }
360 catch (NoSuchUserException nsue) {
361 }
362 }
363 }
364
365 return FAILURE;
366 }
367
368 protected int authenticateRequired(
369 long companyId, long userId, String emailAddress, int failureCode)
370 throws Exception {
371
372 if (PrefsPropsUtil.getBoolean(
373 companyId, PropsKeys.LDAP_AUTH_REQUIRED)) {
374
375 return failureCode;
376 }
377 else {
378 return SUCCESS;
379 }
380 }
381
382 private static Log _log = LogFactoryUtil.getLog(LDAPAuth.class);
383
384 }