Blame SOURCES/0003-adenroll-add-adcli_enroll_get_permitted_keytab_encty.patch

59dcbd
From 0c09070e8beec734e3f0c70e14b0a04788077b73 Mon Sep 17 00:00:00 2001
59dcbd
From: Sumit Bose <sbose@redhat.com>
59dcbd
Date: Thu, 13 Jun 2019 17:25:52 +0200
59dcbd
Subject: [PATCH 3/4] adenroll: add adcli_enroll_get_permitted_keytab_enctypes
59dcbd
 with tests
59dcbd
59dcbd
The new call does not only return the current encryption types set in AD
59dcbd
or a default list but filters them with the list of permitted encryption
59dcbd
types on the client. This makes sure the client can create and use the
59dcbd
keys.
59dcbd
59dcbd
Related to https://gitlab.freedesktop.org/realmd/adcli/issues/3
59dcbd
---
59dcbd
 library/Makefile.am |   5 ++
59dcbd
 library/adenroll.c  | 124 ++++++++++++++++++++++++++++++++++++++++++++
59dcbd
 library/adenroll.h  |   2 +
59dcbd
 3 files changed, 131 insertions(+)
59dcbd
59dcbd
diff --git a/library/Makefile.am b/library/Makefile.am
59dcbd
index 39e8fd1..4829555 100644
59dcbd
--- a/library/Makefile.am
59dcbd
+++ b/library/Makefile.am
59dcbd
@@ -40,6 +40,7 @@ check_PROGRAMS = \
59dcbd
 	test-util \
59dcbd
 	test-ldap \
59dcbd
 	test-attrs \
59dcbd
+	test-adenroll \
59dcbd
 	$(NULL)
59dcbd
 
59dcbd
 test_seq_SOURCES = seq.c test.c test.h
59dcbd
@@ -56,6 +57,10 @@ test_attrs_SOURCES = adattrs.c $(test_ldap_SOURCES)
59dcbd
 test_attrs_CFLAGS = -DATTRS_TESTS
59dcbd
 test_attrs_LDADD = $(test_ldap_LDADD)
59dcbd
 
59dcbd
+test_adenroll_SOURCES = adenroll.c $(test_ldap_SOURCES)
59dcbd
+test_adenroll_CFLAGS = -DADENROLL_TESTS
59dcbd
+test_adenroll_LDADD = $(KRB5_LIBS)
59dcbd
+
59dcbd
 TESTS = $(check_PROGRAMS)
59dcbd
 
59dcbd
 MEMCHECK_ENV = $(TEST_RUNNER) valgrind --error-exitcode=80 --quiet --trace-children=yes
59dcbd
diff --git a/library/adenroll.c b/library/adenroll.c
59dcbd
index f617f28..95c07cd 100644
59dcbd
--- a/library/adenroll.c
59dcbd
+++ b/library/adenroll.c
59dcbd
@@ -2641,6 +2641,50 @@ adcli_enroll_get_keytab_enctypes (adcli_enroll *enroll)
59dcbd
 		return v51_earlier_enctypes;
59dcbd
 }
59dcbd
 
59dcbd
+krb5_enctype *
59dcbd
+adcli_enroll_get_permitted_keytab_enctypes (adcli_enroll *enroll)
59dcbd
+{
59dcbd
+	krb5_enctype *cur_enctypes;
59dcbd
+	krb5_enctype *permitted_enctypes;
59dcbd
+	krb5_enctype *new_enctypes;
59dcbd
+	krb5_error_code code;
59dcbd
+	krb5_context k5;
59dcbd
+	size_t c;
59dcbd
+	size_t p;
59dcbd
+	size_t n;
59dcbd
+
59dcbd
+	return_val_if_fail (enroll != NULL, NULL);
59dcbd
+	cur_enctypes = adcli_enroll_get_keytab_enctypes (enroll);
59dcbd
+
59dcbd
+	k5 = adcli_conn_get_krb5_context (enroll->conn);
59dcbd
+	return_val_if_fail (k5 != NULL, NULL);
59dcbd
+
59dcbd
+	code = krb5_get_permitted_enctypes (k5, &permitted_enctypes);
59dcbd
+	return_val_if_fail (code == 0, NULL);
59dcbd
+
59dcbd
+	for (c = 0; cur_enctypes[c] != 0; c++);
59dcbd
+
59dcbd
+	new_enctypes = calloc (c + 1, sizeof (krb5_enctype));
59dcbd
+	return_val_if_fail (new_enctypes != NULL, NULL);
59dcbd
+
59dcbd
+	n = 0;
59dcbd
+	for (c = 0; cur_enctypes[c] != 0; c++) {
59dcbd
+		for (p = 0; permitted_enctypes[p] != 0; p++) {
59dcbd
+			if (cur_enctypes[c] == permitted_enctypes[p]) {
59dcbd
+				new_enctypes[n++] = cur_enctypes[c];
59dcbd
+				break;
59dcbd
+			}
59dcbd
+		}
59dcbd
+		if (permitted_enctypes[p] == 0) {
59dcbd
+			_adcli_info ("Encryption type [%d] not permitted.", cur_enctypes[c]);
59dcbd
+		}
59dcbd
+	}
59dcbd
+
59dcbd
+	krb5_free_enctypes (k5, permitted_enctypes);
59dcbd
+
59dcbd
+	return new_enctypes;
59dcbd
+}
59dcbd
+
59dcbd
 void
59dcbd
 adcli_enroll_set_keytab_enctypes (adcli_enroll *enroll,
59dcbd
                                   krb5_enctype *value)
