summaryrefslogtreecommitdiffstats
path: root/src/lib-index/mail-index-map-hdr.c
blob: 3287a284d25321bf61779f5ca5dcdd5b8102e704 (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
/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "mail-index-private.h"

int mail_index_map_parse_extensions(struct mail_index_map *map)
{
	struct mail_index *index = map->index;
	const struct mail_index_ext_header *ext_hdr;
	unsigned int i, old_count, offset;
	const char *name, *error;
	uint32_t ext_id, ext_map_idx, ext_offset;

	/* extension headers always start from 64bit offsets, so if base header
	   doesn't happen to be 64bit aligned we'll skip some bytes */
	offset = MAIL_INDEX_HEADER_SIZE_ALIGN(map->hdr.base_header_size);
	if (offset >= map->hdr.header_size && map->extension_pool == NULL) {
		/* nothing to do, skip allocations and all */
		return 0;
	}

	old_count = array_count(&index->extensions);
	mail_index_map_init_extbufs(map, old_count + 5);

	ext_id = (uint32_t)-1;
	for (i = 0; i < old_count; i++)
		array_push_back(&map->ext_id_map, &ext_id);

	for (i = 0; offset < map->hdr.header_size; i++) {
		ext_offset = offset;

		if (mail_index_map_ext_get_next(map, &offset,
						&ext_hdr, &name) < 0) {
			mail_index_set_error(index, "Corrupted index file %s: "
				"Header extension #%d (%s) goes outside header",
				index->filepath, i, name);
			return -1;
		}

		if (mail_index_map_ext_hdr_check(&map->hdr, ext_hdr,
						 name, &error) < 0) {
			mail_index_set_error(index, "Corrupted index file %s: "
					     "Broken extension #%d (%s): %s",
					     index->filepath, i, name, error);
			return -1;
		}
		if (mail_index_map_lookup_ext(map, name, &ext_map_idx)) {
			mail_index_set_error(index, "Corrupted index file %s: "
				"Duplicate header extension %s",
				index->filepath, name);
			return -1;
		}

		(void)mail_index_map_register_ext(map, name, ext_offset, ext_hdr);
	}
	return 0;
}

int mail_index_map_parse_keywords(struct mail_index_map *map)
{
	struct mail_index *index = map->index;
	const struct mail_index_ext *ext;
	const struct mail_index_keyword_header *kw_hdr;
	const struct mail_index_keyword_header_rec *kw_rec;
	const char *name;
	unsigned int i, name_area_end_offset, old_count;
	uint32_t idx;

	if (!mail_index_map_lookup_ext(map, MAIL_INDEX_EXT_KEYWORDS, &idx)) {
		if (array_is_created(&map->keyword_idx_map))
			array_clear(&map->keyword_idx_map);
		return 0;
	}
	ext = array_idx(&map->extensions, idx);

	/* Extension header contains:
	   - struct mail_index_keyword_header
	   - struct mail_index_keyword_header_rec * keywords_count
	   - const char names[] * keywords_count

	   The mail_index_keyword_header_rec are rather unnecessary nowadays.
	   They were originally an optimization when dovecot.index header kept
	   changing constantly, but nowadays the changes are usually read from
	   the .log changes, so re-reading dovecot.index header isn't common.
	   In a later version we could even remove it.
	*/
	i_assert(ext->hdr_offset < map->hdr.header_size);
	kw_hdr = MAIL_INDEX_MAP_HDR_OFFSET(map, ext->hdr_offset);
	kw_rec = (const void *)(kw_hdr + 1);
	name = (const char *)(kw_rec + kw_hdr->keywords_count);

	old_count = !array_is_created(&map->keyword_idx_map) ? 0 :
		array_count(&map->keyword_idx_map);

	/* make sure the header is valid */
	if (kw_hdr->keywords_count < old_count) {
		mail_index_set_error(index, "Corrupted index file %s: "
				     "Keywords removed unexpectedly",
				     index->filepath);
		return -1;
	}

	if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) {
		mail_index_set_error(index, "Corrupted index file %s: "
				     "keywords_count larger than header size",
				     index->filepath);
		return -1;
	}

	name_area_end_offset = (const char *)kw_hdr + ext->hdr_size - name;
	for (i = 0; i < kw_hdr->keywords_count; i++) {
		if (kw_rec[i].name_offset > name_area_end_offset) {
			mail_index_set_error(index, "Corrupted index file %s: "
				"name_offset points outside allocated header",
				index->filepath);
			return -1;
		}
	}
	if (name[name_area_end_offset-1] != '\0') {
		mail_index_set_error(index, "Corrupted index file %s: "
				     "Keyword header doesn't end with NUL",
				     index->filepath);
		return -1;
	}

	/* create file -> index mapping */
	if (!array_is_created(&map->keyword_idx_map)) 
		i_array_init(&map->keyword_idx_map, kw_hdr->keywords_count);

	size_t name_offset = 0;
	/* Check that existing headers are still the same. */
	for (i = 0; i < array_count(&map->keyword_idx_map); i++) {
		const char *keyword = name + kw_rec[i].name_offset;
		const unsigned int *old_idx;
		unsigned int kw_idx;

		if (kw_rec[i].name_offset != name_offset) {
			/* this shouldn't happen, but the old code didn't check
			   for this so for safety keep this as a warning. */
			e_warning(index->event,
				  "Corrupted index file %s: "
				  "Mismatching keyword name_offset",
				  index->filepath);
		}
		name_offset += strlen(keyword) + 1;

		old_idx = array_idx(&map->keyword_idx_map, i);
		if (!mail_index_keyword_lookup(index, keyword, &kw_idx) ||
		    kw_idx != *old_idx) {
			mail_index_set_error(index, "Corrupted index file %s: "
					     "Keywords changed unexpectedly",
					     index->filepath);
			return -1;
		}
	}

	/* Register the newly seen keywords */
	i = array_count(&map->keyword_idx_map);
	for (; i < kw_hdr->keywords_count; i++) {
		const char *keyword = name + kw_rec[i].name_offset;
		unsigned int kw_idx;

		if (kw_rec[i].name_offset != name_offset) {
			/* this shouldn't happen, but the old code didn't check
			   for this so for safety keep this as a warning. */
			e_warning(index->event,
				  "Corrupted index file %s: "
				  "Mismatching keyword name_offset",
				  index->filepath);
		}
		name_offset += strlen(keyword) + 1;

		if (*keyword == '\0') {
			mail_index_set_error(index, "Corrupted index file %s: "
				"Empty keyword name in header",
				index->filepath);
			return -1;
		}
		mail_index_keyword_lookup_or_create(index, keyword, &kw_idx);
		array_push_back(&map->keyword_idx_map, &kw_idx);
	}
	return 0;
}

bool mail_index_check_header_compat(struct mail_index *index,
				    const struct mail_index_header *hdr,
				    uoff_t file_size, const char **error_r)
{
        enum mail_index_header_compat_flags compat_flags = 0;

#ifndef WORDS_BIGENDIAN
	compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
#endif

	if (hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
		/* major version change */
		*error_r = t_strdup_printf("Major version changed (%u != %u)",
			hdr->major_version, MAIL_INDEX_MAJOR_VERSION);
		return FALSE;
	}
	if ((hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
		/* we've already complained about it */
		*error_r = "Header's corrupted flag is set";
		return FALSE;
	}

	if (hdr->compat_flags != compat_flags) {
		/* architecture change */
		*error_r = "CPU architecture changed";
		return FALSE;
	}

	if (hdr->base_header_size < MAIL_INDEX_HEADER_MIN_SIZE ||
	    hdr->header_size < hdr->base_header_size) {
		*error_r = t_strdup_printf(
			"Corrupted header sizes (base %u, full %u)",
			hdr->base_header_size, hdr->header_size);
		return FALSE;
	}
	if (hdr->header_size > file_size) {
		*error_r = t_strdup_printf(
			"Header size is larger than file (%u > %"PRIuUOFF_T")",
			hdr->header_size, file_size);
		return FALSE;
	}

	if (hdr->indexid != index->indexid) {
		if (index->indexid != 0) {
			mail_index_set_error(index, "Index file %s: "
					     "indexid changed: %u -> %u",
					     index->filepath, index->indexid,
					     hdr->indexid);
		}
		index->indexid = hdr->indexid;
		mail_transaction_log_indexid_changed(index->log);
	}
	return TRUE;
}

static void mail_index_map_clear_recent_flags(struct mail_index_map *map)
{
	struct mail_index_record *rec;
	uint32_t seq;

	for (seq = 1; seq <= map->hdr.messages_count; seq++) {
		rec = MAIL_INDEX_REC_AT_SEQ(map, seq);
		rec->flags &= ENUM_NEGATE(MAIL_RECENT);
	}
}

int mail_index_map_check_header(struct mail_index_map *map,
				const char **error_r)
{
	struct mail_index *index = map->index;
	const struct mail_index_header *hdr = &map->hdr;

	if (!mail_index_check_header_compat(index, hdr, UOFF_T_MAX, error_r))
		return 0;

	/* following some extra checks that only take a bit of CPU */
	if (hdr->record_size < sizeof(struct mail_index_record)) {
		*error_r = t_strdup_printf(
			"record_size too small (%u < %zu)",
			hdr->record_size, sizeof(struct mail_index_record));
		return -1;
	}

	if (hdr->uid_validity == 0 && hdr->next_uid != 1) {
		*error_r = t_strdup_printf(
			"uidvalidity=0, but next_uid=%u", hdr->next_uid);
		return 0;
	}
	if (hdr->next_uid == 0) {
		*error_r = "next_uid=0";
		return 0;
	}
	if (hdr->messages_count > map->rec_map->records_count) {
		*error_r = t_strdup_printf(
			"messages_count is higher in header than record map (%u > %u)",
			hdr->messages_count, map->rec_map->records_count);
		return 0;
	}

	if (hdr->seen_messages_count > hdr->messages_count) {
		*error_r = t_strdup_printf(
			"seen_messages_count %u > messages_count %u",
			hdr->seen_messages_count, hdr->messages_count);
		return 0;
	}
	if (hdr->deleted_messages_count > hdr->messages_count) {
		*error_r = t_strdup_printf(
			"deleted_messages_count %u > messages_count %u",
			hdr->deleted_messages_count, hdr->messages_count);
		return 0;
	}
	switch (hdr->minor_version) {
	case 0:
		/* upgrade silently from v1.0 */
		map->hdr.unused_old_recent_messages_count = 0;
		if (hdr->first_recent_uid == 0)
			map->hdr.first_recent_uid = 1;
		if (index->need_recreate == NULL)
			index->need_recreate = i_strdup("Upgrading from index version 1.0");
		/* fall through */
	case 1:
		/* pre-v1.1.rc6: make sure the \Recent flags are gone */
		mail_index_map_clear_recent_flags(map);
		map->hdr.minor_version = MAIL_INDEX_MINOR_VERSION;
		/* fall through */
	case 2:
		/* pre-v2.2 (although should have been done in v2.1 already):
		   make sure the old unused fields are cleared */
		map->hdr.unused_old_sync_size_part1 = 0;
		map->hdr.log2_rotate_time = 0;
		map->hdr.last_temp_file_scan = 0;
	}
	if (hdr->first_recent_uid == 0) {
		*error_r = "first_recent_uid=0";
		return 0;
	}
	if (hdr->first_recent_uid > hdr->next_uid) {
		*error_r = t_strdup_printf(
			"first_recent_uid %u > next_uid %u",
			hdr->first_recent_uid, hdr->next_uid);
		return 0;
	}
	if (hdr->first_unseen_uid_lowwater > hdr->next_uid) {
		*error_r = t_strdup_printf(
			"first_unseen_uid_lowwater %u > next_uid %u",
			hdr->first_unseen_uid_lowwater, hdr->next_uid);
		return 0;
	}
	if (hdr->first_deleted_uid_lowwater > hdr->next_uid) {
		*error_r = t_strdup_printf(
			"first_deleted_uid_lowwater %u > next_uid %u",
			hdr->first_deleted_uid_lowwater, hdr->next_uid);
		return 0;
	}

	if (hdr->messages_count > 0) {
		/* last message's UID must be smaller than next_uid.
		   also make sure it's not zero. */
		const struct mail_index_record *rec;

		rec = MAIL_INDEX_REC_AT_SEQ(map, hdr->messages_count);
		if (rec->uid == 0) {
			*error_r = "last message has uid=0";
			return -1;
		}
		if (rec->uid >= hdr->next_uid) {
			*error_r = t_strdup_printf(
				"last message uid %u >= next_uid %u",
				rec->uid, hdr->next_uid);
			return 0;
		}
	}
	return 1;
}