From 7a3ba4bbee98b81e44617c10d5727992dbd13e12 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 10 Feb 2025 00:45:45 +0100 Subject: Read password from the terminal if none was provided --- src/encpipe.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/encpipe.c b/src/encpipe.c index 735ed87..7a2b8ff 100644 --- a/src/encpipe.c +++ b/src/encpipe.c @@ -171,6 +171,26 @@ read_password_file(Context *ctx, const char *file) return 0; } +static void +read_password_from_terminal(Context *ctx) +{ + char password[512]; + char *p; + + printf("Password: "); + fflush(stdout); + if (fgets(password, sizeof password, stdin) == NULL) { + die(1, "fgets()"); + } + if ((p = strchr(password, '\r')) != NULL) { + *p = 0; + } + if ((p = strchr(password, '\n')) != NULL) { + *p = 0; + } + derive_key(ctx, password, strlen(password)); +} + static void passgen(void) { @@ -209,7 +229,7 @@ options_parse(Context *ctx, int argc, char *argv[]) break; case 'G': passgen(); - break; + break; /* NOTREACHED */ case 'i': ctx->in = optarg; break; @@ -226,9 +246,12 @@ options_parse(Context *ctx, int argc, char *argv[]) usage(); } } - if (ctx->has_key == 0 || ctx->encrypt == -1) { + if (ctx->encrypt == -1) { usage(); } + if (ctx->has_key == 0) { + read_password_from_terminal(ctx); + } } int -- cgit v1.2.3-70-g09d2