]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_ops.c
Normalize to IBM's OPL notice (with permission).
[openldap] / servers / slapd / slapi / slapi_ops.c
index 650122460834ea7df5742fd79a9f655ebcd0a927..199fc8daf3142d5824d26a017e24edc70d8dc956 100644 (file)
@@ -3,51 +3,66 @@
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /*
- * (C) Copyright IBM Corp. 1997,2002
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is
- * given to IBM Corporation. This software is provided ``as is''
- * without express or implied warranty.
+ * Portions Copyright IBM Corp. 1997,2002-2003
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License, version 2.7 or later.
+ */
+/*
+ * Portions (C) Copyright PADL Software Pty Ltd. 2003
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that this notice is preserved
+ * and that due credit is given to PADL Software Pty Ltd. This software
+ * is provided ``as is'' without express or implied warranty.
  */
 
 #include "portable.h"
+
+#include <ac/string.h>
+#include <ac/stdarg.h>
+#include <ac/ctype.h>
+#include <ac/unistd.h>
+
 #include <slap.h>
+#include <lber_pvt.h>
 #include <slapi.h>
-#include <lber.h>
-#include "../../../libraries/liblber/lber-int.h"
 
-
-int bvptr2obj( struct berval **bvptr, struct berval **bvobj );
+/*
+ * 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")
+};
 
 static void
 internal_result_v3(
-       Connection      *conn, 
        Operation       *op, 
-       ber_int_t       err,
-       const char      *matched, 
-       const char      *text, 
-       BerVarray       referrals,
-       LDAPControl     **ctrls )
+       SlapReply       *rs )
 {
+#ifdef notdef
+       /* XXX needs review after internal API change */
+       /* rs->sr_nentries appears to always be 0 */
+       if (op->o_tag == LDAP_REQ_SEARCH)
+               slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
+                       SLAPI_NENTRIES, (void *)rs->sr_nentries );
+#endif
+
        return;
 }
 
 static int
 internal_search_entry(
-       Backend         *be, 
-       Connection      *conn, 
-       Operation       *op, 
-       Entry           *e, 
-       AttributeName   *attrs, 
-       int             attrsonly, 
-       LDAPControl     **ctrls ) 
+       Operation       *op,
+       SlapReply       *rs )
 {
-       char *ent2str = NULL;
        int nentries = 0, len = 0, i = 0;
        Slapi_Entry **head = NULL, **tp;
-       
-       ent2str = slapi_entry2str( e, &len );
-       if ( ent2str == NULL ) {
+       Slapi_Entry *entry;
+
+       entry = slapi_entry_dup( rs->sr_entry );
+       if ( entry == NULL ) {
                return 1;
        }
 
@@ -60,24 +75,19 @@ internal_search_entry(
        if ( nentries == 0 ) {
                tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
                if ( tp == NULL ) {
+                       slapi_entry_free( entry );
                        return 1;
                }
 
-               tp[ 0 ] = (Slapi_Entry *)str2entry( ent2str );
-               if ( tp[ 0 ] == NULL ) { 
-                       return 1;
-               }
-
+               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;
                }
-               tp[ i - 1 ] = (Slapi_Entry *)str2entry( ent2str );
-               if ( tp[ i - 1 ] == NULL ) { 
-                       return 1;
-               }
+               tp[ i - 1 ] = entry;
        }
        tp[ i ] = NULL;
                  
@@ -89,53 +99,24 @@ internal_search_entry(
        return LDAP_SUCCESS;
 }
 
-static void
-internal_search_result(
-       Connection      *conn, 
-       Operation       *op,
-       ber_int_t       err, 
-       const char      *matched, 
-       const char      *text, 
-       BerVarray       refs,
-       LDAPControl     **ctrls,
-       int             nentries ) 
-{
-       slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
-                       SLAPI_NENTRIES, (void *)nentries );
-
-       return;
-}
-
 static void
 internal_result_ext(
-       Connection      *conn, 
-       Operation       *op, 
-       ber_int_t       errnum, 
-       const char      *matched,
-       const char      *text,
-       BerVarray       refs,
-       const char      *rspoid,
-       struct berval   *rspdata,
-       LDAPControl     **ctrls )
+       Operation       *op,    
+       SlapReply       *sr )
 {
        return;
 }
 
 static int
 internal_search_reference(
-       Backend         *be,
-       Connection      *conn, 
-       Operation       *op, 
-       Entry           *e,
-       BerVarray       refs,
-       LDAPControl     **ctrls,
-       BerVarray       *v2refs )
+       Operation       *op,    
+       SlapReply       *sr )
 {
        return LDAP_SUCCESS;
 }
 
 static Connection *
