]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/rwm.c
Sync with HEAD
[openldap] / servers / slapd / overlays / rwm.c
index de376d406a94b3da6cb7b170b6a5f88635258896..85d73d2ad07e030d694d63944a1913689a5f2595 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2004 The OpenLDAP Foundation.
+ * Copyright 2003-2005 The OpenLDAP Foundation.
  * Portions Copyright 2003 Pierangelo Masarati.
  * All rights reserved.
  *
@@ -39,94 +39,180 @@ rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
        dncookie                dc;
 
        /*
-        * Rewrite the bind dn if needed
+        * Rewrite the dn if needed
         */
        dc.rwmap = rwmap;
 #ifdef ENABLE_REWRITE
        dc.conn = op->o_conn;
        dc.rs = rs;
        dc.ctx = (char *)cookie;
-#else
+#else /* ! ENABLE_REWRITE */
        dc.tofrom = ((int *)cookie)[0];
        dc.normalized = 0;
-#endif
+#endif /* ! ENABLE_REWRITE */
+
+       /* NOTE: in those cases where only the ndn is available,
+        * and the caller sets op->o_req_dn = op->o_req_ndn,
+        * only rewrite the op->o_req_ndn and use it as 
+        * op->o_req_dn as well */
+       ndn = op->o_req_ndn;
+       if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
+               dn = op->o_req_dn;
+               rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
+       } else {
+               rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
+       }
 
-       rc = rwm_dn_massage( &dc, &op->o_req_dn, &dn, &ndn );
        if ( rc != LDAP_SUCCESS ) {
                return rc;
        }
 
-       if ( dn.bv_val == op->o_req_dn.bv_val ) {
+       if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
+                       || ndn.bv_val == op->o_req_ndn.bv_val )
+       {
                return LDAP_SUCCESS;
        }
 
-       op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
+       if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
+               op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
+               op->o_req_dn = dn;
+       } else {
+               op->o_req_dn = ndn;
+       }
        op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
-
-       op->o_req_dn = dn;
        op->o_req_ndn = ndn;
 
        return LDAP_SUCCESS;
 }
 
 static int
-rwm_add( Operation *op, SlapReply *rs )
+rwm_op_add( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
        int                     rc,
-                               i,
-                               isupdate;
+                               i;
        Attribute               **ap = NULL;
+       char                    *olddn = op->o_req_dn.bv_val;
+       int                     isupdate;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "addDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "addDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "addDn massage error" );
+               send_ldap_error( op, rs, rc, "addDN massage error" );
                return -1;
        }
 
+       if ( olddn != op->o_req_dn.bv_val ) {
+               ch_free( op->ora_e->e_name.bv_val );
+               ch_free( op->ora_e->e_nname.bv_val );
+
+               ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
+               ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
+       }
+
        /* Count number of attributes in entry */ 
        isupdate = be_shadow_update( op );
        for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
-               struct berval   mapped;
                Attribute       *a;
 
-               if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
-                       goto cleanup_attr;
-               }
+               if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
+                               (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
+               {
+                       int             j, last;
 
-               rwm_map( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
-                               &mapped, RWM_MAP );
-               if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
-                       goto cleanup_attr;
-               }
+                       for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
+                                       /* count values */ ;
+                       last--;
+                       for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
+                               struct ldapmapping      *mapping = NULL;
+
+                               ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
+                                               &mapping, RWM_MAP );
+                               if ( mapping == NULL ) {
+                                       if ( rwmap->rwm_at.drop_missing ) {
+                                               /* FIXME: we allow to remove objectClasses as well;
+                                                * if the resulting entry is inconsistent, that's
+                                                * the relayed database's business...
+                                                */
+                                               ch_free( (*ap)->a_vals[ j ].bv_val );
+                                               if ( last > j ) {
+                                                       (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
+                                               }
+                                               BER_BVZERO( &(*ap)->a_vals[ last ] );
+                                               last--;
+                                               j--;
+                                       }
 
-               if ( (*ap)->a_desc->ad_type->sat_syntax
-                               == slap_schema.si_syn_distinguishedName )
-               {
-                       /*
-                        * FIXME: rewrite could fail; in this case
-                        * the operation should give up, right?
-                        */
+                               } else {
+                                       ch_free( (*ap)->a_vals[ j ].bv_val );
+                                       ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
+                               }
+                       }
+
+               } else if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
+                       goto next_attr;
+
+               } else {
+                       struct ldapmapping      *mapping = NULL;
+
+                       ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
+                                       &mapping, RWM_MAP );
+                       if ( mapping == NULL ) {
+                               if ( rwmap->rwm_at.drop_missing ) {
+                                       goto cleanup_attr;
+                               }
+                       }
+
+                       if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
+                                       || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
+                       {
+                               /*
+                                * FIXME: rewrite could fail; in this case
+                                * the operation should give up, right?
+                                */
 #ifdef ENABLE_REWRITE
-                       rc = rwm_dnattr_rewrite( op, rs, "addDn", (*ap)->a_vals, NULL );
-#else
-                       rc = 1;
-                       rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals, NULL );
-#endif
-                       if ( rc ) {
-                               goto cleanup_attr;
+                               rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
+                                               (*ap)->a_vals,
+                                               (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
+#else /* ! ENABLE_REWRITE */
+                               rc = 1;
+                               rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
+                                               (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
+#endif /* ! ENABLE_REWRITE */
+                               if ( rc ) {
+                                       goto cleanup_attr;
+                               }
+
+                       } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
+#ifdef ENABLE_REWRITE
+                               rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
+                                               (*ap)->a_vals,
+                                               (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
+#else /* ! ENABLE_REWRITE */
+                               rc = 1;
+                               rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
+                                               (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
+#endif /* ! ENABLE_REWRITE */
+                               if ( rc != LDAP_SUCCESS ) {
+                                       goto cleanup_attr;
+                               }
+                       }
+               
+                       if ( mapping != NULL ) {
+                               assert( mapping->m_dst_ad );
+                               (*ap)->a_desc = mapping->m_dst_ad;
                        }
                }
 
+next_attr:;
                ap = &(*ap)->a_next;
                continue;
 
@@ -143,7 +229,7 @@ cleanup_attr:;
 }
 
 static int
-rwm_bind( Operation *op, SlapReply *rs )
+rwm_op_bind( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -154,14 +240,14 @@ rwm_bind( Operation *op, SlapReply *rs )
        ( void )rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
        ( void )rewrite_session_init( rwmap->rwm_rw, op->o_conn );
 
-       rc = rwm_op_dn_massage( op, rs, "bindDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "bindDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "bindDn massage error" );
+               send_ldap_error( op, rs, rc, "bindDN massage error" );
                return -1;
        }
 
@@ -169,7 +255,7 @@ rwm_bind( Operation *op, SlapReply *rs )
 }
 
 static int
