]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/rwm.c
ITS#7178 fix uninit'd op->ors_deref
[openldap] / servers / slapd / overlays / rwm.c
index 37ddd04e78de946da96d36c44f916ba27975812b..cc6211ed2508d4084d84e088afea42fe18a5471a 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2008 The OpenLDAP Foundation.
+ * Copyright 2003-2012 The OpenLDAP Foundation.
  * Portions Copyright 2003 Pierangelo Masarati.
  * All rights reserved.
  *
@@ -34,6 +34,8 @@ typedef struct rwm_op_state {
        struct berval ro_ndn;
        struct berval r_dn;
        struct berval r_ndn;
+       struct berval rx_dn;
+       struct berval rx_ndn;
        AttributeName *mapped_attrs;
        OpRequest o_request;
 } rwm_op_state;
@@ -52,30 +54,46 @@ rwm_send_entry( Operation *op, SlapReply *rs );
 static void
 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
 {
-       if ( !BER_BVISNULL( &ros->ro_dn ) ) {
-               op->o_req_dn = ros->ro_dn;
-       }
-       if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
-               op->o_req_ndn = ros->ro_ndn;
-       }
+       /* in case of successful extended operation cleanup
+        * gets called *after* (ITS#6632); this hack counts
+        * on others to cleanup our o_req_dn/o_req_ndn,
+        * while we cleanup theirs. */
+       if ( ros->r_tag == LDAP_REQ_EXTENDED && rs->sr_err == LDAP_SUCCESS ) {
+               if ( !BER_BVISNULL( &ros->rx_dn ) ) {
+                       ch_free( ros->rx_dn.bv_val );
+               }
+               if ( !BER_BVISNULL( &ros->rx_ndn ) ) {
+                       ch_free( ros->rx_ndn.bv_val );
+               }
 
-       if ( !BER_BVISNULL( &ros->r_dn )
-               && ros->r_dn.bv_val != ros->ro_dn.bv_val )
-       {
-               assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
-               ch_free( ros->r_dn.bv_val );
-               BER_BVZERO( &ros->r_dn );
-       }
+       } else {
+               if ( !BER_BVISNULL( &ros->ro_dn ) ) {
+                       op->o_req_dn = ros->ro_dn;
+               }
+               if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
+                       op->o_req_ndn = ros->ro_ndn;
+               }
 
-       if ( !BER_BVISNULL( &ros->r_ndn )
-               && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
-       {
-               ch_free( ros->r_ndn.bv_val );
-               BER_BVZERO( &ros->r_ndn );
+               if ( !BER_BVISNULL( &ros->r_dn )
+                       && ros->r_dn.bv_val != ros->ro_dn.bv_val )
+               {
+                       assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
+                       ch_free( ros->r_dn.bv_val );
+               }
+
+               if ( !BER_BVISNULL( &ros->r_ndn )
+                       && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
+               {
+                       ch_free( ros->r_ndn.bv_val );
+               }
        }
 
+       BER_BVZERO( &ros->r_dn );
+       BER_BVZERO( &ros->r_ndn );
        BER_BVZERO( &ros->ro_dn );
        BER_BVZERO( &ros->ro_ndn );
+       BER_BVZERO( &ros->rx_dn );
+       BER_BVZERO( &ros->rx_ndn );
 
        switch( ros->r_tag ) {
        case LDAP_REQ_COMPARE:
@@ -96,11 +114,17 @@ rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
                        op->orr_newSup = ros->orr_newSup;
                        op->orr_nnewSup = ros->orr_nnewSup;
                }
+               if ( op->orr_newrdn.bv_val != ros->orr_newrdn.bv_val ) {
+                       ch_free( op->orr_newrdn.bv_val );
+                       ch_free( op->orr_nnewrdn.bv_val );
+                       op->orr_newrdn = ros->orr_newrdn;
+                       op->orr_nnewrdn = ros->orr_nnewrdn;
+               }
                break;
        case LDAP_REQ_SEARCH:
-               ch_free( ros->mapped_attrs );
+               op->o_tmpfree( ros->mapped_attrs, op->o_tmpmemctx );
                filter_free_x( op, op->ors_filter, 1 );
-               ch_free( op->ors_filterstr.bv_val );
+               op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
                op->ors_attrs = ros->ors_attrs;
                op->ors_filter = ros->ors_filter;
                op->ors_filterstr = ros->ors_filterstr;
@@ -116,7 +140,7 @@ rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
 #if 0
                        ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
                        /* too late, c_mutex released */
-                       fprintf( stderr, "*** DN: \"%s\" => \"%s\"\n",
+                       Debug( LDAP_DEBUG_ANY, "*** DN: \"%s\" => \"%s\"\n",
                                op->o_conn->c_ndn.bv_val,
                                op->o_req_ndn.bv_val );
                        ber_bvreplace( &op->o_conn->c_ndn,
@@ -148,9 +172,9 @@ rwm_op_cleanup( Operation *op, SlapReply *rs )
 }
 
 static rwm_op_cb *
-rwm_callback_get( Operation *op, SlapReply *rs )
+rwm_callback_get( Operation *op )
 {
-       rwm_op_cb       *roc = NULL;
+       rwm_op_cb       *roc;
 
        roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
        roc->cb.sc_cleanup = rwm_op_cleanup;
@@ -160,9 +184,12 @@ rwm_callback_get( Operation *op, SlapReply *rs )
        roc->ros.r_tag = op->o_tag;
        roc->ros.ro_dn = op->o_req_dn;
        roc->ros.ro_ndn = op->o_req_ndn;
-       roc->ros.o_request = op->o_request;
        BER_BVZERO( &roc->ros.r_dn );
        BER_BVZERO( &roc->ros.r_ndn );
+       BER_BVZERO( &roc->ros.rx_dn );
+       BER_BVZERO( &roc->ros.rx_ndn );
+       roc->ros.mapped_attrs = NULL;
+       roc->ros.o_request = op->o_request;
 
        return roc;
 }
@@ -222,6 +249,11 @@ rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
        assert( BER_BVISNULL( &ros->r_ndn ) );
        ros->r_ndn = ndn;
 
+       if ( ros->r_tag == LDAP_REQ_EXTENDED ) {
+               ros->rx_dn = ros->r_dn;
+               ros->rx_ndn = ros->r_ndn;
+       }
+
        return LDAP_SUCCESS;
 }
 
@@ -238,7 +270,7 @@ rwm_op_add( Operation *op, SlapReply *rs )
        char                    *olddn = op->o_req_dn.bv_val;
        int                     isupdate;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -381,7 +413,7 @@ rwm_op_bind( Operation *op, SlapReply *rs )
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        int                     rc;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -417,7 +449,7 @@ rwm_op_compare( Operation *op, SlapReply *rs )
        int                     rc;
        struct berval           mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -502,7 +534,7 @@ rwm_op_delete( Operation *op, SlapReply *rs )
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        int                     rc;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -527,7 +559,7 @@ rwm_op_modify( Operation *op, SlapReply *rs )
        Modifications           **mlp;
        int                     rc;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -569,7 +601,7 @@ rwm_op_modify( Operation *op, SlapReply *rs )
                                        &mapping, RWM_MAP );
                        if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
                        {
-                               goto cleanup_mod;
+                               goto skip_mod;
                        }
                }
 
