11 #include <libcryptsetup.h>
14 #include "../config.h"
16 #include "cryptsetup.h"
18 static int opt_verbose = 0;
19 static int opt_debug = 0;
20 static char *opt_cipher = NULL;
21 static char *opt_hash = NULL;
22 static int opt_verify_passphrase = 0;
23 static char *opt_key_file = NULL;
24 static char *opt_master_key_file = NULL;
25 static char *opt_header_backup_file = NULL;
26 static unsigned int opt_key_size = 0;
27 static int opt_key_slot = CRYPT_ANY_SLOT;
28 static uint64_t opt_size = 0;
29 static uint64_t opt_offset = 0;
30 static uint64_t opt_skip = 0;
31 static int opt_readonly = 0;
32 static int opt_iteration_time = 1000;
33 static int opt_batch_mode = 0;
34 static int opt_version_mode = 0;
35 static int opt_timeout = 0;
36 static int opt_tries = 3;
37 static int opt_align_payload = 0;
38 static int opt_non_exclusive = 0;
40 static const char **action_argv;
41 static int action_argc;
43 static int action_create(int arg);
44 static int action_remove(int arg);
45 static int action_resize(int arg);
46 static int action_status(int arg);
47 static int action_luksFormat(int arg);
48 static int action_luksOpen(int arg);
49 static int action_luksAddKey(int arg);
50 static int action_luksDelKey(int arg);
51 static int action_luksKillSlot(int arg);
52 static int action_luksRemoveKey(int arg);
53 static int action_isLuks(int arg);
54 static int action_luksUUID(int arg);
55 static int action_luksDump(int arg);
56 static int action_luksSuspend(int arg);
57 static int action_luksResume(int arg);
58 static int action_luksBackup(int arg);
59 static int action_luksRestore(int arg);
61 static struct action_type {
65 int required_action_argc;
66 int required_dm_backend;
72 { "create", action_create, 0, 2, 1, 1, 1, N_("<name> <device>"),N_("create device") },
73 { "remove", action_remove, 0, 1, 1, 0, 1, N_("<name>"), N_("remove device") },
74 { "resize", action_resize, 0, 1, 1, 1, 1, N_("<name>"), N_("resize active device") },
75 { "status", action_status, 0, 1, 1, 0, 1, N_("<name>"), N_("show device status") },
76 { "luksFormat", action_luksFormat, 0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
77 { "luksOpen", action_luksOpen, 0, 2, 1, 1, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
78 { "luksAddKey", action_luksAddKey, 0, 1, 1, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
79 { "luksRemoveKey",action_luksRemoveKey, 0, 1, 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, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
81 { "luksUUID", action_luksUUID, 0, 1, 0, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
82 { "isLuks", action_isLuks, 0, 1, 0, 0, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
83 { "luksClose", action_remove, 0, 1, 1, 0, 1, N_("<name>"), N_("remove LUKS mapping") },
84 { "luksDump", action_luksDump, 0, 1, 0, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
85 { "luksSuspend",action_luksSuspend, 0, 1, 1, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
86 { "luksResume", action_luksResume, 0, 1, 1, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
87 { "luksHeaderBackup",action_luksBackup, 0, 1, 1, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
88 { "luksHeaderRestore",action_luksRestore,0,1, 1, 1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
89 { "luksDelKey", action_luksDelKey, 0, 2, 1, 1, 1, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
90 { "reload", action_create, 1, 2, 1, 1, 1, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
91 { NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL }
94 /* Interface Callbacks */
95 static int yesDialog(char *msg)
101 if(isatty(0) && !opt_batch_mode) {
102 log_std("\nWARNING!\n========\n");
103 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
104 if(getline(&answer, &size, stdin) == -1) {
109 if(strcmp(answer, "YES\n"))
117 static void cmdLineLog(int class, char *msg) {
120 case CRYPT_LOG_NORMAL:
123 case CRYPT_LOG_ERROR:
127 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
132 static struct interface_callbacks cmd_icb = {
133 .yesDialog = yesDialog,
137 static void _log(int class, const char *msg, void *usrptr)
139 cmdLineLog(class, (char *)msg);
142 static int _yesDialog(const char *msg, void *usrptr)
144 return yesDialog((char*)msg);
149 static void show_status(int errcode)
151 char error[256], *error_;
153 if(!errcode && opt_verbose) {
154 log_std(_("Command successful.\n"));
158 crypt_get_error(error, sizeof(error));
161 error_ = strerror_r(errcode, error, sizeof(error));
162 if (error_ != error) {
163 strncpy(error, error_, sizeof(error));
164 error[sizeof(error) - 1] = '\0';
168 log_err(_("Command failed"));
170 log_err(": %s\n", error);
176 static int action_create(int reload)
178 struct crypt_options options = {
179 .name = action_argv[0],
180 .device = action_argv[1],
181 .cipher = opt_cipher?opt_cipher:DEFAULT_CIPHER,
182 .hash = opt_hash ?: DEFAULT_HASH,
183 .key_file = opt_key_file,
184 .key_size = ((opt_key_size)?opt_key_size:DEFAULT_KEY_SIZE)/8,
185 .key_slot = opt_key_slot,
188 .offset = opt_offset,
190 .timeout = opt_timeout,
197 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"));
199 if (options.hash && strcmp(options.hash, "plain") == 0)
201 if (opt_verify_passphrase)
202 options.flags |= CRYPT_FLAG_VERIFY;
204 options.flags |= CRYPT_FLAG_READONLY;
207 r = crypt_update_device(&options);
209 r = crypt_create_device(&options);
214 static int action_remove(int arg)
216 struct crypt_options options = {
217 .name = action_argv[0],
221 return crypt_remove_device(&options);
224 static int action_resize(int arg)
226 struct crypt_options options = {
227 .name = action_argv[0],
232 return crypt_resize_device(&options);
235 static int action_status(int arg)
237 struct crypt_options options = {
238 .name = action_argv[0],
243 r = crypt_query_device(&options);
249 log_std("%s/%s is inactive.\n", crypt_get_dir(), options.name);
253 log_std("%s/%s is active:\n", crypt_get_dir(), options.name);
254 log_std(" cipher: %s\n", options.cipher);
255 log_std(" keysize: %d bits\n", options.key_size * 8);
256 log_std(" device: %s\n", options.device);
257 log_std(" offset: %" PRIu64 " sectors\n", options.offset);
258 log_std(" size: %" PRIu64 " sectors\n", options.size);
260 log_std(" skipped: %" PRIu64 " sectors\n", options.skip);
261 log_std(" mode: %s\n", (options.flags & CRYPT_FLAG_READONLY)
262 ? "readonly" : "read/write");
263 crypt_put_options(&options);
269 static int _action_luksFormat_generateMK()
271 struct crypt_options options = {
272 .key_size = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8,
273 .key_slot = opt_key_slot,
274 .device = action_argv[0],
275 .cipher = opt_cipher ?: DEFAULT_LUKS_CIPHER,
276 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
277 .new_key_file = action_argc > 1 ? action_argv[1] : NULL,
278 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
279 .iteration_time = opt_iteration_time,
280 .timeout = opt_timeout,
281 .align_payload = opt_align_payload,
285 return crypt_luksFormat(&options);
288 static int _read_mk(const char *file, char **key, int keysize)
292 *key = malloc(keysize);
296 fd = open(file, O_RDONLY);
298 log_err("Cannot read keyfile %s.\n", file);
301 if ((read(fd, *key, keysize) != keysize)) {
302 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
304 memset(*key, 0, keysize);
312 static int _action_luksFormat_useMK()
314 int r = -EINVAL, keysize;
315 char *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
316 struct crypt_device *cd = NULL;
317 struct crypt_params_luks1 params = {
318 .hash = opt_hash ?: DEFAULT_LUKS_HASH,
319 .data_alignment = opt_align_payload,
322 if (parse_into_name_and_mode(opt_cipher ?: DEFAULT_LUKS_CIPHER, cipher, cipher_mode)) {
323 log_err("No known cipher specification pattern detected.\n");
327 keysize = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 8;
328 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
331 if ((r = crypt_init(&cd, action_argv[0])))
334 crypt_set_password_verify(cd, 1);
335 crypt_set_timeout(cd, opt_timeout);
336 if (opt_iteration_time)
337 crypt_set_iterarion_time(cd, opt_iteration_time);
339 if ((r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, keysize, ¶ms)))
342 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
347 memset(key, 0, keysize);
353 static int action_luksFormat(int arg)
355 int r = 0; char *msg = NULL;
357 /* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
358 if (opt_offset || opt_skip) {
359 log_err("Options --offset and --skip are not supported for luksFormat.\n");
363 if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
364 log_err(_("memory allocation error in action_luksFormat"));
373 if (opt_master_key_file)
374 return _action_luksFormat_useMK();
376 return _action_luksFormat_generateMK();
379 static int action_luksOpen(int arg)
381 struct crypt_options options = {
382 .name = action_argv[1],
383 .device = action_argv[0],
384 .key_file = opt_key_file,
385 .key_size = opt_key_file ? (opt_key_size / 8) : 0, /* limit bytes read from keyfile */
386 .timeout = opt_timeout,
387 .tries = opt_key_file ? 1 : opt_tries, /* verify is usefull only for tty */
392 options.flags |= CRYPT_FLAG_READONLY;
393 if (opt_non_exclusive)
394 log_err(_("Obsolete option --non-exclusive is ignored.\n"));
396 return crypt_luksOpen(&options);
399 static int action_luksDelKey(int arg)
401 log_err("luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n");
402 return action_luksKillSlot(arg);
405 static int action_luksKillSlot(int arg)
407 struct crypt_options options = {
408 .device = action_argv[0],
409 .key_slot = atoi(action_argv[1]),
410 .key_file = opt_key_file,
411 .timeout = opt_timeout,
412 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
416 return crypt_luksKillSlot(&options);
419 static int action_luksRemoveKey(int arg)
421 struct crypt_options options = {
422 .device = action_argv[0],
423 .new_key_file = action_argc>1?action_argv[1]:NULL,
424 .key_file = opt_key_file,
425 .timeout = opt_timeout,
426 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
430 return crypt_luksRemoveKey(&options);
433 static int _action_luksAddKey_useMK()
435 int r = -EINVAL, keysize;
437 struct crypt_device *cd = NULL;
439 if ((r = crypt_init(&cd, action_argv[0])))
442 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
445 keysize = crypt_get_volume_key_size(cd);
446 crypt_set_password_verify(cd, 1);
447 crypt_set_timeout(cd, opt_timeout);
448 if (opt_iteration_time)
449 crypt_set_iterarion_time(cd, opt_iteration_time);
451 if (_read_mk(opt_master_key_file, &key, keysize) < 0)
454 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot, key, keysize, NULL, 0);
458 memset(key, 0, keysize);
464 static int action_luksAddKey(int arg)
466 struct crypt_options options = {
467 .device = action_argv[0],
468 .new_key_file = action_argc>1?action_argv[1]:NULL,
469 .key_file = opt_key_file,
470 .key_slot = opt_key_slot,
471 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
472 .iteration_time = opt_iteration_time,
473 .timeout = opt_timeout,
477 if (opt_master_key_file)
478 return _action_luksAddKey_useMK();
480 return crypt_luksAddKey(&options);
483 static int action_isLuks(int arg)
485 struct crypt_options options = {
486 .device = action_argv[0],
490 return crypt_isLuks(&options);
493 static int action_luksUUID(int arg)
495 struct crypt_options options = {
496 .device = action_argv[0],
500 return crypt_luksUUID(&options);
503 static int action_luksDump(int arg)
505 struct crypt_options options = {
506 .device = action_argv[0],
510 return crypt_luksDump(&options);
513 static int action_luksSuspend(int arg)
515 struct crypt_device *cd = NULL;
518 r = crypt_init_by_name(&cd, action_argv[0]);
520 r = crypt_suspend(cd, action_argv[0]);
526 static int action_luksResume(int arg)
528 struct crypt_device *cd = NULL;
531 if ((r = crypt_init_by_name(&cd, action_argv[0])))
534 if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
538 r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
539 opt_key_file, opt_key_size / 8);
541 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
548 static int action_luksBackup(int arg)
550 struct crypt_device *cd = NULL;
553 if (!opt_header_backup_file) {
554 log_err(_("Option --header-backup-file is required.\n"));
558 if ((r = crypt_init(&cd, action_argv[0])))
561 crypt_set_log_callback(cd, _log, NULL);
562 crypt_set_confirm_callback(cd, _yesDialog, NULL);
564 r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
570 static int action_luksRestore(int arg)
572 struct crypt_device *cd = NULL;
575 if (!opt_header_backup_file) {
576 log_err(_("Option --header-backup-file is required.\n"));
580 if ((r = crypt_init(&cd, action_argv[0])))
583 crypt_set_log_callback(cd, _log, NULL);
584 crypt_set_confirm_callback(cd, _yesDialog, NULL);
585 r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
591 static void usage(poptContext popt_context, int exitcode,
592 const char *error, const char *more)
594 poptPrintUsage(popt_context, stderr, 0);
596 log_err("%s: %s\n", more, error);
600 static void help(poptContext popt_context, enum poptCallbackReason reason,
601 struct poptOption *key, const char * arg, void *data)
603 if (key->shortName == '?') {
604 struct action_type *action;
606 log_std("%s\n",PACKAGE_STRING);
608 poptPrintHelp(popt_context, stdout, 0);
611 "<action> is one of:\n"));
613 for(action = action_types; action->type; action++)
614 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
617 "<name> is the device to create under %s\n"
618 "<device> is the encrypted device\n"
619 "<key slot> is the LUKS key slot number to modify\n"
620 "<key file> optional key file for the new key for luksAddKey action\n"),
624 usage(popt_context, 0, NULL, NULL);
627 void set_debug_level(int level);
629 static void _dbg_version_and_cmd(int argc, char **argv)
633 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
634 for (i = 0; i < argc; i++) {
642 static int run_action(struct action_type *action)
646 if (dm_init(NULL, action->required_dm_backend) < 1) {
647 log_err("Cannot communicate with device-mapper. Is dm_mod kernel module loaded?\n");
651 if (action->required_memlock)
652 crypt_memory_lock(NULL, 1);
654 r = action->handler(action->arg);
656 if (action->required_memlock)
657 crypt_memory_lock(NULL, 0);
659 if (action->required_dm_backend)
662 if (r < 0 && (opt_verbose || action->show_status))
668 int main(int argc, char **argv)
670 static char *popt_tmp;
671 static struct poptOption popt_help_options[] = {
672 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
673 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
674 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
677 static struct poptOption popt_options[] = {
678 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
679 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
680 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
681 { "cipher", 'c', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
682 { "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 },
683 { "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
684 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file (can be /dev/random)"), NULL },
685 { "master-key-file", '\0', POPT_ARG_STRING, &opt_master_key_file, 0, N_("Read the volume (master) key from file."), NULL },
686 { "key-size", 's', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
687 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
688 { "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
689 { "offset", 'o', POPT_ARG_STRING, &popt_tmp, 2, N_("The start offset in the backend device"), N_("SECTORS") },
690 { "skip", 'p', POPT_ARG_STRING, &popt_tmp, 3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
691 { "readonly", 'r', POPT_ARG_NONE, &opt_readonly, 0, N_("Create a readonly mapping"), NULL },
692 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"),
694 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
695 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
696 { "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
697 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase canbe retried"), NULL },
698 { "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
699 { "non-exclusive", '\0', POPT_ARG_NONE, &opt_non_exclusive, 0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."), NULL },
700 { "header-backup-file",'\0', POPT_ARG_STRING, &opt_header_backup_file,0, N_("File with LUKS header and keyslots backup."), NULL },
703 poptContext popt_context;
704 struct action_type *action;
707 const char *null_action_argv[] = {NULL};
709 set_default_log(cmdLineLog);
711 setlocale(LC_ALL, "");
712 bindtextdomain(PACKAGE, LOCALEDIR);
715 popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
717 poptSetOtherOptionHelp(popt_context,
718 N_("[OPTION...] <action> <action-specific>]"));
720 while((r = poptGetNextOpt(popt_context)) > 0) {
721 unsigned long long ull_value;
724 ull_value = strtoull(popt_tmp, &endp, 0);
725 if (*endp || !*popt_tmp)
726 r = POPT_ERROR_BADNUMBER;
730 opt_size = ull_value;
733 opt_offset = ull_value;
736 opt_skip = ull_value;
745 usage(popt_context, 1, poptStrerror(r),
746 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
747 if (opt_version_mode) {
748 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
752 if (opt_key_size % 8)
753 usage(popt_context, 1,
754 _("Key size must be a multiple of 8 bits"),
755 poptGetInvocationName(popt_context));
757 if (!(aname = (char *)poptGetArg(popt_context)))
758 usage(popt_context, 1, _("Argument <action> missing."),
759 poptGetInvocationName(popt_context));
760 for(action = action_types; action->type; action++)
761 if (strcmp(action->type, aname) == 0)
764 usage(popt_context, 1, _("Unknown action."),
765 poptGetInvocationName(popt_context));
768 action_argv = poptGetArgs(popt_context);
769 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
771 action_argv = null_action_argv;
773 /* Count args, somewhat unnice, change? */
774 while(action_argv[action_argc] != NULL)
777 if(action_argc < action->required_action_argc) {
779 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
780 usage(popt_context, 1, buf,
781 poptGetInvocationName(popt_context));
786 crypt_set_debug_level(-1);
787 _dbg_version_and_cmd(argc, argv);
790 return run_action(action);