LDAP_BEGIN_DECL
 
 /* slapi_utils.c */
-LDAP_SLAPI_F (int) slapi_int_pblock_set_operation LDAP_P(( Slapi_PBlock *pb, Operation *op ));
-
-LDAP_SLAPI_F (LDAPMod **) slapi_int_modifications2ldapmods LDAP_P((Modifications **));
-LDAP_SLAPI_F (Modifications *) slapi_int_ldapmods2modifications LDAP_P((LDAPMod **));
-LDAP_SLAPI_F (void) slapi_int_free_ldapmods LDAP_P((LDAPMod **));
+LDAP_SLAPI_F (LDAPMod **) slapi_int_modifications2ldapmods LDAP_P(( Modifications **, void *ctx ));
+LDAP_SLAPI_F (Modifications *) slapi_int_ldapmods2modifications LDAP_P(( LDAPMod **, void *ctx ));
+LDAP_SLAPI_F (void) slapi_int_free_ldapmods LDAP_P(( LDAPMod ** ));
+LDAP_SLAPI_F (int) slapi_int_count_controls LDAP_P(( LDAPControl **ctrls ));
 
 LDAP_SLAPI_F (int) slapi_int_access_allowed LDAP_P((Operation *op,
        Entry *entry,
 LDAP_SLAPI_F (char **) slapi_get_supported_extended_ops LDAP_P((void));
 
 /* slapi_ops.c */
-LDAP_SLAPI_F (int) slapi_int_connection_init LDAP_P((Slapi_PBlock *pb, int OpType, Connection **pConn));
-LDAP_SLAPI_F (void) slapi_int_connection_destroy LDAP_P(( Connection **pConn ));
 LDAP_SLAPI_F (int) slapi_int_response LDAP_P(( Slapi_Operation *op, SlapReply *rs ));
+LDAP_SLAPI_F (void) slapi_int_connection_init_pb LDAP_P(( Slapi_PBlock *pb, ber_tag_t OpType ));
+LDAP_SLAPI_F (void) slapi_int_connection_done_pb LDAP_P(( Slapi_PBlock *pb ));
 
 /* slapi_pblock.c */
-LDAP_SLAPI_F (void) slapi_pblock_check_params LDAP_P(( Slapi_PBlock *pb, int flag ));
 LDAP_SLAPI_F (int) slapi_pblock_delete_param LDAP_P(( Slapi_PBlock *p, int param ));
 LDAP_SLAPI_F (void) slapi_pblock_clear LDAP_P(( Slapi_PBlock *pb ));
+LDAP_SLAPI_F (void) slapi_int_mods_free( Modifications *ml );
 
 LDAP_SLAPI_F (int) slapi_int_pblock_get_first LDAP_P(( Backend *be, Slapi_PBlock **pb ));
 LDAP_SLAPI_F (int) slapi_int_pblock_get_next LDAP_P(( Slapi_PBlock **pb ));
 
+#define PBLOCK_ASSERT_OP( _pb, _tag ) do { \
+               assert( (_pb) != NULL ); \
+               assert( (_pb)->pop != NULL ); \
+               assert( (_pb)->pconn != NULL ); \
+               if ( _tag != 0 ) \
+                       assert( (_pb)->pop->o_tag == (_tag)); \
+       } while (0)
+       
+#define PBLOCK_ASSERT_INTOP( _pb, _tag ) do { \
+               PBLOCK_ASSERT_OP( _pb, _tag ); \
+               assert( (_pb)->internal_op ); \
+               assert( pb->pop == (Operation *)pb->pconn->c_pending_ops.stqh_first ); \
+       } while (0)
+       
 /* plugin.c */
 LDAP_SLAPI_F (int) slapi_int_register_plugin LDAP_P((Backend *be, Slapi_PBlock *pPB));
 LDAP_SLAPI_F (int) slapi_int_call_plugins LDAP_P((Backend *be, int funcType, Slapi_PBlock * pPB));
 
        int                     numParams;
        int                     curParams[PBLOCK_MAX_PARAMS];
        void                    *curVals[PBLOCK_MAX_PARAMS];
+       /* native types */
+       Connection              *pconn;
+       Operation               *pop;
+       SlapReply               rs;
+       int                     internal_op;
+       char                    textbuf[ SLAP_TEXT_BUFLEN ];
 };
 
 #endif /* !NO_PBLOCK_CLASS */
 
 #define SLAPI_PLUGIN_IS_POST_FN(x) ((x) >= SLAPI_PLUGIN_POST_BIND_FN && (x) <= SLAPI_PLUGIN_BE_POST_DELETE_FN)
 
-/* really private stuff */
-#define SLAPI_X_CONFIG_ARGV                    1400
-#define SLAPI_X_INTOP_FLAGS                    1401
-#define SLAPI_X_INTOP_RESULT_CALLBACK          1402
-#define SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK    1403
-#define SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK  1404
-#define SLAPI_X_INTOP_CALLBACK_DATA            1405
-
 #define SLAPI_OPERATION_PARAMETERS             138
 
 #define        SLAPI_PLUGIN_DB_TEST_FN                 227
 
 #define SLAPI_DBSIZE                           199
 
+/* really private stuff */
+#define SLAPI_X_CONFIG_ARGV                    1400
+#define SLAPI_X_INTOP_FLAGS                    1401
+#define SLAPI_X_INTOP_RESULT_CALLBACK          1402
+#define SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK    1403
+#define SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK  1404
+#define SLAPI_X_INTOP_CALLBACK_DATA            1405
+#define SLAPI_X_OLD_RESCONTROLS                        1406
+
 #define SLAPI_IBM_PBLOCK                       -3
 
 LDAP_SLAPI_V (ldap_pvt_thread_mutex_t) slapi_hn_mutex;
 
 #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 slap_listener slapi_listener = {
+       BER_BVC("slapi://"),
+       BER_BVC("slapi://")
 };
 
-static void
-slapi_int_mods_free( Modifications *ml )
+static LDAPControl **
+slapi_int_dup_controls( LDAPControl **controls )
 {
-       Modifications           *next;
+       LDAPControl **c;
+       size_t i;
 
-       for ( ; ml != NULL; ml = next ) {
-               next = ml->sml_next;
+       if ( controls == NULL )
+               return NULL;
 
-               /* Don't free unnormalized values */
-               if ( ml->sml_nvalues != NULL ) {
-                       ber_bvarray_free( ml->sml_nvalues );
-                       ml->sml_nvalues = NULL;
-               }
-               slapi_ch_free((void **)&ml->sml_values);
-               slapi_ch_free((void **)&ml);
+       for ( i = 0; controls[i] != NULL; i++ )
+               ;
+
+       c = (LDAPControl **) slapi_ch_calloc( i + 1, sizeof(LDAPControl *) );
+
+       for ( i = 0; controls[i] != NULL; i++ ) {
+               c[i] = slapi_dup_control( controls[i] );
        }
+
+       return c;
 }
 
 static int
        Operation       *op, 
        SlapReply       *rs )
 {
-       LDAPControl             **controls = NULL;
-       size_t                  i;
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
        plugin_result_callback  prc = NULL;
        void                    *callback_data = NULL;
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       LDAPControl             **ctrls = NULL;
 
        assert( pb != NULL );   
 
-       slapi_pblock_get( pb, SLAPI_RESCONTROLS,             (void **)&controls );
        slapi_pblock_get( pb, SLAPI_X_INTOP_RESULT_CALLBACK, (void **)&prc );
        slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,   &callback_data );
 
-       assert( controls == NULL );
-
-       /* Copy these before they go out of scope */
-       if ( rs->sr_ctrls != NULL ) {
-               for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
-                       ;
-
-               controls = (LDAPControl **)slapi_ch_calloc( i + 1,
-                       sizeof(LDAPControl ));
-
-               for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
-                       controls[i] = slapi_dup_control( rs->sr_ctrls[i] );
-
-               controls[i] = NULL;
-       }
-
-       slapi_pblock_set( pb, SLAPI_RESCONTROLS,         (void *)controls );
-       slapi_pblock_set( 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 LDAP_SUCCESS;
+       return rs->sr_err;
 }
 
 static int
        Operation       *op,
        SlapReply       *rs )
 {
+       Slapi_PBlock                    *pb = SLAPI_OPERATION_PBLOCK( op );
        plugin_search_entry_callback    psec = NULL;
        void                            *callback_data = NULL;
-       Slapi_PBlock                    *pb = SLAPI_OPERATION_PBLOCK( op );
-       int                             rc = SLAP_CB_CONTINUE;
+       int                             rc = LDAP_SUCCESS;
 
        assert( pb != NULL );
 
                rc = (*psec)( rs->sr_entry, callback_data );
        }
 
-       return LDAP_SUCCESS;
+       return rc;
 }
 
 static int
                break;
        }
 
-       assert( rc != SLAP_CB_CONTINUE );
+       assert( rc != SLAP_CB_CONTINUE ); /* never try to send a wire response */
 
        return rc;
 }
 
 static int
-slapi_int_get_ctrls( Operation *op, LDAPControl **controls )
+slapi_int_get_ctrls( Slapi_PBlock *pb )
 {
        LDAPControl             **c;
-       int                     rc;
-       SlapReply               rs = { REP_RESULT };
-
-       op->o_ctrls = controls;
-       if ( op->o_ctrls == NULL ) {
-               return LDAP_SUCCESS;
-       }
+       int                     rc = LDAP_SUCCESS;
 
-       for ( c = op->o_ctrls; *c != NULL; c++ ) {
-               rc = slap_parse_ctrl( op, &rs, *c, &rs.sr_text );
-               if ( rc != LDAP_SUCCESS )
-                       break;
+       if ( pb->pop->o_ctrls != NULL ) {
+               for ( c = pb->pop->o_ctrls; *c != NULL; c++ ) {
+                       rc = slap_parse_ctrl( pb->pop, &pb->rs, *c, &pb->rs.sr_text );
+                       if ( rc != LDAP_SUCCESS )
+                               break;
+               }
        }
 
        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 )
-{
-       int                     manageDsaIt = 0, isCritical;
-       LDAPControl             **controls = NULL;
-       BackendDB               *be_op;
-
-       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 **)&be_op );
-       if ( be_op == NULL ) {
-               be_op = select_backend( &op->o_req_ndn, 0, 0 );
-               slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be_op );
-       }
-
-       op->o_bd = frontendDB; /* but we actually use frontend DB */
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_int_pblock_get_connection( Slapi_PBlock *pb, Operation *op )
-{
-       char                    *connDn = NULL;
-       Connection              *conn = op->o_conn;
-
-       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 */
-               ber_str2bv( connDn, 0, 0, &conn->c_ndn );
-               conn->c_dn = conn->c_ndn;
-       }
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_int_pblock_get_operation( Slapi_PBlock *pb, Operation *op )
-{
-       int                     isRoot = 0;
-       int                     isUpdateDn = 0;
-       char                    *requestorDn = NULL;
-       struct berval           targetDn = BER_BVNULL;
-       LDAPControl             **controls;
-       int                     rc;
-       BackendDB               *be_op;
-
-       /* All internal operations must specify a target DN */
-       slapi_pblock_get( pb, SLAPI_TARGET_DN, (void **)&targetDn.bv_val );
-       if ( 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_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 );
-       slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be_op );
-
-       /* Default authorization identity for internal operations is root DN */
-       if ( isRoot || requestorDn == NULL ) {
-               assert( be_op != NULL );
-               op->o_dn = be_op->be_rootdn;
-               op->o_ndn = be_op->be_rootndn;
-               isRoot = 1;
-       } else {
-               ber_str2bv( requestorDn, 0, 0, &op->o_ndn );
-               op->o_dn = op->o_ndn;
-       }
-
-       slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
-
-       rc = slapi_int_pblock_get_connection( pb, op );
-       if ( rc != LDAP_SUCCESS ) {
-               return rc;
-       }
-
-       slapi_pblock_get( pb, SLAPI_REQCONTROLS, (void **)&controls );
-       rc = slapi_int_get_ctrls( op, controls );
-       if ( rc != LDAP_SUCCESS ) {
-               return rc;
-       }
-
-       return LDAP_SUCCESS;
-}
-
-int
-slapi_int_connection_init( Slapi_PBlock *pb,
-       int OpType,
-       Connection **pConn )
+void
+slapi_int_connection_init_pb( Slapi_PBlock *pb, ber_tag_t OpType )
 {
        Connection              *conn;
        Operation               *op;
        ber_len_t               max = sockbuf_max_incoming;
-       int                     rc;
 
        conn = (Connection *) slapi_ch_calloc( 1, sizeof(Connection) );
 
        BER_BVZERO( &conn->c_dn );
        BER_BVZERO( &conn->c_ndn );
 
-       conn->c_listener = &slap_unknown_listener;
+       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 );
 
        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 );
 
        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 );
-
-       slapi_pblock_set( pb, SLAPI_OPERATION, op );
-       slapi_pblock_set( pb, SLAPI_CONNECTION, conn );
+       pb->pop = op;
+       pb->pconn = conn;
+       pb->internal_op = 1;
 
        ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
+}
 
-       if ( rc != LDAP_SUCCESS ) {
-               slapi_int_connection_destroy( &conn );
-               return rc;
-       }
-
-       *pConn = conn;
+static void
+slapi_int_set_operation_dn( Slapi_PBlock *pb )
+{
+       Backend                 *be;
+       Operation               *op = pb->pop;
 
-       return LDAP_SUCCESS;
+       if ( BER_BVISNULL( &op->o_ndn ) ) {
+               /* set to root DN */
+               be = select_backend( &op->o_req_ndn, 0, 0 );
+               assert( be != NULL );
+               ber_dupbv( &op->o_dn, &be->be_rootdn );
+               ber_dupbv( &op->o_ndn, &be->be_rootndn );
+       }
 }
 
