Blame SOURCES/0003-update-allow-to-add-service-names.patch

776d17
From c53a51a61d7ac20900836b1bb005bf272c08a849 Mon Sep 17 00:00:00 2001
776d17
From: Sumit Bose <sbose@redhat.com>
776d17
Date: Mon, 4 Jun 2018 10:49:33 +0200
776d17
Subject: [PATCH 3/7] update: allow to add service names
776d17
776d17
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1547013
776d17
           https://bugzilla.redhat.com/show_bug.cgi?id=1545568
776d17
---
776d17
 library/adenroll.c  | 136 +++++++++++++++++++++++++++++++++-------------------
776d17
 library/adkrb5.c    | 113 +++++++++++++++++++++++++++++++++++++++++++
776d17
 library/adprivate.h |   6 +++
776d17
 3 files changed, 206 insertions(+), 49 deletions(-)
776d17
776d17
diff --git a/library/adenroll.c b/library/adenroll.c
776d17
index 2be6796..1221e89 100644
776d17
--- a/library/adenroll.c
776d17
+++ b/library/adenroll.c
776d17
@@ -305,13 +305,37 @@ ensure_service_names (adcli_result res,
776d17
 }
776d17
 
776d17
 static adcli_result
776d17
-ensure_service_principals (adcli_result res,
776d17
-                           adcli_enroll *enroll)
776d17
+add_service_names_to_service_principals (adcli_enroll *enroll)
776d17
 {
776d17
 	char *name;
776d17
 	int length = 0;
776d17
 	int i;
776d17
 
776d17
+	if (enroll->service_principals != NULL) {
776d17
+		length = seq_count (enroll->service_principals);
776d17
+	}
776d17
+
776d17
+	for (i = 0; enroll->service_names[i] != NULL; i++) {
776d17
+		if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->computer_name) < 0)
776d17
+			return_unexpected_if_reached ();
776d17
+		enroll->service_principals = _adcli_strv_add (enroll->service_principals,
776d17
+			                                      name, &length);
776d17
+
776d17
+		if (enroll->host_fqdn) {
776d17
+			if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->host_fqdn) < 0)
776d17
+				return_unexpected_if_reached ();
776d17
+			enroll->service_principals = _adcli_strv_add (enroll->service_principals,
776d17
+				                                      name, &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
+{
776d17
 	if (res != ADCLI_SUCCESS)
776d17
 		return res;
776d17
 
776d17
@@ -319,20 +343,7 @@ ensure_service_principals (adcli_result res,
776d17
 
776d17
 	if (!enroll->service_principals) {
776d17
 		assert (enroll->service_names != NULL);
776d17
-
776d17
-		for (i = 0; enroll->service_names[i] != NULL; i++) {
776d17
-			if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->computer_name) < 0)
776d17
-				return_unexpected_if_reached ();
776d17
-			enroll->service_principals = _adcli_strv_add (enroll->service_principals,
776d17
-			                                              name, &length);
776d17
-
776d17
-			if (enroll->host_fqdn) {
776d17
-				if (asprintf (&name, "%s/%s", enroll->service_names[i], enroll->host_fqdn) < 0)
776d17
-					return_unexpected_if_reached ();
776d17
-				enroll->service_principals = _adcli_strv_add (enroll->service_principals,
776d17
-				                                              name, &length);
776d17
-			}
776d17
-		}
776d17
+		return add_service_names_to_service_principals (enroll);
776d17
 	}
776d17
 
776d17
 	return ADCLI_SUCCESS;
776d17
@@ -356,6 +367,7 @@ ensure_keytab_principals (adcli_result res,
776d17
 	return_unexpected_if_fail (k5 != NULL);
776d17
 
776d17
 	enroll->keytab_principals = calloc (count + 3, sizeof (krb5_principal));
776d17
+	return_unexpected_if_fail (enroll->keytab_principals != NULL);
776d17
 	at = 0;
776d17
 
776d17
 	/* First add the principal for the computer account name */
776d17
@@ -1266,7 +1278,7 @@ update_computer_account (adcli_enroll *enroll)
776d17
 		}
776d17
 	}
776d17
 
