]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_ops.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / servers / slapd / slapi / slapi_ops.c
index 5a3bb7e74fb6f32450fc0908e4d3d9e63da4320f..19f41961afb2ee1fbf7b8b0b3110e7e39cb34e45 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2002-2005 The OpenLDAP Foundation.
+ * Copyright 2002-2012 The OpenLDAP Foundation.
  * Portions Copyright 1997,2002-2003 IBM Corporation.
  * All rights reserved.
  *
 #include <lber_pvt.h>
 #include <slapi.h>
 
-/*
- * use a fake listener when faking a connection,
- * so it can be used in ACLs
- */
-static struct slap_listener slap_unknown_listener = {
-       BER_BVC("unknown"),     /* FIXME: use a URI form? (e.g. slapi://) */
-       BER_BVC("UNKNOWN")
+#ifdef LDAP_SLAPI
+
+static struct Listener slapi_listener = {
+       BER_BVC("slapi://"),
+       BER_BVC("slapi://")
 };
 
-static void
-slapi_int_send_ldap_result_shim(
-       Operation       *op, 
-       SlapReply       *rs )
+static LDAPControl **
+slapi_int_dup_controls( LDAPControl **controls )
 {
-       LDAPControl             **controls = NULL;
-       size_t                  i;
-       plugin_result_callback  prc = NULL;
-       void                    *callback_data = NULL;
+       LDAPControl **c;
+       size_t i;
 
-       assert( op->o_pb != NULL );
+       if ( controls == NULL )
+               return NULL;
+
+       for ( i = 0; controls[i] != NULL; i++ )
+               ;
 
-       slapi_pblock_get( op->o_pb, SLAPI_RESCONTROLS, (void **)&controls );
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_RESULT_CALLBACK, (void **)&prc );
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
+       c = (LDAPControl **) slapi_ch_calloc( i + 1, sizeof(LDAPControl *) );
 
-       assert( controls == NULL );
+       for ( i = 0; controls[i] != NULL; i++ ) {
+               c[i] = slapi_dup_control( controls[i] );
+       }
 
-       /* Copy these before they go out of scope */
-       if ( rs->sr_ctrls != NULL ) {
-               for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
-                       ;
+       return c;
+}
 
-               controls = (LDAPControl **)slapi_ch_calloc( i + 1,
-                       sizeof(LDAPControl ));
+static int
+slapi_int_result(
+       Operation       *op, 
+       SlapReply       *rs )
+{
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       plugin_result_callback  prc = NULL;
+       void                    *callback_data = NULL;
+       LDAPControl             **ctrls = NULL;
 
-               for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
-                       controls[i] = slapi_dup_control( rs->sr_ctrls[i] );
+       assert( pb != NULL );   
 
-               controls[i] = NULL;
-       }
+       slapi_pblock_get( pb, SLAPI_X_INTOP_RESULT_CALLBACK, (void **)&prc );
+       slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,   &callback_data );
 
-       slapi_pblock_set( op->o_pb, SLAPI_RESCONTROLS, (void *)controls );
-       slapi_pblock_set( op->o_pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs->sr_err );
+       /* we need to duplicate controls because they might go out of scope */
+       ctrls = slapi_int_dup_controls( rs->sr_ctrls );
+       slapi_pblock_set( pb, SLAPI_RESCONTROLS, ctrls );
 
        if ( prc != NULL ) {
                (*prc)( rs->sr_err, callback_data );
        }
 
-       return;
+       return rs->sr_err;
 }
 
 static int
-slapi_int_send_search_entry_shim(
+slapi_int_search_entry(
        Operation       *op,
        SlapReply       *rs )
 {
+       Slapi_PBlock                    *pb = SLAPI_OPERATION_PBLOCK( op );
        plugin_search_entry_callback    psec = NULL;
        void                            *callback_data = NULL;
+       int                             rc = LDAP_SUCCESS;
 
-       assert( op->o_pb != NULL );
+       assert( pb != NULL );
 
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, &psec );
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
+       slapi_pblock_get( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, (void **)&psec );
+       slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,         &callback_data );
 
        if ( psec != NULL ) {
-               return (*psec)( rs->sr_entry, callback_data );
+               rc = (*psec)( rs->sr_entry, callback_data );
        }
 
-       return LDAP_SUCCESS;
-}
-
-static void
-slapi_int_send_ldap_extended_shim(
-       Operation       *op,    
-       SlapReply       *rs )
-{
-       assert( op->o_pb != NULL );
-
-       return;
+       return rc;
 }
 
 static int
