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"
25 #define ANY_LUKS "LUKS"
27 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
28 static char *arg_cipher
= NULL
;
29 static unsigned arg_key_size
= 0;
30 static int arg_key_slot
= CRYPT_ANY_SLOT
;
31 static unsigned arg_keyfile_size
= 0;
32 static uint64_t arg_keyfile_offset
= 0;
33 static char *arg_hash
= NULL
;
34 static char *arg_header
= NULL
;
35 static unsigned arg_tries
= 3;
36 static bool arg_readonly
= false;
37 static bool arg_verify
= false;
38 static bool arg_discards
= false;
39 static bool arg_tcrypt_hidden
= false;
40 static bool arg_tcrypt_system
= false;
41 #ifdef CRYPT_TCRYPT_VERA_MODES
42 static bool arg_tcrypt_veracrypt
= false;
44 static char **arg_tcrypt_keyfiles
= NULL
;
45 static uint64_t arg_offset
= 0;
46 static uint64_t arg_skip
= 0;
47 static usec_t arg_timeout
= USEC_INFINITY
;
49 /* Options Debian's crypttab knows we don't:
59 static int parse_one_option(const char *option
) {
65 /* Handled outside of this tool */
66 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev"))
69 if ((val
= startswith(option
, "cipher="))) {
70 r
= free_and_strdup(&arg_cipher
, val
);
74 } else if ((val
= startswith(option
, "size="))) {
76 r
= safe_atou(val
, &arg_key_size
);
78 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
82 if (arg_key_size
% 8) {
83 log_error("size= not a multiple of 8, ignoring.");
89 } else if ((val
= startswith(option
, "key-slot="))) {
92 r
= safe_atoi(val
, &arg_key_slot
);
94 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
98 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
100 arg_type
= CRYPT_TCRYPT
;
101 if (path_is_absolute(val
)) {
102 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
105 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
107 } else if ((val
= startswith(option
, "keyfile-size="))) {
109 r
= safe_atou(val
, &arg_keyfile_size
);
111 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
115 } else if ((val
= startswith(option
, "keyfile-offset="))) {
118 r
= safe_atou64(val
, &off
);
120 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
124 if ((size_t) off
!= off
) {
125 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
126 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off
);
130 arg_keyfile_offset
= off
;
132 } else if ((val
= startswith(option
, "hash="))) {
133 r
= free_and_strdup(&arg_hash
, val
);
137 } else if ((val
= startswith(option
, "header="))) {
140 if (!path_is_absolute(val
)) {
141 log_error("Header path \"%s\" is not absolute, refusing.", val
);
146 log_error("Duplicate header= option, refusing.");
150 arg_header
= strdup(val
);
154 } else if ((val
= startswith(option
, "tries="))) {
156 r
= safe_atou(val
, &arg_tries
);
158 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
162 } else if (STR_IN_SET(option
, "readonly", "read-only"))
164 else if (streq(option
, "verify"))
166 else if (STR_IN_SET(option
, "allow-discards", "discard"))
168 else if (streq(option
, "luks"))
170 else if (streq(option
, "tcrypt"))
171 arg_type
= CRYPT_TCRYPT
;
172 else if (streq(option
, "tcrypt-hidden")) {
173 arg_type
= CRYPT_TCRYPT
;
174 arg_tcrypt_hidden
= true;
175 } else if (streq(option
, "tcrypt-system")) {
176 arg_type
= CRYPT_TCRYPT
;
177 arg_tcrypt_system
= true;
178 } else if (streq(option
, "tcrypt-veracrypt")) {
179 #ifdef CRYPT_TCRYPT_VERA_MODES
180 arg_type
= CRYPT_TCRYPT
;
181 arg_tcrypt_veracrypt
= true;
183 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
186 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
187 arg_type
= CRYPT_PLAIN
;
188 else if ((val
= startswith(option
, "timeout="))) {
190 r
= parse_sec_fix_0(val
, &arg_timeout
);
192 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
196 } else if ((val
= startswith(option
, "offset="))) {
198 r
= safe_atou64(val
, &arg_offset
);
200 return log_error_errno(r
, "Failed to parse %s: %m", option
);
202 } else if ((val
= startswith(option
, "skip="))) {
204 r
= safe_atou64(val
, &arg_skip
);
206 return log_error_errno(r
, "Failed to parse %s: %m", option
);
208 } else if (!streq(option
, "none"))
209 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
214 static int parse_options(const char *options
) {
215 const char *word
, *state
;
221 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
222 _cleanup_free_
char *o
;
224 o
= strndup(word
, l
);
227 r
= parse_one_option(o
);
232 /* sanity-check options */
233 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
235 log_warning("offset= ignored with type %s", arg_type
);
237 log_warning("skip= ignored with type %s", arg_type
);
243 static char* disk_description(const char *path
) {
245 static const char name_fields
[] =
246 "ID_PART_ENTRY_NAME\0"
248 "ID_MODEL_FROM_DATABASE\0"
251 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
258 if (stat(path
, &st
) < 0)
261 if (!S_ISBLK(st
.st_mode
))
264 r
= sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
);
268 NULSTR_FOREACH(i
, name_fields
) {
271 r
= sd_device_get_property_value(device
, i
, &name
);
272 if (r
>= 0 && !isempty(name
))
279 static char *disk_mount_point(const char *label
) {
280 _cleanup_free_
char *device
= NULL
;
281 _cleanup_endmntent_
FILE *f
= NULL
;
284 /* Yeah, we don't support native systemd unit files here for now */
286 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
289 f
= setmntent("/etc/fstab", "re");
293 while ((m
= getmntent(f
)))
294 if (path_equal(m
->mnt_fsname
, device
))
295 return strdup(m
->mnt_dir
);
300 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
301 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *text
= NULL
, *disk_path
= NULL
;
302 _cleanup_strv_free_erase_
char **passwords
= NULL
;
303 const char *name
= NULL
;
311 description
= disk_description(src
);
312 mount_point
= disk_mount_point(vol
);
314 disk_path
= cescape(src
);
318 if (description
&& streq(vol
, description
))
319 /* If the description string is simply the
320 * volume name, then let's not show this
322 description
= mfree(description
);
324 if (mount_point
&& description
)
325 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
326 else if (mount_point
)
327 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
328 else if (description
)
329 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
334 name
= name_buffer
? name_buffer
: vol
;
336 if (asprintf(&text
, "Please enter passphrase for disk %s!", name
) < 0)
339 id
= strjoina("cryptsetup:", disk_path
);
341 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
342 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
345 return log_error_errno(r
, "Failed to query password: %m");
348 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
350 assert(strv_length(passwords
) == 1);
352 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
355 id
= strjoina("cryptsetup-verification:", disk_path
);
357 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
359 return log_error_errno(r
, "Failed to query verification password: %m");
361 assert(strv_length(passwords2
) == 1);
363 if (!streq(passwords
[0], passwords2
[0])) {
364 log_warning("Passwords did not match, retrying.");
369 strv_uniq(passwords
);
371 STRV_FOREACH(p
, passwords
) {
374 if (strlen(*p
)+1 >= arg_key_size
)
377 /* Pad password if necessary */
378 c
= new(char, arg_key_size
);
382 strncpy(c
, *p
, arg_key_size
);
387 *ret
= TAKE_PTR(passwords
);
392 static int attach_tcrypt(
393 struct crypt_device
*cd
,
395 const char *key_file
,
400 _cleanup_free_
char *passphrase
= NULL
;
401 struct crypt_params_tcrypt params
= {
402 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
403 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
404 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
409 assert(key_file
|| (passwords
&& passwords
[0]));
411 if (arg_tcrypt_hidden
)
412 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
414 if (arg_tcrypt_system
)
415 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
417 #ifdef CRYPT_TCRYPT_VERA_MODES
418 if (arg_tcrypt_veracrypt
)
419 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
423 r
= read_one_line_file(key_file
, &passphrase
);
425 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
429 params
.passphrase
= passphrase
;
431 params
.passphrase
= passwords
[0];
432 params
.passphrase_size
= strlen(params
.passphrase
);
434 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
436 if (key_file
&& r
== -EPERM
) {
437 log_error("Failed to activate using password file '%s'.", key_file
);
443 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
446 static int attach_luks_or_plain(struct crypt_device
*cd
,
448 const char *key_file
,
449 const char *data_device
,
453 bool pass_volume_key
= false;
457 assert(key_file
|| passwords
);
459 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
460 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
462 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
467 r
= crypt_set_data_device(cd
, data_device
);
470 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
471 struct crypt_params_plain params
= {
472 .offset
= arg_offset
,
475 const char *cipher
, *cipher_mode
;
476 _cleanup_free_
char *truncated_cipher
= NULL
;
479 /* plain isn't a real hash type. it just means "use no hash" */
480 if (!streq(arg_hash
, "plain"))
481 params
.hash
= arg_hash
;
482 } else if (!key_file
)
483 /* for CRYPT_PLAIN, the behaviour of cryptsetup
484 * package is to not hash when a key file is provided */
485 params
.hash
= "ripemd160";
490 l
= strcspn(arg_cipher
, "-");
491 truncated_cipher
= strndup(arg_cipher
, l
);
492 if (!truncated_cipher
)
495 cipher
= truncated_cipher
;
496 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
499 cipher_mode
= "cbc-essiv:sha256";
502 /* for CRYPT_PLAIN limit reads
503 * from keyfile to key length, and
504 * ignore keyfile-size */
505 arg_keyfile_size
= arg_key_size
;
507 /* In contrast to what the name
508 * crypt_setup() might suggest this
509 * doesn't actually format anything,
510 * it just configures encryption
511 * parameters when used for plain
513 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
515 /* hash == NULL implies the user passed "plain" */
516 pass_volume_key
= (params
.hash
== NULL
);
520 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
522 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
523 crypt_get_cipher(cd
),
524 crypt_get_cipher_mode(cd
),
525 crypt_get_volume_key_size(cd
)*8,
526 crypt_get_device_name(cd
));
529 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
531 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
537 STRV_FOREACH(p
, passwords
) {
539 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
541 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
551 static int help(void) {
553 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
554 "%s detach VOLUME\n\n"
555 "Attaches or detaches an encrypted block device.\n",
556 program_invocation_short_name
,
557 program_invocation_short_name
);
562 int main(int argc
, char *argv
[]) {
563 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
572 log_error("This program requires at least two arguments.");
576 log_set_target(LOG_TARGET_AUTO
);
577 log_parse_environment();
582 if (streq(argv
[1], "attach")) {
586 crypt_status_info status
;
587 const char *key_file
= NULL
;
589 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
592 log_error("attach requires at least two arguments.");
598 !streq(argv
[4], "-") &&
599 !streq(argv
[4], "none")) {
601 if (!path_is_absolute(argv
[4]))
602 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
607 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
608 if (parse_options(argv
[5]) < 0)
612 /* A delicious drop of snake oil */
613 mlockall(MCL_FUTURE
);
616 log_debug("LUKS header: %s", arg_header
);
617 r
= crypt_init(&cd
, arg_header
);
619 r
= crypt_init(&cd
, argv
[3]);
621 log_error_errno(r
, "crypt_init() failed: %m");
625 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
627 status
= crypt_status(cd
, argv
[2]);
628 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
629 log_info("Volume %s already active.", argv
[2]);
635 flags
|= CRYPT_ACTIVATE_READONLY
;
638 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
640 if (arg_timeout
== USEC_INFINITY
)
643 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
645 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
650 /* Ideally we'd do this on the open fd, but since this is just a
651 * warning it's OK to do this in two steps. */
652 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
653 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
656 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
657 _cleanup_strv_free_erase_
char **passwords
= NULL
;
660 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
667 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
668 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
670 r
= attach_luks_or_plain(cd
,
673 arg_header
? argv
[3] : NULL
,
683 log_error_errno(r
, "Failed to activate: %m");
687 log_warning("Invalid passphrase.");
690 if (arg_tries
!= 0 && tries
>= arg_tries
) {
691 log_error("Too many attempts; giving up.");
696 } else if (streq(argv
[1], "detach")) {
698 r
= crypt_init_by_name(&cd
, argv
[2]);
700 log_info("Volume %s already inactive.", argv
[2]);
705 log_error_errno(r
, "crypt_init_by_name() failed: %m");
709 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
711 r
= crypt_deactivate(cd
, argv
[2]);
713 log_error_errno(r
, "Failed to deactivate: %m");
718 log_error("Unknown verb %s.", argv
[1]);
728 strv_free(arg_tcrypt_keyfiles
);
730 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;