-rwm_unbind( Operation *op, SlapReply *rs )
+rwm_op_unbind( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -177,13 +263,13 @@ rwm_unbind( Operation *op, SlapReply *rs )
 
 #ifdef ENABLE_REWRITE
        rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
-#endif
+#endif /* ENABLE_REWRITE */
 
        return SLAP_CB_CONTINUE;
 }
 
 static int
-rwm_compare( Operation *op, SlapReply *rs )
+rwm_op_compare( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -194,14 +280,14 @@ rwm_compare( Operation *op, SlapReply *rs )
                                mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "compareDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "compareDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "compareDn massage error" );
+               send_ldap_error( op, rs, rc, "compareDN massage error" );
                return -1;
        }
 
@@ -224,24 +310,39 @@ rwm_compare( Operation *op, SlapReply *rs )
                mapped_at = op->orc_ava->aa_desc->ad_cname;
 
        } else {
-               rwm_map( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
-                               &mapped_at, RWM_MAP );
-               if ( BER_BVISNULL( &mapped_at ) || BER_BVISEMPTY( &mapped_at ) )
-               {
-                       op->o_bd->bd_info = (BackendInfo *)on->on_info;
-                       send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
-                       return -1;
+               struct ldapmapping      *mapping = NULL;
+               AttributeDescription    *ad = op->orc_ava->aa_desc;
+
+               ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
+                               &mapping, RWM_MAP );
+               if ( mapping == NULL ) {
+                       if ( rwmap->rwm_at.drop_missing ) {
+                               op->o_bd->bd_info = (BackendInfo *)on->on_info;
+                               send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
+                               return -1;
+                       }
+
+               } else {
+                       assert( mapping->m_dst_ad );
+                       ad = mapping->m_dst_ad;
                }
-               if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
+
+               if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
+                               || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
                {
+                       struct berval   *mapped_valsp[2];
+                       
+                       mapped_valsp[0] = &mapped_vals[0];
+                       mapped_valsp[1] = &mapped_vals[1];
+
                        mapped_vals[0] = op->orc_ava->aa_value;
 
 #ifdef ENABLE_REWRITE
-                       rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", mapped_vals, NULL );
-#else
+                       rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
+#else /* ! ENABLE_REWRITE */
                        rc = 1;
-                       rc = rwm_dnattr_rewrite( op, rs, &rc, mapped_vals, NULL );
-#endif
+                       rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
+#endif /* ! ENABLE_REWRITE */
 
                        if ( rc != LDAP_SUCCESS ) {
                                op->o_bd->bd_info = (BackendInfo *)on->on_info;
@@ -249,39 +350,37 @@ rwm_compare( Operation *op, SlapReply *rs )
                                return -1;
                        }
 
-                       if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
-                               free( op->orc_ava->aa_value.bv_val );
-                               op->orc_ava->aa_value = mapped_vals[0];
-                       }
+                       op->orc_ava->aa_value = mapped_vals[0];
                }
+               op->orc_ava->aa_desc = ad;
        }
 
        return SLAP_CB_CONTINUE;
 }
 
 static int
-rwm_delete( Operation *op, SlapReply *rs )
+rwm_op_delete( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        int                     rc;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "deleteDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "deleteDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "deleteDn massage error" );
-               return rc;
+               send_ldap_error( op, rs, rc, "deleteDN massage error" );
+               return -1;
        }
 
        return SLAP_CB_CONTINUE;
 }
 
 static int
-rwm_modify( Operation *op, SlapReply *rs )
+rwm_op_modify( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -292,57 +391,40 @@ rwm_modify( Operation *op, SlapReply *rs )
        int                     rc;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "modifyDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "modifyDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "modifyDn massage error" );
