>From 7c454bc2a737be05068418a5eef7fe9446bb5fa8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
Date: Wed, 9 Sep 2015 14:37:48 +0200
Subject: [PATCH] util: Include disabled domains in link_forest_roots

Ticket:
https://fedorahosted.org/sssd/ticket/2673
---
 src/db/sysdb_subdomains.c     |  6 +++---
 src/tests/cmocka/test_utils.c |  3 +++
 src/util/domain_info_utils.c  | 21 ++++++++++++++++++---
 src/util/util.h               |  3 +++
 4 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 142520c..57b4d4b 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -190,11 +190,11 @@ static void link_forest_roots(struct sss_domain_info *domain)
     struct sss_domain_info *d;
     struct sss_domain_info *dd;
 
-    for (d = domain; d; d = get_next_domain(d, true)) {
+    for (d = domain; d; d = get_next_domain_ex(d, true, false)) {
         d->forest_root = NULL;
     }
 
-    for (d = domain; d; d = get_next_domain(d, true)) {
+    for (d = domain; d; d = get_next_domain_ex(d, true, false)) {
         if (d->forest_root != NULL) {
             continue;
         }
@@ -203,7 +203,7 @@ static void link_forest_roots(struct sss_domain_info *domain)
             d->forest_root = d;
             DEBUG(SSSDBG_TRACE_INTERNAL, "[%s] is a forest root\n", d->name);
 
-            for (dd = domain; dd; dd = get_next_domain(dd, true)) {
+            for (dd = domain; dd; dd = get_next_domain_ex(dd, true, false)) {
                 if (dd->forest_root != NULL) {
                     continue;
                 }
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index c7ebe09..da70675 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -583,6 +583,9 @@ static void test_get_next_domain_disabled(void **state)
 
     dom = get_next_domain(test_ctx->dom_list, true);
     assert_null(dom);
+
+    dom = get_next_domain_ex(test_ctx->dom_list, true, false);
+    assert_non_null(dom);
 }
 
 struct name_init_test_ctx {
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
index 4eabcff..60f3064 100644
--- a/src/util/domain_info_utils.c
+++ b/src/util/domain_info_utils.c
@@ -34,8 +34,9 @@ struct sss_domain_info *get_domains_head(struct sss_domain_info *domain)
     return dom;
 }
 
-struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
-                                        bool descend)
+struct sss_domain_info *get_next_domain_ex(struct sss_domain_info *domain,
+                                           bool descend,
+                                           bool skip_disabled_domains)
 {
     struct sss_domain_info *dom;
 
@@ -50,12 +51,26 @@ struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
         } else {
             dom = NULL;
         }
-        if (dom && !dom->disabled) break;
+        if (dom) {
+            if (skip_disabled_domains && dom->disabled) {
+                continue;
+            } else {
+                /* Next domain found. */
+                break;
+            }
+        }
     }
 
     return dom;
 }
 
+struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
+                                        bool descend)
+{
+    return get_next_domain_ex(domain, descend, true);
+}
+
+
 bool subdomain_enumerates(struct sss_domain_info *parent,
                           const char *sd_name)
 {
diff --git a/src/util/util.h b/src/util/util.h
index 3e29e74..fff757b 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -559,6 +559,9 @@ void to_sized_string(struct sized_string *out, const char *in);
 /* from domain_info.c */
 struct sss_domain_info *get_domains_head(struct sss_domain_info *domain);
 
+struct sss_domain_info *get_next_domain_ex(struct sss_domain_info *domain,
+                                           bool descend,
+                                           bool skip_disabled_domains);
 struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
                                         bool descend);
 struct sss_domain_info *find_domain_by_name(struct sss_domain_info *domain,
-- 
2.1.0

