From 50640d859ef1cb7ad8f6b14d1e3848cae0d82e65 Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Mon, 15 Jul 2024 22:17:33 +0200 Subject: [PATCH] fix case sensitive and better error logging --- src/db/models.rs | 11 ++++++++++- src/errors.rs | 10 +++++----- src/handlers/query.rs | 3 ++- src/handlers/update/mod.rs | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/db/models.rs b/src/db/models.rs index f71de9e..32b0383 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -3,6 +3,7 @@ use crate::{ structs::{Class, Type, RR}, }; use diesel::prelude::*; +use diesel::sql_types::Text; use self::schema::records::{self}; @@ -31,6 +32,10 @@ struct Record { pub rdata: Vec, } +sql_function! { + fn lower(x: Text) -> Text; +} + impl Record { pub fn get( db: &mut PgConnection, @@ -40,7 +45,11 @@ impl Record { ) -> Result, diesel::result::Error> { let mut query = records::table.into_boxed(); - query = query.filter(records::name.eq(name).and(records::class.eq(class))); + query = query.filter( + lower(records::name) + .eq(name.to_lowercase()) + .and(records::class.eq(class)), + ); if let Some(value) = _type { query = query.filter(records::_type.eq(value)) diff --git a/src/errors.rs b/src/errors.rs index d61b4a3..3179abc 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,6 @@ use thiserror::Error; -use crate::structs::RCODE; +use crate::structs::{Type, RCODE}; #[derive(Error, Debug)] pub enum ZNSError { @@ -17,8 +17,8 @@ pub enum ZNSError { #[error("DNS Query Format Error: {message:?}")] Formerr { message: String }, - #[error("Domain name does not exist")] - NXDomain { domain: String }, + #[error("Domain name does not exist: {qtype:?} {domain:?}")] + NXDomain { domain: String, qtype: Type }, #[error("NotImplemented Error for {object:?}: {message:?}")] NotImp { object: String, message: String }, #[error("Authentication Error: {message:?}")] @@ -35,10 +35,10 @@ impl ZNSError { } ZNSError::Database { .. } | ZNSError::Reqwest(_) => RCODE::SERVFAIL, - ZNSError::NotAuth { .. } | ZNSError::PublicKey { .. } => RCODE::NOTAUTH, + ZNSError::NotAuth { .. } => RCODE::NOTAUTH, ZNSError::NXDomain { .. } => RCODE::NXDOMAIN, ZNSError::NotImp { .. } => RCODE::NOTIMP, - ZNSError::Refused { .. } => RCODE::REFUSED, + ZNSError::Refused { .. } | ZNSError::PublicKey { .. } => RCODE::REFUSED, } } } diff --git a/src/handlers/query.rs b/src/handlers/query.rs index 9ec7179..f372cc7 100644 --- a/src/handlers/query.rs +++ b/src/handlers/query.rs @@ -34,6 +34,7 @@ impl ResponseHandler for QueryHandler { if rrs.len() == 0 { return Err(ZNSError::NXDomain { domain: question.qname.join("."), + qtype: question.qtype.clone(), }); } } @@ -55,7 +56,7 @@ impl ResponseHandler for QueryHandler { fn try_wildcard(question: &Question, connection: &mut PgConnection) -> Result, ZNSError> { let records = get_from_database(&question.qname, None, question.qclass.clone(), connection)?; - if records.len() > 0 { + if records.len() > 0 || question.qname.len() == 0 { Ok(vec![]) } else { let mut qname = question.qname.clone(); diff --git a/src/handlers/update/mod.rs b/src/handlers/update/mod.rs index 55a4cf4..c43527c 100644 --- a/src/handlers/update/mod.rs +++ b/src/handlers/update/mod.rs @@ -57,12 +57,12 @@ impl ResponseHandler for UpdateHandler { .await .is_ok_and(|x| x) { - return Err(ZNSError::NotAuth { + return Err(ZNSError::Refused { message: "Unable to verify authentication".to_string(), }); } } else { - return Err(ZNSError::NotAuth { + return Err(ZNSError::Refused { message: "No KEY record at the end of request found".to_string(), }); }