pop3-migration-plugin.c revision 5407a86a03963261bf5ba8793b8c97879ad2e224
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "lib.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "array.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "istream.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "istream-header-filter.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "str.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "sha1.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "message-size.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "message-header-parser.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "mail-cache.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "mail-namespace.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "mail-search-build.h"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#include "index-storage.h"
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch#include "index-mail.h"
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch#include "pop3-migration-plugin.h"
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch#define POP3_MIGRATION_CONTEXT(obj) \
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch MODULE_CONTEXT(obj, pop3_migration_storage_module)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch#define POP3_MIGRATION_MAIL_CONTEXT(obj) \
10962368c30afde135743fd9796122e88a708e87Stephan Bosch MODULE_CONTEXT(obj, pop3_migration_mail_module)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstruct msg_map_common {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* sha1(header) - set only when needed */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned char hdr_sha1[SHA1_RESULTLEN];
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned int hdr_sha1_set:1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstruct pop3_uidl_map {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct msg_map_common common;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch uint32_t pop3_seq;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch uint32_t imap_uid;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch /* UIDL */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const char *pop3_uidl;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* LIST size */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uoff_t size;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstruct imap_msg_map {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct msg_map_common common;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uint32_t uid, pop3_seq;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uoff_t psize;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch const char *pop3_uidl;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstruct pop3_migration_mail_storage {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch union mail_storage_module_context module_ctx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const char *pop3_box_vname;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ARRAY(struct pop3_uidl_map) pop3_uidl_map;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned int all_mailboxes:1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned int pop3_all_hdr_sha1_set:1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned int ignore_missing_uidls:1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned int skip_size_check:1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstruct pop3_migration_mailbox {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch union mailbox_module_context module_ctx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch ARRAY(struct imap_msg_map) imap_msg_map;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned int first_unfound_idx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mail_cache_field cache_field;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned int cache_field_registered:1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch unsigned int uidl_synced:1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch unsigned int uidl_sync_failed:1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch unsigned int uidl_ordered:1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch/* NOTE: these headers must be sorted */
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic const char *hdr_hash_skip_headers[] = {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch "Content-Length",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch "Return-Path", /* Yahoo IMAP has Return-Path, Yahoo POP3 doesn't */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch "Status",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch "X-IMAP",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch "X-IMAPbase",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Keywords",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Message-Flag",
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch "X-Status",
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch "X-UID",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-UIDL",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Yahoo-Newman-Property"
10962368c30afde135743fd9796122e88a708e87Stephan Bosch};
10962368c30afde135743fd9796122e88a708e87Stephan Boschconst char *pop3_migration_plugin_version = DOVECOT_ABI_VERSION;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic MODULE_CONTEXT_DEFINE_INIT(pop3_migration_storage_module,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch &mail_storage_module_register);
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic MODULE_CONTEXT_DEFINE_INIT(pop3_migration_mail_module,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch &mail_module_register);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic int imap_msg_map_uid_cmp(const struct imap_msg_map *map1,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const struct imap_msg_map *map2)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (map1->uid < map2->uid)
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch return -1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch if (map1->uid > map2->uid)
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch return 1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return 0;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int pop3_uidl_map_pop3_seq_cmp(const struct pop3_uidl_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch const struct pop3_uidl_map *map2)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (map1->pop3_seq < map2->pop3_seq)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return -1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (map1->pop3_seq > map2->pop3_seq)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return 1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return 0;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch}
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Boschstatic int pop3_uidl_map_hdr_cmp(const struct pop3_uidl_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch const struct pop3_uidl_map *map2)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return memcmp(map1->common.hdr_sha1, map2->common.hdr_sha1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch sizeof(map1->common.hdr_sha1));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch}
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int imap_msg_map_hdr_cmp(const struct imap_msg_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch const struct imap_msg_map *map2)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return memcmp(map1->common.hdr_sha1, map2->common.hdr_sha1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch sizeof(map1->common.hdr_sha1));
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch}
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstruct pop3_hdr_context {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch bool have_eoh;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch bool stop;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch};
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic bool header_name_is_valid(const char *name)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned int i;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch for (i = 0; name[i] != '\0'; i++) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if ((uint8_t)name[i] <= 0x20 || name[i] >= 0x7f)
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch return FALSE;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return TRUE;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch}
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Boschstatic void
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Boschpop3_header_filter_callback(struct header_filter_istream *input ATTR_UNUSED,
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch struct message_header_line *hdr,
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch bool *matched, struct pop3_hdr_context *ctx)
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch{
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch if (hdr == NULL)
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch return;
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch if (hdr->eoh) {
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch ctx->have_eoh = TRUE;
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch if (ctx->stop) {
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch /* matched is handled differently for eoh by
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch istream-header-filter. a design bug I guess.. */
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch *matched = FALSE;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch }
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch } else {
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch if (strspn(hdr->name, "\r") == hdr->name_len) {
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch /* CR+CR+LF - some servers stop the header processing
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch here while others don't. To make sure they can be
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch matched correctly we want to stop here entirely. */
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch ctx->stop = TRUE;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch }
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch if (ctx->stop)
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch *matched = TRUE;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch else if (!header_name_is_valid(hdr->name)) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch /* Yahoo IMAP drops headers with invalid names, while
10962368c30afde135743fd9796122e88a708e87Stephan Bosch Yahoo POP3 preserves them. Drop them all. */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch *matched = TRUE;
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschint pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uoff_t hdr_size,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned char sha1_r[SHA1_RESULTLEN],
10962368c30afde135743fd9796122e88a708e87Stephan Bosch bool *have_eoh_r)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct istream *input2;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const unsigned char *data;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch size_t i, start, size;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct sha1_ctxt sha1_ctx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct pop3_hdr_context hdr_ctx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch memset(&hdr_ctx, 0, sizeof(hdr_ctx));
10962368c30afde135743fd9796122e88a708e87Stephan Bosch input2 = i_stream_create_limit(input, hdr_size);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch /* hide headers that might change or be different in IMAP vs. POP3 */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch input = i_stream_create_header_filter(input2,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch hdr_hash_skip_headers,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch N_ELEMENTS(hdr_hash_skip_headers),
10962368c30afde135743fd9796122e88a708e87Stephan Bosch pop3_header_filter_callback, &hdr_ctx);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_stream_unref(&input2);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch sha1_init(&sha1_ctx);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch while (i_stream_read_data(input, &data, &size, 0) > 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* - Dovecot IMAP replaces NULs with 0x80 character.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch - Dovecot POP3 with outlook-no-nuls workaround replaces NULs
b9ee73a064b38d8aeec754b964cc34b23487387aTimo Sirainen with 0x80 character.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch - Zimbra replaces 8bit chars with '?' in header fetches,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch but not body fetches.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch - Yahoo replaces 8bit chars with '?' in partial header
10962368c30afde135743fd9796122e88a708e87Stephan Bosch fetches, but not POP3 TOP.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch So we'll just replace all control and 8bit chars with '?',
10962368c30afde135743fd9796122e88a708e87Stephan Bosch which hopefully will satisfy everybody.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch (Keep this code in sync with dsync.)
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch for (i = start = 0; i < size; i++) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if ((data[i] < 0x20 || data[i] >= 0x80) &&
10962368c30afde135743fd9796122e88a708e87Stephan Bosch (data[i] != '\t' && data[i] != '\n')) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch sha1_loop(&sha1_ctx, data + start, i-start);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch sha1_loop(&sha1_ctx, "?", 1);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch start = i+1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch }
b9ee73a064b38d8aeec754b964cc34b23487387aTimo Sirainen }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch sha1_loop(&sha1_ctx, data + start, i-start);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_stream_skip(input, size);
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody }
b9ee73a064b38d8aeec754b964cc34b23487387aTimo Sirainen if (input->stream_errno != 0) {
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_error("pop3_migration: Failed to read header for msg %u: %s",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mail_seq, i_stream_get_error(input));
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_stream_unref(&input);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return -1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch sha1_result(&sha1_ctx, sha1_r);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen i_stream_unref(&input);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch *have_eoh_r = hdr_ctx.have_eoh;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return 0;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic unsigned int get_cache_idx(struct mail *mail)
b9ee73a064b38d8aeec754b964cc34b23487387aTimo Sirainen{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(mail->box);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (mbox->cache_field_registered)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return mbox->cache_field.idx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mbox->cache_field.name = "pop3-migration.hdr";
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mbox->cache_field.type = MAIL_CACHE_FIELD_FIXED_SIZE;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mbox->cache_field.field_size = SHA1_RESULTLEN;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mail_cache_register_fields(mail->box->cache, &mbox->cache_field, 1);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mbox->cache_field_registered = TRUE;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return mbox->cache_field.idx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int
10962368c30afde135743fd9796122e88a708e87Stephan Boschget_hdr_sha1(struct mail *mail, unsigned char sha1_r[SHA1_RESULTLEN])
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct istream *input;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct message_size hdr_size;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch bool have_eoh;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (mail_get_hdr_stream(mail, &hdr_size, &input) < 0) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_error("pop3_migration: Failed to get header for msg %u: %s",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mail->seq, mailbox_get_last_error(mail->box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return -1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch if (pop3_migration_get_hdr_sha1(mail->seq, input,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch hdr_size.physical_size,
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch sha1_r, &have_eoh) < 0)
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch return -1;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch if (have_eoh) {
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch struct index_mail *imail = (struct index_mail *)mail;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch index_mail_cache_add_idx(imail, get_cache_idx(mail),
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch sha1_r, SHA1_RESULTLEN);
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch return 0;
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch }
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* The empty "end of headers" line is missing. Either this means that
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch the headers ended unexpectedly (which is ok) or that the remote
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch server is buggy. Some servers have problems with
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch 1) header line continuations that contain only whitespace and
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch 2) headers that have no ":". The header gets truncated when such
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch line is reached.
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch At least Oracle IMS IMAP FETCH BODY[HEADER] handles 1) by not
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch returning the whitespace line and 2) by returning the line but
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch truncating the rest. POP3 TOP instead returns the entire header.
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch This causes the IMAP and POP3 hashes not to match.
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch If there's LF+CR+CR+LF in the middle of headers, Courier IMAP's
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch FETCH BODY[HEADER] stops after that, but Courier POP3's TOP doesn't.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch So we'll try to avoid this by falling back to full FETCH BODY[]
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch (and/or RETR) and we'll parse the header ourself from it. This
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch should work around any similar bugs in all IMAP/POP3 servers. */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (mail_get_stream(mail, &hdr_size, NULL, &input) < 0) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_error("pop3_migration: Failed to get body for msg %u: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail->seq, mailbox_get_last_error(mail->box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return -1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return pop3_migration_get_hdr_sha1(mail->seq, input,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch hdr_size.physical_size,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch sha1_r, &have_eoh);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic bool
10962368c30afde135743fd9796122e88a708e87Stephan Boschget_cached_hdr_sha1(struct mail *mail, buffer_t *cache_buf,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned char sha1_r[SHA1_RESULTLEN])
10962368c30afde135743fd9796122e88a708e87Stephan Bosch{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct index_mail *imail = (struct index_mail *)mail;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch buffer_set_used_size(cache_buf, 0);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (index_mail_cache_lookup_field(imail, cache_buf,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch get_cache_idx(mail)) > 0 &&
10962368c30afde135743fd9796122e88a708e87Stephan Bosch cache_buf->used == SHA1_RESULTLEN) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch memcpy(sha1_r, cache_buf->data, cache_buf->used);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return TRUE;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return FALSE;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic struct mailbox *pop3_mailbox_alloc(struct mail_storage *storage)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct pop3_migration_mail_storage *mstorage =
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch POP3_MIGRATION_CONTEXT(storage);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail_namespace *ns;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch ns = mail_namespace_find(storage->user->namespaces,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mstorage->pop3_box_vname);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_assert(ns != NULL);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return mailbox_alloc(ns->list, mstorage->pop3_box_vname,
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody MAILBOX_FLAG_READONLY | MAILBOX_FLAG_POP3_SESSION);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch}
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int pop3_map_read(struct mail_storage *storage, struct mailbox *pop3_box)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct pop3_migration_mail_storage *mstorage =
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch POP3_MIGRATION_CONTEXT(storage);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mailbox_transaction_context *t;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail_search_args *search_args;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail_search_context *ctx;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail *mail;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct pop3_uidl_map *map;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch const char *uidl;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch uoff_t size = (uoff_t)-1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch int ret = 0;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (array_is_created(&mstorage->pop3_uidl_map)) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* already read these, just reset the imap_uids */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch array_foreach_modifiable(&mstorage->pop3_uidl_map, map)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch map->imap_uid = 0;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return 0;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_array_init(&mstorage->pop3_uidl_map, 128);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (mailbox_sync(pop3_box, 0) < 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_error("pop3_migration: Couldn't sync mailbox %s: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch pop3_box->vname, mailbox_get_last_error(pop3_box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return -1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch t = mailbox_transaction_begin(pop3_box, 0);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch search_args = mail_search_build_init();
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail_search_build_add_all(search_args);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ctx = mailbox_search_init(t, search_args, NULL,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mstorage->skip_size_check ? 0 :
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch MAIL_FETCH_PHYSICAL_SIZE, NULL);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail_search_args_unref(&search_args);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch while (mailbox_search_next(ctx, &mail)) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* get the size with LIST instead of RETR */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail->lookup_abort = MAIL_LOOKUP_ABORT_READ_MAIL;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (mstorage->skip_size_check)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch else if (mail_get_physical_size(mail, &size) < 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_error("pop3_migration: Failed to get size for msg %u: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail->seq,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mailbox_get_last_error(pop3_box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ret = -1;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch break;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (mail_get_special(mail, MAIL_FETCH_UIDL_BACKEND, &uidl) < 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_error("pop3_migration: Failed to get UIDL for msg %u: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail->seq,
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch mailbox_get_last_error(pop3_box, NULL));
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch ret = -1;
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch break;
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch }
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch if (*uidl == '\0') {
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch i_warning("pop3_migration: UIDL for msg %u is empty",
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch mail->seq);
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch continue;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch }
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map = array_append_space(&mstorage->pop3_uidl_map);
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map->pop3_seq = mail->seq;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map->pop3_uidl = p_strdup(storage->pool, uidl);
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map->size = size;
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch }
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch if (mailbox_search_deinit(&ctx) < 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_error("pop3_migration: Failed to search all POP3 mails: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mailbox_get_last_error(pop3_box, NULL));
10962368c30afde135743fd9796122e88a708e87Stephan Bosch ret = -1;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch }
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody (void)mailbox_transaction_commit(&t);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch return ret;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic void
1faa520084b901b15d83d3d68baaee2535051defStephan Boschpop3_map_read_cached_hdr_hashes(struct mailbox_transaction_context *t,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail_search_args *search_args,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct array *msg_map)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch{
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail_search_context *ctx;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct mail *mail;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct msg_map_common *map;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch buffer_t *cache_buf;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ctx = mailbox_search_init(t, search_args, NULL, 0, NULL);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch cache_buf = buffer_create_dynamic(pool_datastack_create(), SHA1_RESULTLEN);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch while (mailbox_search_next(ctx, &mail)) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch map = array_idx_modifiable_i(msg_map, mail->seq-1);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (get_cached_hdr_sha1(mail, cache_buf, map->hdr_sha1))
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch map->hdr_sha1_set = TRUE;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (mailbox_search_deinit(&ctx) < 0) {
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_warning("pop3_migration: Failed to search all cached POP3 header hashes: %s - ignoring",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mailbox_get_last_error(t->box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch}
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic void map_remove_found_seqs(struct mail_search_arg *search_arg,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct array *msg_map, uint32_t seq1)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const struct msg_map_common *map;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uint32_t seq, count = array_count_i(msg_map);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_assert(search_arg->type == SEARCH_SEQSET);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch for (seq = seq1; seq <= count; seq++) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch map = array_idx_i(msg_map, seq-1);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (map->hdr_sha1_set)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch seq_range_array_remove(&search_arg->value.seqset, seq);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch }
10962368c30afde135743fd9796122e88a708e87Stephan Bosch}
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmodystatic int
10962368c30afde135743fd9796122e88a708e87Stephan Boschmap_read_hdr_hashes(struct mailbox *box, struct array *msg_map, uint32_t seq1)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mailbox_transaction_context *t;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mail_search_args *search_args;
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody struct mail_search_context *ctx;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mail *mail;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct msg_map_common *map;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch int ret = 0;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch t = mailbox_transaction_begin(box, 0);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* get all the cached hashes */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch search_args = mail_search_build_init();
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mail_search_build_add_seqset(search_args, seq1, array_count_i(msg_map));
10962368c30afde135743fd9796122e88a708e87Stephan Bosch pop3_map_read_cached_hdr_hashes(t, search_args, msg_map);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch /* read all the non-cached hashes. doing this in two passes allows
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch us to set wanted_fields=MAIL_FETCH_STREAM_HEADER, which allows
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch prefetching to work without downloading all the headers even
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch for mails that already are cached. */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch map_remove_found_seqs(search_args->args, msg_map, seq1);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch ctx = mailbox_search_init(t, search_args, NULL,
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch MAIL_FETCH_STREAM_HEADER, NULL);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch mail_search_args_unref(&search_args);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch while (mailbox_search_next(ctx, &mail)) {
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch map = array_idx_modifiable_i(msg_map, mail->seq-1);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (get_hdr_sha1(mail, map->hdr_sha1) < 0) {
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch ret = -1;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch break;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch }
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch map->hdr_sha1_set = TRUE;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch }
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (mailbox_search_deinit(&ctx) < 0) {
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_error("pop3_migration: Failed to search all mail headers: %s",
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mailbox_get_last_error(box, NULL));
10962368c30afde135743fd9796122e88a708e87Stephan Bosch ret = -1;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch }
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch (void)mailbox_transaction_commit(&t);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch return ret;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch}
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmodystatic int
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmodypop3_map_read_hdr_hashes(struct mail_storage *storage, struct mailbox *pop3_box,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch unsigned first_seq)
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody{
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct pop3_migration_mail_storage *mstorage =
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody POP3_MIGRATION_CONTEXT(storage);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (mstorage->pop3_all_hdr_sha1_set)
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch return 0;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (mstorage->all_mailboxes) {
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch /* we may be matching against multiple mailboxes.
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch read all the hashes only once. */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch first_seq = 1;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch }
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (map_read_hdr_hashes(pop3_box, &mstorage->pop3_uidl_map.arr,
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch first_seq) < 0)
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch return -1;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (first_seq == 1)
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch mstorage->pop3_all_hdr_sha1_set = TRUE;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch return 0;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch}
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Boschstatic int imap_map_read(struct mailbox *box)
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch{
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch struct pop3_migration_mail_storage *mstorage =
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch POP3_MIGRATION_CONTEXT(box->storage);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mailbox_status status;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mailbox_transaction_context *t;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mail_search_args *search_args;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct mail_search_context *ctx;
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen struct mail *mail;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct imap_msg_map *map;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch uoff_t psize = (uoff_t)-1;
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch int ret = 0;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mailbox_get_open_status(box, STATUS_MESSAGES, &status);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_assert(!array_is_created(&mbox->imap_msg_map));
p_array_init(&mbox->imap_msg_map, box->pool, status.messages);
t = mailbox_transaction_begin(box, 0);
search_args = mail_search_build_init();
mail_search_build_add_all(search_args);
ctx = mailbox_search_init(t, search_args, NULL,
mstorage->skip_size_check ? 0 :
MAIL_FETCH_PHYSICAL_SIZE, NULL);
mail_search_args_unref(&search_args);
while (mailbox_search_next(ctx, &mail)) {
if (mstorage->skip_size_check)
;
else if (mail_get_physical_size(mail, &psize) < 0) {
i_error("pop3_migration: Failed to get psize for imap uid %u: %s",
mail->uid,
mailbox_get_last_error(box, NULL));
ret = -1;
break;
}
map = array_append_space(&mbox->imap_msg_map);
map->uid = mail->uid;
map->psize = psize;
}
if (mailbox_search_deinit(&ctx) < 0) {
i_error("pop3_migration: Failed to search all IMAP mails: %s",
mailbox_get_last_error(box, NULL));
ret = -1;
}
(void)mailbox_transaction_commit(&t);
return ret;
}
static int imap_map_read_hdr_hashes(struct mailbox *box)
{
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
return map_read_hdr_hashes(box, &mbox->imap_msg_map.arr,
mbox->first_unfound_idx+1);
}
static bool pop3_uidl_assign_by_size(struct mailbox *box)
{
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
struct pop3_migration_mail_storage *mstorage =
POP3_MIGRATION_CONTEXT(box->storage);
struct pop3_uidl_map *pop3_map;
struct imap_msg_map *imap_map;
unsigned int i, pop3_count, imap_count, count;
if (mstorage->skip_size_check)
return FALSE;
pop3_map = array_get_modifiable(&mstorage->pop3_uidl_map, &pop3_count);
imap_map = array_get_modifiable(&mbox->imap_msg_map, &imap_count);
count = I_MIN(pop3_count, imap_count);
/* see if we can match the messages using sizes */
for (i = 0; i < count; i++) {
if (pop3_map[i].size != imap_map[i].psize)
break;
if (i+1 < count && pop3_map[i].size == pop3_map[i+1].size) {
/* two messages with same size, don't trust them */
break;
}
pop3_map[i].imap_uid = imap_map[i].uid;
imap_map[i].pop3_uidl = pop3_map[i].pop3_uidl;
imap_map[i].pop3_seq = pop3_map[i].pop3_seq;
}
mbox->first_unfound_idx = i;
if (box->storage->user->mail_debug)
i_debug("pop3_migration: %u/%u mails matched by size", i, count);
return i == count;
}
static int
pop3_uidl_assign_by_hdr_hash(struct mailbox *box, struct mailbox *pop3_box)
{
struct pop3_migration_mail_storage *mstorage =
POP3_MIGRATION_CONTEXT(box->storage);
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
struct pop3_uidl_map *pop3_map;
struct imap_msg_map *imap_map;
unsigned int pop3_idx, imap_idx, pop3_count, imap_count;
unsigned int first_seq, missing_uids_count;
uint32_t first_missing_idx = (uint32_t)-1;
int ret;
first_seq = mbox->first_unfound_idx+1;
if (pop3_map_read_hdr_hashes(box->storage, pop3_box, first_seq) < 0 ||
imap_map_read_hdr_hashes(box) < 0)
return -1;
array_sort(&mstorage->pop3_uidl_map, pop3_uidl_map_hdr_cmp);
array_sort(&mbox->imap_msg_map, imap_msg_map_hdr_cmp);
pop3_map = array_get_modifiable(&mstorage->pop3_uidl_map, &pop3_count);
imap_map = array_get_modifiable(&mbox->imap_msg_map, &imap_count);
pop3_idx = imap_idx = 0;
while (pop3_idx < pop3_count && imap_idx < imap_count) {
if (!pop3_map[pop3_idx].common.hdr_sha1_set ||
pop3_map[pop3_idx].imap_uid != 0) {
pop3_idx++;
continue;
}
if (!imap_map[imap_idx].common.hdr_sha1_set ||
imap_map[imap_idx].pop3_uidl != NULL) {
imap_idx++;
continue;
}
ret = memcmp(pop3_map[pop3_idx].common.hdr_sha1,
imap_map[imap_idx].common.hdr_sha1,
sizeof(pop3_map[pop3_idx].common.hdr_sha1));
if (ret < 0)
pop3_idx++;
else if (ret > 0)
imap_idx++;
else {
pop3_map[pop3_idx].imap_uid = imap_map[imap_idx].uid;
imap_map[imap_idx].pop3_uidl =
pop3_map[pop3_idx].pop3_uidl;
imap_map[imap_idx].pop3_seq =
pop3_map[pop3_idx].pop3_seq;
}
}
missing_uids_count = 0;
for (pop3_idx = 0; pop3_idx < pop3_count; pop3_idx++) {
if (pop3_map[pop3_idx].imap_uid == 0) {
if (first_missing_idx == (uint32_t)-1)
first_missing_idx = pop3_idx;
missing_uids_count++;
}
}
if (missing_uids_count > 0 && !mstorage->all_mailboxes) {
string_t *str = t_str_new(128);
str_printfa(str, "pop3_migration: %u POP3 messages have no "
"matching IMAP messages (first POP3 msg %u UIDL %s)",
missing_uids_count,
pop3_map[first_missing_idx].pop3_seq,
pop3_map[first_missing_idx].pop3_uidl);
if (imap_count + missing_uids_count == pop3_count) {
str_append(str, " - all IMAP messages were found "
"(POP3 contains more than IMAP INBOX - you may want to set pop3_migration_all_mailboxes=yes)");
}
if (!mstorage->ignore_missing_uidls) {
i_error("%s - set pop3_migration_ignore_missing_uidls=yes to continue anyway",
str_c(str));
return -1;
}
i_warning("%s", str_c(str));
} else if (box->storage->user->mail_debug) {
i_debug("pop3_migration: %u mails matched by headers", pop3_count);
}
array_sort(&mstorage->pop3_uidl_map, pop3_uidl_map_pop3_seq_cmp);
array_sort(&mbox->imap_msg_map, imap_msg_map_uid_cmp);
return 0;
}
static int pop3_migration_uidl_sync(struct mailbox *box)
{
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
struct pop3_migration_mail_storage *mstorage =
POP3_MIGRATION_CONTEXT(box->storage);
struct mailbox *pop3_box;
const struct pop3_uidl_map *pop3_map;
unsigned int i, count;
uint32_t prev_uid;
pop3_box = pop3_mailbox_alloc(box->storage);
/* the POP3 server isn't connected to yet. handle all IMAP traffic
first before connecting, so POP3 server won't disconnect us due to
idling. */
if (imap_map_read(box) < 0 ||
pop3_map_read(box->storage, pop3_box) < 0) {
mailbox_free(&pop3_box);
return -1;
}
if (!pop3_uidl_assign_by_size(box)) {
/* everything wasn't assigned, figure out the rest with
header hashes */
if (pop3_uidl_assign_by_hdr_hash(box, pop3_box) < 0) {
mailbox_free(&pop3_box);
return -1;
}
}
/* see if the POP3 UIDL order is the same as IMAP UID order */
mbox->uidl_ordered = TRUE;
pop3_map = array_get(&mstorage->pop3_uidl_map, &count);
prev_uid = 0;
for (i = 0; i < count; i++) {
if (pop3_map[i].imap_uid == 0)
continue;
if (prev_uid > pop3_map[i].imap_uid) {
mbox->uidl_ordered = FALSE;
break;
}
prev_uid = pop3_map[i].imap_uid;
}
mbox->uidl_synced = TRUE;
mailbox_free(&pop3_box);
return 0;
}
static int pop3_migration_uidl_sync_if_needed(struct mailbox *box)
{
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
if (mbox->uidl_synced)
return 0;
if (mbox->uidl_sync_failed ||
pop3_migration_uidl_sync(box) < 0) {
mbox->uidl_sync_failed = TRUE;
mail_storage_set_error(box->storage, MAIL_ERROR_TEMP,
"POP3 UIDLs couldn't be synced");
return -1;
}
return 0;
}
static int
pop3_migration_get_special(struct mail *_mail, enum mail_fetch_field field,
const char **value_r)
{
struct mail_private *mail = (struct mail_private *)_mail;
union mail_module_context *mmail = POP3_MIGRATION_MAIL_CONTEXT(mail);
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(_mail->box);
struct imap_msg_map map_key, *map;
if (field == MAIL_FETCH_UIDL_BACKEND ||
field == MAIL_FETCH_POP3_ORDER) {
if (pop3_migration_uidl_sync_if_needed(_mail->box) < 0)
return -1;
memset(&map_key, 0, sizeof(map_key));
map_key.uid = _mail->uid;
map = array_bsearch(&mbox->imap_msg_map, &map_key,
imap_msg_map_uid_cmp);
if (map != NULL && map->pop3_uidl != NULL) {
if (field == MAIL_FETCH_UIDL_BACKEND)
*value_r = map->pop3_uidl;
else
*value_r = t_strdup_printf("%u", map->pop3_seq);
return 0;
}
/* not found from POP3 server, fallback to default */
}
return mmail->super.get_special(_mail, field, value_r);
}
static void pop3_migration_mail_allocated(struct mail *_mail)
{
struct pop3_migration_mail_storage *mstorage =
POP3_MIGRATION_CONTEXT(_mail->box->storage);
struct mail_private *mail = (struct mail_private *)_mail;
struct mail_vfuncs *v = mail->vlast;
union mail_module_context *mmail;
struct mail_namespace *ns;
if (mstorage == NULL ||
(!mstorage->all_mailboxes && !_mail->box->inbox_user)) {
/* assigns UIDLs only for INBOX */
return;
}
ns = mail_namespace_find(_mail->box->storage->user->namespaces,
mstorage->pop3_box_vname);
if (ns == mailbox_get_namespace(_mail->box)) {
/* we're accessing the pop3-migration namespace itself */
return;
}
mmail = p_new(mail->pool, union mail_module_context, 1);
mmail->super = *v;
mail->vlast = &mmail->super;
v->get_special = pop3_migration_get_special;
MODULE_CONTEXT_SET_SELF(mail, pop3_migration_mail_module, mmail);
}
static struct mail_search_context *
pop3_migration_mailbox_search_init(struct mailbox_transaction_context *t,
struct mail_search_args *args,
const enum mail_sort_type *sort_program,
enum mail_fetch_field wanted_fields,
struct mailbox_header_lookup_ctx *wanted_headers)
{
struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(t->box);
if ((wanted_fields & (MAIL_FETCH_UIDL_BACKEND |
MAIL_FETCH_POP3_ORDER)) != 0) {
/* Start POP3 UIDL syncing before the search, so we'll do it
before we start sending any FETCH BODY[]s to IMAP. It
shouldn't matter much, except this works around a bug in
Yahoo IMAP where it sometimes breaks its state when doing
a FETCH BODY[] followed by FETCH BODY[HEADER].. */
(void)pop3_migration_uidl_sync_if_needed(t->box);
}
return mbox->module_ctx.super.search_init(t, args, sort_program,
wanted_fields, wanted_headers);
}
static void pop3_migration_mailbox_allocated(struct mailbox *box)
{
struct mailbox_vfuncs *v = box->vlast;
struct pop3_migration_mailbox *mbox;
mbox = p_new(box->pool, struct pop3_migration_mailbox, 1);
mbox->module_ctx.super = *v;
box->vlast = &mbox->module_ctx.super;
v->search_init = pop3_migration_mailbox_search_init;
MODULE_CONTEXT_SET(box, pop3_migration_storage_module, mbox);
}
static void pop3_migration_mail_storage_destroy(struct mail_storage *storage)
{
struct pop3_migration_mail_storage *mstorage =
POP3_MIGRATION_CONTEXT(storage);
if (array_is_created(&mstorage->pop3_uidl_map))
array_free(&mstorage->pop3_uidl_map);
mstorage->module_ctx.super.destroy(storage);
}
static void pop3_migration_mail_storage_created(struct mail_storage *storage)
{
struct pop3_migration_mail_storage *mstorage;
struct mail_storage_vfuncs *v = storage->vlast;
const char *pop3_box_vname;
pop3_box_vname = mail_user_plugin_getenv(storage->user,
"pop3_migration_mailbox");
if (pop3_box_vname == NULL) {
if (storage->user->mail_debug)
i_debug("pop3_migration: No pop3_migration_mailbox setting - disabled");
return;
}
mstorage = p_new(storage->pool, struct pop3_migration_mail_storage, 1);
mstorage->module_ctx.super = *v;
storage->vlast = &mstorage->module_ctx.super;
v->destroy = pop3_migration_mail_storage_destroy;
mstorage->pop3_box_vname = p_strdup(storage->pool, pop3_box_vname);
mstorage->all_mailboxes =
mail_user_plugin_getenv(storage->user,
"pop3_migration_all_mailboxes") != NULL;
mstorage->ignore_missing_uidls =
mail_user_plugin_getenv(storage->user,
"pop3_migration_ignore_missing_uidls") != NULL;
mstorage->skip_size_check =
mail_user_plugin_getenv(storage->user,
"pop3_migration_skip_size_check") != NULL;
MODULE_CONTEXT_SET(storage, pop3_migration_storage_module, mstorage);
}
static struct mail_storage_hooks pop3_migration_mail_storage_hooks = {
.mail_allocated = pop3_migration_mail_allocated,
.mailbox_allocated = pop3_migration_mailbox_allocated,
.mail_storage_created = pop3_migration_mail_storage_created
};
void pop3_migration_plugin_init(struct module *module)
{
mail_storage_hooks_add(module, &pop3_migration_mail_storage_hooks);
}
void pop3_migration_plugin_deinit(void)
{
mail_storage_hooks_remove(&pop3_migration_mail_storage_hooks);
}