quota.c revision 0e5819a061034f1636b124c03a89f67d37c852b1
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "lib.h"
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "array.h"
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen#include "hash.h"
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen#include "mailbox-list-private.h"
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen#include "maildir-storage.h"
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "quota-private.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "quota-fs.h"
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen#include <ctype.h>
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen#include <stdlib.h>
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen#include <sys/wait.h>
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
cb2b5a7d6e7e651191bf9ee1eda94a6e207288b0Timo Sirainen#define DEFAULT_QUOTA_EXCEEDED_MSG \
cb2b5a7d6e7e651191bf9ee1eda94a6e207288b0Timo Sirainen "Quota exceeded (mailbox for user is full)"
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen#define RULE_NAME_DEFAULT_FORCE "*"
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen#define RULE_NAME_DEFAULT_NONFORCE "?"
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstruct quota_root_iter {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota *quota;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mailbox *box;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen};
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenunsigned int quota_module_id = 0;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenextern struct quota_backend quota_backend_dict;
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainenextern struct quota_backend quota_backend_dirsize;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenextern struct quota_backend quota_backend_fs;
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainenextern struct quota_backend quota_backend_maildir;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic const struct quota_backend *quota_backends[] = {
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#ifdef HAVE_FS_QUOTA
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen &quota_backend_fs,
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#endif
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen &quota_backend_dict,
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen &quota_backend_dirsize,
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen &quota_backend_maildir
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen};
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic int quota_default_test_alloc(struct quota_transaction_context *ctx,
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen uoff_t size, bool *too_large_r);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_settings *quota_settings_init(void)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_settings *quota_set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen pool_t pool;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen pool = pool_alloconly_create("quota settings", 256);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set = p_new(pool, struct quota_settings, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->pool = pool;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->test_alloc = quota_default_test_alloc;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->debug = getenv("DEBUG") != NULL;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->quota_exceeded_msg = getenv("QUOTA_EXCEEDED_MESSAGE");
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_set->quota_exceeded_msg == NULL)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->quota_exceeded_msg = DEFAULT_QUOTA_EXCEEDED_MSG;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_array_init(&quota_set->root_sets, pool, 4);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return quota_set;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_settings_deinit(struct quota_settings **_quota_set)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_settings *quota_set = *_quota_set;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *_quota_set = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen pool_unref(&quota_set->pool);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic const struct quota_backend *quota_backend_find(const char *name)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen{
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen unsigned int i;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
fd1f0e9ef52b3e157cfd1a01c464c2ac7458ab17Timo Sirainen for (i = 0; i < N_ELEMENTS(quota_backends); i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (strcmp(quota_backends[i]->name, name) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return quota_backends[i];
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return NULL;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_root_settings *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_settings_init(struct quota_settings *quota_set, const char *root_def)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_root_settings *root_set;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen const struct quota_backend *backend;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen const char *p, *args, *backend_name;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* <backend>[:<quota root name>[:<backend args>]] */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen p = strchr(root_def, ':');
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (p == NULL) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backend_name = root_def;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen args = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen } else {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backend_name = t_strdup_until(root_def, p);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen args = p + 1;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backend = quota_backend_find(backend_name);
d9b8c65d0a0ffc709ba7d23c449dbf2f46b10674Timo Sirainen if (backend == NULL) {
d9b8c65d0a0ffc709ba7d23c449dbf2f46b10674Timo Sirainen i_error("Unknown quota backend: %s", backend_name);
d9b8c65d0a0ffc709ba7d23c449dbf2f46b10674Timo Sirainen return NULL;
d9b8c65d0a0ffc709ba7d23c449dbf2f46b10674Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set = p_new(quota_set->pool, struct quota_root_settings, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->set = quota_set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->backend = backend;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (args != NULL) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* save root's name */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen p = strchr(args, ':');
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (p == NULL) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name = p_strdup(quota_set->pool, args);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen args = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen } else {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name =
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_strdup_until(quota_set->pool, args, p);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen args = p + 1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen } else {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name = "";
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->args = p_strdup(quota_set->pool, args);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_set->debug) {
e3077468777f5d324224365e34d7bbc449168e52Timo Sirainen i_info("Quota root: name=%s backend=%s args=%s",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name, backend_name, args == NULL ? "" : args);
e3077468777f5d324224365e34d7bbc449168e52Timo Sirainen }
e3077468777f5d324224365e34d7bbc449168e52Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_array_init(&root_set->rules, quota_set->pool, 4);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_array_init(&root_set->warning_rules, quota_set->pool, 4);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_append(&quota_set->root_sets, &root_set, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return root_set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstatic struct quota_root *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_init(struct quota_root_settings *root_set, struct quota *quota)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_root *root;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen const char *const *tmp;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root = root_set->backend->v.alloc();
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen root->resource_ret = -1;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->pool = pool_alloconly_create("quota root", 512);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->set = root_set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->quota = quota;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->backend = *root_set->backend;
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen root->bytes_limit = root_set->default_rule.bytes_limit;
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen root->count_limit = root_set->default_rule.count_limit;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_create(&root->quota_module_contexts, root->pool,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen sizeof(void *), 10);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root->backend.v.init != NULL) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root->backend.v.init(root, root_set->args) < 0)
00589bcc8452de52d8a4f753be402487eb83ca41Timo Sirainen i_fatal("Quota root %s init() failed", root_set->name);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen } else if (root_set->args != NULL) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen tmp = t_strsplit_spaces(root_set->args, " ");
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen for (; *tmp != NULL; tmp++) {
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen if (strcmp(*tmp, "noenforcing") == 0)
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen root->no_enforcing = TRUE;
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen else
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen break;
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen }
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen if (*tmp != NULL) {
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen i_fatal("Quota root %s backend %s: "
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen "Unknown parameter: %s",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name, root->backend.name, *tmp);
608aa112aa7d94a1720c909747dcd0b61c079453Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return root;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstatic void quota_root_deinit(struct quota_root *root)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen pool_t pool = root->pool;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->backend.v.deinit(root);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen pool_unref(&pool);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen}
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota *quota_init(struct quota_settings *quota_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct mail_user *user)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota *quota;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_root *root;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_root_settings *const *root_sets;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota = i_new(struct quota, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota->user = user;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota->set = quota_set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen i_array_init(&quota->roots, 8);
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_sets = array_get(&quota_set->root_sets, &count);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen i_array_init(&quota->storages, count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root = quota_root_init(root_sets[i], quota);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_append(&quota->roots, &root, 1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return quota;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_deinit(struct quota **_quota)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota *quota = *_quota;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_root *const *roots;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen unsigned int i, count;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *_quota = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen roots = array_get(&quota->roots, &count);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen for (i = 0; i < count; i++)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_root_deinit(roots[i]);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_free(&quota->roots);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_free(&quota->storages);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen i_free(quota);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainenstruct quota_rule *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_rule_find(struct quota_root_settings *root_set, const char *name)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_rule *rules;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, count;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rules = array_get_modifiable(&root_set->rules, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (strcmp(rules[i].mailbox_name, name) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return &rules[i];
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return NULL;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainenstatic int
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_rule_parse_percentage(struct quota_root_settings *root_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_rule *rule,
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen int64_t *limit, const char **error_r)
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen{
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen int64_t percentage = *limit;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (percentage <= 0 || percentage >= -1U) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen "Invalid rule percentage: %lld", (long long)percentage);
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return -1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen }
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (rule == &root_set->default_rule) {
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen *error_r = "Default rule can't be a percentage";
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return -1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen }
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if (limit == &rule->bytes_limit)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->bytes_percent = percentage;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen else if (limit == &rule->count_limit)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->count_percent = percentage;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen else
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen i_unreached();
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return 0;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen}
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainenstatic void
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainenquota_rule_recalculate_relative_rules(struct quota_rule *rule,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen const struct quota_rule *default_rule)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen{
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (rule->bytes_percent > 0) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->bytes_limit = default_rule->bytes_limit *
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->bytes_percent / 100;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (rule->count_percent > 0) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->count_limit = default_rule->count_limit *
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->count_percent / 100;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen}
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_root_recalculate_relative_rules(struct quota_root_settings *root_set)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen{
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen struct quota_rule *rules;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen struct quota_warning_rule *warning_rules;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen unsigned int i, count;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rules = array_get_modifiable(&root_set->rules, &count);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen quota_rule_recalculate_relative_rules(&rules[i],
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen &root_set->default_rule);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warning_rules = array_get_modifiable(&root_set->warning_rules, &count);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen quota_rule_recalculate_relative_rules(&warning_rules[i].rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen &root_set->default_rule);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen}
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainenstatic int
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_rule_parse_limits(struct quota_root_settings *root_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_rule *rule, const char *limits,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen bool allow_negative, const char **error_r)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen{
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const char **args;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen char *p;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen uint64_t multiply;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen int64_t *limit;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen args = t_strsplit(limits, ":");
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen for (; *args != NULL; args++) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen limit = NULL;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (strncmp(*args, "storage=", 8) == 0) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1024;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen limit = &rule->bytes_limit;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *limit = strtoll(*args + 8, &p, 10);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else if (strncmp(*args, "bytes=", 6) == 0) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen limit = &rule->bytes_limit;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *limit = strtoll(*args + 6, &p, 10);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else if (strncmp(*args, "messages=", 9) == 0) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen limit = &rule->count_limit;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *limit = strtoll(*args + 9, &p, 10);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen "Unknown rule limit name: %s", *args);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return -1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen switch (i_toupper(*p)) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case '\0':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen /* default */
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case 'B':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case 'K':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1024;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case 'M':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1024*1024;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case 'G':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1024*1024*1024;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen case 'T':
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen multiply = 1024ULL*1024*1024*1024;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen case '%':
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen multiply = 0;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_rule_parse_percentage(root_set, rule, limit,
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen error_r) < 0)
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return -1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen default:
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen "Invalid rule limit value: %s", *args);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return -1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *limit *= multiply;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (!allow_negative) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (rule->bytes_limit < 0) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen *error_r = "Bytes limit can't be negative";
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen return -1;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (rule->count_limit < 0) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen *error_r = "Count limit can't be negative";
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen return -1;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen }
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return 0;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen}
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenint quota_root_add_rule(struct quota_root_settings *root_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen const char *rule_def, const char **error_r)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_rule *rule;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const char *p, *mailbox_name;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int ret = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen p = strchr(rule_def, ':');
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen if (p == NULL) {
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen *error_r = "Invalid rule";
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return -1;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* <mailbox name>:<quota limits> */
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen mailbox_name = t_strdup_until(rule_def, p++);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = quota_root_rule_find(root_set, mailbox_name);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (rule == NULL) {
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen if (strcmp(mailbox_name, RULE_NAME_DEFAULT_NONFORCE) == 0)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = &root_set->default_rule;
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen else if (strcmp(mailbox_name, RULE_NAME_DEFAULT_FORCE) == 0) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = &root_set->default_rule;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->force_default_rule = TRUE;
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen } else {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = array_append_space(&root_set->rules);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule->mailbox_name =
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_strdup(root_set->set->pool, mailbox_name);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen if (strcmp(p, "ignore") == 0) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen rule->ignore = TRUE;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root_set->set->debug) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen i_info("Quota rule: root=%s mailbox=%s ignored",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name, mailbox_name);
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen }
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen return 0;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen }
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen if (strncmp(p, "backend=", 8) == 0) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (!root_set->backend->v.parse_rule(root_set, rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p + 8, error_r))
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen ret = -1;
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen } else {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen bool allow_negative = rule != &root_set->default_rule;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_rule_parse_limits(root_set, rule, p,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen allow_negative, error_r) < 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ret = -1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_root_recalculate_relative_rules(root_set);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root_set->set->debug) {
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen i_info("Quota rule: root=%s mailbox=%s "
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen "bytes=%lld (%u%%) messages=%lld (%u%%)", root_set->name,
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen mailbox_name,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (long long)rule->bytes_limit, rule->bytes_percent,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (long long)rule->count_limit, rule->count_percent);
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return ret;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainenstatic bool quota_root_get_rule_limits(struct quota_root *root,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const char *mailbox_name,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint64_t *bytes_limit_r,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint64_t *count_limit_r)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_rule *rule;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int64_t bytes_limit, count_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen bool found;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen bytes_limit = root->bytes_limit;
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen count_limit = root->count_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* if default rule limits are 0, this rule applies only to specific
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen mailboxes */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen found = bytes_limit != 0 || count_limit != 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen rule = quota_root_rule_find(root->set, mailbox_name);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (rule != NULL) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen if (!rule->ignore) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen bytes_limit += rule->bytes_limit;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen count_limit += rule->count_limit;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen } else {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen bytes_limit = 0;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen count_limit = 0;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen found = TRUE;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *bytes_limit_r = bytes_limit <= 0 ? 0 : bytes_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *count_limit_r = count_limit <= 0 ? 0 : count_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return found;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainenstatic void quota_maildir_storage_set(struct mail_storage *storage)
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen{
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen /* FIXME: a bit ugly location for this code. */
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen if (strcmp(storage->name, "maildir") == 0) {
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen /* For newly generated filenames add ,S=size. */
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen struct maildir_storage *mstorage =
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen (struct maildir_storage *)storage;
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen mstorage->save_size_in_filename = TRUE;
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen }
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen}
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_add_user_storage(struct quota *quota, struct mail_storage *storage)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root *const *roots;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen struct mail_storage *const *storages;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_backend **backends;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen const char *path, *path2;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, j, count;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen bool is_file;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen quota_maildir_storage_set(storage);
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen /* first check if there already exists a storage with the exact same
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path. we don't want to count them twice. */
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path = mail_storage_get_mailbox_path(storage, "", &is_file);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (path != NULL) {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen storages = array_get(&quota->storages, &count);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen for (i = 0; i < count; i++) {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path2 = mail_storage_get_mailbox_path(storages[i], "",
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen &is_file);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (path2 != NULL && strcmp(path, path2) == 0) {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen /* duplicate */
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen return;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen }
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen }
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_append(&quota->storages, &storage, 1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&quota->roots, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* @UNSAFE: get different backends into one array */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backends = t_new(struct quota_backend *, count + 1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (j = 0; backends[j] != NULL; j++) {
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen if (backends[j]->name == roots[i]->backend.name)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen break;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (backends[j] == NULL)
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen backends[j] = &roots[i]->backend;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; backends[i] != NULL; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (backends[i]->v.storage_added != NULL)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backends[i]->v.storage_added(quota, storage);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_remove_user_storage(struct mail_storage *storage)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota *quota;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_storage *const *storages;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen quota = storage->ns->owner == NULL ? NULL :
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen quota_get_mail_user_quota(storage->ns->owner);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota == NULL) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen /* no quota for this storage */
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen }
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen storages = array_get(&quota->storages, &count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (storages[i] == storage) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_delete(&quota->storages, i, 1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen break;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenint quota_root_add_warning_rule(struct quota_root_settings *root_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen const char *rule_def, const char **error_r)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_warning_rule *warning;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_rule rule;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen const char *p;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen int ret;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen p = strchr(rule_def, ' ');
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (p == NULL) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *error_r = "No command specified";
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return -1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen memset(&rule, 0, sizeof(rule));
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen ret = quota_rule_parse_limits(root_set, &rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen t_strdup_until(rule_def, p),
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen TRUE, error_r);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (ret < 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return -1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warning = array_append_space(&root_set->warning_rules);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen warning->command = i_strdup(p+1);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen warning->rule = rule;
337d77f9903bf56ccd686479dffaf6069991edf2Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_root_recalculate_relative_rules(root_set);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root_set->set->debug) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen i_info("Quota warning: bytes=%llu (%u%%) "
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen "messages=%llu (%u%%) command=%s",
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (unsigned long long)warning->rule.bytes_limit,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen warning->rule.bytes_percent,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (unsigned long long)warning->rule.count_limit,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen warning->rule.count_percent, warning->command);
337d77f9903bf56ccd686479dffaf6069991edf2Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstruct quota_root_iter *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_iter_init(struct mailbox *box)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct mail_user *user = box->storage->ns->owner;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root_iter *iter;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen iter = i_new(struct quota_root_iter, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen iter->quota = quota_get_mail_user_quota(user);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen iter->box = box;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return iter;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainenstatic bool
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainenquota_root_is_visible(struct quota_root *root, struct mailbox *box,
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen bool enforce)
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen{
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (root->no_enforcing && enforce) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen /* we don't want to include this root in quota enforcing */
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen return FALSE;
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen }
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (array_count(&root->quota->roots) == 1) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen /* a single quota root: don't bother checking further */
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen return TRUE;
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen }
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen return root->backend.v.match_box == NULL ? TRUE :
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen root->backend.v.match_box(root, box);
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen}
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstruct quota_root *quota_root_iter_next(struct quota_root_iter *iter)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root *const *roots, *root = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int count;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint64_t value, limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int ret;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&iter->quota->roots, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (iter->i >= count)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (; iter->i < count; iter->i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[iter->i], iter->box, FALSE))
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen continue;
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen ret = roots[iter->i]->resource_ret;
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen if (ret == -1) {
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen ret = quota_get_resource(roots[iter->i], "",
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen QUOTA_NAME_STORAGE_KILOBYTES,
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen &value, &limit);
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (ret == 0) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ret = quota_get_resource(roots[iter->i], "",
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen QUOTA_NAME_MESSAGES,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &value, &limit);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
0e5819a061034f1636b124c03a89f67d37c852b1Timo Sirainen roots[iter->i]->resource_ret = ret;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (ret > 0) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen root = roots[iter->i];
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen break;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen iter->i++;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return root;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenvoid quota_root_iter_deinit(struct quota_root_iter **_iter)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen struct quota_root_iter *iter = *_iter;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen *_iter = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen i_free(iter);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_root *quota_root_lookup(struct mail_user *user, const char *name)
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota *quota;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root *const *roots;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota = quota_get_mail_user_quota(user);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&quota->roots, &count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (strcmp(roots[i]->set->name, name) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return roots[i];
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return NULL;
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen}
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenconst char *quota_root_get_name(struct quota_root *root)
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen{
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return root->set->name;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenconst char *const *quota_root_get_resources(struct quota_root *root)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen return root->backend.v.get_resources(root);
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen}
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_get_resource(struct quota_root *root, const char *mailbox_name,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const char *name, uint64_t *value_r, uint64_t *limit_r)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint64_t bytes_limit, count_limit;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen bool kilobytes = FALSE;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int ret;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_KILOBYTES) == 0) {
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen name = QUOTA_NAME_STORAGE_BYTES;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen kilobytes = TRUE;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen }
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen /* Get the value first. This call may also update quota limits if
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen they're defined externally. */
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen ret = root->backend.v.get_resource(root, name, value_r);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (ret <= 0)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen return ret;
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen (void)quota_root_get_rule_limits(root, mailbox_name,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &bytes_limit, &count_limit);
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *limit_r = bytes_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *limit_r = count_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen else
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *limit_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if (kilobytes) {
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen *value_r /= 1024;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen *limit_r /= 1024;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen }
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen return *limit_r == 0 ? 0 : 1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainenint quota_set_resource(struct quota_root *root ATTR_UNUSED,
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen const char *name ATTR_UNUSED,
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen uint64_t value ATTR_UNUSED, const char **error_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* the quota information comes from userdb (or even config file),
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen so there's really no way to support this until some major changes
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen are done */
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen *error_r = MAIL_ERRSTR_NO_PERMISSION;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return -1;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_transaction_context *quota_transaction_begin(struct mailbox *box)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct mail_user *user = box->storage->ns->owner;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_transaction_context *ctx;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx = i_new(struct quota_transaction_context, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen ctx->quota = quota_get_mail_user_quota(user);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx->box = box;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx->bytes_left = (uint64_t)-1;
62300a38f91227b9de043a9a8ec1d4f1978e1138Timo Sirainen ctx->count_left = (uint64_t)-1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return ctx;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenstatic int quota_transaction_set_limits(struct quota_transaction_context *ctx)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen struct quota_root *const *roots;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen const char *mailbox_name;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen unsigned int i, count;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint64_t current, limit, left;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen int ret;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ctx->limits_set = TRUE;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen mailbox_name = mailbox_get_name(ctx->box);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen /* find the lowest quota limits from all roots and use them */
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen roots = array_get(&ctx->quota->roots, &count);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen for (i = 0; i < count; i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
d5d23d5ff8b7a06d2ead489ddcf55ee8fb5ca7b6Timo Sirainen continue;
d5d23d5ff8b7a06d2ead489ddcf55ee8fb5ca7b6Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ret = quota_get_resource(roots[i], mailbox_name,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen QUOTA_NAME_STORAGE_BYTES,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen &current, &limit);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (ret > 0) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen current += ctx->bytes_used;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen left = limit < current ? 0 : limit - current;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (ctx->bytes_left > left)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ctx->bytes_left = left;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen } else if (ret < 0) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ctx->failed = TRUE;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return -1;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen }
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ret = quota_get_resource(roots[i], mailbox_name,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen QUOTA_NAME_MESSAGES, &current, &limit);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (ret > 0) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen current += ctx->count_used;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen left = limit < current ? 0 : limit - current;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (ctx->count_left > left)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ctx->count_left = left;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen } else if (ret < 0) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ctx->failed = TRUE;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return -1;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen }
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen }
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return 0;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen}
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainenstatic void quota_warning_execute(struct quota_root *root, const char *cmd)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen int ret;
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen if (root->quota->set->debug)
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen i_info("quota: Executing warning: %s", cmd);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen ret = system(cmd);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (ret < 0) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_error("system(%s) failed: %m", cmd);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen } else if (WIFSIGNALED(ret)) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_error("system(%s) died with signal %d", cmd, WTERMSIG(ret));
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_error("system(%s) exited with status %d",
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen cmd, WIFEXITED(ret) ? WEXITSTATUS(ret) : ret);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainenstatic void quota_warnings_execute(struct quota_transaction_context *ctx,
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen struct quota_root *root)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_warning_rule *warnings;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen unsigned int i, count;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen uint64_t bytes_current, bytes_before, bytes_limit;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen uint64_t count_current, count_before, count_limit;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warnings = array_get_modifiable(&root->set->warning_rules, &count);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (count == 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen &bytes_current, &bytes_limit) < 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen &count_current, &count_limit) < 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen bytes_before = bytes_current - ctx->bytes_used;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen count_before = count_current - ctx->count_used;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if ((bytes_before < (uint64_t)warnings[i].rule.bytes_limit &&
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen bytes_current >= (uint64_t)warnings[i].rule.bytes_limit) ||
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (count_before < (uint64_t)warnings[i].rule.count_limit &&
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen count_current >= (uint64_t)warnings[i].rule.count_limit)) {
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen quota_warning_execute(root, warnings[i].command);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen break;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenint quota_transaction_commit(struct quota_transaction_context **_ctx)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_transaction_context *ctx = *_ctx;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen struct quota_rule *rule;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_root *const *roots;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen unsigned int i, count;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen const char *mailbox_name;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen int ret = 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *_ctx = NULL;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (ctx->failed)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen ret = -1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen else if (ctx->bytes_used != 0 || ctx->count_used != 0 ||
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen ctx->recalculate) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen mailbox_name = mailbox_get_name(ctx->box);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen roots = array_get(&ctx->quota->roots, &count);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen for (i = 0; i < count; i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen continue;
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = quota_root_rule_find(roots[i]->set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen mailbox_name);
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen if (rule != NULL && rule->ignore) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen /* mailbox not included in quota */
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen continue;
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen }
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (roots[i]->backend.v.update(roots[i], ctx) < 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen ret = -1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen /* execute quota warnings after all updates. this makes it
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen work correctly regardless of whether backend.get_resource()
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen returns updated values before backend.update() or not */
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen for (i = 0; i < count; i++)
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen quota_warnings_execute(ctx, roots[i]);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_free(ctx);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return ret;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenvoid quota_transaction_rollback(struct quota_transaction_context **_ctx)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_transaction_context *ctx = *_ctx;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *_ctx = NULL;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_free(ctx);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_try_alloc(struct quota_transaction_context *ctx,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail *mail, bool *too_large_r)
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen{
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen uoff_t size;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen int ret;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_physical_size(mail, &size) < 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen ret = quota_test_alloc(ctx, size, too_large_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (ret <= 0)
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen return ret;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen quota_alloc(ctx, mail);
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen return 1;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen}
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_test_alloc(struct quota_transaction_context *ctx,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uoff_t size, bool *too_large_r)
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (ctx->failed)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return -1;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (!ctx->limits_set) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (quota_transaction_set_limits(ctx) < 0)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return -1;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen }
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return ctx->quota->set->test_alloc(ctx, size, too_large_r);
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen}
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic int quota_default_test_alloc(struct quota_transaction_context *ctx,
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen uoff_t size, bool *too_large_r)
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root *const *roots;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, count;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *too_large_r = FALSE;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (ctx->count_left != 0 && ctx->bytes_left >= ctx->bytes_used + size)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 1;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&ctx->quota->roots, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint64_t bytes_limit, count_limit;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen continue;
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen if (!quota_root_get_rule_limits(roots[i],
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen mailbox_get_name(ctx->box),
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &bytes_limit, &count_limit))
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen continue;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* if size is bigger than any limit, then
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen it is bigger than the lowest limit */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (size > bytes_limit) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *too_large_r = TRUE;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen break;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen }
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen}
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_alloc(struct quota_transaction_context *ctx, struct mail *mail)
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen{
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen uoff_t size;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_physical_size(mail, &size) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx->bytes_used += size;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx->count_used++;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen}
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_free(struct quota_transaction_context *ctx, struct mail *mail)
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen{
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen uoff_t size;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen if (mail_get_physical_size(mail, &size) < 0)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen quota_recalculate(ctx);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen else
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen quota_free_bytes(ctx, size);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenvoid quota_free_bytes(struct quota_transaction_context *ctx,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen uoff_t physical_size)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen ctx->bytes_used -= physical_size;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx->count_used--;
6800c6607013d4fdef5a4f764bae407301c6cce8Timo Sirainen}
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenvoid quota_recalculate(struct quota_transaction_context *ctx)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen{
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen ctx->recalculate = TRUE;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen}