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 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)
99 static void util_ldap_strdup (char **str, const char *newstr)
107 *str = calloc(1, strlen(newstr)+1);
108 strcpy (*str, newstr);
116 * This handler generates a status page about the current performance of
117 * the LDAP cache. It is enabled as follows:
119 * <Location /ldap-status>
120 * SetHandler ldap-status
124 int util_ldap_handler(request_rec *r)
126 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
128 r->allowed |= (1 << M_GET);
129 if (r->method_number != M_GET)
132 if (strcmp(r->handler, "ldap-status")) {
136 r->content_type = "text/html";
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);
144 util_ald_cache_display(r, st);
149 /* ------------------------------------------------------------------ */
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.
157 LDAP_DECLARE(void) util_ldap_connection_close(util_ldap_connection_t *ldc)
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.
167 * For now we unbind the user when we finish with a connection, but
168 * we don't have to...
171 /* mark our connection as available for reuse */
174 apr_thread_mutex_unlock(ldc->lock);
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.
184 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_unbind(void *param)
186 util_ldap_connection_t *ldc = param;
190 ldap_unbind_s(ldc->ldap);
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.
205 LDAP_DECLARE_NONSTD(apr_status_t) util_ldap_connection_cleanup(void *param)
207 util_ldap_connection_t *ldc = param;
211 /* unbind and disconnect from the LDAP server */
212 util_ldap_connection_unbind(ldc);
214 /* free the username and password */
216 free((void*)ldc->bindpw);
219 free((void*)ldc->binddn);
222 /* unlock this entry */
223 util_ldap_connection_close(ldc);
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.
235 * Returns LDAP_SUCCESS on success; and an error code on failure
237 LDAP_DECLARE(int) util_ldap_connection_open(request_rec *r,
238 util_ldap_connection_t *ldc)
242 int version = LDAP_VERSION3;
244 /* If the connection is already bound, return
248 ldc->reason = "LDAP: connection open successful (already bound)";
252 /* create the ldap session handle
254 if (NULL == ldc->ldap)
256 apr_ldap_err_t *result = NULL;
258 apr_ldap_init(r->pool, &(ldc->ldap), ldc->host, ldc->port,
259 ldc->secure, &(result));
261 if (result != NULL) {
262 ldc->reason = result->reason;
265 if (NULL == ldc->ldap)
268 if (NULL == ldc->reason)
269 ldc->reason = "LDAP: ldap initialization failed";
273 /* Set the alias dereferencing option */
274 ldap_set_option(ldc->ldap, LDAP_OPT_DEREF, &(ldc->deref));
276 /* always default to LDAP V3 */
277 ldap_set_option(ldc->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
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.
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.
290 for (failures=0; failures<10; failures++)
292 result = ldap_simple_bind_s(ldc->ldap, ldc->binddn, ldc->bindpw);
293 if (LDAP_SERVER_DOWN != result)
297 /* free the handle if there was an error
299 if (LDAP_SUCCESS != result)
301 ldap_unbind_s(ldc->ldap);
304 ldc->reason = "LDAP: ldap_simple_bind_s() failed";
308 ldc->reason = "LDAP: connection open successful";
316 * Find an existing ldap connection struct that matches the
317 * provided ldap connection parameters.
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.
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,
327 struct util_ldap_connection_t *l, *p; /* To traverse the linked list */
329 util_ldap_state_t *st =
330 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
335 /* mutex lock this function */
337 apr_thread_mutex_create(&st->mutex, APR_THREAD_MUTEX_DEFAULT, st->pool);
339 apr_thread_mutex_lock(st->mutex);
342 /* Search for an exact connection match in the list that is not
345 for (l=st->connections,p=NULL; l; l=l->next) {
347 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
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)) {
357 /* If this connection didn't match the criteria, then we
358 * need to unlock the mutex so it is available to be reused.
360 apr_thread_mutex_unlock(l->lock);
366 /* If nothing found, search again, but we don't care about the
367 * binddn and bindpw this time.
370 for (l=st->connections,p=NULL; l; l=l->next) {
372 if (APR_SUCCESS == apr_thread_mutex_trylock(l->lock)) {
375 if ((l->port == port) && (strcmp(l->host, host) == 0) &&
376 (l->deref == deref) && (l->secure == secure)) {
378 /* the bind credentials have changed */
380 util_ldap_strdup((char**)&(l->binddn), binddn);
381 util_ldap_strdup((char**)&(l->bindpw), bindpw);
385 /* If this connection didn't match the criteria, then we
386 * need to unlock the mutex so it is available to be reused.
388 apr_thread_mutex_unlock(l->lock);
395 /* artificially disable cache */
398 /* If no connection what found after the second search, we
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.
408 /* create the details to the pool in st */
409 l = apr_pcalloc(st->pool, sizeof(util_ldap_connection_t));
411 apr_thread_mutex_create(&l->lock, APR_THREAD_MUTEX_DEFAULT, st->pool);
412 apr_thread_mutex_lock(l->lock);
416 l->host = apr_pstrdup(st->pool, host);
419 util_ldap_strdup((char**)&(l->binddn), binddn);
420 util_ldap_strdup((char**)&(l->bindpw), bindpw);
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);
437 apr_thread_mutex_unlock(st->mutex);
442 /* ------------------------------------------------------------------ */
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.
451 * The lock for the ldap cache should already be acquired.
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)
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;
463 LDAPMessage *res, *entry;
466 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
468 /* get cache entry (or create one) */
472 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
474 curl = util_ald_create_caches(st, url);
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;
486 ldc->reason = "DN Comparison TRUE (direct strcmp())";
487 return LDAP_COMPARE_TRUE;
492 /* no - it's a server side compare */
495 /* is it in the compare cache? */
496 newnode.reqdn = (char *)reqdn;
497 node = util_ald_cache_fetch(curl->dn_compare_cache, &newnode);
499 /* If it's in the cache, it's good */
500 /* unlock this read lock */
502 ldc->reason = "DN Comparison TRUE (cached)";
503 return LDAP_COMPARE_TRUE;
506 /* unlock this read lock */
511 if (failures++ > 10) {
512 /* too many failures */
516 /* make a server connection */
517 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
518 /* connect to server failed */
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);
530 if (result != LDAP_SUCCESS) {
531 /* search for reqdn failed - no match */
532 ldc->reason = "DN Comparison ldap_search_ext_s() failed";
536 entry = ldap_first_entry(ldc->ldap, res);
537 searchdn = ldap_get_dn(ldc->ldap, entry);
540 if (strcmp(dn, searchdn) != 0) {
541 /* compare unsuccessful */
542 ldc->reason = "DN Comparison FALSE (checked on server)";
543 result = LDAP_COMPARE_FALSE;
547 /* compare successful - add to the compare cache */
549 newnode.reqdn = (char *)reqdn;
550 newnode.dn = (char *)dn;
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)) {
556 util_ald_cache_insert(curl->dn_compare_cache, &newnode);
560 ldc->reason = "DN Comparison TRUE (checked on server)";
561 result = LDAP_COMPARE_TRUE;
563 ldap_memfree(searchdn);
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
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)
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 */
588 util_ldap_state_t *st =
589 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
592 /* get cache entry (or create one) */
595 curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
597 curl = util_ald_create_caches(st, url);
602 /* make a comparison to the cache */
604 curtime = apr_time_now();
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;
611 compare_nodep = util_ald_cache_fetch(curl->compare_cache, &the_compare_node);
613 if (compare_nodep != NULL) {
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);
620 /* ...and it is good */
621 /* unlock this read lock */
623 if (LDAP_COMPARE_TRUE == compare_nodep->result) {
624 ldc->reason = "Comparison true (cached)";
625 return compare_nodep->result;
627 else if (LDAP_COMPARE_FALSE == compare_nodep->result) {
628 ldc->reason = "Comparison false (cached)";
629 return compare_nodep->result;
631 else if (LDAP_NO_SUCH_ATTRIBUTE == compare_nodep->result) {
632 ldc->reason = "Comparison no such attribute (cached)";
633 return compare_nodep->result;
636 ldc->reason = "Comparison undefined (cached)";
637 return compare_nodep->result;
641 /* unlock this read lock */
646 if (failures++ > 10) {
647 /* too many failures */
650 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
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);
663 ldc->reason = "Comparison complete";
664 if ((LDAP_COMPARE_TRUE == result) ||
665 (LDAP_COMPARE_FALSE == result) ||
666 (LDAP_NO_SUCH_ATTRIBUTE == result)) {
668 /* compare completed; caching result */
670 the_compare_node.lastcompare = curtime;
671 the_compare_node.result = result;
673 /* If the node doesn't exist then insert it, otherwise just update it with
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)) {
681 util_ald_cache_insert(curl->compare_cache, &the_compare_node);
684 compare_nodep->lastcompare = curtime;
685 compare_nodep->result = result;
689 if (LDAP_COMPARE_TRUE == result) {
690 ldc->reason = "Comparison true (adding to cache)";
691 return LDAP_COMPARE_TRUE;
693 else if (LDAP_COMPARE_FALSE == result) {
694 ldc->reason = "Comparison false (adding to cache)";
695 return LDAP_COMPARE_FALSE;
698 ldc->reason = "Comparison no such attribute (adding to cache)";
699 return LDAP_NO_SUCH_ATTRIBUTE;
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)
710 const char **vals = NULL;
712 LDAPMessage *res, *entry;
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;
722 util_ldap_state_t *st =
723 (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
726 /* Get the cache node for this url */
729 curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
731 curl = util_ald_create_caches(st, url);
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) {
741 /* found entry in search cache... */
742 curtime = apr_time_now();
745 * Remove this item from the cache if its expired, or if the
746 * sent password doesn't match the storepassword.
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);
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);
757 /* ...and entry is valid */
758 *binddn = search_nodep->dn;
759 *retvals = search_nodep->vals;
761 ldc->reason = "Authentication successful (cached)";
765 /* unlock this read lock */
770 * At this point, there is no valid cached search, so lets do the search.
774 * If any LDAP operation fails due to LDAP_SERVER_DOWN, control returns here.
777 if (failures++ > 10) {
780 if (LDAP_SUCCESS != (result = util_ldap_connection_open(r, ldc))) {
784 /* try do the search */
785 if ((result = ldap_search_ext_s(ldc->ldap,
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);
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";
801 * We should have found exactly one entry; to find a different
802 * number is an error.
804 count = ldap_count_entries(ldc->ldap, res);
808 ldc->reason = "User not found";
810 ldc->reason = "User is not unique (search found two or more matches)";
812 return LDAP_NO_SUCH_OBJECT;
815 entry = ldap_first_entry(ldc->ldap, res);
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);
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
829 if (strlen(bindpw) <= 0) {
831 ldc->reason = "Empty password not allowed";
832 return LDAP_INVALID_CREDENTIALS;
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)
841 ldap_simple_bind_s(ldc->ldap, *binddn, bindpw)) ==
843 ldc->reason = "ldap_simple_bind_s() to check user credentials failed with server down";
845 util_ldap_connection_unbind(ldc);
849 /* failure? if so - return */
850 if (result != LDAP_SUCCESS) {
851 ldc->reason = "ldap_simple_bind_s() to check user credentials failed";
853 util_ldap_connection_unbind(ldc);
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.
867 * Get values for the provided attributes.
873 vals = apr_pcalloc(r->pool, sizeof(char *) * (k+1));
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]);
884 ldap_value_free(values);
892 * Add the new username to the search cache.
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;
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)) {
908 util_ald_cache_insert(curl->search_cache, &the_search_node);
911 search_nodep->lastbind = the_search_node.lastbind;
917 ldc->reason = "Authentication successful";
923 * Reports if ssl support is enabled
925 * 1 = enabled, 0 = not enabled
927 LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r)
929 util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
930 r->server->module_config, &ldap_module);
932 return(st->ssl_support);
936 /* ---------------------------------------- */
937 /* config directives */
940 static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, const char *bytes)
942 util_ldap_state_t *st =
943 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
946 st->cache_bytes = atol(bytes);
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);
956 static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
958 util_ldap_state_t *st =
959 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
963 st->cache_file = ap_server_root_relative(st->pool, file);
966 st->cache_file = NULL;
969 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
970 "LDAP cache: Setting shared memory cache file to %s bytes.",
976 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
978 util_ldap_state_t *st =
979 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
982 st->search_cache_ttl = atol(ttl) * 1000000;
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);
991 static const char *util_ldap_set_cache_entries(cmd_parms *cmd, void *dummy, const char *size)
993 util_ldap_state_t *st =
994 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
998 st->search_cache_size = atol(size);
999 if (st->search_cache_size < 0) {
1000 st->search_cache_size = 0;
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);
1010 static const char *util_ldap_set_opcache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
1012 util_ldap_state_t *st =
1013 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1016 st->compare_cache_ttl = atol(ttl) * 1000000;
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);
1025 static const char *util_ldap_set_opcache_entries(cmd_parms *cmd, void *dummy, const char *size)
1027 util_ldap_state_t *st =
1028 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1031 st->compare_cache_size = atol(size);
1032 if (st->compare_cache_size < 0) {
1033 st->compare_cache_size = 0;
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);
1043 static const char *util_ldap_set_cert_auth(cmd_parms *cmd, void *dummy, const char *file)
1045 util_ldap_state_t *st =
1046 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1048 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1056 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1057 "LDAP: SSL trusted certificate authority file - %s",
1060 st->cert_auth_file = ap_server_root_relative(cmd->pool, file);
1062 if (st->cert_auth_file &&
1063 ((rv = apr_stat (&finfo, st->cert_auth_file, APR_FINFO_MIN, cmd->pool)) != APR_SUCCESS))
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";
1075 static const char *util_ldap_set_cert_type(cmd_parms *cmd, void *dummy, const char *Type)
1077 util_ldap_state_t *st =
1078 (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config,
1080 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1085 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server,
1086 "LDAP: SSL trusted certificate authority file type - %s",
1089 if (0 == strcmp("DER_FILE", Type))
1090 st->cert_file_type = LDAP_CA_TYPE_DER;
1092 else if (0 == strcmp("BASE64_FILE", Type))
1093 st->cert_file_type = LDAP_CA_TYPE_BASE64;
1095 else if (0 == strcmp("CERT7_DB_PATH", Type))
1096 st->cert_file_type = LDAP_CA_TYPE_CERT7_DB;
1099 st->cert_file_type = LDAP_CA_TYPE_UNKNOWN;
1105 void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
1107 util_ldap_state_t *st =
1108 (util_ldap_state_t *)apr_pcalloc(p, sizeof(util_ldap_state_t));
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;
1125 static apr_status_t util_ldap_cleanup_module(void *data)
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);
1132 if (st->ssl_support) {
1133 apr_ldap_ssl_deinit();
1140 static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog,
1141 apr_pool_t *ptemp, server_rec *s)
1143 apr_status_t result;
1144 char buf[MAX_STRING_LEN];
1145 server_rec *s_vhost;
1146 util_ldap_state_t *st_vhost;
1148 util_ldap_state_t *st =
1149 (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
1152 const char *userdata_key = "util_ldap_init";
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);
1159 apr_pool_userdata_set((const void *)1, userdata_key,
1160 apr_pool_cleanup_null, s->process->pool);
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);
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) {
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);
1186 #if APR_HAS_SHARED_MEMORY
1187 if (st->cache_file) {
1188 st->lock_file = apr_pstrcat (st->pool, st->cache_file, ".lck", NULL);
1192 st->lock_file = ap_server_root_relative(st->pool, tmpnam(NULL));
1194 result = apr_global_mutex_create(&st->util_ldap_cache_lock, st->lock_file, APR_LOCK_DEFAULT, st->pool);
1195 if (result != APR_SUCCESS) {
1199 /* merge config in all vhost */
1202 st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
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);
1212 st_vhost->lock_file = st->lock_file;
1213 s_vhost = s_vhost->next;
1215 #if APR_HAS_SHARED_MEMORY
1218 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "LDAP cache: LDAPSharedCacheSize is zero, disabling shared memory cache");
1222 /* log the LDAP SDK used
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);
1232 apr_pool_cleanup_register(p, s, util_ldap_cleanup_module,
1233 util_ldap_cleanup_module);
1235 /* initialize SSL support if requested
1237 if (st->cert_auth_file) {
1239 apr_ldap_err_t *result = NULL;
1240 int rc = apr_ldap_ssl_init(p,
1245 if (LDAP_SUCCESS == rc) {
1246 st->ssl_support = 1;
1248 else if (NULL != result) {
1249 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, "%s", result->reason);
1250 st->ssl_support = 0;
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
1259 if (st->ssl_support) {
1260 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1261 "LDAP: SSL support available" );
1264 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
1265 "LDAP: SSL support unavailable" );
1271 static void util_ldap_child_init(apr_pool_t *p, server_rec *s)
1274 util_ldap_state_t *st =
1275 (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
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());
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());
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."),
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."),
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 "
1303 "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
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)."),
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 "
1312 "Zero means no limit; -1 disables the cache. Defaults to 1024 entries."),
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)."),
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."),
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 "),
1331 static void util_ldap_register_hooks(apr_pool_t *p)
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);
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 */