mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
hsm_encryption: read from STDIN if not in a TTY
Changelog-Added: hsmtool: allow piped passwords
This commit is contained in:
committed by
Christian Decker
parent
5e1fadf799
commit
b72c05fbda
@@ -2,6 +2,8 @@
|
|||||||
#include <common/hsm_encryption.h>
|
#include <common/hsm_encryption.h>
|
||||||
#include <sodium/utils.h>
|
#include <sodium/utils.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
char *hsm_secret_encryption_key(const char *pass, struct secret *key)
|
char *hsm_secret_encryption_key(const char *pass, struct secret *key)
|
||||||
{
|
{
|
||||||
@@ -84,6 +86,7 @@ char *read_stdin_pass(char **reason)
|
|||||||
char *passwd = NULL;
|
char *passwd = NULL;
|
||||||
size_t passwd_size = 0;
|
size_t passwd_size = 0;
|
||||||
|
|
||||||
|
if (isatty(fileno(stdin))) {
|
||||||
/* Set a temporary term, same as current but with ECHO disabled. */
|
/* Set a temporary term, same as current but with ECHO disabled. */
|
||||||
if (tcgetattr(fileno(stdin), ¤t_term) != 0) {
|
if (tcgetattr(fileno(stdin), ¤t_term) != 0) {
|
||||||
*reason = "Could not get current terminal options.";
|
*reason = "Could not get current terminal options.";
|
||||||
@@ -110,6 +113,15 @@ char *read_stdin_pass(char **reason)
|
|||||||
free(passwd);
|
free(passwd);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
/* Read from stdin, 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';
|
||||||
|
}
|
||||||
|
|
||||||
return passwd;
|
return passwd;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user