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

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