]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e23a0ce8 | 2 | |
e23a0ce8 | 3 | #include <errno.h> |
ca78ad1d | 4 | #include <fcntl.h> |
ca78ad1d ZJS |
5 | #include <sys/stat.h> |
6 | #include <sys/types.h> | |
e23a0ce8 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
0fa9e53d | 9 | #include "dropin.h" |
7949dfa7 | 10 | #include "escape.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" | |
7949dfa7 | 16 | #include "id128-util.h" |
e23a0ce8 | 17 | #include "log.h" |
49e942b2 | 18 | #include "mkdir.h" |
6bedfcbb | 19 | #include "parse-util.h" |
bde29068 | 20 | #include "path-util.h" |
4e731273 | 21 | #include "proc-cmdline.h" |
98bad05e | 22 | #include "specifier.h" |
07630cea | 23 | #include "string-util.h" |
0fa9e53d JJ |
24 | #include "strv.h" |
25 | #include "unit-name.h" | |
26 | #include "util.h" | |
27 | ||
28 | typedef struct crypto_device { | |
29 | char *uuid; | |
6cd5b12a | 30 | char *keyfile; |
70f5f48e | 31 | char *keydev; |
baade8cc | 32 | char *name; |
0fa9e53d JJ |
33 | char *options; |
34 | bool create; | |
35 | } crypto_device; | |
e23a0ce8 | 36 | |
7a44c7e3 | 37 | static const char *arg_dest = NULL; |
66a78c2b LP |
38 | static bool arg_enabled = true; |
39 | static bool arg_read_crypttab = true; | |
a6c57e74 | 40 | static const char *arg_crypttab = NULL; |
3f5ac303 | 41 | static const char *arg_runtime_directory = NULL; |
6b000af4 | 42 | static bool arg_allow_list = false; |
0fa9e53d JJ |
43 | static Hashmap *arg_disks = NULL; |
44 | static char *arg_default_options = NULL; | |
45 | static char *arg_default_keyfile = NULL; | |
141a79f4 | 46 | |
a4a90e65 YW |
47 | STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep); |
48 | STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep); | |
49 | STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep); | |
50 | ||
f4ea8432 | 51 | static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_keydev) { |
5d2100dc | 52 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; |
f4ea8432 | 53 | const char *c; |
50d2eba2 | 54 | |
f4ea8432 LP |
55 | assert(ret_keyfile); |
56 | assert(ret_keydev); | |
50d2eba2 | 57 | |
fef716b2 ZJS |
58 | if (!keyspec) { |
59 | *ret_keyfile = *ret_keydev = NULL; | |
60 | return 0; | |
61 | } | |
62 | ||
50d2eba2 | 63 | c = strrchr(keyspec, ':'); |
64 | if (c) { | |
32c6237a ZJS |
65 | /* The keydev part has to be either an absolute path to device node (/dev/something, |
66 | * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device | |
67 | * specification starting with LABEL= or similar. The keyfile part has the same syntax. | |
68 | * | |
69 | * Let's try to guess if the second part looks like a keydev specification, or just part of a | |
70 | * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to | |
71 | * an absolute path. If we didn't get an absolute path, assume that it is just part of the | |
72 | * first keyfile argument. */ | |
73 | ||
74 | keydev = fstab_node_to_udev_node(c + 1); | |
75 | if (!keydev) | |
50d2eba2 | 76 | return log_oom(); |
32c6237a ZJS |
77 | |
78 | if (path_is_absolute(keydev)) | |
79 | keyfile = strndup(keyspec, c-keyspec); | |
80 | else { | |
81 | log_debug("Keyspec argument contains a colon, but \"%s\" doesn't look like a device specification.\n" | |
82 | "Assuming that \"%s\" is a single device specification.", | |
83 | c + 1, keyspec); | |
84 | keydev = mfree(keydev); | |
85 | c = NULL; | |
86 | } | |
87 | } | |
88 | ||
89 | if (!c) | |
50d2eba2 | 90 | /* No keydev specified */ |
5d2100dc | 91 | keyfile = strdup(keyspec); |
32c6237a ZJS |
92 | |
93 | if (!keyfile) | |
94 | return log_oom(); | |
50d2eba2 | 95 | |
5d2100dc ZJS |
96 | *ret_keyfile = TAKE_PTR(keyfile); |
97 | *ret_keydev = TAKE_PTR(keydev); | |
50d2eba2 | 98 | |
99 | return 0; | |
100 | } | |
101 | ||
49685fb3 LP |
102 | static int generate_keydev_mount( |
103 | const char *name, | |
104 | const char *keydev, | |
105 | const char *keydev_timeout, | |
106 | bool canfail, | |
107 | char **unit, | |
108 | char **mount) { | |
109 | ||
32c6237a | 110 | _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL; |
70f5f48e MS |
111 | _cleanup_fclose_ FILE *f = NULL; |
112 | int r; | |
50d2eba2 | 113 | usec_t timeout_us; |
70f5f48e MS |
114 | |
115 | assert(name); | |
116 | assert(keydev); | |
117 | assert(unit); | |
118 | assert(mount); | |
119 | ||
3f5ac303 | 120 | r = mkdir_parents(arg_runtime_directory, 0755); |
70f5f48e MS |
121 | if (r < 0) |
122 | return r; | |
123 | ||
3f5ac303 | 124 | r = mkdir(arg_runtime_directory, 0700); |
579875bc MS |
125 | if (r < 0 && errno != EEXIST) |
126 | return -errno; | |
70f5f48e | 127 | |
7949dfa7 MS |
128 | name_escaped = cescape(name); |
129 | if (!name_escaped) | |
130 | return -ENOMEM; | |
131 | ||
3f5ac303 | 132 | where = strjoin(arg_runtime_directory, "/keydev-", name_escaped); |
70f5f48e MS |
133 | if (!where) |
134 | return -ENOMEM; | |
135 | ||
136 | r = mkdir(where, 0700); | |
579875bc MS |
137 | if (r < 0 && errno != EEXIST) |
138 | return -errno; | |
70f5f48e MS |
139 | |
140 | r = unit_name_from_path(where, ".mount", &u); | |
141 | if (r < 0) | |
142 | return r; | |
143 | ||
144 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
145 | if (r < 0) | |
146 | return r; | |
147 | ||
70f5f48e MS |
148 | fprintf(f, |
149 | "[Unit]\n" | |
150 | "DefaultDependencies=no\n\n" | |
151 | "[Mount]\n" | |
152 | "What=%s\n" | |
153 | "Where=%s\n" | |
32c6237a | 154 | "Options=ro%s\n", keydev, where, canfail ? ",nofail" : ""); |
50d2eba2 | 155 | |
156 | if (keydev_timeout) { | |
157 | r = parse_sec_fix_0(keydev_timeout, &timeout_us); | |
158 | if (r >= 0) { | |
32c6237a | 159 | r = unit_name_from_path(keydev, ".device", &device_unit); |
50d2eba2 | 160 | if (r < 0) |
161 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
162 | ||
163 | r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout", | |
164 | "# Automatically generated by systemd-cryptsetup-generator \n\n" | |
165 | "[Unit]\nJobRunningTimeoutSec=%s", keydev_timeout); | |
166 | if (r < 0) | |
167 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
168 | ||
169 | } else | |
170 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", keydev_timeout); | |
171 | ||
172 | } | |
70f5f48e MS |
173 | |
174 | r = fflush_and_check(f); | |
175 | if (r < 0) | |
176 | return r; | |
177 | ||
178 | *unit = TAKE_PTR(u); | |
179 | *mount = TAKE_PTR(where); | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
882f5f42 MS |
184 | static int generate_keydev_umount(const char *name, |
185 | const char *keydev_mount, | |
186 | char **ret_umount_unit) { | |
187 | _cleanup_fclose_ FILE *f = NULL; | |
188 | _cleanup_free_ char *u = NULL, *name_escaped = NULL, *mount = NULL; | |
189 | int r; | |
190 | ||
191 | assert(name); | |
192 | assert(ret_umount_unit); | |
193 | ||
194 | name_escaped = cescape(name); | |
195 | if (!name_escaped) | |
196 | return -ENOMEM; | |
197 | ||
198 | u = strjoin("keydev-", name_escaped, "-umount.service"); | |
199 | if (!u) | |
200 | return -ENOMEM; | |
201 | ||
202 | r = unit_name_from_path(keydev_mount, ".mount", &mount); | |
203 | if (r < 0) | |
204 | return r; | |
205 | ||
206 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
207 | if (r < 0) | |
208 | return r; | |
209 | ||
210 | fprintf(f, | |
211 | "[Unit]\n" | |
212 | "DefaultDependencies=no\n" | |
213 | "After=%s\n\n" | |
214 | "[Service]\n" | |
215 | "ExecStart=-" UMOUNT_PATH " %s\n\n", mount, keydev_mount); | |
216 | ||
217 | r = fflush_and_check(f); | |
218 | if (r < 0) | |
219 | return r; | |
220 | ||
221 | *ret_umount_unit = TAKE_PTR(u); | |
222 | return 0; | |
223 | } | |
224 | ||
38ab1ec9 RS |
225 | static int print_dependencies(FILE *f, const char* device_path) { |
226 | int r; | |
227 | ||
228 | if (STR_IN_SET(device_path, "-", "none")) | |
229 | /* None, nothing to do */ | |
230 | return 0; | |
231 | ||
c6b1d7d1 LP |
232 | if (PATH_IN_SET(device_path, |
233 | "/dev/urandom", | |
234 | "/dev/random", | |
235 | "/dev/hw_random", | |
236 | "/dev/hwrng")) { | |
38ab1ec9 RS |
237 | /* RNG device, add random dep */ |
238 | fputs("After=systemd-random-seed.service\n", f); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path); | |
243 | if (!udev_node) | |
244 | return log_oom(); | |
245 | ||
246 | if (path_equal(udev_node, "/dev/null")) | |
247 | return 0; | |
248 | ||
249 | if (path_startswith(udev_node, "/dev/")) { | |
162392b7 | 250 | /* We are dealing with a block device, add dependency for corresponding unit */ |
38ab1ec9 RS |
251 | _cleanup_free_ char *unit = NULL; |
252 | ||
253 | r = unit_name_from_path(udev_node, ".device", &unit); | |
254 | if (r < 0) | |
255 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
256 | ||
c6b1d7d1 LP |
257 | fprintf(f, |
258 | "After=%1$s\n" | |
259 | "Requires=%1$s\n", unit); | |
38ab1ec9 RS |
260 | } else { |
261 | /* Regular file, add mount dependency */ | |
262 | _cleanup_free_ char *escaped_path = specifier_escape(device_path); | |
263 | if (!escaped_path) | |
264 | return log_oom(); | |
265 | ||
266 | fprintf(f, "RequiresMountsFor=%s\n", escaped_path); | |
267 | } | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
e23a0ce8 LP |
272 | static int create_disk( |
273 | const char *name, | |
274 | const char *device, | |
275 | const char *password, | |
50d2eba2 | 276 | const char *keydev, |
62ca7d3b JL |
277 | const char *options, |
278 | const char *source) { | |
e23a0ce8 | 279 | |
fb883e75 | 280 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
a7e88558 | 281 | *keydev_mount = NULL, *keyfile_timeout_value = NULL, |
53ac130b LP |
282 | *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *password_buffer = NULL, |
283 | *tmp_fstype = NULL; | |
7fd1b19b | 284 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 285 | const char *dmname; |
53ac130b LP |
286 | bool noauto, nofail, swap, netdev, attach_in_initrd; |
287 | int r, detached_header, keyfile_can_timeout, tmp; | |
e23a0ce8 LP |
288 | |
289 | assert(name); | |
290 | assert(device); | |
291 | ||
b9f111b9 ZJS |
292 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
293 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 | 294 | swap = fstab_test_option(options, "swap\0"); |
b001ad61 | 295 | netdev = fstab_test_option(options, "_netdev\0"); |
1dc85eff | 296 | attach_in_initrd = fstab_test_option(options, "x-initrd.attach\0"); |
8c11d3c1 | 297 | |
50d2eba2 | 298 | keyfile_can_timeout = fstab_filter_options(options, "keyfile-timeout\0", NULL, &keyfile_timeout_value, NULL); |
299 | if (keyfile_can_timeout < 0) | |
300 | return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m"); | |
301 | ||
38ab1ec9 RS |
302 | detached_header = fstab_filter_options(options, "header\0", NULL, &header_path, NULL); |
303 | if (detached_header < 0) | |
304 | return log_error_errno(detached_header, "Failed to parse header= option value: %m"); | |
305 | ||
53ac130b LP |
306 | tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL); |
307 | if (tmp < 0) | |
308 | return log_error_errno(tmp, "Failed to parse tmp= option value: %m"); | |
309 | ||
baaa35ad ZJS |
310 | if (tmp && swap) |
311 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
312 | "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", | |
313 | name); | |
155da457 | 314 | |
98bad05e LP |
315 | name_escaped = specifier_escape(name); |
316 | if (!name_escaped) | |
317 | return log_oom(); | |
318 | ||
744198e9 LP |
319 | e = unit_name_escape(name); |
320 | if (!e) | |
321 | return log_oom(); | |
322 | ||
f7f21d33 | 323 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
324 | if (!u) |
325 | return log_oom(); | |
e23a0ce8 | 326 | |
fb883e75 ZJS |
327 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
328 | if (r < 0) | |
329 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
330 | ||
98bad05e LP |
331 | u_escaped = specifier_escape(u); |
332 | if (!u_escaped) | |
333 | return log_oom(); | |
334 | ||
7410616c LP |
335 | r = unit_name_from_path(u, ".device", &d); |
336 | if (r < 0) | |
337 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 338 | |
baaa35ad ZJS |
339 | if (keydev && !password) |
340 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
341 | "Key device is specified, but path to the password file is missing."); | |
70f5f48e | 342 | |
fb883e75 ZJS |
343 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
344 | if (r < 0) | |
345 | return r; | |
0d536673 | 346 | |
62ca7d3b | 347 | r = generator_write_cryptsetup_unit_section(f, source); |
a7e88558 LP |
348 | if (r < 0) |
349 | return r; | |
6bbd539e LP |
350 | |
351 | if (netdev) | |
352 | fprintf(f, "After=remote-fs-pre.target\n"); | |
e23a0ce8 | 353 | |
1dc85eff FB |
354 | /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */ |
355 | if (!attach_in_initrd) | |
356 | fprintf(f, "Conflicts=umount.target\n"); | |
357 | ||
70f5f48e | 358 | if (keydev) { |
882f5f42 | 359 | _cleanup_free_ char *unit = NULL, *umount_unit = NULL; |
70f5f48e | 360 | |
50d2eba2 | 361 | r = generate_keydev_mount(name, keydev, keyfile_timeout_value, keyfile_can_timeout > 0, &unit, &keydev_mount); |
70f5f48e MS |
362 | if (r < 0) |
363 | return log_error_errno(r, "Failed to generate keydev mount unit: %m"); | |
364 | ||
882f5f42 MS |
365 | r = generate_keydev_umount(name, keydev_mount, &umount_unit); |
366 | if (r < 0) | |
367 | return log_error_errno(r, "Failed to generate keydev umount unit: %m"); | |
368 | ||
a7e88558 LP |
369 | password_buffer = path_join(keydev_mount, password); |
370 | if (!password_buffer) | |
70f5f48e MS |
371 | return log_oom(); |
372 | ||
a7e88558 | 373 | password = password_buffer; |
50d2eba2 | 374 | |
375 | fprintf(f, "After=%s\n", unit); | |
376 | if (keyfile_can_timeout > 0) | |
377 | fprintf(f, "Wants=%s\n", unit); | |
378 | else | |
379 | fprintf(f, "Requires=%s\n", unit); | |
882f5f42 MS |
380 | |
381 | if (umount_unit) { | |
382 | fprintf(f, | |
383 | "Wants=%s\n" | |
384 | "Before=%s\n", | |
385 | umount_unit, | |
386 | umount_unit | |
387 | ); | |
388 | } | |
70f5f48e MS |
389 | } |
390 | ||
155da457 LP |
391 | if (!nofail) |
392 | fprintf(f, | |
b001ad61 ZJS |
393 | "Before=%s\n", |
394 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
155da457 | 395 | |
50d2eba2 | 396 | if (password && !keydev) { |
38ab1ec9 RS |
397 | r = print_dependencies(f, password); |
398 | if (r < 0) | |
399 | return r; | |
400 | } | |
bde29068 | 401 | |
38ab1ec9 RS |
402 | /* Check if a header option was specified */ |
403 | if (detached_header > 0) { | |
404 | r = print_dependencies(f, header_path); | |
405 | if (r < 0) | |
406 | return r; | |
ceca9501 | 407 | } |
e23a0ce8 | 408 | |
a7e88558 | 409 | if (path_startswith(u, "/dev/")) |
9ece938a TW |
410 | fprintf(f, |
411 | "BindsTo=%s\n" | |
412 | "After=%s\n" | |
413 | "Before=umount.target\n", | |
414 | d, d); | |
a7e88558 | 415 | else |
b90cbe66 LHS |
416 | /* For loopback devices, add systemd-tmpfiles-setup-dev.service |
417 | dependency to ensure that loopback support is available in | |
418 | the kernel (/dev/loop-control needs to exist) */ | |
9ece938a | 419 | fprintf(f, |
b90cbe66 LHS |
420 | "RequiresMountsFor=%s\n" |
421 | "Requires=systemd-tmpfiles-setup-dev.service\n" | |
422 | "After=systemd-tmpfiles-setup-dev.service\n", | |
98bad05e LP |
423 | u_escaped); |
424 | ||
8eea8687 ZJS |
425 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
426 | if (r < 0) | |
7cecc563 | 427 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); |
8eea8687 | 428 | |
a7e88558 LP |
429 | r = generator_write_cryptsetup_service_section(f, name, u, password, filtered); |
430 | if (r < 0) | |
431 | return r; | |
e23a0ce8 | 432 | |
53ac130b LP |
433 | if (tmp) { |
434 | _cleanup_free_ char *tmp_fstype_escaped = NULL; | |
435 | ||
436 | if (tmp_fstype) { | |
437 | tmp_fstype_escaped = specifier_escape(tmp_fstype); | |
438 | if (!tmp_fstype_escaped) | |
439 | return log_oom(); | |
440 | } | |
441 | ||
e23a0ce8 | 442 | fprintf(f, |
53ac130b LP |
443 | "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n", |
444 | tmp_fstype_escaped ?: "ext4", name_escaped); | |
445 | } | |
e23a0ce8 | 446 | |
8c11d3c1 | 447 | if (swap) |
e23a0ce8 | 448 | fprintf(f, |
db2c56b0 | 449 | "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n", |
98bad05e | 450 | name_escaped); |
e23a0ce8 | 451 | |
4652c56c ZJS |
452 | r = fflush_and_check(f); |
453 | if (r < 0) | |
fb883e75 | 454 | return log_error_errno(r, "Failed to write unit file %s: %m", n); |
e23a0ce8 | 455 | |
155da457 | 456 | if (!noauto) { |
b001ad61 ZJS |
457 | r = generator_add_symlink(arg_dest, |
458 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
b559616f ZJS |
459 | nofail ? "wants" : "requires", n); |
460 | if (r < 0) | |
461 | return r; | |
f7f21d33 | 462 | } |
74715b82 | 463 | |
b559616f ZJS |
464 | dmname = strjoina("dev-mapper-", e, ".device"); |
465 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
466 | if (r < 0) | |
467 | return r; | |
74715b82 | 468 | |
68395007 | 469 | if (!noauto && !nofail) { |
7cecc563 ZJS |
470 | r = write_drop_in(arg_dest, dmname, 40, "device-timeout", |
471 | "# Automatically generated by systemd-cryptsetup-generator\n\n" | |
8eea8687 | 472 | "[Unit]\nJobTimeoutSec=0"); |
23bbb0de | 473 | if (r < 0) |
7cecc563 | 474 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); |
68395007 HH |
475 | } |
476 | ||
24a988e9 | 477 | return 0; |
e23a0ce8 LP |
478 | } |
479 | ||
a4a90e65 YW |
480 | static crypto_device* crypt_device_free(crypto_device *d) { |
481 | if (!d) | |
482 | return NULL; | |
483 | ||
6dd1c368 ZJS |
484 | free(d->uuid); |
485 | free(d->keyfile); | |
70f5f48e | 486 | free(d->keydev); |
6dd1c368 ZJS |
487 | free(d->name); |
488 | free(d->options); | |
a4a90e65 | 489 | return mfree(d); |
0fa9e53d JJ |
490 | } |
491 | ||
492 | static crypto_device *get_crypto_device(const char *uuid) { | |
493 | int r; | |
494 | crypto_device *d; | |
495 | ||
496 | assert(uuid); | |
497 | ||
498 | d = hashmap_get(arg_disks, uuid); | |
499 | if (!d) { | |
500 | d = new0(struct crypto_device, 1); | |
501 | if (!d) | |
502 | return NULL; | |
503 | ||
0fa9e53d | 504 | d->uuid = strdup(uuid); |
6b430fdb ZJS |
505 | if (!d->uuid) |
506 | return mfree(d); | |
0fa9e53d JJ |
507 | |
508 | r = hashmap_put(arg_disks, d->uuid, d); | |
509 | if (r < 0) { | |
510 | free(d->uuid); | |
6b430fdb | 511 | return mfree(d); |
0fa9e53d JJ |
512 | } |
513 | } | |
514 | ||
515 | return d; | |
516 | } | |
517 | ||
96287a49 | 518 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 519 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
520 | crypto_device *d; |
521 | int r; | |
66a78c2b | 522 | |
1d84ad94 | 523 | if (streq(key, "luks")) { |
059cb385 | 524 | |
1d84ad94 | 525 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 526 | if (r < 0) |
1d84ad94 | 527 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
528 | else |
529 | arg_enabled = r; | |
66a78c2b | 530 | |
1d84ad94 | 531 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 532 | |
1d84ad94 | 533 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 534 | if (r < 0) |
1d84ad94 | 535 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
536 | else |
537 | arg_read_crypttab = r; | |
66a78c2b | 538 | |
1d84ad94 LP |
539 | } else if (streq(key, "luks.uuid")) { |
540 | ||
541 | if (proc_cmdline_value_missing(key, value)) | |
542 | return 0; | |
66a78c2b | 543 | |
263a7964 | 544 | d = get_crypto_device(startswith(value, "luks-") ?: value); |
0fa9e53d | 545 | if (!d) |
141a79f4 | 546 | return log_oom(); |
66a78c2b | 547 | |
6b000af4 | 548 | d->create = arg_allow_list = true; |
0fa9e53d | 549 | |
1d84ad94 LP |
550 | } else if (streq(key, "luks.options")) { |
551 | ||
552 | if (proc_cmdline_value_missing(key, value)) | |
553 | return 0; | |
66a78c2b | 554 | |
0fa9e53d JJ |
555 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
556 | if (r == 2) { | |
557 | d = get_crypto_device(uuid); | |
558 | if (!d) | |
559 | return log_oom(); | |
560 | ||
1d84ad94 | 561 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 562 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 563 | return log_oom(); |
66a78c2b | 564 | |
1d84ad94 | 565 | } else if (streq(key, "luks.key")) { |
7949dfa7 MS |
566 | size_t n; |
567 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; | |
7949dfa7 | 568 | const char *keyspec; |
1d84ad94 LP |
569 | |
570 | if (proc_cmdline_value_missing(key, value)) | |
571 | return 0; | |
66a78c2b | 572 | |
7949dfa7 MS |
573 | n = strspn(value, LETTERS DIGITS "-"); |
574 | if (value[n] != '=') { | |
575 | if (free_and_strdup(&arg_default_keyfile, value) < 0) | |
576 | return log_oom(); | |
577 | return 0; | |
578 | } | |
70f5f48e | 579 | |
7949dfa7 MS |
580 | uuid = strndup(value, n); |
581 | if (!uuid) | |
582 | return log_oom(); | |
6cd5b12a | 583 | |
7949dfa7 MS |
584 | if (!id128_is_valid(uuid)) { |
585 | log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring."); | |
586 | return 0; | |
587 | } | |
588 | ||
589 | d = get_crypto_device(uuid); | |
590 | if (!d) | |
591 | return log_oom(); | |
70f5f48e | 592 | |
7949dfa7 | 593 | keyspec = value + n + 1; |
50d2eba2 | 594 | r = split_keyspec(keyspec, &keyfile, &keydev); |
595 | if (r < 0) | |
596 | return r; | |
70f5f48e | 597 | |
7949dfa7 MS |
598 | free_and_replace(d->keyfile, keyfile); |
599 | free_and_replace(d->keydev, keydev); | |
50d2eba2 | 600 | |
1d84ad94 LP |
601 | } else if (streq(key, "luks.name")) { |
602 | ||
603 | if (proc_cmdline_value_missing(key, value)) | |
604 | return 0; | |
baade8cc JJ |
605 | |
606 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
607 | if (r == 2) { | |
608 | d = get_crypto_device(uuid); | |
609 | if (!d) | |
610 | return log_oom(); | |
611 | ||
6b000af4 | 612 | d->create = arg_allow_list = true; |
baade8cc | 613 | |
f9ecfd3b | 614 | free_and_replace(d->name, uuid_value); |
baade8cc JJ |
615 | } else |
616 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 617 | } |
66a78c2b | 618 | |
24a988e9 | 619 | return 0; |
66a78c2b LP |
620 | } |
621 | ||
0fa9e53d | 622 | static int add_crypttab_devices(void) { |
7fd1b19b | 623 | _cleanup_fclose_ FILE *f = NULL; |
b42674a1 | 624 | unsigned crypttab_line = 0; |
b42674a1 | 625 | int r; |
e23a0ce8 | 626 | |
0fa9e53d JJ |
627 | if (!arg_read_crypttab) |
628 | return 0; | |
629 | ||
a6c57e74 | 630 | r = fopen_unlocked(arg_crypttab, "re", &f); |
fdeea3f4 | 631 | if (r < 0) { |
0fa9e53d | 632 | if (errno != ENOENT) |
a6c57e74 | 633 | log_error_errno(errno, "Failed to open %s: %m", arg_crypttab); |
0fa9e53d | 634 | return 0; |
e23a0ce8 LP |
635 | } |
636 | ||
0fa9e53d | 637 | for (;;) { |
fef716b2 | 638 | _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, *keyfile = NULL, *keydev = NULL; |
0fa9e53d | 639 | crypto_device *d = NULL; |
b42674a1 LP |
640 | char *l, *uuid; |
641 | int k; | |
4c12626c | 642 | |
b42674a1 LP |
643 | r = read_line(f, LONG_LINE_MAX, &line); |
644 | if (r < 0) | |
a6c57e74 | 645 | return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); |
b42674a1 | 646 | if (r == 0) |
0fa9e53d | 647 | break; |
66a78c2b | 648 | |
0fa9e53d | 649 | crypttab_line++; |
141a79f4 | 650 | |
0fa9e53d | 651 | l = strstrip(line); |
b42674a1 | 652 | if (IN_SET(l[0], 0, '#')) |
0fa9e53d | 653 | continue; |
66a78c2b | 654 | |
50d2eba2 | 655 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); |
0fa9e53d | 656 | if (k < 2 || k > 4) { |
a6c57e74 | 657 | log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); |
0fa9e53d JJ |
658 | continue; |
659 | } | |
66a78c2b | 660 | |
844d5a87 | 661 | uuid = startswith(device, "UUID="); |
0fa9e53d JJ |
662 | if (!uuid) |
663 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
844d5a87 LR |
664 | if (!uuid) |
665 | uuid = startswith(name, "luks-"); | |
0fa9e53d JJ |
666 | if (uuid) |
667 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 668 | |
6b000af4 | 669 | if (arg_allow_list && !d) { |
0fa9e53d JJ |
670 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); |
671 | continue; | |
8973790e LP |
672 | } |
673 | ||
50d2eba2 | 674 | r = split_keyspec(keyspec, &keyfile, &keydev); |
675 | if (r < 0) | |
676 | return r; | |
677 | ||
62ca7d3b | 678 | r = create_disk(name, device, keyfile, keydev, (d && d->options) ? d->options : options, arg_crypttab); |
0fa9e53d JJ |
679 | if (r < 0) |
680 | return r; | |
8973790e | 681 | |
0fa9e53d JJ |
682 | if (d) |
683 | d->create = false; | |
684 | } | |
8973790e | 685 | |
0fa9e53d JJ |
686 | return 0; |
687 | } | |
66a78c2b | 688 | |
0fa9e53d JJ |
689 | static int add_proc_cmdline_devices(void) { |
690 | int r; | |
0fa9e53d | 691 | crypto_device *d; |
66a78c2b | 692 | |
90e74a66 | 693 | HASHMAP_FOREACH(d, arg_disks) { |
baade8cc | 694 | _cleanup_free_ char *device = NULL; |
66a78c2b | 695 | |
0fa9e53d JJ |
696 | if (!d->create) |
697 | continue; | |
e23a0ce8 | 698 | |
baade8cc | 699 | if (!d->name) { |
b910cc72 | 700 | d->name = strjoin("luks-", d->uuid); |
baade8cc JJ |
701 | if (!d->name) |
702 | return log_oom(); | |
703 | } | |
e23a0ce8 | 704 | |
b910cc72 | 705 | device = strjoin("UUID=", d->uuid); |
0fa9e53d JJ |
706 | if (!device) |
707 | return log_oom(); | |
7ab064a6 | 708 | |
7cecc563 ZJS |
709 | r = create_disk(d->name, |
710 | device, | |
711 | d->keyfile ?: arg_default_keyfile, | |
712 | d->keydev, | |
62ca7d3b JL |
713 | d->options ?: arg_default_options, |
714 | "/proc/cmdline"); | |
0fa9e53d JJ |
715 | if (r < 0) |
716 | return r; | |
e23a0ce8 LP |
717 | } |
718 | ||
0fa9e53d JJ |
719 | return 0; |
720 | } | |
e23a0ce8 | 721 | |
a4a90e65 YW |
722 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, |
723 | crypto_device, crypt_device_free); | |
724 | ||
7a44c7e3 | 725 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
5f4bfe56 | 726 | int r; |
e23a0ce8 | 727 | |
7a44c7e3 | 728 | assert_se(arg_dest = dest); |
e23a0ce8 | 729 | |
a6c57e74 | 730 | arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab"; |
3f5ac303 | 731 | arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup"; |
a6c57e74 | 732 | |
a4a90e65 YW |
733 | arg_disks = hashmap_new(&crypt_device_hash_ops); |
734 | if (!arg_disks) | |
735 | return log_oom(); | |
7ab064a6 | 736 | |
1d84ad94 | 737 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
a4a90e65 YW |
738 | if (r < 0) |
739 | return log_warning_errno(r, "Failed to parse kernel command line: %m"); | |
7ab064a6 | 740 | |
a4a90e65 YW |
741 | if (!arg_enabled) |
742 | return 0; | |
e23a0ce8 | 743 | |
5f4bfe56 LP |
744 | r = add_crypttab_devices(); |
745 | if (r < 0) | |
a4a90e65 | 746 | return r; |
0fa9e53d | 747 | |
5f4bfe56 LP |
748 | r = add_proc_cmdline_devices(); |
749 | if (r < 0) | |
a4a90e65 | 750 | return r; |
141a79f4 | 751 | |
a4a90e65 | 752 | return 0; |
e23a0ce8 | 753 | } |
a4a90e65 | 754 | |
7a44c7e3 | 755 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |