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