2019-11-22 14:42:25 +00:00
|
|
|
|
#!/usr/bin/env bash
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Wrapper for various setup scripts included in the docker-mailserver
|
2016-09-01 10:10:23 +00:00
|
|
|
|
#
|
|
|
|
|
|
2019-11-22 14:42:25 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2019-11-22 15:10:42 +00:00
|
|
|
|
|
2019-11-22 14:42:25 +00:00
|
|
|
|
if [ -z "${CRI}" ]; then
|
|
|
|
|
if [ ! -z "$(command -v docker)" ]; then
|
|
|
|
|
CRI=docker
|
|
|
|
|
else if [ ! -z "$(command -v podman)" ]; then
|
|
|
|
|
CRI=podman
|
|
|
|
|
_check_root
|
2019-11-22 15:10:42 +00:00
|
|
|
|
else
|
|
|
|
|
echo "No Support Container Runtime Interface Detected."
|
2019-11-22 14:42:25 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
INFO=$(${CRI} ps \
|
2016-09-01 10:10:23 +00:00
|
|
|
|
--no-trunc \
|
2019-11-22 14:42:25 +00:00
|
|
|
|
--format="{{.Image}} {{.Names}} {{.Command}}" | \
|
2018-11-04 19:23:50 +00:00
|
|
|
|
grep "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-04-28 08:27:42 +00:00
|
|
|
|
DEFAULT_CONFIG_PATH="$(pwd)/config"
|
2019-07-28 12:33:52 +00:00
|
|
|
|
USE_CONTAINER=false
|
2018-04-28 08:27:42 +00:00
|
|
|
|
|
|
|
|
|
_update_config_path() {
|
2019-11-22 14:42:25 +00:00
|
|
|
|
VOLUME=$(${CRI} inspect $CONTAINER_NAME \
|
2018-04-28 08:27:42 +00:00
|
|
|
|
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
|
|
|
|
|
grep "/tmp/docker-mailserver$" 2>/dev/null)
|
|
|
|
|
|
|
|
|
|
if [ ! -z "$VOLUME" ]; then
|
|
|
|
|
CONFIG_PATH=$(echo $VOLUME | awk '{print $1}')
|
|
|
|
|
fi
|
|
|
|
|
}
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
if [ -z "$IMAGE_NAME" ]; then
|
2019-11-22 14:42:25 +00:00
|
|
|
|
if [ "$CRI" = "docker" ]; then
|
|
|
|
|
IMAGE_NAME=tvial/docker-mailserver:latest
|
|
|
|
|
else if [ "$CRI" = "podman" ]; then
|
|
|
|
|
IMAGE_NAME=docker.io/tvial/docker-mailserver:latest
|
|
|
|
|
fi
|
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"
|
2018-04-28 08:27:42 +00:00
|
|
|
|
echo "Config mount: $CONFIG_PATH"
|
2016-09-01 10:10:23 +00:00
|
|
|
|
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
|
2019-11-22 14:42:25 +00:00
|
|
|
|
'tvial/docker-mailserver:latest' for docker, and
|
|
|
|
|
'docker.io/tvial/docker-mailserver:latest' for podman.
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
-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:
|
|
|
|
|
|
2018-03-03 08:20:23 +00:00
|
|
|
|
$0 config dkim <keysize> (default: 2048)
|
2019-07-29 09:14:36 +00:00
|
|
|
|
$0 config ssl <fqdn>
|
2016-08-28 19:10:25 +00:00
|
|
|
|
|
2018-04-02 08:45:58 +00:00
|
|
|
|
relay:
|
|
|
|
|
|
|
|
|
|
$0 relay add-domain <domain> <host> [<port>]
|
|
|
|
|
$0 relay add-auth <domain> <username> [<password>]
|
|
|
|
|
$0 relay exclude-domain <domain>
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
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() {
|
2019-11-22 14:42:25 +00:00
|
|
|
|
if ${CRI} history -q "$1" >/dev/null 2>&1; then
|
2016-09-01 10:10:23 +00:00
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 08:43:04 +00:00
|
|
|
|
if [ -t 1 ] ; then
|
|
|
|
|
USE_TTY="-ti"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_image() {
|
2019-07-28 12:33:52 +00:00
|
|
|
|
if [ "$USE_CONTAINER" = true ]; then
|
|
|
|
|
# Reuse existing container specified on command line
|
2019-11-22 14:42:25 +00:00
|
|
|
|
${CRI} exec ${USE_TTY} "$CONTAINER_NAME" "$@"
|
2019-07-28 12:33:52 +00:00
|
|
|
|
else
|
|
|
|
|
# Start temporary container with specified image
|
|
|
|
|
if ! _docker_image_exists "$IMAGE_NAME"; then
|
|
|
|
|
echo "Image '$IMAGE_NAME' not found. Pulling ..."
|
2019-11-22 14:42:25 +00:00
|
|
|
|
${CRI} pull "$IMAGE_NAME"
|
2019-07-28 12:33:52 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2019-11-22 14:42:25 +00:00
|
|
|
|
${CRI} run \
|
2016-09-01 10:10:23 +00:00
|
|
|
|
--rm \
|
2018-02-11 17:26:46 +00:00
|
|
|
|
-v "$CONFIG_PATH":/tmp/docker-mailserver \
|
2019-10-21 08:43:04 +00:00
|
|
|
|
${USE_TTY} "$IMAGE_NAME" $@
|
2019-07-28 12:33:52 +00:00
|
|
|
|
fi
|
2016-08-28 19:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_container() {
|
|
|
|
|
if [ -n "$CONTAINER_NAME" ]; then
|
2019-11-22 14:42:25 +00:00
|
|
|
|
${CRI} exec ${USE_TTY} "$CONTAINER_NAME" "$@"
|
2016-09-01 10:10:23 +00:00
|
|
|
|
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"
|
2019-07-28 12:33:52 +00:00
|
|
|
|
USE_CONTAINER=true # Container specified, connect to running instance
|
2016-09-01 10:10:23 +00:00
|
|
|
|
;;
|
|
|
|
|
i)
|
|
|
|
|
IMAGE_NAME="$OPTARG"
|
|
|
|
|
;;
|
2018-02-11 17:26:46 +00:00
|
|
|
|
p)
|
|
|
|
|
case "$OPTARG" in
|
|
|
|
|
/*)
|
2018-04-28 08:27:42 +00:00
|
|
|
|
WISHED_CONFIG_PATH="$OPTARG"
|
2018-02-11 17:26:46 +00:00
|
|
|
|
;;
|
|
|
|
|
*)
|
2018-04-28 08:27:42 +00:00
|
|
|
|
WISHED_CONFIG_PATH="$(pwd)/$OPTARG"
|
2018-02-11 17:26:46 +00:00
|
|
|
|
;;
|
|
|
|
|
esac
|
2018-04-28 08:27:42 +00:00
|
|
|
|
if [ ! -d "$WISHED_CONFIG_PATH" ]; then
|
2018-02-11 17:26:46 +00:00
|
|
|
|
echo "Directory doesn't exist"
|
|
|
|
|
_usage
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
;;
|
2016-09-01 10:10:23 +00:00
|
|
|
|
\?)
|
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2018-04-28 08:27:42 +00:00
|
|
|
|
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
|
|
|
|
|
|
2016-09-01 10:10:23 +00:00
|
|
|
|
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
|
2018-02-18 12:29:43 +00:00
|
|
|
|
_docker_container 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)
|
2018-03-03 08:20:23 +00:00
|
|
|
|
_docker_image generate-dkim-config $2
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
ssl)
|
2019-07-29 09:14:36 +00:00
|
|
|
|
_docker_image generate-ssl-certificate "$2"
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
2018-04-02 08:45:58 +00:00
|
|
|
|
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
|
|
|
|
|
;;
|
|
|
|
|
|
2016-08-28 19:10:25 +00:00
|
|
|
|
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
|
2018-02-13 07:31:12 +00:00
|
|
|
|
_docker_container fail2ban $@
|
2018-01-31 21:25:29 +00:00
|
|
|
|
;;
|
2016-09-01 10:10:23 +00:00
|
|
|
|
show-mail-logs)
|
|
|
|
|
_docker_container cat /var/log/mail/mail.log
|
|
|
|
|
;;
|
|
|
|
|
inspect)
|
|
|
|
|
_inspect
|
|
|
|
|
;;
|
|
|
|
|
login)
|
|
|
|
|
shift
|
2019-10-21 08:43:04 +00:00
|
|
|
|
if [ -z "$1" ]; then
|
2016-09-01 10:10:23 +00:00
|
|
|
|
_docker_container /bin/bash
|
|
|
|
|
else
|
|
|
|
|
_docker_container /bin/bash -c "$@"
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
2016-08-28 19:10:25 +00:00
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
*)
|
|
|
|
|
_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|