summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/index/dbox-common/dbox-storage.c
blob: 3087174b4725520cf40c91a19fb4526c35054ead (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "crc32.h"
#include "path-util.h"
#include "ioloop.h"
#include "fs-api.h"
#include "mkdir-parents.h"
#include "unlink-old-files.h"
#include "mailbox-uidvalidity.h"
#include "mailbox-list-private.h"
#include "index-storage.h"
#include "dbox-storage.h"

#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <utime.h>

void dbox_storage_get_list_settings(const struct mail_namespace *ns ATTR_UNUSED,
				    struct mailbox_list_settings *set)
{
	if (set->layout == NULL)
		set->layout = MAILBOX_LIST_NAME_FS;
	if (set->subscription_fname == NULL)
		set->subscription_fname = DBOX_SUBSCRIPTION_FILE_NAME;
	if (*set->maildir_name == '\0')
		set->maildir_name = DBOX_MAILDIR_NAME;
	if (*set->mailbox_dir_name == '\0')
		set->mailbox_dir_name = DBOX_MAILBOX_DIR_NAME;
}

static bool
dbox_alt_path_has_changed(const char *root_dir, const char *alt_path,
			  const char *alt_path2, const char *alt_symlink_path)
{
	const char *linkpath, *error;

	if (t_readlink(alt_symlink_path, &linkpath, &error) < 0) {
		if (errno == ENOENT)
			return alt_path != NULL;
		i_error("t_readlink(%s) failed: %s", alt_symlink_path, error);
		return FALSE;
	}

	if (alt_path == NULL) {
		i_warning("dbox %s: Original ALT=%s, "
			  "but currently no ALT path set", root_dir, linkpath);
		return TRUE;
	} else if (strcmp(linkpath, alt_path) != 0) {
		if (strcmp(linkpath, alt_path2) == 0) {
			/* FIXME: for backwards compatibility. old versions
			   created the symlink to mailboxes/ directory, which
			   was fine with sdbox, but didn't even exist with
			   mdbox. we'll silently replace the symlink. */
			return TRUE;
		}
		i_warning("dbox %s: Original ALT=%s, "
			  "but currently ALT=%s", root_dir, linkpath, alt_path);
		return TRUE;
	}
	return FALSE;
}

static void dbox_verify_alt_path(struct mailbox_list *list)
{
	const char *root_dir, *alt_symlink_path, *alt_path, *alt_path2;

	root_dir = mailbox_list_get_root_forced(list, MAILBOX_LIST_PATH_TYPE_DIR);
	alt_symlink_path =
		t_strconcat(root_dir, "/"DBOX_ALT_SYMLINK_NAME, NULL);
	(void)mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_ALT_DIR,
					 &alt_path);
	(void)mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX,
					 &alt_path2);
	if (!dbox_alt_path_has_changed(root_dir, alt_path, alt_path2,
				       alt_symlink_path))
		return;

	/* unlink/create the current alt path symlink */
	i_unlink_if_exists(alt_symlink_path);
	if (alt_path != NULL) {
		int ret = symlink(alt_path, alt_symlink_path);
		if (ret < 0 && errno == ENOENT) {
			/* root_dir doesn't exist yet - create it */
			if (mailbox_list_mkdir_root(list, root_dir,
					MAILBOX_LIST_PATH_TYPE_DIR) < 0)
				return;
			ret = symlink(alt_path, alt_symlink_path);
		}
		if (ret < 0 && errno != EEXIST) {
			i_error("symlink(%s, %s) failed: %m",
				alt_path, alt_symlink_path);
		}
	}
}

int dbox_storage_create(struct mail_storage *_storage,
			struct mail_namespace *ns,
			const char **error_r)
{
	struct dbox_storage *storage = DBOX_STORAGE(_storage);
	const struct mail_storage_settings *set = _storage->set;
	const char *error;

	if (*set->mail_attachment_fs != '\0' &&
	    *set->mail_attachment_dir != '\0') {
		const char *name, *args, *dir;

		args = strpbrk(set->mail_attachment_fs, ": ");
		if (args == NULL) {
			name = set->mail_attachment_fs;
			args = "";
		} else {
			name = t_strdup_until(set->mail_attachment_fs, args++);
		}
		if (strcmp(name, "sis-queue") == 0 &&
		    (_storage->class_flags & MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG) != 0) {
			/* FIXME: the deduplication part doesn't work, because
			   sdbox renames the files.. */
			*error_r = "mail_attachment_fs: "
				"sis-queue not currently supported by sdbox";
			return -1;
		}
		dir = mail_user_home_expand(_storage->user,
					    set->mail_attachment_dir);
		storage->attachment_dir = p_strdup(_storage->pool, dir);

		if (mailbox_list_init_fs(ns->list, _storage->event, name, args,
					 storage->attachment_dir,
					 &storage->attachment_fs, &error) < 0) {
			*error_r = t_strdup_printf("mail_attachment_fs: %s",
						   error);
			return -1;
		}
	}

	if (!ns->list->set.alt_dir_nocheck)
		dbox_verify_alt_path(ns->list);
	return 0;
}

