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 split_keyspec(const char *keyspec
, char **ret_keyfile
, char **ret_keydev
) {
50 _cleanup_free_
char *keyfile
= NULL
, *keydev
= NULL
;
57 *ret_keyfile
= *ret_keydev
= NULL
;
61 c
= strrchr(keyspec
, ':');
63 keyfile
= strndup(keyspec
, c
-keyspec
);
64 keydev
= strdup(c
+ 1);
65 if (!keyfile
|| !keydev
)
68 /* No keydev specified */
69 keyfile
= strdup(keyspec
);
75 *ret_keyfile
= TAKE_PTR(keyfile
);
76 *ret_keydev
= TAKE_PTR(keydev
);
81 static int generate_keydev_mount(const char *name
, const char *keydev
, const char *keydev_timeout
, bool canfail
, char **unit
, char **mount
) {
82 _cleanup_free_
char *u
= NULL
, *what
= NULL
, *where
= NULL
, *name_escaped
= NULL
, *device_unit
= NULL
;
83 _cleanup_fclose_
FILE *f
= NULL
;
92 r
= mkdir_parents("/run/systemd/cryptsetup", 0755);
96 r
= mkdir("/run/systemd/cryptsetup", 0700);
97 if (r
< 0 && errno
!= EEXIST
)
100 name_escaped
= cescape(name
);
104 where
= strjoin("/run/systemd/cryptsetup/keydev-", name_escaped
);
108 r
= mkdir(where
, 0700);
109 if (r
< 0 && errno
!= EEXIST
)
112 r
= unit_name_from_path(where
, ".mount", &u
);
116 r
= generator_open_unit_file(arg_dest
, NULL
, u
, &f
);
120 what
= fstab_node_to_udev_node(keydev
);
126 "DefaultDependencies=no\n\n"
130 "Options=ro%s\n", what
, where
, canfail
? ",nofail" : "");
132 if (keydev_timeout
) {
133 r
= parse_sec_fix_0(keydev_timeout
, &timeout_us
);
135 r
= unit_name_from_path(what
, ".device", &device_unit
);
137 return log_error_errno(r
, "Failed to generate unit name: %m");
139 r
= write_drop_in_format(arg_dest
, device_unit
, 90, "device-timeout",
140 "# Automatically generated by systemd-cryptsetup-generator \n\n"
141 "[Unit]\nJobRunningTimeoutSec=%s", keydev_timeout
);
143 return log_error_errno(r
, "Failed to write device drop-in: %m");
146 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", keydev_timeout
);
150 r
= fflush_and_check(f
);
155 *mount
= TAKE_PTR(where
);
160 static int print_dependencies(FILE *f
, const char* device_path
) {
163 if (STR_IN_SET(device_path
, "-", "none"))
164 /* None, nothing to do */
167 if (PATH_IN_SET(device_path
, "/dev/urandom", "/dev/random", "/dev/hw_random")) {
168 /* RNG device, add random dep */
169 fputs("After=systemd-random-seed.service\n", f
);
173 _cleanup_free_
char *udev_node
= fstab_node_to_udev_node(device_path
);
177 if (path_equal(udev_node
, "/dev/null"))
180 if (path_startswith(udev_node
, "/dev/")) {
181 /* We are dealing with a block device, add dependency for correspoding unit */
182 _cleanup_free_
char *unit
= NULL
;
184 r
= unit_name_from_path(udev_node
, ".device", &unit
);
186 return log_error_errno(r
, "Failed to generate unit name: %m");
188 fprintf(f
, "After=%1$s\nRequires=%1$s\n", unit
);
190 /* Regular file, add mount dependency */
191 _cleanup_free_
char *escaped_path
= specifier_escape(device_path
);
195 fprintf(f
, "RequiresMountsFor=%s\n", escaped_path
);
201 static int create_disk(
204 const char *password
,
206 const char *options
) {
208 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
209 *keydev_mount
= NULL
, *keyfile_timeout_value
= NULL
, *password_escaped
= NULL
,
210 *filtered
= NULL
, *u_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
, *header_path
= NULL
;
211 _cleanup_fclose_
FILE *f
= NULL
;
213 bool noauto
, nofail
, tmp
, swap
, netdev
;
214 int r
, detached_header
, keyfile_can_timeout
;
219 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
220 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
221 tmp
= fstab_test_option(options
, "tmp\0");
222 swap
= fstab_test_option(options
, "swap\0");
223 netdev
= fstab_test_option(options
, "_netdev\0");
225 keyfile_can_timeout
= fstab_filter_options(options
, "keyfile-timeout\0", NULL
, &keyfile_timeout_value
, NULL
);
226 if (keyfile_can_timeout
< 0)
227 return log_error_errno(keyfile_can_timeout
, "Failed to parse keyfile-timeout= option value: %m");
229 detached_header
= fstab_filter_options(options
, "header\0", NULL
, &header_path
, NULL
);
230 if (detached_header
< 0)
231 return log_error_errno(detached_header
, "Failed to parse header= option value: %m");
234 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
235 "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
238 name_escaped
= specifier_escape(name
);
242 e
= unit_name_escape(name
);
246 u
= fstab_node_to_udev_node(device
);
250 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
252 return log_error_errno(r
, "Failed to generate unit name: %m");
254 u_escaped
= specifier_escape(u
);
258 r
= unit_name_from_path(u
, ".device", &d
);
260 return log_error_errno(r
, "Failed to generate unit name: %m");
262 if (keydev
&& !password
)
263 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
264 "Key device is specified, but path to the password file is missing.");
266 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
272 "Description=Cryptography Setup for %%I\n"
273 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
274 "SourcePath=/etc/crypttab\n"
275 "DefaultDependencies=no\n"
276 "Conflicts=umount.target\n"
277 "IgnoreOnIsolate=true\n"
279 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
282 password_escaped
= specifier_escape(password
);
283 if (!password_escaped
)
288 _cleanup_free_
char *unit
= NULL
, *p
= NULL
;
290 r
= generate_keydev_mount(name
, keydev
, keyfile_timeout_value
, keyfile_can_timeout
> 0, &unit
, &keydev_mount
);
292 return log_error_errno(r
, "Failed to generate keydev mount unit: %m");
294 p
= path_join(keydev_mount
, password_escaped
);
298 free_and_replace(password_escaped
, p
);
300 fprintf(f
, "After=%s\n", unit
);
301 if (keyfile_can_timeout
> 0)
302 fprintf(f
, "Wants=%s\n", unit
);
304 fprintf(f
, "Requires=%s\n", unit
);
310 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
312 if (password
&& !keydev
) {
313 r
= print_dependencies(f
, password
);
318 /* Check if a header option was specified */
319 if (detached_header
> 0) {
320 r
= print_dependencies(f
, header_path
);
325 if (path_startswith(u
, "/dev/")) {
329 "Before=umount.target\n",
333 fputs("Before=dev-mapper-%i.swap\n",
336 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
337 dependency to ensure that loopback support is available in
338 the kernel (/dev/loop-control needs to exist) */
340 "RequiresMountsFor=%s\n"
341 "Requires=systemd-tmpfiles-setup-dev.service\n"
342 "After=systemd-tmpfiles-setup-dev.service\n",
345 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
350 filtered_escaped
= specifier_escape(filtered
);
351 if (!filtered_escaped
)
358 "RemainAfterExit=yes\n"
359 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
360 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
361 "OOMScoreAdjust=500\n" /* unlocking can allocate a lot of memory if Argon2 is used */
362 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
363 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
364 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
369 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
374 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
379 "ExecStartPost=-" UMOUNT_PATH
" %s\n\n",
382 r
= fflush_and_check(f
);
384 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
387 r
= generator_add_symlink(arg_dest
,
388 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
389 nofail
? "wants" : "requires", n
);
394 dmname
= strjoina("dev-mapper-", e
, ".device");
395 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
399 if (!noauto
&& !nofail
) {
400 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
401 "# Automatically generated by systemd-cryptsetup-generator \n\n"
402 "[Unit]\nJobTimeoutSec=0");
404 return log_error_errno(r
, "Failed to write device drop-in: %m");
410 static crypto_device
* crypt_device_free(crypto_device
*d
) {
422 static crypto_device
*get_crypto_device(const char *uuid
) {
428 d
= hashmap_get(arg_disks
, uuid
);
430 d
= new0(struct crypto_device
, 1);
434 d
->uuid
= strdup(uuid
);
438 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
448 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
449 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
453 if (streq(key
, "luks")) {
455 r
= value
? parse_boolean(value
) : 1;
457 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
461 } else if (streq(key
, "luks.crypttab")) {
463 r
= value
? parse_boolean(value
) : 1;
465 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
467 arg_read_crypttab
= r
;
469 } else if (streq(key
, "luks.uuid")) {
471 if (proc_cmdline_value_missing(key
, value
))
474 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
478 d
->create
= arg_whitelist
= true;
480 } else if (streq(key
, "luks.options")) {
482 if (proc_cmdline_value_missing(key
, value
))
485 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
487 d
= get_crypto_device(uuid
);
491 free_and_replace(d
->options
, uuid_value
);
492 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
495 } else if (streq(key
, "luks.key")) {
497 _cleanup_free_
char *keyfile
= NULL
, *keydev
= NULL
;
500 if (proc_cmdline_value_missing(key
, value
))
503 n
= strspn(value
, LETTERS DIGITS
"-");
504 if (value
[n
] != '=') {
505 if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
510 uuid
= strndup(value
, n
);
514 if (!id128_is_valid(uuid
)) {
515 log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring.");
519 d
= get_crypto_device(uuid
);
523 keyspec
= value
+ n
+ 1;
524 r
= split_keyspec(keyspec
, &keyfile
, &keydev
);
528 free_and_replace(d
->keyfile
, keyfile
);
529 free_and_replace(d
->keydev
, keydev
);
531 } else if (streq(key
, "luks.name")) {
533 if (proc_cmdline_value_missing(key
, value
))
536 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
538 d
= get_crypto_device(uuid
);
542 d
->create
= arg_whitelist
= true;
544 free_and_replace(d
->name
, uuid_value
);
546 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
552 static int add_crypttab_devices(void) {
553 _cleanup_fclose_
FILE *f
= NULL
;
554 unsigned crypttab_line
= 0;
558 if (!arg_read_crypttab
)
561 r
= fopen_unlocked("/etc/crypttab", "re", &f
);
564 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
568 if (fstat(fileno(f
), &st
) < 0) {
569 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
574 _cleanup_free_
char *line
= NULL
, *name
= NULL
, *device
= NULL
, *keyspec
= NULL
, *options
= NULL
, *keyfile
= NULL
, *keydev
= NULL
;
575 crypto_device
*d
= NULL
;
579 r
= read_line(f
, LONG_LINE_MAX
, &line
);
581 return log_error_errno(r
, "Failed to read /etc/crypttab: %m");
588 if (IN_SET(l
[0], 0, '#'))
591 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyspec
, &options
);
592 if (k
< 2 || k
> 4) {
593 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
597 uuid
= startswith(device
, "UUID=");
599 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
601 uuid
= startswith(name
, "luks-");
603 d
= hashmap_get(arg_disks
, uuid
);
605 if (arg_whitelist
&& !d
) {
606 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
610 r
= split_keyspec(keyspec
, &keyfile
, &keydev
);
614 r
= create_disk(name
, device
, keyfile
, keydev
, (d
&& d
->options
) ? d
->options
: options
);
625 static int add_proc_cmdline_devices(void) {
630 HASHMAP_FOREACH(d
, arg_disks
, i
) {
632 _cleanup_free_
char *device
= NULL
;
638 d
->name
= strjoin("luks-", d
->uuid
);
643 device
= strjoin("UUID=", d
->uuid
);
648 options
= d
->options
;
649 else if (arg_default_options
)
650 options
= arg_default_options
;
652 options
= "timeout=0";
654 r
= create_disk(d
->name
, device
, d
->keyfile
?: arg_default_keyfile
, d
->keydev
, options
);
662 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops
, char, string_hash_func
, string_compare_func
,
663 crypto_device
, crypt_device_free
);
665 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
668 assert_se(arg_dest
= dest
);
670 arg_disks
= hashmap_new(&crypt_device_hash_ops
);
674 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
676 return log_warning_errno(r
, "Failed to parse kernel command line: %m");
681 r
= add_crypttab_devices();
685 r
= add_proc_cmdline_devices();
692 DEFINE_MAIN_GENERATOR_FUNCTION(run
);