[SSSD] [PATCHES] ldap_child, krb5_child: copy keytab and FAST ccache into memory

Sumit Bose sbose at redhat.com
Tue Dec 2 12:05:03 UTC 2014


Hi,

this is the follow-up of the POC patches I send earlier in the
krb5_child thread. I added unit tests and reordered the code in
krb5_child and ldap_child a bit to make more clear until which point
they run as root and at which point they drop privileged completely.

The first patch is unrelated but I needed it to make sure that valgrind
doe not report any errors for the new unit tests.

bye,
Sumit
-------------- next part --------------
From 4c2c473c4877cd973811fdf363a4b2d09f1ad4a8 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Fri, 28 Nov 2014 18:16:33 +0100
Subject: [PATCH 1/5] test: avoid leaks in leak tests

---
 src/tests/cmocka/test_ifp.c | 5 ++++-
 src/tests/leak_check.c      | 6 +++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/tests/cmocka/test_ifp.c b/src/tests/cmocka/test_ifp.c
index d6e41706d5f55414c0376bd04d299ec6ad73c11e..5793f91911c1e15c2c241aaa51ffcd4196daea63 100644
--- a/src/tests/cmocka/test_ifp.c
+++ b/src/tests/cmocka/test_ifp.c
@@ -209,6 +209,8 @@ void test_el_to_dict(void **state)
     dbus_message_iter_get_basic(&iter_dict, &attr_val);
     assert_string_equal(attr_val, "two");
     assert_false(dbus_message_iter_next(&iter_dict));
+
+    talloc_free(sr);
 }
 
 static void assert_string_list_equal(const char **s1,
@@ -500,7 +502,8 @@ int main(int argc, const char *argv[])
         unit_test(ifp_test_req_create),
         unit_test(ifp_test_req_wrong_uid),
         unit_test(test_path_prefix),
-        unit_test(test_el_to_dict),
+        unit_test_setup_teardown(test_el_to_dict,
+                                 ifp_test_req_setup, ifp_test_req_teardown),
         unit_test(test_attr_acl),
         unit_test(test_attr_acl_ex),
         unit_test(test_attr_allowed),
diff --git a/src/tests/leak_check.c b/src/tests/leak_check.c
index bb5698cd80d5b360433d95a6297b740bb5cded87..1eaa2cd097bda54094098a2947bb3834a9bf04f4 100644
--- a/src/tests/leak_check.c
+++ b/src/tests/leak_check.c
@@ -130,10 +130,14 @@ leak_check_setup(void)
 bool
 leak_check_teardown(void)
 {
+    bool res;
     check_leaks_pop(global_talloc_context);
     if (snapshot_stack != NULL) {
         _set_leak_err_msg("Exiting with a non-empty stack");
         return false;
     }
-    return check_leaks(global_talloc_context, 0);
+    res = check_leaks(global_talloc_context, 0);
+    talloc_disable_null_tracking();
+    talloc_free(global_talloc_context);
+    return res;
 }
-- 
2.1.0

-------------- next part --------------
From bf4c7758b0e585929fea26820933dc3514d03662 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 17 Nov 2014 17:39:38 +0100
Subject: [PATCH 2/5] krb5: add copy_ccache_into_memory()

---
 Makefile.am                         |  18 +++
 src/providers/krb5/krb5_ccache.c    | 110 +++++++++++++++++
 src/providers/krb5/krb5_ccache.h    |  17 +++
 src/tests/cmocka/test_copy_ccache.c | 238 ++++++++++++++++++++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 src/tests/cmocka/test_copy_ccache.c

diff --git a/Makefile.am b/Makefile.am
index b9c25f90670dca122632176461909bd71f699e94..54527223ae8b0739d6ad00f68f162d3fe64e76fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -215,6 +215,7 @@ if HAVE_CMOCKA
         sdap-tests \
         test_sysdb_views \
         test_be_ptask \
+        test_copy_ccache \
         $(NULL)
 
 if BUILD_IFP
@@ -2096,6 +2097,23 @@ test_be_ptask_LDADD = \
     libsss_test_common.la \
     $(NULL)
 
+test_copy_ccache_SOURCES = \
+    src/tests/cmocka/test_copy_ccache.c \
+    src/providers/krb5/krb5_ccache.c \
+    src/util/sss_krb5.c \
+    $(NULL)
+test_copy_ccache_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(NULL)
+test_copy_ccache_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(POPT_LIBS) \
+    $(TALLOC_LIBS) \
+    $(KRB5_LIBS) \
+    $(SSSD_INTERNAL_LTLIBS) \
+    libsss_test_common.la \
+    $(NULL)
+
 endif # HAVE_CMOCKA
 
 noinst_PROGRAMS = pam_test_client
diff --git a/src/providers/krb5/krb5_ccache.c b/src/providers/krb5/krb5_ccache.c
index 8da562b50456d2344b10bfb410cc0d54de317069..e24179c96bf479f7ceffa8a849ba946bea1634f6 100644
--- a/src/providers/krb5/krb5_ccache.c
+++ b/src/providers/krb5/krb5_ccache.c
@@ -672,3 +672,113 @@ errno_t safe_remove_old_ccache_file(const char *old_ccache,
 
     return sss_krb5_cc_destroy(old_ccache, uid, gid);
 }
+
+krb5_error_code copy_ccache_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
+                                        const char *ccache_file,
+                                        char **_mem_name)
+{
+    krb5_error_code kerr;
+    krb5_ccache ccache;
+    krb5_ccache mem_ccache = NULL;
+    char *ccache_name = NULL;
+    krb5_principal princ = NULL;
+    char *mem_name = NULL;
+    char *sep;
+
+    kerr = krb5_cc_resolve(kctx, ccache_file, &ccache);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error resolving ccache [%s].\n",
+                                    ccache_file);
+        return kerr;
+    }
+
+    kerr = krb5_cc_get_full_name(kctx, ccache, &ccache_name);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to read name for ccache [%s].\n",
+                                    ccache_file);
+        goto done;
+    }
+
+    sep = strchr(ccache_name, ':');
+    if (sep == NULL || sep[1] == '\0') {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "Ccache name [%s] does not have delimiter[:] .\n", ccache_name);
+        kerr = KRB5KRB_ERR_GENERIC;
+        goto done;
+    }
+
+    if (strncmp(ccache_name, "MEMORY:", sizeof("MEMORY:") -1) == 0) {
+        DEBUG(SSSDBG_TRACE_FUNC, "Ccache [%s] is already memory ccache.\n",
+                                 ccache_name);
+        *_mem_name = talloc_strdup(mem_ctx, ccache_name);
+        if(*_mem_name == NULL) {
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
+            kerr = KRB5KRB_ERR_GENERIC;
+            goto done;
+        }
+        kerr = 0;
+        goto done;
+    }
+    if (strncmp(ccache_name, "FILE:", sizeof("FILE:") -1) == 0) {
+        mem_name = talloc_asprintf(mem_ctx, "MEMORY:%s", sep + 1);
+        if (mem_name == NULL) {
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf failed.\n");
+            kerr = KRB5KRB_ERR_GENERIC;
+            goto done;
+        }
+    } else {
+        DEBUG(SSSDBG_MINOR_FAILURE, "Unexpected ccache type for ccache [%s], " \
+                                    "currently only FILE is supported.\n",
+                                    ccache_name);
+        kerr = KRB5KRB_ERR_GENERIC;
+        goto done;
+    }
+
+    kerr = krb5_cc_resolve(kctx, mem_name, &mem_ccache);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error resolving ccache [%s].\n", mem_name);
+        goto done;
+    }
+
+    kerr = krb5_cc_get_principal(kctx, ccache, &princ);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "error reading principal from ccache [%s].\n", ccache_name);
+        goto done;
+    }
+
+    kerr = krb5_cc_initialize(kctx, mem_ccache, princ);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "Failed to initialize ccache [%s].\n", mem_name);
+        goto done;
+    }
+
+    kerr = krb5_cc_copy_creds(kctx, ccache, mem_ccache);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "Failed to copy ccache [%s] to [%s].\n", ccache_name, mem_name);
+        goto done;
+    }
+
+    *_mem_name = mem_name;
+    kerr = 0;
+
+done:
+    if (kerr != 0) {
+        talloc_free(mem_name);
+    }
+
+    free(ccache_name);
+    krb5_free_principal(kctx, princ);
+
+    if (krb5_cc_close(kctx, ccache) != 0) {
+        DEBUG(SSSDBG_OP_FAILURE, "krb5_cc_close failed.\n");
+    }
+
+    if (krb5_cc_close(kctx, mem_ccache) != 0) {
+        DEBUG(SSSDBG_OP_FAILURE, "krb5_cc_close failed.\n");
+    }
+
+    return  kerr;
+}
diff --git a/src/providers/krb5/krb5_ccache.h b/src/providers/krb5/krb5_ccache.h
index e47df3665e3f325cc56d34767b416662577cc048..f3928e644d704ed263cbe28bab327bf75b2f5cce 100644
--- a/src/providers/krb5/krb5_ccache.h
+++ b/src/providers/krb5/krb5_ccache.h
@@ -53,4 +53,21 @@ errno_t safe_remove_old_ccache_file(const char *old_ccache,
                                     const char *new_ccache,
                                     uid_t uid, gid_t gid);
 
