Skip to content

Commit

Permalink
[Minor] Make read_passphrase utility more universal
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed May 20, 2023
1 parent 0ee303d commit 5c43b0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/libutil/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1383,15 +1384,18 @@ rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key)
}

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;
close (input);
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 */
Expand Down
15 changes: 13 additions & 2 deletions src/libutil/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c43b0d

Please sign in to comment.