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