Blame SOURCES/0034-UTIL-make-domain-mapping-content-testable.patch

b2d430
From 9d02728f8d64742e28f32fdf5bfdf083dc15a5c8 Mon Sep 17 00:00:00 2001
b2d430
From: Sumit Bose <sbose@redhat.com>
b2d430
Date: Mon, 18 Jul 2016 17:37:49 +0200
b2d430
Subject: [PATCH 34/44] UTIL: make domain mapping content testable
b2d430
b2d430
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
b2d430
---
b2d430
 src/util/domain_info_utils.c | 216 +++++++++++++++++++++++++++----------------
b2d430
 src/util/util.h              |   4 +
b2d430
 2 files changed, 138 insertions(+), 82 deletions(-)
b2d430
b2d430
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
b2d430
index 8cdd50d8d521d734e9ffd9b4e81cd6fbd7d158c7..587a6b993d2bd70662df8e0b0d5963fa00c84cf8 100644
b2d430
--- a/src/util/domain_info_utils.c
b2d430
+++ b/src/util/domain_info_utils.c
b2d430
@@ -262,11 +262,135 @@ sss_krb5_touch_config(void)
b2d430
     return EOK;
b2d430
 }
b2d430
 
b2d430
+errno_t sss_get_domain_mappings_content(TALLOC_CTX *mem_ctx,
b2d430
+                                        struct sss_domain_info *domain,
b2d430
+                                        char **content)
b2d430
+{
b2d430
+    int ret;
b2d430
+    char *o = NULL;
b2d430
+    struct sss_domain_info *dom;
b2d430
+    struct sss_domain_info *parent_dom;
b2d430
+    char *uc_parent = NULL;
b2d430
+    char *uc_forest = NULL;
b2d430
+    char *parent_capaths = NULL;
b2d430
+    bool capaths_started = false;
b2d430
+
b2d430
+    if (domain == NULL || content == NULL) {
b2d430
+        DEBUG(SSSDBG_CRIT_FAILURE, "Missing parameter.\n");
b2d430
+        return EINVAL;
b2d430
+    }
b2d430
+
b2d430
+    o = talloc_strdup(mem_ctx, "[domain_realm]\n");
b2d430
+    if (o == NULL) {
b2d430
+        DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
b2d430
+        ret = ENOMEM;
b2d430
+        goto done;
b2d430
+    }
b2d430
+
b2d430
+    /* This loops skips the starting parent and start rigth with the first
b2d430
+     * subdomain. Although in all the interesting cases (AD and IPA) the
b2d430
+     * default is that realm and DNS domain are the same strings (expect case)
b2d430
+     * and no domain_realm mapping is needed we might consider to add this
b2d430
+     * domain here as well to cover corner cases? */
b2d430
+    for (dom = get_next_domain(domain, SSS_GND_DESCEND);
b2d430
+                dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
b2d430
+                dom = get_next_domain(dom, 0)) {
b2d430
+        o = talloc_asprintf_append(o, ".%s = %s\n%s = %s\n",
b2d430
+                               dom->name, dom->realm, dom->name, dom->realm);
b2d430
+        if (o == NULL) {
b2d430
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf_append failed.\n");
b2d430
+            ret = ENOMEM;
b2d430
+            goto done;
b2d430
+        }
b2d430
+    }
b2d430
+
b2d430
+    parent_dom = domain;
b2d430
+    uc_parent = get_uppercase_realm(mem_ctx, parent_dom->name);
b2d430
+    if (uc_parent == NULL) {
b2d430
+        DEBUG(SSSDBG_OP_FAILURE, "get_uppercase_realm failed.\n");
b2d430
+        ret = ENOMEM;
b2d430
+        goto done;
b2d430
+    }
b2d430
+
b2d430
+    for (dom = get_next_domain(domain, SSS_GND_DESCEND);
b2d430
+            dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
b2d430
+            dom = get_next_domain(dom, 0)) {
b2d430
+
b2d430
+        if (dom->forest == NULL) {
b2d430
+            continue;
b2d430
+        }
b2d430
+
b2d430
+        talloc_free(uc_forest);
b2d430
+        uc_forest = get_uppercase_realm(mem_ctx, dom->forest);
b2d430
+        if (uc_forest == NULL) {
b2d430
+            DEBUG(SSSDBG_OP_FAILURE, "get_uppercase_realm failed.\n");
b2d430
+            ret = ENOMEM;
b2d430
+            goto done;
b2d430
+        }
b2d430
+
b2d430
+        if (!capaths_started) {
b2d430
+            o = talloc_asprintf_append(o, "[capaths]\n");
b2d430
+            if (o == NULL) {
b2d430
+                DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf_append failed.\n");
b2d430
+                ret = ENOMEM;
b2d430
+                goto done;
b2d430
+            }
b2d430
+            capaths_started = true;
b2d430
+        }
b2d430
+
b2d430
+        o = talloc_asprintf_append(o, "%s = {\n  %s = %s\n}\n",
b2d430
+                                   dom->realm, uc_parent, uc_forest);
b2d430
+        if (o == NULL) {
b2d430
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf_append failed.\n");
b2d430
+            ret = ENOMEM;
b2d430
+            goto done;
b2d430
+        }
b2d430
+
b2d430
+        if (parent_capaths == NULL) {
b2d430
+            parent_capaths = talloc_asprintf(mem_ctx, "  %s = %s\n", dom->realm,
b2d430
+                                                                     uc_forest);
b2d430
+        } else {
b2d430
+            parent_capaths = talloc_asprintf_append(parent_capaths,
b2d430
+                                                    "  %s = %s\n", dom->realm,
b2d430
+                                                    uc_forest);
b2d430
+        }
b2d430
+        if (parent_capaths == NULL) {
b2d430
+            DEBUG(SSSDBG_OP_FAILURE,
b2d430
+                  "talloc_asprintf/talloc_asprintf_append failed.\n");
b2d430
+            ret = ENOMEM;
b2d430
+            goto done;
b2d430
+        }
b2d430
+    }
b2d430
+
b2d430
+    if (parent_capaths != NULL) {
b2d430
+        o = talloc_asprintf_append(o, "%s = {\n%s}\n", uc_parent,
b2d430
+                                                       parent_capaths);
b2d430
+        if (o == NULL) {
b2d430
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf_append failed.\n");
b2d430
+            ret = ENOMEM;
b2d430
+            goto done;
b2d430
+        }
b2d430
+    }
b2d430
+
b2d430
+    ret = EOK;
b2d430
+
b2d430
+done:
b2d430
+    talloc_free(parent_capaths);
b2d430
+    talloc_free(uc_parent);
b2d430
+    talloc_free(uc_forest);
b2d430
+
b2d430
+    if (ret == EOK) {
b2d430
+        *content = o;
b2d430
+    } else {
b2d430
+        talloc_free(o);
b2d430
+    }
b2d430
+
b2d430
+    return ret;
b2d430
+}
b2d430
+
b2d430
 errno_t
