From 21e7c36a0ae14f2200e806fcf66cbe0836cc2ec0 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 25 Dec 2018 23:02:18 +0100 Subject: Add status output for Nitrokey Pro Currently, the status command fails for a Nitrokey Pro. This patch changes the command to also print basic status information for Pro devices. For the sake of consistency, the common status is always queried using the common `Device` functions, even if the Storage status includes the same information. --- nitrocli/src/commands.rs | 61 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index c4d4598..528fa99 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -40,8 +40,11 @@ fn get_device() -> Result { /// Connect to a Nitrokey Storage device and return it. fn get_storage_device() -> Result { - nitrokey::Storage::connect() - .or_else(|_| Err(Error::Error("Nitrokey device not found".to_string()))) + nitrokey::Storage::connect().or_else(|_| { + Err(Error::Error( + "Nitrokey Storage device not found".to_string(), + )) + }) } /// Authenticate the given device using the given PIN type and operation. @@ -171,41 +174,30 @@ where .map_err(|(_data, err)| err) } -/// Pretty print the status that is common to all Nitrokey devices. -fn print_status( - model: &'static str, - smartcard_id: &str, - firmware_major: i32, - firmware_minor: i32, - user_retries: u8, - admin_retries: u8, -) { +/// Query and pretty print the status that is common to all Nitrokey devices. +fn print_status(model: &'static str, device: &nitrokey::DeviceWrapper) -> Result<()> { + let serial_number = device + .get_serial_number() + .map_err(|err| get_error("Could not query the serial number", &err))?; println!( r#"Status: model: {model} - smart card ID: {id} + serial number: 0x{id} firmware version: {fwv0}.{fwv1} user retry count: {urc} admin retry count: {arc}"#, model = model, - id = smartcard_id, - fwv0 = firmware_major, - fwv1 = firmware_minor, - urc = user_retries, - arc = admin_retries, + id = serial_number, + fwv0 = device.get_major_firmware_version(), + fwv1 = device.get_minor_firmware_version(), + urc = device.get_user_retry_count(), + arc = device.get_admin_retry_count(), ); + Ok(()) } -/// Pretty print the response of a status command for the Nitrokey Storage. +/// Pretty print the status of a Nitrokey Storage. fn print_storage_status(status: &nitrokey::StorageStatus) { - print_status( - "Storage", - &format!("{:#x}", status.serial_number_smart_card), - status.firmware_version_major as i32, - status.firmware_version_minor as i32, - status.user_retry_count, - status.admin_retry_count, - ); println!( r#" SD card ID: {id:#x} @@ -234,11 +226,18 @@ fn print_storage_status(status: &nitrokey::StorageStatus) { /// Inquire the status of the nitrokey. pub fn status() -> Result<()> { - let status = get_storage_device()? - .get_status() - .map_err(|err| get_error("Getting Storage status failed", &err))?; - - print_storage_status(&status); + let device = get_device()?; + let model = match device { + nitrokey::DeviceWrapper::Pro(_) => "Pro", + nitrokey::DeviceWrapper::Storage(_) => "Storage", + }; + print_status(model, &device)?; + if let nitrokey::DeviceWrapper::Storage(storage) = device { + let status = storage + .get_status() + .map_err(|err| get_error("Getting Storage status failed", &err))?; + print_storage_status(&status); + } Ok(()) } -- cgit v1.2.1