Blame 0007-enroll-more-filters-for-random-characters.patch

7f6164b
From 2750f536ac6746756335eec8332060d2365a4126 Mon Sep 17 00:00:00 2001
7f6164b
From: Sumit Bose <sbose@redhat.com>
7f6164b
Date: Tue, 27 Oct 2020 14:44:07 +0100
7f6164b
Subject: [PATCH 07/10] enroll: more filters for random characters
7f6164b
7f6164b
Make handling of random strings more flexible.
7f6164b
7f6164b
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1854112
7f6164b
---
7f6164b
 library/adenroll.c | 30 +++++++++++++++++++++++++++---
7f6164b
 1 file changed, 27 insertions(+), 3 deletions(-)
7f6164b
7f6164b
diff --git a/library/adenroll.c b/library/adenroll.c
7f6164b
index 9cdc79b..44383cc 100644
7f6164b
--- a/library/adenroll.c
7f6164b
+++ b/library/adenroll.c
7f6164b
@@ -259,6 +259,29 @@ ensure_computer_sam (adcli_result res,
7f6164b
 	return ADCLI_SUCCESS;
7f6164b
 }
7f6164b
 
7f6164b
+typedef int (rand_filter) (char *password, int length);
7f6164b
+
7f6164b
+static int
7f6164b
+filter_sam_chars (char *password,
7f6164b
+                       int length)
7f6164b
+{
7f6164b
+	int i, j;
7f6164b
+
7f6164b
+	/*
7f6164b
+	 * There are a couple of restrictions for characters in the
7f6164b
+	 * sAMAccountName attribute value, for our purpose (random suffix)
7f6164b
+	 * letters and numbers are sufficient.
7f6164b
+	 */
7f6164b
+	for (i = 0, j = 0; i < length; i++) {
7f6164b
+		if (password[i] >= 48 && password[i] <= 122 &&
7f6164b
+		    isalnum (password[i]))
7f6164b
+			password[j++] = password[i];
7f6164b
+	}
7f6164b
+
7f6164b
+	/* return the number of valid characters remaining */
7f6164b
+	return j;
7f6164b
+}
7f6164b
+
7f6164b
 static int
7f6164b
 filter_password_chars (char *password,
7f6164b
                        int length)
7f6164b
@@ -283,7 +306,8 @@ filter_password_chars (char *password,
7f6164b
 
7f6164b
 static char *
7f6164b
 generate_host_password  (adcli_enroll *enroll,
7f6164b
-                         size_t length)
7f6164b
+                         size_t length,
7f6164b
+                         rand_filter *filter)
7f6164b
 {
7f6164b
 	char *password;
7f6164b
 	krb5_context k5;
7f6164b
@@ -305,7 +329,7 @@ generate_host_password  (adcli_enroll *enroll,
7f6164b
 		code = krb5_c_random_make_octets (k5, &buffer);
7f6164b
 		return_val_if_fail (code == 0, NULL);
7f6164b
 
7f6164b
-		at += filter_password_chars (buffer.data, buffer.length);
7f6164b
+		at += filter (buffer.data, buffer.length);
7f6164b
 		assert (at <= length);
7f6164b
 	}
7f6164b
 
7f6164b
@@ -333,7 +357,7 @@ ensure_computer_password (adcli_result res,
7f6164b
 		_adcli_info ("Using default reset computer password");
7f6164b
 
7f6164b
 	} else {
7f6164b
-		enroll->computer_password = generate_host_password (enroll, length);
7f6164b
+		enroll->computer_password = generate_host_password (enroll, length, filter_password_chars);
7f6164b
 		return_unexpected_if_fail (enroll->computer_password != NULL);
7f6164b
 		_adcli_info ("Generated %d character computer password", length);
7f6164b
 	}
7f6164b
-- 
7f6164b
2.28.0
7f6164b