Also support --skip option for loopaesOpen.
[platform/upstream/cryptsetup.git] / src / cryptsetup.c
1 /*
2  * cryptsetup - setup cryptographic volumes for dm-crypt
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdarg.h>
27 #include <inttypes.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <assert.h>
32 #include <libcryptsetup.h>
33 #include <popt.h>
34
35 #include "cryptsetup.h"
36
37 static int opt_verbose = 0;
38 static int opt_debug = 0;
39 static char *opt_cipher = NULL;
40 static char *opt_hash = NULL;
41 static int opt_verify_passphrase = 0;
42 static char *opt_key_file = NULL;
43 static char *opt_master_key_file = NULL;
44 static char *opt_header_backup_file = NULL;
45 static char *opt_uuid = NULL;
46 static int opt_key_size = 0;
47 static long opt_keyfile_size = 0;
48 static long opt_new_keyfile_size = 0;
49 static int opt_key_slot = CRYPT_ANY_SLOT;
50 static uint64_t opt_size = 0;
51 static uint64_t opt_offset = 0;
52 static uint64_t opt_skip = 0;
53 static int opt_skip_valid = 0;
54 static int opt_readonly = 0;
55 static int opt_iteration_time = 1000;
56 static int opt_batch_mode = 0;
57 static int opt_version_mode = 0;
58 static int opt_timeout = 0;
59 static int opt_tries = 3;
60 static int opt_align_payload = 0;
61 static int opt_random = 0;
62 static int opt_urandom = 0;
63 static int opt_dump_master_key = 0;
64
65 static const char **action_argv;
66 static int action_argc;
67
68 static int action_create(int arg);
69 static int action_remove(int arg);
70 static int action_resize(int arg);
71 static int action_status(int arg);
72 static int action_luksFormat(int arg);
73 static int action_luksOpen(int arg);
74 static int action_luksAddKey(int arg);
75 static int action_luksKillSlot(int arg);
76 static int action_luksRemoveKey(int arg);
77 static int action_luksChangeKey(int arg);
78 static int action_isLuks(int arg);
79 static int action_luksUUID(int arg);
80 static int action_luksDump(int arg);
81 static int action_luksSuspend(int arg);
82 static int action_luksResume(int arg);
83 static int action_luksBackup(int arg);
84 static int action_luksRestore(int arg);
85 static int action_loopaesOpen(int arg);
86
87 static struct action_type {
88         const char *type;
89         int (*handler)(int);
90         int arg;
91         int required_action_argc;
92         int required_memlock;
93         const char *arg_desc;
94         const char *desc;
95 } action_types[] = {
96         { "create",     action_create,          0, 2, 1, N_("<name> <device>"),N_("create device") },
97         { "remove",     action_remove,          0, 1, 1, N_("<name>"), N_("remove device") },
98         { "resize",     action_resize,          0, 1, 1, N_("<name>"), N_("resize active device") },
99         { "status",     action_status,          0, 1, 0, N_("<name>"), N_("show device status") },
100         { "luksFormat", action_luksFormat,      0, 1, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
101         { "luksOpen",   action_luksOpen,        0, 2, 1, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
102         { "luksAddKey", action_luksAddKey,      0, 1, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
103         { "luksRemoveKey",action_luksRemoveKey, 0, 1, 1, N_("<device> [<key file>]"), N_("removes supplied key or key file from LUKS device") },
104         { "luksChangeKey",action_luksChangeKey, 0, 1, 1, N_("<device> [<key file>]"), N_("changes supplied key or key file of LUKS device") },
105         { "luksKillSlot",  action_luksKillSlot, 0, 2, 1, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
106         { "luksUUID",   action_luksUUID,        0, 1, 0, N_("<device>"), N_("print UUID of LUKS device") },
107         { "isLuks",     action_isLuks,          0, 1, 0, N_("<device>"), N_("tests <device> for LUKS partition header") },
108         { "luksClose",  action_remove,          0, 1, 1, N_("<name>"), N_("remove LUKS mapping") },
109         { "luksDump",   action_luksDump,        0, 1, 1, N_("<device>"), N_("dump LUKS partition information") },
110         { "luksSuspend",action_luksSuspend,     0, 1, 1, N_("<device>"), N_("Suspend LUKS device and wipe key (all IOs are frozen).") },
111         { "luksResume", action_luksResume,      0, 1, 1, N_("<device>"), N_("Resume suspended LUKS device.") },
112         { "luksHeaderBackup",action_luksBackup, 0, 1, 1, N_("<device>"), N_("Backup LUKS device header and keyslots") },
113         { "luksHeaderRestore",action_luksRestore,0,1, 1, N_("<device>"), N_("Restore LUKS device header and keyslots") },
114         { "loopaesOpen",action_loopaesOpen,     0, 2, 1, N_("<device> <name> "), N_("open loop-AES device as mapping <name>") },
115         { "loopaesClose",action_remove,         0, 1, 1, N_("<name>"), N_("remove loop-AES mapping") },
116         { NULL, NULL, 0, 0, 0, NULL, NULL }
117 };
118
119 static void clogger(struct crypt_device *cd, int level, const char *file,
120                    int line, const char *format, ...)
121 {
122         va_list argp;
123         char *target = NULL;
124
125         va_start(argp, format);
126
127         if (vasprintf(&target, format, argp) > 0) {
128                 if (level >= 0) {
129                         crypt_log(cd, level, target);
130 #ifdef CRYPT_DEBUG
131                 } else if (opt_debug)
132                         printf("# %s:%d %s\n", file ?: "?", line, target);
133 #else
134                 } else if (opt_debug)
135                         printf("# %s\n", target);
136 #endif
137         }
138
139         va_end(argp);
140         free(target);
141 }
142
143 static int _yesDialog(const char *msg, void *usrptr)
144 {
145         char *answer = NULL;
146         size_t size = 0;
147         int r = 1;
148
149         if(isatty(0) && !opt_batch_mode) {
150                 log_std("\nWARNING!\n========\n");
151                 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
152                 if(getline(&answer, &size, stdin) == -1) {
153                         perror("getline");
154                         free(answer);
155                         return 0;
156                 }
157                 if(strcmp(answer, "YES\n"))
158                         r = 0;
159                 free(answer);
160         }
161
162         return r;
163 }
164
165 static void _log(int level, const char *msg, void *usrptr)
166 {
167         switch(level) {
168
169         case CRYPT_LOG_NORMAL:
170                 fputs(msg, stdout);
171                 break;
172         case CRYPT_LOG_VERBOSE:
173                 if (opt_verbose)
174                         fputs(msg, stdout);
175                 break;
176         case CRYPT_LOG_ERROR:
177                 fputs(msg, stderr);
178                 break;
179         case CRYPT_LOG_DEBUG:
180                 if (opt_debug)
181                         printf("# %s\n", msg);
182                 break;
183         default:
184                 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
185                 break;
186         }
187 }
188
189 static void show_status(int errcode)
190 {
191         char error[256], *error_;
192
193         if(!opt_verbose)
194                 return;
195
196         if(!errcode) {
197                 log_std(_("Command successful.\n"));
198                 return;
199         }
200
201         crypt_get_error(error, sizeof(error));
202
203         if (!error[0]) {
204                 error_ = strerror_r(-errcode, error, sizeof(error));
205                 if (error_ != error) {
206                         strncpy(error, error_, sizeof(error));
207                         error[sizeof(error) - 1] = '\0';
208                 }
209         }
210
211         log_err(_("Command failed with code %i"), -errcode);
212         if (*error)
213                 log_err(": %s\n", error);
214         else
215                 log_err(".\n");
216 }
217
218 static int action_create(int arg)
219 {
220         struct crypt_device *cd = NULL;
221         char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
222         struct crypt_params_plain params = {
223                 .hash = opt_hash ?: DEFAULT_PLAIN_HASH,
224                 .skip = opt_skip,
225                 .offset = opt_offset,
226         };
227         char *password = NULL;
228         size_t passwordLen;
229         size_t key_size = (opt_key_size ?: DEFAULT_PLAIN_KEYBITS) / 8;
230         int r;
231
232         if (params.hash && !strcmp(params.hash, "plain"))
233                 params.hash = NULL;
234
235         /* FIXME: temporary hack */
236         if (opt_key_file && strcmp(opt_key_file, "-"))
237                 params.hash = NULL;
238
239         if (opt_keyfile_size && opt_key_file)
240                 log_std(("Ignoring keyfile size option, keyfile read size "
241                          "is always the same as encryption key size.\n"));
242
243         r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(PLAIN),
244                                       cipher, NULL, cipher_mode);
245         if (r < 0) {
246                 log_err("No known cipher specification pattern detected.\n");
247                 goto out;
248         }
249
250         if ((r = crypt_init(&cd, action_argv[1])))
251                 goto out;
252
253         crypt_set_timeout(cd, opt_timeout);
254         crypt_set_password_retry(cd, opt_tries);
255
256         r = crypt_format(cd, CRYPT_PLAIN,
257                          cipher, cipher_mode,
258                          NULL, NULL,
259                          key_size,
260                          &params);
261         if (r < 0)
262                 goto out;
263
264         if (opt_key_file)
265                 r = crypt_activate_by_keyfile(cd, action_argv[0],
266                         CRYPT_ANY_SLOT, opt_key_file, key_size,
267                         opt_readonly ?  CRYPT_ACTIVATE_READONLY : 0);
268         else {
269                 r = crypt_get_key(_("Enter passphrase: "),
270                                   &password, &passwordLen, opt_keyfile_size,
271                                   NULL, opt_timeout,
272                                   opt_batch_mode ? 0 : opt_verify_passphrase,
273                                   cd);
274                 if (r < 0)
275                         goto out;
276
277                 r = crypt_activate_by_passphrase(cd, action_argv[0],
278                         CRYPT_ANY_SLOT, password, passwordLen,
279                         opt_readonly ?  CRYPT_ACTIVATE_READONLY : 0);
280         }
281 out:
282         crypt_free(cd);
283         crypt_safe_free(password);
284
285         return r;
286 }
287
288 static int action_loopaesOpen(int arg)
289 {
290         struct crypt_device *cd = NULL;
291         struct crypt_params_loopaes params = {
292                 .hash = opt_hash ?: NULL, // FIXME
293                 .offset = opt_offset,
294                 .skip = opt_skip_valid ? opt_skip : opt_offset,
295         };
296         unsigned int key_size = (opt_key_size ?: DEFAULT_LOOPAES_KEYBITS) / 8;
297         int r;
298
299         if (!opt_key_file) {
300                 log_err(_("Option --key-file is required.\n"));
301                 return -EINVAL;
302         }
303
304         if ((r = crypt_init(&cd, action_argv[0])))
305                 goto out;
306
307         r = crypt_format(cd, CRYPT_LOOPAES, opt_cipher ?: DEFAULT_LOOPAES_CIPHER,
308                          NULL, NULL, NULL, key_size, &params);
309         if (r < 0)
310                 goto out;
311
312         r = crypt_activate_by_keyfile(cd, action_argv[1],
313                 CRYPT_ANY_SLOT, opt_key_file, opt_keyfile_size,
314                 opt_readonly ?  CRYPT_ACTIVATE_READONLY : 0);
315 out:
316         crypt_free(cd);
317
318         return r;
319 }
320
321 static int action_remove(int arg)
322 {
323         struct crypt_device *cd = NULL;
324         int r;
325
326         r = crypt_init_by_name(&cd, action_argv[0]);
327         if (r == 0)
328                 r = crypt_deactivate(cd, action_argv[0]);
329
330         crypt_free(cd);
331         return r;
332 }
333
334 static int action_resize(int arg)
335 {
336         struct crypt_device *cd = NULL;
337         int r;
338
339         r = crypt_init_by_name(&cd, action_argv[0]);
340         if (r == 0)
341                 r = crypt_resize(cd, action_argv[0], opt_size);
342
343         crypt_free(cd);
344         return r;
345 }
346
347 static int action_status(int arg)
348 {
349         crypt_status_info ci;
350         struct crypt_active_device cad;
351         struct crypt_device *cd = NULL;
352         char *backing_file;
353         const char *device;
354         int r = 0;
355
356         ci = crypt_status(NULL, action_argv[0]);
357         switch (ci) {
358         case CRYPT_INVALID:
359                 r = -ENODEV;
360                 break;
361         case CRYPT_INACTIVE:
362                 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
363                 break;
364         case CRYPT_ACTIVE:
365         case CRYPT_BUSY:
366                 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
367                         ci == CRYPT_BUSY ? " and is in use" : "");
368                 r = crypt_init_by_name(&cd, action_argv[0]);
369                 if (r < 0 || !crypt_get_type(cd))
370                         goto out;
371
372                 log_std("  type:    %s\n", crypt_get_type(cd));
373
374                 r = crypt_get_active_device(cd, action_argv[0], &cad);
375                 if (r < 0)
376                         goto out;
377
378                 log_std("  cipher:  %s-%s\n", crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
379                 log_std("  keysize: %d bits\n", crypt_get_volume_key_size(cd) * 8);
380                 device = crypt_get_device_name(cd);
381                 log_std("  device:  %s\n", device);
382                 if (crypt_loop_device(device)) {
383                         backing_file = crypt_loop_backing_file(device);
384                         log_std("  loop:    %s\n", backing_file);
385                         free(backing_file);
386                 }
387                 log_std("  offset:  %" PRIu64 " sectors\n", cad.offset);
388                 log_std("  size:    %" PRIu64 " sectors\n", cad.size);
389                 if (cad.iv_offset)
390                         log_std("  skipped: %" PRIu64 " sectors\n", cad.iv_offset);
391                 log_std("  mode:    %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
392                                            "readonly" : "read/write");
393         }
394 out:
395         crypt_free(cd);
396         return r;
397 }
398
399 static int _read_mk(const char *file, char **key, int keysize)
400 {
401         int fd;
402
403         *key = crypt_safe_alloc(keysize);
404         if (!*key)
405                 return -ENOMEM;
406
407         fd = open(file, O_RDONLY);
408         if (fd == -1) {
409                 log_err("Cannot read keyfile %s.\n", file);
410                 goto fail;
411         }
412         if ((read(fd, *key, keysize) != keysize)) {
413                 log_err("Cannot read %d bytes from keyfile %s.\n", keysize, file);
414                 close(fd);
415                 goto fail;
416         }
417         close(fd);
418         return 0;
419 fail:
420         crypt_safe_free(*key);
421         *key = NULL;
422         return -EINVAL;
423 }
424
425 static int action_luksFormat(int arg)
426 {
427         int r = -EINVAL, keysize;
428         char *msg = NULL, *key = NULL, cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
429         char *password = NULL;
430         size_t passwordLen;
431         struct crypt_device *cd = NULL;
432         struct crypt_params_luks1 params = {
433                 .hash = opt_hash ?: DEFAULT_LUKS1_HASH,
434                 .data_alignment = opt_align_payload,
435         };
436
437         if(asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]) == -1) {
438                 log_err(_("memory allocation error in action_luksFormat"));
439                 r = -ENOMEM;
440                 goto out;
441         }
442         r = _yesDialog(msg, NULL) ? 0 : -EINVAL;
443         free(msg);
444         if (r < 0)
445                 goto out;
446
447         r = crypt_parse_name_and_mode(opt_cipher ?: DEFAULT_CIPHER(LUKS1),
448                                       cipher, NULL, cipher_mode);
449         if (r < 0) {
450                 log_err("No known cipher specification pattern detected.\n");
451                 goto out;
452         }
453
454         if ((r = crypt_init(&cd, action_argv[0])))
455                 goto out;
456
457         keysize = (opt_key_size ?: DEFAULT_LUKS1_KEYBITS) / 8;
458
459         crypt_set_password_verify(cd, 1);
460         crypt_set_timeout(cd, opt_timeout);
461         if (opt_iteration_time)
462                 crypt_set_iterarion_time(cd, opt_iteration_time);
463
464         if (opt_random)
465                 crypt_set_rng_type(cd, CRYPT_RNG_RANDOM);
466         else if (opt_urandom)
467                 crypt_set_rng_type(cd, CRYPT_RNG_URANDOM);
468
469         r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen,
470                           opt_keyfile_size, opt_key_file, opt_timeout,
471                           opt_batch_mode ? 0 : 1 /* always verify */, cd);
472         if (r < 0)
473                 goto out;
474
475         if (opt_master_key_file) {
476                 r = _read_mk(opt_master_key_file, &key, keysize);
477                 if (r < 0)
478                         goto out;
479         }
480
481         r = crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode,
482                          opt_uuid, key, keysize, &params);
483         if (r < 0)
484                 goto out;
485
486         r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot,
487                                             key, keysize,
488                                             password, passwordLen);
489 out:
490         crypt_free(cd);
491         crypt_safe_free(key);
492         crypt_safe_free(password);
493
494         return r;
495 }
496
497 static int action_luksOpen(int arg)
498 {
499         struct crypt_device *cd = NULL;
500         uint32_t flags = 0;
501         int r;
502
503         if ((r = crypt_init(&cd, action_argv[0])))
504                 goto out;
505
506         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
507                 goto out;
508
509         crypt_set_timeout(cd, opt_timeout);
510         crypt_set_password_retry(cd, opt_tries);
511
512         if (opt_iteration_time)
513                 crypt_set_iterarion_time(cd, opt_iteration_time);
514         if (opt_readonly)
515                 flags |= CRYPT_ACTIVATE_READONLY;
516
517         if (opt_key_file) {
518                 crypt_set_password_retry(cd, 1);
519                 r = crypt_activate_by_keyfile(cd, action_argv[1],
520                         CRYPT_ANY_SLOT, opt_key_file, opt_keyfile_size,
521                         flags);
522         } else
523                 r = crypt_activate_by_passphrase(cd, action_argv[1],
524                         CRYPT_ANY_SLOT, NULL, 0, flags);
525 out:
526         crypt_free(cd);
527         return r;
528 }
529
530 static int verify_keyslot(struct crypt_device *cd, int key_slot,
531                           char *msg_last, char *msg_pass,
532                           const char *key_file, int keyfile_size)
533 {
534         crypt_keyslot_info ki;
535         char *password = NULL;
536         size_t passwordLen;
537         int i, r;
538
539         ki = crypt_keyslot_status(cd, key_slot);
540         if (ki == CRYPT_SLOT_ACTIVE_LAST && msg_last && !_yesDialog(msg_last, NULL))
541                 return -EPERM;
542
543         r = crypt_get_key(msg_pass, &password, &passwordLen,
544                           keyfile_size, key_file, opt_timeout,
545                           opt_batch_mode ? 0 : opt_verify_passphrase, cd);
546         if(r < 0)
547                 goto out;
548
549         if (ki == CRYPT_SLOT_ACTIVE_LAST) {
550                 /* check the last keyslot */
551                 r = crypt_activate_by_passphrase(cd, NULL, key_slot,
552                                                  password, passwordLen, 0);
553         } else {
554                 /* try all other keyslots */
555                 for (i = 0; i < crypt_keyslot_max(CRYPT_LUKS1); i++) {
556                         if (i == key_slot)
557                                 continue;
558                         ki = crypt_keyslot_status(cd, key_slot);
559                         if (ki == CRYPT_SLOT_ACTIVE)
560                         r = crypt_activate_by_passphrase(cd, NULL, i,
561                                                          password, passwordLen, 0);
562                         if (r == i)
563                                 break;
564                 }
565         }
566
567         if (r < 0)
568                 log_err(_("No key available with this passphrase.\n"));
569 out:
570         crypt_safe_free(password);
571         return r;
572 }
573
574 static int action_luksKillSlot(int arg)
575 {
576         struct crypt_device *cd = NULL;
577         int r;
578
579         if ((r = crypt_init(&cd, action_argv[0])))
580                 goto out;
581
582         crypt_set_confirm_callback(cd, _yesDialog, NULL);
583         crypt_set_timeout(cd, opt_timeout);
584
585         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
586                 goto out;
587
588         switch (crypt_keyslot_status(cd, opt_key_slot)) {
589         case CRYPT_SLOT_ACTIVE_LAST:
590         case CRYPT_SLOT_ACTIVE:
591                 log_verbose(_("Key slot %d selected for deletion.\n"), opt_key_slot);
592                 break;
593         case CRYPT_SLOT_INACTIVE:
594                 log_err(_("Key %d not active. Can't wipe.\n"), opt_key_slot);
595         case CRYPT_SLOT_INVALID:
596                 goto out;
597         }
598
599         if (!opt_batch_mode) {
600                 r = verify_keyslot(cd, opt_key_slot,
601                         _("This is the last keyslot. Device will become unusable after purging this key."),
602                         _("Enter any remaining LUKS passphrase: "),
603                         opt_key_file, opt_keyfile_size);
604                 if (r < 0)
605                         goto out;
606         }
607
608         r = crypt_keyslot_destroy(cd, opt_key_slot);
609 out:
610         crypt_free(cd);
611         return r;
612 }
613
614 static int action_luksRemoveKey(int arg)
615 {
616         struct crypt_device *cd = NULL;
617         char *password = NULL;
618         size_t passwordLen;
619         int r;
620
621         if ((r = crypt_init(&cd, action_argv[0])))
622                 goto out;
623
624         crypt_set_confirm_callback(cd, _yesDialog, NULL);
625         crypt_set_timeout(cd, opt_timeout);
626
627         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
628                 goto out;
629
630         r = crypt_get_key(_("Enter LUKS passphrase to be deleted: "),
631                       &password, &passwordLen,
632                       opt_keyfile_size, opt_key_file,
633                       opt_timeout,
634                       opt_batch_mode ? 0 : opt_verify_passphrase,
635                       cd);
636         if(r < 0)
637                 goto out;
638
639         r = crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT,
640                                          password, passwordLen, 0);
641         if (r < 0)
642                 goto out;
643
644         opt_key_slot = r;
645         log_verbose(_("Key slot %d selected for deletion.\n"), opt_key_slot);
646
647         if (crypt_keyslot_status(cd, opt_key_slot) == CRYPT_SLOT_ACTIVE_LAST &&
648             !_yesDialog(_("This is the last keyslot. "
649                           "Device will become unusable after purging this key."),
650                         NULL)) {
651                 r = -EPERM;
652                 goto out;
653         }
654
655         r = crypt_keyslot_destroy(cd, opt_key_slot);
656 out:
657         crypt_safe_free(password);
658         crypt_free(cd);
659         return r;
660 }
661
662 static int action_luksAddKey(int arg)
663 {
664         int r = -EINVAL, keysize = 0;
665         char *key = NULL;
666         const char *opt_new_key_file = (action_argc > 1 ? action_argv[1] : NULL);
667         struct crypt_device *cd = NULL;
668
669         if ((r = crypt_init(&cd, action_argv[0])))
670                 goto out;
671
672         crypt_set_confirm_callback(cd, _yesDialog, NULL);
673
674         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
675                 goto out;
676
677         keysize = crypt_get_volume_key_size(cd);
678         crypt_set_password_verify(cd, opt_verify_passphrase ? 1 : 0);
679         crypt_set_timeout(cd, opt_timeout);
680         if (opt_iteration_time)
681                 crypt_set_iterarion_time(cd, opt_iteration_time);
682
683         if (opt_master_key_file) {
684                 r = _read_mk(opt_master_key_file, &key, keysize);
685                 if (r < 0)
686                         goto out;
687                 //FIXME: process keyfile arg
688                 r = crypt_keyslot_add_by_volume_key(cd, opt_key_slot,
689                                                     key, keysize, NULL, 0);
690         } else if (opt_key_file || opt_new_key_file) {
691                 r = crypt_keyslot_add_by_keyfile(cd, opt_key_slot,
692                                                  opt_key_file, opt_keyfile_size,
693                                                  opt_new_key_file, opt_new_keyfile_size);
694         } else {
695                 r = crypt_keyslot_add_by_passphrase(cd, opt_key_slot,
696                                                     NULL, 0, NULL, 0);
697         }
698 out:
699         crypt_free(cd);
700         crypt_safe_free(key);
701         return r;
702 }
703
704 static int _slots_full(struct crypt_device *cd)
705 {
706         int i;
707
708         for (i = 0; i < crypt_keyslot_max(crypt_get_type(cd)); i++)
709                 if (crypt_keyslot_status(cd, i) == CRYPT_SLOT_INACTIVE)
710                         return 0;
711         return 1;
712 }
713
714 static int action_luksChangeKey(int arg)
715 {
716         const char *opt_new_key_file = (action_argc > 1 ? action_argv[1] : NULL);
717         struct crypt_device *cd = NULL;
718         char *vk = NULL, *password = NULL;
719         size_t passwordLen = 0;
720         size_t vk_size;
721         int new_key_slot, old_key_slot, r;
722
723         if ((r = crypt_init(&cd, action_argv[0])))
724                 goto out;
725
726         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
727                 goto out;
728
729         if (opt_iteration_time)
730                 crypt_set_iterarion_time(cd, opt_iteration_time);
731
732         r = crypt_get_key(_("Enter LUKS passphrase to be changed: "),
733                       &password, &passwordLen,
734                       opt_keyfile_size, opt_key_file, opt_timeout,
735                       opt_batch_mode ? 0 : opt_verify_passphrase, cd);
736         if (r < 0)
737                 goto out;
738
739         vk_size = crypt_get_volume_key_size(cd);
740         vk = crypt_safe_alloc(vk_size);
741         if (!vk) {
742                 r = -ENOMEM;
743                 goto out;
744         }
745
746         r = crypt_volume_key_get(cd, opt_key_slot, vk, &vk_size,
747                                  password, passwordLen);
748         if (r < 0) {
749                 if (opt_key_slot != CRYPT_ANY_SLOT)
750                         log_err(_("No key available with this passphrase.\n"));
751                 goto out;
752         }
753
754         if (opt_key_slot != CRYPT_ANY_SLOT || _slots_full(cd)) {
755                 log_dbg("Key slot %d is going to be overwritten (%s).",
756                         r, opt_key_slot != CRYPT_ANY_SLOT ?
757                         "explicit key slot specified" : "no free key slot");
758                 old_key_slot = r;
759                 new_key_slot = r;
760         } else {
761                 log_dbg("Allocating new key slot.");
762                 old_key_slot = r;
763                 new_key_slot = CRYPT_ANY_SLOT;
764         }
765
766         crypt_safe_free(password);
767         password = NULL;
768         passwordLen = 0;
769         r = crypt_get_key(_("Enter new LUKS passphrase: "),
770                           &password, &passwordLen,
771                           opt_new_keyfile_size, opt_new_key_file,
772                           opt_timeout, opt_batch_mode ? 0 : 1, cd);
773         if (r < 0)
774                 goto out;
775
776         if (new_key_slot == old_key_slot) {
777                 (void)crypt_keyslot_destroy(cd, old_key_slot);
778                 r = crypt_keyslot_add_by_volume_key(cd, new_key_slot,
779                                                     vk, vk_size,
780                                                     password, passwordLen);
781                 if (r >= 0)
782                         log_verbose(_("Key slot %d changed.\n"), r);
783         } else {
784                 r = crypt_keyslot_add_by_volume_key(cd, CRYPT_ANY_SLOT,
785                                                     vk, vk_size,
786                                                     password, passwordLen);
787                 if (r >= 0) {
788                         log_verbose(_("Replaced with key slot %d.\n"), r);
789                         r = crypt_keyslot_destroy(cd, old_key_slot);
790                 }
791         }
792         if (r < 0)
793                 log_err(_("Failed to swap new key slot.\n"));
794 out:
795         crypt_safe_free(vk);
796         crypt_safe_free(password);
797         crypt_free(cd);
798         return r;
799 }
800
801 static int action_isLuks(int arg)
802 {
803         struct crypt_device *cd = NULL;
804         int r;
805
806         if ((r = crypt_init(&cd, action_argv[0])))
807                 goto out;
808
809         r = crypt_load(cd, CRYPT_LUKS1, NULL);
810 out:
811         crypt_free(cd);
812         return r;
813 }
814
815 static int action_luksUUID(int arg)
816 {
817         struct crypt_device *cd = NULL;
818         const char *existing_uuid = NULL;
819         int r;
820
821         if ((r = crypt_init(&cd, action_argv[0])))
822                 goto out;
823
824         crypt_set_confirm_callback(cd, _yesDialog, NULL);
825
826         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
827                 goto out;
828
829         if (opt_uuid)
830                 r = crypt_set_uuid(cd, opt_uuid);
831         else {
832                 existing_uuid = crypt_get_uuid(cd);
833                 log_std("%s\n", existing_uuid ?: "");
834                 r = existing_uuid ? 0 : 1;
835         }
836 out:
837         crypt_free(cd);
838         return r;
839 }
840
841 static int luksDump_with_volume_key(struct crypt_device *cd)
842 {
843         char *vk = NULL, *password = NULL;
844         size_t passwordLen = 0;
845         size_t vk_size;
846         int i, r;
847
848         crypt_set_confirm_callback(cd, _yesDialog, NULL);
849         if (!_yesDialog(
850             _("LUKS header dump with volume key is sensitive information\n"
851               "which allows access to encrypted partition without passphrase.\n"
852               "This dump should be always stored encrypted on safe place."),
853               NULL))
854                 return -EPERM;
855
856         vk_size = crypt_get_volume_key_size(cd);
857         vk = crypt_safe_alloc(vk_size);
858         if (!vk)
859                 return -ENOMEM;
860
861         r = crypt_get_key(_("Enter LUKS passphrase: "), &password, &passwordLen,
862                           opt_keyfile_size, opt_key_file, opt_timeout, 0, cd);
863         if (r < 0)
864                 goto out;
865
866         r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, vk, &vk_size,
867                                  password, passwordLen);
868         if (r < 0)
869                 goto out;
870
871         log_std("LUKS header information for %s\n", crypt_get_device_name(cd));
872         log_std("Cipher name:   \t%s\n", crypt_get_cipher(cd));
873         log_std("Cipher mode:   \t%s\n", crypt_get_cipher_mode(cd));
874         log_std("Payload offset:\t%d\n", crypt_get_data_offset(cd));
875         log_std("UUID:          \t%s\n", crypt_get_uuid(cd));
876         log_std("MK bits:       \t%d\n", vk_size * 8);
877         log_std("MK dump:\t");
878
879         for(i = 0; i < vk_size; i++) {
880                 if (i && !(i % 16))
881                         log_std("\n\t\t");
882                 log_std("%02hhx ", (char)vk[i]);
883         }
884         log_std("\n");
885
886 out:
887         crypt_safe_free(password);
888         crypt_safe_free(vk);
889         return r;
890 }
891
892 static int action_luksDump(int arg)
893 {
894         struct crypt_device *cd = NULL;
895         int r;
896
897         if ((r = crypt_init(&cd, action_argv[0])))
898                 goto out;
899
900         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
901                 goto out;
902
903         if (opt_dump_master_key)
904                 r = luksDump_with_volume_key(cd);
905         else
906                 r = crypt_dump(cd);
907 out:
908         crypt_free(cd);
909         return r;
910 }
911
912 static int action_luksSuspend(int arg)
913 {
914         struct crypt_device *cd = NULL;
915         int r;
916
917         r = crypt_init_by_name(&cd, action_argv[0]);
918         if (!r)
919                 r = crypt_suspend(cd, action_argv[0]);
920
921         crypt_free(cd);
922         return r;
923 }
924
925 static int action_luksResume(int arg)
926 {
927         struct crypt_device *cd = NULL;
928         int r;
929
930         if ((r = crypt_init_by_name(&cd, action_argv[0])))
931                 goto out;
932
933         if ((r = crypt_load(cd, CRYPT_LUKS1, NULL)))
934                 goto out;
935
936         if (opt_key_file)
937                 r = crypt_resume_by_keyfile(cd, action_argv[0], CRYPT_ANY_SLOT,
938                                             opt_key_file, opt_keyfile_size);
939         else
940                 r = crypt_resume_by_passphrase(cd, action_argv[0], CRYPT_ANY_SLOT,
941                                                NULL, 0);
942 out:
943         crypt_free(cd);
944         return r;
945 }
946
947 static int action_luksBackup(int arg)
948 {
949         struct crypt_device *cd = NULL;
950         int r;
951
952         if (!opt_header_backup_file) {
953                 log_err(_("Option --header-backup-file is required.\n"));
954                 return -EINVAL;
955         }
956
957         if ((r = crypt_init(&cd, action_argv[0])))
958                 goto out;
959
960         crypt_set_confirm_callback(cd, _yesDialog, NULL);
961
962         r = crypt_header_backup(cd, CRYPT_LUKS1, opt_header_backup_file);
963 out:
964         crypt_free(cd);
965         return r;
966 }
967
968 static int action_luksRestore(int arg)
969 {
970         struct crypt_device *cd = NULL;
971         int r = 0;
972
973         if (!opt_header_backup_file) {
974                 log_err(_("Option --header-backup-file is required.\n"));
975                 return -EINVAL;
976         }
977
978         if ((r = crypt_init(&cd, action_argv[0])))
979                 goto out;
980
981         crypt_set_confirm_callback(cd, _yesDialog, NULL);
982         r = crypt_header_restore(cd, CRYPT_LUKS1, opt_header_backup_file);
983 out:
984         crypt_free(cd);
985         return r;
986 }
987
988 static __attribute__ ((noreturn)) void usage(poptContext popt_context,
989                                              int exitcode, const char *error,
990                                              const char *more)
991 {
992         poptPrintUsage(popt_context, stderr, 0);
993         if (error)
994                 log_err("%s: %s\n", more, error);
995         exit(exitcode);
996 }
997
998 static void help(poptContext popt_context, enum poptCallbackReason reason,
999                  struct poptOption *key, const char * arg, void *data)
1000 {
1001         if (key->shortName == '?') {
1002                 struct action_type *action;
1003
1004                 log_std("%s\n",PACKAGE_STRING);
1005
1006                 poptPrintHelp(popt_context, stdout, 0);
1007
1008                 log_std(_("\n"
1009                          "<action> is one of:\n"));
1010
1011                 for(action = action_types; action->type; action++)
1012                         log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
1013
1014                 log_std(_("\n"
1015                          "<name> is the device to create under %s\n"
1016                          "<device> is the encrypted device\n"
1017                          "<key slot> is the LUKS key slot number to modify\n"
1018                          "<key file> optional key file for the new key for luksAddKey action\n"),
1019                         crypt_get_dir());
1020
1021                 log_std(_("\nDefault compiled-in keyfile parameters:\n"
1022                          "\tMaximum keyfile size: %dkB, "
1023                          "Maximum interactive passphrase length %d (characters)\n"),
1024                          DEFAULT_KEYFILE_SIZE_MAXKB, DEFAULT_PASSPHRASE_SIZE_MAX);
1025
1026                 log_std(_("\nDefault compiled-in device cipher parameters:\n"
1027                          "\tloop-AES: %s, Key %d bits\n"
1028                          "\tplain: %s, Key: %d bits, Password hashing: %s\n"
1029                          "\tLUKS1: %s, Key: %d bits, LUKS header hashing: %s, RNG: %s\n"),
1030                          DEFAULT_LOOPAES_CIPHER, DEFAULT_LOOPAES_KEYBITS,
1031                          DEFAULT_CIPHER(PLAIN), DEFAULT_PLAIN_KEYBITS, DEFAULT_PLAIN_HASH,
1032                          DEFAULT_CIPHER(LUKS1), DEFAULT_LUKS1_KEYBITS, DEFAULT_LUKS1_HASH,
1033                          DEFAULT_RNG);
1034                 exit(EXIT_SUCCESS);
1035         } else
1036                 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
1037 }
1038
1039 static void _dbg_version_and_cmd(int argc, char **argv)
1040 {
1041         int i;
1042
1043         log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
1044         for (i = 0; i < argc; i++) {
1045                 if (i)
1046                         log_std(" ");
1047                 log_std(argv[i]);
1048         }
1049         log_std("\"\n");
1050 }
1051
1052 static int run_action(struct action_type *action)
1053 {
1054         int r;
1055
1056         log_dbg("Running command %s.", action->type);
1057
1058         if (action->required_memlock)
1059                 crypt_memory_lock(NULL, 1);
1060
1061         r = action->handler(action->arg);
1062
1063         if (action->required_memlock)
1064                 crypt_memory_lock(NULL, 0);
1065
1066         /* Some functions returns keyslot # */
1067         if (r > 0)
1068                 r = 0;
1069
1070         show_status(r);
1071
1072         /* Translate exit code to simple codes */
1073         switch (r) {
1074         case 0:         r = EXIT_SUCCESS; break;
1075         case -EEXIST:
1076         case -EBUSY:    r = 5; break;
1077         case -ENOTBLK:
1078         case -ENODEV:   r = 4; break;
1079         case -ENOMEM:   r = 3; break;
1080         case -EPERM:    r = 2; break;
1081         case -EINVAL:
1082         case -ENOENT:
1083         case -ENOSYS:
1084         default:        r = EXIT_FAILURE;
1085         }
1086         return r;
1087 }
1088
1089 int main(int argc, char **argv)
1090 {
1091         static char *popt_tmp;
1092         static struct poptOption popt_help_options[] = {
1093                 { NULL,    '\0', POPT_ARG_CALLBACK, help, 0, NULL,                         NULL },
1094                 { "help",  '?',  POPT_ARG_NONE,     NULL, 0, N_("Show this help message"), NULL },
1095                 { "usage", '\0', POPT_ARG_NONE,     NULL, 0, N_("Display brief usage"),    NULL },
1096                 POPT_TABLEEND
1097         };
1098         static struct poptOption popt_options[] = {
1099                 { NULL,                '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
1100                 { "version",           '\0', POPT_ARG_NONE, &opt_version_mode,          0, N_("Print package version"), NULL },
1101                 { "verbose",           'v',  POPT_ARG_NONE, &opt_verbose,               0, N_("Shows more detailed error messages"), NULL },
1102                 { "debug",             '\0', POPT_ARG_NONE, &opt_debug,                 0, N_("Show debug messages"), NULL },
1103                 { "cipher",            'c',  POPT_ARG_STRING, &opt_cipher,              0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
1104                 { "hash",              'h',  POPT_ARG_STRING, &opt_hash,                0, N_("The hash used to create the encryption key from the passphrase"), NULL },
1105                 { "verify-passphrase", 'y',  POPT_ARG_NONE, &opt_verify_passphrase,     0, N_("Verifies the passphrase by asking for it twice"), NULL },
1106                 { "key-file",          'd',  POPT_ARG_STRING, &opt_key_file,            0, N_("Read the key from a file."), NULL },
1107                 { "master-key-file",  '\0',  POPT_ARG_STRING, &opt_master_key_file,     0, N_("Read the volume (master) key from file."), NULL },
1108                 { "dump-master-key",  '\0',  POPT_ARG_NONE, &opt_dump_master_key,       0, N_("Dump volume (master) key instead of keyslots info."), NULL },
1109                 { "key-size",          's',  POPT_ARG_INT, &opt_key_size,               0, N_("The size of the encryption key"), N_("BITS") },
1110                 { "keyfile-size",      'l',  POPT_ARG_LONG, &opt_keyfile_size,          0, N_("Limits the read from keyfile"), N_("bytes") },
1111                 { "new-keyfile-size", '\0',  POPT_ARG_LONG, &opt_new_keyfile_size,      0, N_("Limits the read from newly added keyfile"), N_("bytes") },
1112                 { "key-slot",          'S',  POPT_ARG_INT, &opt_key_slot,               0, N_("Slot number for new key (default is first free)"), NULL },
1113                 { "size",              'b',  POPT_ARG_STRING, &popt_tmp,                1, N_("The size of the device"), N_("SECTORS") },
1114                 { "offset",            'o',  POPT_ARG_STRING, &popt_tmp,                2, N_("The start offset in the backend device"), N_("SECTORS") },
1115                 { "skip",              'p',  POPT_ARG_STRING, &popt_tmp,                3, N_("How many sectors of the encrypted data to skip at the beginning"), N_("SECTORS") },
1116                 { "readonly",          'r',  POPT_ARG_NONE, &opt_readonly,              0, N_("Create a readonly mapping"), NULL },
1117                 { "iter-time",         'i',  POPT_ARG_INT, &opt_iteration_time,         0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
1118                 { "batch-mode",        'q',  POPT_ARG_NONE, &opt_batch_mode,            0, N_("Do not ask for confirmation"), NULL },
1119                 { "timeout",           't',  POPT_ARG_INT, &opt_timeout,                0, N_("Timeout for interactive passphrase prompt (in seconds)"), N_("secs") },
1120                 { "tries",             'T',  POPT_ARG_INT, &opt_tries,                  0, N_("How often the input of the passphrase can be retried"), NULL },
1121                 { "align-payload",     '\0', POPT_ARG_INT, &opt_align_payload,          0, N_("Align payload at <n> sector boundaries - for luksFormat"), N_("SECTORS") },
1122                 { "header-backup-file",'\0', POPT_ARG_STRING, &opt_header_backup_file,  0, N_("File with LUKS header and keyslots backup."), NULL },
1123                 { "use-random",        '\0', POPT_ARG_NONE, &opt_random,                0, N_("Use /dev/random for generating volume key."), NULL },
1124                 { "use-urandom",       '\0', POPT_ARG_NONE, &opt_urandom,               0, N_("Use /dev/urandom for generating volume key."), NULL },
1125                 { "uuid",              '\0',  POPT_ARG_STRING, &opt_uuid,               0, N_("UUID for device to use."), NULL },
1126                 POPT_TABLEEND
1127         };
1128         poptContext popt_context;
1129         struct action_type *action;
1130         char *aname;
1131         int r;
1132         const char *null_action_argv[] = {NULL};
1133
1134         crypt_set_log_callback(NULL, _log, NULL);
1135
1136         setlocale(LC_ALL, "");
1137         bindtextdomain(PACKAGE, LOCALEDIR);
1138         textdomain(PACKAGE);
1139
1140         popt_context = poptGetContext(PACKAGE, argc, (const char **)argv,
1141                                       popt_options, 0);
1142         poptSetOtherOptionHelp(popt_context,
1143                                N_("[OPTION...] <action> <action-specific>]"));
1144
1145         while((r = poptGetNextOpt(popt_context)) > 0) {
1146                 unsigned long long ull_value;
1147                 char *endp;
1148
1149                 ull_value = strtoull(popt_tmp, &endp, 0);
1150                 if (*endp || !*popt_tmp)
1151                         r = POPT_ERROR_BADNUMBER;
1152
1153                 switch(r) {
1154                         case 1:
1155                                 opt_size = ull_value;
1156                                 break;
1157                         case 2:
1158                                 opt_offset = ull_value;
1159                                 break;
1160                         case 3:
1161                                 opt_skip = ull_value;
1162                                 opt_skip_valid = 1;
1163                                 break;
1164                 }
1165
1166                 if (r < 0)
1167                         break;
1168         }
1169
1170         if (r < -1)
1171                 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
1172                       poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
1173         if (opt_version_mode) {
1174                 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1175                 exit(EXIT_SUCCESS);
1176         }
1177
1178         if (!(aname = (char *)poptGetArg(popt_context)))
1179                 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
1180                       poptGetInvocationName(popt_context));
1181         for(action = action_types; action->type; action++)
1182                 if (strcmp(action->type, aname) == 0)
1183                         break;
1184         if (!action->type)
1185                 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
1186                       poptGetInvocationName(popt_context));
1187
1188         action_argc = 0;
1189         action_argv = poptGetArgs(popt_context);
1190         /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
1191         if(!action_argv)
1192                 action_argv = null_action_argv;
1193
1194         /* Count args, somewhat unnice, change? */
1195         while(action_argv[action_argc] != NULL)
1196                 action_argc++;
1197
1198         if(action_argc < action->required_action_argc) {
1199                 char buf[128];
1200                 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
1201                 usage(popt_context, EXIT_FAILURE, buf,
1202                       poptGetInvocationName(popt_context));
1203         }
1204
1205         /* FIXME: rewrite this from scratch */
1206
1207         if (opt_key_size &&
1208            strcmp(aname, "luksFormat") &&
1209            strcmp(aname, "create") &&
1210            strcmp(aname, "loopaesOpen")) {
1211                 usage(popt_context, EXIT_FAILURE,
1212                       _("Option --key-size is allowed only for luksFormat, create and loopaesOpen.\n"
1213                         "To limit read from keyfile use --keyfile-size=(bytes)."),
1214                       poptGetInvocationName(popt_context));
1215         }
1216
1217         if (opt_key_size % 8)
1218                 usage(popt_context, EXIT_FAILURE,
1219                       _("Key size must be a multiple of 8 bits"),
1220                       poptGetInvocationName(popt_context));
1221
1222         if (!strcmp(aname, "luksKillSlot") && action_argc > 1)
1223                 opt_key_slot = atoi(action_argv[1]);
1224         if (opt_key_slot != CRYPT_ANY_SLOT &&
1225             (opt_key_slot < 0 || opt_key_slot > crypt_keyslot_max(CRYPT_LUKS1)))
1226                 usage(popt_context, EXIT_FAILURE, _("Key slot is invalid."),
1227                       poptGetInvocationName(popt_context));
1228
1229         if ((!strcmp(aname, "luksRemoveKey") ||
1230              !strcmp(aname, "luksFormat")) &&
1231              action_argc > 1) {
1232                 if (opt_key_file)
1233                         log_err(_("Option --key-file takes precedence over specified key file argument.\n"));
1234                 else
1235                         opt_key_file = (char*)action_argv[1];
1236         }
1237
1238         if (opt_keyfile_size < 0 || opt_new_keyfile_size < 0 || opt_key_size < 0) {
1239                 usage(popt_context, EXIT_FAILURE,
1240                       _("Negative number for option not permitted."),
1241                       poptGetInvocationName(popt_context));
1242         }
1243
1244         if (opt_random && opt_urandom)
1245                 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
1246                       poptGetInvocationName(popt_context));
1247         if ((opt_random || opt_urandom) && strcmp(aname, "luksFormat"))
1248                 usage(popt_context, EXIT_FAILURE, _("Option --use-[u]random is allowed only for luksFormat."),
1249                       poptGetInvocationName(popt_context));
1250
1251         if (opt_uuid && strcmp(aname, "luksFormat") && strcmp(aname, "luksUUID"))
1252                 usage(popt_context, EXIT_FAILURE, _("Option --uuid is allowed only for luksFormat and luksUUID."),
1253                       poptGetInvocationName(popt_context));
1254
1255         if (opt_skip && strcmp(aname, "create") && strcmp(aname, "loopaesOpen"))
1256                 usage(popt_context, EXIT_FAILURE,
1257                 _("Option --skip is supported only for create and loopaesOpen commands.\n"),
1258                 poptGetInvocationName(popt_context));
1259
1260         if (opt_offset && strcmp(aname, "create") && strcmp(aname, "loopaesOpen"))
1261                 usage(popt_context, EXIT_FAILURE,
1262                 _("Option --offset is supported only for create and loopaesOpen commands.\n"),
1263                 poptGetInvocationName(popt_context));
1264
1265         if (opt_debug) {
1266                 opt_verbose = 1;
1267                 crypt_set_debug_level(-1);
1268                 _dbg_version_and_cmd(argc, argv);
1269         }
1270
1271         return run_action(action);
1272 }