]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/bind.c
Drop unnecessary memset()s
[openldap] / servers / slapd / back-meta / bind.c
index 243a61587988004c3bdc29d8c552f9a7d232761f..0f450bef669f91bfcba697769825ada9b2aae134 100644 (file)
@@ -33,6 +33,8 @@
 #include "slap.h"
 #include "../back-ldap/back-ldap.h"
 #include "back-meta.h"
+#undef ldap_debug      /* silence a warning in ldap-int.h */
+#include "../../../libraries/libldap/ldap-int.h"
 
 #include "lutil_ldap.h"
 
@@ -166,9 +168,7 @@ meta_back_bind( Operation *op, SlapReply *rs )
                                        BER_BVZERO( &msc->msc_bound_ndn );
                                }
 
-                               if ( LDAP_BACK_SAVECRED( mi ) &&
-                                       !BER_BVISNULL( &msc->msc_cred ) )
-                               {
+                               if ( !BER_BVISNULL( &msc->msc_cred ) ) {
                                        /* destroy sensitive data */
                                        memset( msc->msc_cred.bv_val, 0,
                                                msc->msc_cred.bv_len );
@@ -219,6 +219,9 @@ retry_lock:;
                        }
 
                        assert( mc->mc_refcnt == 1 );
+#if META_BACK_PRINT_CONNTREE > 0
+                       meta_back_print_conntree( mi, ">>> meta_back_bind" );
+#endif /* META_BACK_PRINT_CONNTREE */
                        tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
                                meta_back_conndn_cmp );
                        assert( tmpmc == mc );
@@ -229,7 +232,7 @@ retry_lock:;
                                {
                                        Debug( LDAP_DEBUG_TRACE,
                                                "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
-                                               LDAP_BACK_PCONN_ID( mc->mc_conn ), mc->mc_refcnt, 0 );
+                                               LDAP_BACK_PCONN_ID( mc ), mc->mc_refcnt, 0 );
 
                                        if ( tmpmc->mc_refcnt != 0 ) {
                                                /* taint it */
@@ -248,15 +251,18 @@ retry_lock:;
 
                        ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
                        if ( isroot ) {
-                               mc->mc_conn = LDAP_BACK_PCONN_SET( op );
+                               LDAP_BACK_CONN_ISPRIV_SET( mc );
+                               LDAP_BACK_PCONN_SET( mc, op );
                        }
                        lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
                                meta_back_conndn_cmp, meta_back_conndn_dup );
