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