Blame SOURCES/0096-sysdb-add-sysdb_subdomain_content_delete.patch

8d3578
From a9f03f01b95031f748fdb968ae9c16b9c3d6ed21 Mon Sep 17 00:00:00 2001
8d3578
From: Sumit Bose <sbose@redhat.com>
8d3578
Date: Wed, 18 Sep 2019 17:33:55 +0200
8d3578
Subject: [PATCH 96/97] sysdb: add sysdb_subdomain_content_delete()
8d3578
MIME-Version: 1.0
8d3578
Content-Type: text/plain; charset=UTF-8
8d3578
Content-Transfer-Encoding: 8bit
8d3578
8d3578
sysdb_subdomain_content_delete() will remove all user and group objects
8d3578
from a sub-domain container but not the sub-domain object and the user
8d3578
and group container itself.
8d3578
8d3578
Related to https://pagure.io/SSSD/sssd/issue/4078
8d3578
8d3578
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
8d3578
---
8d3578
 src/db/sysdb.h            |  8 ++++
8d3578
 src/db/sysdb_ops.c        | 17 ++++++--
8d3578
 src/db/sysdb_subdomains.c | 20 ++++++++-
8d3578
 src/tests/sysdb-tests.c   | 88 +++++++++++++++++++++++++++++++++++++++
8d3578
 4 files changed, 127 insertions(+), 6 deletions(-)
8d3578
8d3578
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
8d3578
index 0a7e7c4f8..f8a2c87ae 100644
8d3578
--- a/src/db/sysdb.h
8d3578
+++ b/src/db/sysdb.h
8d3578
@@ -557,6 +557,9 @@ errno_t sysdb_master_domain_add_info(struct sss_domain_info *domain,
8d3578
 
8d3578
 errno_t sysdb_subdomain_delete(struct sysdb_ctx *sysdb, const char *name);
8d3578
 
8d3578
+errno_t sysdb_subdomain_content_delete(struct sysdb_ctx *sysdb,
8d3578
+                                       const char *name);
8d3578
+
8d3578
 errno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
8d3578
                              size_t *range_count,
8d3578
                              struct range_info ***range_list);
8d3578
@@ -892,6 +895,11 @@ int sysdb_delete_recursive(struct sysdb_ctx *sysdb,
8d3578
                            struct ldb_dn *dn,
8d3578
                            bool ignore_not_found);
8d3578
 
8d3578
+int sysdb_delete_recursive_with_filter(struct sysdb_ctx *sysdb,
8d3578
+                                       struct ldb_dn *dn,
8d3578
+                                       bool ignore_not_found,
8d3578
+                                       const char *filter);
8d3578
+
8d3578
 /* Mark entry as expired */
8d3578
 errno_t sysdb_mark_entry_as_expired_ldb_dn(struct sss_domain_info *dom,
8d3578
                                            struct ldb_dn *ldbdn);