-slapi_int_send_search_reference_shim(
+slapi_int_search_reference(
        Operation       *op,    
        SlapReply       *rs )
 {
        int                             i, rc = LDAP_SUCCESS;
        plugin_referral_entry_callback  prec = NULL;
        void                            *callback_data = NULL;
+       Slapi_PBlock                    *pb = SLAPI_OPERATION_PBLOCK( op );
 
-       assert( op->o_pb != NULL );
+       assert( pb != NULL );
 
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, &prec );
-       slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
+       slapi_pblock_get( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void **)&prec );
+       slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,           &callback_data );
 
        if ( prec != NULL ) {
                for ( i = 0; rs->sr_ref[i].bv_val != NULL; i++ ) {
@@ -138,198 +134,89 @@ slapi_int_send_search_reference_shim(
        return rc;
 }
 
-static int
-slapi_int_get_ctrls( Operation *op, SlapReply *rs, LDAPControl **controls )
+int
+slapi_int_response( Slapi_Operation *op, SlapReply *rs )
 {
-       LDAPControl **c;
-       int rc;
+       int                             rc;
 
-       op->o_ctrls = controls;
-       if ( op->o_ctrls == NULL ) {
-               return LDAP_SUCCESS;
+       switch ( rs->sr_type ) {
+       case REP_RESULT:
+               rc = slapi_int_result( op, rs );
+               break;
+       case REP_SEARCH:
+               rc = slapi_int_search_entry( op, rs );
+               break;
+       case REP_SEARCHREF:
+               rc = slapi_int_search_reference( op, rs );
+               break;
+       default:
+               rc = LDAP_OTHER;
+               break;
        }
 
-       for ( c = op->o_ctrls; *c != NULL; c++ ) {
-               rc = slap_parse_ctrl( op, rs, *c, &rs->sr_text );
-               if ( rc != LDAP_SUCCESS )
-                       break;
-       }
+       assert( rc != SLAP_CB_CONTINUE ); /* never try to send a wire response */
 
        return rc;
 }
 
-/*
- * To allow plugins to forward frontend requests to internal operations,
- * the internal operation and connection structures should import as
- * much state as practicable from the supplied parameter block.
- */
-
-/*
- * Select the backend to be used for an internal operation, either
- * from the operation target DN or from the parameter block.
- */
 static int
-slapi_int_pblock_get_backend( Slapi_PBlock *pb, Operation *op )
+slapi_int_get_ctrls( Slapi_PBlock *pb )
 {
-       int manageDsaIt = 0, isCritical;
-       LDAPControl **controls = NULL;
-       int rc;
+       LDAPControl             **c;
+       int                     rc = LDAP_SUCCESS;
 
-       slapi_pblock_get( pb, SLAPI_REQCONTROLS, (void **)&controls );
-
-       slapi_pblock_get( pb, SLAPI_MANAGEDSAIT, (void **)&manageDsaIt );
-       if ( manageDsaIt != 0 )
-               manageDsaIt = SLAP_CONTROL_CRITICAL;
-       else if ( slapi_control_present( controls, SLAPI_CONTROL_MANAGEDSAIT_OID,
-                   NULL, &isCritical ))
-               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
-
-       /* let caller force a specific backend */
-       slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&op->o_bd );
-       if ( op->o_bd == NULL ) {
-               op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
-               if ( op->o_bd == NULL ) {
-                       if ( op->o_tag == LDAP_REQ_SEARCH && manageDsaIt > SLAP_CONTROL_NONE )
-                               rc = LDAP_NO_SUCH_OBJECT;
-                       else
-                               rc = LDAP_PARTIAL_RESULTS;
+       if ( pb->pb_op->o_ctrls != NULL ) {
+               for ( c = pb->pb_op->o_ctrls; *c != NULL; c++ ) {
+                       rc = slap_parse_ctrl( pb->pb_op, pb->pb_rs, *c, &pb->pb_rs->sr_text );
+                       if ( rc != LDAP_SUCCESS )
+                               break;
                }
        }
 
-       if ( op->o_bd != NULL ) {
-               rc = LDAP_SUCCESS;
-       }
-
        return rc;
 }
 
-static int
-slapi_int_pblock_get_connection( Slapi_PBlock *pb, Connection *conn )
+void
+slapi_int_connection_init_pb( Slapi_PBlock *pb, ber_tag_t tag )
 {
-       char *connDn = NULL;
-       Operation *op;
-
-       slapi_pblock_get( pb, SLAPI_X_CONN_SSF, (void **)&conn->c_ssf );
-       slapi_pblock_get( pb, SLAPI_X_CONN_SASL_CONTEXT, (void **)&conn->c_sasl_authctx );
-
-       if ( slapi_pblock_get( pb, SLAPI_CONN_DN, (void **)&connDn ) != 0 ||
-            connDn == NULL ) {
-               /* default to operation DN */
-               conn->c_ndn = op->o_ndn;
-               conn->c_dn = op->o_ndn;
-       } else {
-               /* NB: conn DN must be normalized */
-               conn->c_ndn.bv_val = connDn;
-               conn->c_ndn.bv_len = strlen( connDn );
-
-               conn->c_dn = conn->c_ndn;
-       }
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_int_pblock_get_operation( Slapi_PBlock *pb, Operation *op, SlapReply *rs )
-{
-       int isRoot = 0;
-       int isUpdateDn = 0;
-       char *requestorDn = NULL;
-       struct berval targetDn = BER_BVNULL;
-       LDAPControl **controls;
-       int rc;
-
-       /* All internal operations must specify a target DN */
-       if ( slapi_pblock_get( pb, SLAPI_TARGET_DN, (void **)&targetDn.bv_val ) != 0 ||
-            targetDn.bv_val == NULL ) {
-               return LDAP_PARAM_ERROR; 
-       }
-
-       targetDn.bv_len = strlen( targetDn.bv_val );
-
-       rc = dnPrettyNormal( NULL, &targetDn, &op->o_req_dn, &op->o_req_ndn, NULL );
-       if ( rc != LDAP_SUCCESS ) {
-               return rc;
-       }
-
-       rc = slapi_int_pblock_get_connection( pb, op->o_conn );
-       if ( rc != LDAP_SUCCESS ) {
-               return rc;
-       }
-
-       rc = slapi_int_pblock_get_backend( pb, op );
-       if ( rc != LDAP_SUCCESS ) {
-               return rc;
-       }
-
-       slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, (void **)&isRoot );
-       slapi_pblock_get( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void **)&isUpdateDn );
-       /* NB: requestor DN must be normalized */
-       slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, (void **)&requestorDn );
-
-       /* Default authorization identity for internal operations is root DN */
-       if ( isRoot || requestorDn == NULL ) {
-               op->o_dn = op->o_bd->be_rootdn;
-               op->o_ndn = op->o_bd->be_rootndn;
-       } else {
-               op->o_ndn.bv_val = requestorDn;
-               op->o_ndn.bv_len = strlen( requestorDn );
-               op->o_dn = op->o_ndn;
-       }
-
-       slapi_pblock_get( pb, SLAPI_REQCONTROLS, (void **)&controls );
-       rc = slapi_int_get_ctrls( op, rs, controls );
-       if ( rc != LDAP_SUCCESS ) {
-               return rs->sr_err;
-       }
-
-       return LDAP_SUCCESS;
-}
-
-int
-slapi_int_init_connection( Slapi_PBlock *pb,
-       SlapReply *rs,
-       int OpType,
-       Connection **pConn )
-{
-       Connection *conn;
-       Operation *op;
-       ber_len_t max = sockbuf_max_incoming;
-       int rc;
+       Connection              *conn;
+       Operation               *op;
+       ber_len_t               max = sockbuf_max_incoming;
 
-       conn = (Connection *) slapi_ch_calloc(1, sizeof(Connection));
+       conn = (Connection *) slapi_ch_calloc( 1, sizeof(Connection) );
 
        LDAP_STAILQ_INIT( &conn->c_pending_ops );
 
-       op = (Operation *) slapi_ch_calloc( 1, OPERATION_BUFFER_SIZE );
-       op->o_hdr = (Opheader *)(op + 1);
-       op->o_controls = (void **)(op->o_hdr + 1);
-       op->o_pb = pb;
-       op->o_extensions = NULL;
+       op = (Operation *) slapi_ch_calloc( 1, sizeof(OperationBuffer) );
+       op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
+       op->o_controls = ((OperationBuffer *) op)->ob_controls;
+
+       op->o_callback = (slap_callback *) slapi_ch_calloc( 1, sizeof(slap_callback) );
+       op->o_callback->sc_response = slapi_int_response;
+       op->o_callback->sc_cleanup = NULL;
+       op->o_callback->sc_private = pb;
+       op->o_callback->sc_next = NULL;
 
        conn->c_pending_ops.stqh_first = op;
 
        /* connection object authorization information */
        conn->c_authtype = LDAP_AUTH_NONE;
-       conn->c_authmech.bv_val = NULL;
-       conn->c_authmech.bv_len = 0;
-       conn->c_dn.bv_val = NULL;
-       conn->c_dn.bv_len = 0;
-       conn->c_ndn.bv_val = NULL;
-       conn->c_ndn.bv_len = 0;
-
-       conn->c_listener = &slap_unknown_listener;
+       BER_BVZERO( &conn->c_authmech );
+       BER_BVZERO( &conn->c_dn );
+       BER_BVZERO( &conn->c_ndn );
+
+       conn->c_listener = &slapi_listener;
        ber_dupbv( &conn->c_peer_domain, (struct berval *)&slap_unknown_bv );
        ber_dupbv( &conn->c_peer_name, (struct berval *)&slap_unknown_bv );
 
        LDAP_STAILQ_INIT( &conn->c_ops );
 
-       conn->c_sasl_bind_mech.bv_val = NULL;
-       conn->c_sasl_bind_mech.bv_len = 0;
+       BER_BVZERO( &conn->c_sasl_bind_mech );
        conn->c_sasl_authctx = NULL;
        conn->c_sasl_sockctx = NULL;
        conn->c_sasl_extra = NULL;
 
-       conn->c_sb = ber_sockbuf_alloc( );
+       conn->c_sb = ber_sockbuf_alloc();
 
        ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
 
@@ -337,8 +224,10 @@ slapi_int_init_connection( Slapi_PBlock *pb,
 
        /* should check status of thread calls */
        ldap_pvt_thread_mutex_init( &conn->c_mutex );
-       ldap_pvt_thread_mutex_init( &conn->c_write_mutex );
-       ldap_pvt_thread_cond_init( &conn->c_write_cv );
+       ldap_pvt_thread_mutex_init( &conn->c_write1_mutex );
+       ldap_pvt_thread_mutex_init( &conn->c_write2_mutex );
+       ldap_pvt_thread_cond_init( &conn->c_write1_cv );
+       ldap_pvt_thread_cond_init( &conn->c_write2_cv );
 
        ldap_pvt_thread_mutex_lock( &conn->c_mutex );
 
@@ -366,21 +255,20 @@ slapi_int_init_connection( Slapi_PBlock *pb,
        conn->c_conn_state  = 0x01;     /* SLAP_C_ACTIVE */
        conn->c_struct_state = 0x02;    /* SLAP_C_USED */
 
-       conn->c_ssf = conn->c_transport_ssf = 0;
+       conn->c_ssf = conn->c_transport_ssf = local_ssf;
        conn->c_tls_ssf = 0;
 
        backend_connection_init( conn );
 
-       conn->c_send_ldap_result = slapi_int_send_ldap_result_shim;
-       conn->c_send_search_entry = slapi_int_send_search_entry_shim;
-       conn->c_send_ldap_extended = slapi_int_send_ldap_extended_shim;
-       conn->c_send_search_reference = slapi_int_send_search_reference_shim;
+       conn->c_send_ldap_result = slap_send_ldap_result;
+       conn->c_send_search_entry = slap_send_search_entry;
+       conn->c_send_ldap_extended = slap_send_ldap_extended;
+       conn->c_send_search_reference = slap_send_search_reference;
 
        /* operation object */
-       op->o_tag = OpType;
+       op->o_tag = tag;
        op->o_protocol = LDAP_VERSION3; 
-       op->o_authmech.bv_val = NULL; 
-       op->o_authmech.bv_len = 0; 
+       BER_BVZERO( &op->o_authmech );
        op->o_time = slap_get_time();
        op->o_do_not_cache = 1;
        op->o_threadctx = ldap_pvt_thread_pool_context();
@@ -388,749 +276,313 @@ slapi_int_init_connection( Slapi_PBlock *pb,
        op->o_tmpmfuncs = &ch_mfuncs;
        op->o_conn = conn;
        op->o_connid = conn->c_connid;
+       op->o_bd = frontendDB;
 
-       rc = slapi_int_pblock_get_operation( pb, op, rs );
+       /* extensions */
+       slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
+       slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
 
-       slapi_pblock_set( op->o_pb, SLAPI_OPERATION, op );
-       slapi_pblock_set( op->o_pb, SLAPI_CONNECTION, conn );
+       pb->pb_rs = (SlapReply *)slapi_ch_calloc( 1, sizeof(SlapReply) );
+       pb->pb_op = op;
+       pb->pb_conn = conn;
+       pb->pb_intop = 1;
 
        ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
-
-       if ( rc != LDAP_SUCCESS ) {
-               slapi_int_connection_destroy( &conn );
-               return rc;
-       }
-
-       *pConn = conn;
-
-       return LDAP_SUCCESS;
 }
 
-void slapi_int_connection_destroy( Connection **pConn )
+static void
+slapi_int_set_operation_dn( Slapi_PBlock *pb )
 {
-       Connection *conn = *pConn;
-       Operation *op;
+       Backend                 *be;
+       Operation               *op = pb->pb_op;
 
-       if ( conn == NULL ) {
-               return;
-       }
-
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-
-       slap_graduate_commit_csn( op );
-
-       if ( op->o_req_dn.bv_val != NULL ) {
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-       }
-       if ( op->o_req_ndn.bv_val != NULL ) {
-               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
-       }
-
-       if ( conn->c_sb != NULL ) {
-               ber_sockbuf_free( conn->c_sb );
-       }
-
-       slapi_pblock_set( op->o_pb, SLAPI_OPERATION, NULL );
-       slapi_pblock_set( op->o_pb, SLAPI_CONNECTION, NULL );
-
-       if ( op != NULL ) {
-               slapi_ch_free( (void **)&op );
+       if ( BER_BVISNULL( &op->o_ndn ) ) {
+               /* set to root DN */
+               be = select_backend( &op->o_req_ndn, 1 );
+               if ( be != NULL ) {
+                       ber_dupbv( &op->o_dn, &be->be_rootdn );
+                       ber_dupbv( &op->o_ndn, &be->be_rootndn );
+               }
        }
-
-       slapi_ch_free( (void **)pConn );
 }
 
-/*
- * Function : values2obj
- * Convert an array of strings into a BerVarray.
- * the strings.
- */
-static int
-values2obj_copy(
-       char **ppValue,
-       BerVarray *bvobj )
+void
+slapi_int_connection_done_pb( Slapi_PBlock *pb )
 {
-       int i;
-       BerVarray tmpberval;
+       Connection              *conn;
+       Operation               *op;
 
-       if ( ppValue == NULL ) {
-               *bvobj = NULL;
-               return LDAP_SUCCESS;
+       PBLOCK_ASSERT_INTOP( pb, 0 );
+
+       conn = pb->pb_conn;
+       op = pb->pb_op;
+
+       /* free allocated DNs */
+       if ( !BER_BVISNULL( &op->o_dn ) )
+               op->o_tmpfree( op->o_dn.bv_val, op->o_tmpmemctx );
+       if ( !BER_BVISNULL( &op->o_ndn ) )
+               op->o_tmpfree( op->o_ndn.bv_val, op->o_tmpmemctx );
+
+       if ( !BER_BVISNULL( &op->o_req_dn ) )
+               op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
+       if ( !BER_BVISNULL( &op->o_req_ndn ) )
+               op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
+
+       switch ( op->o_tag ) {
+       case LDAP_REQ_MODRDN:
+               if ( !BER_BVISNULL( &op->orr_newrdn ))
+                       op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
+               if ( !BER_BVISNULL( &op->orr_nnewrdn ))
+                       op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
+               if ( op->orr_newSup != NULL ) {
+                       assert( !BER_BVISNULL( op->orr_newSup ) );
+                       op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
+                       op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
+               }
+               if ( op->orr_nnewSup != NULL ) {
+                       assert( !BER_BVISNULL( op->orr_nnewSup ) );
+                       op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
+                       op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
+               }
+               slap_mods_free( op->orr_modlist, 1 );
+               break;
+       case LDAP_REQ_ADD:
+               slap_mods_free( op->ora_modlist, 0 );
+               break;
+       case LDAP_REQ_MODIFY:
+               slap_mods_free( op->orm_modlist, 1 );
+               break;
+       case LDAP_REQ_SEARCH:
+               if ( op->ors_attrs != NULL ) {
+                       op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
+                       op->ors_attrs = NULL;
+               }
+               break;
+       default:
+               break;
        }
 
-       for ( i = 0; ppValue[i] != NULL; i++ )
-               ; /* EMPTY */
-
-       tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
-       if ( tmpberval == NULL ) {
-               return LDAP_NO_MEMORY;
-       }
-       for ( i = 0; ppValue[i] != NULL; i++ ) {
-               size_t len = strlen( ppValue[i] );
+       slapi_ch_free_string( &conn->c_authmech.bv_val );
+       slapi_ch_free_string( &conn->c_dn.bv_val );
+       slapi_ch_free_string( &conn->c_ndn.bv_val );
+       slapi_ch_free_string( &conn->c_peer_domain.bv_val );
+       slapi_ch_free_string( &conn->c_peer_name.bv_val );
 
-               tmpberval[i].bv_val = slapi_ch_malloc( len + 1 );
-               AC_MEMCPY( tmpberval[i].bv_val, ppValue[i], len + 1 );
-               tmpberval[i].bv_len = len;
+       if ( conn->c_sb != NULL ) {
+               ber_sockbuf_free( conn->c_sb );
        }
-       tmpberval[i].bv_val = NULL;
-       tmpberval[i].bv_len = 0;
 
-       *bvobj = tmpberval;
+       slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
+       slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
 
-       return LDAP_SUCCESS;
+       slapi_ch_free( (void **)&pb->pb_op->o_callback );
+       slapi_ch_free( (void **)&pb->pb_op );
+       slapi_ch_free( (void **)&pb->pb_conn );
+       slapi_ch_free( (void **)&pb->pb_rs );
 }
 
 static int
-bvptr2obj_copy(
-       struct berval   **bvptr, 
-       BerVarray       *bvobj )
-{
-       int             i;
-       BerVarray       tmpberval;
-
-       if ( bvptr == NULL ) {
-               *bvobj = NULL;
-               return LDAP_SUCCESS;
-       }
-
-       for ( i = 0; bvptr[i] != NULL; i++ )
-               ; /* EMPTY */
-
-       tmpberval = (BerVarray)slapi_ch_malloc( (i + 1) * sizeof(struct berval));
-       if ( tmpberval == NULL ) {
-               return LDAP_NO_MEMORY;
-       } 
-
-       for ( i = 0; bvptr[i] != NULL; i++ ) {
-               tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
-               tmpberval[i].bv_len = bvptr[i]->bv_len;
-               AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
-       }
-
-       tmpberval[i].bv_val = NULL;
-       tmpberval[i].bv_len = 0;
-
-       *bvobj = tmpberval;
-
-       return LDAP_SUCCESS;
-}
-
-/*
- * Function : slapi_int_ldapmod_to_entry 
- * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
- * with a link list of the correspondent attributes.
- * Return value : LDAP_SUCCESS
- *                LDAP_NO_MEMORY
- *                LDAP_OTHER
-*/
-static Entry *
-slapi_int_ldapmod_to_entry(
-       Connection *pConn,
-       char *ldn, 
-       LDAPMod **mods )
+slapi_int_func_internal_pb( Slapi_PBlock *pb, slap_operation_t which )
 {
-       struct berval           dn = BER_BVNULL;
-       Entry                   *pEntry=NULL;
-       LDAPMod                 *pMod;
-       struct berval           *bv;
-       Operation               *op;
-
-       Modifications           *modlist = NULL;
-       Modifications           **modtail = &modlist;
-       Modifications           tmp;
-
-       int                     rc = LDAP_SUCCESS;
-       int                     i;
-
-       const char              *text = NULL;
+       BI_op_bind              **func;
+       SlapReply               *rs = pb->pb_rs;
+       int                     rc;
 
-       op = (Operation *)pConn->c_pending_ops.stqh_first;
+       PBLOCK_ASSERT_INTOP( pb, 0 );
 
-       pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
-       if ( pEntry == NULL) {
-               rc = LDAP_NO_MEMORY;
-               goto cleanup;
-       } 
-
-       dn.bv_val = ldn;
-       dn.bv_len = strlen( ldn );
-
-       rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
+       rc = slapi_int_get_ctrls( pb );
        if ( rc != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
-
-       if ( rc == LDAP_SUCCESS ) {
-               for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod = mods[++i]) {
-                       Modifications *mod;
-
-                       if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
-                               /*
-                                * Convert an array of pointers to bervals to
-                                * an array of bervals. Note that we need to copy the
-                                * values too, as the slap_mods_check() will free the
-                                * original values after prettying; the modifications
-                                * being passed in may not have been allocated on the
-                                * heap.
-                                */
-                               rc = bvptr2obj_copy( pMod->mod_bvalues, &bv );
-                               if ( rc != LDAP_SUCCESS ) goto cleanup;
-                               tmp.sml_type.bv_val = pMod->mod_type;
-                               tmp.sml_type.bv_len = strlen( pMod->mod_type );
-                               tmp.sml_values = bv;
-                               tmp.sml_nvalues = NULL;
-               
-                               mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
-
-                               mod->sml_op = LDAP_MOD_ADD;
-                               mod->sml_flags = 0;
-                               mod->sml_next = NULL;
-                               mod->sml_desc = NULL;
-                               mod->sml_type = tmp.sml_type;
-                               mod->sml_values = tmp.sml_values;
-                               mod->sml_nvalues = tmp.sml_nvalues;
-
-                               *modtail = mod;
-                               modtail = &mod->sml_next;
-
-                       } else {
-                               /* attr values are in string format, need to be converted */
-                               /* to an array of bervals */ 
-                               if ( pMod->mod_values == NULL ) {
-                                       rc = LDAP_OTHER;
-                               } else {
-                                       rc = values2obj_copy( pMod->mod_values, &bv );
-                                       if ( rc != LDAP_SUCCESS ) goto cleanup;
-                                       tmp.sml_type.bv_val = pMod->mod_type;
-                                       tmp.sml_type.bv_len = strlen( pMod->mod_type );
-                                       tmp.sml_values = bv;
-                                       tmp.sml_nvalues = NULL;
-               
-                                       mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
-
-                                       mod->sml_op = LDAP_MOD_ADD;
-                                       mod->sml_flags = 0;
-                                       mod->sml_next = NULL;
-                                       mod->sml_desc = NULL;
-                                       mod->sml_type = tmp.sml_type;
-                                       mod->sml_values = tmp.sml_values;
-                                       mod->sml_nvalues = tmp.sml_nvalues;
-
-                                       *modtail = mod;
-                                       modtail = &mod->sml_next;
-                               }
-                       }
-               } /* for each LDAPMod */
-       }
-
-       op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
-       if ( op->o_bd == NULL ) {
-               rc = LDAP_PARTIAL_RESULTS;
-       } else {
-               int repl_user = be_isupdate_dn( op->o_bd, &op->o_bd->be_rootdn );
-               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
-                       int     update = !BER_BVISNULL( &op->o_bd->be_update_ndn );
-                       char    textbuf[ SLAP_TEXT_BUFLEN ];
-                       size_t  textlen = sizeof( textbuf );
-
-                       rc = slap_mods_check( modlist, &text, 
-                               textbuf, textlen, NULL );
-
-                       if ( rc != LDAP_SUCCESS) {
-                               goto cleanup;
-                       }
-
-                       if ( !update ) {
-                               rc = slap_mods_no_user_mod_check( op, modlist,
-                                       &text, textbuf, textlen );
-                               if ( rc != LDAP_SUCCESS) {
-                                       goto cleanup;
-                               }
-                       }
-
-                       if ( !repl_user ) {
-                               rc = slap_mods_opattrs( op, modlist, modtail,
-                                       &text, textbuf, textlen, 1 );
-                               if ( rc != LDAP_SUCCESS) {
-                                       goto cleanup;
-                               }
-                       }
-
-                       rc = slap_mods2entry( modlist, &pEntry, repl_user,
-                                             0, &text, textbuf, textlen );
-                       if (rc != LDAP_SUCCESS) {
-                               goto cleanup;
-                       }
-
-               } else {
-                       rc = LDAP_REFERRAL;
-               }
+               rs->sr_err = rc;
+               return rc;
        }
 
-cleanup:;
-       if ( modlist != NULL )
-               slap_mods_free( modlist );
-       if ( rc != LDAP_SUCCESS ) {
-               if ( pEntry != NULL ) {
-                       slapi_entry_free( pEntry );
-               }
-               pEntry = NULL;
-       }
+       pb->pb_op->o_bd = frontendDB;
+       func = &frontendDB->be_bind;
 
-       return( pEntry );
+       return func[which]( pb->pb_op, pb->pb_rs );
 }
 
 int
 slapi_delete_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-       int                     operation_flags = 0;
-
-       SlapReply               rs = { REP_RESULT };
-
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
-
-       rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_DELETE, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
-
-       op = conn->c_pending_ops.stqh_first;
-
-       if ( op->o_bd->be_delete != NULL ) {
-               int repl_user = be_isupdate( op );
-               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
-                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
-                       if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
-                               op->o_callback = &cb;
-
-                       op->o_bd->be_delete( op, &rs );
-               } else {
-                       rs.sr_err = LDAP_REFERRAL;
-               }
-       } else {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
-       }
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_DELETE );
 
-cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-       slapi_int_connection_destroy( &conn );
+       slapi_int_func_internal_pb( pb, op_delete );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_add_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Slapi_Entry             *entry = NULL;
-       Slapi_Entry             *argEntry = NULL;
-       char                    *dn = NULL;
-       LDAPMod                 **mods = NULL;
-       int                     operation_flags = 0;
-       Operation               *op = NULL;
-       int                     i;
-
-       SlapReply               rs = { REP_RESULT };
+       SlapReply               *rs;
+       Slapi_Entry             *entry_orig = NULL;
+       OpExtraDB oex;
+       int rc;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &argEntry );
-       slapi_pblock_get( pb, SLAPI_ADD_TARGET, &dn );
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
-       slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
-
-       if ( argEntry != NULL ) {
-               /*
-                * The caller can specify a new entry, or a target DN and set
-                * of modifications, but not both.
-                */
-               if ( dn != NULL ) {
-                       rs.sr_err = LDAP_PARAM_ERROR;
-                       goto cleanup;
-               }
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_ADD );
 
-               /*
-                * Set the DN here so that slapi_int_init_backend() will select
-                * correct backend.
-                */
-               dn = slapi_entry_get_dn( argEntry );
-               slapi_pblock_set( pb, SLAPI_ADD_TARGET, &entry->e_nname.bv_val );
-       }
+       rs = pb->pb_rs;
 
-       rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_ADD, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS )
-               goto cleanup;
+       entry_orig = pb->pb_op->ora_e;
+       pb->pb_op->ora_e = NULL;
 
-       if ( argEntry != NULL ) {
-               entry = slapi_entry_dup( argEntry );
-       } else {
-               if ( mods == NULL || dn == NULL ) {
-                       rs.sr_err = LDAP_PARAM_ERROR;
+       /*
+        * The caller can specify a new entry, or a target DN and set
+        * of modifications, but not both.
+        */
+       if ( entry_orig != NULL ) {
+               if ( pb->pb_op->ora_modlist != NULL || !BER_BVISNULL( &pb->pb_op->o_req_ndn )) {
+                       rs->sr_err = LDAP_PARAM_ERROR;
                        goto cleanup;
                }
 
-               for ( i = 0; mods[i] != NULL; i++ ) {
-                       if ( (mods[i]->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
-                               rs.sr_err = LDAP_PARAM_ERROR;
-                               goto cleanup;
-                       }
-               }
+               assert( BER_BVISNULL( &pb->pb_op->o_req_dn ) ); /* shouldn't get set */
+               ber_dupbv( &pb->pb_op->o_req_dn, &entry_orig->e_name );
+               ber_dupbv( &pb->pb_op->o_req_ndn, &entry_orig->e_nname );
+       } else if ( pb->pb_op->ora_modlist == NULL || BER_BVISNULL( &pb->pb_op->o_req_ndn )) {
+               rs->sr_err = LDAP_PARAM_ERROR;
+               goto cleanup;
+       }
 
-               entry = slapi_int_ldapmod_to_entry( conn, dn, mods );
-               if ( entry == NULL ) {
-                       rs.sr_err = LDAP_OTHER;
+       pb->pb_op->ora_e = (Entry *)slapi_ch_calloc( 1, sizeof(Entry) );
+       ber_dupbv( &pb->pb_op->ora_e->e_name,  &pb->pb_op->o_req_dn );
+       ber_dupbv( &pb->pb_op->ora_e->e_nname, &pb->pb_op->o_req_ndn );
+
+       if ( entry_orig != NULL ) {
+               assert( pb->pb_op->ora_modlist == NULL );
+
+               rs->sr_err = slap_entry2mods( entry_orig, &pb->pb_op->ora_modlist,
+                       &rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ) );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
                        goto cleanup;
                }
+       } else {
+               assert( pb->pb_op->ora_modlist != NULL );
        }
 
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-       op->oq_add.rs_e = entry;
+       rs->sr_err = slap_mods_check( pb->pb_op, pb->pb_op->ora_modlist, &rs->sr_text,
+               pb->pb_textbuf, sizeof( pb->pb_textbuf ), NULL );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+                goto cleanup;
+        }
 
-       if ( op->o_bd->be_add != NULL ) {
-               int repl_user = be_isupdate( op );
-               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
-                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
+       /* Duplicate the values, because we may call slapi_entry_free() */
+       rs->sr_err = slap_mods2entry( pb->pb_op->ora_modlist, &pb->pb_op->ora_e,
+               1, 0, &rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ) );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               goto cleanup;
+       }
 
-                       if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
-                               op->o_callback = &cb;
+       oex.oe.oe_key = (void *)do_add;
+       oex.oe_db = NULL;
+       LDAP_SLIST_INSERT_HEAD(&pb->pb_op->o_extra, &oex.oe, oe_next);
+       rc = slapi_int_func_internal_pb( pb, op_add );
+       LDAP_SLIST_REMOVE(&pb->pb_op->o_extra, &oex.oe, OpExtra, oe_next);
 
-                       if ( op->o_bd->be_add( op, &rs ) == LDAP_SUCCESS ) {
-                               be_entry_release_w( op, entry );
-                               entry = NULL;
-                       }
-               } else {
-                       rs.sr_err = LDAP_REFERRAL;
+       if ( !rc ) {
+               if ( pb->pb_op->ora_e != NULL && oex.oe_db != NULL ) {
+                       BackendDB       *bd = pb->pb_op->o_bd;
+
+                       pb->pb_op->o_bd = oex.oe_db;
+                       be_entry_release_w( pb->pb_op, pb->pb_op->ora_e );
+                       pb->pb_op->ora_e = NULL;
+                       pb->pb_op->o_bd = bd;
                }
-       } else {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
 
-       if ( entry != NULL ) {
-               slapi_entry_free( entry );
+       if ( pb->pb_op->ora_e != NULL ) {
+               slapi_entry_free( pb->pb_op->ora_e );
+               pb->pb_op->ora_e = NULL;
        }
-       if ( argEntry != NULL ) {
-               slapi_pblock_set( pb, SLAPI_ADD_TARGET, NULL );
+       if ( entry_orig != NULL ) {
+               pb->pb_op->ora_e = entry_orig;
+               slap_mods_free( pb->pb_op->ora_modlist, 1 );
+               pb->pb_op->ora_modlist = NULL;
        }
 
-       slapi_int_connection_destroy( &conn );
-
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_modrdn_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       struct berval           dn = BER_BVNULL;
-       struct berval           newrdn = BER_BVNULL;
-       struct berval           newsupdn = BER_BVNULL;
-       struct berval           newSuperiorPretty = BER_BVNULL;
-       struct berval           newSuperiorNormalized = BER_BVNULL;
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-       int                     manageDsaIt = SLAP_CONTROL_NONE;
-       int                     isCritical;
-
-       char                    *lnewrdn;
-       char                    *newsuperior;
-       int                     deloldrdn;
-       int                     operation_flags;
-
-       SlapReply               rs = { REP_RESULT };
-
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &lnewrdn );
-       slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperior );
-       slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn );
-       slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODRDN );
 
