2020-04-24 12:55:32 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# shellcheck source=../bin-helper.sh
|
|
|
|
. /usr/local/bin/bin-helper.sh
|
|
|
|
|
2020-04-24 12:55:32 +00:00
|
|
|
DATABASE=${DATABASE:-/tmp/docker-mailserver/dovecot-quotas.cf}
|
|
|
|
USER_DATABASE=${USER_DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
USER="${1}"
|
2020-04-24 12:55:32 +00:00
|
|
|
shift
|
2020-10-21 16:16:32 +00:00
|
|
|
QUOTA="${*}"
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
function usage { echo "Usage: setquota <user@domain> [<quota>]" ; }
|
2020-04-24 12:55:32 +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" ; }
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
|
|
|
|
then
|
|
|
|
usage; errex "user ${USER} does not exist"
|
2020-04-24 12:55:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check quota
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
|
2020-04-24 12:55:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Protect config file with lock to avoid race conditions
|
2020-10-21 16:16:32 +00:00
|
|
|
touch "${DATABASE}"
|
2020-04-24 12:55:32 +00:00
|
|
|
(
|
|
|
|
flock -e 200
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
if [ -z "${QUOTA}" ]; then
|
|
|
|
read -r -s "Enter quota (e.g. 10M): " QUOTA
|
2020-04-24 12:55:32 +00:00
|
|
|
echo
|
2020-10-21 16:16:32 +00:00
|
|
|
[[ -z "${QUOTA}" ]] && errex "Quota must not be empty. Use 0 for unlimited quota"
|
2020-04-24 12:55:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check quota
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
|
2020-04-24 12:55:32 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
delquota "${USER}"
|
|
|
|
echo "${USER}:${QUOTA}" >>"${DATABASE}"
|
|
|
|
) 200< "${DATABASE}"
|
2020-04-24 12:55:32 +00:00
|
|
|
|