Blame SOURCES/0002-Add-dont-expire-password-option.patch

54f29a
From a78116ba0e608050f391223bad3834d48c9adf1b Mon Sep 17 00:00:00 2001
54f29a
From: Sumit Bose <sbose@redhat.com>
54f29a
Date: Wed, 2 Jun 2021 17:24:07 +0200
54f29a
Subject: [PATCH 2/2] Add dont-expire-password option
54f29a
54f29a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1769644
54f29a
---
54f29a
 doc/adcli.xml      | 28 ++++++++++++++++++++++++++++
54f29a
 library/adenroll.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
54f29a
 library/adenroll.h |  4 ++++
54f29a
 tools/computer.c   | 12 ++++++++++++
54f29a
 4 files changed, 87 insertions(+), 1 deletion(-)
54f29a
54f29a
diff --git a/doc/adcli.xml b/doc/adcli.xml
54f29a
index 8ec48d4..1ed5d3f 100644
54f29a
--- a/doc/adcli.xml
54f29a
+++ b/doc/adcli.xml
54f29a
@@ -347,6 +347,20 @@ Password for Administrator:
54f29a
 			not allow that Kerberos tickets can be forwarded to the
54f29a
 			host.</para></listitem>
54f29a
 		</varlistentry>
54f29a
+		<varlistentry>
54f29a
+			<term><option>--dont-expire-password=<parameter>yes|no|true|false</parameter></option></term>
54f29a
+			<listitem><para>Set or unset the DONT_EXPIRE_PASSWORD
54f29a
+			flag in the userAccountControl attribute to indicate if
54f29a
+			the machine account password should expire or not. By
54f29a
+			default adcli will set this flag while joining the
54f29a
+			domain which corresponds to the default behavior of
54f29a
+			Windows clients.</para>
54f29a
+			<para>Please note that if the password will expire
54f29a
+			(--dont-expire-password=false) a renewal mechanism has
54f29a
+			to be enabled on the client to not loose the
54f29a
+			connectivity to AD if the password expires.</para>
54f29a
+			</listitem>
54f29a
+		</varlistentry>
54f29a
 		<varlistentry>
54f29a
 			<term><option>--add-service-principal=<parameter>service/hostname</parameter></option></term>
54f29a
 			<listitem><para>Add a service principal name. In
54f29a
@@ -491,6 +505,20 @@ $ adcli update --login-ccache=/tmp/krbcc_123
54f29a
 			not allow that Kerberos tickets can be forwarded to the
54f29a
 			host.</para></listitem>
54f29a
 		</varlistentry>
54f29a
+		<varlistentry>
54f29a
+			<term><option>--dont-expire-password=<parameter>yes|no|true|false</parameter></option></term>
54f29a
+			<listitem><para>Set or unset the DONT_EXPIRE_PASSWORD
54f29a
+			flag in the userAccountControl attribute to indicate if
54f29a
+			the machine account password should expire or not. By
54f29a
+			default adcli will set this flag while joining the
54f29a
+			domain which corresponds to the default behavior of
54f29a
+			Windows clients.</para>
54f29a
+			<para>Please note that if the password will expire
54f29a
+			(--dont-expire-password=false) a renewal mechanism has
54f29a
+			to be enabled on the client to not loose the
54f29a
+			connectivity to AD if the password expires.</para>
54f29a
+			</listitem>
54f29a
+		</varlistentry>
54f29a
 		<varlistentry>
54f29a
 			<term><option>--account-disable=<parameter>yes|no|true|false</parameter></option></term>
54f29a
 			<listitem><para>Set or unset the ACCOUNTDISABLE
