461678c
From 9ad1164405e7b4decb7c4ad96fe5ab27d6e53366 Mon Sep 17 00:00:00 2001
461678c
From: Sumit Bose <sbose@redhat.com>
461678c
Date: Wed, 6 Jun 2018 16:31:32 +0200
461678c
Subject: [PATCH 19/23] Calculate enctypes in a separate function
461678c
461678c
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1542354
461678c
---
461678c
 library/adenroll.c | 137 +++++++++++++++++++++++++++++++----------------------
461678c
 1 file changed, 81 insertions(+), 56 deletions(-)
461678c
461678c
diff --git a/library/adenroll.c b/library/adenroll.c
461678c
index 6fdc773..75ac1e4 100644
461678c
--- a/library/adenroll.c
461678c
+++ b/library/adenroll.c
461678c
@@ -542,6 +542,83 @@ calculate_computer_account (adcli_enroll *enroll,
461678c
 	return ADCLI_SUCCESS;
461678c
 }
461678c
 
461678c
+static adcli_result
461678c
+calculate_enctypes (adcli_enroll *enroll, char **enctype)
461678c
+{
461678c
+	char *value = NULL;
461678c
+	krb5_enctype *read_enctypes;
461678c
+	char *new_value = NULL;
461678c
+	int is_2008_or_later;
461678c
+	LDAP *ldap;
461678c
+
461678c
+	*enctype = NULL;
461678c
+	/*
461678c
+	 * Because we're using a keytab we want the server to be aware of the
461678c
+	 * encryption types supported on the client, because we can't dynamically
461678c
+	 * use a new one that's thrown at us.
461678c
+	 *
461678c
+	 * If the encryption types are not explicitly set by the caller of this
461678c
+	 * library, then see if the account already has some encryption types
461678c
+	 * marked on it.
461678c
+	 *
461678c
+	 * If not, write our default set to the account.
461678c
+	 *
461678c
+	 * Note that Windows 2003 and earlier have a standard set of encryption
461678c
+	 * types, and no msDS-supportedEncryptionTypes attribute.
461678c
+	 */
461678c
+
461678c
+	ldap = adcli_conn_get_ldap_connection (enroll->conn);
461678c
+	return_unexpected_if_fail (ldap != NULL);
461678c
+
461678c
+	is_2008_or_later = adcli_conn_server_has_capability (enroll->conn, ADCLI_CAP_V60_OID);
461678c
+
461678c
+	/* In 2008 or later, use the msDS-supportedEncryptionTypes attribute */
461678c
+	if (is_2008_or_later) {
461678c
+		value = _adcli_ldap_parse_value (ldap, enroll->computer_attributes,
461678c
+		                                 "msDS-supportedEncryptionTypes");
461678c
+
461678c
+		if (!enroll->keytab_enctypes_explicit && value != NULL) {
461678c
+			read_enctypes = _adcli_krb5_parse_enctypes (value);
461678c
+			if (read_enctypes == NULL) {
461678c
+				_adcli_warn ("Invalid or unsupported encryption types are set on "
461678c
+				             "the computer account (%s).", value);
461678c
+			} else {
461678c
+				free (enroll->keytab_enctypes);
461678c
+				enroll->keytab_enctypes = read_enctypes;
461678c
+			}
461678c
+		}
461678c
+
461678c
+	/* In 2003 or earlier, standard set of enc types */
461678c
+	} else {
461678c
+		value = _adcli_krb5_format_enctypes (v51_earlier_enctypes);
461678c
+	}
461678c
+
461678c
+	new_value = _adcli_krb5_format_enctypes (adcli_enroll_get_keytab_enctypes (enroll));
461678c
+	if (new_value == NULL) {
461678c
+		free (value);
461678c
+		_adcli_warn ("The encryption types desired are not available in active directory");
461678c
+		return ADCLI_ERR_CONFIG;
461678c
+	}
461678c
+
461678c
+	/* If we already have this value, then don't need to update */
461678c
+	if (value && strcmp (new_value, value) == 0) {
461678c
+		free (value);
461678c
+		free (new_value);
461678c
+		return ADCLI_SUCCESS;
461678c
+	}
461678c
+	free (value);
461678c
+
461678c
+	if (!is_2008_or_later) {
461678c
+		free (new_value);
461678c
+		_adcli_warn ("Server does not support setting encryption types");
461678c
+		return ADCLI_SUCCESS;
461678c
+	}
461678c
+
461678c
+	*enctype = new_value;
461678c
+	return ADCLI_SUCCESS;
461678c
+}
461678c
+
461678c
+
461678c
 static adcli_result