-       rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODRDN, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       if ( BER_BVISEMPTY( &pb->pb_op->o_req_ndn ) ) {
+               pb->pb_rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-
-       if ( op->o_req_dn.bv_len == 0 ) {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
-               goto cleanup;
-       }
-
-       newrdn.bv_val = lnewrdn;
-       newrdn.bv_len = strlen( lnewrdn );
-
-       rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
-
-       if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
-
-       if ( newsuperior != NULL ) {
-               newsupdn.bv_val = (char *)newsuperior;
-               newsupdn.bv_len = strlen( newsuperior );
-
-               rs.sr_err = dnPrettyNormal( NULL, &newsupdn, &newSuperiorPretty, &newSuperiorNormalized, NULL );
-               if ( rs.sr_err != LDAP_SUCCESS )
-                       goto cleanup;
-
-               op->oq_modrdn.rs_newSup = &newSuperiorPretty;
-               op->oq_modrdn.rs_nnewSup = &newSuperiorNormalized;
-       } else {
-               op->oq_modrdn.rs_newSup = NULL;
-               op->oq_modrdn.rs_nnewSup = NULL;
-       }
-
-       op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
-
-       if ( op->o_bd->be_modrdn != NULL ) {
-               int repl_user = be_isupdate( op );
-               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
-                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
-
-                       if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
-                               op->o_callback = &cb;
-
-                       op->o_bd->be_modrdn( op, &rs );
-               } else {
-                       rs.sr_err = LDAP_REFERRAL;
-               }
-       } else {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
-       }
+       slapi_int_func_internal_pb( pb, op_modrdn );
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-
-       if ( op->oq_modrdn.rs_newrdn.bv_val != NULL )
-               slapi_ch_free_string( &op->oq_modrdn.rs_newrdn.bv_val );
-       if ( op->oq_modrdn.rs_nnewrdn.bv_val != NULL )
-               slapi_ch_free_string( &op->oq_modrdn.rs_nnewrdn.bv_val );
-       if ( newSuperiorPretty.bv_val != NULL )
-               slapi_ch_free_string( &newSuperiorPretty.bv_val );
-       if ( newSuperiorNormalized.bv_val != NULL )
-               slapi_ch_free_string( &newSuperiorNormalized.bv_val );
-
-       slapi_int_connection_destroy( &conn );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
-int slapi_modify_internal_pb( Slapi_PBlock *pb )
+int
+slapi_modify_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       int                     i;
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-
-       struct berval dn = BER_BVNULL;
-
-       int                     manageDsaIt = SLAP_CONTROL_NONE;
-       int                     isCritical;
-       struct berval           *bv;
-       LDAPMod                 *pMod;
-
-       Modifications           *modlist = NULL;
-       Modifications           **modtail = &modlist;
-       Modifications           tmp;
-
-       LDAPMod                 **mods = NULL;
-       int                     operation_flags = 0;
-
-       SlapReply               rs = { REP_RESULT };
+       SlapReply               *rs;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
-       slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODIFY );
 
