From 5268039c3d212318ed4a49b83106b77de1d14b0d Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Wed, 15 Oct 2014 14:00:44 -0400 Subject: [PATCH] A bunch of cross-platform fixes for mpw.c + make config file optional and read master password from input instead. --- MasterPassword/C/build | 55 ++++++++++++++++++++++++++++++++---------- MasterPassword/C/mpw.c | 46 ++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 27 deletions(-) diff --git a/MasterPassword/C/build b/MasterPassword/C/build index 79f24544..11b91f76 100755 --- a/MasterPassword/C/build +++ b/MasterPassword/C/build @@ -1,8 +1,28 @@ #!/usr/bin/env bash -# Run with -DDEBUG to enable trace-level output. +# +# TROUBLESHOOTING +# - See the 'options' array. Comment/uncomment lines as you see fit. +# - If you see 'undefined reference to `clock_gettime'', try ./build -lrt instead. +# +# BUGS +# masterpassword@lyndir.com +# +# AUTHOR +# Maarten Billemont +# cd "${BASH_SOURCE%/*}" set -e +options=( + # optional features. + -DDEBUG # Turn on debugging verbosity. + #-DEDITLINE -ledit -ltermcap # Use editline for reading the master password. + -DREADLINE -lreadline # Use readline for reading the master password. +) + + +### DEPENDENCIES + if ! [[ -e lib/scrypt/scrypt-scryptenc.o ]]; then # libscrypt not built. pushd lib/scrypt @@ -52,23 +72,32 @@ fi echo echo "Building mpw..." -# libscrypt built. -deps=( +cc() { + if hash llvm-gcc; then + llvm-gcc -Qunused-arguments "$@" + else + gcc "$@" + fi +} + + +### MPW + +options+=( # include paths. -I"lib/scrypt/lib" -I"lib/scrypt/libcperciva" # library paths. -L"." -L"lib/scrypt" # link libraries. - -l "scrypt-crypto_aesctr.o" - -l "scrypt-sha256.o" - -l "scrypt-crypto_scrypt-nosse.o" - -l "scrypt-memlimit.o" - -l "scrypt-scryptenc_cpuperf.o" - -l "scrypt-scryptenc.o" - -l "crypto" + "lib/scrypt/scrypt-crypto_aesctr.o" + "lib/scrypt/scrypt-sha256.o" + "lib/scrypt/scrypt-crypto_scrypt-nosse.o" + "lib/scrypt/scrypt-memlimit.o" + "lib/scrypt/scrypt-scryptenc_cpuperf.o" + "lib/scrypt/scrypt-scryptenc.o" + -l"crypto" ) -# build mpw. -gcc "${deps[@]}" -Qunused-arguments -c types.c -o types.o "$@" -gcc "${deps[@]}" -Qunused-arguments -l"types.o" mpw.c -o mpw "$@" +cc "${options[@]}" -std=c99 -c types.c -o types.o "$@" +cc "${options[@]}" -std=c99 -l"types.o" mpw.c -o mpw "$@" echo "done! Now run ./install or use ./mpw" diff --git a/MasterPassword/C/mpw.c b/MasterPassword/C/mpw.c index a435887d..483f7fb5 100644 --- a/MasterPassword/C/mpw.c +++ b/MasterPassword/C/mpw.c @@ -23,6 +23,12 @@ #include #include "types.h" +#if defined(READLINE) +#include +#elif defined(EDITLINE) +#include +#endif + #define MP_N 32768 #define MP_r 8 #define MP_p 2 @@ -98,8 +104,7 @@ int main(int argc, char *const argv[]) { const char *siteCounterString = getenv( MP_env_sitecounter ); // Read the options. - char opt; - while ((opt = getopt(argc, argv, "u:t:c:v:h")) != -1) + for (int opt; (opt = getopt(argc, argv, "u:t:c:v:h")) != -1;) switch (opt) { case 'h': usage(); @@ -172,22 +177,35 @@ int main(int argc, char *const argv[]) { } trc("mpwConfigPath: %s\n", mpwConfigPath); FILE *mpwConfig = fopen(mpwConfigPath, "r"); - if (!mpwConfig) { - fprintf(stderr, "Couldn't open configuration file: %s: %d\n", mpwConfigPath, errno); - return 1; - } free(mpwConfigPath); - char *line = NULL; - size_t linecap = 0; - ssize_t linelen; - while ((linelen = getline(&line, &linecap, mpwConfig)) > 0) - if (strcmp(strsep(&line, ":"), userName) == 0) { - masterPassword = strsep(&line, "\n"); - break; + if (mpwConfig) { + char *line = NULL; + size_t linecap = 0; + ssize_t linelen; + while ((linelen = getline(&line, &linecap, mpwConfig)) > 0) + if (strcmp(strsep(&line, ":"), userName) == 0) { + masterPassword = strsep(&line, "\n"); + break; + } + } + while (!masterPassword) { +#if defined(READLINE) + masterPassword = readline( "Your master password: " ); +#elif defined(EDITLINE) + EditLine *e = el_init("mpw", stdin, stdout, stderr); + int count = 0; + char *line = el_gets(e, &count); + masterPassword = strsep(&line, "\n"); + el_end(e); + + if (count < 0) { + fprintf(stderr, "Could not read master password: %d\n", errno); + continue; } - if (!masterPassword) { +#else fprintf(stderr, "Missing master password for user: %s\n", userName); return 1; +#endif } trc("masterPassword: %s\n", masterPassword);