2022-10-17 08:40:09 +00:00
|
|
|
#!/bin/bash
|
2018-02-07 20:33:07 +00:00
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2018-02-07 20:33:07 +00:00
|
|
|
|
2023-02-18 15:52:42 +00:00
|
|
|
COMMAND=${1:-}
|
|
|
|
DIRECTION=${2:-}
|
|
|
|
DIRECTION=${DIRECTION,,} # make lowercase
|
|
|
|
USER=${3:-}
|
2018-02-07 20:33:07 +00:00
|
|
|
|
2023-02-18 15:52:42 +00:00
|
|
|
function __usage { _log 'info' "Usage: ${0} <add|del|list> <send|receive> [<email@domain.com>]" ; }
|
2018-02-07 20:33:07 +00:00
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${DIRECTION} =~ ^(send|receive)$ ]]; then
|
2023-02-18 15:52:42 +00:00
|
|
|
DATABASE="/tmp/docker-mailserver/postfix-${DIRECTION}-access.cf"
|
|
|
|
else
|
|
|
|
__usage
|
|
|
|
_exit_with_error "Unknown or missing second parameter '${DIRECTION}' - specify 'send' or 'receive'"
|
|
|
|
fi
|
2018-02-07 20:33:07 +00:00
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -z ${USER} ]] && [[ ${COMMAND} != list ]]; then
|
2023-02-18 15:52:42 +00:00
|
|
|
read -r -p 'Provide a username: ' USER
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z ${USER} ]] && _exit_with_error 'User must not be empty'
|
2018-02-07 20:33:07 +00:00
|
|
|
fi
|
|
|
|
|
2023-02-18 15:52:42 +00:00
|
|
|
case "${COMMAND}" in
|
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( 'add' )
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -f ${DATABASE} ]] && grep -q -F "${USER}" "${DATABASE}"; then
|
2023-02-18 15:52:42 +00:00
|
|
|
_exit_with_error "User '${USER}' already denied to ${DIRECTION} mails"
|
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2023-02-18 15:52:42 +00:00
|
|
|
echo -e "${USER} \t\t REJECT" >>"${DATABASE}"
|
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${DIRECTION} == 'send' ]]; then
|
2023-02-18 15:52:42 +00:00
|
|
|
CHECK='check_sender_access'
|
|
|
|
POSTFIX_OPTION='smtpd_sender_restrictions'
|
|
|
|
else
|
|
|
|
CHECK='check_recipient_access'
|
|
|
|
POSTFIX_OPTION='smtpd_recipient_restrictions'
|
|
|
|
fi
|
2018-02-18 12:29:43 +00:00
|
|
|
|
2023-02-18 15:52:42 +00:00
|
|
|
# only adjust Postfix's `main.cf` if we haven't adjusted it before
|
|
|
|
STRING_TO_BE_ADDED="${CHECK} texthash:/tmp/docker-mailserver/postfix-${DIRECTION}-access.cf"
|
2023-05-24 07:06:59 +00:00
|
|
|
if ! grep -q "${STRING_TO_BE_ADDED}" /etc/postfix/main.cf; then
|
2023-02-18 15:52:42 +00:00
|
|
|
sed -i -E "s|^(${POSTFIX_OPTION} =)(.*)|\1 ${STRING_TO_BE_ADDED},\2|" /etc/postfix/main.cf
|
2023-01-12 21:10:58 +00:00
|
|
|
_reload_postfix
|
2020-10-21 16:16:32 +00:00
|
|
|
fi
|
2022-03-26 08:30:09 +00:00
|
|
|
;;
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( 'del' )
|
2023-05-24 07:06:59 +00:00
|
|
|
if ! sed -i "/^$(_escape "${USER}").*/d" "${DATABASE}" 2>/dev/null; then
|
2023-02-18 15:52:42 +00:00
|
|
|
_exit_with_error "User '${USER}' not found"
|
|
|
|
fi
|
2022-03-26 08:30:09 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
( 'list' )
|
2023-02-18 15:52:42 +00:00
|
|
|
grep "REJECT" "${DATABASE}" 2>/dev/null || _log 'info' "Everyone is allowed to ${DIRECTION} mails"
|
2022-03-26 08:30:09 +00:00
|
|
|
;;
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( * )
|
|
|
|
__usage
|
2023-02-18 15:52:42 +00:00
|
|
|
_exit_with_error "Unknown or missing command '${COMMAND}' - specify 'add', 'del' or 'list'"
|
2018-02-07 20:33:07 +00:00
|
|
|
;;
|
2018-02-18 12:29:43 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
esac
|