24 #include "auto_qmail.h"
33 #include "readsubdir.h"
35 /* critical timing feature #1: if not triggered, do not busy-loop */
36 /* critical timing feature #2: if triggered, respond within fixed time */
37 /* important timing feature: when triggered, respond instantly */
38 #define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
39 #define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
40 #define SLEEP_FOREVER 86400 /* absolute maximum time spent in select() */
41 #define SLEEP_CLEANUP 76431 /* time between cleanups */
42 #define SLEEP_SYSFAIL 123
43 #define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */
45 int lifetime = 604800;
47 stralloc percenthack = {0};
48 struct constmap mappercenthack;
49 stralloc locals = {0};
50 struct constmap maplocals;
52 struct constmap mapredir;
54 struct constmap mapvdoms;
55 stralloc envnoathost = {0};
56 stralloc bouncefrom = {0};
57 stralloc bouncehost = {0};
58 stralloc doublebounceto = {0};
59 stralloc doublebouncehost = {0};
61 char strnum2[FMT_ULONG];
62 char strnum3[FMT_ULONG];
65 char *chanaddr[CHANNELS] = { "local/", "remote/" };
66 char *channodelmsg[CHANNELS] = {
67 "local deliveries will be put on hold\n"
68 , "remote deliveries will be put on hold\n"
70 char *tochan[CHANNELS] = { " to local ", " to remote " };
71 int chanfdout[CHANNELS] = { 1, 3 };
72 int chanfdin[CHANNELS] = { 2, 4 };
73 int chanskip[CHANNELS] = { 10, 20 };
75 int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
76 int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
77 int flagreadasap = 0; void sighup() { flagreadasap = 1; }
79 void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
82 int flagspawnalive[CHANNELS];
83 void spawndied(c) int c; { log1("alert: oh no! lost spawn connection! dying...\n");
84 flagspawnalive[c] = 0; flagexitasap = 1; }
86 #define REPORTMAX 10000
91 /* this file is too long ----------------------------------------- FILENAMES */
95 char fnmake_strnum[FMT_ULONG];
99 while (!stralloc_ready(&fn,FMTQFN)) nomem();
100 while (!stralloc_ready(&fn2,FMTQFN)) nomem();
103 void fnmake_info(id) unsigned long id; { fn.len = fmtqfn(fn.s,"info/",id,1); }
104 void fnmake_todo(id) unsigned long id; { fn.len = fmtqfn(fn.s,"todo/",id,0); }
105 void fnmake_mess(id) unsigned long id; { fn.len = fmtqfn(fn.s,"mess/",id,1); }
106 void fnmake_foop(id) unsigned long id; { fn.len = fmtqfn(fn.s,"foop/",id,0); }
107 void fnmake_split(id) unsigned long id; { fn.len = fmtqfn(fn.s,"",id,1); }
108 void fnmake2_bounce(id) unsigned long id;
109 { fn2.len = fmtqfn(fn2.s,"bounce/",id,0); }
110 void fnmake_chanaddr(id,c) unsigned long id; int c;
111 { fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
114 /* this file is too long ----------------------------------------- REWRITING */
116 stralloc rwline = {0};
118 /* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
119 /* may trash recip. must set up rwline, between a T and a \0. */
126 static stralloc addr = {0};
127 static stralloc domain = {0};
129 if (!stralloc_copys(&rwline,"T")) return 0;
130 if (!stralloc_copys(&addr,recip)) return 0;
132 i = byte_rchr(addr.s,addr.len,'@');
134 if (!stralloc_cats(&addr,"@")) return 0;
135 if (!stralloc_cat(&addr,&envnoathost)) return 0;
138 while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
139 j = byte_rchr(addr.s,i,'%');
146 if (x = constmap(&mapredir,addr.s,addr.len))
147 if (x[str_chr(x,'@')])
148 if (!stralloc_copys(&addr,x)) return 0;
150 i = byte_rchr(addr.s,addr.len,'@');
151 if (!stralloc_copyb(&domain,addr.s + i + 1,addr.len - i - 1)) return 0;
154 if (constmap(&maplocals,domain.s,domain.len)) {
155 if (!stralloc_cat(&rwline,&addr)) return 0;
156 if (!stralloc_cats(&rwline,"@")) return 0;
157 if (!stralloc_cat(&rwline,&domain)) return 0;
158 if (!stralloc_0(&rwline)) return 0;
162 for (i = 0;i <= domain.len;++i)
163 if ((i == 0) || (i == domain.len) || (domain.s[i] == '.'))
164 if (x = constmap(&mapvdoms,domain.s + i,domain.len - i)) {
166 if (!stralloc_cats(&rwline,x)) return 0;
167 if (!stralloc_cats(&rwline,"-")) return 0;
168 if (!stralloc_cat(&rwline,&addr)) return 0;
169 if (!stralloc_cats(&rwline,"@")) return 0;
170 if (!stralloc_cat(&rwline,&domain)) return 0;
171 if (!stralloc_0(&rwline)) return 0;
175 if (!stralloc_cat(&rwline,&addr)) return 0;
176 if (!stralloc_cats(&rwline,"@")) return 0;
177 if (!stralloc_cat(&rwline,&domain)) return 0;
178 if (!stralloc_0(&rwline)) return 0;
182 void senderadd(sa,sender,recip)
193 if (str_equal(sender + i - 4,"-@[]"))
195 j = byte_rchr(sender,i - 4,'@');
196 k = str_rchr(recip,'@');
197 if (recip[k] && (j + 5 <= i))
199 /* owner-@host-@[] -> owner-recipbox=reciphost@host */
200 while (!stralloc_catb(sa,sender,j)) nomem();
201 while (!stralloc_catb(sa,recip,k)) nomem();
202 while (!stralloc_cats(sa,"=")) nomem();
203 while (!stralloc_cats(sa,recip + k + 1)) nomem();
204 while (!stralloc_cats(sa,"@")) nomem();
205 while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem();
209 while (!stralloc_cats(sa,sender)) nomem();
213 /* this file is too long ---------------------------------------------- INFO */
215 int getinfo(sa,dt,id)
222 static stralloc line = {0};
228 fdinfo = open_read(fn.s);
229 if (fdinfo == -1) return 0;
230 if (fstat(fdinfo,&st) == -1) { close(fdinfo); return 0; }
231 substdio_fdbuf(&ss,read,fdinfo,buf,sizeof(buf));
232 if (getln(&ss,&line,&match,'\0') == -1) { close(fdinfo); return 0; }
234 if (!match) return 0;
235 if (line.s[0] != 'F') return 0;
238 while (!stralloc_copys(sa,line.s + 1)) nomem();
239 while (!stralloc_0(sa)) nomem();
244 /* this file is too long ------------------------------------- COMMUNICATION */
246 substdio sstoqc; char sstoqcbuf[1024];
247 substdio ssfromqc; char ssfromqcbuf[1024];
248 stralloc comm_buf[CHANNELS] = { {0}, {0} };
249 int comm_pos[CHANNELS];
254 substdio_fdbuf(&sstoqc,write,5,sstoqcbuf,sizeof(sstoqcbuf));
255 substdio_fdbuf(&ssfromqc,read,6,ssfromqcbuf,sizeof(ssfromqcbuf));
256 for (c = 0;c < CHANNELS;++c)
257 if (ndelay_on(chanfdout[c]) == -1)
258 /* this is so stupid: NDELAY semantics should be default on write */
259 spawndied(c); /* drastic, but better than risking deadlock */
265 /* XXX: could allow a bigger buffer; say 10 recipients */
266 if (comm_buf[c].s && comm_buf[c].len) return 0;
270 void comm_write(c,delnum,id,sender,recip)
278 if (comm_buf[c].s && comm_buf[c].len) return;
279 while (!stralloc_copys(&comm_buf[c],"")) nomem();
281 while (!stralloc_append(&comm_buf[c],&ch)) nomem();
283 while (!stralloc_cats(&comm_buf[c],fn.s)) nomem();
284 while (!stralloc_0(&comm_buf[c])) nomem();
285 senderadd(&comm_buf[c],sender,recip);
286 while (!stralloc_0(&comm_buf[c])) nomem();
287 while (!stralloc_cats(&comm_buf[c],recip)) nomem();
288 while (!stralloc_0(&comm_buf[c])) nomem();
292 void comm_selprep(nfds,wfds)
297 for (c = 0;c < CHANNELS;++c)
298 if (flagspawnalive[c])
299 if (comm_buf[c].s && comm_buf[c].len)
301 FD_SET(chanfdout[c],wfds);
302 if (*nfds <= chanfdout[c])
303 *nfds = chanfdout[c] + 1;
311 for (c = 0;c < CHANNELS;++c)
312 if (flagspawnalive[c])
313 if (comm_buf[c].s && comm_buf[c].len)
314 if (FD_ISSET(chanfdout[c],wfds))
318 len = comm_buf[c].len;
319 w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
322 if ((w == -1) && (errno == error_pipe))
325 continue; /* kernel select() bug; can't avoid busy-looping */
330 if (comm_pos[c] == len)
337 /* this file is too long ------------------------------------------ CLEANUPS */
339 int flagcleanup; /* if 1, cleanupdir is initialized and ready */
340 readsubdir cleanupdir;
341 datetime_sec cleanuptime;
349 void cleanup_selprep(wakeup)
350 datetime_sec *wakeup;
352 if (flagcleanup) *wakeup = 0;
353 if (*wakeup > cleanuptime) *wakeup = cleanuptime;
364 if (recent < cleanuptime) return;
365 readsubdir_init(&cleanupdir,"mess",pausedir);
369 switch(readsubdir_next(&cleanupdir,&id))
375 cleanuptime = recent + SLEEP_CLEANUP;
381 if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
382 if (recent <= st.st_atime + OSSIFIED) return;
385 if (stat(fn.s,&st) == 0) return;
386 if (errno != error_noent) return;
388 if (stat(fn.s,&st) == 0) return;
389 if (errno != error_noent) return;
392 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
393 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
395 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
399 /* this file is too long ----------------------------------- PRIORITY QUEUES */
401 prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */
402 prioq pqchan[CHANNELS] = { {0}, {0} };
403 /* pqchan 0: -todo +info +local ?remote */
404 /* pqchan 1: -todo +info ?local +remote */
405 prioq pqfail = {0}; /* stat() failure; has to be pqadded again */
411 struct prioq_elt pechan[CHANNELS];
412 int flagchan[CHANNELS];
416 #define CHECKSTAT if (errno != error_noent) goto fail;
419 if (stat(fn.s,&st) == -1)
422 return; /* someone yanking our chain */
426 if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
429 for (c = 0;c < CHANNELS;++c)
431 fnmake_chanaddr(id,c);
432 if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT }
433 else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; }
436 for (c = 0;c < CHANNELS;++c)
438 while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
440 for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
443 pe.id = id; pe.dt = now();
444 while (!prioq_insert(&pqdone,&pe)) nomem();
450 log3("warning: unable to stat ",fn.s,"; will try again later\n");
451 pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
452 while (!prioq_insert(&pqfail,&pe)) nomem();
461 readsubdir_init(&rs,"info",pausedir);
463 while (x = readsubdir_next(&rs,&id))
472 time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */
474 for (c = 0;c < CHANNELS;++c)
475 while (prioq_min(&pqchan[c],&pe))
477 prioq_delmin(&pqchan[c]);
478 fnmake_chanaddr(pe.id,c);
479 ut[0] = ut[1] = pe.dt;
480 if (utime(fn.s,ut) == -1)
481 log3("warning: unable to utime ",fn.s,"; message will be retried too soon\n");
489 for (c = 0;c < CHANNELS;++c)
492 for (i = 0;i < pqchan[c].len;++i)
493 pqchan[c].p[i].dt = recent;
497 /* this file is too long ---------------------------------------------- JOBS */
501 int refs; /* if 0, this struct is unused */
518 while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
519 for (j = 0;j < numjobs;++j)
529 for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
533 int job_open(id,channel)
538 for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
539 if (j == numjobs) return -1;
542 jo[j].channel = channel;
544 jo[j].flaghiteof = 0;
554 if (0 < --jo[j].refs) return;
558 if (jo[j].flaghiteof && !jo[j].numtodo)
560 fnmake_chanaddr(jo[j].id,jo[j].channel);
561 if (unlink(fn.s) == -1)
563 log3("warning: unable to unlink ",fn.s,"; will try again later\n");
564 pe.dt = now() + SLEEP_SYSFAIL;
569 for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
571 fnmake_chanaddr(jo[j].id,c);
572 if (stat(fn.s,&st) == 0) return; /* more channels going */
573 if (errno != error_noent)
575 log3("warning: unable to stat ",fn.s,"\n");
576 break; /* this is the only reason for HOPEFULLY */
580 while (!prioq_insert(&pqdone,&pe)) nomem();
585 while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
589 /* this file is too long ------------------------------------------- BOUNCES */
591 char *stripvdomprepend(recip)
599 i = str_rchr(recip,'@');
600 if (!recip[i]) return recip;
601 domain = recip + i + 1;
602 domainlen = str_len(domain);
604 for (i = 0;i <= domainlen;++i)
605 if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
606 if (prepend = constmap(&mapvdoms,domain + i,domainlen - i))
608 if (!*prepend) break;
609 i = str_len(prepend);
610 if (str_diffn(recip,prepend,i)) break;
611 if (recip[i] != '-') break;
612 return recip + i + 1;
617 stralloc bouncetext = {0};
619 void addbounce(id,recip,report)
627 while (!stralloc_copys(&bouncetext,"<")) nomem();
628 while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem();
629 for (pos = 0;pos < bouncetext.len;++pos)
630 if (bouncetext.s[pos] == '\n')
631 bouncetext.s[pos] = '_';
632 while (!stralloc_cats(&bouncetext,">:\n")) nomem();
633 while (!stralloc_cats(&bouncetext,report)) nomem();
635 if (report[str_len(report) - 1] != '\n')
636 while (!stralloc_cats(&bouncetext,"\n")) nomem();
637 for (pos = bouncetext.len - 2;pos > 0;--pos)
638 if (bouncetext.s[pos] == '\n')
639 if (bouncetext.s[pos - 1] == '\n')
640 bouncetext.s[pos] = '/';
641 while (!stralloc_cats(&bouncetext,"\n")) nomem();
645 fd = open_append(fn2.s);
647 log1("alert: unable to append to bounce message; HELP! sleeping...\n");
651 while (pos < bouncetext.len)
653 w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
656 log1("alert: unable to append to bounce message; HELP! sleeping...\n");
677 static stralloc sender = {0};
678 static stralloc quoted = {0};
682 if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
684 /* owner-@host-@[] -> owner-@host */
686 if (str_equal(sender.s + sender.len - 5,"-@[]"))
689 sender.s[sender.len - 1] = 0;
694 if (stat(fn2.s,&st) == -1)
696 if (errno == error_noent)
698 log3("warning: unable to stat ",fn2.s,"\n");
701 if (str_equal(sender.s,"#@[]"))
702 log3("triple bounce: discarding ",fn2.s,"\n");
705 if (qmail_open(&qqt) == -1)
706 { log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
709 if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
710 else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
712 while (!newfield_datemake(now())) nomem();
713 qmail_put(&qqt,newfield_date.s,newfield_date.len);
714 qmail_puts(&qqt,"From: ");
715 while (!quote("ed,&bouncefrom)) nomem();
716 qmail_put(&qqt,quoted.s,quoted.len);
717 qmail_puts(&qqt,"@");
718 qmail_put(&qqt,bouncehost.s,bouncehost.len);
719 qmail_puts(&qqt,"\nTo: ");
720 while (!quote2("ed,bouncerecip)) nomem();
721 qmail_put(&qqt,quoted.s,quoted.len);
723 Subject: failure notice\n\
725 Hi. This is the qmail-send program at ");
726 qmail_put(&qqt,bouncehost.s,bouncehost.len);
727 qmail_puts(&qqt,*sender.s ? ".\n\
728 I'm afraid I wasn't able to deliver your message to the following addresses.\n\
729 This is a permanent error; I've given up. Sorry it didn't work out.\n\
732 I tried to deliver a bounce message to this address, but the bounce bounced!\n\
736 fd = open_read(fn2.s);
741 substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
742 while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
743 qmail_put(&qqt,buf,r);
749 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");
750 qmail_puts(&qqt,"Return-Path: <");
751 while (!quote2("ed,sender.s)) nomem();
752 qmail_put(&qqt,quoted.s,quoted.len);
753 qmail_puts(&qqt,">\n");
755 fd = open_read(fn.s);
760 substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
761 while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
762 qmail_put(&qqt,buf,r);
768 qmail_from(&qqt,bouncesender);
769 qmail_to(&qqt,bouncerecip);
770 if (qmail_close(&qqt))
771 { log1("warning: trouble injecting bounce message, will try later\n"); return 0; }
773 strnum2[fmt_ulong(strnum2,id)] = 0;
774 log2("bounce msg ",strnum2);
775 strnum2[fmt_ulong(strnum2,qp)] = 0;
776 log3(" qp ",strnum2,"\n");
778 if (unlink(fn2.s) == -1)
780 log3("warning: unable to unlink ",fn2.s,"\n");
787 /* this file is too long ---------------------------------------- DELIVERIES */
799 unsigned long masterdelid = 1;
800 unsigned int concurrency[CHANNELS] = { 10, 20 };
801 struct del *d[CHANNELS];
802 stralloc dline[CHANNELS];
809 for (c = 0;c < CHANNELS;++c)
811 flagspawnalive[c] = 1;
812 while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
814 for (i = 0;i < concurrency[c];++i)
815 { d[c][i].used = 0; d[c][i].recip.s = 0; }
817 while (!stralloc_copys(&dline[c],"")) nomem();
825 for (c = 0;c < CHANNELS;++c)
826 if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */
827 for (i = 0;i < concurrency[c];++i)
828 if (d[c][i].used) return 0;
832 static int del_lastsaid = 0;
834 void del_saywhynoexit()
840 for (c = 0;c < CHANNELS;++c)
841 if (flagspawnalive[c])
842 for (i = 0;i < concurrency[c];++i)
845 if (!del_lastsaid || (n < del_lastsaid))
847 strnum2[fmt_ulong(strnum2,(unsigned long) n)] = 0;
848 log3("number of deliveries left before exiting: ",strnum2,"\n");
858 if (!flagspawnalive[c]) return 0;
859 if (!comm_canwrite(c)) return 0;
860 for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) return 1;
864 void del_start(j,mpos,recip)
873 if (!flagspawnalive[c]) return;
874 if (!comm_canwrite(c)) return;
876 for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
877 if (i == concurrency[c]) return;
879 if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; }
880 if (!stralloc_0(&d[c][i].recip)) { nomem(); return; }
881 d[c][i].j = j; ++jo[j].refs;
882 d[c][i].delid = masterdelid++;
886 comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
888 strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0;
889 strnum3[fmt_ulong(strnum3,jo[j].id)] = 0;
890 log2("starting delivery ",strnum2);
891 log3(": msg ",strnum3,tochan[c]);
896 void markdone(c,id,pos)
903 fnmake_chanaddr(id,c);
906 fd = open_write(fn.s);
908 if (fstat(fd,&st) == -1) { close(fd); break; }
909 if (seek_set(fd,pos) == -1) { close(fd); break; }
910 if (write(fd,"D",1) != 1) { close(fd); break; }
911 /* further errors -> double delivery without us knowing about it, oh well */
915 log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
925 r = read(chanfdin[c],delbuf,sizeof(delbuf));
927 if (r == 0) { spawndied(c); return; }
928 for (i = 0;i < r;++i)
931 while (!stralloc_append(&dline[c],&ch)) nomem();
932 if (dline[c].len > REPORTMAX)
933 dline[c].len = REPORTMAX;
934 /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */
935 /* but from a security point of view, we don't trust rspawn */
936 if (!ch && (dline[c].len > 1))
938 delnum = (unsigned int) (unsigned char) dline[c].s[0];
939 if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used)
940 log1("warning: internal error: delivery report out of range\n");
943 strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0;
944 if (dline[c].s[1] == 'Z')
945 if (jo[d[c][delnum].j].flagdying)
949 while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem();
950 while (!stralloc_0(&dline[c])) nomem();
952 switch(dline[c].s[1])
955 log3("delivery ",strnum3,": success: ");
956 logsafe(dline[c].s + 2);
958 markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
959 --jo[d[c][delnum].j].numtodo;
962 log3("delivery ",strnum3,": deferral: ");
963 logsafe(dline[c].s + 2);
967 log3("delivery ",strnum3,": failure: ");
968 logsafe(dline[c].s + 2);
970 addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2);
971 markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
972 --jo[d[c][delnum].j].numtodo;
975 log3("delivery ",strnum3,": report mangled, will defer\n");
977 job_close(d[c][delnum].j);
978 d[c][delnum].used = 0;
985 void del_selprep(nfds,rfds)
990 for (c = 0;c < CHANNELS;++c)
991 if (flagspawnalive[c])
993 FD_SET(chanfdin[c],rfds);
994 if (*nfds <= chanfdin[c])
995 *nfds = chanfdin[c] + 1;
1003 for (c = 0;c < CHANNELS;++c)
1004 if (flagspawnalive[c])
1005 if (FD_ISSET(chanfdin[c],rfds))
1010 /* this file is too long -------------------------------------------- PASSES */
1014 unsigned long id; /* if 0, need a new pass */
1015 int j; /* defined if id; job number */
1016 int fd; /* defined if id; reading from {local,remote} */
1017 seek_pos mpos; /* defined if id; mark position */
1026 for (c = 0;c < CHANNELS;++c) pass[c].id = 0;
1029 void pass_selprep(wakeup)
1030 datetime_sec *wakeup;
1033 struct prioq_elt pe;
1034 if (flagexitasap) return;
1035 for (c = 0;c < CHANNELS;++c)
1038 { *wakeup = 0; return; }
1040 for (c = 0;c < CHANNELS;++c)
1042 if (prioq_min(&pqchan[c],&pe))
1043 if (*wakeup > pe.dt)
1045 if (prioq_min(&pqfail,&pe))
1046 if (*wakeup > pe.dt)
1048 if (prioq_min(&pqdone,&pe))
1049 if (*wakeup > pe.dt)
1053 static datetime_sec squareroot(x) /* result^2 <= x < (result + 1)^2 */
1054 datetime_sec x; /* assuming: >= 0 */
1062 for (j = 15;j >= 0;--j)
1064 y21 = (y << (j + 1)) + (1 << (j + j));
1065 if (y21 <= x - yy) { y += (1 << j); yy += y21; }
1070 datetime_sec nextretry(birth,c)
1076 if (birth > recent) n = 0;
1077 else n = squareroot(recent - birth); /* no need to add fuzz to recent */
1079 return birth + n * n;
1086 struct prioq_elt pe;
1087 static stralloc line = {0};
1090 if (flagexitasap) return;
1094 if (!job_avail()) return;
1095 if (!prioq_min(&pqchan[c],&pe)) return;
1096 if (pe.dt > recent) return;
1097 fnmake_chanaddr(pe.id,c);
1099 prioq_delmin(&pqchan[c]);
1101 pass[c].fd = open_read(fn.s);
1102 if (pass[c].fd == -1) goto trouble;
1103 if (!getinfo(&line,&birth,pe.id)) { close(pass[c].fd); goto trouble; }
1105 substdio_fdbuf(&pass[c].ss,read,pass[c].fd,pass[c].buf,sizeof(pass[c].buf));
1106 pass[c].j = job_open(pe.id,c);
1107 jo[pass[c].j].retry = nextretry(birth,c);
1108 jo[pass[c].j].flagdying = (recent > birth + lifetime);
1109 while (!stralloc_copy(&jo[pass[c].j].sender,&line)) nomem();
1112 if (!del_avail(c)) return;
1114 if (getln(&pass[c].ss,&line,&match,'\0') == -1)
1116 fnmake_chanaddr(pass[c].id,c);
1117 log3("warning: trouble reading ",fn.s,"; will try again later\n");
1119 job_close(pass[c].j);
1126 jo[pass[c].j].flaghiteof = 1;
1127 job_close(pass[c].j);
1134 ++jo[pass[c].j].numtodo;
1135 del_start(pass[c].j,pass[c].mpos,line.s + 1);
1140 fnmake_chanaddr(pass[c].id,c);
1141 log3("warning: unknown record type in ",fn.s,"!\n");
1143 job_close(pass[c].j);
1148 pass[c].mpos += line.len;
1152 log3("warning: trouble opening ",fn.s,"; will try again later\n");
1153 pe.dt = recent + SLEEP_SYSFAIL;
1154 while (!prioq_insert(&pqchan[c],&pe)) nomem();
1162 struct prioq_elt pe;
1165 for (c = 0;c < CHANNELS;++c)
1167 fnmake_chanaddr(id,c);
1168 if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */
1169 if (errno != error_noent)
1171 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1177 if (stat(fn.s,&st) == 0) return;
1178 if (errno != error_noent)
1180 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1185 if (stat(fn.s,&st) == -1)
1187 if (errno == error_noent) return;
1188 log3("warning: unable to stat ",fn.s,"; will try again later\n");
1192 /* -todo +info -local -remote ?bounce */
1193 if (!injectbounce(id))
1194 goto fail; /* injectbounce() produced error message */
1196 strnum3[fmt_ulong(strnum3,id)] = 0;
1197 log3("end msg ",strnum3,"\n");
1199 /* -todo +info -local -remote -bounce */
1201 if (unlink(fn.s) == -1)
1203 log3("warning: unable to unlink ",fn.s,"; will try again later\n");
1207 /* -todo -info -local -remote -bounce; we can relax */
1209 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1210 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1212 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1217 pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
1218 while (!prioq_insert(&pqdone,&pe)) nomem();
1224 struct prioq_elt pe;
1226 for (c = 0;c < CHANNELS;++c) pass_dochan(c);
1227 if (prioq_min(&pqfail,&pe))
1228 if (pe.dt <= recent)
1230 prioq_delmin(&pqfail);
1233 if (prioq_min(&pqdone,&pe))
1234 if (pe.dt <= recent)
1236 prioq_delmin(&pqdone);
1242 /* this file is too long ---------------------------------------------- TODO */
1244 datetime_sec nexttodorun;
1245 DIR *tododir; /* if 0, have to opendir again */
1246 stralloc todoline = {0};
1247 char todobuf[SUBSTDIO_INSIZE];
1248 char todobufinfo[512];
1249 char todobufchan[CHANNELS][1024];
1254 nexttodorun = now();
1258 void todo_selprep(nfds,rfds,wakeup)
1261 datetime_sec *wakeup;
1263 if (flagexitasap) return;
1264 trigger_selprep(nfds,rfds);
1265 if (tododir) *wakeup = 0;
1266 if (*wakeup > nexttodorun) *wakeup = nexttodorun;
1273 substdio ss; int fd;
1274 substdio ssinfo; int fdinfo;
1275 substdio sschan[CHANNELS];
1276 int fdchan[CHANNELS];
1277 int flagchan[CHANNELS];
1278 struct prioq_elt pe;
1290 for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
1292 if (flagexitasap) return;
1296 if (!trigger_pulled(rfds))
1297 if (recent < nexttodorun)
1300 tododir = opendir("todo");
1306 nexttodorun = recent + SLEEP_TODO;
1309 d = readdir(tododir);
1316 if (str_equal(d->d_name,".")) return;
1317 if (str_equal(d->d_name,"..")) return;
1318 len = scan_ulong(d->d_name,&id);
1319 if (!len || d->d_name[len]) return;
1323 fd = open_read(fn.s);
1324 if (fd == -1) { log3("warning: unable to open ",fn.s,"\n"); return; }
1327 /* just for the statistics */
1328 if (stat(fn.s,&st) == -1)
1329 { log3("warning: unable to stat ",fn.s,"\n"); goto fail; }
1331 for (c = 0;c < CHANNELS;++c)
1333 fnmake_chanaddr(id,c);
1334 if (unlink(fn.s) == -1) if (errno != error_noent)
1335 { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
1339 if (unlink(fn.s) == -1) if (errno != error_noent)
1340 { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
1342 fdinfo = open_excl(fn.s);
1344 { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
1346 strnum3[fmt_ulong(strnum3,id)] = 0;
1347 log3("new msg ",strnum3,"\n");
1349 for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
1351 substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
1352 substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
1359 if (getln(&ss,&todoline,&match,'\0') == -1)
1361 /* perhaps we're out of memory, perhaps an I/O error */
1363 log3("warning: trouble reading ",fn.s,"\n"); goto fail;
1367 switch(todoline.s[0])
1370 scan_ulong(todoline.s + 1,&uid);
1373 scan_ulong(todoline.s + 1,&pid);
1376 if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
1379 log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1381 log2("info msg ",strnum3);
1382 strnum2[fmt_ulong(strnum2,(unsigned long) st.st_size)] = 0;
1383 log2(": bytes ",strnum2);
1384 log1(" from <"); logsafe(todoline.s + 1);
1385 strnum2[fmt_ulong(strnum2,pid)] = 0;
1386 log2("> qp ",strnum2);
1387 strnum2[fmt_ulong(strnum2,uid)] = 0;
1388 log2(" uid ",strnum2);
1392 switch(rewrite(todoline.s + 1))
1394 case 0: nomem(); goto fail;
1395 case 2: c = 1; break;
1396 default: c = 0; break;
1398 if (fdchan[c] == -1)
1400 fnmake_chanaddr(id,c);
1401 fdchan[c] = open_excl(fn.s);
1402 if (fdchan[c] == -1)
1403 { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
1404 substdio_fdbuf(&sschan[c]
1405 ,write,fdchan[c],todobufchan[c],sizeof(todobufchan[c]));
1408 if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
1410 fnmake_chanaddr(id,c);
1411 log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1416 log3("warning: unknown record type in ",fn.s,"\n"); goto fail;
1423 if (substdio_flush(&ssinfo) == -1)
1424 { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
1425 if (fsync(fdinfo) == -1)
1426 { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
1427 close(fdinfo); fdinfo = -1;
1429 for (c = 0;c < CHANNELS;++c)
1430 if (fdchan[c] != -1)
1432 fnmake_chanaddr(id,c);
1433 if (substdio_flush(&sschan[c]) == -1)
1434 { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
1435 if (fsync(fdchan[c]) == -1)
1436 { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
1437 close(fdchan[c]); fdchan[c] = -1;
1441 if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1442 if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1445 log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1449 pe.id = id; pe.dt = now();
1450 for (c = 0;c < CHANNELS;++c)
1452 while (!prioq_insert(&pqchan[c],&pe)) nomem();
1454 for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
1456 while (!prioq_insert(&pqdone,&pe)) nomem();
1461 if (fd != -1) close(fd);
1462 if (fdinfo != -1) close(fdinfo);
1463 for (c = 0;c < CHANNELS;++c)
1464 if (fdchan[c] != -1) close(fdchan[c]);
1468 /* this file is too long ---------------------------------------------- MAIN */
1470 int getcontrols() { if (control_init() == -1) return 0;
1471 if (control_readint(&lifetime,"control/queuelifetime") == -1) return 0;
1472 if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) return 0;
1473 if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) return 0;
1474 if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0;
1475 if (control_rldef(&bouncefrom,"control/bouncefrom",0,"MAILER-DAEMON") != 1) return 0;
1476 if (control_rldef(&bouncehost,"control/bouncehost",1,"bouncehost") != 1) return 0;
1477 if (control_rldef(&doublebouncehost,"control/doublebouncehost",1,"doublebouncehost") != 1) return 0;
1478 if (control_rldef(&doublebounceto,"control/doublebounceto",0,"postmaster") != 1) return 0;
1479 if (!stralloc_cats(&doublebounceto,"@")) return 0;
1480 if (!stralloc_cat(&doublebounceto,&doublebouncehost)) return 0;
1481 if (!stralloc_0(&doublebounceto)) return 0;
1482 if (control_readfile(&locals,"control/locals",1) != 1) return 0;
1483 if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0;
1484 switch(control_readfile(&percenthack,"control/percenthack",0))
1487 case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break;
1488 case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break;
1490 switch(control_readfile(&redir,"control/recipientmap",0))
1493 case 0: if (!constmap_init(&mapredir,"",0,1)) return 0; break;
1494 case 1: if (!constmap_init(&mapredir,redir.s,redir.len,1)) return 0; break;
1496 switch(control_readfile(&vdoms,"control/virtualdomains",0))
1499 case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break;
1500 case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break;
1504 stralloc newlocals = {0};
1505 stralloc newvdoms = {0};
1507 void regetcontrols()
1511 if (control_readfile(&newlocals,"control/locals",1) != 1)
1512 { log1("alert: unable to reread control/locals\n"); return; }
1513 r = control_readfile(&newvdoms,"control/virtualdomains",0);
1515 { log1("alert: unable to reread control/virtualdomains\n"); return; }
1517 constmap_free(&maplocals);
1518 constmap_free(&mapvdoms);
1520 while (!stralloc_copy(&locals,&newlocals)) nomem();
1521 while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
1525 while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
1526 while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
1529 while (!constmap_init(&mapvdoms,"",0,1)) nomem();
1534 if (chdir(auto_qmail) == -1)
1536 log1("alert: unable to reread controls: unable to switch to home directory\n");
1540 while (chdir("queue") == -1)
1542 log1("alert: unable to switch back to queue directory; HELP! sleeping...\n");
1550 datetime_sec wakeup;
1557 if (chdir(auto_qmail) == -1)
1558 { log1("alert: cannot start: unable to switch to home directory\n"); _exit(111); }
1560 { log1("alert: cannot start: unable to read controls\n"); _exit(111); }
1561 if (chdir("queue") == -1)
1562 { log1("alert: cannot start: unable to switch to queue directory\n"); _exit(111); }
1564 sig_termcatch(sigterm);
1565 sig_alarmcatch(sigalrm);
1566 sig_hangupcatch(sighup);
1570 fd = open_write("lock/sendmutex");
1572 { log1("alert: cannot start: unable to open mutex\n"); _exit(111); }
1573 if (lock_exnb(fd) == -1)
1574 { log1("alert: cannot start: qmail-send is already running\n"); _exit(111); }
1577 for (c = 0;c < CHANNELS;++c)
1583 r = read(chanfdin[c],&ch,1);
1584 while ((r == -1) && (errno == error_intr));
1586 { log1("alert: cannot start: hath the daemon spawn no fire?\n"); _exit(111); }
1587 u = (unsigned int) (unsigned char) ch;
1588 if (concurrency[c] > u) concurrency[c] = u;
1589 numjobs += concurrency[c];
1596 for (c = 0;c < CHANNELS;++c)
1597 if (!concurrency[c])
1598 log1(channodelmsg[c]);
1609 while (!flagexitasap || !del_canexit())
1611 if (flagexitasap) del_saywhynoexit();
1615 if (flagrunasap) { flagrunasap = 0; pqrun(); }
1616 if (flagreadasap) { flagreadasap = 0; reread(); }
1618 wakeup = recent + SLEEP_FOREVER;
1623 comm_selprep(&nfds,&wfds);
1624 del_selprep(&nfds,&rfds);
1625 pass_selprep(&wakeup);
1626 todo_selprep(&nfds,&rfds,&wakeup);
1627 cleanup_selprep(&wakeup);
1629 if (wakeup <= recent) tv.tv_sec = 0;
1630 else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
1633 if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1)
1634 if (errno == error_intr)
1637 log1("warning: trouble in select\n");