Blame SOURCES/0025-KRB5-IPA-AD-Add-a-utility-function-to-create-a-krb5_.patch

cdf651
From 95cb7de6221dad54b37f7dd05dbfc3b717168488 Mon Sep 17 00:00:00 2001
cdf651
From: Jakub Hrozek <jhrozek@redhat.com>
cdf651
Date: Mon, 25 Jun 2018 13:08:25 +0200
cdf651
Subject: [PATCH] KRB5/IPA/AD: Add a utility function to create a krb5_service
cdf651
 instance
cdf651
cdf651
Each Kerberized provider used hand-crafted copy-paste code to set up its
cdf651
copy of the krb5_service structure. Instead of adding yet another copy in
cdf651
this patchset in the IPA subdomains code, create a utility function instead.
cdf651
cdf651
Due to IPA provider first creating the krb5_service in the common setup
cdf651
function, but only later reading the auth options in the auth provider
cdf651
constructor, the code first uses the default true value for the use_kdcinfo
cdf651
flag and then overrides it with the configured value in the auth constructor
cdf651
-- it would be preferable to create the structure with the right value at
cdf651
creation time, but this would require bigger refactoring. Also, the code
cdf651
before this change was even less correct as the flag was initially set the
cdf651
"false" due to the structure being allocated with talloc_zero(). At least
cdf651
now it uses the default value.
cdf651
cdf651
Related:
cdf651
https://pagure.io/SSSD/sssd/issue/3291
cdf651
cdf651
Reviewed-by: Sumit Bose <sbose@redhat.com>
cdf651
(cherry picked from commit a9a9f39342ebd26425cb1b3baedfea2429d88b04)
cdf651
---
cdf651
 src/providers/ad/ad_common.c     | 26 ++--------------
cdf651
 src/providers/ipa/ipa_common.c   | 35 +++++++++-------------
cdf651
 src/providers/krb5/krb5_common.c | 51 ++++++++++++++++++++++----------
cdf651
 src/providers/krb5/krb5_common.h |  6 ++++
cdf651
 4 files changed, 58 insertions(+), 60 deletions(-)
cdf651
cdf651
diff --git a/src/providers/ad/ad_common.c b/src/providers/ad/ad_common.c
cdf651
index feeb5d09643a02b99be1a387b41842a034a323b8..b103410e5915a380d0404e18da869517e4d4e355 100644
cdf651
--- a/src/providers/ad/ad_common.c
cdf651
+++ b/src/providers/ad/ad_common.c
cdf651
@@ -757,20 +757,14 @@ ad_failover_init(TALLOC_CTX *mem_ctx, struct be_ctx *bectx,
cdf651
         goto done;
cdf651
     }
cdf651
 
cdf651
-    service->krb5_service = talloc_zero(service, struct krb5_service);
cdf651
+    service->krb5_service = krb5_service_new(service, bectx,
cdf651
+                                             ad_service, krb5_realm,
cdf651
+                                             use_kdcinfo);
cdf651
     if (!service->krb5_service) {
cdf651
         ret = ENOMEM;
cdf651
         goto done;
cdf651
     }
cdf651
 
cdf651
-    /* Set flag that controls whether we want to write the
cdf651
-     * kdcinfo files at all
cdf651
-     */
cdf651
-    service->krb5_service->write_kdcinfo = use_kdcinfo;
cdf651
-    DEBUG(SSSDBG_CONF_SETTINGS, "write_kdcinfo for realm %s set to %s\n",
cdf651
-                       krb5_realm,
cdf651
-                       service->krb5_service->write_kdcinfo ? "true" : "false");
cdf651
-
cdf651
     ret = be_fo_add_service(bectx, ad_service, ad_user_data_cmp);