+/**
+ * @brief Copy given ccache into a MEMORY ccache
+ *
+ * @param[in] mem_ctx Talloc memory context the new ccache name should be
+ *                    allocated on
+ * @param[in] kctx Kerberos context
+ * @param[in] ccache_file Name of existing ccache
+ * @param[out] _mem_name Name of the new MEMORY ccache
+ *
+ * In contrast to MEMORY keytabs MEMORY ccaches can and must be removed
+ * explicitly with krb5_cc_destroy() from the memory. Just calling
+ * krb5_cc_close() will keep the MEMORY ccache in memory even if there are no
+ * open handles for the given MEMORY ccache.
+ */
+krb5_error_code copy_ccache_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
+                                        const char *ccache_file,
+                                        char **_mem_name);
 #endif /* __KRB5_CCACHE_H__ */
diff --git a/src/tests/cmocka/test_copy_ccache.c b/src/tests/cmocka/test_copy_ccache.c
new file mode 100644
index 0000000000000000000000000000000000000000..c7a5573b83b8faeb5c7447b48fa40ec8957e1aaf
--- /dev/null
+++ b/src/tests/cmocka/test_copy_ccache.c
@@ -0,0 +1,238 @@
+/*
+    Authors:
+        Sumit Bose <sbose at redhat.com>
+
+    Copyright (C) 2014 Red Hat
+
+    SSSD tests: Tests ccache utilities
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <popt.h>
+
+#include "util/sss_krb5.h"
+#include "providers/krb5/krb5_common.h"
+#include "providers/krb5/krb5_ccache.h"
+#include "tests/cmocka/common_mock.h"
+
+#define CCACHE_TEST_CLIENT_PRINC "test/client at TEST.CCACHE"
+#define CCACHE_TEST_SERVER_PRINC "test/server at TEST.CCACHE"
+#define CCACHE_PATH TEST_DIR "/ccache_test.ccache"
+
+struct ccache_test_ctx {
+    krb5_context kctx;
+    const char *ccache_file_name;
+    krb5_principal client_principal;
+    krb5_principal server_principal;
+};
+
+void setup_ccache(void **state)
+{
+    struct ccache_test_ctx *test_ctx;
+    krb5_error_code kerr;
+    krb5_ccache ccache;
+    krb5_creds test_creds;
+    static krb5_address addr;
+    int add=0x12345;
+    krb5_authdata *a;
+
+    static krb5_address *addrs[] = {
+        &addr,
+        NULL,
+    };
+
+    assert_true(leak_check_setup());
+
+
+    test_ctx = talloc_zero(global_talloc_context, struct ccache_test_ctx);
+    assert_non_null(test_ctx);
+
+    kerr = krb5_init_context(&test_ctx->kctx);
+    assert_int_equal(kerr, 0);
+
+    addr.magic = KV5M_ADDRESS;
+    addr.addrtype = ADDRTYPE_INET;
+    addr.length = 4;
+    addr.contents = (krb5_octet *) &add;
+
+    memset(&test_creds, 0, sizeof(test_creds));
+    test_creds.magic = KV5M_CREDS;
+    kerr = krb5_parse_name(test_ctx->kctx, CCACHE_TEST_CLIENT_PRINC,
+                           &test_ctx->client_principal);
+    assert_int_equal(kerr, 0);
+    test_creds.client = test_ctx->client_principal;
+    kerr = krb5_parse_name(test_ctx->kctx, CCACHE_TEST_SERVER_PRINC,
+                           &test_ctx->server_principal);
+    assert_int_equal(kerr, 0);
+    test_creds.server = test_ctx->server_principal;
+
+    test_creds.keyblock.magic = KV5M_KEYBLOCK;
+    test_creds.keyblock.contents = 0;
+    test_creds.keyblock.enctype = 1;
+    test_creds.keyblock.length = 1;
+    test_creds.keyblock.contents = (unsigned char *) discard_const("1");
+    test_creds.times.authtime = 1111;
+    test_creds.times.starttime = 2222;
+    test_creds.times.endtime = 3333;
+    test_creds.times.renew_till = 4444;
+    test_creds.is_skey = 1;
+    test_creds.ticket_flags = 5555;
+    test_creds.addresses = addrs;
+
+    test_creds.ticket.magic = KV5M_DATA;
+    test_creds.ticket.length = sizeof("Ticket");
+    test_creds.ticket.data = discard_const("Ticket");
+
+    test_creds.authdata = malloc (2 * sizeof(krb5_authdata *));
+    assert_non_null(test_creds.authdata);
+
+    a = (krb5_authdata *) malloc(sizeof(krb5_authdata));
+    assert_non_null(a);
+
+    a->magic = KV5M_AUTHDATA;
+    a->ad_type = KRB5_AUTHDATA_IF_RELEVANT;
+    a->contents = (krb5_octet * ) malloc(1);
+    assert_non_null(a->contents);
+    a->contents[0]=5;
+    a->length = 1;
+    test_creds.authdata[0] = a;
+    test_creds.authdata[1] = NULL;
+
+
+    test_ctx->ccache_file_name = "FILE:" CCACHE_PATH;
+
+    kerr = krb5_cc_resolve(test_ctx->kctx, test_ctx->ccache_file_name,
+                               &ccache);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_cc_initialize(test_ctx->kctx, ccache, test_creds.client);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_cc_store_cred(test_ctx->kctx, ccache, &test_creds);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_cc_close(test_ctx->kctx, ccache);
+    assert_int_equal(kerr, 0);
+
+    check_leaks_push(test_ctx);
+    *state = test_ctx;
+
+    krb5_free_authdata(test_ctx->kctx, test_creds.authdata);
+}
+
+void teardown_ccache(void **state)
+{
+    int ret;
+    struct ccache_test_ctx *test_ctx = talloc_get_type(*state,
+                                                        struct ccache_test_ctx);
+    assert_non_null(test_ctx);
+
+    krb5_free_principal(test_ctx->kctx, test_ctx->client_principal);
+    krb5_free_principal(test_ctx->kctx, test_ctx->server_principal);
+    krb5_free_context(test_ctx->kctx);
+
+    ret = unlink(CCACHE_PATH);
+    assert_int_equal(ret, 0);
+
+    assert_true(check_leaks_pop(test_ctx) == true);
+    talloc_free(test_ctx);
+    assert_true(leak_check_teardown());
+}
+
+void test_copy_ccache(void **state)
+{
+    krb5_error_code kerr;
+    char *mem_ccache_name;
+    krb5_ccache ccache;
+    krb5_creds mcreds;
+    krb5_creds creds;
+    krb5_principal mem_principal;
+    struct ccache_test_ctx *test_ctx = talloc_get_type(*state,
+                                                        struct ccache_test_ctx);
+    assert_non_null(test_ctx);
+
+    kerr = copy_ccache_into_memory(test_ctx, test_ctx->kctx,
+                                   test_ctx->ccache_file_name,
+                                   &mem_ccache_name);
+    assert_int_equal(kerr, 0);
+    assert_non_null(mem_ccache_name);
+
+    kerr = krb5_cc_resolve(test_ctx->kctx, mem_ccache_name, &ccache);
+    assert_int_equal(kerr, 0);
+
+    talloc_free(mem_ccache_name);
+
+    kerr = krb5_cc_get_principal(test_ctx->kctx, ccache, &mem_principal);
+    assert_int_equal(kerr, 0);
+    assert_non_null(mem_principal);
+
+    assert_true(krb5_principal_compare(test_ctx->kctx, mem_principal,
+                                       test_ctx->client_principal));
+    krb5_free_principal(test_ctx->kctx, mem_principal);
+
+    memset(&mcreds, 0, sizeof(mcreds));
+    memset(&creds, 0, sizeof(mcreds));
+    mcreds.client = test_ctx->client_principal;
+    mcreds.server = test_ctx->server_principal;
+    kerr = krb5_cc_retrieve_cred(test_ctx->kctx, ccache, 0, &mcreds, &creds);
+    assert_int_equal(kerr, 0);
+    krb5_free_cred_contents(test_ctx->kctx, &creds);
+
+    kerr = krb5_cc_destroy(test_ctx->kctx, ccache);
+    assert_int_equal(kerr, 0);
+}
+
+int main(int argc, const char *argv[])
+{
+    poptContext pc;
+    int opt;
+    int rv;
+    struct poptOption long_options[] = {
+        POPT_AUTOHELP
+        SSSD_DEBUG_OPTS
+        POPT_TABLEEND
+    };
+
+    const UnitTest tests[] = {
+        unit_test_setup_teardown(test_copy_ccache,
+                                 setup_ccache, teardown_ccache),
+    };
+
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
+    debug_level = SSSDBG_INVALID;
+
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+    while((opt = poptGetNextOpt(pc)) != -1) {
+        switch(opt) {
+        default:
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                    poptBadOption(pc, 0), poptStrerror(opt));
+            poptPrintUsage(pc, stderr, 0);
+            return 1;
+        }
+    }
+    poptFreeContext(pc);
+
+    DEBUG_CLI_INIT(debug_level);
+
+    /* Even though normally the tests should clean up after themselves
+     * they might not after a failed run. Remove the old db to be sure */
+    tests_set_cwd();
+
+    rv = run_tests(tests);
+
+    return rv;
+}
-- 
2.1.0