59dcbd
@@ -2833,3 +2877,83 @@ adcli_enroll_add_service_principal_to_remove (adcli_enroll *enroll,
59dcbd
 							    strdup (value), NULL);
59dcbd
 	return_if_fail (enroll->service_principals_to_remove != NULL);
59dcbd
 }
59dcbd
+
59dcbd
+#ifdef ADENROLL_TESTS
59dcbd
+
59dcbd
+#include "test.h"
59dcbd
+
59dcbd
+static void
59dcbd
+test_adcli_enroll_get_permitted_keytab_enctypes (void)
59dcbd
+{
59dcbd
+	krb5_enctype *enctypes;
59dcbd
+	krb5_error_code code;
59dcbd
+	krb5_enctype *permitted_enctypes;
59dcbd
+	krb5_enctype check_enctypes[3] = { 0 };
59dcbd
+	adcli_conn *conn;
59dcbd
+	adcli_enroll *enroll;
59dcbd
+	adcli_result res;
59dcbd
+	krb5_context k5;
59dcbd
+	size_t c;
59dcbd
+
59dcbd
+	conn = adcli_conn_new ("test.dom");
59dcbd
+	assert_ptr_not_null (conn);
59dcbd
+
59dcbd
+	enroll = adcli_enroll_new (conn);
59dcbd
+	assert_ptr_not_null (enroll);
59dcbd
+
59dcbd
+	enctypes = adcli_enroll_get_permitted_keytab_enctypes (NULL);
59dcbd
+	assert_ptr_eq (enctypes, NULL);
59dcbd
+
59dcbd
+	/* krb5 context missing */
59dcbd
+	enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
59dcbd
+	assert_ptr_eq (enctypes, NULL);
59dcbd
+
59dcbd
+	/* check that all permitted enctypes can pass */
59dcbd
+	res = _adcli_krb5_init_context (&k5;;
59dcbd
+	assert_num_eq (res, ADCLI_SUCCESS);
59dcbd
+
59dcbd
+	adcli_conn_set_krb5_context (conn, k5);
59dcbd
+
59dcbd
+	code = krb5_get_permitted_enctypes (k5, &permitted_enctypes);
59dcbd
+	assert_num_eq (code, 0);
59dcbd
+	assert_ptr_not_null (permitted_enctypes);
59dcbd
+	assert_num_cmp (permitted_enctypes[0], !=, 0);
59dcbd
+
59dcbd
+	adcli_enroll_set_keytab_enctypes (enroll, permitted_enctypes);
59dcbd
+
59dcbd
+	enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
59dcbd
+	assert_ptr_not_null (enctypes);
59dcbd
+	for (c = 0; permitted_enctypes[c] != 0; c++) {
59dcbd
+		assert_num_eq (enctypes[c], permitted_enctypes[c]);
59dcbd
+	}
59dcbd
+	assert_num_eq (enctypes[c], 0);
59dcbd
+	krb5_free_enctypes (k5, enctypes);
59dcbd
+
59dcbd
+	/* check that ENCTYPE_UNKNOWN is filtered out */
59dcbd
+	check_enctypes[0] = permitted_enctypes[0];
59dcbd
+	check_enctypes[1] = ENCTYPE_UNKNOWN;
59dcbd
+	check_enctypes[2] = 0;
59dcbd
+	adcli_enroll_set_keytab_enctypes (enroll, check_enctypes);
59dcbd
+
59dcbd
+	enctypes = adcli_enroll_get_permitted_keytab_enctypes (enroll);
59dcbd
+	assert_ptr_not_null (enctypes);
59dcbd
+	assert_num_eq (enctypes[0], permitted_enctypes[0]);
59dcbd
+	assert_num_eq (enctypes[1], 0);
59dcbd
+	krb5_free_enctypes (k5, enctypes);
59dcbd
+
59dcbd
+	krb5_free_enctypes (k5, permitted_enctypes);
59dcbd
+
59dcbd
+	adcli_enroll_unref (enroll);
59dcbd
+	adcli_conn_unref (conn);
59dcbd
+}
59dcbd
+
59dcbd
+int
59dcbd
+main (int argc,
59dcbd
+      char *argv[])
59dcbd
+{
59dcbd
+	test_func (test_adcli_enroll_get_permitted_keytab_enctypes,
59dcbd
+	           "/attrs/adcli_enroll_get_permitted_keytab_enctypes");
59dcbd
+	return test_run (argc, argv);
59dcbd
+}
59dcbd
+
59dcbd
+#endif /* ADENROLL_TESTS */
59dcbd
diff --git a/library/adenroll.h b/library/adenroll.h
59dcbd
index abbbfd4..1d5d00d 100644
59dcbd
--- a/library/adenroll.h
59dcbd
+++ b/library/adenroll.h
59dcbd
@@ -138,6 +138,8 @@ krb5_enctype *     adcli_enroll_get_keytab_enctypes     (adcli_enroll *enroll);
59dcbd
 void               adcli_enroll_set_keytab_enctypes     (adcli_enroll *enroll,
59dcbd
                                                          krb5_enctype *enctypes);
59dcbd
 
59dcbd
+krb5_enctype *     adcli_enroll_get_permitted_keytab_enctypes (adcli_enroll *enroll);
59dcbd
+
59dcbd
 const char *       adcli_enroll_get_os_name             (adcli_enroll *enroll);
59dcbd
 
59dcbd
 void               adcli_enroll_set_os_name             (adcli_enroll *enroll,
59dcbd
-- 
59dcbd
2.21.0
59dcbd