@@ -664,6 +696,10 @@ next_mod:;
                mlp = &ml->sml_next;
                continue;
 
+skip_mod:;
+               *mlp = (*mlp)->sml_next;
+               continue;
+
 cleanup_mod:;
                ml = *mlp;
                *mlp = (*mlp)->sml_next;
@@ -684,11 +720,11 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
                        (struct ldaprwmap *)on->on_bi.bi_private;
        
        int                     rc;
+       dncookie                dc;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        if ( op->orr_newSup ) {
-               dncookie        dc;
                struct berval   nnewSup = BER_BVNULL;
                struct berval   newSup = BER_BVNULL;
 
@@ -718,6 +754,32 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
                }
        }
 
+       /*
+        * Rewrite the newRDN, if needed
+        */
+       {
+               struct berval   newrdn = BER_BVNULL;
+               struct berval   nnewrdn = BER_BVNULL;
+
+               dc.rwmap = rwmap;
+               dc.conn = op->o_conn;
+               dc.rs = rs;
+               dc.ctx = "newRDN";
+               newrdn = op->orr_newrdn;
+               nnewrdn = op->orr_nnewrdn;
+               rc = rwm_dn_massage_pretty_normalize( &dc, &op->orr_newrdn, &newrdn, &nnewrdn );
+               if ( rc != LDAP_SUCCESS ) {
+                       op->o_bd->bd_info = (BackendInfo *)on->on_info;
+                       send_ldap_error( op, rs, rc, "newRDN massage error" );
+                       goto err;
+               }
+
+               if ( op->orr_newrdn.bv_val != newrdn.bv_val ) {
+                       op->orr_newrdn = newrdn;
+                       op->orr_nnewrdn = nnewrdn;
+               }
+       }
+
        /*
         * Rewrite the dn, if needed
         */
@@ -725,6 +787,15 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
                send_ldap_error( op, rs, rc, "renameDN massage error" );
+               goto err;
+       }
+
+       op->o_callback = &roc->cb;
+
+       rc = SLAP_CB_CONTINUE;
+
+       if ( 0 ) {
+err:;
                if ( op->orr_newSup != roc->ros.orr_newSup ) {
                        ch_free( op->orr_newSup->bv_val );
                        ch_free( op->orr_nnewSup->bv_val );
@@ -733,15 +804,16 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
                        op->orr_newSup = roc->ros.orr_newSup;
                        op->orr_nnewSup = roc->ros.orr_nnewSup;
                }
-               return -1;
-       }
-
-       /* TODO: rewrite newRDN, attribute types, 
-        * values of DN-valued attributes (hopefully not used in RDN)... */
 
-       op->o_callback = &roc->cb;
+               if ( op->orr_newrdn.bv_val != roc->ros.orr_newrdn.bv_val ) {
+                       ch_free( op->orr_newrdn.bv_val );
+                       ch_free( op->orr_nnewrdn.bv_val );
+                       op->orr_newrdn = roc->ros.orr_newrdn;
+                       op->orr_nnewrdn = roc->ros.orr_nnewrdn;
+               }
+       }
 
-       return SLAP_CB_CONTINUE;
+       return rc;
 }
 
 
@@ -781,7 +853,7 @@ rwm_entry_release_rw( Operation *op, Entry *e, int rw )
        }
 
        /* just free entry if (probably) ours */
