From 1b5d8a7d4c6396f351e1c7d29d0bd4a24137aa26 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 16 Oct 2020 19:33:12 +0200 Subject: [PATCH] Make it possible to read the passphrase from STDIN. --- frontend/src/main.c | 30 +++++++++++++++++++++--------- frontend/src/utils.c | 1 + libMCrypt/include/mcrypt.h | 3 +++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index f1594a2..992a95d 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -22,24 +22,28 @@ static volatile int g_interrupted = 0; static char* read_passphrase(const CHR* const file_name) { - const size_t buff_size = 1024U; - char *const buffer = (char*) malloc(buff_size * sizeof(char)); + static const size_t buff_size = 512U; + char *buffer = (char*) malloc(buff_size * sizeof(char)); if (!buffer) { return NULL; } - FILE *const file = FOPEN(file_name, T("rb")); + + const int use_stdin = (STRICMP(file_name, T("-")) == 0); + FILE *const file = use_stdin ? stdin : FOPEN(file_name, T("rb")); if (!file) { + free(buffer); return NULL; } + do { if (!fgets(buffer, (int)buff_size, file)) { - fclose(file); free(buffer); - return NULL; + buffer = NULL; + goto finish; } size_t length = strlen(buffer); while ((length > 0U) && ((buffer[length - 1U] == '\r') || (buffer[length - 1U] == '\n'))) @@ -48,7 +52,14 @@ static char* read_passphrase(const CHR* const file_name) } } while (!buffer[0U]); - fclose(file); + +finish: + + if ((!use_stdin) && file) + { + fclose(file); + } + return buffer; } @@ -505,16 +516,17 @@ int MAIN(int argc, CHR* argv[]) FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr); FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr); FPUTS(T("====================================================================\n\n"), stderr); - FPUTS(T("Usage:\n"), stderr); + FPUTS(T("Synopsis:\n"), stderr); FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt [[@][:]] \n"), program); FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]] \n\n"), program); - FPUTS(T("Notes:\n"), stderr); + FPUTS(T("Remarks:\n"), stderr); FPUTS(T("- If is prefixed with a '@' character, then it specifies the file\n"), stderr); FPUTS(T(" to read the passphrase from; only the first line in that file is used!\n"), stderr); FPUTS(T("- If is prefixed with a ':' character, then the leading character\n"), stderr); FPUTS(T(" is skipped and the remainder of the argument is used as passphrase.\n"), stderr); FPUTS(T("- If the argument is *not* present, then the environment variable\n"), stderr); - FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set and it specifies the passphrase to be used.\n\n"), ENVV_PASSWD_NAME); + FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set; it specifies the passphrase to be used.\n"), ENVV_PASSWD_NAME); + FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr); return 0; } if ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test")))) diff --git a/frontend/src/utils.c b/frontend/src/utils.c index dd16004..08e8b2e 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -41,6 +41,7 @@ void init_terminal(void) { #ifdef _WIN32 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stderr), _O_U8TEXT); if (_acmdln) SecureZeroMemory(_acmdln, strlen(_acmdln) * sizeof(char)); if (_wcmdln) SecureZeroMemory(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t)); diff --git a/libMCrypt/include/mcrypt.h b/libMCrypt/include/mcrypt.h index 0e34b02..afcf8f0 100644 --- a/libMCrypt/include/mcrypt.h +++ b/libMCrypt/include/mcrypt.h @@ -9,6 +9,9 @@ #include #include +/* + * Version info + */ extern const char *const LIBMCRYPT_VERSION; extern const char* const LIBMCRYPT_BUILDNO;