]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup.c
main: check if we have a valid PID before getting the name of it
[thirdparty/systemd.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 #include <sys/mman.h>
25 #include <mntent.h>
26
27 #include <libcryptsetup.h>
28 #include <libudev.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "strv.h"
33 #include "ask-password-api.h"
34
35 static const char *opt_type = NULL; /* LUKS1 or PLAIN */
36 static char *opt_cipher = NULL;
37 static unsigned opt_key_size = 0;
38 static char *opt_hash = NULL;
39 static unsigned opt_tries = 0;
40 static bool opt_readonly = false;
41 static bool opt_verify = false;
42 static usec_t opt_timeout = 0;
43
44 /* Options Debian's crypttab knows we don't:
45
46 offset=
47 skip=
48 precheck=
49 check=
50 checkargs=
51 noearly=
52 loud=
53 keyscript=
54 */
55
56 static int parse_one_option(const char *option) {
57 assert(option);
58
59 /* Handled outside of this tool */
60 if (streq(option, "noauto"))
61 return 0;
62
63 if (startswith(option, "cipher=")) {
64 char *t;
65
66 if (!(t = strdup(option+7)))
67 return -ENOMEM;
68
69 free(opt_cipher);
70 opt_cipher = t;
71
72 } else if (startswith(option, "size=")) {
73
74 if (safe_atou(option+5, &opt_key_size) < 0) {
75 log_error("size= parse failure, ignoring.");
76 return 0;
77 }
78
79 } else if (startswith(option, "hash=")) {
80 char *t;
81
82 if (!(t = strdup(option+5)))
83 return -ENOMEM;
84
85 free(opt_hash);
86 opt_hash = t;
87
88 } else if (startswith(option, "tries=")) {
89
90 if (safe_atou(option+6, &opt_tries) < 0) {
91 log_error("tries= parse failure, ignoring.");
92 return 0;
93 }
94
95 } else if (streq(option, "readonly"))
96 opt_readonly = true;
97 else if (streq(option, "verify"))
98 opt_verify = true;
99 else if (streq(option, "luks"))
100 opt_type = CRYPT_LUKS1;
101 else if (streq(option, "plain") ||
102 streq(option, "swap") ||
103 streq(option, "tmp"))
104 opt_type = CRYPT_PLAIN;
105 else if (startswith(option, "timeout=")) {
106
107 if (parse_usec(option+8, &opt_timeout) < 0) {
108 log_error("timeout= parse failure, ignoring.");
109 return 0;
110 }
111
112 } else
113 log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
114
115 return 0;
116 }
117
118 static int parse_options(const char *options) {
119 char *state;
120 char *w;
121 size_t l;
122
123 assert(options);
124
125 FOREACH_WORD_SEPARATOR(w, l, options, ",", state) {
126 char *o;
127 int r;
128
129 if (!(o = strndup(w, l)))
130 return -ENOMEM;
131
132 r = parse_one_option(o);
133 free(o);
134
135 if (r < 0)
136 return r;
137 }
138
139 return 0;
140 }
141
142 static void log_glue(int level, const char *msg, void *usrptr) {
143 log_debug("%s", msg);
144 }
145
146 static char *disk_description(const char *path) {
147 struct udev *udev = NULL;
148 struct udev_device *device = NULL;
149 struct stat st;
150 char *description = NULL;
151 const char *model;
152
153 assert(path);
154
155 if (stat(path, &st) < 0)
156 return NULL;
157
158 if (!S_ISBLK(st.st_mode))
159 return NULL;
160
161 if (!(udev = udev_new()))
162 return NULL;
163
164 if (!(device = udev_device_new_from_devnum(udev, 'b', st.st_rdev)))
165 goto finish;
166
167 if ((model = udev_device_get_property_value(device, "ID_MODEL_FROM_DATABASE")) ||
168 (model = udev_device_get_property_value(device, "ID_MODEL")) ||
169 (model = udev_device_get_property_value(device, "DM_NAME")))
170 description = strdup(model);
171
172 finish:
173 if (device)
174 udev_device_unref(device);
175
176 if (udev)
177 udev_unref(udev);
178
179 return description;
180 }
181
182 static char *disk_mount_point(const char *label) {
183 char *mp = NULL, *device = NULL;
184 FILE *f = NULL;
185 struct mntent *m;
186
187 /* Yeah, we don't support native systemd unit files here for now */
188
189 if (asprintf(&device, "/dev/mapper/%s", label) < 0)
190 goto finish;
191
192 if (!(f = setmntent("/etc/fstab", "r")))
193 goto finish;
194
195 while ((m = getmntent(f)))
196 if (path_equal(m->mnt_fsname, device)) {
197 mp = strdup(m->mnt_dir);
198 break;
199 }
200
201 finish:
202 if (f)
203 endmntent(f);
204
205 free(device);
206
207 return mp;
208 }
209
210 static int help(void) {
211
212 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
213 "%s detach VOLUME\n\n"
214 "Attaches or detaches an encrypted block device.\n",
215 program_invocation_short_name,
216 program_invocation_short_name);
217
218 return 0;
219 }
220
221 int main(int argc, char *argv[]) {
222 int r = EXIT_FAILURE;
223 struct crypt_device *cd = NULL;
224 char **passwords = NULL, *truncated_cipher = NULL;
225 const char *cipher = NULL, *cipher_mode = NULL, *hash = NULL, *name = NULL;
226 char *description = NULL, *name_buffer = NULL, *mount_point = NULL;
227
228 if (argc <= 1) {
229 help();
230 return EXIT_SUCCESS;
231 }
232
233 if (argc < 3) {
234 log_error("This program requires at least two arguments.");
235 return EXIT_FAILURE;
236 }
237
238 log_set_target(LOG_TARGET_AUTO);
239 log_parse_environment();
240 log_open();
241
242 if (streq(argv[1], "attach")) {
243 uint32_t flags = 0;
244 int k;
245 unsigned try;
246 const char *key_file = NULL;
247 usec_t until;
248 crypt_status_info status;
249
250 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
251
252 if (argc < 4) {
253 log_error("attach requires at least two arguments.");
254 goto finish;
255 }
256
257 if (argc >= 5 &&
258 argv[4][0] &&
259 !streq(argv[4], "-") &&
260 !streq(argv[4], "none")) {
261
262 if (!path_is_absolute(argv[4]))
263 log_error("Password file path %s is not absolute. Ignoring.", argv[4]);
264 else
265 key_file = argv[4];
266 }
267
268 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-"))
269 parse_options(argv[5]);
270
271 /* A delicious drop of snake oil */
272 mlockall(MCL_FUTURE);
273
274 description = disk_description(argv[3]);
275 mount_point = disk_mount_point(argv[2]);
276
277 if (description && streq(argv[2], description)) {
278 /* If the description string is simply the
279 * volume name, then let's not show this
280 * twice */
281 free(description);
282 description = NULL;
283 }
284
285 if (mount_point && description)
286 asprintf(&name_buffer, "%s (%s) on %s", description, argv[2], mount_point);
287 else if (mount_point)
288 asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
289 else if (description)
290 asprintf(&name_buffer, "%s (%s)", description, argv[2]);
291
292 name = name_buffer ? name_buffer : argv[2];
293
294 if ((k = crypt_init(&cd, argv[3]))) {
295 log_error("crypt_init() failed: %s", strerror(-k));
296 goto finish;
297 }
298
299 crypt_set_log_callback(cd, log_glue, NULL);
300
301 status = crypt_status(cd, argv[2]);
302 if (status == CRYPT_ACTIVE || status == CRYPT_BUSY) {
303 log_info("Volume %s already active.", argv[2]);
304 r = EXIT_SUCCESS;
305 goto finish;
306 }
307
308 if (opt_readonly)
309 flags |= CRYPT_ACTIVATE_READONLY;
310
311 until = now(CLOCK_MONOTONIC) + (opt_timeout > 0 ? opt_timeout : 60 * USEC_PER_SEC);
312
313 opt_tries = opt_tries > 0 ? opt_tries : 3;
314 opt_key_size = (opt_key_size > 0 ? opt_key_size : 256);
315 hash = opt_hash ? opt_hash : "ripemd160";
316
317 if (opt_cipher) {
318 size_t l;
319
320 l = strcspn(opt_cipher, "-");
321
322 if (!(truncated_cipher = strndup(opt_cipher, l))) {
323 log_error("Out of memory");
324 goto finish;
325 }
326
327 cipher = truncated_cipher;
328 cipher_mode = opt_cipher[l] ? opt_cipher+l+1 : "plain";
329 } else {
330 cipher = "aes";
331 cipher_mode = "cbc-essiv:sha256";
332 }
333
334 for (try = 0; try < opt_tries; try++) {
335 bool pass_volume_key = false;
336
337 strv_free(passwords);
338 passwords = NULL;
339
340 if (!key_file) {
341 char *text;
342 char **p;
343
344 if (asprintf(&text, "Please enter passphrase for disk %s!", name) < 0) {
345 log_error("Out of memory");
346 goto finish;
347 }
348
349 k = ask_password_auto(text, "drive-harddisk", until, try == 0 && !opt_verify, &passwords);
350 free(text);
351
352 if (k < 0) {
353 log_error("Failed to query password: %s", strerror(-k));
354 goto finish;
355 }
356
357 if (opt_verify) {
358 char **passwords2 = NULL;
359
360 assert(strv_length(passwords) == 1);
361
362 if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0) {
363 log_error("Out of memory");
364 goto finish;
365 }
366
367 k = ask_password_auto(text, "drive-harddisk", until, false, &passwords2);
368 free(text);
369
370 if (k < 0) {
371 log_error("Failed to query verification password: %s", strerror(-k));
372 goto finish;
373 }
374
375 assert(strv_length(passwords2) == 1);
376
377 if (!streq(passwords[0], passwords2[0])) {
378 log_warning("Passwords did not match, retrying.");
379 strv_free(passwords2);
380 continue;
381 }
382
383 strv_free(passwords2);
384 }
385
386 strv_uniq(passwords);
387
388 STRV_FOREACH(p, passwords) {
389 char *c;
390
391 if (strlen(*p)+1 >= opt_key_size)
392 continue;
393
394 /* Pad password if necessary */
395 if (!(c = new(char, opt_key_size))) {
396 log_error("Out of memory.");
397 goto finish;
398 }
399
400 strncpy(c, *p, opt_key_size);
401 free(*p);
402 *p = c;
403 }
404 }
405
406 if (!opt_type || streq(opt_type, CRYPT_LUKS1))
407 k = crypt_load(cd, CRYPT_LUKS1, NULL);
408
409 if ((!opt_type && k < 0) || streq_ptr(opt_type, CRYPT_PLAIN)) {
410 struct crypt_params_plain params;
411
412 zero(params);
413 params.hash = hash;
414
415 /* In contrast to what the name
416 * crypt_setup() might suggest this
417 * doesn't actually format anything,
418 * it just configures encryption
419 * parameters when used for plain
420 * mode. */
421 k = crypt_format(cd, CRYPT_PLAIN,
422 cipher,
423 cipher_mode,
424 NULL,
425 NULL,
426 opt_key_size / 8,
427 &params);
428
429 pass_volume_key = streq(hash, "plain");
430 }
431
432 if (k < 0) {
433 log_error("Loading of cryptographic parameters failed: %s", strerror(-k));
434 goto finish;
435 }
436
437 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
438 crypt_get_cipher(cd),
439 crypt_get_cipher_mode(cd),
440 crypt_get_volume_key_size(cd)*8,
441 argv[3]);
442
443 if (key_file)
444 k = crypt_activate_by_keyfile(cd, argv[2], CRYPT_ANY_SLOT, key_file, opt_key_size, flags);
445 else {
446 char **p;
447
448 STRV_FOREACH(p, passwords) {
449
450 if (pass_volume_key)
451 k = crypt_activate_by_volume_key(cd, argv[2], *p, opt_key_size, flags);
452 else
453 k = crypt_activate_by_passphrase(cd, argv[2], CRYPT_ANY_SLOT, *p, strlen(*p), flags);
454
455 if (k >= 0)
456 break;
457 }
458 }
459
460 if (k >= 0)
461 break;
462
463 if (k != -EPERM) {
464 log_error("Failed to activate: %s", strerror(-k));
465 goto finish;
466 }
467
468 log_warning("Invalid passphrase.");
469 }
470
471 if (try >= opt_tries) {
472 log_error("Too many attempts.");
473 r = EXIT_FAILURE;
474 goto finish;
475 }
476
477 } else if (streq(argv[1], "detach")) {
478 int k;
479
480 if ((k = crypt_init_by_name(&cd, argv[2]))) {
481 log_error("crypt_init() failed: %s", strerror(-k));
482 goto finish;
483 }
484
485 crypt_set_log_callback(cd, log_glue, NULL);
486
487 if ((k = crypt_deactivate(cd, argv[2])) < 0) {
488 log_error("Failed to deactivate: %s", strerror(-k));
489 goto finish;
490 }
491
492 } else {
493 log_error("Unknown verb %s.", argv[1]);
494 goto finish;
495 }
496
497 r = EXIT_SUCCESS;
498
499 finish:
500
501 if (cd)
502 crypt_free(cd);
503
504 free(opt_cipher);
505 free(opt_hash);
506
507 free(truncated_cipher);
508
509 strv_free(passwords);
510
511 free(description);
512 free(mount_point);
513 free(name_buffer);
514
515 return r;
516 }