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
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2022-04-20 00:29:28 +00:00
|
|
|
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
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
function __usage { echo 'Usage: updatemailuser <user@domain.tld> [password]' ; }
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z ${USER} ]] && { __usage ; _exit_with_error 'No username specified' ; }
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -z ${PASSWD} ]]
|
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
read -r -s -p 'Enter Password: ' PASSWD
|
2020-10-21 16:16:32 +00:00
|
|
|
echo
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z ${PASSWD} ]] && _exit_with_error '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}"
|
2022-02-21 10:56:57 +00:00
|
|
|
_create_lock # Protect config file with lock to avoid race conditions
|
2022-03-21 14:01:07 +00:00
|
|
|
grep -qi "^$(_escape "${USER}")|" "${DATABASE}" 2>/dev/null || _exit_with_error "User \"${USER}\" does not exist"
|
2021-06-15 12:03:41 +00:00
|
|
|
sed -i "s ^""${USER}""|.* ""${USER}""|""${HASH}"" " "${DATABASE}"
|