Blame SOURCES/0067-Ticket-49551-v3-correct-handling-of-numsubordinates-.patch

96373c
From 233b64f26df76aa50f4b37aaf6b3804d208fdc1b Mon Sep 17 00:00:00 2001
96373c
From: Ludwig Krispenz <lkrispen@redhat.com>
96373c
Date: Mon, 12 Feb 2018 09:24:25 +0100
96373c
Subject: [PATCH] Ticket 49551 - v3 - correct handling of numsubordinates for
96373c
 cenotaphs and tombstone delete
96373c
96373c
        Bug: The ticket exposed several problems with tombstone handling.
96373c
                - tombstone entries of conflicts were not purged in tombstone purging
96373c
                - cenotaphs are tombstone, but the subordinate count was not managed properly
96373c
                - direct delete of tombstones failed with err=1
96373c
                - delete of entry with only conflict children failed correctly, but gave no hint why
96373c
96373c
        Fix: update the correct numsobordinates attribut for cenotaphs
96373c
             set proper flag in directly deleting a tombstone
96373c
             change search filter for tombstone purging to include ldapsubentries
96373c
             check for conflict children if a delete is rejected and add a message to the response
96373c
96373c
        Reviewed by; Thierry, William - thanks
96373c
---
96373c
 ldap/servers/plugins/replication/repl5_replica.c | 14 +++++++++--
96373c
 ldap/servers/plugins/replication/urp.c           |  8 +++---
96373c
 ldap/servers/slapd/back-ldbm/ldbm_add.c          |  8 +++---
96373c
 ldap/servers/slapd/back-ldbm/ldbm_delete.c       | 14 ++++++++---
96373c
 ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c     |  3 ++-
96373c
 ldap/servers/slapd/back-ldbm/parents.c           | 12 ++++++---
96373c
 ldap/servers/slapd/entry.c                       | 31 ++++++++++++++++++++++++
96373c
 ldap/servers/slapd/slapi-plugin.h                |  2 ++
96373c
 ldap/servers/slapd/slapi-private.h               |  1 +
96373c
 ldap/servers/slapd/task.c                        |  4 +--
96373c
 10 files changed, 78 insertions(+), 19 deletions(-)
96373c
96373c
diff --git a/ldap/servers/plugins/replication/repl5_replica.c b/ldap/servers/plugins/replication/repl5_replica.c
96373c
index bdb8a5167..628fb9ceb 100644
96373c
--- a/ldap/servers/plugins/replication/repl5_replica.c
96373c
+++ b/ldap/servers/plugins/replication/repl5_replica.c
96373c
@@ -3017,6 +3017,16 @@ process_reap_entry(Slapi_Entry *entry, void *cb_data)
96373c
        search in the future, see _replica_reap_tombstones below and add more to the
96373c
        attrs array */
96373c
     deletion_csn = entry_get_deletion_csn(entry);
96373c
+    if (deletion_csn == NULL) {
96373c
+        /* this might be a tombstone which was directly added, eg a cenotaph
96373c
+         * check if a tombstonecsn exist and use it
96373c
+         */
96373c
+        char *tombstonecsn = slapi_entry_attr_get_charptr(entry, SLAPI_ATTR_TOMBSTONE_CSN);
96373c
+        if (tombstonecsn) {
96373c
+            deletion_csn = csn_new_by_string(tombstonecsn);
96373c
+            slapi_ch_free_string(&tombstonecsn);
96373c
+        }
96373c
+    }
96373c
 
96373c
     if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
