1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
11 #include "fstab-util.h"
12 #include "generator.h"
14 #include "id128-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "proc-cmdline.h"
20 #include "specifier.h"
21 #include "string-util.h"
23 #include "unit-name.h"
26 typedef struct crypto_device
{
35 static const char *arg_dest
= NULL
;
36 static bool arg_enabled
= true;
37 static bool arg_read_crypttab
= true;
38 static bool arg_whitelist
= false;
39 static Hashmap
*arg_disks
= NULL
;
40 static char *arg_default_options
= NULL
;
41 static char *arg_default_keyfile
= NULL
;
43 STATIC_DESTRUCTOR_REGISTER(arg_disks
, hashmap_freep
);
44 STATIC_DESTRUCTOR_REGISTER(arg_default_options
, freep
);
45 STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile
, freep
);
47 static int generate_keydev_mount(const char *name
, const char *keydev
, char **unit
, char **mount
) {
48 _cleanup_free_
char *u
= NULL
, *what
= NULL
, *where
= NULL
, *name_escaped
= NULL
;
49 _cleanup_fclose_
FILE *f
= NULL
;
57 r
= mkdir_parents("/run/systemd/cryptsetup", 0755);
61 r
= mkdir("/run/systemd/cryptsetup", 0700);
62 if (r
< 0 && errno
!= EEXIST
)
65 name_escaped
= cescape(name
);
69 where
= strjoin("/run/systemd/cryptsetup/keydev-", name_escaped
);
73 r
= mkdir(where
, 0700);
74 if (r
< 0 && errno
!= EEXIST
)
77 r
= unit_name_from_path(where
, ".mount", &u
);
81 r
= generator_open_unit_file(arg_dest
, NULL
, u
, &f
);
85 what
= fstab_node_to_udev_node(keydev
);
91 "DefaultDependencies=no\n\n"
95 "Options=ro\n", what
, where
);
97 r
= fflush_and_check(f
);
102 *mount
= TAKE_PTR(where
);
107 static int create_disk(
111 const char *password
,
112 const char *options
) {
114 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
115 *filtered
= NULL
, *u_escaped
= NULL
, *password_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
, *keydev_mount
= NULL
;
116 _cleanup_fclose_
FILE *f
= NULL
;
118 bool noauto
, nofail
, tmp
, swap
, netdev
;
124 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
125 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
126 tmp
= fstab_test_option(options
, "tmp\0");
127 swap
= fstab_test_option(options
, "swap\0");
128 netdev
= fstab_test_option(options
, "_netdev\0");
131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
132 "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
135 name_escaped
= specifier_escape(name
);
139 e
= unit_name_escape(name
);
143 u
= fstab_node_to_udev_node(device
);
147 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
149 return log_error_errno(r
, "Failed to generate unit name: %m");
151 u_escaped
= specifier_escape(u
);
155 r
= unit_name_from_path(u
, ".device", &d
);
157 return log_error_errno(r
, "Failed to generate unit name: %m");
160 password_escaped
= specifier_escape(password
);
161 if (!password_escaped
)
165 if (keydev
&& !password
)
166 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
167 "Key device is specified, but path to the password file is missing.");
169 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
175 "Description=Cryptography Setup for %%I\n"
176 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
177 "SourcePath=/etc/crypttab\n"
178 "DefaultDependencies=no\n"
179 "Conflicts=umount.target\n"
180 "IgnoreOnIsolate=true\n"
182 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
185 _cleanup_free_
char *unit
= NULL
, *p
= NULL
;
187 r
= generate_keydev_mount(name
, keydev
, &unit
, &keydev_mount
);
189 return log_error_errno(r
, "Failed to generate keydev mount unit: %m");
191 p
= prefix_root(keydev_mount
, password_escaped
);
195 free_and_replace(password_escaped
, p
);
201 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
204 if (PATH_IN_SET(password
, "/dev/urandom", "/dev/random", "/dev/hw_random"))
205 fputs("After=systemd-random-seed.service\n", f
);
206 else if (!STR_IN_SET(password
, "-", "none")) {
207 _cleanup_free_
char *uu
;
209 uu
= fstab_node_to_udev_node(password
);
213 if (!path_equal(uu
, "/dev/null")) {
215 if (path_startswith(uu
, "/dev/")) {
216 _cleanup_free_
char *dd
= NULL
;
218 r
= unit_name_from_path(uu
, ".device", &dd
);
220 return log_error_errno(r
, "Failed to generate unit name: %m");
222 fprintf(f
, "After=%1$s\nRequires=%1$s\n", dd
);
224 fprintf(f
, "RequiresMountsFor=%s\n", password_escaped
);
229 if (path_startswith(u
, "/dev/")) {
233 "Before=umount.target\n",
237 fputs("Before=dev-mapper-%i.swap\n",
240 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
241 dependency to ensure that loopback support is available in
242 the kernel (/dev/loop-control needs to exist) */
244 "RequiresMountsFor=%s\n"
245 "Requires=systemd-tmpfiles-setup-dev.service\n"
246 "After=systemd-tmpfiles-setup-dev.service\n",
249 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
254 filtered_escaped
= specifier_escape(filtered
);
255 if (!filtered_escaped
)
262 "RemainAfterExit=yes\n"
263 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
264 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
265 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
266 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
267 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
272 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
277 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
282 "ExecStartPost=" UMOUNT_PATH
" %s\n\n",
285 r
= fflush_and_check(f
);
287 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
290 r
= generator_add_symlink(arg_dest
,
291 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
292 nofail
? "wants" : "requires", n
);
297 dmname
= strjoina("dev-mapper-", e
, ".device");
298 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
302 if (!noauto
&& !nofail
) {
303 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
304 "# Automatically generated by systemd-cryptsetup-generator \n\n"
305 "[Unit]\nJobTimeoutSec=0");
307 return log_error_errno(r
, "Failed to write device drop-in: %m");
313 static crypto_device
* crypt_device_free(crypto_device
*d
) {
325 static crypto_device
*get_crypto_device(const char *uuid
) {
331 d
= hashmap_get(arg_disks
, uuid
);
333 d
= new0(struct crypto_device
, 1);
338 d
->keyfile
= d
->options
= d
->name
= NULL
;
340 d
->uuid
= strdup(uuid
);
344 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
354 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
355 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
359 if (streq(key
, "luks")) {
361 r
= value
? parse_boolean(value
) : 1;
363 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
367 } else if (streq(key
, "luks.crypttab")) {
369 r
= value
? parse_boolean(value
) : 1;
371 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
373 arg_read_crypttab
= r
;
375 } else if (streq(key
, "luks.uuid")) {
377 if (proc_cmdline_value_missing(key
, value
))
380 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
384 d
->create
= arg_whitelist
= true;
386 } else if (streq(key
, "luks.options")) {
388 if (proc_cmdline_value_missing(key
, value
))
391 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
393 d
= get_crypto_device(uuid
);
397 free_and_replace(d
->options
, uuid_value
);
398 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
401 } else if (streq(key
, "luks.key")) {
403 _cleanup_free_
char *keyfile
= NULL
, *keydev
= NULL
;
407 if (proc_cmdline_value_missing(key
, value
))
410 n
= strspn(value
, LETTERS DIGITS
"-");
411 if (value
[n
] != '=') {
412 if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
417 uuid
= strndup(value
, n
);
421 if (!id128_is_valid(uuid
)) {
422 log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring.");
426 d
= get_crypto_device(uuid
);
430 keyspec
= value
+ n
+ 1;
431 c
= strrchr(keyspec
, ':');
434 keyfile
= strdup(keyspec
);
435 keydev
= strdup(c
+ 1);
437 if (!keyfile
|| !keydev
)
440 /* No keydev specified */
441 keyfile
= strdup(keyspec
);
446 free_and_replace(d
->keyfile
, keyfile
);
447 free_and_replace(d
->keydev
, keydev
);
448 } else if (streq(key
, "luks.name")) {
450 if (proc_cmdline_value_missing(key
, value
))
453 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
455 d
= get_crypto_device(uuid
);
459 d
->create
= arg_whitelist
= true;
461 free_and_replace(d
->name
, uuid_value
);
463 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
469 static int add_crypttab_devices(void) {
470 _cleanup_fclose_
FILE *f
= NULL
;
471 unsigned crypttab_line
= 0;
475 if (!arg_read_crypttab
)
478 f
= fopen("/etc/crypttab", "re");
481 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
485 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
487 if (fstat(fileno(f
), &st
) < 0) {
488 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
493 _cleanup_free_
char *line
= NULL
, *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
494 crypto_device
*d
= NULL
;
498 r
= read_line(f
, LONG_LINE_MAX
, &line
);
500 return log_error_errno(r
, "Failed to read /etc/crypttab: %m");
507 if (IN_SET(l
[0], 0, '#'))
510 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
511 if (k
< 2 || k
> 4) {
512 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
516 uuid
= STARTSWITH_SET(device
, "UUID=", "luks-");
518 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
520 d
= hashmap_get(arg_disks
, uuid
);
522 if (arg_whitelist
&& !d
) {
523 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
527 r
= create_disk(name
, device
, NULL
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
538 static int add_proc_cmdline_devices(void) {
543 HASHMAP_FOREACH(d
, arg_disks
, i
) {
545 _cleanup_free_
char *device
= NULL
;
551 d
->name
= strappend("luks-", d
->uuid
);
556 device
= strappend("UUID=", d
->uuid
);
561 options
= d
->options
;
562 else if (arg_default_options
)
563 options
= arg_default_options
;
565 options
= "timeout=0";
567 r
= create_disk(d
->name
, device
, d
->keydev
, d
->keyfile
?: arg_default_keyfile
, options
);
575 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops
, char, string_hash_func
, string_compare_func
,
576 crypto_device
, crypt_device_free
);
578 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
581 assert_se(arg_dest
= dest
);
583 arg_disks
= hashmap_new(&crypt_device_hash_ops
);
587 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
589 return log_warning_errno(r
, "Failed to parse kernel command line: %m");
594 r
= add_crypttab_devices();
598 r
= add_proc_cmdline_devices();
605 DEFINE_MAIN_GENERATOR_FUNCTION(run
);