bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / experimental / util_ldap.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * 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 <apr_ldap.h>
26 #include <apr_strings.h>
27
28 #include "ap_config.h"
29 #include "httpd.h"
30 #include "http_config.h"
31 #include "http_core.h"
32 #include "http_log.h"
33 #include "http_protocol.h"
34 #include "http_request.h"
35 #include "util_ldap.h"
36 #include "util_ldap_cache.h"
37
38 #if APR_HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #ifndef APU_HAS_LDAP
43 #error mod_ldap requires APR-util to have LDAP support built in
44 #endif
45
46 #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
47 #include "unixd.h"
48 #define UTIL_LDAP_SET_MUTEX_PERMS
49 #endif
50
51     /* defines for certificate file types
52     */
53 #define LDAP_CA_TYPE_UNKNOWN            0
54 #define LDAP_CA_TYPE_DER                1
55 #define LDAP_CA_TYPE_BASE64             2
56 #define LDAP_CA_TYPE_CERT7_DB           3
57
58
59 module AP_MODULE_DECLARE_DATA ldap_module;
60
61 int util_ldap_handler(request_rec *r);
62 void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
63
64
65 /*
66  * Some definitions to help between various versions of apache.
67  */
68
69 #ifndef DOCTYPE_HTML_2_0
70 #define DOCTYPE_HTML_2_0  "<!DOCTYPE HTML PUBLIC \"-//IETF//" \
71                           "DTD HTML 2.0//EN\">\n"
72 #endif
73
74 #ifndef DOCTYPE_HTML_3_2
75 #define DOCTYPE_HTML_3_2  "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
76                           "DTD HTML 3.2 Final//EN\">\n"
77 #endif
78
79 #ifndef DOCTYPE_HTML_4_0S
80 #define DOCTYPE_HTML_4_0S "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
81                           "DTD HTML 4.0//EN\"\n" \
82                           "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
83 #endif
84
85 #ifndef DOCTYPE_HTML_4_0T
86 #define DOCTYPE_HTML_4_0T "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
87                           "DTD HTML 4.0 Transitional//EN\"\n" \
88                           "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
89 #endif
90
91 #ifndef DOCTYPE_HTML_4_0F
92 #define DOCTYPE_HTML_4_0F "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
93                           "DTD HTML 4.0 Frameset//EN\"\n" \
94                           "\"http://www.w3.org/TR/REC-html40/frameset.dtd\">\n"
95 #endif
96
97 #define LDAP_CACHE_LOCK() \
98     if (st->util_ldap_cache_lock) \
99       apr_global_mutex_lock(st->util_ldap_cache_lock)
100 #define LDAP_CACHE_UNLOCK() \
101     if (st->util_ldap_cache_lock) \
102       apr_global_mutex_unlock(st->util_ldap_cache_lock)
103
104
105 static void util_ldap_strdup (char **str, const char *newstr)
106 {
107     if (*str) {
108         free(*str);
109         *str = NULL;
110     }
111
112     if (newstr) {
113         *str = calloc(1, strlen(newstr)+1);
114         strcpy (*str, newstr);
115     }
116 }
117
118 /*
119  * Status Handler
120  * --------------
121  *
122  * This handler generates a status page about the current performance of
123  * the LDAP cache. It is enabled as follows:
124  *
125  * <Location /ldap-status>
126  *   SetHandler ldap-status
127  * </Location>
128  *
129  */
130 int util_ldap_handler(request_rec *r)
131 {
132     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
133
134     r->allowed |= (1 << M_GET);
135     if (r->method_number != M_GET)
136         return DECLINED;
137
138     if (strcmp(r->handler, "ldap-status")) {
139         return DECLINED;
140     }
141
142     r->content_type = "text/html; charset=ISO-8859-1";
143     if (r->header_only)
144         return OK;
145
146     ap_rputs(DOCTYPE_HTML_3_2
147              "<html><head><title>LDAP Cache Information</title></head>\n", r);
148     ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information</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  * util_ldap_connection_find() is called this connection will be
161  * available for reuse.
162  */
163 LDAP_DECLARE(void) util_ldap_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 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_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 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_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         util_ldap_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         util_ldap_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 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r, 
244                                             util_ldap_connection_t *ldc)
245 {
246     int result = 0;
247     int failures = 0;
248     int version  = LDAP_VERSION3;
249     int rc = LDAP_SUCCESS;
250     struct timeval timeOut = {10,0};    /* 10 second connection timeout */
251
252     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
253                                 r->server->module_config, &ldap_module);
254
255     /* If the connection is already bound, return
256     */
257     if (ldc->bound)
258     {
259         ldc->reason = "LDAP: connection open successful (already bound)";
260         return LDAP_SUCCESS;
261     }
262
263     /* create the ldap session handle
264     */
265     if (NULL == ldc->ldap)
266     {
267             /* clear connection requested */
268         if (!ldc->secure)
269         {
270             ldc->ldap = ldap_init(const_cast(ldc->host), ldc->port);
271         }
272         else /* ssl connnection requested */
273         {
274                 /* check configuration to make sure it supports SSL
275                 */
276             if (st->ssl_support)
277             {
278                 #if APR_HAS_LDAP_SSL
279                 
280                 #if APR_HAS_NOVELL_LDAPSDK 
281                 ldc->ldap = ldapssl_init(ldc->host, ldc->port, 1);
282
283                 #elif APR_HAS_NETSCAPE_LDAPSDK
284                 ldc->ldap = ldapssl_init(ldc->host, ldc->port, 1);
285
286                 #elif APR_HAS_OPENLDAP_LDAPSDK
287                 ldc->ldap = ldap_init(ldc->host, ldc->port);
288                 if (NULL != ldc->ldap)
289                 {
290                     int SSLmode = LDAP_OPT_X_TLS_HARD;
291                     result = ldap_set_option(ldc->ldap, LDAP_OPT_X_TLS, &SSLmode);
292                     if (LDAP_SUCCESS != result)
293                     {
294                         ldap_unbind_s(ldc->ldap);
295                         ldc->reason = "LDAP: ldap_set_option - LDAP_OPT_X_TLS_HARD failed";
296                         ldc->ldap = NULL;
297                     }
298                 }
299
300                 #elif APR_HAS_MICROSOFT_LDAPSDK
301                 ldc->ldap = ldap_sslinit(const_cast(ldc->host), ldc->port, 1);
302
303                 #else
304                     ldc->reason = "LDAP: ssl connections not supported";
305                 #endif /* APR_HAS_NOVELL_LDAPSDK */
306             
307                 #endif /* APR_HAS_LDAP_SSL */
308             }
309             else
310                 ldc->reason = "LDAP: ssl connections not supported";
311         }
312
313         if (NULL == ldc->ldap)
314         {
315             ldc->bound = 0;
316             if (NULL == ldc->reason)
317                 ldc->reason = "LDAP: ldap initialization failed";
318             return(-1);
319         }
320
321         /* Set the alias dereferencing option */
322         ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
323
324         /* always default to LDAP V3 */
325         ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
326
327 #ifdef LDAP_OPT_NETWORK_TIMEOUT
328         if (st->connectionTimeout > 0) {
329             timeOut.tv_sec = st->connectionTimeout;
330         }
331
332         if (st->connectionTimeout >= 0) {
333             rc = ldap_set_option(ldc->ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *)&timeOut);
334             if (APR_SUCCESS != rc) {
335                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
336                                  "LDAP: Could not set the connection timeout" );
337             }
338         }
339 #endif
340     }
341
342
343     /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
344      * returned.  Break out of the loop on Success or any other error.
345      *
346      * NOTE: Looping is probably not a great idea. If the server isn't 
347      * responding the chances it will respond after a few tries are poor.
348      * However, the original code looped and it only happens on
349      * the error condition.
350       */
351     for (failures=0; failures<10; failures++)
352     {
353         result = ldap_simple_bind_s(ldc->ldap, const_cast(ldc->binddn), const_cast(ldc->bindpw));
354         if (LDAP_SERVER_DOWN != result)
355             break;
356     }
357
358     /* free the handle if there was an error
359     */
360     if (LDAP_SUCCESS != result)
361     {
362         ldap_unbind_s(ldc->ldap);
363         ldc->ldap = NULL;
364         ldc->bound = 0;
365         ldc->reason = "LDAP: ldap_simple_bind_s() failed";
366     }
367     else {
368         ldc->bound = 1;
369         ldc->reason = "LDAP: connection open successful";
370     }
371
372     return(result);
373 }
374
375
376 /*
377  * Find an existing ldap connection struct that matches the
378  * provided ldap connection parameters.
379  *
380  * If not found in the cache, a new ldc structure will be allocated from st->pool
381  * and returned to the caller. If found in the cache, a pointer to the existing
382  * ldc structure will be returned.
383  */
384 LDAP_DECLARE(util_ldap_connection_t *)util_ldap_connection_find(request_rec *r, const char *host, int port,
385                                               const char *binddn, const char *bindpw, deref_options deref,
386                                               int secure )
387 {
388     struct util_ldap_connection_t *l, *p;       /* To traverse the linked list */
389
390     util_ldap_state_t *st = 
391         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
392         &ldap_module);
393
394
395 #if APR_HAS_THREADS
396     /* mutex lock this function */
397     apr_thread_mutex_lock(st->mutex);
398 #endif
399
400     /* Search for an exact connection match in the list that is not
401      * being used.
402      */
403     for (l=st->connections,p=NULL; l; l=l->next) {
404 #if APR_HAS_THREADS
405         if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
406 #endif
407         if ((l->port == port) && (strcmp(l->host, host) == 0) && 
408             ((!l->binddn && !binddn) || (l->binddn && binddn && !strcmp(l->binddn, binddn))) && 
409             ((!l->bindpw && !bindpw) || (l->bindpw && bindpw && !strcmp(l->bindpw, bindpw))) && 
410             (l->deref == deref) && (l->secure == secure)) {
411
412             break;
413         }
414 #if APR_HAS_THREADS
415             /* If this connection didn't match the criteria, then we
416              * need to unlock the mutex so it is available to be reused.
417              */
418             apr_thread_mutex_unlock(l->lock);
419         }
420 #endif
421         p = l;
422     }
423
424     /* If nothing found, search again, but we don't care about the
425      * binddn and bindpw this time.
426      */
427     if (!l) {
428         for (l=st->connections,p=NULL; l; l=l->next) {
429 #if APR_HAS_THREADS
430             if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
431
432 #endif
433             if ((l->port == port) && (strcmp(l->host, host) == 0) && 
434                 (l->deref == deref) && (l->secure == secure)) {
435
436                 /* the bind credentials have changed */
437                 l->bound = 0;
438                 util_ldap_strdup((char**)&(l->binddn), binddn);
439                 util_ldap_strdup((char**)&(l->bindpw), bindpw);
440                 break;
441             }
442 #if APR_HAS_THREADS
443                 /* If this connection didn't match the criteria, then we
444                  * need to unlock the mutex so it is available to be reused.
445                  */
446                 apr_thread_mutex_unlock(l->lock);
447             }
448 #endif
449             p = l;
450         }
451     }
452
453 /* artificially disable cache */
454 /* l = NULL; */
455
456     /* If no connection what found after the second search, we
457      * must create one.
458      */
459     if (!l) {
460
461         /* 
462          * Add the new connection entry to the linked list. Note that we
463          * don't actually establish an LDAP connection yet; that happens
464          * the first time authentication is requested.
465          */
466         /* create the details to the pool in st */
467         l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
468 #if APR_HAS_THREADS
469         apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
470         apr_thread_mutex_lock(l->lock);
471 #endif
472         l->pool = st->pool;
473         l->bound = 0;
474         l->host = apr_pstrdup(st->pool, host);
475         l->port = port;
476         l->deref = deref;
477         util_ldap_strdup((char**)&(l->binddn), binddn);
478         util_ldap_strdup((char**)&(l->bindpw), bindpw);
479         l->secure = secure;
480
481         /* add the cleanup to the pool */
482         apr_pool_cleanup_register(l->pool, l,
483                                   util_ldap_connection_cleanup,
484                                   apr_pool_cleanup_null);
485
486         if (p) {
487             p->next = l;
488         }
489         else {
490             st->connections = l;
491         }
492     }
493
494 #if APR_HAS_THREADS
495     apr_thread_mutex_unlock(st->mutex);
496 #endif
497     return l;
498 }
499
500 /* ------------------------------------------------------------------ */
501
502 /*
503  * Compares two DNs to see if they're equal. The only way to do this correctly is to 
504  * search for the dn and then do ldap_get_dn() on the result. This should match the 
505  * initial dn, since it would have been also retrieved with ldap_get_dn(). This is
506  * expensive, so if the configuration value compare_dn_on_server is
507  * false, just does an ordinary strcmp.
508  *
509  * The lock for the ldap cache should already be acquired.
510  */
511 LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc, 
512                             const char *url, const char *dn, const char *reqdn, 
513                             int compare_dn_on_server)
514 {
515     int result = 0;
516     util_url_node_t *curl; 
517     util_url_node_t curnode;
518     util_dn_compare_node_t *node;
519     util_dn_compare_node_t newnode;
520     int failures = 0;
521     LDAPMessage *res, *entry;
522     char *searchdn;
523
524     util_ldap_state_t *st =  (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
525
526     /* get cache entry (or create one) */
527     LDAP_CACHE_LOCK();
528
529     curnode.url = url;
530     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
531     if (curl == NULL) {
532         curl = util_ald_create_caches(st, url);
533     }
534     LDAP_CACHE_UNLOCK();
535
536     /* a simple compare? */
537     if (!compare_dn_on_server) {
538         /* unlock this read lock */
539         if (strcmp(dn, reqdn)) {
540             ldc->reason = "DN Comparison FALSE (direct strcmp())";
541             return LDAP_COMPARE_FALSE;
542         }
543         else {
544             ldc->reason = "DN Comparison TRUE (direct strcmp())";
545             return LDAP_COMPARE_TRUE;
546         }
547     }
548
549     if (curl) {
550         /* no - it's a server side compare */
551         LDAP_CACHE_LOCK();
552     
553         /* is it in the compare cache? */
554         newnode.reqdn = (char *)reqdn;
555         node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
556         if (node != NULL) {
557             /* If it's in the cache, it's good */
558             /* unlock this read lock */
559             LDAP_CACHE_UNLOCK();
560             ldc->reason = "DN Comparison TRUE (cached)";
561             return LDAP_COMPARE_TRUE;
562         }
563     
564         /* unlock this read lock */
565         LDAP_CACHE_UNLOCK();
566     }
567
568 start_over:
569     if (failures++ > 10) {
570         /* too many failures */
571         return result;
572     }
573
574     /* make a server connection */
575     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
576         /* connect to server failed */
577         return result;
578     }
579
580     /* search for reqdn */
581     if ((result = ldap_search_ext_s(ldc->ldap, const_cast(reqdn), LDAP_SCOPE_BASE, 
582                                     "(objectclass=*)", NULL, 1, 
583                                     NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
584         ldc->reason = "DN Comparison ldap_search_ext_s() failed with server down";
585         util_ldap_connection_unbind(ldc);
586         goto start_over;
587     }
588     if (result != LDAP_SUCCESS) {
589         /* search for reqdn failed - no match */
590         ldc->reason = "DN Comparison ldap_search_ext_s() failed";
591         return result;
592     }
593
594     entry = ldap_first_entry(ldc->ldap, res);
595     searchdn = ldap_get_dn(ldc->ldap, entry);
596
597     ldap_msgfree(res);
598     if (strcmp(dn, searchdn) != 0) {
599         /* compare unsuccessful */
600         ldc->reason = "DN Comparison FALSE (checked on server)";
601         result = LDAP_COMPARE_FALSE;
602     }
603     else {
604         if (curl) {
605             /* compare successful - add to the compare cache */
606             LDAP_CACHE_LOCK();
607             newnode.reqdn = (char *)reqdn;
608             newnode.dn = (char *)dn;
609             
610             node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
611             if ((node == NULL) || 
612                 (strcmp(reqdn, node->reqdn) != 0) || (strcmp(dn, node->dn) != 0)) {
613
614                 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
615             }
616             LDAP_CACHE_UNLOCK();
617         }
618         ldc->reason = "DN Comparison TRUE (checked on server)";
619         result = LDAP_COMPARE_TRUE;
620     }
621     ldap_memfree(searchdn);
622     return result;
623
624 }
625
626 /*
627  * Does an generic ldap_compare operation. It accepts a cache that it will use
628  * to lookup the compare in the cache. We cache two kinds of compares 
629  * (require group compares) and (require user compares). Each compare has a different
630  * cache node: require group includes the DN; require user does not because the
631  * require user cache is owned by the 
632  *
633  */
634 LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
635                           const char *url, const char *dn,
636                           const char *attrib, const char *value)
637 {
638     int result = 0;
639     util_url_node_t *curl; 
640     util_url_node_t curnode;
641     util_compare_node_t *compare_nodep;
642     util_compare_node_t the_compare_node;
643     apr_time_t curtime = 0; /* silence gcc -Wall */
644     int failures = 0;
645
646     util_ldap_state_t *st = 
647         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
648         &ldap_module);
649
650     /* get cache entry (or create one) */
651     LDAP_CACHE_LOCK();
652     curnode.url = url;
653     curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
654     if (curl == NULL) {
655         curl = util_ald_create_caches(st, url);
656     }
657     LDAP_CACHE_UNLOCK();
658
659     if (curl) {
660         /* make a comparison to the cache */
661         LDAP_CACHE_LOCK();
662         curtime = apr_time_now();
663     
664         the_compare_node.dn = (char *)dn;
665         the_compare_node.attrib = (char *)attrib;
666         the_compare_node.value = (char *)value;
667         the_compare_node.result = 0;
668     
669         compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
670     
671         if (compare_nodep != NULL) {
672             /* found it... */
673             if (curtime - compare_nodep->lastcompare > st->compare_cache_ttl) {
674                 /* ...but it is too old */
675                 util_ald_cache_remove(curl->compare_cache, compare_nodep);
676             }
677             else {
678                 /* ...and it is good */
679                 /* unlock this read lock */
680                 LDAP_CACHE_UNLOCK();
681                 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
682                     ldc->reason = "Comparison true (cached)";
683                     return compare_nodep->result;
684                 }
685                 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
686                     ldc->reason = "Comparison false (cached)";
687                     return compare_nodep->result;
688                 }
689                 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
690                     ldc->reason = "Comparison no such attribute (cached)";
691                     return compare_nodep->result;
692                 }
693                 else {
694                     ldc->reason = "Comparison undefined (cached)";
695                     return compare_nodep->result;
696                 }
697             }
698         }
699         /* unlock this read lock */
700         LDAP_CACHE_UNLOCK();
701     }
702
703 start_over:
704     if (failures++ > 10) {
705         /* too many failures */
706         return result;
707     }
708     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
709         /* connect failed */
710         return result;
711     }
712
713     if ((result = ldap_compare_s(ldc->ldap, const_cast(dn), const_cast(attrib), const_cast(value)))
714         == LDAP_SERVER_DOWN) { 
715         /* connection failed - try again */
716         ldc->reason = "ldap_compare_s() failed with server down";
717         util_ldap_connection_unbind(ldc);
718         goto start_over;
719     }
720
721     ldc->reason = "Comparison complete";
722     if ((LDAP_COMPARE_TRUE == result) || 
723         (LDAP_COMPARE_FALSE == result) ||
724         (LDAP_NO_SUCH_ATTRIBUTE == result)) {
725         if (curl) {
726             /* compare completed; caching result */
727             LDAP_CACHE_LOCK();
728             the_compare_node.lastcompare = curtime;
729             the_compare_node.result = result;
730
731             /* If the node doesn't exist then insert it, otherwise just update it with
732                the last results */
733             compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
734             if ((compare_nodep == NULL) || 
735                 (strcmp(the_compare_node.dn, compare_nodep->dn) != 0) || 
736                 (strcmp(the_compare_node.attrib, compare_nodep->attrib) != 0) || 
737                 (strcmp(the_compare_node.value, compare_nodep->value) != 0)) {
738
739                 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
740             }
741             else {
742                 compare_nodep->lastcompare = curtime;
743                 compare_nodep->result = result;
744             }
745             LDAP_CACHE_UNLOCK();
746         }
747         if (LDAP_COMPARE_TRUE == result) {
748             ldc->reason = "Comparison true (adding to cache)";
749             return LDAP_COMPARE_TRUE;
750         }
751         else if (LDAP_COMPARE_FALSE == result) {
752             ldc->reason = "Comparison false (adding to cache)";
753             return LDAP_COMPARE_FALSE;
754         }
755         else {
756             ldc->reason = "Comparison no such attribute (adding to cache)";
757             return LDAP_NO_SUCH_ATTRIBUTE;
758         }
759     }
760     return result;
761 }
762
763 LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
764                               const char *url, const char *basedn, int scope, char **attrs,
765                               const char *filter, const char *bindpw, const char **binddn,
766                               const char ***retvals)
767 {
768     const char **vals = NULL;
769     int numvals = 0;
770     int result = 0;
771     LDAPMessage *res, *entry;
772     char *dn;
773     int count;
774     int failures = 0;
775     util_url_node_t *curl;              /* Cached URL node */
776     util_url_node_t curnode;
777     util_search_node_t *search_nodep;   /* Cached search node */
778     util_search_node_t the_search_node;
779     apr_time_t curtime;
780
781     util_ldap_state_t *st = 
782         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
783         &ldap_module);
784
785     /* Get the cache node for this url */
786     LDAP_CACHE_LOCK();
787     curnode.url = url;
788     curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
789     if (curl == NULL) {
790         curl = util_ald_create_caches(st, url);
791     }
792     LDAP_CACHE_UNLOCK();
793
794     if (curl) {
795         LDAP_CACHE_LOCK();
796         the_search_node.username = filter;
797         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
798         if (search_nodep != NULL) {
799     
800             /* found entry in search cache... */
801             curtime = apr_time_now();
802     
803             /*
804              * Remove this item from the cache if its expired.
805              * If the sent password doesn't match the stored password,
806              * the entry will be removed and readded later if the
807              * credentials pass authentication.
808              */
809             if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
810                 /* ...but entry is too old */
811                 util_ald_cache_remove(curl->search_cache, search_nodep);
812             }
813             else if ((search_nodep->bindpw) &&
814                 (search_nodep->bindpw[0] != '\0') &&
815                 (strcmp(search_nodep->bindpw, bindpw) == 0)) {
816                 /* ...and entry is valid */
817                 *binddn = search_nodep->dn;
818                 *retvals = search_nodep->vals;
819                 LDAP_CACHE_UNLOCK();
820                 ldc->reason = "Authentication successful (cached)";
821                 return LDAP_SUCCESS;
822             }
823         }
824         /* unlock this read lock */
825         LDAP_CACHE_UNLOCK();
826     }
827
828     /*  
829      * At this point, there is no valid cached search, so lets do the search.
830      */
831
832     /*
833      * If any LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
834      */
835 start_over:
836     if (failures++ > 10) {
837         return result;
838     }
839     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
840         return result;
841     }
842
843     /* try do the search */
844     if ((result = ldap_search_ext_s(ldc->ldap,
845                                     const_cast(basedn), scope, 
846                                     const_cast(filter), attrs, 0, 
847                                     NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
848         ldc->reason = "ldap_search_ext_s() for user failed with server down";
849         util_ldap_connection_unbind(ldc);
850         goto start_over;
851     }
852
853     /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
854     if (result != LDAP_SUCCESS) {
855         ldc->reason = "ldap_search_ext_s() for user failed";
856         return result;
857     }
858
859     /* 
860      * We should have found exactly one entry; to find a different
861      * number is an error.
862      */
863     count = ldap_count_entries(ldc->ldap, res);
864     if (count != 1) 
865     {
866         if (count == 0 )
867             ldc->reason = "User not found";
868         else
869             ldc->reason = "User is not unique (search found two or more matches)";
870         ldap_msgfree(res);
871         return LDAP_NO_SUCH_OBJECT;
872     }
873
874     entry = ldap_first_entry(ldc->ldap, res);
875
876     /* Grab the dn, copy it into the pool, and free it again */
877     dn = ldap_get_dn(ldc->ldap, entry);
878     *binddn = apr_pstrdup(r->pool, dn);
879     ldap_memfree(dn);
880
881     /* 
882      * A bind to the server with an empty password always succeeds, so
883      * we check to ensure that the password is not empty. This implies
884      * that users who actually do have empty passwords will never be
885      * able to authenticate with this module. I don't see this as a big
886      * problem.
887      */
888     if (!bindpw || strlen(bindpw) <= 0) {
889         ldap_msgfree(res);
890         ldc->reason = "Empty password not allowed";
891         return LDAP_INVALID_CREDENTIALS;
892     }
893
894     /* 
895      * Attempt to bind with the retrieved dn and the password. If the bind
896      * fails, it means that the password is wrong (the dn obviously
897      * exists, since we just retrieved it)
898      */
899     if ((result = 
900          ldap_simple_bind_s(ldc->ldap, const_cast(*binddn), const_cast(bindpw))) == 
901          LDAP_SERVER_DOWN) {
902         ldc->reason = "ldap_simple_bind_s() to check user credentials failed with server down";
903         ldap_msgfree(res);
904         util_ldap_connection_unbind(ldc);
905         goto start_over;
906     }
907
908     /* failure? if so - return */
909     if (result != LDAP_SUCCESS) {
910         ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
911         ldap_msgfree(res);
912         util_ldap_connection_unbind(ldc);
913         return result;
914     }
915     else {
916         /*
917          * We have just bound the connection to a different user and password
918          * combination, which might be reused unintentionally next time this
919          * connection is used from the connection pool. To ensure no confusion,
920          * we mark the connection as unbound.
921          */
922         ldc->bound = 0;
923     }
924
925     /*
926      * Get values for the provided attributes.
927      */
928     if (attrs) {
929         int k = 0;
930         int i = 0;
931         while (attrs[k++]);
932         vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
933         numvals = k;
934         while (attrs[i]) {
935             char **values;
936             int j = 0;
937             char *str = NULL;
938             /* get values */
939             values = ldap_get_values(ldc->ldap, entry, attrs[i]);
940             while (values && values[j]) {
941                 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) : apr_pstrdup(r->pool, values[j]);
942                 j++;
943             }
944             ldap_value_free(values);
945             vals[i] = str;
946             i++;
947         }
948         *retvals = vals;
949     }
950
951     /*          
952      * Add the new username to the search cache.
953      */
954     if (curl) {
955         LDAP_CACHE_LOCK();
956         the_search_node.username = filter;
957         the_search_node.dn = *binddn;
958         the_search_node.bindpw = bindpw;
959         the_search_node.lastbind = apr_time_now();
960         the_search_node.vals = vals;
961         the_search_node.numvals = numvals;
962
963         /* Search again to make sure that another thread didn't ready insert this node
964            into the cache before we got here. If it does exist then update the lastbind */
965         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
966         if ((search_nodep == NULL) || 
967             (strcmp(*binddn, search_nodep->dn) != 0)) {
968
969             /* Nothing in cache, insert new entry */
970             util_ald_cache_insert(curl->search_cache, &the_search_node);
971         }
972         else if ((!search_nodep->bindpw) ||
973             (strcmp(bindpw, search_nodep->bindpw) != 0)) {
974
975             /* Entry in cache is invalid, remove it and insert new one */
976             util_ald_cache_remove(curl->search_cache, search_nodep);
977             util_ald_cache_insert(curl->search_cache, &the_search_node);
978         }
979         else {
980             /* Cache entry is valid, update lastbind */
981             search_nodep->lastbind = the_search_node.lastbind;
982         }
983         LDAP_CACHE_UNLOCK();
984     }
985     ldap_msgfree(res);
986
987     ldc->reason = "Authentication successful";
988     return LDAP_SUCCESS;
989 }
990
991 /*
992  * This function will return the DN of the entry matching userid.
993  * It is used to get the DN in case some other module than mod_auth_ldap
994  * has authenticated the user.
995  * The function is basically a copy of util_ldap_cache_checkuserid
996  * with password checking removed.
997  */
998 LDAP_DECLARE(int) util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
999                               const char *url, const char *basedn, int scope, char **attrs,
1000                               const char *filter, const char **binddn,
1001                               const char ***retvals)
1002 {
1003     const char **vals = NULL;
1004     int numvals = 0;
1005     int result = 0;
1006     LDAPMessage *res, *entry;
1007     char *dn;
1008     int count;
1009     int failures = 0;
1010     util_url_node_t *curl;              /* Cached URL node */
1011     util_url_node_t curnode;
1012     util_search_node_t *search_nodep;   /* Cached search node */
1013     util_search_node_t the_search_node;
1014     apr_time_t curtime;
1015
1016     util_ldap_state_t *st = 
1017         (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
1018         &ldap_module);
1019
1020     /* Get the cache node for this url */
1021     LDAP_CACHE_LOCK();
1022     curnode.url = url;
1023     curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
1024     if (curl == NULL) {
1025         curl = util_ald_create_caches(st, url);
1026     }
1027     LDAP_CACHE_UNLOCK();
1028
1029     if (curl) {
1030         LDAP_CACHE_LOCK();
1031         the_search_node.username = filter;
1032         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
1033         if (search_nodep != NULL) {
1034     
1035             /* found entry in search cache... */
1036             curtime = apr_time_now();
1037     
1038             /*
1039              * Remove this item from the cache if its expired.
1040              */
1041             if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
1042                 /* ...but entry is too old */
1043                 util_ald_cache_remove(curl->search_cache, search_nodep);
1044             }
1045             else {
1046                 /* ...and entry is valid */
1047                 *binddn = search_nodep->dn;
1048                 *retvals = search_nodep->vals;
1049                 LDAP_CACHE_UNLOCK();
1050                 ldc->reason = "Search successful (cached)";
1051                 return LDAP_SUCCESS;
1052             }
1053         }
1054         /* unlock this read lock */
1055         LDAP_CACHE_UNLOCK();
1056     }
1057
1058     /*  
1059      * At this point, there is no valid cached search, so lets do the search.
1060      */
1061
1062     /*
1063      * If any LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
1064      */
1065 start_over:
1066     if (failures++ > 10) {
1067         return result;
1068     }
1069     if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
1070         return result;
1071     }
1072
1073     /* try do the search */
1074     if ((result = ldap_search_ext_s(ldc->ldap,
1075                                     const_cast(basedn), scope, 
1076                                     const_cast(filter), attrs, 0, 
1077                                     NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
1078         ldc->reason = "ldap_search_ext_s() for user failed with server down";
1079         util_ldap_connection_unbind(ldc);
1080         goto start_over;
1081     }
1082
1083     /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
1084     if (result != LDAP_SUCCESS) {
1085         ldc->reason = "ldap_search_ext_s() for user failed";
1086         return result;
1087     }
1088
1089     /* 
1090      * We should have found exactly one entry; to find a different
1091      * number is an error.
1092      */
1093     count = ldap_count_entries(ldc->ldap, res);
1094     if (count != 1) 
1095     {
1096         if (count == 0 )
1097             ldc->reason = "User not found";
1098         else
1099             ldc->reason = "User is not unique (search found two or more matches)";
1100         ldap_msgfree(res);
1101         return LDAP_NO_SUCH_OBJECT;
1102     }
1103
1104     entry = ldap_first_entry(ldc->ldap, res);
1105
1106     /* Grab the dn, copy it into the pool, and free it again */
1107     dn = ldap_get_dn(ldc->ldap, entry);
1108     *binddn = apr_pstrdup(r->pool, dn);
1109     ldap_memfree(dn);
1110
1111     /*
1112      * Get values for the provided attributes.
1113      */
1114     if (attrs) {
1115         int k = 0;
1116         int i = 0;
1117         while (attrs[k++]);
1118         vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
1119         numvals = k;
1120         while (attrs[i]) {
1121             char **values;
1122             int j = 0;
1123             char *str = NULL;
1124             /* get values */
1125             values = ldap_get_values(ldc->ldap, entry, attrs[i]);
1126             while (values && values[j]) {
1127                 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) : apr_pstrdup(r->pool, values[j]);
1128                 j++;
1129             }
1130             ldap_value_free(values);
1131             vals[i] = str;
1132             i++;
1133         }
1134         *retvals = vals;
1135     }
1136
1137     /*          
1138      * Add the new username to the search cache.
1139      */
1140     if (curl) {
1141         LDAP_CACHE_LOCK();
1142         the_search_node.username = filter;
1143         the_search_node.dn = *binddn;
1144         the_search_node.bindpw = NULL;
1145         the_search_node.lastbind = apr_time_now();
1146         the_search_node.vals = vals;
1147         the_search_node.numvals = numvals;
1148
1149         /* Search again to make sure that another thread didn't ready insert this node
1150            into the cache before we got here. If it does exist then update the lastbind */
1151         search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
1152         if ((search_nodep == NULL) || 
1153             (strcmp(*binddn, search_nodep->dn) != 0)) {
1154
1155             /* Nothing in cache, insert new entry */
1156             util_ald_cache_insert(curl->search_cache, &the_search_node);
1157         }
1158         /*
1159          * Don't update lastbind on entries with bindpw because
1160          * we haven't verified that password. It's OK to update
1161          * the entry if there is no password in it.
1162          */
1163         else if (!search_nodep->bindpw) {
1164             /* Cache entry is valid, update lastbind */
1165             search_nodep->lastbind = the_search_node.lastbind;
1166         }
1167         LDAP_CACHE_UNLOCK();
1168     }
1169     ldap_msgfree(res);
1170
1171     ldc->reason = "Search successful";
1172     return LDAP_SUCCESS;
1173 }
1174
1175 /*
1176  * Reports if ssl support is enabled 
1177  *
1178  * 1 = enabled, 0 = not enabled
1179  */
1180 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r)
1181 {
1182    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1183                                 r->server->module_config, &ldap_module);
1184
1185    return(st->ssl_support);
1186 }
1187
1188
1189 /* ---------------------------------------- */
1190 /* config directives */
1191
1192
1193 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, const char *bytes)
1194 {
1195     util_ldap_state_t *st = 
1196         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1197                                                   &ldap_module);
1198
1199     st->cache_bytes = atol(bytes);
1200
1201     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1202                  "[%" APR_PID_T_FMT "] ldap cache: Setting shared memory "
1203                  " cache size to %" APR_SIZE_T_FMT " bytes.", 
1204                  getpid(), st->cache_bytes);
1205
1206     return NULL;
1207 }
1208
1209 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
1210 {
1211     util_ldap_state_t *st = 
1212         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1213                                                   &ldap_module);
1214
1215     if (file) {
1216         st->cache_file = ap_server_root_relative(st->pool, file);
1217     }
1218     else {
1219         st->cache_file = NULL;
1220     }
1221
1222     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1223                  "LDAP cache: Setting shared memory cache file to %s bytes.", 
1224                  st->cache_file);
1225
1226     return NULL;
1227 }
1228
1229 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1230 {
1231     util_ldap_state_t *st = 
1232         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1233                                                   &ldap_module);
1234
1235     st->search_cache_ttl = atol(ttl) * 1000000;
1236
1237     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1238                       "[%d] ldap cache: Setting cache TTL to %ld microseconds.", 
1239                       getpid(), st->search_cache_ttl);
1240
1241     return NULL;
1242 }
1243
1244 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy, const char *size)
1245 {
1246     util_ldap_state_t *st = 
1247         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1248                                                   &ldap_module);
1249
1250
1251     st->search_cache_size = atol(size);
1252     if (st->search_cache_size < 0) {
1253         st->search_cache_size = 0;
1254     }
1255
1256     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1257                       "[%d] ldap cache: Setting search cache size to %ld entries.", 
1258                       getpid(), st->search_cache_size);
1259
1260     return NULL;
1261 }
1262
1263 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1264 {
1265     util_ldap_state_t *st = 
1266         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1267                                                   &ldap_module);
1268
1269     st->compare_cache_ttl = atol(ttl) * 1000000;
1270
1271     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1272                       "[%d] ldap cache: Setting operation cache TTL to %ld microseconds.", 
1273                       getpid(), st->compare_cache_ttl);
1274
1275     return NULL;
1276 }
1277
1278 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy, const char *size)
1279 {
1280     util_ldap_state_t *st = 
1281         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1282                                                   &ldap_module);
1283
1284     st->compare_cache_size = atol(size);
1285     if (st->compare_cache_size < 0) {
1286         st->compare_cache_size = 0;
1287     }
1288
1289     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1290                       "[%d] ldap cache: Setting operation cache size to %ld entries.", 
1291                       getpid(), st->compare_cache_size);
1292
1293     return NULL;
1294 }
1295
1296 static const char *util_ldap_set_cert_auth(cmd_parms *cmd, void *dummy, const char *file)
1297 {
1298     util_ldap_state_t *st = 
1299         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1300                                                   &ldap_module);
1301     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1302     apr_finfo_t finfo;
1303     apr_status_t rv;
1304
1305     if (err != NULL) {
1306         return err;
1307     }
1308
1309     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1310                       "LDAP: SSL trusted certificate authority file - %s", 
1311                        file);
1312
1313     st->cert_auth_file = ap_server_root_relative(cmd->pool, file);
1314
1315     if (st->cert_auth_file && 
1316         ((rv = apr_stat (&finfo, st->cert_auth_file, APR_FINFO_MIN, cmd->pool)) != APR_SUCCESS))
1317     {
1318         ap_log_error(APLOG_MARK, APLOG_ERR, rv, cmd->server, 
1319                      "LDAP: Could not open SSL trusted certificate authority file - %s", 
1320                      st->cert_auth_file == NULL ? file : st->cert_auth_file);
1321         return "Invalid file path";
1322     }
1323
1324     return(NULL);
1325 }
1326
1327
1328 static const char *util_ldap_set_cert_type(cmd_parms *cmd, void *dummy, const char *Type)
1329 {
1330     util_ldap_state_t *st = 
1331     (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1332                                               &ldap_module);
1333     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1334     if (err != NULL) {
1335         return err;
1336     }
1337
1338     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1339                       "LDAP: SSL trusted certificate authority file type - %s", 
1340                        Type);
1341
1342     if (0 == strcmp("DER_FILE", Type))
1343         st->cert_file_type = LDAP_CA_TYPE_DER;
1344
1345     else if (0 == strcmp("BASE64_FILE", Type))
1346         st->cert_file_type = LDAP_CA_TYPE_BASE64;
1347
1348     else if (0 == strcmp("CERT7_DB_PATH", Type))
1349         st->cert_file_type = LDAP_CA_TYPE_CERT7_DB;
1350
1351     else
1352         st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1353
1354     return(NULL);
1355 }
1356
1357 static const char *util_ldap_set_connection_timeout(cmd_parms *cmd, void *dummy, const char *ttl)
1358 {
1359     util_ldap_state_t *st = 
1360         (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
1361                                                   &ldap_module);
1362     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1363
1364     if (err != NULL) {
1365         return err;
1366     }
1367
1368 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1369     st->connectionTimeout = atol(ttl);
1370
1371     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
1372                       "[%d] ldap connection: Setting connection timeout to %ld seconds.", 
1373                       getpid(), st->connectionTimeout);
1374 #else
1375     ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1376                      "LDAP: Connection timout option not supported by the LDAP SDK in use." );
1377 #endif
1378
1379     return NULL;
1380 }
1381
1382 void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1383 {
1384     util_ldap_state_t *st = 
1385         (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
1386
1387     /* Create a per vhost pool for mod_ldap to use, serialized with 
1388      * st->mutex (also one per vhost) 
1389      */
1390     apr_pool_create(&st->pool, p);
1391 #if APR_HAS_THREADS
1392     apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT, st->pool);
1393 #endif
1394
1395     st->cache_bytes = 100000;
1396     st->search_cache_ttl = 600000000;
1397     st->search_cache_size = 1024;
1398     st->compare_cache_ttl = 600000000;
1399     st->compare_cache_size = 1024;
1400     st->connections = NULL;
1401     st->cert_auth_file = NULL;
1402     st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1403     st->ssl_support = 0;
1404     st->connectionTimeout = 10;
1405
1406     return st;
1407 }
1408
1409 static apr_status_t util_ldap_cleanup_module(void *data)
1410 {
1411 #if APR_HAS_LDAP_SSL && APR_HAS_NOVELL_LDAPSDK
1412     server_rec *s = data;
1413     util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1414         s->module_config, &ldap_module);
1415     
1416     if (st->ssl_support)
1417         ldapssl_client_deinit();
1418
1419 #endif
1420     return APR_SUCCESS;
1421 }
1422
1423 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, 
1424                                  apr_pool_t *ptemp, server_rec *s)
1425 {
1426     int rc = LDAP_SUCCESS;
1427     apr_status_t result;
1428     char buf[MAX_STRING_LEN];
1429     server_rec *s_vhost;
1430     util_ldap_state_t *st_vhost;
1431
1432     util_ldap_state_t *st =
1433         (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1434
1435     void *data;
1436     const char *userdata_key = "util_ldap_init";
1437
1438     /* util_ldap_post_config() will be called twice. Don't bother
1439      * going through all of the initialization on the first call
1440      * because it will just be thrown away.*/
1441     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1442     if (!data) {
1443         apr_pool_userdata_set((const void *)1, userdata_key,
1444                                apr_pool_cleanup_null, s->process->pool);
1445
1446 #if APR_HAS_SHARED_MEMORY
1447         /* If the cache file already exists then delete it.  Otherwise we are
1448          * going to run into problems creating the shared memory. */
1449         if (st->cache_file) {
1450             char *lck_file = apr_pstrcat (ptemp, st->cache_file, ".lck", NULL);
1451             apr_file_remove(st->cache_file, ptemp);
1452             apr_file_remove(lck_file, ptemp);
1453         }
1454 #endif
1455         return OK;
1456     }
1457
1458 #if APR_HAS_SHARED_MEMORY
1459     /* initializing cache if shared memory size is not zero and we already don't have shm address */
1460     if (!st->cache_shm && st->cache_bytes > 0) {
1461 #endif
1462         result = util_ldap_cache_init(p, st);
1463         if (result != APR_SUCCESS) {
1464             apr_strerror(result, buf, sizeof(buf));
1465             ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
1466                          "LDAP cache: error while creating a shared memory segment: %s", buf);
1467         }
1468
1469
1470 #if APR_HAS_SHARED_MEMORY
1471         if (st->cache_file) {
1472             st->lock_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1473         }
1474         else
1475 #endif
1476             st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1477
1478         result = apr_global_mutex_create(&st->util_ldap_cache_lock, st->lock_file, APR_LOCK_DEFAULT, st->pool);
1479         if (result != APR_SUCCESS) {
1480             return result;
1481         }
1482
1483 #ifdef UTIL_LDAP_SET_MUTEX_PERMS
1484         result = unixd_set_global_mutex_perms(st->util_ldap_cache_lock);
1485         if (result != APR_SUCCESS) {
1486             ap_log_error(APLOG_MARK, APLOG_CRIT, result, s, 
1487                          "LDAP cache: failed to set mutex permissions");
1488             return result;
1489         }
1490 #endif
1491
1492         /* merge config in all vhost */
1493         s_vhost = s->next;
1494         while (s_vhost) {
1495             st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
1496
1497 #if APR_HAS_SHARED_MEMORY
1498             st_vhost->cache_shm = st->cache_shm;
1499             st_vhost->cache_rmm = st->cache_rmm;
1500             st_vhost->cache_file = st->cache_file;
1501             ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
1502                          "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp for VHOST: %s",
1503                          st->cache_shm, st->cache_rmm, s_vhost->server_hostname);
1504 #endif
1505             st_vhost->lock_file = st->lock_file;
1506             s_vhost = s_vhost->next;
1507         }
1508 #if APR_HAS_SHARED_MEMORY
1509     }
1510     else {
1511         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "LDAP cache: LDAPSharedCacheSize is zero, disabling shared memory cache");
1512     }
1513 #endif
1514     
1515     /* log the LDAP SDK used 
1516      */
1517     #if APR_HAS_NETSCAPE_LDAPSDK 
1518     
1519         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1520              "LDAP: Built with Netscape LDAP SDK" );
1521
1522     #elif APR_HAS_NOVELL_LDAPSDK
1523
1524         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1525              "LDAP: Built with Novell LDAP SDK" );
1526
1527     #elif APR_HAS_OPENLDAP_LDAPSDK
1528
1529         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1530              "LDAP: Built with OpenLDAP LDAP SDK" );
1531
1532     #elif APR_HAS_MICROSOFT_LDAPSDK
1533     
1534         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1535              "LDAP: Built with Microsoft LDAP SDK" );
1536     #else
1537     
1538         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1539              "LDAP: Built with unknown LDAP SDK" );
1540
1541     #endif /* APR_HAS_NETSCAPE_LDAPSDK */
1542
1543
1544
1545     apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1546                               util_ldap_cleanup_module); 
1547
1548     /* initialize SSL support if requested
1549     */
1550     if (st->cert_auth_file)
1551     {
1552         #if APR_HAS_LDAP_SSL /* compiled with ssl support */
1553
1554         #if APR_HAS_NETSCAPE_LDAPSDK 
1555
1556             /* Netscape sdk only supports a cert7.db file 
1557             */
1558             if (st->cert_file_type == LDAP_CA_TYPE_CERT7_DB)
1559             {
1560                 rc = ldapssl_client_init(st->cert_auth_file, NULL);
1561             }
1562             else
1563             {
1564                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1565                          "LDAP: Invalid LDAPTrustedCAType directive - "
1566                           "CERT7_DB_PATH type required");
1567                 rc = -1;
1568             }
1569
1570         #elif APR_HAS_NOVELL_LDAPSDK
1571         
1572             /* Novell SDK supports DER or BASE64 files
1573             */
1574             if (st->cert_file_type == LDAP_CA_TYPE_DER  ||
1575                 st->cert_file_type == LDAP_CA_TYPE_BASE64 )
1576             {
1577                 rc = ldapssl_client_init(NULL, NULL);
1578                 if (LDAP_SUCCESS == rc)
1579                 {
1580                     if (st->cert_file_type == LDAP_CA_TYPE_BASE64)
1581                         rc = ldapssl_add_trusted_cert(st->cert_auth_file, 
1582                                                   LDAPSSL_CERT_FILETYPE_B64);
1583                     else
1584                         rc = ldapssl_add_trusted_cert(st->cert_auth_file, 
1585                                                   LDAPSSL_CERT_FILETYPE_DER);
1586
1587                     if (LDAP_SUCCESS != rc)
1588                         ldapssl_client_deinit();
1589                 }
1590             }
1591             else
1592             {
1593                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1594                              "LDAP: Invalid LDAPTrustedCAType directive - "
1595                              "DER_FILE or BASE64_FILE type required");
1596                 rc = -1;
1597             }
1598
1599         #elif APR_HAS_OPENLDAP_LDAPSDK
1600
1601             /* OpenLDAP SDK supports BASE64 files
1602             */
1603             if (st->cert_file_type == LDAP_CA_TYPE_BASE64)
1604             {
1605                 rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, st->cert_auth_file);
1606             }
1607             else
1608             {
1609                 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, 
1610                              "LDAP: Invalid LDAPTrustedCAType directive - "
1611                              "BASE64_FILE type required");
1612                 rc = -1;
1613             }
1614
1615
1616         #elif APR_HAS_MICROSOFT_LDAPSDK
1617             
1618             /* Microsoft SDK use the registry certificate store - always
1619              * assume support is always available
1620             */
1621             rc = LDAP_SUCCESS;
1622
1623         #else
1624             rc = -1;
1625         #endif /* APR_HAS_NETSCAPE_LDAPSDK */
1626
1627         #else  /* not compiled with SSL Support */
1628
1629             ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1630                      "LDAP: Not built with SSL support." );
1631             rc = -1;
1632
1633         #endif /* APR_HAS_LDAP_SSL */
1634
1635         if (LDAP_SUCCESS == rc)
1636         {
1637             st->ssl_support = 1;
1638         }
1639         else
1640         {
1641             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, 
1642                          "LDAP: SSL initialization failed");
1643             st->ssl_support = 0;
1644         }
1645     }
1646       
1647         /* The Microsoft SDK uses the registry certificate store -
1648          * always assume support is available
1649         */
1650     #if APR_HAS_MICROSOFT_LDAPSDK
1651         st->ssl_support = 1;
1652     #endif
1653     
1654
1655         /* log SSL status - If SSL isn't available it isn't necessarily
1656          * an error because the modules asking for LDAP connections 
1657          * may not ask for SSL support
1658         */
1659     if (st->ssl_support)
1660     {
1661        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1662                          "LDAP: SSL support available" );
1663     }
1664     else
1665     {
1666        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
1667                          "LDAP: SSL support unavailable" );
1668     }
1669     
1670     return(OK);
1671 }
1672
1673 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1674 {
1675     apr_status_t sts;
1676     util_ldap_state_t *st = ap_get_module_config(s->module_config, &ldap_module);
1677
1678     if (!st->util_ldap_cache_lock) return;
1679
1680     sts = apr_global_mutex_child_init(&st->util_ldap_cache_lock, st->lock_file, p);
1681     if (sts != APR_SUCCESS) {
1682         ap_log_error(APLOG_MARK, APLOG_CRIT, sts, s,
1683                      "Failed to initialise global mutex %s in child process %"
1684                      APR_PID_T_FMT
1685                      ".",
1686                      st->lock_file, getpid());
1687         return;
1688     }
1689     else {
1690         ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, s, 
1691                      "Initialisation of global mutex %s in child process %"
1692                      APR_PID_T_FMT
1693                      " successful.",
1694                      st->lock_file, getpid());
1695     }
1696 }
1697
1698 command_rec util_ldap_cmds[] = {
1699     AP_INIT_TAKE1("LDAPSharedCacheSize", util_ldap_set_cache_bytes, NULL, RSRC_CONF,
1700                   "Sets the size of the shared memory cache in bytes. "
1701                   "Zero means disable the shared memory cache. Defaults to 100KB."),
1702
1703     AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, NULL, RSRC_CONF,
1704                   "Sets the file of the shared memory cache."
1705                   "Nothing means disable the shared memory cache."),
1706
1707     AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, NULL, RSRC_CONF,
1708                   "Sets the maximum number of entries that are possible in the LDAP "
1709                   "search cache. "
1710                   "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1711
1712     AP_INIT_TAKE1("LDAPCacheTTL", util_ldap_set_cache_ttl, NULL, RSRC_CONF,
1713                   "Sets the maximum time (in seconds) that an item can be cached in the LDAP "
1714                   "search cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1715
1716     AP_INIT_TAKE1("LDAPOpCacheEntries", util_ldap_set_opcache_entries, NULL, RSRC_CONF,
1717                   "Sets the maximum number of entries that are possible in the LDAP "
1718                   "compare cache. "
1719                   "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1720
1721     AP_INIT_TAKE1("LDAPOpCacheTTL", util_ldap_set_opcache_ttl, NULL, RSRC_CONF,
1722                   "Sets the maximum time (in seconds) that an item is cached in the LDAP "
1723                   "operation cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1724
1725     AP_INIT_TAKE1("LDAPTrustedCA", util_ldap_set_cert_auth, NULL, RSRC_CONF,
1726                   "Sets the file containing the trusted Certificate Authority certificate. "
1727                   "Used to validate the LDAP server certificate for SSL connections."),
1728
1729     AP_INIT_TAKE1("LDAPTrustedCAType", util_ldap_set_cert_type, NULL, RSRC_CONF,
1730                  "Specifies the type of the Certificate Authority file.  "
1731                  "The following types are supported:  "
1732                  "    DER_FILE      - file in binary DER format "
1733                  "    BASE64_FILE   - file in Base64 format "
1734                  "    CERT7_DB_PATH - Netscape certificate database file "),
1735
1736     AP_INIT_TAKE1("LDAPConnectionTimeout", util_ldap_set_connection_timeout, NULL, RSRC_CONF,
1737                   "Specifies the LDAP socket connection timeout in seconds. "
1738                   "Default is 10 seconds. "),
1739
1740     {NULL}
1741 };
1742
1743 static void util_ldap_register_hooks(apr_pool_t *p)
1744 {
1745     ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1746     ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
1747     ap_hook_child_init(util_ldap_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1748 }
1749
1750 module ldap_module = {
1751    STANDARD20_MODULE_STUFF,
1752    NULL,                                /* dir config creater */
1753    NULL,                                /* dir merger --- default is to override */
1754    util_ldap_create_config,             /* server config */
1755    NULL,                                /* merge server config */
1756    util_ldap_cmds,                      /* command table */
1757    util_ldap_register_hooks,            /* set up request processing hooks */
1758 };