commit 5c43b0d: [Minor] Make read_passphrase utility more universal

Vsevolod Stakhov vsevolod at rspamd.com
Sat May 20 14:56:03 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-05-20 15:37:30 +0100
URL: https://github.com/rspamd/rspamd/commit/5c43b0d60977d26e7894592853462b751a8aff11

[Minor] Make read_passphrase utility more universal

---
 src/libutil/util.c | 12 ++++++++----
 src/libutil/util.h | 15 +++++++++++++--
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/libutil/util.c b/src/libutil/util.c
index 253b651ad..f9fef347f 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -1351,10 +1351,11 @@ read_pass_tmp_sig_handler (int s)
 #endif
 
 gint
-rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key)
+rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key)
 {
 #ifdef HAVE_READPASSPHRASE_H
-	if (readpassphrase ("Enter passphrase: ", buf, size, RPP_ECHO_OFF |
+	int flags = echo ? RPP_ECHO_ON : RPP_ECHO_OFF;
+	if (readpassphrase (prompt, buf, size, flags |
 		RPP_REQUIRE_TTY) == NULL) {
 		return 0;
 	}
@@ -1383,7 +1384,10 @@ restart:
 	}
 
 	memcpy (&term, &oterm, sizeof(term));
-	term.c_lflag &= ~(ECHO | ECHONL);
+
+	if (!echo) {
+		term.c_lflag &= ~(ECHO | ECHONL);
+	}
 
 	if (tcsetattr (input, TCSAFLUSH, &term) == -1) {
 		errno = ENOTTY;
@@ -1391,7 +1395,7 @@ restart:
 		return 0;
 	}
 
-	g_assert (write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") -
+	g_assert (write (output, prompt, sizeof ("Enter passphrase: ") -
 		1) != -1);
 
 	/* Save the current sighandler */
diff --git a/src/libutil/util.h b/src/libutil/util.h
index ccc642adb..907bcd33f 100644
--- a/src/libutil/util.h
+++ b/src/libutil/util.h
@@ -281,11 +281,22 @@ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst,
  * Read passphrase from tty
  * @param buf buffer to fill with a password
  * @param size size of the buffer
- * @param rwflag unused flag
+ * @param echo turn echo on or off
  * @param key unused key
  * @return size of password read
  */
-gint rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key);
+#define rspamd_read_passphrase(buf, size, echo, key) (rspamd_read_passphrase_with_prompt("Enter passphrase: ", (buf), (size), (echo), (key)))
+
+/**
+ * Read passphrase from tty with prompt
+ * @param prompt
+ * @param buf
+ * @param size
+ * @param echo
+ * @param key
+ * @return
+ */
+gint rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key);
 
 /**
  * Portably return the current clock ticks as seconds


More information about the Commits mailing list