]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdlib.h> | |
4 | #include <sys/stat.h> | |
5 | ||
6 | #include "alloc-util.h" | |
7 | #include "cryptsetup-util.h" | |
8 | #include "dropin.h" | |
9 | #include "escape.h" | |
10 | #include "fd-util.h" | |
11 | #include "fileio.h" | |
12 | #include "fstab-util.h" | |
13 | #include "generator.h" | |
14 | #include "hashmap.h" | |
15 | #include "id128-util.h" | |
16 | #include "log.h" | |
17 | #include "mkdir.h" | |
18 | #include "parse-util.h" | |
19 | #include "path-util.h" | |
20 | #include "proc-cmdline.h" | |
21 | #include "specifier.h" | |
22 | #include "string-util.h" | |
23 | #include "strv.h" | |
24 | #include "time-util.h" | |
25 | #include "unit-name.h" | |
26 | ||
27 | typedef struct crypto_device { | |
28 | char *uuid; | |
29 | char *keyfile; | |
30 | char *keydev; | |
31 | char *headerdev; | |
32 | char *datadev; | |
33 | char *name; | |
34 | char *options; | |
35 | bool create; | |
36 | } crypto_device; | |
37 | ||
38 | static const char *arg_dest = NULL; | |
39 | static bool arg_enabled = true; | |
40 | static bool arg_read_crypttab = true; | |
41 | static const char *arg_crypttab = NULL; | |
42 | static const char *arg_runtime_directory = NULL; | |
43 | static bool arg_allow_list = false; | |
44 | static Hashmap *arg_disks = NULL; | |
45 | static char *arg_default_options = NULL; | |
46 | static char *arg_default_keyfile = NULL; | |
47 | ||
48 | STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep); | |
49 | STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep); | |
50 | STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep); | |
51 | ||
52 | static int split_locationspec(const char *locationspec, char **ret_file, char **ret_device) { | |
53 | _cleanup_free_ char *file = NULL, *device = NULL; | |
54 | const char *c; | |
55 | ||
56 | assert(ret_file); | |
57 | assert(ret_device); | |
58 | ||
59 | if (!locationspec) { | |
60 | *ret_file = *ret_device = NULL; | |
61 | return 0; | |
62 | } | |
63 | ||
64 | c = strrchr(locationspec, ':'); | |
65 | if (c) { | |
66 | /* The device part has to be either an absolute path to device node (/dev/something, | |
67 | * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device | |
68 | * specification starting with LABEL= or similar. The file part has the same syntax. | |
69 | * | |
70 | * Let's try to guess if the second part looks like a device specification, or just part of a | |
71 | * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to | |
72 | * an absolute path. If we didn't get an absolute path, assume that it is just part of the | |
73 | * first file argument. */ | |
74 | ||
75 | device = fstab_node_to_udev_node(c + 1); | |
76 | if (!device) | |
77 | return log_oom(); | |
78 | ||
79 | if (path_is_absolute(device)) | |
80 | file = strndup(locationspec, c-locationspec); | |
81 | else { | |
82 | log_debug("Location specification argument contains a colon, but \"%s\" doesn't look like a device specification.\n" | |
83 | "Assuming that \"%s\" is a single device specification.", | |
84 | c + 1, locationspec); | |
85 | device = mfree(device); | |
86 | c = NULL; | |
87 | } | |
88 | } | |
89 | ||
90 | if (!c) | |
91 | /* No device specified */ | |
92 | file = strdup(locationspec); | |
93 | ||
94 | if (!file) | |
95 | return log_oom(); | |
96 | ||
97 | *ret_file = TAKE_PTR(file); | |
98 | *ret_device = TAKE_PTR(device); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | static int generate_device_mount( | |
104 | const char *name, | |
105 | const char *device, | |
106 | const char *type_prefix, /* "keydev" or "headerdev" */ | |
107 | const char *device_timeout, | |
108 | bool canfail, | |
109 | bool readonly, | |
110 | char **unit, | |
111 | char **mount) { | |
112 | ||
113 | _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL; | |
114 | _cleanup_fclose_ FILE *f = NULL; | |
115 | int r; | |
116 | usec_t timeout_us; | |
117 | ||
118 | assert(name); | |
119 | assert(device); | |
120 | assert(unit); | |
121 | assert(mount); | |
122 | ||
123 | r = mkdir_parents(arg_runtime_directory, 0755); | |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
127 | r = mkdir(arg_runtime_directory, 0700); | |
128 | if (r < 0 && errno != EEXIST) | |
129 | return -errno; | |
130 | ||
131 | name_escaped = cescape(name); | |
132 | if (!name_escaped) | |
133 | return -ENOMEM; | |
134 | ||
135 | where = strjoin(arg_runtime_directory, "/", type_prefix, "-", name_escaped); | |
136 | if (!where) | |
137 | return -ENOMEM; | |
138 | ||
139 | r = mkdir(where, 0700); | |
140 | if (r < 0 && errno != EEXIST) | |
141 | return -errno; | |
142 | ||
143 | r = unit_name_from_path(where, ".mount", &u); | |
144 | if (r < 0) | |
145 | return r; | |
146 | ||
147 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
148 | if (r < 0) | |
149 | return r; | |
150 | ||
151 | fprintf(f, | |
152 | "[Unit]\n" | |
153 | "DefaultDependencies=no\n\n" | |
154 | "[Mount]\n" | |
155 | "What=%s\n" | |
156 | "Where=%s\n" | |
157 | "Options=%s%s\n", device, where, readonly ? "ro" : "rw", canfail ? ",nofail" : ""); | |
158 | ||
159 | if (device_timeout) { | |
160 | r = parse_sec_fix_0(device_timeout, &timeout_us); | |
161 | if (r >= 0) { | |
162 | r = unit_name_from_path(device, ".device", &device_unit); | |
163 | if (r < 0) | |
164 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
165 | ||
166 | r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout", | |
167 | "# Automatically generated by systemd-cryptsetup-generator \n\n" | |
168 | "[Unit]\nJobRunningTimeoutSec=%s", device_timeout); | |
169 | if (r < 0) | |
170 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
171 | ||
172 | } else | |
173 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", device_timeout); | |
174 | ||
175 | } | |
176 | ||
177 | r = fflush_and_check(f); | |
178 | if (r < 0) | |
179 | return r; | |
180 | ||
181 | *unit = TAKE_PTR(u); | |
182 | *mount = TAKE_PTR(where); | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | static int generate_device_umount(const char *name, | |
188 | const char *device_mount, | |
189 | const char *type_prefix, /* "keydev" or "headerdev" */ | |
190 | char **ret_umount_unit) { | |
191 | _cleanup_fclose_ FILE *f = NULL; | |
192 | _cleanup_free_ char *u = NULL, *name_escaped = NULL, *mount = NULL; | |
193 | int r; | |
194 | ||
195 | assert(name); | |
196 | assert(ret_umount_unit); | |
197 | ||
198 | name_escaped = cescape(name); | |
199 | if (!name_escaped) | |
200 | return -ENOMEM; | |
201 | ||
202 | u = strjoin(type_prefix, "-", name_escaped, "-umount.service"); | |
203 | if (!u) | |
204 | return -ENOMEM; | |
205 | ||
206 | r = unit_name_from_path(device_mount, ".mount", &mount); | |
207 | if (r < 0) | |
208 | return r; | |
209 | ||
210 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
211 | if (r < 0) | |
212 | return r; | |
213 | ||
214 | fprintf(f, | |
215 | "[Unit]\n" | |
216 | "DefaultDependencies=no\n" | |
217 | "After=%s\n\n" | |
218 | "[Service]\n" | |
219 | "ExecStart=-" UMOUNT_PATH " %s\n\n", mount, device_mount); | |
220 | ||
221 | r = fflush_and_check(f); | |
222 | if (r < 0) | |
223 | return r; | |
224 | ||
225 | *ret_umount_unit = TAKE_PTR(u); | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int print_dependencies(FILE *f, const char* device_path, const char* timeout_value, bool canfail) { | |
230 | int r; | |
231 | ||
232 | assert(f); | |
233 | assert(device_path); | |
234 | ||
235 | if (!mangle_none(device_path)) | |
236 | /* None, nothing to do */ | |
237 | return 0; | |
238 | ||
239 | if (PATH_IN_SET(device_path, | |
240 | "/dev/urandom", | |
241 | "/dev/random", | |
242 | "/dev/hw_random", | |
243 | "/dev/hwrng")) { | |
244 | /* RNG device, add random dep */ | |
245 | fputs("After=systemd-random-seed.service\n", f); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path); | |
250 | if (!udev_node) | |
251 | return log_oom(); | |
252 | ||
253 | if (path_equal(udev_node, "/dev/null")) | |
254 | return 0; | |
255 | ||
256 | if (path_startswith(udev_node, "/dev/")) { | |
257 | /* We are dealing with a block device, add dependency for corresponding unit */ | |
258 | _cleanup_free_ char *unit = NULL; | |
259 | ||
260 | r = unit_name_from_path(udev_node, ".device", &unit); | |
261 | if (r < 0) | |
262 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
263 | ||
264 | fprintf(f, "After=%1$s\n", unit); | |
265 | if (canfail) { | |
266 | fprintf(f, "Wants=%1$s\n", unit); | |
267 | if (timeout_value) { | |
268 | r = write_drop_in_format(arg_dest, unit, 90, "device-timeout", | |
269 | "# Automatically generated by systemd-cryptsetup-generator \n\n" | |
270 | "[Unit]\nJobRunningTimeoutSec=%s", timeout_value); | |
271 | if (r < 0) | |
272 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
273 | } | |
274 | } else | |
275 | fprintf(f, "Requires=%1$s\n", unit); | |
276 | } else { | |
277 | /* Regular file, add mount dependency */ | |
278 | _cleanup_free_ char *escaped_path = specifier_escape(device_path); | |
279 | if (!escaped_path) | |
280 | return log_oom(); | |
281 | ||
282 | fprintf(f, "%s=%s\n", canfail ? "WantsMountsFor" : "RequiresMountsFor", escaped_path); | |
283 | } | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | static bool attach_in_initrd(const char *name, const char *options) { | |
289 | assert(name); | |
290 | ||
291 | /* Imply x-initrd.attach in case the volume name is among those defined in the Discoverable Partition | |
292 | * Specification for partitions that we require to be mounted during the initrd → host transition, | |
293 | * i.e. for the root fs itself, and /usr/. This mirrors similar behaviour in | |
294 | * systemd-fstab-generator. */ | |
295 | ||
296 | return fstab_test_option(options, "x-initrd.attach\0") || | |
297 | STR_IN_SET(name, "root", "usr"); | |
298 | } | |
299 | ||
300 | static int create_disk( | |
301 | const char *name, | |
302 | const char *device, | |
303 | const char *key_file, | |
304 | const char *keydev, | |
305 | const char *headerdev, | |
306 | const char *options, | |
307 | const char *source) { | |
308 | ||
309 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, | |
310 | *keydev_mount = NULL, *keyfile_timeout_value = NULL, | |
311 | *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *key_file_buffer = NULL, | |
312 | *tmp_fstype = NULL, *filtered_header = NULL, *headerdev_mount = NULL; | |
313 | _cleanup_fclose_ FILE *f = NULL; | |
314 | const char *dmname; | |
315 | bool noauto, nofail, swap, netdev; | |
316 | int r, detached_header, keyfile_can_timeout, tmp; | |
317 | ||
318 | assert(name); | |
319 | assert(device); | |
320 | ||
321 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); | |
322 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
323 | swap = fstab_test_option(options, "swap\0"); | |
324 | netdev = fstab_test_option(options, "_netdev\0"); | |
325 | ||
326 | keyfile_can_timeout = fstab_filter_options(options, | |
327 | "keyfile-timeout\0", | |
328 | NULL, &keyfile_timeout_value, NULL, NULL); | |
329 | if (keyfile_can_timeout < 0) | |
330 | return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m"); | |
331 | ||
332 | detached_header = fstab_filter_options( | |
333 | options, | |
334 | "header\0", | |
335 | NULL, | |
336 | &header_path, | |
337 | NULL, | |
338 | headerdev ? &filtered_header : NULL); | |
339 | if (detached_header < 0) | |
340 | return log_error_errno(detached_header, "Failed to parse header= option value: %m"); | |
341 | ||
342 | tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL, NULL); | |
343 | if (tmp < 0) | |
344 | return log_error_errno(tmp, "Failed to parse tmp= option value: %m"); | |
345 | ||
346 | if (tmp && swap) | |
347 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
348 | "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", | |
349 | name); | |
350 | ||
351 | name_escaped = specifier_escape(name); | |
352 | if (!name_escaped) | |
353 | return log_oom(); | |
354 | ||
355 | e = unit_name_escape(name); | |
356 | if (!e) | |
357 | return log_oom(); | |
358 | ||
359 | u = fstab_node_to_udev_node(device); | |
360 | if (!u) | |
361 | return log_oom(); | |
362 | ||
363 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); | |
364 | if (r < 0) | |
365 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
366 | ||
367 | u_escaped = specifier_escape(u); | |
368 | if (!u_escaped) | |
369 | return log_oom(); | |
370 | ||
371 | r = unit_name_from_path(u, ".device", &d); | |
372 | if (r < 0) | |
373 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
374 | ||
375 | if (keydev && !key_file) | |
376 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
377 | "Key device is specified, but path to the key file is missing."); | |
378 | ||
379 | r = generator_open_unit_file(arg_dest, NULL, n, &f); | |
380 | if (r < 0) | |
381 | return r; | |
382 | ||
383 | r = generator_write_cryptsetup_unit_section(f, source); | |
384 | if (r < 0) | |
385 | return r; | |
386 | ||
387 | if (netdev) | |
388 | fprintf(f, "After=remote-fs-pre.target\n"); | |
389 | ||
390 | /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */ | |
391 | if (!attach_in_initrd(name, options)) | |
392 | fprintf(f, | |
393 | "Conflicts=umount.target\n" | |
394 | "Before=umount.target\n"); | |
395 | ||
396 | if (keydev) { | |
397 | _cleanup_free_ char *unit = NULL, *umount_unit = NULL; | |
398 | ||
399 | r = generate_device_mount( | |
400 | name, | |
401 | keydev, | |
402 | "keydev", | |
403 | keyfile_timeout_value, | |
404 | /* canfail = */ keyfile_can_timeout > 0, | |
405 | /* readonly= */ true, | |
406 | &unit, | |
407 | &keydev_mount); | |
408 | if (r < 0) | |
409 | return log_error_errno(r, "Failed to generate keydev mount unit: %m"); | |
410 | ||
411 | r = generate_device_umount(name, keydev_mount, "keydev", &umount_unit); | |
412 | if (r < 0) | |
413 | return log_error_errno(r, "Failed to generate keydev umount unit: %m"); | |
414 | ||
415 | key_file_buffer = path_join(keydev_mount, key_file); | |
416 | if (!key_file_buffer) | |
417 | return log_oom(); | |
418 | ||
419 | key_file = key_file_buffer; | |
420 | ||
421 | fprintf(f, "After=%s\n", unit); | |
422 | if (keyfile_can_timeout > 0) | |
423 | fprintf(f, "Wants=%s\n", unit); | |
424 | else | |
425 | fprintf(f, "Requires=%s\n", unit); | |
426 | ||
427 | if (umount_unit) | |
428 | fprintf(f, | |
429 | "Wants=%s\n" | |
430 | "Before=%s\n", | |
431 | umount_unit, | |
432 | umount_unit | |
433 | ); | |
434 | } | |
435 | ||
436 | if (headerdev) { | |
437 | _cleanup_free_ char *unit = NULL, *umount_unit = NULL, *p = NULL; | |
438 | ||
439 | r = generate_device_mount( | |
440 | name, | |
441 | headerdev, | |
442 | "headerdev", | |
443 | NULL, | |
444 | /* canfail= */ false, /* header is always necessary */ | |
445 | /* readonly= */ false, /* LUKS2 recovery requires rw header access */ | |
446 | &unit, | |
447 | &headerdev_mount); | |
448 | if (r < 0) | |
449 | return log_error_errno(r, "Failed to generate header device mount unit: %m"); | |
450 | ||
451 | r = generate_device_umount(name, headerdev_mount, "headerdev", &umount_unit); | |
452 | if (r < 0) | |
453 | return log_error_errno(r, "Failed to generate header device umount unit: %m"); | |
454 | ||
455 | p = path_join(headerdev_mount, header_path); | |
456 | if (!p) | |
457 | return log_oom(); | |
458 | ||
459 | free_and_replace(header_path, p); | |
460 | ||
461 | if (isempty(filtered_header)) | |
462 | p = strjoin("header=", header_path); | |
463 | else | |
464 | p = strjoin(filtered_header, ",header=", header_path); | |
465 | ||
466 | if (!p) | |
467 | return log_oom(); | |
468 | ||
469 | free_and_replace(filtered_header, p); | |
470 | options = filtered_header; | |
471 | ||
472 | fprintf(f, "After=%s\n" | |
473 | "Requires=%s\n", unit, unit); | |
474 | ||
475 | if (umount_unit) | |
476 | fprintf(f, | |
477 | "Wants=%s\n" | |
478 | "Before=%s\n", | |
479 | umount_unit, | |
480 | umount_unit | |
481 | ); | |
482 | } | |
483 | ||
484 | if (!nofail) | |
485 | fprintf(f, | |
486 | "Before=%s\n", | |
487 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
488 | ||
489 | if (key_file && !keydev) { | |
490 | r = print_dependencies(f, key_file, | |
491 | keyfile_timeout_value, | |
492 | /* canfail= */ keyfile_can_timeout > 0 || nofail); | |
493 | if (r < 0) | |
494 | return r; | |
495 | } | |
496 | ||
497 | /* Check if a header option was specified */ | |
498 | if (detached_header > 0 && !headerdev) { | |
499 | r = print_dependencies(f, header_path, | |
500 | /* timeout_value= */ NULL, | |
501 | /* canfail= */ nofail); | |
502 | if (r < 0) | |
503 | return r; | |
504 | } | |
505 | ||
506 | if (path_startswith(u, "/dev/")) | |
507 | fprintf(f, | |
508 | "BindsTo=%s\n" | |
509 | "After=%s\n", | |
510 | d, d); | |
511 | else | |
512 | /* For loopback devices make sure to explicitly load loop.ko, as this code might run very | |
513 | * early where device nodes created via systemd-tmpfiles-setup-dev.service might not be | |
514 | * around yet. Hence let's sync on the module itself. */ | |
515 | fprintf(f, | |
516 | "RequiresMountsFor=%s\n" | |
517 | "Wants=modprobe@loop.service\n" | |
518 | "After=modprobe@loop.service\n", | |
519 | u_escaped); | |
520 | ||
521 | r = generator_write_device_timeout(arg_dest, device, options, &filtered); | |
522 | if (r < 0) | |
523 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); | |
524 | ||
525 | r = generator_write_cryptsetup_service_section(f, name, u, key_file, filtered); | |
526 | if (r < 0) | |
527 | return r; | |
528 | ||
529 | if (tmp) { | |
530 | _cleanup_free_ char *tmp_fstype_escaped = NULL; | |
531 | ||
532 | if (tmp_fstype) { | |
533 | tmp_fstype_escaped = specifier_escape(tmp_fstype); | |
534 | if (!tmp_fstype_escaped) | |
535 | return log_oom(); | |
536 | } | |
537 | ||
538 | fprintf(f, | |
539 | "ExecStartPost=" LIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n", | |
540 | tmp_fstype_escaped ?: "ext4", name_escaped); | |
541 | } | |
542 | ||
543 | if (swap) | |
544 | fprintf(f, | |
545 | "ExecStartPost=" LIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n", | |
546 | name_escaped); | |
547 | ||
548 | r = fflush_and_check(f); | |
549 | if (r < 0) | |
550 | return log_error_errno(r, "Failed to write unit file %s: %m", n); | |
551 | ||
552 | if (!noauto) { | |
553 | r = generator_add_symlink(arg_dest, | |
554 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
555 | nofail ? "wants" : "requires", n); | |
556 | if (r < 0) | |
557 | return r; | |
558 | } | |
559 | ||
560 | dmname = strjoina("dev-mapper-", e, ".device"); | |
561 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
562 | if (r < 0) | |
563 | return r; | |
564 | ||
565 | if (!noauto && !nofail) { | |
566 | r = write_drop_in(arg_dest, dmname, 40, "device-timeout", | |
567 | "# Automatically generated by systemd-cryptsetup-generator\n\n" | |
568 | "[Unit]\n" | |
569 | "JobTimeoutSec=infinity\n"); | |
570 | if (r < 0) | |
571 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); | |
572 | } | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | static crypto_device* crypt_device_free(crypto_device *d) { | |
578 | if (!d) | |
579 | return NULL; | |
580 | ||
581 | free(d->uuid); | |
582 | free(d->keyfile); | |
583 | free(d->keydev); | |
584 | free(d->headerdev); | |
585 | free(d->datadev); | |
586 | free(d->name); | |
587 | free(d->options); | |
588 | return mfree(d); | |
589 | } | |
590 | ||
591 | static crypto_device *get_crypto_device(const char *uuid) { | |
592 | int r; | |
593 | crypto_device *d; | |
594 | ||
595 | assert(uuid); | |
596 | ||
597 | d = hashmap_get(arg_disks, uuid); | |
598 | if (!d) { | |
599 | d = new0(struct crypto_device, 1); | |
600 | if (!d) | |
601 | return NULL; | |
602 | ||
603 | d->uuid = strdup(uuid); | |
604 | if (!d->uuid) | |
605 | return mfree(d); | |
606 | ||
607 | r = hashmap_put(arg_disks, d->uuid, d); | |
608 | if (r < 0) { | |
609 | free(d->uuid); | |
610 | return mfree(d); | |
611 | } | |
612 | } | |
613 | ||
614 | return d; | |
615 | } | |
616 | ||
617 | static bool warn_uuid_invalid(const char *uuid, const char *key) { | |
618 | assert(key); | |
619 | ||
620 | if (!id128_is_valid(uuid)) { | |
621 | log_warning("Failed to parse %s= kernel command line switch. UUID is invalid, ignoring.", key); | |
622 | return true; | |
623 | } | |
624 | ||
625 | return false; | |
626 | } | |
627 | ||
628 | static int filter_header_device(const char *options, | |
629 | char **ret_headerdev, | |
630 | char **ret_filtered_headerdev_options) { | |
631 | int r; | |
632 | _cleanup_free_ char *headerfile = NULL, *headerdev = NULL, *headerspec = NULL, | |
633 | *filtered_headerdev = NULL, *filtered_headerspec = NULL; | |
634 | ||
635 | assert(ret_headerdev); | |
636 | assert(ret_filtered_headerdev_options); | |
637 | ||
638 | r = fstab_filter_options(options, "header\0", NULL, &headerspec, NULL, &filtered_headerspec); | |
639 | if (r < 0) | |
640 | return log_error_errno(r, "Failed to parse header= option value: %m"); | |
641 | ||
642 | if (r > 0) { | |
643 | r = split_locationspec(headerspec, &headerfile, &headerdev); | |
644 | if (r < 0) | |
645 | return r; | |
646 | ||
647 | if (isempty(filtered_headerspec)) | |
648 | filtered_headerdev = strjoin("header=", headerfile); | |
649 | else | |
650 | filtered_headerdev = strjoin(filtered_headerspec, ",header=", headerfile); | |
651 | ||
652 | if (!filtered_headerdev) | |
653 | return log_oom(); | |
654 | } else | |
655 | filtered_headerdev = TAKE_PTR(filtered_headerspec); | |
656 | ||
657 | *ret_filtered_headerdev_options = TAKE_PTR(filtered_headerdev); | |
658 | *ret_headerdev = TAKE_PTR(headerdev); | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
663 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { | |
664 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; | |
665 | crypto_device *d; | |
666 | int r; | |
667 | ||
668 | if (streq(key, "luks")) { | |
669 | ||
670 | r = value ? parse_boolean(value) : 1; | |
671 | if (r < 0) | |
672 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); | |
673 | else | |
674 | arg_enabled = r; | |
675 | ||
676 | } else if (streq(key, "luks.crypttab")) { | |
677 | ||
678 | r = value ? parse_boolean(value) : 1; | |
679 | if (r < 0) | |
680 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); | |
681 | else | |
682 | arg_read_crypttab = r; | |
683 | ||
684 | } else if (streq(key, "luks.uuid")) { | |
685 | ||
686 | if (proc_cmdline_value_missing(key, value)) | |
687 | return 0; | |
688 | ||
689 | d = get_crypto_device(startswith(value, "luks-") ?: value); | |
690 | if (!d) | |
691 | return log_oom(); | |
692 | ||
693 | d->create = arg_allow_list = true; | |
694 | ||
695 | } else if (streq(key, "luks.options")) { | |
696 | _cleanup_free_ char *headerdev = NULL, *filtered_headerdev_options = NULL; | |
697 | ||
698 | if (proc_cmdline_value_missing(key, value)) | |
699 | return 0; | |
700 | ||
701 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
702 | if (r != 2) | |
703 | return free_and_strdup_warn(&arg_default_options, value); | |
704 | ||
705 | if (warn_uuid_invalid(uuid, key)) | |
706 | return 0; | |
707 | ||
708 | d = get_crypto_device(uuid); | |
709 | if (!d) | |
710 | return log_oom(); | |
711 | ||
712 | r = filter_header_device(uuid_value, &headerdev, &filtered_headerdev_options); | |
713 | if (r < 0) | |
714 | return r; | |
715 | ||
716 | free_and_replace(d->options, filtered_headerdev_options); | |
717 | free_and_replace(d->headerdev, headerdev); | |
718 | } else if (streq(key, "luks.key")) { | |
719 | size_t n; | |
720 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; | |
721 | const char *keyspec; | |
722 | ||
723 | if (proc_cmdline_value_missing(key, value)) | |
724 | return 0; | |
725 | ||
726 | n = strspn(value, ALPHANUMERICAL "-"); | |
727 | if (value[n] != '=') | |
728 | return free_and_strdup_warn(&arg_default_keyfile, value); | |
729 | ||
730 | uuid = strndup(value, n); | |
731 | if (!uuid) | |
732 | return log_oom(); | |
733 | ||
734 | if (warn_uuid_invalid(uuid, key)) | |
735 | return 0; | |
736 | ||
737 | d = get_crypto_device(uuid); | |
738 | if (!d) | |
739 | return log_oom(); | |
740 | ||
741 | keyspec = value + n + 1; | |
742 | r = split_locationspec(keyspec, &keyfile, &keydev); | |
743 | if (r < 0) | |
744 | return r; | |
745 | ||
746 | free_and_replace(d->keyfile, keyfile); | |
747 | free_and_replace(d->keydev, keydev); | |
748 | } else if (streq(key, "luks.data")) { | |
749 | size_t n; | |
750 | _cleanup_free_ char *datadev = NULL; | |
751 | ||
752 | if (proc_cmdline_value_missing(key, value)) | |
753 | return 0; | |
754 | ||
755 | n = strspn(value, ALPHANUMERICAL "-"); | |
756 | if (value[n] != '=') { | |
757 | log_warning("Failed to parse luks.data= kernel command line switch. UUID is invalid, ignoring."); | |
758 | return 0; | |
759 | } | |
760 | ||
761 | uuid = strndup(value, n); | |
762 | if (!uuid) | |
763 | return log_oom(); | |
764 | ||
765 | if (warn_uuid_invalid(uuid, key)) | |
766 | return 0; | |
767 | ||
768 | d = get_crypto_device(uuid); | |
769 | if (!d) | |
770 | return log_oom(); | |
771 | ||
772 | datadev = fstab_node_to_udev_node(value + n + 1); | |
773 | if (!datadev) | |
774 | return log_oom(); | |
775 | ||
776 | free_and_replace(d->datadev, datadev); | |
777 | } else if (streq(key, "luks.name")) { | |
778 | ||
779 | if (proc_cmdline_value_missing(key, value)) | |
780 | return 0; | |
781 | ||
782 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
783 | if (r == 2) { | |
784 | d = get_crypto_device(uuid); | |
785 | if (!d) | |
786 | return log_oom(); | |
787 | ||
788 | d->create = arg_allow_list = true; | |
789 | ||
790 | free_and_replace(d->name, uuid_value); | |
791 | } else | |
792 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
793 | } | |
794 | ||
795 | return 0; | |
796 | } | |
797 | ||
798 | static int add_crypttab_device(const char *name, const char *device, const char *keyspec, const char *options) { | |
799 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL; | |
800 | crypto_device *d = NULL; | |
801 | char *uuid; | |
802 | int r; | |
803 | ||
804 | uuid = startswith(device, "UUID="); | |
805 | if (!uuid) | |
806 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
807 | if (!uuid) | |
808 | uuid = startswith(name, "luks-"); | |
809 | if (uuid) | |
810 | d = hashmap_get(arg_disks, uuid); | |
811 | ||
812 | if (arg_allow_list && !d) { | |
813 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
814 | return 0; | |
815 | } | |
816 | ||
817 | r = split_locationspec(keyspec, &keyfile, &keydev); | |
818 | if (r < 0) | |
819 | return r; | |
820 | ||
821 | if (options && (!d || !d->options)) { | |
822 | r = filter_header_device(options, &headerdev, &filtered_header); | |
823 | if (r < 0) | |
824 | return r; | |
825 | options = filtered_header; | |
826 | } | |
827 | ||
828 | r = create_disk(name, | |
829 | device, | |
830 | keyfile, | |
831 | keydev, | |
832 | (d && d->options) ? d->headerdev : headerdev, | |
833 | (d && d->options) ? d->options : options, | |
834 | arg_crypttab); | |
835 | if (r < 0) | |
836 | return r; | |
837 | ||
838 | if (d) | |
839 | d->create = false; | |
840 | ||
841 | return 0; | |
842 | } | |
843 | ||
844 | static int add_crypttab_devices(void) { | |
845 | _cleanup_fclose_ FILE *f = NULL; | |
846 | unsigned crypttab_line = 0; | |
847 | int r, ret = 0; | |
848 | ||
849 | if (!arg_read_crypttab) | |
850 | return 0; | |
851 | ||
852 | r = fopen_unlocked(arg_crypttab, "re", &f); | |
853 | if (r < 0) { | |
854 | if (errno != ENOENT) | |
855 | log_error_errno(errno, "Failed to open %s: %m", arg_crypttab); | |
856 | return 0; | |
857 | } | |
858 | ||
859 | for (;;) { | |
860 | _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL; | |
861 | int k; | |
862 | ||
863 | r = read_stripped_line(f, LONG_LINE_MAX, &line); | |
864 | if (r < 0) | |
865 | return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); | |
866 | if (r == 0) | |
867 | break; | |
868 | ||
869 | crypttab_line++; | |
870 | ||
871 | if (IN_SET(line[0], 0, '#')) | |
872 | continue; | |
873 | ||
874 | k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); | |
875 | if (k < 2 || k > 4) { | |
876 | log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); | |
877 | continue; | |
878 | } | |
879 | ||
880 | RET_GATHER(ret, add_crypttab_device(name, device, keyspec, options)); | |
881 | } | |
882 | ||
883 | return ret; | |
884 | } | |
885 | ||
886 | static int add_proc_cmdline_devices(void) { | |
887 | int r, ret = 0; | |
888 | crypto_device *d; | |
889 | ||
890 | HASHMAP_FOREACH(d, arg_disks) { | |
891 | _cleanup_free_ char *device = NULL; | |
892 | ||
893 | if (!d->create) | |
894 | continue; | |
895 | ||
896 | if (!d->name) { | |
897 | d->name = strjoin("luks-", d->uuid); | |
898 | if (!d->name) | |
899 | return log_oom(); | |
900 | } | |
901 | ||
902 | device = strjoin("UUID=", d->uuid); | |
903 | if (!device) | |
904 | return log_oom(); | |
905 | ||
906 | r = create_disk(d->name, | |
907 | d->datadev ?: device, | |
908 | d->keyfile ?: arg_default_keyfile, | |
909 | d->keydev, | |
910 | d->headerdev, | |
911 | d->options ?: arg_default_options, | |
912 | "/proc/cmdline"); | |
913 | RET_GATHER(ret, r); | |
914 | } | |
915 | ||
916 | return ret; | |
917 | } | |
918 | ||
919 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, | |
920 | crypto_device, crypt_device_free); | |
921 | ||
922 | static int run(const char *dest, const char *dest_early, const char *dest_late) { | |
923 | int r; | |
924 | ||
925 | assert_se(arg_dest = dest); | |
926 | ||
927 | arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab"; | |
928 | arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup"; | |
929 | ||
930 | arg_disks = hashmap_new(&crypt_device_hash_ops); | |
931 | if (!arg_disks) | |
932 | return log_oom(); | |
933 | ||
934 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); | |
935 | if (r < 0) | |
936 | return log_warning_errno(r, "Failed to parse kernel command line: %m"); | |
937 | ||
938 | if (!arg_enabled) | |
939 | return 0; | |
940 | ||
941 | r = add_crypttab_devices(); | |
942 | RET_GATHER(r, add_proc_cmdline_devices()); | |
943 | ||
944 | return r; | |
945 | } | |
946 | ||
947 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |