Some noop changes + fixes entry leaks and REP_ENTRY_MODIFIABLE flag leaks.
        /*
         * We are now committed to cloak an attribute.
         */
-       if ( rs->sr_flags & REP_ENTRY_MODIFIABLE )
-               me = e;
-       else
-               me = entry_dup( e );
+       rs_ensure_entry_modifiable( op, rs, (slap_overinst *) op->o_bd->bd_info );
+       me = rs->sr_entry;
                
        for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
                Attribute *a;
 
        }
 
-       if ( me != e ) {
-               if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED )
-                       entry_free( e );
-
-               rs->sr_entry = me;
-               rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
-       }
-
        return ( SLAP_CB_CONTINUE );
 }
 
 
                                }
 
                        if ( !ap ) {
-                               if ( !rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
-                                       rs->sr_entry = entry_dup( rs->sr_entry );
-                                       rs->sr_flags |=
-                                               REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
+                               if ( rs_ensure_entry_modifiable( op,rs, on )) {
                                        a = attr_find( rs->sr_entry->e_attrs,
                                                ad_usnChanged );
                                }
 
                        * don't modify it directly. Make a copy and
                        * work with that instead.
                        */
-                       if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
-                               Entry *e;
-
-                               e = entry_dup( rs->sr_entry );
-                               if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                                       overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                                       rs->sr_flags &= ~REP_ENTRY_MUSTRELEASE;
-                               } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                                       entry_free( rs->sr_entry );
-                               }
-                               rs->sr_entry = e;
-                               rs->sr_flags |= REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
-                       }
+                       rs_ensure_entry_modifiable( op, rs, on );
 
                        /* Loop for each attribute in this collectinfo */
                        for(idx=0; idx<ci->ci_ad_num; idx++) {
 
        }
 
        /* clear entry if required */
-       if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-               entry_free( rs->sr_entry );
-               rs->sr_entry = NULL;
-               rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-       }
+       rs_flush_entry( op, rs, (slap_overinst *) op->o_bd->bd_info );
 
        return rc;
 }
 
                                }
 
                                if ( !ap ) {
-                                       if ( !(rs->sr_flags & REP_ENTRY_MODIFIABLE) ) {
-                                               Entry *e = entry_dup( rs->sr_entry );
-                                               if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                                                       overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                                                       rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-                                               } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                                                       entry_free( rs->sr_entry );
-                                               }
-                                               rs->sr_entry = e;
-                                               rs->sr_flags |=
-                                                       REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
+                                       if ( rs_ensure_entry_modifiable( op, rs, on )) {
                                                a = attr_find( rs->sr_entry->e_attrs,
                                                        slap_schema.si_ad_contextCSN );
                                        }
 
                if ( tc->step & USE_LIST ) {
                        re = tavl_delete( &tc->list, le, entry_dn_cmp );
                        if ( re ) {
-                               if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                                       rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-                                       overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                               }
-                               if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                                       rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-                                       entry_free( rs->sr_entry );
-                               }
+                               rs_flush_entry( op, rs, on );
                                rc = test_filter( op, re, tc->orig );
                                if ( rc == LDAP_COMPARE_TRUE ) {
                                        rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
                rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
                if ( rc == LDAP_SUCCESS && le ) {
                        re = entry_dup( rs->sr_entry );
-                       if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                               rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-                               overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                       }
-                       if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                               rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-                               entry_free( rs->sr_entry );
-                       }
+                       rs_flush_entry( op, rs, on );
                } else {
                        le = NULL;
                }
                }
                /* Dispose of local entry */
                if ( tc->step & LCL_SIDE ) {
-                       if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                               rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-                               overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                       }
-                       if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                               rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-                               entry_free( rs->sr_entry );
-                       }
+                       rs_flush_entry(op, rs, on);
                } else {
                        overlay_entry_release_ov(op, le, 0, on);
                }
 
                a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
                if ( !a ) continue;
 
-               if (( rs->sr_flags & ( REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED ) ) !=
-                       ( REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED ) )
-               {
-                       Entry *e;
-
-                       e = entry_dup( rs->sr_entry );
-                       if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                               overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-                               rs->sr_flags &= ~REP_ENTRY_MUSTRELEASE;
-                       } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                               entry_free( rs->sr_entry );
-                       }
-                       rs->sr_entry = e;
-                       rs->sr_flags |= REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
+               if ( rs_ensure_entry_modifiable( op, rs, on )) {
                        a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
                }
 
 
         * should set it back so that the cleanup functions know
         * what they're doing.
         */
-       if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
-               && rs->sr_entry 
-               && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
-       {
-               entry_free( rs->sr_entry );
-               rs->sr_entry = NULL;
-               rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
+       if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
+               rs_flush_entry( op, rs, NULL );
+       } else {
+               RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
        }
 
        if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
 
                break;
        case SLAPI_SEARCH_RESULT_ENTRY:
                PBLOCK_ASSERT_OP( pb, 0 );
-               if ( pb->pb_rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-                       entry_free( pb->pb_rs->sr_entry );
-               } else if ( pb->pb_rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-                       be_entry_release_r( pb->pb_op, pb->pb_rs->sr_entry );
-                       pb->pb_rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-               }
-               pb->pb_rs->sr_entry = (Slapi_Entry *)value;
+               rs_replace_entry( pb->pb_op, pb->pb_rs, NULL, (Slapi_Entry *)value );
+               /* TODO: Should REP_ENTRY_MODIFIABLE be set? */
                pb->pb_rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
                break;
        case SLAPI_BIND_RET_SASLCREDS:
 }
 
 #endif /* LDAP_SLAPI */
-