]>
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; |
0fa9e53d JJ |
41 | static bool arg_whitelist = false; |
42 | static Hashmap *arg_disks = NULL; | |
43 | static char *arg_default_options = NULL; | |
44 | static char *arg_default_keyfile = NULL; | |
141a79f4 | 45 | |
a4a90e65 YW |
46 | STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep); |
47 | STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep); | |
48 | STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep); | |
49 | ||
f4ea8432 | 50 | static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_keydev) { |
5d2100dc | 51 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; |
f4ea8432 | 52 | const char *c; |
50d2eba2 | 53 | |
f4ea8432 LP |
54 | assert(ret_keyfile); |
55 | assert(ret_keydev); | |
50d2eba2 | 56 | |
fef716b2 ZJS |
57 | if (!keyspec) { |
58 | *ret_keyfile = *ret_keydev = NULL; | |
59 | return 0; | |
60 | } | |
61 | ||
50d2eba2 | 62 | c = strrchr(keyspec, ':'); |
63 | if (c) { | |
5d2100dc ZJS |
64 | keyfile = strndup(keyspec, c-keyspec); |
65 | keydev = strdup(c + 1); | |
66 | if (!keyfile || !keydev) | |
50d2eba2 | 67 | return log_oom(); |
68 | } else { | |
69 | /* No keydev specified */ | |
5d2100dc ZJS |
70 | keyfile = strdup(keyspec); |
71 | keydev = NULL; | |
72 | if (!keyfile) | |
50d2eba2 | 73 | return log_oom(); |
74 | } | |
75 | ||
5d2100dc ZJS |
76 | *ret_keyfile = TAKE_PTR(keyfile); |
77 | *ret_keydev = TAKE_PTR(keydev); | |
50d2eba2 | 78 | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int generate_keydev_mount(const char *name, const char *keydev, const char *keydev_timeout, bool canfail, char **unit, char **mount) { | |
83 | _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL; | |
70f5f48e MS |
84 | _cleanup_fclose_ FILE *f = NULL; |
85 | int r; | |
50d2eba2 | 86 | usec_t timeout_us; |
70f5f48e MS |
87 | |
88 | assert(name); | |
89 | assert(keydev); | |
90 | assert(unit); | |
91 | assert(mount); | |
92 | ||
93 | r = mkdir_parents("/run/systemd/cryptsetup", 0755); | |
94 | if (r < 0) | |
95 | return r; | |
96 | ||
97 | r = mkdir("/run/systemd/cryptsetup", 0700); | |
579875bc MS |
98 | if (r < 0 && errno != EEXIST) |
99 | return -errno; | |
70f5f48e | 100 | |
7949dfa7 MS |
101 | name_escaped = cescape(name); |
102 | if (!name_escaped) | |
103 | return -ENOMEM; | |
104 | ||
105 | where = strjoin("/run/systemd/cryptsetup/keydev-", name_escaped); | |
70f5f48e MS |
106 | if (!where) |
107 | return -ENOMEM; | |
108 | ||
109 | r = mkdir(where, 0700); | |
579875bc MS |
110 | if (r < 0 && errno != EEXIST) |
111 | return -errno; | |
70f5f48e MS |
112 | |
113 | r = unit_name_from_path(where, ".mount", &u); | |
114 | if (r < 0) | |
115 | return r; | |
116 | ||
117 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
118 | if (r < 0) | |
119 | return r; | |
120 | ||
121 | what = fstab_node_to_udev_node(keydev); | |
122 | if (!what) | |
123 | return -ENOMEM; | |
124 | ||
125 | fprintf(f, | |
126 | "[Unit]\n" | |
127 | "DefaultDependencies=no\n\n" | |
128 | "[Mount]\n" | |
129 | "What=%s\n" | |
130 | "Where=%s\n" | |
50d2eba2 | 131 | "Options=ro%s\n", what, where, canfail ? ",nofail" : ""); |
132 | ||
133 | if (keydev_timeout) { | |
134 | r = parse_sec_fix_0(keydev_timeout, &timeout_us); | |
135 | if (r >= 0) { | |
136 | r = unit_name_from_path(what, ".device", &device_unit); | |
137 | if (r < 0) | |
138 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
139 | ||
140 | r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout", | |
141 | "# Automatically generated by systemd-cryptsetup-generator \n\n" | |
142 | "[Unit]\nJobRunningTimeoutSec=%s", keydev_timeout); | |
143 | if (r < 0) | |
144 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
145 | ||
146 | } else | |
147 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", keydev_timeout); | |
148 | ||
149 | } | |
70f5f48e MS |
150 | |
151 | r = fflush_and_check(f); | |
152 | if (r < 0) | |
153 | return r; | |
154 | ||
155 | *unit = TAKE_PTR(u); | |
156 | *mount = TAKE_PTR(where); | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
38ab1ec9 RS |
161 | static int print_dependencies(FILE *f, const char* device_path) { |
162 | int r; | |
163 | ||
164 | if (STR_IN_SET(device_path, "-", "none")) | |
165 | /* None, nothing to do */ | |
166 | return 0; | |
167 | ||
168 | if (PATH_IN_SET(device_path, "/dev/urandom", "/dev/random", "/dev/hw_random")) { | |
169 | /* RNG device, add random dep */ | |
170 | fputs("After=systemd-random-seed.service\n", f); | |
171 | return 0; | |
172 | } | |
173 | ||
174 | _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path); | |
175 | if (!udev_node) | |
176 | return log_oom(); | |
177 | ||
178 | if (path_equal(udev_node, "/dev/null")) | |
179 | return 0; | |
180 | ||
181 | if (path_startswith(udev_node, "/dev/")) { | |
182 | /* We are dealing with a block device, add dependency for correspoding unit */ | |
183 | _cleanup_free_ char *unit = NULL; | |
184 | ||
185 | r = unit_name_from_path(udev_node, ".device", &unit); | |
186 | if (r < 0) | |
187 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
188 | ||
189 | fprintf(f, "After=%1$s\nRequires=%1$s\n", unit); | |
190 | } else { | |
191 | /* Regular file, add mount dependency */ | |
192 | _cleanup_free_ char *escaped_path = specifier_escape(device_path); | |
193 | if (!escaped_path) | |
194 | return log_oom(); | |
195 | ||
196 | fprintf(f, "RequiresMountsFor=%s\n", escaped_path); | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
e23a0ce8 LP |
202 | static int create_disk( |
203 | const char *name, | |
204 | const char *device, | |
205 | const char *password, | |
50d2eba2 | 206 | const char *keydev, |
e23a0ce8 LP |
207 | const char *options) { |
208 | ||
fb883e75 | 209 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
50d2eba2 | 210 | *keydev_mount = NULL, *keyfile_timeout_value = NULL, *password_escaped = NULL, |
211 | *filtered = NULL, *u_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL, *header_path = NULL; | |
7fd1b19b | 212 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 213 | const char *dmname; |
b001ad61 | 214 | bool noauto, nofail, tmp, swap, netdev; |
50d2eba2 | 215 | int r, detached_header, keyfile_can_timeout; |
e23a0ce8 LP |
216 | |
217 | assert(name); | |
218 | assert(device); | |
219 | ||
b9f111b9 ZJS |
220 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
221 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 ZJS |
222 | tmp = fstab_test_option(options, "tmp\0"); |
223 | swap = fstab_test_option(options, "swap\0"); | |
b001ad61 | 224 | netdev = fstab_test_option(options, "_netdev\0"); |
8c11d3c1 | 225 | |
50d2eba2 | 226 | keyfile_can_timeout = fstab_filter_options(options, "keyfile-timeout\0", NULL, &keyfile_timeout_value, NULL); |
227 | if (keyfile_can_timeout < 0) | |
228 | return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m"); | |
229 | ||
38ab1ec9 RS |
230 | detached_header = fstab_filter_options(options, "header\0", NULL, &header_path, NULL); |
231 | if (detached_header < 0) | |
232 | return log_error_errno(detached_header, "Failed to parse header= option value: %m"); | |
233 | ||
baaa35ad ZJS |
234 | if (tmp && swap) |
235 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
236 | "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", | |
237 | name); | |
155da457 | 238 | |
98bad05e LP |
239 | name_escaped = specifier_escape(name); |
240 | if (!name_escaped) | |
241 | return log_oom(); | |
242 | ||
744198e9 LP |
243 | e = unit_name_escape(name); |
244 | if (!e) | |
245 | return log_oom(); | |
246 | ||
f7f21d33 | 247 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
248 | if (!u) |
249 | return log_oom(); | |
e23a0ce8 | 250 | |
fb883e75 ZJS |
251 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
252 | if (r < 0) | |
253 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
254 | ||
98bad05e LP |
255 | u_escaped = specifier_escape(u); |
256 | if (!u_escaped) | |
257 | return log_oom(); | |
258 | ||
7410616c LP |
259 | r = unit_name_from_path(u, ".device", &d); |
260 | if (r < 0) | |
261 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 262 | |
baaa35ad ZJS |
263 | if (keydev && !password) |
264 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
265 | "Key device is specified, but path to the password file is missing."); | |
70f5f48e | 266 | |
fb883e75 ZJS |
267 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
268 | if (r < 0) | |
269 | return r; | |
0d536673 | 270 | |
b001ad61 | 271 | fprintf(f, |
b001ad61 ZJS |
272 | "[Unit]\n" |
273 | "Description=Cryptography Setup for %%I\n" | |
274 | "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n" | |
a6c57e74 | 275 | "SourcePath=%s\n" |
b001ad61 ZJS |
276 | "DefaultDependencies=no\n" |
277 | "Conflicts=umount.target\n" | |
278 | "IgnoreOnIsolate=true\n" | |
279 | "After=%s\n", | |
a6c57e74 | 280 | arg_crypttab, |
a0dd2097 | 281 | netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target"); |
e23a0ce8 | 282 | |
50d2eba2 | 283 | if (password) { |
284 | password_escaped = specifier_escape(password); | |
285 | if (!password_escaped) | |
286 | return log_oom(); | |
287 | } | |
288 | ||
70f5f48e MS |
289 | if (keydev) { |
290 | _cleanup_free_ char *unit = NULL, *p = NULL; | |
291 | ||
50d2eba2 | 292 | r = generate_keydev_mount(name, keydev, keyfile_timeout_value, keyfile_can_timeout > 0, &unit, &keydev_mount); |
70f5f48e MS |
293 | if (r < 0) |
294 | return log_error_errno(r, "Failed to generate keydev mount unit: %m"); | |
295 | ||
c6134d3e | 296 | p = path_join(keydev_mount, password_escaped); |
70f5f48e MS |
297 | if (!p) |
298 | return log_oom(); | |
299 | ||
300 | free_and_replace(password_escaped, p); | |
50d2eba2 | 301 | |
302 | fprintf(f, "After=%s\n", unit); | |
303 | if (keyfile_can_timeout > 0) | |
304 | fprintf(f, "Wants=%s\n", unit); | |
305 | else | |
306 | fprintf(f, "Requires=%s\n", unit); | |
70f5f48e MS |
307 | } |
308 | ||
155da457 LP |
309 | if (!nofail) |
310 | fprintf(f, | |
b001ad61 ZJS |
311 | "Before=%s\n", |
312 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
155da457 | 313 | |
50d2eba2 | 314 | if (password && !keydev) { |
38ab1ec9 RS |
315 | r = print_dependencies(f, password); |
316 | if (r < 0) | |
317 | return r; | |
318 | } | |
bde29068 | 319 | |
38ab1ec9 RS |
320 | /* Check if a header option was specified */ |
321 | if (detached_header > 0) { | |
322 | r = print_dependencies(f, header_path); | |
323 | if (r < 0) | |
324 | return r; | |
ceca9501 | 325 | } |
e23a0ce8 | 326 | |
048dd629 | 327 | if (path_startswith(u, "/dev/")) { |
9ece938a TW |
328 | fprintf(f, |
329 | "BindsTo=%s\n" | |
330 | "After=%s\n" | |
331 | "Before=umount.target\n", | |
332 | d, d); | |
a6f8786a MFO |
333 | |
334 | if (swap) | |
0d536673 LP |
335 | fputs("Before=dev-mapper-%i.swap\n", |
336 | f); | |
a6f8786a | 337 | } else |
b90cbe66 LHS |
338 | /* For loopback devices, add systemd-tmpfiles-setup-dev.service |
339 | dependency to ensure that loopback support is available in | |
340 | the kernel (/dev/loop-control needs to exist) */ | |
9ece938a | 341 | fprintf(f, |
b90cbe66 LHS |
342 | "RequiresMountsFor=%s\n" |
343 | "Requires=systemd-tmpfiles-setup-dev.service\n" | |
344 | "After=systemd-tmpfiles-setup-dev.service\n", | |
98bad05e LP |
345 | u_escaped); |
346 | ||
8eea8687 ZJS |
347 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
348 | if (r < 0) | |
349 | return r; | |
350 | ||
d31eb24f LP |
351 | if (filtered) { |
352 | filtered_escaped = specifier_escape(filtered); | |
353 | if (!filtered_escaped) | |
354 | return log_oom(); | |
355 | } | |
98bad05e | 356 | |
e23a0ce8 LP |
357 | fprintf(f, |
358 | "\n[Service]\n" | |
359 | "Type=oneshot\n" | |
360 | "RemainAfterExit=yes\n" | |
90724929 | 361 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
0b1f68ac | 362 | "KeyringMode=shared\n" /* make sure we can share cached keys among instances */ |
48da47ee | 363 | "OOMScoreAdjust=500\n" /* unlocking can allocate a lot of memory if Argon2 is used */ |
260ab287 | 364 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 365 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
98bad05e LP |
366 | name_escaped, u_escaped, strempty(password_escaped), strempty(filtered_escaped), |
367 | name_escaped); | |
e23a0ce8 | 368 | |
8c11d3c1 | 369 | if (tmp) |
e23a0ce8 | 370 | fprintf(f, |
50109038 | 371 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
98bad05e | 372 | name_escaped); |
e23a0ce8 | 373 | |
8c11d3c1 | 374 | if (swap) |
e23a0ce8 | 375 | fprintf(f, |
50109038 | 376 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
98bad05e | 377 | name_escaped); |
e23a0ce8 | 378 | |
70f5f48e MS |
379 | if (keydev) |
380 | fprintf(f, | |
50d2eba2 | 381 | "ExecStartPost=-" UMOUNT_PATH " %s\n\n", |
70f5f48e MS |
382 | keydev_mount); |
383 | ||
4652c56c ZJS |
384 | r = fflush_and_check(f); |
385 | if (r < 0) | |
fb883e75 | 386 | return log_error_errno(r, "Failed to write unit file %s: %m", n); |
e23a0ce8 | 387 | |
155da457 | 388 | if (!noauto) { |
b001ad61 ZJS |
389 | r = generator_add_symlink(arg_dest, |
390 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
b559616f ZJS |
391 | nofail ? "wants" : "requires", n); |
392 | if (r < 0) | |
393 | return r; | |
f7f21d33 | 394 | } |
74715b82 | 395 | |
b559616f ZJS |
396 | dmname = strjoina("dev-mapper-", e, ".device"); |
397 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
398 | if (r < 0) | |
399 | return r; | |
74715b82 | 400 | |
68395007 | 401 | if (!noauto && !nofail) { |
a6fb0dc1 | 402 | r = write_drop_in(arg_dest, dmname, 90, "device-timeout", |
8eea8687 ZJS |
403 | "# Automatically generated by systemd-cryptsetup-generator \n\n" |
404 | "[Unit]\nJobTimeoutSec=0"); | |
23bbb0de MS |
405 | if (r < 0) |
406 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
68395007 HH |
407 | } |
408 | ||
24a988e9 | 409 | return 0; |
e23a0ce8 LP |
410 | } |
411 | ||
a4a90e65 YW |
412 | static crypto_device* crypt_device_free(crypto_device *d) { |
413 | if (!d) | |
414 | return NULL; | |
415 | ||
6dd1c368 ZJS |
416 | free(d->uuid); |
417 | free(d->keyfile); | |
70f5f48e | 418 | free(d->keydev); |
6dd1c368 ZJS |
419 | free(d->name); |
420 | free(d->options); | |
a4a90e65 | 421 | return mfree(d); |
0fa9e53d JJ |
422 | } |
423 | ||
424 | static crypto_device *get_crypto_device(const char *uuid) { | |
425 | int r; | |
426 | crypto_device *d; | |
427 | ||
428 | assert(uuid); | |
429 | ||
430 | d = hashmap_get(arg_disks, uuid); | |
431 | if (!d) { | |
432 | d = new0(struct crypto_device, 1); | |
433 | if (!d) | |
434 | return NULL; | |
435 | ||
0fa9e53d | 436 | d->uuid = strdup(uuid); |
6b430fdb ZJS |
437 | if (!d->uuid) |
438 | return mfree(d); | |
0fa9e53d JJ |
439 | |
440 | r = hashmap_put(arg_disks, d->uuid, d); | |
441 | if (r < 0) { | |
442 | free(d->uuid); | |
6b430fdb | 443 | return mfree(d); |
0fa9e53d JJ |
444 | } |
445 | } | |
446 | ||
447 | return d; | |
448 | } | |
449 | ||
96287a49 | 450 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 451 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
452 | crypto_device *d; |
453 | int r; | |
66a78c2b | 454 | |
1d84ad94 | 455 | if (streq(key, "luks")) { |
059cb385 | 456 | |
1d84ad94 | 457 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 458 | if (r < 0) |
1d84ad94 | 459 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
460 | else |
461 | arg_enabled = r; | |
66a78c2b | 462 | |
1d84ad94 | 463 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 464 | |
1d84ad94 | 465 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 466 | if (r < 0) |
1d84ad94 | 467 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
468 | else |
469 | arg_read_crypttab = r; | |
66a78c2b | 470 | |
1d84ad94 LP |
471 | } else if (streq(key, "luks.uuid")) { |
472 | ||
473 | if (proc_cmdline_value_missing(key, value)) | |
474 | return 0; | |
66a78c2b | 475 | |
0fa9e53d JJ |
476 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
477 | if (!d) | |
141a79f4 | 478 | return log_oom(); |
66a78c2b | 479 | |
0fa9e53d JJ |
480 | d->create = arg_whitelist = true; |
481 | ||
1d84ad94 LP |
482 | } else if (streq(key, "luks.options")) { |
483 | ||
484 | if (proc_cmdline_value_missing(key, value)) | |
485 | return 0; | |
66a78c2b | 486 | |
0fa9e53d JJ |
487 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
488 | if (r == 2) { | |
489 | d = get_crypto_device(uuid); | |
490 | if (!d) | |
491 | return log_oom(); | |
492 | ||
1d84ad94 | 493 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 494 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 495 | return log_oom(); |
66a78c2b | 496 | |
1d84ad94 | 497 | } else if (streq(key, "luks.key")) { |
7949dfa7 MS |
498 | size_t n; |
499 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; | |
7949dfa7 | 500 | const char *keyspec; |
1d84ad94 LP |
501 | |
502 | if (proc_cmdline_value_missing(key, value)) | |
503 | return 0; | |
66a78c2b | 504 | |
7949dfa7 MS |
505 | n = strspn(value, LETTERS DIGITS "-"); |
506 | if (value[n] != '=') { | |
507 | if (free_and_strdup(&arg_default_keyfile, value) < 0) | |
508 | return log_oom(); | |
509 | return 0; | |
510 | } | |
70f5f48e | 511 | |
7949dfa7 MS |
512 | uuid = strndup(value, n); |
513 | if (!uuid) | |
514 | return log_oom(); | |
6cd5b12a | 515 | |
7949dfa7 MS |
516 | if (!id128_is_valid(uuid)) { |
517 | log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring."); | |
518 | return 0; | |
519 | } | |
520 | ||
521 | d = get_crypto_device(uuid); | |
522 | if (!d) | |
523 | return log_oom(); | |
70f5f48e | 524 | |
7949dfa7 | 525 | keyspec = value + n + 1; |
50d2eba2 | 526 | r = split_keyspec(keyspec, &keyfile, &keydev); |
527 | if (r < 0) | |
528 | return r; | |
70f5f48e | 529 | |
7949dfa7 MS |
530 | free_and_replace(d->keyfile, keyfile); |
531 | free_and_replace(d->keydev, keydev); | |
50d2eba2 | 532 | |
1d84ad94 LP |
533 | } else if (streq(key, "luks.name")) { |
534 | ||
535 | if (proc_cmdline_value_missing(key, value)) | |
536 | return 0; | |
baade8cc JJ |
537 | |
538 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
539 | if (r == 2) { | |
540 | d = get_crypto_device(uuid); | |
541 | if (!d) | |
542 | return log_oom(); | |
543 | ||
544 | d->create = arg_whitelist = true; | |
545 | ||
f9ecfd3b | 546 | free_and_replace(d->name, uuid_value); |
baade8cc JJ |
547 | } else |
548 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 549 | } |
66a78c2b | 550 | |
24a988e9 | 551 | return 0; |
66a78c2b LP |
552 | } |
553 | ||
0fa9e53d | 554 | static int add_crypttab_devices(void) { |
7fd1b19b | 555 | _cleanup_fclose_ FILE *f = NULL; |
b42674a1 LP |
556 | unsigned crypttab_line = 0; |
557 | struct stat st; | |
558 | int r; | |
e23a0ce8 | 559 | |
0fa9e53d JJ |
560 | if (!arg_read_crypttab) |
561 | return 0; | |
562 | ||
a6c57e74 | 563 | r = fopen_unlocked(arg_crypttab, "re", &f); |
fdeea3f4 | 564 | if (r < 0) { |
0fa9e53d | 565 | if (errno != ENOENT) |
a6c57e74 | 566 | log_error_errno(errno, "Failed to open %s: %m", arg_crypttab); |
0fa9e53d | 567 | return 0; |
e23a0ce8 LP |
568 | } |
569 | ||
0fa9e53d | 570 | if (fstat(fileno(f), &st) < 0) { |
a6c57e74 | 571 | log_error_errno(errno, "Failed to stat %s: %m", arg_crypttab); |
0fa9e53d JJ |
572 | return 0; |
573 | } | |
e23a0ce8 | 574 | |
0fa9e53d | 575 | for (;;) { |
fef716b2 | 576 | _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, *keyfile = NULL, *keydev = NULL; |
0fa9e53d | 577 | crypto_device *d = NULL; |
b42674a1 LP |
578 | char *l, *uuid; |
579 | int k; | |
4c12626c | 580 | |
b42674a1 LP |
581 | r = read_line(f, LONG_LINE_MAX, &line); |
582 | if (r < 0) | |
a6c57e74 | 583 | return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); |
b42674a1 | 584 | if (r == 0) |
0fa9e53d | 585 | break; |
66a78c2b | 586 | |
0fa9e53d | 587 | crypttab_line++; |
141a79f4 | 588 | |
0fa9e53d | 589 | l = strstrip(line); |
b42674a1 | 590 | if (IN_SET(l[0], 0, '#')) |
0fa9e53d | 591 | continue; |
66a78c2b | 592 | |
50d2eba2 | 593 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); |
0fa9e53d | 594 | if (k < 2 || k > 4) { |
a6c57e74 | 595 | log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); |
0fa9e53d JJ |
596 | continue; |
597 | } | |
66a78c2b | 598 | |
844d5a87 | 599 | uuid = startswith(device, "UUID="); |
0fa9e53d JJ |
600 | if (!uuid) |
601 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
844d5a87 LR |
602 | if (!uuid) |
603 | uuid = startswith(name, "luks-"); | |
0fa9e53d JJ |
604 | if (uuid) |
605 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 606 | |
0fa9e53d JJ |
607 | if (arg_whitelist && !d) { |
608 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
609 | continue; | |
8973790e LP |
610 | } |
611 | ||
50d2eba2 | 612 | r = split_keyspec(keyspec, &keyfile, &keydev); |
613 | if (r < 0) | |
614 | return r; | |
615 | ||
616 | r = create_disk(name, device, keyfile, keydev, (d && d->options) ? d->options : options); | |
0fa9e53d JJ |
617 | if (r < 0) |
618 | return r; | |
8973790e | 619 | |
0fa9e53d JJ |
620 | if (d) |
621 | d->create = false; | |
622 | } | |
8973790e | 623 | |
0fa9e53d JJ |
624 | return 0; |
625 | } | |
66a78c2b | 626 | |
0fa9e53d JJ |
627 | static int add_proc_cmdline_devices(void) { |
628 | int r; | |
629 | Iterator i; | |
630 | crypto_device *d; | |
66a78c2b | 631 | |
0fa9e53d JJ |
632 | HASHMAP_FOREACH(d, arg_disks, i) { |
633 | const char *options; | |
baade8cc | 634 | _cleanup_free_ char *device = NULL; |
66a78c2b | 635 | |
0fa9e53d JJ |
636 | if (!d->create) |
637 | continue; | |
e23a0ce8 | 638 | |
baade8cc | 639 | if (!d->name) { |
b910cc72 | 640 | d->name = strjoin("luks-", d->uuid); |
baade8cc JJ |
641 | if (!d->name) |
642 | return log_oom(); | |
643 | } | |
e23a0ce8 | 644 | |
b910cc72 | 645 | device = strjoin("UUID=", d->uuid); |
0fa9e53d JJ |
646 | if (!device) |
647 | return log_oom(); | |
7ab064a6 | 648 | |
0fa9e53d JJ |
649 | if (d->options) |
650 | options = d->options; | |
651 | else if (arg_default_options) | |
652 | options = arg_default_options; | |
653 | else | |
654 | options = "timeout=0"; | |
141a79f4 | 655 | |
50d2eba2 | 656 | r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, d->keydev, options); |
0fa9e53d JJ |
657 | if (r < 0) |
658 | return r; | |
e23a0ce8 LP |
659 | } |
660 | ||
0fa9e53d JJ |
661 | return 0; |
662 | } | |
e23a0ce8 | 663 | |
a4a90e65 YW |
664 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, |
665 | crypto_device, crypt_device_free); | |
666 | ||
7a44c7e3 | 667 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
5f4bfe56 | 668 | int r; |
e23a0ce8 | 669 | |
7a44c7e3 | 670 | assert_se(arg_dest = dest); |
e23a0ce8 | 671 | |
a6c57e74 ZJS |
672 | arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab"; |
673 | ||
a4a90e65 YW |
674 | arg_disks = hashmap_new(&crypt_device_hash_ops); |
675 | if (!arg_disks) | |
676 | return log_oom(); | |
7ab064a6 | 677 | |
1d84ad94 | 678 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
a4a90e65 YW |
679 | if (r < 0) |
680 | return log_warning_errno(r, "Failed to parse kernel command line: %m"); | |
7ab064a6 | 681 | |
a4a90e65 YW |
682 | if (!arg_enabled) |
683 | return 0; | |
e23a0ce8 | 684 | |
5f4bfe56 LP |
685 | r = add_crypttab_devices(); |
686 | if (r < 0) | |
a4a90e65 | 687 | return r; |
0fa9e53d | 688 | |
5f4bfe56 LP |
689 | r = add_proc_cmdline_devices(); |
690 | if (r < 0) | |
a4a90e65 | 691 | return r; |
141a79f4 | 692 | |
a4a90e65 | 693 | return 0; |
e23a0ce8 | 694 | } |
a4a90e65 | 695 | |
7a44c7e3 | 696 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |