/***
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 <errno.h>
#include <libcryptsetup.h>
#include <mntent.h>
#include <string.h>
#include "sd-device.h"
#include "alloc-util.h"
#include "ask-password-api.h"
#include "device-util.h"
#include "escape.h"
#include "fileio.h"
#include "log.h"
#include "mount-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
static unsigned arg_key_size = 0;
static unsigned arg_keyfile_size = 0;
static unsigned arg_keyfile_offset = 0;
static bool arg_readonly = false;
static bool arg_verify = false;
static bool arg_discards = false;
static bool arg_tcrypt_hidden = false;
static bool arg_tcrypt_system = false;
/* Options Debian's crypttab knows we don't:
precheck=
check=
checkargs=
noearly=
loud=
keyscript=
*/
/* Handled outside of this tool */
return 0;
char *t;
if (!t)
return log_oom();
arg_cipher = t;
log_error("size= parse failure, ignoring.");
return 0;
}
if (arg_key_size % 8) {
log_error("size= not a multiple of 8, ignoring.");
return 0;
}
arg_key_size /= 8;
log_error("key-slot= parse failure, ignoring.");
return 0;
}
return log_oom();
} else
log_error("keyfile-size= parse failure, ignoring.");
return 0;
}
log_error("keyfile-offset= parse failure, ignoring.");
return 0;
}
char *t;
if (!t)
return log_oom();
arg_hash = t;
return -EINVAL;
}
if (arg_header) {
log_error("Duplicate header= options, refusing.");
return -EINVAL;
}
if (!arg_header)
return log_oom();
log_error("tries= parse failure, ignoring.");
return 0;
}
arg_readonly = true;
arg_verify = true;
arg_discards = true;
arg_tcrypt_hidden = true;
arg_tcrypt_system = true;
log_error("timeout= parse failure, ignoring.");
return 0;
}
log_error("offset= parse failure, refusing.");
return -EINVAL;
}
log_error("skip= parse failure, refusing.");
return -EINVAL;
}
return 0;
}
size_t l;
int r;
_cleanup_free_ char *o;
if (!o)
return -ENOMEM;
r = parse_one_option(o);
if (r < 0)
return r;
}
/* sanity-check options */
if (arg_offset)
if (arg_skip)
}
return 0;
}
}
return -errno;
return -EINVAL;
return -errno;
return 0;
}
static const char name_fields[] =
"ID_PART_ENTRY_NAME\0"
"DM_NAME\0"
"ID_MODEL_FROM_DATABASE\0"
"ID_MODEL\0";
const char *i;
int r;
return NULL;
return NULL;
if (r < 0)
return NULL;
const char *name;
}
return NULL;
}
struct mntent *m;
/* Yeah, we don't support native systemd unit files here for now */
return NULL;
if (!f)
return NULL;
while ((m = getmntent(f)))
return NULL;
}
static int get_password(const char *vol, const char *src, usec_t until, bool accept_cached, char ***ret) {
_cleanup_free_ char *description = NULL, *name_buffer = NULL, *mount_point = NULL, *maj_min = NULL, *text = NULL, *escaped_name = NULL;
char **p, *id;
int r = 0;
/* If the description string is simply the
* volume name, then let's not show this
* twice */
if (mount_point && description)
else if (mount_point)
else if (description)
if (r < 0)
return log_oom();
return log_oom();
if (src)
if (maj_min) {
} else
if (!escaped_name)
return log_oom();
&passwords);
if (r < 0)
return log_error_errno(r, "Failed to query password: %m");
if (arg_verify) {
return log_oom();
r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2);
if (r < 0)
return log_error_errno(r, "Failed to query verification password: %m");
log_warning("Passwords did not match, retrying.");
return -EAGAIN;
}
}
char *c;
continue;
/* Pad password if necessary */
c = new(char, arg_key_size);
if (!c)
return log_oom();
strncpy(c, *p, arg_key_size);
free(*p);
*p = c;
}
return 0;
}
static int attach_tcrypt(
struct crypt_device *cd,
const char *name,
const char *key_file,
char **passwords,
int r = 0;
.keyfiles = (const char **)arg_tcrypt_keyfiles,
};
if (arg_tcrypt_hidden)
if (arg_tcrypt_system)
if (key_file) {
if (r < 0) {
return -EAGAIN;
}
} else
if (r < 0) {
return -EAGAIN;
}
return r;
}
}
const char *name,
const char *key_file,
const char *data_device,
char **passwords,
int r = 0;
bool pass_volume_key = false;
if (r < 0) {
return r;
}
if (data_device)
}
.offset = arg_offset,
};
if (arg_hash) {
/* plain isn't a real hash type. it just means "use no hash" */
} else if (!key_file)
/* for CRYPT_PLAIN, the behaviour of cryptsetup
* package is to not hash when a key file is provided */
if (arg_cipher) {
size_t l;
if (!truncated_cipher)
return log_oom();
} else {
cipher = "aes";
cipher_mode = "cbc-essiv:sha256";
}
/* for CRYPT_PLAIN limit reads
* from keyfile to key length, and
* ignore keyfile-size */
/* 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. */
/* hash == NULL implies the user passed "plain" */
}
if (r < 0)
return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
if (key_file) {
r = crypt_activate_by_keyfile_offset(cd, name, arg_key_slot, key_file, arg_keyfile_size, arg_keyfile_offset, flags);
if (r < 0) {
return -EAGAIN;
}
} else {
char **p;
if (pass_volume_key)
else
if (r >= 0)
break;
}
}
return r;
}
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;
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 tries;
/* 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
}
goto finish;
}
/* A delicious drop of snake oil */
if (arg_header) {
} else
if (k) {
log_error_errno(k, "crypt_init() failed: %m");
goto finish;
}
r = EXIT_SUCCESS;
goto finish;
}
if (arg_readonly)
if (arg_discards)
if (arg_timeout > 0)
else
until = 0;
if (key_file) {
/* Ideally we'd do this on the open fd, but since this is just a
* warning it's OK to do this in two steps. */
}
if (!key_file) {
if (k == -EAGAIN)
continue;
else if (k < 0)
goto finish;
}
else
k = attach_luks_or_plain(cd,
argv[2],
flags);
if (k >= 0)
break;
else if (k == -EAGAIN) {
continue;
} else if (k != -EPERM) {
log_error_errno(k, "Failed to activate: %m");
goto finish;
}
log_warning("Invalid passphrase.");
}
log_error("Too many attempts; giving up.");
r = EXIT_FAILURE;
goto finish;
}
int k;
if (k) {
log_error_errno(k, "crypt_init() failed: %m");
goto finish;
}
if (k < 0) {
log_error_errno(k, "Failed to deactivate: %m");
goto finish;
}
} else {
goto finish;
}
r = EXIT_SUCCESS;
if (cd)
crypt_free(cd);
return r;
}