Blame SOURCES/0022-Add-add-service-principal-and-remove-service-princip.patch

f441eb
From ee71c4c0614a504b4472bf64a24fc3c18c6b9987 Mon Sep 17 00:00:00 2001
f441eb
From: Sumit Bose <sbose@redhat.com>
f441eb
Date: Thu, 14 Jun 2018 16:49:26 +0200
f441eb
Subject: [PATCH 22/23] Add add-service-principal and remove-service-principal
f441eb
 options
f441eb
f441eb
Currently it is only possible to specific a service name for service
f441eb
principals but not to set the full service principal. This is e.g.
f441eb
needed if there is a service running on a host which should be reachable
f441eb
by a different DNS name as well.
f441eb
f441eb
With this patch service principal can be added and removed by specifying
f441eb
the full name.
f441eb
f441eb
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1547014
f441eb
---
f441eb
 doc/adcli.xml      |  21 ++++++++
f441eb
 library/adenroll.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++--
f441eb
 library/adenroll.h |   8 +++
f441eb
 library/adldap.c   |  16 ++++--
f441eb
 tools/computer.c   |  13 +++++
f441eb
 5 files changed, 189 insertions(+), 8 deletions(-)
f441eb
f441eb
diff --git a/doc/adcli.xml b/doc/adcli.xml
f441eb
index b246190..83b6981 100644
f441eb
--- a/doc/adcli.xml
f441eb
+++ b/doc/adcli.xml
f441eb
@@ -290,6 +290,14 @@ Password for Administrator:
f441eb
 			not allow that Kerberos tickets can be forwarded to the
f441eb
 			host.</para></listitem>
f441eb
 		</varlistentry>
f441eb
+		<varlistentry>
f441eb
+			<term><option>--add-service-principal=<parameter>service/hostname</parameter></option></term>
f441eb
+			<listitem><para>Add a service principal name. In
f441eb
+			contrast to the <option>--service-name</option> the
f441eb
+			hostname part can be specified as well in case the
f441eb
+			service should be accessible with a different host
f441eb
+			name as well.</para></listitem>
f441eb
+		</varlistentry>
f441eb
 		<varlistentry>
f441eb
 			<term><option>--show-details</option></term>
f441eb
 			<listitem><para>After a successful join print out information
f441eb
@@ -416,6 +424,19 @@ $ adcli update --login-ccache=/tmp/krbcc_123
f441eb
 			not allow that Kerberos tickets can be forwarded to the
f441eb
 			host.</para></listitem>
f441eb
 		</varlistentry>
f441eb
+		<varlistentry>
f441eb
+			<term><option>--add-service-principal=<parameter>service/hostname</parameter></option></term>
f441eb
+			<listitem><para>Add a service principal name. In
f441eb
+			contrast to the <option>--service-name</option> the
f441eb
+			hostname part can be specified as well in case the
f441eb
+			service should be accessible with a different host
f441eb
+			name as well.</para></listitem>
f441eb
+		</varlistentry>
f441eb
+		<varlistentry>
f441eb
+			<term><option>--remove-service-principal=<parameter>service/hostname</parameter></option></term>
f441eb
+			<listitem><para>Remove a service principal name from
f441eb
+			the keytab and the AD host object.</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 b508caf..c4ba537 100644
f441eb
--- a/library/adenroll.c
f441eb
+++ b/library/adenroll.c
f441eb
@@ -95,6 +95,9 @@ struct _adcli_enroll {
f441eb
 	char **service_principals;
f441eb
 	int service_principals_explicit;
f441eb
 
f441eb
+	char **service_principals_to_add;
f441eb
+	char **service_principals_to_remove;
f441eb
+
f441eb
 	char *user_principal;
f441eb
 	int user_princpal_generate;
f441eb
 
f441eb
@@ -332,6 +335,43 @@ add_service_names_to_service_principals (adcli_enroll *enroll)
f441eb
 	return ADCLI_SUCCESS;
f441eb
 }
f441eb
 