-------------- next part --------------
From 304e6501c8af3c183316735b3ca0e8e2e5a784dc Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 17 Nov 2014 17:40:26 +0100
Subject: [PATCH 3/5] krb5: add copy_keytab_into_memory()

---
 Makefile.am                         |  18 +++
 src/providers/krb5/krb5_common.h    |  31 ++++++
 src/providers/krb5/krb5_keytab.c    | 165 ++++++++++++++++++++++++++++
 src/tests/cmocka/test_copy_keytab.c | 213 ++++++++++++++++++++++++++++++++++++
 4 files changed, 427 insertions(+)
 create mode 100644 src/providers/krb5/krb5_keytab.c
 create mode 100644 src/tests/cmocka/test_copy_keytab.c

diff --git a/Makefile.am b/Makefile.am
index 54527223ae8b0739d6ad00f68f162d3fe64e76fc..2cdba8ced89080db47a7583bfdbfb3012ad36b46 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -216,6 +216,7 @@ if HAVE_CMOCKA
         test_sysdb_views \
         test_be_ptask \
         test_copy_ccache \
+        test_copy_keytab \
         $(NULL)
 
 if BUILD_IFP
@@ -2114,6 +2115,23 @@ test_copy_ccache_LDADD = \
     libsss_test_common.la \
     $(NULL)
 
