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

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