summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mail.c
blob: b3991d6f65c575ae4a60d78026a39c2be9c71585 (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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "ioloop.h"
#include "buffer.h"
#include "hash.h"
#include "hex-binary.h"
#include "crc32.h"
#include "sha1.h"
#include "hostpid.h"
#include "istream.h"
#include "mail-cache.h"
#include "mail-storage-private.h"
#include "message-id.h"
#include "message-part-data.h"
#include "imap-bodystructure.h"

#include <time.h>

struct mail *mail_alloc(struct mailbox_transaction_context *t,
			enum mail_fetch_field wanted_fields,
			struct mailbox_header_lookup_ctx *wanted_headers)
{
	struct mail *mail;

	i_assert(wanted_headers == NULL || wanted_headers->box == t->box);

	T_BEGIN {
		mail = t->box->v.mail_alloc(t, wanted_fields, wanted_headers);
		hook_mail_allocated(mail);
	} T_END;

	return mail;
}

void mail_free(struct mail **mail)
{
	struct mail_private *p = (struct mail_private *)*mail;

	/* make sure mailbox_search_*() users don't try to free the mail
	   directly */
	i_assert(!p->search_mail);

	p->v.free(*mail);
	*mail = NULL;
}

void mail_set_seq(struct mail *mail, uint32_t seq)
{
	struct mail_private *p = (struct mail_private *)mail;

	T_BEGIN {
		p->v.set_seq(mail, seq, FALSE);
	} T_END;
}

void mail_set_seq_saving(struct mail *mail, uint32_t seq)
{
	struct mail_private *p = (struct mail_private *)mail;

	T_BEGIN {
		p->v.set_seq(mail, seq, TRUE);
	} T_END;
}

bool mail_set_uid(struct mail *mail, uint32_t uid)
{
	struct mail_private *p = (struct mail_private *)mail;
	bool ret;

	T_BEGIN {
		ret = p->v.set_uid(mail, uid);
	} T_END;
	return ret;
}

bool mail_prefetch(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;
	bool ret;

	T_BEGIN {
		ret = p->v.prefetch(mail);
	} T_END;
	return ret;
}

void mail_add_temp_wanted_fields(struct mail *mail,
				 enum mail_fetch_field fields,
				 struct mailbox_header_lookup_ctx *headers)
{
	struct mail_private *p = (struct mail_private *)mail;

	i_assert(headers == NULL || headers->box == mail->box);

	p->v.add_temp_wanted_fields(mail, fields, headers);
}

static bool index_mail_get_age_days(struct mail *mail, int *days_r)
{
	int age_days;
	const struct mail_index_header *hdr =
		mail_index_get_header(mail->transaction->view);
	int n_days = N_ELEMENTS(hdr->day_first_uid);

	for (age_days = 0; age_days < n_days; age_days++) {
		if (mail->uid >= hdr->day_first_uid[age_days])
			break;
	}

	if (age_days == n_days) {
		/* mail is too old, cannot determine its age from
		   day_first_uid[]. */
		return FALSE;
	}

	if (hdr->day_stamp != 0) {
		/* offset for hdr->day_stamp */
		age_days += (ioloop_time - hdr->day_stamp) / (3600 * 24);
	}
	*days_r = age_days;
	return TRUE;
}

void mail_event_create(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;
	int age_days;

	if (p->_event != NULL)
		return;
	p->_event = event_create(mail->box->event);
	event_add_category(p->_event, &event_category_mail);
	event_add_int(p->_event, "seq", mail->seq);
	event_add_int(p->_event, "uid", mail->uid);
	/* Add mail age field to event. */
	if (index_mail_get_age_days(mail, &age_days))
		event_add_int(p->_event, "mail_age_days", age_days);

	char uid_buf[MAX_INT_STRLEN];
	const char *prefix = t_strconcat(
		p->mail.saving ? "saving UID " : "UID ",
		dec2str_buf(uid_buf, p->mail.uid), ": ", NULL);
	event_set_append_log_prefix(p->_event, prefix);
}

struct event *mail_event(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	mail_event_create(mail);
	return p->_event;
}

enum mail_flags mail_get_flags(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	return p->v.get_flags(mail);
}

uint64_t mail_get_modseq(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	return p->v.get_modseq(mail);
}

uint64_t mail_get_pvt_modseq(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	return p->v.get_pvt_modseq(mail);
}

const char *const *mail_get_keywords(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	return p->v.get_keywords(mail);
}

const ARRAY_TYPE(keyword_indexes) *mail_get_keyword_indexes(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	return p->v.get_keyword_indexes(mail);
}

int mail_get_parts(struct mail *mail, struct message_part **parts_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_parts(mail, parts_r);
	} T_END;
	return ret;
}

int mail_get_date(struct mail *mail, time_t *date_r, int *timezone_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_date(mail, date_r, timezone_r);
	} T_END;
	return ret;
}

int mail_get_received_date(struct mail *mail, time_t *date_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_received_date(mail, date_r);
	} T_END;
	return ret;
}

int mail_get_save_date(struct mail *mail, time_t *date_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_save_date(mail, date_r);
	} T_END;
	return ret;
}

int mail_get_virtual_size(struct mail *mail, uoff_t *size_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_virtual_size(mail, size_r);
	} T_END;
	return ret;
}

int mail_get_physical_size(struct mail *mail, uoff_t *size_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_physical_size(mail, size_r);
	} T_END;
	return ret;
}

int mail_get_first_header(struct mail *mail, const char *field,
			  const char **value_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_first_header(mail, field, FALSE, value_r);
	} T_END;
	return ret;
}

int mail_get_first_header_utf8(struct mail *mail, const char *field,
			       const char **value_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_first_header(mail, field, TRUE, value_r);
	} T_END;
	return ret;
}

int mail_get_headers(struct mail *mail, const char *field,
		     const char *const **value_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_headers(mail, field, FALSE, value_r);
	} T_END;
	return ret;
}

int mail_get_headers_utf8(struct mail *mail, const char *field,
			  const char *const **value_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.get_headers(mail, field, TRUE, value_r);
	} T_END;
	return ret;
}

int mail_get_header_stream(struct mail *mail,
			   struct mailbox_header_lookup_ctx *headers,
			   struct istream **stream_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	i_assert(headers->count > 0);
	i_assert(headers->box == mail->box);

	T_BEGIN {
		ret = p->v.get_header_stream(mail, headers, stream_r);
	} T_END;
	return ret;
}

void mail_set_aborted(struct mail *mail)
{
	mail_storage_set_error(mail->box->storage, MAIL_ERROR_LOOKUP_ABORTED,
			       "Mail field not cached");
}

int mail_get_stream(struct mail *mail, struct message_size *hdr_size,
		    struct message_size *body_size, struct istream **stream_r)
{
	return mail_get_stream_because(mail, hdr_size, body_size,
				       "mail stream", stream_r);
}

int mail_get_stream_because(struct mail *mail, struct message_size *hdr_size,
			    struct message_size *body_size,
			    const char *reason, struct istream **stream_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) {
		mail_set_aborted(mail);
		return -1;
	}
	T_BEGIN {
		p->get_stream_reason = reason;
		ret = p->v.get_stream(mail, TRUE, hdr_size, body_size, stream_r);
		p->get_stream_reason = "";
	} T_END;
	i_assert(ret < 0 || (*stream_r)->blocking);
	return ret;
}

int mail_get_hdr_stream(struct mail *mail, struct message_size *hdr_size,
			struct istream **stream_r)
{
	return mail_get_hdr_stream_because(mail, hdr_size, "header stream", stream_r);
}

int mail_get_hdr_stream_because(struct mail *mail,
				struct message_size *hdr_size,
				const char *reason, struct istream **stream_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) {
		mail_set_aborted(mail);
		return -1;
	}
	T_BEGIN {
		p->get_stream_reason = reason;
		ret = p->v.get_stream(mail, FALSE, hdr_size, NULL, stream_r);
		p->get_stream_reason = "";
	} T_END;
	i_assert(ret < 0 || (*stream_r)->blocking);
	return ret;
}

int mail_get_binary_stream(struct mail *mail, const struct message_part *part,
			   bool include_hdr, uoff_t *size_r,
			   bool *binary_r, struct istream **stream_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) {
		mail_set_aborted(mail);
		return -1;
	}
	T_BEGIN {
		ret = p->v.get_binary_stream(mail, part, include_hdr,
					     size_r, NULL, binary_r, stream_r);
	} T_END;
	i_assert(ret < 0 || (*stream_r)->blocking);
	return ret;
}

int mail_get_binary_size(struct mail *mail, const struct message_part *part,
			 bool include_hdr, uoff_t *size_r,
			 unsigned int *lines_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	bool binary;
	int ret;

	T_BEGIN {
		ret = p->v.get_binary_stream(mail, part, include_hdr,
					     size_r, lines_r, &binary, NULL);
	} T_END;
	return ret;
}

