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 anyhow::{anyhow, bail, Result};
use protobuf::Message;
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)
}
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() {
otpauth_migration::migration_payload::OtpType::OTP_TYPE_TOTP => {
let mut t = backup::TotpEntry::default();
t.step_seconds = 30;
match param.digits.enum_value_or_default() {
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_UNSPECIFIED =>
t.digit_count = 6,
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_SIX =>
t.digit_count = 6,
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_EIGHT =>
t.digit_count = 8,
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_UNSPECIFIED => {
t.digit_count = 6
}
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_SIX => t.digit_count = 6,
otpauth_migration::migration_payload::DigitCount::DIGIT_COUNT_EIGHT => t.digit_count = 8,
}
match param.algorithm.enum_value_or_default() {
otpauth_migration::migration_payload::Algorithm::ALGORITHM_UNSPECIFIED =>
t.algorithm = backup::HashAlgorithms::SHA1,
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA1 =>
t.algorithm = backup::HashAlgorithms::SHA1,
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA256 =>
t.algorithm = backup::HashAlgorithms::SHA256,
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA512 =>
t.algorithm = backup::HashAlgorithms::SHA512,
otpauth_migration::migration_payload::Algorithm::ALGORITHM_MD5 =>
otpauth_migration::migration_payload::Algorithm::ALGORITHM_UNSPECIFIED => {
t.algorithm = backup::HashAlgorithms::SHA1
}
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA1 => {
t.algorithm = backup::HashAlgorithms::SHA1
}
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA256 => {
t.algorithm = backup::HashAlgorithms::SHA256
}
otpauth_migration::migration_payload::Algorithm::ALGORITHM_SHA512 => {
t.algorithm = backup::HashAlgorithms::SHA512
}
otpauth_migration::migration_payload::Algorithm::ALGORITHM_MD5 => {
bail!("ALGORITHM_MD5 not supported")
}
}
t.name = param.name;
set_issuer(&mut t, param.issuer);
t.shared_secret = base32::encode(base32::Alphabet::RFC4648 { padding: false }, &param.secret);
Ok(t)
}
otpauth_migration::migration_payload::OtpType::OTP_TYPE_HOTP =>
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_HOTP => {
Err(anyhow!("OTP_TYPE_HOTP 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 From<serde_json::Error> for FormatError {
fn from(e: serde_json::Error) -> FormatError {
FormatError::SerdeError(e)
}
fn from(e: serde_json::Error) -> FormatError { FormatError::SerdeError(e) }
}
impl TryFrom<Vec<u8>> for Items {

View file

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

View file

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