]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e23a0ce8 | 2 | |
7f4e0805 | 3 | #include <errno.h> |
07630cea LP |
4 | #include <mntent.h> |
5 | #include <string.h> | |
6 | #include <sys/mman.h> | |
ca78ad1d ZJS |
7 | #include <sys/stat.h> |
8 | #include <sys/types.h> | |
9 | #include <unistd.h> | |
e23a0ce8 | 10 | |
4f5dd394 LP |
11 | #include "sd-device.h" |
12 | ||
b5efdb8a | 13 | #include "alloc-util.h" |
4f5dd394 | 14 | #include "ask-password-api.h" |
294bd454 | 15 | #include "crypt-util.h" |
4f5dd394 LP |
16 | #include "device-util.h" |
17 | #include "escape.h" | |
8cf3ca80 | 18 | #include "fileio.h" |
e23a0ce8 | 19 | #include "log.h" |
3a40f366 | 20 | #include "main-func.h" |
4349cd7c | 21 | #include "mount-util.h" |
d8b4d14d | 22 | #include "nulstr-util.h" |
6bedfcbb | 23 | #include "parse-util.h" |
9eb977db | 24 | #include "path-util.h" |
d8b4d14d | 25 | #include "pretty-print.h" |
07630cea | 26 | #include "string-util.h" |
21bc923a | 27 | #include "strv.h" |
7f4e0805 | 28 | |
b3b4ebab OK |
29 | /* internal helper */ |
30 | #define ANY_LUKS "LUKS" | |
a9fc6406 DJL |
31 | /* as in src/cryptsetup.h */ |
32 | #define CRYPT_SECTOR_SIZE 512 | |
33 | #define CRYPT_MAX_SECTOR_SIZE 4096 | |
b3b4ebab OK |
34 | |
35 | static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */ | |
f75cac37 LP |
36 | static char *arg_cipher = NULL; |
37 | static unsigned arg_key_size = 0; | |
645461f0 | 38 | #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE |
a9fc6406 | 39 | static unsigned arg_sector_size = CRYPT_SECTOR_SIZE; |
645461f0 | 40 | #endif |
f75cac37 LP |
41 | static int arg_key_slot = CRYPT_ANY_SLOT; |
42 | static unsigned arg_keyfile_size = 0; | |
dc0a3555 | 43 | static uint64_t arg_keyfile_offset = 0; |
f75cac37 | 44 | static char *arg_hash = NULL; |
7376e835 | 45 | static char *arg_header = NULL; |
f75cac37 LP |
46 | static unsigned arg_tries = 3; |
47 | static bool arg_readonly = false; | |
48 | static bool arg_verify = false; | |
49 | static bool arg_discards = false; | |
2c65512e YW |
50 | static bool arg_same_cpu_crypt = false; |
51 | static bool arg_submit_from_crypt_cpus = false; | |
f75cac37 LP |
52 | static bool arg_tcrypt_hidden = false; |
53 | static bool arg_tcrypt_system = false; | |
2e914ef4 | 54 | #ifdef CRYPT_TCRYPT_VERA_MODES |
52028838 | 55 | static bool arg_tcrypt_veracrypt = false; |
2e914ef4 | 56 | #endif |
f75cac37 | 57 | static char **arg_tcrypt_keyfiles = NULL; |
4eac2773 MP |
58 | static uint64_t arg_offset = 0; |
59 | static uint64_t arg_skip = 0; | |
0864d311 | 60 | static usec_t arg_timeout = USEC_INFINITY; |
7f4e0805 | 61 | |
3a40f366 YW |
62 | STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep); |
63 | STATIC_DESTRUCTOR_REGISTER(arg_hash, freep); | |
64 | STATIC_DESTRUCTOR_REGISTER(arg_header, freep); | |
65 | STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep); | |
66 | ||
1fc76335 LP |
67 | /* Options Debian's crypttab knows we don't: |
68 | ||
1fc76335 LP |
69 | precheck= |
70 | check= | |
71 | checkargs= | |
72 | noearly= | |
73 | loud= | |
74 | keyscript= | |
75 | */ | |
76 | ||
7f4e0805 | 77 | static int parse_one_option(const char *option) { |
fb4650aa ZJS |
78 | const char *val; |
79 | int r; | |
80 | ||
7f4e0805 LP |
81 | assert(option); |
82 | ||
83 | /* Handled outside of this tool */ | |
f7576eb9 | 84 | if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev")) |
7f4e0805 LP |
85 | return 0; |
86 | ||
fb4650aa ZJS |
87 | if ((val = startswith(option, "cipher="))) { |
88 | r = free_and_strdup(&arg_cipher, val); | |
89 | if (r < 0) | |
4b93637f | 90 | return log_oom(); |
7f4e0805 | 91 | |
fb4650aa | 92 | } else if ((val = startswith(option, "size="))) { |
7f4e0805 | 93 | |
fb4650aa ZJS |
94 | r = safe_atou(val, &arg_key_size); |
95 | if (r < 0) { | |
96 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
97 | return 0; |
98 | } | |
99 | ||
6131a78b DH |
100 | if (arg_key_size % 8) { |
101 | log_error("size= not a multiple of 8, ignoring."); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | arg_key_size /= 8; | |
106 | ||
a9fc6406 DJL |
107 | } else if ((val = startswith(option, "sector-size="))) { |
108 | ||
109 | #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE | |
110 | r = safe_atou(val, &arg_sector_size); | |
111 | if (r < 0) { | |
112 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | if (arg_sector_size % 2) { | |
117 | log_error("sector-size= not a multiple of 2, ignoring."); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) { | |
122 | log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE); | |
123 | return 0; | |
124 | } | |
125 | #else | |
126 | log_error("sector-size= is not supported, compiled with old libcryptsetup."); | |
127 | return 0; | |
128 | #endif | |
129 | ||
fb4650aa | 130 | } else if ((val = startswith(option, "key-slot="))) { |
b4a11878 | 131 | |
b3b4ebab | 132 | arg_type = ANY_LUKS; |
fb4650aa ZJS |
133 | r = safe_atoi(val, &arg_key_slot); |
134 | if (r < 0) { | |
135 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
b4a11878 CS |
136 | return 0; |
137 | } | |
138 | ||
fb4650aa | 139 | } else if ((val = startswith(option, "tcrypt-keyfile="))) { |
8cf3ca80 | 140 | |
f75cac37 | 141 | arg_type = CRYPT_TCRYPT; |
fb4650aa ZJS |
142 | if (path_is_absolute(val)) { |
143 | if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) | |
4b93637f LP |
144 | return log_oom(); |
145 | } else | |
fb4650aa | 146 | log_error("Key file path \"%s\" is not absolute. Ignoring.", val); |
8cf3ca80 | 147 | |
fb4650aa | 148 | } else if ((val = startswith(option, "keyfile-size="))) { |
4271d823 | 149 | |
fb4650aa ZJS |
150 | r = safe_atou(val, &arg_keyfile_size); |
151 | if (r < 0) { | |
152 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
4271d823 TG |
153 | return 0; |
154 | } | |
155 | ||
fb4650aa | 156 | } else if ((val = startswith(option, "keyfile-offset="))) { |
dc0a3555 | 157 | uint64_t off; |
880a599e | 158 | |
dc0a3555 | 159 | r = safe_atou64(val, &off); |
fb4650aa ZJS |
160 | if (r < 0) { |
161 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
880a599e TG |
162 | return 0; |
163 | } | |
164 | ||
dc0a3555 ZJS |
165 | if ((size_t) off != off) { |
166 | /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */ | |
167 | log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | arg_keyfile_offset = off; | |
172 | ||
fb4650aa ZJS |
173 | } else if ((val = startswith(option, "hash="))) { |
174 | r = free_and_strdup(&arg_hash, val); | |
175 | if (r < 0) | |
4b93637f | 176 | return log_oom(); |
7f4e0805 | 177 | |
fb4650aa | 178 | } else if ((val = startswith(option, "header="))) { |
b3b4ebab | 179 | arg_type = ANY_LUKS; |
7376e835 | 180 | |
baaa35ad ZJS |
181 | if (!path_is_absolute(val)) |
182 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
183 | "Header path \"%s\" is not absolute, refusing.", val); | |
7376e835 | 184 | |
baaa35ad ZJS |
185 | if (arg_header) |
186 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
187 | "Duplicate header= option, refusing."); | |
7376e835 | 188 | |
fb4650aa | 189 | arg_header = strdup(val); |
7376e835 AC |
190 | if (!arg_header) |
191 | return log_oom(); | |
192 | ||
fb4650aa | 193 | } else if ((val = startswith(option, "tries="))) { |
7f4e0805 | 194 | |
fb4650aa ZJS |
195 | r = safe_atou(val, &arg_tries); |
196 | if (r < 0) { | |
197 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
198 | return 0; |
199 | } | |
200 | ||
f75cac37 LP |
201 | } else if (STR_IN_SET(option, "readonly", "read-only")) |
202 | arg_readonly = true; | |
7f4e0805 | 203 | else if (streq(option, "verify")) |
f75cac37 LP |
204 | arg_verify = true; |
205 | else if (STR_IN_SET(option, "allow-discards", "discard")) | |
206 | arg_discards = true; | |
2c65512e YW |
207 | else if (streq(option, "same-cpu-crypt")) |
208 | arg_same_cpu_crypt = true; | |
209 | else if (streq(option, "submit-from-crypt-cpus")) | |
210 | arg_submit_from_crypt_cpus = true; | |
260ab287 | 211 | else if (streq(option, "luks")) |
b3b4ebab | 212 | arg_type = ANY_LUKS; |
8cf3ca80 | 213 | else if (streq(option, "tcrypt")) |
f75cac37 | 214 | arg_type = CRYPT_TCRYPT; |
8cf3ca80 | 215 | else if (streq(option, "tcrypt-hidden")) { |
f75cac37 LP |
216 | arg_type = CRYPT_TCRYPT; |
217 | arg_tcrypt_hidden = true; | |
8cf3ca80 | 218 | } else if (streq(option, "tcrypt-system")) { |
f75cac37 LP |
219 | arg_type = CRYPT_TCRYPT; |
220 | arg_tcrypt_system = true; | |
52028838 GH |
221 | } else if (streq(option, "tcrypt-veracrypt")) { |
222 | #ifdef CRYPT_TCRYPT_VERA_MODES | |
223 | arg_type = CRYPT_TCRYPT; | |
224 | arg_tcrypt_veracrypt = true; | |
225 | #else | |
baaa35ad ZJS |
226 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
227 | "This version of cryptsetup does not support tcrypt-veracrypt; refusing."); | |
52028838 | 228 | #endif |
f75cac37 LP |
229 | } else if (STR_IN_SET(option, "plain", "swap", "tmp")) |
230 | arg_type = CRYPT_PLAIN; | |
fb4650aa | 231 | else if ((val = startswith(option, "timeout="))) { |
7f4e0805 | 232 | |
0004f698 | 233 | r = parse_sec_fix_0(val, &arg_timeout); |
fb4650aa ZJS |
234 | if (r < 0) { |
235 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 LP |
236 | return 0; |
237 | } | |
238 | ||
fb4650aa | 239 | } else if ((val = startswith(option, "offset="))) { |
4eac2773 | 240 | |
fb4650aa ZJS |
241 | r = safe_atou64(val, &arg_offset); |
242 | if (r < 0) | |
243 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 244 | |
fb4650aa | 245 | } else if ((val = startswith(option, "skip="))) { |
4eac2773 | 246 | |
fb4650aa ZJS |
247 | r = safe_atou64(val, &arg_skip); |
248 | if (r < 0) | |
249 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 250 | |
41e6f28a | 251 | } else if (!streq(option, "none")) |
fb4650aa | 252 | log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); |
7f4e0805 LP |
253 | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static int parse_options(const char *options) { | |
a2a5291b | 258 | const char *word, *state; |
7f4e0805 | 259 | size_t l; |
74b1c371 | 260 | int r; |
7f4e0805 LP |
261 | |
262 | assert(options); | |
263 | ||
a2a5291b | 264 | FOREACH_WORD_SEPARATOR(word, l, options, ",", state) { |
74b1c371 | 265 | _cleanup_free_ char *o; |
7f4e0805 | 266 | |
a2a5291b | 267 | o = strndup(word, l); |
74b1c371 | 268 | if (!o) |
7f4e0805 | 269 | return -ENOMEM; |
7f4e0805 | 270 | r = parse_one_option(o); |
7f4e0805 LP |
271 | if (r < 0) |
272 | return r; | |
273 | } | |
274 | ||
4eac2773 MP |
275 | /* sanity-check options */ |
276 | if (arg_type != NULL && !streq(arg_type, CRYPT_PLAIN)) { | |
277 | if (arg_offset) | |
278 | log_warning("offset= ignored with type %s", arg_type); | |
279 | if (arg_skip) | |
280 | log_warning("skip= ignored with type %s", arg_type); | |
281 | } | |
282 | ||
7f4e0805 LP |
283 | return 0; |
284 | } | |
285 | ||
1ca208fb | 286 | static char* disk_description(const char *path) { |
f75cac37 | 287 | static const char name_fields[] = |
74b1c371 LP |
288 | "ID_PART_ENTRY_NAME\0" |
289 | "DM_NAME\0" | |
290 | "ID_MODEL_FROM_DATABASE\0" | |
f75cac37 | 291 | "ID_MODEL\0"; |
74b1c371 | 292 | |
4afd3348 | 293 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
2c740afd | 294 | const char *i, *name; |
b1a2da0a | 295 | struct stat st; |
b1a2da0a LP |
296 | |
297 | assert(path); | |
298 | ||
299 | if (stat(path, &st) < 0) | |
300 | return NULL; | |
301 | ||
302 | if (!S_ISBLK(st.st_mode)) | |
303 | return NULL; | |
304 | ||
2c740afd | 305 | if (sd_device_new_from_devnum(&device, 'b', st.st_rdev) < 0) |
1ca208fb | 306 | return NULL; |
b1a2da0a | 307 | |
2c740afd YW |
308 | NULSTR_FOREACH(i, name_fields) |
309 | if (sd_device_get_property_value(device, i, &name) >= 0 && | |
310 | !isempty(name)) | |
1ca208fb | 311 | return strdup(name); |
b1a2da0a | 312 | |
1ca208fb | 313 | return NULL; |
b1a2da0a LP |
314 | } |
315 | ||
b61e476f | 316 | static char *disk_mount_point(const char *label) { |
e7d90b71 | 317 | _cleanup_free_ char *device = NULL; |
5862d652 | 318 | _cleanup_endmntent_ FILE *f = NULL; |
b61e476f LP |
319 | struct mntent *m; |
320 | ||
321 | /* Yeah, we don't support native systemd unit files here for now */ | |
322 | ||
323 | if (asprintf(&device, "/dev/mapper/%s", label) < 0) | |
5862d652 | 324 | return NULL; |
b61e476f | 325 | |
9ffcff0e | 326 | f = setmntent("/etc/fstab", "re"); |
e0295d26 | 327 | if (!f) |
5862d652 | 328 | return NULL; |
b61e476f LP |
329 | |
330 | while ((m = getmntent(f))) | |
5862d652 ZJS |
331 | if (path_equal(m->mnt_fsname, device)) |
332 | return strdup(m->mnt_dir); | |
b61e476f | 333 | |
5862d652 | 334 | return NULL; |
b61e476f LP |
335 | } |
336 | ||
1602b008 | 337 | static int get_password(const char *vol, const char *src, usec_t until, bool accept_cached, char ***ret) { |
ea7e7c1e | 338 | _cleanup_free_ char *description = NULL, *name_buffer = NULL, *mount_point = NULL, *text = NULL, *disk_path = NULL; |
ab84f5b9 | 339 | _cleanup_strv_free_erase_ char **passwords = NULL; |
e51b9486 | 340 | const char *name = NULL; |
e287086b LP |
341 | char **p, *id; |
342 | int r = 0; | |
e7d90b71 | 343 | |
e51b9486 HH |
344 | assert(vol); |
345 | assert(src); | |
1602b008 | 346 | assert(ret); |
e7d90b71 | 347 | |
e51b9486 HH |
348 | description = disk_description(src); |
349 | mount_point = disk_mount_point(vol); | |
350 | ||
ea7e7c1e MS |
351 | disk_path = cescape(src); |
352 | if (!disk_path) | |
353 | return log_oom(); | |
354 | ||
ece174c5 | 355 | if (description && streq(vol, description)) |
e51b9486 HH |
356 | /* If the description string is simply the |
357 | * volume name, then let's not show this | |
358 | * twice */ | |
97b11eed | 359 | description = mfree(description); |
e51b9486 HH |
360 | |
361 | if (mount_point && description) | |
362 | r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point); | |
363 | else if (mount_point) | |
364 | r = asprintf(&name_buffer, "%s on %s", vol, mount_point); | |
365 | else if (description) | |
366 | r = asprintf(&name_buffer, "%s (%s)", description, vol); | |
367 | ||
368 | if (r < 0) | |
369 | return log_oom(); | |
370 | ||
371 | name = name_buffer ? name_buffer : vol; | |
372 | ||
a1c111c2 | 373 | if (asprintf(&text, "Please enter passphrase for disk %s:", name) < 0) |
e7d90b71 JJ |
374 | return log_oom(); |
375 | ||
ea7e7c1e | 376 | id = strjoina("cryptsetup:", disk_path); |
9fa1de96 | 377 | |
ab84f5b9 ZJS |
378 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, |
379 | ASK_PASSWORD_PUSH_CACHE | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED), | |
380 | &passwords); | |
23bbb0de MS |
381 | if (r < 0) |
382 | return log_error_errno(r, "Failed to query password: %m"); | |
e7d90b71 | 383 | |
f75cac37 | 384 | if (arg_verify) { |
ab84f5b9 ZJS |
385 | _cleanup_strv_free_erase_ char **passwords2 = NULL; |
386 | ||
1602b008 | 387 | assert(strv_length(passwords) == 1); |
e7d90b71 | 388 | |
a1c111c2 | 389 | if (asprintf(&text, "Please enter passphrase for disk %s (verification):", name) < 0) |
ab84f5b9 | 390 | return log_oom(); |
e7d90b71 | 391 | |
ea7e7c1e | 392 | id = strjoina("cryptsetup-verification:", disk_path); |
9fa1de96 | 393 | |
e287086b | 394 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2); |
ab84f5b9 ZJS |
395 | if (r < 0) |
396 | return log_error_errno(r, "Failed to query verification password: %m"); | |
e7d90b71 JJ |
397 | |
398 | assert(strv_length(passwords2) == 1); | |
399 | ||
1602b008 | 400 | if (!streq(passwords[0], passwords2[0])) { |
e7d90b71 | 401 | log_warning("Passwords did not match, retrying."); |
ab84f5b9 | 402 | return -EAGAIN; |
e7d90b71 JJ |
403 | } |
404 | } | |
405 | ||
1602b008 | 406 | strv_uniq(passwords); |
e7d90b71 | 407 | |
1602b008 | 408 | STRV_FOREACH(p, passwords) { |
e7d90b71 JJ |
409 | char *c; |
410 | ||
f75cac37 | 411 | if (strlen(*p)+1 >= arg_key_size) |
e7d90b71 JJ |
412 | continue; |
413 | ||
414 | /* Pad password if necessary */ | |
1602b008 | 415 | c = new(char, arg_key_size); |
ab84f5b9 ZJS |
416 | if (!c) |
417 | return log_oom(); | |
e7d90b71 | 418 | |
f75cac37 | 419 | strncpy(c, *p, arg_key_size); |
d135419e | 420 | free_and_replace(*p, c); |
e7d90b71 JJ |
421 | } |
422 | ||
ae2a15bc | 423 | *ret = TAKE_PTR(passwords); |
1602b008 | 424 | |
ab84f5b9 | 425 | return 0; |
e7d90b71 JJ |
426 | } |
427 | ||
1602b008 LP |
428 | static int attach_tcrypt( |
429 | struct crypt_device *cd, | |
430 | const char *name, | |
431 | const char *key_file, | |
432 | char **passwords, | |
433 | uint32_t flags) { | |
434 | ||
8cf3ca80 JJ |
435 | int r = 0; |
436 | _cleanup_free_ char *passphrase = NULL; | |
437 | struct crypt_params_tcrypt params = { | |
438 | .flags = CRYPT_TCRYPT_LEGACY_MODES, | |
f75cac37 LP |
439 | .keyfiles = (const char **)arg_tcrypt_keyfiles, |
440 | .keyfiles_count = strv_length(arg_tcrypt_keyfiles) | |
8cf3ca80 JJ |
441 | }; |
442 | ||
443 | assert(cd); | |
444 | assert(name); | |
f268f57f | 445 | assert(key_file || (passwords && passwords[0])); |
8cf3ca80 | 446 | |
f75cac37 | 447 | if (arg_tcrypt_hidden) |
8cf3ca80 JJ |
448 | params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER; |
449 | ||
f75cac37 | 450 | if (arg_tcrypt_system) |
8cf3ca80 JJ |
451 | params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER; |
452 | ||
52028838 GH |
453 | #ifdef CRYPT_TCRYPT_VERA_MODES |
454 | if (arg_tcrypt_veracrypt) | |
455 | params.flags |= CRYPT_TCRYPT_VERA_MODES; | |
456 | #endif | |
457 | ||
8cf3ca80 JJ |
458 | if (key_file) { |
459 | r = read_one_line_file(key_file, &passphrase); | |
460 | if (r < 0) { | |
da927ba9 | 461 | log_error_errno(r, "Failed to read password file '%s': %m", key_file); |
b7a0fead | 462 | return -EAGAIN; /* log with the actual error, but return EAGAIN */ |
8cf3ca80 JJ |
463 | } |
464 | ||
465 | params.passphrase = passphrase; | |
466 | } else | |
467 | params.passphrase = passwords[0]; | |
468 | params.passphrase_size = strlen(params.passphrase); | |
469 | ||
470 | r = crypt_load(cd, CRYPT_TCRYPT, ¶ms); | |
471 | if (r < 0) { | |
6f177c7d LP |
472 | if (key_file && r == -EPERM) { |
473 | log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file); | |
474 | return -EAGAIN; /* log the actual error, but return EAGAIN */ | |
475 | } | |
476 | ||
477 | return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd)); | |
8cf3ca80 JJ |
478 | } |
479 | ||
6f177c7d LP |
480 | r = crypt_activate_by_volume_key(cd, name, NULL, 0, flags); |
481 | if (r < 0) | |
482 | return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd)); | |
483 | ||
484 | return 0; | |
8cf3ca80 JJ |
485 | } |
486 | ||
10fb4e35 JJ |
487 | static int attach_luks_or_plain(struct crypt_device *cd, |
488 | const char *name, | |
489 | const char *key_file, | |
490 | char **passwords, | |
491 | uint32_t flags) { | |
492 | int r = 0; | |
493 | bool pass_volume_key = false; | |
494 | ||
495 | assert(cd); | |
496 | assert(name); | |
497 | assert(key_file || passwords); | |
498 | ||
2e4beb87 | 499 | if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) { |
4eac2773 MP |
500 | struct crypt_params_plain params = { |
501 | .offset = arg_offset, | |
502 | .skip = arg_skip, | |
a9fc6406 DJL |
503 | #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE |
504 | .sector_size = arg_sector_size, | |
505 | #endif | |
4eac2773 | 506 | }; |
10fb4e35 JJ |
507 | const char *cipher, *cipher_mode; |
508 | _cleanup_free_ char *truncated_cipher = NULL; | |
509 | ||
f75cac37 | 510 | if (arg_hash) { |
10fb4e35 | 511 | /* plain isn't a real hash type. it just means "use no hash" */ |
f75cac37 LP |
512 | if (!streq(arg_hash, "plain")) |
513 | params.hash = arg_hash; | |
8a52210c ZJS |
514 | } else if (!key_file) |
515 | /* for CRYPT_PLAIN, the behaviour of cryptsetup | |
516 | * package is to not hash when a key file is provided */ | |
10fb4e35 JJ |
517 | params.hash = "ripemd160"; |
518 | ||
f75cac37 | 519 | if (arg_cipher) { |
10fb4e35 JJ |
520 | size_t l; |
521 | ||
f75cac37 LP |
522 | l = strcspn(arg_cipher, "-"); |
523 | truncated_cipher = strndup(arg_cipher, l); | |
10fb4e35 JJ |
524 | if (!truncated_cipher) |
525 | return log_oom(); | |
526 | ||
527 | cipher = truncated_cipher; | |
f75cac37 | 528 | cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain"; |
10fb4e35 JJ |
529 | } else { |
530 | cipher = "aes"; | |
531 | cipher_mode = "cbc-essiv:sha256"; | |
532 | } | |
533 | ||
aed68083 | 534 | /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */ |
6131a78b | 535 | arg_keyfile_size = arg_key_size; |
10fb4e35 | 536 | |
aed68083 LP |
537 | /* In contrast to what the name crypt_setup() might suggest this doesn't actually format |
538 | * anything, it just configures encryption parameters when used for plain mode. */ | |
1602b008 | 539 | r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, ¶ms); |
2e4beb87 MB |
540 | if (r < 0) |
541 | return log_error_errno(r, "Loading of cryptographic parameters failed: %m"); | |
10fb4e35 JJ |
542 | |
543 | /* hash == NULL implies the user passed "plain" */ | |
544 | pass_volume_key = (params.hash == NULL); | |
545 | } | |
10fb4e35 JJ |
546 | |
547 | log_info("Set cipher %s, mode %s, key size %i bits for device %s.", | |
548 | crypt_get_cipher(cd), | |
549 | crypt_get_cipher_mode(cd), | |
550 | crypt_get_volume_key_size(cd)*8, | |
551 | crypt_get_device_name(cd)); | |
552 | ||
553 | if (key_file) { | |
1602b008 | 554 | r = crypt_activate_by_keyfile_offset(cd, name, arg_key_slot, key_file, arg_keyfile_size, arg_keyfile_offset, flags); |
6f177c7d LP |
555 | if (r == -EPERM) { |
556 | log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file); | |
557 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
c20db388 RG |
558 | } |
559 | if (r == -EINVAL) { | |
560 | log_error_errno(r, "Failed to activate with key file '%s'. (Key file missing?)", key_file); | |
561 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
10fb4e35 | 562 | } |
6f177c7d LP |
563 | if (r < 0) |
564 | return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file); | |
10fb4e35 JJ |
565 | } else { |
566 | char **p; | |
567 | ||
6f177c7d | 568 | r = -EINVAL; |
10fb4e35 JJ |
569 | STRV_FOREACH(p, passwords) { |
570 | if (pass_volume_key) | |
f75cac37 | 571 | r = crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags); |
10fb4e35 | 572 | else |
f75cac37 | 573 | r = crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags); |
10fb4e35 JJ |
574 | if (r >= 0) |
575 | break; | |
576 | } | |
6f177c7d LP |
577 | if (r == -EPERM) { |
578 | log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)"); | |
579 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
580 | } | |
581 | if (r < 0) | |
582 | return log_error_errno(r, "Failed to activate with specified passphrase: %m"); | |
10fb4e35 JJ |
583 | } |
584 | ||
585 | return r; | |
586 | } | |
587 | ||
dd5e696d | 588 | static int help(void) { |
37ec0fdd LP |
589 | _cleanup_free_ char *link = NULL; |
590 | int r; | |
591 | ||
592 | r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link); | |
593 | if (r < 0) | |
594 | return log_oom(); | |
dd5e696d LP |
595 | |
596 | printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n" | |
597 | "%s detach VOLUME\n\n" | |
37ec0fdd LP |
598 | "Attaches or detaches an encrypted block device.\n" |
599 | "\nSee the %s for details.\n" | |
600 | , program_invocation_short_name | |
601 | , program_invocation_short_name | |
602 | , link | |
603 | ); | |
dd5e696d LP |
604 | |
605 | return 0; | |
606 | } | |
607 | ||
d5d1ae15 LP |
608 | static uint32_t determine_flags(void) { |
609 | uint32_t flags = 0; | |
610 | ||
611 | if (arg_readonly) | |
612 | flags |= CRYPT_ACTIVATE_READONLY; | |
613 | ||
614 | if (arg_discards) | |
615 | flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; | |
616 | ||
617 | if (arg_same_cpu_crypt) | |
618 | flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT; | |
619 | ||
620 | if (arg_submit_from_crypt_cpus) | |
621 | flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS; | |
622 | ||
623 | return flags; | |
624 | } | |
625 | ||
3a40f366 | 626 | static int run(int argc, char *argv[]) { |
294bd454 | 627 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
3a40f366 | 628 | int r; |
e23a0ce8 | 629 | |
3a40f366 YW |
630 | if (argc <= 1) |
631 | return help(); | |
dd5e696d | 632 | |
e23a0ce8 LP |
633 | if (argc < 3) { |
634 | log_error("This program requires at least two arguments."); | |
3a40f366 | 635 | return -EINVAL; |
e23a0ce8 LP |
636 | } |
637 | ||
6bf3c61c | 638 | log_setup_service(); |
e23a0ce8 | 639 | |
4c12626c LP |
640 | umask(0022); |
641 | ||
260ab287 | 642 | if (streq(argv[1], "attach")) { |
7f4e0805 | 643 | uint32_t flags = 0; |
10fb4e35 | 644 | unsigned tries; |
260ab287 | 645 | usec_t until; |
e2d480b9 | 646 | crypt_status_info status; |
e51b9486 | 647 | const char *key_file = NULL; |
7f4e0805 | 648 | |
b61e476f LP |
649 | /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */ |
650 | ||
0ffff81a LP |
651 | if (argc < 4) |
652 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments."); | |
7f4e0805 | 653 | |
1fc76335 LP |
654 | if (argc >= 5 && |
655 | argv[4][0] && | |
656 | !streq(argv[4], "-") && | |
657 | !streq(argv[4], "none")) { | |
7f4e0805 LP |
658 | |
659 | if (!path_is_absolute(argv[4])) | |
44ce4255 | 660 | log_warning("Password file path '%s' is not absolute. Ignoring.", argv[4]); |
7f4e0805 LP |
661 | else |
662 | key_file = argv[4]; | |
663 | } | |
664 | ||
74b1c371 | 665 | if (argc >= 6 && argv[5][0] && !streq(argv[5], "-")) { |
3a40f366 YW |
666 | r = parse_options(argv[5]); |
667 | if (r < 0) | |
668 | return r; | |
74b1c371 | 669 | } |
e23a0ce8 | 670 | |
b853f6e9 LP |
671 | /* A delicious drop of snake oil */ |
672 | mlockall(MCL_FUTURE); | |
673 | ||
7376e835 AC |
674 | if (arg_header) { |
675 | log_debug("LUKS header: %s", arg_header); | |
5f4bfe56 | 676 | r = crypt_init(&cd, arg_header); |
7376e835 | 677 | } else |
5f4bfe56 | 678 | r = crypt_init(&cd, argv[3]); |
3a40f366 YW |
679 | if (r < 0) |
680 | return log_error_errno(r, "crypt_init() failed: %m"); | |
e23a0ce8 | 681 | |
691c2e2e | 682 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); |
7f4e0805 | 683 | |
260ab287 | 684 | status = crypt_status(cd, argv[2]); |
3742095b | 685 | if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { |
260ab287 | 686 | log_info("Volume %s already active.", argv[2]); |
3a40f366 | 687 | return 0; |
7f4e0805 LP |
688 | } |
689 | ||
d5d1ae15 | 690 | flags = determine_flags(); |
2c65512e | 691 | |
0864d311 | 692 | if (arg_timeout == USEC_INFINITY) |
7dcda352 | 693 | until = 0; |
0864d311 AS |
694 | else |
695 | until = now(CLOCK_MONOTONIC) + arg_timeout; | |
260ab287 | 696 | |
6131a78b | 697 | arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8)); |
e2d480b9 | 698 | |
10fb4e35 JJ |
699 | if (key_file) { |
700 | struct stat st; | |
e2d480b9 | 701 | |
10fb4e35 JJ |
702 | /* Ideally we'd do this on the open fd, but since this is just a |
703 | * warning it's OK to do this in two steps. */ | |
3f4d56a0 MP |
704 | if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005)) |
705 | log_warning("Key file %s is world-readable. This is not a good idea!", key_file); | |
e2d480b9 LP |
706 | } |
707 | ||
ea9a9d49 MB |
708 | if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1)) { |
709 | r = crypt_load(cd, CRYPT_LUKS, NULL); | |
710 | if (r < 0) | |
711 | return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd)); | |
712 | ||
713 | if (arg_header) { | |
714 | r = crypt_set_data_device(cd, argv[3]); | |
715 | if (r < 0) | |
716 | return log_error_errno(r, "Failed to set LUKS data device %s: %m", argv[3]); | |
717 | } | |
894bb3ca MB |
718 | #ifdef CRYPT_ANY_TOKEN |
719 | /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */ | |
720 | if (!key_file) { | |
721 | r = crypt_activate_by_token(cd, argv[2], CRYPT_ANY_TOKEN, NULL, flags); | |
722 | if (r >= 0) { | |
723 | log_debug("Volume %s activated with LUKS token id %i.", argv[2], r); | |
724 | return 0; | |
725 | } | |
726 | ||
727 | log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd)); | |
728 | } | |
729 | #endif | |
ea9a9d49 MB |
730 | } |
731 | ||
f75cac37 | 732 | for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) { |
ab84f5b9 | 733 | _cleanup_strv_free_erase_ char **passwords = NULL; |
260ab287 LP |
734 | |
735 | if (!key_file) { | |
5f4bfe56 LP |
736 | r = get_password(argv[2], argv[3], until, tries == 0 && !arg_verify, &passwords); |
737 | if (r == -EAGAIN) | |
e7d90b71 | 738 | continue; |
5f4bfe56 | 739 | if (r < 0) |
3a40f366 | 740 | return r; |
260ab287 LP |
741 | } |
742 | ||
f75cac37 | 743 | if (streq_ptr(arg_type, CRYPT_TCRYPT)) |
5f4bfe56 | 744 | r = attach_tcrypt(cd, argv[2], key_file, passwords, flags); |
8cf3ca80 | 745 | else |
5f4bfe56 | 746 | r = attach_luks_or_plain(cd, |
7376e835 AC |
747 | argv[2], |
748 | key_file, | |
7376e835 AC |
749 | passwords, |
750 | flags); | |
5f4bfe56 | 751 | if (r >= 0) |
260ab287 | 752 | break; |
6f177c7d LP |
753 | if (r != -EAGAIN) |
754 | return r; | |
260ab287 | 755 | |
6f177c7d LP |
756 | /* Passphrase not correct? Let's try again! */ |
757 | key_file = NULL; | |
7f4e0805 LP |
758 | } |
759 | ||
0ffff81a LP |
760 | if (arg_tries != 0 && tries >= arg_tries) |
761 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up."); | |
7f4e0805 LP |
762 | |
763 | } else if (streq(argv[1], "detach")) { | |
7f4e0805 | 764 | |
5f4bfe56 LP |
765 | r = crypt_init_by_name(&cd, argv[2]); |
766 | if (r == -ENODEV) { | |
a0bfc9c2 | 767 | log_info("Volume %s already inactive.", argv[2]); |
3a40f366 | 768 | return 0; |
7f4e0805 | 769 | } |
3a40f366 YW |
770 | if (r < 0) |
771 | return log_error_errno(r, "crypt_init_by_name() failed: %m"); | |
7f4e0805 | 772 | |
691c2e2e | 773 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); |
7f4e0805 | 774 | |
5f4bfe56 | 775 | r = crypt_deactivate(cd, argv[2]); |
3a40f366 YW |
776 | if (r < 0) |
777 | return log_error_errno(r, "Failed to deactivate: %m"); | |
e23a0ce8 | 778 | |
0ffff81a LP |
779 | } else |
780 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", argv[1]); | |
e23a0ce8 | 781 | |
3a40f366 | 782 | return 0; |
e23a0ce8 | 783 | } |
3a40f366 YW |
784 | |
785 | DEFINE_MAIN_FUNCTION(run); |