int mail_get_special(struct mail *mail, enum mail_fetch_field field,
		     const char **value_r)
{
	struct mail_private *p = (struct mail_private *)mail;

	if (p->v.get_special(mail, field, value_r) < 0)
		return -1;
	i_assert(*value_r != NULL);
	return 0;
}

int mail_get_backend_mail(struct mail *mail, struct mail **real_mail_r)
{
	struct mail_private *p = (struct mail_private *)mail;
	return p->v.get_backend_mail(mail, real_mail_r);
}

int mail_get_message_id(struct mail *mail, const char **value_r)
{
	const char *hdr_value, *msgid_bare;
	int ret;

	*value_r = NULL;

	ret = mail_get_first_header(mail, "Message-ID", &hdr_value);
	if (ret <= 0)
		return ret;

	msgid_bare = message_id_get_next(&hdr_value);
	if (msgid_bare == NULL)
		return 0;

	/* Complete the message ID with surrounding `<' and `>'. */
	*value_r = t_strconcat("<",  msgid_bare, ">", NULL);
	return 1;
}

void mail_update_flags(struct mail *mail, enum modify_type modify_type,
		       enum mail_flags flags)
{
	struct mail_private *p = (struct mail_private *)mail;

	p->v.update_flags(mail, modify_type, flags);
}

void mail_update_keywords(struct mail *mail, enum modify_type modify_type,
			  struct mail_keywords *keywords)
{
	struct mail_private *p = (struct mail_private *)mail;

	p->v.update_keywords(mail, modify_type, keywords);
}

void mail_update_modseq(struct mail *mail, uint64_t min_modseq)
{
	struct mail_private *p = (struct mail_private *)mail;

	p->v.update_modseq(mail, min_modseq);
}

void mail_update_pvt_modseq(struct mail *mail, uint64_t min_pvt_modseq)
{
	struct mail_private *p = (struct mail_private *)mail;

	p->v.update_pvt_modseq(mail, min_pvt_modseq);
}

void mail_update_pop3_uidl(struct mail *mail, const char *uidl)
{
	struct mail_private *p = (struct mail_private *)mail;

	if (p->v.update_pop3_uidl != NULL)
		p->v.update_pop3_uidl(mail, uidl);
}

void mail_expunge(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;

	T_BEGIN {
		p->v.expunge(mail);
	} T_END;
	mail_expunge_requested_event(mail);
}

void mail_autoexpunge(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;
	p->autoexpunged = TRUE;
	mail_expunge(mail);
	p->autoexpunged = FALSE;
}

void mail_set_expunged(struct mail *mail)
{
	mail_storage_set_error(mail->box->storage, MAIL_ERROR_EXPUNGED,
			       "Message was expunged");
	mail->expunged = TRUE;
}

int mail_precache(struct mail *mail)
{
	struct mail_private *p = (struct mail_private *)mail;
	int ret;

	T_BEGIN {
		ret = p->v.precache(mail);
	} T_END;
	return ret;
}

void mail_set_cache_corrupted(struct mail *mail,
			      enum mail_fetch_field field,
			      const char *reason)
{
	struct mail_private *p = (struct mail_private *)mail;
	p->v.set_cache_corrupted(mail, field, reason);
}

void mail_generate_guid_128_hash(const char *guid, guid_128_t guid_128_r)
{
	unsigned char sha1_sum[SHA1_RESULTLEN];
	buffer_t buf;

	if (guid_128_from_string(guid, guid_128_r) < 0) {
		/* not 128bit hex. use a hash of it instead. */
		buffer_create_from_data(&buf, guid_128_r, GUID_128_SIZE);
		buffer_set_used_size(&buf, 0);
		sha1_get_digest(guid, strlen(guid), sha1_sum);
#if SHA1_RESULTLEN < GUID_128_SIZE
#  error not possible
#endif
		buffer_append(&buf,
			      sha1_sum + SHA1_RESULTLEN - GUID_128_SIZE,
			      GUID_128_SIZE);
	}
}

static bool
mail_message_has_attachment(struct message_part *part,
			    const struct message_part_attachment_settings *set)
{
	for (; part != NULL; part = part->next) {
		if (message_part_is_attachment(part, set) ||
		    mail_message_has_attachment(part->children, set))
			return TRUE;
	}

	return FALSE;
}

bool mail_has_attachment_keywords(struct mail *mail)
{
	const char *const *kw = mail_get_keywords(mail);
	return (str_array_icase_find(kw, MAIL_KEYWORD_HAS_ATTACHMENT) !=
		str_array_icase_find(kw, MAIL_KEYWORD_HAS_NO_ATTACHMENT));
}

static int mail_parse_parts(struct mail *mail, struct message_part **parts_r)
{
	const char *structure, *error;
	struct mail_private *pmail = (struct mail_private*)mail;

	/* need to get bodystructure first */
	if (mail_get_special(mail, MAIL_FETCH_IMAP_BODYSTRUCTURE,
			     &structure) < 0) {
		/* Don't bother logging an error. See
		   mail_set_attachment_keywords(). */
		return -1;
	}
	if (imap_bodystructure_parse_full(structure, pmail->data_pool, parts_r,
					  &error) < 0) {
		mail_set_cache_corrupted(mail, MAIL_FETCH_IMAP_BODYSTRUCTURE,
					 error);
		return -1;
	}
	return 0;
}

int mail_set_attachment_keywords(struct mail *mail)
{
	int ret;
	const struct mail_storage_settings *mail_set =
		mail_storage_get_settings(mailbox_get_storage(mail->box));

	const char *const keyword_has_attachment[] = {
		MAIL_KEYWORD_HAS_ATTACHMENT,
		NULL,
	};
	const char *const keyword_has_no_attachment[] = {
		MAIL_KEYWORD_HAS_NO_ATTACHMENT,
		NULL
	};
	struct message_part_attachment_settings set = {
		.content_type_filter =
			mail_set->parsed_mail_attachment_content_type_filter,
		.exclude_inlined =
			mail_set->parsed_mail_attachment_exclude_inlined,
	};
	struct mail_keywords *kw_has = NULL, *kw_has_not = NULL;

	/* walk all parts and see if there is an attachment */
	struct message_part *parts;
	if (mail_get_parts(mail, &parts) < 0) {
		/* Callers don't really care about the exact error, and
		   critical errors were already logged. Most importantly we
		   don't want to log MAIL_ERROR_LOOKUP_ABORTED since that is
		   an expected error. */
		ret = -1;
	} else if (parts->data == NULL &&
		   mail_parse_parts(mail, &parts) < 0) {
		ret = -1;
	} else if (mailbox_keywords_create(mail->box, keyword_has_attachment, &kw_has) < 0 ||
		   mailbox_keywords_create(mail->box, keyword_has_no_attachment, &kw_has_not) < 0) {
		mail_set_critical(mail, "Failed to add attachment keywords: "
				  "mailbox_keywords_create(%s) failed: %s",
				  mailbox_get_vname(mail->box),
				  mail_storage_get_last_internal_error(mail->box->storage, NULL));
		ret = -1;
	} else {
		bool has_attachment = mail_message_has_attachment(parts, &set);

		/* make sure only one of the keywords gets set */
		mail_update_keywords(mail, MODIFY_REMOVE, has_attachment ? kw_has_not : kw_has);
		mail_update_keywords(mail, MODIFY_ADD, has_attachment ? kw_has : kw_has_not);
		ret = has_attachment ? 1 : 0;
	}

	if (kw_has != NULL)
		mailbox_keywords_unref(&kw_has);
	if (kw_has_not != NULL)
		mailbox_keywords_unref(&kw_has_not);

	return ret;
}

bool mail_stream_access_start(struct mail *mail)
{
	if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) {
		mail_set_aborted(mail);
		return FALSE;
	}
	mail->mail_stream_accessed = TRUE;
	mail_event_create(mail);
	return TRUE;
}

bool mail_metadata_access_start(struct mail *mail)
{
	if (mail->lookup_abort >= MAIL_LOOKUP_ABORT_NOT_IN_CACHE) {
		mail_set_aborted(mail);
		return FALSE;
	}
	mail->mail_metadata_accessed = TRUE;
	mail_event_create(mail);
	return TRUE;
}

void mail_opened_event(struct mail *mail)
{
	struct mail_private *pmail =
		container_of(mail, struct mail_private, mail);
	struct event_passthrough *e =
		event_create_passthrough(mail_event(mail))->
		set_name("mail_opened")->
		add_str("reason", pmail->get_stream_reason);
	if (pmail->get_stream_reason != NULL)
		e_debug(e->event(), "Opened mail because: %s",
			pmail->get_stream_reason);
	else
		e_debug(e->event(), "Opened mail");
}

void mail_expunge_requested_event(struct mail *mail)
{
	struct event_passthrough *e =
		event_create_passthrough(mail_event(mail))->
		set_name("mail_expunge_requested")->
		add_int("uid", mail->uid)->
		add_int("seq", mail->seq);
	e_debug(e->event(), "Expunge requested");
}