From e6b6c461ec7d4e91299b1e8290e1998e21aa9576 Mon Sep 17 00:00:00 2001
From: Petr Cech <pcech@redhat.com>
Date: Tue, 21 Jun 2016 08:34:15 +0200
Subject: [PATCH 2/5] AD_PROVIDER: Initializing of ad_enabled_domains

We add ad_enabled_domains into ad_subdomains_ctx.

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

diff --git a/src/providers/ad/ad_subdomains.c b/src/providers/ad/ad_subdomains.c
index 5b0bee86665a46f45fde5ec1034a9ccf8700d13a..af438078deb8b103f8a24dbef7cf818fab9df538 100644
--- a/src/providers/ad/ad_subdomains.c
+++ b/src/providers/ad/ad_subdomains.c
@@ -57,6 +57,91 @@
 /* do not refresh more often than every 5 seconds for now */
 #define AD_SUBDOMAIN_REFRESH_LIMIT 5
 
+static errno_t ad_get_enabled_domains(TALLOC_CTX *mem_ctx,
+                                      struct ad_id_ctx *ad_id_ctx,
+                                      const char *ad_domain,
+                                      const char ***_ad_enabled_domains)
+{
+    int ret;
+    const char *str;
+    const char *option_name;
+    char **domains = NULL;
+    const char **list = NULL;
+    int count;
+    bool is_ad_in_domains;
+    TALLOC_CTX *tmp_ctx = NULL;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        return ENOMEM;
+    }
+
+    str = dp_opt_get_cstring(ad_id_ctx->ad_options->basic, AD_ENABLED_DOMAINS);
+    if (str == NULL) {
+        _ad_enabled_domains = NULL;
+        ret = EOK;
+        goto done;
+    }
+
+    count = 0;
+    ret = split_on_separator(tmp_ctx, str, ',', true, true, &domains, &count);
+    if (ret != EOK) {
+        option_name = ad_id_ctx->ad_options->basic[AD_ENABLED_DOMAINS].opt_name;
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to parse option [%s], [%i] [%s]!\n",
+                                   option_name, ret, sss_strerror(ret));
+        ret = EINVAL;
+        goto done;
+    }
+
+    list = talloc_array_size(tmp_ctx, sizeof(char*), count);
+    if (list == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    is_ad_in_domains = false;
+    for (int i = 0; i < count; i++) {
+        list[i] = talloc_strdup(list, domains[i]);
+        if (list[i] == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        is_ad_in_domains += strcmp(ad_domain, domains[i]) == 0 ? true : false;
+    }
+
+    if (is_ad_in_domains == false) {
+        list = talloc_realloc(tmp_ctx, list, const char*, count + 1);
+        if (list == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        list[count] = talloc_strdup(list, ad_domain);
+        if (list[count] == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        count++;
+    }
+
+    list = talloc_realloc(tmp_ctx, list, const char*, count + 1);
+    if (list == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+    list[count] = NULL;
+    count++;
+
+    *_ad_enabled_domains = talloc_steal(mem_ctx, list);
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 static errno_t
 ad_subdom_ad_ctx_new(struct be_ctx *be_ctx,
                      struct ad_id_ctx *id_ctx,
@@ -164,6 +249,7 @@ struct ad_subdomains_ctx {
 
     struct sdap_domain *sdom;
     char *domain_name;
+    const char **ad_enabled_domains;
 
     time_t last_refreshed;
 };
@@ -1346,6 +1432,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;
     time_t period;
     errno_t ret;
 
@@ -1357,6 +1444,12 @@ errno_t ad_subdomains_init(TALLOC_CTX *mem_ctx,
         return ENOMEM;
     }
 
+    ret = ad_get_enabled_domains(sd_ctx, ad_id_ctx, ad_domain,
+                                 &ad_enabled_domains);
+    if (ret != EOK) {
+        return EINVAL;
+    }
+
     sd_ctx->be_ctx = be_ctx;
     sd_ctx->sdom = ad_id_ctx->sdap_id_ctx->opts->sdom;
     sd_ctx->sdap_id_ctx = ad_id_ctx->sdap_id_ctx;
@@ -1365,6 +1458,7 @@ errno_t ad_subdomains_init(TALLOC_CTX *mem_ctx,
         DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
         return ENOMEM;
     }
+    sd_ctx->ad_enabled_domains = ad_enabled_domains;
     sd_ctx->ad_id_ctx = ad_id_ctx;
 
     dp_set_method(dp_methods, DPM_DOMAINS_HANDLER,
-- 
2.5.5

