]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-generator.c
Merge pull request #8754 from poettering/sysusers-fix
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stdio_ext.h>
10
11 #include "alloc-util.h"
12 #include "dropin.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fstab-util.h"
16 #include "generator.h"
17 #include "hashmap.h"
18 #include "log.h"
19 #include "mkdir.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"
25 #include "strv.h"
26 #include "unit-name.h"
27 #include "util.h"
28
29 typedef struct crypto_device {
30 char *uuid;
31 char *keyfile;
32 char *name;
33 char *options;
34 bool create;
35 } crypto_device;
36
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;
44
45 static int create_disk(
46 const char *name,
47 const char *device,
48 const char *password,
49 const char *options) {
50
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;
54 const char *dmname;
55 bool noauto, nofail, tmp, swap, netdev;
56 int r;
57
58 assert(name);
59 assert(device);
60
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");
66
67 if (tmp && swap) {
68 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
69 return -EINVAL;
70 }
71
72 name_escaped = specifier_escape(name);
73 if (!name_escaped)
74 return log_oom();
75
76 e = unit_name_escape(name);
77 if (!e)
78 return log_oom();
79
80 u = fstab_node_to_udev_node(device);
81 if (!u)
82 return log_oom();
83
84 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
85 if (r < 0)
86 return log_error_errno(r, "Failed to generate unit name: %m");
87
88 u_escaped = specifier_escape(u);
89 if (!u_escaped)
90 return log_oom();
91
92 r = unit_name_from_path(u, ".device", &d);
93 if (r < 0)
94 return log_error_errno(r, "Failed to generate unit name: %m");
95
96 if (password) {
97 password_escaped = specifier_escape(password);
98 if (!password_escaped)
99 return log_oom();
100 }
101
102 r = generator_open_unit_file(arg_dest, NULL, n, &f);
103 if (r < 0)
104 return r;
105
106 fprintf(f,
107 "[Unit]\n"
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"
114 "After=%s\n",
115 netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target");
116
117 if (!nofail)
118 fprintf(f,
119 "Before=%s\n",
120 netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
121
122 if (password) {
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;
127
128 uu = fstab_node_to_udev_node(password);
129 if (!uu)
130 return log_oom();
131
132 if (!path_equal(uu, "/dev/null")) {
133
134 if (path_startswith(uu, "/dev/")) {
135 _cleanup_free_ char *dd = NULL;
136
137 r = unit_name_from_path(uu, ".device", &dd);
138 if (r < 0)
139 return log_error_errno(r, "Failed to generate unit name: %m");
140
141 fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
142 } else
143 fprintf(f, "RequiresMountsFor=%s\n", password_escaped);
144 }
145 }
146 }
147
148 if (path_startswith(u, "/dev/")) {
149 fprintf(f,
150 "BindsTo=%s\n"
151 "After=%s\n"
152 "Before=umount.target\n",
153 d, d);
154
155 if (swap)
156 fputs("Before=dev-mapper-%i.swap\n",
157 f);
158 } else
159 fprintf(f,
160 "RequiresMountsFor=%s\n",
161 u_escaped);
162
163 r = generator_write_timeouts(arg_dest, device, name, options, &filtered);
164 if (r < 0)
165 return r;
166
167 if (filtered) {
168 filtered_escaped = specifier_escape(filtered);
169 if (!filtered_escaped)
170 return log_oom();
171 }
172
173 fprintf(f,
174 "\n[Service]\n"
175 "Type=oneshot\n"
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),
182 name_escaped);
183
184 if (tmp)
185 fprintf(f,
186 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
187 name_escaped);
188
189 if (swap)
190 fprintf(f,
191 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
192 name_escaped);
193
194 r = fflush_and_check(f);
195 if (r < 0)
196 return log_error_errno(r, "Failed to write unit file %s: %m", n);
197
198 if (!noauto) {
199 r = generator_add_symlink(arg_dest, d, "wants", n);
200 if (r < 0)
201 return r;
202
203 r = generator_add_symlink(arg_dest,
204 netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
205 nofail ? "wants" : "requires", n);
206 if (r < 0)
207 return r;
208 }
209
210 dmname = strjoina("dev-mapper-", e, ".device");
211 r = generator_add_symlink(arg_dest, dmname, "requires", n);
212 if (r < 0)
213 return r;
214
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");
219 if (r < 0)
220 return log_error_errno(r, "Failed to write device drop-in: %m");
221 }
222
223 return 0;
224 }
225
226 static 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);
232 }
233
234 static 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;
247 d->keyfile = d->options = d->name = NULL;
248
249 d->uuid = strdup(uuid);
250 if (!d->uuid)
251 return mfree(d);
252
253 r = hashmap_put(arg_disks, d->uuid, d);
254 if (r < 0) {
255 free(d->uuid);
256 return mfree(d);
257 }
258 }
259
260 return d;
261 }
262
263 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
264 _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
265 crypto_device *d;
266 int r;
267
268 if (streq(key, "luks")) {
269
270 r = value ? parse_boolean(value) : 1;
271 if (r < 0)
272 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
273 else
274 arg_enabled = r;
275
276 } else if (streq(key, "luks.crypttab")) {
277
278 r = value ? parse_boolean(value) : 1;
279 if (r < 0)
280 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
281 else
282 arg_read_crypttab = r;
283
284 } else if (streq(key, "luks.uuid")) {
285
286 if (proc_cmdline_value_missing(key, value))
287 return 0;
288
289 d = get_crypto_device(startswith(value, "luks-") ? value+5 : value);
290 if (!d)
291 return log_oom();
292
293 d->create = arg_whitelist = true;
294
295 } else if (streq(key, "luks.options")) {
296
297 if (proc_cmdline_value_missing(key, value))
298 return 0;
299
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
306 free_and_replace(d->options, uuid_value);
307 } else if (free_and_strdup(&arg_default_options, value) < 0)
308 return log_oom();
309
310 } else if (streq(key, "luks.key")) {
311
312 if (proc_cmdline_value_missing(key, value))
313 return 0;
314
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
321 free_and_replace(d->keyfile, uuid_value);
322 } else if (free_and_strdup(&arg_default_keyfile, value) < 0)
323 return log_oom();
324
325 } else if (streq(key, "luks.name")) {
326
327 if (proc_cmdline_value_missing(key, value))
328 return 0;
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
338 free_and_replace(d->name, uuid_value);
339 } else
340 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
341 }
342
343 return 0;
344 }
345
346 static int add_crypttab_devices(void) {
347 struct stat st;
348 unsigned crypttab_line = 0;
349 _cleanup_fclose_ FILE *f = NULL;
350
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;
359 }
360
361 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
362
363 if (fstat(fileno(f), &st) < 0) {
364 log_error_errno(errno, "Failed to stat /etc/crypttab: %m");
365 return 0;
366 }
367
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;
373
374 if (!fgets(line, sizeof(line), f))
375 break;
376
377 crypttab_line++;
378
379 l = strstrip(line);
380 if (IN_SET(*l, 0, '#'))
381 continue;
382
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 }
388
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);
396
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;
400 }
401
402 r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options);
403 if (r < 0)
404 return r;
405
406 if (d)
407 d->create = false;
408 }
409
410 return 0;
411 }
412
413 static int add_proc_cmdline_devices(void) {
414 int r;
415 Iterator i;
416 crypto_device *d;
417
418 HASHMAP_FOREACH(d, arg_disks, i) {
419 const char *options;
420 _cleanup_free_ char *device = NULL;
421
422 if (!d->create)
423 continue;
424
425 if (!d->name) {
426 d->name = strappend("luks-", d->uuid);
427 if (!d->name)
428 return log_oom();
429 }
430
431 device = strappend("UUID=", d->uuid);
432 if (!device)
433 return log_oom();
434
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";
441
442 r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options);
443 if (r < 0)
444 return r;
445 }
446
447 return 0;
448 }
449
450 int main(int argc, char *argv[]) {
451 int r;
452
453 if (argc > 1 && argc != 4) {
454 log_error("This program takes three or no arguments.");
455 return EXIT_FAILURE;
456 }
457
458 if (argc > 1)
459 arg_dest = argv[1];
460
461 log_set_prohibit_ipc(true);
462 log_set_target(LOG_TARGET_AUTO);
463 log_parse_environment();
464 log_open();
465
466 umask(0022);
467
468 arg_disks = hashmap_new(&string_hash_ops);
469 if (!arg_disks) {
470 r = log_oom();
471 goto finish;
472 }
473
474 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
475 if (r < 0) {
476 log_warning_errno(r, "Failed to parse kernel command line: %m");
477 goto finish;
478 }
479
480 if (!arg_enabled) {
481 r = 0;
482 goto finish;
483 }
484
485 r = add_crypttab_devices();
486 if (r < 0)
487 goto finish;
488
489 r = add_proc_cmdline_devices();
490 if (r < 0)
491 goto finish;
492
493 r = 0;
494
495 finish:
496 hashmap_free_with_destructor(arg_disks, crypt_device_free);
497 free(arg_default_options);
498 free(arg_default_keyfile);
499
500 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
501 }