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