2 use lazy_static
::lazy_static
;
3 use std
::collections
::HashMap
;
4 use serde
::{Serialize, Deserialize}
;
16 use proxmox
::tools
::{fs::replace_file, fs::CreateOptions}
;
18 use crate::api2
::types
::*;
21 static ref CONFIG
: SectionConfig
= init();
24 pub const REMOTE_PASSWORD_SCHEMA
: Schema
= StringSchema
::new("Password or auth token for remote host.")
25 .format(&PASSWORD_FORMAT
)
34 schema
: SINGLE_LINE_COMMENT_SCHEMA
,
37 schema
: DNS_NAME_OR_IP_SCHEMA
,
40 schema
: PROXMOX_USER_ID_SCHEMA
,
43 schema
: REMOTE_PASSWORD_SCHEMA
,
47 schema
: CERT_FINGERPRINT_SHA256_SCHEMA
,
51 #[derive(Serialize,Deserialize)]
52 /// Remote properties.
54 #[serde(skip_serializing_if="Option::is_none")]
55 pub comment
: Option
<String
>,
59 #[serde(skip_serializing_if="Option::is_none")]
60 pub fingerprint
: Option
<String
>,
63 fn init() -> SectionConfig
{
64 let obj_schema
= match Remote
::API_SCHEMA
{
65 Schema
::Object(ref obj_schema
) => obj_schema
,
69 let plugin
= SectionConfigPlugin
::new("remote".to_string(), obj_schema
);
70 let mut config
= SectionConfig
::new(&REMOTE_ID_SCHEMA
);
71 config
.register_plugin(plugin
);
76 pub const REMOTE_CFG_FILENAME
: &str = "/etc/proxmox-backup/remote.cfg";
77 pub const REMOTE_CFG_LOCKFILE
: &str = "/etc/proxmox-backup/.remote.lck";
79 pub fn config() -> Result
<(SectionConfigData
, [u8;32]), Error
> {
80 let content
= match std
::fs
::read_to_string(REMOTE_CFG_FILENAME
) {
83 if err
.kind() == std
::io
::ErrorKind
::NotFound
{
86 bail
!("unable to read '{}' - {}", REMOTE_CFG_FILENAME
, err
);
91 let digest
= openssl
::sha
::sha256(content
.as_bytes());
92 let data
= CONFIG
.parse(REMOTE_CFG_FILENAME
, &content
)?
;
96 pub fn save_config(config
: &SectionConfigData
) -> Result
<(), Error
> {
97 let raw
= CONFIG
.write(REMOTE_CFG_FILENAME
, &config
)?
;
99 let backup_user
= crate::backup
::backup_user()?
;
100 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
101 // set the correct owner/group/permissions while saving file
102 // owner(rw) = root, group(r)= backup
103 let options
= CreateOptions
::new()
105 .owner(nix
::unistd
::ROOT
)
106 .group(backup_user
.gid
);
108 replace_file(REMOTE_CFG_FILENAME
, raw
.as_bytes(), options
)?
;
113 // shell completion helper
114 pub fn complete_remote_name(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
116 Ok((data
, _digest
)) => data
.sections
.iter().map(|(id
, _
)| id
.to_string()).collect(),
117 Err(_
) => return vec
![],