#![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 saveEncryptedContents(request: &request) {
let responseData = response::MakeSaveResponse();
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, "save"),
(errors::field::FILE, &request.File),
])),
);
}
if request.Contents.is_empty() { eprintln!("The entry contents is missing"); response::SendErrorAndExit( errors::Code::EmptyContents,
Some(response::params_of(&[
(errors::field::MESSAGE, "The entry contents is missing"),
(errors::field::ACTION, "save"),
])),
);
}
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, "save"),
(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, "save"),
(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, "save"),
(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, "save"),
(errors::field::ERROR, &e),
])),
);
}
}
}
let filePath = normalizedStorePath.join(&request.File);
let recipients = match helpers::DetectGpgRecipients(&filePath) { Ok(r) => r,
Err(e) => { eprintln!("Unable to determine recipients for the gpg encryption: {e}"); response::SendErrorAndExit( errors::Code::UnableToDetermineGpgRecipients,
Some(response::params_of(&[
(errors::field::MESSAGE, "Unable to determine recipients for the gpg encryption"),
(errors::field::ACTION, "save"),
(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),
])),
);
}
};
if let Err(e) = helpers::GpgEncryptFile(&filePath, &request.Contents, &recipients, &gpgPath) { eprintln!(
"Unable to encrypt the password file '{}' in the password store '{:?}': {}",
request.File, store, e
);
response::SendErrorAndExit( errors::Code::UnableToEncryptPasswordFile,
Some(response::params_of(&[
(errors::field::MESSAGE, "Unable to encrypt the password file"),
(errors::field::ACTION, "save"),
(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 _: () = ();