-       if ( e->e_private == NULL ) {
+       if ( e->e_private == NULL && BER_BVISNULL( &e->e_bv ) ) {
                entry_free( e );
                return LDAP_SUCCESS;
        }
@@ -794,17 +866,13 @@ rwm_entry_get_rw( Operation *op, struct berval *ndn,
        ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
-       struct ldaprwmap        *rwmap = 
-                       (struct ldaprwmap *)on->on_bi.bi_private;
-
        int                     rc;
-       dncookie                dc;
-
        BackendDB               db;
        Operation               op2;
        SlapReply               rs = { REP_SEARCH };
 
        rwm_op_state            ros = { 0 };
+       struct berval           mndn = BER_BVNULL;
 
        if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
                return SLAP_CB_CONTINUE;
@@ -820,6 +888,8 @@ rwm_entry_get_rw( Operation *op, struct berval *ndn,
                return LDAP_OTHER;
        }
 
+       mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
+
        /* map attribute & objectClass */
        if ( at != NULL ) {
        }
@@ -832,20 +902,29 @@ rwm_entry_get_rw( Operation *op, struct berval *ndn,
        op2.o_bd = &db;
        op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
        op2.ors_attrs = slap_anlist_all_attributes;
-       rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &ros.r_ndn, oc, at, rw, ep );
+       rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
        if ( rc == LDAP_SUCCESS && *ep != NULL ) {
+               /* we assume be_entry_release() needs to be called */
+               rs.sr_flags = REP_ENTRY_MUSTRELEASE;
                rs.sr_entry = *ep;
 
                /* duplicate & release */
                op2.o_bd->bd_info = (BackendInfo *)on;
                rc = rwm_send_entry( &op2, &rs );
+               RS_ASSERT( rs.sr_flags & REP_ENTRY_MUSTFLUSH );
                if ( rc == SLAP_CB_CONTINUE ) {
                        *ep = rs.sr_entry;
                        rc = LDAP_SUCCESS;
+               } else {
+                       assert( rc != LDAP_SUCCESS && rs.sr_entry == *ep );
+                       *ep = NULL;
+                       op2.o_bd->bd_info = (BackendInfo *)on->on_info;
+                       be_entry_release_r( &op2, rs.sr_entry );
+                       op2.o_bd->bd_info = (BackendInfo *)on;
                }
        }
 
-       if ( ros.r_ndn.bv_val != ndn->bv_val ) {
+       if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
                op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
        }
 
@@ -869,7 +948,7 @@ rwm_op_search( Operation *op, SlapReply *rs )
 
        char                    *text = NULL;
 
-       rwm_op_cb               *roc = rwm_callback_get( op, rs );
+       rwm_op_cb               *roc = rwm_callback_get( op );
 
        rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
                "searchFilter", op->ors_filterstr.bv_val );
@@ -904,7 +983,7 @@ rwm_op_search( Operation *op, SlapReply *rs )
        op->ors_filter = f;
        op->ors_filterstr = fstr;
 
-       rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
+       rc = rwm_map_attrnames( op, &rwmap->rwm_at, &rwmap->rwm_oc,
                        op->ors_attrs, &an, RWM_MAP );
        if ( rc != LDAP_SUCCESS ) {
                text = "attribute list mapping error";
@@ -931,7 +1010,7 @@ error_return:;
        }
 
        if ( !BER_BVISNULL( &fstr ) ) {
-               ch_free( fstr.bv_val );
+               op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
        }
 
        rwm_op_rollback( op, rs, &roc->ros );
@@ -988,7 +1067,7 @@ rwm_exop_passwd( Operation *op, SlapReply *rs )
                ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
        }
 
-       roc = rwm_callback_get( op, rs );
+       roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -1060,7 +1139,7 @@ rwm_extended( Operation *op, SlapReply *rs )
                }
        }
 
-       roc = rwm_callback_get( op, rs );
+       roc = rwm_callback_get( op );
 
        rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
        if ( rc != LDAP_SUCCESS ) {
@@ -1075,7 +1154,7 @@ rwm_extended( Operation *op, SlapReply *rs )
        return SLAP_CB_CONTINUE;
 }
 
-static int
+static void
 rwm_matched( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
@@ -1087,7 +1166,7 @@ rwm_matched( Operation *op, SlapReply *rs )
        int                     rc;
 
        if ( rs->sr_matched == NULL ) {
-               return SLAP_CB_CONTINUE;
+               return;
        }
 
        dc.rwmap = rwmap;
@@ -1100,10 +1179,8 @@ rwm_matched( Operation *op, SlapReply *rs )
        if ( rc != LDAP_SUCCESS ) {
                rs->sr_err = rc;
                rs->sr_text = "Rewrite error";
-               return 1;
-       }
 
-       if ( mdn.bv_val != dn.bv_val ) {
+       } else if ( mdn.bv_val != dn.bv_val ) {
                if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
                        ch_free( (void *)rs->sr_matched );
 
@@ -1112,8 +1189,6 @@ rwm_matched( Operation *op, SlapReply *rs )
                }
                rs->sr_matched = mdn.bv_val;
        }
-       
-       return SLAP_CB_CONTINUE;
 }
 
 static int
@@ -1155,76 +1230,77 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
                int                     last = -1;
                Attribute               *a;
 
