forked from huan/docker-simple-mail-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·316 lines (258 loc) · 8.27 KB
/
entrypoint.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
ARGV=$@
function print_help {
cat <<EOF
Docker SMF - Simple Mail Forwarder
===============================================================================
To create a new mail server for your domain,
you could use the following commands:
$ docker run -p 25:25 \\
zixia/simple-mail-forwarder \\
Environment Variables:
SMF_DOMAIN - mail server hostname. use tutum/docker hostname if omitted.
SMF_CONFIG - mail forward addresses mapping list.
SMF_MYNETWORKS - configure relaying from trusted IPs, see http://www.postfix.org/postconf.5.html#mynetworks
SMF_RELAYHOST - configure a relayhost
this creates a new smtp server which listens on port 25,
forward all email from
_______________________________________________________________________________
EOF
}
#
# Start
#
function start_postfix {
#
# OpenSSL Init
#
bash /app/init-openssl.sh
#
# Set virtual user maping
#
if [ "$SMF_CONFIG" = "" ]; then
if [[ "$1" =~ [a-zA-Z0-9_.]+@[a-zA-Z0-9_.]+\. ]]; then
SMF_CONFIG=$1
else
echo ">> SMF_CONFIG not found. format: [email protected]:[email protected];..."
echo ">> I don't know how to do. So I quit."
exit
fi
else
echo ">> SMF_CONFIG found in ENV. use this settings for forward maps."
fi
echo "SMF_CONFIG='$SMF_CONFIG'" > SMF_CONFIG.env
NEWLINE=$'\n'
# seperated by ;, or "\n" also supported(for config file in the furture).
forwardList=(${SMF_CONFIG//;/ })
virtualDomains=""
virtualUsers=""
password='' # global variable for save password in the next for loop.
for forward in "${forwardList[@]}"; do
emailPair=(${forward//:/ })
emailFrom=${emailPair[0]}
emailTo=${emailPair[1]}
emailToArr=(${emailTo//|/ })
emailTo=$(printf "\t%s" "${emailToArr[@]}")
emailTo=${emailTo:1}
tryPassword=${emailPair[2]}
# 1. if user has no password, then use the last seen password from other users.
# 2. if there's no password defined at all, random generate one for the first time.
if [ -z "$tryPassword" ]
then
if [ -z "$password" ]
then
# random generate password
password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1 | tr '[:upper:]' '[:lower:]')
fi
tryPassword=$password
fi
password=$tryPassword
echo ">> Setting password[$password] for user $emailFrom ..."
echo $password | saslpasswd2 $emailFrom
newLine=$(printf '%s\t%s' $emailFrom "$emailTo")
virtualUsers="${virtualUsers}${newLine}${NEWLINE}"
domainFrom=${emailFrom##*@}
[[ $virtualDomains =~ $domainFrom ]] || {
virtualDomains="$virtualDomains $domainFrom"
}
done
#
# issue #1: forward all other emails to the original domain
#
for virtualDomain in $virtualDomains; do
virtualUsers="${virtualUsers}@${virtualDomain} @${virtualDomain} $NEWLINE"
done
echo "$virtualUsers" > /etc/postfix/virtual
# issue #1: postconf -e virtual_alias_domains="$virtualDomains"
postconf -e relay_domains="$virtualDomains"
postconf -e virtual_alias_maps="hash:/etc/postfix/virtual"
# initial user database
postmap /etc/postfix/virtual
#
# Set HOSTNAME to right Domain
#
if [ "$SMF_DOMAIN" != "" ]
then
# get from user setting
HOSTNAME="$SMF_DOMAIN"
elif [ "$TUTUM_CONTAINER_FQDN" != "" ]
then
# use tutum.co FQDN for this container
HOSTNAME="$TUTUM_CONTAINER_FQDN"
elif [ "$TUTUM_NODE_FQDN" != "" ]
then
# use tutum.co FQDN for this node
HOSTNAME="$TUTUM_NODE_FQDN"
elif [ "$domainFrom" != "" ]
then
# use the last virtual domain name
HOSTNAME=$domainFrom
elif [[ "`hostname`" =~ ^[a-zA-Z0-9_.]+\.[a-zA-Z0-9_.]+ ]]
then
# this docker has a valid FQDN hostname!
HOSTNAME=`hostname`
else
# bad! whatever get from Docker
HOSTNAME=`hostname`
fi
# this one is not posix compatible: RE='\d+\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+)'
# use next one: [0-9] for \d , make posix comfortable (I will not :-[ )
RE='[0-9]+\s+IN\s+A\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'
[[ `drill SimpleMailForwarder.smf.$HOSTNAME @wimi.36c33f49.svc.dockerapp.io` =~ $RE ]] && {
# Setting hosts
HOSTS_LINE="${BASH_REMATCH[1]}\t$HOSTNAME"
grep -v $HOSTS_LINE /etc/hosts | grep -v ^$ > /etc/hosts.$$
cat /etc/hosts.$$ > /etc/hosts && rm /etc/hosts.$$
echo -e $HOSTS_LINE >> /etc/hosts
}
echo "SMF_DOMAIN='$HOSTNAME'" > SMF_DOMAIN.env
echo ">> Set hostname to $HOSTNAME"
if [ "$SMF_MYNETWORKS" != "" ]
then
postconf -e mynetworks="$SMF_MYNETWORKS"
fi
# add domain
postconf -e myhostname="$HOSTNAME"
postconf -e mydestination="localhost"
echo "$HOSTNAME" > /etc/mailname
echo "$HOSTNAME" > /etc/hostname
if [ "SMF_RELAYHOST" != "" ]
then
postconf -e relayhost="$SMF_RELAYHOST"
fi
postfix start
}
#
# TEST
#
function test_running_env {
echo ">> Start self-testing..."
bats test/simple-mail-forwarder.bats
if [ $? -eq 0 ]
then
echo ">> Test PASSED"
else
echo ">> Test FAILED!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> !!!!!!!!!!!!!!!!!!!! SYSTEM ERROR !!!!!!!!!!!!!!!!!!!!"
echo ">> But I'll pretend to run... good luck! :P"
fi
}
echo ">> Chdir to /app..."
cd /app
[ -e BUILD.env ] && source BUILD.env
# Generated by figlet
cat BANNER
printf "%50s\n" "Source#$GIT_HASH $GIT_DATE * $GIT_BRANCH"
printf "%50s\n\n" "Built on $SMF_BUILD_DATE by $SMF_BUILD_HOST"
if [ "" == "$SMF_DOMAIN" ]
then
echo ">> ENV SMF_DOMAIN not set."
else
echo ">> END SMF_DOMAIN found. value:[$SMF_DOMAIN]"
fi
if [ "" == "$SMF_CONFIG" ]
then
echo ">> END SMF_CONFIG not set."
else
echo ">> ENV SMF_CONFIG found. value:[$SMF_CONFIG]"
fi
# ARGV
#
#ARGV=$@
if [ "" == "$ARGV" ]
then
echo ">> ARGV arguments not set."
else
echo ">> ARGV arguments found. value:[$ARGV]"
fi
if [ "-h" == "$1" ] || [ "--help" == "$1" ] || [ ! "$1" -a ! "$SMF_CONFIG" ]
then
print_help
exit 0
elif [ "test" == "$1" ]
then
opts="test"
if [ "" != "$2" ]
then
if [[ "$2" =~ \.bats$ ]]
then
opts="$opts/$2"
else
opts="$opts/$2.bats"
fi
fi
# Dummy test data
SMF_CONFIG="[email protected]:[email protected]:test-tset-testo-testi;[email protected]:[email protected]"
echo ">> Start mail server by test data: SMF_CONFIG=$SMF_CONFIG"
start_postfix
sleep 1
echo ">> exec bats $opts"
exec bats $opts
elif [ "sh" == "$1" ] || [ "bash" == "$1" ] || [ "shell" == "$1" ]
then
tty=$(tty)
if [ $? -eq 0 ]
then
echo ">> You are on TTY $tty."
echo ">> Enter shell mode."
exec /bin/bash
echo ">> Ahh!... Enter shell mode FAILED!"
exit 1
else
echo ">> Ahh!... Enter shell mode FAILED!"
echo ">> You need TTY to do this."
echo ">> By add --interactive --tty (or -ti) to Docker run params."
exit 1
fi
fi
if [ ! -f /entrypoint.sh ]
then
>&2 echo ">> you're not inside a valid docker container"
exit 1;
fi
start_postfix && test_running_env
echo
echo
echo ">> CONGRATULATIONS! System is UP and You are SET!"
echo ">> Powered by SMF - a Simple Mail Forwarder"
echo ">> View in DockerHub: https://hub.docker.com/r/zixia/simple-mail-forwarder"
echo
echo
# Init
echo ">> Init System for Servicing..."
exec /init
# ERROR: exec returned?!
ret=$?
echo ">> Exec ERROR: $ret"
sleep 7
exit $ret