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