1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
12 #include "crypt-util.h"
13 #include "device-util.h"
17 #include "mount-util.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "string-util.h"
22 #include "terminal-util.h"
26 #define ANY_LUKS "LUKS"
27 /* as in src/cryptsetup.h */
28 #define CRYPT_SECTOR_SIZE 512
29 #define CRYPT_MAX_SECTOR_SIZE 4096
31 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
32 static char *arg_cipher
= NULL
;
33 static unsigned arg_key_size
= 0;
34 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
35 static unsigned arg_sector_size
= CRYPT_SECTOR_SIZE
;
37 static int arg_key_slot
= CRYPT_ANY_SLOT
;
38 static unsigned arg_keyfile_size
= 0;
39 static uint64_t arg_keyfile_offset
= 0;
40 static char *arg_hash
= NULL
;
41 static char *arg_header
= NULL
;
42 static unsigned arg_tries
= 3;
43 static bool arg_readonly
= false;
44 static bool arg_verify
= false;
45 static bool arg_discards
= false;
46 static bool arg_tcrypt_hidden
= false;
47 static bool arg_tcrypt_system
= false;
48 #ifdef CRYPT_TCRYPT_VERA_MODES
49 static bool arg_tcrypt_veracrypt
= false;
51 static char **arg_tcrypt_keyfiles
= NULL
;
52 static uint64_t arg_offset
= 0;
53 static uint64_t arg_skip
= 0;
54 static usec_t arg_timeout
= USEC_INFINITY
;
56 /* Options Debian's crypttab knows we don't:
66 static int parse_one_option(const char *option
) {
72 /* Handled outside of this tool */
73 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev"))
76 if ((val
= startswith(option
, "cipher="))) {
77 r
= free_and_strdup(&arg_cipher
, val
);
81 } else if ((val
= startswith(option
, "size="))) {
83 r
= safe_atou(val
, &arg_key_size
);
85 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
89 if (arg_key_size
% 8) {
90 log_error("size= not a multiple of 8, ignoring.");
96 } else if ((val
= startswith(option
, "sector-size="))) {
98 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
99 r
= safe_atou(val
, &arg_sector_size
);
101 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
105 if (arg_sector_size
% 2) {
106 log_error("sector-size= not a multiple of 2, ignoring.");
110 if (arg_sector_size
< CRYPT_SECTOR_SIZE
|| arg_sector_size
> CRYPT_MAX_SECTOR_SIZE
) {
111 log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE
, CRYPT_MAX_SECTOR_SIZE
);
115 log_error("sector-size= is not supported, compiled with old libcryptsetup.");
119 } else if ((val
= startswith(option
, "key-slot="))) {
122 r
= safe_atoi(val
, &arg_key_slot
);
124 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
128 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
130 arg_type
= CRYPT_TCRYPT
;
131 if (path_is_absolute(val
)) {
132 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
135 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
137 } else if ((val
= startswith(option
, "keyfile-size="))) {
139 r
= safe_atou(val
, &arg_keyfile_size
);
141 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
145 } else if ((val
= startswith(option
, "keyfile-offset="))) {
148 r
= safe_atou64(val
, &off
);
150 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
154 if ((size_t) off
!= off
) {
155 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
156 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off
);
160 arg_keyfile_offset
= off
;
162 } else if ((val
= startswith(option
, "hash="))) {
163 r
= free_and_strdup(&arg_hash
, val
);
167 } else if ((val
= startswith(option
, "header="))) {
170 if (!path_is_absolute(val
)) {
171 log_error("Header path \"%s\" is not absolute, refusing.", val
);
176 log_error("Duplicate header= option, refusing.");
180 arg_header
= strdup(val
);
184 } else if ((val
= startswith(option
, "tries="))) {
186 r
= safe_atou(val
, &arg_tries
);
188 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
192 } else if (STR_IN_SET(option
, "readonly", "read-only"))
194 else if (streq(option
, "verify"))
196 else if (STR_IN_SET(option
, "allow-discards", "discard"))
198 else if (streq(option
, "luks"))
200 else if (streq(option
, "tcrypt"))
201 arg_type
= CRYPT_TCRYPT
;
202 else if (streq(option
, "tcrypt-hidden")) {
203 arg_type
= CRYPT_TCRYPT
;
204 arg_tcrypt_hidden
= true;
205 } else if (streq(option
, "tcrypt-system")) {
206 arg_type
= CRYPT_TCRYPT
;
207 arg_tcrypt_system
= true;
208 } else if (streq(option
, "tcrypt-veracrypt")) {
209 #ifdef CRYPT_TCRYPT_VERA_MODES
210 arg_type
= CRYPT_TCRYPT
;
211 arg_tcrypt_veracrypt
= true;
213 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
216 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
217 arg_type
= CRYPT_PLAIN
;
218 else if ((val
= startswith(option
, "timeout="))) {
220 r
= parse_sec_fix_0(val
, &arg_timeout
);
222 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
226 } else if ((val
= startswith(option
, "offset="))) {
228 r
= safe_atou64(val
, &arg_offset
);
230 return log_error_errno(r
, "Failed to parse %s: %m", option
);
232 } else if ((val
= startswith(option
, "skip="))) {
234 r
= safe_atou64(val
, &arg_skip
);
236 return log_error_errno(r
, "Failed to parse %s: %m", option
);
238 } else if (!streq(option
, "none"))
239 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
244 static int parse_options(const char *options
) {
245 const char *word
, *state
;
251 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
252 _cleanup_free_
char *o
;
254 o
= strndup(word
, l
);
257 r
= parse_one_option(o
);
262 /* sanity-check options */
263 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
265 log_warning("offset= ignored with type %s", arg_type
);
267 log_warning("skip= ignored with type %s", arg_type
);
273 static char* disk_description(const char *path
) {
274 static const char name_fields
[] =
275 "ID_PART_ENTRY_NAME\0"
277 "ID_MODEL_FROM_DATABASE\0"
280 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
281 const char *i
, *name
;
286 if (stat(path
, &st
) < 0)
289 if (!S_ISBLK(st
.st_mode
))
292 if (sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
) < 0)
295 NULSTR_FOREACH(i
, name_fields
)
296 if (sd_device_get_property_value(device
, i
, &name
) >= 0 &&
303 static char *disk_mount_point(const char *label
) {
304 _cleanup_free_
char *device
= NULL
;
305 _cleanup_endmntent_
FILE *f
= NULL
;
308 /* Yeah, we don't support native systemd unit files here for now */
310 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
313 f
= setmntent("/etc/fstab", "re");
317 while ((m
= getmntent(f
)))
318 if (path_equal(m
->mnt_fsname
, device
))
319 return strdup(m
->mnt_dir
);
324 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
325 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
326 _cleanup_strv_free_erase_
char **passwords
= NULL
;
327 const char *name
= NULL
;
335 description
= disk_description(src
);
336 mount_point
= disk_mount_point(vol
);
338 disk_path
= cescape(src
);
342 if (description
&& streq(vol
, description
))
343 /* If the description string is simply the
344 * volume name, then let's not show this
346 description
= mfree(description
);
348 if (mount_point
&& description
)
349 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
350 else if (mount_point
)
351 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
352 else if (description
)
353 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
358 name
= name_buffer
? name_buffer
: vol
;
360 if (asprintf(&text
, "Please enter passphrase for disk %s!", name
) < 0)
363 id
= strjoina("cryptsetup:", disk_path
);
365 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
366 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
369 return log_error_errno(r
, "Failed to query password: %m");
372 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
374 assert(strv_length(passwords
) == 1);
376 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
379 id
= strjoina("cryptsetup-verification:", disk_path
);
381 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
383 return log_error_errno(r
, "Failed to query verification password: %m");
385 assert(strv_length(passwords2
) == 1);
387 if (!streq(passwords
[0], passwords2
[0])) {
388 log_warning("Passwords did not match, retrying.");
393 strv_uniq(passwords
);
395 STRV_FOREACH(p
, passwords
) {
398 if (strlen(*p
)+1 >= arg_key_size
)
401 /* Pad password if necessary */
402 c
= new(char, arg_key_size
);
406 strncpy(c
, *p
, arg_key_size
);
411 *ret
= TAKE_PTR(passwords
);
416 static int attach_tcrypt(
417 struct crypt_device
*cd
,
419 const char *key_file
,
424 _cleanup_free_
char *passphrase
= NULL
;
425 struct crypt_params_tcrypt params
= {
426 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
427 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
428 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
433 assert(key_file
|| (passwords
&& passwords
[0]));
435 if (arg_tcrypt_hidden
)
436 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
438 if (arg_tcrypt_system
)
439 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
441 #ifdef CRYPT_TCRYPT_VERA_MODES
442 if (arg_tcrypt_veracrypt
)
443 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
447 r
= read_one_line_file(key_file
, &passphrase
);
449 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
453 params
.passphrase
= passphrase
;
455 params
.passphrase
= passwords
[0];
456 params
.passphrase_size
= strlen(params
.passphrase
);
458 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
460 if (key_file
&& r
== -EPERM
) {
461 log_error("Failed to activate using password file '%s'.", key_file
);
467 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
470 static int attach_luks_or_plain(struct crypt_device
*cd
,
472 const char *key_file
,
473 const char *data_device
,
477 bool pass_volume_key
= false;
481 assert(key_file
|| passwords
);
483 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
484 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
486 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
491 r
= crypt_set_data_device(cd
, data_device
);
494 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
495 struct crypt_params_plain params
= {
496 .offset
= arg_offset
,
498 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
499 .sector_size
= arg_sector_size
,
502 const char *cipher
, *cipher_mode
;
503 _cleanup_free_
char *truncated_cipher
= NULL
;
506 /* plain isn't a real hash type. it just means "use no hash" */
507 if (!streq(arg_hash
, "plain"))
508 params
.hash
= arg_hash
;
509 } else if (!key_file
)
510 /* for CRYPT_PLAIN, the behaviour of cryptsetup
511 * package is to not hash when a key file is provided */
512 params
.hash
= "ripemd160";
517 l
= strcspn(arg_cipher
, "-");
518 truncated_cipher
= strndup(arg_cipher
, l
);
519 if (!truncated_cipher
)
522 cipher
= truncated_cipher
;
523 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
526 cipher_mode
= "cbc-essiv:sha256";
529 /* for CRYPT_PLAIN limit reads
530 * from keyfile to key length, and
531 * ignore keyfile-size */
532 arg_keyfile_size
= arg_key_size
;
534 /* In contrast to what the name
535 * crypt_setup() might suggest this
536 * doesn't actually format anything,
537 * it just configures encryption
538 * parameters when used for plain
540 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
542 /* hash == NULL implies the user passed "plain" */
543 pass_volume_key
= (params
.hash
== NULL
);
547 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
549 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
550 crypt_get_cipher(cd
),
551 crypt_get_cipher_mode(cd
),
552 crypt_get_volume_key_size(cd
)*8,
553 crypt_get_device_name(cd
));
556 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
558 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
564 STRV_FOREACH(p
, passwords
) {
566 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
568 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
578 static int help(void) {
579 _cleanup_free_
char *link
= NULL
;
582 r
= terminal_urlify_man("systemd-cryptsetup@.service", "8", &link
);
586 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
587 "%s detach VOLUME\n\n"
588 "Attaches or detaches an encrypted block device.\n"
589 "\nSee the %s for details.\n"
590 , program_invocation_short_name
591 , program_invocation_short_name
598 int main(int argc
, char *argv
[]) {
599 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
608 log_error("This program requires at least two arguments.");
612 log_set_target(LOG_TARGET_AUTO
);
613 log_parse_environment();
618 if (streq(argv
[1], "attach")) {
622 crypt_status_info status
;
623 const char *key_file
= NULL
;
625 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
628 log_error("attach requires at least two arguments.");
634 !streq(argv
[4], "-") &&
635 !streq(argv
[4], "none")) {
637 if (!path_is_absolute(argv
[4]))
638 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
643 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
644 if (parse_options(argv
[5]) < 0)
648 /* A delicious drop of snake oil */
649 mlockall(MCL_FUTURE
);
652 log_debug("LUKS header: %s", arg_header
);
653 r
= crypt_init(&cd
, arg_header
);
655 r
= crypt_init(&cd
, argv
[3]);
657 log_error_errno(r
, "crypt_init() failed: %m");
661 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
663 status
= crypt_status(cd
, argv
[2]);
664 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
665 log_info("Volume %s already active.", argv
[2]);
671 flags
|= CRYPT_ACTIVATE_READONLY
;
674 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
676 if (arg_timeout
== USEC_INFINITY
)
679 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
681 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
686 /* Ideally we'd do this on the open fd, but since this is just a
687 * warning it's OK to do this in two steps. */
688 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
689 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
692 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
693 _cleanup_strv_free_erase_
char **passwords
= NULL
;
696 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
703 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
704 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
706 r
= attach_luks_or_plain(cd
,
709 arg_header
? argv
[3] : NULL
,
719 log_error_errno(r
, "Failed to activate: %m");
723 log_warning("Invalid passphrase.");
726 if (arg_tries
!= 0 && tries
>= arg_tries
) {
727 log_error("Too many attempts; giving up.");
732 } else if (streq(argv
[1], "detach")) {
734 r
= crypt_init_by_name(&cd
, argv
[2]);
736 log_info("Volume %s already inactive.", argv
[2]);
741 log_error_errno(r
, "crypt_init_by_name() failed: %m");
745 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
747 r
= crypt_deactivate(cd
, argv
[2]);
749 log_error_errno(r
, "Failed to deactivate: %m");
754 log_error("Unknown verb %s.", argv
[1]);
764 strv_free(arg_tcrypt_keyfiles
);
766 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;