-void slapi_int_connection_destroy( Connection **pConn )
+void
+slapi_int_connection_done_pb( Slapi_PBlock *pb )
 {
-       Connection              *conn = *pConn;
+       Connection              *conn;
        Operation               *op;
-       Slapi_PBlock            *pb;
 
-       if ( conn == NULL ) {
-               return;
+       PBLOCK_ASSERT_INTOP( pb, 0 );
+
+       conn = pb->pconn;
+       op = pb->pop;
+
+       /* 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 );
+               }
+               break;
+       case LDAP_REQ_ADD:
+               slapi_int_mods_free( op->ora_modlist );
+               break;
+       case LDAP_REQ_MODIFY:
+               slapi_int_mods_free( op->orm_modlist );
+               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;
        }
 
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-       pb = SLAPI_OPERATION_PBLOCK( op );
+       if ( conn->c_sb != NULL ) {
+               ber_sockbuf_free( conn->c_sb );
+       }
 
-       slap_graduate_commit_csn( op );
+       slapi_ch_free( (void **)&pb->pop->o_callback );
+       slapi_ch_free( (void **)&pb->pop );
+       slapi_ch_free( (void **)&pb->pconn );
+}
+
+static int
+slapi_int_func_internal_pb( Slapi_PBlock *pb, slap_operation_t which )
+{
+       BI_op_bind              **func;
+       SlapReply               *rs = &pb->rs;
 
-       slapi_ch_free_string( &op->o_req_dn.bv_val );
-       slapi_ch_free_string( &op->o_req_ndn.bv_val );
-       slapi_ch_free( (void **)&op->o_callback );
+       PBLOCK_ASSERT_INTOP( pb, 0 );
 
-       if ( conn->c_sb != NULL ) {
-               ber_sockbuf_free( conn->c_sb );
+       rs->sr_err = slapi_int_get_ctrls( pb );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               return rs->sr_err;
        }
 
-       slapi_pblock_set( pb, SLAPI_OPERATION,  NULL );
-       slapi_pblock_set( pb, SLAPI_CONNECTION, NULL );
+       func = &pb->pop->o_bd->be_bind;
+
+       rs->sr_err = func[which]( pb->pop, &pb->rs );
 
-       slapi_ch_free( (void **)&op );
-       slapi_ch_free( (void **)pConn );
+       return rs->sr_err;
 }
 
 int
 slapi_delete_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-
-       SlapReply               rs = { REP_RESULT };
-
        if ( pb == NULL ) {
                return -1;
        }
 
-       rs.sr_err = slapi_int_connection_init( pb, LDAP_REQ_DELETE, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
-               slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-               return 0;
-       }
-
-       op = conn->c_pending_ops.stqh_first;
-       rs.sr_err = frontendDB->be_delete( op, &rs );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_DELETE );
 
-       slapi_int_connection_destroy( &conn );
+       slapi_int_func_internal_pb( pb, op_delete );
+       slap_graduate_commit_csn( pb->pop );
 
        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;
-       char                    *dn = NULL;
-       LDAPMod                 **mods = NULL;
-       Operation               *op = NULL;
-       char                    textbuf[ SLAP_TEXT_BUFLEN ];
-       size_t                  textlen = sizeof( textbuf );
-
-       SlapReply               rs = { REP_RESULT };
+       SlapReply               *rs;
+       Slapi_Entry             *entry_orig = NULL;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_ADD_ENTRY,     (void **)&entry );
-       slapi_pblock_get( pb, SLAPI_ADD_TARGET,    (void **)&dn );
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS,   (void **)&mods );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_ADD );
+
+       rs = &pb->rs;
 
-       if ( entry != NULL ) {
-               if ( dn != NULL ) {
-                       rs.sr_err = LDAP_PARAM_ERROR;
+       entry_orig = pb->pop->ora_e;
+       pb->pop->ora_e = NULL;
+
+       if ( entry_orig != NULL ) {
+               if ( pb->pop->ora_modlist != NULL || !BER_BVISNULL( &pb->pop->o_req_ndn )) {
+                       rs->sr_err = LDAP_PARAM_ERROR;
                        goto cleanup;
                }
 
-               dn = slapi_entry_get_dn( entry );
-               slapi_pblock_set( pb, SLAPI_ADD_TARGET, dn );
-       } else if ( mods == NULL || dn == NULL ) {
-               rs.sr_err = LDAP_PARAM_ERROR;
-               goto cleanup;
-       }
-
-       rs.sr_err = slapi_int_connection_init( pb, LDAP_REQ_ADD, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+               assert( BER_BVISNULL( &pb->pop->o_req_dn ) ); /* shouldn't get set */
+               ber_dupbv( &pb->pop->o_req_dn, &entry_orig->e_name );
+               ber_dupbv( &pb->pop->o_req_ndn, &entry_orig->e_nname );
+       } else if ( pb->pop->ora_modlist == NULL || BER_BVISNULL( &pb->pop->o_req_ndn )) {
+               rs->sr_err = LDAP_PARAM_ERROR;
                goto cleanup;
        }
 
-       op = (Operation *)conn->c_pending_ops.stqh_first;
-       op->ora_e = NULL;
-       op->ora_modlist = NULL;
-
        /*
         * The caller can specify a new entry, or a target DN and set
         * of modifications, but not both.
         */
-       op->ora_e = (Entry *)slapi_ch_calloc( 1, sizeof(*entry) );
-       ber_dupbv( &op->ora_e->e_name,  &op->o_req_dn );
-       ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
-
-       if ( mods != NULL ) {
-               /* Entry just contains name; attributes are in modlist */
-               op->ora_modlist = slapi_int_ldapmods2modifications( mods );
-               if ( op->ora_modlist == NULL ) {
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
+       pb->pop->ora_e = (Entry *)slapi_ch_calloc( 1, sizeof(Entry) );
+       ber_dupbv( &pb->pop->ora_e->e_name,  &pb->pop->o_req_dn );
+       ber_dupbv( &pb->pop->ora_e->e_nname, &pb->pop->o_req_ndn );
+
+       if ( entry_orig != NULL ) {
+               assert( pb->pop->ora_modlist == NULL );
+
+               rs->sr_err = slap_entry2mods( entry_orig, &pb->pop->ora_modlist,
+                       &rs->sr_text, pb->textbuf, sizeof( pb->textbuf ) );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
                        goto cleanup;
                }
        } else {
-               rs.sr_err = slap_entry2mods( entry, &op->ora_modlist,
-                       &rs.sr_text, textbuf, textlen );
-               if ( rs.sr_err != LDAP_SUCCESS )
-                       goto cleanup;
+               assert( pb->pop->ora_modlist != NULL );
        }
 
-       rs.sr_err = slap_mods_check( op->ora_modlist, &rs.sr_text,
-               textbuf, textlen, NULL );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       rs->sr_err = slap_mods_check( pb->pop->ora_modlist, &rs->sr_text,
+               pb->textbuf, sizeof( pb->textbuf ), NULL );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                 goto cleanup;
         }
 
-       rs.sr_err = frontendDB->be_add( op, &rs );
-       if ( rs.sr_err == 0 ) {
-               if ( op->ora_e != NULL && op->o_private != NULL ) {
-                       BackendDB       *bd = op->o_bd;
+       if ( slapi_int_func_internal_pb( pb, op_add ) == 0 ) {
+               if ( pb->pop->ora_e != NULL && pb->pop->o_private != NULL ) {
+                       BackendDB       *bd = pb->pop->o_bd;
 
                        /* could we use SLAPI_BACKEND instead? */
-                       op->o_bd = (BackendDB *)op->o_private;
-                       op->o_private = NULL;
-                       be_entry_release_w( op, op->ora_e );
-                       op->ora_e = NULL;
-                       op->o_bd = bd;
-                       op->o_private = NULL;
+                       pb->pop->o_bd = (BackendDB *)pb->pop->o_private;
+                       pb->pop->o_private = NULL;
+                       be_entry_release_w( pb->pop, pb->pop->ora_e );
+                       pb->pop->ora_e = NULL;
+                       pb->pop->o_bd = bd;
+                       pb->pop->o_private = NULL;
                }
        }
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
+       slap_graduate_commit_csn( pb->pop );
 
-       slapi_entry_free( op->ora_e );
-       slapi_int_mods_free( op->ora_modlist );
-       slapi_int_connection_destroy( &conn );
+       if ( pb->pop->ora_e != NULL ) {
+               slapi_entry_free( pb->pop->ora_e );
+               pb->pop->ora_e = NULL;
+       }
+       if ( entry_orig != NULL ) {
+               pb->pop->ora_e = entry_orig;
+               slapi_int_mods_free( pb->pop->ora_modlist );
+               pb->pop->ora_modlist = NULL;
+       }
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_modrdn_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       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;
-
-       char                    *lnewrdn;
-       char                    *newsuperior;
-       int                     deloldrdn;
-
-       SlapReply               rs = { REP_RESULT };
-
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN,      (void **)&lnewrdn );
-       slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, (void **)&newsuperior );
-       slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN,   (void **)&deloldrdn );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODRDN );
 
-       rs.sr_err = slapi_int_connection_init( pb, LDAP_REQ_MODRDN, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       if ( BER_BVISEMPTY( &pb->pop->o_req_ndn ) ) {
+               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->orr_newrdn, &op->orr_nnewrdn, NULL );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
-
-       if ( rdn_validate( &op->orr_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->orr_newSup = &newSuperiorPretty;
-               op->orr_nnewSup = &newSuperiorNormalized;
-       } else {
-               op->orr_newSup = NULL;
-               op->orr_nnewSup = NULL;
-       }
-
-       op->orr_deleteoldrdn = deloldrdn;
-
-       rs.sr_err = frontendDB->be_modrdn( op, &rs );
+       slapi_int_func_internal_pb( pb, op_modrdn );
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-
-       slapi_ch_free_string( &op->orr_newrdn.bv_val );
-       slapi_ch_free_string( &op->orr_nnewrdn.bv_val );
-       slapi_ch_free_string( &newSuperiorPretty.bv_val );
-       slapi_ch_free_string( &newSuperiorNormalized.bv_val );
-
-       slapi_int_connection_destroy( &conn );
+       slap_graduate_commit_csn( pb->pop );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_modify_internal_pb( Slapi_PBlock *pb )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-       LDAPMod                 **mods = NULL;
-       char                    textbuf[ SLAP_TEXT_BUFLEN ];
-       size_t                  textlen = sizeof( textbuf );
-
-       SlapReply               rs = { REP_RESULT };
+       SlapReply               *rs;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&mods );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODIFY );
 
-       if ( mods == NULL ) {
-               rs.sr_err = LDAP_PARAM_ERROR ;
-               goto cleanup;
-       }
+       rs = &pb->rs;
 
-       rs.sr_err = slapi_int_connection_init( pb, LDAP_REQ_MODIFY, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       if ( pb->pop->orm_modlist == NULL ) {
+               rs->sr_err = LDAP_PARAM_ERROR;
                goto cleanup;
        }
 
-       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->pop->o_req_ndn ) ) {
+               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
-       op->orm_modlist = slapi_int_ldapmods2modifications( mods );
-
-       rs.sr_err = slap_mods_check( op->orm_modlist, &rs.sr_text,
-               textbuf, textlen, NULL );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
+       rs->sr_err = slap_mods_check( pb->pop->orm_modlist,
+               &rs->sr_text, pb->textbuf, sizeof( pb->textbuf ), NULL );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
                 goto cleanup;
         }
 
-       rs.sr_err = frontendDB->be_modify( op, &rs );
+       slapi_int_func_internal_pb( pb, op_modify );
 
 cleanup:
-       slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
-
-       slapi_int_mods_free( op->orm_modlist );
-       slapi_int_connection_destroy( &conn );
+       slap_graduate_commit_csn( pb->pop );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
-#ifdef LDAP_SLAPI
 static int
 slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
 {
        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 );
                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 );
 
        return LDAP_SUCCESS;
 }
-#endif /* LDAP_SLAPI */
 
 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
        plugin_search_entry_callback psec,
        plugin_referral_entry_callback prec )
 {
-#ifdef LDAP_SLAPI
-       Connection              *conn = NULL;
-       Operation               *op = NULL;
-       Filter                  *filter = NULL;
-       struct berval           fstr = BER_BVNULL;
-       AttributeName           *an = NULL;
-       const char              *text = NULL;
-
-       int                     scope = LDAP_SCOPE_BASE;
-       char                    *filStr = NULL;
-       char                    **attrs = NULL;
-       int                     attrsonly = 0;
-       int                     freeFilter = 0;
-       int                     i;
-
-       SlapReply               rs = { REP_RESULT };
+       int                     free_filter = 0;
+       SlapReply               *rs;
 
        if ( pb == NULL ) {
                return -1;
        }
 
-       slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE,     (void **)&scope );
-       slapi_pblock_get( pb, SLAPI_SEARCH_FILTER,    (void **)&filter );
-       slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, (void **)&filStr );
-       slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS,     (void **)&attrs );
-       slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, (void **)&attrsonly );
+       PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_SEARCH );
 
-       rs.sr_err = slapi_int_connection_init( pb, LDAP_REQ_SEARCH, &conn );
-       if ( rs.sr_err != LDAP_SUCCESS ) {
-               goto cleanup;
-       }
+       rs = &pb->rs;
 
        /* search callback and arguments */
        slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK,         (void *)prc );
        slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void *)prec );
        slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA,           (void *)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;
+       if ( BER_BVISEMPTY( &pb->pop->ors_filterstr )) {
+               rs->sr_err = LDAP_PARAM_ERROR;
+               goto cleanup;
        }
 
-       if ( filter == NULL ) {
-               if ( filStr == NULL ) {
-                       rs.sr_err = LDAP_PARAM_ERROR;
+       if ( pb->pop->ors_filter == NULL ) {
+               pb->pop->ors_filter = slapi_str2filter( pb->pop->ors_filterstr.bv_val );
+               if ( pb->pop->ors_filter == NULL ) {
+                       rs->sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
 
-               filter = slapi_str2filter( filStr );
-               if ( filter == NULL ) {
-                       rs.sr_err = LDAP_PROTOCOL_ERROR;
-                       goto cleanup;
-               }
-
-               freeFilter = 1;
+               free_filter = 1;
        }
 
-       filter2bv( filter, &fstr );
+       slapi_int_func_internal_pb( pb, op_search );
 
-       for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
-               ; /* count the number of attributes */
-       }
-
-       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 = 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;
+cleanup:
+       if ( free_filter ) {
+               slapi_filter_free( pb->pop->ors_filter, 1 );
+               pb->pop->ors_filter = NULL;
        }
 
-       rs.sr_type = REP_RESULT;
-       rs.sr_err = LDAP_SUCCESS;
-       rs.sr_entry = NULL; /* paranoia */
-       op->ors_scope = scope;
-       op->ors_deref = 0;
-       op->ors_slimit = SLAP_NO_LIMIT;
-       op->ors_tlimit = SLAP_NO_LIMIT;
-       op->ors_attrsonly = attrsonly;
-       op->ors_attrs = an;
-       op->ors_filter = filter;
-       op->ors_filterstr = fstr;
-
-       rs.sr_err = frontendDB->be_search( op, &rs );
-
-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 );
-       slapi_ch_free_string( &fstr.bv_val );
-       slapi_ch_free( (void **)&an );
-
-       slapi_int_connection_destroy( &conn );
-
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /* Wrappers for old API */
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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,     NULL );
+       slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,    NULL );
        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_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_DEREF,     (void *)0 );
+       slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)SLAP_NO_LIMIT );
+       slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)SLAP_NO_LIMIT );
+
+       slapi_int_set_operation_dn( pb );
 }
 
 Slapi_PBlock *
        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_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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_int_set_operation_dn( pb );
 }
 
 /* Function : 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_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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_int_set_operation_dn( pb );
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_PBlock *
        LDAPControl **controls,
        int log_changes )
 {
-#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_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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_int_set_operation_dn( pb );
 }
 
 Slapi_PBlock * 
        LDAPControl **controls, 
        int log_changes )
 {
-#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_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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_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_int_set_operation_dn( pb );
 }
 
 /* Function : 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,
        slapi_modrdn_internal_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void
        Slapi_ComponentId *plugin_identity,
        int operation_flags )
 {
-#ifdef LDAP_SLAPI
+       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_int_set_operation_dn( pb );
 }
 
 /* Function : 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_pb( pb );
 
        return pb;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
+#endif /* LDAP_SLAPI */
+
 
                return NULL;
        }
 
-       slapi_int_pblock_set_operation( pb, op );
+       pb->pop = op;
+       pb->pconn = op->o_conn;
+       pb->internal_op = 0;
+
+       PBLOCK_ASSERT_OP( pb, 0 );
 
        return pb;
 }
        Slapi_PBlock            *pb = NULL;
        slap_callback           *pcb;
 
-       /* Abstraction violating check for SLAPI internal operations */
-       /* allows pblock to remain consistent when invoking internal op plugins */
+       /*
+        * Abstraction violating check for SLAPI internal operations
+        * allows pblock to remain consistent when invoking internal
+        * op plugins
+        */
        for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
                if ( pcb->sc_response == slapi_int_response ) {
                        pb = (Slapi_PBlock *)pcb->sc_private;
+                       PBLOCK_ASSERT_INTOP( pb, 0 );
                        internal_op = 1;
                        break;
                }
        }
 
        if ( cb != NULL ) {
-               if ( !internal_op ) {
-                       cb->sc_response = slapi_over_response;
-                       cb->sc_cleanup = slapi_over_cleanup;
-               } else {
-                       cb->sc_response = NULL;
-                       cb->sc_cleanup = NULL;
-               }
-
                if ( pb == NULL ) {
                        pb = slapi_over_pblock_new( op );
                }
 
+               cb->sc_response = slapi_over_response;
+               cb->sc_cleanup = slapi_over_cleanup;
                cb->sc_private = pb;
                cb->sc_next = op->o_callback;
                op->o_callback = cb;
        for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
                ;
 
-       *a = attr_dup( attribute );
+       *a = slapi_attr_dup( attribute );
 
        return 0;
 }
        /* create a new pblock to not trample on result controls */
        pb = slapi_over_pblock_new( op );
 
-       slapi_pblock_set( pb, SLAPI_RESCONTROLS,         (void *)rs->sr_ctrls );
-       slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY, (void *)rs->sr_entry );
-
        rc = slapi_int_call_plugins( op->o_bd, type, pb );
        if ( rc >= 0 ) /* 1 means no plugins called */
                rc = SLAP_CB_CONTINUE;
        else
                rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
 
-       slapi_pblock_set( pb, SLAPI_RESCONTROLS, NULL ); /* don't free */
        slapi_pblock_destroy(pb);
 
        return rc;
 }
 
