Blame SOURCES/0001-Add-trusted-for-delegation-option.patch

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