-               return rc;
+               send_ldap_error( op, rs, rc, "modifyDN massage error" );
+               return -1;
        }
 
        isupdate = be_shadow_update( op );
        for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
-               int             is_oc = 0;
-
-               if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod  ) {
-                       Modifications   *ml;
-
-                       ml = *mlp;
-                       *mlp = (*mlp)->sml_next;
-                       slap_mod_free( &ml->sml_mod, 0 );
-                       free( ml );
-
-                       continue;
-               }
+               int                     is_oc = 0;
+               Modifications           *ml;
+               struct ldapmapping      *mapping = NULL;
 
                if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass 
-                               || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
+                               || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
+               {
                        is_oc = 1;
 
+               } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod  ) {
+                       goto next_mod;
+
                } else {
-                       struct ldapmapping      *m;
                        int                     drop_missing;
 
-                       drop_missing = rwm_mapping( &rwmap->rwm_at, &(*mlp)->sml_desc->ad_cname, &m, RWM_MAP );
-                       if ( drop_missing || ( m != NULL && BER_BVISNULL( &m->m_dst ) ) )
+                       drop_missing = rwm_mapping( &rwmap->rwm_at,
+                                       &(*mlp)->sml_desc->ad_cname,
+                                       &mapping, RWM_MAP );
+                       if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
                        {
-                               Modifications   *ml;
-
-                               ml = *mlp;
-                               *mlp = (*mlp)->sml_next;
-                               slap_mod_free( &ml->sml_mod, 0 );
-                               free( ml );
-
-                               continue;
-                       }
-
-                       if ( m ) {
-                               /* use new attribute description */
-                               assert( m->m_dst_ad );
-                               (*mlp)->sml_desc = m->m_dst_ad;
+                               goto cleanup_mod;
                        }
                }
 
@@ -350,96 +432,154 @@ rwm_modify( Operation *op, SlapReply *rs )
                        if ( is_oc ) {
                                int     last, j;
 
-                               for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[last] ); last++ )
+                               for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
                                        /* count values */ ;
                                last--;
 