-       if ( mods == NULL || mods[0] == NULL ) {
-               rs.sr_err = LDAP_PARAM_ERROR ;
-               goto cleanup;
-       }
+       rs = pb->pb_rs;
 
-       rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODIFY, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       if ( pb->pb_op->orm_modlist == NULL ) {
+               rs->sr_err = LDAP_PARAM_ERROR;
                goto cleanup;
        }
 
-       for ( i = 0, pMod = mods[0];
-               rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
-               pMod = mods[++i] )
-       {
-               Modifications *mod;
-
-               if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
-                       /*
-                        * attr values are in berval format
-                        * convert an array of pointers to bervals
-                        * to an array of bervals
-                        */
-                       rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
-                       if ( rs.sr_err != LDAP_SUCCESS )
-                               goto cleanup;
-                       tmp.sml_type.bv_val = pMod->mod_type;
-                       tmp.sml_type.bv_len = strlen( pMod->mod_type );
-                       tmp.sml_values = bv;
-                       tmp.sml_nvalues = NULL;
-
-                       mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
-
-                       mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
-                       mod->sml_flags = 0;
-                       mod->sml_next = NULL;
-                       mod->sml_desc = NULL;
-                       mod->sml_type = tmp.sml_type;
-                       mod->sml_values = tmp.sml_values;
-                       mod->sml_nvalues = tmp.sml_nvalues;
-               } else { 
-                       rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
-                       if ( rs.sr_err != LDAP_SUCCESS )
-                               goto cleanup;
-                       tmp.sml_type.bv_val = pMod->mod_type;
-                       tmp.sml_type.bv_len = strlen( pMod->mod_type );
-                       tmp.sml_values = bv;
-                       tmp.sml_nvalues = NULL;
-
-                       mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
-
-                       mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
-                       mod->sml_flags = 0;
-                       mod->sml_next = NULL;
-                       mod->sml_desc = NULL;
-                       mod->sml_type = tmp.sml_type;
-                       mod->sml_values = tmp.sml_values;
-                       mod->sml_nvalues = tmp.sml_nvalues;
-               }
-               *modtail = mod;
-               modtail = &mod->sml_next;
-
-               switch( pMod->mod_op & LDAP_MOD_OP ) {
-               case LDAP_MOD_ADD:
-               if ( mod->sml_values == NULL ) {
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
-                       goto cleanup;
-               }
-
-               /* fall through */
-               case LDAP_MOD_DELETE:
-               case LDAP_MOD_REPLACE:
-               case LDAP_MOD_INCREMENT:
-               break;
-
-               default:
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
-                       goto cleanup;
-               }
-       } 
-       *modtail = NULL;
-
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-
-       if ( op->o_req_ndn.bv_len == 0 ) {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
+       if ( BER_BVISEMPTY( &pb->pb_op->o_req_ndn ) ) {
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
-       op->oq_modify.rs_modlist = modlist;
-
-       if ( op->o_bd->be_modify != NULL ) {
-               int repl_user = be_isupdate( op );
-               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
-                       int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
-                       const char      *text = NULL;
-                       char            textbuf[ SLAP_TEXT_BUFLEN ];
-                       size_t          textlen = sizeof( textbuf );
-                       slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
-
-                       rs.sr_err = slap_mods_check( modlist,
-                               &text, textbuf, textlen, NULL );
-                       if ( rs.sr_err != LDAP_SUCCESS ) {
-                               goto cleanup;
-                       }
-
-                       if ( !update ) {
-                               rs.sr_err = slap_mods_no_user_mod_check( op, modlist,
-                                       &text, textbuf, textlen );
-                               if ( rs.sr_err != LDAP_SUCCESS ) {
-                                       goto cleanup;
-                               }
-                       }
+       rs->sr_err = slap_mods_check( pb->pb_op, pb->pb_op->orm_modlist,
+               &rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ), NULL );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+                goto cleanup;
+        }
 
-                       if ( !repl_user ) {
-                               rs.sr_err = slap_mods_opattrs( op, modlist,
-                                               modtail, &text, textbuf, 
-                                               textlen, 1 );
-                               if ( rs.sr_err != LDAP_SUCCESS ) {
-                                       goto cleanup;
-                               }
-                       }
-
-                       if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
-                               op->o_callback = &cb;
-
-                       op->o_bd->be_modify( op, &rs );
-               } else {
-                       rs.sr_err = LDAP_REFERRAL;
-               }
-       } else {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
-       }
+       slapi_int_func_internal_pb( pb, op_modify );
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-
-       if ( modlist != NULL )
-               slap_mods_free( modlist );
-
-       slapi_int_connection_destroy( &conn );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
-#ifdef LDAP_SLAPI
 static int
 slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
 {
-       int nentries = 0, i = 0;
-       Slapi_Entry **head = NULL, **tp;
-       Slapi_PBlock *pb = (Slapi_PBlock *)callback_data;
+       int             nentries = 0, i = 0;
+       Slapi_Entry     **head = NULL, **tp;
+       Slapi_PBlock    *pb = (Slapi_PBlock *)callback_data;
+
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_SEARCH );
 
        entry = slapi_entry_dup( entry );
        if ( entry == NULL ) {
-               return 1;
+               return LDAP_NO_MEMORY;
        }
 
        slapi_pblock_get( pb, SLAPI_NENTRIES, &nentries );