f441eb
+static adcli_result
f441eb
+add_and_remove_service_principals (adcli_enroll *enroll)
f441eb
+{
f441eb
+	int length = 0;
f441eb
+	size_t c;
f441eb
+	const char **list;
f441eb
+
f441eb
+	if (enroll->service_principals != NULL) {
f441eb
+		length = seq_count (enroll->service_principals);
f441eb
+	}
f441eb
+
f441eb
+	list = adcli_enroll_get_service_principals_to_add (enroll);
f441eb
+	if (list != NULL) {
f441eb
+		for (c = 0; list[c] != NULL; c++) {
f441eb
+			enroll->service_principals = _adcli_strv_add (enroll->service_principals,
f441eb
+			                                              strdup (list[c]),
f441eb
+			                                              &length);
f441eb
+			if (enroll->service_principals == NULL) {
f441eb
+				return ADCLI_ERR_UNEXPECTED;
f441eb
+			}
f441eb
+		}
f441eb
+	}
f441eb
+
f441eb
+	list = adcli_enroll_get_service_principals_to_remove (enroll);
f441eb
+	if (list != NULL) {
f441eb
+		for (c = 0; list[c] != NULL; c++) {
f441eb
+			/* enroll->service_principals typically refects the
f441eb
+			 * order of the principal in the keytabm so it is not
f441eb
+			 * ordered. */
f441eb
+			_adcli_strv_remove_unsorted (enroll->service_principals,
f441eb
+			                             list[c], &length);
f441eb
+		}
f441eb
+	}
f441eb
+
f441eb
+	return ADCLI_SUCCESS;
f441eb
+}
f441eb
+
f441eb
 static adcli_result
f441eb
 ensure_service_principals (adcli_result res,
f441eb
                            adcli_enroll *enroll)
f441eb
@@ -343,10 +383,14 @@ ensure_service_principals (adcli_result res,
f441eb
 
f441eb
 	if (!enroll->service_principals) {
f441eb
 		assert (enroll->service_names != NULL);
f441eb
-		return add_service_names_to_service_principals (enroll);
f441eb
+		res = add_service_names_to_service_principals (enroll);
f441eb
 	}
f441eb
 
f441eb
-	return ADCLI_SUCCESS;
f441eb
+	if (res == ADCLI_SUCCESS) {
f441eb
+		res = add_and_remove_service_principals (enroll);
f441eb
+	}
f441eb
+
f441eb
+	return res;
f441eb
 }
f441eb
 
f441eb
 static adcli_result
f441eb
@@ -1593,6 +1637,39 @@ free_principal_salts (krb5_context k5,
f441eb
 	free (salts);
f441eb
 }
f441eb
 
f441eb
+static adcli_result
f441eb
+remove_principal_from_keytab (adcli_enroll *enroll,
f441eb
+                              krb5_context k5,
f441eb
+                              const char *principal_name)
f441eb
+{
f441eb
+	krb5_error_code code;
f441eb
+	krb5_principal principal;
f441eb
+	match_principal_kvno closure;
f441eb
+
f441eb
+	code = krb5_parse_name (k5, principal_name, &principal);
f441eb
+	if (code != 0) {
f441eb
+		_adcli_err ("Couldn't parse principal: %s: %s",
f441eb
+		            principal_name, krb5_get_error_message (k5, code));
f441eb
+		return ADCLI_ERR_FAIL;
f441eb
+	}
f441eb
+
f441eb
+	closure.kvno = enroll->kvno;
f441eb
+	closure.principal = principal;
f441eb
+	closure.matched = 0;
f441eb
+
f441eb
+	code = _adcli_krb5_keytab_clear (k5, enroll->keytab,
f441eb
+	                                 match_principal_and_kvno, &closure);
f441eb
+	krb5_free_principal (k5, principal);
f441eb
+
f441eb
+	if (code != 0) {
f441eb
+		_adcli_err ("Couldn't update keytab: %s: %s",
f441eb
+		            enroll->keytab_name, krb5_get_error_message (k5, code));
f441eb
+		return ADCLI_ERR_FAIL;
f441eb
+	}
f441eb
+
f441eb
+	return ADCLI_SUCCESS;
f441eb
+}
f441eb
+
f441eb
 static adcli_result
