From 65f0ce13a6c0f1b00d66bb992bde803051ae7f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Mon, 21 Dec 2020 15:28:19 +0100 Subject: [PATCH] Add the rm_x-originating-ip example --- opensmtpd/Cargo.toml | 4 ++++ opensmtpd/examples/rm_x-originating-ip.rs | 27 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 opensmtpd/examples/rm_x-originating-ip.rs diff --git a/opensmtpd/Cargo.toml b/opensmtpd/Cargo.toml index cfaebb2..f0c4865 100644 --- a/opensmtpd/Cargo.toml +++ b/opensmtpd/Cargo.toml @@ -23,3 +23,7 @@ simplelog = "0.9" [[example]] name = "counter" path = "examples/counter.rs" + +[[example]] +name = "rm_x-originating-ip" +path = "examples/rm_x-originating-ip.rs" diff --git a/opensmtpd/examples/rm_x-originating-ip.rs b/opensmtpd/examples/rm_x-originating-ip.rs new file mode 100644 index 0000000..9bfd6ae --- /dev/null +++ b/opensmtpd/examples/rm_x-originating-ip.rs @@ -0,0 +1,27 @@ +use opensmtpd::{return_data_line, run_filter, Filter, FilterEntry}; +use opensmtpd_derive::register; + +pub const HEADER_NAME: &str = "x-originating-ip:"; +pub const HEADER_LEN: usize = 17; + +struct RmXOriginatingIp {} + +impl Filter for RmXOriginatingIp { + #[register] + fn on_filter_data_line(&mut self, entry: &FilterEntry, data_line: &[u8]) { + if data_line.len() >= HEADER_LEN { + let head_start = data_line[..HEADER_LEN].to_vec(); + if let Ok(s) = String::from_utf8(head_start) { + if s.to_lowercase() == HEADER_NAME { + return; + } + } + } + return_data_line(entry, data_line); + } +} + +fn main() { + let mut my_filter = RmXOriginatingIp {}; + run_filter(&mut my_filter); +}