+test_copy_keytab_SOURCES = \
+    src/tests/cmocka/test_copy_keytab.c \
+    src/providers/krb5/krb5_keytab.c \
+    src/util/sss_krb5.c \
+    $(NULL)
+test_copy_keytab_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(NULL)
+test_copy_keytab_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(POPT_LIBS) \
+    $(TALLOC_LIBS) \
+    $(KRB5_LIBS) \
+    $(SSSD_INTERNAL_LTLIBS) \
+    libsss_test_common.la \
+    $(NULL)
+
 endif # HAVE_CMOCKA
 
 noinst_PROGRAMS = pam_test_client
diff --git a/src/providers/krb5/krb5_common.h b/src/providers/krb5/krb5_common.h
index a5cee6497e4930b16b1102a525d9fa3452845a58..81e64688a6d93a4b3ecf41d75d1bf6166b4619ce 100644
--- a/src/providers/krb5/krb5_common.h
+++ b/src/providers/krb5/krb5_common.h
@@ -189,4 +189,35 @@ int sssm_krb5_auth_init(struct be_ctx *bectx,
                         struct bet_ops **ops,
                         void **pvt_auth_data);
 
+/* from krb5_keytab.c */
+
+/**
+ * @brief Copy given keytab into a MEMORY keytab
+ *
+ * @param[in] mem_ctx Talloc memory context the new keytab name should be
+ *                    allocated on
+ * @param[in] kctx Kerberos context
+ * @param[in] inp_keytab_file Existing keytab, if set to NULL the default
+ *                            keytab will be used
+ * @param[out] _mem_name Name of the new MEMORY keytab
+ * @param[out] _mem_keytab Krb5 keytab handle for the new MEMORY keytab, NULL
+ *                         may be passed here if the caller has no use for the
+ *                         handle
+ *
+ * The memory for the MEMORY keytab is handled by libkrb5 internally and
+ * a reference counter is used. If the reference counter of the specific
+ * MEMORY keytab reaches 0, i.e. no open ones are left, the memory is free.
+ * This means we cannot call krb5_kt_close() for the new MEMORY keytab  in
+ * copy_keytab_into_memory() because this would destroy it immediately. Hence
+ * we have to return the handle so that the caller can safely remove the
+ * MEMORY keytab if the is not needed anymore. Since libkrb5 frees the
+ * internal memory when the library is unloaded short running processes can
+ * safely pass NULL as the 5th argument because on exit all memory is freed.
+ * Long running processes which need more control over the memory consumption
+ * should close the handle for free the memory at runtime.
+ */
+krb5_error_code copy_keytab_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
+                                        const char *inp_keytab_file,
+                                        char **_mem_name,
+                                        krb5_keytab *_mem_keytab);
 #endif /* __KRB5_COMMON_H__ */
