From a8e1147824a63b46fb6979a2109aa871fd328de1 Mon Sep 17 00:00:00 2001
From: Ludwig Krispenz <lkrispen@redhat.com>
Date: Wed, 20 Dec 2017 11:45:58 +0100
Subject: [PATCH] better handling of tombstone dns

---
 ldap/servers/slapd/dn.c   | 10 +---------
 ldap/servers/slapd/rdn.c  |  2 +-
 ldap/servers/slapd/util.c |  6 ++++--
 3 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/ldap/servers/slapd/dn.c b/ldap/servers/slapd/dn.c
index afca372..1a43ed2 100644
--- a/ldap/servers/slapd/dn.c
+++ b/ldap/servers/slapd/dn.c
@@ -2643,15 +2643,7 @@ slapi_sdn_scope_test_ext(const Slapi_DN *dn, const Slapi_DN *base, int scope, in
 
     switch (scope) {
     case LDAP_SCOPE_BASE:
-        if (flags & SLAPI_ENTRY_FLAG_TOMBSTONE) {
-            Slapi_DN parent;
-            slapi_sdn_init(&parent);
-            slapi_sdn_get_parent(dn, &parent);
-            rc = (slapi_sdn_compare(dn, &parent) == 0);
-            slapi_sdn_done(&parent);
-        } else {
-            rc = (slapi_sdn_compare(dn, base) == 0);
-        }
+        rc = (slapi_sdn_compare(dn, base) == 0);
         break;
     case LDAP_SCOPE_ONELEVEL:
 #define RUVRDN SLAPI_ATTR_UNIQUEID "=" RUV_STORAGE_ENTRY_UNIQUEID ","
diff --git a/ldap/servers/slapd/rdn.c b/ldap/servers/slapd/rdn.c
index 41c0750..99dbba0 100644
--- a/ldap/servers/slapd/rdn.c
+++ b/ldap/servers/slapd/rdn.c
@@ -173,7 +173,7 @@ _slapi_rdn_init_all_dn_ext(Slapi_RDN *rdn, const Slapi_DN *sdn, int is_tombstone
 
     /* Get the last matched position */
     if (dns) {
-        if (is_tombstone && slapi_is_special_rdn(dns[0], RDN_IS_TOMBSTONE)) {
+        if (slapi_is_special_rdn(dns[0], RDN_IS_TOMBSTONE)) {
             /* merge nsuniqueid=...,<rdn> into one rdn */
             rdn->rdn = slapi_ch_smprintf("%s,%s", dns[0], dns[1]);
             slapi_ch_free_string(&dns[0]);
diff --git a/ldap/servers/slapd/util.c b/ldap/servers/slapd/util.c
index a0f3268..aa31204 100644
--- a/ldap/servers/slapd/util.c
+++ b/ldap/servers/slapd/util.c
@@ -1417,8 +1417,10 @@ slapi_is_special_rdn(const char *rdn, int flag)
         if (!PL_strncasecmp(rp, SLAPI_ATTR_UNIQUEID, SLAPI_ATTR_UNIQUEID_LENGTH) &&
             (*(rp + SLAPI_ATTR_UNIQUEID_LENGTH) == '=')) {
             if (RDN_IS_TOMBSTONE == flag) {
-                if ((*(rp + util_uniqueidlen) == ',') ||
-                    (*(rp + util_uniqueidlen) == '\0')) {
+                if (((*(rp + util_uniqueidlen) == ',') ||
+                    (*(rp + util_uniqueidlen) == '\0')) &&
+                    (strncasecmp( rp + SLAPI_ATTR_UNIQUEID_LENGTH + 1, RUV_STORAGE_ENTRY_UNIQUEID,
+                                 sizeof(RUV_STORAGE_ENTRY_UNIQUEID) - 1))) {
                     return 1;
                 } else {
                     return 0;
-- 
2.5.5

