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