bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mail_index_fsck_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mail_index_fsck_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *fmt, ...)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_error(index, "Fixed index file %s: %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_fsck_error(index, #field" %u -> %u", \
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainenmail_index_fsck_log_pos(struct mail_index *index, struct mail_index_map *map,
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen mail_transaction_log_get_head(index->log, &file_seq, &file_offset);
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen /* index's log_file_seq is too old. move it to log head. */
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen hdr->log_file_head_offset = hdr->log_file_tail_offset =
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen /* index's log_file_seq matches the current log. make sure the
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen offsets are valid. */
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen else if (hdr->log_file_head_offset < MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE)
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen hdr->log_file_head_offset = MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE;
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen if (hdr->log_file_tail_offset > hdr->log_file_head_offset)
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen hdr->log_file_tail_offset = hdr->log_file_head_offset;
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen else if (hdr->log_file_tail_offset < MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE)
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen hdr->log_file_tail_offset = MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE;
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen /* index's log_file_seq is newer than exists. move it to
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen end of the current log head. */
826f767b6017e4572dcc1dcd4f7573a5df6cd767Timo Sirainen hdr->log_file_head_offset = hdr->log_file_tail_offset =
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen if (hdr->log_file_seq == map->hdr.log_file_seq) {
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen /* don't bother complaining about these if file changed too */
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainenmail_index_fsck_header(struct mail_index *index, struct mail_index_map *map,
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen /* mail_index_map_check_header() has already checked that the index
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen isn't completely broken. */
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen if (hdr->uid_validity == 0 && hdr->next_uid != 1)
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenarray_has_name(const ARRAY_TYPE(const_string) *names, const char *name)
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenstatic unsigned int
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenmail_index_fsck_find_keyword_count(struct mail_index_map *map,
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen unsigned int r, i, j, cur, max = 0, kw_pos, kw_size;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen for (r = 0; r < map->rec_map->records_count; r++) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen if (kw[i] != 0) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen for (j = 0; j < 8; j++) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen rec = CONST_PTR_OFFSET(rec, map->hdr.record_size);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenkeyword_name_is_valid(const char *buffer, unsigned int pos, unsigned int size)
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen if (((unsigned char)buffer[pos] & 0x7f) < 32) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* control characters aren't valid */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainenmail_index_fsck_keywords(struct mail_index *index, struct mail_index_map *map,
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen unsigned int ext_offset, unsigned int *offset_p)
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen const struct mail_index_keyword_header *kw_hdr;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen const struct mail_index_keyword_header_rec *kw_rec;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen struct mail_index_keyword_header_rec new_kw_rec;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen unsigned int i, j, name_pos, name_size, rec_pos, hdr_offset, diff;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen unsigned int changed_count, keywords_count, name_base_pos;
5aeb15e5817fbd4b1d8de540aa7673e3819a8030Timo Sirainen mail_index_map_ext_hdr_offset(sizeof(MAIL_INDEX_EXT_KEYWORDS)-1);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen kw_hdr = CONST_PTR_OFFSET(map->hdr_base, hdr_offset);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen name_buffer = (const char *)(kw_rec + keywords_count);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen name_pos = (size_t)(name_buffer - (const char *)kw_hdr);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* the header is completely broken */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen mail_index_fsck_find_keyword_count(map, ext_hdr);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen mail_index_fsck_error(index, "Assuming keywords_count = %u",
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* create keyword name array. invalid keywords are added as
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen empty strings */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen for (i = 0; i < keywords_count; i++) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen !keyword_name_is_valid(name_buffer, kw_rec[i].name_offset,
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen if (*name != '\0' && array_has_name(&names, name)) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* duplicate */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* give new names to invalid keywords */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen for (i = j = 0; i < keywords_count; i++) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* nothing was broken */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen mail_index_fsck_error(index, "Renamed %u keywords to unknown-*",
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen new_kw_hdr = buffer_append_space_unsafe(dest, sizeof(*new_kw_hdr));
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* add keyword records so we can start appending names directly */
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)buffer_append_space_unsafe(dest, keywords_count * sizeof(*kw_rec));
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* write the actual records and names */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen for (i = 0; i < keywords_count; i++) {
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen new_kw_rec.name_offset = dest->used - name_base_pos;
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen buffer_write(dest, rec_pos, &new_kw_rec, sizeof(new_kw_rec));
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen buffer_append(dest, name_array[i], strlen(name_array[i]) + 1);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* keep the header size at least the same size as before */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen buffer_append_zero(dest, ext_hdr->hdr_size - dest->used);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* need to resize the header */
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen buffer_copy(map->hdr_copy_buf, hdr_offset + diff,
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen i_assert(hdr_offset + dest->used <= map->hdr_copy_buf->used);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen buffer_write(map->hdr_copy_buf, hdr_offset, dest->data, dest->used);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* keywords changed unexpectedly, so all views are broken now */
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainenmail_index_fsck_extensions(struct mail_index *index, struct mail_index_map *map,
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen offset = MAIL_INDEX_HEADER_SIZE_ALIGN(hdr->base_header_size);
1944ee7729e2fabd85773a81ca408acd3e36ab1cTimo Sirainen /* mail_index_map_ext_get_next() uses map->hdr, so make sure
1944ee7729e2fabd85773a81ca408acd3e36ab1cTimo Sirainen it's up-to-date */
1944ee7729e2fabd85773a81ca408acd3e36ab1cTimo Sirainen if (mail_index_map_ext_get_next(map, &next_offset,
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen /* the extension continued outside header, drop it */
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen "Dropped extension #%d (%s) "
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen "with invalid header size",
909a45f03f34dcdf9495b037ab87520152a4bc6bTimo Sirainen buffer_set_used_size(map->hdr_copy_buf, hdr->header_size);
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen if (mail_index_map_ext_hdr_check(hdr, ext_hdr, name,
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen "Dropped broken extension #%d (%s)", i, name);
8ababf3e7b15f793370d1dedf85825d38b42633fTimo Sirainen /* name may change if header buffer is changed */
5aeb15e5817fbd4b1d8de540aa7673e3819a8030Timo Sirainen if (strcmp(name, MAIL_INDEX_EXT_KEYWORDS) == 0) {
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen /* drop the field */
746d35bf3dba3ae5ddbcecb9732f60d5e9de77efTimo Sirainen buffer_set_used_size(map->hdr_copy_buf, hdr->header_size);
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainenmail_index_fsck_records(struct mail_index *index, struct mail_index_map *map,
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen bool logged_unordered_uids = FALSE, logged_zero_uids = FALSE;
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen for (i = 0; i < map->rec_map->records_count; ) {
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen /* log an error once, and skip this record */
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen "Record UIDs have zeroes");
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen "Record UIDs unordered");
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen /* not the fastest way when we're skipping lots of
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen records, but this should happen rarely so don't
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen bother optimizing. */
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainen /* all existing views are broken now */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_fsck_error(index, "next_uid %u -> %u",
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen hdr->first_unseen_uid_lowwater = hdr->next_uid;
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainen hdr->first_deleted_uid_lowwater = hdr->next_uid;
9f57ffa4d421951bbfc7f2de2abcbd8033e6a8c3Timo Sirainenmail_index_fsck_map(struct mail_index *index, struct mail_index_map *map)
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen /* Remember the log head position. If we go back in the index's
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen head offset, ignore errors in the log up to this offset. */
909a45f03f34dcdf9495b037ab87520152a4bc6bTimo Sirainen i_assert(map->hdr_copy_buf->used == map->hdr.header_size);
77f386273491b3a20b49b2a5a9db4b6e360615f9Timo Sirainen i_warning("fscking index file %s", index->filepath);
d67f6ca7e313e9192268f6bf9952594524655dcdTimo Sirainen /* we're trying to open the index files, but there wasn't
3e42e3748e489b62554ae0562b5537c20defc1eeTimo Sirainen any .log file. */
3e42e3748e489b62554ae0562b5537c20defc1eeTimo Sirainen if (mail_transaction_log_create(index->log, FALSE) < 0)
6ded8819b9002150a95a7615e4f64f091c250464Timo Sirainen if (mail_transaction_log_sync_lock(index->log, "fscking",
6ded8819b9002150a95a7615e4f64f091c250464Timo Sirainen mail_transaction_log_sync_unlock(index->log, "fscking");
3b80595fcf2001cf7b2fcc6290823e38f4a142fcTimo Sirainenvoid mail_index_fsck_locked(struct mail_index *index)