chiark / gitweb /
cryptsetup: support non-LUKS crypto partitions
[elogind.git] / src / cryptsetup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <string.h>
23 #include <errno.h>
24
25 #include <libcryptsetup.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "ask-password-api.h"
30
31 static const char *opt_type = NULL; /* LUKS1 or PLAIN */
32 static char *opt_cipher = NULL;
33 static char *opt_cipher_mode = 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;
40
41 static int parse_one_option(const char *option) {
42         assert(option);
43
44         /* Handled outside of this tool */
45         if (streq(option, "noauto"))
46                 return 0;
47
48         if (startswith(option, "cipher=")) {
49                 char *t;
50
51                 if (!(t = strdup(option+7)))
52                         return -ENOMEM;
53
54                 free(opt_cipher);
55                 opt_cipher = t;
56
57         } else if (startswith(option, "size=")) {
58
59                 if (safe_atou(option+5, &opt_key_size) < 0) {
60                         log_error("size= parse failure, ignoring.");
61                         return 0;
62                 }
63
64         } else if (startswith(option, "hash=")) {
65                 char *t;
66
67                 if (!(t = strdup(option+5)))
68                         return -ENOMEM;
69
70                 free(opt_hash);
71                 opt_hash = t;
72
73         } else if (startswith(option, "mode=")) {
74                 char *t;
75
76                 if (!(t = strdup(option+5)))
77                         return -ENOMEM;
78
79                 free(opt_cipher_mode);
80                 opt_cipher_mode = t;
81
82         } else if (startswith(option, "tries=")) {
83
84                 if (safe_atou(option+6, &opt_tries) < 0) {
85                         log_error("tries= parse failure, ignoring.");
86                         return 0;
87                 }
88
89         } else if (streq(option, "readonly"))
90                 opt_readonly = true;
91         else if (streq(option, "verify"))
92                 opt_verify = true;
93         else if (streq(option, "luks"))
94                 opt_type = CRYPT_LUKS1;
95         else if (streq(option, "plain") ||
96                  streq(option, "swap") ||
97                  streq(option, "tmp"))
98                 opt_type = CRYPT_PLAIN;
99         else if (startswith(option, "timeout=")) {
100
101                 if (parse_usec(option+8, &opt_timeout) < 0) {
102                         log_error("timeout= parse failure, ignoring.");
103                         return 0;
104                 }
105
106         } else
107                 log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
108
109         return 0;
110 }
111
112 static int parse_options(const char *options) {
113         char *state;
114         char *w;
115         size_t l;
116
117         assert(options);
118
119         FOREACH_WORD_SEPARATOR(w, l, options, ",", state) {
120                 char *o;
121                 int r;
122
123                 if (!(o = strndup(w, l)))
124                         return -ENOMEM;
125
126                 r = parse_one_option(o);
127                 free(o);
128
129                 if (r < 0)
130                         return r;
131         }
132
133         return 0;
134 }
135
136 static void log_glue(int level, const char *msg, void *usrptr) {
137         log_debug("%s", msg);
138 }
139
140 int main(int argc, char *argv[]) {
141         int r = EXIT_FAILURE;
142         struct crypt_device *cd = NULL;
143         char *password = NULL;
144         const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL;
145         crypt_status_info status;
146
147         if (argc < 3) {
148                 log_error("This program requires at least two arguments.");
149                 return EXIT_FAILURE;
150         }
151
152         log_set_target(LOG_TARGET_AUTO);
153         log_parse_environment();
154         log_open();
155
156         if (streq(argv[1], "attach")) {
157                 uint32_t flags = 0;
158                 int k;
159                 unsigned try;
160                 const char *key_file = NULL;
161                 usec_t until;
162
163                 if (argc < 4) {
164                         log_error("attach requires at least two arguments.");
165                         goto finish;
166                 }
167
168                 if (argc >= 5 && argv[4][0] && !streq(argv[4], "-")) {
169
170                         if (!path_is_absolute(argv[4]))
171                                 log_error("Password file path %s is not absolute. Ignoring.", argv[4]);
172                         else
173                                 key_file = argv[4];
174                 }
175
176                 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-"))
177                         parse_options(argv[5]);
178
179                 if ((k = crypt_init(&cd, argv[3]))) {
180                         log_error("crypt_init() failed: %s", strerror(-k));
181                         goto finish;
182                 }
183
184                 crypt_set_log_callback(cd, log_glue, NULL);
185
186                 status = crypt_status(cd, argv[2]);
187                 if (status == CRYPT_ACTIVE || status == CRYPT_BUSY) {
188                         log_info("Volume %s already active.", argv[2]);
189                         r = EXIT_SUCCESS;
190                         goto finish;
191                 }
192
193                 if (opt_readonly)
194                         flags |= CRYPT_ACTIVATE_READONLY;
195
196                 until = now(CLOCK_MONOTONIC) + (opt_timeout > 0 ? opt_timeout : 60 * USEC_PER_SEC);
197
198                 opt_tries = opt_tries > 0 ? opt_tries : 3;
199                 opt_key_size = (opt_key_size > 0 ? opt_key_size : 256);
200                 cipher = opt_cipher ? opt_cipher : "aes";
201                 cipher_mode = opt_cipher_mode ? opt_cipher_mode : "cbc-essiv:sha256";
202                 hash = opt_hash ? opt_hash : "ripemd160";
203
204                 for (try = 0; try < opt_tries; try++) {
205                         bool pass_volume_key = false;
206
207                         free(password);
208                         password = NULL;
209
210                         if (!key_file) {
211
212                                 if ((k = ask_password_auto("Please enter passphrase for disk:", "drive-harddisk", until, &password)) < 0) {
213                                         log_error("Failed to query password: %s", strerror(-k));
214                                         goto finish;
215                                 }
216
217                                 if (opt_verify) {
218                                         char *password2 = NULL;
219
220                                         if ((k = ask_password_auto("Please enter passphrase for disk (verification):", "drive-harddisk", until, &password2)) < 0) {
221                                                 log_error("Failed to query verification password: %s", strerror(-k));
222                                                 goto finish;
223                                         }
224
225                                         if (!streq(password, password2)) {
226                                                 log_warning("Passwords did not match, retrying.");
227                                                 free(password2);
228                                                 continue;
229                                         }
230
231                                         free(password2);
232                                 }
233
234                                 if (strlen(password)+1 < opt_key_size) {
235                                         char *c;
236
237                                         /* Pad password if necessary */
238
239                                         if (!(c = new(char, opt_key_size))) {
240                                                 log_error("Out of memory.");
241                                                 goto finish;
242                                         }
243
244                                         strncpy(c, password, opt_key_size);
245                                         free(password);
246                                         password = c;
247                                 }
248                         }
249
250                         if (!opt_type || streq(opt_type, CRYPT_LUKS1))
251                                 k = crypt_load(cd, CRYPT_LUKS1, NULL);
252
253                         if ((!opt_type && k < 0) || streq_ptr(opt_type, CRYPT_PLAIN)) {
254                                 struct crypt_params_plain params;
255
256                                 zero(params);
257                                 params.hash = hash;
258
259                                 /* In contrast to what the name
260                                  * crypt_setup() might suggest this
261                                  * doesn't actually format anything,
262                                  * it just configures encryption
263                                  * parameters when used for plain
264                                  * mode. */
265                                 k = crypt_format(cd, CRYPT_PLAIN,
266                                                  cipher,
267                                                  cipher_mode,
268                                                  NULL,
269                                                  NULL,
270                                                  opt_key_size / 8,
271                                                  &params);
272
273                                 pass_volume_key = streq(hash, "plain");
274                         }
275
276                         if (k < 0) {
277                                 log_error("Loading of cryptographic parameters failed: %s", strerror(-k));
278                                 goto finish;
279                         }
280
281                         log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
282                                  crypt_get_cipher(cd),
283                                  crypt_get_cipher_mode(cd),
284                                  crypt_get_volume_key_size(cd)*8,
285                                  argv[3]);
286
287                         if (key_file)
288                                 k = crypt_activate_by_keyfile(cd, argv[2], CRYPT_ANY_SLOT, key_file, opt_key_size, flags);
289                         else if (pass_volume_key)
290                                 k = crypt_activate_by_volume_key(cd, argv[2], password, opt_key_size, flags);
291                         else
292                                 k = crypt_activate_by_passphrase(cd, argv[2], CRYPT_ANY_SLOT, password, strlen(password), flags);
293
294                         if (k >= 0)
295                                 break;
296
297                         if (k != -EPERM) {
298                                 log_error("Failed to activate: %s", strerror(-k));
299                                 goto finish;
300                         }
301
302                         log_warning("Invalid passphrase.");
303                 }
304
305                 if (try >= opt_tries) {
306                         log_error("Too many attempts.");
307                         r = EXIT_FAILURE;
308                 }
309
310         } else if (streq(argv[1], "detach")) {
311                 int k;
312
313                 if ((k = crypt_init_by_name(&cd, argv[2]))) {
314                         log_error("crypt_init() failed: %s", strerror(-k));
315                         goto finish;
316                 }
317
318                 crypt_set_log_callback(cd, log_glue, NULL);
319
320                 if ((k = crypt_deactivate(cd, argv[2])) < 0) {
321                         log_error("Failed to deactivate: %s", strerror(-k));
322                         goto finish;
323                 }
324
325         } else {
326                 log_error("Unknown verb %s.", argv[1]);
327                 goto finish;
328         }
329
330         r = EXIT_SUCCESS;
331
332 finish:
333
334         if (cd)
335                 crypt_free(cd);
336
337         free(opt_cipher);
338         free(opt_cipher_mode);
339         free(opt_hash);
340
341         free(password);
342
343         return r;
344 }