diff --git a/src/yubikey.rs b/src/yubikey.rs index 39ef400..6a699e3 100644 --- a/src/yubikey.rs +++ b/src/yubikey.rs @@ -205,19 +205,40 @@ impl YubiKey { pub fn open_by_serial(serial: Serial) -> Result { let mut readers = Context::open()?; + let mut open_error = None; + for reader in readers.iter()? { let yubikey = match reader.open() { Ok(yk) => yk, - Err(_) => continue, + Err(e) => { + // Save the first error we see that indicates we might have been able + // to find a matching YubiKey. + if open_error.is_none() { + if let Error::PcscError { + inner: Some(pcsc::Error::SharingViolation), + } = e + { + open_error = Some(e); + } + } + continue; + } }; if serial == yubikey.serial() { return Ok(yubikey); + } else { + // We didn't want this YubiKey; don't reset it. + let _ = yubikey.disconnect(pcsc::Disposition::LeaveCard); } } - error!("no YubiKey detected with serial: {}", serial); - Err(Error::NotFound) + Err(if let Some(e) = open_error { + e + } else { + error!("no YubiKey detected with serial: {}", serial); + Error::NotFound + }) } /// Reconnect to a YubiKey.