@@ -1141,234 +593,96 @@ slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
                tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
                if ( tp == NULL ) {
                        slapi_entry_free( entry );
-                       return 1;
+                       return LDAP_NO_MEMORY;
                }
 
-               tp[ 0 ] = entry;
+               tp[0] = entry;
        } else {
                tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
                                sizeof(Slapi_Entry *) * ( i + 1 ) );
                if ( tp == NULL ) {
                        slapi_entry_free( entry );
-                       return 1;
+                       return LDAP_NO_MEMORY;
                }
-               tp[ i - 1 ] = entry;
+               tp[i - 1] = entry;
        }
-       tp[ i ] = NULL;
+       tp[i] = NULL;
                  
        slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
-       slapi_pblock_set( pb, SLAPI_NENTRIES, (void *)i );
+       slapi_pblock_set( pb, SLAPI_NENTRIES, (void *)&i );
 
        return LDAP_SUCCESS;
 }
-#endif /* LDAP_SLAPI */
 
-int slapi_search_internal_pb( Slapi_PBlock *pb )
+int
+slapi_search_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
        return slapi_search_internal_callback_pb( pb,
                (void *)pb,
                NULL,
                slapi_int_search_entry_callback,
                NULL );
-#else
-       return -1;
-#endif
 }
 
