]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldif/ldif.c
ITS#4627 fix tool_entry_next
[openldap] / servers / slapd / back-ldif / ldif.c
index c9f3fe1ca9185255ccf4d527f8bca95adebaf4d4..cdbb23be84f085c9e615ebabd0007005fe330402 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2005 The OpenLDAP Foundation.
+ * Copyright 2005-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -43,7 +43,7 @@ struct ldif_info {
        struct berval li_base_path;
        enumCookie li_tool_cookie;
        ID li_tool_current;
-       ldap_pvt_thread_mutex_t  li_mutex;
+       ldap_pvt_thread_rdwr_t  li_rdwr;
 };
 
 #ifdef _WIN32
@@ -82,17 +82,23 @@ static ConfigOCs ldifocs[] = {
 };
 
 static void
-dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
+dn2path(struct berval * dn, struct berval * suffixdn, struct berval * base_path,
        struct berval *res)
 {
        char *ptr, *sep, *end;
 
+       assert( dn != NULL );
+       assert( !BER_BVISNULL( dn ) );
+       assert( suffixdn != NULL );
+       assert( !BER_BVISNULL( suffixdn ) );
+       assert( dnIsSuffix( dn, suffixdn ) );
+
        res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
        res->bv_val = ch_malloc( res->bv_len + 1 );
        ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
        *ptr++ = LDAP_DIRSEP[0];
-       ptr = lutil_strcopy( ptr, rootdn->bv_val );
-       end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
+       ptr = lutil_strcopy( ptr, suffixdn->bv_val );
+       end = dn->bv_val + dn->bv_len - suffixdn->bv_len - 1;
        while ( end > dn->bv_val ) {
                for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
                *ptr++ = LDAP_DIRSEP[0];
@@ -101,14 +107,22 @@ dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
        }
        strcpy(ptr, LDIF);
 #if IX_FSL != IX_DNL
-       ptr = res->bv_val;
-       while( ptr=strchr(ptr, IX_DNL) ) {
-               *ptr++ = IX_FSL;
-               ptr = strchr(ptr, IX_DNR);
-               if ( ptr )
+       {
+               struct berval bv;
+               bv = *res;
+               while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
+                       *ptr++ = IX_FSL;
+                       assert( ( ptr - bv.bv_val ) <= bv.bv_len );
+                       bv.bv_len -= ( ptr - bv.bv_val );
+                       bv.bv_val = ptr;
+                       ptr = ber_bvchr( &bv, IX_DNR );
+                       if ( !ptr )
+                               break;
                        *ptr++ = IX_FSR;
-               else
-                       break;
+                       assert( ( ptr - bv.bv_val ) <= bv.bv_len );
+                       bv.bv_len -= ( ptr - bv.bv_val );
+                       bv.bv_val = ptr;
+               }
        }
 #endif
 }
