~ubuntu-dev/ubuntu/lucid/dovecot/lucid-201002101918

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "imap-match.h"
#include "mailbox-list-private.h"
#include "index-storage.h"
#include "shared-storage.h"

struct shared_mailbox_list_iterate_context {
	struct mailbox_list_iterate_context ctx;
	struct mail_namespace *cur_ns;
	struct imap_match_glob *glob;
	struct mailbox_info info;
};

extern struct mailbox_list shared_mailbox_list;

static struct mailbox_list *shared_list_alloc(void)
{
	struct mailbox_list *list;
	pool_t pool;

	pool = pool_alloconly_create("shared list", 1024);
	list = p_new(pool, struct mailbox_list, 1);
	*list = shared_mailbox_list;
	list->pool = pool;
	return list;
}

static void shared_list_deinit(struct mailbox_list *list)
{
	pool_unref(&list->pool);
}

static void shared_list_copy_error(struct mailbox_list *shared_list,
				   struct mail_namespace *backend_ns)
{
	const char *str;
	enum mail_error error;

	str = mailbox_list_get_last_error(backend_ns->list, &error);
	mailbox_list_set_error(shared_list, error, str);
}

static int
shared_get_storage(struct mailbox_list *list, const char **name,
		   struct mail_storage **storage_r)
{
	struct mail_namespace *ns;

	if (shared_storage_get_namespace(list->ns->storage, name, &ns) < 0)
		return -1;
	*storage_r = ns->storage;
	return 0;
}

static bool
shared_is_valid_pattern(struct mailbox_list *list, const char *pattern)
{
	struct mail_namespace *ns;

	if (shared_storage_get_namespace(list->ns->storage, &pattern, &ns) < 0)
		return FALSE;
	return mailbox_list_is_valid_pattern(ns->list, pattern);
}

static bool
shared_is_valid_existing_name(struct mailbox_list *list, const char *name)
{
	struct mail_namespace *ns;

	if (shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0)
		return FALSE;
	return mailbox_list_is_valid_existing_name(ns->list, name);
}

static bool
shared_is_valid_create_name(struct mailbox_list *list, const char *name)
{
	struct mail_namespace *ns;

	if (shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0)
		return FALSE;
	return mailbox_list_is_valid_create_name(ns->list, name);
}

static const char *
shared_list_get_path(struct mailbox_list *list, const char *name,
		     enum mailbox_list_path_type type)
{
	struct mail_namespace *ns;

	if (list->ns->storage == NULL || name == NULL ||
	    shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0) {
		switch (type) {
		case MAILBOX_LIST_PATH_TYPE_DIR:
		case MAILBOX_LIST_PATH_TYPE_MAILBOX:
		case MAILBOX_LIST_PATH_TYPE_CONTROL:
			break;
		case MAILBOX_LIST_PATH_TYPE_INDEX:
			/* we can safely say we don't use indexes */
			return "";
		}
		/* we don't have a directory we can use. */
		return NULL;
	}
	return mailbox_list_get_path(ns->list, name, type);
}

static int
shared_list_get_mailbox_name_status(struct mailbox_list *list, const char *name,
				    enum mailbox_name_status *status_r)
{
	struct mail_namespace *ns;
	int ret;

	if (shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0)
		return -1;
	ret = mailbox_list_get_mailbox_name_status(ns->list, name, status_r);
	if (ret < 0)
		shared_list_copy_error(list, ns);
	return ret;
}

static const char *
shared_list_get_temp_prefix(struct mailbox_list *list, bool global ATTR_UNUSED)
{
	i_panic("shared mailbox list: Can't return a temp prefix for '%s'",
		list->ns->prefix);
	return NULL;
}

static const char *
shared_list_join_refpattern(struct mailbox_list *list,
			    const char *ref, const char *pattern)
{
	struct mail_namespace *ns;
	const char *ns_ref, *prefix = list->ns->prefix;
	unsigned int prefix_len = strlen(prefix);

	if (*ref != '\0' && strncmp(ref, prefix, prefix_len) == 0)
		ns_ref = ref + prefix_len;
	else
		ns_ref = NULL;

	if (ns_ref != NULL &&
	    shared_storage_get_namespace(list->ns->storage,
					 &ns_ref, &ns) == 0)
		return mailbox_list_join_refpattern(ns->list, ns_ref, pattern);

	/* fallback to default behavior */
	if (*ref != '\0')
		pattern = t_strconcat(ref, pattern, NULL);
	return pattern;
}

static struct mailbox_list_iterate_context *
shared_list_iter_init(struct mailbox_list *list, const char *const *patterns,
		      enum mailbox_list_iter_flags flags)
{
	struct shared_mailbox_list_iterate_context *ctx;

	ctx = i_new(struct shared_mailbox_list_iterate_context, 1);
	ctx->ctx.list = list;
	ctx->ctx.flags = flags;
	ctx->cur_ns = list->ns->user->namespaces;
	ctx->info.ns = list->ns;
	ctx->info.flags = MAILBOX_NONEXISTENT;
	ctx->glob = imap_match_init_multiple(default_pool, patterns,
					     FALSE, list->ns->sep);
	return &ctx->ctx;
}

