]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-generator.c
Merge pull request #32914 from yuwata/test-64-storage
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7
8 #include "alloc-util.h"
9 #include "dropin.h"
10 #include "escape.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fstab-util.h"
14 #include "generator.h"
15 #include "hashmap.h"
16 #include "id128-util.h"
17 #include "log.h"
18 #include "mkdir.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "proc-cmdline.h"
22 #include "specifier.h"
23 #include "string-util.h"
24 #include "strv.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 (STR_IN_SET(device_path, "-", "none"))
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_timeouts(arg_dest, device, name, 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->name);
585 free(d->options);
586 return mfree(d);
587 }
588
589 static crypto_device *get_crypto_device(const char *uuid) {
590 int r;
591 crypto_device *d;
592
593 assert(uuid);
594
595 d = hashmap_get(arg_disks, uuid);
596 if (!d) {
597 d = new0(struct crypto_device, 1);
598 if (!d)
599 return NULL;
600
601 d->uuid = strdup(uuid);
602 if (!d->uuid)
603 return mfree(d);
604
605 r = hashmap_put(arg_disks, d->uuid, d);
606 if (r < 0) {
607 free(d->uuid);
608 return mfree(d);
609 }
610 }
611
612 return d;
613 }
614
615 static bool warn_uuid_invalid(const char *uuid, const char *key) {
616 assert(key);
617
618 if (!id128_is_valid(uuid)) {
619 log_warning("Failed to parse %s= kernel command line switch. UUID is invalid, ignoring.", key);
620 return true;
621 }
622
623 return false;
624 }
625
626 static int filter_header_device(const char *options,
627 char **ret_headerdev,
628 char **ret_filtered_headerdev_options) {
629 int r;
630 _cleanup_free_ char *headerfile = NULL, *headerdev = NULL, *headerspec = NULL,
631 *filtered_headerdev = NULL, *filtered_headerspec = NULL;
632
633 assert(ret_headerdev);
634 assert(ret_filtered_headerdev_options);
635
636 r = fstab_filter_options(options, "header\0", NULL, &headerspec, NULL, &filtered_headerspec);
637 if (r < 0)
638 return log_error_errno(r, "Failed to parse header= option value: %m");
639
640 if (r > 0) {
641 r = split_locationspec(headerspec, &headerfile, &headerdev);
642 if (r < 0)
643 return r;
644
645 if (isempty(filtered_headerspec))
646 filtered_headerdev = strjoin("header=", headerfile);
647 else
648 filtered_headerdev = strjoin(filtered_headerspec, ",header=", headerfile);
649
650 if (!filtered_headerdev)
651 return log_oom();
652 } else
653 filtered_headerdev = TAKE_PTR(filtered_headerspec);
654
655 *ret_filtered_headerdev_options = TAKE_PTR(filtered_headerdev);
656 *ret_headerdev = TAKE_PTR(headerdev);
657
658 return 0;
659 }
660
661 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
662 _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
663 crypto_device *d;
664 int r;
665
666 if (streq(key, "luks")) {
667
668 r = value ? parse_boolean(value) : 1;
669 if (r < 0)
670 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
671 else
672 arg_enabled = r;
673
674 } else if (streq(key, "luks.crypttab")) {
675
676 r = value ? parse_boolean(value) : 1;
677 if (r < 0)
678 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
679 else
680 arg_read_crypttab = r;
681
682 } else if (streq(key, "luks.uuid")) {
683
684 if (proc_cmdline_value_missing(key, value))
685 return 0;
686
687 d = get_crypto_device(startswith(value, "luks-") ?: value);
688 if (!d)
689 return log_oom();
690
691 d->create = arg_allow_list = true;
692
693 } else if (streq(key, "luks.options")) {
694 _cleanup_free_ char *headerdev = NULL, *filtered_headerdev_options = NULL;
695
696 if (proc_cmdline_value_missing(key, value))
697 return 0;
698
699 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
700 if (r != 2)
701 return free_and_strdup_warn(&arg_default_options, value);
702
703 if (warn_uuid_invalid(uuid, key))
704 return 0;
705
706 d = get_crypto_device(uuid);
707 if (!d)
708 return log_oom();
709
710 r = filter_header_device(uuid_value, &headerdev, &filtered_headerdev_options);
711 if (r < 0)
712 return r;
713
714 free_and_replace(d->options, filtered_headerdev_options);
715 free_and_replace(d->headerdev, headerdev);
716 } else if (streq(key, "luks.key")) {
717 size_t n;
718 _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
719 const char *keyspec;
720
721 if (proc_cmdline_value_missing(key, value))
722 return 0;
723
724 n = strspn(value, ALPHANUMERICAL "-");
725 if (value[n] != '=')
726 return free_and_strdup_warn(&arg_default_keyfile, value);
727
728 uuid = strndup(value, n);
729 if (!uuid)
730 return log_oom();
731
732 if (warn_uuid_invalid(uuid, key))
733 return 0;
734
735 d = get_crypto_device(uuid);
736 if (!d)
737 return log_oom();
738
739 keyspec = value + n + 1;
740 r = split_locationspec(keyspec, &keyfile, &keydev);
741 if (r < 0)
742 return r;
743
744 free_and_replace(d->keyfile, keyfile);
745 free_and_replace(d->keydev, keydev);
746 } else if (streq(key, "luks.data")) {
747 size_t n;
748 _cleanup_free_ char *datadev = NULL;
749
750 if (proc_cmdline_value_missing(key, value))
751 return 0;
752
753 n = strspn(value, ALPHANUMERICAL "-");
754 if (value[n] != '=') {
755 log_warning("Failed to parse luks.data= kernel command line switch. UUID is invalid, ignoring.");
756 return 0;
757 }
758
759 uuid = strndup(value, n);
760 if (!uuid)
761 return log_oom();
762
763 if (warn_uuid_invalid(uuid, key))
764 return 0;
765
766 d = get_crypto_device(uuid);
767 if (!d)
768 return log_oom();
769
770 datadev = fstab_node_to_udev_node(value + n + 1);
771 if (!datadev)
772 return log_oom();
773
774 free_and_replace(d->datadev, datadev);
775 } else if (streq(key, "luks.name")) {
776
777 if (proc_cmdline_value_missing(key, value))
778 return 0;
779
780 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
781 if (r == 2) {
782 d = get_crypto_device(uuid);
783 if (!d)
784 return log_oom();
785
786 d->create = arg_allow_list = true;
787
788 free_and_replace(d->name, uuid_value);
789 } else
790 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
791 }
792
793 return 0;
794 }
795
796 static int add_crypttab_devices(void) {
797 _cleanup_fclose_ FILE *f = NULL;
798 unsigned crypttab_line = 0;
799 int r;
800
801 if (!arg_read_crypttab)
802 return 0;
803
804 r = fopen_unlocked(arg_crypttab, "re", &f);
805 if (r < 0) {
806 if (errno != ENOENT)
807 log_error_errno(errno, "Failed to open %s: %m", arg_crypttab);
808 return 0;
809 }
810
811 for (;;) {
812 _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL,
813 *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL;
814 crypto_device *d = NULL;
815 char *uuid;
816 int k;
817
818 r = read_stripped_line(f, LONG_LINE_MAX, &line);
819 if (r < 0)
820 return log_error_errno(r, "Failed to read %s: %m", arg_crypttab);
821 if (r == 0)
822 break;
823
824 crypttab_line++;
825
826 if (IN_SET(line[0], 0, '#'))
827 continue;
828
829 k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
830 if (k < 2 || k > 4) {
831 log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line);
832 continue;
833 }
834
835 uuid = startswith(device, "UUID=");
836 if (!uuid)
837 uuid = path_startswith(device, "/dev/disk/by-uuid/");
838 if (!uuid)
839 uuid = startswith(name, "luks-");
840 if (uuid)
841 d = hashmap_get(arg_disks, uuid);
842
843 if (arg_allow_list && !d) {
844 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
845 continue;
846 }
847
848 r = split_locationspec(keyspec, &keyfile, &keydev);
849 if (r < 0)
850 return r;
851
852 if (options && (!d || !d->options)) {
853 r = filter_header_device(options, &headerdev, &filtered_header);
854 if (r < 0)
855 return r;
856 free_and_replace(options, filtered_header);
857 }
858
859 r = create_disk(name,
860 device,
861 keyfile,
862 keydev,
863 (d && d->options) ? d->headerdev : headerdev,
864 (d && d->options) ? d->options : options,
865 arg_crypttab);
866 if (r < 0)
867 return r;
868
869 if (d)
870 d->create = false;
871 }
872
873 return 0;
874 }
875
876 static int add_proc_cmdline_devices(void) {
877 int r;
878 crypto_device *d;
879
880 HASHMAP_FOREACH(d, arg_disks) {
881 _cleanup_free_ char *device = NULL;
882
883 if (!d->create)
884 continue;
885
886 if (!d->name) {
887 d->name = strjoin("luks-", d->uuid);
888 if (!d->name)
889 return log_oom();
890 }
891
892 device = strjoin("UUID=", d->uuid);
893 if (!device)
894 return log_oom();
895
896 r = create_disk(d->name,
897 d->datadev ?: device,
898 d->keyfile ?: arg_default_keyfile,
899 d->keydev,
900 d->headerdev,
901 d->options ?: arg_default_options,
902 "/proc/cmdline");
903 if (r < 0)
904 return r;
905 }
906
907 return 0;
908 }
909
910 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
911 crypto_device, crypt_device_free);
912
913 static int run(const char *dest, const char *dest_early, const char *dest_late) {
914 int r;
915
916 assert_se(arg_dest = dest);
917
918 arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab";
919 arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup";
920
921 arg_disks = hashmap_new(&crypt_device_hash_ops);
922 if (!arg_disks)
923 return log_oom();
924
925 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
926 if (r < 0)
927 return log_warning_errno(r, "Failed to parse kernel command line: %m");
928
929 if (!arg_enabled)
930 return 0;
931
932 r = add_crypttab_devices();
933 if (r < 0)
934 return r;
935
936 r = add_proc_cmdline_devices();
937 if (r < 0)
938 return r;
939
940 return 0;
941 }
942
943 DEFINE_MAIN_GENERATOR_FUNCTION(run);