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

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