static const struct mailbox_info *
shared_list_iter_next(struct mailbox_list_iterate_context *_ctx)
{
	struct shared_mailbox_list_iterate_context *ctx =
		(struct shared_mailbox_list_iterate_context *)_ctx;
	struct mail_namespace *ns = ctx->cur_ns;

	for (; ns != NULL; ns = ns->next) {
		if (ns->type != NAMESPACE_SHARED ||
		    (ns->flags & NAMESPACE_FLAG_AUTOCREATED) == 0)
			continue;
		if ((ns->flags & (NAMESPACE_FLAG_LIST_PREFIX |
				  NAMESPACE_FLAG_LIST_CHILDREN)) == 0)
			continue;

		if (ns->prefix_len < ctx->info.ns->prefix_len ||
		    strncmp(ns->prefix, ctx->info.ns->prefix,
			    ctx->info.ns->prefix_len) != 0)
			continue;

		/* visible and listable namespace under ourself, see if the
		   prefix matches without the trailing separator */
		i_assert(ns->prefix_len > 0);
		ctx->info.name = t_strndup(ns->prefix, ns->prefix_len - 1);
		if ((_ctx->flags & MAILBOX_LIST_ITER_VIRTUAL_NAMES) == 0)
			ctx->info.name += ctx->info.ns->prefix_len;
		if (imap_match(ctx->glob, ctx->info.name) == IMAP_MATCH_YES) {
			ctx->cur_ns = ns->next;
			return &ctx->info;
		}
	}

	ctx->cur_ns = NULL;
	return NULL;
}

static int shared_list_iter_deinit(struct mailbox_list_iterate_context *_ctx)
{
	struct shared_mailbox_list_iterate_context *ctx =
		(struct shared_mailbox_list_iterate_context *)_ctx;

	imap_match_deinit(&ctx->glob);
	i_free(ctx);
	return 0;
}

static int shared_list_set_subscribed(struct mailbox_list *list,
				      const char *name, bool set)
{
	struct mail_namespace *ns;
	int ret;

	if (shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0)
		return -1;
	ret = mailbox_list_set_subscribed(ns->list, name, set);
	if (ret < 0)
		shared_list_copy_error(list, ns);
	return ret;
}

static int
shared_list_delete_mailbox(struct mailbox_list *list, const char *name)
{
	struct mail_namespace *ns;
	int ret;

	if (shared_storage_get_namespace(list->ns->storage, &name, &ns) < 0)
		return -1;
	ret = mailbox_list_delete_mailbox(ns->list, name);
	if (ret < 0)
		shared_list_copy_error(list, ns);
	return ret;
}

static int shared_list_rename_get_ns(struct mailbox_list *list,
				     const char **oldname, const char **newname,
				     struct mail_namespace **ns_r)
{
	struct mail_namespace *old_ns, *new_ns;

	if (shared_storage_get_namespace(list->ns->storage,
					 oldname, &old_ns) < 0 ||
	    shared_storage_get_namespace(list->ns->storage,
					 newname, &new_ns) < 0)
		return -1;
	if (old_ns != new_ns) {
		mailbox_list_set_error(list, MAIL_ERROR_NOTPOSSIBLE,
			"Can't rename mailboxes across storages");
		return -1;
	}
	*ns_r = old_ns;
	return 0;
}

static int shared_list_rename_mailbox(struct mailbox_list *list,
				      const char *oldname, const char *newname)
{
	struct mail_namespace *ns;
	int ret;

	if (shared_list_rename_get_ns(list, &oldname, &newname, &ns) < 0)
		return -1;
	ret = mailbox_list_rename_mailbox(ns->list, oldname, newname);
	if (ret < 0)
		shared_list_copy_error(list, ns);
	return ret;
}

static int
shared_list_rename_mailbox_pre(struct mailbox_list *list,
			       const char *oldname, const char *newname)
{
	struct mail_namespace *ns;
	int ret;

	if (shared_list_rename_get_ns(list, &oldname, &newname, &ns) < 0)
		return -1;
	ret = ns->list->v.rename_mailbox_pre(ns->list, oldname, newname);
	if (ret < 0)
		shared_list_copy_error(list, ns);
	return ret;
}

struct mailbox_list shared_mailbox_list = {
	MEMBER(name) "shared",
	MEMBER(hierarchy_sep) '/',
	MEMBER(props) 0,
	MEMBER(mailbox_name_max_length) PATH_MAX,

	{
		shared_list_alloc,
		shared_list_deinit,
		shared_get_storage,
		shared_is_valid_pattern,
		shared_is_valid_existing_name,
		shared_is_valid_create_name,
		shared_list_get_path,
		shared_list_get_mailbox_name_status,
		shared_list_get_temp_prefix,
		shared_list_join_refpattern,
		shared_list_iter_init,
		shared_list_iter_next,
		shared_list_iter_deinit,
		NULL,
		shared_list_set_subscribed,
		shared_list_delete_mailbox,
		shared_list_rename_mailbox,
		shared_list_rename_mailbox_pre
	}
};