]> granicus.if.org Git - apache/blob - include/util_ldap.h
Fix spelling in comments and text files.
[apache] / include / util_ldap.h
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_version.h"
32 #if APR_MAJOR_VERSION < 2
33 /* The LDAP API is currently only present in APR 1.x */
34 #include "apr_ldap.h"
35 #include "apr_ldap_rebind.h"
36 #else
37 #define APR_HAS_LDAP 0
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 #if defined(LDAP_UNAVAILABLE) || APR_HAS_MICROSOFT_LDAPSDK
49 #define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN \
50                 ||(s) == LDAP_UNAVAILABLE)
51 #else
52 #define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN)
53 #endif
54
55 /* Apache header files */
56 #include "ap_config.h"
57 #include "httpd.h"
58 #include "http_config.h"
59 #include "http_core.h"
60 #include "http_log.h"
61 #include "http_protocol.h"
62 #include "http_request.h"
63 #include "apr_optional.h"
64
65 /* Create a set of LDAP_DECLARE macros with appropriate export
66  * and import tags for the platform
67  */
68 #if !defined(WIN32)
69 #define LDAP_DECLARE(type)            type
70 #define LDAP_DECLARE_NONSTD(type)     type
71 #define LDAP_DECLARE_DATA
72 #elif defined(LDAP_DECLARE_STATIC)
73 #define LDAP_DECLARE(type)            type __stdcall
74 #define LDAP_DECLARE_NONSTD(type)     type
75 #define LDAP_DECLARE_DATA
76 #elif defined(LDAP_DECLARE_EXPORT)
77 #define LDAP_DECLARE(type)            __declspec(dllexport) type __stdcall
78 #define LDAP_DECLARE_NONSTD(type)     __declspec(dllexport) type
79 #define LDAP_DECLARE_DATA             __declspec(dllexport)
80 #else
81 #define LDAP_DECLARE(type)            __declspec(dllimport) type __stdcall
82 #define LDAP_DECLARE_NONSTD(type)     __declspec(dllimport) type
83 #define LDAP_DECLARE_DATA             __declspec(dllimport)
84 #endif
85
86 #if APR_HAS_MICROSOFT_LDAPSDK
87 #define timeval l_timeval
88 #endif
89
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93
94 /*
95  * LDAP Connections
96  */
97
98 /* Values that the deref member can have */
99 typedef enum {
100     never=LDAP_DEREF_NEVER,
101     searching=LDAP_DEREF_SEARCHING,
102     finding=LDAP_DEREF_FINDING,
103     always=LDAP_DEREF_ALWAYS
104 } deref_options;
105
106 /* Structure representing an LDAP connection */
107 typedef struct util_ldap_connection_t {
108     LDAP *ldap;
109     apr_pool_t *pool;                   /* Pool from which this connection is created */
110 #if APR_HAS_THREADS
111     apr_thread_mutex_t *lock;           /* Lock to indicate this connection is in use */
112 #endif
113
114     const char *host;                   /* Name of the LDAP server (or space separated list) */
115     int port;                           /* Port of the LDAP server */
116     deref_options deref;                /* how to handle alias dereferening */
117
118     const char *binddn;                 /* DN to bind to server (can be NULL) */
119     const char *bindpw;                 /* Password to bind to server (can be NULL) */
120
121     int bound;                          /* Flag to indicate whether this connection is bound yet */
122
123     int secure;                         /* SSL/TLS mode of the connection */
124     apr_array_header_t *client_certs;   /* Client certificates on this connection */
125
126     const char *reason;                 /* Reason for an error failure */
127
128     struct util_ldap_connection_t *next;
129     struct util_ldap_state_t *st;        /* The LDAP vhost config this connection belongs to */
130     int keep;                            /* Will this connection be kept when it's unlocked */
131
132     int ChaseReferrals;                 /* [on|off] (default = AP_LDAP_CHASEREFERRALS_ON)*/
133     int ReferralHopLimit;               /* # of referral hops to follow (default = AP_LDAP_DEFAULT_HOPLIMIT) */
134     apr_time_t freed;                   /* the time this conn was placed back in the pool */
135     apr_pool_t *rebind_pool;            /* frequently cleared pool for rebind data */
136     int must_rebind;                    /* The connection was last bound with other then binddn/bindpw */
137     request_rec *r;                     /* request_rec used to find this util_ldap_connection_t */
138     apr_time_t last_backend_conn;       /* the approximate time of the last backend LDAP requst */
139 } util_ldap_connection_t;
140
141 typedef struct util_ldap_config_t {
142     int ChaseReferrals;
143     int ReferralHopLimit;
144     apr_array_header_t *client_certs;  /* Client certificates */
145 } util_ldap_config_t;
146
147 /* LDAP cache state information */
148 typedef struct util_ldap_state_t {
149     apr_pool_t *pool;           /* pool from which this state is allocated */
150 #if APR_HAS_THREADS
151     apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
152 #endif
153     apr_global_mutex_t *util_ldap_cache_lock;
154
155     apr_size_t cache_bytes;     /* Size (in bytes) of shared memory cache */
156     char *cache_file;           /* filename for shm */
157     long search_cache_ttl;      /* TTL for search cache */
158     long search_cache_size;     /* Size (in entries) of search cache */
159     long compare_cache_ttl;     /* TTL for compare cache */
160     long compare_cache_size;    /* Size (in entries) of compare cache */
161
162     struct util_ldap_connection_t *connections;
163     apr_array_header_t *global_certs;  /* Global CA certificates */
164     int   ssl_supported;
165     int   secure;
166     int   secure_set;
167     int   verify_svr_cert;
168
169 #if APR_HAS_SHARED_MEMORY
170     apr_shm_t *cache_shm;
171     apr_rmm_t *cache_rmm;
172 #endif
173
174     /* cache ald */
175     void *util_ldap_cache;
176
177     long  connectionTimeout;
178     struct timeval *opTimeout;
179
180     int debug_level;                    /* SDK debug level */
181     apr_interval_time_t connection_pool_ttl;
182     int retries;                        /* number of retries for failed bind/search/compare */
183     apr_interval_time_t retry_delay;    /* delay between retries of failed bind/search/compare */
184 } util_ldap_state_t;
185
186 /* Used to store arrays of attribute labels/values. */
187 struct mod_auth_ldap_groupattr_entry_t {
188     char *name;
189 };
190
191 /**
192  * Open a connection to an LDAP server
193  * @param ldc A structure containing the expanded details of the server
194  *            to connect to. The handle to the LDAP connection is returned
195  *            as ldc->ldap.
196  * @tip This function connects to the LDAP server and binds. It does not
197  *      connect if already connected (ldc->ldap != NULL). Does not bind
198  *      if already bound.
199  * @return If successful LDAP_SUCCESS is returned.
200  * @fn int util_ldap_connection_open(request_rec *r,
201  *                                        util_ldap_connection_t *ldc)
202  */
203 APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
204                                             util_ldap_connection_t *ldc));
205
206 /**
207  * Close a connection to an LDAP server
208  * @param ldc A structure containing the expanded details of the server
209  *            that was connected.
210  * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
211  *      It is possible to rebind to this server again using the same ldc
212  *      structure, using apr_ldap_open_connection().
213  * @fn util_ldap_close_connection(util_ldap_connection_t *ldc)
214  */
215 APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
216
217 /**
218  * Unbind a connection to an LDAP server
219  * @param ldc A structure containing the expanded details of the server
220  *            that was connected.
221  * @tip This function unbinds the LDAP connection, and disconnects from
222  *      the server. It is used during error conditions, to bring the LDAP
223  *      connection back to a known state.
224  * @fn apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
225  */
226 APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
227
228 /**
229  * Find a connection in a list of connections
230  * @param r The request record
231  * @param host The hostname to connect to (multiple hosts space separated)
232  * @param port The port to connect to
233  * @param binddn The DN to bind with
234  * @param bindpw The password to bind with
235  * @param deref The dereferencing behavior
236  * @param secure use SSL on the connection
237  * @tip Once a connection is found and returned, a lock will be acquired to
238  *      lock that particular connection, so that another thread does not try and
239  *      use this connection while it is busy. Once you are finished with a connection,
240  *      apr_ldap_connection_close() must be called to release this connection.
241  * @fn util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
242  *                                                           const char *binddn, const char *bindpw, deref_options deref,
243  *                                                           int netscapessl, int starttls)
244  */
245 APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
246                                                   const char *binddn, const char *bindpw, deref_options deref,
247                                                   int secure));
248
249 /**
250  * Compare two DNs for sameness
251  * @param r The request record
252  * @param ldc The LDAP connection being used.
253  * @param url The URL of the LDAP connection - used for deciding which cache to use.
254  * @param dn The first DN to compare.
255  * @param reqdn The DN to compare the first DN to.
256  * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
257  *                             LDAP calls or with a direct string comparison. A direct
258  *                             string comparison is faster, but not as accurate - false
259  *                             negative comparisons are possible.
260  * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
261  *      and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
262  *      performance issues.
263  * @fn int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
264  *                                        const char *url, const char *dn, const char *reqdn,
265  *                                        int compare_dn_on_server)
266  */
267 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
268                               const char *url, const char *dn, const char *reqdn,
269                               int compare_dn_on_server));
270
271 /**
272  * A generic LDAP compare function
273  * @param r The request record
274  * @param ldc The LDAP connection being used.
275  * @param url The URL of the LDAP connection - used for deciding which cache to use.
276  * @param dn The DN of the object in which we do the compare.
277  * @param attrib The attribute within the object we are comparing for.
278  * @param value The value of the attribute we are trying to compare for.
279  * @tip Use this function to determine whether an attribute/value pair exists within an
280  *      object. Typically this would be used to determine LDAP top-level group
281  *      membership.
282  * @fn int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
283  *                                      const char *url, const char *dn, const char *attrib, const char *value)
284  */
285 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
286                             const char *url, const char *dn, const char *attrib, const char *value));
287
288 /**
289  * An LDAP function that checks if the specified user is a member of a subgroup.
290  * @param r The request record
291  * @param ldc The LDAP connection being used.
292  * @param url The URL of the LDAP connection - used for deciding which cache to use.
293  * @param dn The DN of the object in which we find subgroups to search within.
294  * @param attrib The attribute within group objects that identify users.
295  * @param value The user attribute value we are trying to compare for.
296  * @param subgroupAttrs The attributes within group objects that identify subgroups.
297  *                      Array of strings.
298  * @param subgroupclasses The objectClass values used to identify groups (and
299  *                      subgroups). apr_array_header_t *.
300  * @param cur_subgroup_depth Current recursive depth during subgroup processing.
301  * @param max_subgroup_depth Maximum depth of recursion allowed during subgroup
302  *                           processing.
303  * @tip Use this function to determine whether an attribute/value pair exists within a
304  *      starting group object or one of its nested subgroups. Typically this would be
305  *      used to determine LDAP nested group membership.
306  * @deffunc int util_ldap_cache_check_subgroups(request_rec *r, util_ldap_connection_t
307  *                                      *ldc, const char *url, const char *dn,
308  *                                      const char *attrib, const char value,
309  *                                      char **subgroupAttrs, apr_array_header_t
310  *                                      *subgroupclasses, int cur_subgroup_depth, int
311  *                                      max_subgroup_depth )
312  */
313 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_check_subgroups,(request_rec *r, util_ldap_connection_t *ldc,
314                                        const char *url, const char *dn, const char *attrib, const char *value,
315                                        char **subgroupAttrs, apr_array_header_t *subgroupclasses,
316                                        int cur_subgroup_depth, int max_subgroup_depth));
317
318 /**
319  * Checks a username/password combination by binding to the LDAP server
320  * @param r The request record
321  * @param ldc The LDAP connection being used.
322  * @param url The URL of the LDAP connection - used for deciding which cache to use.
323  * @param basedn The Base DN to search for the user in.
324  * @param scope LDAP scope of the search.
325  * @param attrs LDAP attributes to return in search.
326  * @param filter The user to search for in the form of an LDAP filter. This filter must return
327  *               exactly one user for the check to be successful.
328  * @param bindpw The user password to bind as.
329  * @param binddn The DN of the user will be returned in this variable.
330  * @param retvals The values corresponding to the attributes requested in the attrs array.
331  * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
332  *      is made to bind as that user. If this bind succeeds, the user is not validated.
333  * @fn int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
334  *                                          char *url, const char *basedn, int scope, char **attrs,
335  *                                          char *filter, char *bindpw, char **binddn, char ***retvals)
336  */
337 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
338                               const char *url, const char *basedn, int scope, char **attrs,
339                               const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
340
341 /**
342  * Searches for a specified user object in an LDAP directory
343  * @param r The request record
344  * @param ldc The LDAP connection being used.
345  * @param url The URL of the LDAP connection - used for deciding which cache to use.
346  * @param basedn The Base DN to search for the user in.
347  * @param scope LDAP scope of the search.
348  * @param attrs LDAP attributes to return in search.
349  * @param filter The user to search for in the form of an LDAP filter. This filter must return
350  *               exactly one user for the check to be successful.
351  * @param binddn The DN of the user will be returned in this variable.
352  * @param retvals The values corresponding to the attributes requested in the attrs array.
353  * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
354  *      is made to bind as that user. If this bind succeeds, the user is not validated.
355  * @fn int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
356  *                                          char *url, const char *basedn, int scope, char **attrs,
357  *                                          char *filter, char **binddn, char ***retvals)
358  */
359 APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
360                               const char *url, const char *basedn, int scope, char **attrs,
361                               const char *filter, const char **binddn, const char ***retvals));
362
363 /**
364  * Checks if SSL support is available in mod_ldap
365  * @fn int util_ldap_ssl_supported(request_rec *r)
366  */
367 APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
368
369 /* from apr_ldap_cache.c */
370
371 /**
372  * Init the LDAP cache
373  * @param pool The pool to use to initialise the cache
374  * @param reqsize The size of the shared memory segment to request. A size
375  *                of zero requests the max size possible from
376  *                apr_shmem_init()
377  * @fn void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
378  * @return The status code returned is the status code of the
379  *         apr_smmem_init() call. Regardless of the status, the cache
380  *         will be set up at least for in-process or in-thread operation.
381  */
382 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
383
384 /* from apr_ldap_cache_mgr.c */
385
386 /**
387  * Display formatted stats for cache
388  * @param The pool to allocate the returned string from
389  * @tip This function returns a string allocated from the provided pool that describes
390  *      various stats about the cache.
391  * @fn char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
392  */
393 char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
394 #ifdef __cplusplus
395 }
396 #endif
397 #endif /* APR_HAS_LDAP */
398 #endif /* UTIL_LDAP_H */