-static int
-slapi_over_count_controls( LDAPControl **controls )
-{
-       int                     i;
-
-       if ( controls == NULL )
-               return 0;
-
-       for ( i = 0; controls[i] != NULL; i++ )
-               ;
-
-       return i;
-}
-
-static int
-slapi_over_merge_controls( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       LDAPControl             **slapiControls = NULL, **mergedControls;
-       int                     nSlapiControls = 0;
-       int                     nResControls = 0;
-       int                     i;
-
-       slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapiControls );
-       nSlapiControls = slapi_over_count_controls( slapiControls );
-
-       if ( nSlapiControls == 0 ) {
-               /* short-circuit */
-               return LDAP_SUCCESS;
-       }
-
-       nResControls = slapi_over_count_controls( rs->sr_ctrls );
-
-       /* XXX this is a bit tricky, rs->sr_ctrls may have been allocated on stack */
-       mergedControls = (LDAPControl **)op->o_tmpalloc( ( nResControls + nSlapiControls + 1 ) *
-                                                        sizeof( LDAPControl *), op->o_tmpmemctx );
-       if ( mergedControls == NULL ) {
-               return LDAP_NO_MEMORY;
-       }
-
-       if ( rs->sr_ctrls != NULL ) {
-               for ( i = 0; i < nResControls; i++ )
-                       mergedControls[i] = rs->sr_ctrls[i];
-       }
-       if ( slapiControls != NULL ) {
-               for ( i = 0; i < nSlapiControls; i++ )
-                       mergedControls[nResControls + i] = slapiControls[i];
-       }
-       mergedControls[nResControls + nSlapiControls] = NULL;
-
-       if ( slapiControls != NULL ) {
-               slapi_ch_free( (void **)&slapiControls );
-               slapi_pblock_set( pb, SLAPI_RESCONTROLS, NULL ); /* don't free */
-       }
-
-       rs->sr_ctrls = mergedControls;
-
-       return LDAP_SUCCESS;
-}
-
 /*
  * Call pre- and post-result plugins
  */
 static int
 slapi_over_result( Operation *op, SlapReply *rs, int type )
 {
-       int                     rc;
        Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
 
        assert( rs->sr_type == REP_RESULT );
 
-       slapi_pblock_set( pb, SLAPI_RESULT_CODE,    (void *)rs->sr_err );
-       slapi_pblock_set( pb, SLAPI_RESULT_TEXT,    (void *)rs->sr_text );
-       slapi_pblock_set( pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
-
-       rc = slapi_int_call_plugins( op->o_bd, type, pb );
-       
-       slapi_pblock_get( pb, SLAPI_RESULT_CODE,    (void **)&rs->sr_err );
-       slapi_pblock_get( pb, SLAPI_RESULT_TEXT,    (void **)&rs->sr_text );
-       slapi_pblock_get( pb, SLAPI_RESULT_MATCHED, (void **)&rs->sr_matched );
-
-       if ( type == SLAPI_PLUGIN_PRE_RESULT_FN ) {
-               rc = slapi_over_merge_controls( op, rs );
-       }
+       slapi_int_call_plugins( op->o_bd, type, pb );
 
        return SLAP_CB_CONTINUE;
 }
 
-static int
-slapi_op_add_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-
-       slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)op->ora_e );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_bind_preop_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-
-       slapi_pblock_set( pb, SLAPI_BIND_TARGET,      (void *)op->o_req_dn.bv_val );
-       slapi_pblock_set( pb, SLAPI_BIND_METHOD,      (void *)op->orb_method );
-       slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
-       slapi_pblock_set( pb, SLAPI_CONN_DN,          NULL );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_bind_postop_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-
-       if ( rs->sr_err == LDAP_SUCCESS ) {
-               /* fix for ITS#2971 */
-               slapi_pblock_set( pb, SLAPI_CONN_DN, op->o_conn->c_authz.sai_dn.bv_val );
-       }
-
-       return LDAP_SUCCESS;
-}
 
 static int
 slapi_op_bind_callback( Operation *op, SlapReply *rs )
 {
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
        int                     rc = rs->sr_err;
 
        switch ( rc ) {
                break;
        case SLAPI_BIND_ANONYMOUS: /* undocumented */
        default: /* plugin sent result or no plugins called */
-               if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err ) != 0 ) {
-                       rs->sr_err = LDAP_OTHER;
-               }
-
                BER_BVZERO( &op->orb_edn );
 
                if ( rs->sr_err == LDAP_SUCCESS ) {
-                       slapi_pblock_get( pb, SLAPI_CONN_DN, (void *)&op->orb_edn.bv_val );
-                       if ( BER_BVISNULL( &op->orb_edn ) ) {
-                               if ( rc == 1 ) {
-                                       /* No plugins were called; continue processing */
-                                       return LDAP_SUCCESS;
-                               }
-                       } else {
-                               op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
+                       /*
+                        * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
+                        * will have set conn->c_dn and conn->c_ndn
+                        */
+                       if ( BER_BVISNULL( &op->o_conn->c_ndn ) && rc == 1 ) {
+                               /* No plugins were called; continue processing */
+                               return LDAP_SUCCESS;
                        }
-                       rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
-                               &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
-
                        ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
-                       ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
-                       ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
-                       op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
-                       BER_BVZERO( &op->o_req_dn );
-                       op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
-                       BER_BVZERO( &op->o_req_ndn );
-                       if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
+                       if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
                                ber_len_t max = sockbuf_max_incoming_auth;
                                ber_sockbuf_ctrl( op->o_conn->c_sb,
                                        LBER_SB_OPT_SET_MAX_INCOMING, &max );
                        }
+                       ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
 
                        /* log authorization identity */
                        Statslog( LDAP_DEBUG_STATS,
                                        ? "<empty>" : op->o_conn->c_dn.bv_val,
                                op->orb_tmp_mech.bv_val, 0, 0 );
 
-                       ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
                        return -1;
                }
                break;
        return rc;
 }
 
-static int
-slapi_op_compare_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-
-       slapi_pblock_set( pb, SLAPI_COMPARE_TYPE,  (void *)op->orc_ava->aa_desc->ad_cname.bv_val );
-       slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, (void *)&op->orc_ava->aa_value );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_modify_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       LDAPMod                 **modv = NULL;
-
-       modv = slapi_int_modifications2ldapmods( &op->orm_modlist );
-       slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_modify_callback( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       LDAPMod                 **modv = NULL;
-
-       /* check preoperation result code */
-       if ( rs->sr_err < 0 ) {
-               slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
-               return rs->sr_err;
-       }
-
-       /*
-        * NB: it is valid for the plugin to return no modifications
-        * (for example, a plugin might store some attributes elsewhere
-        * and remove them from the modification list; if only those
-        * attribute types were included in the modification request,
-        * then slapi_int_ldapmods2modifications() above will return
-        * NULL).
-        *
-        * However, the post-operation plugin should still be
-        * called.
-        */
-
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
-       op->orm_modlist = slapi_int_ldapmods2modifications( modv );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_modify_cleanup( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       LDAPMod                 **modv = NULL;
-
-       slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
-
-       if ( modv != NULL )
-               slapi_int_free_ldapmods( modv );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_modrdn_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-
-       slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN,      (void *)op->orr_newrdn.bv_val );
-       slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)op->orr_newSup->bv_val );
-       slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN,   (void *)op->orr_deleteoldrdn );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_search_init( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       char                    **attrs;
-
-       attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
-
-       slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE,     (void *)op->ors_scope );
-       slapi_pblock_set( pb, SLAPI_SEARCH_DEREF,     (void *)op->ors_deref );
-       slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
-       slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
-       slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,    (void *)op->ors_filter );
-       slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
-       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS,     (void *)attrs );
-       slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
-
-       return LDAP_SUCCESS;
-}
-
 static int
 slapi_op_search_callback( Operation *op, SlapReply *rs )
 {
                return rs->sr_err;
        }
 
-       if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) != 0 ) {
-               return LDAP_SUCCESS;
-       }
-
-       /*
-        * The plugin can set the SLAPI_SEARCH_FILTER.
-        * SLAPI_SEARCH_STRFILER is not normative.
-        */
-       slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
-       op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
-       filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
+       rs->sr_err = LDAP_SUCCESS;
 
-       slapi_pblock_get( pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
-       op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
-
-       if( !BER_BVISNULL( &op->o_req_ndn ) ) {
-               slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
-       }
-       rs->sr_err = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
-                                 op->o_tmpmemctx );
-       if ( rs->sr_err != LDAP_SUCCESS ) {
-               send_ldap_result( op, rs );
-               return rs->sr_err;
+       if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
+               /*
+                * The plugin can set the SLAPI_SEARCH_FILTER.
+                * SLAPI_SEARCH_STRFILER is not normative.
+                */
+               op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
+               filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
        }
 
-       slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
-       slapi_pblock_get( pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
-
-       return LDAP_SUCCESS;
-}
-
-static int
-slapi_op_search_cleanup( Operation *op, SlapReply *rs )
-{
-       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
-       char                    **attrs = NULL;
-
-       slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS,     (void *)&attrs );
-
-       if ( attrs != NULL )
-               op->o_tmpfree( attrs, op->o_tmpmemctx );
-
        return LDAP_SUCCESS;
 }
 
        int soi_postop;                 /* postoperation plugin parameter */
        int soi_internal_preop;         /* internal preoperation plugin parameter */
        int soi_internal_postop;        /* internal postoperation plugin parameter */
-       slap_response *soi_preop_init;  /* preoperation pblock init function */
        slap_response *soi_callback;    /* preoperation result handler */
-       slap_response *soi_postop_init; /* postoperation pblock init function */
-       slap_response *soi_cleanup;     /* cleanup function (not for internal ops) */
 } slapi_op_dispatch_table[] = {
        {
                SLAPI_PLUGIN_PRE_BIND_FN,
                SLAPI_PLUGIN_POST_BIND_FN,
                0,
                0,
-               slapi_op_bind_preop_init,
-               slapi_op_bind_callback,
-               slapi_op_bind_postop_init,
-               NULL
+               slapi_op_bind_callback
        },
        {
                SLAPI_PLUGIN_PRE_UNBIND_FN,
                SLAPI_PLUGIN_POST_UNBIND_FN,
                0,
                0,
-               NULL,
-               NULL,
-               NULL,
                NULL
        },
        {
                SLAPI_PLUGIN_POST_SEARCH_FN,
                0,
                0,
-               slapi_op_search_init,
-               slapi_op_search_callback,
-               NULL,
-               slapi_op_search_cleanup
+               slapi_op_search_callback
        },
        {
                SLAPI_PLUGIN_PRE_COMPARE_FN,
                SLAPI_PLUGIN_POST_COMPARE_FN,
                0,
                0,
-               slapi_op_compare_init,
-               NULL,
-               NULL,
                NULL
        },
        {
                SLAPI_PLUGIN_POST_MODIFY_FN,
                SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
                SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
-               slapi_op_modify_init,
-               slapi_op_modify_callback,
-               NULL,
-               slapi_op_modify_cleanup
+               NULL
        },
        {
                SLAPI_PLUGIN_PRE_MODRDN_FN,
                SLAPI_PLUGIN_POST_MODRDN_FN,
                SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
                SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
-               slapi_op_modrdn_init,
-               NULL,
-               NULL,
                NULL
        },
        {
                SLAPI_PLUGIN_POST_ADD_FN,
                SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
                SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
-               slapi_op_add_init,
-               NULL,
-               NULL,
                NULL
        },
        {
                SLAPI_PLUGIN_POST_DELETE_FN,
                SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
                SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
-               NULL,
-               NULL,
-               NULL,
                NULL
        },
        {
                SLAPI_PLUGIN_POST_ABANDON_FN,
                0,
                0,
-               NULL,
-               NULL,
-               NULL,
                NULL
        },
        {
                0,
                0,
                0,
-               NULL,
-               NULL,
-               NULL,
                NULL
        }
 };
        return op;
 }
 
+/* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
+ * rs->sr_ctrls could be allocated on the stack */
+static int
+slapi_over_merge_controls( Operation *op, SlapReply *rs )
+{
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       LDAPControl             **ctrls = NULL;
+       LDAPControl             **slapi_ctrls = NULL;
+       size_t                  n_slapi_ctrls = 0;
+       size_t                  n_rs_ctrls = 0;
+       size_t                  i;
+
+       slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
+
+       n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
+       n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
+
+       slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
+
+       if ( n_slapi_ctrls == 0 )
+               return LDAP_SUCCESS; /* no SLAPI controls */
+
+       ctrls = (LDAPControl **) op->o_tmpalloc(
+               ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
+               op->o_tmpmemctx );
+
+       for ( i = 0; i < n_slapi_ctrls; i++ ) {
+               ctrls[i] = slapi_ctrls[i];
+       }
+       if ( rs->sr_ctrls != NULL ) {
+               for ( i = 0; i < n_rs_ctrls; i++ ) {
+                       ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
+               }
+       }
+
+       rs->sr_ctrls = ctrls;
+
+       return LDAP_SUCCESS;
+}
+
+static int
+slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
+{
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       LDAPControl             **rs_ctrls = NULL;
+
+       slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
+
+       if ( rs->sr_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
+               /* no copying done */
+               return LDAP_SUCCESS;
+       }
+
+       op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
+       rs->sr_ctrls = rs_ctrls;
+
+       return LDAP_SUCCESS;
+}
+
 static int
 slapi_over_response( Operation *op, SlapReply *rs )
 {
-       int                     rc;
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       int                     rc = SLAP_CB_CONTINUE;
 
-       switch ( rs->sr_type ) {
-       case REP_RESULT:
-               rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
-               break;
-       case REP_SEARCH:
-               rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
-               break;
-       case REP_SEARCHREF:
-               rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
-               break;
-       default:
-               rc = SLAP_CB_CONTINUE;
-               break;
+       if ( pb->internal_op == 0 ) {
+               switch ( rs->sr_type ) {
+               case REP_RESULT:
+                       rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
+                       break;
+               case REP_SEARCH:
+                       rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
+                       break;
+               case REP_SEARCHREF:
+                       rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
+                       break;
+               default:
+                       break;
+               }
        }
 
+       slapi_over_merge_controls( op, rs );
+
        return rc;
 }
 
 static int
 slapi_over_cleanup( Operation *op, SlapReply *rs )
 {
-       int                     rc;
+       Slapi_PBlock            *pb = SLAPI_OPERATION_PBLOCK( op );
+       int                     rc = SLAP_CB_CONTINUE;
 
-       switch ( rs->sr_type ) {
-       case REP_RESULT:
-               rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
-               break;
-       case REP_SEARCH:
-               rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
-               break;
-       case REP_SEARCHREF:
-               rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
-               break;
-       default:
-               rc = SLAP_CB_CONTINUE;
-               break;
+       slapi_over_unmerge_controls( op, rs );
+
+       if ( pb->internal_op == 0 ) {
+               switch ( rs->sr_type ) {
+               case REP_RESULT:
+                       rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
+                       break;
+               case REP_SEARCH:
+                       rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
+                       break;
+               case REP_SEARCHREF:
+                       rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
+                       break;
+               default:
+                       break;
+               }
        }
 
        return rc;
 
        if ( preop_type == 0 ) {
                /* no SLAPI plugin types for this operation */
-               rs->sr_err = SLAP_CB_CONTINUE;
-               goto cleanup;
+               if ( !internal_op ) {
+                       slapi_pblock_destroy(pb);
+                       cb.sc_private = NULL;
+               }
+               op->o_callback = cb.sc_next;
+               return SLAP_CB_CONTINUE;
        }
 
        pb = SLAPI_OPERATION_PBLOCK( op );
 
-       slapi_pblock_set( pb, SLAPI_TARGET_ADDRESS, NULL );
-       slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, NULL );
-       slapi_pblock_set( pb, SLAPI_TARGET_DN, (void *)op->o_req_dn.bv_val );
-
-       /*
-        * Call preoperation plugins 
-        */
-       if ( opinfo->soi_preop_init != NULL ) {
-               rs->sr_err = (opinfo->soi_preop_init)( op, rs );
-               if ( rs->sr_err != LDAP_SUCCESS )
-                       goto cleanup;
-       }
-
        rs->sr_err = slapi_int_call_plugins( op->o_bd, preop_type, pb );
 
        /*
         */
        if ( opinfo->soi_callback == NULL ) {
                /* default behaviour is preop plugin can abort operation */
-               if ( rs->sr_err < 0 ) {
-                       slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
+               if ( rs->sr_err < 0 )
                        goto cleanup;
-               } else {
+               else
                        rs->sr_err = LDAP_SUCCESS;
-               }
        } else {
                rc = (opinfo->soi_callback)( op, rs );
                if ( rc )
        /*
         * Call postoperation plugins
         */
-       slapi_pblock_set( pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
-
-       if ( opinfo->soi_postop_init != NULL ) {
-               (opinfo->soi_postop_init)( op, rs );
-       }
-
        slapi_int_call_plugins( op->o_bd, postop_type, pb );
 
 cleanup:
        if ( !internal_op ) {
-               if ( opinfo->soi_cleanup != NULL ) {
-                       (opinfo->soi_cleanup)( op, rs );
-               }
                slapi_pblock_destroy(pb);
                cb.sc_private = NULL;
        }
        Slapi_PBlock    *pb;
        SLAPI_FUNC      callback;
        int             rc;
-       struct berval   reqdata = BER_BVNULL;
        int             internal_op;
        slap_callback   cb;
 
 
        pb = SLAPI_OPERATION_PBLOCK( op );
 
-       if ( op->ore_reqdata != NULL ) {
-               reqdata = *op->ore_reqdata;
-       }
-
-       slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,   (void *)op->ore_reqoid.bv_val);
-       slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE, (void *)&reqdata);
-
        rc = (*callback)( pb );
        if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
                slapi_pblock_destroy( pb );
                return SLAP_CB_CONTINUE;
        }
 
-       slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,   (void **)&rs->sr_rspoid );
-       slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, (void **)&rs->sr_rspdata );
+       assert( rs->sr_rspoid != NULL );
 
-       rs->sr_err = rc;
        send_ldap_extended( op, rs );
 
+#if 0
        slapi_ch_free_string( (char **)&rs->sr_rspoid );
+#endif
 
        if ( rs->sr_rspdata != NULL )
                ber_bvfree( rs->sr_rspdata );
                if ( rc >= 0 ) /* 1 means no plugins called */
                        rc = SLAP_CB_CONTINUE;
                else
-                       slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rc );
+                       rc = pb->rs.sr_err;
 
                if ( !internal_op )
                        slapi_pblock_destroy( pb );
 
 #include <slap.h>
 #include <slapi.h>
 
+#ifdef LDAP_SLAPI
+
+/* some parameters require a valid connection and operation */
+#define PBLOCK_LOCK_CONN( _pb )                do { \
+               ldap_pvt_thread_mutex_lock( &(_pb)->pconn->c_mutex ); \
+       } while (0)
+
+#define PBLOCK_UNLOCK_CONN( _pb )      do { \
+               ldap_pvt_thread_mutex_unlock( &(_pb)->pconn->c_mutex ); \
+       } while (0)
+
+/* some parameters are only settable for internal operations */
+#define PBLOCK_VALIDATE_IS_INTOP( _pb )        do { if ( (_pb)->internal_op == 0 ) break; } while ( 0 )
+
 static slapi_pblock_class_t 
