]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/request.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / libldap / request.c
index 31d72ca0e2835b810fcc0d39de133b16d4707d3a..071391d9abc1e3acb7f8fcd1a8e8e85bdaecad6c 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2008 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "ldap-int.h"
 #include "lber.h"
 
+/* used by ldap_send_server_request and ldap_new_connection */
+#ifdef LDAP_R_COMPILE
+#define LDAP_CONN_LOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_conn_mutex ); }
+#define LDAP_CONN_UNLOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex ); }
+#define LDAP_REQ_LOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_req_mutex ); }
+#define LDAP_REQ_UNLOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex ); }
+#define LDAP_RES_LOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_res_mutex ); }
+#define LDAP_RES_UNLOCK_IF(nolock) \
+       { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex ); }
+#else
+#define LDAP_CONN_LOCK_IF(nolock)
+#define LDAP_CONN_UNLOCK_IF(nolock)
+#define LDAP_REQ_LOCK_IF(nolock)
+#define LDAP_REQ_UNLOCK_IF(nolock)
+#define LDAP_RES_LOCK_IF(nolock)
+#define LDAP_RES_UNLOCK_IF(nolock)
+#endif
+
 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
 static void ldap_free_request_int LDAP_P(( LDAP *ld, LDAPRequest *lr ));
@@ -82,10 +105,12 @@ ldap_alloc_ber_with_options( LDAP *ld )
 void
 ldap_set_ber_options( LDAP *ld, BerElement *ber )
 {
+       /* ld_lberoptions is constant, hence no lock */
        ber->ber_options = ld->ld_lberoptions;
 }
 
 
