[SSSD] [PATCH] Create a domain-realm mapping for krb5.conf to be included

Jakub Hrozek jhrozek at redhat.com
Wed Aug 1 19:09:23 UTC 2012


On Wed, Aug 01, 2012 at 07:13:22PM +0200, Jakub Hrozek wrote:
> When new subdomains are discovered, the SSSD creates a file that
> includes the domain-realm mappings. This file can in turn be included in
> the krb5.conf using the includedir directive, such as:
> 
> includedir /var/lib/sss/pubconf/realm_mappings
> 
> The other part of the work is changing ipa-client such that the above
> line is added to krb5.conf when the client is installed.

Simo nacked the origial patch on IRC.

I made the following changes:
    * the directory is now called krb5.include.d. In the future we might
      want to include other config snippets than domain-realm mappings
    * fprintf is used to write the contents instead of first
      constructing the contents and then writing
    * the file is first created with mkstemp's default restrictive
      permissions and then chmod-ed to be readable
    * failure to write the file is not fatal anymore
-------------- next part --------------
>From 9e9994a0828b0df1c5133648b3cfa6070c842ce5 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Wed, 1 Aug 2012 18:28:04 +0200
Subject: [PATCH] Create a domain-realm mapping for krb5.conf to be included

When new subdomains are discovered, the SSSD creates a file that
includes the domain-realm mappings. This file can in turn be included in
the krb5.conf using the includedir directive, such as:

includedir /var/lib/sss/pubconf/realm_mappings
---
 Makefile.am                        |    1 +
 src/providers/ipa/ipa_subdomains.c |  132 ++++++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+)

diff --git a/Makefile.am b/Makefile.am
index 71d4db710023c8419584bb63a923bb4b0eacf3b8..7f27c640ec7dd62b0285015d99c0db3ef947d5c3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1594,6 +1594,7 @@ installsssddirs::
     $(DESTDIR)$(pidpath) \
     $(DESTDIR)$(logpath) \
     $(DESTDIR)$(pubconfpath) \
+    $(DESTDIR)$(pubconfpath)/krb5.include.d \
     $(DESTDIR)$(sudolibdir) \
     $(DESTDIR)$(autofslibdir)
 
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index fd6b6e43c311ac8365852186f4788e3907f41d62..111d9ee2a384907ebe4da6700b5d1bc661694202 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -48,6 +48,9 @@
 /* refresh automatically every 4 hours */
 #define IPA_SUBDOMAIN_REFRESH_PERIOD (3600 * 4)
 
+/* the directory domain - realm mappings are written to */
+#define IPA_SUBDOMAIN_MAPPING_DIR PUBCONF_PATH"/krb5.include.d"
+
 enum ipa_subdomains_req_type {
     IPA_SUBDOMAINS_MASTER,
     IPA_SUBDOMAINS_SLAVE,
@@ -271,6 +274,127 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
     return EOK;
 }
 
+static errno_t
+ipa_subdomains_write_mappings(struct sss_domain_info *domain,
+                              size_t num_subdoms,
+                              struct sysdb_subdom *subdoms)
+{
+    errno_t ret;
+    TALLOC_CTX *tmp_ctx;
+    const char *mapping_file;
+    char *tmp_file = NULL;
+    int fd = -1;
+    FILE *fstream = NULL;
+    size_t i;
+
+    tmp_ctx = talloc_new(NULL);
+    if (!tmp_ctx) return ENOMEM;
+
+    mapping_file = talloc_asprintf(tmp_ctx, "%s/domain_realm_%s",
+                                   IPA_SUBDOMAIN_MAPPING_DIR, domain->name);
+    if (!mapping_file) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    tmp_file = talloc_asprintf(tmp_ctx, "%sXXXXXX", mapping_file);
+    if (tmp_file == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    fd = mkstemp(tmp_file);
+    if (fd < 0) {
+        DEBUG(SSSDBG_OP_FAILURE, ("creating the temp file [%s] for domain-realm "
+                                  "mappings failed.", tmp_file));
+        ret = EIO;
+        talloc_zfree(tmp_ctx);
+        goto done;
+    }
+
+    fstream = fdopen(fd, "a");
+    if (!fstream) {
+        ret = errno;
+        DEBUG(SSSDBG_OP_FAILURE, ("fdopen failed [%d]: %s\n",
+                                  ret, strerror(ret)));
+        ret = close(fd);
+        if (ret != 0) {
+            ret = errno;
+            DEBUG(SSSDBG_CRIT_FAILURE,
+                ("fclose failed [%d][%s].\n", ret, strerror(ret)));
+            /* Nothing to do here, just report the failure */
+        }
+        ret = EIO;
+        goto done;
+    }
+
+    ret = fprintf(fstream, "[domain_realm]\n");
+    if (ret < 0) {
+        DEBUG(SSSDBG_OP_FAILURE, ("fprintf failed\n"));
+        ret = EIO;
+        goto done;
+    }
+
+    for (i = 0; i < num_subdoms; i++) {
+        ret = fprintf(fstream, ".%s = %s\n%s = %s\n",
+                               subdoms[i].name, subdoms[i].realm,
+                               subdoms[i].name, subdoms[i].realm);
+        if (ret < 0) {
+            DEBUG(SSSDBG_CRIT_FAILURE, ("fprintf failed\n"));
+            goto done;
+        }
+    }
+
+    ret = fclose(fstream);
+    if (ret != 0) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("fclose failed [%d][%s].\n", ret, strerror(ret)));
+        goto done;
+    }
+    fstream = NULL;
+
+    ret = rename(tmp_file, mapping_file);
+    if (ret == -1) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("rename failed [%d][%s].\n", ret, strerror(ret)));
+        goto done;
+    }
+
+    talloc_zfree(tmp_file);
+
+    ret = chmod(mapping_file, 0644);
+    if (ret == -1) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("fchmod failed [%d][%s].\n", ret, strerror(ret)));
+        goto done;
+    }
+
+    ret = EOK;
+done:
+    if (fstream) {
+        ret = fclose(fstream);
+        if (ret != 0) {
+            ret = errno;
+            DEBUG(SSSDBG_CRIT_FAILURE,
+                ("fclose failed [%d][%s].\n", ret, strerror(ret)));
+            /* Nothing to do here, just report the failure */
+        }
+    }
+
+    if (tmp_file) {
+        ret = unlink(tmp_file);
+        if (ret < 0) {
+            DEBUG(SSSDBG_MINOR_FAILURE, ("Could not remove file [%s]",
+                                         tmp_file));
+        }
+    }
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
                                       int count, struct sysdb_attrs **reply,
                                       bool *changes)
@@ -598,6 +722,14 @@ static void ipa_subdomains_handler_done(struct tevent_req *req)
             DEBUG(SSSDBG_OP_FAILURE, ("sysdb_update_subdomains failed.\n"));
             goto done;
         }
+
+        ret = ipa_subdomains_write_mappings(sysdb_ctx_get_domain(sysdb),
+                                            ctx->sd_ctx->num_subdoms,
+                                            ctx->sd_ctx->subdoms);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_MINOR_FAILURE,
+                  ("ipa_subdomains_write_mappings failed.\n"));
+        }
     }
 
 
-- 
1.7.10.4



More information about the sssd-devel mailing list