1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <libcryptsetup.h>
30 #include "ask-password-api.h"
32 static const char *opt_type = NULL; /* LUKS1 or PLAIN */
33 static char *opt_cipher = NULL;
34 static unsigned opt_key_size = 0;
35 static char *opt_hash = NULL;
36 static unsigned opt_tries = 0;
37 static bool opt_readonly = false;
38 static bool opt_verify = false;
39 static usec_t opt_timeout = 0;
41 /* Options Debian's crypttab knows we don't:
53 static int parse_one_option(const char *option) {
56 /* Handled outside of this tool */
57 if (streq(option, "noauto"))
60 if (startswith(option, "cipher=")) {
63 if (!(t = strdup(option+7)))
69 } else if (startswith(option, "size=")) {
71 if (safe_atou(option+5, &opt_key_size) < 0) {
72 log_error("size= parse failure, ignoring.");
76 } else if (startswith(option, "hash=")) {
79 if (!(t = strdup(option+5)))
85 } else if (startswith(option, "tries=")) {
87 if (safe_atou(option+6, &opt_tries) < 0) {
88 log_error("tries= parse failure, ignoring.");
92 } else if (streq(option, "readonly"))
94 else if (streq(option, "verify"))
96 else if (streq(option, "luks"))
97 opt_type = CRYPT_LUKS1;
98 else if (streq(option, "plain") ||
99 streq(option, "swap") ||
100 streq(option, "tmp"))
101 opt_type = CRYPT_PLAIN;
102 else if (startswith(option, "timeout=")) {
104 if (parse_usec(option+8, &opt_timeout) < 0) {
105 log_error("timeout= parse failure, ignoring.");
110 log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
115 static int parse_options(const char *options) {
122 FOREACH_WORD_SEPARATOR(w, l, options, ",", state) {
126 if (!(o = strndup(w, l)))
129 r = parse_one_option(o);
139 static void log_glue(int level, const char *msg, void *usrptr) {
140 log_debug("%s", msg);
143 int main(int argc, char *argv[]) {
144 int r = EXIT_FAILURE;
145 struct crypt_device *cd = NULL;
146 char *password = NULL, *truncated_cipher = NULL;
147 const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL;
150 log_error("This program requires at least two arguments.");
154 log_set_target(LOG_TARGET_AUTO);
155 log_parse_environment();
158 if (streq(argv[1], "attach")) {
162 const char *key_file = NULL;
164 crypt_status_info status;
167 log_error("attach requires at least two arguments.");
173 !streq(argv[4], "-") &&
174 !streq(argv[4], "none")) {
176 if (!path_is_absolute(argv[4]))
177 log_error("Password file path %s is not absolute. Ignoring.", argv[4]);
182 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-"))
183 parse_options(argv[5]);
185 /* A delicious drop of snake oil */
186 mlockall(MCL_FUTURE);
188 if ((k = crypt_init(&cd, argv[3]))) {
189 log_error("crypt_init() failed: %s", strerror(-k));
193 crypt_set_log_callback(cd, log_glue, NULL);
195 status = crypt_status(cd, argv[2]);
196 if (status == CRYPT_ACTIVE || status == CRYPT_BUSY) {
197 log_info("Volume %s already active.", argv[2]);
203 flags |= CRYPT_ACTIVATE_READONLY;
205 until = now(CLOCK_MONOTONIC) + (opt_timeout > 0 ? opt_timeout : 60 * USEC_PER_SEC);
207 opt_tries = opt_tries > 0 ? opt_tries : 3;
208 opt_key_size = (opt_key_size > 0 ? opt_key_size : 256);
209 hash = opt_hash ? opt_hash : "ripemd160";
214 l = strcspn(opt_cipher, "-");
216 if (!(truncated_cipher = strndup(opt_cipher, l))) {
217 log_error("Out of memory");
221 cipher = truncated_cipher;
222 cipher_mode = opt_cipher[l] ? opt_cipher+l+1 : "plain";
225 cipher_mode = "cbc-essiv:sha256";
228 for (try = 0; try < opt_tries; try++) {
229 bool pass_volume_key = false;
237 if (asprintf(&text, "Please enter passphrase for disk %s!", argv[2]) < 0) {
238 log_error("Out of memory");
242 k = ask_password_auto(text, "drive-harddisk", until, &password);
246 log_error("Failed to query password: %s", strerror(-k));
251 char *password2 = NULL;
253 if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", argv[2]) < 0) {
254 log_error("Out of memory");
258 k = ask_password_auto(text, "drive-harddisk", until, &password2);
262 log_error("Failed to query verification password: %s", strerror(-k));
266 if (!streq(password, password2)) {
267 log_warning("Passwords did not match, retrying.");
275 if (strlen(password)+1 < opt_key_size) {
278 /* Pad password if necessary */
280 if (!(c = new(char, opt_key_size))) {
281 log_error("Out of memory.");
285 strncpy(c, password, opt_key_size);
291 if (!opt_type || streq(opt_type, CRYPT_LUKS1))
292 k = crypt_load(cd, CRYPT_LUKS1, NULL);
294 if ((!opt_type && k < 0) || streq_ptr(opt_type, CRYPT_PLAIN)) {
295 struct crypt_params_plain params;
300 /* In contrast to what the name
301 * crypt_setup() might suggest this
302 * doesn't actually format anything,
303 * it just configures encryption
304 * parameters when used for plain
306 k = crypt_format(cd, CRYPT_PLAIN,
314 pass_volume_key = streq(hash, "plain");
318 log_error("Loading of cryptographic parameters failed: %s", strerror(-k));
322 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
323 crypt_get_cipher(cd),
324 crypt_get_cipher_mode(cd),
325 crypt_get_volume_key_size(cd)*8,
329 k = crypt_activate_by_keyfile(cd, argv[2], CRYPT_ANY_SLOT, key_file, opt_key_size, flags);
330 else if (pass_volume_key)
331 k = crypt_activate_by_volume_key(cd, argv[2], password, opt_key_size, flags);
333 k = crypt_activate_by_passphrase(cd, argv[2], CRYPT_ANY_SLOT, password, strlen(password), flags);
339 log_error("Failed to activate: %s", strerror(-k));
343 log_warning("Invalid passphrase.");
346 if (try >= opt_tries) {
347 log_error("Too many attempts.");
351 } else if (streq(argv[1], "detach")) {
354 if ((k = crypt_init_by_name(&cd, argv[2]))) {
355 log_error("crypt_init() failed: %s", strerror(-k));
359 crypt_set_log_callback(cd, log_glue, NULL);
361 if ((k = crypt_deactivate(cd, argv[2])) < 0) {
362 log_error("Failed to deactivate: %s", strerror(-k));
367 log_error("Unknown verb %s.", argv[1]);
381 free(truncated_cipher);