@@ -150,7 +164,6 @@ static int spew_file(int fd, char * spew, int len) {
        while(len > 0) {
                writeres = write(fd, spew, len);
                if(writeres == -1) {
-                       perror("could not spew write");
                        return -1;
                }
                else {
@@ -161,21 +174,24 @@ static int spew_file(int fd, char * spew, int len) {
        return writeres;
 }
 
-static int spew_entry(Entry * e, struct berval * path) {
-       int rs;
+static int
+spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
+{
+       int rs, save_errno = 0;
        int openres;
-       int spew_res;
+       int res, spew_res;
        int entry_length;
        char * entry_as_string;
+       char tmpfname[] = "tmpXXXXXX";
 
-       openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
-       if(openres == -1) {
-               if(errno == ENOENT)
-                       rs = LDAP_NO_SUCH_OBJECT;
-               else
-                       rs = LDAP_UNWILLING_TO_PERFORM;
-       }
-       else {
+       openres = mkstemp( tmpfname );
+       if ( openres == -1 ) {
+               save_errno = errno;
+               rs = LDAP_UNWILLING_TO_PERFORM;
+               Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
+                       tmpfname, STRERROR( save_errno ), 0 );
+
+       else {
                struct berval rdn;
                int tmp;
 
@@ -188,6 +204,10 @@ static int spew_entry(Entry * e, struct berval * path) {
                        rdn.bv_len = tmp;
                }
 
+               if ( dolock ) {
+                       ldap_pvt_thread_mutex_lock(&entry2str_mutex);
+               }
+
                entry_as_string = entry2str(e, &entry_length);
 
                /* Restore full DN */
@@ -196,19 +216,56 @@ static int spew_entry(Entry * e, struct berval * path) {
                        e->e_name.bv_len = rdn.bv_len;
                }
 
-               if(entry_as_string == NULL) {
+               if ( entry_as_string == NULL ) {
                        rs = LDAP_UNWILLING_TO_PERFORM;
                        close(openres);
+
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
+                       }
+
+               } else {
+                       spew_res = spew_file( openres,
+                               entry_as_string, entry_length );
+                       if ( spew_res == -1 )
+                               save_errno = errno;
+
+                       if ( dolock ) {
+                               ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
+                       }
+
+                       res = close( openres );
+                       rs = LDAP_UNWILLING_TO_PERFORM;
+                       if ( res == -1 || spew_res == -1 ) {
+                               if ( save_errno == 0 )
+                                       save_errno = errno;
+                               Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
+                                       tmpfname, STRERROR( save_errno ), 0 );
+                       } else {
+                               res = rename( tmpfname, path->bv_val );
+                               if ( res == 0 ) {
+                                       rs = LDAP_SUCCESS;
+
+                               } else {
+                                       save_errno = errno;
+                                       switch ( save_errno ) {
+                                       case ENOENT:
+                                               rs = LDAP_NO_SUCH_OBJECT;
+                                               break;
+
+                                       default:
+                                               break;
+                                       }
+                               }
+                       }
                }
-               else {
-                       spew_res = spew_file(openres, entry_as_string, entry_length);
-                       close(openres);
-                       if(spew_res == -1)
-                               rs = LDAP_UNWILLING_TO_PERFORM;
-                       else
-                               rs = LDAP_SUCCESS;
-               }
+
+               if ( rs != LDAP_SUCCESS )
+                       unlink( tmpfname );
        }
+
+       if ( rs != LDAP_SUCCESS )
+               *save_errnop = save_errno;
        return rs;
 }
 
@@ -255,8 +312,9 @@ static Entry * get_entry(Operation *op, struct berval *base_path) {
        dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
        fd = open(path.bv_val, O_RDONLY);
        /* error opening file (mebbe should log error) */
-       if(fd == -1) {
-               perror("failed to open file");
+       if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
+               Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
+                       path.bv_val, STRERROR(errno), 0 );
        }
 
        if(path.bv_val != NULL)
@@ -297,8 +355,8 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
        fd = open( path->bv_val, O_RDONLY );
        if ( fd < 0 ) {
                Debug( LDAP_DEBUG_TRACE,
-                       "=> ldif_enum_tree: failed to open %s\n",
-                       path->bv_val, 0, 0 );
+                       "=> ldif_enum_tree: failed to open %s: %s\n",
+                       path->bv_val, STRERROR(errno), 0 );
                return LDAP_NO_SUCH_OBJECT;
        }
 
@@ -330,7 +388,7 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                                                        ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
 
                                ck->rs->sr_entry = e;
-                               rc = send_search_reference( ck->op, ck->rs ) < 0;
+                               rc = send_search_reference( ck->op, ck->rs );
                                ber_bvarray_free( ck->rs->sr_ref );
                                ber_bvarray_free( erefs );
                                ck->rs->sr_ref = NULL;
@@ -341,20 +399,20 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                                ck->rs->sr_entry = e;
                                ck->rs->sr_attrs = ck->op->ors_attrs;
                                ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
-                               rc = send_search_entry(ck->op, ck->rs) < 0;
+                               rc = send_search_entry(ck->op, ck->rs);
                                ck->rs->sr_entry = NULL;
                        }
                        fd = 1;
                        if ( rc )
-                               goto leave;
+                               goto done;
                } else {
                /* Queueing up for tool mode */
                        if(ck->entries == NULL) {
-                               ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
+                               ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
                                ck->elen = ENTRY_BUFF_INCREMENT;
                        }
                        if(ck->eind >= ck->elen) { /* grow entries if necessary */      
-                               ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
+                               ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
                                ck->elen *= 2;
                        }
 
@@ -378,11 +436,11 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                                /* it shouldn't be treated as an error
                                 * only if the directory doesn't exist */
                                rc = LDAP_BUSY;
-                               Debug( LDAP_DEBUG_TRACE,
+                               Debug( LDAP_DEBUG_ANY,
                                        "=> ldif_enum_tree: failed to opendir %s (%d)\n",
                                        path->bv_val, errno, 0 );
                        }
-                       goto leave;
+                       goto done;
                }
        
                while(1) {
@@ -402,11 +460,13 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                        bvl = ch_malloc( sizeof(bvlist) );
                        ber_dupbv( &bvl->bv, &fname );
                        BER_BVZERO( &bvl->num );
-                       itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
+                       itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
                        if ( itmp.bv_val ) {
                                char *ptr;
                                itmp.bv_val++;
-                               ptr = strchr( itmp.bv_val, IX_FSR );
+                               itmp.bv_len = bvl->bv.bv_len
+                                       - ( itmp.bv_val - bvl->bv.bv_val );
+                               ptr = ber_bvchr( &itmp, IX_FSR );
                                if ( ptr ) {
                                        itmp.bv_len = ptr - itmp.bv_val;
                                        ber_dupbv( &bvl->num, &itmp );
@@ -453,7 +513,7 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                        free(ptr);
                }
        }
-leave:
+done:
        if ( fd ) entry_free( e );
        return rc;
 }
@@ -463,14 +523,14 @@ enum_tree(
        enumCookie *ck
 )
 {
-       struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
+       struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
        struct berval path;
        struct berval pdn, pndn;
        int rc;
 
        dnParent( &ck->op->o_req_dn, &pdn );
        dnParent( &ck->op->o_req_ndn, &pndn );
-       dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
+       dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
        rc = r_enum_tree(ck, &path, &pdn, &pndn);
        ch_free( path.bv_val );
        return rc;
@@ -497,7 +557,8 @@ static int apply_modify_to_entry(Entry * entry,
                                SlapReply * rs)
 {
        char textbuf[SLAP_TEXT_BUFLEN];
-       int rc = LDAP_UNWILLING_TO_PERFORM;
+       int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
+       int is_oc = 0;
        Modification *mods = NULL;
 
        if (!acl_check_modlist(op, entry, modlist)) {
@@ -507,6 +568,9 @@ static int apply_modify_to_entry(Entry * entry,
        for (; modlist != NULL; modlist = modlist->sml_next) {
                mods = &modlist->sml_mod;
 
+               if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
+                       is_oc = 1;
+               }
                switch (mods->sm_op) {
                case LDAP_MOD_ADD:
                        rc = modify_add_values(entry, mods,
@@ -556,20 +620,21 @@ static int apply_modify_to_entry(Entry * entry,
        }
        
        if(rc == LDAP_SUCCESS) {
-               if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
+               if ( is_oc ) {
                        entry->e_ocflags = 0;
                }
                /* check that the entry still obeys the schema */
-               rc = entry_schema_check(op->o_bd, entry, NULL, 0,
+               rc = entry_schema_check( op, entry, NULL, 0, 0,
                          &rs->sr_text, textbuf, sizeof( textbuf ) );
        }
+
        return rc;
 }
 
 int
 ldif_back_referrals( Operation *op, SlapReply *rs )
 {
-       struct ldif_info        *ni = NULL;
+       struct ldif_info        *li = NULL;
        Entry                   *entry;
        int                     rc = LDAP_SUCCESS;
 
@@ -585,9 +650,9 @@ ldif_back_referrals( Operation *op, SlapReply *rs )
                return rc;
        }
 
-       ni = (struct ldif_info *)op->o_bd->be_private;
-       ldap_pvt_thread_mutex_lock( &ni->li_mutex );
-       entry = (Entry *)get_entry( op, &ni->li_base_path );
+       li = (struct ldif_info *)op->o_bd->be_private;
+       ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
+       entry = get_entry( op, &li->li_base_path );
 
        /* no object is found for them */
        if ( entry == NULL ) {
@@ -606,10 +671,10 @@ ldif_back_referrals( Operation *op, SlapReply *rs )
                        op->o_req_dn = pndn;
                        op->o_req_ndn = pndn;
 
-                       entry = (Entry *)get_entry( op, &ni->li_base_path );
+                       entry = get_entry( op, &li->li_base_path );
                }
 
-               ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+               ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
 
                op->o_req_dn = odn;
                op->o_req_ndn = ondn;
@@ -659,7 +724,7 @@ ldif_back_referrals( Operation *op, SlapReply *rs )
                return rc;
        }
 
-       ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+       ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
 
        if ( is_entry_referral( entry ) ) {
                /* entry is a referral */
@@ -685,25 +750,25 @@ ldif_back_referrals( Operation *op, SlapReply *rs )
 
                rs->sr_matched = NULL;
                ber_bvarray_free( refs );
-
-               entry_free( entry );
        }
 
+       entry_free( entry );
+
        return rc;
 }
 
 static int
 ldif_back_bind( Operation *op, SlapReply *rs )
 {
-       struct ldif_info *ni = NULL;
+       struct ldif_info *li = NULL;
        Attribute * a = NULL;
        AttributeDescription *password = slap_schema.si_ad_userPassword;
        int return_val = 0;
        Entry * entry = NULL;
 
-       ni = (struct ldif_info *) op->o_bd->be_private;
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-       entry = (Entry *) get_entry(op, &ni->li_base_path);
+       li = (struct ldif_info *) op->o_bd->be_private;
+       ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
+       entry = get_entry(op, &li->li_base_path);
 
        /* no object is found for them */
        if(entry == NULL) {
@@ -735,7 +800,7 @@ ldif_back_bind( Operation *op, SlapReply *rs )
        goto return_result;
 
  return_result:
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
        if(return_val != 0)
                send_ldap_result( op, rs );
        if(entry != NULL)
@@ -745,21 +810,21 @@ ldif_back_bind( Operation *op, SlapReply *rs )
 
 static int ldif_back_search(Operation *op, SlapReply *rs)
 {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
-       enumCookie ck = {0};
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
+       enumCookie ck = { NULL, NULL, NULL, 0, 0 };
 
        ck.op = op;
        ck.rs = rs;
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
        rs->sr_err = enum_tree( &ck );
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
        send_ldap_result(op, rs);
 
        return rs->sr_err;
 }
 
 static int ldif_back_add(Operation *op, SlapReply *rs) {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
        Entry * e = op->ora_e;
        struct berval dn = e->e_nname;
        struct berval leaf_path = BER_BVNULL;
@@ -767,13 +832,19 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
        int statres;
        char textbuf[SLAP_TEXT_BUFLEN];
 
-       rs->sr_err = entry_schema_check(op->o_bd, e, NULL, 0,
+       Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
+
+       rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
                &rs->sr_text, textbuf, sizeof( textbuf ) );
        if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
-                               
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
 
-       dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
+       rs->sr_err = slap_add_opattrs( op,
+               &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
+       if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
+
+       ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
+
+       dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
 
        if(leaf_path.bv_val != NULL) {
                struct berval base = BER_BVNULL;
@@ -787,11 +858,15 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
                        base.bv_val[base.bv_len] = '\0';
                        if(statres == -1 && errno == ENOENT) {
                                rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
+                               rs->sr_text = "Parent does not exist";
                        }
                        else if(statres != -1) { /* create parent */
                                int mkdirres = mkdir(base.bv_val, 0750);
                                if(mkdirres == -1) {
                                        rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                                       rs->sr_text = "Could not create parent folder";
+                                       Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
+                                               base.bv_val, STRERROR( errno ), 0 );
                                }
                        }
                        else
@@ -800,9 +875,12 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
                if(rs->sr_err == LDAP_SUCCESS) {
                        statres = stat(leaf_path.bv_val, &stats);
                        if(statres == -1 && errno == ENOENT) {
-                               ldap_pvt_thread_mutex_lock(&entry2str_mutex);
-                               rs->sr_err = (int) spew_entry(e, &leaf_path);
-                               ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
+                               rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
+                       }
+                       else if ( statres == -1 ) {
+                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                               Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
+                                       leaf_path.bv_val, STRERROR( errno ), 0 );
                        }
                        else /* it already exists */
                                rs->sr_err = LDAP_ALREADY_EXISTS;
@@ -811,33 +889,40 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
                SLAP_FREE(leaf_path.bv_val);
        }
 
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
 
 send_res:
+       Debug( LDAP_DEBUG_TRACE, 
+                       "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
+                               rs->sr_text : "", 0);
        send_ldap_result(op, rs);
-       return 0;
+       slap_graduate_commit_csn( op );
+       return rs->sr_err;
 }
 
 static int ldif_back_modify(Operation *op, SlapReply *rs) {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
        Modifications * modlst = op->orm_modlist;
        struct berval path = BER_BVNULL;
        Entry * entry = NULL;
        int spew_res;
 
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-       dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
+       slap_mods_opattrs( op, &op->orm_modlist, 1 );
+
+       ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
+       dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
                &path);
-       entry = (Entry *) get_entry(op, &ni->li_base_path);
+       entry = get_entry(op, &li->li_base_path);
 
        if(entry != NULL) {
                rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
                if(rs->sr_err == LDAP_SUCCESS) {
-                       ldap_pvt_thread_mutex_lock(&entry2str_mutex);
-                       spew_res = spew_entry(entry, &path);
-                       ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
+                       int save_errno;
+                       spew_res = spew_entry(entry, &path, 1, &save_errno);
                        if(spew_res == -1) {
-                               perror("could not output entry");
+                               Debug( LDAP_DEBUG_ANY,
+                                       "%s ldif_back_modify: could not output entry \"%s\": %s\n",
+                                       op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
                                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                        }
                }
@@ -851,54 +936,81 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
        if(path.bv_val != NULL)
                SLAP_FREE(path.bv_val);
        rs->sr_text = NULL;
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
        send_ldap_result(op, rs);
-       return 0;
+       slap_graduate_commit_csn( op );
+       return rs->sr_err;
 }
 
 static int ldif_back_delete(Operation *op, SlapReply *rs) {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
        struct berval path = BER_BVNULL;
        int res = 0;
 
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-       dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
+       if ( BER_BVISEMPTY( &op->o_csn )) {
+               struct berval csn;
+               char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
+
+               csn.bv_val = csnbuf;
+               csn.bv_len = sizeof( csnbuf );
+               slap_get_csn( op, &csn, 1 );
+       }
+
+       ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
+       dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
 
        path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
        res = rmdir(path.bv_val);
        path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
-       if ( res && errno != ENOENT ) {
-               rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
-       } else {
-               res = unlink(path.bv_val);
-       }
+       rs->sr_err = LDAP_SUCCESS;
+       if ( res ) {
+               switch ( errno ) {
+               case ENOTEMPTY:
+                       rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
+                       break;
 
-       if(res == -1) {
-               if(errno == ENOENT)
+               case ENOENT:
                        rs->sr_err = LDAP_NO_SUCH_OBJECT;
-               else
+                       break;
+
+               default:
                        rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                       break;
+               }
+
+       } else {
+               res = unlink(path.bv_val);
+               if ( res == -1 ) {
+                       switch ( errno ) {
+                       case ENOENT:
+                               rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                               break;
+
+                       default:
+                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
+                               break;
+                       }
+               }
        }
-       else
-               rs->sr_err = LDAP_SUCCESS;
 
        SLAP_FREE(path.bv_val);
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
        send_ldap_result(op, rs);
-       return 0;
+       slap_graduate_commit_csn( op );
+       return rs->sr_err;
 }
 
 
 static int move_entry(Entry * entry, struct berval * ndn,
-                          struct berval * newndn, struct berval * rootdn,
+                          struct berval * newndn, struct berval * suffixdn,
                           struct berval * base_path) {
        int res;
        int exists_res;
        struct berval path;
        struct berval newpath;
 
-       dn2path(ndn, rootdn, base_path, &path);
-       dn2path(newndn, rootdn, base_path, &newpath);
+       dn2path(ndn, suffixdn, base_path, &path);
+       dn2path(newndn, suffixdn, base_path, &newpath);
 
        if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
                /* some object doesn't exist */
@@ -907,10 +1019,14 @@ static int move_entry(Entry * entry, struct berval * ndn,
        else { /* do the modrdn */
                exists_res = open(newpath.bv_val, O_RDONLY);
                if(exists_res == -1 && errno == ENOENT) {
-                       res = spew_entry(entry, &newpath);
+                       ldap_pvt_thread_mutex_lock( &entry2str_mutex );
+                       res = spew_entry(entry, &newpath, 0, NULL);
                        if(res != -1) {
                                /* if this fails we should log something bad */
                                res = unlink(path.bv_val);
+                               path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
+                               newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
+                               res = rename(path.bv_val, newpath.bv_val);
                                res = LDAP_SUCCESS;
                        }
                        else {
@@ -920,6 +1036,7 @@ static int move_entry(Entry * entry, struct berval * ndn,
                                        res = LDAP_UNWILLING_TO_PERFORM;
                                unlink(newpath.bv_val); /* in case file was created */
                        }
+                       ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
                }
                else if(exists_res) {
                        int close_res = close(exists_res);
@@ -940,176 +1057,170 @@ static int move_entry(Entry * entry, struct berval * ndn,
        return res;
 }
 
-static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
+static int
+ldif_back_modrdn(Operation *op, SlapReply *rs)
+{
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
        struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
-       struct berval p_dn, bv = BER_BVNULL;
+       struct berval p_dn;
        Entry * entry = NULL;
-       LDAPRDN new_rdn = NULL;
-       LDAPRDN old_rdn = NULL;
-       Modifications * mods = NULL;
        int res;
 
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-       ldap_pvt_thread_mutex_lock(&entry2str_mutex);
-       entry = (Entry *) get_entry(op, &ni->li_base_path);
+       slap_mods_opattrs( op, &op->orr_modlist, 1 );
 
-       /* build the mods to the entry */
-       if(entry != NULL) {
-               if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
-                       (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
-                       rs->sr_err = LDAP_INVALID_DN_SYNTAX;
-               }
-               else if(op->oq_modrdn.rs_deleteoldrdn &&
-                       ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
-                       LDAP_DN_FORMAT_LDAP)) {
-                       rs->sr_err = LDAP_OTHER;
-               }
-               else { /* got both rdns successfully, ready to build mods */
-                       if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
-                               != LDAP_SUCCESS) {
-                               rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
-                       }
-                       else { /* built mods successfully */
+       ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
+       entry = get_entry( op, &li->li_base_path );
 
-                               /* build new dn, and new ndn for the entry */
-                               if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
-                                       p_dn = *op->oq_modrdn.rs_newSup;
-                               else
-                                       p_dn = slap_empty_bv;
-                               dnParent(&entry->e_name, &p_dn);
-                               build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); 
-                               dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
-                               ber_dupbv( &new_ndn, &bv );
-                               entry->e_name = new_dn;
-                               entry->e_nname = new_ndn;
-
-                               /* perform the modifications */
-                               res = apply_modify_to_entry(entry, mods, op, rs);
-                               if(res == LDAP_SUCCESS) {
-                                       rs->sr_err = move_entry(entry, &op->o_req_ndn,
-                                                               &new_ndn,
-                                                               &op->o_bd->be_nsuffix[0],
-                                                               &ni->li_base_path);
-                               }
-                               else
-                                       rs->sr_err = res;
+       /* build the mods to the entry */
+       if ( entry != NULL ) {
+               /* build new dn, and new ndn for the entry */
+               if ( op->oq_modrdn.rs_newSup != NULL ) {
+                       struct berval   op_dn = op->o_req_dn,
+                                       op_ndn = op->o_req_ndn;
+                       Entry           *np;
+
+                       /* new superior */
+                       p_dn = *op->oq_modrdn.rs_newSup;
+                       op->o_req_dn = *op->oq_modrdn.rs_newSup;
+                       op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
+                       np = get_entry( op, &li->li_base_path );
+                       op->o_req_dn = op_dn;
+                       op->o_req_ndn = op_ndn;
+                       if ( np == NULL ) {
+                               goto no_such_object;
                        }
+                       entry_free( np );
+               } else {
+                       dnParent( &entry->e_name, &p_dn );
                }
-       }
-       else /* entry was null */
+               build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
+               dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
+               ber_memfree_x( entry->e_name.bv_val, NULL );
+               ber_memfree_x( entry->e_nname.bv_val, NULL );
+               entry->e_name = new_dn;
+               entry->e_nname = new_ndn;
+
+               /* perform the modifications */
+               res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
+               if ( res == LDAP_SUCCESS ) {
+                       rs->sr_err = move_entry( entry, &op->o_req_ndn,
+                                               &new_ndn,
+                                               &op->o_bd->be_nsuffix[0],
+                                               &li->li_base_path );
+               } else {
+                       rs->sr_err = res;
+               }
+       } else {
+no_such_object:;
+               /* entry was null */
                rs->sr_err = LDAP_NO_SUCH_OBJECT;
+       }
 
-       if(entry != NULL)
-               entry_free(entry);
+       if ( entry != NULL ) {
+               entry_free( entry );
+       }
        rs->sr_text = "";
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
-       ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
-       send_ldap_result(op, rs);
-       return 0;
+       ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
+       send_ldap_result( op, rs );
+       slap_graduate_commit_csn( op );
+       return rs->sr_err;
 }
 
-static int ldif_back_compare(Operation *op, SlapReply *rs) {
-       struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
-       Entry * e = NULL;
-       Attribute       *a;
-
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-
-       e = (Entry *) get_entry(op, &ni->li_base_path);
-       if(e != NULL) {
-               for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
-                       a != NULL;
-                       a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
-                       rs->sr_err = LDAP_COMPARE_FALSE;
-               
-                       if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
-                                               SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
-                                               SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
-                                               a->a_nvals, &op->oq_compare.rs_ava->aa_value,
-                                               op->o_tmpmemctx ) == 0) {
-                               rs->sr_err = LDAP_COMPARE_TRUE;
-                               break;
-                       }
-               }
-       }
-       else {
-               rs->sr_err = LDAP_NO_SUCH_OBJECT;
+/* return LDAP_SUCCESS IFF we can retrieve the specified entry.
+ */
+int ldif_back_entry_get(
+       Operation *op,
+       struct berval *ndn,
+       ObjectClass *oc,
+       AttributeDescription *at,
+       int rw,
+       Entry **ent )
+{
+       struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
+       struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
+
+       assert( ndn != NULL );
+       assert( !BER_BVISNULL( ndn ) );
+
+       ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
+       op->o_req_dn = *ndn;
+       op->o_req_ndn = *ndn;
+       *ent = get_entry( op, &li->li_base_path );
+       op->o_req_dn = op_dn;
+       op->o_req_ndn = op_ndn;
+       ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
+
+       if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
+               entry_free( *ent );
+               *ent = NULL;
        }
 
-       if(e != NULL)
-               entry_free(e);
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
-       send_ldap_result(op, rs);
-       return 0;
+       return ( *ent == NULL ? 1 : 0 );
 }
 
-static int ldif_tool_entry_open(BackendDB * be, int mode) {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
-       ni->li_tool_current = 0;
+static int ldif_tool_entry_open(BackendDB *be, int mode) {
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
+       li->li_tool_current = 0;
        return 0;
 }                                      
 
 static int ldif_tool_entry_close(BackendDB * be) {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
 
-       SLAP_FREE(ni->li_tool_cookie.entries);
+       SLAP_FREE(li->li_tool_cookie.entries);
        return 0;
 }
 
+static ID ldif_tool_entry_next(BackendDB *be)
+{
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
+       if(li->li_tool_current >= li->li_tool_cookie.eind)
+               return NOID;
+       else
+               return ++li->li_tool_current;
+}
+
 static ID
 ldif_tool_entry_first(BackendDB *be)
 {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
-       ID id = 1; /* first entry in the array of entries shifted by one */
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
 
-       ni->li_tool_current = 1;
-       if(ni->li_tool_cookie.entries == NULL) {
+       if(li->li_tool_cookie.entries == NULL) {
                Operation op = {0};
 
                op.o_bd = be;
                op.o_req_dn = *be->be_suffix;
                op.o_req_ndn = *be->be_nsuffix;
                op.ors_scope = LDAP_SCOPE_SUBTREE;
-               ni->li_tool_cookie.op = &op;
-               (void)enum_tree( &ni->li_tool_cookie );
-               ni->li_tool_cookie.op = NULL;
+               li->li_tool_cookie.op = &op;
+               (void)enum_tree( &li->li_tool_cookie );
+               li->li_tool_cookie.op = NULL;
        }
-       return id;
-}
-
-static ID ldif_tool_entry_next(BackendDB *be)
-{
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
-       ni->li_tool_current += 1;
-       if(ni->li_tool_current > ni->li_tool_cookie.eind)
-               return NOID;
-       else
-               return ni->li_tool_current;
+       return ldif_tool_entry_next( be );
 }
 
 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
        Entry * e;
 
-       if(id > ni->li_tool_cookie.eind || id < 1)
+       if(id > li->li_tool_cookie.eind || id < 1)
                return NULL;
        else {
-               e = ni->li_tool_cookie.entries[id - 1];
-               ni->li_tool_cookie.entries[id - 1] = NULL;
+               e = li->li_tool_cookie.entries[id - 1];
+               li->li_tool_cookie.entries[id - 1] = NULL;
                return e;
        }
 }
 
 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
        struct berval dn = e->e_nname;
        struct berval leaf_path = BER_BVNULL;
        struct stat stats;
        int statres;
        int res = LDAP_SUCCESS;
 
-       dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
+       dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
 
        if(leaf_path.bv_val != NULL) {
                struct berval base = BER_BVNULL;
@@ -1136,7 +1247,7 @@ static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
                if(res == LDAP_SUCCESS) {
                        statres = stat(leaf_path.bv_val, &stats);
                        if(statres == -1 && errno == ENOENT) {
-                               res = (int) spew_entry(e, &leaf_path);
+                               res = spew_entry(e, &leaf_path, 0, NULL);
                        }
                        else /* it already exists */
                                res = LDAP_ALREADY_EXISTS;
@@ -1153,38 +1264,34 @@ static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
 }
 
 static int
-ldif_back_db_init( BackendDB *be )
+ldif_back_db_init( BackendDB *be, ConfigReply *cr )
 {
-       struct ldif_info *ni;
+       struct ldif_info *li;
 
-       ni = ch_calloc( 1, sizeof(struct ldif_info) );
-       be->be_private = ni;
+       li = ch_calloc( 1, sizeof(struct ldif_info) );
+       be->be_private = li;
        be->be_cf_ocs = ldifocs;
-       ldap_pvt_thread_mutex_init(&ni->li_mutex);
+       ldap_pvt_thread_rdwr_init(&li->li_rdwr);
        return 0;
 }
 
 static int
-ldif_back_db_destroy(
-                          Backend      *be
-                          )
+ldif_back_db_destroy( Backend *be, ConfigReply *cr )
 {
-       struct ldif_info *ni = be->be_private;
+       struct ldif_info *li = be->be_private;
 
-       ch_free(ni->li_base_path.bv_val);
-       ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
+       ch_free(li->li_base_path.bv_val);
+       ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
        free( be->be_private );
        return 0;
 }
 
 static int
-ldif_back_db_open(
-                       Backend *be
-                       )
+ldif_back_db_open( Backend *be, ConfigReply *cr)
 {
-       struct ldif_info *ni = (struct ldif_info *) be->be_private;
-       if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
-               fprintf(stderr, "missing base path for back-ldif\n");
+       struct ldif_info *li = (struct ldif_info *) be->be_private;
+       if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
+               Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
                return 1;
        }
        return 0;
@@ -1221,7 +1328,7 @@ ldif_back_initialize(
        bi->bi_op_bind = ldif_back_bind;
        bi->bi_op_unbind = 0;
        bi->bi_op_search = ldif_back_search;
-       bi->bi_op_compare = ldif_back_compare;
+       bi->bi_op_compare = 0;
        bi->bi_op_modify = ldif_back_modify;
        bi->bi_op_modrdn = ldif_back_modrdn;
        bi->bi_op_add = ldif_back_add;
@@ -1235,6 +1342,12 @@ ldif_back_initialize(
        bi->bi_connection_init = 0;
        bi->bi_connection_destroy = 0;
 
+       bi->bi_entry_get_rw = ldif_back_entry_get;
+
+#if 0  /* NOTE: uncomment to completely disable access control */
+       bi->bi_access_allowed = slap_access_always_allowed;
+#endif
+
        bi->bi_tool_entry_open = ldif_tool_entry_open;
        bi->bi_tool_entry_close = ldif_tool_entry_close;
        bi->bi_tool_entry_first = ldif_tool_entry_first;
@@ -1245,7 +1358,6 @@ ldif_back_initialize(
        bi->bi_tool_sync = 0;
        
        bi->bi_tool_dn2id_get = 0;
-       bi->bi_tool_id2entry_get = 0;
        bi->bi_tool_entry_modify = 0;
 
        bi->bi_cf_ocs = ldifocs;