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

02b4cc
From 74b52a30c2b142118b7f26f78c014e7bee825e84 Mon Sep 17 00:00:00 2001
02b4cc
From: Sumit Bose <sbose@redhat.com>
02b4cc
Date: Wed, 2 Jun 2021 17:24:07 +0200
02b4cc
Subject: [PATCH 2/2] Add dont-expire-password option
02b4cc
02b4cc
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1769644
02b4cc
---
02b4cc
 doc/adcli.xml      | 28 ++++++++++++++++++++++
02b4cc
 library/adenroll.c | 58 +++++++++++++++++++++++++++++++++++++++++-----
02b4cc
 library/adenroll.h |  4 ++++
02b4cc
 tools/computer.c   | 12 ++++++++++
02b4cc
 4 files changed, 96 insertions(+), 6 deletions(-)
02b4cc
02b4cc
diff --git a/doc/adcli.xml b/doc/adcli.xml
02b4cc
index a64687a..7c2e126 100644
02b4cc
--- a/doc/adcli.xml
02b4cc
+++ b/doc/adcli.xml
02b4cc
@@ -347,6 +347,20 @@ Password for Administrator:
02b4cc
 			not allow that Kerberos tickets can be forwarded to the
02b4cc
 			host.</para></listitem>
02b4cc
 		</varlistentry>
02b4cc
+		<varlistentry>
02b4cc
+			<term><option>--dont-expire-password=<parameter>yes|no|true|false</parameter></option></term>
02b4cc
+			<listitem><para>Set or unset the DONT_EXPIRE_PASSWORD
02b4cc
+			flag in the userAccountControl attribute to indicate if
02b4cc
+			the machine account password should expire or not. By
02b4cc
+			default adcli will set this flag while joining the
02b4cc
+			domain which corresponds to the default behavior of
02b4cc
+			Windows clients.</para>
02b4cc
+			<para>Please note that if the password will expire
02b4cc
+			(--dont-expire-password=false) a renewal mechanism has
02b4cc
+			to be enabled on the client to not loose the
02b4cc
+			connectivity to AD if the password expires.</para>
02b4cc
+			</listitem>
02b4cc
+		</varlistentry>
02b4cc
 		<varlistentry>
02b4cc
 			<term><option>--add-service-principal=<parameter>service/hostname</parameter></option></term>
02b4cc
 			<listitem><para>Add a service principal name. In
02b4cc
@@ -491,6 +505,20 @@ $ adcli update --login-ccache=/tmp/krbcc_123
02b4cc
 			not allow that Kerberos tickets can be forwarded to the
02b4cc
 			host.</para></listitem>
02b4cc
 		</varlistentry>
02b4cc
+		<varlistentry>
02b4cc
+			<term><option>--dont-expire-password=<parameter>yes|no|true|false</parameter></option></term>
02b4cc
+			<listitem><para>Set or unset the DONT_EXPIRE_PASSWORD
02b4cc
+			flag in the userAccountControl attribute to indicate if
02b4cc
+			the machine account password should expire or not. By
02b4cc
+			default adcli will set this flag while joining the
02b4cc
+			domain which corresponds to the default behavior of
02b4cc
+			Windows clients.</para>
02b4cc
+			<para>Please note that if the password will expire
02b4cc
+			(--dont-expire-password=false) a renewal mechanism has
02b4cc
+			to be enabled on the client to not loose the
02b4cc
+			connectivity to AD if the password expires.</para>
02b4cc
+			</listitem>
02b4cc
+		</varlistentry>
02b4cc
 		<varlistentry>
02b4cc
 			<term><option>--add-service-principal=<parameter>service/hostname</parameter></option></term>
02b4cc
 			<listitem><para>Add a service principal name. In
02b4cc
diff --git a/library/adenroll.c b/library/adenroll.c
02b4cc
index c726093..01f149c 100644
02b4cc
--- a/library/adenroll.c
02b4cc
+++ b/library/adenroll.c
02b4cc
@@ -152,6 +152,8 @@ struct _adcli_enroll {
02b4cc
 	char *samba_data_tool;
02b4cc
 	bool trusted_for_delegation;
02b4cc
 	int trusted_for_delegation_explicit;
02b4cc
+	bool dont_expire_password;
02b4cc
+	int dont_expire_password_explicit;
02b4cc
 	char *description;
02b4cc
 };
02b4cc
 
