49
54
static int action_isLuks(int arg);
50
55
static int action_luksUUID(int arg);
51
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);
53
62
static struct action_type {
57
66
int required_action_argc;
58
68
const char *arg_desc;
60
70
} action_types[] = {
61
{ "create", action_create, 0, 2, N_("<name> <device>"), N_("create device") },
62
{ "remove", action_remove, 0, 1, N_("<name>"), N_("remove device") },
63
{ "resize", action_resize, 0, 1, N_("<name>"), N_("resize active device") },
64
{ "status", action_status, 0, 1, N_("<name>"), N_("show device status") },
65
{ "luksFormat", action_luksFormat, 0, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
66
{ "luksOpen", action_luksOpen, 0, 2, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
67
{ "luksAddKey", action_luksAddKey, 0, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
68
{ "luksRemoveKey", action_luksRemoveKey, 0, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
69
{ "luksKillSlot", action_luksKillSlot, 0, 2, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
70
{ "luksUUID", action_luksUUID, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
71
{ "isLuks", action_isLuks, 0, 1, N_("<device>"), N_("tests <device> for LUKS partition header") },
72
{ "luksClose", action_remove, 0, 1, N_("<name>"), N_("remove LUKS mapping") },
73
{ "luksDump", action_luksDump, 0, 1, N_("<device>"), N_("dump LUKS partition information") },
74
{ "luksDelKey", action_luksDelKey, 0, 2, N_("<device> <key slot>"), N_("identical to luksKillSlot - DEPRECATED - see man page") },
75
{ "reload", action_create, 1, 2, N_("<name> <device>"), N_("modify active device - DEPRECATED - see man page") },
76
{ NULL, NULL, 0, 0, NULL }
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 class, 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, class, 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);
79
117
/* Interface Callbacks */
80
118
static int yesDialog(char *msg)
83
124
if(isatty(0) && !opt_batch_mode) {
86
fprintf(stderr,"\nWARNING!\n========\n");
87
fprintf(stderr,"%s\n\nAre you sure? (Type uppercase yes): ",msg);
88
if(getline(&answer,&size,stdin) == -1)
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) {
90
if(strcmp(answer,"YES\n") == 0)
132
if(strcmp(answer, "YES\n"))
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_LUKS_HASH,
344
.data_alignment = opt_align_payload,
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");
354
keysize = (opt_key_size ?: DEFAULT_LUKS_KEY_SIZE) / 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)
265
382
int r = 0; char *msg = NULL;
267
if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), options.device) == -1) {
268
fputs(_("memory allocation error in action_luksFormat"), stderr);
270
r = yesDialog(msg) ? crypt_luksFormat(&options) : -EINVAL;
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(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
391
log_err(_("memory allocation error in action_luksFormat"));
400
if (opt_master_key_file)
401
return _action_luksFormat_useMK();
403
return _action_luksFormat_generateMK();
277
406
static int action_luksOpen(int arg)
388
533
.device = action_argv[0],
393
r = crypt_luksDump(&options);
537
return crypt_luksDump(&options);
540
static int action_luksSuspend(int arg)
542
struct crypt_device *cd = NULL;
545
r = crypt_init_by_name(&cd, action_argv[0]);
547
r = crypt_suspend(cd, action_argv[0]);
553
static int action_luksResume(int arg)
555
struct crypt_device *cd = NULL;
558
if ((r = crypt_init_by_name(&cd, action_argv[0])))
561
if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
565
r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
566
opt_key_file, opt_key_size / 8);
568
r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
575
static int action_luksBackup(int arg)
577
struct crypt_device *cd = NULL;
580
if (!opt_header_backup_file) {
581
log_err(_("Option --header-backup-file is required.\n"));
585
if ((r = crypt_init(&cd, action_argv[0])))
588
crypt_set_log_callback(cd, _log, NULL);
589
crypt_set_confirm_callback(cd, _yesDialog, NULL);
591
r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
597
static int action_luksRestore(int arg)
599
struct crypt_device *cd = NULL;
602
if (!opt_header_backup_file) {
603
log_err(_("Option --header-backup-file is required.\n"));
607
if ((r = crypt_init(&cd, action_argv[0])))
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);
444
695
static struct poptOption popt_options[] = {
445
696
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
446
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 },
447
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 },
448
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 },
449
701
{ "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
450
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 },
451
704
{ "key-size", 's', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
452
705
{ "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
453
706
{ "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
459
712
{ "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
460
713
{ "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
461
{ "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
462
{ "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
463
{ "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
464
{ "non-exclusive", '\0', POPT_ARG_NONE, &opt_non_exclusive, 0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."), 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 },
467
721
poptContext popt_context;