b2d430
 sss_write_domain_mappings(struct sss_domain_info *domain)
b2d430
 {
b2d430
-    struct sss_domain_info *dom;
b2d430
-    struct sss_domain_info *parent_dom;
b2d430
     errno_t ret;
b2d430
     errno_t err;
b2d430
     TALLOC_CTX *tmp_ctx;
b2d430
@@ -277,10 +401,7 @@ sss_write_domain_mappings(struct sss_domain_info *domain)
b2d430
     mode_t old_mode;
b2d430
     FILE *fstream = NULL;
b2d430
     int i;
b2d430
-    bool capaths_started = false;
b2d430
-    char *uc_forest;
b2d430
-    char *uc_parent;
b2d430
-    char *parent_capaths = NULL;
b2d430
+    char *content = NULL;
b2d430
 
b2d430
     if (domain == NULL || domain->name == NULL) {
b2d430
         DEBUG(SSSDBG_CRIT_FAILURE, "No domain name provided\n");
b2d430
@@ -290,6 +411,12 @@ sss_write_domain_mappings(struct sss_domain_info *domain)
b2d430
     tmp_ctx = talloc_new(NULL);
b2d430
     if (!tmp_ctx) return ENOMEM;
b2d430
 
b2d430
+    ret = sss_get_domain_mappings_content(tmp_ctx, domain, &content);
b2d430
+    if (ret != EOK) {
b2d430
+        DEBUG(SSSDBG_OP_FAILURE, "sss_get_domain_mappings_content failed.\n");
b2d430
+        goto done;
b2d430
+    }
b2d430
+
b2d430
     sanitized_domain = talloc_strdup(tmp_ctx, domain->name);
b2d430
     if (sanitized_domain == NULL) {
b2d430
         DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n");
b2d430
@@ -349,88 +476,13 @@ sss_write_domain_mappings(struct sss_domain_info *domain)
b2d430
         goto done;
b2d430
     }
b2d430
 
b2d430
-    ret = fprintf(fstream, "[domain_realm]\n");
b2d430
+    ret = fprintf(fstream, "%s", content);
b2d430
     if (ret < 0) {
b2d430
         DEBUG(SSSDBG_OP_FAILURE, "fprintf failed\n");
b2d430
         ret = EIO;
b2d430
         goto done;
b2d430
     }
b2d430
 
b2d430
-    for (dom = get_next_domain(domain, SSS_GND_DESCEND);
b2d430
-         dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
b2d430
-         dom = get_next_domain(dom, 0)) {
b2d430
-        ret = fprintf(fstream, ".%s = %s\n%s = %s\n",
b2d430
-                               dom->name, dom->realm, dom->name, dom->realm);
b2d430
-        if (ret < 0) {
b2d430
-            DEBUG(SSSDBG_CRIT_FAILURE, "fprintf failed\n");
b2d430
-            goto done;
b2d430
-        }
b2d430
-    }
b2d430
-
b2d430
-    parent_dom = domain;
b2d430
-    uc_parent = get_uppercase_realm(tmp_ctx, parent_dom->name);
b2d430
-    if (uc_parent == NULL) {
b2d430
-        DEBUG(SSSDBG_OP_FAILURE, "get_uppercase_realm failed.\n");
b2d430
-        ret = ENOMEM;
b2d430
-        goto done;
b2d430
-    }
b2d430
-
b2d430
-    for (dom = get_next_domain(domain, SSS_GND_DESCEND);
b2d430
-            dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
b2d430
-            dom = get_next_domain(dom, 0)) {
b2d430
-
b2d430
-        if (dom->forest == NULL) {
b2d430
-            continue;
b2d430
-        }
b2d430
-
b2d430
-        uc_forest = get_uppercase_realm(tmp_ctx, dom->forest);
b2d430
-        if (uc_forest == NULL) {
b2d430
-            DEBUG(SSSDBG_OP_FAILURE, "get_uppercase_realm failed.\n");
b2d430
-            ret = ENOMEM;
b2d430
-            goto done;
b2d430
-        }
b2d430
-
b2d430
-        if (!capaths_started) {
b2d430
-            ret = fprintf(fstream, "[capaths]\n");
b2d430
-            if (ret < 0) {
b2d430
-                DEBUG(SSSDBG_OP_FAILURE, "fprintf failed\n");
b2d430
-                ret = EIO;
b2d430
-                goto done;
b2d430
-            }
b2d430
-            capaths_started = true;
b2d430
-        }
b2d430
-
b2d430
-        ret = fprintf(fstream, "%s = {\n  %s = %s\n}\n",
b2d430
-                                dom->realm, uc_parent, uc_forest);
b2d430
-        if (ret < 0) {
b2d430
-            DEBUG(SSSDBG_CRIT_FAILURE, "fprintf failed\n");
b2d430
-            goto done;
b2d430
-        }
b2d430
-
b2d430
-        if (parent_capaths == NULL) {
b2d430
-            parent_capaths = talloc_asprintf(tmp_ctx, "  %s = %s\n", dom->realm,
b2d430
-                                                                     uc_forest);
b2d430
-        } else {
b2d430
-            parent_capaths = talloc_asprintf_append(parent_capaths,
b2d430
-                                                    "  %s = %s\n", dom->realm,
b2d430
-                                                    uc_forest);
b2d430
-        }
b2d430
-        if (parent_capaths == NULL) {
b2d430
-            DEBUG(SSSDBG_OP_FAILURE,
b2d430
-                  "talloc_asprintf/talloc_asprintf_append failed.\n");
b2d430
-            ret = ENOMEM;
b2d430
-            goto done;
b2d430
-        }
b2d430
-    }
b2d430
-
b2d430
-    if (parent_capaths != NULL) {
b2d430
-        ret = fprintf(fstream, "%s = {\n%s}\n", uc_parent, parent_capaths);
b2d430
-        if (ret < 0) {
b2d430
-            DEBUG(SSSDBG_CRIT_FAILURE, "fprintf failed\n");
b2d430
-            goto done;
b2d430
-        }
b2d430
-    }
b2d430
-
b2d430
     ret = fclose(fstream);
b2d430
     fstream = NULL;
b2d430
     if (ret != 0) {
b2d430
diff --git a/src/util/util.h b/src/util/util.h
b2d430
index 8a5caa52c2dc5243c3ae51c5a38fd65a949f4ac4..122be90b967fb7793adaff95f3754d7a199fcf48 100644
b2d430
--- a/src/util/util.h
b2d430
+++ b/src/util/util.h
b2d430
@@ -540,6 +540,10 @@ errno_t sssd_domain_init(TALLOC_CTX *mem_ctx,
b2d430
  * written to */
b2d430
 #define KRB5_MAPPING_DIR PUBCONF_PATH"/krb5.include.d"
b2d430
 
b2d430
+errno_t sss_get_domain_mappings_content(TALLOC_CTX *mem_ctx,
b2d430
+                                        struct sss_domain_info *domain,
b2d430
+                                        char **content);
b2d430
+
b2d430
 errno_t sss_write_domain_mappings(struct sss_domain_info *domain);
b2d430
 
b2d430
 errno_t sss_write_krb5_conf_snippet(const char *path, bool canonicalize);
b2d430
-- 
b2d430
2.4.11
b2d430