1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
13 #include "fstab-util.h"
14 #include "generator.h"
16 #include "id128-util.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"
25 #include "unit-name.h"
28 typedef struct crypto_device
{
37 static const char *arg_dest
= NULL
;
38 static bool arg_enabled
= true;
39 static bool arg_read_crypttab
= true;
40 static bool arg_whitelist
= false;
41 static Hashmap
*arg_disks
= NULL
;
42 static char *arg_default_options
= NULL
;
43 static char *arg_default_keyfile
= NULL
;
45 STATIC_DESTRUCTOR_REGISTER(arg_disks
, hashmap_freep
);
46 STATIC_DESTRUCTOR_REGISTER(arg_default_options
, freep
);
47 STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile
, freep
);
49 static int generate_keydev_mount(const char *name
, const char *keydev
, char **unit
, char **mount
) {
50 _cleanup_free_
char *u
= NULL
, *what
= NULL
, *where
= NULL
, *name_escaped
= NULL
;
51 _cleanup_fclose_
FILE *f
= NULL
;
59 r
= mkdir_parents("/run/systemd/cryptsetup", 0755);
63 r
= mkdir("/run/systemd/cryptsetup", 0700);
64 if (r
< 0 && errno
!= EEXIST
)
67 name_escaped
= cescape(name
);
71 where
= strjoin("/run/systemd/cryptsetup/keydev-", name_escaped
);
75 r
= mkdir(where
, 0700);
76 if (r
< 0 && errno
!= EEXIST
)
79 r
= unit_name_from_path(where
, ".mount", &u
);
83 r
= generator_open_unit_file(arg_dest
, NULL
, u
, &f
);
87 what
= fstab_node_to_udev_node(keydev
);
93 "DefaultDependencies=no\n\n"
97 "Options=ro\n", what
, where
);
99 r
= fflush_and_check(f
);
104 *mount
= TAKE_PTR(where
);
109 static int print_dependencies(FILE *f
, const char* device_path
) {
112 if (STR_IN_SET(device_path
, "-", "none"))
113 /* None, nothing to do */
116 if (PATH_IN_SET(device_path
, "/dev/urandom", "/dev/random", "/dev/hw_random")) {
117 /* RNG device, add random dep */
118 fputs("After=systemd-random-seed.service\n", f
);
122 _cleanup_free_
char *udev_node
= fstab_node_to_udev_node(device_path
);
126 if (path_equal(udev_node
, "/dev/null"))
129 if (path_startswith(udev_node
, "/dev/")) {
130 /* We are dealing with a block device, add dependency for correspoding unit */
131 _cleanup_free_
char *unit
= NULL
;
133 r
= unit_name_from_path(udev_node
, ".device", &unit
);
135 return log_error_errno(r
, "Failed to generate unit name: %m");
137 fprintf(f
, "After=%1$s\nRequires=%1$s\n", unit
);
139 /* Regular file, add mount dependency */
140 _cleanup_free_
char *escaped_path
= specifier_escape(device_path
);
144 fprintf(f
, "RequiresMountsFor=%s\n", escaped_path
);
150 static int create_disk(
154 const char *password
,
155 const char *options
) {
157 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
158 *filtered
= NULL
, *u_escaped
= NULL
, *password_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
, *keydev_mount
= NULL
, *header_path
= NULL
;
159 _cleanup_fclose_
FILE *f
= NULL
;
161 bool noauto
, nofail
, tmp
, swap
, netdev
;
162 int r
, detached_header
;
167 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
168 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
169 tmp
= fstab_test_option(options
, "tmp\0");
170 swap
= fstab_test_option(options
, "swap\0");
171 netdev
= fstab_test_option(options
, "_netdev\0");
173 detached_header
= fstab_filter_options(options
, "header\0", NULL
, &header_path
, NULL
);
174 if (detached_header
< 0)
175 return log_error_errno(detached_header
, "Failed to parse header= option value: %m");
178 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
179 "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
182 name_escaped
= specifier_escape(name
);
186 e
= unit_name_escape(name
);
190 u
= fstab_node_to_udev_node(device
);
194 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
196 return log_error_errno(r
, "Failed to generate unit name: %m");
198 u_escaped
= specifier_escape(u
);
202 r
= unit_name_from_path(u
, ".device", &d
);
204 return log_error_errno(r
, "Failed to generate unit name: %m");
207 password_escaped
= specifier_escape(password
);
208 if (!password_escaped
)
212 if (keydev
&& !password
)
213 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
214 "Key device is specified, but path to the password file is missing.");
216 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
222 "Description=Cryptography Setup for %%I\n"
223 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
224 "SourcePath=/etc/crypttab\n"
225 "DefaultDependencies=no\n"
226 "Conflicts=umount.target\n"
227 "IgnoreOnIsolate=true\n"
229 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
232 _cleanup_free_
char *unit
= NULL
, *p
= NULL
;
234 r
= generate_keydev_mount(name
, keydev
, &unit
, &keydev_mount
);
236 return log_error_errno(r
, "Failed to generate keydev mount unit: %m");
238 p
= prefix_root(keydev_mount
, password_escaped
);
242 free_and_replace(password_escaped
, p
);
248 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
251 r
= print_dependencies(f
, password
);
256 /* Check if a header option was specified */
257 if (detached_header
> 0) {
258 r
= print_dependencies(f
, header_path
);
263 if (path_startswith(u
, "/dev/")) {
267 "Before=umount.target\n",
271 fputs("Before=dev-mapper-%i.swap\n",
274 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
275 dependency to ensure that loopback support is available in
276 the kernel (/dev/loop-control needs to exist) */
278 "RequiresMountsFor=%s\n"
279 "Requires=systemd-tmpfiles-setup-dev.service\n"
280 "After=systemd-tmpfiles-setup-dev.service\n",
283 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
288 filtered_escaped
= specifier_escape(filtered
);
289 if (!filtered_escaped
)
296 "RemainAfterExit=yes\n"
297 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
298 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
299 "OOMScoreAdjust=500\n" /* unlocking can allocate a lot of memory if Argon2 is used */
300 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
301 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
302 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
307 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
312 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
317 "ExecStartPost=" UMOUNT_PATH
" %s\n\n",
320 r
= fflush_and_check(f
);
322 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
325 r
= generator_add_symlink(arg_dest
,
326 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
327 nofail
? "wants" : "requires", n
);
332 dmname
= strjoina("dev-mapper-", e
, ".device");
333 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
337 if (!noauto
&& !nofail
) {
338 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
339 "# Automatically generated by systemd-cryptsetup-generator \n\n"
340 "[Unit]\nJobTimeoutSec=0");
342 return log_error_errno(r
, "Failed to write device drop-in: %m");
348 static crypto_device
* crypt_device_free(crypto_device
*d
) {
360 static crypto_device
*get_crypto_device(const char *uuid
) {
366 d
= hashmap_get(arg_disks
, uuid
);
368 d
= new0(struct crypto_device
, 1);
373 d
->keyfile
= d
->options
= d
->name
= NULL
;
375 d
->uuid
= strdup(uuid
);
379 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
389 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
390 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
394 if (streq(key
, "luks")) {
396 r
= value
? parse_boolean(value
) : 1;
398 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
402 } else if (streq(key
, "luks.crypttab")) {
404 r
= value
? parse_boolean(value
) : 1;
406 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
408 arg_read_crypttab
= r
;
410 } else if (streq(key
, "luks.uuid")) {
412 if (proc_cmdline_value_missing(key
, value
))
415 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
419 d
->create
= arg_whitelist
= true;
421 } else if (streq(key
, "luks.options")) {
423 if (proc_cmdline_value_missing(key
, value
))
426 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
428 d
= get_crypto_device(uuid
);
432 free_and_replace(d
->options
, uuid_value
);
433 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
436 } else if (streq(key
, "luks.key")) {
438 _cleanup_free_
char *keyfile
= NULL
, *keydev
= NULL
;
442 if (proc_cmdline_value_missing(key
, value
))
445 n
= strspn(value
, LETTERS DIGITS
"-");
446 if (value
[n
] != '=') {
447 if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
452 uuid
= strndup(value
, n
);
456 if (!id128_is_valid(uuid
)) {
457 log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring.");
461 d
= get_crypto_device(uuid
);
465 keyspec
= value
+ n
+ 1;
466 c
= strrchr(keyspec
, ':');
469 keyfile
= strdup(keyspec
);
470 keydev
= strdup(c
+ 1);
472 if (!keyfile
|| !keydev
)
475 /* No keydev specified */
476 keyfile
= strdup(keyspec
);
481 free_and_replace(d
->keyfile
, keyfile
);
482 free_and_replace(d
->keydev
, keydev
);
483 } else if (streq(key
, "luks.name")) {
485 if (proc_cmdline_value_missing(key
, value
))
488 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
490 d
= get_crypto_device(uuid
);
494 d
->create
= arg_whitelist
= true;
496 free_and_replace(d
->name
, uuid_value
);
498 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
504 static int add_crypttab_devices(void) {
505 _cleanup_fclose_
FILE *f
= NULL
;
506 unsigned crypttab_line
= 0;
510 if (!arg_read_crypttab
)
513 r
= fopen_unlocked("/etc/crypttab", "re", &f
);
516 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
520 if (fstat(fileno(f
), &st
) < 0) {
521 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
526 _cleanup_free_
char *line
= NULL
, *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
527 crypto_device
*d
= NULL
;
531 r
= read_line(f
, LONG_LINE_MAX
, &line
);
533 return log_error_errno(r
, "Failed to read /etc/crypttab: %m");
540 if (IN_SET(l
[0], 0, '#'))
543 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
544 if (k
< 2 || k
> 4) {
545 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
549 uuid
= STARTSWITH_SET(device
, "UUID=", "luks-");
551 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
553 d
= hashmap_get(arg_disks
, uuid
);
555 if (arg_whitelist
&& !d
) {
556 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
560 r
= create_disk(name
, device
, NULL
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
571 static int add_proc_cmdline_devices(void) {
576 HASHMAP_FOREACH(d
, arg_disks
, i
) {
578 _cleanup_free_
char *device
= NULL
;
584 d
->name
= strappend("luks-", d
->uuid
);
589 device
= strappend("UUID=", d
->uuid
);
594 options
= d
->options
;
595 else if (arg_default_options
)
596 options
= arg_default_options
;
598 options
= "timeout=0";
600 r
= create_disk(d
->name
, device
, d
->keydev
, d
->keyfile
?: arg_default_keyfile
, options
);
608 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops
, char, string_hash_func
, string_compare_func
,
609 crypto_device
, crypt_device_free
);
611 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
614 assert_se(arg_dest
= dest
);
616 arg_disks
= hashmap_new(&crypt_device_hash_ops
);
620 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
622 return log_warning_errno(r
, "Failed to parse kernel command line: %m");
627 r
= add_crypttab_devices();
631 r
= add_proc_cmdline_devices();
638 DEFINE_MAIN_GENERATOR_FUNCTION(run
);