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

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