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

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