2 * crypt_reencrypt - crypt utility for offline reencryption
4 * Copyright (C) 2012 Milan Broz All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* The code works as follows:
21 * - create backup (detached) headers fo old and new device
22 * - mark original device unusable
23 * - maps two devices, one with old header one with new onto
24 * the _same_ underlying device
25 * - with direct-io reads old device and copy to new device in defined steps
26 * - keps simple off in file (allows restart)
27 * - there is several windows when corruption can happen
30 * dmsetup create x --table "0 $(blockdev --getsz DEV) crypt cipher_null-ecb-null - 0 DEV 0"
32 #define _LARGEFILE64_SOURCE
33 #define _FILE_OFFSET_BITS 64
44 #include <sys/ioctl.h>
50 #include <libcryptsetup.h>
53 #include "cryptsetup.h"
55 static int opt_verbose = 0;
56 static int opt_debug = 0;
57 static const char *opt_cipher = NULL;
58 static const char *opt_hash = NULL;
59 static const char *opt_key_file = NULL;
60 static int opt_iteration_time = 1000;
61 static int opt_batch_mode = 0;
62 static int opt_version_mode = 0;
63 static int opt_random = 0;
64 static int opt_urandom = 0;
65 static int opt_bsize = 4;
66 static int opt_new = 0;
67 static int opt_directio = 0;
68 static int opt_write_log = 0;
69 static const char *opt_new_file = NULL;
71 static const char **action_argv;
73 static volatile int quit = 0;
79 uint64_t device_offset;
80 uint64_t device_shift;
83 enum { FORWARD = 0, BACKWARD = 1 } reencrypt_direction;
85 char header_file_org[PATH_MAX];
86 char header_file_new[PATH_MAX];
87 char log_file[PATH_MAX];
89 char crypt_path_org[PATH_MAX];
90 char crypt_path_new[PATH_MAX];
97 struct timeval start_time, end_time;
98 uint64_t restart_bytest;
101 char MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
102 char NOMAGIC[] = {'L','U','K','S', 0xde, 0xad};
111 __attribute__((format(printf, 5, 6)))
112 static void clogger(struct crypt_device *cd, int level, const char *file,
113 int line, const char *format, ...)
118 va_start(argp, format);
120 if (vasprintf(&target, format, argp) > 0) {
122 crypt_log(cd, level, target);
123 } else if (opt_debug)
124 printf("# %s\n", target);
131 static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
135 case CRYPT_LOG_NORMAL:
138 case CRYPT_LOG_VERBOSE:
142 case CRYPT_LOG_ERROR:
145 case CRYPT_LOG_DEBUG:
147 printf("# %s\n", msg);
150 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
155 static void _quiet_log(int level, const char *msg, void *usrptr)
157 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
158 level = CRYPT_LOG_VERBOSE;
159 _log(level, msg, usrptr);
162 static void int_handler(int sig __attribute__((__unused__)))
167 static void set_int_block(int block)
169 sigset_t signals_open;
171 sigemptyset(&signals_open);
172 sigaddset(&signals_open, SIGINT);
173 sigaddset(&signals_open, SIGTERM);
174 sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
177 static void set_int_handler(void)
179 struct sigaction sigaction_open;
181 memset(&sigaction_open, 0, sizeof(struct sigaction));
182 sigaction_open.sa_handler = int_handler;
183 sigaction(SIGINT, &sigaction_open, 0);
184 sigaction(SIGTERM, &sigaction_open, 0);
188 /* The difference in seconds between two times in "timeval" format. */
189 double time_diff(struct timeval start, struct timeval end)
191 return (end.tv_sec - start.tv_sec)
192 + (end.tv_usec - start.tv_usec) / 1E6;
195 static int alignment(int fd)
199 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
205 static int device_magic(header_magic set_magic)
208 size_t block_size = 512;
212 devfd = open(rnc.device, O_RDWR | O_DIRECT);
214 return errno == EBUSY ? -EBUSY : -EINVAL;
216 if (posix_memalign((void *)&buf, alignment(devfd), block_size)) {
221 s = read(devfd, buf, block_size);
222 if (s < 0 || s != block_size) {
223 log_verbose(_("Cannot read device %s.\n"), rnc.device);
228 if (set_magic == MAKE_UNUSABLE && !memcmp(buf, MAGIC, MAGIC_L)) {
229 log_dbg("Marking LUKS device %s unusable.", rnc.device);
230 memcpy(buf, NOMAGIC, MAGIC_L);
233 } else if (set_magic == MAKE_USABLE && !memcmp(buf, NOMAGIC, MAGIC_L)) {
234 log_dbg("Marking LUKS device %s usable.", rnc.device);
235 memcpy(buf, MAGIC, MAGIC_L);
237 } else if (set_magic == CHECK_UNUSABLE) {
238 r = memcmp(buf, NOMAGIC, MAGIC_L) ? -EINVAL : 0;
240 rnc.device_uuid = strndup(&buf[0xa8], 40);
246 if (lseek(devfd, 0, SEEK_SET) == -1)
248 s = write(devfd, buf, block_size);
249 if (s < 0 || s != block_size) {
250 log_verbose(_("Cannot write device %s.\n"), rnc.device);
254 log_dbg("LUKS signature check failed for %s.", rnc.device);
257 memset(buf, 0, block_size);
263 static int create_empty_header(const char *new_file, const char *old_file)
270 if (stat(old_file, &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG)
274 log_dbg("Creating empty file %s of size %lu.", new_file, (unsigned long)size);
276 if (!(buf = malloc(size)))
278 memset(buf, 0, size);
280 fd = creat(new_file, S_IRUSR|S_IWUSR);
286 if (write(fd, buf, size) < size)
294 static int write_log(void)
296 static char buf[512];
299 //log_dbg("Updating LUKS reencryption log offset %" PRIu64 ".", offset);
300 memset(buf, 0, sizeof(buf));
301 snprintf(buf, sizeof(buf), "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
302 "version = %d\nUUID = %s\ndirection = %d\n"
303 "offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
304 1, rnc.device_uuid, rnc.reencrypt_direction,
305 rnc.device_offset, rnc.device_shift);
307 lseek(rnc.log_fd, 0, SEEK_SET);
308 r = write(rnc.log_fd, buf, sizeof(buf));
309 if (r < 0 || r != sizeof(buf))
315 static int parse_line_log(const char *line)
325 if (sscanf(line, "version = %d", &i) == 1) {
327 log_dbg("Log: Unexpected version = %i", i);
330 } else if (sscanf(line, "UUID = %40s", s) == 1) {
331 if (!rnc.device_uuid || strcmp(rnc.device_uuid, s)) {
332 log_dbg("Log: Unexpected UUID %s", s);
335 } else if (sscanf(line, "direction = %d", &i) == 1) {
336 log_dbg("Log: direction = %i", i);
337 rnc.reencrypt_direction = i;
338 } else if (sscanf(line, "offset = %" PRIu64, &u64) == 1) {
339 log_dbg("Log: offset = %" PRIu64, u64);
340 rnc.device_offset = u64;
341 } else if (sscanf(line, "shift = %" PRIu64, &u64) == 1) {
342 log_dbg("Log: shift = %" PRIu64, u64);
343 rnc.device_shift = u64;
350 static int parse_log(void)
352 static char buf[512];
356 s = read(rnc.log_fd, buf, sizeof(buf));
363 end = strchr(start, '\n');
366 if (parse_line_log(start)) {
367 log_err("Wrong log format.\n");
378 static int open_log(void)
383 if(stat(rnc.log_file, &st) < 0) {
384 log_dbg("Creating LUKS reencryption log file %s.", rnc.log_file);
386 // FIXME: move that somewhere else
387 rnc.reencrypt_direction = BACKWARD;
389 flags = opt_directio ? O_RDWR|O_CREAT|O_DIRECT : O_RDWR|O_CREAT;
390 rnc.log_fd = open(rnc.log_file, flags, S_IRUSR|S_IWUSR);
391 if (rnc.log_fd == -1)
396 log_dbg("Log file %s exists, restarting.", rnc.log_file);
397 flags = opt_directio ? O_RDWR|O_DIRECT : O_RDWR;
398 rnc.log_fd = open(rnc.log_file, flags);
399 if (rnc.log_fd == -1)
404 /* Be sure it is correct format */
408 static void close_log(void)
410 log_dbg("Closing LUKS reencryption log file %s.", rnc.log_file);
411 if (rnc.log_fd != -1)
415 static int activate_luks_headers(void)
417 struct crypt_device *cd = NULL, *cd_new = NULL;
420 log_dbg("Activating LUKS devices from headers.");
422 if ((r = crypt_init(&cd, rnc.header_file_org)) ||
423 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
424 (r = crypt_set_data_device(cd, rnc.device)))
427 if ((r = crypt_activate_by_passphrase(cd, rnc.header_file_org,
428 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
429 CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_PRIVATE)) < 0)
432 if ((r = crypt_init(&cd_new, rnc.header_file_new)) ||
433 (r = crypt_load(cd_new, CRYPT_LUKS1, NULL)) ||
434 (r = crypt_set_data_device(cd_new, rnc.device)))
437 if ((r = crypt_activate_by_passphrase(cd_new, rnc.header_file_new,
438 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
439 CRYPT_ACTIVATE_SHARED|CRYPT_ACTIVATE_PRIVATE)) < 0)
447 static int backup_luks_headers(void)
449 struct crypt_device *cd = NULL, *cd_new = NULL;
450 struct crypt_params_luks1 params = {0};
451 char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
454 log_dbg("Creating LUKS header backup for device %s.", rnc.device);
455 if ((r = crypt_init(&cd, rnc.device)) ||
456 (r = crypt_load(cd, CRYPT_LUKS1, NULL)))
459 crypt_set_confirm_callback(cd, NULL, NULL);
460 if ((r = crypt_header_backup(cd, CRYPT_LUKS1, rnc.header_file_org)))
463 if ((r = create_empty_header(rnc.header_file_new, rnc.header_file_org)))
466 params.hash = opt_hash ?: DEFAULT_LUKS1_HASH;
467 params.data_alignment = crypt_get_data_offset(cd);
468 params.data_device = rnc.device;
470 if ((r = crypt_init(&cd_new, rnc.header_file_new)))
474 crypt_set_rng_type(cd_new, CRYPT_RNG_RANDOM);
475 else if (opt_urandom)
476 crypt_set_rng_type(cd_new, CRYPT_RNG_URANDOM);
478 if (opt_iteration_time)
479 crypt_set_iteration_time(cd_new, opt_iteration_time);
482 r = crypt_parse_name_and_mode(opt_cipher, cipher, NULL, cipher_mode);
484 log_err(_("No known cipher specification pattern detected.\n"));
489 if ((r = crypt_format(cd_new, CRYPT_LUKS1,
490 opt_cipher ? cipher : crypt_get_cipher(cd),
491 opt_cipher ? cipher_mode : crypt_get_cipher_mode(cd),
493 NULL, crypt_get_volume_key_size(cd), ¶ms)))
496 if ((r = crypt_keyslot_add_by_volume_key(cd_new, rnc.keyslot,
497 NULL, 0, rnc.password, rnc.passwordLen)) < 0)
506 static void remove_headers(void)
508 struct crypt_device *cd = NULL;
510 if (crypt_init(&cd, NULL))
512 crypt_set_log_callback(cd, _quiet_log, NULL);
513 (void)crypt_deactivate(cd, rnc.header_file_org);
514 (void)crypt_deactivate(cd, rnc.header_file_new);
518 static int restore_luks_header(const char *backup)
520 struct crypt_device *cd = NULL;
523 r = crypt_init(&cd, rnc.device);
526 crypt_set_confirm_callback(cd, NULL, NULL);
527 r = crypt_header_restore(cd, CRYPT_LUKS1, backup);
534 void print_progress(uint64_t bytes, int final)
536 uint64_t mbytes = (bytes - rnc.restart_bytest) / 1024 / 1024;
537 struct timeval now_time;
540 gettimeofday(&now_time, NULL);
541 if (!final && time_diff(rnc.end_time, now_time) < 0.5)
544 rnc.end_time = now_time;
549 tdiff = time_diff(rnc.start_time, rnc.end_time);
553 log_err("\33[2K\rProgress: %5.1f%%, time elapsed %3.1f seconds, %4"
554 PRIu64 " MB written, speed %5.2f MB/s%s",
555 (double)bytes / rnc.device_size * 100,
556 time_diff(rnc.start_time, rnc.end_time),
557 mbytes, (double)(mbytes) / tdiff,
561 static int copy_data_forward(int fd_old, int fd_new, size_t block_size, void *buf, uint64_t *bytes)
565 rnc.restart_bytest = *bytes = rnc.device_offset;
566 while (!quit && rnc.device_offset < rnc.device_size) {
567 s1 = read(fd_old, buf, block_size);
568 if (s1 < 0 || (s1 != block_size && (rnc.device_offset + s1) != rnc.device_size)) {
569 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
572 s2 = write(fd_new, buf, s1);
574 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
577 rnc.device_offset += s1;
578 if (opt_write_log && write_log() < 0) {
579 log_err("Log write error, some data are perhaps lost.\n");
583 *bytes += (uint64_t)s2;
584 print_progress(*bytes, 0);
587 return quit ? -EAGAIN : 0;
590 static int copy_data_backward(int fd_old, int fd_new, size_t block_size, void *buf, uint64_t *bytes)
592 ssize_t s1, s2, working_block;
593 off64_t working_offset;
595 rnc.restart_bytest = *bytes = rnc.device_size - rnc.device_offset;
596 while (!quit && rnc.device_offset) {
597 if (rnc.device_offset < block_size) {
599 working_block = rnc.device_offset;
601 working_offset = rnc.device_offset - block_size;
602 working_block = block_size;
605 if (lseek64(fd_old, working_offset, SEEK_SET) < 0 ||
606 lseek64(fd_new, working_offset, SEEK_SET) < 0) {
607 log_err("Cannot seek to device offset.\n");
611 s1 = read(fd_old, buf, working_block);
612 if (s1 < 0 || (s1 != working_block)) {
613 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
616 s2 = write(fd_new, buf, working_block);
618 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
621 rnc.device_offset -= s1;
622 if (opt_write_log && write_log() < 0) {
623 log_err("Log write error, some data are perhaps lost.\n");
627 *bytes += (uint64_t)s2;
628 print_progress(*bytes, 0);
631 return quit ? -EAGAIN : 0;
634 static int copy_data(void)
636 size_t block_size = opt_bsize * 1024 * 1024;
637 int fd_old = -1, fd_new = -1;
642 fd_old = open(rnc.crypt_path_org, O_RDONLY | (opt_directio ? O_DIRECT : 0));
646 fd_new = open(rnc.crypt_path_new, O_WRONLY | (opt_directio ? O_DIRECT : 0));
650 if (lseek(fd_old, rnc.device_offset, SEEK_SET) == -1)
653 if (lseek(fd_new, rnc.device_offset, SEEK_SET) == -1)
657 if (ioctl(fd_old, BLKGETSIZE64, &rnc.device_size) < 0)
660 if (posix_memalign((void *)&buf, alignment(fd_new), block_size)) {
666 // FIXME: all this should be in init
667 if (!rnc.in_progress && rnc.reencrypt_direction == BACKWARD)
668 rnc.device_offset = rnc.device_size;
670 gettimeofday(&rnc.start_time, NULL);
672 if (rnc.reencrypt_direction == FORWARD)
673 r = copy_data_forward(fd_old, fd_new, block_size, buf, &bytes);
675 r = copy_data_backward(fd_old, fd_new, block_size, buf, &bytes);
678 print_progress(bytes, 1);
681 log_err("ERROR during reencryption.\n");
684 log_err("Log write error, ignored.\n");
695 static int initialize_uuid(void)
697 struct crypt_device *cd = NULL;
700 /* Try to load LUKS from device */
701 if ((r = crypt_init(&cd, rnc.device)))
703 crypt_set_log_callback(cd, _quiet_log, NULL);
704 r = crypt_load(cd, CRYPT_LUKS1, NULL);
706 rnc.device_uuid = strdup(crypt_get_uuid(cd));
708 /* Reencryption already in progress - magic header? */
709 r = device_magic(CHECK_UNUSABLE);
715 static int initialize_passphrase(const char *device)
717 struct crypt_device *cd = NULL;
720 if ((r = crypt_init(&cd, device)) ||
721 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
722 (r = crypt_set_data_device(cd, rnc.device)))
725 if ((r = crypt_get_key(_("Enter LUKS passphrase: "),
726 &rnc.password, &rnc.passwordLen,
731 if ((r = crypt_activate_by_passphrase(cd, NULL,
732 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen, 0) < 0))
744 static int initialize_context(const char *device)
746 log_dbg("Initialising reencryption context.");
750 if (!(rnc.device = strndup(device, PATH_MAX)))
753 if (opt_new_file && !create_uuid()) {
754 log_err("Cannot create fake header.\n");
758 if (initialize_uuid()) {
759 log_err("No header found on device.\n");
763 /* Prepare device names */
764 if (snprintf(rnc.log_file, PATH_MAX,
765 "LUKS-%s.log", rnc.device_uuid) < 0)
767 if (snprintf(rnc.header_file_org, PATH_MAX,
768 "LUKS-%s.org", rnc.device_uuid) < 0)
770 if (snprintf(rnc.header_file_new, PATH_MAX,
771 "LUKS-%s.new", rnc.device_uuid) < 0)
774 /* Paths to encrypted devices */
775 if (snprintf(rnc.crypt_path_org, PATH_MAX,
776 "%s/%s", crypt_get_dir(), rnc.header_file_org) < 0)
778 if (snprintf(rnc.crypt_path_new, PATH_MAX,
779 "%s/%s", crypt_get_dir(), rnc.header_file_new) < 0)
787 static void destroy_context(void)
789 log_dbg("Destroying reencryption context.");
794 if ((rnc.reencrypt_direction == FORWARD &&
795 rnc.device_offset == rnc.device_size) ||
796 rnc.device_offset == 0) {
797 unlink(rnc.log_file);
798 unlink(rnc.header_file_org);
799 unlink(rnc.header_file_new);
802 crypt_safe_free(rnc.password);
805 free(rnc.device_uuid);
808 int run_reencrypt(const char *device)
811 if (initialize_context(device))
814 log_dbg("Running reencryption.");
816 if (!rnc.in_progress) {
817 if ((r = initialize_passphrase(rnc.device)) ||
818 (r = backup_luks_headers()) ||
819 (r = device_magic(MAKE_UNUSABLE)))
822 if ((r = initialize_passphrase(rnc.header_file_org)))
826 if ((r = activate_luks_headers()))
829 if ((r = copy_data()))
832 r = restore_luks_header(rnc.header_file_new);
838 static __attribute__ ((noreturn)) void usage(poptContext popt_context,
839 int exitcode, const char *error,
842 poptPrintUsage(popt_context, stderr, 0);
844 log_err("%s: %s\n", more, error);
845 poptFreeContext(popt_context);
849 static void help(poptContext popt_context,
850 enum poptCallbackReason reason __attribute__((unused)),
851 struct poptOption *key,
852 const char *arg __attribute__((unused)),
853 void *data __attribute__((unused)))
855 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
858 static void _dbg_version_and_cmd(int argc, const char **argv)
862 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
863 for (i = 0; i < argc; i++) {
866 log_std("%s", argv[i]);
871 int main(int argc, const char **argv)
873 static struct poptOption popt_help_options[] = {
874 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
875 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
876 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
879 static struct poptOption popt_options[] = {
880 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
881 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
882 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
883 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
884 { "block-size", 'B', POPT_ARG_INT, &opt_bsize, 0, N_("Reencryption block size"), N_("MB") },
885 { "new-header", 'N', POPT_ARG_INT, &opt_new, 0, N_("Create new header, need size on the end of device"), N_("MB") },
886 { "new-crypt", 'f', POPT_ARG_STRING, &opt_new_file, 0, N_("Log suffix for new reencryption file."), NULL },
887 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
888 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
889 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file."), NULL },
890 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
891 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
892 { "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
893 { "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
894 { "use-directio", '\0', POPT_ARG_NONE, &opt_directio, 0, N_("Use direct-io when accesing devices."), NULL },
895 { "write-log", '\0', POPT_ARG_NONE, &opt_write_log, 0, N_("Update log file after every block."), NULL },
898 poptContext popt_context;
901 crypt_set_log_callback(NULL, _log, NULL);
902 log_err("WARNING: this is experimental code, it can completely break your data.\n");
906 setlocale(LC_ALL, "");
907 bindtextdomain(PACKAGE, LOCALEDIR);
910 popt_context = poptGetContext(PACKAGE, argc, argv, popt_options, 0);
911 poptSetOtherOptionHelp(popt_context,
912 N_("[OPTION...] <device>]"));
914 while((r = poptGetNextOpt(popt_context)) > 0) {
920 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
921 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
922 if (opt_version_mode) {
923 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
924 poptFreeContext(popt_context);
928 action_argv = poptGetArgs(popt_context);
930 usage(popt_context, EXIT_FAILURE, _("Argument required."),
931 poptGetInvocationName(popt_context));
933 if (opt_random && opt_urandom)
934 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
935 poptGetInvocationName(popt_context));
937 if (opt_new && !opt_new_file)
938 usage(popt_context, EXIT_FAILURE, _("You have to use -f with -N."),
939 poptGetInvocationName(popt_context));
943 crypt_set_debug_level(-1);
944 _dbg_version_and_cmd(argc, argv);
947 r = run_reencrypt(action_argv[0]);
949 poptFreeContext(popt_context);
951 /* Translate exit code to simple codes */
953 case 0: r = EXIT_SUCCESS; break;
955 case -EBUSY: r = 5; break;
957 case -ENODEV: r = 4; break;
958 case -ENOMEM: r = 3; break;
959 case -EPERM: r = 2; break;
960 case -EAGAIN: log_err(_("Interrupted by a signal.\n"));
964 default: r = EXIT_FAILURE;