-               if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
+               if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
+                               op->ors_attrs != NULL && 
+                               !SLAP_USERATTRS( rs->sr_attr_flags ) &&
+                               !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
                {
-                       /* go on */ ;
-                       
-               } else {
-                       if ( op->ors_attrs != NULL && 
-                                       !SLAP_USERATTRS( rs->sr_attr_flags ) &&
-                                       !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
-                       {
-                               goto cleanup_attr;
-                       }
+                       goto cleanup_attr;
+               }
 
-                       drop_missing = rwm_mapping( &rwmap->rwm_at,
-                                       &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
-                       if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
-                       {
-                               goto cleanup_attr;
-                       }
-                       if ( mapping != NULL ) {
-                               assert( mapping->m_dst_ad != NULL );
+               drop_missing = rwm_mapping( &rwmap->rwm_at,
+                               &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
+               if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
+               {
+                       goto cleanup_attr;
+               }
+               if ( mapping != NULL ) {
+                       assert( mapping->m_dst_ad != NULL );
 
-                               /* try to normalize mapped Attributes if the original 
-                                * AttributeType was not normalized */
-                               if ( (!(*ap)->a_desc->ad_type->sat_equality || 
-                                       !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
-                                       mapping->m_dst_ad->ad_type->sat_equality &&
-                                       mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
+                       /* try to normalize mapped Attributes if the original 
+                        * AttributeType was not normalized */
+                       if ( (!(*ap)->a_desc->ad_type->sat_equality || 
+                               !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
+                               mapping->m_dst_ad->ad_type->sat_equality &&
+                               mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
+                       {
+                               if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
                                {
-                                       if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
+                                       int i = 0;
+
+                                       last = (*ap)->a_numvals;
+                                       if ( last )
                                        {
-                                               int i = 0;
-
-                                               last = (*ap)->a_numvals;
-                                               if ( last )
-                                               {
-                                                       (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
-
-                                                       for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
-                                                               int             rc;
-                                                               /*
-                                                                * check that each value is valid per syntax
-                                                                * and pretty if appropriate
+                                               (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
+
+                                               for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
+                                                       int             rc;
+                                                       /*
+                                                        * check that each value is valid per syntax
+                                                        * and pretty if appropriate
+                                                        */
+                                                       rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
+                                                               SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                                                               mapping->m_dst_ad->ad_type->sat_syntax,
+                                                               mapping->m_dst_ad->ad_type->sat_equality,
+                                                               &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
+                                                               NULL );
+
+                                                       if ( rc != LDAP_SUCCESS ) {
+                                                               /* FIXME: this is wrong, putting a non-normalized value
+                                                                * into nvals. But when a proxy sends us bogus data,
+                                                                * we still need to give it to the client, even if it
+                                                                * violates the syntax. I.e., we don't want to silently
+                                                                * drop things and trigger an apparent data loss.
                                                                 */
-                                                               rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
-                                                                       SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
-                                                                       mapping->m_dst_ad->ad_type->sat_syntax,
-                                                                       mapping->m_dst_ad->ad_type->sat_equality,
-                                                                       &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
-                                                                       NULL );
-
-                                                               if ( rc != LDAP_SUCCESS ) {
-                                                                       BER_BVZERO( &(*ap)->a_nvals[i] );
-                                                               }
+                                                               ber_dupbv( &(*ap)->a_nvals[i], &(*ap)->a_vals[i] );
                                                        }
-                                                       BER_BVZERO( &(*ap)->a_nvals[i] );
                                                }
-
-                                       } else {
-                                               assert( (*ap)->a_nvals == (*ap)->a_vals );
-                                               (*ap)->a_nvals = NULL;
-                                               ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
+                                               BER_BVZERO( &(*ap)->a_nvals[i] );
                                        }
+
+                               } else {
+                                       assert( (*ap)->a_nvals == (*ap)->a_vals );
+                                       (*ap)->a_nvals = NULL;
+                                       ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
                                }
+                       }
 
-                               /* rewrite the attribute description */
-                               (*ap)->a_desc = mapping->m_dst_ad;
+                       /* rewrite the attribute description */
+                       (*ap)->a_desc = mapping->m_dst_ad;
 
-                               /* will need to check for duplicate attrs */
-                               check_duplicate_attrs++;
-                       }
+                       /* will need to check for duplicate attrs */
+                       check_duplicate_attrs++;
                }
 
                if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
@@ -1389,6 +1465,7 @@ cleanup_attr:;
        return 0;
 }
 
+/* Should return SLAP_CB_CONTINUE or failure, never LDAP_SUCCESS. */
 static int
 rwm_send_entry( Operation *op, SlapReply *rs )
 {
@@ -1397,7 +1474,6 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
        Entry                   *e = NULL;
-       slap_mask_t             flags;
        struct berval           dn = BER_BVNULL,
                                ndn = BER_BVNULL;
        dncookie                dc;
@@ -1414,7 +1490,6 @@ rwm_send_entry( Operation *op, SlapReply *rs )
        dc.ctx = "searchEntryDN";
 
        e = rs->sr_entry;
-       flags = rs->sr_flags;
        if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
                /* FIXME: all we need to duplicate are:
                 * - dn
@@ -1422,15 +1497,17 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                 * - attributes that are requested
                 * - no values if attrsonly is set
                 */
-
                e = entry_dup( e );
                if ( e == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        goto fail;
                }
-
-               flags &= ~REP_ENTRY_MUSTRELEASE;
-               flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
+       } else if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
+               /* ITS#6423: REP_ENTRY_MUSTRELEASE incompatible
+                * with REP_ENTRY_MODIFIABLE */
+               RS_ASSERT( 0 );
+               rc = 1;
+               goto fail;
        }
 
        /*
@@ -1462,13 +1539,21 @@ rwm_send_entry( Operation *op, SlapReply *rs )
         * to return, and remap them accordingly */
        (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
 
-       if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-               overlay_entry_release_ov( op, rs->sr_entry, 0, on );
+       if ( e != rs->sr_entry ) {
+               /* Reimplementing rs_replace_entry(), I suppose to
+                * bypass our own dubious rwm_entry_release_rw() */
+               if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
+                       rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
+                       op->o_bd->bd_info = (BackendInfo *)on->on_info;
+                       be_entry_release_r( op, rs->sr_entry );
+                       op->o_bd->bd_info = (BackendInfo *)on;
+               } 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->sr_entry = e;
-       rs->sr_flags = flags;
-
        return SLAP_CB_CONTINUE;
 
 fail:;
@@ -1575,12 +1660,12 @@ rwm_suffixmassage_config(
         */
        if ( argc == 2 ) {
                if ( be->be_suffix == NULL ) {
-                       fprintf( stderr, "%s: line %d: "
+                       Debug( LDAP_DEBUG_ANY, "%s: line %d: "
                                       " \"suffixMassage [<suffix>]"
                                       " <massaged suffix>\" without "
                                       "<suffix> part requires database "
                                       "suffix be defined first.\n",
-                               fname, lineno );
+                               fname, lineno, 0 );
                        return 1;
                }
                bvnc = be->be_suffix[ 0 ];
@@ -1591,22 +1676,22 @@ rwm_suffixmassage_config(
                massaged = 2;
 
        } else  {
-               fprintf( stderr, "%s: line %d: syntax is"
+               Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is"
                               " \"suffixMassage [<suffix>]"
                               " <massaged suffix>\"\n",
-                       fname, lineno );
+                       fname, lineno, 0 );
                return 1;
        }
 
        if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
-               fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
+               Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
                        fname, lineno, bvnc.bv_val );
                return 1;
        }
 
        ber_str2bv( argv[ massaged ], 0, 0, &brnc );
        if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
-               fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
+               Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
                                fname, lineno, brnc.bv_val );
                free( nvnc.bv_val );
                free( pvnc.bv_val );
@@ -1678,20 +1763,18 @@ rwm_response( Operation *op, SlapReply *rs )
                        dc.rs = NULL; 
                        dc.ctx = "referralDN";
                        rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
+                       /* FIXME: impossible, so far */
                        if ( rc != LDAP_SUCCESS ) {
-                               rc = 1;
+                               rs->sr_err = rc;
                                break;
                        }
                }
-               rc = rwm_matched( op, rs );
-               break;
 
-       default:
-               rc = SLAP_CB_CONTINUE;
+               rwm_matched( op, rs );
                break;
        }
 
-       return rc;
+       return SLAP_CB_CONTINUE;
 }
 
 static int