96373c
         (!is_ruv_tombstone_entry(entry))) {
96373c
@@ -3116,11 +3126,11 @@ _replica_reap_tombstones(void *arg)
96373c
              */
96373c
             csn_as_string(purge_csn, PR_FALSE, deletion_csn_str);
96373c
             PR_snprintf(tombstone_filter, 128,
96373c
-                        "(&(%s<=%s)(objectclass=nsTombstone))", SLAPI_ATTR_TOMBSTONE_CSN,
96373c
+                        "(&(%s<=%s)(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))", SLAPI_ATTR_TOMBSTONE_CSN,
96373c
                         csn_as_string(purge_csn, PR_FALSE, deletion_csn_str));
96373c
         } else {
96373c
             /* Use the old inefficient filter */
96373c
-            PR_snprintf(tombstone_filter, 128, "(objectclass=nsTombstone)");
96373c
+            PR_snprintf(tombstone_filter, 128, "(&(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))");
96373c
         }
96373c
 
96373c
         /* we just need the objectclass - for the deletion csn
96373c
diff --git a/ldap/servers/plugins/replication/urp.c b/ldap/servers/plugins/replication/urp.c
96373c
index d4556d7fd..11c5da7cf 100644
96373c
--- a/ldap/servers/plugins/replication/urp.c
96373c
+++ b/ldap/servers/plugins/replication/urp.c
96373c
@@ -911,7 +911,7 @@ urp_fixup_add_cenotaph (Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
96373c
                                     cenotaph,
96373c
                                     NULL,
96373c
                                     repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
96373c
-                                    OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP);
96373c
+                                    OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP|OP_FLAG_CENOTAPH_ENTRY);
96373c
     slapi_add_internal_pb(add_pb);
96373c
     slapi_pblock_get(add_pb, SLAPI_PLUGIN_INTOP_RESULT, &ret;;
96373c
 
96373c
@@ -1922,7 +1922,7 @@ done:
96373c
     newpb = NULL;
96373c
 
96373c
     slapi_log_err(SLAPI_LOG_REPL, sessionid,
96373c
-                  "urp_get_min_naming_conflict_entry - Found %d entries\n", i);
96373c
+                  "urp_get_min_naming_conflict_entry - Found %d entries\n", min_csn?1:0);
96373c
 
96373c
     return min_naming_conflict_entry;
96373c
 }
96373c
@@ -2172,8 +2172,8 @@ mod_objectclass_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi
96373c
     char csnstr[CSN_STRSIZE+1] = {0};
96373c
 
96373c
     slapi_mods_init(&smods, 3);
96373c
-    slapi_mods_add(&smods, LDAP_MOD_ADD, "objectclass", strlen("ldapsubentry"),"ldapsubentry");
96373c
-    slapi_mods_add(&smods, LDAP_MOD_REPLACE, "conflictcsn", CSN_STRSIZE, csn_as_string(opcsn, PR_FALSE, csnstr));
96373c
+    slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectclass", "ldapsubentry");
96373c
+    slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "conflictcsn", csn_as_string(opcsn, PR_FALSE, csnstr));
96373c
     op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, 0);
96373c
     slapi_mods_done(&smods);
96373c
     if (op_result == LDAP_TYPE_OR_VALUE_EXISTS) {
96373c
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_add.c b/ldap/servers/slapd/back-ldbm/ldbm_add.c
96373c
index c93d44a65..f0a3262ec 100644
96373c
--- a/ldap/servers/slapd/back-ldbm/ldbm_add.c
96373c
+++ b/ldap/servers/slapd/back-ldbm/ldbm_add.c
96373c
@@ -81,6 +81,7 @@ ldbm_back_add(Slapi_PBlock *pb)
96373c
     Slapi_Operation *operation;
96373c
     int is_replicated_operation = 0;
96373c
     int is_resurect_operation = 0;
96373c
+    int is_cenotaph_operation = 0;
96373c
     int is_tombstone_operation = 0;
96373c
     int is_fixup_operation = 0;
96373c
     int is_remove_from_cache = 0;
96373c
@@ -116,6 +117,7 @@ ldbm_back_add(Slapi_PBlock *pb)
96373c
     }
96373c
 
96373c
     is_resurect_operation = operation_is_flag_set(operation, OP_FLAG_RESURECT_ENTRY);
96373c
+    is_cenotaph_operation = operation_is_flag_set(operation, OP_FLAG_CENOTAPH_ENTRY);
96373c
     is_tombstone_operation = operation_is_flag_set(operation, OP_FLAG_TOMBSTONE_ENTRY);
96373c
     is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
96373c
     is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
96373c
@@ -846,9 +848,9 @@ ldbm_back_add(Slapi_PBlock *pb)
96373c
                the in-memory state of the parent to reflect the new child (update
96373c
                subordinate count specifically */