cdf651
     if (ret != EOK) {
cdf651
         DEBUG(SSSDBG_CRIT_FAILURE, "Failed to create failover service!\n");
cdf651
@@ -783,12 +777,6 @@ ad_failover_init(TALLOC_CTX *mem_ctx, struct be_ctx *bectx,
cdf651
         goto done;
cdf651
     }
cdf651
 
cdf651
-    service->krb5_service->name = talloc_strdup(service->krb5_service,
cdf651
-                                                ad_service);
cdf651
-    if (!service->krb5_service->name) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
     service->sdap->kinit_service_name = service->krb5_service->name;
cdf651
     service->gc->kinit_service_name = service->krb5_service->name;
cdf651
 
cdf651
@@ -797,14 +785,6 @@ ad_failover_init(TALLOC_CTX *mem_ctx, struct be_ctx *bectx,
cdf651
         ret = EINVAL;
cdf651
         goto done;
cdf651
     }
cdf651
-    service->krb5_service->realm =
cdf651
-        talloc_strdup(service->krb5_service, krb5_realm);
cdf651
-    if (!service->krb5_service->realm) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
-
cdf651
-    service->krb5_service->be_ctx = bectx;
cdf651
 
cdf651
     if (!primary_servers) {
cdf651
         DEBUG(SSSDBG_CONF_SETTINGS,
cdf651
diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
cdf651
index dcbb54a744358718e444972b9827ee64887e5e33..5808513bfd570c43bc1712114aabba5749ba0fec 100644
cdf651
--- a/src/providers/ipa/ipa_common.c
cdf651
+++ b/src/providers/ipa/ipa_common.c
cdf651
@@ -965,6 +965,13 @@ int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
         return ENOMEM;
cdf651
     }
cdf651
 
cdf651
+    realm = dp_opt_get_string(options->basic, IPA_KRB5_REALM);
cdf651
+    if (!realm) {
cdf651
+        DEBUG(SSSDBG_CRIT_FAILURE, "No Kerberos realm set\n");
cdf651
+        ret = EINVAL;
cdf651
+        goto done;
cdf651
+    }
cdf651
+
cdf651
     service = talloc_zero(tmp_ctx, struct ipa_service);
cdf651
     if (!service) {
cdf651
         ret = ENOMEM;
cdf651
@@ -975,7 +982,13 @@ int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
         ret = ENOMEM;
cdf651
         goto done;
cdf651
     }
cdf651
-    service->krb5_service = talloc_zero(service, struct krb5_service);
cdf651
+
cdf651
+    service->krb5_service = krb5_service_new(service, ctx,
cdf651
+                                             "IPA", realm,
cdf651
+                                             true); /* The configured value
cdf651
+                                                     * will be set later when
cdf651
+                                                     * the auth provider is set up
cdf651
+                                                     */
cdf651
     if (!service->krb5_service) {
cdf651
         ret = ENOMEM;
cdf651
         goto done;
cdf651
@@ -993,28 +1006,8 @@ int ipa_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
         goto done;
cdf651
     }
cdf651
 
cdf651
-    service->krb5_service->name = talloc_strdup(service, "IPA");
cdf651
-    if (!service->krb5_service->name) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
     service->sdap->kinit_service_name = service->krb5_service->name;
cdf651
 
cdf651
-    realm = dp_opt_get_string(options->basic, IPA_KRB5_REALM);
cdf651
-    if (!realm) {
cdf651
-        DEBUG(SSSDBG_CRIT_FAILURE, "No Kerberos realm set\n");
cdf651
-        ret = EINVAL;
cdf651
-        goto done;
cdf651
-    }
cdf651
-    service->krb5_service->realm =
cdf651
-        talloc_strdup(service->krb5_service, realm);
cdf651
-    if (!service->krb5_service->realm) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
-
cdf651
-    service->krb5_service->be_ctx = ctx;
cdf651
-
cdf651
     if (!primary_servers) {
cdf651
         DEBUG(SSSDBG_CONF_SETTINGS,
cdf651
               "No primary servers defined, using service discovery\n");
cdf651
diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c
cdf651
index d064a09ac3726c4185c2fa1eeac76ef6c261d33b..2a50dfec55c29b8d7f8b8751c904977c22aa906a 100644
cdf651
--- a/src/providers/krb5/krb5_common.c
cdf651
+++ b/src/providers/krb5/krb5_common.c
cdf651
@@ -807,6 +807,40 @@ static int krb5_user_data_cmp(void *ud1, void *ud2)
cdf651
     return strcasecmp((char*) ud1, (char*) ud2);
cdf651
 }
cdf651
 
cdf651
+struct krb5_service *krb5_service_new(TALLOC_CTX *mem_ctx,
cdf651
+                                      struct be_ctx *be_ctx,
cdf651
+                                      const char *service_name,
cdf651
+                                      const char *realm,
cdf651
+                                      bool use_kdcinfo)
cdf651
+{
cdf651
+    struct krb5_service *service;
cdf651
+
cdf651
+    service = talloc_zero(mem_ctx, struct krb5_service);
cdf651
+    if (service == NULL) {
cdf651
+        return NULL;
cdf651
+    }
cdf651
+
cdf651
+    service->name = talloc_strdup(service, service_name);
cdf651
+    if (service->name == NULL) {
cdf651
+        talloc_free(service);
cdf651
+        return NULL;
cdf651
+    }
cdf651
+
cdf651
+    service->realm = talloc_strdup(service, realm);
cdf651
+    if (service->realm == NULL) {
cdf651
+        talloc_free(service);
cdf651
+        return NULL;
cdf651
+    }
cdf651
+
cdf651
+    DEBUG(SSSDBG_CONF_SETTINGS,
cdf651
+          "write_kdcinfo for realm %s set to %s\n",
cdf651
+          realm,
cdf651
+          use_kdcinfo ? "true" : "false");
cdf651
+    service->write_kdcinfo = use_kdcinfo;
cdf651
+    service->be_ctx = be_ctx;
cdf651
+    return service;
cdf651
+}
cdf651
+
cdf651
 int krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
                       const char *service_name,
cdf651
                       const char *primary_servers,
cdf651
@@ -824,7 +858,7 @@ int krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
         return ENOMEM;
cdf651
     }
cdf651
 
cdf651
-    service = talloc_zero(tmp_ctx, struct krb5_service);
cdf651
+    service = krb5_service_new(tmp_ctx, ctx, service_name, realm, use_kdcinfo);
cdf651
     if (!service) {
cdf651
         ret = ENOMEM;
cdf651
         goto done;
cdf651
@@ -836,21 +870,6 @@ int krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
         goto done;
cdf651
     }
cdf651
 
cdf651
-    service->name = talloc_strdup(service, service_name);
cdf651
-    if (!service->name) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
-
cdf651
-    service->realm = talloc_strdup(service, realm);
cdf651
-    if (!service->realm) {
cdf651
-        ret = ENOMEM;
cdf651
-        goto done;
cdf651
-    }
cdf651
-
cdf651
-    service->write_kdcinfo = use_kdcinfo;
cdf651
-    service->be_ctx = ctx;
cdf651
-
cdf651
     if (!primary_servers) {
cdf651
         DEBUG(SSSDBG_CONF_SETTINGS,
cdf651
               "No primary servers defined, using service discovery\n");
cdf651
diff --git a/src/providers/krb5/krb5_common.h b/src/providers/krb5/krb5_common.h
cdf651
index 3529d740b89fee91281f936fdafd1bdb99e95bd7..1c12d5652ccef7e1738177eedad1c9de543916b7 100644
cdf651
--- a/src/providers/krb5/krb5_common.h
cdf651
+++ b/src/providers/krb5/krb5_common.h
cdf651
@@ -164,6 +164,12 @@ errno_t write_krb5info_file(struct krb5_service *krb5_service,
cdf651
                             const char *server,
cdf651
                             const char *service);
cdf651
 
cdf651
+struct krb5_service *krb5_service_new(TALLOC_CTX *mem_ctx,
cdf651
+                                      struct be_ctx *be_ctx,
cdf651
+                                      const char *service_name,
cdf651
+                                      const char *realm,
cdf651
+                                      bool use_kdcinfo);
cdf651
+
cdf651
 int krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
cdf651
                       const char *service_name,
cdf651
                       const char *primary_servers,
cdf651
-- 
cdf651
2.17.1
cdf651