void dbox_storage_destroy(struct mail_storage *_storage)
{
	struct dbox_storage *storage = DBOX_STORAGE(_storage);

	fs_deinit(&storage->attachment_fs);
	index_storage_destroy(_storage);
}

uint32_t dbox_get_uidvalidity_next(struct mailbox_list *list)
{
	const char *path;

	path = mailbox_list_get_root_forced(list, MAILBOX_LIST_PATH_TYPE_CONTROL);
	path = t_strconcat(path, "/"DBOX_UIDVALIDITY_FILE_NAME, NULL);
	return mailbox_uidvalidity_next(list, path);
}

void dbox_notify_changes(struct mailbox *box)
{
	const char *dir, *path;

	if (box->notify_callback == NULL)
		mailbox_watch_remove_all(box);
	else {
		if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_INDEX,
					&dir) <= 0)
			return;
		path = t_strdup_printf("%s/"MAIL_INDEX_PREFIX".log", dir);
		mailbox_watch_add(box, path);
	}
}

static time_t cleanup_interval(struct mail_user *user)
{
	const struct mail_storage_settings *set =
		mail_user_set_get_storage_set(user);
	time_t interval = set->mail_temp_scan_interval;

	/* No need for a cryptographic-quality hash here. */
	unsigned int hash = crc32_str(user->username);

	/* spread from 0.00 to to 30.00% more than the base interval */
	unsigned int spread_factor = 100000 + hash % 30001;
	return (interval * spread_factor) / 100000;
}

static bool
dbox_cleanup_temp_files(struct mail_user *user, const char *path,
			time_t last_scan_time, time_t last_change_time)
{
	/* check once in a while if there are temp files to clean up */
	time_t interval = cleanup_interval(user);
	if (interval == 0) {
		/* disabled */
		return FALSE;
	}

	time_t deadline = ioloop_time - interval;
	if (last_scan_time >= deadline) {
		/* not the time to scan it yet */
		return FALSE;
	}

	bool stated = FALSE;
	if (last_change_time == (time_t)-1) {
		/* Don't know the ctime yet - look it up. */
		struct stat st;
		if (stat(path, &st) < 0) {
			if (errno != ENOENT)
				i_error("stat(%s) failed: %m", path);
			return FALSE;
		}
		last_change_time = st.st_ctime;
		stated = TRUE;
	}

	if (last_scan_time > last_change_time + DBOX_TMP_DELETE_SECS) {
		/* there haven't been any changes to this directory
		   since we last checked it. If we did an extra stat(),
		   we need to update the last_scan_time to avoid
		   stat()ing the next time. */
		return stated;
	}

	(void)unlink_old_files(path, DBOX_TEMP_FILE_PREFIX,
			       ioloop_time - DBOX_TMP_DELETE_SECS);
	return TRUE;
}

int dbox_mailbox_check_existence(struct mailbox *box)
{
	const char *index_path, *box_path = mailbox_get_path(box);
	struct stat st;
	int ret = -1;

	if (box->list->set.iter_from_index_dir) {
		/* Just because the index directory exists, it doesn't mean
		   that the mailbox is selectable. Check that by seeing if
		   dovecot.index.log exists. If it doesn't, fallback to
		   checking for the dbox-Mails in the mail root directory.
		   So this also means that if a mailbox is \NoSelect, listing
		   it will always do a stat() for dbox-Mails in the mail root
		   directory. That's not ideal, but this makes the behavior
		   safer and \NoSelect mailboxes are somewhat rare. */
		if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_INDEX,
					&index_path) < 0)
			return -1;
		i_assert(index_path != NULL);
		index_path = t_strconcat(index_path, "/", box->index_prefix,
					 ".log", NULL);
		ret = stat(index_path, &st);
	}
	if (ret < 0) {
		ret = stat(box_path, &st);
	}

	if (ret == 0) {
		return 0;
	} else if (errno == ENOENT || errno == ENAMETOOLONG) {
		mail_storage_set_error(box->storage, MAIL_ERROR_NOTFOUND,
			T_MAIL_ERR_MAILBOX_NOT_FOUND(box->vname));
		return -1;
	} else if (errno == EACCES) {
		mailbox_set_critical(box, "%s",
			mail_error_eacces_msg("stat", box_path));
		return -1;
	} else {
		mailbox_set_critical(box, "stat(%s) failed: %m", box_path);
		return -1;
	}
}

int dbox_mailbox_open(struct mailbox *box)
{
	if (index_storage_mailbox_open(box, FALSE) < 0)
		return -1;
	mail_index_set_fsync_mode(box->index,
				  box->storage->set->parsed_fsync_mode,
				  MAIL_INDEX_FSYNC_MASK_APPENDS |
				  MAIL_INDEX_FSYNC_MASK_EXPUNGES);
	return 0;
}

