2019-01-05 15:53:28 +00:00
|
|
|
//! Tools and utilities
|
|
|
|
//!
|
|
|
|
//! This is a collection of small and useful tools.
|
2019-08-22 08:57:56 +00:00
|
|
|
use std::any::Any;
|
2018-12-19 09:02:24 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-11-14 10:19:43 +00:00
|
|
|
use openssl::hash::{hash, DigestBytes, MessageDigest};
|
2019-01-17 11:14:02 +00:00
|
|
|
|
2021-05-17 09:29:24 +00:00
|
|
|
use proxmox_http::{
|
2021-05-14 13:44:57 +00:00
|
|
|
client::SimpleHttp,
|
|
|
|
client::SimpleHttpOptions,
|
|
|
|
ProxyConfig,
|
|
|
|
};
|
2021-05-14 13:44:56 +00:00
|
|
|
|
2020-10-31 19:40:05 +00:00
|
|
|
pub mod apt;
|
2021-04-22 14:01:58 +00:00
|
|
|
pub mod config;
|
2020-05-05 08:14:41 +00:00
|
|
|
pub mod disks;
|
2021-05-06 08:20:53 +00:00
|
|
|
|
2020-11-12 10:49:38 +00:00
|
|
|
pub mod statistics;
|
2020-10-27 11:25:59 +00:00
|
|
|
pub mod subscription;
|
2020-11-12 10:49:38 +00:00
|
|
|
pub mod systemd;
|
|
|
|
pub mod ticket;
|
2020-01-22 11:49:08 +00:00
|
|
|
|
2020-11-12 10:49:38 +00:00
|
|
|
pub mod parallel_handler;
|
|
|
|
pub use parallel_handler::ParallelHandler;
|
2020-09-25 10:13:06 +00:00
|
|
|
|
2019-11-14 10:19:43 +00:00
|
|
|
/// Shortcut for md5 sums.
|
|
|
|
pub fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
|
|
|
|
hash(MessageDigest::md5(), data).map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:50:19 +00:00
|
|
|
pub fn get_hardware_address() -> Result<String, Error> {
|
2019-01-25 11:23:47 +00:00
|
|
|
static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
|
2019-01-22 11:50:19 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
let contents = proxmox_sys::fs::file_get_contents(FILENAME)
|
2020-10-27 11:26:53 +00:00
|
|
|
.map_err(|e| format_err!("Error getting host key - {}", e))?;
|
|
|
|
let digest = md5sum(&contents)
|
|
|
|
.map_err(|e| format_err!("Error digesting host key - {}", e))?;
|
2019-01-22 11:50:19 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
Ok(hex::encode(&digest).to_uppercase())
|
2019-01-22 11:50:19 +00:00
|
|
|
}
|
2019-01-25 09:58:28 +00:00
|
|
|
|
2019-01-25 10:38:59 +00:00
|
|
|
pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
|
|
|
|
if digest1 != digest2 {
|
2019-08-22 08:57:56 +00:00
|
|
|
bail!("detected modified configuration - file changed by other user? Try again.");
|
2019-01-25 10:38:59 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-31 11:22:00 +00:00
|
|
|
|
2019-03-02 15:12:34 +00:00
|
|
|
|
2020-01-15 11:27:05 +00:00
|
|
|
/// Detect modified configuration files
|
|
|
|
///
|
2020-05-30 14:37:33 +00:00
|
|
|
/// This function fails with a reasonable error message if checksums do not match.
|
2020-01-15 11:27:05 +00:00
|
|
|
pub fn detect_modified_configuration_file(digest1: &[u8;32], digest2: &[u8;32]) -> Result<(), Error> {
|
|
|
|
if digest1 != digest2 {
|
2020-11-30 12:44:46 +00:00
|
|
|
bail!("detected modified configuration - file changed by other user? Try again.");
|
2020-01-15 11:27:05 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:05:38 +00:00
|
|
|
/// An easy way to convert types to Any
|
|
|
|
///
|
|
|
|
/// Mostly useful to downcast trait objects (see RpcEnvironment).
|
|
|
|
pub trait AsAny {
|
2019-06-07 11:10:56 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
2019-05-08 09:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Any> AsAny for T {
|
2019-08-22 08:57:56 +00:00
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
2019-05-08 09:05:38 +00:00
|
|
|
}
|
2019-12-13 10:55:52 +00:00
|
|
|
|
2021-05-14 13:44:51 +00:00
|
|
|
/// The default 2 hours are far too long for PBS
|
|
|
|
pub const PROXMOX_BACKUP_TCP_KEEPALIVE_TIME: u32 = 120;
|
2021-05-14 13:44:56 +00:00
|
|
|
pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-backup-client/1.0";
|
|
|
|
|
|
|
|
/// Returns a new instance of `SimpleHttp` configured for PBS usage.
|
|
|
|
pub fn pbs_simple_http(proxy_config: Option<ProxyConfig>) -> SimpleHttp {
|
|
|
|
let options = SimpleHttpOptions {
|
|
|
|
proxy_config,
|
|
|
|
user_agent: Some(DEFAULT_USER_AGENT_STRING.to_string()),
|
|
|
|
tcp_keepalive: Some(PROXMOX_BACKUP_TCP_KEEPALIVE_TIME),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
SimpleHttp::with_options(options)
|
|
|
|
}
|
2021-05-14 13:44:51 +00:00
|
|
|
|
2020-06-15 08:38:30 +00:00
|
|
|
pub fn setup_safe_path_env() {
|
|
|
|
std::env::set_var("PATH", "/sbin:/bin:/usr/sbin:/usr/bin");
|
|
|
|
// Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
|
|
|
|
for name in &["IFS", "CDPATH", "ENV", "BASH_ENV"] {
|
|
|
|
std::env::remove_var(name);
|
|
|
|
}
|
|
|
|
}
|