Skip to content

Commit

Permalink
Implement list command
Browse files Browse the repository at this point in the history
This patch implements the list command that lists all attached Nitrokey
devices.  Currently the Nitrokey Storage does not report its serial
number during HID enumeration, see [0].  So if we detect a Nitrokey
Storage device, we connect to it and use the get_serial_number function
to query its serial number.  This can be disabled using the --no-connect
option.

Note that even the get_serial_number function reports a wrong serial
number for the Nitrokey Storage, see [1].

[0] Nitrokey/nitrokey-storage-firmware#88
[1] Nitrokey/nitrokey-storage-firmware#76
  • Loading branch information
robinkrahl committed Jan 15, 2020
1 parent 9f55cda commit c25f31e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions nitrocli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,38 @@ pub fn status(ctx: &mut args::ExecCtx<'_>) -> Result<()> {
}

/// List the attached Nitrokey devices.
pub fn list(_ctx: &mut args::ExecCtx<'_>, _no_connect: bool) -> Result<()> {
unimplemented!();
pub fn list(ctx: &mut args::ExecCtx<'_>, no_connect: bool) -> Result<()> {
set_log_level(ctx);

let device_infos = nitrokey::list_devices()?;
if device_infos.is_empty() {
println!(ctx, "No Nitrokey device connected")?;
} else {
println!(ctx, "device path\tmodel\tserial number")?;
let mut manager = nitrokey::take()?;

for device_info in device_infos {
let model = device_info
.model
.map(|m| m.to_string())
.unwrap_or_else(|| "unknown".into());
let serial_number = match device_info.serial_number {
Some(serial_number) => serial_number,
None => {
if no_connect {
"?".to_string()
} else {
let device = manager.connect_path(device_info.path.clone())?;
device.get_serial_number()?
}
}
};

println!(ctx, "{}\t{}\t0x{}", device_info.path, model, serial_number)?;
}
}

Ok(())
}

/// Perform a factory reset.
Expand Down

0 comments on commit c25f31e

Please sign in to comment.