@@ -1725,9 +1808,9 @@ rwm_db_config(
 
        } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
                if ( argc != 2 ) {
-                       fprintf( stderr,
+                       Debug( LDAP_DEBUG_ANY,
                "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
-                                       fname, lineno );
+                                       fname, lineno, 0 );
                        return( 1 );
                }
 
@@ -1739,17 +1822,17 @@ rwm_db_config(
 
                /* TODO: not implemented yet */
                } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
-                       fprintf( stderr,
+                       Debug( LDAP_DEBUG_ANY,
                "%s: line %d: \"discover\" not supported yet "
                "in \"t-f-support {no|yes|discover}\".\n",
-                                       fname, lineno );
+                                       fname, lineno, 0 );
                        return( 1 );
 #if 0
                        rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
 #endif
 
                } else {
-                       fprintf( stderr,
+                       Debug( LDAP_DEBUG_ANY,
        "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
                                fname, lineno, argv[ 1 ] );
                        return 1;
@@ -1757,9 +1840,9 @@ rwm_db_config(
 
        } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) ==  0 ) {
                if ( argc !=2 ) { 
-                       fprintf( stderr,
+                       Debug( LDAP_DEBUG_ANY,
                "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
-                                       fname, lineno );
+                                       fname, lineno, 0 );
                        return( 1 );
                }
 
@@ -1788,19 +1871,21 @@ rwm_db_config(
 enum {
        /* rewrite */
        RWM_CF_REWRITE = 1,
-       RWM_CF_SUFFIXMASSAGE,
 
        /* map */
        RWM_CF_MAP,
        RWM_CF_T_F_SUPPORT,
        RWM_CF_NORMALIZE_MAPPED,
+       RWM_CF_DROP_UNREQUESTED,
 
        RWM_CF_LAST
 };
 
 static slap_verbmasks t_f_mode[] = {
+       { BER_BVC( "true" ),            RWM_F_SUPPORT_T_F },
        { BER_BVC( "yes" ),             RWM_F_SUPPORT_T_F },
        { BER_BVC( "discover" ),        RWM_F_SUPPORT_T_F_DISCOVER },
+       { BER_BVC( "false" ),           RWM_F_NONE },
        { BER_BVC( "no" ),              RWM_F_NONE },
        { BER_BVNULL,                   0 }
 };
@@ -1810,7 +1895,7 @@ static ConfigDriver rwm_cf_gen;
 static ConfigTable rwmcfg[] = {
        { "rwm-rewrite", "rewrite",
                2, 0, STRLENOF("rwm-rewrite"),
-               ARG_MAGIC|ARG_QUOTE|RWM_CF_REWRITE, rwm_cf_gen,
+               ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
                "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
                        "DESC 'Rewrites strings' "
                        "EQUALITY caseIgnoreMatch "
@@ -1819,7 +1904,7 @@ static ConfigTable rwmcfg[] = {
                NULL, NULL },
 
        { "rwm-suffixmassage", "[virtual]> <real",
-               2, 3, 0, ARG_MAGIC|RWM_CF_SUFFIXMASSAGE, rwm_cf_gen,
+               2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
                NULL, NULL, NULL },
                
        { "rwm-t-f-support", "true|false|discover",
@@ -1834,6 +1919,7 @@ static ConfigTable rwmcfg[] = {
                2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
                "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
                        "DESC 'maps attributes/objectClasses' "
+                       "EQUALITY caseIgnoreMatch "
                        "SYNTAX OMsDirectoryString "
                        "X-ORDERED 'VALUES' )",
                NULL, NULL },
@@ -1846,6 +1932,14 @@ static ConfigTable rwmcfg[] = {
                        "SINGLE-VALUE )",
                NULL, NULL },
 
+       { "rwm-drop-unrequested-attrs", "true|false",
+               2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
+               "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
+                       "DESC 'Drop unrequested attributes' "
+                       "SYNTAX OMsBoolean "
+                       "SINGLE-VALUE )",
+               NULL, NULL },
+
        { NULL, NULL, 0, 0, 0, ARG_IGNORED }
 };
 
@@ -1865,7 +1959,7 @@ static ConfigOCs rwmocs[] = {
 };
 
 static void
-slap_rewrite_unparse( BerVarray in, BerVarray *out )
+slap_bv_x_ordered_unparse( BerVarray in, BerVarray *out )
 {
        int             i;
        BerVarray       bva = NULL;
@@ -1904,6 +1998,82 @@ slap_rewrite_unparse( BerVarray in, BerVarray *out )
        *out = bva;
 }
 
+static int
+rwm_bva_add(
+       BerVarray               *bva,
+       int                     idx,
+       char                    **argv )
+{
+       char            *line;
+       struct berval   bv;
+
+       line = ldap_charray2str( argv, "\" \"" );
+       if ( line != NULL ) {
+               int     len = strlen( argv[ 0 ] );
+
+               ber_str2bv( line, 0, 0, &bv );
+               AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
+                       bv.bv_len - ( len + 1 ) );
+               bv.bv_val[ bv.bv_len - 1 ] = '"';
+
+               if ( idx == -1 ) {
+                       ber_bvarray_add( bva, &bv );
+
+               } else {
+                       (*bva)[ idx ] = bv;
+               }
+
+               return 0;
+       }
+
+       return -1;
+}
+
+static int
+rwm_bva_rewrite_add(
+       struct ldaprwmap        *rwmap,
+       int                     idx,
+       char                    **argv )
+{
+       return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
+}
+
+#ifdef unused
+static int
+rwm_bva_map_add(
+       struct ldaprwmap        *rwmap,
+       int                     idx,
+       char                    **argv )
+{
+       return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
+}
+#endif /* unused */
+
+static int
+rwm_info_init( struct rewrite_info ** rwm_rw )
+{
+       char                    *rargv[ 3 ];
+
+       *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
+       if ( *rwm_rw == NULL ) {
+               return -1;
+       }
+
+       /* this rewriteContext by default must be null;
+        * rules can be added if required */
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "searchFilter";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
+
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "default";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
+
+       return 0;
+}
+
 static int
 rwm_cf_gen( ConfigArgs *c )
 {
@@ -1913,6 +2083,7 @@ rwm_cf_gen( ConfigArgs *c )
 
        BackendDB               db;
        char                    *argv0;
+       int                     idx0 = 0;
        int                     rc = 0;
 
        db = *c->be;
@@ -1927,7 +2098,7 @@ rwm_cf_gen( ConfigArgs *c )
                                rc = 1;
 
                        } else {
-                               slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
+                               slap_bv_x_ordered_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
                                if ( !c->rvalue_vals ) {
                                        rc = 1;
                                }
@@ -1951,7 +2122,10 @@ rwm_cf_gen( ConfigArgs *c )
                                rc = 1;
 
                        } else {
-                               value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
+                               slap_bv_x_ordered_unparse( rwmap->rwm_bva_map, &c->rvalue_vals );
+                               if ( !c->rvalue_vals ) {
+                                       rc = 1;
+                               }
                        }
                        break;
 
@@ -1959,6 +2133,10 @@ rwm_cf_gen( ConfigArgs *c )
                        c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
                        break;
 
+               case RWM_CF_DROP_UNREQUESTED:
+                       c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
+                       break;
+
                default:
                        assert( 0 );
                        rc = 1;
@@ -1970,8 +2148,50 @@ rwm_cf_gen( ConfigArgs *c )
                switch ( c->type ) {
                case RWM_CF_REWRITE:
                        if ( c->valx >= 0 ) {
-                               /* single modification is not allowed */
-                               rc = 1;
+                               int i;
+
+                               for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
+                                       /* count'em */ ;
+
+                               if ( c->valx >= i ) {
+                                       rc = 1;
+                                       break;
+                               }
+
+                               ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
+                               for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
+                               {
+                                       rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
+                               }
+                               BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
+
+                               rewrite_info_delete( &rwmap->rwm_rw );
+                               assert( rwmap->rwm_rw == NULL );
+
+                               rc = rwm_info_init( &rwmap->rwm_rw );
+
+                               for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
+                               {
+                                       ConfigArgs ca = { 0 };
+
+                                       ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
+                                       ca.argc = 0;
+                                       config_fp_parse_line( &ca );
+                                       
+                                       if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
+                                               rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
+                                                       ca.argc, ca.argv );
+
+                                       } else {
+                                               rc = rwm_rw_config( &db, c->fname, c->lineno,
+                                                       ca.argc, ca.argv );
+                                       }
+
+                                       ch_free( ca.tline );
+                                       ch_free( ca.argv );
+
+                                       assert( rc == 0 );
+                               }
 
                        } else if ( rwmap->rwm_rw != NULL ) {
                                rewrite_info_delete( &rwmap->rwm_rw );
@@ -1979,6 +2199,8 @@ rwm_cf_gen( ConfigArgs *c )
 
                                ber_bvarray_free( rwmap->rwm_bva_rewrite );
                                rwmap->rwm_bva_rewrite = NULL;
+
+                               rc = rwm_info_init( &rwmap->rwm_rw );
                        }
                        break;
 
@@ -1988,8 +2210,74 @@ rwm_cf_gen( ConfigArgs *c )
 
                case RWM_CF_MAP:
                        if ( c->valx >= 0 ) {
-                               /* single modification is not allowed */
-                               rc = 1;
+                               struct ldapmap rwm_oc = rwmap->rwm_oc;
+                               struct ldapmap rwm_at = rwmap->rwm_at;
+                               char *argv[5];
+                               int cnt = 0;
+
+                               if ( rwmap->rwm_bva_map ) {
+                                       for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
+                                               /* count */ ;
+                               }
+
+                               if ( c->valx >= cnt ) {
+                                       rc = 1;
+                                       break;
+                               }
+
+                               memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
+                               memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
+
+                               /* re-parse all mappings except the one
+                                * that needs to be eliminated */
+                               argv[0] = "map";
+                               for ( cnt = 0; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
+                                       ConfigArgs ca = { 0 };
+
+                                       if ( cnt == c->valx ) {
+                                               continue;
+                                       }
+
+                                       ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
+                                       ca.argc = 0;
+                                       config_fp_parse_line( &ca );
+                                       
+                                       argv[1] = ca.argv[0];
+                                       argv[2] = ca.argv[1];
+                                       argv[3] = ca.argv[2];
+                                       argv[4] = ca.argv[3];
+                       
+                                       rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
+
+                                       ch_free( ca.tline );
+                                       ch_free( ca.argv );
+
+                                       /* in case of failure, restore
+                                        * the existing mapping */
+                                       if ( rc ) {
+                                               avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
+                                               avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
+                                               avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
+                                               avl_free( rwmap->rwm_at.map, rwm_mapping_free );
+                                               rwmap->rwm_oc = rwm_oc;
+                                               rwmap->rwm_at = rwm_at;
+                                               break;
+                                       }
+                               }
+
+                               /* in case of success, destroy the old mapping
+                                * and eliminate the deleted one */
+                               if ( rc == 0 ) {
+                                       avl_free( rwm_oc.remap, rwm_mapping_dst_free );
+                                       avl_free( rwm_oc.map, rwm_mapping_free );
+                                       avl_free( rwm_at.remap, rwm_mapping_dst_free );
+                                       avl_free( rwm_at.map, rwm_mapping_free );
+
+                                       ber_memfree( rwmap->rwm_bva_map[ c->valx ].bv_val );
+                                       for ( cnt = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
+                                               rwmap->rwm_bva_map[ cnt ] = rwmap->rwm_bva_map[ cnt + 1 ];
+                                       }
+                               }
 
                        } else {
                                avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
@@ -2011,65 +2299,149 @@ rwm_cf_gen( ConfigArgs *c )
                        rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
                        break;
 
+               case RWM_CF_DROP_UNREQUESTED:
+                       rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
+                       break;
+
                default:
                        return 1;
                }
                return rc;
        }
 
+       if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
+               idx0 = 1;
+       }
+
        switch ( c->type ) {
        case RWM_CF_REWRITE:
-               argv0 = c->argv[ 0 ];
-               c->argv[ 0 ] += STRLENOF( "rwm-" );
-               rc = rwm_rw_config( &db, c->fname, c->lineno, c->argc, c->argv );
-               c->argv[ 0 ] = argv0;
-               if ( rc ) {
-                       return 1;
+               if ( c->valx >= 0 ) {
+                       struct rewrite_info *rwm_rw = rwmap->rwm_rw;
+                       int i, last;
 
-               } else {
-                       char            *line;
-                       struct berval   bv;
+                       for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
+                               /* count'em */ ;
 
-                       line = ldap_charray2str( c->argv, "\" \"" );
-                       if ( line != NULL ) {
-                               int     len = strlen( c->argv[ 0 ] );
+                       if ( c->valx > last ) {
+                               c->valx = last;
+                       }
 
-                               ber_str2bv( line, 0, 0, &bv );
-                               AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
-                                       bv.bv_len - ( len + 1 ) );
-                               bv.bv_val[ bv.bv_len - 1 ] = '"';
-                               ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
+                       rwmap->rwm_rw = NULL;
+                       rc = rwm_info_init( &rwmap->rwm_rw );
+
+                       for ( i = 0; i < c->valx; i++ ) {
+                               ConfigArgs ca = { 0 };
+
+                               ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
+                               ca.argc = 0;
+                               config_fp_parse_line( &ca );
+
+                               argv0 = ca.argv[ 0 ];
+                               ca.argv[ 0 ] += STRLENOF( "rwm-" );
+                               
+                               if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
+                                       rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
+                                               ca.argc, ca.argv );
+
+                               } else {
+                                       rc = rwm_rw_config( &db, c->fname, c->lineno,
+                                               ca.argc, ca.argv );
+                               }
+
+                               ca.argv[ 0 ] = argv0;
+
+                               ch_free( ca.tline );
+                               ch_free( ca.argv );
+
+                               assert( rc == 0 );
                        }
-               }
-               break;
 
