From f6de4a033ed958716121d0f865e5ba2f6d11e77a Mon Sep 17 00:00:00 2001 From: shimun Date: Fri, 27 Mar 2020 18:28:33 +0100 Subject: [PATCH] more detailed messages --- src/cli.rs | 19 +++++++++++++------ src/error.rs | 7 ++++++- src/luks.rs | 15 +++++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index ce77947..c467d08 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -209,13 +209,20 @@ pub fn run_cli() -> Fido2LuksResult<()> { }?; let added_slot = luks::add_key(device.clone(), &secret, &old_secret[..], Some(10))?; if *exclusive { - luks::remove_keyslots(&device, &[added_slot])?; + let destroyed = luks::remove_keyslots(&device, &[added_slot])?; + println!( + "Added to key to device {}, slot: {}\nRemoved {} old keys", + device.display(), + added_slot, + destroyed + ); + } else { + println!( + "Added to key to device {}, slot: {}", + device.display(), + added_slot + ); } - println!( - "Added to key to device {}, slot: {}", - device.display(), - added_slot - ); Ok(()) } Command::ReplaceKey { diff --git a/src/error.rs b/src/error.rs index 2085fc2..3af80c3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -58,7 +58,12 @@ impl From for Fido2LuksError { impl From for Fido2LuksError { fn from(e: LibcryptErr) -> Self { - LuksError { cause: e } + match e { + LibcryptErr::IOError(e) if e.raw_os_error().iter().any(|code| code == &1i32) => { + WrongSecret + } + _ => LuksError { cause: e }, + } } } impl From for Fido2LuksError { diff --git a/src/luks.rs b/src/luks.rs index 0b1722d..cda0ff4 100644 --- a/src/luks.rs +++ b/src/luks.rs @@ -1,9 +1,6 @@ use crate::error::*; -use libcryptsetup_rs::{ - CryptActivateFlags, CryptDevice, CryptInit, CryptKeyslot, CryptVolumeKeyFlags, - EncryptionFormat, KeyslotInfo, LibcryptErr, -}; +use libcryptsetup_rs::{CryptActivateFlags, CryptDevice, CryptInit, EncryptionFormat, KeyslotInfo}; use std::path::Path; fn load_device_handle>(path: P) -> Fido2LuksResult { @@ -14,7 +11,7 @@ fn load_device_handle>(path: P) -> Fido2LuksResult { .into_iter() .fold(None, |res, format| match res { Some(Ok(())) => res, - Some(e) => Some(e.and(load(format))), + Some(e) => Some(e.or(load(format))), None => Some(load(format)), }) .unwrap()?; @@ -47,16 +44,18 @@ pub fn add_key>( Ok(slot) } -pub fn remove_keyslots>(path: P, exclude: &[u32]) -> Fido2LuksResult<()> { +pub fn remove_keyslots>(path: P, exclude: &[u32]) -> Fido2LuksResult { let mut device = load_device_handle(path)?; let mut slot = 0; let mut handle; + let mut destroyed = 0; loop { handle = device.keyslot_handle(Some(slot)); match handle.status()? { KeyslotInfo::Inactive => continue, KeyslotInfo::Active if !exclude.contains(&slot) => { - dbg!((slot, handle.destroy()?)); + handle.destroy()?; + destroyed += 1; } _ => (), } @@ -66,7 +65,7 @@ pub fn remove_keyslots>(path: P, exclude: &[u32]) -> Fido2LuksRes } slot += 1; } - Ok(()) + Ok(destroyed) } pub fn replace_key>(