]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldif/ldif.c
ITS#4384 rework entryCSN handling, retrieve from incoming request
[openldap] / servers / slapd / back-ldif / ldif.c
index 39244c85040d5bba4880a76beb045fc89af66b95..8293c52fa149481a68747cad01dc486699bfa984 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-2006 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -150,7 +150,8 @@ 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");
+                       Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
+                               strerror( errno ), 0, 0 );
                        return -1;
                }
                else {
@@ -256,7 +257,8 @@ static Entry * get_entry(Operation *op, struct berval *base_path) {
        fd = open(path.bv_val, O_RDONLY);
        /* error opening file (mebbe should log error) */
        if(fd == -1) {
-               perror("failed to open file");
+               Debug(LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
+                       path.bv_val, strerror(errno), 0 );
        }
 
        if(path.bv_val != NULL)
@@ -296,7 +298,7 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
 
        fd = open( path->bv_val, O_RDONLY );
        if ( fd < 0 ) {
-               Debug( LDAP_DEBUG_TRACE,
+               Debug( LDAP_DEBUG_ANY,
                        "=> ldif_enum_tree: failed to open %s\n",
                        path->bv_val, 0, 0 );
                return LDAP_NO_SUCH_OBJECT;
@@ -330,7 +332,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,12 +343,12 @@ 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) {
@@ -378,11 +380,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) {
@@ -453,7 +455,7 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                        free(ptr);
                }
        }
-leave:
+done:
        if ( fd ) entry_free( e );
        return rc;
 }
@@ -685,10 +687,10 @@ ldif_back_referrals( Operation *op, SlapReply *rs )
 
                rs->sr_matched = NULL;
                ber_bvarray_free( refs );
-
-               entry_free( entry );
        }
 
+       entry_free( entry );
+
        return rc;
 }
 
@@ -767,6 +769,8 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
        int statres;
        char textbuf[SLAP_TEXT_BUFLEN];
 
+       slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
+
        rs->sr_err = entry_schema_check(op, e, NULL, 0,
                &rs->sr_text, textbuf, sizeof( textbuf ) );
        if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
@@ -815,6 +819,7 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
 
 send_res:
        send_ldap_result(op, rs);
+       slap_graduate_commit_csn( op );
        return 0;
 }
 
@@ -825,6 +830,8 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
        Entry * entry = NULL;
        int spew_res;
 
+       slap_mods_opattrs( op, &op->orm_modlist, 1 );
+
        ldap_pvt_thread_mutex_lock(&ni->li_mutex);
        dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
                &path);
@@ -833,11 +840,15 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
        if(entry != NULL) {
                rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
                if(rs->sr_err == LDAP_SUCCESS) {
+                       int save_errno;
                        ldap_pvt_thread_mutex_lock(&entry2str_mutex);
                        spew_res = spew_entry(entry, &path);
+                       save_errno = errno;
                        ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
                        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;
                        }
                }
@@ -853,6 +864,7 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
        rs->sr_text = NULL;
        ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
        send_ldap_result(op, rs);
+       slap_graduate_commit_csn( op );
        return 0;
 }
 
@@ -861,6 +873,15 @@ static int ldif_back_delete(Operation *op, SlapReply *rs) {
        struct berval path = BER_BVNULL;
        int res = 0;
 
+       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_mutex_lock(&ni->li_mutex);
        dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
 
@@ -885,6 +906,7 @@ static int ldif_back_delete(Operation *op, SlapReply *rs) {
        SLAP_FREE(path.bv_val);
        ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
        send_ldap_result(op, rs);
+       slap_graduate_commit_csn( op );
        return 0;
 }
 
@@ -940,108 +962,83 @@ static int move_entry(Entry * entry, struct berval * ndn,
        return res;
 }
 
-static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
+static int
+ldif_back_modrdn(Operation *op, SlapReply *rs)
+{
        struct ldif_info *ni = (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 );
+
+       ldap_pvt_thread_mutex_lock( &ni->li_mutex );
+       ldap_pvt_thread_mutex_lock( &entry2str_mutex );
+       entry = (Entry *) get_entry( op, &ni->li_base_path );
 
        /* 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;
+       if ( entry != NULL ) {
+               /* 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;
                }
-               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 */
-
-                               /* 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;
-                       }
+               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, &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],
+                                               &ni->li_base_path );
+               } else {
+                       rs->sr_err = res;
                }
-       }
-       else /* entry was null */
+       } else {
+               /* 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);
+       ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+       ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
+       send_ldap_result( op, rs );
+       slap_graduate_commit_csn( op );
        return 0;
 }
 
-static int ldif_back_compare(Operation *op, SlapReply *rs) {
+/* 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 *ni = (struct ldif_info *) op->o_bd->be_private;
-       Entry * e = NULL;
-       Attribute       *a;
 
-       ldap_pvt_thread_mutex_lock(&ni->li_mutex);
+       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;
-       }
+       *ent = (Entry *) get_entry( op, &ni->li_base_path );
 
-       if(e != NULL)
-               entry_free(e);
-       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
-       send_ldap_result(op, rs);
-       return 0;
+       ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+
+       return ( *ent == NULL ? 1 : 0 );
 }
 
 static int ldif_tool_entry_open(BackendDB * be, int mode) {
@@ -1184,7 +1181,7 @@ ldif_back_db_open(
 {
        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");
+               Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
                return 1;
        }
        return 0;
@@ -1221,7 +1218,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 +1232,14 @@ 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 */
+#ifdef SLAP_OVERLAY_ACCESS
+       bi->bi_access_allowed = slap_access_always_allowed;
+#endif /* SLAP_OVERLAY_ACCESS */
+#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;