opensmtpd-filter-dkimout/src/message.rs

142 lines
3.5 KiB
Rust
Raw Normal View History

2023-03-26 16:07:49 +02:00
use crate::config::Config;
2023-03-19 21:42:36 +01:00
use crate::entry::Entry;
use crate::parsed_message::ParsedMessage;
2023-04-15 19:24:04 +02:00
use crate::signature::Signature;
2023-04-10 13:09:56 +02:00
use anyhow::Result;
2023-04-15 19:24:04 +02:00
use sqlx::SqlitePool;
2023-04-09 11:07:16 +02:00
use tokio::io::{AsyncWriteExt, BufWriter};
2023-03-19 21:42:36 +01:00
pub const RETURN_SEP: &[u8] = b"|";
pub const RETURN_START: &[u8] = b"filter-dataline|";
2023-03-19 20:32:11 +01:00
#[derive(Debug)]
pub struct Message {
2023-03-19 21:42:36 +01:00
session_id: String,
token: String,
2023-03-23 20:54:20 +01:00
content: Vec<u8>,
nb_lines: usize,
2023-03-19 20:32:11 +01:00
}
impl Message {
2023-03-19 21:42:36 +01:00
pub fn from_entry(entry: &Entry) -> Self {
let mut ret = Self {
session_id: entry.get_session_id().to_string(),
token: entry.get_token().to_string(),
2023-03-23 20:54:20 +01:00
content: Vec::with_capacity(crate::DEFAULT_MSG_SIZE),
nb_lines: 0,
2023-03-19 21:42:36 +01:00
};
if !entry.is_end_of_message() {
ret.append_line(entry.get_data());
2023-03-19 20:32:11 +01:00
}
2023-03-19 21:42:36 +01:00
ret
2023-03-19 20:32:11 +01:00
}
pub fn append_line(&mut self, line: &[u8]) {
2023-03-23 20:54:20 +01:00
self.nb_lines += 1;
if self.content.len() + line.len() > self.content.capacity() {
self.content.reserve(crate::DEFAULT_MSG_SIZE);
}
self.content.extend_from_slice(line);
match line.last() {
Some(&c) => {
if c != b'\r' {
self.content.push(b'\r');
}
}
None => {
self.content.push(b'\r');
}
}
self.content.push(b'\n');
2023-03-19 20:32:11 +01:00
}
2023-03-21 13:00:44 +01:00
pub fn nb_lines(&self) -> usize {
2023-03-23 20:54:20 +01:00
self.nb_lines
2023-03-21 13:00:44 +01:00
}
2023-04-15 19:24:04 +02:00
pub async fn sign_and_return(&self, db: &SqlitePool, cnf: &Config) -> String {
let msg_id = get_msg_id(&self.session_id, &self.token);
log::trace!(
"{msg_id}: content: {}",
crate::display_bytes!(&self.content)
);
match ParsedMessage::from_bytes(&self.content) {
2023-03-25 15:42:46 +01:00
Ok(parsed_msg) => {
log::trace!("mail parsed");
for h in &parsed_msg.headers {
log::trace!(
"ParsedMessage: header: raw: {}",
crate::display_bytes!(h.raw)
);
log::trace!(
"ParsedMessage: header: name: {}",
crate::display_bytes!(h.name)
);
log::trace!(
"ParsedMessage: header: value: {}",
crate::display_bytes!(h.value)
);
2023-03-25 15:42:46 +01:00
}
log::trace!(
"ParsedMessage: body: {}",
crate::display_bytes!(parsed_msg.body)
);
2023-04-15 19:24:04 +02:00
match Signature::new(db, cnf, &parsed_msg).await {
Ok(signature) => {
let sig_header = signature.get_header();
if let Err(err) = self.print_sig_header(&sig_header).await {
log::error!("{msg_id}: unable to add the signature header: {err}");
}
}
Err(err) => log::error!("{msg_id}: unable to sign message: {err}"),
}
2023-03-25 15:42:46 +01:00
}
2023-04-10 16:29:36 +02:00
Err(err) => {
log::error!("{msg_id}: unable to parse message: {err}");
2023-03-25 15:42:46 +01:00
}
}
2023-04-10 13:09:56 +02:00
if let Err(err) = self.print_msg().await {
2023-04-15 19:24:04 +02:00
log::error!("{msg_id}: unable to write message: {err}");
2023-04-10 13:09:56 +02:00
}
2023-04-15 19:24:04 +02:00
msg_id
}
async fn print_sig_header(&self, sig_header: &str) -> Result<()> {
for line in sig_header.split("\r\n") {
self.print_line(line.as_bytes()).await?;
}
Ok(())
2023-03-25 15:42:46 +01:00
}
2023-04-10 13:09:56 +02:00
async fn print_msg(&self) -> Result<()> {
2023-03-23 20:54:20 +01:00
let i = self.content.len() - 1;
for line in self.content[0..i].split(|&b| b == b'\n') {
2023-04-10 13:09:56 +02:00
self.print_line(line).await?;
2023-03-19 21:42:36 +01:00
}
2023-04-10 13:09:56 +02:00
self.print_line(b".").await?;
Ok(())
2023-03-19 21:42:36 +01:00
}
2023-04-10 13:09:56 +02:00
async fn print_line(&self, line: &[u8]) -> Result<()> {
2023-04-15 19:24:35 +02:00
let line = if line.ends_with(&[b'\r']) {
&line[..line.len() - 1]
} else {
line
};
2023-04-09 11:07:16 +02:00
let mut stdout = BufWriter::new(tokio::io::stdout());
2023-04-10 13:09:56 +02:00
stdout.write_all(RETURN_START).await?;
stdout.write_all(self.session_id.as_bytes()).await?;
stdout.write_all(RETURN_SEP).await?;
stdout.write_all(self.token.as_bytes()).await?;
stdout.write_all(RETURN_SEP).await?;
stdout.write_all(line).await?;
stdout.write_all(b"\n").await?;
stdout.flush().await?;
Ok(())
2023-03-19 20:32:11 +01:00
}
}
2023-04-09 15:13:18 +02:00
pub fn get_msg_id(session_id: &str, token: &str) -> String {
format!("{session_id}.{token}")
}