]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/rwm.c
Omit logpurge attr if no purge time was configured
[openldap] / servers / slapd / overlays / rwm.c
index d9ca042554945b0fef7b05c07cfd9004f7c6080a..2e07c98104628baf49a59f04c70f7aebb79f7943 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2005 The OpenLDAP Foundation.
+ * Copyright 2003-2006 The OpenLDAP Foundation.
  * Portions Copyright 2003 Pierangelo Masarati.
  * All rights reserved.
  *
@@ -26,6 +26,9 @@
 #include "slap.h"
 #include "rwm.h"
 
+static int
+rwm_db_destroy( BackendDB *be );
+
 static int
 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
 {
@@ -73,14 +76,14 @@ rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
                return LDAP_SUCCESS;
        }
 
-       op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
-       op->o_req_ndn = ndn;
        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_ndn = ndn;
 
        return LDAP_SUCCESS;
 }
@@ -111,11 +114,8 @@ rwm_op_add( Operation *op, SlapReply *rs )
        }
 
        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 );
+               ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
+               ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
        }
 
        /* Count number of attributes in entry */ 
@@ -157,7 +157,8 @@ rwm_op_add( Operation *op, SlapReply *rs )
                                }
                        }
 
-               } else if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod ) {
+               } else if ( !isupdate && !get_manageDIT( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
+               {
                        goto next_attr;
 
                } else {
@@ -171,8 +172,8 @@ rwm_op_add( Operation *op, SlapReply *rs )
                                }
                        }
 
-                       if ( (*ap)->a_desc->ad_type->sat_syntax
-                                       == slap_schema.si_syn_distinguishedName )
+                       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
@@ -207,7 +208,7 @@ rwm_op_add( Operation *op, SlapReply *rs )
                        }
                
                        if ( mapping != NULL ) {
-                               assert( mapping->m_dst_ad );
+                               assert( mapping->m_dst_ad != NULL );
                                (*ap)->a_desc = mapping->m_dst_ad;
                        }
                }
@@ -228,18 +229,39 @@ cleanup_attr:;
        return SLAP_CB_CONTINUE;
 }
 
+#ifdef ENABLE_REWRITE
 static int
-rwm_op_bind( Operation *op, SlapReply *rs )
+rwm_conn_init( BackendDB *be, Connection *conn )
 {
-       slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
+       slap_overinst           *on = (slap_overinst *) be->bd_info;
+       struct ldaprwmap        *rwmap = 
+                       (struct ldaprwmap *)on->on_bi.bi_private;
+
+       ( void )rewrite_session_init( rwmap->rwm_rw, conn );
+
+       return SLAP_CB_CONTINUE;
+}
+
+static int
+rwm_conn_destroy( BackendDB *be, Connection *conn )
+{
+       slap_overinst           *on = (slap_overinst *) be->bd_info;
        struct ldaprwmap        *rwmap = 
                        (struct ldaprwmap *)on->on_bi.bi_private;
+
+       ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
+
+       return SLAP_CB_CONTINUE;
+}
+#endif /* ENABLE_REWRITE */
+
+static int
+rwm_op_bind( Operation *op, SlapReply *rs )
+{
+       slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        int                     rc;
 
 #ifdef ENABLE_REWRITE
-       ( 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 /* ! ENABLE_REWRITE */
        rc = 1;
@@ -304,8 +326,7 @@ rwm_op_compare( Operation *op, SlapReply *rs )
                        return -1;
 
                } else 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];
+                       ber_bvreplace_x( &op->orc_ava->aa_value, &mapped_vals[0], op->o_tmpmemctx );
                }
                mapped_at = op->orc_ava->aa_desc->ad_cname;
 
@@ -323,11 +344,12 @@ rwm_op_compare( Operation *op, SlapReply *rs )
                        }
 
                } else {
-                       assert( mapping->m_dst_ad );
+                       assert( mapping->m_dst_ad != NULL );
                        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];
                        
@@ -349,7 +371,14 @@ rwm_op_compare( Operation *op, SlapReply *rs )
                                return -1;
                        }
 
-                       op->orc_ava->aa_value = mapped_vals[0];
+                       if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
+                               /* NOTE: if we get here, rwm_dnattr_rewrite()
+                                * already freed the old value, so now 
+                                * it's invalid */
+                               ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
+                                               op->o_tmpmemctx );
+                               ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
+                       }
                }
                op->orc_ava->aa_desc = ad;
        }
