Blame SOURCES/0001-Add-setattr-option.patch

419917
From c5b0cee2976682b4fc1aeb02636cc9f2c6dbc2a5 Mon Sep 17 00:00:00 2001
419917
From: Sumit Bose <sbose@redhat.com>
419917
Date: Mon, 14 Jun 2021 07:54:01 +0200
419917
Subject: [PATCH 1/2] Add setattr option
419917
419917
With the new option common LDAP attributes can be set.
419917
419917
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1690920
419917
---
419917
 doc/adcli.xml      |  34 +++++++++
419917
 library/adenroll.c | 169 ++++++++++++++++++++++++++++++++++++++++++++-
419917
 library/adenroll.h |   4 ++
419917
 tools/computer.c   |  10 +++
419917
 4 files changed, 216 insertions(+), 1 deletion(-)
419917
419917
diff --git a/doc/adcli.xml b/doc/adcli.xml
419917
index 6c36297..8383aa7 100644
419917
--- a/doc/adcli.xml
419917
+++ b/doc/adcli.xml
419917
@@ -374,6 +374,23 @@ Password for Administrator:
419917
 			service should be accessible with a different host
419917
 			name as well.</para></listitem>
419917
 		</varlistentry>
419917
+		<varlistentry>
419917
+			<term><option>--setattr=<parameter>name=value</parameter></option></term>
419917
+			<listitem><para>Add the LDAP attribute
419917
+			<option><parameter>name</parameter></option> with the
419917
+			given <option><parameter>value</parameter></option> to
419917
+			the new LDAP host object.
419917
+			This option can be used multiple times to add multiple
419917
+			different attributes. Multi-value attributes are
419917
+			currently not supported.</para>
419917
+			<para>Please note that the account used to join the
419917
+			domain must have the required privileges to add the
419917
+			given attributes. Some attributes might have
419917
+			constraints with respect to syntax and allowed values
419917
+			which must be met as well. Attributes managed by other
419917
+			adcli options cannot be set with this option.</para>
419917
+			</listitem>
419917
+		</varlistentry>
419917
 		<varlistentry>
419917
 			<term><option>--show-details</option></term>
419917
 			<listitem><para>After a successful join print out information
419917
@@ -543,6 +560,23 @@ $ adcli update --login-ccache=/tmp/krbcc_123
419917
 			<listitem><para>Remove a service principal name from
419917
 			the keytab and the AD host object.</para></listitem>
419917
 		</varlistentry>
419917
+		<varlistentry>
419917
+			<term><option>--setattr=<parameter>name=value</parameter></option></term>
419917
+			<listitem><para>Add the LDAP attribute
419917
+			<option><parameter>name</parameter></option> with the
419917
+			given <option><parameter>value</parameter></option> to
419917
+			the LDAP host object.
419917
+			This option can be used multiple times to add multiple
419917
+			different attributes. Multi-value attributes are
419917
+			currently not supported.</para>
419917
+			<para>Please note that the account used to update the
419917
+			host object must have the required privileges to modify
419917
+			the given attributes. Some attributes might have
419917
+			constraints with respect to syntax and allowed values
419917
+			which must be met as well. Attributes managed by other
419917
+			adcli options cannot be set with this option.</para>
419917
+			</listitem>
419917
+		</varlistentry>
419917
 		<varlistentry>
419917
 			<term><option>--show-details</option></term>
419917
 			<listitem><para>After a successful join print out information
419917
diff --git a/library/adenroll.c b/library/adenroll.c
419917
index 0b1c066..dd51567 100644
419917
--- a/library/adenroll.c
419917
+++ b/library/adenroll.c
419917
@@ -150,4 +150,5 @@ struct _adcli_enroll {
419917
 	char *description;
419917
+	char **setattr;
419917
 };
419917
 
419917
 static const char *
419917
@@ -795,6 +796,56 @@ calculate_enctypes (adcli_enroll *enroll, char **enctype)
419917
 	return ADCLI_SUCCESS;
419917
 }
419917
 
