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
;
41 pub use parallel_handler
::*;
43 mod wrapped_reader_stream
;
44 pub use wrapped_reader_stream
::*;
46 mod std_channel_writer
;
47 pub use std_channel_writer
::*;
52 pub use process_locker
::*;
55 pub use file_logger
::*;
58 pub use broadcast_future
::*;
60 /// The `BufferedRead` trait provides a single function
61 /// `buffered_read`. It returns a reference to an internal buffer. The
62 /// purpose of this traid is to avoid unnecessary data copies.
63 pub trait BufferedRead
{
64 /// This functions tries to fill the internal buffers, then
65 /// returns a reference to the available data. It returns an empty
66 /// buffer if `offset` points to the end of the file.
67 fn buffered_read(&mut self, offset
: u64) -> Result
<&[u8], Error
>;
70 /// Split a file into equal sized chunks. The last chunk may be
71 /// smaller. Note: We cannot implement an `Iterator`, because iterators
72 /// cannot return a borrowed buffer ref (we want zero-copy)
73 pub fn file_chunker
<C
, R
>(mut file
: R
, chunk_size
: usize, mut chunk_cb
: C
) -> Result
<(), Error
>
75 C
: FnMut(usize, &[u8]) -> Result
<bool
, Error
>,
78 const READ_BUFFER_SIZE
: usize = 4 * 1024 * 1024; // 4M
80 if chunk_size
> READ_BUFFER_SIZE
{
81 bail
!("chunk size too large!");
84 let mut buf
= vec
::undefined(READ_BUFFER_SIZE
);
90 let mut tmp
= &mut buf
[..];
91 // try to read large portions, at least chunk_size
92 while pos
< chunk_size
{
93 match file
.read(tmp
) {
100 if pos
> chunk_size
{
105 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> { /* try again */ }
106 Err(e
) => bail
!("read chunk failed - {}", e
.to_string()),
110 while start
+ chunk_size
<= pos
{
111 if !(chunk_cb
)(file_pos
, &buf
[start
..start
+ chunk_size
])?
{
114 file_pos
+= chunk_size
;
119 (chunk_cb
)(file_pos
, &buf
[start
..pos
])?
;
120 //file_pos += pos - start;
124 let rest
= pos
- start
;
126 let ptr
= buf
.as_mut_ptr();
128 std
::ptr
::copy_nonoverlapping(ptr
.add(start
), ptr
, rest
);
140 pub fn json_object_to_query(data
: Value
) -> Result
<String
, Error
> {
141 let mut query
= url
::form_urlencoded
::Serializer
::new(String
::new());
143 let object
= data
.as_object().ok_or_else(|| {
144 format_err
!("json_object_to_query: got wrong data type (expected object).")
147 for (key
, value
) in object
{
150 query
.append_pair(key
, &b
.to_string());
152 Value
::Number(n
) => {
153 query
.append_pair(key
, &n
.to_string());
155 Value
::String(s
) => {
156 query
.append_pair(key
, &s
);
158 Value
::Array(arr
) => {
162 query
.append_pair(key
, &b
.to_string());
164 Value
::Number(n
) => {
165 query
.append_pair(key
, &n
.to_string());
167 Value
::String(s
) => {
168 query
.append_pair(key
, &s
);
171 "json_object_to_query: unable to handle complex array data types."
176 _
=> bail
!("json_object_to_query: unable to handle complex data types."),
183 pub fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
184 match param
[name
].as_str() {
186 None
=> bail
!("missing parameter '{}'", name
),
190 pub fn required_string_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
191 match param
[name
].as_str() {
193 None
=> bail
!("missing property '{}'", name
),
197 pub fn required_integer_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
198 match param
[name
].as_i64() {
200 None
=> bail
!("missing parameter '{}'", name
),
204 pub fn required_integer_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
205 match param
[name
].as_i64() {
207 None
=> bail
!("missing property '{}'", name
),
211 pub fn required_array_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
212 match param
[name
].as_array() {
213 Some(s
) => Ok(s
.to_vec()),
214 None
=> bail
!("missing parameter '{}'", name
),
218 pub fn required_array_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
219 match param
[name
].as_array() {
220 Some(s
) => Ok(s
.to_vec()),
221 None
=> bail
!("missing property '{}'", name
),
225 pub fn complete_file_name
<S
: BuildHasher
>(arg
: &str, _param
: &HashMap
<String
, String
, S
>) -> Vec
<String
> {
226 let mut result
= vec
![];
228 use nix
::fcntl
::AtFlags
;
229 use nix
::fcntl
::OFlag
;
230 use nix
::sys
::stat
::Mode
;
232 let mut dirname
= std
::path
::PathBuf
::from(if arg
.is_empty() { "./" }
else { arg }
);
234 let is_dir
= match nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &dirname
, AtFlags
::empty()) {
235 Ok(stat
) => (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
,
240 if let Some(parent
) = dirname
.parent() {
241 dirname
= parent
.to_owned();
246 match nix
::dir
::Dir
::openat(libc
::AT_FDCWD
, &dirname
, OFlag
::O_DIRECTORY
, Mode
::empty()) {
248 Err(_
) => return result
,
251 for item
in dir
.iter() {
252 if let Ok(entry
) = item
{
253 if let Ok(name
) = entry
.file_name().to_str() {
254 if name
== "." || name
== ".." {
257 let mut newpath
= dirname
.clone();
261 nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &newpath
, AtFlags
::empty())
263 if (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
{
265 if let Some(newpath
) = newpath
.to_str() {
266 result
.push(newpath
.to_owned());
271 if let Some(newpath
) = newpath
.to_str() {
272 result
.push(newpath
.to_owned());
281 /// Scan directory for matching file names.
283 /// Scan through all directory entries and call `callback()` function
284 /// if the entry name matches the regular expression. This function
285 /// used unix `openat()`, so you can pass absolute or relative file
286 /// names. This function simply skips non-UTF8 encoded names.
287 pub fn scandir
<P
, F
>(
290 regex
: ®ex
::Regex
,
292 ) -> Result
<(), Error
>
294 F
: FnMut(RawFd
, &str, nix
::dir
::Type
) -> Result
<(), Error
>,
295 P
: ?Sized
+ nix
::NixPath
,
297 for entry
in self::fs
::scan_subdir(dirfd
, path
, regex
)?
{
299 let file_type
= match entry
.file_type() {
300 Some(file_type
) => file_type
,
301 None
=> bail
!("unable to detect file type"),
306 unsafe { entry.file_name_utf8_unchecked() }
,
313 /// Shortcut for md5 sums.
314 pub fn md5sum(data
: &[u8]) -> Result
<DigestBytes
, Error
> {
315 hash(MessageDigest
::md5(), data
).map_err(Error
::from
)
318 pub fn get_hardware_address() -> Result
<String
, Error
> {
319 static FILENAME
: &str = "/etc/ssh/ssh_host_rsa_key.pub";
321 let contents
= proxmox
::tools
::fs
::file_get_contents(FILENAME
)?
;
322 let digest
= md5sum(&contents
)?
;
324 Ok(proxmox
::tools
::bin_to_hex(&digest
))
327 pub fn assert_if_modified(digest1
: &str, digest2
: &str) -> Result
<(), Error
> {
328 if digest1
!= digest2
{
329 bail
!("detected modified configuration - file changed by other user? Try again.");
334 /// Extract a specific cookie from cookie header.
335 /// We assume cookie_name is already url encoded.
336 pub fn extract_cookie(cookie
: &str, cookie_name
: &str) -> Option
<String
> {
337 for pair
in cookie
.split('
;'
) {
338 let (name
, value
) = match pair
.find('
='
) {
339 Some(i
) => (pair
[..i
].trim(), pair
[(i
+ 1)..].trim()),
340 None
=> return None
, // Cookie format error
343 if name
== cookie_name
{
344 use percent_encoding
::percent_decode
;
345 if let Ok(value
) = percent_decode(value
.as_bytes()).decode_utf8() {
346 return Some(value
.into());
348 return None
; // Cookie format error
356 pub fn join(data
: &Vec
<String
>, sep
: char) -> String
{
357 let mut list
= String
::new();
360 if !list
.is_empty() {
369 /// Detect modified configuration files
371 /// This function fails with a reasonable error message if checksums do not match.
372 pub fn detect_modified_configuration_file(digest1
: &[u8;32], digest2
: &[u8;32]) -> Result
<(), Error
> {
373 if digest1
!= digest2
{
374 bail
!("detected modified configuration - file changed by other user? Try again.");
379 /// normalize uri path
381 /// Do not allow ".", "..", or hidden files ".XXXX"
382 /// Also remove empty path components
383 pub fn normalize_uri_path(path
: &str) -> Result
<(String
, Vec
<&str>), Error
> {
384 let items
= path
.split('
/'
);
386 let mut path
= String
::new();
387 let mut components
= vec
![];
393 if name
.starts_with('
.'
) {
394 bail
!("Path contains illegal components.");
398 components
.push(name
);
401 Ok((path
, components
))
404 /// Helper to check result from std::process::Command output
406 /// The exit_code_check() function should return true if the exit code
407 /// is considered successful.
408 pub fn command_output(
409 output
: std
::process
::Output
,
410 exit_code_check
: Option
<fn(i32) -> bool
>,
411 ) -> Result
<Vec
<u8>, Error
> {
413 if !output
.status
.success() {
414 match output
.status
.code() {
416 let is_ok
= match exit_code_check
{
417 Some(check_fn
) => check_fn(code
),
421 let msg
= String
::from_utf8(output
.stderr
)
422 .map(|m
| if m
.is_empty() { String::from("no error message") }
else { m }
)
423 .unwrap_or_else(|_
| String
::from("non utf8 error message (suppressed)"));
425 bail
!("status code: {} - {}", code
, msg
);
428 None
=> bail
!("terminated by signal"),
435 /// Helper to check result from std::process::Command output, returns String.
437 /// The exit_code_check() function should return true if the exit code
438 /// is considered successful.
439 pub fn command_output_as_string(
440 output
: std
::process
::Output
,
441 exit_code_check
: Option
<fn(i32) -> bool
>,
442 ) -> Result
<String
, Error
> {
443 let output
= command_output(output
, exit_code_check
)?
;
444 let output
= String
::from_utf8(output
)?
;
449 mut command
: std
::process
::Command
,
450 exit_code_check
: Option
<fn(i32) -> bool
>,
451 ) -> Result
<String
, Error
> {
453 let output
= command
.output()
454 .map_err(|err
| format_err
!("failed to execute {:?} - {}", command
, err
))?
;
456 let output
= crate::tools
::command_output_as_string(output
, exit_code_check
)
457 .map_err(|err
| format_err
!("command {:?} failed - {}", command
, err
))?
;
462 pub fn fd_change_cloexec(fd
: RawFd
, on
: bool
) -> Result
<(), Error
> {
463 use nix
::fcntl
::{fcntl, FdFlag, F_GETFD, F_SETFD}
;
464 let mut flags
= FdFlag
::from_bits(fcntl(fd
, F_GETFD
)?
)
465 .ok_or_else(|| format_err
!("unhandled file flags"))?
; // nix crate is stupid this way...
466 flags
.set(FdFlag
::FD_CLOEXEC
, on
);
467 fcntl(fd
, F_SETFD(flags
))?
;
471 static mut SHUTDOWN_REQUESTED
: bool
= false;
473 pub fn request_shutdown() {
475 SHUTDOWN_REQUESTED
= true;
477 crate::server
::server_shutdown();
481 pub fn shutdown_requested() -> bool
{
482 unsafe { SHUTDOWN_REQUESTED }
485 pub fn fail_on_shutdown() -> Result
<(), Error
> {
486 if shutdown_requested() {
487 bail
!("Server shutdown requested - aborting task");
492 /// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file
494 pub fn pipe() -> Result
<(Fd
, Fd
), Error
> {
495 let (pin
, pout
) = nix
::unistd
::pipe2(nix
::fcntl
::OFlag
::O_CLOEXEC
)?
;
496 Ok((Fd(pin
), Fd(pout
)))
499 /// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file
501 pub fn socketpair() -> Result
<(Fd
, Fd
), Error
> {
502 use nix
::sys
::socket
;
503 let (pa
, pb
) = socket
::socketpair(
504 socket
::AddressFamily
::Unix
,
505 socket
::SockType
::Stream
,
507 socket
::SockFlag
::SOCK_CLOEXEC
,
513 /// An easy way to convert types to Any
515 /// Mostly useful to downcast trait objects (see RpcEnvironment).
517 fn as_any(&self) -> &dyn Any
;
520 impl<T
: Any
> AsAny
for T
{
521 fn as_any(&self) -> &dyn Any
{
526 /// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}`
527 pub const DEFAULT_ENCODE_SET
: &AsciiSet
= &percent_encoding
::CONTROLS
// 0..1f and 7e
528 // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above)
531 // the DEFAULT_ENCODE_SET added:
542 /// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a
544 pub fn file_get_non_comment_lines<P: AsRef<Path>>(
546 ) -> Result<impl Iterator<Item = io::Result<String>>, Error> {
547 let path = path.as_ref();
549 Ok(io::BufReader::new(
550 File::open(path).map_err(|err| format_err!("error opening {:?}
: {}
", path, err))?,
553 .filter_map(|line| match line {
555 let line = line.trim();
556 if line.is_empty() || line.starts_with('#') {
559 Some(Ok(line.to_string()))
562 Err(err) => Some(Err(err)),
566 pub fn setup_safe_path_env() {
567 std::env::set_var("PATH
", "/sbin
:/bin
:/usr
/sbin
:/usr
/bin
");
568 // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html
569 for name in &["IFS
", "CDPATH
", "ENV
", "BASH_ENV
"] {
570 std::env::remove_var(name);
574 pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] {
575 let line = match line.iter().position(|&b| !b.is_ascii_whitespace()) {
576 Some(n) => &line[n..],
579 match line.iter().rev().position(|&b| !b.is_ascii_whitespace()) {
580 Some(n) => &line[..(line.len() - n)],
585 /// Seeks to start of file and computes the SHA256 hash
586 pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
588 file.seek(SeekFrom::Start(0))?;
590 let mut hasher = openssl::sha::Sha256::new();
591 let mut buffer = proxmox::tools::vec::undefined(256*1024);
592 let mut size: u64 = 0;
595 let count = match file.read(&mut buffer) {
597 Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
600 Err(err) => return Err(err.into()),
605 size += count as u64;
606 hasher.update(&buffer[..count]);
609 let csum = hasher.finish();