Blame 0016-Add-trusted-for-delegation-option.patch

461678c
From f306f2f20c1d35fac63d27147824f039f7ef2d67 Mon Sep 17 00:00:00 2001
461678c
From: Sumit Bose <sbose@redhat.com>
461678c
Date: Thu, 31 May 2018 18:27:37 +0200
461678c
Subject: [PATCH 16/23] Add trusted-for-delegation option
461678c
461678c
Resolves https://bugzilla.redhat.com/show_bug.cgi?id=1538730
461678c
---
461678c
 doc/adcli.xml      | 14 ++++++++++
461678c
 library/adenroll.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
461678c
 library/adenroll.h |  4 +++
461678c
 tools/computer.c   | 12 ++++++++
461678c
 4 files changed, 108 insertions(+), 2 deletions(-)
461678c
461678c
diff --git a/doc/adcli.xml b/doc/adcli.xml
461678c
index c2b7760..b246190 100644
461678c
--- a/doc/adcli.xml
461678c
+++ b/doc/adcli.xml
461678c
@@ -283,6 +283,13 @@ Password for Administrator:
461678c
 			<option>--login-type=computer</option> and providing a
461678c
 			password as input.</para></listitem>
461678c
 		</varlistentry>
461678c
+		<varlistentry>
461678c
+			<term><option>--trusted-for-delegation=<parameter>yes|no|true|false</parameter></option></term>
461678c
+			<listitem><para>Set or unset the TRUSTED_FOR_DELEGATION
461678c
+			flag in the userAccountControl attribute to allow or
461678c
+			not allow that Kerberos tickets can be forwarded to the
461678c
+			host.</para></listitem>
461678c
+		</varlistentry>
461678c
 		<varlistentry>
461678c
 			<term><option>--show-details</option></term>
461678c
 			<listitem><para>After a successful join print out information
461678c
@@ -402,6 +409,13 @@ $ adcli update --login-ccache=/tmp/krbcc_123
461678c
 			in days. By default the password is updated if it is
461678c
 			older than 30 days.</para></listitem>
461678c
 		</varlistentry>
461678c
+		<varlistentry>
461678c
+			<term><option>--trusted-for-delegation=<parameter>yes|no|true|false</parameter></option></term>
461678c
+			<listitem><para>Set or unset the TRUSTED_FOR_DELEGATION
461678c
+			flag in the userAccountControl attribute to allow or
461678c
+			not allow that Kerberos tickets can be forwarded to the
461678c
+			host.</para></listitem>
461678c
+		</varlistentry>
461678c
 		<varlistentry>
461678c
 			<term><option>--show-details</option></term>
461678c
 			<listitem><para>After a successful join print out information
461678c
diff --git a/library/adenroll.c b/library/adenroll.c
461678c
index a693049..eca3c37 100644
461678c
--- a/library/adenroll.c
461678c
+++ b/library/adenroll.c
461678c
@@ -63,6 +63,13 @@ static krb5_enctype v51_earlier_enctypes[] = {
461678c
 	0
461678c
 };
461678c
 
461678c
+/* Some constants for the userAccountControl AD LDAP attribute, see e.g.
461678c
+ * https://support.microsoft.com/en-us/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
461678c
+ * for details. */
461678c
+#define UAC_WORKSTATION_TRUST_ACCOUNT  0x1000
461678c
+#define UAC_DONT_EXPIRE_PASSWORD      0x10000
461678c
+#define UAC_TRUSTED_FOR_DELEGATION    0x80000
461678c
+
461678c
 struct _adcli_enroll {
461678c
 	int refs;
461678c
 	adcli_conn *conn;
461678c
@@ -105,6 +112,7 @@ struct _adcli_enroll {
461678c
 	unsigned int computer_password_lifetime;
461678c
 	int computer_password_lifetime_explicit;
461678c
 	char *samba_data_tool;
461678c
+	bool trusted_for_delegation;
461678c
 };
461678c
 
461678c
 static adcli_result
461678c
@@ -538,6 +546,10 @@ create_computer_account (adcli_enroll *enroll,
461678c
 		NULL,
461678c
 	};
461678c
 
461678c
+	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
461678c
+		vals_userAccountControl[0] = "593920"; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD | TRUSTED_FOR_DELEGATION */
461678c
+	}
461678c
+
461678c
 	ret = ldap_add_ext_s (ldap, enroll->computer_dn, mods, NULL, NULL);
461678c
 
