From 4c8254583f9eefced9b483bcb6b2a99c28fba8c1 Mon Sep 17 00:00:00 2001 From: openoms Date: Tue, 1 Jun 2021 18:38:16 +0100 Subject: [PATCH 1/4] hsm_encryption: read from STDIN if not in a TTY Changelog-Added: hsmtool: allow piped passwords --- common/hsm_encryption.c | 62 ++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/common/hsm_encryption.c b/common/hsm_encryption.c index d33c0ff56ba6..41388843924d 100644 --- a/common/hsm_encryption.c +++ b/common/hsm_encryption.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include char *hsm_secret_encryption_key(const char *pass, struct secret *key) { @@ -84,31 +86,41 @@ char *read_stdin_pass(char **reason) 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; + if (isatty(fileno(stdin))) { + /* 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; + } + } 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; From 8e86a57824107c11913490f84e78c9ea8c4f766b Mon Sep 17 00:00:00 2001 From: openoms Date: Tue, 1 Jun 2021 19:25:20 +0100 Subject: [PATCH 2/4] lightningd: remove duplicate temp term creation --- lightningd/options.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lightningd/options.c b/lightningd/options.c index 6801d73f75be..e5d2cc1e812c 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -390,16 +390,8 @@ static char *opt_important_plugin(const char *arg, struct lightningd *ld) */ static char *opt_set_hsm_password(struct lightningd *ld) { - struct termios current_term, temp_term; char *passwd, *passwd_confirmation, *err; - /* Get the password from stdin, but don't echo it. */ - if (tcgetattr(fileno(stdin), ¤t_term) != 0) - return "Could not get current terminal options."; - temp_term = current_term; - temp_term.c_lflag &= ~ECHO; - if (tcsetattr(fileno(stdin), TCSAFLUSH, &temp_term) != 0) - return "Could not disable password echoing."; printf("The hsm_secret is encrypted with a password. In order to " "decrypt it and start the node you must provide the password.\n"); printf("Enter hsm_secret password:\n"); From 07269b0a60cbaee71322dacefbde3c7c6ab86da1 Mon Sep 17 00:00:00 2001 From: openoms <43343391+openoms@users.noreply.github.com> Date: Thu, 3 Jun 2021 10:07:10 +0100 Subject: [PATCH 3/4] hsm_encryption.c: remove whitespace from line end --- common/hsm_encryption.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/hsm_encryption.c b/common/hsm_encryption.c index 41388843924d..8909a0dd0db6 100644 --- a/common/hsm_encryption.c +++ b/common/hsm_encryption.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include char *hsm_secret_encryption_key(const char *pass, struct secret *key) { From cf5eabc2df094450914cb0f38124e9dae6eafe07 Mon Sep 17 00:00:00 2001 From: openoms <43343391+openoms@users.noreply.github.com> Date: Thu, 3 Jun 2021 11:45:03 +0100 Subject: [PATCH 4/4] hsm_encryption.c: fix source include order --- common/hsm_encryption.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/hsm_encryption.c b/common/hsm_encryption.c index 8909a0dd0db6..2bc457a27443 100644 --- a/common/hsm_encryption.c +++ b/common/hsm_encryption.c @@ -1,9 +1,9 @@ #include #include #include +#include #include #include -#include char *hsm_secret_encryption_key(const char *pass, struct secret *key) {