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

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