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
|
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "safe-mkstemp.h"
#include "fs-api.h"
#include "istream.h"
#include "ostream.h"
#include "base64.h"
#include "hash-format.h"
#include "str.h"
#include "message-parser.h"
#include "rfc822-parser.h"
#include "fs-api.h"
#include "istream-fs-file.h"
#include "istream-attachment-connector.h"
#include "istream-attachment-extractor.h"
#include "mail-user.h"
#include "index-mail.h"
#include "index-attachment.h"
enum mail_attachment_decode_option {
MAIL_ATTACHMENT_DECODE_OPTION_NONE = '-',
MAIL_ATTACHMENT_DECODE_OPTION_BASE64 = 'B',
MAIL_ATTACHMENT_DECODE_OPTION_CRLF = 'C'
};
struct mail_save_attachment {
pool_t pool;
struct fs *fs;
struct istream *input;
struct fs_file *cur_file;
ARRAY_TYPE(mail_attachment_extref) extrefs;
};
static const char *index_attachment_dir_get(struct mail_storage *storage)
{
return mail_user_home_expand(storage->user,
storage->set->mail_attachment_dir);
}
static bool index_attachment_want(const struct istream_attachment_header *hdr,
void *context)
{
struct mail_save_context *ctx = context;
struct mail_attachment_part apart;
i_zero(&apart);
apart.part = hdr->part;
apart.content_type = hdr->content_type;
apart.content_disposition = hdr->content_disposition;
if (ctx->part_is_attachment != NULL)
return ctx->part_is_attachment(ctx, &apart);
/* don't treat text/ parts as attachments */
return hdr->content_type != NULL &&
strncasecmp(hdr->content_type, "text/", 5) != 0;
}
static int index_attachment_open_temp_fd(void *context)
{
struct mail_save_context *ctx = context;
struct mail_storage *storage = ctx->transaction->box->storage;
string_t *temp_path;
int fd;
temp_path = t_str_new(256);
mail_user_set_get_temp_prefix(temp_path, storage->user->set);
fd = safe_mkstemp_hostpid(temp_path, 0600, (uid_t)-1, (gid_t)-1);
if (fd == -1) {
mailbox_set_critical(ctx->transaction->box,
"safe_mkstemp(%s) failed: %m", str_c(temp_path));
return -1;
}
if (unlink(str_c(temp_path)) < 0) {
mailbox_set_critical(ctx->transaction->box,
"unlink(%s) failed: %m", str_c(temp_path));
i_close_fd(&fd);
return -1;
}
return fd;
}
static int
index_attachment_open_ostream(struct istream_attachment_info *info,
struct ostream **output_r,
const char **error_r ATTR_UNUSED, void *context)
{
struct mail_save_context *ctx = context;
struct mail_save_attachment *attach = ctx->data.attach;
struct mail_storage *storage = ctx->transaction->box->storage;
struct mail_attachment_extref *extref;
enum fs_open_flags flags = 0;
const char *attachment_dir, *path, *digest = info->hash;
guid_128_t guid_128;
i_assert(attach->cur_file == NULL);
if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER)
flags |= FS_OPEN_FLAG_FSYNC;
if (strlen(digest) < 4) {
/* make sure we can access first 4 bytes without accessing
out of bounds memory */
digest = t_strconcat(digest, "\0\0\0\0", NULL);
}
guid_128_generate(guid_128);
attachment_dir = index_attachment_dir_get(storage);
path = t_strdup_printf("%s/%c%c/%c%c/%s-%s", attachment_dir,
digest[0], digest[1],
digest[2], digest[3], digest,
guid_128_to_string(guid_128));
attach->cur_file = fs_file_init(attach->fs, path,
FS_OPEN_MODE_REPLACE | flags);
extref = array_append_space(&attach->extrefs);
extref->start_offset = info->start_offset;
extref->size = info->encoded_size;
extref->path = p_strdup(attach->pool,
path + strlen(attachment_dir) + 1);
extref->base64_blocks_per_line = info->base64_blocks_per_line;
extref->base64_have_crlf = info->base64_have_crlf;
*output_r = fs_write_stream(attach->cur_file);
return 0;
}
static int
index_attachment_close_ostream(struct ostream *output, bool success,
const char **error, void *context)
{
struct mail_save_context *ctx = context;
struct mail_save_attachment *attach = ctx->data.attach;
int ret = success ? 0 : -1;
i_assert(attach->cur_file != NULL);
if (ret < 0)
fs_write_stream_abort_error(attach->cur_file, &output, "%s", *error);
else if (fs_write_stream_finish(attach->cur_file, &output) < 0) {
*error = t_strdup_printf("Couldn't create attachment %s: %s",
fs_file_path(attach->cur_file),
fs_file_last_error(attach->cur_file));
ret = -1;
}
fs_file_deinit(&attach->cur_file);
if (ret < 0) {
array_pop_back(&attach->extrefs);
}
return ret;
}
void index_attachment_save_begin(struct mail_save_context *ctx,
struct fs *fs, struct istream *input)
{
struct mail_storage *storage = ctx->transaction->box->storage;
struct mail_save_attachment *attach;
struct istream_attachment_settings set;
const char *error;
pool_t pool;
i_assert(ctx->data.attach == NULL);
if (*storage->set->mail_attachment_dir == '\0')
return;
i_zero(&set);
set.min_size = storage->set->mail_attachment_min_size;
if (hash_format_init(storage->set->mail_attachment_hash,
&set.hash_format, &error) < 0) {
/* we already checked this when verifying settings */
i_panic("mail_attachment_hash=%s unexpectedly failed: %s",
storage->set->mail_attachment_hash, error);
}
set.want_attachment = index_attachment_want;
set.open_temp_fd = index_attachment_open_temp_fd;
set.open_attachment_ostream = index_attachment_open_ostream;
set.close_attachment_ostream = index_attachment_close_ostream;
pool = pool_alloconly_create("save attachment", 1024);
attach = p_new(pool, struct mail_save_attachment, 1);
attach->pool = pool;
attach->fs = fs;
attach->input = i_stream_create_attachment_extractor(input, &set, ctx);
p_array_init(&attach->extrefs, attach->pool, 8);
ctx->data.attach = attach;
}
static int save_check_write_error(struct mail_save_context *ctx,
struct ostream *output)
{
struct mail_storage *storage = ctx->transaction->box->storage;
if (output->stream_errno == 0)
return 0;
if (!mail_storage_set_error_from_errno(storage)) {
mail_set_critical(ctx->dest_mail, "write(%s) failed: %s",
o_stream_get_name(output), o_stream_get_error(output));
}
return -1;
}
int index_attachment_save_continue(struct mail_save_context *ctx)
{
struct mail_save_attachment *attach = ctx->data.attach;
const unsigned char *data;
size_t size;
ssize_t ret;
if (attach->input->stream_errno != 0)
return -1;
do {
ret = i_stream_read(attach->input);
if (ret > 0 || ret == -2) {
data = i_stream_get_data(attach->input, &size);
o_stream_nsend(ctx->data.output, data, size);
i_stream_skip(attach->input, size);
}
index_mail_cache_parse_continue(ctx->dest_mail);
if (ret == 0 && !i_stream_attachment_extractor_can_retry(attach->input)) {
/* need more input */
return 0;
}
} while (ret != -1);
if (attach->input->stream_errno != 0) {
mail_set_critical(ctx->dest_mail, "read(%s) failed: %s",
i_stream_get_name(attach->input),
i_stream_get_error(attach->input));
return -1;
}
if (ctx->data.output != NULL) {
if (save_check_write_error(ctx, ctx->data.output) < 0)
return -1;
}
return 0;
}
int index_attachment_save_finish(struct mail_save_context *ctx)
{
struct mail_save_attachment *attach = ctx->data.attach;
(void)i_stream_read(attach->input);
i_assert(attach->input->eof);
return attach->input->stream_errno == 0 ? 0 : -1;
}
void index_attachment_save_free(struct mail_save_context *ctx)
{
struct mail_save_attachment *attach = ctx->data.attach;
if (attach != NULL) {
i_stream_unref(&attach->input);
pool_unref(&attach->pool);
ctx->data.attach = NULL;
}
}
const ARRAY_TYPE(mail_attachment_extref) *
index_attachment_save_get_extrefs(struct mail_save_context *ctx)
{
return ctx->data.attach == NULL ? NULL :
&ctx->data.attach->extrefs;
}
static int
index_attachment_delete_real(struct mail_storage *storage,
struct fs *fs, const char *name)
{
struct fs_file *file;
const char *path;
int ret;
path = t_strdup_printf("%s/%s", index_attachment_dir_get(storage), name);
file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
if ((ret = fs_delete(file)) < 0)
mail_storage_set_critical(storage, "%s", fs_file_last_error(file));
fs_file_deinit(&file);
return ret;
}
int index_attachment_delete(struct mail_storage *storage,
struct fs *fs, const char *name)
{
int ret;
T_BEGIN {
ret = index_attachment_delete_real(storage, fs, name);
} T_END;
return ret;
}
void index_attachment_append_extrefs(string_t *str,
const ARRAY_TYPE(mail_attachment_extref) *extrefs)
{
const struct mail_attachment_extref *extref;
bool add_space = FALSE;
unsigned int startpos;
array_foreach(extrefs, extref) {
if (!add_space)
add_space = TRUE;
else
str_append_c(str, ' ');
str_printfa(str, "%"PRIuUOFF_T" %"PRIuUOFF_T" ",
extref->start_offset, extref->size);
startpos = str_len(str);
if (extref->base64_have_crlf)
str_append_c(str, MAIL_ATTACHMENT_DECODE_OPTION_CRLF);
if (extref->base64_blocks_per_line > 0) {
str_printfa(str, "%c%u",
MAIL_ATTACHMENT_DECODE_OPTION_BASE64,
extref->base64_blocks_per_line * 4);
}
if (startpos == str_len(str)) {
/* make it clear there are no options */
str_append_c(str, MAIL_ATTACHMENT_DECODE_OPTION_NONE);
}
str_append_c(str, ' ');
str_append(str, extref->path);
}
}
static bool
parse_extref_decode_options(const char *str,
struct mail_attachment_extref *extref)
{
unsigned int num;
if (*str == MAIL_ATTACHMENT_DECODE_OPTION_NONE)
return str[1] == '\0';
while (*str != '\0') {
switch (*str) {
case MAIL_ATTACHMENT_DECODE_OPTION_BASE64:
str++; num = 0;
while (*str >= '0' && *str <= '9') {
num = num*10 + (*str-'0');
str++;
}
if (num == 0 || num % 4 != 0)
return FALSE;
extref->base64_blocks_per_line = num/4;
break;
case MAIL_ATTACHMENT_DECODE_OPTION_CRLF:
extref->base64_have_crlf = TRUE;
str++;
break;
default:
return FALSE;
}
}
return TRUE;
}
bool index_attachment_parse_extrefs(const char *line, pool_t pool,
ARRAY_TYPE(mail_attachment_extref) *extrefs)
{
struct mail_attachment_extref extref;
const char *const *args;
unsigned int i, len;
uoff_t last_voffset;
args = t_strsplit(line, " ");
len = str_array_length(args);
if ((len % 4) != 0)
return FALSE;
last_voffset = 0;
for (i = 0; args[i] != NULL; i += 4) {
const char *start_offset_str = args[i+0];
const char *size_str = args[i+1];
const char *decode_options = args[i+2];
const char *path = args[i+3];
i_zero(&extref);
if (str_to_uoff(start_offset_str, &extref.start_offset) < 0 ||
str_to_uoff(size_str, &extref.size) < 0 ||
extref.start_offset < last_voffset ||
!parse_extref_decode_options(decode_options, &extref))
return FALSE;
last_voffset += extref.size +
(extref.start_offset - last_voffset);
extref.path = p_strdup(pool, path);
array_push_back(extrefs, &extref);
}
return TRUE;
}
int index_attachment_stream_get(struct fs *fs, const char *attachment_dir,
const char *path_suffix,
struct istream **stream, uoff_t full_size,
const char *ext_refs, const char **error_r)
{
ARRAY_TYPE(mail_attachment_extref) extrefs_arr;
const struct mail_attachment_extref *extref;
struct istream_attachment_connector *conn;
struct istream *input;
struct fs_file *file;
const char *path;
int ret;
*error_r = NULL;
t_array_init(&extrefs_arr, 16);
if (!index_attachment_parse_extrefs(ext_refs, pool_datastack_create(),
&extrefs_arr)) {
*error_r = "Broken ext-refs string";
return -1;
}
conn = istream_attachment_connector_begin(*stream, full_size);
array_foreach(&extrefs_arr, extref) {
path = t_strdup_printf("%s/%s%s", attachment_dir,
extref->path, path_suffix);
file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY |
FS_OPEN_FLAG_SEEKABLE);
input = i_stream_create_fs_file(&file, IO_BLOCK_SIZE);
ret = istream_attachment_connector_add(conn, input,
extref->start_offset, extref->size,
extref->base64_blocks_per_line,
extref->base64_have_crlf, error_r);
i_stream_unref(&input);
if (ret < 0) {
istream_attachment_connector_abort(&conn);
return -1;
}
}
input = istream_attachment_connector_finish(&conn);
i_stream_set_name(input, t_strdup_printf(
"attachments-connector(%s)", i_stream_get_name(*stream)));
i_stream_unref(stream);
*stream = input;
return 0;
}
|