]> granicus.if.org Git - apache/blob - include/util_ldap.h
update transformation
[apache] / include / util_ldap.h
1 /* Copyright 2001-2004 The Apache Software Foundation
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef UTIL_LDAP_H
17 #define UTIL_LDAP_H
18
19 #include <apr_ldap.h>
20
21 /* this whole thing disappears if LDAP is not enabled */
22 #if APR_HAS_LDAP
23
24 /* APR header files */
25 #include <apr_thread_mutex.h>
26 #include <apr_thread_rwlock.h>
27 #include <apr_tables.h>
28 #include <apr_time.h>
29
30 /* Apache header files */
31 #include "ap_config.h"
32 #include "httpd.h"
33 #include "http_config.h"
34 #include "http_core.h"
35 #include "http_log.h"
36 #include "http_protocol.h"
37 #include "http_request.h"
38
39 #if APR_HAS_SHARED_MEMORY
40 #include "apr_rmm.h"
41 #include "apr_shm.h"
42 #endif
43
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
46  */
47 #if !defined(WIN32)
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)
59 #else
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)
63 #endif
64
65 /*
66  * LDAP Connections
67  */
68
69 /* Values that the deref member can have */
70 typedef enum {
71     never=LDAP_DEREF_NEVER, 
72     searching=LDAP_DEREF_SEARCHING, 
73     finding=LDAP_DEREF_FINDING, 
74     always=LDAP_DEREF_ALWAYS
75 } deref_options;
76
77 /* Structure representing an LDAP connection */
78 typedef struct util_ldap_connection_t {
79     LDAP *ldap;
80     apr_pool_t *pool;                   /* Pool from which this connection is created */
81 #if APR_HAS_THREADS
82     apr_thread_mutex_t *lock;           /* Lock to indicate this connection is in use */
83 #endif
84     int bound;                          /* Flag to indicate whether this connection is bound yet */
85
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 */
89
90     const char *binddn;                 /* DN to bind to server (can be NULL) */
91     const char *bindpw;                 /* Password to bind to server (can be NULL) */
92
93     int secure;                         /* True if use SSL connection */
94
95     const char *reason;                 /* Reason for an error failure */
96
97     struct util_ldap_connection_t *next;
98 } util_ldap_connection_t;
99
100 /* LDAP cache state information */ 
101 typedef struct util_ldap_state_t {
102     apr_pool_t *pool;           /* pool from which this state is allocated */
103 #if APR_HAS_THREADS
104     apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
105     apr_thread_rwlock_t *util_ldap_cache_lock;
106 #endif
107
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 */
114
115     struct util_ldap_connection_t *connections;
116     char *cert_auth_file; 
117     int   cert_file_type;
118     int   ssl_support;
119
120 #if APR_HAS_SHARED_MEMORY
121     apr_shm_t *cache_shm;
122     apr_rmm_t *cache_rmm;
123 #endif
124
125     /* cache ald */
126     void *util_ldap_cache;
127
128 } util_ldap_state_t;
129
130
131 /**
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
135  *            as ldc->ldap.
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
138  *      if already bound.
139  * @return If successful LDAP_SUCCESS is returned.
140  * @deffunc int util_ldap_connection_open(request_rec *r,
141  *                                        util_ldap_connection_t *ldc)
142  */
143 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r, 
144                                             util_ldap_connection_t *ldc);
145
146 /**
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)
154  */
155 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc);
156
157 /**
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)
164  */
165 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_destroy(void *param);
166
167 /**
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)
183  */
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,
186                                                   int secure);
187
188
189 /**
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)
206  */
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);
210
211 /**
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)
223  */
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);
226
227 /**
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)
245  */
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);
249
250 /**
251  * Checks if SSL support is available in mod_ldap
252  * @deffunc int util_ldap_ssl_supported(request_rec *r)
253  */
254 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r);
255
256 /* from apr_ldap_cache.c */
257
258 /**
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
263  *                apr_shmem_init()
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.
268  */
269 apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
270
271 /**
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)
277  */
278 char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
279
280
281 /* from apr_ldap_cache_mgr.c */
282
283 /**
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)
289  */
290 char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
291
292 #endif /* APR_HAS_LDAP */
293 #endif /* UTIL_LDAP_H */