2019-01-23 12:05:32 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
use proxmox::{sortable, identity};
|
2019-08-03 11:05:38 +00:00
|
|
|
use proxmox::tools::fs::{file_get_contents, file_set_contents};
|
2019-08-03 14:44:31 +00:00
|
|
|
use proxmox::tools::*; // required to use IPRE!() macro ???
|
|
|
|
|
2019-01-25 09:15:32 +00:00
|
|
|
use crate::api2::*;
|
2019-05-09 05:44:09 +00:00
|
|
|
use crate::api_schema::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
//use crate::api_schema::router::*;
|
2019-05-09 05:44:09 +00:00
|
|
|
use crate::api2::types::*;
|
2019-01-24 13:59:40 +00:00
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2019-01-25 10:38:59 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-01-25 10:03:41 +00:00
|
|
|
use openssl::sha;
|
2019-01-25 10:38:59 +00:00
|
|
|
use regex::Regex;
|
2019-01-24 13:59:40 +00:00
|
|
|
|
2019-01-23 12:05:32 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2019-01-24 13:59:40 +00:00
|
|
|
static RESOLV_CONF_FN: &str = "/etc/resolv.conf";
|
|
|
|
|
|
|
|
fn read_etc_resolv_conf() -> Result<Value, Error> {
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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);
|
2019-01-24 13:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn update_dns(
|
|
|
|
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-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
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let search = crate::tools::required_string_param(¶m, "search")?;
|
2019-01-25 09:15:32 +00:00
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
let raw = file_get_contents(RESOLV_CONF_FN)?;
|
2019-06-14 09:40:04 +00:00
|
|
|
let old_digest = proxmox::tools::digest_to_hex(&sha::sha256(&raw));
|
2019-01-25 10:38:59 +00:00
|
|
|
|
|
|
|
if let Some(digest) = param["digest"].as_str() {
|
2019-08-03 11:05:38 +00:00
|
|
|
crate::tools::assert_if_modified(&old_digest, &digest)?;
|
2019-01-25 10:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let old_data = String::from_utf8(raw)?;
|
|
|
|
|
2019-01-25 09:15:32 +00:00
|
|
|
let mut data = format!("search {}\n", search);
|
|
|
|
|
|
|
|
for opt in &["dns1", "dns2", "dns3"] {
|
|
|
|
if let Some(server) = param[opt].as_str() {
|
|
|
|
data.push_str(&format!("nameserver {}\n", server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:38:59 +00:00
|
|
|
// append other data
|
|
|
|
lazy_static! {
|
|
|
|
static ref SKIP_REGEX: Regex = Regex::new(r"^(search|domain|nameserver)\s+").unwrap();
|
|
|
|
}
|
|
|
|
for line in old_data.lines() {
|
|
|
|
if SKIP_REGEX.is_match(line) { continue; }
|
|
|
|
data.push_str(line);
|
|
|
|
data.push('\n');
|
|
|
|
}
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
|
2019-01-25 09:15:32 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn get_dns(
|
|
|
|
_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 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&get_dns),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Read DNS settings.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([ ("node", false, &NODE_SCHEMA) ]),
|
2019-01-24 13:59:40 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
).returns(
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Returns DNS server IPs and sreach domain.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("digest", false, &PVE_CONFIG_DIGEST_SCHEMA),
|
|
|
|
("search", true, &SEARCH_DOMAIN_SCHEMA),
|
|
|
|
("dns1", true, &FIRST_DNS_SERVER_SCHEMA),
|
|
|
|
("dns2", true, &SECOND_DNS_SERVER_SCHEMA),
|
|
|
|
("dns3", true, &THIRD_DNS_SERVER_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
).schema()
|
2019-01-25 09:15:32 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
.put(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&update_dns),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Returns DNS server IPs and sreach domain.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("search", false, &SEARCH_DOMAIN_SCHEMA),
|
|
|
|
("dns1", true, &FIRST_DNS_SERVER_SCHEMA),
|
|
|
|
("dns2", true, &SECOND_DNS_SERVER_SCHEMA),
|
|
|
|
("dns3", true, &THIRD_DNS_SERVER_SCHEMA),
|
|
|
|
("digest", true, &PVE_CONFIG_DIGEST_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
).protected(true)
|
|
|
|
);
|