-getPBlockClass( int param ) 
+pblock_get_param_class( int param ) 
 {
        switch ( param ) {
        case SLAPI_PLUGIN_TYPE:
        case SLAPI_PLUGIN_PRE_ENTRY_FN:
        case SLAPI_PLUGIN_PRE_REFERRAL_FN:
        case SLAPI_PLUGIN_PRE_RESULT_FN:
+       case SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN:
+       case SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN:
+       case SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN:
+       case SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN:
+       case SLAPI_PLUGIN_BE_PRE_ADD_FN:
+       case SLAPI_PLUGIN_BE_PRE_MODIFY_FN:
+       case SLAPI_PLUGIN_BE_PRE_MODRDN_FN:
+       case SLAPI_PLUGIN_BE_PRE_DELETE_FN:
        case SLAPI_PLUGIN_POST_BIND_FN:
        case SLAPI_PLUGIN_POST_UNBIND_FN:
        case SLAPI_PLUGIN_POST_SEARCH_FN:
        case SLAPI_PLUGIN_POST_ENTRY_FN:
        case SLAPI_PLUGIN_POST_REFERRAL_FN:
        case SLAPI_PLUGIN_POST_RESULT_FN:
+       case SLAPI_PLUGIN_INTERNAL_POST_ADD_FN:
+       case SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN:
+       case SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN:
+       case SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN:
+       case SLAPI_PLUGIN_BE_POST_ADD_FN:
+       case SLAPI_PLUGIN_BE_POST_MODIFY_FN:
+       case SLAPI_PLUGIN_BE_POST_MODRDN_FN:
+       case SLAPI_PLUGIN_BE_POST_DELETE_FN:
        case SLAPI_PLUGIN_MR_FILTER_CREATE_FN:
        case SLAPI_PLUGIN_MR_INDEXER_CREATE_FN:
        case SLAPI_PLUGIN_MR_FILTER_MATCH_FN:
        case SLAPI_ENTRY_PRE_OP:
        case SLAPI_ENTRY_POST_OP:
        case SLAPI_RESCONTROLS:
+       case SLAPI_X_OLD_RESCONTROLS:
        case SLAPI_ADD_RESCONTROL:
+       case SLAPI_CONTROLS_ARG:
        case SLAPI_ADD_ENTRY:
        case SLAPI_ADD_EXISTING_DN_ENTRY:
        case SLAPI_ADD_PARENT_ENTRY:
 }
 
 static void
-Lock( Slapi_PBlock *pb )
+pblock_lock( Slapi_PBlock *pb )
 {
        ldap_pvt_thread_mutex_lock(&pb->pblockMutex);
 }
 
 static void
-unLock( Slapi_PBlock *pb )
+pblock_unlock( Slapi_PBlock *pb )
 {
        ldap_pvt_thread_mutex_unlock(&pb->pblockMutex);
 }
 
 static int 
-get( Slapi_PBlock *pb, int param, void **val ) 
+pblock_get_default( Slapi_PBlock *pb, int param, void **value ) 
 {      
        int i;
        slapi_pblock_class_t pbClass;
 
-       pbClass = getPBlockClass( param );
+       pbClass = pblock_get_param_class( param );
        if ( pbClass == PBLOCK_CLASS_INVALID ) {
                return PBLOCK_ERROR;
        }
        
-       Lock( pb );
-
        switch ( pbClass ) {
        case PBLOCK_CLASS_INTEGER:
-               *((int *)val) = 0;
+               *((int *)value) = 0;
                break;
        case PBLOCK_CLASS_LONG_INTEGER:
-               *((long *)val) = 0L;
+               *((long *)value) = 0L;
                break;
        case PBLOCK_CLASS_POINTER:
        case PBLOCK_CLASS_FUNCTION_POINTER:
-               *val = NULL;
+               *value = NULL;
                break;
+       case PBLOCK_CLASS_INVALID:
+               return PBLOCK_ERROR;
        }
 
        for ( i = 0; i < pb->numParams; i++ ) {
                if ( pb->curParams[i] == param ) {
                        switch ( pbClass ) {
                        case PBLOCK_CLASS_INTEGER:
-                               *((int *)val) = (int)pb->curVals[i];
+                               *((int *)value) = (int)pb->curVals[i];
                                break;
                        case PBLOCK_CLASS_LONG_INTEGER:
-                               *((long *)val) = (long)pb->curVals[i];
+                               *((long *)value) = (long)pb->curVals[i];
                                break;
                        case PBLOCK_CLASS_POINTER:
                        case PBLOCK_CLASS_FUNCTION_POINTER:
-                               *val = pb->curVals[i];
+                               *value = pb->curVals[i];
                                break;
                        default:
                                break;
                        break;
                }
        }
-       unLock( pb );   
+
        return PBLOCK_SUCCESS;
 }
 
+static char *
+pblock_get_authtype( AuthorizationInformation *authz, int is_tls )
+{
+       char *authType;
+
+       switch ( authz->sai_method ) {
+       case LDAP_AUTH_SASL:
+               authType = SLAPD_AUTH_SASL;
+               break;
+       case LDAP_AUTH_SIMPLE:
+               authType = SLAPD_AUTH_SIMPLE;
+               break;
+       case LDAP_AUTH_NONE:
+               authType = SLAPD_AUTH_NONE;
+               break;
+       default:
+               authType = NULL;
+               break;
+       }
+
+       if ( is_tls && authType == NULL ) {
+               authType = SLAPD_AUTH_SSL;
+       }
+
+       return authType;
+}
+
 static int 
-set( Slapi_PBlock *pb, int param, void *val ) 
+pblock_set_default( Slapi_PBlock *pb, int param, void *value ) 
 {
-#if defined(LDAP_SLAPI)
-       int i, freeit;
-       int addcon = 0;
        slapi_pblock_class_t pbClass;
+       size_t i;
 
-       pbClass = getPBlockClass( param );
+       pbClass = pblock_get_param_class( param );
        if ( pbClass == PBLOCK_CLASS_INVALID ) {
                return PBLOCK_ERROR;
        }
 
-       Lock( pb );     
-
        if ( pb->numParams == PBLOCK_MAX_PARAMS ) {
-               unLock( pb );
-               return PBLOCK_ERROR; 
+               return PBLOCK_ERROR;
        }
 
-       if ( param == SLAPI_ADD_RESCONTROL ) {
-               addcon = 1;
-               param = SLAPI_RES_CONTROLS;
+       for ( i = 0; i < pb->numParams; i++ ) { 
+               if ( pb->curParams[i] == param ) {
+                       break;
+               }
        }
 
+       if ( i >= pb->numParams ) {
+               pb->curParams[i] = param;
+               pb->numParams++;
+       }
+
+       pb->curVals[i] = value;
+
+       return PBLOCK_SUCCESS;
+}
+
+static int 
+pblock_get( Slapi_PBlock *pb, int param, void **value ) 
+{
+       int rc = PBLOCK_SUCCESS;
+
+       pblock_lock( pb );
+
        switch ( param ) {
-        case SLAPI_CONN_DN:
-        case SLAPI_CONN_AUTHMETHOD:
+       case SLAPI_OPERATION:
+               *value = pb->pop;
+               break;
+       case SLAPI_OPINITIATED_TIME:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((time_t *)value) = pb->pop->o_time;
+               break;
+       case SLAPI_OPERATION_ID:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((unsigned long *)value) = pb->pop->o_opid;
+               break;
+       case SLAPI_OPERATION_TYPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((ber_tag_t *)value) = pb->pop->o_tag;
+               break;
+       case SLAPI_REQCONTROLS:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((LDAPControl ***)value) = pb->pop->o_ctrls;
+               break;
+       case SLAPI_REQUESTOR_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((char **)value) = pb->pop->o_ndn.bv_val;
+               break;
+       case SLAPI_MANAGEDSAIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((int *)value) = get_manageDSAit( pb->pop );
+               break;
+       case SLAPI_BACKEND:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((BackendDB **)value) = pb->pop->o_bd;
+               break;
+       case SLAPI_BE_TYPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_bd != NULL )
+                       *((char **)value) = pb->pop->o_bd->bd_info->bi_type;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_CONNECTION:
+               *value = pb->pconn;
+               break;
+       case SLAPI_X_CONN_SSF:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((slap_ssf_t *)value) = pb->pconn->c_ssf;
+               break;
+       case SLAPI_X_CONN_SASL_CONTEXT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pconn->c_sasl_authctx != NULL )
+                       *value = pb->pconn->c_sasl_authctx;
+               else
+                       *value = pb->pconn->c_sasl_sockctx;
+               break;
+       case SLAPI_TARGET_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((char **)value) = pb->pop->o_req_ndn.bv_val;
+               break;
+       case SLAPI_REQUESTOR_ISROOT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((int *)value) = be_isroot( pb->pop );
+               break;
+       case SLAPI_REQUESTOR_ISUPDATEDN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((int *)value) = be_isupdate( pb->pop );
+               break;
+       case SLAPI_CONN_AUTHTYPE:
+       case SLAPI_CONN_AUTHMETHOD: /* XXX should return SASL mech */
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((char **)value) = pblock_get_authtype( &pb->pconn->c_authz,
+#ifdef HAVE_TLS
+                                                        pb->pconn->c_is_tls
+#else
+                                                        0
+#endif
+                                                        );
+               break;
+       case SLAPI_X_CONN_IS_UDP:
+               PBLOCK_ASSERT_OP( pb, 0 );
+#ifdef LDAP_CONNECTIONLESS
+               *((int *)value) = pb->pconn->c_is_udp;
+#else
+               *((int *)value) = 0;
+#endif
+               break;
+       case SLAPI_CONN_ID:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((unsigned long *)value) = pb->pconn->c_connid;
+               break;
+       case SLAPI_CONN_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               *((char **)value) = pb->pconn->c_dn.bv_val;
+               break;
+       case SLAPI_CONN_CLIENTIP:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( strncmp( pb->pconn->c_peer_name.bv_val, "IP=", 3 ) == 0 )
+                       *((char **)value) = &pb->pconn->c_peer_name.bv_val[3];
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_X_CONN_CLIENTPATH:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( strncmp( pb->pconn->c_peer_name.bv_val, "PATH=", 3 ) == 0 )
+                       *((char **)value) = &pb->pconn->c_peer_name.bv_val[5];
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_CONN_SERVERIP:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( strncmp( pb->pconn->c_peer_name.bv_val, "IP=", 3 ) == 0 )
+                       *((char **)value) = &pb->pconn->c_sock_name.bv_val[3];
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_X_CONN_SERVERPATH:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( strncmp( pb->pconn->c_peer_name.bv_val, "PATH=", 3 ) == 0 )
+                       *((char **)value) = &pb->pconn->c_sock_name.bv_val[5];
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_RESULT_CODE:
+       case SLAPI_PLUGIN_INTOP_RESULT:
+               *((int *)value) = pb->rs.sr_err;
+               break;
         case SLAPI_RESULT_TEXT:
+               *((const char **)value) = pb->rs.sr_text;
+               break;
         case SLAPI_RESULT_MATCHED:
-               freeit = 1;
+               *((const char **)value) = pb->rs.sr_matched;
+               break;
+       case SLAPI_ADD_ENTRY:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_ADD )
+                       *((Slapi_Entry **)value) = pb->pop->ora_e;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_MODIFY_MODS: {
+               LDAPMod **mods = NULL;
+
+               pblock_get_default( pb, param, (void **)&mods );
+               if ( mods == NULL && pb->internal_op == 0 ) {
+                       if ( pb->pop->o_tag != LDAP_REQ_MODIFY ) {
+                               rc = PBLOCK_ERROR;
+                               break;
+                       }
+                       mods = slapi_int_modifications2ldapmods( &pb->pop->orm_modlist, NULL );
+                       pblock_set_default( pb, param, (void *)mods );
+               }
+               *((LDAPMod ***)value) = mods;
+               break;
+       }
+       case SLAPI_MODRDN_NEWRDN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN )
+                       *((char **)value) = pb->pop->orr_newrdn.bv_val;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_MODRDN_NEWSUPERIOR:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN && pb->pop->orr_newSup != NULL )
+                       *((char **)value) = pb->pop->orr_newSup->bv_val;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_MODRDN_DELOLDRDN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN )
+                       *((int *)value) = pb->pop->orr_deleteoldrdn;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_SCOPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((int *)value) = pb->pop->ors_scope;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_DEREF:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((int *)value) = pb->pop->ors_deref;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_SIZELIMIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((int *)value) = pb->pop->ors_slimit;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_TIMELIMIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((int *)value) = pb->pop->ors_tlimit;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_FILTER:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((Slapi_Filter **)value) = pb->pop->ors_filter;
+               else
+                       *((Slapi_Filter **)value) = NULL;
+               break;
+       case SLAPI_SEARCH_STRFILTER:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((char **)value) = pb->pop->ors_filterstr.bv_val;
+               else
+                       *((char **)value) = NULL;
+               break;
+       case SLAPI_SEARCH_ATTRS: {
+               char **attrs = NULL;
+
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag != LDAP_REQ_SEARCH ) {
+                       rc = PBLOCK_ERROR;
+                       break;
+               }
+               pblock_get_default( pb, param, (void **)&attrs );
+               if ( attrs == NULL && pb->internal_op == 0 ) {
+                       attrs = anlist2charray_x( pb->pop->ors_attrs, 0, pb->pop->o_tmpmemctx );
+                       pblock_set_default( pb, param, (void *)attrs );
+               }
+               *((char ***)attrs) = attrs;
+       }
+       case SLAPI_SEARCH_ATTRSONLY:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       *((int *)value) = pb->pop->ors_attrsonly;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_SEARCH_RESULT_ENTRY:
+               *((Slapi_Entry **)value) = pb->rs.sr_entry;
+               break;
+       case SLAPI_BIND_RET_SASLCREDS:
+               *((struct berval **)value) = pb->rs.sr_sasldata;
+               break;
+       case SLAPI_EXT_OP_REQ_OID:
+               *((const char **)value) = pb->pop->ore_reqoid.bv_val;
+               break;
+       case SLAPI_EXT_OP_REQ_VALUE:
+               *((struct berval **)value) = pb->pop->ore_reqdata;
+               break;
+       case SLAPI_EXT_OP_RET_OID:
+               *((const char **)value) = pb->rs.sr_rspoid;
+               break;
+       case SLAPI_EXT_OP_RET_VALUE:
+               *((struct berval **)value) = pb->rs.sr_rspdata;
+               break;
+       case SLAPI_BIND_METHOD:
+               if ( pb->pop->o_tag == LDAP_REQ_BIND )
+                       *((int *)value) = pb->pop->orb_method;
+               else
+                       *((int *)value) = 0;
+               break;
+       case SLAPI_BIND_CREDENTIALS:
+               if ( pb->pop->o_tag == LDAP_REQ_BIND )
+                       *((struct berval **)value) = &pb->pop->orb_cred;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_COMPARE_TYPE:
+               if ( pb->pop->o_tag == LDAP_REQ_COMPARE )
+                       *((char **)value) = pb->pop->orc_ava->aa_desc->ad_cname.bv_val;
+               else
+                       *value = NULL;
+               break;
+       case SLAPI_COMPARE_VALUE:
+               if ( pb->pop->o_tag == LDAP_REQ_COMPARE )
+                       *((struct berval **)value) = &pb->pop->orc_ava->aa_value;
+               else
+                       *value = NULL;
                break;
        default:
-               freeit = 0;
+               rc = pblock_get_default( pb, param, value );
                break;
        }
