]>
Commit | Line | Data |
---|---|---|
e23a0ce8 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
e23a0ce8 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
e23a0ce8 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
e23a0ce8 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
e23a0ce8 | 20 | #include <errno.h> |
e23a0ce8 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
0fa9e53d | 23 | #include "dropin.h" |
3ffd4af2 | 24 | #include "fd-util.h" |
0d39fa9c | 25 | #include "fileio.h" |
07630cea | 26 | #include "fstab-util.h" |
0fa9e53d JJ |
27 | #include "generator.h" |
28 | #include "hashmap.h" | |
e23a0ce8 | 29 | #include "log.h" |
49e942b2 | 30 | #include "mkdir.h" |
6bedfcbb | 31 | #include "parse-util.h" |
bde29068 | 32 | #include "path-util.h" |
4e731273 | 33 | #include "proc-cmdline.h" |
07630cea | 34 | #include "string-util.h" |
0fa9e53d JJ |
35 | #include "strv.h" |
36 | #include "unit-name.h" | |
37 | #include "util.h" | |
38 | ||
39 | typedef struct crypto_device { | |
40 | char *uuid; | |
6cd5b12a | 41 | char *keyfile; |
baade8cc | 42 | char *name; |
0fa9e53d JJ |
43 | char *options; |
44 | bool create; | |
45 | } crypto_device; | |
e23a0ce8 | 46 | |
66a78c2b LP |
47 | static const char *arg_dest = "/tmp"; |
48 | static bool arg_enabled = true; | |
49 | static bool arg_read_crypttab = true; | |
0fa9e53d JJ |
50 | static bool arg_whitelist = false; |
51 | static Hashmap *arg_disks = NULL; | |
52 | static char *arg_default_options = NULL; | |
53 | static char *arg_default_keyfile = NULL; | |
141a79f4 | 54 | |
e23a0ce8 LP |
55 | static int create_disk( |
56 | const char *name, | |
57 | const char *device, | |
58 | const char *password, | |
59 | const char *options) { | |
60 | ||
b559616f | 61 | _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
8eea8687 | 62 | *filtered = NULL; |
7fd1b19b | 63 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 64 | const char *dmname; |
8c11d3c1 | 65 | bool noauto, nofail, tmp, swap; |
744198e9 | 66 | int r; |
e23a0ce8 LP |
67 | |
68 | assert(name); | |
69 | assert(device); | |
70 | ||
b9f111b9 ZJS |
71 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
72 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 ZJS |
73 | tmp = fstab_test_option(options, "tmp\0"); |
74 | swap = fstab_test_option(options, "swap\0"); | |
8c11d3c1 TG |
75 | |
76 | if (tmp && swap) { | |
77 | log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name); | |
78 | return -EINVAL; | |
79 | } | |
155da457 | 80 | |
744198e9 LP |
81 | e = unit_name_escape(name); |
82 | if (!e) | |
83 | return log_oom(); | |
84 | ||
7410616c LP |
85 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
86 | if (r < 0) | |
87 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 88 | |
605405c6 | 89 | p = strjoin(arg_dest, "/", n); |
24a988e9 HH |
90 | if (!p) |
91 | return log_oom(); | |
e23a0ce8 | 92 | |
f7f21d33 | 93 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
94 | if (!u) |
95 | return log_oom(); | |
e23a0ce8 | 96 | |
7410616c LP |
97 | r = unit_name_from_path(u, ".device", &d); |
98 | if (r < 0) | |
99 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 100 | |
f7f21d33 | 101 | f = fopen(p, "wxe"); |
4a62c710 MS |
102 | if (!f) |
103 | return log_error_errno(errno, "Failed to create unit file %s: %m", p); | |
e23a0ce8 | 104 | |
4b61c875 LP |
105 | fputs_unlocked("# Automatically generated by systemd-cryptsetup-generator\n\n" |
106 | "[Unit]\n" | |
107 | "Description=Cryptography Setup for %I\n" | |
108 | "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n" | |
109 | "SourcePath=/etc/crypttab\n" | |
110 | "DefaultDependencies=no\n" | |
111 | "Conflicts=umount.target\n" | |
4b61c875 LP |
112 | "IgnoreOnIsolate=true\n" |
113 | "After=cryptsetup-pre.target\n", | |
114 | f); | |
e23a0ce8 | 115 | |
155da457 LP |
116 | if (!nofail) |
117 | fprintf(f, | |
118 | "Before=cryptsetup.target\n"); | |
119 | ||
ceca9501 | 120 | if (password) { |
744198e9 | 121 | if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random")) |
4b61c875 | 122 | fputs_unlocked("After=systemd-random-seed.service\n", f); |
b559616f | 123 | else if (!STR_IN_SET(password, "-", "none")) { |
744198e9 LP |
124 | _cleanup_free_ char *uu; |
125 | ||
126 | uu = fstab_node_to_udev_node(password); | |
127 | if (!uu) | |
66a5dbdf DR |
128 | return log_oom(); |
129 | ||
bde29068 | 130 | if (!path_equal(uu, "/dev/null")) { |
744198e9 | 131 | |
048dd629 | 132 | if (path_startswith(uu, "/dev/")) { |
7410616c | 133 | _cleanup_free_ char *dd = NULL; |
66a5dbdf | 134 | |
7410616c LP |
135 | r = unit_name_from_path(uu, ".device", &dd); |
136 | if (r < 0) | |
137 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
bde29068 LP |
138 | |
139 | fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); | |
140 | } else | |
141 | fprintf(f, "RequiresMountsFor=%s\n", password); | |
142 | } | |
66a5dbdf | 143 | } |
ceca9501 | 144 | } |
e23a0ce8 | 145 | |
048dd629 | 146 | if (path_startswith(u, "/dev/")) { |
9ece938a TW |
147 | fprintf(f, |
148 | "BindsTo=%s\n" | |
149 | "After=%s\n" | |
150 | "Before=umount.target\n", | |
151 | d, d); | |
a6f8786a MFO |
152 | |
153 | if (swap) | |
4b61c875 LP |
154 | fputs_unlocked("Before=dev-mapper-%i.swap\n", |
155 | f); | |
a6f8786a | 156 | } else |
9ece938a TW |
157 | fprintf(f, |
158 | "RequiresMountsFor=%s\n", | |
159 | u); | |
160 | ||
8eea8687 ZJS |
161 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
162 | if (r < 0) | |
163 | return r; | |
164 | ||
e23a0ce8 LP |
165 | fprintf(f, |
166 | "\n[Service]\n" | |
167 | "Type=oneshot\n" | |
168 | "RemainAfterExit=yes\n" | |
90724929 | 169 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
260ab287 | 170 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 171 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
8eea8687 | 172 | name, u, strempty(password), strempty(filtered), |
e23a0ce8 LP |
173 | name); |
174 | ||
8c11d3c1 | 175 | if (tmp) |
e23a0ce8 | 176 | fprintf(f, |
50109038 | 177 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
1d3399e6 | 178 | name); |
e23a0ce8 | 179 | |
8c11d3c1 | 180 | if (swap) |
e23a0ce8 | 181 | fprintf(f, |
50109038 | 182 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
1d3399e6 | 183 | name); |
e23a0ce8 | 184 | |
4652c56c ZJS |
185 | r = fflush_and_check(f); |
186 | if (r < 0) | |
187 | return log_error_errno(r, "Failed to write file %s: %m", p); | |
e23a0ce8 | 188 | |
155da457 | 189 | if (!noauto) { |
b559616f ZJS |
190 | r = generator_add_symlink(arg_dest, d, "wants", n); |
191 | if (r < 0) | |
192 | return r; | |
e23a0ce8 | 193 | |
b559616f ZJS |
194 | r = generator_add_symlink(arg_dest, "cryptsetup.target", |
195 | nofail ? "wants" : "requires", n); | |
196 | if (r < 0) | |
197 | return r; | |
f7f21d33 | 198 | } |
74715b82 | 199 | |
b559616f ZJS |
200 | dmname = strjoina("dev-mapper-", e, ".device"); |
201 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
202 | if (r < 0) | |
203 | return r; | |
74715b82 | 204 | |
68395007 | 205 | if (!noauto && !nofail) { |
a6fb0dc1 | 206 | r = write_drop_in(arg_dest, dmname, 90, "device-timeout", |
8eea8687 ZJS |
207 | "# Automatically generated by systemd-cryptsetup-generator \n\n" |
208 | "[Unit]\nJobTimeoutSec=0"); | |
23bbb0de MS |
209 | if (r < 0) |
210 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
68395007 HH |
211 | } |
212 | ||
24a988e9 | 213 | return 0; |
e23a0ce8 LP |
214 | } |
215 | ||
0fa9e53d JJ |
216 | static void free_arg_disks(void) { |
217 | crypto_device *d; | |
218 | ||
219 | while ((d = hashmap_steal_first(arg_disks))) { | |
220 | free(d->uuid); | |
6cd5b12a | 221 | free(d->keyfile); |
baade8cc | 222 | free(d->name); |
0fa9e53d JJ |
223 | free(d->options); |
224 | free(d); | |
225 | } | |
226 | ||
227 | hashmap_free(arg_disks); | |
228 | } | |
229 | ||
230 | static crypto_device *get_crypto_device(const char *uuid) { | |
231 | int r; | |
232 | crypto_device *d; | |
233 | ||
234 | assert(uuid); | |
235 | ||
236 | d = hashmap_get(arg_disks, uuid); | |
237 | if (!d) { | |
238 | d = new0(struct crypto_device, 1); | |
239 | if (!d) | |
240 | return NULL; | |
241 | ||
242 | d->create = false; | |
baade8cc | 243 | d->keyfile = d->options = d->name = NULL; |
0fa9e53d JJ |
244 | |
245 | d->uuid = strdup(uuid); | |
6b430fdb ZJS |
246 | if (!d->uuid) |
247 | return mfree(d); | |
0fa9e53d JJ |
248 | |
249 | r = hashmap_put(arg_disks, d->uuid, d); | |
250 | if (r < 0) { | |
251 | free(d->uuid); | |
6b430fdb | 252 | return mfree(d); |
0fa9e53d JJ |
253 | } |
254 | } | |
255 | ||
256 | return d; | |
257 | } | |
258 | ||
96287a49 | 259 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 260 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
261 | crypto_device *d; |
262 | int r; | |
66a78c2b | 263 | |
1d84ad94 | 264 | if (streq(key, "luks")) { |
059cb385 | 265 | |
1d84ad94 | 266 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 267 | if (r < 0) |
1d84ad94 | 268 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
269 | else |
270 | arg_enabled = r; | |
66a78c2b | 271 | |
1d84ad94 | 272 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 273 | |
1d84ad94 | 274 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 275 | if (r < 0) |
1d84ad94 | 276 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
277 | else |
278 | arg_read_crypttab = r; | |
66a78c2b | 279 | |
1d84ad94 LP |
280 | } else if (streq(key, "luks.uuid")) { |
281 | ||
282 | if (proc_cmdline_value_missing(key, value)) | |
283 | return 0; | |
66a78c2b | 284 | |
0fa9e53d JJ |
285 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
286 | if (!d) | |
141a79f4 | 287 | return log_oom(); |
66a78c2b | 288 | |
0fa9e53d JJ |
289 | d->create = arg_whitelist = true; |
290 | ||
1d84ad94 LP |
291 | } else if (streq(key, "luks.options")) { |
292 | ||
293 | if (proc_cmdline_value_missing(key, value)) | |
294 | return 0; | |
66a78c2b | 295 | |
0fa9e53d JJ |
296 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
297 | if (r == 2) { | |
298 | d = get_crypto_device(uuid); | |
299 | if (!d) | |
300 | return log_oom(); | |
301 | ||
1d84ad94 | 302 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 303 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 304 | return log_oom(); |
66a78c2b | 305 | |
1d84ad94 LP |
306 | } else if (streq(key, "luks.key")) { |
307 | ||
308 | if (proc_cmdline_value_missing(key, value)) | |
309 | return 0; | |
66a78c2b | 310 | |
6cd5b12a JJ |
311 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
312 | if (r == 2) { | |
313 | d = get_crypto_device(uuid); | |
314 | if (!d) | |
315 | return log_oom(); | |
316 | ||
1d84ad94 | 317 | free_and_replace(d->keyfile, uuid_value); |
c802a730 | 318 | } else if (free_and_strdup(&arg_default_keyfile, value) < 0) |
141a79f4 | 319 | return log_oom(); |
7ab064a6 | 320 | |
1d84ad94 LP |
321 | } else if (streq(key, "luks.name")) { |
322 | ||
323 | if (proc_cmdline_value_missing(key, value)) | |
324 | return 0; | |
baade8cc JJ |
325 | |
326 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
327 | if (r == 2) { | |
328 | d = get_crypto_device(uuid); | |
329 | if (!d) | |
330 | return log_oom(); | |
331 | ||
332 | d->create = arg_whitelist = true; | |
333 | ||
334 | free(d->name); | |
335 | d->name = uuid_value; | |
336 | uuid_value = NULL; | |
337 | } else | |
338 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 339 | } |
66a78c2b | 340 | |
24a988e9 | 341 | return 0; |
66a78c2b LP |
342 | } |
343 | ||
0fa9e53d JJ |
344 | static int add_crypttab_devices(void) { |
345 | struct stat st; | |
346 | unsigned crypttab_line = 0; | |
7fd1b19b | 347 | _cleanup_fclose_ FILE *f = NULL; |
e23a0ce8 | 348 | |
0fa9e53d JJ |
349 | if (!arg_read_crypttab) |
350 | return 0; | |
351 | ||
352 | f = fopen("/etc/crypttab", "re"); | |
353 | if (!f) { | |
354 | if (errno != ENOENT) | |
355 | log_error_errno(errno, "Failed to open /etc/crypttab: %m"); | |
356 | return 0; | |
e23a0ce8 LP |
357 | } |
358 | ||
0fa9e53d JJ |
359 | if (fstat(fileno(f), &st) < 0) { |
360 | log_error_errno(errno, "Failed to stat /etc/crypttab: %m"); | |
361 | return 0; | |
362 | } | |
e23a0ce8 | 363 | |
0fa9e53d JJ |
364 | for (;;) { |
365 | int r, k; | |
366 | char line[LINE_MAX], *l, *uuid; | |
367 | crypto_device *d = NULL; | |
368 | _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL; | |
4c12626c | 369 | |
0fa9e53d JJ |
370 | if (!fgets(line, sizeof(line), f)) |
371 | break; | |
66a78c2b | 372 | |
0fa9e53d | 373 | crypttab_line++; |
141a79f4 | 374 | |
0fa9e53d JJ |
375 | l = strstrip(line); |
376 | if (*l == '#' || *l == 0) | |
377 | continue; | |
66a78c2b | 378 | |
0fa9e53d JJ |
379 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options); |
380 | if (k < 2 || k > 4) { | |
381 | log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line); | |
382 | continue; | |
383 | } | |
66a78c2b | 384 | |
0fa9e53d JJ |
385 | uuid = startswith(device, "UUID="); |
386 | if (!uuid) | |
387 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
388 | if (!uuid) | |
389 | uuid = startswith(name, "luks-"); | |
390 | if (uuid) | |
391 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 392 | |
0fa9e53d JJ |
393 | if (arg_whitelist && !d) { |
394 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
395 | continue; | |
8973790e LP |
396 | } |
397 | ||
0fa9e53d JJ |
398 | r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options); |
399 | if (r < 0) | |
400 | return r; | |
8973790e | 401 | |
0fa9e53d JJ |
402 | if (d) |
403 | d->create = false; | |
404 | } | |
8973790e | 405 | |
0fa9e53d JJ |
406 | return 0; |
407 | } | |
66a78c2b | 408 | |
0fa9e53d JJ |
409 | static int add_proc_cmdline_devices(void) { |
410 | int r; | |
411 | Iterator i; | |
412 | crypto_device *d; | |
66a78c2b | 413 | |
0fa9e53d JJ |
414 | HASHMAP_FOREACH(d, arg_disks, i) { |
415 | const char *options; | |
baade8cc | 416 | _cleanup_free_ char *device = NULL; |
66a78c2b | 417 | |
0fa9e53d JJ |
418 | if (!d->create) |
419 | continue; | |
e23a0ce8 | 420 | |
baade8cc JJ |
421 | if (!d->name) { |
422 | d->name = strappend("luks-", d->uuid); | |
423 | if (!d->name) | |
424 | return log_oom(); | |
425 | } | |
e23a0ce8 | 426 | |
0fa9e53d JJ |
427 | device = strappend("UUID=", d->uuid); |
428 | if (!device) | |
429 | return log_oom(); | |
7ab064a6 | 430 | |
0fa9e53d JJ |
431 | if (d->options) |
432 | options = d->options; | |
433 | else if (arg_default_options) | |
434 | options = arg_default_options; | |
435 | else | |
436 | options = "timeout=0"; | |
141a79f4 | 437 | |
baade8cc | 438 | r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options); |
0fa9e53d JJ |
439 | if (r < 0) |
440 | return r; | |
e23a0ce8 LP |
441 | } |
442 | ||
0fa9e53d JJ |
443 | return 0; |
444 | } | |
e23a0ce8 | 445 | |
0fa9e53d | 446 | int main(int argc, char *argv[]) { |
5f4bfe56 | 447 | int r; |
e23a0ce8 | 448 | |
0fa9e53d JJ |
449 | if (argc > 1 && argc != 4) { |
450 | log_error("This program takes three or no arguments."); | |
451 | return EXIT_FAILURE; | |
452 | } | |
e23a0ce8 | 453 | |
0fa9e53d JJ |
454 | if (argc > 1) |
455 | arg_dest = argv[1]; | |
e23a0ce8 | 456 | |
0fa9e53d JJ |
457 | log_set_target(LOG_TARGET_SAFE); |
458 | log_parse_environment(); | |
459 | log_open(); | |
e2cb60fa | 460 | |
0fa9e53d | 461 | umask(0022); |
e23a0ce8 | 462 | |
0fa9e53d | 463 | arg_disks = hashmap_new(&string_hash_ops); |
5f4bfe56 LP |
464 | if (!arg_disks) { |
465 | r = log_oom(); | |
466 | goto finish; | |
467 | } | |
7ab064a6 | 468 | |
1d84ad94 | 469 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
0fa9e53d | 470 | if (r < 0) { |
5f4bfe56 LP |
471 | log_warning_errno(r, "Failed to parse kernel command line: %m"); |
472 | goto finish; | |
0fa9e53d | 473 | } |
7ab064a6 | 474 | |
0fa9e53d | 475 | if (!arg_enabled) { |
5f4bfe56 LP |
476 | r = 0; |
477 | goto finish; | |
e23a0ce8 LP |
478 | } |
479 | ||
5f4bfe56 LP |
480 | r = add_crypttab_devices(); |
481 | if (r < 0) | |
482 | goto finish; | |
0fa9e53d | 483 | |
5f4bfe56 LP |
484 | r = add_proc_cmdline_devices(); |
485 | if (r < 0) | |
486 | goto finish; | |
0fa9e53d | 487 | |
5f4bfe56 | 488 | r = 0; |
141a79f4 | 489 | |
5f4bfe56 | 490 | finish: |
0fa9e53d JJ |
491 | free_arg_disks(); |
492 | free(arg_default_options); | |
493 | free(arg_default_keyfile); | |
141a79f4 | 494 | |
5f4bfe56 | 495 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
e23a0ce8 | 496 | } |