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

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