]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-generator.c
networkd: add PRL options to send when Anonymize is true
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
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
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.
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
14 Lesser General Public License for more details.
15
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/>.
18 ***/
19
20 #include <errno.h>
21
22 #include "alloc-util.h"
23 #include "dropin.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "fstab-util.h"
27 #include "generator.h"
28 #include "hashmap.h"
29 #include "log.h"
30 #include "mkdir.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "proc-cmdline.h"
34 #include "string-util.h"
35 #include "strv.h"
36 #include "unit-name.h"
37 #include "util.h"
38
39 typedef struct crypto_device {
40 char *uuid;
41 char *keyfile;
42 char *name;
43 char *options;
44 bool create;
45 } crypto_device;
46
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;
54
55 static int create_disk(
56 const char *name,
57 const char *device,
58 const char *password,
59 const char *options) {
60
61 _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *e = NULL,
62 *filtered = NULL;
63 _cleanup_fclose_ FILE *f = NULL;
64 const char *dmname;
65 bool noauto, nofail, tmp, swap, netdev;
66 int r;
67
68 assert(name);
69 assert(device);
70
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");
76
77 if (tmp && swap) {
78 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
79 return -EINVAL;
80 }
81
82 e = unit_name_escape(name);
83 if (!e)
84 return log_oom();
85
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");
89
90 p = strjoin(arg_dest, "/", n);
91 if (!p)
92 return log_oom();
93
94 u = fstab_node_to_udev_node(device);
95 if (!u)
96 return log_oom();
97
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");
101
102 f = fopen(p, "wxe");
103 if (!f)
104 return log_error_errno(errno, "Failed to create unit file %s: %m", p);
105
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");
117
118 if (!nofail)
119 fprintf(f,
120 "Before=%s\n",
121 netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
122
123 if (password) {
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;
128
129 uu = fstab_node_to_udev_node(password);
130 if (!uu)
131 return log_oom();
132
133 if (!path_equal(uu, "/dev/null")) {
134
135 if (path_startswith(uu, "/dev/")) {
136 _cleanup_free_ char *dd = NULL;
137
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");
141
142 fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
143 } else
144 fprintf(f, "RequiresMountsFor=%s\n", password);
145 }
146 }
147 }
148
149 if (path_startswith(u, "/dev/")) {
150 fprintf(f,
151 "BindsTo=%s\n"
152 "After=%s\n"
153 "Before=umount.target\n",
154 d, d);
155
156 if (swap)
157 fputs_unlocked("Before=dev-mapper-%i.swap\n",
158 f);
159 } else
160 fprintf(f,
161 "RequiresMountsFor=%s\n",
162 u);
163
164 r = generator_write_timeouts(arg_dest, device, name, options, &filtered);
165 if (r < 0)
166 return r;
167
168 fprintf(f,
169 "\n[Service]\n"
170 "Type=oneshot\n"
171 "RemainAfterExit=yes\n"
172 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
173 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
174 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
175 name, u, strempty(password), strempty(filtered),
176 name);
177
178 if (tmp)
179 fprintf(f,
180 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
181 name);
182
183 if (swap)
184 fprintf(f,
185 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
186 name);
187
188 r = fflush_and_check(f);
189 if (r < 0)
190 return log_error_errno(r, "Failed to write file %s: %m", p);
191
192 if (!noauto) {
193 r = generator_add_symlink(arg_dest, d, "wants", n);
194 if (r < 0)
195 return r;
196
197 r = generator_add_symlink(arg_dest,
198 netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
199 nofail ? "wants" : "requires", n);
200 if (r < 0)
201 return r;
202 }
203
204 dmname = strjoina("dev-mapper-", e, ".device");
205 r = generator_add_symlink(arg_dest, dmname, "requires", n);
206 if (r < 0)
207 return r;
208
209 if (!noauto && !nofail) {
210 r = write_drop_in(arg_dest, dmname, 90, "device-timeout",
211 "# Automatically generated by systemd-cryptsetup-generator \n\n"
212 "[Unit]\nJobTimeoutSec=0");
213 if (r < 0)
214 return log_error_errno(r, "Failed to write device drop-in: %m");
215 }
216
217 return 0;
218 }
219
220 static void free_arg_disks(void) {
221 crypto_device *d;
222
223 while ((d = hashmap_steal_first(arg_disks))) {
224 free(d->uuid);
225 free(d->keyfile);
226 free(d->name);
227 free(d->options);
228 free(d);
229 }
230
231 hashmap_free(arg_disks);
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(d->name);
339 d->name = uuid_value;
340 uuid_value = NULL;
341 } else
342 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
343 }
344
345 return 0;
346 }
347
348 static int add_crypttab_devices(void) {
349 struct stat st;
350 unsigned crypttab_line = 0;
351 _cleanup_fclose_ FILE *f = NULL;
352
353 if (!arg_read_crypttab)
354 return 0;
355
356 f = fopen("/etc/crypttab", "re");
357 if (!f) {
358 if (errno != ENOENT)
359 log_error_errno(errno, "Failed to open /etc/crypttab: %m");
360 return 0;
361 }
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 (*l == '#' || *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_target(LOG_TARGET_SAFE);
462 log_parse_environment();
463 log_open();
464
465 umask(0022);
466
467 arg_disks = hashmap_new(&string_hash_ops);
468 if (!arg_disks) {
469 r = log_oom();
470 goto finish;
471 }
472
473 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
474 if (r < 0) {
475 log_warning_errno(r, "Failed to parse kernel command line: %m");
476 goto finish;
477 }
478
479 if (!arg_enabled) {
480 r = 0;
481 goto finish;
482 }
483
484 r = add_crypttab_devices();
485 if (r < 0)
486 goto finish;
487
488 r = add_proc_cmdline_devices();
489 if (r < 0)
490 goto finish;
491
492 r = 0;
493
494 finish:
495 free_arg_disks();
496 free(arg_default_options);
497 free(arg_default_keyfile);
498
499 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
500 }