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.
21 /* this whole thing disappears if LDAP is not enabled */
24 /* APR header files */
25 #include <apr_thread_mutex.h>
26 #include <apr_thread_rwlock.h>
27 #include <apr_tables.h>
30 /* Apache header files */
31 #include "ap_config.h"
33 #include "http_config.h"
34 #include "http_core.h"
36 #include "http_protocol.h"
37 #include "http_request.h"
39 #if APR_HAS_SHARED_MEMORY
44 /* Create a set of LDAP_DECLARE(type), LDLDAP_DECLARE(type) and
45 * LDAP_DECLARE_DATA with appropriate export 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; /* True if use SSL connection */
95 const char *reason; /* Reason for an error failure */
97 struct util_ldap_connection_t *next;
98 } util_ldap_connection_t;
100 /* LDAP cache state information */
101 typedef struct util_ldap_state_t {
102 apr_pool_t *pool; /* pool from which this state is allocated */
104 apr_thread_mutex_t *mutex; /* mutex lock for the connection list */
105 apr_thread_rwlock_t *util_ldap_cache_lock;
108 apr_size_t cache_bytes; /* Size (in bytes) of shared memory cache */
109 char *cache_file; /* filename for shm */
110 long search_cache_ttl; /* TTL for search cache */
111 long search_cache_size; /* Size (in entries) of search cache */
112 long compare_cache_ttl; /* TTL for compare cache */
113 long compare_cache_size; /* Size (in entries) of compare cache */
115 struct util_ldap_connection_t *connections;
116 char *cert_auth_file;
120 #if APR_HAS_SHARED_MEMORY
121 apr_shm_t *cache_shm;
122 apr_rmm_t *cache_rmm;
126 void *util_ldap_cache;
132 * Open a connection to an LDAP server
133 * @param ldc A structure containing the expanded details of the server
134 * to connect to. The handle to the LDAP connection is returned
136 * @tip This function connects to the LDAP server and binds. It does not
137 * connect if already connected (ldc->ldap != NULL). Does not bind
139 * @return If successful LDAP_SUCCESS is returned.
140 * @deffunc int util_ldap_connection_open(request_rec *r,
141 * util_ldap_connection_t *ldc)
143 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r,
144 util_ldap_connection_t *ldc);
147 * Close a connection to an LDAP server
148 * @param ldc A structure containing the expanded details of the server
149 * that was connected.
150 * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
151 * It is possible to rebind to this server again using the same ldc
152 * structure, using apr_ldap_open_connection().
153 * @deffunc util_ldap_close_connection(util_ldap_connection_t *ldc)
155 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc);
158 * Destroy a connection to an LDAP server
159 * @param ldc A structure containing the expanded details of the server
160 * that was connected.
161 * @tip This function is registered with the pool cleanup to close down the
162 * LDAP connections when the server is finished with them.
163 * @deffunc apr_status_t util_ldap_connection_destroy(util_ldap_connection_t *ldc)
165 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_destroy(void *param);
168 * Find a connection in a list of connections
169 * @param r The request record
170 * @param host The hostname to connect to (multiple hosts space separated)
171 * @param port The port to connect to
172 * @param binddn The DN to bind with
173 * @param bindpw The password to bind with
174 * @param deref The dereferencing behavior
175 * @param secure use SSL on the connection
176 * @tip Once a connection is found and returned, a lock will be acquired to
177 * lock that particular connection, so that another thread does not try and
178 * use this connection while it is busy. Once you are finished with a connection,
179 * apr_ldap_connection_close() must be called to release this connection.
180 * @deffunc util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
181 * const char *binddn, const char *bindpw, deref_options deref,
182 * int netscapessl, int starttls)
184 LDAP_DECLARE(util_ldap_connection_t *) util_ldap_connection_find(request_rec *r, const char *host, int port,
185 const char *binddn, const char *bindpw, deref_options deref,
190 * Compare two DNs for sameness
191 * @param r The request record
192 * @param ldc The LDAP connection being used.
193 * @param url The URL of the LDAP connection - used for deciding which cache to use.
194 * @param dn The first DN to compare.
195 * @param reqdn The DN to compare the first DN to.
196 * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
197 * LDAP calls or with a direct string comparision. A direct
198 * string comparison is faster, but not as accurate - false
199 * negative comparisons are possible.
200 * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
201 * and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
202 * performance issues.
203 * @deffunc int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
204 * const char *url, const char *dn, const char *reqdn,
205 * int compare_dn_on_server)
207 LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
208 const char *url, const char *dn, const char *reqdn,
209 int compare_dn_on_server);
212 * A generic LDAP compare function
213 * @param r The request record
214 * @param ldc The LDAP connection being used.
215 * @param url The URL of the LDAP connection - used for deciding which cache to use.
216 * @param dn The DN of the object in which we do the compare.
217 * @param attrib The attribute within the object we are comparing for.
218 * @param value The value of the attribute we are trying to compare for.
219 * @tip Use this function to determine whether an attribute/value pair exists within an
220 * object. Typically this would be used to determine LDAP group membership.
221 * @deffunc int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
222 * const char *url, const char *dn, const char *attrib, const char *value)
224 LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
225 const char *url, const char *dn, const char *attrib, const char *value);
228 * Checks a username/password combination by binding to the LDAP server
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 basedn The Base DN to search for the user in.
233 * @param scope LDAP scope of the search.
234 * @param attrs LDAP attributes to return in search.
235 * @param filter The user to search for in the form of an LDAP filter. This filter must return
236 * exactly one user for the check to be successful.
237 * @param bindpw The user password to bind as.
238 * @param binddn The DN of the user will be returned in this variable.
239 * @param retvals The values corresponding to the attributes requested in the attrs array.
240 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
241 * is made to bind as that user. If this bind succeeds, the user is not validated.
242 * @deffunc int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
243 * char *url, const char *basedn, int scope, char **attrs,
244 * char *filter, char *bindpw, char **binddn, char ***retvals)
246 LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
247 const char *url, const char *basedn, int scope, char **attrs,
248 const char *filter, const char *bindpw, const char **binddn, const char ***retvals);
251 * Checks if SSL support is available in mod_ldap
252 * @deffunc int util_ldap_ssl_supported(request_rec *r)
254 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r);
256 /* from apr_ldap_cache.c */
259 * Init the LDAP cache
260 * @param pool The pool to use to initialise the cache
261 * @param reqsize The size of the shared memory segement to request. A size
262 * of zero requests the max size possible from
264 * @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
265 * @return The status code returned is the status code of the
266 * apr_smmem_init() call. Regardless of the status, the cache
267 * will be set up at least for in-process or in-thread operation.
269 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
272 * Display formatted stats for cache
273 * @param The pool to allocate the returned string from
274 * @tip This function returns a string allocated from the provided pool that describes
275 * various stats about the cache.
276 * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
278 char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
281 /* from apr_ldap_cache_mgr.c */
284 * Display formatted stats for cache
285 * @param The pool to allocate the returned string from
286 * @tip This function returns a string allocated from the provided pool that describes
287 * various stats about the cache.
288 * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
290 char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
292 #endif /* APR_HAS_LDAP */
293 #endif /* UTIL_LDAP_H */