2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <libcryptsetup.h>
26 #include "sd-device.h"
28 #include "alloc-util.h"
29 #include "ask-password-api.h"
30 #include "device-util.h"
34 #include "mount-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "string-util.h"
41 static const char *arg_type
= NULL
; /* CRYPT_LUKS1, CRYPT_TCRYPT or CRYPT_PLAIN */
42 static char *arg_cipher
= NULL
;
43 static unsigned arg_key_size
= 0;
44 static int arg_key_slot
= CRYPT_ANY_SLOT
;
45 static unsigned arg_keyfile_size
= 0;
46 static unsigned arg_keyfile_offset
= 0;
47 static char *arg_hash
= NULL
;
48 static char *arg_header
= NULL
;
49 static unsigned arg_tries
= 3;
50 static bool arg_readonly
= false;
51 static bool arg_verify
= false;
52 static bool arg_discards
= false;
53 static bool arg_tcrypt_hidden
= false;
54 static bool arg_tcrypt_system
= false;
55 static bool arg_tcrypt_veracrypt
= false;
56 static char **arg_tcrypt_keyfiles
= NULL
;
57 static uint64_t arg_offset
= 0;
58 static uint64_t arg_skip
= 0;
59 static usec_t arg_timeout
= 0;
61 /* Options Debian's crypttab knows we don't:
71 static int parse_one_option(const char *option
) {
77 /* Handled outside of this tool */
78 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail"))
81 if ((val
= startswith(option
, "cipher="))) {
82 r
= free_and_strdup(&arg_cipher
, val
);
86 } else if ((val
= startswith(option
, "size="))) {
88 r
= safe_atou(val
, &arg_key_size
);
90 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
94 if (arg_key_size
% 8) {
95 log_error("size= not a multiple of 8, ignoring.");
101 } else if ((val
= startswith(option
, "key-slot="))) {
103 arg_type
= CRYPT_LUKS1
;
104 r
= safe_atoi(val
, &arg_key_slot
);
106 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
110 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
112 arg_type
= CRYPT_TCRYPT
;
113 if (path_is_absolute(val
)) {
114 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
117 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
119 } else if ((val
= startswith(option
, "keyfile-size="))) {
121 r
= safe_atou(val
, &arg_keyfile_size
);
123 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
127 } else if ((val
= startswith(option
, "keyfile-offset="))) {
129 r
= safe_atou(val
, &arg_keyfile_offset
);
131 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
135 } else if ((val
= startswith(option
, "hash="))) {
136 r
= free_and_strdup(&arg_hash
, val
);
140 } else if ((val
= startswith(option
, "header="))) {
141 arg_type
= CRYPT_LUKS1
;
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"))
172 arg_type
= CRYPT_LUKS1
;
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(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 void log_glue(int level
, const char *msg
, void *usrptr
) {
247 log_debug("%s", msg
);
250 static int disk_major_minor(const char *path
, char **ret
) {
255 if (stat(path
, &st
) < 0)
258 if (!S_ISBLK(st
.st_mode
))
261 if (asprintf(ret
, "/dev/block/%d:%d", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
267 static char* disk_description(const char *path
) {
269 static const char name_fields
[] =
270 "ID_PART_ENTRY_NAME\0"
272 "ID_MODEL_FROM_DATABASE\0"
275 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
282 if (stat(path
, &st
) < 0)
285 if (!S_ISBLK(st
.st_mode
))
288 r
= sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
);
292 NULSTR_FOREACH(i
, name_fields
) {
295 r
= sd_device_get_property_value(device
, i
, &name
);
296 if (r
>= 0 && !isempty(name
))
303 static char *disk_mount_point(const char *label
) {
304 _cleanup_free_
char *device
= NULL
;
305 _cleanup_endmntent_
FILE *f
= NULL
;
308 /* Yeah, we don't support native systemd unit files here for now */
310 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
313 f
= setmntent("/etc/fstab", "re");
317 while ((m
= getmntent(f
)))
318 if (path_equal(m
->mnt_fsname
, device
))
319 return strdup(m
->mnt_dir
);
324 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
325 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *maj_min
= NULL
, *text
= NULL
, *escaped_name
= NULL
;
326 _cleanup_strv_free_erase_
char **passwords
= NULL
;
327 const char *name
= NULL
;
335 description
= disk_description(src
);
336 mount_point
= disk_mount_point(vol
);
338 if (description
&& streq(vol
, description
))
339 /* If the description string is simply the
340 * volume name, then let's not show this
342 description
= mfree(description
);
344 if (mount_point
&& description
)
345 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
346 else if (mount_point
)
347 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
348 else if (description
)
349 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
354 name
= name_buffer
? name_buffer
: vol
;
356 if (asprintf(&text
, "Please enter passphrase for disk %s!", name
) < 0)
360 (void) disk_major_minor(src
, &maj_min
);
363 escaped_name
= maj_min
;
366 escaped_name
= cescape(name
);
371 id
= strjoina("cryptsetup:", escaped_name
);
373 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
374 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
377 return log_error_errno(r
, "Failed to query password: %m");
380 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
382 assert(strv_length(passwords
) == 1);
384 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
387 id
= strjoina("cryptsetup-verification:", escaped_name
);
389 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
391 return log_error_errno(r
, "Failed to query verification password: %m");
393 assert(strv_length(passwords2
) == 1);
395 if (!streq(passwords
[0], passwords2
[0])) {
396 log_warning("Passwords did not match, retrying.");
401 strv_uniq(passwords
);
403 STRV_FOREACH(p
, passwords
) {
406 if (strlen(*p
)+1 >= arg_key_size
)
409 /* Pad password if necessary */
410 c
= new(char, arg_key_size
);
414 strncpy(c
, *p
, arg_key_size
);
425 static int attach_tcrypt(
426 struct crypt_device
*cd
,
428 const char *key_file
,
433 _cleanup_free_
char *passphrase
= NULL
;
434 struct crypt_params_tcrypt params
= {
435 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
436 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
437 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
442 assert(key_file
|| (passwords
&& passwords
[0]));
444 if (arg_tcrypt_hidden
)
445 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
447 if (arg_tcrypt_system
)
448 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
450 #ifdef CRYPT_TCRYPT_VERA_MODES
451 if (arg_tcrypt_veracrypt
)
452 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
456 r
= read_one_line_file(key_file
, &passphrase
);
458 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
462 params
.passphrase
= passphrase
;
464 params
.passphrase
= passwords
[0];
465 params
.passphrase_size
= strlen(params
.passphrase
);
467 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
469 if (key_file
&& r
== -EPERM
) {
470 log_error("Failed to activate using password file '%s'.", key_file
);
476 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
479 static int attach_luks_or_plain(struct crypt_device
*cd
,
481 const char *key_file
,
482 const char *data_device
,
486 bool pass_volume_key
= false;
490 assert(key_file
|| passwords
);
492 if (!arg_type
|| streq(arg_type
, CRYPT_LUKS1
)) {
493 r
= crypt_load(cd
, CRYPT_LUKS1
, NULL
);
495 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
500 r
= crypt_set_data_device(cd
, data_device
);
503 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
504 struct crypt_params_plain params
= {
505 .offset
= arg_offset
,
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) {
586 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
587 "%s detach VOLUME\n\n"
588 "Attaches or detaches an encrypted block device.\n",
589 program_invocation_short_name
,
590 program_invocation_short_name
);
595 int main(int argc
, char *argv
[]) {
596 struct crypt_device
*cd
= NULL
;
605 log_error("This program requires at least two arguments.");
609 log_set_target(LOG_TARGET_AUTO
);
610 log_parse_environment();
615 if (streq(argv
[1], "attach")) {
619 crypt_status_info status
;
620 const char *key_file
= NULL
;
622 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
625 log_error("attach requires at least two arguments.");
631 !streq(argv
[4], "-") &&
632 !streq(argv
[4], "none")) {
634 if (!path_is_absolute(argv
[4]))
635 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
640 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
641 if (parse_options(argv
[5]) < 0)
645 /* A delicious drop of snake oil */
646 mlockall(MCL_FUTURE
);
649 log_debug("LUKS header: %s", arg_header
);
650 r
= crypt_init(&cd
, arg_header
);
652 r
= crypt_init(&cd
, argv
[3]);
654 log_error_errno(r
, "crypt_init() failed: %m");
658 crypt_set_log_callback(cd
, log_glue
, NULL
);
660 status
= crypt_status(cd
, argv
[2]);
661 if (status
== CRYPT_ACTIVE
|| status
== CRYPT_BUSY
) {
662 log_info("Volume %s already active.", argv
[2]);
668 flags
|= CRYPT_ACTIVATE_READONLY
;
671 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
674 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 log_error_errno(r
, "Failed to activate: %m");
720 log_warning("Invalid passphrase.");
723 if (arg_tries
!= 0 && tries
>= arg_tries
) {
724 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]);
738 log_error_errno(r
, "crypt_init_by_name() failed: %m");
742 crypt_set_log_callback(cd
, log_glue
, NULL
);
744 r
= crypt_deactivate(cd
, argv
[2]);
746 log_error_errno(r
, "Failed to deactivate: %m");
751 log_error("Unknown verb %s.", argv
[1]);
764 strv_free(arg_tcrypt_keyfiles
);
766 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;