+/* sets needed mutexes - no mutexes set to this point */
 ber_int_t
 ldap_send_initial_request(
        LDAP *ld,
@@ -95,22 +120,21 @@ ldap_send_initial_request(
        ber_int_t msgid)
 {
        int rc = 1;
+       ber_socket_t sd = AC_SOCKET_INVALID;
 
        Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
 
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
-       if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
+       LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
+       if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ) == -1 ) {
                /* not connected yet */
                rc = ldap_open_defconn( ld );
 
        }
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
+       if ( ld->ld_defconn && ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING )
+               rc = ldap_int_check_async_open( ld, sd );
        if( rc < 0 ) {
                ber_free( ber, 1 );
+               LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
                return( -1 );
        } else if ( rc == 0 ) {
                Debug( LDAP_DEBUG_TRACE,
@@ -121,27 +145,33 @@ ldap_send_initial_request(
 #ifdef LDAP_CONNECTIONLESS
        if (LDAP_IS_UDP(ld)) {
                if (msgtype == LDAP_REQ_BIND) {
+                       LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
                        if (ld->ld_options.ldo_cldapdn)
                                ldap_memfree(ld->ld_options.ldo_cldapdn);
                        ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
+                       ber_free( ber, 1 );
+                       LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
+                       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
                        return 0;
                }
                if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
+               {
+                       ber_free( ber, 1 );
+                       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
                        return LDAP_PARAM_ERROR;
+               }
        }
 #endif
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
+       LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
        rc = ldap_send_server_request( ld, ber, msgid, NULL,
-               NULL, NULL, NULL );
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
+               NULL, NULL, NULL, 0, 0 );
+       LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
+       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
        return(rc);
 }
 
 
+/* protected by conn_mutex */
 int
 ldap_int_flush_request(
        LDAP *ld,
@@ -149,6 +179,7 @@ ldap_int_flush_request(
 {
        LDAPConn *lc = lr->lr_conn;
 
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
        if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
                if ( sock_errno() == EAGAIN ) {
                        /* need to continue write later */
@@ -171,10 +202,18 @@ ldap_int_flush_request(
 
                /* sent -- waiting for a response */
                ldap_mark_select_read( ld, lc->lconn_sb );
+               ldap_clear_select_write( ld, lc->lconn_sb );
        }
        return 0;
 }
 
+/*
+ * protected by req_mutex
+ * if m_noconn then protect using conn_lock
+ * else already protected with conn_lock
+ * if m_res then also protected by res_mutex
+ */
+
 int
 ldap_send_server_request(
        LDAP *ld,
@@ -183,16 +222,20 @@ ldap_send_server_request(
        LDAPRequest *parentreq,
        LDAPURLDesc **srvlist,
        LDAPConn *lc,
-       LDAPreqinfo *bind )
+       LDAPreqinfo *bind,
+       int m_noconn,
+       int m_res )
 {
        LDAPRequest     *lr;
        int             incparent, rc;
 
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
        Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
 
        incparent = 0;
        ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
 
+       LDAP_CONN_LOCK_IF(m_noconn);
        if ( lc == NULL ) {
                if ( srvlist == NULL ) {
                        lc = ld->ld_defconn;
@@ -204,7 +247,8 @@ ldap_send_server_request(
                                        incparent = 1;
                                        ++parentreq->lr_outrefcnt;
                                }
-                               lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
+                               lc = ldap_new_connection( ld, srvlist, 0,
+                                       1, bind, 1, m_res );
                        }
                }
        }
@@ -227,11 +271,13 @@ ldap_send_server_request(
                        /* async only occurs if a network timeout is set */
 
                        /* honor network timeout */
+                       LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
                        if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
                        {
                                /* caller will have to call again */
                                ld->ld_errno = LDAP_X_CONNECTING;
                        }
+                       LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
                        /* fallthru */
 
                default:
@@ -250,6 +296,7 @@ ldap_send_server_request(
                        /* Forget about the bind */
                        --parentreq->lr_outrefcnt; 
                }
+               LDAP_CONN_UNLOCK_IF(m_noconn);
                return( -1 );
        }
 
@@ -259,10 +306,13 @@ ldap_send_server_request(
        if ( LDAP_IS_UDP( ld )) {
                BerElement tmpber = *ber;
                ber_rewind( &tmpber );
+               LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
                rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
                        sizeof( struct sockaddr ), 0 );
+               LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
                if ( rc == -1 ) {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
+                       LDAP_CONN_UNLOCK_IF(m_noconn);
                        return rc;
                }
        }
@@ -280,7 +330,10 @@ ldap_send_server_request(
        {
                rc = -1;
        }
-       if ( rc ) return rc;
+       if ( rc ) {
+               LDAP_CONN_UNLOCK_IF(m_noconn);
+               return rc;
+       }
 
        lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
        if ( lr == NULL ) {
@@ -291,6 +344,7 @@ ldap_send_server_request(
                        /* Forget about the bind */
                        --parentreq->lr_outrefcnt; 
                }
+               LDAP_CONN_UNLOCK_IF(m_noconn);
                return( -1 );
        } 
        lr->lr_msgid = msgid;
@@ -349,16 +403,48 @@ ldap_send_server_request(
                msgid = -1;
        }
 
+       LDAP_CONN_UNLOCK_IF(m_noconn);
        return( msgid );
 }
 
+/* return 0 if no StartTLS ext, 1 if present, 2 if critical */
+static int
+find_tls_ext( LDAPURLDesc *srv )
+{
+       int i, crit;
+       char *ext;
+
+       if ( !srv->lud_exts )
+               return 0;
+
+       for (i=0; srv->lud_exts[i]; i++) {
+               crit = 0;
+               ext = srv->lud_exts[i];
+               if ( ext[0] == '!') {
+                       ext++;
+                       crit = 1;
+               }
+               if ( !strcasecmp( ext, "StartTLS" ) ||
+                       !strcasecmp( ext, "X-StartTLS" ) ||
+                       !strcmp( ext, LDAP_EXOP_START_TLS )) {
+                       return crit + 1;
+               }
+       }
+       return 0;
+}
+
+/*
+ * always protected by conn_mutex
+ * optionally protected by req_mutex and res_mutex
+ */
 LDAPConn *
 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
-       int connect, LDAPreqinfo *bind )
+       int connect, LDAPreqinfo *bind, int m_req, int m_res )
 {
        LDAPConn        *lc;
        int             async = 0;
 
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
        Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
                use_ldsb, connect, (bind != NULL) );
        /*
@@ -417,14 +503,38 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
        }
 
        lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
-#endif
        lc->lconn_next = ld->ld_conns;
        ld->ld_conns = lc;
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+
+       if ( connect ) {
+#ifdef HAVE_TLS
+               if ( lc->lconn_server->lud_exts ) {
+                       int rc, ext = find_tls_ext( lc->lconn_server );
+                       if ( ext ) {
+                               LDAPConn        *savedefconn;
+
+                               savedefconn = ld->ld_defconn;
+                               ++lc->lconn_refcnt;     /* avoid premature free */
+                               ld->ld_defconn = lc;
+
+                               LDAP_REQ_UNLOCK_IF(m_req);
+                               LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
+                               LDAP_RES_UNLOCK_IF(m_res);
+                               rc = ldap_start_tls_s( ld, NULL, NULL );
+                               LDAP_RES_LOCK_IF(m_res);
+                               LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
+                               LDAP_REQ_LOCK_IF(m_req);
+                               ld->ld_defconn = savedefconn;
+                               --lc->lconn_refcnt;
+
+                               if ( rc != LDAP_SUCCESS && ext == 2 ) {
+                                       ldap_free_connection( ld, lc, 1, 0 );
+                                       return NULL;
+                               }
+                       }
+               }
 #endif
