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