1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 /* APR header files */
21 #include "apr_thread_mutex.h"
22 #include "apr_thread_rwlock.h"
23 #include "apr_tables.h"
27 #if APR_HAS_SHARED_MEMORY
32 /* this whole thing disappears if LDAP is not enabled */
35 /* Apache header files */
36 #include "ap_config.h"
38 #include "http_config.h"
39 #include "http_core.h"
41 #include "http_protocol.h"
42 #include "http_request.h"
44 /* Create a set of LDAP_DECLARE macros with appropriate export
45 * and import tags for the platform
48 #define LDAP_DECLARE(type) type
49 #define LDAP_DECLARE_NONSTD(type) type
50 #define LDAP_DECLARE_DATA
51 #elif defined(LDAP_DECLARE_STATIC)
52 #define LDAP_DECLARE(type) type __stdcall
53 #define LDAP_DECLARE_NONSTD(type) type
54 #define LDAP_DECLARE_DATA
55 #elif defined(LDAP_DECLARE_EXPORT)
56 #define LDAP_DECLARE(type) __declspec(dllexport) type __stdcall
57 #define LDAP_DECLARE_NONSTD(type) __declspec(dllexport) type
58 #define LDAP_DECLARE_DATA __declspec(dllexport)
60 #define LDAP_DECLARE(type) __declspec(dllimport) type __stdcall
61 #define LDAP_DECLARE_NONSTD(type) __declspec(dllimport) type
62 #define LDAP_DECLARE_DATA __declspec(dllimport)
69 /* Values that the deref member can have */
71 never=LDAP_DEREF_NEVER,
72 searching=LDAP_DEREF_SEARCHING,
73 finding=LDAP_DEREF_FINDING,
74 always=LDAP_DEREF_ALWAYS
77 /* Structure representing an LDAP connection */
78 typedef struct util_ldap_connection_t {
80 apr_pool_t *pool; /* Pool from which this connection is created */
82 apr_thread_mutex_t *lock; /* Lock to indicate this connection is in use */
84 int bound; /* Flag to indicate whether this connection is bound yet */
86 const char *host; /* Name of the LDAP server (or space separated list) */
87 int port; /* Port of the LDAP server */
88 deref_options deref; /* how to handle alias dereferening */
90 const char *binddn; /* DN to bind to server (can be NULL) */
91 const char *bindpw; /* Password to bind to server (can be NULL) */
93 int secure; /* SSL/TLS mode of the connection */
94 apr_array_header_t *client_certs; /* Client certificates on this connection */
96 const char *reason; /* Reason for an error failure */
98 struct util_ldap_connection_t *next;
99 } util_ldap_connection_t;
101 /* LDAP cache state information */
102 typedef struct util_ldap_state_t {
103 apr_pool_t *pool; /* pool from which this state is allocated */
105 apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
107 apr_global_mutex_t *util_ldap_cache_lock;
109 apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
110 char *cache_file; /* filename for shm */
111 long search_cache_ttl; /* TTL for search cache */
112 long search_cache_size; /* Size (in entries) of search cache */
113 long compare_cache_ttl; /* TTL for compare cache */
114 long compare_cache_size; /* Size (in entries) of compare cache */
116 struct util_ldap_connection_t *connections;
118 apr_array_header_t *global_certs; /* Global CA certificates */
119 apr_array_header_t *client_certs; /* Client certificates */
123 #if APR_HAS_SHARED_MEMORY
124 apr_shm_t *cache_shm;
125 apr_rmm_t *cache_rmm;
129 void *util_ldap_cache;
130 char *lock_file; /* filename for shm lock mutex */
131 int connectionTimeout;
137 * Open a connection to an LDAP server
138 * @param ldc A structure containing the expanded details of the server
139 * to connect to. The handle to the LDAP connection is returned
141 * @tip This function connects to the LDAP server and binds. It does not
142 * connect if already connected (ldc->ldap != NULL). Does not bind
144 * @return If successful LDAP_SUCCESS is returned.
145 * @deffunc int util_ldap_connection_open(request_rec *r,
146 * util_ldap_connection_t *ldc)
148 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r,
149 util_ldap_connection_t *ldc);
152 * Close a connection to an LDAP server
153 * @param ldc A structure containing the expanded details of the server
154 * that was connected.
155 * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
156 * It is possible to rebind to this server again using the same ldc
157 * structure, using apr_ldap_open_connection().
158 * @deffunc util_ldap_close_connection(util_ldap_connection_t *ldc)
160 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc);
163 * Unbind a connection to an LDAP server
164 * @param ldc A structure containing the expanded details of the server
165 * that was connected.
166 * @tip This function unbinds the LDAP connection, and disconnects from
167 * the server. It is used during error conditions, to bring the LDAP
168 * connection back to a known state.
169 * @deffunc apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
171 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_unbind(void *param);
174 * Cleanup a connection to an LDAP server
175 * @param ldc A structure containing the expanded details of the server
176 * that was connected.
177 * @tip This function is registered with the pool cleanup to close down the
178 * LDAP connections when the server is finished with them.
179 * @deffunc apr_status_t util_ldap_connection_cleanup(util_ldap_connection_t *ldc)
181 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_cleanup(void *param);
184 * Find a connection in a list of connections
185 * @param r The request record
186 * @param host The hostname to connect to (multiple hosts space separated)
187 * @param port The port to connect to
188 * @param binddn The DN to bind with
189 * @param bindpw The password to bind with
190 * @param deref The dereferencing behavior
191 * @param secure use SSL on the connection
192 * @tip Once a connection is found and returned, a lock will be acquired to
193 * lock that particular connection, so that another thread does not try and
194 * use this connection while it is busy. Once you are finished with a connection,
195 * apr_ldap_connection_close() must be called to release this connection.
196 * @deffunc util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
197 * const char *binddn, const char *bindpw, deref_options deref,
198 * int netscapessl, int starttls)
200 LDAP_DECLARE(util_ldap_connection_t *) util_ldap_connection_find(request_rec *r, const char *host, int port,
201 const char *binddn, const char *bindpw, deref_options deref,
206 * Compare two DNs for sameness
207 * @param r The request record
208 * @param ldc The LDAP connection being used.
209 * @param url The URL of the LDAP connection - used for deciding which cache to use.
210 * @param dn The first DN to compare.
211 * @param reqdn The DN to compare the first DN to.
212 * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
213 * LDAP calls or with a direct string comparision. A direct
214 * string comparison is faster, but not as accurate - false
215 * negative comparisons are possible.
216 * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
217 * and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
218 * performance issues.
219 * @deffunc int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
220 * const char *url, const char *dn, const char *reqdn,
221 * int compare_dn_on_server)
223 LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
224 const char *url, const char *dn, const char *reqdn,
225 int compare_dn_on_server);
228 * A generic LDAP compare function
229 * @param r The request record
230 * @param ldc The LDAP connection being used.
231 * @param url The URL of the LDAP connection - used for deciding which cache to use.
232 * @param dn The DN of the object in which we do the compare.
233 * @param attrib The attribute within the object we are comparing for.
234 * @param value The value of the attribute we are trying to compare for.
235 * @tip Use this function to determine whether an attribute/value pair exists within an
236 * object. Typically this would be used to determine LDAP group membership.
237 * @deffunc int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
238 * const char *url, const char *dn, const char *attrib, const char *value)
240 LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
241 const char *url, const char *dn, const char *attrib, const char *value);
244 * Checks a username/password combination by binding to the LDAP server
245 * @param r The request record
246 * @param ldc The LDAP connection being used.
247 * @param url The URL of the LDAP connection - used for deciding which cache to use.
248 * @param basedn The Base DN to search for the user in.
249 * @param scope LDAP scope of the search.
250 * @param attrs LDAP attributes to return in search.
251 * @param filter The user to search for in the form of an LDAP filter. This filter must return
252 * exactly one user for the check to be successful.
253 * @param bindpw The user password to bind as.
254 * @param binddn The DN of the user will be returned in this variable.
255 * @param retvals The values corresponding to the attributes requested in the attrs array.
256 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
257 * is made to bind as that user. If this bind succeeds, the user is not validated.
258 * @deffunc int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
259 * char *url, const char *basedn, int scope, char **attrs,
260 * char *filter, char *bindpw, char **binddn, char ***retvals)
262 LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
263 const char *url, const char *basedn, int scope, char **attrs,
264 const char *filter, const char *bindpw, const char **binddn, const char ***retvals);
267 * Searches for a specified user object in an LDAP directory
268 * @param r The request record
269 * @param ldc The LDAP connection being used.
270 * @param url The URL of the LDAP connection - used for deciding which cache to use.
271 * @param basedn The Base DN to search for the user in.
272 * @param scope LDAP scope of the search.
273 * @param attrs LDAP attributes to return in search.
274 * @param filter The user to search for in the form of an LDAP filter. This filter must return
275 * exactly one user for the check to be successful.
276 * @param binddn The DN of the user will be returned in this variable.
277 * @param retvals The values corresponding to the attributes requested in the attrs array.
278 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
279 * is made to bind as that user. If this bind succeeds, the user is not validated.
280 * @deffunc int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
281 * char *url, const char *basedn, int scope, char **attrs,
282 * char *filter, char **binddn, char ***retvals)
284 LDAP_DECLARE(int) util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
285 const char *url, const char *basedn, int scope, char **attrs,
286 const char *filter, const char **binddn, const char ***retvals);
289 * Checks if SSL support is available in mod_ldap
290 * @deffunc int util_ldap_ssl_supported(request_rec *r)
292 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r);
294 /* from apr_ldap_cache.c */
297 * Init the LDAP cache
298 * @param pool The pool to use to initialise the cache
299 * @param reqsize The size of the shared memory segement to request. A size
300 * of zero requests the max size possible from
302 * @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
303 * @return The status code returned is the status code of the
304 * apr_smmem_init() call. Regardless of the status, the cache
305 * will be set up at least for in-process or in-thread operation.
307 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
309 /* from apr_ldap_cache_mgr.c */
312 * Display formatted stats for cache
313 * @param The pool to allocate the returned string from
314 * @tip This function returns a string allocated from the provided pool that describes
315 * various stats about the cache.
316 * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
318 char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
320 #endif /* APR_HAS_LDAP */
321 #endif /* UTIL_LDAP_H */