2021-05-03 09:39:53 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2022-01-11 11:39:35 +00:00
|
|
|
use openssl::ssl::{SslAcceptor, SslMethod};
|
2021-05-11 15:33:06 +00:00
|
|
|
use anyhow::{bail, Error};
|
2021-05-03 09:39:53 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_schema::{api, ApiStringFormat, ApiType, Updater};
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-05-17 09:29:24 +00:00
|
|
|
use proxmox_http::ProxyConfig;
|
2021-05-14 13:44:54 +00:00
|
|
|
|
2022-01-11 11:39:35 +00:00
|
|
|
use pbs_api_types::{EMAIL_SCHEMA, OPENSSL_CIPHERS_TLS_1_2_SCHEMA, OPENSSL_CIPHERS_TLS_1_3_SCHEMA};
|
2021-07-06 09:56:35 +00:00
|
|
|
use pbs_buildcfg::configdir;
|
2021-09-02 10:47:11 +00:00
|
|
|
use pbs_config::{open_backup_lockfile, BackupLockGuard};
|
2021-07-06 09:56:35 +00:00
|
|
|
|
2021-05-11 15:35:54 +00:00
|
|
|
use crate::acme::AcmeClient;
|
2021-05-07 10:53:00 +00:00
|
|
|
use crate::api2::types::{
|
2021-05-11 15:35:54 +00:00
|
|
|
AcmeAccountName, AcmeDomain, ACME_DOMAIN_PROPERTY_SCHEMA, HTTP_PROXY_SCHEMA,
|
2021-05-07 10:53:00 +00:00
|
|
|
};
|
2021-05-03 09:39:53 +00:00
|
|
|
|
|
|
|
const CONF_FILE: &str = configdir!("/node.cfg");
|
2021-05-04 07:15:57 +00:00
|
|
|
const LOCK_FILE: &str = configdir!("/.node.lck");
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-07-20 11:51:54 +00:00
|
|
|
pub fn lock() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(LOCK_FILE, None, true)
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the Node Config.
|
|
|
|
pub fn config() -> Result<(NodeConfig, [u8; 32]), Error> {
|
|
|
|
let content =
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::fs::file_read_optional_string(CONF_FILE)?.unwrap_or_else(|| "".to_string());
|
2021-05-03 09:39:53 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data: NodeConfig = crate::tools::config::from_str(&content, &NodeConfig::API_SCHEMA)?;
|
|
|
|
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write the Node Config, requires the write lock to be held.
|
|
|
|
pub fn save_config(config: &NodeConfig) -> Result<(), Error> {
|
|
|
|
config.validate()?;
|
|
|
|
|
|
|
|
let raw = crate::tools::config::to_bytes(config, &NodeConfig::API_SCHEMA)?;
|
2021-09-02 10:47:11 +00:00
|
|
|
pbs_config::replace_backup_config(CONF_FILE, &raw)
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
2021-05-04 09:29:27 +00:00
|
|
|
account: { type: AcmeAccountName },
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
)]
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
/// The ACME configuration.
|
|
|
|
///
|
|
|
|
/// Currently only contains the name of the account use.
|
|
|
|
pub struct AcmeConfig {
|
|
|
|
/// Account to use to acquire ACME certificates.
|
2021-05-04 09:29:27 +00:00
|
|
|
account: AcmeAccountName,
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
|
|
|
acme: {
|
|
|
|
optional: true,
|
|
|
|
type: String,
|
2021-05-04 07:15:57 +00:00
|
|
|
format: &ApiStringFormat::PropertyString(&AcmeConfig::API_SCHEMA),
|
2021-05-03 09:39:53 +00:00
|
|
|
},
|
|
|
|
acmedomain0: {
|
2021-05-04 07:15:57 +00:00
|
|
|
schema: ACME_DOMAIN_PROPERTY_SCHEMA,
|
2021-05-03 09:39:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
acmedomain1: {
|
2021-05-04 07:15:57 +00:00
|
|
|
schema: ACME_DOMAIN_PROPERTY_SCHEMA,
|
2021-05-03 09:39:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
acmedomain2: {
|
2021-05-04 07:15:57 +00:00
|
|
|
schema: ACME_DOMAIN_PROPERTY_SCHEMA,
|
2021-05-03 09:39:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
acmedomain3: {
|
2021-05-04 07:15:57 +00:00
|
|
|
schema: ACME_DOMAIN_PROPERTY_SCHEMA,
|
2021-05-03 09:39:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
acmedomain4: {
|
2021-05-04 07:15:57 +00:00
|
|
|
schema: ACME_DOMAIN_PROPERTY_SCHEMA,
|
2021-05-03 09:39:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
2021-05-07 10:53:00 +00:00
|
|
|
"http-proxy": {
|
|
|
|
schema: HTTP_PROXY_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2022-01-03 09:04:53 +00:00
|
|
|
"email-from": {
|
|
|
|
schema: EMAIL_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2022-01-11 11:39:35 +00:00
|
|
|
"ciphers-tls13": {
|
|
|
|
schema: OPENSSL_CIPHERS_TLS_1_3_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"ciphers-tls12": {
|
|
|
|
schema: OPENSSL_CIPHERS_TLS_1_2_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-05-03 09:39:53 +00:00
|
|
|
},
|
|
|
|
)]
|
|
|
|
#[derive(Deserialize, Serialize, Updater)]
|
2021-05-07 10:53:00 +00:00
|
|
|
#[serde(rename_all = "kebab-case")]
|
2021-05-03 09:39:53 +00:00
|
|
|
/// Node specific configuration.
|
|
|
|
pub struct NodeConfig {
|
|
|
|
/// The acme account to use on this node.
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acme: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acmedomain0: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acmedomain1: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acmedomain2: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acmedomain3: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub acmedomain4: Option<String>,
|
2021-05-07 10:53:00 +00:00
|
|
|
|
2021-08-12 07:30:41 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub http_proxy: Option<String>,
|
2022-01-03 09:04:53 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub email_from: Option<String>,
|
2022-01-11 11:39:35 +00:00
|
|
|
|
|
|
|
/// List of SSL ciphers for tls 1.3 that will be used by the proxy. (Proxy has to be restarted for changes to take effect)
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub ciphers_tls13: Option<String>,
|
|
|
|
|
|
|
|
/// List of SSL ciphers for tls <= 1.2 that will be used by the proxy. (Proxy has to be restarted for changes to take effect)
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub ciphers_tls12: Option<String>,
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeConfig {
|
|
|
|
pub fn acme_config(&self) -> Option<Result<AcmeConfig, Error>> {
|
|
|
|
self.acme.as_deref().map(|config| -> Result<_, Error> {
|
|
|
|
Ok(crate::tools::config::from_property_string(
|
|
|
|
config,
|
|
|
|
&AcmeConfig::API_SCHEMA,
|
|
|
|
)?)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn acme_client(&self) -> Result<AcmeClient, Error> {
|
2021-05-11 15:33:06 +00:00
|
|
|
let account = if let Some(cfg) = self.acme_config().transpose()? {
|
|
|
|
cfg.account
|
|
|
|
} else {
|
|
|
|
AcmeAccountName::from_string("default".to_string())? // should really not happen
|
|
|
|
};
|
|
|
|
AcmeClient::load(&account).await
|
2021-05-03 09:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn acme_domains(&self) -> AcmeDomainIter {
|
|
|
|
AcmeDomainIter::new(self)
|
|
|
|
}
|
|
|
|
|
2021-05-10 11:07:46 +00:00
|
|
|
/// Returns the parsed ProxyConfig
|
2021-05-07 10:53:00 +00:00
|
|
|
pub fn http_proxy(&self) -> Option<ProxyConfig> {
|
|
|
|
if let Some(http_proxy) = &self.http_proxy {
|
2021-12-30 11:57:37 +00:00
|
|
|
match ProxyConfig::parse_proxy_url(http_proxy) {
|
2021-05-07 10:53:00 +00:00
|
|
|
Ok(proxy) => Some(proxy),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 11:07:46 +00:00
|
|
|
/// Sets the HTTP proxy configuration
|
|
|
|
pub fn set_http_proxy(&mut self, http_proxy: Option<String>) {
|
2021-05-07 10:53:00 +00:00
|
|
|
self.http_proxy = http_proxy;
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
/// Validate the configuration.
|
|
|
|
pub fn validate(&self) -> Result<(), Error> {
|
|
|
|
let mut domains = HashSet::new();
|
|
|
|
for domain in self.acme_domains() {
|
|
|
|
let domain = domain?;
|
|
|
|
if !domains.insert(domain.domain.to_lowercase()) {
|
|
|
|
bail!("duplicate domain '{}' in ACME config", domain.domain);
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 11:39:35 +00:00
|
|
|
let mut dummy_acceptor = SslAcceptor::mozilla_intermediate_v5(SslMethod::tls()).unwrap();
|
|
|
|
if let Some(ciphers) = self.ciphers_tls13.as_deref() {
|
|
|
|
dummy_acceptor.set_ciphersuites(ciphers)?;
|
|
|
|
}
|
|
|
|
if let Some(ciphers) = self.ciphers_tls12.as_deref() {
|
|
|
|
dummy_acceptor.set_cipher_list(ciphers)?;
|
|
|
|
}
|
2021-05-03 09:39:53 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AcmeDomainIter<'a> {
|
|
|
|
config: &'a NodeConfig,
|
|
|
|
index: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AcmeDomainIter<'a> {
|
|
|
|
fn new(config: &'a NodeConfig) -> Self {
|
|
|
|
Self { config, index: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for AcmeDomainIter<'a> {
|
|
|
|
type Item = Result<AcmeDomain, Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let domain = loop {
|
|
|
|
let index = self.index;
|
|
|
|
self.index += 1;
|
|
|
|
|
|
|
|
let domain = match index {
|
|
|
|
0 => self.config.acmedomain0.as_deref(),
|
|
|
|
1 => self.config.acmedomain1.as_deref(),
|
|
|
|
2 => self.config.acmedomain2.as_deref(),
|
|
|
|
3 => self.config.acmedomain3.as_deref(),
|
|
|
|
4 => self.config.acmedomain4.as_deref(),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(domain) = domain {
|
|
|
|
break domain;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(crate::tools::config::from_property_string(
|
|
|
|
domain,
|
|
|
|
&AcmeDomain::API_SCHEMA,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|