]> git.proxmox.com Git - proxmox-backup.git/blob - src/config/remote.rs
src/config/acl.rs: implement cached_config
[proxmox-backup.git] / src / config / remote.rs
1 use failure::*;
2 use lazy_static::lazy_static;
3 use std::collections::HashMap;
4 use serde::{Serialize, Deserialize};
5
6 use proxmox::api::{
7 api,
8 schema::*,
9 section_config::{
10 SectionConfig,
11 SectionConfigData,
12 SectionConfigPlugin,
13 }
14 };
15
16 use proxmox::tools::{fs::replace_file, fs::CreateOptions};
17
18 use crate::api2::types::*;
19
20 lazy_static! {
21 static ref CONFIG: SectionConfig = init();
22 }
23
24 pub const REMOTE_PASSWORD_SCHEMA: Schema = StringSchema::new("Password or auth token for remote host.")
25 .format(&PASSWORD_FORMAT)
26 .min_length(1)
27 .max_length(1024)
28 .schema();
29
30 #[api(
31 properties: {
32 comment: {
33 optional: true,
34 schema: SINGLE_LINE_COMMENT_SCHEMA,
35 },
36 host: {
37 schema: DNS_NAME_OR_IP_SCHEMA,
38 },
39 userid: {
40 schema: PROXMOX_USER_ID_SCHEMA,
41 },
42 password: {
43 schema: REMOTE_PASSWORD_SCHEMA,
44 },
45 fingerprint: {
46 optional: true,
47 schema: CERT_FINGERPRINT_SHA256_SCHEMA,
48 },
49 }
50 )]
51 #[derive(Serialize,Deserialize)]
52 /// Remote properties.
53 pub struct Remote {
54 #[serde(skip_serializing_if="Option::is_none")]
55 pub comment: Option<String>,
56 pub host: String,
57 pub userid: String,
58 pub password: String,
59 #[serde(skip_serializing_if="Option::is_none")]
60 pub fingerprint: Option<String>,
61 }
62
63 fn init() -> SectionConfig {
64 let obj_schema = match Remote::API_SCHEMA {
65 Schema::Object(ref obj_schema) => obj_schema,
66 _ => unreachable!(),
67 };
68
69 let plugin = SectionConfigPlugin::new("remote".to_string(), obj_schema);
70 let mut config = SectionConfig::new(&REMOTE_ID_SCHEMA);
71 config.register_plugin(plugin);
72
73 config
74 }
75
76 pub const REMOTE_CFG_FILENAME: &str = "/etc/proxmox-backup/remote.cfg";
77 pub const REMOTE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.remote.lck";
78
79 pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
80 let content = match std::fs::read_to_string(REMOTE_CFG_FILENAME) {
81 Ok(c) => c,
82 Err(err) => {
83 if err.kind() == std::io::ErrorKind::NotFound {
84 String::from("")
85 } else {
86 bail!("unable to read '{}' - {}", REMOTE_CFG_FILENAME, err);
87 }
88 }
89 };
90
91 let digest = openssl::sha::sha256(content.as_bytes());
92 let data = CONFIG.parse(REMOTE_CFG_FILENAME, &content)?;
93 Ok((data, digest))
94 }
95
96 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
97 let raw = CONFIG.write(REMOTE_CFG_FILENAME, &config)?;
98
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()
104 .perm(mode)
105 .owner(nix::unistd::ROOT)
106 .group(backup_user.gid);
107
108 replace_file(REMOTE_CFG_FILENAME, raw.as_bytes(), options)?;
109
110 Ok(())
111 }
112
113 // shell completion helper
114 pub fn complete_remote_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
115 match config() {
116 Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
117 Err(_) => return vec![],
118 }
119 }