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