2021-07-08 08:07:01 +00:00
|
|
|
//! Server/client-specific parts for what's otherwise in pbs-datastore.
|
2018-12-31 15:08:04 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-12-19 09:20:13 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
// Note: .pcat1 => Proxmox Catalog Format version 1
|
|
|
|
pub const CATALOG_NAME: &str = "catalog.pcat1.didx";
|
2019-09-03 11:15:44 +00:00
|
|
|
|
2019-12-19 09:20:13 +00:00
|
|
|
/// Unix system user used by proxmox-backup-proxy
|
|
|
|
pub const BACKUP_USER_NAME: &str = "backup";
|
2020-08-26 08:02:18 +00:00
|
|
|
/// Unix system group used by proxmox-backup-proxy
|
|
|
|
pub const BACKUP_GROUP_NAME: &str = "backup";
|
2019-12-19 09:20:13 +00:00
|
|
|
|
|
|
|
/// Return User info for the 'backup' user (``getpwnam_r(3)``)
|
|
|
|
pub fn backup_user() -> Result<nix::unistd::User, Error> {
|
2021-07-21 06:12:51 +00:00
|
|
|
if cfg!(test) {
|
|
|
|
// fix permission problems with regressions test (when run as non-root).
|
|
|
|
Ok(nix::unistd::User::from_uid(nix::unistd::Uid::current())?.unwrap())
|
|
|
|
} else {
|
|
|
|
match nix::unistd::User::from_name(BACKUP_USER_NAME)? {
|
|
|
|
Some(user) => Ok(user),
|
|
|
|
None => bail!("Unable to lookup backup user."),
|
|
|
|
}
|
2019-12-19 09:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 08:02:18 +00:00
|
|
|
/// Return Group info for the 'backup' group (``getgrnam(3)``)
|
|
|
|
pub fn backup_group() -> Result<nix::unistd::Group, Error> {
|
2021-07-21 06:12:51 +00:00
|
|
|
if cfg!(test) {
|
|
|
|
// fix permission problems with regressions test (when run as non-root).
|
|
|
|
Ok(nix::unistd::Group::from_gid(nix::unistd::Gid::current())?.unwrap())
|
|
|
|
} else {
|
|
|
|
match nix::unistd::Group::from_name(BACKUP_GROUP_NAME)? {
|
|
|
|
Some(group) => Ok(group),
|
|
|
|
None => bail!("Unable to lookup backup user."),
|
|
|
|
}
|
2020-08-26 08:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 09:34:45 +00:00
|
|
|
pub use pbs_datastore::backup_info;
|
|
|
|
pub use pbs_datastore::backup_info::*;
|
2021-07-06 10:49:10 +00:00
|
|
|
pub use pbs_datastore::catalog;
|
2021-07-07 07:04:09 +00:00
|
|
|
pub use pbs_datastore::catalog::*;
|
|
|
|
pub use pbs_datastore::checksum_reader;
|
|
|
|
pub use pbs_datastore::checksum_reader::*;
|
|
|
|
pub use pbs_datastore::checksum_writer;
|
|
|
|
pub use pbs_datastore::checksum_writer::*;
|
2021-07-08 07:17:28 +00:00
|
|
|
pub use pbs_datastore::chunk_stat;
|
|
|
|
pub use pbs_datastore::chunk_stat::*;
|
2021-07-07 12:37:47 +00:00
|
|
|
pub use pbs_datastore::chunk_store;
|
|
|
|
pub use pbs_datastore::chunk_store::*;
|
2021-07-07 07:04:09 +00:00
|
|
|
pub use pbs_datastore::chunker;
|
|
|
|
pub use pbs_datastore::chunker::*;
|
2021-07-06 10:49:10 +00:00
|
|
|
pub use pbs_datastore::crypt_config;
|
|
|
|
pub use pbs_datastore::crypt_config::*;
|
|
|
|
pub use pbs_datastore::crypt_reader;
|
|
|
|
pub use pbs_datastore::crypt_reader::*;
|
|
|
|
pub use pbs_datastore::crypt_writer;
|
|
|
|
pub use pbs_datastore::crypt_writer::*;
|
|
|
|
pub use pbs_datastore::data_blob;
|
|
|
|
pub use pbs_datastore::data_blob::*;
|
|
|
|
pub use pbs_datastore::data_blob_reader;
|
|
|
|
pub use pbs_datastore::data_blob_reader::*;
|
|
|
|
pub use pbs_datastore::data_blob_writer;
|
|
|
|
pub use pbs_datastore::data_blob_writer::*;
|
2021-07-07 07:04:09 +00:00
|
|
|
pub use pbs_datastore::file_formats;
|
|
|
|
pub use pbs_datastore::file_formats::*;
|
|
|
|
pub use pbs_datastore::index;
|
|
|
|
pub use pbs_datastore::index::*;
|
|
|
|
pub use pbs_datastore::key_derivation;
|
|
|
|
pub use pbs_datastore::key_derivation::*;
|
2021-07-07 09:34:45 +00:00
|
|
|
pub use pbs_datastore::manifest;
|
|
|
|
pub use pbs_datastore::manifest::*;
|
2021-07-08 08:07:01 +00:00
|
|
|
pub use pbs_datastore::prune;
|
|
|
|
pub use pbs_datastore::prune::*;
|
|
|
|
|
2021-07-09 12:26:42 +00:00
|
|
|
pub use pbs_datastore::store_progress::StoreProgress;
|
|
|
|
|
2021-07-21 12:20:00 +00:00
|
|
|
pub use pbs_datastore::cached_chunk_reader::*;
|
2021-07-08 08:07:01 +00:00
|
|
|
pub use pbs_datastore::dynamic_index::*;
|
|
|
|
pub use pbs_datastore::fixed_index;
|
|
|
|
pub use pbs_datastore::fixed_index::*;
|
|
|
|
|
2021-07-08 07:17:28 +00:00
|
|
|
pub use pbs_datastore::read_chunk::*;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2021-07-08 07:17:28 +00:00
|
|
|
// Split
|
2019-07-02 06:22:29 +00:00
|
|
|
mod read_chunk;
|
|
|
|
pub use read_chunk::*;
|
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
mod datastore;
|
|
|
|
pub use datastore::*;
|
2019-11-21 11:47:51 +00:00
|
|
|
|
2020-06-24 11:11:45 +00:00
|
|
|
mod verify;
|
|
|
|
pub use verify::*;
|
|
|
|
|
2021-07-20 11:51:54 +00:00
|
|
|
pub struct BackupLockGuard(std::fs::File);
|
|
|
|
|
|
|
|
/// Open or create a lock file owned by user "backup" and lock it.
|
|
|
|
///
|
|
|
|
/// Owner/Group of the file is set to backup/backup.
|
|
|
|
/// File mode is 0660.
|
|
|
|
/// Default timeout is 10 seconds.
|
|
|
|
///
|
|
|
|
/// Note: This method needs to be called by user "root" or "backup".
|
|
|
|
pub fn open_backup_lockfile<P: AsRef<std::path::Path>>(
|
|
|
|
path: P,
|
|
|
|
timeout: Option<std::time::Duration>,
|
|
|
|
exclusive: bool,
|
|
|
|
) -> Result<BackupLockGuard, Error> {
|
|
|
|
let user = backup_user()?;
|
|
|
|
let options = proxmox::tools::fs::CreateOptions::new()
|
|
|
|
.perm(nix::sys::stat::Mode::from_bits_truncate(0o660))
|
|
|
|
.owner(user.uid)
|
|
|
|
.group(user.gid);
|
|
|
|
|
|
|
|
let timeout = timeout.unwrap_or(std::time::Duration::new(10, 0));
|
|
|
|
|
|
|
|
let file = proxmox::tools::fs::open_file_locked(&path, timeout, exclusive, options)?;
|
|
|
|
Ok(BackupLockGuard(file))
|
|
|
|
}
|
2021-07-20 11:51:55 +00:00
|
|
|
|
|
|
|
/// Atomically write data to file owned by "root:backup" with permission "0640"
|
|
|
|
///
|
|
|
|
/// Only the superuser can write those files, but group 'backup' can read them.
|
|
|
|
pub fn replace_backup_config<P: AsRef<std::path::Path>>(
|
|
|
|
path: P,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let backup_user = backup_user()?;
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)= backup
|
|
|
|
let options = proxmox::tools::fs::CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
|
|
|
proxmox::tools::fs::replace_file(path, data, options)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Atomically write data to file owned by "root:root" with permission "0600"
|
|
|
|
///
|
|
|
|
/// Only the superuser can read and write those files.
|
|
|
|
pub fn replace_secret_config<P: AsRef<std::path::Path>>(
|
|
|
|
path: P,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)= root
|
|
|
|
let options = proxmox::tools::fs::CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(nix::unistd::Gid::from_raw(0));
|
|
|
|
|
|
|
|
proxmox::tools::fs::replace_file(path, data, options)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|