]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/types/mod.rs
backup/datastore: count still bad chunks for the status
[proxmox-backup.git] / src / api2 / types / mod.rs
CommitLineData
e7cb4dc5
WB
1use anyhow::bail;
2use serde::{Deserialize, Serialize};
4ebf0eab 3
9ea4bce4
WB
4use proxmox::api::{api, schema::*};
5use proxmox::const_regex;
60b9b48e 6use proxmox::{IPRE, IPRE_BRACKET, IPV4RE, IPV6RE, IPV4OCTET, IPV6H16, IPV6LS32};
255f378a 7
f28d9088 8use crate::backup::CryptMode;
3b2046d2 9use crate::server::UPID;
f28d9088 10
e7cb4dc5
WB
11#[macro_use]
12mod macros;
13
14#[macro_use]
15mod userid;
16pub use userid::{Realm, RealmRef};
17pub use userid::{Username, UsernameRef};
18pub use userid::Userid;
19pub use userid::PROXMOX_GROUP_ID_SCHEMA;
20
255f378a
DM
21// File names: may not contain slashes, may not start with "."
22pub const FILENAME_FORMAT: ApiStringFormat = ApiStringFormat::VerifyFn(|name| {
23 if name.starts_with('.') {
24 bail!("file names may not start with '.'");
25 }
26 if name.contains('/') {
27 bail!("file names may not contain slashes");
28 }
29 Ok(())
30});
31
b25f313d 32macro_rules! DNS_LABEL { () => (r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)") }
4c95d58c 33macro_rules! DNS_NAME { () => (concat!(r"(?:(?:", DNS_LABEL!() , r"\.)*", DNS_LABEL!(), ")")) }
255f378a 34
76cf5208
DM
35macro_rules! CIDR_V4_REGEX_STR { () => (concat!(r"(?:", IPV4RE!(), r"/\d{1,2})$")) }
36macro_rules! CIDR_V6_REGEX_STR { () => (concat!(r"(?:", IPV6RE!(), r"/\d{1,3})$")) }
37
255f378a 38const_regex!{
76cf5208
DM
39 pub IP_V4_REGEX = concat!(r"^", IPV4RE!(), r"$");
40 pub IP_V6_REGEX = concat!(r"^", IPV6RE!(), r"$");
41 pub IP_REGEX = concat!(r"^", IPRE!(), r"$");
42 pub CIDR_V4_REGEX = concat!(r"^", CIDR_V4_REGEX_STR!(), r"$");
43 pub CIDR_V6_REGEX = concat!(r"^", CIDR_V6_REGEX_STR!(), r"$");
44 pub CIDR_REGEX = concat!(r"^(?:", CIDR_V4_REGEX_STR!(), "|", CIDR_V6_REGEX_STR!(), r")$");
45
255f378a
DM
46 pub SHA256_HEX_REGEX = r"^[a-f0-9]{64}$"; // fixme: define in common_regex ?
47 pub SYSTEMD_DATETIME_REGEX = r"^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$"; // fixme: define in common_regex ?
d0adf270 48
da4a15a3
DM
49 pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
50
d0adf270
DM
51 /// Regex for safe identifiers.
52 ///
53 /// This
54 /// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
55 /// contains further information why it is reasonable to restict
56 /// names this way. This is not only useful for filenames, but for
57 /// any identifier command line tools work with.
163dc16c 58 pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
454c13ed
DM
59
60 pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
b25f313d
DM
61
62 pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$";
63
ae62c4fe 64 pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$");
b25f313d 65
4c95d58c 66 pub DNS_NAME_OR_IP_REGEX = concat!(r"^(?:", DNS_NAME!(), "|", IPRE!(), r")$");
163dc16c 67
ba20987a 68 pub BACKUP_REPO_URL_REGEX = concat!(r"^^(?:(?:(", USER_ID_REGEX_STR!(), ")@)?(", DNS_NAME!(), "|", IPRE_BRACKET!() ,"):)?(?:([0-9]{1,5}):)?(", PROXMOX_SAFE_ID_REGEX_STR!(), r")$");
090decbe 69
dcb8db66 70 pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
ed3e60ae 71
9765092e 72 pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$");
9069debc
DM
73
74 pub BLOCKDEVICE_NAME_REGEX = r"^(:?(:?h|s|x?v)d[a-z]+)|(:?nvme\d+n\d+)$";
7957fabf
DC
75
76 pub ZPOOL_NAME_REGEX = r"^[a-zA-Z][a-z0-9A-Z\-_.:]+$";
255f378a 77}
4ebf0eab 78
255f378a
DM
79pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat =
80 ApiStringFormat::Pattern(&SYSTEMD_DATETIME_REGEX);
4ebf0eab 81
76cf5208
DM
82pub const IP_V4_FORMAT: ApiStringFormat =
83 ApiStringFormat::Pattern(&IP_V4_REGEX);
84
85pub const IP_V6_FORMAT: ApiStringFormat =
86 ApiStringFormat::Pattern(&IP_V6_REGEX);
87
255f378a 88pub const IP_FORMAT: ApiStringFormat =
76cf5208 89 ApiStringFormat::Pattern(&IP_REGEX);
bbf9e7e9 90
255f378a
DM
91pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat =
92 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
93
dcb8db66
DM
94pub const CERT_FINGERPRINT_SHA256_FORMAT: ApiStringFormat =
95 ApiStringFormat::Pattern(&CERT_FINGERPRINT_SHA256_REGEX);
96
d0adf270
DM
97pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
98 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
99
454c13ed
DM
100pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
101 ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
102
b25f313d
DM
103pub const HOSTNAME_FORMAT: ApiStringFormat =
104 ApiStringFormat::Pattern(&HOSTNAME_REGEX);
105
106pub const DNS_NAME_FORMAT: ApiStringFormat =
107 ApiStringFormat::Pattern(&DNS_NAME_REGEX);
108
109pub const DNS_NAME_OR_IP_FORMAT: ApiStringFormat =
110 ApiStringFormat::Pattern(&DNS_NAME_OR_IP_REGEX);
111
7e7b781a
DM
112pub const PASSWORD_FORMAT: ApiStringFormat =
113 ApiStringFormat::Pattern(&PASSWORD_REGEX);
114
ed3e60ae
DM
115pub const ACL_PATH_FORMAT: ApiStringFormat =
116 ApiStringFormat::Pattern(&ACL_PATH_REGEX);
117
68da20bf
DM
118pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat =
119 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
454c13ed 120
76cf5208
DM
121pub const CIDR_V4_FORMAT: ApiStringFormat =
122 ApiStringFormat::Pattern(&CIDR_V4_REGEX);
123
124pub const CIDR_V6_FORMAT: ApiStringFormat =
125 ApiStringFormat::Pattern(&CIDR_V6_REGEX);
126
127pub const CIDR_FORMAT: ApiStringFormat =
128 ApiStringFormat::Pattern(&CIDR_REGEX);
129
9069debc
DM
130pub const BLOCKDEVICE_NAME_FORMAT: ApiStringFormat =
131 ApiStringFormat::Pattern(&BLOCKDEVICE_NAME_REGEX);
76cf5208 132
685e1334
DM
133pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
134 .format(&PASSWORD_FORMAT)
135 .min_length(1)
b88f9c5b 136 .max_length(1024)
685e1334
DM
137 .schema();
138
139pub const PBS_PASSWORD_SCHEMA: Schema = StringSchema::new("User Password.")
140 .format(&PASSWORD_FORMAT)
141 .min_length(5)
142 .max_length(64)
143 .schema();
dcb8db66
DM
144
145pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema = StringSchema::new(
146 "X509 certificate fingerprint (sha256)."
147)
148 .format(&CERT_FINGERPRINT_SHA256_FORMAT)
149 .schema();
150
002a191a 151pub const PROXMOX_CONFIG_DIGEST_SCHEMA: Schema = StringSchema::new(r#"\
255f378a
DM
152Prevent changes if current configuration file has different SHA256 digest.
153This can be used to prevent concurrent modifications.
154"#
155)
156 .format(&PVE_CONFIG_DIGEST_FORMAT)
157 .schema();
158
159
160pub const CHUNK_DIGEST_FORMAT: ApiStringFormat =
161 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
162
163pub const CHUNK_DIGEST_SCHEMA: Schema = StringSchema::new("Chunk digest (SHA256).")
164 .format(&CHUNK_DIGEST_FORMAT)
165 .schema();
166
167pub const NODE_SCHEMA: Schema = StringSchema::new("Node name (or 'localhost')")
168 .format(&ApiStringFormat::VerifyFn(|node| {
169 if node == "localhost" || node == proxmox::tools::nodename() {
170 Ok(())
171 } else {
172 bail!("no such node '{}'", node);
173 }
174 }))
175 .schema();
176
177pub const SEARCH_DOMAIN_SCHEMA: Schema =
178 StringSchema::new("Search domain for host-name lookup.").schema();
179
180pub const FIRST_DNS_SERVER_SCHEMA: Schema =
181 StringSchema::new("First name server IP address.")
182 .format(&IP_FORMAT)
183 .schema();
184
185pub const SECOND_DNS_SERVER_SCHEMA: Schema =
186 StringSchema::new("Second name server IP address.")
187 .format(&IP_FORMAT)
188 .schema();
189
190pub const THIRD_DNS_SERVER_SCHEMA: Schema =
191 StringSchema::new("Third name server IP address.")
192 .format(&IP_FORMAT)
193 .schema();
194
76cf5208
DM
195pub const IP_V4_SCHEMA: Schema =
196 StringSchema::new("IPv4 address.")
197 .format(&IP_V4_FORMAT)
198 .max_length(15)
199 .schema();
200
201pub const IP_V6_SCHEMA: Schema =
202 StringSchema::new("IPv6 address.")
203 .format(&IP_V6_FORMAT)
204 .max_length(39)
205 .schema();
206
207pub const IP_SCHEMA: Schema =
208 StringSchema::new("IP (IPv4 or IPv6) address.")
209 .format(&IP_FORMAT)
210 .max_length(39)
211 .schema();
212
213pub const CIDR_V4_SCHEMA: Schema =
214 StringSchema::new("IPv4 address with netmask (CIDR notation).")
215 .format(&CIDR_V4_FORMAT)
216 .max_length(18)
217 .schema();
218
219pub const CIDR_V6_SCHEMA: Schema =
220 StringSchema::new("IPv6 address with netmask (CIDR notation).")
221 .format(&CIDR_V6_FORMAT)
222 .max_length(43)
223 .schema();
224
225pub const CIDR_SCHEMA: Schema =
226 StringSchema::new("IP address (IPv4 or IPv6) with netmask (CIDR notation).")
227 .format(&CIDR_FORMAT)
228 .max_length(43)
229 .schema();
230
4b40148c
DM
231pub const TIME_ZONE_SCHEMA: Schema = StringSchema::new(
232 "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")
233 .format(&SINGLE_LINE_COMMENT_FORMAT)
234 .min_length(2)
235 .max_length(64)
236 .schema();
237
ca257c80
DM
238pub const ACL_PATH_SCHEMA: Schema = StringSchema::new(
239 "Access control path.")
240 .format(&ACL_PATH_FORMAT)
241 .min_length(1)
242 .max_length(128)
243 .schema();
244
245pub const ACL_PROPAGATE_SCHEMA: Schema = BooleanSchema::new(
246 "Allow to propagate (inherit) permissions.")
247 .default(true)
248 .schema();
249
250pub const ACL_UGID_TYPE_SCHEMA: Schema = StringSchema::new(
251 "Type of 'ugid' property.")
ca257c80 252 .format(&ApiStringFormat::Enum(&[
bc0d0388
DM
253 EnumEntry::new("user", "User"),
254 EnumEntry::new("group", "Group")]))
ca257c80
DM
255 .schema();
256
255f378a
DM
257pub const BACKUP_ARCHIVE_NAME_SCHEMA: Schema =
258 StringSchema::new("Backup archive name.")
1ae5677d 259 .format(&PROXMOX_SAFE_ID_FORMAT)
255f378a
DM
260 .schema();
261
262pub const BACKUP_TYPE_SCHEMA: Schema =
263 StringSchema::new("Backup type.")
bc0d0388
DM
264 .format(&ApiStringFormat::Enum(&[
265 EnumEntry::new("vm", "Virtual Machine Backup"),
266 EnumEntry::new("ct", "Container Backup"),
267 EnumEntry::new("host", "Host Backup")]))
255f378a
DM
268 .schema();
269
270pub const BACKUP_ID_SCHEMA: Schema =
271 StringSchema::new("Backup ID.")
1ae5677d 272 .format(&PROXMOX_SAFE_ID_FORMAT)
255f378a
DM
273 .schema();
274
275pub const BACKUP_TIME_SCHEMA: Schema =
276 IntegerSchema::new("Backup time (Unix epoch.)")
277 .minimum(1_547_797_308)
278 .schema();
5830c205
DM
279
280pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
281 .max_length(256)
282 .schema();
66c49c21
DM
283
284pub const DATASTORE_SCHEMA: Schema = StringSchema::new("Datastore name.")
d0adf270 285 .format(&PROXMOX_SAFE_ID_FORMAT)
688fbe07 286 .min_length(3)
66c49c21
DM
287 .max_length(32)
288 .schema();
fc189b19 289
2888b27f
DC
290pub const SYNC_SCHEDULE_SCHEMA: Schema = StringSchema::new(
291 "Run sync job at specified schedule.")
292 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
293 .schema();
294
42fdbe51
DM
295pub const GC_SCHEDULE_SCHEMA: Schema = StringSchema::new(
296 "Run garbage collection job at specified schedule.")
297 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
298 .schema();
299
67f7ffd0
DM
300pub const PRUNE_SCHEDULE_SCHEMA: Schema = StringSchema::new(
301 "Run prune job at specified schedule.")
302 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
303 .schema();
304
78efafc2 305pub const VERIFICATION_SCHEDULE_SCHEMA: Schema = StringSchema::new(
f37ef25b
HL
306 "Run verify job at specified schedule.")
307 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
308 .schema();
309
167971ed
DM
310pub const REMOTE_ID_SCHEMA: Schema = StringSchema::new("Remote ID.")
311 .format(&PROXMOX_SAFE_ID_FORMAT)
312 .min_length(3)
313 .max_length(32)
314 .schema();
315
b4900286
DM
316pub const JOB_ID_SCHEMA: Schema = StringSchema::new("Job ID.")
317 .format(&PROXMOX_SAFE_ID_FORMAT)
318 .min_length(3)
319 .max_length(32)
320 .schema();
321
322pub const REMOVE_VANISHED_BACKUPS_SCHEMA: Schema = BooleanSchema::new(
323 "Delete vanished backups. This remove the local copy if the remote backup was deleted.")
324 .default(true)
325 .schema();
326
9b2bad7a
HL
327pub const IGNORE_VERIFIED_BACKUPS_SCHEMA: Schema = BooleanSchema::new(
328 "Do not verify backups that are already verified if their verification is not outdated.")
329 .default(true)
330 .schema();
331
332pub const VERIFICATION_OUTDATED_AFTER_SCHEMA: Schema = IntegerSchema::new(
333 "Days after that a verification becomes outdated")
334 .minimum(1)
335 .schema();
336
454c13ed
DM
337pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
338 .format(&SINGLE_LINE_COMMENT_FORMAT)
339 .schema();
fc189b19 340
b25f313d
DM
341pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).")
342 .format(&HOSTNAME_FORMAT)
343 .schema();
344
345pub const DNS_NAME_OR_IP_SCHEMA: Schema = StringSchema::new("DNS name or IP address.")
346 .format(&DNS_NAME_OR_IP_FORMAT)
347 .schema();
348
9069debc
DM
349pub const BLOCKDEVICE_NAME_SCHEMA: Schema = StringSchema::new("Block device name (/sys/block/<name>).")
350 .format(&BLOCKDEVICE_NAME_FORMAT)
351 .min_length(3)
352 .max_length(64)
353 .schema();
fc189b19
DM
354
355// Complex type definitions
356
b31c8019
DM
357#[api(
358 properties: {
359 "backup-type": {
360 schema: BACKUP_TYPE_SCHEMA,
361 },
362 "backup-id": {
363 schema: BACKUP_ID_SCHEMA,
364 },
365 "last-backup": {
366 schema: BACKUP_TIME_SCHEMA,
367 },
368 "backup-count": {
369 type: Integer,
370 },
371 files: {
372 items: {
373 schema: BACKUP_ARCHIVE_NAME_SCHEMA
374 },
375 },
e7cb4dc5
WB
376 owner: {
377 type: Userid,
378 optional: true,
379 },
b31c8019
DM
380 },
381)]
382#[derive(Serialize, Deserialize)]
383#[serde(rename_all="kebab-case")]
384/// Basic information about a backup group.
385pub struct GroupListItem {
386 pub backup_type: String, // enum
387 pub backup_id: String,
388 pub last_backup: i64,
389 /// Number of contained snapshots
390 pub backup_count: u64,
391 /// List of contained archive files.
392 pub files: Vec<String>,
04b0ca8b
DC
393 /// The owner of group
394 #[serde(skip_serializing_if="Option::is_none")]
e7cb4dc5 395 pub owner: Option<Userid>,
b31c8019
DM
396}
397
d10332a1
SR
398#[api()]
399#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
400#[serde(rename_all = "lowercase")]
401/// Result of a verify operation.
402pub enum VerifyState {
403 /// Verification was successful
404 Ok,
405 /// Verification reported one or more errors
406 Failed,
407}
408
3b2046d2
TL
409#[api(
410 properties: {
411 upid: {
412 schema: UPID_SCHEMA
413 },
414 state: {
d10332a1 415 type: VerifyState
3b2046d2
TL
416 },
417 },
418)]
419#[derive(Serialize, Deserialize)]
420/// Task properties.
421pub struct SnapshotVerifyState {
422 /// UPID of the verify task
423 pub upid: UPID,
d10332a1
SR
424 /// State of the verification. Enum.
425 pub state: VerifyState,
3b2046d2
TL
426}
427
fc189b19 428#[api(
fc189b19
DM
429 properties: {
430 "backup-type": {
431 schema: BACKUP_TYPE_SCHEMA,
432 },
433 "backup-id": {
434 schema: BACKUP_ID_SCHEMA,
435 },
436 "backup-time": {
437 schema: BACKUP_TIME_SCHEMA,
438 },
5255e641
TL
439 comment: {
440 schema: SINGLE_LINE_COMMENT_SCHEMA,
441 optional: true,
442 },
3b2046d2
TL
443 verification: {
444 type: SnapshotVerifyState,
445 optional: true,
446 },
71da3d6a
DM
447 files: {
448 items: {
449 schema: BACKUP_ARCHIVE_NAME_SCHEMA
450 },
451 },
e7cb4dc5
WB
452 owner: {
453 type: Userid,
454 optional: true,
455 },
fc189b19
DM
456 },
457)]
458#[derive(Serialize, Deserialize)]
459#[serde(rename_all="kebab-case")]
71da3d6a 460/// Basic information about backup snapshot.
fc189b19
DM
461pub struct SnapshotListItem {
462 pub backup_type: String, // enum
463 pub backup_id: String,
464 pub backup_time: i64,
70030b43
DM
465 /// The first line from manifest "notes"
466 #[serde(skip_serializing_if="Option::is_none")]
467 pub comment: Option<String>,
3b2046d2
TL
468 /// The result of the last run verify task
469 #[serde(skip_serializing_if="Option::is_none")]
470 pub verification: Option<SnapshotVerifyState>,
71da3d6a 471 /// List of contained archive files.
1c090810 472 pub files: Vec<BackupContent>,
71da3d6a 473 /// Overall snapshot size (sum of all archive sizes).
fc189b19
DM
474 #[serde(skip_serializing_if="Option::is_none")]
475 pub size: Option<u64>,
04b0ca8b
DC
476 /// The owner of the snapshots group
477 #[serde(skip_serializing_if="Option::is_none")]
e7cb4dc5 478 pub owner: Option<Userid>,
fc189b19 479}
ff620a3d 480
db1e061d
DM
481#[api(
482 properties: {
483 "backup-type": {
484 schema: BACKUP_TYPE_SCHEMA,
485 },
486 "backup-id": {
487 schema: BACKUP_ID_SCHEMA,
488 },
489 "backup-time": {
490 schema: BACKUP_TIME_SCHEMA,
491 },
492 },
493)]
494#[derive(Serialize, Deserialize)]
495#[serde(rename_all="kebab-case")]
496/// Prune result.
497pub struct PruneListItem {
498 pub backup_type: String, // enum
499 pub backup_id: String,
500 pub backup_time: i64,
501 /// Keep snapshot
502 pub keep: bool,
503}
504
49ff1092
DM
505pub const PRUNE_SCHEMA_KEEP_DAILY: Schema = IntegerSchema::new(
506 "Number of daily backups to keep.")
507 .minimum(1)
508 .schema();
509
510pub const PRUNE_SCHEMA_KEEP_HOURLY: Schema = IntegerSchema::new(
511 "Number of hourly backups to keep.")
512 .minimum(1)
513 .schema();
514
515pub const PRUNE_SCHEMA_KEEP_LAST: Schema = IntegerSchema::new(
516 "Number of backups to keep.")
517 .minimum(1)
518 .schema();
519
520pub const PRUNE_SCHEMA_KEEP_MONTHLY: Schema = IntegerSchema::new(
521 "Number of monthly backups to keep.")
522 .minimum(1)
523 .schema();
524
525pub const PRUNE_SCHEMA_KEEP_WEEKLY: Schema = IntegerSchema::new(
526 "Number of weekly backups to keep.")
527 .minimum(1)
528 .schema();
529
530pub const PRUNE_SCHEMA_KEEP_YEARLY: Schema = IntegerSchema::new(
531 "Number of yearly backups to keep.")
532 .minimum(1)
533 .schema();
534
09b1f7b2
DM
535#[api(
536 properties: {
537 "filename": {
538 schema: BACKUP_ARCHIVE_NAME_SCHEMA,
539 },
f28d9088
WB
540 "crypt-mode": {
541 type: CryptMode,
542 optional: true,
543 },
09b1f7b2
DM
544 },
545)]
546#[derive(Serialize, Deserialize)]
547#[serde(rename_all="kebab-case")]
548/// Basic information about archive files inside a backup snapshot.
549pub struct BackupContent {
550 pub filename: String,
f28d9088 551 /// Info if file is encrypted, signed, or neither.
e181d2f6 552 #[serde(skip_serializing_if="Option::is_none")]
f28d9088 553 pub crypt_mode: Option<CryptMode>,
09b1f7b2
DM
554 /// Archive size (from backup manifest).
555 #[serde(skip_serializing_if="Option::is_none")]
556 pub size: Option<u64>,
557}
558
a92830dc
DM
559#[api(
560 properties: {
561 "upid": {
562 optional: true,
563 schema: UPID_SCHEMA,
564 },
565 },
566)]
567#[derive(Clone, Serialize, Deserialize)]
568#[serde(rename_all="kebab-case")]
569/// Garbage collection status.
570pub struct GarbageCollectionStatus {
571 pub upid: Option<String>,
572 /// Number of processed index files.
573 pub index_file_count: usize,
574 /// Sum of bytes referred by index files.
575 pub index_data_bytes: u64,
576 /// Bytes used on disk.
577 pub disk_bytes: u64,
578 /// Chunks used on disk.
579 pub disk_chunks: usize,
580 /// Sum of removed bytes.
581 pub removed_bytes: u64,
582 /// Number of removed chunks.
583 pub removed_chunks: usize,
cf459b19
DM
584 /// Sum of pending bytes (pending removal - kept for safety).
585 pub pending_bytes: u64,
586 /// Number of pending chunks (pending removal - kept for safety).
587 pub pending_chunks: usize,
a9767cf7
SR
588 /// Number of chunks marked as .bad by verify that have been removed by GC.
589 pub removed_bad: usize,
a2285525
DC
590 /// Number of chunks still marked as .bad after garbage collection.
591 pub still_bad: usize,
a92830dc
DM
592}
593
594impl Default for GarbageCollectionStatus {
595 fn default() -> Self {
596 GarbageCollectionStatus {
597 upid: None,
598 index_file_count: 0,
599 index_data_bytes: 0,
600 disk_bytes: 0,
601 disk_chunks: 0,
602 removed_bytes: 0,
603 removed_chunks: 0,
cf459b19
DM
604 pending_bytes: 0,
605 pending_chunks: 0,
a9767cf7 606 removed_bad: 0,
a2285525 607 still_bad: 0,
a92830dc
DM
608 }
609 }
610}
611
612
1dc117bb
DM
613#[api()]
614#[derive(Serialize, Deserialize)]
615/// Storage space usage information.
616pub struct StorageStatus {
617 /// Total space (bytes).
618 pub total: u64,
619 /// Used space (bytes).
620 pub used: u64,
621 /// Available space (bytes).
622 pub avail: u64,
623}
ff620a3d 624
99384f79
DM
625#[api(
626 properties: {
e7cb4dc5
WB
627 upid: { schema: UPID_SCHEMA },
628 user: { type: Userid },
99384f79
DM
629 },
630)]
631#[derive(Serialize, Deserialize)]
632/// Task properties.
633pub struct TaskListItem {
634 pub upid: String,
635 /// The node name where the task is running on.
636 pub node: String,
637 /// The Unix PID
638 pub pid: i64,
639 /// The task start time (Epoch)
640 pub pstart: u64,
641 /// The task start time (Epoch)
642 pub starttime: i64,
643 /// Worker type (arbitrary ASCII string)
644 pub worker_type: String,
645 /// Worker ID (arbitrary ASCII string)
646 pub worker_id: Option<String>,
647 /// The user who started the task
e7cb4dc5 648 pub user: Userid,
99384f79
DM
649 /// The task end time (Epoch)
650 #[serde(skip_serializing_if="Option::is_none")]
651 pub endtime: Option<i64>,
652 /// Task end status
653 #[serde(skip_serializing_if="Option::is_none")]
654 pub status: Option<String>,
655}
656
df528ee6
DC
657impl From<crate::server::TaskListInfo> for TaskListItem {
658 fn from(info: crate::server::TaskListInfo) -> Self {
659 let (endtime, status) = info
660 .state
77bd2a46 661 .map_or_else(|| (None, None), |a| (Some(a.endtime()), Some(a.to_string())));
df528ee6
DC
662
663 TaskListItem {
664 upid: info.upid_str,
665 node: "localhost".to_string(),
666 pid: info.upid.pid as i64,
667 pstart: info.upid.pstart,
668 starttime: info.upid.starttime,
669 worker_type: info.upid.worker_type,
670 worker_id: info.upid.worker_id,
e7cb4dc5 671 user: info.upid.userid,
df528ee6
DC
672 endtime,
673 status,
674 }
675 }
676}
677
5976c392
DC
678#[api()]
679#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
680#[serde(rename_all = "lowercase")]
681pub enum TaskStateType {
682 /// Ok
683 OK,
684 /// Warning
685 Warning,
686 /// Error
687 Error,
688 /// Unknown
689 Unknown,
690}
691
ed751dc2
DM
692#[api()]
693#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
694#[serde(rename_all = "lowercase")]
695/// Node Power command type.
696pub enum NodePowerCommand {
697 /// Restart the server
698 Reboot,
699 /// Shutdown the server
700 Shutdown,
701}
702
c357260d
DM
703#[api()]
704#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
705#[serde(rename_all = "lowercase")]
706/// Interface configuration method
707pub enum NetworkConfigMethod {
708 /// Configuration is done manually using other tools
709 Manual,
710 /// Define interfaces with statically allocated addresses.
711 Static,
712 /// Obtain an address via DHCP
713 DHCP,
714 /// Define the loopback interface.
715 Loopback,
716}
717
bab5d18c
DM
718#[api()]
719#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
720#[serde(rename_all = "kebab-case")]
721#[allow(non_camel_case_types)]
722#[repr(u8)]
723/// Linux Bond Mode
724pub enum LinuxBondMode {
725 /// Round-robin policy
726 balance_rr = 0,
727 /// Active-backup policy
728 active_backup = 1,
729 /// XOR policy
730 balance_xor = 2,
731 /// Broadcast policy
732 broadcast = 3,
733 /// IEEE 802.3ad Dynamic link aggregation
8f2f3dd7 734 #[serde(rename = "802.3ad")]
bab5d18c
DM
735 ieee802_3ad = 4,
736 /// Adaptive transmit load balancing
737 balance_tlb = 5,
738 /// Adaptive load balancing
739 balance_alb = 6,
740}
741
8f2f3dd7
DC
742#[api()]
743#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
744#[serde(rename_all = "kebab-case")]
745#[allow(non_camel_case_types)]
746#[repr(u8)]
747/// Bond Transmit Hash Policy for LACP (802.3ad)
748pub enum BondXmitHashPolicy {
749 /// Layer 2
750 layer2 = 0,
751 /// Layer 2+3
752 #[serde(rename = "layer2+3")]
753 layer2_3 = 1,
754 /// Layer 3+4
755 #[serde(rename = "layer3+4")]
756 layer3_4 = 2,
757}
758
02269f3d
DM
759#[api()]
760#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
761#[serde(rename_all = "lowercase")]
762/// Network interface type
763pub enum NetworkInterfaceType {
764 /// Loopback
765 Loopback,
766 /// Physical Ethernet device
7b22acd0 767 Eth,
02269f3d
DM
768 /// Linux Bridge
769 Bridge,
770 /// Linux Bond
771 Bond,
772 /// Linux VLAN (eth.10)
773 Vlan,
774 /// Interface Alias (eth:1)
775 Alias,
776 /// Unknown interface type
777 Unknown,
778}
779
68da20bf
DM
780pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network interface name.")
781 .format(&NETWORK_INTERFACE_FORMAT)
782 .min_length(1)
783 .max_length(libc::IFNAMSIZ-1)
784 .schema();
785
3aedb738 786pub const NETWORK_INTERFACE_ARRAY_SCHEMA: Schema = ArraySchema::new(
1d9a68c2
DM
787 "Network interface list.", &NETWORK_INTERFACE_NAME_SCHEMA)
788 .schema();
789
3aedb738
DM
790pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = StringSchema::new(
791 "A list of network devices, comma separated.")
792 .format(&ApiStringFormat::PropertyString(&NETWORK_INTERFACE_ARRAY_SCHEMA))
793 .schema();
794
c357260d
DM
795#[api(
796 properties: {
797 name: {
68da20bf 798 schema: NETWORK_INTERFACE_NAME_SCHEMA,
c357260d 799 },
7b22acd0 800 "type": {
02269f3d
DM
801 type: NetworkInterfaceType,
802 },
7b22acd0 803 method: {
c357260d
DM
804 type: NetworkConfigMethod,
805 optional: true,
806 },
7b22acd0 807 method6: {
c357260d
DM
808 type: NetworkConfigMethod,
809 optional: true,
810 },
7b22acd0
DM
811 cidr: {
812 schema: CIDR_V4_SCHEMA,
813 optional: true,
814 },
815 cidr6: {
816 schema: CIDR_V6_SCHEMA,
817 optional: true,
818 },
819 gateway: {
820 schema: IP_V4_SCHEMA,
821 optional: true,
822 },
823 gateway6: {
824 schema: IP_V6_SCHEMA,
825 optional: true,
826 },
827 options: {
c357260d
DM
828 description: "Option list (inet)",
829 type: Array,
830 items: {
68da20bf 831 description: "Optional attribute line.",
c357260d
DM
832 type: String,
833 },
834 },
7b22acd0 835 options6: {
c357260d
DM
836 description: "Option list (inet6)",
837 type: Array,
838 items: {
68da20bf 839 description: "Optional attribute line.",
c357260d
DM
840 type: String,
841 },
842 },
7b22acd0 843 comments: {
8a6b86b8
DM
844 description: "Comments (inet, may span multiple lines)",
845 type: String,
846 optional: true,
5f60a58f 847 },
7b22acd0 848 comments6: {
8a6b86b8
DM
849 description: "Comments (inet6, may span multiple lines)",
850 type: String,
851 optional: true,
5f60a58f 852 },
1d9a68c2 853 bridge_ports: {
3aedb738 854 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
1d9a68c2
DM
855 optional: true,
856 },
bab5d18c 857 slaves: {
3aedb738 858 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
42fbe91a
DM
859 optional: true,
860 },
bab5d18c
DM
861 bond_mode: {
862 type: LinuxBondMode,
863 optional: true,
85959a99
DC
864 },
865 "bond-primary": {
866 schema: NETWORK_INTERFACE_NAME_SCHEMA,
867 optional: true,
868 },
8f2f3dd7
DC
869 bond_xmit_hash_policy: {
870 type: BondXmitHashPolicy,
871 optional: true,
872 },
c357260d
DM
873 }
874)]
875#[derive(Debug, Serialize, Deserialize)]
876/// Network Interface configuration
877pub struct Interface {
878 /// Autostart interface
7b22acd0
DM
879 #[serde(rename = "autostart")]
880 pub autostart: bool,
c357260d
DM
881 /// Interface is active (UP)
882 pub active: bool,
883 /// Interface name
884 pub name: String,
02269f3d 885 /// Interface type
7b22acd0 886 #[serde(rename = "type")]
02269f3d 887 pub interface_type: NetworkInterfaceType,
c357260d 888 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 889 pub method: Option<NetworkConfigMethod>,
c357260d 890 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 891 pub method6: Option<NetworkConfigMethod>,
c357260d 892 #[serde(skip_serializing_if="Option::is_none")]
8b57cd44 893 /// IPv4 address with netmask
7b22acd0 894 pub cidr: Option<String>,
c357260d
DM
895 #[serde(skip_serializing_if="Option::is_none")]
896 /// IPv4 gateway
7b22acd0 897 pub gateway: Option<String>,
c357260d 898 #[serde(skip_serializing_if="Option::is_none")]
8b57cd44 899 /// IPv6 address with netmask
7b22acd0 900 pub cidr6: Option<String>,
c357260d
DM
901 #[serde(skip_serializing_if="Option::is_none")]
902 /// IPv6 gateway
7b22acd0 903 pub gateway6: Option<String>,
3fce3bc3 904
c357260d 905 #[serde(skip_serializing_if="Vec::is_empty")]
7b22acd0 906 pub options: Vec<String>,
c357260d 907 #[serde(skip_serializing_if="Vec::is_empty")]
7b22acd0 908 pub options6: Vec<String>,
2c18efd9 909
8a6b86b8 910 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 911 pub comments: Option<String>,
8a6b86b8 912 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 913 pub comments6: Option<String>,
5f60a58f 914
2c18efd9
DM
915 #[serde(skip_serializing_if="Option::is_none")]
916 /// Maximum Transmission Unit
917 pub mtu: Option<u64>,
1d9a68c2
DM
918
919 #[serde(skip_serializing_if="Option::is_none")]
920 pub bridge_ports: Option<Vec<String>>,
7b22acd0
DM
921 /// Enable bridge vlan support.
922 #[serde(skip_serializing_if="Option::is_none")]
923 pub bridge_vlan_aware: Option<bool>,
42fbe91a
DM
924
925 #[serde(skip_serializing_if="Option::is_none")]
bab5d18c
DM
926 pub slaves: Option<Vec<String>>,
927 #[serde(skip_serializing_if="Option::is_none")]
928 pub bond_mode: Option<LinuxBondMode>,
85959a99
DC
929 #[serde(skip_serializing_if="Option::is_none")]
930 #[serde(rename = "bond-primary")]
931 pub bond_primary: Option<String>,
8f2f3dd7 932 pub bond_xmit_hash_policy: Option<BondXmitHashPolicy>,
c357260d
DM
933}
934
ff620a3d
DM
935// Regression tests
936
dcb8db66 937#[test]
ff329f97 938fn test_cert_fingerprint_schema() -> Result<(), anyhow::Error> {
dcb8db66
DM
939
940 let schema = CERT_FINGERPRINT_SHA256_SCHEMA;
941
942 let invalid_fingerprints = [
943 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
944 "88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
945 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8:ff",
946 "XX:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
947 "86:88:Y4:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
948 "86:88:0:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
949 ];
950
951 for fingerprint in invalid_fingerprints.iter() {
952 if let Ok(_) = parse_simple_value(fingerprint, &schema) {
add5861e 953 bail!("test fingerprint '{}' failed - got Ok() while exception an error.", fingerprint);
dcb8db66
DM
954 }
955 }
956
957 let valid_fingerprints = [
958 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
959 "86:88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
960 ];
961
962 for fingerprint in valid_fingerprints.iter() {
963 let v = match parse_simple_value(fingerprint, &schema) {
964 Ok(v) => v,
965 Err(err) => {
966 bail!("unable to parse fingerprint '{}' - {}", fingerprint, err);
967 }
968 };
969
970 if v != serde_json::json!(fingerprint) {
971 bail!("unable to parse fingerprint '{}' - got wrong value {:?}", fingerprint, v);
972 }
973 }
974
975 Ok(())
976}
977
ff620a3d 978#[test]
ff329f97 979fn test_proxmox_user_id_schema() -> Result<(), anyhow::Error> {
ff620a3d
DM
980 let invalid_user_ids = [
981 "x", // too short
982 "xx", // too short
983 "xxx", // no realm
984 "xxx@", // no realm
985 "xx x@test", // contains space
986 "xx\nx@test", // contains control character
987 "x:xx@test", // contains collon
988 "xx/x@test", // contains slash
989 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@test", // too long
990 ];
991
992 for name in invalid_user_ids.iter() {
e7cb4dc5 993 if let Ok(_) = parse_simple_value(name, &Userid::API_SCHEMA) {
add5861e 994 bail!("test userid '{}' failed - got Ok() while exception an error.", name);
ff620a3d
DM
995 }
996 }
997
998 let valid_user_ids = [
999 "xxx@y",
1000 "name@y",
1001 "xxx@test-it.com",
1002 "xxx@_T_E_S_T-it.com",
1003 "x_x-x.x@test-it.com",
1004 ];
1005
1006 for name in valid_user_ids.iter() {
e7cb4dc5 1007 let v = match parse_simple_value(name, &Userid::API_SCHEMA) {
ff620a3d
DM
1008 Ok(v) => v,
1009 Err(err) => {
1010 bail!("unable to parse userid '{}' - {}", name, err);
1011 }
1012 };
1013
1014 if v != serde_json::json!(name) {
1015 bail!("unable to parse userid '{}' - got wrong value {:?}", name, v);
1016 }
1017 }
1018
1019 Ok(())
1020}
a2f862ee
DM
1021
1022#[api()]
1023#[derive(Copy, Clone, Serialize, Deserialize)]
1024#[serde(rename_all = "UPPERCASE")]
1025pub enum RRDMode {
1026 /// Maximum
1027 Max,
1028 /// Average
1029 Average,
1030}
1031
1032
1033#[api()]
1034#[repr(u64)]
1035#[derive(Copy, Clone, Serialize, Deserialize)]
1036#[serde(rename_all = "lowercase")]
1037pub enum RRDTimeFrameResolution {
1038 /// 1 min => last 70 minutes
1039 Hour = 60,
1040 /// 30 min => last 35 hours
1041 Day = 60*30,
1042 /// 3 hours => about 8 days
1043 Week = 60*180,
1044 /// 12 hours => last 35 days
1045 Month = 60*720,
1046 /// 1 week => last 490 days
1047 Year = 60*10080,
1048}
a4e86972
SR
1049
1050#[api()]
1051#[derive(Serialize, Deserialize)]
1052#[serde(rename_all = "PascalCase")]
1053/// Describes a package for which an update is available.
1054pub struct APTUpdateInfo {
1055 /// Package name
1056 pub package: String,
1057 /// Package title
1058 pub title: String,
1059 /// Package architecture
1060 pub arch: String,
1061 /// Human readable package description
1062 pub description: String,
1063 /// New version to be updated to
1064 pub version: String,
1065 /// Old version currently installed
1066 pub old_version: String,
1067 /// Package origin
1068 pub origin: String,
1069 /// Package priority in human-readable form
1070 pub priority: String,
1071 /// Package section
1072 pub section: String,
1073 /// URL under which the package's changelog can be retrieved
1074 pub change_log_url: String,
1075}