aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/commands.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-05 18:36:33 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-05 18:36:33 -0800
commitb750c4b13272908a51b85072008554c344b25016 (patch)
treece1512eacb1bfc0d507c01f171534fb7d66c3241 /nitrocli/src/commands.rs
parent1d1cc940f47c41637adea5c5a1e5d3c80807f9d7 (diff)
downloadnitrocli-b750c4b13272908a51b85072008554c344b25016.tar.gz
nitrocli-b750c4b13272908a51b85072008554c344b25016.tar.bz2
Work with mutable ExecCtx references
So far we have used a read-only reference to a command execution context and passed that through to all consumers. However, with upcoming changes we would will need to provide data that can be modified. This change adjusts all function signatures accordingly. Also, because the ExecCtx will contain references itself in the future, this change already introduces a lifetime for the struct, as that also requires signature adjustments.
Diffstat (limited to 'nitrocli/src/commands.rs')
-rw-r--r--nitrocli/src/commands.rs44
1 files changed, 24 insertions, 20 deletions
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs
index 289c257..6c83c29 100644
--- a/nitrocli/src/commands.rs
+++ b/nitrocli/src/commands.rs
@@ -39,7 +39,7 @@ fn get_error(msg: &str, err: nitrokey::CommandError) -> Error {
}
/// Set `libnitrokey`'s log level based on the execution context's verbosity.
-fn set_log_level(ctx: &args::ExecCtx) {
+fn set_log_level(ctx: &mut args::ExecCtx<'_>) {
let log_lvl = match ctx.verbosity {
// The error log level is what libnitrokey uses by default. As such,
// there is no harm in us setting that as well when the user did not
@@ -55,7 +55,7 @@ fn set_log_level(ctx: &args::ExecCtx) {
}
/// Connect to any Nitrokey device and return it.
-fn get_device(ctx: &args::ExecCtx) -> Result<nitrokey::DeviceWrapper> {
+fn get_device(ctx: &mut args::ExecCtx<'_>) -> Result<nitrokey::DeviceWrapper> {
set_log_level(ctx);
match ctx.model {
@@ -71,7 +71,7 @@ fn get_device(ctx: &args::ExecCtx) -> Result<nitrokey::DeviceWrapper> {
}
/// Connect to a Nitrokey Storage device and return it.
-fn get_storage_device(ctx: &args::ExecCtx) -> Result<nitrokey::Storage> {
+fn get_storage_device(ctx: &mut args::ExecCtx<'_>) -> Result<nitrokey::Storage> {
set_log_level(ctx);
if let Some(model) = ctx.model {
@@ -254,7 +254,7 @@ fn print_status(model: &'static str, device: &nitrokey::DeviceWrapper) -> Result
}
/// Inquire the status of the nitrokey.
-pub fn status(ctx: &args::ExecCtx) -> Result<()> {
+pub fn status(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
let device = get_device(ctx)?;
let model = match device {
nitrokey::DeviceWrapper::Pro(_) => "Pro",
@@ -264,7 +264,7 @@ pub fn status(ctx: &args::ExecCtx) -> Result<()> {
}
/// Open the encrypted volume on the nitrokey.
-pub fn storage_open(ctx: &args::ExecCtx) -> Result<()> {
+pub fn storage_open(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
let device = get_storage_device(ctx)?;
try_with_passphrase(
pinentry::PinType::User,
@@ -274,7 +274,7 @@ pub fn storage_open(ctx: &args::ExecCtx) -> Result<()> {
}
/// Close the previously opened encrypted volume.
-pub fn storage_close(ctx: &args::ExecCtx) -> Result<()> {
+pub fn storage_close(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
// Flush all filesystem caches to disk. We are mostly interested in
// making sure that the encrypted volume on the nitrokey we are
// about to close is not closed while not all data was written to
@@ -315,7 +315,7 @@ fn print_storage_status(status: &nitrokey::StorageStatus) {
}
/// Connect to and pretty print the status of a Nitrokey Storage.
-pub fn storage_status(ctx: &args::ExecCtx) -> Result<()> {
+pub fn storage_status(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
let device = get_storage_device(ctx)?;
let status = device
.get_status()
@@ -334,7 +334,7 @@ fn format_option<T: fmt::Display>(option: Option<T>) -> String {
}
/// Read the Nitrokey configuration.
-pub fn config_get(ctx: &args::ExecCtx) -> Result<()> {
+pub fn config_get(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
let config = get_device(ctx)?
.get_config()
.map_err(|err| get_error("Could not get configuration", err))?;
@@ -354,7 +354,7 @@ pub fn config_get(ctx: &args::ExecCtx) -> Result<()> {
/// Write the Nitrokey configuration.
pub fn config_set(
- ctx: &args::ExecCtx,
+ ctx: &mut args::ExecCtx<'_>,
numlock: args::ConfigOption<u8>,
capslock: args::ConfigOption<u8>,
scrollock: args::ConfigOption<u8>,
@@ -376,7 +376,7 @@ pub fn config_set(
}
/// Lock the Nitrokey device.
-pub fn lock(ctx: &args::ExecCtx) -> Result<()> {
+pub fn lock(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
get_device(ctx)?
.lock()
.map_err(|err| get_error("Getting Storage status failed", err))
@@ -403,7 +403,7 @@ fn get_unix_timestamp() -> Result<u64> {
/// Generate a one-time password on the Nitrokey device.
pub fn otp_get(
- ctx: &args::ExecCtx,
+ ctx: &mut args::ExecCtx<'_>,
slot: u8,
algorithm: args::OtpAlgorithm,
time: Option<u64>,
@@ -466,7 +466,7 @@ fn prepare_base32_secret(secret: &str) -> Result<String> {
/// Configure a one-time password slot on the Nitrokey device.
pub fn otp_set(
- ctx: &args::ExecCtx,
+ ctx: &mut args::ExecCtx<'_>,
data: nitrokey::OtpSlotData,
algorithm: args::OtpAlgorithm,
counter: u64,
@@ -489,7 +489,11 @@ pub fn otp_set(
}
/// Clear an OTP slot.
-pub fn otp_clear(ctx: &args::ExecCtx, slot: u8, algorithm: args::OtpAlgorithm) -> Result<()> {
+pub fn otp_clear(
+ ctx: &mut args::ExecCtx<'_>,
+ slot: u8,
+ algorithm: args::OtpAlgorithm,
+) -> Result<()> {
let device = authenticate_admin(get_device(ctx)?)?;
match algorithm {
args::OtpAlgorithm::Hotp => device.erase_hotp_slot(slot),
@@ -535,7 +539,7 @@ fn print_otp_status(
}
/// Print the status of the OTP slots.
-pub fn otp_status(ctx: &args::ExecCtx, all: bool) -> Result<()> {
+pub fn otp_status(ctx: &mut args::ExecCtx<'_>, all: bool) -> Result<()> {
let device = get_device(ctx)?;
println!("alg\tslot\tname");
print_otp_status(args::OtpAlgorithm::Hotp, &device, all)?;
@@ -584,7 +588,7 @@ fn choose_pin(pintype: pinentry::PinType) -> Result<String> {
}
/// Change a PIN.
-pub fn pin_set(ctx: &args::ExecCtx, pintype: pinentry::PinType) -> Result<()> {
+pub fn pin_set(ctx: &mut args::ExecCtx<'_>, pintype: pinentry::PinType) -> Result<()> {
let device = get_device(ctx)?;
let new_pin = choose_pin(pintype)?;
try_with_passphrase(
@@ -598,7 +602,7 @@ pub fn pin_set(ctx: &args::ExecCtx, pintype: pinentry::PinType) -> Result<()> {
}
/// Unblock and reset the user PIN.
-pub fn pin_unblock(ctx: &args::ExecCtx) -> Result<()> {
+pub fn pin_unblock(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
let device = get_device(ctx)?;
let user_pin = choose_pin(pinentry::PinType::User)?;
try_with_passphrase(
@@ -624,7 +628,7 @@ fn print_pws_data(
/// Read a PWS slot.
pub fn pws_get(
- ctx: &args::ExecCtx,
+ ctx: &mut args::ExecCtx<'_>,
slot: u8,
show_name: bool,
show_login: bool,
@@ -648,7 +652,7 @@ pub fn pws_get(
/// Write a PWS slot.
pub fn pws_set(
- ctx: &args::ExecCtx,
+ ctx: &mut args::ExecCtx<'_>,
slot: u8,
name: &str,
login: &str,
@@ -662,7 +666,7 @@ pub fn pws_set(
}
/// Clear a PWS slot.
-pub fn pws_clear(ctx: &args::ExecCtx, slot: u8) -> Result<()> {
+pub fn pws_clear(ctx: &mut args::ExecCtx<'_>, slot: u8) -> Result<()> {
let device = get_device(ctx)?;
let pws = get_password_safe(&device)?;
pws
@@ -687,7 +691,7 @@ fn print_pws_slot(pws: &nitrokey::PasswordSafe<'_>, slot: usize, programmed: boo
}
/// Print the status of all PWS slots.
-pub fn pws_status(ctx: &args::ExecCtx, all: bool) -> Result<()> {
+pub fn pws_status(ctx: &mut args::ExecCtx<'_>, all: bool) -> Result<()> {
let device = get_device(ctx)?;
let pws = get_password_safe(&device)?;
let slots = pws