/
qmail-send.c
1612 lines (1411 loc) · 38.2 KB
/
qmail-send.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sys/types.h>
#include <sys/stat.h>
#include "readwrite.h"
#include "sig.h"
#include "direntry.h"
#include "control.h"
#include "select.h"
#include "open.h"
#include "seek.h"
#include "exit.h"
#include "lock.h"
#include "ndelay.h"
#include "now.h"
#include "getln.h"
#include "substdio.h"
#include "alloc.h"
#include "error.h"
#include "stralloc.h"
#include "str.h"
#include "byte.h"
#include "fmt.h"
#include "scan.h"
#include "case.h"
#include "auto_qmail.h"
#include "trigger.h"
#include "newfield.h"
#include "quote.h"
#include "qmail.h"
#include "qsutil.h"
#include "prioq.h"
#include "constmap.h"
#include "fmtqfn.h"
#include "readsubdir.h"
/* critical timing feature #1: if not triggered, do not busy-loop */
/* critical timing feature #2: if triggered, respond within fixed time */
/* important timing feature: when triggered, respond instantly */
#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
#define SLEEP_FOREVER 86400 /* absolute maximum time spent in select() */
#define SLEEP_CLEANUP 76431 /* time between cleanups */
#define SLEEP_SYSFAIL 123
#define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */
int lifetime = 604800;
stralloc percenthack = {0};
struct constmap mappercenthack;
stralloc locals = {0};
struct constmap maplocals;
stralloc vdoms = {0};
struct constmap mapvdoms;
stralloc envnoathost = {0};
stralloc bouncefrom = {0};
stralloc bouncehost = {0};
stralloc doublebounceto = {0};
stralloc doublebouncehost = {0};
char strnum2[FMT_ULONG];
char strnum3[FMT_ULONG];
#define CHANNELS 2
char *chanaddr[CHANNELS] = { "local/", "remote/" };
char *chanstatusmsg[CHANNELS] = { " local ", " remote " };
char *tochan[CHANNELS] = { " to local ", " to remote " };
int chanfdout[CHANNELS] = { 1, 3 };
int chanfdin[CHANNELS] = { 2, 4 };
int chanskip[CHANNELS] = { 10, 20 };
int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
int flagreadasap = 0; void sighup() { flagreadasap = 1; }
void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
flagexitasap = 1; }
int flagspawnalive[CHANNELS];
void spawndied(c) int c; { log1("alert: oh no! lost spawn connection! dying...\n");
flagspawnalive[c] = 0; flagexitasap = 1; }
#define REPORTMAX 10000
datetime_sec recent;
/* this file is too long ----------------------------------------- FILENAMES */
stralloc fn = {0};
stralloc fn2 = {0};
char fnmake_strnum[FMT_ULONG];
void fnmake_init()
{
while (!stralloc_ready(&fn,FMTQFN)) nomem();
while (!stralloc_ready(&fn2,FMTQFN)) nomem();
}
void fnmake_info(id) unsigned long id; { fn.len = fmtqfn(fn.s,"info/",id,1); }
void fnmake_todo(id) unsigned long id; { fn.len = fmtqfn(fn.s,"todo/",id,0); }
void fnmake_mess(id) unsigned long id; { fn.len = fmtqfn(fn.s,"mess/",id,1); }
void fnmake_foop(id) unsigned long id; { fn.len = fmtqfn(fn.s,"foop/",id,0); }
void fnmake_split(id) unsigned long id; { fn.len = fmtqfn(fn.s,"",id,1); }
void fnmake2_bounce(id) unsigned long id;
{ fn2.len = fmtqfn(fn2.s,"bounce/",id,0); }
void fnmake_chanaddr(id,c) unsigned long id; int c;
{ fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
/* this file is too long ----------------------------------------- REWRITING */
stralloc rwline = {0};
/* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
/* may trash recip. must set up rwline, between a T and a \0. */
int rewrite(recip)
char *recip;
{
int i;
int j;
char *x;
static stralloc addr = {0};
int at;
if (!stralloc_copys(&rwline,"T")) return 0;
if (!stralloc_copys(&addr,recip)) return 0;
i = byte_rchr(addr.s,addr.len,'@');
if (i == addr.len) {
if (!stralloc_cats(&addr,"@")) return 0;
if (!stralloc_cat(&addr,&envnoathost)) return 0;
}
while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
j = byte_rchr(addr.s,i,'%');
if (j == i) break;
addr.len = i;
i = j;
addr.s[i] = '@';
}
at = byte_rchr(addr.s,addr.len,'@');
if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) {
if (!stralloc_cat(&rwline,&addr)) return 0;
if (!stralloc_0(&rwline)) return 0;
return 1;
}
for (i = 0;i <= addr.len;++i)
if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.')))
if (x = constmap(&mapvdoms,addr.s + i,addr.len - i)) {
if (!*x) break;
if (!stralloc_cats(&rwline,x)) return 0;
if (!stralloc_cats(&rwline,"-")) return 0;
if (!stralloc_cat(&rwline,&addr)) return 0;
if (!stralloc_0(&rwline)) return 0;
return 1;
}
if (!stralloc_cat(&rwline,&addr)) return 0;
if (!stralloc_0(&rwline)) return 0;
return 2;
}
void senderadd(sa,sender,recip)
stralloc *sa;
char *sender;
char *recip;
{
int i;
int j;
int k;
i = str_len(sender);
if (i >= 4)
if (str_equal(sender + i - 4,"-@[]"))
{
j = byte_rchr(sender,i - 4,'@');
k = str_rchr(recip,'@');
if (recip[k] && (j + 5 <= i))
{
/* owner-@host-@[] -> owner-recipbox=reciphost@host */
while (!stralloc_catb(sa,sender,j)) nomem();
while (!stralloc_catb(sa,recip,k)) nomem();
while (!stralloc_cats(sa,"=")) nomem();
while (!stralloc_cats(sa,recip + k + 1)) nomem();
while (!stralloc_cats(sa,"@")) nomem();
while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem();
return;
}
}
while (!stralloc_cats(sa,sender)) nomem();
}
/* this file is too long ---------------------------------------------- INFO */
int getinfo(sa,dt,id)
stralloc *sa;
datetime_sec *dt;
unsigned long id;
{
int fdinfo;
struct stat st;
static stralloc line = {0};
int match;
substdio ss;
char buf[128];
fnmake_info(id);
fdinfo = open_read(fn.s);
if (fdinfo == -1) return 0;
if (fstat(fdinfo,&st) == -1) { close(fdinfo); return 0; }
substdio_fdbuf(&ss,read,fdinfo,buf,sizeof(buf));
if (getln(&ss,&line,&match,'\0') == -1) { close(fdinfo); return 0; }
close(fdinfo);
if (!match) return 0;
if (line.s[0] != 'F') return 0;
*dt = st.st_mtime;
while (!stralloc_copys(sa,line.s + 1)) nomem();
while (!stralloc_0(sa)) nomem();
return 1;
}
/* this file is too long ------------------------------------- COMMUNICATION */
substdio sstoqc; char sstoqcbuf[1024];
substdio ssfromqc; char ssfromqcbuf[1024];
stralloc comm_buf[CHANNELS] = { {0}, {0} };
int comm_pos[CHANNELS];
void comm_init()
{
int c;
substdio_fdbuf(&sstoqc,write,5,sstoqcbuf,sizeof(sstoqcbuf));
substdio_fdbuf(&ssfromqc,read,6,ssfromqcbuf,sizeof(ssfromqcbuf));
for (c = 0;c < CHANNELS;++c)
if (ndelay_on(chanfdout[c]) == -1)
/* this is so stupid: NDELAY semantics should be default on write */
spawndied(c); /* drastic, but better than risking deadlock */
}
int comm_canwrite(c)
int c;
{
/* XXX: could allow a bigger buffer; say 10 recipients */
if (comm_buf[c].s && comm_buf[c].len) return 0;
return 1;
}
void comm_write(c,delnum,id,sender,recip)
int c;
int delnum;
unsigned long id;
char *sender;
char *recip;
{
char ch;
if (comm_buf[c].s && comm_buf[c].len) return;
while (!stralloc_copys(&comm_buf[c],"")) nomem();
ch = delnum;
while (!stralloc_append(&comm_buf[c],&ch)) nomem();
fnmake_split(id);
while (!stralloc_cats(&comm_buf[c],fn.s)) nomem();
while (!stralloc_0(&comm_buf[c])) nomem();
senderadd(&comm_buf[c],sender,recip);
while (!stralloc_0(&comm_buf[c])) nomem();
while (!stralloc_cats(&comm_buf[c],recip)) nomem();
while (!stralloc_0(&comm_buf[c])) nomem();
comm_pos[c] = 0;
}
void comm_selprep(nfds,wfds)
int *nfds;
fd_set *wfds;
{
int c;
for (c = 0;c < CHANNELS;++c)
if (flagspawnalive[c])
if (comm_buf[c].s && comm_buf[c].len)
{
FD_SET(chanfdout[c],wfds);
if (*nfds <= chanfdout[c])
*nfds = chanfdout[c] + 1;
}
}
void comm_do(wfds)
fd_set *wfds;
{
int c;
for (c = 0;c < CHANNELS;++c)
if (flagspawnalive[c])
if (comm_buf[c].s && comm_buf[c].len)
if (FD_ISSET(chanfdout[c],wfds))
{
int w;
int len;
len = comm_buf[c].len;
w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
if (w <= 0)
{
if ((w == -1) && (errno == error_pipe))
spawndied(c);
else
continue; /* kernel select() bug; can't avoid busy-looping */
}
else
{
comm_pos[c] += w;
if (comm_pos[c] == len)
comm_buf[c].len = 0;
}
}
}
/* this file is too long ------------------------------------------ CLEANUPS */
int flagcleanup; /* if 1, cleanupdir is initialized and ready */
readsubdir cleanupdir;
datetime_sec cleanuptime;
void cleanup_init()
{
flagcleanup = 0;
cleanuptime = now();
}
void cleanup_selprep(wakeup)
datetime_sec *wakeup;
{
if (flagcleanup) *wakeup = 0;
if (*wakeup > cleanuptime) *wakeup = cleanuptime;
}
void cleanup_do()
{
char ch;
struct stat st;
unsigned long id;
if (!flagcleanup)
{
if (recent < cleanuptime) return;
readsubdir_init(&cleanupdir,"mess",pausedir);
flagcleanup = 1;
}
switch(readsubdir_next(&cleanupdir,&id))
{
case 1:
break;
case 0:
flagcleanup = 0;
cleanuptime = recent + SLEEP_CLEANUP;
default:
return;
}
fnmake_mess(id);
if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
if (recent <= st.st_atime + OSSIFIED) return;
fnmake_info(id);
if (stat(fn.s,&st) == 0) return;
if (errno != error_noent) return;
fnmake_todo(id);
if (stat(fn.s,&st) == 0) return;
if (errno != error_noent) return;
fnmake_foop(id);
if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
if (ch != '+')
log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
}
/* this file is too long ----------------------------------- PRIORITY QUEUES */
prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */
prioq pqchan[CHANNELS] = { {0}, {0} };
/* pqchan 0: -todo +info +local ?remote */
/* pqchan 1: -todo +info ?local +remote */
prioq pqfail = {0}; /* stat() failure; has to be pqadded again */
void pqadd(id)
unsigned long id;
{
struct prioq_elt pe;
struct prioq_elt pechan[CHANNELS];
int flagchan[CHANNELS];
struct stat st;
int c;
#define CHECKSTAT if (errno != error_noent) goto fail;
fnmake_info(id);
if (stat(fn.s,&st) == -1)
{
CHECKSTAT
return; /* someone yanking our chain */
}
fnmake_todo(id);
if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
CHECKSTAT
for (c = 0;c < CHANNELS;++c)
{
fnmake_chanaddr(id,c);
if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT }
else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; }
}
for (c = 0;c < CHANNELS;++c)
if (flagchan[c])
while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
if (c == CHANNELS)
{
pe.id = id; pe.dt = now();
while (!prioq_insert(&pqdone,&pe)) nomem();
}
return;
fail:
log3("warning: unable to stat ",fn.s,"; will try again later\n");
pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
while (!prioq_insert(&pqfail,&pe)) nomem();
}
void pqstart()
{
readsubdir rs;
int x;
unsigned long id;
readsubdir_init(&rs,"info",pausedir);
while (x = readsubdir_next(&rs,&id))
if (x > 0)
pqadd(id);
}
void pqfinish()
{
int c;
struct prioq_elt pe;
time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */
for (c = 0;c < CHANNELS;++c)
while (prioq_min(&pqchan[c],&pe))
{
prioq_delmin(&pqchan[c]);
fnmake_chanaddr(pe.id,c);
ut[0] = ut[1] = pe.dt;
if (utime(fn.s,ut) == -1)
log3("warning: unable to utime ",fn.s,"; message will be retried too soon\n");
}
}
void pqrun()
{
int c;
int i;
for (c = 0;c < CHANNELS;++c)
if (pqchan[c].p)
if (pqchan[c].len)
for (i = 0;i < pqchan[c].len;++i)
pqchan[c].p[i].dt = recent;
}
/* this file is too long ---------------------------------------------- JOBS */
struct job
{
int refs; /* if 0, this struct is unused */
unsigned long id;
int channel;
datetime_sec retry;
stralloc sender;
int numtodo;
int flaghiteof;
int flagdying;
}
;
int numjobs;
struct job *jo;
void job_init()
{
int j;
while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
for (j = 0;j < numjobs;++j)
{
jo[j].refs = 0;
jo[j].sender.s = 0;
}
}
int job_avail()
{
int j;
for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
return 0;
}
int job_open(id,channel)
unsigned long id;
int channel;
{
int j;
for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
if (j == numjobs) return -1;
jo[j].refs = 1;
jo[j].id = id;
jo[j].channel = channel;
jo[j].numtodo = 0;
jo[j].flaghiteof = 0;
return j;
}
void job_close(j)
int j;
{
struct prioq_elt pe;
struct stat st;
if (0 < --jo[j].refs) return;
pe.id = jo[j].id;
pe.dt = jo[j].retry;
if (jo[j].flaghiteof && !jo[j].numtodo)
{
fnmake_chanaddr(jo[j].id,jo[j].channel);
if (unlink(fn.s) == -1)
{
log3("warning: unable to unlink ",fn.s,"; will try again later\n");
pe.dt = now() + SLEEP_SYSFAIL;
}
else
{
int c;
for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
{
fnmake_chanaddr(jo[j].id,c);
if (stat(fn.s,&st) == 0) return; /* more channels going */
if (errno != error_noent)
{
log3("warning: unable to stat ",fn.s,"\n");
break; /* this is the only reason for HOPEFULLY */
}
}
pe.dt = now();
while (!prioq_insert(&pqdone,&pe)) nomem();
return;
}
}
while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
}
/* this file is too long ------------------------------------------- BOUNCES */
char *stripvdomprepend(recip)
char *recip;
{
int i;
char *domain;
int domainlen;
char *prepend;
i = str_rchr(recip,'@');
if (!recip[i]) return recip;
domain = recip + i + 1;
domainlen = str_len(domain);
for (i = 0;i <= domainlen;++i)
if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
if (prepend = constmap(&mapvdoms,domain + i,domainlen - i))
{
if (!*prepend) break;
i = str_len(prepend);
if (str_diffn(recip,prepend,i)) break;
if (recip[i] != '-') break;
return recip + i + 1;
}
return recip;
}
stralloc bouncetext = {0};
void addbounce(id,recip,report)
unsigned long id;
char *recip;
char *report;
{
int fd;
int pos;
int w;
while (!stralloc_copys(&bouncetext,"<")) nomem();
while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem();
for (pos = 0;pos < bouncetext.len;++pos)
if (bouncetext.s[pos] == '\n')
bouncetext.s[pos] = '_';
while (!stralloc_cats(&bouncetext,">:\n")) nomem();
while (!stralloc_cats(&bouncetext,report)) nomem();
if (report[0])
if (report[str_len(report) - 1] != '\n')
while (!stralloc_cats(&bouncetext,"\n")) nomem();
for (pos = bouncetext.len - 2;pos > 0;--pos)
if (bouncetext.s[pos] == '\n')
if (bouncetext.s[pos - 1] == '\n')
bouncetext.s[pos] = '/';
while (!stralloc_cats(&bouncetext,"\n")) nomem();
fnmake2_bounce(id);
for (;;)
{
fd = open_append(fn2.s);
if (fd != -1) break;
log1("alert: unable to append to bounce message; HELP! sleeping...\n");
sleep(10);
}
pos = 0;
while (pos < bouncetext.len)
{
w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
if (w <= 0)
{
log1("alert: unable to append to bounce message; HELP! sleeping...\n");
sleep(10);
}
else
pos += w;
}
close(fd);
}
int injectbounce(id)
unsigned long id;
{
struct qmail qqt;
struct stat st;
char *bouncesender;
char *bouncerecip;
int r;
int fd;
substdio ssread;
char buf[128];
char inbuf[128];
static stralloc sender = {0};
static stralloc quoted = {0};
datetime_sec birth;
unsigned long qp;
if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
/* owner-@host-@[] -> owner-@host */
if (sender.len >= 5)
if (str_equal(sender.s + sender.len - 5,"-@[]"))
{
sender.len -= 4;
sender.s[sender.len - 1] = 0;
}
fnmake2_bounce(id);
fnmake_mess(id);
if (stat(fn2.s,&st) == -1)
{
if (errno == error_noent)
return 1;
log3("warning: unable to stat ",fn2.s,"\n");
return 0;
}
if (str_equal(sender.s,"#@[]"))
log3("triple bounce: discarding ",fn2.s,"\n");
else
{
if (qmail_open(&qqt) == -1)
{ log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
qp = qmail_qp(&qqt);
if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
while (!newfield_datemake(now())) nomem();
qmail_put(&qqt,newfield_date.s,newfield_date.len);
qmail_puts(&qqt,"From: ");
while (!quote("ed,&bouncefrom)) nomem();
qmail_put(&qqt,quoted.s,quoted.len);
qmail_puts(&qqt,"@");
qmail_put(&qqt,bouncehost.s,bouncehost.len);
qmail_puts(&qqt,"\nTo: ");
while (!quote2("ed,bouncerecip)) nomem();
qmail_put(&qqt,quoted.s,quoted.len);
qmail_puts(&qqt,"\n\
Subject: failure notice\n\
\n\
Hi. This is the qmail-send program at ");
qmail_put(&qqt,bouncehost.s,bouncehost.len);
qmail_puts(&qqt,*sender.s ? ".\n\
I'm afraid I wasn't able to deliver your message to the following addresses.\n\
This is a permanent error; I've given up. Sorry it didn't work out.\n\
\n\
" : ".\n\
I tried to deliver a bounce message to this address, but the bounce bounced!\n\
\n\
");
fd = open_read(fn2.s);
if (fd == -1)
qmail_fail(&qqt);
else
{
substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
qmail_put(&qqt,buf,r);
close(fd);
if (r == -1)
qmail_fail(&qqt);
}
qmail_puts(&qqt,*sender.s ? "--- Below this line is a copy of the message.\n\n" : "--- Below this line is the original bounce.\n\n");
qmail_puts(&qqt,"Return-Path: <");
while (!quote2("ed,sender.s)) nomem();
qmail_put(&qqt,quoted.s,quoted.len);
qmail_puts(&qqt,">\n");
fd = open_read(fn.s);
if (fd == -1)
qmail_fail(&qqt);
else
{
substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
qmail_put(&qqt,buf,r);
close(fd);
if (r == -1)
qmail_fail(&qqt);
}
qmail_from(&qqt,bouncesender);
qmail_to(&qqt,bouncerecip);
if (*qmail_close(&qqt))
{ log1("warning: trouble injecting bounce message, will try later\n"); return 0; }
strnum2[fmt_ulong(strnum2,id)] = 0;
log2("bounce msg ",strnum2);
strnum2[fmt_ulong(strnum2,qp)] = 0;
log3(" qp ",strnum2,"\n");
}
if (unlink(fn2.s) == -1)
{
log3("warning: unable to unlink ",fn2.s,"\n");
return 0;
}
return 1;
}
/* this file is too long ---------------------------------------- DELIVERIES */
struct del
{
int used;
int j;
unsigned long delid;
seek_pos mpos;
stralloc recip;
}
;
unsigned long masterdelid = 1;
unsigned int concurrency[CHANNELS] = { 10, 20 };
unsigned int concurrencyused[CHANNELS] = { 0, 0 };
struct del *d[CHANNELS];
stralloc dline[CHANNELS];
char delbuf[2048];
void del_status()
{
int c;
log1("status:");
for (c = 0;c < CHANNELS;++c) {
strnum2[fmt_ulong(strnum2,(unsigned long) concurrencyused[c])] = 0;
strnum3[fmt_ulong(strnum3,(unsigned long) concurrency[c])] = 0;
log2(chanstatusmsg[c],strnum2);
log2("/",strnum3);
}
if (flagexitasap) log1(" exitasap");
log1("\n");
}
void del_init()
{
int c;
int i;
for (c = 0;c < CHANNELS;++c)
{
flagspawnalive[c] = 1;
while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
nomem();
for (i = 0;i < concurrency[c];++i)
{ d[c][i].used = 0; d[c][i].recip.s = 0; }
dline[c].s = 0;
while (!stralloc_copys(&dline[c],"")) nomem();
}
del_status();
}
int del_canexit()
{
int c;
for (c = 0;c < CHANNELS;++c)
if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */
if (concurrencyused[c]) return 0;
return 1;
}
int del_avail(c)
int c;
{
return flagspawnalive[c] && comm_canwrite(c) && (concurrencyused[c] < concurrency[c]);
}
void del_start(j,mpos,recip)
int j;
seek_pos mpos;
char *recip;
{
int i;
int c;
c = jo[j].channel;
if (!flagspawnalive[c]) return;
if (!comm_canwrite(c)) return;
for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
if (i == concurrency[c]) return;
if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; }
if (!stralloc_0(&d[c][i].recip)) { nomem(); return; }
d[c][i].j = j; ++jo[j].refs;
d[c][i].delid = masterdelid++;
d[c][i].mpos = mpos;
d[c][i].used = 1; ++concurrencyused[c];
comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0;
strnum3[fmt_ulong(strnum3,jo[j].id)] = 0;
log2("starting delivery ",strnum2);
log3(": msg ",strnum3,tochan[c]);
logsafe(recip);
log1("\n");
del_status();
}
void markdone(c,id,pos)
int c;
unsigned long id;
seek_pos pos;
{
struct stat st;
int fd;
fnmake_chanaddr(id,c);
for (;;)
{
fd = open_write(fn.s);
if (fd == -1) break;
if (fstat(fd,&st) == -1) { close(fd); break; }
if (seek_set(fd,pos) == -1) { close(fd); break; }
if (write(fd,"D",1) != 1) { close(fd); break; }
/* further errors -> double delivery without us knowing about it, oh well */
close(fd);
return;
}
log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
}
void del_dochan(c)
int c;
{
int r;
char ch;
int i;
int delnum;
r = read(chanfdin[c],delbuf,sizeof(delbuf));
if (r == -1) return;
if (r == 0) { spawndied(c); return; }
for (i = 0;i < r;++i)
{
ch = delbuf[i];
while (!stralloc_append(&dline[c],&ch)) nomem();
if (dline[c].len > REPORTMAX)
dline[c].len = REPORTMAX;
/* qmail-lspawn and qmail-rspawn are responsible for keeping it short */
/* but from a security point of view, we don't trust rspawn */
if (!ch && (dline[c].len > 1))
{
delnum = (unsigned int) (unsigned char) dline[c].s[0];
if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used)
log1("warning: internal error: delivery report out of range\n");
else
{
strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0;
if (dline[c].s[1] == 'Z')
if (jo[d[c][delnum].j].flagdying)
{
dline[c].s[1] = 'D';
--dline[c].len;
while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem();
while (!stralloc_0(&dline[c])) nomem();
}
switch(dline[c].s[1])
{
case 'K':
log3("delivery ",strnum3,": success: ");
logsafe(dline[c].s + 2);
log1("\n");
markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
--jo[d[c][delnum].j].numtodo;
break;
case 'Z':
log3("delivery ",strnum3,": deferral: ");
logsafe(dline[c].s + 2);
log1("\n");
break;
case 'D':
log3("delivery ",strnum3,": failure: ");
logsafe(dline[c].s + 2);
log1("\n");
addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2);
markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
--jo[d[c][delnum].j].numtodo;
break;
default:
log3("delivery ",strnum3,": report mangled, will defer\n");
}
job_close(d[c][delnum].j);
d[c][delnum].used = 0; --concurrencyused[c];
del_status();
}
dline[c].len = 0;
}
}
}
void del_selprep(nfds,rfds)
int *nfds;
fd_set *rfds;
{
int c;
for (c = 0;c < CHANNELS;++c)
if (flagspawnalive[c])
{
FD_SET(chanfdin[c],rfds);
if (*nfds <= chanfdin[c])
*nfds = chanfdin[c] + 1;
}
}
void del_do(rfds)
fd_set *rfds;
{
int c;
for (c = 0;c < CHANNELS;++c)
if (flagspawnalive[c])
if (FD_ISSET(chanfdin[c],rfds))
del_dochan(c);
}
/* this file is too long -------------------------------------------- PASSES */
struct
{
unsigned long id; /* if 0, need a new pass */
int j; /* defined if id; job number */
int fd; /* defined if id; reading from {local,remote} */
seek_pos mpos; /* defined if id; mark position */
substdio ss;
char buf[128];
}
pass[CHANNELS];
void pass_init()
{
int c;
for (c = 0;c < CHANNELS;++c) pass[c].id = 0;