1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements ldap configuration 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: daigle $'
|
9
|
* '$Date: 2008-11-19 15:25:56 -0800 (Wed, 19 Nov 2008) $'
|
10
|
* '$Revision: 4590 $'
|
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.admin;
|
28
|
|
29
|
import java.io.IOException;
|
30
|
import java.net.ConnectException;
|
31
|
import java.util.Set;
|
32
|
import java.util.SortedMap;
|
33
|
import java.util.Vector;
|
34
|
|
35
|
import javax.servlet.ServletException;
|
36
|
import javax.servlet.http.HttpServletRequest;
|
37
|
import javax.servlet.http.HttpServletResponse;
|
38
|
|
39
|
import org.apache.log4j.Logger;
|
40
|
|
41
|
import edu.ucsb.nceas.metacat.AuthSession;
|
42
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
43
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
44
|
import edu.ucsb.nceas.utilities.FileUtil;
|
45
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
46
|
import edu.ucsb.nceas.utilities.MetaDataProperty;
|
47
|
import edu.ucsb.nceas.utilities.PropertiesMetaData;
|
48
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
49
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
50
|
import edu.ucsb.nceas.utilities.StringUtil;
|
51
|
|
52
|
/**
|
53
|
* Control the display of the Authentication configuration page and the processing
|
54
|
* of the configuration values.
|
55
|
*/
|
56
|
public class AuthAdmin extends MetaCatAdmin {
|
57
|
|
58
|
private static AuthAdmin authAdmin = null;
|
59
|
private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
|
60
|
|
61
|
/**
|
62
|
* private constructor since this is a singleton
|
63
|
*/
|
64
|
private AuthAdmin() {}
|
65
|
|
66
|
/**
|
67
|
* Get the single instance of the MetaCatConfig.
|
68
|
*
|
69
|
* @return the single instance of MetaCatConfig
|
70
|
*/
|
71
|
public static AuthAdmin getInstance() {
|
72
|
if (authAdmin == null) {
|
73
|
authAdmin = new AuthAdmin();
|
74
|
}
|
75
|
return authAdmin;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Handle configuration of the Authentication properties
|
80
|
*
|
81
|
* @param request
|
82
|
* the http request information
|
83
|
* @param response
|
84
|
* the http response to be sent back to the client
|
85
|
*/
|
86
|
public void configureAuth(HttpServletRequest request,
|
87
|
HttpServletResponse response) throws AdminException {
|
88
|
|
89
|
String processForm = request.getParameter("processForm");
|
90
|
String formErrors = (String) request.getAttribute("formErrors");
|
91
|
|
92
|
if (processForm == null || !processForm.equals("true") || formErrors != null) {
|
93
|
// The servlet configuration parameters have not been set, or there
|
94
|
// were form errors on the last attempt to configure, so redirect to
|
95
|
// the web form for configuring metacat
|
96
|
|
97
|
try {
|
98
|
// Load the properties metadata file so that the JSP page can
|
99
|
// use the metadata to construct the editing form
|
100
|
PropertiesMetaData metadata = PropertyService.getAuthMetaData();
|
101
|
request.setAttribute("metadata", metadata);
|
102
|
request.setAttribute("groupMap", metadata.getGroups());
|
103
|
|
104
|
// add the list of auth options and their values to the request
|
105
|
Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
|
106
|
for (String name : propertyNames) {
|
107
|
request.setAttribute(name, PropertyService.getProperty(name));
|
108
|
}
|
109
|
|
110
|
// Check for any backup properties and apply them to the request.
|
111
|
// These are properties from previous configurations. They keep
|
112
|
// the user from having to re-enter all values when upgrading.
|
113
|
// If this is a first time install, getBackupProperties will return
|
114
|
// null.
|
115
|
SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
|
116
|
if (backupProperties != null) {
|
117
|
Vector<String> backupKeys = backupProperties.getPropertyNames();
|
118
|
for (String key : backupKeys) {
|
119
|
String value = backupProperties.getProperty(key);
|
120
|
if (value != null) {
|
121
|
request.setAttribute(key, value);
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
// Forward the request to the JSP page
|
126
|
RequestUtil.forwardRequest(request, response,
|
127
|
"/admin/auth-configuration.jsp");
|
128
|
} catch (PropertyNotFoundException pnfe) {
|
129
|
throw new AdminException("Problem getting property while initializing "
|
130
|
+ "LDAP properties page: " + pnfe.getMessage());
|
131
|
} catch (IOException ioe) {
|
132
|
throw new AdminException("IO problem while initializing "
|
133
|
+ "LDAP properties page:" + ioe.getMessage());
|
134
|
} catch (ServletException se) {
|
135
|
throw new AdminException("problem forwarding request while "
|
136
|
+ "initializing LDAP properties page: " + se.getMessage());
|
137
|
}
|
138
|
} else {
|
139
|
// The configuration form is being submitted and needs to be
|
140
|
// processed.
|
141
|
Vector<String> processingSuccess = new Vector<String>();
|
142
|
Vector<String> processingErrors = new Vector<String>();
|
143
|
Vector<String> validationErrors = new Vector<String>();
|
144
|
|
145
|
try {
|
146
|
// For each property, check if it is changed and save it
|
147
|
PropertiesMetaData authMetaData = PropertyService
|
148
|
.getAuthMetaData();
|
149
|
|
150
|
// process the fields for the global options (group 1)
|
151
|
SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
|
152
|
.getPropertiesInGroup(1);
|
153
|
Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
|
154
|
for (Integer globalPropertyIndex : globalPropertyIndexes) {
|
155
|
String globalPropertyKey = globalPropertyMap.get(
|
156
|
globalPropertyIndex).getKey();
|
157
|
PropertyService.checkAndSetProperty(request,
|
158
|
globalPropertyKey);
|
159
|
}
|
160
|
|
161
|
// we need to write the options from memory to the properties
|
162
|
// file
|
163
|
PropertyService.persistProperties();
|
164
|
|
165
|
// Validate that the options provided are legitimate. Note that
|
166
|
// we've allowed them to persist their entries. As of this point
|
167
|
// there is no other easy way to go back to the configure form
|
168
|
// and preserve their entries.
|
169
|
validationErrors.addAll(validateOptions(request));
|
170
|
|
171
|
// Try to create data file and backup directories if
|
172
|
// necessary.
|
173
|
String backupDir = PropertyService.getBackupDir();
|
174
|
try {
|
175
|
FileUtil.createDirectory(backupDir);
|
176
|
} catch (IOException ioe) {
|
177
|
String errorString = "Could not create directory: " + backupDir
|
178
|
+ " : " + ioe.getMessage();
|
179
|
logMetacat.error(errorString);
|
180
|
validationErrors.add(errorString);
|
181
|
}
|
182
|
|
183
|
// Write out the configurable properties to a backup file
|
184
|
// outside the install directory. Note that we allow them to
|
185
|
// do this even if they have validation errors. They will
|
186
|
// need to go back and fix the errors before they can run metacat.
|
187
|
PropertyService.persistAuthBackupProperties(request.getSession()
|
188
|
.getServletContext());
|
189
|
|
190
|
} catch (GeneralPropertyException gpe) {
|
191
|
String errorMessage = "Problem getting or setting property while "
|
192
|
+ "processing LDAP properties page: " + gpe.getMessage();
|
193
|
logMetacat.error(errorMessage);
|
194
|
processingErrors.add(errorMessage);
|
195
|
}
|
196
|
|
197
|
try {
|
198
|
if (validationErrors.size() > 0 || processingErrors.size() > 0) {
|
199
|
RequestUtil.clearRequestMessages(request);
|
200
|
RequestUtil.setRequestFormErrors(request, validationErrors);
|
201
|
RequestUtil.setRequestErrors(request, processingErrors);
|
202
|
RequestUtil.forwardRequest(request, response, "/admin");
|
203
|
} else {
|
204
|
// Now that the options have been set, change the
|
205
|
// 'authConfigured' option to 'true'
|
206
|
PropertyService.setProperty("configutil.authConfigured",
|
207
|
PropertyService.CONFIGURED);
|
208
|
|
209
|
// Reload the main metacat configuration page
|
210
|
processingSuccess.add("Authentication successfully configured");
|
211
|
RequestUtil.clearRequestMessages(request);
|
212
|
RequestUtil.setRequestSuccess(request, processingSuccess);
|
213
|
RequestUtil.forwardRequest(request, response,
|
214
|
"/admin?configureType=configure&processForm=false");
|
215
|
}
|
216
|
} catch (ServletException se) {
|
217
|
throw new AdminException("problem forwarding request while "
|
218
|
+ "processing LDAP properties page: " + se.getMessage());
|
219
|
} catch (IOException ioe) {
|
220
|
throw new AdminException("IO problem while processing Authentication "
|
221
|
+ "properties page: " + ioe.getMessage());
|
222
|
} catch (GeneralPropertyException gpe) {
|
223
|
String errorMessage = "Problem getting or setting property while "
|
224
|
+ "processing Authentication properties page: " + gpe.getMessage();
|
225
|
logMetacat.error(errorMessage);
|
226
|
processingErrors.add(errorMessage);
|
227
|
}
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Validate the most important configuration options submitted by the user.
|
233
|
*
|
234
|
* @return a vector holding error message for any fields that fail
|
235
|
* validation.
|
236
|
*/
|
237
|
protected Vector<String> validateOptions(HttpServletRequest request) {
|
238
|
Vector<String> errorVector = new Vector<String>();
|
239
|
|
240
|
String adminUsers = request.getParameter("auth.administrators");
|
241
|
Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
|
242
|
|
243
|
try {
|
244
|
AuthSession authSession = new AuthSession();
|
245
|
for (String adminUser : adminUserList) {
|
246
|
try {
|
247
|
authSession.getAttributes(adminUser);
|
248
|
} catch (ConnectException ce) {
|
249
|
if (ce.getMessage() != null
|
250
|
&& ce.getMessage().contains("NameNotFoundException")) {
|
251
|
errorVector.add("User : " + adminUser + " is not in LDAP.");
|
252
|
} else {
|
253
|
errorVector.add("Connection error while verifying Metacat " +
|
254
|
"Administrators : " + ce.getMessage());
|
255
|
}
|
256
|
}
|
257
|
}
|
258
|
} catch (InstantiationException ie) {
|
259
|
errorVector.add("Instantiation error while verifying Metacat Administrators : "
|
260
|
+ ie.getMessage());
|
261
|
} catch (Exception e) {
|
262
|
errorVector.add("Error while verifying Metacat Administrators : "
|
263
|
+ e.getMessage());
|
264
|
}
|
265
|
|
266
|
return errorVector;
|
267
|
}
|
268
|
}
|