1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
10 #include "fstab-util.h"
11 #include "generator.h"
15 #include "parse-util.h"
16 #include "path-util.h"
17 #include "proc-cmdline.h"
18 #include "specifier.h"
19 #include "string-util.h"
21 #include "unit-name.h"
24 typedef struct crypto_device
{
32 static const char *arg_dest
= "/tmp";
33 static bool arg_enabled
= true;
34 static bool arg_read_crypttab
= true;
35 static bool arg_whitelist
= false;
36 static Hashmap
*arg_disks
= NULL
;
37 static char *arg_default_options
= NULL
;
38 static char *arg_default_keyfile
= NULL
;
40 static int create_disk(
44 const char *options
) {
46 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
47 *filtered
= NULL
, *u_escaped
= NULL
, *password_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
;
48 _cleanup_fclose_
FILE *f
= NULL
;
50 bool noauto
, nofail
, tmp
, swap
, netdev
;
56 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
57 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
58 tmp
= fstab_test_option(options
, "tmp\0");
59 swap
= fstab_test_option(options
, "swap\0");
60 netdev
= fstab_test_option(options
, "_netdev\0");
63 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name
);
67 name_escaped
= specifier_escape(name
);
71 e
= unit_name_escape(name
);
75 u
= fstab_node_to_udev_node(device
);
79 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
81 return log_error_errno(r
, "Failed to generate unit name: %m");
83 u_escaped
= specifier_escape(u
);
87 r
= unit_name_from_path(u
, ".device", &d
);
89 return log_error_errno(r
, "Failed to generate unit name: %m");
92 password_escaped
= specifier_escape(password
);
93 if (!password_escaped
)
97 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
103 "Description=Cryptography Setup for %%I\n"
104 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
105 "SourcePath=/etc/crypttab\n"
106 "DefaultDependencies=no\n"
107 "Conflicts=umount.target\n"
108 "IgnoreOnIsolate=true\n"
110 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
115 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
118 if (STR_IN_SET(password
, "/dev/urandom", "/dev/random", "/dev/hw_random"))
119 fputs("After=systemd-random-seed.service\n", f
);
120 else if (!STR_IN_SET(password
, "-", "none")) {
121 _cleanup_free_
char *uu
;
123 uu
= fstab_node_to_udev_node(password
);
127 if (!path_equal(uu
, "/dev/null")) {
129 if (path_startswith(uu
, "/dev/")) {
130 _cleanup_free_
char *dd
= NULL
;
132 r
= unit_name_from_path(uu
, ".device", &dd
);
134 return log_error_errno(r
, "Failed to generate unit name: %m");
136 fprintf(f
, "After=%1$s\nRequires=%1$s\n", dd
);
138 fprintf(f
, "RequiresMountsFor=%s\n", password_escaped
);
143 if (path_startswith(u
, "/dev/")) {
147 "Before=umount.target\n",
151 fputs("Before=dev-mapper-%i.swap\n",
155 "RequiresMountsFor=%s\n",
158 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
163 filtered_escaped
= specifier_escape(filtered
);
164 if (!filtered_escaped
)
171 "RemainAfterExit=yes\n"
172 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
173 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
174 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
175 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
176 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
181 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
186 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
189 r
= fflush_and_check(f
);
191 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
194 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
198 r
= generator_add_symlink(arg_dest
,
199 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
200 nofail
? "wants" : "requires", n
);
205 dmname
= strjoina("dev-mapper-", e
, ".device");
206 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
210 if (!noauto
&& !nofail
) {
211 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
212 "# Automatically generated by systemd-cryptsetup-generator \n\n"
213 "[Unit]\nJobTimeoutSec=0");
215 return log_error_errno(r
, "Failed to write device drop-in: %m");
221 static void crypt_device_free(crypto_device
*d
) {
229 static crypto_device
*get_crypto_device(const char *uuid
) {
235 d
= hashmap_get(arg_disks
, uuid
);
237 d
= new0(struct crypto_device
, 1);
242 d
->keyfile
= d
->options
= d
->name
= NULL
;
244 d
->uuid
= strdup(uuid
);
248 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
258 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
259 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
263 if (streq(key
, "luks")) {
265 r
= value
? parse_boolean(value
) : 1;
267 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
271 } else if (streq(key
, "luks.crypttab")) {
273 r
= value
? parse_boolean(value
) : 1;
275 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
277 arg_read_crypttab
= r
;
279 } else if (streq(key
, "luks.uuid")) {
281 if (proc_cmdline_value_missing(key
, value
))
284 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
288 d
->create
= arg_whitelist
= true;
290 } else if (streq(key
, "luks.options")) {
292 if (proc_cmdline_value_missing(key
, value
))
295 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
297 d
= get_crypto_device(uuid
);
301 free_and_replace(d
->options
, uuid_value
);
302 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
305 } else if (streq(key
, "luks.key")) {
307 if (proc_cmdline_value_missing(key
, value
))
310 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
312 d
= get_crypto_device(uuid
);
316 free_and_replace(d
->keyfile
, uuid_value
);
317 } else if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
320 } else if (streq(key
, "luks.name")) {
322 if (proc_cmdline_value_missing(key
, value
))
325 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
327 d
= get_crypto_device(uuid
);
331 d
->create
= arg_whitelist
= true;
333 free_and_replace(d
->name
, uuid_value
);
335 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
341 static int add_crypttab_devices(void) {
343 unsigned crypttab_line
= 0;
344 _cleanup_fclose_
FILE *f
= NULL
;
346 if (!arg_read_crypttab
)
349 f
= fopen("/etc/crypttab", "re");
352 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
356 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
358 if (fstat(fileno(f
), &st
) < 0) {
359 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
365 char line
[LINE_MAX
], *l
, *uuid
;
366 crypto_device
*d
= NULL
;
367 _cleanup_free_
char *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
369 if (!fgets(line
, sizeof(line
), f
))
375 if (IN_SET(*l
, 0, '#'))
378 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
379 if (k
< 2 || k
> 4) {
380 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
384 uuid
= startswith(device
, "UUID=");
386 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
388 uuid
= startswith(name
, "luks-");
390 d
= hashmap_get(arg_disks
, uuid
);
392 if (arg_whitelist
&& !d
) {
393 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
397 r
= create_disk(name
, device
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
408 static int add_proc_cmdline_devices(void) {
413 HASHMAP_FOREACH(d
, arg_disks
, i
) {
415 _cleanup_free_
char *device
= NULL
;
421 d
->name
= strappend("luks-", d
->uuid
);
426 device
= strappend("UUID=", d
->uuid
);
431 options
= d
->options
;
432 else if (arg_default_options
)
433 options
= arg_default_options
;
435 options
= "timeout=0";
437 r
= create_disk(d
->name
, device
, d
->keyfile
?: arg_default_keyfile
, options
);
445 int main(int argc
, char *argv
[]) {
448 if (argc
> 1 && argc
!= 4) {
449 log_error("This program takes three or no arguments.");
456 log_set_prohibit_ipc(true);
457 log_set_target(LOG_TARGET_AUTO
);
458 log_parse_environment();
463 arg_disks
= hashmap_new(&string_hash_ops
);
469 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
471 log_warning_errno(r
, "Failed to parse kernel command line: %m");
480 r
= add_crypttab_devices();
484 r
= add_proc_cmdline_devices();
491 hashmap_free_with_destructor(arg_disks
, crypt_device_free
);
492 free(arg_default_options
);
493 free(arg_default_keyfile
);
495 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;