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