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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <auth.h>
#include <libsec.h>
#include "imap4d.h"
static int saveMsg(char *dst, char *digest, int flags, char *head, int nhead, Biobuf *b, long n);
static int saveb(int fd, DigestState *dstate, char *buf, int nr, int nw);
static long appSpool(Biobuf *bout, Biobuf *bin, long n);
/*
* check if the message exists
*/
int
copyCheck(Box *box, Msg *m, int uids, void *v)
{
int fd;
USED(box);
USED(uids);
USED(v);
if(m->expunged)
return 0;
fd = msgFile(m, "raw");
if(fd < 0){
msgDead(m);
return 0;
}
close(fd);
return 1;
}
int
copySave(Box *box, Msg *m, int uids, void *vs)
{
Dir *d;
Biobuf b;
vlong length;
char *head;
int ok, hfd, bfd, nhead;
USED(box);
USED(uids);
if(m->expunged)
return 0;
hfd = msgFile(m, "unixheader");
if(hfd < 0){
msgDead(m);
return 0;
}
head = readFile(hfd);
if(head == nil){
close(hfd);
return 0;
}
/*
* clear out the header if it doesn't end in a newline,
* since it is a runt and the "header" will show up in the raw file.
*/
nhead = strlen(head);
if(nhead > 0 && head[nhead-1] != '\n')
nhead = 0;
bfd = msgFile(m, "raw");
close(hfd);
if(bfd < 0){
msgDead(m);
return 0;
}
d = dirfstat(bfd);
if(d == nil){
close(bfd);
return 0;
}
length = d->length;
free(d);
Binit(&b, bfd, OREAD);
ok = saveMsg(vs, m->info[IDigest], m->flags, head, nhead, &b, length);
Bterm(&b);
close(bfd);
return ok;
}
/*
* first spool the input into a temorary file,
* and massage the input in the process.
* then save to real box.
*/
int
appendSave(char *mbox, int flags, char *head, Biobuf *b, long n)
{
Biobuf btmp;
int fd, ok;
fd = imapTmp();
if(fd < 0)
return 0;
Bprint(&bout, "+ Ready for literal data\r\n");
if(Bflush(&bout) < 0)
writeErr();
Binit(&btmp, fd, OWRITE);
n = appSpool(&btmp, b, n);
Bterm(&btmp);
if(n < 0){
close(fd);
return 0;
}
seek(fd, 0, 0);
Binit(&btmp, fd, OREAD);
ok = saveMsg(mbox, nil, flags, head, strlen(head), &btmp, n);
Bterm(&btmp);
close(fd);
return ok;
}
/*
* copy from bin to bout,
* mapping "\r\n" to "\n" and "\nFrom " to "\n From "
* return the number of bytes in the mapped file.
*
* exactly n bytes must be read from the input,
* unless an input error occurs.
*/
static long
appSpool(Biobuf *bout, Biobuf *bin, long n)
{
int i, c;
c = '\n';
while(n > 0){
if(c == '\n' && n >= STRLEN("From ")){
for(i = 0; i < STRLEN("From "); i++){
c = Bgetc(bin);
if(c != "From "[i]){
if(c < 0)
return -1;
Bungetc(bin);
break;
}
n--;
}
if(i == STRLEN("From "))
Bputc(bout, ' ');
Bwrite(bout, "From ", i);
}
c = Bgetc(bin);
n--;
if(c == '\r' && n-- > 0){
c = Bgetc(bin);
if(c != '\n')
Bputc(bout, '\r');
}
if(c < 0)
return -1;
if(Bputc(bout, c) < 0)
return -1;
}
if(c != '\n')
Bputc(bout, '\n');
if(Bflush(bout) < 0)
return -1;
return Boffset(bout);
}
static int
saveMsg(char *dst, char *digest, int flags, char *head, int nhead, Biobuf *b, long n)
{
DigestState *dstate;
MbLock *ml;
uchar shadig[SHA1dlen];
char buf[BufSize + 1], digbuf[NDigest + 1];
int i, fd, nr, nw, ok;
ml = mbLock();
if(ml == nil)
return 0;
fd = openLocked(mboxDir, dst, OWRITE);
if(fd < 0){
mbUnlock(ml);
return 0;
}
seek(fd, 0, 2);
dstate = nil;
if(digest == nil)
dstate = sha1(nil, 0, nil, nil);
if(!saveb(fd, dstate, head, nhead, nhead)){
if(dstate != nil)
sha1(nil, 0, shadig, dstate);
mbUnlock(ml);
close(fd);
return 0;
}
ok = 1;
if(n == 0)
ok = saveb(fd, dstate, "\n", 0, 1);
while(n > 0){
nr = n;
if(nr > BufSize)
nr = BufSize;
nr = Bread(b, buf, nr);
if(nr <= 0){
saveb(fd, dstate, "\n\n", 0, 2);
ok = 0;
break;
}
n -= nr;
nw = nr;
if(n == 0){
if(buf[nw - 1] != '\n')
buf[nw++] = '\n';
buf[nw++] = '\n';
}
if(!saveb(fd, dstate, buf, nr, nw)){
ok = 0;
break;
}
mbLockRefresh(ml);
}
close(fd);
if(dstate != nil){
digest = digbuf;
sha1(nil, 0, shadig, dstate);
for(i = 0; i < SHA1dlen; i++)
snprint(digest+2*i, NDigest+1-2*i, "%2.2ux", shadig[i]);
}
if(ok){
fd = cdOpen(mboxDir, impName(dst), OWRITE);
if(fd < 0)
fd = emptyImp(dst);
if(fd >= 0){
seek(fd, 0, 2);
wrImpFlags(buf, flags, 1);
fprint(fd, "%.*s %.*lud %s\n", NDigest, digest, NUid, 0UL, buf);
close(fd);
}
}
mbUnlock(ml);
return 1;
}
static int
saveb(int fd, DigestState *dstate, char *buf, int nr, int nw)
{
if(dstate != nil)
sha1((uchar*)buf, nr, nil, dstate);
if(write(fd, buf, nw) != nw)
return 0;
return 1;
}
|