-       for( i = 0; i < pb->numParams; i++ ) { 
-               if ( pb->curParams[i] == param ) {
+
+       pblock_unlock( pb );
+
+       return rc;
+}
+
+static int
+pblock_add_control( Slapi_PBlock *pb, LDAPControl *control )
+{
+       LDAPControl **controls = NULL;
+       size_t i;
+
+       pblock_get_default( pb, SLAPI_RESCONTROLS, (void **)&controls );
+
+       if ( controls != NULL ) {
+               for ( i = 0; controls[i] != NULL; i++ )
+                       ;
+       } else {
+               i = 0;
+       }
+
+       controls = (LDAPControl **)slapi_ch_realloc( (char *)controls,
+               ( i + 2 ) * sizeof(LDAPControl *));
+       controls[i] = control;
+
+       return pblock_set_default( pb, SLAPI_RESCONTROLS, (void *)controls );
+}
+
+static int
+pblock_set_dn( void *value, struct berval *dn, struct berval *ndn, void *memctx )
+{
+       struct berval bv;
+
+       if ( !BER_BVISNULL( dn )) {
+               slap_sl_free( dn->bv_val, memctx );
+               BER_BVZERO( dn );
+       }
+       if ( !BER_BVISNULL( ndn )) {
+               slap_sl_free( ndn->bv_val, memctx );
+               BER_BVZERO( ndn );
+       }
+
+       bv.bv_val = (char *)value;
+       bv.bv_len = strlen( bv.bv_val );
+
+       return dnPrettyNormal( NULL, &bv, dn, ndn, memctx );
+}
+
+static int 
+pblock_set( Slapi_PBlock *pb, int param, void *value ) 
+{
+       int rc = PBLOCK_SUCCESS;
+
+       pblock_lock( pb );      
+
+       switch ( param ) {
+       case SLAPI_OPERATION:
+               pb->pop = (Operation *)value;
+               break;
+       case SLAPI_OPINITIATED_TIME:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_time = (time_t)value;
+               break;
+       case SLAPI_OPERATION_ID:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_opid = (unsigned long)value;
+               break;
+       case SLAPI_OPERATION_TYPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_tag = (ber_tag_t)value;
+               break;
+       case SLAPI_REQCONTROLS:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_ctrls = (LDAPControl **)value;
+               break;
+       case SLAPI_RESCONTROLS: {
+               LDAPControl **ctrls = NULL;
+
+               pblock_get_default( pb, param, (void **)&ctrls );
+               if ( ctrls == NULL ) {
+                       /* free old ones first */
+                       ldap_controls_free( ctrls );
+               }
+               rc = pblock_set_default( pb, param, value );
+               break;
+       }
+       case SLAPI_ADD_RESCONTROL:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               rc = pblock_add_control( pb, (LDAPControl *)value );
+               break;
+       case SLAPI_REQUESTOR_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               rc = pblock_set_dn( value, &pb->pop->o_dn, &pb->pop->o_ndn, pb->pop->o_tmpmemctx );
+               break;
+       case SLAPI_MANAGEDSAIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_managedsait = (int)value;
+               break;
+       case SLAPI_BACKEND:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->pop->o_bd = (BackendDB *)value;
+               break;
+       case SLAPI_CONNECTION:
+               pb->pconn = (Connection *)value;
+               break;
+       case SLAPI_X_CONN_SSF:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_LOCK_CONN( pb );
+               pb->pconn->c_ssf = (slap_ssf_t)value;
+               PBLOCK_UNLOCK_CONN( pb );
+               break;
+       case SLAPI_X_CONN_SASL_CONTEXT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_LOCK_CONN( pb );
+               pb->pconn->c_sasl_authctx = value;
+               PBLOCK_UNLOCK_CONN( pb );
+               break;
+       case SLAPI_TARGET_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               rc = pblock_set_dn( value, &pb->pop->o_req_dn, &pb->pop->o_req_ndn, pb->pop->o_tmpmemctx );
+               break;
+       case SLAPI_CONN_ID:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_LOCK_CONN( pb );
+               pb->pconn->c_connid = (unsigned long)value;
+               PBLOCK_UNLOCK_CONN( pb );
+               break;
+       case SLAPI_CONN_DN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_LOCK_CONN( pb );
+               rc = pblock_set_dn( value, &pb->pconn->c_dn, &pb->pconn->c_ndn, NULL );
+               PBLOCK_UNLOCK_CONN( pb );
+               break;
+       case SLAPI_RESULT_CODE:
+       case SLAPI_PLUGIN_INTOP_RESULT:
+               pb->rs.sr_err = (int)value;
+               break;
+       case SLAPI_RESULT_TEXT:
+               snprintf( pb->textbuf, sizeof( pb->textbuf ), "%s", (char *)value );
+               pb->rs.sr_text = pb->textbuf;
+               break;
+       case SLAPI_RESULT_MATCHED:
+               pb->rs.sr_matched = (char *)value; /* XXX should dup? */
+               break;
+       case SLAPI_ADD_ENTRY:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_ADD ) {
+                       pb->pop->ora_e = (Slapi_Entry *)value;
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_MODIFY_MODS: {
+               Modifications **mlp;
+
+               PBLOCK_ASSERT_OP( pb, 0 );
+               rc = pblock_set_default( pb, param, value );
+               if ( rc != PBLOCK_SUCCESS ) {
                        break;
                }
+
+               if ( pb->pop->o_tag == LDAP_REQ_MODIFY ) {
+                       mlp = &pb->pop->orm_modlist;
+               } else if ( pb->pop->o_tag == LDAP_REQ_ADD ) {
+                       mlp = &pb->pop->ora_modlist;
+               } else {
+                       break;
+               }
+
+               if ( *mlp != NULL ) {
+                       if ( pb->internal_op )
+                               slapi_int_mods_free( *mlp ); /* caller owns values */
+                       else
+                               slap_mods_free( *mlp );  /* we own values */
+                       *mlp = NULL;
+               }
+               *mlp = slapi_int_ldapmods2modifications( (LDAPMod **)value, NULL );
+               break;
        }
+       case SLAPI_MODRDN_NEWRDN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN ) {
+                       rc = pblock_set_dn( value, &pb->pop->orr_newrdn, &pb->pop->orr_nnewrdn, pb->pop->o_tmpmemctx );
+                       if ( rc == LDAP_SUCCESS ) rc = rdn_validate( &pb->pop->orr_nnewrdn );
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_MODRDN_NEWSUPERIOR:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN ) {
+                       if ( value == NULL ) {
+                               if ( pb->pop->orr_newSup != NULL ) {
+                                       pb->pop->o_tmpfree( pb->pop->orr_newSup, pb->pop->o_tmpmemctx );
+                                       pb->pop->orr_newSup = NULL;
+                               }
+                               if ( pb->pop->orr_newSup != NULL ) {
+                                       pb->pop->o_tmpfree( pb->pop->orr_nnewSup, pb->pop->o_tmpmemctx );
+                                       pb->pop->orr_nnewSup = NULL;
+                               }
+                       } else {
+                               if ( pb->pop->orr_newSup == NULL ) {
+                                       pb->pop->orr_newSup = (struct berval *)pb->pop->o_tmpalloc(
+                                               sizeof(struct berval), pb->pop->o_tmpmemctx );
+                               }
+                               if ( pb->pop->orr_nnewSup == NULL ) {
+                                       pb->pop->orr_nnewSup = (struct berval *)pb->pop->o_tmpalloc(
+                                               sizeof(struct berval), pb->pop->o_tmpmemctx );
+                               }
+                               rc = pblock_set_dn( value, pb->pop->orr_newSup, pb->pop->orr_nnewSup, pb->pop->o_tmpmemctx );
+                       }
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_MODRDN_DELOLDRDN:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+               if ( pb->pop->o_tag == LDAP_REQ_MODRDN )
+                       pb->pop->orr_deleteoldrdn = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_SEARCH_SCOPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH ) {
+                       switch ( (int)value ) {
+                       case LDAP_SCOPE_BASE:
+                       case LDAP_SCOPE_ONELEVEL:
+                       case LDAP_SCOPE_SUBTREE:
+#ifdef LDAP_SCOPE_SUBORDINATE
+                       case LDAP_SCOPE_SUBORDINATE:
+#endif
+                               pb->pop->ors_scope = (int)value;
+                               break;
+                       default:
+                               rc = PBLOCK_ERROR;
+                               break;
+                       }
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_SEARCH_DEREF:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       pb->pop->ors_deref = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_SEARCH_SIZELIMIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       pb->pop->ors_slimit = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_SEARCH_TIMELIMIT:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       pb->pop->ors_tlimit = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_SEARCH_FILTER:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH ) {
+                       pb->pop->ors_filter = (Slapi_Filter *)value;
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_SEARCH_STRFILTER:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH ) {
+                       pb->pop->ors_filterstr.bv_val = (char *)value;
+                       pb->pop->ors_filterstr.bv_len = strlen((char *)value);
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_SEARCH_ATTRS: {
+               AttributeName *an = NULL;
+               size_t i = 0;
+               char **attrs = (char **)value;
 
-       if ( i >= pb->numParams ) {
-               pb->curParams[i] = param;
-               pb->numParams++;
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag != LDAP_REQ_SEARCH ) {
+                       rc = PBLOCK_ERROR;
+                       break;
+               }
+               /* also set mapped attrs */
+               rc = pblock_set_default( pb, param, value );
+               if ( rc != PBLOCK_SUCCESS ) {
+                       break;
+               }
+               if ( pb->pop->ors_attrs != NULL ) {
+                       pb->pop->o_tmpfree( pb->pop->ors_attrs, pb->pop->o_tmpmemctx );
+                       pb->pop->ors_attrs = NULL;
+               }
+               if ( attrs != NULL ) {
+                       for ( i = 0; attrs[i] != NULL; i++ )
+                               ;
+               }
+               if ( i ) {
+                       an = (AttributeName *)pb->pop->o_tmpalloc( (i + 1) *
+                               sizeof(AttributeName), pb->pop->o_tmpmemctx );
+                       for ( i = 0; attrs[i] != NULL; i++ ) {
+                               an[i].an_desc = NULL;
+                               an[i].an_oc = NULL;
+                               an[i].an_oc_exclude = 0;
+                               an[i].an_name.bv_val = attrs[i];
+                               an[i].an_name.bv_len = strlen( attrs[i] );
+                               slap_bv2ad( &an[i].an_name, &an[i].an_desc, &pb->rs.sr_text );
+                       }
+                       an[i].an_name.bv_val = NULL;
+                       an[i].an_name.bv_len = 0;
+               }       
+               pb->pop->ors_attrs = an;
+               break;
        }
-       if ( addcon ) {
-               LDAPControl **ctrls = pb->curVals[i];
-               int j;
+       case SLAPI_SEARCH_ATTRSONLY:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       pb->pop->ors_attrsonly = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_SEARCH_RESULT_ENTRY:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               pb->rs.sr_entry = (Slapi_Entry *)value;
+               break;
+       case SLAPI_BIND_RET_SASLCREDS:
+               pb->rs.sr_sasldata = (struct berval *)value;
+               break;
+       case SLAPI_EXT_OP_REQ_OID:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
 
-               if ( ctrls ) {
-                       for (j=0; ctrls[j]; j++);
-                       ctrls = ch_realloc( ctrls, (j+2)*sizeof(LDAPControl *) );
+               if ( pb->pop->o_tag == LDAP_REQ_EXTENDED ) {
+                       pb->pop->ore_reqoid.bv_val = (char *)value;
+                       pb->pop->ore_reqoid.bv_len = strlen((char *)value);
                } else {
-                       ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
-                       j = 0;
+                       rc = PBLOCK_ERROR;
                }
-               ctrls[j] = val;
-               ctrls[j+1] = NULL;
-               pb->curVals[i] = ctrls;
-       } else {
-               if ( freeit ) ch_free( pb->curVals[i] );
-               pb->curVals[i] = val;
+               break;
+       case SLAPI_EXT_OP_REQ_VALUE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_EXTENDED )
+                       pb->pop->ore_reqdata = (struct berval *)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_EXT_OP_RET_OID:
+               pb->rs.sr_rspoid = (char *)value;
+               break;
+       case SLAPI_EXT_OP_RET_VALUE:
+               pb->rs.sr_rspdata = (struct berval *)value;
+               break;
+       case SLAPI_BIND_METHOD:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_BIND )
+                       pb->pop->orb_method = (int)value;
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_BIND_CREDENTIALS:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_BIND )
+                       pb->pop->orb_cred = *((struct berval *)value);
+               else
+                       rc = PBLOCK_ERROR;
+               break;
+       case SLAPI_COMPARE_TYPE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_COMPARE ) {
+                       const char *text;
+
+                       pb->pop->orc_ava->aa_desc = NULL;
+                       rc = slap_str2ad( (char *)value, &pb->pop->orc_ava->aa_desc, &text );
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_COMPARE_VALUE:
+               PBLOCK_ASSERT_OP( pb, 0 );
+               PBLOCK_VALIDATE_IS_INTOP( pb );
+
+               if ( pb->pop->o_tag == LDAP_REQ_COMPARE ) {
+                       pb->pop->orc_ava->aa_value = *((struct berval *)value);
+               } else {
+                       rc = PBLOCK_ERROR;
+               }
+               break;
+       case SLAPI_REQUESTOR_ISROOT:
+       case SLAPI_REQUESTOR_ISUPDATEDN:
+       case SLAPI_CONN_AUTHTYPE:
+       case SLAPI_CONN_AUTHMETHOD:
+       case SLAPI_X_CONN_IS_UDP:
+       case SLAPI_CONN_CLIENTIP:
+       case SLAPI_X_CONN_CLIENTPATH:
+       case SLAPI_CONN_SERVERIP:
+       case SLAPI_X_CONN_SERVERPATH:
+               rc = PBLOCK_ERROR;
+               break;
+       default:
+               rc = pblock_set_default( pb, param, value );
+               break;
        }
 
-       unLock( pb );   
-       return PBLOCK_SUCCESS;
-#endif /* LDAP_SLAPI */
-       return PBLOCK_ERROR;
-}
+       pblock_unlock( pb );
 
-static void
-clearPB( Slapi_PBlock *pb ) 
-{
-       pb->numParams = 1;
+       return rc;
 }
 
 static void
-checkParams( Slapi_PBlock *pb, int flag ) 
+pblock_clear( Slapi_PBlock *pb ) 
 {
-       pb->ckParams = flag;
+       pb->numParams = 1;
 }
 
 static int
-deleteParam( Slapi_PBlock *p, int param ) 
+pblock_delete_param( Slapi_PBlock *p, int param ) 
 {
        int i;
 
-       Lock(p);
+       pblock_lock(p);
+
        for ( i = 0; i < p->numParams; i++ ) { 
                if ( p->curParams[i] == param ) {
                        break;
        }
     
        if (i >= p->numParams ) {
-               unLock( p );
+               pblock_unlock( p );
                return PBLOCK_ERROR;
        }
        if ( p->numParams > 1 ) {
                p->curVals[i] = p->curVals[p->numParams];
        }
        p->numParams--;
-       unLock( p );    
+
+       pblock_unlock( p );     
+
        return PBLOCK_SUCCESS;
 }
 
 Slapi_PBlock *
-slapi_pblock_new() 
+slapi_pblock_new(void) 
 {
-#if defined(LDAP_SLAPI)
        Slapi_PBlock *pb;
 
-       pb = (Slapi_PBlock *) ch_malloc(sizeof(Slapi_PBlock));
+       pb = (Slapi_PBlock *) ch_calloc( 1, sizeof(Slapi_PBlock) );
        if ( pb != NULL ) {
                pb->ckParams = 1;
                ldap_pvt_thread_mutex_init( &pb->pblockMutex );
                pb->curParams[0] = SLAPI_IBM_PBLOCK;
                pb->curVals[0] = NULL;
                pb->numParams = 1;
+               pb->pconn = NULL;
+               pb->pop = NULL;
+               pb->internal_op = 0;
        }
        return pb;
-#endif /* LDAP_SLAPI */
-       return NULL;
 }
 
 void 
 slapi_pblock_destroy( Slapi_PBlock* pb ) 
 {
-#if defined(LDAP_SLAPI)
-       char *str = NULL;
-       LDAPControl **rescontrols = NULL;
-
-       get( pb, SLAPI_CONN_DN,(void **)&str );
-       if ( str != NULL ) {
-               ch_free( str );
-               str = NULL;
-       }
+       LDAPControl **controls = NULL;
 
-       get( pb, SLAPI_CONN_AUTHMETHOD, (void **)&str );
-       if ( str != NULL ) {
-               ch_free( str );
-               str = NULL;
-       }
+       assert( pb != NULL );
 
-       get( pb, SLAPI_RESULT_TEXT, (void **)&str );
-       if ( str != NULL ) {
-               ch_free( str );
-               str = NULL;
-       }
+       if ( pb->internal_op ) {
+               slapi_int_connection_done_pb( pb );
+       } else {
+               LDAPMod **mods = NULL;
+               char **attrs = NULL;
+
+               pblock_get_default( pb, SLAPI_MODIFY_MODS, (void **)&mods );
+
+               if ( mods != NULL )
+                       slapi_int_free_ldapmods( mods );
 
-       get( pb, SLAPI_RESULT_MATCHED, (void **)&str );
-       if ( str != NULL ) {
-               ch_free( str );
-               str = NULL;
+               pblock_get_default( pb, SLAPI_SEARCH_ATTRS, (void **)&attrs );
+
+               if ( attrs != NULL )
+                       pb->pop->o_tmpfree( attrs, pb->pop->o_tmpmemctx );
        }
 
-       get( pb, SLAPI_RESCONTROLS, (void **)&rescontrols );
-       if ( rescontrols != NULL ) {
-               ldap_controls_free( rescontrols );
-               rescontrols = NULL;
+       pblock_get_default( pb, SLAPI_RESCONTROLS, (void **)&controls );
+       if ( controls != NULL ) {
+               ldap_controls_free( controls );
        }
 
        ldap_pvt_thread_mutex_destroy( &pb->pblockMutex );
 
-       ch_free( pb ); 
-#endif /* LDAP_SLAPI */
+       slapi_ch_free( (void **)&pb ); 
 }
 
 int 
 slapi_pblock_get( Slapi_PBlock *pb, int arg, void *value ) 
 {
-#if defined(LDAP_SLAPI)
-       return get( pb, arg, (void **)value );
-#endif /* LDAP_SLAPI */
-       return PBLOCK_ERROR;
+       return pblock_get( pb, arg, (void **)value );
 }
 
 int 
 slapi_pblock_set( Slapi_PBlock *pb, int arg, void *value ) 
 {
-#if defined(LDAP_SLAPI)
-       void *pTmp = NULL;
-
-       switch ( arg ) {
-        case SLAPI_CONN_DN:
-        case SLAPI_CONN_AUTHMETHOD:
-        case SLAPI_RESULT_TEXT:
-        case SLAPI_RESULT_MATCHED:
-               if ( value != NULL ) {
-                       pTmp = (void *)slapi_ch_strdup((char *)value);
-                       if ( pTmp == NULL ) {
-                               return LDAP_NO_MEMORY;
-                       }
-               }
-               break;
-       default:
-               pTmp = value;
-               break;
-       }
-       return set( pb, arg, pTmp );
-#endif /* LDAP_SLAPI */
-       return LDAP_NO_MEMORY;
+       return pblock_set( pb, arg, value );
 }
 
 void
 slapi_pblock_clear( Slapi_PBlock *pb ) 
 {
-#if defined(LDAP_SLAPI)
-   clearPB( pb );
-#endif /* LDAP_SLAPI */
+       pblock_clear( pb );
 }
 
 int 
 slapi_pblock_delete_param( Slapi_PBlock *p, int param ) 
 {
-#if defined(LDAP_SLAPI)
-       return deleteParam( p, param );
-#endif /* LDAP_SLAPI */
-       return PBLOCK_ERROR;
-}
-
-void
-slapi_pblock_check_params( Slapi_PBlock *pb, int flag ) 
-{
-#if defined(LDAP_SLAPI)
-       checkParams( pb, flag );
-#endif /* LDAP_SLAPI */
+       return pblock_delete_param( p, param );
 }
 
 /*
 int
 slapi_int_pblock_get_first( Backend *be, Slapi_PBlock **pb )
 {
-#if defined(LDAP_SLAPI)
        assert( pb != NULL );
        *pb = SLAPI_BACKEND_PBLOCK( be );
        return (*pb == NULL ? LDAP_OTHER : LDAP_SUCCESS);
-#else /* LDAP_SLAPI */
-       return LDAP_OTHER;
-#endif /* LDAP_SLAPI */
 }
 
 /*
 int
 slapi_int_pblock_get_next( Slapi_PBlock **pb )
 {
-#if defined(LDAP_SLAPI)
        assert( pb != NULL );
        return slapi_pblock_get( *pb, SLAPI_IBM_PBLOCK, pb );
-#else /* LDAP_SLAPI */
-       return LDAP_OTHER;
-#endif /* LDAP_SLAPI */
 }
 
+#endif /* LDAP_SLAPI */
+
 
 
 #include <netdb.h>
 
+#ifdef LDAP_SLAPI
+
 /*
  * server start time (should we use a struct timeval also in slapd?
  */
        ldap_pvt_thread_mutex_t mutex;
 };
 
