vaultbackup-rs is not part of workspace but still needs formatting...

This commit is contained in:
bunnie 2024-01-15 01:03:45 +08:00
parent 01fe4700d2
commit c411ee881a
4 changed files with 73 additions and 84 deletions

View file

@ -1,5 +1,6 @@
use anyhow::{Result, anyhow, bail};
use std::str::FromStr; use std::str::FromStr;
use anyhow::{anyhow, bail, Result};
use protobuf::Message; use protobuf::Message;
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs")); include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
@ -47,40 +48,48 @@ pub fn otpauth_to_entry(uri: &url::Url) -> Result<backup::TotpEntry, anyhow::Err
Ok(t) Ok(t)
} }
fn migration_payload_to_entry(param: otpauth_migration::migration_payload::OtpParameters) -> Result<backup::TotpEntry, anyhow::Error> { fn migration_payload_to_entry(
param: otpauth_migration::migration_payload::OtpParameters,
) -> Result<backup::TotpEntry, anyhow::Error> {
match param.type_.enum_value_or_default() { match param.type_.enum_value_or_default() {
otpauth_migration::migration_payload::OtpType::OTP_TYPE_TOTP => { otpauth_migration::migration_payload::OtpType::OTP_TYPE_TOTP => {
let mut t = backup::TotpEntry::default(); let mut t = backup::TotpEntry::default();
t.step_seconds = 30; t.step_seconds = 30;
match param.digits.enum_value_or_default() { match param.digits.enum_value_or_default() {
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_UNSPECIFIED => otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_UNSPECIFIED => {
t.digit_count = 6, t.digit_count = 6
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_SIX => }
t.digit_count = 6, otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_SIX => t.digit_count = 6,
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_EIGHT => otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_EIGHT => t.digit_count = 8,
t.digit_count = 8,
} }
match param.algorithm.enum_value_or_default() { match param.algorithm.enum_value_or_default() {
otpauth_migration::migration_payload::Algorithm::ALGORITHM_UNSPECIFIED => otpauth_migration::migration_payload::Algorithm::ALGORITHM_UNSPECIFIED => {
t.algorithm = backup::HashAlgorithms::SHA1, t.algorithm = backup::HashAlgorithms::SHA1
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA1 => }
t.algorithm = backup::HashAlgorithms::SHA1, otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA1 => {
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA256 => t.algorithm = backup::HashAlgorithms::SHA1
t.algorithm = backup::HashAlgorithms::SHA256, }
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA512 => otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA256 => {
t.algorithm = backup::HashAlgorithms::SHA512, t.algorithm = backup::HashAlgorithms::SHA256
otpauth_migration::migration_payload::Algorithm::ALGORITHM_MD5 => }
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA512 => {
t.algorithm = backup::HashAlgorithms::SHA512
}
otpauth_migration::migration_payload::Algorithm::ALGORITHM_MD5 => {
bail!("ALGORITHM_MD5 not supported") bail!("ALGORITHM_MD5 not supported")
}
} }
t.name = param.name; t.name = param.name;
set_issuer(&mut t, param.issuer); set_issuer(&mut t, param.issuer);
t.shared_secret = base32::encode(base32::Alphabet::RFC4648 { padding: false }, &param.secret); t.shared_secret = base32::encode(base32::Alphabet::RFC4648 { padding: false }, &param.secret);
Ok(t) Ok(t)
} }
otpauth_migration::migration_payload::OtpType::OTP_TYPE_HOTP => otpauth_migration::migration_payload::OtpType::OTP_TYPE_HOTP => {
Err(anyhow!("OTP_TYPE_HOTP not supported")), Err(anyhow!("OTP_TYPE_HOTP not supported"))
otpauth_migration::migration_payload::OtpType::OTP_TYPE_UNSPECIFIED => }
Err(anyhow!("OTP_TYPE_UNSPECIFIED not supported")), otpauth_migration::migration_payload::OtpType::OTP_TYPE_UNSPECIFIED => {
Err(anyhow!("OTP_TYPE_UNSPECIFIED not supported"))
}
} }
} }

View file

