]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/mod.rs
03b9da863d53a157f82ea960d9dc81245a4bc65d
[proxmox-backup.git] / src / tools / mod.rs
1 //! Tools and utilities
2 //!
3 //! This is a collection of small and useful tools.
4 use std::any::Any;
5
6 use anyhow::{bail, format_err, Error};
7 use openssl::hash::{hash, DigestBytes, MessageDigest};
8
9 use proxmox_http::{
10 client::SimpleHttp,
11 client::SimpleHttpOptions,
12 ProxyConfig,
13 };
14
15 pub mod apt;
16 pub mod config;
17 pub mod disks;
18
19 pub mod statistics;
20 pub mod subscription;
21 pub mod systemd;
22 pub mod ticket;
23
24 pub mod parallel_handler;
25 pub use parallel_handler::ParallelHandler;
26
27 /// Shortcut for md5 sums.
28 pub fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
29 hash(MessageDigest::md5(), data).map_err(Error::from)
30 }
31
32 pub fn get_hardware_address() -> Result<String, Error> {
33 static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
34
35 let contents = proxmox::tools::fs::file_get_contents(FILENAME)
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))?;
39
40 Ok(proxmox::tools::bin_to_hex(&digest).to_uppercase())
41 }
42
43 pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
44 if digest1 != digest2 {
45 bail!("detected modified configuration - file changed by other user? Try again.");
46 }
47 Ok(())
48 }
49
50
51 /// Detect modified configuration files
52 ///
53 /// This function fails with a reasonable error message if checksums do not match.
54 pub fn detect_modified_configuration_file(digest1: &[u8;32], digest2: &[u8;32]) -> Result<(), Error> {
55 if digest1 != digest2 {
56 bail!("detected modified configuration - file changed by other user? Try again.");
57 }
58 Ok(())
59 }
60
61 /// An easy way to convert types to Any
62 ///
63 /// Mostly useful to downcast trait objects (see RpcEnvironment).
64 pub trait AsAny {
65 fn as_any(&self) -> &dyn Any;
66 }
67
68 impl<T: Any> AsAny for T {
69 fn as_any(&self) -> &dyn Any {
70 self
71 }
72 }
73
74 /// The default 2 hours are far too long for PBS
75 pub const PROXMOX_BACKUP_TCP_KEEPALIVE_TIME: u32 = 120;
76 pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-backup-client/1.0";
77
78 /// Returns a new instance of `SimpleHttp` configured for PBS usage.
79 pub 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 }
89
90 pub 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 }