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