]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cryptsetup/cryptsetup-generator.c
Merge pull request #10007 from keszybz/fix-no-logind
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e23a0ce8 2
e23a0ce8 3#include <errno.h>
0d536673 4#include <stdio_ext.h>
e23a0ce8 5
b5efdb8a 6#include "alloc-util.h"
0fa9e53d 7#include "dropin.h"
3ffd4af2 8#include "fd-util.h"
0d39fa9c 9#include "fileio.h"
07630cea 10#include "fstab-util.h"
0fa9e53d
JJ
11#include "generator.h"
12#include "hashmap.h"
e23a0ce8 13#include "log.h"
49e942b2 14#include "mkdir.h"
6bedfcbb 15#include "parse-util.h"
bde29068 16#include "path-util.h"
4e731273 17#include "proc-cmdline.h"
98bad05e 18#include "specifier.h"
07630cea 19#include "string-util.h"
0fa9e53d
JJ
20#include "strv.h"
21#include "unit-name.h"
22#include "util.h"
23
24typedef struct crypto_device {
25 char *uuid;
6cd5b12a 26 char *keyfile;
baade8cc 27 char *name;
0fa9e53d
JJ
28 char *options;
29 bool create;
30} crypto_device;
e23a0ce8 31
66a78c2b
LP
32static const char *arg_dest = "/tmp";
33static bool arg_enabled = true;
34static bool arg_read_crypttab = true;
0fa9e53d
JJ
35static bool arg_whitelist = false;
36static Hashmap *arg_disks = NULL;
37static char *arg_default_options = NULL;
38static char *arg_default_keyfile = NULL;
141a79f4 39
e23a0ce8
LP
40static int create_disk(
41 const char *name,
42 const char *device,
43 const char *password,
44 const char *options) {
45
fb883e75 46 _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
98bad05e 47 *filtered = NULL, *u_escaped = NULL, *password_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL;
7fd1b19b 48 _cleanup_fclose_ FILE *f = NULL;
b559616f 49 const char *dmname;
b001ad61 50 bool noauto, nofail, tmp, swap, netdev;
744198e9 51 int r;
e23a0ce8
LP
52
53 assert(name);
54 assert(device);
55
b9f111b9
ZJS
56 noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
57 nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
a6dba978
ZJS
58 tmp = fstab_test_option(options, "tmp\0");
59 swap = fstab_test_option(options, "swap\0");
b001ad61 60 netdev = fstab_test_option(options, "_netdev\0");
8c11d3c1
TG
61
62 if (tmp && swap) {
63 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
64 return -EINVAL;
65 }
155da457 66
98bad05e
LP
67 name_escaped = specifier_escape(name);
68 if (!name_escaped)
69 return log_oom();
70
744198e9
LP
71 e = unit_name_escape(name);
72 if (!e)
73 return log_oom();
74
f7f21d33 75 u = fstab_node_to_udev_node(device);
24a988e9
HH
76 if (!u)
77 return log_oom();
e23a0ce8 78
fb883e75
ZJS
79 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
80 if (r < 0)
81 return log_error_errno(r, "Failed to generate unit name: %m");
82
98bad05e
LP
83 u_escaped = specifier_escape(u);
84 if (!u_escaped)
85 return log_oom();
86
7410616c
LP
87 r = unit_name_from_path(u, ".device", &d);
88 if (r < 0)
89 return log_error_errno(r, "Failed to generate unit name: %m");
e23a0ce8 90
d31eb24f
LP
91 if (password) {
92 password_escaped = specifier_escape(password);
93 if (!password_escaped)
94 return log_oom();
95 }
98bad05e 96
fb883e75
ZJS
97 r = generator_open_unit_file(arg_dest, NULL, n, &f);
98 if (r < 0)
99 return r;
0d536673 100
b001ad61 101 fprintf(f,
b001ad61
ZJS
102 "[Unit]\n"
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"
109 "After=%s\n",
a0dd2097 110 netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target");
e23a0ce8 111
155da457
LP
112 if (!nofail)
113 fprintf(f,
b001ad61
ZJS
114 "Before=%s\n",
115 netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
155da457 116
ceca9501 117 if (password) {
744198e9 118 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
0d536673 119 fputs("After=systemd-random-seed.service\n", f);
b559616f 120 else if (!STR_IN_SET(password, "-", "none")) {
744198e9
LP
121 _cleanup_free_ char *uu;
122
123 uu = fstab_node_to_udev_node(password);
124 if (!uu)
66a5dbdf
DR
125 return log_oom();
126
bde29068 127 if (!path_equal(uu, "/dev/null")) {
744198e9 128
048dd629 129 if (path_startswith(uu, "/dev/")) {
7410616c 130 _cleanup_free_ char *dd = NULL;
66a5dbdf 131
7410616c
LP
132 r = unit_name_from_path(uu, ".device", &dd);
133 if (r < 0)
134 return log_error_errno(r, "Failed to generate unit name: %m");
bde29068
LP
135
136 fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
137 } else
98bad05e 138 fprintf(f, "RequiresMountsFor=%s\n", password_escaped);
bde29068 139 }
66a5dbdf 140 }
ceca9501 141 }
e23a0ce8 142
048dd629 143 if (path_startswith(u, "/dev/")) {
9ece938a
TW
144 fprintf(f,
145 "BindsTo=%s\n"
146 "After=%s\n"
147 "Before=umount.target\n",
148 d, d);
a6f8786a
MFO
149
150 if (swap)
0d536673
LP
151 fputs("Before=dev-mapper-%i.swap\n",
152 f);
a6f8786a 153 } else
b90cbe66
LHS
154 /* For loopback devices, add systemd-tmpfiles-setup-dev.service
155 dependency to ensure that loopback support is available in
156 the kernel (/dev/loop-control needs to exist) */
9ece938a 157 fprintf(f,
b90cbe66
LHS
158 "RequiresMountsFor=%s\n"
159 "Requires=systemd-tmpfiles-setup-dev.service\n"
160 "After=systemd-tmpfiles-setup-dev.service\n",
98bad05e
LP
161 u_escaped);
162
8eea8687
ZJS
163 r = generator_write_timeouts(arg_dest, device, name, options, &filtered);
164 if (r < 0)
165 return r;
166
d31eb24f
LP
167 if (filtered) {
168 filtered_escaped = specifier_escape(filtered);
169 if (!filtered_escaped)
170 return log_oom();
171 }
98bad05e 172
e23a0ce8
LP
173 fprintf(f,
174 "\n[Service]\n"
175 "Type=oneshot\n"
176 "RemainAfterExit=yes\n"
90724929 177 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
0b1f68ac 178 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
260ab287 179 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
7f4e0805 180 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
98bad05e
LP
181 name_escaped, u_escaped, strempty(password_escaped), strempty(filtered_escaped),
182 name_escaped);
e23a0ce8 183
8c11d3c1 184 if (tmp)
e23a0ce8 185 fprintf(f,
50109038 186 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
98bad05e 187 name_escaped);
e23a0ce8 188
8c11d3c1 189 if (swap)
e23a0ce8 190 fprintf(f,
50109038 191 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
98bad05e 192 name_escaped);
e23a0ce8 193
4652c56c
ZJS
194 r = fflush_and_check(f);
195 if (r < 0)
fb883e75 196 return log_error_errno(r, "Failed to write unit file %s: %m", n);
e23a0ce8 197
155da457 198 if (!noauto) {
b559616f
ZJS
199 r = generator_add_symlink(arg_dest, d, "wants", n);
200 if (r < 0)
201 return r;
e23a0ce8 202
b001ad61
ZJS
203 r = generator_add_symlink(arg_dest,
204 netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
b559616f
ZJS
205 nofail ? "wants" : "requires", n);
206 if (r < 0)
207 return r;
f7f21d33 208 }
74715b82 209
b559616f
ZJS
210 dmname = strjoina("dev-mapper-", e, ".device");
211 r = generator_add_symlink(arg_dest, dmname, "requires", n);
212 if (r < 0)
213 return r;
74715b82 214
68395007 215 if (!noauto && !nofail) {
a6fb0dc1 216 r = write_drop_in(arg_dest, dmname, 90, "device-timeout",
8eea8687
ZJS
217 "# Automatically generated by systemd-cryptsetup-generator \n\n"
218 "[Unit]\nJobTimeoutSec=0");
23bbb0de
MS
219 if (r < 0)
220 return log_error_errno(r, "Failed to write device drop-in: %m");
68395007
HH
221 }
222
24a988e9 223 return 0;
e23a0ce8
LP
224}
225
6dd1c368
ZJS
226static void crypt_device_free(crypto_device *d) {
227 free(d->uuid);
228 free(d->keyfile);
229 free(d->name);
230 free(d->options);
231 free(d);
0fa9e53d
JJ
232}
233
234static crypto_device *get_crypto_device(const char *uuid) {
235 int r;
236 crypto_device *d;
237
238 assert(uuid);
239
240 d = hashmap_get(arg_disks, uuid);
241 if (!d) {
242 d = new0(struct crypto_device, 1);
243 if (!d)
244 return NULL;
245
246 d->create = false;
baade8cc 247 d->keyfile = d->options = d->name = NULL;
0fa9e53d
JJ
248
249 d->uuid = strdup(uuid);
6b430fdb
ZJS
250 if (!d->uuid)
251 return mfree(d);
0fa9e53d
JJ
252
253 r = hashmap_put(arg_disks, d->uuid, d);
254 if (r < 0) {
255 free(d->uuid);
6b430fdb 256 return mfree(d);
0fa9e53d
JJ
257 }
258 }
259
260 return d;
261}
262
96287a49 263static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
0fa9e53d 264 _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
1d84ad94
LP
265 crypto_device *d;
266 int r;
66a78c2b 267
1d84ad94 268 if (streq(key, "luks")) {
059cb385 269
1d84ad94 270 r = value ? parse_boolean(value) : 1;
141a79f4 271 if (r < 0)
1d84ad94 272 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
141a79f4
ZJS
273 else
274 arg_enabled = r;
66a78c2b 275
1d84ad94 276 } else if (streq(key, "luks.crypttab")) {
66a78c2b 277
1d84ad94 278 r = value ? parse_boolean(value) : 1;
141a79f4 279 if (r < 0)
1d84ad94 280 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
141a79f4
ZJS
281 else
282 arg_read_crypttab = r;
66a78c2b 283
1d84ad94
LP
284 } else if (streq(key, "luks.uuid")) {
285
286 if (proc_cmdline_value_missing(key, value))
287 return 0;
66a78c2b 288
0fa9e53d
JJ
289 d = get_crypto_device(startswith(value, "luks-") ? value+5 : value);
290 if (!d)
141a79f4 291 return log_oom();
66a78c2b 292
0fa9e53d
JJ
293 d->create = arg_whitelist = true;
294
1d84ad94
LP
295 } else if (streq(key, "luks.options")) {
296
297 if (proc_cmdline_value_missing(key, value))
298 return 0;
66a78c2b 299
0fa9e53d
JJ
300 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
301 if (r == 2) {
302 d = get_crypto_device(uuid);
303 if (!d)
304 return log_oom();
305
1d84ad94 306 free_and_replace(d->options, uuid_value);
0fa9e53d 307 } else if (free_and_strdup(&arg_default_options, value) < 0)
141a79f4 308 return log_oom();
66a78c2b 309
1d84ad94
LP
310 } else if (streq(key, "luks.key")) {
311
312 if (proc_cmdline_value_missing(key, value))
313 return 0;
66a78c2b 314
6cd5b12a
JJ
315 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
316 if (r == 2) {
317 d = get_crypto_device(uuid);
318 if (!d)
319 return log_oom();
320
1d84ad94 321 free_and_replace(d->keyfile, uuid_value);
c802a730 322 } else if (free_and_strdup(&arg_default_keyfile, value) < 0)
141a79f4 323 return log_oom();
7ab064a6 324
1d84ad94
LP
325 } else if (streq(key, "luks.name")) {
326
327 if (proc_cmdline_value_missing(key, value))
328 return 0;
baade8cc
JJ
329
330 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
331 if (r == 2) {
332 d = get_crypto_device(uuid);
333 if (!d)
334 return log_oom();
335
336 d->create = arg_whitelist = true;
337
f9ecfd3b 338 free_and_replace(d->name, uuid_value);
baade8cc
JJ
339 } else
340 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
85013844 341 }
66a78c2b 342
24a988e9 343 return 0;
66a78c2b
LP
344}
345
0fa9e53d
JJ
346static int add_crypttab_devices(void) {
347 struct stat st;
348 unsigned crypttab_line = 0;
7fd1b19b 349 _cleanup_fclose_ FILE *f = NULL;
e23a0ce8 350
0fa9e53d
JJ
351 if (!arg_read_crypttab)
352 return 0;
353
354 f = fopen("/etc/crypttab", "re");
355 if (!f) {
356 if (errno != ENOENT)
357 log_error_errno(errno, "Failed to open /etc/crypttab: %m");
358 return 0;
e23a0ce8
LP
359 }
360
0d536673
LP
361 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
362
0fa9e53d
JJ
363 if (fstat(fileno(f), &st) < 0) {
364 log_error_errno(errno, "Failed to stat /etc/crypttab: %m");
365 return 0;
366 }
e23a0ce8 367
0fa9e53d
JJ
368 for (;;) {
369 int r, k;
370 char line[LINE_MAX], *l, *uuid;
371 crypto_device *d = NULL;
372 _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL;
4c12626c 373
0fa9e53d
JJ
374 if (!fgets(line, sizeof(line), f))
375 break;
66a78c2b 376
0fa9e53d 377 crypttab_line++;
141a79f4 378
0fa9e53d 379 l = strstrip(line);
4c701096 380 if (IN_SET(*l, 0, '#'))
0fa9e53d 381 continue;
66a78c2b 382
0fa9e53d
JJ
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);
386 continue;
387 }
66a78c2b 388
0fa9e53d
JJ
389 uuid = startswith(device, "UUID=");
390 if (!uuid)
391 uuid = path_startswith(device, "/dev/disk/by-uuid/");
392 if (!uuid)
393 uuid = startswith(name, "luks-");
394 if (uuid)
395 d = hashmap_get(arg_disks, uuid);
8973790e 396
0fa9e53d
JJ
397 if (arg_whitelist && !d) {
398 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
399 continue;
8973790e
LP
400 }
401
0fa9e53d
JJ
402 r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options);
403 if (r < 0)
404 return r;
8973790e 405
0fa9e53d
JJ
406 if (d)
407 d->create = false;
408 }
8973790e 409
0fa9e53d
JJ
410 return 0;
411}
66a78c2b 412
0fa9e53d
JJ
413static int add_proc_cmdline_devices(void) {
414 int r;
415 Iterator i;
416 crypto_device *d;
66a78c2b 417
0fa9e53d
JJ
418 HASHMAP_FOREACH(d, arg_disks, i) {
419 const char *options;
baade8cc 420 _cleanup_free_ char *device = NULL;
66a78c2b 421
0fa9e53d
JJ
422 if (!d->create)
423 continue;
e23a0ce8 424
baade8cc
JJ
425 if (!d->name) {
426 d->name = strappend("luks-", d->uuid);
427 if (!d->name)
428 return log_oom();
429 }
e23a0ce8 430
0fa9e53d
JJ
431 device = strappend("UUID=", d->uuid);
432 if (!device)
433 return log_oom();
7ab064a6 434
0fa9e53d
JJ
435 if (d->options)
436 options = d->options;
437 else if (arg_default_options)
438 options = arg_default_options;
439 else
440 options = "timeout=0";
141a79f4 441
baade8cc 442 r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options);
0fa9e53d
JJ
443 if (r < 0)
444 return r;
e23a0ce8
LP
445 }
446
0fa9e53d
JJ
447 return 0;
448}
e23a0ce8 449
0fa9e53d 450int main(int argc, char *argv[]) {
5f4bfe56 451 int r;
e23a0ce8 452
0fa9e53d
JJ
453 if (argc > 1 && argc != 4) {
454 log_error("This program takes three or no arguments.");
455 return EXIT_FAILURE;
456 }
e23a0ce8 457
0fa9e53d
JJ
458 if (argc > 1)
459 arg_dest = argv[1];
e23a0ce8 460
6c347d50
LP
461 log_set_prohibit_ipc(true);
462 log_set_target(LOG_TARGET_AUTO);
0fa9e53d
JJ
463 log_parse_environment();
464 log_open();
e2cb60fa 465
0fa9e53d 466 umask(0022);
e23a0ce8 467
0fa9e53d 468 arg_disks = hashmap_new(&string_hash_ops);
5f4bfe56
LP
469 if (!arg_disks) {
470 r = log_oom();
471 goto finish;
472 }
7ab064a6 473
1d84ad94 474 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
0fa9e53d 475 if (r < 0) {
5f4bfe56
LP
476 log_warning_errno(r, "Failed to parse kernel command line: %m");
477 goto finish;
0fa9e53d 478 }
7ab064a6 479
0fa9e53d 480 if (!arg_enabled) {
5f4bfe56
LP
481 r = 0;
482 goto finish;
e23a0ce8
LP
483 }
484
5f4bfe56
LP
485 r = add_crypttab_devices();
486 if (r < 0)
487 goto finish;
0fa9e53d 488
5f4bfe56
LP
489 r = add_proc_cmdline_devices();
490 if (r < 0)
491 goto finish;
0fa9e53d 492
5f4bfe56 493 r = 0;
141a79f4 494
5f4bfe56 495finish:
6dd1c368 496 hashmap_free_with_destructor(arg_disks, crypt_device_free);
0fa9e53d
JJ
497 free(arg_default_options);
498 free(arg_default_keyfile);
141a79f4 499
5f4bfe56 500 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
e23a0ce8 501}