Imported Upstream version 2.3.7
[platform/upstream/cryptsetup.git] / src / integritysetup.c
1 /*
2  * integritysetup - setup integrity protected volumes for dm-integrity
3  *
4  * Copyright (C) 2017-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2017-2021 Milan Broz
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "cryptsetup.h"
23 #include <uuid/uuid.h>
24
25 #define PACKAGE_INTEGRITY "integritysetup"
26
27 #define DEFAULT_ALG_NAME "crc32c"
28
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;
36
37 /* helper strings converted to uint64_t later */
38 static char *opt_journal_size_str = NULL;
39
40 static uint64_t opt_journal_size = 0;
41
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;
62
63 static const char *integrity_alg = DEFAULT_ALG_NAME;
64 static const char **action_argv;
65 static int action_argc;
66
67 void tools_cleanup(void)
68 {
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);
77 }
78
79 // FIXME: move this to tools and handle EINTR
80 static int _read_mk(const char *file, char **key, int keysize)
81 {
82         int fd;
83
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);
86                 return -EINVAL;
87         }
88
89         *key = crypt_safe_alloc(keysize);
90         if (!*key)
91                 return -ENOMEM;
92
93         fd = open(file, O_RDONLY);
94         if (fd == -1) {
95                 log_err(_("Cannot read keyfile %s."), file);
96                 goto fail;
97         }
98         if ((read(fd, *key, keysize) != keysize)) {
99                 log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
100                 close(fd);
101                 goto fail;
102         }
103         close(fd);
104         return 0;
105 fail:
106         crypt_safe_free(*key);
107         *key = NULL;
108         return -EINVAL;
109 }
110
111 static int _read_keys(char **integrity_key, struct crypt_params_integrity *params)
112 {
113         char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL;
114         int r;
115
116         if (integrity_key && opt_integrity_key_file) {
117                 r = _read_mk(opt_integrity_key_file, &int_key, opt_integrity_key_size);
118                 if (r < 0)
119                         return r;
120                 params->integrity_key_size = opt_integrity_key_size;
121         }
122
123         if (opt_journal_integrity_key_file) {
124                 r = _read_mk(opt_journal_integrity_key_file, &journal_integrity_key, opt_journal_integrity_key_size);
125                 if (r < 0) {
126                         crypt_safe_free(int_key);
127                         return r;
128                 }
129                 params->journal_integrity_key = journal_integrity_key;
130                 params->journal_integrity_key_size = opt_journal_integrity_key_size;
131         }
132
133         if (opt_journal_crypt_key_file) {
134                 r = _read_mk(opt_journal_crypt_key_file, &journal_crypt_key, opt_journal_crypt_key_size);
135                 if (r < 0) {
136                         crypt_safe_free(int_key);
137                         crypt_safe_free(journal_integrity_key);
138                         return r;
139                 }
140                 params->journal_crypt_key = journal_crypt_key;
141                 params->journal_crypt_key_size = opt_journal_crypt_key_size;
142         }
143
144         if (integrity_key)
145                 *integrity_key = int_key;
146
147         return 0;
148 }
149
150 static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key)
151 {
152         char tmp_name[64], tmp_path[128], tmp_uuid[40];
153         uuid_t tmp_uuid_bin;
154         int r;
155
156         if (!opt_batch_mode)
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"));
160
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)
165                 return -EINVAL;
166         if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_name) < 0)
167                 return -EINVAL;
168
169         r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key,
170                 opt_integrity_key_size, CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_NO_JOURNAL);
171         if (r < 0)
172                 return r;
173
174         /* Wipe the device */
175         set_int_handler(0);
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);
180         set_int_block(0);
181
182         return r;
183 }
184
185 static int action_format(int arg)
186 {
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,
197         }, params2;
198         char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
199         char *integrity_key = NULL, *msg = NULL;
200         int r;
201         size_t signatures;
202
203         r = crypt_parse_hash_integrity_mode(integrity_alg, integrity);
204         if (r < 0) {
205                 log_err(_("No known integrity specification pattern detected."));
206                 return r;
207         }
208         params.integrity = integrity;
209
210         if (opt_journal_integrity) {
211                 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
212                 if (r < 0) {
213                         log_err(_("No known integrity specification pattern detected."));
214                         return r;
215                 }
216                 params.journal_integrity = journal_integrity;
217         }
218
219         if (opt_journal_crypt) {
220                 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
221                 if (r < 0) {
222                         log_err(_("No known integrity specification pattern detected."));
223                         return r;
224                 }
225                 params.journal_crypt = journal_crypt;
226         }
227
228         r = _read_keys(&integrity_key, &params);
229         if (r)
230                 goto out;
231
232         r = crypt_init_data_device(&cd, action_argv[0], opt_data_device);
233         if (r < 0)
234                 goto out;
235
236         r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]);
237         if (r == -1) {
238                 r = -ENOMEM;
239                 goto out;
240         }
241
242         r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL;
243         free(msg);
244         if (r < 0)
245                 goto out;
246
247         r = tools_detect_signatures(action_argv[0], 0, &signatures);
248         if (r < 0)
249                 goto out;
250
251         /* Signature candidates found */
252         if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0))
253                 goto out;
254
255         if (opt_integrity_legacy_padding)
256                 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING);
257
258         if (opt_integrity_legacy_hmac)
259                 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_HMAC);
260
261         r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, &params);
262         if (r < 0) /* FIXME: call wipe signatures again */
263                 goto out;
264
265         if (!opt_batch_mode && !crypt_get_integrity_info(cd, &params2))
266                 log_std(_("Formatted with tag size %u, internal integrity %s.\n"),
267                         params2.tag_size, params2.integrity);
268
269         if (!opt_no_wipe)
270                 r = _wipe_data_device(cd, integrity_key);
271 out:
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);
275         crypt_free(cd);
276         return r;
277 }
278
279 static int action_open(int arg)
280 {
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,
287         };
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;
291         int r;
292
293         r = crypt_parse_hash_integrity_mode(integrity_alg, integrity);
294         if (r < 0) {
295                 log_err(_("No known integrity specification pattern detected."));
296                 return r;
297         }
298         params.integrity = integrity;
299
300         if (opt_journal_integrity) {
301                 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
302                 if (r < 0) {
303                         log_err(_("No known integrity specification pattern detected."));
304                         return r;
305
306                 }
307                 params.journal_integrity = journal_integrity;
308         }
309
310         if (opt_journal_crypt) {
311                 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
312                 if (r < 0) {
313                         log_err(_("No known integrity specification pattern detected."));
314                         return r;
315                 }
316                 params.journal_crypt = journal_crypt;
317         }
318
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;
325
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;
330
331         r = _read_keys(&integrity_key, &params);
332         if (r)
333                 goto out;
334
335         if ((r = crypt_init_data_device(&cd, action_argv[0], opt_data_device)))
336                 goto out;
337
338         r = crypt_load(cd, CRYPT_INTEGRITY, &params);
339         if (r)
340                 goto out;
341
342         if (opt_integrity_legacy_recalculate)
343                 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_RECALC);
344
345         r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key,
346                                          opt_integrity_key_size, activate_flags);
347 out:
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);
351         crypt_free(cd);
352         return r;
353 }
354
355 static int action_close(int arg)
356 {
357         struct crypt_device *cd = NULL;
358         int r;
359
360         r = crypt_init_by_name(&cd, action_argv[0]);
361         if (r == 0)
362                 r = crypt_deactivate(cd, action_argv[0]);
363
364         crypt_free(cd);
365         return r;
366 }
367
368 static int action_status(int arg)
369 {
370         crypt_status_info ci;
371         struct crypt_active_device cad;
372         struct crypt_params_integrity ip = {};
373         struct crypt_device *cd = NULL;
374         char *backing_file;
375         const char *device, *metadata_device;
376         int path = 0, r = 0;
377
378         /* perhaps a path, not a dm device name */
379         if (strchr(action_argv[0], '/'))
380                 path = 1;
381
382         ci = crypt_status(NULL, action_argv[0]);
383         switch (ci) {
384         case CRYPT_INVALID:
385                 r = -EINVAL;
386                 break;
387         case CRYPT_INACTIVE:
388                 if (path)
389                         log_std("%s is inactive.\n", action_argv[0]);
390                 else
391                         log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
392                 r = -ENODEV;
393                 break;
394         case CRYPT_ACTIVE:
395         case CRYPT_BUSY:
396                 if (path)
397                         log_std("%s is active%s.\n", action_argv[0],
398                                 ci == CRYPT_BUSY ? " and is in use" : "");
399                 else
400                         log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
401                                 ci == CRYPT_BUSY ? " and is in use" : "");
402
403                 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
404                 if (r < 0)
405                         goto out;
406
407                 log_std("  type:    %s\n", crypt_get_type(cd) ?: "n/a");
408
409                 r = crypt_get_active_device(cd, action_argv[0], &cad);
410                 if (r < 0)
411                         goto out;
412
413                 /* Print only INTEGRITY (and LUKS2 with integrity) info */
414                 r = crypt_get_integrity_info(cd, &ip);
415                 if (r < 0)
416                         goto out;
417
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);
425                         free(backing_file);
426                 }
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);
431                                 free(backing_file);
432                         }
433                 }
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");
447                 } else {
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);
455                 }
456                 if (cad.flags & (CRYPT_ACTIVATE_ALLOW_DISCARDS))
457                         log_std("  flags: %s\n",
458                                 (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "discards " : "");
459         }
460 out:
461         crypt_free(cd);
462         if (r == -ENOTSUP)
463                 r = 0;
464         return r;
465         return -EINVAL;
466 }
467
468 static int action_dump(int arg)
469 {
470         struct crypt_device *cd = NULL;
471         struct crypt_params_integrity params = {};
472         int r;
473
474         if ((r = crypt_init(&cd, action_argv[0])))
475                 return r;
476
477         r = crypt_load(cd, CRYPT_INTEGRITY, &params);
478         if (!r)
479                 crypt_dump(cd);
480
481         crypt_free(cd);
482         return r;
483 }
484
485 static struct action_type {
486         const char *type;
487         int (*handler)(int);
488         int required_action_argc;
489         const char *arg_desc;
490         const char *desc;
491 } action_types[] = {
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 }
498 };
499
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)))
505 {
506         struct action_type *action;
507
508         if (key->shortName == '?') {
509                 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
510                 poptPrintHelp(popt_context, stdout, 0);
511                 log_std(_("\n"
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));
515                 log_std(_("\n"
516                          "<name> is the device to create under %s\n"
517                          "<integrity_device> is the device containing data with integrity tags\n"),
518                         crypt_get_dir());
519
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);
524                 tools_cleanup();
525                 poptFreeContext(popt_context);
526                 exit(EXIT_SUCCESS);
527         } else if (key->shortName == 'V') {
528                 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
529                 tools_cleanup();
530                 poptFreeContext(popt_context);
531                 exit(EXIT_SUCCESS);
532         } else
533                 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
534 }
535
536 static int run_action(struct action_type *action)
537 {
538         int r;
539
540         log_dbg("Running command %s.", action->type);
541
542         r = action->handler(0);
543
544         show_status(r);
545         return translate_errno(r);
546 }
547
548 int main(int argc, const char **argv)
549 {
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 },
556                 POPT_TABLEEND
557         };
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 },
565
566                 { "data-device",        '\0', POPT_ARG_STRING, &opt_data_device,      0, N_("Path to data device (if separated)"), N_("path") },
567
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") },
577
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 },
581
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 },
585
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 },
589
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 },
595
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 },
598
599                 { "allow-discards",            '\0', POPT_ARG_NONE,  &opt_allow_discards, 0, N_("Allow discards (aka TRIM) requests for device"), NULL },
600                 POPT_TABLEEND
601         };
602         poptContext popt_context;
603         struct action_type *action;
604         const char *aname;
605         int r;
606
607         crypt_set_log_callback(NULL, tool_log, NULL);
608
609         setlocale(LC_ALL, "");
610         bindtextdomain(PACKAGE, LOCALEDIR);
611         textdomain(PACKAGE);
612
613         popt_context = poptGetContext("integrity", argc, argv, popt_options, 0);
614         poptSetOtherOptionHelp(popt_context,
615                                _("[OPTION...] <action> <action-specific>"));
616
617
618         while ((r = poptGetNextOpt(popt_context)) >= 0) {
619         }
620
621         if (r < -1)
622                 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
623                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
624
625         if (!(aname = poptGetArg(popt_context)))
626                 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
627                       poptGetInvocationName(popt_context));
628
629         action_argc = 0;
630         action_argv = poptGetArgs(popt_context);
631         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
632         if (!action_argv)
633                 action_argv = null_action_argv;
634
635         /* Count args, somewhat unnice, change? */
636         while (action_argv[action_argc] != NULL)
637                 action_argc++;
638
639         /* Handle aliases */
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;
646                 }
647                 aname = "open";
648         } else if (!strcmp(aname, "remove")) {
649                 aname = "close";
650         }
651
652         if (opt_integrity)
653                 integrity_alg = opt_integrity;
654
655         for (action = action_types; action->type; action++)
656                 if (strcmp(action->type, aname) == 0)
657                         break;
658
659         if (!action->type)
660                 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
661                       poptGetInvocationName(popt_context));
662
663         if (action_argc < action->required_action_argc) {
664                 char buf[128];
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));
668         }
669
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));
674
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));
679
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));
688
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));
695
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));
700
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));
705
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));
713
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));
721
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));
725
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));
729
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));
733
734         if (opt_debug) {
735                 opt_verbose = 1;
736                 crypt_set_debug_level(-1);
737                 dbg_version_and_cmd(argc, argv);
738         }
739
740         r = run_action(action);
741         tools_cleanup();
742         poptFreeContext(popt_context);
743         return r;
744 }