-
Notifications
You must be signed in to change notification settings - Fork 7
/
send
executable file
·96 lines (68 loc) · 2.86 KB
/
send
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh -e
# Setup environment with needed environment vars
. /etc/cable/profile
# Creates ${queuedir}/<msgid>{username,hostname,message,send.req}
queuedir=${CABLE_QUEUES}/queue
msgidbytes=20
emailregex="${CABLE_REGEX}"
# Temporary directory on same filesystem
tmpdir=`mktemp -d --tmpdir=${queuedir}`
cleanup() {
rm -r ${tmpdir}
}
trap 'st=$?; set +e; cleanup; exit ${st}' 0
trap : INT QUIT TERM SEGV
error() {
# don't log via syslog, since invocation is interactive
echo "send: $@" 1>&2
exit 1
}
# Read the message on stdin, and remove X-*: headers
formail -fz -I X- > ${tmpdir}/message
# Prepare headers for local MUA confirmation message
formail -f -X From: -X To: -X Cc: -X Bcc: -X Subject: -X Date: \
-X Message-ID: -X In-Reply-To: -X References: -a 'Subject: ' \
< ${tmpdir}/message | sed 's/^Subject: /&[vfy] /i' \
> ${tmpdir}/message.hdr
# Extract bare x@y addresses
addresses=`formail -fcz -x To: -x Cc: -x Bcc: < ${tmpdir}/message \
| tr , '\n' \
| sed 's/^.*<\([^>]*\)>/\1/' \
| sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//' \
| tr '[:upper:]' '[:lower:]'`
addresses=`echo ${addresses} | tr '[:blank:]' '\n' | sort -u`
# Extract the bare From: address
fromaddr=`formail -fcz -x From: < ${tmpdir}/message \
| sed 's/^.*<\([^>]*\)>/\1/' \
| tr '[:upper:]' '[:lower:]'`
# Check address validity to prevent accidental information leaks
for addr in "${fromaddr}" ${addresses}; do
if ! echo x "${addr}" | egrep -q "^x ${emailregex}$"; then
error "unsupported address: ${addr}"
fi
done
# Extract the from-user and from-host
echo ${fromaddr%@*} > ${tmpdir}/susername
echo ${fromaddr#*@} > ${tmpdir}/shostname
# Remove Bcc: header and replace Date: with a UTC one:
date=`date -uR`
formail -f -I Bcc: -I "Date: ${date}" < ${tmpdir}/message \
| gzip -c9n > ${tmpdir}/message.gz
mv ${tmpdir}/message.gz ${tmpdir}/message
# Create actual <msgid> directories, one per addressee
for addr in ${addresses}; do
# generate <msgid> and create as subdirectory
msgid=`openssl rand -hex ${msgidbytes}`
[ ${#msgid} = $((msgidbytes * 2)) ] || error "could not generate msgid"
mkdir ${tmpdir}/${msgid}
# extract username and hostname from address, and generate respective files
echo ${addr%@*} > ${tmpdir}/${msgid}/username
echo ${addr#*@} > ${tmpdir}/${msgid}/hostname
# copy the sanitized message (assume the files will not be modified later)
ln ${tmpdir}/message ${tmpdir}/message.hdr \
${tmpdir}/susername ${tmpdir}/shostname \
${tmpdir}/${msgid}/
# atomically move directory to queue dir, and create send.req indicator
touch ${tmpdir}/${msgid}/send.req
mv -T ${tmpdir}/${msgid} ${queuedir}/${msgid}
done