Coverage Report

Created: 2026-12-01 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib-storage/index/dbox-single/sdbox-file.c
Line
Count
Source
1
/* Copyright (c) Dovecot authors, see top-level COPYING file */
2
3
#include "lib.h"
4
#include "eacces-error.h"
5
#include "fdatasync-path.h"
6
#include "mkdir-parents.h"
7
#include "istream.h"
8
#include "ostream.h"
9
#include "str.h"
10
#include "fs-api.h"
11
#include "dbox-attachment.h"
12
#include "sdbox-storage.h"
13
#include "sdbox-file.h"
14
15
#include <stdio.h>
16
#include <utime.h>
17
18
static void sdbox_file_init_paths(struct sdbox_file *file, const char *fname)
19
0
{
20
0
  struct mailbox *box = &file->mbox->box;
21
0
  const char *alt_path;
22
23
0
  i_free(file->file.primary_path);
24
0
  i_free(file->file.alt_path);
25
0
  file->file.primary_path =
26
0
    i_strdup_printf("%s/%s", mailbox_get_path(box), fname);
27
0
  file->file.cur_path = file->file.primary_path;
28
29
0
  if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX,
30
0
        &alt_path) > 0)
31
0
    file->file.alt_path = i_strdup_printf("%s/%s", alt_path, fname);
