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)
76
/* Values that the deref member can have */
78
never=LDAP_DEREF_NEVER,
79
searching=LDAP_DEREF_SEARCHING,
80
finding=LDAP_DEREF_FINDING,
81
always=LDAP_DEREF_ALWAYS
84
/* Structure representing an LDAP connection */
85
typedef struct util_ldap_connection_t {
87
apr_pool_t *pool; /* Pool from which this connection is created */
89
apr_thread_mutex_t *lock; /* Lock to indicate this connection is in use */
91
int bound; /* Flag to indicate whether this connection is bound yet */
93
const char *host; /* Name of the LDAP server (or space separated list) */
94
int port; /* Port of the LDAP server */
95
deref_options deref; /* how to handle alias dereferening */
97
const char *binddn; /* DN to bind to server (can be NULL) */
98
const char *bindpw; /* Password to bind to server (can be NULL) */
100
int secure; /* SSL/TLS mode of the connection */
101
apr_array_header_t *client_certs; /* Client certificates on this connection */
103
const char *reason; /* Reason for an error failure */
105
struct util_ldap_connection_t *next;
106
} util_ldap_connection_t;
108
/* LDAP cache state information */
109
typedef struct util_ldap_state_t {
110
apr_pool_t *pool; /* pool from which this state is allocated */
112
apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
114
apr_global_mutex_t *util_ldap_cache_lock;
116
apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
117
char *cache_file; /* filename for shm */
118
long search_cache_ttl; /* TTL for search cache */
119
long search_cache_size; /* Size (in entries) of search cache */
120
long compare_cache_ttl; /* TTL for compare cache */
121
long compare_cache_size; /* Size (in entries) of compare cache */
123
struct util_ldap_connection_t *connections;
125
apr_array_header_t *global_certs; /* Global CA certificates */
126
apr_array_header_t *client_certs; /* Client certificates */
130
#if APR_HAS_SHARED_MEMORY
131
apr_shm_t *cache_shm;
132
apr_rmm_t *cache_rmm;
136
void *util_ldap_cache;
137
char *lock_file; /* filename for shm lock mutex */
138
long connectionTimeout;
145
* Open a connection to an LDAP server
146
* @param ldc A structure containing the expanded details of the server
147
* to connect to. The handle to the LDAP connection is returned
149
* @tip This function connects to the LDAP server and binds. It does not
150
* connect if already connected (ldc->ldap != NULL). Does not bind
152
* @return If successful LDAP_SUCCESS is returned.
153
* @deffunc int util_ldap_connection_open(request_rec *r,
154
* util_ldap_connection_t *ldc)
156
APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
157
util_ldap_connection_t *ldc));
160
* Close a connection to an LDAP server
161
* @param ldc A structure containing the expanded details of the server
162
* that was connected.
163
* @tip This function unbinds from the LDAP server, and clears ldc->ldap.
164
* It is possible to rebind to this server again using the same ldc
165
* structure, using apr_ldap_open_connection().
166
* @deffunc util_ldap_close_connection(util_ldap_connection_t *ldc)
168
APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
171
* Unbind a connection to an LDAP server
172
* @param ldc A structure containing the expanded details of the server
173
* that was connected.
174
* @tip This function unbinds the LDAP connection, and disconnects from
175
* the server. It is used during error conditions, to bring the LDAP
176
* connection back to a known state.
177
* @deffunc apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
179
APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
182
* Cleanup a connection to an LDAP server
183
* @param ldc A structure containing the expanded details of the server
184
* that was connected.
185
* @tip This function is registered with the pool cleanup to close down the
186
* LDAP connections when the server is finished with them.
187
* @deffunc apr_status_t util_ldap_connection_cleanup(util_ldap_connection_t *ldc)
189
APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_cleanup,(void *param));
192
* Find a connection in a list of connections
193
* @param r The request record
194
* @param host The hostname to connect to (multiple hosts space separated)
195
* @param port The port to connect to
196
* @param binddn The DN to bind with
197
* @param bindpw The password to bind with
198
* @param deref The dereferencing behavior
199
* @param secure use SSL on the connection
200
* @tip Once a connection is found and returned, a lock will be acquired to
201
* lock that particular connection, so that another thread does not try and
202
* use this connection while it is busy. Once you are finished with a connection,
203
* apr_ldap_connection_close() must be called to release this connection.
204
* @deffunc util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
205
* const char *binddn, const char *bindpw, deref_options deref,
206
* int netscapessl, int starttls)
208
APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
209
const char *binddn, const char *bindpw, deref_options deref,
213
* Compare two DNs for sameness
214
* @param r The request record
215
* @param ldc The LDAP connection being used.
216
* @param url The URL of the LDAP connection - used for deciding which cache to use.
217
* @param dn The first DN to compare.
218
* @param reqdn The DN to compare the first DN to.
219
* @param compare_dn_on_server Flag to determine whether the DNs should be checked using
220
* LDAP calls or with a direct string comparision. A direct
221
* string comparison is faster, but not as accurate - false
222
* negative comparisons are possible.
223
* @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
224
* and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
225
* performance issues.
226
* @deffunc int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
227
* const char *url, const char *dn, const char *reqdn,
228
* int compare_dn_on_server)
230
APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
231
const char *url, const char *dn, const char *reqdn,
232
int compare_dn_on_server));
235
* A generic LDAP compare function
236
* @param r The request record
237
* @param ldc The LDAP connection being used.
238
* @param url The URL of the LDAP connection - used for deciding which cache to use.
239
* @param dn The DN of the object in which we do the compare.
240
* @param attrib The attribute within the object we are comparing for.
241
* @param value The value of the attribute we are trying to compare for.
242
* @tip Use this function to determine whether an attribute/value pair exists within an
243
* object. Typically this would be used to determine LDAP group membership.
244
* @deffunc int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
245
* const char *url, const char *dn, const char *attrib, const char *value)
247
APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
248
const char *url, const char *dn, const char *attrib, const char *value));
251
* Checks a username/password combination by binding to the LDAP server
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 basedn The Base DN to search for the user in.
256
* @param scope LDAP scope of the search.
257
* @param attrs LDAP attributes to return in search.
258
* @param filter The user to search for in the form of an LDAP filter. This filter must return
259
* exactly one user for the check to be successful.
260
* @param bindpw The user password to bind as.
261
* @param binddn The DN of the user will be returned in this variable.
262
* @param retvals The values corresponding to the attributes requested in the attrs array.
263
* @tip The filter supplied will be searched for. If a single entry is returned, an attempt
264
* is made to bind as that user. If this bind succeeds, the user is not validated.
265
* @deffunc int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
266
* char *url, const char *basedn, int scope, char **attrs,
267
* char *filter, char *bindpw, char **binddn, char ***retvals)
269
APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
270
const char *url, const char *basedn, int scope, char **attrs,
271
const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
274
* Searches for a specified user object in an LDAP directory
275
* @param r The request record
276
* @param ldc The LDAP connection being used.
277
* @param url The URL of the LDAP connection - used for deciding which cache to use.
278
* @param basedn The Base DN to search for the user in.
279
* @param scope LDAP scope of the search.
280
* @param attrs LDAP attributes to return in search.
281
* @param filter The user to search for in the form of an LDAP filter. This filter must return
282
* exactly one user for the check to be successful.
283
* @param binddn The DN of the user will be returned in this variable.
284
* @param retvals The values corresponding to the attributes requested in the attrs array.
285
* @tip The filter supplied will be searched for. If a single entry is returned, an attempt
286
* is made to bind as that user. If this bind succeeds, the user is not validated.
287
* @deffunc int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
288
* char *url, const char *basedn, int scope, char **attrs,
289
* char *filter, char **binddn, char ***retvals)
291
APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
292
const char *url, const char *basedn, int scope, char **attrs,
293
const char *filter, const char **binddn, const char ***retvals));
296
* Checks if SSL support is available in mod_ldap
297
* @deffunc int util_ldap_ssl_supported(request_rec *r)
299
APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
301
/* from apr_ldap_cache.c */
304
* Init the LDAP cache
305
* @param pool The pool to use to initialise the cache
306
* @param reqsize The size of the shared memory segement to request. A size
307
* of zero requests the max size possible from
309
* @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
310
* @return The status code returned is the status code of the
311
* apr_smmem_init() call. Regardless of the status, the cache
312
* will be set up at least for in-process or in-thread operation.
314
apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
316
/* from apr_ldap_cache_mgr.c */
319
* Display formatted stats for cache
320
* @param The pool to allocate the returned string from
321
* @tip This function returns a string allocated from the provided pool that describes
322
* various stats about the cache.
323
* @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
325
char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
327
#endif /* APR_HAS_LDAP */
328
#endif /* UTIL_LDAP_H */