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"
32 #include "apr_ldap_rebind.h"
34 #if APR_HAS_MICROSOFT_LDAPSDK
35 #define AP_LDAP_IS_SERVER_DOWN(s) ((s) == LDAP_SERVER_DOWN \
36 ||(s) == LDAP_UNAVAILABLE)
38 #define AP_LDAP_IS_SERVER_DOWN(s) ((s) == LDAP_SERVER_DOWN)
41 #if APR_HAS_SHARED_MEMORY
46 /* this whole thing disappears if LDAP is not enabled */
49 /* Apache header files */
50 #include "ap_config.h"
52 #include "http_config.h"
53 #include "http_core.h"
55 #include "http_protocol.h"
56 #include "http_request.h"
57 #include "apr_optional.h"
59 /* Create a set of LDAP_DECLARE macros with appropriate export
60 * and import tags for the platform
63 #define LDAP_DECLARE(type) type
64 #define LDAP_DECLARE_NONSTD(type) type
65 #define LDAP_DECLARE_DATA
66 #elif defined(LDAP_DECLARE_STATIC)
67 #define LDAP_DECLARE(type) type __stdcall
68 #define LDAP_DECLARE_NONSTD(type) type
69 #define LDAP_DECLARE_DATA
70 #elif defined(LDAP_DECLARE_EXPORT)
71 #define LDAP_DECLARE(type) __declspec(dllexport) type __stdcall
72 #define LDAP_DECLARE_NONSTD(type) __declspec(dllexport) type
73 #define LDAP_DECLARE_DATA __declspec(dllexport)
75 #define LDAP_DECLARE(type) __declspec(dllimport) type __stdcall
76 #define LDAP_DECLARE_NONSTD(type) __declspec(dllimport) type
77 #define LDAP_DECLARE_DATA __declspec(dllimport)
88 /* Values that the deref member can have */
90 never=LDAP_DEREF_NEVER,
91 searching=LDAP_DEREF_SEARCHING,
92 finding=LDAP_DEREF_FINDING,
93 always=LDAP_DEREF_ALWAYS
96 /* Structure representing an LDAP connection */
97 typedef struct util_ldap_connection_t {
99 apr_pool_t *pool; /* Pool from which this connection is created */
101 apr_thread_mutex_t *lock; /* Lock to indicate this connection is in use */
103 int bound; /* Flag to indicate whether this connection is bound yet */
105 const char *host; /* Name of the LDAP server (or space separated list) */
106 int port; /* Port of the LDAP server */
107 deref_options deref; /* how to handle alias dereferening */
109 const char *binddn; /* DN to bind to server (can be NULL) */
110 const char *bindpw; /* Password to bind to server (can be NULL) */
112 int secure; /* SSL/TLS mode of the connection */
113 apr_array_header_t *client_certs; /* Client certificates on this connection */
115 const char *reason; /* Reason for an error failure */
117 struct util_ldap_connection_t *next;
118 struct util_ldap_state_t *st; /* The LDAP vhost config this connection belongs to */
119 int keep; /* Will this connection be kept when it's unlocked */
121 int ChaseReferrals; /* [on|off] (default = AP_LDAP_CHASEREFERRALS_ON)*/
122 int ReferralHopLimit; /* # of referral hops to follow (default = AP_LDAP_DEFAULT_HOPLIMIT) */
123 } util_ldap_connection_t;
125 typedef struct util_ldap_config_t {
127 int ReferralHopLimit;
128 } util_ldap_config_t;
130 /* LDAP cache state information */
131 typedef struct util_ldap_state_t {
132 apr_pool_t *pool; /* pool from which this state is allocated */
134 apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
136 apr_global_mutex_t *util_ldap_cache_lock;
138 apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
139 char *cache_file; /* filename for shm */
140 long search_cache_ttl; /* TTL for search cache */
141 long search_cache_size; /* Size (in entries) of search cache */
142 long compare_cache_ttl; /* TTL for compare cache */
143 long compare_cache_size; /* Size (in entries) of compare cache */
145 struct util_ldap_connection_t *connections;
147 apr_array_header_t *global_certs; /* Global CA certificates */
148 apr_array_header_t *client_certs; /* Client certificates */
152 #if APR_HAS_SHARED_MEMORY
153 apr_shm_t *cache_shm;
154 apr_rmm_t *cache_rmm;
158 void *util_ldap_cache;
159 char *lock_file; /* filename for shm lock mutex */
160 long connectionTimeout;
162 int debug_level; /* SDK debug level */
166 /* Used to store arrays of attribute labels/values. */
167 struct mod_auth_ldap_groupattr_entry_t {
172 * Open a connection to an LDAP server
173 * @param ldc A structure containing the expanded details of the server
174 * to connect to. The handle to the LDAP connection is returned
176 * @tip This function connects to the LDAP server and binds. It does not
177 * connect if already connected (ldc->ldap != NULL). Does not bind
179 * @return If successful LDAP_SUCCESS is returned.
180 * @fn int util_ldap_connection_open(request_rec *r,
181 * util_ldap_connection_t *ldc)
183 APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
184 util_ldap_connection_t *ldc));
187 * Close a connection to an LDAP server
188 * @param ldc A structure containing the expanded details of the server
189 * that was connected.
190 * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
191 * It is possible to rebind to this server again using the same ldc
192 * structure, using apr_ldap_open_connection().
193 * @fn util_ldap_close_connection(util_ldap_connection_t *ldc)
195 APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
198 * Unbind a connection to an LDAP server
199 * @param ldc A structure containing the expanded details of the server
200 * that was connected.
201 * @tip This function unbinds the LDAP connection, and disconnects from
202 * the server. It is used during error conditions, to bring the LDAP
203 * connection back to a known state.
204 * @fn apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
206 APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
209 * Cleanup a connection to an LDAP server
210 * @param ldc A structure containing the expanded details of the server
211 * that was connected.
212 * @tip This functions unbinds and closes the connection to the LDAP server
213 * @fn apr_status_t util_ldap_connection_cleanup(util_ldap_connection_t *ldc)
215 APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_cleanup,(void *param));
218 * Find a connection in a list of connections
219 * @param r The request record
220 * @param host The hostname to connect to (multiple hosts space separated)
221 * @param port The port to connect to
222 * @param binddn The DN to bind with
223 * @param bindpw The password to bind with
224 * @param deref The dereferencing behavior
225 * @param secure use SSL on the connection
226 * @tip Once a connection is found and returned, a lock will be acquired to
227 * lock that particular connection, so that another thread does not try and
228 * use this connection while it is busy. Once you are finished with a connection,
229 * apr_ldap_connection_close() must be called to release this connection.
230 * @fn util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
231 * const char *binddn, const char *bindpw, deref_options deref,
232 * int netscapessl, int starttls)
234 APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
235 const char *binddn, const char *bindpw, deref_options deref,
239 * Compare two DNs for sameness
240 * @param r The request record
241 * @param ldc The LDAP connection being used.
242 * @param url The URL of the LDAP connection - used for deciding which cache to use.
243 * @param dn The first DN to compare.
244 * @param reqdn The DN to compare the first DN to.
245 * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
246 * LDAP calls or with a direct string comparision. A direct
247 * string comparison is faster, but not as accurate - false
248 * negative comparisons are possible.
249 * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
250 * and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
251 * performance issues.
252 * @fn int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
253 * const char *url, const char *dn, const char *reqdn,
254 * int compare_dn_on_server)
256 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
257 const char *url, const char *dn, const char *reqdn,
258 int compare_dn_on_server));
261 * A generic LDAP compare function
262 * @param r The request record
263 * @param ldc The LDAP connection being used.
264 * @param url The URL of the LDAP connection - used for deciding which cache to use.
265 * @param dn The DN of the object in which we do the compare.
266 * @param attrib The attribute within the object we are comparing for.
267 * @param value The value of the attribute we are trying to compare for.
268 * @tip Use this function to determine whether an attribute/value pair exists within an
269 * object. Typically this would be used to determine LDAP top-level group
271 * @fn int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
272 * const char *url, const char *dn, const char *attrib, const char *value)
274 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
275 const char *url, const char *dn, const char *attrib, const char *value));
278 * An LDAP function that checks if the specified user is a member of a subgroup.
279 * @param r The request record
280 * @param ldc The LDAP connection being used.
281 * @param url The URL of the LDAP connection - used for deciding which cache to use.
282 * @param dn The DN of the object in which we find subgroups to search within.
283 * @param attrib The attribute within group objects that identify users.
284 * @param value The user attribute value we are trying to compare for.
285 * @param subgroupAttrs The attributes within group objects that identify subgroups.
287 * @param subgroupclasses The objectClass values used to identify groups (and
288 * subgroups). apr_array_header_t *.
289 * @param cur_subgroup_depth Current recursive depth during subgroup processing.
290 * @param max_subgroup_depth Maximum depth of recursion allowed during subgroup
292 * @tip Use this function to determine whether an attribute/value pair exists within a
293 * starting group object or one of its nested subgroups. Typically this would be
294 * used to determine LDAP nested group membership.
295 * @deffunc int util_ldap_cache_check_subgroups(request_rec *r, util_ldap_connection_t
296 * *ldc, const char *url, const char *dn,
297 * const char *attrib, const char value,
298 * char **subgroupAttrs, apr_array_header_t
299 * *subgroupclasses, int cur_subgroup_depth, int
300 * max_subgroup_depth )
302 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_check_subgroups,(request_rec *r, util_ldap_connection_t *ldc,
303 const char *url, const char *dn, const char *attrib, const char *value,
304 char **subgroupAttrs, apr_array_header_t *subgroupclasses,
305 int cur_subgroup_depth, int max_subgroup_depth));
308 * Checks a username/password combination by binding to the LDAP server
309 * @param r The request record
310 * @param ldc The LDAP connection being used.
311 * @param url The URL of the LDAP connection - used for deciding which cache to use.
312 * @param basedn The Base DN to search for the user in.
313 * @param scope LDAP scope of the search.
314 * @param attrs LDAP attributes to return in search.
315 * @param filter The user to search for in the form of an LDAP filter. This filter must return
316 * exactly one user for the check to be successful.
317 * @param bindpw The user password to bind as.
318 * @param binddn The DN of the user will be returned in this variable.
319 * @param retvals The values corresponding to the attributes requested in the attrs array.
320 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
321 * is made to bind as that user. If this bind succeeds, the user is not validated.
322 * @fn int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
323 * char *url, const char *basedn, int scope, char **attrs,
324 * char *filter, char *bindpw, char **binddn, char ***retvals)
326 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
327 const char *url, const char *basedn, int scope, char **attrs,
328 const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
331 * Searches for a specified user object in an LDAP directory
332 * @param r The request record
333 * @param ldc The LDAP connection being used.
334 * @param url The URL of the LDAP connection - used for deciding which cache to use.
335 * @param basedn The Base DN to search for the user in.
336 * @param scope LDAP scope of the search.
337 * @param attrs LDAP attributes to return in search.
338 * @param filter The user to search for in the form of an LDAP filter. This filter must return
339 * exactly one user for the check to be successful.
340 * @param binddn The DN of the user will be returned in this variable.
341 * @param retvals The values corresponding to the attributes requested in the attrs array.
342 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
343 * is made to bind as that user. If this bind succeeds, the user is not validated.
344 * @fn int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
345 * char *url, const char *basedn, int scope, char **attrs,
346 * char *filter, char **binddn, char ***retvals)
348 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
349 const char *url, const char *basedn, int scope, char **attrs,
350 const char *filter, const char **binddn, const char ***retvals));
353 * Checks if SSL support is available in mod_ldap
354 * @fn int util_ldap_ssl_supported(request_rec *r)
356 APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
358 /* from apr_ldap_cache.c */
361 * Init the LDAP cache
362 * @param pool The pool to use to initialise the cache
363 * @param reqsize The size of the shared memory segement to request. A size
364 * of zero requests the max size possible from
366 * @fn void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
367 * @return The status code returned is the status code of the
368 * apr_smmem_init() call. Regardless of the status, the cache
369 * will be set up at least for in-process or in-thread operation.
371 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
373 /* from apr_ldap_cache_mgr.c */
376 * Display formatted stats for cache
377 * @param The pool to allocate the returned string from
378 * @tip This function returns a string allocated from the provided pool that describes
379 * various stats about the cache.
380 * @fn char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
382 char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
386 #endif /* APR_HAS_LDAP */
387 #endif /* UTIL_LDAP_H */