pop3-migration-plugin.c revision 5407a86a03963261bf5ba8793b8c97879ad2e224
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch MODULE_CONTEXT(obj, pop3_migration_storage_module)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch MODULE_CONTEXT(obj, pop3_migration_mail_module)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* sha1(header) - set only when needed */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* LIST size */
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 "X-IMAPbase",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Keywords",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Message-Flag",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch "X-Yahoo-Newman-Property"
10962368c30afde135743fd9796122e88a708e87Stephan Boschconst char *pop3_migration_plugin_version = DOVECOT_ABI_VERSION;
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic MODULE_CONTEXT_DEFINE_INIT(pop3_migration_storage_module,
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic MODULE_CONTEXT_DEFINE_INIT(pop3_migration_mail_module,
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic int imap_msg_map_uid_cmp(const struct imap_msg_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int pop3_uidl_map_pop3_seq_cmp(const struct pop3_uidl_map *map1,
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Boschstatic int pop3_uidl_map_hdr_cmp(const struct pop3_uidl_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return memcmp(map1->common.hdr_sha1, map2->common.hdr_sha1,
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int imap_msg_map_hdr_cmp(const struct imap_msg_map *map1,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return memcmp(map1->common.hdr_sha1, map2->common.hdr_sha1,
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic bool header_name_is_valid(const char *name)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch unsigned int i;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if ((uint8_t)name[i] <= 0x20 || name[i] >= 0x7f)
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Boschpop3_header_filter_callback(struct header_filter_istream *input ATTR_UNUSED,
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch /* matched is handled differently for eoh by
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch istream-header-filter. a design bug I guess.. */
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. */
10962368c30afde135743fd9796122e88a708e87Stephan Bosch /* Yahoo IMAP drops headers with invalid names, while
10962368c30afde135743fd9796122e88a708e87Stephan Bosch Yahoo POP3 preserves them. Drop them all. */
10962368c30afde135743fd9796122e88a708e87Stephan Boschint pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch const unsigned char *data;
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 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 So we'll just replace all control and 8bit chars with '?',
10962368c30afde135743fd9796122e88a708e87Stephan Bosch which hopefully will satisfy everybody.
10962368c30afde135743fd9796122e88a708e87Stephan Bosch (Keep this code in sync with dsync.)
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmody i_error("pop3_migration: Failed to read header for msg %u: %s",
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic unsigned int get_cache_idx(struct mail *mail)
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(mail->box);
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 Boschget_hdr_sha1(struct mail *mail, unsigned char sha1_r[SHA1_RESULTLEN])
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));
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch if (pop3_migration_get_hdr_sha1(mail->seq, input,
f74dbd3ff682fea040f60383e001620d1f1b09d3Stephan Bosch struct index_mail *imail = (struct index_mail *)mail;
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch index_mail_cache_add_idx(imail, get_cache_idx(mail),
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
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 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.
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.
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 pop3_migration_get_hdr_sha1(mail->seq, input,
10962368c30afde135743fd9796122e88a708e87Stephan Boschget_cached_hdr_sha1(struct mail *mail, buffer_t *cache_buf,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct index_mail *imail = (struct index_mail *)mail;
10962368c30afde135743fd9796122e88a708e87Stephan Bosch if (index_mail_cache_lookup_field(imail, cache_buf,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch memcpy(sha1_r, cache_buf->data, cache_buf->used);
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic struct mailbox *pop3_mailbox_alloc(struct mail_storage *storage)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct pop3_migration_mail_storage *mstorage =
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch ns = mail_namespace_find(storage->user->namespaces,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch return mailbox_alloc(ns->list, mstorage->pop3_box_vname,
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmody MAILBOX_FLAG_READONLY | MAILBOX_FLAG_POP3_SESSION);
1faa520084b901b15d83d3d68baaee2535051defStephan Boschstatic int pop3_map_read(struct mail_storage *storage, struct mailbox *pop3_box)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch struct pop3_migration_mail_storage *mstorage =
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 i_error("pop3_migration: Couldn't sync mailbox %s: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch pop3_box->vname, mailbox_get_last_error(pop3_box, NULL));
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch ctx = mailbox_search_init(t, search_args, NULL,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* get the size with LIST instead of RETR */
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch mail->lookup_abort = MAIL_LOOKUP_ABORT_READ_MAIL;
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 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",
d577bb9027e4ceb19ada88d6884265efa5e16b15Stephan Bosch i_warning("pop3_migration: UIDL for msg %u is empty",
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map = array_append_space(&mstorage->pop3_uidl_map);
6e62aa36a3190ef7193bd86158a4245da49132f0Stephan Bosch map->pop3_uidl = p_strdup(storage->pool, uidl);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_error("pop3_migration: Failed to search all POP3 mails: %s",
1faa520084b901b15d83d3d68baaee2535051defStephan Boschpop3_map_read_cached_hdr_hashes(struct mailbox_transaction_context *t,
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 map = array_idx_modifiable_i(msg_map, mail->seq-1);
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch if (get_cached_hdr_sha1(mail, cache_buf, map->hdr_sha1))
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch i_warning("pop3_migration: Failed to search all cached POP3 header hashes: %s - ignoring",
10962368c30afde135743fd9796122e88a708e87Stephan Boschstatic void map_remove_found_seqs(struct mail_search_arg *search_arg,
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch seq_range_array_remove(&search_arg->value.seqset, seq);
10962368c30afde135743fd9796122e88a708e87Stephan Boschmap_read_hdr_hashes(struct mailbox *box, struct array *msg_map, uint32_t seq1)
1faa520084b901b15d83d3d68baaee2535051defStephan Bosch /* get all the cached hashes */
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 map = array_idx_modifiable_i(msg_map, mail->seq-1);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_error("pop3_migration: Failed to search all mail headers: %s",
5968fa8151eecd191b1973b44dd8bec9b75810a6Phil Carmodypop3_map_read_hdr_hashes(struct mail_storage *storage, struct mailbox *pop3_box,
10962368c30afde135743fd9796122e88a708e87Stephan Bosch struct pop3_migration_mail_storage *mstorage =
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch /* we may be matching against multiple mailboxes.
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch read all the hashes only once. */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (map_read_hdr_hashes(pop3_box, &mstorage->pop3_uidl_map.arr,
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch struct pop3_migration_mailbox *mbox = POP3_MIGRATION_CONTEXT(box);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch struct pop3_migration_mail_storage *mstorage =
10962368c30afde135743fd9796122e88a708e87Stephan Bosch mailbox_get_open_status(box, STATUS_MESSAGES, &status);
10962368c30afde135743fd9796122e88a708e87Stephan Bosch i_assert(!array_is_created(&mbox->imap_msg_map));
(void)mailbox_transaction_commit(&t);
return ret;
return FALSE;
for (i = 0; i < count; i++) {
return i == count;
int ret;
pop3_idx++;
imap_idx++;
if (ret < 0)
pop3_idx++;
else if (ret > 0)
imap_idx++;
missing_uids_count = 0;
unsigned int i, count;
prev_uid = 0;
for (i = 0; i < count; i++) {
const char **value_r)
static struct mail_search_context *
MAIL_FETCH_POP3_ORDER)) != 0) {
const char *pop3_box_vname;
void pop3_migration_plugin_deinit(void)