Fixed the build error for riscv64 arch using gcc 13
[platform/upstream/cryptsetup.git] / src / veritysetup.c
1 /*
2  * veritysetup - setup cryptographic volumes for dm-verity
3  *
4  * Copyright (C) 2012-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2012-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
24 #define PACKAGE_VERITY "veritysetup"
25
26 static char *opt_fec_device = NULL;
27 static char *opt_hash_algorithm = NULL;
28 static char *opt_salt = NULL;
29 static char *opt_uuid = NULL;
30 static char *opt_root_hash_signature = NULL;
31
32 static int opt_use_superblock = 1;
33 static int opt_fec_roots = DEFAULT_VERITY_FEC_ROOTS;
34 static int opt_hash_type = 1;
35 static int opt_data_block_size = DEFAULT_VERITY_DATA_BLOCK;
36 static int opt_hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
37 static uint64_t data_blocks = 0;
38 static uint64_t hash_offset = 0;
39 static uint64_t fec_offset = 0;
40 static int opt_restart_on_corruption = 0;
41 static int opt_panic_on_corruption = 0;
42 static int opt_ignore_corruption = 0;
43 static int opt_ignore_zero_blocks = 0;
44 static int opt_check_at_most_once = 0;
45
46 static const char **action_argv;
47 static int action_argc;
48
49 void tools_cleanup(void)
50 {
51         FREE_AND_NULL(opt_fec_device);
52         FREE_AND_NULL(opt_hash_algorithm);
53         FREE_AND_NULL(opt_salt);
54         FREE_AND_NULL(opt_uuid);
55         FREE_AND_NULL(opt_root_hash_signature);
56 }
57
58 static int _prepare_format(struct crypt_params_verity *params,
59                            const char *data_device,
60                            uint32_t flags)
61 {
62         char *salt = NULL;
63         int len;
64
65         params->hash_name = opt_hash_algorithm ?: DEFAULT_VERITY_HASH;
66         params->data_device = data_device;
67         params->fec_device = opt_fec_device;
68         params->fec_roots = opt_fec_roots;
69
70         if (opt_salt && !strcmp(opt_salt, "-")) {
71                 params->salt_size = 0;
72                 params->salt = NULL;
73         } else if (opt_salt) {
74                 len = crypt_hex_to_bytes(opt_salt, &salt, 0);
75                 if (len < 0) {
76                         log_err(_("Invalid salt string specified."));
77                         return -EINVAL;
78                 }
79                 params->salt_size = len;
80                 params->salt = salt;
81         } else {
82                 params->salt_size = DEFAULT_VERITY_SALT_SIZE;
83                 params->salt = NULL;
84         }
85
86         params->data_block_size = opt_data_block_size;
87         params->hash_block_size = opt_hash_block_size;
88         params->data_size = data_blocks;
89         params->hash_area_offset = hash_offset;
90         params->fec_area_offset = fec_offset;
91         params->hash_type = opt_hash_type;
92         params->flags = flags;
93
94         return 0;
95 }
96
97 static int action_format(int arg)
98 {
99         struct crypt_device *cd = NULL;
100         struct crypt_params_verity params = {};
101         uint32_t flags = CRYPT_VERITY_CREATE_HASH;
102         int r;
103
104         /* Try to create hash image if doesn't exist */
105         r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
106         if (r < 0 && errno != EEXIST) {
107                 log_err(_("Cannot create hash image %s for writing."), action_argv[1]);
108                 return -EINVAL;
109         } else if (r >= 0) {
110                 log_dbg("Created hash image %s.", action_argv[1]);
111                 close(r);
112         }
113         /* Try to create FEC image if doesn't exist */
114         if (opt_fec_device) {
115                 r = open(opt_fec_device, O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
116                 if (r < 0 && errno != EEXIST) {
117                         log_err(_("Cannot create FEC image %s for writing."), opt_fec_device);
118                         return -EINVAL;
119                 } else if (r >= 0) {
120                         log_dbg("Created FEC image %s.", opt_fec_device);
121                         close(r);
122                 }
123         }
124
125         if ((r = crypt_init(&cd, action_argv[1])))
126                 goto out;
127
128         if (!opt_use_superblock)
129                 flags |= CRYPT_VERITY_NO_HEADER;
130
131         r = _prepare_format(&params, action_argv[0], flags);
132         if (r < 0)
133                 goto out;
134
135         r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, opt_uuid, NULL, 0, &params);
136         if (!r)
137                 crypt_dump(cd);
138 out:
139         crypt_free(cd);
140         free(CONST_CAST(char*)params.salt);
141         return r;
142 }
143
144 static int _activate(const char *dm_device,
145                       const char *data_device,
146                       const char *hash_device,
147                       const char *root_hash,
148                       uint32_t flags)
149 {
150         struct crypt_device *cd = NULL;
151         struct crypt_params_verity params = {};
152         uint32_t activate_flags = CRYPT_ACTIVATE_READONLY;
153         char *root_hash_bytes = NULL;
154         ssize_t hash_size;
155         struct stat st;
156         char *signature = NULL;
157         int signature_size = 0, r;
158
159         if ((r = crypt_init_data_device(&cd, hash_device, data_device)))
160                 goto out;
161
162         if (opt_ignore_corruption)
163                 activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
164         if (opt_restart_on_corruption)
165                 activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
166         if (opt_panic_on_corruption)
167                 activate_flags |= CRYPT_ACTIVATE_PANIC_ON_CORRUPTION;
168         if (opt_ignore_zero_blocks)
169                 activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
170         if (opt_check_at_most_once)
171                 activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
172
173         if (opt_use_superblock) {
174                 params.flags = flags;
175                 params.hash_area_offset = hash_offset;
176                 params.fec_area_offset = fec_offset;
177                 params.fec_device = opt_fec_device;
178                 params.fec_roots = opt_fec_roots;
179                 r = crypt_load(cd, CRYPT_VERITY, &params);
180         } else {
181                 r = _prepare_format(&params, data_device, flags | CRYPT_VERITY_NO_HEADER);
182                 if (r < 0)
183                         goto out;
184                 r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, &params);
185         }
186         if (r < 0)
187                 goto out;
188
189         hash_size = crypt_get_volume_key_size(cd);
190         if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) {
191                 log_err(_("Invalid root hash string specified."));
192                 r = -EINVAL;
193                 goto out;
194         }
195
196         if (opt_root_hash_signature) {
197                 // FIXME: check max file size
198                 if (stat(opt_root_hash_signature, &st) || !S_ISREG(st.st_mode) || !st.st_size) {
199                         log_err(_("Invalid signature file %s."), opt_root_hash_signature);
200                         r = -EINVAL;
201                         goto out;
202                 }
203                 signature_size = st.st_size;
204                 r = tools_read_mk(opt_root_hash_signature, &signature, signature_size);
205                 if (r < 0) {
206                         log_err(_("Cannot read signature file %s."), opt_root_hash_signature);
207                         goto out;
208                 }
209         }
210         r = crypt_activate_by_signed_key(cd, dm_device,
211                                          root_hash_bytes,
212                                          hash_size,
213                                          signature, signature_size,
214                                          activate_flags);
215 out:
216         crypt_safe_free(signature);
217         crypt_free(cd);
218         free(root_hash_bytes);
219         free(CONST_CAST(char*)params.salt);
220         return r;
221 }
222
223 static int action_open(int arg)
224 {
225         return _activate(action_argv[1],
226                          action_argv[0],
227                          action_argv[2],
228                          action_argv[3],
229                          opt_root_hash_signature ? CRYPT_VERITY_ROOT_HASH_SIGNATURE : 0);
230 }
231
232 static int action_verify(int arg)
233 {
234         return _activate(NULL,
235                          action_argv[0],
236                          action_argv[1],
237                          action_argv[2],
238                          CRYPT_VERITY_CHECK_HASH);
239 }
240
241 static int action_close(int arg)
242 {
243         struct crypt_device *cd = NULL;
244         int r;
245
246         r = crypt_init_by_name(&cd, action_argv[0]);
247         if (r == 0)
248                 r = crypt_deactivate(cd, action_argv[0]);
249
250         crypt_free(cd);
251         return r;
252 }
253
254 static int action_status(int arg)
255 {
256         crypt_status_info ci;
257         struct crypt_active_device cad;
258         struct crypt_params_verity vp = {};
259         struct crypt_device *cd = NULL;
260         struct stat st;
261         char *backing_file, *root_hash;
262         size_t root_hash_size;
263         unsigned i, path = 0;
264         int r = 0;
265
266         /* perhaps a path, not a dm device name */
267         if (strchr(action_argv[0], '/') && !stat(action_argv[0], &st))
268                 path = 1;
269
270         ci = crypt_status(NULL, action_argv[0]);
271         switch (ci) {
272         case CRYPT_INVALID:
273                 r = -EINVAL;
274                 break;
275         case CRYPT_INACTIVE:
276                 if (path)
277                         log_std("%s is inactive.\n", action_argv[0]);
278                 else
279                         log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
280                 r = -ENODEV;
281                 break;
282         case CRYPT_ACTIVE:
283         case CRYPT_BUSY:
284                 if (path)
285                         log_std("%s is active%s.\n", action_argv[0],
286                                 ci == CRYPT_BUSY ? " and is in use" : "");
287                 else
288                         log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
289                                 ci == CRYPT_BUSY ? " and is in use" : "");
290
291                 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
292                 if (r < 0)
293                         goto out;
294
295                 log_std("  type:        %s\n", crypt_get_type(cd) ?: "n/a");
296
297                 r = crypt_get_active_device(cd, action_argv[0], &cad);
298                 if (r < 0)
299                         goto out;
300
301                 /* Print only VERITY type devices */
302                 r = crypt_get_verity_info(cd, &vp);
303                 if (r < 0)
304                         goto out;
305
306                 log_std("  status:      %s%s\n",
307                         cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified",
308                         vp.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE ? " (with signature)" : "");
309
310                 log_std("  hash type:   %u\n", vp.hash_type);
311                 log_std("  data block:  %u\n", vp.data_block_size);
312                 log_std("  hash block:  %u\n", vp.hash_block_size);
313                 log_std("  hash name:   %s\n", vp.hash_name);
314                 log_std("  salt:        ");
315                 if (vp.salt_size)
316                         for(i = 0; i < vp.salt_size; i++)
317                                 log_std("%02hhx", (const char)vp.salt[i]);
318                 else
319                         log_std("-");
320                 log_std("\n");
321
322                 log_std("  data device: %s\n", vp.data_device);
323                 if ((backing_file = crypt_loop_backing_file(vp.data_device))) {
324                         log_std("  data loop:   %s\n", backing_file);
325                         free(backing_file);
326                 }
327                 log_std("  size:        %" PRIu64 " sectors\n", cad.size);
328                 log_std("  mode:        %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
329                                            "readonly" : "read/write");
330
331                 log_std("  hash device: %s\n", vp.hash_device);
332                 if ((backing_file = crypt_loop_backing_file(vp.hash_device))) {
333                         log_std("  hash loop:   %s\n", backing_file);
334                         free(backing_file);
335                 }
336                 log_std("  hash offset: %" PRIu64 " sectors\n",
337                         vp.hash_area_offset * vp.hash_block_size / 512);
338
339                 if (vp.fec_device) {
340                         log_std("  FEC device:  %s\n", vp.fec_device);
341                         if ((backing_file = crypt_loop_backing_file(opt_fec_device))) {
342                                 log_std("  FEC loop:    %s\n", backing_file);
343                                 free(backing_file);
344                         }
345                         log_std("  FEC offset:  %" PRIu64 " sectors\n",
346                                 vp.fec_area_offset * vp.hash_block_size / 512);
347                         log_std("  FEC roots:   %u\n", vp.fec_roots);
348                 }
349
350                 root_hash_size = crypt_get_volume_key_size(cd);
351                 if (root_hash_size > 0 && (root_hash = malloc(root_hash_size))) {
352                         r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0);
353                         if (!r) {
354                                 log_std("  root hash:   ");
355                                 for (i = 0; i < root_hash_size; i++)
356                                         log_std("%02hhx", (const char)root_hash[i]);
357                                 log_std("\n");
358                         }
359                         free(root_hash);
360                 }
361
362                 if (cad.flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
363                                  CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
364                                  CRYPT_ACTIVATE_PANIC_ON_CORRUPTION|
365                                  CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
366                                  CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE))
367                         log_std("  flags:       %s%s%s%s%s\n",
368                                 (cad.flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? "ignore_corruption " : "",
369                                 (cad.flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? "restart_on_corruption " : "",
370                                 (cad.flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION) ? "panic_on_corruption " : "",
371                                 (cad.flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? "ignore_zero_blocks " : "",
372                                 (cad.flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? "check_at_most_once" : "");
373         }
374 out:
375         crypt_free(cd);
376         if (r == -ENOTSUP)
377                 r = 0;
378         return r;
379 }
380
381 static int action_dump(int arg)
382 {
383         struct crypt_device *cd = NULL;
384         struct crypt_params_verity params = {};
385         int r;
386
387         if ((r = crypt_init(&cd, action_argv[0])))
388                 return r;
389
390         params.hash_area_offset = hash_offset;
391         params.fec_area_offset = fec_offset;
392         r = crypt_load(cd, CRYPT_VERITY, &params);
393         if (!r)
394                 crypt_dump(cd);
395         crypt_free(cd);
396         return r;
397 }
398
399 static struct action_type {
400         const char *type;
401         int (*handler)(int);
402         int required_action_argc;
403         const char *arg_desc;
404         const char *desc;
405 } action_types[] = {
406         { "format",     action_format, 2, N_("<data_device> <hash_device>"),N_("format device") },
407         { "verify",     action_verify, 3, N_("<data_device> <hash_device> <root_hash>"),N_("verify device") },
408         { "open",       action_open,   4, N_("<data_device> <name> <hash_device> <root_hash>"),N_("open device as <name>") },
409         { "close",      action_close,  1, N_("<name>"),N_("close device (remove mapping)") },
410         { "status",     action_status, 1, N_("<name>"),N_("show active device status") },
411         { "dump",       action_dump,   1, N_("<hash_device>"),N_("show on-disk information") },
412         { NULL, NULL, 0, NULL, NULL }
413 };
414
415 static void help(poptContext popt_context,
416                  enum poptCallbackReason reason __attribute__((unused)),
417                  struct poptOption *key,
418                  const char *arg __attribute__((unused)),
419                  void *data __attribute__((unused)))
420 {
421         struct action_type *action;
422
423         if (key->shortName == '?') {
424                 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
425                 poptPrintHelp(popt_context, stdout, 0);
426                 log_std(_("\n"
427                          "<action> is one of:\n"));
428                 for(action = action_types; action->type; action++)
429                         log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
430                 log_std(_("\n"
431                          "<name> is the device to create under %s\n"
432                          "<data_device> is the data device\n"
433                          "<hash_device> is the device containing verification data\n"
434                          "<root_hash> hash of the root node on <hash_device>\n"),
435                         crypt_get_dir());
436
437                 log_std(_("\nDefault compiled-in dm-verity parameters:\n"
438                          "\tHash: %s, Data block (bytes): %u, "
439                          "Hash block (bytes): %u, Salt size: %u, Hash format: %u\n"),
440                         DEFAULT_VERITY_HASH, DEFAULT_VERITY_DATA_BLOCK,
441                         DEFAULT_VERITY_HASH_BLOCK, DEFAULT_VERITY_SALT_SIZE,
442                         1);
443                 tools_cleanup();
444                 poptFreeContext(popt_context);
445                 exit(EXIT_SUCCESS);
446         } else if (key->shortName == 'V') {
447                 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
448                 tools_cleanup();
449                 poptFreeContext(popt_context);
450                 exit(EXIT_SUCCESS);
451         } else
452                 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
453 }
454
455 static int run_action(struct action_type *action)
456 {
457         int r;
458
459         log_dbg("Running command %s.", action->type);
460
461         r = action->handler(0);
462
463         show_status(r);
464         return translate_errno(r);
465 }
466
467 int main(int argc, const char **argv)
468 {
469         static const char *null_action_argv[] = {NULL};
470         static struct poptOption popt_help_options[] = {
471                 { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
472                 { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
473                 { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
474                 { "version",'V', POPT_ARG_NONE,     NULL, 0, N_("Print package version"),  NULL },
475                 POPT_TABLEEND
476         };
477         static struct poptOption popt_options[] = {
478                 { NULL,              '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
479                 { "verbose",         'v',  POPT_ARG_NONE, &opt_verbose,      0, N_("Shows more detailed error messages"), NULL },
480                 { "debug",           '\0', POPT_ARG_NONE, &opt_debug,        0, N_("Show debug messages"), NULL },
481                 { "no-superblock",   0,    POPT_ARG_VAL,  &opt_use_superblock,   0, N_("Do not use verity superblock"), NULL },
482                 { "format",          0,    POPT_ARG_INT,  &opt_hash_type,        0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
483                 { "data-block-size", 0,    POPT_ARG_INT,  &opt_data_block_size,  0, N_("Block size on the data device"), N_("bytes") },
484                 { "hash-block-size", 0,    POPT_ARG_INT,  &opt_hash_block_size,  0, N_("Block size on the hash device"), N_("bytes") },
485                 { "fec-roots",       0,    POPT_ARG_INT,  &opt_fec_roots,        0, N_("FEC parity bytes"), N_("bytes") },
486                 { "data-blocks",     0,    POPT_ARG_STRING, NULL,       1, N_("The number of blocks in the data file"), N_("blocks") },
487                 { "fec-device",      0,    POPT_ARG_STRING, &opt_fec_device,     0, N_("Path to device with error correction data"), N_("path") },
488                 { "hash-offset",     0,    POPT_ARG_STRING, NULL,       2, N_("Starting offset on the hash device"), N_("bytes") },
489                 { "fec-offset",      0,    POPT_ARG_STRING, NULL,       3, N_("Starting offset on the FEC device"), N_("bytes") },
490                 { "hash",            'h',  POPT_ARG_STRING, &opt_hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
491                 { "salt",            's',  POPT_ARG_STRING, &opt_salt,    0, N_("Salt"), N_("hex string") },
492                 { "uuid",            '\0', POPT_ARG_STRING, &opt_uuid,       0, N_("UUID for device to use"), NULL },
493                 { "root-hash-signature",'\0', POPT_ARG_STRING, &opt_root_hash_signature,  0, N_("Path to root hash signature file"), NULL },
494                 { "restart-on-corruption", 0,POPT_ARG_NONE,&opt_restart_on_corruption, 0, N_("Restart kernel if corruption is detected"), NULL },
495                 { "panic-on-corruption", 0,POPT_ARG_NONE, &opt_panic_on_corruption, 0, N_("Panic kernel if corruption is detected"), NULL },
496                 { "ignore-corruption", 0,  POPT_ARG_NONE, &opt_ignore_corruption,  0, N_("Ignore corruption, log it only"), NULL },
497                 { "ignore-zero-blocks", 0, POPT_ARG_NONE, &opt_ignore_zero_blocks, 0, N_("Do not verify zeroed blocks"), NULL },
498                 { "check-at-most-once", 0, POPT_ARG_NONE, &opt_check_at_most_once, 0, N_("Verify data block only the first time it is read"), NULL },
499                 POPT_TABLEEND
500         };
501
502         poptContext popt_context;
503         struct action_type *action;
504         const char *aname;
505         int r;
506
507         crypt_set_log_callback(NULL, tool_log, NULL);
508
509         setlocale(LC_ALL, "");
510         bindtextdomain(PACKAGE, LOCALEDIR);
511         textdomain(PACKAGE);
512
513         popt_context = poptGetContext("verity", argc, argv, popt_options, 0);
514         poptSetOtherOptionHelp(popt_context,
515                                _("[OPTION...] <action> <action-specific>"));
516
517         while((r = poptGetNextOpt(popt_context)) > 0) {
518                 unsigned long long ull_value;
519                 char *endp, *str = poptGetOptArg(popt_context);
520
521                 errno = 0;
522                 ull_value = strtoull(str, &endp, 10);
523                 if (*endp || !*str || !isdigit(*str) ||
524                     (errno == ERANGE && ull_value == ULLONG_MAX) ||
525                     (errno != 0 && ull_value == 0))
526                         r = POPT_ERROR_BADNUMBER;
527
528                 free(str);
529
530                 switch(r) {
531                         case 1:
532                                 data_blocks = ull_value;
533                                 break;
534                         case 2:
535                                 hash_offset = ull_value;
536                                 break;
537                         case 3:
538                                 fec_offset = ull_value;
539                                 break;
540                 }
541
542                 if (r < 0)
543                         break;
544         }
545
546         if (r < -1)
547                 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
548                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
549
550         if (!(aname = poptGetArg(popt_context)))
551                 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
552                       poptGetInvocationName(popt_context));
553
554         action_argc = 0;
555         action_argv = poptGetArgs(popt_context);
556         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
557         if(!action_argv)
558                 action_argv = null_action_argv;
559
560         /* Count args, somewhat unnice, change? */
561         while(action_argv[action_argc] != NULL)
562                 action_argc++;
563
564         /* Handle aliases */
565         if (!strcmp(aname, "create") && action_argc > 1) {
566                 /* create command had historically switched arguments */
567                 if (action_argv[0] && action_argv[1]) {
568                         const char *tmp = action_argv[0];
569                         action_argv[0] = action_argv[1];
570                         action_argv[1] = tmp;
571                 }
572                 aname = "open";
573         } else if (!strcmp(aname, "remove")) {
574                 aname = "close";
575         }
576
577         for (action = action_types; action->type; action++)
578                 if (strcmp(action->type, aname) == 0)
579                         break;
580
581         if (!action->type)
582                 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
583                       poptGetInvocationName(popt_context));
584
585         if (action_argc < action->required_action_argc) {
586                 char buf[128];
587                 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
588                 usage(popt_context, EXIT_FAILURE, buf,
589                       poptGetInvocationName(popt_context));
590         }
591
592         if (opt_data_block_size < 0 || opt_hash_block_size < 0 || opt_hash_type < 0) {
593                 usage(popt_context, EXIT_FAILURE,
594                       _("Negative number for option not permitted."),
595                       poptGetInvocationName(popt_context));
596         }
597
598         if ((opt_ignore_corruption || opt_restart_on_corruption || opt_ignore_zero_blocks) && strcmp(aname, "open"))
599                 usage(popt_context, EXIT_FAILURE,
600                 _("Option --ignore-corruption, --restart-on-corruption or --ignore-zero-blocks is allowed only for open operation."),
601                 poptGetInvocationName(popt_context));
602
603         if (opt_root_hash_signature && strcmp(aname, "open"))
604                 usage(popt_context, EXIT_FAILURE,
605                 _("Option --root-hash-signature can be used only for open operation."),
606                 poptGetInvocationName(popt_context));
607
608         if (opt_ignore_corruption && opt_restart_on_corruption)
609                 usage(popt_context, EXIT_FAILURE,
610                 _("Option --ignore-corruption and --restart-on-corruption cannot be used together."),
611                 poptGetInvocationName(popt_context));
612
613         if (opt_panic_on_corruption && opt_restart_on_corruption)
614                 usage(popt_context, EXIT_FAILURE,
615                 _("Option --panic-on-corruption and --restart-on-corruption cannot be used together."),
616                 poptGetInvocationName(popt_context));
617
618         if (opt_debug) {
619                 opt_verbose = 1;
620                 crypt_set_debug_level(-1);
621                 dbg_version_and_cmd(argc, argv);
622         }
623
624         r = run_action(action);
625         tools_cleanup();
626         poptFreeContext(popt_context);
627         return r;
628 }