/
ezmlm-return.c
475 lines (424 loc) · 14.1 KB
/
ezmlm-return.c
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include "direntry.h"
#include "stralloc.h"
#include "str.h"
#include "env.h"
#include "sender.h"
#include "sig.h"
#include "strerr.h"
#include "byte.h"
#include "case.h"
#include "open.h"
#include "scan.h"
#include "lock.h"
#include "slurpclose.h"
#include "getln.h"
#include "substdio.h"
#include "error.h"
#include "quote.h"
#include "readwrite.h"
#include "fmt.h"
#include "now.h"
#include "cookie.h"
#include "getconfopt.h"
#include "subdb.h"
#include "messages.h"
#include "die.h"
#include "wrap.h"
#include "config.h"
#include "idx.h"
#include "auto_version.h"
const char FATAL[] = "ezmlm-return: fatal: ";
const char INFO[] = "ezmlm-return: info: ";
const char USAGE[] =
"ezmlm-return: usage: ezmlm-return [-dD] dir";
static int flagdig = 0;
static struct option options[] = {
OPT_FLAG(flagdig,'d',1,0),
OPT_FLAG(flagdig,'D',0,0),
OPT_END
};
void die_badretaddr(void)
{
strerr_die2x(100,FATAL,MSG(ERR_BAD_RETURN_ADDRESS));
}
void die_trash(void)
{
strerr_die2x(99,INFO,"trash address");
}
char outbuf[1024];
substdio ssout;
char inbuf[1024];
substdio ssin;
char strnum[FMT_ULONG];
char hash[COOKIE];
char hashcopy[COOKIE];
char *hashp = (char *) 0;
unsigned long cookiedate;
unsigned long addrno = 0L;
unsigned long addrno1 = 0L;
stralloc fndir = {0};
stralloc fndate = {0};
stralloc fndatenew = {0};
stralloc fnhash = {0};
stralloc fnhashnew = {0};
stralloc quoted = {0};
const char *sender;
const char *dir;
const char *workdir;
void die_hashnew(void)
{ strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,fnhashnew.s)); }
void die_datenew(void)
{ strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,fndatenew.s)); }
void die_msgin(void)
{ strerr_die2sys(111,FATAL,MSG(ERR_READ_INPUT)); }
void makedir(const char *s)
{
if (mkdir(s,0755) == -1)
if (errno != error_exist)
strerr_die2sys(111,FATAL,MSG1(ERR_CREATE,s));
}
void dowit(const char *addr,unsigned long when,const stralloc *bounce)
{
int fd;
unsigned int wpos;
unsigned long wdir,wfile;
if (!issub(workdir,addr,0)) return;
if (!stralloc_copys(&fndate,workdir)) die_nomem();
if (!stralloc_cats(&fndate,"/bounce/d")) die_nomem();
if (!stralloc_0(&fndate)) die_nomem();
fndate.s[fndate.len - 1] = '/'; /* replace '\0' */
wpos = fndate.len - 1;
wdir = when / 10000;
wfile = when - 10000 * wdir;
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,wdir))) die_nomem();
if (!stralloc_0(&fndate)) die_nomem();
makedir(fndate.s);
--fndate.len; /* remove terminal '\0' */
if (!stralloc_cats(&fndate,"/w")) die_nomem();
wpos = fndate.len - 1;
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,wfile))) die_nomem();
if (!stralloc_cats(&fndate,".")) die_nomem();
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,(unsigned long) getpid())))
die_nomem();
if (!stralloc_0(&fndate)) die_nomem();
if (!stralloc_copy(&fndatenew,&fndate)) die_nomem();
fndatenew.s[wpos] = 'W';
fd = open_trunc(fndatenew.s);
if (fd == -1) die_datenew();
substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
if (substdio_puts(&ssout,addr) == -1) die_datenew();
if (substdio_put(&ssout,"",1) == -1) die_datenew();
if (substdio_puts(&ssout,"Return-Path: <") == -1) die_datenew();
if (!quote2("ed,sender)) die_nomem();
if (substdio_put(&ssout,quoted.s,quoted.len) == -1) die_datenew();
if (substdio_puts(&ssout,">\n") == -1) die_datenew();
if (substdio_put(&ssout,bounce->s,bounce->len) == -1) die_datenew();
if (substdio_flush(&ssout) == -1) die_datenew();
if (fsync(fd) == -1) die_datenew();
if (close(fd) == -1) die_datenew(); /* NFS stupidity */
wrap_rename(fndatenew.s,fndate.s);
}
void doit(const char *addr,unsigned long msgnum,unsigned long when,
const stralloc *bounce)
{
int fd;
int fdnew;
unsigned int pos;
unsigned long ddir,dfile;
if (!issub(workdir,addr,0)) return;
if (!stralloc_copys(&fndate,workdir)) die_nomem();
if (!stralloc_cats(&fndate,"/bounce/d")) die_nomem();
if (!stralloc_0(&fndate)) die_nomem();
makedir(fndate.s);
fndate.s[fndate.len-1] = '/'; /* replace terminal '\0' */
ddir = when / 10000;
dfile = when - 10000 * ddir;
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,ddir))) die_nomem();
if (!stralloc_copy(&fndir,&fndate)) die_nomem();
if (!stralloc_0(&fndir)) die_nomem(); /* make later if necessary (new addr)*/
if (!stralloc_cats(&fndate,"/d")) die_nomem();
pos = fndate.len - 2;
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,dfile))) die_nomem();
if (!stralloc_cats(&fndate,".")) die_nomem();
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,(unsigned long) getpid())))
die_nomem();
if (addrno) { /* so that pre-VERP bounces make a d... file per address */
/* for the first one we use the std-style fname */
if (!stralloc_cats(&fndate,".")) die_nomem();
if (!stralloc_catb(&fndate,strnum,fmt_ulong(strnum,addrno))) die_nomem();
}
addrno++; /* get ready for next */
if (!stralloc_0(&fndate)) die_nomem();
if (!stralloc_copy(&fndatenew,&fndate)) die_nomem();
fndatenew.s[pos] = '_'; /* fndate = bounce/d/nnnn/dmmmmmm */
/* fndatenew = bounce/d/nnnn_dmmmmmm */
fd = open_trunc(fndatenew.s);
if (fd == -1) die_datenew();
substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
if (substdio_puts(&ssout,addr) == -1) die_datenew();
if (substdio_put(&ssout,"",1) == -1) die_datenew();
if (substdio_puts(&ssout,"Return-Path: <") == -1) die_datenew();
if (!quote2("ed,sender)) die_nomem();
if (substdio_put(&ssout,quoted.s,quoted.len) == -1) die_datenew();
if (substdio_puts(&ssout,">\n") == -1) die_datenew();
if (substdio_put(&ssout,bounce->s,bounce->len) == -1) die_datenew();
if (substdio_flush(&ssout) == -1) die_datenew();
if (fsync(fd) == -1) die_datenew();
if (close(fd) == -1) die_datenew(); /* NFS stupidity */
cookie(hash,"",0,"",addr,"");
if (!stralloc_copys(&fnhash,workdir)) die_nomem();
if (!stralloc_cats(&fnhash,"/bounce/h")) die_nomem();
if (!stralloc_0(&fnhash)) die_nomem();
makedir(fnhash.s);
fnhash.s[fnhash.len - 1] = '/'; /* replace terminal '\0' */
if (!stralloc_catb(&fnhash,hash,1)) die_nomem();
if (!stralloc_0(&fnhash)) die_nomem();
makedir(fnhash.s);
--fnhash.len; /* remove terminal '\0' */
if (!stralloc_cats(&fnhash,"/h")) die_nomem();
pos = fnhash.len - 1;
if (!stralloc_catb(&fnhash,hash+1,COOKIE-1)) die_nomem();
if (!stralloc_0(&fnhash)) die_nomem();
if (!stralloc_copy(&fnhashnew,&fnhash)) die_nomem();
fnhashnew.s[pos] = 'H';
fdnew = open_trunc(fnhashnew.s);
if (fdnew == -1) die_hashnew();
substdio_fdbuf(&ssout,write,fdnew,outbuf,sizeof(outbuf));
fd = open_read(fnhash.s);
if (fd == -1) {
if (errno != error_noent)
strerr_die2sys(111,FATAL,MSG1(ERR_READ,fnhash.s));
makedir(fndir.s);
wrap_rename(fndatenew.s,fndate.s);
}
else {
substdio_fdbuf(&ssin,read,fd,inbuf,sizeof(inbuf));
switch(substdio_copy(&ssout,&ssin)) {
case -2: die_msgin();
case -3: die_hashnew();
}
close(fd);
if (unlink(fndatenew.s) == -1)
strerr_die2sys(111,FATAL,MSG1(ERR_DELETE,fndatenew.s));
}
if (substdio_puts(&ssout," ") == -1) die_hashnew();
if (substdio_put(&ssout,strnum,fmt_ulong(strnum,msgnum)) == -1) die_hashnew();
if (substdio_puts(&ssout,"\n") == -1) die_hashnew();
if (substdio_flush(&ssout) == -1) die_hashnew();
if (fsync(fdnew) == -1) die_hashnew();
if (close(fdnew) == -1) die_hashnew(); /* NFS stupidity */
wrap_rename(fnhashnew.s,fnhash.s);
}
stralloc bounce = {0};
stralloc line = {0};
stralloc header = {0};
stralloc intro = {0};
stralloc failure = {0};
stralloc paragraph = {0};
int flagmasterbounce = 0;
int flaghaveheader;
int flaghaveintro;
char msginbuf[1024];
substdio ssmsgin;
int main(int argc,char **argv)
{
char *action;
const char *ret;
char *cp;
unsigned long msgnum;
unsigned long cookiedate;
unsigned long when;
unsigned long listno = 0L;
int match;
unsigned int i;
int flagmaster = 0;
int flagreceipt = 0;
int fdlock;
char ch;
umask(022);
sig_pipeignore();
when = (unsigned long) now();
getconfopt(argc,argv,options,1,0);
sender = get_sender();
if (!sender) die_sender();
action = env_get("DEFAULT");
if (!action) strerr_die2x(100,FATAL,MSG(ERR_NODEFAULT));
initsub(0);
if (str_start(action,"receipt-")) {
flagreceipt = 1;
action += 8;
}
ch = *action; /* -d -digest, -m -master, -g -getmaster */
if (ch && action[1] == '-') {
switch (ch) {
case 'g': flagmaster = 1; flagdig = 1; action += 2; break;
case 'm': flagmaster = 1; action += 2; break;
default: break;
}
}
workdir = flagdig ? "digest" : ".";
if (!*action) die_trash();
if (flagreceipt || flagmaster) /* check cookie */
if (str_chr(action,'-') == COOKIE) {
action[COOKIE] = '\0';
hashp = action;
action += COOKIE + 1;
}
if (!flagreceipt) {
if (!flagmaster && str_start(action,"probe-")) {
action += 6;
action += scan_ulong(action,&cookiedate);
if (now() - cookiedate > 3000000) die_trash();
if (*action++ != '.') die_trash();
i = str_chr(action,'-');
if (i != COOKIE) die_trash();
byte_copy(hashcopy,COOKIE,action);
action += COOKIE;
if (*action++ != '-') die_trash();
i = str_rchr(action,'=');
if (!stralloc_copyb(&line,action,i)) die_nomem();
if (action[i]) {
if (!stralloc_cats(&line,"@")) die_nomem();
if (!stralloc_cats(&line,action + i + 1)) die_nomem();
}
if (!stralloc_0(&line)) die_nomem();
strnum[fmt_ulong(strnum,cookiedate)] = 0;
cookie(hash,key.s,key.len,strnum,line.s,"P");
if (byte_diff(hash,COOKIE,hashcopy)) die_trash();
(void) subscribe(workdir,line.s,0,"","-probe",-1);
_exit(99);
}
if (!stralloc_copys(&line,workdir)) die_nomem();
if (!stralloc_cats(&line,"/lockbounce")) die_nomem();
if (!stralloc_0(&line)) die_nomem();
fdlock = lockfile(line.s);
if (!flagmaster && str_start(action,"warn-")) {
action += 5;
action += scan_ulong(action,&cookiedate);
if (now() - cookiedate > 3000000) die_trash();
if (*action++ != '.') die_trash();
i = str_chr(action,'-');
if (i != COOKIE) die_trash();
byte_copy(hashcopy,COOKIE,action);
action += COOKIE;
if (*action++ != '-') die_trash();
i = str_rchr(action,'=');
if (!stralloc_copyb(&line,action,i)) die_nomem();
if (action[i]) {
if (!stralloc_cats(&line,"@")) die_nomem();
if (!stralloc_cats(&line,action + i + 1)) die_nomem();
}
if (!stralloc_0(&line)) die_nomem();
strnum[fmt_ulong(strnum,cookiedate)] = 0;
cookie(hash,key.s,key.len,strnum,line.s,"W");
if (byte_diff(hash,COOKIE,hashcopy)) die_trash();
if (slurpclose(0,&bounce,1024) == -1) die_msgin();
dowit(line.s,when,&bounce);
_exit(99);
}
}
action += scan_ulong(action,&msgnum);
if (*action++ != '-') die_badretaddr();
cp = action;
if (*action >= '0' && *action <= '9') { /* listno */
action += scan_ulong(action,&listno);
listno++; /* logging is 1-53, not 0-52 */
}
if (hashp) { /* scrap bad cookies */
if ((ret = checktag(msgnum,0L,"x",(char *) 0,hashp))) {
if (*ret)
strerr_die2x(111,FATAL,ret);
else
die_trash();
} else if (flagreceipt) {
if (!(ret = logmsg(msgnum,listno,0L,5))) {
closesub();
strerr_die6x(99,INFO,"receipt:",cp," [",hashp,"]");
}
if (*ret) strerr_die2x(111,FATAL,ret);
else strerr_die2x(0,INFO,MSG(ERR_DONE));
} else if (*action) { /* post VERP master bounce */
if ((ret = logmsg(msgnum,listno,0L,-1))) {
closesub();
strerr_die4x(0,INFO,"bounce [",hashp,"]");
}
if (*ret) strerr_die2x(111,FATAL,ret);
else strerr_die2x(99,INFO,MSG(ERR_DONE));
}
} else if (flagreceipt || flagmaster)
die_badretaddr();
if (*action) {
i = str_rchr(action,'=');
if (!stralloc_copyb(&line,action,i)) die_nomem();
if (action[i]) {
if (!stralloc_cats(&line,"@")) die_nomem();
if (!stralloc_cats(&line,action + i + 1)) die_nomem();
}
if (!stralloc_0(&line)) die_nomem();
if (slurpclose(0,&bounce,1024) == -1) die_msgin();
doit(line.s,msgnum,when,&bounce);
_exit(99);
}
/* pre-VERP bounce, in QSBMF format. Receipts are never pre-VERP */
substdio_fdbuf(&ssmsgin,read,0,msginbuf,sizeof(msginbuf));
flaghaveheader = 0;
flaghaveintro = 0;
for (;;) {
if (!stralloc_copys(¶graph,"")) die_nomem();
for (;;) {
if (getln(&ssmsgin,&line,&match,'\n') == -1) die_msgin();
if (!match) die_trash();
if (!stralloc_cat(¶graph,&line)) die_nomem();
if (line.len <= 1) break;
}
if (!flaghaveheader) {
if (!stralloc_copy(&header,¶graph)) die_nomem();
flaghaveheader = 1;
continue;
}
if (!flaghaveintro) {
if (paragraph.s[0] == '-' && paragraph.s[1] == '-')
continue; /* skip MIME boundary if it exists */
if (paragraph.len < 15) die_trash();
if (str_diffn(paragraph.s,"Hi. This is the",15)) die_trash();
if (!stralloc_copy(&intro,¶graph)) die_nomem();
flaghaveintro = 1;
continue;
}
if (paragraph.s[0] == '-')
break;
if (paragraph.s[0] == '<') {
if (!stralloc_copy(&failure,¶graph)) die_nomem();
if (!stralloc_copy(&bounce,&header)) die_nomem();
if (!stralloc_cat(&bounce,&intro)) die_nomem();
if (!stralloc_cat(&bounce,&failure)) die_nomem();
i = byte_chr(failure.s,failure.len,'\n');
if (i < 3) die_trash();
if (!stralloc_copyb(&line,failure.s + 1,i - 3)) die_nomem();
if (byte_chr(line.s,line.len,'\0') == line.len) {
if (!stralloc_0(&line)) die_nomem();
if (flagmaster) { /* bounced msg slave! */
if ((i = str_rchr(line.s,'@')) >= 5) { /* 00_52@host */
line.s[i] = '\0'; /* 00_52 */
(void) scan_ulong(line.s + i - 5,&listno);
if ((ret = logmsg(msgnum,listno + 1,0L,-1)) && *ret)
strerr_die2x(111,FATAL,ret);
strerr_warn3(INFO,"bounce ",line.s + i - 5,(struct strerr *) 0);
flagmasterbounce = 1;
}
} else
doit(line.s,msgnum,when,&bounce);
}
}
}
closesub();
if (flagmasterbounce)
strerr_die3x(0,"[",hashp,"]");
else
_exit(99);
(void)argc;
}