1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
11 #include "alloc-util.h"
15 #include "fstab-util.h"
16 #include "generator.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
{
37 static const char *arg_dest
= "/tmp";
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 int create_disk(
49 const char *options
) {
51 _cleanup_free_
char *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
52 *filtered
= NULL
, *u_escaped
= NULL
, *password_escaped
= NULL
, *filtered_escaped
= NULL
, *name_escaped
= NULL
;
53 _cleanup_fclose_
FILE *f
= NULL
;
55 bool noauto
, nofail
, tmp
, swap
, netdev
;
61 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
62 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
63 tmp
= fstab_test_option(options
, "tmp\0");
64 swap
= fstab_test_option(options
, "swap\0");
65 netdev
= fstab_test_option(options
, "_netdev\0");
68 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name
);
72 name_escaped
= specifier_escape(name
);
76 e
= unit_name_escape(name
);
80 u
= fstab_node_to_udev_node(device
);
84 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
86 return log_error_errno(r
, "Failed to generate unit name: %m");
88 u_escaped
= specifier_escape(u
);
92 r
= unit_name_from_path(u
, ".device", &d
);
94 return log_error_errno(r
, "Failed to generate unit name: %m");
97 password_escaped
= specifier_escape(password
);
98 if (!password_escaped
)
102 r
= generator_open_unit_file(arg_dest
, NULL
, n
, &f
);
108 "Description=Cryptography Setup for %%I\n"
109 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
110 "SourcePath=/etc/crypttab\n"
111 "DefaultDependencies=no\n"
112 "Conflicts=umount.target\n"
113 "IgnoreOnIsolate=true\n"
115 netdev
? "remote-fs-pre.target" : "cryptsetup-pre.target");
120 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
123 if (STR_IN_SET(password
, "/dev/urandom", "/dev/random", "/dev/hw_random"))
124 fputs("After=systemd-random-seed.service\n", f
);
125 else if (!STR_IN_SET(password
, "-", "none")) {
126 _cleanup_free_
char *uu
;
128 uu
= fstab_node_to_udev_node(password
);
132 if (!path_equal(uu
, "/dev/null")) {
134 if (path_startswith(uu
, "/dev/")) {
135 _cleanup_free_
char *dd
= NULL
;
137 r
= unit_name_from_path(uu
, ".device", &dd
);
139 return log_error_errno(r
, "Failed to generate unit name: %m");
141 fprintf(f
, "After=%1$s\nRequires=%1$s\n", dd
);
143 fprintf(f
, "RequiresMountsFor=%s\n", password_escaped
);
148 if (path_startswith(u
, "/dev/")) {
152 "Before=umount.target\n",
156 fputs("Before=dev-mapper-%i.swap\n",
160 "RequiresMountsFor=%s\n",
163 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
168 filtered_escaped
= specifier_escape(filtered
);
169 if (!filtered_escaped
)
176 "RemainAfterExit=yes\n"
177 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
178 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
179 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
180 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
181 name_escaped
, u_escaped
, strempty(password_escaped
), strempty(filtered_escaped
),
186 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
191 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
194 r
= fflush_and_check(f
);
196 return log_error_errno(r
, "Failed to write unit file %s: %m", n
);
199 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
203 r
= generator_add_symlink(arg_dest
,
204 netdev
? "remote-cryptsetup.target" : "cryptsetup.target",
205 nofail
? "wants" : "requires", n
);
210 dmname
= strjoina("dev-mapper-", e
, ".device");
211 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
215 if (!noauto
&& !nofail
) {
216 r
= write_drop_in(arg_dest
, dmname
, 90, "device-timeout",
217 "# Automatically generated by systemd-cryptsetup-generator \n\n"
218 "[Unit]\nJobTimeoutSec=0");
220 return log_error_errno(r
, "Failed to write device drop-in: %m");
226 static void crypt_device_free(crypto_device
*d
) {
234 static crypto_device
*get_crypto_device(const char *uuid
) {
240 d
= hashmap_get(arg_disks
, uuid
);
242 d
= new0(struct crypto_device
, 1);
247 d
->keyfile
= d
->options
= d
->name
= NULL
;
249 d
->uuid
= strdup(uuid
);
253 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
263 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
264 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
268 if (streq(key
, "luks")) {
270 r
= value
? parse_boolean(value
) : 1;
272 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
276 } else if (streq(key
, "luks.crypttab")) {
278 r
= value
? parse_boolean(value
) : 1;
280 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
282 arg_read_crypttab
= r
;
284 } else if (streq(key
, "luks.uuid")) {
286 if (proc_cmdline_value_missing(key
, value
))
289 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
293 d
->create
= arg_whitelist
= true;
295 } else if (streq(key
, "luks.options")) {
297 if (proc_cmdline_value_missing(key
, value
))
300 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
302 d
= get_crypto_device(uuid
);
306 free_and_replace(d
->options
, uuid_value
);
307 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
310 } else if (streq(key
, "luks.key")) {
312 if (proc_cmdline_value_missing(key
, value
))
315 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
317 d
= get_crypto_device(uuid
);
321 free_and_replace(d
->keyfile
, uuid_value
);
322 } else if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
325 } else if (streq(key
, "luks.name")) {
327 if (proc_cmdline_value_missing(key
, value
))
330 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
332 d
= get_crypto_device(uuid
);
336 d
->create
= arg_whitelist
= true;
338 free_and_replace(d
->name
, uuid_value
);
340 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
346 static int add_crypttab_devices(void) {
348 unsigned crypttab_line
= 0;
349 _cleanup_fclose_
FILE *f
= NULL
;
351 if (!arg_read_crypttab
)
354 f
= fopen("/etc/crypttab", "re");
357 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
361 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
363 if (fstat(fileno(f
), &st
) < 0) {
364 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
370 char line
[LINE_MAX
], *l
, *uuid
;
371 crypto_device
*d
= NULL
;
372 _cleanup_free_
char *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
374 if (!fgets(line
, sizeof(line
), f
))
380 if (IN_SET(*l
, 0, '#'))
383 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
384 if (k
< 2 || k
> 4) {
385 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
389 uuid
= startswith(device
, "UUID=");
391 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
393 uuid
= startswith(name
, "luks-");
395 d
= hashmap_get(arg_disks
, uuid
);
397 if (arg_whitelist
&& !d
) {
398 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
402 r
= create_disk(name
, device
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
413 static int add_proc_cmdline_devices(void) {
418 HASHMAP_FOREACH(d
, arg_disks
, i
) {
420 _cleanup_free_
char *device
= NULL
;
426 d
->name
= strappend("luks-", d
->uuid
);
431 device
= strappend("UUID=", d
->uuid
);
436 options
= d
->options
;
437 else if (arg_default_options
)
438 options
= arg_default_options
;
440 options
= "timeout=0";
442 r
= create_disk(d
->name
, device
, d
->keyfile
?: arg_default_keyfile
, options
);
450 int main(int argc
, char *argv
[]) {
453 if (argc
> 1 && argc
!= 4) {
454 log_error("This program takes three or no arguments.");
461 log_set_prohibit_ipc(true);
462 log_set_target(LOG_TARGET_AUTO
);
463 log_parse_environment();
468 arg_disks
= hashmap_new(&string_hash_ops
);
474 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
476 log_warning_errno(r
, "Failed to parse kernel command line: %m");
485 r
= add_crypttab_devices();
489 r
= add_proc_cmdline_devices();
496 hashmap_free_with_destructor(arg_disks
, crypt_device_free
);
497 free(arg_default_options
);
498 free(arg_default_keyfile
);
500 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;