cryptsetup-generator.c revision bde29068aa3815c88190a91e9867605a0aeaf9c4
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "log.h"
#include "util.h"
#include "unit-name.h"
#include "mkdir.h"
#include "strv.h"
#include "fileio.h"
#include "path-util.h"
static const char *arg_dest = "/tmp";
static bool arg_enabled = true;
static bool arg_read_crypttab = true;
static char **arg_options = NULL;
static char *arg_keyfile = NULL;
const char *f = haystack;
size_t l;
if (!haystack)
return false;
f++;
continue;
}
if (f[l] != 0 && f[l] != ',') {
f++;
continue;
}
return true;
}
return false;
}
static int create_disk(
const char *name,
const char *device,
const char *password,
const char *options) {
char *from;
int r;
return -EINVAL;
}
e = unit_name_escape(name);
if (!e)
return log_oom();
if (!n)
return log_oom();
if (!p)
return log_oom();
if (!u)
return log_oom();
d = unit_name_from_path(u, ".device");
if (!d)
return log_oom();
f = fopen(p, "wxe");
if (!f) {
log_error("Failed to create unit file %s: %m", p);
return -errno;
}
"# Automatically generated by systemd-cryptsetup-generator\n\n"
"[Unit]\n"
"Description=Cryptography Setup for %I\n"
"Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
"DefaultDependencies=no\n"
"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",
f);
if (!nofail)
fprintf(f,
"Before=cryptsetup.target\n");
if (password) {
fputs("After=systemd-random-seed.service\n", f);
_cleanup_free_ char *uu;
if (!uu)
return log_oom();
if (is_device_path(uu)) {
_cleanup_free_ char *dd;
if (!dd)
return log_oom();
} else
}
}
}
if (is_device_path(u))
fprintf(f,
"BindsTo=%s\n"
"After=%s\n"
"Before=umount.target\n",
d, d);
else
fprintf(f,
"RequiresMountsFor=%s\n",
u);
fprintf(f,
"\n[Service]\n"
"Type=oneshot\n"
"RemainAfterExit=yes\n"
"TimeoutSec=0\n" /* the binary handles timeouts anyway */
name);
if (tmp)
fprintf(f,
name);
if (swap)
fprintf(f,
name);
fflush(f);
if (ferror(f)) {
log_error("Failed to write file %s: %m", p);
return -errno;
}
if (!noauto) {
if (!to)
return log_oom();
return -errno;
}
if (!nofail)
else
if (!to)
return log_oom();
return -errno;
}
}
if (!to)
return log_oom();
return -errno;
}
free(p);
if (!p)
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 */
if (r < 0) {
return r;
}
}
return 0;
}
int r;
r = parse_boolean(value);
if (r < 0)
else
arg_enabled = r;
r = parse_boolean(value);
if (r < 0)
else
arg_read_crypttab = r;
return log_oom();
return log_oom();
if (!arg_keyfile)
return log_oom();
}
return 0;
}
unsigned n = 0;
char **i;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
log_open();
umask(0022);
if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
goto cleanup;
if (!arg_enabled) {
r = r2 = EXIT_SUCCESS;
goto cleanup;
}
if (arg_read_crypttab) {
if (!f) {
r = EXIT_SUCCESS;
else
goto next;
}
goto next;
}
/* 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. */
for (;;) {
int k;
break;
n++;
if (*l == '#' || *l == 0)
continue;
if (k < 2 || k > 4) {
continue;
}
/*
If options are specified on the kernel commandline, let them override
the ones from crypttab.
*/
STRV_FOREACH(i, arg_options) {
const char *p = *i;
if (!proc_options) {
log_oom();
goto cleanup;
}
}
}
if (arg_disks) {
/*
If luks UUIDs are specified on the kernel command line, use them as a filter
*/
STRV_FOREACH(i, arg_disks) {
const char *p = *i;
if (startswith(p, "luks-"))
p += 5;
if (!proc_name || !proc_device) {
log_oom();
goto cleanup;
}
goto cleanup;
if (strv_extend(&disks_done, p) < 0) {
log_oom();
goto cleanup;
}
}
}
goto cleanup;
}
}
r = EXIT_SUCCESS;
next:
STRV_FOREACH(i, arg_disks) {
/*
Generate units for those UUIDs, which were specified
on the kernel command line and not yet written.
*/
const char *p = *i;
if (startswith(p, "luks-"))
p += 5;
if (strv_contains(disks_done, p))
continue;
log_oom();
goto cleanup;
}
if (arg_options) {
/*
If options are specified on the kernel commandline, use them.
*/
char **j;
STRV_FOREACH(j, arg_options) {
const char *s = *j;
int k;
if (k == 2) {
proc_options = NULL;
}
} else if (!options) {
/*
Fall back to options without a specified UUID
*/
if (!options) {
log_oom();
goto cleanup;
};
}
}
}
if (!options) {
if (!options) {
log_oom();
goto cleanup;
}
}
goto cleanup;
}
r2 = EXIT_SUCCESS;
return r != EXIT_SUCCESS ? r : r2;
}