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

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