12 #include <libcryptsetup.h>
15 #include "../config.h"
17 #include "cryptsetup.h"
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;
41 static const char **action_argv;
42 static int action_argc;
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);
62 static struct action_type {
66 int required_action_argc;
72 { "create", action_create, 0, 2, 1, 1, N_("<name> <device>"),N_("create device") },
73 { "remove", action_remove, 0, 1, 1, 1, N_("<name>"), N_("remove device") },
74 { "resize", action_resize, 0, 1, 1, 1, N_("<name>"), N_("resize active device") },
75 { "status", action_status, 0, 1, 0, 1, N_("<name>"), N_("show device status") },
76 { "luksFormat", action_luksFormat, 0, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
77 { "luksOpen", action_luksOpen, 0, 2, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
78 { "luksAddKey", action_luksAddKey, 0, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
79 { "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
80 { "luksKillSlot", action_luksKillSlot, 0, 2, 1, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
81 { "luksUUID", action_luksUUID, 0, 1, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
82 { "isLuks", action_isLuks, 0, 1, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
83 { "luksClose", action_remove, 0, 1, 1, 1, N_("<name>"), N_("remove LUKS mapping") },
84 { "luksDump", action_luksDump, 0, 1, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
85 { "luksSuspend",action_luksSuspend, 0, 1, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
86 { "luksResume", action_luksResume, 0, 1, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
87 { "luksHeaderBackup",action_luksBackup, 0, 1, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
88 { "luksHeaderRestore",action_luksRestore,0,1, 1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
89 { "luksDelKey", action_luksDelKey, 0, 2, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
90 { "reload", action_create, 1, 2, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
91 { NULL, NULL, 0, 0, 0, 0, NULL, NULL }
94 static void clogger(struct crypt_device *cd, int class, const char *file,
95 int line, const char *format, ...)
100 va_start(argp, format);
102 if (vasprintf(&target, format, argp) > 0) {
104 crypt_log(cd, class, target);
106 } else if (opt_debug)
107 printf("# %s:%d %s\n", file ?: "?", line, target);
109 } else if (opt_debug)
110 printf("# %s\n", target);
118 /* Interface Callbacks */
119 static int yesDialog(char *msg)
125 if(isatty(0) && !opt_batch_mode) {
126 log_std("\nWARNING!\n========\n");
127 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
128 if(getline(&answer, &size, stdin) == -1) {
133 if(strcmp(answer, "YES\n"))
141 static void cmdLineLog(int class, char *msg) {
144 case CRYPT_LOG_NORMAL:
147 case CRYPT_LOG_ERROR:
151 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
156 static struct interface_callbacks cmd_icb = {
157 .yesDialog = yesDialog,
161 static void _log(int class, const char *msg, void *usrptr)
163 cmdLineLog(class, (char *)msg);
166 static int _yesDialog(const char *msg, void *usrptr)
168 return yesDialog((char*)msg);
173 static void show_status(int errcode)
175 char error[256], *error_;
177 if(!errcode && opt_verbose) {
178 log_std(_("Command successful.\n"));
182 crypt_get_error(error, sizeof(error));
185 error_ = strerror_r(errcode, error, sizeof(error));
186 if (error_ != error) {
187 strncpy(error, error_, sizeof(error));
188 error[sizeof(error) - 1] = '\0';
192 log_err(_("Command failed"));
194 log_err(": %s\n", error);
200 static int action_create(int reload)
202 struct crypt_options options = {
203 .name = action_argv[0],
204 .device = action_argv[1],
205 .cipher = opt_cipher?opt_cipher:DEFAULT_CIPHER,
206 .hash = opt_hash ?: DEFAULT_HASH,
207 .key_file = opt_key_file,
208 .key_size = ((opt_key_size)?opt_key_size:DEFAULT_KEY_SIZE)/8,
209 .key_slot = opt_key_slot,
212 .offset = opt_offset,
214 .timeout = opt_timeout,
221 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 if (options.hash && strcmp(options.hash, "plain") == 0)
225 if (opt_verify_passphrase)
226 options.flags |= CRYPT_FLAG_VERIFY;
228 options.flags |= CRYPT_FLAG_READONLY;
231 r = crypt_update_device(&options);
233 r = crypt_create_device(&options);
238 static int action_remove(int arg)
240 struct crypt_options options = {
241 .name = action_argv[0],
245 return crypt_remove_device(&options);
248 static int action_resize(int arg)
250 struct crypt_options options = {
251 .name = action_argv[0],
256 return crypt_resize_device(&options);
259 static int action_status(int arg)
261 struct crypt_options options = {
262 .name = action_argv[0],
267 r = crypt_query_device(&options);
273 log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
277 log_std("%s/%s is active:\n", crypt_get_dir(), options.name);
278 log_std(" cipher: %s\n", options.cipher);
279 log_std(" keysize: %d bits\n", options.key_size * 8);
280 log_std(" device: %s\n", options.device);
281 log_std(" offset: %" PRIu64 " sectors\n", options.offset);
282 log_std(" size: %" PRIu64 " sectors\n", options.size);
284 log_std(" skipped: %" PRIu64 " sectors\n", options.skip);
285 log_std(" mode: %s\n", (options.flags & CRYPT_FLAG_READONLY)
286 ? "readonly" : "read/write");
287 crypt_put_options(&options);
293 static int _action_luksFormat_generateMK()
295 struct crypt_options options = {
296 .key_size = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8,
297 .key_slot = opt_key_slot,
298 .device = action_argv[0],
299 .cipher = opt_cipher ?: DEFAULT_LUKS_CIPHER,
300 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
301 .new_key_file = action_argc > 1 ? action_argv[1] : NULL,
302 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
303 .iteration_time = opt_iteration_time,
304 .timeout = opt_timeout,
305 .align_payload = opt_align_payload,
309 return crypt_luksFormat(&options);
312 static int _read_mk(const char *file, char **key, int keysize)
316 *key = malloc(keysize);
320 fd = open(file, O_RDONLY);
322 log_err("Cannot read keyfile %s.\n", file);
325 if ((read(fd, *key, keysize) != keysize)) {
326 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
328 memset(*key, 0, keysize);
336 static int _action_luksFormat_useMK()
338 int r = -EINVAL, keysize;
339 char *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
340 struct crypt_device *cd = NULL;
341 struct crypt_params_luks1 params = {
342 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
343 .data_alignment = opt_align_payload,
346 if (sscanf(opt_cipher ?: DEFAULT_LUKS_CIPHER,
347 "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
348 cipher, cipher_mode) != 2) {
349 log_err("No known cipher specification pattern detected.\n");
353 keysize = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8;
354 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
357 if ((r = crypt_init(&cd, action_argv[0])))
360 crypt_set_password_verify(cd, 1);
361 crypt_set_timeout(cd, opt_timeout);
362 if (opt_iteration_time)
363 crypt_set_iterarion_time(cd, opt_iteration_time);
365 if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, ¶ms)))
368 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
373 memset(key, 0, keysize);
379 static int action_luksFormat(int arg)
381 int r = 0; char *msg = NULL;
383 /* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
384 if (opt_offset || opt_skip) {
385 log_err("Options --offset and --skip are not supported for luksFormat.\n");
389 if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
390 log_err(_("memory allocation error in action_luksFormat"));
399 if (opt_master_key_file)
400 return _action_luksFormat_useMK();
402 return _action_luksFormat_generateMK();
405 static int action_luksOpen(int arg)
407 struct crypt_options options = {
408 .name = action_argv[1],
409 .device = action_argv[0],
410 .key_file = opt_key_file,
411 .key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
412 .timeout = opt_timeout,
413 .tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
418 options.flags |= CRYPT_FLAG_READONLY;
419 if (opt_non_exclusive)
420 log_err(_("Obsolete option --non-exclusive is ignored.\n"));
422 return crypt_luksOpen(&options);
425 static int action_luksDelKey(int arg)
427 log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n");
428 return action_luksKillSlot(arg);
431 static int action_luksKillSlot(int arg)
433 struct crypt_options options = {
434 .device = action_argv[0],
435 .key_slot = atoi(action_argv[1]),
436 .key_file = opt_key_file,
437 .timeout = opt_timeout,
438 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
442 return crypt_luksKillSlot(&options);
445 static int action_luksRemoveKey(int arg)
447 struct crypt_options options = {
448 .device = action_argv[0],
449 .new_key_file = action_argc>1?action_argv[1]:NULL,
450 .key_file = opt_key_file,
451 .timeout = opt_timeout,
452 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
456 return crypt_luksRemoveKey(&options);
459 static int _action_luksAddKey_useMK()
461 int r = -EINVAL, keysize;
463 struct crypt_device *cd = NULL;
465 if ((r = crypt_init(&cd, action_argv[0])))
468 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
471 keysize = crypt_get_volume_key_size(cd);
472 crypt_set_password_verify(cd, 1);
473 crypt_set_timeout(cd, opt_timeout);
474 if (opt_iteration_time)
475 crypt_set_iterarion_time(cd, opt_iteration_time);
477 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
480 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
484 memset(key, 0, keysize);
490 static int action_luksAddKey(int arg)
492 struct crypt_options options = {
493 .device = action_argv[0],
494 .new_key_file = action_argc>1?action_argv[1]:NULL,
495 .key_file = opt_key_file,
496 .key_slot = opt_key_slot,
497 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
498 .iteration_time = opt_iteration_time,
499 .timeout = opt_timeout,
503 if (opt_master_key_file)
504 return _action_luksAddKey_useMK();
506 return crypt_luksAddKey(&options);
509 static int action_isLuks(int arg)
511 struct crypt_options options = {
512 .device = action_argv[0],
516 return crypt_isLuks(&options);
519 static int action_luksUUID(int arg)
521 struct crypt_options options = {
522 .device = action_argv[0],
526 return crypt_luksUUID(&options);
529 static int action_luksDump(int arg)
531 struct crypt_options options = {
532 .device = action_argv[0],
536 return crypt_luksDump(&options);
539 static int action_luksSuspend(int arg)
541 struct crypt_device *cd = NULL;
544 r = crypt_init_by_name(&cd, action_argv[0]);
546 r = crypt_suspend(cd, action_argv[0]);
552 static int action_luksResume(int arg)
554 struct crypt_device *cd = NULL;
557 if ((r = crypt_init_by_name(&cd, action_argv[0])))
560 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
564 r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
565 opt_key_file, opt_key_size / 8);
567 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
574 static int action_luksBackup(int arg)
576 struct crypt_device *cd = NULL;
579 if (!opt_header_backup_file) {
580 log_err(_("Option --header-backup-file is required.\n"));
584 if ((r = crypt_init(&cd, action_argv[0])))
587 crypt_set_log_callback(cd, _log, NULL);
588 crypt_set_confirm_callback(cd, _yesDialog, NULL);
590 r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
596 static int action_luksRestore(int arg)
598 struct crypt_device *cd = NULL;
601 if (!opt_header_backup_file) {
602 log_err(_("Option --header-backup-file is required.\n"));
606 if ((r = crypt_init(&cd, action_argv[0])))
609 crypt_set_log_callback(cd, _log, NULL);
610 crypt_set_confirm_callback(cd, _yesDialog, NULL);
611 r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
617 static void usage(poptContext popt_context, int exitcode,
618 const char *error, const char *more)
620 poptPrintUsage(popt_context, stderr, 0);
622 log_err("%s: %s\n", more, error);
626 static void help(poptContext popt_context, enum poptCallbackReason reason,
627 struct poptOption *key, const char * arg, void *data)
629 if (key->shortName == '?') {
630 struct action_type *action;
632 log_std("%s\n",PACKAGE_STRING);
634 poptPrintHelp(popt_context, stdout, 0);
637 "<action> is one of:\n"));
639 for(action = action_types; action->type; action++)
640 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
643 "<name> is the device to create under %s\n"
644 "<device> is the encrypted device\n"
645 "<key slot> is the LUKS key slot number to modify\n"
646 "<key file> optional key file for the new key for luksAddKey action\n"),
650 usage(popt_context, 0, NULL, NULL);
653 void set_debug_level(int level);
655 static void _dbg_version_and_cmd(int argc, char **argv)
659 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
660 for (i = 0; i < argc; i++) {
668 static int run_action(struct action_type *action)
672 if (action->required_memlock)
673 crypt_memory_lock(NULL, 1);
675 r = action->handler(action->arg);
677 if (action->required_memlock)
678 crypt_memory_lock(NULL, 0);
680 if (r < 0 && (opt_verbose || action->show_status))
686 int main(int argc, char **argv)
688 static char *popt_tmp;
689 static struct poptOption popt_help_options[] = {
690 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
691 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
692 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
695 static struct poptOption popt_options[] = {
696 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
697 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
698 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
699 { "cipher", 'c', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
700 { "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 },
701 { "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
702 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file (can be /dev/random)"), NULL },
703 { "master-key-file", '\0', POPT_ARG_STRING, &opt_master_key_file, 0, N_("Read the volume (master) key from file."), NULL },
704 { "key-size", 's', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
705 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
706 { "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
707 { "offset", 'o', POPT_ARG_STRING, &popt_tmp, 2, N_("The start offset in the backend device"), N_("SECTORS") },
708 { "skip", 'p', POPT_ARG_STRING, &popt_tmp, 3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
709 { "readonly", 'r', POPT_ARG_NONE, &opt_readonly, 0, N_("Create a readonly mapping"), NULL },
710 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"),
712 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
713 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
714 { "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
715 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase canbe retried"), NULL },
716 { "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
717 { "non-exclusive", '\0', POPT_ARG_NONE, &opt_non_exclusive, 0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."), NULL },
718 { "header-backup-file",'\0', POPT_ARG_STRING, &opt_header_backup_file,0, N_("File with LUKS header and keyslots backup."), NULL },
721 poptContext popt_context;
722 struct action_type *action;
725 const char *null_action_argv[] = {NULL};
727 crypt_set_log_callback(NULL, _log, NULL);
729 setlocale(LC_ALL, "");
730 bindtextdomain(PACKAGE, LOCALEDIR);
733 popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
735 poptSetOtherOptionHelp(popt_context,
736 N_("[OPTION...] <action> <action-specific>]"));
738 while((r = poptGetNextOpt(popt_context)) > 0) {
739 unsigned long long ull_value;
742 ull_value = strtoull(popt_tmp, &endp, 0);
743 if (*endp || !*popt_tmp)
744 r = POPT_ERROR_BADNUMBER;
748 opt_size = ull_value;
751 opt_offset = ull_value;
754 opt_skip = ull_value;
763 usage(popt_context, 1, poptStrerror(r),
764 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
765 if (opt_version_mode) {
766 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
770 if (opt_key_size % 8)
771 usage(popt_context, 1,
772 _("Key size must be a multiple of 8 bits"),
773 poptGetInvocationName(popt_context));
775 if (!(aname = (char *)poptGetArg(popt_context)))
776 usage(popt_context, 1, _("Argument <action> missing."),
777 poptGetInvocationName(popt_context));
778 for(action = action_types; action->type; action++)
779 if (strcmp(action->type, aname) == 0)
782 usage(popt_context, 1, _("Unknown action."),
783 poptGetInvocationName(popt_context));
786 action_argv = poptGetArgs(popt_context);
787 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
789 action_argv = null_action_argv;
791 /* Count args, somewhat unnice, change? */
792 while(action_argv[action_argc] != NULL)
795 if(action_argc < action->required_action_argc) {
797 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
798 usage(popt_context, 1, buf,
799 poptGetInvocationName(popt_context));
804 crypt_set_debug_level(-1);
805 _dbg_version_and_cmd(argc, argv);
808 return run_action(action);