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

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