Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / src / integritysetup.c
1 /*
2  * integritysetup - setup integrity protected volumes for dm-integrity
3  *
4  * Copyright (C) 2017-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2017-2020 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 #define MAX_KEY_SIZE 4096
29
30 static const char *opt_journal_size_str = NULL;
31 static uint64_t opt_journal_size = 0;
32 static int opt_interleave_sectors = 0;
33 static int opt_journal_watermark = 0;
34 static int opt_bitmap_sectors_per_bit = 0;
35 static int opt_journal_commit_time = 0;
36 static int opt_bitmap_flush_time = 0;
37 static int opt_tag_size = 0;
38 static int opt_sector_size = 0;
39 static int opt_buffer_sectors = 0;
40
41 static int opt_no_wipe = 0;
42
43 static const char *opt_data_device = NULL;
44
45 static const char *opt_integrity = DEFAULT_ALG_NAME;
46 static const char *opt_integrity_key_file = NULL;
47 static int opt_integrity_key_size = 0;
48
49 static const char *opt_journal_integrity = NULL; /* none */
50 static const char *opt_journal_integrity_key_file = NULL;
51 static int opt_journal_integrity_key_size = 0;
52
53 static const char *opt_journal_crypt = NULL; /* none */
54 static const char *opt_journal_crypt_key_file = NULL;
55 static int opt_journal_crypt_key_size = 0;
56
57 static int opt_integrity_nojournal = 0;
58 static int opt_integrity_recovery = 0;
59 static int opt_integrity_bitmap = 0;
60 static int opt_integrity_legacy_padding = 0;
61
62 static int opt_integrity_recalculate = 0;
63 static int opt_allow_discards = 0;
64
65 static const char **action_argv;
66 static int action_argc;
67
68 // FIXME: move this to tools and handle EINTR
69 static int _read_mk(const char *file, char **key, int keysize)
70 {
71         int fd;
72
73         if (keysize <= 0 || keysize > MAX_KEY_SIZE) {
74                 log_err(_("Invalid key size."));
75                 return -EINVAL;
76         }
77
78         *key = crypt_safe_alloc(keysize);
79         if (!*key)
80                 return -ENOMEM;
81
82         fd = open(file, O_RDONLY);
83         if (fd == -1) {
84                 log_err(_("Cannot read keyfile %s."), file);
85                 goto fail;
86         }
87         if ((read(fd, *key, keysize) != keysize)) {
88                 log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
89                 close(fd);
90                 goto fail;
91         }
92         close(fd);
93         return 0;
94 fail:
95         crypt_safe_free(*key);
96         *key = NULL;
97         return -EINVAL;
98 }
99
100 static int _read_keys(char **integrity_key, struct crypt_params_integrity *params)
101 {
102         char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL;
103         int r;
104
105         if (integrity_key && opt_integrity_key_file) {
106                 r = _read_mk(opt_integrity_key_file, &int_key, opt_integrity_key_size);
107                 if (r < 0)
108                         return r;
109                 params->integrity_key_size = opt_integrity_key_size;
110         }
111
112         if (opt_journal_integrity_key_file) {
113                 r = _read_mk(opt_journal_integrity_key_file, &journal_integrity_key, opt_journal_integrity_key_size);
114                 if (r < 0) {
115                         crypt_safe_free(int_key);
116                         return r;
117                 }
118                 params->journal_integrity_key = journal_integrity_key;
119                 params->journal_integrity_key_size = opt_journal_integrity_key_size;
120         }
121
122         if (opt_journal_crypt_key_file) {
123                 r = _read_mk(opt_journal_crypt_key_file, &journal_crypt_key, opt_journal_crypt_key_size);
124                 if (r < 0) {
125                         crypt_safe_free(int_key);
126                         crypt_safe_free(journal_integrity_key);
127                         return r;
128                 }
129                 params->journal_crypt_key = journal_crypt_key;
130                 params->journal_crypt_key_size = opt_journal_crypt_key_size;
131         }
132
133         if (integrity_key)
134                 *integrity_key = int_key;
135
136         return 0;
137 }
138
139 static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key)
140 {
141         char tmp_name[64], tmp_path[128], tmp_uuid[40];
142         uuid_t tmp_uuid_bin;
143         int r;
144
145         if (!opt_batch_mode)
146                 log_std(_("Wiping device to initialize integrity checksum.\n"
147                         "You can interrupt this by pressing CTRL+c "
148                         "(rest of not wiped device will contain invalid checksum).\n"));
149
150         /* Activate the device a temporary one */
151         uuid_generate(tmp_uuid_bin);
152         uuid_unparse(tmp_uuid_bin, tmp_uuid);
153         if (snprintf(tmp_name, sizeof(tmp_name), "temporary-cryptsetup-%s", tmp_uuid) < 0)
154                 return -EINVAL;
155         if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_name) < 0)
156                 return -EINVAL;
157
158         r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key,
159                 opt_integrity_key_size, CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_NO_JOURNAL);
160         if (r < 0)
161                 return r;
162
163         /* Wipe the device */
164         set_int_handler(0);
165         r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK,
166                        0, &tools_wipe_progress, NULL);
167         if (crypt_deactivate(cd, tmp_name))
168                 log_err(_("Cannot deactivate temporary device %s."), tmp_path);
169         set_int_block(0);
170
171         return r;
172 }
173
174 static int action_format(int arg)
175 {
176         struct crypt_device *cd = NULL;
177         struct crypt_params_integrity params = {
178                 .journal_size = opt_journal_size,
179                 .interleave_sectors = opt_interleave_sectors,
180                 /* in bitmap mode we have to overload these values... */
181                 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
182                 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
183                 .buffer_sectors = opt_buffer_sectors,
184                 .tag_size = opt_tag_size,
185                 .sector_size = opt_sector_size ?: SECTOR_SIZE,
186         }, params2;
187         char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
188         char *integrity_key = NULL, *msg = NULL;
189         int r;
190         size_t signatures;
191
192         if (opt_integrity) {
193                 r = crypt_parse_hash_integrity_mode(opt_integrity, integrity);
194                 if (r < 0) {
195                         log_err(_("No known integrity specification pattern detected."));
196                         return r;
197                 }
198                 params.integrity = integrity;
199         }
200
201         if (opt_journal_integrity) {
202                 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
203                 if (r < 0) {
204                         log_err(_("No known integrity specification pattern detected."));
205                         return r;
206                 }
207                 params.journal_integrity = journal_integrity;
208         }
209
210         if (opt_journal_crypt) {
211                 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
212                 if (r < 0) {
213                         log_err(_("No known integrity specification pattern detected."));
214                         return r;
215                 }
216                 params.journal_crypt = journal_crypt;
217         }
218
219         r = _read_keys(&integrity_key, &params);
220         if (r)
221                 goto out;
222
223         r = crypt_init_data_device(&cd, action_argv[0], opt_data_device);
224         if (r < 0)
225                 goto out;
226
227         r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]);
228         if (r == -1) {
229                 r = -ENOMEM;
230                 goto out;
231         }
232
233         r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL;
234         free(msg);
235         if (r < 0)
236                 goto out;
237
238         r = tools_detect_signatures(action_argv[0], 0, &signatures);
239         if (r < 0)
240                 goto out;
241
242         /* Signature candidates found */
243         if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0))
244                 goto out;
245
246         if (opt_integrity_legacy_padding)
247                 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING);
248
249         r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, &params);
250         if (r < 0) /* FIXME: call wipe signatures again */
251                 goto out;
252
253         if (!opt_batch_mode && !crypt_get_integrity_info(cd, &params2))
254                 log_std(_("Formatted with tag size %u, internal integrity %s.\n"),
255                         params2.tag_size, params2.integrity);
256
257         if (!opt_no_wipe)
258                 r = _wipe_data_device(cd, integrity_key);
259 out:
260         crypt_safe_free(integrity_key);
261         crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
262         crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
263         crypt_free(cd);
264         return r;
265 }
266
267 static int action_open(int arg)
268 {
269         struct crypt_device *cd = NULL;
270         struct crypt_params_integrity params = {
271                 /* in bitmap mode we have to overload these values... */
272                 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
273                 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
274                 .buffer_sectors = opt_buffer_sectors,
275         };
276         uint32_t activate_flags = 0;
277         char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
278         char *integrity_key = NULL;
279         int r;
280
281         if (opt_integrity) {
282                 r = crypt_parse_hash_integrity_mode(opt_integrity, integrity);
283                 if (r < 0) {
284                         log_err(_("No known integrity specification pattern detected."));
285                         return r;
286                 }
287                 params.integrity = integrity;
288         }
289
290         if (opt_journal_integrity) {
291                 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
292                 if (r < 0) {
293                         log_err(_("No known integrity specification pattern detected."));
294                         return r;
295
296                 }
297                 params.journal_integrity = journal_integrity;
298         }
299
300         if (opt_journal_crypt) {
301                 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
302                 if (r < 0) {
303                         log_err(_("No known integrity specification pattern detected."));
304                         return r;
305                 }
306                 params.journal_crypt = journal_crypt;
307         }
308
309         if (opt_integrity_nojournal || opt_integrity_bitmap)
310                 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
311         if (opt_integrity_recovery)
312                 activate_flags |= CRYPT_ACTIVATE_RECOVERY;
313         if (opt_integrity_bitmap)
314                 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
315
316         if (opt_integrity_recalculate)
317                 activate_flags |= CRYPT_ACTIVATE_RECALCULATE;
318         if (opt_allow_discards)
319                 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
320
321         r = _read_keys(&integrity_key, &params);
322         if (r)
323                 goto out;
324
325         if ((r = crypt_init_data_device(&cd, action_argv[0], opt_data_device)))
326                 goto out;
327
328         r = crypt_load(cd, CRYPT_INTEGRITY, &params);
329         if (r)
330                 goto out;
331
332         r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key,
333                                          opt_integrity_key_size, activate_flags);
334 out:
335         crypt_safe_free(integrity_key);
336         crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
337         crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
338         crypt_free(cd);
339         return r;
340 }
341
342 static int action_close(int arg)
343 {
344         struct crypt_device *cd = NULL;
345         int r;
346
347         r = crypt_init_by_name(&cd, action_argv[0]);
348         if (r == 0)
349                 r = crypt_deactivate(cd, action_argv[0]);
350
351         crypt_free(cd);
352         return r;
353 }
354
355 static int action_status(int arg)
356 {
357         crypt_status_info ci;
358         struct crypt_active_device cad;
359         struct crypt_params_integrity ip = {};
360         struct crypt_device *cd = NULL;
361         char *backing_file;
362         const char *device, *metadata_device;
363         int path = 0, r = 0;
364
365         /* perhaps a path, not a dm device name */
366         if (strchr(action_argv[0], '/'))
367                 path = 1;
368
369         ci = crypt_status(NULL, action_argv[0]);
370         switch (ci) {
371         case CRYPT_INVALID:
372                 r = -EINVAL;
373                 break;
374         case CRYPT_INACTIVE:
375                 if (path)
376                         log_std("%s is inactive.\n", action_argv[0]);
377                 else
378                         log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
379                 r = -ENODEV;
380                 break;
381         case CRYPT_ACTIVE:
382         case CRYPT_BUSY:
383                 if (path)
384                         log_std("%s is active%s.\n", action_argv[0],
385                                 ci == CRYPT_BUSY ? " and is in use" : "");
386                 else
387                         log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
388                                 ci == CRYPT_BUSY ? " and is in use" : "");
389
390                 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
391                 if (r < 0)
392                         goto out;
393
394                 log_std("  type:    %s\n", crypt_get_type(cd) ?: "n/a");
395
396                 r = crypt_get_active_device(cd, action_argv[0], &cad);
397                 if (r < 0)
398                         goto out;
399
400                 /* Print only INTEGRITY (and LUKS2 with integrity) info */
401                 r = crypt_get_integrity_info(cd, &ip);
402                 if (r < 0)
403                         goto out;
404
405                 log_std("  tag size: %u\n", ip.tag_size);
406                 log_std("  integrity: %s\n", ip.integrity ?: "(none)");
407                 device = crypt_get_device_name(cd);
408                 metadata_device = crypt_get_metadata_device_name(cd);
409                 log_std("  device:  %s%s\n", device, metadata_device ? " (detached)" : "");
410                 if ((backing_file = crypt_loop_backing_file(device))) {
411                         log_std("  loop:    %s\n", backing_file);
412                         free(backing_file);
413                 }
414                 if (metadata_device) {
415                         log_std("  metadata device:  %s\n", metadata_device);
416                         if ((backing_file = crypt_loop_backing_file(metadata_device))) {
417                                 log_std("  loop:    %s\n", backing_file);
418                                 free(backing_file);
419                         }
420                 }
421                 log_std("  sector size:  %u bytes\n", crypt_get_sector_size(cd));
422                 log_std("  interleave sectors: %u\n", ip.interleave_sectors);
423                 log_std("  size:    %" PRIu64 " sectors\n", cad.size);
424                 log_std("  mode:    %s%s\n",
425                         cad.flags & CRYPT_ACTIVATE_READONLY ? "readonly" : "read/write",
426                         cad.flags & CRYPT_ACTIVATE_RECOVERY ? " recovery" : "");
427                 log_std("  failures: %" PRIu64 "\n",
428                         crypt_get_active_integrity_failures(cd, action_argv[0]));
429                 if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) {
430                         log_std("  bitmap 512-byte sectors per bit: %u\n", ip.journal_watermark);
431                         log_std("  bitmap flush interval: %u ms\n", ip.journal_commit_time);
432                 } if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL) {
433                         log_std("  journal: not active\n");
434                 } else {
435                         log_std("  journal size: %" PRIu64 " bytes\n", ip.journal_size);
436                         log_std("  journal watermark: %u%%\n", ip.journal_watermark);
437                         log_std("  journal commit time: %u ms\n", ip.journal_commit_time);
438                         if (ip.journal_integrity)
439                                 log_std("  journal integrity MAC: %s\n", ip.journal_integrity);
440                         if (ip.journal_crypt)
441                                 log_std("  journal encryption: %s\n", ip.journal_crypt);
442                 }
443                 if (cad.flags & (CRYPT_ACTIVATE_ALLOW_DISCARDS))
444                         log_std("  flags: %s\n",
445                                 (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "discards " : "");
446         }
447 out:
448         crypt_free(cd);
449         if (r == -ENOTSUP)
450                 r = 0;
451         return r;
452         return -EINVAL;
453 }
454
455 static int action_dump(int arg)
456 {
457         struct crypt_device *cd = NULL;
458         struct crypt_params_integrity params = {};
459         int r;
460
461         if ((r = crypt_init(&cd, action_argv[0])))
462                 return r;
463
464         r = crypt_load(cd, CRYPT_INTEGRITY, &params);
465         if (!r)
466                 crypt_dump(cd);
467
468         crypt_free(cd);
469         return r;
470 }
471
472 static struct action_type {
473         const char *type;
474         int (*handler)(int);
475         int required_action_argc;
476         const char *arg_desc;
477         const char *desc;
478 } action_types[] = {
479         { "format",     action_format, 1, N_("<integrity_device>"),N_("format device") },
480         { "open",       action_open,   2, N_("<integrity_device> <name>"),N_("open device as <name>") },
481         { "close",      action_close,  1, N_("<name>"),N_("close device (remove mapping)") },
482         { "status",     action_status, 1, N_("<name>"),N_("show active device status") },
483         { "dump",       action_dump,   1, N_("<integrity_device>"),N_("show on-disk information") },
484         { NULL, NULL, 0, NULL, NULL }
485 };
486
487 static void help(poptContext popt_context,
488                  enum poptCallbackReason reason __attribute__((unused)),
489                  struct poptOption *key,
490                  const char *arg __attribute__((unused)),
491                  void *data __attribute__((unused)))
492 {
493         struct action_type *action;
494
495         if (key->shortName == '?') {
496                 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
497                 poptPrintHelp(popt_context, stdout, 0);
498                 log_std(_("\n"
499                          "<action> is one of:\n"));
500                 for(action = action_types; action->type; action++)
501                         log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
502                 log_std(_("\n"
503                          "<name> is the device to create under %s\n"
504                          "<integrity_device> is the device containing data with integrity tags\n"),
505                         crypt_get_dir());
506
507                 log_std(_("\nDefault compiled-in dm-integrity parameters:\n"
508                           "\tChecksum algorithm: %s\n"), DEFAULT_ALG_NAME);
509                 poptFreeContext(popt_context);
510                 exit(EXIT_SUCCESS);
511         } else if (key->shortName == 'V') {
512                 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
513                 poptFreeContext(popt_context);
514                 exit(EXIT_SUCCESS);
515         } else
516                 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
517 }
518
519 static int run_action(struct action_type *action)
520 {
521         int r;
522
523         log_dbg("Running command %s.", action->type);
524
525         r = action->handler(0);
526
527         show_status(r);
528         return translate_errno(r);
529 }
530
531 int main(int argc, const char **argv)
532 {
533         static const char *null_action_argv[] = {NULL};
534         static struct poptOption popt_help_options[] = {
535                 { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
536                 { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
537                 { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
538                 { "version",'V', POPT_ARG_NONE,     NULL, 0, N_("Print package version"),  NULL },
539                 POPT_TABLEEND
540         };
541         static struct poptOption popt_options[] = {
542                 { NULL,                 '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
543                 { "verbose",             'v', POPT_ARG_NONE, &opt_verbose,            0, N_("Shows more detailed error messages"), NULL },
544                 { "debug",              '\0', POPT_ARG_NONE, &opt_debug,              0, N_("Show debug messages"), NULL },
545                 { "batch-mode",          'q', POPT_ARG_NONE, &opt_batch_mode,         0, N_("Do not ask for confirmation"), NULL },
546                 { "progress-frequency", '\0', POPT_ARG_INT,  &opt_progress_frequency, 0, N_("Progress line update (in seconds)"), N_("secs") },
547                 { "no-wipe",            '\0', POPT_ARG_NONE, &opt_no_wipe,            0, N_("Do not wipe device after format"), NULL },
548
549                 { "data-device",        '\0', POPT_ARG_STRING, &opt_data_device,      0, N_("Path to data device (if separated)"), N_("path") },
550
551                 { "journal-size",        'j', POPT_ARG_STRING,&opt_journal_size_str,  0, N_("Journal size"), N_("bytes") },
552                 { "interleave-sectors", '\0', POPT_ARG_INT,  &opt_interleave_sectors, 0, N_("Interleave sectors"), N_("SECTORS") },
553                 { "journal-watermark",  '\0', POPT_ARG_INT,  &opt_journal_watermark,  0, N_("Journal watermark"),N_("percent") },
554                 { "journal-commit-time",'\0', POPT_ARG_INT,  &opt_journal_commit_time,0, N_("Journal commit time"), N_("ms") },
555                 { "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 },
556                 { "bitmap-flush-time",  '\0', POPT_ARG_INT,  &opt_bitmap_flush_time,  0, N_("Bitmap mode flush time"), N_("ms") },
557                 { "tag-size",            't', POPT_ARG_INT,  &opt_tag_size,           0, N_("Tag size (per-sector)"), N_("bytes") },
558                 { "sector-size",         's', POPT_ARG_INT,  &opt_sector_size,        0, N_("Sector size"), N_("bytes") },
559                 { "buffer-sectors",     '\0', POPT_ARG_INT,  &opt_buffer_sectors,     0, N_("Buffers size"), N_("SECTORS") },
560
561                 { "integrity",                  'I', POPT_ARG_STRING, &opt_integrity,                 0, N_("Data integrity algorithm"), NULL },
562                 { "integrity-key-size",        '\0', POPT_ARG_INT,    &opt_integrity_key_size,        0, N_("The size of the data integrity key"), N_("BITS") },
563                 { "integrity-key-file",        '\0', POPT_ARG_STRING, &opt_integrity_key_file,        0, N_("Read the integrity key from a file"), NULL },
564
565                 { "journal-integrity",         '\0', POPT_ARG_STRING, &opt_journal_integrity,         0, N_("Journal integrity algorithm"), NULL },
566                 { "journal-integrity-key-size",'\0', POPT_ARG_INT,    &opt_journal_integrity_key_size,0, N_("The size of the journal integrity key"), N_("BITS") },
567                 { "journal-integrity-key-file",'\0', POPT_ARG_STRING, &opt_journal_integrity_key_file,0, N_("Read the journal integrity key from a file"), NULL },
568
569                 { "journal-crypt",             '\0', POPT_ARG_STRING, &opt_journal_crypt,             0, N_("Journal encryption algorithm"), NULL },
570                 { "journal-crypt-key-size",    '\0', POPT_ARG_INT,    &opt_journal_crypt_key_size,    0, N_("The size of the journal encryption key"), N_("BITS") },
571                 { "journal-crypt-key-file",    '\0', POPT_ARG_STRING, &opt_journal_crypt_key_file,    0, N_("Read the journal encryption key from a file"), NULL },
572
573                 { "integrity-no-journal",       'D', POPT_ARG_NONE,  &opt_integrity_nojournal, 0, N_("Disable journal for integrity device"), NULL },
574                 { "integrity-recovery-mode",    'R', POPT_ARG_NONE,  &opt_integrity_recovery,  0, N_("Recovery mode (no journal, no tag checking)"), NULL },
575                 { "integrity-bitmap-mode",      'B', POPT_ARG_NONE,  &opt_integrity_bitmap, 0, N_("Use bitmap to track changes and disable journal for integrity device"), NULL },
576                 { "integrity-recalculate",     '\0', POPT_ARG_NONE,  &opt_integrity_recalculate,  0, N_("Recalculate initial tags automatically."), NULL },
577                 { "integrity-legacy-padding",  '\0', POPT_ARG_NONE,  &opt_integrity_legacy_padding, 0, N_("Use inefficient legacy padding (old kernels)"), NULL },
578
579                 { "allow-discards",            '\0', POPT_ARG_NONE,  &opt_allow_discards, 0, N_("Allow discards (aka TRIM) requests for device"), NULL },
580                 POPT_TABLEEND
581         };
582         poptContext popt_context;
583         struct action_type *action;
584         const char *aname;
585         int r;
586
587         crypt_set_log_callback(NULL, tool_log, NULL);
588
589         setlocale(LC_ALL, "");
590         bindtextdomain(PACKAGE, LOCALEDIR);
591         textdomain(PACKAGE);
592
593         popt_context = poptGetContext("integrity", argc, argv, popt_options, 0);
594         poptSetOtherOptionHelp(popt_context,
595                                _("[OPTION...] <action> <action-specific>"));
596
597
598         while ((r = poptGetNextOpt(popt_context)) >= 0) {
599         }
600
601         if (r < -1)
602                 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
603                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
604
605         if (!(aname = poptGetArg(popt_context)))
606                 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
607                       poptGetInvocationName(popt_context));
608
609         action_argc = 0;
610         action_argv = poptGetArgs(popt_context);
611         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
612         if (!action_argv)
613                 action_argv = null_action_argv;
614
615         /* Count args, somewhat unnice, change? */
616         while (action_argv[action_argc] != NULL)
617                 action_argc++;
618
619         /* Handle aliases */
620         if (!strcmp(aname, "create") && action_argc > 1) {
621                 /* create command had historically switched arguments */
622                 if (action_argv[0] && action_argv[1]) {
623                         const char *tmp = action_argv[0];
624                         action_argv[0] = action_argv[1];
625                         action_argv[1] = tmp;
626                 }
627                 aname = "open";
628         } else if (!strcmp(aname, "remove")) {
629                 aname = "close";
630         }
631
632         for (action = action_types; action->type; action++)
633                 if (strcmp(action->type, aname) == 0)
634                         break;
635
636         if (!action->type)
637                 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
638                       poptGetInvocationName(popt_context));
639
640         if (action_argc < action->required_action_argc) {
641                 char buf[128];
642                 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
643                 usage(popt_context, EXIT_FAILURE, buf,
644                       poptGetInvocationName(popt_context));
645         }
646
647         if (opt_integrity_recalculate && strcmp(aname, "open"))
648                 usage(popt_context, EXIT_FAILURE,
649                       _("Option --integrity-recalculate can be used only for open action."),
650                       poptGetInvocationName(popt_context));
651
652         if (opt_allow_discards && strcmp(aname, "open"))
653                 usage(popt_context, EXIT_FAILURE,
654                       _("Option --allow-discards is allowed only for open operation."),
655                       poptGetInvocationName(popt_context));
656
657         if (opt_interleave_sectors < 0 || opt_journal_watermark < 0 ||
658             opt_journal_commit_time < 0 || opt_tag_size < 0 ||
659             opt_sector_size < 0 || opt_buffer_sectors < 0 ||
660             opt_integrity_key_size < 0 || opt_journal_integrity_key_size < 0 ||
661             opt_journal_crypt_key_size < 0 || opt_bitmap_flush_time < 0 || opt_bitmap_sectors_per_bit < 0)
662                 usage(popt_context, EXIT_FAILURE,
663                       _("Negative number for option not permitted."),
664                       poptGetInvocationName(popt_context));
665
666         if (strcmp(aname, "format") && (opt_journal_size_str || opt_interleave_sectors ||
667                 opt_sector_size || opt_tag_size || opt_no_wipe ))
668                 usage(popt_context, EXIT_FAILURE,
669                       _("Options --journal-size, --interleave-sectors, --sector-size, --tag-size"
670                         " and --no-wipe can be used only for format action."),
671                       poptGetInvocationName(popt_context));
672
673         if (opt_journal_size_str &&
674             tools_string_to_size(NULL, opt_journal_size_str, &opt_journal_size))
675                 usage(popt_context, EXIT_FAILURE, _("Invalid journal size specification."),
676                       poptGetInvocationName(popt_context));
677
678         if ((opt_integrity_key_file && !opt_integrity_key_size) ||
679            (!opt_integrity_key_file && opt_integrity_key_size))
680                 usage(popt_context, EXIT_FAILURE, _("Both key file and key size options must be specified."),
681                       poptGetInvocationName(popt_context));
682         if (!opt_integrity && opt_integrity_key_file)
683                 usage(popt_context, EXIT_FAILURE, _("Integrity algorithm must be specified if integrity key is used."),
684                       poptGetInvocationName(popt_context));
685
686         if ((opt_journal_integrity_key_file && !opt_journal_integrity_key_size) ||
687            (!opt_journal_integrity_key_file && opt_journal_integrity_key_size))
688                 usage(popt_context, EXIT_FAILURE, _("Both journal integrity key file and key size options must be specified."),
689                       poptGetInvocationName(popt_context));
690         if (!opt_journal_integrity && opt_journal_integrity_key_file)
691                 usage(popt_context, EXIT_FAILURE, _("Journal integrity algorithm must be specified if journal integrity key is used."),
692                       poptGetInvocationName(popt_context));
693
694         if ((opt_journal_crypt_key_file && !opt_journal_crypt_key_size) ||
695            (!opt_journal_crypt_key_file && opt_journal_crypt_key_size))
696                 usage(popt_context, EXIT_FAILURE, _("Both journal encryption key file and key size options must be specified."),
697                       poptGetInvocationName(popt_context));
698         if (!opt_journal_crypt && opt_journal_crypt_key_file)
699                 usage(popt_context, EXIT_FAILURE, _("Journal encryption algorithm must be specified if journal encryption key is used."),
700                       poptGetInvocationName(popt_context));
701
702         if (opt_integrity_recovery && opt_integrity_bitmap)
703                 usage(popt_context, EXIT_FAILURE, _("Recovery and bitmap mode options are mutually exclusive."),
704                       poptGetInvocationName(popt_context));
705
706         if (opt_integrity_bitmap && (opt_journal_integrity_key_file || opt_journal_crypt || opt_journal_watermark || opt_journal_commit_time))
707                 usage(popt_context, EXIT_FAILURE, _("Journal options cannot be used in bitmap mode."),
708                       poptGetInvocationName(popt_context));
709
710         if (!opt_integrity_bitmap && (opt_bitmap_flush_time || opt_bitmap_sectors_per_bit))
711                 usage(popt_context, EXIT_FAILURE, _("Bitmap options can be used only in bitmap mode."),
712                       poptGetInvocationName(popt_context));
713
714         if (opt_debug) {
715                 opt_verbose = 1;
716                 crypt_set_debug_level(-1);
717                 dbg_version_and_cmd(argc, argv);
718         }
719
720         r = run_action(action);
721         poptFreeContext(popt_context);
722         return r;
723 }