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