From 08d2294e5ec8c6dfd551a0227f9878d0620ed394 Mon Sep 17 00:00:00 2001 From: discomfitor Date: Thu, 11 Aug 2011 03:41:22 -0400 Subject: [PATCH] add password callback hook --- lrzip.c | 42 +++++++++++++++++++++++------------------- lrzip_private.h | 2 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lrzip.c b/lrzip.c index 2de8920..6edea41 100644 --- a/lrzip.c +++ b/lrzip.c @@ -538,34 +538,38 @@ static void get_hash(rzip_control *control, int make_hash) mlock(control->salt_pass, PASS_LEN); mlock(control->hash, HASH_LEN); - /* Disable stdin echo to screen */ - tcgetattr(fileno(stdin), &termios_p); - termios_p.c_lflag &= ~ECHO; - tcsetattr(fileno(stdin), 0, &termios_p); + if (control->pass_cb) { + control->pass_cb(control->pass_data, passphrase, PASS_LEN); + if (!passphrase[0]) fatal(control, "Supplied password was null!"); + } else { + /* Disable stdin echo to screen */ + tcgetattr(fileno(stdin), &termios_p); + termios_p.c_lflag &= ~ECHO; + tcsetattr(fileno(stdin), 0, &termios_p); retry_pass: - print_output("Enter passphrase: "); - control->salt_pass_len = get_pass(control, passphrase) + SALT_LEN; - print_output("\n"); - if (make_hash) { - print_output("Re-enter passphrase: "); - get_pass(control, testphrase); + print_output("Enter passphrase: "); + control->salt_pass_len = get_pass(control, passphrase) + SALT_LEN; print_output("\n"); - if (strcmp(passphrase, testphrase)) { - print_output("Passwords do not match. Try again.\n"); - goto retry_pass; + if (make_hash) { + print_output("Re-enter passphrase: "); + get_pass(control, testphrase); + print_output("\n"); + if (strcmp(passphrase, testphrase)) { + print_output("Passwords do not match. Try again.\n"); + goto retry_pass; + } } + termios_p.c_lflag |= ECHO; + tcsetattr(fileno(stdin), 0, &termios_p); + memset(testphrase, 0, PASS_LEN); } - termios_p.c_lflag |= ECHO; - tcsetattr(fileno(stdin), 0, &termios_p); - memset(testphrase, 0, PASS_LEN); - munlock(testphrase, PASS_LEN); - free(testphrase); - memcpy(control->salt_pass, control->salt, SALT_LEN); memcpy(control->salt_pass + SALT_LEN, passphrase, PASS_LEN - SALT_LEN); lrz_stretch(control); memset(passphrase, 0, PASS_LEN); munlock(passphrase, PASS_LEN); + munlock(testphrase, PASS_LEN); + free(testphrase); free(passphrase); } diff --git a/lrzip_private.h b/lrzip_private.h index f080323..e96cc99 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -311,6 +311,8 @@ struct rzip_control { int fd_hist; i64 encloops; i64 secs; + void (*pass_cb)(void *, char *, size_t); /* callback to get password in lib */ + void *pass_data; uchar salt[SALT_LEN]; uchar *salt_pass; int salt_pass_len;