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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
/* MD5 header summing logic was pretty much copy&pasted from popa3d by
Solar Designer */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "buffer.h"
#include "istream.h"
#include "str.h"
#include "write-full.h"
#include "message-parser.h"
#include "mail-index.h"
#include "mbox-storage.h"
#include "mbox-md5.h"
#include "mbox-sync-private.h"
#define IS_LWSP_LF(c) (IS_LWSP(c) || (c) == '\n')
struct mbox_sync_header_func {
const char *header;
bool (*func)(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr);
};
struct mbox_flag_type mbox_status_flags[] = {
{ 'R', MAIL_SEEN },
{ 'O', MBOX_NONRECENT_KLUDGE },
{ 0, 0 }
};
struct mbox_flag_type mbox_xstatus_flags[] = {
{ 'A', MAIL_ANSWERED },
{ 'F', MAIL_FLAGGED },
{ 'T', MAIL_DRAFT },
{ 'D', MAIL_DELETED },
{ 0, 0 }
};
static void parse_trailing_whitespace(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
size_t i, space = 0;
/* the value may contain newlines. we can't count whitespace before
and after it as a single contiguous whitespace block, as that may
get us into situation where removing whitespace goes eg.
" \n \n" -> " \n\n" which would then be treated as end of headers.
that could probably be avoided by being careful, but as newlines
should never be there (we don't generate them), it's not worth the
trouble. */
for (i = hdr->full_value_len; i > 0; i--) {
if (!IS_LWSP(hdr->full_value[i-1]))
break;
space++;
}
if ((ssize_t)space > ctx->mail.space) {
i_assert(space != 0);
ctx->mail.offset = ctx->hdr_offset + str_len(ctx->header) + i;
ctx->mail.space = space;
}
}
static enum mail_flags mbox_flag_find(struct mbox_flag_type *flags, char chr)
{
int i;
for (i = 0; flags[i].chr != 0; i++) {
if (flags[i].chr == chr)
return flags[i].flag;
}
return 0;
}
static bool parse_status_flags(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr,
struct mbox_flag_type *flags_list)
{
enum mail_flags flag;
size_t i;
bool duplicates = FALSE;
ctx->mail.flags ^= MBOX_NONRECENT_KLUDGE;
for (i = 0; i < hdr->full_value_len; i++) {
flag = mbox_flag_find(flags_list, hdr->full_value[i]);
if ((ctx->mail.flags & flag) != 0)
duplicates = TRUE;
else
ctx->mail.flags |= flag;
}
ctx->mail.flags ^= MBOX_NONRECENT_KLUDGE;
return duplicates;
}
static bool parse_status(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
if (parse_status_flags(ctx, hdr, mbox_status_flags))
ctx->mail.status_broken = TRUE;
ctx->hdr_pos[MBOX_HDR_STATUS] = str_len(ctx->header);
return TRUE;
}
static bool parse_x_status(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
if (parse_status_flags(ctx, hdr, mbox_xstatus_flags))
ctx->mail.xstatus_broken = TRUE;
ctx->hdr_pos[MBOX_HDR_X_STATUS] = str_len(ctx->header);
return TRUE;
}
static void
parse_imap_keywords_list(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr, size_t pos)
{
struct mailbox *box = &ctx->sync_ctx->mbox->box;
struct index_mailbox_context *ibox = INDEX_STORAGE_CONTEXT(box);
const char *keyword, *error;
size_t keyword_start;
unsigned int idx, count;
count = 0;
while (pos < hdr->full_value_len) {
if (IS_LWSP_LF(hdr->full_value[pos])) {
pos++;
continue;
}
/* read the keyword */
keyword_start = pos;
for (; pos < hdr->full_value_len; pos++) {
if (IS_LWSP_LF(hdr->full_value[pos]))
break;
}
/* add it to index's keyword list if it's not there already */
keyword = t_strndup(hdr->full_value + keyword_start,
pos - keyword_start);
if (mailbox_keyword_is_valid(&ctx->sync_ctx->mbox->box,
keyword, &error)) {
mail_index_keyword_lookup_or_create(box->index,
keyword, &idx);
}
count++;
}
if (count != array_count(ibox->keyword_names)) {
/* need to update this list */
ctx->imapbase_rewrite = TRUE;
ctx->need_rewrite = TRUE;
}
}
static bool parse_x_imap_base(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
size_t i, j, uid_last_pos;
uint32_t uid_validity, uid_last;
if (ctx->seq != 1 || ctx->seen_imapbase ||
ctx->sync_ctx->renumber_uids) {
/* Valid only in first message */
return FALSE;
}
/* <uid-validity> 10x<uid-last> */
for (i = 0, uid_validity = 0; i < hdr->full_value_len; i++) {
if (hdr->full_value[i] < '0' || hdr->full_value[i] > '9') {
if (hdr->full_value[i] != ' ')
return FALSE;
break;
}
uid_validity = uid_validity * 10 + (hdr->full_value[i] - '0');
}
if (uid_validity == 0) {
/* broken */
return FALSE;
}
for (; i < hdr->full_value_len; i++) {
if (!IS_LWSP_LF(hdr->full_value[i]))
break;
}
uid_last_pos = i;
for (uid_last = 0, j = 0; i < hdr->full_value_len; i++, j++) {
if (hdr->full_value[i] < '0' || hdr->full_value[i] > '9') {
if (!IS_LWSP_LF(hdr->full_value[i]))
return FALSE;
break;
}
uid_last = uid_last * 10 + (hdr->full_value[i] - '0');
}
if (j != 10 ||
hdr->full_value_offset != ctx->hdr_offset + str_len(ctx->header)) {
/* uid-last field must be exactly 10 characters to make
rewriting it easier. also don't try to do this if some
headers have been removed */
ctx->imapbase_rewrite = TRUE;
ctx->need_rewrite = TRUE;
} else {
ctx->last_uid_value_start_pos = uid_last_pos;
ctx->sync_ctx->base_uid_last_offset =
hdr->full_value_offset + uid_last_pos;
}
if (ctx->sync_ctx->base_uid_validity == 0) {
/* first time parsing this (ie. we're not rewriting).
save the values. */
ctx->sync_ctx->base_uid_validity = uid_validity;
ctx->sync_ctx->base_uid_last = uid_last;
if (ctx->sync_ctx->next_uid-1 <= uid_last) {
/* new messages have been added since our last sync.
just update our internal next_uid. */
ctx->sync_ctx->next_uid = uid_last+1;
} else {
/* we need to rewrite the next-uid */
ctx->need_rewrite = TRUE;
}
i_assert(ctx->sync_ctx->next_uid > ctx->sync_ctx->prev_msg_uid);
}
ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] = str_len(ctx->header);
ctx->seen_imapbase = TRUE;
T_BEGIN {
parse_imap_keywords_list(ctx, hdr, i);
} T_END;
parse_trailing_whitespace(ctx, hdr);
return TRUE;
}
static bool parse_x_imap(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
if (!parse_x_imap_base(ctx, hdr))
return FALSE;
/* this is the c-client style "FOLDER INTERNAL DATA" message.
skip it. */
ctx->mail.pseudo = TRUE;
return TRUE;
}
static bool parse_x_keywords_real(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
struct mailbox *box = &ctx->sync_ctx->mbox->box;
ARRAY_TYPE(keyword_indexes) keyword_list;
const unsigned int *list;
string_t *keyword;
size_t keyword_start;
unsigned int i, idx, count;
size_t pos;
if (array_is_created(&ctx->mail.keywords))
return FALSE; /* duplicate header, delete */
/* read keyword indexes to temporary array first */
keyword = t_str_new(128);
t_array_init(&keyword_list, 16);
for (pos = 0; pos < hdr->full_value_len; ) {
if (IS_LWSP_LF(hdr->full_value[pos])) {
pos++;
continue;
}
/* read the keyword string */
keyword_start = pos;
for (; pos < hdr->full_value_len; pos++) {
if (IS_LWSP_LF(hdr->full_value[pos]))
break;
}
str_truncate(keyword, 0);
str_append_data(keyword, hdr->full_value + keyword_start,
pos - keyword_start);
if (!mail_index_keyword_lookup(box->index, str_c(keyword),
&idx)) {
/* keyword wasn't found. that means the sent mail
originally contained X-Keywords header. Delete it. */
return FALSE;
}
/* check that the keyword isn't already added there.
we don't want duplicates. */
list = array_get(&keyword_list, &count);
for (i = 0; i < count; i++) {
if (list[i] == idx)
break;
}
if (i == count)
array_push_back(&keyword_list, &idx);
}
/* once we know how many keywords there are, we can allocate the array
from mail_keyword_pool without wasting memory. */
if (array_count(&keyword_list) > 0) {
p_array_init(&ctx->mail.keywords,
ctx->sync_ctx->mail_keyword_pool,
array_count(&keyword_list));
array_append_array(&ctx->mail.keywords, &keyword_list);
}
ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] = str_len(ctx->header);
parse_trailing_whitespace(ctx, hdr);
return TRUE;
}
static bool parse_x_keywords(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
bool ret;
T_BEGIN {
ret = parse_x_keywords_real(ctx, hdr);
} T_END;
return ret;
}
static bool parse_x_uid(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
uint32_t value = 0;
size_t i;
if (ctx->mail.uid != 0) {
/* duplicate */
return FALSE;
}
for (i = 0; i < hdr->full_value_len; i++) {
if (hdr->full_value[i] < '0' || hdr->full_value[i] > '9')
break;
value = value*10 + (hdr->full_value[i] - '0');
}
for (; i < hdr->full_value_len; i++) {
if (!IS_LWSP_LF(hdr->full_value[i])) {
/* broken value */
return FALSE;
}
}
if (ctx->sync_ctx == NULL) {
/* we're in mbox_sync_parse_match_mail().
don't do any extra checks. */
ctx->mail.uid = value;
return TRUE;
}
if (ctx->seq == 1 && !ctx->seen_imapbase) {
/* Don't bother allowing X-UID before X-IMAPbase
header. c-client doesn't allow it either, and this
way the UID doesn't have to be reset if X-IMAPbase
header isn't what we expect it to be. */
return FALSE;
}
if (value == ctx->sync_ctx->next_uid) {
/* X-UID is the next expected one. allow it because
we'd just use this UID anyway. X-IMAPbase header
still needs to be updated for this. */
ctx->sync_ctx->next_uid++;
} else if (value > ctx->sync_ctx->next_uid) {
/* UID is larger than expected. Don't allow it because
incoming mails can contain untrusted X-UID fields,
causing possibly DoS if the UIDs get large enough. */
ctx->mail.uid_broken = TRUE;
return FALSE;
}
if (value <= ctx->sync_ctx->prev_msg_uid) {
/* broken - UIDs must be growing */
ctx->mail.uid_broken = TRUE;
return FALSE;
}
ctx->mail.uid = value;
/* if we had multiple X-UID headers, we could have
uid_broken=TRUE here. */
ctx->mail.uid_broken = FALSE;
if (ctx->sync_ctx->dest_first_mail && ctx->seq != 1) {
/* if we're expunging the first mail, delete this header since
otherwise X-IMAPbase header would be added after this, which
we don't like */
return FALSE;
}
ctx->hdr_pos[MBOX_HDR_X_UID] = str_len(ctx->header);
ctx->parsed_uid = value;
parse_trailing_whitespace(ctx, hdr);
return TRUE;
}
static bool parse_content_length(struct mbox_sync_mail_context *ctx,
struct message_header_line *hdr)
{
uoff_t value = 0;
size_t i;
if (ctx->content_length != UOFF_T_MAX) {
/* duplicate */
return FALSE;
}
for (i = 0; i < hdr->full_value_len; i++) {
if (hdr->full_value[i] < '0' || hdr->full_value[i] > '9')
break;
value = value*10 + (hdr->full_value[i] - '0');
}
for (; i < hdr->full_value_len; i++) {
if (!IS_LWSP_LF(hdr->full_value[i])) {
/* broken value */
return FALSE;
}
}
ctx->content_length = value;
return TRUE;
}
static struct mbox_sync_header_func header_funcs[] = {
{ "Content-Length", parse_content_length },
{ "Status", parse_status },
{ "X-IMAP", parse_x_imap },
{ "X-IMAPbase", parse_x_imap_base },
{ "X-Keywords", parse_x_keywords },
{ "X-Status", parse_x_status },
{ "X-UID", parse_x_uid }
};
static int mbox_sync_bsearch_header_func_cmp(const void *p1, const void *p2)
{
const char *key = p1;
const struct mbox_sync_header_func *func = p2;
return strcasecmp(key, func->header);
}
int mbox_sync_parse_next_mail(struct istream *input,
struct mbox_sync_mail_context *ctx)
{
struct mbox_sync_context *sync_ctx = ctx->sync_ctx;
struct message_header_parser_ctx *hdr_ctx;
struct message_header_line *hdr;
struct mbox_sync_header_func *func;
struct mbox_md5_context *mbox_md5_ctx;
size_t line_start_pos;
int i, ret;
ctx->hdr_offset = ctx->mail.offset;
ctx->mail.flags = MAIL_RECENT; /* default to having recent flag */
ctx->header_first_change = SIZE_MAX;
ctx->header_last_change = 0;
for (i = 0; i < MBOX_HDR_COUNT; i++)
ctx->hdr_pos[i] = SIZE_MAX;
ctx->content_length = UOFF_T_MAX;
str_truncate(ctx->header, 0);
mbox_md5_ctx = ctx->sync_ctx->mbox->md5_v.init();
line_start_pos = 0;
hdr_ctx = message_parse_header_init(input, NULL, 0);
while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
if (hdr->eoh) {
ctx->have_eoh = TRUE;
break;
}
if (!hdr->continued) {
line_start_pos = str_len(ctx->header);
str_append(ctx->header, hdr->name);
str_append_data(ctx->header, hdr->middle, hdr->middle_len);
}
func = bsearch(hdr->name, header_funcs,
N_ELEMENTS(header_funcs), sizeof(*header_funcs),
mbox_sync_bsearch_header_func_cmp);
if (func != NULL) {
if (hdr->continues) {
hdr->use_full_value = TRUE;
continue;
}
if (!func->func(ctx, hdr)) {
/* this header is broken, remove it */
ctx->need_rewrite = TRUE;
str_truncate(ctx->header, line_start_pos);
if (ctx->header_first_change == SIZE_MAX) {
ctx->header_first_change =
line_start_pos;
}
continue;
}
buffer_append(ctx->header, hdr->full_value,
hdr->full_value_len);
} else {
ctx->sync_ctx->mbox->md5_v.more(mbox_md5_ctx, hdr);
buffer_append(ctx->header, hdr->value,
hdr->value_len);
}
if (!hdr->no_newline) {
if (hdr->crlf_newline)
str_append_c(ctx->header, '\r');
str_append_c(ctx->header, '\n');
}
}
i_assert(ret != 0);
message_parse_header_deinit(&hdr_ctx);
ctx->sync_ctx->mbox->md5_v.finish(mbox_md5_ctx, ctx->hdr_md5_sum);
if ((ctx->seq == 1 && !ctx->seen_imapbase) ||
(ctx->seq > 1 && sync_ctx->dest_first_mail)) {
/* missing X-IMAPbase */
ctx->need_rewrite = TRUE;
if (sync_ctx->base_uid_validity == 0) {
/* figure out a new UIDVALIDITY for us. */
sync_ctx->base_uid_validity =
sync_ctx->hdr->uid_validity != 0 &&
!sync_ctx->renumber_uids ?
sync_ctx->hdr->uid_validity :
I_MAX((uint32_t)ioloop_time, 1);
}
}
ctx->body_offset = input->v_offset;
if (input->stream_errno != 0) {
mbox_sync_set_critical(ctx->sync_ctx, "read(%s) failed: %s",
i_stream_get_name(input), i_stream_get_error(input));
return -1;
}
return 0;
}
bool mbox_sync_parse_match_mail(struct mbox_mailbox *mbox,
struct mail_index_view *view, uint32_t seq)
{
struct mbox_sync_mail_context ctx;
struct message_header_parser_ctx *hdr_ctx;
struct message_header_line *hdr;
struct header_func *func;
struct mbox_md5_context *mbox_md5_ctx;
const void *data;
bool expunged;
uint32_t uid;
int ret;
/* we only wish to be sure that this mail actually is what we expect
it to be. If there's X-UID header and it matches our UID, we use it.
Otherwise it could mean that the X-UID header is invalid and it's
just not yet been rewritten. In that case use MD5 sum, if it
exists. */
mail_index_lookup_uid(view, seq, &uid);
i_zero(&ctx);
mbox_md5_ctx = mbox->md5_v.init();
hdr_ctx = message_parse_header_init(mbox->mbox_stream, NULL, 0);
while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
if (hdr->eoh)
break;
func = bsearch(hdr->name, header_funcs,
N_ELEMENTS(header_funcs), sizeof(*header_funcs),
mbox_sync_bsearch_header_func_cmp);
if (func != NULL) {
if (strcasecmp(hdr->name, "X-UID") == 0) {
if (hdr->continues) {
hdr->use_full_value = TRUE;
continue;
}
(void)parse_x_uid(&ctx, hdr);
if (ctx.mail.uid == uid)
break;
}
} else {
mbox->md5_v.more(mbox_md5_ctx, hdr);
}
}
i_assert(ret != 0);
message_parse_header_deinit(&hdr_ctx);
mbox->md5_v.finish(mbox_md5_ctx, ctx.hdr_md5_sum);
if (ctx.mail.uid == uid)
return TRUE;
/* match by MD5 sum */
mbox->mbox_save_md5 = TRUE;
mail_index_lookup_ext(view, seq, mbox->md5hdr_ext_idx,
&data, &expunged);
return data == NULL ? 0 :
memcmp(data, ctx.hdr_md5_sum, 16) == 0;
}
|