1 /* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "ioloop.h"
5 #include "hash.h"
6 #include "mail-storage-private.h"
7 #include "mailbox-list-private.h"
8 #include "mailbox-guid-cache.h"
9 
10 struct mailbox_guid_cache_rec {
11 	guid_128_t guid;
12 	const char *vname;
13 };
14 
mailbox_guid_cache_find(struct mailbox_list * list,const guid_128_t guid,const char ** vname_r)15 int mailbox_guid_cache_find(struct mailbox_list *list,
16 			    const guid_128_t guid, const char **vname_r)
17 {
18 	const struct mailbox_guid_cache_rec *rec;
19 	const uint8_t *guid_p = guid;
20 
21 	if (!hash_table_is_created(list->guid_cache) ||
22 	    list->guid_cache_invalidated) {
23 		mailbox_guid_cache_refresh(list);
24 		rec = hash_table_lookup(list->guid_cache, guid_p);
25 	} else {
26 		rec = hash_table_lookup(list->guid_cache, guid_p);
27 		if (rec == NULL && list->guid_cache_updated) {
28 			mailbox_guid_cache_refresh(list);
29 			rec = hash_table_lookup(list->guid_cache, guid_p);
30 		}
31 	}
32 	if (rec == NULL) {
33 		*vname_r = NULL;
34 		return list->guid_cache_errors ? -1 : 0;
35 	}
36 	*vname_r = rec->vname;
37 	return 0;
38 }
39 
mailbox_guid_cache_add_mailbox(struct mailbox_list * list,const struct mailbox_info * info)40 static void mailbox_guid_cache_add_mailbox(struct mailbox_list *list,
41 					   const struct mailbox_info *info)
42 {
43 	struct mailbox *box;
44 	struct mailbox_metadata metadata;
45 	struct mailbox_guid_cache_rec *rec;
46 	uint8_t *guid_p;
47 
48 	box = mailbox_alloc(list, info->vname, 0);
49 	if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID,
50 				 &metadata) < 0) {
51 		e_error(box->event, "Couldn't get mailbox GUID: %s",
52 			mailbox_get_last_internal_error(box, NULL));
53 		list->guid_cache_errors = TRUE;
54 	} else if ((rec = hash_table_lookup(list->guid_cache,
55 			(const uint8_t *)metadata.guid)) != NULL) {
56 		e_warning(list->ns->user->event,
57 			  "Mailbox %s has duplicate GUID with %s: %s",
58 			  info->vname, rec->vname,
59 			  guid_128_to_string(metadata.guid));
60 	} else {
61 		rec = p_new(list->guid_cache_pool,
62 			    struct mailbox_guid_cache_rec, 1);
63 		memcpy(rec->guid, metadata.guid, sizeof(rec->guid));
64 		rec->vname = p_strdup(list->guid_cache_pool, info->vname);
65 		guid_p = rec->guid;
66 		hash_table_insert(list->guid_cache, guid_p, rec);
67 	}
68 	mailbox_free(&box);
69 }
70 
mailbox_guid_cache_refresh(struct mailbox_list * list)71 void mailbox_guid_cache_refresh(struct mailbox_list *list)
72 {
73 	struct mailbox_list_iterate_context *ctx;
74 	const struct mailbox_info *info;
75 
76 	if (!hash_table_is_created(list->guid_cache)) {
77 		list->guid_cache_pool =
78 			pool_alloconly_create("guid cache", 1024*16);
79 		hash_table_create(&list->guid_cache, list->guid_cache_pool, 0,
80 				  guid_128_hash, guid_128_cmp);
81 	} else {
82 		hash_table_clear(list->guid_cache, TRUE);
83 		p_clear(list->guid_cache_pool);
84 	}
85 	list->guid_cache_invalidated = FALSE;
86 	list->guid_cache_updated = FALSE;
87 	list->guid_cache_errors = FALSE;
88 
89 	ctx = mailbox_list_iter_init(list, "*",
90 				     MAILBOX_LIST_ITER_SKIP_ALIASES |
91 				     MAILBOX_LIST_ITER_NO_AUTO_BOXES);
92 	while ((info = mailbox_list_iter_next(ctx)) != NULL) {
93 		if ((info->flags &
94 		     (MAILBOX_NOSELECT | MAILBOX_NONEXISTENT)) != 0)
95 			continue;
96 		T_BEGIN {
97 			mailbox_guid_cache_add_mailbox(list, info);
98 		} T_END;
99 	}
100 	if ((list->ns->prefix_len > 0) && !mail_namespace_prefix_is_inbox(list->ns)) {
101 		/* Also check if namespace prefix is a selectable mailbox
102 		   and add it to cache. Does not need to include INBOX since
103 		   it is added separately by mailbox_list_iter_init above. */
104 		const char *ns_vname = t_strndup(list->ns->prefix,
105 					         list->ns->prefix_len-1);
106 		const struct mailbox_info ns_info = {
107 			.vname = ns_vname,
108 			.ns = list->ns,
109 		};
110 		struct mailbox *box = mailbox_alloc(list, ns_vname, 0);
111 		enum mailbox_existence existence;
112 		if (mailbox_exists(box, FALSE, &existence) == 0 &&
113 		    existence == MAILBOX_EXISTENCE_SELECT)
114 			mailbox_guid_cache_add_mailbox(list, &ns_info);
115 		mailbox_free(&box);
116 	}
117 
118 	if (mailbox_list_iter_deinit(&ctx) < 0)
119 		list->guid_cache_errors = TRUE;
120 }
121