461678c
 	/*
461678c
@@ -971,6 +983,7 @@ retrieve_computer_account (adcli_enroll *enroll)
461678c
 		"operatingSystemVersion",
461678c
 		"operatingSystemServicePack",
461678c
 		"pwdLastSet",
461678c
+		"userAccountControl",
461678c
 		NULL,
461678c
 	};
461678c
 
461678c
@@ -1149,6 +1162,47 @@ update_computer_attribute (adcli_enroll *enroll,
461678c
 	return res;
461678c
 }
461678c
 
461678c
+static char *get_user_account_control (adcli_enroll *enroll)
461678c
+{
461678c
+	uint32_t uac = 0;
461678c
+	unsigned long attr_val;
461678c
+	char *uac_str;
461678c
+	LDAP *ldap;
461678c
+	char *end;
461678c
+
461678c
+	ldap = adcli_conn_get_ldap_connection (enroll->conn);
461678c
+	return_val_if_fail (ldap != NULL, NULL);
461678c
+
461678c
+	uac_str = _adcli_ldap_parse_value (ldap, enroll->computer_attributes, "userAccountControl");
461678c
+	if (uac_str != NULL) {
461678c
+
461678c
+		attr_val = strtoul (uac_str, &end, 10);
461678c
+		if (*end != '\0' || attr_val > UINT32_MAX) {
461678c
+			_adcli_warn ("Invalid userAccountControl '%s' for computer account in directory: %s, assuming 0",
461678c
+			            uac_str, enroll->computer_dn);
461678c
+		} else {
461678c
+			uac = attr_val;
461678c
+		}
461678c
+		free (uac_str);
461678c
+	}
461678c
+
461678c
+	if (uac == 0) {
461678c
+		uac = UAC_WORKSTATION_TRUST_ACCOUNT | UAC_DONT_EXPIRE_PASSWORD;
461678c
+	}
461678c
+
461678c
+	if (adcli_enroll_get_trusted_for_delegation (enroll)) {
461678c
+		uac |= UAC_TRUSTED_FOR_DELEGATION;
461678c
+	} else {
461678c
+		uac &= ~(UAC_TRUSTED_FOR_DELEGATION);
461678c
+	}
461678c
+
461678c
+	if (asprintf (&uac_str, "%d", uac) < 0) {
461678c
+		return_val_if_reached (NULL);
461678c
+	}
461678c
+
461678c
+	return uac_str;
461678c
+}
461678c
+
461678c
 static void
461678c
 update_computer_account (adcli_enroll *enroll)
461678c
 {
461678c
@@ -1167,11 +1221,16 @@ update_computer_account (adcli_enroll *enroll)
461678c
 	}
461678c
 
461678c
 	if (res == ADCLI_SUCCESS) {
461678c
-		char *vals_userAccountControl[] = { "69632", NULL }; /* WORKSTATION_TRUST_ACCOUNT | DONT_EXPIRE_PASSWD */
461678c
+		char *vals_userAccountControl[] = { NULL , NULL };
461678c
 		LDAPMod userAccountControl = { LDAP_MOD_REPLACE, "userAccountControl", { vals_userAccountControl, } };
461678c
 		LDAPMod *mods[] = { &userAccountControl, NULL };
461678c
 
461678c
-		res |= update_computer_attribute (enroll, ldap, mods);
461678c
+		vals_userAccountControl[0] = get_user_account_control (enroll);
461678c
+		if (vals_userAccountControl[0] != NULL) {
461678c
+			res |= update_computer_attribute (enroll, ldap, mods);
461678c
+		} else {
461678c
+			_adcli_warn ("Cannot update userAccountControl");
461678c
+		}
461678c
 	}
461678c
 
461678c
 	if (res == ADCLI_SUCCESS) {
461678c
@@ -2375,3 +2434,20 @@ adcli_enroll_get_samba_data_tool (adcli_enroll *enroll)
461678c
 	return_val_if_fail (enroll != NULL, NULL);
461678c
 	return enroll->samba_data_tool;
461678c
 }
