]> granicus.if.org Git - apache/blob - modules/experimental/util_ldap.c
3b5e1c5869a3338455021c248be89a46a5066f04
[apache] / modules / experimental / util_ldap.c
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 /*
17  * util_ldap.c: LDAP things
18  * 
19  * Original code from auth_ldap module for Apache v1.3:
20  * Copyright 1998, 1999 Enbridge Pipelines Inc. 
21  * Copyright 1999-2001 Dave Carrigan
22  */
23
24 #include <apr_ldap.h>
25 #include <apr_strings.h>
26
27 #include "ap_config.h"
28 #include "httpd.h"
29 #include "http_config.h"
30 #include "http_core.h"
31 #include "http_log.h"
32 #include "http_protocol.h"
33 #include "http_request.h"
34 #include "util_ldap.h"
35 #include "util_ldap_cache.h"
36
37 #if APR_HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #if !APR_HAS_LDAP
42 #error mod_ldap requires APR-util to have LDAP support built in
43 #endif
44
45     /* defines for certificate file types
46     */
47 #define LDAP_CA_TYPE_UNKNOWN            0
48 #define LDAP_CA_TYPE_DER                1
49 #define LDAP_CA_TYPE_BASE64             2
50 #define LDAP_CA_TYPE_CERT7_DB           3
51
52
53 module AP_MODULE_DECLARE_DATA ldap_module;
54
55 int util_ldap_handler(request_rec *r);
56 void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
57
58
59 /*
60  * Some definitions to help between various versions of apache.
61  */
62
63 #ifndef DOCTYPE_HTML_2_0
64 #define DOCTYPE_HTML_2_0  "<!DOCTYPE HTML PUBLIC \"-//IETF//" \
65                           "DTD HTML 2.0//EN\">\n"
66 #endif
67
68 #ifndef DOCTYPE_HTML_3_2
69 #define DOCTYPE_HTML_3_2  "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
70                           "DTD HTML 3.2 Final//EN\">\n"
71 #endif
72
73 #ifndef DOCTYPE_HTML_4_0S
74 #define DOCTYPE_HTML_4_0S "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
75                           "DTD HTML 4.0//EN\"\n" \
76                           "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
77 #endif
78
79 #ifndef DOCTYPE_HTML_4_0T
80 #define DOCTYPE_HTML_4_0T "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
81                           "DTD HTML 4.0 Transitional//EN\"\n" \
82                           "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
83 #endif
84
85 #ifndef DOCTYPE_HTML_4_0F
86 #define DOCTYPE_HTML_4_0F "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
87                           "DTD HTML 4.0 Frameset//EN\"\n" \
88                           "\"http://www.w3.org/TR/REC-html40/frameset.dtd\">\n"
89 #endif
90
91 #define LDAP_CACHE_LOCK() \
92     apr_global_mutex_lock(st->util_ldap_cache_lock)
93 #define LDAP_CACHE_UNLOCK() \
94     apr_global_mutex_unlock(st->util_ldap_cache_lock)
95
96
97 static void util_ldap_strdup (char **str, const char *newstr)
98 {
99     if (*str) {
100         free(*str);
101         *str = NULL;
102     }
103
104     if (newstr) {
105         *str = calloc(1, strlen(newstr)+1);
106         strcpy (*str, newstr);
107     }
108 }
109
110 /*
111  * Status Handler
112  * --------------
113  *
114  * This handler generates a status page about the current performance of
115  * the LDAP cache. It is enabled as follows:
116  *
117  * <Location /ldap-status>
118  *   SetHandler ldap-status
119  * </Location>
120  *
121  */
122 int util_ldap_handler(request_rec *r)
123 {
124     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
125
126     r->allowed |= (1 << M_GET);
127     if (r->method_number != M_GET)
128         return DECLINED;
129
130     if (strcmp(r->handler, "ldap-status")) {
131         return DECLINED;
132     }
133
134     r->content_type = "text/html";
135     if (r->header_only)
136         return OK;
137
138     ap_rputs(DOCTYPE_HTML_3_2
139              "<html><head><title>LDAP Cache Information</title></head>\n", r);
140     ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information</h1>\n", r);
141
142     util_ald_cache_display(r, st);
143
144     return OK;
145 }
146
147 /* ------------------------------------------------------------------ */
148
149
150 /*
151  * Closes an LDAP connection by unlocking it. The next time
152  * util_ldap_connection_find() is called this connection will be
153  * available for reuse.
154  */
155 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc)
156 {
157
158     /*
159      * QUESTION:
160      *
161      * Is it safe leaving bound connections floating around between the
162      * different modules? Keeping the user bound is a performance boost,
163      * but it is also a potential security problem - maybe.
164      *
165      * For now we unbind the user when we finish with a connection, but
166      * we don't have to...
167      */
168
169     /* mark our connection as available for reuse */
170
171 #if APR_HAS_THREADS
172     apr_thread_mutex_unlock(ldc->lock);
173 #endif
174 }
175
176
177 /*
178  * Destroys an LDAP connection by unbinding and closing the connection to
179  * the LDAP server. It is used to bring the connection back to a known
180  * state after an error, and during pool cleanup.
181  */
182 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_unbind(void *param)
183 {
184     util_ldap_connection_t *ldc = param;
185
186     if (ldc) {
187         if (ldc->ldap) {
188             ldap_unbind_s(ldc->ldap);
189             ldc->ldap = NULL;
190         }
191         ldc->bound = 0;
192     }
193
194     return APR_SUCCESS;
195 }
196
197
198 /*
199  * Clean up an LDAP connection by unbinding and unlocking the connection.
200  * This function is registered with the pool cleanup function - causing
201  * the LDAP connections to be shut down cleanly on graceful restart.
202  */
203 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_cleanup(void *param)
204 {
205     util_ldap_connection_t *ldc = param;
206
207     if (ldc) {
208
209         /* unbind and disconnect from the LDAP server */
210         util_ldap_connection_unbind(ldc);
211
212         /* free the username and password */
213         if (ldc->bindpw) {
214             free((void*)ldc->bindpw);
215         }
216         if (ldc->binddn) {
217             free((void*)ldc->binddn);
218         }
219
220         /* unlock this entry */
221         util_ldap_connection_close(ldc);
222     
223     }
224
225     return APR_SUCCESS;
226 }
227
228
229 /*
230  * Connect to the LDAP server and binds. Does not connect if already
231  * connected (i.e. ldc->ldap is non-NULL.) Does not bind if already bound.
232  *
233  * Returns LDAP_SUCCESS on success; and an error code on failure
234  */
235 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r, 
236                                             util_ldap_connection_t *ldc)
237 {
238     int result = 0;
239     int failures = 0;
240     int version  = LDAP_VERSION3;
241
242     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
243                                 r->server->module_config, &ldap_module);
244
245     /* If the connection is already bound, return
246     */
247     if (ldc->bound)
248     {
249         ldc->reason = "LDAP: connection open successful (already bound)";
250         return LDAP_SUCCESS;
251     }
252
253     /* create the ldap session handle
254     */
255     if (NULL == ldc->ldap)
256     {
257             /* clear connection requested */
258         if (!ldc->secure)
259         {
260             ldc->ldap = ldap_init(const_cast(ldc->host), ldc->port);
261         }
262         else /* ssl connnection requested */
263         {
264                 /* check configuration to make sure it supports SSL
265                 */
266             if (st->ssl_support)
267             {
268                 #if APR_HAS_LDAP_SSL
269                 
270                 #if APR_HAS_NOVELL_LDAPSDK 
271                 ldc->ldap = ldapssl_init(ldc->host, ldc->port, 1);
272
273                 #elif APR_HAS_NETSCAPE_LDAPSDK
274                 ldc->ldap = ldapssl_init(ldc->host, ldc->port, 1);
275
276                 #elif APR_HAS_OPENLDAP_LDAPSDK
277                 ldc->ldap = ldap_init(ldc->host, ldc->port);
278                 if (NULL != ldc->ldap)
279                 {
280                     int SSLmode = LDAP_OPT_X_TLS_HARD;
281                     result = ldap_set_option(ldc->ldap, LDAP_OPT_X_TLS, &SSLmode);
282                     if (LDAP_SUCCESS != result)
283                     {
284                         ldap_unbind_s(ldc->ldap);
285                         ldc->reason = "LDAP: ldap_set_option - LDAP_OPT_X_TLS_HARD failed";
286                         ldc->ldap = NULL;
287                     }
288                 }
289
290                 #elif APR_HAS_MICROSOFT_LDAPSDK
291                 ldc->ldap = ldap_sslinit(const_cast(ldc->host), ldc->port, 1);
292
293                 #else
294                     ldc->reason = "LDAP: ssl connections not supported";
295                 #endif /* APR_HAS_NOVELL_LDAPSDK */
296             
297                 #endif /* APR_HAS_LDAP_SSL */
298             }
299             else
300                 ldc->reason = "LDAP: ssl connections not supported";
301         }
302
303         if (NULL == ldc->ldap)
304         {
305             ldc->bound = 0;
306             if (NULL == ldc->reason)
307                 ldc->reason = "LDAP: ldap initialization failed";
308             return(-1);
309         }
310
311         /* Set the alias dereferencing option */
312         ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
313
314         /* always default to LDAP V3 */
315         ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
316
317     }
318
319
320     /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
321      * returned.  Break out of the loop on Success or any other error.
322      *
323      * NOTE: Looping is probably not a great idea. If the server isn't 
324      * responding the chances it will respond after a few tries are poor.
325      * However, the original code looped and it only happens on
326      * the error condition.
327       */
328     for (failures=0; failures<10; failures++)
329     {
330         result = ldap_simple_bind_s(ldc->ldap, const_cast(ldc->binddn), const_cast(ldc->bindpw));
331         if (LDAP_SERVER_DOWN != result)
332             break;
333     }
334
335     /* free the handle if there was an error
336     */
337     if (LDAP_SUCCESS != result)
338     {
339         ldap_unbind_s(ldc->ldap);
340         ldc->ldap = NULL;
341         ldc->bound = 0;
342         ldc->reason = "LDAP: ldap_simple_bind_s() failed";
343     }
344     else {
345         ldc->bound = 1;
346         ldc->reason = "LDAP: connection open successful";
347     }
348
349     return(result);
350 }
351
352
353 /*
354  * Find an existing ldap connection struct that matches the
355  * provided ldap connection parameters.
356  *
357  * If not found in the cache, a new ldc structure will be allocated from st->pool
358  * and returned to the caller. If found in the cache, a pointer to the existing
359  * ldc structure will be returned.
360  */
361 LDAP_DECLARE(util_ldap_connection_t *)util_ldap_connection_find(request_rec *r, const char *host, int port,
362                                               const char *binddn, const char *bindpw, deref_options deref,
363                                               int secure )
364 {
365     struct util_ldap_connection_t *l, *p;       /* To traverse the linked list */
366
367     util_ldap_state_t *st = 
368         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
369         &ldap_module);
370
371
372 #if APR_HAS_THREADS
373     /* mutex lock this function */
374     if (!st->mutex) {
375         apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT, st->pool);
376     }
377     apr_thread_mutex_lock(st->mutex);
378 #endif
379
380     /* Search for an exact connection match in the list that is not
381      * being used.
382      */
383     for (l=st->connections,p=NULL; l; l=l->next) {
384 #if APR_HAS_THREADS
385         if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
386 #endif
387         if ((l->port == port) && (strcmp(l->host, host) == 0) && 
388             ((!l->binddn && !binddn) || (l->binddn && binddn && !strcmp(l->binddn, binddn))) && 
389             ((!l->bindpw && !bindpw) || (l->bindpw && bindpw && !strcmp(l->bindpw, bindpw))) && 
390             (l->deref == deref) && (l->secure == secure)) {
391
392             break;
393         }
394 #if APR_HAS_THREADS
395             /* If this connection didn't match the criteria, then we
396              * need to unlock the mutex so it is available to be reused.
397              */
398             apr_thread_mutex_unlock(l->lock);
399         }
400 #endif
401         p = l;
402     }
403
404     /* If nothing found, search again, but we don't care about the
405      * binddn and bindpw this time.
406      */
407     if (!l) {
408         for (l=st->connections,p=NULL; l; l=l->next) {
409 #if APR_HAS_THREADS
410             if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
411
412 #endif
413             if ((l->port == port) && (strcmp(l->host, host) == 0) && 
414                 (l->deref == deref) && (l->secure == secure)) {
415
416                 /* the bind credentials have changed */
417                 l->bound = 0;
418                 util_ldap_strdup((char**)&(l->binddn), binddn);
419                 util_ldap_strdup((char**)&(l->bindpw), bindpw);
420                 break;
421             }
422 #if APR_HAS_THREADS
423                 /* If this connection didn't match the criteria, then we
424                  * need to unlock the mutex so it is available to be reused.
425                  */
426                 apr_thread_mutex_unlock(l->lock);
427             }
428 #endif
429             p = l;
430         }
431     }
432
433 /* artificially disable cache */
434 /* l = NULL; */
435
436     /* If no connection what found after the second search, we
437      * must create one.
438      */
439     if (!l) {
440
441         /* 
442          * Add the new connection entry to the linked list. Note that we
443          * don't actually establish an LDAP connection yet; that happens
444          * the first time authentication is requested.
445          */
446         /* create the details to the pool in st */
447         l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
448 #if APR_HAS_THREADS
449         apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
450         apr_thread_mutex_lock(l->lock);
451 #endif
452         l->pool = st->pool;
453         l->bound = 0;
454         l->host = apr_pstrdup(st->pool, host);
455         l->port = port;
456         l->deref = deref;
457         util_ldap_strdup((char**)&(l->binddn), binddn);
458         util_ldap_strdup((char**)&(l->bindpw), bindpw);
459         l->secure = secure;
460
461         /* add the cleanup to the pool */
462         apr_pool_cleanup_register(l->pool, l,
463                                   util_ldap_connection_cleanup,
464                                   apr_pool_cleanup_null);
465
466         if (p) {
467             p->next = l;
468         }
469         else {
470             st->connections = l;
471         }
472     }
473
474 #if APR_HAS_THREADS
475     apr_thread_mutex_unlock(st->mutex);
476 #endif
477     return l;
478 }
479
480 /* ------------------------------------------------------------------ */
481
482 /*
483  * Compares two DNs to see if they're equal. The only way to do this correctly is to 
484  * search for the dn and then do ldap_get_dn() on the result. This should match the 
485  * initial dn, since it would have been also retrieved with ldap_get_dn(). This is
486  * expensive, so if the configuration value compare_dn_on_server is
487  * false, just does an ordinary strcmp.
488  *
489  * The lock for the ldap cache should already be acquired.
490  */
491 LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc, 
492                             const char *url, const char *dn, const char *reqdn, 
493                             int compare_dn_on_server)
494 {
495     int result = 0;
496     util_url_node_t *curl; 
497     util_url_node_t curnode;
498     util_dn_compare_node_t *node;
499     util_dn_compare_node_t newnode;
500     int failures = 0;
501     LDAPMessage *res, *entry;
502     char *searchdn;
503
504     util_ldap_state_t *st =  (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
505
506     /* get cache entry (or create one) */
507     LDAP_CACHE_LOCK();
508
509     curnode.url = url;
510     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
511     if (curl == NULL) {
512         curl = util_ald_create_caches(st, url);
513     }
514     LDAP_CACHE_UNLOCK();
515
516     /* a simple compare? */
517     if (!compare_dn_on_server) {
518         /* unlock this read lock */
519         if (strcmp(dn, reqdn)) {
520             ldc->reason = "DN Comparison FALSE (direct strcmp())";
521             return LDAP_COMPARE_FALSE;
522         }
523         else {
524             ldc->reason = "DN Comparison TRUE (direct strcmp())";
525             return LDAP_COMPARE_TRUE;
526         }
527     }
528
529     if (curl) {
530         /* no - it's a server side compare */
531         LDAP_CACHE_LOCK();
532     
533         /* is it in the compare cache? */
534         newnode.reqdn = (char *)reqdn;
535         node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
536         if (node != NULL) {
537             /* If it's in the cache, it's good */
538             /* unlock this read lock */
539             LDAP_CACHE_UNLOCK();
540             ldc->reason = "DN Comparison TRUE (cached)";
541             return LDAP_COMPARE_TRUE;
542         }
543     
544         /* unlock this read lock */
545         LDAP_CACHE_UNLOCK();
546     }
547
548 start_over:
549     if (failures++ > 10) {
550         /* too many failures */
551         return result;
552     }
553
554     /* make a server connection */
555     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
556         /* connect to server failed */
557         return result;
558     }
559
560     /* search for reqdn */
561     if ((result = ldap_search_ext_s(ldc->ldap, const_cast(reqdn), LDAP_SCOPE_BASE, 
562                                     "(objectclass=*)", NULL, 1, 
563                                     NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
564         ldc->reason = "DN Comparison ldap_search_ext_s() failed with server down";
565         util_ldap_connection_unbind(ldc);
566         goto start_over;
567     }
568     if (result != LDAP_SUCCESS) {
569         /* search for reqdn failed - no match */
570         ldc->reason = "DN Comparison ldap_search_ext_s() failed";
571         return result;
572     }
573
574     entry = ldap_first_entry(ldc->ldap, res);
575     searchdn = ldap_get_dn(ldc->ldap, entry);
576
577     ldap_msgfree(res);
578     if (strcmp(dn, searchdn) != 0) {
579         /* compare unsuccessful */
580         ldc->reason = "DN Comparison FALSE (checked on server)";
581         result = LDAP_COMPARE_FALSE;
582     }
583     else {
584         if (curl) {
585             /* compare successful - add to the compare cache */
586             LDAP_CACHE_LOCK();
587             newnode.reqdn = (char *)reqdn;
588             newnode.dn = (char *)dn;
589             
590             node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
591             if ((node == NULL) || 
592                 (strcmp(reqdn, node->reqdn) != 0) || (strcmp(dn, node->dn) != 0)) {
593
594                 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
595             }
596             LDAP_CACHE_UNLOCK();
597         }
598         ldc->reason = "DN Comparison TRUE (checked on server)";
599         result = LDAP_COMPARE_TRUE;
600     }
601     ldap_memfree(searchdn);
602     return result;
603
604 }
605
606 /*
607  * Does an generic ldap_compare operation. It accepts a cache that it will use
608  * to lookup the compare in the cache. We cache two kinds of compares 
609  * (require group compares) and (require user compares). Each compare has a different
610  * cache node: require group includes the DN; require user does not because the
611  * require user cache is owned by the 
612  *
613  */
614 LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
615                           const char *url, const char *dn,
616                           const char *attrib, const char *value)
617 {
618     int result = 0;
619     util_url_node_t *curl; 
620     util_url_node_t curnode;
621     util_compare_node_t *compare_nodep;
622     util_compare_node_t the_compare_node;
623     apr_time_t curtime = 0; /* silence gcc -Wall */
624     int failures = 0;
625
626     util_ldap_state_t *st = 
627         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
628         &ldap_module);
629
630     /* get cache entry (or create one) */
631     LDAP_CACHE_LOCK();
632     curnode.url = url;
633     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
634     if (curl == NULL) {
635         curl = util_ald_create_caches(st, url);
636     }
637     LDAP_CACHE_UNLOCK();
638
639     if (curl) {
640         /* make a comparison to the cache */
641         LDAP_CACHE_LOCK();
642         curtime = apr_time_now();
643     
644         the_compare_node.dn = (char *)dn;
645         the_compare_node.attrib = (char *)attrib;
646         the_compare_node.value = (char *)value;
647         the_compare_node.result = 0;
648     
649         compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
650     
651         if (compare_nodep != NULL) {
652             /* found it... */
653             if (curtime - compare_nodep->lastcompare > st->compare_cache_ttl) {
654                 /* ...but it is too old */
655                 util_ald_cache_remove(curl->compare_cache, compare_nodep);
656             }
657             else {
658                 /* ...and it is good */
659                 /* unlock this read lock */
660                 LDAP_CACHE_UNLOCK();
661                 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
662                     ldc->reason = "Comparison true (cached)";
663                     return compare_nodep->result;
664                 }
665                 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
666                     ldc->reason = "Comparison false (cached)";
667                     return compare_nodep->result;
668                 }
669                 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
670                     ldc->reason = "Comparison no such attribute (cached)";
671                     return compare_nodep->result;
672                 }
673                 else {
674                     ldc->reason = "Comparison undefined (cached)";
675                     return compare_nodep->result;
676                 }
677             }
678         }
679         /* unlock this read lock */
680         LDAP_CACHE_UNLOCK();
681     }
682
683 start_over:
684     if (failures++ > 10) {
685         /* too many failures */
686         return result;
687     }
688     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
689         /* connect failed */
690         return result;
691     }
692
693     if ((result = ldap_compare_s(ldc->ldap, const_cast(dn), const_cast(attrib), const_cast(value)))
694         == LDAP_SERVER_DOWN) { 
695         /* connection failed - try again */
696         ldc->reason = "ldap_compare_s() failed with server down";
697         util_ldap_connection_unbind(ldc);
698         goto start_over;
699     }
700
701     ldc->reason = "Comparison complete";
702     if ((LDAP_COMPARE_TRUE == result) || 
703         (LDAP_COMPARE_FALSE == result) ||
704         (LDAP_NO_SUCH_ATTRIBUTE == result)) {
705         if (curl) {
706             /* compare completed; caching result */
707             LDAP_CACHE_LOCK();
708             the_compare_node.lastcompare = curtime;
709             the_compare_node.result = result;
710
711             /* If the node doesn't exist then insert it, otherwise just update it with
712                the last results */
713             compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
714             if ((compare_nodep == NULL) || 
715                 (strcmp(the_compare_node.dn, compare_nodep->dn) != 0) || 
716                 (strcmp(the_compare_node.attrib, compare_nodep->attrib) != 0) || 
717                 (strcmp(the_compare_node.value, compare_nodep->value) != 0)) {
718
719                 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
720             }
721             else {
722                 compare_nodep->lastcompare = curtime;
723                 compare_nodep->result = result;
724             }
725             LDAP_CACHE_UNLOCK();
726         }
727         if (LDAP_COMPARE_TRUE == result) {
728             ldc->reason = "Comparison true (adding to cache)";
729             return LDAP_COMPARE_TRUE;
730         }
731         else if (LDAP_COMPARE_FALSE == result) {
732             ldc->reason = "Comparison false (adding to cache)";
733             return LDAP_COMPARE_FALSE;
734         }
735         else {
736             ldc->reason = "Comparison no such attribute (adding to cache)";
737             return LDAP_NO_SUCH_ATTRIBUTE;
738         }
739     }
740     return result;
741 }
742
743 LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
744                               const char *url, const char *basedn, int scope, char **attrs,
745                               const char *filter, const char *bindpw, const char **binddn,
746                               const char ***retvals)
747 {
748     const char **vals = NULL;
749     int result = 0;
750     LDAPMessage *res, *entry;
751     char *dn;
752     int count;
753     int failures = 0;
754     util_url_node_t *curl;              /* Cached URL node */
755     util_url_node_t curnode;
756     util_search_node_t *search_nodep;   /* Cached search node */
757     util_search_node_t the_search_node;
758     apr_time_t curtime;
759
760     util_ldap_state_t *st = 
761         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
762         &ldap_module);
763
764     /* Get the cache node for this url */
765     LDAP_CACHE_LOCK();
766     curnode.url = url;
767     curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
768     if (curl == NULL) {
769         curl = util_ald_create_caches(st, url);
770     }
771     LDAP_CACHE_UNLOCK();
772
773     if (curl) {
774         LDAP_CACHE_LOCK();
775         the_search_node.username = filter;
776         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
777         if (search_nodep != NULL && search_nodep->bindpw) {
778     
779             /* found entry in search cache... */
780             curtime = apr_time_now();
781     
782             /*
783              * Remove this item from the cache if its expired, or if the 
784              * sent password doesn't match the storepassword.
785              */
786             if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
787                 /* ...but entry is too old */
788                 util_ald_cache_remove(curl->search_cache, search_nodep);
789             }
790             else if (strcmp(search_nodep->bindpw, bindpw) != 0) {
791             /* ...but cached password doesn't match sent password */
792                 util_ald_cache_remove(curl->search_cache, search_nodep);
793             }
794             else {
795                 /* ...and entry is valid */
796                 *binddn = search_nodep->dn;
797                 *retvals = search_nodep->vals;
798                 LDAP_CACHE_UNLOCK();
799                 ldc->reason = "Authentication successful (cached)";
800                 return LDAP_SUCCESS;
801             }
802         }
803         /* unlock this read lock */
804         LDAP_CACHE_UNLOCK();
805     }
806
807     /*  
808      * At this point, there is no valid cached search, so lets do the search.
809      */
810
811     /*
812      * If any LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
813      */
814 start_over:
815     if (failures++ > 10) {
816         return result;
817     }
818     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
819         return result;
820     }
821
822     /* try do the search */
823     if ((result = ldap_search_ext_s(ldc->ldap,
824                                     const_cast(basedn), scope, 
825                                     const_cast(filter), attrs, 0, 
826                                     NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
827         ldc->reason = "ldap_search_ext_s() for user failed with server down";
828         util_ldap_connection_unbind(ldc);
829         goto start_over;
830     }
831
832     /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
833     if (result != LDAP_SUCCESS) {
834         ldc->reason = "ldap_search_ext_s() for user failed";
835         return result;
836     }
837
838     /* 
839      * We should have found exactly one entry; to find a different
840      * number is an error.
841      */
842     count = ldap_count_entries(ldc->ldap, res);
843     if (count != 1) 
844     {
845         if (count == 0 )
846             ldc->reason = "User not found";
847         else
848             ldc->reason = "User is not unique (search found two or more matches)";
849         ldap_msgfree(res);
850         return LDAP_NO_SUCH_OBJECT;
851     }
852
853     entry = ldap_first_entry(ldc->ldap, res);
854
855     /* Grab the dn, copy it into the pool, and free it again */
856     dn = ldap_get_dn(ldc->ldap, entry);
857     *binddn = apr_pstrdup(r->pool, dn);
858     ldap_memfree(dn);
859
860     /* 
861      * A bind to the server with an empty password always succeeds, so
862      * we check to ensure that the password is not empty. This implies
863      * that users who actually do have empty passwords will never be
864      * able to authenticate with this module. I don't see this as a big
865      * problem.
866      */
867     if (strlen(bindpw) <= 0) {
868         ldap_msgfree(res);
869         ldc->reason = "Empty password not allowed";
870         return LDAP_INVALID_CREDENTIALS;
871     }
872
873     /* 
874      * Attempt to bind with the retrieved dn and the password. If the bind
875      * fails, it means that the password is wrong (the dn obviously
876      * exists, since we just retrieved it)
877      */
878     if ((result = 
879          ldap_simple_bind_s(ldc->ldap, const_cast(*binddn), const_cast(bindpw))) == 
880          LDAP_SERVER_DOWN) {
881         ldc->reason = "ldap_simple_bind_s() to check user credentials failed with server down";
882         ldap_msgfree(res);
883         util_ldap_connection_unbind(ldc);
884         goto start_over;
885     }
886
887     /* failure? if so - return */
888     if (result != LDAP_SUCCESS) {
889         ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
890         ldap_msgfree(res);
891         util_ldap_connection_unbind(ldc);
892         return result;
893     }
894     else {
895         /*
896          * We have just bound the connection to a different user and password
897          * combination, which might be reused unintentionally next time this
898          * connection is used from the connection pool. To ensure no confusion,
899          * we mark the connection as unbound.
900          */
901         ldc->bound = 0;
902     }
903
904     /*
905      * Get values for the provided attributes.
906      */
907     if (attrs) {
908         int k = 0;
909         int i = 0;
910         while (attrs[k++]);
911         vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
912         while (attrs[i]) {
913             char **values;
914             int j = 0;
915             char *str = NULL;
916             /* get values */
917             values = ldap_get_values(ldc->ldap, entry, attrs[i]);
918             while (values && values[j]) {
919                 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) : apr_pstrdup(r->pool, values[j]);
920                 j++;
921             }
922             ldap_value_free(values);
923             vals[i] = str;
924             i++;
925         }
926         *retvals = vals;
927     }
928
929     /*          
930      * Add the new username to the search cache.
931      */
932     if (curl) {
933         LDAP_CACHE_LOCK();
934         the_search_node.username = filter;
935         the_search_node.dn = *binddn;
936         the_search_node.bindpw = bindpw;
937         the_search_node.lastbind = apr_time_now();
938         the_search_node.vals = vals;
939
940         /* Search again to make sure that another thread didn't ready insert this node
941            into the cache before we got here. If it does exist then update the lastbind */
942         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
943         if ((search_nodep == NULL) || 
944             (strcmp(*binddn, search_nodep->dn) != 0) || (strcmp(bindpw, search_nodep->bindpw) != 0)) {
945
946             util_ald_cache_insert(curl->search_cache, &the_search_node);
947         }
948         else {
949             search_nodep->lastbind = the_search_node.lastbind;
950         }
951         LDAP_CACHE_UNLOCK();
952     }
953     ldap_msgfree(res);
954
955     ldc->reason = "Authentication successful";
956     return LDAP_SUCCESS;
957 }
958
959
960 /*
961  * Reports if ssl support is enabled 
962  *
963  * 1 = enabled, 0 = not enabled
964  */
965 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r)
966 {
967    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
968                                 r->server->module_config, &ldap_module);
969
970    return(st->ssl_support);
971 }
972
973
974 /* ---------------------------------------- */
975 /* config directives */
976
977
978 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, const char *bytes)
979 {
980     util_ldap_state_t *st = 
981         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
982                                                   &ldap_module);
983
984     st->cache_bytes = atol(bytes);
985
986     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
987                  "[%" APR_PID_T_FMT "] ldap cache: Setting shared memory "
988                  " cache size to %" APR_SIZE_T_FMT " bytes.", 
989                  getpid(), st->cache_bytes);
990
991     return NULL;
992 }
993
994 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
995 {
996     util_ldap_state_t *st = 
997         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
998                                                   &ldap_module);
999
1000     if (file) {
1001         st->cache_file = ap_server_root_relative(st->pool, file);
1002     }
1003     else {
1004         st->cache_file = NULL;
1005     }
1006
1007     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1008                  "LDAP cache: Setting shared memory cache file to %s bytes.", 
1009                  st->cache_file);
1010
1011     return NULL;
1012 }
1013
1014 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1015 {
1016     util_ldap_state_t *st = 
1017         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1018                                                   &ldap_module);
1019
1020     st->search_cache_ttl = atol(ttl) * 1000000;
1021
1022     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1023                       "[%d] ldap cache: Setting cache TTL to %ld microseconds.", 
1024                       getpid(), st->search_cache_ttl);
1025
1026     return NULL;
1027 }
1028
1029 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy, const char *size)
1030 {
1031     util_ldap_state_t *st = 
1032         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1033                                                   &ldap_module);
1034
1035
1036     st->search_cache_size = atol(size);
1037     if (st->search_cache_size < 0) {
1038         st->search_cache_size = 0;
1039     }
1040
1041     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1042                       "[%d] ldap cache: Setting search cache size to %ld entries.", 
1043                       getpid(), st->search_cache_size);
1044
1045     return NULL;
1046 }
1047
1048 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1049 {
1050     util_ldap_state_t *st = 
1051         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1052                                                   &ldap_module);
1053
1054     st->compare_cache_ttl = atol(ttl) * 1000000;
1055
1056     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1057                       "[%d] ldap cache: Setting operation cache TTL to %ld microseconds.", 
1058                       getpid(), st->compare_cache_ttl);
1059
1060     return NULL;
1061 }
1062
1063 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy, const char *size)
1064 {
1065     util_ldap_state_t *st = 
1066         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1067                                                   &ldap_module);
1068
1069     st->compare_cache_size = atol(size);
1070     if (st->compare_cache_size < 0) {
1071         st->compare_cache_size = 0;
1072     }
1073
1074     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1075                       "[%d] ldap cache: Setting operation cache size to %ld entries.", 
1076                       getpid(), st->compare_cache_size);
1077
1078     return NULL;
1079 }
1080
1081 static const char *util_ldap_set_cert_auth(cmd_parms *cmd, void *dummy, const char *file)
1082 {
1083     util_ldap_state_t *st = 
1084         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1085                                                   &ldap_module);
1086     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1087     if (err != NULL) {
1088         return err;
1089     }
1090
1091     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1092                       "LDAP: SSL trusted certificate authority file - %s", 
1093                        file);
1094
1095     st->cert_auth_file = ap_server_root_relative(cmd->pool, file);
1096
1097     return(NULL);
1098 }
1099
1100
1101 static const char *util_ldap_set_cert_type(cmd_parms *cmd, void *dummy, const char *Type)
1102 {
1103     util_ldap_state_t *st = 
1104     (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1105                                               &ldap_module);
1106     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1107     if (err != NULL) {
1108         return err;
1109     }
1110
1111     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1112                       "LDAP: SSL trusted certificate authority file type - %s", 
1113                        Type);
1114
1115     if (0 == strcmp("DER_FILE", Type))
1116         st->cert_file_type = LDAP_CA_TYPE_DER;
1117
1118     else if (0 == strcmp("BASE64_FILE", Type))
1119         st->cert_file_type = LDAP_CA_TYPE_BASE64;
1120
1121     else if (0 == strcmp("CERT7_DB_PATH", Type))
1122         st->cert_file_type = LDAP_CA_TYPE_CERT7_DB;
1123
1124     else
1125         st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1126
1127     return(NULL);
1128 }
1129
1130
1131 void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1132 {
1133     util_ldap_state_t *st = 
1134         (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
1135
1136     st->pool = p;
1137
1138     st->cache_bytes = 100000;
1139     st->search_cache_ttl = 600000000;
1140     st->search_cache_size = 1024;
1141     st->compare_cache_ttl = 600000000;
1142     st->compare_cache_size = 1024;
1143     st->connections = NULL;
1144     st->cert_auth_file = NULL;
1145     st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1146     st->ssl_support = 0;
1147
1148     return st;
1149 }
1150
1151 static apr_status_t util_ldap_cleanup_module(void *data)
1152 {
1153 #if APR_HAS_LDAP_SSL && APR_HAS_NOVELL_LDAPSDK
1154     server_rec *s = data;
1155     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1156         s->module_config, &ldap_module);
1157     
1158     if (st->ssl_support)
1159         ldapssl_client_deinit();
1160
1161 #endif
1162     return APR_SUCCESS;
1163 }
1164
1165 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, 
1166                                  apr_pool_t *ptemp, server_rec *s)
1167 {
1168     int rc = LDAP_SUCCESS;
1169     apr_status_t result;
1170     char buf[MAX_STRING_LEN];
1171     server_rec *s_vhost;
1172     util_ldap_state_t *st_vhost;
1173
1174     util_ldap_state_t *st =
1175         (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1176
1177     void *data;
1178     const char *userdata_key = "util_ldap_init";
1179
1180     /* util_ldap_post_config() will be called twice. Don't bother
1181      * going through all of the initialization on the first call
1182      * because it will just be thrown away.*/
1183     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1184     if (!data) {
1185         apr_pool_userdata_set((const void *)1, userdata_key,
1186                                apr_pool_cleanup_null, s->process->pool);
1187
1188 #if APR_HAS_SHARED_MEMORY
1189         /* If the cache file already exists then delete it.  Otherwise we are
1190          * going to run into problems creating the shared memory. */
1191         if (st->cache_file) {
1192             char *lck_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1193             apr_file_remove(st->cache_file, ptemp);
1194             apr_file_remove(lck_file, ptemp);
1195         }
1196 #endif
1197         return OK;
1198     }
1199
1200 #if APR_HAS_SHARED_MEMORY
1201     /* initializing cache if shared memory size is not zero and we already don't have shm address */
1202     if (!st->cache_shm && st->cache_bytes > 0) {
1203 #endif
1204         result = util_ldap_cache_init(p, st);
1205         if (result != APR_SUCCESS) {
1206             apr_strerror(result, buf, sizeof(buf));
1207             ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
1208                          "LDAP cache: error while creating a shared memory segment: %s", buf);
1209         }
1210
1211
1212 #if APR_HAS_SHARED_MEMORY
1213         if (st->cache_file) {
1214             st->lock_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1215         }
1216         else
1217 #endif
1218             st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1219
1220         result = apr_global_mutex_create(&st->util_ldap_cache_lock, st->lock_file, APR_LOCK_DEFAULT, st->pool);
1221         if (result != APR_SUCCESS) {
1222             return result;
1223         }
1224
1225         /* merge config in all vhost */
1226         s_vhost = s->next;
1227         while (s_vhost) {
1228             st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
1229
1230 #if APR_HAS_SHARED_MEMORY
1231             st_vhost->cache_shm = st->cache_shm;
1232             st_vhost->cache_rmm = st->cache_rmm;
1233             st_vhost->cache_file = st->cache_file;
1234             ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
1235                          "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp for VHOST: %s",
1236                          st->cache_shm, st->cache_rmm, s_vhost->server_hostname);
1237 #endif
1238             st_vhost->lock_file = st->lock_file;
1239             s_vhost = s_vhost->next;
1240         }
1241 #if APR_HAS_SHARED_MEMORY
1242     }
1243     else {
1244         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "LDAP cache: LDAPSharedCacheSize is zero, disabling shared memory cache");
1245     }
1246 #endif
1247     
1248     /* log the LDAP SDK used 
1249      */
1250     #if APR_HAS_NETSCAPE_LDAPSDK 
1251     
1252         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1253              "LDAP: Built with Netscape LDAP SDK" );
1254
1255     #elif APR_HAS_NOVELL_LDAPSDK
1256
1257         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1258              "LDAP: Built with Novell LDAP SDK" );
1259
1260     #elif APR_HAS_OPENLDAP_LDAPSDK
1261
1262         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1263              "LDAP: Built with OpenLDAP LDAP SDK" );
1264
1265     #elif APR_HAS_MICROSOFT_LDAPSDK
1266     
1267         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1268              "LDAP: Built with Microsoft LDAP SDK" );
1269     #else
1270     
1271         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1272              "LDAP: Built with unknown LDAP SDK" );
1273
1274     #endif /* APR_HAS_NETSCAPE_LDAPSDK */
1275
1276
1277
1278     apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1279                               util_ldap_cleanup_module); 
1280
1281     /* initialize SSL support if requested
1282     */
1283     if (st->cert_auth_file)
1284     {
1285         #if APR_HAS_LDAP_SSL /* compiled with ssl support */
1286
1287         #if APR_HAS_NETSCAPE_LDAPSDK 
1288
1289             /* Netscape sdk only supports a cert7.db file 
1290             */
1291             if (st->cert_file_type == LDAP_CA_TYPE_CERT7_DB)
1292             {
1293                 rc = ldapssl_client_init(st->cert_auth_file, NULL);
1294             }
1295             else
1296             {
1297                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1298                          "LDAP: Invalid LDAPTrustedCAType directive - "
1299                           "CERT7_DB_PATH type required");
1300                 rc = -1;
1301             }
1302
1303         #elif APR_HAS_NOVELL_LDAPSDK
1304         
1305             /* Novell SDK supports DER or BASE64 files
1306             */
1307             if (st->cert_file_type == LDAP_CA_TYPE_DER  ||
1308                 st->cert_file_type == LDAP_CA_TYPE_BASE64 )
1309             {
1310                 rc = ldapssl_client_init(NULL, NULL);
1311                 if (LDAP_SUCCESS == rc)
1312                 {
1313                     if (st->cert_file_type == LDAP_CA_TYPE_BASE64)
1314                         rc = ldapssl_add_trusted_cert(st->cert_auth_file, 
1315                                                   LDAPSSL_CERT_FILETYPE_B64);
1316                     else
1317                         rc = ldapssl_add_trusted_cert(st->cert_auth_file, 
1318                                                   LDAPSSL_CERT_FILETYPE_DER);
1319
1320                     if (LDAP_SUCCESS != rc)
1321                         ldapssl_client_deinit();
1322                 }
1323             }
1324             else
1325             {
1326                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1327                              "LDAP: Invalid LDAPTrustedCAType directive - "
1328                              "DER_FILE or BASE64_FILE type required");
1329                 rc = -1;
1330             }
1331
1332         #elif APR_HAS_OPENLDAP_LDAPSDK
1333
1334             /* OpenLDAP SDK supports BASE64 files
1335             */
1336             if (st->cert_file_type == LDAP_CA_TYPE_BASE64)
1337             {
1338                 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, st->cert_auth_file);
1339             }
1340             else
1341             {
1342                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1343                              "LDAP: Invalid LDAPTrustedCAType directive - "
1344                              "BASE64_FILE type required");
1345                 rc = -1;
1346             }
1347
1348
1349         #elif APR_HAS_MICROSOFT_LDAPSDK
1350             
1351             /* Microsoft SDK use the registry certificate store - always
1352              * assume support is always available
1353             */
1354             rc = LDAP_SUCCESS;
1355
1356         #else
1357             rc = -1;
1358         #endif /* APR_HAS_NETSCAPE_LDAPSDK */
1359
1360         #else  /* not compiled with SSL Support */
1361
1362             ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1363                      "LDAP: Not built with SSL support." );
1364             rc = -1;
1365
1366         #endif /* APR_HAS_LDAP_SSL */
1367
1368         if (LDAP_SUCCESS == rc)
1369         {
1370             st->ssl_support = 1;
1371         }
1372         else
1373         {
1374             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, 
1375                          "LDAP: SSL initialization failed");
1376             st->ssl_support = 0;
1377         }
1378     }
1379       
1380         /* The Microsoft SDK uses the registry certificate store -
1381          * always assume support is available
1382         */
1383     #if APR_HAS_MICROSOFT_LDAPSDK
1384         st->ssl_support = 1;
1385     #endif
1386     
1387
1388         /* log SSL status - If SSL isn't available it isn't necessarily
1389          * an error because the modules asking for LDAP connections 
1390          * may not ask for SSL support
1391         */
1392     if (st->ssl_support)
1393     {
1394        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1395                          "LDAP: SSL support available" );
1396     }
1397     else
1398     {
1399        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1400                          "LDAP: SSL support unavailable" );
1401     }
1402     
1403     return(OK);
1404 }
1405
1406 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1407 {
1408     apr_status_t sts;
1409     util_ldap_state_t *st =
1410         (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1411
1412     sts = apr_global_mutex_child_init(&st->util_ldap_cache_lock, st->lock_file, p);
1413     if (sts != APR_SUCCESS) {
1414         ap_log_error(APLOG_MARK, APLOG_CRIT, sts, s, "failed to init caching lock in child process");
1415         return;
1416     }
1417     else {
1418         ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, s, 
1419                      "INIT global mutex %s in child %d ", st->lock_file, getpid());
1420     }
1421 }
1422
1423 command_rec util_ldap_cmds[] = {
1424     AP_INIT_TAKE1("LDAPSharedCacheSize", util_ldap_set_cache_bytes, NULL, RSRC_CONF,
1425                   "Sets the size of the shared memory cache in bytes. "
1426                   "Zero means disable the shared memory cache. Defaults to 100KB."),
1427
1428     AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, NULL, RSRC_CONF,
1429                   "Sets the file of the shared memory cache."
1430                   "Nothing means disable the shared memory cache."),
1431
1432     AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, NULL, RSRC_CONF,
1433                   "Sets the maximum number of entries that are possible in the LDAP "
1434                   "search cache. "
1435                   "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1436
1437     AP_INIT_TAKE1("LDAPCacheTTL", util_ldap_set_cache_ttl, NULL, RSRC_CONF,
1438                   "Sets the maximum time (in seconds) that an item can be cached in the LDAP "
1439                   "search cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1440
1441     AP_INIT_TAKE1("LDAPOpCacheEntries", util_ldap_set_opcache_entries, NULL, RSRC_CONF,
1442                   "Sets the maximum number of entries that are possible in the LDAP "
1443                   "compare cache. "
1444                   "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1445
1446     AP_INIT_TAKE1("LDAPOpCacheTTL", util_ldap_set_opcache_ttl, NULL, RSRC_CONF,
1447                   "Sets the maximum time (in seconds) that an item is cached in the LDAP "
1448                   "operation cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1449
1450     AP_INIT_TAKE1("LDAPTrustedCA", util_ldap_set_cert_auth, NULL, RSRC_CONF,
1451                   "Sets the file containing the trusted Certificate Authority certificate. "
1452                   "Used to validate the LDAP server certificate for SSL connections."),
1453
1454     AP_INIT_TAKE1("LDAPTrustedCAType", util_ldap_set_cert_type, NULL, RSRC_CONF,
1455                  "Specifies the type of the Certificate Authority file.  "
1456                  "The following types are supported:  "
1457                  "    DER_FILE      - file in binary DER format "
1458                  "    BASE64_FILE   - file in Base64 format "
1459                  "    CERT7_DB_PATH - Netscape certificate database file "),
1460     {NULL}
1461 };
1462
1463 static void util_ldap_register_hooks(apr_pool_t *p)
1464 {
1465     ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1466     ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
1467     ap_hook_child_init(util_ldap_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1468 }
1469
1470 module ldap_module = {
1471    STANDARD20_MODULE_STUFF,
1472    NULL,                                /* dir config creater */
1473    NULL,                                /* dir merger --- default is to override */
1474    util_ldap_create_config,             /* server config */
1475    NULL,                                /* merge server config */
1476    util_ldap_cmds,                      /* command table */
1477    util_ldap_register_hooks,            /* set up request processing hooks */
1478 };