]>
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 | ||
38ab1ec9 RS |
184 | static int print_dependencies(FILE *f, const char* device_path) { |
185 | int r; | |
186 | ||
187 | if (STR_IN_SET(device_path, "-", "none")) | |
188 | /* None, nothing to do */ | |
189 | return 0; | |
190 | ||
c6b1d7d1 LP |
191 | if (PATH_IN_SET(device_path, |
192 | "/dev/urandom", | |
193 | "/dev/random", | |
194 | "/dev/hw_random", | |
195 | "/dev/hwrng")) { | |
38ab1ec9 RS |
196 | /* RNG device, add random dep */ |
197 | fputs("After=systemd-random-seed.service\n", f); | |
198 | return 0; | |
199 | } | |
200 | ||
201 | _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path); | |
202 | if (!udev_node) | |
203 | return log_oom(); | |
204 | ||
205 | if (path_equal(udev_node, "/dev/null")) | |
206 | return 0; | |
207 | ||
208 | if (path_startswith(udev_node, "/dev/")) { | |
162392b7 | 209 | /* We are dealing with a block device, add dependency for corresponding unit */ |
38ab1ec9 RS |
210 | _cleanup_free_ char *unit = NULL; |
211 | ||
212 | r = unit_name_from_path(udev_node, ".device", &unit); | |
213 | if (r < 0) | |
214 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
215 | ||
c6b1d7d1 LP |
216 | fprintf(f, |
217 | "After=%1$s\n" | |
218 | "Requires=%1$s\n", unit); | |
38ab1ec9 RS |
219 | } else { |
220 | /* Regular file, add mount dependency */ | |
221 | _cleanup_free_ char *escaped_path = specifier_escape(device_path); | |
222 | if (!escaped_path) | |
223 | return log_oom(); | |
224 | ||
225 | fprintf(f, "RequiresMountsFor=%s\n", escaped_path); | |
226 | } | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
e23a0ce8 LP |
231 | static int create_disk( |
232 | const char *name, | |
233 | const char *device, | |
234 | const char *password, | |
50d2eba2 | 235 | const char *keydev, |
e23a0ce8 LP |
236 | const char *options) { |
237 | ||
fb883e75 | 238 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
a7e88558 | 239 | *keydev_mount = NULL, *keyfile_timeout_value = NULL, |
53ac130b LP |
240 | *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *password_buffer = NULL, |
241 | *tmp_fstype = NULL; | |
7fd1b19b | 242 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 243 | const char *dmname; |
53ac130b LP |
244 | bool noauto, nofail, swap, netdev, attach_in_initrd; |
245 | int r, detached_header, keyfile_can_timeout, tmp; | |
e23a0ce8 LP |
246 | |
247 | assert(name); | |
248 | assert(device); | |
249 | ||
b9f111b9 ZJS |
250 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
251 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 | 252 | swap = fstab_test_option(options, "swap\0"); |
b001ad61 | 253 | netdev = fstab_test_option(options, "_netdev\0"); |
1dc85eff | 254 | attach_in_initrd = fstab_test_option(options, "x-initrd.attach\0"); |
8c11d3c1 | 255 | |
50d2eba2 | 256 | keyfile_can_timeout = fstab_filter_options(options, "keyfile-timeout\0", NULL, &keyfile_timeout_value, NULL); |
257 | if (keyfile_can_timeout < 0) | |
258 | return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m"); | |
259 | ||
38ab1ec9 RS |
260 | detached_header = fstab_filter_options(options, "header\0", NULL, &header_path, NULL); |
261 | if (detached_header < 0) | |
262 | return log_error_errno(detached_header, "Failed to parse header= option value: %m"); | |
263 | ||
53ac130b LP |
264 | tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL); |
265 | if (tmp < 0) | |
266 | return log_error_errno(tmp, "Failed to parse tmp= option value: %m"); | |
267 | ||
baaa35ad ZJS |
268 | if (tmp && swap) |
269 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
270 | "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", | |
271 | name); | |
155da457 | 272 | |
98bad05e LP |
273 | name_escaped = specifier_escape(name); |
274 | if (!name_escaped) | |
275 | return log_oom(); | |
276 | ||
744198e9 LP |
277 | e = unit_name_escape(name); |
278 | if (!e) | |
279 | return log_oom(); | |
280 | ||
f7f21d33 | 281 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
282 | if (!u) |
283 | return log_oom(); | |
e23a0ce8 | 284 | |
fb883e75 ZJS |
285 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
286 | if (r < 0) | |
287 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
288 | ||
98bad05e LP |
289 | u_escaped = specifier_escape(u); |
290 | if (!u_escaped) | |
291 | return log_oom(); | |
292 | ||
7410616c LP |
293 | r = unit_name_from_path(u, ".device", &d); |
294 | if (r < 0) | |
295 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 296 | |
baaa35ad ZJS |
297 | if (keydev && !password) |
298 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
299 | "Key device is specified, but path to the password file is missing."); | |
70f5f48e | 300 | |
fb883e75 ZJS |
301 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
302 | if (r < 0) | |
303 | return r; | |
0d536673 | 304 | |
a7e88558 LP |
305 | r = generator_write_cryptsetup_unit_section(f, arg_crypttab); |
306 | if (r < 0) | |
307 | return r; | |
6bbd539e LP |
308 | |
309 | if (netdev) | |
310 | fprintf(f, "After=remote-fs-pre.target\n"); | |
e23a0ce8 | 311 | |
1dc85eff FB |
312 | /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */ |
313 | if (!attach_in_initrd) | |
314 | fprintf(f, "Conflicts=umount.target\n"); | |
315 | ||
70f5f48e | 316 | if (keydev) { |
a7e88558 | 317 | _cleanup_free_ char *unit = NULL; |
70f5f48e | 318 | |
50d2eba2 | 319 | r = generate_keydev_mount(name, keydev, keyfile_timeout_value, keyfile_can_timeout > 0, &unit, &keydev_mount); |
70f5f48e MS |
320 | if (r < 0) |
321 | return log_error_errno(r, "Failed to generate keydev mount unit: %m"); | |
322 | ||
a7e88558 LP |
323 | password_buffer = path_join(keydev_mount, password); |
324 | if (!password_buffer) | |
70f5f48e MS |
325 | return log_oom(); |
326 | ||
a7e88558 | 327 | password = password_buffer; |
50d2eba2 | 328 | |
329 | fprintf(f, "After=%s\n", unit); | |
330 | if (keyfile_can_timeout > 0) | |
331 | fprintf(f, "Wants=%s\n", unit); | |
332 | else | |
333 | fprintf(f, "Requires=%s\n", unit); | |
70f5f48e MS |
334 | } |
335 | ||
155da457 LP |
336 | if (!nofail) |
337 | fprintf(f, | |
b001ad61 ZJS |
338 | "Before=%s\n", |
339 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
155da457 | 340 | |
50d2eba2 | 341 | if (password && !keydev) { |
38ab1ec9 RS |
342 | r = print_dependencies(f, password); |
343 | if (r < 0) | |
344 | return r; | |
345 | } | |
bde29068 | 346 | |
38ab1ec9 RS |
347 | /* Check if a header option was specified */ |
348 | if (detached_header > 0) { | |
349 | r = print_dependencies(f, header_path); | |
350 | if (r < 0) | |
351 | return r; | |
ceca9501 | 352 | } |
e23a0ce8 | 353 | |
a7e88558 | 354 | if (path_startswith(u, "/dev/")) |
9ece938a TW |
355 | fprintf(f, |
356 | "BindsTo=%s\n" | |
357 | "After=%s\n" | |
358 | "Before=umount.target\n", | |
359 | d, d); | |
a7e88558 | 360 | else |
b90cbe66 LHS |
361 | /* For loopback devices, add systemd-tmpfiles-setup-dev.service |
362 | dependency to ensure that loopback support is available in | |
363 | the kernel (/dev/loop-control needs to exist) */ | |
9ece938a | 364 | fprintf(f, |
b90cbe66 LHS |
365 | "RequiresMountsFor=%s\n" |
366 | "Requires=systemd-tmpfiles-setup-dev.service\n" | |
367 | "After=systemd-tmpfiles-setup-dev.service\n", | |
98bad05e LP |
368 | u_escaped); |
369 | ||
8eea8687 ZJS |
370 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
371 | if (r < 0) | |
7cecc563 | 372 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); |
8eea8687 | 373 | |
a7e88558 LP |
374 | r = generator_write_cryptsetup_service_section(f, name, u, password, filtered); |
375 | if (r < 0) | |
376 | return r; | |
e23a0ce8 | 377 | |
53ac130b LP |
378 | if (tmp) { |
379 | _cleanup_free_ char *tmp_fstype_escaped = NULL; | |
380 | ||
381 | if (tmp_fstype) { | |
382 | tmp_fstype_escaped = specifier_escape(tmp_fstype); | |
383 | if (!tmp_fstype_escaped) | |
384 | return log_oom(); | |
385 | } | |
386 | ||
e23a0ce8 | 387 | fprintf(f, |
53ac130b LP |
388 | "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n", |
389 | tmp_fstype_escaped ?: "ext4", name_escaped); | |
390 | } | |
e23a0ce8 | 391 | |
8c11d3c1 | 392 | if (swap) |
e23a0ce8 | 393 | fprintf(f, |
db2c56b0 | 394 | "ExecStartPost=" ROOTLIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n", |
98bad05e | 395 | name_escaped); |
e23a0ce8 | 396 | |
70f5f48e MS |
397 | if (keydev) |
398 | fprintf(f, | |
50d2eba2 | 399 | "ExecStartPost=-" UMOUNT_PATH " %s\n\n", |
70f5f48e MS |
400 | keydev_mount); |
401 | ||
4652c56c ZJS |
402 | r = fflush_and_check(f); |
403 | if (r < 0) | |
fb883e75 | 404 | return log_error_errno(r, "Failed to write unit file %s: %m", n); |
e23a0ce8 | 405 | |
155da457 | 406 | if (!noauto) { |
b001ad61 ZJS |
407 | r = generator_add_symlink(arg_dest, |
408 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
b559616f ZJS |
409 | nofail ? "wants" : "requires", n); |
410 | if (r < 0) | |
411 | return r; | |
f7f21d33 | 412 | } |
74715b82 | 413 | |
b559616f ZJS |
414 | dmname = strjoina("dev-mapper-", e, ".device"); |
415 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
416 | if (r < 0) | |
417 | return r; | |
74715b82 | 418 | |
68395007 | 419 | if (!noauto && !nofail) { |
7cecc563 ZJS |
420 | r = write_drop_in(arg_dest, dmname, 40, "device-timeout", |
421 | "# Automatically generated by systemd-cryptsetup-generator\n\n" | |
8eea8687 | 422 | "[Unit]\nJobTimeoutSec=0"); |
23bbb0de | 423 | if (r < 0) |
7cecc563 | 424 | log_warning_errno(r, "Failed to write device timeout drop-in: %m"); |
68395007 HH |
425 | } |
426 | ||
24a988e9 | 427 | return 0; |
e23a0ce8 LP |
428 | } |
429 | ||
a4a90e65 YW |
430 | static crypto_device* crypt_device_free(crypto_device *d) { |
431 | if (!d) | |
432 | return NULL; | |
433 | ||
6dd1c368 ZJS |
434 | free(d->uuid); |
435 | free(d->keyfile); | |
70f5f48e | 436 | free(d->keydev); |
6dd1c368 ZJS |
437 | free(d->name); |
438 | free(d->options); | |
a4a90e65 | 439 | return mfree(d); |
0fa9e53d JJ |
440 | } |
441 | ||
442 | static crypto_device *get_crypto_device(const char *uuid) { | |
443 | int r; | |
444 | crypto_device *d; | |
445 | ||
446 | assert(uuid); | |
447 | ||
448 | d = hashmap_get(arg_disks, uuid); | |
449 | if (!d) { | |
450 | d = new0(struct crypto_device, 1); | |
451 | if (!d) | |
452 | return NULL; | |
453 | ||
0fa9e53d | 454 | d->uuid = strdup(uuid); |
6b430fdb ZJS |
455 | if (!d->uuid) |
456 | return mfree(d); | |
0fa9e53d JJ |
457 | |
458 | r = hashmap_put(arg_disks, d->uuid, d); | |
459 | if (r < 0) { | |
460 | free(d->uuid); | |
6b430fdb | 461 | return mfree(d); |
0fa9e53d JJ |
462 | } |
463 | } | |
464 | ||
465 | return d; | |
466 | } | |
467 | ||
96287a49 | 468 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 469 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
470 | crypto_device *d; |
471 | int r; | |
66a78c2b | 472 | |
1d84ad94 | 473 | if (streq(key, "luks")) { |
059cb385 | 474 | |
1d84ad94 | 475 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 476 | if (r < 0) |
1d84ad94 | 477 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
478 | else |
479 | arg_enabled = r; | |
66a78c2b | 480 | |
1d84ad94 | 481 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 482 | |
1d84ad94 | 483 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 484 | if (r < 0) |
1d84ad94 | 485 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
486 | else |
487 | arg_read_crypttab = r; | |
66a78c2b | 488 | |
1d84ad94 LP |
489 | } else if (streq(key, "luks.uuid")) { |
490 | ||
491 | if (proc_cmdline_value_missing(key, value)) | |
492 | return 0; | |
66a78c2b | 493 | |
0fa9e53d JJ |
494 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
495 | if (!d) | |
141a79f4 | 496 | return log_oom(); |
66a78c2b | 497 | |
6b000af4 | 498 | d->create = arg_allow_list = true; |
0fa9e53d | 499 | |
1d84ad94 LP |
500 | } else if (streq(key, "luks.options")) { |
501 | ||
502 | if (proc_cmdline_value_missing(key, value)) | |
503 | return 0; | |
66a78c2b | 504 | |
0fa9e53d JJ |
505 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
506 | if (r == 2) { | |
507 | d = get_crypto_device(uuid); | |
508 | if (!d) | |
509 | return log_oom(); | |
510 | ||
1d84ad94 | 511 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 512 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 513 | return log_oom(); |
66a78c2b | 514 | |
1d84ad94 | 515 | } else if (streq(key, "luks.key")) { |
7949dfa7 MS |
516 | size_t n; |
517 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; | |
7949dfa7 | 518 | const char *keyspec; |
1d84ad94 LP |
519 | |
520 | if (proc_cmdline_value_missing(key, value)) | |
521 | return 0; | |
66a78c2b | 522 | |
7949dfa7 MS |
523 | n = strspn(value, LETTERS DIGITS "-"); |
524 | if (value[n] != '=') { | |
525 | if (free_and_strdup(&arg_default_keyfile, value) < 0) | |
526 | return log_oom(); | |
527 | return 0; | |
528 | } | |
70f5f48e | 529 | |
7949dfa7 MS |
530 | uuid = strndup(value, n); |
531 | if (!uuid) | |
532 | return log_oom(); | |
6cd5b12a | 533 | |
7949dfa7 MS |
534 | if (!id128_is_valid(uuid)) { |
535 | log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring."); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | d = get_crypto_device(uuid); | |
540 | if (!d) | |
541 | return log_oom(); | |
70f5f48e | 542 | |
7949dfa7 | 543 | keyspec = value + n + 1; |
50d2eba2 | 544 | r = split_keyspec(keyspec, &keyfile, &keydev); |
545 | if (r < 0) | |
546 | return r; | |
70f5f48e | 547 | |
7949dfa7 MS |
548 | free_and_replace(d->keyfile, keyfile); |
549 | free_and_replace(d->keydev, keydev); | |
50d2eba2 | 550 | |
1d84ad94 LP |
551 | } else if (streq(key, "luks.name")) { |
552 | ||
553 | if (proc_cmdline_value_missing(key, value)) | |
554 | return 0; | |
baade8cc JJ |
555 | |
556 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
557 | if (r == 2) { | |
558 | d = get_crypto_device(uuid); | |
559 | if (!d) | |
560 | return log_oom(); | |
561 | ||
6b000af4 | 562 | d->create = arg_allow_list = true; |
baade8cc | 563 | |
f9ecfd3b | 564 | free_and_replace(d->name, uuid_value); |
baade8cc JJ |
565 | } else |
566 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 567 | } |
66a78c2b | 568 | |
24a988e9 | 569 | return 0; |
66a78c2b LP |
570 | } |
571 | ||
0fa9e53d | 572 | static int add_crypttab_devices(void) { |
7fd1b19b | 573 | _cleanup_fclose_ FILE *f = NULL; |
b42674a1 LP |
574 | unsigned crypttab_line = 0; |
575 | struct stat st; | |
576 | int r; | |
e23a0ce8 | 577 | |
0fa9e53d JJ |
578 | if (!arg_read_crypttab) |
579 | return 0; | |
580 | ||
a6c57e74 | 581 | r = fopen_unlocked(arg_crypttab, "re", &f); |
fdeea3f4 | 582 | if (r < 0) { |
0fa9e53d | 583 | if (errno != ENOENT) |
a6c57e74 | 584 | log_error_errno(errno, "Failed to open %s: %m", arg_crypttab); |
0fa9e53d | 585 | return 0; |
e23a0ce8 LP |
586 | } |
587 | ||
0fa9e53d | 588 | if (fstat(fileno(f), &st) < 0) { |
a6c57e74 | 589 | log_error_errno(errno, "Failed to stat %s: %m", arg_crypttab); |
0fa9e53d JJ |
590 | return 0; |
591 | } | |
e23a0ce8 | 592 | |
0fa9e53d | 593 | for (;;) { |
fef716b2 | 594 | _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, *keyfile = NULL, *keydev = NULL; |
0fa9e53d | 595 | crypto_device *d = NULL; |
b42674a1 LP |
596 | char *l, *uuid; |
597 | int k; | |
4c12626c | 598 | |
b42674a1 LP |
599 | r = read_line(f, LONG_LINE_MAX, &line); |
600 | if (r < 0) | |
a6c57e74 | 601 | return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); |
b42674a1 | 602 | if (r == 0) |
0fa9e53d | 603 | break; |
66a78c2b | 604 | |
0fa9e53d | 605 | crypttab_line++; |
141a79f4 | 606 | |
0fa9e53d | 607 | l = strstrip(line); |
b42674a1 | 608 | if (IN_SET(l[0], 0, '#')) |
0fa9e53d | 609 | continue; |
66a78c2b | 610 | |
50d2eba2 | 611 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); |
0fa9e53d | 612 | if (k < 2 || k > 4) { |
a6c57e74 | 613 | log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); |
0fa9e53d JJ |
614 | continue; |
615 | } | |
66a78c2b | 616 | |
844d5a87 | 617 | uuid = startswith(device, "UUID="); |
0fa9e53d JJ |
618 | if (!uuid) |
619 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
844d5a87 LR |
620 | if (!uuid) |
621 | uuid = startswith(name, "luks-"); | |
0fa9e53d JJ |
622 | if (uuid) |
623 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 624 | |
6b000af4 | 625 | if (arg_allow_list && !d) { |
0fa9e53d JJ |
626 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); |
627 | continue; | |
8973790e LP |
628 | } |
629 | ||
50d2eba2 | 630 | r = split_keyspec(keyspec, &keyfile, &keydev); |
631 | if (r < 0) | |
632 | return r; | |
633 | ||
634 | r = create_disk(name, device, keyfile, keydev, (d && d->options) ? d->options : options); | |
0fa9e53d JJ |
635 | if (r < 0) |
636 | return r; | |
8973790e | 637 | |
0fa9e53d JJ |
638 | if (d) |
639 | d->create = false; | |
640 | } | |
8973790e | 641 | |
0fa9e53d JJ |
642 | return 0; |
643 | } | |
66a78c2b | 644 | |
0fa9e53d JJ |
645 | static int add_proc_cmdline_devices(void) { |
646 | int r; | |
647 | Iterator i; | |
648 | crypto_device *d; | |
66a78c2b | 649 | |
0fa9e53d | 650 | HASHMAP_FOREACH(d, arg_disks, i) { |
baade8cc | 651 | _cleanup_free_ char *device = NULL; |
66a78c2b | 652 | |
0fa9e53d JJ |
653 | if (!d->create) |
654 | continue; | |
e23a0ce8 | 655 | |
baade8cc | 656 | if (!d->name) { |
b910cc72 | 657 | d->name = strjoin("luks-", d->uuid); |
baade8cc JJ |
658 | if (!d->name) |
659 | return log_oom(); | |
660 | } | |
e23a0ce8 | 661 | |
b910cc72 | 662 | device = strjoin("UUID=", d->uuid); |
0fa9e53d JJ |
663 | if (!device) |
664 | return log_oom(); | |
7ab064a6 | 665 | |
7cecc563 ZJS |
666 | r = create_disk(d->name, |
667 | device, | |
668 | d->keyfile ?: arg_default_keyfile, | |
669 | d->keydev, | |
670 | d->options ?: arg_default_options); | |
0fa9e53d JJ |
671 | if (r < 0) |
672 | return r; | |
e23a0ce8 LP |
673 | } |
674 | ||
0fa9e53d JJ |
675 | return 0; |
676 | } | |
e23a0ce8 | 677 | |
a4a90e65 YW |
678 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, |
679 | crypto_device, crypt_device_free); | |
680 | ||
7a44c7e3 | 681 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
5f4bfe56 | 682 | int r; |
e23a0ce8 | 683 | |
7a44c7e3 | 684 | assert_se(arg_dest = dest); |
e23a0ce8 | 685 | |
a6c57e74 | 686 | arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab"; |
3f5ac303 | 687 | arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup"; |
a6c57e74 | 688 | |
a4a90e65 YW |
689 | arg_disks = hashmap_new(&crypt_device_hash_ops); |
690 | if (!arg_disks) | |
691 | return log_oom(); | |
7ab064a6 | 692 | |
1d84ad94 | 693 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
a4a90e65 YW |
694 | if (r < 0) |
695 | return log_warning_errno(r, "Failed to parse kernel command line: %m"); | |
7ab064a6 | 696 | |
a4a90e65 YW |
697 | if (!arg_enabled) |
698 | return 0; | |
e23a0ce8 | 699 | |
5f4bfe56 LP |
700 | r = add_crypttab_devices(); |
701 | if (r < 0) | |
a4a90e65 | 702 | return r; |
0fa9e53d | 703 | |
5f4bfe56 LP |
704 | r = add_proc_cmdline_devices(); |
705 | if (r < 0) | |
a4a90e65 | 706 | return r; |
141a79f4 | 707 | |
a4a90e65 | 708 | return 0; |
e23a0ce8 | 709 | } |
a4a90e65 | 710 | |
7a44c7e3 | 711 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |