Blame SOURCES/0081-ad-make-ad_srv_plugin_ctx_switch_site-public.patch

5a92ae
From 664ae9d2247b5139d2286975228baa0cea39a8e4 Mon Sep 17 00:00:00 2001
5a92ae
From: Sumit Bose <sbose@redhat.com>
5a92ae
Date: Wed, 20 Oct 2021 13:59:40 +0200
5a92ae
Subject: [PATCH 81/83] ad: make ad_srv_plugin_ctx_switch_site() public
5a92ae
MIME-Version: 1.0
5a92ae
Content-Type: text/plain; charset=UTF-8
5a92ae
Content-Transfer-Encoding: 8bit
5a92ae
5a92ae
If the name of the AD DCs are given explicitly with the ad_server option
5a92ae
the forest and site lookups are not done in the discovery phase, which
5a92ae
is skipped, but with a netlogon query on the current connection. This
5a92ae
patch makes sure the results are stored in the same way as during the
5a92ae
discovery step.
5a92ae
5a92ae
Resolves: https://github.com/SSSD/sssd/issues/5820
5a92ae
5a92ae
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
5a92ae
(cherry picked from commit 918abaf37d7f13d72b29863933e133bcbd24d87c)
5a92ae
5a92ae
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
5a92ae
---
5a92ae
 src/providers/ad/ad_common.c      | 48 +++++++++++++++++++++
5a92ae
 src/providers/ad/ad_common.h      |  3 ++
5a92ae
 src/providers/ad/ad_domain_info.h |  1 -
5a92ae
 src/providers/ad/ad_srv.c         | 70 ++++++-------------------------
5a92ae
 src/providers/ad/ad_subdomains.c  | 34 ++++++++++++++-
5a92ae
 5 files changed, 96 insertions(+), 60 deletions(-)
5a92ae
5a92ae
diff --git a/src/providers/ad/ad_common.c b/src/providers/ad/ad_common.c
5a92ae
index c99c4d110..4463349e4 100644
5a92ae
--- a/src/providers/ad/ad_common.c
5a92ae
+++ b/src/providers/ad/ad_common.c
5a92ae
@@ -1554,3 +1554,51 @@ done:
5a92ae
 
5a92ae
     return ret;
5a92ae
 }
5a92ae
+
5a92ae
+errno_t
5a92ae
+ad_options_switch_site(struct ad_options *ad_options, struct be_ctx *be_ctx,
5a92ae
+                       const char *new_site, const char *new_forest)
5a92ae
+{
5a92ae
+    const char *site;
5a92ae
+    const char *forest;
5a92ae
+    errno_t ret;
5a92ae
+
5a92ae
+    /* Switch forest. */
5a92ae
+    if (new_forest != NULL
5a92ae
+        && (ad_options->current_forest == NULL
5a92ae
+            || strcmp(ad_options->current_forest, new_forest) != 0)) {
5a92ae
+        forest = talloc_strdup(ad_options, new_forest);
5a92ae
+        if (forest == NULL) {
5a92ae
+            return ENOMEM;
5a92ae
+        }
5a92ae
+
5a92ae
+        talloc_zfree(ad_options->current_forest);
5a92ae
+        ad_options->current_forest = forest;
5a92ae
+    }
5a92ae
+
5a92ae
+    if (new_site == NULL) {
5a92ae
+        return EOK;
5a92ae
+    }
5a92ae
+
5a92ae
+    if (ad_options->current_site != NULL
5a92ae
+                    && strcmp(ad_options->current_site, new_site) == 0) {
5a92ae
+        return EOK;
5a92ae
+    }
5a92ae
+
5a92ae
+    site = talloc_strdup(ad_options, new_site);
5a92ae
+    if (site == NULL) {
5a92ae
+        return ENOMEM;
5a92ae
+    }
5a92ae
+
5a92ae
+    talloc_zfree(ad_options->current_site);
5a92ae
+    ad_options->current_site = site;
5a92ae
+
5a92ae
+    ret = sysdb_set_site(be_ctx->domain, ad_options->current_site);
5a92ae
+    if (ret != EOK) {
5a92ae
+        /* Not fatal. */
5a92ae
+        DEBUG(SSSDBG_MINOR_FAILURE, "Unable to store site information "
5a92ae
+              "[%d]: %s\n", ret, sss_strerror(ret));
5a92ae
+    }
5a92ae
+
5a92ae
+    return EOK;
5a92ae
+}
5a92ae
diff --git a/src/providers/ad/ad_common.h b/src/providers/ad/ad_common.h
5a92ae
index 311b84f4c..08fcc00fd 100644
5a92ae
--- a/src/providers/ad/ad_common.h
5a92ae
+++ b/src/providers/ad/ad_common.h
5a92ae
@@ -238,4 +238,7 @@ errno_t ad_inherit_opts_if_needed(struct dp_option *parent_opts,
5a92ae
 errno_t ad_refresh_init(struct be_ctx *be_ctx,
5a92ae
                         struct ad_id_ctx *id_ctx);
5a92ae
 
5a92ae
+errno_t
5a92ae
+ad_options_switch_site(struct ad_options *ad_options, struct be_ctx *be_ctx,
5a92ae
+                       const char *new_site, const char *new_forest);
5a92ae
 #endif /* AD_COMMON_H_ */
5a92ae
diff --git a/src/providers/ad/ad_domain_info.h b/src/providers/ad/ad_domain_info.h
5a92ae
index 631e543f5..cf601cff6 100644
5a92ae
--- a/src/providers/ad/ad_domain_info.h
5a92ae
+++ b/src/providers/ad/ad_domain_info.h
5a92ae
@@ -39,5 +39,4 @@ ad_domain_info_recv(struct tevent_req *req,
5a92ae
                       char **_id,
5a92ae
                       char **_site,
5a92ae
                       char **_forest);
5a92ae
-
5a92ae
 #endif /* _AD_DOMAIN_INFO_H_ */
5a92ae
diff --git a/src/providers/ad/ad_srv.c b/src/providers/ad/ad_srv.c
5a92ae
index a10c6a247..d45f1601e 100644
5a92ae
--- a/src/providers/ad/ad_srv.c
5a92ae
+++ b/src/providers/ad/ad_srv.c
5a92ae
@@ -196,55 +196,6 @@ fail:
5a92ae
     return NULL;
5a92ae
 }
5a92ae
 
5a92ae
-static errno_t
5a92ae
-ad_srv_plugin_ctx_switch_site(struct ad_srv_plugin_ctx *ctx,
5a92ae
-                              const char *new_site,
5a92ae
-                              const char *new_forest)
5a92ae
-{
5a92ae
-    const char *site;
5a92ae
-    const char *forest;
5a92ae
-    errno_t ret;
5a92ae
-
5a92ae
-    /* Switch forest. */
5a92ae
-    if (new_forest != NULL
5a92ae
-        && (ctx->ad_options->current_forest == NULL
5a92ae
-            || strcmp(ctx->ad_options->current_forest, new_forest) != 0)) {
5a92ae
-        forest = talloc_strdup(ctx->ad_options, new_forest);
5a92ae
-        if (forest == NULL) {
5a92ae
-            return ENOMEM;
5a92ae
-        }
5a92ae
-
5a92ae
-        talloc_zfree(ctx->ad_options->current_forest);
5a92ae
-        ctx->ad_options->current_forest = forest;
5a92ae
-    }
5a92ae
-
5a92ae
-    if (new_site == NULL) {
5a92ae
-        return EOK;
5a92ae
-    }
5a92ae
-
5a92ae
-    if (ctx->ad_options->current_site != NULL
5a92ae
-                    && strcmp(ctx->ad_options->current_site, new_site) == 0) {
5a92ae
-        return EOK;
5a92ae
-    }
5a92ae
-
5a92ae
-    site = talloc_strdup(ctx->ad_options, new_site);
5a92ae
-    if (site == NULL) {
5a92ae
-        return ENOMEM;
5a92ae
-    }
5a92ae
-
5a92ae
-    talloc_zfree(ctx->ad_options->current_site);
5a92ae
-    ctx->ad_options->current_site = site;
5a92ae
-
5a92ae
-    ret = sysdb_set_site(ctx->be_ctx->domain, ctx->ad_options->current_site);
5a92ae
-    if (ret != EOK) {
5a92ae
-        /* Not fatal. */
5a92ae
-        DEBUG(SSSDBG_MINOR_FAILURE, "Unable to store site information "
5a92ae
-              "[%d]: %s\n", ret, sss_strerror(ret));
5a92ae
-    }
5a92ae
-
5a92ae
-    return EOK;
5a92ae
-}
5a92ae
-
5a92ae
 struct ad_srv_plugin_state {
5a92ae
     struct tevent_context *ev;
5a92ae
     struct ad_srv_plugin_ctx *ctx;
5a92ae
@@ -382,16 +333,19 @@ static void ad_srv_plugin_ping_done(struct tevent_req *subreq)
5a92ae
         /* Remember current site so it can be used during next lookup so
5a92ae
          * we can contact directory controllers within a known reachable
5a92ae
          * site first. */
5a92ae
-        ret = ad_srv_plugin_ctx_switch_site(state->ctx, state->site,
5a92ae
-                                            state->forest);
5a92ae
-        if (ret != EOK) {
5a92ae
-            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set site [%d]: %s\n",
5a92ae
-                  ret, sss_strerror(ret));
5a92ae
-            goto done;
5a92ae
-        }
5a92ae
+        if (state->site != NULL) {
5a92ae
+            ret = ad_options_switch_site(state->ctx->ad_options,
5a92ae
+                                         state->ctx->be_ctx,
5a92ae
+                                         state->site, state->forest);
5a92ae
+            if (ret != EOK) {
5a92ae
+                DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set site [%d]: %s\n",
5a92ae
+                      ret, sss_strerror(ret));
5a92ae
+                goto done;
5a92ae
+            }
5a92ae
 
