]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools.rs
set reasonable TCP keepalive timeout
[proxmox-backup.git] / src / tools.rs
CommitLineData
51b499db
DM
1//! Tools and utilities
2//!
3//! This is a collection of small and useful tools.
6100071f
WB
4use std::any::Any;
5use std::collections::HashMap;
62ee2eb4 6use std::hash::BuildHasher;
98c259b4 7use std::fs::File;
1bc1d81a 8use std::io::{self, BufRead, ErrorKind, Read, Seek, SeekFrom};
98c259b4 9use std::os::unix::io::RawFd;
6100071f 10use std::path::Path;
365bb90f 11
f7d4e4b5 12use anyhow::{bail, format_err, Error};
af926291 13use serde_json::Value;
c5946faf 14use openssl::hash::{hash, DigestBytes, MessageDigest};
8a1028e0 15use percent_encoding::AsciiSet;
0fe5d605 16
f35197f4
WB
17use proxmox::tools::vec;
18
00ec8d16
WB
19pub use proxmox::tools::fd::Fd;
20
6100071f 21pub mod acl;
556eb70e 22pub mod async_io;
6ed25cbe 23pub mod borrow;
ec01eead 24pub mod cert;
6100071f 25pub mod daemon;
10effc98 26pub mod disks;
b4d5787d 27pub mod fs;
4939255f 28pub mod format;
1685c2e3 29pub mod lru_cache;
daef93f4 30pub mod runtime;
6100071f 31pub mod ticket;
ba974798 32pub mod statistics;
f486e9e5 33pub mod systemd;
177a2de9 34pub mod nom;
8074d2b0 35pub mod logrotate;
45f9b32e
SR
36pub mod loopdev;
37pub mod fuse_loop;
97168f92 38pub mod socket;
f1d99e3f 39
3c9b3702
DM
40mod parallel_handler;
41pub use parallel_handler::*;
42
f1d99e3f
DM
43mod wrapped_reader_stream;
44pub use wrapped_reader_stream::*;
dcd033a5
DM
45
46mod std_channel_writer;
47pub use std_channel_writer::*;
48
2dcdd3b4 49pub mod xattr;
8cf6e764 50
a650f503
DM
51mod process_locker;
52pub use process_locker::*;
53
3b151414
DM
54mod file_logger;
55pub use file_logger::*;
56
490be29e
DM
57mod broadcast_future;
58pub use broadcast_future::*;
59
fded74d0 60/// The `BufferedRead` trait provides a single function
0a72e267
DM
61/// `buffered_read`. It returns a reference to an internal buffer. The
62/// purpose of this traid is to avoid unnecessary data copies.
fded74d0 63pub trait BufferedRead {
318564ac
DM
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.
0a72e267
DM
67 fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error>;
68}
69
51b499db
DM
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)
6100071f
WB
73pub fn file_chunker<C, R>(mut file: R, chunk_size: usize, mut chunk_cb: C) -> Result<(), Error>
74where
75 C: FnMut(usize, &[u8]) -> Result<bool, Error>,
76 R: Read,
43eeef28 77{
6100071f 78 const READ_BUFFER_SIZE: usize = 4 * 1024 * 1024; // 4M
43eeef28 79
6100071f
WB
80 if chunk_size > READ_BUFFER_SIZE {
81 bail!("chunk size too large!");
82 }
43eeef28 83
8ea3b1d1 84 let mut buf = vec::undefined(READ_BUFFER_SIZE);
43eeef28
DM
85
86 let mut pos = 0;
87 let mut file_pos = 0;
88 loop {
89 let mut eof = false;
90 let mut tmp = &mut buf[..];
6100071f 91 // try to read large portions, at least chunk_size
43eeef28
DM
92 while pos < chunk_size {
93 match file.read(tmp) {
6100071f
WB
94 Ok(0) => {
95 eof = true;
96 break;
97 }
43eeef28
DM
98 Ok(n) => {
99 pos += n;
6100071f
WB
100 if pos > chunk_size {
101 break;
102 }
43eeef28
DM
103 tmp = &mut tmp[n..];
104 }
105 Err(ref e) if e.kind() == ErrorKind::Interrupted => { /* try again */ }
5f0c2d56 106 Err(e) => bail!("read chunk failed - {}", e.to_string()),
43eeef28
DM
107 }
108 }
43eeef28
DM
109 let mut start = 0;
110 while start + chunk_size <= pos {
6100071f
WB
111 if !(chunk_cb)(file_pos, &buf[start..start + chunk_size])? {
112 break;
113 }
43eeef28
DM
114 file_pos += chunk_size;
115 start += chunk_size;
116 }
117 if eof {
118 if start < pos {
119 (chunk_cb)(file_pos, &buf[start..pos])?;
120 //file_pos += pos - start;
121 }
122 break;
123 } else {
124 let rest = pos - start;
125 if rest > 0 {
126 let ptr = buf.as_mut_ptr();
6100071f
WB
127 unsafe {
128 std::ptr::copy_nonoverlapping(ptr.add(start), ptr, rest);
129 }
43eeef28
DM
130 pos = rest;
131 } else {
132 pos = 0;
133 }
134 }
135 }
136
137 Ok(())
43eeef28 138}
0fe5d605 139
f5f13ebc 140pub fn json_object_to_query(data: Value) -> Result<String, Error> {
f5f13ebc
DM
141 let mut query = url::form_urlencoded::Serializer::new(String::new());
142
143 let object = data.as_object().ok_or_else(|| {
144 format_err!("json_object_to_query: got wrong data type (expected object).")
145 })?;
146
147 for (key, value) in object {
148 match value {
6100071f
WB
149 Value::Bool(b) => {
150 query.append_pair(key, &b.to_string());
151 }
152 Value::Number(n) => {
153 query.append_pair(key, &n.to_string());
154 }
155 Value::String(s) => {
156 query.append_pair(key, &s);
157 }
f5f13ebc
DM
158 Value::Array(arr) => {
159 for element in arr {
160 match element {
6100071f
WB
161 Value::Bool(b) => {
162 query.append_pair(key, &b.to_string());
163 }
164 Value::Number(n) => {
165 query.append_pair(key, &n.to_string());
166 }
167 Value::String(s) => {
168 query.append_pair(key, &s);
169 }
170 _ => bail!(
171 "json_object_to_query: unable to handle complex array data types."
172 ),
f5f13ebc
DM
173 }
174 }
175 }
176 _ => bail!("json_object_to_query: unable to handle complex data types."),
177 }
178 }
179
180 Ok(query.finish())
181}
182
0fe5d605 183pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> {
6100071f 184 match param[name].as_str() {
0fe5d605
DM
185 Some(s) => Ok(s),
186 None => bail!("missing parameter '{}'", name),
187 }
188}
0d38dcb4 189
e17d5d86
DM
190pub fn required_string_property<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> {
191 match param[name].as_str() {
192 Some(s) => Ok(s),
193 None => bail!("missing property '{}'", name),
194 }
195}
196
0d38dcb4 197pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, Error> {
6100071f 198 match param[name].as_i64() {
0d38dcb4
DM
199 Some(s) => Ok(s),
200 None => bail!("missing parameter '{}'", name),
f8dfbb45
DM
201 }
202}
203
e17d5d86
DM
204pub fn required_integer_property<'a>(param: &'a Value, name: &str) -> Result<i64, Error> {
205 match param[name].as_i64() {
206 Some(s) => Ok(s),
207 None => bail!("missing property '{}'", name),
208 }
209}
210
f8dfbb45 211pub fn required_array_param<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> {
6100071f 212 match param[name].as_array() {
f8dfbb45
DM
213 Some(s) => Ok(s.to_vec()),
214 None => bail!("missing parameter '{}'", name),
0d38dcb4
DM
215 }
216}
383e8577 217
e17d5d86
DM
218pub 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),
222 }
223}
224
62ee2eb4 225pub fn complete_file_name<S: BuildHasher>(arg: &str, _param: &HashMap<String, String, S>) -> Vec<String> {
383e8577
DM
226 let mut result = vec![];
227
6100071f 228 use nix::fcntl::AtFlags;
383e8577
DM
229 use nix::fcntl::OFlag;
230 use nix::sys::stat::Mode;
383e8577 231
62ee2eb4 232 let mut dirname = std::path::PathBuf::from(if arg.is_empty() { "./" } else { arg });
383e8577
DM
233
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,
236 Err(_) => false,
237 };
238
239 if !is_dir {
240 if let Some(parent) = dirname.parent() {
241 dirname = parent.to_owned();
242 }
243 }
244
6100071f
WB
245 let mut dir =
246 match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) {
247 Ok(d) => d,
248 Err(_) => return result,
249 };
383e8577
DM
250
251 for item in dir.iter() {
252 if let Ok(entry) = item {
253 if let Ok(name) = entry.file_name().to_str() {
6100071f
WB
254 if name == "." || name == ".." {
255 continue;
256 }
383e8577
DM
257 let mut newpath = dirname.clone();
258 newpath.push(name);
259
6100071f
WB
260 if let Ok(stat) =
261 nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty())
262 {
383e8577
DM
263 if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
264 newpath.push("");
265 if let Some(newpath) = newpath.to_str() {
266 result.push(newpath.to_owned());
267 }
268 continue;
6100071f 269 }
383e8577
DM
270 }
271 if let Some(newpath) = newpath.to_str() {
272 result.push(newpath.to_owned());
273 }
6100071f 274 }
383e8577
DM
275 }
276 }
277
278 result
279}
443f3743
DM
280
281/// Scan directory for matching file names.
282///
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.
287pub fn scandir<P, F>(
288 dirfd: RawFd,
121f18ef 289 path: &P,
443f3743 290 regex: &regex::Regex,
6100071f 291 mut callback: F,
443f3743 292) -> Result<(), Error>
6100071f
WB
293where
294 F: FnMut(RawFd, &str, nix::dir::Type) -> Result<(), Error>,
295 P: ?Sized + nix::NixPath,
443f3743 296{
121f18ef 297 for entry in self::fs::scan_subdir(dirfd, path, regex)? {
443f3743
DM
298 let entry = entry?;
299 let file_type = match entry.file_type() {
300 Some(file_type) => file_type,
301 None => bail!("unable to detect file type"),
302 };
443f3743 303
6100071f
WB
304 callback(
305 entry.parent_fd(),
306 unsafe { entry.file_name_utf8_unchecked() },
307 file_type,
308 )?;
443f3743
DM
309 }
310 Ok(())
311}
7e13b2d6 312
c5946faf
WB
313/// Shortcut for md5 sums.
314pub fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
315 hash(MessageDigest::md5(), data).map_err(Error::from)
316}
317
7e13b2d6 318pub fn get_hardware_address() -> Result<String, Error> {
1631c54f 319 static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
7e13b2d6 320
e18a6c9e 321 let contents = proxmox::tools::fs::file_get_contents(FILENAME)?;
c5946faf 322 let digest = md5sum(&contents)?;
7e13b2d6 323
c5946faf 324 Ok(proxmox::tools::bin_to_hex(&digest))
7e13b2d6 325}
22968600 326
af2fddea
DM
327pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
328 if digest1 != digest2 {
6100071f 329 bail!("detected modified configuration - file changed by other user? Try again.");
af2fddea
DM
330 }
331 Ok(())
332}
b9903d63 333
09f12d1c 334/// Extract a specific cookie from cookie header.
b9903d63 335/// We assume cookie_name is already url encoded.
09f12d1c 336pub fn extract_cookie(cookie: &str, cookie_name: &str) -> Option<String> {
b9903d63 337 for pair in cookie.split(';') {
b9903d63
DM
338 let (name, value) = match pair.find('=') {
339 Some(i) => (pair[..i].trim(), pair[(i + 1)..].trim()),
340 None => return None, // Cookie format error
341 };
342
343 if name == cookie_name {
8a1028e0 344 use percent_encoding::percent_decode;
b9903d63
DM
345 if let Ok(value) = percent_decode(value.as_bytes()).decode_utf8() {
346 return Some(value.into());
347 } else {
348 return None; // Cookie format error
349 }
350 }
351 }
352
353 None
354}
af53186e
DM
355
356pub fn join(data: &Vec<String>, sep: char) -> String {
af53186e
DM
357 let mut list = String::new();
358
359 for item in data {
62ee2eb4 360 if !list.is_empty() {
6100071f
WB
361 list.push(sep);
362 }
af53186e
DM
363 list.push_str(item);
364 }
365
366 list
367}
ff7049d4 368
002a191a
DM
369/// Detect modified configuration files
370///
add5861e 371/// This function fails with a reasonable error message if checksums do not match.
002a191a
DM
372pub 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.");
375 }
376 Ok(())
377}
378
3578d99f
DM
379/// normalize uri path
380///
381/// Do not allow ".", "..", or hidden files ".XXXX"
382/// Also remove empty path components
383pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> {
3578d99f
DM
384 let items = path.split('/');
385
386 let mut path = String::new();
387 let mut components = vec![];
388
389 for name in items {
6100071f
WB
390 if name.is_empty() {
391 continue;
392 }
62ee2eb4 393 if name.starts_with('.') {
3578d99f
DM
394 bail!("Path contains illegal components.");
395 }
396 path.push('/');
397 path.push_str(name);
398 components.push(name);
399 }
400
401 Ok((path, components))
402}
403
97fab7aa 404/// Helper to check result from std::process::Command output
143b6545
DM
405///
406/// The exit_code_check() function should return true if the exit code
407/// is considered successful.
408pub fn command_output(
409 output: std::process::Output,
144006fa 410 exit_code_check: Option<fn(i32) -> bool>,
e64b9f92 411) -> Result<Vec<u8>, Error> {
97fab7aa
DM
412
413 if !output.status.success() {
414 match output.status.code() {
415 Some(code) => {
143b6545
DM
416 let is_ok = match exit_code_check {
417 Some(check_fn) => check_fn(code),
418 None => code == 0,
419 };
420 if !is_ok {
97fab7aa
DM
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)"));
424
425 bail!("status code: {} - {}", code, msg);
426 }
427 }
428 None => bail!("terminated by signal"),
429 }
430 }
431
e64b9f92
DM
432 Ok(output.stdout)
433}
97fab7aa 434
e64b9f92
DM
435/// Helper to check result from std::process::Command output, returns String.
436///
437/// The exit_code_check() function should return true if the exit code
438/// is considered successful.
439pub 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)?;
97fab7aa
DM
445 Ok(output)
446}
447
144006fa
DM
448pub fn run_command(
449 mut command: std::process::Command,
450 exit_code_check: Option<fn(i32) -> bool>,
451) -> Result<String, Error> {
452
453 let output = command.output()
454 .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
455
e64b9f92 456 let output = crate::tools::command_output_as_string(output, exit_code_check)
144006fa
DM
457 .map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
458
459 Ok(output)
460}
97fab7aa 461
ff7049d4 462pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> {
6100071f 463 use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD};
ff7049d4
WB
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))?;
468 Ok(())
469}
9136f857 470
9136f857
DM
471static mut SHUTDOWN_REQUESTED: bool = false;
472
473pub fn request_shutdown() {
6100071f
WB
474 unsafe {
475 SHUTDOWN_REQUESTED = true;
476 }
7a630df7 477 crate::server::server_shutdown();
9136f857
DM
478}
479
480#[inline(always)]
481pub fn shutdown_requested() -> bool {
482 unsafe { SHUTDOWN_REQUESTED }
483}
92da93b2
DM
484
485pub fn fail_on_shutdown() -> Result<(), Error> {
486 if shutdown_requested() {
487 bail!("Server shutdown requested - aborting task");
488 }
489 Ok(())
490}
d96bb7f1 491
c4044009
WB
492/// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file
493/// descriptors.
efd1536e
WB
494pub fn pipe() -> Result<(Fd, Fd), Error> {
495 let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
496 Ok((Fd(pin), Fd(pout)))
497}
2edc341b 498
c4044009
WB
499/// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file
500/// descriptors.
501pub 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,
506 None,
507 socket::SockFlag::SOCK_CLOEXEC,
508 )?;
509 Ok((Fd(pa), Fd(pb)))
510}
511
512
2edc341b
DM
513/// An easy way to convert types to Any
514///
515/// Mostly useful to downcast trait objects (see RpcEnvironment).
516pub trait AsAny {
dd5495d6 517 fn as_any(&self) -> &dyn Any;
2edc341b
DM
518}
519
520impl<T: Any> AsAny for T {
6100071f
WB
521 fn as_any(&self) -> &dyn Any {
522 self
523 }
2edc341b 524}
8a1028e0
WB
525
526/// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}`
527pub 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)
529 .add(0x20)
530 .add(0x7f)
531 // the DEFAULT_ENCODE_SET added:
532 .add(b' ')
533 .add(b'"')
534 .add(b'#')
535 .add(b'<')
536 .add(b'>')
537 .add(b'`')
538 .add(b'?')
539 .add(b'{')
540 .add(b'}');
386990ba
WB
541
542/// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a
543/// `#`).
544pub fn file_get_non_comment_lines<P: AsRef<Path>>(
545 path: P,
546) -> Result<impl Iterator<Item = io::Result<String>>, Error> {
547 let path = path.as_ref();
548
549 Ok(io::BufReader::new(
550 File::open(path).map_err(|err| format_err!("error opening {:?}: {}", path, err))?,
551 )
552 .lines()
553 .filter_map(|line| match line {
554 Ok(line) => {
555 let line = line.trim();
556 if line.is_empty() || line.starts_with('#') {
557 None
558 } else {
559 Some(Ok(line.to_string()))
560 }
561 }
562 Err(err) => Some(Err(err)),
563 }))
564}
e693818a 565
ac7513e3
DM
566pub 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);
571 }
572}
cdf1da28
WB
573
574pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] {
575 let line = match line.iter().position(|&b| !b.is_ascii_whitespace()) {
576 Some(n) => &line[n..],
577 None => return &[],
578 };
579 match line.iter().rev().position(|&b| !b.is_ascii_whitespace()) {
580 Some(n) => &line[..(line.len() - n)],
581 None => &[],
582 }
583}
1bc1d81a
DM
584
585/// Seeks to start of file and computes the SHA256 hash
586pub fn compute_file_csum(file: &mut File) -> Result<([u8; 32], u64), Error> {
587
588 file.seek(SeekFrom::Start(0))?;
589
590 let mut hasher = openssl::sha::Sha256::new();
591 let mut buffer = proxmox::tools::vec::undefined(256*1024);
592 let mut size: u64 = 0;
593
594 loop {
595 let count = match file.read(&mut buffer) {
596 Ok(count) => count,
597 Err(ref err) if err.kind() == std::io::ErrorKind::Interrupted => {
598 continue;
599 }
600 Err(err) => return Err(err.into()),
601 };
602 if count == 0 {
603 break;
604 }
605 size += count as u64;
606 hasher.update(&buffer[..count]);
607 }
608
609 let csum = hasher.finish();
610
611 Ok((csum, size))
612}