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

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