From fd4ee6a7124099afe87224810767f7710a8462db Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Fri, 6 May 2022 08:13:01 +0200 Subject: [PATCH] Use the correct file descriptor for printing Before we used printf("\n"); to simulate the enter key the user pressed, which prints to normal stdout, which could have been redirected. Now we directly write to the found TTY. We also close the TTY file descriptor on every code path now. --- readpassphrase.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/readpassphrase.h b/readpassphrase.h index 13eac35..94a63d9 100644 --- a/readpassphrase.h +++ b/readpassphrase.h @@ -39,23 +39,25 @@ char* readpassphrase(const char* prompt, char* buf, size_t bufsz) { if (write(fd, prompt, strlen(prompt)) < 0) { tcsetattr(ttyfd, 0, &term); + close(fd); return NULL; } n = read(fd, buf, bufsz); if (n < 0) { tcsetattr(ttyfd, 0, &term); - printf("\n"); + n = write(fd, "\n", 1); + close(fd); return NULL; } buf[n-1] = '\0'; + // NOTE: As we disabled echo, the enter sent by the user isn't displayed, so we resend it. + n = write(fd, "\n", 1); + close(fd); tcsetattr(ttyfd, 0, &term); - // NOTE: As we disabled echo, the enter sent by the user isn't displayed. - printf("\n"); - return buf; }