1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
11 #include "sd-device.h"
13 #include "alloc-util.h"
14 #include "ask-password-api.h"
15 #include "crypt-util.h"
16 #include "device-util.h"
20 #include "mount-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "string-util.h"
28 #define ANY_LUKS "LUKS"
30 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
31 static char *arg_cipher
= NULL
;
32 static unsigned arg_key_size
= 0;
33 static int arg_key_slot
= CRYPT_ANY_SLOT
;
34 static unsigned arg_keyfile_size
= 0;
35 static uint64_t arg_keyfile_offset
= 0;
36 static char *arg_hash
= NULL
;
37 static char *arg_header
= NULL
;
38 static unsigned arg_tries
= 3;
39 static bool arg_readonly
= false;
40 static bool arg_verify
= false;
41 static bool arg_discards
= false;
42 static bool arg_tcrypt_hidden
= false;
43 static bool arg_tcrypt_system
= false;
44 #ifdef CRYPT_TCRYPT_VERA_MODES
45 static bool arg_tcrypt_veracrypt
= false;
47 static char **arg_tcrypt_keyfiles
= NULL
;
48 static uint64_t arg_offset
= 0;
49 static uint64_t arg_skip
= 0;
50 static usec_t arg_timeout
= USEC_INFINITY
;
52 /* Options Debian's crypttab knows we don't:
62 static int parse_one_option(const char *option
) {
68 /* Handled outside of this tool */
69 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev"))
72 if ((val
= startswith(option
, "cipher="))) {
73 r
= free_and_strdup(&arg_cipher
, val
);
77 } else if ((val
= startswith(option
, "size="))) {
79 r
= safe_atou(val
, &arg_key_size
);
81 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
85 if (arg_key_size
% 8) {
86 log_error("size= not a multiple of 8, ignoring.");
92 } else if ((val
= startswith(option
, "key-slot="))) {
95 r
= safe_atoi(val
, &arg_key_slot
);
97 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
101 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
103 arg_type
= CRYPT_TCRYPT
;
104 if (path_is_absolute(val
)) {
105 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
108 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
110 } else if ((val
= startswith(option
, "keyfile-size="))) {
112 r
= safe_atou(val
, &arg_keyfile_size
);
114 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
118 } else if ((val
= startswith(option
, "keyfile-offset="))) {
121 r
= safe_atou64(val
, &off
);
123 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
127 if ((size_t) off
!= off
) {
128 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
129 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off
);
133 arg_keyfile_offset
= off
;
135 } else if ((val
= startswith(option
, "hash="))) {
136 r
= free_and_strdup(&arg_hash
, val
);
140 } else if ((val
= startswith(option
, "header="))) {
143 if (!path_is_absolute(val
)) {
144 log_error("Header path \"%s\" is not absolute, refusing.", val
);
149 log_error("Duplicate header= option, refusing.");
153 arg_header
= strdup(val
);
157 } else if ((val
= startswith(option
, "tries="))) {
159 r
= safe_atou(val
, &arg_tries
);
161 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
165 } else if (STR_IN_SET(option
, "readonly", "read-only"))
167 else if (streq(option
, "verify"))
169 else if (STR_IN_SET(option
, "allow-discards", "discard"))
171 else if (streq(option
, "luks"))
173 else if (streq(option
, "tcrypt"))
174 arg_type
= CRYPT_TCRYPT
;
175 else if (streq(option
, "tcrypt-hidden")) {
176 arg_type
= CRYPT_TCRYPT
;
177 arg_tcrypt_hidden
= true;
178 } else if (streq(option
, "tcrypt-system")) {
179 arg_type
= CRYPT_TCRYPT
;
180 arg_tcrypt_system
= true;
181 } else if (streq(option
, "tcrypt-veracrypt")) {
182 #ifdef CRYPT_TCRYPT_VERA_MODES
183 arg_type
= CRYPT_TCRYPT
;
184 arg_tcrypt_veracrypt
= true;
186 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
189 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
190 arg_type
= CRYPT_PLAIN
;
191 else if ((val
= startswith(option
, "timeout="))) {
193 r
= parse_sec_fix_0(val
, &arg_timeout
);
195 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
199 } else if ((val
= startswith(option
, "offset="))) {
201 r
= safe_atou64(val
, &arg_offset
);
203 return log_error_errno(r
, "Failed to parse %s: %m", option
);
205 } else if ((val
= startswith(option
, "skip="))) {
207 r
= safe_atou64(val
, &arg_skip
);
209 return log_error_errno(r
, "Failed to parse %s: %m", option
);
211 } else if (!streq(option
, "none"))
212 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
217 static int parse_options(const char *options
) {
218 const char *word
, *state
;
224 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
225 _cleanup_free_
char *o
;
227 o
= strndup(word
, l
);
230 r
= parse_one_option(o
);
235 /* sanity-check options */
236 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
238 log_warning("offset= ignored with type %s", arg_type
);
240 log_warning("skip= ignored with type %s", arg_type
);
246 static char* disk_description(const char *path
) {
248 static const char name_fields
[] =
249 "ID_PART_ENTRY_NAME\0"
251 "ID_MODEL_FROM_DATABASE\0"
254 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
261 if (stat(path
, &st
) < 0)
264 if (!S_ISBLK(st
.st_mode
))
267 r
= sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
);
271 NULSTR_FOREACH(i
, name_fields
) {
274 r
= sd_device_get_property_value(device
, i
, &name
);
275 if (r
>= 0 && !isempty(name
))
282 static char *disk_mount_point(const char *label
) {
283 _cleanup_free_
char *device
= NULL
;
284 _cleanup_endmntent_
FILE *f
= NULL
;
287 /* Yeah, we don't support native systemd unit files here for now */
289 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
292 f
= setmntent("/etc/fstab", "re");
296 while ((m
= getmntent(f
)))
297 if (path_equal(m
->mnt_fsname
, device
))
298 return strdup(m
->mnt_dir
);
303 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
304 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
305 _cleanup_strv_free_erase_
char **passwords
= NULL
;
306 const char *name
= NULL
;
314 description
= disk_description(src
);
315 mount_point
= disk_mount_point(vol
);
317 disk_path
= cescape(src
);
321 if (description
&& streq(vol
, description
))
322 /* If the description string is simply the
323 * volume name, then let's not show this
325 description
= mfree(description
);
327 if (mount_point
&& description
)
328 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
329 else if (mount_point
)
330 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
331 else if (description
)
332 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
337 name
= name_buffer
? name_buffer
: vol
;
339 if (asprintf(&text
, "Please enter passphrase for disk %s!", name
) < 0)
342 id
= strjoina("cryptsetup:", disk_path
);
344 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
345 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
348 return log_error_errno(r
, "Failed to query password: %m");
351 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
353 assert(strv_length(passwords
) == 1);
355 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
358 id
= strjoina("cryptsetup-verification:", disk_path
);
360 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
362 return log_error_errno(r
, "Failed to query verification password: %m");
364 assert(strv_length(passwords2
) == 1);
366 if (!streq(passwords
[0], passwords2
[0])) {
367 log_warning("Passwords did not match, retrying.");
372 strv_uniq(passwords
);
374 STRV_FOREACH(p
, passwords
) {
377 if (strlen(*p
)+1 >= arg_key_size
)
380 /* Pad password if necessary */
381 c
= new(char, arg_key_size
);
385 strncpy(c
, *p
, arg_key_size
);
390 *ret
= TAKE_PTR(passwords
);
395 static int attach_tcrypt(
396 struct crypt_device
*cd
,
398 const char *key_file
,
403 _cleanup_free_
char *passphrase
= NULL
;
404 struct crypt_params_tcrypt params
= {
405 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
406 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
407 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
412 assert(key_file
|| (passwords
&& passwords
[0]));
414 if (arg_tcrypt_hidden
)
415 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
417 if (arg_tcrypt_system
)
418 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
420 #ifdef CRYPT_TCRYPT_VERA_MODES
421 if (arg_tcrypt_veracrypt
)
422 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
426 r
= read_one_line_file(key_file
, &passphrase
);
428 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
432 params
.passphrase
= passphrase
;
434 params
.passphrase
= passwords
[0];
435 params
.passphrase_size
= strlen(params
.passphrase
);
437 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
439 if (key_file
&& r
== -EPERM
) {
440 log_error("Failed to activate using password file '%s'.", key_file
);
446 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
449 static int attach_luks_or_plain(struct crypt_device
*cd
,
451 const char *key_file
,
452 const char *data_device
,
456 bool pass_volume_key
= false;
460 assert(key_file
|| passwords
);
462 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
463 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
465 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
470 r
= crypt_set_data_device(cd
, data_device
);
473 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
474 struct crypt_params_plain params
= {
475 .offset
= arg_offset
,
478 const char *cipher
, *cipher_mode
;
479 _cleanup_free_
char *truncated_cipher
= NULL
;
482 /* plain isn't a real hash type. it just means "use no hash" */
483 if (!streq(arg_hash
, "plain"))
484 params
.hash
= arg_hash
;
485 } else if (!key_file
)
486 /* for CRYPT_PLAIN, the behaviour of cryptsetup
487 * package is to not hash when a key file is provided */
488 params
.hash
= "ripemd160";
493 l
= strcspn(arg_cipher
, "-");
494 truncated_cipher
= strndup(arg_cipher
, l
);
495 if (!truncated_cipher
)
498 cipher
= truncated_cipher
;
499 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
502 cipher_mode
= "cbc-essiv:sha256";
505 /* for CRYPT_PLAIN limit reads
506 * from keyfile to key length, and
507 * ignore keyfile-size */
508 arg_keyfile_size
= arg_key_size
;
510 /* In contrast to what the name
511 * crypt_setup() might suggest this
512 * doesn't actually format anything,
513 * it just configures encryption
514 * parameters when used for plain
516 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
518 /* hash == NULL implies the user passed "plain" */
519 pass_volume_key
= (params
.hash
== NULL
);
523 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
525 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
526 crypt_get_cipher(cd
),
527 crypt_get_cipher_mode(cd
),
528 crypt_get_volume_key_size(cd
)*8,
529 crypt_get_device_name(cd
));
532 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
534 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
540 STRV_FOREACH(p
, passwords
) {
542 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
544 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
554 static int help(void) {
556 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
557 "%s detach VOLUME\n\n"
558 "Attaches or detaches an encrypted block device.\n",
559 program_invocation_short_name
,
560 program_invocation_short_name
);
565 int main(int argc
, char *argv
[]) {
566 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
575 log_error("This program requires at least two arguments.");
579 log_set_target(LOG_TARGET_AUTO
);
580 log_parse_environment();
585 if (streq(argv
[1], "attach")) {
589 crypt_status_info status
;
590 const char *key_file
= NULL
;
592 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
595 log_error("attach requires at least two arguments.");
601 !streq(argv
[4], "-") &&
602 !streq(argv
[4], "none")) {
604 if (!path_is_absolute(argv
[4]))
605 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
610 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
611 if (parse_options(argv
[5]) < 0)
615 /* A delicious drop of snake oil */
616 mlockall(MCL_FUTURE
);
619 log_debug("LUKS header: %s", arg_header
);
620 r
= crypt_init(&cd
, arg_header
);
622 r
= crypt_init(&cd
, argv
[3]);
624 log_error_errno(r
, "crypt_init() failed: %m");
628 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
630 status
= crypt_status(cd
, argv
[2]);
631 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
632 log_info("Volume %s already active.", argv
[2]);
638 flags
|= CRYPT_ACTIVATE_READONLY
;
641 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
643 if (arg_timeout
== USEC_INFINITY
)
646 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
648 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
653 /* Ideally we'd do this on the open fd, but since this is just a
654 * warning it's OK to do this in two steps. */
655 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
656 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
659 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
660 _cleanup_strv_free_erase_
char **passwords
= NULL
;
663 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
670 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
671 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
673 r
= attach_luks_or_plain(cd
,
676 arg_header
? argv
[3] : NULL
,
686 log_error_errno(r
, "Failed to activate: %m");
690 log_warning("Invalid passphrase.");
693 if (arg_tries
!= 0 && tries
>= arg_tries
) {
694 log_error("Too many attempts; giving up.");
699 } else if (streq(argv
[1], "detach")) {
701 r
= crypt_init_by_name(&cd
, argv
[2]);
703 log_info("Volume %s already inactive.", argv
[2]);
708 log_error_errno(r
, "crypt_init_by_name() failed: %m");
712 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
714 r
= crypt_deactivate(cd
, argv
[2]);
716 log_error_errno(r
, "Failed to deactivate: %m");
721 log_error("Unknown verb %s.", argv
[1]);
731 strv_free(arg_tcrypt_keyfiles
);
733 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;