54f29a
diff --git a/library/adenroll.c b/library/adenroll.c
54f29a
index 0f3e8b9..7653f89 100644
54f29a
--- a/library/adenroll.c
54f29a
+++ b/library/adenroll.c
54f29a
@@ -153,6 +153,8 @@ struct _adcli_enroll {
54f29a
 	char *samba_data_tool;
54f29a
 	bool trusted_for_delegation;
54f29a
 	int trusted_for_delegation_explicit;
54f29a
+	bool dont_expire_password;
54f29a
+	int dont_expire_password_explicit;
54f29a
 	bool account_disable;
54f29a
 	int account_disable_explicit;
54f29a
 	char *description;
54f29a
@@ -832,6 +834,8 @@ create_computer_account (adcli_enroll *enroll,
54f29a
 	int ret;
54f29a
 	size_t c;
54f29a
 	size_t m;
54f29a
+	uint32_t uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD ;
54f29a
+	char *uac_str = NULL;
54f29a
 
54f29a
 	LDAPMod *all_mods[] = {
54f29a
 		&objectClass,
54f29a
@@ -852,11 +856,21 @@ create_computer_account (adcli_enroll *enroll,
54f29a
 	LDAPMod *mods[mods_count];
54f29a
 
54f29a
 	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
54f29a
-		vals_userAccountControl[0] = "593920"; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD | TRUSTED_FOR_DELEGATION */
54f29a
+		uac |= UAC_TRUSTED_FOR_DELEGATION;
54f29a
 	}
54f29a
 
54f29a
+	if (!adcli_enroll_get_dont_expire_password (enroll)) {
54f29a
+		uac &= ~(UAC_DONT_EXPIRE_PASSWORD);
54f29a
+	}
54f29a
+
54f29a
+	if (asprintf (&uac_str, "%d", uac) < 0) {
54f29a
+		return_val_if_reached (ADCLI_ERR_UNEXPECTED);
54f29a
+	}
54f29a
+	vals_userAccountControl[0] = uac_str;
54f29a
+
54f29a
 	ret = calculate_enctypes (enroll, &val;;
54f29a
 	if (ret != ADCLI_SUCCESS) {
54f29a
+		free (uac_str);
54f29a
 		return ret;
54f29a
 	}
54f29a
 	vals_supportedEncryptionTypes[0] = val;
54f29a
@@ -871,6 +885,7 @@ create_computer_account (adcli_enroll *enroll,
54f29a
 	mods[m] = NULL;
54f29a
 
54f29a
 	ret = ldap_add_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
54f29a
+	free (uac_str);
54f29a
 	free (val);
54f29a
 
54f29a
 	/*
54f29a
@@ -1577,6 +1592,14 @@ static char *get_user_account_control (adcli_enroll *enroll)
54f29a
 		}
54f29a
 	}
54f29a
 
54f29a
+	if (enroll->dont_expire_password_explicit) {
54f29a
+		if (adcli_enroll_get_dont_expire_password (enroll)) {
54f29a
+			uac |= UAC_DONT_EXPIRE_PASSWORD;
54f29a
+		} else {
54f29a
+			uac &= ~(UAC_DONT_EXPIRE_PASSWORD);
54f29a
+		}
54f29a
+	}
54f29a
+
54f29a
 	if (enroll->account_disable_explicit) {
54f29a
 		if (adcli_enroll_get_account_disable (enroll)) {
54f29a
 			uac |= UAC_ACCOUNTDISABLE;
54f29a
@@ -1627,6 +1650,7 @@ update_computer_account (adcli_enroll *enroll)
54f29a
 	free (value);
54f29a
 
54f29a
 	if (res == ADCLI_SUCCESS && (enroll->trusted_for_delegation_explicit ||
54f29a
+	                             enroll->dont_expire_password_explicit ||
54f29a
 	                             enroll->account_disable_explicit)) {
54f29a
 		char *vals_userAccountControl[] = { NULL , NULL };
54f29a
 		LDAPMod userAccountControl = { LDAP_MOD_REPLACE, "userAccountControl", { vals_userAccountControl, } };
54f29a
@@ -3208,6 +3232,24 @@ adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
54f29a
 	enroll->trusted_for_delegation_explicit = 1;
54f29a
 }
54f29a
 
54f29a
+bool
54f29a
+adcli_enroll_get_dont_expire_password (adcli_enroll *enroll)
54f29a
+{
54f29a
+	return_val_if_fail (enroll != NULL, false);
54f29a
+
54f29a
+	return enroll->dont_expire_password;
54f29a
+}
54f29a
+
54f29a
+void
54f29a
+adcli_enroll_set_dont_expire_password (adcli_enroll *enroll,
54f29a
+                                       bool value)
54f29a
+{
54f29a
+	return_if_fail (enroll != NULL);
54f29a
+
54f29a
+	enroll->dont_expire_password = value;
54f29a
+	enroll->dont_expire_password_explicit = 1;
54f29a
+}
54f29a
+
54f29a
 bool
54f29a
 adcli_enroll_get_account_disable (adcli_enroll *enroll)
54f29a
 {
54f29a
diff --git a/library/adenroll.h b/library/adenroll.h
54f29a
index 8b1c1c7..34dc683 100644
54f29a
--- a/library/adenroll.h
54f29a
+++ b/library/adenroll.h
54f29a
@@ -126,6 +126,10 @@ bool               adcli_enroll_get_trusted_for_delegation (adcli_enroll *enroll
54f29a
 void               adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
54f29a
                                                             bool value);
54f29a
 
54f29a
+bool               adcli_enroll_get_dont_expire_password (adcli_enroll *enroll);
54f29a
+void               adcli_enroll_set_dont_expire_password (adcli_enroll *enroll,
54f29a
+                                                          bool value);
54f29a
+
54f29a
 bool               adcli_enroll_get_account_disable     (adcli_enroll *enroll);
54f29a
 void               adcli_enroll_set_account_disable     (adcli_enroll *enroll,
54f29a
                                                          bool value);
54f29a
diff --git a/tools/computer.c b/tools/computer.c
54f29a
index 6e309d9..16a1983 100644
54f29a
--- a/tools/computer.c
54f29a
+++ b/tools/computer.c
54f29a
@@ -110,6 +110,7 @@ typedef enum {
54f29a
 	opt_add_samba_data,
54f29a
 	opt_samba_data_tool,
54f29a
 	opt_trusted_for_delegation,
54f29a
+	opt_dont_expire_password,
54f29a
 	opt_add_service_principal,
54f29a
 	opt_remove_service_principal,
54f29a
 	opt_description,
54f29a
@@ -144,6 +145,8 @@ static adcli_tool_desc common_usages[] = {
54f29a
 	{ opt_computer_password_lifetime, "lifetime of the host accounts password in days", },
54f29a
 	{ opt_trusted_for_delegation, "set/unset the TRUSTED_FOR_DELEGATION flag\n"
54f29a
 	                              "in the userAccountControl attribute", },
54f29a
+	{ opt_dont_expire_password, "set/unset the DONT_EXPIRE_PASSWORD flag\n"
54f29a
+	                            "in the userAccountControl attribute", },
54f29a
 	{ opt_account_disable, "set/unset the ACCOUNTDISABLE flag\n"
54f29a
 	                       "in the userAccountControl attribute", },
54f29a
 	{ opt_add_service_principal, "add the given service principal to the account\n" },
54f29a
@@ -307,6 +310,13 @@ parse_option (Option opt,
54f29a
 			adcli_enroll_set_trusted_for_delegation (enroll, false);
54f29a
 		}
54f29a
 		return ADCLI_SUCCESS;
54f29a
+	case opt_dont_expire_password:
54f29a
+		if (strcasecmp (optarg, "true") == 0 || strcasecmp (optarg, "yes") == 0) {
54f29a
+			adcli_enroll_set_dont_expire_password (enroll, true);
54f29a
+		} else {
54f29a
+			adcli_enroll_set_dont_expire_password (enroll, false);
54f29a
+		}
54f29a
+		return ADCLI_SUCCESS;
54f29a
 	case opt_account_disable:
54f29a
 		if (strcasecmp (optarg, "true") == 0 || strcasecmp (optarg, "yes") == 0) {
54f29a
 			adcli_enroll_set_account_disable (enroll, true);
54f29a
@@ -393,6 +403,7 @@ adcli_tool_computer_join (adcli_conn *conn,
54f29a
 		{ "description", optional_argument, NULL, opt_description },
54f29a
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
54f29a
 		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
54f29a
+		{ "dont-expire-password", required_argument, NULL, opt_dont_expire_password },
54f29a
 		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
54f29a
 		{ "show-details", no_argument, NULL, opt_show_details },
54f29a
 		{ "show-password", no_argument, NULL, opt_show_password },
54f29a
@@ -516,6 +527,7 @@ adcli_tool_computer_update (adcli_conn *conn,
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
+		{ "dont-expire-password", required_argument, NULL, opt_dont_expire_password },
54f29a
 		{ "account-disable", required_argument, NULL, opt_account_disable },
54f29a
 		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
54f29a
 		{ "remove-service-principal", required_argument, NULL, opt_remove_service_principal },
54f29a
-- 
54f29a
2.31.1
54f29a