]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e23a0ce8 | 2 | |
7f4e0805 | 3 | #include <errno.h> |
07630cea | 4 | #include <mntent.h> |
07630cea | 5 | #include <sys/mman.h> |
ca78ad1d ZJS |
6 | #include <sys/stat.h> |
7 | #include <sys/types.h> | |
8 | #include <unistd.h> | |
e23a0ce8 | 9 | |
4f5dd394 LP |
10 | #include "sd-device.h" |
11 | ||
b5efdb8a | 12 | #include "alloc-util.h" |
4f5dd394 | 13 | #include "ask-password-api.h" |
1e2f3230 | 14 | #include "cryptsetup-keyfile.h" |
08669709 | 15 | #include "cryptsetup-pkcs11.h" |
7407f689 | 16 | #include "cryptsetup-util.h" |
4f5dd394 LP |
17 | #include "device-util.h" |
18 | #include "escape.h" | |
8cf3ca80 | 19 | #include "fileio.h" |
d3d49e76 | 20 | #include "fs-util.h" |
ed4ad488 | 21 | #include "fstab-util.h" |
08669709 | 22 | #include "hexdecoct.h" |
e23a0ce8 | 23 | #include "log.h" |
3a40f366 | 24 | #include "main-func.h" |
7407f689 | 25 | #include "memory-util.h" |
4349cd7c | 26 | #include "mount-util.h" |
d8b4d14d | 27 | #include "nulstr-util.h" |
6bedfcbb | 28 | #include "parse-util.h" |
9eb977db | 29 | #include "path-util.h" |
08669709 | 30 | #include "pkcs11-util.h" |
d8b4d14d | 31 | #include "pretty-print.h" |
e2c2f868 | 32 | #include "random-util.h" |
07630cea | 33 | #include "string-util.h" |
21bc923a | 34 | #include "strv.h" |
7f4e0805 | 35 | |
b3b4ebab OK |
36 | /* internal helper */ |
37 | #define ANY_LUKS "LUKS" | |
a9fc6406 DJL |
38 | /* as in src/cryptsetup.h */ |
39 | #define CRYPT_SECTOR_SIZE 512 | |
40 | #define CRYPT_MAX_SECTOR_SIZE 4096 | |
b3b4ebab | 41 | |
6cc27c29 | 42 | static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */ |
f75cac37 LP |
43 | static char *arg_cipher = NULL; |
44 | static unsigned arg_key_size = 0; | |
a9fc6406 | 45 | static unsigned arg_sector_size = CRYPT_SECTOR_SIZE; |
f75cac37 LP |
46 | static int arg_key_slot = CRYPT_ANY_SLOT; |
47 | static unsigned arg_keyfile_size = 0; | |
dc0a3555 | 48 | static uint64_t arg_keyfile_offset = 0; |
d3d49e76 | 49 | static bool arg_keyfile_erase = false; |
0ba6f85e | 50 | static bool arg_try_empty_password = false; |
f75cac37 | 51 | static char *arg_hash = NULL; |
7376e835 | 52 | static char *arg_header = NULL; |
f75cac37 LP |
53 | static unsigned arg_tries = 3; |
54 | static bool arg_readonly = false; | |
55 | static bool arg_verify = false; | |
56 | static bool arg_discards = false; | |
2c65512e YW |
57 | static bool arg_same_cpu_crypt = false; |
58 | static bool arg_submit_from_crypt_cpus = false; | |
f75cac37 LP |
59 | static bool arg_tcrypt_hidden = false; |
60 | static bool arg_tcrypt_system = false; | |
52028838 | 61 | static bool arg_tcrypt_veracrypt = false; |
f75cac37 | 62 | static char **arg_tcrypt_keyfiles = NULL; |
4eac2773 MP |
63 | static uint64_t arg_offset = 0; |
64 | static uint64_t arg_skip = 0; | |
0864d311 | 65 | static usec_t arg_timeout = USEC_INFINITY; |
08669709 | 66 | static char *arg_pkcs11_uri = NULL; |
7f4e0805 | 67 | |
3a40f366 YW |
68 | STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep); |
69 | STATIC_DESTRUCTOR_REGISTER(arg_hash, freep); | |
70 | STATIC_DESTRUCTOR_REGISTER(arg_header, freep); | |
71 | STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep); | |
08669709 | 72 | STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep); |
3a40f366 | 73 | |
1fc76335 LP |
74 | /* Options Debian's crypttab knows we don't: |
75 | ||
1fc76335 LP |
76 | check= |
77 | checkargs= | |
8ced40c0 LP |
78 | noearly |
79 | loud | |
80 | quiet | |
1fc76335 | 81 | keyscript= |
8ced40c0 | 82 | initramfs |
1fc76335 LP |
83 | */ |
84 | ||
7f4e0805 | 85 | static int parse_one_option(const char *option) { |
fb4650aa ZJS |
86 | const char *val; |
87 | int r; | |
88 | ||
7f4e0805 LP |
89 | assert(option); |
90 | ||
91 | /* Handled outside of this tool */ | |
50d2eba2 | 92 | if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout")) |
93 | return 0; | |
94 | ||
95 | if (startswith(option, "keyfile-timeout=")) | |
7f4e0805 LP |
96 | return 0; |
97 | ||
fb4650aa ZJS |
98 | if ((val = startswith(option, "cipher="))) { |
99 | r = free_and_strdup(&arg_cipher, val); | |
100 | if (r < 0) | |
4b93637f | 101 | return log_oom(); |
7f4e0805 | 102 | |
fb4650aa | 103 | } else if ((val = startswith(option, "size="))) { |
7f4e0805 | 104 | |
fb4650aa ZJS |
105 | r = safe_atou(val, &arg_key_size); |
106 | if (r < 0) { | |
107 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
108 | return 0; |
109 | } | |
110 | ||
6131a78b DH |
111 | if (arg_key_size % 8) { |
112 | log_error("size= not a multiple of 8, ignoring."); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | arg_key_size /= 8; | |
117 | ||
a9fc6406 DJL |
118 | } else if ((val = startswith(option, "sector-size="))) { |
119 | ||
a9fc6406 DJL |
120 | r = safe_atou(val, &arg_sector_size); |
121 | if (r < 0) { | |
122 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | if (arg_sector_size % 2) { | |
127 | log_error("sector-size= not a multiple of 2, ignoring."); | |
128 | return 0; | |
129 | } | |
130 | ||
131 | if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) { | |
132 | log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE); | |
133 | return 0; | |
134 | } | |
a9fc6406 | 135 | |
8ced40c0 LP |
136 | } else if ((val = startswith(option, "key-slot=")) || |
137 | (val = startswith(option, "keyslot="))) { | |
b4a11878 | 138 | |
b3b4ebab | 139 | arg_type = ANY_LUKS; |
fb4650aa ZJS |
140 | r = safe_atoi(val, &arg_key_slot); |
141 | if (r < 0) { | |
142 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
b4a11878 CS |
143 | return 0; |
144 | } | |
145 | ||
fb4650aa | 146 | } else if ((val = startswith(option, "tcrypt-keyfile="))) { |
8cf3ca80 | 147 | |
f75cac37 | 148 | arg_type = CRYPT_TCRYPT; |
fb4650aa ZJS |
149 | if (path_is_absolute(val)) { |
150 | if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) | |
4b93637f LP |
151 | return log_oom(); |
152 | } else | |
fb4650aa | 153 | log_error("Key file path \"%s\" is not absolute. Ignoring.", val); |
8cf3ca80 | 154 | |
fb4650aa | 155 | } else if ((val = startswith(option, "keyfile-size="))) { |
4271d823 | 156 | |
fb4650aa ZJS |
157 | r = safe_atou(val, &arg_keyfile_size); |
158 | if (r < 0) { | |
159 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
4271d823 TG |
160 | return 0; |
161 | } | |
162 | ||
fb4650aa | 163 | } else if ((val = startswith(option, "keyfile-offset="))) { |
880a599e | 164 | |
d90874b4 | 165 | r = safe_atou64(val, &arg_keyfile_offset); |
fb4650aa ZJS |
166 | if (r < 0) { |
167 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
880a599e TG |
168 | return 0; |
169 | } | |
170 | ||
d3d49e76 LP |
171 | } else if ((val = startswith(option, "keyfile-erase="))) { |
172 | ||
173 | r = parse_boolean(val); | |
174 | if (r < 0) { | |
175 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | arg_keyfile_erase = r; | |
180 | ||
181 | } else if (streq(option, "keyfile-erase")) | |
182 | arg_keyfile_erase = true; | |
183 | ||
184 | else if ((val = startswith(option, "hash="))) { | |
fb4650aa ZJS |
185 | r = free_and_strdup(&arg_hash, val); |
186 | if (r < 0) | |
4b93637f | 187 | return log_oom(); |
7f4e0805 | 188 | |
fb4650aa | 189 | } else if ((val = startswith(option, "header="))) { |
b3b4ebab | 190 | arg_type = ANY_LUKS; |
7376e835 | 191 | |
baaa35ad ZJS |
192 | if (!path_is_absolute(val)) |
193 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
194 | "Header path \"%s\" is not absolute, refusing.", val); | |
7376e835 | 195 | |
baaa35ad ZJS |
196 | if (arg_header) |
197 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
198 | "Duplicate header= option, refusing."); | |
7376e835 | 199 | |
fb4650aa | 200 | arg_header = strdup(val); |
7376e835 AC |
201 | if (!arg_header) |
202 | return log_oom(); | |
203 | ||
fb4650aa | 204 | } else if ((val = startswith(option, "tries="))) { |
7f4e0805 | 205 | |
fb4650aa ZJS |
206 | r = safe_atou(val, &arg_tries); |
207 | if (r < 0) { | |
208 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
209 | return 0; |
210 | } | |
211 | ||
f75cac37 LP |
212 | } else if (STR_IN_SET(option, "readonly", "read-only")) |
213 | arg_readonly = true; | |
7f4e0805 | 214 | else if (streq(option, "verify")) |
f75cac37 LP |
215 | arg_verify = true; |
216 | else if (STR_IN_SET(option, "allow-discards", "discard")) | |
217 | arg_discards = true; | |
2c65512e YW |
218 | else if (streq(option, "same-cpu-crypt")) |
219 | arg_same_cpu_crypt = true; | |
220 | else if (streq(option, "submit-from-crypt-cpus")) | |
221 | arg_submit_from_crypt_cpus = true; | |
260ab287 | 222 | else if (streq(option, "luks")) |
b3b4ebab | 223 | arg_type = ANY_LUKS; |
6cc27c29 MF |
224 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
225 | #ifdef CRYPT_BITLK | |
226 | else if (streq(option, "bitlk")) | |
227 | arg_type = CRYPT_BITLK; | |
228 | #endif | |
8cf3ca80 | 229 | else if (streq(option, "tcrypt")) |
f75cac37 | 230 | arg_type = CRYPT_TCRYPT; |
8ced40c0 | 231 | else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) { |
f75cac37 LP |
232 | arg_type = CRYPT_TCRYPT; |
233 | arg_tcrypt_hidden = true; | |
8cf3ca80 | 234 | } else if (streq(option, "tcrypt-system")) { |
f75cac37 LP |
235 | arg_type = CRYPT_TCRYPT; |
236 | arg_tcrypt_system = true; | |
8ced40c0 | 237 | } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) { |
52028838 GH |
238 | arg_type = CRYPT_TCRYPT; |
239 | arg_tcrypt_veracrypt = true; | |
53ac130b LP |
240 | } else if (STR_IN_SET(option, "plain", "swap", "tmp") || |
241 | startswith(option, "tmp=")) | |
f75cac37 | 242 | arg_type = CRYPT_PLAIN; |
fb4650aa | 243 | else if ((val = startswith(option, "timeout="))) { |
7f4e0805 | 244 | |
0004f698 | 245 | r = parse_sec_fix_0(val, &arg_timeout); |
fb4650aa ZJS |
246 | if (r < 0) { |
247 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
248 | return 0; |
249 | } | |
250 | ||
fb4650aa | 251 | } else if ((val = startswith(option, "offset="))) { |
4eac2773 | 252 | |
fb4650aa ZJS |
253 | r = safe_atou64(val, &arg_offset); |
254 | if (r < 0) | |
255 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 256 | |
fb4650aa | 257 | } else if ((val = startswith(option, "skip="))) { |
4eac2773 | 258 | |
fb4650aa ZJS |
259 | r = safe_atou64(val, &arg_skip); |
260 | if (r < 0) | |
261 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 262 | |
08669709 LP |
263 | } else if ((val = startswith(option, "pkcs11-uri="))) { |
264 | ||
265 | if (!pkcs11_uri_valid(val)) | |
266 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing"); | |
267 | ||
268 | r = free_and_strdup(&arg_pkcs11_uri, val); | |
269 | if (r < 0) | |
270 | return log_oom(); | |
271 | ||
0ba6f85e LP |
272 | } else if ((val = startswith(option, "try-empty-password="))) { |
273 | ||
274 | r = parse_boolean(val); | |
275 | if (r < 0) { | |
276 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | arg_try_empty_password = r; | |
281 | ||
282 | } else if (streq(option, "try-empty-password")) | |
283 | arg_try_empty_password = true; | |
284 | ||
285 | else if (!streq(option, "x-initrd.attach")) | |
fb4650aa | 286 | log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); |
7f4e0805 LP |
287 | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static int parse_options(const char *options) { | |
7f4e0805 LP |
292 | assert(options); |
293 | ||
dd2fff3a ZJS |
294 | for (;;) { |
295 | _cleanup_free_ char *word = NULL; | |
296 | int r; | |
297 | ||
7bb553bb | 298 | r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS); |
dd2fff3a | 299 | if (r < 0) |
be36bc1e | 300 | return log_error_errno(r, "Failed to parse options: %m"); |
dd2fff3a ZJS |
301 | if (r == 0) |
302 | break; | |
7f4e0805 | 303 | |
dd2fff3a | 304 | r = parse_one_option(word); |
7f4e0805 LP |
305 | if (r < 0) |
306 | return r; | |
307 | } | |
308 | ||
4eac2773 | 309 | /* sanity-check options */ |
9c5253ff LP |
310 | if (arg_type && !streq(arg_type, CRYPT_PLAIN)) { |
311 | if (arg_offset != 0) | |
4eac2773 | 312 | log_warning("offset= ignored with type %s", arg_type); |
9c5253ff | 313 | if (arg_skip != 0) |
4eac2773 MP |
314 | log_warning("skip= ignored with type %s", arg_type); |
315 | } | |
316 | ||
7f4e0805 LP |
317 | return 0; |
318 | } | |
319 | ||
1ca208fb | 320 | static char* disk_description(const char *path) { |
f75cac37 | 321 | static const char name_fields[] = |
74b1c371 LP |
322 | "ID_PART_ENTRY_NAME\0" |
323 | "DM_NAME\0" | |
324 | "ID_MODEL_FROM_DATABASE\0" | |
f75cac37 | 325 | "ID_MODEL\0"; |
74b1c371 | 326 | |
4afd3348 | 327 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
2c740afd | 328 | const char *i, *name; |
b1a2da0a | 329 | struct stat st; |
b1a2da0a LP |
330 | |
331 | assert(path); | |
332 | ||
333 | if (stat(path, &st) < 0) | |
334 | return NULL; | |
335 | ||
336 | if (!S_ISBLK(st.st_mode)) | |
337 | return NULL; | |
338 | ||
2c740afd | 339 | if (sd_device_new_from_devnum(&device, 'b', st.st_rdev) < 0) |
1ca208fb | 340 | return NULL; |
b1a2da0a | 341 | |
2c740afd YW |
342 | NULSTR_FOREACH(i, name_fields) |
343 | if (sd_device_get_property_value(device, i, &name) >= 0 && | |
344 | !isempty(name)) | |
1ca208fb | 345 | return strdup(name); |
b1a2da0a | 346 | |
1ca208fb | 347 | return NULL; |
b1a2da0a LP |
348 | } |
349 | ||
b61e476f | 350 | static char *disk_mount_point(const char *label) { |
e7d90b71 | 351 | _cleanup_free_ char *device = NULL; |
5862d652 | 352 | _cleanup_endmntent_ FILE *f = NULL; |
b61e476f LP |
353 | struct mntent *m; |
354 | ||
355 | /* Yeah, we don't support native systemd unit files here for now */ | |
356 | ||
090685b5 LP |
357 | device = strjoin("/dev/mapper/", label); |
358 | if (!device) | |
5862d652 | 359 | return NULL; |
b61e476f | 360 | |
ed4ad488 | 361 | f = setmntent(fstab_path(), "re"); |
e0295d26 | 362 | if (!f) |
5862d652 | 363 | return NULL; |
b61e476f LP |
364 | |
365 | while ((m = getmntent(f))) | |
5862d652 ZJS |
366 | if (path_equal(m->mnt_fsname, device)) |
367 | return strdup(m->mnt_dir); | |
b61e476f | 368 | |
5862d652 | 369 | return NULL; |
b61e476f LP |
370 | } |
371 | ||
08669709 LP |
372 | static char *friendly_disk_name(const char *src, const char *vol) { |
373 | _cleanup_free_ char *description = NULL, *mount_point = NULL; | |
374 | char *name_buffer = NULL; | |
375 | int r; | |
e7d90b71 | 376 | |
e51b9486 | 377 | assert(src); |
08669709 | 378 | assert(vol); |
e7d90b71 | 379 | |
e51b9486 HH |
380 | description = disk_description(src); |
381 | mount_point = disk_mount_point(vol); | |
382 | ||
08669709 | 383 | /* If the description string is simply the volume name, then let's not show this twice */ |
ece174c5 | 384 | if (description && streq(vol, description)) |
97b11eed | 385 | description = mfree(description); |
e51b9486 HH |
386 | |
387 | if (mount_point && description) | |
388 | r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point); | |
389 | else if (mount_point) | |
390 | r = asprintf(&name_buffer, "%s on %s", vol, mount_point); | |
391 | else if (description) | |
392 | r = asprintf(&name_buffer, "%s (%s)", description, vol); | |
08669709 LP |
393 | else |
394 | return strdup(vol); | |
e51b9486 | 395 | if (r < 0) |
08669709 LP |
396 | return NULL; |
397 | ||
398 | return name_buffer; | |
399 | } | |
400 | ||
401 | static int get_password( | |
402 | const char *vol, | |
403 | const char *src, | |
404 | usec_t until, | |
405 | bool accept_cached, | |
406 | char ***ret) { | |
407 | ||
408 | _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL; | |
409 | _cleanup_strv_free_erase_ char **passwords = NULL; | |
410 | char **p, *id; | |
411 | int r = 0; | |
412 | ||
413 | assert(vol); | |
414 | assert(src); | |
415 | assert(ret); | |
416 | ||
417 | friendly = friendly_disk_name(src, vol); | |
418 | if (!friendly) | |
e51b9486 HH |
419 | return log_oom(); |
420 | ||
08669709 LP |
421 | if (asprintf(&text, "Please enter passphrase for disk %s:", friendly) < 0) |
422 | return log_oom(); | |
e51b9486 | 423 | |
08669709 LP |
424 | disk_path = cescape(src); |
425 | if (!disk_path) | |
e7d90b71 JJ |
426 | return log_oom(); |
427 | ||
ea7e7c1e | 428 | id = strjoina("cryptsetup:", disk_path); |
9fa1de96 | 429 | |
ab84f5b9 ZJS |
430 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, |
431 | ASK_PASSWORD_PUSH_CACHE | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED), | |
432 | &passwords); | |
23bbb0de MS |
433 | if (r < 0) |
434 | return log_error_errno(r, "Failed to query password: %m"); | |
e7d90b71 | 435 | |
f75cac37 | 436 | if (arg_verify) { |
ab84f5b9 ZJS |
437 | _cleanup_strv_free_erase_ char **passwords2 = NULL; |
438 | ||
1602b008 | 439 | assert(strv_length(passwords) == 1); |
e7d90b71 | 440 | |
08669709 | 441 | if (asprintf(&text, "Please enter passphrase for disk %s (verification):", friendly) < 0) |
ab84f5b9 | 442 | return log_oom(); |
e7d90b71 | 443 | |
ea7e7c1e | 444 | id = strjoina("cryptsetup-verification:", disk_path); |
9fa1de96 | 445 | |
e287086b | 446 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2); |
ab84f5b9 ZJS |
447 | if (r < 0) |
448 | return log_error_errno(r, "Failed to query verification password: %m"); | |
e7d90b71 JJ |
449 | |
450 | assert(strv_length(passwords2) == 1); | |
451 | ||
8bc6ade7 LP |
452 | if (!streq(passwords[0], passwords2[0])) |
453 | return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN), | |
454 | "Passwords did not match, retrying."); | |
e7d90b71 JJ |
455 | } |
456 | ||
1602b008 | 457 | strv_uniq(passwords); |
e7d90b71 | 458 | |
1602b008 | 459 | STRV_FOREACH(p, passwords) { |
e7d90b71 JJ |
460 | char *c; |
461 | ||
f75cac37 | 462 | if (strlen(*p)+1 >= arg_key_size) |
e7d90b71 JJ |
463 | continue; |
464 | ||
465 | /* Pad password if necessary */ | |
1602b008 | 466 | c = new(char, arg_key_size); |
ab84f5b9 ZJS |
467 | if (!c) |
468 | return log_oom(); | |
e7d90b71 | 469 | |
f75cac37 | 470 | strncpy(c, *p, arg_key_size); |
d135419e | 471 | free_and_replace(*p, c); |
e7d90b71 JJ |
472 | } |
473 | ||
ae2a15bc | 474 | *ret = TAKE_PTR(passwords); |
1602b008 | 475 | |
ab84f5b9 | 476 | return 0; |
e7d90b71 JJ |
477 | } |
478 | ||
1602b008 LP |
479 | static int attach_tcrypt( |
480 | struct crypt_device *cd, | |
481 | const char *name, | |
482 | const char *key_file, | |
7407f689 LP |
483 | const void *key_data, |
484 | size_t key_data_size, | |
1602b008 LP |
485 | char **passwords, |
486 | uint32_t flags) { | |
487 | ||
8cf3ca80 JJ |
488 | int r = 0; |
489 | _cleanup_free_ char *passphrase = NULL; | |
490 | struct crypt_params_tcrypt params = { | |
491 | .flags = CRYPT_TCRYPT_LEGACY_MODES, | |
f75cac37 LP |
492 | .keyfiles = (const char **)arg_tcrypt_keyfiles, |
493 | .keyfiles_count = strv_length(arg_tcrypt_keyfiles) | |
8cf3ca80 JJ |
494 | }; |
495 | ||
496 | assert(cd); | |
497 | assert(name); | |
7407f689 | 498 | assert(key_file || key_data || !strv_isempty(passwords)); |
8cf3ca80 | 499 | |
e514aa1e FS |
500 | if (arg_pkcs11_uri) |
501 | /* Ask for a regular password */ | |
502 | return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), | |
503 | "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11 support."); | |
08669709 | 504 | |
f75cac37 | 505 | if (arg_tcrypt_hidden) |
8cf3ca80 JJ |
506 | params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER; |
507 | ||
f75cac37 | 508 | if (arg_tcrypt_system) |
8cf3ca80 JJ |
509 | params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER; |
510 | ||
52028838 GH |
511 | if (arg_tcrypt_veracrypt) |
512 | params.flags |= CRYPT_TCRYPT_VERA_MODES; | |
52028838 | 513 | |
7407f689 LP |
514 | if (key_data) { |
515 | params.passphrase = key_data; | |
516 | params.passphrase_size = key_data_size; | |
517 | } else { | |
518 | if (key_file) { | |
519 | r = read_one_line_file(key_file, &passphrase); | |
520 | if (r < 0) { | |
521 | log_error_errno(r, "Failed to read password file '%s': %m", key_file); | |
522 | return -EAGAIN; /* log with the actual error, but return EAGAIN */ | |
523 | } | |
8cf3ca80 | 524 | |
7407f689 LP |
525 | params.passphrase = passphrase; |
526 | } else | |
527 | params.passphrase = passwords[0]; | |
528 | ||
529 | params.passphrase_size = strlen(params.passphrase); | |
530 | } | |
8cf3ca80 JJ |
531 | |
532 | r = crypt_load(cd, CRYPT_TCRYPT, ¶ms); | |
533 | if (r < 0) { | |
7407f689 | 534 | if (r == -EPERM) { |
cb6c9283 | 535 | if (key_data) |
7407f689 | 536 | log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)"); |
7407f689 | 537 | |
cb6c9283 | 538 | if (key_file) |
7407f689 | 539 | log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file); |
cb6c9283 LP |
540 | |
541 | return -EAGAIN; /* log the actual error, but return EAGAIN */ | |
6f177c7d LP |
542 | } |
543 | ||
544 | return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd)); | |
8cf3ca80 JJ |
545 | } |
546 | ||
6f177c7d LP |
547 | r = crypt_activate_by_volume_key(cd, name, NULL, 0, flags); |
548 | if (r < 0) | |
549 | return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd)); | |
550 | ||
551 | return 0; | |
8cf3ca80 JJ |
552 | } |
553 | ||
e2c2f868 LP |
554 | static char *make_bindname(const char *volume) { |
555 | char *s; | |
556 | ||
557 | if (asprintf(&s, "@%" PRIx64"/cryptsetup/%s", random_u64(), volume) < 0) | |
558 | return NULL; | |
559 | ||
560 | return s; | |
561 | } | |
562 | ||
6cc27c29 | 563 | static int attach_luks_or_plain_or_bitlk( |
9c5253ff LP |
564 | struct crypt_device *cd, |
565 | const char *name, | |
566 | const char *key_file, | |
7407f689 LP |
567 | const void *key_data, |
568 | size_t key_data_size, | |
9c5253ff | 569 | char **passwords, |
08669709 LP |
570 | uint32_t flags, |
571 | usec_t until) { | |
9c5253ff | 572 | |
10fb4e35 JJ |
573 | int r = 0; |
574 | bool pass_volume_key = false; | |
575 | ||
576 | assert(cd); | |
577 | assert(name); | |
10fb4e35 | 578 | |
2e4beb87 | 579 | if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) { |
4eac2773 MP |
580 | struct crypt_params_plain params = { |
581 | .offset = arg_offset, | |
582 | .skip = arg_skip, | |
a9fc6406 | 583 | .sector_size = arg_sector_size, |
4eac2773 | 584 | }; |
10fb4e35 JJ |
585 | const char *cipher, *cipher_mode; |
586 | _cleanup_free_ char *truncated_cipher = NULL; | |
587 | ||
f75cac37 | 588 | if (arg_hash) { |
10fb4e35 | 589 | /* plain isn't a real hash type. it just means "use no hash" */ |
f75cac37 LP |
590 | if (!streq(arg_hash, "plain")) |
591 | params.hash = arg_hash; | |
8a52210c ZJS |
592 | } else if (!key_file) |
593 | /* for CRYPT_PLAIN, the behaviour of cryptsetup | |
594 | * package is to not hash when a key file is provided */ | |
10fb4e35 JJ |
595 | params.hash = "ripemd160"; |
596 | ||
f75cac37 | 597 | if (arg_cipher) { |
10fb4e35 JJ |
598 | size_t l; |
599 | ||
f75cac37 LP |
600 | l = strcspn(arg_cipher, "-"); |
601 | truncated_cipher = strndup(arg_cipher, l); | |
10fb4e35 JJ |
602 | if (!truncated_cipher) |
603 | return log_oom(); | |
604 | ||
605 | cipher = truncated_cipher; | |
f75cac37 | 606 | cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain"; |
10fb4e35 JJ |
607 | } else { |
608 | cipher = "aes"; | |
609 | cipher_mode = "cbc-essiv:sha256"; | |
610 | } | |
611 | ||
aed68083 | 612 | /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */ |
6131a78b | 613 | arg_keyfile_size = arg_key_size; |
10fb4e35 | 614 | |
30747265 | 615 | /* In contrast to what the name crypt_format() might suggest this doesn't actually format |
aed68083 | 616 | * anything, it just configures encryption parameters when used for plain mode. */ |
1602b008 | 617 | r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, ¶ms); |
2e4beb87 MB |
618 | if (r < 0) |
619 | return log_error_errno(r, "Loading of cryptographic parameters failed: %m"); | |
10fb4e35 JJ |
620 | |
621 | /* hash == NULL implies the user passed "plain" */ | |
622 | pass_volume_key = (params.hash == NULL); | |
623 | } | |
10fb4e35 JJ |
624 | |
625 | log_info("Set cipher %s, mode %s, key size %i bits for device %s.", | |
626 | crypt_get_cipher(cd), | |
627 | crypt_get_cipher_mode(cd), | |
628 | crypt_get_volume_key_size(cd)*8, | |
629 | crypt_get_device_name(cd)); | |
630 | ||
08669709 LP |
631 | if (arg_pkcs11_uri) { |
632 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
633 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
634 | _cleanup_free_ void *decrypted_key = NULL; | |
635 | _cleanup_free_ char *friendly = NULL; | |
636 | size_t decrypted_key_size = 0; | |
637 | ||
7407f689 | 638 | if (!key_file && !key_data) |
08669709 LP |
639 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing."); |
640 | ||
641 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
642 | if (!friendly) | |
643 | return log_oom(); | |
644 | ||
645 | for (;;) { | |
646 | bool processed = false; | |
647 | ||
648 | r = decrypt_pkcs11_key( | |
4d1bb8f3 | 649 | name, |
08669709 LP |
650 | friendly, |
651 | arg_pkcs11_uri, | |
7407f689 LP |
652 | key_file, arg_keyfile_size, arg_keyfile_offset, |
653 | key_data, key_data_size, | |
08669709 LP |
654 | until, |
655 | &decrypted_key, &decrypted_key_size); | |
656 | if (r >= 0) | |
657 | break; | |
658 | if (r != -EAGAIN) /* EAGAIN means: token not found */ | |
659 | return r; | |
660 | ||
661 | if (!monitor) { | |
662 | /* We didn't find the token. In this case, watch for it via udev. Let's | |
663 | * create an event loop and monitor first. */ | |
664 | ||
665 | assert(!event); | |
666 | ||
667 | r = sd_event_default(&event); | |
668 | if (r < 0) | |
669 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
670 | ||
671 | r = sd_device_monitor_new(&monitor); | |
672 | if (r < 0) | |
673 | return log_error_errno(r, "Failed to allocate device monitor: %m"); | |
674 | ||
675 | r = sd_device_monitor_filter_add_match_tag(monitor, "security-device"); | |
676 | if (r < 0) | |
677 | return log_error_errno(r, "Failed to configure device monitor: %m"); | |
678 | ||
679 | r = sd_device_monitor_attach_event(monitor, event); | |
680 | if (r < 0) | |
681 | return log_error_errno(r, "Failed to attach device monitor: %m"); | |
682 | ||
683 | r = sd_device_monitor_start(monitor, NULL, NULL); | |
684 | if (r < 0) | |
685 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
686 | ||
687 | log_notice("Security token %s not present for unlocking volume %s, please plug it in.", | |
4eb08bdb | 688 | arg_pkcs11_uri, friendly); |
08669709 LP |
689 | |
690 | /* Let's immediately rescan in case the token appeared in the time we needed | |
691 | * to create and configure the monitor */ | |
692 | continue; | |
693 | } | |
694 | ||
695 | for (;;) { | |
696 | /* Wait for one event, and then eat all subsequent events until there are no | |
697 | * further ones */ | |
698 | r = sd_event_run(event, processed ? 0 : UINT64_MAX); | |
699 | if (r < 0) | |
700 | return log_error_errno(r, "Failed to run event loop: %m"); | |
701 | if (r == 0) | |
702 | break; | |
703 | ||
704 | processed = true; | |
705 | } | |
706 | ||
707 | log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11..."); | |
708 | } | |
709 | ||
710 | if (pass_volume_key) | |
711 | r = crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); | |
712 | else { | |
713 | _cleanup_free_ char *base64_encoded = NULL; | |
714 | ||
715 | /* Before using this key as passphrase we base64 encode it. Why? For compatibility | |
716 | * with homed's PKCS#11 hookup: there we want to use the key we acquired through | |
717 | * PKCS#11 for other authentication/decryption mechanisms too, and some of them do | |
718 | * not not take arbitrary binary blobs, but require NUL-terminated strings — most | |
719 | * importantly UNIX password hashes. Hence, for compatibility we want to use a string | |
720 | * without embedded NUL here too, and that's easiest to generate from a binary blob | |
721 | * via base64 encoding. */ | |
722 | ||
723 | r = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); | |
724 | if (r < 0) | |
725 | return log_oom(); | |
726 | ||
727 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, base64_encoded, strlen(base64_encoded), flags); | |
728 | } | |
729 | if (r == -EPERM) { | |
730 | log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)"); | |
731 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
732 | } | |
733 | if (r < 0) | |
734 | return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m"); | |
735 | ||
7407f689 LP |
736 | } else if (key_data) { |
737 | if (pass_volume_key) | |
738 | r = crypt_activate_by_volume_key(cd, name, key_data, key_data_size, flags); | |
739 | else | |
740 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data, key_data_size, flags); | |
741 | if (r == -EPERM) { | |
742 | log_error_errno(r, "Failed to activate. (Key incorrect?)"); | |
743 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
744 | } | |
745 | if (r < 0) | |
746 | return log_error_errno(r, "Failed to activate: %m"); | |
747 | ||
08669709 | 748 | } else if (key_file) { |
e2c2f868 LP |
749 | _cleanup_(erase_and_freep) char *kfdata = NULL; |
750 | _cleanup_free_ char *bindname = NULL; | |
751 | size_t kfsize; | |
752 | ||
753 | /* If we read the key via AF_UNIX, make this client recognizable */ | |
754 | bindname = make_bindname(name); | |
755 | if (!bindname) | |
756 | return log_oom(); | |
757 | ||
758 | r = read_full_file_full( | |
759 | AT_FDCWD, key_file, | |
760 | arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset, | |
761 | arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size, | |
762 | READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET, | |
763 | bindname, | |
764 | &kfdata, &kfsize); | |
765 | if (r == -ENOENT) { | |
766 | log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file); | |
6f177c7d | 767 | return -EAGAIN; /* Log actual error, but return EAGAIN */ |
c20db388 | 768 | } |
e2c2f868 LP |
769 | |
770 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags); | |
771 | if (r == -EPERM) { | |
772 | log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file); | |
c20db388 | 773 | return -EAGAIN; /* Log actual error, but return EAGAIN */ |
10fb4e35 | 774 | } |
6f177c7d LP |
775 | if (r < 0) |
776 | return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file); | |
9c5253ff | 777 | |
10fb4e35 JJ |
778 | } else { |
779 | char **p; | |
780 | ||
6f177c7d | 781 | r = -EINVAL; |
10fb4e35 JJ |
782 | STRV_FOREACH(p, passwords) { |
783 | if (pass_volume_key) | |
f75cac37 | 784 | r = crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags); |
10fb4e35 | 785 | else |
f75cac37 | 786 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags); |
10fb4e35 JJ |
787 | if (r >= 0) |
788 | break; | |
789 | } | |
6f177c7d LP |
790 | if (r == -EPERM) { |
791 | log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)"); | |
792 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
793 | } | |
794 | if (r < 0) | |
795 | return log_error_errno(r, "Failed to activate with specified passphrase: %m"); | |
10fb4e35 JJ |
796 | } |
797 | ||
798 | return r; | |
799 | } | |
800 | ||
dd5e696d | 801 | static int help(void) { |
37ec0fdd LP |
802 | _cleanup_free_ char *link = NULL; |
803 | int r; | |
804 | ||
805 | r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link); | |
806 | if (r < 0) | |
807 | return log_oom(); | |
dd5e696d LP |
808 | |
809 | printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n" | |
810 | "%s detach VOLUME\n\n" | |
37ec0fdd LP |
811 | "Attaches or detaches an encrypted block device.\n" |
812 | "\nSee the %s for details.\n" | |
813 | , program_invocation_short_name | |
814 | , program_invocation_short_name | |
815 | , link | |
816 | ); | |
dd5e696d LP |
817 | |
818 | return 0; | |
819 | } | |
820 | ||
d5d1ae15 LP |
821 | static uint32_t determine_flags(void) { |
822 | uint32_t flags = 0; | |
823 | ||
824 | if (arg_readonly) | |
825 | flags |= CRYPT_ACTIVATE_READONLY; | |
826 | ||
827 | if (arg_discards) | |
828 | flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; | |
829 | ||
830 | if (arg_same_cpu_crypt) | |
831 | flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT; | |
832 | ||
833 | if (arg_submit_from_crypt_cpus) | |
834 | flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS; | |
835 | ||
408c81f6 MS |
836 | #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF |
837 | /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */ | |
838 | /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */ | |
839 | flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF; | |
840 | #endif | |
841 | ||
d5d1ae15 LP |
842 | return flags; |
843 | } | |
844 | ||
d3d49e76 LP |
845 | static void remove_and_erasep(const char **p) { |
846 | int r; | |
847 | ||
848 | if (!*p) | |
849 | return; | |
850 | ||
851 | r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE); | |
852 | if (r < 0 && r != -ENOENT) | |
853 | log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p); | |
854 | } | |
855 | ||
3a40f366 | 856 | static int run(int argc, char *argv[]) { |
294bd454 | 857 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
3a40f366 | 858 | int r; |
e23a0ce8 | 859 | |
3a40f366 YW |
860 | if (argc <= 1) |
861 | return help(); | |
dd5e696d | 862 | |
d7a0f1f4 FS |
863 | if (argc < 3) |
864 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
865 | "This program requires at least two arguments."); | |
e23a0ce8 | 866 | |
6bf3c61c | 867 | log_setup_service(); |
e23a0ce8 | 868 | |
efc3b12f | 869 | cryptsetup_enable_logging(cd); |
568a8404 | 870 | |
4c12626c LP |
871 | umask(0022); |
872 | ||
260ab287 | 873 | if (streq(argv[1], "attach")) { |
7f4e0805 | 874 | uint32_t flags = 0; |
10fb4e35 | 875 | unsigned tries; |
260ab287 | 876 | usec_t until; |
e2d480b9 | 877 | crypt_status_info status; |
d3d49e76 | 878 | _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL; |
e51b9486 | 879 | const char *key_file = NULL; |
7407f689 LP |
880 | _cleanup_(erase_and_freep) void *key_data = NULL; |
881 | size_t key_data_size = 0; | |
7f4e0805 | 882 | |
b61e476f LP |
883 | /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */ |
884 | ||
0ffff81a LP |
885 | if (argc < 4) |
886 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments."); | |
7f4e0805 | 887 | |
7407f689 LP |
888 | if (!filename_is_valid(argv[2])) |
889 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", argv[2]); | |
890 | ||
9120aa82 LP |
891 | if (argc >= 5 && !STR_IN_SET(argv[4], "", "-", "none")) { |
892 | if (path_is_absolute(argv[4])) | |
7f4e0805 | 893 | key_file = argv[4]; |
9120aa82 LP |
894 | else |
895 | log_warning("Password file path '%s' is not absolute. Ignoring.", argv[4]); | |
7f4e0805 LP |
896 | } |
897 | ||
9120aa82 | 898 | if (argc >= 6 && !STR_IN_SET(argv[5], "", "-", "none")) { |
3a40f366 YW |
899 | r = parse_options(argv[5]); |
900 | if (r < 0) | |
901 | return r; | |
74b1c371 | 902 | } |
e23a0ce8 | 903 | |
7bb553bb ZJS |
904 | log_debug("%s %s ← %s type=%s cipher=%s", __func__, |
905 | argv[2], argv[3], strempty(arg_type), strempty(arg_cipher)); | |
906 | ||
b853f6e9 | 907 | /* A delicious drop of snake oil */ |
9c5253ff | 908 | (void) mlockall(MCL_FUTURE); |
b853f6e9 | 909 | |
7407f689 LP |
910 | if (!key_file) { |
911 | const char *fn; | |
912 | ||
913 | /* If a key file is not explicitly specified, search for a key in a well defined | |
914 | * search path, and load it. */ | |
915 | ||
916 | fn = strjoina(argv[2], ".key"); | |
917 | r = load_key_file(fn, | |
918 | STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"), | |
919 | 0, 0, /* Note we leave arg_keyfile_offset/arg_keyfile_size as something that only applies to arg_keyfile! */ | |
920 | &key_data, &key_data_size); | |
921 | if (r < 0) | |
922 | return r; | |
923 | if (r > 0) | |
924 | log_debug("Automatically discovered key for volume '%s'.", argv[2]); | |
925 | } else if (arg_keyfile_erase) | |
d3d49e76 LP |
926 | destroy_key_file = key_file; /* let's get this baby erased when we leave */ |
927 | ||
7376e835 AC |
928 | if (arg_header) { |
929 | log_debug("LUKS header: %s", arg_header); | |
5f4bfe56 | 930 | r = crypt_init(&cd, arg_header); |
7376e835 | 931 | } else |
5f4bfe56 | 932 | r = crypt_init(&cd, argv[3]); |
3a40f366 YW |
933 | if (r < 0) |
934 | return log_error_errno(r, "crypt_init() failed: %m"); | |
e23a0ce8 | 935 | |
efc3b12f | 936 | cryptsetup_enable_logging(cd); |
7f4e0805 | 937 | |
260ab287 | 938 | status = crypt_status(cd, argv[2]); |
3742095b | 939 | if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { |
260ab287 | 940 | log_info("Volume %s already active.", argv[2]); |
3a40f366 | 941 | return 0; |
7f4e0805 LP |
942 | } |
943 | ||
d5d1ae15 | 944 | flags = determine_flags(); |
2c65512e | 945 | |
0864d311 | 946 | if (arg_timeout == USEC_INFINITY) |
7dcda352 | 947 | until = 0; |
0864d311 AS |
948 | else |
949 | until = now(CLOCK_MONOTONIC) + arg_timeout; | |
260ab287 | 950 | |
6131a78b | 951 | arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8)); |
e2d480b9 | 952 | |
10fb4e35 JJ |
953 | if (key_file) { |
954 | struct stat st; | |
e2d480b9 | 955 | |
10fb4e35 JJ |
956 | /* Ideally we'd do this on the open fd, but since this is just a |
957 | * warning it's OK to do this in two steps. */ | |
3f4d56a0 MP |
958 | if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005)) |
959 | log_warning("Key file %s is world-readable. This is not a good idea!", key_file); | |
e2d480b9 LP |
960 | } |
961 | ||
6930d069 LP |
962 | if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) { |
963 | r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL); | |
ea9a9d49 MB |
964 | if (r < 0) |
965 | return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd)); | |
966 | ||
967 | if (arg_header) { | |
968 | r = crypt_set_data_device(cd, argv[3]); | |
969 | if (r < 0) | |
970 | return log_error_errno(r, "Failed to set LUKS data device %s: %m", argv[3]); | |
971 | } | |
d90874b4 | 972 | |
894bb3ca | 973 | /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */ |
7407f689 | 974 | if (!key_file && !key_data) { |
894bb3ca MB |
975 | r = crypt_activate_by_token(cd, argv[2], CRYPT_ANY_TOKEN, NULL, flags); |
976 | if (r >= 0) { | |
977 | log_debug("Volume %s activated with LUKS token id %i.", argv[2], r); | |
978 | return 0; | |
979 | } | |
980 | ||
981 | log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd)); | |
982 | } | |
ea9a9d49 MB |
983 | } |
984 | ||
6cc27c29 MF |
985 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
986 | #ifdef CRYPT_BITLK | |
5af39ac8 | 987 | if (streq_ptr(arg_type, CRYPT_BITLK)) { |
6cc27c29 MF |
988 | r = crypt_load(cd, CRYPT_BITLK, NULL); |
989 | if (r < 0) | |
990 | return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd)); | |
991 | } | |
992 | #endif | |
993 | ||
f75cac37 | 994 | for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) { |
ab84f5b9 | 995 | _cleanup_strv_free_erase_ char **passwords = NULL; |
260ab287 | 996 | |
0ba6f85e LP |
997 | /* When we were able to acquire multiple keys, let's always process them in this order: |
998 | * | |
999 | * 1. A key acquired via PKCS#11 token | |
1000 | * 2. The discovered key: i.e. key_data + key_data_size | |
1001 | * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size | |
1002 | * 4. The empty password, in case arg_try_empty_password is set | |
1003 | * 5. We enquire the user for a password | |
1004 | */ | |
1005 | ||
7407f689 | 1006 | if (!key_file && !key_data && !arg_pkcs11_uri) { |
0ba6f85e LP |
1007 | |
1008 | if (arg_try_empty_password) { | |
1009 | /* Hmm, let's try an empty password now, but only once */ | |
1010 | arg_try_empty_password = false; | |
1011 | ||
1012 | key_data = strdup(""); | |
1013 | if (!key_data) | |
1014 | return log_oom(); | |
1015 | ||
1016 | key_data_size = 0; | |
1017 | } else { | |
1018 | /* Ask the user for a passphrase only as last resort, if we have | |
1019 | * nothing else to check for */ | |
1020 | ||
1021 | r = get_password(argv[2], argv[3], until, tries == 0 && !arg_verify, &passwords); | |
1022 | if (r == -EAGAIN) | |
1023 | continue; | |
1024 | if (r < 0) | |
1025 | return r; | |
1026 | } | |
260ab287 LP |
1027 | } |
1028 | ||
f75cac37 | 1029 | if (streq_ptr(arg_type, CRYPT_TCRYPT)) |
7407f689 | 1030 | r = attach_tcrypt(cd, argv[2], key_file, key_data, key_data_size, passwords, flags); |
8cf3ca80 | 1031 | else |
6cc27c29 | 1032 | r = attach_luks_or_plain_or_bitlk(cd, argv[2], key_file, key_data, key_data_size, passwords, flags, until); |
5f4bfe56 | 1033 | if (r >= 0) |
260ab287 | 1034 | break; |
6f177c7d LP |
1035 | if (r != -EAGAIN) |
1036 | return r; | |
260ab287 | 1037 | |
2424fb7e LP |
1038 | /* Key not correct? Let's try again! */ |
1039 | ||
6f177c7d | 1040 | key_file = NULL; |
7407f689 LP |
1041 | key_data = erase_and_free(key_data); |
1042 | key_data_size = 0; | |
2424fb7e | 1043 | arg_pkcs11_uri = mfree(arg_pkcs11_uri); |
7f4e0805 LP |
1044 | } |
1045 | ||
0ffff81a LP |
1046 | if (arg_tries != 0 && tries >= arg_tries) |
1047 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up."); | |
7f4e0805 LP |
1048 | |
1049 | } else if (streq(argv[1], "detach")) { | |
7f4e0805 | 1050 | |
7407f689 LP |
1051 | if (!filename_is_valid(argv[2])) |
1052 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", argv[2]); | |
1053 | ||
5f4bfe56 LP |
1054 | r = crypt_init_by_name(&cd, argv[2]); |
1055 | if (r == -ENODEV) { | |
a0bfc9c2 | 1056 | log_info("Volume %s already inactive.", argv[2]); |
3a40f366 | 1057 | return 0; |
7f4e0805 | 1058 | } |
3a40f366 YW |
1059 | if (r < 0) |
1060 | return log_error_errno(r, "crypt_init_by_name() failed: %m"); | |
7f4e0805 | 1061 | |
efc3b12f | 1062 | cryptsetup_enable_logging(cd); |
7f4e0805 | 1063 | |
5f4bfe56 | 1064 | r = crypt_deactivate(cd, argv[2]); |
3a40f366 YW |
1065 | if (r < 0) |
1066 | return log_error_errno(r, "Failed to deactivate: %m"); | |
e23a0ce8 | 1067 | |
0ffff81a LP |
1068 | } else |
1069 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", argv[1]); | |
e23a0ce8 | 1070 | |
3a40f366 | 1071 | return 0; |
e23a0ce8 | 1072 | } |
3a40f366 YW |
1073 | |
1074 | DEFINE_MAIN_FUNCTION(run); |