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