1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
14 #include "fstab-util.h"
15 #include "generator.h"
17 #include "id128-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "proc-cmdline.h"
23 #include "specifier.h"
24 #include "string-util.h"
26 #include "unit-name.h"
29 typedef struct crypto_device
{
38 static const char *arg_dest
= NULL
;
39 static bool arg_enabled
= true;
40 static bool arg_read_crypttab
= true;
41 static bool arg_whitelist
= false;
42 static Hashmap
*arg_disks
= NULL
;
43 static char *arg_default_options
= NULL
;
44 static char *arg_default_keyfile
= NULL
;
46 STATIC_DESTRUCTOR_REGISTER(arg_disks
, hashmap_freep
);
47 STATIC_DESTRUCTOR_REGISTER(arg_default_options
, freep
);
48 STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile
, freep
);
50 static int generate_keydev_mount(const char *name
, const char *keydev
, char **unit
, char **mount
) {
51 _cleanup_free_
char *u
= NULL
, *what
= NULL
, *where
= NULL
, *name_escaped
= NULL
;
52 _cleanup_fclose_
FILE *f
= NULL
;
60 r
= mkdir_parents("/run/systemd/cryptsetup", 0755);
64 r
= mkdir("/run/systemd/cryptsetup", 0700);
65 if (r
< 0 && errno
!= EEXIST
)
68 name_escaped
= cescape(name
);
72 where
= strjoin("/run/systemd/cryptsetup/keydev-", name_escaped
);
76 r
= mkdir(where
, 0700);
77 if (r
< 0 && errno
!= EEXIST
)
80 r
= unit_name_from_path(where
, ".mount", &u
);
84 r
= generator_open_unit_file(arg_dest
, NULL
, u
, &f
);
88 what
= fstab_node_to_udev_node(keydev
);
94 "DefaultDependencies=no\n\n"
98 "Options=ro\n", what
, where
);
100 r
= fflush_and_check(f
);
105 *mount
= TAKE_PTR(where
);
110 static int create_disk(
114 const char *password
,
115 const char *options
) {
117 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
118 *filtered
= NULL
, *u_escaped
= NULL
, *password_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
, *keydev_mount
= NULL
;
119 _cleanup_fclose_
FILE *f
= NULL
;
121 bool noauto
, nofail
, tmp
, swap
, netdev
;
127 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
128 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
129 tmp
= fstab_test_option(options
, "tmp\0");
130 swap
= fstab_test_option(options
, "swap\0");
131 netdev
= fstab_test_option(options
, "_netdev\0");
134 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
135 "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
138 name_escaped
= specifier_escape(name
);
142 e
= unit_name_escape(name
);
146 u
= fstab_node_to_udev_node(device
);
150 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
152 return log_error_errno(r
, "Failed to generate unit name: %m");
154 u_escaped
= specifier_escape(u
);
158 r
= unit_name_from_path(u
, ".device", &d
);
160 return log_error_errno(r
, "Failed to generate unit name: %m");
163 password_escaped
= specifier_escape(password
);
164 if (!password_escaped
)
168 if (keydev
&& !password
)
169 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
170 "Key device is specified, but path to the password file is missing.");
172 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
178 "Description=Cryptography Setup for %%I\n"
179 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
180 "SourcePath=/etc/crypttab\n"
181 "DefaultDependencies=no\n"
182 "Conflicts=umount.target\n"
183 "IgnoreOnIsolate=true\n"
185 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
188 _cleanup_free_
char *unit
= NULL
, *p
= NULL
;
190 r
= generate_keydev_mount(name
, keydev
, &unit
, &keydev_mount
);
192 return log_error_errno(r
, "Failed to generate keydev mount unit: %m");
194 p
= prefix_root(keydev_mount
, password_escaped
);
198 free_and_replace(password_escaped
, p
);
204 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
207 if (PATH_IN_SET(password
, "/dev/urandom", "/dev/random", "/dev/hw_random"))
208 fputs("After=systemd-random-seed.service\n", f
);
209 else if (!STR_IN_SET(password
, "-", "none")) {
210 _cleanup_free_
char *uu
;
212 uu
= fstab_node_to_udev_node(password
);
216 if (!path_equal(uu
, "/dev/null")) {
218 if (path_startswith(uu
, "/dev/")) {
219 _cleanup_free_
char *dd
= NULL
;
221 r
= unit_name_from_path(uu
, ".device", &dd
);
223 return log_error_errno(r
, "Failed to generate unit name: %m");
225 fprintf(f
, "After=%1$s\nRequires=%1$s\n", dd
);
227 fprintf(f
, "RequiresMountsFor=%s\n", password_escaped
);
232 if (path_startswith(u
, "/dev/")) {
236 "Before=umount.target\n",
240 fputs("Before=dev-mapper-%i.swap\n",
243 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
244 dependency to ensure that loopback support is available in
245 the kernel (/dev/loop-control needs to exist) */
247 "RequiresMountsFor=%s\n"
248 "Requires=systemd-tmpfiles-setup-dev.service\n"
249 "After=systemd-tmpfiles-setup-dev.service\n",
252 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
257 filtered_escaped
= specifier_escape(filtered
);
258 if (!filtered_escaped
)
265 "RemainAfterExit=yes\n"
266 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
267 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
268 "OOMScoreAdjust=500\n" /* unlocking can allocate a lot of memory if Argon2 is used */
269 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
270 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
271 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
276 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
281 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
286 "ExecStartPost=" UMOUNT_PATH
" %s\n\n",
289 r
= fflush_and_check(f
);
291 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
294 r
= generator_add_symlink(arg_dest
,
295 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
296 nofail
? "wants" : "requires", n
);
301 dmname
= strjoina("dev-mapper-", e
, ".device");
302 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
306 if (!noauto
&& !nofail
) {
307 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
308 "# Automatically generated by systemd-cryptsetup-generator \n\n"
309 "[Unit]\nJobTimeoutSec=0");
311 return log_error_errno(r
, "Failed to write device drop-in: %m");
317 static crypto_device
* crypt_device_free(crypto_device
*d
) {
329 static crypto_device
*get_crypto_device(const char *uuid
) {
335 d
= hashmap_get(arg_disks
, uuid
);
337 d
= new0(struct crypto_device
, 1);
342 d
->keyfile
= d
->options
= d
->name
= NULL
;
344 d
->uuid
= strdup(uuid
);
348 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
358 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
359 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
363 if (streq(key
, "luks")) {
365 r
= value
? parse_boolean(value
) : 1;
367 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
371 } else if (streq(key
, "luks.crypttab")) {
373 r
= value
? parse_boolean(value
) : 1;
375 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
377 arg_read_crypttab
= r
;
379 } else if (streq(key
, "luks.uuid")) {
381 if (proc_cmdline_value_missing(key
, value
))
384 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
388 d
->create
= arg_whitelist
= true;
390 } else if (streq(key
, "luks.options")) {
392 if (proc_cmdline_value_missing(key
, value
))
395 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
397 d
= get_crypto_device(uuid
);
401 free_and_replace(d
->options
, uuid_value
);
402 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
405 } else if (streq(key
, "luks.key")) {
407 _cleanup_free_
char *keyfile
= NULL
, *keydev
= NULL
;
411 if (proc_cmdline_value_missing(key
, value
))
414 n
= strspn(value
, LETTERS DIGITS
"-");
415 if (value
[n
] != '=') {
416 if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
421 uuid
= strndup(value
, n
);
425 if (!id128_is_valid(uuid
)) {
426 log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring.");
430 d
= get_crypto_device(uuid
);
434 keyspec
= value
+ n
+ 1;
435 c
= strrchr(keyspec
, ':');
438 keyfile
= strdup(keyspec
);
439 keydev
= strdup(c
+ 1);
441 if (!keyfile
|| !keydev
)
444 /* No keydev specified */
445 keyfile
= strdup(keyspec
);
450 free_and_replace(d
->keyfile
, keyfile
);
451 free_and_replace(d
->keydev
, keydev
);
452 } else if (streq(key
, "luks.name")) {
454 if (proc_cmdline_value_missing(key
, value
))
457 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
459 d
= get_crypto_device(uuid
);
463 d
->create
= arg_whitelist
= true;
465 free_and_replace(d
->name
, uuid_value
);
467 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
473 static int add_crypttab_devices(void) {
474 _cleanup_fclose_
FILE *f
= NULL
;
475 unsigned crypttab_line
= 0;
479 if (!arg_read_crypttab
)
482 f
= fopen("/etc/crypttab", "re");
485 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
489 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
491 if (fstat(fileno(f
), &st
) < 0) {
492 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
497 _cleanup_free_
char *line
= NULL
, *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
498 crypto_device
*d
= NULL
;
502 r
= read_line(f
, LONG_LINE_MAX
, &line
);
504 return log_error_errno(r
, "Failed to read /etc/crypttab: %m");
511 if (IN_SET(l
[0], 0, '#'))
514 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
515 if (k
< 2 || k
> 4) {
516 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
520 uuid
= STARTSWITH_SET(device
, "UUID=", "luks-");
522 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
524 d
= hashmap_get(arg_disks
, uuid
);
526 if (arg_whitelist
&& !d
) {
527 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
531 r
= create_disk(name
, device
, NULL
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
542 static int add_proc_cmdline_devices(void) {
547 HASHMAP_FOREACH(d
, arg_disks
, i
) {
549 _cleanup_free_
char *device
= NULL
;
555 d
->name
= strappend("luks-", d
->uuid
);
560 device
= strappend("UUID=", d
->uuid
);
565 options
= d
->options
;
566 else if (arg_default_options
)
567 options
= arg_default_options
;
569 options
= "timeout=0";
571 r
= create_disk(d
->name
, device
, d
->keydev
, d
->keyfile
?: arg_default_keyfile
, options
);
579 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops
, char, string_hash_func
, string_compare_func
,
580 crypto_device
, crypt_device_free
);
582 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
585 assert_se(arg_dest
= dest
);
587 arg_disks
= hashmap_new(&crypt_device_hash_ops
);
591 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
593 return log_warning_errno(r
, "Failed to parse kernel command line: %m");
598 r
= add_crypttab_devices();
602 r
= add_proc_cmdline_devices();
609 DEFINE_MAIN_GENERATOR_FUNCTION(run
);