1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @brief Apache LDAP library
25 /* APR header files */
27 #include "apr_thread_mutex.h"
28 #include "apr_thread_rwlock.h"
29 #include "apr_tables.h"
33 #if APR_HAS_SHARED_MEMORY
38 /* this whole thing disappears if LDAP is not enabled */
41 /* Apache header files */
42 #include "ap_config.h"
44 #include "http_config.h"
45 #include "http_core.h"
47 #include "http_protocol.h"
48 #include "http_request.h"
49 #include "apr_optional.h"
51 /* Create a set of LDAP_DECLARE macros with appropriate export
52 * and import tags for the platform
55 #define LDAP_DECLARE(type) type
56 #define LDAP_DECLARE_NONSTD(type) type
57 #define LDAP_DECLARE_DATA
58 #elif defined(LDAP_DECLARE_STATIC)
59 #define LDAP_DECLARE(type) type __stdcall
60 #define LDAP_DECLARE_NONSTD(type) type
61 #define LDAP_DECLARE_DATA
62 #elif defined(LDAP_DECLARE_EXPORT)
63 #define LDAP_DECLARE(type) __declspec(dllexport) type __stdcall
64 #define LDAP_DECLARE_NONSTD(type) __declspec(dllexport) type
65 #define LDAP_DECLARE_DATA __declspec(dllexport)
67 #define LDAP_DECLARE(type) __declspec(dllimport) type __stdcall
68 #define LDAP_DECLARE_NONSTD(type) __declspec(dllimport) type
69 #define LDAP_DECLARE_DATA __declspec(dllimport)
80 /* Values that the deref member can have */
82 never=LDAP_DEREF_NEVER,
83 searching=LDAP_DEREF_SEARCHING,
84 finding=LDAP_DEREF_FINDING,
85 always=LDAP_DEREF_ALWAYS
88 /* Structure representing an LDAP connection */
89 typedef struct util_ldap_connection_t {
91 apr_pool_t *pool; /* Pool from which this connection is created */
93 apr_thread_mutex_t *lock; /* Lock to indicate this connection is in use */
95 int bound; /* Flag to indicate whether this connection is bound yet */
97 const char *host; /* Name of the LDAP server (or space separated list) */
98 int port; /* Port of the LDAP server */
99 deref_options deref; /* how to handle alias dereferening */
101 const char *binddn; /* DN to bind to server (can be NULL) */
102 const char *bindpw; /* Password to bind to server (can be NULL) */
104 int secure; /* SSL/TLS mode of the connection */
105 apr_array_header_t *client_certs; /* Client certificates on this connection */
107 const char *reason; /* Reason for an error failure */
109 struct util_ldap_connection_t *next;
110 } util_ldap_connection_t;
112 /* LDAP cache state information */
113 typedef struct util_ldap_state_t {
114 apr_pool_t *pool; /* pool from which this state is allocated */
116 apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
118 apr_global_mutex_t *util_ldap_cache_lock;
120 apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
121 char *cache_file; /* filename for shm */
122 long search_cache_ttl; /* TTL for search cache */
123 long search_cache_size; /* Size (in entries) of search cache */
124 long compare_cache_ttl; /* TTL for compare cache */
125 long compare_cache_size; /* Size (in entries) of compare cache */
127 struct util_ldap_connection_t *connections;
129 apr_array_header_t *global_certs; /* Global CA certificates */
130 apr_array_header_t *client_certs; /* Client certificates */
134 #if APR_HAS_SHARED_MEMORY
135 apr_shm_t *cache_shm;
136 apr_rmm_t *cache_rmm;
140 void *util_ldap_cache;
141 char *lock_file; /* filename for shm lock mutex */
142 long connectionTimeout;
147 /* Used to store arrays of attribute labels/values. */
148 struct mod_auth_ldap_groupattr_entry_t {
153 * Open a connection to an LDAP server
154 * @param ldc A structure containing the expanded details of the server
155 * to connect to. The handle to the LDAP connection is returned
157 * @tip This function connects to the LDAP server and binds. It does not
158 * connect if already connected (ldc->ldap != NULL). Does not bind
160 * @return If successful LDAP_SUCCESS is returned.
161 * @fn int util_ldap_connection_open(request_rec *r,
162 * util_ldap_connection_t *ldc)
164 APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
165 util_ldap_connection_t *ldc));
168 * Close a connection to an LDAP server
169 * @param ldc A structure containing the expanded details of the server
170 * that was connected.
171 * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
172 * It is possible to rebind to this server again using the same ldc
173 * structure, using apr_ldap_open_connection().
174 * @fn util_ldap_close_connection(util_ldap_connection_t *ldc)
176 APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
179 * Unbind a connection to an LDAP server
180 * @param ldc A structure containing the expanded details of the server
181 * that was connected.
182 * @tip This function unbinds the LDAP connection, and disconnects from
183 * the server. It is used during error conditions, to bring the LDAP
184 * connection back to a known state.
185 * @fn apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
187 APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
190 * Cleanup a connection to an LDAP server
191 * @param ldc A structure containing the expanded details of the server
192 * that was connected.
193 * @tip This functions unbinds and closes the connection to the LDAP server
194 * @fn apr_status_t util_ldap_connection_cleanup(util_ldap_connection_t *ldc)
196 APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_cleanup,(void *param));
199 * Find a connection in a list of connections
200 * @param r The request record
201 * @param host The hostname to connect to (multiple hosts space separated)
202 * @param port The port to connect to
203 * @param binddn The DN to bind with
204 * @param bindpw The password to bind with
205 * @param deref The dereferencing behavior
206 * @param secure use SSL on the connection
207 * @tip Once a connection is found and returned, a lock will be acquired to
208 * lock that particular connection, so that another thread does not try and
209 * use this connection while it is busy. Once you are finished with a connection,
210 * apr_ldap_connection_close() must be called to release this connection.
211 * @fn util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
212 * const char *binddn, const char *bindpw, deref_options deref,
213 * int netscapessl, int starttls)
215 APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
216 const char *binddn, const char *bindpw, deref_options deref,
220 * Compare two DNs for sameness
221 * @param r The request record
222 * @param ldc The LDAP connection being used.
223 * @param url The URL of the LDAP connection - used for deciding which cache to use.
224 * @param dn The first DN to compare.
225 * @param reqdn The DN to compare the first DN to.
226 * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
227 * LDAP calls or with a direct string comparision. A direct
228 * string comparison is faster, but not as accurate - false
229 * negative comparisons are possible.
230 * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
231 * and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
232 * performance issues.
233 * @fn int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
234 * const char *url, const char *dn, const char *reqdn,
235 * int compare_dn_on_server)
237 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
238 const char *url, const char *dn, const char *reqdn,
239 int compare_dn_on_server));
242 * A generic LDAP compare function
243 * @param r The request record
244 * @param ldc The LDAP connection being used.
245 * @param url The URL of the LDAP connection - used for deciding which cache to use.
246 * @param dn The DN of the object in which we do the compare.
247 * @param attrib The attribute within the object we are comparing for.
248 * @param value The value of the attribute we are trying to compare for.
249 * @tip Use this function to determine whether an attribute/value pair exists within an
250 * object. Typically this would be used to determine LDAP top-level group
252 * @fn int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
253 * const char *url, const char *dn, const char *attrib, const char *value)
255 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
256 const char *url, const char *dn, const char *attrib, const char *value));
259 * An LDAP function that checks if the specified user is a member of a subgroup.
260 * @param r The request record
261 * @param ldc The LDAP connection being used.
262 * @param url The URL of the LDAP connection - used for deciding which cache to use.
263 * @param dn The DN of the object in which we find subgroups to search within.
264 * @param attrib The attribute within group objects that identify users.
265 * @param value The user attribute value we are trying to compare for.
266 * @param subgroupAttrs The attributes within group objects that identify subgroups.
268 * @param subgroupclasses The objectClass values used to identify groups (and
269 * subgroups). apr_array_header_t *.
270 * @param cur_subgroup_depth Current recursive depth during subgroup processing.
271 * @param max_subgroup_depth Maximum depth of recursion allowed during subgroup
273 * @tip Use this function to determine whether an attribute/value pair exists within a
274 * starting group object or one of its nested subgroups. Typically this would be
275 * used to determine LDAP nested group membership.
276 * @deffunc int util_ldap_cache_check_subgroups(request_rec *r, util_ldap_connection_t
277 * *ldc, const char *url, const char *dn,
278 * const char *attrib, const char value,
279 * char **subgroupAttrs, apr_array_header_t
280 * *subgroupclasses, int cur_subgroup_depth, int
281 * max_subgroup_depth )
283 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_check_subgroups,(request_rec *r, util_ldap_connection_t *ldc,
284 const char *url, const char *dn, const char *attrib, const char *value,
285 char **subgroupAttrs, apr_array_header_t *subgroupclasses,
286 int cur_subgroup_depth, int max_subgroup_depth));
289 * Checks a username/password combination by binding to the LDAP server
290 * @param r The request record
291 * @param ldc The LDAP connection being used.
292 * @param url The URL of the LDAP connection - used for deciding which cache to use.
293 * @param basedn The Base DN to search for the user in.
294 * @param scope LDAP scope of the search.
295 * @param attrs LDAP attributes to return in search.
296 * @param filter The user to search for in the form of an LDAP filter. This filter must return
297 * exactly one user for the check to be successful.
298 * @param bindpw The user password to bind as.
299 * @param binddn The DN of the user will be returned in this variable.
300 * @param retvals The values corresponding to the attributes requested in the attrs array.
301 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
302 * is made to bind as that user. If this bind succeeds, the user is not validated.
303 * @fn int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
304 * char *url, const char *basedn, int scope, char **attrs,
305 * char *filter, char *bindpw, char **binddn, char ***retvals)
307 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
308 const char *url, const char *basedn, int scope, char **attrs,
309 const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
312 * Searches for a specified user object in an LDAP directory
313 * @param r The request record
314 * @param ldc The LDAP connection being used.
315 * @param url The URL of the LDAP connection - used for deciding which cache to use.
316 * @param basedn The Base DN to search for the user in.
317 * @param scope LDAP scope of the search.
318 * @param attrs LDAP attributes to return in search.
319 * @param filter The user to search for in the form of an LDAP filter. This filter must return
320 * exactly one user for the check to be successful.
321 * @param binddn The DN of the user will be returned in this variable.
322 * @param retvals The values corresponding to the attributes requested in the attrs array.
323 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
324 * is made to bind as that user. If this bind succeeds, the user is not validated.
325 * @fn int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
326 * char *url, const char *basedn, int scope, char **attrs,
327 * char *filter, char **binddn, char ***retvals)
329 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
330 const char *url, const char *basedn, int scope, char **attrs,
331 const char *filter, const char **binddn, const char ***retvals));
334 * Checks if SSL support is available in mod_ldap
335 * @fn int util_ldap_ssl_supported(request_rec *r)
337 APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
339 /* from apr_ldap_cache.c */
342 * Init the LDAP cache
343 * @param pool The pool to use to initialise the cache
344 * @param reqsize The size of the shared memory segement to request. A size
345 * of zero requests the max size possible from
347 * @fn void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
348 * @return The status code returned is the status code of the
349 * apr_smmem_init() call. Regardless of the status, the cache
350 * will be set up at least for in-process or in-thread operation.
352 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
354 /* from apr_ldap_cache_mgr.c */
357 * Display formatted stats for cache
358 * @param The pool to allocate the returned string from
359 * @tip This function returns a string allocated from the provided pool that describes
360 * various stats about the cache.
361 * @fn char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
363 char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
367 #endif /* APR_HAS_LDAP */
368 #endif /* UTIL_LDAP_H */