@@ -412,7 +441,8 @@ rwm_op_modify( Operation *op, SlapReply *rs )
                {
                        is_oc = 1;
 
-               } else if ( !isupdate && (*mlp)->sml_desc->ad_type->sat_no_user_mod  ) {
+               } else if ( !isupdate && !get_manageDIT( op ) && (*mlp)->sml_desc->ad_type->sat_no_user_mod  )
+               {
                        goto next_mod;
 
                } else {
@@ -436,11 +466,11 @@ rwm_op_modify( Operation *op, SlapReply *rs )
                                last--;
 
                                for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
-                                       struct ldapmapping      *mapping = NULL;
-
+                                       struct ldapmapping      *oc_mapping = NULL;
+               
                                        ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
-                                                       &mapping, RWM_MAP );
-                                       if ( mapping == NULL ) {
+                                                       &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
@@ -457,13 +487,13 @@ rwm_op_modify( Operation *op, SlapReply *rs )
        
                                        } else {
                                                ch_free( (*mlp)->sml_values[ j ].bv_val );
-                                               ber_dupbv( &(*mlp)->sml_values[ j ], &mapping->m_dst );
+                                               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, "modifyAttrDN",
@@ -502,7 +532,7 @@ rwm_op_modify( Operation *op, SlapReply *rs )
 next_mod:;
                if ( mapping != NULL ) {
                        /* use new attribute description */
-                       assert( mapping->m_dst_ad );
+                       assert( mapping->m_dst_ad != NULL );
                        (*mlp)->sml_desc = mapping->m_dst_ad;
                }
 
@@ -516,7 +546,6 @@ cleanup_mod:;
                free( ml );
        }
 
-       /* TODO: rewrite attribute types, values of DN-valued attributes ... */
        return SLAP_CB_CONTINUE;
 }
 
@@ -583,6 +612,36 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
        return SLAP_CB_CONTINUE;
 }
 
+static slap_callback   rwm_cb;
+
+static void
+rwm_keyfree(
+       void            *key,
+       void            *data )
+{
+       ber_memfree_x( data, NULL );
+}
+
+static slap_callback *
+rwm_callback_get( Operation *op )
+{
+       void            *data = NULL;
+
+       if ( op->o_threadctx == NULL ) {
+               return &rwm_cb;
+       }
+
+       ldap_pvt_thread_pool_getkey( op->o_threadctx,
+                       rwm_keyfree, &data, NULL );
+       if ( data == NULL ) {
+               data = ch_calloc( sizeof( slap_callback ), 1 );
+               ldap_pvt_thread_pool_setkey( op->o_threadctx,
+                               rwm_keyfree, data, rwm_keyfree );
+       }
+
+       return (slap_callback *)data;
+}
+
 static int
 rwm_swap_attrs( Operation *op, SlapReply *rs )
 {
@@ -594,18 +653,6 @@ 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_op_search( Operation *op, SlapReply *rs )
 {
@@ -619,13 +666,16 @@ rwm_op_search( Operation *op, SlapReply *rs )
        struct berval           fstr = BER_BVNULL;
        Filter                  *f = NULL;
 
-       slap_callback           *cb;
+       slap_callback           *cb = NULL;
        AttributeName           *an = NULL;
 
        char                    *text = NULL;
 
 #ifdef ENABLE_REWRITE
-       rc = rwm_op_dn_massage( op, rs, "searchDN" );
+       rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
+               "searchFilter", op->ors_filterstr.bv_val );
+       if ( rc == LDAP_SUCCESS )
+               rc = rwm_op_dn_massage( op, rs, "searchDN" );
 #else /* ! ENABLE_REWRITE */
        rc = 1;
        rc = rwm_op_dn_massage( op, rs, &rc );
@@ -648,7 +698,7 @@ rwm_op_search( Operation *op, SlapReply *rs )
        dc.normalized = 0;
 #endif /* ! ENABLE_REWRITE */
 
-       rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
+       rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
        if ( rc != LDAP_SUCCESS ) {
                text = "searchFilter/searchFilterAttrDN massage error";
                goto error_return;
@@ -679,15 +729,10 @@ rwm_op_search( Operation *op, SlapReply *rs )
                goto error_return;
        }
 
-       cb = (slap_callback *) op->o_tmpcalloc( sizeof( slap_callback ),
-                       1, op->o_tmpmemctx );
-       if ( cb == NULL ) {
-               rc = LDAP_NO_MEMORY;
-               goto error_return;
-       }
+       cb = rwm_callback_get( op );
 
        cb->sc_response = rwm_swap_attrs;
-       cb->sc_cleanup = rwm_freeself;
+       cb->sc_cleanup = NULL;
        cb->sc_private = (void *)op->ors_attrs;
        cb->sc_next = op->o_callback;
 
@@ -716,12 +761,97 @@ error_return:;
 
 }
 
