Project

General

Profile

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: 2009-01-14 16:05:37 -0800 (Wed, 14 Jan 2009) $'
10
 * '$Revision: 4751 $'
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.GeneralPropertyException;
45
import edu.ucsb.nceas.utilities.MetaDataProperty;
46
import edu.ucsb.nceas.utilities.PropertiesMetaData;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48
import edu.ucsb.nceas.utilities.SortedProperties;
49
import edu.ucsb.nceas.utilities.StringUtil;
50

    
51
/**
52
 * Control the display of the Authentication configuration page and the processing
53
 * of the configuration values.
54
 */
55
public class AuthAdmin extends MetaCatAdmin {
56

    
57
	private static AuthAdmin authAdmin = null;
58
	private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
59

    
60
	/**
61
	 * private constructor since this is a singleton
62
	 */
63
	private AuthAdmin() {}
64

    
65
	/**
66
	 * Get the single instance of the MetaCatConfig.
67
	 * 
68
	 * @return the single instance of MetaCatConfig
69
	 */
70
	public static AuthAdmin getInstance() {
71
		if (authAdmin == null) {
72
			authAdmin = new AuthAdmin();
73
		}
74
		return authAdmin;
75
	}
76
	
77
	/**
78
	 * Handle configuration of the Authentication properties
79
	 * 
80
	 * @param request
81
	 *            the http request information
82
	 * @param response
83
	 *            the http response to be sent back to the client
84
	 */
85
	public void configureAuth(HttpServletRequest request,
86
			HttpServletResponse response) throws AdminException {
87

    
88
		String processForm = request.getParameter("processForm");
89
		String formErrors = (String) request.getAttribute("formErrors");
90

    
91
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
92
			// The servlet configuration parameters have not been set, or there
93
			// were form errors on the last attempt to configure, so redirect to
94
			// the web form for configuring metacat
95
			
96
			try {
97
				// Load the properties metadata file so that the JSP page can
98
				// use the metadata to construct the editing form
99
				PropertiesMetaData metadata = PropertyService.getAuthMetaData();
100
				request.setAttribute("metadata", metadata);
101
				request.setAttribute("groupMap", metadata.getGroups());
102

    
103
				// add the list of auth options and their values to the request
104
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
105
				for (String name : propertyNames) {
106
					request.setAttribute(name, PropertyService.getProperty(name));
107
				} 
108
				
109
				// add the list of organization options and their values to the request.  This is because
110
				// currently we use the organization.unaffiliated values to configure metacat client for 
111
				// password change and account creation.  Eventually, these should get moved to organization
112
				// level configuration.
113
				Vector<String> orgPropertyNames = PropertyService.getPropertyNamesByGroup("organization");
114
				for (String name : orgPropertyNames) {
115
					request.setAttribute(name, PropertyService.getProperty(name));
116
				} 
117

    
118
				// Check for any backup properties and apply them to the request.
119
				// These are properties from previous configurations. They keep
120
				// the user from having to re-enter all values when upgrading.
121
				// If this is a first time install, getBackupProperties will return
122
				// null.
123
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
124
				if (backupProperties != null) {
125
					Vector<String> backupKeys = backupProperties.getPropertyNames();
126
					for (String key : backupKeys) {
127
						String value = backupProperties.getProperty(key);
128
						if (value != null) {
129
							request.setAttribute(key, value);
130
						}
131
					}
132
				}
133
				// Forward the request to the JSP page
134
				RequestUtil.forwardRequest(request, response,
135
						"/admin/auth-configuration.jsp");
136
			} catch (PropertyNotFoundException pnfe) {
137
				throw new AdminException("Problem getting property while initializing "
138
						+ "LDAP properties page: " + pnfe.getMessage());
139
			} catch (IOException ioe) {
140
				throw new AdminException("IO problem while initializing "
141
						+ "LDAP properties page:" + ioe.getMessage());
142
			} catch (ServletException se) {
143
				throw new AdminException("problem forwarding request while " 
144
						+ "initializing LDAP properties page: " + se.getMessage());
145
			}
146
		} else {
147
			// The configuration form is being submitted and needs to be
148
			// processed.
149
			Vector<String> processingSuccess = new Vector<String>();
150
			Vector<String> processingErrors = new Vector<String>();
151
			Vector<String> validationErrors = new Vector<String>();
152

    
153
			try {
154
				// For each property, check if it is changed and save it
155
				PropertiesMetaData authMetaData = PropertyService
156
						.getAuthMetaData();
157

    
158
				// process the fields for the global options (group 1)
159
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
160
						.getPropertiesInGroup(1);
161
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
162
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
163
					String globalPropertyKey = globalPropertyMap.get(
164
							globalPropertyIndex).getKey();
165
					PropertyService.checkAndSetProperty(request,
166
							globalPropertyKey);
167
				}
168
				
169
				// process the fields for the global options (group 1)
170
				SortedMap<Integer, MetaDataProperty> authClientPropertyMap = authMetaData
171
						.getPropertiesInGroup(2);
172
				Set<Integer> authClientPropertyIndexes = authClientPropertyMap.keySet();
173
				for (Integer authClientPropertyIndex : authClientPropertyIndexes) {
174
					String authClientPropertyKey = authClientPropertyMap.get(
175
							authClientPropertyIndex).getKey();
176
					PropertyService.checkAndSetProperty(request,
177
							authClientPropertyKey);
178
				}
179

    
180
				// we need to write the options from memory to the properties
181
				// file
182
				PropertyService.persistProperties();
183

    
184
				// Validate that the options provided are legitimate. Note that
185
				// we've allowed them to persist their entries. As of this point
186
				// there is no other easy way to go back to the configure form
187
				// and preserve their entries.
188
				validationErrors.addAll(validateOptions(request));
189

    
190

    
191
				// Write out the configurable properties to a backup file
192
				// outside the install directory.  Note that we allow them to
193
				// do this even if they have validation errors.  They will
194
				// need to go back and fix the errors before they can run metacat.
195
				
196
				// This is a special case, since it is possible that the backup directory
197
				// may not have been specified yet.  If not, authentication values need to be
198
				// persisted by the BackupAdmin when the backup directory is specified.
199
				String backupDir = PropertyService.getProperty("application.backupDir");
200
				if (backupDir != null) {
201
					PropertyService.persistAuthBackupProperties(request.getSession()
202
						.getServletContext());
203
				}
204
			
205
			} catch (GeneralPropertyException gpe) {
206
				String errorMessage = "Problem getting or setting property while "
207
					+ "processing LDAP properties page: " + gpe.getMessage();
208
				logMetacat.error(errorMessage);
209
				processingErrors.add(errorMessage);
210
			} 
211
			
212
			try {
213
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
214
					RequestUtil.clearRequestMessages(request);
215
					RequestUtil.setRequestFormErrors(request, validationErrors);
216
					RequestUtil.setRequestErrors(request, processingErrors);
217
					RequestUtil.forwardRequest(request, response, "/admin");
218
				} else {
219
					// Now that the options have been set, change the
220
					// 'authConfigured' option to 'true'
221
					PropertyService.setProperty("configutil.authConfigured",
222
							PropertyService.CONFIGURED);
223
					
224
					// Reload the main metacat configuration page
225
					processingSuccess.add("Authentication successfully configured");
226
					RequestUtil.clearRequestMessages(request);
227
					RequestUtil.setRequestSuccess(request, processingSuccess);
228
					RequestUtil.forwardRequest(request, response,
229
							"/admin?configureType=configure&processForm=false");
230
				}
231
			} catch (ServletException se) {
232
				throw new AdminException("problem forwarding request while "
233
						+ "processing LDAP properties page: " + se.getMessage());
234
			} catch (IOException ioe) {
235
				throw new AdminException("IO problem while processing Authentication "
236
						+ "properties page: " + ioe.getMessage());
237
			} catch (GeneralPropertyException gpe) {
238
				String errorMessage = "Problem getting or setting property while "
239
					+ "processing Authentication properties page: " + gpe.getMessage();
240
				logMetacat.error(errorMessage);
241
				processingErrors.add(errorMessage);
242
			}
