2016-08-28 19:10:25 +00:00
|
|
|
|
#! /bin/sh
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Wrapper for various setup scripts included in the docker-mailserver
|
2016-09-01 10:10:23 +00:00
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
INFO=$(docker ps \
|
|
|
|
|
--no-trunc \
|
|
|
|
|
--format="{{.Image}}\t{{.Names}}\t{{.Command}}" | \
|
2017-08-07 15:39:40 +00:00
|
|
|
|
grep "/bin/sh -c 'supervisord -c /etc/supervisor/supervisord.conf'")
|
2016-09-01 10:10:23 +00:00
|
|
|
|
|
|
|
|
|
IMAGE_NAME=$(echo $INFO | awk '{print $1}')
|
|
|
|
|
CONTAINER_NAME=$(echo $INFO | awk '{print $2}')
|
2018-02-11 17:26:46 +00:00
|
|
|
|
CONFIG_PATH="$(pwd)/config"
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
if [ -z "$IMAGE_NAME" ]; then
|
|
|
|
|
IMAGE_NAME=tvial/docker-mailserver:latest
|
2016-08-28 19:10:25 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_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"
|
|
|
|
|
else
|
|
|
|
|
echo "Container: Not running, please start docker-mailserver."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
_usage() {
|
2016-09-01 10:10:23 +00:00
|
|
|
|
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'.
|
|
|
|
|
-c CONTAINER_NAME The name of the running container.
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
2018-02-11 17:26:46 +00:00
|
|
|
|
-p PATH config folder path (default: $(pwd)/config)
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
SUBCOMMANDS:
|
|
|
|
|
|
|
|
|
|
email:
|
|
|
|
|
|
2018-02-07 20:33:07 +00:00
|
|
|
|
$0 email add <email> [<password>]
|
|
|
|
|
$0 email update <email> [<password>]
|
2016-08-28 19:10:25 +00:00
|
|
|
|
$0 email del <email>
|
2018-02-07 20:33:07 +00:00
|
|
|
|
$0 email restrict <add|del|list> <send|receive> [<email>]
|
2016-08-29 11:44:36 +00:00
|
|
|
|
$0 email list
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
2017-04-17 16:27:28 +00:00
|
|
|
|
alias:
|
|
|
|
|
$0 alias add <email> <recipient>
|
|
|
|
|
$0 alias del <email> <recipient>
|
|
|
|
|
$0 alias list
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
config:
|
|
|
|
|
|
|
|
|
|
$0 config dkim
|
|
|
|
|
$0 config ssl
|
|
|
|
|
|
|
|
|
|
debug:
|
|
|
|
|
|
2016-08-29 17:03:45 +00:00
|
|
|
|
$0 debug fetchmail
|
2018-02-11 17:26:46 +00:00
|
|
|
|
$0 debug fail2ban [<unban> <ip-address>]
|
2016-09-01 10:10:23 +00:00
|
|
|
|
$0 debug show-mail-logs
|
|
|
|
|
$0 debug inspect
|
|
|
|
|
$0 debug login <commands>
|
2016-08-28 19:10:25 +00:00
|
|
|
|
"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image_exists() {
|
|
|
|
|
if docker history -q "$1" >/dev/null 2>&1; then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_docker_image() {
|
|
|
|
|
if ! _docker_image_exists "$IMAGE_NAME"; then
|
|
|
|
|
echo "Image '$IMAGE_NAME' not found. Pulling ..."
|
|
|
|
|
docker pull "$IMAGE_NAME"
|
|
|
|
|
fi
|
|
|
|
|
docker run \
|
|
|
|
|
--rm \
|
2018-02-11 17:26:46 +00:00
|
|
|
|
-v "$CONFIG_PATH":/tmp/docker-mailserver \
|
2016-09-01 10:10:23 +00:00
|
|
|
|
-ti "$IMAGE_NAME" $@
|
2016-08-28 19:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_container() {
|
|
|
|
|
if [ -n "$CONTAINER_NAME" ]; then
|
|
|
|
|
docker exec -ti "$CONTAINER_NAME" $@
|
|
|
|
|
else
|
|
|
|
|
echo "The docker-mailserver is not running!"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 17:26:46 +00:00
|
|
|
|
while getopts ":c:i:p:" OPT; do
|
2016-09-01 10:10:23 +00:00
|
|
|
|
case $OPT in
|
|
|
|
|
c)
|
|
|
|
|
CONTAINER_NAME="$OPTARG"
|
|
|
|
|
;;
|
|
|
|
|
i)
|
|
|
|
|
IMAGE_NAME="$OPTARG"
|
|
|
|
|
;;
|
2018-02-11 17:26:46 +00:00
|
|
|
|
p)
|
|
|
|
|
case "$OPTARG" in
|
|
|
|
|
/*)
|
|
|
|
|
CONFIG_PATH="$OPTARG"
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
CONFIG_PATH="$(pwd)/$OPTARG"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
if [ ! -d "$CONFIG_PATH" ]; then
|
|
|
|
|
echo "Directory doesn't exist"
|
|
|
|
|
_usage
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
;;
|
2016-09-01 10:10:23 +00:00
|
|
|
|
\?)
|
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
case $1 in
|
|
|
|
|
|
|
|
|
|
email)
|
|
|
|
|
shift
|
|
|
|
|
case $1 in
|
|
|
|
|
add)
|
|
|
|
|
shift
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image addmailuser $@
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
2016-12-21 19:12:05 +00:00
|
|
|
|
update)
|
|
|
|
|
shift
|
2017-01-09 15:27:20 +00:00
|
|
|
|
_docker_image updatemailuser $@
|
2016-12-21 19:12:05 +00:00
|
|
|
|
;;
|
2016-08-28 19:10:25 +00:00
|
|
|
|
del)
|
|
|
|
|
shift
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image delmailuser $@
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
2018-02-07 20:33:07 +00:00
|
|
|
|
restrict)
|
|
|
|
|
shift
|
|
|
|
|
_docker_image restrict-access $@
|
2018-02-11 17:26:46 +00:00
|
|
|
|
;;
|
2016-08-29 11:44:36 +00:00
|
|
|
|
list)
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image listmailuser
|
2016-08-29 11:44:36 +00:00
|
|
|
|
;;
|
2016-08-28 19:10:25 +00:00
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
2017-04-17 16:27:28 +00:00
|
|
|
|
alias)
|
|
|
|
|
shift
|
|
|
|
|
case $1 in
|
|
|
|
|
add)
|
|
|
|
|
shift
|
|
|
|
|
_docker_image addalias $@
|
|
|
|
|
;;
|
|
|
|
|
del)
|
|
|
|
|
shift
|
|
|
|
|
_docker_image delalias $@
|
|
|
|
|
;;
|
|
|
|
|
list)
|
|
|
|
|
shift
|
|
|
|
|
_docker_image listalias $@
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
config)
|
|
|
|
|
shift
|
|
|
|
|
case $1 in
|
|
|
|
|
dkim)
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image generate-dkim-config
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
ssl)
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image generate-ssl-certificate
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
debug)
|
|
|
|
|
shift
|
|
|
|
|
case $1 in
|
|
|
|
|
fetchmail)
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image debug-fetchmail
|
|
|
|
|
;;
|
2018-01-31 21:25:29 +00:00
|
|
|
|
fail2ban)
|
|
|
|
|
shift
|
|
|
|
|
JAILS=$(_docker_container fail2ban-client status | grep "Jail list" | cut -f2- | sed 's/,//g')
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
|
IP_COUNT=0
|
|
|
|
|
for JAIL in $JAILS; do
|
|
|
|
|
BANNED_IP=$(_docker_container iptables -L f2b-$JAIL -n | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -v '0.0.0.0')
|
|
|
|
|
if [ -n "$BANNED_IP" ]; then
|
|
|
|
|
BANNED_IP=$(echo $BANNED_IP | sed -e 's/\n/,/g')
|
|
|
|
|
echo "Banned in $JAIL: $BANNED_IP"
|
|
|
|
|
IP_COUNT=$((IP_COUNT+1))
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if [ "$IP_COUNT" -eq 0 ]; then
|
|
|
|
|
echo "No IPs have been banned"
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
case $1 in
|
|
|
|
|
unban)
|
|
|
|
|
shift
|
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
|
for JAIL in $JAILS; do
|
|
|
|
|
RESULT=`_docker_container fail2ban-client set $JAIL unbanip $@`
|
|
|
|
|
case "$RESULT" in
|
|
|
|
|
*"is not banned"*) ;;
|
|
|
|
|
*"NOK"*) ;;
|
2018-02-11 17:26:46 +00:00
|
|
|
|
*) echo -n "unbanned IP from $JAIL: "
|
2018-01-31 21:25:29 +00:00
|
|
|
|
echo "$RESULT";;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
else
|
|
|
|
|
echo "You need to specify an IP address. Run \"./setup.sh debug fail2ban\" to get a list of banned IP addresses."
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
fi
|
|
|
|
|
;;
|
2016-09-01 10:10:23 +00:00
|
|
|
|
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
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|