2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
26 #include "fstab-util.h"
27 #include "generator.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "proc-cmdline.h"
34 #include "string-util.h"
36 #include "unit-name.h"
39 typedef struct crypto_device
{
47 static const char *arg_dest
= "/tmp";
48 static bool arg_enabled
= true;
49 static bool arg_read_crypttab
= true;
50 static bool arg_whitelist
= false;
51 static Hashmap
*arg_disks
= NULL
;
52 static char *arg_default_options
= NULL
;
53 static char *arg_default_keyfile
= NULL
;
55 static int create_disk(
59 const char *options
) {
61 _cleanup_free_
char *p
= NULL
, *n
= NULL
, *d
= NULL
, *u
= NULL
, *e
= NULL
,
63 _cleanup_fclose_
FILE *f
= NULL
;
65 bool noauto
, nofail
, tmp
, swap
, netdev
;
71 noauto
= fstab_test_yes_no_option(options
, "noauto\0" "auto\0");
72 nofail
= fstab_test_yes_no_option(options
, "nofail\0" "fail\0");
73 tmp
= fstab_test_option(options
, "tmp\0");
74 swap
= fstab_test_option(options
, "swap\0");
75 netdev
= fstab_test_option(options
, "_netdev\0");
78 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name
);
82 e
= unit_name_escape(name
);
86 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
88 return log_error_errno(r
, "Failed to generate unit name: %m");
90 p
= strjoin(arg_dest
, "/", n
);
94 u
= fstab_node_to_udev_node(device
);
98 r
= unit_name_from_path(u
, ".device", &d
);
100 return log_error_errno(r
, "Failed to generate unit name: %m");
104 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
107 "# Automatically generated by systemd-cryptsetup-generator\n\n"
109 "Description=Cryptography Setup for %%I\n"
110 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
111 "SourcePath=/etc/crypttab\n"
112 "DefaultDependencies=no\n"
113 "Conflicts=umount.target\n"
114 "IgnoreOnIsolate=true\n"
116 netdev
? "remote-cryptsetup-pre.target" : "cryptsetup-pre.target");
121 netdev
? "remote-cryptsetup.target" : "cryptsetup.target");
124 if (STR_IN_SET(password
, "/dev/urandom", "/dev/random", "/dev/hw_random"))
125 fputs_unlocked("After=systemd-random-seed.service\n", f
);
126 else if (!STR_IN_SET(password
, "-", "none")) {
127 _cleanup_free_
char *uu
;
129 uu
= fstab_node_to_udev_node(password
);
133 if (!path_equal(uu
, "/dev/null")) {
135 if (path_startswith(uu
, "/dev/")) {
136 _cleanup_free_
char *dd
= NULL
;
138 r
= unit_name_from_path(uu
, ".device", &dd
);
140 return log_error_errno(r
, "Failed to generate unit name: %m");
142 fprintf(f
, "After=%1$s\nRequires=%1$s\n", dd
);
144 fprintf(f
, "RequiresMountsFor=%s\n", password
);
149 if (path_startswith(u
, "/dev/")) {
153 "Before=umount.target\n",
157 fputs_unlocked("Before=dev-mapper-%i.swap\n",
161 "RequiresMountsFor=%s\n",
164 r
= generator_write_timeouts(arg_dest
, device
, name
, options
, &filtered
);
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
, u
, strempty(password
), strempty(filtered
),
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 file %s: %m", p
);
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 free_arg_disks(void) {
224 while ((d
= hashmap_steal_first(arg_disks
))) {
232 hashmap_free(arg_disks
);
235 static crypto_device
*get_crypto_device(const char *uuid
) {
241 d
= hashmap_get(arg_disks
, uuid
);
243 d
= new0(struct crypto_device
, 1);
248 d
->keyfile
= d
->options
= d
->name
= NULL
;
250 d
->uuid
= strdup(uuid
);
254 r
= hashmap_put(arg_disks
, d
->uuid
, d
);
264 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
265 _cleanup_free_
char *uuid
= NULL
, *uuid_value
= NULL
;
269 if (streq(key
, "luks")) {
271 r
= value
? parse_boolean(value
) : 1;
273 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value
);
277 } else if (streq(key
, "luks.crypttab")) {
279 r
= value
? parse_boolean(value
) : 1;
281 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value
);
283 arg_read_crypttab
= r
;
285 } else if (streq(key
, "luks.uuid")) {
287 if (proc_cmdline_value_missing(key
, value
))
290 d
= get_crypto_device(startswith(value
, "luks-") ? value
+5 : value
);
294 d
->create
= arg_whitelist
= true;
296 } else if (streq(key
, "luks.options")) {
298 if (proc_cmdline_value_missing(key
, value
))
301 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
303 d
= get_crypto_device(uuid
);
307 free_and_replace(d
->options
, uuid_value
);
308 } else if (free_and_strdup(&arg_default_options
, value
) < 0)
311 } else if (streq(key
, "luks.key")) {
313 if (proc_cmdline_value_missing(key
, value
))
316 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
318 d
= get_crypto_device(uuid
);
322 free_and_replace(d
->keyfile
, uuid_value
);
323 } else if (free_and_strdup(&arg_default_keyfile
, value
) < 0)
326 } else if (streq(key
, "luks.name")) {
328 if (proc_cmdline_value_missing(key
, value
))
331 r
= sscanf(value
, "%m[0-9a-fA-F-]=%ms", &uuid
, &uuid_value
);
333 d
= get_crypto_device(uuid
);
337 d
->create
= arg_whitelist
= true;
340 d
->name
= uuid_value
;
343 log_warning("Failed to parse luks name switch %s. Ignoring.", value
);
349 static int add_crypttab_devices(void) {
351 unsigned crypttab_line
= 0;
352 _cleanup_fclose_
FILE *f
= NULL
;
354 if (!arg_read_crypttab
)
357 f
= fopen("/etc/crypttab", "re");
360 log_error_errno(errno
, "Failed to open /etc/crypttab: %m");
364 if (fstat(fileno(f
), &st
) < 0) {
365 log_error_errno(errno
, "Failed to stat /etc/crypttab: %m");
371 char line
[LINE_MAX
], *l
, *uuid
;
372 crypto_device
*d
= NULL
;
373 _cleanup_free_
char *name
= NULL
, *device
= NULL
, *keyfile
= NULL
, *options
= NULL
;
375 if (!fgets(line
, sizeof(line
), f
))
381 if (*l
== '#' || *l
== 0)
384 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &keyfile
, &options
);
385 if (k
< 2 || k
> 4) {
386 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line
);
390 uuid
= startswith(device
, "UUID=");
392 uuid
= path_startswith(device
, "/dev/disk/by-uuid/");
394 uuid
= startswith(name
, "luks-");
396 d
= hashmap_get(arg_disks
, uuid
);
398 if (arg_whitelist
&& !d
) {
399 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name
);
403 r
= create_disk(name
, device
, keyfile
, (d
&& d
->options
) ? d
->options
: options
);
414 static int add_proc_cmdline_devices(void) {
419 HASHMAP_FOREACH(d
, arg_disks
, i
) {
421 _cleanup_free_
char *device
= NULL
;
427 d
->name
= strappend("luks-", d
->uuid
);
432 device
= strappend("UUID=", d
->uuid
);
437 options
= d
->options
;
438 else if (arg_default_options
)
439 options
= arg_default_options
;
441 options
= "timeout=0";
443 r
= create_disk(d
->name
, device
, d
->keyfile
?: arg_default_keyfile
, options
);
451 int main(int argc
, char *argv
[]) {
454 if (argc
> 1 && argc
!= 4) {
455 log_error("This program takes three or no arguments.");
462 log_set_target(LOG_TARGET_SAFE
);
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();
497 free(arg_default_options
);
498 free(arg_default_keyfile
);
500 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;