X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcryptsetup%2Fcryptsetup-generator.c;h=5d234e6a5a30d4bebe0e07c6d7ac195cc93f235c;hp=dfdca1e47b4e3ce82ddf7fd6027a5f89e748b5d8;hb=71e4e1258436e7e81d772aed52a02bb5d9c87cb8;hpb=9542239eaf481decca6c254dffb3f2b4e716545e diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index dfdca1e47..5d234e6a5 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -19,52 +19,34 @@ along with systemd; If not, see . ***/ -#include #include -#include +#include "dropin.h" +#include "generator.h" +#include "hashmap.h" #include "log.h" -#include "util.h" -#include "unit-name.h" #include "mkdir.h" +#include "path-util.h" +#include "fstab-util.h" #include "strv.h" -#include "fileio.h" +#include "unit-name.h" +#include "util.h" + +typedef struct crypto_device { + char *uuid; + char *keyfile; + char *name; + char *options; + bool create; +} crypto_device; static const char *arg_dest = "/tmp"; static bool arg_enabled = true; static bool arg_read_crypttab = true; -static char **arg_disks = NULL; -static char **arg_options = NULL; -static char *arg_keyfile = NULL; - -static bool has_option(const char *haystack, const char *needle) { - const char *f = haystack; - size_t l; - - assert(needle); - - if (!haystack) - return false; - - l = strlen(needle); - - while ((f = strstr(f, needle))) { - - if (f > haystack && f[-1] != ',') { - f++; - continue; - } - - if (f[l] != 0 && f[l] != ',') { - f++; - continue; - } - - return true; - } - - return false; -} +static bool arg_whitelist = false; +static Hashmap *arg_disks = NULL; +static char *arg_default_options = NULL; +static char *arg_default_keyfile = NULL; static int create_disk( const char *name, @@ -72,7 +54,8 @@ static int create_disk( const char *password, const char *options) { - _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL; + _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL, + *filtered = NULL; _cleanup_fclose_ FILE *f = NULL; bool noauto, nofail, tmp, swap; char *from; @@ -81,10 +64,10 @@ static int create_disk( assert(name); assert(device); - noauto = has_option(options, "noauto"); - nofail = has_option(options, "nofail"); - tmp = has_option(options, "tmp"); - swap = has_option(options, "swap"); + noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); + nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); + tmp = fstab_test_option(options, "tmp\0"); + swap = fstab_test_option(options, "swap\0"); if (tmp && swap) { log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name); @@ -112,10 +95,8 @@ static int create_disk( return log_oom(); f = fopen(p, "wxe"); - if (!f) { - log_error("Failed to create unit file %s: %m", p); - return -errno; - } + if (!f) + return log_error_errno(errno, "Failed to create unit file %s: %m", p); fputs( "# Automatically generated by systemd-cryptsetup-generator\n\n" @@ -127,7 +108,7 @@ static int create_disk( "Conflicts=umount.target\n" "BindsTo=dev-mapper-%i.device\n" "IgnoreOnIsolate=true\n" - "After=systemd-readahead-collect.service systemd-readahead-replay.service cryptsetup-pre.target\n", + "After=cryptsetup-pre.target\n", f); if (!nofail) @@ -144,16 +125,19 @@ static int create_disk( if (!uu) return log_oom(); - if (is_device_path(uu)) { - _cleanup_free_ char *dd; + if (!path_equal(uu, "/dev/null")) { + + if (is_device_path(uu)) { + _cleanup_free_ char *dd; - dd = unit_name_from_path(uu, ".device"); - if (!dd) - return log_oom(); + dd = unit_name_from_path(uu, ".device"); + if (!dd) + return log_oom(); - fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); - } else - fprintf(f, "RequiresMountsFor=%s\n", password); + fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); + } else + fprintf(f, "RequiresMountsFor=%s\n", password); + } } } @@ -168,6 +152,10 @@ static int create_disk( "RequiresMountsFor=%s\n", u); + r = generator_write_timeouts(arg_dest, device, name, options, &filtered); + if (r < 0) + return r; + fprintf(f, "\n[Service]\n" "Type=oneshot\n" @@ -175,7 +163,7 @@ static int create_disk( "TimeoutSec=0\n" /* the binary handles timeouts anyway */ "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", - name, u, strempty(password), strempty(options), + name, u, strempty(password), strempty(filtered), name); if (tmp) @@ -189,12 +177,10 @@ static int create_disk( name); fflush(f); - if (ferror(f)) { - log_error("Failed to write file %s: %m", p); - return -errno; - } + if (ferror(f)) + return log_error_errno(errno, "Failed to write file %s: %m", p); - from = strappenda("../", n); + from = strjoina("../", n); if (!noauto) { @@ -203,10 +189,8 @@ static int create_disk( return log_oom(); mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) { - log_error("Failed to create symlink %s: %m", to); - return -errno; - } + if (symlink(from, to) < 0) + return log_error_errno(errno, "Failed to create symlink %s: %m", to); free(to); if (!nofail) @@ -217,10 +201,8 @@ static int create_disk( return log_oom(); mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) { - log_error("Failed to create symlink %s: %m", to); - return -errno; - } + if (symlink(from, to) < 0) + return log_error_errno(errno, "Failed to create symlink %s: %m", to); } free(to); @@ -229,34 +211,75 @@ static int create_disk( return log_oom(); mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) { - log_error("Failed to create symlink %s: %m", to); - return -errno; - } + if (symlink(from, to) < 0) + return log_error_errno(errno, "Failed to create symlink %s: %m", to); if (!noauto && !nofail) { - - free(p); - p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL); - if (!p) + _cleanup_free_ char *dmname; + dmname = strjoin("dev-mapper-", e, ".device", NULL); + if (!dmname) return log_oom(); - mkdir_parents_label(p, 0755); - r = write_string_file(p, - "# Automatically generated by systemd-cryptsetup-generator\n\n" - "[Unit]\n" - "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */ + r = write_drop_in(arg_dest, dmname, 90, "device-timeout", + "# Automatically generated by systemd-cryptsetup-generator \n\n" + "[Unit]\nJobTimeoutSec=0"); + if (r < 0) + return log_error_errno(r, "Failed to write device drop-in: %m"); + } + + return 0; +} + +static void free_arg_disks(void) { + crypto_device *d; + + while ((d = hashmap_steal_first(arg_disks))) { + free(d->uuid); + free(d->keyfile); + free(d->name); + free(d->options); + free(d); + } + + hashmap_free(arg_disks); +} + +static crypto_device *get_crypto_device(const char *uuid) { + int r; + crypto_device *d; + + assert(uuid); + + d = hashmap_get(arg_disks, uuid); + if (!d) { + d = new0(struct crypto_device, 1); + if (!d) + return NULL; + + d->create = false; + d->keyfile = d->options = d->name = NULL; + + d->uuid = strdup(uuid); + if (!d->uuid) { + free(d); + return NULL; + } + + r = hashmap_put(arg_disks, d->uuid, d); if (r < 0) { - log_error("Failed to write device drop-in: %s", strerror(-r)); - return r; + free(d->uuid); + free(d); + return NULL; } } - return 0; + return d; } static int parse_proc_cmdline_item(const char *key, const char *value) { int r; + crypto_device *d; + _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; if (STR_IN_SET(key, "luks", "rd.luks") && value) { @@ -276,233 +299,207 @@ static int parse_proc_cmdline_item(const char *key, const char *value) { } else if (STR_IN_SET(key, "luks.uuid", "rd.luks.uuid") && value) { - if (strv_extend(&arg_disks, value) < 0) + d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); + if (!d) return log_oom(); + d->create = arg_whitelist = true; + } else if (STR_IN_SET(key, "luks.options", "rd.luks.options") && value) { - if (strv_extend(&arg_options, value) < 0) + r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); + if (r == 2) { + d = get_crypto_device(uuid); + if (!d) + return log_oom(); + + free(d->options); + d->options = uuid_value; + uuid_value = NULL; + } else if (free_and_strdup(&arg_default_options, value) < 0) return log_oom(); } else if (STR_IN_SET(key, "luks.key", "rd.luks.key") && value) { - free(arg_keyfile); - arg_keyfile = strdup(value); - if (!arg_keyfile) + r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); + if (r == 2) { + d = get_crypto_device(uuid); + if (!d) + return log_oom(); + + free(d->keyfile); + d->keyfile = uuid_value; + uuid_value = NULL; + } else if (free_and_strdup(&arg_default_keyfile, value)) return log_oom(); - } else if (startswith(key, "luks.") || startswith(key, "rd.luks.")) - log_warning("Unknown kernel switch %s. Ignoring.", key); + } else if (STR_IN_SET(key, "luks.name", "rd.luks.name") && value) { - return 0; -} + r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); + if (r == 2) { + d = get_crypto_device(uuid); + if (!d) + return log_oom(); -int main(int argc, char *argv[]) { - _cleanup_strv_free_ char **disks_done = NULL; - _cleanup_fclose_ FILE *f = NULL; - unsigned n = 0; - int r = EXIT_FAILURE, r2 = EXIT_FAILURE; - char **i; + d->create = arg_whitelist = true; + + free(d->name); + d->name = uuid_value; + uuid_value = NULL; + } else + log_warning("Failed to parse luks name switch %s. Ignoring.", value); - if (argc > 1 && argc != 4) { - log_error("This program takes three or no arguments."); - return EXIT_FAILURE; } - if (argc > 1) - arg_dest = argv[1]; + return 0; +} - log_set_target(LOG_TARGET_SAFE); - log_parse_environment(); - log_open(); +static int add_crypttab_devices(void) { + struct stat st; + unsigned crypttab_line = 0; + _cleanup_fclose_ FILE *f = NULL; - umask(0022); + if (!arg_read_crypttab) + return 0; - if (parse_proc_cmdline(parse_proc_cmdline_item) < 0) - goto cleanup; + f = fopen("/etc/crypttab", "re"); + if (!f) { + if (errno != ENOENT) + log_error_errno(errno, "Failed to open /etc/crypttab: %m"); + return 0; + } - if (!arg_enabled) { - r = r2 = EXIT_SUCCESS; - goto cleanup; + if (fstat(fileno(f), &st) < 0) { + log_error_errno(errno, "Failed to stat /etc/crypttab: %m"); + return 0; } - strv_uniq(arg_disks); + for (;;) { + int r, k; + char line[LINE_MAX], *l, *uuid; + crypto_device *d = NULL; + _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL; - if (arg_read_crypttab) { - struct stat st; + if (!fgets(line, sizeof(line), f)) + break; - f = fopen("/etc/crypttab", "re"); - if (!f) { - if (errno == ENOENT) - r = EXIT_SUCCESS; - else - log_error("Failed to open /etc/crypttab: %m"); + crypttab_line++; - goto next; - } + l = strstrip(line); + if (*l == '#' || *l == 0) + continue; - if (fstat(fileno(f), &st) < 0) { - log_error("Failed to stat /etc/crypttab: %m"); - goto next; + k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options); + if (k < 2 || k > 4) { + log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line); + continue; } - /* If we readd support for specifying passphrases - * directly in crypttabe we should upgrade the warning - * below, though possibly only if a passphrase is - * specified directly. */ - if (st.st_mode & 0005) - log_debug("/etc/crypttab is world-readable. This is usually not a good idea."); + uuid = startswith(device, "UUID="); + if (!uuid) + uuid = path_startswith(device, "/dev/disk/by-uuid/"); + if (!uuid) + uuid = startswith(name, "luks-"); + if (uuid) + d = hashmap_get(arg_disks, uuid); - for (;;) { - char line[LINE_MAX], *l; - _cleanup_free_ char *name = NULL, *device = NULL, *password = NULL, *options = NULL; - int k; + if (arg_whitelist && !d) { + log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); + continue; + } - if (!fgets(line, sizeof(line), f)) - break; + r = create_disk(name, device, keyfile, (d && d->options) ? d->options : options); + if (r < 0) + return r; - n++; + if (d) + d->create = false; + } - l = strstrip(line); - if (*l == '#' || *l == 0) - continue; + return 0; +} - k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &password, &options); - if (k < 2 || k > 4) { - log_error("Failed to parse /etc/crypttab:%u, ignoring.", n); - continue; - } +static int add_proc_cmdline_devices(void) { + int r; + Iterator i; + crypto_device *d; - /* - If options are specified on the kernel commandline, let them override - the ones from crypttab. - */ - STRV_FOREACH(i, arg_options) { - _cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL; - const char *p = *i; - - k = sscanf(p, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options); - if (k == 2 && streq(proc_uuid, device + 5)) { - free(options); - options = strdup(p); - if (!proc_options) { - log_oom(); - goto cleanup; - } - } - } + HASHMAP_FOREACH(d, arg_disks, i) { + const char *options; + _cleanup_free_ char *device = NULL; - if (arg_disks) { - /* - If luks UUIDs are specified on the kernel command line, use them as a filter - for /etc/crypttab and only generate units for those. - */ - STRV_FOREACH(i, arg_disks) { - _cleanup_free_ char *proc_device = NULL, *proc_name = NULL; - const char *p = *i; - - if (startswith(p, "luks-")) - p += 5; - - proc_name = strappend("luks-", p); - proc_device = strappend("UUID=", p); - - if (!proc_name || !proc_device) { - log_oom(); - goto cleanup; - } - - if (streq(proc_device, device) || streq(proc_name, name)) { - if (create_disk(name, device, password, options) < 0) - goto cleanup; - - if (strv_extend(&disks_done, p) < 0) { - log_oom(); - goto cleanup; - } - } - } - } else if (create_disk(name, device, password, options) < 0) - goto cleanup; + if (!d->create) + continue; + if (!d->name) { + d->name = strappend("luks-", d->uuid); + if (!d->name) + return log_oom(); } - } - r = EXIT_SUCCESS; + device = strappend("UUID=", d->uuid); + if (!device) + return log_oom(); -next: - STRV_FOREACH(i, arg_disks) { - /* - Generate units for those UUIDs, which were specified - on the kernel command line and not yet written. - */ + if (d->options) + options = d->options; + else if (arg_default_options) + options = arg_default_options; + else + options = "timeout=0"; - _cleanup_free_ char *name = NULL, *device = NULL, *options = NULL; - const char *p = *i; + r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, options); + if (r < 0) + return r; + } - if (startswith(p, "luks-")) - p += 5; + return 0; +} - if (strv_contains(disks_done, p)) - continue; +int main(int argc, char *argv[]) { + int r = EXIT_FAILURE; - name = strappend("luks-", p); - device = strappend("UUID=", p); + if (argc > 1 && argc != 4) { + log_error("This program takes three or no arguments."); + return EXIT_FAILURE; + } - if (!name || !device) { - log_oom(); - goto cleanup; - } + if (argc > 1) + arg_dest = argv[1]; - if (arg_options) { - /* - If options are specified on the kernel commandline, use them. - */ - char **j; - - STRV_FOREACH(j, arg_options) { - _cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL; - const char *s = *j; - int k; - - k = sscanf(s, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options); - if (k == 2) { - if (streq(proc_uuid, device + 5)) { - free(options); - options = proc_options; - proc_options = NULL; - } - } else if (!options) { - /* - Fall back to options without a specified UUID - */ - options = strdup(s); - if (!options) { - log_oom(); - goto cleanup; - }; - } - } - } + log_set_target(LOG_TARGET_SAFE); + log_parse_environment(); + log_open(); - if (!options) { - options = strdup("timeout=0"); - if (!options) { - log_oom(); - goto cleanup; - } - } + umask(0022); + + arg_disks = hashmap_new(&string_hash_ops); + if (!arg_disks) + goto cleanup; + + r = parse_proc_cmdline(parse_proc_cmdline_item); + if (r < 0) { + log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); + r = EXIT_FAILURE; + } - if (create_disk(name, device, arg_keyfile, options) < 0) - goto cleanup; + if (!arg_enabled) { + r = EXIT_SUCCESS; + goto cleanup; } - r2 = EXIT_SUCCESS; + if (add_crypttab_devices() < 0) + goto cleanup; + + if (add_proc_cmdline_devices() < 0) + goto cleanup; + + r = EXIT_SUCCESS; cleanup: - strv_free(arg_disks); - strv_free(arg_options); - free(arg_keyfile); + free_arg_disks(); + free(arg_default_options); + free(arg_default_keyfile); - return r != EXIT_SUCCESS ? r : r2; + return r; }