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