bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainenextern struct mail_storage dbox_storage, sdbox_storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenextern struct dbox_storage_vfuncs sdbox_dbox_storage_vfuncs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct mail_storage *sdbox_storage_alloc(void)
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen pool = pool_alloconly_create("sdbox storage", 512+256);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen storage = p_new(pool, struct sdbox_storage, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen storage->storage.v = sdbox_dbox_storage_vfuncs;
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainenstatic int sdbox_storage_create(struct mail_storage *_storage,
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen const char **error_r)
804fa3f03bd9170272168a5ad214053bbe3160c7Josef 'Jeff' Sipek struct dbox_storage *storage = DBOX_STORAGE(_storage);
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen if (dbox_storage_create(_storage, ns, error_r) < 0)
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen props = fs_get_properties(storage->attachment_fs);
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen "Backend doesn't support renaming";
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainenstatic const char *
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainensdbox_storage_find_root_dir(const struct mail_namespace *ns)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox: access(%s, rwx): failed: %m", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainenstatic bool sdbox_storage_autodetect(const struct mail_namespace *ns,
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen /* NOTE: this check works for mdbox as well. we'll rely on the
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen autodetect ordering to catch mdbox before we get here. */
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen path = t_strconcat(root_dir, "/"DBOX_MAILBOX_DIR_NAME, NULL);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox autodetect: stat(%s) failed: %m", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox autodetect: %s not a directory", path);
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainenstatic struct mailbox *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainensdbox_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* dbox can't work without index files */
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen pool = pool_alloconly_create("sdbox mailbox", 1024*3);
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ibox->index_flags |= MAIL_INDEX_OPEN_FLAG_KEEP_BACKUPS |
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_read_header(struct sdbox_mailbox *mbox,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen struct sdbox_index_header *hdr, bool log_error,
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mail_index_get_header_ext(view, mbox->hdr_ext_id,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (data_size < SDBOX_INDEX_HEADER_MIN_SIZE &&
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "sdbox: Invalid dbox header size");
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen memcpy(hdr, data, I_MIN(data_size, sizeof(*hdr)));
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* data is valid. remember it in case mailbox
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen is being reset */
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainenstatic void sdbox_update_header(struct sdbox_mailbox *mbox,
1127f3e1ad7135b6ee5d5e13f8fd1e72f85a76f8Timo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (update != NULL && !guid_128_is_empty(update->mailbox_guid)) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen memcpy(new_hdr.mailbox_guid, update->mailbox_guid,
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen } else if (guid_128_is_empty(new_hdr.mailbox_guid)) {
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen mail_index_ext_resize_hdr(trans, mbox->hdr_ext_id,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (memcmp(&hdr, &new_hdr, sizeof(hdr)) != 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_update_header_ext(trans, mbox->hdr_ext_id, 0,
7e1b549a1d841e59faad16430cbd1d56317db8afTimo Sirainen memcpy(mbox->mailbox_guid, new_hdr.mailbox_guid,
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainenint sdbox_mailbox_create_indexes(struct mailbox *box,
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen struct mail_index_transaction *new_trans = NULL;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen new_trans = mail_index_transaction_begin(box->view, 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && update->uid_validity != 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* set uidvalidity */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uid_validity = dbox_get_uidvalidity_next(box->list);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen offsetof(struct mail_index_header, uid_validity),
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && hdr->next_uid < update->min_next_uid) {
ee9254cc7875519a9c71cc58a40610e6f320e907Timo Sirainen if (update != NULL && update->min_first_recent_uid != 0 &&
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen hdr->first_recent_uid < update->min_first_recent_uid) {
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen uint32_t first_recent_uid = update->min_first_recent_uid;
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen offsetof(struct mail_index_header, first_recent_uid),
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen &first_recent_uid, sizeof(first_recent_uid), FALSE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && update->min_highest_modseq != 0 &&
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen /* initialize pop3-uidl header when creating mailbox
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen (not on mailbox_update()) */
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (mail_index_transaction_commit(&new_trans) < 0) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic const char *
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainensdbox_get_attachment_path_suffix(struct dbox_file *_file)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct sdbox_file *file = (struct sdbox_file *)_file;
0a7b04ec6441fdcf083392888b2e30844fc3e86dTimo Sirainenvoid sdbox_set_mailbox_corrupted(struct mailbox *box)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0 ||
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mbox->corrupted_rebuild_count = hdr.rebuild_count;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainenstatic void sdbox_set_file_corrupted(struct dbox_file *_file)
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen struct sdbox_file *file = (struct sdbox_file *)_file;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen sdbox_set_mailbox_corrupted(&file->mbox->box);
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainenstatic int sdbox_mailbox_alloc_index(struct sdbox_mailbox *mbox)
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen if (index_storage_mailbox_alloc_index(&mbox->box) < 0)
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mail_index_ext_register(mbox->box.index, "dbox-hdr",
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen sizeof(struct sdbox_index_header), 0, 0);
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* set the initialization data in case the mailbox is created */
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mail_index_set_ext_init_data(mbox->box.index, mbox->hdr_ext_id,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic int sdbox_mailbox_open(struct mailbox *box)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
7b15788793354ca0fd4fdb4dda8e426ca1e9e9d7Timo Sirainen if (dbox_mailbox_check_existence(box, &path_ctime) < 0)
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen /* wait for mailbox creation to initialize the index */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* get/generate mailbox guid */
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) {
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* looks like the mailbox is corrupted */
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen (void)sdbox_sync(mbox, SDBOX_SYNC_FLAG_FORCE);
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* regenerate it */
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen if (sdbox_mailbox_create_indexes(box, NULL, NULL) < 0 ||
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainenstatic void sdbox_mailbox_close(struct mailbox *box)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen const struct mailbox_update *update, bool directory)
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (dbox_mailbox_create(box, update, directory) < 0)
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (directory || !guid_128_is_empty(mbox->mailbox_guid))
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen /* another process just created the mailbox. read the mailbox_guid. */
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "sdbox: Failed to read newly created dbox header");
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen i_assert(!guid_128_is_empty(mbox->mailbox_guid));
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainensdbox_mailbox_get_metadata(struct mailbox *box,
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
206ed2f6fa3a6fb291498627b2da626581c07a18Timo Sirainen if (index_mailbox_get_metadata(box, items, metadata_r) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainendbox_mailbox_update(struct mailbox *box, const struct mailbox_update *update)
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen if (sdbox_mailbox_create_indexes(box, update, NULL) < 0)
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen return index_storage_mailbox_update_common(box, update);
8b31f966d9688e07672ef1958dcbdb7686523c04Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG |
d46a1e3f999dda802dc5137e883adcd7a6629cd3Timo Sirainen MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_SAVE_GUIDS |
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG,