cryptsetup.c revision b4d0195b0598df76d30f006507fa8466f5a5d330
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <errno.h>
#include <mntent.h>
#include <libcryptsetup.h>
#include <libudev.h>
#include "log.h"
#include "util.h"
#include "strv.h"
#include "ask-password-api.h"
#include "def.h"
static char *opt_cipher = NULL;
static unsigned opt_key_size = 0;
static unsigned opt_tries = 0;
static bool opt_readonly = false;
static bool opt_verify = false;
/* Options Debian's crypttab knows we don't:
offset=
skip=
precheck=
check=
checkargs=
noearly=
loud=
keyscript=
*/
static int parse_one_option(const char *option) {
/* Handled outside of this tool */
return 0;
char *t;
return -ENOMEM;
opt_cipher = t;
log_error("size= parse failure, ignoring.");
return 0;
}
char *t;
return -ENOMEM;
opt_hash = t;
log_error("tries= parse failure, ignoring.");
return 0;
}
opt_readonly = true;
opt_verify = true;
log_error("timeout= parse failure, ignoring.");
return 0;
}
return 0;
}
static int parse_options(const char *options) {
char *state;
char *w;
size_t l;
char *o;
int r;
if (!(o = strndup(w, l)))
return -ENOMEM;
r = parse_one_option(o);
free(o);
if (r < 0)
return r;
}
return 0;
}
}
static char *disk_description(const char *path) {
char *description = NULL;
const char *model;
return NULL;
return NULL;
return NULL;
goto finish;
if (device)
if (udev)
return description;
}
static char *disk_mount_point(const char *label) {
struct mntent *m;
/* Yeah, we don't support native systemd unit files here for now */
goto finish;
goto finish;
while ((m = getmntent(f)))
break;
}
if (f)
endmntent(f);
return mp;
}
static int help(void) {
printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
"%s detach VOLUME\n\n"
"Attaches or detaches an encrypted block device.\n",
return 0;
}
int r = EXIT_FAILURE;
unsigned keyfile_size = 0;
if (argc <= 1) {
help();
return EXIT_SUCCESS;
}
if (argc < 3) {
log_error("This program requires at least two arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
int k;
unsigned try;
/* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
if (argc < 4) {
log_error("attach requires at least two arguments.");
goto finish;
}
if (argc >= 5 &&
argv[4][0] &&
else
}
/* A delicious drop of snake oil */
/* If the description string is simply the
* volume name, then let's not show this
* twice */
description = NULL;
}
if (mount_point && description)
else if (mount_point)
else if (description)
goto finish;
}
r = EXIT_SUCCESS;
goto finish;
}
if (opt_readonly)
if (opt_timeout > 0)
else
until = 0;
if (opt_cipher) {
size_t l;
log_error("Out of memory");
goto finish;
}
} else {
cipher = "aes";
cipher_mode = "cbc-essiv:sha256";
}
bool pass_volume_key = false;
if (!key_file) {
char *text;
char **p;
log_error("Out of memory");
goto finish;
}
if (k < 0) {
goto finish;
}
if (opt_verify) {
char **passwords2 = NULL;
log_error("Out of memory");
goto finish;
}
if (k < 0) {
goto finish;
}
log_warning("Passwords did not match, retrying.");
continue;
}
}
STRV_FOREACH(p, passwords) {
char *c;
continue;
/* Pad password if necessary */
if (!(c = new(char, opt_key_size))) {
log_error("Out of memory.");
goto finish;
}
strncpy(c, *p, opt_key_size);
free(*p);
*p = c;
}
}
k = 0;
struct crypt_params_plain params;
/* In contrast to what the name
* crypt_setup() might suggest this
* doesn't actually format anything,
* it just configures encryption
* parameters when used for plain
* mode. */
NULL,
NULL,
opt_key_size / 8,
¶ms);
/* for CRYPT_PLAIN limit reads
* from keyfile to key length */
}
if (k < 0) {
goto finish;
}
log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
argv[3]);
if (key_file)
else {
char **p;
STRV_FOREACH(p, passwords) {
if (pass_volume_key)
else
if (k >= 0)
break;
}
}
if (k >= 0)
break;
if (k != -EPERM) {
goto finish;
}
log_warning("Invalid passphrase.");
}
log_error("Too many attempts.");
r = EXIT_FAILURE;
goto finish;
}
int k;
goto finish;
}
goto finish;
}
} else {
goto finish;
}
r = EXIT_SUCCESS;
if (cd)
crypt_free(cd);
return r;
}