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

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