2019-11-21 13:36:28 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{Error};
|
2019-11-21 13:36:28 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use openssl::sha;
|
|
|
|
use regex::Regex;
|
|
|
|
use serde_json::{json, Value};
|
2020-04-26 06:23:23 +00:00
|
|
|
use ::serde::{Deserialize, Serialize};
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
|
2019-12-18 10:05:30 +00:00
|
|
|
use proxmox::tools::fs::{file_get_contents, replace_file, CreateOptions};
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::{IPRE, IPV4RE, IPV6RE, IPV4OCTET, IPV6H16, IPV6LS32};
|
2019-08-03 14:44:31 +00:00
|
|
|
|
2019-05-09 05:44:09 +00:00
|
|
|
use crate::api2::types::*;
|
2020-04-16 08:01:59 +00:00
|
|
|
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
|
2019-01-24 13:59:40 +00:00
|
|
|
|
|
|
|
static RESOLV_CONF_FN: &str = "/etc/resolv.conf";
|
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
#[api()]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
/// Deletable property name
|
|
|
|
pub enum DeletableProperty {
|
|
|
|
/// Delete first nameserver entry
|
|
|
|
dns1,
|
|
|
|
/// Delete second nameserver entry
|
|
|
|
dns2,
|
|
|
|
/// Delete third nameserver entry
|
|
|
|
dns3,
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:35:06 +00:00
|
|
|
pub fn read_etc_resolv_conf() -> Result<Value, Error> {
|
2019-01-24 13:59:40 +00:00
|
|
|
|
|
|
|
let mut result = json!({});
|
|
|
|
|
|
|
|
let mut nscount = 0;
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let raw = file_get_contents(RESOLV_CONF_FN)?;
|
2019-01-24 13:59:40 +00:00
|
|
|
|
2019-06-14 09:40:04 +00:00
|
|
|
result["digest"] = Value::from(proxmox::tools::digest_to_hex(&sha::sha256(&raw)));
|
2019-01-25 10:03:41 +00:00
|
|
|
|
|
|
|
let data = String::from_utf8(raw)?;
|
2019-01-24 13:59:40 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2019-01-25 10:38:59 +00:00
|
|
|
static ref DOMAIN_REGEX: Regex = Regex::new(r"^\s*(?:search|domain)\s+(\S+)\s*").unwrap();
|
|
|
|
static ref SERVER_REGEX: Regex = Regex::new(
|
2019-01-24 13:59:40 +00:00
|
|
|
concat!(r"^\s*nameserver\s+(", IPRE!(), r")\s*")).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
let mut options = String::new();
|
|
|
|
|
2019-01-25 10:03:41 +00:00
|
|
|
for line in data.lines() {
|
2019-01-24 13:59:40 +00:00
|
|
|
|
2019-01-25 09:15:32 +00:00
|
|
|
if let Some(caps) = DOMAIN_REGEX.captures(&line) {
|
|
|
|
result["search"] = Value::from(&caps[1]);
|
|
|
|
} else if let Some(caps) = SERVER_REGEX.captures(&line) {
|
2019-01-24 13:59:40 +00:00
|
|
|
nscount += 1;
|
|
|
|
if nscount > 3 { continue };
|
2019-01-25 09:15:32 +00:00
|
|
|
let nameserver = &caps[1];
|
2019-01-24 13:59:40 +00:00
|
|
|
let id = format!("dns{}", nscount);
|
2019-01-25 09:15:32 +00:00
|
|
|
result[id] = Value::from(nameserver);
|
2020-04-26 06:23:23 +00:00
|
|
|
} else {
|
|
|
|
if !options.is_empty() { options.push('\n'); }
|
|
|
|
options.push_str(line);
|
2019-01-24 13:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
if !options.is_empty() {
|
|
|
|
result["options"] = options.into();
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:59:40 +00:00
|
|
|
Ok(result)
|
|
|
|
}
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2020-04-15 08:09:18 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
description: "Update DNS settings.",
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
search: {
|
|
|
|
schema: SEARCH_DOMAIN_SCHEMA,
|
2020-04-26 06:23:23 +00:00
|
|
|
optional: true,
|
2020-04-15 08:09:18 +00:00
|
|
|
},
|
|
|
|
dns1: {
|
|
|
|
optional: true,
|
|
|
|
schema: FIRST_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
|
|
|
dns2: {
|
|
|
|
optional: true,
|
|
|
|
schema: SECOND_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
|
|
|
dns3: {
|
|
|
|
optional: true,
|
|
|
|
schema: THIRD_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
2020-04-26 06:23:23 +00:00
|
|
|
delete: {
|
|
|
|
description: "List of properties to delete.",
|
|
|
|
type: Array,
|
|
|
|
optional: true,
|
|
|
|
items: {
|
|
|
|
type: DeletableProperty,
|
|
|
|
}
|
|
|
|
},
|
2020-04-15 08:09:18 +00:00
|
|
|
digest: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-16 08:01:59 +00:00
|
|
|
access: {
|
2020-04-30 07:30:00 +00:00
|
|
|
permission: &Permission::Privilege(&["system", "network", "dns"], PRIV_SYS_MODIFY, false),
|
2020-04-16 08:01:59 +00:00
|
|
|
}
|
2020-04-15 08:09:18 +00:00
|
|
|
)]
|
|
|
|
/// Update DNS settings
|
2020-04-26 06:23:23 +00:00
|
|
|
pub fn update_dns(
|
|
|
|
search: Option<String>,
|
|
|
|
dns1: Option<String>,
|
|
|
|
dns2: Option<String>,
|
|
|
|
dns3: Option<String>,
|
|
|
|
delete: Option<Vec<DeletableProperty>>,
|
|
|
|
digest: Option<String>,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2019-01-25 09:15:32 +00:00
|
|
|
|
2019-01-25 10:38:59 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref MUTEX: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:25:37 +00:00
|
|
|
let _guard = MUTEX.lock();
|
2019-01-25 10:38:59 +00:00
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
let mut config = read_etc_resolv_conf()?;
|
|
|
|
let old_digest = config["digest"].as_str().unwrap();
|
2019-01-25 09:15:32 +00:00
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(digest) = digest {
|
|
|
|
crate::tools::assert_if_modified(old_digest, &digest)?;
|
|
|
|
}
|
2019-01-25 10:38:59 +00:00
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(delete) = delete {
|
|
|
|
for delete_prop in delete {
|
|
|
|
let config = config.as_object_mut().unwrap();
|
|
|
|
match delete_prop {
|
|
|
|
DeletableProperty::dns1 => { config.remove("dns1"); },
|
|
|
|
DeletableProperty::dns2 => { config.remove("dns2"); },
|
|
|
|
DeletableProperty::dns3 => { config.remove("dns3"); },
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 10:38:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(search) = search { config["search"] = search.into(); }
|
|
|
|
if let Some(dns1) = dns1 { config["dns1"] = dns1.into(); }
|
|
|
|
if let Some(dns2) = dns2 { config["dns2"] = dns2.into(); }
|
|
|
|
if let Some(dns3) = dns3 { config["dns3"] = dns3.into(); }
|
2019-01-25 10:38:59 +00:00
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
let mut data = String::new();
|
2019-01-25 09:15:32 +00:00
|
|
|
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(search) = config["search"].as_str() {
|
|
|
|
data.push_str(&format!("search {}\n", search));
|
|
|
|
}
|
2019-01-25 09:15:32 +00:00
|
|
|
for opt in &["dns1", "dns2", "dns3"] {
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(server) = config[opt].as_str() {
|
2019-01-25 09:15:32 +00:00
|
|
|
data.push_str(&format!("nameserver {}\n", server));
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 06:23:23 +00:00
|
|
|
if let Some(options) = config["options"].as_str() {
|
|
|
|
data.push_str(options);
|
2019-01-25 10:38:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 10:05:30 +00:00
|
|
|
replace_file(RESOLV_CONF_FN, data.as_bytes(), CreateOptions::new())?;
|
2019-01-25 09:15:32 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-04-15 08:09:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "Returns DNS server IPs and sreach domain.",
|
|
|
|
type: Object,
|
|
|
|
properties: {
|
|
|
|
digest: {
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
|
|
|
search: {
|
|
|
|
optional: true,
|
|
|
|
schema: SEARCH_DOMAIN_SCHEMA,
|
|
|
|
},
|
|
|
|
dns1: {
|
|
|
|
optional: true,
|
|
|
|
schema: FIRST_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
|
|
|
dns2: {
|
|
|
|
optional: true,
|
|
|
|
schema: SECOND_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
|
|
|
dns3: {
|
|
|
|
optional: true,
|
|
|
|
schema: THIRD_DNS_SERVER_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-16 08:01:59 +00:00
|
|
|
access: {
|
2020-04-30 07:30:00 +00:00
|
|
|
permission: &Permission::Privilege(&["system", "network", "dns"], PRIV_SYS_AUDIT, false),
|
2020-04-16 08:01:59 +00:00
|
|
|
}
|
2020-04-15 08:09:18 +00:00
|
|
|
)]
|
|
|
|
/// Read DNS settings.
|
2020-04-26 06:23:23 +00:00
|
|
|
pub fn get_dns(
|
2019-01-26 13:50:37 +00:00
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2019-01-24 13:59:40 +00:00
|
|
|
read_etc_resolv_conf()
|
2019-01-23 12:05:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-04-15 08:09:18 +00:00
|
|
|
.get(&API_METHOD_GET_DNS)
|
|
|
|
.put(&API_METHOD_UPDATE_DNS);
|