2020-11-29 13:14:23 +01:00
|
|
|
|
#! /bin/bash
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
# Wrapper for various setup scripts
|
|
|
|
|
# included in docker-mailserver
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
SCRIPT='SETUP'
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
set -euEo pipefail
|
|
|
|
|
trap '__log_err ${FUNCNAME[0]:-"?"} ${_:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
|
|
|
|
|
trap '_unset_vars || :' EXIT
|
|
|
|
|
|
|
|
|
|
function __log_err
|
|
|
|
|
{
|
|
|
|
|
local FUNC_NAME LINE EXIT_CODE
|
|
|
|
|
FUNC_NAME="${1} / ${2}"
|
|
|
|
|
LINE="${3}"
|
|
|
|
|
EXIT_CODE="${4}"
|
|
|
|
|
|
|
|
|
|
printf "\n––– \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
|
|
|
|
|
" – script = ${SCRIPT,,}.sh" \
|
|
|
|
|
" – function = ${FUNC_NAME}" \
|
|
|
|
|
" – line = ${LINE}" \
|
|
|
|
|
" – exit code = ${EXIT_CODE}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _unset_vars
|
|
|
|
|
{
|
|
|
|
|
unset CDIR CRI INFO IMAGE_NAME CONTAINER_NAME DEFAULT_CONFIG_PATH
|
|
|
|
|
unset USE_CONTAINER WISHED_CONFIG_PATH CONFIG_PATH VOLUME USE_TTY
|
|
|
|
|
unset SCRIPT USING_SELINUX
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _get_current_directory
|
|
|
|
|
{
|
|
|
|
|
if dirname "$(readlink -f "${0}")" &>/dev/null
|
|
|
|
|
then
|
|
|
|
|
CDIR="$(cd "$(dirname "$(readlink -f "${0}")")" && pwd)"
|
|
|
|
|
elif realpath -e -L "${0}" &>/dev/null
|
|
|
|
|
then
|
|
|
|
|
CDIR="$(realpath -e -L "${0}")"
|
|
|
|
|
CDIR="${CDIR%/setup.sh}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
2020-11-29 13:14:23 +01:00
|
|
|
|
}
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
CDIR="$(pwd)"
|
|
|
|
|
_get_current_directory
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
CRI=
|
|
|
|
|
INFO=
|
|
|
|
|
IMAGE_NAME=
|
|
|
|
|
CONTAINER_NAME='mail'
|
|
|
|
|
DEFAULT_CONFIG_PATH="${CDIR}/config"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
USE_CONTAINER=false
|
2020-11-29 13:14:23 +01:00
|
|
|
|
WISHED_CONFIG_PATH=
|
|
|
|
|
CONFIG_PATH=
|
|
|
|
|
VOLUME=
|
|
|
|
|
USE_TTY=
|
|
|
|
|
USING_SELINUX=
|
|
|
|
|
|
|
|
|
|
function _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."
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _update_config_path
|
|
|
|
|
{
|
|
|
|
|
if [[ -n ${CONTAINER_NAME} ]]
|
|
|
|
|
then
|
|
|
|
|
VOLUME=$(${CRI} inspect "${CONTAINER_NAME}" \
|
2020-07-14 11:07:33 +02:00
|
|
|
|
--format="{{range .Mounts}}{{ println .Source .Destination}}{{end}}" | \
|
|
|
|
|
grep "/tmp/docker-mailserver$" 2>/dev/null)
|
|
|
|
|
fi
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
if [[ -n ${VOLUME} ]]
|
|
|
|
|
then
|
|
|
|
|
CONFIG_PATH=$(echo "${VOLUME}" | awk '{print $1}')
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _inspect
|
|
|
|
|
{
|
|
|
|
|
if _docker_image_exists "${IMAGE_NAME}"
|
|
|
|
|
then
|
|
|
|
|
echo "Image: ${IMAGE_NAME}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
else
|
2020-11-29 13:14:23 +01:00
|
|
|
|
echo "Image: '${IMAGE_NAME}' can’t be found."
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
2020-11-29 13:14:23 +01:00
|
|
|
|
|
|
|
|
|
if [[ -n ${CONTAINER_NAME} ]]
|
|
|
|
|
then
|
|
|
|
|
echo "Container: ${CONTAINER_NAME}"
|
|
|
|
|
echo "Config mount: ${CONFIG_PATH}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
else
|
|
|
|
|
echo "Container: Not running, please start docker-mailserver."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _usage
|
|
|
|
|
{
|
|
|
|
|
echo "${SCRIPT,,}.sh Bootstrapping Script
|
|
|
|
|
|
|
|
|
|
Usage: ${0} [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
-p PATH Config folder path (default: ${CDIR}/config)
|
|
|
|
|
|
|
|
|
|
-h Show this help dialogue
|
|
|
|
|
|
|
|
|
|
-z Allow container access to the bind mount content
|
|
|
|
|
that is shared among multiple containers
|
|
|
|
|
on a SELinux-enabled host.
|
|
|
|
|
|
|
|
|
|
-Z Allow container access to the bind mount content
|
|
|
|
|
that is private and unshared with other containers
|
|
|
|
|
on a SELinux-enabled host.
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
SUBCOMMANDS:
|
|
|
|
|
|
|
|
|
|
email:
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${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
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
alias:
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${0} alias add <email> <recipient>
|
|
|
|
|
${0} alias del <email> <recipient>
|
|
|
|
|
${0} alias list
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
quota:
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${0} quota set <email> [<quota>]
|
|
|
|
|
${0} quota del <email>
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
config:
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${0} config dkim <keysize> (default: 2048)
|
|
|
|
|
${0} config ssl <fqdn>
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
relay:
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${0} relay add-domain <domain> <host> [<port>]
|
|
|
|
|
${0} relay add-auth <domain> <username> [<password>]
|
|
|
|
|
${0} relay exclude-domain <domain>
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
|
|
|
|
debug:
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${0} debug fetchmail
|
|
|
|
|
${0} debug fail2ban [<unban> <ip-address>]
|
|
|
|
|
${0} debug show-mail-logs
|
|
|
|
|
${0} debug inspect
|
|
|
|
|
${0} debug login <commands>
|
|
|
|
|
|
|
|
|
|
help: Show this help dialogue
|
|
|
|
|
|
2020-07-14 11:07:33 +02:00
|
|
|
|
"
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _docker_image_exists
|
|
|
|
|
{
|
|
|
|
|
if ${CRI} history -q "${1}" >/dev/null 2>&1
|
|
|
|
|
then
|
2020-07-14 11:07:33 +02:00
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _docker_image
|
|
|
|
|
{
|
|
|
|
|
if ${USE_CONTAINER}
|
|
|
|
|
then
|
|
|
|
|
# reuse existing container specified on command line
|
|
|
|
|
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
else
|
2020-11-29 13:14:23 +01:00
|
|
|
|
# start temporary container with specified image
|
|
|
|
|
if ! _docker_image_exists "${IMAGE_NAME}"
|
|
|
|
|
then
|
|
|
|
|
echo "Image '${IMAGE_NAME}' not found. Pulling ..."
|
|
|
|
|
${CRI} pull "${IMAGE_NAME}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
${CRI} run --rm \
|
|
|
|
|
-v "${CONFIG_PATH}":/tmp/docker-mailserver"${USING_SELINUX}" \
|
|
|
|
|
"${USE_TTY}" "${IMAGE_NAME}" "${@}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _docker_container
|
|
|
|
|
{
|
|
|
|
|
if [[ -n ${CONTAINER_NAME} ]]
|
|
|
|
|
then
|
|
|
|
|
${CRI} exec "${USE_TTY}" "${CONTAINER_NAME}" "${@}"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
else
|
|
|
|
|
echo "The docker-mailserver is not running!"
|
2020-11-29 13:14:23 +01:00
|
|
|
|
exit 5
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
function _main
|
|
|
|
|
{
|
|
|
|
|
if [[ -n $(command -v docker) ]]
|
|
|
|
|
then
|
|
|
|
|
CRI=docker
|
|
|
|
|
elif [[ -n $(command -v podman) ]]
|
|
|
|
|
then
|
|
|
|
|
CRI=podman
|
|
|
|
|
_check_root
|
|
|
|
|
else
|
|
|
|
|
echo "No supported Container Runtime Interface detected."
|
|
|
|
|
exit 10
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
INFO=$(${CRI} ps \
|
|
|
|
|
--no-trunc \
|
|
|
|
|
--format "{{.Image}};{{.Names}}" \
|
|
|
|
|
--filter label=org.label-schema.name="docker-mailserver" | \
|
|
|
|
|
tail -1)
|
|
|
|
|
|
|
|
|
|
IMAGE_NAME=${INFO%;*}
|
|
|
|
|
CONTAINER_NAME=${INFO#*;}
|
|
|
|
|
|
|
|
|
|
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
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
if tty -s
|
|
|
|
|
then
|
|
|
|
|
USE_TTY="-ti"
|
|
|
|
|
fi
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
local OPTIND
|
|
|
|
|
while getopts ":c:i:p:hzZ" OPT
|
|
|
|
|
do
|
|
|
|
|
case ${OPT} in
|
|
|
|
|
i ) IMAGE_NAME="${OPTARG}" ;;
|
|
|
|
|
z ) USING_SELINUX=":z" ;;
|
|
|
|
|
Z ) USING_SELINUX=":Z" ;;
|
|
|
|
|
c )
|
|
|
|
|
# container specified, connect to running instance
|
|
|
|
|
CONTAINER_NAME="${OPTARG}"
|
|
|
|
|
USE_CONTAINER=true
|
2020-07-14 11:07:33 +02:00
|
|
|
|
;;
|
2020-11-29 13:14:23 +01:00
|
|
|
|
|
|
|
|
|
h )
|
2020-07-14 11:07:33 +02:00
|
|
|
|
_usage
|
2020-11-29 13:14:23 +01:00
|
|
|
|
return
|
2020-07-14 11:07:33 +02:00
|
|
|
|
;;
|
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
p )
|
|
|
|
|
case "${OPTARG}" in
|
|
|
|
|
/* ) WISHED_CONFIG_PATH="${OPTARG}" ;;
|
|
|
|
|
* ) WISHED_CONFIG_PATH="${CDIR}/${OPTARG}" ;;
|
|
|
|
|
esac
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
if [[ ! -d ${WISHED_CONFIG_PATH} ]]
|
|
|
|
|
then
|
|
|
|
|
echo "Directory doesn't exist"
|
2020-07-14 11:07:33 +02:00
|
|
|
|
_usage
|
2020-11-29 13:14:23 +01:00
|
|
|
|
exit 40
|
2020-07-14 11:07:33 +02:00
|
|
|
|
fi
|
|
|
|
|
;;
|
2020-11-29 13:14:23 +01:00
|
|
|
|
|
|
|
|
|
* )
|
|
|
|
|
echo "Invalid option: -${OPTARG}" >&2
|
2020-07-14 11:07:33 +02:00
|
|
|
|
;;
|
2020-11-29 13:14:23 +01:00
|
|
|
|
|
2020-07-14 11:07:33 +02:00
|
|
|
|
esac
|
2020-11-29 13:14:23 +01:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
shift $(( OPTIND - 1 ))
|
|
|
|
|
|
|
|
|
|
if [[ -z ${WISHED_CONFIG_PATH} ]]
|
|
|
|
|
then
|
|
|
|
|
# no wished config path
|
|
|
|
|
_update_config_path
|
|
|
|
|
|
|
|
|
|
if [[ -z ${CONFIG_PATH} ]]
|
|
|
|
|
then
|
|
|
|
|
CONFIG_PATH=${DEFAULT_CONFIG_PATH}
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
CONFIG_PATH=${WISHED_CONFIG_PATH}
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 "${1}" "${2}" ;;
|
|
|
|
|
del ) shift ; _docker_image delalias "${1}" "${2}" ;;
|
|
|
|
|
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:-2048}" ;;
|
|
|
|
|
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 ; exit 1 ;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
help ) _usage ;;
|
|
|
|
|
* ) _usage ; exit 1 ;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
2020-07-14 11:07:33 +02:00
|
|
|
|
|
2020-11-29 13:14:23 +01:00
|
|
|
|
_main "${@}"
|