dc337fc1f8644ea31409dc345dd65c31f999a5cb
[platform/upstream/cryptsetup.git] / src / cryptsetup.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <inttypes.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <assert.h>
10
11 #include <libcryptsetup.h>
12 #include <popt.h>
13
14 #include "../config.h"
15
16 #include "cryptsetup.h"
17
18 static int opt_verbose = 0;
19 static int opt_debug = 0;
20 static char *opt_cipher = NULL;
21 static char *opt_hash = NULL;
22 static int opt_verify_passphrase = 0;
23 static char *opt_key_file = NULL;
24 static char *opt_master_key_file = NULL;
25 static unsigned int opt_key_size = 0;
26 static int opt_key_slot = CRYPT_ANY_SLOT;
27 static uint64_t opt_size = 0;
28 static uint64_t opt_offset = 0;
29 static uint64_t opt_skip = 0;
30 static int opt_readonly = 0;
31 static int opt_iteration_time = 1000;
32 static int opt_batch_mode = 0;
33 static int opt_version_mode = 0;
34 static int opt_timeout = 0;
35 static int opt_tries = 3;
36 static int opt_align_payload = 0;
37 static int opt_non_exclusive = 0;
38
39 static const char **action_argv;
40 static int action_argc;
41
42 static int action_create(int arg);
43 static int action_remove(int arg);
44 static int action_resize(int arg);
45 static int action_status(int arg);
46 static int action_luksFormat(int arg);
47 static int action_luksOpen(int arg);
48 static int action_luksAddKey(int arg);
49 static int action_luksDelKey(int arg);
50 static int action_luksKillSlot(int arg);
51 static int action_luksRemoveKey(int arg);
52 static int action_isLuks(int arg);
53 static int action_luksUUID(int arg);
54 static int action_luksDump(int arg);
55 static int action_luksSuspend(int arg);
56 static int action_luksResume(int arg);
57
58 static struct action_type {
59         const char *type;
60         int (*handler)(int);
61         int arg;
62         int required_action_argc;
63         int required_dm_backend;
64         int required_memlock;
65         int show_status;
66         const char *arg_desc;
67         const char *desc;
68 } action_types[] = {
69         { "create",     action_create,          0, 2, 1, 1, 1, N_("<name> <device>"),N_("create device") },
70         { "remove",     action_remove,          0, 1, 1, 0, 1, N_("<name>"), N_("remove device") },
71         { "resize",     action_resize,          0, 1, 1, 1, 1, N_("<name>"), N_("resize active device") },
72         { "status",     action_status,          0, 1, 1, 0, 1, N_("<name>"), N_("show device status") },
73         { "luksFormat", action_luksFormat,      0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
74         { "luksOpen",   action_luksOpen,        0, 2, 1, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
75         { "luksAddKey", action_luksAddKey,      0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
76         { "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
77         { "luksKillSlot",  action_luksKillSlot, 0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
78         { "luksUUID",   action_luksUUID,        0, 1, 0, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
79         { "isLuks",     action_isLuks,          0, 1, 0, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
80         { "luksClose",  action_remove,          0, 1, 1, 0, 1, N_("<name>"), N_("remove LUKS mapping") },
81         { "luksDump",   action_luksDump,        0, 1, 0, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
82         { "luksSuspend",action_luksSuspend,     0, 1, 1, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
83         { "luksResume", action_luksResume,      0, 1, 1, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
84         { "luksDelKey", action_luksDelKey,      0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
85         { "reload",     action_create,          1, 2, 1, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
86         { NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL }
87 };
88
89 /* Interface Callbacks */
90 static int yesDialog(char *msg)
91 {
92         char *answer = NULL;
93         size_t size = 0;
94         int r = 1;
95
96         if(isatty(0) && !opt_batch_mode) {
97                 log_std("\nWARNING!\n========\n");
98                 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
99                 if(getline(&answer, &size, stdin) == -1) {
100                         perror("getline");
101                         free(answer);
102                         return 0;
103                 }
104                 if(strcmp(answer, "YES\n"))
105                         r = 0;
106                 free(answer);
107         }
108
109         return r;
110 }
111
112 static void cmdLineLog(int class, char *msg) {
113     switch(class) {
114
115     case CRYPT_LOG_NORMAL:
116             fputs(msg, stdout);
117             break;
118     case CRYPT_LOG_ERROR:
119             fputs(msg, stderr);
120             break;
121     default:
122             fprintf(stderr, "Internal error on logging class for msg: %s", msg);
123             break;
124     }
125 }
126
127 static struct interface_callbacks cmd_icb = {
128         .yesDialog = yesDialog,
129         .log = cmdLineLog,
130 };
131
132 /* End ICBs */
133
134 static void show_status(int errcode)
135 {
136         char error[256];
137
138         if(!errcode && opt_verbose) {
139                 log_std(_("Command successful.\n"));
140                 return;
141         }
142
143         crypt_get_error(error, sizeof(error));
144
145         if (!opt_verbose) {
146                 char *error_ = strerror_r(errcode, error, sizeof(error));
147                 if (error_ != error) {
148                         strncpy(error, error_, sizeof(error));
149                         error[sizeof error - 1] = '\0';
150                 }
151         }
152
153         log_err(_("Command failed"));
154         if (*error)
155                 log_err(": %s\n", error);
156         else
157                 log_err(".\n");
158         return;
159 }
160
161 static int action_create(int reload)
162 {
163         struct crypt_options options = {
164                 .name = action_argv[0],
165                 .device = action_argv[1],
166                 .cipher = opt_cipher?opt_cipher:DEFAULT_CIPHER,
167                 .hash = opt_hash ?: DEFAULT_HASH,
168                 .key_file = opt_key_file,
169                 .key_size = ((opt_key_size)?opt_key_size:DEFAULT_KEY_SIZE)/8,
170                 .key_slot = opt_key_slot,
171                 .flags = 0,
172                 .size = opt_size,
173                 .offset = opt_offset,
174                 .skip = opt_skip,
175                 .timeout = opt_timeout,
176                 .tries = opt_tries,
177                 .icb = &cmd_icb,
178         };
179         int r;
180
181         if(reload) 
182                 log_err(_("The reload action is deprecated. Please use \"dmsetup reload\" in case you really need this functionality.\nWARNING: do not use reload to touch LUKS devices. If that is the case, hit Ctrl-C now.\n"));
183
184         if (options.hash && strcmp(options.hash, "plain") == 0)
185                 options.hash = NULL;
186         if (opt_verify_passphrase)
187                 options.flags |= CRYPT_FLAG_VERIFY;
188         if (opt_readonly)
189                 options.flags |= CRYPT_FLAG_READONLY;
190
191         if (reload)
192                 r = crypt_update_device(&options);
193         else
194                 r = crypt_create_device(&options);
195
196         return r;
197 }
198
199 static int action_remove(int arg)
200 {
201         struct crypt_options options = {
202                 .name = action_argv[0],
203                 .icb = &cmd_icb,
204         };
205
206         return crypt_remove_device(&options);
207 }
208
209 static int action_resize(int arg)
210 {
211         struct crypt_options options = {
212                 .name = action_argv[0],
213                 .size = opt_size,
214                 .icb = &cmd_icb,
215         };
216
217         return crypt_resize_device(&options);
218 }
219
220 static int action_status(int arg)
221 {
222         struct crypt_options options = {
223                 .name = action_argv[0],
224                 .icb = &cmd_icb,
225         };
226         int r;
227
228         r = crypt_query_device(&options);
229         if (r < 0)
230                 return r;
231
232         if (r == 0) {
233                 /* inactive */
234                 log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
235                 r = 1;
236         } else {
237                 /* active */
238                 log_std("%s/%s is active:\n", crypt_get_dir(), options.name);
239                 log_std("  cipher:  %s\n", options.cipher);
240                 log_std("  keysize: %d bits\n", options.key_size * 8);
241                 log_std("  device:  %s\n", options.device);
242                 log_std("  offset:  %" PRIu64 " sectors\n", options.offset);
243                 log_std("  size:    %" PRIu64 " sectors\n", options.size);
244                 if (options.skip)
245                         log_std("  skipped: %" PRIu64 " sectors\n", options.skip);
246                 log_std("  mode:    %s\n", (options.flags & CRYPT_FLAG_READONLY)
247                                            ? "readonly" : "read/write");
248                 crypt_put_options(&options);
249                 r = 0;
250         }
251         return r;
252 }
253
254 static int _action_luksFormat_generateMK()
255 {
256         struct crypt_options options = {
257                 .key_size = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8,
258                 .key_slot = opt_key_slot,
259                 .device = action_argv[0],
260                 .cipher = opt_cipher ?: DEFAULT_LUKS_CIPHER,
261                 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
262                 .new_key_file = action_argc > 1 ? action_argv[1] : NULL,
263                 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE :  0),
264                 .iteration_time = opt_iteration_time,
265                 .timeout = opt_timeout,
266                 .align_payload = opt_align_payload,
267                 .icb = &cmd_icb,
268         };
269
270         return crypt_luksFormat(&options);
271 }
272
273 static int _read_mk(const char *file, char **key, int keysize)
274 {
275         int fd;
276
277         *key = malloc(keysize);
278         if (!*key)
279                 return -ENOMEM;
280
281         fd = open(file, O_RDONLY);
282         if (fd == -1) {
283                 log_err("Cannot read keyfile %s.\n", file);
284                 return -EINVAL;
285         }
286         if ((read(fd, *key, keysize) != keysize)) {
287                 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
288                 close(fd);
289                 memset(*key, 0, keysize);
290                 free(*key);
291                 return -EINVAL;
292         }
293         close(fd);
294         return 0;
295 }
296
297 static int _action_luksFormat_useMK()
298 {
299         int r = -EINVAL, keysize;
300         char *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
301         struct crypt_device *cd = NULL;
302         struct crypt_params_luks1 params = {
303                 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
304                 .data_alignment = opt_align_payload,
305         };
306
307         if (parse_into_name_and_mode(opt_cipher ?: DEFAULT_LUKS_CIPHER, cipher, cipher_mode)) {
308                 log_err("No known cipher specification pattern detected.\n");
309                 return -EINVAL;
310         }
311
312         keysize = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8;
313         if (_read_mk(opt_master_key_file, &key, keysize) < 0)
314                 return -EINVAL;
315
316         if ((r = crypt_init(&cd, action_argv[0])))
317                 goto out;
318
319         crypt_set_password_verify(cd, 1);
320         crypt_set_timeout(cd, opt_timeout);
321         if (opt_iteration_time)
322                 crypt_set_iterarion_time(cd, opt_iteration_time);
323
324         if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, &params)))
325                 goto out;
326
327         r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
328 out:
329
330         crypt_free(cd);
331         if (key) {
332                 memset(key, 0, keysize);
333                 free(key);
334         }
335         return r;
336 }
337
338 static int action_luksFormat(int arg)
339 {
340         int r = 0; char *msg = NULL;
341
342         /* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
343         if (opt_offset || opt_skip) {
344                 log_err("Options --offset and --skip are not supported for luksFormat.\n"); 
345                 return -EINVAL;
346         }
347
348         if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
349                 log_err(_("memory allocation error in action_luksFormat"));
350                 return -ENOMEM;
351         }
352         r = yesDialog(msg);
353         free(msg);
354
355         if (!r)
356                 return -EINVAL;
357
358         if (opt_master_key_file)
359                 return _action_luksFormat_useMK();
360         else
361                 return _action_luksFormat_generateMK();
362 }
363
364 static int action_luksOpen(int arg)
365 {
366         struct crypt_options options = {
367                 .name = action_argv[1],
368                 .device = action_argv[0],
369                 .key_file = opt_key_file,
370                 .key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
371                 .timeout = opt_timeout,
372                 .tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
373                 .icb = &cmd_icb,
374         };
375
376         if (opt_readonly)
377                 options.flags |= CRYPT_FLAG_READONLY;
378         if (opt_non_exclusive)
379                 log_err(_("Obsolete option --non-exclusive is ignored.\n"));
380         return crypt_luksOpen(&options);
381 }
382
383 static int action_luksDelKey(int arg)
384 {
385         log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n"); 
386         return action_luksKillSlot(arg);
387 }
388
389 static int action_luksKillSlot(int arg)
390 {
391         struct crypt_options options = {
392                 .device = action_argv[0],
393                 .key_slot = atoi(action_argv[1]),
394                 .key_file = opt_key_file,
395                 .timeout = opt_timeout,
396                 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
397                 .icb = &cmd_icb,
398         };
399
400         return crypt_luksKillSlot(&options);
401 }
402
403 static int action_luksRemoveKey(int arg)
404 {
405         struct crypt_options options = {
406                 .device = action_argv[0],
407                 .new_key_file = action_argc>1?action_argv[1]:NULL,
408                 .key_file = opt_key_file,
409                 .timeout = opt_timeout,
410                 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
411                 .icb = &cmd_icb,
412         };
413
414         return crypt_luksRemoveKey(&options);
415 }
416
417 static int _action_luksAddKey_useMK()
418 {
419         int r = -EINVAL, keysize;
420         char *key = NULL;
421         struct crypt_device *cd = NULL;
422
423         if ((r = crypt_init(&cd, action_argv[0])))
424                 goto out;
425
426         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
427                 goto out;
428
429         keysize = crypt_get_volume_key_size(cd);
430         crypt_set_password_verify(cd, 1);
431         crypt_set_timeout(cd, opt_timeout);
432         if (opt_iteration_time)
433                 crypt_set_iterarion_time(cd, opt_iteration_time);
434
435         if (_read_mk(opt_master_key_file, &key, keysize) < 0)
436                 goto out;
437
438         r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
439 out:
440         crypt_free(cd);
441         if (key) {
442                 memset(key, 0, keysize);
443                 free(key);
444         }
445         return r;
446 }
447
448 static int action_luksAddKey(int arg)
449 {
450         struct crypt_options options = {
451                 .device = action_argv[0],
452                 .new_key_file = action_argc>1?action_argv[1]:NULL,
453                 .key_file = opt_key_file,
454                 .key_slot = opt_key_slot,
455                 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
456                 .iteration_time = opt_iteration_time,
457                 .timeout = opt_timeout,
458                 .icb = &cmd_icb,
459         };
460
461         if (opt_master_key_file)
462                 return _action_luksAddKey_useMK();
463         else
464                 return crypt_luksAddKey(&options);
465 }
466
467 static int action_isLuks(int arg)
468 {
469         struct crypt_options options = {
470                 .device = action_argv[0],
471                 .icb = &cmd_icb,
472         };
473
474         return crypt_isLuks(&options);
475 }
476
477 static int action_luksUUID(int arg)
478 {
479         struct crypt_options options = {
480                 .device = action_argv[0],
481                 .icb = &cmd_icb,
482         };
483
484         return crypt_luksUUID(&options);
485 }
486
487 static int action_luksDump(int arg)
488 {
489         struct crypt_options options = {
490                 .device = action_argv[0],
491                 .icb = &cmd_icb,
492         };
493
494         return crypt_luksDump(&options);
495 }
496
497 static int action_luksSuspend(int arg)
498 {
499         struct crypt_device *cd = NULL;
500         int r;
501
502         r = crypt_init_by_name(&cd, action_argv[0]);
503         if (!r)
504                 r = crypt_suspend(cd, action_argv[0]);
505
506         crypt_free(cd);
507         return r;
508 }
509
510 static int action_luksResume(int arg)
511 {
512         struct crypt_device *cd = NULL;
513         int r;
514
515         if ((r = crypt_init_by_name(&cd, action_argv[0])))
516                 goto out;
517
518         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
519                 goto out;
520
521         if (opt_key_file)
522                 r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
523                                             opt_key_file, opt_key_size / 8);
524         else
525                 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
526                                                NULL, 0);
527 out:
528         crypt_free(cd);
529         return r;
530 }
531
532 static void usage(poptContext popt_context, int exitcode,
533                   const char *error, const char *more)
534 {
535         poptPrintUsage(popt_context, stderr, 0);
536         if (error)
537                 log_err("%s: %s\n", more, error);
538         exit(exitcode);
539 }
540
541 static void help(poptContext popt_context, enum poptCallbackReason reason,
542                  struct poptOption *key, const char * arg, void *data)
543 {
544         if (key->shortName == '?') {
545                 struct action_type *action;
546
547                 log_std("%s\n",PACKAGE_STRING);
548
549                 poptPrintHelp(popt_context, stdout, 0);
550
551                 log_std(_("\n"
552                          "<action> is one of:\n"));
553
554                 for(action = action_types; action->type; action++)
555                         log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
556                 
557                 log_std(_("\n"
558                          "<name> is the device to create under %s\n"
559                          "<device> is the encrypted device\n"
560                          "<key slot> is the LUKS key slot number to modify\n"
561                          "<key file> optional key file for the new key for luksAddKey action\n"),
562                         crypt_get_dir());
563                 exit(0);
564         } else
565                 usage(popt_context, 0, NULL, NULL);
566 }
567
568 void set_debug_level(int level);
569
570 static void _dbg_version_and_cmd(int argc, char **argv)
571 {
572         int i;
573
574         log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
575         for (i = 0; i < argc; i++) {
576                 if (i)
577                         log_std(" ");
578                 log_std(argv[i]);
579         }
580         log_std("\"\n");
581 }
582
583 static int run_action(struct action_type *action)
584 {
585         int r;
586
587         if (dm_init(NULL, action->required_dm_backend) < 1) {
588                 log_err("Cannot communicate with device-mapper. Is dm_mod kernel module loaded?\n");
589                 return -ENOSYS;
590         }
591
592         if (action->required_memlock)
593                 crypt_memory_lock(NULL, 1);
594
595         r = action->handler(action->arg);
596
597         if (action->required_memlock)
598                 crypt_memory_lock(NULL, 0);
599
600         if (action->required_dm_backend)
601                 dm_exit();
602
603         if (r < 0 && (opt_verbose || action->show_status))
604                 show_status(-r);
605
606         return r;
607 }
608
609 int main(int argc, char **argv)
610 {
611         static char *popt_tmp;
612         static struct poptOption popt_help_options[] = {
613                 { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
614                 { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
615                 { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
616                 POPT_TABLEEND
617         };
618         static struct poptOption popt_options[] = {
619                 { NULL,                '\0', POPT_ARG_INCLUDE_TABLE,                      popt_help_options,      0, N_("Help options:"),                                                   NULL },
620                 { "verbose",           'v',  POPT_ARG_NONE,                               &opt_verbose,           0, N_("Shows more detailed error messages"),                              NULL },
621                 { "debug",             '\0', POPT_ARG_NONE,                               &opt_debug,             0, N_("Show debug messsgages"),                                           NULL },
622                 { "cipher",            'c',  POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_cipher,            0, N_("The cipher used to encrypt the disk (see /proc/crypto)"),          NULL },
623                 { "hash",              'h',  POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_hash,              0, N_("The hash used to create the encryption key from the passphrase"),  NULL },
624                 { "verify-passphrase", 'y',  POPT_ARG_NONE,                               &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"),                  NULL },
625                 { "key-file",          'd',  POPT_ARG_STRING,                             &opt_key_file,          0, N_("Read the key from a file (can be /dev/random)"),                   NULL },
626                 { "master-key-file",  '\0',  POPT_ARG_STRING,                             &opt_master_key_file,   0, N_("Read the volume (master) key from file."),                         NULL },
627                 { "key-size",          's',  POPT_ARG_INT    | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size,          0, N_("The size of the encryption key"),                                  N_("BITS") },
628                 { "key-slot",          'S',  POPT_ARG_INT,                                &opt_key_slot,          0, N_("Slot number for new key (default is first free)"),      NULL },
629                 { "size",              'b',  POPT_ARG_STRING,                             &popt_tmp,              1, N_("The size of the device"),                                          N_("SECTORS") },
630                 { "offset",            'o',  POPT_ARG_STRING,                             &popt_tmp,              2, N_("The start offset in the backend device"),                          N_("SECTORS") },
631                 { "skip",              'p',  POPT_ARG_STRING,                             &popt_tmp,              3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
632                 { "readonly",          'r',  POPT_ARG_NONE,                               &opt_readonly,          0, N_("Create a readonly mapping"),                                       NULL },
633                 { "iter-time",         'i',  POPT_ARG_INT,                                &opt_iteration_time,    0, N_("PBKDF2 iteration time for LUKS (in ms)"),
634                   N_("msecs") },
635                 { "batch-mode",        'q',  POPT_ARG_NONE,                               &opt_batch_mode,        0, N_("Do not ask for confirmation"),                                     NULL },
636                 { "version",        '\0',  POPT_ARG_NONE,                                 &opt_version_mode,        0, N_("Print package version"),                                     NULL },
637                 { "timeout",           't',  POPT_ARG_INT,                                &opt_timeout,           0, N_("Timeout for interactive passphrase prompt (in seconds)"),          N_("secs") },
638                 { "tries",             'T',  POPT_ARG_INT,                                &opt_tries,             0, N_("How often the input of the passphrase canbe retried"),            NULL },
639                 { "align-payload",     '\0',  POPT_ARG_INT,                               &opt_align_payload,     0, N_("Align payload at <n> sector boundaries - for luksFormat"),         N_("SECTORS") },
640                 { "non-exclusive",     '\0',  POPT_ARG_NONE,                              &opt_non_exclusive,     0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."),        NULL },
641                 POPT_TABLEEND
642         };
643         poptContext popt_context;
644         struct action_type *action;
645         char *aname;
646         int r;
647         const char *null_action_argv[] = {NULL};
648
649         set_default_log(cmdLineLog);
650
651         setlocale(LC_ALL, "");
652         bindtextdomain(PACKAGE, LOCALEDIR);
653         textdomain(PACKAGE);
654
655         popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
656                                       popt_options, 0);
657         poptSetOtherOptionHelp(popt_context,
658                                N_("[OPTION...] <action> <action-specific>]"));
659
660         while((r = poptGetNextOpt(popt_context)) > 0) {
661                 unsigned long long ull_value;
662                 char *endp;
663
664                 ull_value = strtoull(popt_tmp, &endp, 0);
665                 if (*endp || !*popt_tmp)
666                         r = POPT_ERROR_BADNUMBER;
667
668                 switch(r) {
669                         case 1:
670                                 opt_size = ull_value;
671                                 break;
672                         case 2:
673                                 opt_offset = ull_value;
674                                 break;
675                         case 3:
676                                 opt_skip = ull_value;
677                                 break;
678                 }
679
680                 if (r < 0)
681                         break;
682         }
683
684         if (r < -1)
685                 usage(popt_context, 1, poptStrerror(r),
686                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
687         if (opt_version_mode) {
688                 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
689                 exit(0);
690         }
691
692         if (opt_key_size % 8)
693                 usage(popt_context, 1,
694                       _("Key size must be a multiple of 8 bits"),
695                       poptGetInvocationName(popt_context));
696
697         if (!(aname = (char *)poptGetArg(popt_context)))
698                 usage(popt_context, 1, _("Argument <action> missing."),
699                       poptGetInvocationName(popt_context));
700         for(action = action_types; action->type; action++)
701                 if (strcmp(action->type, aname) == 0)
702                         break;
703         if (!action->type)
704                 usage(popt_context, 1, _("Unknown action."),
705                       poptGetInvocationName(popt_context));
706
707         action_argc = 0;
708         action_argv = poptGetArgs(popt_context);
709         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
710         if(!action_argv) 
711                 action_argv = null_action_argv;
712
713         /* Count args, somewhat unnice, change? */
714         while(action_argv[action_argc] != NULL)
715                 action_argc++;
716
717         if(action_argc < action->required_action_argc) {
718                 char buf[128];
719                 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
720                 usage(popt_context, 1, buf,
721                       poptGetInvocationName(popt_context));
722         }
723
724         if (opt_debug) {
725                 opt_verbose = 1;
726                 crypt_set_debug_level(-1);
727                 _dbg_version_and_cmd(argc, argv);
728         }
729
730         return run_action(action);
731 }