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