-#ifdef LDAP_SLAPI
 static int checkBVString(const struct berval *bv)
 {
        int i;
 
        return 1;
 }
-#endif /* LDAP_SLAPI */
 
 /*
  * This function converts an array of pointers to berval objects to
        char            *s, 
        int             flags )
 {
-#ifdef LDAP_SLAPI
        Slapi_Entry     *e = NULL;
        char            *pTmpS;
 
        }
 
        return e;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
        Slapi_Entry     *e, 
        int             *len ) 
 {
-#ifdef LDAP_SLAPI
        char            *ret;
 
        ldap_pvt_thread_mutex_lock( &entry2str_mutex );
        ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
 
        return ret;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_entry_get_dn( Slapi_Entry *e ) 
 {
-#ifdef LDAP_SLAPI
        return e->e_name.bv_val;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_x_entry_get_id( Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
        return e->e_id;
-#else
-       return NOID;
-#endif /* LDAP_SLAPI */
 }
 
 void 
        Slapi_Entry     *e, 
        char            *ldn )
 {
-#ifdef LDAP_SLAPI
        struct berval   dn = BER_BVNULL;
 
        dn.bv_val = ldn;
        dn.bv_len = strlen( ldn );
 
        dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Entry *
 slapi_entry_dup( Slapi_Entry *e ) 
 {
-#ifdef LDAP_SLAPI
        return entry_dup( e );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int 
        Slapi_Entry     *e,             
        char            *type ) 
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
 
        } else {
                return -1;      /* something went wrong */
        }
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Entry *
 slapi_entry_alloc( void ) 
 {
-#ifdef LDAP_SLAPI
        return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_entry_free( Slapi_Entry *e ) 
 {
-#ifdef LDAP_SLAPI
        if ( e != NULL )
                entry_free( e );
-#endif /* LDAP_SLAPI */
 }
 
 int 
        char            *type, 
        struct berval   **vals ) 
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        BerVarray               bv;
        ch_free( bv );
 
        return rc;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
        char            *type, 
        Slapi_Attr      **attr ) 
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        int                     rc;
        }
 
        return 0;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
 
        return NULL;
-#else
-       return -1;
-#endif
 }
 
 int
 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
 
        return slapi_value_get_int( attr->a_vals );
-#else
-       return 0;
-#endif
 }
 
 long
 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
 
        return slapi_value_get_long( attr->a_vals );
-#else
-       return 0;
-#endif
 }
 
 unsigned int
 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
 
        return slapi_value_get_uint( attr->a_vals );
-#else
-       return 0;
-#endif
 }
 
 unsigned long
 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
 
        return slapi_value_get_ulong( attr->a_vals );