461678c
 create_computer_account (adcli_enroll *enroll,
461678c
                          LDAP *ldap)
461678c
@@ -1053,75 +1130,23 @@ retrieve_computer_account (adcli_enroll *enroll)
461678c
 static adcli_result
461678c
 update_and_calculate_enctypes (adcli_enroll *enroll)
461678c
 {
461678c
-	char *value = NULL;
461678c
-	krb5_enctype *read_enctypes;
461678c
 	char *vals_supportedEncryptionTypes[] = { NULL, NULL };
461678c
 	LDAPMod mod = { LDAP_MOD_REPLACE, "msDS-supportedEncryptionTypes", { vals_supportedEncryptionTypes, } };
461678c
 	LDAPMod *mods[2] = { &mod, NULL };
461678c
-	int is_2008_or_later;
461678c
 	char *new_value;
461678c
 	LDAP *ldap;
461678c
 	int ret;
461678c
 
461678c
-	/*
461678c
-	 * Because we're using a keytab we want the server to be aware of the
461678c
-	 * encryption types supported on the client, because we can't dynamically
461678c
-	 * use a new one that's thrown at us.
461678c
-	 *
461678c
-	 * If the encryption types are not explicitly set by the caller of this
461678c
-	 * library, then see if the account already has some encryption types
461678c
-	 * marked on it.
461678c
-	 *
461678c
-	 * If not, write our default set to the account.
461678c
-	 *
461678c
-	 * Note that Windows 2003 and earlier have a standard set of encryption
461678c
-	 * types, and no msDS-supportedEncryptionTypes attribute.
461678c
-	 */
461678c
-
461678c
 	ldap = adcli_conn_get_ldap_connection (enroll->conn);
461678c
 	return_unexpected_if_fail (ldap != NULL);
461678c
 
461678c
-	is_2008_or_later = adcli_conn_server_has_capability (enroll->conn, ADCLI_CAP_V60_OID);
461678c
-
461678c
-	/* In 2008 or later, use the msDS-supportedEncryptionTypes attribute */
461678c
-	if (is_2008_or_later) {
461678c
-		value = _adcli_ldap_parse_value (ldap, enroll->computer_attributes,
461678c
-		                                 "msDS-supportedEncryptionTypes");
461678c
-
461678c
-		if (!enroll->keytab_enctypes_explicit && value != NULL) {
461678c
-			read_enctypes = _adcli_krb5_parse_enctypes (value);
461678c
-			if (read_enctypes == NULL) {
461678c
-				_adcli_warn ("Invalid or unsupported encryption types are set on "
461678c
-				             "the computer account (%s).", value);
461678c
-			} else {
461678c
-				free (enroll->keytab_enctypes);
461678c
-				enroll->keytab_enctypes = read_enctypes;
461678c
-			}
461678c
-		}
461678c
-
461678c
-	/* In 2003 or earlier, standard set of enc types */
461678c
-	} else {
461678c
-		value = _adcli_krb5_format_enctypes (v51_earlier_enctypes);
461678c
-	}
461678c
-
461678c
-	new_value = _adcli_krb5_format_enctypes (adcli_enroll_get_keytab_enctypes (enroll));
461678c
-	if (new_value == NULL) {
461678c
-		free (value);
461678c
-		_adcli_warn ("The encryption types desired are not available in active directory");
461678c
-		return ADCLI_ERR_CONFIG;
461678c
-	}
461678c
-
461678c
-	/* If we already have this value, then don't need to update */
461678c
-	if (value && strcmp (new_value, value) == 0) {
461678c
-		free (value);
461678c
+	ret = calculate_enctypes (enroll, &new_value);
461678c
+	if (ret != ADCLI_SUCCESS) {
461678c
 		free (new_value);
461678c
-		return ADCLI_SUCCESS;
461678c
+		return ret;
461678c
 	}
461678c
-	free (value);
461678c
 
461678c
-	if (!is_2008_or_later) {
461678c
-		free (new_value);
461678c
-		_adcli_warn ("Server does not support setting encryption types");
461678c
+	if (new_value == NULL) {
461678c
 		return ADCLI_SUCCESS;
461678c
 	}
461678c
 
461678c
-- 
461678c
2.14.4
461678c