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