bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen#define CACHE_FIELD_IS_NEWLY_WANTED(cache, field_idx) \
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ((cache)->field_file_map[field_idx] == (uint32_t)-1 && \
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenstatic bool field_has_fixed_size(enum mail_cache_field_type type)
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenstatic bool field_decision_is_valid(enum mail_cache_decision_type type)
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenstatic int field_type_verify(struct mail_cache *cache, unsigned int idx,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen enum mail_cache_field_type type, unsigned int size)
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen const struct mail_cache_field *field = &cache->fields[idx].field;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen "registered field %s type changed", field->name);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen if (field->field_size != size && field_has_fixed_size(type)) {
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen "registered field %s size changed", field->name);
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainenmail_cache_field_update(struct mail_cache *cache,
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainen i_assert(newfield->type < MAIL_CACHE_FIELD_COUNT);
c2d7c8a808e78ed256346fb8529a565dc68e92ccTimo Sirainen /* are we still doing the initial cache field registering for
c2d7c8a808e78ed256346fb8529a565dc68e92ccTimo Sirainen internal fields and for mail_*cache_fields settings? */
c2d7c8a808e78ed256346fb8529a565dc68e92ccTimo Sirainen initial_registering = cache->file_fields_count == 0;
a16d9a651aaa36a308f1aaae87e73e143fdff887Timo Sirainen if ((newfield->decision & MAIL_CACHE_DECISION_FORCED) != 0 ||
c2d7c8a808e78ed256346fb8529a565dc68e92ccTimo Sirainen ((orig->field.decision & MAIL_CACHE_DECISION_FORCED) == 0 &&
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainen if (orig->field.last_used < newfield->last_used) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid mail_cache_register_fields(struct mail_cache *cache,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < fields_count; i++) {
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen if (hash_table_lookup_full(cache->field_name_hash,
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen fields[i].idx = POINTER_CAST_TO(value, unsigned int);
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen /* check if the same header is being registered in the
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen same field array */
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen for (j = 0; j < i; j++) {
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen if (strcasecmp(fields[i].name, fields[j].name) == 0) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* @UNSAFE */
e6d4f540bf5c3b7ef5d6e154b217a2422210048cTimo Sirainen i_realloc_type(cache->field_file_map, uint32_t,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* new index - save it */
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen name = p_strdup(cache->field_pool, fields[i].name);
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainen cache->fields[idx].field.last_used = fields[i].last_used;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen if (!field_has_fixed_size(cache->fields[idx].field.type))
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen cache->fields[idx].field.field_size = UINT_MAX;
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen hash_table_insert(cache->field_name_hash, name,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenmail_cache_register_lookup(struct mail_cache *cache, const char *name)
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen if (hash_table_lookup_full(cache->field_name_hash, name, &key, &value))
e2e105f6182f550efa82f77d2a46b3e8a2d4da10Timo Sirainenmail_cache_register_get_field(struct mail_cache *cache, unsigned int field_idx)
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainenmail_cache_register_get_list(struct mail_cache *cache, pool_t pool,
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen unsigned int *count_r)
16598a1fb870ae40d6177755a4306216e4d6a4cdTimo Sirainen unsigned int i;
32c24eab7111799d880139634144ba8aea5a666eTimo Sirainen p_new(pool, struct mail_cache_field, cache->fields_count);
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainenmail_cache_header_fields_get_offset(struct mail_cache *cache,
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainen const struct mail_cache_header_fields **field_hdr_r)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const struct mail_cache_header_fields *field_hdr;
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen struct mail_cache_header_fields tmp_field_hdr;
05d1312c8456f9d548470bf5662481a4cf812d70Timo Sirainen uint32_t offset = 0, next_offset, field_hdr_size;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* find the latest header */
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen next_offset = cache->last_field_header_offset != 0 ?
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen mail_index_offset_to_uint32(cache->hdr->field_header_offset);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen while (next_offset != 0) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen "next_offset in field header loops");
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen /* In Dovecot v2.2+ we don't try to use any holes,
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen so next_offset must always be larger than current offset.
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen also makes it easier to guarantee there aren't any loops
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen (which we don't bother doing for old files) */
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen if (next_offset < offset && cache->hdr->minor_version != 0) {
26b007545685688e8931dc05ba465df0b00c156aTimo Sirainen "next_offset in field header decreases");
ab1e5b156d1b5480d36ed6e8e06197339d803038Timo Sirainen if (cache->mmap_base != NULL || cache->map_with_read) {
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainen ret = mail_cache_map(cache, offset, sizeof(*field_hdr),
73b251f13abfbd8e06c8c78784eb570d28fe7e40Timo Sirainen "header field next_offset points outside file");
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen /* if we need to follow multiple offsets to get to
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen the last one, it's faster to just pread() the file
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen instead of going through cache */
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen mail_cache_set_syscall_error(cache, "pread()");
73b251f13abfbd8e06c8c78784eb570d28fe7e40Timo Sirainen "header field next_offset points outside file");
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen mail_index_offset_to_uint32(field_hdr->next_offset);
6e235046e1d8e9d89fc948f5c623676c20421a28Timo Sirainen mail_cache_set_corrupted(cache, "missing header fields");
67bbcd664bebce9a507a49c67273be4814d07c97Timo Sirainen if (next_count > cache->index->optimization_set.cache.compress_header_continue_count)
abb83d133dd082527d500916fca66a72fbbbaa8dTimo Sirainen cache->need_compress_file_seq = cache->hdr->file_seq;
05d1312c8456f9d548470bf5662481a4cf812d70Timo Sirainen /* detect corrupted size later */
05d1312c8456f9d548470bf5662481a4cf812d70Timo Sirainen field_hdr_size = I_MAX(field_hdr->size, sizeof(*field_hdr));
131b073bdc3650083b00616dc778dd3017c2bbb5Timo Sirainen /* invalidate the cache fields area to make sure we
131b073bdc3650083b00616dc778dd3017c2bbb5Timo Sirainen get the latest cache decisions/last_used fields */
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen file_cache_invalidate(cache->file_cache, offset,
05d1312c8456f9d548470bf5662481a4cf812d70Timo Sirainen ret = mail_cache_map(cache, offset, field_hdr_size, &data);
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainen "header field size outside file");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenint mail_cache_header_fields_read(struct mail_cache *cache)
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainen const struct mail_cache_header_fields *field_hdr;
82ed69779f49bd71ef1b570ce8aca67d357dbee8Timo Sirainen if (mail_cache_header_fields_get_offset(cache, &offset, &field_hdr) < 0)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* no fields - the file is empty */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* check the fixed size of the header. name[] has to be checked
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen separately */
c5bf07c1339a288701e7566e6f90c4034ee74c24Timo Sirainen if (field_hdr->fields_count > INT_MAX / MAIL_CACHE_FIELD_NAMES(1) ||
c5bf07c1339a288701e7566e6f90c4034ee74c24Timo Sirainen field_hdr->size < MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count)) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen mail_cache_set_corrupted(cache, "invalid field header size");
e6d4f540bf5c3b7ef5d6e154b217a2422210048cTimo Sirainen i_realloc_type(cache->file_field_map, unsigned int,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen last_used = CONST_PTR_OFFSET(field_hdr, MAIL_CACHE_FIELD_LAST_USED());
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_SIZE(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_TYPE(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_DECISION(field_hdr->fields_count));
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen MAIL_CACHE_FIELD_NAMES(field_hdr->fields_count));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen end = CONST_PTR_OFFSET(field_hdr, field_hdr->size);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* clear the old mapping */
6ebcdea168735ee76e32b871c1f50f3526690447Timo Sirainen max_drop_time = cache->index->map->hdr.day_stamp == 0 ? 0 :
67bbcd664bebce9a507a49c67273be4814d07c97Timo Sirainen cache->index->optimization_set.cache.unaccessed_field_drop_secs;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < field_hdr->fields_count; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (p = names; p != end && *p != '\0'; p++) ;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen "field header names corrupted");
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen mail_cache_set_corrupted(cache, "field type corrupted");
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen "field decision type corrupted");
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen /* ignore any forced-flags in the file */
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen if (hash_table_lookup_full(cache->field_name_hash, names,
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen /* already exists, see if decision can be updated */
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen fidx = POINTER_CAST_TO(orig_value, unsigned int);
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen if ((cur_dec & MAIL_CACHE_DECISION_FORCED) != 0) {
0ee986fe3dfc5ce700d232d2f6bc1b47aef1bfe7Timo Sirainen /* Forced decision. If the decision has
0ee986fe3dfc5ce700d232d2f6bc1b47aef1bfe7Timo Sirainen changed, update the fields in the file. */
0ee986fe3dfc5ce700d232d2f6bc1b47aef1bfe7Timo Sirainen if ((cur_dec & ~MAIL_CACHE_DECISION_FORCED) != file_dec)
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen } else if (cache->fields[fidx].decision_dirty) {
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen /* Decisions have recently been updated
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen internally. Don't change them. */
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen /* Use the decision from the cache file. */
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen cache->fields[fidx].field.decision = file_dec;
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen /* field is currently unknown, so just use whatever
8841d0b8b47241d238b5524bded3ee116d10de33Timo Sirainen exists in the file. */
a04b6515c20b431294626400e173d81f3d25889bTimo Sirainen if (cache->field_file_map[fidx] != (uint32_t)-1) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen /* update last_used if it's newer than ours */
7dd73e056c7d9976080393d9cd731f69111818eeTimo Sirainen if ((time_t)last_used[i] > cache->fields[fidx].field.last_used)
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainen cache->fields[fidx].field.last_used = last_used[i];
008a83e9f680f04f69789fb702232416eab2a86cTimo Sirainen if (cache->fields[fidx].field.last_used < max_drop_time &&
9c4c535b86e9473ad97c6e9242ed84f3d9d69d0dTimo Sirainen /* time to drop this field. don't bother dropping
9c4c535b86e9473ad97c6e9242ed84f3d9d69d0dTimo Sirainen fields that have never been used. */
9a583c7a827f7a4d89ee43774f2d51ea6a214543Timo Sirainen cache->need_compress_file_seq = cache->hdr->file_seq;
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainenstatic void copy_to_buf(struct mail_cache *cache, buffer_t *dest, bool add_new,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy the existing fields */
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy newly wanted fields */
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainenstatic void copy_to_buf_byte(struct mail_cache *cache, buffer_t *dest,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy the existing fields */
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
e96fb85799dc95603bb1a6b4d3685df2d042a2f8Timo Sirainen data = CONST_PTR_OFFSET(&cache->fields[field], offset);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* copy newly wanted fields */
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen data = CONST_PTR_OFFSET(&cache->fields[i], offset);
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainenstatic int mail_cache_header_fields_update_locked(struct mail_cache *cache)
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen if (mail_cache_header_fields_read(cache) < 0 ||
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody mail_cache_header_fields_get_offset(cache, &offset, NULL) < 0)
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ret = mail_cache_write(cache, buffer->data, buffer->used,
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen MAIL_CACHE_FIELD_DECISION(cache->file_fields_count);
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen ret = mail_cache_write(cache, buffer->data, buffer->used,
2c20ffcb5bb1ccdfdcd0b0ff0c7296f65b990362Timo Sirainen for (i = 0; i < cache->file_fields_count; i++)
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainenint mail_cache_header_fields_update(struct mail_cache *cache)
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ret = mail_cache_header_fields_update_locked(cache);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ret = mail_cache_header_fields_update_locked(cache);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid mail_cache_header_fields_get(struct mail_cache *cache, buffer_t *dest)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* we have to keep the field order for the existing fields. */
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen offsetof(struct mail_cache_field, field_size),
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* add existing fields' names */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < cache->file_fields_count; i++) {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen /* add newly wanted fields' names */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenint mail_cache_header_fields_get_next_offset(struct mail_cache *cache,
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody if (mail_cache_header_fields_get_offset(cache, offset_r, NULL) < 0)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *offset_r = offsetof(struct mail_cache_header,