Simplify tty finding process in readpassphrase
This went through a bunch of hoops before to acquire a file descriptor we already had, this is now ommited.
This commit is contained in:
parent
c1acb73270
commit
9ce20a62a4
1 changed files with 4 additions and 21 deletions
|
|
@ -4,8 +4,6 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
char* readpassphrase(const char* prompt, char* buf, size_t bufsz) {
|
char* readpassphrase(const char* prompt, char* buf, size_t bufsz) {
|
||||||
char stdin_path[256];
|
|
||||||
char tty_link_path[256];
|
|
||||||
int n;
|
int n;
|
||||||
int ttyfd = -1;
|
int ttyfd = -1;
|
||||||
|
|
||||||
|
|
@ -21,42 +19,27 @@ char* readpassphrase(const char* prompt, char* buf, size_t bufsz) {
|
||||||
if (ttyfd < 0)
|
if (ttyfd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
snprintf(tty_link_path, sizeof(tty_link_path), "/proc/self/fd/%d", ttyfd);
|
|
||||||
|
|
||||||
n = readlink(tty_link_path, stdin_path, sizeof(stdin_path));
|
|
||||||
if (n < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
stdin_path[n] = '\0';
|
|
||||||
|
|
||||||
int fd = open(stdin_path, O_RDWR);
|
|
||||||
if (fd < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
term.c_lflag &= ~ECHO;
|
term.c_lflag &= ~ECHO;
|
||||||
tcsetattr(ttyfd, 0, &term);
|
tcsetattr(ttyfd, 0, &term);
|
||||||
term.c_lflag |= ECHO;
|
term.c_lflag |= ECHO;
|
||||||
|
|
||||||
if (write(fd, prompt, strlen(prompt)) < 0) {
|
if (write(ttyfd, prompt, strlen(prompt)) < 0) {
|
||||||
tcsetattr(ttyfd, 0, &term);
|
tcsetattr(ttyfd, 0, &term);
|
||||||
close(fd);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
n = read(fd, buf, bufsz);
|
n = read(ttyfd, buf, bufsz);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
tcsetattr(ttyfd, 0, &term);
|
tcsetattr(ttyfd, 0, &term);
|
||||||
n = write(fd, "\n", 1);
|
n = write(ttyfd, "\n", 1);
|
||||||
close(fd);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[n-1] = '\0';
|
buf[n-1] = '\0';
|
||||||
|
|
||||||
// NOTE: As we disabled echo, the enter sent by the user isn't displayed, so we resend it.
|
// NOTE: As we disabled echo, the enter sent by the user isn't displayed, so we resend it.
|
||||||
n = write(fd, "\n", 1);
|
n = write(ttyfd, "\n", 1);
|
||||||
|
|
||||||
close(fd);
|
|
||||||
tcsetattr(ttyfd, 0, &term);
|
tcsetattr(ttyfd, 0, &term);
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue