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

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