461678c
+
461678c
+bool
461678c
+adcli_enroll_get_trusted_for_delegation (adcli_enroll *enroll)
461678c
+{
461678c
+	return_val_if_fail (enroll != NULL, false);
461678c
+
461678c
+	return enroll->trusted_for_delegation;
461678c
+}
461678c
+
461678c
+void
461678c
+adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
461678c
+                                         bool value)
461678c
+{
461678c
+	return_if_fail (enroll != NULL);
461678c
+
461678c
+	enroll->trusted_for_delegation = value;
461678c
+}
461678c
diff --git a/library/adenroll.h b/library/adenroll.h
461678c
index 31ca0bc..be2ca18 100644
461678c
--- a/library/adenroll.h
461678c
+++ b/library/adenroll.h
461678c
@@ -109,6 +109,10 @@ unsigned int       adcli_enroll_get_computer_password_lifetime (adcli_enroll *en
461678c
 void               adcli_enroll_set_computer_password_lifetime (adcli_enroll *enroll,
461678c
                                                          unsigned int lifetime);
461678c
 
461678c
+bool               adcli_enroll_get_trusted_for_delegation (adcli_enroll *enroll);
461678c
+void               adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
461678c
+                                                            bool value);
461678c
+
461678c
 krb5_kvno          adcli_enroll_get_kvno                (adcli_enroll *enroll);
461678c
 
461678c
 void               adcli_enroll_set_kvno                (adcli_enroll *enroll,
461678c
diff --git a/tools/computer.c b/tools/computer.c
461678c
index f86548b..b905fd1 100644
461678c
--- a/tools/computer.c
461678c
+++ b/tools/computer.c
461678c
@@ -109,6 +109,7 @@ typedef enum {
461678c
 	opt_computer_password_lifetime,
461678c
 	opt_add_samba_data,
461678c
 	opt_samba_data_tool,
461678c
+	opt_trusted_for_delegation,
461678c
 } Option;
461678c
 
461678c
 static adcli_tool_desc common_usages[] = {
461678c
@@ -135,6 +136,8 @@ static adcli_tool_desc common_usages[] = {
461678c
 	{ opt_os_service_pack, "the computer operating system service pack", },
461678c
 	{ opt_user_principal, "add an authentication principal to the account", },
461678c
 	{ opt_computer_password_lifetime, "lifetime of the host accounts password in days", },
461678c
+	{ opt_trusted_for_delegation, "set/unset the TRUSTED_FOR_DELEGATION flag\n"
461678c
+	                              "in the userAccountControl attribute", },
461678c
 	{ opt_no_password, "don't prompt for or read a password" },
461678c
 	{ opt_prompt_password, "prompt for a password if necessary" },
461678c
 	{ opt_stdin_password, "read a password from stdin (until EOF) if\n"
461678c
@@ -279,6 +282,13 @@ parse_option (Option opt,
461678c
 			adcli_enroll_set_samba_data_tool (enroll, optarg);
461678c
 		}
461678c
 		return;
461678c
+	case opt_trusted_for_delegation:
461678c
+		if (strcasecmp (optarg, "true") == 0 || strcasecmp (optarg, "yes") == 0) {
461678c
+			adcli_enroll_set_trusted_for_delegation (enroll, true);
461678c
+		} else {
461678c
+			adcli_enroll_set_trusted_for_delegation (enroll, false);
461678c
+		}
461678c
+		return;
461678c
 	case opt_verbose:
461678c
 		return;
461678c
 
461678c
@@ -342,6 +352,7 @@ adcli_tool_computer_join (adcli_conn *conn,
461678c
 		{ "os-version", required_argument, NULL, opt_os_version },
461678c
 		{ "os-service-pack", optional_argument, NULL, opt_os_service_pack },
461678c
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
461678c
+		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
461678c
 		{ "show-details", no_argument, NULL, opt_show_details },
461678c
 		{ "show-password", no_argument, NULL, opt_show_password },
461678c
 		{ "add-samba-data", no_argument, NULL, opt_add_samba_data },
461678c
@@ -446,6 +457,7 @@ adcli_tool_computer_update (adcli_conn *conn,
461678c
 		{ "os-service-pack", optional_argument, NULL, opt_os_service_pack },
461678c
 		{ "user-principal", optional_argument, NULL, opt_user_principal },
461678c
 		{ "computer-password-lifetime", optional_argument, NULL, opt_computer_password_lifetime },
461678c
+		{ "trusted-for-delegation", required_argument, NULL, opt_trusted_for_delegation },
461678c
 		{ "show-details", no_argument, NULL, opt_show_details },
461678c
 		{ "show-password", no_argument, NULL, opt_show_password },
461678c
 		{ "add-samba-data", no_argument, NULL, opt_add_samba_data },
461678c
-- 
461678c
2.14.4
461678c