2016-12-30 19:06:44 +00:00
|
|
|
#! /bin/bash
|
2016-12-21 19:12:05 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
# ? 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
|
2016-12-21 19:12:05 +00:00
|
|
|
|
2021-01-16 09:16:05 +00:00
|
|
|
# shellcheck source=../scripts/helper-functions.sh
|
|
|
|
. /usr/local/bin/helper-functions.sh
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
USER="${1}"
|
|
|
|
shift
|
|
|
|
PASSWD="${*}"
|
2016-12-21 19:12:05 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
function usage { echo "Usage: updatemailuser <user@domain.tld> [password]" ; }
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -z ${PASSWD} ]]
|
|
|
|
then
|
|
|
|
read -r -s -p "Enter Password: " PASSWD
|
|
|
|
echo
|
|
|
|
[[ -z ${PASSWD} ]] && errex "Password must not be empty"
|
2018-04-08 14:12:41 +00:00
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
|
|
|
|
HASH="$(doveadm pw -s SHA512-CRYPT -u "${USER}" -p "${PASSWD}")"
|
2018-04-08 14:12:41 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
touch "${DATABASE}"
|
2021-09-13 08:09:01 +00:00
|
|
|
create_lock # Protect config file with lock to avoid race conditions
|
2021-06-15 12:03:41 +00:00
|
|
|
grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null || errex "User \"${USER}\" does not exist"
|
|
|
|
sed -i "s ^""${USER}""|.* ""${USER}""|""${HASH}"" " "${DATABASE}"
|