-       case RWM_CF_SUFFIXMASSAGE:
-               argv0 = c->argv[ 0 ];
-               c->argv[ 0 ] += STRLENOF( "rwm-" );
-               rc = rwm_suffixmassage_config( &db, c->fname, c->lineno, c->argc, c->argv );
-               c->argv[ 0 ] = argv0;
-               if ( rc ) {
-                       return 1;
+                       argv0 = c->argv[ idx0 ];
+                       if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
+                               return 1;
+                       }
+                       c->argv[ idx0 ] += STRLENOF( "rwm-" );
+                       if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
+                               rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
+                                       c->argc - idx0, &c->argv[ idx0 ] );
 
-               } else {
-                       char            *line;
-                       struct berval   bv;
+                       } else {
+                               rc = rwm_rw_config( &db, c->fname, c->lineno,
+                                       c->argc - idx0, &c->argv[ idx0 ] );
+                       }
+                       c->argv[ idx0 ] = argv0;
+                       if ( rc != 0 ) {
+                               rewrite_info_delete( &rwmap->rwm_rw );
+                               assert( rwmap->rwm_rw == NULL );
 
-                       /* FIXME: not optimal; in fact, this keeps track
-                        * of the fact that a set of rules was added
-                        * using the rwm-suffixmassage shortcut, but the
-                        * rules are not clarified */
+                               rwmap->rwm_rw = rwm_rw;
+                               return 1;
+                       }
 
