Skip to content

Commit

Permalink
g3proxy: add SMTP mailbox address to transformed PUT request
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Jun 17, 2024
1 parent ca0e66f commit 786411c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 4 deletions.
9 changes: 9 additions & 0 deletions g3proxy/doc/protocol/helper/icap_smtp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ The following headers will be set in the HTTP PUT request:

The value will be "message/rfc822" for SMTP DATA message.

- X-SMTP-From

The value will be the *reverse-path* part of the SMTP MAIL command, which will contain the sender's Mailbox address.

- X-SMTP-To

The value will be the *forward-path* part of the SMTP RCPT command, which will contain the recipients' Mailbox address.
There will be multiple of this header if there are more than one recipients.

The body of the HTTP PUT request will be the corresponding SMTP message data.

Not Implemented
Expand Down
8 changes: 7 additions & 1 deletion g3proxy/src/inspect/smtp/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,13 @@ impl<'a, SC: ServerConfig> Transaction<'a, SC> {

let mut adaptation_state = ReqmodAdaptationRunState::new(Instant::now());
match adapter
.xfer_txt_data(&mut adaptation_state, clt_r, ups_w)
.xfer_txt_data(
&mut adaptation_state,
clt_r,
ups_w,
&self.mail_from,
&self.mail_to,
)
.await
{
Ok(ReqmodAdaptationEndState::OriginalTransferred) => Ok(()),
Expand Down
12 changes: 10 additions & 2 deletions lib/g3-icap-client/src/reqmod/smtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

use std::io::Write;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -24,6 +25,7 @@ use tokio::time::Instant;

use g3_http::HttpBodyDecodeReader;
use g3_io_ext::{IdleCheck, LimitedCopyConfig};
use g3_smtp_proto::command::{MailParam, RecipientParam};

use super::IcapReqmodClient;
use crate::{IcapClientConnection, IcapServiceClient};
Expand Down Expand Up @@ -98,10 +100,14 @@ impl<I: IdleCheck> SmtpMessageAdapter<I> {
self.client_username = Some(user.to_string());
}

pub fn build_http_header(&self) -> Vec<u8> {
pub fn build_http_header(&self, mail_from: &MailParam, mail_to: &[RecipientParam]) -> Vec<u8> {
let mut header = Vec::with_capacity(128);
header.extend_from_slice(b"PUT / HTTP/1.1\r\n");
header.extend_from_slice(b"Content-Type: message/rfc822\r\n");
let _ = write!(&mut header, "X-SMTP-From: {}\r\n", mail_from.reverse_path());
for to in mail_to {
let _ = write!(&mut header, "X-SMTP-To: {}\r\n", to.forward_path());
}
header.extend_from_slice(b"\r\n");
header
}
Expand All @@ -121,13 +127,15 @@ impl<I: IdleCheck> SmtpMessageAdapter<I> {
state: &mut ReqmodAdaptationRunState,
clt_r: &mut CR,
ups_w: &mut UW,
mail_from: &MailParam,
mail_to: &[RecipientParam],
) -> Result<ReqmodAdaptationEndState, SmtpAdaptationError>
where
CR: AsyncRead + Unpin,
UW: AsyncWrite + Unpin,
{
// TODO support preview?
self.xfer_txt_data_without_preview(state, clt_r, ups_w)
self.xfer_txt_data_without_preview(state, clt_r, ups_w, mail_from, mail_to)
.await
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/g3-icap-client/src/reqmod/smtp/txt_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use tokio::io::{AsyncRead, AsyncWrite, BufWriter};

use g3_http::StreamToChunkedTransfer;
use g3_io_ext::{IdleCheck, LimitedWriteExt};
use g3_smtp_proto::command::{MailParam, RecipientParam};
use g3_smtp_proto::io::TextDataDecodeReader;

use super::{
Expand Down Expand Up @@ -53,12 +54,14 @@ impl<I: IdleCheck> SmtpMessageAdapter<I> {
state: &mut ReqmodAdaptationRunState,
clt_r: &mut CR,
ups_w: &mut UW,
mail_from: &MailParam,
mail_to: &[RecipientParam],
) -> Result<ReqmodAdaptationEndState, SmtpAdaptationError>
where
CR: AsyncRead + Unpin,
UW: AsyncWrite + Unpin,
{
let http_header = self.build_http_header();
let http_header = self.build_http_header(mail_from, mail_to);
let icap_header = self.build_forward_all_request(http_header.len());

let icap_w = &mut self.icap_connection.0;
Expand Down
6 changes: 6 additions & 0 deletions lib/g3-smtp-proto/src/command/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ impl MailParam {
"MAIL",
"invalid reverse path prefix",
))?;
if !super::path::is_valid(&reverse_path) {
return Err(CommandLineError::InvalidCommandParam(
"MAIL",
"invalid reverse path prefix",
));
}

Ok(MailParam { reverse_path })
}
Expand Down
2 changes: 2 additions & 0 deletions lib/g3-smtp-proto/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use g3_types::net::Host;

use crate::response::ResponseEncoder;

mod path;

mod hello;
mod mail;
mod recipient;
Expand Down
35 changes: 35 additions & 0 deletions lib/g3-smtp-proto/src/command/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 ByteDance and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub(super) fn is_valid(s: &str) -> bool {
let Some(s) = s.strip_prefix('<') else {
return false;
};
let Some(s) = s.strip_suffix('>') else {
return false;
};

for c in s.chars() {
let c = c as u32;
// 0-31 and >127 are not allowed
// see https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
if !(32..127).contains(&c) {
return false;
}
}

true
}
11 changes: 11 additions & 0 deletions lib/g3-smtp-proto/src/command/recipient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub struct RecipientParam {
}

impl RecipientParam {
#[inline]
pub fn forward_path(&self) -> &str {
&self.forward_path
}

pub(super) fn parse(msg: &[u8]) -> Result<Self, CommandLineError> {
let msg = str::from_utf8(msg).map_err(CommandLineError::InvalidUtf8Command)?;

Expand All @@ -41,6 +46,12 @@ impl RecipientParam {
"RCPT",
"invalid forward path prefix",
))?;
if !super::path::is_valid(&forward_path) {
return Err(CommandLineError::InvalidCommandParam(
"RCPT",
"invalid forward path prefix",
));
}

Ok(RecipientParam { forward_path })
}
Expand Down

0 comments on commit 786411c

Please sign in to comment.