+#if META_BACK_PRINT_CONNTREE > 0
+                       meta_back_print_conntree( mi, "<<< meta_back_bind" );
+#endif /* META_BACK_PRINT_CONNTREE */
                        ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
                        if ( lerr == -1 ) {
-                               meta_clear_candidates( op, mc );
-
                                /* we can do this because mc_refcnt == 1 */
+                               assert( mc->mc_refcnt == 1 );
                                mc->mc_refcnt = 0;
                                meta_back_conn_free( mc );
                                mc = NULL;
@@ -314,20 +320,66 @@ meta_back_bind_op_result(
        int                     nretries = mt->mt_nretries;
        char                    buf[ SLAP_TEXT_BUFLEN ];
 
+       Debug( LDAP_DEBUG_TRACE,
+               ">>> %s meta_back_bind_op_result[%d]\n",
+               op->o_log_prefix, candidate, 0 );
+
        if ( rs->sr_err == LDAP_SUCCESS ) {
+               time_t          stoptime = (time_t)(-1),
+                               timeout;
+               int             timeout_err = op->o_protocol >= LDAP_VERSION3 ?
+                               LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
+               const char      *timeout_text = "Operation timed out";
+               slap_op_t       opidx = slap_req2op( op->o_tag );
+
+               /* since timeout is not specified, compute and use
+                * the one specific to the ongoing operation */
+               if ( opidx == LDAP_REQ_SEARCH ) {
+                       if ( op->ors_tlimit <= 0 ) {
+                               timeout = 0;
+
+                       } else {
+                               timeout = op->ors_tlimit;
+                               timeout_err = LDAP_TIMELIMIT_EXCEEDED;
+                               timeout_text = NULL;
+                       }
+
+               } else {
+                       timeout = mt->mt_timeout[ opidx ];
+               }
+
+               /* better than nothing :) */
+               if ( timeout == 0 ) {
+                       if ( mi->mi_idle_timeout ) {
+                               timeout = mi->mi_idle_timeout;
+
+                       } else if ( mi->mi_conn_ttl ) {
+                               timeout = mi->mi_conn_ttl;
+                       }
+               }
+
+               if ( timeout ) {
+                       stoptime = op->o_time + timeout;
+               }
+
                LDAP_BACK_TV_SET( &tv );
 
                /*
                 * handle response!!!
                 */
 retry:;
-               switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
+               rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
+               switch ( rc ) {
                case 0:
+#if 0
                        Debug( LDAP_DEBUG_ANY,
                                "%s meta_back_bind_op_result[%d]: ldap_result=0 nretries=%d.\n",
                                op->o_log_prefix, candidate, nretries );
+#endif
 
-                       if ( nretries != META_RETRY_NEVER ) {
+                       if ( nretries != META_RETRY_NEVER 
+                               || ( timeout && slap_get_time() <= stoptime ) )
+                       {
                                ldap_pvt_thread_yield();
                                if ( nretries > 0 ) {
                                        nretries--;
@@ -336,12 +388,26 @@ retry:;
                                goto retry;
                        }
 
-                       rs->sr_err = LDAP_BUSY;
-                       (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
+                       /* don't let anyone else use this handler,
+                        * because there's a pending bind that will not
+                        * be acknowledged */
+                       ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
+                       assert( LDAP_BACK_CONN_BINDING( msc ) );
+
+#ifdef DEBUG_205
+                       Debug( LDAP_DEBUG_ANY, "### %s meta_back_bind_op_result ldap_unbind_ext[%d] ld=%p\n",
+                               op->o_log_prefix, candidate, (void *)msc->msc_ld );
+#endif /* DEBUG_205 */
+
+                       meta_clear_one_candidate( op, mc, candidate );
+                       ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+
+                       rs->sr_err = timeout_err;
+                       rs->sr_text = timeout_text;
                        break;
 
                case -1:
-                       ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
+                       ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE,
                                &rs->sr_err );
 
                        snprintf( buf, sizeof( buf ),
@@ -353,6 +419,11 @@ retry:;
                        break;
 
                default:
+                       /* only touch when activity actually took place... */
+                       if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
+                               msc->msc_time = op->o_time;
+                       }
+
                        /* FIXME: matched? referrals? response controls? */
                        rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
                                        NULL, NULL, NULL, NULL, 1 );
@@ -363,7 +434,13 @@ retry:;
                }
        }
 
-       return rs->sr_err = slap_map_api2result( rs );
+       rs->sr_err = slap_map_api2result( rs );
+
+       Debug( LDAP_DEBUG_TRACE,
+               "<<< %s meta_back_bind_op_result[%d] err=%d\n",
+               op->o_log_prefix, candidate, rs->sr_err );
+
+       return rs->sr_err;
 }
 
 /*
@@ -390,7 +467,7 @@ meta_back_single_bind(
                BER_BVZERO( &msc->msc_bound_ndn );
        }
 
-       if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
+       if ( !BER_BVISNULL( &msc->msc_cred ) ) {
                /* destroy sensitive data */
                memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
                ch_free( msc->msc_cred.bv_val );
@@ -442,6 +519,10 @@ meta_back_single_bind(
        mc->mc_authz_target = candidate;
 
        if ( LDAP_BACK_SAVECRED( mi ) ) {
+               if ( !BER_BVISNULL( &msc->msc_cred ) ) {
+                       memset( msc->msc_cred.bv_val, 0,
+                               msc->msc_cred.bv_len );
+               }
                ber_bvreplace( &msc->msc_cred, &op->orb_cred );
                ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
        }
@@ -494,6 +575,7 @@ meta_back_single_dobind(
                !op->o_do_not_cache &&
                ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
                        BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
+                       ( LDAP_BACK_CONN_ISPRIV( mc ) && dn_match( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN ) ) ||
                        ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
        {
                (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok );
@@ -560,7 +642,7 @@ meta_back_dobind(
        Debug( LDAP_DEBUG_TRACE,
                "%s meta_back_dobind: conn=%ld%s\n",
                op->o_log_prefix,
-               LDAP_BACK_PCONN_ID( mc->mc_conn ),
+               LDAP_BACK_PCONN_ID( mc ),
                isroot ? " (isroot)" : "" );
 
        /*
@@ -599,7 +681,8 @@ retry_binding:;
                        ++bound;
                        continue;
 
-               } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
+               } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) )
+               {
                        ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
                        ldap_pvt_thread_yield();
                        goto retry_binding;
@@ -626,7 +709,7 @@ retry_binding:;
 
 
                        if ( rc == LDAP_UNAVAILABLE ) {
-                               /* FIXME: meta_back_retry() already calls
+                               /* FIXME: meta_back_retry() already re-calls
                                 * meta_back_single_dobind() */
                                if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
                                        goto retry_ok;
@@ -636,6 +719,7 @@ retry_binding:;
                                        ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
                                        LDAP_BACK_CONN_BINDING_CLEAR( msc );
                                        ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+                                       meta_back_release_conn( op, mc );
                                }
 
                                return 0;
@@ -691,7 +775,7 @@ retry_ok:;
 done:;
        Debug( LDAP_DEBUG_TRACE,
                "%s meta_back_dobind: conn=%ld bound=%d\n",
-               op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
+               op->o_log_prefix, LDAP_BACK_PCONN_ID( mc ), bound );
 
        if ( bound == 0 ) {
                meta_back_release_conn( op, mc );
@@ -782,23 +866,29 @@ meta_back_cancel(
        metatarget_t            *mt = mi->mi_targets[ candidate ];
        metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
 
+       int                     rc = LDAP_OTHER;
+
+       Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
+               op->o_log_prefix, candidate, msgid );
+
        /* default behavior */
        if ( META_BACK_TGT_ABANDON( mt ) ) {
-               return ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
-       }
+               rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
 
-       if ( META_BACK_TGT_IGNORE( mt ) ) {
-               return LDAP_SUCCESS;
-       }
+       } else if ( META_BACK_TGT_IGNORE( mt ) ) {
+               rc = ldap_pvt_discard( msc->msc_ld, msgid );
 
-       if ( META_BACK_TGT_CANCEL( mt ) ) {
-               /* FIXME: asynchronous? */
-               return ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
+       } else if ( META_BACK_TGT_CANCEL( mt ) ) {
+               rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
+
+       } else {
+               assert( 0 );
        }
 
-       assert( 0 );
+       Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
+               op->o_log_prefix, candidate, rc );
 
-       return LDAP_OTHER;
+       return rc;
 }
 
 
@@ -808,79 +898,175 @@ meta_back_cancel(
  */
 int
 meta_back_op_result(
-       metaconn_t      *mc,
-       Operation       *op,
-       SlapReply       *rs,
-       int             candidate )
+       metaconn_t              *mc,
+       Operation               *op,
+       SlapReply               *rs,
+       int                     candidate,
+       ber_int_t               msgid,
+       time_t                  timeout,
+       ldap_back_send_t        sendok )
 {
-       metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
+       metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
+
+       const char      *save_text = rs->sr_text,
+                       *save_matched = rs->sr_matched;
+       BerVarray       save_ref = rs->sr_ref;
+       LDAPControl     **save_ctrls = rs->sr_ctrls;
+       void            *matched_ctx = NULL;
 
-       int                     i,
-                               rerr = LDAP_SUCCESS;
-       char                    *rmsg = NULL,
-                               *rmatch = NULL;
-       const char              *save_rmsg = NULL,
-                               *save_rmatch = NULL;
-       void                    *rmatch_ctx = NULL;
+       char            *matched = NULL;
+       char            *text = NULL;
+       char            **refs = NULL;
+       LDAPControl     **ctrls = NULL;
 
        assert( mc != NULL );
 
+       rs->sr_text = NULL;
+       rs->sr_matched = NULL;
+       rs->sr_ref = NULL;
+       rs->sr_ctrls = NULL;
+
        if ( candidate != META_TARGET_NONE ) {
+               metatarget_t            *mt = mi->mi_targets[ candidate ];
                metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
 
-               rs->sr_err = LDAP_SUCCESS;
+#define        ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
 
-               ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
-               if ( rs->sr_err != LDAP_SUCCESS ) {
-                       /*
-                        * better check the type of error. In some cases
-                        * (search ?) it might be better to return a
-                        * success if at least one of the targets gave
-                        * positive result ...
-                        */
-                       ldap_get_option( msc->msc_ld,
-                                       LDAP_OPT_ERROR_STRING, &rmsg );
-                       if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
-                               ldap_memfree( rmsg );
-                               rmsg = NULL;
+               if ( ERR_OK( rs->sr_err ) ) {
+                       int             rc;
+                       struct timeval  tv;
+                       LDAPMessage     *res = NULL;
+                       time_t          stoptime = (time_t)(-1);
+                       int             timeout_err = op->o_protocol >= LDAP_VERSION3 ?
+                                               LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
+                       const char      *timeout_text = "Operation timed out";
+
+                       /* if timeout is not specified, compute and use
+                        * the one specific to the ongoing operation */
+                       if ( timeout == (time_t)(-1) ) {
+                               slap_op_t       opidx = slap_req2op( op->o_tag );
+
+                               if ( opidx == SLAP_OP_SEARCH ) {
+                                       if ( op->ors_tlimit <= 0 ) {
+                                               timeout = 0;
+
+                                       } else {
+                                               timeout = op->ors_tlimit;
+                                               timeout_err = LDAP_TIMELIMIT_EXCEEDED;
+                                               timeout_text = NULL;
+                                       }
+
+                               } else {
+                                       timeout = mt->mt_timeout[ opidx ];
+                               }
+                       }
+
+                       /* better than nothing :) */
+                       if ( timeout == 0 ) {
+                               if ( mi->mi_idle_timeout ) {
+                                       timeout = mi->mi_idle_timeout;
+
+                               } else if ( mi->mi_conn_ttl ) {
+                                       timeout = mi->mi_conn_ttl;
+                               }
                        }
 
-                       ldap_get_option( msc->msc_ld,
-                                       LDAP_OPT_MATCHED_DN, &rmatch );
-                       if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
-                               ldap_memfree( rmatch );
-                               rmatch = NULL;
+                       if ( timeout ) {
+                               stoptime = op->o_time + timeout;
                        }
 
-                       rerr = rs->sr_err = slap_map_api2result( rs );
+                       LDAP_BACK_TV_SET( &tv );
+
+retry:;
+                       rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
+                       switch ( rc ) {
+                       case 0:
+                               if ( timeout && slap_get_time() > stoptime ) {
+                                       (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
+                                       rs->sr_err = timeout_err;
+                                       rs->sr_text = timeout_text;
+                                       break;
+                               }
 
-                       if ( LogTest( LDAP_DEBUG_ANY ) ) {
-                               char    buf[ SLAP_TEXT_BUFLEN ];
+                               LDAP_BACK_TV_SET( &tv );
+                               ldap_pvt_thread_yield();
+                               goto retry;
+
+                       case -1:
+                               ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE,
+                                               &rs->sr_err );
+                               break;
 
-                               snprintf( buf, sizeof( buf ),
-                                       "meta_back_op_result[%d] "
-                                       "err=%d text=\"%s\" matched=\"%s\"", 
-                                       candidate, rs->sr_err,
-                                       ( rmsg ? rmsg : "" ),
-                                       ( rmatch ? rmatch : "" ) );
-                               Debug( LDAP_DEBUG_ANY, "%s %s.\n",
-                                       op->o_log_prefix, buf, 0 );
+
+                       /* otherwise get the result; if it is not
+                        * LDAP_SUCCESS, record it in the reply
+                        * structure (this includes 
+                        * LDAP_COMPARE_{TRUE|FALSE}) */
+                       default:
+                               /* only touch when activity actually took place... */
+                               if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
+                                       msc->msc_time = op->o_time;
+                               }
+
+                               rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
+                                               &matched, &text, &refs, &ctrls, 1 );
+                               res = NULL;
+                               rs->sr_text = text;
+                               if ( rc != LDAP_SUCCESS ) {
+                                       rs->sr_err = rc;
+                               }
+                               if ( refs != NULL ) {
+                                       int     i;
+       
+                                       for ( i = 0; refs[ i ] != NULL; i++ )
+                                               /* count */ ;
+                                       rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
+                                               op->o_tmpmemctx );
+                                       for ( i = 0; refs[ i ] != NULL; i++ ) {
+                                               ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
+                                       }
+                                       BER_BVZERO( &rs->sr_ref[ i ] );
+                               }
+                               if ( ctrls != NULL ) {
+                                       rs->sr_ctrls = ctrls;
+                               }
                        }
+
+                       assert( res == NULL );
                }
 
-               if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ candidate ] ) ) {
+               /* if the error in the reply structure is not
+                * LDAP_SUCCESS, try to map it from client 
+                * to server error */
+               if ( !ERR_OK( rs->sr_err ) ) {
+                       rs->sr_err = slap_map_api2result( rs );
+
+                       /* internal ops ( op->o_conn == NULL ) 
+                        * must not reply to client */
+                       if ( op->o_conn && !op->o_do_not_cache && matched ) {
+
+                               /* record the (massaged) matched
+                                * DN into the reply structure */
+                               rs->sr_matched = matched;
+                       }
+               }
+
+               if ( META_BACK_TGT_QUARANTINE( mt ) ) {
                        meta_back_quarantine( op, rs, candidate );
                }
 
        } else {
+               int     i,
+                       err = rs->sr_err;
+
                for ( i = 0; i < mi->mi_ntargets; i++ ) {
                        metasingleconn_t        *msc = &mc->mc_conns[ i ];
-                       char                    *msg = NULL;
-                       char                    *match = NULL;
+                       char                    *xtext = NULL;
+                       char                    *xmatched = NULL;
 
                        rs->sr_err = LDAP_SUCCESS;
 
-                       ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
+                       ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE, &rs->sr_err );
                        if ( rs->sr_err != LDAP_SUCCESS ) {
                                /*
                                 * better check the type of error. In some cases
@@ -889,17 +1075,17 @@ meta_back_op_result(
                                 * positive result ...
                                 */
                                ldap_get_option( msc->msc_ld,
-                                               LDAP_OPT_ERROR_STRING, &msg );
-                               if ( msg != NULL && msg[ 0 ] == '\0' ) {
-                                       ldap_memfree( msg );
-                                       msg = NULL;
+                                               LDAP_OPT_DIAGNOSTIC_MESSAGE, &xtext );
+                               if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
+                                       ldap_memfree( xtext );
+                                       xtext = NULL;
                                }
 
                                ldap_get_option( msc->msc_ld,
-                                               LDAP_OPT_MATCHED_DN, &match );
-                               if ( match != NULL && match[ 0 ] == '\0' ) {
-                                       ldap_memfree( match );
-                                       match = NULL;
+                                               LDAP_OPT_MATCHED_DN, &xmatched );
+                               if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
+                                       ldap_memfree( xmatched );
+                                       xmatched = NULL;
                                }
 
                                rs->sr_err = slap_map_api2result( rs );
@@ -911,41 +1097,41 @@ meta_back_op_result(
                                                "meta_back_op_result[%d] "
                                                "err=%d text=\"%s\" matched=\"%s\"", 
                                                i, rs->sr_err,
-                                               ( rmsg ? rmsg : "" ),
-                                               ( rmatch ? rmatch : "" ) );
+                                               ( xtext ? xtext : "" ),
+                                               ( xmatched ? xmatched : "" ) );
                                        Debug( LDAP_DEBUG_ANY, "%s %s.\n",
                                                op->o_log_prefix, buf, 0 );
                                }