+static int
+rwm_exop_passwd( Operation *op, SlapReply *rs )
+{
+       slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
+       int                     rc;
+
+       struct berval   id = BER_BVNULL,
+                       pwold = BER_BVNULL,
+                       pwnew = BER_BVNULL;
+
+       if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
+               return LDAP_SUCCESS;
+       }
+
+       if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
+               rs->sr_err = LDAP_OTHER;
+               return rs->sr_err;
+       }
+
+       rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
+               &pwold, &pwnew, &rs->sr_text );
+       if ( rs->sr_err != LDAP_SUCCESS ) {
+               return rs->sr_err;
+       }
+
+       if ( !BER_BVISNULL( &id ) ) {
+               rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
+                               &op->o_req_ndn, op->o_tmpmemctx );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
+                       rs->sr_text = "Invalid DN";
+                       return rs->sr_err;
+               }
+
+       } else {
+               ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
+               ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
+       }
+
+#ifdef ENABLE_REWRITE
+       rc = rwm_op_dn_massage( op, rs, "extendedDN" );
+#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, "extendedDN massage error" );
+               return -1;
+       }
+
+       /* TODO: re-encode the request */
+
+       return SLAP_CB_CONTINUE;
+}
+
+static struct exop {
+       struct berval   oid;
+       BI_op_extended  *extended;
+} exop_table[] = {
+       { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     rwm_exop_passwd },
+       { BER_BVNULL, NULL }
+};
+
 static int
 rwm_extended( Operation *op, SlapReply *rs )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        int                     rc;
 
+       int     i;
+
+       for ( i = 0; exop_table[i].extended != NULL; i++ ) {
+               if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
+               {
+                       rc = exop_table[i].extended( op, rs );
+                       switch ( rc ) {
+                       case LDAP_SUCCESS:
+                               break;
+
+                       case SLAP_CB_CONTINUE:
+                       case SLAPD_ABANDON:
+                               return rc;
+
+                       default:
+                               send_ldap_result( op, rs );
+                               return rc;
+                       }
+                       break;
+               }
+       }
+
 #ifdef ENABLE_REWRITE
        rc = rwm_op_dn_massage( op, rs, "extendedDN" );
 #else /* ! ENABLE_REWRITE */
@@ -785,7 +915,7 @@ rwm_matched( Operation *op, SlapReply *rs )
 }
 
 static int
-rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
+rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
 {
        slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -822,7 +952,7 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
         * about duplicate values?) */
        isupdate = be_shadow_update( op );
        for ( ap = a_first; *ap; ) {
-               struct ldapmapping      *mapping;
+               struct ldapmapping      *mapping = NULL;
                int                     drop_missing;
                int                     last;
                Attribute               *a;
@@ -832,6 +962,13 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
                        /* 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;
+                       }
+
                        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 ) ) )
@@ -842,21 +979,17 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
                        if ( mapping != NULL ) {
                                (*ap)->a_desc = mapping->m_dst_ad;
                        }
-
-                       if ( op->ors_attrs != NULL && 
-                                       !SLAP_USERATTRS( rs->sr_attr_flags ) &&
-                                       !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
-                       {
-                               goto cleanup_attr;
-                       }
                }
 
                if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
-                       /* will be generated by frontend */
-                       goto cleanup_attr;
-               }
-
-               if ( !isupdate && (*ap)->a_desc->ad_type->sat_no_user_mod 
+                       if ( stripEntryDN ) {
+                               /* will be generated by frontend */
+                               goto cleanup_attr;
+                       }
+                       
+               } else if ( !isupdate
+                       && !get_manageDIT( op )
+                       && (*ap)->a_desc->ad_type->sat_no_user_mod 
                        && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
                {
                        goto next_attr;
@@ -866,7 +999,7 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
                        /* 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--;
@@ -896,8 +1029,7 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
                                         * the value is replaced by
                                         * ch_alloc'ed memory
                                         */
-                                       ch_free( bv[0].bv_val );
-                                       ber_dupbv( &bv[0], &mapped );
+                                       ber_bvreplace( &bv[0], &mapped );
                                }
                        }
 
