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