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 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
178 "Header path \"%s\" is not absolute, refusing.", val
);
181 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
182 "Duplicate header= option, refusing.");
184 arg_header
= strdup(val
);
188 } else if ((val
= startswith(option
, "tries="))) {
190 r
= safe_atou(val
, &arg_tries
);
192 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
196 } else if (STR_IN_SET(option
, "readonly", "read-only"))
198 else if (streq(option
, "verify"))
200 else if (STR_IN_SET(option
, "allow-discards", "discard"))
202 else if (streq(option
, "luks"))
204 else if (streq(option
, "tcrypt"))
205 arg_type
= CRYPT_TCRYPT
;
206 else if (streq(option
, "tcrypt-hidden")) {
207 arg_type
= CRYPT_TCRYPT
;
208 arg_tcrypt_hidden
= true;
209 } else if (streq(option
, "tcrypt-system")) {
210 arg_type
= CRYPT_TCRYPT
;
211 arg_tcrypt_system
= true;
212 } else if (streq(option
, "tcrypt-veracrypt")) {
213 #ifdef CRYPT_TCRYPT_VERA_MODES
214 arg_type
= CRYPT_TCRYPT
;
215 arg_tcrypt_veracrypt
= true;
217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
218 "This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
220 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
221 arg_type
= CRYPT_PLAIN
;
222 else if ((val
= startswith(option
, "timeout="))) {
224 r
= parse_sec_fix_0(val
, &arg_timeout
);
226 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
230 } else if ((val
= startswith(option
, "offset="))) {
232 r
= safe_atou64(val
, &arg_offset
);
234 return log_error_errno(r
, "Failed to parse %s: %m", option
);
236 } else if ((val
= startswith(option
, "skip="))) {
238 r
= safe_atou64(val
, &arg_skip
);
240 return log_error_errno(r
, "Failed to parse %s: %m", option
);
242 } else if (!streq(option
, "none"))
243 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
248 static int parse_options(const char *options
) {
249 const char *word
, *state
;
255 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
256 _cleanup_free_
char *o
;
258 o
= strndup(word
, l
);
261 r
= parse_one_option(o
);
266 /* sanity-check options */
267 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
269 log_warning("offset= ignored with type %s", arg_type
);
271 log_warning("skip= ignored with type %s", arg_type
);
277 static char* disk_description(const char *path
) {
278 static const char name_fields
[] =
279 "ID_PART_ENTRY_NAME\0"
281 "ID_MODEL_FROM_DATABASE\0"
284 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
285 const char *i
, *name
;
290 if (stat(path
, &st
) < 0)
293 if (!S_ISBLK(st
.st_mode
))
296 if (sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
) < 0)
299 NULSTR_FOREACH(i
, name_fields
)
300 if (sd_device_get_property_value(device
, i
, &name
) >= 0 &&
307 static char *disk_mount_point(const char *label
) {
308 _cleanup_free_
char *device
= NULL
;
309 _cleanup_endmntent_
FILE *f
= NULL
;
312 /* Yeah, we don't support native systemd unit files here for now */
314 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
317 f
= setmntent("/etc/fstab", "re");
321 while ((m
= getmntent(f
)))
322 if (path_equal(m
->mnt_fsname
, device
))
323 return strdup(m
->mnt_dir
);
328 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
329 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
330 _cleanup_strv_free_erase_
char **passwords
= NULL
;
331 const char *name
= NULL
;
339 description
= disk_description(src
);
340 mount_point
= disk_mount_point(vol
);
342 disk_path
= cescape(src
);
346 if (description
&& streq(vol
, description
))
347 /* If the description string is simply the
348 * volume name, then let's not show this
350 description
= mfree(description
);
352 if (mount_point
&& description
)
353 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
354 else if (mount_point
)
355 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
356 else if (description
)
357 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
362 name
= name_buffer
? name_buffer
: vol
;
364 if (asprintf(&text
, "Please enter passphrase for disk %s:", name
) < 0)
367 id
= strjoina("cryptsetup:", disk_path
);
369 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
370 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
373 return log_error_errno(r
, "Failed to query password: %m");
376 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
378 assert(strv_length(passwords
) == 1);
380 if (asprintf(&text
, "Please enter passphrase for disk %s (verification):", name
) < 0)
383 id
= strjoina("cryptsetup-verification:", disk_path
);
385 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
387 return log_error_errno(r
, "Failed to query verification password: %m");
389 assert(strv_length(passwords2
) == 1);
391 if (!streq(passwords
[0], passwords2
[0])) {
392 log_warning("Passwords did not match, retrying.");
397 strv_uniq(passwords
);
399 STRV_FOREACH(p
, passwords
) {
402 if (strlen(*p
)+1 >= arg_key_size
)
405 /* Pad password if necessary */
406 c
= new(char, arg_key_size
);
410 strncpy(c
, *p
, arg_key_size
);
411 free_and_replace(*p
, c
);
414 *ret
= TAKE_PTR(passwords
);
419 static int attach_tcrypt(
420 struct crypt_device
*cd
,
422 const char *key_file
,
427 _cleanup_free_
char *passphrase
= NULL
;
428 struct crypt_params_tcrypt params
= {
429 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
430 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
431 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
436 assert(key_file
|| (passwords
&& passwords
[0]));
438 if (arg_tcrypt_hidden
)
439 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
441 if (arg_tcrypt_system
)
442 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
444 #ifdef CRYPT_TCRYPT_VERA_MODES
445 if (arg_tcrypt_veracrypt
)
446 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
450 r
= read_one_line_file(key_file
, &passphrase
);
452 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
456 params
.passphrase
= passphrase
;
458 params
.passphrase
= passwords
[0];
459 params
.passphrase_size
= strlen(params
.passphrase
);
461 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
463 if (key_file
&& r
== -EPERM
)
464 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN
),
465 "Failed to activate using password file '%s'.",
470 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
473 static int attach_luks_or_plain(struct crypt_device
*cd
,
475 const char *key_file
,
476 const char *data_device
,
480 bool pass_volume_key
= false;
484 assert(key_file
|| passwords
);
486 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
487 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
489 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
494 r
= crypt_set_data_device(cd
, data_device
);
497 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
498 struct crypt_params_plain params
= {
499 .offset
= arg_offset
,
501 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
502 .sector_size
= arg_sector_size
,
505 const char *cipher
, *cipher_mode
;
506 _cleanup_free_
char *truncated_cipher
= NULL
;
509 /* plain isn't a real hash type. it just means "use no hash" */
510 if (!streq(arg_hash
, "plain"))
511 params
.hash
= arg_hash
;
512 } else if (!key_file
)
513 /* for CRYPT_PLAIN, the behaviour of cryptsetup
514 * package is to not hash when a key file is provided */
515 params
.hash
= "ripemd160";
520 l
= strcspn(arg_cipher
, "-");
521 truncated_cipher
= strndup(arg_cipher
, l
);
522 if (!truncated_cipher
)
525 cipher
= truncated_cipher
;
526 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
529 cipher_mode
= "cbc-essiv:sha256";
532 /* for CRYPT_PLAIN limit reads
533 * from keyfile to key length, and
534 * ignore keyfile-size */
535 arg_keyfile_size
= arg_key_size
;
537 /* In contrast to what the name
538 * crypt_setup() might suggest this
539 * doesn't actually format anything,
540 * it just configures encryption
541 * parameters when used for plain
543 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
545 /* hash == NULL implies the user passed "plain" */
546 pass_volume_key
= (params
.hash
== NULL
);
550 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
552 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
553 crypt_get_cipher(cd
),
554 crypt_get_cipher_mode(cd
),
555 crypt_get_volume_key_size(cd
)*8,
556 crypt_get_device_name(cd
));
559 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
561 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
567 STRV_FOREACH(p
, passwords
) {
569 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
571 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
581 static int help(void) {
582 _cleanup_free_
char *link
= NULL
;
585 r
= terminal_urlify_man("systemd-cryptsetup@.service", "8", &link
);
589 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
590 "%s detach VOLUME\n\n"
591 "Attaches or detaches an encrypted block device.\n"
592 "\nSee the %s for details.\n"
593 , program_invocation_short_name
594 , program_invocation_short_name
601 static int run(int argc
, char *argv
[]) {
602 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
609 log_error("This program requires at least two arguments.");
617 if (streq(argv
[1], "attach")) {
621 crypt_status_info status
;
622 const char *key_file
= NULL
;
624 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
627 log_error("attach requires at least two arguments.");
633 !streq(argv
[4], "-") &&
634 !streq(argv
[4], "none")) {
636 if (!path_is_absolute(argv
[4]))
637 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
642 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
643 r
= parse_options(argv
[5]);
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 return log_error_errno(r
, "crypt_init() failed: %m");
659 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
661 status
= crypt_status(cd
, argv
[2]);
662 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
663 log_info("Volume %s already active.", argv
[2]);
668 flags
|= CRYPT_ACTIVATE_READONLY
;
671 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
673 if (arg_timeout
== USEC_INFINITY
)
676 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
678 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
683 /* Ideally we'd do this on the open fd, but since this is just a
684 * warning it's OK to do this in two steps. */
685 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
686 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
689 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
690 _cleanup_strv_free_erase_
char **passwords
= NULL
;
693 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
700 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
701 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
703 r
= attach_luks_or_plain(cd
,
706 arg_header
? argv
[3] : NULL
,
716 return log_error_errno(r
, "Failed to activate: %m");
718 log_warning("Invalid passphrase.");
721 if (arg_tries
!= 0 && tries
>= arg_tries
) {
722 log_error("Too many attempts; giving up.");
726 } else if (streq(argv
[1], "detach")) {
728 r
= crypt_init_by_name(&cd
, argv
[2]);
730 log_info("Volume %s already inactive.", argv
[2]);
734 return log_error_errno(r
, "crypt_init_by_name() failed: %m");
736 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
738 r
= crypt_deactivate(cd
, argv
[2]);
740 return log_error_errno(r
, "Failed to deactivate: %m");
743 log_error("Unknown verb %s.", argv
[1]);
750 DEFINE_MAIN_FUNCTION(run
);