-                               for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[j] ); j++ ) {
-                                       struct berval   mapped = BER_BVNULL;
-
-                                       rwm_map( &rwmap->rwm_oc,
-                                                       &(*mlp)->sml_values[j],
-                                                       &mapped, RWM_MAP );
-                                       if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
-                                               /* FIXME: we allow to remove objectClasses as well;
-                                                * if the resulting entry is inconsistent, that's
-                                                * the relayed database's business...
-                                                */
-#if 0
-                                               Modifications   *ml;
-
-                                               ml = *mlp;
-                                               *mlp = (*mlp)->sml_next;
-                                               slap_mod_free( &ml->sml_mod, 0 );
-                                               free( ml );
-
-                                               continue;
-#endif
-                                               if ( last > j ) {
-                                                       (*mlp)->sml_values[j] = (*mlp)->sml_values[last];
-                                                       BER_BVZERO( &(*mlp)->sml_values[last] );
+                               for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
+                                       struct ldapmapping      *oc_mapping = NULL;
+               
+                                       ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
+                                                       &oc_mapping, RWM_MAP );
+                                       if ( oc_mapping == NULL ) {
+                                               if ( rwmap->rwm_at.drop_missing ) {
+                                                       /* FIXME: we allow to remove objectClasses as well;
+                                                        * if the resulting entry is inconsistent, that's
+                                                        * the relayed database's business...
+                                                        */
+                                                       ch_free( (*mlp)->sml_values[ j ].bv_val );
+                                                       if ( last > j ) {
+                                                               (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
+                                                       }
+                                                       BER_BVZERO( &(*mlp)->sml_values[ last ] );
+                                                       last--;
+                                                       j--;
                                                }
-                                               last--;
-
+       
                                        } else {
-                                               ch_free( (*mlp)->sml_values[j].bv_val );
-                                               ber_dupbv( &(*mlp)->sml_values[j], &mapped );
+                                               ch_free( (*mlp)->sml_values[ j ].bv_val );
+                                               ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
                                        }
                                }
 
                        } else {
-                               if ( (*mlp)->sml_desc->ad_type->sat_syntax ==
-                                               slap_schema.si_syn_distinguishedName )
+                               if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
+                                               || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
                                {
 #ifdef ENABLE_REWRITE
-                                       rc = rwm_dnattr_rewrite( op, rs, "modifyDn",
-                                                       (*mlp)->sml_values, &(*mlp)->sml_nvalues );
-#else
+                                       rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
+                                                       (*mlp)->sml_values,
+                                                       (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
+#else /* ! ENABLE_REWRITE */
                                        rc = 1;
                                        rc = rwm_dnattr_rewrite( op, rs, &rc, 
-                                                       (*mlp)->sml_values, &(*mlp)->sml_nvalues );
-#endif
+                                                       (*mlp)->sml_values,
+                                                       (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
+#endif /* ! ENABLE_REWRITE */
+
+                               } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
+#ifdef ENABLE_REWRITE
+                                       rc = rwm_referral_rewrite( op, rs,
+                                                       "referralAttrDN",
+                                                       (*mlp)->sml_values,
+                                                       (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
+#else /* ! ENABLE_REWRITE */
+                                       rc = 1;
+                                       rc = rwm_referral_rewrite( op, rs, &rc,
+                                                       (*mlp)->sml_values,
+                                                       (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
+#endif /* ! ENABLE_REWRITE */
+                                       if ( rc != LDAP_SUCCESS ) {
+                                               goto cleanup_mod;
+                                       }
                                }
 
                                if ( rc != LDAP_SUCCESS ) {
-                                       Modifications   *ml;
-
-                                       ml = *mlp;
-                                       *mlp = (*mlp)->sml_next;
-                                       slap_mod_free( &ml->sml_mod, 0 );
-                                       free( ml );
-
-                                       continue;
+                                       goto cleanup_mod;
                                }
                        }
                }
 
+next_mod:;
+               if ( mapping != NULL ) {
+                       /* use new attribute description */
+                       assert( mapping->m_dst_ad );
+                       (*mlp)->sml_desc = mapping->m_dst_ad;
+               }
+
                mlp = &(*mlp)->sml_next;
+               continue;
+
+cleanup_mod:;
+               ml = *mlp;
+               *mlp = (*mlp)->sml_next;
+               slap_mod_free( &ml->sml_mod, 0 );
+               free( ml );
        }
 
-       /* TODO: rewrite attribute types, values of DN-valued attributes ... */
        return SLAP_CB_CONTINUE;
 }
 
 static int
-rwm_modrdn( Operation *op, SlapReply *rs )
+rwm_op_modrdn( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
+       struct ldaprwmap        *rwmap = 
+                       (struct ldaprwmap *)on->on_bi.bi_private;
+       
        int                     rc;
 
+       if ( op->orr_newSup ) {
+               dncookie        dc;
+               struct berval   nnewSup = BER_BVNULL;
+               struct berval   newSup = BER_BVNULL;
+
+               /*
+                * Rewrite the new superior, if defined and required
+                */
+               dc.rwmap = rwmap;
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "renameDn" );
-#else
+               dc.conn = op->o_conn;
+               dc.rs = rs;
+               dc.ctx = "newSuperiorDN";
+#else /* ! ENABLE_REWRITE */
+               dc.tofrom = 0;
+               dc.normalized = 0;
+#endif /* ! ENABLE_REWRITE */
+               newSup = *op->orr_newSup;
+               nnewSup = *op->orr_nnewSup;
+               rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
+               if ( rc != LDAP_SUCCESS ) {
+                       op->o_bd->bd_info = (BackendInfo *)on->on_info;
+                       send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
+                       return -1;
+               }
+
+               if ( op->orr_newSup->bv_val != newSup.bv_val ) {
+                       op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
+                       op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
+                       *op->orr_newSup = newSup;
+                       *op->orr_nnewSup = nnewSup;
+               }
+       }
+
+       /*
+        * Rewrite the dn, if needed
+        */
+#ifdef ENABLE_REWRITE
+       rc = rwm_op_dn_massage( op, rs, "renameDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "renameDn massage error" );
-               return rc;
+               send_ldap_error( op, rs, rc, "renameDN massage error" );
+               return -1;
        }
 
-       /* TODO: rewrite attribute types, values of DN-valued attributes ... */
+       /* TODO: rewrite newRDN, attribute types, 
+        * values of DN-valued attributes ... */
        return SLAP_CB_CONTINUE;
 }
 
@@ -454,8 +594,20 @@ rwm_swap_attrs( Operation *op, SlapReply *rs )
        return SLAP_CB_CONTINUE;
 }
 
+static int rwm_freeself( Operation *op, SlapReply *rs )
+{
+       if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_RESULT ) {
+               assert( op->o_callback );
+
+               op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
+               op->o_callback = NULL;
+       }
+
+       return SLAP_CB_CONTINUE;
+}
+
 static int
-rwm_search( Operation *op, SlapReply *rs )
+rwm_op_search( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -473,28 +625,28 @@ rwm_search( Operation *op, SlapReply *rs )
        char                    *text = NULL;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "searchDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "searchDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
-               text = "searchDn massage error";
+               text = "searchDN massage error";
                goto error_return;
        }
 
        /*
-        * Rewrite the bind dn if needed
+        * Rewrite the dn if needed
         */
        dc.rwmap = rwmap;
 #ifdef ENABLE_REWRITE
        dc.conn = op->o_conn;
        dc.rs = rs;
        dc.ctx = "searchFilterAttrDN";
-#else
+#else /* ! ENABLE_REWRITE */
        dc.tofrom = 0;
        dc.normalized = 0;
-#endif
+#endif /* ! ENABLE_REWRITE */
 
        rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
        if ( rc != LDAP_SUCCESS ) {
@@ -535,14 +687,13 @@ rwm_search( Operation *op, SlapReply *rs )
        }
 
        cb->sc_response = rwm_swap_attrs;
-       cb->sc_cleanup = NULL;
+       cb->sc_cleanup = rwm_freeself;
        cb->sc_private = (void *)op->ors_attrs;
        cb->sc_next = op->o_callback;
 
        op->o_callback = cb;
        op->ors_attrs = an;
 
