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