10 #include <libcryptsetup.h>
13 #include "../config.h"
15 #include "cryptsetup.h"
17 static int opt_verbose = 1;
18 static char *opt_cipher = NULL;
19 static char *opt_hash = NULL;
20 static int opt_verify_passphrase = 0;
21 static char *opt_key_file = NULL;
22 static unsigned int opt_key_size = 0;
23 static int opt_key_slot = -1;
24 static uint64_t opt_size = 0;
25 static uint64_t opt_offset = 0;
26 static uint64_t opt_skip = 0;
27 static int opt_readonly = 0;
28 static int opt_iteration_time = 1000;
29 static int opt_batch_mode = 0;
30 static int opt_version_mode = 0;
31 static int opt_timeout = 0;
32 static int opt_tries = 3;
33 static int opt_align_payload = 0;
34 static int opt_non_exclusive = 0;
36 static const char **action_argv;
37 static int action_argc;
39 static int action_create(int arg);
40 static int action_remove(int arg);
41 static int action_resize(int arg);
42 static int action_status(int arg);
43 static int action_luksFormat(int arg);
44 static int action_luksOpen(int arg);
45 static int action_luksAddKey(int arg);
46 static int action_luksDelKey(int arg);
47 static int action_luksKillSlot(int arg);
48 static int action_luksRemoveKey(int arg);
49 static int action_isLuks(int arg);
50 static int action_luksUUID(int arg);
51 static int action_luksDump(int arg);
53 static struct action_type {
57 int required_action_argc;
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 }
79 /* Interface Callbacks */
80 static int yesDialog(char *msg)
86 if(isatty(0) && !opt_batch_mode) {
87 fprintf(stderr, "\nWARNING!\n========\n");
88 fprintf(stderr, "%s\n\nAre you sure? (Type uppercase yes): ", msg);
89 if(getline(&answer, &size, stdin) == -1) {
94 if(strcmp(answer, "YES\n"))
102 static void cmdLineLog(int class, char *msg) {
105 case CRYPT_LOG_NORMAL:
108 case CRYPT_LOG_ERROR:
112 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
117 static struct interface_callbacks cmd_icb = {
118 .yesDialog = yesDialog,
124 static void show_status(int errcode)
129 fprintf(stderr, _("Command successful.\n"));
133 crypt_get_error(error, sizeof(error));
135 char *error_ = strerror_r(errcode, error, sizeof(error));
136 if (error_ != error) {
137 strncpy(error, error_, sizeof(error));
138 error[sizeof error - 1] = '\0';
142 fprintf(stderr, _("Command failed"));
144 fprintf(stderr, ": %s\n", error);
146 fputs(".\n", stderr);
150 static int action_create(int reload)
152 struct crypt_options options = {
153 .name = action_argv[0],
154 .device = action_argv[1],
155 .cipher = opt_cipher?opt_cipher:DEFAULT_CIPHER,
156 .hash = opt_hash ?: DEFAULT_HASH,
157 .key_file = opt_key_file,
158 .key_size = ((opt_key_size)?opt_key_size:DEFAULT_KEY_SIZE)/8,
159 .key_slot = opt_key_slot,
160 .passphrase_fd = 0, /* stdin */
163 .offset = opt_offset,
165 .timeout = opt_timeout,
172 fprintf(stderr, _("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"));
174 if (options.hash && strcmp(options.hash, "plain") == 0)
176 if (opt_verify_passphrase)
177 options.flags |= CRYPT_FLAG_VERIFY;
179 options.flags |= CRYPT_FLAG_READONLY;
182 r = crypt_update_device(&options);
184 r = crypt_create_device(&options);
190 static int action_remove(int arg)
192 struct crypt_options options = {
193 .name = action_argv[0],
198 r = crypt_remove_device(&options);
204 static int action_resize(int arg)
206 struct crypt_options options = {
207 .name = action_argv[0],
213 r = crypt_resize_device(&options);
219 static int action_status(int arg)
221 struct crypt_options options = {
222 .name = action_argv[0],
227 r = crypt_query_device(&options);
234 printf("%s/%s is inactive.\n", crypt_get_dir(), options.name);
238 printf("%s/%s is active:\n", crypt_get_dir(), options.name);
239 printf(" cipher: %s\n", options.cipher);
240 printf(" keysize: %d bits\n", options.key_size * 8);
241 printf(" device: %s\n", options.device);
242 printf(" offset: %" PRIu64 " sectors\n", options.offset);
243 printf(" size: %" PRIu64 " sectors\n", options.size);
245 printf(" skipped: %" PRIu64 " sectors\n", options.skip);
246 printf(" mode: %s\n", (options.flags & CRYPT_FLAG_READONLY)
247 ? "readonly" : "read/write");
248 crypt_put_options(&options);
254 static int action_luksFormat(int arg)
256 struct crypt_options options = {
257 .key_size = (opt_key_size != 0 ? opt_key_size : DEFAULT_LUKS_KEY_SIZE) / 8,
258 .key_slot = opt_key_slot,
259 .device = action_argv[0],
260 .cipher = opt_cipher?opt_cipher:DEFAULT_LUKS_CIPHER,
261 .hash = DEFAULT_LUKS_HASH,
262 // FIXME: enable other hash use here
263 //.hash = opt_hash ?: DEFAULT_LUKS_HASH,
264 .new_key_file = action_argc > 1 ? action_argv[1] : NULL,
265 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
266 .iteration_time = opt_iteration_time,
267 .timeout = opt_timeout,
268 .align_payload = opt_align_payload,
272 int r = 0; char *msg = NULL;
274 /* Avoid overwriting possibly wrong part of device than user requested by rejecting these options */
275 if (opt_offset || opt_skip) {
276 fprintf(stderr,"Options --offset and --skip are not supported for luksFormat.\n");
280 if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), options.device) == -1) {
281 fputs(_("memory allocation error in action_luksFormat"), stderr);
283 r = yesDialog(msg) ? crypt_luksFormat(&options) : -EINVAL;
290 static int action_luksOpen(int arg)
292 struct crypt_options options = {
293 .name = action_argv[1],
294 .device = action_argv[0],
295 .key_file = opt_key_file,
296 .timeout = opt_timeout,
305 options.flags |= CRYPT_FLAG_READONLY;
306 if (opt_non_exclusive)
307 options.flags |= CRYPT_FLAG_NON_EXCLUSIVE_ACCESS;
308 r = crypt_luksOpen(&options);
313 static int action_luksDelKey(int arg)
315 fprintf(stderr,"luksDelKey is a deprecated action name.\nPlease use luksKillSlot.\n");
316 return action_luksKillSlot(arg);
319 static int action_luksKillSlot(int arg)
321 struct crypt_options options = {
322 .device = action_argv[0],
323 .key_slot = atoi(action_argv[1]),
324 .key_file = opt_key_file,
325 .timeout = opt_timeout,
326 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
332 r = crypt_luksKillSlot(&options);
337 static int action_luksRemoveKey(int arg)
339 struct crypt_options options = {
340 .device = action_argv[0],
341 .new_key_file = action_argc>1?action_argv[1]:NULL,
342 .key_file = opt_key_file,
343 .timeout = opt_timeout,
344 .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
350 r = crypt_luksRemoveKey(&options);
355 static int action_luksAddKey(int arg)
357 struct crypt_options options = {
358 .device = action_argv[0],
359 .new_key_file = action_argc>1?action_argv[1]:NULL,
360 .key_file = opt_key_file,
361 .key_slot = opt_key_slot,
362 .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
363 .iteration_time = opt_iteration_time,
364 .timeout = opt_timeout,
370 r = crypt_luksAddKey(&options);
375 static int action_isLuks(int arg)
377 struct crypt_options options = {
378 .device = action_argv[0],
381 return crypt_isLuks(&options);
384 static int action_luksUUID(int arg)
386 struct crypt_options options = {
387 .device = action_argv[0],
392 r = crypt_luksUUID(&options);
398 static int action_luksDump(int arg)
400 struct crypt_options options = {
401 .device = action_argv[0],
406 r = crypt_luksDump(&options);
412 static void usage(poptContext popt_context, int exitcode,
413 const char *error, const char *more)
415 poptPrintUsage(popt_context, stderr, 0);
417 fprintf(stderr, "%s: %s\n", more, error);
421 static void help(poptContext popt_context, enum poptCallbackReason reason,
422 struct poptOption *key, const char * arg, void *data)
424 if (key->shortName == '?') {
425 struct action_type *action;
427 fprintf(stdout, "%s\n",PACKAGE_STRING);
429 poptPrintHelp(popt_context, stdout, 0);
432 "<action> is one of:\n"));
434 for(action = action_types; action->type; action++)
435 printf("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
438 "<name> is the device to create under %s\n"
439 "<device> is the encrypted device\n"
440 "<key slot> is the LUKS key slot number to modify\n"
441 "<key file> optional key file for the new key for luksAddKey action\n"),
445 usage(popt_context, 0, NULL, NULL);
448 int main(int argc, char **argv)
450 static char *popt_tmp;
451 static struct poptOption popt_help_options[] = {
452 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
453 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
454 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
457 static struct poptOption popt_options[] = {
458 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
459 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
460 { "cipher", 'c', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
461 { "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 },
462 { "verify-passphrase", 'y', POPT_ARG_NONE, &opt_verify_passphrase, 0, N_("Verifies the passphrase by asking for it twice"), NULL },
463 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file (can be /dev/random)"), NULL },
464 { "key-size", 's', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
465 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Slot number for new key (default is first free)"), NULL },
466 { "size", 'b', POPT_ARG_STRING, &popt_tmp, 1, N_("The size of the device"), N_("SECTORS") },
467 { "offset", 'o', POPT_ARG_STRING, &popt_tmp, 2, N_("The start offset in the backend device"), N_("SECTORS") },
468 { "skip", 'p', POPT_ARG_STRING, &popt_tmp, 3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
469 { "readonly", 'r', POPT_ARG_NONE, &opt_readonly, 0, N_("Create a readonly mapping"), NULL },
470 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"),
472 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
473 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
474 { "timeout", 't', POPT_ARG_INT, &opt_timeout, 0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
475 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
476 { "align-payload", '\0', POPT_ARG_INT, &opt_align_payload, 0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
477 { "non-exclusive", '\0', POPT_ARG_NONE, &opt_non_exclusive, 0, N_("Allows non-exclusive access for luksOpen, WARNING see manpage."), NULL },
480 poptContext popt_context;
481 struct action_type *action;
484 const char *null_action_argv[] = {NULL};
486 setlocale(LC_ALL, "");
487 bindtextdomain(PACKAGE, LOCALEDIR);
490 popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
492 poptSetOtherOptionHelp(popt_context,
493 N_("[OPTION...] <action> <action-specific>]"));
495 while((r = poptGetNextOpt(popt_context)) > 0) {
496 unsigned long long ull_value;
499 ull_value = strtoull(popt_tmp, &endp, 0);
500 if (*endp || !*popt_tmp)
501 r = POPT_ERROR_BADNUMBER;
505 opt_size = ull_value;
508 opt_offset = ull_value;
511 opt_skip = ull_value;
520 usage(popt_context, 1, poptStrerror(r),
521 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
522 if (opt_version_mode) {
523 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
527 if (opt_key_size % 8)
528 usage(popt_context, 1,
529 _("Key size must be a multiple of 8 bits"),
530 poptGetInvocationName(popt_context));
532 if (!(aname = (char *)poptGetArg(popt_context)))
533 usage(popt_context, 1, _("Argument <action> missing."),
534 poptGetInvocationName(popt_context));
535 for(action = action_types; action->type; action++)
536 if (strcmp(action->type, aname) == 0)
539 usage(popt_context, 1, _("Unknown action."),
540 poptGetInvocationName(popt_context));
543 action_argv = poptGetArgs(popt_context);
544 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
546 action_argv = null_action_argv;
548 /* Count args, somewhat unnice, change? */
549 while(action_argv[action_argc] != NULL)
552 if(action_argc < action->required_action_argc) {
554 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
555 usage(popt_context, 1, buf,
556 poptGetInvocationName(popt_context));
558 return action->handler(action->arg);
563 // indent-tabs-mode: nil