2 * integritysetup - setup integrity protected volumes for dm-integrity
4 * Copyright (C) 2017-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2017-2021 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
23 #include <uuid/uuid.h>
25 #define PACKAGE_INTEGRITY "integritysetup"
27 #define DEFAULT_ALG_NAME "crc32c"
29 static char *opt_data_device = NULL;
30 static char *opt_integrity = NULL; /* DEFAULT_ALG_NAME */
31 static char *opt_integrity_key_file = NULL;
32 static char *opt_journal_integrity = NULL; /* none */
33 static char *opt_journal_integrity_key_file = NULL;
34 static char *opt_journal_crypt = NULL; /* none */
35 static char *opt_journal_crypt_key_file = NULL;
37 /* helper strings converted to uint64_t later */
38 static char *opt_journal_size_str = NULL;
40 static uint64_t opt_journal_size = 0;
42 static int opt_interleave_sectors = 0;
43 static int opt_journal_watermark = 0;
44 static int opt_bitmap_sectors_per_bit = 0;
45 static int opt_journal_commit_time = 0;
46 static int opt_bitmap_flush_time = 0;
47 static int opt_tag_size = 0;
48 static int opt_sector_size = 0;
49 static int opt_buffer_sectors = 0;
50 static int opt_no_wipe = 0;
51 static int opt_integrity_key_size = 0;
52 static int opt_journal_integrity_key_size = 0;
53 static int opt_journal_crypt_key_size = 0;
54 static int opt_integrity_nojournal = 0;
55 static int opt_integrity_recovery = 0;
56 static int opt_integrity_bitmap = 0;
57 static int opt_integrity_legacy_padding = 0;
58 static int opt_integrity_legacy_hmac = 0;
59 static int opt_integrity_legacy_recalculate = 0;
60 static int opt_integrity_recalculate = 0;
61 static int opt_allow_discards = 0;
63 static const char *integrity_alg = DEFAULT_ALG_NAME;
64 static const char **action_argv;
65 static int action_argc;
67 void tools_cleanup(void)
69 FREE_AND_NULL(opt_data_device);
70 FREE_AND_NULL(opt_integrity);
71 FREE_AND_NULL(opt_integrity_key_file);
72 FREE_AND_NULL(opt_journal_integrity);
73 FREE_AND_NULL(opt_journal_integrity_key_file);
74 FREE_AND_NULL(opt_journal_crypt);
75 FREE_AND_NULL(opt_journal_crypt_key_file);
76 FREE_AND_NULL(opt_journal_size_str);
79 // FIXME: move this to tools and handle EINTR
80 static int _read_mk(const char *file, char **key, int keysize)
84 if (keysize <= 0 || keysize > (DEFAULT_INTEGRITY_KEYFILE_SIZE_MAXKB * 1024)) {
85 log_err(_("Invalid key size. Maximum is %u bytes."), DEFAULT_INTEGRITY_KEYFILE_SIZE_MAXKB * 1024);
89 *key = crypt_safe_alloc(keysize);
93 fd = open(file, O_RDONLY);
95 log_err(_("Cannot read keyfile %s."), file);
98 if ((read(fd, *key, keysize) != keysize)) {
99 log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
106 crypt_safe_free(*key);
111 static int _read_keys(char **integrity_key, struct crypt_params_integrity *params)
113 char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL;
116 if (integrity_key && opt_integrity_key_file) {
117 r = _read_mk(opt_integrity_key_file, &int_key, opt_integrity_key_size);
120 params->integrity_key_size = opt_integrity_key_size;
123 if (opt_journal_integrity_key_file) {
124 r = _read_mk(opt_journal_integrity_key_file, &journal_integrity_key, opt_journal_integrity_key_size);
126 crypt_safe_free(int_key);
129 params->journal_integrity_key = journal_integrity_key;
130 params->journal_integrity_key_size = opt_journal_integrity_key_size;
133 if (opt_journal_crypt_key_file) {
134 r = _read_mk(opt_journal_crypt_key_file, &journal_crypt_key, opt_journal_crypt_key_size);
136 crypt_safe_free(int_key);
137 crypt_safe_free(journal_integrity_key);
140 params->journal_crypt_key = journal_crypt_key;
141 params->journal_crypt_key_size = opt_journal_crypt_key_size;
145 *integrity_key = int_key;
150 static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key)
152 char tmp_name[64], tmp_path[128], tmp_uuid[40];
157 log_std(_("Wiping device to initialize integrity checksum.\n"
158 "You can interrupt this by pressing CTRL+c "
159 "(rest of not wiped device will contain invalid checksum).\n"));
161 /* Activate the device a temporary one */
162 uuid_generate(tmp_uuid_bin);
163 uuid_unparse(tmp_uuid_bin, tmp_uuid);
164 if (snprintf(tmp_name, sizeof(tmp_name), "temporary-cryptsetup-%s", tmp_uuid) < 0)
166 if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_name) < 0)
169 r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key,
170 opt_integrity_key_size, CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_NO_JOURNAL);
174 /* Wipe the device */
176 r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK,
177 0, &tools_wipe_progress, NULL);
178 if (crypt_deactivate(cd, tmp_name))
179 log_err(_("Cannot deactivate temporary device %s."), tmp_path);
185 static int action_format(int arg)
187 struct crypt_device *cd = NULL;
188 struct crypt_params_integrity params = {
189 .journal_size = opt_journal_size,
190 .interleave_sectors = opt_interleave_sectors,
191 /* in bitmap mode we have to overload these values... */
192 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
193 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
194 .buffer_sectors = opt_buffer_sectors,
195 .tag_size = opt_tag_size,
196 .sector_size = opt_sector_size ?: SECTOR_SIZE,
198 char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
199 char *integrity_key = NULL, *msg = NULL;
203 r = crypt_parse_hash_integrity_mode(integrity_alg, integrity);
205 log_err(_("No known integrity specification pattern detected."));
208 params.integrity = integrity;
210 if (opt_journal_integrity) {
211 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
213 log_err(_("No known integrity specification pattern detected."));
216 params.journal_integrity = journal_integrity;
219 if (opt_journal_crypt) {
220 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
222 log_err(_("No known integrity specification pattern detected."));
225 params.journal_crypt = journal_crypt;
228 r = _read_keys(&integrity_key, ¶ms);
232 r = crypt_init_data_device(&cd, action_argv[0], opt_data_device);
236 r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]);
242 r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL;
247 r = tools_detect_signatures(action_argv[0], 0, &signatures);
251 /* Signature candidates found */
252 if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0))
255 if (opt_integrity_legacy_padding)
256 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING);
258 if (opt_integrity_legacy_hmac)
259 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_HMAC);
261 r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, ¶ms);
262 if (r < 0) /* FIXME: call wipe signatures again */
265 if (!opt_batch_mode && !crypt_get_integrity_info(cd, ¶ms2))
266 log_std(_("Formatted with tag size %u, internal integrity %s.\n"),
267 params2.tag_size, params2.integrity);
270 r = _wipe_data_device(cd, integrity_key);
272 crypt_safe_free(integrity_key);
273 crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
274 crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
279 static int action_open(int arg)
281 struct crypt_device *cd = NULL;
282 struct crypt_params_integrity params = {
283 /* in bitmap mode we have to overload these values... */
284 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
285 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
286 .buffer_sectors = opt_buffer_sectors,
288 uint32_t activate_flags = 0;
289 char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
290 char *integrity_key = NULL;
293 r = crypt_parse_hash_integrity_mode(integrity_alg, integrity);
295 log_err(_("No known integrity specification pattern detected."));
298 params.integrity = integrity;
300 if (opt_journal_integrity) {
301 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
303 log_err(_("No known integrity specification pattern detected."));
307 params.journal_integrity = journal_integrity;
310 if (opt_journal_crypt) {
311 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
313 log_err(_("No known integrity specification pattern detected."));
316 params.journal_crypt = journal_crypt;
319 if (opt_integrity_nojournal || opt_integrity_bitmap)
320 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
321 if (opt_integrity_recovery)
322 activate_flags |= CRYPT_ACTIVATE_RECOVERY;
323 if (opt_integrity_bitmap)
324 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
326 if (opt_integrity_recalculate || opt_integrity_legacy_recalculate)
327 activate_flags |= CRYPT_ACTIVATE_RECALCULATE;
328 if (opt_allow_discards)
329 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
331 r = _read_keys(&integrity_key, ¶ms);
335 if ((r = crypt_init_data_device(&cd, action_argv[0], opt_data_device)))
338 r = crypt_load(cd, CRYPT_INTEGRITY, ¶ms);
342 if (opt_integrity_legacy_recalculate)
343 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_RECALC);
345 r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key,
346 opt_integrity_key_size, activate_flags);
348 crypt_safe_free(integrity_key);
349 crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
350 crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
355 static int action_close(int arg)
357 struct crypt_device *cd = NULL;
360 r = crypt_init_by_name(&cd, action_argv[0]);
362 r = crypt_deactivate(cd, action_argv[0]);
368 static int action_status(int arg)
370 crypt_status_info ci;
371 struct crypt_active_device cad;
372 struct crypt_params_integrity ip = {};
373 struct crypt_device *cd = NULL;
375 const char *device, *metadata_device;
378 /* perhaps a path, not a dm device name */
379 if (strchr(action_argv[0], '/'))
382 ci = crypt_status(NULL, action_argv[0]);
389 log_std("%s is inactive.\n", action_argv[0]);
391 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
397 log_std("%s is active%s.\n", action_argv[0],
398 ci == CRYPT_BUSY ? " and is in use" : "");
400 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
401 ci == CRYPT_BUSY ? " and is in use" : "");
403 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
407 log_std(" type: %s\n", crypt_get_type(cd) ?: "n/a");
409 r = crypt_get_active_device(cd, action_argv[0], &cad);
413 /* Print only INTEGRITY (and LUKS2 with integrity) info */
414 r = crypt_get_integrity_info(cd, &ip);
418 log_std(" tag size: %u\n", ip.tag_size);
419 log_std(" integrity: %s\n", ip.integrity ?: "(none)");
420 device = crypt_get_device_name(cd);
421 metadata_device = crypt_get_metadata_device_name(cd);
422 log_std(" device: %s%s\n", device, metadata_device ? " (detached)" : "");
423 if ((backing_file = crypt_loop_backing_file(device))) {
424 log_std(" loop: %s\n", backing_file);
427 if (metadata_device) {
428 log_std(" metadata device: %s\n", metadata_device);
429 if ((backing_file = crypt_loop_backing_file(metadata_device))) {
430 log_std(" loop: %s\n", backing_file);
434 log_std(" sector size: %u bytes\n", crypt_get_sector_size(cd));
435 log_std(" interleave sectors: %u\n", ip.interleave_sectors);
436 log_std(" size: %" PRIu64 " sectors\n", cad.size);
437 log_std(" mode: %s%s\n",
438 cad.flags & CRYPT_ACTIVATE_READONLY ? "readonly" : "read/write",
439 cad.flags & CRYPT_ACTIVATE_RECOVERY ? " recovery" : "");
440 log_std(" failures: %" PRIu64 "\n",
441 crypt_get_active_integrity_failures(cd, action_argv[0]));
442 if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) {
443 log_std(" bitmap 512-byte sectors per bit: %u\n", ip.journal_watermark);
444 log_std(" bitmap flush interval: %u ms\n", ip.journal_commit_time);
445 } if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL) {
446 log_std(" journal: not active\n");
448 log_std(" journal size: %" PRIu64 " bytes\n", ip.journal_size);
449 log_std(" journal watermark: %u%%\n", ip.journal_watermark);
450 log_std(" journal commit time: %u ms\n", ip.journal_commit_time);
451 if (ip.journal_integrity)
452 log_std(" journal integrity MAC: %s\n", ip.journal_integrity);
453 if (ip.journal_crypt)
454 log_std(" journal encryption: %s\n", ip.journal_crypt);
456 if (cad.flags & (CRYPT_ACTIVATE_ALLOW_DISCARDS))
457 log_std(" flags: %s\n",
458 (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "discards " : "");
468 static int action_dump(int arg)
470 struct crypt_device *cd = NULL;
471 struct crypt_params_integrity params = {};
474 if ((r = crypt_init(&cd, action_argv[0])))
477 r = crypt_load(cd, CRYPT_INTEGRITY, ¶ms);
485 static struct action_type {
488 int required_action_argc;
489 const char *arg_desc;
492 { "format", action_format, 1, N_("<integrity_device>"),N_("format device") },
493 { "open", action_open, 2, N_("<integrity_device> <name>"),N_("open device as <name>") },
494 { "close", action_close, 1, N_("<name>"),N_("close device (remove mapping)") },
495 { "status", action_status, 1, N_("<name>"),N_("show active device status") },
496 { "dump", action_dump, 1, N_("<integrity_device>"),N_("show on-disk information") },
497 { NULL, NULL, 0, NULL, NULL }
500 static void help(poptContext popt_context,
501 enum poptCallbackReason reason __attribute__((unused)),
502 struct poptOption *key,
503 const char *arg __attribute__((unused)),
504 void *data __attribute__((unused)))
506 struct action_type *action;
508 if (key->shortName == '?') {
509 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
510 poptPrintHelp(popt_context, stdout, 0);
512 "<action> is one of:\n"));
513 for(action = action_types; action->type; action++)
514 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
516 "<name> is the device to create under %s\n"
517 "<integrity_device> is the device containing data with integrity tags\n"),
520 log_std(_("\nDefault compiled-in dm-integrity parameters:\n"
521 "\tChecksum algorithm: %s\n"
522 "\tMaximum keyfile size: %dkB\n"),
523 DEFAULT_ALG_NAME, DEFAULT_INTEGRITY_KEYFILE_SIZE_MAXKB);
525 poptFreeContext(popt_context);
527 } else if (key->shortName == 'V') {
528 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
530 poptFreeContext(popt_context);
533 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
536 static int run_action(struct action_type *action)
540 log_dbg("Running command %s.", action->type);
542 r = action->handler(0);
545 return translate_errno(r);
548 int main(int argc, const char **argv)
550 static const char *null_action_argv[] = {NULL};
551 static struct poptOption popt_help_options[] = {
552 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
553 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
554 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
555 { "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package version"), NULL },
558 static struct poptOption popt_options[] = {
559 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
560 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
561 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
562 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
563 { "progress-frequency", '\0', POPT_ARG_INT, &opt_progress_frequency, 0, N_("Progress line update (in seconds)"), N_("secs") },
564 { "no-wipe", '\0', POPT_ARG_NONE, &opt_no_wipe, 0, N_("Do not wipe device after format"), NULL },
566 { "data-device", '\0', POPT_ARG_STRING, &opt_data_device, 0, N_("Path to data device (if separated)"), N_("path") },
568 { "journal-size", 'j', POPT_ARG_STRING,&opt_journal_size_str, 0, N_("Journal size"), N_("bytes") },
569 { "interleave-sectors", '\0', POPT_ARG_INT, &opt_interleave_sectors, 0, N_("Interleave sectors"), N_("SECTORS") },
570 { "journal-watermark", '\0', POPT_ARG_INT, &opt_journal_watermark, 0, N_("Journal watermark"),N_("percent") },
571 { "journal-commit-time",'\0', POPT_ARG_INT, &opt_journal_commit_time,0, N_("Journal commit time"), N_("ms") },
572 { "bitmap-sectors-per-bit",'\0', POPT_ARG_INT,&opt_bitmap_sectors_per_bit, 0, N_("Number of 512-byte sectors per bit (bitmap mode)."), NULL },
573 { "bitmap-flush-time", '\0', POPT_ARG_INT, &opt_bitmap_flush_time, 0, N_("Bitmap mode flush time"), N_("ms") },
574 { "tag-size", 't', POPT_ARG_INT, &opt_tag_size, 0, N_("Tag size (per-sector)"), N_("bytes") },
575 { "sector-size", 's', POPT_ARG_INT, &opt_sector_size, 0, N_("Sector size"), N_("bytes") },
576 { "buffer-sectors", '\0', POPT_ARG_INT, &opt_buffer_sectors, 0, N_("Buffers size"), N_("SECTORS") },
578 { "integrity", 'I', POPT_ARG_STRING, &opt_integrity, 0, N_("Data integrity algorithm"), NULL },
579 { "integrity-key-size", '\0', POPT_ARG_INT, &opt_integrity_key_size, 0, N_("The size of the data integrity key"), N_("BITS") },
580 { "integrity-key-file", '\0', POPT_ARG_STRING, &opt_integrity_key_file, 0, N_("Read the integrity key from a file"), NULL },
582 { "journal-integrity", '\0', POPT_ARG_STRING, &opt_journal_integrity, 0, N_("Journal integrity algorithm"), NULL },
583 { "journal-integrity-key-size",'\0', POPT_ARG_INT, &opt_journal_integrity_key_size,0, N_("The size of the journal integrity key"), N_("BITS") },
584 { "journal-integrity-key-file",'\0', POPT_ARG_STRING, &opt_journal_integrity_key_file,0, N_("Read the journal integrity key from a file"), NULL },
586 { "journal-crypt", '\0', POPT_ARG_STRING, &opt_journal_crypt, 0, N_("Journal encryption algorithm"), NULL },
587 { "journal-crypt-key-size", '\0', POPT_ARG_INT, &opt_journal_crypt_key_size, 0, N_("The size of the journal encryption key"), N_("BITS") },
588 { "journal-crypt-key-file", '\0', POPT_ARG_STRING, &opt_journal_crypt_key_file, 0, N_("Read the journal encryption key from a file"), NULL },
590 { "integrity-no-journal", 'D', POPT_ARG_NONE, &opt_integrity_nojournal, 0, N_("Disable journal for integrity device"), NULL },
591 { "integrity-recovery-mode", 'R', POPT_ARG_NONE, &opt_integrity_recovery, 0, N_("Recovery mode (no journal, no tag checking)"), NULL },
592 { "integrity-bitmap-mode", 'B', POPT_ARG_NONE, &opt_integrity_bitmap, 0, N_("Use bitmap to track changes and disable journal for integrity device"), NULL },
593 { "integrity-recalculate", '\0', POPT_ARG_NONE, &opt_integrity_recalculate, 0, N_("Recalculate initial tags automatically."), NULL },
594 { "integrity-legacy-padding", '\0', POPT_ARG_NONE, &opt_integrity_legacy_padding, 0, N_("Use inefficient legacy padding (old kernels)"), NULL },
596 { "integrity-legacy-hmac", '\0', POPT_ARG_NONE, &opt_integrity_legacy_hmac, 0, N_("Do not protect superblock with HMAC (old kernels)"), NULL },
597 { "integrity-legacy-recalculate",'\0',POPT_ARG_NONE, &opt_integrity_legacy_recalculate, 0, N_("Allow recalculating of volumes with HMAC keys (old kernels)"), NULL },
599 { "allow-discards", '\0', POPT_ARG_NONE, &opt_allow_discards, 0, N_("Allow discards (aka TRIM) requests for device"), NULL },
602 poptContext popt_context;
603 struct action_type *action;
607 crypt_set_log_callback(NULL, tool_log, NULL);
609 setlocale(LC_ALL, "");
610 bindtextdomain(PACKAGE, LOCALEDIR);
613 popt_context = poptGetContext("integrity", argc, argv, popt_options, 0);
614 poptSetOtherOptionHelp(popt_context,
615 _("[OPTION...] <action> <action-specific>"));
618 while ((r = poptGetNextOpt(popt_context)) >= 0) {
622 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
623 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
625 if (!(aname = poptGetArg(popt_context)))
626 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
627 poptGetInvocationName(popt_context));
630 action_argv = poptGetArgs(popt_context);
631 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
633 action_argv = null_action_argv;
635 /* Count args, somewhat unnice, change? */
636 while (action_argv[action_argc] != NULL)
640 if (!strcmp(aname, "create") && action_argc > 1) {
641 /* create command had historically switched arguments */
642 if (action_argv[0] && action_argv[1]) {
643 const char *tmp = action_argv[0];
644 action_argv[0] = action_argv[1];
645 action_argv[1] = tmp;
648 } else if (!strcmp(aname, "remove")) {
653 integrity_alg = opt_integrity;
655 for (action = action_types; action->type; action++)
656 if (strcmp(action->type, aname) == 0)
660 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
661 poptGetInvocationName(popt_context));
663 if (action_argc < action->required_action_argc) {
665 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
666 usage(popt_context, EXIT_FAILURE, buf,
667 poptGetInvocationName(popt_context));
670 if (opt_integrity_recalculate && strcmp(aname, "open"))
671 usage(popt_context, EXIT_FAILURE,
672 _("Option --integrity-recalculate can be used only for open action."),
673 poptGetInvocationName(popt_context));
675 if (opt_allow_discards && strcmp(aname, "open"))
676 usage(popt_context, EXIT_FAILURE,
677 _("Option --allow-discards is allowed only for open operation."),
678 poptGetInvocationName(popt_context));
680 if (opt_interleave_sectors < 0 || opt_journal_watermark < 0 ||
681 opt_journal_commit_time < 0 || opt_tag_size < 0 ||
682 opt_sector_size < 0 || opt_buffer_sectors < 0 ||
683 opt_integrity_key_size < 0 || opt_journal_integrity_key_size < 0 ||
684 opt_journal_crypt_key_size < 0 || opt_bitmap_flush_time < 0 || opt_bitmap_sectors_per_bit < 0)
685 usage(popt_context, EXIT_FAILURE,
686 _("Negative number for option not permitted."),
687 poptGetInvocationName(popt_context));
689 if (strcmp(aname, "format") && (opt_journal_size_str || opt_interleave_sectors ||
690 opt_sector_size || opt_tag_size || opt_no_wipe ))
691 usage(popt_context, EXIT_FAILURE,
692 _("Options --journal-size, --interleave-sectors, --sector-size, --tag-size"
693 " and --no-wipe can be used only for format action."),
694 poptGetInvocationName(popt_context));
696 if (opt_journal_size_str &&
697 tools_string_to_size(NULL, opt_journal_size_str, &opt_journal_size))
698 usage(popt_context, EXIT_FAILURE, _("Invalid journal size specification."),
699 poptGetInvocationName(popt_context));
701 if ((opt_integrity_key_file && !opt_integrity_key_size) ||
702 (!opt_integrity_key_file && opt_integrity_key_size))
703 usage(popt_context, EXIT_FAILURE, _("Both key file and key size options must be specified."),
704 poptGetInvocationName(popt_context));
706 if ((opt_journal_integrity_key_file && !opt_journal_integrity_key_size) ||
707 (!opt_journal_integrity_key_file && opt_journal_integrity_key_size))
708 usage(popt_context, EXIT_FAILURE, _("Both journal integrity key file and key size options must be specified."),
709 poptGetInvocationName(popt_context));
710 if (!opt_journal_integrity && opt_journal_integrity_key_file)
711 usage(popt_context, EXIT_FAILURE, _("Journal integrity algorithm must be specified if journal integrity key is used."),
712 poptGetInvocationName(popt_context));
714 if ((opt_journal_crypt_key_file && !opt_journal_crypt_key_size) ||
715 (!opt_journal_crypt_key_file && opt_journal_crypt_key_size))
716 usage(popt_context, EXIT_FAILURE, _("Both journal encryption key file and key size options must be specified."),
717 poptGetInvocationName(popt_context));
718 if (!opt_journal_crypt && opt_journal_crypt_key_file)
719 usage(popt_context, EXIT_FAILURE, _("Journal encryption algorithm must be specified if journal encryption key is used."),
720 poptGetInvocationName(popt_context));
722 if (opt_integrity_recovery && opt_integrity_bitmap)
723 usage(popt_context, EXIT_FAILURE, _("Recovery and bitmap mode options are mutually exclusive."),
724 poptGetInvocationName(popt_context));
726 if (opt_integrity_bitmap && (opt_journal_integrity_key_file || opt_journal_crypt || opt_journal_watermark || opt_journal_commit_time))
727 usage(popt_context, EXIT_FAILURE, _("Journal options cannot be used in bitmap mode."),
728 poptGetInvocationName(popt_context));
730 if (!opt_integrity_bitmap && (opt_bitmap_flush_time || opt_bitmap_sectors_per_bit))
731 usage(popt_context, EXIT_FAILURE, _("Bitmap options can be used only in bitmap mode."),
732 poptGetInvocationName(popt_context));
736 crypt_set_debug_level(-1);
737 dbg_version_and_cmd(argc, argv);
740 r = run_action(action);
742 poptFreeContext(popt_context);