Blame SOURCES/0006-enroll-allow-fqdn-for-locate_computer_account.patch

436d93
From 2a695dfe09cafeee3a648d3b969c364f8d3f494f Mon Sep 17 00:00:00 2001
436d93
From: Sumit Bose <sbose@redhat.com>
436d93
Date: Tue, 27 Oct 2020 14:49:55 +0100
436d93
Subject: [PATCH 6/7] enroll: allow fqdn for locate_computer_account
436d93
436d93
Make it possible to find existing manages service account by the
436d93
fully-qualified name.
436d93
436d93
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1854112
436d93
---
436d93
 library/adenroll.c | 45 +++++++++++++++++++++++++++++++--------------
436d93
 1 file changed, 31 insertions(+), 14 deletions(-)
436d93
436d93
diff --git a/library/adenroll.c b/library/adenroll.c
436d93
index 05bb085..98cd5fa 100644
436d93
--- a/library/adenroll.c
436d93
+++ b/library/adenroll.c
436d93
@@ -990,10 +990,11 @@ delete_computer_account (adcli_enroll *enroll,
436d93
 static adcli_result
436d93
 locate_computer_account (adcli_enroll *enroll,
436d93
                          LDAP *ldap,
436d93
+                         bool use_fqdn,
436d93
                          LDAPMessage **rresults,
436d93
                          LDAPMessage **rentry)
436d93
 {
436d93
-	char *attrs[] = { "objectClass", NULL };
436d93
+	char *attrs[] = { "objectClass", "CN", NULL };
436d93
 	LDAPMessage *results = NULL;
436d93
 	LDAPMessage *entry = NULL;
436d93
 	const char *base;
436d93
@@ -1003,12 +1004,22 @@ locate_computer_account (adcli_enroll *enroll,
436d93
 	int ret = 0;
436d93
 
436d93
 	/* If we don't yet know our computer dn, then try and find it */
436d93
-	value = _adcli_ldap_escape_filter (enroll->computer_sam);
436d93
-	return_unexpected_if_fail (value != NULL);
436d93
-	if (asprintf (&filter, "(&(objectClass=%s)(sAMAccountName=%s))",
436d93
-	              enroll->is_service ? "msDS-ManagedServiceAccount" : "computer",
436d93
-	              value) < 0)
436d93
-		return_unexpected_if_reached ();
436d93
+	if (use_fqdn) {
436d93
+		return_unexpected_if_fail (enroll->host_fqdn != NULL);
436d93
+		value = _adcli_ldap_escape_filter (enroll->host_fqdn);
436d93
+		return_unexpected_if_fail (value != NULL);
436d93
+		if (asprintf (&filter, "(&(objectClass=%s)(dNSHostName=%s))",
436d93
+		              enroll->is_service ? "msDS-ManagedServiceAccount" : "computer",
436d93
+		              value) < 0)
436d93
+			return_unexpected_if_reached ();
436d93
+	} else {
436d93
+		value = _adcli_ldap_escape_filter (enroll->computer_sam);
436d93
+		return_unexpected_if_fail (value != NULL);
436d93
+		if (asprintf (&filter, "(&(objectClass=%s)(sAMAccountName=%s))",
436d93
+		              enroll->is_service ? "msDS-ManagedServiceAccount" : "computer",
436d93
+		              value) < 0)
436d93
+			return_unexpected_if_reached ();
436d93
+	}
436d93
 	free (value);
436d93
 
436d93
 	base = adcli_conn_get_default_naming_context (enroll->conn);
436d93
@@ -1031,21 +1042,26 @@ locate_computer_account (adcli_enroll *enroll,
436d93
 			enroll->computer_dn = strdup (dn);
436d93
 			return_unexpected_if_fail (enroll->computer_dn != NULL);
436d93
 			_adcli_info ("Found %s account for %s at: %s",
436d93
-			             s_or_c (enroll), enroll->computer_sam, dn);
436d93
+			             s_or_c (enroll),
436d93
+			             use_fqdn ? enroll->host_fqdn
436d93
+			                      : enroll->computer_sam, dn);
436d93
 			ldap_memfree (dn);
436d93
 
436d93
 		} else {
436d93
 			ldap_msgfree (results);
436d93
 			results = NULL;
436d93
 			_adcli_info ("A %s account for %s does not exist",
436d93
-			             s_or_c (enroll), enroll->computer_sam);
436d93
+			             s_or_c (enroll),
436d93
+			             use_fqdn ? enroll->host_fqdn
436d93
+			                      : enroll->computer_sam);
436d93
 		}
436d93
 
436d93
 	} else {
436d93
 		return _adcli_ldap_handle_failure (ldap, ADCLI_ERR_DIRECTORY,
436d93
 		                                   "Couldn't lookup %s account: %s",
436d93
 		                                   s_or_c (enroll),
436d93
-		                                   enroll->computer_sam);
436d93
+		                                   use_fqdn ? enroll->host_fqdn
436d93
+		                                            :enroll->computer_sam);
436d93
 	}
436d93
 
436d93
 	if (rresults)
436d93
@@ -1120,7 +1136,8 @@ locate_or_create_computer_account (adcli_enroll *enroll,
436d93
 
436d93
 	/* Try to find the computer account */
436d93
 	if (!enroll->computer_dn) {
436d93
-		res = locate_computer_account (enroll, ldap, &results, &entry);
436d93
+		res = locate_computer_account (enroll, ldap, false,
436d93
+		                               &results, &entry);
436d93
 		if (res != ADCLI_SUCCESS)
436d93
 			return res;
436d93
 		searched = 1;
436d93
@@ -2395,7 +2412,7 @@ adcli_enroll_read_computer_account (adcli_enroll *enroll,
436d93
 
436d93
 	/* Find the computer dn */
436d93
 	if (!enroll->computer_dn) {
436d93
-		res = locate_computer_account (enroll, ldap, NULL, NULL);
436d93
+		res = locate_computer_account (enroll, ldap, false, NULL, NULL);
436d93
 		if (res != ADCLI_SUCCESS)
436d93
 			return res;
436d93
 		if (!enroll->computer_dn) {
436d93
@@ -2508,7 +2525,7 @@ adcli_enroll_delete (adcli_enroll *enroll,
436d93
 
436d93
 	/* Find the computer dn */
436d93
 	if (!enroll->computer_dn) {
436d93
-		res = locate_computer_account (enroll, ldap, NULL, NULL);
436d93
+		res = locate_computer_account (enroll, ldap, false, NULL, NULL);
436d93
 		if (res != ADCLI_SUCCESS)
436d93
 			return res;
436d93
 		if (!enroll->computer_dn) {
436d93
@@ -2552,7 +2569,7 @@ adcli_enroll_password (adcli_enroll *enroll,
436d93
 
436d93
 	/* Find the computer dn */
436d93
 	if (!enroll->computer_dn) {
436d93
-		res = locate_computer_account (enroll, ldap, NULL, NULL);
436d93
+		res = locate_computer_account (enroll, ldap, false, NULL, NULL);
436d93
 		if (res != ADCLI_SUCCESS)
436d93
 			return res;
436d93
 		if (!enroll->computer_dn) {
436d93
-- 
436d93
2.28.0
436d93