@@ -913,8 +1045,8 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
                 * 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 )
+               } 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";
@@ -936,7 +1068,7 @@ rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first )
 
                if ( mapping != NULL ) {
                        /* rewrite the attribute description */
-                       assert( mapping->m_dst_ad );
+                       assert( mapping->m_dst_ad != NULL );
                        (*ap)->a_desc = mapping->m_dst_ad;
                }
 
@@ -962,13 +1094,13 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
        Entry                   *e = NULL;
-       int                     flags;
+       slap_mask_t             flags;
        struct berval           dn = BER_BVNULL,
                                ndn = BER_BVNULL;
        dncookie                dc;
        int                     rc;
 
-       assert( rs->sr_entry );
+       assert( rs->sr_entry != NULL );
 
        /*
         * Rewrite the dn of the result, if needed
@@ -999,6 +1131,7 @@ rwm_send_entry( Operation *op, SlapReply *rs )
                        goto fail;
                }
 
+               flags &= ~REP_ENTRY_MUSTRELEASE;
                flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
        }
 
@@ -1029,7 +1162,11 @@ rwm_send_entry( 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 */
-       (void)rwm_attrs( op, rs, &e->e_attrs );
+       (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
+
+       if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
+               be_entry_release_rw( op, rs->sr_entry, 0 );
+       }
 
        rs->sr_entry = e;
        rs->sr_flags = flags;
@@ -1037,6 +1174,18 @@ rwm_send_entry( Operation *op, SlapReply *rs )
        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 );
        }
@@ -1045,10 +1194,6 @@ fail:;
                ch_free( ndn.bv_val );
        }
 
-       if ( e != NULL && e != rs->sr_entry ) {
-               entry_free( e );
-       }
-
        return rc;
 }
 
@@ -1059,7 +1204,7 @@ rwm_operational( Operation *op, SlapReply *rs )
         * 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 );
+               rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
        }
 
        return SLAP_CB_CONTINUE;
