use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, PoisonError};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::error::Result;
const SPF_LOOKUP_LIMIT: usize = 10;
#[async_trait]
pub trait TxtLookup: Send + Sync {
async fn txt(&self, name: &str) -> Result<Vec<String>>;
}
#[derive(Debug, Default)]
pub struct StaticTxt {
records: Mutex<BTreeMap<String, Vec<String>>>,
}
impl StaticTxt {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with(self, name: &str, text: &str) -> Self {
self.insert(name, text);
self
}
pub fn insert(&self, name: &str, text: &str) {
self.records
.lock()
.unwrap_or_else(PoisonError::into_inner)
.entry(lookup_key(name))
.or_default()
.push(text.to_string());
}
pub fn clear(&self, name: &str) {
self.records.lock().unwrap_or_else(PoisonError::into_inner).remove(&lookup_key(name));
}
}
fn lookup_key(name: &str) -> String {
name.trim().trim_end_matches('.').to_ascii_lowercase()
}
#[async_trait]
impl TxtLookup for StaticTxt {
async fn txt(&self, name: &str) -> Result<Vec<String>> {
Ok(self
.records
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(&lookup_key(name))
.cloned()
.unwrap_or_default())
}
}
#[cfg(feature = "system-dns")]
#[derive(Debug, Clone, Default)]
pub struct SystemTxtLookup {
resolver: Arc<tokio::sync::OnceCell<hickory_resolver::TokioResolver>>,
}
#[cfg(feature = "system-dns")]
impl SystemTxtLookup {
pub fn new() -> Result<Self> {
Ok(Self::default())
}
}
#[cfg(feature = "system-dns")]
#[async_trait]
impl TxtLookup for SystemTxtLookup {
async fn txt(&self, name: &str) -> Result<Vec<String>> {
let resolver = self
.resolver
.get_or_try_init(|| async {
hickory_resolver::TokioResolver::builder_tokio()
.map_err(|error| crate::DriverError::unavailable("dns", error))?
.build()
.map_err(|error| crate::DriverError::unavailable("dns", error))
})
.await?;
match resolver.txt_lookup(name).await {
Ok(lookup) => Ok(lookup
.answers()
.iter()
.filter_map(|record| match &record.data {
hickory_resolver::proto::rr::RData::TXT(txt) => Some(
txt.txt_data
.iter()
.map(|part| String::from_utf8_lossy(part))
.collect::<String>(),
),
_ => None,
})
.collect()),
Err(error) if error.is_no_records_found() || error.is_nx_domain() => Ok(Vec::new()),
Err(error) => Err(crate::DriverError::unavailable("dns", error)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SenderPolicy {
pub domain: String,
pub dkim_selector: String,
pub spf_mechanism: String,
}
impl SenderPolicy {
pub fn new(
domain: impl Into<String>,
dkim_selector: impl Into<String>,
spf_mechanism: impl Into<String>,
) -> Self {
Self {
domain: lookup_key(&domain.into()),
dkim_selector: dkim_selector.into().trim().to_string(),
spf_mechanism: spf_mechanism.into().trim().to_ascii_lowercase(),
}
}
pub fn dkim_name(&self) -> String {
format!("{}._domainkey.{}", self.dkim_selector, self.domain)
}
pub fn dmarc_name(&self) -> String {
format!("_dmarc.{}", self.domain)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AuthCheck {
pub pass: bool,
pub detail: String,
}
impl AuthCheck {
fn pass(detail: impl Into<String>) -> Self {
Self { pass: true, detail: detail.into() }
}
fn fail(detail: impl Into<String>) -> Self {
Self { pass: false, detail: detail.into() }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DomainAuthReport {
pub domain: String,
pub spf: AuthCheck,
pub dkim: AuthCheck,
pub dmarc: AuthCheck,
pub checked_at: DateTime<Utc>,
}
impl DomainAuthReport {
pub fn passes(&self) -> bool {
self.spf.pass && self.dkim.pass && self.dmarc.pass
}
pub fn failures(&self) -> Vec<String> {
[("SPF", &self.spf), ("DKIM", &self.dkim), ("DMARC", &self.dmarc)]
.into_iter()
.filter(|(_, check)| !check.pass)
.map(|(name, check)| format!("{name}: {}", check.detail))
.collect()
}
}
#[derive(Clone)]
pub struct DomainAuthCheck {
lookup: Arc<dyn TxtLookup>,
}
impl std::fmt::Debug for DomainAuthCheck {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DomainAuthCheck").finish_non_exhaustive()
}
}
impl DomainAuthCheck {
pub fn new(lookup: Arc<dyn TxtLookup>) -> Self {
Self { lookup }
}
pub async fn run(&self, policy: &SenderPolicy) -> Result<DomainAuthReport> {
Ok(DomainAuthReport {
domain: policy.domain.clone(),
spf: self.spf(policy).await?,
dkim: self.dkim(policy).await?,
dmarc: self.dmarc(policy).await?,
checked_at: Utc::now(),
})
}
async fn spf_records(&self, domain: &str) -> Result<Vec<String>> {
Ok(self
.lookup
.txt(domain)
.await?
.into_iter()
.filter(|text| {
let lower = text.trim().to_ascii_lowercase();
lower == "v=spf1" || lower.starts_with("v=spf1 ")
})
.collect())
}
async fn spf(&self, policy: &SenderPolicy) -> Result<AuthCheck> {
let records = self.spf_records(&policy.domain).await?;
let record = match records.as_slice() {
[] => {
return Ok(AuthCheck::fail(format!(
"publish a TXT record at {} reading `v=spf1 {} ~all`",
policy.domain, policy.spf_mechanism
)));
}
[record] => record.clone(),
_ => {
return Ok(AuthCheck::fail(
"the domain publishes more than one SPF record; keep one",
));
}
};
let mut lookups = 0;
let mut pending = vec![record.clone()];
let mut authorised = false;
while let Some(record) = pending.pop() {
let mut redirect = None;
for term in record.split_whitespace().skip(1) {
let term = term.to_ascii_lowercase();
let (qualifier, mechanism) = match term.chars().next() {
Some(c @ ('+' | '-' | '~' | '?')) => (c, term[1..].to_string()),
_ => ('+', term.clone()),
};
if mechanism == "all" && qualifier == '+' {
return Ok(AuthCheck::fail(format!(
"`{term}` lets anyone send as {}; end the record with `~all` or `-all`",
policy.domain
)));
}
if mechanism == policy.spf_mechanism && qualifier == '+' {
authorised = true;
}
if let Some(domain) = mechanism.strip_prefix("include:") {
lookups += 1;
if lookups > SPF_LOOKUP_LIMIT {
return Ok(AuthCheck::fail(
"the SPF record needs more than 10 lookups (RFC 7208 §4.6.4)",
));
}
if qualifier == '+' {
pending.extend(self.spf_records(domain).await?);
}
} else if let Some(domain) = mechanism.strip_prefix("redirect=") {
redirect = Some(domain.to_string());
}
}
if let Some(domain) = redirect {
lookups += 1;
if lookups > SPF_LOOKUP_LIMIT {
return Ok(AuthCheck::fail(
"the SPF record needs more than 10 lookups (RFC 7208 §4.6.4)",
));
}
pending.extend(self.spf_records(&domain).await?);
}
}
Ok(if authorised {
AuthCheck::pass(format!("`{record}` authorises {}", policy.spf_mechanism))
} else {
AuthCheck::fail(format!(
"`{record}` does not authorise {}; add it",
policy.spf_mechanism
))
})
}
async fn dkim(&self, policy: &SenderPolicy) -> Result<AuthCheck> {
let name = policy.dkim_name();
let records = self.lookup.txt(&name).await?;
let key = records.iter().find(|text| {
let tags = tags(text);
tags.get("v").is_none_or(|version| version.eq_ignore_ascii_case("DKIM1"))
&& tags.get("p").is_some_and(|key| !key.is_empty())
});
Ok(match key {
Some(_) => AuthCheck::pass(format!("a DKIM key is published at {name}")),
None => AuthCheck::fail(format!(
"publish the sending service's DKIM key as a TXT record at {name} (`v=DKIM1; k=rsa; p=...`)"
)),
})
}
async fn dmarc(&self, policy: &SenderPolicy) -> Result<AuthCheck> {
let name = policy.dmarc_name();
let records: Vec<String> = self
.lookup
.txt(&name)
.await?
.into_iter()
.filter(|text| text.trim().to_ascii_lowercase().starts_with("v=dmarc1"))
.collect();
Ok(match records.as_slice() {
[] => AuthCheck::fail(format!(
"publish a TXT record at {name} such as `v=DMARC1; p=quarantine`"
)),
[record] => match tags(record).get("p").map(|p| p.to_ascii_lowercase()) {
Some(p) if matches!(p.as_str(), "none" | "quarantine" | "reject") => {
AuthCheck::pass(format!("DMARC policy `{p}` at {name}"))
}
_ => {
AuthCheck::fail(format!("the DMARC record at {name} has no valid `p=` policy"))
}
},
_ => AuthCheck::fail(format!("{name} has more than one DMARC record; keep one")),
})
}
}
fn tags(text: &str) -> BTreeMap<String, String> {
text.split(';')
.filter_map(|part| {
let (tag, value) = part.split_once('=')?;
Some((tag.trim().to_ascii_lowercase(), value.split_whitespace().collect::<String>()))
})
.collect()
}