Blame SOURCES/0007-service-account-add-random-suffix-to-account-name.patch

e06942
From 6b94f9712378b8f1fa1bc530c64cb987abb0c43b Mon Sep 17 00:00:00 2001
e06942
From: Sumit Bose <sbose@redhat.com>
e06942
Date: Tue, 27 Oct 2020 15:23:04 +0100
e06942
Subject: [PATCH 7/7] service-account: add random suffix to account name
e06942
e06942
Add a random component to the default managed service account name to
e06942
avoid name collisions.
e06942
e06942
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1854112
e06942
---
e06942
 library/adenroll.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++
e06942
 1 file changed, 79 insertions(+)
e06942
e06942
diff --git a/library/adenroll.c b/library/adenroll.c
e06942
index 98cd5fa..f693e58 100644
e06942
--- a/library/adenroll.c
e06942
+++ b/library/adenroll.c
e06942
@@ -1121,6 +1121,59 @@ load_computer_account (adcli_enroll *enroll,
e06942
 	return ADCLI_SUCCESS;
e06942
 }
e06942
 
e06942
+static adcli_result
e06942
+refresh_service_account_name_sam_and_princ (adcli_enroll *enroll,
e06942
+                                            const char *name)
e06942
+{
e06942
+	adcli_result res;
e06942
+
e06942
+	adcli_enroll_set_computer_name (enroll, name);
e06942
+	res = ensure_computer_sam (ADCLI_SUCCESS, enroll);
e06942
+	res = ensure_keytab_principals (res, enroll);
e06942
+
e06942
+	return res;
e06942
+}
e06942
+
e06942
+static adcli_result
e06942
+calculate_random_service_account_name (adcli_enroll *enroll)
e06942
+{
e06942
+	char *suffix;
e06942
+	char *new_name;
e06942
+	int ret;
e06942
+	adcli_result res;
e06942
+
e06942
+	suffix = generate_host_password (enroll, 3, filter_sam_chars);
e06942
+	return_unexpected_if_fail (suffix != NULL);
e06942
+
e06942
+	ret = asprintf (&new_name, "%s!%s", enroll->computer_name, suffix);
e06942
+	free (suffix);
e06942
+	return_unexpected_if_fail (ret > 0);
e06942
+
e06942
+	res = refresh_service_account_name_sam_and_princ (enroll, new_name);
e06942
+	free (new_name);
e06942
+
e06942
+	return res;
e06942
+}
e06942
+
e06942
+static adcli_result
e06942
+get_service_account_name_from_ldap (adcli_enroll *enroll, LDAPMessage *results)
e06942
+{
e06942
+	LDAP *ldap;
e06942
+	char *cn;
e06942
+	adcli_result res;
e06942
+
e06942
+	ldap = adcli_conn_get_ldap_connection (enroll->conn);
e06942
+	assert (ldap != NULL);
e06942
+
e06942
+	cn = _adcli_ldap_parse_value (ldap, results, "CN");
e06942
+	return_unexpected_if_fail (cn != NULL);
e06942
+
e06942
+	res = refresh_service_account_name_sam_and_princ (enroll, cn);
e06942
+	free (cn);
e06942
+
e06942
+	return res;
e06942
+}
e06942
+
e06942
 static adcli_result
e06942
 locate_or_create_computer_account (adcli_enroll *enroll,
e06942
                                    int allow_overwrite)
e06942
@@ -1143,8 +1196,32 @@ locate_or_create_computer_account (adcli_enroll *enroll,
e06942
 		searched = 1;
e06942
 	}
e06942
 
e06942
+	/* Try with fqdn for service accounts */
e06942
+	if (!enroll->computer_dn && enroll->is_service
e06942
+	                && enroll->host_fqdn != NULL) {
e06942
+		res = locate_computer_account (enroll, ldap, true,
e06942
+		                               &results, &entry);
e06942
+		if (res != ADCLI_SUCCESS)
e06942
+			return res;
e06942
+		searched = 1;
e06942
+
e06942
+		if (results != NULL) {
e06942
+			res = get_service_account_name_from_ldap (enroll,
e06942
+			                                          results);
e06942
+			if (res != ADCLI_SUCCESS) {
e06942
+				return res;
e06942
+			}
e06942
+		}
e06942
+	}
e06942
+
e06942
 	/* Next try and come up with where we think it should be */
e06942
 	if (enroll->computer_dn == NULL) {
e06942
+		if (enroll->is_service && !enroll->computer_name_explicit) {
e06942
+			res = calculate_random_service_account_name (enroll);
e06942
+			if (res != ADCLI_SUCCESS) {
e06942
+				return res;
e06942
+			}
e06942
+		}
e06942
 		res = calculate_computer_account (enroll, ldap);
e06942
 		if (res != ADCLI_SUCCESS)
e06942
 			return res;
e06942
@@ -2113,6 +2190,8 @@ adcli_enroll_prepare (adcli_enroll *enroll,
e06942
 
e06942
 	if (enroll->is_service) {
e06942
 		/* Ensure basic params for service accounts */
e06942
+		res = ensure_host_fqdn (res, enroll);
e06942
+		res = ensure_computer_name (res, enroll);
e06942
 		res = ensure_computer_sam (res, enroll);
e06942
 		res = ensure_computer_password (res, enroll);
e06942
 		res = ensure_host_keytab (res, enroll);
e06942
-- 
e06942
2.28.0
e06942