Project

General

Profile

1 4080 daigle
/**
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$'
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.admin;
28
29
import java.io.IOException;
30 4547 daigle
import java.net.ConnectException;
31 4080 daigle
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 4590 daigle
import edu.ucsb.nceas.metacat.AuthSession;
42 4080 daigle
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 4547 daigle
import edu.ucsb.nceas.utilities.StringUtil;
51 4080 daigle
52
/**
53 4590 daigle
 * Control the display of the Authentication configuration page and the processing
54 4080 daigle
 * of the configuration values.
55
 */
56 4590 daigle
public class AuthAdmin extends MetaCatAdmin {
57 4080 daigle
58 4590 daigle
	private static AuthAdmin authAdmin = null;
59
	private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
60 4080 daigle
61
	/**
62
	 * private constructor since this is a singleton
63
	 */
64 4590 daigle
	private AuthAdmin() {}
65 4080 daigle
66
	/**
67
	 * Get the single instance of the MetaCatConfig.
68
	 *
69
	 * @return the single instance of MetaCatConfig
70
	 */
71 4590 daigle
	public static AuthAdmin getInstance() {
72
		if (authAdmin == null) {
73
			authAdmin = new AuthAdmin();
74 4080 daigle
		}
75 4590 daigle
		return authAdmin;
76 4080 daigle
	}
77
78
	/**
79 4590 daigle
	 * Handle configuration of the Authentication properties
80 4080 daigle
	 *
81
	 * @param request
82
	 *            the http request information
83
	 * @param response
84
	 *            the http response to be sent back to the client
85
	 */
86 4590 daigle
	public void configureAuth(HttpServletRequest request,
87 4080 daigle
			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 4159 daigle
				// use the metadata to construct the editing form
100 4590 daigle
				PropertiesMetaData metadata = PropertyService.getAuthMetaData();
101 4080 daigle
				request.setAttribute("metadata", metadata);
102
				request.setAttribute("groupMap", metadata.getGroups());
103
104 4590 daigle
				// add the list of auth options and their values to the request
105
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
106 4080 daigle
				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 4590 daigle
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
116 4080 daigle
				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 4590 daigle
						"/admin/auth-configuration.jsp");
128 4080 daigle
			} 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 4590 daigle
				PropertiesMetaData authMetaData = PropertyService
148
						.getAuthMetaData();
149 4080 daigle
150
				// process the fields for the global options (group 1)
151 4590 daigle
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
152 4080 daigle
						.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 4440 daigle
				try {
175
					FileUtil.createDirectory(backupDir);
176
				} catch (IOException ioe) {
177
					String errorString = "Could not create directory: " + backupDir
178
							+ " : " + ioe.getMessage();
179 4080 daigle
					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 4590 daigle
				PropertyService.persistAuthBackupProperties(request.getSession()
188 4080 daigle
						.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 4159 daigle
			}
196 4080 daigle
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 4590 daigle
					// 'authConfigured' option to 'true'
206
					PropertyService.setProperty("configutil.authConfigured",
207 4080 daigle
							PropertyService.CONFIGURED);
208
209
					// Reload the main metacat configuration page
210 4590 daigle
					processingSuccess.add("Authentication successfully configured");
211 4080 daigle
					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 4590 daigle
				throw new AdminException("IO problem while processing Authentication "
221 4080 daigle
						+ "properties page: " + ioe.getMessage());
222 4084 daigle
			} catch (GeneralPropertyException gpe) {
223
				String errorMessage = "Problem getting or setting property while "
224 4590 daigle
					+ "processing Authentication properties page: " + gpe.getMessage();
225 4084 daigle
				logMetacat.error(errorMessage);
226
				processingErrors.add(errorMessage);
227 4080 daigle
			}
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 4590 daigle
		String adminUsers = request.getParameter("auth.administrators");
241 4547 daigle
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
242 4080 daigle
243 4547 daigle
		try {
244 4590 daigle
			AuthSession authSession = new AuthSession();
245 4547 daigle
			for (String adminUser : adminUserList) {
246
				try {
247 4590 daigle
					authSession.getAttributes(adminUser);
248 4547 daigle
				} 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 4590 daigle
			errorVector.add("Instantiation error while verifying Metacat Administrators : "
260 4547 daigle
							+ ie.getMessage());
261 4590 daigle
		} catch (Exception e) {
262
			errorVector.add("Error while verifying Metacat Administrators : "
263
					+ e.getMessage());
264 4547 daigle
		}
265
266 4080 daigle
		return errorVector;
267
	}
268
}