-int slapi_search_internal_callback_pb( Slapi_PBlock *pb,
+int
+slapi_search_internal_callback_pb( Slapi_PBlock *pb,
        void *callback_data,
        plugin_result_callback prc,
        plugin_search_entry_callback psec,
        plugin_referral_entry_callback prec )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-       struct berval           dn = BER_BVNULL;
-       Filter                  *filter = NULL;
-       struct berval           fstr = BER_BVNULL;
-       AttributeName           *an = NULL;
-       const char              *text = NULL;
-
-       int                     manageDsaIt = SLAP_CONTROL_NONE;
-       int                     isCritical;
-       int                     i;
-
-       int                     scope = LDAP_SCOPE_BASE;
-       char                    *filStr = NULL;
-       LDAPControl             **controls = NULL;
-       char                    **attrs = NULL;
-       char                    *uniqueid = NULL;
-       int                     attrsonly = 0;
-       int                     operation_flags = 0;
-       int                     freeFilter = 0;
-
-       SlapReply               rs = { REP_RESULT };
+       int                     free_filter = 0;
+       SlapReply               *rs;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, &scope );
-       slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &filter );
-       slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, &filStr );
-       slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS, &attrs );
-       slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
-       slapi_pblock_get( pb, SLAPI_REQCONTROLS, &controls );
-       slapi_pblock_get( pb, SLAPI_TARGET_UNIQUEID, &uniqueid );
-       slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
-
-       rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_SEARCH, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS )
-               goto cleanup;
-
-       /* search callback and arguments */
-       slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK, prc );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, psec );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, prec );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA, callback_data );
-
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-
-       switch ( scope ) {
-               case LDAP_SCOPE_BASE:
-               case LDAP_SCOPE_ONELEVEL:
-               case LDAP_SCOPE_SUBTREE:
-#ifdef LDAP_SCOPE_SUBORDINATE
-               case LDAP_SCOPE_SUBORDINATE:
-#endif
-                       break;
-               default:
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
-                       goto cleanup;
-       }
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_SEARCH );
 