-       /* TODO: rewrite/map filter & attrs */
        return SLAP_CB_CONTINUE;
 
 error_return:;
@@ -561,7 +712,7 @@ error_return:;
        op->o_bd->bd_info = (BackendInfo *)on->on_info;
        send_ldap_error( op, rs, rc, text );
 
-       return 1;
+       return -1;
 
 }
 
@@ -572,19 +723,19 @@ rwm_extended( Operation *op, SlapReply *rs )
        int                     rc;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "extendedDn" );
-#else
+       rc = rwm_op_dn_massage( op, rs, "extendedDN" );
+#else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
-#endif
+#endif /* ! ENABLE_REWRITE */
        if ( rc != LDAP_SUCCESS ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
-               send_ldap_error( op, rs, rc, "extendedDn massage error" );
-               return rc;
+               send_ldap_error( op, rs, rc, "extendedDN massage error" );
+               return -1;
        }
 
        /* TODO: rewrite/map extended data ? ... */
-       return 0;
+       return SLAP_CB_CONTINUE;
 }
 
 static int
@@ -607,12 +758,13 @@ rwm_matched( Operation *op, SlapReply *rs )
        dc.conn = op->o_conn;
        dc.rs = rs;
        dc.ctx = "matchedDN";
-#else
+#else /* ! ENABLE_REWRITE */
        dc.tofrom = 0;
        dc.normalized = 0;
-#endif
+#endif /* ! ENABLE_REWRITE */
        ber_str2bv( rs->sr_matched, 0, 0, &dn );
-       rc = rwm_dn_massage( &dc, &dn, &mdn, NULL );
+       mdn = dn;
+       rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
        if ( rc != LDAP_SUCCESS ) {
                rs->sr_err = rc;
                rs->sr_text = "Rewrite error";
@@ -633,132 +785,90 @@ rwm_matched( Operation *op, SlapReply *rs )
 }
 
 static int
-rwm_send_entry( Operation *op, SlapReply *rs )
+rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
-       Entry           *e = NULL;
-       int             flags;
-       struct berval   dn = BER_BVNULL,
-                       ndn = BER_BVNULL;
-       dncookie        dc;
-       int             rc;
-       Attribute       **ap;
-
-       assert( rs->sr_entry );
-
-       e = rs->sr_entry;
+       dncookie                dc;
+       int                     rc;
+       Attribute               **ap;
+       int                     isupdate;
 
        /*
-        * Rewrite the dn of the result, if needed
+        * Rewrite the dn attrs, if needed
         */
        dc.rwmap = rwmap;
 #ifdef ENABLE_REWRITE
        dc.conn = op->o_conn;
        dc.rs = NULL; 
-       dc.ctx = "searchResult";
-#else
+#else /* ! ENABLE_REWRITE */
        dc.tofrom = 0;
        dc.normalized = 0;
-#endif
-
-       flags = rs->sr_flags;
-
-       if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
-               /* FIXME: all we need to duplicate are:
-                * - dn
-                * - ndn
-                * - 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_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
-       }
-
-       /*
-        * Note: this may fail if the target host(s) schema differs
-        * from the one known to the meta, and a DN with unknown
-        * attributes is returned.
-        */
-       rc = rwm_dn_massage( &dc, &e->e_name, &dn, &ndn );
-       if ( rc != LDAP_SUCCESS ) {
-               goto fail;
-       }
-
-       if ( e->e_name.bv_val != dn.bv_val ) {
-               free( e->e_name.bv_val );
-               free( e->e_nname.bv_val );
-
-               e->e_name = dn;
-               e->e_nname = ndn;
-       }
-
-       /* TODO: map entry attribute types, objectclasses 
-        * and dn-valued attribute values */
+#endif /* ! ENABLE_REWRITE */
 
        /* FIXME: the entries are in the remote mapping form;
         * so we need to select those attributes we are willing
         * to return, and remap them accordingly */
 
