Blame SOURCES/0002-Add-delattr-option.patch

2d8b37
From cd5b6cdcf3e6bfc5776f2865f460f608421dfa3f Mon Sep 17 00:00:00 2001
2d8b37
From: Sumit Bose <sbose@redhat.com>
2d8b37
Date: Mon, 14 Jun 2021 08:42:21 +0200
2d8b37
Subject: [PATCH 2/2] Add delattr option
2d8b37
2d8b37
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1690920
2d8b37
---
2d8b37
 doc/adcli.xml      | 11 ++++++++
2d8b37
 library/adenroll.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++
2d8b37
 library/adenroll.h |  4 +++
2d8b37
 tools/computer.c   |  9 +++++++
2d8b37
 4 files changed, 90 insertions(+)
2d8b37
2d8b37
diff --git a/doc/adcli.xml b/doc/adcli.xml
2d8b37
index 8383aa7..bcf4857 100644
2d8b37
--- a/doc/adcli.xml
2d8b37
+++ b/doc/adcli.xml
2d8b37
@@ -577,6 +577,17 @@ $ adcli update --login-ccache=/tmp/krbcc_123
2d8b37
 			adcli options cannot be set with this option.</para>
2d8b37
 			</listitem>
2d8b37
 		</varlistentry>
2d8b37
+		<varlistentry>
2d8b37
+			<term><option>--delattr=<parameter>name</parameter></option></term>
2d8b37
+			<listitem><para>Remove the LDAP attribute
2d8b37
+			<option><parameter>name</parameter></option> from the
2d8b37
+			LDAP host object. This option can be used multiple
2d8b37
+			times to remove multiple different attributes.</para>
2d8b37
+			<para>Please note that the account used to update the
2d8b37
+			host object must have the required privileges to delete
2d8b37
+			the given attributes. Attributes managed by other adcli
2d8b37
+			options cannot be removed.</para></listitem>
2d8b37
+		</varlistentry>
2d8b37
 		<varlistentry>
2d8b37
 			<term><option>--show-details</option></term>
2d8b37
 			<listitem><para>After a successful join print out information
2d8b37
diff --git a/library/adenroll.c b/library/adenroll.c
2d8b37
index dd51567..9a06d52 100644
2d8b37
--- a/library/adenroll.c
2d8b37
+++ b/library/adenroll.c
2d8b37
@@ -151,5 +151,6 @@ struct _adcli_enroll {
2d8b37
 	char *description;
2d8b37
 	char **setattr;
2d8b37
+	char **delattr;
2d8b37
 };
2d8b37
 
2d8b37
 static const char *
2d8b37
@@ -845,6 +846,39 @@ get_mods_for_attrs (adcli_enroll *enroll, int mod_op)
2d8b37
 	return mods;
2d8b37
 }
2d8b37
 
2d8b37
+static LDAPMod **
2d8b37
+get_del_mods_for_attrs (adcli_enroll *enroll, int mod_op)
2d8b37
+{
2d8b37
+	size_t len;
2d8b37
+	size_t c;
2d8b37
+	LDAPMod **mods = NULL;
2d8b37
+
2d8b37
+	len = _adcli_strv_len (enroll->delattr);
2d8b37
+	if (len == 0) {
2d8b37
+		return NULL;
2d8b37
+	}
2d8b37
+
2d8b37
+	mods = calloc (len + 1, sizeof (LDAPMod *));
2d8b37
+	return_val_if_fail (mods != NULL, NULL);
2d8b37
+
2d8b37
+	for (c = 0; c < len; c++) {
2d8b37
+		mods[c] = calloc (1, sizeof (LDAPMod));
2d8b37
+		if (mods[c] == NULL) {
2d8b37
+			ldap_mods_free (mods, 1);
2d8b37
+			return NULL;
2d8b37
+		}
2d8b37
+
2d8b37
+		mods[c]->mod_op = mod_op;
2d8b37
+		mods[c]->mod_type = strdup (enroll->delattr[c]);
2d8b37
+		mods[c]->mod_values = NULL;
2d8b37
+		if (mods[c]->mod_type == NULL) {
2d8b37
+			ldap_mods_free (mods, 1);
2d8b37
+			return NULL;
2d8b37
+		}
2d8b37
+	}
2d8b37
+
2d8b37
+	return mods;
2d8b37
+}
2d8b37
 
2d8b37
 static adcli_result
2d8b37
 create_computer_account (adcli_enroll *enroll,
2d8b37
@@ -1775,6 +1809,14 @@ update_computer_account (adcli_enroll *enroll)
2d8b37
 		}
2d8b37
 	}
2d8b37
 
2d8b37
+	if (res == ADCLI_SUCCESS && enroll->delattr != NULL) {
2d8b37
+		LDAPMod **mods = get_del_mods_for_attrs (enroll, LDAP_MOD_DELETE);
2d8b37
+		if (mods != NULL) {
2d8b37
+			res |= update_computer_attribute (enroll, ldap, mods);
2d8b37
+			ldap_mods_free (mods, 1);
2d8b37
+		}
2d8b37
+	}
2d8b37
+
2d8b37
 	if (res != 0)
2d8b37
 		_adcli_info ("Updated existing computer account: %s", enroll->computer_dn);
2d8b37
 }
