From 0c679b30ef04de8bc556ce33b305d3ccf740a970 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 27 Dec 2018 17:49:14 +0100 Subject: Implement the pws status subcommand This patch implements the pws status command that can be used to print status information for the slots in the password safe. --- nitrocli/src/commands.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 4deb1a7..c903cfd 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -20,6 +20,7 @@ use std::fmt; use std::result; use std::time; +use std::u8; use nitrokey::ConfigureOtp; use nitrokey::Device; @@ -602,6 +603,40 @@ pub fn pws_clear(slot: u8) -> Result<()> { .map_err(|err| get_error("Could not clear PWS slot", &err)) } +fn print_pws_slot(pws: &nitrokey::PasswordSafe<'_>, slot: usize, programmed: bool) -> Result<()> { + if slot > u8::MAX as usize { + return Err(Error::Error("Invalid PWS slot number".to_string())); + } + let slot = slot as u8; + let name = if programmed { + pws + .get_slot_name(slot) + .map_err(|err| get_error("Could not read PWS slot", &err))? + } else { + "[not programmed]".to_string() + }; + println!("{}\t{}", slot, name); + Ok(()) +} + +/// Print the status of all PWS slots. +pub fn pws_status(all: bool) -> Result<()> { + let device = get_device()?; + let pws = get_password_safe(&device)?; + let slots = pws + .get_slot_status() + .map_err(|err| get_error("Could not read PWS slot status", &err))?; + println!("slot\tname"); + for (i, &value) in slots + .into_iter() + .enumerate() + .filter(|(_, &value)| all || value) + { + print_pws_slot(&pws, i, value)?; + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.1