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;
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 }
93 static void clogger(struct crypt_device *cd, int level, const char *file,
94 int line, const char *format, ...)
99 va_start(argp, format);
101 if (vasprintf(&target, format, argp) > 0) {
103 crypt_log(cd, level, target);
105 } else if (opt_debug)
106 printf("# %s:%d %s\n", file ?: "?", line, target);
108 } else if (opt_debug)
109 printf("# %s\n", target);
117 /* Interface Callbacks */
118 static int yesDialog(char *msg)
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) {
132 if(strcmp(answer, "YES\n"))
140 static void cmdLineLog(int level, char *msg) {
143 case CRYPT_LOG_NORMAL:
146 case CRYPT_LOG_ERROR:
150 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
155 static struct interface_callbacks cmd_icb = {
156 .yesDialog = yesDialog,
160 static void _log(int level, const char *msg, void *usrptr)
162 cmdLineLog(level, (char *)msg);
165 static int _yesDialog(const char *msg, void *usrptr)
167 return yesDialog((char*)msg);
172 static void show_status(int errcode)
174 char error[256], *error_;
180 log_std(_("Command successful.\n"));
184 crypt_get_error(error, sizeof(error));
187 error_ = strerror_r(-errcode, error, sizeof(error));
188 if (error_ != error) {
189 strncpy(error, error_, sizeof(error));
190 error[sizeof(error) - 1] = '\0';
194 log_err(_("Command failed with code %i"), -errcode);
196 log_err(": %s\n", error);
201 static int action_create(int reload)
203 struct crypt_options options = {
204 .name = action_argv[0],
205 .device = action_argv[1],
206 .cipher = opt_cipher ? opt_cipher : DEFAULT_CIPHER(PLAIN),
207 .hash = opt_hash ?: DEFAULT_PLAIN_HASH,
208 .key_file = opt_key_file,
209 .key_size = (opt_key_size ?: DEFAULT_PLAIN_KEYBITS) / 8,
210 .key_slot = opt_key_slot,
213 .offset = opt_offset,
215 .timeout = opt_timeout,
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"));
224 if (options.hash && strcmp(options.hash, "plain") == 0)
226 if (opt_verify_passphrase)
227 options.flags |= CRYPT_FLAG_VERIFY;
229 options.flags |= CRYPT_FLAG_READONLY;
232 r = crypt_update_device(&options);
234 r = crypt_create_device(&options);
239 static int action_remove(int arg)
241 struct crypt_options options = {
242 .name = action_argv[0],
246 return crypt_remove_device(&options);
249 static int action_resize(int arg)
251 struct crypt_options options = {
252 .name = action_argv[0],
257 return crypt_resize_device(&options);
260 static int action_status(int arg)
262 struct crypt_options options = {
263 .name = action_argv[0],
268 r = crypt_query_device(&options);
274 log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
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);
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);
294 static int _action_luksFormat_generateMK()
296 struct crypt_options options = {
297 .key_size = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8,
298 .key_slot = opt_key_slot,
299 .device = action_argv[0],
300 .cipher = opt_cipher ?: DEFAULT_CIPHER(LUKS1),
301 .hash = opt_hash ?: DEFAULT_LUKS1_HASH,
302 .new_key_file = opt_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,
310 return crypt_luksFormat(&options);
313 static int _read_mk(const char *file, char **key, int keysize)
317 *key = malloc(keysize);
321 fd = open(file, O_RDONLY);
323 log_err("Cannot read keyfile %s.\n", file);
326 if ((read(fd, *key, keysize) != keysize)) {
327 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
329 memset(*key, 0, keysize);
337 static int _action_luksFormat_useMK()
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_LUKS1_HASH,
344 .data_alignment = opt_align_payload,
347 if (sscanf(opt_cipher ?: DEFAULT_CIPHER(LUKS1),
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");
354 keysize = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8;
355 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
358 if ((r = crypt_init(&cd, action_argv[0])))
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);
366 if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, ¶ms)))
369 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
374 memset(key, 0, keysize);
380 static int action_luksFormat(int arg)
382 int r = 0; char *msg = NULL;
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");
390 if (action_argc > 1 && opt_key_file)
391 log_err(_("Option --key-file takes precedence over specified key file argument.\n"));
393 if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
394 log_err(_("memory allocation error in action_luksFormat"));
403 if (opt_master_key_file)
404 return _action_luksFormat_useMK();
406 return _action_luksFormat_generateMK();
409 static int action_luksOpen(int arg)
411 struct crypt_options options = {
412 .name = action_argv[1],
413 .device = action_argv[0],
414 .key_file = opt_key_file,
415 .key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
416 .timeout = opt_timeout,
417 .tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
422 options.flags |= CRYPT_FLAG_READONLY;
423 if (opt_non_exclusive)
424 log_err(_("Obsolete option --non-exclusive is ignored.\n"));
426 return crypt_luksOpen(&options);
429 static int action_luksDelKey(int arg)
431 log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n");
432 return action_luksKillSlot(arg);
435 static int action_luksKillSlot(int arg)
437 struct crypt_options options = {
438 .device = action_argv[0],
439 .key_slot = atoi(action_argv[1]),
440 .key_file = opt_key_file,
441 .timeout = opt_timeout,
442 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
446 return crypt_luksKillSlot(&options);
449 static int action_luksRemoveKey(int arg)
451 struct crypt_options options = {
452 .device = action_argv[0],
453 .new_key_file = action_argc>1?action_argv[1]:NULL,
454 .key_file = opt_key_file,
455 .timeout = opt_timeout,
456 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
460 return crypt_luksRemoveKey(&options);
463 static int _action_luksAddKey_useMK()
465 int r = -EINVAL, keysize = 0;
467 struct crypt_device *cd = NULL;
469 if ((r = crypt_init(&cd, action_argv[0])))
472 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
475 keysize = crypt_get_volume_key_size(cd);
476 crypt_set_password_verify(cd, 1);
477 crypt_set_timeout(cd, opt_timeout);
478 if (opt_iteration_time)
479 crypt_set_iterarion_time(cd, opt_iteration_time);
481 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
484 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
488 memset(key, 0, keysize);
494 static int action_luksAddKey(int arg)
496 struct crypt_options options = {
497 .device = action_argv[0],
498 .new_key_file = action_argc>1?action_argv[1]:NULL,
499 .key_file = opt_key_file,
500 .key_slot = opt_key_slot,
501 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
502 .iteration_time = opt_iteration_time,
503 .timeout = opt_timeout,
507 if (opt_master_key_file)
508 return _action_luksAddKey_useMK();
510 return crypt_luksAddKey(&options);
513 static int action_isLuks(int arg)
515 struct crypt_options options = {
516 .device = action_argv[0],
520 return crypt_isLuks(&options);
523 static int action_luksUUID(int arg)
525 struct crypt_options options = {
526 .device = action_argv[0],
530 return crypt_luksUUID(&options);
533 static int action_luksDump(int arg)
535 struct crypt_options options = {
536 .device = action_argv[0],
540 return crypt_luksDump(&options);
543 static int action_luksSuspend(int arg)
545 struct crypt_device *cd = NULL;
548 r = crypt_init_by_name(&cd, action_argv[0]);
550 r = crypt_suspend(cd, action_argv[0]);
556 static int action_luksResume(int arg)
558 struct crypt_device *cd = NULL;
561 if ((r = crypt_init_by_name(&cd, action_argv[0])))
564 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
568 r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
569 opt_key_file, opt_key_size / 8);
571 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
578 static int action_luksBackup(int arg)
580 struct crypt_device *cd = NULL;
583 if (!opt_header_backup_file) {
584 log_err(_("Option --header-backup-file is required.\n"));
588 if ((r = crypt_init(&cd, action_argv[0])))
591 crypt_set_log_callback(cd, _log, NULL);
592 crypt_set_confirm_callback(cd, _yesDialog, NULL);
594 r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
600 static int action_luksRestore(int arg)
602 struct crypt_device *cd = NULL;
605 if (!opt_header_backup_file) {
606 log_err(_("Option --header-backup-file is required.\n"));
610 if ((r = crypt_init(&cd, action_argv[0])))
613 crypt_set_log_callback(cd, _log, NULL);
614 crypt_set_confirm_callback(cd, _yesDialog, NULL);
615 r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
621 static void usage(poptContext popt_context, int exitcode,
622 const char *error, const char *more)
624 poptPrintUsage(popt_context, stderr, 0);
626 log_err("%s: %s\n", more, error);
630 static void help(poptContext popt_context, enum poptCallbackReason reason,
631 struct poptOption *key, const char * arg, void *data)
633 if (key->shortName == '?') {
634 struct action_type *action;
636 log_std("%s\n",PACKAGE_STRING);
638 poptPrintHelp(popt_context, stdout, 0);
641 "<action> is one of:\n"));
643 for(action = action_types; action->type; action++)
644 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
647 "<name> is the device to create under %s\n"
648 "<device> is the encrypted device\n"
649 "<key slot> is the LUKS key slot number to modify\n"
650 "<key file> optional key file for the new key for luksAddKey action\n"),
653 log_std(_("\nDefault compiled-in device cipher parameters:\n"
654 "\tplain: %s, Key: %d bits, Password hashing: %s\n"
655 "\tLUKS1: %s, Key: %d bits, LUKS header hashing: %s\n"),
656 DEFAULT_CIPHER(PLAIN), DEFAULT_PLAIN_KEYBITS, DEFAULT_PLAIN_HASH,
657 DEFAULT_CIPHER(LUKS1), DEFAULT_LUKS1_KEYBITS, DEFAULT_LUKS1_HASH);
660 usage(popt_context, 0, NULL, NULL);
663 void set_debug_level(int level);
665 static void _dbg_version_and_cmd(int argc, char **argv)
669 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
670 for (i = 0; i < argc; i++) {
678 static int run_action(struct action_type *action)
682 if (action->required_memlock)
683 crypt_memory_lock(NULL, 1);
685 r = action->handler(action->arg);
687 if (action->required_memlock)
688 crypt_memory_lock(NULL, 0);
695 int main(int argc, char **argv)
697 static char *popt_tmp;
698 static struct poptOption popt_help_options[] = {
699 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
700 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
701 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
704 static struct poptOption popt_options[] = {
705 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
706 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
707 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
708 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
709 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
710 { "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
711 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file (can be /dev/random)"), NULL },
712 { "master-key-file", '\0', POPT_ARG_STRING, &opt_master_key_file, 0, N_("Read the volume (master) key from file."), NULL },
713 { "key-size", 's', POPT_ARG_INT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
714 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
715 { "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
716 { "offset", 'o', POPT_ARG_STRING, &popt_tmp, 2, N_("The start offset in the backend device"), N_("SECTORS") },
717 { "skip", 'p', POPT_ARG_STRING, &popt_tmp, 3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
718 { "readonly", 'r', POPT_ARG_NONE, &opt_readonly, 0, N_("Create a readonly mapping"), NULL },
719 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
720 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
721 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
722 { "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
723 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
724 { "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
725 { "non-exclusive", '\0', POPT_ARG_NONE, &opt_non_exclusive, 0, N_("(Obsoleted, see man page.)"), NULL },
726 { "header-backup-file",'\0', POPT_ARG_STRING, &opt_header_backup_file, 0, N_("File with LUKS header and keyslots backup."), NULL },
729 poptContext popt_context;
730 struct action_type *action;
733 const char *null_action_argv[] = {NULL};
735 crypt_set_log_callback(NULL, _log, NULL);
737 setlocale(LC_ALL, "");
738 bindtextdomain(PACKAGE, LOCALEDIR);
741 popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
743 poptSetOtherOptionHelp(popt_context,
744 N_("[OPTION...] <action> <action-specific>]"));
746 while((r = poptGetNextOpt(popt_context)) > 0) {
747 unsigned long long ull_value;
750 ull_value = strtoull(popt_tmp, &endp, 0);
751 if (*endp || !*popt_tmp)
752 r = POPT_ERROR_BADNUMBER;
756 opt_size = ull_value;
759 opt_offset = ull_value;
762 opt_skip = ull_value;
771 usage(popt_context, 1, poptStrerror(r),
772 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
773 if (opt_version_mode) {
774 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
778 if (opt_key_size % 8)
779 usage(popt_context, 1,
780 _("Key size must be a multiple of 8 bits"),
781 poptGetInvocationName(popt_context));
783 if (!(aname = (char *)poptGetArg(popt_context)))
784 usage(popt_context, 1, _("Argument <action> missing."),
785 poptGetInvocationName(popt_context));
786 for(action = action_types; action->type; action++)
787 if (strcmp(action->type, aname) == 0)
790 usage(popt_context, 1, _("Unknown action."),
791 poptGetInvocationName(popt_context));
794 action_argv = poptGetArgs(popt_context);
795 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
797 action_argv = null_action_argv;
799 /* Count args, somewhat unnice, change? */
800 while(action_argv[action_argc] != NULL)
803 if(action_argc < action->required_action_argc) {
805 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
806 usage(popt_context, 1, buf,
807 poptGetInvocationName(popt_context));
812 crypt_set_debug_level(-1);
813 _dbg_version_and_cmd(argc, argv);
816 return run_action(action);