-       for ( ap = &e->e_attrs; *ap; ) {
-               struct ldapmapping      *m;
+       /* FIXME: in principle, one could map an attribute
+        * on top of another, which already exists.
+        * As such, in the end there might exist more than
+        * one instance of an attribute.
+        * We should at least check if this occurs, and issue
+        * an error (because multiple instances of attrs in 
+        * response are not valid), or merge the values (what
+        * about duplicate values?) */
+       isupdate = be_shadow_update( op );
+       for ( ap = a_first; *ap; ) {
+               struct ldapmapping      *mapping = NULL;
                int                     drop_missing;
                int                     last;
+               Attribute               *a;
 
-               if ( op->ors_attrs != NULL && !ad_inlist( (*ap)->a_desc, op->ors_attrs ) ) {
-                       Attribute       *a;
+               if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
+               {
+                       /* 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;
+                       }
 
-                       a = *ap;
-                       *ap = (*ap)->a_next;
+                       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;
+                       }
 
-                       attr_free( a );
-                       continue;
+                       if ( mapping != NULL ) {
+                               (*ap)->a_desc = mapping->m_dst_ad;
+                       }
                }
 
-               drop_missing = rwm_mapping( &rwmap->rwm_at,
-                               &(*ap)->a_desc->ad_cname, &m, RWM_REMAP );
-               if ( drop_missing || ( m != NULL && BER_BVISEMPTY( &m->m_dst ) ) ) {
-                       Attribute       *a;
-
-                       a = *ap;
-                       *ap = (*ap)->a_next;
-
-                       attr_free( a );
-                       continue;
+               if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
+                       if ( stripEntryDN ) {
+                               /* will be generated by frontend */
+                               goto cleanup_attr;
+                       }
+                       
+               } else if ( !isupdate
+                       && (*ap)->a_desc->ad_type->sat_no_user_mod 
+                       && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
+               {
+                       goto next_attr;
                }
 
-               /* no subschemaSubentry */
-               if ( (*ap)->a_desc == slap_schema.si_ad_subschemaSubentry ) {
-
-                       /* 
-                        * We eat target's subschemaSubentry because
-                        * a search for this value is likely not
-                        * to resolve to the appropriate backend;
-                        * later, the local subschemaSubentry is
-                        * added.
-                        */
-                       Attribute       *a;
-
-                       a = *ap;
-                       *ap = (*ap)->a_next;
-
-                       attr_free( a );
-                       continue;
-               }
-               
                for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
                        /* just count */ ;
 
                if ( last == 0 ) {
-                       /* empty? for now, we leave it in place */
+                       /* empty? leave it in place because of attrsonly and vlv */
                        goto next_attr;
                }
                last--;
@@ -788,7 +898,7 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                                         * the value is replaced by
                                         * ch_alloc'ed memory
                                         */
-                                       free( bv[0].bv_val );
+                                       ch_free( bv[0].bv_val );
                                        ber_dupbv( &bv[0], &mapped );
                                }
                        }
@@ -802,33 +912,132 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                 * http://www.OpenLDAP.org/faq/data/cache/452.html)
                 * The problem can be overcome by moving the dn-based
                 * ACLs to the target directory server, and letting
-                * everything pass thru the ldap backend.
-                */
-               } else if ( (*ap)->a_desc->ad_type->sat_syntax ==
-                               slap_schema.si_syn_distinguishedName )
+                * everything pass thru the ldap backend. */
+               /* FIXME: handle distinguishedName-like syntaxes, like
+                * nameAndOptionalUID */
+               } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
+                               || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
                {
+#ifdef ENABLE_REWRITE
+                       dc.ctx = "searchAttrDN";
+#endif /* ENABLE_REWRITE */
                        rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
                        if ( rc != LDAP_SUCCESS ) {
-                               Attribute       *a;
-
-                               a = *ap;
-                               *ap = (*ap)->a_next;
+                               goto cleanup_attr;
+                       }
 
-                               attr_free( a );
-                               continue;
+               } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
+#ifdef ENABLE_REWRITE
+                       dc.ctx = "searchAttrDN";
+#endif /* ENABLE_REWRITE */
+                       rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
+                       if ( rc != LDAP_SUCCESS ) {
+                               goto cleanup_attr;
                        }
                }
 
