Blob Blame History Raw
From ecfb7df52fd3b0edf8549d42cfa6b378407fb982 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Fri, 8 Oct 2021 13:14:30 +0200
Subject: [PATCH 79/83] ad: move current site and forest name to a more global
 context
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently only during the DNS discovery steps the stored forest and site
name are reused to avoid redundant lookups. Since those names are needed
in other areas of the code as well it would be good to make them
available in a more global context.

Resolves: https://github.com/SSSD/sssd/issues/5820

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
(cherry picked from commit 4508ef5f7183c640191393605ea163044d9ac267)

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
---
 src/providers/ad/ad_cldap_ping.c          | 15 ++++++-----
 src/providers/ad/ad_common.h              |  4 +++
 src/providers/ad/ad_init.c                |  1 +
 src/providers/ad/ad_srv.c                 | 33 +++++++++++++----------
 src/providers/ad/ad_srv.h                 |  4 +--
 src/providers/ad/ad_subdomains.c          |  1 +
 src/providers/ipa/ipa_subdomains_server.c |  1 +
 7 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/src/providers/ad/ad_cldap_ping.c b/src/providers/ad/ad_cldap_ping.c
index ab234f4d7..100d448f5 100644
--- a/src/providers/ad/ad_cldap_ping.c
+++ b/src/providers/ad/ad_cldap_ping.c
@@ -601,10 +601,12 @@ struct tevent_req *ad_cldap_ping_send(TALLOC_CTX *mem_ctx,
     }
 
     if (!srv_ctx->renew_site) {
-        state->site = talloc_strdup(state, srv_ctx->current_site);
-        state->forest = talloc_strdup(state, srv_ctx->current_forest);
-        if ((srv_ctx->current_site != NULL && state->site == NULL)
-                || (srv_ctx->current_forest != NULL && state->forest == NULL)) {
+        state->site = talloc_strdup(state, srv_ctx->ad_options->current_site);
+        state->forest = talloc_strdup(state,
+                                      srv_ctx->ad_options->current_forest);
+        if ((srv_ctx->ad_options->current_site != NULL && state->site == NULL)
+                || (srv_ctx->ad_options->current_forest != NULL
+                                    && state->forest == NULL)) {
             DEBUG(SSSDBG_OP_FAILURE,
                   "Failed to copy current site or forest name.\n");
             ret = ENOMEM;
@@ -629,9 +631,10 @@ struct tevent_req *ad_cldap_ping_send(TALLOC_CTX *mem_ctx,
     state->discovery_domain = discovery_domain;
 
     /* If possible, lookup the information in the current site first. */
-    if (srv_ctx->current_site != NULL) {
+    if (srv_ctx->ad_options->current_site != NULL) {
         state->all_tried = false;
-        domain = ad_site_dns_discovery_domain(state, srv_ctx->current_site,
+        domain = ad_site_dns_discovery_domain(state,
+                                              srv_ctx->ad_options->current_site,
                                               discovery_domain);
         if (domain == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!");
diff --git a/src/providers/ad/ad_common.h b/src/providers/ad/ad_common.h
index 815b41419..311b84f4c 100644
--- a/src/providers/ad/ad_common.h
+++ b/src/providers/ad/ad_common.h
@@ -105,6 +105,10 @@ struct ad_options {
     /* Dynamic DNS updates */
     struct be_resolv_ctx *be_res;
     struct be_nsupdate_ctx *dyndns_ctx;
+
+    /* Discovered site and forest names */
+    const char *current_site;
+    const char *current_forest;
 };
 
 errno_t
diff --git a/src/providers/ad/ad_init.c b/src/providers/ad/ad_init.c
index 5abd28b7c..9f258e2bd 100644
--- a/src/providers/ad/ad_init.c
+++ b/src/providers/ad/ad_init.c
@@ -206,6 +206,7 @@ static errno_t ad_init_srv_plugin(struct be_ctx *be_ctx,
 
     srv_ctx = ad_srv_plugin_ctx_init(be_ctx, be_ctx, be_ctx->be_res,
                                      default_host_dbs, ad_options->id,
+                                     ad_options,
                                      hostname, ad_domain,
                                      ad_site_override);
     if (srv_ctx == NULL) {
diff --git a/src/providers/ad/ad_srv.c b/src/providers/ad/ad_srv.c
index e58c19aac..a10c6a247 100644
--- a/src/providers/ad/ad_srv.c
+++ b/src/providers/ad/ad_srv.c
@@ -130,6 +130,7 @@ ad_srv_plugin_ctx_init(TALLOC_CTX *mem_ctx,
                        struct be_resolv_ctx *be_res,
                        enum host_database *host_dbs,
                        struct sdap_options *opts,
+                       struct ad_options *ad_options,
                        const char *hostname,
                        const char *ad_domain,
                        const char *ad_site_override)
@@ -147,6 +148,7 @@ ad_srv_plugin_ctx_init(TALLOC_CTX *mem_ctx,
     ctx->host_dbs = host_dbs;
     ctx->opts = opts;
     ctx->renew_site = true;
+    ctx->ad_options = ad_options;
 
     ctx->hostname = talloc_strdup(ctx, hostname);
     if (ctx->hostname == NULL) {
@@ -164,18 +166,20 @@ ad_srv_plugin_ctx_init(TALLOC_CTX *mem_ctx,
             goto fail;
         }
 
-        ctx->current_site = talloc_strdup(ctx, ad_site_override);
-        if (ctx->current_site == NULL) {
+        ctx->ad_options->current_site = talloc_strdup(ctx->ad_options,
+                                                      ad_site_override);
+        if (ctx->ad_options->current_site == NULL) {
             goto fail;
         }
     } else {
-        ret = sysdb_get_site(ctx, be_ctx->domain, &ctx->current_site);
+        ret = sysdb_get_site(ctx->ad_options, be_ctx->domain,
+                             &ctx->ad_options->current_site);
         if (ret != EOK) {
             /* Not fatal. */
             DEBUG(SSSDBG_MINOR_FAILURE,
                   "Unable to get current site from cache [%d]: %s\n",
                   ret, sss_strerror(ret));
-            ctx->current_site = NULL;
+            ctx->ad_options->current_site = NULL;
         }
     }
 
@@ -203,34 +207,35 @@ ad_srv_plugin_ctx_switch_site(struct ad_srv_plugin_ctx *ctx,
 
     /* Switch forest. */
     if (new_forest != NULL
-        && (ctx->current_forest == NULL
-            || strcmp(ctx->current_forest, new_forest) != 0)) {
-        forest = talloc_strdup(ctx, new_forest);
+        && (ctx->ad_options->current_forest == NULL
+            || strcmp(ctx->ad_options->current_forest, new_forest) != 0)) {
+        forest = talloc_strdup(ctx->ad_options, new_forest);
         if (forest == NULL) {
             return ENOMEM;
         }
 
-        talloc_zfree(ctx->current_forest);
-        ctx->current_forest = forest;
+        talloc_zfree(ctx->ad_options->current_forest);
+        ctx->ad_options->current_forest = forest;
     }
 
     if (new_site == NULL) {
         return EOK;
     }
 
-    if (ctx->current_site != NULL && strcmp(ctx->current_site, new_site) == 0) {
+    if (ctx->ad_options->current_site != NULL
+                    && strcmp(ctx->ad_options->current_site, new_site) == 0) {
         return EOK;
     }
 
-    site = talloc_strdup(ctx, new_site);
+    site = talloc_strdup(ctx->ad_options, new_site);
     if (site == NULL) {
         return ENOMEM;
     }
 
-    talloc_zfree(ctx->current_site);
-    ctx->current_site = site;
+    talloc_zfree(ctx->ad_options->current_site);
+    ctx->ad_options->current_site = site;
 
-    ret = sysdb_set_site(ctx->be_ctx->domain, ctx->current_site);
+    ret = sysdb_set_site(ctx->be_ctx->domain, ctx->ad_options->current_site);
     if (ret != EOK) {
         /* Not fatal. */
         DEBUG(SSSDBG_MINOR_FAILURE, "Unable to store site information "
diff --git a/src/providers/ad/ad_srv.h b/src/providers/ad/ad_srv.h
index 3c6a779ea..fd70f15a8 100644
--- a/src/providers/ad/ad_srv.h
+++ b/src/providers/ad/ad_srv.h
@@ -26,11 +26,10 @@ struct ad_srv_plugin_ctx {
     struct be_resolv_ctx *be_res;
     enum host_database *host_dbs;
     struct sdap_options *opts;
+    struct ad_options *ad_options;
     const char *hostname;
     const char *ad_domain;
     const char *ad_site_override;
-    const char *current_site;
-    const char *current_forest;
 
     bool renew_site;
 };
@@ -41,6 +40,7 @@ ad_srv_plugin_ctx_init(TALLOC_CTX *mem_ctx,
                        struct be_resolv_ctx *be_res,
                        enum host_database *host_dbs,
                        struct sdap_options *opts,
+                       struct ad_options *ad_options,
                        const char *hostname,
                        const char *ad_domain,
                        const char *ad_site_override);
diff --git a/src/providers/ad/ad_subdomains.c b/src/providers/ad/ad_subdomains.c
index 5be7c2003..8a331c503 100644
--- a/src/providers/ad/ad_subdomains.c
+++ b/src/providers/ad/ad_subdomains.c
@@ -416,6 +416,7 @@ ad_subdom_ad_ctx_new(struct be_ctx *be_ctx,
     srv_ctx = ad_srv_plugin_ctx_init(be_ctx, be_ctx, be_ctx->be_res,
                                      default_host_dbs,
                                      ad_id_ctx->ad_options->id,
+                                     ad_id_ctx->ad_options,
                                      hostname,
                                      ad_domain,
                                      ad_site_override);
diff --git a/src/providers/ipa/ipa_subdomains_server.c b/src/providers/ipa/ipa_subdomains_server.c
index f0d8a6a20..ed363c22f 100644
--- a/src/providers/ipa/ipa_subdomains_server.c
+++ b/src/providers/ipa/ipa_subdomains_server.c
@@ -342,6 +342,7 @@ ipa_ad_ctx_new(struct be_ctx *be_ctx,
     srv_ctx = ad_srv_plugin_ctx_init(be_ctx, be_ctx, be_ctx->be_res,
                                      default_host_dbs,
                                      ad_id_ctx->ad_options->id,
+                                     ad_id_ctx->ad_options,
                                      id_ctx->server_mode->hostname,
                                      ad_domain,
                                      ad_site_override);
-- 
2.26.3