32
0
}
33
34
struct dbox_file *sdbox_file_init(struct sdbox_mailbox *mbox, uint32_t uid)
35
0
{
36
0
  struct sdbox_file *file;
37
0
  const char *fname;
38
39
0
  file = i_new(struct sdbox_file, 1);
40
0
  file->file.storage = &mbox->storage->storage;
41
0
  file->mbox = mbox;
42
0
  T_BEGIN {
43
0
    if (uid != 0) {
44
0
      fname = t_strdup_printf(SDBOX_MAIL_FILE_FORMAT, uid);
45
0
      sdbox_file_init_paths(file, fname);
46
0
      file->uid = uid;
47
0
    } else {
48
0
      sdbox_file_init_paths(file, dbox_generate_tmp_filename());
49
0
    }
50
0
  } T_END;
51
0
  dbox_file_init(&file->file);
52
0
  return &file->file;
53
0
}
54
55
struct dbox_file *sdbox_file_create(struct sdbox_mailbox *mbox)
56
0
{
57
0
  struct dbox_file *file;
58
59
0
  file = sdbox_file_init(mbox, 0);
60
0
  file->fd = file->storage->v.
61
0
    file_create_fd(file, file->primary_path, FALSE);
62
0
  return file;
63
0
}
64
65
void sdbox_file_free(struct dbox_file *file)
66
0
{
67
0
  struct sdbox_file *sfile = (struct sdbox_file *)file;
68
69
0
  pool_unref(&sfile->attachment_pool);
70
0
  dbox_file_free(file);
71
0
}
72
73
int sdbox_file_get_attachments(struct dbox_file *file, const char **extrefs_r)
74
0
{
75
0
  const char *line;
76
0
  bool deleted;
77
0
  int ret;
78
79
0
  *extrefs_r = NULL;
80
81
  /* read the metadata */
82
0
  ret = dbox_file_open(file, &deleted);
83
0
  if (ret > 0) {
84
0
    if (deleted)
85
0
      return 0;
86
0
    if ((ret = dbox_file_seek(file, 0)) > 0)
87
0
      ret = dbox_file_metadata_read(file);
88
0
  }
89
0
  if (ret <= 0) {
90
0
    if (ret < 0)
91
0
      return -1;
92
    /* corrupted file. we're deleting it anyway. */
93
0
    line = NULL;
94
0
  } else {
95
0
    line = dbox_file_metadata_get(file, DBOX_METADATA_EXT_REF);
96
0
  }
97
0
  if (line == NULL) {
98
    /* no attachments */
99
0
    return 0;
100
0
  }
101
0
  *extrefs_r = line;
102
0
  return 1;
103
0
}
104
105
const char *
106
sdbox_file_attachment_relpath(struct sdbox_file *file, const char *srcpath)
107
0
{
108
0
  const char *p;
109
110
0
  p = strchr(srcpath, '-');
111
0
  if (p == NULL) {
112
0
    mailbox_set_critical(&file->mbox->box,
113
0
      "sdbox attachment path in invalid format: %s", srcpath);
114
0
  } else {
115
0
    p = strchr(p+1, '-');
116
0
  }
117
0
  return t_strdup_printf("%s-%s-%u",
118
0
      p == NULL ? srcpath : t_strdup_until(srcpath, p),
119
0
      guid_128_to_string(file->mbox->mailbox_guid),
120
0
      file->uid);
121
0
}
122
123
static int sdbox_file_rename_attachments(struct sdbox_file *file)
124
0
{
125
0
  struct dbox_storage *storage = file->file.storage;
126
0
  struct fs_file *src_file, *dest_file;
127
0
  const char *path, *src, *dest;
128
0
  int ret = 0;
129
130
0
  array_foreach_elem(&file->attachment_paths, path) T_BEGIN {
131
0
    src = t_strdup_printf("%s/%s", storage->attachment_dir, path);
132
0
    dest = t_strdup_printf("%s/%s", storage->attachment_dir,
133
0
        sdbox_file_attachment_relpath(file, path));
134
0
    src_file = fs_file_init(storage->attachment_fs, src,
135
0
          FS_OPEN_MODE_READONLY);
136
0
    dest_file = fs_file_init(storage->attachment_fs, dest,
137
0
          FS_OPEN_MODE_READONLY);
138
0
    if (fs_rename(src_file, dest_file) < 0) {
139
0
      mailbox_set_critical(&file->mbox->box, "%s",
140
0
        fs_file_last_error(dest_file));
141
0
      ret = -1;
142
0
    }
143
0
    fs_file_deinit(&src_file);
144
0
    fs_file_deinit(&dest_file);
145
0
  } T_END;
146
0
  return ret;
147
0
}
148
149
int sdbox_file_assign_uid(struct sdbox_file *file, uint32_t uid)
150
0
{
151
0
  const char *p, *old_path, *dir, *new_fname, *new_path;
152
0
  struct stat st;
153
154
0
  i_assert(file->uid == 0);
155
0
  i_assert(uid != 0);
156
157
0
  old_path = file->file.cur_path;
158
0
  p = strrchr(old_path, '/');
159
0
  i_assert(p != NULL);
160
0
  dir = t_strdup_until(old_path, p);
161
162
0
  new_fname = t_strdup_printf(SDBOX_MAIL_FILE_FORMAT, uid);
163
0
  new_path = t_strdup_printf("%s/%s", dir, new_fname);
164
165
0
  if (stat(new_path, &st) == 0) {
166
0
    sdbox_set_mailbox_corrupted(&file->mbox->box, t_strdup_printf(
167
0
      "%s already exists, rebuilding index", new_path));
168
0
    return -1;
169
0
  }
170
0
  if (rename(old_path, new_path) < 0) {
171
0
    mailbox_set_critical(&file->mbox->box,
172
0
             "rename(%s, %s) failed: %m",
173
0
             old_path, new_path);
174
0
    return -1;
175
0
  }
176
0
  sdbox_file_init_paths(file, new_fname);
177
0
  file->uid = uid;
178
179
0
  if (array_is_created(&file->attachment_paths)) {
180
0
    if (sdbox_file_rename_attachments(file) < 0)
181
0
      return -1;
182
0
  }
183
0
  return 0;
184
0
}
185
186
static int sdbox_file_unlink_aborted_save_attachments(struct sdbox_file *file)
187
0
{
188
0
  struct dbox_storage *storage = file->file.storage;
189
0
  struct fs *fs = storage->attachment_fs;
190
0
  struct fs_file *fs_file;
191
0
  const char *path, *att_path;
192
0
  int ret = 0;
193
194
0
  array_foreach_elem(&file->attachment_paths, att_path) T_BEGIN {
195
    /* we don't know if we aborted before renaming this attachment,
196
       so try deleting both source and dest path. the source paths
197
       point to temporary files (not to source messages'
198
       attachment paths), so it's safe to delete them. */
199
0
    path = t_strdup_printf("%s/%s", storage->attachment_dir,
200
0
               att_path);
201
0
    fs_file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
202
0
    if (fs_delete(fs_file) < 0 &&
203
0
        errno != ENOENT) {
204
0
      mailbox_set_critical(&file->mbox->box, "%s",
205
0
               fs_file_last_error(fs_file));
206
0
      ret = -1;
207
0
    }
208
0
    fs_file_deinit(&fs_file);
209
210
0
    path = t_strdup_printf("%s/%s", storage->attachment_dir,
211
0
        sdbox_file_attachment_relpath(file, att_path));
212
0
    fs_file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
213
0
    if (fs_delete(fs_file) < 0 &&
214
0
        errno != ENOENT) {
215
0
      mailbox_set_critical(&file->mbox->box, "%s",
216
0
               fs_file_last_error(fs_file));
217
0
      ret = -1;
218
0
    }
219
0
    fs_file_deinit(&fs_file);
220
0
  } T_END;
221
0
  return ret;
222
0
}
223
224
int sdbox_file_unlink_aborted_save(struct sdbox_file *file)
225
0
{
226
0
  int ret = 0;
227
228
0
  if (unlink(file->file.cur_path) < 0) {
229
0
    mailbox_set_critical(&file->mbox->box,
230
0
      "unlink(%s) failed: %m", file->file.cur_path);
231
0
    ret = -1;
232
0
  }
233
0
  if (array_is_created(&file->attachment_paths)) {
234
0
    if (sdbox_file_unlink_aborted_save_attachments(file) < 0)
235
0
      ret = -1;
236
0
  }
237
0
  return ret;
238
0
}
239
240
int sdbox_file_create_fd(struct dbox_file *file, const char *path, bool parents)
241
0
{
242
0
  struct sdbox_file *sfile = (struct sdbox_file *)file;
243
0
  struct mailbox *box = &sfile->mbox->box;
244
0
  const struct mailbox_permissions *perm = mailbox_get_permissions(box);
245
0
  const char *p, *dir;
246
0
  mode_t old_mask;
247
0
  int fd;
248
249
0
  old_mask = umask(0666 & ~perm->file_create_mode);
250
0
  fd = open(path, O_RDWR | O_CREAT | O_TRUNC | O_NOFOLLOW, 0666);
251
0
  umask(old_mask);
252
0
  if (fd == -1 && errno == ENOENT && parents &&
253
0
      (p = strrchr(path, '/')) != NULL) {
254
0
    dir = t_strdup_until(path, p);
255
0
    if (mkdir_parents_chgrp(dir, perm->dir_create_mode,
256
0
          perm->file_create_gid,
257
0
          perm->file_create_gid_origin) < 0 &&
258
0
       errno != EEXIST) {
259
0
      mailbox_set_critical(box,
260
0
        "mkdir_parents(%s) failed: %m", dir);
261
0
      return -1;
262
0
    }
263
    /* try again */
264
0
    old_mask = umask(0666 & ~perm->file_create_mode);
265
0
    fd = open(path, O_RDWR | O_CREAT | O_TRUNC | O_NOFOLLOW, 0666);
266
0
    umask(old_mask);
267
0
  }
268
0
  if (fd == -1) {
269
0
    if (errno == ENOENT) {
270
0
      sdbox_set_mailbox_corrupted(box, t_strdup_printf(
271
0
        "open(%s, O_CREAT) failed: %m", path));
272
0
    } else {
273
0
      mailbox_set_critical(box, "open(%s, O_CREAT) failed: %m", path);
274
0
    }
275
0
  } else if (perm->file_create_gid == (gid_t)-1) {
276
    /* no group change */
277
0
  } else if (fchown(fd, (uid_t)-1, perm->file_create_gid) < 0) {
278
0
    if (errno == EPERM) {
279
0
      mailbox_set_critical(box, "%s",
280
0
        eperm_error_get_chgrp("fchown", path,
281
0
          perm->file_create_gid,
282
0
          perm->file_create_gid_origin));
283
0
    } else {
284
0
      mailbox_set_critical(box,
285
0
        "fchown(%s, -1, %ld) failed: %m",
286
0
        path, (long)perm->file_create_gid);
287
0
    }
288
    /* continue anyway */
289
0
  }
290
0
  return fd;
291
0
}
292
293
int sdbox_file_move(struct dbox_file *file, bool alt_path)
294
0
{
295
0
  struct mail_storage *storage = &file->storage->storage;
296
0
  struct ostream *output;
297
0
  const char *dest_dir, *temp_path, *dest_path, *p;
298
0
  struct stat st;
299
0
  struct utimbuf ut;
300
0
  bool deleted;
301
0
  int out_fd, ret = 0;
302
303
0
  i_assert(file->input != NULL);
304
305
0
  if (dbox_file_is_in_alt(file) == alt_path)
306
0
    return 0;
307
0
  if (file->alt_path == NULL)
308
0
    return 0;
309
310
0
  if (stat(file->cur_path, &st) < 0 && errno == ENOENT) {
311
    /* already expunged/moved by another session */
312
0
    return 0;
313
0
  }
314
315
0
  dest_path = !alt_path ? file->primary_path : file->alt_path;
316
317
0
  i_assert(dest_path != NULL);
318
319
0
  p = strrchr(dest_path, '/');
320
0
  i_assert(p != NULL);
321
0
  dest_dir = t_strdup_until(dest_path, p);
322
0
  temp_path = t_strdup_printf("%s/%s", dest_dir,
323
0
            dbox_generate_tmp_filename());
324
325
  /* first copy the file. make sure to catch every possible error
326
     since we really don't want to break the file. */
327
0
  out_fd = file->storage->v.file_create_fd(file, temp_path, TRUE);
328
0
  if (out_fd == -1)
329
0
    return -1;
330
331
0
  output = o_stream_create_fd_file(out_fd, 0, FALSE);
332
0
  i_stream_seek(file->input, 0);
333
0
  o_stream_nsend_istream(output, file->input);
334
0
  if (o_stream_finish(output) < 0) {
335
0
    mail_storage_set_critical(storage, "write(%s) failed: %s",
336
0
      temp_path, o_stream_get_error(output));
337
0
    ret = -1;
338
0
  }
339
0
  o_stream_unref(&output);
340
341
0
  if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER && ret == 0) {
342
0
    if (fsync(out_fd) < 0) {
343
0
      mail_storage_set_critical(storage,
344
0
        "fsync(%s) failed: %m", temp_path);
345
0
      ret = -1;
346
0
    }
347
0
  }
