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
|
|
|
|
2022-04-20 00:29:28 +00:00
|
|
|
DATABASE='/tmp/docker-mailserver/dovecot-quotas.cf'
|
|
|
|
USER_DATABASE='/tmp/docker-mailserver/postfix-accounts.cf'
|
2020-04-24 12:55:32 +00:00
|
|
|
|
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
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
function __usage { echo 'Usage: setquota <user@domain> [<quota>]' ; }
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z ${USER} ]] && { __usage ; _exit_with_error "No username specified" ; }
|
|
|
|
[[ ${USER} =~ .*\@.* ]] || { __usage ; _exit_with_error "Username must include the domain" ; }
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
grep -qE "^${USER}\|" "${USER_DATABASE}" || _exit_with_error "User '${USER}' does not exist"
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2021-12-19 10:56:22 +00:00
|
|
|
if [[ -z ${QUOTA} ]]
|
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
read -r -s 'Enter quota (e.g. 10M): ' QUOTA
|
2021-06-15 12:03:41 +00:00
|
|
|
echo
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z "${QUOTA}" ]] && _exit_with_error 'Quota must not be empty (use 0 for unlimited quota)'
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check quota
|
2022-03-26 08:30:09 +00:00
|
|
|
if ! grep -qE "^([0-9]+(B|k|M|G|T)|0)\$" <<< "${QUOTA}"
|
2021-06-15 12:03:41 +00:00
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
__usage
|
|
|
|
_exit_with_error '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
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
_create_lock # Protect config file with lock to avoid race conditions
|
|
|
|
touch "${DATABASE}"
|
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
delquota "${USER}"
|
|
|
|
echo "${USER}:${QUOTA}" >>"${DATABASE}"
|