1 diff -urN apache-tomcat-6.0.29-src.orig/java/org/apache/catalina/startup/UserConfig.java apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/UserConfig.java
2 --- apache-tomcat-6.0.29-src.orig/java/org/apache/catalina/startup/UserConfig.java 2010-07-19 15:02:32.000000000 +0200
3 +++ apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/UserConfig.java 2010-11-18 22:02:22.953623269 +0100
8 + * The connection URL for the LDAP server we will contact.
10 + private String connectionURL = null;
14 + * The connection username for the LDAP server we will contact.
16 + private String connectionName = null;
20 + * The connection password for the LDAP server we will contact.
22 + private String connectionPassword = null;
26 + * The type of authentication to use
28 + private String authentication = null;
32 + * The page size for paged LDAP query.
34 + private int pageSize = 0;
38 + * The base element for user searches.
40 + private String searchBase = "";
44 + * The message format used to search for users.
46 + private String searchFilter = "(objectClass=posixAccount)";
50 + * Should we search the entire subtree for matching users?
52 + private boolean searchSubtree = false;
56 + * The attibute that denotes user name(s).
58 + private String userAttr = "uid";
62 + * The attibute that denotes user home directory.
64 + private String homeAttr = "homeDirectory";
67 * The string resources for this package.
69 private static final StringManager sm =
76 + * The connection URL for the LDAP server we will contact.
78 + public String getConnectionURL() {
80 + return (this.connectionURL);
83 + public void setConnectionURL(String connectionURL) {
85 + this.connectionURL = connectionURL;
91 + * The connection username for the LDAP server we will contact.
93 + public String getConnectionName() {
95 + return (this.connectionName);
98 + public void setConnectionName(String connectionName) {
100 + this.connectionName = connectionName;
106 + * The connection password for the LDAP server we will contact.
108 + public String getConnectionPassword() {
110 + return (this.connectionPassword);
113 + public void setConnectionPassword(String connectionPassword) {
115 + this.connectionPassword = connectionPassword;
120 + * Return the type of authentication to use.
122 + public String getAuthentication() {
124 + return (this.authentication);
129 + * Set the type of authentication to use.
131 + * @param authentication The authentication
133 + public void setAuthentication(String authentication) {
135 + this.authentication = authentication;
141 + * Return the page size for paged LDAP query.
143 + public int getPageSize() {
145 + return (this.pageSize);
150 + * Set the page size for paged LDAP query.
152 + * @param pagesize The page size
154 + public void setPageSize(int pageSize) {
156 + this.pageSize = pageSize;
162 + * The base element for user searches.
164 + public String getSearchBase() {
166 + return (this.searchBase);
169 + public void setSearchBase(String searchBase) {
171 + this.searchBase = searchBase;
177 + * The message format used to search for users.
179 + public String getSearchFilter() {
181 + return (this.searchFilter);
184 + public void setSearchFilter(String searchFilter) {
186 + this.searchFilter = searchFilter;
192 + * Should we search the entire subtree for matching users?
194 + public boolean getSearchSubtree() {
196 + return (this.searchSubtree);
199 + public void setSearchSubtree(boolean searchSubtree) {
201 + this.searchSubtree = searchSubtree;
207 + * The attibute that denotes user name(s).
209 + public String getUserAttr() {
211 + return (this.userAttr);
214 + public void setUserAttr(String userAttr) {
216 + this.userAttr = userAttr;
222 + * The attibute that denotes user home directory.
224 + public String getHomeAttr() {
226 + return (this.homeAttr);
229 + public void setHomeAttr(String homeAttr) {
231 + this.homeAttr = homeAttr;
237 * Return the user database class name for this component.
238 diff -urN apache-tomcat-6.0.29-src.orig/java/org/apache/catalina/startup/LDAPUserDatabase.java apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/LDAPUserDatabase.java
239 --- apache-tomcat-6.0.29-src.orig/java/org/apache/catalina/startup/LDAPUserDatabase.java 1970-01-01 01:00:00.000000000 +0100
240 +++ apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/LDAPUserDatabase.java 2010-11-18 22:09:07.816261633 +0100
243 + * Licensed to the Apache Software Foundation (ASF) under one or more
244 + * contributor license agreements. See the NOTICE file distributed with
245 + * this work for additional information regarding copyright ownership.
246 + * The ASF licenses this file to You under the Apache License, Version 2.0
247 + * (the "License"); you may not use this file except in compliance with
248 + * the License. You may obtain a copy of the License at
250 + * http://www.apache.org/licenses/LICENSE-2.0
252 + * Unless required by applicable law or agreed to in writing, software
253 + * distributed under the License is distributed on an "AS IS" BASIS,
254 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
255 + * See the License for the specific language governing permissions and
256 + * limitations under the License.
260 +package org.apache.catalina.startup;
262 +import java.util.Enumeration;
263 +import java.util.Hashtable;
265 +import javax.naming.Context;
266 +import javax.naming.NamingEnumeration;
267 +import javax.naming.NamingException;
268 +import javax.naming.directory.Attribute;
269 +import javax.naming.directory.Attributes;
270 +import javax.naming.directory.SearchControls;
271 +import javax.naming.directory.SearchResult;
272 +import javax.naming.ldap.Control;
273 +import javax.naming.ldap.InitialLdapContext;
274 +import javax.naming.ldap.LdapContext;
275 +import javax.naming.ldap.PagedResultsControl;
276 +import javax.naming.ldap.PagedResultsResponseControl;
280 + * Concrete implementation of the <code>UserDatabase</code> interface
281 + * that retrieves user data from LDAP server.
283 + * @author Jan Rękorajski
287 +public final class LDAPUserDatabase
288 + implements UserDatabase {
290 + // --------------------------------------------------------- Constructors
293 + * Initialize a new instance of this user database component.
295 + public LDAPUserDatabase() {
299 + // --------------------------------------------------- Instance Variables
302 + * The set of home directories for all defined users, keyed by username.
304 + private Hashtable<String,String> homes = new Hashtable<String,String>();
307 + * The UserConfig listener with which we are associated.
309 + private UserConfig userConfig = null;
311 + // ----------------------------------------------------------- Properties
314 + * Return the UserConfig listener with which we are associated.
316 + public UserConfig getUserConfig() {
317 + return (this.userConfig);
321 + * Set the UserConfig listener with which we are associated.
323 + * @param userConfig The new UserConfig listener
325 + public void setUserConfig(UserConfig userConfig) {
326 + this.userConfig = userConfig;
330 + // ------------------------------------------------------- Public Methods
333 + * Return an absolute pathname to the home directory for the specified user.
335 + * @param user User for which a home directory should be retrieved
337 + public String getHome(String user) {
338 + return homes.get(user);
343 + * Return an enumeration of the usernames defined on this server.
345 + public Enumeration<String> getUsers() {
346 + return (homes.keys());
349 + // ------------------------------------------------------ Private Methods
353 + * Initialize our set of users and home directories.
355 + private void init() {
356 + String connectionURL = userConfig.getConnectionURL();
357 + String connectionName = userConfig.getConnectionName();
358 + String connectionPassword = userConfig.getConnectionPassword();
359 + String authentication = userConfig.getAuthentication();
360 + String searchBase = userConfig.getSearchBase();
361 + String searchFilter = userConfig.getSearchFilter();
362 + boolean searchSubtree = userConfig.getSearchSubtree();
363 + String userAttr = userConfig.getUserAttr();
364 + String homeAttr = userConfig.getHomeAttr();
365 + int pageSize = userConfig.getPageSize();
368 + Hashtable<String,String> env = new Hashtable<String,String>();
370 + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
371 + if (connectionName != null)
372 + env.put(Context.SECURITY_PRINCIPAL, connectionName);
373 + if (connectionPassword != null)
374 + env.put(Context.SECURITY_CREDENTIALS, connectionPassword);
375 + if (connectionURL != null)
376 + env.put(Context.PROVIDER_URL, connectionURL);
377 + if (authentication != null)
378 + env.put(Context.SECURITY_AUTHENTICATION, authentication);
380 + LdapContext ctx = new InitialLdapContext(env, null);
382 + SearchControls constraints = new SearchControls();
383 + String returnAttrs[] = { userAttr, homeAttr };
384 + constraints.setReturningAttributes(returnAttrs);
385 + if (searchSubtree) {
386 + constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
388 + constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
391 + // Request the paged results control
392 + if (pageSize > 0) {
393 + Control[] ctls = new Control[]{ new PagedResultsControl(pageSize, Control.NONCRITICAL) };
394 + ctx.setRequestControls(ctls);
397 + byte[] cookie = null;
399 + NamingEnumeration results = ctx.search(searchBase, searchFilter, constraints);
400 + while (results != null && results.hasMore()) {
401 + SearchResult sr = (SearchResult)results.next();
402 + String username = null;
403 + String home = null;
404 + Attributes attr = sr.getAttributes();
405 + Attribute a = attr.get(homeAttr);
406 + if (a != null && a.size() == 1)
407 + home = (String)a.get();
408 + a = attr.get(userAttr);
409 + if ((home != null) && (a != null)) {
410 + // Add all possible names of this user and corresponding directory
411 + for (int i = 0; i < a.size(); i++) {
412 + username = (String)a.get(i);
413 + if (username != null) {
414 + homes.put(username, home);
420 + Control[] controls = ctx.getResponseControls();
421 + if (controls != null) {
422 + for (int i = 0; i < controls.length; i++) {
423 + if (controls[i] instanceof PagedResultsResponseControl) {
424 + PagedResultsResponseControl prrc = (PagedResultsResponseControl)controls[i];
425 + cookie = prrc.getCookie();
429 + if (cookie != null) {
430 + ctx.setRequestControls(new Control[]{ new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
432 + } while ((cookie != null) && (cookie.length != 0));
433 + } catch (Exception e) {
434 + e.printStackTrace();