forked from docker-mailserver/docker-mailserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·345 lines (302 loc) · 6.59 KB
/
setup.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/sh
##
# Wrapper for various setup scripts included in the docker-mailserver
#
CRI=
_check_root() {
if [[ $EUID -ne 0 ]]; then
echo "Curently docker-mailserver doesn't support podman's rootless mode, please run this script as root user."
exit 1
fi
}
if [ -z "$CRI" ]; then
if [ ! -z "$(command -v docker)" ]; then
CRI=docker
elif [ ! -z "$(command -v podman)" ]; then
CRI=podman
_check_root
else
echo "No Support Container Runtime Interface Detected."
exit 1
fi
fi
INFO=$($CRI ps \
--no-trunc \
--format="{{.Image}} {{.Names}} {{.Command}}" | \
grep "supervisord -c /etc/supervisor/supervisord.conf")
IMAGE_NAME=$(echo $INFO | awk '{print $1}')
CONTAINER_NAME=$(echo $INFO | awk '{print $2}')
DEFAULT_CONFIG_PATH="$(pwd)/config"
USE_CONTAINER=false
_update_config_path() {
if [ ! -z "$CONTAINER_NAME" ]; then
VOLUME=$(docker inspect $CONTAINER_NAME \
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
grep "/tmp/docker-mailserver$" 2>/dev/null)
fi
if [ ! -z "$VOLUME" ]; then
CONFIG_PATH=$(echo $VOLUME | awk '{print $1}')
fi
}
if [ -z "$IMAGE_NAME" ]; then
if [ "$CRI" = "docker" ]; then
IMAGE_NAME=tvial/docker-mailserver:latest
elif [ "$CRI" = "podman" ]; then
IMAGE_NAME=docker.io/tvial/docker-mailserver:latest
fi
fi
_inspect() {
if _docker_image_exists "$IMAGE_NAME"; then
echo "Image: $IMAGE_NAME"
else
echo "Image: '$IMAGE_NAME' can’t be found."
fi
if [ -n "$CONTAINER_NAME" ]; then
echo "Container: $CONTAINER_NAME"
echo "Config mount: $CONFIG_PATH"
else
echo "Container: Not running, please start docker-mailserver."
fi
}
_usage() {
echo "Usage: $0 [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
OPTIONS:
-i IMAGE_NAME The name of the docker-mailserver image, by default
'tvial/docker-mailserver:latest' for docker, and
'docker.io/tvial/docker-mailserver:latest' for podman.
-c CONTAINER_NAME The name of the running container.
-p PATH config folder path (default: $(pwd)/config)
SUBCOMMANDS:
email:
$0 email add <email> [<password>]
$0 email update <email> [<password>]
$0 email del <email>
$0 email restrict <add|del|list> <send|receive> [<email>]
$0 email list
alias:
$0 alias add <email> <recipient>
$0 alias del <email> <recipient>
$0 alias list
quota:
$0 quota set <email> [<quota>]
$0 quota del <email>
config:
$0 config dkim <keysize> (default: 2048)
$0 config ssl <fqdn>
relay:
$0 relay add-domain <domain> <host> [<port>]
$0 relay add-auth <domain> <username> [<password>]
$0 relay exclude-domain <domain>
debug:
$0 debug fetchmail
$0 debug fail2ban [<unban> <ip-address>]
$0 debug show-mail-logs
$0 debug inspect
$0 debug login <commands>
"
exit 1
}
_docker_image_exists() {
if ${CRI} history -q "$1" >/dev/null 2>&1; then
return 0
else
return 1
fi
}
if tty -s ; then
USE_TTY="-ti"
fi
_docker_image() {
if [ "$USE_CONTAINER" = true ]; then
# Reuse existing container specified on command line
${CRI} exec ${USE_TTY} "$CONTAINER_NAME" "$@"
else
# Start temporary container with specified image
if ! _docker_image_exists "$IMAGE_NAME"; then
echo "Image '$IMAGE_NAME' not found. Pulling ..."
${CRI} pull "$IMAGE_NAME"
fi
${CRI} run \
--rm \
-v "$CONFIG_PATH":/tmp/docker-mailserver \
${USE_TTY} "$IMAGE_NAME" $@
fi
}
_docker_container() {
if [ -n "$CONTAINER_NAME" ]; then
${CRI} exec ${USE_TTY} "$CONTAINER_NAME" "$@"
else
echo "The docker-mailserver is not running!"
exit 1
fi
}
while getopts ":c:i:p:" OPT; do
case $OPT in
c)
CONTAINER_NAME="$OPTARG"
USE_CONTAINER=true # Container specified, connect to running instance
;;
i)
IMAGE_NAME="$OPTARG"
;;
p)
case "$OPTARG" in
/*)
WISHED_CONFIG_PATH="$OPTARG"
;;
*)
WISHED_CONFIG_PATH="$(pwd)/$OPTARG"
;;
esac
if [ ! -d "$WISHED_CONFIG_PATH" ]; then
echo "Directory doesn't exist"
_usage
exit 1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [ ! -n "$WISHED_CONFIG_PATH" ]; then
# no wished config path
_update_config_path
if [ ! -n "$CONFIG_PATH" ]; then
CONFIG_PATH=$DEFAULT_CONFIG_PATH
fi
else
CONFIG_PATH=$WISHED_CONFIG_PATH
fi
shift $((OPTIND-1))
case $1 in
email)
shift
case $1 in
add)
shift
_docker_image addmailuser $@
;;
update)
shift
_docker_image updatemailuser $@
;;
del)
shift
_docker_image delmailuser $@
;;
restrict)
shift
_docker_container restrict-access $@
;;
list)
_docker_image listmailuser
;;
*)
_usage
;;
esac
;;
alias)
shift
case $1 in
add)
shift
_docker_image addalias $@
;;
del)
shift
_docker_image delalias $@
;;
list)
shift
_docker_image listalias $@
;;
*)
_usage
;;
esac
;;
quota)
shift
case $1 in
set)
shift
_docker_image setquota $@
;;
del)
shift
_docker_image delquota $@
;;
*)
_usage
;;
esac
;;
config)
shift
case $1 in
dkim)
_docker_image generate-dkim-config $2
;;
ssl)
_docker_image generate-ssl-certificate "$2"
;;
*)
_usage
;;
esac
;;
relay)
shift
case $1 in
add-domain)
shift
_docker_image addrelayhost $@
;;
add-auth)
shift
_docker_image addsaslpassword $@
;;
exclude-domain)
shift
_docker_image excluderelaydomain $@
;;
*)
_usage
;;
esac
;;
debug)
shift
case $1 in
fetchmail)
_docker_image debug-fetchmail
;;
fail2ban)
shift
_docker_container fail2ban $@
;;
show-mail-logs)
_docker_container cat /var/log/mail/mail.log
;;
inspect)
_inspect
;;
login)
shift
if [ -z "$1" ]; then
_docker_container /bin/bash
else
_docker_container /bin/bash -c "$@"
fi
;;
*)
_usage
;;
esac
;;
*)
_usage
;;
esac