-#else
-       return 0;
-#endif
 }
 
 int
 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
 {
-#ifdef LDAP_SLAPI
        struct berval bv;
        AttributeDescription *ad = NULL;
        const char *text;
        bv.bv_len = strlen( value );
 
        return ( slapi_attr_value_find( attr, &bv ) != -1 );
-#else
-       return 0;
-#endif
 }
 
 void
 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        int                     rc;
                bv.bv_len = strlen(value);
                attr_merge_normalize_one( e, ad, &bv, NULL );
        }
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
 {
-#ifdef LDAP_SLAPI
        char buf[64];
 
        snprintf( buf, sizeof( buf ), "%d", l );
        slapi_entry_attr_set_charptr( e, type, buf );
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
 {
-#ifdef LDAP_SLAPI
        char buf[64];
 
        snprintf( buf, sizeof( buf ), "%u", l );
        slapi_entry_attr_set_charptr( e, type, buf );
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
 {
-#ifdef LDAP_SLAPI
        char buf[64];
 
        snprintf( buf, sizeof( buf ), "%ld", l );
        slapi_entry_attr_set_charptr( e, type, buf );
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
 {
-#ifdef LDAP_SLAPI
        char buf[64];
 
        snprintf( buf, sizeof( buf ), "%lu", l );
        slapi_entry_attr_set_charptr( e, type, buf );
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_is_rootdse( const char *dn )
 {
-#ifdef LDAP_SLAPI
        return ( dn == NULL || dn[0] == '\0' );
-#else
-       return 0;
-#endif
 }
 
 int
-slapi_entry_has_children(const Slapi_Entry *e)
+slapi_entry_has_children( const Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
-       Connection *conn;
-       Operation *op;
-       int hasSubordinates = 0;
-       int rc;
        Slapi_PBlock *pb;
+       int hasSubordinates = 0;
 
        pb = slapi_pblock_new();
+       slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
 
        slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );
 
-       rc = slapi_int_connection_init( pb, LDAP_REQ_SEARCH, &conn );
-       if ( rc != LDAP_SUCCESS ) {
-               slapi_pblock_destroy( pb );
-               return 0;
-       }
-
-       op = conn->c_pending_ops.stqh_first;
-       op->o_bd = select_backend( &e->e_nname, 0, 0 );
-       if ( op->o_bd != NULL ) {
-               op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
+       pb->pop->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
+       if ( pb->pop->o_bd != NULL ) {
+               pb->pop->o_bd->be_has_subordinates( pb->pop, (Entry *)e, &hasSubordinates );
        }
 
-       slapi_int_connection_destroy( &conn );
        slapi_pblock_destroy( pb );
 
        return ( hasSubordinates == LDAP_COMPARE_TRUE );
-#else
-       return 0;
-#endif
 }
 
 /*
  */
 size_t slapi_entry_size(Slapi_Entry *e)
 {
-#ifdef LDAP_SLAPI
        size_t size;
        Attribute *a;
        int i;
        size -= (size % 1024);
 
        return size;
-#else
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 /*
 int
 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
 {
-#ifdef LDAP_SLAPI
        Modification            mod;
        const char              *text;
        int                     rc;
 
        rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
-       ch_free( mod.sm_values );
+       slapi_ch_free( (void **)&mod.sm_values );
 
        return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
 {
-#ifdef LDAP_SLAPI
        return slapi_entry_add_values( e, type, vals );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        const char              *text;
        int                     rc;
        }
 
        return attr_merge_normalize( e, ad, *vs, NULL );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
 {
-#ifdef LDAP_SLAPI
        Modification            mod;
        const char              *text;
        int                     rc;
 
        rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
 
-       ch_free( mod.sm_values );
+       slapi_ch_free( (void **)&mod.sm_values );
 
        return rc;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
 {
-#ifdef LDAP_SLAPI
        return slapi_entry_delete_values( e, type, vals );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
 {
-#ifdef LDAP_SLAPI
        return slapi_entry_attr_merge( e, (char *)type, vals );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        AttributeDescription    *ad = NULL;
        int                     rc;
        const char              *text;
        }
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
 {
-#ifdef LDAP_SLAPI
        Slapi_Value val;
 
        val.bv_val = (char *)value;
        val.bv_len = strlen( value );
 
        return slapi_entry_add_value( e, type, &val );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
 {
-#ifdef LDAP_SLAPI
        Slapi_Value *vals[2];
        Slapi_Value val;
 
        vals[1] = NULL;
 
        return slapi_entry_delete_values_sv( e, type, vals );   
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 
 int
 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
 {
-#ifdef LDAP_SLAPI
        return slapi_entry_attr_merge( e, (char *)type, vals );
-#else
-       return -1;
-#endif
 }
 
 int
 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
 {
-#ifdef LDAP_SLAPI
        if ( e == NULL ) {
                return -1;
        }
        *attr = e->e_attrs;
 
        return ( *attr != NULL ) ? 0 : -1;
-#else
-       return -1;
-#endif
 }
 
 int
 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
 {
-#ifdef LDAP_SLAPI
        if ( e == NULL ) {
                return -1;
        }
        *attr = prevattr->a_next;
 
        return ( *attr != NULL ) ? 0 : -1;
-#else
-       return -1;
-#endif
 }
 
 int
 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
        int rc;
        }
        
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /* 
        Slapi_Attr      *attr, 
        struct berval   ***vals ) 
 {
-#ifdef LDAP_SLAPI
        int             i, j;
        struct berval   **bv;
 
        *vals = (struct berval **)bv;
 
        return 0;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_dn_normalize( char *dn ) 
 {
-#ifdef LDAP_SLAPI
        struct berval   bdn;
        struct berval   pdn;
 
        }
 
        return pdn.bv_val;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_dn_normalize_case( char *dn ) 
 {
-#ifdef LDAP_SLAPI
        struct berval   bdn;
        struct berval   ndn;
 
        }
 
        return ndn.bv_val;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int 
        char            *dn, 
        char            *suffix )
 {
-#ifdef LDAP_SLAPI
        struct berval   bdn, ndn;
        struct berval   bsuffix, nsuffix;
        int rc;
        slapi_ch_free( (void **)&nsuffix.bv_val );
 
        return rc;
-#else /* LDAP_SLAPI */
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 int
        const char      *parentdn,
        const char      *childdn )
 {
-#ifdef LDAP_SLAPI
        struct berval   assertedParentDN, normalizedAssertedParentDN;
        struct berval   childDN, normalizedChildDN;
        struct berval   normalizedParentDN;
        slapi_ch_free( (void **)&normalizedChildDN.bv_val );
 
        return ( match == 0 );
-#else
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 /*
 char *
 slapi_dn_parent( const char *_dn )
 {
-#ifdef LDAP_SLAPI
        struct berval   dn, prettyDN;
        struct berval   parentDN;
 
        }
 
        return slapi_ch_strdup( parentDN.bv_val );
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 /*
  */
 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
 {
-#ifdef LDAP_SLAPI
        Backend         *be;
        struct berval   dn, prettyDN;
        struct berval   normalizedDN, parentDN;
        }
 
        return slapi_ch_strdup( parentDN.bv_val );
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_dn_ignore_case( char *dn )
 {       
-#ifdef LDAP_SLAPI
        return slapi_dn_normalize_case( dn );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_ch_malloc( unsigned long size ) 
 {
-#ifdef LDAP_SLAPI
        return ch_malloc( size );       
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_ch_free( void **ptr ) 
 {
-#ifdef LDAP_SLAPI
        if ( ptr == NULL || *ptr == NULL )
                return;
        ch_free( *ptr );
        *ptr = NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_ch_free_string( char **ptr ) 
 {
-#ifdef LDAP_SLAPI
        slapi_ch_free( (void **)ptr );
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_ch_array_free( char **arrayp )
 {
-#ifdef LDAP_SLAPI
        char **p;
 
        if ( arrayp != NULL ) {
                }
                slapi_ch_free( (void **)&arrayp );
        }
-#endif
 }
 
 struct berval *
 slapi_ch_bvdup(const struct berval *v)
 {
-#ifdef LDAP_SLAPI
        struct berval *bv;
 
        bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
        AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
 
        return bv;
-#else
-       return NULL;
-#endif
 }
 
 struct berval **
 slapi_ch_bvecdup(const struct berval **v)
 {
-#ifdef LDAP_SLAPI
        int i;
        struct berval **rv;
 
        rv[i] = NULL;
 
        return rv;
-#else
-       return NULL;
-#endif
 }
 
 char *
        unsigned long nelem, 
        unsigned long size ) 
 {
-#ifdef LDAP_SLAPI
        return ch_calloc( nelem, size );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
        char *block, 
        unsigned long size ) 
 {
-#ifdef LDAP_SLAPI
        return ch_realloc( block, size );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char *
 slapi_ch_strdup( char *s ) 
 {
-#ifdef LDAP_SLAPI
        return ch_strdup( (const char *)s );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 size_t
 slapi_ch_stlen( char *s ) 
 {
-#ifdef LDAP_SLAPI
        return strlen( (const char *)s );
-#else /* LDAP_SLAPI */
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 int 
        struct berval   **val, 
        int             *iscritical ) 
 {
-#ifdef LDAP_SLAPI
        int             i;
        int             rc = 0;
 
 
                rc = 1;
                if ( controls[i]->ldctl_value.bv_len != 0 ) {
-                       /*
-                        * FIXME: according to 6.1 specification,
-                        *    "The val output parameter is set
-                        *    to point into the controls array.
-                        *    A copy of the control value is
-                        *    not made."
-                        */
-#if 0
-                       struct berval   *pTmpBval;
-
-                       pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
-                       if ( pTmpBval == NULL ) {
-                               rc = 0;
-                       } else {
-                               pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
-                               pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
-                               if ( val ) {
-                                       *val = pTmpBval;
-                               } else {
-                                       slapi_ch_free( (void **)&pTmpBval );
-                                       rc = 0;
-                               }
-                       }
-#endif /* 0 */
                        if ( val ) {
                                *val = &controls[i]->ldctl_value;
                        }
        }
 
        return rc;
-#else /* LDAP_SLAPI */
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
-#ifdef LDAP_SLAPI
 static void
 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
        unsigned long *slapi_mask)
 
        return LDAP_SUCCESS;
 }
-#endif /* LDAP_SLAPI */
 
 void 
 slapi_register_supported_control(
        char            *controloid, 
        unsigned long   controlops )
 {
-#ifdef LDAP_SLAPI
        slap_mask_t controlmask;
 
        slapiControlOp2SlapControlMask( controlops, &controlmask );
 
        register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
-#endif /* LDAP_SLAPI */
 }
 
 int 
        char            ***ctrloidsp, 
        unsigned long   **ctrlopsp ) 
 {
-#ifdef LDAP_SLAPI
        int i, rc;
 
        rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
        }
 
        return LDAP_SUCCESS;
-#else /* LDAP_SLAPI */
-       return 1;
-#endif /* LDAP_SLAPI */
 }
 
 LDAPControl *
 slapi_dup_control( LDAPControl *ctrl )
 {
-#ifdef LDAP_SLAPI
        LDAPControl *ret;
 
        ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
        ret->ldctl_iscritical = ctrl->ldctl_iscritical;
 
        return ret;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
 slapi_register_supported_saslmechanism( char *mechanism )
 {
-#ifdef LDAP_SLAPI
        /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
        slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
                        "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
-#endif /* LDAP_SLAPI */
 }
 
 char **
 slapi_get_supported_saslmechanisms( void )
 {
-#ifdef LDAP_SLAPI
        /* FIXME -- can not get the saslmechanism without a connection. */
        slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
                        "can not get the SASL mechanism list "
                        "without a connection\n" );
        return NULL;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 char **
 slapi_get_supported_extended_ops( void )
 {
-#ifdef LDAP_SLAPI
        int             i, j, k;
        char            **ppExtOpOID = NULL;
        int             numExtOps = 0;
        ppExtOpOID[ i + k ] = NULL;
 
        return ppExtOpOID;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
        int             nentries, 
        struct berval   **urls ) 
 {
-#ifdef LDAP_SLAPI
-       Operation       *op;
-       char            *extOID = NULL;
-       SlapReply       rs = { REP_RESULT };
+       SlapReply       *rs;
 
-       slapi_pblock_get( pb, SLAPI_OPERATION, &op );
+       assert( pb->pop != NULL );
 
-       rs.sr_err = err;
-       rs.sr_matched = matched;
-       rs.sr_text = text;
-       rs.sr_ref = NULL;
-       rs.sr_ctrls = NULL;
+       rs = &pb->rs;
 
-       slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
+       rs->sr_err = err;
+       rs->sr_matched = matched;
+       rs->sr_text = text;
+       rs->sr_ref = NULL;
 
        if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
-               slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
-               send_ldap_sasl( op, &rs );
-               return;
-       }
+               send_ldap_sasl( pb->pop, rs );
+       } else if ( rs->sr_rspoid != NULL ) {
+               send_ldap_extended( pb->pop, rs );
+       } else {
+               if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
+                       rs->sr_nentries = nentries;
 
-       slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
-       if ( extOID != NULL ) {
-               rs.sr_rspoid = extOID;
-               slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
-               send_ldap_extended( op, &rs );
-               return;
+               send_ldap_result( pb->pop, rs );
        }
-
-       if ( op->o_tag == LDAP_REQ_SEARCH )
-               rs.sr_nentries = nentries;
-
-       send_ldap_result( op, &rs );
-#endif /* LDAP_SLAPI */
 }
 
 int 
        char            **attrs, 
        int             attrsonly )
 {
-#ifdef LDAP_SLAPI
-       Operation       *pOp;
-       SlapReply       rs = { REP_RESULT };
-       int             i;
-       AttributeName   *an = NULL;
-       const char      *text;
+       SlapReply               rs = { REP_SEARCH };
+       int                     i = 0;
+       AttributeName           *an = NULL;
+       const char              *text;
+       int                     rc;
+
+       assert( pb->pop != NULL );
 
        if ( attrs != NULL ) {
                for ( i = 0; attrs[ i ] != NULL; i++ ) {
                        ; /* empty */
                }
-       } else {
-               i = 0;
        }
 
-       if ( i > 0 ) {
-               an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
+       if ( i ) {
+               an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
                for ( i = 0; attrs[i] != NULL; i++ ) {
-                       an[i].an_name.bv_val = ch_strdup( attrs[i] );
+                       an[i].an_name.bv_val = attrs[i];
                        an[i].an_name.bv_len = strlen( attrs[i] );
                        an[i].an_desc = NULL;
-                       if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
+                       rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
+                       if ( rs.sr_err != LDAP_SUCCESS) {
+                               slapi_ch_free( (void **)&an );
                                return -1;
+                       }
                }
                an[i].an_name.bv_len = 0;
                an[i].an_name.bv_val = NULL;
        rs.sr_v2ref = NULL;
        rs.sr_flags = 0;
 
-       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
-               return LDAP_OTHER;
-       }
+       rc = send_search_entry( pb->pop, &rs );
 
-       return send_search_entry( pOp, &rs );
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
+       slapi_ch_free( (void **)&an );
+
+       return rc;
 }
 
 int 
        struct berval   **v2refs
        )
 {
-#ifdef LDAP_SLAPI
-       Operation       *pOp;
        SlapReply       rs = { REP_SEARCHREF };
        int             rc;
 
                rs.sr_v2ref = NULL;
        }
 
-       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
-               return LDAP_OTHER;
-       }
-
-       rc = send_search_reference( pOp, &rs );
-
-       if ( rs.sr_ref != NULL )
-               slapi_ch_free( (void **)&rs.sr_ref );
+       rc = send_search_reference( pb->pop, &rs );
 
-       if ( rs.sr_v2ref != NULL )
-               slapi_ch_free( (void **)&rs.sr_v2ref );
+       slapi_ch_free( (void **)&rs.sr_ref );
+       slapi_ch_free( (void **)&rs.sr_v2ref );
 
        return rc;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
 slapi_str2filter( char *str ) 
 {
-#ifdef LDAP_SLAPI
        return str2filter( str );
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void 
        Slapi_Filter    *f, 
        int             recurse ) 
 {
-#ifdef LDAP_SLAPI
        filter_free( f );
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
 slapi_filter_dup( Slapi_Filter *filter )
 {
-#ifdef LDAP_SLAPI
        Filter *f;
 
        f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
        }
 
        return f;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int 
 slapi_filter_get_choice( Slapi_Filter *f )
 {
-#ifdef LDAP_SLAPI
        int             rc;
 
        if ( f != NULL ) {
        }
 
        return rc;
-#else /* LDAP_SLAPI */
-       return -1;              /* invalid filter type */
-#endif /* LDAP_SLAPI */
 }
 
 int 
        char            **type, 
        struct berval   **bval )
 {
-#ifdef LDAP_SLAPI
        int             ftype;
        int             rc = LDAP_SUCCESS;
 
        }
 
        return rc;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
 slapi_filter_list_first( Slapi_Filter *f )
 {
-#ifdef LDAP_SLAPI
        int             ftype;
 
        if ( f == NULL ) {
        } else {
                return NULL;
        }
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
        Slapi_Filter    *f, 
        Slapi_Filter    *fprev )
 {
-#ifdef LDAP_SLAPI
        int             ftype;
 
        if ( f == NULL ) {
        }
 
        return NULL;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
 {
-#ifdef LDAP_SLAPI
        if ( f == NULL ) {
                return -1;
        }
        }
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription **adp, *ad = NULL;
        const char *text;
        int rc;
                *adp = ad;
 
        return ( rc == LDAP_SUCCESS ) ? 0 : -1;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
        char ***any, char **final )
 {
-#ifdef LDAP_SLAPI
        int i;
 
        if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
        *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Filter *
 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
 {
-#ifdef LDAP_SLAPI
        Slapi_Filter *f = NULL;
 
        if ( ftype == LDAP_FILTER_AND ||
        }
 
        return f;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int
        Slapi_Filter **pNextFilter,
        Slapi_Filter *filterToAppend )
 {
-#ifdef LDAP_SLAPI
        if ( ftype == LDAP_FILTER_AND ||
             ftype == LDAP_FILTER_OR ||
             ftype == LDAP_FILTER_NOT )
 
                return 0;
        }
-#endif /* LDAP_SLAPI */
        return -1;
 }
 
 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
        int verify_access )
 {
-#ifdef LDAP_SLAPI
        Operation *op;
        int rc;
 
        }
 
        return rc;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int
 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
 {
-#ifdef LDAP_SLAPI
        return slapi_filter_test( NULL, e, f, 0 );
-#else
-       return -1;
-#endif
 }
 
 int
 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
 {
-#ifdef LDAP_SLAPI
        switch ( f->f_choice ) {
        case LDAP_FILTER_AND:
        case LDAP_FILTER_NOT:
        }
 
        return -1;
-#else
-       *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
-       return -1;
-#endif /* LDAP_SLAPI */
-}
-
-int 
-slapi_send_ldap_extended_response(
-       Connection      *conn, 
-       Operation       *op,
-       int             errornum, 
-       char            *respName,
-       struct berval   *response )
-{
-#ifdef LDAP_SLAPI
-       SlapReply       rs;
-
-       rs.sr_err = errornum;
-       rs.sr_matched = NULL;
-       rs.sr_text = NULL;
-       rs.sr_ref = NULL;
-       rs.sr_ctrls = NULL;
-       rs.sr_rspoid = respName;
-       rs.sr_rspdata = response;
-
-       send_ldap_extended( op, &rs );
-
-       return LDAP_SUCCESS;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int 
        struct berval   **vals, 
        struct berval   *v ) 
 {
-#ifdef LDAP_SLAPI
        /*
         * FIXME: what's the point?
         */
        return 1;
-#else /* LDAP_SLAPI */
-       return 1;
-#endif /* LDAP_SLAPI */
 }
 
 #define MAX_HOSTNAME 512
 char *
 slapi_get_hostname( void ) 
 {
-#ifdef LDAP_SLAPI
        char            *hn = NULL;
        static int      been_here = 0;   
        static char     *static_hn = NULL;
        hn = ch_strdup( static_hn );
 
        return hn;
-#else /* LDAP_SLAPI */
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 /*
        char            *fmt, 
        ... ) 
 {
-#ifdef LDAP_SLAPI
        int             rc = LDAP_SUCCESS;
        va_list         arglist;
 
        va_end( arglist );
 
        return rc;
-#else /* LDAP_SLAPI */
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 
 unsigned long
 slapi_timer_current_time( void ) 
 {
-#ifdef LDAP_SLAPI
        static int      first_time = 1;
 #if !defined (_WIN32)
        struct timeval  now;
        QueryPerformanceCounter( &now );
        return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
 #endif /* _WIN32 */
-#else /* LDAP_SLAPI */
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 /*
 unsigned long
 slapi_timer_get_time( char *label ) 
 {
-#ifdef LDAP_SLAPI
        unsigned long start = slapi_timer_current_time();
        printf("%10ld %10d usec %s\n", start, 0, label);
        return start;
-#else /* LDAP_SLAPI */
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 /*
        char *label,
        unsigned long start ) 
 {
-#ifdef LDAP_SLAPI
        unsigned long stop = slapi_timer_current_time();
        printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
-#endif /* LDAP_SLAPI */
 }
 
 void
 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
 {
-#ifdef LDAP_SLAPI
        Slapi_Entry     **entries;
        int             k = 0, nEnt = 0;
 
        slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
        slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
-       if ( nEnt == 0 ) {
-               return;
-       }
-       
-       if ( entries == NULL ) {
+       if ( nEnt == 0 || entries == NULL ) {
                return;
        }
-       
+
        for ( k = 0; k < nEnt; k++ ) {
                slapi_entry_free( entries[k] );
+               entries[k] = NULL;
        }
        
        slapi_ch_free( (void **)&entries );
-#endif /* LDAP_SLAPI */
-}
-
-#ifdef LDAP_SLAPI
-/*
- * Internal API to prime a Slapi_PBlock with a Backend.
- */
-static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
-{
-       slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
-       
-       if ( be != NULL ) {
-               slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
-       }
-
-       return 0;
-}
-
-/*
- * If oldStyle is TRUE, then a value suitable for setting to
- * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
- * (pointer to static storage).
- *
- * If oldStyle is FALSE, then a value suitable for setting to
- * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
- * a pointer to allocated memory and will include the SASL
- * mechanism (if any).
- */
-static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
-{
-       size_t len;
-       char *authType;
-
-       switch ( authz->sai_method ) {
-       case LDAP_AUTH_SASL:
-               if ( oldStyle ) {
-                       authType = SLAPD_AUTH_SASL;
-               } else {
-                       len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
-                       authType = slapi_ch_malloc( len );
-                       snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
-               }
-               break;
-       case LDAP_AUTH_SIMPLE:
-               authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
-               break;
-       case LDAP_AUTH_NONE:
-               authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
-               break;
-       default:
-               authType = NULL;
-               break;
-       }
-       if ( is_tls && authType == NULL ) {
-               authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
-       }
-
-       return authType;
 }
 
-/*
- * Internal API to prime a Slapi_PBlock with a Connection.
- */
-static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
+int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
 {
-       char *connAuthType;
-
-       slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
-
-       if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
-               slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
-       } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
-               slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
-       }
-
-       if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
-               slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
-       } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
-               slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
-       }
-
-#ifdef LDAP_CONNECTIONLESS
-       slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
-#endif
-
-       slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
+       Connection *conn = NULL;
+       int rc;
 
-       /* Returns pointer to static string */
-       connAuthType = Authorization2AuthType( &conn->c_authz,
-#ifdef HAVE_TLS
-               conn->c_is_tls,
-#else
-               0,
-#endif
-               1 );
-       if ( connAuthType != NULL ) {
-               slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
+       rc = slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
+       if ( rc != PBLOCK_SUCCESS || conn == NULL ) {
+               return LDAP_OTHER;
        }
 
-       /* Returns pointer to allocated string */
-       connAuthType = Authorization2AuthType( &conn->c_authz,
-#ifdef HAVE_TLS
-               conn->c_is_tls,
-#else
-               0,
-#endif
-               0 );
-       if ( connAuthType != NULL ) {
-               slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
-               /* slapi_pblock_set dups this itself */
-               slapi_ch_free( (void **)&connAuthType );
-       }
-
-       if ( conn->c_authz.sai_method != LDAP_AUTH_NONE &&
-            conn->c_authz.sai_dn.bv_val != NULL ) {
-               /* slapi_pblock_set dups this itself */
-               slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
-       }
-
-       slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
-
-       slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
-               ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
-                                                conn->c_sasl_sockctx ) );
-
-       return 0;
-}
-#endif /* LDAP_SLAPI */
-
-/*
- * Internal API to prime a Slapi_PBlock with an Operation.
- */
-int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
-{
-#ifdef LDAP_SLAPI
-       int isRoot = 0;
-       int isUpdateDn = 0;
-       char *opAuthType;
-       void *existingOp = NULL;
-       BackendDB *be_orig;
-
-       be_orig = op->o_bd;
-
-       if ( op->o_req_ndn.bv_len ) {
-               op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
-               if ( op->o_bd != NULL ) {
-                       isRoot = be_isroot( op );
-                       isUpdateDn = be_isupdate( op );
-               }
-       }
-
-       /* This should only be called once per operation */
-       slapi_pblock_get( pb, SLAPI_OPERATION, &existingOp );
-       assert( existingOp == NULL );
-
-       slapi_int_pblock_set_backend( pb, op->o_bd );
-       slapi_int_pblock_set_connection( pb, op->o_conn );
-
-       slapi_pblock_set( pb, SLAPI_OPERATION,            (void *)op );
-       slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME,     (void *)op->o_time );
-       slapi_pblock_set( pb, SLAPI_OPERATION_ID,         (void *)op->o_opid );
-       slapi_pblock_set( pb, SLAPI_OPERATION_TYPE,       (void *)op->o_tag );
-       slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT,     (void *)isRoot );
-       slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
-       slapi_pblock_set( pb, SLAPI_REQCONTROLS,          (void *)op->o_ctrls );
-       slapi_pblock_set( pb, SLAPI_REQUESTOR_DN,         (void *)op->o_ndn.bv_val );
-       slapi_pblock_set( pb, SLAPI_MANAGEDSAIT,          (void *)get_manageDSAit( op ) );
-       slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD,      (void *)&opAuthType );
-       if ( opAuthType != NULL )
-               slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
-
-       op->o_bd = be_orig;
-
-       return 0;
-#else
-       return -1;
-#endif
-}
-
-int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
-{
-#ifdef LDAP_SLAPI
-       Connection *conn;
-
-       slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
 #ifdef HAVE_TLS
        *isSSL = conn->c_is_tls;
 #else
 #endif
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /*
 
 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
 {
-#ifdef LDAP_SLAPI
        AttributeType *at;
 
        if ( attr == NULL )
                *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
 {
-#ifdef LDAP_SLAPI
        unsigned long flags;
 
        if ( slapi_attr_get_flags( attr, &flags ) != 0 )
                return 0;
        return (flags & flag) ? 1 : 0;
-#else
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Attr *slapi_attr_new( void )
 {
-#ifdef LDAP_SLAPI
        Attribute *ad;
 
        ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
 
        return ad;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
 {
-#ifdef LDAP_SLAPI
        const char *text;
        AttributeDescription *ad = NULL;
 
        a->a_flags = 0;
 
        return a;
-#else
-       return NULL;
-#endif
 }
 
 void slapi_attr_free( Slapi_Attr **a )
 {
-#ifdef LDAP_SLAPI
        attr_free( *a );
        *a = NULL;
-#endif
 }
 
 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
 {
-#ifdef LDAP_SLAPI
        return attr_dup( (Slapi_Attr *)attr );
-#else
-       return NULL;
-#endif
 }
 
 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
 {
-#ifdef LDAP_SLAPI
        struct berval nval;
        struct berval *nvalp;
        int rc;
        }
 
        return rc;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_type2plugin( const char *type, void **pi )
 
 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
 {
-#ifdef LDAP_SLAPI
        if ( attr == NULL ) {
                return LDAP_PARAM_ERROR;
        }
        *type = attr->a_desc->ad_cname.bv_val;
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
 {
-#ifdef LDAP_SLAPI
        if ( attr == NULL ) {
                return LDAP_PARAM_ERROR;
        }
        *oidp = attr->a_desc->ad_type->sat_oid;
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
 {
-#ifdef LDAP_SLAPI
        MatchingRule *mr;
        int ret;
        int rc;
                return -1;
 
        return ( ret == 0 ) ? 0 : -1;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
 {
-#ifdef LDAP_SLAPI
        MatchingRule *mr;
        struct berval *bv;
        int j;
                        return 0;
                }
        }
-#endif /* LDAP_SLAPI */
        return -1;
 }
 
 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *a1 = NULL;
        AttributeDescription *a2 = NULL;
        const char *text;
        }
 
        return ret;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
 {
-#ifdef LDAP_SLAPI
        return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
 {
-#ifdef LDAP_SLAPI
        return slapi_valueset_first_value( &a->a_vals, v );
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
 {
-#ifdef LDAP_SLAPI
        return slapi_valueset_next_value( &a->a_vals, hint, v );
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
 {
-#ifdef LDAP_SLAPI
        *numValues = slapi_valueset_count( &a->a_vals );
 
        return 0;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
 {
-#ifdef LDAP_SLAPI
        *vs = &((Slapi_Attr *)a)->a_vals;
 
        return 0;
-#else
-       return -1;
-#endif
 }
 
 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
 {
-#ifdef LDAP_SLAPI
        return slapi_attr_get_values( a, vals );
-#else
-       return -1;
-#endif
 }
 
 char *slapi_attr_syntax_normalize( const char *s )
 {
-#ifdef LDAP_SLAPI
        AttributeDescription *ad = NULL;
        const char *text;
 
        }
 
        return ad->ad_cname.bv_val;
-#else
-       return -1;
-#endif
 }
 
 Slapi_Value *slapi_value_new( void )
 {
-#ifdef LDAP_SLAPI
        struct berval *bv;
 
        bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
 
        return bv;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
 {
-#ifdef LDAP_SLAPI
        return ber_dupbv( NULL, (struct berval *)bval );
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
 {
-#ifdef LDAP_SLAPI
        return slapi_value_new_berval( v );
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_new_string(const char *s)
 {
-#ifdef LDAP_SLAPI
        struct berval bv;
 
        bv.bv_val = (char *)s;
        bv.bv_len = strlen( s );
 
        return slapi_value_new_berval( &bv );
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_init(Slapi_Value *val)
 {
-#ifdef LDAP_SLAPI
        val->bv_val = NULL;
        val->bv_len = 0;
 
        return val;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
 {
-#ifdef LDAP_SLAPI
        return ber_dupbv( v, bval );
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
 {
-#ifdef LDAP_SLAPI
        v->bv_val = slapi_ch_strdup( (char *)s );
        v->bv_len = strlen( s );
 
        return v;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
 {
-#ifdef LDAP_SLAPI
        return slapi_value_new_value( v );
-#else
-       return NULL;
-#endif
 }
 
 void slapi_value_free(Slapi_Value **value)
 {
-#ifdef LDAP_SLAPI      
        if ( value == NULL ) {
                return;
        }
                slapi_ch_free( (void **)&(*value)->bv_val );
                slapi_ch_free( (void **)value );
        }
-#endif
 }
 
 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
 {
-#ifdef LDAP_SLAPI
        return value;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) {
                return NULL;
        }
        slapi_value_init_berval( value, (struct berval *)bval );
 
        return value;
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) {
                return NULL;
        }
        return slapi_value_set_berval( value, vfrom );
-#else
-       return NULL;
-#endif
 }
 
 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) {
                return NULL;
        }
        AC_MEMCPY( value->bv_val, val, len );
 
        return value;
-#else
-       return NULL;
-#endif
 }
 
 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) {
                return -1;
        }
        slapi_value_set( value, (void *)strVal, strlen( strVal ) );
        return 0;
-#else
-       return NULL;
-#endif
 }
 
 int slapi_value_set_int(Slapi_Value *value, int intVal)
 {
-#ifdef LDAP_SLAPI
        char buf[64];
 
        snprintf( buf, sizeof( buf ), "%d", intVal );
 
        return slapi_value_set_string( value, buf );
-#else
-       return -1;
-#endif
 }
 
 const char *slapi_value_get_string(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) return NULL;
        if ( value->bv_val == NULL ) return NULL;
        if ( !checkBVString( value ) ) return NULL;
 
        return value->bv_val;
-#else
-       return NULL;
-#endif
 }
 
 int slapi_value_get_int(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) return 0;
        if ( value->bv_val == NULL ) return 0;
        if ( !checkBVString( value ) ) return 0;
 
        return (int)strtol( value->bv_val, NULL, 10 );
-#else
-       return NULL;
-#endif
 }
 
 unsigned int slapi_value_get_uint(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) return 0;
        if ( value->bv_val == NULL ) return 0;
        if ( !checkBVString( value ) ) return 0;
 
        return (unsigned int)strtoul( value->bv_val, NULL, 10 );
-#else
-       return NULL;
-#endif
 }
 
 long slapi_value_get_long(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) return 0;
        if ( value->bv_val == NULL ) return 0;
        if ( !checkBVString( value ) ) return 0;
 
        return strtol( value->bv_val, NULL, 10 );
