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

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