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
|
/*
** Copyright 1998 - 2009 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "config.h"
#include "maildir.h"
#include "mio.h"
#include "alarmtimer.h"
#include "alarmsleep.h"
#include "config.h"
#include "xconfig.h"
#include "funcs.h"
#include "varlist.h"
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include "mytime.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "../maildir/maildirquota.h"
#include "../maildir/maildircreate.h"
#include "../maildir/maildirmisc.h"
#include "../maildir/maildirkeywords.h"
#include "../maildir/maildirquota.h"
extern int quota_warn_percent;
extern const char *quota_warn_message;
Maildir::Maildir() : is_open(0) , is_afs(0)
{
}
Maildir::~Maildir()
{
MaildirAbort();
}
////////////////////////////////////////////////////////////////////////////
//
// Attempt to detect if this is a Maildir directory.
//
////////////////////////////////////////////////////////////////////////////
int Maildir::IsMaildir(const char *name)
{
Buffer dirname;
Buffer subdirname;
struct stat stat_buf;
int c;
if (!name || !*name) return (0); // Nope, not a Maildir
dirname=name;
c=dirname.pop();
if (c != SLASH_CHAR) dirname.push(c); // Strip trailing /
subdirname=dirname;
subdirname += "/tmp";
subdirname += '\0';
if ( stat( (const char *)subdirname, &stat_buf ) ||
! S_ISDIR(stat_buf.st_mode) ) return (0);
subdirname=dirname;
subdirname += "/new";
subdirname += '\0';
if ( stat( (const char *)subdirname, &stat_buf ) ||
! S_ISDIR(stat_buf.st_mode) ) return (0);
subdirname=dirname;
subdirname += "/cur";
subdirname += '\0';
if ( stat( (const char *)subdirname, &stat_buf ) ||
! S_ISDIR(stat_buf.st_mode) ) return (0);
return (1); // If it looks like a duck, walks like a duck...
}
int Maildir::MaildirOpen(const char *dir, Mio &file, off_t s)
{
Buffer buf;
struct maildirsize quotainfo;
const char *quotap;
Buffer quotabuf;
quotabuf="MAILDIRQUOTA"; /* Reuse a convenient buffer */
quotabuf= *GetVar(quotabuf);
quotabuf += '\0';
quotap=quotabuf;
if (!*quotap)
quotap=NULL;
MaildirAbort();
AlarmTimer abort_timer;
static long counter=0;
buf.set(counter++);
buf += '\0';
struct maildir_tmpcreate_info createInfo;
maildir_tmpcreate_init(&createInfo);
createInfo.maildir=dir;
createInfo.uniq=(const char *)buf;
createInfo.msgsize=s;
createInfo.openmode=0666;
abort_timer.Set( 24 * 60 * 60 );
while (!abort_timer.Expired())
{
Buffer name_buf;
name_buf="UMASK";
const char *um=GetVarStr(name_buf);
unsigned int umask_val=077;
sscanf(um, "%o", &umask_val);
umask_val=umask(umask_val);
int f=maildir_tmpcreate_fd(&createInfo);
umask(umask_val);
if (f >= 0)
{
Buffer b;
b="FLAGS";
const char *flags=GetVarStr(b);
tmpname=createInfo.tmpname;
tmpname += '\0';
if (flags)
{
const char *p=flags;
while (*p)
{
if (strchr("DRSF", *p) == NULL)
{
f=0;
break;
}
++p;
}
}
if (flags && *flags)
{
newname=createInfo.curname;
newname += ':';
newname += '2';
newname += ',';
newname += flags;
}
else
{
newname=createInfo.newname;
}
newname += '\0';
maildir_tmpcreate_free(&createInfo);
file.fd(f);
is_open=1;
maildirRoot=dir;
maildirRoot += '\0';
if (maildir_quota_add_start(dir, "ainfo, s,
1, quotap))
{
file.fd(-1);
unlink( (const char *)tmpname );
is_open=0;
maildir_deliver_quota_warning(dir,
quota_warn_percent,
quota_warn_message);
merr << "maildrop: maildir over quota.\n";
return (-1);
}
maildir_quota_add_end("ainfo, s, 1);
return (0);
}
if (errno != EAGAIN)
{
merr << "maildrop: " << dir << ": " << strerror(errno)
<< "\n";
return -1;
}
AlarmSleep try_again(2);
}
merr << "maildrop: time out on maildir directory.\n";
return (-1);
}
void Maildir::MaildirSave()
{
if (is_open)
{
Buffer keywords;
keywords="KEYWORDS";
keywords=*GetVar(keywords);
keywords += '\0';
const char *keywords_s=keywords;
while (*keywords_s && *keywords_s == ',')
++keywords_s;
if (*keywords_s)
{
struct libmail_kwHashtable kwh;
struct libmail_kwMessage *kwm;
libmail_kwhInit(&kwh);
if ((kwm=libmail_kwmCreate()) == NULL)
throw strerror(errno);
while (*keywords_s)
{
const char *p=keywords_s;
while (*keywords_s && *keywords_s != ',')
++keywords_s;
char *n=new char [keywords_s - p + 1];
if (!n)
{
libmail_kwmDestroy(kwm);
throw strerror(errno);
}
memcpy(n, p, keywords_s - p);
n[keywords_s - p]=0;
while (*keywords_s && *keywords_s == ',')
++keywords_s;
if (libmail_kwmSetName(&kwh, kwm, n) < 0)
{
delete [] n;
libmail_kwmDestroy(kwm);
throw strerror(errno);
}
delete [] n;
}
char *tmpkname, *newkname;
if (maildir_kwSave( (const char *)maildirRoot,
strrchr(newname, '/')+1, kwm,
&tmpkname, &newkname, 0) < 0)
{
libmail_kwmDestroy(kwm);
throw "maildir_kwSave() failed.";
}
libmail_kwmDestroy(kwm);
if (rename(tmpkname, newkname) < 0)
{
/* Maybe the keyword directory needs creating */
struct stat stat_buf;
if (stat(maildirRoot, &stat_buf) < 0)
{
free(tmpkname);
free(newkname);
throw strerror(errno);
}
char *keywordDir=strrchr(newkname, '/');
*keywordDir=0;
mkdir(newkname, 0700);
chmod(newkname, stat_buf.st_mode & 0777);
*keywordDir='/';
if (rename(tmpkname, newkname) < 0)
{
free(tmpkname);
free(newkname);
throw strerror(errno);
}
}
free(tmpkname);
free(newkname);
}
Buffer dir;
if (link( (const char *)tmpname, (const char *)newname) < 0)
{
if (errno == EXDEV){
if(rename((const char *)tmpname, (const char *)newname) < 0)
throw "rename() failed.";
is_afs = 1;
}
else
{
throw "link() failed.";
}
}
dir=newname;
const char *p=dir;
const char *q=strrchr(p, '/');
if (q)
{
dir.Length(q-p);
dir.push((char)0);
#if EXPLICITDIRSYNC
int syncfd=open(dir, O_RDONLY);
if (syncfd >= 0)
{
fsync(syncfd);
close(syncfd);
}
#endif
dir.Length(q-p);
dir += "/../";
dir.push((char)0);
maildir_deliver_quota_warning(dir, quota_warn_percent,
quota_warn_message);
}
}
}
void Maildir::MaildirAbort()
{
if (is_open && !is_afs) unlink( (const char *)tmpname );
}
|