1 //! Tools and utilities
3 //! This is a collection of small and useful tools.
5 use std
::collections
::HashMap
;
6 use std
::hash
::BuildHasher
;
8 use std
::io
::{self, BufRead, ErrorKind, Read, Seek, SeekFrom}
;
9 use std
::os
::unix
::io
::RawFd
;
12 use anyhow
::{bail, format_err, Error}
;
13 use serde_json
::Value
;
14 use openssl
::hash
::{hash, DigestBytes, MessageDigest}
;
15 use percent_encoding
::AsciiSet
;
17 use proxmox
::tools
::vec
;
19 pub use proxmox
::tools
::fd
::Fd
;
43 pub use parallel_handler
::*;
45 mod wrapped_reader_stream
;
46 pub use wrapped_reader_stream
::*;
48 mod async_channel_writer
;
49 pub use async_channel_writer
::*;
52 mod std_channel_writer
;
53 pub use std_channel_writer
::*;
58 pub use process_locker
::*;
61 pub use file_logger
::*;
64 pub use broadcast_future
::*;
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 /// Split a file into equal sized chunks. The last chunk may be
77 /// smaller. Note: We cannot implement an `Iterator`, because iterators
78 /// cannot return a borrowed buffer ref (we want zero-copy)
79 pub fn file_chunker
<C
, R
>(mut file
: R
, chunk_size
: usize, mut chunk_cb
: C
) -> Result
<(), Error
>
81 C
: FnMut(usize, &[u8]) -> Result
<bool
, Error
>,
84 const READ_BUFFER_SIZE
: usize = 4 * 1024 * 1024; // 4M
86 if chunk_size
> READ_BUFFER_SIZE
{
87 bail
!("chunk size too large!");
90 let mut buf
= vec
::undefined(READ_BUFFER_SIZE
);
96 let mut tmp
= &mut buf
[..];
97 // try to read large portions, at least chunk_size
98 while pos
< chunk_size
{
99 match file
.read(tmp
) {
106 if pos
> chunk_size
{
111 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> { /* try again */ }
112 Err(e
) => bail
!("read chunk failed - {}", e
.to_string()),
116 while start
+ chunk_size
<= pos
{
117 if !(chunk_cb
)(file_pos
, &buf
[start
..start
+ chunk_size
])?
{
120 file_pos
+= chunk_size
;
125 (chunk_cb
)(file_pos
, &buf
[start
..pos
])?
;
126 //file_pos += pos - start;
130 let rest
= pos
- start
;
132 let ptr
= buf
.as_mut_ptr();
134 std
::ptr
::copy_nonoverlapping(ptr
.add(start
), ptr
, rest
);
146 pub fn json_object_to_query(data
: Value
) -> Result
<String
, Error
> {
147 let mut query
= url
::form_urlencoded
::Serializer
::new(String
::new());
149 let object
= data
.as_object().ok_or_else(|| {
150 format_err
!("json_object_to_query: got wrong data type (expected object).")
153 for (key
, value
) in object
{
156 query
.append_pair(key
, &b
.to_string());
158 Value
::Number(n
) => {
159 query
.append_pair(key
, &n
.to_string());
161 Value
::String(s
) => {
162 query
.append_pair(key
, &s
);
164 Value
::Array(arr
) => {
168 query
.append_pair(key
, &b
.to_string());
170 Value
::Number(n
) => {
171 query
.append_pair(key
, &n
.to_string());
173 Value
::String(s
) => {
174 query
.append_pair(key
, &s
);
177 "json_object_to_query: unable to handle complex array data types."
182 _
=> bail
!("json_object_to_query: unable to handle complex data types."),
189 pub fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
190 match param
[name
].as_str() {
192 None
=> bail
!("missing parameter '{}'", name
),
196 pub fn required_string_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
197 match param
[name
].as_str() {
199 None
=> bail
!("missing property '{}'", name
),
203 pub fn required_integer_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
204 match param
[name
].as_i64() {
206 None
=> bail
!("missing parameter '{}'", name
),
210 pub fn required_integer_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
211 match param
[name
].as_i64() {
213 None
=> bail
!("missing property '{}'", name
),
217 pub fn required_array_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
218 match param
[name
].as_array() {
219 Some(s
) => Ok(s
.to_vec()),
220 None
=> bail
!("missing parameter '{}'", name
),
224 pub fn required_array_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
225 match param
[name
].as_array() {
226 Some(s
) => Ok(s
.to_vec()),
227 None
=> bail
!("missing property '{}'", name
),
231 pub fn complete_file_name
<S
: BuildHasher
>(arg
: &str, _param
: &HashMap
<String
, String
, S
>) -> Vec
<String
> {
232 let mut result
= vec
![];
234 use nix
::fcntl
::AtFlags
;
235 use nix
::fcntl
::OFlag
;
236 use nix
::sys
::stat
::Mode
;
238 let mut dirname
= std
::path
::PathBuf
::from(if arg
.is_empty() { "./" }
else { arg }
);
240 let is_dir
= match nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &dirname
, AtFlags
::empty()) {
241 Ok(stat
) => (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
,
246 if let Some(parent
) = dirname
.parent() {
247 dirname
= parent
.to_owned();
252 match nix
::dir
::Dir
::openat(libc
::AT_FDCWD
, &dirname
, OFlag
::O_DIRECTORY
, Mode
::empty()) {
254 Err(_
) => return result
,
257 for item
in dir
.iter() {
258 if let Ok(entry
) = item
{
259 if let Ok(name
) = entry
.file_name().to_str() {
260 if name
== "." || name
== ".." {
263 let mut newpath
= dirname
.clone();
267 nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &newpath
, AtFlags
::empty())
269 if (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
{
271 if let Some(newpath
) = newpath
.to_str() {
272 result
.push(newpath
.to_owned());
277 if let Some(newpath
) = newpath
.to_str() {
278 result
.push(newpath
.to_owned());
287 /// Scan directory for matching file names.
289 /// Scan through all directory entries and call `callback()` function
290 /// if the entry name matches the regular expression. This function
291 /// used unix `openat()`, so you can pass absolute or relative file
292 /// names. This function simply skips non-UTF8 encoded names.
293 pub fn scandir
<P
, F
>(
296 regex
: ®ex
::Regex
,
298 ) -> Result
<(), Error
>
300 F
: FnMut(RawFd
, &str, nix
::dir
::Type
) -> Result
<(), Error
>,
301 P
: ?Sized
+ nix
::NixPath
,
303 for entry
in self::fs
::scan_subdir(dirfd
, path
, regex
)?
{
305 let file_type
= match entry
.file_type() {
306 Some(file_type
) => file_type
,
307 None
=> bail
!("unable to detect file type"),
312 unsafe { entry.file_name_utf8_unchecked() }
,
319 /// Shortcut for md5 sums.
320 pub fn md5sum(data
: &[u8]) -> Result
<DigestBytes
, Error
> {
321 hash(MessageDigest
::md5(), data
).map_err(Error
::from
)
324 pub fn get_hardware_address() -> Result
<String
, Error
> {
325 static FILENAME
: &str = "/etc/ssh/ssh_host_rsa_key.pub";
327 let contents
= proxmox
::tools
::fs
::file_get_contents(FILENAME
)?
;
328 let digest
= md5sum(&contents
)?
;
330 Ok(proxmox
::tools
::bin_to_hex(&digest
))
333 pub fn assert_if_modified(digest1
: &str, digest2
: &str) -> Result
<(), Error
> {
334 if digest1
!= digest2
{
335 bail
!("detected modified configuration - file changed by other user? Try again.");
340 /// Extract a specific cookie from cookie header.
341 /// We assume cookie_name is already url encoded.
342 pub fn extract_cookie(cookie
: &str, cookie_name
: &str) -> Option
<String
> {
343 for pair
in cookie
.split('
;'
) {
344 let (name
, value
) = match pair
.find('
='
) {
345 Some(i
) => (pair
[..i
].trim(), pair
[(i
+ 1)..].trim()),
346 None
=> return None
, // Cookie format error
349 if name
== cookie_name
{
350 use percent_encoding
::percent_decode
;
351 if let Ok(value
) = percent_decode(value
.as_bytes()).decode_utf8() {
352 return Some(value
.into());
354 return None
; // Cookie format error
362 pub fn join(data
: &Vec
<String
>, sep
: char) -> String
{
363 let mut list
= String
::new();
366 if !list
.is_empty() {
375 /// Detect modified configuration files
377 /// This function fails with a reasonable error message if checksums do not match.
378 pub fn detect_modified_configuration_file(digest1
: &[u8;32], digest2
: &[u8;32]) -> Result
<(), Error
> {
379 if digest1
!= digest2
{
380 bail
!("detected modified configuration - file changed by other user? Try again.");
385 /// normalize uri path
387 /// Do not allow ".", "..", or hidden files ".XXXX"
388 /// Also remove empty path components
389 pub fn normalize_uri_path(path
: &str) -> Result
<(String
, Vec
<&str>), Error
> {
390 let items
= path
.split('
/'
);
392 let mut path
= String
::new();
393 let mut components
= vec
![];
399 if name
.starts_with('
.'
) {
400 bail
!("Path contains illegal components.");
404 components
.push(name
);
407 Ok((path
, components
))
410 /// Helper to check result from std::process::Command output
412 /// The exit_code_check() function should return true if the exit code
413 /// is considered successful.
414 pub fn command_output(
415 output
: std
::process
::Output
,
416 exit_code_check
: Option
<fn(i32) -> bool
>,
417 ) -> Result
<Vec
<u8>, Error
> {
419 if !output
.status
.success() {
420 match output
.status
.code() {
422 let is_ok
= match exit_code_check
{
423 Some(check_fn
) => check_fn(code
),
427 let msg
= String
::from_utf8(output
.stderr
)
428 .map(|m
| if m
.is_empty() { String::from("no error message") }
else { m }
)
429 .unwrap_or_else(|_
| String
::from("non utf8 error message (suppressed)"));
431 bail
!("status code: {} - {}", code
, msg
);
434 None
=> bail
!("terminated by signal"),
441 /// Helper to check result from std::process::Command output, returns String.
443 /// The exit_code_check() function should return true if the exit code
444 /// is considered successful.
445 pub fn command_output_as_string(
446 output
: std
::process
::Output
,
447 exit_code_check
: Option
<fn(i32) -> bool
>,
448 ) -> Result
<String
, Error
> {
449 let output
= command_output(output
, exit_code_check
)?
;
450 let output
= String
::from_utf8(output
)?
;
455 mut command
: std
::process
::Command
,
456 exit_code_check
: Option
<fn(i32) -> bool
>,
457 ) -> Result
<String
, Error
> {
459 let output
= command
.output()
460 .map_err(|err
| format_err
!("failed to execute {:?} - {}", command
, err
))?
;
462 let output
= crate::tools
::command_output_as_string(output
, exit_code_check
)
463 .map_err(|err
| format_err
!("command {:?} failed - {}", command
, err
))?
;
468 pub fn fd_change_cloexec(fd
: RawFd
, on
: bool
) -> Result
<(), Error
> {
469 use nix
::fcntl
::{fcntl, FdFlag, F_GETFD, F_SETFD}
;
470 let mut flags
= FdFlag
::from_bits(fcntl(fd
, F_GETFD
)?
)
471 .ok_or_else(|| format_err
!("unhandled file flags"))?
; // nix crate is stupid this way...
472 flags
.set(FdFlag
::FD_CLOEXEC
, on
);
473 fcntl(fd
, F_SETFD(flags
))?
;
477 static mut SHUTDOWN_REQUESTED
: bool
= false;
479 pub fn request_shutdown() {
481 SHUTDOWN_REQUESTED
= true;
483 crate::server
::server_shutdown();
487 pub fn shutdown_requested() -> bool
{
488 unsafe { SHUTDOWN_REQUESTED }
491 pub fn fail_on_shutdown() -> Result
<(), Error
> {
492 if shutdown_requested() {
493 bail
!("Server shutdown requested - aborting task");
498 /// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file
500 pub fn pipe() -> Result
<(Fd
, Fd
), Error
> {
501 let (pin
, pout
) = nix
::unistd
::pipe2(nix
::fcntl
::OFlag
::O_CLOEXEC
)?
;
502 Ok((Fd(pin
), Fd(pout
)))
505 /// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file
507 pub fn socketpair() -> Result
<(Fd
, Fd
), Error
> {
508 use nix
::sys
::socket
;
509 let (pa
, pb
) = socket
::socketpair(
510 socket
::AddressFamily
::Unix
,
511 socket
::SockType
::Stream
,
513 socket
::SockFlag
::SOCK_CLOEXEC
,
519 /// An easy way to convert types to Any
521 /// Mostly useful to downcast trait objects (see RpcEnvironment).
523 fn as_any(&self) -> &dyn Any
;
526 impl<T
: Any
> AsAny
for T
{
527 fn as_any(&self) -> &dyn Any
{
532 /// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}`
533 pub const DEFAULT_ENCODE_SET
: &AsciiSet
= &percent_encoding
::CONTROLS
// 0..1f and 7e
534 // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above)
537 // the DEFAULT_ENCODE_SET added:
548 /// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a
550 pub fn file_get_non_comment_lines<P: AsRef<Path>>(
552 ) -> Result<impl Iterator<Item = io::Result<String>>, Error> {
553 let path = path.as_ref();
555 Ok(io::BufReader::new(
556 File::open(path).map_err(|err| format_err!("error opening {:?}
: {}
", path, err))?,
559 .filter_map(|line| match line {
561 let line = line.trim();
562 if line.is_empty() || line.starts_with('#') {
565 Some(Ok(line.to_string()))
568 Err(err) => Some(Err(err)),
572 pub fn setup_safe_path_env() {
573 std::env::set_var("PATH
", "/sbin
:/bin
:/usr
/sbin
:/usr
/bin
");
574 // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
575 for name in &["IFS
", "CDPATH
", "ENV
", "BASH_ENV
"] {
576 std::env::remove_var(name);
580 pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] {
581 let line = match line.iter().position(|&b| !b.is_ascii_whitespace()) {
582 Some(n) => &line[n..],
585 match line.iter().rev().position(|&b| !b.is_ascii_whitespace()) {
586 Some(n) => &line[..(line.len() - n)],
591 /// Seeks to start of file and computes the SHA256 hash
592 pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
594 file.seek(SeekFrom::Start(0))?;
596 let mut hasher = openssl::sha::Sha256::new();
597 let mut buffer = proxmox::tools::vec::undefined(256*1024);
598 let mut size: u64 = 0;
601 let count = match file.read(&mut buffer) {
603 Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
606 Err(err) => return Err(err.into()),
611 size += count as u64;
612 hasher.update(&buffer[..count]);
615 let csum = hasher.finish();