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 "main-func.h"
18 #include "mount-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "string-util.h"
23 #include "pretty-print.h"
27 #define ANY_LUKS "LUKS"
28 /* as in src/cryptsetup.h */
29 #define CRYPT_SECTOR_SIZE 512
30 #define CRYPT_MAX_SECTOR_SIZE 4096
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 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
36 static unsigned arg_sector_size
= CRYPT_SECTOR_SIZE
;
38 static int arg_key_slot
= CRYPT_ANY_SLOT
;
39 static unsigned arg_keyfile_size
= 0;
40 static uint64_t arg_keyfile_offset
= 0;
41 static char *arg_hash
= NULL
;
42 static char *arg_header
= NULL
;
43 static unsigned arg_tries
= 3;
44 static bool arg_readonly
= false;
45 static bool arg_verify
= false;
46 static bool arg_discards
= false;
47 static bool arg_tcrypt_hidden
= false;
48 static bool arg_tcrypt_system
= false;
49 #ifdef CRYPT_TCRYPT_VERA_MODES
50 static bool arg_tcrypt_veracrypt
= false;
52 static char **arg_tcrypt_keyfiles
= NULL
;
53 static uint64_t arg_offset
= 0;
54 static uint64_t arg_skip
= 0;
55 static usec_t arg_timeout
= USEC_INFINITY
;
57 STATIC_DESTRUCTOR_REGISTER(arg_cipher
, freep
);
58 STATIC_DESTRUCTOR_REGISTER(arg_hash
, freep
);
59 STATIC_DESTRUCTOR_REGISTER(arg_header
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles
, strv_freep
);
62 /* Options Debian's crypttab knows we don't:
72 static int parse_one_option(const char *option
) {
78 /* Handled outside of this tool */
79 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev"))
82 if ((val
= startswith(option
, "cipher="))) {
83 r
= free_and_strdup(&arg_cipher
, val
);
87 } else if ((val
= startswith(option
, "size="))) {
89 r
= safe_atou(val
, &arg_key_size
);
91 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
95 if (arg_key_size
% 8) {
96 log_error("size= not a multiple of 8, ignoring.");
102 } else if ((val
= startswith(option
, "sector-size="))) {
104 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
105 r
= safe_atou(val
, &arg_sector_size
);
107 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
111 if (arg_sector_size
% 2) {
112 log_error("sector-size= not a multiple of 2, ignoring.");
116 if (arg_sector_size
< CRYPT_SECTOR_SIZE
|| arg_sector_size
> CRYPT_MAX_SECTOR_SIZE
) {
117 log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE
, CRYPT_MAX_SECTOR_SIZE
);
121 log_error("sector-size= is not supported, compiled with old libcryptsetup.");
125 } else if ((val
= startswith(option
, "key-slot="))) {
128 r
= safe_atoi(val
, &arg_key_slot
);
130 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
134 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
136 arg_type
= CRYPT_TCRYPT
;
137 if (path_is_absolute(val
)) {
138 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
141 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
143 } else if ((val
= startswith(option
, "keyfile-size="))) {
145 r
= safe_atou(val
, &arg_keyfile_size
);
147 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
151 } else if ((val
= startswith(option
, "keyfile-offset="))) {
154 r
= safe_atou64(val
, &off
);
156 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
160 if ((size_t) off
!= off
) {
161 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
162 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off
);
166 arg_keyfile_offset
= off
;
168 } else if ((val
= startswith(option
, "hash="))) {
169 r
= free_and_strdup(&arg_hash
, val
);
173 } else if ((val
= startswith(option
, "header="))) {
176 if (!path_is_absolute(val
)) {
177 log_error("Header path \"%s\" is not absolute, refusing.", val
);
182 log_error("Duplicate header= option, refusing.");
186 arg_header
= strdup(val
);
190 } else if ((val
= startswith(option
, "tries="))) {
192 r
= safe_atou(val
, &arg_tries
);
194 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
198 } else if (STR_IN_SET(option
, "readonly", "read-only"))
200 else if (streq(option
, "verify"))
202 else if (STR_IN_SET(option
, "allow-discards", "discard"))
204 else if (streq(option
, "luks"))
206 else if (streq(option
, "tcrypt"))
207 arg_type
= CRYPT_TCRYPT
;
208 else if (streq(option
, "tcrypt-hidden")) {
209 arg_type
= CRYPT_TCRYPT
;
210 arg_tcrypt_hidden
= true;
211 } else if (streq(option
, "tcrypt-system")) {
212 arg_type
= CRYPT_TCRYPT
;
213 arg_tcrypt_system
= true;
214 } else if (streq(option
, "tcrypt-veracrypt")) {
215 #ifdef CRYPT_TCRYPT_VERA_MODES
216 arg_type
= CRYPT_TCRYPT
;
217 arg_tcrypt_veracrypt
= true;
219 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
222 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
223 arg_type
= CRYPT_PLAIN
;
224 else if ((val
= startswith(option
, "timeout="))) {
226 r
= parse_sec_fix_0(val
, &arg_timeout
);
228 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
232 } else if ((val
= startswith(option
, "offset="))) {
234 r
= safe_atou64(val
, &arg_offset
);
236 return log_error_errno(r
, "Failed to parse %s: %m", option
);
238 } else if ((val
= startswith(option
, "skip="))) {
240 r
= safe_atou64(val
, &arg_skip
);
242 return log_error_errno(r
, "Failed to parse %s: %m", option
);
244 } else if (!streq(option
, "none"))
245 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
250 static int parse_options(const char *options
) {
251 const char *word
, *state
;
257 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
258 _cleanup_free_
char *o
;
260 o
= strndup(word
, l
);
263 r
= parse_one_option(o
);
268 /* sanity-check options */
269 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
271 log_warning("offset= ignored with type %s", arg_type
);
273 log_warning("skip= ignored with type %s", arg_type
);
279 static char* disk_description(const char *path
) {
280 static const char name_fields
[] =
281 "ID_PART_ENTRY_NAME\0"
283 "ID_MODEL_FROM_DATABASE\0"
286 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
287 const char *i
, *name
;
292 if (stat(path
, &st
) < 0)
295 if (!S_ISBLK(st
.st_mode
))
298 if (sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
) < 0)
301 NULSTR_FOREACH(i
, name_fields
)
302 if (sd_device_get_property_value(device
, i
, &name
) >= 0 &&
309 static char *disk_mount_point(const char *label
) {
310 _cleanup_free_
char *device
= NULL
;
311 _cleanup_endmntent_
FILE *f
= NULL
;
314 /* Yeah, we don't support native systemd unit files here for now */
316 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
319 f
= setmntent("/etc/fstab", "re");
323 while ((m
= getmntent(f
)))
324 if (path_equal(m
->mnt_fsname
, device
))
325 return strdup(m
->mnt_dir
);
330 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
331 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
332 _cleanup_strv_free_erase_
char **passwords
= NULL
;
333 const char *name
= NULL
;
341 description
= disk_description(src
);
342 mount_point
= disk_mount_point(vol
);
344 disk_path
= cescape(src
);
348 if (description
&& streq(vol
, description
))
349 /* If the description string is simply the
350 * volume name, then let's not show this
352 description
= mfree(description
);
354 if (mount_point
&& description
)
355 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
356 else if (mount_point
)
357 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
358 else if (description
)
359 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
364 name
= name_buffer
? name_buffer
: vol
;
366 if (asprintf(&text
, "Please enter passphrase for disk %s:", name
) < 0)
369 id
= strjoina("cryptsetup:", disk_path
);
371 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
372 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
375 return log_error_errno(r
, "Failed to query password: %m");
378 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
380 assert(strv_length(passwords
) == 1);
382 if (asprintf(&text
, "Please enter passphrase for disk %s (verification):", name
) < 0)
385 id
= strjoina("cryptsetup-verification:", disk_path
);
387 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
389 return log_error_errno(r
, "Failed to query verification password: %m");
391 assert(strv_length(passwords2
) == 1);
393 if (!streq(passwords
[0], passwords2
[0])) {
394 log_warning("Passwords did not match, retrying.");
399 strv_uniq(passwords
);
401 STRV_FOREACH(p
, passwords
) {
404 if (strlen(*p
)+1 >= arg_key_size
)
407 /* Pad password if necessary */
408 c
= new(char, arg_key_size
);
412 strncpy(c
, *p
, arg_key_size
);
417 *ret
= TAKE_PTR(passwords
);
422 static int attach_tcrypt(
423 struct crypt_device
*cd
,
425 const char *key_file
,
430 _cleanup_free_
char *passphrase
= NULL
;
431 struct crypt_params_tcrypt params
= {
432 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
433 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
434 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
439 assert(key_file
|| (passwords
&& passwords
[0]));
441 if (arg_tcrypt_hidden
)
442 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
444 if (arg_tcrypt_system
)
445 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
447 #ifdef CRYPT_TCRYPT_VERA_MODES
448 if (arg_tcrypt_veracrypt
)
449 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
453 r
= read_one_line_file(key_file
, &passphrase
);
455 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
459 params
.passphrase
= passphrase
;
461 params
.passphrase
= passwords
[0];
462 params
.passphrase_size
= strlen(params
.passphrase
);
464 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
466 if (key_file
&& r
== -EPERM
) {
467 log_error("Failed to activate using password file '%s'.", key_file
);
473 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
476 static int attach_luks_or_plain(struct crypt_device
*cd
,
478 const char *key_file
,
479 const char *data_device
,
483 bool pass_volume_key
= false;
487 assert(key_file
|| passwords
);
489 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
490 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
492 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
497 r
= crypt_set_data_device(cd
, data_device
);
500 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
501 struct crypt_params_plain params
= {
502 .offset
= arg_offset
,
504 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
505 .sector_size
= arg_sector_size
,
508 const char *cipher
, *cipher_mode
;
509 _cleanup_free_
char *truncated_cipher
= NULL
;
512 /* plain isn't a real hash type. it just means "use no hash" */
513 if (!streq(arg_hash
, "plain"))
514 params
.hash
= arg_hash
;
515 } else if (!key_file
)
516 /* for CRYPT_PLAIN, the behaviour of cryptsetup
517 * package is to not hash when a key file is provided */
518 params
.hash
= "ripemd160";
523 l
= strcspn(arg_cipher
, "-");
524 truncated_cipher
= strndup(arg_cipher
, l
);
525 if (!truncated_cipher
)
528 cipher
= truncated_cipher
;
529 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
532 cipher_mode
= "cbc-essiv:sha256";
535 /* for CRYPT_PLAIN limit reads
536 * from keyfile to key length, and
537 * ignore keyfile-size */
538 arg_keyfile_size
= arg_key_size
;
540 /* In contrast to what the name
541 * crypt_setup() might suggest this
542 * doesn't actually format anything,
543 * it just configures encryption
544 * parameters when used for plain
546 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
548 /* hash == NULL implies the user passed "plain" */
549 pass_volume_key
= (params
.hash
== NULL
);
553 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
555 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
556 crypt_get_cipher(cd
),
557 crypt_get_cipher_mode(cd
),
558 crypt_get_volume_key_size(cd
)*8,
559 crypt_get_device_name(cd
));
562 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
564 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
570 STRV_FOREACH(p
, passwords
) {
572 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
574 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
584 static int help(void) {
585 _cleanup_free_
char *link
= NULL
;
588 r
= terminal_urlify_man("systemd-cryptsetup@.service", "8", &link
);
592 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
593 "%s detach VOLUME\n\n"
594 "Attaches or detaches an encrypted block device.\n"
595 "\nSee the %s for details.\n"
596 , program_invocation_short_name
597 , program_invocation_short_name
604 static int run(int argc
, char *argv
[]) {
605 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
612 log_error("This program requires at least two arguments.");
620 if (streq(argv
[1], "attach")) {
624 crypt_status_info status
;
625 const char *key_file
= NULL
;
627 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
630 log_error("attach requires at least two arguments.");
636 !streq(argv
[4], "-") &&
637 !streq(argv
[4], "none")) {
639 if (!path_is_absolute(argv
[4]))
640 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
645 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
646 r
= parse_options(argv
[5]);
651 /* A delicious drop of snake oil */
652 mlockall(MCL_FUTURE
);
655 log_debug("LUKS header: %s", arg_header
);
656 r
= crypt_init(&cd
, arg_header
);
658 r
= crypt_init(&cd
, argv
[3]);
660 return log_error_errno(r
, "crypt_init() failed: %m");
662 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
664 status
= crypt_status(cd
, argv
[2]);
665 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
666 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 return log_error_errno(r
, "Failed to activate: %m");
721 log_warning("Invalid passphrase.");
724 if (arg_tries
!= 0 && tries
>= arg_tries
) {
725 log_error("Too many attempts; giving up.");
729 } else if (streq(argv
[1], "detach")) {
731 r
= crypt_init_by_name(&cd
, argv
[2]);
733 log_info("Volume %s already inactive.", argv
[2]);
737 return log_error_errno(r
, "crypt_init_by_name() failed: %m");
739 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
741 r
= crypt_deactivate(cd
, argv
[2]);
743 return log_error_errno(r
, "Failed to deactivate: %m");
746 log_error("Unknown verb %s.", argv
[1]);
753 DEFINE_MAIN_FUNCTION(run
);