diff --git a/src/providers/krb5/krb5_keytab.c b/src/providers/krb5/krb5_keytab.c
new file mode 100644
index 0000000000000000000000000000000000000000..855f69419611b863a7aea79e2788272f819b0736
--- /dev/null
+++ b/src/providers/krb5/krb5_keytab.c
@@ -0,0 +1,165 @@
+/*
+    SSSD
+
+    Kerberos 5 Backend Module -- keytab related utilities
+
+    Authors:
+        Sumit Bose <sbose at redhat.com>
+
+    Copyright (C) 2014 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "util/util.h"
+#include "util/sss_krb5.h"
+
+krb5_error_code copy_keytab_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
+                                        char *inp_keytab_file,
+                                        char **_mem_name,
+                                        krb5_keytab *_mem_keytab)
+{
+    krb5_error_code kerr;
+    krb5_error_code kt_err;
+    krb5_keytab keytab = NULL;
+    krb5_keytab mem_keytab = NULL;
+    krb5_kt_cursor cursor;
+    krb5_keytab_entry entry;
+    char keytab_name[MAX_KEYTAB_NAME_LEN];
+    char *sep;
+    char *mem_name = NULL;
+    char *keytab_file;
+    char default_keytab_name[MAX_KEYTAB_NAME_LEN];
+
+    keytab_file = inp_keytab_file;
+    if (keytab_file == NULL) {
+        kerr = krb5_kt_default_name(kctx, default_keytab_name,
+                                    sizeof(default_keytab_name));
+        if (kerr != 0) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "krb5_kt_default_name failed.\n");
+            return kerr;
+        }
+
+        keytab_file = default_keytab_name;
+    }
+
+    kerr = krb5_kt_resolve(kctx, keytab_file, &keytab);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error resolving keytab [%s].\n",
+                                    keytab_file);
+        return kerr;
+    }
+
+    kerr = krb5_kt_have_content(kctx, keytab);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "keytab [%s] has not entries.\n",
+                                    keytab_file);
+        goto done;
+    }
+
+    kerr = krb5_kt_get_name(kctx, keytab, keytab_name, sizeof(keytab_name));
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to read name for keytab [%s].\n",
+                                    keytab_file);
+        goto done;
+    }
+
+    sep = strchr(keytab_name, ':');
+    if (sep == NULL || sep[1] == '\0') {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "Keytab name [%s] does not have delimiter[:] .\n", keytab_name);
+        kerr = KRB5KRB_ERR_GENERIC;
+        goto done;
+    }
+
+    if (strncmp(keytab_name, "MEMORY:", sizeof("MEMORY:") -1) == 0) {
+        DEBUG(SSSDBG_TRACE_FUNC, "Keytab [%s] is already memory keytab.\n",
+                                 keytab_name);
+        *_mem_name = talloc_strdup(mem_ctx, keytab_name);
+        if(*_mem_name == NULL) {
+            DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n");
+            kerr = KRB5KRB_ERR_GENERIC;
+            goto done;
+        }
+        kerr = 0;
+        goto done;
+    }
+
+    mem_name = talloc_asprintf(mem_ctx, "MEMORY:%s", sep + 1);
+    if (mem_name == NULL) {
+        DEBUG(SSSDBG_OP_FAILURE, "talloc_asprintf failed.\n");
+        kerr = KRB5KRB_ERR_GENERIC;
+        goto done;
+    }
+
+    kerr = krb5_kt_resolve(kctx, mem_name, &mem_keytab);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error resolving keytab [%s].\n",
+                                    mem_name);
+        goto done;
+    }
+
+    memset(&cursor, 0, sizeof(cursor));
+    kerr = krb5_kt_start_seq_get(kctx, keytab, &cursor);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error reading keytab [%s].\n", keytab_file);
+        goto done;
+    }
+
+    memset(&entry, 0, sizeof(entry));
+    while ((kt_err = krb5_kt_next_entry(kctx, keytab, &entry, &cursor)) == 0) {
+        kerr = krb5_kt_add_entry(kctx, mem_keytab, &entry);
+        if (kerr != 0) {
+            DEBUG(SSSDBG_OP_FAILURE, "krb5_kt_add_entry failed.\n");
+            goto done;
+        }
+
+        kerr = sss_krb5_free_keytab_entry_contents(kctx, &entry);
+        if (kerr != 0) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "Failed to free keytab entry.\n");
+        }
+        memset(&entry, 0, sizeof(entry));
+    }
+
+    kerr = krb5_kt_end_seq_get(kctx, keytab, &cursor);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "krb5_kt_end_seq_get failed.\n");
+        goto done;
+    }
+
+    /* check if we got any errors from krb5_kt_next_entry */
+    if (kt_err != 0 && kt_err != KRB5_KT_END) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "error reading keytab [%s].\n", keytab_file);
+        kerr = KRB5KRB_ERR_GENERIC;
+        goto done;
+    }
+
+    *_mem_name = mem_name;
+    if (_mem_keytab != NULL) {
+        *_mem_keytab = mem_keytab;
+    }
+
+    kerr = 0;
+done:
+
+    if (kerr != 0) {
+        talloc_free(mem_name);
+    }
+
+    if (keytab != NULL && krb5_kt_close(kctx, keytab) != 0) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "krb5_kt_close failed");
+    }
+
+    return kerr;
+}
diff --git a/src/tests/cmocka/test_copy_keytab.c b/src/tests/cmocka/test_copy_keytab.c
new file mode 100644
index 0000000000000000000000000000000000000000..9d2b801564f738b4a75445045bd7602b2fd01625
--- /dev/null
+++ b/src/tests/cmocka/test_copy_keytab.c
@@ -0,0 +1,213 @@
+/*
+    Authors:
+        Sumit Bose <sbose at redhat.com>
+
+    Copyright (C) 2014 Red Hat
+
+    SSSD tests: Tests keytab utilities
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <popt.h>
+
+#include "util/sss_krb5.h"
+#include "providers/krb5/krb5_common.h"
+#include "tests/cmocka/common_mock.h"
+
+#define KEYTAB_TEST_PRINC "test/keytab at TEST.KEYTAB"
+#define KEYTAB_PATH TEST_DIR "/keytab_test.keytab"
+
+struct keytab_test_ctx {
+    krb5_context kctx;
+    const char *keytab_file_name;
+    krb5_principal principal;
+};
+
+void setup_keytab(void **state)
+{
+    struct keytab_test_ctx *test_ctx;
+    krb5_error_code kerr;
+    krb5_keytab keytab;
+    krb5_keytab_entry kent;
+
+    assert_true(leak_check_setup());
+
+    test_ctx = talloc_zero(global_talloc_context, struct keytab_test_ctx);
+    assert_non_null(test_ctx);
+
+    kerr = krb5_init_context(&test_ctx->kctx);
+    assert_int_equal(kerr, 0);
+
+    test_ctx->keytab_file_name = "FILE:" KEYTAB_PATH;
+
+    kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_parse_name(test_ctx->kctx, KEYTAB_TEST_PRINC,
+                           &test_ctx->principal);
+    assert_int_equal(kerr, 0);
+
+    memset(&kent, 0, sizeof(kent));
+    kent.magic = KV5M_KEYTAB_ENTRY;
+    kent.principal = test_ctx->principal;
+    kent.timestamp = 12345;
+    kent.vno = 1;
+    kent.key.magic = KV5M_KEYBLOCK;
+    kent.key.enctype = 1;
+    kent.key.length = 2;
+    kent.key.contents = (krb5_octet *) discard_const("11");
+
+    kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
+    assert_int_equal(kerr, 0);
+
+    kent.key.enctype = 2;
+    kent.key.contents = (krb5_octet *) discard_const("12");
+
+    kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
+    assert_int_equal(kerr, 0);
+
+    kent.vno = 2;
+    kent.key.enctype = 1;
+    kent.key.contents = (krb5_octet *) discard_const("21");
+
+    kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
+    assert_int_equal(kerr, 0);
+
+    kent.key.enctype = 2;
+    kent.key.contents = (krb5_octet *) discard_const("22");
+
+    kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_kt_close(test_ctx->kctx, keytab);
+    assert_int_equal(kerr, 0);
+
+    check_leaks_push(test_ctx);
+    *state = test_ctx;
+}
+
+void teardown_keytab(void **state)
+{
+    int ret;
+    struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
+                                                        struct keytab_test_ctx);
+    assert_non_null(test_ctx);
+
+    krb5_free_principal(test_ctx->kctx, test_ctx->principal);
+    krb5_free_context(test_ctx->kctx);
+
+    ret = unlink(KEYTAB_PATH);
+    assert_int_equal(ret, 0);
+
+    assert_true(check_leaks_pop(test_ctx) == true);
+    talloc_free(test_ctx);
+    assert_true(leak_check_teardown());
+}
+
+void test_copy_keytab(void **state)
+{
+    krb5_error_code kerr;
+    char *mem_keytab_name;
+    krb5_keytab mem_keytab;
+    krb5_keytab keytab;
+    krb5_keytab_entry kent;
+    struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
+                                                        struct keytab_test_ctx);
+    assert_non_null(test_ctx);
+
+    kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
+                                   test_ctx->keytab_file_name,
+                                   &mem_keytab_name, &mem_keytab);
+    assert_int_equal(kerr, 0);
+    assert_non_null(mem_keytab_name);
+
+    kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &keytab);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 9, 9,
+                             &kent);
+    assert_int_not_equal(kerr, 0);
+
+    kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 1,
+                             &kent);
+    assert_int_equal(kerr, 0);
+    krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
+
+    kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 2,
+                             &kent);
+    assert_int_equal(kerr, 0);
+    krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
+
+    kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 1,
+                             &kent);
+    assert_int_equal(kerr, 0);
+    krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
+
+    kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 2,
+                             &kent);
+    assert_int_equal(kerr, 0);
+    krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
+
+    talloc_free(mem_keytab_name);
+
+    kerr = krb5_kt_close(test_ctx->kctx, keytab);
+    assert_int_equal(kerr, 0);
+
+    kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
+    assert_int_equal(kerr, 0);
+}
+
+int main(int argc, const char *argv[])
+{
+    poptContext pc;
+    int opt;
+    int rv;
+    struct poptOption long_options[] = {
+        POPT_AUTOHELP
+        SSSD_DEBUG_OPTS
+        POPT_TABLEEND
+    };
+
+    const UnitTest tests[] = {
+        unit_test_setup_teardown(test_copy_keytab,
+                                 setup_keytab, teardown_keytab),
+    };
+
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
+    debug_level = SSSDBG_INVALID;
+
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+    while((opt = poptGetNextOpt(pc)) != -1) {
+        switch(opt) {
+        default:
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                    poptBadOption(pc, 0), poptStrerror(opt));
+            poptPrintUsage(pc, stderr, 0);
+            return 1;
+        }
+    }
+    poptFreeContext(pc);
+
+    DEBUG_CLI_INIT(debug_level);
+
+    /* Even though normally the tests should clean up after themselves
+     * they might not after a failed run. Remove the old db to be sure */
+    tests_set_cwd();
+
+    rv = run_tests(tests);
+
+    return rv;
+}
-- 
2.1.0

