|
|
905b4d |
From c3dd5cd9ba4020d0db0dd4ae1d9003ba852780c8 Mon Sep 17 00:00:00 2001
|
|
|
905b4d |
From: Sumit Bose <sbose@redhat.com>
|
|
|
905b4d |
Date: Mon, 17 Nov 2014 17:40:26 +0100
|
|
|
905b4d |
Subject: [PATCH 123/128] krb5: add copy_keytab_into_memory()
|
|
|
905b4d |
|
|
|
905b4d |
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
---
|
|
|
905b4d |
Makefile.am | 18 +++
|
|
|
905b4d |
src/providers/krb5/krb5_common.h | 31 ++++++
|
|
|
905b4d |
src/providers/krb5/krb5_keytab.c | 165 ++++++++++++++++++++++++++++
|
|
|
905b4d |
src/tests/cmocka/test_copy_keytab.c | 213 ++++++++++++++++++++++++++++++++++++
|
|
|
905b4d |
4 files changed, 427 insertions(+)
|
|
|
905b4d |
create mode 100644 src/providers/krb5/krb5_keytab.c
|
|
|
905b4d |
create mode 100644 src/tests/cmocka/test_copy_keytab.c
|
|
|
905b4d |
|
|
|
905b4d |
diff --git a/Makefile.am b/Makefile.am
|
|
|
905b4d |
index 62d900dec654baff59762c934885aef9ae5510b9..065992b84ce491b8b6ce1826cb5e88d7e0295176 100644
|
|
|
905b4d |
--- a/Makefile.am
|
|
|
905b4d |
+++ b/Makefile.am
|
|
|
905b4d |
@@ -215,6 +215,7 @@ if HAVE_CMOCKA
|
|
|
905b4d |
sdap-tests \
|
|
|
905b4d |
test_sysdb_views \
|
|
|
905b4d |
test_copy_ccache \
|
|
|
905b4d |
+ test_copy_keytab \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
|
|
|
905b4d |
if BUILD_IFP
|
|
|
905b4d |
@@ -2097,6 +2098,23 @@ test_copy_ccache_LDADD = \
|
|
|
905b4d |
libsss_test_common.la \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
|
|
|
905b4d |
+test_copy_keytab_SOURCES = \
|
|
|
905b4d |
+ src/tests/cmocka/test_copy_keytab.c \
|
|
|
905b4d |
+ src/providers/krb5/krb5_keytab.c \
|
|
|
905b4d |
+ src/util/sss_krb5.c \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+test_copy_keytab_CFLAGS = \
|
|
|
905b4d |
+ $(AM_CFLAGS) \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+test_copy_keytab_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_common.h b/src/providers/krb5/krb5_common.h
|
|
|
905b4d |
index a5cee6497e4930b16b1102a525d9fa3452845a58..81e64688a6d93a4b3ecf41d75d1bf6166b4619ce 100644
|
|
|
905b4d |
--- a/src/providers/krb5/krb5_common.h
|
|
|
905b4d |
+++ b/src/providers/krb5/krb5_common.h
|
|
|
905b4d |
@@ -189,4 +189,35 @@ int sssm_krb5_auth_init(struct be_ctx *bectx,
|
|
|
905b4d |
struct bet_ops **ops,
|
|
|
905b4d |
void **pvt_auth_data);
|
|
|
905b4d |
|
|
|
905b4d |
+/* from krb5_keytab.c */
|
|
|
905b4d |
+
|
|
|
905b4d |
+/**
|
|
|
905b4d |
+ * @brief Copy given keytab into a MEMORY keytab
|
|
|
905b4d |
+ *
|
|
|
905b4d |
+ * @param[in] mem_ctx Talloc memory context the new keytab name should be
|
|
|
905b4d |
+ * allocated on
|
|
|
905b4d |
+ * @param[in] kctx Kerberos context
|
|
|
905b4d |
+ * @param[in] inp_keytab_file Existing keytab, if set to NULL the default
|
|
|
905b4d |
+ * keytab will be used
|
|
|
905b4d |
+ * @param[out] _mem_name Name of the new MEMORY keytab
|
|
|
905b4d |
+ * @param[out] _mem_keytab Krb5 keytab handle for the new MEMORY keytab, NULL
|
|
|
905b4d |
+ * may be passed here if the caller has no use for the
|
|
|
905b4d |
+ * handle
|
|
|
905b4d |
+ *
|
|
|
905b4d |
+ * The memory for the MEMORY keytab is handled by libkrb5 internally and
|
|
|
905b4d |
+ * a reference counter is used. If the reference counter of the specific
|
|
|
905b4d |
+ * MEMORY keytab reaches 0, i.e. no open ones are left, the memory is free.
|
|
|
905b4d |
+ * This means we cannot call krb5_kt_close() for the new MEMORY keytab in
|
|
|
905b4d |
+ * copy_keytab_into_memory() because this would destroy it immediately. Hence
|
|
|
905b4d |
+ * we have to return the handle so that the caller can safely remove the
|
|
|
905b4d |
+ * MEMORY keytab if the is not needed anymore. Since libkrb5 frees the
|
|
|
905b4d |
+ * internal memory when the library is unloaded short running processes can
|
|
|
905b4d |
+ * safely pass NULL as the 5th argument because on exit all memory is freed.
|
|
|
905b4d |
+ * Long running processes which need more control over the memory consumption
|
|
|
905b4d |
+ * should close the handle for free the memory at runtime.
|
|
|
905b4d |
+ */
|
|
|
905b4d |
+krb5_error_code copy_keytab_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
|
|
|
905b4d |
+ const char *inp_keytab_file,
|
|
|
905b4d |
+ char **_mem_name,
|
|
|
905b4d |
+ krb5_keytab *_mem_keytab);
|
|
|
905b4d |
#endif /* __KRB5_COMMON_H__ */
|
|
|
905b4d |
diff --git a/src/providers/krb5/krb5_keytab.c b/src/providers/krb5/krb5_keytab.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..855f69419611b863a7aea79e2788272f819b0736
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/providers/krb5/krb5_keytab.c
|
|
|
905b4d |
@@ -0,0 +1,165 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ SSSD
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Kerberos 5 Backend Module -- keytab related utilities
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Sumit Bose <sbose@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
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 "util/util.h"
|
|
|
905b4d |
+#include "util/sss_krb5.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+krb5_error_code copy_keytab_into_memory(TALLOC_CTX *mem_ctx, krb5_context kctx,
|
|
|
905b4d |
+ char *inp_keytab_file,
|
|
|
905b4d |
+ char **_mem_name,
|
|
|
905b4d |
+ krb5_keytab *_mem_keytab)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ krb5_error_code kerr;
|
|
|
905b4d |
+ krb5_error_code kt_err;
|
|
|
905b4d |
+ krb5_keytab keytab = NULL;
|
|
|
905b4d |
+ krb5_keytab mem_keytab = NULL;
|
|
|
905b4d |
+ krb5_kt_cursor cursor;
|
|
|
905b4d |
+ krb5_keytab_entry entry;
|
|
|
905b4d |
+ char keytab_name[MAX_KEYTAB_NAME_LEN];
|
|
|
905b4d |
+ char *sep;
|
|
|
905b4d |
+ char *mem_name = NULL;
|
|
|
905b4d |
+ char *keytab_file;
|
|
|
905b4d |
+ char default_keytab_name[MAX_KEYTAB_NAME_LEN];
|
|
|
905b4d |
+
|
|
|
905b4d |
+ keytab_file = inp_keytab_file;
|
|
|
905b4d |
+ if (keytab_file == NULL) {
|
|
|
905b4d |
+ kerr = krb5_kt_default_name(kctx, default_keytab_name,
|
|
|
905b4d |
+ sizeof(default_keytab_name));
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "krb5_kt_default_name failed.\n");
|
|
|
905b4d |
+ return kerr;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ keytab_file = default_keytab_name;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_resolve(kctx, keytab_file, &keytab);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "error resolving keytab [%s].\n",
|
|
|
905b4d |
+ keytab_file);
|
|
|
905b4d |
+ return kerr;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_have_content(kctx, keytab);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "keytab [%s] has not entries.\n",
|
|
|
905b4d |
+ keytab_file);
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_name(kctx, keytab, keytab_name, sizeof(keytab_name));
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Failed to read name for keytab [%s].\n",
|
|
|
905b4d |
+ keytab_file);
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ sep = strchr(keytab_name, ':');
|
|
|
905b4d |
+ if (sep == NULL || sep[1] == '\0') {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE,
|
|
|
905b4d |
+ "Keytab name [%s] does not have delimiter[:] .\n", keytab_name);
|
|
|
905b4d |
+ kerr = KRB5KRB_ERR_GENERIC;
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (strncmp(keytab_name, "MEMORY:", sizeof("MEMORY:") -1) == 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_TRACE_FUNC, "Keytab [%s] is already memory keytab.\n",
|
|
|
905b4d |
+ keytab_name);
|
|
|
905b4d |
+ *_mem_name = talloc_strdup(mem_ctx, keytab_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 |
+
|
|
|
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 |
+
|
|
|
905b4d |
+ kerr = krb5_kt_resolve(kctx, mem_name, &mem_keytab);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "error resolving keytab [%s].\n",
|
|
|
905b4d |
+ mem_name);
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ memset(&cursor, 0, sizeof(cursor));
|
|
|
905b4d |
+ kerr = krb5_kt_start_seq_get(kctx, keytab, &cursor);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "error reading keytab [%s].\n", keytab_file);
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ memset(&entry, 0, sizeof(entry));
|
|
|
905b4d |
+ while ((kt_err = krb5_kt_next_entry(kctx, keytab, &entry, &cursor)) == 0) {
|
|
|
905b4d |
+ kerr = krb5_kt_add_entry(kctx, mem_keytab, &entry);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_OP_FAILURE, "krb5_kt_add_entry failed.\n");
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = sss_krb5_free_keytab_entry_contents(kctx, &entry);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_MINOR_FAILURE, "Failed to free keytab entry.\n");
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ memset(&entry, 0, sizeof(entry));
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_end_seq_get(kctx, keytab, &cursor);
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "krb5_kt_end_seq_get failed.\n");
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* check if we got any errors from krb5_kt_next_entry */
|
|
|
905b4d |
+ if (kt_err != 0 && kt_err != KRB5_KT_END) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_CRIT_FAILURE, "error reading keytab [%s].\n", keytab_file);
|
|
|
905b4d |
+ kerr = KRB5KRB_ERR_GENERIC;
|
|
|
905b4d |
+ goto done;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ *_mem_name = mem_name;
|
|
|
905b4d |
+ if (_mem_keytab != NULL) {
|
|
|
905b4d |
+ *_mem_keytab = mem_keytab;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = 0;
|
|
|
905b4d |
+done:
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (kerr != 0) {
|
|
|
905b4d |
+ talloc_free(mem_name);
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (keytab != NULL && krb5_kt_close(kctx, keytab) != 0) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_MINOR_FAILURE, "krb5_kt_close failed");
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ return kerr;
|
|
|
905b4d |
+}
|
|
|
905b4d |
diff --git a/src/tests/cmocka/test_copy_keytab.c b/src/tests/cmocka/test_copy_keytab.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..9d2b801564f738b4a75445045bd7602b2fd01625
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/tests/cmocka/test_copy_keytab.c
|
|
|
905b4d |
@@ -0,0 +1,213 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Sumit Bose <sbose@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
905b4d |
+
|
|
|
905b4d |
+ SSSD tests: Tests keytab 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 "tests/cmocka/common_mock.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+#define KEYTAB_TEST_PRINC "test/keytab@TEST.KEYTAB"
|
|
|
905b4d |
+#define KEYTAB_PATH TEST_DIR "/keytab_test.keytab"
|
|
|
905b4d |
+
|
|
|
905b4d |
+struct keytab_test_ctx {
|
|
|
905b4d |
+ krb5_context kctx;
|
|
|
905b4d |
+ const char *keytab_file_name;
|
|
|
905b4d |
+ krb5_principal principal;
|
|
|
905b4d |
+};
|
|
|
905b4d |
+
|
|
|
905b4d |
+void setup_keytab(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ struct keytab_test_ctx *test_ctx;
|
|
|
905b4d |
+ krb5_error_code kerr;
|
|
|
905b4d |
+ krb5_keytab keytab;
|
|
|
905b4d |
+ krb5_keytab_entry kent;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ assert_true(leak_check_setup());
|
|
|
905b4d |
+
|
|
|
905b4d |
+ test_ctx = talloc_zero(global_talloc_context, struct keytab_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 |
+ test_ctx->keytab_file_name = "FILE:" KEYTAB_PATH;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_resolve(test_ctx->kctx, test_ctx->keytab_file_name, &keytab);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_parse_name(test_ctx->kctx, KEYTAB_TEST_PRINC,
|
|
|
905b4d |
+ &test_ctx->principal);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ memset(&kent, 0, sizeof(kent));
|
|
|
905b4d |
+ kent.magic = KV5M_KEYTAB_ENTRY;
|
|
|
905b4d |
+ kent.principal = test_ctx->principal;
|
|
|
905b4d |
+ kent.timestamp = 12345;
|
|
|
905b4d |
+ kent.vno = 1;
|
|
|
905b4d |
+ kent.key.magic = KV5M_KEYBLOCK;
|
|
|
905b4d |
+ kent.key.enctype = 1;
|
|
|
905b4d |
+ kent.key.length = 2;
|
|
|
905b4d |
+ kent.key.contents = (krb5_octet *) discard_const("11");
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kent.key.enctype = 2;
|
|
|
905b4d |
+ kent.key.contents = (krb5_octet *) discard_const("12");
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kent.vno = 2;
|
|
|
905b4d |
+ kent.key.enctype = 1;
|
|
|
905b4d |
+ kent.key.contents = (krb5_octet *) discard_const("21");
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kent.key.enctype = 2;
|
|
|
905b4d |
+ kent.key.contents = (krb5_octet *) discard_const("22");
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_add_entry(test_ctx->kctx, keytab, &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_close(test_ctx->kctx, keytab);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_push(test_ctx);
|
|
|
905b4d |
+ *state = test_ctx;
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void teardown_keytab(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ int ret;
|
|
|
905b4d |
+ struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
|
|
|
905b4d |
+ struct keytab_test_ctx);
|
|
|
905b4d |
+ assert_non_null(test_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ krb5_free_principal(test_ctx->kctx, test_ctx->principal);
|
|
|
905b4d |
+ krb5_free_context(test_ctx->kctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = unlink(KEYTAB_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_keytab(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ krb5_error_code kerr;
|
|
|
905b4d |
+ char *mem_keytab_name;
|
|
|
905b4d |
+ krb5_keytab mem_keytab;
|
|
|
905b4d |
+ krb5_keytab keytab;
|
|
|
905b4d |
+ krb5_keytab_entry kent;
|
|
|
905b4d |
+ struct keytab_test_ctx *test_ctx = talloc_get_type(*state,
|
|
|
905b4d |
+ struct keytab_test_ctx);
|
|
|
905b4d |
+ assert_non_null(test_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = copy_keytab_into_memory(test_ctx, test_ctx->kctx,
|
|
|
905b4d |
+ test_ctx->keytab_file_name,
|
|
|
905b4d |
+ &mem_keytab_name, &mem_keytab);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+ assert_non_null(mem_keytab_name);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_resolve(test_ctx->kctx, mem_keytab_name, &keytab);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 9, 9,
|
|
|
905b4d |
+ &kent);
|
|
|
905b4d |
+ assert_int_not_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 1,
|
|
|
905b4d |
+ &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+ krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 1, 2,
|
|
|
905b4d |
+ &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+ krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 1,
|
|
|
905b4d |
+ &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+ krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_get_entry(test_ctx->kctx, keytab, test_ctx->principal, 2, 2,
|
|
|
905b4d |
+ &kent);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+ krb5_free_keytab_entry_contents(test_ctx->kctx, &kent);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ talloc_free(mem_keytab_name);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_close(test_ctx->kctx, keytab);
|
|
|
905b4d |
+ assert_int_equal(kerr, 0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ kerr = krb5_kt_close(test_ctx->kctx, mem_keytab);
|
|
|
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_keytab,
|
|
|
905b4d |
+ setup_keytab, teardown_keytab),
|
|
|
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 |
|