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

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