5a92ae
-        /* Do not renew the site again unless we go offline. */
5a92ae
-        state->ctx->renew_site = false;
5a92ae
+            /* Do not renew the site again unless we go offline. */
5a92ae
+            state->ctx->renew_site = false;
5a92ae
+        }
5a92ae
 
5a92ae
         if (strcmp(state->service, "gc") == 0) {
5a92ae
             if (state->forest != NULL) {
5a92ae
diff --git a/src/providers/ad/ad_subdomains.c b/src/providers/ad/ad_subdomains.c
5a92ae
index 562047a02..e4e248c1d 100644
5a92ae
--- a/src/providers/ad/ad_subdomains.c
5a92ae
+++ b/src/providers/ad/ad_subdomains.c
5a92ae
@@ -2080,13 +2080,15 @@ static void ad_subdomains_refresh_master_done(struct tevent_req *subreq)
5a92ae
     const char *realm;
5a92ae
     char *master_sid;
5a92ae
     char *flat_name;
5a92ae
+    char *site = NULL;
5a92ae
     errno_t ret;
5a92ae
+    char *ad_site_override = NULL;
5a92ae
 
5a92ae
     req = tevent_req_callback_data(subreq, struct tevent_req);
5a92ae
     state = tevent_req_data(req, struct ad_subdomains_refresh_state);
5a92ae
 
5a92ae
     ret = ad_domain_info_recv(subreq, state, &flat_name, &master_sid,
5a92ae
-                              NULL, &state->forest);
5a92ae
+                              &site, &state->forest);
5a92ae
     talloc_zfree(subreq);
5a92ae
     if (ret != EOK) {
5a92ae
         DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get master domain information "
5a92ae
@@ -2112,6 +2114,36 @@ static void ad_subdomains_refresh_master_done(struct tevent_req *subreq)
5a92ae
         }
5a92ae
     }
5a92ae
 
5a92ae
+    /* If the site was not discovered during the DNS discovery, e.g. because
5a92ae
+     * the server name was given explicitly in sssd.conf, we try to set the
5a92ae
+     * site here. */
5a92ae
+    if (state->ad_options->current_site == NULL) {
5a92ae
+        /* Ignore AD site found in netlogon attribute if specific site is set in
5a92ae
+         * configuration file. */
5a92ae
+        ad_site_override = dp_opt_get_string(state->ad_options->basic, AD_SITE);
5a92ae
+        if (ad_site_override != NULL) {
5a92ae
+            DEBUG(SSSDBG_TRACE_INTERNAL,
5a92ae
+                  "Ignoring AD site found by DNS discovery: '%s', "
5a92ae
+                  "using configured value: '%s' instead.\n",
5a92ae
+                  site, ad_site_override);
5a92ae
+            site = ad_site_override;
5a92ae
+        }
5a92ae
+
5a92ae
+        if (site != NULL) {
5a92ae
+            ret = ad_options_switch_site(state->ad_options, state->be_ctx, site,
5a92ae
+                                         state->forest);
5a92ae
+            if (ret != EOK) {
5a92ae
+                DEBUG(SSSDBG_OP_FAILURE, "Failed to store forest and site name, "
5a92ae
+                                         "will try again after a new lookup.\n");
5a92ae
+            }
5a92ae
+        } else {
5a92ae
+            DEBUG(SSSDBG_MINOR_FAILURE,
5a92ae
+                  "Site name currently not available will try again later. "
5a92ae
+                  "The site name can be added manually my setting 'ad_site' "
5a92ae
+                  "in sssd.conf.\n");
5a92ae
+        }
5a92ae
+    }
5a92ae
+
5a92ae
     realm = dp_opt_get_cstring(state->ad_options->basic, AD_KRB5_REALM);
5a92ae
     if (realm == NULL) {
5a92ae
         DEBUG(SSSDBG_CONF_SETTINGS, "Missing realm.\n");
5a92ae
-- 
5a92ae
2.26.3
5a92ae