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

Jakub Hrozek jhrozek at redhat.com
Wed Aug 1 17:13:22 UTC 2012


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.
-------------- next part --------------
>From 455334c250b9daba50ffb9fa70531523c65a25bb 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 |  118 ++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)

diff --git a/Makefile.am b/Makefile.am
index 71d4db710023c8419584bb63a923bb4b0eacf3b8..abac262b6c0d9ce978f6a8f9c9ba0fe3d2b8d121 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1594,6 +1594,7 @@ installsssddirs::
     $(DESTDIR)$(pidpath) \
     $(DESTDIR)$(logpath) \
     $(DESTDIR)$(pubconfpath) \
+    $(DESTDIR)$(pubconfpath)/realm_mappings \
     $(DESTDIR)$(sudolibdir) \
     $(DESTDIR)$(autofslibdir)
 
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index fd6b6e43c311ac8365852186f4788e3907f41d62..aea4f60026576015a44020134e7803f1914c5e0e 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"/realm_mappings"
+
 enum ipa_subdomains_req_type {
     IPA_SUBDOMAINS_MASTER,
     IPA_SUBDOMAINS_SLAVE,
@@ -271,6 +274,112 @@ 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;
+    mode_t oldmask;
+    int fd = -1;
+    char *content;
+    size_t content_len;
+    size_t i;
+    ssize_t written;
+
+    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;
+    }
+
+    content = talloc_zero(tmp_ctx, char);
+    if (!content) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    for (i=0; i < num_subdoms; i++) {
+        content = talloc_asprintf_append(content, ".%s = %s\n%s = %s\n",
+                                         subdoms[i].name, subdoms[i].realm,
+                                         subdoms[i].name, subdoms[i].realm);
+        if (!content) {
+            ret = ENOMEM;
+            goto done;
+        }
+    }
+    content_len = strlen(content) + 1;
+
+    oldmask = umask(022);
+    fd = mkstemp(tmp_file);
+    umask(oldmask);
+    if (fd < 0) {
+        DEBUG(SSSDBG_OP_FAILURE, ("creating the temp file [%s] for domain-realm "
+                                  "mappings failed.", tmp_file));
+        ret = EIO;
+        goto done;
+    }
+
+    errno = 0;
+    written = sss_atomic_write_s(fd, content, content_len);
+    if (written == -1) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("write failed [%d][%s].\n", ret, strerror(ret)));
+        goto done;
+    }
+
+    if (written != content_len) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("Write error, wrote [%d] bytes, expected [%d]\n",
+               written, content_len));
+        ret = EIO;
+        goto done;
+    }
+
+    ret = close(fd);
+    if (ret == -1) {
+        ret = errno;
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              ("close failed [%d][%s].\n", ret, strerror(ret)));
+        goto done;
+    }
+    fd = -1;
+
+    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;
+    }
+
+    ret = EOK;
+done:
+    if (fd > -1) {
+        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 +707,15 @@ 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_OP_FAILURE,
+                  ("ipa_subdomains_write_mappings failed.\n"));
+            goto done;
+        }
     }
 
 
-- 
1.7.10.4



More information about the sssd-devel mailing list