proxmox-backup/src/api2/node/dns.rs

119 lines
3.4 KiB
Rust
Raw Normal View History

use failure::*;
use crate::tools;
2019-01-25 09:15:32 +00:00
use crate::api2::*;
//use crate::api::schema::*;
//use crate::api::router::*;
use lazy_static::lazy_static;
use std::io::{BufRead, BufReader};
2019-01-25 09:15:32 +00:00
use std::sync::Arc;
use serde_json::{json, Value};
static RESOLV_CONF_FN: &str = "/etc/resolv.conf";
fn read_etc_resolv_conf() -> Result<Value, Error> {
let mut result = json!({});
let mut nscount = 0;
let file = std::fs::File::open(RESOLV_CONF_FN)?;
2019-01-25 09:15:32 +00:00
let reader = BufReader::new(file);
let test = IPRE!();
lazy_static! {
static ref DOMAIN_REGEX: regex::Regex = regex::Regex::new(r"^\s*(?:search|domain)\s+(\S+)\s*").unwrap();
static ref SERVER_REGEX: regex::Regex = regex::Regex::new(
concat!(r"^\s*nameserver\s+(", IPRE!(), r")\s*")).unwrap();
}
for line in reader.lines() {
let line = line?;
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) {
nscount += 1;
if nscount > 3 { continue };
2019-01-25 09:15:32 +00:00
let nameserver = &caps[1];
let id = format!("dns{}", nscount);
2019-01-25 09:15:32 +00:00
result[id] = Value::from(nameserver);
}
}
Ok(result)
}
2019-01-25 09:15:32 +00:00
fn update_dns(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
let search = tools::required_string_param(&param, "search")?;
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));
}
}
tools::file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
Ok(Value::Null)
}
fn get_dns(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
read_etc_resolv_conf()
}
2019-01-25 09:15:32 +00:00
lazy_static! {
pub static ref SEARCH_DOMAIN_SCHEMA: Arc<Schema> =
StringSchema::new("Search domain for host-name lookup.").into();
pub static ref FIRST_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("First name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref SECOND_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Second name server IP address.")
.format(IP_FORMAT.clone()).into();
pub static ref THIRD_DNS_SERVER_SCHEMA: Arc<Schema> =
StringSchema::new("Third name server IP address.")
.format(IP_FORMAT.clone()).into();
}
pub fn router() -> Router {
let route = Router::new()
.get(
ApiMethod::new(
get_dns,
ObjectSchema::new("Read DNS settings.")
).returns(
ObjectSchema::new("Returns DNS server IPs and sreach domain.")
2019-01-25 09:15:32 +00:00
.optional("search", SEARCH_DOMAIN_SCHEMA.clone())
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())
.optional("dns3", THIRD_DNS_SERVER_SCHEMA.clone())
)
2019-01-25 09:15:32 +00:00
)
.put(
ApiMethod::new(
update_dns,
ObjectSchema::new("Returns DNS server IPs and sreach domain.")
.required("search", SEARCH_DOMAIN_SCHEMA.clone())
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())
.optional("dns3", THIRD_DNS_SERVER_SCHEMA.clone())
)
);
route
}