-       if ( filter == NULL ) {
-               if ( filStr == NULL ) {
-                       rs.sr_err = LDAP_PARAM_ERROR;
-                       goto cleanup;
-               }
+       rs = pb->pb_rs;
 
-               filter = slapi_str2filter( filStr );
-               if ( filter == NULL ) {
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
-                       goto cleanup;
-               }
-
-               freeFilter = 1;
-       }
-
-       filter2bv( filter, &fstr );
-
-       for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
-               ; /* count the number of attributes */
-       }
+       /* search callback and arguments */
+       slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK,         (void *)prc );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK,   (void *)psec );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void *)prec );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA,           (void *)callback_data );
 
-       if ( i > 0 ) {
-               an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
-               for (i = 0; attrs[i] != 0; i++) {
-                       an[i].an_desc = NULL;
-                       an[i].an_oc = NULL;
-                       an[i].an_oc_exclude = 0;
-                       an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
-                       an[i].an_name.bv_len = strlen(attrs[i]);
-                       slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
-               }
-               an[i].an_name.bv_val = NULL;
+       if ( BER_BVISEMPTY( &pb->pb_op->ors_filterstr )) {
+               rs->sr_err = LDAP_PARAM_ERROR;
+               goto cleanup;
        }
 
-       rs.sr_type = REP_RESULT;
-       rs.sr_err = LDAP_SUCCESS;
-       rs.sr_entry = NULL; /* paranoia */
-
-       if ( scope == LDAP_SCOPE_BASE ) {
-               rs.sr_entry = NULL;
-
-               if ( op->o_req_ndn.bv_len == 0 ) {
-                       rs.sr_err = root_dse_info( conn, &rs.sr_entry, &rs.sr_text );
-               }
-
-               if( rs.sr_err != LDAP_SUCCESS ) {
-                       send_ldap_result( op, &rs );
-                       goto cleanup;
-               } else if ( rs.sr_entry != NULL ) {
-                       rs.sr_err = test_filter( op, rs.sr_entry, filter );
-
-                       if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
-                               rs.sr_type = REP_SEARCH;
-                               rs.sr_err = LDAP_SUCCESS;
-                               rs.sr_attrs = an;
-                               rs.sr_operational_attrs = NULL;
-                               rs.sr_flags = REP_ENTRY_MODIFIABLE;
-
-                               send_search_entry( op, &rs );
-                       }
-
-                       entry_free( rs.sr_entry );
-
-                       rs.sr_type = REP_RESULT;
-                       rs.sr_err = LDAP_SUCCESS;
-
-                       send_ldap_result( op, &rs );
-
+       if ( pb->pb_op->ors_filter == NULL ) {
+               pb->pb_op->ors_filter = slapi_str2filter( pb->pb_op->ors_filterstr.bv_val );
+               if ( pb->pb_op->ors_filter == NULL ) {
+                       rs->sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
-       }
 
-       op->oq_search.rs_scope = scope;
-       op->oq_search.rs_deref = 0;
-       op->oq_search.rs_slimit = SLAP_NO_LIMIT;
-       op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
-       op->oq_search.rs_attrsonly = attrsonly;
-       op->oq_search.rs_attrs = an;
-       op->oq_search.rs_filter = filter;
-       op->oq_search.rs_filterstr = fstr;
-
-       if ( BER_BVISEMPTY( &op->o_req_ndn ) &&
-            !BER_BVISEMPTY( &default_search_nbase ) ) {
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
-
-               ber_dupbv( &op->o_req_dn, &default_search_base );
-               ber_dupbv( &op->o_req_ndn, &default_search_nbase );
-
-               rs.sr_err = slapi_int_pblock_get_backend( pb, op );
-               if ( rs.sr_err != LDAP_SUCCESS )
-                       goto cleanup;
+               free_filter = 1;
        }
 
-       if ( op->o_bd->be_search != NULL ) {
-               (*op->o_bd->be_search)( op, &rs );
-       } else {
-               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
-       }
+       slapi_int_func_internal_pb( pb, op_search );
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK, NULL );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, NULL );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, NULL );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA, NULL );
-
-       if ( freeFilter && filter != NULL )
-               slapi_filter_free( filter, 1 );
-       if ( fstr.bv_val )
-               slapi_ch_free( (void **)&fstr.bv_val );
-       if ( an != NULL )
-               slapi_ch_free( (void **)&an );
+       if ( free_filter ) {
+               slapi_filter_free( pb->pb_op->ors_filter, 1 );
+               pb->pb_op->ors_filter = NULL;
+       }
 
-       slapi_int_connection_destroy( &conn );
+       slapi_pblock_delete_param( pb, SLAPI_X_INTOP_RESULT_CALLBACK );
+       slapi_pblock_delete_param( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK );
+       slapi_pblock_delete_param( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK );
+       slapi_pblock_delete_param( pb, SLAPI_X_INTOP_CALLBACK_DATA );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /* Wrappers for old API */
 
