Reformatting

This commit is contained in:
notplants 2021-05-25 16:14:29 +02:00
parent a45b21d28e
commit 7a0a7b720d
4 changed files with 44 additions and 35 deletions

View File

@ -1,4 +1,3 @@
// this regex is used to validate that domains are in the correct format // this regex is used to validate that domains are in the correct format
// e.g. blue.dyn.peachcloud.org // e.g. blue.dyn.peachcloud.org
pub const DOMAIN_REGEX: &str = r"^.*\.dyn\.peachcloud\.org$"; pub const DOMAIN_REGEX: &str = r"^.*\.dyn\.peachcloud\.org$";

View File

@ -9,7 +9,6 @@ pub type BoxError = Box<dyn error::Error>;
#[derive(Debug, Snafu)] #[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))] #[snafu(visibility(pub(crate)))]
pub enum PeachDynDnsError { pub enum PeachDynDnsError {
#[snafu(display("Missing expected parameters: {}", e))] #[snafu(display("Missing expected parameters: {}", e))]
MissingParams { e: Error }, MissingParams { e: Error },
@ -26,16 +25,14 @@ pub enum PeachDynDnsError {
KeyFileParseError { source: FromUtf8Error }, KeyFileParseError { source: FromUtf8Error },
#[snafu(display("Error generating key: {}", source))] #[snafu(display("Error generating key: {}", source))]
KeyGenerationError { source: std::io::Error } KeyGenerationError { source: std::io::Error },
} }
impl From<PeachDynDnsError> for Error { impl From<PeachDynDnsError> for Error {
fn from(err: PeachDynDnsError) -> Self { fn from(err: PeachDynDnsError) -> Self {
match &err { match &err {
PeachDynDnsError::MissingParams { e } => e.clone(), PeachDynDnsError::MissingParams { e } => e.clone(),
PeachDynDnsError::InvalidDomain { domain} => Error { PeachDynDnsError::InvalidDomain { domain } => Error {
code: ErrorCode::ServerError(-32028), code: ErrorCode::ServerError(-32028),
message: format!("Domain is invalid format: {}", domain), message: format!("Domain is invalid format: {}", domain),
data: None, data: None,
@ -45,21 +42,21 @@ impl From<PeachDynDnsError> for Error {
message: "There was a bind configuration error".to_string(), message: "There was a bind configuration error".to_string(),
data: None, data: None,
}, },
PeachDynDnsError::DomainAlreadyExistsError { domain} => Error { PeachDynDnsError::DomainAlreadyExistsError { domain } => Error {
code: ErrorCode::ServerError(-32030), code: ErrorCode::ServerError(-32030),
message: format!("Can't register domain that already exists: {}", domain), message: format!("Can't register domain that already exists: {}", domain),
data: None, data: None,
}, },
PeachDynDnsError::KeyFileParseError { source: _} => Error { PeachDynDnsError::KeyFileParseError { source: _ } => Error {
code: ErrorCode::ServerError(-32031), code: ErrorCode::ServerError(-32031),
message: "Error parsing key file".to_string(), message: "Error parsing key file".to_string(),
data: None, data: None,
}, },
PeachDynDnsError::KeyGenerationError { source: _} => Error { PeachDynDnsError::KeyGenerationError { source: _ } => Error {
code: ErrorCode::ServerError(-32032), code: ErrorCode::ServerError(-32032),
message: "Key generation error".to_string(), message: "Key generation error".to_string(),
data: None, data: None,
}, },
} }
} }
} }

View File

@ -2,16 +2,15 @@
* Functions for generating bind9 configurations to enable dynamic dns for a subdomain via TSIG authentication * Functions for generating bind9 configurations to enable dynamic dns for a subdomain via TSIG authentication
* which is unique to that subdomain * which is unique to that subdomain
*/ */
use crate::constants::DOMAIN_REGEX;
use crate::errors::*;
use log::{error, info};
use snafu::ResultExt;
use std::fs::File; use std::fs::File;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::process::Command; use std::process::Command;
use tera::{Tera, Context}; use tera::{Context, Tera};
use snafu::{ResultExt};
use log::{info, error};
use crate::errors::*;
use crate::constants::DOMAIN_REGEX;
/// function to generate the text of a TSIG key file /// function to generate the text of a TSIG key file
pub fn generate_tsig_key(full_domain: &str) -> Result<String, PeachDynDnsError> { pub fn generate_tsig_key(full_domain: &str) -> Result<String, PeachDynDnsError> {
@ -19,7 +18,8 @@ pub fn generate_tsig_key(full_domain: &str) -> Result<String, PeachDynDnsError>
.arg("-a") .arg("-a")
.arg("hmac-md5") .arg("hmac-md5")
.arg(full_domain) .arg(full_domain)
.output().context(KeyGenerationError)?; .output()
.context(KeyGenerationError)?;
let key_file_text = String::from_utf8(output.stdout).context(KeyFileParseError)?; let key_file_text = String::from_utf8(output.stdout).context(KeyFileParseError)?;
Ok(key_file_text) Ok(key_file_text)
} }
@ -40,12 +40,14 @@ pub fn check_domain_available(full_domain: &str) -> bool {
let status1 = Command::new("/bin/grep") let status1 = Command::new("/bin/grep")
.arg(full_domain) .arg(full_domain)
.arg("/etc/bind/named.conf.local") .arg("/etc/bind/named.conf.local")
.status().expect("error running grep on /etc/bind/named.conf.local"); .status()
.expect("error running grep on /etc/bind/named.conf.local");
let code1 = status1.code().expect("error getting code from grep"); let code1 = status1.code().expect("error getting code from grep");
let status2 = Command::new("/bin/grep") let status2 = Command::new("/bin/grep")
.arg(full_domain) .arg(full_domain)
.arg("/etc/bind/dyn.peachcloud.org.keys") .arg("/etc/bind/dyn.peachcloud.org.keys")
.status().expect("error running grep on /etc/bind/dyn.peachcloud.org.keys"); .status()
.expect("error running grep on /etc/bind/dyn.peachcloud.org.keys");
let code2 = status2.code().expect("error getting code from grep"); let code2 = status2.code().expect("error getting code from grep");
let condition3 = std::path::Path::new(&format!("/var/lib/bind/{}", full_domain)).exists(); let condition3 = std::path::Path::new(&format!("/var/lib/bind/{}", full_domain)).exists();
@ -64,16 +66,19 @@ pub fn check_domain_available(full_domain: &str) -> bool {
/// - add a minimal zone file to /var/lib/bind/subdomain.dyn.peachcloud.org /// - add a minimal zone file to /var/lib/bind/subdomain.dyn.peachcloud.org
/// - reload bind and return the secret key to the client /// - reload bind and return the secret key to the client
pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> { pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
// first safety check domain is in correct format // first safety check domain is in correct format
if !validate_domain(full_domain) { if !validate_domain(full_domain) {
return Err(PeachDynDnsError::InvalidDomain{ domain: full_domain.to_string() }); return Err(PeachDynDnsError::InvalidDomain {
domain: full_domain.to_string(),
});
} }
// safety check if the domain is available // safety check if the domain is available
let is_available = check_domain_available(full_domain); let is_available = check_domain_available(full_domain);
if !is_available { if !is_available {
return Err(PeachDynDnsError::DomainAlreadyExistsError { domain: full_domain.to_string() }); return Err(PeachDynDnsError::DomainAlreadyExistsError {
domain: full_domain.to_string(),
});
} }
// generate string with text for TSIG key file // generate string with text for TSIG key file
@ -81,7 +86,9 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
// append key_file_text to /etc/bind/dyn.peachcloud.org.keys // append key_file_text to /etc/bind/dyn.peachcloud.org.keys
let key_file_path = "/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) let mut file = OpenOptions::new()
.append(true)
.open(key_file_path)
.unwrap_or_else(|_| panic!("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) { if let Err(e) = writeln!(file, "{}", key_file_text) {
error!("Couldn't write to file: {}", e); error!("Couldn't write to file: {}", e);
@ -105,7 +112,8 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
", ",
full_domain = full_domain full_domain = full_domain
); );
writeln!(file, "{}", zone_section_text).unwrap_or_else(|_| panic!("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 // use tera to render the zone file
let tera = match Tera::new("templates/*.tera") { let tera = match Tera::new("templates/*.tera") {
@ -117,22 +125,26 @@ pub fn generate_zone(full_domain: &str) -> Result<String, PeachDynDnsError> {
}; };
let mut context = Context::new(); let mut context = Context::new();
context.insert("full_domain", full_domain); context.insert("full_domain", full_domain);
let result = tera.render("zonefile.tera", &context).expect("error loading zonefile.tera"); let result = tera
.render("zonefile.tera", &context)
.expect("error loading zonefile.tera");
// write new zone file to /var/lib/bind // write new zone file to /var/lib/bind
let zone_file_path = format!("/var/lib/bind/{}", full_domain); let zone_file_path = format!("/var/lib/bind/{}", full_domain);
let mut file = File::create(&zone_file_path) let mut file = File::create(&zone_file_path)
.unwrap_or_else(|_| panic!("failed to create {}", 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)); writeln!(file, "{}", result)
.unwrap_or_else(|_| panic!("Couldn't write to file: {}", zone_file_path));
// restart bind // restart bind
// we use the /etc/sudoers.d/bindctl to allow peach-dyndns user to restart bind as sudo without entering a password // we use the /etc/sudoers.d/bindctl to allow peach-dyndns user to restart bind as sudo without entering a password
// using a binary at /bin/reloadbind which runs 'systemctl reload bind9' // using a binary at /bin/reloadbind which runs 'systemctl reload bind9'
let status = Command::new("sudo") let status = Command::new("sudo")
.arg("/usr/bin/reloadbind") .arg("/usr/bin/reloadbind")
.status().expect("error restarting bind9"); .status()
.expect("error restarting bind9");
if !status.success() { if !status.success() {
return Err(PeachDynDnsError::BindConfigurationError); return Err(PeachDynDnsError::BindConfigurationError);
// TODO: for extra safety consider to revert bind configurations to whatever they were before // TODO: for extra safety consider to revert bind configurations to whatever they were before
} }

View File

@ -5,19 +5,18 @@
* NOT IMPLEMENTED- register_user (sends an email verification to create a new account) * NOT IMPLEMENTED- register_user (sends an email verification to create a new account)
* NOT IMPLEMENTED- verify_user (for clicking the link in the email) * NOT IMPLEMENTED- verify_user (for clicking the link in the email)
*/ */
mod constants;
mod errors; mod errors;
mod generate_zone; mod generate_zone;
mod constants;
use crate::generate_zone::{check_domain_available, generate_zone, validate_domain}; use crate::generate_zone::{check_domain_available, generate_zone, validate_domain};
use std::result::Result;
use jsonrpc_core::{types::error::Error, IoHandler, Params, Value}; use jsonrpc_core::{types::error::Error, IoHandler, Params, Value};
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder}; use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
use log::info; use log::info;
use std::env; use std::env;
use std::result::Result;
use crate::errors::{BoxError, PeachDynDnsError}; use crate::errors::{BoxError, PeachDynDnsError};
use serde::{Deserialize}; use serde::Deserialize;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct RegisterDomainPost { pub struct RegisterDomainPost {
@ -56,14 +55,16 @@ pub fn run() -> Result<(), BoxError> {
Ok(d) => { Ok(d) => {
// if the domain has an invalid format return an erro // if the domain has an invalid format return an erro
if !validate_domain(&d.domain) { if !validate_domain(&d.domain) {
Err(Error::from(PeachDynDnsError::InvalidDomain{ domain: d.domain })) Err(Error::from(PeachDynDnsError::InvalidDomain {
domain: d.domain,
}))
} }
// if it has a valid format, check if its available // if it has a valid format, check if its available
else { else {
let result = check_domain_available(&d.domain); let result = check_domain_available(&d.domain);
Ok(Value::String(result.to_string())) Ok(Value::String(result.to_string()))
} }
}, }
Err(e) => Err(Error::from(PeachDynDnsError::MissingParams { e })), Err(e) => Err(Error::from(PeachDynDnsError::MissingParams { e })),
} }
}); });