f441eb
 add_principal_to_keytab (adcli_enroll *enroll,
f441eb
                          krb5_context k5,
f441eb
@@ -1702,6 +1779,17 @@ update_keytab_for_principals (adcli_enroll *enroll,
f441eb
 			return res;
f441eb
 	}
f441eb
 
f441eb
+	if (enroll->service_principals_to_remove != NULL) {
f441eb
+		for (i = 0; enroll->service_principals_to_remove[i] != NULL; i++) {
f441eb
+			res = remove_principal_from_keytab (enroll, k5,
f441eb
+			                                    enroll->service_principals_to_remove[i]);
f441eb
+			if (res != ADCLI_SUCCESS) {
f441eb
+				_adcli_warn ("Failed to remove %s from keytab.",
f441eb
+				             enroll->service_principals_to_remove[i]);
f441eb
+			}
f441eb
+		}
f441eb
+	}
f441eb
+
f441eb
 	return ADCLI_SUCCESS;
f441eb
 }
f441eb
 
f441eb
@@ -2029,8 +2117,11 @@ adcli_enroll_update (adcli_enroll *enroll,
f441eb
 	if (_adcli_check_nt_time_string_lifetime (value,
f441eb
 	                adcli_enroll_get_computer_password_lifetime (enroll))) {
f441eb
 		/* Do not update keytab if neither new service principals have
f441eb
-                 * to be added nor the user principal has to be changed. */
f441eb
-		if (enroll->service_names == NULL && (enroll->user_principal == NULL || enroll->user_princpal_generate)) {
f441eb
+                 * to be added or deleted nor the user principal has to be changed. */
f441eb
+		if (enroll->service_names == NULL
f441eb
+		              && (enroll->user_principal == NULL || enroll->user_princpal_generate)
f441eb
+		              && enroll->service_principals_to_add == NULL
f441eb
+		              && enroll->service_principals_to_remove == NULL) {
f441eb
 			flags |= ADCLI_ENROLL_NO_KEYTAB;
f441eb
 		}
f441eb
 		flags |= ADCLI_ENROLL_PASSWORD_VALID;
f441eb
@@ -2581,3 +2672,43 @@ adcli_enroll_set_trusted_for_delegation (adcli_enroll *enroll,
f441eb
 	enroll->trusted_for_delegation = value;
f441eb
 	enroll->trusted_for_delegation_explicit = 1;
f441eb
 }
f441eb
+
f441eb
+const char **
f441eb
+adcli_enroll_get_service_principals_to_add (adcli_enroll *enroll)
f441eb
+{
f441eb
+	return_val_if_fail (enroll != NULL, NULL);
f441eb
+
f441eb
+	return (const char **)enroll->service_principals_to_add;
f441eb
+}
f441eb
+
f441eb
+void
f441eb
+adcli_enroll_add_service_principal_to_add (adcli_enroll *enroll,
f441eb
+                                           const char *value)
f441eb
+{
f441eb
+	return_if_fail (enroll != NULL);
f441eb
+	return_if_fail (value != NULL);
f441eb
+
f441eb
+	enroll->service_principals_to_add = _adcli_strv_add (enroll->service_principals_to_add,
f441eb
+							    strdup (value), NULL);
f441eb
+	return_if_fail (enroll->service_principals_to_add != NULL);
f441eb
+}
f441eb
+
f441eb
+const char **
f441eb
+adcli_enroll_get_service_principals_to_remove (adcli_enroll *enroll)
f441eb
+{
f441eb
+	return_val_if_fail (enroll != NULL, NULL);
f441eb
+
f441eb
+	return (const char **)enroll->service_principals_to_remove;
f441eb
+}
f441eb
+
f441eb
+void
f441eb
+adcli_enroll_add_service_principal_to_remove (adcli_enroll *enroll,
f441eb
+                                              const char *value)
f441eb
+{
f441eb
+	return_if_fail (enroll != NULL);
f441eb
+	return_if_fail (value != NULL);
f441eb
+
f441eb
+	enroll->service_principals_to_remove = _adcli_strv_add (enroll->service_principals_to_remove,
f441eb
+							    strdup (value), NULL);
f441eb
+	return_if_fail (enroll->service_principals_to_remove != NULL);
f441eb
+}
f441eb
diff --git a/library/adenroll.h b/library/adenroll.h
f441eb
index be2ca18..f87dffa 100644
f441eb
--- a/library/adenroll.h
f441eb
+++ b/library/adenroll.h
f441eb
@@ -98,6 +98,14 @@ const char **      adcli_enroll_get_service_principals  (adcli_enroll *enroll);
f441eb
 void               adcli_enroll_set_service_principals  (adcli_enroll *enroll,
f441eb
                                                          const char **value);
f441eb
 
f441eb
+const char **      adcli_enroll_get_service_principals_to_add (adcli_enroll *enroll);
f441eb
+void               adcli_enroll_add_service_principal_to_add (adcli_enroll *enroll,
f441eb
+                                                              const char *value);
f441eb
+
f441eb
+const char **      adcli_enroll_get_service_principals_to_remove (adcli_enroll *enroll);
f441eb
+void               adcli_enroll_add_service_principal_to_remove (adcli_enroll *enroll,
f441eb
+                                                                 const char *value);
f441eb
+
f441eb
 const char *       adcli_enroll_get_user_principal      (adcli_enroll *enroll);
f441eb
 
f441eb
 void               adcli_enroll_set_user_principal      (adcli_enroll *enroll,
f441eb
diff --git a/library/adldap.c b/library/adldap.c
f441eb
index 07dc373..d93efb7 100644
f441eb
--- a/library/adldap.c
f441eb
+++ b/library/adldap.c
f441eb
@@ -210,16 +210,24 @@ _adcli_ldap_have_in_mod (LDAPMod *mod,
f441eb
 	struct berval *vals;
f441eb
 	struct berval **pvals;
f441eb
 	int count = 0;
f441eb
+	int count_have = 0;
f441eb
 	int i;
f441eb
 	int ret;
f441eb
 
f441eb
-	/* Already in berval format, just compare */
f441eb
-	if (mod->mod_op & LDAP_MOD_BVALUES)
f441eb
-		return _adcli_ldap_have_vals (mod->mod_vals.modv_bvals, have);
f441eb
-
f441eb
 	/* Count number of values */
f441eb
 	for (i = 0; mod->mod_vals.modv_strvals[i] != 0; i++)
f441eb
 		count++;
f441eb
+	for (i = 0; have[i] != 0; i++)
f441eb
+		count_have++;
f441eb
+
f441eb
+	/* If numbers different something has to be added or removed */
f441eb
+	if (count != count_have) {
f441eb
+		return 0;
f441eb
+	}
f441eb
+
f441eb
+	/* Already in berval format, just compare */
f441eb
+	if (mod->mod_op & LDAP_MOD_BVALUES)
f441eb
+		return _adcli_ldap_have_vals (mod->mod_vals.modv_bvals, have);
f441eb
 
f441eb
 	vals = malloc (sizeof (struct berval) * (count + 1));
f441eb
 	pvals = malloc (sizeof (struct berval *) * (count + 1));
f441eb
diff --git a/tools/computer.c b/tools/computer.c
f441eb
index b905fd1..377d449 100644
f441eb
--- a/tools/computer.c
f441eb
+++ b/tools/computer.c
f441eb
@@ -110,6 +110,8 @@ typedef enum {
f441eb
 	opt_add_samba_data,
f441eb
 	opt_samba_data_tool,
f441eb
 	opt_trusted_for_delegation,
f441eb
+	opt_add_service_principal,
f441eb
+	opt_remove_service_principal,
f441eb
 } Option;
f441eb
 
f441eb
 static adcli_tool_desc common_usages[] = {
f441eb
@@ -138,6 +140,8 @@ static adcli_tool_desc common_usages[] = {
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_add_service_principal, "add the given service principal to the account\n" },
f441eb
+	{ opt_remove_service_principal, "remove the given service principal from the account\n" },
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
@@ -289,6 +293,12 @@ parse_option (Option opt,
f441eb
 			adcli_enroll_set_trusted_for_delegation (enroll, false);
f441eb
 		}
f441eb
 		return;
f441eb
+	case opt_add_service_principal:
f441eb
+		adcli_enroll_add_service_principal_to_add (enroll, optarg);
f441eb
+		return;
f441eb
+	case opt_remove_service_principal:
f441eb
+		adcli_enroll_add_service_principal_to_remove (enroll, optarg);
f441eb
+		return;
f441eb
 	case opt_verbose:
f441eb
 		return;
f441eb
 
f441eb
@@ -353,6 +363,7 @@ adcli_tool_computer_join (adcli_conn *conn,
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
+		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
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
@@ -458,6 +469,8 @@ adcli_tool_computer_update (adcli_conn *conn,
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
+		{ "add-service-principal", required_argument, NULL, opt_add_service_principal },
f441eb
+		{ "remove-service-principal", required_argument, NULL, opt_remove_service_principal },
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