summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/managesieve/managesieve-quota.c
blob: 07b58caf34678ace7f52f640e8ef9fa69812235a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "strfuncs.h"

#include "sieve.h"
#include "sieve-storage.h"

#include "managesieve-client.h"
#include "managesieve-quota.h"

uint64_t managesieve_quota_max_script_size(struct client *client)
{
	return sieve_storage_quota_max_script_size(client->storage);
}

bool managesieve_quota_check_validsize(struct client_command_context *cmd,
				       size_t size)
{
	struct client *client = cmd->client;
	uint64_t limit;

	if (!sieve_storage_quota_validsize(client->storage, size, &limit)) {
		const char *error_msg;

		error_msg = t_strdup_printf(
			"Script is too large (max %llu bytes).",
			(unsigned long long int)limit);

		struct event_passthrough *e =
			client_command_create_finish_event(cmd);
		e_debug(e->event(),
			"Script size check failed (size %zu bytes): %s",
			size, error_msg);

		client_send_noresp(client, "QUOTA/MAXSIZE", error_msg);
		return FALSE;
	}
	return TRUE;
}

bool managesieve_quota_check_all(struct client_command_context *cmd,
				 const char *scriptname, size_t size)
{
	struct client *client = cmd->client;
	enum sieve_storage_quota quota;
	uint64_t limit;
	const char *resp_code = NULL, *error_msg = NULL;
	int ret;

	ret = sieve_storage_quota_havespace(client->storage, scriptname,
					    size, &quota, &limit);
	if (ret > 0)
		return TRUE;
	if (ret < 0) {
		client_command_storage_error(
			cmd, "Failed to check quota for script `%s' "
			     "(size %zu bytes)", scriptname, size);
		return FALSE;
	}

	switch (quota) {
	case SIEVE_STORAGE_QUOTA_MAXSIZE:
		resp_code = "QUOTA/MAXSIZE";
		error_msg = t_strdup_printf("Script is too large "
					    "(max %llu bytes).",
					    (unsigned long long int)limit);
		break;
	case SIEVE_STORAGE_QUOTA_MAXSCRIPTS:
		resp_code = "QUOTA/MAXSCRIPTS";
		error_msg = t_strdup_printf("Script count quota exceeded "
					    "(max %llu scripts).",
					    (unsigned long long int)limit);
		break;
	case SIEVE_STORAGE_QUOTA_MAXSTORAGE:
		resp_code = "QUOTA/MAXSTORAGE";
		error_msg = t_strdup_printf("Script storage quota exceeded "
					    "(max %llu bytes).",
					    (unsigned long long int)limit);
		break;
	default:
		resp_code = "QUOTA";
		error_msg = "Quota exceeded.";
	}

	struct event_passthrough *e =
		client_command_create_finish_event(cmd)->
		add_str("error", error_msg);
	e_debug(e->event(),
		"Quota check failed for script `%s' (size %zu bytes): %s",
		scriptname, size, error_msg);

	client_send_noresp(client, resp_code, error_msg);

	return FALSE;
}