Blame SOURCES/0006-utils-add-SSS_GND_SUBDOMAINS-flag-for-get_next_domai.patch

bac598
From 385af99ff4d5a75d0c1edc9ad830da3eb7478295 Mon Sep 17 00:00:00 2001
bac598
From: Sumit Bose <sbose@redhat.com>
bac598
Date: Thu, 8 Oct 2020 17:57:29 +0200
bac598
Subject: [PATCH 6/8] utils: add SSS_GND_SUBDOMAINS flag for get_next_domain()
bac598
bac598
To allow to only iterate over a singel domain an its sub-domains a new
bac598
flag is added to get_next_domain().
bac598
bac598
Resolves: https://github.com/SSSD/sssd/issues/5238
bac598
bac598
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
bac598
---
bac598
 src/tests/cmocka/test_utils.c | 31 +++++++++++++++++++++++++++++++
bac598
 src/util/domain_info_utils.c  | 10 +++++++---
bac598
 src/util/util.h               |  4 ++++
bac598
 3 files changed, 42 insertions(+), 3 deletions(-)
bac598
bac598
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
bac598
index 945f5cb44..d77a972c1 100644
bac598
--- a/src/tests/cmocka/test_utils.c
bac598
+++ b/src/tests/cmocka/test_utils.c
bac598
@@ -877,6 +877,37 @@ static void test_get_next_domain_flags(void **state)
bac598
 
bac598
     dom = get_next_domain(dom, gnd_flags);
bac598
     assert_null(dom);
bac598
+
bac598
+    /* Descend only to subdomains */
bac598
+    gnd_flags = SSS_GND_SUBDOMAINS | SSS_GND_INCLUDE_DISABLED;
bac598
+
bac598
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
bac598
+    assert_non_null(dom);
bac598
+    assert_string_equal(dom->name, "sub1a");
bac598
+
bac598
+    dom = get_next_domain(dom, gnd_flags);
bac598
+    assert_null(dom);
bac598
+
bac598
+    dom = find_domain_by_name_ex(test_ctx->dom_list, "dom2", true,
bac598
+                                 SSS_GND_ALL_DOMAINS);
bac598
+    assert_non_null(dom);
bac598
+    assert_string_equal(dom->name, "dom2");
bac598
+
bac598
+    dom = get_next_domain(dom, gnd_flags);
bac598
+    assert_non_null(dom);
bac598
+    assert_string_equal(dom->name, "sub2a");
bac598
+
bac598
+    dom = get_next_domain(dom, gnd_flags);
bac598
+    assert_non_null(dom);
bac598
+    assert_string_equal(dom->name, "sub2b");
bac598
+
bac598
+    dom = get_next_domain(dom, gnd_flags);
bac598
+    assert_null(dom);
bac598
+
bac598
+    /* Expect NULL if the domain has no sub-domains */
bac598
+    test_ctx->dom_list->subdomains = NULL;
bac598
+    dom = get_next_domain(test_ctx->dom_list, gnd_flags);
bac598
+    assert_null(dom);
bac598
 }
bac598
 
bac598
 struct name_init_test_ctx {
bac598
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
bac598
index aa3582f03..4d4726daa 100644
bac598
--- a/src/util/domain_info_utils.c
bac598
+++ b/src/util/domain_info_utils.c
bac598
@@ -39,16 +39,20 @@ struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
bac598
                                         uint32_t gnd_flags)
bac598
 {
bac598
     struct sss_domain_info *dom;
bac598
-    bool descend = gnd_flags & SSS_GND_DESCEND;
bac598
+    bool descend = gnd_flags & (SSS_GND_DESCEND | SSS_GND_SUBDOMAINS);
bac598
     bool include_disabled = gnd_flags & SSS_GND_INCLUDE_DISABLED;
bac598
+    bool only_subdomains = gnd_flags & SSS_GND_SUBDOMAINS;
bac598
 
bac598
     dom = domain;
bac598
     while (dom) {
bac598
         if (descend && dom->subdomains) {
bac598
             dom = dom->subdomains;
bac598
-        } else if (dom->next) {
bac598
+        } else if (dom->next && only_subdomains && IS_SUBDOMAIN(dom)) {
bac598
             dom = dom->next;
bac598
-        } else if (descend && IS_SUBDOMAIN(dom) && dom->parent->next) {
bac598
+        } else if (dom->next && !only_subdomains) {
bac598
+            dom = dom->next;
bac598
+        } else if (descend && !only_subdomains && IS_SUBDOMAIN(dom)
bac598
+                            && dom->parent->next) {
bac598
             dom = dom->parent->next;
bac598
         } else {
bac598
             dom = NULL;
bac598
diff --git a/src/util/util.h b/src/util/util.h
bac598
index fbcac5cd0..581c0edfb 100644
bac598
--- a/src/util/util.h
bac598
+++ b/src/util/util.h
bac598
@@ -565,7 +565,11 @@ struct sss_domain_info *get_domains_head(struct sss_domain_info *domain);
bac598
 
bac598
 #define SSS_GND_DESCEND 0x01
bac598
 #define SSS_GND_INCLUDE_DISABLED 0x02
bac598
+/* Descend to sub-domains of current domain but do not go to next parent */
bac598
+#define SSS_GND_SUBDOMAINS 0x04
bac598
 #define SSS_GND_ALL_DOMAINS (SSS_GND_DESCEND | SSS_GND_INCLUDE_DISABLED)
bac598
+#define SSS_GND_ALL_SUBDOMAINS (SSS_GND_SUBDOMAINS | SSS_GND_INCLUDE_DISABLED)
bac598
+
bac598
 struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
bac598
                                         uint32_t gnd_flags);
bac598
 struct sss_domain_info *find_domain_by_name(struct sss_domain_info *domain,
bac598
-- 
bac598
2.21.3
bac598