-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnitmo_fakesmtp.sh
executable file
·148 lines (117 loc) · 4.25 KB
/
nitmo_fakesmtp.sh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
LOG_UNKNOWN_COMMANDS="/tmp/nitmoMail_unknown_commands.log"
SENDLOG_PATH="/tmp/nitmoMail"
sender(){
from=$1
to=$2
subject=$3
text=$4
headers=$5
# Set TO host to include @log or +log to log the email in $SENDLOG_PATH. Ex: test@log / hello_world@example+log.com
if [[ "$to" == *@log* ]] || [[ "$to" == *+log* ]]; then
date=$(date '+%Y-%m-%d')
datetime=$(date '+%Y-%m-%d %H:%M:%S')
[ -d $SENDLOG_PATH ] || mkdir -p $SENDLOG_PATH
file="${SENDLOG_PATH}/${date}.log"
echo "${datetime}${NL} ${subject} [F: ${from} | T: ${to}]" >> $file
while read -r line; do
echo " > $line" >> $file
done <<< $text
echo "" >> $file
fi
# Set TO host to include @drop or +drop to not do any further. Ex: test@log+drop / [email protected]
([[ "$to" == *@drop* ]] || [[ "$to" == *+drop* ]]) && return
# Send Message using the telegram-send installed from pip
# See https://www.rahielkasim.com/telegram-send/
# pip install telegram-send
if [[ "$to" == telegram@* ]]; then
send="*${subject}*${NL}${NL}${text}"
/usr/bin/telegram-send --format markdown "$send"
fi
# Forward the message to any other type of script, binary, that-so-ever.
if [[ "$to" == push@* ]]; then
send="${subject}${NL}${NL}${text}"
# Using this litte helper: https://github.com/ivkos/Pushbullet-for-PHP
php /usr/boppy/bin/pushbullet.php "N5X" "$send"
fi
}
################################################################################
# Library Code starts here. You shouldn't change anything beyond...
################################################################################
trim() {
echo -e "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
NL="
"
################################################################################
# SMTP Server
################################################################################
echo '220 nitmo mail relay';
receiveData=0
allowFinalize=0
allowBody=0
from=""
to=""
header=""
body=""
subject=""
while true; do
if [ $receiveData -eq 0 ]; then
read -r arg1 arg2 rest
arg1=$(trim "$arg1")
arg2=$(trim "$arg2")
rest=$(trim "$rest")
arg1=$(echo "$arg1" | tr '/a-z/' '/A-Z/')
if [ "$arg1" = "HELO" ] || [ "$arg1" = "EHLO" ]; then
echo '250 HELO faithful employee'
elif [ "$arg1" = "MAIL" ] && [ "$arg2" = "FROM:" ]; then
from=$rest
echo '250 2.1.0 Ok'
elif [ "$arg1" = "RCPT" ] && [ "$arg2" = "TO:" ]; then
to=$rest
echo '250 2.1.5 Ok'
elif [ "$arg1" = "DATA" ]; then
receiveData=1;
echo '354 Ok Send data ending with <CRLF>.<CRLF>';
elif [ "$arg1" = "QUIT" ]; then
echo '221 2.0.0 Bye faithful employee';
exit
else
echo "502 5.5.2 Error: command not recognized"
if [ ! -z "$LOG_UNKNOWN_COMMANDS" ]; then
cur=$(date "+%Y-%m-%d %H:%M:%S")
echo -e "${cur}: °${arg1}° °${arg2}° °${rest}°" >> $LOG_UNKNOWN_COMMANDS
fi
fi
else
read -r arg1 input2
arg1=$(trim "$arg1")
input2=$(trim "$input2")
input="$arg1 $input2"
if [ $allowBody -eq 0 ]; then
if [ "$header" = "" ]; then
header=$input
else
[ "$arg1" = "" ] && allowBody=1
[ "$arg1" = "Subject:" ] && subject=$input2
header="${header}${NL}${input}"
fi
else
if [ $allowFinalize -eq 1 ] && [ "$input" = ". " ]; then
receiveData=0
sender "$from" "$to" "$subject" "$body" "$header"
echo '250 2.0.0 Ok: queued as '$(od -vN 10 -An -tx1 /dev/urandom | tr -d " \n")
elif [ "$body" = "" ]; then
body=$input
else
if [ "$input" = " " ]; then
allowFinalize=1
else
allowFinalize=0
fi
body="${body}${NL}${input}"
fi
fi
fi
done
exit 0