02b4cc
@@ -829,6 +831,8 @@ create_computer_account (adcli_enroll *enroll,
02b4cc
 	int ret;
02b4cc
 	size_t c;
02b4cc
 	size_t m;
02b4cc
+	uint32_t uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD ;
02b4cc
+	char *uac_str = NULL;
02b4cc
 
02b4cc
 	LDAPMod *all_mods[] = {
02b4cc
 		&objectClass,
02b4cc
@@ -849,11 +853,21 @@ create_computer_account (adcli_enroll *enroll,
02b4cc
 	LDAPMod *mods[mods_count];
02b4cc
 
02b4cc
 	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
02b4cc
-		vals_userAccountControl[0] = "593920"; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD | TRUSTED_FOR_DELEGATION */
02b4cc
+		uac |= UAC_TRUSTED_FOR_DELEGATION;
02b4cc
+	}
02b4cc
+
02b4cc
+	if (!adcli_enroll_get_dont_expire_password (enroll)) {
02b4cc
+		uac &= ~(UAC_DONT_EXPIRE_PASSWORD);
02b4cc
+	}
02b4cc
+
02b4cc
+	if (asprintf (&uac_str, "%d", uac) < 0) {
02b4cc
+		return_val_if_reached (ADCLI_ERR_UNEXPECTED);
02b4cc
 	}
02b4cc
+	vals_userAccountControl[0] = uac_str;
02b4cc
 
02b4cc
 	ret = calculate_enctypes (enroll, &val;;
02b4cc
 	if (ret != ADCLI_SUCCESS) {
02b4cc
+		free (uac_str);
02b4cc
 		return ret;
02b4cc
 	}
02b4cc
 	vals_supportedEncryptionTypes[0] = val;
02b4cc
@@ -868,6 +882,7 @@ create_computer_account (adcli_enroll *enroll,
02b4cc
 	mods[m] = NULL;
02b4cc
 
02b4cc
 	ret = ldap_add_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
02b4cc
+	free (uac_str);
02b4cc
 	free (val);
02b4cc
 
02b4cc
 	/*
02b4cc
@@ -1566,10 +1581,20 @@ static char *get_user_account_control (adcli_enroll *enroll)
02b4cc
 		uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD;
02b4cc
 	}
02b4cc
 
02b4cc
-	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
02b4cc
-		uac |= UAC_TRUSTED_FOR_DELEGATION;
02b4cc
-	} else {
02b4cc
-		uac &= ~(UAC_TRUSTED_FOR_DELEGATION);
02b4cc
+	if (enroll->trusted_for_delegation_explicit) {
02b4cc
+		if (adcli_enroll_get_trusted_for_delegation (enroll)) {
02b4cc
+			uac |= UAC_TRUSTED_FOR_DELEGATION;
02b4cc
+		} else {
02b4cc
+			uac &= ~(UAC_TRUSTED_FOR_DELEGATION);
02b4cc
+		}
02b4cc
+	}
02b4cc
+
02b4cc
+	if (enroll->dont_expire_password_explicit) {
02b4cc
+		if (adcli_enroll_get_dont_expire_password (enroll)) {
02b4cc
+			uac |= UAC_DONT_EXPIRE_PASSWORD;
02b4cc
+		} else {
02b4cc
+			uac &= ~(UAC_DONT_EXPIRE_PASSWORD);
02b4cc
+		}
02b4cc
 	}
02b4cc
 
02b4cc
 	if (asprintf (&uac_str, "%d", uac) < 0) {
02b4cc
@@ -1613,7 +1640,8 @@ update_computer_account (adcli_enroll *enroll)
02b4cc
 	}
02b4cc
 	free (value);
02b4cc
 
02b4cc
-	if (res == ADCLI_SUCCESS && enroll->trusted_for_delegation_explicit) {
02b4cc
+	if (res == ADCLI_SUCCESS && (enroll->trusted_for_delegation_explicit ||
02b4cc
+	                             enroll->dont_expire_password_explicit)) {
02b4cc
 		char *vals_userAccountControl[] = { NULL , NULL };
02b4cc
 		LDAPMod userAccountControl = { LDAP_MOD_REPLACE, "userAccountControl", { vals_userAccountControl, } };
02b4cc
 		LDAPMod *mods[] = { &userAccountControl, NULL };
02b4cc
@@ -3194,6 +3222,24 @@ adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
02b4cc
 	enroll->trusted_for_delegation_explicit = 1;
02b4cc
 }
02b4cc
 
02b4cc
+bool
02b4cc
+adcli_enroll_get_dont_expire_password (adcli_enroll *enroll)
02b4cc
+{
02b4cc
+	return_val_if_fail (enroll != NULL, false);
02b4cc
+
02b4cc
+	return enroll->dont_expire_password;
02b4cc
+}
02b4cc
+
02b4cc
+void
02b4cc
+adcli_enroll_set_dont_expire_password (adcli_enroll *enroll,
02b4cc
+                                       bool value)
02b4cc
+{
02b4cc
+	return_if_fail (enroll != NULL);
02b4cc
+
02b4cc
+	enroll->dont_expire_password = value;
02b4cc
+	enroll->dont_expire_password_explicit = 1;
02b4cc
+}
02b4cc
+
02b4cc
 void
02b4cc
 adcli_enroll_set_description (adcli_enroll *enroll, const char *value)
02b4cc
 {
02b4cc
diff --git a/library/adenroll.h b/library/adenroll.h
02b4cc
index 11a30c8..5190eb6 100644
02b4cc
--- a/library/adenroll.h
02b4cc
+++ b/library/adenroll.h
02b4cc
@@ -126,6 +126,10 @@ bool               adcli_enroll_get_trusted_for_delegation (adcli_enroll *enroll
02b4cc
 void               adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
02b4cc
                                                             bool value);
02b4cc
 
02b4cc
+bool               adcli_enroll_get_dont_expire_password (adcli_enroll *enroll);
02b4cc
+void               adcli_enroll_set_dont_expire_password (adcli_enroll *enroll,
02b4cc
+                                                          bool value);
02b4cc
+
02b4cc
 const char *       adcli_enroll_get_desciption          (adcli_enroll *enroll);
02b4cc
 void               adcli_enroll_set_description         (adcli_enroll *enroll,
02b4cc
                                                          const char *value);
02b4cc
diff --git a/tools/computer.c b/tools/computer.c
02b4cc
index 98a0472..954066a 100644
02b4cc
--- a/tools/computer.c
02b4cc
+++ b/tools/computer.c
02b4cc
@@ -110,6 +110,7 @@ typedef enum {
02b4cc
 	opt_add_samba_data,
02b4cc
 	opt_samba_data_tool,
02b4cc
 	opt_trusted_for_delegation,
02b4cc
+	opt_dont_expire_password,
02b4cc
 	opt_add_service_principal,
02b4cc
 	opt_remove_service_principal,
02b4cc
 	opt_description,
02b4cc
@@ -143,6 +144,8 @@ static adcli_tool_desc common_usages[] = {
02b4cc
 	{ opt_computer_password_lifetime, "lifetime of the host accounts password in days", },
02b4cc
 	{ opt_trusted_for_delegation, "set/unset the TRUSTED_FOR_DELEGATION flag\n"
02b4cc
 	                              "in the userAccountControl attribute", },
02b4cc
+	{ opt_dont_expire_password, "set/unset the DONT_EXPIRE_PASSWORD flag\n"
02b4cc
+	                            "in the userAccountControl attribute", },
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
@@ -304,6 +307,13 @@ parse_option (Option opt,
02b4cc
 			adcli_enroll_set_trusted_for_delegation (enroll, false);
02b4cc
 		}
02b4cc
 		return ADCLI_SUCCESS;
02b4cc
+	case opt_dont_expire_password:
02b4cc
+		if (strcasecmp (optarg, "true") == 0 || strcasecmp (optarg, "yes") == 0) {
02b4cc
+			adcli_enroll_set_dont_expire_password (enroll, true);
02b4cc
+		} else {
02b4cc
+			adcli_enroll_set_dont_expire_password (enroll, false);
02b4cc
+		}
02b4cc
+		return ADCLI_SUCCESS;
02b4cc
 	case opt_add_service_principal:
02b4cc
 		adcli_enroll_add_service_principal_to_add (enroll, optarg);
02b4cc
 		return ADCLI_SUCCESS;
02b4cc
@@ -383,6 +393,7 @@ adcli_tool_computer_join (adcli_conn *conn,
02b4cc
 		{ "description", optional_argument, NULL, opt_description },
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
 		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
02b4cc
 		{ "show-details", no_argument, NULL, opt_show_details },
02b4cc
 		{ "show-password", no_argument, NULL, opt_show_password },
02b4cc
@@ -504,6 +515,7 @@ adcli_tool_computer_update (adcli_conn *conn,
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
+		{ "dont-expire-password", required_argument, NULL, opt_dont_expire_password },
02b4cc
 		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
02b4cc
 		{ "remove-service-principal", required_argument, NULL, opt_remove_service_principal },
02b4cc
 		{ "show-details", no_argument, NULL, opt_show_details },
02b4cc
-- 
02b4cc
2.31.1
02b4cc