add password callback hook

This commit is contained in:
discomfitor 2011-08-11 03:41:22 -04:00 committed by Con Kolivas
parent cbff8faef7
commit 08d2294e5e
2 changed files with 25 additions and 19 deletions

42
lrzip.c
View file

@ -538,34 +538,38 @@ static void get_hash(rzip_control *control, int make_hash)
mlock(control->salt_pass, PASS_LEN); mlock(control->salt_pass, PASS_LEN);
mlock(control->hash, HASH_LEN); mlock(control->hash, HASH_LEN);
/* Disable stdin echo to screen */ if (control->pass_cb) {
tcgetattr(fileno(stdin), &termios_p); control->pass_cb(control->pass_data, passphrase, PASS_LEN);
termios_p.c_lflag &= ~ECHO; if (!passphrase[0]) fatal(control, "Supplied password was null!");
tcsetattr(fileno(stdin), 0, &termios_p); } else {
/* Disable stdin echo to screen */
tcgetattr(fileno(stdin), &termios_p);
termios_p.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), 0, &termios_p);
retry_pass: retry_pass:
print_output("Enter passphrase: "); print_output("Enter passphrase: ");
control->salt_pass_len = get_pass(control, passphrase) + SALT_LEN; 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("\n"); print_output("\n");
if (strcmp(passphrase, testphrase)) { if (make_hash) {
print_output("Passwords do not match. Try again.\n"); print_output("Re-enter passphrase: ");
goto retry_pass; 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, control->salt, SALT_LEN);
memcpy(control->salt_pass + SALT_LEN, passphrase, PASS_LEN - SALT_LEN); memcpy(control->salt_pass + SALT_LEN, passphrase, PASS_LEN - SALT_LEN);
lrz_stretch(control); lrz_stretch(control);
memset(passphrase, 0, PASS_LEN); memset(passphrase, 0, PASS_LEN);
munlock(passphrase, PASS_LEN); munlock(passphrase, PASS_LEN);
munlock(testphrase, PASS_LEN);
free(testphrase);
free(passphrase); free(passphrase);
} }

View file

@ -311,6 +311,8 @@ struct rzip_control {
int fd_hist; int fd_hist;
i64 encloops; i64 encloops;
i64 secs; i64 secs;
void (*pass_cb)(void *, char *, size_t); /* callback to get password in lib */
void *pass_data;
uchar salt[SALT_LEN]; uchar salt[SALT_LEN];
uchar *salt_pass; uchar *salt_pass;
int salt_pass_len; int salt_pass_len;