]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/types.rs
5ce3883812933eed0a3e5bafbac047d6f1bd91fe
[proxmox-backup.git] / src / api2 / types.rs
1 use anyhow::{bail};
2 use ::serde::{Deserialize, Serialize};
3
4 use proxmox::api::{api, schema::*};
5 use proxmox::const_regex;
6 use proxmox::{IPRE, IPV4RE, IPV6RE, IPV4OCTET, IPV6H16, IPV6LS32};
7
8 // File names: may not contain slashes, may not start with "."
9 pub const FILENAME_FORMAT: ApiStringFormat = ApiStringFormat::VerifyFn(|name| {
10 if name.starts_with('.') {
11 bail!("file names may not start with '.'");
12 }
13 if name.contains('/') {
14 bail!("file names may not contain slashes");
15 }
16 Ok(())
17 });
18
19 macro_rules! DNS_LABEL { () => (r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)") }
20 macro_rules! DNS_NAME { () => (concat!(r"(?:", DNS_LABEL!() , r"\.)*", DNS_LABEL!())) }
21
22 // we only allow a limited set of characters
23 // colon is not allowed, because we store usernames in
24 // colon separated lists)!
25 // slash is not allowed because it is used as pve API delimiter
26 // also see "man useradd"
27 macro_rules! USER_NAME_REGEX_STR { () => (r"(?:[^\s:/[:cntrl:]]+)") }
28 macro_rules! GROUP_NAME_REGEX_STR { () => (USER_NAME_REGEX_STR!()) }
29
30 #[macro_export]
31 macro_rules! PROXMOX_SAFE_ID_REGEX_STR { () => (r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)") }
32
33 macro_rules! CIDR_V4_REGEX_STR { () => (concat!(r"(?:", IPV4RE!(), r"/\d{1,2})$")) }
34 macro_rules! CIDR_V6_REGEX_STR { () => (concat!(r"(?:", IPV6RE!(), r"/\d{1,3})$")) }
35
36 const_regex!{
37 pub IP_V4_REGEX = concat!(r"^", IPV4RE!(), r"$");
38 pub IP_V6_REGEX = concat!(r"^", IPV6RE!(), r"$");
39 pub IP_REGEX = concat!(r"^", IPRE!(), r"$");
40 pub CIDR_V4_REGEX = concat!(r"^", CIDR_V4_REGEX_STR!(), r"$");
41 pub CIDR_V6_REGEX = concat!(r"^", CIDR_V6_REGEX_STR!(), r"$");
42 pub CIDR_REGEX = concat!(r"^(?:", CIDR_V4_REGEX_STR!(), "|", CIDR_V6_REGEX_STR!(), r")$");
43
44 pub SHA256_HEX_REGEX = r"^[a-f0-9]{64}$"; // fixme: define in common_regex ?
45 pub SYSTEMD_DATETIME_REGEX = r"^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$"; // fixme: define in common_regex ?
46
47 pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
48
49 /// Regex for safe identifiers.
50 ///
51 /// This
52 /// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
53 /// contains further information why it is reasonable to restict
54 /// names this way. This is not only useful for filenames, but for
55 /// any identifier command line tools work with.
56 pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
57
58 pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
59
60 pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$";
61
62 pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$");
63
64 pub DNS_NAME_OR_IP_REGEX = concat!(r"^", DNS_NAME!(), "|", IPRE!(), r"$");
65
66 pub PROXMOX_USER_ID_REGEX = concat!(r"^", USER_NAME_REGEX_STR!(), r"@", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
67
68 pub PROXMOX_GROUP_ID_REGEX = concat!(r"^", GROUP_NAME_REGEX_STR!(), r"$");
69
70 pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
71
72 pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$");
73 }
74
75 pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat =
76 ApiStringFormat::Pattern(&SYSTEMD_DATETIME_REGEX);
77
78 pub const IP_V4_FORMAT: ApiStringFormat =
79 ApiStringFormat::Pattern(&IP_V4_REGEX);
80
81 pub const IP_V6_FORMAT: ApiStringFormat =
82 ApiStringFormat::Pattern(&IP_V6_REGEX);
83
84 pub const IP_FORMAT: ApiStringFormat =
85 ApiStringFormat::Pattern(&IP_REGEX);
86
87 pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat =
88 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
89
90 pub const CERT_FINGERPRINT_SHA256_FORMAT: ApiStringFormat =
91 ApiStringFormat::Pattern(&CERT_FINGERPRINT_SHA256_REGEX);
92
93 pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
94 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
95
96 pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
97 ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
98
99 pub const HOSTNAME_FORMAT: ApiStringFormat =
100 ApiStringFormat::Pattern(&HOSTNAME_REGEX);
101
102 pub const DNS_NAME_FORMAT: ApiStringFormat =
103 ApiStringFormat::Pattern(&DNS_NAME_REGEX);
104
105 pub const DNS_NAME_OR_IP_FORMAT: ApiStringFormat =
106 ApiStringFormat::Pattern(&DNS_NAME_OR_IP_REGEX);
107
108 pub const PROXMOX_USER_ID_FORMAT: ApiStringFormat =
109 ApiStringFormat::Pattern(&PROXMOX_USER_ID_REGEX);
110
111 pub const PROXMOX_GROUP_ID_FORMAT: ApiStringFormat =
112 ApiStringFormat::Pattern(&PROXMOX_GROUP_ID_REGEX);
113
114 pub const PASSWORD_FORMAT: ApiStringFormat =
115 ApiStringFormat::Pattern(&PASSWORD_REGEX);
116
117 pub const ACL_PATH_FORMAT: ApiStringFormat =
118 ApiStringFormat::Pattern(&ACL_PATH_REGEX);
119
120 pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat =
121 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
122
123 pub const CIDR_V4_FORMAT: ApiStringFormat =
124 ApiStringFormat::Pattern(&CIDR_V4_REGEX);
125
126 pub const CIDR_V6_FORMAT: ApiStringFormat =
127 ApiStringFormat::Pattern(&CIDR_V6_REGEX);
128
129 pub const CIDR_FORMAT: ApiStringFormat =
130 ApiStringFormat::Pattern(&CIDR_REGEX);
131
132
133 pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
134 .format(&PASSWORD_FORMAT)
135 .min_length(1)
136 .max_length(1024)
137 .schema();
138
139 pub const PBS_PASSWORD_SCHEMA: Schema = StringSchema::new("User Password.")
140 .format(&PASSWORD_FORMAT)
141 .min_length(5)
142 .max_length(64)
143 .schema();
144
145 pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema = StringSchema::new(
146 "X509 certificate fingerprint (sha256)."
147 )
148 .format(&CERT_FINGERPRINT_SHA256_FORMAT)
149 .schema();
150
151 pub const PROXMOX_CONFIG_DIGEST_SCHEMA: Schema = StringSchema::new(r#"\
152 Prevent changes if current configuration file has different SHA256 digest.
153 This can be used to prevent concurrent modifications.
154 "#
155 )
156 .format(&PVE_CONFIG_DIGEST_FORMAT)
157 .schema();
158
159
160 pub const CHUNK_DIGEST_FORMAT: ApiStringFormat =
161 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
162
163 pub const CHUNK_DIGEST_SCHEMA: Schema = StringSchema::new("Chunk digest (SHA256).")
164 .format(&CHUNK_DIGEST_FORMAT)
165 .schema();
166
167 pub 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
177 pub const SEARCH_DOMAIN_SCHEMA: Schema =
178 StringSchema::new("Search domain for host-name lookup.").schema();
179
180 pub const FIRST_DNS_SERVER_SCHEMA: Schema =
181 StringSchema::new("First name server IP address.")
182 .format(&IP_FORMAT)
183 .schema();
184
185 pub const SECOND_DNS_SERVER_SCHEMA: Schema =
186 StringSchema::new("Second name server IP address.")
187 .format(&IP_FORMAT)
188 .schema();
189
190 pub const THIRD_DNS_SERVER_SCHEMA: Schema =
191 StringSchema::new("Third name server IP address.")
192 .format(&IP_FORMAT)
193 .schema();
194
195 pub const IP_V4_SCHEMA: Schema =
196 StringSchema::new("IPv4 address.")
197 .format(&IP_V4_FORMAT)
198 .max_length(15)
199 .schema();
200
201 pub const IP_V6_SCHEMA: Schema =
202 StringSchema::new("IPv6 address.")
203 .format(&IP_V6_FORMAT)
204 .max_length(39)
205 .schema();
206
207 pub const IP_SCHEMA: Schema =
208 StringSchema::new("IP (IPv4 or IPv6) address.")
209 .format(&IP_FORMAT)
210 .max_length(39)
211 .schema();
212
213 pub 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
219 pub 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
225 pub 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
231 pub 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
238 pub 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
245 pub const ACL_PROPAGATE_SCHEMA: Schema = BooleanSchema::new(
246 "Allow to propagate (inherit) permissions.")
247 .default(true)
248 .schema();
249
250 pub const ACL_UGID_TYPE_SCHEMA: Schema = StringSchema::new(
251 "Type of 'ugid' property.")
252 .format(&ApiStringFormat::Enum(&[
253 EnumEntry::new("user", "User"),
254 EnumEntry::new("group", "Group")]))
255 .schema();
256
257 pub const BACKUP_ARCHIVE_NAME_SCHEMA: Schema =
258 StringSchema::new("Backup archive name.")
259 .format(&PROXMOX_SAFE_ID_FORMAT)
260 .schema();
261
262 pub const BACKUP_TYPE_SCHEMA: Schema =
263 StringSchema::new("Backup type.")
264 .format(&ApiStringFormat::Enum(&[
265 EnumEntry::new("vm", "Virtual Machine Backup"),
266 EnumEntry::new("ct", "Container Backup"),
267 EnumEntry::new("host", "Host Backup")]))
268 .schema();
269
270 pub const BACKUP_ID_SCHEMA: Schema =
271 StringSchema::new("Backup ID.")
272 .format(&PROXMOX_SAFE_ID_FORMAT)
273 .schema();
274
275 pub const BACKUP_TIME_SCHEMA: Schema =
276 IntegerSchema::new("Backup time (Unix epoch.)")
277 .minimum(1_547_797_308)
278 .schema();
279
280 pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
281 .max_length(256)
282 .schema();
283
284 pub const DATASTORE_SCHEMA: Schema = StringSchema::new("Datastore name.")
285 .format(&PROXMOX_SAFE_ID_FORMAT)
286 .min_length(3)
287 .max_length(32)
288 .schema();
289
290 pub const GC_SCHEDULE_SCHEMA: Schema = StringSchema::new(
291 "Run garbage collection job at specified schedule.")
292 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
293 .schema();
294
295 pub const PRUNE_SCHEDULE_SCHEMA: Schema = StringSchema::new(
296 "Run prune job at specified schedule.")
297 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
298 .schema();
299
300 pub const REMOTE_ID_SCHEMA: Schema = StringSchema::new("Remote ID.")
301 .format(&PROXMOX_SAFE_ID_FORMAT)
302 .min_length(3)
303 .max_length(32)
304 .schema();
305
306 pub const JOB_ID_SCHEMA: Schema = StringSchema::new("Job ID.")
307 .format(&PROXMOX_SAFE_ID_FORMAT)
308 .min_length(3)
309 .max_length(32)
310 .schema();
311
312 pub const REMOVE_VANISHED_BACKUPS_SCHEMA: Schema = BooleanSchema::new(
313 "Delete vanished backups. This remove the local copy if the remote backup was deleted.")
314 .default(true)
315 .schema();
316
317 pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
318 .format(&SINGLE_LINE_COMMENT_FORMAT)
319 .schema();
320
321 pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).")
322 .format(&HOSTNAME_FORMAT)
323 .schema();
324
325 pub const DNS_NAME_OR_IP_SCHEMA: Schema = StringSchema::new("DNS name or IP address.")
326 .format(&DNS_NAME_OR_IP_FORMAT)
327 .schema();
328
329 pub const PROXMOX_AUTH_REALM_SCHEMA: Schema = StringSchema::new("Authentication domain ID")
330 .format(&PROXMOX_SAFE_ID_FORMAT)
331 .min_length(3)
332 .max_length(32)
333 .schema();
334
335 pub const PROXMOX_USER_ID_SCHEMA: Schema = StringSchema::new("User ID")
336 .format(&PROXMOX_USER_ID_FORMAT)
337 .min_length(3)
338 .max_length(64)
339 .schema();
340
341 pub const PROXMOX_GROUP_ID_SCHEMA: Schema = StringSchema::new("Group ID")
342 .format(&PROXMOX_GROUP_ID_FORMAT)
343 .min_length(3)
344 .max_length(64)
345 .schema();
346
347
348 // Complex type definitions
349
350 #[api(
351 properties: {
352 "backup-type": {
353 schema: BACKUP_TYPE_SCHEMA,
354 },
355 "backup-id": {
356 schema: BACKUP_ID_SCHEMA,
357 },
358 "last-backup": {
359 schema: BACKUP_TIME_SCHEMA,
360 },
361 "backup-count": {
362 type: Integer,
363 },
364 files: {
365 items: {
366 schema: BACKUP_ARCHIVE_NAME_SCHEMA
367 },
368 },
369 },
370 )]
371 #[derive(Serialize, Deserialize)]
372 #[serde(rename_all="kebab-case")]
373 /// Basic information about a backup group.
374 pub struct GroupListItem {
375 pub backup_type: String, // enum
376 pub backup_id: String,
377 pub last_backup: i64,
378 /// Number of contained snapshots
379 pub backup_count: u64,
380 /// List of contained archive files.
381 pub files: Vec<String>,
382 }
383
384 #[api(
385 properties: {
386 "backup-type": {
387 schema: BACKUP_TYPE_SCHEMA,
388 },
389 "backup-id": {
390 schema: BACKUP_ID_SCHEMA,
391 },
392 "backup-time": {
393 schema: BACKUP_TIME_SCHEMA,
394 },
395 files: {
396 items: {
397 schema: BACKUP_ARCHIVE_NAME_SCHEMA
398 },
399 },
400 },
401 )]
402 #[derive(Serialize, Deserialize)]
403 #[serde(rename_all="kebab-case")]
404 /// Basic information about backup snapshot.
405 pub struct SnapshotListItem {
406 pub backup_type: String, // enum
407 pub backup_id: String,
408 pub backup_time: i64,
409 /// List of contained archive files.
410 pub files: Vec<String>,
411 /// Overall snapshot size (sum of all archive sizes).
412 #[serde(skip_serializing_if="Option::is_none")]
413 pub size: Option<u64>,
414 }
415
416 #[api(
417 properties: {
418 "backup-type": {
419 schema: BACKUP_TYPE_SCHEMA,
420 },
421 "backup-id": {
422 schema: BACKUP_ID_SCHEMA,
423 },
424 "backup-time": {
425 schema: BACKUP_TIME_SCHEMA,
426 },
427 },
428 )]
429 #[derive(Serialize, Deserialize)]
430 #[serde(rename_all="kebab-case")]
431 /// Prune result.
432 pub struct PruneListItem {
433 pub backup_type: String, // enum
434 pub backup_id: String,
435 pub backup_time: i64,
436 /// Keep snapshot
437 pub keep: bool,
438 }
439
440 pub const PRUNE_SCHEMA_KEEP_DAILY: Schema = IntegerSchema::new(
441 "Number of daily backups to keep.")
442 .minimum(1)
443 .schema();
444
445 pub const PRUNE_SCHEMA_KEEP_HOURLY: Schema = IntegerSchema::new(
446 "Number of hourly backups to keep.")
447 .minimum(1)
448 .schema();
449
450 pub const PRUNE_SCHEMA_KEEP_LAST: Schema = IntegerSchema::new(
451 "Number of backups to keep.")
452 .minimum(1)
453 .schema();
454
455 pub const PRUNE_SCHEMA_KEEP_MONTHLY: Schema = IntegerSchema::new(
456 "Number of monthly backups to keep.")
457 .minimum(1)
458 .schema();
459
460 pub const PRUNE_SCHEMA_KEEP_WEEKLY: Schema = IntegerSchema::new(
461 "Number of weekly backups to keep.")
462 .minimum(1)
463 .schema();
464
465 pub const PRUNE_SCHEMA_KEEP_YEARLY: Schema = IntegerSchema::new(
466 "Number of yearly backups to keep.")
467 .minimum(1)
468 .schema();
469
470 #[api(
471 properties: {
472 "filename": {
473 schema: BACKUP_ARCHIVE_NAME_SCHEMA,
474 },
475 },
476 )]
477 #[derive(Serialize, Deserialize)]
478 #[serde(rename_all="kebab-case")]
479 /// Basic information about archive files inside a backup snapshot.
480 pub struct BackupContent {
481 pub filename: String,
482 /// Archive size (from backup manifest).
483 #[serde(skip_serializing_if="Option::is_none")]
484 pub size: Option<u64>,
485 }
486
487 #[api(
488 properties: {
489 "upid": {
490 optional: true,
491 schema: UPID_SCHEMA,
492 },
493 },
494 )]
495 #[derive(Clone, Serialize, Deserialize)]
496 #[serde(rename_all="kebab-case")]
497 /// Garbage collection status.
498 pub struct GarbageCollectionStatus {
499 pub upid: Option<String>,
500 /// Number of processed index files.
501 pub index_file_count: usize,
502 /// Sum of bytes referred by index files.
503 pub index_data_bytes: u64,
504 /// Bytes used on disk.
505 pub disk_bytes: u64,
506 /// Chunks used on disk.
507 pub disk_chunks: usize,
508 /// Sum of removed bytes.
509 pub removed_bytes: u64,
510 /// Number of removed chunks.
511 pub removed_chunks: usize,
512 /// Sum of pending bytes (pending removal - kept for safety).
513 pub pending_bytes: u64,
514 /// Number of pending chunks (pending removal - kept for safety).
515 pub pending_chunks: usize,
516 }
517
518 impl Default for GarbageCollectionStatus {
519 fn default() -> Self {
520 GarbageCollectionStatus {
521 upid: None,
522 index_file_count: 0,
523 index_data_bytes: 0,
524 disk_bytes: 0,
525 disk_chunks: 0,
526 removed_bytes: 0,
527 removed_chunks: 0,
528 pending_bytes: 0,
529 pending_chunks: 0,
530 }
531 }
532 }
533
534
535 #[api()]
536 #[derive(Serialize, Deserialize)]
537 /// Storage space usage information.
538 pub struct StorageStatus {
539 /// Total space (bytes).
540 pub total: u64,
541 /// Used space (bytes).
542 pub used: u64,
543 /// Available space (bytes).
544 pub avail: u64,
545 }
546
547 #[api(
548 properties: {
549 "upid": { schema: UPID_SCHEMA },
550 },
551 )]
552 #[derive(Serialize, Deserialize)]
553 /// Task properties.
554 pub struct TaskListItem {
555 pub upid: String,
556 /// The node name where the task is running on.
557 pub node: String,
558 /// The Unix PID
559 pub pid: i64,
560 /// The task start time (Epoch)
561 pub pstart: u64,
562 /// The task start time (Epoch)
563 pub starttime: i64,
564 /// Worker type (arbitrary ASCII string)
565 pub worker_type: String,
566 /// Worker ID (arbitrary ASCII string)
567 pub worker_id: Option<String>,
568 /// The user who started the task
569 pub user: String,
570 /// The task end time (Epoch)
571 #[serde(skip_serializing_if="Option::is_none")]
572 pub endtime: Option<i64>,
573 /// Task end status
574 #[serde(skip_serializing_if="Option::is_none")]
575 pub status: Option<String>,
576 }
577
578 #[api()]
579 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
580 #[serde(rename_all = "lowercase")]
581 /// Node Power command type.
582 pub enum NodePowerCommand {
583 /// Restart the server
584 Reboot,
585 /// Shutdown the server
586 Shutdown,
587 }
588
589 #[api()]
590 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
591 #[serde(rename_all = "lowercase")]
592 /// Interface configuration method
593 pub enum NetworkConfigMethod {
594 /// Configuration is done manually using other tools
595 Manual,
596 /// Define interfaces with statically allocated addresses.
597 Static,
598 /// Obtain an address via DHCP
599 DHCP,
600 /// Define the loopback interface.
601 Loopback,
602 }
603
604 #[api()]
605 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
606 #[serde(rename_all = "kebab-case")]
607 #[allow(non_camel_case_types)]
608 #[repr(u8)]
609 /// Linux Bond Mode
610 pub enum LinuxBondMode {
611 /// Round-robin policy
612 balance_rr = 0,
613 /// Active-backup policy
614 active_backup = 1,
615 /// XOR policy
616 balance_xor = 2,
617 /// Broadcast policy
618 broadcast = 3,
619 /// IEEE 802.3ad Dynamic link aggregation
620 //#[serde(rename = "802.3ad")]
621 ieee802_3ad = 4,
622 /// Adaptive transmit load balancing
623 balance_tlb = 5,
624 /// Adaptive load balancing
625 balance_alb = 6,
626 }
627
628 #[api()]
629 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
630 #[serde(rename_all = "lowercase")]
631 /// Network interface type
632 pub enum NetworkInterfaceType {
633 /// Loopback
634 Loopback,
635 /// Physical Ethernet device
636 Eth,
637 /// Linux Bridge
638 Bridge,
639 /// Linux Bond
640 Bond,
641 /// Linux VLAN (eth.10)
642 Vlan,
643 /// Interface Alias (eth:1)
644 Alias,
645 /// Unknown interface type
646 Unknown,
647 }
648
649 pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network interface name.")
650 .format(&NETWORK_INTERFACE_FORMAT)
651 .min_length(1)
652 .max_length(libc::IFNAMSIZ-1)
653 .schema();
654
655 pub const NETWORK_INTERFACE_ARRAY_SCHEMA: Schema = ArraySchema::new(
656 "Network interface list.", &NETWORK_INTERFACE_NAME_SCHEMA)
657 .schema();
658
659 pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = StringSchema::new(
660 "A list of network devices, comma separated.")
661 .format(&ApiStringFormat::PropertyString(&NETWORK_INTERFACE_ARRAY_SCHEMA))
662 .schema();
663
664 #[api(
665 properties: {
666 name: {
667 schema: NETWORK_INTERFACE_NAME_SCHEMA,
668 },
669 "type": {
670 type: NetworkInterfaceType,
671 },
672 method: {
673 type: NetworkConfigMethod,
674 optional: true,
675 },
676 method6: {
677 type: NetworkConfigMethod,
678 optional: true,
679 },
680 cidr: {
681 schema: CIDR_V4_SCHEMA,
682 optional: true,
683 },
684 cidr6: {
685 schema: CIDR_V6_SCHEMA,
686 optional: true,
687 },
688 gateway: {
689 schema: IP_V4_SCHEMA,
690 optional: true,
691 },
692 gateway6: {
693 schema: IP_V6_SCHEMA,
694 optional: true,
695 },
696 options: {
697 description: "Option list (inet)",
698 type: Array,
699 items: {
700 description: "Optional attribute line.",
701 type: String,
702 },
703 },
704 options6: {
705 description: "Option list (inet6)",
706 type: Array,
707 items: {
708 description: "Optional attribute line.",
709 type: String,
710 },
711 },
712 comments: {
713 description: "Comments (inet, may span multiple lines)",
714 type: String,
715 optional: true,
716 },
717 comments6: {
718 description: "Comments (inet6, may span multiple lines)",
719 type: String,
720 optional: true,
721 },
722 bridge_ports: {
723 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
724 optional: true,
725 },
726 slaves: {
727 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
728 optional: true,
729 },
730 bond_mode: {
731 type: LinuxBondMode,
732 optional: true,
733 }
734 }
735 )]
736 #[derive(Debug, Serialize, Deserialize)]
737 /// Network Interface configuration
738 pub struct Interface {
739 /// Autostart interface
740 #[serde(rename = "autostart")]
741 pub autostart: bool,
742 /// Interface is active (UP)
743 pub active: bool,
744 /// Interface name
745 pub name: String,
746 /// Interface type
747 #[serde(rename = "type")]
748 pub interface_type: NetworkInterfaceType,
749 #[serde(skip_serializing_if="Option::is_none")]
750 pub method: Option<NetworkConfigMethod>,
751 #[serde(skip_serializing_if="Option::is_none")]
752 pub method6: Option<NetworkConfigMethod>,
753 #[serde(skip_serializing_if="Option::is_none")]
754 /// IPv4 address with netmask
755 pub cidr: Option<String>,
756 #[serde(skip_serializing_if="Option::is_none")]
757 /// IPv4 gateway
758 pub gateway: Option<String>,
759 #[serde(skip_serializing_if="Option::is_none")]
760 /// IPv6 address with netmask
761 pub cidr6: Option<String>,
762 #[serde(skip_serializing_if="Option::is_none")]
763 /// IPv6 gateway
764 pub gateway6: Option<String>,
765
766 #[serde(skip_serializing_if="Vec::is_empty")]
767 pub options: Vec<String>,
768 #[serde(skip_serializing_if="Vec::is_empty")]
769 pub options6: Vec<String>,
770
771 #[serde(skip_serializing_if="Option::is_none")]
772 pub comments: Option<String>,
773 #[serde(skip_serializing_if="Option::is_none")]
774 pub comments6: Option<String>,
775
776 #[serde(skip_serializing_if="Option::is_none")]
777 /// Maximum Transmission Unit
778 pub mtu: Option<u64>,
779
780 #[serde(skip_serializing_if="Option::is_none")]
781 pub bridge_ports: Option<Vec<String>>,
782 /// Enable bridge vlan support.
783 #[serde(skip_serializing_if="Option::is_none")]
784 pub bridge_vlan_aware: Option<bool>,
785
786 #[serde(skip_serializing_if="Option::is_none")]
787 pub slaves: Option<Vec<String>>,
788 #[serde(skip_serializing_if="Option::is_none")]
789 pub bond_mode: Option<LinuxBondMode>,
790 }
791
792 // Regression tests
793
794 #[test]
795 fn test_cert_fingerprint_schema() -> Result<(), anyhow::Error> {
796
797 let schema = CERT_FINGERPRINT_SHA256_SCHEMA;
798
799 let invalid_fingerprints = [
800 "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",
801 "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",
802 "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",
803 "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",
804 "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",
805 "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",
806 ];
807
808 for fingerprint in invalid_fingerprints.iter() {
809 if let Ok(_) = parse_simple_value(fingerprint, &schema) {
810 bail!("test fingerprint '{}' failed - got Ok() while expection an error.", fingerprint);
811 }
812 }
813
814 let valid_fingerprints = [
815 "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",
816 "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",
817 ];
818
819 for fingerprint in valid_fingerprints.iter() {
820 let v = match parse_simple_value(fingerprint, &schema) {
821 Ok(v) => v,
822 Err(err) => {
823 bail!("unable to parse fingerprint '{}' - {}", fingerprint, err);
824 }
825 };
826
827 if v != serde_json::json!(fingerprint) {
828 bail!("unable to parse fingerprint '{}' - got wrong value {:?}", fingerprint, v);
829 }
830 }
831
832 Ok(())
833 }
834
835 #[test]
836 fn test_proxmox_user_id_schema() -> Result<(), anyhow::Error> {
837
838 let schema = PROXMOX_USER_ID_SCHEMA;
839
840 let invalid_user_ids = [
841 "x", // too short
842 "xx", // too short
843 "xxx", // no realm
844 "xxx@", // no realm
845 "xx x@test", // contains space
846 "xx\nx@test", // contains control character
847 "x:xx@test", // contains collon
848 "xx/x@test", // contains slash
849 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@test", // too long
850 ];
851
852 for name in invalid_user_ids.iter() {
853 if let Ok(_) = parse_simple_value(name, &schema) {
854 bail!("test userid '{}' failed - got Ok() while expection an error.", name);
855 }
856 }
857
858 let valid_user_ids = [
859 "xxx@y",
860 "name@y",
861 "xxx@test-it.com",
862 "xxx@_T_E_S_T-it.com",
863 "x_x-x.x@test-it.com",
864 ];
865
866 for name in valid_user_ids.iter() {
867 let v = match parse_simple_value(name, &schema) {
868 Ok(v) => v,
869 Err(err) => {
870 bail!("unable to parse userid '{}' - {}", name, err);
871 }
872 };
873
874 if v != serde_json::json!(name) {
875 bail!("unable to parse userid '{}' - got wrong value {:?}", name, v);
876 }
877 }
878
879 Ok(())
880 }
881
882 #[api()]
883 #[derive(Copy, Clone, Serialize, Deserialize)]
884 #[serde(rename_all = "UPPERCASE")]
885 pub enum RRDMode {
886 /// Maximum
887 Max,
888 /// Average
889 Average,
890 }
891
892
893 #[api()]
894 #[repr(u64)]
895 #[derive(Copy, Clone, Serialize, Deserialize)]
896 #[serde(rename_all = "lowercase")]
897 pub enum RRDTimeFrameResolution {
898 /// 1 min => last 70 minutes
899 Hour = 60,
900 /// 30 min => last 35 hours
901 Day = 60*30,
902 /// 3 hours => about 8 days
903 Week = 60*180,
904 /// 12 hours => last 35 days
905 Month = 60*720,
906 /// 1 week => last 490 days
907 Year = 60*10080,
908 }