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
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/**
18 * @file util_ldap.h
19 * @brief Apache LDAP library
20 */
21
22#ifndef UTIL_LDAP_H
23#define UTIL_LDAP_H
24
25/* APR header files */
26#include "apr.h"
27#include "apr_thread_mutex.h"
28#include "apr_thread_rwlock.h"
29#include "apr_tables.h"
30#include "apr_time.h"
31#include "apr_ldap.h"
32
33#if defined(LDAP_UNAVAILABLE) || APR_HAS_MICROSOFT_LDAPSDK
34#define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN \
35                ||(s) == LDAP_UNAVAILABLE)
36#else
37#define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN)
38#endif
39
40#if APR_HAS_SHARED_MEMORY
41#include "apr_rmm.h"
42#include "apr_shm.h"
43#endif
44
45/* this whole thing disappears if LDAP is not enabled */
46#if APR_HAS_LDAP
47
48/* Apache header files */
49#include "ap_config.h"
50#include "httpd.h"
51#include "http_config.h"
52#include "http_core.h"
53#include "http_log.h"
54#include "http_protocol.h"
55#include "http_request.h"
56#include "apr_optional.h"
57
58/* Create a set of LDAP_DECLARE macros with appropriate export
59 * and import tags for the platform
60 */
61#if !defined(WIN32)
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)
73#else
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)
77#endif
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83/*
84 * LDAP Connections
85 */
86
87/* Values that the deref member can have */
88typedef enum {
89    never=LDAP_DEREF_NEVER,
90    searching=LDAP_DEREF_SEARCHING,
91    finding=LDAP_DEREF_FINDING,
92    always=LDAP_DEREF_ALWAYS
93} deref_options;
94
95/* Structure representing an LDAP connection */
96typedef struct util_ldap_connection_t {
97    LDAP *ldap;
98    apr_pool_t *pool;                   /* Pool from which this connection is created */
99#if APR_HAS_THREADS
100    apr_thread_mutex_t *lock;           /* Lock to indicate this connection is in use */
101#endif
102    int bound;                          /* Flag to indicate whether this connection is bound yet */
103
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 */
107
108    const char *binddn;                 /* DN to bind to server (can be NULL) */
109    const char *bindpw;                 /* Password to bind to server (can be NULL) */
110
111    int secure;                         /* SSL/TLS mode of the connection */
112    apr_array_header_t *client_certs;   /* Client certificates on this connection */
113
114    const char *reason;                 /* Reason for an error failure */
115
116    struct util_ldap_connection_t *next;
117} util_ldap_connection_t;
118
119/* LDAP cache state information */
120typedef struct util_ldap_state_t {
121    apr_pool_t *pool;           /* pool from which this state is allocated */
122#if APR_HAS_THREADS
123    apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
124#endif
125    apr_global_mutex_t *util_ldap_cache_lock;
126
127    apr_size_t cache_bytes;     /* Size (in bytes) of shared memory cache */
128    char *cache_file;           /* filename for shm */
129    long search_cache_ttl;      /* TTL for search cache */
130    long search_cache_size;     /* Size (in entries) of search cache */
131    long compare_cache_ttl;     /* TTL for compare cache */
132    long compare_cache_size;    /* Size (in entries) of compare cache */
133
134    struct util_ldap_connection_t *connections;
135    int   ssl_supported;
136    apr_array_header_t *global_certs;  /* Global CA certificates */
137    apr_array_header_t *client_certs;  /* Client certificates */
138    int   secure;
139    int   secure_set;
140
141#if APR_HAS_SHARED_MEMORY
142    apr_shm_t *cache_shm;
143    apr_rmm_t *cache_rmm;
144#endif
145
146    /* cache ald */
147    void *util_ldap_cache;
148    char *lock_file;           /* filename for shm lock mutex */
149    long  connectionTimeout;
150    int   verify_svr_cert;
151
152} util_ldap_state_t;
153
154
155/**
156 * Open a connection to an LDAP server
157 * @param ldc A structure containing the expanded details of the server
158 *            to connect to. The handle to the LDAP connection is returned
159 *            as ldc->ldap.
160 * @tip This function connects to the LDAP server and binds. It does not
161 *      connect if already connected (ldc->ldap != NULL). Does not bind
162 *      if already bound.
163 * @return If successful LDAP_SUCCESS is returned.
164 * @deffunc int util_ldap_connection_open(request_rec *r,
165 *                                        util_ldap_connection_t *ldc)
166 */
167APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
168                                            util_ldap_connection_t *ldc));
169
170/**
171 * Close 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 from the LDAP server, and clears ldc->ldap.
175 *      It is possible to rebind to this server again using the same ldc
176 *      structure, using apr_ldap_open_connection().
177 * @deffunc util_ldap_close_connection(util_ldap_connection_t *ldc)
178 */
179APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
180
181/**
182 * Unbind 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 unbinds the LDAP connection, and disconnects from
186 *      the server. It is used during error conditions, to bring the LDAP
187 *      connection back to a known state.
188 * @deffunc apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
189 */
190APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
191
192/**
193 * Cleanup a connection to an LDAP server
194 * @param ldc A structure containing the expanded details of the server
195 *            that was connected.
196 * @tip This function is registered with the pool cleanup to close down the
197 *      LDAP connections when the server is finished with them.
198 * @deffunc apr_status_t util_ldap_connection_cleanup(util_ldap_connection_t *ldc)
199 */
200APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_cleanup,(void *param));
201
202/**
203 * Find a connection in a list of connections
204 * @param r The request record
205 * @param host The hostname to connect to (multiple hosts space separated)
206 * @param port The port to connect to
207 * @param binddn The DN to bind with
208 * @param bindpw The password to bind with
209 * @param deref The dereferencing behavior
210 * @param secure use SSL on the connection
211 * @tip Once a connection is found and returned, a lock will be acquired to
212 *      lock that particular connection, so that another thread does not try and
213 *      use this connection while it is busy. Once you are finished with a connection,
214 *      apr_ldap_connection_close() must be called to release this connection.
215 * @deffunc util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
216 *                                                           const char *binddn, const char *bindpw, deref_options deref,
217 *                                                           int netscapessl, int starttls)
218 */
219APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
220                                                  const char *binddn, const char *bindpw, deref_options deref,
221                                                  int secure));
222
223/**
224 * Compare two DNs for sameness
225 * @param r The request record
226 * @param ldc The LDAP connection being used.
227 * @param url The URL of the LDAP connection - used for deciding which cache to use.
228 * @param dn The first DN to compare.
229 * @param reqdn The DN to compare the first DN to.
230 * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
231 *                             LDAP calls or with a direct string comparision. A direct
232 *                             string comparison is faster, but not as accurate - false
233 *                             negative comparisons are possible.
234 * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
235 *      and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
236 *      performance issues.
237 * @deffunc int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
238 *                                        const char *url, const char *dn, const char *reqdn,
239 *                                        int compare_dn_on_server)
240 */
241APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
242                              const char *url, const char *dn, const char *reqdn,
243                              int compare_dn_on_server));
244
245/**
246 * A generic LDAP compare function
247 * @param r The request record
248 * @param ldc The LDAP connection being used.
249 * @param url The URL of the LDAP connection - used for deciding which cache to use.
250 * @param dn The DN of the object in which we do the compare.
251 * @param attrib The attribute within the object we are comparing for.
252 * @param value The value of the attribute we are trying to compare for.
253 * @tip Use this function to determine whether an attribute/value pair exists within an
254 *      object. Typically this would be used to determine LDAP group membership.
255 * @deffunc int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
256 *                                      const char *url, const char *dn, const char *attrib, const char *value)
257 */
258APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
259                            const char *url, const char *dn, const char *attrib, const char *value));
260
261/**
262 * Checks a username/password combination by binding to the LDAP server
263 * @param r The request record
264 * @param ldc The LDAP connection being used.
265 * @param url The URL of the LDAP connection - used for deciding which cache to use.
266 * @param basedn The Base DN to search for the user in.
267 * @param scope LDAP scope of the search.
268 * @param attrs LDAP attributes to return in search.
269 * @param filter The user to search for in the form of an LDAP filter. This filter must return
270 *               exactly one user for the check to be successful.
271 * @param bindpw The user password to bind as.
272 * @param binddn The DN of the user will be returned in this variable.
273 * @param retvals The values corresponding to the attributes requested in the attrs array.
274 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
275 *      is made to bind as that user. If this bind succeeds, the user is not validated.
276 * @deffunc int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
277 *                                          char *url, const char *basedn, int scope, char **attrs,
278 *                                          char *filter, char *bindpw, char **binddn, char ***retvals)
279 */
280APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
281                              const char *url, const char *basedn, int scope, char **attrs,
282                              const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
283
284/**
285 * Searches for a specified user object in an LDAP directory
286 * @param r The request record
287 * @param ldc The LDAP connection being used.
288 * @param url The URL of the LDAP connection - used for deciding which cache to use.
289 * @param basedn The Base DN to search for the user in.
290 * @param scope LDAP scope of the search.
291 * @param attrs LDAP attributes to return in search.
292 * @param filter The user to search for in the form of an LDAP filter. This filter must return
293 *               exactly one user for the check to be successful.
294 * @param binddn The DN of the user will be returned in this variable.
295 * @param retvals The values corresponding to the attributes requested in the attrs array.
296 * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
297 *      is made to bind as that user. If this bind succeeds, the user is not validated.
298 * @deffunc int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
299 *                                          char *url, const char *basedn, int scope, char **attrs,
300 *                                          char *filter, char **binddn, char ***retvals)
301 */
302APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
303                              const char *url, const char *basedn, int scope, char **attrs,
304                              const char *filter, const char **binddn, const char ***retvals));
305
306/**
307 * Checks if SSL support is available in mod_ldap
308 * @deffunc int util_ldap_ssl_supported(request_rec *r)
309 */
310APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
311
312/* from apr_ldap_cache.c */
313
314/**
315 * Init the LDAP cache
316 * @param pool The pool to use to initialise the cache
317 * @param reqsize The size of the shared memory segement to request. A size
318 *                of zero requests the max size possible from
319 *                apr_shmem_init()
320 * @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
321 * @return The status code returned is the status code of the
322 *         apr_smmem_init() call. Regardless of the status, the cache
323 *         will be set up at least for in-process or in-thread operation.
324 */
325apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
326
327/* from apr_ldap_cache_mgr.c */
328
329/**
330 * Display formatted stats for cache
331 * @param The pool to allocate the returned string from
332 * @tip This function returns a string allocated from the provided pool that describes
333 *      various stats about the cache.
334 * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
335 */
336char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
337#ifdef __cplusplus
338}
339#endif
340#endif /* APR_HAS_LDAP */
341#endif /* UTIL_LDAP_H */
342