-------------- next part --------------
From 9dd548c64ef3d891e66d87db1ee93e0ef9c9842b Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 1 Dec 2014 17:24:31 +0100
Subject: [PATCH 4/5] ldap_child: copy keytab into memory to drop privileges
 earlier

---
 Makefile.am                     |  1 +
 src/providers/ldap/ldap_child.c | 64 +++++++++++++++++++++++++++++------------
 2 files changed, 46 insertions(+), 19 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 2cdba8ced89080db47a7583bfdbfb3012ad36b46..7d58a967730915a3c806b8d2b1d3ff63cb57751c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2581,6 +2581,7 @@ krb5_child_LDADD = \
 
 ldap_child_SOURCES = \
     src/providers/ldap/ldap_child.c \
+    src/providers/krb5/krb5_keytab.c \
     src/util/sss_krb5.c \
     src/util/atomic_io.c \
     src/util/authtok.c \
diff --git a/src/providers/ldap/ldap_child.c b/src/providers/ldap/ldap_child.c
index a922b181715c5e89301e9f50bdb81723d1ff2a6a..f7f8d1d2eab3f66fe4f7d09e50458b495739c1d2 100644
--- a/src/providers/ldap/ldap_child.c
+++ b/src/providers/ldap/ldap_child.c
@@ -33,6 +33,7 @@
 #include "util/sss_krb5.h"
 #include "util/child_common.h"
 #include "providers/dp_backend.h"
+#include "providers/krb5/krb5_common.h"
 
 static krb5_context krb5_error_ctx;
 #define LDAP_CHILD_DEBUG(level, error) KRB5_DEBUG(level, krb5_error_ctx, error)
@@ -47,8 +48,9 @@ static const char *__ldap_child_krb5_error_msg;
 struct input_buffer {
     const char *realm_str;
     const char *princ_str;
-    const char *keytab_name;
+    char *keytab_name;
     krb5_deltat lifetime;
+    krb5_context context;
     uid_t uid;
     gid_t gid;
 };
@@ -246,12 +248,11 @@ static int lc_verify_keytab_ex(const char *principal,
 }
 
 static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
+                                               krb5_context context,
                                                const char *realm_str,
                                                const char *princ_str,
                                                const char *keytab_name,
                                                const krb5_deltat lifetime,
