From 49937ac5912300869a4deff0dfc4d178bb2345ef Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Tue, 25 Jan 2011 18:48:48 +0000 Subject: [PATCH] Detect # of keys from cipher string. Fix status output string. git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@417 36d66b0a-2a48-0410-832c-cd162a569da5 --- lib/setup.c | 31 ++++++++++++++++++------------- lib/utils_crypt.c | 14 ++++++++++++-- lib/utils_crypt.h | 3 ++- src/cryptsetup.c | 8 +++++--- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/setup.c b/lib/setup.c index a478986..94e8941 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -722,7 +722,7 @@ int crypt_luksFormat(struct crypt_options *options) }; int r; - r = crypt_parse_name_and_mode(options->cipher, cipherName, cipherMode); + r = crypt_parse_name_and_mode(options->cipher, cipherName, NULL, cipherMode); if(r < 0) { log_err(cd, _("No known cipher specification pattern detected.\n")); return r; @@ -1035,7 +1035,7 @@ int crypt_init_by_name(struct crypt_device **cd, const char *name) goto out; } - r = crypt_parse_name_and_mode(cipher_full, cipher, cipher_mode); + r = crypt_parse_name_and_mode(cipher_full, cipher, NULL, cipher_mode); if (!r) { (*cd)->plain_cipher = strdup(cipher); (*cd)->plain_cipher_mode = strdup(cipher_mode); @@ -1179,11 +1179,11 @@ int crypt_format(struct crypt_device *cd, { int r; - log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", cd->type ?: "(none)"); - if (!type) return -EINVAL; + log_dbg("Formatting device %s as type %s.", cd->device ?: "(none)", type); + r = init_crypto(cd); if (r < 0) return r; @@ -1834,12 +1834,11 @@ int crypt_activate_by_keyfile(struct crypt_device *cd, if (!keyfile) return -EINVAL; - r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read, - &passphrase_size_read, keyfile, keyfile_size); - if (r < 0) - goto out; - if (isPLAIN(cd->type)) { + r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read, + &passphrase_size_read, keyfile, keyfile_size); + if (r < 0) + goto out; r = create_device_helper(cd, name, cd->plain_hdr.hash, cd->plain_cipher, cd->plain_cipher_mode, NULL, passphrase_read, passphrase_size_read, @@ -1847,17 +1846,23 @@ int crypt_activate_by_keyfile(struct crypt_device *cd, cd->plain_hdr.skip, cd->plain_hdr.offset, cd->plain_uuid, flags & CRYPT_ACTIVATE_READONLY, 0, 0); - keyslot = 0; } else if (isLUKS(cd->type)) { + r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read, + &passphrase_size_read, keyfile, keyfile_size); + if (r < 0) + goto out; r = LUKS_open_key_with_hdr(cd->device, keyslot, passphrase_read, passphrase_size_read, &cd->hdr, &vk, cd); if (r < 0) goto out; - keyslot = r; - if (name) + if (name) { r = open_from_hdr_and_vk(cd, vk, name, flags); + if (r < 0) + goto out; + } + r = keyslot; } else r = -EINVAL; @@ -1865,7 +1870,7 @@ out: crypt_safe_free(passphrase_read); crypt_free_volume_key(vk); - return r < 0 ? r : keyslot; + return r; } int crypt_activate_by_volume_key(struct crypt_device *cd, diff --git a/lib/utils_crypt.c b/lib/utils_crypt.c index 6ad4196..71afdd0 100644 --- a/lib/utils_crypt.c +++ b/lib/utils_crypt.c @@ -18,17 +18,27 @@ struct safe_allocation { char data[0]; }; -int crypt_parse_name_and_mode(const char *s, char *cipher, char *cipher_mode) +int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums, + char *cipher_mode) { if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s", cipher, cipher_mode) == 2) { if (!strcmp(cipher_mode, "plain")) strncpy(cipher_mode, "cbc-plain", 10); + if (key_nums) { + char *tmp = strchr(cipher, ':'); + *key_nums = tmp ? atoi(++tmp) : 1; + if (!*key_nums) + return -EINVAL; + } + return 0; } if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) { strncpy(cipher_mode, "cbc-plain", 10); + if (key_nums) + *key_nums = 1; return 0; } @@ -143,7 +153,7 @@ static int interactive_pass(const char *prompt, char *pass, size_t maxlen, memcpy(&tmp, &orig, sizeof(tmp)); tmp.c_lflag &= ~ECHO; - if (write(outfd, prompt, strlen(prompt)) < 0) + if (prompt && write(outfd, prompt, strlen(prompt)) < 0) goto out_err; tcsetattr(infd, TCSAFLUSH, &tmp); diff --git a/lib/utils_crypt.h b/lib/utils_crypt.h index f7a396a..35bbe4e 100644 --- a/lib/utils_crypt.h +++ b/lib/utils_crypt.h @@ -8,7 +8,8 @@ struct crypt_device; -int crypt_parse_name_and_mode(const char *s, char *cipher, char *cipher_mode); +int crypt_parse_name_and_mode(const char *s, char *cipher, + int *key_nums, char *cipher_mode); int crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size, const char *key_file, int timeout, int how2verify, diff --git a/src/cryptsetup.c b/src/cryptsetup.c index e83b00b..36394b4 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -209,7 +209,7 @@ static int action_create(int arg) params.hash = NULL; r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(PLAIN), - cipher, cipher_mode); + cipher, NULL, cipher_mode); if (r < 0) { log_err("No known cipher specification pattern detected.\n"); goto out; @@ -302,7 +302,7 @@ static int action_status(int arg) if (r < 0 || !crypt_get_type(cd)) goto out; - log_std(" type: %s\n", crypt_get_type(cd)); + log_std(" type: %s\n", crypt_get_type(cd)); r = crypt_get_active_device(cd, action_argv[0], &cad); if (r < 0) @@ -372,7 +372,7 @@ static int action_luksFormat(int arg) goto out; r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(LUKS1), - cipher, cipher_mode); + cipher, NULL, cipher_mode); if (r < 0) { log_err("No known cipher specification pattern detected.\n"); goto out; @@ -875,6 +875,8 @@ static int run_action(struct action_type *action) { int r; + log_dbg("Running command %s.", action->type); + if (action->required_memlock) crypt_memory_lock(NULL, 1); -- 2.7.4