From 2e1a494e21317062898722861e1f03f32a2e8909 Mon Sep 17 00:00:00 2001
From: Petr Cech <pcech@redhat.com>
Date: Mon, 27 Jun 2016 11:51:30 +0200
Subject: [PATCH 4/5] AD_PROVIDER: ad_enabled_domains - other then master

We can skip looking up other domains if
option ad_enabled_domains doesn't contain them.

Resolves:
https://fedorahosted.org/sssd/ticket/2828
---
 src/providers/ad/ad_subdomains.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/providers/ad/ad_subdomains.c b/src/providers/ad/ad_subdomains.c
index 7e35ecd2c67cd033e3cdef3d14aaee6c00a50964..ceb60a2b8be0eb8bb35d1b9f078c026a73515a92 100644
--- a/src/providers/ad/ad_subdomains.c
+++ b/src/providers/ad/ad_subdomains.c
@@ -485,6 +485,7 @@ done:
 
 static errno_t ad_subdomains_process(TALLOC_CTX *mem_ctx,
                                      struct sss_domain_info *domain,
+                                     const char **enabled_domains_list,
                                      size_t nsd, struct sysdb_attrs **sd,
                                      struct sysdb_attrs *root,
                                      size_t *_nsd_out,
@@ -493,9 +494,10 @@ static errno_t ad_subdomains_process(TALLOC_CTX *mem_ctx,
     size_t i, sdi;
     struct sysdb_attrs **sd_out;
     const char *sd_name;
+    const bool is_sd_filtered = (enabled_domains_list == NULL) ? false : true;
     errno_t ret;
 
-    if (root == NULL) {
+    if (root == NULL && is_sd_filtered == false) {
         /* We are connected directly to the root domain. The 'sd'
          * list is complete and we can just use it
          */
@@ -522,6 +524,14 @@ static errno_t ad_subdomains_process(TALLOC_CTX *mem_ctx,
             goto fail;
         }
 
+        if (is_sd_filtered == true) {
+            if (string_in_list(sd_name,
+                               discard_const_p(char *, &enabled_domains_list),
+                               true) == false) {
+                continue;
+            }
+        }
+
         if (strcasecmp(sd_name, domain->name) == 0) {
             DEBUG(SSSDBG_TRACE_INTERNAL,
                   "Not including primary domain %s in the subdomain list\n",
@@ -534,9 +544,12 @@ static errno_t ad_subdomains_process(TALLOC_CTX *mem_ctx,
     }
 
     /* Now include the root */
-    sd_out[sdi] = talloc_steal(sd_out, root);
+    if (root != NULL) {
+        sd_out[sdi] = talloc_steal(sd_out, root);
+        sdi++;
+    }
 
-    *_nsd_out = sdi+1;
+    *_nsd_out = sdi;
     *_sd_out = sd_out;
     return EOK;
 
@@ -782,6 +795,7 @@ static void ad_get_slave_domain_done(struct tevent_req *subreq)
      * subdomains.
      */
     ret = ad_subdomains_process(state, state->be_ctx->domain,
+                                state->sd_ctx->ad_enabled_domains,
                                 reply_count, reply, state->root_attrs,
                                 &nsubdoms, &subdoms);
     if (ret != EOK) {
@@ -1441,7 +1455,7 @@ errno_t ad_subdomains_init(TALLOC_CTX *mem_ctx,
 {
     struct ad_subdomains_ctx *sd_ctx;
     const char *ad_domain;
-    char **ad_enabled_domains = NULL;
+    const char **ad_enabled_domains = NULL;
     time_t period;
     errno_t ret;
 
-- 
2.7.4

