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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ioloop.h"
#include "imap-arg.h"
#include "imapc-storage.h"
#include "mailbox-list-private.h"
#include "quota-private.h"
struct imapc_quota_refresh_root {
const char *name;
unsigned int order;
uint64_t bytes_cur, count_cur;
uint64_t bytes_limit, count_limit;
};
struct imapc_quota_refresh {
pool_t pool;
const char *box_name;
ARRAY(struct imapc_quota_refresh_root) roots;
};
struct imapc_quota_root {
struct quota_root root;
const char *box_name, *root_name;
struct mail_namespace *imapc_ns;
struct imapc_storage_client *client;
bool initialized;
uint64_t bytes_last, count_last;
struct timeval last_refresh;
struct imapc_quota_refresh refresh;
};
extern struct quota_backend quota_backend_imapc;
static struct quota_root *imapc_quota_alloc(void)
{
struct imapc_quota_root *root;
root = i_new(struct imapc_quota_root, 1);
return &root->root;
}
static void handle_box_param(struct quota_root *_root, const char *param_value)
{
((struct imapc_quota_root *)_root)->box_name = p_strdup(_root->pool, param_value);
}
static void handle_root_param(struct quota_root *_root, const char *param_value)
{
((struct imapc_quota_root *)_root)->root_name = p_strdup(_root->pool, param_value);
}
static int imapc_quota_init(struct quota_root *_root, const char *args,
const char **error_r)
{
struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
const struct quota_param_parser imapc_params[] = {
{.param_name = "box=", .param_handler = handle_box_param},
{.param_name = "root=", .param_handler = handle_root_param},
quota_param_ns,
{.param_name = NULL}
};
_root->auto_updating = TRUE;
event_set_append_log_prefix(root->root.backend.event, "quota-imapc: ");
if (quota_parse_parameters(_root, &args, error_r, imapc_params, TRUE) < 0)
return -1;
if (root->box_name == NULL && root->root_name == NULL)
root->box_name = "INBOX";
/* we'll never try to enforce the quota - it's just a lot of
unnecessary remote GETQUOTA calls. */
_root->no_enforcing = TRUE;
return 0;
}
static void imapc_quota_deinit(struct quota_root *_root)
{
i_free(_root);
}
static void
imapc_quota_root_namespace_added(struct quota_root *_root,
struct mail_namespace *ns)
{
struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
if (root->imapc_ns == NULL)
root->imapc_ns = ns;
}
static struct imapc_quota_refresh *
imapc_quota_root_refresh_find(struct imapc_storage_client *client)
{
struct imapc_storage *storage = client->_storage;
struct quota *quota;
struct quota_root *const *rootp;
i_assert(storage != NULL);
quota = quota_get_mail_user_quota(storage->storage.user);
i_assert(quota != NULL);
/* find the quota root that is being refreshed */
array_foreach("a->roots, rootp) {
if ((*rootp)->backend.name == quota_backend_imapc.name) {
struct imapc_quota_root *root =
(struct imapc_quota_root *)*rootp;
if (root->refresh.pool != NULL)
return &root->refresh;
}
}
return NULL;
}
static struct imapc_quota_refresh_root *
imapc_quota_refresh_root_get(struct imapc_quota_refresh *refresh,
const char *root_name)
{
struct imapc_quota_refresh_root *refresh_root;
array_foreach_modifiable(&refresh->roots, refresh_root) {
if (strcmp(refresh_root->name, root_name) == 0)
return refresh_root;
}
refresh_root = array_append_space(&refresh->roots);
refresh_root->order = UINT_MAX;
refresh_root->name = p_strdup(refresh->pool, root_name);
refresh_root->bytes_limit = (uint64_t)-1;
refresh_root->count_limit = (uint64_t)-1;
return refresh_root;
}
static void imapc_untagged_quotaroot(const struct imapc_untagged_reply *reply,
struct imapc_storage_client *client)
{
struct imapc_quota_refresh *refresh;
struct imapc_quota_refresh_root *refresh_root;
const char *mailbox_name, *root_name;
unsigned int i;
if (!imap_arg_get_astring(&reply->args[0], &mailbox_name))
return;
if ((refresh = imapc_quota_root_refresh_find(client)) == NULL ||
refresh->box_name == NULL ||
strcmp(refresh->box_name, mailbox_name) != 0) {
/* unsolicited QUOTAROOT reply - ignore */
return;
}
if (array_count(&refresh->roots) > 0) {
/* duplicate QUOTAROOT reply - ignore */
return;
}
i = 1;
while (imap_arg_get_astring(&reply->args[i], &root_name)) {
refresh_root = imapc_quota_refresh_root_get(refresh, root_name);
refresh_root->order = i;
i++;
}
}
static void imapc_untagged_quota(const struct imapc_untagged_reply *reply,
struct imapc_storage_client *client)
{
const struct imap_arg *list;
struct imapc_quota_refresh *refresh;
struct imapc_quota_refresh_root *refresh_root;
const char *root_name, *resource, *value_str, *limit_str;
uint64_t value, limit;
unsigned int i;
if (!imap_arg_get_astring(&reply->args[0], &root_name) ||
!imap_arg_get_list(&reply->args[1], &list))
return;
if ((refresh = imapc_quota_root_refresh_find(client)) == NULL) {
/* unsolicited QUOTA reply - ignore */
return;
}
refresh_root = imapc_quota_refresh_root_get(refresh, root_name);
for (i = 0; list[i].type != IMAP_ARG_EOL; i += 3) {
if (!imap_arg_get_atom(&list[i], &resource) ||
!imap_arg_get_atom(&list[i+1], &value_str) ||
!imap_arg_get_atom(&list[i+2], &limit_str) ||
/* RFC2087 uses 32bit number, but be ready for future */
str_to_uint64(value_str, &value) < 0 ||
str_to_uint64(limit_str, &limit) < 0)
return;
if (strcasecmp(resource, QUOTA_NAME_STORAGE_KILOBYTES) == 0) {
refresh_root->bytes_cur = value * 1024;
refresh_root->bytes_limit = limit * 1024;
} else if (strcasecmp(resource, QUOTA_NAME_MESSAGES) == 0) {
refresh_root->count_cur = value;
refresh_root->count_limit = limit;
}
}
}
static bool imapc_quota_client_init(struct imapc_quota_root *root)
{
struct mailbox_list *list;
struct mail_storage *storage;
if (root->initialized)
return root->client != NULL;
root->initialized = TRUE;
list = root->imapc_ns->list;
if (mailbox_list_get_storage(&list, "", &storage) == 0 &&
strcmp(storage->name, IMAPC_STORAGE_NAME) != 0) {
/* non-imapc namespace, skip */
if ((storage->class_flags &
MAIL_STORAGE_CLASS_FLAG_NOQUOTA) == 0) {
e_warning(root->root.backend.event,
"Namespace '%s' is not imapc, "
"skipping for imapc quota",
root->imapc_ns->prefix);
}
return FALSE;
}
root->client = ((struct imapc_storage *)storage)->client;
imapc_storage_client_register_untagged(root->client, "QUOTAROOT",
imapc_untagged_quotaroot);
imapc_storage_client_register_untagged(root->client, "QUOTA",
imapc_untagged_quota);
return TRUE;
}
static void imapc_quota_refresh_init(struct imapc_quota_refresh *refresh)
{
i_assert(refresh->pool == NULL);
refresh->pool = pool_alloconly_create("imapc quota refresh", 256);
p_array_init(&refresh->roots, refresh->pool, 4);
}
static void
imapc_quota_refresh_update(struct quota *quota,
struct imapc_quota_refresh *refresh)
{
struct quota_root *const *rootp;
const struct imapc_quota_refresh_root *refresh_root;
if (array_count(&refresh->roots) == 0) {
e_error(quota_backend_imapc.event,
"imapc didn't return any QUOTA results");
return;
}
/* use the first quota root for everything */
refresh_root = array_front(&refresh->roots);
array_foreach("a->roots, rootp) {
if ((*rootp)->backend.name == quota_backend_imapc.name) {
struct imapc_quota_root *root =
(struct imapc_quota_root *)*rootp;
root->bytes_last = refresh_root->bytes_cur;
root->count_last = refresh_root->count_cur;
/* If limits are higher than what dovecot can handle
consider them unlimited. */
if (refresh_root->bytes_limit > INT64_MAX)
root->root.bytes_limit = 0;
else
root->root.bytes_limit = refresh_root->bytes_limit;
if (refresh_root->count_limit > INT64_MAX)
root->root.count_limit = 0;
else
root->root.count_limit = refresh_root->count_limit;
}
}
}
static void
imapc_quota_refresh_deinit(struct quota *quota,
struct imapc_quota_refresh *refresh, bool success)
{
if (success)
imapc_quota_refresh_update(quota, refresh);
pool_unref(&refresh->pool);
i_zero(refresh);
}
static int
imapc_quota_refresh_root_order_cmp(const struct imapc_quota_refresh_root *root1,
const struct imapc_quota_refresh_root *root2)
{
if (root1->order < root2->order)
return -1;
else if (root1->order > root2->order)
return 1;
else
return 0;
}
static int imapc_quota_refresh_mailbox(struct imapc_quota_root *root,
const char **error_r)
{
struct imapc_simple_context sctx;
struct imapc_command *cmd;
i_assert(root->box_name != NULL);
/* ask quotas for the configured mailbox */
imapc_quota_refresh_init(&root->refresh);
root->refresh.box_name = root->box_name;
imapc_simple_context_init(&sctx, root->client);
cmd = imapc_client_cmd(root->client->client,
imapc_simple_callback, &sctx);
imapc_command_sendf(cmd, "GETQUOTAROOT %s", root->box_name);
imapc_simple_run(&sctx, &cmd);
/* if there are multiple quota roots, use the first one returned by
the QUOTAROOT */
array_sort(&root->refresh.roots, imapc_quota_refresh_root_order_cmp);
imapc_quota_refresh_deinit(root->root.quota, &root->refresh,
sctx.ret == 0);
if (sctx.ret < 0)
*error_r = t_strdup_printf(
"GETQUOTAROOT %s failed: %s",
root->box_name,
mail_storage_get_last_internal_error(
&root->client->_storage->storage, NULL));
return sctx.ret;
}
static int imapc_quota_refresh_root(struct imapc_quota_root *root,
const char **error_r)
{
struct imapc_simple_context sctx;
struct imapc_command *cmd;
i_assert(root->root_name != NULL);
/* ask quotas for the configured quota root */
imapc_quota_refresh_init(&root->refresh);
imapc_simple_context_init(&sctx, root->client);
cmd = imapc_client_cmd(root->client->client,
imapc_simple_callback, &sctx);
imapc_command_sendf(cmd, "GETQUOTA %s", root->root_name);
imapc_simple_run(&sctx, &cmd);
/* there shouldn't be more than one QUOTA reply, but ignore anyway
anything we didn't expect. */
while (array_count(&root->refresh.roots) > 0) {
const struct imapc_quota_refresh_root *refresh_root =
array_front(&root->refresh.roots);
if (strcmp(refresh_root->name, root->root_name) == 0)
break;
array_pop_front(&root->refresh.roots);
}
imapc_quota_refresh_deinit(root->root.quota, &root->refresh,
sctx.ret == 0);
if (sctx.ret < 0)
*error_r = t_strdup_printf(
"GETQUOTA %s failed: %s",
root->root_name,
mail_storage_get_last_internal_error(
&root->client->_storage->storage, NULL));
return sctx.ret;
}
static int imapc_quota_refresh(struct imapc_quota_root *root,
const char **error_r)
{
enum imapc_capability capa;
int ret;
if (root->imapc_ns == NULL) {
/* imapc namespace is missing - disable this quota backend */
return 0;
}
if (root->last_refresh.tv_sec == ioloop_timeval.tv_sec &&
root->last_refresh.tv_usec == ioloop_timeval.tv_usec)
return 0;
if (!imapc_quota_client_init(root))
return 0;
if (imapc_client_get_capabilities(root->client->client, &capa) < 0) {
*error_r = "Failed to get server capabilities";
return -1;
}
if ((capa & IMAPC_CAPABILITY_QUOTA) == 0) {
/* no QUOTA capability - disable quota */
e_warning(root->root.backend.event,
"Remote IMAP server doesn't support QUOTA - disabling");
root->client = NULL;
return 0;
}
if (root->root_name == NULL)
ret = imapc_quota_refresh_mailbox(root, error_r);
else
ret = imapc_quota_refresh_root(root, error_r);
/* set the last_refresh only after the refresh, because it changes
ioloop_timeval. */
root->last_refresh = ioloop_timeval;
return ret;
}
static int imapc_quota_init_limits(struct quota_root *_root,
const char **error_r)
{
struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
return imapc_quota_refresh(root, error_r);
}
static void
imapc_quota_namespace_added(struct quota *quota, struct mail_namespace *ns)
{
struct quota_root **roots;
unsigned int i, count;
roots = array_get_modifiable("a->roots, &count);
for (i = 0; i < count; i++) {
if (roots[i]->backend.name == quota_backend_imapc.name &&
((roots[i]->ns_prefix == NULL &&
ns->type == MAIL_NAMESPACE_TYPE_PRIVATE) ||
roots[i]->ns == ns))
imapc_quota_root_namespace_added(roots[i], ns);
}
}
static const char *const *
imapc_quota_root_get_resources(struct quota_root *root ATTR_UNUSED)
{
static const char *resources_both[] = {
QUOTA_NAME_STORAGE_KILOBYTES,
QUOTA_NAME_MESSAGES,
NULL
};
return resources_both;
}
static enum quota_get_result
imapc_quota_get_resource(struct quota_root *_root, const char *name,
uint64_t *value_r, const char **error_r)
{
struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
if (imapc_quota_refresh(root, error_r) < 0)
return QUOTA_GET_RESULT_INTERNAL_ERROR;
if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
*value_r = root->bytes_last;
else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
*value_r = root->count_last;
else {
*error_r = QUOTA_UNKNOWN_RESOURCE_ERROR_STRING;
return QUOTA_GET_RESULT_UNKNOWN_RESOURCE;
}
return QUOTA_GET_RESULT_LIMITED;
}
static int
imapc_quota_update(struct quota_root *root ATTR_UNUSED,
struct quota_transaction_context *ctx ATTR_UNUSED,
const char **error_r ATTR_UNUSED)
{
return 0;
}
struct quota_backend quota_backend_imapc = {
.name = "imapc",
.v = {
.alloc = imapc_quota_alloc,
.init = imapc_quota_init,
.deinit = imapc_quota_deinit,
.init_limits = imapc_quota_init_limits,
.namespace_added = imapc_quota_namespace_added,
.get_resources = imapc_quota_root_get_resources,
.get_resource = imapc_quota_get_resource,
.update = imapc_quota_update,
}
};
|