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

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