-#else
-       return NULL;
-#endif
 }
 
 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL ) return 0;
        if ( value->bv_val == NULL ) return 0;
        if ( !checkBVString( value ) ) return 0;
 
        return strtoul( value->bv_val, NULL, 10 );
-#else
-       return NULL;
-#endif
 }
 
 size_t slapi_value_get_length(const Slapi_Value *value)
 {
-#ifdef LDAP_SLAPI
        if ( value == NULL )
                return 0;
 
        return (size_t) value->bv_len;
-#else
-       return 0;
-#endif
 }
 
 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
 {
-#ifdef LDAP_SLAPI
        return slapi_attr_value_cmp( a, v1, v2 );
-#else
-       return -1;
-#endif
 }
 
 /* A ValueSet is a container for a BerVarray. */
 Slapi_ValueSet *slapi_valueset_new( void )
 {
-#ifdef LDAP_SLAPI
        Slapi_ValueSet *vs;
 
        vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
        *vs = NULL;
 
        return vs;
-#else
-       return NULL;
-#endif
 }
 
 void slapi_valueset_free(Slapi_ValueSet *vs)
 {
-#ifdef LDAP_SLAPI
        if ( vs != NULL ) {
                BerVarray vp = *vs;
 
 
                *vs = NULL;
        }
-#endif
 }
 
 void slapi_valueset_init(Slapi_ValueSet *vs)
 {
-#ifdef LDAP_SLAPI
        if ( vs != NULL && *vs == NULL ) {
                *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
                (*vs)->bv_val = NULL;
                (*vs)->bv_len = 0;
        }
-#endif
 }
 
 void slapi_valueset_done(Slapi_ValueSet *vs)
 {
-#ifdef LDAP_SLAPI
        BerVarray vp;
 
        if ( vs == NULL )
                slapi_ch_free( (void **)&vp->bv_val );
        }
        /* but don't free *vs or vs */
-#endif
 }
 
 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
 {
-#ifdef LDAP_SLAPI
        struct berval bv;
 
        ber_dupbv( &bv, (Slapi_Value *)addval );
        ber_bvarray_add( vs, &bv );
-#endif
 }
 
 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
 {
-#ifdef LDAP_SLAPI
        return slapi_valueset_next_value( vs, 0, v );
-#else
-       return -1;
-#endif
 }
 
 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
 {
-#ifdef LDAP_SLAPI
        int i;
        BerVarray vp;
 
                        return index + 1;
                }
        }
-#endif
 
        return -1;
 }
 
 int slapi_valueset_count( const Slapi_ValueSet *vs )
 {
-#ifdef LDAP_SLAPI
        int i;
        BerVarray vp;
 
                ;
 
        return i;
-#else
-       return 0;
-#endif
 
 }
 
 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
 {
-#ifdef LDAP_SLAPI
        BerVarray vp;
 
        for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
                slapi_valueset_add_value( vs1, vp );
        }
-#endif
 }
 
 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
        struct berval *val, int access )
 {
-#ifdef LDAP_SLAPI
-       Backend *be;
-       Connection *conn;
-       Operation *op;
        int rc;
        slap_access_t slap_access;
        AttributeDescription *ad = NULL;
                break;
        }
 
-       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
-               return LDAP_PARAM_ERROR;
-       }
-
-       if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
-               return LDAP_PARAM_ERROR;
-       }
+       assert( pb->pop != NULL );
 
-       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
-               return LDAP_PARAM_ERROR;
-       }
-
-       if ( access_allowed( op, e, ad, val, slap_access, NULL ) ) {
+       if ( access_allowed( pb->pop, e, ad, val, slap_access, NULL ) ) {
                return LDAP_SUCCESS;
        }
 
        return LDAP_INSUFFICIENT_ACCESS;
-#else
-       return LDAP_UNWILLING_TO_PERFORM;
-#endif
 }
 
 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
 {
-#ifdef LDAP_SLAPI
        Operation *op;
        int rc = LDAP_SUCCESS;
-       Modifications *ml, *mp;
+       Modifications *ml;
 
        if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
                return LDAP_PARAM_ERROR;
        }
 
-       ml = slapi_int_ldapmods2modifications( mods );
+       ml = slapi_int_ldapmods2modifications( mods, NULL );
        if ( ml == NULL ) {
                return LDAP_OTHER;
        }
        }
 
        /* Careful when freeing the modlist because it has pointers into the mods array. */
-       for ( ; ml != NULL; ml = mp ) {
-               mp = ml->sml_next;
-
-               /* just free the containing array */
-               slapi_ch_free( (void **)&ml->sml_values );
-               slapi_ch_free( (void **)&ml );
-       }
+       slapi_int_mods_free( ml );
 
        return rc;
-#else
-       return LDAP_UNWILLING_TO_PERFORM;
-#endif
 }
 
 /*
  * 
  * This function must also be called before slap_mods_check().
  */
-LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
+LDAPMod **slapi_int_modifications2ldapmods(
+       Modifications **pmodlist,
+       void *memctx
+)
 {
-#ifdef LDAP_SLAPI
        Modifications *ml, *modlist;
        LDAPMod **mods, *modp;
        int i, j;
        for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
                ;
 
-       mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
+       mods = (LDAPMod **)slap_sl_malloc( (i + 1) * sizeof(LDAPMod *), memctx );
 
        for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
-               mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
+               mods[i] = (LDAPMod *)slap_sl_malloc( sizeof(LDAPMod), memctx );
                modp = mods[i];
                modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
 
                if ( ml->sml_values != NULL ) {
                        for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
                                ;
-                       modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
-                               sizeof(struct berval *) );
+                       modp->mod_bvalues = (struct berval **)slap_sl_malloc( (j + 1) *
+                               sizeof(struct berval *), memctx );
                        for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
                                /* Take ownership of original values. */
-                               modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
+                               modp->mod_bvalues[j] = (struct berval *)slap_sl_malloc(
+                                               sizeof(struct berval), memctx );
                                modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
                                modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
                                ml->sml_values[j].bv_len = 0;
        *pmodlist = NULL;
 
        return mods;
-#else
-       return NULL;
-#endif
 }
 
 /*
  * LDAPMods array; the latter MUST be freed with
  * slapi_int_free_ldapmods() (see below).
  */
-Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods )
+Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods, void *memctx )
 {
-#ifdef LDAP_SLAPI
        Modifications *modlist = NULL, **modtail;
        LDAPMod **modp;
 
                if ( slap_str2ad( (*modp)->mod_type, &ad, &text ) != LDAP_SUCCESS )
                        continue;
 
-               mod = (Modifications *) ch_malloc( sizeof(Modifications) );
+               mod = (Modifications *) slap_sl_malloc( sizeof(Modifications), memctx );
                mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
                mod->sml_flags = 0;
                mod->sml_type.bv_val = (*modp)->mod_type;
                if ( i == 0 ) {
                        mod->sml_values = NULL;
                } else {
-                       mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
+                       mod->sml_values = (BerVarray) slap_sl_malloc( (i + 1) * sizeof(struct berval), memctx );
 
                        /* NB: This implicitly trusts a plugin to return valid modifications. */
                        if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
        }
        
        return modlist;
-#else
-       return NULL;
-#endif 
+}
+
+void
+slapi_int_mods_free( Modifications *ml )
+{
+       Modifications           *next;
+
+       for ( ; ml != NULL; ml = next ) {
+               next = ml->sml_next;
+
+               /* Don't free unnormalized values */
+               if ( ml->sml_nvalues != NULL ) {
+                       ber_bvarray_free( ml->sml_nvalues );
+                       ml->sml_nvalues = NULL;
+               }
+               slapi_ch_free((void **)&ml->sml_values);
+               slapi_ch_free((void **)&ml);
+       }
 }
 
 /*
  * by slapi_int_ldapmods2modifications(). 
  *
  */
-void slapi_int_free_ldapmods (LDAPMod **mods)
+void
+slapi_int_free_ldapmods ( LDAPMod **mods )
 {
-#ifdef LDAP_SLAPI
        int i, j;
 
        if (mods == NULL)
                 * Modification list. Do free the containing array.
                 */
                if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
-                       for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
-                               ch_free( mods[i]->mod_values[j] );
+                       if ( mods[i]->mod_bvalues != NULL ) {
+                               for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ )
+                                       slapi_ch_free( (void **)&mods[i]->mod_bvalues[j] );
+                               slapi_ch_free( (void **)&mods[i]->mod_bvalues );
                        }
-                       ch_free( mods[i]->mod_values );
                } else {
-                       ch_free( mods[i]->mod_values );
+                       slapi_ch_free( (void **)&mods[i]->mod_values );
                }
                /* Don't free type, for same reasons. */
-               ch_free( mods[i] );
+               slapi_ch_free( (void **)&mods[i] );
        }
-       ch_free( mods );
-#endif /* LDAP_SLAPI */
+       slapi_ch_free( (void **)&mods );
 }
 
 /*
  */
 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pPlugin = NULL;
        int rc;
 
        }
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /*
  */
 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
 {
-#ifdef LDAP_SLAPI
        Slapi_PBlock *pPlugin = NULL;
        int rc;
 
        }
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /*
  */
 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
 {
-#ifdef LDAP_SLAPI
        int rc = 0;
        slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
 
        slapi_ch_free( (void **)&tmpPlugin );
 
        return rc;
-#else
-       return 1;
-#endif /* LDAP_SLAPI */
 }
 
 int compute_rewrite_search_filter(Slapi_PBlock *pb)
 {
-#ifdef LDAP_SLAPI
        Backend *be;
        int rc;
 
        }
 
        return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 /*
  */
 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
 {
-#ifdef LDAP_SLAPI
        if ( c == NULL )
                return -1;
 
        *pb = c->cac_pb;
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 Slapi_Mutex *slapi_new_mutex( void )
 {
-#ifdef LDAP_SLAPI
        Slapi_Mutex *m;
 
        m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
        }
 
        return m;
-#else
-       return NULL;
-#endif
 }
 
 void slapi_destroy_mutex( Slapi_Mutex *mutex )
 {
-#ifdef LDAP_SLAPI
        if ( mutex != NULL ) {
                ldap_pvt_thread_mutex_destroy( &mutex->mutex );
                slapi_ch_free( (void **)&mutex);
        }
-#endif
 }
 
 void slapi_lock_mutex( Slapi_Mutex *mutex )
 {
-#ifdef LDAP_SLAPI
        ldap_pvt_thread_mutex_lock( &mutex->mutex );
-#endif
 }
 
 int slapi_unlock_mutex( Slapi_Mutex *mutex )
 {
-#ifdef LDAP_SLAPI
        return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
-#else
-       return -1;
-#endif
 }
 
 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
 {
-#ifdef LDAP_SLAPI
        Slapi_CondVar *cv;
 
        if ( mutex == NULL ) {
                return NULL;
        }
 
-       /* XXX struct copy */
        cv->mutex = mutex->mutex;
 
        return cv;
-#else  
-       return NULL;
-#endif
 }
 
 void slapi_destroy_condvar( Slapi_CondVar *cvar )
 {
-#ifdef LDAP_SLAPI
        if ( cvar != NULL ) {
                ldap_pvt_thread_cond_destroy( &cvar->cond );
                slapi_ch_free( (void **)&cvar );
        }
-#endif
 }
 
 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
 {
-#ifdef LDAP_SLAPI
        if ( cvar == NULL ) {
                return -1;
        }
 
        return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
-#else
-       return -1;
-#endif
 }
 
 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
 {
-#ifdef LDAP_SLAPI
        if ( cvar == NULL ) {
                return -1;
        }
        }
 
        return ldap_pvt_thread_cond_signal( &cvar->cond );
-#else
-       return -1;
-#endif
 }
 
 int slapi_int_access_allowed( Operation *op,
        slap_access_t access,
        AccessControlState *state )
 {
-#ifdef LDAP_SLAPI
        int rc, slap_access = 0;
        slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
        Slapi_PBlock *pb;
        slapi_ch_free( (void **)&tmpPlugin );
 
        return rc;
-#else
-       return 1;
-#endif /* LDAP_SLAPI */
 }
 
 /*
  */
 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
 {
-#ifdef LDAP_SLAPI
        LDAPRDN lrdn;
        LDAPAVA *ava;
        int rc;
        ldap_rdnfree(lrdn);
 
        return 0;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
 {
-#ifdef LDAP_SLAPI
        struct berval new_dn, parent_dn, newrdn;
 
        new_dn.bv_val = NULL;
        build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
 
        return new_dn.bv_val;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
        Backend *be;
        const char *text;
        char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
        size_t textlen = sizeof textbuf;
-       int rc;
-
-       if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
-               return -1;
+       int rc = LDAP_SUCCESS;
 
-       rc = entry_schema_check( be, e, NULL, 0,
-               &text, textbuf, textlen );
+       be = select_backend( &e->e_nname, 0, 0 );
+       if ( be != NULL )
+               rc = entry_schema_check( be, e, NULL, 0,
+                       &text, textbuf, textlen );
 
        return ( rc == LDAP_SUCCESS ) ? 0 : 1;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
        LDAPDN dn;
        int rc;
        int i = 0, match = 0;
        ldap_dnfree( dn );
 
        return ( i == match );
-#else
-       return 0;
-#endif /* LDAP_SLAPI */
 }
 
 int slapi_entry_add_rdn_values( Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
        LDAPDN dn;
        int i, rc;
 
        ldap_dnfree( dn );
 
        return LDAP_SUCCESS;
-#else
-       return LDAP_OTHER;
-#endif /* LDAP_SLAPI */
 }
 
 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
 {
-#ifdef LDAP_SLAPI
        Attribute *attr;
 
        attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
        if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
                return slapi_value_get_string( &attr->a_vals[0] );
        }
-#endif /* LDAP_SLAPI */
 
        return NULL;
 }
 
 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
 {
-#ifdef LDAP_SLAPI
        struct berval bv;
 
        attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
        bv.bv_val = uniqueid;
        bv.bv_len = strlen( uniqueid );
        attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
-#endif /* LDAP_SLAPI */
 }
 
 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
 {
-#ifdef LDAP_SLAPI
        LDAP *ld;
        char *url;
        size_t size;
        slapi_ch_free_string( &url );
 
        return ( rc == LDAP_SUCCESS ) ? ld : NULL;
-#else
-       return NULL;
-#endif /* LDAP_SLAPI */
 }
 
 void slapi_ldap_unbind( LDAP *ld )
 {
-#ifdef LDAP_SLAPI
        ldap_unbind( ld );
-#endif /* LDAP_SLAPI */
 }
 
 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
 {
-#ifdef LDAP_SLAPI
        if ( be == NULL )
                return LDAP_PARAM_ERROR;
 
        *flags = SLAP_DBFLAGS(be);
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
+int
+slapi_int_count_controls( LDAPControl **ctrls )
+{
+       size_t i;
+
+       if ( ctrls == NULL )
+               return 0;
+
+       for ( i = 0; ctrls[i] != NULL; i++ )
+               ;
+
+       return i;
+}
+
+#endif /* LDAP_SLAPI */
+