+       }
 
        if ( bind != NULL ) {
                int             err = 0;
@@ -449,17 +559,15 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
                                ld->ld_defconn = lc;
 
                                Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
-#ifdef LDAP_R_COMPILE
-                               ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-                               ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
-#endif
+                               LDAP_REQ_UNLOCK_IF(m_req);
+                               LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
+                               LDAP_RES_UNLOCK_IF(m_res);
                                err = (*ld->ld_rebind_proc)( ld,
                                        bind->ri_url, bind->ri_request, bind->ri_msgid,
                                        ld->ld_rebind_params );
-#ifdef LDAP_R_COMPILE
-                               ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
-                               ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
+                               LDAP_RES_LOCK_IF(m_res);
+                               LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
+                               LDAP_REQ_LOCK_IF(m_req);
 
                                ld->ld_defconn = savedefconn;
                                --lc->lconn_refcnt;
@@ -484,10 +592,9 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
                                "anonymous rebind via ldap_sasl_bind(\"\")\n",
                                0, 0, 0);
 
-#ifdef LDAP_R_COMPILE
-                       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-                       ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
-#endif
+                       LDAP_REQ_UNLOCK_IF(m_req);
+                       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
+                       LDAP_RES_UNLOCK_IF(m_res);
                        rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
                                NULL, NULL, &msgid );
                        if ( rc != LDAP_SUCCESS ) {
@@ -531,10 +638,9 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
                                        }
                                }
                        }
-#ifdef LDAP_R_COMPILE
-                       ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
-                       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
+                       LDAP_RES_LOCK_IF(m_res);
+                       LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
+                       LDAP_REQ_LOCK_IF(m_req);
                        ld->ld_defconn = savedefconn;
                        --lc->lconn_refcnt;
 
@@ -546,11 +652,11 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
                if ( lc != NULL )
                        lc->lconn_rebind_inprogress = 0;
        }
-
        return( lc );
 }
 
 
+/* protected by ld_conn_mutex */
 static LDAPConn *
 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
 /*
@@ -563,9 +669,7 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
        int lcu_port, lsu_port;
        int found = 0;
 
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
-#endif
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
        for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
                lcu = lc->lconn_server;
                lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
@@ -577,8 +681,7 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
 
                        if ( lsu_port == lcu_port
                                && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
-                               && lcu->lud_host != NULL && *lcu->lud_host != '\0'
-                               && lsu->lud_host != NULL && *lsu->lud_host != '\0'
+                               && lcu->lud_host != NULL && lsu->lud_host != NULL
                                && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
                        {
                                found = 1;
@@ -590,36 +693,34 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
                if ( found )
                        break;
        }
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
-#endif
        return lc;
 }
 
 
 
+/* protected by ld_conn_mutex */
 static void
 use_connection( LDAP *ld, LDAPConn *lc )
 {
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
        ++lc->lconn_refcnt;
        lc->lconn_lastused = time( NULL );
 }
 
 
