docker-mailserver/target/bin/addmailuser

37 lines
921 B
Plaintext
Raw Normal View History

#! /bin/bash
2016-06-14 11:16:11 +00:00
# shellcheck disable=SC2094
2016-06-14 11:16:11 +00:00
# shellcheck source=../bin-helper.sh
. /usr/local/bin/bin-helper.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
USER="${1}"
shift
PASSWD="${*}"
function usage { echo "Usage: addmailuser <user@domain> [<password>]" ; }
2016-06-14 11:16:11 +00:00
[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
[[ "${USER}" =~ .*\@.* ]] || { usage ; errex "username must include the domain" ; }
2019-08-01 17:39:25 +00:00
# Protect config file with lock to avoid race conditions
touch "${DATABASE}"
(
2019-08-01 17:39:25 +00:00
flock -e 200
grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null &&
errex "User \"${USER}\" already exists"
2019-08-01 17:39:25 +00:00
if [[ -z ${PASSWD} ]]
then
read -r -s -p "Enter Password: " PASSWD
2019-08-01 17:39:25 +00:00
echo
[[ -z ${PASSWD} ]] && errex "Password must not be empty"
2019-08-01 17:39:25 +00:00
fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "${USER}" -p "${PASSWD}")"
echo "${USER}|${HASH}" >> "${DATABASE}"
) 200< "${DATABASE}"