776d17
-	if (res == ADCLI_SUCCESS && !enroll->user_princpal_generate) {
776d17
+	if (res == ADCLI_SUCCESS && enroll->user_principal != NULL && !enroll->user_princpal_generate) {
776d17
 		char *vals_userPrincipalName[] = { enroll->user_principal, NULL };
776d17
 		LDAPMod userPrincipalName = { LDAP_MOD_REPLACE, "userPrincipalName", { vals_userPrincipalName, }, };
776d17
 		LDAPMod *mods[] = { &userPrincipalName, NULL, };
776d17
@@ -1519,7 +1531,8 @@ add_principal_to_keytab (adcli_enroll *enroll,
776d17
                          krb5_context k5,
776d17
                          krb5_principal principal,
776d17
                          const char *principal_name,
776d17
-                         int *which_salt)
776d17
+                         int *which_salt,
776d17
+                         adcli_enroll_flags flags)
776d17
 {
776d17
 	match_principal_kvno closure;
776d17
 	krb5_data password;
776d17
@@ -1547,41 +1560,47 @@ add_principal_to_keytab (adcli_enroll *enroll,
776d17
 		             enroll->keytab_name);
776d17
 	}
776d17
 
776d17
-	password.data = enroll->computer_password;
776d17
-	password.length = strlen (enroll->computer_password);
776d17
-
776d17
 	enctypes = adcli_enroll_get_keytab_enctypes (enroll);
776d17
 
776d17
-	/*
776d17
-	 * So we need to discover which salt to use. As a side effect we are
776d17
-	 * also testing that our account works.
776d17
-	 */
776d17
+	if (flags & ADCLI_ENROLL_PASSWORD_VALID) {
776d17
+		code = _adcli_krb5_keytab_copy_entries (k5, enroll->keytab, principal,
776d17
+		                                        enroll->kvno, enctypes);
776d17
+	} else {
776d17
 
776d17
-	salts = build_principal_salts (enroll, k5, principal);
776d17
-	return_unexpected_if_fail (salts != NULL);
776d17
+		password.data = enroll->computer_password;
776d17
+		password.length = strlen (enroll->computer_password);
776d17
 
776d17
-	if (*which_salt < 0) {
776d17
-		code = _adcli_krb5_keytab_discover_salt (k5, principal, enroll->kvno, &password,
776d17
-		                                         enctypes, salts, which_salt);
776d17
-		if (code != 0) {
776d17
-			_adcli_warn ("Couldn't authenticate with keytab while discovering which salt to use: %s: %s",
776d17
-			             principal_name, krb5_get_error_message (k5, code));
776d17
-			*which_salt = DEFAULT_SALT;
776d17
-		} else {
776d17
-			assert (*which_salt >= 0);
776d17
-			_adcli_info ("Discovered which keytab salt to use");
776d17
+		/*
776d17
+		 * So we need to discover which salt to use. As a side effect we are
776d17
+		 * also testing that our account works.
776d17
+		 */
776d17
+
776d17
+		salts = build_principal_salts (enroll, k5, principal);
776d17
+		return_unexpected_if_fail (salts != NULL);
776d17
+
776d17
+		if (*which_salt < 0) {
776d17
+			code = _adcli_krb5_keytab_discover_salt (k5, principal, enroll->kvno, &password,
776d17
+			                                         enctypes, salts, which_salt);
776d17
+			if (code != 0) {
776d17
+				_adcli_warn ("Couldn't authenticate with keytab while discovering which salt to use: %s: %s",
776d17
+				             principal_name, krb5_get_error_message (k5, code));
776d17
+				*which_salt = DEFAULT_SALT;
776d17
+			} else {
776d17
+				assert (*which_salt >= 0);
776d17
+				_adcli_info ("Discovered which keytab salt to use");
776d17
+			}
776d17
 		}
776d17
-	}
776d17
 
776d17
-	code = _adcli_krb5_keytab_add_entries (k5, enroll->keytab, principal,
776d17
-	                                       enroll->kvno, &password, enctypes, &salts[*which_salt]);
776d17
+		code = _adcli_krb5_keytab_add_entries (k5, enroll->keytab, principal,
776d17
+		                                       enroll->kvno, &password, enctypes, &salts[*which_salt]);
776d17
 
776d17
-	free_principal_salts (k5, salts);
776d17
+		free_principal_salts (k5, salts);
776d17
 
776d17
-	if (code != 0) {
776d17
-		_adcli_err ("Couldn't add keytab entries: %s: %s",
776d17
-		            enroll->keytab_name, krb5_get_error_message (k5, code));
776d17
-		return ADCLI_ERR_FAIL;
776d17
+		if (code != 0) {
776d17
+			_adcli_err ("Couldn't add keytab entries: %s: %s",
776d17
+			            enroll->keytab_name, krb5_get_error_message (k5, code));
776d17
+			return ADCLI_ERR_FAIL;
776d17
+		}
776d17
 	}
776d17
 
776d17
 
776d17
@@ -1591,7 +1610,8 @@ add_principal_to_keytab (adcli_enroll *enroll,
776d17
 }
776d17
 
776d17
 static adcli_result
776d17
-update_keytab_for_principals (adcli_enroll *enroll)
776d17
+update_keytab_for_principals (adcli_enroll *enroll,
776d17
+                              adcli_enroll_flags flags)
776d17
 {
776d17
 	krb5_context k5;
776d17
 	adcli_result res;
776d17
@@ -1608,7 +1628,7 @@ update_keytab_for_principals (adcli_enroll *enroll)
776d17
 		if (krb5_unparse_name (k5, enroll->keytab_principals[i], &name) != 0)
776d17
 			name = "";
776d17
 		res = add_principal_to_keytab (enroll, k5, enroll->keytab_principals[i],
776d17
-		                               name, &which_salt);
776d17
+		                               name, &which_salt, flags);
776d17
 		krb5_free_unparsed_name (k5, name);
776d17
 
776d17
 		if (res != ADCLI_SUCCESS)
776d17
@@ -1807,6 +1827,20 @@ enroll_join_or_update_tasks (adcli_enroll *enroll,
776d17
 	/* We ignore failures of setting these fields */
776d17
 	update_and_calculate_enctypes (enroll);
776d17
 	update_computer_account (enroll);
776d17
+
776d17
+	/* service_names is only set from input on the command line, so no
776d17
+	 * additional check for explicit is needed here */
776d17
+	if (enroll->service_names != NULL) {
776d17
+		res = add_service_names_to_service_principals (enroll);
776d17
+		if (res != ADCLI_SUCCESS) {
776d17
+			return res;
776d17
+		}
776d17
+		res = ensure_keytab_principals (res, enroll);
776d17
+		if (res != ADCLI_SUCCESS) {
776d17
+			return res;
776d17
+		}
776d17
+	}
776d17
+
776d17
 	update_service_principals (enroll);
776d17
 
776d17
 	if ( (flags & ADCLI_ENROLL_ADD_SAMBA_DATA) && ! (flags & ADCLI_ENROLL_PASSWORD_VALID)) {
776d17
@@ -1826,7 +1860,7 @@ enroll_join_or_update_tasks (adcli_enroll *enroll,
776d17
 	 * that we use for salting.
776d17
 	 */
776d17
 
776d17
-	return update_keytab_for_principals (enroll);
776d17
+	return update_keytab_for_principals (enroll, flags);
776d17
 }
776d17
 
776d17
 adcli_result
776d17
@@ -1927,7 +1961,11 @@ adcli_enroll_update (adcli_enroll *enroll,
776d17
 
776d17
 	if (_adcli_check_nt_time_string_lifetime (value,
776d17
 	                adcli_enroll_get_computer_password_lifetime (enroll))) {
776d17
-		flags |= ADCLI_ENROLL_NO_KEYTAB;
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
+			flags |= ADCLI_ENROLL_NO_KEYTAB;
776d17
+		}
776d17
 		flags |= ADCLI_ENROLL_PASSWORD_VALID;
776d17
 	}
776d17
 	free (value);
776d17
diff --git a/library/adkrb5.c b/library/adkrb5.c
776d17
index b0e903e..033c181 100644
776d17
--- a/library/adkrb5.c
776d17
+++ b/library/adkrb5.c
776d17
@@ -204,6 +204,119 @@ _adcli_krb5_open_keytab (krb5_context k5,
776d17
 	return ADCLI_SUCCESS;
776d17
 }
776d17
 
776d17
+typedef struct {
776d17
+	krb5_kvno kvno;
776d17
+	krb5_enctype enctype;
776d17
+	int matched;
776d17
+} match_enctype_kvno;
776d17
+
776d17
+static krb5_boolean
776d17
+match_enctype_and_kvno (krb5_context k5,
776d17
+                        krb5_keytab_entry *entry,
776d17
+                        void *data)
776d17
+{
776d17
+	krb5_boolean similar = FALSE;
776d17
+	match_enctype_kvno *closure = data;
776d17
+	krb5_error_code code;
776d17
+
776d17
+	assert (closure->enctype);
776d17
+
776d17
+	code = krb5_c_enctype_compare (k5, closure->enctype, entry->key.enctype,
776d17
+	                               &similar);
776d17
+
776d17
+	if (code == 0 && entry->vno == closure->kvno && similar) {
776d17
+		closure->matched = 1;
776d17
+		return 1;
776d17
+	}
776d17
+
776d17
+	return 0;
776d17
+}
776d17
+
776d17
+static krb5_error_code
776d17
+_adcli_krb5_get_keyblock (krb5_context k5,
776d17
+                          krb5_keytab keytab,
776d17
+                          krb5_keyblock *keyblock,
776d17
+                          krb5_boolean (* match_func) (krb5_context,
776d17
+                                                       krb5_keytab_entry *,
776d17
+                                                       void *),
776d17
+                          void *match_data)
776d17
+{
776d17
+	krb5_kt_cursor cursor;
776d17
+	krb5_keytab_entry entry;
776d17
+	krb5_error_code code;
776d17
+
776d17
+	code = krb5_kt_start_seq_get (k5, keytab, &cursor);
776d17
+	if (code == KRB5_KT_END || code == ENOENT)
776d17
+		return 0;
776d17
+	else if (code != 0)
776d17
+		return code;
776d17
+
776d17
+	for (;;) {
776d17
+		code = krb5_kt_next_entry (k5, keytab, &entry, &cursor);
776d17
+		if (code != 0)
776d17
+			break;
776d17
+
776d17
+		/* See if we should remove this entry */
776d17
+		if (!match_func (k5, &entry, match_data)) {
776d17
+			krb5_free_keytab_entry_contents (k5, &entry);
776d17
+			continue;
776d17
+		}
776d17
+
776d17
+		code = krb5_copy_keyblock_contents (k5, &entry.key, keyblock);
776d17
+		krb5_free_keytab_entry_contents (k5, &entry);
776d17
+		break;
776d17
+
776d17
+
776d17
+	}
776d17
+
776d17
+	if (code == KRB5_KT_END)
776d17
+		code = 0;
776d17
+
776d17
+	krb5_kt_end_seq_get (k5, keytab, &cursor);
776d17
+	return code;
776d17
+}
776d17
+
776d17
+krb5_error_code
776d17
+_adcli_krb5_keytab_copy_entries (krb5_context k5,
776d17
+                                 krb5_keytab keytab,
776d17
+                                 krb5_principal principal,
776d17
+                                 krb5_kvno kvno,
776d17
+                                 krb5_enctype *enctypes)
776d17
+{
776d17
+	krb5_keytab_entry entry;
776d17
+	krb5_error_code code;
776d17
+	int i;
776d17
+	match_enctype_kvno closure;
776d17
+
776d17
+	for (i = 0; enctypes[i] != 0; i++) {
776d17
+
776d17
+		closure.kvno = kvno;
776d17
+		closure.enctype = enctypes[i];
776d17
+		closure.matched = 0;
776d17
+
776d17
+		memset (&entry, 0, sizeof (entry));
776d17
+
776d17
+		code = _adcli_krb5_get_keyblock (k5, keytab, &entry.key,
776d17
+		                                 match_enctype_and_kvno, &closure);
776d17
+		if (code != 0) {
776d17
+			return code;
776d17
+		}
776d17
+
776d17
+
776d17
+		entry.principal = principal;
776d17
+		entry.vno = kvno;
776d17
+
776d17
+		code = krb5_kt_add_entry (k5, keytab, &entry);
776d17
+
776d17
+		entry.principal = NULL;
776d17
+		krb5_free_keytab_entry_contents (k5, &entry);
776d17
+
776d17
+		if (code != 0)
776d17
+			return code;
776d17
+	}
776d17
+
776d17
+	return 0;
776d17
+}
776d17
 
776d17
 krb5_error_code
776d17
 _adcli_krb5_keytab_add_entries (krb5_context k5,
776d17
diff --git a/library/adprivate.h b/library/adprivate.h
776d17
index 83a88f6..7485249 100644
776d17
--- a/library/adprivate.h
776d17
+++ b/library/adprivate.h
776d17
@@ -282,6 +282,12 @@ krb5_enctype *   _adcli_krb5_parse_enctypes       (const char *value);
776d17
 
776d17
 char *           _adcli_krb5_format_enctypes      (krb5_enctype *enctypes);
776d17
 
776d17
+krb5_error_code  _adcli_krb5_keytab_copy_entries  (krb5_context k5,
776d17
+                                                   krb5_keytab keytab,
776d17
+                                                   krb5_principal principal,
776d17
+                                                   krb5_kvno kvno,
776d17
+                                                   krb5_enctype *enctypes);
776d17
+
776d17
 struct _adcli_attrs {
776d17
 	LDAPMod **mods;
776d17
 	int len;
776d17
-- 
776d17
2.14.4
776d17