-                       line = ldap_charray2str( c->argv, "\" \"" );
-                       if ( line != NULL ) {
-                               int     len = strlen( c->argv[ 0 ] );
+                       for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
+                       {
+                               ConfigArgs ca = { 0 };
+
+                               ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
+                               ca.argc = 0;
+                               config_fp_parse_line( &ca );
+                               
+                               argv0 = ca.argv[ 0 ];
+                               ca.argv[ 0 ] += STRLENOF( "rwm-" );
+                               
+                               if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
+                                       rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
+                                               ca.argc, ca.argv );
 
-                               ber_str2bv( line, 0, 0, &bv );
-                               AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
-                                       bv.bv_len - ( len + 1 ) );
-                               bv.bv_val[ bv.bv_len - 1 ] = '"';
-                               ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
+                               } else {
+                                       rc = rwm_rw_config( &db, c->fname, c->lineno,
+                                               ca.argc, ca.argv );
+                               }
+
+                               ca.argv[ 0 ] = argv0;
+
+                               ch_free( ca.tline );
+                               ch_free( ca.argv );
+
+                               assert( rc == 0 );
+                       }
+
+                       rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
+                               ( last + 2 )*sizeof( struct berval ) );
+                       BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
+
+                       for ( i = last - 1; i >= c->valx; i-- )
+                       {
+                               rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
                        }
+
+                       rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
+
+                       rewrite_info_delete( &rwm_rw );
+                       assert( rwm_rw == NULL );
+
+                       break;
+               }
+
+               argv0 = c->argv[ idx0 ];
+               if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
+                       return 1;
+               }
+               c->argv[ idx0 ] += STRLENOF( "rwm-" );
+               if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
+                       rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
+                               c->argc - idx0, &c->argv[ idx0 ] );
+
+               } else {
+                       rc = rwm_rw_config( &db, c->fname, c->lineno,
+                               c->argc - idx0, &c->argv[ idx0 ] );
+               }
+               c->argv[ idx0 ] = argv0;
+               if ( rc ) {
+                       return 1;
+
+               } else {
+                       rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
                }
                break;
 