+/* protected by ld_conn_mutex */
 void
 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
 {
        LDAPConn        *tmplc, *prevlc;
 
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
        Debug( LDAP_DEBUG_TRACE,
                "ldap_free_connection %d %d\n",
                force, unbind, 0 );
 
        if ( force || --lc->lconn_refcnt <= 0 ) {
                /* remove from connections list first */
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
-#endif
 
                for ( prevlc = NULL, tmplc = ld->ld_conns;
                        tmplc != NULL;
@@ -638,9 +739,32 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                        }
                        prevlc = tmplc;
                }
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
-#endif
+
+               /* process connection callbacks */
+               {
+                       struct ldapoptions *lo;
+                       ldaplist *ll;
+                       ldap_conncb *cb;
+
+                       lo = &ld->ld_options;
+                       LDAP_MUTEX_LOCK( &lo->ldo_mutex );
+                       if ( lo->ldo_conn_cbs ) {
+                               for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
+                                       cb = ll->ll_data;
+                                       cb->lc_del( ld, lc->lconn_sb, cb );
+                               }
+                       }
+                       LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
+                       lo = LDAP_INT_GLOBAL_OPT();
+                       LDAP_MUTEX_LOCK( &lo->ldo_mutex );
+                       if ( lo->ldo_conn_cbs ) {
+                               for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
+                                       cb = ll->ll_data;
+                                       cb->lc_del( ld, lc->lconn_sb, cb );
+                               }
+                       }
+                       LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
+               }
 
                if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
                        ldap_mark_select_clear( ld, lc->lconn_sb );
@@ -655,6 +779,9 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                }
 
                ldap_int_sasl_close( ld, lc );
+#ifdef HAVE_GSSAPI
+               ldap_int_gssapi_close( ld, lc );
+#endif
 
                ldap_free_urllist( lc->lconn_server );
 
@@ -704,6 +831,7 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
 }
 
 
+/* Protects self with ld_conn_mutex */
 #ifdef LDAP_DEBUG
 void
 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
@@ -712,6 +840,7 @@ ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
        char            timebuf[32];
 
        Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
+       LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
        for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
                if ( lc->lconn_server != NULL ) {
                        Debug( LDAP_DEBUG_TRACE, "* host: %s  port: %d%s\n",
@@ -748,9 +877,11 @@ ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
                        break;
                }
        }
+       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
 }
 
 
+/* protected by req_mutex and res_mutex */
 void
 ldap_dump_requests_and_responses( LDAP *ld )
 {
@@ -799,9 +930,11 @@ ldap_dump_requests_and_responses( LDAP *ld )
 }
 #endif /* LDAP_DEBUG */
 
