cryptsetup.c revision 21bc923aa35d455cdef1607eb7022608c705c9f3
/*-*- 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 <libcryptsetup.h>
#include <libudev.h>
#include "log.h"
#include "util.h"
#include "strv.h"
#include "ask-password-api.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;
static usec_t opt_timeout = 0;
/* 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;
}
} else
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;
}
int r = EXIT_FAILURE;
char *description = NULL;
if (argc < 3) {
log_error("This program requires at least two arguments.");
return EXIT_FAILURE;
}
log_open();
int k;
unsigned try;
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 */
goto finish;
}
r = EXIT_SUCCESS;
goto finish;
}
if (opt_readonly)
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;
}
}
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);
}
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;
}