Cross Reference: /dovecot/src/lib-lda/mail-deliver.c
mail-deliver.c revision 2584e86cc2d8c31ba30a4109cf4ba09d1e37e28a
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
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "lib.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "ioloop.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "array.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "str.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "str-sanitize.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "var-expand.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "message-address.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "imap-utf7.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "lda-settings.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "mail-storage.h"
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "mail-namespace.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "duplicate.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen#include "mail-deliver.h"
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainendeliver_mail_func_t *deliver_mail = NULL;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainenconst char *mail_deliver_get_address(struct mail_deliver_context *ctx,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const char *header)
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen{
2766f1de8141c09767a959d2d2c3065c5a300bf0Timo Sirainen struct message_address *addr;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const char *str;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen if (mail_get_first_header(ctx->src_mail, header, &str) <= 0)
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen return NULL;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen addr = message_address_parse(pool_datastack_create(),
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen (const unsigned char *)str,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen strlen(str), 1, FALSE);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen return addr == NULL || addr->mailbox == NULL || addr->domain == NULL ||
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen *addr->mailbox == '\0' || *addr->domain == '\0' ?
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen NULL : t_strconcat(addr->mailbox, "@", addr->domain, NULL);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen}
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstatic const struct var_expand_table *
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenget_log_var_expand_table(struct mail_deliver_context *ctx, const char *message)
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen{
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen static struct var_expand_table static_tab[] = {
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen { '$', NULL, NULL },
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen { 'm', NULL, "msgid" },
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen { 's', NULL, "subject" },
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen { 'f', NULL, "from" },
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen { '\0', NULL, NULL }
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen };
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen struct var_expand_table *tab;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen unsigned int i;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen tab = t_malloc(sizeof(static_tab));
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen tab[0].value = message;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen (void)mail_get_first_header(ctx->src_mail, "Message-ID", &tab[1].value);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen (void)mail_get_first_header_utf8(ctx->src_mail, "Subject", &tab[2].value);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen tab[3].value = mail_deliver_get_address(ctx, "From");
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen for (i = 1; tab[i].key != '\0'; i++)
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen tab[i].value = str_sanitize(tab[i].value, 80);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen return tab;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen}
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenvoid mail_deliver_log(struct mail_deliver_context *ctx, const char *fmt, ...)
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen{
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen va_list args;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen string_t *str;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen const char *msg;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen va_start(args, fmt);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen msg = t_strdup_vprintf(fmt, args);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen str = t_str_new(256);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen if (ctx->session_id != NULL)
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen str_printfa(str, "%s: ", ctx->session_id);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen var_expand(str, ctx->set->deliver_log_format,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen get_log_var_expand_table(ctx, msg));
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen i_info("%s", str_c(str));
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen va_end(args);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen}
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstatic struct mailbox *
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenmailbox_open_or_create_synced(struct mail_deliver_context *ctx,
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen const char *name, struct mail_namespace **ns_r,
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen const char **error_r)
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen{
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct mail_namespace *ns;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen struct mail_storage *storage;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen struct mailbox *box;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen enum mail_error error;
enum mailbox_flags flags =
MAILBOX_FLAG_KEEP_RECENT | MAILBOX_FLAG_SAVEONLY |
MAILBOX_FLAG_POST_SESSION;
*error_r = NULL;
if (strcasecmp(name, "INBOX") == 0) {
/* deliveries to INBOX must always succeed,
regardless of ACLs */
flags |= MAILBOX_FLAG_IGNORE_ACLS;
}
*ns_r = ns = mail_namespace_find(ctx->dest_user->namespaces, &name);
if (*ns_r == NULL)
return NULL;
if (*name == '\0') {
/* delivering to a namespace prefix means we actually want to
deliver to the INBOX instead */
return NULL;
}
box = mailbox_alloc(ns->list, name, NULL, flags);
if (mailbox_open(box) == 0)
return box;
storage = mailbox_get_storage(box);
*error_r = mail_storage_get_last_error(storage, &error);
if (!ctx->set->lda_mailbox_autocreate || error != MAIL_ERROR_NOTFOUND) {
mailbox_close(&box);
return NULL;
}
/* try creating it. */
if (mailbox_create(box, NULL, FALSE) < 0) {
*error_r = mail_storage_get_last_error(storage, &error);
mailbox_close(&box);
return NULL;
}
if (ctx->set->lda_mailbox_autosubscribe) {
/* (try to) subscribe to it */
(void)mailbox_list_set_subscribed(ns->list, name, TRUE);
}
/* and try opening again */
if (mailbox_sync(box, 0) < 0) {
*error_r = mail_storage_get_last_error(storage, &error);
mailbox_close(&box);
return NULL;
}
return box;
}
static const char *mailbox_name_to_mutf7(const char *mailbox_utf8)
{
string_t *str = t_str_new(128);
if (imap_utf8_to_utf7(mailbox_utf8, str) < 0)
return mailbox_utf8;
else
return str_c(str);
}
int mail_deliver_save(struct mail_deliver_context *ctx, const char *mailbox,
enum mail_flags flags, const char *const *keywords,
struct mail_storage **storage_r)
{
struct mail_namespace *ns;
struct mailbox *box;
enum mailbox_transaction_flags trans_flags;
struct mailbox_transaction_context *t;
struct mail_save_context *save_ctx;
struct mail_keywords *kw;
enum mail_error error;
const char *mailbox_name, *errstr;
struct mail_transaction_commit_changes changes;
const struct seq_range *range;
bool default_save;
int ret = 0;
default_save = strcmp(mailbox, ctx->dest_mailbox_name) == 0;
if (default_save)
ctx->tried_default_save = TRUE;
mailbox_name = str_sanitize(mailbox, 80);
mailbox = mailbox_name_to_mutf7(mailbox);
box = mailbox_open_or_create_synced(ctx, mailbox, &ns, &errstr);
if (box == NULL) {
if (ns == NULL) {
mail_deliver_log(ctx,
"save failed to %s: Unknown namespace",
mailbox_name);
return -1;
}
if (default_save && strcmp(ns->prefix, mailbox) == 0) {
/* silently store to the INBOX instead */
return -1;
}
mail_deliver_log(ctx, "save failed to %s: %s",
mailbox_name, errstr);
return -1;
}
*storage_r = mailbox_get_storage(box);
trans_flags = MAILBOX_TRANSACTION_FLAG_EXTERNAL;
if (ctx->save_dest_mail)
trans_flags |= MAILBOX_TRANSACTION_FLAG_ASSIGN_UIDS;
t = mailbox_transaction_begin(box, trans_flags);
kw = str_array_length(keywords) == 0 ? NULL :
mailbox_keywords_create_valid(box, keywords);
save_ctx = mailbox_save_alloc(t);
mailbox_save_set_flags(save_ctx, flags, kw);
if (mailbox_copy(&save_ctx, ctx->src_mail) < 0)
ret = -1;
if (kw != NULL)
mailbox_keywords_unref(box, &kw);
if (ret < 0)
mailbox_transaction_rollback(&t);
else
ret = mailbox_transaction_commit_get_changes(&t, &changes);
if (ret == 0) {
ctx->saved_mail = TRUE;
mail_deliver_log(ctx, "saved mail to %s", mailbox_name);
if (ctx->save_dest_mail && mailbox_sync(box, 0) == 0) {
range = array_idx(&changes.saved_uids, 0);
i_assert(range[0].seq1 == range[0].seq2);
t = mailbox_transaction_begin(box, 0);
ctx->dest_mail = mail_alloc(t, MAIL_FETCH_STREAM_BODY,
NULL);
if (mail_set_uid(ctx->dest_mail, range[0].seq1) < 0) {
mail_free(&ctx->dest_mail);
mailbox_transaction_rollback(&t);
}
}
pool_unref(&changes.pool);
} else {
mail_deliver_log(ctx, "save failed to %s: %s", mailbox_name,
mail_storage_get_last_error(*storage_r, &error));
}
if (ctx->dest_mail == NULL)
mailbox_close(&box);
return ret;
}
const char *mail_deliver_get_return_address(struct mail_deliver_context *ctx)
{
if (ctx->src_envelope_sender != NULL)
return ctx->src_envelope_sender;
return mail_deliver_get_address(ctx, "Return-Path");
}
const char *mail_deliver_get_new_message_id(struct mail_deliver_context *ctx)
{
static int count = 0;
return t_strdup_printf("<dovecot-%s-%s-%d@%s>",
dec2str(ioloop_timeval.tv_sec),
dec2str(ioloop_timeval.tv_usec),
count++, ctx->set->hostname);
}
int mail_deliver(struct mail_deliver_context *ctx,
struct mail_storage **storage_r)
{
int ret;
*storage_r = NULL;
if (deliver_mail == NULL)
ret = -1;
else {
ctx->dup_ctx = duplicate_init(ctx->dest_user);
if (deliver_mail(ctx, storage_r) <= 0) {
/* if message was saved, don't bounce it even though
the script failed later. */
ret = ctx->saved_mail ? 0 : -1;
} else {
/* success. message may or may not have been saved. */
ret = 0;
}
duplicate_deinit(&ctx->dup_ctx);
}
if (ret < 0 && !ctx->tried_default_save) {
/* plugins didn't handle this. save into the default mailbox. */
ret = mail_deliver_save(ctx, ctx->dest_mailbox_name, 0, NULL,
storage_r);
}
if (ret < 0 && strcasecmp(ctx->dest_mailbox_name, "INBOX") != 0) {
/* still didn't work. try once more to save it
to INBOX. */
ret = mail_deliver_save(ctx, "INBOX", 0, NULL, storage_r);
}
return ret;
}