1 //! Tools and utilities
3 //! This is a collection of small and useful tools.
5 use std
::borrow
::Borrow
;
6 use std
::collections
::HashMap
;
7 use std
::hash
::BuildHasher
;
9 use std
::io
::{self, BufRead, Read, Seek, SeekFrom}
;
10 use std
::os
::unix
::io
::RawFd
;
13 use anyhow
::{bail, format_err, Error}
;
14 use serde_json
::Value
;
15 use openssl
::hash
::{hash, DigestBytes, MessageDigest}
;
16 use percent_encoding
::{utf8_percent_encode, AsciiSet}
;
18 pub use proxmox
::tools
::fd
::Fd
;
45 pub mod parallel_handler
;
46 pub use parallel_handler
::ParallelHandler
;
48 mod wrapped_reader_stream
;
49 pub use wrapped_reader_stream
::{AsyncReaderStream, StdChannelStream, WrappedReaderStream}
;
51 mod async_channel_writer
;
52 pub use async_channel_writer
::AsyncChannelWriter
;
54 mod std_channel_writer
;
55 pub use std_channel_writer
::StdChannelWriter
;
58 pub use process_locker
::{ProcessLocker, ProcessLockExclusiveGuard, ProcessLockSharedGuard}
;
61 pub use file_logger
::{FileLogger, FileLogOptions}
;
64 pub use broadcast_future
::{BroadcastData, BroadcastFuture}
;
66 /// The `BufferedRead` trait provides a single function
67 /// `buffered_read`. It returns a reference to an internal buffer. The
68 /// purpose of this traid is to avoid unnecessary data copies.
69 pub trait BufferedRead
{
70 /// This functions tries to fill the internal buffers, then
71 /// returns a reference to the available data. It returns an empty
72 /// buffer if `offset` points to the end of the file.
73 fn buffered_read(&mut self, offset
: u64) -> Result
<&[u8], Error
>;
76 pub fn json_object_to_query(data
: Value
) -> Result
<String
, Error
> {
77 let mut query
= url
::form_urlencoded
::Serializer
::new(String
::new());
79 let object
= data
.as_object().ok_or_else(|| {
80 format_err
!("json_object_to_query: got wrong data type (expected object).")
83 for (key
, value
) in object
{
86 query
.append_pair(key
, &b
.to_string());
89 query
.append_pair(key
, &n
.to_string());
92 query
.append_pair(key
, &s
);
94 Value
::Array(arr
) => {
98 query
.append_pair(key
, &b
.to_string());
100 Value
::Number(n
) => {
101 query
.append_pair(key
, &n
.to_string());
103 Value
::String(s
) => {
104 query
.append_pair(key
, &s
);
107 "json_object_to_query: unable to handle complex array data types."
112 _
=> bail
!("json_object_to_query: unable to handle complex data types."),
119 pub fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
120 match param
[name
].as_str() {
122 None
=> bail
!("missing parameter '{}'", name
),
126 pub fn required_string_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
127 match param
[name
].as_str() {
129 None
=> bail
!("missing property '{}'", name
),
133 pub fn required_integer_param(param
: &Value
, name
: &str) -> Result
<i64, Error
> {
134 match param
[name
].as_i64() {
136 None
=> bail
!("missing parameter '{}'", name
),
140 pub fn required_integer_property(param
: &Value
, name
: &str) -> Result
<i64, Error
> {
141 match param
[name
].as_i64() {
143 None
=> bail
!("missing property '{}'", name
),
147 pub fn required_array_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
[Value
], Error
> {
148 match param
[name
].as_array() {
150 None
=> bail
!("missing parameter '{}'", name
),
154 pub fn required_array_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
[Value
], Error
> {
155 match param
[name
].as_array() {
157 None
=> bail
!("missing property '{}'", name
),
161 pub fn complete_file_name
<S
>(arg
: &str, _param
: &HashMap
<String
, String
, S
>) -> Vec
<String
>
165 let mut result
= vec
![];
167 use nix
::fcntl
::AtFlags
;
168 use nix
::fcntl
::OFlag
;
169 use nix
::sys
::stat
::Mode
;
171 let mut dirname
= std
::path
::PathBuf
::from(if arg
.is_empty() { "./" }
else { arg }
);
173 let is_dir
= match nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &dirname
, AtFlags
::empty()) {
174 Ok(stat
) => (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
,
179 if let Some(parent
) = dirname
.parent() {
180 dirname
= parent
.to_owned();
185 match nix
::dir
::Dir
::openat(libc
::AT_FDCWD
, &dirname
, OFlag
::O_DIRECTORY
, Mode
::empty()) {
187 Err(_
) => return result
,
190 for item
in dir
.iter() {
191 if let Ok(entry
) = item
{
192 if let Ok(name
) = entry
.file_name().to_str() {
193 if name
== "." || name
== ".." {
196 let mut newpath
= dirname
.clone();
200 nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &newpath
, AtFlags
::empty())
202 if (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
{
204 if let Some(newpath
) = newpath
.to_str() {
205 result
.push(newpath
.to_owned());
210 if let Some(newpath
) = newpath
.to_str() {
211 result
.push(newpath
.to_owned());
220 /// Scan directory for matching file names.
222 /// Scan through all directory entries and call `callback()` function
223 /// if the entry name matches the regular expression. This function
224 /// used unix `openat()`, so you can pass absolute or relative file
225 /// names. This function simply skips non-UTF8 encoded names.
226 pub fn scandir
<P
, F
>(
229 regex
: ®ex
::Regex
,
231 ) -> Result
<(), Error
>
233 F
: FnMut(RawFd
, &str, nix
::dir
::Type
) -> Result
<(), Error
>,
234 P
: ?Sized
+ nix
::NixPath
,
236 for entry
in self::fs
::scan_subdir(dirfd
, path
, regex
)?
{
238 let file_type
= match entry
.file_type() {
239 Some(file_type
) => file_type
,
240 None
=> bail
!("unable to detect file type"),
245 unsafe { entry.file_name_utf8_unchecked() }
,
252 /// Shortcut for md5 sums.
253 pub fn md5sum(data
: &[u8]) -> Result
<DigestBytes
, Error
> {
254 hash(MessageDigest
::md5(), data
).map_err(Error
::from
)
257 pub fn get_hardware_address() -> Result
<String
, Error
> {
258 static FILENAME
: &str = "/etc/ssh/ssh_host_rsa_key.pub";
260 let contents
= proxmox
::tools
::fs
::file_get_contents(FILENAME
)
261 .map_err(|e
| format_err
!("Error getting host key - {}", e
))?
;
262 let digest
= md5sum(&contents
)
263 .map_err(|e
| format_err
!("Error digesting host key - {}", e
))?
;
265 Ok(proxmox
::tools
::bin_to_hex(&digest
).to_uppercase())
268 pub fn assert_if_modified(digest1
: &str, digest2
: &str) -> Result
<(), Error
> {
269 if digest1
!= digest2
{
270 bail
!("detected modified configuration - file changed by other user? Try again.");
275 /// Extract a specific cookie from cookie header.
276 /// We assume cookie_name is already url encoded.
277 pub fn extract_cookie(cookie
: &str, cookie_name
: &str) -> Option
<String
> {
278 for pair
in cookie
.split('
;'
) {
279 let (name
, value
) = match pair
.find('
='
) {
280 Some(i
) => (pair
[..i
].trim(), pair
[(i
+ 1)..].trim()),
281 None
=> return None
, // Cookie format error
284 if name
== cookie_name
{
285 use percent_encoding
::percent_decode
;
286 if let Ok(value
) = percent_decode(value
.as_bytes()).decode_utf8() {
287 return Some(value
.into());
289 return None
; // Cookie format error
297 /// percent encode a url component
298 pub fn percent_encode_component(comp
: &str) -> String
{
299 utf8_percent_encode(comp
, percent_encoding
::NON_ALPHANUMERIC
).to_string()
302 pub fn join
<S
: Borrow
<str>>(data
: &[S
], sep
: char) -> String
{
303 let mut list
= String
::new();
306 if !list
.is_empty() {
309 list
.push_str(item
.borrow());
315 /// Detect modified configuration files
317 /// This function fails with a reasonable error message if checksums do not match.
318 pub fn detect_modified_configuration_file(digest1
: &[u8;32], digest2
: &[u8;32]) -> Result
<(), Error
> {
319 if digest1
!= digest2
{
320 bail
!("detected modified configuration - file changed by other user? Try again.");
325 /// normalize uri path
327 /// Do not allow ".", "..", or hidden files ".XXXX"
328 /// Also remove empty path components
329 pub fn normalize_uri_path(path
: &str) -> Result
<(String
, Vec
<&str>), Error
> {
330 let items
= path
.split('
/'
);
332 let mut path
= String
::new();
333 let mut components
= vec
![];
339 if name
.starts_with('
.'
) {
340 bail
!("Path contains illegal components.");
344 components
.push(name
);
347 Ok((path
, components
))
350 /// Helper to check result from std::process::Command output
352 /// The exit_code_check() function should return true if the exit code
353 /// is considered successful.
354 pub fn command_output(
355 output
: std
::process
::Output
,
356 exit_code_check
: Option
<fn(i32) -> bool
>,
357 ) -> Result
<Vec
<u8>, Error
> {
359 if !output
.status
.success() {
360 match output
.status
.code() {
362 let is_ok
= match exit_code_check
{
363 Some(check_fn
) => check_fn(code
),
367 let msg
= String
::from_utf8(output
.stderr
)
368 .map(|m
| if m
.is_empty() { String::from("no error message") }
else { m }
)
369 .unwrap_or_else(|_
| String
::from("non utf8 error message (suppressed)"));
371 bail
!("status code: {} - {}", code
, msg
);
374 None
=> bail
!("terminated by signal"),
381 /// Helper to check result from std::process::Command output, returns String.
383 /// The exit_code_check() function should return true if the exit code
384 /// is considered successful.
385 pub fn command_output_as_string(
386 output
: std
::process
::Output
,
387 exit_code_check
: Option
<fn(i32) -> bool
>,
388 ) -> Result
<String
, Error
> {
389 let output
= command_output(output
, exit_code_check
)?
;
390 let output
= String
::from_utf8(output
)?
;
395 mut command
: std
::process
::Command
,
396 exit_code_check
: Option
<fn(i32) -> bool
>,
397 ) -> Result
<String
, Error
> {
399 let output
= command
.output()
400 .map_err(|err
| format_err
!("failed to execute {:?} - {}", command
, err
))?
;
402 let output
= command_output_as_string(output
, exit_code_check
)
403 .map_err(|err
| format_err
!("command {:?} failed - {}", command
, err
))?
;
408 pub fn fd_change_cloexec(fd
: RawFd
, on
: bool
) -> Result
<(), Error
> {
409 use nix
::fcntl
::{fcntl, FdFlag, F_GETFD, F_SETFD}
;
410 let mut flags
= FdFlag
::from_bits(fcntl(fd
, F_GETFD
)?
)
411 .ok_or_else(|| format_err
!("unhandled file flags"))?
; // nix crate is stupid this way...
412 flags
.set(FdFlag
::FD_CLOEXEC
, on
);
413 fcntl(fd
, F_SETFD(flags
))?
;
417 static mut SHUTDOWN_REQUESTED
: bool
= false;
419 pub fn request_shutdown() {
421 SHUTDOWN_REQUESTED
= true;
423 crate::server
::server_shutdown();
427 pub fn shutdown_requested() -> bool
{
428 unsafe { SHUTDOWN_REQUESTED }
431 pub fn fail_on_shutdown() -> Result
<(), Error
> {
432 if shutdown_requested() {
433 bail
!("Server shutdown requested - aborting task");
438 /// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file
440 pub fn pipe() -> Result
<(Fd
, Fd
), Error
> {
441 let (pin
, pout
) = nix
::unistd
::pipe2(nix
::fcntl
::OFlag
::O_CLOEXEC
)?
;
442 Ok((Fd(pin
), Fd(pout
)))
445 /// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file
447 pub fn socketpair() -> Result
<(Fd
, Fd
), Error
> {
448 use nix
::sys
::socket
;
449 let (pa
, pb
) = socket
::socketpair(
450 socket
::AddressFamily
::Unix
,
451 socket
::SockType
::Stream
,
453 socket
::SockFlag
::SOCK_CLOEXEC
,
459 /// An easy way to convert types to Any
461 /// Mostly useful to downcast trait objects (see RpcEnvironment).
463 fn as_any(&self) -> &dyn Any
;
466 impl<T
: Any
> AsAny
for T
{
467 fn as_any(&self) -> &dyn Any
{
472 /// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}`
473 pub const DEFAULT_ENCODE_SET
: &AsciiSet
= &percent_encoding
::CONTROLS
// 0..1f and 7e
474 // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above)
477 // the DEFAULT_ENCODE_SET added:
488 /// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a
490 pub fn file_get_non_comment_lines<P: AsRef<Path>>(
492 ) -> Result<impl Iterator<Item = io::Result<String>>, Error> {
493 let path = path.as_ref();
495 Ok(io::BufReader::new(
496 File::open(path).map_err(|err| format_err!("error opening {:?}
: {}
", path, err))?,
499 .filter_map(|line| match line {
501 let line = line.trim();
502 if line.is_empty() || line.starts_with('#') {
505 Some(Ok(line.to_string()))
508 Err(err) => Some(Err(err)),
512 pub fn setup_safe_path_env() {
513 std::env::set_var("PATH
", "/sbin
:/bin
:/usr
/sbin
:/usr
/bin
");
514 // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
515 for name in &["IFS
", "CDPATH
", "ENV
", "BASH_ENV
"] {
516 std::env::remove_var(name);
520 pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] {
521 let line = match line.iter().position(|&b| !b.is_ascii_whitespace()) {
522 Some(n) => &line[n..],
525 match line.iter().rev().position(|&b| !b.is_ascii_whitespace()) {
526 Some(n) => &line[..(line.len() - n)],
531 /// Seeks to start of file and computes the SHA256 hash
532 pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
534 file.seek(SeekFrom::Start(0))?;
536 let mut hasher = openssl::sha::Sha256::new();
537 let mut buffer = proxmox::tools::vec::undefined(256*1024);
538 let mut size: u64 = 0;
541 let count = match file.read(&mut buffer) {
544 Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
547 Err(err) => return Err(err.into()),
549 size += count as u64;
550 hasher.update(&buffer[..count]);
553 let csum = hasher.finish();
558 /// Create the base run-directory.
560 /// This exists to fixate the permissions for the run *base* directory while allowing intermediate
561 /// directories after it to have different permissions.
562 pub fn create_run_dir() -> Result<(), Error> {
563 let _: bool = proxmox::tools::fs::create_path(PROXMOX_BACKUP_RUN_DIR_M!(), None, None)?;