1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * util_ldap.c: LDAP things
20 * Original code from auth_ldap module for Apache v1.3:
21 * Copyright 1998, 1999 Enbridge Pipelines Inc.
22 * Copyright 1999-2001 Dave Carrigan
26 #include "http_config.h"
27 #include "http_core.h"
29 #include "http_protocol.h"
30 #include "http_request.h"
31 #include "util_ldap.h"
32 #include "util_ldap_cache.h"
34 #include <apr_strings.h>
41 #error mod_ldap requires APR-util to have LDAP support built in
44 #ifdef AP_NEED_SET_MUTEX_PERMS
48 /* defines for certificate file types
50 #define LDAP_CA_TYPE_UNKNOWN 0
51 #define LDAP_CA_TYPE_DER 1
52 #define LDAP_CA_TYPE_BASE64 2
53 #define LDAP_CA_TYPE_CERT7_DB 3
56 module AP_MODULE_DECLARE_DATA ldap_module;
58 static int util_ldap_handler(request_rec *r);
59 static void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
63 * Some definitions to help between various versions of apache.
66 #ifndef DOCTYPE_HTML_2_0
67 #define DOCTYPE_HTML_2_0 "<!DOCTYPE HTML PUBLIC \"-//IETF//" \
68 "DTD HTML 2.0//EN\">\n"
71 #ifndef DOCTYPE_HTML_3_2
72 #define DOCTYPE_HTML_3_2 "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
73 "DTD HTML 3.2 Final//EN\">\n"
76 #ifndef DOCTYPE_HTML_4_0S
77 #define DOCTYPE_HTML_4_0S "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
78 "DTD HTML 4.0//EN\"\n" \
79 "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
82 #ifndef DOCTYPE_HTML_4_0T
83 #define DOCTYPE_HTML_4_0T "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
84 "DTD HTML 4.0 Transitional//EN\"\n" \
85 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
88 #ifndef DOCTYPE_HTML_4_0F
89 #define DOCTYPE_HTML_4_0F "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
90 "DTD HTML 4.0 Frameset//EN\"\n" \
91 "\"http://www.w3.org/TR/REC-html40/frameset.dtd\">\n"
94 #define LDAP_CACHE_LOCK() \
95 if (st->util_ldap_cache_lock) \
96 apr_global_mutex_lock(st->util_ldap_cache_lock)
97 #define LDAP_CACHE_UNLOCK() \
98 if (st->util_ldap_cache_lock) \
99 apr_global_mutex_unlock(st->util_ldap_cache_lock)
102 static void util_ldap_strdup (char **str, const char *newstr)
110 *str = calloc(1, strlen(newstr)+1);
111 strcpy (*str, newstr);
119 * This handler generates a status page about the current performance of
120 * the LDAP cache. It is enabled as follows:
122 * <Location /ldap-status>
123 * SetHandler ldap-status
127 static int util_ldap_handler(request_rec *r)
129 util_ldap_state_t *st = (util_ldap_state_t *)
130 ap_get_module_config(r->server->module_config,
133 r->allowed |= (1 << M_GET);
134 if (r->method_number != M_GET)
137 if (strcmp(r->handler, "ldap-status")) {
141 r->content_type = "text/html";
145 ap_rputs(DOCTYPE_HTML_3_2
146 "<html><head><title>LDAP Cache Information</title></head>\n", r);
147 ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information"
150 util_ald_cache_display(r, st);
155 /* ------------------------------------------------------------------ */
159 * Closes an LDAP connection by unlocking it. The next time
160 * uldap_connection_find() is called this connection will be
161 * available for reuse.
163 static void uldap_connection_close(util_ldap_connection_t *ldc)
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.
173 * For now we unbind the user when we finish with a connection, but
174 * we don't have to...
177 /* mark our connection as available for reuse */
180 apr_thread_mutex_unlock(ldc->lock);
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.
190 static apr_status_t uldap_connection_unbind(void *param)
192 util_ldap_connection_t *ldc = param;
196 ldap_unbind_s(ldc->ldap);
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.
211 static apr_status_t uldap_connection_cleanup(void *param)
213 util_ldap_connection_t *ldc = param;
217 /* unbind and disconnect from the LDAP server */
218 uldap_connection_unbind(ldc);
220 /* free the username and password */
222 free((void*)ldc->bindpw);
225 free((void*)ldc->binddn);
228 /* unlock this entry */
229 uldap_connection_close(ldc);
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.
241 * Returns LDAP_SUCCESS on success; and an error code on failure
243 static int uldap_connection_open(request_rec *r,
244 util_ldap_connection_t *ldc)
248 int version = LDAP_VERSION3;
249 apr_ldap_err_t *result = NULL;
250 struct timeval timeOut = {10,0}; /* 10 second connection timeout */
251 util_ldap_state_t *st =
252 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
255 /* sanity check for NULL */
260 /* If the connection is already bound, return
264 ldc->reason = "LDAP: connection open successful (already bound)";
268 /* create the ldap session handle
270 if (NULL == ldc->ldap)
272 /* Since the host will include a port if the default port is not used,
273 * always specify the default ports for the port parameter. This will
274 * allow a host string that contains multiple hosts the ability to mix
275 * some hosts with ports and some without. All hosts which do not
276 * specify a port will use the default port.
278 apr_ldap_init(ldc->pool, &(ldc->ldap),
280 APR_LDAP_SSL == ldc->secure ? LDAPS_PORT : LDAP_PORT,
285 if (result != NULL && result->rc) {
286 ldc->reason = result->reason;
289 if (NULL == ldc->ldap)
292 if (NULL == ldc->reason) {
293 ldc->reason = "LDAP: ldap initialization failed";
296 ldc->reason = result->reason;
301 /* set client certificates */
302 if (!apr_is_empty_array(ldc->client_certs)) {
303 apr_ldap_set_option(ldc->pool, ldc->ldap, APR_LDAP_OPT_TLS_CERT,
304 ldc->client_certs, &(result));
305 if (LDAP_SUCCESS != result->rc) {
306 ldap_unbind_s(ldc->ldap);
309 ldc->reason = result->reason;
314 /* switch on SSL/TLS */
315 if (APR_LDAP_NONE != ldc->secure) {
316 apr_ldap_set_option(ldc->pool, ldc->ldap,
317 APR_LDAP_OPT_TLS, &ldc->secure, &(result));
318 if (LDAP_SUCCESS != result->rc) {
319 ldap_unbind_s(ldc->ldap);
322 ldc->reason = result->reason;
327 /* Set the alias dereferencing option */
328 ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
330 /* always default to LDAP V3 */
331 ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
333 /*XXX All of the #ifdef's need to be removed once apr-util 1.2 is released */
334 #ifdef APR_LDAP_OPT_VERIFY_CERT
335 apr_ldap_set_option(ldc->pool, ldc->ldap,
336 APR_LDAP_OPT_VERIFY_CERT, &(st->verify_svr_cert), &(result));
338 #if defined(LDAPSSL_VERIFY_SERVER)
339 if (st->verify_svr_cert) {
340 result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_SERVER);
343 result->rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_NONE);
345 #elif defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
346 /* This is not a per-connection setting so just pass NULL for the
347 Ldap connection handle */
348 if (st->verify_svr_cert) {
349 int i = LDAP_OPT_X_TLS_DEMAND;
350 result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
353 int i = LDAP_OPT_X_TLS_NEVER;
354 result->rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &i);
359 #ifdef LDAP_OPT_NETWORK_TIMEOUT
360 if (st->connectionTimeout > 0) {
361 timeOut.tv_sec = st->connectionTimeout;
364 if (st->connectionTimeout >= 0) {
365 rc = apr_ldap_set_option(ldc->pool, ldc->ldap, LDAP_OPT_NETWORK_TIMEOUT,
366 (void *)&timeOut, &(result));
367 if (APR_SUCCESS != rc) {
368 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
369 "LDAP: Could not set the connection timeout");
378 /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
379 * returned. Break out of the loop on Success or any other error.
381 * NOTE: Looping is probably not a great idea. If the server isn't
382 * responding the chances it will respond after a few tries are poor.
383 * However, the original code looped and it only happens on
384 * the error condition.
386 for (failures=0; failures<10; failures++)
388 rc = ldap_simple_bind_s(ldc->ldap,
390 (char *)ldc->bindpw);
391 if (LDAP_SERVER_DOWN != rc) {
396 /* free the handle if there was an error
398 if (LDAP_SUCCESS != rc)
400 ldap_unbind_s(ldc->ldap);
403 ldc->reason = "LDAP: ldap_simple_bind_s() failed";
407 ldc->reason = "LDAP: connection open successful";
415 * Compare client certificate arrays.
417 * Returns 1 on compare failure, 0 otherwise.
419 static int compare_client_certs(apr_array_header_t *srcs,
420 apr_array_header_t *dests)
423 struct apr_ldap_opt_tls_cert_t *src, *dest;
425 /* arrays both NULL? if so, then equal */
426 if (srcs == NULL && dests == NULL) {
430 /* arrays different length or either NULL? If so, then not equal */
431 if (srcs == NULL || dests == NULL || srcs->nelts != dests->nelts) {
435 /* run an actual comparison */
436 src = (struct apr_ldap_opt_tls_cert_t *)srcs->elts;
437 dest = (struct apr_ldap_opt_tls_cert_t *)dests->elts;
438 for (i = 0; i < srcs->nelts; i++) {
439 if (strcmp(src[i].path, dest[i].path) ||
440 strcmp(src[i].password, dest[i].password) ||
441 src[i].type != dest[i].type) {
446 /* if we got here, the cert arrays were identical */
453 * Find an existing ldap connection struct that matches the
454 * provided ldap connection parameters.
456 * If not found in the cache, a new ldc structure will be allocated
457 * from st->pool and returned to the caller. If found in the cache,
458 * a pointer to the existing ldc structure will be returned.
460 static util_ldap_connection_t *
461 uldap_connection_find(request_rec *r,
462 const char *host, int port,
463 const char *binddn, const char *bindpw,
464 deref_options deref, int secure)
466 struct util_ldap_connection_t *l, *p; /* To traverse the linked list */
467 int secureflag = secure;
469 util_ldap_state_t *st =
470 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
475 /* mutex lock this function */
477 apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT,
480 apr_thread_mutex_lock(st->mutex);
483 if (secure < APR_LDAP_NONE) {
484 secureflag = st->secure;
487 /* Search for an exact connection match in the list that is not
490 for (l=st->connections,p=NULL; l; l=l->next) {
492 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
494 if ( (l->port == port) && (strcmp(l->host, host) == 0)
495 && ((!l->binddn && !binddn) || (l->binddn && binddn
496 && !strcmp(l->binddn, binddn)))
497 && ((!l->bindpw && !bindpw) || (l->bindpw && bindpw
498 && !strcmp(l->bindpw, bindpw)))
499 && (l->deref == deref) && (l->secure == secureflag)
500 && !compare_client_certs(st->client_certs, l->client_certs))
505 /* If this connection didn't match the criteria, then we
506 * need to unlock the mutex so it is available to be reused.
508 apr_thread_mutex_unlock(l->lock);
514 /* If nothing found, search again, but we don't care about the
515 * binddn and bindpw this time.
518 for (l=st->connections,p=NULL; l; l=l->next) {
520 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
523 if ((l->port == port) && (strcmp(l->host, host) == 0) &&
524 (l->deref == deref) && (l->secure == secureflag) &&
525 !compare_client_certs(st->client_certs, l->client_certs))
527 /* the bind credentials have changed */
529 util_ldap_strdup((char**)&(l->binddn), binddn);
530 util_ldap_strdup((char**)&(l->bindpw), bindpw);
534 /* If this connection didn't match the criteria, then we
535 * need to unlock the mutex so it is available to be reused.
537 apr_thread_mutex_unlock(l->lock);
544 /* artificially disable cache */
547 /* If no connection what found after the second search, we
553 * Add the new connection entry to the linked list. Note that we
554 * don't actually establish an LDAP connection yet; that happens
555 * the first time authentication is requested.
557 /* create the details to the pool in st */
558 l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
560 apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
561 apr_thread_mutex_lock(l->lock);
565 l->host = apr_pstrdup(st->pool, host);
568 util_ldap_strdup((char**)&(l->binddn), binddn);
569 util_ldap_strdup((char**)&(l->bindpw), bindpw);
571 /* The security mode after parsing the URL will always be either
572 * APR_LDAP_NONE (ldap://) or APR_LDAP_SSL (ldaps://).
573 * If the security setting is NONE, override it to the security
574 * setting optionally supplied by the admin using LDAPTrustedMode
576 l->secure = secureflag;
578 /* save away a copy of the client cert list that is presently valid */
579 l->client_certs = apr_array_copy_hdr(l->pool, st->client_certs);
581 /* add the cleanup to the pool */
582 apr_pool_cleanup_register(l->pool, l,
583 uldap_connection_cleanup,
584 apr_pool_cleanup_null);
595 apr_thread_mutex_unlock(st->mutex);
600 /* ------------------------------------------------------------------ */
603 * Compares two DNs to see if they're equal. The only way to do this correctly
604 * is to search for the dn and then do ldap_get_dn() on the result. This should
605 * match the initial dn, since it would have been also retrieved with
606 * ldap_get_dn(). This is expensive, so if the configuration value
607 * compare_dn_on_server is false, just does an ordinary strcmp.
609 * The lock for the ldap cache should already be acquired.
611 static int uldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
612 const char *url, const char *dn,
613 const char *reqdn, int compare_dn_on_server)
616 util_url_node_t *curl;
617 util_url_node_t curnode;
618 util_dn_compare_node_t *node;
619 util_dn_compare_node_t newnode;
621 LDAPMessage *res, *entry;
624 util_ldap_state_t *st = (util_ldap_state_t *)
625 ap_get_module_config(r->server->module_config,
628 /* get cache entry (or create one) */
632 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
634 curl = util_ald_create_caches(st, url);
638 /* a simple compare? */
639 if (!compare_dn_on_server) {
640 /* unlock this read lock */
641 if (strcmp(dn, reqdn)) {
642 ldc->reason = "DN Comparison FALSE (direct strcmp())";
643 return LDAP_COMPARE_FALSE;
646 ldc->reason = "DN Comparison TRUE (direct strcmp())";
647 return LDAP_COMPARE_TRUE;
652 /* no - it's a server side compare */
655 /* is it in the compare cache? */
656 newnode.reqdn = (char *)reqdn;
657 node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
659 /* If it's in the cache, it's good */
660 /* unlock this read lock */
662 ldc->reason = "DN Comparison TRUE (cached)";
663 return LDAP_COMPARE_TRUE;
666 /* unlock this read lock */
671 if (failures++ > 10) {
672 /* too many failures */
676 /* make a server connection */
677 if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
678 /* connect to server failed */
682 /* search for reqdn */
683 if ((result = ldap_search_ext_s(ldc->ldap, (char *)reqdn, LDAP_SCOPE_BASE,
684 "(objectclass=*)", NULL, 1,
685 NULL, NULL, NULL, -1, &res))
688 ldc->reason = "DN Comparison ldap_search_ext_s() "
689 "failed with server down";
690 uldap_connection_unbind(ldc);
693 if (result != LDAP_SUCCESS) {
694 /* search for reqdn failed - no match */
695 ldc->reason = "DN Comparison ldap_search_ext_s() failed";
699 entry = ldap_first_entry(ldc->ldap, res);
700 searchdn = ldap_get_dn(ldc->ldap, entry);
703 if (strcmp(dn, searchdn) != 0) {
704 /* compare unsuccessful */
705 ldc->reason = "DN Comparison FALSE (checked on server)";
706 result = LDAP_COMPARE_FALSE;
710 /* compare successful - add to the compare cache */
712 newnode.reqdn = (char *)reqdn;
713 newnode.dn = (char *)dn;
715 node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
717 || (strcmp(reqdn, node->reqdn) != 0)
718 || (strcmp(dn, node->dn) != 0))
720 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
724 ldc->reason = "DN Comparison TRUE (checked on server)";
725 result = LDAP_COMPARE_TRUE;
727 ldap_memfree(searchdn);
733 * Does an generic ldap_compare operation. It accepts a cache that it will use
734 * to lookup the compare in the cache. We cache two kinds of compares
735 * (require group compares) and (require user compares). Each compare has a different
736 * cache node: require group includes the DN; require user does not because the
737 * require user cache is owned by the
740 static int uldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
741 const char *url, const char *dn,
742 const char *attrib, const char *value)
745 util_url_node_t *curl;
746 util_url_node_t curnode;
747 util_compare_node_t *compare_nodep;
748 util_compare_node_t the_compare_node;
749 apr_time_t curtime = 0; /* silence gcc -Wall */
752 util_ldap_state_t *st = (util_ldap_state_t *)
753 ap_get_module_config(r->server->module_config,
756 /* get cache entry (or create one) */
759 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
761 curl = util_ald_create_caches(st, url);
766 /* make a comparison to the cache */
768 curtime = apr_time_now();
770 the_compare_node.dn = (char *)dn;
771 the_compare_node.attrib = (char *)attrib;
772 the_compare_node.value = (char *)value;
773 the_compare_node.result = 0;
775 compare_nodep = util_ald_cache_fetch(curl->compare_cache,
778 if (compare_nodep != NULL) {
780 if (curtime - compare_nodep->lastcompare > st->compare_cache_ttl) {
781 /* ...but it is too old */
782 util_ald_cache_remove(curl->compare_cache, compare_nodep);
785 /* ...and it is good */
786 /* unlock this read lock */
788 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
789 ldc->reason = "Comparison true (cached)";
790 return compare_nodep->result;
792 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
793 ldc->reason = "Comparison false (cached)";
794 return compare_nodep->result;
796 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
797 ldc->reason = "Comparison no such attribute (cached)";
798 return compare_nodep->result;
801 ldc->reason = "Comparison undefined (cached)";
802 return compare_nodep->result;
806 /* unlock this read lock */
811 if (failures++ > 10) {
812 /* too many failures */
815 if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
820 if ((result = ldap_compare_s(ldc->ldap,
824 == LDAP_SERVER_DOWN) {
825 /* connection failed - try again */
826 ldc->reason = "ldap_compare_s() failed with server down";
827 uldap_connection_unbind(ldc);
831 ldc->reason = "Comparison complete";
832 if ((LDAP_COMPARE_TRUE == result) ||
833 (LDAP_COMPARE_FALSE == result) ||
834 (LDAP_NO_SUCH_ATTRIBUTE == result)) {
836 /* compare completed; caching result */
838 the_compare_node.lastcompare = curtime;
839 the_compare_node.result = result;
841 /* If the node doesn't exist then insert it, otherwise just update
842 * it with the last results
844 compare_nodep = util_ald_cache_fetch(curl->compare_cache,
846 if ( (compare_nodep == NULL)
847 || (strcmp(the_compare_node.dn, compare_nodep->dn) != 0)
848 || (strcmp(the_compare_node.attrib,compare_nodep->attrib) != 0)
849 || (strcmp(the_compare_node.value, compare_nodep->value) != 0))
851 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
854 compare_nodep->lastcompare = curtime;
855 compare_nodep->result = result;
859 if (LDAP_COMPARE_TRUE == result) {
860 ldc->reason = "Comparison true (adding to cache)";
861 return LDAP_COMPARE_TRUE;
863 else if (LDAP_COMPARE_FALSE == result) {
864 ldc->reason = "Comparison false (adding to cache)";
865 return LDAP_COMPARE_FALSE;
868 ldc->reason = "Comparison no such attribute (adding to cache)";
869 return LDAP_NO_SUCH_ATTRIBUTE;
875 static int uldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
876 const char *url, const char *basedn,
877 int scope, char **attrs, const char *filter,
878 const char *bindpw, const char **binddn,
879 const char ***retvals)
881 const char **vals = NULL;
884 LDAPMessage *res, *entry;
888 util_url_node_t *curl; /* Cached URL node */
889 util_url_node_t curnode;
890 util_search_node_t *search_nodep; /* Cached search node */
891 util_search_node_t the_search_node;
894 util_ldap_state_t *st =
895 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
898 /* Get the cache node for this url */
901 curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache,
904 curl = util_ald_create_caches(st, url);
910 the_search_node.username = filter;
911 search_nodep = util_ald_cache_fetch(curl->search_cache,
913 if (search_nodep != NULL) {
915 /* found entry in search cache... */
916 curtime = apr_time_now();
919 * Remove this item from the cache if its expired. If the sent
920 * password doesn't match the storepassword, the entry will
921 * be removed and readded later if the credentials pass
924 if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
925 /* ...but entry is too old */
926 util_ald_cache_remove(curl->search_cache, search_nodep);
928 else if ( (search_nodep->bindpw)
929 && (search_nodep->bindpw[0] != '\0')
930 && (strcmp(search_nodep->bindpw, bindpw) == 0))
932 /* ...and entry is valid */
933 *binddn = search_nodep->dn;
934 *retvals = search_nodep->vals;
936 ldc->reason = "Authentication successful (cached)";
940 /* unlock this read lock */
945 * At this point, there is no valid cached search, so lets do the search.
949 * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
952 if (failures++ > 10) {
955 if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
959 /* try do the search */
960 if ((result = ldap_search_ext_s(ldc->ldap,
961 (char *)basedn, scope,
962 (char *)filter, attrs, 0,
963 NULL, NULL, NULL, -1, &res))
966 ldc->reason = "ldap_search_ext_s() for user failed with server down";
967 uldap_connection_unbind(ldc);
971 /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
972 if (result != LDAP_SUCCESS) {
973 ldc->reason = "ldap_search_ext_s() for user failed";
978 * We should have found exactly one entry; to find a different
979 * number is an error.
981 count = ldap_count_entries(ldc->ldap, res);
985 ldc->reason = "User not found";
987 ldc->reason = "User is not unique (search found two "
990 return LDAP_NO_SUCH_OBJECT;
993 entry = ldap_first_entry(ldc->ldap, res);
995 /* Grab the dn, copy it into the pool, and free it again */
996 dn = ldap_get_dn(ldc->ldap, entry);
997 *binddn = apr_pstrdup(r->pool, dn);
1001 * A bind to the server with an empty password always succeeds, so
1002 * we check to ensure that the password is not empty. This implies
1003 * that users who actually do have empty passwords will never be
1004 * able to authenticate with this module. I don't see this as a big
1007 if (!bindpw || strlen(bindpw) <= 0) {
1009 ldc->reason = "Empty password not allowed";
1010 return LDAP_INVALID_CREDENTIALS;
1014 * Attempt to bind with the retrieved dn and the password. If the bind
1015 * fails, it means that the password is wrong (the dn obviously
1016 * exists, since we just retrieved it)
1018 if ((result = ldap_simple_bind_s(ldc->ldap,
1020 (char *)bindpw)) == LDAP_SERVER_DOWN) {
1021 ldc->reason = "ldap_simple_bind_s() to check user credentials "
1022 "failed with server down";
1024 uldap_connection_unbind(ldc);
1028 /* failure? if so - return */
1029 if (result != LDAP_SUCCESS) {
1030 ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
1032 uldap_connection_unbind(ldc);
1037 * We have just bound the connection to a different user and password
1038 * combination, which might be reused unintentionally next time this
1039 * connection is used from the connection pool. To ensure no confusion,
1040 * we mark the connection as unbound.
1046 * Get values for the provided attributes.
1052 vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
1059 values = ldap_get_values(ldc->ldap, entry, attrs[i]);
1060 while (values && values[j]) {
1061 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL)
1062 : apr_pstrdup(r->pool, values[j]);
1065 ldap_value_free(values);
1073 * Add the new username to the search cache.
1077 the_search_node.username = filter;
1078 the_search_node.dn = *binddn;
1079 the_search_node.bindpw = bindpw;
1080 the_search_node.lastbind = apr_time_now();
1081 the_search_node.vals = vals;
1082 the_search_node.numvals = numvals;
1084 /* Search again to make sure that another thread didn't ready insert
1085 * this node into the cache before we got here. If it does exist then
1086 * update the lastbind
1088 search_nodep = util_ald_cache_fetch(curl->search_cache,
1090 if ((search_nodep == NULL) ||
1091 (strcmp(*binddn, search_nodep->dn) != 0)) {
1093 /* Nothing in cache, insert new entry */
1094 util_ald_cache_insert(curl->search_cache, &the_search_node);
1096 else if ((!search_nodep->bindpw) ||
1097 (strcmp(bindpw, search_nodep->bindpw) != 0)) {
1099 /* Entry in cache is invalid, remove it and insert new one */
1100 util_ald_cache_remove(curl->search_cache, search_nodep);
1101 util_ald_cache_insert(curl->search_cache, &the_search_node);
1104 /* Cache entry is valid, update lastbind */
1105 search_nodep->lastbind = the_search_node.lastbind;
1107 LDAP_CACHE_UNLOCK();
1111 ldc->reason = "Authentication successful";
1112 return LDAP_SUCCESS;
1116 * This function will return the DN of the entry matching userid.
1117 * It is used to get the DN in case some other module than mod_auth_ldap
1118 * has authenticated the user.
1119 * The function is basically a copy of uldap_cache_checkuserid
1120 * with password checking removed.
1122 static int uldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
1123 const char *url, const char *basedn,
1124 int scope, char **attrs, const char *filter,
1125 const char **binddn, const char ***retvals)
1127 const char **vals = NULL;
1130 LDAPMessage *res, *entry;
1134 util_url_node_t *curl; /* Cached URL node */
1135 util_url_node_t curnode;
1136 util_search_node_t *search_nodep; /* Cached search node */
1137 util_search_node_t the_search_node;
1140 util_ldap_state_t *st =
1141 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
1144 /* Get the cache node for this url */
1147 curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache,
1150 curl = util_ald_create_caches(st, url);
1152 LDAP_CACHE_UNLOCK();
1156 the_search_node.username = filter;
1157 search_nodep = util_ald_cache_fetch(curl->search_cache,
1159 if (search_nodep != NULL) {
1161 /* found entry in search cache... */
1162 curtime = apr_time_now();
1165 * Remove this item from the cache if its expired.
1167 if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
1168 /* ...but entry is too old */
1169 util_ald_cache_remove(curl->search_cache, search_nodep);
1172 /* ...and entry is valid */
1173 *binddn = search_nodep->dn;
1174 *retvals = search_nodep->vals;
1175 LDAP_CACHE_UNLOCK();
1176 ldc->reason = "Search successful (cached)";
1177 return LDAP_SUCCESS;
1180 /* unlock this read lock */
1181 LDAP_CACHE_UNLOCK();
1185 * At this point, there is no valid cached search, so lets do the search.
1189 * If LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
1192 if (failures++ > 10) {
1195 if (LDAP_SUCCESS != (result = uldap_connection_open(r, ldc))) {
1199 /* try do the search */
1200 if ((result = ldap_search_ext_s(ldc->ldap,
1201 (char *)basedn, scope,
1202 (char *)filter, attrs, 0,
1203 NULL, NULL, NULL, -1, &res))
1204 == LDAP_SERVER_DOWN)
1206 ldc->reason = "ldap_search_ext_s() for user failed with server down";
1207 uldap_connection_unbind(ldc);
1211 /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
1212 if (result != LDAP_SUCCESS) {
1213 ldc->reason = "ldap_search_ext_s() for user failed";
1218 * We should have found exactly one entry; to find a different
1219 * number is an error.
1221 count = ldap_count_entries(ldc->ldap, res);
1225 ldc->reason = "User not found";
1227 ldc->reason = "User is not unique (search found two "
1230 return LDAP_NO_SUCH_OBJECT;
1233 entry = ldap_first_entry(ldc->ldap, res);
1235 /* Grab the dn, copy it into the pool, and free it again */
1236 dn = ldap_get_dn(ldc->ldap, entry);
1237 *binddn = apr_pstrdup(st->pool, dn);
1241 * Get values for the provided attributes.
1247 vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
1254 values = ldap_get_values(ldc->ldap, entry, attrs[i]);
1255 while (values && values[j]) {
1256 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL)
1257 : apr_pstrdup(r->pool, values[j]);
1260 ldap_value_free(values);
1268 * Add the new username to the search cache.
1272 the_search_node.username = filter;
1273 the_search_node.dn = *binddn;
1274 the_search_node.bindpw = NULL;
1275 the_search_node.lastbind = apr_time_now();
1276 the_search_node.vals = vals;
1277 the_search_node.numvals = numvals;
1279 /* Search again to make sure that another thread didn't ready insert
1280 * this node into the cache before we got here. If it does exist then
1281 * update the lastbind
1283 search_nodep = util_ald_cache_fetch(curl->search_cache,
1285 if ((search_nodep == NULL) ||
1286 (strcmp(*binddn, search_nodep->dn) != 0)) {
1288 /* Nothing in cache, insert new entry */
1289 util_ald_cache_insert(curl->search_cache, &the_search_node);
1292 * Don't update lastbind on entries with bindpw because
1293 * we haven't verified that password. It's OK to update
1294 * the entry if there is no password in it.
1296 else if (!search_nodep->bindpw) {
1297 /* Cache entry is valid, update lastbind */
1298 search_nodep->lastbind = the_search_node.lastbind;
1300 LDAP_CACHE_UNLOCK();
1305 ldc->reason = "Search successful";
1306 return LDAP_SUCCESS;
1310 * Reports if ssl support is enabled
1312 * 1 = enabled, 0 = not enabled
1314 static int uldap_ssl_supported(request_rec *r)
1316 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1317 r->server->module_config, &ldap_module);
1319 return(st->ssl_supported);
1323 /* ---------------------------------------- */
1324 /* config directives */
1327 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy,
1330 util_ldap_state_t *st =
1331 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1334 st->cache_bytes = atol(bytes);
1336 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1337 "[%" APR_PID_T_FMT "] ldap cache: Setting shared memory "
1338 " cache size to %" APR_SIZE_T_FMT " bytes.",
1339 getpid(), st->cache_bytes);
1344 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy,
1347 util_ldap_state_t *st =
1348 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1352 st->cache_file = ap_server_root_relative(st->pool, file);
1355 st->cache_file = NULL;
1358 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1359 "LDAP cache: Setting shared memory cache file to %s bytes.",
1365 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy,
1368 util_ldap_state_t *st =
1369 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1372 st->search_cache_ttl = atol(ttl) * 1000000;
1374 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1375 "[%d] ldap cache: Setting cache TTL to %ld microseconds.",
1376 getpid(), st->search_cache_ttl);
1381 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy,
1384 util_ldap_state_t *st =
1385 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1389 st->search_cache_size = atol(size);
1390 if (st->search_cache_size < 0) {
1391 st->search_cache_size = 0;
1394 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1395 "[%d] ldap cache: Setting search cache size to %ld entries.",
1396 getpid(), st->search_cache_size);
1401 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy,
1404 util_ldap_state_t *st =
1405 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1408 st->compare_cache_ttl = atol(ttl) * 1000000;
1410 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1411 "[%d] ldap cache: Setting operation cache TTL to %ld microseconds.",
1412 getpid(), st->compare_cache_ttl);
1417 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy,
1420 util_ldap_state_t *st =
1421 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1424 st->compare_cache_size = atol(size);
1425 if (st->compare_cache_size < 0) {
1426 st->compare_cache_size = 0;
1429 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1430 "[%d] ldap cache: Setting operation cache size to %ld "
1431 "entries.", getpid(), st->compare_cache_size);
1438 * Parse the certificate type.
1440 * The type can be one of the following:
1441 * CA_DER, CA_BASE64, CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64,
1442 * CERT_KEY3_DB, CERT_NICKNAME, KEY_DER, KEY_BASE64
1444 * If no matches are found, APR_LDAP_CA_TYPE_UNKNOWN is returned.
1446 static const int util_ldap_parse_cert_type(const char *type) {
1448 /* Authority file in binary DER format */
1449 if (0 == strcasecmp("CA_DER", type)) {
1450 return APR_LDAP_CA_TYPE_DER;
1453 /* Authority file in Base64 format */
1454 else if (0 == strcasecmp("CA_BASE64", type)) {
1455 return APR_LDAP_CA_TYPE_BASE64;
1458 /* Netscape certificate database file/directory */
1459 else if (0 == strcasecmp("CA_CERT7_DB", type)) {
1460 return APR_LDAP_CA_TYPE_CERT7_DB;
1463 /* Netscape secmod file/directory */
1464 else if (0 == strcasecmp("CA_SECMOD", type)) {
1465 return APR_LDAP_CA_TYPE_SECMOD;
1468 /* Client cert file in DER format */
1469 else if (0 == strcasecmp("CERT_DER", type)) {
1470 return APR_LDAP_CERT_TYPE_DER;
1473 /* Client cert file in Base64 format */
1474 else if (0 == strcasecmp("CERT_BASE64", type)) {
1475 return APR_LDAP_CERT_TYPE_BASE64;
1478 /* Client cert file in PKCS#12 format */
1479 else if (0 == strcasecmp("CERT_PFX", type)) {
1480 return APR_LDAP_CERT_TYPE_PFX;
1483 /* Netscape client cert database file/directory */
1484 else if (0 == strcasecmp("CERT_KEY3_DB", type)) {
1485 return APR_LDAP_CERT_TYPE_KEY3_DB;
1488 /* Netscape client cert nickname */
1489 else if (0 == strcasecmp("CERT_NICKNAME", type)) {
1490 return APR_LDAP_CERT_TYPE_NICKNAME;
1493 /* Client cert key file in DER format */
1494 else if (0 == strcasecmp("KEY_DER", type)) {
1495 return APR_LDAP_KEY_TYPE_DER;
1498 /* Client cert key file in Base64 format */
1499 else if (0 == strcasecmp("KEY_BASE64", type)) {
1500 return APR_LDAP_KEY_TYPE_BASE64;
1503 /* Client cert key file in PKCS#12 format */
1504 else if (0 == strcasecmp("KEY_PFX", type)) {
1505 return APR_LDAP_KEY_TYPE_PFX;
1509 return APR_LDAP_CA_TYPE_UNKNOWN;
1516 * Set LDAPTrustedGlobalCert.
1518 * This directive takes either two or three arguments:
1519 * - certificate type
1520 * - certificate file / directory / nickname
1521 * - certificate password (optional)
1523 * This directive may only be used globally.
1525 static const char *util_ldap_set_trusted_global_cert(cmd_parms *cmd,
1529 const char *password)
1531 util_ldap_state_t *st =
1532 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1534 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1538 apr_ldap_opt_tls_cert_t *cert;
1544 /* handle the certificate type */
1546 cert_type = util_ldap_parse_cert_type(type);
1547 if (APR_LDAP_CA_TYPE_UNKNOWN == cert_type) {
1548 return apr_psprintf(cmd->pool, "The certificate type %s is "
1549 "not recognised. It should be one "
1550 "of CA_DER, CA_BASE64, CA_CERT7_DB, "
1551 "CA_SECMOD, CERT_DER, CERT_BASE64, "
1552 "CERT_KEY3_DB, CERT_NICKNAME, "
1553 "KEY_DER, KEY_BASE64", type);
1557 return "Certificate type was not specified.";
1560 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1561 "LDAP: SSL trusted global cert - %s (type %s)",
1564 /* add the certificate to the global array */
1565 cert = (apr_ldap_opt_tls_cert_t *)apr_array_push(st->global_certs);
1566 cert->type = cert_type;
1568 cert->password = password;
1570 /* if file is a file or path, fix the path */
1571 if (cert_type != APR_LDAP_CA_TYPE_UNKNOWN &&
1572 cert_type != APR_LDAP_CERT_TYPE_NICKNAME) {
1574 cert->path = ap_server_root_relative(cmd->pool, file);
1576 ((rv = apr_stat (&finfo, cert->path, APR_FINFO_MIN, cmd->pool))
1579 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cmd->server,
1580 "LDAP: Could not open SSL trusted certificate "
1581 "authority file - %s",
1582 cert->path == NULL ? file : cert->path);
1583 return "Invalid global certificate file path";
1592 * Set LDAPTrustedClientCert.
1594 * This directive takes either two or three arguments:
1595 * - certificate type
1596 * - certificate file / directory / nickname
1597 * - certificate password (optional)
1599 static const char *util_ldap_set_trusted_client_cert(cmd_parms *cmd,
1603 const char *password)
1605 util_ldap_state_t *st =
1606 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1611 apr_ldap_opt_tls_cert_t *cert;
1613 /* handle the certificate type */
1615 cert_type = util_ldap_parse_cert_type(type);
1616 if (APR_LDAP_CA_TYPE_UNKNOWN == cert_type) {
1617 return apr_psprintf(cmd->pool, "The certificate type \"%s\" is "
1618 "not recognised. It should be one "
1619 "of CERT_DER, CERT_BASE64, "
1620 "CERT_NICKNAME, CERT_PFX,"
1621 "KEY_DER, KEY_BASE64, KEY_PFX",
1624 else if (APR_LDAP_CA_TYPE_DER == cert_type ||
1625 APR_LDAP_CA_TYPE_BASE64 == cert_type ||
1626 APR_LDAP_CA_TYPE_CERT7_DB == cert_type ||
1627 APR_LDAP_CA_TYPE_SECMOD == cert_type ||
1628 APR_LDAP_CERT_TYPE_PFX == cert_type ||
1629 APR_LDAP_CERT_TYPE_KEY3_DB == cert_type) {
1630 return apr_psprintf(cmd->pool, "The certificate type \"%s\" is "
1631 "only valid within a "
1632 "LDAPTrustedGlobalCert directive. "
1633 "Only CERT_DER, CERT_BASE64, "
1634 "CERT_NICKNAME, KEY_DER, and "
1635 "KEY_BASE64 may be used.", type);
1639 return "Certificate type was not specified.";
1642 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1643 "LDAP: SSL trusted client cert - %s (type %s)",
1646 /* add the certificate to the global array */
1647 cert = (apr_ldap_opt_tls_cert_t *)apr_array_push(st->global_certs);
1648 cert->type = cert_type;
1650 cert->password = password;
1652 /* if file is a file or path, fix the path */
1653 if (cert_type != APR_LDAP_CA_TYPE_UNKNOWN &&
1654 cert_type != APR_LDAP_CERT_TYPE_NICKNAME) {
1656 cert->path = ap_server_root_relative(cmd->pool, file);
1658 ((rv = apr_stat (&finfo, cert->path, APR_FINFO_MIN, cmd->pool))
1661 ap_log_error(APLOG_MARK, APLOG_ERR, rv, cmd->server,
1662 "LDAP: Could not open SSL client certificate "
1664 cert->path == NULL ? file : cert->path);
1665 return "Invalid client certificate file path";
1675 * Set LDAPTrustedMode.
1677 * This directive sets what encryption mode to use on a connection:
1678 * - None (No encryption)
1679 * - SSL (SSL encryption)
1680 * - STARTTLS (TLS encryption)
1682 static const char *util_ldap_set_trusted_mode(cmd_parms *cmd, void *dummy,
1685 util_ldap_state_t *st =
1686 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1689 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1690 "LDAP: SSL trusted mode - %s",
1693 if (0 == strcasecmp("NONE", mode)) {
1694 st->secure = APR_LDAP_NONE;
1696 else if (0 == strcasecmp("SSL", mode)) {
1697 st->secure = APR_LDAP_SSL;
1699 else if ( (0 == strcasecmp("TLS", mode))
1700 || (0 == strcasecmp("STARTTLS", mode))) {
1701 st->secure = APR_LDAP_STARTTLS;
1704 return "Invalid LDAPTrustedMode setting: must be one of NONE, "
1705 "SSL, or TLS/STARTTLS";
1712 static const char *util_ldap_set_verify_srv_cert(cmd_parms *cmd,
1716 util_ldap_state_t *st =
1717 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1720 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1721 "LDAP: SSL verify server certificate - %s",
1722 mode?"TRUE":"FALSE");
1724 st->verify_svr_cert = mode;
1730 static const char *util_ldap_set_connection_timeout(cmd_parms *cmd,
1734 util_ldap_state_t *st =
1735 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1737 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1743 #ifdef LDAP_OPT_NETWORK_TIMEOUT
1744 st->connectionTimeout = atol(ttl);
1746 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1747 "[%d] ldap connection: Setting connection timeout to "
1748 "%ld seconds.", getpid(), st->connectionTimeout);
1750 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1751 "LDAP: Connection timout option not supported by the "
1752 "LDAP SDK in use." );
1759 static void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1761 util_ldap_state_t *st =
1762 (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
1766 st->cache_bytes = 100000;
1767 st->search_cache_ttl = 600000000;
1768 st->search_cache_size = 1024;
1769 st->compare_cache_ttl = 600000000;
1770 st->compare_cache_size = 1024;
1771 st->connections = NULL;
1772 st->ssl_supported = 0;
1773 st->global_certs = apr_array_make(p, 10, sizeof(apr_ldap_opt_tls_cert_t));
1774 st->client_certs = apr_array_make(p, 10, sizeof(apr_ldap_opt_tls_cert_t));
1775 st->secure = APR_LDAP_NONE;
1777 st->connectionTimeout = 10;
1778 st->verify_svr_cert = 1;
1783 static void *util_ldap_merge_config(apr_pool_t *p, void *basev,
1786 util_ldap_state_t *st = apr_pcalloc(p, sizeof(util_ldap_state_t));
1787 util_ldap_state_t *base = (util_ldap_state_t *) basev;
1788 util_ldap_state_t *overrides = (util_ldap_state_t *) overridesv;
1792 st->cache_bytes = base->cache_bytes;
1793 st->search_cache_ttl = base->search_cache_ttl;
1794 st->search_cache_size = base->search_cache_size;
1795 st->compare_cache_ttl = base->compare_cache_ttl;
1796 st->compare_cache_size = base->compare_cache_size;
1797 st->connections = base->connections;
1798 st->ssl_supported = base->ssl_supported;
1799 st->global_certs = apr_array_append(p, base->global_certs,
1800 overrides->global_certs);
1801 st->client_certs = apr_array_append(p, base->client_certs,
1802 overrides->client_certs);
1803 st->secure = (overrides->secure_set == 0) ? base->secure
1804 : overrides->secure;
1809 static apr_status_t util_ldap_cleanup_module(void *data)
1812 server_rec *s = data;
1813 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1814 s->module_config, &ldap_module);
1816 if (st->ssl_supported) {
1817 apr_ldap_ssl_deinit();
1824 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog,
1825 apr_pool_t *ptemp, server_rec *s)
1827 apr_status_t result;
1828 char buf[MAX_STRING_LEN];
1829 server_rec *s_vhost;
1830 util_ldap_state_t *st_vhost;
1832 util_ldap_state_t *st = (util_ldap_state_t *)
1833 ap_get_module_config(s->module_config,
1837 const char *userdata_key = "util_ldap_init";
1838 apr_ldap_err_t *result_err = NULL;
1841 /* util_ldap_post_config() will be called twice. Don't bother
1842 * going through all of the initialization on the first call
1843 * because it will just be thrown away.*/
1844 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1846 apr_pool_userdata_set((const void *)1, userdata_key,
1847 apr_pool_cleanup_null, s->process->pool);
1849 #if APR_HAS_SHARED_MEMORY
1850 /* If the cache file already exists then delete it. Otherwise we are
1851 * going to run into problems creating the shared memory. */
1852 if (st->cache_file) {
1853 char *lck_file = apr_pstrcat(st->pool, st->cache_file, ".lck",
1855 apr_file_remove(st->cache_file, ptemp);
1856 apr_file_remove(lck_file, ptemp);
1862 #if APR_HAS_SHARED_MEMORY
1863 /* initializing cache if shared memory size is not zero and we already
1864 * don't have shm address
1866 if (!st->cache_shm && st->cache_bytes > 0) {
1868 result = util_ldap_cache_init(p, st);
1869 if (result != APR_SUCCESS) {
1870 apr_strerror(result, buf, sizeof(buf));
1871 ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
1872 "LDAP cache: error while creating a shared memory "
1873 "segment: %s", buf);
1877 #if APR_HAS_SHARED_MEMORY
1878 if (st->cache_file) {
1879 st->lock_file = apr_pstrcat(st->pool, st->cache_file, ".lck",
1884 st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1886 result = apr_global_mutex_create(&st->util_ldap_cache_lock,
1887 st->lock_file, APR_LOCK_DEFAULT,
1889 if (result != APR_SUCCESS) {
1893 #ifdef AP_NEED_SET_MUTEX_PERMS
1894 result = unixd_set_global_mutex_perms(st->util_ldap_cache_lock);
1895 if (result != APR_SUCCESS) {
1896 ap_log_error(APLOG_MARK, APLOG_CRIT, result, s,
1897 "LDAP cache: failed to set mutex permissions");
1902 /* merge config in all vhost */
1905 st_vhost = (util_ldap_state_t *)
1906 ap_get_module_config(s_vhost->module_config,
1909 #if APR_HAS_SHARED_MEMORY
1910 st_vhost->cache_shm = st->cache_shm;
1911 st_vhost->cache_rmm = st->cache_rmm;
1912 st_vhost->cache_file = st->cache_file;
1913 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s,
1914 "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp "
1915 "for VHOST: %s", st->cache_shm, st->cache_rmm,
1916 s_vhost->server_hostname);
1918 st_vhost->lock_file = st->lock_file;
1919 s_vhost = s_vhost->next;
1921 #if APR_HAS_SHARED_MEMORY
1924 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1925 "LDAP cache: LDAPSharedCacheSize is zero, disabling "
1926 "shared memory cache");
1930 /* log the LDAP SDK used
1933 apr_ldap_err_t *result = NULL;
1934 apr_ldap_info(p, &(result));
1935 if (result != NULL) {
1936 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "%s", result->reason);
1940 apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1941 util_ldap_cleanup_module);
1944 * Initialize SSL support, and log the result for the benefit of the admin.
1946 * If SSL is not supported it is not necessarily an error, as the
1947 * application may not want to use it.
1949 rc = apr_ldap_ssl_init(p,
1953 if (APR_SUCCESS == rc) {
1954 rc = apr_ldap_set_option(p, NULL, APR_LDAP_OPT_TLS_CERT,
1955 (void *)st->global_certs, &(result_err));
1958 if (APR_SUCCESS == rc) {
1959 st->ssl_supported = 1;
1960 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1961 "LDAP: SSL support available" );
1964 st->ssl_supported = 0;
1965 if (NULL != result_err) {
1966 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, "%s",
1967 result_err->reason);
1969 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1970 "LDAP: SSL support unavailable" );
1976 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1979 util_ldap_state_t *st = ap_get_module_config(s->module_config,
1982 if (!st->util_ldap_cache_lock) return;
1984 sts = apr_global_mutex_child_init(&st->util_ldap_cache_lock,
1986 if (sts != APR_SUCCESS) {
1987 ap_log_error(APLOG_MARK, APLOG_CRIT, sts, s,
1988 "Failed to initialise global mutex %s in child process %"
1990 st->lock_file, getpid());
1994 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, s,
1995 "Initialisation of global mutex %s in child process %"
1998 st->lock_file, getpid());
2002 command_rec util_ldap_cmds[] = {
2003 AP_INIT_TAKE1("LDAPSharedCacheSize", util_ldap_set_cache_bytes,
2005 "Set the size of the shared memory cache (in bytes). Use "
2006 "0 to disable the shared memory cache. (default: 100000)"),
2008 AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file,
2010 "Set the file name for the shared memory cache."),
2012 AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries,
2014 "Set the maximum number of entries that are possible in the "
2015 "LDAP search cache. Use 0 for no limit. "
2016 "-1 disables the cache. (default: 1024)"),
2018 AP_INIT_TAKE1("LDAPCacheTTL", util_ldap_set_cache_ttl,
2020 "Set the maximum time (in seconds) that an item can be "
2021 "cached in the LDAP search cache. Use 0 for no limit. "
2024 AP_INIT_TAKE1("LDAPOpCacheEntries", util_ldap_set_opcache_entries,
2026 "Set the maximum number of entries that are possible "
2027 "in the LDAP compare cache. Use 0 for no limit. "
2028 "Use -1 to disable the cache. (default: 1024)"),
2030 AP_INIT_TAKE1("LDAPOpCacheTTL", util_ldap_set_opcache_ttl,
2032 "Set the maximum time (in seconds) that an item is cached "
2033 "in the LDAP operation cache. Use 0 for no limit. "
2036 AP_INIT_TAKE23("LDAPTrustedGlobalCert", util_ldap_set_trusted_global_cert,
2038 "Takes three args; the file and/or directory containing "
2039 "the trusted CA certificates (and global client certs "
2040 "for Netware) used to validate the LDAP server. Second "
2041 "arg is the cert type for the first arg, one of CA_DER, "
2042 "CA_BASE64, CA_CERT7_DB, CA_SECMOD, CERT_DER, CERT_BASE64, "
2043 "CERT_KEY3_DB, CERT_NICKNAME, KEY_DER, or KEY_BASE64. "
2044 "Third arg is an optional passphrase if applicable."),
2046 AP_INIT_TAKE23("LDAPTrustedClientCert", util_ldap_set_trusted_client_cert,
2048 "Takes three args; the file and/or directory containing "
2049 "the client certificate, or certificate ID used to "
2050 "validate this LDAP client. Second arg is the cert type "
2051 "for the first arg, one of CA_DER, CA_BASE64, CA_CERT7_DB, "
2052 "CA_SECMOD, CERT_DER, CERT_BASE64, CERT_KEY3_DB, "
2053 "CERT_NICKNAME, KEY_DER, or KEY_BASE64. Third arg is an "
2054 "optional passphrase if applicable."),
2056 AP_INIT_TAKE1("LDAPTrustedMode", util_ldap_set_trusted_mode,
2058 "Specify the type of security that should be applied to "
2059 "an LDAP connection. One of; NONE, SSL or STARTTLS."),
2061 AP_INIT_FLAG("LDAPVerifyServerCert", util_ldap_set_verify_srv_cert,
2063 "Set to 'ON' requires that the server certificate be verified "
2064 "before a secure LDAP connection can be establish. Default 'ON'"),
2066 AP_INIT_TAKE1("LDAPConnectionTimeout", util_ldap_set_connection_timeout,
2068 "Specify the LDAP socket connection timeout in seconds "
2074 static void util_ldap_register_hooks(apr_pool_t *p)
2076 APR_REGISTER_OPTIONAL_FN(uldap_connection_open);
2077 APR_REGISTER_OPTIONAL_FN(uldap_connection_close);
2078 APR_REGISTER_OPTIONAL_FN(uldap_connection_unbind);
2079 APR_REGISTER_OPTIONAL_FN(uldap_connection_cleanup);
2080 APR_REGISTER_OPTIONAL_FN(uldap_connection_find);
2081 APR_REGISTER_OPTIONAL_FN(uldap_cache_comparedn);
2082 APR_REGISTER_OPTIONAL_FN(uldap_cache_compare);
2083 APR_REGISTER_OPTIONAL_FN(uldap_cache_checkuserid);
2084 APR_REGISTER_OPTIONAL_FN(uldap_cache_getuserdn);
2085 APR_REGISTER_OPTIONAL_FN(uldap_ssl_supported);
2087 ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
2088 ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
2089 ap_hook_child_init(util_ldap_child_init, NULL, NULL, APR_HOOK_MIDDLE);
2092 module AP_MODULE_DECLARE_DATA ldap_module = {
2093 STANDARD20_MODULE_STUFF,
2094 NULL, /* create dir config */
2095 NULL, /* merge dir config */
2096 util_ldap_create_config, /* create server config */
2097 util_ldap_merge_config, /* merge server config */
2098 util_ldap_cmds, /* command table */
2099 util_ldap_register_hooks, /* set up request processing hooks */