Sumit Bose 461678
From 27c7dde2c0e84c3bb610d1aadb0fd8faff70d3fa Mon Sep 17 00:00:00 2001
Sumit Bose 461678
From: Sumit Bose <sbose@redhat.com>
Sumit Bose 461678
Date: Fri, 1 Jun 2018 21:26:47 +0200
Sumit Bose 461678
Subject: [PATCH 17/23] Only update attributes given on the command line
Sumit Bose 461678
Sumit Bose 461678
When updating attributes of the LDAP computer object we only want to
Sumit Bose 461678
update attributes which are related to options given on the command
Sumit Bose 461678
line. Otherwise a simple call of 'adcli update' to check if the machine
Sumit Bose 461678
account password needs an update might unexpectedly reset other
Sumit Bose 461678
attributes as well.
Sumit Bose 461678
Sumit Bose 461678
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1547013
Sumit Bose 461678
           https://bugzilla.redhat.com/show_bug.cgi?id=1545568
Sumit Bose 461678
           https://bugzilla.redhat.com/show_bug.cgi?id=1538730
Sumit Bose 461678
---
Sumit Bose 461678
 library/adenroll.c | 35 ++++++++++++++++++++++++++++++-----
Sumit Bose 461678
 1 file changed, 30 insertions(+), 5 deletions(-)