348
0
  if (close(out_fd) < 0) {
349
0
    mail_storage_set_critical(storage,
350
0
      "close(%s) failed: %m", temp_path);
351
0
    ret = -1;
352
0
  }
353
0
  if (ret < 0) {
354
0
    i_unlink(temp_path);
355
0
    return -1;
356
0
  }
357
  /* preserve the original atime/mtime. this isn't necessary for Dovecot,
358
     but could be useful for external reasons. */
359
0
  ut.actime = st.st_atime;
360
0
  ut.modtime = st.st_mtime;
361
0
  if (utime(temp_path, &ut) < 0) {
362
0
    mail_storage_set_critical(storage,
363
0
      "utime(%s) failed: %m", temp_path);
364
0
  }
365
366
  /* the temp file was successfully written. rename it now to the
367
     destination file. the destination shouldn't exist, but if it does
368
     its contents should be the same (except for maybe older metadata) */
369
0
  if (rename(temp_path, dest_path) < 0) {
370
0
    mail_storage_set_critical(storage,
371
0
      "rename(%s, %s) failed: %m", temp_path, dest_path);
372
0
    i_unlink_if_exists(temp_path);
373
0
    return -1;
374
0
  }
375
0
  if (storage->set->parsed_fsync_mode != FSYNC_MODE_NEVER) {
376
0
    if (fdatasync_path(dest_dir) < 0) {
377
0
      mail_storage_set_critical(storage,
378
0
        "fdatasync(%s) failed: %m", dest_dir);
379
0
      i_unlink(dest_path);
380
0
      return -1;
381
0
    }
382
0
  }
