]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e23a0ce8 | 2 | /*** |
e23a0ce8 | 3 | Copyright 2010 Lennart Poettering |
e23a0ce8 LP |
4 | ***/ |
5 | ||
e23a0ce8 | 6 | #include <errno.h> |
0d536673 | 7 | #include <stdio_ext.h> |
e23a0ce8 | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
0fa9e53d | 10 | #include "dropin.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
0d39fa9c | 12 | #include "fileio.h" |
07630cea | 13 | #include "fstab-util.h" |
0fa9e53d JJ |
14 | #include "generator.h" |
15 | #include "hashmap.h" | |
e23a0ce8 | 16 | #include "log.h" |
49e942b2 | 17 | #include "mkdir.h" |
6bedfcbb | 18 | #include "parse-util.h" |
bde29068 | 19 | #include "path-util.h" |
4e731273 | 20 | #include "proc-cmdline.h" |
98bad05e | 21 | #include "specifier.h" |
07630cea | 22 | #include "string-util.h" |
0fa9e53d JJ |
23 | #include "strv.h" |
24 | #include "unit-name.h" | |
25 | #include "util.h" | |
26 | ||
27 | typedef struct crypto_device { | |
28 | char *uuid; | |
6cd5b12a | 29 | char *keyfile; |
baade8cc | 30 | char *name; |
0fa9e53d JJ |
31 | char *options; |
32 | bool create; | |
33 | } crypto_device; | |
e23a0ce8 | 34 | |
66a78c2b LP |
35 | static const char *arg_dest = "/tmp"; |
36 | static bool arg_enabled = true; | |
37 | static bool arg_read_crypttab = true; | |
0fa9e53d JJ |
38 | static bool arg_whitelist = false; |
39 | static Hashmap *arg_disks = NULL; | |
40 | static char *arg_default_options = NULL; | |
41 | static char *arg_default_keyfile = NULL; | |
141a79f4 | 42 | |
e23a0ce8 LP |
43 | static int create_disk( |
44 | const char *name, | |
45 | const char *device, | |
46 | const char *password, | |
47 | const char *options) { | |
48 | ||
fb883e75 | 49 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
98bad05e | 50 | *filtered = NULL, *u_escaped = NULL, *password_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL; |
7fd1b19b | 51 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 52 | const char *dmname; |
b001ad61 | 53 | bool noauto, nofail, tmp, swap, netdev; |
744198e9 | 54 | int r; |
e23a0ce8 LP |
55 | |
56 | assert(name); | |
57 | assert(device); | |
58 | ||
b9f111b9 ZJS |
59 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
60 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 ZJS |
61 | tmp = fstab_test_option(options, "tmp\0"); |
62 | swap = fstab_test_option(options, "swap\0"); | |
b001ad61 | 63 | netdev = fstab_test_option(options, "_netdev\0"); |
8c11d3c1 TG |
64 | |
65 | if (tmp && swap) { | |
66 | log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name); | |
67 | return -EINVAL; | |
68 | } | |
155da457 | 69 | |
98bad05e LP |
70 | name_escaped = specifier_escape(name); |
71 | if (!name_escaped) | |
72 | return log_oom(); | |
73 | ||
744198e9 LP |
74 | e = unit_name_escape(name); |
75 | if (!e) | |
76 | return log_oom(); | |
77 | ||
f7f21d33 | 78 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
79 | if (!u) |
80 | return log_oom(); | |
e23a0ce8 | 81 | |
fb883e75 ZJS |
82 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
83 | if (r < 0) | |
84 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
85 | ||
98bad05e LP |
86 | u_escaped = specifier_escape(u); |
87 | if (!u_escaped) | |
88 | return log_oom(); | |
89 | ||
7410616c LP |
90 | r = unit_name_from_path(u, ".device", &d); |
91 | if (r < 0) | |
92 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 93 | |
d31eb24f LP |
94 | if (password) { |
95 | password_escaped = specifier_escape(password); | |
96 | if (!password_escaped) | |
97 | return log_oom(); | |
98 | } | |
98bad05e | 99 | |
fb883e75 ZJS |
100 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
101 | if (r < 0) | |
102 | return r; | |
0d536673 | 103 | |
b001ad61 | 104 | fprintf(f, |
b001ad61 ZJS |
105 | "[Unit]\n" |
106 | "Description=Cryptography Setup for %%I\n" | |
107 | "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n" | |
108 | "SourcePath=/etc/crypttab\n" | |
109 | "DefaultDependencies=no\n" | |
110 | "Conflicts=umount.target\n" | |
111 | "IgnoreOnIsolate=true\n" | |
112 | "After=%s\n", | |
a0dd2097 | 113 | netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target"); |
e23a0ce8 | 114 | |
155da457 LP |
115 | if (!nofail) |
116 | fprintf(f, | |
b001ad61 ZJS |
117 | "Before=%s\n", |
118 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
155da457 | 119 | |
ceca9501 | 120 | if (password) { |
744198e9 | 121 | if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random")) |
0d536673 | 122 | fputs("After=systemd-random-seed.service\n", f); |
b559616f | 123 | else if (!STR_IN_SET(password, "-", "none")) { |
744198e9 LP |
124 | _cleanup_free_ char *uu; |
125 | ||
126 | uu = fstab_node_to_udev_node(password); | |
127 | if (!uu) | |
66a5dbdf DR |
128 | return log_oom(); |
129 | ||
bde29068 | 130 | if (!path_equal(uu, "/dev/null")) { |
744198e9 | 131 | |
048dd629 | 132 | if (path_startswith(uu, "/dev/")) { |
7410616c | 133 | _cleanup_free_ char *dd = NULL; |
66a5dbdf | 134 | |
7410616c LP |
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"); | |
bde29068 LP |
138 | |
139 | fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); | |
140 | } else | |
98bad05e | 141 | fprintf(f, "RequiresMountsFor=%s\n", password_escaped); |
bde29068 | 142 | } |
66a5dbdf | 143 | } |
ceca9501 | 144 | } |
e23a0ce8 | 145 | |
048dd629 | 146 | if (path_startswith(u, "/dev/")) { |
9ece938a TW |
147 | fprintf(f, |
148 | "BindsTo=%s\n" | |
149 | "After=%s\n" | |
150 | "Before=umount.target\n", | |
151 | d, d); | |
a6f8786a MFO |
152 | |
153 | if (swap) | |
0d536673 LP |
154 | fputs("Before=dev-mapper-%i.swap\n", |
155 | f); | |
a6f8786a | 156 | } else |
9ece938a TW |
157 | fprintf(f, |
158 | "RequiresMountsFor=%s\n", | |
98bad05e LP |
159 | u_escaped); |
160 | ||
8eea8687 ZJS |
161 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
162 | if (r < 0) | |
163 | return r; | |
164 | ||
d31eb24f LP |
165 | if (filtered) { |
166 | filtered_escaped = specifier_escape(filtered); | |
167 | if (!filtered_escaped) | |
168 | return log_oom(); | |
169 | } | |
98bad05e | 170 | |
e23a0ce8 LP |
171 | fprintf(f, |
172 | "\n[Service]\n" | |
173 | "Type=oneshot\n" | |
174 | "RemainAfterExit=yes\n" | |
90724929 | 175 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
0b1f68ac | 176 | "KeyringMode=shared\n" /* make sure we can share cached keys among instances */ |
260ab287 | 177 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 178 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
98bad05e LP |
179 | name_escaped, u_escaped, strempty(password_escaped), strempty(filtered_escaped), |
180 | name_escaped); | |
e23a0ce8 | 181 | |
8c11d3c1 | 182 | if (tmp) |
e23a0ce8 | 183 | fprintf(f, |
50109038 | 184 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
98bad05e | 185 | name_escaped); |
e23a0ce8 | 186 | |
8c11d3c1 | 187 | if (swap) |
e23a0ce8 | 188 | fprintf(f, |
50109038 | 189 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
98bad05e | 190 | name_escaped); |
e23a0ce8 | 191 | |
4652c56c ZJS |
192 | r = fflush_and_check(f); |
193 | if (r < 0) | |
fb883e75 | 194 | return log_error_errno(r, "Failed to write unit file %s: %m", n); |
e23a0ce8 | 195 | |
155da457 | 196 | if (!noauto) { |
b559616f ZJS |
197 | r = generator_add_symlink(arg_dest, d, "wants", n); |
198 | if (r < 0) | |
199 | return r; | |
e23a0ce8 | 200 | |
b001ad61 ZJS |
201 | r = generator_add_symlink(arg_dest, |
202 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
b559616f ZJS |
203 | nofail ? "wants" : "requires", n); |
204 | if (r < 0) | |
205 | return r; | |
f7f21d33 | 206 | } |
74715b82 | 207 | |
b559616f ZJS |
208 | dmname = strjoina("dev-mapper-", e, ".device"); |
209 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
210 | if (r < 0) | |
211 | return r; | |
74715b82 | 212 | |
68395007 | 213 | if (!noauto && !nofail) { |
a6fb0dc1 | 214 | r = write_drop_in(arg_dest, dmname, 90, "device-timeout", |
8eea8687 ZJS |
215 | "# Automatically generated by systemd-cryptsetup-generator \n\n" |
216 | "[Unit]\nJobTimeoutSec=0"); | |
23bbb0de MS |
217 | if (r < 0) |
218 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
68395007 HH |
219 | } |
220 | ||
24a988e9 | 221 | return 0; |
e23a0ce8 LP |
222 | } |
223 | ||
6dd1c368 ZJS |
224 | static void crypt_device_free(crypto_device *d) { |
225 | free(d->uuid); | |
226 | free(d->keyfile); | |
227 | free(d->name); | |
228 | free(d->options); | |
229 | free(d); | |
0fa9e53d JJ |
230 | } |
231 | ||
232 | static crypto_device *get_crypto_device(const char *uuid) { | |
233 | int r; | |
234 | crypto_device *d; | |
235 | ||
236 | assert(uuid); | |
237 | ||
238 | d = hashmap_get(arg_disks, uuid); | |
239 | if (!d) { | |
240 | d = new0(struct crypto_device, 1); | |
241 | if (!d) | |
242 | return NULL; | |
243 | ||
244 | d->create = false; | |
baade8cc | 245 | d->keyfile = d->options = d->name = NULL; |
0fa9e53d JJ |
246 | |
247 | d->uuid = strdup(uuid); | |
6b430fdb ZJS |
248 | if (!d->uuid) |
249 | return mfree(d); | |
0fa9e53d JJ |
250 | |
251 | r = hashmap_put(arg_disks, d->uuid, d); | |
252 | if (r < 0) { | |
253 | free(d->uuid); | |
6b430fdb | 254 | return mfree(d); |
0fa9e53d JJ |
255 | } |
256 | } | |
257 | ||
258 | return d; | |
259 | } | |
260 | ||
96287a49 | 261 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 262 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
263 | crypto_device *d; |
264 | int r; | |
66a78c2b | 265 | |
1d84ad94 | 266 | if (streq(key, "luks")) { |
059cb385 | 267 | |
1d84ad94 | 268 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 269 | if (r < 0) |
1d84ad94 | 270 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
271 | else |
272 | arg_enabled = r; | |
66a78c2b | 273 | |
1d84ad94 | 274 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 275 | |
1d84ad94 | 276 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 277 | if (r < 0) |
1d84ad94 | 278 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
279 | else |
280 | arg_read_crypttab = r; | |
66a78c2b | 281 | |
1d84ad94 LP |
282 | } else if (streq(key, "luks.uuid")) { |
283 | ||
284 | if (proc_cmdline_value_missing(key, value)) | |
285 | return 0; | |
66a78c2b | 286 | |
0fa9e53d JJ |
287 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
288 | if (!d) | |
141a79f4 | 289 | return log_oom(); |
66a78c2b | 290 | |
0fa9e53d JJ |
291 | d->create = arg_whitelist = true; |
292 | ||
1d84ad94 LP |
293 | } else if (streq(key, "luks.options")) { |
294 | ||
295 | if (proc_cmdline_value_missing(key, value)) | |
296 | return 0; | |
66a78c2b | 297 | |
0fa9e53d JJ |
298 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
299 | if (r == 2) { | |
300 | d = get_crypto_device(uuid); | |
301 | if (!d) | |
302 | return log_oom(); | |
303 | ||
1d84ad94 | 304 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 305 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 306 | return log_oom(); |
66a78c2b | 307 | |
1d84ad94 LP |
308 | } else if (streq(key, "luks.key")) { |
309 | ||
310 | if (proc_cmdline_value_missing(key, value)) | |
311 | return 0; | |
66a78c2b | 312 | |
6cd5b12a JJ |
313 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
314 | if (r == 2) { | |
315 | d = get_crypto_device(uuid); | |
316 | if (!d) | |
317 | return log_oom(); | |
318 | ||
1d84ad94 | 319 | free_and_replace(d->keyfile, uuid_value); |
c802a730 | 320 | } else if (free_and_strdup(&arg_default_keyfile, value) < 0) |
141a79f4 | 321 | return log_oom(); |
7ab064a6 | 322 | |
1d84ad94 LP |
323 | } else if (streq(key, "luks.name")) { |
324 | ||
325 | if (proc_cmdline_value_missing(key, value)) | |
326 | return 0; | |
baade8cc JJ |
327 | |
328 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
329 | if (r == 2) { | |
330 | d = get_crypto_device(uuid); | |
331 | if (!d) | |
332 | return log_oom(); | |
333 | ||
334 | d->create = arg_whitelist = true; | |
335 | ||
f9ecfd3b | 336 | free_and_replace(d->name, uuid_value); |
baade8cc JJ |
337 | } else |
338 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 339 | } |
66a78c2b | 340 | |
24a988e9 | 341 | return 0; |
66a78c2b LP |
342 | } |
343 | ||
0fa9e53d JJ |
344 | static int add_crypttab_devices(void) { |
345 | struct stat st; | |
346 | unsigned crypttab_line = 0; | |
7fd1b19b | 347 | _cleanup_fclose_ FILE *f = NULL; |
e23a0ce8 | 348 | |
0fa9e53d JJ |
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; | |
e23a0ce8 LP |
357 | } |
358 | ||
0d536673 LP |
359 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
360 | ||
0fa9e53d JJ |
361 | if (fstat(fileno(f), &st) < 0) { |
362 | log_error_errno(errno, "Failed to stat /etc/crypttab: %m"); | |
363 | return 0; | |
364 | } | |
e23a0ce8 | 365 | |
0fa9e53d JJ |
366 | for (;;) { |
367 | int r, k; | |
368 | char line[LINE_MAX], *l, *uuid; | |
369 | crypto_device *d = NULL; | |
370 | _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL; | |
4c12626c | 371 | |
0fa9e53d JJ |
372 | if (!fgets(line, sizeof(line), f)) |
373 | break; | |
66a78c2b | 374 | |
0fa9e53d | 375 | crypttab_line++; |
141a79f4 | 376 | |
0fa9e53d | 377 | l = strstrip(line); |
4c701096 | 378 | if (IN_SET(*l, 0, '#')) |
0fa9e53d | 379 | continue; |
66a78c2b | 380 | |
0fa9e53d JJ |
381 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options); |
382 | if (k < 2 || k > 4) { | |
383 | log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line); | |
384 | continue; | |
385 | } | |
66a78c2b | 386 | |
0fa9e53d JJ |
387 | uuid = startswith(device, "UUID="); |
388 | if (!uuid) | |
389 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
390 | if (!uuid) | |
391 | uuid = startswith(name, "luks-"); | |
392 | if (uuid) | |
393 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 394 | |
0fa9e53d JJ |
395 | if (arg_whitelist && !d) { |
396 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
397 | continue; | |
8973790e LP |
398 | } |
399 | ||
0fa9e53d JJ |
400 | r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options); |
401 | if (r < 0) | |
402 | return r; | |
8973790e | 403 | |
0fa9e53d JJ |
404 | if (d) |
405 | d->create = false; | |
406 | } | |
8973790e | 407 | |
0fa9e53d JJ |
408 | return 0; |
409 | } | |
66a78c2b | 410 | |
0fa9e53d JJ |
411 | static int add_proc_cmdline_devices(void) { |
412 | int r; | |
413 | Iterator i; | |
414 | crypto_device *d; | |
66a78c2b | 415 | |
0fa9e53d JJ |
416 | HASHMAP_FOREACH(d, arg_disks, i) { |
417 | const char *options; | |
baade8cc | 418 | _cleanup_free_ char *device = NULL; |
66a78c2b | 419 | |
0fa9e53d JJ |
420 | if (!d->create) |
421 | continue; | |
e23a0ce8 | 422 | |
baade8cc JJ |
423 | if (!d->name) { |
424 | d->name = strappend("luks-", d->uuid); | |
425 | if (!d->name) | |
426 | return log_oom(); | |
427 | } | |
e23a0ce8 | 428 | |
0fa9e53d JJ |
429 | device = strappend("UUID=", d->uuid); |
430 | if (!device) | |
431 | return log_oom(); | |
7ab064a6 | 432 | |
0fa9e53d JJ |
433 | if (d->options) |
434 | options = d->options; | |
435 | else if (arg_default_options) | |
436 | options = arg_default_options; | |
437 | else | |
438 | options = "timeout=0"; | |
141a79f4 | 439 | |
baade8cc | 440 | r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options); |
0fa9e53d JJ |
441 | if (r < 0) |
442 | return r; | |
e23a0ce8 LP |
443 | } |
444 | ||
0fa9e53d JJ |
445 | return 0; |
446 | } | |
e23a0ce8 | 447 | |
0fa9e53d | 448 | int main(int argc, char *argv[]) { |
5f4bfe56 | 449 | int r; |
e23a0ce8 | 450 | |
0fa9e53d JJ |
451 | if (argc > 1 && argc != 4) { |
452 | log_error("This program takes three or no arguments."); | |
453 | return EXIT_FAILURE; | |
454 | } | |
e23a0ce8 | 455 | |
0fa9e53d JJ |
456 | if (argc > 1) |
457 | arg_dest = argv[1]; | |
e23a0ce8 | 458 | |
6c347d50 LP |
459 | log_set_prohibit_ipc(true); |
460 | log_set_target(LOG_TARGET_AUTO); | |
0fa9e53d JJ |
461 | log_parse_environment(); |
462 | log_open(); | |
e2cb60fa | 463 | |
0fa9e53d | 464 | umask(0022); |
e23a0ce8 | 465 | |
0fa9e53d | 466 | arg_disks = hashmap_new(&string_hash_ops); |
5f4bfe56 LP |
467 | if (!arg_disks) { |
468 | r = log_oom(); | |
469 | goto finish; | |
470 | } | |
7ab064a6 | 471 | |
1d84ad94 | 472 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
0fa9e53d | 473 | if (r < 0) { |
5f4bfe56 LP |
474 | log_warning_errno(r, "Failed to parse kernel command line: %m"); |
475 | goto finish; | |
0fa9e53d | 476 | } |
7ab064a6 | 477 | |
0fa9e53d | 478 | if (!arg_enabled) { |
5f4bfe56 LP |
479 | r = 0; |
480 | goto finish; | |
e23a0ce8 LP |
481 | } |
482 | ||
5f4bfe56 LP |
483 | r = add_crypttab_devices(); |
484 | if (r < 0) | |
485 | goto finish; | |
0fa9e53d | 486 | |
5f4bfe56 LP |
487 | r = add_proc_cmdline_devices(); |
488 | if (r < 0) | |
489 | goto finish; | |
0fa9e53d | 490 | |
5f4bfe56 | 491 | r = 0; |
141a79f4 | 492 | |
5f4bfe56 | 493 | finish: |
6dd1c368 | 494 | hashmap_free_with_destructor(arg_disks, crypt_device_free); |
0fa9e53d JJ |
495 | free(arg_default_options); |
496 | free(arg_default_keyfile); | |
141a79f4 | 497 | |
5f4bfe56 | 498 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
e23a0ce8 | 499 | } |