]>
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" | |
112 | "BindsTo=dev-mapper-%i.device\n" | |
113 | "IgnoreOnIsolate=true\n" | |
114 | "After=cryptsetup-pre.target\n", | |
115 | f); | |
e23a0ce8 | 116 | |
155da457 LP |
117 | if (!nofail) |
118 | fprintf(f, | |
119 | "Before=cryptsetup.target\n"); | |
120 | ||
ceca9501 | 121 | if (password) { |
744198e9 | 122 | if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random")) |
4b61c875 | 123 | fputs_unlocked("After=systemd-random-seed.service\n", f); |
b559616f | 124 | else if (!STR_IN_SET(password, "-", "none")) { |
744198e9 LP |
125 | _cleanup_free_ char *uu; |
126 | ||
127 | uu = fstab_node_to_udev_node(password); | |
128 | if (!uu) | |
66a5dbdf DR |
129 | return log_oom(); |
130 | ||
bde29068 | 131 | if (!path_equal(uu, "/dev/null")) { |
744198e9 | 132 | |
bde29068 | 133 | if (is_device_path(uu)) { |
7410616c | 134 | _cleanup_free_ char *dd = NULL; |
66a5dbdf | 135 | |
7410616c LP |
136 | r = unit_name_from_path(uu, ".device", &dd); |
137 | if (r < 0) | |
138 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
bde29068 LP |
139 | |
140 | fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); | |
141 | } else | |
142 | fprintf(f, "RequiresMountsFor=%s\n", password); | |
143 | } | |
66a5dbdf | 144 | } |
ceca9501 | 145 | } |
e23a0ce8 | 146 | |
a6f8786a | 147 | if (is_device_path(u)) { |
9ece938a TW |
148 | fprintf(f, |
149 | "BindsTo=%s\n" | |
150 | "After=%s\n" | |
151 | "Before=umount.target\n", | |
152 | d, d); | |
a6f8786a MFO |
153 | |
154 | if (swap) | |
4b61c875 LP |
155 | fputs_unlocked("Before=dev-mapper-%i.swap\n", |
156 | f); | |
a6f8786a | 157 | } else |
9ece938a TW |
158 | fprintf(f, |
159 | "RequiresMountsFor=%s\n", | |
160 | u); | |
161 | ||
8eea8687 ZJS |
162 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
163 | if (r < 0) | |
164 | return r; | |
165 | ||
e23a0ce8 LP |
166 | fprintf(f, |
167 | "\n[Service]\n" | |
168 | "Type=oneshot\n" | |
169 | "RemainAfterExit=yes\n" | |
90724929 | 170 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
260ab287 | 171 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 172 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
8eea8687 | 173 | name, u, strempty(password), strempty(filtered), |
e23a0ce8 LP |
174 | name); |
175 | ||
8c11d3c1 | 176 | if (tmp) |
e23a0ce8 | 177 | fprintf(f, |
50109038 | 178 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
1d3399e6 | 179 | name); |
e23a0ce8 | 180 | |
8c11d3c1 | 181 | if (swap) |
e23a0ce8 | 182 | fprintf(f, |
50109038 | 183 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
1d3399e6 | 184 | name); |
e23a0ce8 | 185 | |
4652c56c ZJS |
186 | r = fflush_and_check(f); |
187 | if (r < 0) | |
188 | return log_error_errno(r, "Failed to write file %s: %m", p); | |
e23a0ce8 | 189 | |
155da457 | 190 | if (!noauto) { |
b559616f ZJS |
191 | r = generator_add_symlink(arg_dest, d, "wants", n); |
192 | if (r < 0) | |
193 | return r; | |
e23a0ce8 | 194 | |
b559616f ZJS |
195 | r = generator_add_symlink(arg_dest, "cryptsetup.target", |
196 | nofail ? "wants" : "requires", n); | |
197 | if (r < 0) | |
198 | return r; | |
f7f21d33 | 199 | } |
74715b82 | 200 | |
b559616f ZJS |
201 | dmname = strjoina("dev-mapper-", e, ".device"); |
202 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
203 | if (r < 0) | |
204 | return r; | |
74715b82 | 205 | |
68395007 | 206 | if (!noauto && !nofail) { |
a6fb0dc1 | 207 | r = write_drop_in(arg_dest, dmname, 90, "device-timeout", |
8eea8687 ZJS |
208 | "# Automatically generated by systemd-cryptsetup-generator \n\n" |
209 | "[Unit]\nJobTimeoutSec=0"); | |
23bbb0de MS |
210 | if (r < 0) |
211 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
68395007 HH |
212 | } |
213 | ||
24a988e9 | 214 | return 0; |
e23a0ce8 LP |
215 | } |
216 | ||
0fa9e53d JJ |
217 | static void free_arg_disks(void) { |
218 | crypto_device *d; | |
219 | ||
220 | while ((d = hashmap_steal_first(arg_disks))) { | |
221 | free(d->uuid); | |
6cd5b12a | 222 | free(d->keyfile); |
baade8cc | 223 | free(d->name); |
0fa9e53d JJ |
224 | free(d->options); |
225 | free(d); | |
226 | } | |
227 | ||
228 | hashmap_free(arg_disks); | |
229 | } | |
230 | ||
231 | static crypto_device *get_crypto_device(const char *uuid) { | |
232 | int r; | |
233 | crypto_device *d; | |
234 | ||
235 | assert(uuid); | |
236 | ||
237 | d = hashmap_get(arg_disks, uuid); | |
238 | if (!d) { | |
239 | d = new0(struct crypto_device, 1); | |
240 | if (!d) | |
241 | return NULL; | |
242 | ||
243 | d->create = false; | |
baade8cc | 244 | d->keyfile = d->options = d->name = NULL; |
0fa9e53d JJ |
245 | |
246 | d->uuid = strdup(uuid); | |
6b430fdb ZJS |
247 | if (!d->uuid) |
248 | return mfree(d); | |
0fa9e53d JJ |
249 | |
250 | r = hashmap_put(arg_disks, d->uuid, d); | |
251 | if (r < 0) { | |
252 | free(d->uuid); | |
6b430fdb | 253 | return mfree(d); |
0fa9e53d JJ |
254 | } |
255 | } | |
256 | ||
257 | return d; | |
258 | } | |
259 | ||
96287a49 | 260 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 261 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
262 | crypto_device *d; |
263 | int r; | |
66a78c2b | 264 | |
1d84ad94 | 265 | if (streq(key, "luks")) { |
059cb385 | 266 | |
1d84ad94 | 267 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 268 | if (r < 0) |
1d84ad94 | 269 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
270 | else |
271 | arg_enabled = r; | |
66a78c2b | 272 | |
1d84ad94 | 273 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 274 | |
1d84ad94 | 275 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 276 | if (r < 0) |
1d84ad94 | 277 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
278 | else |
279 | arg_read_crypttab = r; | |
66a78c2b | 280 | |
1d84ad94 LP |
281 | } else if (streq(key, "luks.uuid")) { |
282 | ||
283 | if (proc_cmdline_value_missing(key, value)) | |
284 | return 0; | |
66a78c2b | 285 | |
0fa9e53d JJ |
286 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
287 | if (!d) | |
141a79f4 | 288 | return log_oom(); |
66a78c2b | 289 | |
0fa9e53d JJ |
290 | d->create = arg_whitelist = true; |
291 | ||
1d84ad94 LP |
292 | } else if (streq(key, "luks.options")) { |
293 | ||
294 | if (proc_cmdline_value_missing(key, value)) | |
295 | return 0; | |
66a78c2b | 296 | |
0fa9e53d JJ |
297 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
298 | if (r == 2) { | |
299 | d = get_crypto_device(uuid); | |
300 | if (!d) | |
301 | return log_oom(); | |
302 | ||
1d84ad94 | 303 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 304 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 305 | return log_oom(); |
66a78c2b | 306 | |
1d84ad94 LP |
307 | } else if (streq(key, "luks.key")) { |
308 | ||
309 | if (proc_cmdline_value_missing(key, value)) | |
310 | return 0; | |
66a78c2b | 311 | |
6cd5b12a JJ |
312 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
313 | if (r == 2) { | |
314 | d = get_crypto_device(uuid); | |
315 | if (!d) | |
316 | return log_oom(); | |
317 | ||
1d84ad94 | 318 | free_and_replace(d->keyfile, uuid_value); |
c802a730 | 319 | } else if (free_and_strdup(&arg_default_keyfile, value) < 0) |
141a79f4 | 320 | return log_oom(); |
7ab064a6 | 321 | |
1d84ad94 LP |
322 | } else if (streq(key, "luks.name")) { |
323 | ||
324 | if (proc_cmdline_value_missing(key, value)) | |
325 | return 0; | |
baade8cc JJ |
326 | |
327 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
328 | if (r == 2) { | |
329 | d = get_crypto_device(uuid); | |
330 | if (!d) | |
331 | return log_oom(); | |
332 | ||
333 | d->create = arg_whitelist = true; | |
334 | ||
335 | free(d->name); | |
336 | d->name = uuid_value; | |
337 | uuid_value = NULL; | |
338 | } else | |
339 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 340 | } |
66a78c2b | 341 | |
24a988e9 | 342 | return 0; |
66a78c2b LP |
343 | } |
344 | ||
0fa9e53d JJ |
345 | static int add_crypttab_devices(void) { |
346 | struct stat st; | |
347 | unsigned crypttab_line = 0; | |
7fd1b19b | 348 | _cleanup_fclose_ FILE *f = NULL; |
e23a0ce8 | 349 | |
0fa9e53d JJ |
350 | if (!arg_read_crypttab) |
351 | return 0; | |
352 | ||
353 | f = fopen("/etc/crypttab", "re"); | |
354 | if (!f) { | |
355 | if (errno != ENOENT) | |
356 | log_error_errno(errno, "Failed to open /etc/crypttab: %m"); | |
357 | return 0; | |
e23a0ce8 LP |
358 | } |
359 | ||
0fa9e53d JJ |
360 | if (fstat(fileno(f), &st) < 0) { |
361 | log_error_errno(errno, "Failed to stat /etc/crypttab: %m"); | |
362 | return 0; | |
363 | } | |
e23a0ce8 | 364 | |
0fa9e53d JJ |
365 | for (;;) { |
366 | int r, k; | |
367 | char line[LINE_MAX], *l, *uuid; | |
368 | crypto_device *d = NULL; | |
369 | _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL; | |
4c12626c | 370 | |
0fa9e53d JJ |
371 | if (!fgets(line, sizeof(line), f)) |
372 | break; | |
66a78c2b | 373 | |
0fa9e53d | 374 | crypttab_line++; |
141a79f4 | 375 | |
0fa9e53d JJ |
376 | l = strstrip(line); |
377 | if (*l == '#' || *l == 0) | |
378 | continue; | |
66a78c2b | 379 | |
0fa9e53d JJ |
380 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options); |
381 | if (k < 2 || k > 4) { | |
382 | log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line); | |
383 | continue; | |
384 | } | |
66a78c2b | 385 | |
0fa9e53d JJ |
386 | uuid = startswith(device, "UUID="); |
387 | if (!uuid) | |
388 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
389 | if (!uuid) | |
390 | uuid = startswith(name, "luks-"); | |
391 | if (uuid) | |
392 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 393 | |
0fa9e53d JJ |
394 | if (arg_whitelist && !d) { |
395 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
396 | continue; | |
8973790e LP |
397 | } |
398 | ||
0fa9e53d JJ |
399 | r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options); |
400 | if (r < 0) | |
401 | return r; | |
8973790e | 402 | |
0fa9e53d JJ |
403 | if (d) |
404 | d->create = false; | |
405 | } | |
8973790e | 406 | |
0fa9e53d JJ |
407 | return 0; |
408 | } | |
66a78c2b | 409 | |
0fa9e53d JJ |
410 | static int add_proc_cmdline_devices(void) { |
411 | int r; | |
412 | Iterator i; | |
413 | crypto_device *d; | |
66a78c2b | 414 | |
0fa9e53d JJ |
415 | HASHMAP_FOREACH(d, arg_disks, i) { |
416 | const char *options; | |
baade8cc | 417 | _cleanup_free_ char *device = NULL; |
66a78c2b | 418 | |
0fa9e53d JJ |
419 | if (!d->create) |
420 | continue; | |
e23a0ce8 | 421 | |
baade8cc JJ |
422 | if (!d->name) { |
423 | d->name = strappend("luks-", d->uuid); | |
424 | if (!d->name) | |
425 | return log_oom(); | |
426 | } | |
e23a0ce8 | 427 | |
0fa9e53d JJ |
428 | device = strappend("UUID=", d->uuid); |
429 | if (!device) | |
430 | return log_oom(); | |
7ab064a6 | 431 | |
0fa9e53d JJ |
432 | if (d->options) |
433 | options = d->options; | |
434 | else if (arg_default_options) | |
435 | options = arg_default_options; | |
436 | else | |
437 | options = "timeout=0"; | |
141a79f4 | 438 | |
baade8cc | 439 | r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options); |
0fa9e53d JJ |
440 | if (r < 0) |
441 | return r; | |
e23a0ce8 LP |
442 | } |
443 | ||
0fa9e53d JJ |
444 | return 0; |
445 | } | |
e23a0ce8 | 446 | |
0fa9e53d | 447 | int main(int argc, char *argv[]) { |
5f4bfe56 | 448 | int r; |
e23a0ce8 | 449 | |
0fa9e53d JJ |
450 | if (argc > 1 && argc != 4) { |
451 | log_error("This program takes three or no arguments."); | |
452 | return EXIT_FAILURE; | |
453 | } | |
e23a0ce8 | 454 | |
0fa9e53d JJ |
455 | if (argc > 1) |
456 | arg_dest = argv[1]; | |
e23a0ce8 | 457 | |
0fa9e53d JJ |
458 | log_set_target(LOG_TARGET_SAFE); |
459 | log_parse_environment(); | |
460 | log_open(); | |
e2cb60fa | 461 | |
0fa9e53d | 462 | umask(0022); |
e23a0ce8 | 463 | |
0fa9e53d | 464 | arg_disks = hashmap_new(&string_hash_ops); |
5f4bfe56 LP |
465 | if (!arg_disks) { |
466 | r = log_oom(); | |
467 | goto finish; | |
468 | } | |
7ab064a6 | 469 | |
1d84ad94 | 470 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
0fa9e53d | 471 | if (r < 0) { |
5f4bfe56 LP |
472 | log_warning_errno(r, "Failed to parse kernel command line: %m"); |
473 | goto finish; | |
0fa9e53d | 474 | } |
7ab064a6 | 475 | |
0fa9e53d | 476 | if (!arg_enabled) { |
5f4bfe56 LP |
477 | r = 0; |
478 | goto finish; | |
e23a0ce8 LP |
479 | } |
480 | ||
5f4bfe56 LP |
481 | r = add_crypttab_devices(); |
482 | if (r < 0) | |
483 | goto finish; | |
0fa9e53d | 484 | |
5f4bfe56 LP |
485 | r = add_proc_cmdline_devices(); |
486 | if (r < 0) | |
487 | goto finish; | |
0fa9e53d | 488 | |
5f4bfe56 | 489 | r = 0; |
141a79f4 | 490 | |
5f4bfe56 | 491 | finish: |
0fa9e53d JJ |
492 | free_arg_disks(); |
493 | free(arg_default_options); | |
494 | free(arg_default_keyfile); | |
141a79f4 | 495 | |
5f4bfe56 | 496 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
e23a0ce8 | 497 | } |