243
		}
244
	}
245
	
246
	/**
247
	 * Validate the most important configuration options submitted by the user.
248
	 * 
249
	 * @return a vector holding error message for any fields that fail
250
	 *         validation.
251
	 */
252
	protected Vector<String> validateOptions(HttpServletRequest request) {
253
		Vector<String> errorVector = new Vector<String>();
254

    
255
		String adminUsers = request.getParameter("auth.administrators");
256
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
257

    
258
		try {
259
			AuthSession authSession = new AuthSession();
260
			for (String adminUser : adminUserList) {
261
				try {
262
					authSession.getAttributes(adminUser);
263
				} catch (ConnectException ce) {
264
					if (ce.getMessage() != null
265
							&& ce.getMessage().contains("NameNotFoundException")) {
266
						errorVector.add("User : " + adminUser + " is not in LDAP.");
267
					} else {
268
						errorVector.add("Connection error while verifying Metacat " + 
269
								"Administrators : " + ce.getMessage());
270
					}
271
				}
272
			}
273
		} catch (InstantiationException ie) {
274
			errorVector.add("Instantiation error while verifying Metacat Administrators : "
275
							+ ie.getMessage());
276
		} catch (Exception e) {
277
			errorVector.add("Error while verifying Metacat Administrators : "
278
					+ e.getMessage());
279
		}
280

    
281
		return errorVector;
282
	}
283
}
(2-2/10)