-       
+
                                /*
                                 * FIXME: need to rewrite "match" (need rwinfo)
                                 */
                                switch ( rs->sr_err ) {
                                default:
-                                       rerr = rs->sr_err;
-                                       if ( msg != NULL ) {
-                                               if ( rmsg ) {
-                                                       ldap_memfree( rmsg );
+                                       err = rs->sr_err;
+                                       if ( xtext != NULL ) {
+                                               if ( text ) {
+                                                       ldap_memfree( text );
                                                }
-                                               rmsg = msg;
-                                               msg = NULL;
+                                               text = xtext;
+                                               xtext = NULL;
                                        }
-                                       if ( match != NULL ) {
-                                               if ( rmatch ) {
-                                                       ldap_memfree( rmatch );
+                                       if ( xmatched != NULL ) {
+                                               if ( matched ) {
+                                                       ldap_memfree( matched );
                                                }
-                                               rmatch = match;
-                                               match = NULL;
+                                               matched = xmatched;
+                                               xmatched = NULL;
                                        }
                                        break;
                                }
 
-                               if ( msg ) {
-                                       ldap_memfree( msg );
+                               if ( xtext ) {
+                                       ldap_memfree( xtext );
                                }
        
-                               if ( match ) {
-                                       ldap_memfree( match );
+                               if ( xmatched ) {
+                                       ldap_memfree( xmatched );
                                }
                        }
 
@@ -953,36 +1139,52 @@ meta_back_op_result(
                                meta_back_quarantine( op, rs, i );
                        }
                }
+
+               if ( err != LDAP_SUCCESS ) {
+                       rs->sr_err = err;
+               }
        }
-       
-       rs->sr_err = rerr;
-       if ( rmsg != NULL ) {
-               save_rmsg = rs->sr_text;
-               rs->sr_text = rmsg;
-       }
-       if ( rmatch != NULL ) {
+
+       if ( matched != NULL ) {
                struct berval   dn, pdn;
 
-               ber_str2bv( rmatch, 0, 0, &dn );
+               ber_str2bv( matched, 0, 0, &dn );
                if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
-                       ldap_memfree( rmatch );
-                       rmatch_ctx = op->o_tmpmemctx;
-                       rmatch = pdn.bv_val;
+                       ldap_memfree( matched );
+                       matched_ctx = op->o_tmpmemctx;
+                       matched = pdn.bv_val;
                }
-               save_rmatch = rs->sr_matched;
-               rs->sr_matched = rmatch;
+               rs->sr_matched = matched;
+       }
+
+       if ( op->o_conn &&
+               ( ( sendok & LDAP_BACK_SENDOK ) 
+                       || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
+       {
+               send_ldap_result( op, rs );
        }
-       send_ldap_result( op, rs );
-       if ( rmsg != NULL ) {
-               ber_memfree( rmsg );
-               rs->sr_text = save_rmsg;
+       if ( matched ) {
+               op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
        }
-       if ( rmatch != NULL ) {
-               ber_memfree_x( rmatch, rmatch_ctx );
-               rs->sr_matched = save_rmatch;
+       if ( text ) {
+               ldap_memfree( text );
        }
+       if ( rs->sr_ref ) {
+               assert( refs != NULL );
+               ber_memvfree( (void **)refs );
+               op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
+       }
+       if ( ctrls ) {
+               assert( rs->sr_ctrls != NULL );
+               ldap_controls_free( ctrls );
+       }
+
+       rs->sr_text = save_text;
+       rs->sr_matched = save_matched;
+       rs->sr_ref = save_ref;
+       rs->sr_ctrls = save_ctrls;
 
-       return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
+       return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
 }
 
 /*