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
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2020-10-21 16:16:32 +00:00
|
|
|
|
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
|
|
|
|
2022-02-21 10:56:57 +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
|
2022-02-21 10:56:57 +00:00
|
|
|
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
|
2022-02-21 10:56:57 +00:00
|
|
|
_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
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
_create_lock # Protect config file with lock to avoid race conditions
|
2021-06-15 12:03:41 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
touch "${DATABASE}"
|
2021-12-19 10:56:22 +00:00
|
|
|
if [[ -z ${QUOTA} ]]
|
|
|
|
then
|
2021-06-15 12:03:41 +00:00
|
|
|
read -r -s "Enter quota (e.g. 10M): " QUOTA
|
|
|
|
echo
|
2022-02-21 10:56:57 +00:00
|
|
|
[[ -z "${QUOTA}" ]] && _errex "Quota must not be empty. Use 0 for unlimited quota"
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check quota
|
|
|
|
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
|
|
|
|
then
|
|
|
|
usage
|
2022-02-21 10:56:57 +00:00
|
|
|
_errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
delquota "${USER}"
|
|
|
|
echo "${USER}:${QUOTA}" >>"${DATABASE}"
|