]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cryptsetup/cryptsetup-generator.c
cryptsetup-generator: rename split_keyspec to split_locationspec
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e23a0ce8 2
e23a0ce8 3#include <errno.h>
ca78ad1d 4#include <fcntl.h>
ca78ad1d
ZJS
5#include <sys/stat.h>
6#include <sys/types.h>
e23a0ce8 7
b5efdb8a 8#include "alloc-util.h"
0fa9e53d 9#include "dropin.h"
7949dfa7 10#include "escape.h"
3ffd4af2 11#include "fd-util.h"
0d39fa9c 12#include "fileio.h"
07630cea 13#include "fstab-util.h"
0fa9e53d
JJ
14#include "generator.h"
15#include "hashmap.h"
7949dfa7 16#include "id128-util.h"
e23a0ce8 17#include "log.h"
49e942b2 18#include "mkdir.h"
6bedfcbb 19#include "parse-util.h"
bde29068 20#include "path-util.h"
4e731273 21#include "proc-cmdline.h"
98bad05e 22#include "specifier.h"
07630cea 23#include "string-util.h"
0fa9e53d
JJ
24#include "strv.h"
25#include "unit-name.h"
26#include "util.h"
27
28typedef struct crypto_device {
29 char *uuid;
6cd5b12a 30 char *keyfile;
70f5f48e 31 char *keydev;
baade8cc 32 char *name;
0fa9e53d
JJ
33 char *options;
34 bool create;
35} crypto_device;
e23a0ce8 36
7a44c7e3 37static const char *arg_dest = NULL;
66a78c2b
LP
38static bool arg_enabled = true;
39static bool arg_read_crypttab = true;
a6c57e74 40static const char *arg_crypttab = NULL;
3f5ac303 41static const char *arg_runtime_directory = NULL;
6b000af4 42static bool arg_allow_list = false;
0fa9e53d
JJ
43static Hashmap *arg_disks = NULL;
44static char *arg_default_options = NULL;
45static char *arg_default_keyfile = NULL;
141a79f4 46
a4a90e65
YW
47STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep);
48STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep);
49STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep);
50
fc6f1ad1
OK
51static int split_locationspec(const char *locationspec, char **ret_file, char **ret_device) {
52 _cleanup_free_ char *file = NULL, *device = NULL;
f4ea8432 53 const char *c;
50d2eba2 54
fc6f1ad1
OK
55 assert(ret_file);
56 assert(ret_device);
50d2eba2 57
fc6f1ad1
OK
58 if (!locationspec) {
59 *ret_file = *ret_device = NULL;
fef716b2
ZJS
60 return 0;
61 }
62
fc6f1ad1 63 c = strrchr(locationspec, ':');
50d2eba2 64 if (c) {
fc6f1ad1 65 /* The device part has to be either an absolute path to device node (/dev/something,
32c6237a 66 * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device
fc6f1ad1 67 * specification starting with LABEL= or similar. The file part has the same syntax.
32c6237a 68 *
fc6f1ad1 69 * Let's try to guess if the second part looks like a device specification, or just part of a
32c6237a
ZJS
70 * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to
71 * an absolute path. If we didn't get an absolute path, assume that it is just part of the
fc6f1ad1 72 * first file argument. */
32c6237a 73
fc6f1ad1
OK
74 device = fstab_node_to_udev_node(c + 1);
75 if (!device)
50d2eba2 76 return log_oom();
32c6237a 77
fc6f1ad1
OK
78 if (path_is_absolute(device))
79 file = strndup(locationspec, c-locationspec);
32c6237a 80 else {
fc6f1ad1 81 log_debug("Location specification argument contains a colon, but \"%s\" doesn't look like a device specification.\n"
32c6237a 82 "Assuming that \"%s\" is a single device specification.",
fc6f1ad1
OK
83 c + 1, locationspec);
84 device = mfree(device);
32c6237a
ZJS
85 c = NULL;
86 }
87 }
88
89 if (!c)
fc6f1ad1
OK
90 /* No device specified */
91 file = strdup(locationspec);
32c6237a 92
fc6f1ad1 93 if (!file)
32c6237a 94 return log_oom();
50d2eba2 95
fc6f1ad1
OK
96 *ret_file = TAKE_PTR(file);
97 *ret_device = TAKE_PTR(device);
50d2eba2 98
99 return 0;
100}
101
eb7d9aa3 102static int generate_device_mount(
49685fb3 103 const char *name,
eb7d9aa3
OK
104 const char *device,
105 const char *type_prefix, /* "keydev" or "headerdev" */
106 const char *device_timeout,
49685fb3 107 bool canfail,
eb7d9aa3 108 bool readonly,
49685fb3
LP
109 char **unit,
110 char **mount) {
111
32c6237a 112 _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
70f5f48e
MS
113 _cleanup_fclose_ FILE *f = NULL;
114 int r;
50d2eba2 115 usec_t timeout_us;
70f5f48e
MS
116
117 assert(name);
eb7d9aa3 118 assert(device);
70f5f48e
MS
119 assert(unit);
120 assert(mount);
121
3f5ac303 122 r = mkdir_parents(arg_runtime_directory, 0755);
70f5f48e
MS
123 if (r < 0)
124 return r;
125
3f5ac303 126 r = mkdir(arg_runtime_directory, 0700);
579875bc
MS
127 if (r < 0 && errno != EEXIST)
128 return -errno;
70f5f48e 129
7949dfa7
MS
130 name_escaped = cescape(name);
131 if (!name_escaped)
132 return -ENOMEM;
133
eb7d9aa3 134 where = strjoin(arg_runtime_directory, "/", type_prefix, "-", name_escaped);
70f5f48e
MS
135 if (!where)
136 return -ENOMEM;
137
138 r = mkdir(where, 0700);
579875bc
MS
139 if (r < 0 && errno != EEXIST)
140 return -errno;
70f5f48e
MS
141
142 r = unit_name_from_path(where, ".mount", &u);
143 if (r < 0)
144 return r;
145
146 r = generator_open_unit_file(arg_dest, NULL, u, &f);
147 if (r < 0)
148 return r;
149
70f5f48e
MS
150 fprintf(f,
151 "[Unit]\n"
152 "DefaultDependencies=no\n\n"
153 "[Mount]\n"
154 "What=%s\n"
155 "Where=%s\n"
eb7d9aa3 156 "Options=%s%s\n", device, where, readonly ? "ro" : "rw", canfail ? ",nofail" : "");
50d2eba2 157
eb7d9aa3
OK
158 if (device_timeout) {
159 r = parse_sec_fix_0(device_timeout, &timeout_us);
50d2eba2 160 if (r >= 0) {
eb7d9aa3 161 r = unit_name_from_path(device, ".device", &device_unit);
50d2eba2 162 if (r < 0)
163 return log_error_errno(r, "Failed to generate unit name: %m");
164
165 r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout",
166 "# Automatically generated by systemd-cryptsetup-generator \n\n"
eb7d9aa3 167 "[Unit]\nJobRunningTimeoutSec=%s", device_timeout);
50d2eba2 168 if (r < 0)
169 return log_error_errno(r, "Failed to write device drop-in: %m");
170
171 } else
eb7d9aa3 172 log_warning_errno(r, "Failed to parse %s, ignoring: %m", device_timeout);
50d2eba2 173
174 }
70f5f48e
MS
175
176 r = fflush_and_check(f);
177 if (r < 0)
178 return r;
179
180 *unit = TAKE_PTR(u);
181 *mount = TAKE_PTR(where);
182
183 return 0;
184}
185
eb7d9aa3
OK
186static int generate_device_umount(const char *name,
187 const char *device_mount,
188 const char *type_prefix, /* "keydev" or "headerdev" */
882f5f42
MS
189 char **ret_umount_unit) {
190 _cleanup_fclose_ FILE *f = NULL;
191 _cleanup_free_ char *u = NULL, *name_escaped = NULL, *mount = NULL;
192 int r;
193
194 assert(name);
195 assert(ret_umount_unit);
196
197 name_escaped = cescape(name);
198 if (!name_escaped)
199 return -ENOMEM;
200
eb7d9aa3 201 u = strjoin(type_prefix, "-", name_escaped, "-umount.service");
882f5f42
MS
202 if (!u)
203 return -ENOMEM;
204
eb7d9aa3 205 r = unit_name_from_path(device_mount, ".mount", &mount);
882f5f42
MS
206 if (r < 0)
207 return r;
208
209 r = generator_open_unit_file(arg_dest, NULL, u, &f);
210 if (r < 0)
211 return r;
212
213 fprintf(f,
214 "[Unit]\n"
215 "DefaultDependencies=no\n"
216 "After=%s\n\n"
217 "[Service]\n"
eb7d9aa3 218 "ExecStart=-" UMOUNT_PATH " %s\n\n", mount, device_mount);
882f5f42
MS
219
220 r = fflush_and_check(f);
221 if (r < 0)
222 return r;
223
224 *ret_umount_unit = TAKE_PTR(u);
225 return 0;
226}
227
38ab1ec9
RS
228static int print_dependencies(FILE *f, const char* device_path) {
229 int r;
230
231 if (STR_IN_SET(device_path, "-", "none"))
232 /* None, nothing to do */
233 return 0;
234
c6b1d7d1
LP
235 if (PATH_IN_SET(device_path,
236 "/dev/urandom",
237 "/dev/random",
238 "/dev/hw_random",
239 "/dev/hwrng")) {
38ab1ec9
RS
240 /* RNG device, add random dep */
241 fputs("After=systemd-random-seed.service\n", f);
242 return 0;
243 }
244
245 _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path);
246 if (!udev_node)
247 return log_oom();
248
249 if (path_equal(udev_node, "/dev/null"))
250 return 0;
251
252 if (path_startswith(udev_node, "/dev/")) {
162392b7 253 /* We are dealing with a block device, add dependency for corresponding unit */
38ab1ec9
RS
254 _cleanup_free_ char *unit = NULL;
255
256 r = unit_name_from_path(udev_node, ".device", &unit);
257 if (r < 0)
258 return log_error_errno(r, "Failed to generate unit name: %m");
259
c6b1d7d1
LP
260 fprintf(f,
261 "After=%1$s\n"
262 "Requires=%1$s\n", unit);
38ab1ec9
RS
263 } else {
264 /* Regular file, add mount dependency */
265 _cleanup_free_ char *escaped_path = specifier_escape(device_path);
266 if (!escaped_path)
267 return log_oom();
268
269 fprintf(f, "RequiresMountsFor=%s\n", escaped_path);
270 }
271
272 return 0;
273}
274
e23a0ce8
LP
275static int create_disk(
276 const char *name,
277 const char *device,
278 const char *password,
50d2eba2 279 const char *keydev,
62ca7d3b
JL
280 const char *options,
281 const char *source) {
e23a0ce8 282
fb883e75 283 _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
a7e88558 284 *keydev_mount = NULL, *keyfile_timeout_value = NULL,
53ac130b
LP
285 *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *password_buffer = NULL,
286 *tmp_fstype = NULL;
7fd1b19b 287 _cleanup_fclose_ FILE *f = NULL;
b559616f 288 const char *dmname;
53ac130b
LP
289 bool noauto, nofail, swap, netdev, attach_in_initrd;
290 int r, detached_header, keyfile_can_timeout, tmp;
e23a0ce8
LP
291
292 assert(name);
293 assert(device);
294
b9f111b9
ZJS
295 noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
296 nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
a6dba978 297 swap = fstab_test_option(options, "swap\0");
b001ad61 298 netdev = fstab_test_option(options, "_netdev\0");
1dc85eff 299 attach_in_initrd = fstab_test_option(options, "x-initrd.attach\0");
8c11d3c1 300
50d2eba2 301 keyfile_can_timeout = fstab_filter_options(options, "keyfile-timeout\0", NULL, &keyfile_timeout_value, NULL);
302 if (keyfile_can_timeout < 0)
303 return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m");
304
38ab1ec9
RS
305 detached_header = fstab_filter_options(options, "header\0", NULL, &header_path, NULL);
306 if (detached_header < 0)
307 return log_error_errno(detached_header, "Failed to parse header= option value: %m");
308
53ac130b
LP
309 tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL);
310 if (tmp < 0)
311 return log_error_errno(tmp, "Failed to parse tmp= option value: %m");
312
baaa35ad
ZJS
313 if (tmp && swap)
314 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
315 "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
316 name);
155da457 317
98bad05e
LP
318 name_escaped = specifier_escape(name);
319 if (!name_escaped)
320 return log_oom();
321
744198e9
LP
322 e = unit_name_escape(name);
323 if (!e)
324 return log_oom();
325
f7f21d33 326 u = fstab_node_to_udev_node(device);
24a988e9
HH
327 if (!u)
328 return log_oom();
e23a0ce8 329
fb883e75
ZJS
330 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
331 if (r < 0)
332 return log_error_errno(r, "Failed to generate unit name: %m");
333
98bad05e
LP
334 u_escaped = specifier_escape(u);
335 if (!u_escaped)
336 return log_oom();
337
7410616c
LP
338 r = unit_name_from_path(u, ".device", &d);
339 if (r < 0)
340 return log_error_errno(r, "Failed to generate unit name: %m");
e23a0ce8 341
baaa35ad
ZJS
342 if (keydev && !password)
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
344 "Key device is specified, but path to the password file is missing.");
70f5f48e 345
fb883e75
ZJS
346 r = generator_open_unit_file(arg_dest, NULL, n, &f);
347 if (r < 0)
348 return r;
0d536673 349
62ca7d3b 350 r = generator_write_cryptsetup_unit_section(f, source);
a7e88558
LP
351 if (r < 0)
352 return r;
6bbd539e
LP
353
354 if (netdev)
355 fprintf(f, "After=remote-fs-pre.target\n");
e23a0ce8 356
1dc85eff
FB
357 /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
358 if (!attach_in_initrd)
359 fprintf(f, "Conflicts=umount.target\n");
360
70f5f48e 361 if (keydev) {
882f5f42 362 _cleanup_free_ char *unit = NULL, *umount_unit = NULL;
70f5f48e 363
eb7d9aa3
OK
364 r = generate_device_mount(
365 name,
366 keydev,
367 "keydev",
368 keyfile_timeout_value,
369 /* canfail = */ keyfile_can_timeout > 0,
370 /* readonly= */ true,
371 &unit,
372 &keydev_mount);
70f5f48e
MS
373 if (r < 0)
374 return log_error_errno(r, "Failed to generate keydev mount unit: %m");
375
eb7d9aa3 376 r = generate_device_umount(name, keydev_mount, "keydev", &umount_unit);
882f5f42
MS
377 if (r < 0)
378 return log_error_errno(r, "Failed to generate keydev umount unit: %m");
379
a7e88558
LP
380 password_buffer = path_join(keydev_mount, password);
381 if (!password_buffer)
70f5f48e
MS
382 return log_oom();
383
a7e88558 384 password = password_buffer;
50d2eba2 385
386 fprintf(f, "After=%s\n", unit);
387 if (keyfile_can_timeout > 0)
388 fprintf(f, "Wants=%s\n", unit);
389 else
390 fprintf(f, "Requires=%s\n", unit);
882f5f42
MS
391
392 if (umount_unit) {
393 fprintf(f,
394 "Wants=%s\n"
395 "Before=%s\n",
396 umount_unit,
397 umount_unit
398 );
399 }
70f5f48e
MS
400 }
401
155da457
LP
402 if (!nofail)
403 fprintf(f,
b001ad61
ZJS
404 "Before=%s\n",
405 netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
155da457 406
50d2eba2 407 if (password && !keydev) {
38ab1ec9
RS
408 r = print_dependencies(f, password);
409 if (r < 0)
410 return r;
411 }
bde29068 412
38ab1ec9
RS
413 /* Check if a header option was specified */
414 if (detached_header > 0) {
415 r = print_dependencies(f, header_path);
416 if (r < 0)
417 return r;
ceca9501 418 }
e23a0ce8 419
a7e88558 420 if (path_startswith(u, "/dev/"))
9ece938a
TW
421 fprintf(f,
422 "BindsTo=%s\n"
423 "After=%s\n"
424 "Before=umount.target\n",
425 d, d);
a7e88558 426 else
b90cbe66
LHS
427 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
428 dependency to ensure that loopback support is available in
429 the kernel (/dev/loop-control needs to exist) */
9ece938a 430 fprintf(f,
b90cbe66
LHS
431 "RequiresMountsFor=%s\n"
432 "Requires=systemd-tmpfiles-setup-dev.service\n"
433 "After=systemd-tmpfiles-setup-dev.service\n",
98bad05e
LP
434 u_escaped);
435
8eea8687
ZJS
436 r = generator_write_timeouts(arg_dest, device, name, options, &filtered);
437 if (r < 0)
7cecc563 438 log_warning_errno(r, "Failed to write device timeout drop-in: %m");
8eea8687 439
a7e88558
LP
440 r = generator_write_cryptsetup_service_section(f, name, u, password, filtered);
441 if (r < 0)
442 return r;
e23a0ce8 443
53ac130b
LP
444 if (tmp) {
445 _cleanup_free_ char *tmp_fstype_escaped = NULL;
446
447 if (tmp_fstype) {
448 tmp_fstype_escaped = specifier_escape(tmp_fstype);
449 if (!tmp_fstype_escaped)
450 return log_oom();
451 }
452
e23a0ce8 453 fprintf(f,
53ac130b
LP
454 "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n",
455 tmp_fstype_escaped ?: "ext4", name_escaped);
456 }
e23a0ce8 457
8c11d3c1 458 if (swap)
e23a0ce8 459 fprintf(f,
db2c56b0 460 "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n",
98bad05e 461 name_escaped);
e23a0ce8 462
4652c56c
ZJS
463 r = fflush_and_check(f);
464 if (r < 0)
fb883e75 465 return log_error_errno(r, "Failed to write unit file %s: %m", n);
e23a0ce8 466
155da457 467 if (!noauto) {
b001ad61
ZJS
468 r = generator_add_symlink(arg_dest,
469 netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
b559616f
ZJS
470 nofail ? "wants" : "requires", n);
471 if (r < 0)
472 return r;
f7f21d33 473 }
74715b82 474
b559616f
ZJS
475 dmname = strjoina("dev-mapper-", e, ".device");
476 r = generator_add_symlink(arg_dest, dmname, "requires", n);
477 if (r < 0)
478 return r;
74715b82 479
68395007 480 if (!noauto && !nofail) {
7cecc563
ZJS
481 r = write_drop_in(arg_dest, dmname, 40, "device-timeout",
482 "# Automatically generated by systemd-cryptsetup-generator\n\n"
8eea8687 483 "[Unit]\nJobTimeoutSec=0");
23bbb0de 484 if (r < 0)
7cecc563 485 log_warning_errno(r, "Failed to write device timeout drop-in: %m");
68395007
HH
486 }
487
24a988e9 488 return 0;
e23a0ce8
LP
489}
490
a4a90e65
YW
491static crypto_device* crypt_device_free(crypto_device *d) {
492 if (!d)
493 return NULL;
494
6dd1c368
ZJS
495 free(d->uuid);
496 free(d->keyfile);
70f5f48e 497 free(d->keydev);
6dd1c368
ZJS
498 free(d->name);
499 free(d->options);
a4a90e65 500 return mfree(d);
0fa9e53d
JJ
501}
502
503static crypto_device *get_crypto_device(const char *uuid) {
504 int r;
505 crypto_device *d;
506
507 assert(uuid);
508
509 d = hashmap_get(arg_disks, uuid);
510 if (!d) {
511 d = new0(struct crypto_device, 1);
512 if (!d)
513 return NULL;
514
0fa9e53d 515 d->uuid = strdup(uuid);
6b430fdb
ZJS
516 if (!d->uuid)
517 return mfree(d);
0fa9e53d
JJ
518
519 r = hashmap_put(arg_disks, d->uuid, d);
520 if (r < 0) {
521 free(d->uuid);
6b430fdb 522 return mfree(d);
0fa9e53d
JJ
523 }
524 }
525
526 return d;
527}
528
c3ee5b34
OK
529static bool warn_uuid_invalid(const char *uuid, const char *key) {
530 assert(key);
531
532 if (!id128_is_valid(uuid)) {
533 log_warning("Failed to parse %s= kernel command line switch. UUID is invalid, ignoring.", key);
534 return true;
535 }
536
537 return false;
538}
539
96287a49 540static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
0fa9e53d 541 _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
1d84ad94
LP
542 crypto_device *d;
543 int r;
66a78c2b 544
1d84ad94 545 if (streq(key, "luks")) {
059cb385 546
1d84ad94 547 r = value ? parse_boolean(value) : 1;
141a79f4 548 if (r < 0)
1d84ad94 549 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
141a79f4
ZJS
550 else
551 arg_enabled = r;
66a78c2b 552
1d84ad94 553 } else if (streq(key, "luks.crypttab")) {
66a78c2b 554
1d84ad94 555 r = value ? parse_boolean(value) : 1;
141a79f4 556 if (r < 0)
1d84ad94 557 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
141a79f4
ZJS
558 else
559 arg_read_crypttab = r;
66a78c2b 560
1d84ad94
LP
561 } else if (streq(key, "luks.uuid")) {
562
563 if (proc_cmdline_value_missing(key, value))
564 return 0;
66a78c2b 565
263a7964 566 d = get_crypto_device(startswith(value, "luks-") ?: value);
0fa9e53d 567 if (!d)
141a79f4 568 return log_oom();
66a78c2b 569
6b000af4 570 d->create = arg_allow_list = true;
0fa9e53d 571
1d84ad94
LP
572 } else if (streq(key, "luks.options")) {
573
574 if (proc_cmdline_value_missing(key, value))
575 return 0;
66a78c2b 576
0fa9e53d
JJ
577 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
578 if (r == 2) {
579 d = get_crypto_device(uuid);
580 if (!d)
581 return log_oom();
582
1d84ad94 583 free_and_replace(d->options, uuid_value);
0fa9e53d 584 } else if (free_and_strdup(&arg_default_options, value) < 0)
141a79f4 585 return log_oom();
66a78c2b 586
1d84ad94 587 } else if (streq(key, "luks.key")) {
7949dfa7
MS
588 size_t n;
589 _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
7949dfa7 590 const char *keyspec;
1d84ad94
LP
591
592 if (proc_cmdline_value_missing(key, value))
593 return 0;
66a78c2b 594
008fd4f9 595 n = strspn(value, ALPHANUMERICAL "-");
7949dfa7
MS
596 if (value[n] != '=') {
597 if (free_and_strdup(&arg_default_keyfile, value) < 0)
598 return log_oom();
599 return 0;
600 }
70f5f48e 601
7949dfa7
MS
602 uuid = strndup(value, n);
603 if (!uuid)
604 return log_oom();
6cd5b12a 605
c3ee5b34 606 if (warn_uuid_invalid(uuid, key))
7949dfa7 607 return 0;
7949dfa7
MS
608
609 d = get_crypto_device(uuid);
610 if (!d)
611 return log_oom();
70f5f48e 612
7949dfa7 613 keyspec = value + n + 1;
fc6f1ad1 614 r = split_locationspec(keyspec, &keyfile, &keydev);
50d2eba2 615 if (r < 0)
616 return r;
70f5f48e 617
7949dfa7
MS
618 free_and_replace(d->keyfile, keyfile);
619 free_and_replace(d->keydev, keydev);
50d2eba2 620
1d84ad94
LP
621 } else if (streq(key, "luks.name")) {
622
623 if (proc_cmdline_value_missing(key, value))
624 return 0;
baade8cc
JJ
625
626 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
627 if (r == 2) {
628 d = get_crypto_device(uuid);
629 if (!d)
630 return log_oom();
631
6b000af4 632 d->create = arg_allow_list = true;
baade8cc 633
f9ecfd3b 634 free_and_replace(d->name, uuid_value);
baade8cc
JJ
635 } else
636 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
85013844 637 }
66a78c2b 638
24a988e9 639 return 0;
66a78c2b
LP
640}
641
0fa9e53d 642static int add_crypttab_devices(void) {
7fd1b19b 643 _cleanup_fclose_ FILE *f = NULL;
b42674a1
LP
644 unsigned crypttab_line = 0;
645 struct stat st;
646 int r;
e23a0ce8 647
0fa9e53d
JJ
648 if (!arg_read_crypttab)
649 return 0;
650
a6c57e74 651 r = fopen_unlocked(arg_crypttab, "re", &f);
fdeea3f4 652 if (r < 0) {
0fa9e53d 653 if (errno != ENOENT)
a6c57e74 654 log_error_errno(errno, "Failed to open %s: %m", arg_crypttab);
0fa9e53d 655 return 0;
e23a0ce8
LP
656 }
657
0fa9e53d 658 if (fstat(fileno(f), &st) < 0) {
a6c57e74 659 log_error_errno(errno, "Failed to stat %s: %m", arg_crypttab);
0fa9e53d
JJ
660 return 0;
661 }
e23a0ce8 662
0fa9e53d 663 for (;;) {
fef716b2 664 _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, *keyfile = NULL, *keydev = NULL;
0fa9e53d 665 crypto_device *d = NULL;
b42674a1
LP
666 char *l, *uuid;
667 int k;
4c12626c 668
b42674a1
LP
669 r = read_line(f, LONG_LINE_MAX, &line);
670 if (r < 0)
a6c57e74 671 return log_error_errno(r, "Failed to read %s: %m", arg_crypttab);
b42674a1 672 if (r == 0)
0fa9e53d 673 break;
66a78c2b 674
0fa9e53d 675 crypttab_line++;
141a79f4 676
0fa9e53d 677 l = strstrip(line);
b42674a1 678 if (IN_SET(l[0], 0, '#'))
0fa9e53d 679 continue;
66a78c2b 680
50d2eba2 681 k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
0fa9e53d 682 if (k < 2 || k > 4) {
a6c57e74 683 log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line);
0fa9e53d
JJ
684 continue;
685 }
66a78c2b 686
844d5a87 687 uuid = startswith(device, "UUID=");
0fa9e53d
JJ
688 if (!uuid)
689 uuid = path_startswith(device, "/dev/disk/by-uuid/");
844d5a87
LR
690 if (!uuid)
691 uuid = startswith(name, "luks-");
0fa9e53d
JJ
692 if (uuid)
693 d = hashmap_get(arg_disks, uuid);
8973790e 694
6b000af4 695 if (arg_allow_list && !d) {
0fa9e53d
JJ
696 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
697 continue;
8973790e
LP
698 }
699
fc6f1ad1 700 r = split_locationspec(keyspec, &keyfile, &keydev);
50d2eba2 701 if (r < 0)
702 return r;
703
62ca7d3b 704 r = create_disk(name, device, keyfile, keydev, (d && d->options) ? d->options : options, arg_crypttab);
0fa9e53d
JJ
705 if (r < 0)
706 return r;
8973790e 707
0fa9e53d
JJ
708 if (d)
709 d->create = false;
710 }
8973790e 711
0fa9e53d
JJ
712 return 0;
713}
66a78c2b 714
0fa9e53d
JJ
715static int add_proc_cmdline_devices(void) {
716 int r;
0fa9e53d 717 crypto_device *d;
66a78c2b 718
90e74a66 719 HASHMAP_FOREACH(d, arg_disks) {
baade8cc 720 _cleanup_free_ char *device = NULL;
66a78c2b 721
0fa9e53d
JJ
722 if (!d->create)
723 continue;
e23a0ce8 724
baade8cc 725 if (!d->name) {
b910cc72 726 d->name = strjoin("luks-", d->uuid);
baade8cc
JJ
727 if (!d->name)
728 return log_oom();
729 }
e23a0ce8 730
b910cc72 731 device = strjoin("UUID=", d->uuid);
0fa9e53d
JJ
732 if (!device)
733 return log_oom();
7ab064a6 734
7cecc563
ZJS
735 r = create_disk(d->name,
736 device,
737 d->keyfile ?: arg_default_keyfile,
738 d->keydev,
62ca7d3b
JL
739 d->options ?: arg_default_options,
740 "/proc/cmdline");
0fa9e53d
JJ
741 if (r < 0)
742 return r;
e23a0ce8
LP
743 }
744
0fa9e53d
JJ
745 return 0;
746}
e23a0ce8 747
a4a90e65
YW
748DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
749 crypto_device, crypt_device_free);
750
7a44c7e3 751static int run(const char *dest, const char *dest_early, const char *dest_late) {
5f4bfe56 752 int r;
e23a0ce8 753
7a44c7e3 754 assert_se(arg_dest = dest);
e23a0ce8 755
a6c57e74 756 arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab";
3f5ac303 757 arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup";
a6c57e74 758
a4a90e65
YW
759 arg_disks = hashmap_new(&crypt_device_hash_ops);
760 if (!arg_disks)
761 return log_oom();
7ab064a6 762
1d84ad94 763 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
a4a90e65
YW
764 if (r < 0)
765 return log_warning_errno(r, "Failed to parse kernel command line: %m");
7ab064a6 766
a4a90e65
YW
767 if (!arg_enabled)
768 return 0;
e23a0ce8 769
5f4bfe56
LP
770 r = add_crypttab_devices();
771 if (r < 0)
a4a90e65 772 return r;
0fa9e53d 773
5f4bfe56
LP
774 r = add_proc_cmdline_devices();
775 if (r < 0)
a4a90e65 776 return r;
141a79f4 777
a4a90e65 778 return 0;
e23a0ce8 779}
a4a90e65 780
7a44c7e3 781DEFINE_MAIN_GENERATOR_FUNCTION(run);