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

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