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