Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start-mailserver.sh split #1820

Merged
merged 8 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The development workflow is the following:
2. Run `git submodule update --init --recursive`
2. Write the code that is needed :D
3. Add integration tests if necessary
4. Get the linters with `make install_linters`
4. Get the linters with `make install_linters` and install `jq` with the package manager of your OS
5. Use `make clean all` to build image locally and run tests (note that tests work on Linux **only**)
6. Document your improvements if necessary (e.g. if you introduced new environment variables, write the description in [`ENVIRONMENT.md`](./ENVIRONMENT.md))
7. [Commit][commit] and [sign your commit][gpg], push and create a pull-request to merge into `master`. Please **use the pull-request template** to provide a minimum of contextual information and make sure to meet the requirements of the checklist.
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ RUN \
apt-get -y --no-install-recommends install \
# A - D
altermime amavisd-new apt-transport-https arj binutils bzip2 \
ca-certificates cabextract clamav clamav-daemon cpio curl \
dovecot-core dovecot-imapd dovecot-ldap dovecot-lmtpd \
dovecot-managesieved dovecot-pop3d dovecot-sieve dovecot-solr \
dumb-init ca-certificates cabextract clamav clamav-daemon cpio curl \
dumb-init \
# E - O
ed fail2ban fetchmail file gamin gnupg gzip iproute2 iptables \
locales logwatch lhasa libdate-manip-perl liblz4-tool \
Expand Down Expand Up @@ -125,6 +126,7 @@ RUN \
COPY \
./target/bin/* \
./target/scripts/*.sh \
./target/scripts/startup/*.sh \
./target/docker-configomat/configomat.sh \
/usr/local/bin/

Expand Down
9 changes: 0 additions & 9 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ function _usage
Allows container access to the bind mount content that is private and
unshared with other containers on a SELinux-enabled host.

\e[94mOthers\e[39m
-h \e[31m|\e[39m help
Shows this help dialogue.

\e[31m[\e[38;5;214mSUB\e[31m]\e[38;5;214mCOMMANDS\e[39m
\e[94mCOMMAND\e[39m email \e[31m:=\e[39m
${0} email add <EMAIL ADDRESS> [<PASSWORD>]
Expand Down Expand Up @@ -301,11 +297,6 @@ function _main
USE_CONTAINER=true
;;

h )
_usage
return
;;

p )
case "${OPTARG}" in
/* ) WISHED_CONFIG_PATH="${OPTARG}" ;;
Expand Down
34 changes: 30 additions & 4 deletions target/bin/addalias
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,39 @@

DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}

function __usage
{
printf "\e[35mADDALIAS\e[31m(\e[93m8\e[31m)

\e[38;5;214mNAME\e[39m
addalias - add an email alias for an existing user

\e[38;5;214mSYNOPSIS\e[39m
./setup.sh alias add <EMAIL ADDRESS> <RECIPIENT>

\e[38;5;214mOPTIONS\e[39m
\e[94mGeneric Program Information\e[39m
help Print the usage information.

\e[38;5;214mEXAMPLES\e[39m
\e[37m./setup.sh alias add alias-for-me@domain.tld admin@domain.tld\e[39m
Add the alias alias-for-me@doamin.tld for the existing user
admin@domain.tld.

\e[38;5;214mEXIT STATUS\e[39m
Exit status is 0 if command was successful. If wrong arguments are provided
or arguments contain errors, the script will exit early with exit status 1.

"
}

[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

EMAIL="${1}"
RECIPIENT="${2}"

function usage { echo "Usage: addalias <alias@domain> <recipient@other>" ; }

[[ -z ${EMAIL} ]] && { usage ; errex "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { usage ; errex "Error: No recipient specified" ; }
[[ -z ${EMAIL} ]] && { __usage ; errex 'No alias specified' ; }
[[ -z ${RECIPIENT} ]] && { __usage ; errex 'No recipient specified' ; }

grep \
-qi "^$(escape "${EMAIL}")[a-zA-Z@.\ ]*$(escape "${RECIPIENT}")" \
Expand Down
34 changes: 30 additions & 4 deletions target/bin/addmailuser
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,40 @@

DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}

function __usage
{
printf "\e[35mADDMAILUSER\e[31m(\e[93m8\e[31m)

\e[38;5;214mNAME\e[39m
addmailuser - add an email address (i.e. a user)

\e[38;5;214mSYNOPSIS\e[39m
./setup.sh email add <EMAIL ADDRESS> [<PASSWORD>]

\e[38;5;214mOPTIONS\e[39m
\e[94mGeneric Program Information\e[39m
help Print the usage information.

\e[38;5;214mEXAMPLES\e[39m
\e[37m./setup.sh email add test@domain.tld\e[39m
Add the email account test@domain.tld. You will be prompted
to input a password afterwards since no password was supplied.

\e[38;5;214mEXIT STATUS\e[39m
Exit status is 0 if command was successful. If wrong arguments are provided
or arguments contain errors, the script will exit early with exit status 1.

"
}

[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

USER="${1}"
shift
PASSWD="${*}"

function usage { echo "Usage: addmailuser <user@domain> [<password>]" ; }

[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
[[ "${USER}" =~ .*\@.* ]] || { usage ; errex "username must include the domain" ; }
[[ -z ${USER} ]] && { __usage ; errex 'No username specified' ; }
[[ "${USER}" =~ .*\@.* ]] || { __usage ; errex 'Username must include the domain' ; }

# Protect config file with lock to avoid race conditions
touch "${DATABASE}"
Expand Down
38 changes: 29 additions & 9 deletions target/bin/addrelayhost
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@

DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-relaymap.cf}

function __usage
{
printf "\e[35mADDRELAYHOST\e[31m(\e[93m8\e[31m)

\e[38;5;214mNAME\e[39m
addrelayhost - add an relay host

\e[38;5;214mSYNOPSIS\e[39m
./setup.sh relay add-domain <DOMAIN> <HOST> [<PORT>]

\e[38;5;214mOPTIONS\e[39m
\e[94mGeneric Program Information\e[39m
help Print the usage information.

\e[38;5;214mEXIT STATUS\e[39m
Exit status is 0 if command was successful. If wrong arguments are provided
or arguments contain errors, the script will exit early with exit status 1.

"
}

[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

DOMAIN="${1}"
HOST="${2}"
PORT="${3}"

function usage { echo "Usage: addrelayhost <domain> <host> [<port>]" ; }

[[ -z ${DOMAIN} ]] && { usage ; errex "no domain specified" ; }
[[ -z ${HOST} ]] && { usage ; errex "no relay host specified" ; }

[[ -z ${DOMAIN} ]] && { __usage ; errex 'No domain specified' ; }
[[ -z ${HOST} ]] && { __usage ; errex 'No relay host specified' ; }
[[ -z ${PORT} ]] && PORT=25

if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null
then
# TODO check if fixed
# sed -i "s ^@${DOMAIN}.* @${DOMAIN}\t\t[${HOST}]:${PORT}" "${DATABASE}"
sed -i "s ^@""${DOMAIN}"".* ""@""${DOMAIN}""\t\t[""${HOST}""]:""${PORT}"" " "${DATABASE}"
sed -i \
"s|^@${DOMAIN}.*|@${DOMAIN}\t\t[${HOST}]:${PORT}|" \
"${DATABASE}"
else
echo -e "@${DOMAIN}\t\t[${HOST}]:${PORT}" >> "${DATABASE}"
echo -e "@${DOMAIN}\t\t[${HOST}]:${PORT}" >>"${DATABASE}"
fi
18 changes: 11 additions & 7 deletions target/bin/addsaslpassword
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@

DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-sasl-password.cf}

function __usage { echo "Usage: addsaslpassword <domain> <username> <password>" ; }

[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

DOMAIN="${1}"
USER="${2}"
PASSWD="${3}"

function usage { echo "Usage: addsaslpassword <domain> <username> <password>" ; }

[[ -z ${DOMAIN} ]] && { usage ; errex "no domain specified" ; }
[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
[[ -z ${DOMAIN} ]] && { __usage ; errex 'No domain specified' ; }
[[ -z ${USER} ]] && { __usage ; errex 'No username specified' ; }

if [[ -z ${PASSWD} ]]
then
read -r -s -p "Enter Password: " PASSWD
echo
[[ -z ${PASSWD} ]] && errex "Password must not be empty"
[[ -z ${PASSWD} ]] && errex 'Password must not be empty'
fi

if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null
then
sed -i "s ^@""${DOMAIN}"".* ""@""${DOMAIN}""\t\t""${USER}"":""${PASSWD}"" " "${DATABASE}"
sed -i \
"s|^@${DOMAIN}.*|@${DOMAIN}\t\t${USER}:${PASSWD}|" \
"${DATABASE}"
else
echo -e "@${DOMAIN}\t\t${USER}:${PASSWD}" >> "${DATABASE}"
echo -e "@${DOMAIN}\t\t${USER}:${PASSWD}" >>"${DATABASE}"
fi
14 changes: 9 additions & 5 deletions target/bin/delalias
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
EMAIL="${1}"
RECIPIENT="${2}"

function usage { echo "Usage: delalias <alias@domain> <recipient@other>" ; }
function __usage { echo "Usage: delalias <alias@domain> <recipient@other>" ; }

[[ -z ${EMAIL} ]] && { usage ; errex "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { usage ; errex "Error: No recipient specified" ; }
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

[[ -z ${EMAIL} ]] && { __usage ; errex "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { __usage ; errex "Error: No recipient specified" ; }
[[ -s ${DATABASE} ]] || exit 0

sed -i -e "/^${EMAIL} *${RECIPIENT}$/d" \
sed -i \
-e "/^${EMAIL} *${RECIPIENT}$/d" \
-e "/^${EMAIL}/s/,${RECIPIENT}//g" \
-e "/^${EMAIL}/s/${RECIPIENT},//g" "${DATABASE}"
-e "/^${EMAIL}/s/${RECIPIENT},//g" \
"${DATABASE}"
12 changes: 7 additions & 5 deletions target/bin/delquota
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
DATABASE=${DATABASE:-/tmp/docker-mailserver/dovecot-quotas.cf}
USER_DATABASE=${USER_DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}

USER="${1}"
function __usage { echo "Usage: delquota <username@domain>" ; }

[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

function usage { echo "Usage: delquota <username@domain>" ; }
USER="${1}"

[[ -z ${USER} ]] && { usage ; errex "No username specified" ; }
[[ "${USER}" =~ .*\@.* ]] || { usage ; errex "username must include the domain"; }
[[ -z ${USER} ]] && { __usage ; errex "No username specified" ; }
[[ "${USER}" =~ .*\@.* ]] || { __usage ; errex "Username must include the domain"; }

if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
then
usage
__usage
errex "user ${USER} does not exist"
fi

Expand Down
3 changes: 2 additions & 1 deletion target/bin/listalias
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
[[ -f ${DATABASE} ]] || errex "Error: No postfix-virtual.cf file"
[[ -s ${DATABASE} ]] || errex "Error: Empty postfix-virtual.cf - no aliases have been added"

( grep -v "^\s*$\|^\s*\#" "${DATABASE}" || true )
grep -v "^\s*$\|^\s*\#" "${DATABASE}"
exit 0
12 changes: 2 additions & 10 deletions target/bin/listmailuser
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#! /bin/bash

# ? This is done to ignore the message "Make sure not to read and write
# ? the same file in the same pipeline", which is a result of ${DATABASE}
# ? being used below. (This disables the message file-wide.)
# shellcheck disable=SC2094

# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh

Expand All @@ -13,8 +8,5 @@ DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
[[ -f ${DATABASE} ]] || errex "Error: No postfix-virtual.cf file"
[[ -s ${DATABASE} ]] || errex "Error: Empty postfix-virtual.cf - no aliases have been added"

# Lock database even though we are only reading
(
flock -e 200
( grep -v "^\s*$\|^\s*\#" "${DATABASE}" || true ) | awk -F '|' '{ print $1; }'
) 200< "${DATABASE}"
{ grep -v "^\s*$\|^\s*\#" "${DATABASE}" || true ; } | awk -F '|' '{ print $1; }'
exit 0
8 changes: 2 additions & 6 deletions target/bin/open-dkim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DOMAINS=

function __usage
{
printf "\e[35mOPEN-DKIM\e[31m(\e[93m8\e[31m)
printf "\e[35mOPEN-DKIM\e[31m(\e[93m8\e[31m)

\e[38;5;214mNAME\e[39m
open-dkim - configure DomainKeys Identified Mail (DKIM)
Expand Down Expand Up @@ -48,11 +48,7 @@ function __usage
"
}

if [[ ${1:-} == 'help' ]]
then
__usage
exit 0
fi
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }

while [[ ${#} -gt 0 ]]
do
Expand Down
12 changes: 6 additions & 6 deletions target/scripts/helper-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DMS_DEBUG="${DMS_DEBUG:=0}"

function errex
{
echo "${@}" 1>&2
echo -e "Error :: ${*}\nAborting." >&2
exit 1
}

Expand Down Expand Up @@ -149,13 +149,13 @@ function _populate_relayhost_map
sed -n '/^\s*[^#[:space:]]/ s/^[^@|]*@\([^|]\+\)|.*$/\1/p' /tmp/docker-mailserver/postfix-accounts.cf

[ -f /tmp/docker-mailserver/postfix-virtual.cf ] && sed -n '/^\s*[^#[:space:]]/ s/^\s*[^@[:space:]]*@\(\S\+\)\s.*/\1/p' /tmp/docker-mailserver/postfix-virtual.cf
} | while read -r domain
} | while read -r DOMAIN
do
# domain not already present *and* not ignored
if ! grep -q -e "^@${domain}\b" /etc/postfix/relayhost_map && ! grep -qs -e "^\s*@${domain}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf
# DOMAIN not already present *and* not ignored
if ! grep -q -e "^@${DOMAIN}\b" /etc/postfix/relayhost_map && ! grep -qs -e "^\s*@${DOMAIN}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf
then
_notify 'inf' "Adding relay mapping for ${domain}"
echo "@${domain} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
_notify 'inf' "Adding relay mapping for ${DOMAIN}"
echo "@${DOMAIN} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
fi
done
}
Expand Down