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