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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "llist.h"
#include "str.h"
#include "str-sanitize.h"
#include "imap-util.h"
#include "mail-user.h"
#include "mail-storage-private.h"
#include "notify-plugin.h"
#include "mail-log-plugin.h"
#define MAILBOX_NAME_LOG_LEN 64
#define HEADER_LOG_LEN 80
#define MAIL_LOG_USER_CONTEXT(obj) \
MODULE_CONTEXT_REQUIRE(obj, mail_log_user_module)
enum mail_log_field {
MAIL_LOG_FIELD_UID = 0x01,
MAIL_LOG_FIELD_BOX = 0x02,
MAIL_LOG_FIELD_MSGID = 0x04,
MAIL_LOG_FIELD_PSIZE = 0x08,
MAIL_LOG_FIELD_VSIZE = 0x10,
MAIL_LOG_FIELD_FLAGS = 0x20,
MAIL_LOG_FIELD_FROM = 0x40,
MAIL_LOG_FIELD_SUBJECT = 0x80
};
#define MAIL_LOG_DEFAULT_FIELDS \
(MAIL_LOG_FIELD_UID | MAIL_LOG_FIELD_BOX | \
MAIL_LOG_FIELD_MSGID | MAIL_LOG_FIELD_PSIZE)
enum mail_log_event {
MAIL_LOG_EVENT_DELETE = 0x01,
MAIL_LOG_EVENT_UNDELETE = 0x02,
MAIL_LOG_EVENT_EXPUNGE = 0x04,
MAIL_LOG_EVENT_SAVE = 0x08,
MAIL_LOG_EVENT_COPY = 0x10,
MAIL_LOG_EVENT_MAILBOX_CREATE = 0x20,
MAIL_LOG_EVENT_MAILBOX_DELETE = 0x40,
MAIL_LOG_EVENT_MAILBOX_RENAME = 0x80,
MAIL_LOG_EVENT_FLAG_CHANGE = 0x100
};
#define MAIL_LOG_DEFAULT_EVENTS \
(MAIL_LOG_EVENT_DELETE | MAIL_LOG_EVENT_UNDELETE | \
MAIL_LOG_EVENT_EXPUNGE | MAIL_LOG_EVENT_SAVE | MAIL_LOG_EVENT_COPY | \
MAIL_LOG_EVENT_MAILBOX_DELETE | MAIL_LOG_EVENT_MAILBOX_RENAME)
static const char *field_names[] = {
"uid",
"box",
"msgid",
"size",
"vsize",
"flags",
"from",
"subject",
NULL
};
static const char *event_names[] = {
"delete",
"undelete",
"expunge",
"save",
"copy",
"mailbox_create",
"mailbox_delete",
"mailbox_rename",
"flag_change",
NULL
};
struct mail_log_user {
union mail_user_module_context module_ctx;
enum mail_log_field fields;
enum mail_log_event events;
bool cached_only;
};
struct mail_log_message {
struct mail_log_message *prev, *next;
enum mail_log_event event;
bool ignore;
const char *pretext, *text;
};
struct mail_log_mail_txn_context {
pool_t pool;
struct mail_log_message *messages, *messages_tail;
};
static MODULE_CONTEXT_DEFINE_INIT(mail_log_user_module,
&mail_user_module_register);
static enum mail_log_field mail_log_field_find(const char *name)
{
unsigned int i;
for (i = 0; field_names[i] != NULL; i++) {
if (strcmp(name, field_names[i]) == 0)
return 1 << i;
}
return 0;
}
static enum mail_log_event mail_log_event_find(const char *name)
{
unsigned int i;
if (strcmp(name, "append") == 0) {
/* v1.x backwards compatibility */
name = "save";
}
for (i = 0; event_names[i] != NULL; i++) {
if (strcmp(name, event_names[i]) == 0)
return 1 << i;
}
return 0;
}
static enum mail_log_field mail_log_parse_fields(const char *str)
{
const char *const *tmp;
static enum mail_log_field field, fields = 0;
for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
field = mail_log_field_find(*tmp);
if (field == 0)
i_fatal("Unknown field in mail_log_fields: '%s'", *tmp);
fields |= field;
}
return fields;
}
static enum mail_log_event mail_log_parse_events(const char *str)
{
const char *const *tmp;
static enum mail_log_event event, events = 0;
for (tmp = t_strsplit_spaces(str, ", "); *tmp != NULL; tmp++) {
event = mail_log_event_find(*tmp);
if (event == 0)
i_fatal("Unknown event in mail_log_events: '%s'", *tmp);
events |= event;
}
return events;
}
static void mail_log_mail_user_created(struct mail_user *user)
{
struct mail_log_user *muser;
const char *str;
muser = p_new(user->pool, struct mail_log_user, 1);
MODULE_CONTEXT_SET(user, mail_log_user_module, muser);
str = mail_user_plugin_getenv(user, "mail_log_fields");
muser->fields = str == NULL ? MAIL_LOG_DEFAULT_FIELDS :
mail_log_parse_fields(str);
str = mail_user_plugin_getenv(user, "mail_log_events");
muser->events = str == NULL ? MAIL_LOG_DEFAULT_EVENTS :
mail_log_parse_events(str);
muser->cached_only =
mail_user_plugin_getenv_bool(user, "mail_log_cached_only");
}
static void mail_log_append_mailbox_name(string_t *str, struct mail *mail)
{
const char *mailbox_str;
mailbox_str = mailbox_get_vname(mail->box);
str_printfa(str, "box=%s",
str_sanitize(mailbox_str, MAILBOX_NAME_LOG_LEN));
}
static void
mail_log_append_mail_header(string_t *str, struct mail *mail,
const char *name, const char *header)
{
const char *value;
if (mail_get_first_header_utf8(mail, header, &value) <= 0)
value = "";
str_printfa(str, "%s=%s", name, str_sanitize(value, HEADER_LOG_LEN));
}
static void
mail_log_append_uid(struct mail_log_mail_txn_context *ctx,
struct mail_log_message *msg, string_t *str, uint32_t uid)
{
if (uid != 0)
str_printfa(str, "uid=%u", uid);
else {
/* we don't know the uid yet, assign it later */
str_printfa(str, "uid=");
msg->pretext = p_strdup(ctx->pool, str_c(str));
str_truncate(str, 0);
}
}
static void
mail_log_update_wanted_fields(struct mail *mail, enum mail_log_field fields)
{
enum mail_fetch_field wanted_fields = 0;
struct mailbox_header_lookup_ctx *wanted_headers = NULL;
const char *headers[4];
unsigned int hdr_idx = 0;
if ((fields & MAIL_LOG_FIELD_MSGID) != 0)
headers[hdr_idx++] = "Message-ID";
if ((fields & MAIL_LOG_FIELD_FROM) != 0)
headers[hdr_idx++] = "From";
if ((fields & MAIL_LOG_FIELD_SUBJECT) != 0)
headers[hdr_idx++] = "Subject";
if (hdr_idx > 0) {
i_assert(hdr_idx < N_ELEMENTS(headers));
headers[hdr_idx] = NULL;
wanted_headers = mailbox_header_lookup_init(mail->box, headers);
}
if ((fields & MAIL_LOG_FIELD_PSIZE) != 0)
wanted_fields |= MAIL_FETCH_PHYSICAL_SIZE;
if ((fields & MAIL_LOG_FIELD_VSIZE) != 0)
wanted_fields |= MAIL_FETCH_VIRTUAL_SIZE;
mail_add_temp_wanted_fields(mail, wanted_fields, wanted_headers);
mailbox_header_lookup_unref(&wanted_headers);
}
static void
mail_log_append_mail_message_real(struct mail_log_mail_txn_context *ctx,
struct mail *mail, enum mail_log_event event,
const char *desc)
{
struct mail_log_user *muser =
MAIL_LOG_USER_CONTEXT(mail->box->storage->user);
struct mail_log_message *msg;
string_t *text;
uoff_t size;
msg = p_new(ctx->pool, struct mail_log_message, 1);
/* avoid parsing through the message multiple times */
mail_log_update_wanted_fields(mail, muser->fields);
text = t_str_new(128);
str_append(text, desc);
str_append(text, ": ");
if ((muser->fields & MAIL_LOG_FIELD_BOX) != 0) {
mail_log_append_mailbox_name(text, mail);
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_UID) != 0) {
if (event != MAIL_LOG_EVENT_SAVE &&
event != MAIL_LOG_EVENT_COPY)
mail_log_append_uid(ctx, msg, text, mail->uid);
else {
/* with mbox mail->uid contains the uid, but handle
this consistently with all mailbox formats */
mail_log_append_uid(ctx, msg, text, 0);
}
/* make sure UID is assigned to this mail */
mail->transaction->flags |= MAILBOX_TRANSACTION_FLAG_ASSIGN_UIDS;
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_MSGID) != 0) {
mail_log_append_mail_header(text, mail, "msgid", "Message-ID");
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_PSIZE) != 0) {
if (mail_get_physical_size(mail, &size) == 0)
str_printfa(text, "size=%"PRIuUOFF_T, size);
else
str_printfa(text, "size=error");
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_VSIZE) != 0) {
if (mail_get_virtual_size(mail, &size) == 0)
str_printfa(text, "vsize=%"PRIuUOFF_T, size);
else
str_printfa(text, "vsize=error");
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_FROM) != 0) {
mail_log_append_mail_header(text, mail, "from", "From");
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_SUBJECT) != 0) {
mail_log_append_mail_header(text, mail, "subject", "Subject");
str_append(text, ", ");
}
if ((muser->fields & MAIL_LOG_FIELD_FLAGS) != 0) {
str_printfa(text, "flags=(");
imap_write_flags(text, mail_get_flags(mail),
mail_get_keywords(mail));
str_append(text, "), ");
}
str_truncate(text, str_len(text)-2);
msg->event = event;
msg->text = p_strdup(ctx->pool, str_c(text));
DLLIST2_APPEND(&ctx->messages, &ctx->messages_tail, msg);
}
static void mail_log_add_dummy_msg(struct mail_log_mail_txn_context *ctx,
enum mail_log_event event)
{
struct mail_log_message *msg;
msg = p_new(ctx->pool, struct mail_log_message, 1);
msg->event = event;
msg->ignore = TRUE;
DLLIST2_APPEND(&ctx->messages, &ctx->messages_tail, msg);
}
static void
mail_log_append_mail_message(struct mail_log_mail_txn_context *ctx,
struct mail *mail, enum mail_log_event event,
const char *desc)
{
struct mail_log_user *muser =
MAIL_LOG_USER_CONTEXT(mail->box->storage->user);
if ((muser->events & event) == 0) {
if (event == MAIL_LOG_EVENT_SAVE ||
event == MAIL_LOG_EVENT_COPY)
mail_log_add_dummy_msg(ctx, event);
return;
}
T_BEGIN {
enum mail_lookup_abort orig_lookup_abort = mail->lookup_abort;
if (event != MAIL_LOG_EVENT_SAVE && muser->cached_only)
mail->lookup_abort = MAIL_LOOKUP_ABORT_NOT_IN_CACHE;
mail_log_append_mail_message_real(ctx, mail, event, desc);
mail->lookup_abort = orig_lookup_abort;
} T_END;
}
static void *
mail_log_mail_transaction_begin(struct mailbox_transaction_context *t ATTR_UNUSED)
{
pool_t pool;
struct mail_log_mail_txn_context *ctx;
pool = pool_alloconly_create("mail-log", 2048);
ctx = p_new(pool, struct mail_log_mail_txn_context, 1);
ctx->pool = pool;
return ctx;
}
static void mail_log_mail_save(void *txn, struct mail *mail)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_SAVE, "save");
}
static void mail_log_mail_copy(void *txn, struct mail *src, struct mail *dst)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
struct mail_private *src_pmail = (struct mail_private *)src;
struct mailbox *src_box = src->box;
const char *desc;
if (src_pmail->vmail != NULL) {
/* copying a mail from virtual storage. src points to the
backend mail, but we want to log the virtual mailbox name. */
src_box = src_pmail->vmail->box;
}
desc = t_strdup_printf("copy from %s",
str_sanitize(mailbox_get_vname(src_box),
MAILBOX_NAME_LOG_LEN));
mail_log_append_mail_message(ctx, dst,
MAIL_LOG_EVENT_COPY, desc);
}
static void mail_log_mail_expunge(void *txn, struct mail *mail)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
struct mail_private *p = (struct mail_private*)mail;
mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_EXPUNGE,
p->autoexpunged ? "autoexpunge" : "expunge");
}
static void mail_log_mail_update_flags(void *txn, struct mail *mail,
enum mail_flags old_flags)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
enum mail_flags new_flags = mail_get_flags(mail);
if (((old_flags ^ new_flags) & MAIL_DELETED) == 0) {
mail_log_append_mail_message(ctx, mail,
MAIL_LOG_EVENT_FLAG_CHANGE,
"flag_change");
} else if ((old_flags & MAIL_DELETED) == 0) {
mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_DELETE,
"delete");
} else {
mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_UNDELETE,
"undelete");
}
}
static void
mail_log_mail_update_keywords(void *txn, struct mail *mail,
const char *const *old_keywords ATTR_UNUSED)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
mail_log_append_mail_message(ctx, mail, MAIL_LOG_EVENT_FLAG_CHANGE,
"flag_change");
}
static void mail_log_save(const struct mail_log_message *msg, uint32_t uid)
{
if (msg->ignore) {
/* not logging this save/copy */
} else if (msg->pretext == NULL)
i_info("%s", msg->text);
else if (uid != 0)
i_info("%s%u%s", msg->pretext, uid, msg->text);
else
i_info("%serror%s", msg->pretext, msg->text);
}
static void
mail_log_mail_transaction_commit(void *txn,
struct mail_transaction_commit_changes *changes)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
struct mail_log_message *msg;
struct seq_range_iter iter;
unsigned int n = 0;
uint32_t uid;
seq_range_array_iter_init(&iter, &changes->saved_uids);
for (msg = ctx->messages; msg != NULL; msg = msg->next) {
if (msg->event == MAIL_LOG_EVENT_SAVE ||
msg->event == MAIL_LOG_EVENT_COPY) {
if (!seq_range_array_iter_nth(&iter, n++, &uid))
uid = 0;
mail_log_save(msg, uid);
} else {
i_assert(msg->pretext == NULL);
i_info("%s", msg->text);
}
}
i_assert(!seq_range_array_iter_nth(&iter, n, &uid));
pool_unref(&ctx->pool);
}
static void mail_log_mail_transaction_rollback(void *txn)
{
struct mail_log_mail_txn_context *ctx =
(struct mail_log_mail_txn_context *)txn;
pool_unref(&ctx->pool);
}
static void
mail_log_mailbox_create(struct mailbox *box)
{
struct mail_log_user *muser = MAIL_LOG_USER_CONTEXT(box->storage->user);
if ((muser->events & MAIL_LOG_EVENT_MAILBOX_CREATE) == 0)
return;
i_info("Mailbox created: %s",
str_sanitize(mailbox_get_vname(box), MAILBOX_NAME_LOG_LEN));
}
static void
mail_log_mailbox_delete_commit(void *txn ATTR_UNUSED, struct mailbox *box)
{
struct mail_log_user *muser = MAIL_LOG_USER_CONTEXT(box->storage->user);
if ((muser->events & MAIL_LOG_EVENT_MAILBOX_DELETE) == 0)
return;
i_info("Mailbox deleted: %s",
str_sanitize(mailbox_get_vname(box), MAILBOX_NAME_LOG_LEN));
}
static void
mail_log_mailbox_rename(struct mailbox *src, struct mailbox *dest)
{
struct mail_log_user *muser = MAIL_LOG_USER_CONTEXT(src->storage->user);
if ((muser->events & MAIL_LOG_EVENT_MAILBOX_RENAME) == 0)
return;
i_info("Mailbox renamed: %s -> %s",
str_sanitize(mailbox_get_vname(src), MAILBOX_NAME_LOG_LEN),
str_sanitize(mailbox_get_vname(dest), MAILBOX_NAME_LOG_LEN));
}
static const struct notify_vfuncs mail_log_vfuncs = {
.mail_transaction_begin = mail_log_mail_transaction_begin,
.mail_save = mail_log_mail_save,
.mail_copy = mail_log_mail_copy,
.mail_expunge = mail_log_mail_expunge,
.mail_update_flags = mail_log_mail_update_flags,
.mail_update_keywords = mail_log_mail_update_keywords,
.mail_transaction_commit = mail_log_mail_transaction_commit,
.mail_transaction_rollback = mail_log_mail_transaction_rollback,
.mailbox_create = mail_log_mailbox_create,
.mailbox_delete_commit = mail_log_mailbox_delete_commit,
.mailbox_rename = mail_log_mailbox_rename
};
static struct notify_context *mail_log_ctx;
static struct mail_storage_hooks mail_log_mail_storage_hooks = {
.mail_user_created = mail_log_mail_user_created
};
void mail_log_plugin_init(struct module *module)
{
mail_log_ctx = notify_register(&mail_log_vfuncs);
mail_storage_hooks_add(module, &mail_log_mail_storage_hooks);
}
void mail_log_plugin_deinit(void)
{
mail_storage_hooks_remove(&mail_log_mail_storage_hooks);
notify_unregister(mail_log_ctx);
}
const char *mail_log_plugin_dependencies[] = { "notify", NULL };
|