-                                               uid_t uid,
-                                               gid_t gid,
                                                const char **ccname_out,
                                                time_t *expire_time_out)
 {
@@ -262,7 +263,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
     char *full_princ = NULL;
     char *default_realm = NULL;
     char *tmp_str = NULL;
-    krb5_context context = NULL;
     krb5_keytab keytab = NULL;
     krb5_ccache ccache = NULL;
     krb5_principal kprinc;
@@ -278,13 +278,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
     char *ccname_file;
     mode_t old_umask;
 
-    krberr = krb5_init_context(&context);
-    if (krberr) {
-        DEBUG(SSSDBG_OP_FAILURE, "Failed to init kerberos context\n");
-        return krberr;
-    }
-    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
-
     tmp_ctx = talloc_new(memctx);
     if (tmp_ctx == NULL) {
         krberr = KRB5KRB_ERR_GENERIC;
@@ -440,12 +433,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
     }
     DEBUG(SSSDBG_TRACE_INTERNAL, "credentials initialized\n");
 
-    krberr = become_user(uid, gid);
-    if (krberr != 0) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "become_user failed.\n");
-        goto done;
-    }
-
     ccname_dummy = talloc_asprintf(tmp_ctx, "FILE:%s", ccname_file_dummy);
     ccname = talloc_asprintf(tmp_ctx, "FILE:%s", ccname_file);
     if (ccname_dummy == NULL || ccname == NULL) {
@@ -558,6 +545,30 @@ static int prepare_response(TALLOC_CTX *mem_ctx,
     return EOK;
 }
 
+static krb5_error_code privileged_krb5_setup(struct input_buffer *ibuf)
+{
+    krb5_error_code kerr;
+    char *keytab_name;
+
+    kerr = krb5_init_context(&ibuf->context);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to init kerberos context\n");
+        return kerr;
+    }
+    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
+
+    kerr = copy_keytab_into_memory(ibuf, ibuf->context, ibuf->keytab_name,
+                                   &keytab_name, NULL);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_OP_FAILURE, "copy_keytab_into_memory failed.\n");
+        return kerr;
+    }
+    talloc_free(ibuf->keytab_name);
+    ibuf->keytab_name = keytab_name;
+
+    return 0;
+}
+
 int main(int argc, const char *argv[])
 {
     int ret;
@@ -662,11 +673,26 @@ int main(int argc, const char *argv[])
         goto fail;
     }
 
+    kerr = privileged_krb5_setup(ibuf);
+    if (kerr != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Privileged Krb5 setup failed.\n");
+        goto fail;
+    }
+    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
+
+    kerr = become_user(ibuf->uid, ibuf->gid);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "become_user failed.\n");
+        goto fail;
+    }
+
+    DEBUG(SSSDBG_TRACE_INTERNAL,
+          "Running as [%"SPRIuid"][%"SPRIgid"].\n", geteuid(), getegid());
+
     DEBUG(SSSDBG_TRACE_INTERNAL, "getting TGT sync\n");
-    kerr = ldap_child_get_tgt_sync(main_ctx,
+    kerr = ldap_child_get_tgt_sync(main_ctx, ibuf->context,
                                    ibuf->realm_str, ibuf->princ_str,
                                    ibuf->keytab_name, ibuf->lifetime,
-                                   ibuf->uid, ibuf->gid,
                                    &ccname, &expire_time);
     if (kerr != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, "ldap_child_get_tgt_sync failed.\n");
-- 
2.1.0

-------------- next part --------------
From 227afb9d1aa16e2a7a64ef12618b1881556ae285 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 1 Dec 2014 17:36:56 +0100
Subject: [PATCH 5/5] krb5_child: become user earlier

The host keytab and the FAST credential cache are copied into memory
early at startup to allow to drop privileges earlier.
---
 Makefile.am                     |   1 +
 src/providers/krb5/krb5_child.c | 131 ++++++++++++++++++++++++++++------------
 2 files changed, 94 insertions(+), 38 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7d58a967730915a3c806b8d2b1d3ff63cb57751c..a903e350e1fd6988c3c5bb7c9a0e202a465c68c5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2548,6 +2548,7 @@ libsss_ad_la_LDFLAGS = \
 krb5_child_SOURCES = \
     src/providers/krb5/krb5_child.c \
     src/providers/krb5/krb5_ccache.c \
+    src/providers/krb5/krb5_keytab.c \
     src/providers/dp_pam_data_util.c \
     src/util/user_info_msg.c \
     src/util/sss_krb5.c \
diff --git a/src/providers/krb5/krb5_child.c b/src/providers/krb5/krb5_child.c
index 5bda1cfc13362b89166ded68e77238d3159c13b9..cd78874db111e620a6a156f4310bc4e026a45121 100644
--- a/src/providers/krb5/krb5_child.c
+++ b/src/providers/krb5/krb5_child.c
@@ -42,6 +42,12 @@
 
 #define SSSD_KRB5_CHANGEPW_PRINCIPAL "kadmin/changepw"
 
+enum k5c_fast_opt {
+    K5C_FAST_NEVER,
+    K5C_FAST_TRY,
+    K5C_FAST_DEMAND,
+};
+
 struct krb5_req {
     krb5_context ctx;
     krb5_principal princ;
@@ -67,6 +73,7 @@ struct krb5_req {
     char *old_ccname;
     bool old_cc_valid;
     bool old_cc_active;
+    enum k5c_fast_opt fast_val;
 };
 
 static krb5_context krb5_error_ctx;
@@ -1847,6 +1854,7 @@ static int k5c_setup_fast(struct krb5_req *kr, bool demand)
     char *fast_principal;
     krb5_error_code kerr;
     char *tmp_str;
+    char *new_ccname;
 
     tmp_str = getenv(SSSD_KRB5_FAST_PRINCIPAL);
     if (tmp_str) {
@@ -1889,6 +1897,15 @@ static int k5c_setup_fast(struct krb5_req *kr, bool demand)
         return kerr;
     }
 
+    kerr = copy_ccache_into_memory(kr, kr->ctx, kr->fast_ccname, &new_ccname);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "copy_ccache_into_memory failed.\n");
+        return kerr;
+    }
+
+    talloc_free(kr->fast_ccname);
+    kr->fast_ccname = new_ccname;
+
     kerr = sss_krb5_get_init_creds_opt_set_fast_ccache_name(kr->ctx,
                                                             kr->options,
                                                             kr->fast_ccname);
@@ -1916,12 +1933,6 @@ static int k5c_setup_fast(struct krb5_req *kr, bool demand)
     return EOK;
 }
 