+/* protected by req_mutex */
 static void
 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
 {
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
        /* if lr_refcnt > 0, the request has been looked up 
         * by ldap_find_request_by_msgid(); if in the meanwhile
         * the request is free()'d by someone else, just decrease
@@ -852,13 +985,11 @@ ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
        LDAP_FREE( lr );
 }
 
+/* protected by req_mutex */
 void
 ldap_free_request( LDAP *ld, LDAPRequest *lr )
 {
-#ifdef LDAP_R_COMPILE
-       LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
-#endif
-
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
        Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
                lr->lr_origid, lr->lr_msgid, 0 );
 
@@ -926,6 +1057,8 @@ static int ldap_int_nextref(
  *  (OUT) hadrefp = 1 if sucessfully followed referral
  *
  * Return value - number of referrals followed
+ *
+ * Protected by res_mutex, conn_mutex and req_mutex    (try_read1msg)
  */
 int
 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
@@ -939,12 +1072,16 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
        LDAPConn        *lc;
        int                      rc, count, i, j, id;
        LDAPreqinfo  rinfo;
+       LDAP_NEXTREF_PROC       *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
+
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
+       Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
 
        ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
        *hadrefp = 0;
 
-       Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
-
        unfollowed = NULL;
        rc = count = 0;
 
@@ -974,15 +1111,12 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
        refarray = refs;
        refs = NULL;
 
-       if ( ld->ld_nextref_proc == NULL ) {
-               ld->ld_nextref_proc = ldap_int_nextref;
-       }
-
        /* parse out & follow referrals */
+       /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
        i = -1;
-       for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
+       for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
                        i != -1;
-                       ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
+                       nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
        {
 
                /* Parse the referral URL */
@@ -996,10 +1130,18 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
                }
 
                if( srv->lud_crit_exts ) {
-                       /* we do not support any extensions */
-                       ld->ld_errno = LDAP_NOT_SUPPORTED;
-                       rc = -1;
-                       goto done;
+                       int ok = 0;
+#ifdef HAVE_TLS
+                       /* If StartTLS is the only critical ext, OK. */
+                       if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
+                               ok = 1;
+#endif
+                       if ( !ok ) {
+                               /* we do not support any other extensions */
+                               ld->ld_errno = LDAP_NOT_SUPPORTED;
+                               rc = -1;
+                               goto done;
+                       }
                }
 
                /* check connection for re-bind in progress */
@@ -1007,7 +1149,7 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
                        /* See if we've already requested this DN with this conn */
                        LDAPRequest *lp;
                        int looped = 0;
-                       int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
+                       ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
                        for ( lp = origreq; lp; ) {
                                if ( lp->lr_conn == lc
                                        && len == lp->lr_dn.bv_len
@@ -1111,14 +1253,8 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
                /* Send the new request to the server - may require a bind */
                rinfo.ri_msgid = origreq->lr_origid;
                rinfo.ri_url = refarray[i];
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
                rc = ldap_send_server_request( ld, ber, id,
-                       origreq, &srv, NULL, &rinfo );
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
+                       origreq, &srv, NULL, &rinfo, 0, 1 );
                if ( rc < 0 ) {
                        /* Failure, try next referral in the list */
                        Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n", 
@@ -1139,6 +1275,7 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char *
                                if ( lc == NULL ) {
                                        ld->ld_errno = LDAP_OPERATIONS_ERROR;
                                        rc = -1;
+                                       LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
                                        goto done;
                                }
                        }
@@ -1184,6 +1321,7 @@ done:
 
 /*
  * XXX merging of errors in this routine needs to be improved
+ * Protected by res_mutex, conn_mutex and req_mutex    (try_read1msg)
  */
 int
 ldap_chase_referrals( LDAP *ld,
@@ -1201,6 +1339,9 @@ ldap_chase_referrals( LDAP *ld,
        LDAPreqinfo  rinfo;
        LDAPConn        *lc;
 
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
+       LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
        Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
 
        ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
@@ -1266,7 +1407,7 @@ ldap_chase_referrals( LDAP *ld,
                if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
                        LDAPRequest *lp;
                        int looped = 0;
-                       int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
+                       ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
                        for ( lp = lr; lp; lp = lp->lr_parent ) {
                                if ( lp->lr_conn == lc
                                        && len == lp->lr_dn.bv_len )
@@ -1298,15 +1439,8 @@ ldap_chase_referrals( LDAP *ld,
 
                rinfo.ri_msgid = origreq->lr_origid;
 
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
                rc = ldap_send_server_request( ld, ber, id,
-                       lr, &srv, NULL, &rinfo );
-#ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
-
+                       lr, &srv, NULL, &rinfo, 0, 1 );
                LDAP_FREE( rinfo.ri_url );
 
                if( rc >= 0 ) {
@@ -1496,14 +1630,12 @@ re_encode_request( LDAP *ld,
 }
 
 
+/* protected by req_mutex */
 LDAPRequest *
 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
 {
        LDAPRequest     *lr;
 
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
                if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
                        continue;       /* Skip completed requests */
@@ -1513,21 +1645,16 @@ ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
                        break;
                }
        }
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
 
        return( lr );
 }
 
+/* protected by req_mutex */
 void
 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
 {
        LDAPRequest     *lr;
 
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
-#endif
        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
                if ( lr == lrx ) {
                        if ( lr->lr_refcnt > 0 ) {
@@ -1548,7 +1675,4 @@ ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
        } else if ( freeit ) {
                ldap_free_request( ld, lrx );
        }
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
-#endif
 }