383
0
  if (unlink(file->cur_path) < 0) {
384
0
    dbox_file_set_syscall_error(file, "unlink()");
385
0
    if (ENOACCESS(errno)) {
386
      /* configuration problem? revert the write */
387
0
      i_unlink(dest_path);
388
0
    }
389
    /* who knows what happened to the file. keep both just to be
390
       sure both won't get deleted. */
391
0
    return -1;
392
0
  }
393
394
  /* file was successfully moved - reopen it */
395
0
  dbox_file_close(file);
396
0
  if (dbox_file_open(file, &deleted) <= 0) {
397
0
    mail_storage_set_critical(storage,
398
0
      "dbox_file_move(%s): reopening file failed", dest_path);
399
0
    return -1;
400
0
  }
401
0
  return 0;
402
0
}
403
404
static int
405
sdbox_unlink_attachments(struct sdbox_file *sfile,
406
       const ARRAY_TYPE(mail_attachment_extref) *extrefs)
407
0
{
408
0
  struct dbox_storage *storage = sfile->file.storage;
409
0
  const struct mail_attachment_extref *extref;
410
0
  const char *path;
411
0
  int ret = 0;
412
413
0
  array_foreach(extrefs, extref) T_BEGIN {
414
0
    path = sdbox_file_attachment_relpath(sfile, extref->path);
415
0
    if (index_attachment_delete(&storage->storage,
416
0
              storage->attachment_fs, path) < 0)
417
0
      ret = -1;
418
0
  } T_END;
419
0
  return ret;
420
0
}
421
422
int sdbox_file_unlink_with_attachments(struct sdbox_file *sfile)
423
0
{
424
0
  struct event *event = sfile->mbox->box.event;
425
0
  ARRAY_TYPE(mail_attachment_extref) extrefs;
426
0
  const char *extrefs_line;
427
0
  pool_t pool;
428
0
  int ret;
429
430
0
  ret = sdbox_file_get_attachments(&sfile->file, &extrefs_line);
431
0
  if (ret < 0)
432
0
    return -1;
433
0
  if (ret == 0) {
434
    /* no attachments */
435
0
    return dbox_file_unlink(&sfile->file);
436
0
  }
437
438
0
  pool = pool_alloconly_create("sdbox attachments unlink", 1024);
439
0
  p_array_init(&extrefs, pool, 16);
440
0
  if (!index_attachment_parse_extrefs(extrefs_line, pool, &extrefs)) {
441
0
    e_warning(event, "%s: Ignoring corrupted extref: %s",
442
0
        sfile->file.cur_path, extrefs_line);
443
0
    array_clear(&extrefs);
444
0
  }
445
446
  /* try to delete the file first, so if it fails we don't have
447
     missing attachments */
448
0
  if ((ret = dbox_file_unlink(&sfile->file)) >= 0)
449
0
    (void)sdbox_unlink_attachments(sfile, &extrefs);
450
0
  pool_unref(&pool);
451
0
  return ret;
452
0
}