]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-generator.c
Merge pull request #5494 from poettering/run-fixes
[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, *to = NULL, *e = NULL,
62 *filtered = NULL;
63 _cleanup_fclose_ FILE *f = NULL;
64 bool noauto, nofail, tmp, swap;
65 char *from;
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
76 if (tmp && swap) {
77 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
78 return -EINVAL;
79 }
80
81 e = unit_name_escape(name);
82 if (!e)
83 return log_oom();
84
85 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
86 if (r < 0)
87 return log_error_errno(r, "Failed to generate unit name: %m");
88
89 p = strjoin(arg_dest, "/", n);
90 if (!p)
91 return log_oom();
92
93 u = fstab_node_to_udev_node(device);
94 if (!u)
95 return log_oom();
96
97 r = unit_name_from_path(u, ".device", &d);
98 if (r < 0)
99 return log_error_errno(r, "Failed to generate unit name: %m");
100
101 f = fopen(p, "wxe");
102 if (!f)
103 return log_error_errno(errno, "Failed to create unit file %s: %m", p);
104
105 fputs("# Automatically generated by systemd-cryptsetup-generator\n\n"
106 "[Unit]\n"
107 "Description=Cryptography Setup for %I\n"
108 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
109 "SourcePath=/etc/crypttab\n"
110 "DefaultDependencies=no\n"
111 "Conflicts=umount.target\n"
112 "BindsTo=dev-mapper-%i.device\n"
113 "IgnoreOnIsolate=true\n"
114 "After=cryptsetup-pre.target\n",
115 f);
116
117 if (!nofail)
118 fprintf(f,
119 "Before=cryptsetup.target\n");
120
121 if (password) {
122 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
123 fputs("After=systemd-random-seed.service\n", f);
124 else if (!streq(password, "-") && !streq(password, "none")) {
125 _cleanup_free_ char *uu;
126
127 uu = fstab_node_to_udev_node(password);
128 if (!uu)
129 return log_oom();
130
131 if (!path_equal(uu, "/dev/null")) {
132
133 if (is_device_path(uu)) {
134 _cleanup_free_ char *dd = NULL;
135
136 r = unit_name_from_path(uu, ".device", &dd);
137 if (r < 0)
138 return log_error_errno(r, "Failed to generate unit name: %m");
139
140 fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
141 } else
142 fprintf(f, "RequiresMountsFor=%s\n", password);
143 }
144 }
145 }
146
147 if (is_device_path(u)) {
148 fprintf(f,
149 "BindsTo=%s\n"
150 "After=%s\n"
151 "Before=umount.target\n",
152 d, d);
153
154 if (swap)
155 fputs("Before=dev-mapper-%i.swap\n",
156 f);
157 } else
158 fprintf(f,
159 "RequiresMountsFor=%s\n",
160 u);
161
162 r = generator_write_timeouts(arg_dest, device, name, options, &filtered);
163 if (r < 0)
164 return r;
165
166 fprintf(f,
167 "\n[Service]\n"
168 "Type=oneshot\n"
169 "RemainAfterExit=yes\n"
170 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
171 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
172 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
173 name, u, strempty(password), strempty(filtered),
174 name);
175
176 if (tmp)
177 fprintf(f,
178 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
179 name);
180
181 if (swap)
182 fprintf(f,
183 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
184 name);
185
186 r = fflush_and_check(f);
187 if (r < 0)
188 return log_error_errno(r, "Failed to write file %s: %m", p);
189
190 from = strjoina("../", n);
191
192 if (!noauto) {
193
194 to = strjoin(arg_dest, "/", d, ".wants/", n);
195 if (!to)
196 return log_oom();
197
198 mkdir_parents_label(to, 0755);
199 if (symlink(from, to) < 0)
200 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
201
202 free(to);
203 if (!nofail)
204 to = strjoin(arg_dest, "/cryptsetup.target.requires/", n);
205 else
206 to = strjoin(arg_dest, "/cryptsetup.target.wants/", n);
207 if (!to)
208 return log_oom();
209
210 mkdir_parents_label(to, 0755);
211 if (symlink(from, to) < 0)
212 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
213 }
214
215 free(to);
216 to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n);
217 if (!to)
218 return log_oom();
219
220 mkdir_parents_label(to, 0755);
221 if (symlink(from, to) < 0)
222 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
223
224 if (!noauto && !nofail) {
225 _cleanup_free_ char *dmname;
226 dmname = strjoin("dev-mapper-", e, ".device");
227 if (!dmname)
228 return log_oom();
229
230 r = write_drop_in(arg_dest, dmname, 90, "device-timeout",
231 "# Automatically generated by systemd-cryptsetup-generator \n\n"
232 "[Unit]\nJobTimeoutSec=0");
233 if (r < 0)
234 return log_error_errno(r, "Failed to write device drop-in: %m");
235 }
236
237 return 0;
238 }
239
240 static void free_arg_disks(void) {
241 crypto_device *d;
242
243 while ((d = hashmap_steal_first(arg_disks))) {
244 free(d->uuid);
245 free(d->keyfile);
246 free(d->name);
247 free(d->options);
248 free(d);
249 }
250
251 hashmap_free(arg_disks);
252 }
253
254 static crypto_device *get_crypto_device(const char *uuid) {
255 int r;
256 crypto_device *d;
257
258 assert(uuid);
259
260 d = hashmap_get(arg_disks, uuid);
261 if (!d) {
262 d = new0(struct crypto_device, 1);
263 if (!d)
264 return NULL;
265
266 d->create = false;
267 d->keyfile = d->options = d->name = NULL;
268
269 d->uuid = strdup(uuid);
270 if (!d->uuid)
271 return mfree(d);
272
273 r = hashmap_put(arg_disks, d->uuid, d);
274 if (r < 0) {
275 free(d->uuid);
276 return mfree(d);
277 }
278 }
279
280 return d;
281 }
282
283 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
284 _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
285 crypto_device *d;
286 int r;
287
288 if (streq(key, "luks")) {
289
290 r = value ? parse_boolean(value) : 1;
291 if (r < 0)
292 log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
293 else
294 arg_enabled = r;
295
296 } else if (streq(key, "luks.crypttab")) {
297
298 r = value ? parse_boolean(value) : 1;
299 if (r < 0)
300 log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
301 else
302 arg_read_crypttab = r;
303
304 } else if (streq(key, "luks.uuid")) {
305
306 if (proc_cmdline_value_missing(key, value))
307 return 0;
308
309 d = get_crypto_device(startswith(value, "luks-") ? value+5 : value);
310 if (!d)
311 return log_oom();
312
313 d->create = arg_whitelist = true;
314
315 } else if (streq(key, "luks.options")) {
316
317 if (proc_cmdline_value_missing(key, value))
318 return 0;
319
320 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
321 if (r == 2) {
322 d = get_crypto_device(uuid);
323 if (!d)
324 return log_oom();
325
326 free_and_replace(d->options, uuid_value);
327 } else if (free_and_strdup(&arg_default_options, value) < 0)
328 return log_oom();
329
330 } else if (streq(key, "luks.key")) {
331
332 if (proc_cmdline_value_missing(key, value))
333 return 0;
334
335 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
336 if (r == 2) {
337 d = get_crypto_device(uuid);
338 if (!d)
339 return log_oom();
340
341 free_and_replace(d->keyfile, uuid_value);
342 } else if (free_and_strdup(&arg_default_keyfile, value) < 0)
343 return log_oom();
344
345 } else if (streq(key, "luks.name")) {
346
347 if (proc_cmdline_value_missing(key, value))
348 return 0;
349
350 r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
351 if (r == 2) {
352 d = get_crypto_device(uuid);
353 if (!d)
354 return log_oom();
355
356 d->create = arg_whitelist = true;
357
358 free(d->name);
359 d->name = uuid_value;
360 uuid_value = NULL;
361 } else
362 log_warning("Failed to parse luks name switch %s. Ignoring.", value);
363 }
364
365 return 0;
366 }
367
368 static int add_crypttab_devices(void) {
369 struct stat st;
370 unsigned crypttab_line = 0;
371 _cleanup_fclose_ FILE *f = NULL;
372
373 if (!arg_read_crypttab)
374 return 0;
375
376 f = fopen("/etc/crypttab", "re");
377 if (!f) {
378 if (errno != ENOENT)
379 log_error_errno(errno, "Failed to open /etc/crypttab: %m");
380 return 0;
381 }
382
383 if (fstat(fileno(f), &st) < 0) {
384 log_error_errno(errno, "Failed to stat /etc/crypttab: %m");
385 return 0;
386 }
387
388 for (;;) {
389 int r, k;
390 char line[LINE_MAX], *l, *uuid;
391 crypto_device *d = NULL;
392 _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL;
393
394 if (!fgets(line, sizeof(line), f))
395 break;
396
397 crypttab_line++;
398
399 l = strstrip(line);
400 if (*l == '#' || *l == 0)
401 continue;
402
403 k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options);
404 if (k < 2 || k > 4) {
405 log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line);
406 continue;
407 }
408
409 uuid = startswith(device, "UUID=");
410 if (!uuid)
411 uuid = path_startswith(device, "/dev/disk/by-uuid/");
412 if (!uuid)
413 uuid = startswith(name, "luks-");
414 if (uuid)
415 d = hashmap_get(arg_disks, uuid);
416
417 if (arg_whitelist && !d) {
418 log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
419 continue;
420 }
421
422 r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options);
423 if (r < 0)
424 return r;
425
426 if (d)
427 d->create = false;
428 }
429
430 return 0;
431 }
432
433 static int add_proc_cmdline_devices(void) {
434 int r;
435 Iterator i;
436 crypto_device *d;
437
438 HASHMAP_FOREACH(d, arg_disks, i) {
439 const char *options;
440 _cleanup_free_ char *device = NULL;
441
442 if (!d->create)
443 continue;
444
445 if (!d->name) {
446 d->name = strappend("luks-", d->uuid);
447 if (!d->name)
448 return log_oom();
449 }
450
451 device = strappend("UUID=", d->uuid);
452 if (!device)
453 return log_oom();
454
455 if (d->options)
456 options = d->options;
457 else if (arg_default_options)
458 options = arg_default_options;
459 else
460 options = "timeout=0";
461
462 r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options);
463 if (r < 0)
464 return r;
465 }
466
467 return 0;
468 }
469
470 int main(int argc, char *argv[]) {
471 int r;
472
473 if (argc > 1 && argc != 4) {
474 log_error("This program takes three or no arguments.");
475 return EXIT_FAILURE;
476 }
477
478 if (argc > 1)
479 arg_dest = argv[1];
480
481 log_set_target(LOG_TARGET_SAFE);
482 log_parse_environment();
483 log_open();
484
485 umask(0022);
486
487 arg_disks = hashmap_new(&string_hash_ops);
488 if (!arg_disks) {
489 r = log_oom();
490 goto finish;
491 }
492
493 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
494 if (r < 0) {
495 log_warning_errno(r, "Failed to parse kernel command line: %m");
496 goto finish;
497 }
498
499 if (!arg_enabled) {
500 r = 0;
501 goto finish;
502 }
503
504 r = add_crypttab_devices();
505 if (r < 0)
506 goto finish;
507
508 r = add_proc_cmdline_devices();
509 if (r < 0)
510 goto finish;
511
512 r = 0;
513
514 finish:
515 free_arg_disks();
516 free(arg_default_options);
517 free(arg_default_keyfile);
518
519 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
520 }