-void slapi_search_internal_set_pb( Slapi_PBlock *pb,
+void
+slapi_search_internal_set_pb( Slapi_PBlock *pb,
        const char *base,
        int scope,
        const char *filter,
@@ -1379,18 +693,25 @@ void slapi_search_internal_set_pb( Slapi_PBlock *pb,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_SEARCH_TARGET, (void *)base );
-       slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE, (void *)scope );
-       slapi_pblock_set( pb, SLAPI_SEARCH_FILTER, NULL );
+       int no_limit = SLAP_NO_LIMIT;
+       int deref = LDAP_DEREF_NEVER;
+
+       slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
+       slapi_pblock_set( pb, SLAPI_SEARCH_TARGET,    (void *)base );
+       slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE,     (void *)&scope );
+       slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,    (void *)0 );
        slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)filter );
-       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
-       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)attrsonly );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
-       slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
-       slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
-#endif /* LDAP_SLAPI */
+       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS,     (void *)attrs );
+       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)&attrsonly );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,      (void *)controls );
+       slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,  (void *)uniqueid );
+       slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,  (void *)plugin_identity );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,    (void *)&operation_flags );
+       slapi_pblock_set( pb, SLAPI_SEARCH_DEREF,     (void *)&deref );
+       slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)&no_limit );
+       slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)&no_limit );
+
+       slapi_int_set_operation_dn( pb );
 }
 
 Slapi_PBlock *
@@ -1402,26 +723,21 @@ slapi_search_internal(
        char **attrs, 
        int attrsonly ) 
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
        pb = slapi_pblock_new();
-       if ( pb == NULL ) {
-               return NULL;
-       }
 
-       slapi_search_internal_set_pb( pb, ldn, scope, filStr, attrs, attrsonly,
+       slapi_search_internal_set_pb( pb, ldn, scope, filStr,
+               attrs, attrsonly,
                controls, NULL, NULL, 0 );
 
        slapi_search_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
-void slapi_modify_internal_set_pb( Slapi_PBlock *pb,
+void
+slapi_modify_internal_set_pb( Slapi_PBlock *pb,
        const char *dn,
        LDAPMod **mods,
        LDAPControl **controls,
@@ -1429,14 +745,14 @@ void slapi_modify_internal_set_pb( Slapi_PBlock *pb,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn );
-       slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)mods );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
+       slapi_int_connection_init_pb( pb, LDAP_REQ_MODIFY );
+       slapi_pblock_set( pb, SLAPI_MODIFY_TARGET,   (void *)dn );
+       slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)mods );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
        slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
        slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
-#endif /* LDAP_SLAPI */
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
+       slapi_int_set_operation_dn( pb );
 }
 
 /* Function : slapi_modify_internal
@@ -1456,108 +772,88 @@ slapi_modify_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
        pb = slapi_pblock_new();
-       if ( pb == NULL ) {
-               return NULL;
-       }
-
-       slapi_modify_internal_set_pb( pb, ldn, mods, controls, NULL, NULL,
-               log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
 
+       slapi_modify_internal_set_pb( pb, ldn, mods, controls, NULL, NULL, 0 );
+       slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
        slapi_modify_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
-int slapi_add_internal_set_pb( Slapi_PBlock *pb,
+int
+slapi_add_internal_set_pb( Slapi_PBlock *pb,
        const char *dn,
        LDAPMod **attrs,
        LDAPControl **controls,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn );
-       slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)attrs );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
+       slapi_int_connection_init_pb( pb, LDAP_REQ_ADD );
+       slapi_pblock_set( pb, SLAPI_ADD_TARGET,      (void *)dn );
+       slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)attrs );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
        slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
+       slapi_int_set_operation_dn( pb );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
-Slapi_PBlock *slapi_add_internal(
+Slapi_PBlock *
+slapi_add_internal(
        char * dn,
        LDAPMod **attrs,
        LDAPControl **controls,
-       int log_changes )
+       int log_change )
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
        pb = slapi_pblock_new();
-       if ( pb == NULL )
-               return NULL;
 
-       slapi_add_internal_set_pb( pb, dn, attrs, controls, NULL,
-               log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
-       
+       slapi_add_internal_set_pb( pb, dn, attrs, controls, NULL, 0);
+       slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
        slapi_add_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
-void slapi_add_entry_internal_set_pb( Slapi_PBlock *pb,
+void
+slapi_add_entry_internal_set_pb( Slapi_PBlock *pb,
        Slapi_Entry *e,
        LDAPControl **controls,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
+       slapi_int_connection_init_pb( pb, LDAP_REQ_ADD );
+       slapi_pblock_set( pb, SLAPI_ADD_ENTRY,       (void *)e );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
        slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
-#endif /* LDAP_SLAPI */
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
+       slapi_int_set_operation_dn( pb );
 }
 
 Slapi_PBlock * 
 slapi_add_entry_internal(
        Slapi_Entry *e, 
        LDAPControl **controls, 
-       int log_changes )
+       int log_change )
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
        pb = slapi_pblock_new();
-       if ( pb == NULL )
-               return NULL;
 
-       slapi_add_entry_internal_set_pb( pb, e, controls, NULL,
-               log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
-       
+       slapi_add_entry_internal_set_pb( pb, e, controls, NULL, 0 );
+       slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
        slapi_add_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
-void slapi_rename_internal_set_pb( Slapi_PBlock *pb,
+void
+slapi_rename_internal_set_pb( Slapi_PBlock *pb,
        const char *olddn,
        const char *newrdn,
        const char *newsuperior,
@@ -1567,16 +863,17 @@ void slapi_rename_internal_set_pb( Slapi_PBlock *pb,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)olddn );
-       slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn );
+       slapi_int_connection_init_pb( pb, LDAP_REQ_MODRDN );
+       slapi_pblock_set( pb, SLAPI_MODRDN_TARGET,      (void *)olddn );
+       slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN,      (void *)newrdn );
        slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)newsuperior );
-       slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
-       slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
-       slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
-#endif /* LDAP_SLAPI */
+       slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN,   (void *)&deloldrdn );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,        (void *)controls );
+       slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,    (void *)uniqueid );
+       slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,    (void *)plugin_identity );
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,      (void *)&operation_flags );
+       slap_modrdn2mods( pb->pb_op, pb->pb_rs );
+       slapi_int_set_operation_dn( pb );
 }
 
 /* Function : slapi_modrdn_internal
@@ -1599,40 +896,33 @@ slapi_modrdn_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
-       pb = slapi_pblock_new();
-       if ( pb == NULL ) {
-               return NULL;
-       }
+       pb = slapi_pblock_new ();
 
        slapi_rename_internal_set_pb( pb, olddn, lnewrdn, NULL,
-               deloldrdn, controls, NULL, NULL,
-               log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
-
+               deloldrdn, controls, NULL, NULL, 0 );
+       slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
        slapi_modrdn_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
-void slapi_delete_internal_set_pb( Slapi_PBlock *pb,
+void
+slapi_delete_internal_set_pb( Slapi_PBlock *pb,
        const char *dn,
        LDAPControl **controls,
        const char *uniqueid,
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
-       slapi_pblock_set( pb, SLAPI_TARGET_DN, (void *)dn );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
+       slapi_int_connection_init_pb( pb, LDAP_REQ_DELETE );
+       slapi_pblock_set( pb, SLAPI_TARGET_DN,       (void *)dn );
+       slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
        slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
        slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
-       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
-#endif /* LDAP_SLAPI */
+       slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
+       slapi_int_set_operation_dn( pb );
 }
 
 /* Function : slapi_delete_internal
@@ -1651,21 +941,16 @@ slapi_delete_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pb;
 
        pb = slapi_pblock_new();
-       if ( pb == NULL )
-               return NULL;
-
-       slapi_delete_internal_set_pb( pb, ldn, controls, NULL, NULL,
-               log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
 
+       slapi_delete_internal_set_pb( pb, ldn, controls, NULL, NULL, 0 );
+       slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
        slapi_delete_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
+#endif /* LDAP_SLAPI */
+