8d3578
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
8d3578
index fa3842d8f..262e12380 100644
8d3578
--- a/src/db/sysdb_ops.c
8d3578
+++ b/src/db/sysdb_ops.c
8d3578
@@ -196,9 +196,10 @@ int sysdb_delete_entry(struct sysdb_ctx *sysdb,
8d3578
 
8d3578
 /* =Remove-Subentries-From-Sysdb=========================================== */
8d3578
 
8d3578
-int sysdb_delete_recursive(struct sysdb_ctx *sysdb,
8d3578
-                           struct ldb_dn *dn,
8d3578
-                           bool ignore_not_found)
8d3578
+int sysdb_delete_recursive_with_filter(struct sysdb_ctx *sysdb,
8d3578
+                                       struct ldb_dn *dn,
8d3578
+                                       bool ignore_not_found,
8d3578
+                                       const char *filter)
8d3578
 {
8d3578
     const char *no_attrs[] = { NULL };
8d3578
     struct ldb_message **msgs;
8d3578
@@ -219,7 +220,7 @@ int sysdb_delete_recursive(struct sysdb_ctx *sysdb,
8d3578
     }
8d3578
 
8d3578
     ret = sysdb_search_entry(tmp_ctx, sysdb, dn,
8d3578
-                             LDB_SCOPE_SUBTREE, "(distinguishedName=*)",
8d3578
+                             LDB_SCOPE_SUBTREE, filter,
8d3578
                              no_attrs, &msgs_count, &msgs);
8d3578
     if (ret) {
8d3578
         if (ignore_not_found && ret == ENOENT) {
8d3578
@@ -258,6 +259,14 @@ done:
8d3578
     return ret;
8d3578
 }
8d3578
 
8d3578
+int sysdb_delete_recursive(struct sysdb_ctx *sysdb,
8d3578
+                           struct ldb_dn *dn,
8d3578
+                           bool ignore_not_found)
8d3578
+{
8d3578
+    return sysdb_delete_recursive_with_filter(sysdb, dn, ignore_not_found,
8d3578
+                                              "(distinguishedName=*)");
8d3578
+}
8d3578
+
8d3578
 
8d3578
 /* =Search-Entry========================================================== */
8d3578
 
8d3578
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
8d3578
index af838b44c..0ca6a611f 100644
8d3578
--- a/src/db/sysdb_subdomains.c
8d3578
+++ b/src/db/sysdb_subdomains.c
8d3578
@@ -1250,7 +1250,9 @@ done:
8d3578
     return ret;
8d3578
 }
8d3578
 
8d3578
-errno_t sysdb_subdomain_delete(struct sysdb_ctx *sysdb, const char *name)
8d3578
+static errno_t sysdb_subdomain_delete_with_filter(struct sysdb_ctx *sysdb,
8d3578
+                                                  const char *name,
8d3578
+                                                  const char *filter)
8d3578
 {
8d3578
     TALLOC_CTX *tmp_ctx = NULL;
8d3578
     struct ldb_dn *dn;
8d3578
@@ -1269,7 +1271,7 @@ errno_t sysdb_subdomain_delete(struct sysdb_ctx *sysdb, const char *name)
8d3578
         goto done;
8d3578
     }
8d3578
 
8d3578
-    ret = sysdb_delete_recursive(sysdb, dn, true);
8d3578
+    ret = sysdb_delete_recursive_with_filter(sysdb, dn, true, filter);
8d3578
     if (ret != EOK) {
8d3578
         DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_recursive failed.\n");
8d3578
         goto done;
8d3578
@@ -1280,6 +1282,20 @@ done:
8d3578
     return ret;
8d3578
 }
8d3578
 
8d3578
+errno_t sysdb_subdomain_delete(struct sysdb_ctx *sysdb, const char *name)
8d3578
+{
8d3578
+    return sysdb_subdomain_delete_with_filter(sysdb, name,
8d3578
+                                              "(distinguishedName=*)");
8d3578
+}
8d3578
+
8d3578
+errno_t sysdb_subdomain_content_delete(struct sysdb_ctx *sysdb,
8d3578
+                                       const char *name)
8d3578
+{
8d3578
+    const char *filter = "(|("SYSDB_UC")("SYSDB_GC"))";
8d3578
+
8d3578
+    return sysdb_subdomain_delete_with_filter(sysdb, name, filter);
8d3578
+}
8d3578
+
8d3578
 errno_t
8d3578
 sysdb_domain_get_domain_resolution_order(TALLOC_CTX *mem_ctx,
8d3578
                                          struct sysdb_ctx *sysdb,
8d3578
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
8d3578
index 22460d9db..45a278e2a 100644
8d3578
--- a/src/tests/sysdb-tests.c
8d3578
+++ b/src/tests/sysdb-tests.c
8d3578
@@ -6204,6 +6204,93 @@ START_TEST(test_sysdb_subdomain_store_user)
8d3578
 }
8d3578
 END_TEST
8d3578
 
8d3578
+START_TEST(test_sysdb_subdomain_content_delete)
8d3578
+{
8d3578
+    struct sysdb_test_ctx *test_ctx;
8d3578
+    errno_t ret;
8d3578
+    struct sss_domain_info *subdomain = NULL;
8d3578
+    struct ldb_result *results = NULL;
8d3578
+    struct ldb_dn *base_dn = NULL;
8d3578
+    struct ldb_dn *check_dn = NULL;
8d3578
+    struct ldb_dn *check_dom_dn = NULL;
8d3578
+    struct test_data *data;
8d3578
+    char *alias;
8d3578
+
8d3578
+    ret = setup_sysdb_tests(&test_ctx);
8d3578
+    fail_if(ret != EOK, "Could not set up the test");
8d3578
+
8d3578
+    subdomain = new_subdomain(test_ctx, test_ctx->domain,
8d3578
+                              testdom[0], testdom[1], testdom[2], testdom[3],
8d3578
+                              MPG_DISABLED, false, NULL, NULL, 0, NULL, true);
8d3578
+    fail_unless(subdomain != NULL, "Failed to create new subdomain.");
8d3578
+    ret = sysdb_subdomain_store(test_ctx->sysdb,
8d3578
+                                testdom[0], testdom[1], testdom[2], testdom[3],
8d3578
+                                false, false, NULL, 0, NULL);
8d3578
+    fail_if(ret != EOK, "Could not set up the test (test subdom)");
8d3578
+
8d3578
+    ret = sysdb_update_subdomains(test_ctx->domain, NULL);
8d3578
+    fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
8d3578
+                            ret, strerror(ret));
8d3578
+
8d3578
+    data = test_data_new_user(test_ctx, 12345);
8d3578
+    fail_if(data == NULL);
8d3578
+    data->username = test_asprintf_fqname(data, subdomain, "SubDomUser");
8d3578
+
8d3578
+    alias = test_asprintf_fqname(data, subdomain, "subdomuser");
8d3578
+    fail_if(alias == NULL);
8d3578
+
8d3578
+    ret = sysdb_attrs_add_string(data->attrs, SYSDB_NAME_ALIAS, alias);
8d3578
+    fail_unless(ret == EOK, "sysdb_store_user failed.");
8d3578
+
8d3578
+    ret = sysdb_store_user(subdomain, data->username,
8d3578
+                           NULL, data->uid, 0, "Sub Domain User",
8d3578
+                           "/home/subdomuser", "/bin/bash",
8d3578
+                           NULL, data->attrs, NULL, -1, 0);
8d3578
+    fail_unless(ret == EOK, "sysdb_store_user failed.");
8d3578
+
8d3578
+    base_dn =ldb_dn_new(test_ctx, test_ctx->sysdb->ldb, "cn=sysdb");
8d3578
+    fail_unless(base_dn != NULL);
8d3578
+
8d3578
+    check_dn = sysdb_user_dn(data, subdomain, data->username);
8d3578
+    fail_unless(check_dn != NULL);
8d3578
+
8d3578
+    ret = ldb_search(test_ctx->sysdb->ldb, test_ctx, &results, base_dn,
8d3578
+                     LDB_SCOPE_SUBTREE, NULL, "name=%s", data->username);
8d3578
+    fail_unless(ret == EOK, "ldb_search failed.");
8d3578
+    fail_unless(results->count == 1, "Unexpected number of results, "
8d3578
+                                     "expected [%d], got [%d]",
8d3578
+                                     1, results->count);
8d3578
+    fail_unless(ldb_dn_compare(results->msgs[0]->dn, check_dn) == 0,
8d3578
+                "Unexpected DN returned");
8d3578
+
8d3578
+    ret = sysdb_subdomain_content_delete(test_ctx->sysdb, testdom[0]);
8d3578
+    fail_unless(ret == EOK, "sysdb_subdomain_content_delete failed.");
8d3578
+
8d3578
+    /* Check if user is removed */
8d3578
+    ret = ldb_search(test_ctx->sysdb->ldb, test_ctx, &results, base_dn,
8d3578
+                     LDB_SCOPE_SUBTREE, NULL, "name=%s", alias);
8d3578
+    fail_unless(ret == EOK, "ldb_search failed.");
8d3578
+    fail_unless(results->count == 0, "Unexpected number of results, "
8d3578
+                                     "expected [%d], got [%d]",
8d3578
+                                     0, results->count);
8d3578
+
8d3578
+    check_dom_dn = ldb_dn_new_fmt(test_ctx, test_ctx->sysdb->ldb,
8d3578
+                                  SYSDB_DOM_BASE, testdom[0]);
8d3578
+    fail_unless(check_dom_dn != NULL, "ldb_dn_new_fmt failed.");
8d3578
+
8d3578
+    /* Check if domain object is still present */
8d3578
+    ret = ldb_search(test_ctx->sysdb->ldb, test_ctx, &results, base_dn,
8d3578
+                     LDB_SCOPE_SUBTREE, NULL, "cn=%s", testdom[0]);
8d3578
+    fail_unless(ret == EOK, "ldb_search failed.");
8d3578
+    fail_unless(results->count == 1, "Unexpected number of results, "
8d3578
+                                     "expected [%d], got [%d]",
8d3578
+                                     1, results->count);
8d3578
+    fail_unless(ldb_dn_compare(results->msgs[0]->dn, check_dom_dn) == 0,
8d3578
+                "Unexpected DN returned");
8d3578
+
8d3578
+}
8d3578
+END_TEST
8d3578
+
8d3578
 START_TEST(test_sysdb_subdomain_user_ops)
8d3578
 {
8d3578
     struct sysdb_test_ctx *test_ctx;
8d3578
@@ -7574,6 +7661,7 @@ Suite *create_sysdb_suite(void)
8d3578
     TCase *tc_subdomain = tcase_create("SYSDB sub-domain Tests");
8d3578
 
8d3578
     tcase_add_test(tc_subdomain, test_sysdb_subdomain_store_user);
8d3578
+    tcase_add_test(tc_subdomain, test_sysdb_subdomain_content_delete);
8d3578
     tcase_add_test(tc_subdomain, test_sysdb_subdomain_user_ops);
8d3578
     tcase_add_test(tc_subdomain, test_sysdb_subdomain_group_ops);
8d3578
 
8d3578
-- 
8d3578
2.20.1
8d3578