@ -99,9 +99,7 @@ impl std::fmt::Display for FormatError {
impl std::error::Error for FormatError {} impl std::error::Error for FormatError {}
impl From<serde_json::Error> for FormatError { impl From<serde_json::Error> for FormatError {
fn from(e: serde_json::Error) -> FormatError { fn from(e: serde_json::Error) -> FormatError { FormatError::SerdeError(e) }
FormatError::SerdeError(e)
}
} }
impl TryFrom<Vec<u8>> for Items { impl TryFrom<Vec<u8>> for Items {

View file

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use csv; use csv;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Debug, Deserialize)] #[derive(Serialize, Debug, Deserialize)]
pub struct Entry { pub struct Entry {
@ -16,11 +16,7 @@ pub struct Items {
impl Items { impl Items {
pub fn logins(&self) -> Vec<&Entry> { pub fn logins(&self) -> Vec<&Entry> {
let ret: Vec<&Entry> = self let ret: Vec<&Entry> = self.items.iter().filter(|element| element.site.is_some()).collect();
.items
.iter()
.filter(|element| element.site.is_some())
.collect();
ret ret
} }
} }
@ -31,12 +27,8 @@ impl std::error::Error for CsvError {}
pub enum CsvError { pub enum CsvError {
Err(String), Err(String),
} }
impl From::<csv::Error> for CsvError { impl From<csv::Error> for CsvError {
fn from(err: csv::Error) -> Self { fn from(err: csv::Error) -> Self { CsvError::Err(format!("{:?}", err)) }
CsvError::Err(
format!("{:?}", err)
)
}
} }
impl std::fmt::Display for CsvError { impl std::fmt::Display for CsvError {
@ -63,6 +55,6 @@ impl TryFrom<std::fs::File> for Items {
break; break;
} }
} }
Ok(Items{ items: ret }) Ok(Items { items: ret })
} }
} }

View file

@ -1,11 +1,12 @@
mod bitwarden;
mod authenticator; mod authenticator;
mod bitwarden;
mod csvpass; mod csvpass;
use std::io::BufRead;
use std::time::Instant;
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use clap::Subcommand; use clap::Subcommand;
use std::time::Instant;
use std::io::BufRead;
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs")); include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
@ -144,8 +145,11 @@ fn main() -> Result<()> {
let mut pw = backup::PasswordEntry::default(); let mut pw = backup::PasswordEntry::default();
if item.username.is_none() || item.site.is_none() { if item.username.is_none() || item.site.is_none() {
log::error!("(non-fatal) entry {} is missing username and/or site. Ignoring entry.", idx); log::error!(
continue "(non-fatal) entry {} is missing username and/or site. Ignoring entry.",
idx
);
continue;
} }
pw.password = item.password.as_ref().unwrap_or(&String::new()).clone(); pw.password = item.password.as_ref().unwrap_or(&String::new()).clone();
pw.username = item.username.as_ref().unwrap().clone(); pw.username = item.username.as_ref().unwrap().clone();
@ -155,10 +159,7 @@ fn main() -> Result<()> {
passwords.0.push(pw); passwords.0.push(pw);
} }
std::fs::write( std::fs::write("csv_to_vault_passwords.json", serde_json::ser::to_vec(&passwords)?)?;
"csv_to_vault_passwords.json",
serde_json::ser::to_vec(&passwords)?,
)?;
Ok(()) Ok(())
} }
@ -177,7 +178,7 @@ fn main() -> Result<()> {
Ok(()) => (), Ok(()) => (),
Err(err) => { Err(err) => {
log::error!("entry {} is invalid: {}", idx, err); log::error!("entry {} is invalid: {}", idx, err);
continue continue;
} }
} }
@ -196,10 +197,8 @@ fn main() -> Result<()> {
t.algorithm = backup::HashAlgorithms::SHA256; t.algorithm = backup::HashAlgorithms::SHA256;
t.digit_count = 6; t.digit_count = 6;
t.step_seconds = 30; t.step_seconds = 30;
t.shared_secret = totp t.shared_secret =
.strip_prefix("otpauth://totp/") totp.strip_prefix("otpauth://totp/").unwrap_or(&totp).to_string();
.unwrap_or(&totp)
.to_string();
totps.0.push(t); totps.0.push(t);
} }
@ -211,10 +210,7 @@ fn main() -> Result<()> {
)?; )?;
if totps.0.len() > 0 { if totps.0.len() > 0 {
std::fs::write( std::fs::write("bitwarden_to_vault_totps.json", serde_json::ser::to_vec(&totps)?)?;
"bitwarden_to_vault_totps.json",
serde_json::ser::to_vec(&totps)?,
)?;
} }
Ok(()) Ok(())
@ -225,9 +221,7 @@ fn main() -> Result<()> {
for uri in std::io::BufReader::new(f).lines() { for uri in std::io::BufReader::new(f).lines() {
let uri = url::Url::parse(&uri?)?; let uri = url::Url::parse(&uri?)?;
match (uri.scheme(), uri.host_str()) { match (uri.scheme(), uri.host_str()) {
("otpauth", Some("totp")) => { ("otpauth", Some("totp")) => totps.0.push(authenticator::otpauth_to_entry(&uri)?),
totps.0.push(authenticator::otpauth_to_entry(&uri)?)
}
("otpauth-migration", Some("offline")) => { ("otpauth-migration", Some("offline")) => {
for t in authenticator::otpauth_migration_to_entries(&uri)? { for t in authenticator::otpauth_migration_to_entries(&uri)? {
totps.0.push(t); totps.0.push(t);
@ -239,10 +233,7 @@ fn main() -> Result<()> {
} }
} }
std::fs::write( std::fs::write("authenticator_to_vault_totps.json", serde_json::ser::to_vec(&totps)?)?;
"authenticator_to_vault_totps.json",
serde_json::ser::to_vec(&totps)?,
)?;
Ok(()) Ok(())
} }
@ -264,16 +255,17 @@ fn main() -> Result<()> {
Ok(we) => we, Ok(we) => we,
Err(error) => match error { Err(error) => match error {
ctaphid::error::Error::DeviceError( ctaphid::error::Error::DeviceError(ctaphid::error::DeviceError::Unknown(
ctaphid::error::DeviceError::Unknown(value), value,
) => { )) => {
if value == 88 { if value == 88 {
break; break;
} }
if value == 44 { if value == 44 {
return Err( return Err(std::io::Error::new(
std::io::Error::new(std::io::ErrorKind::PermissionDenied, std::io::ErrorKind::PermissionDenied,
"Host readout is not enabled, unable to proceed!\nPlease select 'Enable host readout' from the vault context menu first."))?; "Host readout is not enabled, unable to proceed!\nPlease select 'Enable host readout' from the vault context menu first.",
))?;
} }
return Err(error)?; return Err(error)?;
@ -315,24 +307,22 @@ fn main() -> Result<()> {
log::debug!("sending chunk {}", idx); log::debug!("sending chunk {}", idx);
let chunk = &chunk; let chunk = &chunk;
let chunk_bytes: Vec<u8> = chunk.into(); let chunk_bytes: Vec<u8> = chunk.into();
let vcres = let vcres = match device.vendor_command(ctaphid::command::VendorCommand::H71, &chunk_bytes) {
match device.vendor_command(ctaphid::command::VendorCommand::H71, &chunk_bytes) { Ok(vcres) => vcres,
Ok(vcres) => vcres, Err(error) => match error {
Err(error) => match error { ctaphid::error::Error::DeviceError(ctaphid::error::DeviceError::Unknown(value)) => {
ctaphid::error::Error::DeviceError( if value == 44 {
ctaphid::error::DeviceError::Unknown(value), return Err(std::io::Error::new(
) => { std::io::ErrorKind::PermissionDenied,
if value == 44 { "Host readout is not enabled, unable to proceed!\nPlease select 'Enable host readout' from the vault context menu first.",
return Err( ))?;
std::io::Error::new(std::io::ErrorKind::PermissionDenied,
"Host readout is not enabled, unable to proceed!\nPlease select 'Enable host readout' from the vault context menu first."))?;
}
return Err(error)?;
} }
_ => return Err(error)?,
return Err(error)?;
} }
}; _ => return Err(error)?,
},
};
if vcres.eq(backup::CONTINUE_RESPONSE) { if vcres.eq(backup::CONTINUE_RESPONSE) {
log::debug!("received CONTINUE response"); log::debug!("received CONTINUE response");