-               if ( m != NULL ) {
+               if ( mapping != NULL ) {
                        /* rewrite the attribute description */
-                       assert( m->m_dst_ad );
-                       (*ap)->a_desc = m->m_dst_ad;
+                       assert( mapping->m_dst_ad );
+                       (*ap)->a_desc = mapping->m_dst_ad;
                }
 
 next_attr:;
                ap = &(*ap)->a_next;
+               continue;
+
+cleanup_attr:;
+               a = *ap;
+               *ap = (*ap)->a_next;
+
+               attr_free( a );
+       }
+
+       return 0;
+}
+
+static int
+rwm_send_entry( Operation *op, SlapReply *rs )
+{
+       slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
+       struct ldaprwmap        *rwmap = 
+                       (struct ldaprwmap *)on->on_bi.bi_private;
+
+       Entry                   *e = NULL;
+       int                     flags;
+       struct berval           dn = BER_BVNULL,
+                               ndn = BER_BVNULL;
+       dncookie                dc;
+       int                     rc;
+
+       assert( rs->sr_entry );
+
+       /*
+        * Rewrite the dn of the result, if needed
+        */
+       dc.rwmap = rwmap;
+#ifdef ENABLE_REWRITE
+       dc.conn = op->o_conn;
+       dc.rs = NULL; 
+       dc.ctx = "searchEntryDN";
+#else /* ! ENABLE_REWRITE */
+       dc.tofrom = 0;
+       dc.normalized = 0;
+#endif /* ! ENABLE_REWRITE */
+
+       e = rs->sr_entry;
+       flags = rs->sr_flags;
+       if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
+               /* FIXME: all we need to duplicate are:
+                * - dn
+                * - ndn
+                * - 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_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
+       }
+
+       /*
+        * Note: this may fail if the target host(s) schema differs
+        * from the one known to the meta, and a DN with unknown
+        * attributes is returned.
+        */
+       dn = e->e_name;
+       ndn = e->e_nname;
+       rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
+       if ( rc != LDAP_SUCCESS ) {
+               rc = 1;
+               goto fail;
+       }
+
+       if ( e->e_name.bv_val != dn.bv_val ) {
+               ch_free( e->e_name.bv_val );
+               ch_free( e->e_nname.bv_val );
+
+               e->e_name = dn;
+               e->e_nname = ndn;
        }
 
+       /* TODO: map entry attribute types, objectclasses 
+        * and dn-valued attribute values */
+
+       /* FIXME: the entries are in the remote mapping form;
+        * so we need to select those attributes we are willing
+        * to return, and remap them accordingly */
+       (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
+
+#if 0
+       if ( rs->sr_operational_attrs ) {
+               (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
+       }
+#endif
 
        rs->sr_entry = e;
        rs->sr_flags = flags;
@@ -836,6 +1045,18 @@ next_attr:;
        return SLAP_CB_CONTINUE;
 
 fail:;
+       if ( e != NULL && e != rs->sr_entry ) {
+               if ( e->e_name.bv_val == dn.bv_val ) {
+                       BER_BVZERO( &e->e_name );
+               }
+
+               if ( e->e_nname.bv_val == ndn.bv_val ) {
+                       BER_BVZERO( &e->e_nname );
+               }
+
+               entry_free( e );
+       }
+
        if ( !BER_BVISNULL( &dn ) ) {
                ch_free( dn.bv_val );
        }
@@ -844,12 +1065,47 @@ fail:;
                ch_free( ndn.bv_val );
        }
 
-       if ( e != NULL && e != rs->sr_entry ) {
-               entry_free( e );
+       return rc;
+}
+
+static int
+rwm_operational( Operation *op, SlapReply *rs )
+{
+       /* FIXME: the entries are in the remote mapping form;
+        * so we need to select those attributes we are willing
+        * to return, and remap them accordingly */
+       if ( rs->sr_operational_attrs ) {
+               rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
        }
 
-       return rc;
+       return SLAP_CB_CONTINUE;
+}
+
+#if 0
+/* don't use this; it cannot be reverted, and leaves op->o_req_dn
+ * rewritten for subsequent operations; fine for plain suffixmassage,
+ * but destroys everything else */
+static int
+rwm_chk_referrals( Operation *op, SlapReply *rs )
+{
+       slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
+       int                     rc;
+
+#ifdef ENABLE_REWRITE
+       rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
+#else /* ! ENABLE_REWRITE */
+       rc = 1;
+       rc = rwm_op_dn_massage( op, rs, &rc );
+#endif /* ! ENABLE_REWRITE */
+       if ( rc != LDAP_SUCCESS ) {
+               op->o_bd->bd_info = (BackendInfo *)on->on_info;
+               send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
+               return -1;
+       }
+
+       return SLAP_CB_CONTINUE;
 }
+#endif
 
 static int
 rwm_rw_config(
@@ -889,38 +1145,54 @@ rwm_suffixmassage_config(
        struct ldaprwmap        *rwmap = 
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
-       struct berval   bvnc, nvnc, pvnc, brnc, nrnc, prnc;
+       struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
+       int                     massaged;
 #ifdef ENABLE_REWRITE
-       int             rc;
+       int                     rc;
 #endif /* ENABLE_REWRITE */
                
        /*
         * syntax:
         * 
-        *      suffixmassage <suffix> <massaged suffix>
+        *      suffixmassage [<suffix>] <massaged suffix>
         *
-        * the <suffix> field must be defined as a valid suffix
-        * (or suffixAlias?) for the current database;
+        * the [<suffix>] field must be defined as a valid suffix
+        * for the current database;
         * the <massaged suffix> shouldn't have already been
-        * defined as a valid suffix or suffixAlias for the 
-        * current server
+        * defined as a valid suffix for the current server
         */
-       if ( argc != 3 ) {
+       if ( argc == 2 ) {
+               if ( be->be_suffix == NULL ) {
+                       fprintf( stderr, "%s: line %d: "
+                                      " \"suffixMassage [<suffix>]"
+                                      " <massaged suffix>\" without "
+                                      "<suffix> part requires database "
+                                      "suffix be defined first.\n",
+                               fname, lineno );
+                       return 1;
+               }
+               bvnc = be->be_suffix[ 0 ];
+               massaged = 1;
+
+       } else if ( argc == 3 ) {
+               ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
+               massaged = 2;
+
+       } else  {
                fprintf( stderr, "%s: line %d: syntax is"
-                              " \"suffixMassage <suffix>"
+                              " \"suffixMassage [<suffix>]"
                               " <massaged suffix>\"\n",
                        fname, lineno );
                return 1;
        }
-               
-       ber_str2bv( argv[1], 0, 0, &bvnc );
+
        if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
                fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
                        fname, lineno, bvnc.bv_val );
                return 1;
        }
 
-       ber_str2bv( argv[2], 0, 0, &brnc );
+       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",
                                fname, lineno, brnc.bv_val );
@@ -976,6 +1248,10 @@ rwm_m_config(
 static int
 rwm_response( Operation *op, SlapReply *rs )
 {
+       slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
+       struct ldaprwmap        *rwmap = 
+                       (struct ldaprwmap *)on->on_bi.bi_private;
+
        int             rc;
 
        if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
@@ -985,7 +1261,9 @@ rwm_response( Operation *op, SlapReply *rs )
        switch( op->o_tag ) {
        case LDAP_REQ_SEARCH:
                /* Note: the operation attrs are remapped */
-               if ( op->ors_attrs != NULL && op->ors_attrs != rs->sr_attrs )
+               if ( rs->sr_type == REP_RESULT
+                               && op->ors_attrs != NULL
+                               && op->ors_attrs != rs->sr_attrs )
                {
                        ch_free( op->ors_attrs );
                        op->ors_attrs = rs->sr_attrs;
@@ -999,6 +1277,27 @@ rwm_response( Operation *op, SlapReply *rs )
        case LDAP_REQ_MODIFY:
        case LDAP_REQ_COMPARE:
        case LDAP_REQ_EXTENDED:
+               if ( rs->sr_ref ) {
+                       dncookie                dc;
+
+                       /*
+                        * Rewrite the dn of the referrals, if needed
+                        */
+                       dc.rwmap = rwmap;
+#ifdef ENABLE_REWRITE
+                       dc.conn = op->o_conn;
+                       dc.rs = NULL; 
+                       dc.ctx = "referralDN";
+#else /* ! ENABLE_REWRITE */
+                       dc.tofrom = 0;
+                       dc.normalized = 0;
+#endif /* ! ENABLE_REWRITE */
+                       rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
+                       if ( rc != LDAP_SUCCESS ) {
+                               rc = 1;
+                               break;
+                       }
+               }
                rc = rwm_matched( op, rs );
                break;
 
@@ -1011,7 +1310,7 @@ rwm_response( Operation *op, SlapReply *rs )
 }
 
 static int
-rwm_config(
+rwm_db_config(
     BackendDB  *be,
     const char *fname,
     int                lineno,
@@ -1019,6 +1318,10 @@ rwm_config(
     char       **argv
 )
 {
+       slap_overinst           *on = (slap_overinst *) be->bd_info;
+       struct ldaprwmap        *rwmap = 
+                       (struct ldaprwmap *)on->on_bi.bi_private;
+
        int             rc = 0;
        char            *argv0 = NULL;
 
@@ -1036,6 +1339,33 @@ rwm_config(
        } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
                rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
 
+       } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
+               if ( argc != 2 ) {
+                       fprintf( stderr,
+               "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
+                                       fname, lineno );
+                       return( 1 );
+               }
+
+               if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
+                       rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
+
+               } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
+                       rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
+
+#if 0
+               /* TODO: not implemented yet */
+               } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
+                       rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
+#endif
+
+               } else {
+                       fprintf( stderr,
+       "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
+                               fname, lineno, argv[ 1 ] );
+                       return 1;
+               }
+
        } else {
                rc = SLAP_CONF_UNKNOWN;
        }
@@ -1048,7 +1378,7 @@ rwm_config(
 }
 
 static int
-rwm_over_init(
+rwm_db_init(
        BackendDB *be
 )
 {
@@ -1096,7 +1426,7 @@ rwm_over_init(
 }
 
 static int
-rwm_destroy(
+rwm_db_destroy(
        BackendDB *be
 )
 {
@@ -1131,32 +1461,38 @@ static slap_overinst rwm = { { NULL } };
 int
 rwm_init(void)
 {
-       memset( &rwm, 0, sizeof(slap_overinst) );
+       memset( &rwm, 0, sizeof( slap_overinst ) );
 
        rwm.on_bi.bi_type = "rwm";
-       rwm.on_bi.bi_db_init = rwm_over_init;
-       rwm.on_bi.bi_db_config = rwm_config;
-       rwm.on_bi.bi_db_destroy = rwm_destroy;
-
-       rwm.on_bi.bi_op_bind = rwm_bind;
-       rwm.on_bi.bi_op_search = rwm_search;
-       rwm.on_bi.bi_op_compare = rwm_compare;
-       rwm.on_bi.bi_op_modify = rwm_modify;
-       rwm.on_bi.bi_op_modrdn = rwm_modrdn;
-       rwm.on_bi.bi_op_add = rwm_add;
-       rwm.on_bi.bi_op_delete = rwm_delete;
-       rwm.on_bi.bi_op_unbind = rwm_unbind;
+
+       rwm.on_bi.bi_db_init = rwm_db_init;
+       rwm.on_bi.bi_db_config = rwm_db_config;
+       rwm.on_bi.bi_db_destroy = rwm_db_destroy;
+
+       rwm.on_bi.bi_op_bind = rwm_op_bind;
+       rwm.on_bi.bi_op_search = rwm_op_search;
+       rwm.on_bi.bi_op_compare = rwm_op_compare;
+       rwm.on_bi.bi_op_modify = rwm_op_modify;
+       rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
+       rwm.on_bi.bi_op_add = rwm_op_add;
+       rwm.on_bi.bi_op_delete = rwm_op_delete;
+       rwm.on_bi.bi_op_unbind = rwm_op_unbind;
        rwm.on_bi.bi_extended = rwm_extended;
 
+       rwm.on_bi.bi_operational = rwm_operational;
+       rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
+
        rwm.on_response = rwm_response;
 
        return overlay_register( &rwm );
 }
 
 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
-int init_module(int argc, char *argv[]) {
+int
+init_module( int argc, char *argv[] )
+{
        return rwm_init();
 }
-#endif
+#endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
 
 #endif /* SLAPD_OVER_RWM */