This commit is contained in:
notplants 2021-05-25 16:12:12 +02:00
parent a11c146ec5
commit a3781b5b21
3 changed files with 11 additions and 37 deletions

View File

@ -42,7 +42,7 @@ impl From<PeachDynDnsError> for Error {
},
PeachDynDnsError::BindConfigurationError => Error {
code: ErrorCode::ServerError(-32029),
message: format!("There was a bind configuration error"),
message: "There was a bind configuration error".to_string(),
data: None,
},
PeachDynDnsError::DomainAlreadyExistsError { domain} => Error {
@ -52,36 +52,14 @@ impl From<PeachDynDnsError> for Error {
},
PeachDynDnsError::KeyFileParseError { source: _} => Error {
code: ErrorCode::ServerError(-32031),
message: format!("Error parsing key file"),
message: "Error parsing key file".to_string(),
data: None,
},
PeachDynDnsError::KeyGenerationError { source: _} => Error {
code: ErrorCode::ServerError(-32032),
message: format!("Key generation error"),
message: "Key generation error".to_string(),
data: None,
},
}
}
}
//#[derive(Debug)]
//pub enum PeachDynError {
// GenerateTsigIoError(std::io::Error),
// GenerateTsigParseError(std::string::FromUtf8Error),
// DomainAlreadyExistsError(String),
// BindConfigurationError(String),
// InvalidDomainError(String)
// NetworkError::MissingParams { e } => e.clone(),
//}
//
//impl From<std::io::Error> for PeachDynError {
// fn from(err: std::io::Error) -> PeachDynError {
// PeachDynError::GenerateTsigIoError(err)
// }
//}
//
//impl From<FromUtf8Error> for PeachDynError {
// fn from(err: std::string::FromUtf8Error) -> PeachDynError {
// PeachDynError::GenerateTsigParseError(err)
// }
//}
}

View File

@ -52,11 +52,7 @@ pub fn check_domain_available(full_domain: &str) -> bool {
// domain is only available if domain does not exist in either named.conf.local or dyn.peachcloud.orgkeys
// and a file with that name is not found in /var/lib/bind/
// grep returns a status code of 1 if lines are not found, which is why we check that the codes equal 1
let domain_available = (code1 == 1) & (code2 == 1) & (!condition3);
// return
domain_available
(code1 == 1) & (code2 == 1) & (!condition3)
}
/// function which generates all necessary bind configuration to serve the given
@ -86,7 +82,7 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
// append key_file_text to /etc/bind/dyn.peachcloud.org.keys
let key_file_path = "/etc/bind/dyn.peachcloud.org.keys";
let mut file = OpenOptions::new().append(true).open(key_file_path)
.expect(&format!("failed to open {}", key_file_path));
.unwrap_or_else(|_| panic!("failed to open {}", key_file_path));
if let Err(e) = writeln!(file, "{}", key_file_text) {
error!("Couldn't write to file: {}", e);
}
@ -96,7 +92,7 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
let mut file = OpenOptions::new()
.append(true)
.open(bind_conf_path)
.expect(&format!("failed to open {}", bind_conf_path));
.unwrap_or_else(|_| panic!("failed to open {}", bind_conf_path));
let zone_section_text = format!(
"\
zone \"{full_domain}\" {{
@ -109,7 +105,7 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
",
full_domain = full_domain
);
writeln!(file, "{}", zone_section_text).expect(&format!("Couldn't write to file: {}", bind_conf_path));
writeln!(file, "{}", zone_section_text).unwrap_or_else(|_| panic!("Couldn't write to file: {}", bind_conf_path));
// use tera to render the zone file
let tera = match Tera::new("templates/*.tera") {
@ -126,8 +122,8 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
// write new zone file to /var/lib/bind
let zone_file_path = format!("/var/lib/bind/{}", full_domain);
let mut file = File::create(&zone_file_path)
.expect(&format!("failed to create {}", zone_file_path));
writeln!(file, "{}", result).expect(&format!("Couldn't write to file: {}", zone_file_path));
.unwrap_or_else(|_| panic!("failed to create {}", zone_file_path));
writeln!(file, "{}", result).unwrap_or_else(|_| panic!("Couldn't write to file: {}", zone_file_path));
// restart bind
// we use the /etc/sudoers.d/bindctl to allow peach-dyndns user to restart bind as sudo without entering a password

View File

@ -56,7 +56,7 @@ pub fn run() -> Result<(), BoxError> {
Ok(d) => {
// if the domain has an invalid format return an erro
if !validate_domain(&d.domain) {
Err(Error::from(PeachDynDnsError::InvalidDomain{ domain: d.domain.to_string() }))
Err(Error::from(PeachDynDnsError::InvalidDomain{ domain: d.domain }))
}
// if it has a valid format, check if its available
else {