]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/tools.rs
src/bin/proxmox-backup-proxy.rs: implement unpriviledged server
[proxmox-backup.git] / src / tools.rs
index 6c22a62ca2a4689154e02558eaff380c15a5ae59..81747e64244a24271a3417821de0025e23f73e36 100644 (file)
@@ -1,7 +1,12 @@
+//! Tools and utilities
+//!
+//! This is a collection of small and useful tools.
 use failure::*;
 use nix::unistd;
 use nix::sys::stat;
 
+use lazy_static::lazy_static;
+
 use std::fs::{File, OpenOptions};
 use std::io::Write;
 use std::path::Path;
@@ -9,25 +14,82 @@ use std::io::Read;
 use std::io::ErrorKind;
 use std::time::Duration;
 
+use std::os::unix::io::RawFd;
 use std::os::unix::io::AsRawFd;
 
+use serde_json::Value;
+
 pub mod timer;
+pub mod wrapped_reader_stream;
+#[macro_use]
+pub mod common_regex;
+
+/// The `BufferedReader` trait provides a single function
+/// `buffered_read`. It returns a reference to an internal buffer. The
+/// purpose of this traid is to avoid unnecessary data copies.
+pub trait BufferedReader {
+    /// This functions tries to fill the internal buffers, then
+    /// returns a reference to the available data. It returns an empty
+    /// buffer if `offset` points to the end of the file.
+    fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error>;
+}
 
-fn map_struct<T>(buffer: &[u8]) -> Result<&T, Error> {
+/// Directly map a type into a binary buffer. This is mostly useful
+/// for reading structured data from a byte stream (file). You need to
+/// make sure that the buffer location does not change, so please
+/// avoid vec resize while you use such map.
+///
+/// This function panics if the buffer is not large enough.
+pub fn map_struct<T>(buffer: &[u8]) -> Result<&T, Error> {
     if buffer.len() < ::std::mem::size_of::<T>() {
         bail!("unable to map struct - buffer too small");
     }
-    return Ok(unsafe { & * (buffer.as_ptr() as *const T) });
+    Ok(unsafe { & * (buffer.as_ptr() as *const T) })
 }
 
-fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
+/// Directly map a type into a mutable binary buffer. This is mostly
+/// useful for writing structured data into a byte stream (file). You
+/// need to make sure that the buffer location does not change, so
+/// please avoid vec resize while you use such map.
+///
+/// This function panics if the buffer is not large enough.
+pub fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
     if buffer.len() < ::std::mem::size_of::<T>() {
         bail!("unable to map struct - buffer too small");
     }
-    return Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) });
+    Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) })
 }
 
+pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> {
+
+    let path = path.as_ref();
+
+    let file = std::fs::File::open(path)?;
+
+    use std::io::{BufRead, BufReader};
+
+    let mut reader = BufReader::new(file);
+
+    let mut line = String::new();
+
+    let _ = reader.read_line(&mut line)?;
 
+    Ok(line)
+}
+
+pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, std::io::Error> {
+
+    let mut file = std::fs::File::open(path)?;
+
+    let mut buffer = Vec::new();
+
+    file.read_to_end(&mut buffer)?;
+
+    Ok(buffer)
+}
+
+/// Atomically write a file. We first create a temporary file, which
+/// is then renamed.
 pub fn file_set_contents<P: AsRef<Path>>(
     path: P,
     data: &[u8],
@@ -73,6 +135,8 @@ pub fn file_set_contents<P: AsRef<Path>>(
     Ok(())
 }
 
+/// Create a file lock using fntl. This function allows you to specify
+/// a timeout if you want to avoid infinite blocking.
 pub fn lock_file<F: AsRawFd>(
     file: &mut F,
     exclusive: bool,
@@ -110,6 +174,8 @@ pub fn lock_file<F: AsRawFd>(
     Ok(())
 }
 
+/// Open or create a lock file (append mode). Then try to
+/// aquire a lock using `lock_file()`.
 pub fn open_file_locked<P: AsRef<Path>>(path: P, timeout: Duration)
     -> Result<File, Error>
 {
@@ -131,8 +197,9 @@ pub fn open_file_locked<P: AsRef<Path>>(path: P, timeout: Duration)
     }
 }
 
-// Note: We cannot implement an Iterator, because Iterators cannot
-// return a borrowed buffer ref (we want zero-copy)
+/// Split a file into equal sized chunks. The last chunk may be
+/// smaller. Note: We cannot implement an `Iterator`, because iterators
+/// cannot return a borrowed buffer ref (we want zero-copy)
 pub fn file_chunker<C, R>(
     mut file: R,
     chunk_size: usize,
@@ -192,3 +259,159 @@ pub fn file_chunker<C, R>(
 
     Ok(())
 }
+
+/// Returns the hosts node name (UTS node name)
+pub fn nodename() -> &'static str {
+
+    lazy_static!{
+        static ref NODENAME: String = {
+
+            nix::sys::utsname::uname()
+                .nodename()
+                .split('.')
+                .next()
+                .unwrap()
+                .to_owned()
+        };
+    }
+
+    &NODENAME
+}
+
+pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> {
+    match param[name].as_str()   {
+        Some(s) => Ok(s),
+        None => bail!("missing parameter '{}'", name),
+    }
+}
+
+pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, Error> {
+    match param[name].as_i64()   {
+        Some(s) => Ok(s),
+        None => bail!("missing parameter '{}'", name),
+    }
+}
+
+pub fn complete_file_name(arg: &str) -> Vec<String> {
+
+    let mut result = vec![];
+
+    use nix::fcntl::OFlag;
+    use nix::sys::stat::Mode;
+    use nix::fcntl::AtFlags;
+
+    let mut dirname = std::path::PathBuf::from(arg);
+
+    let is_dir = match nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) {
+        Ok(stat) => (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR,
+        Err(_) => false,
+    };
+
+    if !is_dir {
+        if let Some(parent) = dirname.parent() {
+            dirname = parent.to_owned();
+        }
+    }
+
+    let mut dir = match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) {
+        Ok(d) => d,
+        Err(_) => return result,
+    };
+
+    for item in dir.iter() {
+        if let Ok(entry) = item {
+            if let Ok(name) = entry.file_name().to_str() {
+                if name == "." || name == ".." { continue; }
+                let mut newpath = dirname.clone();
+                newpath.push(name);
+
+                if let Ok(stat) = nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty()) {
+                    if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
+                        newpath.push("");
+                        if let Some(newpath) = newpath.to_str() {
+                            result.push(newpath.to_owned());
+                        }
+                        continue;
+                     }
+                }
+                if let Some(newpath) = newpath.to_str() {
+                    result.push(newpath.to_owned());
+                }
+
+             }
+        }
+    }
+
+    result
+}
+
+/// Scan directory for matching file names.
+///
+/// Scan through all directory entries and call `callback()` function
+/// if the entry name matches the regular expression. This function
+/// used unix `openat()`, so you can pass absolute or relative file
+/// names. This function simply skips non-UTF8 encoded names.
+pub fn scandir<P, F>(
+    dirfd: RawFd,
+    path: P,
+    regex: &regex::Regex,
+    mut callback: F
+) -> Result<(), Error>
+    where F: FnMut(RawFd, &str, nix::dir::Type) -> Result<(), Error>,
+          P: AsRef<Path>
+{
+    use nix::fcntl::OFlag;
+    use nix::sys::stat::Mode;
+
+    let mut subdir = nix::dir::Dir::openat(dirfd, path.as_ref(), OFlag::O_RDONLY, Mode::empty())?;
+    let subdir_fd = subdir.as_raw_fd();
+
+    for entry in subdir.iter() {
+        let entry = entry?;
+        let file_type = match entry.file_type() {
+            Some(file_type) => file_type,
+            None => bail!("unable to detect file type"),
+        };
+        let filename = entry.file_name();
+        let filename_str = match filename.to_str() {
+            Ok(name) => name,
+            Err(_) => continue /* ignore non utf8 entries*/,
+        };
+
+        if !regex.is_match(filename_str) { continue; }
+
+        (callback)(subdir_fd, filename_str, file_type)?;
+    }
+    Ok(())
+}
+
+pub fn get_hardware_address() -> Result<String, Error> {
+
+    static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
+
+    let contents = file_get_contents(FILENAME)?;
+    let digest = md5::compute(contents);
+
+    Ok(format!("{:0x}", digest))
+}
+
+pub fn digest_to_hex(digest: &[u8]) -> String {
+
+    const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
+
+    let mut buf = Vec::<u8>::with_capacity(digest.len()*2);
+
+    for i in 0..digest.len() {
+        buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
+        buf.push(HEX_CHARS[(digest[i] & 0xf) as usize]);
+    }
+
+    unsafe { String::from_utf8_unchecked(buf) }
+}
+
+pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
+    if digest1 != digest2 {
+       bail!("detected modified configuration - file changed by other user? Try again.");
+    }
+    Ok(())
+}