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