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