]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools/mod.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / src / tools / mod.rs
CommitLineData
51b499db
DM
1//! Tools and utilities
2//!
3//! This is a collection of small and useful tools.
6100071f 4use std::any::Any;
365bb90f 5
f7d4e4b5 6use anyhow::{bail, format_err, Error};
c5946faf 7use openssl::hash::{hash, DigestBytes, MessageDigest};
0fe5d605 8
1d781c5b 9use proxmox_http::{
7d2be91b
FG
10 client::SimpleHttp,
11 client::SimpleHttpOptions,
12 ProxyConfig,
13};
57889533 14
e6513bd5 15pub mod apt;
bc5c1a9a 16pub mod config;
10effc98 17pub mod disks;
e5ef69ec 18
fb01fd3a 19pub mod statistics;
7b22fb25 20pub mod subscription;
fb01fd3a
WB
21pub mod systemd;
22pub mod ticket;
f1d99e3f 23
fb01fd3a
WB
24pub mod parallel_handler;
25pub use parallel_handler::ParallelHandler;
3c9b3702 26
c5946faf
WB
27/// Shortcut for md5 sums.
28pub fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
29 hash(MessageDigest::md5(), data).map_err(Error::from)
30}
31
7e13b2d6 32pub fn get_hardware_address() -> Result<String, Error> {
1631c54f 33 static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
7e13b2d6 34
25877d05 35 let contents = proxmox_sys::fs::file_get_contents(FILENAME)
72c0e102
TL
36 .map_err(|e| format_err!("Error getting host key - {}", e))?;
37 let digest = md5sum(&contents)
38 .map_err(|e| format_err!("Error digesting host key - {}", e))?;
7e13b2d6 39
25877d05 40 Ok(hex::encode(&digest).to_uppercase())
7e13b2d6 41}
22968600 42
af2fddea
DM
43pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
44 if digest1 != digest2 {
6100071f 45 bail!("detected modified configuration - file changed by other user? Try again.");
af2fddea
DM
46 }
47 Ok(())
48}
b9903d63 49
af53186e 50
002a191a
DM
51/// Detect modified configuration files
52///
add5861e 53/// This function fails with a reasonable error message if checksums do not match.
002a191a
DM
54pub fn detect_modified_configuration_file(digest1: &[u8;32], digest2: &[u8;32]) -> Result<(), Error> {
55 if digest1 != digest2 {
a4ba60be 56 bail!("detected modified configuration - file changed by other user? Try again.");
002a191a
DM
57 }
58 Ok(())
59}
60
2edc341b
DM
61/// An easy way to convert types to Any
62///
63/// Mostly useful to downcast trait objects (see RpcEnvironment).
64pub trait AsAny {
dd5495d6 65 fn as_any(&self) -> &dyn Any;
2edc341b
DM
66}
67
68impl<T: Any> AsAny for T {
6100071f
WB
69 fn as_any(&self) -> &dyn Any {
70 self
71 }
2edc341b 72}
8a1028e0 73
32413921
FG
74/// The default 2 hours are far too long for PBS
75pub const PROXMOX_BACKUP_TCP_KEEPALIVE_TIME: u32 = 120;
57889533
FG
76pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-backup-client/1.0";
77
78/// Returns a new instance of `SimpleHttp` configured for PBS usage.
79pub fn pbs_simple_http(proxy_config: Option<ProxyConfig>) -> SimpleHttp {
80 let options = SimpleHttpOptions {
81 proxy_config,
82 user_agent: Some(DEFAULT_USER_AGENT_STRING.to_string()),
83 tcp_keepalive: Some(PROXMOX_BACKUP_TCP_KEEPALIVE_TIME),
84 ..Default::default()
85 };
86
87 SimpleHttp::with_options(options)
88}
32413921 89
ac7513e3
DM
90pub fn setup_safe_path_env() {
91 std::env::set_var("PATH", "/sbin:/bin:/usr/sbin:/usr/bin");
92 // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
93 for name in &["IFS", "CDPATH", "ENV", "BASH_ENV"] {
94 std::env::remove_var(name);
95 }
96}