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: 2008-12-26 13:15:19 -0800 (Fri, 26 Dec 2008) $'
10
 * '$Revision: 4705 $'
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
				// Check for any backup properties and apply them to the request.
110
				// These are properties from previous configurations. They keep
111
				// the user from having to re-enter all values when upgrading.
112
				// If this is a first time install, getBackupProperties will return
113
				// null.
114
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
115
				if (backupProperties != null) {
116
					Vector<String> backupKeys = backupProperties.getPropertyNames();
117
					for (String key : backupKeys) {
118
						String value = backupProperties.getProperty(key);
119
						if (value != null) {
120
							request.setAttribute(key, value);
121
						}
122
					}
123
				}
124
				// Forward the request to the JSP page
125
				RequestUtil.forwardRequest(request, response,
126
						"/admin/auth-configuration.jsp");
127
			} catch (PropertyNotFoundException pnfe) {
128
				throw new AdminException("Problem getting property while initializing "
129
						+ "LDAP properties page: " + pnfe.getMessage());
130
			} catch (IOException ioe) {
131
				throw new AdminException("IO problem while initializing "
132
						+ "LDAP properties page:" + ioe.getMessage());
133
			} catch (ServletException se) {
134
				throw new AdminException("problem forwarding request while " 
135
						+ "initializing LDAP properties page: " + se.getMessage());
136
			}
137
		} else {
138
			// The configuration form is being submitted and needs to be
139
			// processed.
140
			Vector<String> processingSuccess = new Vector<String>();
141
			Vector<String> processingErrors = new Vector<String>();
142
			Vector<String> validationErrors = new Vector<String>();
143

    
144
			try {
145
				// For each property, check if it is changed and save it
146
				PropertiesMetaData authMetaData = PropertyService
147
						.getAuthMetaData();
148

    
149
				// process the fields for the global options (group 1)
150
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
151
						.getPropertiesInGroup(1);
152
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
153
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
154
					String globalPropertyKey = globalPropertyMap.get(
155
							globalPropertyIndex).getKey();
156
					PropertyService.checkAndSetProperty(request,
157
							globalPropertyKey);
158
				}
159

    
160
				// we need to write the options from memory to the properties
161
				// file
162
				PropertyService.persistProperties();
163

    
164
				// Validate that the options provided are legitimate. Note that
165
				// we've allowed them to persist their entries. As of this point
166
				// there is no other easy way to go back to the configure form
167
				// and preserve their entries.
168
				validationErrors.addAll(validateOptions(request));
169

    
170

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

    
235
		String adminUsers = request.getParameter("auth.administrators");
236
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
237

    
238
		try {
239
			AuthSession authSession = new AuthSession();
240
			for (String adminUser : adminUserList) {
241
				try {
242
					authSession.getAttributes(adminUser);
243
				} catch (ConnectException ce) {
244
					if (ce.getMessage() != null
245
							&& ce.getMessage().contains("NameNotFoundException")) {
246
						errorVector.add("User : " + adminUser + " is not in LDAP.");
247
					} else {
248
						errorVector.add("Connection error while verifying Metacat " + 
249
								"Administrators : " + ce.getMessage());
250
					}
251
				}
252
			}
253
		} catch (InstantiationException ie) {
254
			errorVector.add("Instantiation error while verifying Metacat Administrators : "
255
							+ ie.getMessage());
256
		} catch (Exception e) {
257
			errorVector.add("Error while verifying Metacat Administrators : "
258
					+ e.getMessage());
259
		}
260

    
261
		return errorVector;
262
	}
263
}
(2-2/10)