419917
+static LDAPMod **
419917
+get_mods_for_attrs (adcli_enroll *enroll, int mod_op)
419917
+{
419917
+	size_t len;
419917
+	size_t c;
419917
+	char *end;
419917
+	LDAPMod **mods = NULL;
419917
+
419917
+	len = _adcli_strv_len (enroll->setattr);
419917
+	if (len == 0) {
419917
+		return NULL;
419917
+	}
419917
+
419917
+	mods = calloc (len + 1, sizeof (LDAPMod *));
419917
+	return_val_if_fail (mods != NULL, NULL);
419917
+
419917
+	for (c = 0; c < len; c++) {
419917
+		end = strchr (enroll->setattr[c], '=');
419917
+		if (end == NULL) {
419917
+			ldap_mods_free (mods, 1);
419917
+			return NULL;
419917
+		}
419917
+
419917
+		mods[c] = calloc (1, sizeof (LDAPMod));
419917
+		if (mods[c] == NULL) {
419917
+			ldap_mods_free (mods, 1);
419917
+			return NULL;
419917
+		}
419917
+
419917
+		mods[c]->mod_op = mod_op;
419917
+		*end = '\0';
419917
+		mods[c]->mod_type = strdup (enroll->setattr[c]);
419917
+		*end = '=';
419917
+		mods[c]->mod_values = calloc (2, sizeof (char *));
419917
+		if (mods[c]->mod_type == NULL || mods[c]->mod_values == NULL) {
419917
+			ldap_mods_free (mods, 1);
419917
+			return NULL;
419917
+		}
419917
+
419917
+		mods[c]->mod_values[0] = strdup (end + 1);
419917
+		if (mods[c]->mod_values[0] == NULL) {
419917
+			ldap_mods_free (mods, 1);
419917
+			return NULL;
419917
+		}
419917
+	}
419917
+
419917
+	return mods;
419917
+}
419917
+
419917
+
419917
 static adcli_result
419917
 create_computer_account (adcli_enroll *enroll,
419917
                          LDAP *ldap)
419917
@@ -828,6 +879,7 @@ create_computer_account (adcli_enroll *enroll,
419917
 	size_t m;
419917
 	uint32_t uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD ;
419917
 	char *uac_str = NULL;
419917
+	LDAPMod **extra_mods = NULL;
419917
 
419917
 	LDAPMod *all_mods[] = {
419917
 		&objectClass,
419917
@@ -845,7 +897,7 @@ create_computer_account (adcli_enroll *enroll,
419917
 	};
419917
 
419917
 	size_t mods_count = sizeof (all_mods) / sizeof (LDAPMod *);
419917
-	LDAPMod *mods[mods_count];
419917
+	LDAPMod **mods;
419917
 
419917
 	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
419917
 		uac |= UAC_TRUSTED_FOR_DELEGATION;
419917
@@ -868,6 +920,17 @@ create_computer_account (adcli_enroll *enroll,
419917
 	}
419917
 	vals_supportedEncryptionTypes[0] = val;
419917
 
419917
+	if (enroll->setattr != NULL) {
419917
+		extra_mods = get_mods_for_attrs (enroll, LDAP_MOD_ADD);
419917
+		if (extra_mods == NULL) {
419917
+			_adcli_err ("Failed to add setattr attributes, "
419917
+			            "just using defaults");
419917
+		}
419917
+	}
419917
+
419917
+	mods = calloc (mods_count + seq_count (extra_mods) + 1, sizeof (LDAPMod *));
419917
+	return_val_if_fail (mods != NULL, ADCLI_ERR_UNEXPECTED);
419917
+
419917
 	m = 0;
419917
 	for (c = 0; c < mods_count - 1; c++) {
419917
 		/* Skip empty LDAP sttributes */
419917
@@ -875,9 +938,15 @@ create_computer_account (adcli_enroll *enroll,
419917
 			mods[m++] = all_mods[c];
419917
 		}
419917
 	}
419917
+
419917
+	for (c = 0; c < seq_count (extra_mods); c++) {
419917
+		mods[m++] = extra_mods[c];
419917
+	}
419917
 	mods[m] = NULL;
419917
 
419917
 	ret = ldap_add_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
419917
+	ldap_mods_free (extra_mods, 1);
419917
+	free (mods);
419917
 	free (uac_str);
419917
 	free (val);
419917
 
419917
@@ -1698,6 +1767,14 @@ update_computer_account (adcli_enroll *enroll)
419917
 		res |= update_computer_attribute (enroll, ldap, mods);
419917
 	}
419917
 
419917
+	if (res == ADCLI_SUCCESS && enroll->setattr != NULL) {
419917
+		LDAPMod **mods = get_mods_for_attrs (enroll, LDAP_MOD_REPLACE);
419917
+		if (mods != NULL) {
419917
+			res |= update_computer_attribute (enroll, ldap, mods);
419917
+			ldap_mods_free (mods, 1);
419917
+		}
419917
+	}
419917
+
419917
 	if (res != 0)
419917
 		_adcli_info ("Updated existing computer account: %s", enroll->computer_dn);
419917
 }
419917
@@ -2751,6 +2828,7 @@ enroll_free (adcli_enroll *enroll)
419917
 	free (enroll->user_principal);
419917
 	_adcli_strv_free (enroll->service_names);
419917
 	_adcli_strv_free (enroll->service_principals);
