mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-21 16:14:23 +01:00
lightningd: regroup hsm_secret password input logic
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
This commit is contained in:
committed by
Christian Decker
parent
917f78a4f8
commit
a441485a35
@@ -1,6 +1,8 @@
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/hsm_encryption.h>
|
||||
#include <sodium.h>
|
||||
#include <sodium/utils.h>
|
||||
#include <termios.h>
|
||||
|
||||
|
||||
char *hsm_secret_encryption_key(const char *pass, struct secret *key)
|
||||
@@ -36,3 +38,39 @@ void discard_key(struct secret *key TAKES)
|
||||
if (taken(key))
|
||||
tal_free(key);
|
||||
}
|
||||
|
||||
char *read_stdin_pass(char **reason)
|
||||
{
|
||||
struct termios current_term, temp_term;
|
||||
char *passwd = NULL;
|
||||
size_t passwd_size = 0;
|
||||
|
||||
/* Set a temporary term, same as current but with ECHO disabled. */
|
||||
if (tcgetattr(fileno(stdin), ¤t_term) != 0) {
|
||||
*reason = "Could not get current terminal options.";
|
||||
return NULL;
|
||||
}
|
||||
temp_term = current_term;
|
||||
temp_term.c_lflag &= ~ECHO;
|
||||
if (tcsetattr(fileno(stdin), TCSAFLUSH, &temp_term) != 0) {
|
||||
*reason = "Could not disable pass echoing.";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Read the password, do not take the newline character into account. */
|
||||
if (getline(&passwd, &passwd_size, stdin) < 0) {
|
||||
*reason = "Could not read pass from stdin.";
|
||||
return NULL;
|
||||
}
|
||||
if (passwd[strlen(passwd) - 1] == '\n')
|
||||
passwd[strlen(passwd) - 1] = '\0';
|
||||
|
||||
/* Restore the original terminal */
|
||||
if (tcsetattr(fileno(stdin), TCSAFLUSH, ¤t_term) != 0) {
|
||||
*reason = "Could not restore terminal options.";
|
||||
free(passwd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return passwd;
|
||||
}
|
||||
|
||||
@@ -19,4 +19,11 @@ char *hsm_secret_encryption_key(const char *pass, struct secret *encryption_key)
|
||||
*/
|
||||
void discard_key(struct secret *key TAKES);
|
||||
|
||||
/** Read hsm_secret encryption pass from stdin, disabling echoing.
|
||||
* @reason: if NULL is returned, will point to the human-readable error.
|
||||
*
|
||||
* Caller must free the string as it does tal-reallocate getline's output.
|
||||
*/
|
||||
char *read_stdin_pass(char **reason);
|
||||
|
||||
#endif /* LIGHTNING_COMMON_HSM_ENCRYPTION_H */
|
||||
|
||||
Reference in New Issue
Block a user