1 |
4080
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements administrative methods
|
4 |
|
|
* Copyright: 2008 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Michael Daigle
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package edu.ucsb.nceas.metacat.util;
|
28 |
|
|
|
29 |
|
|
import java.util.Calendar;
|
30 |
|
|
import java.util.Vector;
|
31 |
|
|
|
32 |
|
|
import javax.naming.AuthenticationException;
|
33 |
|
|
import javax.naming.NamingException;
|
34 |
|
|
import javax.servlet.http.HttpServletRequest;
|
35 |
|
|
import javax.servlet.http.HttpSession;
|
36 |
|
|
|
37 |
|
|
import edu.ucsb.nceas.metacat.AuthLdap;
|
38 |
|
|
import edu.ucsb.nceas.metacat.AuthSession;
|
39 |
|
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
40 |
|
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
41 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
42 |
|
|
import edu.ucsb.nceas.utilities.StringUtil;
|
43 |
|
|
|
44 |
|
|
public class LDAPUtil {
|
45 |
|
|
|
46 |
|
|
private static Vector<String> administrators = null;
|
47 |
|
|
private static Vector<String> moderators = null;
|
48 |
|
|
private static Vector<String> allowedSubmitters = null;
|
49 |
|
|
private static Vector<String> deniedSubmitters = null;
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* private constructor - all methods are static so there is no no need to
|
53 |
|
|
* instantiate.
|
54 |
|
|
*/
|
55 |
|
|
private LDAPUtil() {}
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Get the administrators from metacat.properties
|
59 |
|
|
*
|
60 |
|
|
* @return a Vector of Strings holding the administrators
|
61 |
|
|
*/
|
62 |
|
|
public static Vector<String> getAdministrators() throws UtilException {
|
63 |
|
|
if (administrators == null) {
|
64 |
|
|
populateAdministrators();
|
65 |
|
|
}
|
66 |
|
|
return administrators;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Get the allowed submitters from metacat.properties
|
71 |
|
|
*
|
72 |
|
|
* @return a Vector of Strings holding the submitters
|
73 |
|
|
*/
|
74 |
|
|
public static Vector<String> getAllowedSubmitters() throws UtilException {
|
75 |
|
|
if (allowedSubmitters == null) {
|
76 |
|
|
populateAllowedSubmitters();
|
77 |
|
|
}
|
78 |
|
|
return allowedSubmitters;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Get the denied submitters from metacat.properties
|
83 |
|
|
*
|
84 |
|
|
* @return a Vector of Strings holding the denied submitters
|
85 |
|
|
*/
|
86 |
|
|
public static Vector<String> getDeniedSubmitters() throws UtilException {
|
87 |
|
|
if (deniedSubmitters == null) {
|
88 |
|
|
populateDeniedSubmitters();
|
89 |
|
|
}
|
90 |
|
|
return deniedSubmitters;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Get the vector of administrator credentials from metacat.properties
|
95 |
|
|
* and put into global administrators list
|
96 |
|
|
*/
|
97 |
|
|
private static void populateAdministrators() throws UtilException {
|
98 |
|
|
String administratorString = null;
|
99 |
|
|
try {
|
100 |
|
|
administratorString =
|
101 |
|
|
PropertyService.getProperty("ldap.administrators");
|
102 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
103 |
|
|
throw new UtilException("Could not get metacat property: ldap.administrators. "
|
104 |
|
|
+ "There will be no registered metacat adminstrators: "
|
105 |
|
|
+ pnfe.getMessage());
|
106 |
|
|
}
|
107 |
|
|
administrators = StringUtil.toVector(administratorString, ':');
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Get the vector of allowed submitter credentials from metacat.properties
|
112 |
|
|
* and put into global allowedSubmitters list
|
113 |
|
|
*/
|
114 |
|
|
private static void populateAllowedSubmitters() throws UtilException {
|
115 |
|
|
String allowedSubmitterString = null;
|
116 |
|
|
try {
|
117 |
|
|
allowedSubmitterString = PropertyService.getProperty("ldap.allowedSubmitters");
|
118 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
119 |
|
|
throw new UtilException("Could not get metacat property: ldap.allowedSubmitters. "
|
120 |
|
|
+ "Anyone will be allowed to submit: "
|
121 |
|
|
+ pnfe.getMessage());
|
122 |
|
|
}
|
123 |
|
|
allowedSubmitters = StringUtil.toVector(allowedSubmitterString, ':');
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
/**
|
127 |
|
|
* Get the vector of denied submitter credentials from metacat.properties
|
128 |
|
|
* and put into global deniedSubmitters list
|
129 |
|
|
*/
|
130 |
|
|
private static void populateDeniedSubmitters() throws UtilException {
|
131 |
|
|
String deniedSubmitterString = null;
|
132 |
|
|
try {
|
133 |
|
|
deniedSubmitterString = PropertyService.getProperty("ldap.deniedSubmitters");
|
134 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
135 |
|
|
throw new UtilException("Could not get metacat property: ldap.deniedSubmitters: "
|
136 |
|
|
+ pnfe.getMessage());
|
137 |
|
|
}
|
138 |
|
|
deniedSubmitters = StringUtil.toVector(deniedSubmitterString, ':');
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
* Validate connectivity to the ldap server. This does not test user
|
143 |
|
|
* authentication. Validation methods return a string error message if there
|
144 |
|
|
* is an issue. This allows the calling code to run several validations and
|
145 |
|
|
* compile the errors into a list that can be displayed on a web page if
|
146 |
|
|
* desired.
|
147 |
|
|
*
|
148 |
|
|
* @param ldapurl
|
149 |
|
|
* the url of the ldap server
|
150 |
|
|
* @param ldapbase
|
151 |
|
|
* the ldap base value to test
|
152 |
|
|
* @return a string holding error message if validation fails.
|
153 |
|
|
*/
|
154 |
|
|
public static String validateLDAPConnectivity(String ldapurl,
|
155 |
|
|
String ldapbase) {
|
156 |
|
|
try {
|
157 |
|
|
AuthLdap authLdap = new AuthLdap();
|
158 |
|
|
authLdap.testCredentials(
|
159 |
|
|
"uid=bogusname,o=NCEAS,dc=ecoinformatics,dc=org",
|
160 |
|
|
"boguspassword", ldapurl, ldapbase);
|
161 |
|
|
} catch (AuthenticationException ae) {
|
162 |
|
|
// Do nothing here. We are using dummy uid and password, so we
|
163 |
|
|
// expect authentication exceptions
|
164 |
|
|
} catch (javax.naming.InvalidNameException ine) {
|
165 |
|
|
return "An invalid domain name was provided: " + ine.getMessage();
|
166 |
|
|
} catch (NamingException ne) {
|
167 |
|
|
return "An invalid ldap name was provided: " + ne.getMessage();
|
168 |
|
|
} catch (InstantiationException ie) {
|
169 |
|
|
return "Could not instantiate AuthLdap: " + ie.getMessage();
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
return null;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/**
|
176 |
|
|
* log the user in against ldap. If the login is successful, add
|
177 |
|
|
* the session information to the session list in SessionUtil.
|
178 |
|
|
*
|
179 |
|
|
* @param request the http request.
|
180 |
|
|
*/
|
181 |
|
|
public static boolean logUserIn(HttpServletRequest request, String userName, String organization,
|
182 |
|
|
Vector<String> dnList, String password) throws UtilException {
|
183 |
|
|
AuthSession authSession = null;
|
184 |
|
|
|
185 |
|
|
// make sure we have username and password.
|
186 |
|
|
if (userName == null || password == null || dnList == null || dnList.size() == 0) {
|
187 |
|
|
throw new UtilException("null username, password, or dn list when logging user in");
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// put the login credentials into an LDAP string
|
191 |
|
|
String ldapString = createLDAPString(userName, organization, dnList);
|
192 |
|
|
|
193 |
|
|
// Create auth session
|
194 |
|
|
try {
|
195 |
|
|
authSession = new AuthSession();
|
196 |
|
|
} catch (Exception e) {
|
197 |
|
|
throw new UtilException("Could not instantiate AuthSession: "
|
198 |
|
|
+ e.getMessage());
|
199 |
|
|
}
|
200 |
|
|
// authenticate user against ldap
|
201 |
|
|
boolean isValid = authSession.authenticate(request, ldapString,
|
202 |
|
|
password);
|
203 |
|
|
|
204 |
|
|
// if login was successful, add the session information to the
|
205 |
|
|
// global session list.
|
206 |
|
|
if (isValid) {
|
207 |
|
|
HttpSession session = authSession.getSessions();
|
208 |
|
|
String sessionId = session.getId();
|
209 |
|
|
SessionService.registerSession(sessionId,
|
210 |
|
|
(String) session.getAttribute("username"),
|
211 |
4298
|
daigle
|
(String[]) session.getAttribute("groupnames"),
|
212 |
4080
|
daigle
|
(String) session.getAttribute("password"));
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
return isValid;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
/**
|
219 |
|
|
* Checks to see if the user is logged in by grabbing the session from the
|
220 |
|
|
* request and seeing if it exists in the global session list.
|
221 |
|
|
*
|
222 |
|
|
* @param request the http request that holds the login session
|
223 |
|
|
* @return boolean that is true if the user is logged in, false otherwise
|
224 |
|
|
*/
|
225 |
|
|
public static boolean isUserLoggedIn(HttpServletRequest request) throws UtilException{
|
226 |
|
|
SessionData sessionData = null;
|
227 |
|
|
String sessionId = request.getSession().getId();
|
228 |
|
|
|
229 |
|
|
try {
|
230 |
|
|
|
231 |
|
|
if (sessionId != null && SessionService.isSessionRegistered(sessionId)) {
|
232 |
|
|
// get the registered session data
|
233 |
|
|
sessionData = SessionService.getRegisteredSession(sessionId);
|
234 |
|
|
|
235 |
|
|
// get the timeout limit
|
236 |
|
|
String sessionTimeout = PropertyService.getProperty("auth.timeoutMinutes");
|
237 |
|
|
int sessionTimeoutInt = Integer.parseInt(sessionTimeout);
|
238 |
|
|
|
239 |
|
|
// get the last time the session was accessed
|
240 |
|
|
Calendar lastAccessedTime = sessionData.getLastAccessedTime();
|
241 |
|
|
// get the current time and set back "sessionTimoutInt" minutes
|
242 |
|
|
Calendar now = Calendar.getInstance();
|
243 |
|
|
now.add(Calendar.MINUTE, 0 - sessionTimeoutInt);
|
244 |
|
|
|
245 |
|
|
// if the last accessed time is before now minus the timeout,
|
246 |
|
|
// the session has expired. Unregister it and return false.
|
247 |
|
|
if (lastAccessedTime.before(now)) {
|
248 |
|
|
SessionService.unRegisterSession(sessionId);
|
249 |
|
|
return false;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
return true;
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
256 |
|
|
throw new UtilException("Could not determine if user is logged in because "
|
257 |
|
|
+ "of property error: " + pnfe.getMessage());
|
258 |
|
|
} catch (NumberFormatException nfe) {
|
259 |
|
|
throw new UtilException("Could not determine if user is logged in because "
|
260 |
|
|
+ "of number conversion error: " + nfe.getMessage());
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
return false;
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* Checks to see if the user is logged in as admin by first checking if the
|
268 |
|
|
* user is logged in and then seeing if the user's account is on the
|
269 |
|
|
* administrators list in metacat.properties.
|
270 |
|
|
*
|
271 |
|
|
* @param request
|
272 |
|
|
* the http request that holds the login session
|
273 |
|
|
* @return boolean that is true if the user is logged in as admin, false
|
274 |
|
|
* otherwise
|
275 |
|
|
*/
|
276 |
|
|
public static boolean isUserLoggedInAsAdmin(HttpServletRequest request) throws UtilException {
|
277 |
|
|
if (!isUserLoggedIn(request)) {
|
278 |
|
|
return false;
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
String userName = getUserName(request);
|
282 |
|
|
boolean isAdmin = isAdministrator(userName, null);
|
283 |
|
|
|
284 |
|
|
return isAdmin;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* Gets the user name from the login session on the http request
|
289 |
|
|
*
|
290 |
|
|
* @param request
|
291 |
|
|
* the http request that holds the login session
|
292 |
|
|
* @return String that holds the user name
|
293 |
|
|
*/
|
294 |
|
|
public static String getUserName(HttpServletRequest request) {
|
295 |
|
|
String userName = (String)request.getSession().getAttribute("username");
|
296 |
|
|
|
297 |
|
|
return userName;
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
/**
|
301 |
|
|
* Gets the user group names from the login session on the http request
|
302 |
|
|
*
|
303 |
|
|
* @param request
|
304 |
|
|
* the http request that holds the login session
|
305 |
|
|
* @return String array that holds the user groups
|
306 |
|
|
*/
|
307 |
|
|
public static String[] getGroupNames(HttpServletRequest request) {
|
308 |
|
|
String sessionId = request.getSession().getId();;
|
309 |
|
|
SessionData sessionData = SessionService.getRegisteredSession(sessionId);
|
310 |
|
|
String[] groupNames = { "" };
|
311 |
|
|
|
312 |
|
|
if (sessionData != null) {
|
313 |
|
|
groupNames = sessionData.getGroupNames();
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
return groupNames;
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
/**
|
320 |
|
|
* Creates an ldap credentail string from the username, organization
|
321 |
|
|
* and dn list.
|
322 |
|
|
*
|
323 |
|
|
* @param username the user name
|
324 |
|
|
* @param organization the organization
|
325 |
|
|
* @param dnList a list of dns
|
326 |
|
|
* @return String holding the ldap login string
|
327 |
|
|
*/
|
328 |
|
|
public static String createLDAPString(String username, String organization,
|
329 |
|
|
Vector<String> dnList) throws UtilException {
|
330 |
|
|
|
331 |
|
|
if (username == null || organization == null || dnList == null || dnList.size() == 0) {
|
332 |
|
|
throw new UtilException("Could not generate LDAP user string. One of the following is null: username, organization or dnlist");
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
String ldapString = "uid=" + username + ",o=" + organization;
|
336 |
|
|
|
337 |
|
|
for (String dn : dnList) {
|
338 |
|
|
ldapString += "," + dn;
|
339 |
|
|
}
|
340 |
|
|
|
341 |
|
|
return ldapString;
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
/**
|
345 |
|
|
* Reports whether LDAP is fully configured.
|
346 |
|
|
*
|
347 |
|
|
* @return a boolean that is true if all sections are configured and false
|
348 |
|
|
* otherwise
|
349 |
|
|
*/
|
350 |
|
|
public static boolean isLDAPConfigured() throws UtilException {
|
351 |
|
|
String ldapConfiguredString = PropertyService.UNCONFIGURED;
|
352 |
|
|
try {
|
353 |
|
|
ldapConfiguredString = PropertyService.getProperty("configutil.ldapConfigured");
|
354 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
355 |
|
|
throw new UtilException("Could not determine if LDAP is configured: "
|
356 |
|
|
+ pnfe.getMessage());
|
357 |
|
|
}
|
358 |
|
|
return !ldapConfiguredString.equals(PropertyService.UNCONFIGURED);
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
/**
|
362 |
|
|
* Check if the specified user is part of the administrators list
|
363 |
|
|
*
|
364 |
|
|
* @param username
|
365 |
|
|
* the user login credentails
|
366 |
|
|
* @param groups
|
367 |
|
|
* a list of the user's groups
|
368 |
|
|
*/
|
369 |
|
|
public static boolean isAdministrator(String username, String[] groups)
|
370 |
|
|
throws UtilException {
|
371 |
|
|
return onAccessList(getAdministrators(), username, groups);
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
/**
|
375 |
|
|
* Check if the specified user is part of the moderators list
|
376 |
|
|
*
|
377 |
|
|
* @param username
|
378 |
|
|
* the user login credentails
|
379 |
|
|
* @param groups
|
380 |
|
|
* a list of the user's groups
|
381 |
|
|
*/
|
382 |
|
|
public static boolean isModerator(String username, String[] groups) {
|
383 |
|
|
return onAccessList(moderators, username, groups);
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
/**
|
387 |
|
|
* Check if the specified user is part of the moderators list
|
388 |
|
|
*
|
389 |
|
|
* @param username
|
390 |
|
|
* the user login credentails
|
391 |
|
|
* @param groups
|
392 |
|
|
* a list of the user's groups
|
393 |
|
|
*/
|
394 |
|
|
public static boolean isAllowedSubmitter(String username, String[] groups)
|
395 |
|
|
throws UtilException {
|
396 |
|
|
if (getAllowedSubmitters().size() == 0) {
|
397 |
|
|
// no allowedSubmitters list specified -
|
398 |
|
|
// hence everyone should be allowed
|
399 |
|
|
return true;
|
400 |
|
|
}
|
401 |
|
|
return (onAccessList(getAllowedSubmitters(), username, groups));
|
402 |
|
|
}
|
403 |
|
|
|
404 |
|
|
/**
|
405 |
|
|
* Check if the specified user is part of the moderators list
|
406 |
|
|
*
|
407 |
|
|
* @param username
|
408 |
|
|
* the user login credentails
|
409 |
|
|
* @param groups
|
410 |
|
|
* a list of the user's groups
|
411 |
|
|
*/
|
412 |
|
|
public static boolean isDeniedSubmitter(String username, String[] groups)
|
413 |
|
|
throws UtilException {
|
414 |
|
|
return (onAccessList(getDeniedSubmitters(), username, groups));
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* Check if the specified user can insert the document
|
419 |
|
|
*
|
420 |
|
|
* @param username
|
421 |
|
|
* the user login credentails
|
422 |
|
|
* @param groups
|
423 |
|
|
* a list of the user's groups
|
424 |
|
|
*/
|
425 |
|
|
public static boolean canInsertOrUpdate(String username, String[] groups)
|
426 |
|
|
throws UtilException {
|
427 |
|
|
return (isAllowedSubmitter(username, groups) && !isDeniedSubmitter(username,
|
428 |
|
|
groups));
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
/**
|
432 |
|
|
* Check if the user is on a given access list. This is true if either the
|
433 |
|
|
* user or the user's group is on the list.
|
434 |
|
|
*
|
435 |
|
|
* @param accessList the list we want to check against
|
436 |
|
|
* @param username the name of the user we want to check
|
437 |
|
|
* @param groups a list of the user's groups
|
438 |
|
|
*/
|
439 |
|
|
private static boolean onAccessList(Vector<String> accessList, String username,
|
440 |
|
|
String[] groups) {
|
441 |
|
|
|
442 |
|
|
// this should never happen. All calls to this method should use the
|
443 |
|
|
// appropriate getter to retrieve the accessList. That should guarentee
|
444 |
|
|
// that the access is at least an empty Vector.
|
445 |
|
|
if (accessList == null) {
|
446 |
|
|
return false;
|
447 |
|
|
}
|
448 |
|
|
|
449 |
|
|
// Check that the user is authenticated as an administrator account
|
450 |
|
|
for (String accessString : accessList) {
|
451 |
|
|
// check the given admin dn is a group dn...
|
452 |
|
|
if (groups != null && accessString.startsWith("cn=")) {
|
453 |
|
|
// is a group dn
|
454 |
|
|
for (int j = 0; j < groups.length; j++) {
|
455 |
|
|
if (groups[j].equals(accessString)) {
|
456 |
|
|
return true;
|
457 |
|
|
}
|
458 |
|
|
}
|
459 |
|
|
} else {
|
460 |
|
|
// is a user dn
|
461 |
|
|
if (username != null && username.equals(accessString)) {
|
462 |
|
|
return true;
|
463 |
|
|
}
|
464 |
|
|
}
|
465 |
|
|
}
|
466 |
|
|
return false;
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
}
|