419917
+	_adcli_strv_free (enroll->setattr);
419917
 	_adcli_password_free (enroll->computer_password);
419917
 
419917
 	adcli_enroll_set_keytab_name (enroll, NULL);
419917
@@ -3332,6 +3410,72 @@ adcli_enroll_add_service_principal_to_remove (adcli_enroll *enroll,
419917
 	return_if_fail (enroll->service_principals_to_remove != NULL);
419917
 }
419917
 
419917
+static int comp_attr_name (const char *s1, const char *s2)
419917
+{
419917
+	size_t c = 0;
419917
+
419917
+	/* empty strings cannot contain an attribute name */
419917
+	if (s1 == NULL || s2 == NULL || *s1 == '\0' || *s2 == '\0') {
419917
+		return 1;
419917
+	}
419917
+
419917
+	for (c = 0 ; s1[c] != '\0' && s2[c] != '\0'; c++) {
419917
+		if (s1[c] == '=' && s2[c] == '=') {
419917
+			return 0;
419917
+		} else if (tolower (s1[c]) != tolower (s2[c])) {
419917
+			return 1;
419917
+		}
419917
+	}
419917
+
419917
+	return 1;
419917
+}
419917
+
419917
+adcli_result
419917
+adcli_enroll_add_setattr (adcli_enroll *enroll, const char *value)
419917
+{
419917
+	char *delim;
419917
+
419917
+	return_val_if_fail (enroll != NULL, ADCLI_ERR_CONFIG);
419917
+	return_val_if_fail (value != NULL, ADCLI_ERR_CONFIG);
419917
+
419917
+	delim = strchr (value, '=');
419917
+	if (delim == NULL) {
419917
+		_adcli_err ("Missing '=' in setattr option [%s]", value);
419917
+		return ADCLI_ERR_CONFIG;
419917
+	}
419917
+
419917
+	if (*(delim + 1) == '\0') {
419917
+		_adcli_err ("Missing value in setattr option [%s]", value);
419917
+		return ADCLI_ERR_CONFIG;
419917
+	}
419917
+
419917
+	*delim = '\0';
419917
+	if (_adcli_strv_has_ex (default_ad_ldap_attrs, value, strcasecmp) == 1) {
419917
+		_adcli_err ("Attribute [%s] cannot be set with setattr", value);
419917
+		return ADCLI_ERR_CONFIG;
419917
+	}
419917
+	*delim = '=';
419917
+
419917
+	if (_adcli_strv_has_ex (enroll->setattr, value, comp_attr_name) == 1) {
419917
+		_adcli_err ("Attribute [%s] already set", value);
419917
+		return ADCLI_ERR_CONFIG;
419917
+	}
419917
+
419917
+	enroll->setattr = _adcli_strv_add (enroll->setattr, strdup (value),
419917
+	                                   NULL);
419917
+	return_val_if_fail (enroll->setattr != NULL, ADCLI_ERR_CONFIG);
419917
+
419917
+	return ADCLI_SUCCESS;
419917
+}
419917
+
419917
+const char **
419917
+adcli_enroll_get_setattr (adcli_enroll *enroll)
419917
+{
419917
+	return_val_if_fail (enroll != NULL, NULL);
419917
+	return (const char **) enroll->setattr;
419917
+}
419917
+
419917
+
419917
 #ifdef ADENROLL_TESTS
419917
 
419917
 #include "test.h"
419917
@@ -3401,12 +3545,35 @@ test_adcli_enroll_get_permitted_keytab_enctypes (void)
419917
 	adcli_conn_unref (conn);
419917
 }
419917
 
419917
+static void
419917
+test_comp_attr_name (void)
419917
+{
419917
+	assert_num_eq (1, comp_attr_name (NULL ,NULL));
419917
+	assert_num_eq (1, comp_attr_name ("" ,NULL));
419917
+	assert_num_eq (1, comp_attr_name ("" ,""));
419917
+	assert_num_eq (1, comp_attr_name (NULL ,""));
419917
+	assert_num_eq (1, comp_attr_name (NULL ,"abc=xyz"));
419917
+	assert_num_eq (1, comp_attr_name ("" ,"abc=xyz"));
419917
+	assert_num_eq (1, comp_attr_name ("abc=xyz", NULL));
419917
+	assert_num_eq (1, comp_attr_name ("abc=xyz", ""));
419917
+	assert_num_eq (1, comp_attr_name ("abc=xyz", "ab=xyz"));
419917
+	assert_num_eq (1, comp_attr_name ("ab=xyz", "abc=xyz"));
419917
+	assert_num_eq (1, comp_attr_name ("abcxyz", "abc=xyz"));
419917
+	assert_num_eq (1, comp_attr_name ("abc=xyz", "abcxyz"));
419917
+	assert_num_eq (1, comp_attr_name ("abc=xyz", "a"));
419917
+	assert_num_eq (1, comp_attr_name ("a", "abc=xyz"));
419917
+
419917
+	assert_num_eq (0, comp_attr_name ("abc=xyz", "abc=xyz"));
419917
+	assert_num_eq (0, comp_attr_name ("abc=xyz", "abc=123"));
419917
+}
419917
+
419917
 int
