]>
Commit | Line | Data |
---|---|---|
bf78f708 DM |
1 | //! Datastore Management |
2 | ||
0d08fcee | 3 | use std::collections::HashSet; |
d33d8f4e DC |
4 | use std::ffi::OsStr; |
5 | use std::os::unix::ffi::OsStrExt; | |
d6688884 | 6 | use std::path::PathBuf; |
6da20161 | 7 | use std::sync::Arc; |
cad540e9 | 8 | |
6ef9bb59 | 9 | use anyhow::{bail, format_err, Error}; |
9e47c0a5 | 10 | use futures::*; |
cad540e9 WB |
11 | use hyper::http::request::Parts; |
12 | use hyper::{header, Body, Response, StatusCode}; | |
8c74349b | 13 | use serde::Deserialize; |
15e9b4ed | 14 | use serde_json::{json, Value}; |
7c667013 | 15 | use tokio_stream::wrappers::ReceiverStream; |
15e9b4ed | 16 | |
dc7a5b34 TL |
17 | use proxmox_async::blocking::WrappedReaderStream; |
18 | use proxmox_async::{io::AsyncChannelWriter, stream::AsyncReaderStream}; | |
984ddb2f | 19 | use proxmox_compression::zstd::ZstdEncoder; |
6ef1b649 | 20 | use proxmox_router::{ |
dc7a5b34 TL |
21 | http_err, list_subdirs_api_method, ApiHandler, ApiMethod, ApiResponseFuture, Permission, |
22 | Router, RpcEnvironment, RpcEnvironmentType, SubdirMap, | |
6ef1b649 WB |
23 | }; |
24 | use proxmox_schema::*; | |
dc7a5b34 TL |
25 | use proxmox_sys::fs::{ |
26 | file_read_firstline, file_read_optional_string, replace_file, CreateOptions, | |
27 | }; | |
28 | use proxmox_sys::sortable; | |
d5790a9f | 29 | use proxmox_sys::{task_log, task_warn}; |
e18a6c9e | 30 | |
2e219481 | 31 | use pxar::accessor::aio::Accessor; |
d33d8f4e DC |
32 | use pxar::EntryKind; |
33 | ||
dc7a5b34 | 34 | use pbs_api_types::{ |
8c74349b WB |
35 | Authid, BackupContent, BackupNamespace, BackupType, Counts, CryptMode, DataStoreListItem, |
36 | DataStoreStatus, GarbageCollectionStatus, GroupListItem, Operation, PruneOptions, RRDMode, | |
37 | RRDTimeFrame, SnapshotListItem, SnapshotVerifyState, BACKUP_ARCHIVE_NAME_SCHEMA, | |
33f2c2a1 WB |
38 | BACKUP_ID_SCHEMA, BACKUP_NAMESPACE_SCHEMA, BACKUP_TIME_SCHEMA, BACKUP_TYPE_SCHEMA, |
39 | DATASTORE_SCHEMA, IGNORE_VERIFIED_BACKUPS_SCHEMA, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_BACKUP, | |
8c74349b WB |
40 | PRIV_DATASTORE_MODIFY, PRIV_DATASTORE_PRUNE, PRIV_DATASTORE_READ, PRIV_DATASTORE_VERIFY, |
41 | UPID_SCHEMA, VERIFICATION_OUTDATED_AFTER_SCHEMA, | |
b2065dc7 | 42 | }; |
984ddb2f | 43 | use pbs_client::pxar::{create_tar, create_zip}; |
dc7a5b34 | 44 | use pbs_config::CachedUserInfo; |
b2065dc7 WB |
45 | use pbs_datastore::backup_info::BackupInfo; |
46 | use pbs_datastore::cached_chunk_reader::CachedChunkReader; | |
013b1e8b | 47 | use pbs_datastore::catalog::{ArchiveEntry, CatalogReader}; |
b2065dc7 WB |
48 | use pbs_datastore::data_blob::DataBlob; |
49 | use pbs_datastore::data_blob_reader::DataBlobReader; | |
50 | use pbs_datastore::dynamic_index::{BufferedDynamicReader, DynamicIndexReader, LocalDynamicReadAt}; | |
dc7a5b34 | 51 | use pbs_datastore::fixed_index::FixedIndexReader; |
b2065dc7 WB |
52 | use pbs_datastore::index::IndexFile; |
53 | use pbs_datastore::manifest::{BackupManifest, CLIENT_LOG_BLOB_NAME, MANIFEST_BLOB_NAME}; | |
89725197 | 54 | use pbs_datastore::prune::compute_prune_info; |
dc7a5b34 TL |
55 | use pbs_datastore::{ |
56 | check_backup_owner, task_tracking, BackupDir, BackupGroup, DataStore, LocalChunkReader, | |
57 | StoreProgress, CATALOG_NAME, | |
58 | }; | |
8c74349b | 59 | use pbs_tools::json::required_string_param; |
dc7a5b34 | 60 | use proxmox_rest_server::{formatter, WorkerTask}; |
2b7f8dd5 | 61 | |
133d718f | 62 | use crate::api2::backup::optional_ns_param; |
431cc7b1 | 63 | use crate::api2::node::rrd::create_value_from_rrd; |
dc7a5b34 | 64 | use crate::backup::{verify_all_backups, verify_backup_dir, verify_backup_group, verify_filter}; |
54552dda | 65 | |
b9700a9f | 66 | use crate::server::jobstate::Job; |
804f6143 | 67 | |
d6688884 SR |
68 | const GROUP_NOTES_FILE_NAME: &str = "notes"; |
69 | ||
133d718f WB |
70 | fn get_group_note_path( |
71 | store: &DataStore, | |
72 | ns: &BackupNamespace, | |
73 | group: &pbs_api_types::BackupGroup, | |
74 | ) -> PathBuf { | |
75 | let mut note_path = store.group_path(ns, group); | |
d6688884 SR |
76 | note_path.push(GROUP_NOTES_FILE_NAME); |
77 | note_path | |
78 | } | |
79 | ||
7d6fc15b TL |
80 | // TODO: move somewhere we can reuse it from (namespace has its own copy atm.) |
81 | fn get_ns_privs(store: &str, ns: &BackupNamespace, auth_id: &Authid) -> Result<u64, Error> { | |
82 | let user_info = CachedUserInfo::new()?; | |
83 | ||
84 | Ok(if ns.is_root() { | |
85 | user_info.lookup_privs(auth_id, &["datastore", store]) | |
86 | } else { | |
87 | user_info.lookup_privs(auth_id, &["datastore", store, &ns.to_string()]) | |
88 | }) | |
89 | } | |
90 | ||
2bc2435a FG |
91 | // asserts that either either `full_access_privs` or `partial_access_privs` are fulfilled, |
92 | // returning value indicates whether further checks like group ownerships are required | |
93 | fn check_ns_privs( | |
7d6fc15b TL |
94 | store: &str, |
95 | ns: &BackupNamespace, | |
96 | auth_id: &Authid, | |
2bc2435a FG |
97 | full_access_privs: u64, |
98 | partial_access_privs: u64, | |
99 | ) -> Result<bool, Error> { | |
7d6fc15b TL |
100 | let privs = get_ns_privs(store, ns, auth_id)?; |
101 | ||
2bc2435a FG |
102 | if full_access_privs != 0 && (privs & full_access_privs) != 0 { |
103 | return Ok(false); | |
104 | } | |
105 | if partial_access_privs != 0 && (privs & partial_access_privs) != 0 { | |
106 | return Ok(true); | |
7d6fc15b | 107 | } |
2bc2435a FG |
108 | |
109 | proxmox_router::http_bail!(FORBIDDEN, "permission check failed"); | |
7d6fc15b TL |
110 | } |
111 | ||
e7cb4dc5 WB |
112 | fn read_backup_index( |
113 | store: &DataStore, | |
114 | backup_dir: &BackupDir, | |
115 | ) -> Result<(BackupManifest, Vec<BackupContent>), Error> { | |
ff86ef00 | 116 | let (manifest, index_size) = store.load_manifest(backup_dir)?; |
8c70e3eb | 117 | |
09b1f7b2 DM |
118 | let mut result = Vec::new(); |
119 | for item in manifest.files() { | |
120 | result.push(BackupContent { | |
121 | filename: item.filename.clone(), | |
f28d9088 | 122 | crypt_mode: Some(item.crypt_mode), |
09b1f7b2 DM |
123 | size: Some(item.size), |
124 | }); | |
8c70e3eb DM |
125 | } |
126 | ||
09b1f7b2 | 127 | result.push(BackupContent { |
96d65fbc | 128 | filename: MANIFEST_BLOB_NAME.to_string(), |
882c0823 FG |
129 | crypt_mode: match manifest.signature { |
130 | Some(_) => Some(CryptMode::SignOnly), | |
131 | None => Some(CryptMode::None), | |
132 | }, | |
09b1f7b2 DM |
133 | size: Some(index_size), |
134 | }); | |
4f1e40a2 | 135 | |
70030b43 | 136 | Ok((manifest, result)) |
8c70e3eb DM |
137 | } |
138 | ||
1c090810 DC |
139 | fn get_all_snapshot_files( |
140 | store: &DataStore, | |
141 | info: &BackupInfo, | |
70030b43 | 142 | ) -> Result<(BackupManifest, Vec<BackupContent>), Error> { |
9a37bd6c | 143 | let (manifest, mut files) = read_backup_index(store, &info.backup_dir)?; |
1c090810 DC |
144 | |
145 | let file_set = files.iter().fold(HashSet::new(), |mut acc, item| { | |
146 | acc.insert(item.filename.clone()); | |
147 | acc | |
148 | }); | |
149 | ||
150 | for file in &info.files { | |
dc7a5b34 TL |
151 | if file_set.contains(file) { |
152 | continue; | |
153 | } | |
f28d9088 WB |
154 | files.push(BackupContent { |
155 | filename: file.to_string(), | |
156 | size: None, | |
157 | crypt_mode: None, | |
158 | }); | |
1c090810 DC |
159 | } |
160 | ||
70030b43 | 161 | Ok((manifest, files)) |
1c090810 DC |
162 | } |
163 | ||
b31c8019 DM |
164 | #[api( |
165 | input: { | |
166 | properties: { | |
167 | store: { | |
168 | schema: DATASTORE_SCHEMA, | |
169 | }, | |
89ae3c32 WB |
170 | "backup-ns": { |
171 | type: BackupNamespace, | |
172 | optional: true, | |
173 | }, | |
b31c8019 DM |
174 | }, |
175 | }, | |
7b570c17 | 176 | returns: pbs_api_types::ADMIN_DATASTORE_LIST_GROUPS_RETURN_TYPE, |
bb34b589 | 177 | access: { |
7d6fc15b TL |
178 | permission: &Permission::Anybody, |
179 | description: "Requires DATASTORE_AUDIT for all or DATASTORE_BACKUP for owned groups on \ | |
180 | /datastore/{store}[/{namespace}]", | |
bb34b589 | 181 | }, |
b31c8019 DM |
182 | )] |
183 | /// List backup groups. | |
b2362a12 | 184 | pub fn list_groups( |
b31c8019 | 185 | store: String, |
89ae3c32 | 186 | backup_ns: Option<BackupNamespace>, |
54552dda | 187 | rpcenv: &mut dyn RpcEnvironment, |
b31c8019 | 188 | ) -> Result<Vec<GroupListItem>, Error> { |
e6dc35ac | 189 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
7d6fc15b TL |
190 | |
191 | let backup_ns = backup_ns.unwrap_or_default(); | |
2bc2435a | 192 | let list_all = !check_ns_privs( |
7d6fc15b TL |
193 | &store, |
194 | &backup_ns, | |
195 | &auth_id, | |
2bc2435a FG |
196 | PRIV_DATASTORE_AUDIT, |
197 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 198 | )?; |
54552dda | 199 | |
e9d2fc93 | 200 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
0d08fcee | 201 | |
249dde8b | 202 | datastore |
133d718f | 203 | .iter_backup_groups(backup_ns.clone())? // FIXME: Namespaces and recursion parameters! |
249dde8b TL |
204 | .try_fold(Vec::new(), |mut group_info, group| { |
205 | let group = group?; | |
133d718f | 206 | let owner = match datastore.get_owner(&backup_ns, group.as_ref()) { |
249dde8b TL |
207 | Ok(auth_id) => auth_id, |
208 | Err(err) => { | |
209 | let id = &store; | |
210 | eprintln!("Failed to get owner of group '{}/{}' - {}", id, group, err); | |
211 | return Ok(group_info); | |
dc7a5b34 | 212 | } |
249dde8b TL |
213 | }; |
214 | if !list_all && check_backup_owner(&owner, &auth_id).is_err() { | |
215 | return Ok(group_info); | |
216 | } | |
0d08fcee | 217 | |
6da20161 | 218 | let snapshots = match group.list_backups() { |
249dde8b TL |
219 | Ok(snapshots) => snapshots, |
220 | Err(_) => return Ok(group_info), | |
221 | }; | |
0d08fcee | 222 | |
249dde8b TL |
223 | let backup_count: u64 = snapshots.len() as u64; |
224 | if backup_count == 0 { | |
225 | return Ok(group_info); | |
226 | } | |
0d08fcee | 227 | |
249dde8b TL |
228 | let last_backup = snapshots |
229 | .iter() | |
230 | .fold(&snapshots[0], |a, b| { | |
231 | if a.is_finished() && a.backup_dir.backup_time() > b.backup_dir.backup_time() { | |
232 | a | |
233 | } else { | |
234 | b | |
235 | } | |
236 | }) | |
237 | .to_owned(); | |
238 | ||
133d718f | 239 | let note_path = get_group_note_path(&datastore, &backup_ns, group.as_ref()); |
249dde8b TL |
240 | let comment = file_read_firstline(¬e_path).ok(); |
241 | ||
242 | group_info.push(GroupListItem { | |
988d575d | 243 | backup: group.into(), |
249dde8b TL |
244 | last_backup: last_backup.backup_dir.backup_time(), |
245 | owner: Some(owner), | |
246 | backup_count, | |
247 | files: last_backup.files, | |
248 | comment, | |
0d08fcee FG |
249 | }); |
250 | ||
249dde8b TL |
251 | Ok(group_info) |
252 | }) | |
812c6f87 | 253 | } |
8f579717 | 254 | |
f32791b4 DC |
255 | #[api( |
256 | input: { | |
257 | properties: { | |
988d575d | 258 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
259 | "backup-ns": { |
260 | type: BackupNamespace, | |
261 | optional: true, | |
262 | }, | |
8c74349b WB |
263 | group: { |
264 | type: pbs_api_types::BackupGroup, | |
265 | flatten: true, | |
266 | }, | |
f32791b4 DC |
267 | }, |
268 | }, | |
269 | access: { | |
7d6fc15b TL |
270 | permission: &Permission::Anybody, |
271 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any\ | |
272 | or DATASTORE_PRUNE and being the owner of the group", | |
f32791b4 DC |
273 | }, |
274 | )] | |
275 | /// Delete backup group including all snapshots. | |
276 | pub fn delete_group( | |
277 | store: String, | |
133d718f | 278 | backup_ns: Option<BackupNamespace>, |
8c74349b | 279 | group: pbs_api_types::BackupGroup, |
f32791b4 DC |
280 | _info: &ApiMethod, |
281 | rpcenv: &mut dyn RpcEnvironment, | |
282 | ) -> Result<Value, Error> { | |
f32791b4 DC |
283 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
284 | ||
133d718f WB |
285 | let backup_ns = backup_ns.unwrap_or_default(); |
286 | ||
2bc2435a | 287 | let owner_check_required = check_ns_privs( |
7d6fc15b | 288 | &store, |
133d718f | 289 | &backup_ns, |
7d6fc15b | 290 | &auth_id, |
2bc2435a FG |
291 | PRIV_DATASTORE_MODIFY, |
292 | PRIV_DATASTORE_PRUNE, | |
7d6fc15b TL |
293 | )?; |
294 | ||
e9d2fc93 | 295 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
f32791b4 | 296 | |
2bc2435a | 297 | if owner_check_required { |
a724f5fd FG |
298 | let owner = datastore.get_owner(&backup_ns, &group)?; |
299 | check_backup_owner(&owner, &auth_id)?; | |
300 | } | |
f32791b4 | 301 | |
133d718f | 302 | if !datastore.remove_backup_group(&backup_ns, &group)? { |
171a00ca | 303 | bail!("group only partially deleted due to protected snapshots"); |
5cc7d891 | 304 | } |
f32791b4 DC |
305 | |
306 | Ok(Value::Null) | |
307 | } | |
308 | ||
09b1f7b2 DM |
309 | #[api( |
310 | input: { | |
311 | properties: { | |
988d575d | 312 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
313 | "backup-ns": { |
314 | type: BackupNamespace, | |
315 | optional: true, | |
316 | }, | |
8c74349b WB |
317 | backup_dir: { |
318 | type: pbs_api_types::BackupDir, | |
319 | flatten: true, | |
320 | }, | |
09b1f7b2 DM |
321 | }, |
322 | }, | |
7b570c17 | 323 | returns: pbs_api_types::ADMIN_DATASTORE_LIST_SNAPSHOT_FILES_RETURN_TYPE, |
bb34b589 | 324 | access: { |
7d6fc15b TL |
325 | permission: &Permission::Anybody, |
326 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_AUDIT or \ | |
327 | DATASTORE_READ for any or DATASTORE_BACKUP and being the owner of the group", | |
bb34b589 | 328 | }, |
09b1f7b2 DM |
329 | )] |
330 | /// List snapshot files. | |
ea5f547f | 331 | pub fn list_snapshot_files( |
09b1f7b2 | 332 | store: String, |
133d718f | 333 | backup_ns: Option<BackupNamespace>, |
8c74349b | 334 | backup_dir: pbs_api_types::BackupDir, |
01a13423 | 335 | _info: &ApiMethod, |
54552dda | 336 | rpcenv: &mut dyn RpcEnvironment, |
09b1f7b2 | 337 | ) -> Result<Vec<BackupContent>, Error> { |
e6dc35ac | 338 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
7d6fc15b | 339 | |
133d718f WB |
340 | let backup_ns = backup_ns.unwrap_or_default(); |
341 | ||
2bc2435a | 342 | let owner_check_required = check_ns_privs( |
7d6fc15b | 343 | &store, |
133d718f | 344 | &backup_ns, |
7d6fc15b | 345 | &auth_id, |
2bc2435a FG |
346 | PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_READ, |
347 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b TL |
348 | )?; |
349 | ||
e9d2fc93 | 350 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
54552dda | 351 | |
2bc2435a | 352 | if owner_check_required { |
a724f5fd FG |
353 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
354 | check_backup_owner(&owner, &auth_id)?; | |
355 | } | |
01a13423 | 356 | |
a724f5fd | 357 | let snapshot = datastore.backup_dir(backup_ns, backup_dir)?; |
54552dda | 358 | |
6da20161 | 359 | let info = BackupInfo::new(snapshot)?; |
01a13423 | 360 | |
70030b43 DM |
361 | let (_manifest, files) = get_all_snapshot_files(&datastore, &info)?; |
362 | ||
363 | Ok(files) | |
01a13423 DM |
364 | } |
365 | ||
68a6a0ee DM |
366 | #[api( |
367 | input: { | |
368 | properties: { | |
988d575d | 369 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
370 | "backup-ns": { |
371 | type: BackupNamespace, | |
372 | optional: true, | |
373 | }, | |
8c74349b WB |
374 | backup_dir: { |
375 | type: pbs_api_types::BackupDir, | |
376 | flatten: true, | |
377 | }, | |
68a6a0ee DM |
378 | }, |
379 | }, | |
bb34b589 | 380 | access: { |
7d6fc15b TL |
381 | permission: &Permission::Anybody, |
382 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any\ | |
383 | or DATASTORE_PRUNE and being the owner of the group", | |
bb34b589 | 384 | }, |
68a6a0ee DM |
385 | )] |
386 | /// Delete backup snapshot. | |
bf78f708 | 387 | pub fn delete_snapshot( |
68a6a0ee | 388 | store: String, |
133d718f | 389 | backup_ns: Option<BackupNamespace>, |
8c74349b | 390 | backup_dir: pbs_api_types::BackupDir, |
6f62c924 | 391 | _info: &ApiMethod, |
54552dda | 392 | rpcenv: &mut dyn RpcEnvironment, |
6f62c924 | 393 | ) -> Result<Value, Error> { |
e6dc35ac | 394 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
54552dda | 395 | |
133d718f WB |
396 | let backup_ns = backup_ns.unwrap_or_default(); |
397 | ||
2bc2435a | 398 | let owner_check_required = check_ns_privs( |
7d6fc15b | 399 | &store, |
133d718f | 400 | &backup_ns, |
7d6fc15b | 401 | &auth_id, |
2bc2435a FG |
402 | PRIV_DATASTORE_MODIFY, |
403 | PRIV_DATASTORE_PRUNE, | |
7d6fc15b TL |
404 | )?; |
405 | ||
e9d2fc93 | 406 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
6f62c924 | 407 | |
2bc2435a | 408 | if owner_check_required { |
a724f5fd FG |
409 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
410 | check_backup_owner(&owner, &auth_id)?; | |
411 | } | |
412 | ||
413 | let snapshot = datastore.backup_dir(backup_ns, backup_dir)?; | |
54552dda | 414 | |
133d718f | 415 | snapshot.destroy(false)?; |
6f62c924 DM |
416 | |
417 | Ok(Value::Null) | |
418 | } | |
419 | ||
fc189b19 | 420 | #[api( |
b7c3eaa9 | 421 | streaming: true, |
fc189b19 DM |
422 | input: { |
423 | properties: { | |
988d575d | 424 | store: { schema: DATASTORE_SCHEMA }, |
8c74349b WB |
425 | "backup-ns": { |
426 | type: BackupNamespace, | |
427 | optional: true, | |
428 | }, | |
fc189b19 DM |
429 | "backup-type": { |
430 | optional: true, | |
988d575d | 431 | type: BackupType, |
fc189b19 DM |
432 | }, |
433 | "backup-id": { | |
434 | optional: true, | |
435 | schema: BACKUP_ID_SCHEMA, | |
436 | }, | |
437 | }, | |
438 | }, | |
7b570c17 | 439 | returns: pbs_api_types::ADMIN_DATASTORE_LIST_SNAPSHOTS_RETURN_TYPE, |
bb34b589 | 440 | access: { |
7d6fc15b TL |
441 | permission: &Permission::Anybody, |
442 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_AUDIT for any \ | |
443 | or DATASTORE_BACKUP and being the owner of the group", | |
bb34b589 | 444 | }, |
fc189b19 DM |
445 | )] |
446 | /// List backup snapshots. | |
dc7a5b34 | 447 | pub fn list_snapshots( |
54552dda | 448 | store: String, |
8c74349b | 449 | backup_ns: Option<BackupNamespace>, |
988d575d | 450 | backup_type: Option<BackupType>, |
54552dda DM |
451 | backup_id: Option<String>, |
452 | _param: Value, | |
184f17af | 453 | _info: &ApiMethod, |
54552dda | 454 | rpcenv: &mut dyn RpcEnvironment, |
fc189b19 | 455 | ) -> Result<Vec<SnapshotListItem>, Error> { |
e6dc35ac | 456 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
7d6fc15b TL |
457 | |
458 | let backup_ns = backup_ns.unwrap_or_default(); | |
459 | ||
2bc2435a | 460 | let list_all = !check_ns_privs( |
7d6fc15b TL |
461 | &store, |
462 | &backup_ns, | |
463 | &auth_id, | |
2bc2435a FG |
464 | PRIV_DATASTORE_AUDIT, |
465 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 466 | )?; |
184f17af | 467 | |
e9d2fc93 | 468 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
184f17af | 469 | |
249dde8b TL |
470 | // FIXME: filter also owner before collecting, for doing that nicely the owner should move into |
471 | // backup group and provide an error free (Err -> None) accessor | |
0d08fcee | 472 | let groups = match (backup_type, backup_id) { |
db87d93e | 473 | (Some(backup_type), Some(backup_id)) => { |
8c74349b | 474 | vec![datastore.backup_group_from_parts(backup_ns, backup_type, backup_id)] |
db87d93e | 475 | } |
8c74349b | 476 | // FIXME: Recursion |
7d9cb8c4 | 477 | (Some(backup_type), None) => datastore |
8c74349b | 478 | .iter_backup_groups_ok(backup_ns)? |
dc7a5b34 TL |
479 | .filter(|group| group.backup_type() == backup_type) |
480 | .collect(), | |
8c74349b | 481 | // FIXME: Recursion |
7d9cb8c4 | 482 | (None, Some(backup_id)) => datastore |
8c74349b | 483 | .iter_backup_groups_ok(backup_ns)? |
dc7a5b34 TL |
484 | .filter(|group| group.backup_id() == backup_id) |
485 | .collect(), | |
8c74349b WB |
486 | // FIXME: Recursion |
487 | (None, None) => datastore.list_backup_groups(backup_ns)?, | |
0d08fcee | 488 | }; |
54552dda | 489 | |
0d08fcee | 490 | let info_to_snapshot_list_item = |group: &BackupGroup, owner, info: BackupInfo| { |
988d575d WB |
491 | let backup = pbs_api_types::BackupDir { |
492 | group: group.into(), | |
493 | time: info.backup_dir.backup_time(), | |
494 | }; | |
6da20161 | 495 | let protected = info.backup_dir.is_protected(); |
1c090810 | 496 | |
79c53595 | 497 | match get_all_snapshot_files(&datastore, &info) { |
70030b43 | 498 | Ok((manifest, files)) => { |
70030b43 DM |
499 | // extract the first line from notes |
500 | let comment: Option<String> = manifest.unprotected["notes"] | |
501 | .as_str() | |
502 | .and_then(|notes| notes.lines().next()) | |
503 | .map(String::from); | |
504 | ||
035c40e6 FG |
505 | let fingerprint = match manifest.fingerprint() { |
506 | Ok(fp) => fp, | |
507 | Err(err) => { | |
508 | eprintln!("error parsing fingerprint: '{}'", err); | |
509 | None | |
dc7a5b34 | 510 | } |
035c40e6 FG |
511 | }; |
512 | ||
79c53595 | 513 | let verification = manifest.unprotected["verify_state"].clone(); |
dc7a5b34 TL |
514 | let verification: Option<SnapshotVerifyState> = |
515 | match serde_json::from_value(verification) { | |
516 | Ok(verify) => verify, | |
517 | Err(err) => { | |
518 | eprintln!("error parsing verification state : '{}'", err); | |
519 | None | |
520 | } | |
521 | }; | |
3b2046d2 | 522 | |
0d08fcee FG |
523 | let size = Some(files.iter().map(|x| x.size.unwrap_or(0)).sum()); |
524 | ||
79c53595 | 525 | SnapshotListItem { |
988d575d | 526 | backup, |
79c53595 FG |
527 | comment, |
528 | verification, | |
035c40e6 | 529 | fingerprint, |
79c53595 FG |
530 | files, |
531 | size, | |
532 | owner, | |
02db7267 | 533 | protected, |
79c53595 | 534 | } |
dc7a5b34 | 535 | } |
1c090810 DC |
536 | Err(err) => { |
537 | eprintln!("error during snapshot file listing: '{}'", err); | |
79c53595 | 538 | let files = info |
dc7a5b34 TL |
539 | .files |
540 | .into_iter() | |
541 | .map(|filename| BackupContent { | |
542 | filename, | |
543 | size: None, | |
544 | crypt_mode: None, | |
545 | }) | |
546 | .collect(); | |
79c53595 FG |
547 | |
548 | SnapshotListItem { | |
988d575d | 549 | backup, |
79c53595 FG |
550 | comment: None, |
551 | verification: None, | |
035c40e6 | 552 | fingerprint: None, |
79c53595 FG |
553 | files, |
554 | size: None, | |
555 | owner, | |
02db7267 | 556 | protected, |
79c53595 | 557 | } |
dc7a5b34 | 558 | } |
0d08fcee FG |
559 | } |
560 | }; | |
184f17af | 561 | |
dc7a5b34 | 562 | groups.iter().try_fold(Vec::new(), |mut snapshots, group| { |
133d718f | 563 | let owner = match group.get_owner() { |
dc7a5b34 TL |
564 | Ok(auth_id) => auth_id, |
565 | Err(err) => { | |
566 | eprintln!( | |
567 | "Failed to get owner of group '{}/{}' - {}", | |
568 | &store, group, err | |
569 | ); | |
0d08fcee FG |
570 | return Ok(snapshots); |
571 | } | |
dc7a5b34 | 572 | }; |
0d08fcee | 573 | |
dc7a5b34 TL |
574 | if !list_all && check_backup_owner(&owner, &auth_id).is_err() { |
575 | return Ok(snapshots); | |
576 | } | |
0d08fcee | 577 | |
6da20161 | 578 | let group_backups = group.list_backups()?; |
0d08fcee | 579 | |
dc7a5b34 TL |
580 | snapshots.extend( |
581 | group_backups | |
582 | .into_iter() | |
583 | .map(|info| info_to_snapshot_list_item(group, Some(owner.clone()), info)), | |
584 | ); | |
585 | ||
586 | Ok(snapshots) | |
587 | }) | |
184f17af DM |
588 | } |
589 | ||
6da20161 WB |
590 | fn get_snapshots_count( |
591 | store: &Arc<DataStore>, | |
592 | filter_owner: Option<&Authid>, | |
593 | ) -> Result<Counts, Error> { | |
7d9cb8c4 | 594 | store |
8c74349b | 595 | .iter_backup_groups_ok(Default::default())? // FIXME: Recurse! |
fdfcb74d | 596 | .filter(|group| { |
133d718f WB |
597 | // FIXME: namespace: |
598 | let owner = match store.get_owner(&BackupNamespace::root(), group.as_ref()) { | |
fdfcb74d FG |
599 | Ok(owner) => owner, |
600 | Err(err) => { | |
72f81545 TL |
601 | let id = store.name(); |
602 | eprintln!("Failed to get owner of group '{}/{}' - {}", id, group, err); | |
fdfcb74d | 603 | return false; |
dc7a5b34 | 604 | } |
fdfcb74d | 605 | }; |
14e08625 | 606 | |
fdfcb74d FG |
607 | match filter_owner { |
608 | Some(filter) => check_backup_owner(&owner, filter).is_ok(), | |
609 | None => true, | |
610 | } | |
611 | }) | |
612 | .try_fold(Counts::default(), |mut counts, group| { | |
6da20161 | 613 | let snapshot_count = group.list_backups()?.len() as u64; |
fdfcb74d | 614 | |
72f81545 | 615 | // only include groups with snapshots, counting/displaying emtpy groups can confuse |
b44483a8 DM |
616 | if snapshot_count > 0 { |
617 | let type_count = match group.backup_type() { | |
988d575d WB |
618 | BackupType::Ct => counts.ct.get_or_insert(Default::default()), |
619 | BackupType::Vm => counts.vm.get_or_insert(Default::default()), | |
620 | BackupType::Host => counts.host.get_or_insert(Default::default()), | |
b44483a8 | 621 | }; |
14e08625 | 622 | |
b44483a8 DM |
623 | type_count.groups += 1; |
624 | type_count.snapshots += snapshot_count; | |
625 | } | |
16f9f244 | 626 | |
fdfcb74d FG |
627 | Ok(counts) |
628 | }) | |
16f9f244 DC |
629 | } |
630 | ||
1dc117bb DM |
631 | #[api( |
632 | input: { | |
633 | properties: { | |
634 | store: { | |
635 | schema: DATASTORE_SCHEMA, | |
636 | }, | |
98afc7b1 FG |
637 | verbose: { |
638 | type: bool, | |
639 | default: false, | |
640 | optional: true, | |
641 | description: "Include additional information like snapshot counts and GC status.", | |
642 | }, | |
1dc117bb | 643 | }, |
98afc7b1 | 644 | |
1dc117bb DM |
645 | }, |
646 | returns: { | |
14e08625 | 647 | type: DataStoreStatus, |
1dc117bb | 648 | }, |
bb34b589 | 649 | access: { |
7d6fc15b TL |
650 | permission: &Permission::Privilege( |
651 | &["datastore", "{store}"], PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP, true), | |
bb34b589 | 652 | }, |
1dc117bb DM |
653 | )] |
654 | /// Get datastore status. | |
ea5f547f | 655 | pub fn status( |
1dc117bb | 656 | store: String, |
98afc7b1 | 657 | verbose: bool, |
0eecf38f | 658 | _info: &ApiMethod, |
fdfcb74d | 659 | rpcenv: &mut dyn RpcEnvironment, |
14e08625 | 660 | ) -> Result<DataStoreStatus, Error> { |
e9d2fc93 | 661 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
14e08625 | 662 | let storage = crate::tools::disks::disk_usage(&datastore.base_path())?; |
fdfcb74d FG |
663 | let (counts, gc_status) = if verbose { |
664 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; | |
665 | let user_info = CachedUserInfo::new()?; | |
666 | ||
667 | let store_privs = user_info.lookup_privs(&auth_id, &["datastore", &store]); | |
668 | let filter_owner = if store_privs & PRIV_DATASTORE_AUDIT != 0 { | |
669 | None | |
670 | } else { | |
671 | Some(&auth_id) | |
672 | }; | |
673 | ||
674 | let counts = Some(get_snapshots_count(&datastore, filter_owner)?); | |
675 | let gc_status = Some(datastore.last_gc_status()); | |
676 | ||
677 | (counts, gc_status) | |
678 | } else { | |
679 | (None, None) | |
98afc7b1 | 680 | }; |
16f9f244 | 681 | |
14e08625 DC |
682 | Ok(DataStoreStatus { |
683 | total: storage.total, | |
684 | used: storage.used, | |
685 | avail: storage.avail, | |
686 | gc_status, | |
687 | counts, | |
688 | }) | |
0eecf38f DM |
689 | } |
690 | ||
c2009e53 DM |
691 | #[api( |
692 | input: { | |
693 | properties: { | |
694 | store: { | |
695 | schema: DATASTORE_SCHEMA, | |
696 | }, | |
8c74349b WB |
697 | "backup-ns": { |
698 | type: BackupNamespace, | |
699 | optional: true, | |
700 | }, | |
c2009e53 | 701 | "backup-type": { |
988d575d | 702 | type: BackupType, |
c2009e53 DM |
703 | optional: true, |
704 | }, | |
705 | "backup-id": { | |
706 | schema: BACKUP_ID_SCHEMA, | |
707 | optional: true, | |
708 | }, | |
dcbf29e7 HL |
709 | "ignore-verified": { |
710 | schema: IGNORE_VERIFIED_BACKUPS_SCHEMA, | |
711 | optional: true, | |
712 | }, | |
713 | "outdated-after": { | |
714 | schema: VERIFICATION_OUTDATED_AFTER_SCHEMA, | |
715 | optional: true, | |
716 | }, | |
c2009e53 DM |
717 | "backup-time": { |
718 | schema: BACKUP_TIME_SCHEMA, | |
719 | optional: true, | |
720 | }, | |
721 | }, | |
722 | }, | |
723 | returns: { | |
724 | schema: UPID_SCHEMA, | |
725 | }, | |
726 | access: { | |
7d6fc15b TL |
727 | permission: &Permission::Anybody, |
728 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_VERIFY for any \ | |
729 | or DATASTORE_BACKUP and being the owner of the group", | |
c2009e53 DM |
730 | }, |
731 | )] | |
732 | /// Verify backups. | |
733 | /// | |
734 | /// This function can verify a single backup snapshot, all backup from a backup group, | |
735 | /// or all backups in the datastore. | |
736 | pub fn verify( | |
737 | store: String, | |
8c74349b | 738 | backup_ns: Option<BackupNamespace>, |
988d575d | 739 | backup_type: Option<BackupType>, |
c2009e53 DM |
740 | backup_id: Option<String>, |
741 | backup_time: Option<i64>, | |
dcbf29e7 HL |
742 | ignore_verified: Option<bool>, |
743 | outdated_after: Option<i64>, | |
c2009e53 DM |
744 | rpcenv: &mut dyn RpcEnvironment, |
745 | ) -> Result<Value, Error> { | |
7d6fc15b TL |
746 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
747 | let backup_ns = backup_ns.unwrap_or_default(); | |
2bc2435a | 748 | let owner_check_required = check_ns_privs( |
7d6fc15b TL |
749 | &store, |
750 | &backup_ns, | |
751 | &auth_id, | |
2bc2435a FG |
752 | PRIV_DATASTORE_VERIFY, |
753 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 754 | )?; |
a724f5fd | 755 | |
e9d2fc93 | 756 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
dcbf29e7 | 757 | let ignore_verified = ignore_verified.unwrap_or(true); |
c2009e53 | 758 | |
8ea00f6e | 759 | let worker_id; |
c2009e53 DM |
760 | |
761 | let mut backup_dir = None; | |
762 | let mut backup_group = None; | |
133042b5 | 763 | let mut worker_type = "verify"; |
c2009e53 | 764 | |
8c74349b WB |
765 | // FIXME: Recursion |
766 | // FIXME: Namespaces and worker ID, could this be an issue? | |
c2009e53 DM |
767 | match (backup_type, backup_id, backup_time) { |
768 | (Some(backup_type), Some(backup_id), Some(backup_time)) => { | |
dc7a5b34 | 769 | worker_id = format!( |
8c74349b WB |
770 | "{}:{}/{}/{}/{:08X}", |
771 | store, | |
772 | backup_ns.display_as_path(), | |
773 | backup_type, | |
774 | backup_id, | |
775 | backup_time | |
dc7a5b34 | 776 | ); |
8c74349b WB |
777 | let dir = |
778 | datastore.backup_dir_from_parts(backup_ns, backup_type, backup_id, backup_time)?; | |
09f6a240 | 779 | |
a724f5fd FG |
780 | if owner_check_required { |
781 | let owner = datastore.get_owner(dir.backup_ns(), dir.as_ref())?; | |
782 | check_backup_owner(&owner, &auth_id)?; | |
783 | } | |
09f6a240 | 784 | |
c2009e53 | 785 | backup_dir = Some(dir); |
133042b5 | 786 | worker_type = "verify_snapshot"; |
c2009e53 DM |
787 | } |
788 | (Some(backup_type), Some(backup_id), None) => { | |
8c74349b WB |
789 | worker_id = format!( |
790 | "{}:{}/{}/{}", | |
791 | store, | |
792 | backup_ns.display_as_path(), | |
793 | backup_type, | |
794 | backup_id | |
795 | ); | |
133d718f | 796 | let group = pbs_api_types::BackupGroup::from((backup_type, backup_id)); |
09f6a240 | 797 | |
a724f5fd FG |
798 | if owner_check_required { |
799 | let owner = datastore.get_owner(&backup_ns, &group)?; | |
800 | check_backup_owner(&owner, &auth_id)?; | |
801 | } | |
09f6a240 | 802 | |
133d718f | 803 | backup_group = Some(datastore.backup_group(backup_ns, group)); |
133042b5 | 804 | worker_type = "verify_group"; |
c2009e53 DM |
805 | } |
806 | (None, None, None) => { | |
8ea00f6e | 807 | worker_id = store.clone(); |
c2009e53 | 808 | } |
5a718dce | 809 | _ => bail!("parameters do not specify a backup group or snapshot"), |
c2009e53 DM |
810 | } |
811 | ||
39735609 | 812 | let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI; |
c2009e53 DM |
813 | |
814 | let upid_str = WorkerTask::new_thread( | |
133042b5 | 815 | worker_type, |
44288184 | 816 | Some(worker_id), |
049a22a3 | 817 | auth_id.to_string(), |
e7cb4dc5 WB |
818 | to_stdout, |
819 | move |worker| { | |
9c26a3d6 | 820 | let verify_worker = crate::backup::VerifyWorker::new(worker.clone(), datastore); |
adfdc369 | 821 | let failed_dirs = if let Some(backup_dir) = backup_dir { |
adfdc369 | 822 | let mut res = Vec::new(); |
f6b1d1cc | 823 | if !verify_backup_dir( |
9c26a3d6 | 824 | &verify_worker, |
f6b1d1cc | 825 | &backup_dir, |
f6b1d1cc | 826 | worker.upid().clone(), |
dc7a5b34 | 827 | Some(&move |manifest| verify_filter(ignore_verified, outdated_after, manifest)), |
f6b1d1cc | 828 | )? { |
adfdc369 DC |
829 | res.push(backup_dir.to_string()); |
830 | } | |
831 | res | |
c2009e53 | 832 | } else if let Some(backup_group) = backup_group { |
7e25b9aa | 833 | let failed_dirs = verify_backup_group( |
9c26a3d6 | 834 | &verify_worker, |
63d9aca9 | 835 | &backup_group, |
7e25b9aa | 836 | &mut StoreProgress::new(1), |
f6b1d1cc | 837 | worker.upid(), |
dc7a5b34 | 838 | Some(&move |manifest| verify_filter(ignore_verified, outdated_after, manifest)), |
63d9aca9 DM |
839 | )?; |
840 | failed_dirs | |
c2009e53 | 841 | } else { |
a724f5fd | 842 | let owner = if owner_check_required { |
09f6a240 FG |
843 | Some(auth_id) |
844 | } else { | |
845 | None | |
846 | }; | |
847 | ||
a724f5fd FG |
848 | // FIXME namespace missing here.. |
849 | ||
dcbf29e7 HL |
850 | verify_all_backups( |
851 | &verify_worker, | |
852 | worker.upid(), | |
853 | owner, | |
dc7a5b34 | 854 | Some(&move |manifest| verify_filter(ignore_verified, outdated_after, manifest)), |
dcbf29e7 | 855 | )? |
c2009e53 | 856 | }; |
3984a5fd | 857 | if !failed_dirs.is_empty() { |
1ec0d70d | 858 | task_log!(worker, "Failed to verify the following snapshots/groups:"); |
adfdc369 | 859 | for dir in failed_dirs { |
1ec0d70d | 860 | task_log!(worker, "\t{}", dir); |
adfdc369 | 861 | } |
1ffe0301 | 862 | bail!("verification failed - please check the log for details"); |
c2009e53 DM |
863 | } |
864 | Ok(()) | |
e7cb4dc5 WB |
865 | }, |
866 | )?; | |
c2009e53 DM |
867 | |
868 | Ok(json!(upid_str)) | |
869 | } | |
870 | ||
0a240aaa DC |
871 | #[api( |
872 | input: { | |
873 | properties: { | |
133d718f WB |
874 | "backup-ns": { |
875 | type: BackupNamespace, | |
876 | optional: true, | |
877 | }, | |
8c74349b WB |
878 | group: { |
879 | type: pbs_api_types::BackupGroup, | |
880 | flatten: true, | |
881 | }, | |
0a240aaa DC |
882 | "dry-run": { |
883 | optional: true, | |
884 | type: bool, | |
885 | default: false, | |
886 | description: "Just show what prune would do, but do not delete anything.", | |
887 | }, | |
888 | "prune-options": { | |
889 | type: PruneOptions, | |
890 | flatten: true, | |
891 | }, | |
892 | store: { | |
893 | schema: DATASTORE_SCHEMA, | |
894 | }, | |
895 | }, | |
896 | }, | |
7b570c17 | 897 | returns: pbs_api_types::ADMIN_DATASTORE_PRUNE_RETURN_TYPE, |
0a240aaa | 898 | access: { |
7d6fc15b TL |
899 | permission: &Permission::Anybody, |
900 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any\ | |
901 | or DATASTORE_PRUNE and being the owner of the group", | |
0a240aaa DC |
902 | }, |
903 | )] | |
9805207a | 904 | /// Prune a group on the datastore |
bf78f708 | 905 | pub fn prune( |
133d718f | 906 | backup_ns: Option<BackupNamespace>, |
8c74349b | 907 | group: pbs_api_types::BackupGroup, |
0a240aaa DC |
908 | dry_run: bool, |
909 | prune_options: PruneOptions, | |
910 | store: String, | |
911 | _param: Value, | |
54552dda | 912 | rpcenv: &mut dyn RpcEnvironment, |
83b7db02 | 913 | ) -> Result<Value, Error> { |
e6dc35ac | 914 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f WB |
915 | |
916 | let backup_ns = backup_ns.unwrap_or_default(); | |
917 | ||
2bc2435a | 918 | let owner_check_required = check_ns_privs( |
7d6fc15b | 919 | &store, |
133d718f | 920 | &backup_ns, |
7d6fc15b | 921 | &auth_id, |
2bc2435a FG |
922 | PRIV_DATASTORE_MODIFY, |
923 | PRIV_DATASTORE_PRUNE, | |
7d6fc15b | 924 | )?; |
54552dda | 925 | |
e9d2fc93 | 926 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
54552dda | 927 | |
2bc2435a | 928 | if owner_check_required { |
a724f5fd FG |
929 | let owner = datastore.get_owner(&backup_ns, &group)?; |
930 | check_backup_owner(&owner, &auth_id)?; | |
931 | } | |
db87d93e | 932 | |
a724f5fd | 933 | let group = datastore.backup_group(backup_ns, group); |
83b7db02 | 934 | |
8c74349b | 935 | let worker_id = format!("{}:{}", store, group); |
503995c7 | 936 | |
dda70154 DM |
937 | let mut prune_result = Vec::new(); |
938 | ||
6da20161 | 939 | let list = group.list_backups()?; |
dda70154 DM |
940 | |
941 | let mut prune_info = compute_prune_info(list, &prune_options)?; | |
942 | ||
943 | prune_info.reverse(); // delete older snapshots first | |
944 | ||
89725197 | 945 | let keep_all = !pbs_datastore::prune::keeps_something(&prune_options); |
dda70154 DM |
946 | |
947 | if dry_run { | |
02db7267 DC |
948 | for (info, mark) in prune_info { |
949 | let keep = keep_all || mark.keep(); | |
dda70154 | 950 | |
33f2c2a1 | 951 | let mut result = json!({ |
db87d93e WB |
952 | "backup-type": info.backup_dir.backup_type(), |
953 | "backup-id": info.backup_dir.backup_id(), | |
954 | "backup-time": info.backup_dir.backup_time(), | |
dda70154 | 955 | "keep": keep, |
02db7267 | 956 | "protected": mark.protected(), |
33f2c2a1 WB |
957 | }); |
958 | let ns = info.backup_dir.backup_ns(); | |
959 | if !ns.is_root() { | |
960 | result["backup-ns"] = serde_json::to_value(ns)?; | |
961 | } | |
962 | prune_result.push(result); | |
dda70154 DM |
963 | } |
964 | return Ok(json!(prune_result)); | |
965 | } | |
966 | ||
163e9bbe | 967 | // We use a WorkerTask just to have a task log, but run synchrounously |
049a22a3 | 968 | let worker = WorkerTask::new("prune", Some(worker_id), auth_id.to_string(), true)?; |
dda70154 | 969 | |
f1539300 | 970 | if keep_all { |
1ec0d70d | 971 | task_log!(worker, "No prune selection - keeping all files."); |
f1539300 | 972 | } else { |
dc7a5b34 TL |
973 | task_log!( |
974 | worker, | |
975 | "retention options: {}", | |
976 | pbs_datastore::prune::cli_options_string(&prune_options) | |
977 | ); | |
978 | task_log!( | |
979 | worker, | |
8c74349b | 980 | "Starting prune on store \"{}\" group \"{}\"", |
dc7a5b34 | 981 | store, |
8c74349b | 982 | group, |
dc7a5b34 | 983 | ); |
f1539300 | 984 | } |
3b03abfe | 985 | |
02db7267 DC |
986 | for (info, mark) in prune_info { |
987 | let keep = keep_all || mark.keep(); | |
dda70154 | 988 | |
f1539300 SR |
989 | let backup_time = info.backup_dir.backup_time(); |
990 | let timestamp = info.backup_dir.backup_time_string(); | |
db87d93e WB |
991 | let group: &pbs_api_types::BackupGroup = info.backup_dir.as_ref(); |
992 | ||
993 | let msg = format!("{}/{}/{} {}", group.ty, group.id, timestamp, mark,); | |
f1539300 | 994 | |
1ec0d70d | 995 | task_log!(worker, "{}", msg); |
f1539300 | 996 | |
133d718f | 997 | prune_result.push(json!({ |
db87d93e WB |
998 | "backup-type": group.ty, |
999 | "backup-id": group.id, | |
f1539300 SR |
1000 | "backup-time": backup_time, |
1001 | "keep": keep, | |
02db7267 | 1002 | "protected": mark.protected(), |
133d718f | 1003 | })); |
f1539300 SR |
1004 | |
1005 | if !(dry_run || keep) { | |
133d718f | 1006 | if let Err(err) = info.backup_dir.destroy(false) { |
1ec0d70d DM |
1007 | task_warn!( |
1008 | worker, | |
1009 | "failed to remove dir {:?}: {}", | |
1010 | info.backup_dir.relative_path(), | |
1011 | err, | |
f1539300 | 1012 | ); |
8f0b4c1f | 1013 | } |
8f579717 | 1014 | } |
f1539300 | 1015 | } |
dd8e744f | 1016 | |
f1539300 | 1017 | worker.log_result(&Ok(())); |
83b7db02 | 1018 | |
dda70154 | 1019 | Ok(json!(prune_result)) |
83b7db02 DM |
1020 | } |
1021 | ||
9805207a DC |
1022 | #[api( |
1023 | input: { | |
1024 | properties: { | |
1025 | "dry-run": { | |
1026 | optional: true, | |
1027 | type: bool, | |
1028 | default: false, | |
1029 | description: "Just show what prune would do, but do not delete anything.", | |
1030 | }, | |
1031 | "prune-options": { | |
1032 | type: PruneOptions, | |
1033 | flatten: true, | |
1034 | }, | |
1035 | store: { | |
1036 | schema: DATASTORE_SCHEMA, | |
1037 | }, | |
2f5417f8 TL |
1038 | ns: { |
1039 | type: BackupNamespace, | |
1040 | optional: true, | |
1041 | }, | |
9805207a DC |
1042 | }, |
1043 | }, | |
1044 | returns: { | |
1045 | schema: UPID_SCHEMA, | |
1046 | }, | |
1047 | access: { | |
7d6fc15b TL |
1048 | permission: &Permission::Privilege( |
1049 | &["datastore", "{store}"], PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE, true), | |
9805207a DC |
1050 | }, |
1051 | )] | |
1052 | /// Prune the datastore | |
1053 | pub fn prune_datastore( | |
1054 | dry_run: bool, | |
1055 | prune_options: PruneOptions, | |
1056 | store: String, | |
2f5417f8 | 1057 | ns: Option<BackupNamespace>, |
9805207a DC |
1058 | _param: Value, |
1059 | rpcenv: &mut dyn RpcEnvironment, | |
1060 | ) -> Result<String, Error> { | |
9805207a DC |
1061 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
1062 | ||
e9d2fc93 | 1063 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
9805207a | 1064 | |
bfa942c0 DC |
1065 | let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI; |
1066 | ||
7d6fc15b TL |
1067 | // FIXME: also allow a per-namespace pruning with max-depth |
1068 | ||
9805207a DC |
1069 | let upid_str = WorkerTask::new_thread( |
1070 | "prune", | |
1071 | Some(store.clone()), | |
049a22a3 | 1072 | auth_id.to_string(), |
bfa942c0 | 1073 | to_stdout, |
dc7a5b34 TL |
1074 | move |worker| { |
1075 | crate::server::prune_datastore( | |
1076 | worker, | |
1077 | auth_id, | |
1078 | prune_options, | |
dc7a5b34 | 1079 | datastore, |
2f5417f8 | 1080 | ns.unwrap_or_default(), |
dc7a5b34 TL |
1081 | dry_run, |
1082 | ) | |
1083 | }, | |
9805207a DC |
1084 | )?; |
1085 | ||
1086 | Ok(upid_str) | |
1087 | } | |
1088 | ||
dfc58d47 DM |
1089 | #[api( |
1090 | input: { | |
1091 | properties: { | |
1092 | store: { | |
1093 | schema: DATASTORE_SCHEMA, | |
1094 | }, | |
1095 | }, | |
1096 | }, | |
1097 | returns: { | |
1098 | schema: UPID_SCHEMA, | |
1099 | }, | |
bb34b589 | 1100 | access: { |
54552dda | 1101 | permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false), |
bb34b589 | 1102 | }, |
dfc58d47 DM |
1103 | )] |
1104 | /// Start garbage collection. | |
bf78f708 | 1105 | pub fn start_garbage_collection( |
dfc58d47 | 1106 | store: String, |
6049b71f | 1107 | _info: &ApiMethod, |
dd5495d6 | 1108 | rpcenv: &mut dyn RpcEnvironment, |
6049b71f | 1109 | ) -> Result<Value, Error> { |
e9d2fc93 | 1110 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
e6dc35ac | 1111 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
15e9b4ed | 1112 | |
dc7a5b34 | 1113 | let job = Job::new("garbage_collection", &store) |
4fdf5ddf | 1114 | .map_err(|_| format_err!("garbage collection already running"))?; |
15e9b4ed | 1115 | |
39735609 | 1116 | let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI; |
15e9b4ed | 1117 | |
dc7a5b34 TL |
1118 | let upid_str = |
1119 | crate::server::do_garbage_collection_job(job, datastore, &auth_id, None, to_stdout) | |
1120 | .map_err(|err| { | |
1121 | format_err!( | |
1122 | "unable to start garbage collection job on datastore {} - {}", | |
1123 | store, | |
1124 | err | |
1125 | ) | |
1126 | })?; | |
0f778e06 DM |
1127 | |
1128 | Ok(json!(upid_str)) | |
15e9b4ed DM |
1129 | } |
1130 | ||
a92830dc DM |
1131 | #[api( |
1132 | input: { | |
1133 | properties: { | |
1134 | store: { | |
1135 | schema: DATASTORE_SCHEMA, | |
1136 | }, | |
1137 | }, | |
1138 | }, | |
1139 | returns: { | |
1140 | type: GarbageCollectionStatus, | |
bb34b589 DM |
1141 | }, |
1142 | access: { | |
1143 | permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, false), | |
1144 | }, | |
a92830dc DM |
1145 | )] |
1146 | /// Garbage collection status. | |
5eeea607 | 1147 | pub fn garbage_collection_status( |
a92830dc | 1148 | store: String, |
6049b71f | 1149 | _info: &ApiMethod, |
dd5495d6 | 1150 | _rpcenv: &mut dyn RpcEnvironment, |
a92830dc | 1151 | ) -> Result<GarbageCollectionStatus, Error> { |
e9d2fc93 | 1152 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
f2b99c34 | 1153 | |
f2b99c34 | 1154 | let status = datastore.last_gc_status(); |
691c89a0 | 1155 | |
a92830dc | 1156 | Ok(status) |
691c89a0 DM |
1157 | } |
1158 | ||
7d6fc15b TL |
1159 | fn can_access_any_ns(store: Arc<DataStore>, auth_id: &Authid, user_info: &CachedUserInfo) -> bool { |
1160 | // NOTE: traversing the datastore could be avoided if we had an "ACL tree: is there any priv | |
1161 | // below /datastore/{store}" helper | |
1162 | let mut iter = | |
1163 | if let Ok(iter) = store.recursive_iter_backup_ns_ok(BackupNamespace::root(), None) { | |
1164 | iter | |
1165 | } else { | |
1166 | return false; | |
1167 | }; | |
1168 | let wanted = | |
1169 | PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP; | |
1170 | let name = store.name(); | |
1171 | iter.any(|ns| -> bool { | |
1172 | let user_privs = user_info.lookup_privs(&auth_id, &["datastore", name, &ns.to_string()]); | |
1173 | user_privs & wanted != 0 | |
1174 | }) | |
1175 | } | |
1176 | ||
bb34b589 | 1177 | #[api( |
30fb6025 DM |
1178 | returns: { |
1179 | description: "List the accessible datastores.", | |
1180 | type: Array, | |
9b93c620 | 1181 | items: { type: DataStoreListItem }, |
30fb6025 | 1182 | }, |
bb34b589 | 1183 | access: { |
54552dda | 1184 | permission: &Permission::Anybody, |
bb34b589 DM |
1185 | }, |
1186 | )] | |
1187 | /// Datastore list | |
bf78f708 | 1188 | pub fn get_datastore_list( |
6049b71f DM |
1189 | _param: Value, |
1190 | _info: &ApiMethod, | |
54552dda | 1191 | rpcenv: &mut dyn RpcEnvironment, |
455e5f71 | 1192 | ) -> Result<Vec<DataStoreListItem>, Error> { |
e7d4be9d | 1193 | let (config, _digest) = pbs_config::datastore::config()?; |
15e9b4ed | 1194 | |
e6dc35ac | 1195 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
54552dda DM |
1196 | let user_info = CachedUserInfo::new()?; |
1197 | ||
30fb6025 | 1198 | let mut list = Vec::new(); |
54552dda | 1199 | |
30fb6025 | 1200 | for (store, (_, data)) in &config.sections { |
9a37bd6c | 1201 | let user_privs = user_info.lookup_privs(&auth_id, &["datastore", store]); |
dc7a5b34 | 1202 | let allowed = (user_privs & (PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP)) != 0; |
7d6fc15b TL |
1203 | |
1204 | let mut allow_id = false; | |
1205 | if !allowed { | |
1206 | let scfg: pbs_api_types::DataStoreConfig = serde_json::from_value(data.to_owned())?; | |
1207 | // safety: we just cannot go through lookup as we must avoid an operation check | |
1208 | if let Ok(datastore) = unsafe { DataStore::open_from_config(scfg, None) } { | |
1209 | allow_id = can_access_any_ns(datastore, &auth_id, &user_info); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | if allowed || allow_id { | |
dc7a5b34 TL |
1214 | list.push(DataStoreListItem { |
1215 | store: store.clone(), | |
7d6fc15b TL |
1216 | comment: if !allowed { |
1217 | None | |
1218 | } else { | |
1219 | data["comment"].as_str().map(String::from) | |
1220 | }, | |
e022d13c | 1221 | maintenance: data["maintenance-mode"].as_str().map(String::from), |
dc7a5b34 | 1222 | }); |
30fb6025 | 1223 | } |
54552dda DM |
1224 | } |
1225 | ||
44288184 | 1226 | Ok(list) |
15e9b4ed DM |
1227 | } |
1228 | ||
0ab08ac9 DM |
1229 | #[sortable] |
1230 | pub const API_METHOD_DOWNLOAD_FILE: ApiMethod = ApiMethod::new( | |
1231 | &ApiHandler::AsyncHttp(&download_file), | |
1232 | &ObjectSchema::new( | |
1233 | "Download single raw file from backup snapshot.", | |
1234 | &sorted!([ | |
66c49c21 | 1235 | ("store", false, &DATASTORE_SCHEMA), |
33f2c2a1 | 1236 | ("backup-ns", true, &BACKUP_NAMESPACE_SCHEMA), |
0ab08ac9 | 1237 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
dc7a5b34 | 1238 | ("backup-id", false, &BACKUP_ID_SCHEMA), |
0ab08ac9 | 1239 | ("backup-time", false, &BACKUP_TIME_SCHEMA), |
4191018c | 1240 | ("file-name", false, &BACKUP_ARCHIVE_NAME_SCHEMA), |
0ab08ac9 | 1241 | ]), |
dc7a5b34 TL |
1242 | ), |
1243 | ) | |
1244 | .access( | |
7d6fc15b TL |
1245 | Some( |
1246 | "Requires on /datastore/{store}[/{namespace}] either DATASTORE_READ for any or \ | |
1247 | DATASTORE_BACKUP and being the owner of the group", | |
dc7a5b34 | 1248 | ), |
7d6fc15b | 1249 | &Permission::Anybody, |
54552dda | 1250 | ); |
691c89a0 | 1251 | |
bf78f708 | 1252 | pub fn download_file( |
9e47c0a5 DM |
1253 | _parts: Parts, |
1254 | _req_body: Body, | |
1255 | param: Value, | |
255f378a | 1256 | _info: &ApiMethod, |
54552dda | 1257 | rpcenv: Box<dyn RpcEnvironment>, |
bb084b9c | 1258 | ) -> ApiResponseFuture { |
ad51d02a | 1259 | async move { |
7d6fc15b | 1260 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
3c8c2827 | 1261 | let store = required_string_param(¶m, "store")?; |
133d718f | 1262 | let backup_ns = optional_ns_param(¶m)?; |
7d6fc15b | 1263 | let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; |
2bc2435a | 1264 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1265 | &store, |
133d718f | 1266 | &backup_ns, |
7d6fc15b | 1267 | &auth_id, |
2bc2435a FG |
1268 | PRIV_DATASTORE_READ, |
1269 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 1270 | )?; |
e9d2fc93 | 1271 | let datastore = DataStore::lookup_datastore(store, Some(Operation::Read))?; |
a724f5fd | 1272 | |
2bc2435a | 1273 | if owner_check_required { |
a724f5fd FG |
1274 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
1275 | check_backup_owner(&owner, &auth_id)?; | |
1276 | } | |
133d718f | 1277 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
54552dda | 1278 | |
3c8c2827 | 1279 | let file_name = required_string_param(¶m, "file-name")?.to_owned(); |
9e47c0a5 | 1280 | |
dc7a5b34 TL |
1281 | println!( |
1282 | "Download {} from {} ({}/{})", | |
1283 | file_name, store, backup_dir, file_name | |
1284 | ); | |
9e47c0a5 | 1285 | |
ad51d02a DM |
1286 | let mut path = datastore.base_path(); |
1287 | path.push(backup_dir.relative_path()); | |
1288 | path.push(&file_name); | |
1289 | ||
ba694720 | 1290 | let file = tokio::fs::File::open(&path) |
8aa67ee7 WB |
1291 | .await |
1292 | .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?; | |
ad51d02a | 1293 | |
dc7a5b34 TL |
1294 | let payload = |
1295 | tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) | |
1296 | .map_ok(|bytes| bytes.freeze()) | |
1297 | .map_err(move |err| { | |
1298 | eprintln!("error during streaming of '{:?}' - {}", &path, err); | |
1299 | err | |
1300 | }); | |
ad51d02a | 1301 | let body = Body::wrap_stream(payload); |
9e47c0a5 | 1302 | |
ad51d02a DM |
1303 | // fixme: set other headers ? |
1304 | Ok(Response::builder() | |
dc7a5b34 TL |
1305 | .status(StatusCode::OK) |
1306 | .header(header::CONTENT_TYPE, "application/octet-stream") | |
1307 | .body(body) | |
1308 | .unwrap()) | |
1309 | } | |
1310 | .boxed() | |
9e47c0a5 DM |
1311 | } |
1312 | ||
6ef9bb59 DC |
1313 | #[sortable] |
1314 | pub const API_METHOD_DOWNLOAD_FILE_DECODED: ApiMethod = ApiMethod::new( | |
1315 | &ApiHandler::AsyncHttp(&download_file_decoded), | |
1316 | &ObjectSchema::new( | |
1317 | "Download single decoded file from backup snapshot. Only works if it's not encrypted.", | |
1318 | &sorted!([ | |
1319 | ("store", false, &DATASTORE_SCHEMA), | |
33f2c2a1 | 1320 | ("backup-ns", true, &BACKUP_NAMESPACE_SCHEMA), |
6ef9bb59 | 1321 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
dc7a5b34 | 1322 | ("backup-id", false, &BACKUP_ID_SCHEMA), |
6ef9bb59 DC |
1323 | ("backup-time", false, &BACKUP_TIME_SCHEMA), |
1324 | ("file-name", false, &BACKUP_ARCHIVE_NAME_SCHEMA), | |
1325 | ]), | |
dc7a5b34 TL |
1326 | ), |
1327 | ) | |
1328 | .access( | |
7d6fc15b TL |
1329 | Some( |
1330 | "Requires on /datastore/{store}[/{namespace}] either DATASTORE_READ for any or \ | |
1331 | DATASTORE_BACKUP and being the owner of the group", | |
dc7a5b34 | 1332 | ), |
7d6fc15b | 1333 | &Permission::Anybody, |
6ef9bb59 DC |
1334 | ); |
1335 | ||
bf78f708 | 1336 | pub fn download_file_decoded( |
6ef9bb59 DC |
1337 | _parts: Parts, |
1338 | _req_body: Body, | |
1339 | param: Value, | |
1340 | _info: &ApiMethod, | |
1341 | rpcenv: Box<dyn RpcEnvironment>, | |
1342 | ) -> ApiResponseFuture { | |
6ef9bb59 | 1343 | async move { |
7d6fc15b | 1344 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
3c8c2827 | 1345 | let store = required_string_param(¶m, "store")?; |
133d718f | 1346 | let backup_ns = optional_ns_param(¶m)?; |
7d6fc15b | 1347 | let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; |
2bc2435a | 1348 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1349 | &store, |
133d718f | 1350 | &backup_ns, |
7d6fc15b | 1351 | &auth_id, |
2bc2435a FG |
1352 | PRIV_DATASTORE_READ, |
1353 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 1354 | )?; |
e9d2fc93 | 1355 | let datastore = DataStore::lookup_datastore(store, Some(Operation::Read))?; |
a724f5fd | 1356 | |
2bc2435a | 1357 | if owner_check_required { |
a724f5fd FG |
1358 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
1359 | check_backup_owner(&owner, &auth_id)?; | |
1360 | } | |
1361 | ||
133d718f | 1362 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
6ef9bb59 | 1363 | |
3c8c2827 | 1364 | let file_name = required_string_param(¶m, "file-name")?.to_owned(); |
6ef9bb59 | 1365 | |
2d55beec | 1366 | let (manifest, files) = read_backup_index(&datastore, &backup_dir)?; |
6ef9bb59 | 1367 | for file in files { |
f28d9088 | 1368 | if file.filename == file_name && file.crypt_mode == Some(CryptMode::Encrypt) { |
6ef9bb59 DC |
1369 | bail!("cannot decode '{}' - is encrypted", file_name); |
1370 | } | |
1371 | } | |
1372 | ||
dc7a5b34 TL |
1373 | println!( |
1374 | "Download {} from {} ({}/{})", | |
1375 | file_name, store, backup_dir, file_name | |
1376 | ); | |
6ef9bb59 DC |
1377 | |
1378 | let mut path = datastore.base_path(); | |
1379 | path.push(backup_dir.relative_path()); | |
1380 | path.push(&file_name); | |
1381 | ||
1382 | let extension = file_name.rsplitn(2, '.').next().unwrap(); | |
1383 | ||
1384 | let body = match extension { | |
1385 | "didx" => { | |
dc7a5b34 TL |
1386 | let index = DynamicIndexReader::open(&path).map_err(|err| { |
1387 | format_err!("unable to read dynamic index '{:?}' - {}", &path, err) | |
1388 | })?; | |
2d55beec FG |
1389 | let (csum, size) = index.compute_csum(); |
1390 | manifest.verify_file(&file_name, &csum, size)?; | |
6ef9bb59 | 1391 | |
14f6c9cb | 1392 | let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None); |
1ef6e8b6 | 1393 | let reader = CachedChunkReader::new(chunk_reader, index, 1).seekable(); |
dc7a5b34 TL |
1394 | Body::wrap_stream(AsyncReaderStream::new(reader).map_err(move |err| { |
1395 | eprintln!("error during streaming of '{:?}' - {}", path, err); | |
1396 | err | |
1397 | })) | |
1398 | } | |
6ef9bb59 | 1399 | "fidx" => { |
dc7a5b34 TL |
1400 | let index = FixedIndexReader::open(&path).map_err(|err| { |
1401 | format_err!("unable to read fixed index '{:?}' - {}", &path, err) | |
1402 | })?; | |
6ef9bb59 | 1403 | |
2d55beec FG |
1404 | let (csum, size) = index.compute_csum(); |
1405 | manifest.verify_file(&file_name, &csum, size)?; | |
1406 | ||
14f6c9cb | 1407 | let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None); |
1ef6e8b6 | 1408 | let reader = CachedChunkReader::new(chunk_reader, index, 1).seekable(); |
dc7a5b34 TL |
1409 | Body::wrap_stream( |
1410 | AsyncReaderStream::with_buffer_size(reader, 4 * 1024 * 1024).map_err( | |
1411 | move |err| { | |
1412 | eprintln!("error during streaming of '{:?}' - {}", path, err); | |
1413 | err | |
1414 | }, | |
1415 | ), | |
1416 | ) | |
1417 | } | |
6ef9bb59 DC |
1418 | "blob" => { |
1419 | let file = std::fs::File::open(&path) | |
8aa67ee7 | 1420 | .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?; |
6ef9bb59 | 1421 | |
2d55beec FG |
1422 | // FIXME: load full blob to verify index checksum? |
1423 | ||
6ef9bb59 | 1424 | Body::wrap_stream( |
dc7a5b34 TL |
1425 | WrappedReaderStream::new(DataBlobReader::new(file, None)?).map_err( |
1426 | move |err| { | |
6ef9bb59 DC |
1427 | eprintln!("error during streaming of '{:?}' - {}", path, err); |
1428 | err | |
dc7a5b34 TL |
1429 | }, |
1430 | ), | |
6ef9bb59 | 1431 | ) |
dc7a5b34 | 1432 | } |
6ef9bb59 DC |
1433 | extension => { |
1434 | bail!("cannot download '{}' files", extension); | |
dc7a5b34 | 1435 | } |
6ef9bb59 DC |
1436 | }; |
1437 | ||
1438 | // fixme: set other headers ? | |
1439 | Ok(Response::builder() | |
dc7a5b34 TL |
1440 | .status(StatusCode::OK) |
1441 | .header(header::CONTENT_TYPE, "application/octet-stream") | |
1442 | .body(body) | |
1443 | .unwrap()) | |
1444 | } | |
1445 | .boxed() | |
6ef9bb59 DC |
1446 | } |
1447 | ||
552c2259 | 1448 | #[sortable] |
0ab08ac9 DM |
1449 | pub const API_METHOD_UPLOAD_BACKUP_LOG: ApiMethod = ApiMethod::new( |
1450 | &ApiHandler::AsyncHttp(&upload_backup_log), | |
255f378a | 1451 | &ObjectSchema::new( |
54552dda | 1452 | "Upload the client backup log file into a backup snapshot ('client.log.blob').", |
552c2259 | 1453 | &sorted!([ |
66c49c21 | 1454 | ("store", false, &DATASTORE_SCHEMA), |
33f2c2a1 | 1455 | ("backup-ns", true, &BACKUP_NAMESPACE_SCHEMA), |
255f378a | 1456 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
0ab08ac9 | 1457 | ("backup-id", false, &BACKUP_ID_SCHEMA), |
255f378a | 1458 | ("backup-time", false, &BACKUP_TIME_SCHEMA), |
552c2259 | 1459 | ]), |
dc7a5b34 TL |
1460 | ), |
1461 | ) | |
1462 | .access( | |
54552dda | 1463 | Some("Only the backup creator/owner is allowed to do this."), |
7d6fc15b | 1464 | &Permission::Anybody, |
54552dda | 1465 | ); |
9e47c0a5 | 1466 | |
bf78f708 | 1467 | pub fn upload_backup_log( |
07ee2235 DM |
1468 | _parts: Parts, |
1469 | req_body: Body, | |
1470 | param: Value, | |
255f378a | 1471 | _info: &ApiMethod, |
54552dda | 1472 | rpcenv: Box<dyn RpcEnvironment>, |
bb084b9c | 1473 | ) -> ApiResponseFuture { |
ad51d02a | 1474 | async move { |
7d6fc15b | 1475 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
3c8c2827 | 1476 | let store = required_string_param(¶m, "store")?; |
133d718f | 1477 | let backup_ns = optional_ns_param(¶m)?; |
7d6fc15b | 1478 | let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; |
2bc2435a FG |
1479 | |
1480 | check_ns_privs(&store, &backup_ns, &auth_id, PRIV_DATASTORE_BACKUP, 0)?; | |
1481 | ||
e9d2fc93 | 1482 | let datastore = DataStore::lookup_datastore(store, Some(Operation::Write))?; |
133d718f | 1483 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
07ee2235 | 1484 | |
dc7a5b34 | 1485 | let file_name = CLIENT_LOG_BLOB_NAME; |
07ee2235 | 1486 | |
133d718f | 1487 | let owner = backup_dir.get_owner()?; |
bff85572 | 1488 | check_backup_owner(&owner, &auth_id)?; |
54552dda | 1489 | |
133d718f | 1490 | let mut path = backup_dir.full_path(); |
ad51d02a | 1491 | path.push(&file_name); |
07ee2235 | 1492 | |
ad51d02a DM |
1493 | if path.exists() { |
1494 | bail!("backup already contains a log."); | |
1495 | } | |
e128d4e8 | 1496 | |
8c74349b | 1497 | println!("Upload backup log to {store}/{backup_dir}/{file_name}"); |
ad51d02a DM |
1498 | |
1499 | let data = req_body | |
1500 | .map_err(Error::from) | |
1501 | .try_fold(Vec::new(), |mut acc, chunk| { | |
1502 | acc.extend_from_slice(&*chunk); | |
1503 | future::ok::<_, Error>(acc) | |
1504 | }) | |
1505 | .await?; | |
1506 | ||
39f18b30 DM |
1507 | // always verify blob/CRC at server side |
1508 | let blob = DataBlob::load_from_reader(&mut &data[..])?; | |
1509 | ||
e0a19d33 | 1510 | replace_file(&path, blob.raw_data(), CreateOptions::new(), false)?; |
ad51d02a DM |
1511 | |
1512 | // fixme: use correct formatter | |
53daae8e | 1513 | Ok(formatter::JSON_FORMATTER.format_data(Value::Null, &*rpcenv)) |
dc7a5b34 TL |
1514 | } |
1515 | .boxed() | |
07ee2235 DM |
1516 | } |
1517 | ||
5b1cfa01 DC |
1518 | #[api( |
1519 | input: { | |
1520 | properties: { | |
988d575d | 1521 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
1522 | "backup-ns": { |
1523 | type: BackupNamespace, | |
1524 | optional: true, | |
1525 | }, | |
8c74349b WB |
1526 | backup_dir: { |
1527 | type: pbs_api_types::BackupDir, | |
1528 | flatten: true, | |
1529 | }, | |
5b1cfa01 DC |
1530 | "filepath": { |
1531 | description: "Base64 encoded path.", | |
1532 | type: String, | |
1533 | } | |
1534 | }, | |
1535 | }, | |
1536 | access: { | |
7d6fc15b TL |
1537 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_READ for any or \ |
1538 | DATASTORE_BACKUP and being the owner of the group", | |
1539 | permission: &Permission::Anybody, | |
5b1cfa01 DC |
1540 | }, |
1541 | )] | |
1542 | /// Get the entries of the given path of the catalog | |
bf78f708 | 1543 | pub fn catalog( |
5b1cfa01 | 1544 | store: String, |
133d718f | 1545 | backup_ns: Option<BackupNamespace>, |
8c74349b | 1546 | backup_dir: pbs_api_types::BackupDir, |
5b1cfa01 | 1547 | filepath: String, |
5b1cfa01 | 1548 | rpcenv: &mut dyn RpcEnvironment, |
227501c0 | 1549 | ) -> Result<Vec<ArchiveEntry>, Error> { |
e6dc35ac | 1550 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 1551 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 1552 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1553 | &store, |
133d718f | 1554 | &backup_ns, |
7d6fc15b | 1555 | &auth_id, |
2bc2435a FG |
1556 | PRIV_DATASTORE_READ, |
1557 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 1558 | )?; |
5b1cfa01 | 1559 | |
7d6fc15b | 1560 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
5b1cfa01 | 1561 | |
2bc2435a | 1562 | if owner_check_required { |
a724f5fd FG |
1563 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
1564 | check_backup_owner(&owner, &auth_id)?; | |
1565 | } | |
1566 | ||
1567 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; | |
5b1cfa01 | 1568 | |
9238cdf5 FG |
1569 | let file_name = CATALOG_NAME; |
1570 | ||
2d55beec | 1571 | let (manifest, files) = read_backup_index(&datastore, &backup_dir)?; |
9238cdf5 FG |
1572 | for file in files { |
1573 | if file.filename == file_name && file.crypt_mode == Some(CryptMode::Encrypt) { | |
1574 | bail!("cannot decode '{}' - is encrypted", file_name); | |
1575 | } | |
1576 | } | |
1577 | ||
5b1cfa01 DC |
1578 | let mut path = datastore.base_path(); |
1579 | path.push(backup_dir.relative_path()); | |
9238cdf5 | 1580 | path.push(file_name); |
5b1cfa01 DC |
1581 | |
1582 | let index = DynamicIndexReader::open(&path) | |
1583 | .map_err(|err| format_err!("unable to read dynamic index '{:?}' - {}", &path, err))?; | |
1584 | ||
2d55beec | 1585 | let (csum, size) = index.compute_csum(); |
9a37bd6c | 1586 | manifest.verify_file(file_name, &csum, size)?; |
2d55beec | 1587 | |
14f6c9cb | 1588 | let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None); |
5b1cfa01 DC |
1589 | let reader = BufferedDynamicReader::new(index, chunk_reader); |
1590 | ||
1591 | let mut catalog_reader = CatalogReader::new(reader); | |
5b1cfa01 | 1592 | |
5279ee74 | 1593 | let path = if filepath != "root" && filepath != "/" { |
227501c0 DC |
1594 | base64::decode(filepath)? |
1595 | } else { | |
1596 | vec![b'/'] | |
1597 | }; | |
5b1cfa01 | 1598 | |
86582454 | 1599 | catalog_reader.list_dir_contents(&path) |
5b1cfa01 DC |
1600 | } |
1601 | ||
d33d8f4e DC |
1602 | #[sortable] |
1603 | pub const API_METHOD_PXAR_FILE_DOWNLOAD: ApiMethod = ApiMethod::new( | |
1604 | &ApiHandler::AsyncHttp(&pxar_file_download), | |
1605 | &ObjectSchema::new( | |
1ffe0301 | 1606 | "Download single file from pxar file of a backup snapshot. Only works if it's not encrypted.", |
d33d8f4e DC |
1607 | &sorted!([ |
1608 | ("store", false, &DATASTORE_SCHEMA), | |
33f2c2a1 | 1609 | ("backup-ns", true, &BACKUP_NAMESPACE_SCHEMA), |
d33d8f4e DC |
1610 | ("backup-type", false, &BACKUP_TYPE_SCHEMA), |
1611 | ("backup-id", false, &BACKUP_ID_SCHEMA), | |
1612 | ("backup-time", false, &BACKUP_TIME_SCHEMA), | |
1613 | ("filepath", false, &StringSchema::new("Base64 encoded path").schema()), | |
984ddb2f | 1614 | ("tar", true, &BooleanSchema::new("Download as .tar.zst").schema()), |
d33d8f4e DC |
1615 | ]), |
1616 | ) | |
7d6fc15b TL |
1617 | ).access( |
1618 | Some( | |
1619 | "Requires on /datastore/{store}[/{namespace}] either DATASTORE_READ for any or \ | |
1620 | DATASTORE_BACKUP and being the owner of the group", | |
1621 | ), | |
1622 | &Permission::Anybody, | |
d33d8f4e DC |
1623 | ); |
1624 | ||
bf78f708 | 1625 | pub fn pxar_file_download( |
d33d8f4e DC |
1626 | _parts: Parts, |
1627 | _req_body: Body, | |
1628 | param: Value, | |
1629 | _info: &ApiMethod, | |
1630 | rpcenv: Box<dyn RpcEnvironment>, | |
1631 | ) -> ApiResponseFuture { | |
d33d8f4e | 1632 | async move { |
7d6fc15b | 1633 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
3c8c2827 | 1634 | let store = required_string_param(¶m, "store")?; |
133d718f | 1635 | let backup_ns = optional_ns_param(¶m)?; |
7d6fc15b | 1636 | let backup_dir: pbs_api_types::BackupDir = Deserialize::deserialize(¶m)?; |
2bc2435a | 1637 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1638 | &store, |
133d718f | 1639 | &backup_ns, |
7d6fc15b | 1640 | &auth_id, |
2bc2435a FG |
1641 | PRIV_DATASTORE_READ, |
1642 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 1643 | )?; |
e9d2fc93 | 1644 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
a724f5fd | 1645 | |
2bc2435a | 1646 | if owner_check_required { |
a724f5fd FG |
1647 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
1648 | check_backup_owner(&owner, &auth_id)?; | |
1649 | } | |
1650 | ||
133d718f | 1651 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
d33d8f4e | 1652 | |
3c8c2827 | 1653 | let filepath = required_string_param(¶m, "filepath")?.to_owned(); |
d33d8f4e | 1654 | |
984ddb2f DC |
1655 | let tar = param["tar"].as_bool().unwrap_or(false); |
1656 | ||
d33d8f4e | 1657 | let mut components = base64::decode(&filepath)?; |
3984a5fd | 1658 | if !components.is_empty() && components[0] == b'/' { |
d33d8f4e DC |
1659 | components.remove(0); |
1660 | } | |
1661 | ||
d8d8af98 | 1662 | let mut split = components.splitn(2, |c| *c == b'/'); |
9238cdf5 | 1663 | let pxar_name = std::str::from_utf8(split.next().unwrap())?; |
0dfce17a | 1664 | let file_path = split.next().unwrap_or(b"/"); |
2d55beec | 1665 | let (manifest, files) = read_backup_index(&datastore, &backup_dir)?; |
9238cdf5 FG |
1666 | for file in files { |
1667 | if file.filename == pxar_name && file.crypt_mode == Some(CryptMode::Encrypt) { | |
1668 | bail!("cannot decode '{}' - is encrypted", pxar_name); | |
1669 | } | |
1670 | } | |
d33d8f4e | 1671 | |
9238cdf5 FG |
1672 | let mut path = datastore.base_path(); |
1673 | path.push(backup_dir.relative_path()); | |
1674 | path.push(pxar_name); | |
d33d8f4e DC |
1675 | |
1676 | let index = DynamicIndexReader::open(&path) | |
1677 | .map_err(|err| format_err!("unable to read dynamic index '{:?}' - {}", &path, err))?; | |
1678 | ||
2d55beec | 1679 | let (csum, size) = index.compute_csum(); |
9a37bd6c | 1680 | manifest.verify_file(pxar_name, &csum, size)?; |
2d55beec | 1681 | |
14f6c9cb | 1682 | let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None); |
d33d8f4e DC |
1683 | let reader = BufferedDynamicReader::new(index, chunk_reader); |
1684 | let archive_size = reader.archive_size(); | |
1685 | let reader = LocalDynamicReadAt::new(reader); | |
1686 | ||
1687 | let decoder = Accessor::new(reader, archive_size).await?; | |
1688 | let root = decoder.open_root().await?; | |
2e219481 | 1689 | let path = OsStr::from_bytes(file_path).to_os_string(); |
d33d8f4e | 1690 | let file = root |
dc7a5b34 TL |
1691 | .lookup(&path) |
1692 | .await? | |
2e219481 | 1693 | .ok_or_else(|| format_err!("error opening '{:?}'", path))?; |
d33d8f4e | 1694 | |
804f6143 DC |
1695 | let body = match file.kind() { |
1696 | EntryKind::File { .. } => Body::wrap_stream( | |
1697 | AsyncReaderStream::new(file.contents().await?).map_err(move |err| { | |
1698 | eprintln!("error during streaming of file '{:?}' - {}", filepath, err); | |
1699 | err | |
1700 | }), | |
1701 | ), | |
1702 | EntryKind::Hardlink(_) => Body::wrap_stream( | |
1703 | AsyncReaderStream::new(decoder.follow_hardlink(&file).await?.contents().await?) | |
1704 | .map_err(move |err| { | |
dc7a5b34 | 1705 | eprintln!("error during streaming of hardlink '{:?}' - {}", path, err); |
804f6143 DC |
1706 | err |
1707 | }), | |
1708 | ), | |
1709 | EntryKind::Directory => { | |
984ddb2f | 1710 | let (sender, receiver) = tokio::sync::mpsc::channel::<Result<_, Error>>(100); |
804f6143 | 1711 | let channelwriter = AsyncChannelWriter::new(sender, 1024 * 1024); |
984ddb2f | 1712 | if tar { |
dc7a5b34 TL |
1713 | proxmox_rest_server::spawn_internal_task(create_tar( |
1714 | channelwriter, | |
1715 | decoder, | |
1716 | path.clone(), | |
1717 | false, | |
1718 | )); | |
984ddb2f DC |
1719 | let zstdstream = ZstdEncoder::new(ReceiverStream::new(receiver))?; |
1720 | Body::wrap_stream(zstdstream.map_err(move |err| { | |
1721 | eprintln!("error during streaming of tar.zst '{:?}' - {}", path, err); | |
1722 | err | |
1723 | })) | |
1724 | } else { | |
dc7a5b34 TL |
1725 | proxmox_rest_server::spawn_internal_task(create_zip( |
1726 | channelwriter, | |
1727 | decoder, | |
1728 | path.clone(), | |
1729 | false, | |
1730 | )); | |
984ddb2f DC |
1731 | Body::wrap_stream(ReceiverStream::new(receiver).map_err(move |err| { |
1732 | eprintln!("error during streaming of zip '{:?}' - {}", path, err); | |
1733 | err | |
1734 | })) | |
1735 | } | |
804f6143 DC |
1736 | } |
1737 | other => bail!("cannot download file of type {:?}", other), | |
1738 | }; | |
d33d8f4e DC |
1739 | |
1740 | // fixme: set other headers ? | |
1741 | Ok(Response::builder() | |
dc7a5b34 TL |
1742 | .status(StatusCode::OK) |
1743 | .header(header::CONTENT_TYPE, "application/octet-stream") | |
1744 | .body(body) | |
1745 | .unwrap()) | |
1746 | } | |
1747 | .boxed() | |
d33d8f4e DC |
1748 | } |
1749 | ||
1a0d3d11 DM |
1750 | #[api( |
1751 | input: { | |
1752 | properties: { | |
1753 | store: { | |
1754 | schema: DATASTORE_SCHEMA, | |
1755 | }, | |
1756 | timeframe: { | |
c68fa58a | 1757 | type: RRDTimeFrame, |
1a0d3d11 DM |
1758 | }, |
1759 | cf: { | |
1760 | type: RRDMode, | |
1761 | }, | |
1762 | }, | |
1763 | }, | |
1764 | access: { | |
7d6fc15b TL |
1765 | permission: &Permission::Privilege( |
1766 | &["datastore", "{store}"], PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP, true), | |
1a0d3d11 DM |
1767 | }, |
1768 | )] | |
1769 | /// Read datastore stats | |
bf78f708 | 1770 | pub fn get_rrd_stats( |
1a0d3d11 | 1771 | store: String, |
c68fa58a | 1772 | timeframe: RRDTimeFrame, |
1a0d3d11 DM |
1773 | cf: RRDMode, |
1774 | _param: Value, | |
1775 | ) -> Result<Value, Error> { | |
e9d2fc93 | 1776 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
f27b6086 DC |
1777 | let disk_manager = crate::tools::disks::DiskManage::new(); |
1778 | ||
1779 | let mut rrd_fields = vec![ | |
dc7a5b34 TL |
1780 | "total", |
1781 | "used", | |
1782 | "read_ios", | |
1783 | "read_bytes", | |
1784 | "write_ios", | |
1785 | "write_bytes", | |
f27b6086 DC |
1786 | ]; |
1787 | ||
1788 | // we do not have io_ticks for zpools, so don't include them | |
1789 | match disk_manager.find_mounted_device(&datastore.base_path()) { | |
dc7a5b34 | 1790 | Ok(Some((fs_type, _, _))) if fs_type.as_str() == "zfs" => {} |
f27b6086 DC |
1791 | _ => rrd_fields.push("io_ticks"), |
1792 | }; | |
1793 | ||
dc7a5b34 | 1794 | create_value_from_rrd(&format!("datastore/{}", store), &rrd_fields, timeframe, cf) |
1a0d3d11 DM |
1795 | } |
1796 | ||
5fd823c3 HL |
1797 | #[api( |
1798 | input: { | |
1799 | properties: { | |
1800 | store: { | |
1801 | schema: DATASTORE_SCHEMA, | |
1802 | }, | |
1803 | }, | |
1804 | }, | |
1805 | access: { | |
1806 | permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, true), | |
1807 | }, | |
1808 | )] | |
1809 | /// Read datastore stats | |
dc7a5b34 | 1810 | pub fn get_active_operations(store: String, _param: Value) -> Result<Value, Error> { |
5fd823c3 HL |
1811 | let active_operations = task_tracking::get_active_operations(&store)?; |
1812 | Ok(json!({ | |
1813 | "read": active_operations.read, | |
1814 | "write": active_operations.write, | |
1815 | })) | |
1816 | } | |
1817 | ||
d6688884 SR |
1818 | #[api( |
1819 | input: { | |
1820 | properties: { | |
988d575d | 1821 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
1822 | "backup-ns": { |
1823 | type: BackupNamespace, | |
1824 | optional: true, | |
1825 | }, | |
8c74349b WB |
1826 | backup_group: { |
1827 | type: pbs_api_types::BackupGroup, | |
1828 | flatten: true, | |
1829 | }, | |
d6688884 SR |
1830 | }, |
1831 | }, | |
1832 | access: { | |
7d6fc15b TL |
1833 | permission: &Permission::Anybody, |
1834 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_AUDIT for any \ | |
1835 | or DATASTORE_BACKUP and being the owner of the group", | |
d6688884 SR |
1836 | }, |
1837 | )] | |
1838 | /// Get "notes" for a backup group | |
1839 | pub fn get_group_notes( | |
1840 | store: String, | |
133d718f | 1841 | backup_ns: Option<BackupNamespace>, |
8c74349b | 1842 | backup_group: pbs_api_types::BackupGroup, |
d6688884 SR |
1843 | rpcenv: &mut dyn RpcEnvironment, |
1844 | ) -> Result<String, Error> { | |
d6688884 | 1845 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 1846 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 1847 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1848 | &store, |
133d718f | 1849 | &backup_ns, |
7d6fc15b | 1850 | &auth_id, |
2bc2435a FG |
1851 | PRIV_DATASTORE_AUDIT, |
1852 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b TL |
1853 | )?; |
1854 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; | |
d6688884 | 1855 | |
2bc2435a | 1856 | if owner_check_required { |
a724f5fd FG |
1857 | let owner = datastore.get_owner(&backup_ns, &backup_group)?; |
1858 | check_backup_owner(&owner, &auth_id)?; | |
1859 | } | |
d6688884 | 1860 | |
133d718f | 1861 | let note_path = get_group_note_path(&datastore, &backup_ns, &backup_group); |
d6688884 SR |
1862 | Ok(file_read_optional_string(note_path)?.unwrap_or_else(|| "".to_owned())) |
1863 | } | |
1864 | ||
1865 | #[api( | |
1866 | input: { | |
1867 | properties: { | |
988d575d | 1868 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
1869 | "backup-ns": { |
1870 | type: BackupNamespace, | |
1871 | optional: true, | |
1872 | }, | |
8c74349b WB |
1873 | backup_group: { |
1874 | type: pbs_api_types::BackupGroup, | |
1875 | flatten: true, | |
1876 | }, | |
d6688884 SR |
1877 | notes: { |
1878 | description: "A multiline text.", | |
1879 | }, | |
1880 | }, | |
1881 | }, | |
1882 | access: { | |
7d6fc15b TL |
1883 | permission: &Permission::Anybody, |
1884 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \ | |
1885 | or DATASTORE_BACKUP and being the owner of the group", | |
d6688884 SR |
1886 | }, |
1887 | )] | |
1888 | /// Set "notes" for a backup group | |
1889 | pub fn set_group_notes( | |
1890 | store: String, | |
133d718f | 1891 | backup_ns: Option<BackupNamespace>, |
8c74349b | 1892 | backup_group: pbs_api_types::BackupGroup, |
d6688884 SR |
1893 | notes: String, |
1894 | rpcenv: &mut dyn RpcEnvironment, | |
1895 | ) -> Result<(), Error> { | |
d6688884 | 1896 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 1897 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 1898 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1899 | &store, |
133d718f | 1900 | &backup_ns, |
7d6fc15b | 1901 | &auth_id, |
2bc2435a FG |
1902 | PRIV_DATASTORE_MODIFY, |
1903 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b TL |
1904 | )?; |
1905 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; | |
2bc2435a | 1906 | if owner_check_required { |
a724f5fd FG |
1907 | let owner = datastore.get_owner(&backup_ns, &backup_group)?; |
1908 | check_backup_owner(&owner, &auth_id)?; | |
1909 | } | |
d6688884 | 1910 | |
133d718f | 1911 | let note_path = get_group_note_path(&datastore, &backup_ns, &backup_group); |
e0a19d33 | 1912 | replace_file(note_path, notes.as_bytes(), CreateOptions::new(), false)?; |
d6688884 SR |
1913 | |
1914 | Ok(()) | |
1915 | } | |
1916 | ||
912b3f5b DM |
1917 | #[api( |
1918 | input: { | |
1919 | properties: { | |
988d575d | 1920 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
1921 | "backup-ns": { |
1922 | type: BackupNamespace, | |
1923 | optional: true, | |
1924 | }, | |
8c74349b WB |
1925 | backup_dir: { |
1926 | type: pbs_api_types::BackupDir, | |
1927 | flatten: true, | |
1928 | }, | |
912b3f5b DM |
1929 | }, |
1930 | }, | |
1931 | access: { | |
7d6fc15b TL |
1932 | permission: &Permission::Anybody, |
1933 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_AUDIT for any \ | |
1934 | or DATASTORE_BACKUP and being the owner of the group", | |
912b3f5b DM |
1935 | }, |
1936 | )] | |
1937 | /// Get "notes" for a specific backup | |
bf78f708 | 1938 | pub fn get_notes( |
912b3f5b | 1939 | store: String, |
133d718f | 1940 | backup_ns: Option<BackupNamespace>, |
8c74349b | 1941 | backup_dir: pbs_api_types::BackupDir, |
912b3f5b DM |
1942 | rpcenv: &mut dyn RpcEnvironment, |
1943 | ) -> Result<String, Error> { | |
7d6fc15b | 1944 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 1945 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 1946 | let owner_check_required = check_ns_privs( |
7d6fc15b | 1947 | &store, |
133d718f | 1948 | &backup_ns, |
7d6fc15b | 1949 | &auth_id, |
2bc2435a FG |
1950 | PRIV_DATASTORE_AUDIT, |
1951 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 1952 | )?; |
e9d2fc93 | 1953 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
2bc2435a | 1954 | if owner_check_required { |
a724f5fd FG |
1955 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
1956 | check_backup_owner(&owner, &auth_id)?; | |
1957 | } | |
912b3f5b | 1958 | |
133d718f | 1959 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
912b3f5b | 1960 | |
133d718f | 1961 | let (manifest, _) = backup_dir.load_manifest()?; |
912b3f5b | 1962 | |
dc7a5b34 | 1963 | let notes = manifest.unprotected["notes"].as_str().unwrap_or(""); |
912b3f5b DM |
1964 | |
1965 | Ok(String::from(notes)) | |
1966 | } | |
1967 | ||
1968 | #[api( | |
1969 | input: { | |
1970 | properties: { | |
988d575d | 1971 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
1972 | "backup-ns": { |
1973 | type: BackupNamespace, | |
1974 | optional: true, | |
1975 | }, | |
8c74349b WB |
1976 | backup_dir: { |
1977 | type: pbs_api_types::BackupDir, | |
1978 | flatten: true, | |
1979 | }, | |
912b3f5b DM |
1980 | notes: { |
1981 | description: "A multiline text.", | |
1982 | }, | |
1983 | }, | |
1984 | }, | |
1985 | access: { | |
7d6fc15b TL |
1986 | permission: &Permission::Anybody, |
1987 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \ | |
1988 | or DATASTORE_BACKUP and being the owner of the group", | |
912b3f5b DM |
1989 | }, |
1990 | )] | |
1991 | /// Set "notes" for a specific backup | |
bf78f708 | 1992 | pub fn set_notes( |
912b3f5b | 1993 | store: String, |
133d718f | 1994 | backup_ns: Option<BackupNamespace>, |
8c74349b | 1995 | backup_dir: pbs_api_types::BackupDir, |
912b3f5b DM |
1996 | notes: String, |
1997 | rpcenv: &mut dyn RpcEnvironment, | |
1998 | ) -> Result<(), Error> { | |
7d6fc15b | 1999 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 2000 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 2001 | let owner_check_required = check_ns_privs( |
7d6fc15b | 2002 | &store, |
133d718f | 2003 | &backup_ns, |
7d6fc15b | 2004 | &auth_id, |
2bc2435a FG |
2005 | PRIV_DATASTORE_MODIFY, |
2006 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 2007 | )?; |
e9d2fc93 | 2008 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
2bc2435a | 2009 | if owner_check_required { |
a724f5fd FG |
2010 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
2011 | check_backup_owner(&owner, &auth_id)?; | |
2012 | } | |
912b3f5b | 2013 | |
133d718f | 2014 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
912b3f5b | 2015 | |
133d718f WB |
2016 | backup_dir |
2017 | .update_manifest(|manifest| { | |
dc7a5b34 TL |
2018 | manifest.unprotected["notes"] = notes.into(); |
2019 | }) | |
2020 | .map_err(|err| format_err!("unable to update manifest blob - {}", err))?; | |
912b3f5b DM |
2021 | |
2022 | Ok(()) | |
2023 | } | |
2024 | ||
8292d3d2 DC |
2025 | #[api( |
2026 | input: { | |
2027 | properties: { | |
988d575d | 2028 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
2029 | "backup-ns": { |
2030 | type: BackupNamespace, | |
2031 | optional: true, | |
2032 | }, | |
8c74349b WB |
2033 | backup_dir: { |
2034 | type: pbs_api_types::BackupDir, | |
2035 | flatten: true, | |
2036 | }, | |
8292d3d2 DC |
2037 | }, |
2038 | }, | |
2039 | access: { | |
7d6fc15b TL |
2040 | permission: &Permission::Anybody, |
2041 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_AUDIT for any \ | |
2042 | or DATASTORE_BACKUP and being the owner of the group", | |
8292d3d2 DC |
2043 | }, |
2044 | )] | |
2045 | /// Query protection for a specific backup | |
2046 | pub fn get_protection( | |
2047 | store: String, | |
133d718f | 2048 | backup_ns: Option<BackupNamespace>, |
8c74349b | 2049 | backup_dir: pbs_api_types::BackupDir, |
8292d3d2 DC |
2050 | rpcenv: &mut dyn RpcEnvironment, |
2051 | ) -> Result<bool, Error> { | |
7d6fc15b | 2052 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 2053 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 2054 | let owner_check_required = check_ns_privs( |
7d6fc15b | 2055 | &store, |
133d718f | 2056 | &backup_ns, |
7d6fc15b | 2057 | &auth_id, |
2bc2435a FG |
2058 | PRIV_DATASTORE_AUDIT, |
2059 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 2060 | )?; |
e9d2fc93 | 2061 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Read))?; |
2bc2435a FG |
2062 | |
2063 | if owner_check_required { | |
a724f5fd FG |
2064 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
2065 | check_backup_owner(&owner, &auth_id)?; | |
2066 | } | |
8292d3d2 | 2067 | |
133d718f | 2068 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
8292d3d2 | 2069 | |
6da20161 | 2070 | Ok(backup_dir.is_protected()) |
8292d3d2 DC |
2071 | } |
2072 | ||
2073 | #[api( | |
2074 | input: { | |
2075 | properties: { | |
988d575d | 2076 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
2077 | "backup-ns": { |
2078 | type: BackupNamespace, | |
2079 | optional: true, | |
2080 | }, | |
8c74349b WB |
2081 | backup_dir: { |
2082 | type: pbs_api_types::BackupDir, | |
2083 | flatten: true, | |
2084 | }, | |
8292d3d2 DC |
2085 | protected: { |
2086 | description: "Enable/disable protection.", | |
2087 | }, | |
2088 | }, | |
2089 | }, | |
2090 | access: { | |
7d6fc15b TL |
2091 | permission: &Permission::Anybody, |
2092 | description: "Requires on /datastore/{store}[/{namespace}] either DATASTORE_MODIFY for any \ | |
2093 | or DATASTORE_BACKUP and being the owner of the group", | |
8292d3d2 DC |
2094 | }, |
2095 | )] | |
2096 | /// En- or disable protection for a specific backup | |
2097 | pub fn set_protection( | |
2098 | store: String, | |
133d718f | 2099 | backup_ns: Option<BackupNamespace>, |
8c74349b | 2100 | backup_dir: pbs_api_types::BackupDir, |
8292d3d2 DC |
2101 | protected: bool, |
2102 | rpcenv: &mut dyn RpcEnvironment, | |
2103 | ) -> Result<(), Error> { | |
7d6fc15b | 2104 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 2105 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 2106 | let owner_check_required = check_ns_privs( |
7d6fc15b | 2107 | &store, |
133d718f | 2108 | &backup_ns, |
7d6fc15b | 2109 | &auth_id, |
2bc2435a FG |
2110 | PRIV_DATASTORE_MODIFY, |
2111 | PRIV_DATASTORE_BACKUP, | |
7d6fc15b | 2112 | )?; |
e9d2fc93 | 2113 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
2bc2435a | 2114 | if owner_check_required { |
a724f5fd FG |
2115 | let owner = datastore.get_owner(&backup_ns, &backup_dir.group)?; |
2116 | check_backup_owner(&owner, &auth_id)?; | |
2117 | } | |
8292d3d2 | 2118 | |
133d718f | 2119 | let backup_dir = datastore.backup_dir(backup_ns, backup_dir)?; |
8292d3d2 | 2120 | |
8292d3d2 DC |
2121 | datastore.update_protection(&backup_dir, protected) |
2122 | } | |
2123 | ||
72be0eb1 | 2124 | #[api( |
4940012d | 2125 | input: { |
72be0eb1 | 2126 | properties: { |
988d575d | 2127 | store: { schema: DATASTORE_SCHEMA }, |
133d718f WB |
2128 | "backup-ns": { |
2129 | type: BackupNamespace, | |
2130 | optional: true, | |
2131 | }, | |
8c74349b WB |
2132 | backup_group: { |
2133 | type: pbs_api_types::BackupGroup, | |
2134 | flatten: true, | |
2135 | }, | |
72be0eb1 | 2136 | "new-owner": { |
e6dc35ac | 2137 | type: Authid, |
72be0eb1 DW |
2138 | }, |
2139 | }, | |
4940012d FG |
2140 | }, |
2141 | access: { | |
bff85572 | 2142 | permission: &Permission::Anybody, |
7d6fc15b TL |
2143 | description: "Datastore.Modify on whole datastore, or changing ownership between user and \ |
2144 | a user's token for owned backups with Datastore.Backup" | |
4940012d | 2145 | }, |
72be0eb1 DW |
2146 | )] |
2147 | /// Change owner of a backup group | |
bf78f708 | 2148 | pub fn set_backup_owner( |
72be0eb1 | 2149 | store: String, |
133d718f | 2150 | backup_ns: Option<BackupNamespace>, |
8c74349b | 2151 | backup_group: pbs_api_types::BackupGroup, |
e6dc35ac | 2152 | new_owner: Authid, |
bff85572 | 2153 | rpcenv: &mut dyn RpcEnvironment, |
72be0eb1 | 2154 | ) -> Result<(), Error> { |
e9d2fc93 | 2155 | let datastore = DataStore::lookup_datastore(&store, Some(Operation::Write))?; |
72be0eb1 | 2156 | |
bff85572 | 2157 | let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; |
133d718f | 2158 | let backup_ns = backup_ns.unwrap_or_default(); |
2bc2435a | 2159 | let owner_check_required = check_ns_privs( |
a724f5fd FG |
2160 | &store, |
2161 | &backup_ns, | |
2162 | &auth_id, | |
2bc2435a FG |
2163 | PRIV_DATASTORE_MODIFY, |
2164 | PRIV_DATASTORE_BACKUP, | |
a724f5fd | 2165 | )?; |
133d718f | 2166 | let backup_group = datastore.backup_group(backup_ns, backup_group); |
bff85572 | 2167 | |
2bc2435a | 2168 | if owner_check_required { |
133d718f | 2169 | let owner = backup_group.get_owner()?; |
bff85572 | 2170 | |
2bc2435a | 2171 | let allowed = match (owner.is_token(), new_owner.is_token()) { |
bff85572 FG |
2172 | (true, true) => { |
2173 | // API token to API token, owned by same user | |
2174 | let owner = owner.user(); | |
2175 | let new_owner = new_owner.user(); | |
2176 | owner == new_owner && Authid::from(owner.clone()) == auth_id | |
dc7a5b34 | 2177 | } |
bff85572 FG |
2178 | (true, false) => { |
2179 | // API token to API token owner | |
dc7a5b34 TL |
2180 | Authid::from(owner.user().clone()) == auth_id && new_owner == auth_id |
2181 | } | |
bff85572 FG |
2182 | (false, true) => { |
2183 | // API token owner to API token | |
dc7a5b34 TL |
2184 | owner == auth_id && Authid::from(new_owner.user().clone()) == auth_id |
2185 | } | |
bff85572 FG |
2186 | (false, false) => { |
2187 | // User to User, not allowed for unprivileged users | |
2188 | false | |
dc7a5b34 | 2189 | } |
2bc2435a | 2190 | }; |
bff85572 | 2191 | |
2bc2435a FG |
2192 | if !allowed { |
2193 | return Err(http_err!( | |
2194 | UNAUTHORIZED, | |
2195 | "{} does not have permission to change owner of backup group '{}' to {}", | |
2196 | auth_id, | |
2197 | backup_group, | |
2198 | new_owner, | |
2199 | )); | |
2200 | } | |
bff85572 FG |
2201 | } |
2202 | ||
7d6fc15b TL |
2203 | let user_info = CachedUserInfo::new()?; |
2204 | ||
e6dc35ac | 2205 | if !user_info.is_active_auth_id(&new_owner) { |
dc7a5b34 TL |
2206 | bail!( |
2207 | "{} '{}' is inactive or non-existent", | |
2208 | if new_owner.is_token() { | |
2209 | "API token".to_string() | |
2210 | } else { | |
2211 | "user".to_string() | |
2212 | }, | |
2213 | new_owner | |
2214 | ); | |
72be0eb1 DW |
2215 | } |
2216 | ||
133d718f | 2217 | backup_group.set_owner(&new_owner, true)?; |
72be0eb1 DW |
2218 | |
2219 | Ok(()) | |
2220 | } | |
2221 | ||
552c2259 | 2222 | #[sortable] |
255f378a | 2223 | const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ |
5fd823c3 HL |
2224 | ( |
2225 | "active-operations", | |
dc7a5b34 | 2226 | &Router::new().get(&API_METHOD_GET_ACTIVE_OPERATIONS), |
5b1cfa01 | 2227 | ), |
dc7a5b34 | 2228 | ("catalog", &Router::new().get(&API_METHOD_CATALOG)), |
72be0eb1 DW |
2229 | ( |
2230 | "change-owner", | |
dc7a5b34 | 2231 | &Router::new().post(&API_METHOD_SET_BACKUP_OWNER), |
72be0eb1 | 2232 | ), |
255f378a DM |
2233 | ( |
2234 | "download", | |
dc7a5b34 | 2235 | &Router::new().download(&API_METHOD_DOWNLOAD_FILE), |
255f378a | 2236 | ), |
6ef9bb59 DC |
2237 | ( |
2238 | "download-decoded", | |
dc7a5b34 | 2239 | &Router::new().download(&API_METHOD_DOWNLOAD_FILE_DECODED), |
255f378a | 2240 | ), |
dc7a5b34 | 2241 | ("files", &Router::new().get(&API_METHOD_LIST_SNAPSHOT_FILES)), |
255f378a DM |
2242 | ( |
2243 | "gc", | |
2244 | &Router::new() | |
2245 | .get(&API_METHOD_GARBAGE_COLLECTION_STATUS) | |
dc7a5b34 | 2246 | .post(&API_METHOD_START_GARBAGE_COLLECTION), |
255f378a | 2247 | ), |
d6688884 SR |
2248 | ( |
2249 | "group-notes", | |
2250 | &Router::new() | |
2251 | .get(&API_METHOD_GET_GROUP_NOTES) | |
dc7a5b34 | 2252 | .put(&API_METHOD_SET_GROUP_NOTES), |
d6688884 | 2253 | ), |
255f378a DM |
2254 | ( |
2255 | "groups", | |
2256 | &Router::new() | |
b31c8019 | 2257 | .get(&API_METHOD_LIST_GROUPS) |
dc7a5b34 | 2258 | .delete(&API_METHOD_DELETE_GROUP), |
255f378a | 2259 | ), |
18934ae5 TL |
2260 | ( |
2261 | "namespace", | |
2262 | // FIXME: move into datastore:: sub-module?! | |
2263 | &crate::api2::admin::namespace::ROUTER, | |
2264 | ), | |
912b3f5b DM |
2265 | ( |
2266 | "notes", | |
2267 | &Router::new() | |
2268 | .get(&API_METHOD_GET_NOTES) | |
dc7a5b34 | 2269 | .put(&API_METHOD_SET_NOTES), |
912b3f5b | 2270 | ), |
8292d3d2 DC |
2271 | ( |
2272 | "protected", | |
2273 | &Router::new() | |
2274 | .get(&API_METHOD_GET_PROTECTION) | |
dc7a5b34 | 2275 | .put(&API_METHOD_SET_PROTECTION), |
255f378a | 2276 | ), |
dc7a5b34 | 2277 | ("prune", &Router::new().post(&API_METHOD_PRUNE)), |
9805207a DC |
2278 | ( |
2279 | "prune-datastore", | |
dc7a5b34 | 2280 | &Router::new().post(&API_METHOD_PRUNE_DATASTORE), |
9805207a | 2281 | ), |
d33d8f4e DC |
2282 | ( |
2283 | "pxar-file-download", | |
dc7a5b34 | 2284 | &Router::new().download(&API_METHOD_PXAR_FILE_DOWNLOAD), |
1a0d3d11 | 2285 | ), |
dc7a5b34 | 2286 | ("rrd", &Router::new().get(&API_METHOD_GET_RRD_STATS)), |
255f378a DM |
2287 | ( |
2288 | "snapshots", | |
2289 | &Router::new() | |
fc189b19 | 2290 | .get(&API_METHOD_LIST_SNAPSHOTS) |
dc7a5b34 | 2291 | .delete(&API_METHOD_DELETE_SNAPSHOT), |
255f378a | 2292 | ), |
dc7a5b34 | 2293 | ("status", &Router::new().get(&API_METHOD_STATUS)), |
255f378a DM |
2294 | ( |
2295 | "upload-backup-log", | |
dc7a5b34 | 2296 | &Router::new().upload(&API_METHOD_UPLOAD_BACKUP_LOG), |
c2009e53 | 2297 | ), |
dc7a5b34 | 2298 | ("verify", &Router::new().post(&API_METHOD_VERIFY)), |
255f378a DM |
2299 | ]; |
2300 | ||
ad51d02a | 2301 | const DATASTORE_INFO_ROUTER: Router = Router::new() |
255f378a DM |
2302 | .get(&list_subdirs_api_method!(DATASTORE_INFO_SUBDIRS)) |
2303 | .subdirs(DATASTORE_INFO_SUBDIRS); | |
2304 | ||
255f378a | 2305 | pub const ROUTER: Router = Router::new() |
bb34b589 | 2306 | .get(&API_METHOD_GET_DATASTORE_LIST) |
255f378a | 2307 | .match_all("store", &DATASTORE_INFO_ROUTER); |