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 /* libcryptsetup define for any LUKS version, compatible with libcryptsetup 1.x */
43 #define CRYPT_LUKS NULL
47 #define ANY_LUKS "LUKS"
49 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
50 static char *arg_cipher
= NULL
;
51 static unsigned arg_key_size
= 0;
52 static int arg_key_slot
= CRYPT_ANY_SLOT
;
53 static unsigned arg_keyfile_size
= 0;
54 static unsigned arg_keyfile_offset
= 0;
55 static char *arg_hash
= NULL
;
56 static char *arg_header
= NULL
;
57 static unsigned arg_tries
= 3;
58 static bool arg_readonly
= false;
59 static bool arg_verify
= false;
60 static bool arg_discards
= false;
61 static bool arg_tcrypt_hidden
= false;
62 static bool arg_tcrypt_system
= false;
63 #ifdef CRYPT_TCRYPT_VERA_MODES
64 static bool arg_tcrypt_veracrypt
= false;
66 static char **arg_tcrypt_keyfiles
= NULL
;
67 static uint64_t arg_offset
= 0;
68 static uint64_t arg_skip
= 0;
69 static usec_t arg_timeout
= USEC_INFINITY
;
71 /* Options Debian's crypttab knows we don't:
81 static int parse_one_option(const char *option
) {
87 /* Handled outside of this tool */
88 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail"))
91 if ((val
= startswith(option
, "cipher="))) {
92 r
= free_and_strdup(&arg_cipher
, val
);
96 } else if ((val
= startswith(option
, "size="))) {
98 r
= safe_atou(val
, &arg_key_size
);
100 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
104 if (arg_key_size
% 8) {
105 log_error("size= not a multiple of 8, ignoring.");
111 } else if ((val
= startswith(option
, "key-slot="))) {
114 r
= safe_atoi(val
, &arg_key_slot
);
116 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
120 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
122 arg_type
= CRYPT_TCRYPT
;
123 if (path_is_absolute(val
)) {
124 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
127 log_error("Key file path \"%s\" is not absolute. Ignoring.", val
);
129 } else if ((val
= startswith(option
, "keyfile-size="))) {
131 r
= safe_atou(val
, &arg_keyfile_size
);
133 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
137 } else if ((val
= startswith(option
, "keyfile-offset="))) {
139 r
= safe_atou(val
, &arg_keyfile_offset
);
141 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
145 } else if ((val
= startswith(option
, "hash="))) {
146 r
= free_and_strdup(&arg_hash
, val
);
150 } else if ((val
= startswith(option
, "header="))) {
153 if (!path_is_absolute(val
)) {
154 log_error("Header path \"%s\" is not absolute, refusing.", val
);
159 log_error("Duplicate header= option, refusing.");
163 arg_header
= strdup(val
);
167 } else if ((val
= startswith(option
, "tries="))) {
169 r
= safe_atou(val
, &arg_tries
);
171 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
175 } else if (STR_IN_SET(option
, "readonly", "read-only"))
177 else if (streq(option
, "verify"))
179 else if (STR_IN_SET(option
, "allow-discards", "discard"))
181 else if (streq(option
, "luks"))
183 else if (streq(option
, "tcrypt"))
184 arg_type
= CRYPT_TCRYPT
;
185 else if (streq(option
, "tcrypt-hidden")) {
186 arg_type
= CRYPT_TCRYPT
;
187 arg_tcrypt_hidden
= true;
188 } else if (streq(option
, "tcrypt-system")) {
189 arg_type
= CRYPT_TCRYPT
;
190 arg_tcrypt_system
= true;
191 } else if (streq(option
, "tcrypt-veracrypt")) {
192 #ifdef CRYPT_TCRYPT_VERA_MODES
193 arg_type
= CRYPT_TCRYPT
;
194 arg_tcrypt_veracrypt
= true;
196 log_error("This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
199 } else if (STR_IN_SET(option
, "plain", "swap", "tmp"))
200 arg_type
= CRYPT_PLAIN
;
201 else if ((val
= startswith(option
, "timeout="))) {
203 r
= parse_sec_fix_0(val
, &arg_timeout
);
205 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
209 } else if ((val
= startswith(option
, "offset="))) {
211 r
= safe_atou64(val
, &arg_offset
);
213 return log_error_errno(r
, "Failed to parse %s: %m", option
);
215 } else if ((val
= startswith(option
, "skip="))) {
217 r
= safe_atou64(val
, &arg_skip
);
219 return log_error_errno(r
, "Failed to parse %s: %m", option
);
221 } else if (!streq(option
, "none"))
222 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
227 static int parse_options(const char *options
) {
228 const char *word
, *state
;
234 FOREACH_WORD_SEPARATOR(word
, l
, options
, ",", state
) {
235 _cleanup_free_
char *o
;
237 o
= strndup(word
, l
);
240 r
= parse_one_option(o
);
245 /* sanity-check options */
246 if (arg_type
!= NULL
&& !streq(arg_type
, CRYPT_PLAIN
)) {
248 log_warning("offset= ignored with type %s", arg_type
);
250 log_warning("skip= ignored with type %s", arg_type
);
256 static void log_glue(int level
, const char *msg
, void *usrptr
) {
257 log_debug("%s", msg
);
260 static int disk_major_minor(const char *path
, char **ret
) {
265 if (stat(path
, &st
) < 0)
268 if (!S_ISBLK(st
.st_mode
))
271 if (asprintf(ret
, "/dev/block/%d:%d", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
277 static char* disk_description(const char *path
) {
279 static const char name_fields
[] =
280 "ID_PART_ENTRY_NAME\0"
282 "ID_MODEL_FROM_DATABASE\0"
285 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
292 if (stat(path
, &st
) < 0)
295 if (!S_ISBLK(st
.st_mode
))
298 r
= sd_device_new_from_devnum(&device
, 'b', st
.st_rdev
);
302 NULSTR_FOREACH(i
, name_fields
) {
305 r
= sd_device_get_property_value(device
, i
, &name
);
306 if (r
>= 0 && !isempty(name
))
313 static char *disk_mount_point(const char *label
) {
314 _cleanup_free_
char *device
= NULL
;
315 _cleanup_endmntent_
FILE *f
= NULL
;
318 /* Yeah, we don't support native systemd unit files here for now */
320 if (asprintf(&device
, "/dev/mapper/%s", label
) < 0)
323 f
= setmntent("/etc/fstab", "re");
327 while ((m
= getmntent(f
)))
328 if (path_equal(m
->mnt_fsname
, device
))
329 return strdup(m
->mnt_dir
);
334 static int get_password(const char *vol
, const char *src
, usec_t until
, bool accept_cached
, char ***ret
) {
335 _cleanup_free_
char *description
= NULL
, *name_buffer
= NULL
, *mount_point
= NULL
, *maj_min
= NULL
, *text
= NULL
, *escaped_name
= NULL
;
336 _cleanup_strv_free_erase_
char **passwords
= NULL
;
337 const char *name
= NULL
;
345 description
= disk_description(src
);
346 mount_point
= disk_mount_point(vol
);
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)
370 (void) disk_major_minor(src
, &maj_min
);
373 escaped_name
= maj_min
;
376 escaped_name
= cescape(name
);
381 id
= strjoina("cryptsetup:", escaped_name
);
383 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
,
384 ASK_PASSWORD_PUSH_CACHE
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
387 return log_error_errno(r
, "Failed to query password: %m");
390 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
392 assert(strv_length(passwords
) == 1);
394 if (asprintf(&text
, "Please enter passphrase for disk %s! (verification)", name
) < 0)
397 id
= strjoina("cryptsetup-verification:", escaped_name
);
399 r
= ask_password_auto(text
, "drive-harddisk", id
, "cryptsetup", until
, ASK_PASSWORD_PUSH_CACHE
, &passwords2
);
401 return log_error_errno(r
, "Failed to query verification password: %m");
403 assert(strv_length(passwords2
) == 1);
405 if (!streq(passwords
[0], passwords2
[0])) {
406 log_warning("Passwords did not match, retrying.");
411 strv_uniq(passwords
);
413 STRV_FOREACH(p
, passwords
) {
416 if (strlen(*p
)+1 >= arg_key_size
)
419 /* Pad password if necessary */
420 c
= new(char, arg_key_size
);
424 strncpy(c
, *p
, arg_key_size
);
435 static int attach_tcrypt(
436 struct crypt_device
*cd
,
438 const char *key_file
,
443 _cleanup_free_
char *passphrase
= NULL
;
444 struct crypt_params_tcrypt params
= {
445 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
446 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
447 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
452 assert(key_file
|| (passwords
&& passwords
[0]));
454 if (arg_tcrypt_hidden
)
455 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
457 if (arg_tcrypt_system
)
458 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
460 #ifdef CRYPT_TCRYPT_VERA_MODES
461 if (arg_tcrypt_veracrypt
)
462 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
466 r
= read_one_line_file(key_file
, &passphrase
);
468 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
472 params
.passphrase
= passphrase
;
474 params
.passphrase
= passwords
[0];
475 params
.passphrase_size
= strlen(params
.passphrase
);
477 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
479 if (key_file
&& r
== -EPERM
) {
480 log_error("Failed to activate using password file '%s'.", key_file
);
486 return crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
489 static int attach_luks_or_plain(struct crypt_device
*cd
,
491 const char *key_file
,
492 const char *data_device
,
496 bool pass_volume_key
= false;
500 assert(key_file
|| passwords
);
502 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
)) {
503 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
505 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd
));
510 r
= crypt_set_data_device(cd
, data_device
);
513 if ((!arg_type
&& r
< 0) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
514 struct crypt_params_plain params
= {
515 .offset
= arg_offset
,
518 const char *cipher
, *cipher_mode
;
519 _cleanup_free_
char *truncated_cipher
= NULL
;
522 /* plain isn't a real hash type. it just means "use no hash" */
523 if (!streq(arg_hash
, "plain"))
524 params
.hash
= arg_hash
;
525 } else if (!key_file
)
526 /* for CRYPT_PLAIN, the behaviour of cryptsetup
527 * package is to not hash when a key file is provided */
528 params
.hash
= "ripemd160";
533 l
= strcspn(arg_cipher
, "-");
534 truncated_cipher
= strndup(arg_cipher
, l
);
535 if (!truncated_cipher
)
538 cipher
= truncated_cipher
;
539 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
542 cipher_mode
= "cbc-essiv:sha256";
545 /* for CRYPT_PLAIN limit reads
546 * from keyfile to key length, and
547 * ignore keyfile-size */
548 arg_keyfile_size
= arg_key_size
;
550 /* In contrast to what the name
551 * crypt_setup() might suggest this
552 * doesn't actually format anything,
553 * it just configures encryption
554 * parameters when used for plain
556 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
558 /* hash == NULL implies the user passed "plain" */
559 pass_volume_key
= (params
.hash
== NULL
);
563 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
565 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
566 crypt_get_cipher(cd
),
567 crypt_get_cipher_mode(cd
),
568 crypt_get_volume_key_size(cd
)*8,
569 crypt_get_device_name(cd
));
572 r
= crypt_activate_by_keyfile_offset(cd
, name
, arg_key_slot
, key_file
, arg_keyfile_size
, arg_keyfile_offset
, flags
);
574 log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
580 STRV_FOREACH(p
, passwords
) {
582 r
= crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
584 r
= crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
594 static int help(void) {
596 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
597 "%s detach VOLUME\n\n"
598 "Attaches or detaches an encrypted block device.\n",
599 program_invocation_short_name
,
600 program_invocation_short_name
);
605 int main(int argc
, char *argv
[]) {
606 struct crypt_device
*cd
= NULL
;
615 log_error("This program requires at least two arguments.");
619 log_set_target(LOG_TARGET_AUTO
);
620 log_parse_environment();
625 if (streq(argv
[1], "attach")) {
629 crypt_status_info status
;
630 const char *key_file
= NULL
;
632 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
635 log_error("attach requires at least two arguments.");
641 !streq(argv
[4], "-") &&
642 !streq(argv
[4], "none")) {
644 if (!path_is_absolute(argv
[4]))
645 log_error("Password file path '%s' is not absolute. Ignoring.", argv
[4]);
650 if (argc
>= 6 && argv
[5][0] && !streq(argv
[5], "-")) {
651 if (parse_options(argv
[5]) < 0)
655 /* A delicious drop of snake oil */
656 mlockall(MCL_FUTURE
);
659 log_debug("LUKS header: %s", arg_header
);
660 r
= crypt_init(&cd
, arg_header
);
662 r
= crypt_init(&cd
, argv
[3]);
664 log_error_errno(r
, "crypt_init() failed: %m");
668 crypt_set_log_callback(cd
, log_glue
, NULL
);
670 status
= crypt_status(cd
, argv
[2]);
671 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
672 log_info("Volume %s already active.", argv
[2]);
678 flags
|= CRYPT_ACTIVATE_READONLY
;
681 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
683 if (arg_timeout
== USEC_INFINITY
)
686 until
= now(CLOCK_MONOTONIC
) + arg_timeout
;
688 arg_key_size
= (arg_key_size
> 0 ? arg_key_size
: (256 / 8));
693 /* Ideally we'd do this on the open fd, but since this is just a
694 * warning it's OK to do this in two steps. */
695 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
696 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
699 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
700 _cleanup_strv_free_erase_
char **passwords
= NULL
;
703 r
= get_password(argv
[2], argv
[3], until
, tries
== 0 && !arg_verify
, &passwords
);
710 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
711 r
= attach_tcrypt(cd
, argv
[2], key_file
, passwords
, flags
);
713 r
= attach_luks_or_plain(cd
,
716 arg_header
? argv
[3] : NULL
,
726 log_error_errno(r
, "Failed to activate: %m");
730 log_warning("Invalid passphrase.");
733 if (arg_tries
!= 0 && tries
>= arg_tries
) {
734 log_error("Too many attempts; giving up.");
739 } else if (streq(argv
[1], "detach")) {
741 r
= crypt_init_by_name(&cd
, argv
[2]);
743 log_info("Volume %s already inactive.", argv
[2]);
748 log_error_errno(r
, "crypt_init_by_name() failed: %m");
752 crypt_set_log_callback(cd
, log_glue
, NULL
);
754 r
= crypt_deactivate(cd
, argv
[2]);
756 log_error_errno(r
, "Failed to deactivate: %m");
761 log_error("Unknown verb %s.", argv
[1]);
774 strv_free(arg_tcrypt_keyfiles
);
776 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;