419917
 main (int argc,
419917
       char *argv[])
419917
 {
419917
 	test_func (test_adcli_enroll_get_permitted_keytab_enctypes,
419917
 	           "/attrs/adcli_enroll_get_permitted_keytab_enctypes");
419917
+	test_func (test_comp_attr_name, "/attrs/comp_attr_name");
419917
 	return test_run (argc, argv);
419917
 }
419917
 
419917
diff --git a/library/adenroll.h b/library/adenroll.h
419917
index 34dc683..862bb60 100644
419917
--- a/library/adenroll.h
419917
+++ b/library/adenroll.h
419917
@@ -138,6 +138,10 @@ const char *       adcli_enroll_get_desciption          (adcli_enroll *enroll);
419917
 void               adcli_enroll_set_description         (adcli_enroll *enroll,
419917
                                                          const char *value);
419917
 
419917
+const char **      adcli_enroll_get_setattr             (adcli_enroll *enroll);
419917
+adcli_result       adcli_enroll_add_setattr             (adcli_enroll *enroll,
419917
+                                                         const char *value);
419917
+
419917
 bool               adcli_enroll_get_is_service          (adcli_enroll *enroll);
419917
 void               adcli_enroll_set_is_service          (adcli_enroll *enroll,
419917
                                                          bool value);
419917
diff --git a/tools/computer.c b/tools/computer.c
419917
index 16a1983..af38894 100644
419917
--- a/tools/computer.c
419917
+++ b/tools/computer.c
419917
@@ -114,6 +114,7 @@ typedef enum {
419917
 	opt_add_service_principal,
419917
 	opt_remove_service_principal,
419917
 	opt_description,
419917
+	opt_setattr,
419917
 	opt_use_ldaps,
419917
 } Option;
419917
 
419917
@@ -152,6 +153,7 @@ static adcli_tool_desc common_usages[] = {
419917
 	{ opt_add_service_principal, "add the given service principal to the account\n" },
419917
 	{ opt_remove_service_principal, "remove the given service principal from the account\n" },
419917
 	{ opt_description, "add a description to the account\n" },
419917
+	{ opt_setattr, "add an attribute with a value\n" },
419917
 	{ opt_no_password, "don't prompt for or read a password" },
419917
 	{ opt_prompt_password, "prompt for a password if necessary" },
419917
 	{ opt_stdin_password, "read a password from stdin (until EOF) if\n"
419917
@@ -333,6 +335,12 @@ parse_option (Option opt,
419917
 	case opt_description:
419917
 		adcli_enroll_set_description (enroll, optarg);
419917
 		return ADCLI_SUCCESS;
419917
+	case opt_setattr:
419917
+		ret =  adcli_enroll_add_setattr (enroll, optarg);
419917
+		if (ret != ADCLI_SUCCESS) {
419917
+			warnx ("parsing setattr option failed");
419917
+		}
419917
+		return ret;
419917
 	case opt_use_ldaps:
419917
 		adcli_conn_set_use_ldaps (conn, true);
419917
 		return ADCLI_SUCCESS;
419917
@@ -401,6 +409,7 @@ adcli_tool_computer_join (adcli_conn *conn,
419917
 		{ "os-version", required_argument, NULL, opt_os_version },
419917
 		{ "os-service-pack", optional_argument, NULL, opt_os_service_pack },
419917
 		{ "description", optional_argument, NULL, opt_description },
419917
+		{ "setattr", required_argument, NULL, opt_setattr },
419917
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
419917
 		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
419917
 		{ "dont-expire-password", required_argument, NULL, opt_dont_expire_password },
419917
@@ -524,6 +533,7 @@ adcli_tool_computer_update (adcli_conn *conn,
419917
 		{ "os-version", required_argument, NULL, opt_os_version },
419917
 		{ "os-service-pack", optional_argument, NULL, opt_os_service_pack },
419917
 		{ "description", optional_argument, NULL, opt_description },
419917
+		{ "setattr", required_argument, NULL, opt_setattr },
419917
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
419917
 		{ "computer-password-lifetime", optional_argument, NULL, opt_computer_password_lifetime },
419917
 		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
419917
-- 
419917
2.31.1
419917