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