quota.c revision bbe7af3946f071eaf3f81ac769dd1c735168b735
/* Copyright (c) 2005-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "str.h"
#include "ioloop.h"
#include "net.h"
#include "write-full.h"
#include "eacces-error.h"
#include "wildcard-match.h"
#include "dict.h"
#include "mailbox-list-private.h"
#include "quota-private.h"
#include "quota-fs.h"
#include "llist.h"
#include "program-client.h"
#define DEFAULT_QUOTA_EXCEEDED_MSG \
"Quota exceeded (mailbox for user is full)"
/* How many seconds after the userdb lookup do we still want to execute the
quota_over_script. This applies to quota_over_flag_lazy_check=yes and also
after unhibernating IMAP connections. */
#define QUOTA_OVER_FLAG_MAX_DELAY_SECS 10
struct quota_root_iter {
unsigned int i;
};
unsigned int quota_module_id = 0;
extern struct quota_backend quota_backend_count;
extern struct quota_backend quota_backend_dict;
extern struct quota_backend quota_backend_dirsize;
extern struct quota_backend quota_backend_fs;
extern struct quota_backend quota_backend_maildir;
static const struct quota_backend *quota_backends[] = {
#ifdef HAVE_FS_QUOTA
#endif
};
{
unsigned int i;
for (i = 0; i < N_ELEMENTS(quota_backends); i++) {
return quota_backends[i];
}
return NULL;
}
struct quota_root_settings *root_set,
const char **error_r)
{
unsigned int i;
for (i = 2;; i++) {
break;
return -1;
}
}
return 0;
}
static int
struct quota_root_settings *root_set,
const char **error_r)
{
unsigned int i;
for (i = 2;; i++) {
break;
rule);
return -1;
}
}
return 0;
}
static int
struct quota_root_settings *root_set,
const char **error_r)
{
return 0;
return -1;
}
return 0;
}
static int
struct quota_root_settings **set_r,
const char **error_r)
{
struct quota_root_settings *root_set;
const struct quota_backend *backend;
const char *p, *args, *backend_name;
/* <backend>[:<quota root name>[:<backend args>]] */
if (p == NULL) {
} else {
args = p + 1;
}
return -1;
}
/* save root's name */
if (p == NULL) {
} else {
args = p + 1;
}
} else {
}
i_debug("Quota root: name=%s backend=%s args=%s",
}
return 0;
}
static int
{
struct quota_root_settings *root_set;
return -1;
return -1;
return -1;
return -1;
return -1;
}
return 0;
}
struct quota_settings **set_r,
const char **error_r)
{
struct quota_settings *quota_set;
unsigned int i;
i_unreached();
for (i = 2;; i++) {
break;
&error) < 0) {
pool_unref(&pool);
return -1;
}
i_unreached();
}
pool_unref(&pool);
return 0;
}
return 1;
}
{
*_quota_set = NULL;
}
{
pool_unref(&pool);
}
const char **error_r)
{
const char *const *tmp;
return 0;
else
break;
}
"Unknown parameter for backend %s: %s",
return -1;
}
return 0;
}
static int
{
struct quota_root *root;
sizeof(void *), 10);
return -1;
}
} else {
return -1;
}
return 0;
}
return 1;
}
{
struct quota_root *root;
struct quota_root_settings *const *root_sets;
unsigned int i, count;
const char *error;
int ret;
for (i = 0; i < count; i++) {
if (ret < 0) {
return -1;
}
if (ret > 0)
}
return 0;
}
{
struct quota_root *const *roots;
unsigned int i, count;
for (i = 0; i < count; i++)
quota_root_deinit(roots[i]);
/* deinit quota roots before setting quser->quota=NULL */
}
const char *mailbox_name,
bool *ignored_r)
{
struct quota_rule *rule;
bool enabled;
return -1;
}
}
/* if default rule limits are 0, user has unlimited quota.
ignore any specific quota rules */
&mailbox_name);
} else {
bytes_limit = 0;
count_limit = 0;
}
}
return enabled ? 1 : 0;
}
static bool
{
struct mail_namespace *const *namespaces;
unsigned int i, count;
return TRUE;
for (i = 0; i < count; i++) {
/* duplicate path */
return TRUE;
/* this is inbox=yes namespace, but the earlier one
that had the same location was inbox=no. we need to
include the INBOX also in quota calculations, so we
can't just ignore this namespace. but since we've
already called backend's namespace_added(), we can't
just remove it either. so just mark the old one as
unwanted namespace.
an alternative would be to do a bit larger change so
namespaces wouldn't be added until
mail_namespaces_created() hook is called */
return FALSE;
}
}
return FALSE;
}
{
struct quota_root *const *roots;
struct quota_backend **backends;
unsigned int i, j, count;
/* first check if there already exists a namespace with the exact same
path. we don't want to count them twice. */
return;
/* @UNSAFE: get different backends into one array */
for (i = 0; i < count; i++) {
break;
}
}
}
}
{
struct mail_namespace *const *namespaces;
unsigned int i, count;
/* no quota for this namespace */
return;
}
for (i = 0; i < count; i++) {
if (namespaces[i] == ns) {
break;
}
}
}
struct quota_root_iter *
{
struct quota_root_iter *iter;
return iter;
}
struct mail_namespace *ns)
{
struct mail_storage *storage;
/* this check works as long as there is only one storage per list */
return FALSE;
return FALSE;
return FALSE;
} else {
return FALSE;
}
return TRUE;
}
static bool
bool enforce)
{
/* we don't want to include this root in quota enforcing */
return FALSE;
}
return FALSE;
/* a single quota root: don't bother checking further */
return TRUE;
}
}
{
unsigned int count;
int ret;
return NULL;
return NULL;
continue;
if (ret == -1) {
}
if (ret == 0) {
}
if (ret > 0) {
break;
}
}
iter->i++;
return root;
}
{
}
{
struct quota_root *const *roots;
unsigned int i, count;
return NULL;
for (i = 0; i < count; i++) {
return roots[i];
}
return NULL;
}
{
}
{
/* if we haven't checked the quota_over_flag yet, do it now */
}
{
}
{
int ret;
}
/* Get the value first. This call may also update quota limits if
they're defined externally. */
if (ret <= 0)
return ret;
&ignored) < 0)
return -1;
*limit_r = bytes_limit;
*limit_r = count_limit;
else
*limit_r = 0;
if (kilobytes) {
}
return *limit_r == 0 ? 0 : 1;
}
{
struct dict_transaction_context *trans;
return -1;
}
key = "storage";
key = "bytes";
key = "messages";
else {
return -1;
}
struct dict_settings set;
return -1;
}
*error_r = "Internal quota limit update error";
return -1;
}
return 0;
}
{
struct quota_transaction_context *ctx;
struct quota_root *const *rootp;
if (!(*rootp)->auto_updating)
}
/* ignore quota for dsync */
}
return ctx;
}
{
struct quota_root *const *roots;
const char *mailbox_name;
unsigned int i, count;
int ret;
if (ctx->limits_set)
return 0;
/* find the lowest quota limits from all roots and use them */
for (i = 0; i < count; i++) {
continue;
&ignored) < 0) {
return -1;
}
if (!ignored)
if (bytes_limit > 0) {
if (ret > 0) {
/* over quota */
ctx->bytes_ceil = 0;
ctx->bytes_ceil2 = 0;
} else {
}
} else if (ret < 0) {
return -1;
}
}
if (count_limit > 0) {
if (ret > 0) {
/* over quota */
ctx->count_ceil = 0;
} else {
}
} else if (ret < 0) {
return -1;
}
}
}
return 0;
}
{
struct program_client_settings set = {
.client_connect_timeout_msecs = 1000,
};
struct program_client *pc;
}
socket_path = args[0];
} else {
scheme = "unix";
}
if (*socket_path != '/' &&
args++;
error);
return;
}
(void)program_client_run(pc);
}
struct quota_root *root)
{
struct quota_warning_rule *warnings;
unsigned int i, count;
const char *reason;
if (count == 0)
return;
&bytes_current, &bytes_limit) < 0)
return;
&count_current, &count_limit) < 0)
return;
bytes_before = 0;
else
count_before = 0;
else
for (i = 0; i < count; i++) {
if (quota_warning_match(&warnings[i],
&reason)) {
break;
}
}
}
{
struct quota_rule *rule;
struct quota_root *const *roots;
unsigned int i, count;
const char *mailbox_name;
int ret = 0;
ret = -1;
(void)mail_namespace_find_unalias(
for (i = 0; i < count; i++) {
continue;
/* mailbox not included in quota */
continue;
}
ret = -1;
else if (!ctx->sync_transaction)
}
/* execute quota warnings after all updates. this makes it
work correctly regardless of whether backend.get_resource()
returns updated values before backend.update() or not.
warnings aren't executed when dsync bring the user over,
because the user probably already got the warning on the
other replica. */
} T_END;
return ret;
}
const char **quota_over_script_r,
const char **quota_over_flag_r,
bool *status_r)
{
if (*quota_over_script_r == NULL) {
i_debug("quota: quota_over_flag check: "
"%s unset - skipping", name);
}
return FALSE;
}
/* e.g.: quota_over_flag_value=TRUE or quota_over_flag_value=* */
i_debug("quota: quota_over_flag check: "
"%s unset - skipping", name);
}
return FALSE;
}
/* compare quota_over_flag's value (that comes from userdb) to
quota_over_flag_value and save the result. */
return TRUE;
}
{
const char *quota_over_script, *quota_over_flag;
const char *const *resources;
unsigned int i;
bool cur_overquota = FALSE;
bool quota_over_status;
int ret;
if (root->quota_over_flag_checked)
return;
/* userdb's quota_over_flag lookup is too old. */
i_debug("quota: quota_over_flag check: "
"Flag lookup time is too old - skipping");
}
return;
}
/* we don't know whether the quota_over_script was executed
before hibernation. just assume that it was, so we don't
unnecessarily call it too often. */
i_debug("quota: quota_over_flag check: "
"Session was already hibernated - skipping");
}
return;
}
return;
if (ret < 0) {
/* can't reliably verify this */
i_debug("quota: Quota %s lookup failed - can't verify quota_over_flag",
resources[i]);
}
return;
}
i_debug("quota: quota_over_flag check: %s ret=%d value=%llu limit=%llu",
(unsigned long long)value,
(unsigned long long)limit);
}
}
i_debug("quota: quota_over_flag=%d(%s) vs currently overquota=%d",
quota_over_status ? 1 : 0,
cur_overquota ? 1 : 0);
}
if (cur_overquota != quota_over_status) {
"quota_over_flag mismatch");
}
}
{
struct quota_root *const *roots;
unsigned int i, count;
const char *name;
for (i = 0; i < count; i++) {
}
}
{
}
{
int ret;
if (quota_transaction_set_limits(ctx) < 0)
return -1;
if (ctx->no_quota_updates)
return 1;
enum mail_error error;
if (error == MAIL_ERROR_EXPUNGED) {
/* mail being copied was already expunged. it'll fail,
so just return success for the quota allocated. */
return 1;
}
i_error("quota: Failed to get mail size (box=%s, uid=%u): %s",
return -1;
}
if (ret <= 0)
return ret;
/* with quota_try_alloc() we want to keep track of how many bytes
optimization. this of course doesn't work perfectly if
quota_alloc() or quota_free*() was already used within the same
transaction, but that doesn't normally happen. */
return 1;
}
{
return -1;
if (quota_transaction_set_limits(ctx) < 0)
return -1;
if (ctx->no_quota_updates)
return 1;
/* this is a virtual function mainly for trash plugin and similar,
which may automatically delete mails to stay under quota. */
}
{
struct quota_root *const *roots;
unsigned int i, count;
bool ignore;
int ret;
*too_large_r = FALSE;
return 1;
/* limit reached. only thing left to do now is to set too_large_r. */
for (i = 0; i < count; i++) {
continue;
&ignore);
if (ret < 0)
return -1;
/* if size is bigger than any limit, then
it is bigger than the lowest limit */
*too_large_r = TRUE;
break;
}
}
return 0;
}
{
if (ctx->auto_updating)
return;
ctx->count_used++;
}
{
if (ctx->auto_updating)
return;
else
}
{
ctx->count_used--;
}
enum quota_recalculate recalculate)
{
}