dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0122-krb5-add-copy_ccache_into_memory.patch

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