1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
13 #include "sd-device.h"
15 #include "alloc-util.h"
16 #include "ask-password-api.h"
17 #include "crypt-util.h"
18 #include "device-util.h"
22 #include "mount-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "string-util.h"
30 #define ANY_LUKS "LUKS"
32 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
33 static char *arg_cipher
= NULL
;
34 static unsigned arg_key_size
= 0;
35 static int arg_key_slot
= CRYPT_ANY_SLOT
;
36 static unsigned arg_keyfile_size
= 0;
37 static uint64_t arg_keyfile_offset
= 0;
38 static char *arg_hash
= NULL
;
39 static char *arg_header
= NULL
;
40 static unsigned arg_tries
= 3;
41 static bool arg_readonly
= false;
42 static bool arg_verify
= false;
43 static bool arg_discards
= false;
44 static bool arg_tcrypt_hidden
= false;
45 static bool arg_tcrypt_system
= false;
46 #ifdef CRYPT_TCRYPT_VERA_MODES
47 static bool arg_tcrypt_veracrypt
= false;
49 static char **arg_tcrypt_keyfiles
= NULL
;
50 static uint64_t arg_offset
= 0;
51 static uint64_t arg_skip
= 0;
52 static usec_t arg_timeout
= USEC_INFINITY
;
54 /* Options Debian's crypttab knows we don't:
64 static int parse_one_option(const char *option
) {
70 /* Handled outside of this tool */
71 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev"))
74 if ((val
= startswith(option
, "cipher="))) {
75 r
= free_and_strdup(&arg_cipher
, val
);
79 } else if ((val
= startswith(option
, "size="))) {
81 r
= safe_atou(val
, &arg_key_size
);
83 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
87 if (arg_key_size
% 8) {
88 log_error("size= not a multiple of 8, ignoring.");
94 } else if ((val
= startswith(option
, "key-slot="))) {
97 r
= safe_atoi(val
, &arg_key_slot
);
99 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
103 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
105 arg_type
= CRYPT_TCRYPT
;
106 if (path_is_absolute(val
)) {
107 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
110 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
112 } else if ((val
= startswith(option
, "keyfile-size="))) {
114 r
= safe_atou(val
, &arg_keyfile_size
);
116 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
120 } else if ((val
= startswith(option
, "keyfile-offset="))) {
123 r
= safe_atou64(val
, &off
);
125 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
129 if ((size_t) off
!= off
) {
130 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
131 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off
);
135 arg_keyfile_offset
= off
;
137 } else if ((val
= startswith(option
, "hash="))) {
138 r
= free_and_strdup(&arg_hash
, val
);
142 } else if ((val
= startswith(option
, "header="))) {
145 if (!path_is_absolute(val
)) {
146 log_error("Header path \"%s\" is not absolute, refusing.", val
);
151 log_error("Duplicate header= option, refusing.");
155 arg_header
= strdup(val
);
159 } else if ((val
= startswith(option
, "tries="))) {
161 r
= safe_atou(val
, &arg_tries
);
163 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
167 } else if (STR_IN_SET(option
, "readonly", "read-only"))
169 else if (streq(option
, "verify"))
171 else if (STR_IN_SET(option
, "allow-discards", "discard"))
173 else if (streq(option
, "luks"))
175 else if (streq(option
, "tcrypt"))
176 arg_type
= CRYPT_TCRYPT
;
177 else if (streq(option
, "tcrypt-hidden")) {
178 arg_type
= CRYPT_TCRYPT
;
179 arg_tcrypt_hidden
= true;
180 } else if (streq(option
, "tcrypt-system")) {
181 arg_type
= CRYPT_TCRYPT
;
182 arg_tcrypt_system
= true;
183 } else if (streq(option
, "tcrypt-veracrypt")) {
184 #ifdef CRYPT_TCRYPT_VERA_MODES
185 arg_type
= CRYPT_TCRYPT
;
186 arg_tcrypt_veracrypt
= true;
188 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
191 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
192 arg_type
= CRYPT_PLAIN
;
193 else if ((val
= startswith(option
, "timeout="))) {
195 r
= parse_sec_fix_0(val
, &arg_timeout
);
197 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
201 } else if ((val
= startswith(option
, "offset="))) {
203 r
= safe_atou64(val
, &arg_offset
);
205 return log_error_errno(r
, "Failed to parse %s: %m", option
);
207 } else if ((val
= startswith(option
, "skip="))) {
209 r
= safe_atou64(val
, &arg_skip
);
211 return log_error_errno(r
, "Failed to parse %s: %m", option
);
213 } else if (!streq(option
, "none"))
214 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
219 static int parse_options(const char *options
) {
220 const char *word
, *state
;
226 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
227 _cleanup_free_
char *o
;
229 o
= strndup(word
, l
);
232 r
= parse_one_option(o
);
237 /* sanity-check options */
238 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
240 log_warning("offset= ignored with type %s", arg_type
);
242 log_warning("skip= ignored with type %s", arg_type
);
248 static char* disk_description(const char *path
) {
250 static const char name_fields
[] =
251 "ID_PART_ENTRY_NAME\0"
253 "ID_MODEL_FROM_DATABASE\0"
256 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
263 if (stat(path
, &st
) < 0)
266 if (!S_ISBLK(st
.st_mode
))
269 r
= sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
);
273 NULSTR_FOREACH(i
, name_fields
) {
276 r
= sd_device_get_property_value(device
, i
, &name
);
277 if (r
>= 0 && !isempty(name
))
284 static char *disk_mount_point(const char *label
) {
285 _cleanup_free_
char *device
= NULL
;
286 _cleanup_endmntent_
FILE *f
= NULL
;
289 /* Yeah, we don't support native systemd unit files here for now */
291 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
294 f
= setmntent("/etc/fstab", "re");
298 while ((m
= getmntent(f
)))
299 if (path_equal(m
->mnt_fsname
, device
))
300 return strdup(m
->mnt_dir
);
305 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
306 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
307 _cleanup_strv_free_erase_
char **passwords
= NULL
;
308 const char *name
= NULL
;
316 description
= disk_description(src
);
317 mount_point
= disk_mount_point(vol
);
319 disk_path
= cescape(src
);
323 if (description
&& streq(vol
, description
))
324 /* If the description string is simply the
325 * volume name, then let's not show this
327 description
= mfree(description
);
329 if (mount_point
&& description
)
330 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
331 else if (mount_point
)
332 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
333 else if (description
)
334 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
339 name
= name_buffer
? name_buffer
: vol
;
341 if (asprintf(&text
, "Please enter passphrase for disk %s!", name
) < 0)
344 id
= strjoina("cryptsetup:", disk_path
);
346 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
347 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
350 return log_error_errno(r
, "Failed to query password: %m");
353 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
355 assert(strv_length(passwords
) == 1);
357 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
360 id
= strjoina("cryptsetup-verification:", disk_path
);
362 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
364 return log_error_errno(r
, "Failed to query verification password: %m");
366 assert(strv_length(passwords2
) == 1);
368 if (!streq(passwords
[0], passwords2
[0])) {
369 log_warning("Passwords did not match, retrying.");
374 strv_uniq(passwords
);
376 STRV_FOREACH(p
, passwords
) {
379 if (strlen(*p
)+1 >= arg_key_size
)
382 /* Pad password if necessary */
383 c
= new(char, arg_key_size
);
387 strncpy(c
, *p
, arg_key_size
);
392 *ret
= TAKE_PTR(passwords
);
397 static int attach_tcrypt(
398 struct crypt_device
*cd
,
400 const char *key_file
,
405 _cleanup_free_
char *passphrase
= NULL
;
406 struct crypt_params_tcrypt params
= {
407 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
408 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
409 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
414 assert(key_file
|| (passwords
&& passwords
[0]));
416 if (arg_tcrypt_hidden
)
417 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
419 if (arg_tcrypt_system
)
420 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
422 #ifdef CRYPT_TCRYPT_VERA_MODES
423 if (arg_tcrypt_veracrypt
)
424 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
428 r
= read_one_line_file(key_file
, &passphrase
);
430 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
434 params
.passphrase
= passphrase
;
436 params
.passphrase
= passwords
[0];
437 params
.passphrase_size
= strlen(params
.passphrase
);
439 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
441 if (key_file
&& r
== -EPERM
) {
442 log_error("Failed to activate using password file '%s'.", key_file
);
448 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
451 static int attach_luks_or_plain(struct crypt_device
*cd
,
453 const char *key_file
,
454 const char *data_device
,
458 bool pass_volume_key
= false;
462 assert(key_file
|| passwords
);
464 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
465 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
467 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
472 r
= crypt_set_data_device(cd
, data_device
);
475 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
476 struct crypt_params_plain params
= {
477 .offset
= arg_offset
,
480 const char *cipher
, *cipher_mode
;
481 _cleanup_free_
char *truncated_cipher
= NULL
;
484 /* plain isn't a real hash type. it just means "use no hash" */
485 if (!streq(arg_hash
, "plain"))
486 params
.hash
= arg_hash
;
487 } else if (!key_file
)
488 /* for CRYPT_PLAIN, the behaviour of cryptsetup
489 * package is to not hash when a key file is provided */
490 params
.hash
= "ripemd160";
495 l
= strcspn(arg_cipher
, "-");
496 truncated_cipher
= strndup(arg_cipher
, l
);
497 if (!truncated_cipher
)
500 cipher
= truncated_cipher
;
501 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
504 cipher_mode
= "cbc-essiv:sha256";
507 /* for CRYPT_PLAIN limit reads
508 * from keyfile to key length, and
509 * ignore keyfile-size */
510 arg_keyfile_size
= arg_key_size
;
512 /* In contrast to what the name
513 * crypt_setup() might suggest this
514 * doesn't actually format anything,
515 * it just configures encryption
516 * parameters when used for plain
518 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
520 /* hash == NULL implies the user passed "plain" */
521 pass_volume_key
= (params
.hash
== NULL
);
525 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
527 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
528 crypt_get_cipher(cd
),
529 crypt_get_cipher_mode(cd
),
530 crypt_get_volume_key_size(cd
)*8,
531 crypt_get_device_name(cd
));
534 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
536 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
542 STRV_FOREACH(p
, passwords
) {
544 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
546 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
556 static int help(void) {
558 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
559 "%s detach VOLUME\n\n"
560 "Attaches or detaches an encrypted block device.\n",
561 program_invocation_short_name
,
562 program_invocation_short_name
);
567 int main(int argc
, char *argv
[]) {
568 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
577 log_error("This program requires at least two arguments.");
581 log_set_target(LOG_TARGET_AUTO
);
582 log_parse_environment();
587 if (streq(argv
[1], "attach")) {
591 crypt_status_info status
;
592 const char *key_file
= NULL
;
594 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
597 log_error("attach requires at least two arguments.");
603 !streq(argv
[4], "-") &&
604 !streq(argv
[4], "none")) {
606 if (!path_is_absolute(argv
[4]))
607 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
612 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
613 if (parse_options(argv
[5]) < 0)
617 /* A delicious drop of snake oil */
618 mlockall(MCL_FUTURE
);
621 log_debug("LUKS header: %s", arg_header
);
622 r
= crypt_init(&cd
, arg_header
);
624 r
= crypt_init(&cd
, argv
[3]);
626 log_error_errno(r
, "crypt_init() failed: %m");
630 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
632 status
= crypt_status(cd
, argv
[2]);
633 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
634 log_info("Volume %s already active.", argv
[2]);
640 flags
|= CRYPT_ACTIVATE_READONLY
;
643 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
645 if (arg_timeout
== USEC_INFINITY
)
648 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
650 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
655 /* Ideally we'd do this on the open fd, but since this is just a
656 * warning it's OK to do this in two steps. */
657 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
658 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
661 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
662 _cleanup_strv_free_erase_
char **passwords
= NULL
;
665 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
672 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
673 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
675 r
= attach_luks_or_plain(cd
,
678 arg_header
? argv
[3] : NULL
,
688 log_error_errno(r
, "Failed to activate: %m");
692 log_warning("Invalid passphrase.");
695 if (arg_tries
!= 0 && tries
>= arg_tries
) {
696 log_error("Too many attempts; giving up.");
701 } else if (streq(argv
[1], "detach")) {
703 r
= crypt_init_by_name(&cd
, argv
[2]);
705 log_info("Volume %s already inactive.", argv
[2]);
710 log_error_errno(r
, "crypt_init_by_name() failed: %m");
714 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
716 r
= crypt_deactivate(cd
, argv
[2]);
718 log_error_errno(r
, "Failed to deactivate: %m");
723 log_error("Unknown verb %s.", argv
[1]);
733 strv_free(arg_tcrypt_keyfiles
);
735 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;