2d8b37
@@ -3475,6 +3517,30 @@ adcli_enroll_get_setattr (adcli_enroll *enroll)
2d8b37
 	return (const char **) enroll->setattr;
2d8b37
 }
2d8b37
 
2d8b37
+adcli_result
2d8b37
+adcli_enroll_add_delattr (adcli_enroll *enroll, const char *value)
2d8b37
+{
2d8b37
+	return_val_if_fail (enroll != NULL, ADCLI_ERR_CONFIG);
2d8b37
+	return_val_if_fail (value != NULL, ADCLI_ERR_CONFIG);
2d8b37
+
2d8b37
+	if (_adcli_strv_has_ex (default_ad_ldap_attrs, value, strcasecmp) == 1) {
2d8b37
+		_adcli_err ("Attribute [%s] cannot be removed with delattr", value);
2d8b37
+		return ADCLI_ERR_CONFIG;
2d8b37
+	}
2d8b37
+
2d8b37
+	enroll->delattr = _adcli_strv_add (enroll->delattr, strdup (value),
2d8b37
+	                                   NULL);
2d8b37
+	return_val_if_fail (enroll->delattr != NULL, ADCLI_ERR_CONFIG);
2d8b37
+
2d8b37
+	return ADCLI_SUCCESS;
2d8b37
+}
2d8b37
+
2d8b37
+const char **
2d8b37
+adcli_enroll_get_delattr (adcli_enroll *enroll)
2d8b37
+{
2d8b37
+	return_val_if_fail (enroll != NULL, NULL);
2d8b37
+	return (const char **) enroll->delattr;
2d8b37
+}
2d8b37
 
2d8b37
 #ifdef ADENROLL_TESTS
2d8b37
 
2d8b37
diff --git a/library/adenroll.h b/library/adenroll.h
2d8b37
index 862bb60..e3ada33 100644
2d8b37
--- a/library/adenroll.h
2d8b37
+++ b/library/adenroll.h
2d8b37
@@ -142,6 +142,10 @@ const char **      adcli_enroll_get_setattr             (adcli_enroll *enroll);
2d8b37
 adcli_result       adcli_enroll_add_setattr             (adcli_enroll *enroll,
2d8b37
                                                          const char *value);
2d8b37
 
2d8b37
+const char **      adcli_enroll_get_delattr             (adcli_enroll *enroll);
2d8b37
+adcli_result       adcli_enroll_add_delattr             (adcli_enroll *enroll,
2d8b37
+                                                         const char *value);
2d8b37
+
2d8b37
 bool               adcli_enroll_get_is_service          (adcli_enroll *enroll);
2d8b37
 void               adcli_enroll_set_is_service          (adcli_enroll *enroll,
2d8b37
                                                          bool value);
2d8b37
diff --git a/tools/computer.c b/tools/computer.c
2d8b37
index af38894..dffeecb 100644
2d8b37
--- a/tools/computer.c
2d8b37
+++ b/tools/computer.c
2d8b37
@@ -115,6 +115,7 @@ typedef enum {
2d8b37
 	opt_remove_service_principal,
2d8b37
 	opt_description,
2d8b37
 	opt_setattr,
2d8b37
+	opt_delattr,
2d8b37
 	opt_use_ldaps,
2d8b37
 } Option;
2d8b37
 
2d8b37
@@ -154,6 +155,7 @@ static adcli_tool_desc common_usages[] = {
2d8b37
 	{ opt_remove_service_principal, "remove the given service principal from the account\n" },
2d8b37
 	{ opt_description, "add a description to the account\n" },
2d8b37
 	{ opt_setattr, "add an attribute with a value\n" },
2d8b37
+	{ opt_delattr, "remove an attribute\n" },
2d8b37
 	{ opt_no_password, "don't prompt for or read a password" },
2d8b37
 	{ opt_prompt_password, "prompt for a password if necessary" },
2d8b37
 	{ opt_stdin_password, "read a password from stdin (until EOF) if\n"
2d8b37
@@ -341,6 +343,12 @@ parse_option (Option opt,
2d8b37
 			warnx ("parsing setattr option failed");
2d8b37
 		}
2d8b37
 		return ret;
2d8b37
+	case opt_delattr:
2d8b37
+		ret = adcli_enroll_add_delattr (enroll, optarg);
2d8b37
+		if (ret != ADCLI_SUCCESS) {
2d8b37
+			warnx ("parsing delattr option failed");
2d8b37
+		}
2d8b37
+		return ret;
2d8b37
 	case opt_use_ldaps:
2d8b37
 		adcli_conn_set_use_ldaps (conn, true);
2d8b37
 		return ADCLI_SUCCESS;
2d8b37
@@ -534,6 +542,7 @@ adcli_tool_computer_update (adcli_conn *conn,
2d8b37
 		{ "os-service-pack", optional_argument, NULL, opt_os_service_pack },
2d8b37
 		{ "description", optional_argument, NULL, opt_description },
2d8b37
 		{ "setattr", required_argument, NULL, opt_setattr },
2d8b37
+		{ "delattr", required_argument, NULL, opt_delattr },
2d8b37
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
2d8b37
 		{ "computer-password-lifetime", optional_argument, NULL, opt_computer_password_lifetime },
2d8b37
 		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
2d8b37
-- 
2d8b37
2.31.1
2d8b37