HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/cryptsetup/cryptsetup.c
Git blob: 425fc7be32b5b7f2d380a78cfecc8640727d6b68
Lines 2001–2795 of 2,795
/* EAGAIN means: no tpm2 chip found */
if (r != -EAGAIN) {
log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
}
} else {
r = attach_luks2_by_tpm2_via_plugin(cd, name, until, flags);
if (r >= 0)
return 0;
/* EAGAIN means: no tpm2 chip found
* EOPNOTSUPP means: no libcryptsetup plugins support */
if (r == -ENXIO)
return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
"No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
if (r == -ENOENT)
return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
"No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) {
log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
}
}
if (r == -EOPNOTSUPP) { /* Plugin not available, let's process TPM2 stuff right here instead */
bool found_some = false;
int token = 0; /* first token to look at */
/* If no key data is specified, look for it in the header. In order to support
* software upgrades we'll iterate through all suitable tokens, maybe one of them
* works. */
for (;;) {
_cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {};
_cleanup_free_ char *pubkey_policy_ref = NULL;
struct iovec *blobs = NULL, *policy_hash = NULL;
uint32_t hash_pcr_mask, pubkey_pcr_mask;
size_t n_blobs = 0, n_policy_hash = 0;
uint16_t pcr_bank, primary_alg;
Argon2IdParameters argon2id_params = {};
TPM2Flags tpm2_flags;
CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free);
CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free);
r = find_tpm2_auto_data(
cd,
arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
token, /* search for the token with this index, or any later index than this */
&hash_pcr_mask,
&pcr_bank,
&pubkey,
&pubkey_policy_ref,
&pubkey_pcr_mask,
&primary_alg,
&blobs,
&n_blobs,
&policy_hash,
&n_policy_hash,
&salt,
&srk,
&pcrlock_nv,
&tpm2_flags,
&keyslot,
&token,
&argon2id_params);
if (r == -ENXIO)
/* No further TPM2 tokens found in the LUKS2 header. */
return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG,
SYNTHETIC_ERRNO(EAGAIN),
found_some
? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
: "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
/* TPM2 support not compiled in? */
return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
"TPM2 support not available, falling back to traditional unlocking.");
if (r < 0)
return r;
found_some = true;
r = acquire_tpm2_key(
name,
arg_tpm2_device,
hash_pcr_mask,
pcr_bank,
&pubkey,
pubkey_policy_ref,
pubkey_pcr_mask,
arg_tpm2_signature,
arg_tpm2_pcrlock,
primary_alg,
/* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
blobs,
n_blobs,
policy_hash,
n_policy_hash,
&salt,
&srk,
&pcrlock_nv,
tpm2_flags,
until,
"cryptsetup.tpm2-pin",
arg_ask_password_flags,
&argon2id_params,
&decrypted_key);
if (IN_SET(r, -EACCES, -ENOLCK))
return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
/* Stop unless we should keep iterating to next token because the tried one
* does not match boot state. For now without -EUCLEAN because currently the
* only error it reports won't be solved by moving to another token. */
if (!ERRNO_IS_NEG_TPM2_TOKEN_MISMATCH(r))
break;
token++; /* try a different token next time */
}
if (r >= 0)
break;
/* EAGAIN means: no tpm2 chip found */
if (r != -EAGAIN) {
log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");
return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
}
}
if (!monitor) {
/* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create
* an event loop and monitor first. */
assert(!event);
if (is_efi_boot() && !efi_has_tpm2())
return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
"No TPM2 hardware discovered and EFI firmware does not see it either, falling back to traditional unlocking.");
r = make_tpm2_device_monitor(&event, &monitor);
if (r < 0)
return r;
log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly);
/* Let's immediately rescan in case the device appeared in the time we needed
* to create and configure the monitor */
continue;
}
r = run_security_device_monitor(event, monitor);
if (r < 0)
return r;
log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
}
if (pass_volume_key)
r = measured_crypt_activate_by_volume_key(
cd,
name,
"tpm2",
/* keyslot= */ -1,
decrypted_key.iov_base,
decrypted_key.iov_len,
flags);
else {
_cleanup_(erase_and_freep) char *base64_encoded = NULL;
ssize_t base64_encoded_size;
/* Before using this key as passphrase we base64 encode it, for compat with homed */
base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
if (base64_encoded_size < 0)
return log_oom();
r = measured_crypt_activate_by_passphrase(
cd,
name,
"tpm2",
keyslot,
base64_encoded,
base64_encoded_size,
flags);
}
if (r == -EPERM) {
log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
return -EAGAIN; /* log actual error, but return EAGAIN */
}
if (r < 0)
return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
return 0;
}
static int attach_luks_or_plain_or_bitlk_by_key_data(
struct crypt_device *cd,
const char *name,
const struct iovec *key_data,
uint32_t flags,
bool pass_volume_key) {
int r;
assert(cd);
assert(name);
assert(key_data);
if (pass_volume_key)
r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, key_data->iov_base, key_data->iov_len, flags);
else
r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, key_data->iov_base, key_data->iov_len, flags);
if (r == -EPERM) {
log_error_errno(r, "Failed to activate. (Key incorrect?)");
return -EAGAIN; /* Log actual error, but return EAGAIN */
}
if (r < 0)
return log_error_errno(r, "Failed to activate: %m");
return 0;
}
static int attach_luks_or_plain_or_bitlk_by_key_file(
struct crypt_device *cd,
const char *name,
const char *key_file,
uint32_t flags,
bool pass_volume_key) {
_cleanup_(erase_and_freep) char *kfdata = NULL;
_cleanup_free_ char *bindname = NULL;
size_t kfsize;
int r;
assert(cd);
assert(name);
assert(key_file);
/* If we read the key via AF_UNIX, make this client recognizable */
bindname = make_bindname(name, /* token_type= */ _TOKEN_TYPE_INVALID);
if (!bindname)
return log_oom();
r = read_full_file_full(
AT_FDCWD, key_file,
arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
bindname,
&kfdata, &kfsize);
if (r == -E2BIG) {
log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
return -EAGAIN;
}
if (r == -ENOENT) {
log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
return -EAGAIN; /* Log actual error, but return EAGAIN */
}
if (r < 0)
return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
if (pass_volume_key)
r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, kfdata, kfsize, flags);
else
r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, kfdata, kfsize, flags);
if (r == -EPERM) {
log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
return -EAGAIN; /* Log actual error, but return EAGAIN */
}
if (r < 0)
return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
return 0;
}
static int attach_luks_or_plain_or_bitlk_by_passphrase(
struct crypt_device *cd,
const char *name,
char **passwords,
uint32_t flags,
bool pass_volume_key) {
int r;
assert(cd);
assert(name);
r = -EINVAL;
STRV_FOREACH(p, passwords) {
if (pass_volume_key)
r = measured_crypt_activate_by_volume_key(cd, name, /* mechanism= */ NULL, /* keyslot= */ -1, *p, arg_key_size, flags);
else
r = measured_crypt_activate_by_passphrase(cd, name, /* mechanism= */ NULL, arg_key_slot, *p, strlen(*p), flags);
if (r >= 0)
break;
}
if (r == -EPERM) {
log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
return -EAGAIN; /* log actual error, but return EAGAIN */
}
if (r < 0)
return log_error_errno(r, "Failed to activate with specified passphrase: %m");
return 0;
}
static int attach_luks_or_plain_or_bitlk(
struct crypt_device *cd,
const char *name,
TokenType token_type,
const char *key_file,
const struct iovec *key_data,
char **passwords,
uint32_t flags,
usec_t until) {
bool pass_volume_key = false;
int r;
assert(cd);
assert(name);
if ((!arg_type && !sym_crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
struct crypt_params_plain params = {
.offset = arg_offset,
.skip = arg_skip,
.sector_size = arg_sector_size,
};
const char *cipher, *cipher_mode;
_cleanup_free_ char *truncated_cipher = NULL;
if (streq_ptr(arg_hash, "plain"))
/* plain isn't a real hash type. it just means "use no hash" */
params.hash = NULL;
else if (arg_hash)
params.hash = arg_hash;
else if (!key_file)
/* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
* file is provided */
params.hash = "ripemd160";
if (arg_cipher) {
size_t l;
l = strcspn(arg_cipher, "-");
truncated_cipher = strndup(arg_cipher, l);
if (!truncated_cipher)
return log_oom();
cipher = truncated_cipher;
cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
} else {
cipher = "aes";
cipher_mode = "cbc-essiv:sha256";
}
/* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
arg_keyfile_size = arg_key_size;
/* In contrast to what the name crypt_format() might suggest this doesn't actually format
* anything, it just configures encryption parameters when used for plain mode. */
r = sym_crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, ¶ms);
if (r < 0)
return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
/* hash == NULL implies the user passed "plain" */
pass_volume_key = !params.hash;
}
log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
sym_crypt_get_cipher(cd),
sym_crypt_get_cipher_mode(cd),
sym_crypt_get_volume_key_size(cd)*8,
sym_crypt_get_device_name(cd));
if (token_type == TOKEN_TPM2)
return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, until, flags, pass_volume_key);
if (token_type == TOKEN_FIDO2)
return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, until, flags, pass_volume_key);
if (token_type == TOKEN_PKCS11)
return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, until, flags, pass_volume_key);
if (key_data)
return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, flags, pass_volume_key);
if (key_file)
return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
}
VERB_COMMON_HELP_AUTO_HIDDEN();
static int parse_argv(int argc, char *argv[], char ***ret_args) {
assert(argc >= 0);
assert(argv);
assert(ret_args);
OptionParser opts = { argc, argv };
FOREACH_OPTION_OR_RETURN(c, &opts)
switch (c) {
OPTION_COMMON_HELP:
return command_print_help();
OPTION_COMMON_VERSION:
return version();
OPTION_COMMON_INTROSPECT_CLI:
return introspect_cli(SD_JSON_FORMAT_OFF);
}
*ret_args = option_parser_get_args(&opts);
return 1;
}
static uint32_t determine_flags(void) {
uint32_t flags = 0;
if (arg_readonly)
flags |= CRYPT_ACTIVATE_READONLY;
if (arg_discards)
flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
if (arg_same_cpu_crypt)
flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
if (arg_submit_from_crypt_cpus)
flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
if (arg_no_read_workqueue)
flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
if (arg_no_write_workqueue)
flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
/* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
/* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
return flags;
}
static void remove_and_erasep(const char **p) {
int r;
assert(p);
if (!*p)
return;
r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
if (r < 0 && r != -ENOENT)
log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
}
static TokenType determine_token_type(void) {
if (arg_tpm2_device || arg_tpm2_device_auto)
return TOKEN_TPM2;
if (arg_fido2_device || arg_fido2_device_auto)
return TOKEN_FIDO2;
if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
return TOKEN_PKCS11;
return _TOKEN_TYPE_INVALID;
}
static int discover_key(const char *key_file, const char *volume, TokenType token_type, struct iovec *ret_key_data) {
_cleanup_free_ char *bindname = NULL;
const char *token_type_name;
int r;
assert(key_file);
assert(volume);
assert(ret_key_data);
bindname = make_bindname(volume, token_type);
if (!bindname)
return log_oom();
/* If a key file is not explicitly specified, search for a key in a well defined search path, and load it. */
r = find_key_file(key_file, STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"), bindname, ret_key_data);
if (r <= 0)
return r;
token_type_name = token_type_to_string(token_type);
if (token_type_name)
log_debug("Automatically discovered encrypted key for volume '%s' (token type: %s).", volume, token_type_name);
else
log_debug("Automatically discovered key for volume '%s'.", volume);
return r;
}
VERB(verb_attach, "attach", "VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\0", 3, 5, 0,
"Attach an encrypted block device");
static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
_unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
crypt_status_info status;
uint32_t flags = 0;
unsigned tries;
usec_t until;
PassphraseType passphrase_type = PASSPHRASE_NONE;
int r;
/* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG] */
assert(argc >= 3 && argc <= 5);
const char *volume = ASSERT_PTR(argv[1]),
*source = ASSERT_PTR(argv[2]),
*key_file = argc >= 4 ? mangle_none(argv[3]) : NULL,
*config = argc >= 5 ? mangle_none(argv[4]) : NULL;
if (!filename_is_valid(volume))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
if (key_file && !path_is_absolute(key_file)) {
log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
key_file = NULL;
}
if (config) {
r = parse_crypt_config(config);
if (r < 0)
return r;
}
log_debug("%s %s ← %s type=%s cipher=%s", __func__,
volume, source, strempty(arg_type), strempty(arg_cipher));
/* A delicious drop of snake oil */
(void) safe_mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
/* Only erase key files explicitly configured on the command line, never the ones we
* auto-discover in /etc/cryptsetup-keys.d/ and /run/cryptsetup-keys.d/: those are shared
* resources not owned by an individual volume. (key_file is NULL when auto-discovery is used.) */
if (key_file && arg_keyfile_erase)
destroy_key_file = key_file; /* let's get this baby erased when we leave */
if (arg_header) {
if (streq_ptr(arg_type, CRYPT_TCRYPT)){
log_debug("tcrypt header: %s", arg_header);
r = sym_crypt_init_data_device(&cd, arg_header, source);
} else {
log_debug("LUKS header: %s", arg_header);
r = sym_crypt_init(&cd, arg_header);
}
} else
r = sym_crypt_init(&cd, source);
if (r < 0)
return log_error_errno(r, "crypt_init() failed: %m");
cryptsetup_enable_logging(cd);
status = sym_crypt_status(cd, volume);
if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
log_info("Volume %s already active.", volume);
return 0;
}
flags = determine_flags();
until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
if (until == USEC_INFINITY)
until = 0;
if (arg_key_size == 0)
arg_key_size = 256U / 8U;
if (key_file) {
struct stat st;
/* Ideally we'd do this on the open fd, but since this is just a warning it's OK to do this
* in two steps. */
if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
}
if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
r = sym_crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
if (r < 0)
return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", sym_crypt_get_device_name(cd));
/* since cryptsetup 2.7.0 (Jan 2024) */
if (arg_link_key_description) {
r = sym_crypt_set_keyring_to_link(cd, arg_link_key_description, NULL, arg_link_key_type, arg_link_keyring);
if (r == -ENOSYS)
log_warning("Loaded libcryptsetup does not support linking volume keys in user specified kernel keyrings upon device activation, ignoring.");
else if (r < 0)
log_warning_errno(r, "Failed to set keyring or key description to link volume key in, ignoring: %m");
}
if (arg_header) {
r = sym_crypt_set_data_device(cd, source);
if (r < 0)
return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
}
/* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
if (!key_file && use_token_plugins()) {
r = crypt_activate_by_token_pin_ask_password(
cd,
volume,
/* type= */ NULL,
until,
/* userdata= */ NULL,
flags,
"Please enter LUKS2 token PIN:",
"luks2-pin",
"cryptsetup.luks2-pin");
if (r >= 0) {
log_debug("Volume %s activated with a LUKS token.", volume);
return 0;
}
log_debug_errno(r, "Token activation unsuccessful for device %s: %m", sym_crypt_get_device_name(cd));
}
}
if (streq_ptr(arg_type, CRYPT_BITLK)) {
r = sym_crypt_load(cd, CRYPT_BITLK, NULL);
if (r < 0)
return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", sym_crypt_get_device_name(cd));
}
bool use_cached_passphrase = true, try_discover_key = !key_file;
const char *discovered_key_fn = strjoina(volume, ".key");
_cleanup_strv_free_erase_ char **passwords = NULL;
for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
_cleanup_(iovec_done_erase) struct iovec discovered_key_data = {};
const struct iovec *key_data = NULL;
TokenType token_type = determine_token_type();
log_debug("Beginning attempt %u to unlock.", tries);
/* When we were able to acquire multiple keys, let's always process them in this order:
*
* 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
* 2. The configured or discovered key, of which both are exclusive and optional
* 3. The empty password, in case arg_try_empty_password is set
* 4. We enquire the user for a password
*/
if (try_discover_key) {
r = discover_key(discovered_key_fn, volume, token_type, &discovered_key_data);
if (r < 0)
return r;
if (r > 0)
key_data = &discovered_key_data;
else
try_discover_key = false;
}
if (token_type < 0 && !key_file && !key_data && !passwords) {
/* If we have nothing to try anymore, then acquire a new password */
if (arg_try_empty_password) {
/* Hmm, let's try an empty password now, but only once */
arg_try_empty_password = false;
key_data = &iovec_empty;
} else {
/* Ask the user for a passphrase or recovery key only as last resort, if we
* have nothing else to check for */
if (passphrase_type == PASSPHRASE_NONE) {
passphrase_type = check_registered_passwords(cd);
if (passphrase_type == PASSPHRASE_NONE)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered.");
}
r = get_password(
volume,
source,
until,
/* ignore_cached= */ !use_cached_passphrase || arg_verify,
passphrase_type,
&passwords);
use_cached_passphrase = false;
if (r == -EAGAIN)
continue;
if (r < 0)
return r;
}
}
if (streq_ptr(arg_type, CRYPT_TCRYPT))
r = attach_tcrypt(cd, volume, token_type, key_file, key_data, passwords, flags);
else
r = attach_luks_or_plain_or_bitlk(cd, volume, token_type, key_file, key_data, passwords, flags, until);
if (r >= 0)
break;
if (r != -EAGAIN)
return r;
/* Key not correct? Let's try again, but let's invalidate one of the passed fields, so that
* we fall back to the next best thing. */
if (token_type == TOKEN_TPM2) {
arg_tpm2_device = mfree(arg_tpm2_device);
arg_tpm2_device_auto = false;
continue;
}
if (token_type == TOKEN_FIDO2) {
arg_fido2_device = mfree(arg_fido2_device);
arg_fido2_device_auto = false;
continue;
}
if (token_type == TOKEN_PKCS11) {
arg_pkcs11_uri = mfree(arg_pkcs11_uri);
arg_pkcs11_uri_auto = false;
continue;
}
if (try_discover_key) {
try_discover_key = false;
continue;
}
if (key_file) {
key_file = NULL;
continue;
}
if (passwords) {
passwords = strv_free_erase(passwords);
continue;
}
log_debug("Prepared for next attempt to unlock.");
}
if (arg_tries != 0 && tries >= arg_tries)
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
return 0;
}
VERB(verb_detach, "detach", "VOLUME\0", 2, 2, 0,
"Detach an encrypted block device");
static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
const char *volume = ASSERT_PTR(argv[1]);
int r;
assert(argc == 2);
if (!filename_is_valid(volume))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
r = sym_crypt_init_by_name(&cd, volume);
if (r == -ENODEV) {
log_info("Volume %s already inactive.", volume);
return 0;
}
if (r < 0)
return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
cryptsetup_enable_logging(cd);
r = sym_crypt_deactivate(cd, volume);
if (r < 0)
return log_error_errno(r, "Failed to deactivate '%s': %m", volume);
return 0;
}
static int run(int argc, char *argv[]) {
int r;
LIBCRYPTO_NOTE(recommended);
LIBCRYPTSETUP_NOTE(required);
LIBFIDO2_NOTE(suggested);
LIBMOUNT_NOTE(recommended);
LIBP11KIT_NOTE(suggested);
TPM2_NOTE(suggested);
log_setup();
umask(0022);
char **args = NULL;
r = parse_argv(argc, argv, &args);
if (r <= 0)
return r;
r = dlopen_cryptsetup(LOG_ERR);
if (r < 0)
return r;
return dispatch_verb(args, NULL);
}
DEFINE_MAIN_FUNCTION(run);