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

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