aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/commands.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-27 17:49:14 +0100
committerDaniel Mueller <deso@posteo.net>2019-01-07 18:13:46 -0800
commit0c679b30ef04de8bc556ce33b305d3ccf740a970 (patch)
tree2e47aa26e71ca65cd6d6e9ad6d6b7e11a5fc82e5 /nitrocli/src/commands.rs
parent6a69bbf736966c493cead716026db92c5b44962a (diff)
downloadnitrocli-0c679b30ef04de8bc556ce33b305d3ccf740a970.tar.gz
nitrocli-0c679b30ef04de8bc556ce33b305d3ccf740a970.tar.bz2
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.
Diffstat (limited to 'nitrocli/src/commands.rs')
-rw-r--r--nitrocli/src/commands.rs35
1 files changed, 35 insertions, 0 deletions
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::*;