]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup.c
cryptsetup: minor modernizations
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <mntent.h>
5 #include <sys/mman.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include "sd-device.h"
11
12 #include "alloc-util.h"
13 #include "ask-password-api.h"
14 #include "cryptsetup-fido2.h"
15 #include "cryptsetup-keyfile.h"
16 #include "cryptsetup-pkcs11.h"
17 #include "cryptsetup-tpm2.h"
18 #include "cryptsetup-util.h"
19 #include "device-util.h"
20 #include "efi-loader.h"
21 #include "env-util.h"
22 #include "escape.h"
23 #include "fileio.h"
24 #include "fs-util.h"
25 #include "fstab-util.h"
26 #include "hexdecoct.h"
27 #include "libfido2-util.h"
28 #include "log.h"
29 #include "main-func.h"
30 #include "memory-util.h"
31 #include "mount-util.h"
32 #include "nulstr-util.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "pkcs11-util.h"
36 #include "pretty-print.h"
37 #include "random-util.h"
38 #include "string-util.h"
39 #include "strv.h"
40 #include "tpm2-util.h"
41
42 /* internal helper */
43 #define ANY_LUKS "LUKS"
44 /* as in src/cryptsetup.h */
45 #define CRYPT_SECTOR_SIZE 512
46 #define CRYPT_MAX_SECTOR_SIZE 4096
47
48 static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */
49 static char *arg_cipher = NULL;
50 static unsigned arg_key_size = 0;
51 static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
52 static int arg_key_slot = CRYPT_ANY_SLOT;
53 static unsigned arg_keyfile_size = 0;
54 static uint64_t arg_keyfile_offset = 0;
55 static bool arg_keyfile_erase = false;
56 static bool arg_try_empty_password = false;
57 static char *arg_hash = NULL;
58 static char *arg_header = NULL;
59 static unsigned arg_tries = 3;
60 static bool arg_readonly = false;
61 static bool arg_verify = false;
62 static AskPasswordFlags arg_ask_password_flags = 0;
63 static bool arg_discards = false;
64 static bool arg_same_cpu_crypt = false;
65 static bool arg_submit_from_crypt_cpus = false;
66 static bool arg_no_read_workqueue = false;
67 static bool arg_no_write_workqueue = false;
68 static bool arg_tcrypt_hidden = false;
69 static bool arg_tcrypt_system = false;
70 static bool arg_tcrypt_veracrypt = false;
71 static char **arg_tcrypt_keyfiles = NULL;
72 static uint64_t arg_offset = 0;
73 static uint64_t arg_skip = 0;
74 static usec_t arg_timeout = USEC_INFINITY;
75 static char *arg_pkcs11_uri = NULL;
76 static bool arg_pkcs11_uri_auto = false;
77 static char *arg_fido2_device = NULL;
78 static bool arg_fido2_device_auto = false;
79 static void *arg_fido2_cid = NULL;
80 static size_t arg_fido2_cid_size = 0;
81 static char *arg_fido2_rp_id = NULL;
82 static char *arg_tpm2_device = NULL;
83 static bool arg_tpm2_device_auto = false;
84 static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
85 static bool arg_headless = false;
86 static usec_t arg_token_timeout_usec = 30*USEC_PER_SEC;
87
88 STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
89 STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
90 STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
91 STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
92 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep);
93 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep);
94 STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep);
95 STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep);
96 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
97
98 /* Options Debian's crypttab knows we don't:
99
100 check=
101 checkargs=
102 noearly
103 loud
104 quiet
105 keyscript=
106 initramfs
107 */
108
109 static int parse_one_option(const char *option) {
110 const char *val;
111 int r;
112
113 assert(option);
114
115 /* Handled outside of this tool */
116 if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
117 return 0;
118
119 if (startswith(option, "keyfile-timeout="))
120 return 0;
121
122 if ((val = startswith(option, "cipher="))) {
123 r = free_and_strdup(&arg_cipher, val);
124 if (r < 0)
125 return log_oom();
126
127 } else if ((val = startswith(option, "size="))) {
128
129 r = safe_atou(val, &arg_key_size);
130 if (r < 0) {
131 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
132 return 0;
133 }
134
135 if (arg_key_size % 8) {
136 log_error("size= not a multiple of 8, ignoring.");
137 return 0;
138 }
139
140 arg_key_size /= 8;
141
142 } else if ((val = startswith(option, "sector-size="))) {
143
144 r = safe_atou(val, &arg_sector_size);
145 if (r < 0) {
146 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
147 return 0;
148 }
149
150 if (arg_sector_size % 2) {
151 log_error("sector-size= not a multiple of 2, ignoring.");
152 return 0;
153 }
154
155 if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) {
156 log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
157 return 0;
158 }
159
160 } else if ((val = startswith(option, "key-slot=")) ||
161 (val = startswith(option, "keyslot="))) {
162
163 arg_type = ANY_LUKS;
164 r = safe_atoi(val, &arg_key_slot);
165 if (r < 0) {
166 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
167 return 0;
168 }
169
170 } else if ((val = startswith(option, "tcrypt-keyfile="))) {
171
172 arg_type = CRYPT_TCRYPT;
173 if (path_is_absolute(val)) {
174 if (strv_extend(&arg_tcrypt_keyfiles, val) < 0)
175 return log_oom();
176 } else
177 log_error("Key file path \"%s\" is not absolute. Ignoring.", val);
178
179 } else if ((val = startswith(option, "keyfile-size="))) {
180
181 r = safe_atou(val, &arg_keyfile_size);
182 if (r < 0) {
183 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
184 return 0;
185 }
186
187 } else if ((val = startswith(option, "keyfile-offset="))) {
188
189 r = safe_atou64(val, &arg_keyfile_offset);
190 if (r < 0) {
191 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
192 return 0;
193 }
194
195 } else if ((val = startswith(option, "keyfile-erase="))) {
196
197 r = parse_boolean(val);
198 if (r < 0) {
199 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
200 return 0;
201 }
202
203 arg_keyfile_erase = r;
204
205 } else if (streq(option, "keyfile-erase"))
206 arg_keyfile_erase = true;
207
208 else if ((val = startswith(option, "hash="))) {
209 r = free_and_strdup(&arg_hash, val);
210 if (r < 0)
211 return log_oom();
212
213 } else if ((val = startswith(option, "header="))) {
214 arg_type = ANY_LUKS;
215
216 if (!path_is_absolute(val))
217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
218 "Header path \"%s\" is not absolute, refusing.", val);
219
220 if (arg_header)
221 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
222 "Duplicate header= option, refusing.");
223
224 arg_header = strdup(val);
225 if (!arg_header)
226 return log_oom();
227
228 } else if ((val = startswith(option, "tries="))) {
229
230 r = safe_atou(val, &arg_tries);
231 if (r < 0) {
232 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
233 return 0;
234 }
235
236 } else if (STR_IN_SET(option, "readonly", "read-only"))
237 arg_readonly = true;
238 else if (streq(option, "verify"))
239 arg_verify = true;
240 else if ((val = startswith(option, "password-echo="))) {
241 if (streq(val, "masked"))
242 arg_ask_password_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT);
243 else {
244 r = parse_boolean(val);
245 if (r < 0) {
246 log_warning_errno(r, "Invalid password-echo= option \"%s\", ignoring.", val);
247 return 0;
248 }
249
250 SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r);
251 SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r);
252 }
253 } else if (STR_IN_SET(option, "allow-discards", "discard"))
254 arg_discards = true;
255 else if (streq(option, "same-cpu-crypt"))
256 arg_same_cpu_crypt = true;
257 else if (streq(option, "submit-from-crypt-cpus"))
258 arg_submit_from_crypt_cpus = true;
259 else if (streq(option, "no-read-workqueue"))
260 arg_no_read_workqueue = true;
261 else if (streq(option, "no-write-workqueue"))
262 arg_no_write_workqueue = true;
263 else if (streq(option, "luks"))
264 arg_type = ANY_LUKS;
265 /* since cryptsetup 2.3.0 (Feb 2020) */
266 #ifdef CRYPT_BITLK
267 else if (streq(option, "bitlk"))
268 arg_type = CRYPT_BITLK;
269 #endif
270 else if (streq(option, "tcrypt"))
271 arg_type = CRYPT_TCRYPT;
272 else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) {
273 arg_type = CRYPT_TCRYPT;
274 arg_tcrypt_hidden = true;
275 } else if (streq(option, "tcrypt-system")) {
276 arg_type = CRYPT_TCRYPT;
277 arg_tcrypt_system = true;
278 } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) {
279 arg_type = CRYPT_TCRYPT;
280 arg_tcrypt_veracrypt = true;
281 } else if (STR_IN_SET(option, "plain", "swap", "tmp") ||
282 startswith(option, "tmp="))
283 arg_type = CRYPT_PLAIN;
284 else if ((val = startswith(option, "timeout="))) {
285
286 r = parse_sec_fix_0(val, &arg_timeout);
287 if (r < 0) {
288 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
289 return 0;
290 }
291
292 } else if ((val = startswith(option, "offset="))) {
293
294 r = safe_atou64(val, &arg_offset);
295 if (r < 0)
296 return log_error_errno(r, "Failed to parse %s: %m", option);
297
298 } else if ((val = startswith(option, "skip="))) {
299
300 r = safe_atou64(val, &arg_skip);
301 if (r < 0)
302 return log_error_errno(r, "Failed to parse %s: %m", option);
303
304 } else if ((val = startswith(option, "pkcs11-uri="))) {
305
306 if (streq(val, "auto")) {
307 arg_pkcs11_uri = mfree(arg_pkcs11_uri);
308 arg_pkcs11_uri_auto = true;
309 } else {
310 if (!pkcs11_uri_valid(val))
311 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing");
312
313 r = free_and_strdup(&arg_pkcs11_uri, val);
314 if (r < 0)
315 return log_oom();
316
317 arg_pkcs11_uri_auto = false;
318 }
319
320 } else if ((val = startswith(option, "fido2-device="))) {
321
322 if (streq(val, "auto")) {
323 arg_fido2_device = mfree(arg_fido2_device);
324 arg_fido2_device_auto = true;
325 } else {
326 r = free_and_strdup(&arg_fido2_device, val);
327 if (r < 0)
328 return log_oom();
329
330 arg_fido2_device_auto = false;
331 }
332
333 } else if ((val = startswith(option, "fido2-cid="))) {
334
335 if (streq(val, "auto"))
336 arg_fido2_cid = mfree(arg_fido2_cid);
337 else {
338 _cleanup_free_ void *cid = NULL;
339 size_t cid_size;
340
341 r = unbase64mem(val, SIZE_MAX, &cid, &cid_size);
342 if (r < 0)
343 return log_error_errno(r, "Failed to decode FIDO2 CID data: %m");
344
345 free(arg_fido2_cid);
346 arg_fido2_cid = TAKE_PTR(cid);
347 arg_fido2_cid_size = cid_size;
348 }
349
350 /* Turn on FIDO2 as side-effect, if not turned on yet. */
351 if (!arg_fido2_device && !arg_fido2_device_auto)
352 arg_fido2_device_auto = true;
353
354 } else if ((val = startswith(option, "fido2-rp="))) {
355
356 r = free_and_strdup(&arg_fido2_rp_id, val);
357 if (r < 0)
358 return log_oom();
359
360 } else if ((val = startswith(option, "tpm2-device="))) {
361
362 if (streq(val, "auto")) {
363 arg_tpm2_device = mfree(arg_tpm2_device);
364 arg_tpm2_device_auto = true;
365 } else {
366 r = free_and_strdup(&arg_tpm2_device, val);
367 if (r < 0)
368 return log_oom();
369
370 arg_tpm2_device_auto = false;
371 }
372
373 } else if ((val = startswith(option, "tpm2-pcrs="))) {
374
375 if (isempty(val))
376 arg_tpm2_pcr_mask = 0;
377 else {
378 uint32_t mask;
379
380 r = tpm2_parse_pcrs(val, &mask);
381 if (r < 0)
382 return r;
383
384 if (arg_tpm2_pcr_mask == UINT32_MAX)
385 arg_tpm2_pcr_mask = mask;
386 else
387 arg_tpm2_pcr_mask |= mask;
388 }
389
390 } else if ((val = startswith(option, "try-empty-password="))) {
391
392 r = parse_boolean(val);
393 if (r < 0) {
394 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
395 return 0;
396 }
397
398 arg_try_empty_password = r;
399
400 } else if (streq(option, "try-empty-password"))
401 arg_try_empty_password = true;
402 else if ((val = startswith(option, "headless="))) {
403
404 r = parse_boolean(val);
405 if (r < 0) {
406 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
407 return 0;
408 }
409
410 arg_headless = r;
411 } else if (streq(option, "headless"))
412 arg_headless = true;
413
414 else if ((val = startswith(option, "token-timeout="))) {
415
416 r = parse_sec_fix_0(val, &arg_token_timeout_usec);
417 if (r < 0) {
418 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
419 return 0;
420 }
421
422 } else if (!streq(option, "x-initrd.attach"))
423 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
424
425 return 0;
426 }
427
428 static int parse_options(const char *options) {
429 assert(options);
430
431 for (;;) {
432 _cleanup_free_ char *word = NULL;
433 int r;
434
435 r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
436 if (r < 0)
437 return log_error_errno(r, "Failed to parse options: %m");
438 if (r == 0)
439 break;
440
441 r = parse_one_option(word);
442 if (r < 0)
443 return r;
444 }
445
446 /* sanity-check options */
447 if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
448 if (arg_offset != 0)
449 log_warning("offset= ignored with type %s", arg_type);
450 if (arg_skip != 0)
451 log_warning("skip= ignored with type %s", arg_type);
452 }
453
454 return 0;
455 }
456
457 static char* disk_description(const char *path) {
458 static const char name_fields[] =
459 "DM_NAME\0"
460 "ID_MODEL_FROM_DATABASE\0"
461 "ID_MODEL\0";
462
463 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
464 const char *i, *name;
465 struct stat st;
466
467 assert(path);
468
469 if (stat(path, &st) < 0)
470 return NULL;
471
472 if (!S_ISBLK(st.st_mode))
473 return NULL;
474
475 if (sd_device_new_from_stat_rdev(&device, &st) < 0)
476 return NULL;
477
478 if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
479 _cleanup_free_ char *unescaped = NULL;
480 ssize_t l;
481
482 /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
483 * reverse this here to make the string more human friendly in case people embed spaces or
484 * other weird stuff. */
485
486 l = cunescape(name, UNESCAPE_RELAX, &unescaped);
487 if (l < 0) {
488 log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
489 return NULL;
490 }
491
492 if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
493 return TAKE_PTR(unescaped);
494 }
495
496 /* These need no unescaping. */
497 NULSTR_FOREACH(i, name_fields)
498 if (sd_device_get_property_value(device, i, &name) >= 0 &&
499 !isempty(name))
500 return strdup(name);
501
502 return NULL;
503 }
504
505 static char *disk_mount_point(const char *label) {
506 _cleanup_free_ char *device = NULL;
507 _cleanup_endmntent_ FILE *f = NULL;
508 struct mntent *m;
509
510 /* Yeah, we don't support native systemd unit files here for now */
511
512 device = strjoin("/dev/mapper/", label);
513 if (!device)
514 return NULL;
515
516 f = setmntent(fstab_path(), "re");
517 if (!f)
518 return NULL;
519
520 while ((m = getmntent(f)))
521 if (path_equal(m->mnt_fsname, device))
522 return strdup(m->mnt_dir);
523
524 return NULL;
525 }
526
527 static char *friendly_disk_name(const char *src, const char *vol) {
528 _cleanup_free_ char *description = NULL, *mount_point = NULL;
529 char *name_buffer = NULL;
530 int r;
531
532 assert(src);
533 assert(vol);
534
535 description = disk_description(src);
536 mount_point = disk_mount_point(vol);
537
538 /* If the description string is simply the volume name, then let's not show this twice */
539 if (description && streq(vol, description))
540 description = mfree(description);
541
542 if (mount_point && description)
543 r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
544 else if (mount_point)
545 r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
546 else if (description)
547 r = asprintf(&name_buffer, "%s (%s)", description, vol);
548 else
549 return strdup(vol);
550 if (r < 0)
551 return NULL;
552
553 return name_buffer;
554 }
555
556 static int get_password(
557 const char *vol,
558 const char *src,
559 usec_t until,
560 bool accept_cached,
561 char ***ret) {
562
563 _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL;
564 _cleanup_strv_free_erase_ char **passwords = NULL;
565 char **p, *id;
566 int r = 0;
567 AskPasswordFlags flags = arg_ask_password_flags | ASK_PASSWORD_PUSH_CACHE;
568
569 assert(vol);
570 assert(src);
571 assert(ret);
572
573 if (arg_headless)
574 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
575
576 friendly = friendly_disk_name(src, vol);
577 if (!friendly)
578 return log_oom();
579
580 if (asprintf(&text, "Please enter passphrase for disk %s:", friendly) < 0)
581 return log_oom();
582
583 disk_path = cescape(src);
584 if (!disk_path)
585 return log_oom();
586
587 id = strjoina("cryptsetup:", disk_path);
588
589 r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until,
590 flags | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED),
591 &passwords);
592 if (r < 0)
593 return log_error_errno(r, "Failed to query password: %m");
594
595 if (arg_verify) {
596 _cleanup_strv_free_erase_ char **passwords2 = NULL;
597
598 assert(strv_length(passwords) == 1);
599
600 if (asprintf(&text, "Please enter passphrase for disk %s (verification):", friendly) < 0)
601 return log_oom();
602
603 id = strjoina("cryptsetup-verification:", disk_path);
604
605 r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until, flags, &passwords2);
606 if (r < 0)
607 return log_error_errno(r, "Failed to query verification password: %m");
608
609 assert(strv_length(passwords2) == 1);
610
611 if (!streq(passwords[0], passwords2[0]))
612 return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
613 "Passwords did not match, retrying.");
614 }
615
616 strv_uniq(passwords);
617
618 STRV_FOREACH(p, passwords) {
619 char *c;
620
621 if (strlen(*p)+1 >= arg_key_size)
622 continue;
623
624 /* Pad password if necessary */
625 c = new(char, arg_key_size);
626 if (!c)
627 return log_oom();
628
629 strncpy(c, *p, arg_key_size);
630 erase_and_free(*p);
631 *p = TAKE_PTR(c);
632 }
633
634 *ret = TAKE_PTR(passwords);
635
636 return 0;
637 }
638
639 static int attach_tcrypt(
640 struct crypt_device *cd,
641 const char *name,
642 const char *key_file,
643 const void *key_data,
644 size_t key_data_size,
645 char **passwords,
646 uint32_t flags) {
647
648 int r = 0;
649 _cleanup_(erase_and_freep) char *passphrase = NULL;
650 struct crypt_params_tcrypt params = {
651 .flags = CRYPT_TCRYPT_LEGACY_MODES,
652 .keyfiles = (const char **)arg_tcrypt_keyfiles,
653 .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
654 };
655
656 assert(cd);
657 assert(name);
658 assert(key_file || key_data || !strv_isempty(passwords));
659
660 if (arg_pkcs11_uri || arg_pkcs11_uri_auto || arg_fido2_device || arg_fido2_device_auto || arg_tpm2_device || arg_tpm2_device_auto)
661 /* Ask for a regular password */
662 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
663 "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
664
665 if (arg_tcrypt_hidden)
666 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
667
668 if (arg_tcrypt_system)
669 params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
670
671 if (arg_tcrypt_veracrypt)
672 params.flags |= CRYPT_TCRYPT_VERA_MODES;
673
674 if (key_data) {
675 params.passphrase = key_data;
676 params.passphrase_size = key_data_size;
677 } else {
678 if (key_file) {
679 r = read_one_line_file(key_file, &passphrase);
680 if (r < 0) {
681 log_error_errno(r, "Failed to read password file '%s': %m", key_file);
682 return -EAGAIN; /* log with the actual error, but return EAGAIN */
683 }
684
685 params.passphrase = passphrase;
686 } else
687 params.passphrase = passwords[0];
688
689 params.passphrase_size = strlen(params.passphrase);
690 }
691
692 r = crypt_load(cd, CRYPT_TCRYPT, &params);
693 if (r < 0) {
694 if (r == -EPERM) {
695 if (key_data)
696 log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)");
697
698 if (key_file)
699 log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file);
700
701 return -EAGAIN; /* log the actual error, but return EAGAIN */
702 }
703
704 return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd));
705 }
706
707 r = crypt_activate_by_volume_key(cd, name, NULL, 0, flags);
708 if (r < 0)
709 return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd));
710
711 return 0;
712 }
713
714 static char *make_bindname(const char *volume) {
715 char *s;
716
717 if (asprintf(&s, "@%" PRIx64"/cryptsetup/%s", random_u64(), volume) < 0)
718 return NULL;
719
720 return s;
721 }
722
723 static int make_security_device_monitor(
724 sd_event **ret_event,
725 sd_device_monitor **ret_monitor) {
726 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
727 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
728 int r;
729
730 assert(ret_event);
731 assert(ret_monitor);
732
733 /* Waits for a device with "security-device" tag to show up in udev */
734
735 r = sd_event_default(&event);
736 if (r < 0)
737 return log_error_errno(r, "Failed to allocate event loop: %m");
738
739 r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
740 if (r < 0)
741 return log_error_errno(r, "Failed to install timeout event source: %m");
742
743 r = sd_device_monitor_new(&monitor);
744 if (r < 0)
745 return log_error_errno(r, "Failed to allocate device monitor: %m");
746
747 r = sd_device_monitor_filter_add_match_tag(monitor, "security-device");
748 if (r < 0)
749 return log_error_errno(r, "Failed to configure device monitor: %m");
750
751 r = sd_device_monitor_attach_event(monitor, event);
752 if (r < 0)
753 return log_error_errno(r, "Failed to attach device monitor: %m");
754
755 r = sd_device_monitor_start(monitor, NULL, NULL);
756 if (r < 0)
757 return log_error_errno(r, "Failed to start device monitor: %m");
758
759 *ret_event = TAKE_PTR(event);
760 *ret_monitor = TAKE_PTR(monitor);
761 return 0;
762 }
763
764 static int run_security_device_monitor(
765 sd_event *event,
766 sd_device_monitor *monitor) {
767 bool processed = false;
768 int r;
769
770 assert(event);
771 assert(monitor);
772
773 /* Runs the event loop for the device monitor until either something happens, or the time-out is
774 * hit. */
775
776 for (;;) {
777 int x;
778
779 r = sd_event_get_exit_code(event, &x);
780 if (r < 0) {
781 if (r != -ENODATA)
782 return log_error_errno(r, "Failed to query exit code from event loop: %m");
783
784 /* On ENODATA we aren't told to exit yet. */
785 } else {
786 assert(x == -ETIMEDOUT);
787 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
788 "Timed out waiting for security device, aborting security device based authentication attempt.");
789 }
790
791 /* Wait for one event, and then eat all subsequent events until there are no further ones */
792 r = sd_event_run(event, processed ? 0 : UINT64_MAX);
793 if (r < 0)
794 return log_error_errno(r, "Failed to run event loop: %m");
795 if (r == 0) /* no events queued anymore */
796 return 0;
797
798 processed = true;
799 }
800 }
801
802 static bool libcryptsetup_plugins_support(void) {
803 #if HAVE_LIBCRYPTSETUP_PLUGINS
804 int r;
805
806 /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
807 r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
808 if (r < 0 && r != -ENXIO)
809 log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
810 if (r == 0)
811 return false;
812
813 return crypt_token_external_path();
814 #else
815 return false;
816 #endif
817 }
818
819 #if HAVE_LIBCRYPTSETUP_PLUGINS
820 static int acquire_pins_from_env_variable(char ***ret_pins) {
821 char *e;
822 _cleanup_strv_free_erase_ char **pins = NULL;
823
824 assert(ret_pins);
825
826 e = getenv("PIN");
827 if (e) {
828 pins = strv_new(e);
829 if (!pins)
830 return log_oom();
831
832 string_erase(e);
833 if (unsetenv("PIN") < 0)
834 return log_error_errno(errno, "Failed to unset $PIN: %m");
835 }
836
837 *ret_pins = TAKE_PTR(pins);
838
839 return 0;
840 }
841 #endif
842
843 static int attach_luks2_by_fido2(
844 struct crypt_device *cd,
845 const char *name,
846 usec_t until,
847 bool headless,
848 void *usrptr,
849 uint32_t activation_flags) {
850
851 #if HAVE_LIBCRYPTSETUP_PLUGINS
852 AskPasswordFlags flags = ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_ACCEPT_CACHED;
853 _cleanup_strv_free_erase_ char **pins = NULL;
854 char **p;
855 int r;
856
857 r = crypt_activate_by_token_pin(cd, name, "systemd-fido2", CRYPT_ANY_TOKEN, NULL, 0, usrptr, activation_flags);
858 if (r > 0) /* returns unlocked keyslot id on success */
859 r = 0;
860 if (r != -ENOANO) /* needs pin or pin is wrong */
861 return r;
862
863 r = acquire_pins_from_env_variable(&pins);
864 if (r < 0)
865 return r;
866
867 STRV_FOREACH(p, pins) {
868 r = crypt_activate_by_token_pin(cd, name, "systemd-fido2", CRYPT_ANY_TOKEN, *p, strlen(*p), usrptr, activation_flags);
869 if (r > 0) /* returns unlocked keyslot id on success */
870 r = 0;
871 if (r != -ENOANO) /* needs pin or pin is wrong */
872 return r;
873 }
874
875 if (headless)
876 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
877
878 for (;;) {
879 pins = strv_free_erase(pins);
880 r = ask_password_auto("Please enter security token PIN:", "drive-harddisk", NULL, "fido2-pin", "cryptsetup.fido2-pin", until, flags, &pins);
881 if (r < 0)
882 return r;
883
884 STRV_FOREACH(p, pins) {
885 r = crypt_activate_by_token_pin(cd, name, "systemd-fido2", CRYPT_ANY_TOKEN, *p, strlen(*p), usrptr, activation_flags);
886 if (r > 0) /* returns unlocked keyslot id on success */
887 r = 0;
888 if (r != -ENOANO) /* needs pin or pin is wrong */
889 return r;
890 }
891
892 flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
893 }
894 return r;
895 #else
896 return -EOPNOTSUPP;
897 #endif
898 }
899
900 static int attach_luks_or_plain_or_bitlk_by_fido2(
901 struct crypt_device *cd,
902 const char *name,
903 const char *key_file,
904 const void *key_data,
905 size_t key_data_size,
906 usec_t until,
907 uint32_t flags,
908 bool pass_volume_key) {
909
910 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
911 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
912 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
913 _cleanup_free_ void *discovered_salt = NULL, *discovered_cid = NULL;
914 size_t discovered_salt_size, discovered_cid_size, decrypted_key_size, cid_size = 0;
915 _cleanup_free_ char *friendly = NULL, *discovered_rp_id = NULL;
916 int keyslot = arg_key_slot, r;
917 const char *rp_id = NULL;
918 const void *cid = NULL;
919 Fido2EnrollFlags required;
920 bool use_libcryptsetup_plugin = libcryptsetup_plugins_support();
921
922 assert(cd);
923 assert(name);
924 assert(arg_fido2_device || arg_fido2_device_auto);
925
926 if (arg_fido2_cid) {
927 if (!key_file && !key_data)
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
929 "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
930
931 rp_id = arg_fido2_rp_id;
932 cid = arg_fido2_cid;
933 cid_size = arg_fido2_cid_size;
934
935 /* For now and for compatibility, if the user explicitly configured FIDO2 support and we do
936 * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we
937 * use PIN + UP when needed, and do not configure UV at all. Eventually, we should make this
938 * explicitly configurable. */
939 required = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT;
940 } else if (!use_libcryptsetup_plugin) {
941 r = find_fido2_auto_data(
942 cd,
943 &discovered_rp_id,
944 &discovered_salt,
945 &discovered_salt_size,
946 &discovered_cid,
947 &discovered_cid_size,
948 &keyslot,
949 &required);
950
951 if (IN_SET(r, -ENOTUNIQ, -ENXIO))
952 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
953 "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
954 if (r < 0)
955 return r;
956
957 if ((required & (FIDO2ENROLL_PIN | FIDO2ENROLL_UP | FIDO2ENROLL_UV)) && arg_headless)
958 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG),
959 "Local verification is required to unlock this volume, but the 'headless' parameter was set.");
960
961 rp_id = discovered_rp_id;
962 key_data = discovered_salt;
963 key_data_size = discovered_salt_size;
964 cid = discovered_cid;
965 cid_size = discovered_cid_size;
966 }
967
968 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
969 if (!friendly)
970 return log_oom();
971
972 for (;;) {
973 if (use_libcryptsetup_plugin && !arg_fido2_cid) {
974 r = attach_luks2_by_fido2(cd, name, until, arg_headless, arg_fido2_device, flags);
975 if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT))
976 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
977 "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
978
979 } else {
980 r = acquire_fido2_key(
981 name,
982 friendly,
983 arg_fido2_device,
984 rp_id,
985 cid, cid_size,
986 key_file, arg_keyfile_size, arg_keyfile_offset,
987 key_data, key_data_size,
988 until,
989 arg_headless,
990 required,
991 &decrypted_key, &decrypted_key_size,
992 arg_ask_password_flags);
993 if (r >= 0)
994 break;
995 }
996
997 if (r != -EAGAIN) /* EAGAIN means: token not found */
998 return r;
999
1000 if (!monitor) {
1001 /* We didn't find the token. In this case, watch for it via udev. Let's
1002 * create an event loop and monitor first. */
1003
1004 assert(!event);
1005
1006 r = make_security_device_monitor(&event, &monitor);
1007 if (r < 0)
1008 return r;
1009
1010 log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly);
1011
1012 /* Let's immediately rescan in case the token appeared in the time we needed
1013 * to create and configure the monitor */
1014 continue;
1015 }
1016
1017 r = run_security_device_monitor(event, monitor);
1018 if (r < 0)
1019 return r;
1020
1021 log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
1022 }
1023
1024 if (pass_volume_key)
1025 r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
1026 else {
1027 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1028
1029 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1030
1031 r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
1032 if (r < 0)
1033 return log_oom();
1034
1035 r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags);
1036 }
1037 if (r == -EPERM) {
1038 log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
1039 return -EAGAIN; /* log actual error, but return EAGAIN */
1040 }
1041 if (r < 0)
1042 return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m");
1043
1044 return 0;
1045 }
1046
1047 static int attach_luks2_by_pkcs11(
1048 struct crypt_device *cd,
1049 const char *name,
1050 const char *friendly_name,
1051 usec_t until,
1052 bool headless,
1053 uint32_t flags) {
1054
1055 #if HAVE_LIBCRYPTSETUP_PLUGINS
1056 int r;
1057
1058 if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2))
1059 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device.");
1060
1061 systemd_pkcs11_plugin_params params = {
1062 .friendly_name = friendly_name,
1063 .until = until,
1064 .headless = headless
1065 };
1066
1067 r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
1068 if (r > 0) /* returns unlocked keyslot id on success */
1069 r = 0;
1070
1071 return r;
1072 #else
1073 return -EOPNOTSUPP;
1074 #endif
1075 }
1076
1077 static int attach_luks_or_plain_or_bitlk_by_pkcs11(
1078 struct crypt_device *cd,
1079 const char *name,
1080 const char *key_file,
1081 const void *key_data,
1082 size_t key_data_size,
1083 usec_t until,
1084 uint32_t flags,
1085 bool pass_volume_key) {
1086
1087 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1088 _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL;
1089 size_t decrypted_key_size = 0, discovered_key_size = 0;
1090 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
1091 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1092 _cleanup_free_ void *discovered_key = NULL;
1093 int keyslot = arg_key_slot, r;
1094 const char *uri = NULL;
1095 bool use_libcryptsetup_plugin = libcryptsetup_plugins_support();
1096
1097 assert(cd);
1098 assert(name);
1099 assert(arg_pkcs11_uri || arg_pkcs11_uri_auto);
1100
1101 if (arg_pkcs11_uri_auto) {
1102 if (!use_libcryptsetup_plugin) {
1103 r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot);
1104 if (IN_SET(r, -ENOTUNIQ, -ENXIO))
1105 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1106 "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1107 if (r < 0)
1108 return r;
1109
1110 uri = discovered_uri;
1111 key_data = discovered_key;
1112 key_data_size = discovered_key_size;
1113 }
1114 } else {
1115 uri = arg_pkcs11_uri;
1116
1117 if (!key_file && !key_data)
1118 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing.");
1119 }
1120
1121 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
1122 if (!friendly)
1123 return log_oom();
1124
1125 for (;;) {
1126 if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto)
1127 r = attach_luks2_by_pkcs11(cd, name, friendly, until, arg_headless, flags);
1128 else {
1129 r = decrypt_pkcs11_key(
1130 name,
1131 friendly,
1132 uri,
1133 key_file, arg_keyfile_size, arg_keyfile_offset,
1134 key_data, key_data_size,
1135 until,
1136 arg_headless,
1137 &decrypted_key, &decrypted_key_size);
1138 if (r >= 0)
1139 break;
1140 }
1141
1142 if (r != -EAGAIN) /* EAGAIN means: token not found */
1143 return r;
1144
1145 if (!monitor) {
1146 /* We didn't find the token. In this case, watch for it via udev. Let's
1147 * create an event loop and monitor first. */
1148
1149 assert(!event);
1150
1151 r = make_security_device_monitor(&event, &monitor);
1152 if (r < 0)
1153 return r;
1154
1155 log_notice("Security token %s not present for unlocking volume %s, please plug it in.",
1156 uri, friendly);
1157
1158 /* Let's immediately rescan in case the token appeared in the time we needed
1159 * to create and configure the monitor */
1160 continue;
1161 }
1162
1163 r = run_security_device_monitor(event, monitor);
1164 if (r < 0)
1165 return r;
1166
1167 log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
1168 }
1169 assert(decrypted_key);
1170
1171 if (pass_volume_key)
1172 r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
1173 else {
1174 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1175
1176 /* Before using this key as passphrase we base64 encode it. Why? For compatibility
1177 * with homed's PKCS#11 hookup: there we want to use the key we acquired through
1178 * PKCS#11 for other authentication/decryption mechanisms too, and some of them do
1179 * not not take arbitrary binary blobs, but require NUL-terminated strings — most
1180 * importantly UNIX password hashes. Hence, for compatibility we want to use a string
1181 * without embedded NUL here too, and that's easiest to generate from a binary blob
1182 * via base64 encoding. */
1183
1184 r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
1185 if (r < 0)
1186 return log_oom();
1187
1188 r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags);
1189 }
1190 if (r == -EPERM) {
1191 log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)");
1192 return -EAGAIN; /* log actual error, but return EAGAIN */
1193 }
1194 if (r < 0)
1195 return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m");
1196
1197 return 0;
1198 }
1199
1200 static int make_tpm2_device_monitor(
1201 sd_event **ret_event,
1202 sd_device_monitor **ret_monitor) {
1203
1204 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1205 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1206 int r;
1207
1208 assert(ret_event);
1209 assert(ret_monitor);
1210
1211 r = sd_event_default(&event);
1212 if (r < 0)
1213 return log_error_errno(r, "Failed to allocate event loop: %m");
1214
1215 r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT));
1216 if (r < 0)
1217 return log_error_errno(r, "Failed to install timeout event source: %m");
1218
1219 r = sd_device_monitor_new(&monitor);
1220 if (r < 0)
1221 return log_error_errno(r, "Failed to allocate device monitor: %m");
1222
1223 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL);
1224 if (r < 0)
1225 return log_error_errno(r, "Failed to configure device monitor: %m");
1226
1227 r = sd_device_monitor_attach_event(monitor, event);
1228 if (r < 0)
1229 return log_error_errno(r, "Failed to attach device monitor: %m");
1230
1231 r = sd_device_monitor_start(monitor, NULL, NULL);
1232 if (r < 0)
1233 return log_error_errno(r, "Failed to start device monitor: %m");
1234
1235 *ret_event = TAKE_PTR(event);
1236 *ret_monitor = TAKE_PTR(monitor);
1237 return 0;
1238 }
1239
1240 static int attach_luks2_by_tpm2(
1241 struct crypt_device *cd,
1242 const char *name,
1243 uint32_t flags) {
1244
1245 #if HAVE_LIBCRYPTSETUP_PLUGINS
1246 int r;
1247
1248 systemd_tpm2_plugin_params params = {
1249 .search_pcr_mask = arg_tpm2_pcr_mask,
1250 .device = arg_tpm2_device
1251 };
1252
1253 if (!crypt_token_external_path())
1254 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1255 "Libcryptsetup has external plugins support disabled.");
1256
1257 r = crypt_activate_by_token_pin(cd, name, "systemd-tpm2", CRYPT_ANY_TOKEN, NULL, 0, &params, flags);
1258 if (r > 0) /* returns unlocked keyslot id on success */
1259 r = 0;
1260
1261 return r;
1262 #else
1263 return -EOPNOTSUPP;
1264 #endif
1265 }
1266
1267 static int attach_luks_or_plain_or_bitlk_by_tpm2(
1268 struct crypt_device *cd,
1269 const char *name,
1270 const char *key_file,
1271 const void *key_data,
1272 size_t key_data_size,
1273 usec_t until,
1274 uint32_t flags,
1275 bool pass_volume_key) {
1276
1277 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
1278 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
1279 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1280 _cleanup_free_ char *friendly = NULL;
1281 int keyslot = arg_key_slot, r;
1282 size_t decrypted_key_size;
1283
1284 assert(cd);
1285 assert(name);
1286 assert(arg_tpm2_device || arg_tpm2_device_auto);
1287
1288 friendly = friendly_disk_name(crypt_get_device_name(cd), name);
1289 if (!friendly)
1290 return log_oom();
1291
1292 for (;;) {
1293 if (key_file || key_data) {
1294 /* If key data is specified, use that */
1295
1296 r = acquire_tpm2_key(
1297 name,
1298 arg_tpm2_device,
1299 arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT : arg_tpm2_pcr_mask,
1300 UINT16_MAX,
1301 0,
1302 key_file, arg_keyfile_size, arg_keyfile_offset,
1303 key_data, key_data_size,
1304 NULL, 0, /* we don't know the policy hash */
1305 &decrypted_key, &decrypted_key_size);
1306 if (r >= 0)
1307 break;
1308 if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
1309 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
1310 if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */
1311 return r;
1312 } else {
1313 r = attach_luks2_by_tpm2(cd, name, flags);
1314 /* EAGAIN means: no tpm2 chip found
1315 * EOPNOTSUPP means: no libcryptsetup plugins support */
1316 if (r == -ENXIO)
1317 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1318 "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
1319 if (r == -ENOENT)
1320 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1321 "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
1322 if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN))
1323 return r;
1324 }
1325
1326 if (r == -EOPNOTSUPP) {
1327 _cleanup_free_ void *blob = NULL, *policy_hash = NULL;
1328 size_t blob_size, policy_hash_size;
1329 bool found_some = false;
1330 int token = 0; /* first token to look at */
1331
1332 /* If no key data is specified, look for it in the header. In order to support
1333 * software upgrades we'll iterate through all suitable tokens, maybe one of them
1334 * works. */
1335
1336 for (;;) {
1337 uint32_t pcr_mask;
1338 uint16_t pcr_bank, primary_alg;
1339
1340 r = find_tpm2_auto_data(
1341 cd,
1342 arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
1343 token, /* search for the token with this index, or any later index than this */
1344 &pcr_mask,
1345 &pcr_bank,
1346 &primary_alg,
1347 &blob, &blob_size,
1348 &policy_hash, &policy_hash_size,
1349 &keyslot,
1350 &token);
1351 if (r == -ENXIO)
1352 /* No further TPM2 tokens found in the LUKS2 header. */
1353 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
1354 found_some
1355 ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
1356 : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
1357 if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
1358 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
1359 if (r < 0)
1360 return r;
1361
1362 found_some = true;
1363
1364 r = acquire_tpm2_key(
1365 name,
1366 arg_tpm2_device,
1367 pcr_mask,
1368 pcr_bank,
1369 primary_alg,
1370 NULL, 0, 0, /* no key file */
1371 blob, blob_size,
1372 policy_hash, policy_hash_size,
1373 &decrypted_key, &decrypted_key_size);
1374 if (r != -EPERM)
1375 break;
1376
1377 token++; /* try a different token next time */
1378 }
1379
1380 if (r >= 0)
1381 break;
1382 if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */
1383 return r;
1384 }
1385
1386 if (!monitor) {
1387 /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create
1388 * an event loop and monitor first. */
1389
1390 assert(!event);
1391
1392 if (is_efi_boot() && !efi_has_tpm2())
1393 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN),
1394 "No TPM2 hardware discovered and EFI bios indicates no support for it either, assuming TPM2-less system, falling back to traditional unocking.");
1395
1396 r = make_tpm2_device_monitor(&event, &monitor);
1397 if (r < 0)
1398 return r;
1399
1400 log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly);
1401
1402 /* Let's immediately rescan in case the device appeared in the time we needed
1403 * to create and configure the monitor */
1404 continue;
1405 }
1406
1407 r = run_security_device_monitor(event, monitor);
1408 if (r < 0)
1409 return r;
1410
1411 log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
1412 }
1413 assert(decrypted_key);
1414
1415 if (pass_volume_key)
1416 r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
1417 else {
1418 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
1419
1420 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1421
1422 r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
1423 if (r < 0)
1424 return log_oom();
1425
1426 r = crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, strlen(base64_encoded), flags);
1427 }
1428 if (r == -EPERM) {
1429 log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
1430 return -EAGAIN; /* log actual error, but return EAGAIN */
1431 }
1432 if (r < 0)
1433 return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m");
1434
1435 return 0;
1436 }
1437
1438 static int attach_luks_or_plain_or_bitlk_by_key_data(
1439 struct crypt_device *cd,
1440 const char *name,
1441 const void *key_data,
1442 size_t key_data_size,
1443 uint32_t flags,
1444 bool pass_volume_key) {
1445
1446 int r;
1447
1448 assert(cd);
1449 assert(name);
1450 assert(key_data);
1451
1452 if (pass_volume_key)
1453 r = crypt_activate_by_volume_key(cd, name, key_data, key_data_size, flags);
1454 else
1455 r = crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data, key_data_size, flags);
1456 if (r == -EPERM) {
1457 log_error_errno(r, "Failed to activate. (Key incorrect?)");
1458 return -EAGAIN; /* Log actual error, but return EAGAIN */
1459 }
1460 if (r < 0)
1461 return log_error_errno(r, "Failed to activate: %m");
1462
1463 return 0;
1464 }
1465
1466 static int attach_luks_or_plain_or_bitlk_by_key_file(
1467 struct crypt_device *cd,
1468 const char *name,
1469 const char *key_file,
1470 uint32_t flags,
1471 bool pass_volume_key) {
1472
1473 _cleanup_(erase_and_freep) char *kfdata = NULL;
1474 _cleanup_free_ char *bindname = NULL;
1475 size_t kfsize;
1476 int r;
1477
1478 assert(cd);
1479 assert(name);
1480 assert(key_file);
1481
1482 /* If we read the key via AF_UNIX, make this client recognizable */
1483 bindname = make_bindname(name);
1484 if (!bindname)
1485 return log_oom();
1486
1487 r = read_full_file_full(
1488 AT_FDCWD, key_file,
1489 arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset,
1490 arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size,
1491 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
1492 bindname,
1493 &kfdata, &kfsize);
1494 if (r == -E2BIG) {
1495 log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file);
1496 return -EAGAIN;
1497 }
1498 if (r == -ENOENT) {
1499 log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file);
1500 return -EAGAIN; /* Log actual error, but return EAGAIN */
1501 }
1502 if (r < 0)
1503 return log_error_errno(r, "Failed to read key file '%s': %m", key_file);
1504
1505 if (pass_volume_key)
1506 r = crypt_activate_by_volume_key(cd, name, kfdata, kfsize, flags);
1507 else
1508 r = crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags);
1509 if (r == -EPERM) {
1510 log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file);
1511 return -EAGAIN; /* Log actual error, but return EAGAIN */
1512 }
1513 if (r < 0)
1514 return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
1515
1516 return 0;
1517 }
1518
1519 static int attach_luks_or_plain_or_bitlk_by_passphrase(
1520 struct crypt_device *cd,
1521 const char *name,
1522 char **passwords,
1523 uint32_t flags,
1524 bool pass_volume_key) {
1525
1526 char **p;
1527 int r;
1528
1529 assert(cd);
1530 assert(name);
1531
1532 r = -EINVAL;
1533 STRV_FOREACH(p, passwords) {
1534 if (pass_volume_key)
1535 r = crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags);
1536 else
1537 r = crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags);
1538 if (r >= 0)
1539 break;
1540 }
1541 if (r == -EPERM) {
1542 log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
1543 return -EAGAIN; /* log actual error, but return EAGAIN */
1544 }
1545 if (r < 0)
1546 return log_error_errno(r, "Failed to activate with specified passphrase: %m");
1547
1548 return 0;
1549 }
1550
1551 static int attach_luks_or_plain_or_bitlk(
1552 struct crypt_device *cd,
1553 const char *name,
1554 const char *key_file,
1555 const void *key_data,
1556 size_t key_data_size,
1557 char **passwords,
1558 uint32_t flags,
1559 usec_t until) {
1560
1561 bool pass_volume_key = false;
1562 int r;
1563
1564 assert(cd);
1565 assert(name);
1566
1567 if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
1568 struct crypt_params_plain params = {
1569 .offset = arg_offset,
1570 .skip = arg_skip,
1571 .sector_size = arg_sector_size,
1572 };
1573 const char *cipher, *cipher_mode;
1574 _cleanup_free_ char *truncated_cipher = NULL;
1575
1576 if (streq_ptr(arg_hash, "plain"))
1577 /* plain isn't a real hash type. it just means "use no hash" */
1578 params.hash = NULL;
1579 else if (arg_hash)
1580 params.hash = arg_hash;
1581 else if (!key_file)
1582 /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
1583 * file is provided */
1584 params.hash = "ripemd160";
1585
1586 if (arg_cipher) {
1587 size_t l;
1588
1589 l = strcspn(arg_cipher, "-");
1590 truncated_cipher = strndup(arg_cipher, l);
1591 if (!truncated_cipher)
1592 return log_oom();
1593
1594 cipher = truncated_cipher;
1595 cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
1596 } else {
1597 cipher = "aes";
1598 cipher_mode = "cbc-essiv:sha256";
1599 }
1600
1601 /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
1602 arg_keyfile_size = arg_key_size;
1603
1604 /* In contrast to what the name crypt_format() might suggest this doesn't actually format
1605 * anything, it just configures encryption parameters when used for plain mode. */
1606 r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, &params);
1607 if (r < 0)
1608 return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
1609
1610 /* hash == NULL implies the user passed "plain" */
1611 pass_volume_key = !params.hash;
1612 }
1613
1614 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
1615 crypt_get_cipher(cd),
1616 crypt_get_cipher_mode(cd),
1617 crypt_get_volume_key_size(cd)*8,
1618 crypt_get_device_name(cd));
1619
1620 if (arg_tpm2_device || arg_tpm2_device_auto)
1621 return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
1622 if (arg_fido2_device || arg_fido2_device_auto)
1623 return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
1624 if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
1625 return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
1626 if (key_data)
1627 return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, key_data_size, flags, pass_volume_key);
1628 if (key_file)
1629 return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key);
1630
1631 return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key);
1632 }
1633
1634 static int help(void) {
1635 _cleanup_free_ char *link = NULL;
1636 int r;
1637
1638 r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link);
1639 if (r < 0)
1640 return log_oom();
1641
1642 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
1643 "%s detach VOLUME\n\n"
1644 "Attaches or detaches an encrypted block device.\n"
1645 "\nSee the %s for details.\n",
1646 program_invocation_short_name,
1647 program_invocation_short_name,
1648 link);
1649
1650 return 0;
1651 }
1652
1653 static uint32_t determine_flags(void) {
1654 uint32_t flags = 0;
1655
1656 if (arg_readonly)
1657 flags |= CRYPT_ACTIVATE_READONLY;
1658
1659 if (arg_discards)
1660 flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
1661
1662 if (arg_same_cpu_crypt)
1663 flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
1664
1665 if (arg_submit_from_crypt_cpus)
1666 flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
1667
1668 if (arg_no_read_workqueue)
1669 flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
1670
1671 if (arg_no_write_workqueue)
1672 flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
1673
1674 #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
1675 /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
1676 /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
1677 flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF;
1678 #endif
1679
1680 return flags;
1681 }
1682
1683 static void remove_and_erasep(const char **p) {
1684 int r;
1685
1686 if (!*p)
1687 return;
1688
1689 r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE);
1690 if (r < 0 && r != -ENOENT)
1691 log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p);
1692 }
1693
1694 static int run(int argc, char *argv[]) {
1695 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
1696 const char *verb;
1697 int r;
1698
1699 if (argc <= 1)
1700 return help();
1701
1702 if (argc < 3)
1703 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1704 "This program requires at least two arguments.");
1705
1706 log_setup();
1707
1708 cryptsetup_enable_logging(NULL);
1709
1710 umask(0022);
1711
1712 verb = argv[1];
1713
1714 if (streq(verb, "attach")) {
1715 _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL;
1716 _cleanup_(erase_and_freep) void *key_data = NULL;
1717 const char *volume, *source, *key_file, *options;
1718 crypt_status_info status;
1719 size_t key_data_size = 0;
1720 uint32_t flags = 0;
1721 unsigned tries;
1722 usec_t until;
1723
1724 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
1725
1726 if (argc < 4)
1727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments.");
1728
1729 volume = argv[2];
1730 source = argv[3];
1731 key_file = argc >= 5 && !STR_IN_SET(argv[4], "", "-", "none") ? argv[4] : NULL;
1732 options = argc >= 6 && !STR_IN_SET(argv[5], "", "-", "none") ? argv[5] : NULL;
1733
1734 if (!filename_is_valid(volume))
1735 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
1736
1737 if (key_file && !path_is_absolute(key_file)) {
1738 log_warning("Password file path '%s' is not absolute. Ignoring.", key_file);
1739 key_file = NULL;
1740 }
1741
1742 if (options) {
1743 r = parse_options(options);
1744 if (r < 0)
1745 return r;
1746 }
1747
1748 log_debug("%s %s ← %s type=%s cipher=%s", __func__,
1749 volume, source, strempty(arg_type), strempty(arg_cipher));
1750
1751 /* A delicious drop of snake oil */
1752 (void) mlockall(MCL_FUTURE);
1753
1754 if (!key_file) {
1755 _cleanup_free_ char *bindname = NULL;
1756 const char *fn;
1757
1758 bindname = make_bindname(volume);
1759 if (!bindname)
1760 return log_oom();
1761
1762 /* If a key file is not explicitly specified, search for a key in a well defined
1763 * search path, and load it. */
1764
1765 fn = strjoina(volume, ".key");
1766 r = find_key_file(
1767 fn,
1768 STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"),
1769 bindname,
1770 &key_data, &key_data_size);
1771 if (r < 0)
1772 return r;
1773 if (r > 0)
1774 log_debug("Automatically discovered key for volume '%s'.", volume);
1775 } else if (arg_keyfile_erase)
1776 destroy_key_file = key_file; /* let's get this baby erased when we leave */
1777
1778 if (arg_header) {
1779 log_debug("LUKS header: %s", arg_header);
1780 r = crypt_init(&cd, arg_header);
1781 } else
1782 r = crypt_init(&cd, source);
1783 if (r < 0)
1784 return log_error_errno(r, "crypt_init() failed: %m");
1785
1786 cryptsetup_enable_logging(cd);
1787
1788 status = crypt_status(cd, volume);
1789 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
1790 log_info("Volume %s already active.", volume);
1791 return 0;
1792 }
1793
1794 flags = determine_flags();
1795
1796 until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
1797 if (until == USEC_INFINITY)
1798 until = 0;
1799
1800 arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8));
1801
1802 if (key_file) {
1803 struct stat st;
1804
1805 /* Ideally we'd do this on the open fd, but since this is just a
1806 * warning it's OK to do this in two steps. */
1807 if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
1808 log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
1809 }
1810
1811 if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) {
1812 r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL);
1813 if (r < 0)
1814 return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
1815
1816 if (arg_header) {
1817 r = crypt_set_data_device(cd, source);
1818 if (r < 0)
1819 return log_error_errno(r, "Failed to set LUKS data device %s: %m", source);
1820 }
1821
1822 /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
1823 if (!key_file && !key_data) {
1824 r = crypt_activate_by_token(cd, volume, CRYPT_ANY_TOKEN, NULL, flags);
1825 if (r >= 0) {
1826 log_debug("Volume %s activated with LUKS token id %i.", volume, r);
1827 return 0;
1828 }
1829
1830 log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd));
1831 }
1832 }
1833
1834 /* since cryptsetup 2.3.0 (Feb 2020) */
1835 #ifdef CRYPT_BITLK
1836 if (streq_ptr(arg_type, CRYPT_BITLK)) {
1837 r = crypt_load(cd, CRYPT_BITLK, NULL);
1838 if (r < 0)
1839 return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd));
1840 }
1841 #endif
1842
1843 for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
1844 _cleanup_strv_free_erase_ char **passwords = NULL;
1845
1846 /* When we were able to acquire multiple keys, let's always process them in this order:
1847 *
1848 * 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
1849 * 2. The discovered key: i.e. key_data + key_data_size
1850 * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size
1851 * 4. The empty password, in case arg_try_empty_password is set
1852 * 5. We enquire the user for a password
1853 */
1854
1855 if (!key_file && !key_data && !arg_pkcs11_uri && !arg_pkcs11_uri_auto && !arg_fido2_device && !arg_fido2_device_auto && !arg_tpm2_device && !arg_tpm2_device_auto) {
1856
1857 if (arg_try_empty_password) {
1858 /* Hmm, let's try an empty password now, but only once */
1859 arg_try_empty_password = false;
1860
1861 key_data = strdup("");
1862 if (!key_data)
1863 return log_oom();
1864
1865 key_data_size = 0;
1866 } else {
1867 /* Ask the user for a passphrase only as last resort, if we have
1868 * nothing else to check for */
1869
1870 r = get_password(volume, source, until, tries == 0 && !arg_verify, &passwords);
1871 if (r == -EAGAIN)
1872 continue;
1873 if (r < 0)
1874 return r;
1875 }
1876 }
1877
1878 if (streq_ptr(arg_type, CRYPT_TCRYPT))
1879 r = attach_tcrypt(cd, volume, key_file, key_data, key_data_size, passwords, flags);
1880 else
1881 r = attach_luks_or_plain_or_bitlk(cd, volume, key_file, key_data, key_data_size, passwords, flags, until);
1882 if (r >= 0)
1883 break;
1884 if (r != -EAGAIN)
1885 return r;
1886
1887 /* Key not correct? Let's try again! */
1888
1889 key_file = NULL;
1890 key_data = erase_and_free(key_data);
1891 key_data_size = 0;
1892 arg_pkcs11_uri = mfree(arg_pkcs11_uri);
1893 arg_pkcs11_uri_auto = false;
1894 arg_fido2_device = mfree(arg_fido2_device);
1895 arg_fido2_device_auto = false;
1896 arg_tpm2_device = mfree(arg_tpm2_device);
1897 arg_tpm2_device_auto = false;
1898 }
1899
1900 if (arg_tries != 0 && tries >= arg_tries)
1901 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up.");
1902
1903 } else if (streq(verb, "detach")) {
1904 const char *volume;
1905
1906 volume = argv[2];
1907
1908 if (!filename_is_valid(volume))
1909 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
1910
1911 r = crypt_init_by_name(&cd, volume);
1912 if (r == -ENODEV) {
1913 log_info("Volume %s already inactive.", volume);
1914 return 0;
1915 }
1916 if (r < 0)
1917 return log_error_errno(r, "crypt_init_by_name() failed: %m");
1918
1919 cryptsetup_enable_logging(cd);
1920
1921 r = crypt_deactivate(cd, volume);
1922 if (r < 0)
1923 return log_error_errno(r, "Failed to deactivate: %m");
1924
1925 } else
1926 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", verb);
1927
1928 return 0;
1929 }
1930
1931 DEFINE_MAIN_FUNCTION(run);