-fakeConnection(
+slapiConnectionInit(
        char *DN, 
        int OpType ) 
 { 
@@ -173,7 +154,10 @@ fakeConnection(
        c->c_pending_ops.stqh_first->o_authmech.bv_len = 0; 
        c->c_pending_ops.stqh_first->o_time = slap_get_time();
        c->c_pending_ops.stqh_first->o_do_not_cache = 1;
-       c->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context( &connection_pool );
+       c->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context();
+       c->c_pending_ops.stqh_first->o_tmpmemctx = NULL;
+       c->c_pending_ops.stqh_first->o_tmpmfuncs = &ch_mfuncs;
+       c->c_pending_ops.stqh_first->o_conn = c;
 
        /* connection object */
        c->c_authmech.bv_val = NULL;
@@ -182,19 +166,17 @@ fakeConnection(
        c->c_dn.bv_len = 0;
        c->c_ndn.bv_val = NULL;
        c->c_ndn.bv_len = 0;
-       c->c_groups = NULL;
 
-       c->c_listener = NULL;
-       c->c_peer_domain.bv_val = NULL;
-       c->c_peer_domain.bv_len = 0;
-       c->c_peer_name.bv_val = NULL;
-       c->c_peer_name.bv_len = 0;
+       c->c_listener = &slap_unknown_listener;
+       ber_dupbv( &c->c_peer_domain, (struct berval *)&slap_unknown_bv );
+       ber_dupbv( &c->c_peer_name, (struct berval *)&slap_unknown_bv );
 
        LDAP_STAILQ_INIT( &c->c_ops );
 
        c->c_sasl_bind_mech.bv_val = NULL;
        c->c_sasl_bind_mech.bv_len = 0;
-       c->c_sasl_context = NULL;
+       c->c_sasl_authctx = NULL;
+       c->c_sasl_sockctx = NULL;
        c->c_sasl_extra = NULL;
 
        c->c_sb = ber_sockbuf_alloc( );
@@ -233,24 +215,49 @@ fakeConnection(
 
        pConn->c_send_ldap_result = internal_result_v3;
        pConn->c_send_search_entry = internal_search_entry;
-       pConn->c_send_search_result = internal_search_result;
        pConn->c_send_ldap_extended = internal_result_ext;
        pConn->c_send_search_reference = internal_search_reference;
 
        return pConn;
 }
 
+static void slapiConnectionDestroy( Connection **pConn )
+{
+       Connection *conn = *pConn;
+       Operation *op;
+
+       if ( pConn == NULL ) {
+               return;
+       }
+
+       op = (Operation *)conn->c_pending_ops.stqh_first;
+
+       if ( op->o_req_dn.bv_val != NULL ) {
+               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
+       }
+       if ( op->o_req_ndn.bv_val != NULL ) {
+               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
+       }
+
+       if ( conn->c_sb != NULL ) {
+               ber_sockbuf_free( conn->c_sb );
+       }
+       if ( op != NULL ) {
+               slapi_ch_free( (void **)&op );
+       }
+       slapi_ch_free( (void **)pConn );
+}
+
 /*
  * Function : values2obj
  * Convert an array of strings into a BerVarray.
  * the strings.
  */
 static int
-values2obj(
+values2obj_copy(
        char **ppValue,
-       BerVarray *bvobj)
+       BerVarray *bvobj )
 {
-       int rc = LDAP_SUCCESS;
        int i;
        BerVarray tmpberval;
 
@@ -267,8 +274,11 @@ values2obj(
                return LDAP_NO_MEMORY;
        }
        for ( i = 0; ppValue[i] != NULL; i++ ) {
-               tmpberval[i].bv_val = ppValue[i];
-               tmpberval[i].bv_len = strlen( ppValue[i] );
+               size_t len = strlen( ppValue[i] );
+
+               tmpberval[i].bv_val = slapi_ch_malloc( len + 1 );
+               AC_MEMCPY( tmpberval[i].bv_val, ppValue[i], len + 1 );
+               tmpberval[i].bv_len = len;
        }
        tmpberval[i].bv_val = NULL;
        tmpberval[i].bv_len = 0;
@@ -278,22 +288,41 @@ values2obj(
        return LDAP_SUCCESS;
 }
 
-static void
-freeMods( Modifications *ml )
+static int
+bvptr2obj_copy(
+       struct berval   **bvptr, 
+       BerVarray       *bvobj )
 {
-       /*
-        * Free a modification list whose values have been 
-        * set with bvptr2obj() or values2obj() (ie. they
-        * do not own the pointer to the underlying values)
-        */
-       Modifications *next;
+       int             rc = LDAP_SUCCESS;
+       int             i;
+       BerVarray       tmpberval;
+
+       if ( bvptr == NULL || *bvptr == NULL ) {
+               return LDAP_OTHER;
+       }
+
+       for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
+               ; /* EMPTY */
+       }
+
+       tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
+       if ( tmpberval == NULL ) {
+               return LDAP_NO_MEMORY;
+       } 
 
-       for ( ; ml != NULL; ml = next ) {
-               next = ml->sml_next;
+       for ( i = 0; bvptr[i] != NULL; i++ ) {
+               tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
+               tmpberval[i].bv_len = bvptr[i]->bv_len;
+               AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
+       }
+       tmpberval[i].bv_val = NULL;
+       tmpberval[i].bv_len = 0;
 
-               slapi_ch_free( (void **)&ml->sml_bvalues );
-               slapi_ch_free( (void **)&ml );
+       if ( rc == LDAP_SUCCESS ) {
+               *bvobj = tmpberval;
        }
+
+       return rc;
 }
 
 /*
@@ -313,7 +342,6 @@ LDAPModToEntry(
        Entry                   *pEntry=NULL;
        LDAPMod                 *pMod;
        struct berval           *bv;
-       Backend                 *be;
        Operation               *op;
 
        Modifications           *modlist = NULL;
@@ -327,7 +355,7 @@ LDAPModToEntry(
 
 
        op = (Operation *) slapi_ch_calloc(1, sizeof(Operation));
-       if ( pEntry == NULL) {
+       if ( op == NULL) {
                rc = LDAP_NO_MEMORY;
                goto cleanup;
        }  
@@ -342,20 +370,30 @@ LDAPModToEntry(
        dn.bv_val = slapi_ch_strdup(ldn);
        dn.bv_len = strlen(ldn);
 
-       rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname );
-       if (rc != LDAP_SUCCESS) goto cleanup;
+       rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
+       if ( rc != LDAP_SUCCESS ) {
+               goto cleanup;
+       }
 
        if ( rc == LDAP_SUCCESS ) {
-               for ( i=0, pMod=mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod=mods[++i]) {
+               for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod = mods[++i]) {
                        Modifications *mod;
+
                        if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
-                               /* attr values are in berval format */
-                               /* convert an array of pointers to bervals to an array of bervals */
-                               rc = bvptr2obj(pMod->mod_bvalues, &bv);
-                               if (rc != LDAP_SUCCESS) goto cleanup;
+                               /*
+                                * Convert an array of pointers to bervals to
+                                * an array of bervals. Note that we need to copy the
+                                * values too, as the slap_mods_check() will free the
+                                * original values after prettying; the modifications
+                                * being passed in may not have been allocated on the
+                                * heap.
+                                */
+                               rc = bvptr2obj_copy( pMod->mod_bvalues, &bv );
+                               if ( rc != LDAP_SUCCESS ) goto cleanup;
                                tmp.sml_type.bv_val = pMod->mod_type;
                                tmp.sml_type.bv_len = strlen( pMod->mod_type );
                                tmp.sml_bvalues = bv;
+                               tmp.sml_nvalues = NULL;
                
                                mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
@@ -364,6 +402,7 @@ LDAPModToEntry(
                                mod->sml_desc = NULL;
                                mod->sml_type = tmp.sml_type;
                                mod->sml_bvalues = tmp.sml_bvalues;
+                               mod->sml_nvalues = tmp.sml_nvalues;
 
                                *modtail = mod;
                                modtail = &mod->sml_next;
@@ -374,11 +413,12 @@ LDAPModToEntry(
                                if ( pMod->mod_values == NULL ) {
                                        rc = LDAP_OTHER;
                                } else {
-                                       rc = values2obj( pMod->mod_values, &bv );
-                                       if (rc != LDAP_SUCCESS) goto cleanup;
+                                       rc = values2obj_copy( pMod->mod_values, &bv );
+                                       if ( rc != LDAP_SUCCESS ) goto cleanup;
                                        tmp.sml_type.bv_val = pMod->mod_type;
                                        tmp.sml_type.bv_len = strlen( pMod->mod_type );
                                        tmp.sml_bvalues = bv;
+                                       tmp.sml_nvalues = NULL;
                
                                        mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
@@ -387,6 +427,7 @@ LDAPModToEntry(
                                        mod->sml_desc = NULL;
                                        mod->sml_type = tmp.sml_type;
                                        mod->sml_bvalues = tmp.sml_bvalues;
+                                       mod->sml_nvalues = tmp.sml_nvalues;
 
                                        *modtail = mod;
                                        modtail = &mod->sml_next;
@@ -395,27 +436,24 @@ LDAPModToEntry(
                } /* for each LDAPMod */
        }
 
-       be = select_backend(&pEntry->e_nname, 0, 0);
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
-               goto cleanup;
-       }
-
-       if ( be ) {
-               int repl_user = be_isupdate(be, &be->be_rootdn );
-               if ( !be->be_update_ndn.bv_len || repl_user ) {
-                       int update = be->be_update_ndn.bv_len;
+       op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
+       if ( op->o_bd == NULL ) {
+               rc = LDAP_PARTIAL_RESULTS;
+       } else {
+               int repl_user = be_isupdate( op->o_bd, &op->o_bd->be_rootdn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       int update = op->o_bd->be_update_ndn.bv_len;
                        char textbuf[SLAP_TEXT_BUFLEN];
                        size_t textlen = sizeof textbuf;
 
                        rc = slap_mods_check( modlist, update, &text, 
-                                       textbuf, textlen );
+                                       textbuf, textlen, NULL );
                        if ( rc != LDAP_SUCCESS) {
                                goto cleanup;
                        }
 
                        if ( !repl_user ) {
-                               rc = slap_mods_opattrs( be, op,
+                               rc = slap_mods_opattrs( op,
                                                modlist, modtail, &text, 
                                                textbuf, textlen );
                                if ( rc != LDAP_SUCCESS) {
@@ -423,12 +461,8 @@ LDAPModToEntry(
                                }
                        }
 
-                       /*
-                        * FIXME: slap_mods2entry is declared static 
-                        * in servers/slapd/add.c
-                        */
                        rc = slap_mods2entry( modlist, &pEntry, repl_user,
-                                       &text, textbuf, textlen );
+                                             0, &text, textbuf, textlen );
                        if (rc != LDAP_SUCCESS) {
                                goto cleanup;
                        }
@@ -436,15 +470,16 @@ LDAPModToEntry(
                } else {
                        rc = LDAP_REFERRAL;
                }
-       } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
 
-       if ( dn.bv_val ) slapi_ch_free( (void **)&dn.bv_val );
-       if ( op ) slapi_ch_free( (void **)&op );
-       if ( modlist != NULL ) freeMods( modlist );
+       if ( dn.bv_val )
+               slapi_ch_free( (void **)&dn.bv_val );
+       if ( op )
+               slapi_ch_free( (void **)&op );
+       if ( modlist != NULL )
+               slap_mods_free( modlist );
        if ( rc != LDAP_SUCCESS ) {
                if ( pEntry != NULL ) {
                        slapi_entry_free( pEntry );
@@ -471,29 +506,25 @@ slapi_delete_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#if defined(LDAP_SLAPI)
-       Backend                 *be;
+#ifdef LDAP_SLAPI
        Connection              *pConn = NULL;
        Operation               *op = NULL;
        Slapi_PBlock            *pPB = NULL;
        Slapi_PBlock            *pSavePB = NULL;
+       SlapReply               rs = { REP_RESULT };
+       struct berval           dn = { 0, NULL };
 
-       struct berval dn  = { 0, NULL };
-       struct berval pdn = { 0, NULL };
-       struct berval ndn = { 0, NULL };
-
-       int                             rc=LDAP_SUCCESS;
-       int                             manageDsaIt = 0;
-       int                             isCritical;
+       int                     manageDsaIt = 0;
+       int                     isCritical;
 
        if ( ldn == NULL ) {
-               rc = LDAP_PARAM_ERROR; 
+               rs.sr_err = LDAP_PARAM_ERROR; 
                goto cleanup;
        }
 
-       pConn = fakeConnection( NULL, LDAP_REQ_DELETE );
+       pConn = slapiConnectionInit( NULL, LDAP_REQ_DELETE );
        if (pConn == NULL) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -502,89 +533,84 @@ slapi_delete_internal(
        op->o_ctrls = controls;
 
        dn.bv_val = slapi_ch_strdup(ldn);
-       dn.bv_len = slapi_strlen(ldn);
-       rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
-       if ( rc != LDAP_SUCCESS ) goto cleanup;
+       dn.bv_len = strlen(ldn);
+       rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS )
+               goto cleanup;
 
        if ( slapi_control_present( controls, 
                        SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical) ) {
                manageDsaIt = 1; 
        }
 
-       be = select_backend( &ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
+       if ( op->o_bd == NULL ) {
+               rs.sr_err = LDAP_PARTIAL_RESULTS;
                goto cleanup;
        }
 
-       op->o_dn = pConn->c_dn = be->be_rootdn;
-       op->o_ndn = pConn->c_ndn = be->be_rootndn;
-
-       suffix_alias( be, &ndn );
+       op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
+       op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
 
-       if ( be->be_delete ) {
-               int repl_user = be_isupdate( be, &op->o_ndn );
-               if ( !be->be_update_ndn.bv_len || repl_user ) {
-                       rc = (*be->be_delete)( be, pConn, op, &pdn, &ndn );
-                       if ( rc == 0 ) {
-                               if (log_change) {
-                                       replog( be, op, &pdn, &ndn, NULL );
+       if ( op->o_bd->be_delete ) {
+               int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       if ( (*op->o_bd->be_delete)( op, &rs ) == 0 ) {
+                               if ( log_change ) {
+                                       replog( op );
                                }
-                               rc = LDAP_SUCCESS;
                        } else {
-                               rc = LDAP_OPERATIONS_ERROR;
+                               rs.sr_err = LDAP_OTHER;
                        }
                } else {
-                       rc = LDAP_REFERRAL;
+                       rs.sr_err = LDAP_REFERRAL;
                }
        } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
-       if (pPB != NULL) 
-               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
-
-       if (dn.bv_val) slapi_ch_free( (void **)&dn.bv_val );
-       if (pdn.bv_val) slapi_ch_free( (void **)&pdn.bv_val );
-       if (ndn.bv_val) slapi_ch_free( (void **)&ndn.bv_val );
-
+       if ( pPB != NULL ) {
+               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
+       }
+       if ( dn.bv_val ) {
+               slapi_ch_free( (void **)&dn.bv_val );
+       }
        if ( pConn != NULL ) {
-               if ( pConn->c_sb != NULL ) ber_sockbuf_free( pConn->c_sb );
-               if ( op ) slapi_ch_free( (void **)&op );
                pSavePB = pPB;
-               free( pConn );
        }
-       
+
+       slapiConnectionDestroy( &pConn );
+
        return (pSavePB);
-#endif /* LDAP_SLAPI */
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
-Slapi_PBlock * 
-slapi_add_entry_internal(
-       Slapi_Entry *e, 
+#ifdef LDAP_SLAPI
+static Slapi_PBlock * 
+slapi_add_entry_internal_locked(
+       Slapi_Entry **e, 
        LDAPControl **controls, 
        int log_changes ) 
 {
-#if defined(LDAP_SLAPI)
        Connection              *pConn = NULL;
        Operation               *op = NULL;
        Slapi_PBlock            *pPB = NULL, *pSavePB = NULL;
-       Backend                 *be;
 
        int                     manageDsaIt = 0;
        int                     isCritical;
-       int                     rc = LDAP_SUCCESS;
+       SlapReply               rs = { REP_RESULT };
 
-       if ( e == NULL ) {
-               rc = LDAP_PARAM_ERROR;
+       if ( *e == NULL ) {
+               rs.sr_err = LDAP_PARAM_ERROR;
                goto cleanup;
        }
        
-       pConn = fakeConnection( NULL, LDAP_REQ_ADD );
+       pConn = slapiConnectionInit( NULL, LDAP_REQ_ADD );
        if ( pConn == NULL ) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -597,50 +623,74 @@ slapi_add_entry_internal(
        pPB = (Slapi_PBlock *)op->o_pb;
        op->o_ctrls = controls;
 
-       be = select_backend( &e->e_nname, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc = LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &((*e)->e_nname), manageDsaIt, 0 );
+       if ( op->o_bd == NULL ) {
+               rs.sr_err = LDAP_PARTIAL_RESULTS;
                goto cleanup;
        }
 
-       op->o_dn = pConn->c_dn = be->be_rootdn;
-       op->o_ndn = pConn->c_ndn = be->be_rootndn;
+       op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
+       op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
+       op->oq_add.rs_e = *e;
 
-       if ( be->be_add ) {
-               int repl_user = be_isupdate( be, &op->o_ndn );
-               if ( !be->be_update_ndn.bv_len || repl_user ){
-                       if ( (*be->be_add)( be, pConn, op, e ) == 0 ) {
+       if ( op->o_bd->be_add ) {
+               int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
                                if ( log_changes ) {
-                                       replog( be, op, &e->e_name, 
-                                                       &e->e_nname, e );
+                                       replog( op );
                                }
-                               rc = LDAP_SUCCESS;
+                               be_entry_release_w( op, *e );
+                               *e = NULL;
                        }
                } else {
-                       rc = LDAP_REFERRAL;
+                       rs.sr_err = LDAP_REFERRAL;
                }
        } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
 
        if ( pPB != NULL ) {
-               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
+               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
        }
 
        if ( pConn != NULL ) {
-               if ( pConn->c_sb != NULL ) ber_sockbuf_free( pConn->c_sb );
-               if ( op != NULL ) slapi_ch_free( (void **)&op );
                pSavePB = pPB;
-               free( pConn );
        }
+
+       slapiConnectionDestroy( &pConn );
+
        return( pSavePB );
+}
 #endif /* LDAP_SLAPI */
+
+Slapi_PBlock * 
+slapi_add_entry_internal(
+       Slapi_Entry *e, 
+       LDAPControl **controls, 
+       int log_changes ) 
+{
+#ifdef LDAP_SLAPI
+       Slapi_PBlock *pb;
+       Slapi_Entry *entry;
+
+       /*
+        * We make a copy to avoid an entry that may be freed later
+        * by the caller being placed in the cache.
+        */
+       entry = slapi_entry_dup( e );
+       pb = slapi_add_entry_internal_locked( &entry, controls, log_changes );
+       if ( entry != NULL ) {
+               slapi_entry_free( entry );
+       }
+       return pb;
+#else
        return NULL;
+#endif
 }
 
-
 Slapi_PBlock *
 slapi_add_internal(
        char *dn, 
@@ -648,11 +698,11 @@ slapi_add_internal(
        LDAPControl **controls, 
        int log_changes  ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        LDAPMod                 *pMod = NULL;
        Slapi_PBlock            *pb = NULL;
        Entry                   *pEntry = NULL;
-       int                     i, rc=LDAP_SUCCESS;
+       int                     i, rc = LDAP_SUCCESS;
 
        if ( mods == NULL || *mods == NULL || dn == NULL || *dn == '\0' ) {
                rc = LDAP_PARAM_ERROR ;
@@ -668,7 +718,8 @@ slapi_add_internal(
        }
 
        if ( rc == LDAP_SUCCESS ) {
-               if((pEntry = LDAPModToEntry( dn, mods )) == NULL) {
+               pEntry = LDAPModToEntry( dn, mods );
+               if ( pEntry == NULL ) {
                        rc = LDAP_OTHER;
                }
        }
@@ -677,16 +728,17 @@ slapi_add_internal(
                pb = slapi_pblock_new();
                slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
        } else {
-               pb = slapi_add_entry_internalpEntry, controls, log_changes );
+               pb = slapi_add_entry_internal_locked( &pEntry, controls, log_changes );
        }
 
-       if ( pEntry ) {
+       if ( pEntry != NULL ) {
                slapi_entry_free(pEntry);
        }
 
        return(pb);
-#endif /* LDAP_SLAPI */
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 /* Function : slapi_modrdn_internal
@@ -695,7 +747,6 @@ slapi_add_internal(
  *                              of an entry in the backend directly
  * Return values : LDAP_SUCCESS
  *                 LDAP_PARAM_ERROR
- *                 LDAP_OPERATIONS_ERROR
  *                 LDAP_NO_MEMORY
  *                 LDAP_OTHER
  *                 LDAP_UNWILLING_TO_PERFORM
@@ -710,143 +761,113 @@ slapi_modrdn_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#if defined(LDAP_SLAPI)
-       int                     rc = LDAP_SUCCESS;
-
+#ifdef LDAP_SLAPI
        struct berval           dn = { 0, NULL };
-       struct berval           pdn = { 0, NULL };
-       struct berval           ndn = { 0, NULL };
-
        struct berval           newrdn = { 0, NULL };
-       struct berval           pnewrdn = { 0, NULL };
-       struct berval           nnewrdn = { 0, NULL };
-
-#if 0 /* currently unused */
-       struct berval           newSuperior = { 0, NULL };
-#endif
-       struct berval           pnewSuperior = { 0, NULL }; 
-#if 0 /* currently unused */
-       struct berval           nnewSuperior = { 0, NULL }; 
-#endif
-
-       struct berval           *pnewS = NULL;
-       struct berval           *nnewS = NULL;
-
        Connection              *pConn = NULL;
        Operation               *op = NULL;
        Slapi_PBlock            *pPB = NULL;
        Slapi_PBlock            *pSavePB = NULL;
-
-       Backend                 *be;
-#if 0 /* currently unused */
-       Backend                 *newSuperior_be = NULL;
-#endif
-
        int                     manageDsaIt = 0;
        int                     isCritical;
-#if 0 /* currently unused */
-       const char              *text = NULL;
-#endif
+       SlapReply               rs = { REP_RESULT };
 
-       dn.bv_val = slapi_ch_strdup(olddn);
-       dn.bv_len = slapi_ch_stlen(olddn);
+       pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODRDN);
+       if ( pConn == NULL) {
+               rs.sr_err = LDAP_NO_MEMORY;
+               goto cleanup;
+       }
 
-       rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
+       op = (Operation *)pConn->c_pending_ops.stqh_first;
+       pPB = (Slapi_PBlock *)op->o_pb;
+       op->o_ctrls = controls;
 
-       if ( rc != LDAP_SUCCESS ) goto cleanup;
+       if ( slapi_control_present( controls, 
+                       SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
+               manageDsaIt = 1;
+       }
 
-       if ( ndn.bv_len == 0 ) {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
+       if ( op->o_bd == NULL ) {
+               rs.sr_err =  LDAP_PARTIAL_RESULTS;
                goto cleanup;
        }
 
-       newrdn.bv_val = slapi_ch_strdup( lnewrdn );
-       newrdn.bv_len = slapi_ch_stlen( lnewrdn );
-
-       rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
+       op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
+       op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
 
-       if ( rc != LDAP_SUCCESS ) goto cleanup;
+       dn.bv_val = slapi_ch_strdup( olddn );
+       dn.bv_len = strlen( olddn );
 
-       if ( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) goto cleanup;
+       rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
+               goto cleanup;
+       }
 
-       pConn = fakeConnection( NULL,  LDAP_REQ_MODRDN);
-       if ( pConn == NULL) {
-               rc = LDAP_NO_MEMORY;
+       if ( op->o_req_dn.bv_len == 0 ) {
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
-       op = (Operation *)pConn->c_pending_ops.stqh_first;
-       pPB = (Slapi_PBlock *)op->o_pb;
-       op->o_ctrls = controls;
+       newrdn.bv_val = slapi_ch_strdup( lnewrdn );
+       newrdn.bv_len = strlen( lnewrdn );
 
-       if ( slapi_control_present( controls, 
-                       SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
-               manageDsaIt = 1;
+       rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
+               goto cleanup;
        }
 
-       be = select_backend( &ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       if ( rdnValidate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
                goto cleanup;
        }
 
-       op->o_dn = pConn->c_dn = be->be_rootdn;
-       op->o_ndn = pConn->c_ndn = be->be_rootndn;
+       op->oq_modrdn.rs_newSup = NULL;
+       op->oq_modrdn.rs_nnewSup = NULL;
+       op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
 
-       suffix_alias( be, &ndn );
-
-       if ( be->be_modrdn ) {
-               int repl_user = be_isupdate( be, &op->o_ndn );
-               if ( !be->be_update_ndn.bv_len || repl_user ) {
-                       rc = (*be->be_modrdn)( be, pConn, op, &pdn, &ndn,
-                                       &pnewrdn, &nnewrdn, deloldrdn, pnewS,
-                                       nnewS );
-                       if ( rc == 0 ) {
-                               struct slap_replog_moddn moddn;
-                               moddn.newrdn = &pnewrdn;
-                               moddn.deloldrdn = deloldrdn;
-                               moddn.newsup = &pnewSuperior;
+       if ( op->o_bd->be_modrdn ) {
+               int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       if ( (*op->o_bd->be_modrdn)( op, &rs ) == 0 ) {
                                if ( log_change ) {
-                                       replog( be, op, &pdn, &ndn, &moddn );
+                                       replog( op );
                                }
-                               rc = LDAP_SUCCESS;
-
                        } else {
-                               rc = LDAP_OPERATIONS_ERROR;
+                               rs.sr_err = LDAP_OTHER;
                        }
-
                } else {
-                       rc = LDAP_REFERRAL;
+                       rs.sr_err = LDAP_REFERRAL;
                }
-
        } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
 
        if ( pPB != NULL ) {
-               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
+               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
        }
        
-       if ( dn.bv_val ) ch_free( dn.bv_val );
-       if ( pdn.bv_val ) ch_free( pdn.bv_val );
-       if ( ndn.bv_val ) ch_free( ndn.bv_val );
+       if ( dn.bv_val )
+               slapi_ch_free( (void **)&dn.bv_val );
 
-       if ( newrdn.bv_val ) ch_free( newrdn.bv_val );
-       if ( pnewrdn.bv_val ) ch_free( newrdn.bv_val );
-       if ( nnewrdn.bv_val ) ch_free( newrdn.bv_val );
+       if ( newrdn.bv_val )
+               slapi_ch_free( (void **)&newrdn.bv_val );
+       if ( op->oq_modrdn.rs_newrdn.bv_val )
+               slapi_ch_free( (void **)&op->oq_modrdn.rs_newrdn.bv_val );
+       if ( op->oq_modrdn.rs_nnewrdn.bv_val )
+               slapi_ch_free( (void **)&op->oq_modrdn.rs_nnewrdn.bv_val );
 
        if ( pConn != NULL ) {
-               if ( pConn->c_sb != NULL ) ber_sockbuf_free( pConn->c_sb );
-               if ( op != NULL ) slapi_ch_free( (void **)&op );
                pSavePB = pPB;
-               free( pConn );
        }
 
+       slapiConnectionDestroy( &pConn );
+
        return( pSavePB );
-#endif /* LDAP_SLAPI */
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 /* Function : slapi_modify_internal
@@ -856,7 +877,6 @@ cleanup:
  * Return values : LDAP_SUCCESS
  *                 LDAP_PARAM_ERROR
  *                 LDAP_NO_MEMORY
- *                 LDAP_OPERATIONS_ERROR
  *                 LDAP_OTHER
  *                 LDAP_UNWILLING_TO_PERFORM
 */
@@ -867,20 +887,17 @@ slapi_modify_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#if defined(LDAP_SLAPI)
-       int                     i, rc = LDAP_SUCCESS;
+#ifdef LDAP_SLAPI
+       int                     i;
        Connection              *pConn = NULL;
        Operation               *op = NULL;
        Slapi_PBlock            *pPB = NULL;
        Slapi_PBlock            *pSavePB = NULL;
 
        struct berval dn = { 0, NULL };
-       struct berval pdn = { 0, NULL };
-       struct berval ndn = { 0, NULL };
 
        int                     manageDsaIt = 0;
        int                     isCritical;
-       Backend                 *be;
        struct berval           *bv;
        LDAPMod                 *pMod;
 
@@ -888,14 +905,16 @@ slapi_modify_internal(
        Modifications           **modtail = &modlist;
        Modifications           tmp;
 
+       SlapReply               rs = { REP_RESULT };
+
        if ( mods == NULL || *mods == NULL || ldn == NULL ) {
-               rc = LDAP_PARAM_ERROR ;
+               rs.sr_err = LDAP_PARAM_ERROR ;
                goto cleanup;
        }
 
-       pConn = fakeConnection( NULL,  LDAP_REQ_MODIFY );
+       pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODIFY );
        if ( pConn == NULL ) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -904,40 +923,45 @@ slapi_modify_internal(
        op->o_ctrls = controls;
 
        dn.bv_val = slapi_ch_strdup( ldn );
-       dn.bv_len = slapi_strlen( ldn );
-       rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
-       if ( rc != LDAP_SUCCESS ) goto cleanup;
+       dn.bv_len = strlen( ldn );
+       rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
+               goto cleanup;
+       }
 
        if ( slapi_control_present( controls, 
                        SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
                manageDsaIt = 1;
        }
 
-       be = select_backend( &ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
+       if ( op->o_bd == NULL ) {
+               rs.sr_err = LDAP_PARTIAL_RESULTS;
                goto cleanup;
        }
 
-       op->o_dn = pConn->c_dn = be->be_rootdn;
-       op->o_ndn = pConn->c_ndn = be->be_rootndn;
-
-       suffix_alias( be, &ndn );
+       op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
+       op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
 
-       for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; 
-                       pMod = mods[++i] ) {
+       for ( i = 0, pMod = mods[0];
+               rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
+               pMod = mods[++i] )
+       {
                Modifications *mod;
+
                if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
                        /*
                         * attr values are in berval format
                         * convert an array of pointers to bervals
                         * to an array of bervals
                         */
-                       rc = bvptr2obj( pMod->mod_bvalues, &bv );
-                       if ( rc != LDAP_SUCCESS ) goto cleanup;
+                       rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
+                       if ( rs.sr_err != LDAP_SUCCESS )
+                               goto cleanup;
                        tmp.sml_type.bv_val = pMod->mod_type;
                        tmp.sml_type.bv_len = strlen( pMod->mod_type );
                        tmp.sml_bvalues = bv;
+                       tmp.sml_nvalues = NULL;
 
                        mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
 
@@ -946,12 +970,15 @@ slapi_modify_internal(
                        mod->sml_desc = NULL;
                        mod->sml_type = tmp.sml_type;
                        mod->sml_bvalues = tmp.sml_bvalues;
+                       mod->sml_nvalues = tmp.sml_nvalues;
                } else { 
-                       rc = values2obj( pMod->mod_values, &bv );
-                       if ( rc != LDAP_SUCCESS ) goto cleanup;
+                       rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
+                       if ( rs.sr_err != LDAP_SUCCESS )
+                               goto cleanup;
                        tmp.sml_type.bv_val = pMod->mod_type;
                        tmp.sml_type.bv_len = strlen( pMod->mod_type );
                        tmp.sml_bvalues = bv;
+                       tmp.sml_nvalues = NULL;
 
                        mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
@@ -960,6 +987,7 @@ slapi_modify_internal(
                        mod->sml_desc = NULL;
                        mod->sml_type = tmp.sml_type;
                        mod->sml_bvalues = tmp.sml_bvalues;
+                       mod->sml_nvalues = tmp.sml_nvalues;
                }
                *modtail = mod;
                modtail = &mod->sml_next;
@@ -967,7 +995,7 @@ slapi_modify_internal(
                switch( pMod->mod_op ) {
                case LDAP_MOD_ADD:
                if ( mod->sml_bvalues == NULL ) {
-                       rc = LDAP_PROTOCOL_ERROR;
+                       rs.sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
 
@@ -977,78 +1005,76 @@ slapi_modify_internal(
                break;
 
                default:
-                       rc = LDAP_PROTOCOL_ERROR;
+                       rs.sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
        } 
        *modtail = NULL;
 
-       if ( ndn.bv_len == 0 ) {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+       if ( op->o_req_ndn.bv_len == 0 ) {
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
-       if ( be->be_modify ) {
-               int repl_user = be_isupdate( be, &op->o_ndn );
-               if ( !be->be_update_ndn.bv_len || repl_user ) {
-                       int update = be->be_update_ndn.bv_len;
+       op->oq_modify.rs_modlist = modlist;
+
+       if ( op->o_bd->be_modify ) {
+               int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       int update = op->o_bd->be_update_ndn.bv_len;
                        const char *text = NULL;
                        char textbuf[SLAP_TEXT_BUFLEN];
                        size_t textlen = sizeof( textbuf );
 
-                       rc = slap_mods_check( modlist, update,
-                                       &text, textbuf, textlen );
-                       if (rc != LDAP_SUCCESS) {
+                       rs.sr_err = slap_mods_check( modlist, update,
+                                       &text, textbuf, textlen, NULL );
+                       if ( rs.sr_err != LDAP_SUCCESS ) {
                                goto cleanup;
                        }
 
                        if ( !repl_user ) {
-                               rc = slap_mods_opattrs( be, op, modlist,
+                               rs.sr_err = slap_mods_opattrs( op, modlist,
                                                modtail, &text, textbuf, 
                                                textlen );
-                               if (rc != LDAP_SUCCESS) {
+                               if ( rs.sr_err != LDAP_SUCCESS ) {
                                        goto cleanup;
                                }
                        }
-                       rc = (*be->be_modify)( be, pConn, op,
-                                       &pdn, &ndn, modlist );
-                       if ( rc == 0 ) {
+                       if ( (*op->o_bd->be_modify)( op, &rs ) == 0 ) {
                                if ( log_change ) {
-                                       replog( be, op, &pdn, &ndn, modlist );
+                                       replog( op );
                                }
-                               rc = LDAP_SUCCESS;
                        } else {
-                               rc = LDAP_OPERATIONS_ERROR;
+                               rs.sr_err = LDAP_OTHER;
                        }
                } else {
-                       rc = LDAP_REFERRAL;
+                       rs.sr_err = LDAP_REFERRAL;
                }
        } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
 
        if ( pPB != NULL ) 
-               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
+               slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
 
-       if ( dn.bv_val ) ch_free( dn.bv_val );
-       if ( pdn.bv_val ) ch_free( pdn.bv_val );
-       if ( ndn.bv_val ) ch_free( ndn.bv_val );
+       if ( dn.bv_val )
+               slapi_ch_free( (void **)&dn.bv_val );
 
-       if ( modlist != NULL ) freeMods( modlist );
+       if ( modlist != NULL )
+               slap_mods_free( modlist );
 
        if ( pConn != NULL ) {
-               if ( pConn->c_sb != NULL ) ber_sockbuf_free( pConn->c_sb );
-               if ( op != NULL ) slapi_ch_free( (void **)&op );
                pSavePB = pPB;
-               free( pConn );
        }
 
-       return ( pSavePB );
+       slapiConnectionDestroy( &pConn );
 
-#endif /* LDAP_SLAPI */
+       return ( pSavePB );
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_PBlock *
@@ -1061,31 +1087,26 @@ slapi_search_internal_bind(
        char **attrs, 
        int attrsonly ) 
 {      
-#if defined(LDAP_SLAPI)
-       Backend                 *be;
+#ifdef LDAP_SLAPI
        Connection              *c;
        Operation               *op = NULL;
        Slapi_PBlock            *ptr = NULL;            
        Slapi_PBlock            *pSavePB = NULL;                
        struct berval           dn = { 0, NULL };
-       struct berval           pdn = { 0, NULL };
-       struct berval           ndn = { 0, NULL };
        Filter                  *filter=NULL;
        struct berval           fstr = { 0, NULL };
        AttributeName           *an = NULL;
        const char              *text = NULL;
 
-       int                     deref=0;
-       int                     sizelimit=-1, timelimit=-1;
-
        int                     manageDsaIt = 0; 
        int                     isCritical;
+       int                     i;
 
-       int                     i, rc = LDAP_SUCCESS;
-       
-       c = fakeConnection( NULL, LDAP_REQ_SEARCH );
-       if (c == NULL) {
-               rc = LDAP_NO_MEMORY;
+       SlapReply               rs = { REP_RESULT };
+
+       c = slapiConnectionInit( NULL, LDAP_REQ_SEARCH );
+       if ( c == NULL ) {
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -1093,22 +1114,26 @@ slapi_search_internal_bind(
        ptr = (Slapi_PBlock *)op->o_pb;
        op->o_ctrls = controls;
 
-       dn.bv_val = slapi_ch_strdup(ldn);
-       dn.bv_len = slapi_strlen(ldn);
+       if ( ldn != NULL ) {
+               dn.bv_val = slapi_ch_strdup(ldn);
+               dn.bv_len = strlen(ldn);
+       }
 
-       rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
-       if (rc != LDAP_SUCCESS) goto cleanup;
+       rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
+               goto cleanup;
+       }
 
        if ( scope != LDAP_SCOPE_BASE && 
                        scope != LDAP_SCOPE_ONELEVEL && 
                        scope != LDAP_SCOPE_SUBTREE ) {
-               rc = LDAP_PROTOCOL_ERROR;
+               rs.sr_err = LDAP_PROTOCOL_ERROR;
                goto cleanup;
        }
 
        filter = slapi_str2filter(filStr);
        if ( filter == NULL ) {
-               rc = LDAP_PROTOCOL_ERROR;
+               rs.sr_err = LDAP_PROTOCOL_ERROR;
                goto cleanup;
        }
 
@@ -1124,47 +1149,55 @@ slapi_search_internal_bind(
                        an[i].an_desc = NULL;
                        an[i].an_oc = NULL;
                        an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
-                       an[i].an_name.bv_len = slapi_strlen(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;
        }
 
+       memset( &rs, 0, sizeof(rs) );
+       rs.sr_type = REP_RESULT;
+       rs.sr_err = LDAP_SUCCESS;
+       rs.sr_entry = NULL; /* paranoia */
+
        if ( scope == LDAP_SCOPE_BASE ) {
-               Entry *entry = NULL;
+               rs.sr_entry = NULL;
 
-               if ( ndn.bv_len == 0 ) {
-                       rc = root_dse_info( c, &entry, &text );
+               if ( op->o_req_ndn.bv_len == 0 ) {
+                       rs.sr_err = root_dse_info( c, &rs.sr_entry, &rs.sr_text );
                }
 
-               if( rc != LDAP_SUCCESS ) {
-                       send_ldap_result( c, op, rc, NULL, text, NULL, NULL );
+               if( rs.sr_err != LDAP_SUCCESS ) {
+                       send_ldap_result( op, &rs );
                        goto cleanup;
-               } else if ( entry != NULL ) {
-                       rc = test_filter( NULL, c, op, entry, filter );
+               } else if ( rs.sr_entry != NULL ) {
+                       rs.sr_err = test_filter( op, rs.sr_entry, filter );
+
+                       if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
+                               rs.sr_type = REP_SEARCH;
+                               rs.sr_err = LDAP_SUCCESS;
+                               rs.sr_attrs = an;
 
-                       if( rc == LDAP_COMPARE_TRUE ) {
-                               send_search_entry( NULL, c, op, entry,
-                                               an, attrsonly, NULL );
+                               send_search_entry( op, &rs );
                        }
 
-                       entry_free( entry );
+                       entry_free( rs.sr_entry );
 
-                       send_ldap_result( c, op, LDAP_SUCCESS, 
-                                       NULL, NULL, NULL, NULL );
+                       rs.sr_type = REP_RESULT;
+                       rs.sr_err = LDAP_SUCCESS;
 
-                       rc = LDAP_SUCCESS;
+                       send_ldap_result( op, &rs );
 
                        goto cleanup;
                }
        }
 
-       if ( !ndn.bv_len && default_search_nbase.bv_len ) {
-               ch_free( pdn.bv_val );
-               ch_free( ndn.bv_val );
+       if ( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
+               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
+               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
 
-               ber_dupbv( &pdn, &default_search_base );
-               ber_dupbv( &ndn, &default_search_nbase );
+               ber_dupbv( &op->o_req_dn, &default_search_base );
+               ber_dupbv( &op->o_req_ndn, &default_search_nbase );
        }
 
        if ( slapi_control_present( controls,
@@ -1172,55 +1205,60 @@ slapi_search_internal_bind(
                manageDsaIt = 1;
        }
 
-       be = select_backend( &ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
+       if ( op->o_bd == NULL ) {
                if ( manageDsaIt == 1 ) {
-                       rc = LDAP_NO_SUCH_OBJECT;
+                       rs.sr_err = LDAP_NO_SUCH_OBJECT;
                } else {
-                       rc = LDAP_PARTIAL_RESULTS;
+                       rs.sr_err = LDAP_PARTIAL_RESULTS;
                }
                goto cleanup;
        } 
 
-       op->o_dn = c->c_dn = be->be_rootdn;
-       op->o_ndn = c->c_ndn = be->be_rootndn;
-
-       if ( be->be_search ) {
-               rc = (*be->be_search)( be, c, op, &pdn, &ndn,
-                       scope, deref, sizelimit, timelimit,
-                       filter, &fstr, an, attrsonly );
-               if ( rc == 0 ) {
-                       rc = LDAP_SUCCESS;
-               } else {
-                       rc = LDAP_OPERATIONS_ERROR;
+       op->o_dn = c->c_dn = op->o_bd->be_rootdn;
+       op->o_ndn = c->c_ndn = op->o_bd->be_rootndn;
+
+       op->oq_search.rs_scope = scope;
+       op->oq_search.rs_deref = 0;
+       op->oq_search.rs_slimit = LDAP_NO_LIMIT;
+       op->oq_search.rs_tlimit = LDAP_NO_LIMIT;
+       op->oq_search.rs_attrsonly = attrsonly;
+       op->oq_search.rs_attrs = an;
+       op->oq_search.rs_filter = filter;
+       op->oq_search.rs_filterstr = fstr;
+
+       if ( op->o_bd->be_search ) {
+               if ( (*op->o_bd->be_search)( op, &rs ) != 0 ) {
+                       rs.sr_err = LDAP_OTHER;
                }
        } else {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
        }
 
 cleanup:
 
        if ( ptr != NULL )
-               slapi_pblock_set( ptr, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
-
-       if ( dn.bv_val ) free( dn.bv_val );
-       if ( ndn.bv_val ) free( ndn.bv_val );
-       if ( pdn.bv_val ) free( pdn.bv_val );
+               slapi_pblock_set( ptr, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
 
-       if ( filter ) slapi_filter_free( filter, 1 );
-       if ( fstr.bv_val ) free ( fstr.bv_val );
-
-       if ( an != NULL ) free( an );
+       if ( dn.bv_val )
+               slapi_ch_free( (void **)&dn.bv_val );
+       if ( filter )
+               slapi_filter_free( filter, 1 );
+       if ( fstr.bv_val )
+               slapi_ch_free( (void **)&fstr.bv_val );
+       if ( an != NULL )
+               slapi_ch_free( (void **)&an );
 
        if ( c != NULL ) {
-               if ( c->c_sb != NULL ) ber_sockbuf_free( c->c_sb );
-               if ( op != NULL ) slapi_ch_free( (void **)&op );
                pSavePB = ptr;
-               free( c );
        }
+
+       slapiConnectionDestroy( &c );
+
        return( pSavePB );
-#endif /* LDAP_SLAPI */
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_PBlock * 
@@ -1232,7 +1270,7 @@ slapi_search_internal(
        char **attrs,
        int attrsonly ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        return slapi_search_internal_bind( NULL, base, scope, filStr,
                        controls, attrs, attrsonly );
 #else