- mail-oci: OCI Email Delivery client (signed REST), per-customer domain/DKIM/sender onboarding, suppression mirror, sending limits - mail-mta: RelayPool transport with OCI primary, optional secondary, health/back-off/quota failover; direct MTA kept but off - mail-inbound: DSN/ARF/DMARC/TLS-RPT parsing + HMAC-signed POST /inbound/raw ingest (no port 25) - workers/inbound-forwarder: Cloudflare Email Routing worker forwarding raw mail over HTTPS Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
122 lines
5.0 KiB
Rust
122 lines
5.0 KiB
Rust
//! Feeds hand-written reports to a running `mail-inbound` over plain SMTP
|
|
//! and checks that relaying and unknown users are refused.
|
|
//!
|
|
//! ```sh
|
|
//! cargo run -p mail-inbound --example feed # targets 127.0.0.1:2525
|
|
//! FEED_TARGET=127.0.0.1:2525 FEED_DOMAIN=dev.test cargo run -p mail-inbound --example feed
|
|
//! ```
|
|
|
|
mod samples;
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
|
use tokio::net::TcpStream;
|
|
|
|
struct Client {
|
|
io: BufReader<TcpStream>,
|
|
}
|
|
|
|
impl Client {
|
|
async fn connect(addr: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
|
let stream = tokio::time::timeout(Duration::from_secs(5), TcpStream::connect(addr)).await??;
|
|
let mut c = Client { io: BufReader::new(stream) };
|
|
let banner = c.read().await?;
|
|
println!("<< {banner}");
|
|
Ok(c)
|
|
}
|
|
|
|
async fn read(&mut self) -> Result<String, Box<dyn std::error::Error>> {
|
|
let mut all = String::new();
|
|
loop {
|
|
let mut line = String::new();
|
|
let n = tokio::time::timeout(Duration::from_secs(30), self.io.read_line(&mut line)).await??;
|
|
if n == 0 {
|
|
return Err("connection closed".into());
|
|
}
|
|
all.push_str(line.trim_end());
|
|
if line.len() >= 4 && &line[3..4] == "-" {
|
|
all.push('\n');
|
|
continue;
|
|
}
|
|
return Ok(all);
|
|
}
|
|
}
|
|
|
|
async fn cmd(&mut self, line: &str) -> Result<String, Box<dyn std::error::Error>> {
|
|
println!(">> {line}");
|
|
self.io.get_mut().write_all(format!("{line}\r\n").as_bytes()).await?;
|
|
let reply = self.read().await?;
|
|
println!("<< {reply}");
|
|
Ok(reply)
|
|
}
|
|
|
|
async fn send(&mut self, from: &str, to: &str, body: &[u8]) -> Result<String, Box<dyn std::error::Error>> {
|
|
let r = self.cmd(&format!("MAIL FROM:<{from}>")).await?;
|
|
if !r.starts_with("250") {
|
|
return Ok(r);
|
|
}
|
|
let r = self.cmd(&format!("RCPT TO:<{to}>")).await?;
|
|
if !r.starts_with("250") {
|
|
self.cmd("RSET").await?;
|
|
return Ok(r);
|
|
}
|
|
let r = self.cmd("DATA").await?;
|
|
if !r.starts_with("354") {
|
|
return Ok(r);
|
|
}
|
|
let mut stuffed = Vec::with_capacity(body.len() + 64);
|
|
for line in body.split(|&b| b == b'\n') {
|
|
let line = line.strip_suffix(b"\r").unwrap_or(line);
|
|
if line.first() == Some(&b'.') {
|
|
stuffed.push(b'.');
|
|
}
|
|
stuffed.extend_from_slice(line);
|
|
stuffed.extend_from_slice(b"\r\n");
|
|
}
|
|
stuffed.extend_from_slice(b".\r\n");
|
|
println!(">> <{} bytes of message data>", stuffed.len());
|
|
self.io.get_mut().write_all(&stuffed).await?;
|
|
let reply = self.read().await?;
|
|
println!("<< {reply}");
|
|
Ok(reply)
|
|
}
|
|
}
|
|
|
|
fn check(label: &str, reply: &str, expect_prefix: &str) {
|
|
let ok = reply.starts_with(expect_prefix);
|
|
println!("[{}] {label}: expected {expect_prefix}..., got {}\n", if ok { "PASS" } else { "FAIL" }, reply.lines().next().unwrap_or(""));
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let target = std::env::var("FEED_TARGET").unwrap_or_else(|_| "127.0.0.1:2525".into());
|
|
let domain = std::env::var("FEED_DOMAIN").unwrap_or_else(|_| "dev.test".into());
|
|
let reports_domain = std::env::var("FEED_REPORTS_DOMAIN").unwrap_or_else(|_| format!("reports.{domain}"));
|
|
let mut c = Client::connect(&target).await?;
|
|
check("EHLO", &c.cmd("EHLO feeder.example").await?, "250");
|
|
|
|
let dsn = samples::dsn(&domain);
|
|
check("RFC 3464 DSN to bounces+abc", &c.send("", &format!("bounces+abc@{domain}"), &dsn).await?, "250");
|
|
|
|
let arf = samples::arf(&domain);
|
|
check("ARF complaint to abuse@", &c.send("feedback@isp.example", &format!("abuse@{domain}"), &arf).await?, "250");
|
|
|
|
let dmarc = samples::dmarc_gzip(&domain);
|
|
check("DMARC aggregate (gzip XML) to dmarc@", &c.send("noreply-dmarc-support@google.com", &format!("dmarc@{domain}"), &dmarc).await?, "250");
|
|
|
|
let tlsrpt = samples::tlsrpt(&domain);
|
|
check("TLS-RPT JSON to tls-rpt@", &c.send("tlsrpt@google.com", &format!("tls-rpt@{domain}"), &tlsrpt).await?, "250");
|
|
|
|
let plain = samples::plain_bounce(&domain);
|
|
check("non-RFC3464 bounce to bounces+xyz", &c.send("mailer-daemon@legacy.example", &format!("bounces+xyz@{domain}"), &plain).await?, "250");
|
|
|
|
check("relay attempt to gmail.com", &c.send("someone@evil.example", "victim@gmail.com", b"Subject: relay\r\n\r\nx").await?, "550 5.7.1");
|
|
check("unknown user on report-only domain", &c.send("someone@evil.example", &format!("nobody@{reports_domain}"), b"Subject: x\r\n\r\nx").await?, "550 5.1.1");
|
|
check("dmarc@ on report-only domain", &c.cmd("MAIL FROM:<a@b.example>").await.and(c.cmd(&format!("RCPT TO:<dmarc@{reports_domain}>")).await)?, "250");
|
|
c.cmd("RSET").await?;
|
|
check("oversize MAIL FROM SIZE", &c.cmd("MAIL FROM:<big@example.com> SIZE=99999999999").await?, "552");
|
|
check("QUIT", &c.cmd("QUIT").await?, "221");
|
|
Ok(())
|
|
}
|