]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup.c
cryptsetup: use free_and_replace() where appropriate
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <mntent.h>
5 #include <string.h>
6 #include <sys/mman.h>
7
8 #include "sd-device.h"
9
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
12 #include "crypt-util.h"
13 #include "device-util.h"
14 #include "escape.h"
15 #include "fileio.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "mount-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "pretty-print.h"
24 #include "util.h"
25
26 /* internal helper */
27 #define ANY_LUKS "LUKS"
28 /* as in src/cryptsetup.h */
29 #define CRYPT_SECTOR_SIZE 512
30 #define CRYPT_MAX_SECTOR_SIZE 4096
31
32 static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
33 static char *arg_cipher = NULL;
34 static unsigned arg_key_size = 0;
35 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
36 static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
37 #endif
38 static int arg_key_slot = CRYPT_ANY_SLOT;
39 static unsigned arg_keyfile_size = 0;
40 static uint64_t arg_keyfile_offset = 0;
41 static char *arg_hash = NULL;
42 static char *arg_header = NULL;
43 static unsigned arg_tries = 3;
44 static bool arg_readonly = false;
45 static bool arg_verify = false;
46 static bool arg_discards = false;
47 static bool arg_tcrypt_hidden = false;
48 static bool arg_tcrypt_system = false;
49 #ifdef CRYPT_TCRYPT_VERA_MODES
50 static bool arg_tcrypt_veracrypt = false;
51 #endif
52 static char **arg_tcrypt_keyfiles = NULL;
53 static uint64_t arg_offset = 0;
54 static uint64_t arg_skip = 0;
55 static usec_t arg_timeout = USEC_INFINITY;
56
57 STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
58 STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
59 STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
60 STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
61
62 /* Options Debian's crypttab knows we don't:
63
64 precheck=
65 check=
66 checkargs=
67 noearly=
68 loud=
69 keyscript=
70 */
71
72 static int parse_one_option(const char *option) {
73 const char *val;
74 int r;
75
76 assert(option);
77
78 /* Handled outside of this tool */
79 if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev"))
80 return 0;
81
82 if ((val = startswith(option, "cipher="))) {
83 r = free_and_strdup(&arg_cipher, val);
84 if (r < 0)
85 return log_oom();
86
87 } else if ((val = startswith(option, "size="))) {
88
89 r = safe_atou(val, &arg_key_size);
90 if (r < 0) {
91 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
92 return 0;
93 }
94
95 if (arg_key_size % 8) {
96 log_error("size= not a multiple of 8, ignoring.");
97 return 0;
98 }
99
100 arg_key_size /= 8;
101
102 } else if ((val = startswith(option, "sector-size="))) {
103
104 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
105 r = safe_atou(val, &arg_sector_size);
106 if (r < 0) {
107 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
108 return 0;
109 }
110
111 if (arg_sector_size % 2) {
112 log_error("sector-size= not a multiple of 2, ignoring.");
113 return 0;
114 }
115
116 if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) {
117 log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
118 return 0;
119 }
120 #else
121 log_error("sector-size= is not supported, compiled with old libcryptsetup.");
122 return 0;
123 #endif
124
125 } else if ((val = startswith(option, "key-slot="))) {
126
127 arg_type = ANY_LUKS;
128 r = safe_atoi(val, &arg_key_slot);
129 if (r < 0) {
130 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
131 return 0;
132 }
133
134 } else if ((val = startswith(option, "tcrypt-keyfile="))) {
135
136 arg_type = CRYPT_TCRYPT;
137 if (path_is_absolute(val)) {
138 if (strv_extend(&arg_tcrypt_keyfiles, val) < 0)
139 return log_oom();
140 } else
141 log_error("Key file path \"%s\" is not absolute. Ignoring.", val);
142
143 } else if ((val = startswith(option, "keyfile-size="))) {
144
145 r = safe_atou(val, &arg_keyfile_size);
146 if (r < 0) {
147 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
148 return 0;
149 }
150
151 } else if ((val = startswith(option, "keyfile-offset="))) {
152 uint64_t off;
153
154 r = safe_atou64(val, &off);
155 if (r < 0) {
156 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
157 return 0;
158 }
159
160 if ((size_t) off != off) {
161 /* https://gitlab.com/cryptsetup/cryptsetup/issues/359 */
162 log_error("keyfile-offset= value would truncated to %zu, ignoring.", (size_t) off);
163 return 0;
164 }
165
166 arg_keyfile_offset = off;
167
168 } else if ((val = startswith(option, "hash="))) {
169 r = free_and_strdup(&arg_hash, val);
170 if (r < 0)
171 return log_oom();
172
173 } else if ((val = startswith(option, "header="))) {
174 arg_type = ANY_LUKS;
175
176 if (!path_is_absolute(val))
177 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
178 "Header path \"%s\" is not absolute, refusing.", val);
179
180 if (arg_header)
181 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
182 "Duplicate header= option, refusing.");
183
184 arg_header = strdup(val);
185 if (!arg_header)
186 return log_oom();
187
188 } else if ((val = startswith(option, "tries="))) {
189
190 r = safe_atou(val, &arg_tries);
191 if (r < 0) {
192 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
193 return 0;
194 }
195
196 } else if (STR_IN_SET(option, "readonly", "read-only"))
197 arg_readonly = true;
198 else if (streq(option, "verify"))
199 arg_verify = true;
200 else if (STR_IN_SET(option, "allow-discards", "discard"))
201 arg_discards = true;
202 else if (streq(option, "luks"))
203 arg_type = ANY_LUKS;
204 else if (streq(option, "tcrypt"))
205 arg_type = CRYPT_TCRYPT;
206 else if (streq(option, "tcrypt-hidden")) {
207 arg_type = CRYPT_TCRYPT;
208 arg_tcrypt_hidden = true;
209 } else if (streq(option, "tcrypt-system")) {
210 arg_type = CRYPT_TCRYPT;
211 arg_tcrypt_system = true;
212 } else if (streq(option, "tcrypt-veracrypt")) {
213 #ifdef CRYPT_TCRYPT_VERA_MODES
214 arg_type = CRYPT_TCRYPT;
215 arg_tcrypt_veracrypt = true;
216 #else
217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
218 "This version of cryptsetup does not support tcrypt-veracrypt; refusing.");
219 #endif
220 } else if (STR_IN_SET(option, "plain", "swap", "tmp"))
221 arg_type = CRYPT_PLAIN;
222 else if ((val = startswith(option, "timeout="))) {
223
224 r = parse_sec_fix_0(val, &arg_timeout);
225 if (r < 0) {
226 log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
227 return 0;
228 }
229
230 } else if ((val = startswith(option, "offset="))) {
231
232 r = safe_atou64(val, &arg_offset);
233 if (r < 0)
234 return log_error_errno(r, "Failed to parse %s: %m", option);
235
236 } else if ((val = startswith(option, "skip="))) {
237
238 r = safe_atou64(val, &arg_skip);
239 if (r < 0)
240 return log_error_errno(r, "Failed to parse %s: %m", option);
241
242 } else if (!streq(option, "none"))
243 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
244
245 return 0;
246 }
247
248 static int parse_options(const char *options) {
249 const char *word, *state;
250 size_t l;
251 int r;
252
253 assert(options);
254
255 FOREACH_WORD_SEPARATOR(word, l, options, ",", state) {
256 _cleanup_free_ char *o;
257
258 o = strndup(word, l);
259 if (!o)
260 return -ENOMEM;
261 r = parse_one_option(o);
262 if (r < 0)
263 return r;
264 }
265
266 /* sanity-check options */
267 if (arg_type != NULL && !streq(arg_type, CRYPT_PLAIN)) {
268 if (arg_offset)
269 log_warning("offset= ignored with type %s", arg_type);
270 if (arg_skip)
271 log_warning("skip= ignored with type %s", arg_type);
272 }
273
274 return 0;
275 }
276
277 static char* disk_description(const char *path) {
278 static const char name_fields[] =
279 "ID_PART_ENTRY_NAME\0"
280 "DM_NAME\0"
281 "ID_MODEL_FROM_DATABASE\0"
282 "ID_MODEL\0";
283
284 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
285 const char *i, *name;
286 struct stat st;
287
288 assert(path);
289
290 if (stat(path, &st) < 0)
291 return NULL;
292
293 if (!S_ISBLK(st.st_mode))
294 return NULL;
295
296 if (sd_device_new_from_devnum(&device, 'b', st.st_rdev) < 0)
297 return NULL;
298
299 NULSTR_FOREACH(i, name_fields)
300 if (sd_device_get_property_value(device, i, &name) >= 0 &&
301 !isempty(name))
302 return strdup(name);
303
304 return NULL;
305 }
306
307 static char *disk_mount_point(const char *label) {
308 _cleanup_free_ char *device = NULL;
309 _cleanup_endmntent_ FILE *f = NULL;
310 struct mntent *m;
311
312 /* Yeah, we don't support native systemd unit files here for now */
313
314 if (asprintf(&device, "/dev/mapper/%s", label) < 0)
315 return NULL;
316
317 f = setmntent("/etc/fstab", "re");
318 if (!f)
319 return NULL;
320
321 while ((m = getmntent(f)))
322 if (path_equal(m->mnt_fsname, device))
323 return strdup(m->mnt_dir);
324
325 return NULL;
326 }
327
328 static int get_password(const char *vol, const char *src, usec_t until, bool accept_cached, char ***ret) {
329 _cleanup_free_ char *description = NULL, *name_buffer = NULL, *mount_point = NULL, *text = NULL, *disk_path = NULL;
330 _cleanup_strv_free_erase_ char **passwords = NULL;
331 const char *name = NULL;
332 char **p, *id;
333 int r = 0;
334
335 assert(vol);
336 assert(src);
337 assert(ret);
338
339 description = disk_description(src);
340 mount_point = disk_mount_point(vol);
341
342 disk_path = cescape(src);
343 if (!disk_path)
344 return log_oom();
345
346 if (description && streq(vol, description))
347 /* If the description string is simply the
348 * volume name, then let's not show this
349 * twice */
350 description = mfree(description);
351
352 if (mount_point && description)
353 r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
354 else if (mount_point)
355 r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
356 else if (description)
357 r = asprintf(&name_buffer, "%s (%s)", description, vol);
358
359 if (r < 0)
360 return log_oom();
361
362 name = name_buffer ? name_buffer : vol;
363
364 if (asprintf(&text, "Please enter passphrase for disk %s:", name) < 0)
365 return log_oom();
366
367 id = strjoina("cryptsetup:", disk_path);
368
369 r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until,
370 ASK_PASSWORD_PUSH_CACHE | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED),
371 &passwords);
372 if (r < 0)
373 return log_error_errno(r, "Failed to query password: %m");
374
375 if (arg_verify) {
376 _cleanup_strv_free_erase_ char **passwords2 = NULL;
377
378 assert(strv_length(passwords) == 1);
379
380 if (asprintf(&text, "Please enter passphrase for disk %s (verification):", name) < 0)
381 return log_oom();
382
383 id = strjoina("cryptsetup-verification:", disk_path);
384
385 r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", until, ASK_PASSWORD_PUSH_CACHE, &passwords2);
386 if (r < 0)
387 return log_error_errno(r, "Failed to query verification password: %m");
388
389 assert(strv_length(passwords2) == 1);
390
391 if (!streq(passwords[0], passwords2[0])) {
392 log_warning("Passwords did not match, retrying.");
393 return -EAGAIN;
394 }
395 }
396
397 strv_uniq(passwords);
398
399 STRV_FOREACH(p, passwords) {
400 char *c;
401
402 if (strlen(*p)+1 >= arg_key_size)
403 continue;
404
405 /* Pad password if necessary */
406 c = new(char, arg_key_size);
407 if (!c)
408 return log_oom();
409
410 strncpy(c, *p, arg_key_size);
411 free_and_replace(*p, c);
412 }
413
414 *ret = TAKE_PTR(passwords);
415
416 return 0;
417 }
418
419 static int attach_tcrypt(
420 struct crypt_device *cd,
421 const char *name,
422 const char *key_file,
423 char **passwords,
424 uint32_t flags) {
425
426 int r = 0;
427 _cleanup_free_ char *passphrase = NULL;
428 struct crypt_params_tcrypt params = {
429 .flags = CRYPT_TCRYPT_LEGACY_MODES,
430 .keyfiles = (const char **)arg_tcrypt_keyfiles,
431 .keyfiles_count = strv_length(arg_tcrypt_keyfiles)
432 };
433
434 assert(cd);
435 assert(name);
436 assert(key_file || (passwords && passwords[0]));
437
438 if (arg_tcrypt_hidden)
439 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
440
441 if (arg_tcrypt_system)
442 params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
443
444 #ifdef CRYPT_TCRYPT_VERA_MODES
445 if (arg_tcrypt_veracrypt)
446 params.flags |= CRYPT_TCRYPT_VERA_MODES;
447 #endif
448
449 if (key_file) {
450 r = read_one_line_file(key_file, &passphrase);
451 if (r < 0) {
452 log_error_errno(r, "Failed to read password file '%s': %m", key_file);
453 return -EAGAIN;
454 }
455
456 params.passphrase = passphrase;
457 } else
458 params.passphrase = passwords[0];
459 params.passphrase_size = strlen(params.passphrase);
460
461 r = crypt_load(cd, CRYPT_TCRYPT, &params);
462 if (r < 0) {
463 if (key_file && r == -EPERM)
464 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN),
465 "Failed to activate using password file '%s'.",
466 key_file);
467 return r;
468 }
469
470 return crypt_activate_by_volume_key(cd, name, NULL, 0, flags);
471 }
472
473 static int attach_luks_or_plain(struct crypt_device *cd,
474 const char *name,
475 const char *key_file,
476 const char *data_device,
477 char **passwords,
478 uint32_t flags) {
479 int r = 0;
480 bool pass_volume_key = false;
481
482 assert(cd);
483 assert(name);
484 assert(key_file || passwords);
485
486 if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1)) {
487 r = crypt_load(cd, CRYPT_LUKS, NULL);
488 if (r < 0) {
489 log_error("crypt_load() failed on device %s.\n", crypt_get_device_name(cd));
490 return r;
491 }
492
493 if (data_device)
494 r = crypt_set_data_device(cd, data_device);
495 }
496
497 if ((!arg_type && r < 0) || streq_ptr(arg_type, CRYPT_PLAIN)) {
498 struct crypt_params_plain params = {
499 .offset = arg_offset,
500 .skip = arg_skip,
501 #if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
502 .sector_size = arg_sector_size,
503 #endif
504 };
505 const char *cipher, *cipher_mode;
506 _cleanup_free_ char *truncated_cipher = NULL;
507
508 if (arg_hash) {
509 /* plain isn't a real hash type. it just means "use no hash" */
510 if (!streq(arg_hash, "plain"))
511 params.hash = arg_hash;
512 } else if (!key_file)
513 /* for CRYPT_PLAIN, the behaviour of cryptsetup
514 * package is to not hash when a key file is provided */
515 params.hash = "ripemd160";
516
517 if (arg_cipher) {
518 size_t l;
519
520 l = strcspn(arg_cipher, "-");
521 truncated_cipher = strndup(arg_cipher, l);
522 if (!truncated_cipher)
523 return log_oom();
524
525 cipher = truncated_cipher;
526 cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain";
527 } else {
528 cipher = "aes";
529 cipher_mode = "cbc-essiv:sha256";
530 }
531
532 /* for CRYPT_PLAIN limit reads
533 * from keyfile to key length, and
534 * ignore keyfile-size */
535 arg_keyfile_size = arg_key_size;
536
537 /* In contrast to what the name
538 * crypt_setup() might suggest this
539 * doesn't actually format anything,
540 * it just configures encryption
541 * parameters when used for plain
542 * mode. */
543 r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, &params);
544
545 /* hash == NULL implies the user passed "plain" */
546 pass_volume_key = (params.hash == NULL);
547 }
548
549 if (r < 0)
550 return log_error_errno(r, "Loading of cryptographic parameters failed: %m");
551
552 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
553 crypt_get_cipher(cd),
554 crypt_get_cipher_mode(cd),
555 crypt_get_volume_key_size(cd)*8,
556 crypt_get_device_name(cd));
557
558 if (key_file) {
559 r = crypt_activate_by_keyfile_offset(cd, name, arg_key_slot, key_file, arg_keyfile_size, arg_keyfile_offset, flags);
560 if (r < 0) {
561 log_error_errno(r, "Failed to activate with key file '%s': %m", key_file);
562 return -EAGAIN;
563 }
564 } else {
565 char **p;
566
567 STRV_FOREACH(p, passwords) {
568 if (pass_volume_key)
569 r = crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags);
570 else
571 r = crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags);
572
573 if (r >= 0)
574 break;
575 }
576 }
577
578 return r;
579 }
580
581 static int help(void) {
582 _cleanup_free_ char *link = NULL;
583 int r;
584
585 r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link);
586 if (r < 0)
587 return log_oom();
588
589 printf("%s attach VOLUME SOURCEDEVICE [PASSWORD] [OPTIONS]\n"
590 "%s detach VOLUME\n\n"
591 "Attaches or detaches an encrypted block device.\n"
592 "\nSee the %s for details.\n"
593 , program_invocation_short_name
594 , program_invocation_short_name
595 , link
596 );
597
598 return 0;
599 }
600
601 static int run(int argc, char *argv[]) {
602 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
603 int r;
604
605 if (argc <= 1)
606 return help();
607
608 if (argc < 3) {
609 log_error("This program requires at least two arguments.");
610 return -EINVAL;
611 }
612
613 log_setup_service();
614
615 umask(0022);
616
617 if (streq(argv[1], "attach")) {
618 uint32_t flags = 0;
619 unsigned tries;
620 usec_t until;
621 crypt_status_info status;
622 const char *key_file = NULL;
623
624 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [PASSWORD] [OPTIONS] */
625
626 if (argc < 4) {
627 log_error("attach requires at least two arguments.");
628 return -EINVAL;
629 }
630
631 if (argc >= 5 &&
632 argv[4][0] &&
633 !streq(argv[4], "-") &&
634 !streq(argv[4], "none")) {
635
636 if (!path_is_absolute(argv[4]))
637 log_error("Password file path '%s' is not absolute. Ignoring.", argv[4]);
638 else
639 key_file = argv[4];
640 }
641
642 if (argc >= 6 && argv[5][0] && !streq(argv[5], "-")) {
643 r = parse_options(argv[5]);
644 if (r < 0)
645 return r;
646 }
647
648 /* A delicious drop of snake oil */
649 mlockall(MCL_FUTURE);
650
651 if (arg_header) {
652 log_debug("LUKS header: %s", arg_header);
653 r = crypt_init(&cd, arg_header);
654 } else
655 r = crypt_init(&cd, argv[3]);
656 if (r < 0)
657 return log_error_errno(r, "crypt_init() failed: %m");
658
659 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
660
661 status = crypt_status(cd, argv[2]);
662 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
663 log_info("Volume %s already active.", argv[2]);
664 return 0;
665 }
666
667 if (arg_readonly)
668 flags |= CRYPT_ACTIVATE_READONLY;
669
670 if (arg_discards)
671 flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
672
673 if (arg_timeout == USEC_INFINITY)
674 until = 0;
675 else
676 until = now(CLOCK_MONOTONIC) + arg_timeout;
677
678 arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8));
679
680 if (key_file) {
681 struct stat st;
682
683 /* Ideally we'd do this on the open fd, but since this is just a
684 * warning it's OK to do this in two steps. */
685 if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005))
686 log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
687 }
688
689 for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
690 _cleanup_strv_free_erase_ char **passwords = NULL;
691
692 if (!key_file) {
693 r = get_password(argv[2], argv[3], until, tries == 0 && !arg_verify, &passwords);
694 if (r == -EAGAIN)
695 continue;
696 if (r < 0)
697 return r;
698 }
699
700 if (streq_ptr(arg_type, CRYPT_TCRYPT))
701 r = attach_tcrypt(cd, argv[2], key_file, passwords, flags);
702 else
703 r = attach_luks_or_plain(cd,
704 argv[2],
705 key_file,
706 arg_header ? argv[3] : NULL,
707 passwords,
708 flags);
709 if (r >= 0)
710 break;
711 if (r == -EAGAIN) {
712 key_file = NULL;
713 continue;
714 }
715 if (r != -EPERM)
716 return log_error_errno(r, "Failed to activate: %m");
717
718 log_warning("Invalid passphrase.");
719 }
720
721 if (arg_tries != 0 && tries >= arg_tries) {
722 log_error("Too many attempts; giving up.");
723 return -EPERM;
724 }
725
726 } else if (streq(argv[1], "detach")) {
727
728 r = crypt_init_by_name(&cd, argv[2]);
729 if (r == -ENODEV) {
730 log_info("Volume %s already inactive.", argv[2]);
731 return 0;
732 }
733 if (r < 0)
734 return log_error_errno(r, "crypt_init_by_name() failed: %m");
735
736 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
737
738 r = crypt_deactivate(cd, argv[2]);
739 if (r < 0)
740 return log_error_errno(r, "Failed to deactivate: %m");
741
742 } else {
743 log_error("Unknown verb %s.", argv[1]);
744 return -EINVAL;
745 }
746
747 return 0;
748 }
749
750 DEFINE_MAIN_FUNCTION(run);