Try to make get_key() paramater more obvious...

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@236 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
Milan Broz
2010-05-27 18:43:47 +00:00
parent 4635d7bf31
commit 205d62af89
2 changed files with 18 additions and 26 deletions

View File

@@ -380,7 +380,7 @@ out_err:
/* /*
* Password reading behaviour matrix of get_key * Password reading behaviour matrix of get_key
* * FIXME: rewrite this from scratch.
* p v n h * p v n h
* -----------------+---+---+---+--- * -----------------+---+---+---+---
* interactive | Y | Y | Y | Inf * interactive | Y | Y | Y | Inf
@@ -400,31 +400,23 @@ void get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
const int verify = how2verify & CRYPT_FLAG_VERIFY; const int verify = how2verify & CRYPT_FLAG_VERIFY;
const int verify_if_possible = how2verify & CRYPT_FLAG_VERIFY_IF_POSSIBLE; const int verify_if_possible = how2verify & CRYPT_FLAG_VERIFY_IF_POSSIBLE;
char *pass = NULL; char *pass = NULL;
int newline_stop;
int read_horizon; int read_horizon;
int regular_file = 0; int regular_file = 0;
int read_stdin;
int r; int r;
struct stat st;
if(key_file && !strcmp(key_file, "-")) { /* Passphrase read from stdin? */
/* Allow binary reading from stdin */ read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
fd = STDIN_FILENO;
newline_stop = 0;
read_horizon = 0;
} else if (key_file) {
fd = open(key_file, O_RDONLY);
if (fd < 0) {
log_err(cd, _("Failed to open key file %s.\n"), key_file);
goto out_err;
}
newline_stop = 0;
/* This can either be 0 (LUKS) or the actually number /* read_horizon applies only for real keyfile, not stdin or terminal */
* of key bytes (default or passed by -s) */ read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
read_horizon = key_size;
} else { /* Setup file descriptior */
fd = STDIN_FILENO; fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
newline_stop = 1; if (fd < 0) {
read_horizon = 0; /* Infinite, if read from terminal or fd */ log_err(cd, _("Failed to open key file %s.\n"), key_file ?: "-");
goto out_err;
} }
/* Interactive case */ /* Interactive case */
@@ -464,9 +456,8 @@ void get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
* should warn the user, if it's a non-regular file, * should warn the user, if it's a non-regular file,
* such as /dev/random, because in this case, the loop * such as /dev/random, because in this case, the loop
* will read forever. * will read forever.
*/ */
if(key_file && strcmp(key_file, "-") && read_horizon == 0) { if(!read_stdin && read_horizon == 0) {
struct stat st;
if(stat(key_file, &st) < 0) { if(stat(key_file, &st) < 0) {
log_err(cd, _("Failed to stat key file %s.\n"), key_file); log_err(cd, _("Failed to stat key file %s.\n"), key_file);
goto out_err; goto out_err;
@@ -495,7 +486,8 @@ void get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
goto out_err; goto out_err;
} }
if(r == 0 || (newline_stop && pass[i] == '\n')) /* Stop on newline only if not requested read from keyfile */
if(r == 0 || (!key_file && pass[i] == '\n'))
break; break;
} }
/* Fail if piped input dies reading nothing */ /* Fail if piped input dies reading nothing */

View File

@@ -204,7 +204,7 @@ in the mkfs.xfs manual page. By default, the payload is aligned at an 8 sector (
Show the version. Show the version.
.SH NOTES ON PASSWORD PROCESSING .SH NOTES ON PASSWORD PROCESSING
\fIFrom a file descriptor or a terminal\fR: Password processing is new-line sensitive, meaning the reading will stop after encountering \\n. It will process the read material (without newline) with the default hash or the hash given by \-\-hash. After hashing, it will be cropped to the key size given by \-s. \fIFrom a terminal\fR: Password processing is new-line sensitive, meaning the reading will stop after encountering \\n. It will process the read material (without newline) with the default hash or the hash given by \-\-hash. After hashing, it will be cropped to the key size given by \-s.
\fIFrom stdin\fR: Reading will continue until EOF (so using e.g. /dev/random as stdin will not work), with the trailing newline stripped. After that the read data will be hashed with the default hash or the hash given by \-\-hash and the result will be cropped to the keysize given by \-s. If "plain" is used as an argument to the hash option, the input data will not be hashed. \fIFrom stdin\fR: Reading will continue until EOF (so using e.g. /dev/random as stdin will not work), with the trailing newline stripped. After that the read data will be hashed with the default hash or the hash given by \-\-hash and the result will be cropped to the keysize given by \-s. If "plain" is used as an argument to the hash option, the input data will not be hashed.
Instead, it will be zero padded (if shorter than the keysize) or truncated (if longer than the keysize) and used directly as the key. No warning will be given if the amount of data read from stdin is less than the keysize. Instead, it will be zero padded (if shorter than the keysize) or truncated (if longer than the keysize) and used directly as the key. No warning will be given if the amount of data read from stdin is less than the keysize.