-enum k5c_fast_opt {
-    K5C_FAST_NEVER,
-    K5C_FAST_TRY,
-    K5C_FAST_DEMAND,
-};
-
 static errno_t check_use_fast(enum k5c_fast_opt *_fast_val)
 {
     char *use_fast_str;
@@ -2072,19 +2083,8 @@ static int k5c_setup(struct krb5_req *kr, uint32_t offline)
 {
     krb5_error_code kerr;
     int parse_flags;
-    enum k5c_fast_opt fast_val;
 
-    kerr = check_use_fast(&fast_val);
-    if (kerr != EOK) {
-        return kerr;
-    }
-
-    kerr = k5c_ccache_setup(kr, offline);
-    if (kerr != EOK) {
-        return kerr;
-    }
-
-    if (offline || (fast_val == K5C_FAST_NEVER && kr->validate == false)) {
+    if (offline || (kr->fast_val == K5C_FAST_NEVER && kr->validate == false)) {
         /* If krb5_child was started as setuid, but we don't need to
          * perform either validation or FAST, just drop privileges to
          * the user who is logging in. The same applies to the offline case
@@ -2105,12 +2105,6 @@ static int k5c_setup(struct krb5_req *kr, uint32_t offline)
               "Cannot read [%s] from environment.\n", SSSD_KRB5_REALM);
     }
 
-    kerr = krb5_init_context(&kr->ctx);
-    if (kerr != 0) {
-        KRB5_CHILD_DEBUG(SSSDBG_CRIT_FAILURE, kerr);
-        return kerr;
-    }
-
     /* Set the global error context */
     krb5_error_ctx = kr->ctx;
 
@@ -2153,12 +2147,6 @@ static int k5c_setup(struct krb5_req *kr, uint32_t offline)
         return ENOMEM;
     }
 
-    kerr = sss_krb5_get_init_creds_opt_alloc(kr->ctx, &kr->options);
-    if (kerr != 0) {
-        KRB5_CHILD_DEBUG(SSSDBG_CRIT_FAILURE, kerr);
-        return kerr;
-    }
-
 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_RESPONDER
     kerr = krb5_get_init_creds_opt_set_responder(kr->ctx, kr->options,
                                                  sss_krb5_responder, kr);
@@ -2183,14 +2171,6 @@ static int k5c_setup(struct krb5_req *kr, uint32_t offline)
 
     if (!offline) {
         set_canonicalize_option(kr->options);
-
-        if (fast_val != K5C_FAST_NEVER) {
-            kerr = k5c_setup_fast(kr, fast_val == K5C_FAST_DEMAND);
-            if (kerr != EOK) {
-                DEBUG(SSSDBG_OP_FAILURE, "Cannot set up FAST\n");
-                return kerr;
-            }
-        }
     }
 
 /* TODO: set options, e.g.
@@ -2207,6 +2187,63 @@ static int k5c_setup(struct krb5_req *kr, uint32_t offline)
     return kerr;
 }
 
+static krb5_error_code privileged_krb5_setup(struct krb5_req *kr,
+                                             uint32_t offline)
+{
+    krb5_error_code kerr;
+    int ret;
+    char *mem_keytab;
+
+    kerr = krb5_init_context(&kr->ctx);
+    if (kerr != 0) {
+        KRB5_CHILD_DEBUG(SSSDBG_CRIT_FAILURE, kerr);
+        return kerr;
+    }
+
+    kerr = sss_krb5_get_init_creds_opt_alloc(kr->ctx, &kr->options);
+    if (kerr != 0) {
+        KRB5_CHILD_DEBUG(SSSDBG_CRIT_FAILURE, kerr);
+        return kerr;
+    }
+
+    ret = check_use_fast(&kr->fast_val);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "check_use_fast failed.\n");
+        return ret;;
+    }
+
+    /* For ccache types FILE: and DIR: we might need to create some directory
+     * components as root */
+    ret = k5c_ccache_setup(kr, offline);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "k5c_ccache_setup failed.\n");
+        return ret;
+    }
+
+    if (!(offline ||
+            (kr->fast_val == K5C_FAST_NEVER && kr->validate == false))) {
+        kerr = copy_keytab_into_memory(kr, kr->ctx, kr->keytab, &mem_keytab,
+                                       NULL);
+        if (kerr != 0) {
+            DEBUG(SSSDBG_OP_FAILURE, "copy_keytab_into_memory failed.\n");
+            return kerr;
+        }
+
+        talloc_free(kr->keytab);
+        kr->keytab = mem_keytab;
+
+        if (kr->fast_val != K5C_FAST_NEVER) {
+            kerr = k5c_setup_fast(kr, kr->fast_val == K5C_FAST_DEMAND);
+            if (kerr != EOK) {
+                DEBUG(SSSDBG_OP_FAILURE, "Cannot set up FAST\n");
+                return kerr;
+            }
+        }
+    }
+
+    return 0;
+}
+
 int main(int argc, const char *argv[])
 {
     struct krb5_req *kr = NULL;
@@ -2215,6 +2252,7 @@ int main(int argc, const char *argv[])
     poptContext pc;
     int debug_fd = -1;
     errno_t ret;
+    krb5_error_code kerr;
 
     struct poptOption long_options[] = {
         POPT_AUTOHELP
@@ -2282,6 +2320,23 @@ int main(int argc, const char *argv[])
 
     close(STDIN_FILENO);
 
+    kerr = privileged_krb5_setup(kr, offline);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "privileged_krb5_setup failed.\n");
+        ret = EFAULT;
+        goto done;
+    }
+
+    kerr = become_user(kr->uid, kr->gid);
+    if (kerr != 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "become_user failed.\n");
+        ret = EFAULT;
+        goto done;
+    }
+
+    DEBUG(SSSDBG_TRACE_INTERNAL,
+          "Running as [%"SPRIuid"][%"SPRIgid"].\n", geteuid(), getegid());
+
     ret = k5c_setup(kr, offline);
     if (ret != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, "krb5_child_setup failed.\n");
-- 
2.1.0



More information about the sssd-devel mailing list