bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen/* note that everything here depends on uidlist file being locked the whole
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen time. that's why we don't have any locking of our own, or that we do things
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen that would be racy otherwise. */
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen/* how many seconds to wait before overriding dovecot-keywords.lock */
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen HASH_TABLE(char *, void *) hash; /* name -> idx+1 */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int chridx_to_idx[MAILDIR_MAX_KEYWORDS];
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstruct maildir_keywords *maildir_keywords_init(struct maildir_mailbox *mbox)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mk = maildir_keywords_init_readonly(&mbox->box);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenmaildir_keywords_init_readonly(struct mailbox *box)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_CONTROL, &dir) <= 0)
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen mk->path = i_strconcat(dir, "/" MAILDIR_KEYWORDS_NAME, NULL);
0449e50c99f3fab4485f9b983e3936aaa003c79aTimo Sirainen mk->pool = pool_alloconly_create("maildir keywords", 512);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen i_array_init(&mk->list, MAILDIR_MAX_KEYWORDS);
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen hash_table_create(&mk->hash, mk->pool, 0, strcase_hash, strcasecmp);
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen mk->dotlock_settings.stale_timeout = KEYWORDS_LOCK_STALE_TIMEOUT;
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainenvoid maildir_keywords_deinit(struct maildir_keywords **_mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void maildir_keywords_clear(struct maildir_keywords *mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic int maildir_keywords_sync(struct maildir_keywords *mk)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen /* Remember that we rely on uidlist file locking in here. That's why
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen we rely on stat()'s timestamp and don't bother handling ESTALE
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen /* file is updated only by replacing it, no need to flush
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen attribute cache */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* hasn't changed */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen while ((line = i_stream_read_next_line(input)) != NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* note that when converting .customflags file this
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen case happens in the first line. */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* shouldn't happen */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* save it */
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen hash_table_insert(mk->hash, new_name, POINTER_CAST(idx + 1));
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenmaildir_keywords_lookup(struct maildir_keywords *mk, const char *name,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen unsigned int *chridx_r)
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen *chridx_r = POINTER_CAST_TO(value, unsigned int)-1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_create(struct maildir_keywords *mk, const char *name,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int chridx)
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen hash_table_insert(mk->hash, new_name, POINTER_CAST(chridx + 1));
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenmaildir_keywords_lookup_or_create(struct maildir_keywords *mk, const char *name,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen unsigned int *chridx_r)
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen if ((ret = maildir_keywords_lookup(mk, name, chridx_r)) != 0)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* see if we are full */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen for (i = 0; i < count; i++) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (i == count && count >= MAILDIR_MAX_KEYWORDS)
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen if (!maildir_uidlist_is_locked(mk->mbox->uidlist))
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic const char *
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_idx(struct maildir_keywords *mk, unsigned int idx)
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic int maildir_keywords_write_fd(struct maildir_keywords *mk,
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen const struct mailbox_permissions *perm = mailbox_get_permissions(box);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen for (i = 0; i < count; i++) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (write_full(fd, str_data(str), str_len(str)) < 0) {
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen if (fchown(fd, (uid_t)-1, perm->file_create_gid) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box, "%s",
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen /* mtime must grow every time */
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen mk->synced_mtime = ioloop_time <= mk->synced_mtime ?
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic int maildir_keywords_commit(struct maildir_keywords *mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen lock_path = t_strconcat(mk->path, ".lock", NULL);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen perm = mailbox_get_permissions(&mk->mbox->box);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen for (i = 0;; i++) {
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen /* we could just create the temp file directly, but doing it
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen this ways avoids potential problems with overwriting
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen contents in malicious symlinks */
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen old_mask = umask(0777 & ~perm->file_create_mode);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen fd = file_dotlock_open(&mk->dotlock_settings, mk->path,
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen if (errno != ENOENT || i == MAILDIR_DELETE_RETRY_COUNT) {
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen "file_dotlock_open(%s) failed: %m", mk->path);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen /* the control dir doesn't exist. create it unless the whole
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen mailbox was just deleted. */
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (maildir_keywords_write_fd(mk, lock_path, fd) < 0) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen "file_dotlock_replace(%s) failed: %m", mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_sync_init(struct maildir_keywords *mk,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx = i_new(struct maildir_keywords_sync_ctx, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx->keywords = mail_index_get_keywords(index);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen i_array_init(&ctx->idx_to_chr, MAILDIR_MAX_KEYWORDS);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainenmaildir_keywords_sync_init_readonly(struct maildir_keywords *mk,
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainenvoid maildir_keywords_sync_deinit(struct maildir_keywords_sync_ctx **_ctx)
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen struct maildir_keywords_sync_ctx *ctx = *_ctx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenunsigned int maildir_keywords_char_idx(struct maildir_keywords_sync_ctx *ctx,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* lookup / create */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* name is lost. just generate one ourself. */
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen while (maildir_keywords_lookup(ctx->mk, name, &idx) > 0) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* don't create a duplicate name.
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keep changing the name until it doesn't exist */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen maildir_keywords_create(ctx->mk, name, chridx);
6825360d446542046757b06064282301c4c6b27cTimo Sirainen mail_index_keyword_lookup_or_create(ctx->index, name, &idx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenchar maildir_keywords_idx_char(struct maildir_keywords_sync_ctx *ctx,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int idx)
139143f1b798472438b813343a48601f1c564060Sergey Kitov chr_p = array_idx_get_space(&ctx->idx_to_chr, idx);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen maildir_keywords_lookup_or_create(ctx->mk, *name_p, &chridx) :