1 //! Tools and utilities
3 //! This is a collection of small and useful tools.
6 use anyhow
::{bail, format_err, Error}
;
7 use openssl
::hash
::{hash, DigestBytes, MessageDigest}
;
11 client
::SimpleHttpOptions
,
24 pub mod parallel_handler
;
25 pub use parallel_handler
::ParallelHandler
;
27 /// Shortcut for md5 sums.
28 pub fn md5sum(data
: &[u8]) -> Result
<DigestBytes
, Error
> {
29 hash(MessageDigest
::md5(), data
).map_err(Error
::from
)
32 pub fn get_hardware_address() -> Result
<String
, Error
> {
33 static FILENAME
: &str = "/etc/ssh/ssh_host_rsa_key.pub";
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
))?
;
40 Ok(proxmox
::tools
::bin_to_hex(&digest
).to_uppercase())
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.");
51 /// Detect modified configuration files
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.");
61 /// An easy way to convert types to Any
63 /// Mostly useful to downcast trait objects (see RpcEnvironment).
65 fn as_any(&self) -> &dyn Any
;
68 impl<T
: Any
> AsAny
for T
{
69 fn as_any(&self) -> &dyn Any
{
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";
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
{
82 user_agent
: Some(DEFAULT_USER_AGENT_STRING
.to_string()),
83 tcp_keepalive
: Some(PROXMOX_BACKUP_TCP_KEEPALIVE_TIME
),
87 SimpleHttp
::with_options(options
)
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
);