This commit is contained in:
Rodolphe Bréard 2023-03-21 13:00:44 +01:00
parent d0e63c8318
commit dd955fcb44
6 changed files with 63 additions and 2 deletions

View file

@ -8,4 +8,6 @@ license = "MIT OR Apache-2.0"
publish = false
[dependencies]
env_logger = "^0.10"
log = "^0.4"
nom = "^7.1"

View file

@ -1,3 +1,4 @@
use crate::display_bytes;
use crate::stdin_reader::StdinReader;
pub const CONFIG_END: &[u8] = b"config|ready\n";
@ -7,15 +8,18 @@ pub fn read_config(reader: &mut StdinReader) {
loop {
let line = reader.read_line();
if line == CONFIG_END {
log::trace!("configuration is ready");
return;
}
if !line.starts_with(CONFIG_TAG) {
eprintln!("invalid config line: {line:?}");
log::warn!("invalid config line: {}", display_bytes!(line));
}
}
}
pub fn register_filter() {
log::trace!("registering the filter");
println!("register|filter|smtp-in|data-line");
println!("register|ready");
log::trace!("filter registered");
}

10
src/logs.rs Normal file
View file

@ -0,0 +1,10 @@
use env_logger::{Builder, Env, Target};
pub fn init_log_system() {
let env = Env::new()
.filter_or(crate::LOG_LEVEL_ENV_VAR, "warn")
.write_style_or(crate::LOG_STYLE_ENV_VAR, "never");
let mut builder = Builder::from_env(env);
builder.target(Target::Stderr);
builder.init();
}

View file

@ -1,5 +1,6 @@
mod entry;
mod handshake;
mod logs;
mod message;
mod stdin_reader;
@ -9,12 +10,43 @@ use std::collections::HashMap;
use stdin_reader::StdinReader;
const DEFAULT_BUFF_SIZE: usize = 1024;
const LOG_LEVEL_ENV_VAR: &str = "OPENSMTPD_FILTER_DKIMOUT_LOG_LEVEL";
const LOG_STYLE_ENV_VAR: &str = "OPENSMTPD_FILTER_DKIMOUT_LOG_STYLE";
#[macro_export]
macro_rules! display_bytes {
($bytes: expr) => {
$bytes
.iter()
.map(|b| {
let v: Vec<u8> = std::ascii::escape_default(*b).collect();
String::from_utf8_lossy(&v).to_string()
})
.collect::<String>()
};
}
macro_rules! log_messages {
($list: ident) => {
log::trace!(
"message list has {} elements: {}",
$list.len(),
$list
.iter()
.map(|(k, v)| { format!("{k} ({} lines)", v.nb_lines()) })
.collect::<Vec<String>>()
.join(", ")
)
};
}
fn main() {
logs::init_log_system();
let mut reader = StdinReader::new();
let mut messages: HashMap<String, Message> = HashMap::new();
handshake::read_config(&mut reader);
handshake::register_filter();
log_messages!(messages);
loop {
match Entry::from_bytes(&reader.read_line()) {
Ok(entry) => {
@ -22,25 +54,31 @@ fn main() {
match messages.get_mut(&msg_id) {
Some(msg) => {
if !entry.is_end_of_message() {
log::debug!("new line in message: {msg_id}");
msg.append_line(entry.get_data());
} else {
log::debug!("message ready: {msg_id}");
msg.sign_and_return();
messages.remove(&msg_id);
log::debug!("message removed: {msg_id}");
}
}
None => {
let msg = Message::from_entry(&entry);
if !entry.is_end_of_message() {
log::debug!("new message: {msg_id}");
messages.insert(msg_id, msg);
} else {
log::debug!("empty new message: {msg_id}");
msg.sign_and_return();
}
}
}
}
Err(err) => {
eprintln!("{err}");
log::error!("invalid filter line: {err}");
}
}
log_messages!(messages);
}
}

View file

@ -28,6 +28,10 @@ impl Message {
self.lines.push(line.to_vec())
}
pub fn nb_lines(&self) -> usize {
self.lines.len()
}
pub fn sign_and_return(&self) {
// TODO: sign the message using DKIM
for line in &self.lines {

View file

@ -1,3 +1,4 @@
use crate::display_bytes;
use std::io::{BufRead, BufReader};
pub struct StdinReader {
@ -15,9 +16,11 @@ impl StdinReader {
pub fn read_line(&mut self) -> Vec<u8> {
self.buffer.clear();
log::trace!("reading line from stdin");
if self.reader.read_until(b'\n', &mut self.buffer).unwrap() == 0 {
std::process::exit(0)
}
log::trace!("line read from stdin: {}", display_bytes!(self.buffer));
self.buffer.clone()
}
}