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
|
/**
* @file maildir-scan.cc
* @brief Implementation of the Maildir class.
* @author Andreas Aardal Hanssen
* @date 2002-2005
*/
#include "iodevice.h"
#include "iofactory.h"
#include "maildir.h"
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace Binc;
using std::endl;
using std::string;
Lock::Lock(const string &path)
{
lock = (path == "" ? "." : path) + "/bincimap-scan-lock";
int lockfd = -1;
while ((lockfd = ::open(lock.c_str(), O_CREAT | O_WRONLY | O_EXCL, 0666)) == -1) {
if (errno != EEXIST) {
bincWarning << "unable to lock mailbox: " << lock << ", " << string(strerror(errno))
<< endl;
return;
}
struct stat mystat;
bincWarning << "waiting for mailbox lock " << lock << "." << endl;
if (lstat(lock.c_str(), &mystat) == 0) {
if ((time(nullptr) - mystat.st_ctime) > 300) {
if (unlink(lock.c_str()) == 0) {
continue;
} else {
bincWarning << "failed to force mailbox lock: " << lock << ", "
<< string(strerror(errno)) << endl;
}
}
} else if (errno != ENOENT) {
string err = "invalid lock " + lock + ": " + strerror(errno);
bincWarning << err << endl;
return;
}
// sleep one second.
sleep(1);
}
close(lockfd);
}
Lock::~Lock()
{
// remove the lock
if (unlink(lock.c_str()) != 0) {
bincWarning << "failed to unlock mailbox: " << lock << ", " << strerror(errno) << endl;
}
}
// scan the maildir. update flags, find messages in new/ and move them
// to cur, setting the recent flag in memory only. check for expunged
// messages. give newly arrived messages uids.
Maildir::ScanResult Maildir::scan(bool forceScan)
{
const string newpath = path + "/new/";
const string curpath = path + "/cur/";
const string cachepath = path + "/bincimap-cache";
// check wether or not we need to bother scanning the folder.
if (firstscan || forceScan) {
struct stat oldstat;
if (stat(newpath.c_str(), &oldstat) != 0) {
setLastError("Invalid Mailbox, " + newpath + ": " + string(strerror(errno)));
return PermanentError;
}
old_new_st_mtime = oldstat.st_mtime;
old_new_st_ctime = oldstat.st_ctime;
if (stat(curpath.c_str(), &oldstat) != 0) {
setLastError("Invalid Mailbox, " + curpath + ": " + string(strerror(errno)));
return PermanentError;
}
old_cur_st_mtime = oldstat.st_mtime;
old_cur_st_ctime = oldstat.st_ctime;
if (stat(cachepath.c_str(), &oldstat) == 0) {
old_bincimap_cache_st_mtime = oldstat.st_mtime;
old_bincimap_cache_st_ctime = oldstat.st_ctime;
} else {
old_bincimap_cache_st_mtime = {};
old_bincimap_cache_st_ctime = {};
}
} else {
struct stat oldcurstat;
struct stat oldnewstat;
struct stat oldbincimapcachestat;
if (stat(newpath.c_str(), &oldnewstat) != 0) {
setLastError("Invalid Mailbox, " + newpath + ": " + string(strerror(errno)));
return PermanentError;
}
if (stat(curpath.c_str(), &oldcurstat) != 0) {
setLastError("Invalid Mailbox, " + curpath + ": " + string(strerror(errno)));
return PermanentError;
}
if (stat(cachepath.c_str(), &oldbincimapcachestat) != 0) {
oldbincimapcachestat.st_ctime = 0;
oldbincimapcachestat.st_mtime = 0;
}
if (oldnewstat.st_mtime == old_new_st_mtime && oldnewstat.st_ctime == old_new_st_ctime
&& oldcurstat.st_mtime == old_cur_st_mtime && oldcurstat.st_ctime == old_cur_st_ctime
&& oldbincimapcachestat.st_mtime == old_bincimap_cache_st_mtime
&& oldbincimapcachestat.st_ctime == old_bincimap_cache_st_ctime)
{
return Success;
}
old_bincimap_cache_st_mtime = oldbincimapcachestat.st_mtime;
old_bincimap_cache_st_ctime = oldbincimapcachestat.st_ctime;
old_cur_st_mtime = oldcurstat.st_mtime;
old_cur_st_ctime = oldcurstat.st_ctime;
old_new_st_mtime = oldnewstat.st_mtime;
old_new_st_ctime = oldnewstat.st_ctime;
}
// lock the directory as we are scanning. this prevents race
// conditions with uid delegation
Lock lock(path);
// Read the cache file if it's there. It holds important information
// about the state of the depository, and serves to communicate
// changes to the depot across Binc IMAP instances that can not be
// communicated via the depot itself.
switch (readCache()) {
case NoCache:
case Error:
// An error with reading the cache files when it's not the first
// time we scan the depot is treated as an error.
if (!firstscan && !readOnly) {
old_cur_st_mtime = {};
old_cur_st_ctime = {};
old_new_st_mtime = {};
old_new_st_ctime = {};
return TemporaryError;
}
mailboxchanged = true;
break;
default:
break;
}
// open new/ directory
DIR *pdir = opendir(newpath.c_str());
if (pdir == nullptr) {
string reason = "failed to open \"" + newpath + "\" (";
reason += strerror(errno);
reason += ")";
setLastError(reason);
return PermanentError;
}
// scan all entries
struct dirent *pdirent;
while ((pdirent = readdir(pdir)) != nullptr) {
// "Unless you're writing messages to a maildir, the format of a
// unique name is none of your business. A unique name can be
// anything that doesn't contain a colon (or slash) and doesn't
// start with a dot. Do not try to extract information from unique
// names." - The Maildir spec from cr.yp.to
string filename = pdirent->d_name;
if (filename[0] == '.' || filename.find(':') != string::npos
|| filename.find('/') != string::npos)
continue;
string fullfilename = newpath + filename;
// We need to find the timestamp of the message in order to
// determine whether or not it's safe to move the message in from
// new/. qmail's default message file naming algorithm forces us
// to never move messages out of new/ that are less than one
// second old.
struct stat mystat;
if (stat(fullfilename.c_str(), &mystat) != 0) {
if (errno == ENOENT) {
// prevent looping due to stale symlinks
if (lstat(fullfilename.c_str(), &mystat) == 0) {
bincWarning << "dangling symlink: " << fullfilename << endl;
continue;
}
// a rare race between readdir and stat force us to restart the scan.
closedir(pdir);
if ((pdir = opendir(newpath.c_str())) == nullptr) {
string reason = "Warning: opendir(\"" + newpath + "\") == 0 (";
reason += strerror(errno);
reason += ")";
setLastError(reason);
return PermanentError;
}
} else {
bincWarning << "junk in Maildir: \"" << fullfilename << "\": " << strerror(errno);
}
continue;
}
// this is important. do not move messages from new/ that are not
// at least one second old or messages may disappear. this
// introduces a special case: we can not cache the old st_ctime
// and st_mtime. the next time the mailbox is scanned, it must not
// simply be skipped. :-)
bool ours = false;
for (const auto &newIt : newMessages) {
if ((filename == newIt.getUnique())
&& (newIt.getInternalFlags() & MaildirMessage::Committed))
{
ours = true;
break;
}
}
if (!ours && ::time(nullptr) <= mystat.st_mtime) {
old_cur_st_mtime = {};
old_cur_st_ctime = {};
old_new_st_mtime = {};
old_new_st_ctime = {};
continue;
}
// move files from new/ to cur/
string newName = curpath + pdirent->d_name;
if (rename((newpath + pdirent->d_name).c_str(), (newName + ":2,").c_str()) != 0) {
bincWarning << "error moving messages from new to cur: skipping " << newpath
<< pdirent->d_name << ": " << strerror(errno) << endl;
continue;
}
}
closedir(pdir);
// Now, assume all known messages were expunged and have them prove
// otherwise.
{
Mailbox::iterator i = begin(SequenceSet::all(), INCLUDE_EXPUNGED | SQNR_MODE);
for (; i != end(); ++i)
(*i).setExpunged();
}
// Then, scan cur
// open directory
if ((pdir = opendir(curpath.c_str())) == nullptr) {
string reason = "Maildir::scan::opendir(\"" + curpath + "\") == 0 (";
reason += strerror(errno);
reason += ")";
setLastError(reason);
return PermanentError;
}
// erase all old maps between fixed filenames and actual file names.
// we'll get a new list now, which will be more up to date.
index.clearFileNames();
// this is to sort recent messages by internaldate
std::multimap<unsigned int, MaildirMessage> tempMessageMap;
// scan all entries
while ((pdirent = readdir(pdir)) != nullptr) {
string filename = pdirent->d_name;
if (filename[0] == '.') continue;
string uniquename;
string standard;
string::size_type pos;
if ((pos = filename.find(':')) != string::npos) {
uniquename = filename.substr(0, pos);
string tmp = filename.substr(pos);
if ((pos = tmp.find("2,")) != string::npos) standard = tmp.substr(pos + 2);
} else {
uniquename = filename;
}
unsigned char mflags = Message::F_NONE;
for (char i : standard) {
switch (i) {
case 'R':
mflags |= Message::F_ANSWERED;
break;
case 'S':
mflags |= Message::F_SEEN;
break;
case 'T':
mflags |= Message::F_DELETED;
break;
case 'D':
mflags |= Message::F_DRAFT;
break;
case 'F':
mflags |= Message::F_FLAGGED;
break;
case 'P':
mflags |= Message::F_PASSED;
break;
default:
break;
}
}
struct stat mystat;
MaildirMessage *message = get(uniquename);
if (!message || message->getInternalDate() == 0) {
string fullfilename = curpath + filename;
if (stat(fullfilename.c_str(), &mystat) != 0) {
if (errno == ENOENT) {
// prevent looping due to stale symlinks
if (lstat(fullfilename.c_str(), &mystat) == 0) {
bincWarning << "dangling symlink: " << fullfilename << endl;
continue;
}
// a rare race between readdir and stat force us to restart
// the scan.
index.clearFileNames();
closedir(pdir);
if ((pdir = opendir(newpath.c_str())) == nullptr) {
string reason = "Warning: opendir(\"" + newpath + "\") == 0 (";
reason += strerror(errno);
reason += ")";
setLastError(reason);
return PermanentError;
}
}
continue;
}
mailboxchanged = true;
}
index.insert(uniquename, 0, filename);
// If we have this message in memory already..
if (message) {
if (message->getInternalDate() == time_t{}) {
mailboxchanged = true;
message->setInternalDate(mystat.st_mtime);
}
// then confirm that this message was not expunged
message->setUnExpunged();
// update the flags with what new flags we found in the filename,
// but keep the \Recent flag regardless.
if (mflags != (message->getStdFlags() & ~Message::F_RECENT)) {
int oldflags = message->getStdFlags();
message->resetStdFlags();
message->setStdFlag(mflags | (oldflags & Message::F_RECENT));
}
continue;
}
// Wait with delegating UIDs until all entries have been
// read. Only then can we sort by internaldate and delegate new
// UIDs.
MaildirMessage m(*this);
m.setUID(0);
m.setSize(0);
m.setInternalDate(mystat.st_mtime);
m.setStdFlag((mflags | Message::F_RECENT) & ~Message::F_EXPUNGED);
m.setUnique(uniquename);
tempMessageMap.insert(std::make_pair((unsigned int)mystat.st_mtime, m));
mailboxchanged = true;
}
closedir(pdir);
// Recent messages are added, ordered by internaldate.
{
int readonlyuidnext = uidnext;
for (auto i = tempMessageMap.begin(); i != tempMessageMap.end(); ++i) {
i->second.setUID(readOnly ? readonlyuidnext++ : uidnext++);
add(i->second);
tempMessageMap.erase(i);
mailboxchanged = true;
}
}
tempMessageMap.clear();
// Messages that existed in the cache that we read, but did not
// exist in the Maildir, are removed from the messages list.
Mailbox::iterator jj = begin(SequenceSet::all(), INCLUDE_EXPUNGED | SQNR_MODE);
while (jj != end()) {
auto &message = dynamic_cast<MaildirMessage &>(*jj);
if (message.isExpunged()) {
mailboxchanged = true;
if (message.getInternalFlags() & MaildirMessage::JustArrived) {
jj.erase();
continue;
}
} else if (message.getInternalFlags() & MaildirMessage::JustArrived) {
message.clearInternalFlag(MaildirMessage::JustArrived);
}
++jj;
}
// Special case: The first time we scan is in SELECT. All flags
// changes for new messages will then appear to be recent, and
// to avoid this to be sent to the client as a pending update,
// we explicitly unset the "flagsChanged" flag in all messages.
if (firstscan) {
unsigned int lastuid = 0;
Mailbox::iterator ii = begin(SequenceSet::all(), INCLUDE_EXPUNGED | SQNR_MODE);
for (; ii != end(); ++ii) {
MaildirMessage &message = dynamic_cast<MaildirMessage &>(*ii);
message.clearInternalFlag(MaildirMessage::JustArrived);
if (lastuid < message.getUID()) {
lastuid = message.getUID();
} else {
bincWarning << "UID values are not strictly ascending in this"
" mailbox: "
<< path << ". This is usually caused by "
<< "access from a broken accessor. Bumping UIDVALIDITY." << endl;
setLastError("An error occurred while scanning the mailbox. "
"Please contact your system administrator.");
if (!readOnly) {
bumpUidValidity(path);
old_cur_st_mtime = {};
old_cur_st_ctime = {};
old_new_st_mtime = {};
old_new_st_ctime = {};
return TemporaryError;
} else {
return PermanentError;
}
}
message.setFlagsUnchanged();
}
}
if (mailboxchanged && !readOnly) {
if (!writeCache()) return PermanentError;
mailboxchanged = false;
}
firstscan = false;
newMessages.clear();
return Success;
}
|