2018-04-02 08:45:58 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2018-04-02 08:45:58 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-sasl-password.cf}
|
2018-04-02 08:45:58 +00:00
|
|
|
|
2021-02-23 19:03:01 +00:00
|
|
|
function __usage { echo "Usage: addsaslpassword <domain> <username> <password>" ; }
|
|
|
|
|
|
|
|
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
DOMAIN="${1}"
|
|
|
|
USER="${2}"
|
|
|
|
PASSWD="${3}"
|
2018-04-02 08:45:58 +00:00
|
|
|
|
2022-03-21 14:01:07 +00:00
|
|
|
[[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; }
|
|
|
|
[[ -z ${USER} ]] && { __usage ; _exit_with_error 'No username specified' ; }
|
2018-04-02 08:45:58 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -z ${PASSWD} ]]
|
|
|
|
then
|
|
|
|
read -r -s -p "Enter Password: " PASSWD
|
|
|
|
echo
|
2022-03-21 14:01:07 +00:00
|
|
|
[[ -z ${PASSWD} ]] && _exit_with_error 'Password must not be empty'
|
2018-04-02 08:45:58 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null
|
|
|
|
then
|
2021-02-23 19:03:01 +00:00
|
|
|
sed -i \
|
|
|
|
"s|^@${DOMAIN}.*|@${DOMAIN}\t\t${USER}:${PASSWD}|" \
|
|
|
|
"${DATABASE}"
|
2018-04-02 08:45:58 +00:00
|
|
|
else
|
2021-02-23 19:03:01 +00:00
|
|
|
echo -e "@${DOMAIN}\t\t${USER}:${PASSWD}" >>"${DATABASE}"
|
2018-04-02 08:45:58 +00:00
|
|
|
fi
|