96373c
             if (parententry) {
96373c
-                retval = parent_update_on_childchange(&parent_modify_c,
96373c
-                                                      is_resurect_operation ? PARENTUPDATE_RESURECT : PARENTUPDATE_ADD,
96373c
-                                                      NULL);
96373c
+                int op = is_resurect_operation ? PARENTUPDATE_RESURECT : PARENTUPDATE_ADD;
96373c
+                if (is_cenotaph_operation ) op |= PARENTUPDATE_CREATE_TOMBSTONE;
96373c
+                retval = parent_update_on_childchange(&parent_modify_c, op, NULL);
96373c
                 slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_add",
96373c
                               "conn=%lu op=%d parent_update_on_childchange: old_entry=0x%p, new_entry=0x%p, rc=%d\n",
96373c
                               conn_id, op_id, parent_modify_c.old_entry, parent_modify_c.new_entry, retval);
96373c
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_delete.c b/ldap/servers/slapd/back-ldbm/ldbm_delete.c
96373c
index be0db1bd0..bc0a3654e 100644
96373c
--- a/ldap/servers/slapd/back-ldbm/ldbm_delete.c
96373c
+++ b/ldap/servers/slapd/back-ldbm/ldbm_delete.c
96373c
@@ -291,9 +291,16 @@ replace_entry:
96373c
             retval = slapi_entry_has_children(e->ep_entry);
96373c
             if (retval && !is_replicated_operation) {
96373c
                 ldap_result_code= LDAP_NOT_ALLOWED_ON_NONLEAF;
96373c
-                slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_delete", 
96373c
-                              "conn=%lu op=%d Deleting entry %s has %d children.\n", 
96373c
-                               conn_id, op_id, slapi_entry_get_dn(e->ep_entry), retval);
96373c
+                if (slapi_entry_has_conflict_children(e->ep_entry, (void *)li->li_identity) > 0) {
96373c
+                    ldap_result_message = "Entry has replication conflicts as children";
96373c
+                    slapi_log_err(SLAPI_LOG_ERR, "ldbm_back_delete",
96373c
+                                  "conn=%lu op=%d Deleting entry %s has replication conflicts as children.\n",
96373c
+                                   conn_id, op_id, slapi_entry_get_dn(e->ep_entry));
96373c
+                } else {
96373c
+                    slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_delete",
96373c
+                                  "conn=%lu op=%d Deleting entry %s has %d children.\n",
96373c
+                                   conn_id, op_id, slapi_entry_get_dn(e->ep_entry), retval);
96373c
+                }
96373c
                 retval = -1;
96373c
                 goto error_return;
96373c
             }
96373c
@@ -431,6 +438,7 @@ replace_entry:
96373c
                     slapi_log_err(SLAPI_LOG_WARNING, "ldbm_back_delete",
96373c
                             "Attempt to Tombstone again a tombstone entry %s\n", dn);
96373c
                     delete_tombstone_entry = 1;
96373c
+                    operation_set_flag(operation, OP_FLAG_TOMBSTONE_ENTRY);
96373c
                 }
96373c
             }
96373c
 
96373c
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c b/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
96373c
index b41a2d241..5797dd779 100644
96373c
--- a/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
96373c
+++ b/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
96373c
@@ -2824,7 +2824,8 @@ _entryrdn_delete_key(backend *be,
96373c
                     break;
96373c
                 }
