]> git.pld-linux.org Git - packages/tomcat.git/blob - tomcat-LDAPUserDatabase.patch
- attempt to get user list from LDAP (for userdirs)
[packages/tomcat.git] / tomcat-LDAPUserDatabase.patch
1 --- apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/UserConfig.java.orig      2010-07-19 15:02:32.000000000 +0200
2 +++ apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/UserConfig.java   2010-11-18 13:50:33.433156115 +0100
3 @@ -83,6 +83,58 @@
4  
5  
6      /**
7 +     * The connection URL for the LDAP server we will contact.
8 +     */
9 +    private String connectionURL = null;
10 +
11 +
12 +    /**
13 +     * The connection username for the LDAP server we will contact.
14 +     */
15 +    private String connectionName = null;
16 +
17 +
18 +    /**
19 +     * The connection password for the LDAP server we will contact.
20 +     */
21 +    private String connectionPassword = null;
22 +
23 +
24 +    /**
25 +     *  The type of authentication to use
26 +     */
27 +    private String authentication = null;
28 +
29 +    /**
30 +     * The base element for user searches.
31 +     */
32 +    private String searchBase = "";
33 +
34 +
35 +    /**
36 +     * The message format used to search for users.
37 +     */
38 +    private String searchFilter = "(objectClass=posixAccount)";
39 +
40 +
41 +    /**
42 +     * Should we search the entire subtree for matching users?
43 +     */
44 +    private boolean searchSubtree = false;
45 +
46 +
47 +    /**
48 +     * The attibute that denotes user name(s).
49 +     */
50 +    private String userAttr = "uid";
51 +
52 +
53 +    /**
54 +     * The attibute that denotes user home directory.
55 +     */
56 +    private String homeAttr = "homeDirectory";
57 +
58 +    /**
59       * The string resources for this package.
60       */
61      private static final StringManager sm =
62 @@ -186,6 +238,146 @@
63  
64      }
65  
66 +
67 +    /**
68 +     * The connection URL for the LDAP server we will contact.
69 +     */
70 +    public String getConnectionURL() {
71 +
72 +        return (this.connectionURL);
73 +
74 +    }
75 +    public void setConnectionURL(String connectionURL) {
76 +
77 +        this.connectionURL = connectionURL;
78 +
79 +    }
80 +
81 +
82 +    /**
83 +     * The connection username for the LDAP server we will contact.
84 +     */
85 +    public String getConnectionName() {
86 +
87 +        return (this.connectionName);
88 +
89 +    }
90 +    public void setConnectionName(String connectionName) {
91 +
92 +        this.connectionName = connectionName;
93 +
94 +    }
95 +
96 +
97 +    /**
98 +     * The connection password for the LDAP server we will contact.
99 +     */
100 +    public String getConnectionPassword() {
101 +
102 +        return (this.connectionPassword);
103 +
104 +    }
105 +    public void setConnectionPassword(String connectionPassword) {
106 +
107 +        this.connectionPassword = connectionPassword;
108 +
109 +    }
110 +
111 +    /**
112 +     * Return the type of authentication to use.
113 +     */
114 +    public String getAuthentication() {
115 +
116 +       return (this.authentication);
117 +
118 +    }
119 +
120 +    /**
121 +     * Set the type of authentication to use.
122 +     *
123 +     * @param authentication The authentication
124 +     */
125 +    public void setAuthentication(String authentication) {
126 +
127 +       this.authentication = authentication;
128 +
129 +    }
130 +
131 +
132 +    /**
133 +     * The base element for user searches.
134 +     */
135 +    public String getSearchBase() {
136 +
137 +        return (this.searchBase);
138 +
139 +    }
140 +    public void setSearchBase(String searchBase) {
141 +
142 +        this.searchBase = searchBase;
143 +
144 +    }
145 +
146 +
147 +    /**
148 +     * The message format used to search for users.
149 +     */
150 +    public String getSearchFilter() {
151 +
152 +        return (this.searchFilter);
153 +
154 +    }
155 +    public void setSearchFilter(String searchFilter) {
156 +
157 +        this.searchFilter = searchFilter;
158 +
159 +    }
160 +
161 +
162 +    /**
163 +     * Should we search the entire subtree for matching users?
164 +     */
165 +    public boolean getSearchSubtree() {
166 +
167 +        return (this.searchSubtree);
168 +
169 +    }
170 +    public void setSearchSubtree(String searchSubtree) {
171 +
172 +        this.searchSubtree = searchSubtree;
173 +
174 +    }
175 +
176 +
177 +    /**
178 +     * The attibute that denotes user name(s).
179 +     */
180 +    public String getUserAttr() {
181 +
182 +        return (this.userAttr);
183 +
184 +    }
185 +    public void setUserAttr(String userAttr) {
186 +
187 +        this.userAttr = userAttr;
188 +
189 +    }
190 +
191 +
192 +    /**
193 +     * The attibute that denotes user home directory.
194 +     */
195 +    public String getHomeAttr() {
196 +
197 +        return (this.homeAttr);
198 +
199 +    }
200 +    public void setHomeAttr(String homeAttr) {
201 +
202 +        this.homeAttr = homeAttr;
203 +
204 +    }
205 +
206  
207      /**
208       * Return the user database class name for this component.
209 --- /dev/null   2010-10-22 13:07:45.106999849 +0200
210 +++ apache-tomcat-6.0.29-src/java/org/apache/catalina/startup/LDAPUserDatabase.java     2010-11-18 13:50:32.671156104 +0100
211 @@ -0,0 +1,169 @@
212 +/*
213 + * Licensed to the Apache Software Foundation (ASF) under one or more
214 + * contributor license agreements.  See the NOTICE file distributed with
215 + * this work for additional information regarding copyright ownership.
216 + * The ASF licenses this file to You under the Apache License, Version 2.0
217 + * (the "License"); you may not use this file except in compliance with
218 + * the License.  You may obtain a copy of the License at
219 + * 
220 + *      http://www.apache.org/licenses/LICENSE-2.0
221 + * 
222 + * Unless required by applicable law or agreed to in writing, software
223 + * distributed under the License is distributed on an "AS IS" BASIS,
224 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
225 + * See the License for the specific language governing permissions and
226 + * limitations under the License.
227 + */
228 +
229 +
230 +package org.apache.catalina.startup;
231 +
232 +
233 +import java.util.Collection;
234 +import java.util.Hashtable;
235 +import java.util.Enumeration;
236 +import java.util.Vector;
237 +import javax.naming.directory.DirContext;
238 +import javax.naming.directory.InitialDirContext;
239 +import javax.naming.directory.Attribute;
240 +import javax.naming.directory.Attributes;
241 +import javax.naming.directory.SearchControls;
242 +import javax.naming.directory.SearchResult;
243 +import javax.naming.Context;
244 +import javax.naming.NamingException;
245 +import javax.naming.NamingEnumeration;
246 +
247 +/**
248 + * Concrete implementation of the <code>UserDatabase</code> interface
249 + * that retrieves user data from LDAP server.
250 + *
251 + * @author Jan RÄ™korajski
252 + * @version 0.1
253 + */
254 +
255 +public final class LDAPUserDatabase
256 +    implements UserDatabase {
257 +
258 +    // --------------------------------------------------------- Constructors
259 +
260 +    /**
261 +     * Initialize a new instance of this user database component.
262 +     */
263 +    public LDAPUserDatabase() {
264 +        super();
265 +    }
266 +
267 +    // --------------------------------------------------- Instance Variables
268 +
269 +    /**
270 +     * The set of home directories for all defined users, keyed by username.
271 +     */
272 +    private Hashtable homes = new Hashtable();
273 +
274 +    /**
275 +     * The UserConfig listener with which we are associated.
276 +     */
277 +    private UserConfig userConfig = null;
278 +
279 +    // ----------------------------------------------------------- Properties
280 +
281 +    /**
282 +     * Return the UserConfig listener with which we are associated.
283 +     */
284 +    public UserConfig getUserConfig() {
285 +        return (this.userConfig);
286 +    }
287 +
288 +    /**
289 +     * Set the UserConfig listener with which we are associated.
290 +     *
291 +     * @param userConfig The new UserConfig listener
292 +     */
293 +    public void setUserConfig(UserConfig userConfig) {
294 +        this.userConfig = userConfig;
295 +        init();
296 +    }
297 +
298 +    // ------------------------------------------------------- Public Methods
299 +
300 +    /**
301 +     * Return an absolute pathname to the home directory for the specified user.
302 +     *
303 +     * @param user User for which a home directory should be retrieved
304 +     */
305 +    public String getHome(String user) {
306 +        return ((String) homes.get(user));
307 +    }
308 +
309 +
310 +    /**
311 +     * Return an enumeration of the usernames defined on this server.
312 +     */
313 +    public Enumeration getUsers() {
314 +        return (homes.keys());
315 +    }
316 +
317 +    // ------------------------------------------------------ Private Methods
318 +
319 +
320 +    /**
321 +     * Initialize our set of users and home directories.
322 +     */
323 +    private void init() {
324 +       static String INIT_CTX = "com.sun.jndi.ldap.LdapCtxFactory";
325 +
326 +       String connectionURL = userConfig.getConnectionURL();
327 +       String connectionName = userConfig.getConnectionName();
328 +       String connectionPassword = userConfig.getConnectionPassword();
329 +       String authentication = userConfig.getAuthentication();
330 +       String searchBase = userConfig.getSearchBase();
331 +       String searchFilter = userConfig.getSearchFilter();
332 +       boolean searchSubtree = userConfig.getSearchSubtree();
333 +       String userAttr = userConfig.getUserAttr();
334 +       String homeAttr = userConfig.getHomeAttr();
335 +
336 +       try {
337 +               Hashtable<String,String> env = new Hashtable<String,String>();
338 +
339 +               env.put(Context.INITIAL_CONTEXT_FACTORY, INIT_CTX);
340 +               if (connectionName != null)
341 +                       env.put(Context.SECURITY_PRINCIPAL, connectionName);
342 +               if (connectionPassword != null)
343 +                       env.put(Context.SECURITY_CREDENTIALS, connectionPassword);
344 +               if (connectionURL != null)
345 +                       env.put(Context.PROVIDER_URL, connectionURL);
346 +               if (authentication != null)
347 +                       env.put(Context.SECURITY_AUTHENTICATION, authentication);
348 +
349 +               DirContext dirContext = new InitialDirContext(env);
350 +
351 +               SearchControls constraints = new SearchControls();
352 +               if (searchSubtree) {
353 +                       constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
354 +               } else {
355 +                       constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
356 +               }
357 +               NamingEnumeration results = dirContext.search(searchBase, searchFilter, constraints);
358 +               while (results != null && results.hasMore()) {
359 +                   SearchResult sr = (SearchResult)results.next();
360 +                   String username = null;
361 +                   String home = null;
362 +                   Attributes attr = sr.getAttributes();
363 +                   Attribute a = attr.get(homeAttr);
364 +                   if (a != null && a.size() == 1)
365 +                          home = (String)a.get();
366 +                   Attribute a = attr.get(userAttr);
367 +                   if ((home != null) && (a != null)) {
368 +                           // Add all possible names of this user and corresponding directory
369 +                           for (int i = 0; i < a.size(); i++) {
370 +                                   username = (String)a.get(i);
371 +                                   if (username != null)
372 +                                           homes.put(username, home);
373 +                           }
374 +                   }
375 +               }
376 +       } catch (Exception e) {
377 +       //        e.printStackTrace();
378 +       }
379 +    }
380 +}
This page took 0.067885 seconds and 3 git commands to generate.