-
Notifications
You must be signed in to change notification settings - Fork 912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hsm_encryption: read from STDIN if not in a TTY #4571
Changes from 2 commits
4c82545
8e86a57
07269b0
cf5eabc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,6 +2,8 @@ | |||
#include <common/hsm_encryption.h> | ||||
#include <sodium/utils.h> | ||||
#include <termios.h> | ||||
#include <unistd.h> | ||||
#include <stdio.h> | ||||
|
||||
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))) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that if this fails in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, indeed the piped password still fails for lightning/lightningd/options.c Line 397 in 03cfe0b
read_stdin_pass .Should be able to clean that, testing now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved with: 8e86a57 tested ok with: and when called in the command line the password (echo) is still hidden
|
||||
/* 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; | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hidden whitespace at the end of line makes it fail make check-source:
Damn our pedantic source checks!
(I would normally just push a fix directly, but some people consider that rude :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, fixed in 07269b0. It's fair, every character counts!