mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-01-07 17:47:14 +00:00
smtpsrv: Implement a post-DATA hook
This patch implements a post-DATA hook, which is run after receiving the data but before sending a reply. It can be used to implement content filtering when receiving email, for example for passing the email through an anti-spam or an anti-virus.
This commit is contained in:
1
test/t-10-hooks/.gitignore
vendored
Normal file
1
test/t-10-hooks/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
config/hooks/post-data
|
||||
8
test/t-10-hooks/config/chasquid.conf
Normal file
8
test/t-10-hooks/config/chasquid.conf
Normal file
@@ -0,0 +1,8 @@
|
||||
smtp_address: ":1025"
|
||||
submission_address: ":1587"
|
||||
monitoring_address: ":1099"
|
||||
|
||||
mail_delivery_agent_bin: "test-mda"
|
||||
mail_delivery_agent_args: "%to%"
|
||||
|
||||
data_dir: "../.data"
|
||||
5
test/t-10-hooks/config/hooks/post-data.bad1
Executable file
5
test/t-10-hooks/config/hooks/post-data.bad1
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo $0 > ../.data/post-data.out
|
||||
echo "This is not a header"
|
||||
|
||||
8
test/t-10-hooks/config/hooks/post-data.bad2
Executable file
8
test/t-10-hooks/config/hooks/post-data.bad2
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo $0 > ../.data/post-data.out
|
||||
|
||||
echo "X-Post-DATA: This starts like a header"
|
||||
echo
|
||||
echo "But then is not"
|
||||
|
||||
7
test/t-10-hooks/config/hooks/post-data.bad3
Executable file
7
test/t-10-hooks/config/hooks/post-data.bad3
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo $0 > ../.data/post-data.out
|
||||
|
||||
# Just a newline is quite problematic, as it would break the headers.
|
||||
echo
|
||||
|
||||
5
test/t-10-hooks/config/hooks/post-data.bad4
Executable file
5
test/t-10-hooks/config/hooks/post-data.bad4
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo $0 > ../.data/post-data.out
|
||||
|
||||
echo -n "X-Post-DATA: valid header with no newline at the end"
|
||||
14
test/t-10-hooks/config/hooks/post-data.good
Executable file
14
test/t-10-hooks/config/hooks/post-data.good
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
env > ../.data/post-data.out
|
||||
echo >> ../.data/post-data.out
|
||||
|
||||
cat >> ../.data/post-data.out
|
||||
|
||||
if [ "$RCPT_TO" == "blockme@testserver" ]; then
|
||||
echo "¡No pasarán!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "X-Post-Data: success"
|
||||
|
||||
4
test/t-10-hooks/content
Normal file
4
test/t-10-hooks/content
Normal file
@@ -0,0 +1,4 @@
|
||||
Subject: Prueba desde el test
|
||||
|
||||
Crece desde el test el futuro
|
||||
Crece desde el test
|
||||
1
test/t-10-hooks/hosts
Normal file
1
test/t-10-hooks/hosts
Normal file
@@ -0,0 +1 @@
|
||||
testserver localhost
|
||||
14
test/t-10-hooks/msmtprc
Normal file
14
test/t-10-hooks/msmtprc
Normal file
@@ -0,0 +1,14 @@
|
||||
account default
|
||||
|
||||
host testserver
|
||||
port 1587
|
||||
|
||||
tls on
|
||||
tls_trust_file config/certs/testserver/fullchain.pem
|
||||
|
||||
from user@testserver
|
||||
|
||||
auth on
|
||||
user user@testserver
|
||||
password secretpassword
|
||||
|
||||
64
test/t-10-hooks/run.sh
Executable file
64
test/t-10-hooks/run.sh
Executable file
@@ -0,0 +1,64 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
. $(dirname ${0})/../util/lib.sh
|
||||
|
||||
init
|
||||
|
||||
generate_certs_for testserver
|
||||
add_user testserver user secretpassword
|
||||
add_user testserver someone secretpassword
|
||||
add_user testserver blockme secretpassword
|
||||
|
||||
mkdir -p .logs
|
||||
chasquid -v=2 --log_dir=.logs --config_dir=config &
|
||||
wait_until_ready 1025
|
||||
|
||||
cp config/hooks/post-data.good config/hooks/post-data
|
||||
|
||||
run_msmtp someone@testserver < content
|
||||
|
||||
wait_for_file .mail/someone@testserver
|
||||
|
||||
mail_diff content .mail/someone@testserver
|
||||
|
||||
if ! grep -q "X-Post-Data: success" .mail/someone@testserver; then
|
||||
echo "missing X-Post-Data header"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
function check() {
|
||||
if ! grep -q "$1" .data/post-data.out; then
|
||||
echo missing: $1
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Verify that the environment for the hook was reasonable.
|
||||
check "RCPT_TO=someone@testserver"
|
||||
check "MAIL_FROM=user@testserver"
|
||||
check "USER=$USER"
|
||||
check "PWD=$PWD/config"
|
||||
check "FROM_LOCAL_DOMAIN=1"
|
||||
check "ON_TLS=1"
|
||||
check "AUTH_AS=user@testserver"
|
||||
check "PATH="
|
||||
check "REMOTE_ADDR="
|
||||
|
||||
|
||||
# Check that a failure in the script results in failing delivery.
|
||||
if run_msmtp blockme@testserver < content 2>/dev/null; then
|
||||
echo "ERROR: hook did not block email as expected"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that the bad hooks don't prevent delivery.
|
||||
for i in config/hooks/post-data.bad*; do
|
||||
cp $i config/hooks/post-data
|
||||
|
||||
run_msmtp someone@testserver < content
|
||||
wait_for_file .mail/someone@testserver
|
||||
mail_diff content .mail/someone@testserver
|
||||
done
|
||||
|
||||
success
|
||||
Reference in New Issue
Block a user