96373c
                 childelem = (rdn_elem *)dataret.data;
96373c
-                if (!slapi_is_special_rdn(childelem->rdn_elem_nrdn_rdn, RDN_IS_TOMBSTONE)) {
96373c
+                if (!slapi_is_special_rdn(childelem->rdn_elem_nrdn_rdn, RDN_IS_TOMBSTONE) &&
96373c
+                    !strcasestr(childelem->rdn_elem_nrdn_rdn, "cenotaphid")) {
96373c
                     /* there's at least one live child */
96373c
                     slapi_log_err(SLAPI_LOG_ERR, "_entryrdn_delete_key",
96373c
                                   "Failed to remove %s; has a child %s\n", nrdn,
96373c
diff --git a/ldap/servers/slapd/back-ldbm/parents.c b/ldap/servers/slapd/back-ldbm/parents.c
96373c
index 79e66451e..1afc795c0 100644
96373c
--- a/ldap/servers/slapd/back-ldbm/parents.c
96373c
+++ b/ldap/servers/slapd/back-ldbm/parents.c
96373c
@@ -89,7 +89,11 @@ parent_update_on_childchange(modify_context *mc, int op, size_t *new_sub_count)
96373c
         }
96373c
     }
96373c
 
96373c
-    if (PARENTUPDATE_DELETE_TOMBSTONE != repl_op) {
96373c
+    if ((PARENTUPDATE_ADD == op) && (PARENTUPDATE_CREATE_TOMBSTONE == repl_op)) {
96373c
+        /* we are directly adding a tombstone entry, only need to
96373c
+         * update the tombstone subordinates
96373c
+         */
96373c
+    } else if (PARENTUPDATE_DELETE_TOMBSTONE != repl_op) {
96373c
         /* are we adding ? */
96373c
         if (((PARENTUPDATE_ADD == op) || (PARENTUPDATE_RESURECT == op)) && !already_present) {
96373c
             /* If so, and the parent entry does not already have a subcount
96373c
@@ -136,10 +140,10 @@ parent_update_on_childchange(modify_context *mc, int op, size_t *new_sub_count)
96373c
         }
96373c
     }
96373c
 
96373c
-    /* tombstoneNumSubordinates is needed only when this is repl op
96373c
-     * and a child is being deleted */
96373c
+    /* tombstoneNumSubordinates has to be updated if a tombstone child has been
96373c
+     * deleted or a tombstone has been directly added (cenotaph) */
96373c
     current_sub_count = LDAP_MAXINT;
96373c
-    if ((repl_op && (PARENTUPDATE_DEL == op)) || (PARENTUPDATE_RESURECT == op)) {
96373c
+    if (repl_op) {
96373c
         ret = slapi_entry_attr_find(mc->old_entry->ep_entry,
96373c
                                     tombstone_numsubordinates, &read_attr);
96373c
         if (0 == ret) {
96373c
diff --git a/ldap/servers/slapd/entry.c b/ldap/servers/slapd/entry.c
96373c
index 32828b4e2..b85e9f5b0 100644
96373c
--- a/ldap/servers/slapd/entry.c
96373c
+++ b/ldap/servers/slapd/entry.c
96373c
@@ -3238,6 +3238,37 @@ slapi_entry_has_children(const Slapi_Entry *entry)
96373c
     return slapi_entry_has_children_ext(entry, 0);
96373c
 }
96373c
 
96373c
+int
96373c
+slapi_entry_has_conflict_children(const Slapi_Entry *entry, void *plg_id)
96373c
+{
96373c
+    Slapi_PBlock *search_pb = NULL;
96373c
+    Slapi_Entry **entries;
96373c
+    int rc = 0;
96373c
+
96373c
+    search_pb = slapi_pblock_new();
96373c
+    slapi_search_internal_set_pb(search_pb, slapi_entry_get_dn_const(entry),
96373c
+                                 LDAP_SCOPE_ONELEVEL,
96373c
+                                 "(&(objectclass=ldapsubentry)(nsds5ReplConflict=namingConflict*))",
96373c
+                                 NULL, 0, NULL, NULL, plg_id, 0);
96373c
+    slapi_search_internal_pb(search_pb);
96373c
+    slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
96373c
+    if (rc) {
96373c
+        rc = -1;
96373c
+    } else {
96373c
+        slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
96373c
+        if (entries && entries[0]) {
96373c
+            /* we found at least one conflict entry */
96373c
+            rc = 1;
96373c
+        } else {
96373c
+            rc = 0;
96373c
+        }
96373c
+        slapi_free_search_results_internal(search_pb);
96373c
+    }
96373c
+    slapi_pblock_destroy(search_pb);
96373c
+
96373c
+    return rc;
96373c
+}
96373c
+
96373c
 /*
96373c
  * Renames an entry to simulate a MODRDN operation
96373c
  */
96373c
diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h
96373c
index 95cdcc0da..6978e258f 100644
96373c
--- a/ldap/servers/slapd/slapi-plugin.h
96373c
+++ b/ldap/servers/slapd/slapi-plugin.h
96373c
@@ -2000,6 +2000,8 @@ int slapi_entry_has_children(const Slapi_Entry *e);
96373c
  */
96373c
 int slapi_entry_has_children_ext(const Slapi_Entry *e, int include_tombstone);
96373c
 
96373c
+int slapi_entry_has_conflict_children(const Slapi_Entry *e, void *plg_id);
96373c
+
96373c
 /**
96373c
  * This function determines if an entry is the root DSE.
96373c
  *
96373c
diff --git a/ldap/servers/slapd/slapi-private.h b/ldap/servers/slapd/slapi-private.h
96373c
index 548d5cabb..b08c0d7ce 100644
96373c
--- a/ldap/servers/slapd/slapi-private.h
96373c
+++ b/ldap/servers/slapd/slapi-private.h
96373c
@@ -403,6 +403,7 @@ char *slapi_filter_to_string_internal(const struct slapi_filter *f, char *buf, s
96373c
 #define OP_FLAG_NEVER_CHAIN SLAPI_OP_FLAG_NEVER_CHAIN         /* 0x000800 */
96373c
 #define OP_FLAG_TOMBSTONE_ENTRY SLAPI_OP_FLAG_TOMBSTONE_ENTRY /* 0x001000 */
96373c
 #define OP_FLAG_RESURECT_ENTRY 0x002000
96373c
+#define OP_FLAG_CENOTAPH_ENTRY 0x004000
96373c
 #define OP_FLAG_ACTION_NOLOG 0x008000            /* Do not log the entry in \
96373c
                                                   * audit log or change log  \
96373c
                                                   */
96373c
diff --git a/ldap/servers/slapd/task.c b/ldap/servers/slapd/task.c
96373c
index 4bd8895ff..3f9d5d995 100644
96373c
--- a/ldap/servers/slapd/task.c
96373c
+++ b/ldap/servers/slapd/task.c
96373c
@@ -2352,10 +2352,10 @@ task_fixup_tombstone_thread(void *arg)
96373c
 
96373c
     if (task_data->stripcsn) {
96373c
         /* find tombstones with nsTombstoneCSN */
96373c
-        filter = "(&(nstombstonecsn=*)(objectclass=nsTombstone))";
96373c
+        filter = "(&(nstombstonecsn=*)(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))";
96373c
     } else {
96373c
         /* find tombstones missing nsTombstoneCSN */
96373c
-        filter = "(&(!(nstombstonecsn=*))(objectclass=nsTombstone))";
96373c
+        filter = "(&(!(nstombstonecsn=*))(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))";
96373c
     }
96373c
 
96373c
     /* Okay check the specified backends only */
96373c
-- 
96373c
2.13.6
96373c