2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-01-09 13:49:40 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::collections::HashMap;
|
2020-01-10 12:28:15 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-01-09 13:49:40 +00:00
|
|
|
|
2020-03-02 11:52:11 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api,
|
|
|
|
schema::*,
|
|
|
|
section_config::{
|
|
|
|
SectionConfig,
|
|
|
|
SectionConfigData,
|
|
|
|
SectionConfigPlugin,
|
|
|
|
}
|
|
|
|
};
|
2020-01-09 13:49:40 +00:00
|
|
|
|
|
|
|
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
|
|
|
|
|
2020-01-13 10:47:07 +00:00
|
|
|
use crate::api2::types::*;
|
2020-01-09 13:49:40 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:17:22 +00:00
|
|
|
pub const REMOTE_PASSWORD_SCHEMA: Schema = StringSchema::new("Password or auth token for remote host.")
|
2020-01-14 10:32:02 +00:00
|
|
|
.format(&PASSWORD_FORMAT)
|
2020-01-14 10:22:42 +00:00
|
|
|
.min_length(1)
|
2020-01-13 13:17:22 +00:00
|
|
|
.max_length(1024)
|
|
|
|
.schema();
|
2020-01-09 13:49:40 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
2020-05-22 12:51:40 +00:00
|
|
|
name: {
|
|
|
|
schema: REMOTE_ID_SCHEMA,
|
|
|
|
},
|
2020-01-09 13:49:40 +00:00
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-09 13:49:40 +00:00
|
|
|
},
|
|
|
|
host: {
|
2020-01-13 13:17:22 +00:00
|
|
|
schema: DNS_NAME_OR_IP_SCHEMA,
|
2020-01-09 13:49:40 +00:00
|
|
|
},
|
|
|
|
userid: {
|
2020-01-13 13:17:22 +00:00
|
|
|
schema: PROXMOX_USER_ID_SCHEMA,
|
2020-01-09 13:49:40 +00:00
|
|
|
},
|
|
|
|
password: {
|
|
|
|
schema: REMOTE_PASSWORD_SCHEMA,
|
|
|
|
},
|
2020-01-25 08:48:39 +00:00
|
|
|
fingerprint: {
|
|
|
|
optional: true,
|
|
|
|
schema: CERT_FINGERPRINT_SHA256_SCHEMA,
|
|
|
|
},
|
2020-01-09 13:49:40 +00:00
|
|
|
}
|
|
|
|
)]
|
2020-01-10 12:28:15 +00:00
|
|
|
#[derive(Serialize,Deserialize)]
|
2020-01-09 13:49:40 +00:00
|
|
|
/// Remote properties.
|
|
|
|
pub struct Remote {
|
2020-05-22 12:51:40 +00:00
|
|
|
pub name: String,
|
2020-01-13 11:14:14 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-01-09 13:49:40 +00:00
|
|
|
pub comment: Option<String>,
|
|
|
|
pub host: String,
|
|
|
|
pub userid: String,
|
2020-05-22 12:51:45 +00:00
|
|
|
#[serde(skip_serializing_if="String::is_empty")]
|
2020-05-26 10:23:24 +00:00
|
|
|
#[serde(with = "proxmox::tools::serde::string_as_base64")]
|
2020-01-09 13:49:40 +00:00
|
|
|
pub password: String,
|
2020-01-31 08:09:24 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-01-25 08:48:39 +00:00
|
|
|
pub fingerprint: Option<String>,
|
2020-01-09 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
let obj_schema = match Remote::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2020-05-22 12:51:40 +00:00
|
|
|
let plugin = SectionConfigPlugin::new("remote".to_string(), Some("name".to_string()), obj_schema);
|
2020-01-09 13:49:40 +00:00
|
|
|
let mut config = SectionConfig::new(&REMOTE_ID_SCHEMA);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
pub const REMOTE_CFG_FILENAME: &str = "/etc/proxmox-backup/remote.cfg";
|
|
|
|
pub const REMOTE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.remote.lck";
|
2020-01-09 13:49:40 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-01-16 13:32:06 +00:00
|
|
|
let content = match std::fs::read_to_string(REMOTE_CFG_FILENAME) {
|
2020-01-09 13:49:40 +00:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
String::from("")
|
|
|
|
} else {
|
2020-01-16 13:32:06 +00:00
|
|
|
bail!("unable to read '{}' - {}", REMOTE_CFG_FILENAME, err);
|
2020-01-09 13:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
2020-01-16 13:32:06 +00:00
|
|
|
let data = CONFIG.parse(REMOTE_CFG_FILENAME, &content)?;
|
2020-01-14 11:57:03 +00:00
|
|
|
Ok((data, digest))
|
2020-01-09 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
2020-01-16 13:32:06 +00:00
|
|
|
let raw = CONFIG.write(REMOTE_CFG_FILENAME, &config)?;
|
2020-01-09 13:49:40 +00:00
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)= backup
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
replace_file(REMOTE_CFG_FILENAME, raw.as_bytes(), options)?;
|
2020-01-09 13:49:40 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_remote_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
2020-01-14 11:57:03 +00:00
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
2020-01-09 13:49:40 +00:00
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|