chiark / gitweb /
Upstream qmail 1.01
[qmail] / qmail-send.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include "readwrite.h"
4 #include "sig.h"
5 #include "direntry.h"
6 #include "control.h"
7 #include "select.h"
8 #include "open.h"
9 #include "seek.h"
10 #include "exit.h"
11 #include "lock.h"
12 #include "ndelay.h"
13 #include "now.h"
14 #include "getln.h"
15 #include "substdio.h"
16 #include "alloc.h"
17 #include "error.h"
18 #include "stralloc.h"
19 #include "str.h"
20 #include "byte.h"
21 #include "fmt.h"
22 #include "scan.h"
23 #include "case.h"
24 #include "auto_qmail.h"
25 #include "trigger.h"
26 #include "newfield.h"
27 #include "quote.h"
28 #include "qmail.h"
29 #include "qsutil.h"
30 #include "prioq.h"
31 #include "constmap.h"
32 #include "fmtqfn.h"
33 #include "readsubdir.h"
34
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) */
44
45 int lifetime = 604800;
46
47 stralloc percenthack = {0};
48 struct constmap mappercenthack;
49 stralloc locals = {0};
50 struct constmap maplocals;
51 stralloc redir = {0};
52 struct constmap mapredir;
53 stralloc vdoms = {0};
54 struct constmap mapvdoms;
55 stralloc envnoathost = {0};
56 stralloc bouncefrom = {0};
57 stralloc bouncehost = {0};
58 stralloc doublebounceto = {0};
59 stralloc doublebouncehost = {0};
60
61 char strnum2[FMT_ULONG];
62 char strnum3[FMT_ULONG];
63
64 #define CHANNELS 2
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"
69 };
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 };
74
75 int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
76 int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
77 int flagreadasap = 0; void sighup() { flagreadasap = 1; }
78
79 void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
80  flagexitasap = 1; }
81
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; }
85
86 #define REPORTMAX 10000
87
88 datetime_sec recent;
89
90
91 /* this file is too long ----------------------------------------- FILENAMES */
92
93 stralloc fn = {0};
94 stralloc fn2 = {0};
95 char fnmake_strnum[FMT_ULONG];
96
97 void fnmake_init()
98 {
99  while (!stralloc_ready(&fn,FMTQFN)) nomem();
100  while (!stralloc_ready(&fn2,FMTQFN)) nomem();
101 }
102
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); }
112
113
114 /* this file is too long ----------------------------------------- REWRITING */
115
116 stralloc rwline = {0};
117
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. */
120 int rewrite(recip)
121 char *recip;
122 {
123   int i;
124   int j;
125   char *x;
126   static stralloc addr = {0};
127   static stralloc domain = {0};
128
129   if (!stralloc_copys(&rwline,"T")) return 0;
130   if (!stralloc_copys(&addr,recip)) return 0;
131
132   i = byte_rchr(addr.s,addr.len,'@');
133   if (i == addr.len) {
134     if (!stralloc_cats(&addr,"@")) return 0;
135     if (!stralloc_cat(&addr,&envnoathost)) return 0;
136   }
137
138   while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
139     j = byte_rchr(addr.s,i,'%');
140     if (j == i) break;
141     addr.len = i;
142     i = j;
143     addr.s[i] = '@';
144   }
145
146   if (x = constmap(&mapredir,addr.s,addr.len))
147     if (x[str_chr(x,'@')])
148       if (!stralloc_copys(&addr,x)) return 0;
149
150   i = byte_rchr(addr.s,addr.len,'@');
151   if (!stralloc_copyb(&domain,addr.s + i + 1,addr.len - i - 1)) return 0;
152   addr.len = i;
153
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;
159     return 1;
160   }
161
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)) {
165         if (!*x) break;
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;
172         return 1;
173       }
174  
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;
179   return 2;
180 }
181
182 void senderadd(sa,sender,recip)
183 stralloc *sa;
184 char *sender;
185 char *recip;
186 {
187  int i;
188  int j;
189  int k;
190
191  i = str_len(sender);
192  if (i >= 4)
193    if (str_equal(sender + i - 4,"-@[]"))
194     {
195      j = byte_rchr(sender,i - 4,'@');
196      k = str_rchr(recip,'@');
197      if (recip[k] && (j + 5 <= i))
198       {
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();
206        return;
207       }
208     }
209  while (!stralloc_cats(sa,sender)) nomem();
210 }
211
212
213 /* this file is too long ---------------------------------------------- INFO */
214
215 int getinfo(sa,dt,id)
216 stralloc *sa;
217 datetime_sec *dt;
218 unsigned long id;
219 {
220  int fdinfo;
221  struct stat st;
222  static stralloc line = {0};
223  int match;
224  substdio ss;
225  char buf[128];
226
227  fnmake_info(id);
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; }
233  close(fdinfo);
234  if (!match) return 0;
235  if (line.s[0] != 'F') return 0;
236
237  *dt = st.st_mtime;
238  while (!stralloc_copys(sa,line.s + 1)) nomem();
239  while (!stralloc_0(sa)) nomem();
240  return 1;
241 }
242
243
244 /* this file is too long ------------------------------------- COMMUNICATION */
245
246 substdio sstoqc; char sstoqcbuf[1024];
247 substdio ssfromqc; char ssfromqcbuf[1024];
248 stralloc comm_buf[CHANNELS] = { {0}, {0} };
249 int comm_pos[CHANNELS];
250
251 void comm_init()
252 {
253  int c;
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 */
260 }
261
262 int comm_canwrite(c)
263 int c;
264 {
265  /* XXX: could allow a bigger buffer; say 10 recipients */
266  if (comm_buf[c].s && comm_buf[c].len) return 0;
267  return 1;
268 }
269
270 void comm_write(c,delnum,id,sender,recip)
271 int c;
272 int delnum;
273 unsigned long id;
274 char *sender;
275 char *recip;
276 {
277  char ch;
278  if (comm_buf[c].s && comm_buf[c].len) return;
279  while (!stralloc_copys(&comm_buf[c],"")) nomem();
280  ch = delnum;
281  while (!stralloc_append(&comm_buf[c],&ch)) nomem();
282  fnmake_split(id);
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();
289  comm_pos[c] = 0;
290 }
291
292 void comm_selprep(nfds,wfds)
293 int *nfds;
294 fd_set *wfds;
295 {
296  int c;
297  for (c = 0;c < CHANNELS;++c)
298    if (flagspawnalive[c])
299      if (comm_buf[c].s && comm_buf[c].len)
300       {
301        FD_SET(chanfdout[c],wfds);
302        if (*nfds <= chanfdout[c])
303          *nfds = chanfdout[c] + 1;
304       }
305 }
306
307 void comm_do(wfds)
308 fd_set *wfds;
309 {
310  int c;
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))
315         {
316          int w;
317          int len;
318          len = comm_buf[c].len;
319          w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
320          if (w <= 0)
321           {
322            if ((w == -1) && (errno == error_pipe))
323              spawndied(c);
324            else
325              continue; /* kernel select() bug; can't avoid busy-looping */
326           }
327          else
328           {
329            comm_pos[c] += w;
330            if (comm_pos[c] == len)
331              comm_buf[c].len = 0;
332           }
333         }
334 }
335
336
337 /* this file is too long ------------------------------------------ CLEANUPS */
338
339 int flagcleanup; /* if 1, cleanupdir is initialized and ready */
340 readsubdir cleanupdir;
341 datetime_sec cleanuptime;
342
343 void cleanup_init()
344 {
345  flagcleanup = 0;
346  cleanuptime = now();
347 }
348
349 void cleanup_selprep(wakeup)
350 datetime_sec *wakeup;
351 {
352  if (flagcleanup) *wakeup = 0;
353  if (*wakeup > cleanuptime) *wakeup = cleanuptime;
354 }
355
356 void cleanup_do()
357 {
358  char ch;
359  struct stat st;
360  unsigned long id;
361
362  if (!flagcleanup)
363   {
364    if (recent < cleanuptime) return;
365    readsubdir_init(&cleanupdir,"mess",pausedir);
366    flagcleanup = 1;
367   }
368
369  switch(readsubdir_next(&cleanupdir,&id))
370   {
371    case 1:
372      break;
373    case 0:
374      flagcleanup = 0;
375      cleanuptime = recent + SLEEP_CLEANUP;
376    default:
377      return;
378   }
379
380  fnmake_mess(id);
381  if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
382  if (recent <= st.st_atime + OSSIFIED) return;
383
384  fnmake_info(id);
385  if (stat(fn.s,&st) == 0) return;
386  if (errno != error_noent) return;
387  fnmake_todo(id);
388  if (stat(fn.s,&st) == 0) return;
389  if (errno != error_noent) return;
390
391  fnmake_foop(id);
392  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
393  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
394  if (ch != '+')
395    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
396 }
397
398
399 /* this file is too long ----------------------------------- PRIORITY QUEUES */
400
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 */
406
407 void pqadd(id)
408 unsigned long id;
409 {
410  struct prioq_elt pe;
411  struct prioq_elt pechan[CHANNELS];
412  int flagchan[CHANNELS];
413  struct stat st;
414  int c;
415
416 #define CHECKSTAT if (errno != error_noent) goto fail;
417
418  fnmake_info(id);
419  if (stat(fn.s,&st) == -1)
420   {
421    CHECKSTAT
422    return; /* someone yanking our chain */
423   }
424
425  fnmake_todo(id);
426  if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
427  CHECKSTAT
428
429  for (c = 0;c < CHANNELS;++c)
430   {
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; }
434   }
435
436  for (c = 0;c < CHANNELS;++c)
437    if (flagchan[c])
438      while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
439
440  for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
441  if (c == CHANNELS)
442   {
443    pe.id = id; pe.dt = now();
444    while (!prioq_insert(&pqdone,&pe)) nomem();
445   }
446
447  return;
448
449  fail:
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();
453 }
454
455 void pqstart()
456 {
457  readsubdir rs;
458  int x;
459  unsigned long id;
460
461  readsubdir_init(&rs,"info",pausedir);
462
463  while (x = readsubdir_next(&rs,&id))
464    if (x > 0)
465      pqadd(id);
466 }
467
468 void pqfinish()
469 {
470  int c;
471  struct prioq_elt pe;
472  time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */
473
474  for (c = 0;c < CHANNELS;++c)
475    while (prioq_min(&pqchan[c],&pe))
476     {
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");
482     }
483 }
484
485 void pqrun()
486 {
487  int c;
488  int i;
489  for (c = 0;c < CHANNELS;++c)
490    if (pqchan[c].p)
491      if (pqchan[c].len)
492        for (i = 0;i < pqchan[c].len;++i)
493          pqchan[c].p[i].dt = recent;
494 }
495
496
497 /* this file is too long ---------------------------------------------- JOBS */
498
499 struct job
500  {
501   int refs; /* if 0, this struct is unused */
502   unsigned long id;
503   int channel;
504   datetime_sec retry;
505   stralloc sender;
506   int numtodo;
507   int flaghiteof;
508   int flagdying;
509  }
510 ;
511
512 int numjobs;
513 struct job *jo;
514
515 void job_init()
516 {
517  int j;
518  while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
519  for (j = 0;j < numjobs;++j)
520   {
521    jo[j].refs = 0;
522    jo[j].sender.s = 0;
523   }
524 }
525
526 int job_avail()
527 {
528  int j;
529  for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
530  return 0;
531 }
532
533 int job_open(id,channel)
534 unsigned long id;
535 int channel;
536 {
537  int j;
538  for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
539  if (j == numjobs) return -1;
540  jo[j].refs = 1;
541  jo[j].id = id;
542  jo[j].channel = channel;
543  jo[j].numtodo = 0;
544  jo[j].flaghiteof = 0;
545  return j;
546 }
547
548 void job_close(j)
549 int j;
550 {
551  struct prioq_elt pe;
552  struct stat st;
553
554  if (0 < --jo[j].refs) return;
555
556  pe.id = jo[j].id;
557  pe.dt = jo[j].retry;
558  if (jo[j].flaghiteof && !jo[j].numtodo)
559   {
560    fnmake_chanaddr(jo[j].id,jo[j].channel);
561    if (unlink(fn.s) == -1)
562     {
563      log3("warning: unable to unlink ",fn.s,"; will try again later\n");
564      pe.dt = now() + SLEEP_SYSFAIL;
565     }
566    else
567     {
568      int c;
569      for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
570       {
571        fnmake_chanaddr(jo[j].id,c);
572        if (stat(fn.s,&st) == 0) return; /* more channels going */
573        if (errno != error_noent)
574         {
575          log3("warning: unable to stat ",fn.s,"\n");
576          break; /* this is the only reason for HOPEFULLY */
577         }
578       }
579      pe.dt = now();
580      while (!prioq_insert(&pqdone,&pe)) nomem();
581      return;
582     }
583   }
584
585  while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
586 }
587
588
589 /* this file is too long ------------------------------------------- BOUNCES */
590
591 char *stripvdomprepend(recip)
592 char *recip;
593 {
594  int i;
595  char *domain;
596  int domainlen;
597  char *prepend;
598
599  i = str_rchr(recip,'@');
600  if (!recip[i]) return recip;
601  domain = recip + i + 1;
602  domainlen = str_len(domain);
603
604  for (i = 0;i <= domainlen;++i)
605    if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
606      if (prepend = constmap(&mapvdoms,domain + i,domainlen - i))
607       {
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;
613       }
614  return recip;
615 }
616
617 stralloc bouncetext = {0};
618
619 void addbounce(id,recip,report)
620 unsigned long id;
621 char *recip;
622 char *report;
623 {
624  int fd;
625  int pos;
626  int w;
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();
634  if (report[0])
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();
642  fnmake2_bounce(id);
643  for (;;)
644   {
645    fd = open_append(fn2.s);
646    if (fd != -1) break;
647    log1("alert: unable to append to bounce message; HELP! sleeping...\n");
648    sleep(10);
649   }
650  pos = 0;
651  while (pos < bouncetext.len)
652   {
653    w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
654    if (w <= 0)
655     {
656      log1("alert: unable to append to bounce message; HELP! sleeping...\n");
657      sleep(10);
658     }
659    else
660      pos += w;
661   }
662  close(fd);
663 }
664
665 int injectbounce(id)
666 unsigned long id;
667 {
668  struct qmail qqt;
669  struct stat st;
670  char *bouncesender;
671  char *bouncerecip;
672  int r;
673  int fd;
674  substdio ssread;
675  char buf[128];
676  char inbuf[128];
677  static stralloc sender = {0};
678  static stralloc quoted = {0};
679  datetime_sec birth;
680  unsigned long qp;
681
682  if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
683
684  /* owner-@host-@[] -> owner-@host */
685  if (sender.len >= 5)
686    if (str_equal(sender.s + sender.len - 5,"-@[]"))
687     {
688      sender.len -= 4;
689      sender.s[sender.len - 1] = 0;
690     }
691
692  fnmake2_bounce(id);
693  fnmake_mess(id);
694  if (stat(fn2.s,&st) == -1)
695   {
696    if (errno == error_noent)
697      return 1;
698    log3("warning: unable to stat ",fn2.s,"\n");
699    return 0;
700   }
701  if (str_equal(sender.s,"#@[]"))
702    log3("triple bounce: discarding ",fn2.s,"\n");
703  else
704   {
705    if (qmail_open(&qqt) == -1)
706     { log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
707    qp = qmail_qp(&qqt);
708
709    if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
710    else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
711
712    while (!newfield_datemake(now())) nomem();
713    qmail_put(&qqt,newfield_date.s,newfield_date.len);
714    qmail_puts(&qqt,"From: ");
715    while (!quote(&quoted,&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(&quoted,bouncerecip)) nomem();
721    qmail_put(&qqt,quoted.s,quoted.len);
722    qmail_puts(&qqt,"\n\
723 Subject: failure notice\n\
724 \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\
730 \n\
731 " : ".\n\
732 I tried to deliver a bounce message to this address, but the bounce bounced!\n\
733 \n\
734 ");
735
736    fd = open_read(fn2.s);
737    if (fd == -1)
738      qmail_fail(&qqt);
739    else
740     {
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);
744      close(fd);
745      if (r == -1)
746        qmail_fail(&qqt);
747     }
748
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(&quoted,sender.s)) nomem();
752    qmail_put(&qqt,quoted.s,quoted.len);
753    qmail_puts(&qqt,">\n");
754
755    fd = open_read(fn.s);
756    if (fd == -1)
757      qmail_fail(&qqt);
758    else
759     {
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);
763      close(fd);
764      if (r == -1)
765        qmail_fail(&qqt);
766     }
767
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; }
772
773    strnum2[fmt_ulong(strnum2,id)] = 0;
774    log2("bounce msg ",strnum2);
775    strnum2[fmt_ulong(strnum2,qp)] = 0;
776    log3(" qp ",strnum2,"\n");
777   }
778  if (unlink(fn2.s) == -1)
779   {
780    log3("warning: unable to unlink ",fn2.s,"\n");
781    return 0;
782   }
783  return 1;
784 }
785
786
787 /* this file is too long ---------------------------------------- DELIVERIES */
788
789 struct del
790  {
791   int used;
792   int j;
793   unsigned long delid;
794   seek_pos mpos;
795   stralloc recip;
796  }
797 ;
798
799 unsigned long masterdelid = 1;
800 unsigned int concurrency[CHANNELS] = { 10, 20 };
801 struct del *d[CHANNELS];
802 stralloc dline[CHANNELS];
803 char delbuf[2048];
804
805 void del_init()
806 {
807  int c;
808  int i;
809  for (c = 0;c < CHANNELS;++c)
810   {
811    flagspawnalive[c] = 1;
812    while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
813      nomem();
814    for (i = 0;i < concurrency[c];++i)
815     { d[c][i].used = 0; d[c][i].recip.s = 0; }
816    dline[c].s = 0;
817    while (!stralloc_copys(&dline[c],"")) nomem();
818   }
819 }
820
821 int del_canexit()
822 {
823  int i;
824  int c;
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;
829  return 1;
830 }
831
832 static int del_lastsaid = 0;
833
834 void del_saywhynoexit()
835 {
836  int i;
837  int c;
838  int n;
839  n = 0;
840  for (c = 0;c < CHANNELS;++c)
841    if (flagspawnalive[c])
842      for (i = 0;i < concurrency[c];++i)
843        if (d[c][i].used)
844          ++n;
845  if (!del_lastsaid || (n < del_lastsaid))
846   {
847    strnum2[fmt_ulong(strnum2,(unsigned long) n)] = 0;
848    log3("number of deliveries left before exiting: ",strnum2,"\n");
849    del_lastsaid = n;
850   }
851 }
852
853 int del_avail(c)
854 int c;
855 {
856  int i;
857
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;
861  return 0;
862 }
863
864 void del_start(j,mpos,recip)
865 int j;
866 seek_pos mpos;
867 char *recip;
868 {
869  int i;
870  int c;
871
872  c = jo[j].channel;
873  if (!flagspawnalive[c]) return;
874  if (!comm_canwrite(c)) return;
875
876  for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
877  if (i == concurrency[c]) return;
878
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++;
883  d[c][i].mpos = mpos;
884  d[c][i].used = 1;
885
886  comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
887
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]);
892  logsafe(recip);
893  log1("\n");
894 }
895
896 void markdone(c,id,pos)
897 int c;
898 unsigned long id;
899 seek_pos pos;
900 {
901  struct stat st;
902  int fd;
903  fnmake_chanaddr(id,c);
904  for (;;)
905   {
906    fd = open_write(fn.s);
907    if (fd == -1) break;
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 */
912    close(fd);
913    return;
914   }
915  log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
916 }
917
918 void del_dochan(c)
919 int c;
920 {
921  int r;
922  char ch;
923  int i;
924  int delnum;
925  r = read(chanfdin[c],delbuf,sizeof(delbuf));
926  if (r == -1) return;
927  if (r == 0) { spawndied(c); return; }
928  for (i = 0;i < r;++i)
929   {
930    ch = delbuf[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))
937     {
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");
941      else
942       {
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)
946           {
947            dline[c].s[1] = 'D';
948            --dline[c].len;
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();
951           }
952        switch(dline[c].s[1])
953         {
954          case 'K':
955            log3("delivery ",strnum3,": success: ");
956            logsafe(dline[c].s + 2);
957            log1("\n");
958            markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
959            --jo[d[c][delnum].j].numtodo;
960            break;
961          case 'Z':
962            log3("delivery ",strnum3,": deferral: ");
963            logsafe(dline[c].s + 2);
964            log1("\n");
965            break;
966          case 'D':
967            log3("delivery ",strnum3,": failure: ");
968            logsafe(dline[c].s + 2);
969            log1("\n");
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;
973            break;
974          default:
975            log3("delivery ",strnum3,": report mangled, will defer\n");
976         }
977        job_close(d[c][delnum].j);
978        d[c][delnum].used = 0;
979       }
980      dline[c].len = 0;
981     }
982   }
983 }
984
985 void del_selprep(nfds,rfds)
986 int *nfds;
987 fd_set *rfds;
988 {
989  int c;
990  for (c = 0;c < CHANNELS;++c)
991    if (flagspawnalive[c])
992     {
993      FD_SET(chanfdin[c],rfds);
994      if (*nfds <= chanfdin[c])
995        *nfds = chanfdin[c] + 1;
996     }
997 }
998
999 void del_do(rfds)
1000 fd_set *rfds;
1001 {
1002  int c;
1003  for (c = 0;c < CHANNELS;++c)
1004    if (flagspawnalive[c])
1005      if (FD_ISSET(chanfdin[c],rfds))
1006        del_dochan(c);
1007 }
1008
1009
1010 /* this file is too long -------------------------------------------- PASSES */
1011
1012 struct
1013  {
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 */
1018   substdio ss;
1019   char buf[128];
1020  }
1021 pass[CHANNELS];
1022
1023 void pass_init()
1024 {
1025  int c;
1026  for (c = 0;c < CHANNELS;++c) pass[c].id = 0;
1027 }
1028
1029 void pass_selprep(wakeup)
1030 datetime_sec *wakeup;
1031 {
1032  int c;
1033  struct prioq_elt pe;
1034  if (flagexitasap) return;
1035  for (c = 0;c < CHANNELS;++c)
1036    if (pass[c].id)
1037      if (del_avail(c))
1038       { *wakeup = 0; return; }
1039  if (job_avail())
1040    for (c = 0;c < CHANNELS;++c)
1041      if (!pass[c].id)
1042        if (prioq_min(&pqchan[c],&pe))
1043          if (*wakeup > pe.dt)
1044            *wakeup = pe.dt;
1045  if (prioq_min(&pqfail,&pe))
1046    if (*wakeup > pe.dt)
1047      *wakeup = pe.dt;
1048  if (prioq_min(&pqdone,&pe))
1049    if (*wakeup > pe.dt)
1050      *wakeup = pe.dt;
1051 }
1052
1053 static datetime_sec squareroot(x) /* result^2 <= x < (result + 1)^2 */
1054 datetime_sec x; /* assuming: >= 0 */
1055 {
1056  datetime_sec y;
1057  datetime_sec yy;
1058  datetime_sec y21;
1059  int j;
1060
1061  y = 0; yy = 0;
1062  for (j = 15;j >= 0;--j)
1063   {
1064    y21 = (y << (j + 1)) + (1 << (j + j));
1065    if (y21 <= x - yy) { y += (1 << j); yy += y21; }
1066   }
1067  return y;
1068 }
1069
1070 datetime_sec nextretry(birth,c)
1071 datetime_sec birth;
1072 int c;
1073 {
1074  int n;
1075
1076  if (birth > recent) n = 0;
1077  else n = squareroot(recent - birth); /* no need to add fuzz to recent */
1078  n += chanskip[c];
1079  return birth + n * n;
1080 }
1081
1082 void pass_dochan(c)
1083 int c;
1084 {
1085  datetime_sec birth;
1086  struct prioq_elt pe;
1087  static stralloc line = {0};
1088  int match;
1089
1090  if (flagexitasap) return;
1091
1092  if (!pass[c].id)
1093   {
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);
1098
1099    prioq_delmin(&pqchan[c]);
1100    pass[c].mpos = 0;
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; }
1104    pass[c].id = pe.id;
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();
1110   }
1111
1112  if (!del_avail(c)) return;
1113
1114  if (getln(&pass[c].ss,&line,&match,'\0') == -1)
1115   {
1116    fnmake_chanaddr(pass[c].id,c);
1117    log3("warning: trouble reading ",fn.s,"; will try again later\n");
1118    close(pass[c].fd);
1119    job_close(pass[c].j);
1120    pass[c].id = 0;
1121    return;
1122   }
1123  if (!match)
1124   {
1125    close(pass[c].fd);
1126    jo[pass[c].j].flaghiteof = 1;
1127    job_close(pass[c].j);
1128    pass[c].id = 0;
1129    return;
1130   }
1131  switch(line.s[0])
1132   {
1133    case 'T':
1134      ++jo[pass[c].j].numtodo;
1135      del_start(pass[c].j,pass[c].mpos,line.s + 1);
1136      break;
1137    case 'D':
1138      break;
1139    default:
1140      fnmake_chanaddr(pass[c].id,c);
1141      log3("warning: unknown record type in ",fn.s,"!\n");
1142      close(pass[c].fd);
1143      job_close(pass[c].j);
1144      pass[c].id = 0;
1145      return;
1146   }
1147
1148  pass[c].mpos += line.len;
1149  return;
1150
1151  trouble:
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();
1155 }
1156
1157 void messdone(id)
1158 unsigned long id;
1159 {
1160  char ch;
1161  int c;
1162  struct prioq_elt pe;
1163  struct stat st;
1164
1165  for (c = 0;c < CHANNELS;++c)
1166   {
1167    fnmake_chanaddr(id,c);
1168    if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */
1169    if (errno != error_noent)
1170     {
1171      log3("warning: unable to stat ",fn.s,"; will try again later\n");
1172      goto fail;
1173     }
1174   }
1175
1176  fnmake_todo(id);
1177  if (stat(fn.s,&st) == 0) return;
1178  if (errno != error_noent)
1179   {
1180    log3("warning: unable to stat ",fn.s,"; will try again later\n");
1181    goto fail;
1182   }
1183  
1184  fnmake_info(id);
1185  if (stat(fn.s,&st) == -1)
1186   {
1187    if (errno == error_noent) return;
1188    log3("warning: unable to stat ",fn.s,"; will try again later\n");
1189    goto fail;
1190   }
1191  
1192  /* -todo +info -local -remote ?bounce */
1193  if (!injectbounce(id))
1194    goto fail; /* injectbounce() produced error message */
1195
1196  strnum3[fmt_ulong(strnum3,id)] = 0;
1197  log3("end msg ",strnum3,"\n");
1198
1199  /* -todo +info -local -remote -bounce */
1200  fnmake_info(id);
1201  if (unlink(fn.s) == -1)
1202   {
1203    log3("warning: unable to unlink ",fn.s,"; will try again later\n");
1204    goto fail;
1205   }
1206
1207  /* -todo -info -local -remote -bounce; we can relax */
1208  fnmake_foop(id);
1209  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1210  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1211  if (ch != '+')
1212    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1213
1214  return;
1215
1216  fail:
1217  pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
1218  while (!prioq_insert(&pqdone,&pe)) nomem();
1219 }
1220
1221 void pass_do()
1222 {
1223  int c;
1224  struct prioq_elt pe;
1225
1226  for (c = 0;c < CHANNELS;++c) pass_dochan(c);
1227  if (prioq_min(&pqfail,&pe))
1228    if (pe.dt <= recent)
1229     {
1230      prioq_delmin(&pqfail);
1231      pqadd(pe.id);
1232     }
1233  if (prioq_min(&pqdone,&pe))
1234    if (pe.dt <= recent)
1235     {
1236      prioq_delmin(&pqdone);
1237      messdone(pe.id);
1238     }
1239 }
1240
1241
1242 /* this file is too long ---------------------------------------------- TODO */
1243
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];
1250
1251 void todo_init()
1252 {
1253  tododir = 0;
1254  nexttodorun = now();
1255  trigger_set();
1256 }
1257
1258 void todo_selprep(nfds,rfds,wakeup)
1259 int *nfds;
1260 fd_set *rfds;
1261 datetime_sec *wakeup;
1262 {
1263  if (flagexitasap) return;
1264  trigger_selprep(nfds,rfds);
1265  if (tododir) *wakeup = 0;
1266  if (*wakeup > nexttodorun) *wakeup = nexttodorun;
1267 }
1268
1269 void todo_do(rfds)
1270 fd_set *rfds;
1271 {
1272  struct stat st;
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;
1279  char ch;
1280  int match;
1281  unsigned long id;
1282  unsigned int len;
1283  direntry *d;
1284  int c;
1285  unsigned long uid;
1286  unsigned long pid;
1287
1288  fd = -1;
1289  fdinfo = -1;
1290  for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
1291
1292  if (flagexitasap) return;
1293
1294  if (!tododir)
1295   {
1296    if (!trigger_pulled(rfds))
1297      if (recent < nexttodorun)
1298        return;
1299    trigger_set();
1300    tododir = opendir("todo");
1301    if (!tododir)
1302     {
1303      pausedir("todo");
1304      return;
1305     }
1306    nexttodorun = recent + SLEEP_TODO;
1307   }
1308
1309  d = readdir(tododir);
1310  if (!d)
1311   {
1312    closedir(tododir);
1313    tododir = 0;
1314    return;
1315   }
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;
1320
1321  fnmake_todo(id);
1322
1323  fd = open_read(fn.s);
1324  if (fd == -1) { log3("warning: unable to open ",fn.s,"\n"); return; }
1325
1326  fnmake_mess(id);
1327  /* just for the statistics */
1328  if (stat(fn.s,&st) == -1)
1329   { log3("warning: unable to stat ",fn.s,"\n"); goto fail; }
1330
1331  for (c = 0;c < CHANNELS;++c)
1332   {
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; }
1336   }
1337
1338  fnmake_info(id);
1339  if (unlink(fn.s) == -1) if (errno != error_noent)
1340   { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
1341
1342  fdinfo = open_excl(fn.s);
1343  if (fdinfo == -1)
1344   { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
1345
1346  strnum3[fmt_ulong(strnum3,id)] = 0;
1347  log3("new msg ",strnum3,"\n");
1348
1349  for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
1350
1351  substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
1352  substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
1353
1354  uid = 0;
1355  pid = 0;
1356
1357  for (;;)
1358   {
1359    if (getln(&ss,&todoline,&match,'\0') == -1)
1360     {
1361      /* perhaps we're out of memory, perhaps an I/O error */
1362      fnmake_todo(id);
1363      log3("warning: trouble reading ",fn.s,"\n"); goto fail;
1364     }
1365    if (!match) break;
1366
1367    switch(todoline.s[0])
1368     {
1369      case 'u':
1370        scan_ulong(todoline.s + 1,&uid);
1371        break;
1372      case 'p':
1373        scan_ulong(todoline.s + 1,&pid);
1374        break;
1375      case 'F':
1376        if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
1377         {
1378          fnmake_info(id);
1379          log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1380         }
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);
1389        log1("\n");
1390        break;
1391      case 'T':
1392        switch(rewrite(todoline.s + 1))
1393         {
1394          case 0: nomem(); goto fail;
1395          case 2: c = 1; break;
1396          default: c = 0; break;
1397         }
1398        if (fdchan[c] == -1)
1399         {
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]));
1406          flagchan[c] = 1;
1407         }
1408        if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
1409         {
1410          fnmake_chanaddr(id,c);
1411          log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
1412         }
1413        break;
1414      default:
1415        fnmake_todo(id);
1416        log3("warning: unknown record type in ",fn.s,"\n"); goto fail;
1417     }
1418   }
1419
1420  close(fd); fd = -1;
1421
1422  fnmake_info(id);
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;
1428
1429  for (c = 0;c < CHANNELS;++c)
1430    if (fdchan[c] != -1)
1431     {
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;
1438     }
1439
1440  fnmake_todo(id);
1441  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
1442  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
1443  if (ch != '+')
1444   {
1445    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
1446    return;
1447   }
1448
1449  pe.id = id; pe.dt = now();
1450  for (c = 0;c < CHANNELS;++c)
1451    if (flagchan[c])
1452      while (!prioq_insert(&pqchan[c],&pe)) nomem();
1453
1454  for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
1455  if (c == CHANNELS)
1456    while (!prioq_insert(&pqdone,&pe)) nomem();
1457
1458  return;
1459
1460  fail:
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]);
1465 }
1466
1467
1468 /* this file is too long ---------------------------------------------- MAIN */
1469
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))
1485   {
1486    case -1: return 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;
1489   }
1490  switch(control_readfile(&redir,"control/recipientmap",0))
1491   {
1492    case -1: return 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;
1495   }
1496  switch(control_readfile(&vdoms,"control/virtualdomains",0))
1497   {
1498    case -1: return 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;
1501   }
1502  return 1; }
1503
1504 stralloc newlocals = {0};
1505 stralloc newvdoms = {0};
1506
1507 void regetcontrols()
1508 {
1509  int r;
1510
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);
1514  if (r == -1)
1515   { log1("alert: unable to reread control/virtualdomains\n"); return; }
1516
1517  constmap_free(&maplocals);
1518  constmap_free(&mapvdoms);
1519
1520  while (!stralloc_copy(&locals,&newlocals)) nomem();
1521  while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
1522
1523  if (r)
1524   {
1525    while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
1526    while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
1527   }
1528  else
1529    while (!constmap_init(&mapvdoms,"",0,1)) nomem();
1530 }
1531
1532 void reread()
1533 {
1534  if (chdir(auto_qmail) == -1)
1535   {
1536    log1("alert: unable to reread controls: unable to switch to home directory\n");
1537    return;
1538   }
1539  regetcontrols();
1540  while (chdir("queue") == -1)
1541   {
1542    log1("alert: unable to switch back to queue directory; HELP! sleeping...\n");
1543    sleep(10);
1544   }
1545 }
1546
1547 void main()
1548 {
1549  int fd;
1550  datetime_sec wakeup;
1551  fd_set rfds;
1552  fd_set wfds;
1553  int nfds;
1554  struct timeval tv;
1555  int c;
1556
1557  if (chdir(auto_qmail) == -1)
1558   { log1("alert: cannot start: unable to switch to home directory\n"); _exit(111); }
1559  if (!getcontrols())
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); }
1563  sig_pipeignore();
1564  sig_termcatch(sigterm);
1565  sig_alarmcatch(sigalrm);
1566  sig_hangupcatch(sighup);
1567  sig_childdefault();
1568  umask(077);
1569
1570  fd = open_write("lock/sendmutex");
1571  if (fd == -1)
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); }
1575
1576  numjobs = 0;
1577  for (c = 0;c < CHANNELS;++c)
1578   {
1579    char ch;
1580    int u;
1581    int r;
1582    do
1583      r = read(chanfdin[c],&ch,1);
1584    while ((r == -1) && (errno == error_intr));
1585    if (r < 1)
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];
1590   }
1591
1592  log1("running\n");
1593
1594  fnmake_init();
1595
1596  for (c = 0;c < CHANNELS;++c)
1597    if (!concurrency[c])
1598      log1(channodelmsg[c]);
1599
1600  comm_init();
1601
1602  pqstart();
1603  job_init();
1604  del_init();
1605  pass_init();
1606  todo_init();
1607  cleanup_init();
1608
1609  while (!flagexitasap || !del_canexit())
1610   {
1611    if (flagexitasap) del_saywhynoexit();
1612
1613    recent = now();
1614
1615    if (flagrunasap) { flagrunasap = 0; pqrun(); }
1616    if (flagreadasap) { flagreadasap = 0; reread(); }
1617
1618    wakeup = recent + SLEEP_FOREVER;
1619    FD_ZERO(&rfds);
1620    FD_ZERO(&wfds);
1621    nfds = 1;
1622
1623    comm_selprep(&nfds,&wfds);
1624    del_selprep(&nfds,&rfds);
1625    pass_selprep(&wakeup);
1626    todo_selprep(&nfds,&rfds,&wakeup);
1627    cleanup_selprep(&wakeup);
1628
1629    if (wakeup <= recent) tv.tv_sec = 0;
1630    else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
1631    tv.tv_usec = 0;
1632
1633    if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1)
1634      if (errno == error_intr)
1635        ;
1636      else
1637        log1("warning: trouble in select\n");
1638    else
1639     {
1640      recent = now();
1641
1642      comm_do(&wfds);
1643      del_do(&rfds);
1644      todo_do(&rfds);
1645      pass_do();
1646      cleanup_do();
1647     }
1648   }
1649  pqfinish();
1650  log1("exiting\n");
1651  _exit(0);
1652 }