ca8514
diff --git a/include/util_ldap.h b/include/util_ldap.h
ca8514
index f7cd736..f401e79 100644
ca8514
--- a/include/util_ldap.h
ca8514
+++ b/include/util_ldap.h
ca8514
@@ -32,7 +32,6 @@
ca8514
 #if APR_MAJOR_VERSION < 2
ca8514
 /* The LDAP API is currently only present in APR 1.x */
ca8514
 #include "apr_ldap.h"
ca8514
-#include "apr_ldap_rebind.h"
ca8514
 #else
ca8514
 #define APR_HAS_LDAP 0
ca8514
 #endif
ca8514
diff --git a/modules/ldap/util_ldap.c b/modules/ldap/util_ldap.c
ca8514
index 08f986c..eecb3f7 100644
ca8514
--- a/modules/ldap/util_ldap.c
ca8514
+++ b/modules/ldap/util_ldap.c
ca8514
@@ -140,6 +140,38 @@ static int util_ldap_handler(request_rec *r)
ca8514
     return OK;
ca8514
 }
ca8514
 
ca8514
+/* For OpenLDAP with the 3-arg version of ldap_set_rebind_proc(), use
ca8514
+ * a simpler rebind callback than the implementation in APR-util.
ca8514
+ * Testing for API version >= 3001 appears safe although OpenLDAP
ca8514
+ * 2.1.x (API version = 2004) also has the 3-arg API. */
ca8514
+#if APR_HAS_OPENLDAP_LDAPSDK && defined(LDAP_API_VERSION) && LDAP_API_VERSION >= 3001
ca8514
+
ca8514
+#define uldap_rebind_init(p) APR_SUCCESS /* noop */
ca8514
+
ca8514
+static int uldap_rebind_proc(LDAP *ld, const char *url, ber_tag_t request,
ca8514
+                             ber_int_t msgid, void *params)
ca8514
+{
ca8514
+    util_ldap_connection_t *ldc = params;
ca8514
+
ca8514
+    return ldap_bind_s(ld, ldc->binddn, ldc->bindpw, LDAP_AUTH_SIMPLE);
ca8514
+}
ca8514
+
ca8514
+static apr_status_t uldap_rebind_add(util_ldap_connection_t *ldc)
ca8514
+{
ca8514
+    ldap_set_rebind_proc(ldc->ldap, uldap_rebind_proc, ldc);
ca8514
+    return APR_SUCCESS;
ca8514
+}
ca8514
+
ca8514
+#else /* !APR_HAS_OPENLDAP_LDAPSDK */
ca8514
+
ca8514
+#define USE_APR_LDAP_REBIND
ca8514
+#include <apr_ldap_rebind.h>
ca8514
+
ca8514
+#define uldap_rebind_init(p) apr_ldap_rebind_init(p)
ca8514
+#define uldap_rebind_add(ldc) apr_ldap_rebind_add((ldc)->rebind_pool, \
ca8514
+                                                  (ldc)->ldap, (ldc)->binddn, \
ca8514
+                                                  (ldc)->bindpw)
ca8514
+#endif
ca8514
 
ca8514
 
ca8514
 /* ------------------------------------------------------------------ */
ca8514
@@ -181,6 +213,13 @@ static apr_status_t uldap_connection_unbind(void *param)
ca8514
     util_ldap_connection_t *ldc = param;
ca8514
 
ca8514
     if (ldc) {
ca8514
+#ifdef USE_APR_LDAP_REBIND
ca8514
+        /* forget the rebind info for this conn */
ca8514
+        if (ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
ca8514
+            apr_pool_clear(ldc->rebind_pool);
ca8514
+        }
ca8514
+#endif
ca8514
+
ca8514
         if (ldc->ldap) {
ca8514
             if (ldc->r) { 
ca8514
                 ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, ldc->r, "LDC %pp unbind", ldc); 
ca8514
@@ -189,12 +228,6 @@ static apr_status_t uldap_connection_unbind(void *param)
ca8514
             ldc->ldap = NULL;
ca8514
         }
ca8514
         ldc->bound = 0;
ca8514
-
ca8514
-        /* forget the rebind info for this conn */
ca8514
-        if (ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
ca8514
-            apr_ldap_rebind_remove(ldc->ldap);
ca8514
-            apr_pool_clear(ldc->rebind_pool);
ca8514
-        }
ca8514
     }
ca8514
 
ca8514
     return APR_SUCCESS;
ca8514
@@ -330,7 +363,7 @@ static int uldap_connection_init(request_rec *r,
ca8514
 
ca8514
     if (ldc->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
ca8514
         /* Now that we have an ldap struct, add it to the referral list for rebinds. */
ca8514
-        rc = apr_ldap_rebind_add(ldc->rebind_pool, ldc->ldap, ldc->binddn, ldc->bindpw);
ca8514
+        rc = uldap_rebind_add(ldc);
ca8514
         if (rc != APR_SUCCESS) {
ca8514
             ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server, APLOGNO(01277)
ca8514
                     "LDAP: Unable to add rebind cross reference entry. Out of memory?");
ca8514
@@ -855,6 +888,7 @@ static util_ldap_connection_t *
ca8514
         /* whether or not to keep this connection in the pool when it's returned */
ca8514
         l->keep = (st->connection_pool_ttl == 0) ? 0 : 1;
ca8514
 
ca8514
+#ifdef USE_APR_LDAP_REBIND
ca8514
         if (l->ChaseReferrals == AP_LDAP_CHASEREFERRALS_ON) {
ca8514
             if (apr_pool_create(&(l->rebind_pool), l->pool) != APR_SUCCESS) {
ca8514
                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01286)
ca8514
@@ -865,6 +899,7 @@ static util_ldap_connection_t *
ca8514
                 return NULL;
ca8514
             }
ca8514
         }
ca8514
+#endif
ca8514
 
ca8514
         if (p) {
ca8514
             p->next = l;
ca8514
@@ -3051,7 +3086,7 @@ static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog,
ca8514
     }
ca8514
 
ca8514
     /* Initialize the rebind callback's cross reference list. */
ca8514
-    apr_ldap_rebind_init (p);
ca8514
+    (void) uldap_rebind_init(p);
ca8514
 
ca8514
 #ifdef AP_LDAP_OPT_DEBUG
ca8514
     if (st->debug_level > 0) {