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

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