@@ -2085,6 +2457,129 @@ rwm_cf_gen( ConfigArgs *c )
                break;
 
        case RWM_CF_MAP:
+               if ( c->valx >= 0 ) {
+                       struct ldapmap rwm_oc = rwmap->rwm_oc;
+                       struct ldapmap rwm_at = rwmap->rwm_at;
+                       char *argv[5];
+                       int cnt = 0;
+
+                       if ( rwmap->rwm_bva_map ) {
+                               for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
+                                       /* count */ ;
+                       }
+
+                       if ( c->valx >= cnt ) {
+                               c->valx = cnt;
+                       }
+
+                       memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
+                       memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
+
+                       /* re-parse all mappings, including the one
+                        * that needs to be added */
+                       argv[0] = "map";
+                       for ( cnt = 0; cnt < c->valx; cnt++ ) {
+                               ConfigArgs ca = { 0 };
+
+                               ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
+                               ca.argc = 0;
+                               config_fp_parse_line( &ca );
+
+                               argv[1] = ca.argv[0];
+                               argv[2] = ca.argv[1];
+                               argv[3] = ca.argv[2];
+                               argv[4] = ca.argv[3];
+                       
+                               rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
+
+                               ch_free( ca.tline );
+                               ch_free( ca.argv );
+
+                               /* in case of failure, restore
+                                * the existing mapping */
+                               if ( rc ) {
+                                       goto rwmmap_fail;
+                               }
+                       }
+
+                       argv0 = c->argv[0];
+                       c->argv[0] = "map";
+                       rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
+                       c->argv[0] = argv0;
+                       if ( rc ) {
+                               goto rwmmap_fail;
+                       }
+
+                       if ( rwmap->rwm_bva_map ) {
+                               for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
+                                       ConfigArgs ca = { 0 };
+
+                                       ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
+                                       ca.argc = 0;
+                                       config_fp_parse_line( &ca );
+                       
+                                       argv[1] = ca.argv[0];
+                                       argv[2] = ca.argv[1];
+                                       argv[3] = ca.argv[2];
+                                       argv[4] = ca.argv[3];
+                       
+                                       rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
+
+                                       ch_free( ca.tline );
+                                       ch_free( ca.argv );
+
+                                       /* in case of failure, restore
+                                        * the existing mapping */
+                                       if ( rc ) {
+                                               goto rwmmap_fail;
+                                       }
+                               }
+                       }
+
+                       /* in case of success, destroy the old mapping
+                        * and add the new one */
+                       if ( rc == 0 ) {
+                               BerVarray tmp;
+                               struct berval bv, *bvp = &bv;
+
+                               if ( rwm_bva_add( &bvp, 0, &c->argv[ idx0 ] ) ) {
+                                       rc = 1;
+                                       goto rwmmap_fail;
+                               }
+                                       
+                               tmp = ber_memrealloc( rwmap->rwm_bva_map,
+                                       sizeof( struct berval )*( cnt + 2 ) );
+                               if ( tmp == NULL ) {
+                                       ber_memfree( bv.bv_val );
+                                       rc = 1;
+                                       goto rwmmap_fail;
+                               }
+                               rwmap->rwm_bva_map = tmp;
+                               BER_BVZERO( &rwmap->rwm_bva_map[ cnt + 1 ] );
+
+                               avl_free( rwm_oc.remap, rwm_mapping_dst_free );
+                               avl_free( rwm_oc.map, rwm_mapping_free );
+                               avl_free( rwm_at.remap, rwm_mapping_dst_free );
+                               avl_free( rwm_at.map, rwm_mapping_free );
+
+                               for ( ; cnt-- > c->valx; ) {
+                                       rwmap->rwm_bva_map[ cnt + 1 ] = rwmap->rwm_bva_map[ cnt ];
+                               }
+                               rwmap->rwm_bva_map[ c->valx ] = bv;
+
+                       } else {
+rwmmap_fail:;
+                               avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
+                               avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
+                               avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
+                               avl_free( rwmap->rwm_at.map, rwm_mapping_free );
+                               rwmap->rwm_oc = rwm_oc;
+                               rwmap->rwm_at = rwm_at;
+                       }
+
+                       break;
+               }
+
                argv0 = c->argv[ 0 ];
                c->argv[ 0 ] += STRLENOF( "rwm-" );
                rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
@@ -2112,6 +2607,14 @@ rwm_cf_gen( ConfigArgs *c )
                }
                break;
 
+       case RWM_CF_DROP_UNREQUESTED:
+               if ( c->value_int ) {
+                       rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
+               } else {
+                       rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
+               }
+               break;
+
        default:
                assert( 0 );
                return 1;
@@ -2127,30 +2630,15 @@ rwm_db_init(
 {
        slap_overinst           *on = (slap_overinst *) be->bd_info;
        struct ldaprwmap        *rwmap;
-       char                    *rargv[ 3 ];
        int                     rc = 0;
 
        rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
 
-       rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
-       if ( rwmap->rwm_rw == NULL ) {
-               rc = -1;
-               goto error_return;
-       }
-
-       /* this rewriteContext by default must be null;
-        * rules can be added if required */
-       rargv[ 0 ] = "rewriteContext";
-       rargv[ 1 ] = "searchFilter";
-       rargv[ 2 ] = NULL;
-       rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
+       /* default */
+       rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
 
-       rargv[ 0 ] = "rewriteContext";
-       rargv[ 1 ] = "default";
-       rargv[ 2 ] = NULL;
-       rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
+       rc = rwm_info_init( &rwmap->rwm_rw );
 
-error_return:;
        on->on_bi.bi_private = (void *)rwmap;
 
        if ( rc ) {
@@ -2174,7 +2662,8 @@ rwm_db_destroy(
 
                if ( rwmap->rwm_rw ) {
                        rewrite_info_delete( &rwmap->rwm_rw );
-                       ber_bvarray_free( rwmap->rwm_bva_rewrite );
+                       if ( rwmap->rwm_bva_rewrite )
+                               ber_bvarray_free( rwmap->rwm_bva_rewrite );
                }
 
                avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );