#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(unused_assignments)]
use crate::ported::errors;
use crate::ported::helpers;
use crate::ported::request::common::normalizePasswordStorePath;
use crate::ported::request::process::request;
use crate::ported::response;
pub fn fetchDecryptedContents(request: &request) {
let mut responseData = response::MakeFetchResponse();
if !request.File.ends_with(".gpg") {
eprintln!(
"The requested password file '{}' does not have the expected '.gpg' extension",
request.File
);
response::SendErrorAndExit(
errors::Code::InvalidPasswordFileExtension,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"The requested password file does not have the expected '.gpg' extension",
),
(errors::field::ACTION, "fetch"),
(errors::field::FILE, &request.File),
])),
);
}
let store = match request.Settings.Stores.get(&request.StoreID) {
Some(s) => s.clone(),
None => {
eprintln!(
"The password store with ID '{}' is not present in the list of stores '{:?}'",
request.StoreID, request.Settings.Stores
);
response::SendErrorAndExit(
errors::Code::InvalidPasswordStore,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"The password store is not present in the list of stores",
),
(errors::field::ACTION, "fetch"),
(errors::field::STORE_ID, &request.StoreID),
])),
);
}
};
let normalizedStorePath = match normalizePasswordStorePath(&store.Path) {
Ok(p) => p,
Err(e) => {
eprintln!(
"The password store '{:?}' is not accessible at its location: {}",
store, e
);
response::SendErrorAndExit(
errors::Code::InaccessiblePasswordStore,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"The password store is not accessible",
),
(errors::field::ACTION, "fetch"),
(errors::field::ERROR, &e),
(errors::field::STORE_ID, &store.ID),
(errors::field::STORE_NAME, &store.Name),
(errors::field::STORE_PATH, &store.Path),
])),
);
}
};
let mut store = store; store.Path = normalizedStorePath.to_string_lossy().into_owned();
let mut gpgPath: String = String::new(); if !request.Settings.GpgPath.is_empty() || !store.Settings.GpgPath.is_empty() {
if !request.Settings.GpgPath.is_empty() {
gpgPath = request.Settings.GpgPath.clone(); } else {
gpgPath = store.Settings.GpgPath.clone(); }
if let Err(e) = helpers::ValidateGpgBinary(&gpgPath) {
eprintln!("The provided gpg binary path '{gpgPath}' is invalid: {e}"); response::SendErrorAndExit(
errors::Code::InvalidGpgPath,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"The provided gpg binary path is invalid",
),
(errors::field::ACTION, "fetch"),
(errors::field::ERROR, &e),
(errors::field::GPG_PATH, &gpgPath),
])),
);
}
} else {
match helpers::DetectGpgBinary() {
Ok(p) => gpgPath = p,
Err(e) => {
eprintln!("Unable to detect the location of the gpg binary: {e}"); response::SendErrorAndExit(
errors::Code::UnableToDetectGpgPath,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"Unable to detect the location of the gpg binary",
),
(errors::field::ACTION, "fetch"),
(errors::field::ERROR, &e),
])),
);
}
}
}
let file_path = normalizedStorePath.join(&request.File); match helpers::GpgDecryptFile(&file_path, &gpgPath) {
Ok(contents) => {
responseData.Contents = contents;
}
Err(e) => {
eprintln!(
"Unable to decrypt the password file '{}' in the password store '{:?}': {}",
request.File, store, e
);
response::SendErrorAndExit(
errors::Code::UnableToDecryptPasswordFile,
Some(response::params_of(&[
(
errors::field::MESSAGE,
"Unable to decrypt the password file",
),
(errors::field::ACTION, "fetch"),
(errors::field::ERROR, &e),
(errors::field::FILE, &request.File),
(errors::field::STORE_ID, &store.ID),
(errors::field::STORE_NAME, &store.Name),
(errors::field::STORE_PATH, &store.Path),
])),
);
}
}
response::SendOk(responseData); }
#[allow(non_snake_case)]
const _: () = ();