@@ -1093,12 +1238,11 @@ rwm_chk_referrals( Operation *op, SlapReply *rs )
 
 static int
 rwm_rw_config(
-    BackendDB  *be,
-    const char *fname,
-    int                lineno,
-    int                argc,
-    char       **argv
-)
+       BackendDB       *be,
+       const char      *fname,
+       int             lineno,
+       int             argc,
+       char            **argv )
 {
 #ifdef ENABLE_REWRITE
        slap_overinst           *on = (slap_overinst *) be->bd_info;
@@ -1118,12 +1262,11 @@ rwm_rw_config(
 
 static int
 rwm_suffixmassage_config(
-    BackendDB  *be,
-    const char *fname,
-    int                lineno,
-    int                argc,
-    char       **argv
-)
+       BackendDB       *be,
+       const char      *fname,
+       int             lineno,
+       int             argc,
+       char            **argv )
 {
        slap_overinst           *on = (slap_overinst *) be->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -1212,12 +1355,11 @@ rwm_suffixmassage_config(
 
 static int
 rwm_m_config(
-    BackendDB  *be,
-    const char *fname,
-    int                lineno,
-    int                argc,
-    char       **argv
-)
+       BackendDB       *be,
+       const char      *fname,
+       int             lineno,
+       int             argc,
+       char            **argv )
 {
        slap_overinst           *on = (slap_overinst *) be->bd_info;
        struct ldaprwmap        *rwmap = 
@@ -1245,7 +1387,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;
@@ -1293,13 +1437,16 @@ rwm_response( Operation *op, SlapReply *rs )
 
 static int
 rwm_db_config(
-    BackendDB  *be,
-    const char *fname,
-    int                lineno,
-    int                argc,
-    char       **argv
-)
+       BackendDB       *be,
+       const char      *fname,
+       int             lineno,
+       int             argc,
+       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;
 
@@ -1317,6 +1464,38 @@ rwm_db_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;
+
+               /* TODO: not implemented yet */
+               } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
+                       fprintf( stderr,
+               "%s: line %d: \"discover\" not supported yet "
+               "in \"t-f-support {no|yes|discover}\".\n",
+                                       fname, lineno );
+                       return( 1 );
+#if 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;
        }
@@ -1330,56 +1509,58 @@ rwm_db_config(
 
 static int
 rwm_db_init(
-       BackendDB *be
-)
+       BackendDB       *be )
 {
        slap_overinst           *on = (slap_overinst *) be->bd_info;
        struct ldapmapping      *mapping = NULL;
        struct ldaprwmap        *rwmap;
+#ifdef ENABLE_REWRITE
+       char                    *rargv[ 3 ];
+#endif /* ENABLE_REWRITE */
+       int                     rc = 0;
 
-       rwmap = (struct ldaprwmap *)ch_malloc(sizeof(struct ldaprwmap));
-       memset(rwmap, 0, sizeof(struct ldaprwmap));
+       rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
 
 #ifdef ENABLE_REWRITE
        rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
        if ( rwmap->rwm_rw == NULL ) {
-               ch_free( rwmap );
-               return -1;
+               rc = -1;
+               goto error_return;
        }
 
-       {
-               char    *rargv[3];
-
-               /* 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 );
-
-               rargv[ 0 ] = "rewriteContext";
-               rargv[ 1 ] = "default";
-               rargv[ 2 ] = NULL;
-               rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
-       }
-       
+       /* 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 );
+
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "default";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
 #endif /* ENABLE_REWRITE */
 
        if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
                        rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
        {
-               return 1;
+               rc = 1;
+               goto error_return;
        }
 
+error_return:;
        on->on_bi.bi_private = (void *)rwmap;
 
-       return 0;
+       if ( rc ) {
+               (void)rwm_db_destroy( be );
+       }
+
+       return rc;
 }
 
 static int
 rwm_db_destroy(
-       BackendDB *be
-)
+       BackendDB       *be )
 {
        slap_overinst   *on = (slap_overinst *) be->bd_info;
        int             rc = 0;
@@ -1389,7 +1570,7 @@ rwm_db_destroy(
                        (struct ldaprwmap *)on->on_bi.bi_private;
 
 #ifdef ENABLE_REWRITE
-               if (rwmap->rwm_rw) {
+               if ( rwmap->rwm_rw ) {
                        rewrite_info_delete( &rwmap->rwm_rw );
                }
 #else /* !ENABLE_REWRITE */
@@ -1398,10 +1579,12 @@ rwm_db_destroy(
                }
 #endif /* !ENABLE_REWRITE */
 
-               avl_free( rwmap->rwm_oc.remap, NULL );
+               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, NULL );
+               avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
                avl_free( rwmap->rwm_at.map, rwm_mapping_free );
+
+               ch_free( rwmap );
        }
 
        return rc;
@@ -1409,12 +1592,18 @@ rwm_db_destroy(
 
 static slap_overinst rwm = { { NULL } };
 
+#if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
+static
+#endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
 int
-rwm_init(void)
+rwm_initialize( void )
 {
        memset( &rwm, 0, sizeof( slap_overinst ) );
 
        rwm.on_bi.bi_type = "rwm";
+       rwm.on_bi.bi_flags =
+               SLAPO_BFLAG_SINGLE |
+               0;
 
        rwm.on_bi.bi_db_init = rwm_db_init;
        rwm.on_bi.bi_db_config = rwm_db_config;
@@ -1433,6 +1622,11 @@ rwm_init(void)
        rwm.on_bi.bi_operational = rwm_operational;
        rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
 
+#ifdef ENABLE_REWRITE
+       rwm.on_bi.bi_connection_init = rwm_conn_init;
+       rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
+#endif /* ENABLE_REWRITE */
+
        rwm.on_response = rwm_response;
 
        return overlay_register( &rwm );
@@ -1442,7 +1636,7 @@ rwm_init(void)
 int
 init_module( int argc, char *argv[] )
 {
-       return rwm_init();
+       return rwm_initialize();
 }
 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */