1 /* Copyright 2001-2004 The Apache Software Foundation
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * util_ldap.c: LDAP things
19 * Original code from auth_ldap module for Apache v1.3:
20 * Copyright 1998, 1999 Enbridge Pipelines Inc.
21 * Copyright 1999-2001 Dave Carrigan
25 #include <apr_strings.h>
27 #include "ap_config.h"
29 #include "http_config.h"
30 #include "http_core.h"
32 #include "http_protocol.h"
33 #include "http_request.h"
34 #include "util_ldap.h"
35 #include "util_ldap_cache.h"
42 #error mod_ldap requires APR-util to have LDAP support built in
45 /* defines for certificate file types
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
53 module AP_MODULE_DECLARE_DATA ldap_module;
55 int util_ldap_handler(request_rec *r);
56 void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
60 * Some definitions to help between various versions of apache.
63 #ifndef DOCTYPE_HTML_2_0
64 #define DOCTYPE_HTML_2_0 "<!DOCTYPE HTML PUBLIC \"-//IETF//" \
65 "DTD HTML 2.0//EN\">\n"
68 #ifndef DOCTYPE_HTML_3_2
69 #define DOCTYPE_HTML_3_2 "<!DOCTYPE HTML PUBLIC \"-//W3C//" \
70 "DTD HTML 3.2 Final//EN\">\n"
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"
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"
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"
91 #define LDAP_CACHE_LOCK() \
92 apr_global_mutex_lock(st->util_ldap_cache_lock)
93 #define LDAP_CACHE_UNLOCK() \
94 apr_global_mutex_unlock(st->util_ldap_cache_lock)
97 static void util_ldap_strdup (char **str, const char *newstr)
105 *str = calloc(1, strlen(newstr)+1);
106 strcpy (*str, newstr);
114 * This handler generates a status page about the current performance of
115 * the LDAP cache. It is enabled as follows:
117 * <Location /ldap-status>
118 * SetHandler ldap-status
122 int util_ldap_handler(request_rec *r)
124 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
126 r->allowed |= (1 << M_GET);
127 if (r->method_number != M_GET)
130 if (strcmp(r->handler, "ldap-status")) {
134 r->content_type = "text/html";
138 ap_rputs(DOCTYPE_HTML_3_2
139 "<html><head><title>LDAP Cache Information</title></head>\n", r);
140 ap_rputs("<body bgcolor='#ffffff'><h1 align=center>LDAP Cache Information</h1>\n", r);
142 util_ald_cache_display(r, st);
147 /* ------------------------------------------------------------------ */
151 * Closes an LDAP connection by unlocking it. The next time
152 * util_ldap_connection_find() is called this connection will be
153 * available for reuse.
155 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc)
161 * Is it safe leaving bound connections floating around between the
162 * different modules? Keeping the user bound is a performance boost,
163 * but it is also a potential security problem - maybe.
165 * For now we unbind the user when we finish with a connection, but
166 * we don't have to...
169 /* mark our connection as available for reuse */
172 apr_thread_mutex_unlock(ldc->lock);
178 * Destroys an LDAP connection by unbinding and closing the connection to
179 * the LDAP server. It is used to bring the connection back to a known
180 * state after an error, and during pool cleanup.
182 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_unbind(void *param)
184 util_ldap_connection_t *ldc = param;
188 ldap_unbind_s(ldc->ldap);
199 * Clean up an LDAP connection by unbinding and unlocking the connection.
200 * This function is registered with the pool cleanup function - causing
201 * the LDAP connections to be shut down cleanly on graceful restart.
203 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_cleanup(void *param)
205 util_ldap_connection_t *ldc = param;
209 /* unbind and disconnect from the LDAP server */
210 util_ldap_connection_unbind(ldc);
212 /* free the username and password */
214 free((void*)ldc->bindpw);
217 free((void*)ldc->binddn);
220 /* unlock this entry */
221 util_ldap_connection_close(ldc);
230 * Connect to the LDAP server and binds. Does not connect if already
231 * connected (i.e. ldc->ldap is non-NULL.) Does not bind if already bound.
233 * Returns LDAP_SUCCESS on success; and an error code on failure
235 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r,
236 util_ldap_connection_t *ldc)
240 int version = LDAP_VERSION3;
242 /* If the connection is already bound, return
246 ldc->reason = "LDAP: connection open successful (already bound)";
250 /* create the ldap session handle
252 if (NULL == ldc->ldap)
254 apr_ldap_err_t *result = NULL;
256 apr_ldap_init(r->pool, &(ldc->ldap), ldc->host, ldc->port,
257 ldc->secure, &(result));
259 if (result != NULL) {
260 ldc->reason = result->reason;
263 if (NULL == ldc->ldap)
266 if (NULL == ldc->reason)
267 ldc->reason = "LDAP: ldap initialization failed";
271 /* Set the alias dereferencing option */
272 ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
274 /* always default to LDAP V3 */
275 ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
280 /* loop trying to bind up to 10 times if LDAP_SERVER_DOWN error is
281 * returned. Break out of the loop on Success or any other error.
283 * NOTE: Looping is probably not a great idea. If the server isn't
284 * responding the chances it will respond after a few tries are poor.
285 * However, the original code looped and it only happens on
286 * the error condition.
288 for (failures=0; failures<10; failures++)
290 result = ldap_simple_bind_s(ldc->ldap, ldc->binddn, ldc->bindpw);
291 if (LDAP_SERVER_DOWN != result)
295 /* free the handle if there was an error
297 if (LDAP_SUCCESS != result)
299 ldap_unbind_s(ldc->ldap);
302 ldc->reason = "LDAP: ldap_simple_bind_s() failed";
306 ldc->reason = "LDAP: connection open successful";
314 * Find an existing ldap connection struct that matches the
315 * provided ldap connection parameters.
317 * If not found in the cache, a new ldc structure will be allocated from st->pool
318 * and returned to the caller. If found in the cache, a pointer to the existing
319 * ldc structure will be returned.
321 LDAP_DECLARE(util_ldap_connection_t *)util_ldap_connection_find(request_rec *r, const char *host, int port,
322 const char *binddn, const char *bindpw, deref_options deref,
325 struct util_ldap_connection_t *l, *p; /* To traverse the linked list */
327 util_ldap_state_t *st =
328 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
333 /* mutex lock this function */
335 apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT, st->pool);
337 apr_thread_mutex_lock(st->mutex);
340 /* Search for an exact connection match in the list that is not
343 for (l=st->connections,p=NULL; l; l=l->next) {
345 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
347 if ((l->port == port) && (strcmp(l->host, host) == 0) &&
348 ((!l->binddn && !binddn) || (l->binddn && binddn && !strcmp(l->binddn, binddn))) &&
349 ((!l->bindpw && !bindpw) || (l->bindpw && bindpw && !strcmp(l->bindpw, bindpw))) &&
350 (l->deref == deref) && (l->secure == secure)) {
355 /* If this connection didn't match the criteria, then we
356 * need to unlock the mutex so it is available to be reused.
358 apr_thread_mutex_unlock(l->lock);
364 /* If nothing found, search again, but we don't care about the
365 * binddn and bindpw this time.
368 for (l=st->connections,p=NULL; l; l=l->next) {
370 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
373 if ((l->port == port) && (strcmp(l->host, host) == 0) &&
374 (l->deref == deref) && (l->secure == secure)) {
376 /* the bind credentials have changed */
378 util_ldap_strdup((char**)&(l->binddn), binddn);
379 util_ldap_strdup((char**)&(l->bindpw), bindpw);
383 /* If this connection didn't match the criteria, then we
384 * need to unlock the mutex so it is available to be reused.
386 apr_thread_mutex_unlock(l->lock);
393 /* artificially disable cache */
396 /* If no connection what found after the second search, we
402 * Add the new connection entry to the linked list. Note that we
403 * don't actually establish an LDAP connection yet; that happens
404 * the first time authentication is requested.
406 /* create the details to the pool in st */
407 l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
409 apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
410 apr_thread_mutex_lock(l->lock);
414 l->host = apr_pstrdup(st->pool, host);
417 util_ldap_strdup((char**)&(l->binddn), binddn);
418 util_ldap_strdup((char**)&(l->bindpw), bindpw);
421 /* add the cleanup to the pool */
422 apr_pool_cleanup_register(l->pool, l,
423 util_ldap_connection_cleanup,
424 apr_pool_cleanup_null);
435 apr_thread_mutex_unlock(st->mutex);
440 /* ------------------------------------------------------------------ */
443 * Compares two DNs to see if they're equal. The only way to do this correctly is to
444 * search for the dn and then do ldap_get_dn() on the result. This should match the
445 * initial dn, since it would have been also retrieved with ldap_get_dn(). This is
446 * expensive, so if the configuration value compare_dn_on_server is
447 * false, just does an ordinary strcmp.
449 * The lock for the ldap cache should already be acquired.
451 LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
452 const char *url, const char *dn, const char *reqdn,
453 int compare_dn_on_server)
456 util_url_node_t *curl;
457 util_url_node_t curnode;
458 util_dn_compare_node_t *node;
459 util_dn_compare_node_t newnode;
461 LDAPMessage *res, *entry;
464 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
466 /* get cache entry (or create one) */
470 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
472 curl = util_ald_create_caches(st, url);
476 /* a simple compare? */
477 if (!compare_dn_on_server) {
478 /* unlock this read lock */
479 if (strcmp(dn, reqdn)) {
480 ldc->reason = "DN Comparison FALSE (direct strcmp())";
481 return LDAP_COMPARE_FALSE;
484 ldc->reason = "DN Comparison TRUE (direct strcmp())";
485 return LDAP_COMPARE_TRUE;
490 /* no - it's a server side compare */
493 /* is it in the compare cache? */
494 newnode.reqdn = (char *)reqdn;
495 node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
497 /* If it's in the cache, it's good */
498 /* unlock this read lock */
500 ldc->reason = "DN Comparison TRUE (cached)";
501 return LDAP_COMPARE_TRUE;
504 /* unlock this read lock */
509 if (failures++ > 10) {
510 /* too many failures */
514 /* make a server connection */
515 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
516 /* connect to server failed */
520 /* search for reqdn */
521 if ((result = ldap_search_ext_s(ldc->ldap, reqdn, LDAP_SCOPE_BASE,
522 "(objectclass=*)", NULL, 1,
523 NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
524 ldc->reason = "DN Comparison ldap_search_ext_s() failed with server down";
525 util_ldap_connection_unbind(ldc);
528 if (result != LDAP_SUCCESS) {
529 /* search for reqdn failed - no match */
530 ldc->reason = "DN Comparison ldap_search_ext_s() failed";
534 entry = ldap_first_entry(ldc->ldap, res);
535 searchdn = ldap_get_dn(ldc->ldap, entry);
538 if (strcmp(dn, searchdn) != 0) {
539 /* compare unsuccessful */
540 ldc->reason = "DN Comparison FALSE (checked on server)";
541 result = LDAP_COMPARE_FALSE;
545 /* compare successful - add to the compare cache */
547 newnode.reqdn = (char *)reqdn;
548 newnode.dn = (char *)dn;
550 node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
551 if ((node == NULL) ||
552 (strcmp(reqdn, node->reqdn) != 0) || (strcmp(dn, node->dn) != 0)) {
554 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
558 ldc->reason = "DN Comparison TRUE (checked on server)";
559 result = LDAP_COMPARE_TRUE;
561 ldap_memfree(searchdn);
567 * Does an generic ldap_compare operation. It accepts a cache that it will use
568 * to lookup the compare in the cache. We cache two kinds of compares
569 * (require group compares) and (require user compares). Each compare has a different
570 * cache node: require group includes the DN; require user does not because the
571 * require user cache is owned by the
574 LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
575 const char *url, const char *dn,
576 const char *attrib, const char *value)
579 util_url_node_t *curl;
580 util_url_node_t curnode;
581 util_compare_node_t *compare_nodep;
582 util_compare_node_t the_compare_node;
583 apr_time_t curtime = 0; /* silence gcc -Wall */
586 util_ldap_state_t *st =
587 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
590 /* get cache entry (or create one) */
593 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
595 curl = util_ald_create_caches(st, url);
600 /* make a comparison to the cache */
602 curtime = apr_time_now();
604 the_compare_node.dn = (char *)dn;
605 the_compare_node.attrib = (char *)attrib;
606 the_compare_node.value = (char *)value;
607 the_compare_node.result = 0;
609 compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
611 if (compare_nodep != NULL) {
613 if (curtime - compare_nodep->lastcompare > st->compare_cache_ttl) {
614 /* ...but it is too old */
615 util_ald_cache_remove(curl->compare_cache, compare_nodep);
618 /* ...and it is good */
619 /* unlock this read lock */
621 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
622 ldc->reason = "Comparison true (cached)";
623 return compare_nodep->result;
625 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
626 ldc->reason = "Comparison false (cached)";
627 return compare_nodep->result;
629 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
630 ldc->reason = "Comparison no such attribute (cached)";
631 return compare_nodep->result;
634 ldc->reason = "Comparison undefined (cached)";
635 return compare_nodep->result;
639 /* unlock this read lock */
644 if (failures++ > 10) {
645 /* too many failures */
648 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
653 if ((result = ldap_compare_s(ldc->ldap, dn, attrib, value))
654 == LDAP_SERVER_DOWN) {
655 /* connection failed - try again */
656 ldc->reason = "ldap_compare_s() failed with server down";
657 util_ldap_connection_unbind(ldc);
661 ldc->reason = "Comparison complete";
662 if ((LDAP_COMPARE_TRUE == result) ||
663 (LDAP_COMPARE_FALSE == result) ||
664 (LDAP_NO_SUCH_ATTRIBUTE == result)) {
666 /* compare completed; caching result */
668 the_compare_node.lastcompare = curtime;
669 the_compare_node.result = result;
671 /* If the node doesn't exist then insert it, otherwise just update it with
673 compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
674 if ((compare_nodep == NULL) ||
675 (strcmp(the_compare_node.dn, compare_nodep->dn) != 0) ||
676 (strcmp(the_compare_node.attrib, compare_nodep->attrib) != 0) ||
677 (strcmp(the_compare_node.value, compare_nodep->value) != 0)) {
679 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
682 compare_nodep->lastcompare = curtime;
683 compare_nodep->result = result;
687 if (LDAP_COMPARE_TRUE == result) {
688 ldc->reason = "Comparison true (adding to cache)";
689 return LDAP_COMPARE_TRUE;
691 else if (LDAP_COMPARE_FALSE == result) {
692 ldc->reason = "Comparison false (adding to cache)";
693 return LDAP_COMPARE_FALSE;
696 ldc->reason = "Comparison no such attribute (adding to cache)";
697 return LDAP_NO_SUCH_ATTRIBUTE;
703 LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
704 const char *url, const char *basedn, int scope, char **attrs,
705 const char *filter, const char *bindpw, const char **binddn,
706 const char ***retvals)
708 const char **vals = NULL;
710 LDAPMessage *res, *entry;
714 util_url_node_t *curl; /* Cached URL node */
715 util_url_node_t curnode;
716 util_search_node_t *search_nodep; /* Cached search node */
717 util_search_node_t the_search_node;
720 util_ldap_state_t *st =
721 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
724 /* Get the cache node for this url */
727 curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
729 curl = util_ald_create_caches(st, url);
735 the_search_node.username = filter;
736 search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
737 if (search_nodep != NULL && search_nodep->bindpw) {
739 /* found entry in search cache... */
740 curtime = apr_time_now();
743 * Remove this item from the cache if its expired, or if the
744 * sent password doesn't match the storepassword.
746 if ((curtime - search_nodep->lastbind) > st->search_cache_ttl) {
747 /* ...but entry is too old */
748 util_ald_cache_remove(curl->search_cache, search_nodep);
750 else if (strcmp(search_nodep->bindpw, bindpw) != 0) {
751 /* ...but cached password doesn't match sent password */
752 util_ald_cache_remove(curl->search_cache, search_nodep);
755 /* ...and entry is valid */
756 *binddn = search_nodep->dn;
757 *retvals = search_nodep->vals;
759 ldc->reason = "Authentication successful (cached)";
763 /* unlock this read lock */
768 * At this point, there is no valid cached search, so lets do the search.
772 * If any LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
775 if (failures++ > 10) {
778 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
782 /* try do the search */
783 if ((result = ldap_search_ext_s(ldc->ldap,
786 NULL, NULL, NULL, -1, &res)) == LDAP_SERVER_DOWN) {
787 ldc->reason = "ldap_search_ext_s() for user failed with server down";
788 util_ldap_connection_unbind(ldc);
792 /* if there is an error (including LDAP_NO_SUCH_OBJECT) return now */
793 if (result != LDAP_SUCCESS) {
794 ldc->reason = "ldap_search_ext_s() for user failed";
799 * We should have found exactly one entry; to find a different
800 * number is an error.
802 count = ldap_count_entries(ldc->ldap, res);
806 ldc->reason = "User not found";
808 ldc->reason = "User is not unique (search found two or more matches)";
810 return LDAP_NO_SUCH_OBJECT;
813 entry = ldap_first_entry(ldc->ldap, res);
815 /* Grab the dn, copy it into the pool, and free it again */
816 dn = ldap_get_dn(ldc->ldap, entry);
817 *binddn = apr_pstrdup(r->pool, dn);
821 * A bind to the server with an empty password always succeeds, so
822 * we check to ensure that the password is not empty. This implies
823 * that users who actually do have empty passwords will never be
824 * able to authenticate with this module. I don't see this as a big
827 if (strlen(bindpw) <= 0) {
829 ldc->reason = "Empty password not allowed";
830 return LDAP_INVALID_CREDENTIALS;
834 * Attempt to bind with the retrieved dn and the password. If the bind
835 * fails, it means that the password is wrong (the dn obviously
836 * exists, since we just retrieved it)
839 ldap_simple_bind_s(ldc->ldap, *binddn, bindpw)) ==
841 ldc->reason = "ldap_simple_bind_s() to check user credentials failed with server down";
843 util_ldap_connection_unbind(ldc);
847 /* failure? if so - return */
848 if (result != LDAP_SUCCESS) {
849 ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
851 util_ldap_connection_unbind(ldc);
856 * We have just bound the connection to a different user and password
857 * combination, which might be reused unintentionally next time this
858 * connection is used from the connection pool. To ensure no confusion,
859 * we mark the connection as unbound.
865 * Get values for the provided attributes.
871 vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
877 values = ldap_get_values(ldc->ldap, entry, attrs[i]);
878 while (values && values[j]) {
879 str = str ? apr_pstrcat(r->pool, str, "; ", values[j], NULL) : apr_pstrdup(r->pool, values[j]);
882 ldap_value_free(values);
890 * Add the new username to the search cache.
894 the_search_node.username = filter;
895 the_search_node.dn = *binddn;
896 the_search_node.bindpw = bindpw;
897 the_search_node.lastbind = apr_time_now();
898 the_search_node.vals = vals;
900 /* Search again to make sure that another thread didn't ready insert this node
901 into the cache before we got here. If it does exist then update the lastbind */
902 search_nodep = util_ald_cache_fetch(curl->search_cache, &the_search_node);
903 if ((search_nodep == NULL) ||
904 (strcmp(*binddn, search_nodep->dn) != 0) || (strcmp(bindpw, search_nodep->bindpw) != 0)) {
906 util_ald_cache_insert(curl->search_cache, &the_search_node);
909 search_nodep->lastbind = the_search_node.lastbind;
915 ldc->reason = "Authentication successful";
921 * Reports if ssl support is enabled
923 * 1 = enabled, 0 = not enabled
925 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r)
927 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
928 r->server->module_config, &ldap_module);
930 return(st->ssl_support);
934 /* ---------------------------------------- */
935 /* config directives */
938 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, const char *bytes)
940 util_ldap_state_t *st =
941 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
944 st->cache_bytes = atol(bytes);
946 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
947 "[%" APR_PID_T_FMT "] ldap cache: Setting shared memory "
948 " cache size to %" APR_SIZE_T_FMT " bytes.",
949 getpid(), st->cache_bytes);
954 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
956 util_ldap_state_t *st =
957 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
961 st->cache_file = ap_server_root_relative(st->pool, file);
964 st->cache_file = NULL;
967 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
968 "LDAP cache: Setting shared memory cache file to %s bytes.",
974 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
976 util_ldap_state_t *st =
977 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
980 st->search_cache_ttl = atol(ttl) * 1000000;
982 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
983 "[%d] ldap cache: Setting cache TTL to %ld microseconds.",
984 getpid(), st->search_cache_ttl);
989 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy, const char *size)
991 util_ldap_state_t *st =
992 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
996 st->search_cache_size = atol(size);
997 if (st->search_cache_size < 0) {
998 st->search_cache_size = 0;
1001 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1002 "[%d] ldap cache: Setting search cache size to %ld entries.",
1003 getpid(), st->search_cache_size);
1008 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1010 util_ldap_state_t *st =
1011 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1014 st->compare_cache_ttl = atol(ttl) * 1000000;
1016 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1017 "[%d] ldap cache: Setting operation cache TTL to %ld microseconds.",
1018 getpid(), st->compare_cache_ttl);
1023 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy, const char *size)
1025 util_ldap_state_t *st =
1026 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1029 st->compare_cache_size = atol(size);
1030 if (st->compare_cache_size < 0) {
1031 st->compare_cache_size = 0;
1034 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1035 "[%d] ldap cache: Setting operation cache size to %ld entries.",
1036 getpid(), st->compare_cache_size);
1041 static const char *util_ldap_set_cert_auth(cmd_parms *cmd, void *dummy, const char *file)
1043 util_ldap_state_t *st =
1044 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1046 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1051 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1052 "LDAP: SSL trusted certificate authority file - %s",
1055 st->cert_auth_file = ap_server_root_relative(cmd->pool, file);
1061 static const char *util_ldap_set_cert_type(cmd_parms *cmd, void *dummy, const char *Type)
1063 util_ldap_state_t *st =
1064 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1066 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1071 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1072 "LDAP: SSL trusted certificate authority file type - %s",
1075 if (0 == strcmp("DER_FILE", Type))
1076 st->cert_file_type = LDAP_CA_TYPE_DER;
1078 else if (0 == strcmp("BASE64_FILE", Type))
1079 st->cert_file_type = LDAP_CA_TYPE_BASE64;
1081 else if (0 == strcmp("CERT7_DB_PATH", Type))
1082 st->cert_file_type = LDAP_CA_TYPE_CERT7_DB;
1085 st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1091 void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1093 util_ldap_state_t *st =
1094 (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
1098 st->cache_bytes = 100000;
1099 st->search_cache_ttl = 600000000;
1100 st->search_cache_size = 1024;
1101 st->compare_cache_ttl = 600000000;
1102 st->compare_cache_size = 1024;
1103 st->connections = NULL;
1104 st->cert_auth_file = NULL;
1105 st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1106 st->ssl_support = 0;
1111 static apr_status_t util_ldap_cleanup_module(void *data)
1114 server_rec *s = data;
1115 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
1116 s->module_config, &ldap_module);
1118 if (st->ssl_support) {
1119 apr_ldap_ssl_deinit();
1126 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog,
1127 apr_pool_t *ptemp, server_rec *s)
1129 apr_status_t result;
1130 char buf[MAX_STRING_LEN];
1131 server_rec *s_vhost;
1132 util_ldap_state_t *st_vhost;
1134 util_ldap_state_t *st =
1135 (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1138 const char *userdata_key = "util_ldap_init";
1140 /* util_ldap_post_config() will be called twice. Don't bother
1141 * going through all of the initialization on the first call
1142 * because it will just be thrown away.*/
1143 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1145 apr_pool_userdata_set((const void *)1, userdata_key,
1146 apr_pool_cleanup_null, s->process->pool);
1148 #if APR_HAS_SHARED_MEMORY
1149 /* If the cache file already exists then delete it. Otherwise we are
1150 * going to run into problems creating the shared memory. */
1151 if (st->cache_file) {
1152 char *lck_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1153 apr_file_remove(st->cache_file, ptemp);
1154 apr_file_remove(lck_file, ptemp);
1160 #if APR_HAS_SHARED_MEMORY
1161 /* initializing cache if shared memory size is not zero and we already don't have shm address */
1162 if (!st->cache_shm && st->cache_bytes > 0) {
1164 result = util_ldap_cache_init(p, st);
1165 if (result != APR_SUCCESS) {
1166 apr_strerror(result, buf, sizeof(buf));
1167 ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
1168 "LDAP cache: error while creating a shared memory segment: %s", buf);
1172 #if APR_HAS_SHARED_MEMORY
1173 if (st->cache_file) {
1174 st->lock_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1178 st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1180 result = apr_global_mutex_create(&st->util_ldap_cache_lock, st->lock_file, APR_LOCK_DEFAULT, st->pool);
1181 if (result != APR_SUCCESS) {
1185 /* merge config in all vhost */
1188 st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
1190 #if APR_HAS_SHARED_MEMORY
1191 st_vhost->cache_shm = st->cache_shm;
1192 st_vhost->cache_rmm = st->cache_rmm;
1193 st_vhost->cache_file = st->cache_file;
1194 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s,
1195 "LDAP merging Shared Cache conf: shm=0x%pp rmm=0x%pp for VHOST: %s",
1196 st->cache_shm, st->cache_rmm, s_vhost->server_hostname);
1198 st_vhost->lock_file = st->lock_file;
1199 s_vhost = s_vhost->next;
1201 #if APR_HAS_SHARED_MEMORY
1204 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "LDAP cache: LDAPSharedCacheSize is zero, disabling shared memory cache");
1208 /* log the LDAP SDK used
1211 apr_ldap_err_t *result = NULL;
1212 apr_ldap_info(p, &(result));
1213 if (result != NULL) {
1214 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, result->reason);
1218 apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1219 util_ldap_cleanup_module);
1221 /* initialize SSL support if requested
1223 if (st->cert_auth_file) {
1225 apr_ldap_err_t *result = NULL;
1226 int rc = apr_ldap_ssl_init(p,
1231 if (LDAP_SUCCESS == rc) {
1232 st->ssl_support = 1;
1234 else if (NULL != result) {
1235 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, result->reason);
1236 st->ssl_support = 0;
1241 /* log SSL status - If SSL isn't available it isn't necessarily
1242 * an error because the modules asking for LDAP connections
1243 * may not ask for SSL support
1245 if (st->ssl_support) {
1246 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1247 "LDAP: SSL support available" );
1250 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1251 "LDAP: SSL support unavailable" );
1257 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1260 util_ldap_state_t *st =
1261 (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1263 sts = apr_global_mutex_child_init(&st->util_ldap_cache_lock, st->lock_file, p);
1264 if (sts != APR_SUCCESS) {
1265 ap_log_error(APLOG_MARK, APLOG_CRIT, sts, s, "failed to init caching lock in child process");
1269 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, s,
1270 "INIT global mutex %s in child %d ", st->lock_file, getpid());
1274 command_rec util_ldap_cmds[] = {
1275 AP_INIT_TAKE1("LDAPSharedCacheSize", util_ldap_set_cache_bytes, NULL, RSRC_CONF,
1276 "Sets the size of the shared memory cache in bytes. "
1277 "Zero means disable the shared memory cache. Defaults to 100KB."),
1279 AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, NULL, RSRC_CONF,
1280 "Sets the file of the shared memory cache."
1281 "Nothing means disable the shared memory cache."),
1283 AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, NULL, RSRC_CONF,
1284 "Sets the maximum number of entries that are possible in the LDAP "
1286 "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1288 AP_INIT_TAKE1("LDAPCacheTTL", util_ldap_set_cache_ttl, NULL, RSRC_CONF,
1289 "Sets the maximum time (in seconds) that an item can be cached in the LDAP "
1290 "search cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1292 AP_INIT_TAKE1("LDAPOpCacheEntries", util_ldap_set_opcache_entries, NULL, RSRC_CONF,
1293 "Sets the maximum number of entries that are possible in the LDAP "
1295 "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
1297 AP_INIT_TAKE1("LDAPOpCacheTTL", util_ldap_set_opcache_ttl, NULL, RSRC_CONF,
1298 "Sets the maximum time (in seconds) that an item is cached in the LDAP "
1299 "operation cache. Zero means no limit. Defaults to 600 seconds (10 minutes)."),
1301 AP_INIT_TAKE1("LDAPTrustedCA", util_ldap_set_cert_auth, NULL, RSRC_CONF,
1302 "Sets the file containing the trusted Certificate Authority certificate. "
1303 "Used to validate the LDAP server certificate for SSL connections."),
1305 AP_INIT_TAKE1("LDAPTrustedCAType", util_ldap_set_cert_type, NULL, RSRC_CONF,
1306 "Specifies the type of the Certificate Authority file. "
1307 "The following types are supported: "
1308 " DER_FILE - file in binary DER format "
1309 " BASE64_FILE - file in Base64 format "
1310 " CERT7_DB_PATH - Netscape certificate database file "),
1314 static void util_ldap_register_hooks(apr_pool_t *p)
1316 ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
1317 ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
1318 ap_hook_child_init(util_ldap_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1321 module ldap_module = {
1322 STANDARD20_MODULE_STUFF,
1323 NULL, /* dir config creater */
1324 NULL, /* dir merger --- default is to override */
1325 util_ldap_create_config, /* server config */
1326 NULL, /* merge server config */
1327 util_ldap_cmds, /* command table */
1328 util_ldap_register_hooks, /* set up request processing hooks */