int dbox_mailbox_list_cleanup(struct mail_user *user, const char *path,
			      time_t last_temp_file_scan)
{
	time_t change_time = -1;

	if (last_temp_file_scan == 0) {
		/* Try to fetch the scan time from the directory's atime
		   if the directory exists. In case, get also the ctime */
		struct stat stats;
		if (stat(path, &stats) == 0) {
			last_temp_file_scan = stats.st_atim.tv_sec;
			change_time = stats.st_ctim.tv_sec;
		} else {
			if (errno != ENOENT)
				e_error(user->event, "stat(%s) failed: %m", path);
			return -1;
		}
	}

	if (dbox_cleanup_temp_files(user, path, last_temp_file_scan, change_time) ||
	    last_temp_file_scan == 0) {
		/* temp files were scanned. update the last scan timestamp. */
		return 1;
	}
	return 0;
}

void dbox_mailbox_close_cleanup(struct mailbox *box)
{
	if (box->view == NULL)
		return;

	const struct mail_index_header *hdr =
		mail_index_get_header(box->view);
	if (dbox_mailbox_list_cleanup(box->storage->user,
				      mailbox_get_path(box),
				      hdr->last_temp_file_scan) > 0)
		index_mailbox_update_last_temp_file_scan(box);
}

void dbox_mailbox_close(struct mailbox *box)
{
	index_storage_mailbox_close(box);
}

static int dir_is_empty(struct mail_storage *storage, const char *path)
{
	DIR *dir;
	struct dirent *d;
	int ret = 1;

	dir = opendir(path);
	if (dir == NULL) {
		if (errno == ENOENT) {
			/* race condition with DELETE/RENAME? */
			return 1;
		}
		mail_storage_set_critical(storage, "opendir(%s) failed: %m",
					  path);
		return -1;
	}
	while ((d = readdir(dir)) != NULL) {
		if (*d->d_name == '.')
			continue;

		ret = 0;
		break;
	}
	if (closedir(dir) < 0) {
		mail_storage_set_critical(storage, "closedir(%s) failed: %m",
					  path);
		ret = -1;
	}
	return ret;
}

int dbox_mailbox_create(struct mailbox *box,
			const struct mailbox_update *update, bool directory)
{
	struct dbox_storage *storage = DBOX_STORAGE(box->storage);
	const char *alt_path;
	struct stat st;
	int ret;

	if ((ret = index_storage_mailbox_create(box, directory)) <= 0)
		return ret;
	if (mailbox_open(box) < 0)
		return -1;
	if (mail_index_get_header(box->view)->uid_validity != 0 &&
	    !box->storage->rebuilding_list_index) {
		mail_storage_set_error(box->storage, MAIL_ERROR_EXISTS,
				       "Mailbox already exists");
		return -1;
	}

	/* if alt path already exists and contains files, rebuild storage so
	   that we don't start overwriting files. */
	ret = mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX, &alt_path);
	if (ret > 0 && stat(alt_path, &st) == 0) {
		ret = dir_is_empty(box->storage, alt_path);
		if (ret < 0)
			return -1;
		if (ret == 0) {
			mailbox_set_critical(box,
				"Existing files in alt path, "
				"rebuilding storage to avoid losing messages");
			storage->v.set_mailbox_corrupted(box);
			return -1;
		}
		/* dir is empty, ignore it */
	}
	if (dbox_mailbox_create_indexes(box, update) < 0)
		return -1;
	return index_mailbox_update_last_temp_file_scan(box);
}

int dbox_mailbox_create_indexes(struct mailbox *box,
				const struct mailbox_update *update)
{
	struct dbox_storage *storage = DBOX_STORAGE(box->storage);
	struct mail_index_sync_ctx *sync_ctx;
	struct mail_index_view *view;
	struct mail_index_transaction *trans;
	int ret;

	/* use syncing as a lock */
	ret = mail_index_sync_begin(box->index, &sync_ctx, &view, &trans, 0);
	if (ret <= 0) {
		i_assert(ret != 0);
		mailbox_set_index_error(box);
		return -1;
	}

	if (mail_index_get_header(view)->uid_validity == 0) {
		if (storage->v.mailbox_create_indexes(box, update, trans) < 0) {
			mail_index_sync_rollback(&sync_ctx);
			return -1;
		}
	}

	return mail_index_sync_commit(&sync_ctx);
}

int dbox_verify_alt_storage(struct mailbox_list *list)
{
	const char *alt_path;
	struct stat st;

	if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_ALT_DIR,
					&alt_path))
		return 0;

	/* make sure alt storage is mounted. if it's not, abort the rebuild. */
	if (stat(alt_path, &st) == 0)
		return 0;
	if (errno != ENOENT) {
		i_error("stat(%s) failed: %m", alt_path);
		return -1;
	}

	/* try to create the alt directory. if it fails, it means alt
	   storage isn't mounted. */
	if (mailbox_list_mkdir_root(list, alt_path,
				    MAILBOX_LIST_PATH_TYPE_ALT_DIR) < 0)
		return -1;
	return 0;
}

bool dbox_header_have_flag(struct mailbox *box, uint32_t ext_id,
			   unsigned int flags_offset, uint8_t flag)
{
	const void *data;
	size_t data_size;
	uint8_t flags = 0;

	mail_index_get_header_ext(box->view, ext_id, &data, &data_size);
	if (flags_offset < data_size)
		flags = *((const uint8_t *)data + flags_offset);
	return (flags & flag) != 0;
}