From 9751ef4b3655efa34e4f197a4d8d150529195581 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Fri, 16 Jul 2021 10:53:22 +0200 Subject: [PATCH] backup/datastore: refactor check_backup_owner there and add a 'owns_backup' convenience function Signed-off-by: Dominik Csapak --- src/api2/admin/datastore.rs | 12 ------------ src/backup/datastore.rs | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 0bf6a86b..79ab97e7 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -74,18 +74,6 @@ fn check_priv_or_backup_owner( Ok(()) } -fn check_backup_owner( - owner: &Authid, - auth_id: &Authid, -) -> Result<(), Error> { - let correct_owner = owner == auth_id - || (owner.is_token() && &Authid::from(owner.user().clone()) == auth_id); - if !correct_owner { - bail!("backup owner check failed ({} != {})", auth_id, owner); - } - Ok(()) -} - fn read_backup_index( store: &DataStore, backup_dir: &BackupDir, diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index d47c412b..29700846 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -37,6 +37,20 @@ lazy_static! { static ref DATASTORE_MAP: Mutex>> = Mutex::new(HashMap::new()); } +/// checks if auth_id is owner, or, if owner is a token, if +/// auth_id is the user of the token +pub fn check_backup_owner( + owner: &Authid, + auth_id: &Authid, +) -> Result<(), Error> { + let correct_owner = owner == auth_id + || (owner.is_token() && &Authid::from(owner.user().clone()) == auth_id); + if !correct_owner { + bail!("backup owner check failed ({} != {})", auth_id, owner); + } + Ok(()) +} + /// Datastore Management /// /// A Datastore can store severals backups, and provides the @@ -338,6 +352,12 @@ impl DataStore { Ok(owner.trim_end().parse()?) // remove trailing newline } + pub fn owns_backup(&self, backup_group: &BackupGroup, auth_id: &Authid) -> Result { + let owner = self.get_owner(backup_group)?; + + Ok(check_backup_owner(owner, auth_id).is_ok()) + } + /// Set the backup owner. pub fn set_owner( &self,