]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_ops.c
Tweak online_index task interval
[openldap] / servers / slapd / slapi / slapi_ops.c
index af2b62287642d0965c95cda5af25d97908820ec6..3e32409ecf40e4adf29a0995d9fdf769ab298acf 100644 (file)
@@ -1,17 +1,32 @@
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2002-2005 The OpenLDAP Foundation.
+ * Portions Copyright 1997,2002-2003 IBM Corporation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*
- * (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.
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by IBM Corporation for use in
+ * IBM products and subsequently ported to OpenLDAP Software by
+ * Steve Omrani.  Additional significant contributors include:
+ *   Luke Howard
  */
 
 #include "portable.h"
-//#include "../../../libraries/liblber/lber-int.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>
  * so it can be used in ACLs
  */
 static struct slap_listener slap_unknown_listener = {
-       BER_BVC("unknown"),     /* FIXME: use a URI form? */
+       BER_BVC("unknown"),     /* FIXME: use a URI form? (e.g. slapi://) */
        BER_BVC("UNKNOWN")
 };
 
-int bvptr2obj( struct berval **bvptr, struct berval **bvobj );
-
 static void
 internal_result_v3(
        Operation       *op, 
        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;
 }
 
@@ -40,12 +61,12 @@ internal_search_entry(
        Operation       *op,
        SlapReply       *rs )
 {
-       char *ent2str = NULL;
        int nentries = 0, len = 0, i = 0;
        Slapi_Entry **head = NULL, **tp;
-       
-       ent2str = slapi_entry2str( rs->sr_entry, &len );
-       if ( ent2str == NULL ) {
+       Slapi_Entry *entry;
+
+       entry = slapi_entry_dup( rs->sr_entry );
+       if ( entry == NULL ) {
                return 1;
        }
 
@@ -58,24 +79,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;
                  
@@ -87,17 +103,6 @@ internal_search_entry(
        return LDAP_SUCCESS;
 }
 
-static void
-internal_search_result(
-       Operation       *op,
-       SlapReply       *sr )
-{
-       slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
-                       SLAPI_NENTRIES, (void *)sr->sr_nentries );
-
-       return;
-}
-
 static void
 internal_result_ext(
        Operation       *op,    
@@ -114,12 +119,12 @@ internal_search_reference(
        return LDAP_SUCCESS;
 }
 
-static Connection *
-fakeConnection(
+Connection *
+slapi_int_init_connection(
        char *DN, 
        int OpType ) 
 { 
-       Connection *pConn, *c;
+       Connection *pConn;
        ber_len_t max = sockbuf_max_incoming;
 
        pConn = (Connection *) slapi_ch_calloc(1, sizeof(Connection));
@@ -144,89 +149,129 @@ fakeConnection(
                return (Connection *)NULL;
        }
 
-       c = pConn;
-
-       /* operation object */
-       c->c_pending_ops.stqh_first->o_tag = OpType;
-       c->c_pending_ops.stqh_first->o_protocol = LDAP_VERSION3; 
-       c->c_pending_ops.stqh_first->o_authmech.bv_val = NULL; 
-       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 );
-
        /* connection object */
-       c->c_authmech.bv_val = NULL;
-       c->c_authmech.bv_len = 0;
-       c->c_dn.bv_val = NULL;
-       c->c_dn.bv_len = 0;
-       c->c_ndn.bv_val = NULL;
-       c->c_ndn.bv_len = 0;
-       c->c_groups = NULL;
+       pConn->c_authmech.bv_val = NULL;
+       pConn->c_authmech.bv_len = 0;
+       pConn->c_dn.bv_val = NULL;
+       pConn->c_dn.bv_len = 0;
+       pConn->c_ndn.bv_val = NULL;
+       pConn->c_ndn.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 );
+       pConn->c_listener = &slap_unknown_listener;
+       ber_dupbv( &pConn->c_peer_domain, (struct berval *)&slap_unknown_bv );
+       ber_dupbv( &pConn->c_peer_name, (struct berval *)&slap_unknown_bv );
 
-       LDAP_STAILQ_INIT( &c->c_ops );
+       LDAP_STAILQ_INIT( &pConn->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_extra = NULL;
+       pConn->c_sasl_bind_mech.bv_val = NULL;
+       pConn->c_sasl_bind_mech.bv_len = 0;
+       pConn->c_sasl_authctx = NULL;
+       pConn->c_sasl_sockctx = NULL;
+       pConn->c_sasl_extra = NULL;
 
-       c->c_sb = ber_sockbuf_alloc( );
+       pConn->c_sb = ber_sockbuf_alloc( );
 
-       ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
+       ber_sockbuf_ctrl( pConn->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
 
-       c->c_currentber = NULL;
+       pConn->c_currentber = NULL;
 
        /* should check status of thread calls */
-       ldap_pvt_thread_mutex_init( &c->c_mutex );
-       ldap_pvt_thread_mutex_init( &c->c_write_mutex );
-       ldap_pvt_thread_cond_init( &c->c_write_cv );
+       ldap_pvt_thread_mutex_init( &pConn->c_mutex );
+       ldap_pvt_thread_mutex_init( &pConn->c_write_mutex );
+       ldap_pvt_thread_cond_init( &pConn->c_write_cv );
 
-       c->c_n_ops_received = 0;
-       c->c_n_ops_executing = 0;
-       c->c_n_ops_pending = 0;
-       c->c_n_ops_completed = 0;
+       ldap_pvt_thread_mutex_lock( &pConn->c_mutex );
 
-       c->c_n_get = 0;
-       c->c_n_read = 0;
-       c->c_n_write = 0;
+       pConn->c_n_ops_received = 0;
+       pConn->c_n_ops_executing = 0;
+       pConn->c_n_ops_pending = 0;
+       pConn->c_n_ops_completed = 0;
 
-       c->c_protocol = LDAP_VERSION3; 
+       pConn->c_n_get = 0;
+       pConn->c_n_read = 0;
+       pConn->c_n_write = 0;
 
-       c->c_activitytime = c->c_starttime = slap_get_time();
+       pConn->c_protocol = LDAP_VERSION3; 
 
-       c->c_connid = 0;
+       pConn->c_activitytime = pConn->c_starttime = slap_get_time();
 
-       c->c_conn_state  = 0x01;        /* SLAP_C_ACTIVE */
-       c->c_struct_state = 0x02;       /* SLAP_C_USED */
+       /*
+        * A real connection ID is required, because syncrepl associates
+        * pending CSNs with unique ( connection, operation ) tuples.
+        * Setting a fake connection ID will cause slap_get_commit_csn()
+        * to return a stale value.
+        */
+       connection_assign_nextid( pConn );
 
-       c->c_ssf = c->c_transport_ssf = 0;
-       c->c_tls_ssf = 0;
+       pConn->c_conn_state  = 0x01;    /* SLAP_C_ACTIVE */
+       pConn->c_struct_state = 0x02;   /* SLAP_C_USED */
 
-       backend_connection_init( c );
+       pConn->c_ssf = pConn->c_transport_ssf = 0;
+       pConn->c_tls_ssf = 0;
+
+       backend_connection_init( pConn );
 
        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;
 
+       /* operation object */
+       pConn->c_pending_ops.stqh_first->o_tag = OpType;
+       pConn->c_pending_ops.stqh_first->o_protocol = LDAP_VERSION3; 
+       pConn->c_pending_ops.stqh_first->o_authmech.bv_val = NULL; 
+       pConn->c_pending_ops.stqh_first->o_authmech.bv_len = 0; 
+       pConn->c_pending_ops.stqh_first->o_time = slap_get_time();
+       pConn->c_pending_ops.stqh_first->o_do_not_cache = 1;
+       pConn->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context();
+       pConn->c_pending_ops.stqh_first->o_tmpmemctx = NULL;
+       pConn->c_pending_ops.stqh_first->o_tmpmfuncs = &ch_mfuncs;
+       pConn->c_pending_ops.stqh_first->o_conn = pConn;
+       pConn->c_pending_ops.stqh_first->o_connid = pConn->c_connid;
+
+       ldap_pvt_thread_mutex_unlock( &pConn->c_mutex );
+
        return pConn;
 }
 
+void slapi_int_connection_destroy( Connection **pConn )
+{
+       Connection *conn = *pConn;
+       Operation *op;
+
+       if ( pConn == NULL ) {
+               return;
+       }
+
+       op = (Operation *)conn->c_pending_ops.stqh_first;
+
+       slap_graduate_commit_csn( op );
+
+       if ( op->o_req_dn.bv_val != NULL ) {
+               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
+       }
+       if ( op->o_req_ndn.bv_val != NULL ) {
+               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
+       }
+
+       if ( conn->c_sb != NULL ) {
+               ber_sockbuf_free( conn->c_sb );
+       }
+       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 i;
        BerVarray tmpberval;
@@ -237,15 +282,18 @@ values2obj(
        }
 
        for ( i = 0; ppValue[i] != NULL; i++ )
-               ;
+               ; /* EMPTY */
 
        tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
        if ( tmpberval == NULL ) {
                return LDAP_NO_MEMORY;
        }
        for ( i = 0; ppValue[i] != NULL; i++ ) {
-               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;
@@ -255,45 +303,59 @@ 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             i;
+       BerVarray       tmpberval;
 
-       for ( ; ml != NULL; ml = next ) {
-               next = ml->sml_next;
+       if ( bvptr == NULL ) {
+               *bvobj = NULL;
+               return LDAP_SUCCESS;
+       }
 
-               slapi_ch_free( (void **)&ml->sml_bvalues );
-#ifdef SLAP_NVALUES
-               slapi_ch_free( (void **)&ml->sml_nvalues );
-#endif
-               slapi_ch_free( (void **)&ml );
+       for ( i = 0; bvptr[i] != NULL; i++ )
+               ; /* EMPTY */
+
+       tmpberval = (BerVarray)slapi_ch_malloc( (i + 1) * sizeof(struct berval));
+       if ( tmpberval == NULL ) {
+               return LDAP_NO_MEMORY;
+       } 
+
+       for ( i = 0; bvptr[i] != NULL; i++ ) {
+               tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
+               tmpberval[i].bv_len = bvptr[i]->bv_len;
+               AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
        }
+
+       tmpberval[i].bv_val = NULL;
+       tmpberval[i].bv_len = 0;
+
+       *bvobj = tmpberval;
+
+       return LDAP_SUCCESS;
 }
 
 /*
- * Function : LDAPModToEntry 
+ * Function : slapi_int_ldapmod_to_entry 
  * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
  * with a link list of the correspondent attributes.
  * Return value : LDAP_SUCCESS
  *                LDAP_NO_MEMORY
  *                LDAP_OTHER
 */
-Entry *
-LDAPModToEntry(
+static Entry *
+slapi_int_ldapmod_to_entry(
+       Connection *pConn,
        char *ldn, 
        LDAPMod **mods )
 {
-       struct berval           dn = { 0, NULL };
+       struct berval           dn = BER_BVNULL;
        Entry                   *pEntry=NULL;
        LDAPMod                 *pMod;
        struct berval           *bv;
-       Backend                 *be;
        Operation               *op;
 
        Modifications           *modlist = NULL;
@@ -305,13 +367,7 @@ LDAPModToEntry(
 
        const char              *text = NULL;
 
-
-       op = (Operation *) slapi_ch_calloc(1, sizeof(Operation));
-       if ( pEntry == NULL) {
-               rc = LDAP_NO_MEMORY;
-               goto cleanup;
-       }  
-       op->o_tag = LDAP_REQ_ADD;
+       op = (Operation *)pConn->c_pending_ops.stqh_first;
 
        pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
        if ( pEntry == NULL) {
@@ -322,23 +378,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;
-#ifdef SLAP_NVALUES
+                               tmp.sml_values = bv;
                                tmp.sml_nvalues = NULL;
-#endif
                
                                mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
@@ -346,10 +409,8 @@ LDAPModToEntry(
                                mod->sml_next = NULL;
                                mod->sml_desc = NULL;
                                mod->sml_type = tmp.sml_type;
-                               mod->sml_bvalues = tmp.sml_bvalues;
-#ifdef SLAP_NVALUES
+                               mod->sml_values = tmp.sml_values;
                                mod->sml_nvalues = tmp.sml_nvalues;
-#endif
 
                                *modtail = mod;
                                modtail = &mod->sml_next;
@@ -360,14 +421,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;
-#ifdef SLAP_NVALUES
+                                       tmp.sml_values = bv;
                                        tmp.sml_nvalues = NULL;
-#endif
                
                                        mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
@@ -375,10 +434,8 @@ LDAPModToEntry(
                                        mod->sml_next = NULL;
                                        mod->sml_desc = NULL;
                                        mod->sml_type = tmp.sml_type;
-                                       mod->sml_bvalues = tmp.sml_bvalues;
-#ifdef SLAP_NVALUES
+                                       mod->sml_values = tmp.sml_values;
                                        mod->sml_nvalues = tmp.sml_nvalues;
-#endif
 
                                        *modtail = mod;
                                        modtail = &mod->sml_next;
@@ -387,40 +444,41 @@ 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;
-                       char textbuf[SLAP_TEXT_BUFLEN];
-                       size_t textlen = sizeof textbuf;
-
-                       rc = slap_mods_check( modlist, update, &text, 
-                                       textbuf, textlen );
+       op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
+       if ( op->o_bd == NULL ) {
+               rc = LDAP_PARTIAL_RESULTS;
+       } else {
+               int repl_user = be_isupdate_dn( op->o_bd, &op->o_bd->be_rootdn );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       int     update = !BER_BVISNULL( &op->o_bd->be_update_ndn );
+                       char    textbuf[ SLAP_TEXT_BUFLEN ];
+                       size_t  textlen = sizeof( textbuf );
+
+                       rc = slap_mods_check( modlist, &text, 
+                                       textbuf, textlen, NULL );
                        if ( rc != LDAP_SUCCESS) {
                                goto cleanup;
                        }
 
+                       if ( !update ) {
+                               rc = slap_mods_no_update_check( modlist,
+                                               &text, textbuf, textlen );
+                               if ( rc != LDAP_SUCCESS) {
+                                       goto cleanup;
+                               }
+                       }
+
                        if ( !repl_user ) {
                                rc = slap_mods_opattrs( op,
                                                modlist, modtail, &text, 
-                                               textbuf, textlen );
+                                               textbuf, textlen, 1 );
                                if ( rc != LDAP_SUCCESS) {
                                        goto cleanup;
                                }
                        }
 
-                       /*
-                        * 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;
                        }
@@ -428,15 +486,14 @@ 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 ( modlist != NULL )
+               slap_mods_free( modlist );
        if ( rc != LDAP_SUCCESS ) {
                if ( pEntry != NULL ) {
                        slapi_entry_free( pEntry );
@@ -463,28 +520,24 @@ 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 = BER_BVNULL;
 
-       struct berval dn  = { 0, NULL };
-
-       int                             rc=LDAP_SUCCESS;
-       int                             manageDsaIt = 0;
-       int                             isCritical;
+       int                     manageDsaIt = SLAP_CONTROL_NONE;
+       int                     isCritical;
 
        if ( ldn == NULL ) {
-               rc = LDAP_PARAM_ERROR; 
+               rs.sr_err = LDAP_PARAM_ERROR; 
                goto cleanup;
        }
 
-       pConn = fakeConnection( NULL, LDAP_REQ_DELETE );
+       pConn = slapi_int_init_connection( NULL, LDAP_REQ_DELETE );
        if (pConn == NULL) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -494,146 +547,153 @@ slapi_delete_internal(
 
        dn.bv_val = slapi_ch_strdup(ldn);
        dn.bv_len = strlen(ldn);
-       rc = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_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 ( slapi_control_present( controls, 
                        SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical) ) {
-               manageDsaIt = 1
+               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL
        }
 
-       be = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
+       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;
 
-       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)( op, &rs );
-                       if ( rc == 0 ) {
-                               if (log_change) {
-                                       replog( op );
-                               }
-                               rc = LDAP_SUCCESS;
-                       } else {
-                               rc = LDAP_OTHER;
+       if ( op->o_bd->be_delete ) {
+               int repl_user = be_isupdate( op );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
+                       if ( log_change ) op->o_callback = &cb;
+                       if ( (*op->o_bd->be_delete)( op, &rs ) ) {
+                               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 )
+       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 ( op->o_req_dn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-       if ( op->o_req_ndn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_ndn.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 );
        }
-       
-       return (pSavePB);
-#endif /* LDAP_SLAPI */
+
+       slapi_int_connection_destroy( &pConn );
+
+       return pPB;
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
-Slapi_PBlock * 
-slapi_add_entry_internal(
-       Slapi_Entry *e, 
+#ifdef LDAP_SLAPI
+static Slapi_PBlock * 
+slapi_int_add_entry_locked(
+       Connection *pConn,
+       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;
+       Slapi_PBlock            *pPB = NULL;
 
-       int                     manageDsaIt = 0;
+       int                     manageDsaIt = SLAP_CONTROL_NONE;
        int                     isCritical;
-       int                     rc = LDAP_SUCCESS;
        SlapReply               rs = { REP_RESULT };
 
-       if ( e == NULL ) {
-               rc = LDAP_PARAM_ERROR;
-               goto cleanup;
-       }
-       
-       pConn = fakeConnection( NULL, LDAP_REQ_ADD );
-       if ( pConn == NULL ) {
-               rc = LDAP_NO_MEMORY;
+       if ( *e == NULL ) {
+               rs.sr_err = LDAP_PARAM_ERROR;
                goto cleanup;
        }
 
        if ( slapi_control_present( controls, LDAP_CONTROL_MANAGEDSAIT,
                                NULL, &isCritical ) ) {
-               manageDsaIt = 1
+               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL
        }
 
        op = (Operation *)pConn->c_pending_ops.stqh_first;
        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, 1 );
+       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->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)( op, &rs ) == 0 ) {
-                               if ( log_changes ) {
-                                       replog( op );
-                               }
-                               rc = LDAP_SUCCESS;
+       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 ( op->o_bd->be_add ) {
+               int repl_user = be_isupdate( op );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
+                       if ( log_changes ) op->o_callback = &cb;
+                       if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
+                               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 );
-       }
-       return( pSavePB );
+       return( pPB );
+}
 #endif /* LDAP_SLAPI */
+
+Slapi_PBlock * 
+slapi_add_entry_internal(
+       Slapi_Entry *e, 
+       LDAPControl **controls, 
+       int log_changes ) 
+{
+#ifdef LDAP_SLAPI
+       Slapi_PBlock            *pb = NULL;
+       Slapi_Entry             *entry = NULL;
+       Connection              *pConn = NULL;
+
+       pConn = slapi_int_init_connection( NULL, LDAP_REQ_ADD );
+       if ( pConn == NULL ) {
+               return NULL;
+       }
+
+       /*
+        * 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_int_add_entry_locked( pConn, &entry, controls, log_changes );
+       if ( entry != NULL ) {
+               slapi_entry_free( entry );
+       }
+
+       slapi_int_connection_destroy( &pConn );
+
+       return pb;
+#else
        return NULL;
+#endif
 }
 
-
 Slapi_PBlock *
 slapi_add_internal(
        char *dn, 
@@ -641,11 +701,12 @@ slapi_add_internal(
        LDAPControl **controls, 
        int log_changes  ) 
 {
-#if defined(LDAP_SLAPI)
+#ifdef LDAP_SLAPI
        LDAPMod                 *pMod = NULL;
+       Connection              *pConn = 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 ;
@@ -653,7 +714,7 @@ slapi_add_internal(
 
        if ( rc == LDAP_SUCCESS ) {
                for ( i = 0, pMod = mods[0]; pMod != NULL; pMod = mods[++i] ) {
-                       if ( (pMod->mod_op & ~LDAP_MOD_BVALUES) != LDAP_MOD_ADD ) {
+                       if ( (pMod->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
                                rc = LDAP_OTHER;
                                break;
                        }
@@ -661,8 +722,12 @@ slapi_add_internal(
        }
 
        if ( rc == LDAP_SUCCESS ) {
-               if((pEntry = LDAPModToEntry( dn, mods )) == NULL) {
-                       rc = LDAP_OTHER;
+               pConn = slapi_int_init_connection( NULL, LDAP_REQ_ADD );
+               if ( pConn != NULL ) {
+                       pEntry = slapi_int_ldapmod_to_entry( pConn, dn, mods );
+                       if ( pEntry == NULL ) {
+                               rc = LDAP_OTHER;
+                       }
                }
        }
 
@@ -670,16 +735,19 @@ slapi_add_internal(
                pb = slapi_pblock_new();
                slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
        } else {
-               pb = slapi_add_entry_internal( pEntry, controls, log_changes );
+               pb = slapi_int_add_entry_locked( pConn, &pEntry, controls, log_changes );
        }
 
-       if ( pEntry ) {
-               slapi_entry_free(pEntry);
+       if ( pEntry != NULL ) {
+               slapi_entry_free( pEntry );
        }
 
-       return(pb);
-#endif /* LDAP_SLAPI */
+       slapi_int_connection_destroy( &pConn );
+
+       return pb;
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 /* Function : slapi_modrdn_internal
@@ -702,22 +770,19 @@ slapi_modrdn_internal(
        LDAPControl **controls, 
        int log_change )
 {
-#if defined(LDAP_SLAPI)
-       int                     rc = LDAP_SUCCESS;
-       struct berval           dn = { 0, NULL };
-       struct berval           newrdn = { 0, NULL };
+#ifdef LDAP_SLAPI
+       struct berval           dn = BER_BVNULL;
+       struct berval           newrdn = BER_BVNULL;
        Connection              *pConn = NULL;
        Operation               *op = NULL;
        Slapi_PBlock            *pPB = NULL;
-       Slapi_PBlock            *pSavePB = NULL;
-       Backend                 *be;
-       int                     manageDsaIt = 0;
+       int                     manageDsaIt = SLAP_CONTROL_NONE;
        int                     isCritical;
        SlapReply               rs = { REP_RESULT };
 
-       pConn = fakeConnection( NULL,  LDAP_REQ_MODRDN);
+       pConn = slapi_int_init_connection( NULL,  LDAP_REQ_MODRDN );
        if ( pConn == NULL) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -727,40 +792,40 @@ slapi_modrdn_internal(
 
        if ( slapi_control_present( controls, 
                        SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
-               manageDsaIt = 1;
+               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
        }
 
-       be = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
+       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;
 
        dn.bv_val = slapi_ch_strdup( olddn );
        dn.bv_len = strlen( olddn );
 
-       rc = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn );
-       if ( rc != LDAP_SUCCESS ) {
+       rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
                goto cleanup;
        }
 
        if ( op->o_req_dn.bv_len == 0 ) {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
        newrdn.bv_val = slapi_ch_strdup( lnewrdn );
        newrdn.bv_len = strlen( lnewrdn );
 
-       rc = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn );
-       if ( rc != LDAP_SUCCESS ) {
+       rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
+       if ( rs.sr_err != LDAP_SUCCESS ) {
                goto cleanup;
        }
 
-       if ( rdnValidate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
+       if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
                goto cleanup;
        }
 
@@ -768,40 +833,29 @@ slapi_modrdn_internal(
        op->oq_modrdn.rs_nnewSup = NULL;
        op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
 
-       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)( op, &rs );
-                       if ( rc == 0 ) {
-                               if ( log_change ) {
-                                       replog( op );
-                               }
-                               rc = LDAP_SUCCESS;
-
-                       } else {
-                               rc = LDAP_OTHER;
+       if ( op->o_bd->be_modrdn ) {
+               int repl_user = be_isupdate( op );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
+                       if ( log_change ) op->o_callback = &cb;
+                       if ( (*op->o_bd->be_modrdn)( op, &rs ) ) {
+                               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 )
                slapi_ch_free( (void **)&dn.bv_val );
-       if ( op->o_req_dn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-       if ( op->o_req_ndn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
 
        if ( newrdn.bv_val )
                slapi_ch_free( (void **)&newrdn.bv_val );
@@ -810,18 +864,12 @@ cleanup:
        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 );
-       }
+       slapi_int_connection_destroy( &pConn );
 
-       return( pSavePB );
-#endif /* LDAP_SLAPI */
+       return pPB;
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 /* Function : slapi_modify_internal
@@ -841,18 +889,16 @@ 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 dn = BER_BVNULL;
 
-       int                     manageDsaIt = 0;
+       int                     manageDsaIt = SLAP_CONTROL_NONE;
        int                     isCritical;
-       Backend                 *be;
        struct berval           *bv;
        LDAPMod                 *pMod;
 
@@ -863,13 +909,13 @@ slapi_modify_internal(
        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 = slapi_int_init_connection( NULL,  LDAP_REQ_MODIFY );
        if ( pConn == NULL ) {
-               rc = LDAP_NO_MEMORY;
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
@@ -879,171 +925,164 @@ slapi_modify_internal(
 
        dn.bv_val = slapi_ch_strdup( ldn );
        dn.bv_len = strlen( ldn );
-       rc = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn );
-       if ( rc != LDAP_SUCCESS ) {
+       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;
+               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
        }
 
-       be = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               rc =  LDAP_PARTIAL_RESULTS;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
+       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;
 
-       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;
-#ifdef SLAP_NVALUES
+                       tmp.sml_values = bv;
                        tmp.sml_nvalues = NULL;
-#endif
 
                        mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
 
-                       mod->sml_op = pMod->mod_op;
+                       mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
                        mod->sml_next = NULL;
                        mod->sml_desc = NULL;
                        mod->sml_type = tmp.sml_type;
-                       mod->sml_bvalues = tmp.sml_bvalues;
-#ifdef SLAP_NVALUES
+                       mod->sml_values = tmp.sml_values;
                        mod->sml_nvalues = tmp.sml_nvalues;
-#endif
                } 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;
-#ifdef SLAP_NVALUES
+                       tmp.sml_values = bv;
                        tmp.sml_nvalues = NULL;
-#endif
 
                        mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
 
-                       mod->sml_op = pMod->mod_op;
+                       mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
                        mod->sml_next = NULL;
                        mod->sml_desc = NULL;
                        mod->sml_type = tmp.sml_type;
-                       mod->sml_bvalues = tmp.sml_bvalues;
-#ifdef SLAP_NVALUES
+                       mod->sml_values = tmp.sml_values;
                        mod->sml_nvalues = tmp.sml_nvalues;
-#endif
                }
                *modtail = mod;
                modtail = &mod->sml_next;
 
-               switch( pMod->mod_op ) {
+               switch( pMod->mod_op & LDAP_MOD_OP ) {
                case LDAP_MOD_ADD:
-               if ( mod->sml_bvalues == NULL ) {
-                       rc = LDAP_PROTOCOL_ERROR;
+               if ( mod->sml_values == NULL ) {
+                       rs.sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
 
                /* fall through */
                case LDAP_MOD_DELETE:
                case LDAP_MOD_REPLACE:
+               case LDAP_MOD_INCREMENT:
                break;
 
                default:
-                       rc = LDAP_PROTOCOL_ERROR;
+                       rs.sr_err = LDAP_PROTOCOL_ERROR;
                        goto cleanup;
                }
        } 
        *modtail = NULL;
 
        if ( op->o_req_ndn.bv_len == 0 ) {
-               rc = LDAP_UNWILLING_TO_PERFORM;
+               rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
                goto cleanup;
        }
 
        op->oq_modify.rs_modlist = modlist;
 
-       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;
-                       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) {
+       if ( op->o_bd->be_modify ) {
+               int repl_user = be_isupdate( op );
+               if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
+                       int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
+                       const char      *text = NULL;
+                       char            textbuf[ SLAP_TEXT_BUFLEN ];
+                       size_t          textlen = sizeof( textbuf );
+                       slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
+
+                       rs.sr_err = slap_mods_check( modlist,
+                                       &text, textbuf, textlen, NULL );
+                       if ( rs.sr_err != LDAP_SUCCESS ) {
                                goto cleanup;
                        }
 
+                       if ( !update ) {
+                               rs.sr_err = slap_mods_no_update_check( modlist,
+                                               &text, textbuf, textlen );
+                               if ( rs.sr_err != LDAP_SUCCESS ) {
+                                       goto cleanup;
+                               }
+                       }
+
                        if ( !repl_user ) {
-                               rc = slap_mods_opattrs( op, modlist,
+                               rs.sr_err = slap_mods_opattrs( op, modlist,
                                                modtail, &text, textbuf, 
-                                               textlen );
-                               if (rc != LDAP_SUCCESS) {
+                                               textlen, 1 );
+                               if ( rs.sr_err != LDAP_SUCCESS ) {
                                        goto cleanup;
                                }
                        }
-                       rc = (*be->be_modify)( op, &rs );
-                       if ( rc == 0 ) {
-                               if ( log_change ) {
-                                       replog( op );
-                               }
-                               rc = LDAP_SUCCESS;
-                       } else {
-                               rc = LDAP_OTHER;
+                       if ( log_change ) op->o_callback = &cb;
+                       if ( (*op->o_bd->be_modify)( op, &rs ) ) {
+                               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 )
                slapi_ch_free( (void **)&dn.bv_val );
-       if ( op->o_req_dn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-       if ( op->o_req_ndn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
-
-       if ( modlist != NULL ) freeMods( 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 );
-       }
+       if ( modlist != NULL )
+               slap_mods_free( modlist );
 
-       return ( pSavePB );
+       slapi_int_connection_destroy( &pConn );
 
-#endif /* LDAP_SLAPI */
+       return pPB;
+#else
        return NULL;
+#endif /* LDAP_SLAPI */
 }
 
 Slapi_PBlock *
-slapi_search_internal_bind(
-       char *bindDN, 
+slapi_search_internal(
        char *ldn, 
        int scope, 
        char *filStr, 
@@ -1051,53 +1090,52 @@ 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 };
+       Slapi_PBlock            *pPB = NULL;            
+       struct berval           dn = BER_BVNULL;
        Filter                  *filter=NULL;
-       struct berval           fstr = { 0, NULL };
+       struct berval           fstr = BER_BVNULL;
        AttributeName           *an = NULL;
        const char              *text = NULL;
 
-       int                     manageDsaIt = 0
+       int                     manageDsaIt = SLAP_CONTROL_NONE
        int                     isCritical;
-
-       int                     i, rc = LDAP_SUCCESS;
+       int                     i;
 
        SlapReply               rs = { REP_RESULT };
 
-       c = fakeConnection( NULL, LDAP_REQ_SEARCH );
-       if (c == NULL) {
-               rc = LDAP_NO_MEMORY;
+       c = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
+       if ( c == NULL ) {
+               rs.sr_err = LDAP_NO_MEMORY;
                goto cleanup;
        }
 
        op = (Operation *)c->c_pending_ops.stqh_first;
-       ptr = (Slapi_PBlock *)op->o_pb;
+       pPB = (Slapi_PBlock *)op->o_pb;
        op->o_ctrls = controls;
 
-       dn.bv_val = slapi_ch_strdup(ldn);
-       dn.bv_len = strlen(ldn);
+       if ( ldn != NULL ) {
+               dn.bv_val = slapi_ch_strdup(ldn);
+               dn.bv_len = strlen(ldn);
+       }
 
-       rc = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn );
-       if ( rc != LDAP_SUCCESS ) {
+       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;
        }
 
@@ -1112,6 +1150,7 @@ slapi_search_internal_bind(
                for (i = 0; attrs[i] != 0; i++) {
                        an[i].an_desc = NULL;
                        an[i].an_oc = NULL;
+                       an[i].an_oc_exclude = 0;
                        an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
                        an[i].an_name.bv_len = strlen(attrs[i]);
                        slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
@@ -1121,7 +1160,8 @@ slapi_search_internal_bind(
 
        memset( &rs, 0, sizeof(rs) );
        rs.sr_type = REP_RESULT;
-       rs.sr_err = LDAP_OTHER;
+       rs.sr_err = LDAP_SUCCESS;
+       rs.sr_entry = NULL; /* paranoia */
 
        if ( scope == LDAP_SCOPE_BASE ) {
                rs.sr_entry = NULL;
@@ -1134,12 +1174,14 @@ slapi_search_internal_bind(
                        send_ldap_result( op, &rs );
                        goto cleanup;
                } else if ( rs.sr_entry != NULL ) {
-                       rc = test_filter( op, rs.sr_entry, filter );
+                       rs.sr_err = test_filter( op, rs.sr_entry, filter );
 
-                       if( rc == LDAP_COMPARE_TRUE ) {
+                       if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
                                rs.sr_type = REP_SEARCH;
                                rs.sr_err = LDAP_SUCCESS;
                                rs.sr_attrs = an;
+                               rs.sr_operational_attrs = NULL;
+                               rs.sr_flags = REP_ENTRY_MODIFIABLE;
 
                                send_search_entry( op, &rs );
                        }
@@ -1151,15 +1193,13 @@ slapi_search_internal_bind(
 
                        send_ldap_result( op, &rs );
 
-                       rc = LDAP_SUCCESS;
-
                        goto cleanup;
                }
        }
 
        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 );
+               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
+               slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
 
                ber_dupbv( &op->o_req_dn, &default_search_base );
                ber_dupbv( &op->o_req_ndn, &default_search_nbase );
@@ -1167,82 +1207,56 @@ slapi_search_internal_bind(
 
        if ( slapi_control_present( controls,
                        LDAP_CONTROL_MANAGEDSAIT, NULL, &isCritical ) ) {
-               manageDsaIt = 1;
+               manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
        }
 
-       be = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
-       if ( be == NULL ) {
-               if ( manageDsaIt == 1 ) {
-                       rc = LDAP_NO_SUCH_OBJECT;
+       op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
+       if ( op->o_bd == NULL ) {
+               if ( manageDsaIt > SLAP_CONTROL_NONE  ) {
+                       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;
+       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_slimit = SLAP_NO_LIMIT;
+       op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
        op->oq_search.rs_attrsonly = attrsonly;
        op->oq_search.rs_attrs = an;
        op->oq_search.rs_filter = filter;
        op->oq_search.rs_filterstr = fstr;
 
-       if ( be->be_search ) {
-               rc = (*be->be_search)( op, &rs );
-               if ( rc == 0 ) {
-                       rc = LDAP_SUCCESS;
-               } else {
-                       rc = LDAP_OTHER;
+       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 ( 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 ( op->o_req_dn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_dn.bv_val );
-       if ( op->o_req_ndn.bv_val )
-               slapi_ch_free( (void **)&op->o_req_ndn.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 ( filter ) slapi_filter_free( filter, 1 );
-       if ( fstr.bv_val ) free ( fstr.bv_val );
+       slapi_int_connection_destroy( &c );
 
-       if ( an != NULL ) free( 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 );
-       }
-       return( pSavePB );
-#endif /* LDAP_SLAPI */
-       return NULL;
-}
-
-Slapi_PBlock * 
-slapi_search_internal(
-       char *base,
-       int scope,
-       char *filStr, 
-       LDAPControl **controls,
-       char **attrs,
-       int attrsonly ) 
-{
-#if defined(LDAP_SLAPI)
-       return slapi_search_internal_bind( NULL, base, scope, filStr,
-                       controls, attrs, attrsonly );
+       return pPB;
 #else
        return NULL;
 #endif /* LDAP_SLAPI */