diff --git a/src/cryptsetup.c b/src/cryptsetup.c index af268e43..3bc0cd26 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -40,6 +40,8 @@ static int action_argc; static const char *null_action_argv[] = {NULL, NULL}; static int total_keyfiles = 0; +static struct tools_log_params log_parms; + void tools_cleanup(void) { tools_args_free(tool_core_args, ARRAY_SIZE(tool_core_args)); @@ -252,12 +254,12 @@ static int action_open_plain(void) /* Skip blkid scan when activating plain device with offset */ if (!ARG_UINT64(OPT_OFFSET_ID)) { /* Print all present signatures in read-only mode */ - r = tools_detect_signatures(action_argv[0], 0, &signatures); + r = tools_detect_signatures(action_argv[0], 0, &signatures, ARG_SET(OPT_BATCH_MODE_ID)); if (r < 0) goto out; } - if (signatures) { + if (signatures && !ARG_SET(OPT_BATCH_MODE_ID)) { r = asprintf(&msg, _("Detected device signature(s) on %s. Proceeding further may damage existing data."), action_argv[0]); if (r == -1) { r = -ENOMEM; @@ -544,7 +546,7 @@ static int tcryptDump_with_volume_key(struct crypt_device *cd) unsigned i; int r; - if (!yesDialog( + if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( _("Header dump with volume key is sensitive information\n" "which allows access to encrypted partition without passphrase.\n" "This dump should be always stored encrypted on safe place."), @@ -1132,15 +1134,15 @@ static int _do_luks2_reencrypt_recovery(struct crypt_device *cd) case CRYPT_REENCRYPT_NONE: /* fall through */ case CRYPT_REENCRYPT_CLEAN: - r = noDialog(_("Seems device does not require reencryption recovery.\n" - "Do you want to proceed anyway?"), NULL); - if (!r) + if (ARG_SET(OPT_BATCH_MODE_ID) || + !noDialog(_("Seems device does not require reencryption recovery.\n" + "Do you want to proceed anyway?"), NULL)) return 0; break; case CRYPT_REENCRYPT_CRASH: - r = yesDialog(_("Really proceed with LUKS2 reencryption recovery?"), - _("Operation aborted.\n")); - if (!r) + if (!ARG_SET(OPT_BATCH_MODE_ID) && + !yesDialog(_("Really proceed with LUKS2 reencryption recovery?"), + _("Operation aborted.\n"))) return -EINVAL; break; default: @@ -1178,21 +1180,23 @@ static int action_luksRepair(void) action_argv[0]))) goto out; - crypt_set_log_callback(cd, quiet_log, NULL); + crypt_set_log_callback(cd, quiet_log, &log_parms); r = crypt_load(cd, luksType(device_type), NULL); - crypt_set_log_callback(cd, tool_log, NULL); + crypt_set_log_callback(cd, tool_log, &log_parms); if (r == 0) { log_verbose(_("No known problems detected for LUKS header.")); goto skip_repair; } - r = tools_detect_signatures(action_argv[0], 1, NULL); + r = tools_detect_signatures(action_argv[0], 1, NULL, ARG_SET(OPT_BATCH_MODE_ID)); if (r < 0) goto out; - r = yesDialog(_("Really try to repair LUKS device header?"), - _("Operation aborted.\n")) ? 0 : -EINVAL; - if (r == 0) + if (!ARG_SET(OPT_BATCH_MODE_ID) && + !yesDialog(_("Really try to repair LUKS device header?"), + _("Operation aborted.\n"))) + r = -EINVAL; + else r = crypt_repair(cd, luksType(device_type), NULL); skip_repair: if (!r && crypt_get_type(cd) && !strcmp(crypt_get_type(cd), CRYPT_LUKS2)) @@ -1207,6 +1211,10 @@ static int _wipe_data_device(struct crypt_device *cd) char tmp_name[64], tmp_path[128], tmp_uuid[40]; uuid_t tmp_uuid_bin; int r; + struct tools_progress_params prog_parms = { + .frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID), + .batch_mode = ARG_SET(OPT_BATCH_MODE_ID) + }; if (!ARG_SET(OPT_BATCH_MODE_ID)) log_std(_("Wiping device to initialize integrity checksum.\n" @@ -1229,7 +1237,7 @@ static int _wipe_data_device(struct crypt_device *cd) /* Wipe the device */ set_int_handler(0); r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK, - 0, &tools_wipe_progress, NULL); + 0, &tools_wipe_progress, &prog_parms); if (crypt_deactivate(cd, tmp_name)) log_err(_("Cannot deactivate temporary device %s."), tmp_path); set_int_block(0); @@ -1357,11 +1365,11 @@ static int _luksFormat(struct crypt_device **r_cd, char **r_password, size_t *r_ } /* Print all present signatures in read-only mode */ - r = tools_detect_signatures(header_device, 0, &signatures); + r = tools_detect_signatures(header_device, 0, &signatures, ARG_SET(OPT_BATCH_MODE_ID)); if (r < 0) goto out; - if (!created) { + if (!created && !ARG_SET(OPT_BATCH_MODE_ID)) { r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), header_device); if (r == -1) { r = -ENOMEM; @@ -1567,7 +1575,7 @@ static int verify_keyslot(struct crypt_device *cd, int key_slot, crypt_keyslot_i int i, max, r; if (ki == CRYPT_SLOT_ACTIVE_LAST && !ARG_SET(OPT_BATCH_MODE_ID) && !key_file && - msg_last && !yesDialog(msg_last, msg_fail)) + msg_last && !ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog(msg_last, msg_fail)) return -EPERM; r = tools_get_key(msg_pass, &password, &passwordLen, @@ -1699,9 +1707,10 @@ static int action_luksRemoveKey(void) log_verbose(_("Keyslot %d is selected for deletion."), ARG_INT32(OPT_KEY_SLOT_ID)); if (crypt_keyslot_status(cd, ARG_INT32(OPT_KEY_SLOT_ID)) == CRYPT_SLOT_ACTIVE_LAST && + !ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog(_("This is the last keyslot. " - "Device will become unusable after purging this key."), - _("Operation aborted, the keyslot was NOT wiped.\n"))) { + "Device will become unusable after purging this key."), + _("Operation aborted, the keyslot was NOT wiped.\n"))) { r = -EPERM; goto out; } @@ -2011,7 +2020,7 @@ static int action_isLuks(void) if ((r = crypt_init(&cd, uuid_or_device_header(NULL)))) goto out; - crypt_set_log_callback(cd, quiet_log, NULL); + crypt_set_log_callback(cd, quiet_log, &log_parms); r = crypt_load(cd, luksType(device_type), NULL); out: crypt_free(cd); @@ -2027,7 +2036,8 @@ static int action_luksUUID(void) if ((r = crypt_init(&cd, uuid_or_device_header(NULL)))) goto out; - crypt_set_confirm_callback(cd, yesDialog, _("Operation aborted.\n")); + if (!ARG_SET(OPT_BATCH_MODE_ID)) + crypt_set_confirm_callback(cd, yesDialog, _("Operation aborted.\n")); if ((r = crypt_load(cd, luksType(device_type), NULL))) goto out; @@ -2052,7 +2062,7 @@ static int luksDump_with_volume_key(struct crypt_device *cd) unsigned i; int r; - if (!yesDialog( + if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( _("The header dump with volume key is sensitive information\n" "that allows access to encrypted partition without a passphrase.\n" "This dump should be stored encrypted in a safe place."), @@ -2122,7 +2132,7 @@ static int luksDump_with_unbound_key(struct crypt_device *cd) return -EINVAL; } - if (!yesDialog( + if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog( _("The header dump with unbound key is sensitive information.\n" "This dump should be stored encrypted in a safe place."), NULL)) @@ -2284,7 +2294,8 @@ static int action_luksRestore(void) if ((r = crypt_init(&cd, uuid_or_device_header(NULL)))) goto out; - crypt_set_confirm_callback(cd, yesDialog, NULL); + if (!ARG_SET(OPT_BATCH_MODE_ID)) + crypt_set_confirm_callback(cd, yesDialog, NULL); r = crypt_header_restore(cd, NULL, ARG_STR(OPT_HEADER_BACKUP_FILE_ID)); out: crypt_free(cd); @@ -2390,7 +2401,7 @@ static int action_luksErase(void) goto out; } - if (!yesDialog(msg, _("Operation aborted, keyslots were NOT wiped.\n"))) { + if (!ARG_SET(OPT_BATCH_MODE_ID) && !yesDialog(msg, _("Operation aborted, keyslots were NOT wiped.\n"))) { r = -EPERM; goto out; } @@ -2448,16 +2459,16 @@ static int action_luksConvert(void) return -EINVAL; } - if (asprintf(&msg, _("This operation will convert %s to %s format.\n"), - uuid_or_device_header(NULL), to_type) == -1) { - crypt_free(cd); - return -ENOMEM; + r = 0; + if (!ARG_SET(OPT_BATCH_MODE_ID)) { + if (asprintf(&msg, _("This operation will convert %s to %s format.\n"), + uuid_or_device_header(NULL), to_type) == -1) + r = -ENOMEM; + else if (!yesDialog(msg, _("Operation aborted, device was NOT converted.\n"))) + r = -EPERM; } - if (yesDialog(msg, _("Operation aborted, device was NOT converted.\n"))) - r = crypt_convert(cd, to_type, NULL); - else - r = -EPERM; + r = r ?: crypt_convert(cd, to_type, NULL); free(msg); crypt_free(cd); @@ -2591,7 +2602,7 @@ static int _token_import(struct crypt_device *cd) } } - r = tools_read_json_file(cd, ARG_STR(OPT_JSON_FILE_ID), &json, &json_length); + r = tools_read_json_file(cd, ARG_STR(OPT_JSON_FILE_ID), &json, &json_length, ARG_SET(OPT_BATCH_MODE_ID)); if (r) return r; @@ -2720,14 +2731,17 @@ static int _get_device_active_name(struct crypt_device *cd, const char *data_dev else log_err(_("Failed to auto-detect device %s holders."), data_device); - r = asprintf(&msg, _("Unable to decide if device %s is activated or not.\n" - "Are you sure you want to proceed with reencryption in offline mode?\n" - "It may lead to data corruption if the device is actually activated.\n" - "To run reencryption in online mode, use --active-name parameter instead.\n"), data_device); - if (r < 0) - return -ENOMEM; - r = noDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL; - free(msg); + r = -EINVAL; + if (!ARG_SET(OPT_BATCH_MODE_ID)) { + r = asprintf(&msg, _("Unable to decide if device %s is activated or not.\n" + "Are you sure you want to proceed with reencryption in offline mode?\n" + "It may lead to data corruption if the device is actually activated.\n" + "To run reencryption in online mode, use --active-name parameter instead.\n"), data_device); + if (r < 0) + return -ENOMEM; + r = noDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL; + free(msg); + } } return r; @@ -2847,7 +2861,7 @@ static int action_encrypt_luks2(struct crypt_device **cd) r = crypt_load(*cd, CRYPT_LUKS, NULL); crypt_free(*cd); *cd = NULL; - if (!r) { + if (!r && !ARG_SET(OPT_BATCH_MODE_ID)) { r = asprintf(&msg, _("Detected LUKS device on %s. Do you want to encrypt that LUKS device again?"), action_argv[0]); if (r == -1) return -ENOMEM; @@ -3319,6 +3333,10 @@ static int action_reencrypt(void) struct crypt_device *cd = NULL; struct crypt_params_integrity ip = { 0 }; int r = 0; + struct tools_progress_params prog_parms = { + .frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID), + .batch_mode = ARG_SET(OPT_BATCH_MODE_ID) + }; if (action_argc < 1 && (!ARG_SET(OPT_ACTIVE_NAME_ID) || ARG_SET(OPT_ENCRYPT_ID))) { log_err(_("Command requires device as argument.")); @@ -3388,7 +3406,7 @@ static int action_reencrypt(void) if (r >= 0 && !ARG_SET(OPT_INIT_ONLY_ID)) { set_int_handler(0); - r = crypt_reencrypt(cd, tools_reencrypt_progress, NULL); + r = crypt_reencrypt(cd, tools_reencrypt_progress, &prog_parms); } out: crypt_free(cd); @@ -3559,6 +3577,14 @@ static void basic_options_cb(poptContext popt_context, /* special cases additional handling */ switch (key->val) { + case OPT_DEBUG_JSON_ID: + /* fall through */ + case OPT_DEBUG_ID: + log_parms.debug = true; + /* fall through */ + case OPT_VERBOSE_ID: + log_parms.verbose = true; + break; case OPT_DEVICE_SIZE_ID: if (ARG_UINT64(OPT_DEVICE_SIZE_ID) == 0) usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER), @@ -3652,7 +3678,7 @@ int main(int argc, const char **argv) const char *aname; int r; - crypt_set_log_callback(NULL, tool_log, NULL); + crypt_set_log_callback(NULL, tool_log, &log_parms); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -3895,8 +3921,6 @@ int main(int argc, const char **argv) poptGetInvocationName(popt_context)); if (ARG_SET(OPT_DEBUG_ID) || ARG_SET(OPT_DEBUG_JSON_ID)) { - ARG_SET_TRUE(OPT_DEBUG_ID); - ARG_SET_TRUE(OPT_VERBOSE_ID); crypt_set_debug_level(ARG_SET(OPT_DEBUG_JSON_ID)? CRYPT_DEBUG_JSON : CRYPT_DEBUG_ALL); dbg_version_and_cmd(argc, argv); } @@ -3925,13 +3949,6 @@ int main(int argc, const char **argv) return 0; } - /* TODO: Remove global variables from future cli library */ - opt_verbose = ARG_SET(OPT_VERBOSE_ID) ? 1 : 0; - opt_debug = ARG_SET(OPT_DEBUG_ID) ? 1 : 0; - opt_debug_json = ARG_SET(OPT_DEBUG_JSON_ID) ? 1 : 0; - opt_batch_mode = ARG_SET(OPT_BATCH_MODE_ID) ? 1 : 0; - opt_progress_frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID); - if (ARG_SET(OPT_DISABLE_KEYRING_ID)) (void) crypt_volume_key_keyring(NULL, 0); diff --git a/src/cryptsetup.h b/src/cryptsetup.h index 35ec8ec5..65f6d0dc 100644 --- a/src/cryptsetup.h +++ b/src/cryptsetup.h @@ -64,12 +64,6 @@ # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #endif -extern int opt_debug; -extern int opt_debug_json; -extern int opt_verbose; -extern int opt_batch_mode; -extern int opt_progress_frequency; - /* Common tools */ void tool_log(int level, const char *msg, void *usrptr __attribute__((unused))); void quiet_log(int level, const char *msg, void *usrptr); @@ -104,13 +98,21 @@ int tools_is_stdin(const char *key_file); int tools_string_to_size(struct crypt_device *cd, const char *s, uint64_t *size); int tools_is_cipher_null(const char *cipher); +struct tools_progress_params { + uint32_t frequency; + struct timeval start_time; + struct timeval end_time; + uint64_t start_offset; + bool batch_mode; +}; + int tools_wipe_progress(uint64_t size, uint64_t offset, void *usrptr); int tools_reencrypt_progress(uint64_t size, uint64_t offset, void *usrptr); -int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size); +int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size, bool batch_mode); int tools_write_json_file(struct crypt_device *cd, const char *file, const char *json); -int tools_detect_signatures(const char *device, int ignore_luks, size_t *count); +int tools_detect_signatures(const char *device, int ignore_luks, size_t *count, bool batch_mode); int tools_wipe_all_signatures(const char *path); int tools_lookup_crypt_device(struct crypt_device *cd, const char *type, @@ -133,4 +135,9 @@ void tools_cleanup(void); #define log_verbose(x...) crypt_logf(NULL, CRYPT_LOG_VERBOSE, x) #define log_err(x...) crypt_logf(NULL, CRYPT_LOG_ERROR, x) +struct tools_log_params { + bool verbose; + bool debug; +}; + #endif /* CRYPTSETUP_H */ diff --git a/src/cryptsetup_reencrypt.c b/src/cryptsetup_reencrypt.c index 9b7f155d..7fce6b23 100644 --- a/src/cryptsetup_reencrypt.c +++ b/src/cryptsetup_reencrypt.c @@ -35,6 +35,8 @@ static const char **action_argv; static const char *set_pbkdf = NULL; +static struct tools_log_params log_parms; + #define MAX_SLOT 32 #define MAX_TOKEN 32 struct reenc_ctx { @@ -909,6 +911,10 @@ static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new, size_t block_size, void *buf, uint64_t *bytes) { ssize_t s1, s2; + struct tools_progress_params prog_parms = { + .frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID), + .batch_mode = ARG_SET(OPT_BATCH_MODE_ID) + }; log_dbg("Reencrypting in forward direction."); @@ -920,7 +926,7 @@ static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new, rc->resume_bytes = *bytes = rc->device_offset; - tools_reencrypt_progress(rc->device_size, *bytes, NULL); + tools_reencrypt_progress(rc->device_size, *bytes, &prog_parms); if (write_log(rc) < 0) return -EIO; @@ -956,7 +962,7 @@ static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new, *bytes += (uint64_t)s2; - tools_reencrypt_progress(rc->device_size, *bytes, NULL); + tools_reencrypt_progress(rc->device_size, *bytes, &prog_parms); } return quit ? -EAGAIN : 0; @@ -967,6 +973,10 @@ static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new, { ssize_t s1, s2, working_block; off64_t working_offset; + struct tools_progress_params prog_parms = { + .frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID), + .batch_mode = ARG_SET(OPT_BATCH_MODE_ID) + }; log_dbg("Reencrypting in backward direction."); @@ -979,7 +989,7 @@ static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new, *bytes = rc->resume_bytes; } - tools_reencrypt_progress(rc->device_size, *bytes, NULL); + tools_reencrypt_progress(rc->device_size, *bytes, &prog_parms); if (write_log(rc) < 0) return -EIO; @@ -1027,7 +1037,7 @@ static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new, *bytes += (uint64_t)s2; - tools_reencrypt_progress(rc->device_size, *bytes, NULL); + tools_reencrypt_progress(rc->device_size, *bytes, &prog_parms); } return quit ? -EAGAIN : 0; @@ -1593,6 +1603,12 @@ static void basic_options_cb(poptContext popt_context, /* special cases additional handling */ switch (key->val) { + case OPT_DEBUG_ID: + log_parms.debug = true; + /* fall through */ + case OPT_VERBOSE_ID: + log_parms.verbose = true; + break; case OPT_BLOCK_SIZE_ID: if (ARG_UINT32(OPT_BLOCK_SIZE_ID) < 1 || ARG_UINT32(OPT_BLOCK_SIZE_ID) > 64) usage(popt_context, EXIT_FAILURE, @@ -1643,7 +1659,7 @@ int main(int argc, const char **argv) poptContext popt_context; int r; - crypt_set_log_callback(NULL, tool_log, NULL); + crypt_set_log_callback(NULL, tool_log, &log_parms); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -1720,16 +1736,10 @@ int main(int argc, const char **argv) poptGetInvocationName(popt_context)); if (ARG_SET(OPT_DEBUG_ID)) { - ARG_SET_TRUE(OPT_VERBOSE_ID); crypt_set_debug_level(CRYPT_DEBUG_ALL); dbg_version_and_cmd(argc, argv); } - opt_verbose = ARG_SET(OPT_VERBOSE_ID) ? 1 : 0; - opt_debug = ARG_SET(OPT_DEBUG_ID) ? 1 : 0; - opt_batch_mode = ARG_SET(OPT_BATCH_MODE_ID) ? 1 : 0; - opt_progress_frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID); - r = run_reencrypt(action_argv[0]); tools_cleanup(); poptFreeContext(popt_context); diff --git a/src/integritysetup.c b/src/integritysetup.c index b7a8b8f3..eb2ba7de 100644 --- a/src/integritysetup.c +++ b/src/integritysetup.c @@ -33,6 +33,7 @@ static const char **action_argv; static int action_argc; +static struct tools_log_params log_parms; void tools_cleanup(void) { @@ -83,6 +84,10 @@ static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key) char tmp_name[64], tmp_path[128], tmp_uuid[40]; uuid_t tmp_uuid_bin; int r; + struct tools_progress_params prog_parms = { + .frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID), + .batch_mode = ARG_SET(OPT_BATCH_MODE_ID) + }; if (!ARG_SET(OPT_BATCH_MODE_ID)) log_std(_("Wiping device to initialize integrity checksum.\n" @@ -105,7 +110,7 @@ static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key) /* Wipe the device */ set_int_handler(0); r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK, - 0, &tools_wipe_progress, NULL); + 0, &tools_wipe_progress, &prog_parms); if (crypt_deactivate(cd, tmp_name)) log_err(_("Cannot deactivate temporary device %s."), tmp_path); set_int_block(0); @@ -164,18 +169,20 @@ static int action_format(int arg) if (r < 0) goto out; - r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]); - if (r == -1) { - r = -ENOMEM; - goto out; + if (!ARG_SET(OPT_BATCH_MODE_ID)) { + r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]); + if (r == -1) { + r = -ENOMEM; + goto out; + } + + r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL; + free(msg); + if (r < 0) + goto out; } - r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL; - free(msg); - if (r < 0) - goto out; - - r = tools_detect_signatures(action_argv[0], 0, &signatures); + r = tools_detect_signatures(action_argv[0], 0, &signatures, ARG_SET(OPT_BATCH_MODE_ID)); if (r < 0) goto out; @@ -499,6 +506,12 @@ static void basic_options_cb(poptContext popt_context, /* special cases additional handling */ switch (key->val) { + case OPT_DEBUG_ID: + log_parms.debug = true; + /* fall through */ + case OPT_VERBOSE_ID: + log_parms.verbose = true; + break; case OPT_INTEGRITY_KEY_SIZE_ID: /* fall through */ case OPT_JOURNAL_INTEGRITY_KEY_SIZE_ID: @@ -539,7 +552,7 @@ int main(int argc, const char **argv) const char *aname; int r; - crypt_set_log_callback(NULL, tool_log, NULL); + crypt_set_log_callback(NULL, tool_log, &log_parms); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -641,16 +654,10 @@ int main(int argc, const char **argv) poptGetInvocationName(popt_context)); if (ARG_SET(OPT_DEBUG_ID)) { - ARG_SET_TRUE(OPT_VERBOSE_ID); crypt_set_debug_level(CRYPT_DEBUG_ALL); dbg_version_and_cmd(argc, argv); } - opt_verbose = ARG_SET(OPT_VERBOSE_ID) ? 1 : 0; - opt_debug = ARG_SET(OPT_DEBUG_ID) ? 1 : 0; - opt_batch_mode = ARG_SET(OPT_BATCH_MODE_ID) ? 1 : 0; - opt_progress_frequency = ARG_UINT32(OPT_PROGRESS_FREQUENCY_ID); - r = run_action(action); tools_cleanup(); poptFreeContext(popt_context); diff --git a/src/utils_blockdev.c b/src/utils_blockdev.c index 9b524a56..78b488b1 100644 --- a/src/utils_blockdev.c +++ b/src/utils_blockdev.c @@ -189,23 +189,23 @@ int tools_lookup_crypt_device(struct crypt_device *cd, const char *type, } -static void report_partition(const char *value, const char *device) +static void report_partition(const char *value, const char *device, bool batch_mode) { - if (opt_batch_mode) + if (batch_mode) log_dbg("Device %s already contains a '%s' partition signature.", device, value); else log_std(_("WARNING: Device %s already contains a '%s' partition signature.\n"), device, value); } -static void report_superblock(const char *value, const char *device) +static void report_superblock(const char *value, const char *device, bool batch_mode) { - if (opt_batch_mode) + if (batch_mode) log_dbg("Device %s already contains a '%s' superblock signature.", device, value); else log_std(_("WARNING: Device %s already contains a '%s' superblock signature.\n"), device, value); } -int tools_detect_signatures(const char *device, int ignore_luks, size_t *count) +int tools_detect_signatures(const char *device, int ignore_luks, size_t *count, bool batch_mode) { int r; size_t tmp_count; @@ -236,9 +236,9 @@ int tools_detect_signatures(const char *device, int ignore_luks, size_t *count) while ((pr = blk_probe(h)) < PRB_EMPTY) { if (blk_is_partition(h)) - report_partition(blk_get_partition_type(h), device); + report_partition(blk_get_partition_type(h), device, batch_mode); else if (blk_is_superblock(h)) - report_superblock(blk_get_superblock_type(h), device); + report_superblock(blk_get_superblock_type(h), device, batch_mode); else { log_dbg("Internal tools_detect_signatures() error."); r = -EINVAL; diff --git a/src/utils_luks2.c b/src/utils_luks2.c index 535d85f5..82a2c9e2 100644 --- a/src/utils_luks2.c +++ b/src/utils_luks2.c @@ -27,7 +27,7 @@ * In future, read max allowed JSON size from config section. */ #define LUKS2_MAX_MDA_SIZE 0x400000 -int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size) +int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size, bool batch_mode) { ssize_t ret; int fd, block, r; @@ -56,7 +56,7 @@ int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, goto out; } - if (isatty(fd) && !opt_batch_mode) + if (isatty(fd) && !batch_mode) log_std(_("Provide valid LUKS2 token JSON:\n")); /* we expect JSON (string) */ diff --git a/src/utils_tools.c b/src/utils_tools.c index 16c2b235..9adbba67 100644 --- a/src/utils_tools.c +++ b/src/utils_tools.c @@ -25,12 +25,6 @@ #include #include -int opt_verbose = 0; -int opt_debug = 0; -int opt_debug_json = 0; -int opt_batch_mode = 0; -int opt_progress_frequency = 0; - /* interrupt handling */ volatile int quit = 0; static int signals_blocked = 0; @@ -77,15 +71,17 @@ void check_signal(int *r) *r = -EINTR; } -void tool_log(int level, const char *msg, void *usrptr __attribute__((unused))) +void tool_log(int level, const char *msg, void *usrptr) { - switch(level) { + struct tools_log_params *params = (struct tools_log_params *)usrptr; + + switch (level) { case CRYPT_LOG_NORMAL: fprintf(stdout, "%s", msg); break; case CRYPT_LOG_VERBOSE: - if (opt_verbose) + if (params && params->verbose) fprintf(stdout, "%s", msg); break; case CRYPT_LOG_ERROR: @@ -93,7 +89,7 @@ void tool_log(int level, const char *msg, void *usrptr __attribute__((unused))) break; case CRYPT_LOG_DEBUG_JSON: case CRYPT_LOG_DEBUG: - if (opt_debug) + if (params && params->debug) fprintf(stdout, "# %s", msg); break; } @@ -101,7 +97,9 @@ void tool_log(int level, const char *msg, void *usrptr __attribute__((unused))) void quiet_log(int level, const char *msg, void *usrptr) { - if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL)) + struct tools_log_params *params = (struct tools_log_params *)usrptr; + + if ((!params || !params->verbose) && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL)) return; tool_log(level, msg, usrptr); } @@ -117,7 +115,7 @@ static int _dialog(const char *msg, void *usrptr, int default_answer) if (block) set_int_block(0); - if (isatty(STDIN_FILENO) && !opt_batch_mode) { + if (isatty(STDIN_FILENO)) { log_std("\nWARNING!\n========\n"); log_std("%s\n\nAre you sure? (Type 'yes' in capital letters): ", msg); fflush(stdout); @@ -156,11 +154,8 @@ void show_status(int errcode) { char *crypt_error; - if(!opt_verbose) - return; - - if(!errcode) { - log_std(_("Command successful.\n")); + if (!errcode) { + log_verbose(_("Command successful.")); return; } @@ -180,7 +175,7 @@ void show_status(int errcode) else crypt_error = _("unknown error"); - log_std(_("Command failed with code %i (%s).\n"), -errcode, crypt_error); + log_verbose(_("Command failed with code %i (%s)."), -errcode, crypt_error); } const char *uuid_or_device(const char *spec) @@ -352,14 +347,11 @@ static double time_diff(struct timeval *start, struct timeval *end) static void tools_clear_line(void) { - if (opt_progress_frequency) - return; /* vt100 code clear line */ log_std("\33[2K\r"); } -static void tools_time_progress(uint64_t device_size, uint64_t bytes, uint64_t *start_bytes, - struct timeval *start_time, struct timeval *end_time) +static void tools_time_progress(uint64_t device_size, uint64_t bytes, struct tools_progress_params *parms) { struct timeval now_time; unsigned long long mbytes, eta; @@ -367,36 +359,33 @@ static void tools_time_progress(uint64_t device_size, uint64_t bytes, uint64_t * int final = (bytes == device_size); const char *eol, *ustr = ""; - if (opt_batch_mode) - return; - gettimeofday(&now_time, NULL); - if (start_time->tv_sec == 0 && start_time->tv_usec == 0) { - *start_time = now_time; - *end_time = now_time; - *start_bytes = bytes; + if (parms->start_time.tv_sec == 0 && parms->start_time.tv_usec == 0) { + parms->start_time = now_time; + parms->end_time = now_time; + parms->start_offset = bytes; return; } - if (opt_progress_frequency) { - frequency = (double)opt_progress_frequency; + if (parms->frequency) { + frequency = (double)parms->frequency; eol = "\n"; } else { frequency = 0.5; eol = ""; } - if (!final && time_diff(end_time, &now_time) < frequency) + if (!final && time_diff(&parms->end_time, &now_time) < frequency) return; - *end_time = now_time; + parms->end_time = now_time; - tdiff = time_diff(start_time, end_time); + tdiff = time_diff(&parms->start_time, &parms->end_time); if (!tdiff) return; mbytes = bytes / 1024 / 1024; - uib = (double)(bytes - *start_bytes) / tdiff; + uib = (double)(bytes - parms->start_offset) / tdiff; /* FIXME: calculate this from last minute only. */ eta = (unsigned long long)(device_size / uib - tdiff); @@ -412,7 +401,8 @@ static void tools_time_progress(uint64_t device_size, uint64_t bytes, uint64_t * ustr = "Ki"; } - tools_clear_line(); + if (!parms->frequency) + tools_clear_line(); if (final) log_std("Finished, time %02llu:%02llu.%03llu, " "%4llu MiB written, speed %5.1f %sB/s\n", @@ -430,15 +420,16 @@ static void tools_time_progress(uint64_t device_size, uint64_t bytes, uint64_t * int tools_wipe_progress(uint64_t size, uint64_t offset, void *usrptr) { - static struct timeval start_time = {}, end_time = {}; - static uint64_t start_offset = 0; int r = 0; + struct tools_progress_params *parms = (struct tools_progress_params *)usrptr; - tools_time_progress(size, offset, &start_offset, &start_time, &end_time); + if (parms && !parms->batch_mode) + tools_time_progress(size, offset, parms); check_signal(&r); if (r) { - tools_clear_line(); + if (!parms || !parms->frequency) + tools_clear_line(); log_err(_("\nWipe interrupted.")); } @@ -466,15 +457,16 @@ int tools_is_stdin(const char *key_file) int tools_reencrypt_progress(uint64_t size, uint64_t offset, void *usrptr) { - static struct timeval start_time = {}, end_time = {}; - static uint64_t start_offset = 0; int r = 0; + struct tools_progress_params *parms = (struct tools_progress_params *)usrptr; - tools_time_progress(size, offset, &start_offset, &start_time, &end_time); + if (parms && !parms->batch_mode) + tools_time_progress(size, offset, parms); check_signal(&r); if (r) { - tools_clear_line(); + if (!parms || !parms->frequency) + tools_clear_line(); log_err(_("\nReencryption interrupted.")); } diff --git a/src/veritysetup.c b/src/veritysetup.c index 757a3cb1..92bfcd4b 100644 --- a/src/veritysetup.c +++ b/src/veritysetup.c @@ -26,6 +26,7 @@ static const char **action_argv; static int action_argc; +static struct tools_log_params log_parms; void tools_cleanup(void) { @@ -462,6 +463,14 @@ static void basic_options_cb(poptContext popt_context, void *data __attribute__((unused))) { tools_parse_arg_value(popt_context, tool_core_args[key->val].type, tool_core_args + key->val, arg, key->val, NULL); + + switch (key->val) { + case OPT_DEBUG_ID: + log_parms.debug = true; + /* fall through */ + case OPT_VERBOSE_ID: + log_parms.verbose = true; + } } int main(int argc, const char **argv) @@ -492,7 +501,7 @@ int main(int argc, const char **argv) const char *aname; int r; - crypt_set_log_callback(NULL, tool_log, NULL); + crypt_set_log_callback(NULL, tool_log, &log_parms); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -568,14 +577,10 @@ int main(int argc, const char **argv) poptGetInvocationName(popt_context)); if (ARG_SET(OPT_DEBUG_ID)) { - ARG_SET_TRUE(OPT_VERBOSE_ID); crypt_set_debug_level(CRYPT_DEBUG_ALL); dbg_version_and_cmd(argc, argv); } - opt_verbose = ARG_SET(OPT_VERBOSE_ID) ? 1 : 0; - opt_debug = ARG_SET(OPT_DEBUG_ID) ? 1 : 0; - r = run_action(action); tools_cleanup(); poptFreeContext(popt_context);