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

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