]> granicus.if.org Git - apache/blob - modules/ldap/util_ldap.c
End abuse of apr_strnat[case]cmp - it isn't str[case]cmp.
[apache] / modules / ldap / util_ldap.c
1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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  * util_ldap.c: LDAP things
19  * 
20  * Original code from auth_ldap module for Apache v1.3:
21  * Copyright 1998, 1999 Enbridge Pipelines Inc. 
22  * Copyright 1999-2001 Dave Carrigan
23  */
24
25 #include "httpd.h"
26 #include "http_config.h"
27 #include "http_core.h"
28 #include "http_log.h"
29 #include "http_protocol.h"
30 #include "http_request.h"
31 #include "util_ldap.h"
32 #include "util_ldap_cache.h"
33
34 #include <apr_strings.h>
35
36 #if APR_HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #if !APR_HAS_LDAP
41 #error mod_ldap requires APR-util to have LDAP support built in
42 #endif
43
44 #ifdef AP_NEED_SET_MUTEX_PERMS
45 #include "unixd.h"
46 #endif
47
48     /* defines for certificate file types
49     */
50 #define LDAP_CA_TYPE_UNKNOWN            0
51 #define LDAP_CA_TYPE_DER                1
52 #define LDAP_CA_TYPE_BASE64             2
53 #define LDAP_CA_TYPE_CERT7_DB           3
54
55
56 module AP_MODULE_DECLARE_DATA ldap_module;
57
58 static int util_ldap_handler(request_rec *r);
59 static void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
60
61
62 /*
63  * Some definitions to help between various versions of apache.
64  */
65
66 #ifndef DOCTYPE_HTML_2_0
67 #define DOCTYPE_HTML_2_0  "<!DOCTYPE HTML PUBLIC \"-//IETF//" \
68                           "DTD HTML 2.0//EN\">\n"
69 #endif
70
71 #ifndef DOCTYPE_HTML_3_2
72 #define DOCTYPE_HTML_3_2  "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
73                           "DTD HTML 3.2 Final//EN\">\n"
74 #endif
75
76 #ifndef DOCTYPE_HTML_4_0S
77 #define DOCTYPE_HTML_4_0S "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
78                           "DTD HTML 4.0//EN\"\n" \
79                           "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
80 #endif
81
82 #ifndef DOCTYPE_HTML_4_0T
83 #define DOCTYPE_HTML_4_0T "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
84                           "DTD HTML 4.0 Transitional//EN\"\n" \
85                           "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
86 #endif
87
88 #ifndef DOCTYPE_HTML_4_0F
89 #define DOCTYPE_HTML_4_0F "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
90                           "DTD HTML 4.0 Frameset//EN\"\n" \
91                           "\"http://www.w3.org/TR/REC-html40/frameset.dtd\">\n"
92 #endif
93
94 #define LDAP_CACHE_LOCK() \
95     if (st->util_ldap_cache_lock) \
96       apr_global_mutex_lock(st->util_ldap_cache_lock)
97 #define LDAP_CACHE_UNLOCK() \
98     if (st->util_ldap_cache_lock) \
99       apr_global_mutex_unlock(st->util_ldap_cache_lock)
100
101
102 static void util_ldap_strdup (char **str, const char *newstr)
103 {
104     if (*str) {
105         free(*str);
106         *str = NULL;
107     }
108
109     if (newstr) {
110         *str = calloc(1, strlen(newstr)+1);
111         strcpy (*str, newstr);
112     }
113 }
114
115 /*
116  * Status Handler
117  * --------------
118  *
119  * This handler generates a status page about the current performance of
120  * the LDAP cache. It is enabled as follows:
121  *
122  * <Location /ldap-status>
123  *   SetHandler ldap-status
124  * </Location>
125  *
126  */
127 static int util_ldap_handler(request_rec *r)
128 {
129     util_ldap_state_t *st = (util_ldap_state_t *)
130                             ap_get_module_config(r->server->module_config, 
131                                                  &ldap_module);
132
133     r->allowed |= (1 << M_GET);
134     if (r->method_number != M_GET)
135         return DECLINED;
136
137     if (strcmp(r->handler, "ldap-status")) {
138         return DECLINED;
139     }
140
141     r->content_type = "text/html";
142     if (r->header_only)
143         return OK;
144
145     ap_rputs(DOCTYPE_HTML_3_2
146              "<html><head><title>LDAP Cache Information</title></head>\n", r);
147     ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information"
148              "</h1>\n", r);
149
150     util_ald_cache_display(r, st);
151
152     return OK;
153 }
154
155 /* ------------------------------------------------------------------ */
156
157
158 /*
159  * Closes an LDAP connection by unlocking it. The next time
160  * uldap_connection_find() is called this connection will be
161  * available for reuse.
162  */
163 static void uldap_connection_close(util_ldap_connection_t *ldc)
164 {
165
166     /*
167      * QUESTION:
168      *
169      * Is it safe leaving bound connections floating around between the
170      * different modules? Keeping the user bound is a performance boost,
171      * but it is also a potential security problem - maybe.
172      *
173      * For now we unbind the user when we finish with a connection, but
174      * we don't have to...
175      */
176
177     /* mark our connection as available for reuse */
178
179 #if APR_HAS_THREADS
180     apr_thread_mutex_unlock(ldc->lock);
181 #endif
182 }
183
184
185 /*
186  * Destroys an LDAP connection by unbinding and closing the connection to
187  * the LDAP server. It is used to bring the connection back to a known
188  * state after an error, and during pool cleanup.
189  */
190 static apr_status_t uldap_connection_unbind(void *param)
191 {
192     util_ldap_connection_t *ldc = param;
193
194     if (ldc) {
195         if (ldc->ldap) {
196             ldap_unbind_s(ldc->ldap);
197             ldc->ldap = NULL;
198         }
199         ldc->bound = 0;
200     }
201
202     return APR_SUCCESS;
203 }
204
205
206 /*
207  * Clean up an LDAP connection by unbinding and unlocking the connection.
208  * This function is registered with the pool cleanup function - causing
209  * the LDAP connections to be shut down cleanly on graceful restart.
210  */
211 static apr_status_t uldap_connection_cleanup(void *param)
212 {
213     util_ldap_connection_t *ldc = param;
214
215     if (ldc) {
216
217         /* unbind and disconnect from the LDAP server */
218         uldap_connection_unbind(ldc);
219
220         /* free the username and password */
221         if (ldc->bindpw) {
222             free((void*)ldc->bindpw);
223         }
224         if (ldc->binddn) {
225             free((void*)ldc->binddn);
226         }
227
228         /* unlock this entry */
229         uldap_connection_close(ldc);
230     
231     }
232
233     return APR_SUCCESS;
234 }
235
236
237 /*
238  * Connect to the LDAP server and binds. Does not connect if already
239  * connected (i.e. ldc->ldap is non-NULL.) Does not bind if already bound.
240  *
241  * Returns LDAP_SUCCESS on success; and an error code on failure
242  */
243 static int uldap_connection_open(request_rec *r, 
244                                  util_ldap_connection_t *ldc)
245 {
246     int rc = 0;
247     int failures = 0;
248     int version  = LDAP_VERSION3;
249     apr_ldap_err_t *result = NULL;
250     struct timeval timeOut = {10,0};    /* 10 second connection timeout */
251     util_ldap_state_t *st = 
252         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
253         &ldap_module);
254
255     /* sanity check for NULL */
256     if (!ldc) {
257         return -1;
258     }
259
260     /* If the connection is already bound, return
261     */
262     if (ldc->bound)
263     {
264         ldc->reason = "LDAP: connection open successful (already bound)";
265         return LDAP_SUCCESS;
266     }
267
268     /* create the ldap session handle
269     */
270     if (NULL == ldc->ldap)
271     {
272         /* Since the host will include a port if the default port is not used,
273          * always specify the default ports for the port parameter.  This will
274          * allow a host string that contains multiple hosts the ability to mix
275          * some hosts with ports and some without. All hosts which do not
276          * specify a port will use the default port.
277          */
278         apr_ldap_init(ldc->pool, &(ldc->ldap),
279                       ldc->host,
280                       APR_LDAP_SSL == ldc->secure ? LDAPS_PORT : LDAP_PORT,
281                       APR_LDAP_NONE,
282                       &(result));
283
284
285         if (result != NULL && result->rc) {
286             ldc->reason = result->reason;
287         }
288
289         if (NULL == ldc->ldap)
290         {
291             ldc->bound = 0;
292             if (NULL == ldc->reason) {
293                 ldc->reason = "LDAP: ldap initialization failed";
294             }
295             else {
296                 ldc->reason = result->reason;
297             }
298             return(result->rc);
299         }
300
301         /* set client certificates */
302         if (!apr_is_empty_array(ldc->client_certs)) {
303             apr_ldap_set_option(ldc->pool, ldc->ldap, APR_LDAP_OPT_TLS_CERT,
304                                 ldc->client_certs, &(result));
305             if (LDAP_SUCCESS != result->rc) {
306                 ldap_unbind_s(ldc->ldap);
307                 ldc->ldap = NULL;
308                 ldc->bound = 0;
309                 ldc->reason = result->reason;
310                 return(result->rc);
311             }
312         }
313
314         /* switch on SSL/TLS */
315         if (APR_LDAP_NONE != ldc->secure) {
316             apr_ldap_set_option(ldc->pool, ldc->ldap, 
317                                 APR_LDAP_OPT_TLS, &ldc->secure, &(result));
318             if (LDAP_SUCCESS != result->rc) {
319                 ldap_unbind_s(ldc->ldap);
320                 ldc->ldap = NULL;
321                 ldc->bound = 0;
322                 ldc->reason = result->reason;
323                 return(result->rc);
324             }
325         }
326
327         /* Set the alias dereferencing option */
328         ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
329
330         /* always default to LDAP V3 */
331         ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
332
333 /*XXX All of the #ifdef's need to be removed once apr-util 1.2 is released */
334 #ifdef APR_LDAP_OPT_VERIFY_CERT
335         apr_ldap_set_option(ldc->pool, ldc->ldap, 
336                             APR_LDAP_OPT_VERIFY_CERT, &(st->verify_svr_cert), &(result));
337 #else
338 #if defined(LDAPSSL_VERIFY_SERVER)
339         if (st->verify_svr_cert) {
340             result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_SERVER);
341         }
342         else {
343             result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_NONE);
344         }
345 #elif defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
346                 /* This is not a per-connection setting so just pass NULL for the
347                    Ldap connection handle */
348         if (st->verify_svr_cert) {
349                         int i = LDAP_OPT_X_TLS_DEMAND;
350                         result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
351         }
352         else {
353                         int i = LDAP_OPT_X_TLS_NEVER;
354                         result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
355         }
356 #endif
357 #endif
358
359 #ifdef LDAP_OPT_NETWORK_TIMEOUT
360         if (st->connectionTimeout > 0) {
361             timeOut.tv_sec = st->connectionTimeout;
362         }
363     
364         if (st->connectionTimeout >= 0) {
365             rc = apr_ldap_set_option(ldc->pool, ldc->ldap, LDAP_OPT_NETWORK_TIMEOUT,
366                                      (void *)&timeOut, &(result));
367             if (APR_SUCCESS != rc) {
368                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
369                                  "LDAP: Could not set the connection timeout");
370             }
371         }
372 #endif
373
374     
375     }
376
377
378     /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
379      * returned.  Break out of the loop on Success or any other error.
380      *
381      * NOTE: Looping is probably not a great idea. If the server isn't 
382      * responding the chances it will respond after a few tries are poor.
383      * However, the original code looped and it only happens on
384      * the error condition.
385       */
386     for (failures=0; failures<10; failures++)
387     {
388         rc = ldap_simple_bind_s(ldc->ldap,
389                                 (char *)ldc->binddn,
390                                 (char *)ldc->bindpw);
391         if (LDAP_SERVER_DOWN != rc) {
392             break;
393         }
394     }
395
396     /* free the handle if there was an error
397     */
398     if (LDAP_SUCCESS != rc)
399     {
400         ldap_unbind_s(ldc->ldap);
401         ldc->ldap = NULL;
402         ldc->bound = 0;
403         ldc->reason = "LDAP: ldap_simple_bind_s() failed";
404     }
405     else {
406         ldc->bound = 1;
407         ldc->reason = "LDAP: connection open successful";
408     }
409
410     return(rc);
411 }
412
413
414 /*
415  * Compare client certificate arrays.
416  *
417  * Returns 1 on compare failure, 0 otherwise.
418  */
419 static int compare_client_certs(apr_array_header_t *srcs, 
420                                 apr_array_header_t *dests)
421 {
422     int i = 0;
423     struct apr_ldap_opt_tls_cert_t *src, *dest;
424
425     /* arrays both NULL? if so, then equal */
426     if (srcs == NULL && dests == NULL) {
427         return 0;
428     }
429
430     /* arrays different length or either NULL? If so, then not equal */
431     if (srcs == NULL || dests == NULL || srcs->nelts != dests->nelts) {
432         return 1;
433     }
434
435     /* run an actual comparison */
436     src = (struct apr_ldap_opt_tls_cert_t *)srcs->elts;
437     dest = (struct apr_ldap_opt_tls_cert_t *)dests->elts;
438     for (i = 0; i < srcs->nelts; i++) {
439         if (strcmp(src[i].path, dest[i].path) ||
440             strcmp(src[i].password, dest[i].password) ||
441             src[i].type != dest[i].type) {
442             return 1;
443         }
444     }
445
446     /* if we got here, the cert arrays were identical */
447     return 0;
448
449 }
450
451
452 /*
453  * Find an existing ldap connection struct that matches the
454  * provided ldap connection parameters.
455  *
456  * If not found in the cache, a new ldc structure will be allocated 
457  * from st->pool and returned to the caller.  If found in the cache, 
458  * a pointer to the existing ldc structure will be returned.
459  */
460 static util_ldap_connection_t * 
461             uldap_connection_find(request_rec *r,
462                                   const char *host, int port,
463                                   const char *binddn, const char *bindpw,
464                                   deref_options deref, int secure) 
465 {
466     struct util_ldap_connection_t *l, *p; /* To traverse the linked list */
467     int secureflag = secure;
468
469     util_ldap_state_t *st = 
470         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
471         &ldap_module);
472
473
474 #if APR_HAS_THREADS
475     /* mutex lock this function */
476     if (!st->mutex) {
477         apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT, 
478                                 st->pool);
479     }
480     apr_thread_mutex_lock(st->mutex);
481 #endif
482
483     if (secure < APR_LDAP_NONE) {
484         secureflag = st->secure;
485     }
486
487     /* Search for an exact connection match in the list that is not
488      * being used.
489      */
490     for (l=st->connections,p=NULL; l; l=l->next) {
491 #if APR_HAS_THREADS
492         if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
493 #endif
494         if (   (l->port == port) && (strcmp(l->host, host) == 0) 
495             && ((!l->binddn && !binddn) || (l->binddn && binddn 
496                                              && !strcmp(l->binddn, binddn))) 
497             && ((!l->bindpw && !bindpw) || (l->bindpw && bindpw 
498                                              && !strcmp(l->bindpw, bindpw))) 
499             && (l->deref == deref) && (l->secure == secureflag) 
500             && !compare_client_certs(st->client_certs, l->client_certs)) 
501         {
502             break;
503         }
504 #if APR_HAS_THREADS
505             /* If this connection didn't match the criteria, then we
506              * need to unlock the mutex so it is available to be reused.
507              */
508             apr_thread_mutex_unlock(l->lock);
509         }
510 #endif
511         p = l;
512     }
513
514     /* If nothing found, search again, but we don't care about the
515      * binddn and bindpw this time.
516      */
517     if (!l) {
518         for (l=st->connections,p=NULL; l; l=l->next) {
519 #if APR_HAS_THREADS
520             if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
521
522 #endif
523             if ((l->port == port) && (strcmp(l->host, host) == 0) && 
524                 (l->deref == deref) && (l->secure == secureflag) &&
525                 !compare_client_certs(st->client_certs, l->client_certs)) 
526             {
527                 /* the bind credentials have changed */
528                 l->bound = 0;
529                 util_ldap_strdup((char**)&(l->binddn), binddn);
530                 util_ldap_strdup((char**)&(l->bindpw), bindpw);
531                 break;
532             }
533 #if APR_HAS_THREADS
534                 /* If this connection didn't match the criteria, then we
535                  * need to unlock the mutex so it is available to be reused.
536                  */
537                 apr_thread_mutex_unlock(l->lock);
538             }
539 #endif
540             p = l;
541         }
542     }
543
544 /* artificially disable cache */
545 /* l = NULL; */
546
547     /* If no connection what found after the second search, we
548      * must create one.
549      */
550     if (!l) {
551
552         /* 
553          * Add the new connection entry to the linked list. Note that we
554          * don't actually establish an LDAP connection yet; that happens
555          * the first time authentication is requested.
556          */
557         /* create the details to the pool in st */
558         l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
559 #if APR_HAS_THREADS
560         apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
561         apr_thread_mutex_lock(l->lock);
562 #endif
563         l->pool = st->pool;
564         l->bound = 0;
565         l->host = apr_pstrdup(st->pool, host);
566         l->port = port;
567         l->deref = deref;
568         util_ldap_strdup((char**)&(l->binddn), binddn);
569         util_ldap_strdup((char**)&(l->bindpw), bindpw);
570
571         /* The security mode after parsing the URL will always be either
572          * APR_LDAP_NONE (ldap://) or APR_LDAP_SSL (ldaps://).
573          * If the security setting is NONE, override it to the security
574          * setting optionally supplied by the admin using LDAPTrustedMode
575          */
576         l->secure = secureflag;
577
578         /* save away a copy of the client cert list that is presently valid */
579         l->client_certs = apr_array_copy_hdr(l->pool, st->client_certs);
580
581         /* add the cleanup to the pool */
582         apr_pool_cleanup_register(l->pool, l,
583                                   uldap_connection_cleanup,
584                                   apr_pool_cleanup_null);
585
586         if (p) {
587             p->next = l;
588         }
589         else {
590             st->connections = l;
591         }
592     }
593
594 #if APR_HAS_THREADS
595     apr_thread_mutex_unlock(st->mutex);
596 #endif
597     return l;
598 }
599
600 /* ------------------------------------------------------------------ */
601
602 /*
603  * Compares two DNs to see if they're equal. The only way to do this correctly
604  * is to search for the dn and then do ldap_get_dn() on the result. This should
605  * match the initial dn, since it would have been also retrieved with 
606  * ldap_get_dn(). This is expensive, so if the configuration value 
607  * compare_dn_on_server is false, just does an ordinary strcmp.
608  *
609  * The lock for the ldap cache should already be acquired.
610  */
611 static int uldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc, 
612                                  const char *url, const char *dn, 
613                                  const char *reqdn, int compare_dn_on_server)
614 {
615     int result = 0;
616     util_url_node_t *curl; 
617     util_url_node_t curnode;
618     util_dn_compare_node_t *node;
619     util_dn_compare_node_t newnode;
620     int failures = 0;
621     LDAPMessage *res, *entry;
622     char *searchdn;
623
624     util_ldap_state_t *st = (util_ldap_state_t *)
625                             ap_get_module_config(r->server->module_config, 
626                                                  &ldap_module);
627
628     /* get cache entry (or create one) */
629     LDAP_CACHE_LOCK();
630
631     curnode.url = url;
632     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
633     if (curl == NULL) {
634         curl = util_ald_create_caches(st, url);
635     }
636     LDAP_CACHE_UNLOCK();
637
638     /* a simple compare? */
639     if (!compare_dn_on_server) {
640         /* unlock this read lock */
641         if (strcmp(dn, reqdn)) {
642             ldc->reason = "DN Comparison FALSE (direct strcmp())";
643             return LDAP_COMPARE_FALSE;
644         }
645         else {
646             ldc->reason = "DN Comparison TRUE (direct strcmp())";
647             return LDAP_COMPARE_TRUE;
648         }
649     }
650
651     if (curl) {
652         /* no - it's a server side compare */
653         LDAP_CACHE_LOCK();
654     
655         /* is it in the compare cache? */
656         newnode.reqdn = (char *)reqdn;
657         node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
658         if (node != NULL) {
659             /* If it's in the cache, it's good */
660             /* unlock this read lock */
661             LDAP_CACHE_UNLOCK();
662             ldc->reason = "DN Comparison TRUE (cached)";
663             return LDAP_COMPARE_TRUE;
664         }
665     
666         /* unlock this read lock */
667         LDAP_CACHE_UNLOCK();
668     }
669
670 start_over:
671     if (failures++ > 10) {
672         /* too many failures */
673         return result;
674     }
675
676     /* make a server connection */
677     if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
678         /* connect to server failed */
679         return result;
680     }
681
682     /* search for reqdn */
683     if ((result = ldap_search_ext_s(ldc->ldap, (char *)reqdn, LDAP_SCOPE_BASE,
684                                     "(objectclass=*)", NULL, 1, 
685                                     NULL, NULL, NULL, -1, &res)) 
686             == LDAP_SERVER_DOWN) 
687     {
688         ldc->reason = "DN Comparison ldap_search_ext_s() "
689                       "failed with server down";
690         uldap_connection_unbind(ldc);
691         goto start_over;
692     }
693     if (result != LDAP_SUCCESS) {
694         /* search for reqdn failed - no match */
695         ldc->reason = "DN Comparison ldap_search_ext_s() failed";
696         return result;
697     }
698
699     entry = ldap_first_entry(ldc->ldap, res);
700     searchdn = ldap_get_dn(ldc->ldap, entry);
701
702     ldap_msgfree(res);
703     if (strcmp(dn, searchdn) != 0) {
704         /* compare unsuccessful */
705         ldc->reason = "DN Comparison FALSE (checked on server)";
706         result = LDAP_COMPARE_FALSE;
707     }
708     else {
709         if (curl) {
710             /* compare successful - add to the compare cache */
711             LDAP_CACHE_LOCK();
712             newnode.reqdn = (char *)reqdn;
713             newnode.dn = (char *)dn;
714             
715             node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
716             if (   (node == NULL)
717                 || (strcmp(reqdn, node->reqdn) != 0) 
718                 || (strcmp(dn, node->dn) != 0)) 
719             {
720                 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
721             }
722             LDAP_CACHE_UNLOCK();
723         }
724         ldc->reason = "DN Comparison TRUE (checked on server)";
725         result = LDAP_COMPARE_TRUE;
726     }
727     ldap_memfree(searchdn);
728     return result;
729
730 }
731
732 /*
733  * Does an generic ldap_compare operation. It accepts a cache that it will use
734  * to lookup the compare in the cache. We cache two kinds of compares 
735  * (require group compares) and (require user compares). Each compare has a different
736  * cache node: require group includes the DN; require user does not because the
737  * require user cache is owned by the 
738  *
739  */
740 static int uldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
741                                const char *url, const char *dn,
742                                const char *attrib, const char *value)
743 {
744     int result = 0;
745     util_url_node_t *curl; 
746     util_url_node_t curnode;
747     util_compare_node_t *compare_nodep;
748     util_compare_node_t the_compare_node;
749     apr_time_t curtime = 0; /* silence gcc -Wall */
750     int failures = 0;
751
752     util_ldap_state_t *st = (util_ldap_state_t *)
753                             ap_get_module_config(r->server->module_config,
754                                                  &ldap_module);
755
756     /* get cache entry (or create one) */
757     LDAP_CACHE_LOCK();
758     curnode.url = url;
759     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
760     if (curl == NULL) {
761         curl = util_ald_create_caches(st, url);
762     }
763     LDAP_CACHE_UNLOCK();
764
765     if (curl) {
766         /* make a comparison to the cache */
767         LDAP_CACHE_LOCK();
768         curtime = apr_time_now();
769     
770         the_compare_node.dn = (char *)dn;
771         the_compare_node.attrib = (char *)attrib;
772         the_compare_node.value = (char *)value;
773         the_compare_node.result = 0;
774     
775         compare_nodep = util_ald_cache_fetch(curl->compare_cache, 
776                                              &the_compare_node);
777     
778         if (compare_nodep != NULL) {
779             /* found it... */
780             if (curtime - compare_nodep->lastcompare > st->compare_cache_ttl) {
781                 /* ...but it is too old */
782                 util_ald_cache_remove(curl->compare_cache, compare_nodep);
783             }
784             else {
785                 /* ...and it is good */
786                 /* unlock this read lock */
787                 LDAP_CACHE_UNLOCK();
788                 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
789                     ldc->reason = "Comparison true (cached)";
790                     return compare_nodep->result;
791                 }
792                 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
793                     ldc->reason = "Comparison false (cached)";
794                     return compare_nodep->result;
795                 }
796                 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
797                     ldc->reason = "Comparison no such attribute (cached)";
798                     return compare_nodep->result;
799                 }
800                 else {
801                     ldc->reason = "Comparison undefined (cached)";
802                     return compare_nodep->result;
803                 }
804             }
805         }
806         /* unlock this read lock */
807         LDAP_CACHE_UNLOCK();
808     }
809
810 start_over:
811     if (failures++ > 10) {
812         /* too many failures */
813         return result;
814     }
815     if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
816         /* connect failed */
817         return result;
818     }
819
820     if ((result = ldap_compare_s(ldc->ldap,
821                                  (char *)dn,
822                                  (char *)attrib,
823                                  (char *)value))
824                                                == LDAP_SERVER_DOWN) { 
825         /* connection failed - try again */
826         ldc->reason = "ldap_compare_s() failed with server down";
827         uldap_connection_unbind(ldc);
828         goto start_over;
829     }
830
831     ldc->reason = "Comparison complete";
832     if ((LDAP_COMPARE_TRUE == result) || 
833         (LDAP_COMPARE_FALSE == result) ||
834         (LDAP_NO_SUCH_ATTRIBUTE == result)) {
835         if (curl) {
836             /* compare completed; caching result */
837             LDAP_CACHE_LOCK();
838             the_compare_node.lastcompare = curtime;
839             the_compare_node.result = result;
840
841             /* If the node doesn't exist then insert it, otherwise just update
842              * it with the last results 
843              */
844             compare_nodep = util_ald_cache_fetch(curl->compare_cache, 
845                                                  &the_compare_node);
846             if (   (compare_nodep == NULL) 
847                 || (strcmp(the_compare_node.dn, compare_nodep->dn) != 0) 
848                 || (strcmp(the_compare_node.attrib,compare_nodep->attrib) != 0) 
849                 || (strcmp(the_compare_node.value, compare_nodep->value) != 0))
850             {
851                 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
852             }
853             else {
854                 compare_nodep->lastcompare = curtime;
855                 compare_nodep->result = result;
856             }
857             LDAP_CACHE_UNLOCK();
858         }
859         if (LDAP_COMPARE_TRUE == result) {
860             ldc->reason = "Comparison true (adding to cache)";
861             return LDAP_COMPARE_TRUE;
862         }
863         else if (LDAP_COMPARE_FALSE == result) {
864             ldc->reason = "Comparison false (adding to cache)";
865             return LDAP_COMPARE_FALSE;
866         }
867         else {
868             ldc->reason = "Comparison no such attribute (adding to cache)";
869             return LDAP_NO_SUCH_ATTRIBUTE;
870         }
871     }
872     return result;
873 }
874
875 static int uldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
876                                    const char *url, const char *basedn, 
877                                    int scope, char **attrs, const char *filter,
878                                    const char *bindpw, const char **binddn, 
879                                    const char ***retvals)
880 {
881     const char **vals = NULL;
882     int numvals = 0;
883     int result = 0;
884     LDAPMessage *res, *entry;
885     char *dn;
886     int count;
887     int failures = 0;
888     util_url_node_t *curl;              /* Cached URL node */
889     util_url_node_t curnode;
890     util_search_node_t *search_nodep;   /* Cached search node */
891     util_search_node_t the_search_node;
892     apr_time_t curtime;
893
894     util_ldap_state_t *st = 
895         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
896         &ldap_module);
897
898     /* Get the cache node for this url */
899     LDAP_CACHE_LOCK();
900     curnode.url = url;
901     curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, 
902                                                    &curnode);
903     if (curl == NULL) {
904         curl = util_ald_create_caches(st, url);
905     }
906     LDAP_CACHE_UNLOCK();
907
908     if (curl) {
909         LDAP_CACHE_LOCK();
910         the_search_node.username = filter;
911         search_nodep = util_ald_cache_fetch(curl->search_cache, 
912                                             &the_search_node);
913         if (search_nodep != NULL) {
914     
915             /* found entry in search cache... */
916             curtime = apr_time_now();
917     
918             /*
919              * Remove this item from the cache if its expired. If the sent 
920              * password doesn't match the storepassword, the entry will 
921              * be removed and readded later if the credentials pass 
922              * authentication.
923              */
924             if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
925                 /* ...but entry is too old */
926                 util_ald_cache_remove(curl->search_cache, search_nodep);
927             }
928             else if (   (search_nodep->bindpw) 
929                      && (search_nodep->bindpw[0] != '\0') 
930                      && (strcmp(search_nodep->bindpw, bindpw) == 0)) 
931             {
932                 /* ...and entry is valid */
933                 *binddn = search_nodep->dn;
934                 *retvals = search_nodep->vals;
935                 LDAP_CACHE_UNLOCK();
936                 ldc->reason = "Authentication successful (cached)";
937                 return LDAP_SUCCESS;
938             }
939         }
940         /* unlock this read lock */
941         LDAP_CACHE_UNLOCK();
942     }
943
944     /*  
945      * At this point, there is no valid cached search, so lets do the search.
946      */
947
948     /*
949      * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
950      */
951 start_over:
952     if (failures++ > 10) {
953         return result;
954     }
955     if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
956         return result;
957     }
958
959     /* try do the search */
960     if ((result = ldap_search_ext_s(ldc->ldap,
961                                     (char *)basedn, scope, 
962                                     (char *)filter, attrs, 0, 
963                                     NULL, NULL, NULL, -1, &res)) 
964             == LDAP_SERVER_DOWN) 
965     {
966         ldc->reason = "ldap_search_ext_s() for user failed with server down";
967         uldap_connection_unbind(ldc);
968         goto start_over;
969     }
970
971     /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
972     if (result != LDAP_SUCCESS) {
973         ldc->reason = "ldap_search_ext_s() for user failed";
974         return result;
975     }
976
977     /* 
978      * We should have found exactly one entry; to find a different
979      * number is an error.
980      */
981     count = ldap_count_entries(ldc->ldap, res);
982     if (count != 1) 
983     {
984         if (count == 0 )
985             ldc->reason = "User not found";
986         else
987             ldc->reason = "User is not unique (search found two "
988                           "or more matches)";
989         ldap_msgfree(res);
990         return LDAP_NO_SUCH_OBJECT;
991     }
992
993     entry = ldap_first_entry(ldc->ldap, res);
994
995     /* Grab the dn, copy it into the pool, and free it again */
996     dn = ldap_get_dn(ldc->ldap, entry);
997     *binddn = apr_pstrdup(r->pool, dn);
998     ldap_memfree(dn);
999
1000     /* 
1001      * A bind to the server with an empty password always succeeds, so
1002      * we check to ensure that the password is not empty. This implies
1003      * that users who actually do have empty passwords will never be
1004      * able to authenticate with this module. I don't see this as a big
1005      * problem.
1006      */
1007     if (!bindpw || strlen(bindpw) <= 0) {
1008         ldap_msgfree(res);
1009         ldc->reason = "Empty password not allowed";
1010         return LDAP_INVALID_CREDENTIALS;
1011     }
1012
1013     /* 
1014      * Attempt to bind with the retrieved dn and the password. If the bind
1015      * fails, it means that the password is wrong (the dn obviously
1016      * exists, since we just retrieved it)
1017      */
1018     if ((result = ldap_simple_bind_s(ldc->ldap,
1019                                      (char *)*binddn,
1020                                      (char *)bindpw)) == LDAP_SERVER_DOWN) {
1021         ldc->reason = "ldap_simple_bind_s() to check user credentials "
1022                       "failed with server down";
1023         ldap_msgfree(res);
1024         uldap_connection_unbind(ldc);
1025         goto start_over;
1026     }
1027
1028     /* failure? if so - return */
1029     if (result != LDAP_SUCCESS) {
1030         ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
1031         ldap_msgfree(res);
1032         uldap_connection_unbind(ldc);
1033         return result;
1034     }
1035     else {
1036         /*
1037          * We have just bound the connection to a different user and password
1038          * combination, which might be reused unintentionally next time this
1039          * connection is used from the connection pool. To ensure no confusion,
1040          * we mark the connection as unbound.
1041          */
1042         ldc->bound = 0;
1043     }
1044
1045     /*
1046      * Get values for the provided attributes.
1047      */
1048     if (attrs) {
1049         int k = 0;
1050         int i = 0;
1051         while (attrs[k++]);
1052         vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
1053         numvals = k;
1054         while (attrs[i]) {
1055             char **values;
1056             int j = 0;
1057             char *str = NULL;
1058             /* get values */
1059             values = ldap_get_values(ldc->ldap, entry, attrs[i]);
1060             while (values && values[j]) {
1061                 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) 
1062                           : apr_pstrdup(r->pool, values[j]);
1063                 j++;
1064             }
1065             ldap_value_free(values);
1066             vals[i] = str;
1067             i++;
1068         }
1069         *retvals = vals;
1070     }
1071
1072     /*          
1073      * Add the new username to the search cache.
1074      */
1075     if (curl) {
1076         LDAP_CACHE_LOCK();
1077         the_search_node.username = filter;
1078         the_search_node.dn = *binddn;
1079         the_search_node.bindpw = bindpw;
1080         the_search_node.lastbind = apr_time_now();
1081         the_search_node.vals = vals;
1082         the_search_node.numvals = numvals;
1083
1084         /* Search again to make sure that another thread didn't ready insert 
1085          * this node into the cache before we got here. If it does exist then 
1086          * update the lastbind 
1087          */
1088         search_nodep = util_ald_cache_fetch(curl->search_cache, 
1089                                             &the_search_node);
1090         if ((search_nodep == NULL) || 
1091             (strcmp(*binddn, search_nodep->dn) != 0)) {
1092
1093             /* Nothing in cache, insert new entry */
1094             util_ald_cache_insert(curl->search_cache, &the_search_node);
1095         }
1096         else if ((!search_nodep->bindpw) ||
1097             (strcmp(bindpw, search_nodep->bindpw) != 0)) {
1098
1099             /* Entry in cache is invalid, remove it and insert new one */
1100             util_ald_cache_remove(curl->search_cache, search_nodep);
1101             util_ald_cache_insert(curl->search_cache, &the_search_node);
1102         }
1103         else {
1104             /* Cache entry is valid, update lastbind */
1105             search_nodep->lastbind = the_search_node.lastbind;
1106         }
1107         LDAP_CACHE_UNLOCK();
1108     }
1109     ldap_msgfree(res);
1110
1111     ldc->reason = "Authentication successful";
1112     return LDAP_SUCCESS;
1113 }
1114
1115 /*
1116  * This function will return the DN of the entry matching userid.
1117  * It is used to get the DN in case some other module than mod_auth_ldap
1118  * has authenticated the user.
1119  * The function is basically a copy of uldap_cache_checkuserid
1120  * with password checking removed.
1121  */
1122 static int uldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
1123                                  const char *url, const char *basedn, 
1124                                  int scope, char **attrs, const char *filter, 
1125                                  const char **binddn, const char ***retvals)
1126 {
1127     const char **vals = NULL;
1128     int numvals = 0;
1129     int result = 0;
1130     LDAPMessage *res, *entry;
1131     char *dn;
1132     int count;
1133     int failures = 0;
1134     util_url_node_t *curl;              /* Cached URL node */
1135     util_url_node_t curnode;
1136     util_search_node_t *search_nodep;   /* Cached search node */
1137     util_search_node_t the_search_node;
1138     apr_time_t curtime;
1139
1140     util_ldap_state_t *st = 
1141         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
1142         &ldap_module);
1143
1144     /* Get the cache node for this url */
1145     LDAP_CACHE_LOCK();
1146     curnode.url = url;
1147     curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, 
1148                                                    &curnode);
1149     if (curl == NULL) {
1150         curl = util_ald_create_caches(st, url);
1151     }
1152     LDAP_CACHE_UNLOCK();
1153
1154     if (curl) {
1155         LDAP_CACHE_LOCK();
1156         the_search_node.username = filter;
1157         search_nodep = util_ald_cache_fetch(curl->search_cache, 
1158                                             &the_search_node);
1159         if (search_nodep != NULL) {
1160     
1161             /* found entry in search cache... */
1162             curtime = apr_time_now();
1163     
1164             /*
1165              * Remove this item from the cache if its expired.
1166              */
1167             if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
1168                 /* ...but entry is too old */
1169                 util_ald_cache_remove(curl->search_cache, search_nodep);
1170             }
1171             else {
1172                 /* ...and entry is valid */
1173                 *binddn = search_nodep->dn;
1174                 *retvals = search_nodep->vals;
1175                 LDAP_CACHE_UNLOCK();
1176                 ldc->reason = "Search successful (cached)";
1177                 return LDAP_SUCCESS;
1178             }
1179         }
1180         /* unlock this read lock */
1181         LDAP_CACHE_UNLOCK();
1182     }
1183
1184     /*  
1185      * At this point, there is no valid cached search, so lets do the search.
1186      */
1187
1188     /*
1189      * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
1190      */
1191 start_over:
1192     if (failures++ > 10) {
1193         return result;
1194     }
1195     if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
1196         return result;
1197     }
1198
1199     /* try do the search */
1200     if ((result = ldap_search_ext_s(ldc->ldap,
1201                                     (char *)basedn, scope,
1202                                     (char *)filter, attrs, 0, 
1203                                     NULL, NULL, NULL, -1, &res)) 
1204             == LDAP_SERVER_DOWN) 
1205     {
1206         ldc->reason = "ldap_search_ext_s() for user failed with server down";
1207         uldap_connection_unbind(ldc);
1208         goto start_over;
1209     }
1210
1211     /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
1212     if (result != LDAP_SUCCESS) {
1213         ldc->reason = "ldap_search_ext_s() for user failed";
1214         return result;
1215     }
1216
1217     /* 
1218      * We should have found exactly one entry; to find a different
1219      * number is an error.
1220      */
1221     count = ldap_count_entries(ldc->ldap, res);
1222     if (count != 1) 
1223     {
1224         if (count == 0 )
1225             ldc->reason = "User not found";
1226         else
1227             ldc->reason = "User is not unique (search found two "
1228                           "or more matches)";
1229         ldap_msgfree(res);
1230         return LDAP_NO_SUCH_OBJECT;
1231     }
1232
1233     entry = ldap_first_entry(ldc->ldap, res);
1234
1235     /* Grab the dn, copy it into the pool, and free it again */
1236     dn = ldap_get_dn(ldc->ldap, entry);
1237     *binddn = apr_pstrdup(st->pool, dn);
1238     ldap_memfree(dn);
1239
1240     /*
1241      * Get values for the provided attributes.
1242      */
1243     if (attrs) {
1244         int k = 0;
1245         int i = 0;
1246         while (attrs[k++]);
1247         vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
1248         numvals = k;
1249         while (attrs[i]) {
1250             char **values;
1251             int j = 0;
1252             char *str = NULL;
1253             /* get values */
1254             values = ldap_get_values(ldc->ldap, entry, attrs[i]);
1255             while (values && values[j]) {
1256                 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) 
1257                           : apr_pstrdup(r->pool, values[j]);
1258                 j++;
1259             }
1260             ldap_value_free(values);
1261             vals[i] = str;
1262             i++;
1263         }
1264         *retvals = vals;
1265     }
1266
1267     /*          
1268      * Add the new username to the search cache.
1269      */
1270     if (curl) {
1271         LDAP_CACHE_LOCK();
1272         the_search_node.username = filter;
1273         the_search_node.dn = *binddn;
1274         the_search_node.bindpw = NULL;
1275         the_search_node.lastbind = apr_time_now();
1276         the_search_node.vals = vals;
1277         the_search_node.numvals = numvals;
1278
1279         /* Search again to make sure that another thread didn't ready insert 
1280          * this node into the cache before we got here. If it does exist then 
1281          * update the lastbind 
1282          */
1283         search_nodep = util_ald_cache_fetch(curl->search_cache,
1284                                             &the_search_node);
1285         if ((search_nodep == NULL) || 
1286             (strcmp(*binddn, search_nodep->dn) != 0)) {
1287
1288             /* Nothing in cache, insert new entry */
1289             util_ald_cache_insert(curl->search_cache, &the_search_node);
1290         }
1291         /*
1292          * Don't update lastbind on entries with bindpw because
1293          * we haven't verified that password. It's OK to update
1294          * the entry if there is no password in it.
1295          */
1296         else if (!search_nodep->bindpw) {
1297             /* Cache entry is valid, update lastbind */
1298             search_nodep->lastbind = the_search_node.lastbind;
1299         }
1300         LDAP_CACHE_UNLOCK();
1301     }
1302
1303     ldap_msgfree(res);
1304
1305     ldc->reason = "Search successful";
1306     return LDAP_SUCCESS;
1307 }
1308
1309 /*
1310  * Reports if ssl support is enabled 
1311  *
1312  * 1 = enabled, 0 = not enabled
1313  */
1314 static int uldap_ssl_supported(request_rec *r)
1315 {
1316    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1317                                 r->server->module_config, &ldap_module);
1318
1319    return(st->ssl_supported);
1320 }
1321
1322
1323 /* ---------------------------------------- */
1324 /* config directives */
1325
1326
1327 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, 
1328                                              const char *bytes)
1329 {
1330     util_ldap_state_t *st = 
1331         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1332                                                   &ldap_module);
1333
1334     st->cache_bytes = atol(bytes);
1335
1336     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1337                  "[%" APR_PID_T_FMT "] ldap cache: Setting shared memory "
1338                  " cache size to %" APR_SIZE_T_FMT " bytes.", 
1339                  getpid(), st->cache_bytes);
1340
1341     return NULL;
1342 }
1343
1344 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, 
1345                                             const char *file)
1346 {
1347     util_ldap_state_t *st = 
1348         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1349                                                   &ldap_module);
1350
1351     if (file) {
1352         st->cache_file = ap_server_root_relative(st->pool, file);
1353     }
1354     else {
1355         st->cache_file = NULL;
1356     }
1357
1358     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1359                  "LDAP cache: Setting shared memory cache file to %s bytes.", 
1360                  st->cache_file);
1361
1362     return NULL;
1363 }
1364
1365 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, 
1366                                            const char *ttl)
1367 {
1368     util_ldap_state_t *st = 
1369         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1370                                                   &ldap_module);
1371
1372     st->search_cache_ttl = atol(ttl) * 1000000;
1373
1374     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1375                  "[%d] ldap cache: Setting cache TTL to %ld microseconds.",
1376                  getpid(), st->search_cache_ttl);
1377
1378     return NULL;
1379 }
1380
1381 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy, 
1382                                                const char *size)
1383 {
1384     util_ldap_state_t *st = 
1385         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1386                                                   &ldap_module);
1387
1388
1389     st->search_cache_size = atol(size);
1390     if (st->search_cache_size < 0) {
1391         st->search_cache_size = 0;
1392     }
1393
1394     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1395                  "[%d] ldap cache: Setting search cache size to %ld entries.",
1396                  getpid(), st->search_cache_size);
1397
1398     return NULL;
1399 }
1400
1401 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy, 
1402                                              const char *ttl)
1403 {
1404     util_ldap_state_t *st = 
1405         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1406                                                   &ldap_module);
1407
1408     st->compare_cache_ttl = atol(ttl) * 1000000;
1409
1410     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1411                  "[%d] ldap cache: Setting operation cache TTL to %ld microseconds.", 
1412                  getpid(), st->compare_cache_ttl);
1413
1414     return NULL;
1415 }
1416
1417 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy, 
1418                                                  const char *size)
1419 {
1420     util_ldap_state_t *st = 
1421         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1422                                                   &ldap_module);
1423
1424     st->compare_cache_size = atol(size);
1425     if (st->compare_cache_size < 0) {
1426         st->compare_cache_size = 0;
1427     }
1428
1429     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1430                  "[%d] ldap cache: Setting operation cache size to %ld "
1431                  "entries.", getpid(), st->compare_cache_size);
1432
1433     return NULL;
1434 }
1435
1436
1437 /**
1438  * Parse the certificate type.
1439  *
1440  * The type can be one of the following:
1441  * CA_DER, CA_BASE64, CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64,
1442  * CERT_KEY3_DB, CERT_NICKNAME, KEY_DER, KEY_BASE64
1443  *
1444  * If no matches are found, APR_LDAP_CA_TYPE_UNKNOWN is returned.
1445  */
1446 static const int util_ldap_parse_cert_type(const char *type) {
1447
1448     /* Authority file in binary DER format */
1449     if (0 == strcasecmp("CA_DER", type)) {
1450         return APR_LDAP_CA_TYPE_DER;
1451     }
1452
1453     /* Authority file in Base64 format */
1454     else if (0 == strcasecmp("CA_BASE64", type)) {
1455         return APR_LDAP_CA_TYPE_BASE64;
1456     }
1457
1458     /* Netscape certificate database file/directory */
1459     else if (0 == strcasecmp("CA_CERT7_DB", type)) {
1460         return APR_LDAP_CA_TYPE_CERT7_DB;
1461     }
1462
1463     /* Netscape secmod file/directory */
1464     else if (0 == strcasecmp("CA_SECMOD", type)) {
1465         return APR_LDAP_CA_TYPE_SECMOD;
1466     }
1467
1468     /* Client cert file in DER format */
1469     else if (0 == strcasecmp("CERT_DER", type)) {
1470         return APR_LDAP_CERT_TYPE_DER;
1471     }
1472
1473     /* Client cert file in Base64 format */
1474     else if (0 == strcasecmp("CERT_BASE64", type)) {
1475         return APR_LDAP_CERT_TYPE_BASE64;
1476     }
1477
1478     /* Client cert file in PKCS#12 format */
1479     else if (0 == strcasecmp("CERT_PFX", type)) {
1480         return APR_LDAP_CERT_TYPE_PFX;
1481     }
1482
1483     /* Netscape client cert database file/directory */
1484     else if (0 == strcasecmp("CERT_KEY3_DB", type)) {
1485         return APR_LDAP_CERT_TYPE_KEY3_DB;
1486     }
1487
1488     /* Netscape client cert nickname */
1489     else if (0 == strcasecmp("CERT_NICKNAME", type)) {
1490         return APR_LDAP_CERT_TYPE_NICKNAME;
1491     }
1492
1493     /* Client cert key file in DER format */
1494     else if (0 == strcasecmp("KEY_DER", type)) {
1495         return APR_LDAP_KEY_TYPE_DER;
1496     }
1497
1498     /* Client cert key file in Base64 format */
1499     else if (0 == strcasecmp("KEY_BASE64", type)) {
1500         return APR_LDAP_KEY_TYPE_BASE64;
1501     }
1502
1503     /* Client cert key file in PKCS#12 format */
1504     else if (0 == strcasecmp("KEY_PFX", type)) {
1505         return APR_LDAP_KEY_TYPE_PFX;
1506     }
1507
1508     else {
1509         return APR_LDAP_CA_TYPE_UNKNOWN;
1510     }
1511
1512 }
1513
1514
1515 /**
1516  * Set LDAPTrustedGlobalCert.
1517  *
1518  * This directive takes either two or three arguments:
1519  * - certificate type
1520  * - certificate file / directory / nickname
1521  * - certificate password (optional)
1522  *
1523  * This directive may only be used globally.
1524  */
1525 static const char *util_ldap_set_trusted_global_cert(cmd_parms *cmd, 
1526                                                      void *dummy, 
1527                                                      const char *type, 
1528                                                      const char *file, 
1529                                                      const char *password)
1530 {
1531     util_ldap_state_t *st =
1532         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1533                                                   &ldap_module);
1534     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1535     apr_finfo_t finfo;
1536     apr_status_t rv;
1537     int cert_type = 0;
1538     apr_ldap_opt_tls_cert_t *cert;
1539
1540     if (err != NULL) {
1541         return err;
1542     }
1543
1544     /* handle the certificate type */
1545     if (type) {
1546         cert_type = util_ldap_parse_cert_type(type);
1547         if (APR_LDAP_CA_TYPE_UNKNOWN == cert_type) {
1548            return apr_psprintf(cmd->pool, "The certificate type %s is "
1549                                           "not recognised. It should be one "
1550                                           "of CA_DER, CA_BASE64, CA_CERT7_DB, "
1551                                           "CA_SECMOD, CERT_DER, CERT_BASE64, "
1552                                           "CERT_KEY3_DB, CERT_NICKNAME, "
1553                                           "KEY_DER, KEY_BASE64", type);
1554         }
1555     }
1556     else {
1557         return "Certificate type was not specified.";
1558     }
1559
1560     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1561                       "LDAP: SSL trusted global cert - %s (type %s)",
1562                        file, type);
1563
1564     /* add the certificate to the global array */
1565     cert = (apr_ldap_opt_tls_cert_t *)apr_array_push(st->global_certs);
1566     cert->type = cert_type;
1567     cert->path = file;
1568     cert->password = password;
1569
1570     /* if file is a file or path, fix the path */
1571     if (cert_type != APR_LDAP_CA_TYPE_UNKNOWN &&
1572         cert_type != APR_LDAP_CERT_TYPE_NICKNAME) {
1573
1574         cert->path = ap_server_root_relative(cmd->pool, file);
1575         if (cert->path &&
1576             ((rv = apr_stat (&finfo, cert->path, APR_FINFO_MIN, cmd->pool)) 
1577                 != APR_SUCCESS)) 
1578         {
1579             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cmd->server,
1580                          "LDAP: Could not open SSL trusted certificate "
1581                          "authority file - %s",
1582                          cert->path == NULL ? file : cert->path);
1583             return "Invalid global certificate file path";
1584         }
1585     }
1586
1587     return(NULL);
1588 }
1589
1590
1591 /**
1592  * Set LDAPTrustedClientCert.
1593  *
1594  * This directive takes either two or three arguments:
1595  * - certificate type
1596  * - certificate file / directory / nickname
1597  * - certificate password (optional)
1598  */
1599 static const char *util_ldap_set_trusted_client_cert(cmd_parms *cmd, 
1600                                                      void *config, 
1601                                                      const char *type, 
1602                                                      const char *file, 
1603                                                      const char *password)
1604 {
1605     util_ldap_state_t *st =
1606         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1607                                                   &ldap_module);
1608     apr_finfo_t finfo;
1609     apr_status_t rv;
1610     int cert_type = 0;
1611     apr_ldap_opt_tls_cert_t *cert;
1612
1613     /* handle the certificate type */
1614     if (type) {
1615         cert_type = util_ldap_parse_cert_type(type);
1616         if (APR_LDAP_CA_TYPE_UNKNOWN == cert_type) {
1617             return apr_psprintf(cmd->pool, "The certificate type \"%s\" is "
1618                                            "not recognised. It should be one "
1619                                            "of CERT_DER, CERT_BASE64, "
1620                                            "CERT_NICKNAME, CERT_PFX,"
1621                                            "KEY_DER, KEY_BASE64, KEY_PFX", 
1622                                            type);
1623         }
1624         else if (APR_LDAP_CA_TYPE_DER == cert_type ||
1625                  APR_LDAP_CA_TYPE_BASE64 == cert_type ||
1626                  APR_LDAP_CA_TYPE_CERT7_DB == cert_type ||
1627                  APR_LDAP_CA_TYPE_SECMOD == cert_type ||
1628                  APR_LDAP_CERT_TYPE_PFX == cert_type ||
1629                  APR_LDAP_CERT_TYPE_KEY3_DB == cert_type) {
1630             return apr_psprintf(cmd->pool, "The certificate type \"%s\" is "
1631                                            "only valid within a "
1632                                            "LDAPTrustedGlobalCert directive. "
1633                                            "Only CERT_DER, CERT_BASE64, "
1634                                            "CERT_NICKNAME, KEY_DER, and "
1635                                            "KEY_BASE64 may be used.", type);
1636         }
1637     }
1638     else {
1639         return "Certificate type was not specified.";
1640     }
1641
1642     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1643                       "LDAP: SSL trusted client cert - %s (type %s)",
1644                        file, type);
1645
1646     /* add the certificate to the global array */
1647     cert = (apr_ldap_opt_tls_cert_t *)apr_array_push(st->global_certs);
1648     cert->type = cert_type; 
1649     cert->path = file;
1650     cert->password = password;
1651
1652     /* if file is a file or path, fix the path */
1653     if (cert_type != APR_LDAP_CA_TYPE_UNKNOWN &&
1654         cert_type != APR_LDAP_CERT_TYPE_NICKNAME) {
1655
1656         cert->path = ap_server_root_relative(cmd->pool, file);
1657         if (cert->path &&
1658             ((rv = apr_stat (&finfo, cert->path, APR_FINFO_MIN, cmd->pool)) 
1659                 != APR_SUCCESS)) 
1660         {
1661             ap_log_error(APLOG_MARK, APLOG_ERR, rv, cmd->server,
1662                          "LDAP: Could not open SSL client certificate "
1663                          "file - %s",
1664                          cert->path == NULL ? file : cert->path);
1665             return "Invalid client certificate file path";
1666         }
1667
1668     }
1669
1670     return(NULL);
1671 }
1672
1673
1674 /**
1675  * Set LDAPTrustedMode.
1676  *                    
1677  * This directive sets what encryption mode to use on a connection:
1678  * - None (No encryption)
1679  * - SSL (SSL encryption)
1680  * - STARTTLS (TLS encryption)
1681  */ 
1682 static const char *util_ldap_set_trusted_mode(cmd_parms *cmd, void *dummy, 
1683                                               const char *mode)
1684 {
1685     util_ldap_state_t *st =
1686     (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1687                                               &ldap_module);
1688
1689     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1690                       "LDAP: SSL trusted mode - %s",
1691                        mode);
1692
1693     if (0 == strcasecmp("NONE", mode)) {
1694         st->secure = APR_LDAP_NONE;
1695     }
1696     else if (0 == strcasecmp("SSL", mode)) {
1697         st->secure = APR_LDAP_SSL;
1698     }
1699     else if (   (0 == strcasecmp("TLS", mode))
1700              || (0 == strcasecmp("STARTTLS", mode))) {
1701         st->secure = APR_LDAP_STARTTLS;
1702     }
1703     else {
1704         return "Invalid LDAPTrustedMode setting: must be one of NONE, "
1705                "SSL, or TLS/STARTTLS";
1706     }
1707
1708     st->secure_set = 1;
1709     return(NULL);
1710 }
1711
1712 static const char *util_ldap_set_verify_srv_cert(cmd_parms *cmd, 
1713                                                  void *dummy, 
1714                                                  int mode)
1715 {
1716     util_ldap_state_t *st =
1717     (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1718                                               &ldap_module);
1719
1720     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1721                       "LDAP: SSL verify server certificate - %s", 
1722                       mode?"TRUE":"FALSE");
1723
1724     st->verify_svr_cert = mode;
1725
1726     return(NULL);
1727 }
1728
1729
1730 static const char *util_ldap_set_connection_timeout(cmd_parms *cmd, 
1731                                                     void *dummy, 
1732                                                     const char *ttl)
1733 {
1734     util_ldap_state_t *st = 
1735         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1736                                                   &ldap_module);
1737     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1738
1739     if (err != NULL) {
1740         return err;
1741     }
1742
1743 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1744     st->connectionTimeout = atol(ttl);
1745
1746     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1747                  "[%d] ldap connection: Setting connection timeout to "
1748                  "%ld seconds.", getpid(), st->connectionTimeout);
1749 #else
1750     ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1751                  "LDAP: Connection timout option not supported by the "
1752                  "LDAP SDK in use." );
1753 #endif
1754
1755     return NULL;
1756 }
1757
1758
1759 static void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1760 {
1761     util_ldap_state_t *st = 
1762         (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
1763
1764     st->pool = p;
1765
1766     st->cache_bytes = 100000;
1767     st->search_cache_ttl = 600000000;
1768     st->search_cache_size = 1024;
1769     st->compare_cache_ttl = 600000000;
1770     st->compare_cache_size = 1024;
1771     st->connections = NULL;
1772     st->ssl_supported = 0;
1773     st->global_certs = apr_array_make(p, 10, sizeof(apr_ldap_opt_tls_cert_t));
1774     st->client_certs = apr_array_make(p, 10, sizeof(apr_ldap_opt_tls_cert_t));
1775     st->secure = APR_LDAP_NONE;
1776     st->secure_set = 0;
1777     st->connectionTimeout = 10;
1778     st->verify_svr_cert = 1;
1779
1780     return st;
1781 }
1782
1783 static void *util_ldap_merge_config(apr_pool_t *p, void *basev, 
1784                                     void *overridesv)
1785 {
1786     util_ldap_state_t *st = apr_pcalloc(p, sizeof(util_ldap_state_t));
1787     util_ldap_state_t *base = (util_ldap_state_t *) basev;
1788     util_ldap_state_t *overrides = (util_ldap_state_t *) overridesv;
1789
1790     st->pool = p;
1791
1792     st->cache_bytes = base->cache_bytes;
1793     st->search_cache_ttl = base->search_cache_ttl;
1794     st->search_cache_size = base->search_cache_size;
1795     st->compare_cache_ttl = base->compare_cache_ttl;
1796     st->compare_cache_size = base->compare_cache_size;
1797     st->connections = base->connections;
1798     st->ssl_supported = base->ssl_supported;
1799     st->global_certs = apr_array_append(p, base->global_certs, 
1800                                            overrides->global_certs);
1801     st->client_certs = apr_array_append(p, base->client_certs, 
1802                                            overrides->client_certs);
1803     st->secure = (overrides->secure_set == 0) ? base->secure 
1804                                               : overrides->secure;
1805
1806     return st;
1807 }
1808
1809 static apr_status_t util_ldap_cleanup_module(void *data)
1810 {
1811
1812     server_rec *s = data;
1813     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1814         s->module_config, &ldap_module);
1815     
1816     if (st->ssl_supported) {
1817         apr_ldap_ssl_deinit();
1818     }
1819
1820     return APR_SUCCESS;
1821
1822 }
1823
1824 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, 
1825                                  apr_pool_t *ptemp, server_rec *s)
1826 {
1827     apr_status_t result;
1828     char buf[MAX_STRING_LEN];
1829     server_rec *s_vhost;
1830     util_ldap_state_t *st_vhost;
1831
1832     util_ldap_state_t *st = (util_ldap_state_t *)
1833                             ap_get_module_config(s->module_config, 
1834                                                  &ldap_module);
1835
1836     void *data;
1837     const char *userdata_key = "util_ldap_init";
1838     apr_ldap_err_t *result_err = NULL;
1839     int rc;
1840
1841     /* util_ldap_post_config() will be called twice. Don't bother
1842      * going through all of the initialization on the first call
1843      * because it will just be thrown away.*/
1844     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1845     if (!data) {
1846         apr_pool_userdata_set((const void *)1, userdata_key,
1847                                apr_pool_cleanup_null, s->process->pool);
1848
1849 #if APR_HAS_SHARED_MEMORY
1850         /* If the cache file already exists then delete it.  Otherwise we are
1851          * going to run into problems creating the shared memory. */
1852         if (st->cache_file) {
1853             char *lck_file = apr_pstrcat(st->pool, st->cache_file, ".lck", 
1854                                          NULL);
1855             apr_file_remove(st->cache_file, ptemp);
1856             apr_file_remove(lck_file, ptemp);
1857         }
1858 #endif
1859         return OK;
1860     }
1861
1862 #if APR_HAS_SHARED_MEMORY
1863     /* initializing cache if shared memory size is not zero and we already 
1864      * don't have shm address 
1865      */
1866     if (!st->cache_shm && st->cache_bytes > 0) {
1867 #endif
1868         result = util_ldap_cache_init(p, st);
1869         if (result != APR_SUCCESS) {
1870             apr_strerror(result, buf, sizeof(buf));
1871             ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
1872                          "LDAP cache: error while creating a shared memory "
1873                          "segment: %s", buf);
1874         }
1875
1876
1877 #if APR_HAS_SHARED_MEMORY
1878         if (st->cache_file) {
1879             st->lock_file = apr_pstrcat(st->pool, st->cache_file, ".lck", 
1880                                         NULL);
1881         }
1882         else
1883 #endif
1884             st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1885
1886         result = apr_global_mutex_create(&st->util_ldap_cache_lock, 
1887                                          st->lock_file, APR_LOCK_DEFAULT, 
1888                                          st->pool);
1889         if (result != APR_SUCCESS) {
1890             return result;
1891         }
1892
1893 #ifdef AP_NEED_SET_MUTEX_PERMS
1894         result = unixd_set_global_mutex_perms(st->util_ldap_cache_lock);
1895         if (result != APR_SUCCESS) {
1896             ap_log_error(APLOG_MARK, APLOG_CRIT, result, s, 
1897                          "LDAP cache: failed to set mutex permissions");
1898             return result;
1899         }
1900 #endif
1901
1902         /* merge config in all vhost */
1903         s_vhost = s->next;
1904         while (s_vhost) {
1905             st_vhost = (util_ldap_state_t *)
1906                        ap_get_module_config(s_vhost->module_config, 
1907                                             &ldap_module);
1908
1909 #if APR_HAS_SHARED_MEMORY
1910             st_vhost->cache_shm = st->cache_shm;
1911             st_vhost->cache_rmm = st->cache_rmm;
1912             st_vhost->cache_file = st->cache_file;
1913             ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
1914                          "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp "
1915                          "for VHOST: %s", st->cache_shm, st->cache_rmm, 
1916                          s_vhost->server_hostname);
1917 #endif
1918             st_vhost->lock_file = st->lock_file;
1919             s_vhost = s_vhost->next;
1920         }
1921 #if APR_HAS_SHARED_MEMORY
1922     }
1923     else {
1924         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, 
1925                      "LDAP cache: LDAPSharedCacheSize is zero, disabling "
1926                      "shared memory cache");
1927     }
1928 #endif
1929     
1930     /* log the LDAP SDK used 
1931      */
1932     {
1933         apr_ldap_err_t *result = NULL;
1934         apr_ldap_info(p, &(result));
1935         if (result != NULL) {
1936             ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "%s", result->reason);
1937         }
1938     }
1939
1940     apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1941                               util_ldap_cleanup_module); 
1942
1943     /*
1944      * Initialize SSL support, and log the result for the benefit of the admin.
1945      *
1946      * If SSL is not supported it is not necessarily an error, as the
1947      * application may not want to use it.
1948      */
1949     rc = apr_ldap_ssl_init(p, 
1950                       NULL, 
1951                       0, 
1952                       &(result_err));
1953     if (APR_SUCCESS == rc) {
1954         rc = apr_ldap_set_option(p, NULL, APR_LDAP_OPT_TLS_CERT,
1955                                  (void *)st->global_certs, &(result_err));
1956     }
1957
1958     if (APR_SUCCESS == rc) {
1959         st->ssl_supported = 1;
1960         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1961                          "LDAP: SSL support available" );
1962     }
1963     else {
1964         st->ssl_supported = 0;
1965         if (NULL != result_err) {
1966             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, "%s", 
1967                          result_err->reason);
1968         }
1969         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1970                          "LDAP: SSL support unavailable" );
1971     }
1972
1973     return(OK);
1974 }
1975
1976 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1977 {
1978     apr_status_t sts;
1979     util_ldap_state_t *st = ap_get_module_config(s->module_config, 
1980                                                  &ldap_module);
1981
1982     if (!st->util_ldap_cache_lock) return;
1983
1984     sts = apr_global_mutex_child_init(&st->util_ldap_cache_lock, 
1985                                       st->lock_file, p);
1986     if (sts != APR_SUCCESS) {
1987         ap_log_error(APLOG_MARK, APLOG_CRIT, sts, s,
1988                      "Failed to initialise global mutex %s in child process %"
1989                      APR_PID_T_FMT ".",
1990                      st->lock_file, getpid());
1991         return;
1992     }
1993     else {
1994         ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, s, 
1995                      "Initialisation of global mutex %s in child process %"
1996                      APR_PID_T_FMT
1997                      " successful.",
1998                      st->lock_file, getpid());
1999     }
2000 }
2001
2002 command_rec util_ldap_cmds[] = {
2003     AP_INIT_TAKE1("LDAPSharedCacheSize", util_ldap_set_cache_bytes, 
2004                   NULL, RSRC_CONF,
2005                   "Set the size of the shared memory cache (in bytes). Use "
2006                   "0 to disable the shared memory cache. (default: 100000)"),
2007
2008     AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, 
2009                   NULL, RSRC_CONF,
2010                   "Set the file name for the shared memory cache."),
2011
2012     AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, 
2013                   NULL, RSRC_CONF,
2014                   "Set the maximum number of entries that are possible in the "
2015                   "LDAP search cache. Use 0 for no limit. "
2016                   "-1 disables the cache. (default: 1024)"),
2017
2018     AP_INIT_TAKE1("LDAPCacheTTL", util_ldap_set_cache_ttl, 
2019                   NULL, RSRC_CONF,
2020                   "Set the maximum time (in seconds) that an item can be "
2021                   "cached in the LDAP search cache. Use 0 for no limit. "
2022                   "(default 600)"),
2023
2024     AP_INIT_TAKE1("LDAPOpCacheEntries", util_ldap_set_opcache_entries, 
2025                   NULL, RSRC_CONF,
2026                   "Set the maximum number of entries that are possible "
2027                   "in the LDAP compare cache. Use 0 for no limit. "
2028                   "Use -1 to disable the cache. (default: 1024)"),
2029
2030     AP_INIT_TAKE1("LDAPOpCacheTTL", util_ldap_set_opcache_ttl, 
2031                   NULL, RSRC_CONF,
2032                   "Set the maximum time (in seconds) that an item is cached "
2033                   "in the LDAP operation cache. Use 0 for no limit. "
2034                   "(default: 600)"),
2035
2036     AP_INIT_TAKE23("LDAPTrustedGlobalCert", util_ldap_set_trusted_global_cert,
2037                    NULL, RSRC_CONF,
2038                    "Takes three args; the file and/or directory containing "
2039                    "the trusted CA certificates (and global client certs "
2040                    "for Netware) used to validate the LDAP server.  Second "
2041                    "arg is the cert type for the first arg, one of CA_DER, "
2042                    "CA_BASE64, CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64, "
2043                    "CERT_KEY3_DB, CERT_NICKNAME, KEY_DER, or KEY_BASE64. "
2044                    "Third arg is an optional passphrase if applicable."),
2045
2046     AP_INIT_TAKE23("LDAPTrustedClientCert", util_ldap_set_trusted_client_cert,
2047                    NULL, RSRC_CONF,
2048                    "Takes three args; the file and/or directory containing "
2049                    "the client certificate, or certificate ID used to "
2050                    "validate this LDAP client.  Second arg is the cert type "
2051                    "for the first arg, one of CA_DER, CA_BASE64, CA_CERT7_DB, "
2052                    "CA_SECMOD, CERT_DER, CERT_BASE64, CERT_KEY3_DB, "
2053                    "CERT_NICKNAME, KEY_DER, or KEY_BASE64. Third arg is an "
2054                    "optional passphrase if applicable."),
2055
2056     AP_INIT_TAKE1("LDAPTrustedMode", util_ldap_set_trusted_mode, 
2057                   NULL, RSRC_CONF,
2058                   "Specify the type of security that should be applied to "
2059                   "an LDAP connection. One of; NONE, SSL or STARTTLS."),
2060
2061     AP_INIT_FLAG("LDAPVerifyServerCert", util_ldap_set_verify_srv_cert, 
2062                   NULL, RSRC_CONF,
2063                   "Set to 'ON' requires that the server certificate be verified "
2064                   "before a secure LDAP connection can be establish.  Default 'ON'"),
2065
2066     AP_INIT_TAKE1("LDAPConnectionTimeout", util_ldap_set_connection_timeout, 
2067                   NULL, RSRC_CONF,
2068                   "Specify the LDAP socket connection timeout in seconds "
2069                   "(default: 10)"),
2070
2071     {NULL}
2072 };
2073
2074 static void util_ldap_register_hooks(apr_pool_t *p)
2075 {
2076     APR_REGISTER_OPTIONAL_FN(uldap_connection_open);
2077     APR_REGISTER_OPTIONAL_FN(uldap_connection_close);
2078     APR_REGISTER_OPTIONAL_FN(uldap_connection_unbind);
2079     APR_REGISTER_OPTIONAL_FN(uldap_connection_cleanup);
2080     APR_REGISTER_OPTIONAL_FN(uldap_connection_find);
2081     APR_REGISTER_OPTIONAL_FN(uldap_cache_comparedn);
2082     APR_REGISTER_OPTIONAL_FN(uldap_cache_compare);
2083     APR_REGISTER_OPTIONAL_FN(uldap_cache_checkuserid);
2084     APR_REGISTER_OPTIONAL_FN(uldap_cache_getuserdn);
2085     APR_REGISTER_OPTIONAL_FN(uldap_ssl_supported);
2086
2087     ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
2088     ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
2089     ap_hook_child_init(util_ldap_child_init, NULL, NULL, APR_HOOK_MIDDLE);
2090 }
2091
2092 module AP_MODULE_DECLARE_DATA ldap_module = {
2093    STANDARD20_MODULE_STUFF,
2094    NULL,                                /* create dir config */
2095    NULL,                                /* merge dir config */
2096    util_ldap_create_config,             /* create server config */
2097    util_ldap_merge_config,              /* merge server config */
2098    util_ldap_cmds,                      /* command table */
2099    util_ldap_register_hooks,            /* set up request processing hooks */
2100 };