Sumit Bose 461678
Sumit Bose 461678
diff --git a/library/adenroll.c b/library/adenroll.c
Sumit Bose 461678
index eca3c37..ee845ef 100644
Sumit Bose 461678
--- a/library/adenroll.c
Sumit Bose 461678
+++ b/library/adenroll.c
Sumit Bose 461678
@@ -99,8 +99,11 @@ struct _adcli_enroll {
Sumit Bose 461678
 	int user_princpal_generate;
Sumit Bose 461678
 
Sumit Bose 461678
 	char *os_name;
Sumit Bose 461678
+	int os_name_explicit;
Sumit Bose 461678
 	char *os_version;
Sumit Bose 461678
+	int os_version_explicit;
Sumit Bose 461678
 	char *os_service_pack;
Sumit Bose 461678
+	int os_service_pack_explicit;
Sumit Bose 461678
 
Sumit Bose 461678
 	krb5_kvno kvno;
Sumit Bose 461678
 	char *keytab_name;
Sumit Bose 461678
@@ -113,6 +116,7 @@ struct _adcli_enroll {
Sumit Bose 461678
 	int computer_password_lifetime_explicit;
Sumit Bose 461678
 	char *samba_data_tool;
Sumit Bose 461678
 	bool trusted_for_delegation;
Sumit Bose 461678
+	int trusted_for_delegation_explicit;
Sumit Bose 461678
 };
Sumit Bose 461678
 
Sumit Bose 461678
 static adcli_result
Sumit Bose 461678
@@ -1212,7 +1216,11 @@ update_computer_account (adcli_enroll *enroll)
Sumit Bose 461678
 	ldap = adcli_conn_get_ldap_connection (enroll->conn);
Sumit Bose 461678
 	return_if_fail (ldap != NULL);
Sumit Bose 461678
 
Sumit Bose 461678
-	{
Sumit Bose 461678
+	/* Only update attributes which are explicitly given on the command
Sumit Bose 461678
+	 * line. Otherwise 'adcli update' must be always called with the same
Sumit Bose 461678
+	 * set of options to make sure existing attributes are not deleted or
Sumit Bose 461678
+	 * overwritten with different values. */
Sumit Bose 461678
+	if (enroll->host_fqdn_explicit) {
Sumit Bose 461678
 		char *vals_dNSHostName[] = { enroll->host_fqdn, NULL };
Sumit Bose 461678
 		LDAPMod dNSHostName = { LDAP_MOD_REPLACE, "dNSHostName", { vals_dNSHostName, } };
Sumit Bose 461678
 		LDAPMod *mods[] = { &dNSHostName, NULL };
Sumit Bose 461678
@@ -1220,7 +1228,7 @@ update_computer_account (adcli_enroll *enroll)
Sumit Bose 461678
 		res |= update_computer_attribute (enroll, ldap, mods);
Sumit Bose 461678
 	}
Sumit Bose 461678
 
Sumit Bose 461678
-	if (res == ADCLI_SUCCESS) {
Sumit Bose 461678
+	if (res == ADCLI_SUCCESS && enroll->trusted_for_delegation_explicit) {
Sumit Bose 461678
 		char *vals_userAccountControl[] = { NULL , NULL };
Sumit Bose 461678
 		LDAPMod userAccountControl = { LDAP_MOD_REPLACE, "userAccountControl", { vals_userAccountControl, } };
Sumit Bose 461678
 		LDAPMod *mods[] = { &userAccountControl, NULL };
Sumit Bose 461678
@@ -1240,12 +1248,25 @@ update_computer_account (adcli_enroll *enroll)
Sumit Bose 461678
 		LDAPMod operatingSystemVersion = { LDAP_MOD_REPLACE, "operatingSystemVersion", { vals_operatingSystemVersion, } };
Sumit Bose 461678
 		char *vals_operatingSystemServicePack[] = { enroll->os_service_pack, NULL };
Sumit Bose 461678
 		LDAPMod operatingSystemServicePack = { LDAP_MOD_REPLACE, "operatingSystemServicePack", { vals_operatingSystemServicePack, } };
Sumit Bose 461678
-		LDAPMod *mods[] = { &operatingSystem, &operatingSystemVersion, &operatingSystemServicePack, NULL };
Sumit Bose 461678
+		LDAPMod *mods[] = { NULL, NULL, NULL, NULL };
Sumit Bose 461678
+		size_t c = 0;
Sumit Bose 461678
 
Sumit Bose 461678
-		res |= update_computer_attribute (enroll, ldap, mods);
Sumit Bose 461678
+		if (enroll->os_name_explicit) {
Sumit Bose 461678
+			mods[c++] = &operatingSystem;
Sumit Bose 461678
+		}
Sumit Bose 461678
+		if (enroll->os_version_explicit) {
Sumit Bose 461678
+			mods[c++] = &operatingSystemVersion;
Sumit Bose 461678
+		}
Sumit Bose 461678
+		if (enroll->os_service_pack_explicit) {
Sumit Bose 461678
+			mods[c++] = &operatingSystemServicePack;
Sumit Bose 461678
+		}
Sumit Bose 461678
+
Sumit Bose 461678
+		if (c != 0) {
Sumit Bose 461678
+			res |= update_computer_attribute (enroll, ldap, mods);
Sumit Bose 461678
+		}
Sumit Bose 461678
 	}
Sumit Bose 461678
 
Sumit Bose 461678
-	if (res == ADCLI_SUCCESS) {
Sumit Bose 461678
+	if (res == ADCLI_SUCCESS && !enroll->user_princpal_generate) {
Sumit Bose 461678
 		char *vals_userPrincipalName[] = { enroll->user_principal, NULL };
Sumit Bose 461678
 		LDAPMod userPrincipalName = { LDAP_MOD_REPLACE, "userPrincipalName", { vals_userPrincipalName, }, };
Sumit Bose 461678
 		LDAPMod *mods[] = { &userPrincipalName, NULL, };
Sumit Bose 461678
@@ -2337,6 +2358,7 @@ adcli_enroll_set_os_name (adcli_enroll *enroll,
Sumit Bose 461678
 	if (value && value[0] == '\0')
Sumit Bose 461678
 		value = NULL;
Sumit Bose 461678
 	_adcli_str_set (&enroll->os_name, value);
Sumit Bose 461678
+	enroll->os_name_explicit = 1;
Sumit Bose 461678
 }
Sumit Bose 461678
 
Sumit Bose 461678
 const char *
Sumit Bose 461678
@@ -2354,6 +2376,7 @@ adcli_enroll_set_os_version (adcli_enroll *enroll,
Sumit Bose 461678
 	if (value && value[0] == '\0')
Sumit Bose 461678
 		value = NULL;
Sumit Bose 461678
 	_adcli_str_set (&enroll->os_version, value);
Sumit Bose 461678
+	enroll->os_version_explicit = 1;
Sumit Bose 461678
 }
Sumit Bose 461678
 
Sumit Bose 461678
 const char *
Sumit Bose 461678
@@ -2371,6 +2394,7 @@ adcli_enroll_set_os_service_pack (adcli_enroll *enroll,
Sumit Bose 461678
 	if (value && value[0] == '\0')
Sumit Bose 461678
 		value = NULL;
Sumit Bose 461678
 	_adcli_str_set (&enroll->os_service_pack, value);
Sumit Bose 461678
+	enroll->os_service_pack_explicit = 1;
Sumit Bose 461678
 }
Sumit Bose 461678
 
Sumit Bose 461678
 const char *
Sumit Bose 461678
@@ -2450,4 +2474,5 @@ adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
Sumit Bose 461678
 	return_if_fail (enroll != NULL);
Sumit Bose 461678
 
Sumit Bose 461678
 	enroll->trusted_for_delegation = value;
Sumit Bose 461678
+	enroll->trusted_for_delegation_explicit = 1;
Sumit Bose 461678
 }
Sumit Bose 461678
-- 
Sumit Bose 461678
2.14.4
Sumit Bose 461678