qmail-send.c
changeset 0 068428edee47
equal deleted inserted replaced
-1:000000000000 0:068428edee47
       
     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 vdoms = {0};
       
    52 struct constmap mapvdoms;
       
    53 stralloc envnoathost = {0};
       
    54 stralloc bouncefrom = {0};
       
    55 stralloc bouncehost = {0};
       
    56 stralloc doublebounceto = {0};
       
    57 stralloc doublebouncehost = {0};
       
    58 
       
    59 char strnum2[FMT_ULONG];
       
    60 char strnum3[FMT_ULONG];
       
    61 
       
    62 #define CHANNELS 2
       
    63 char *chanaddr[CHANNELS] = { "local/", "remote/" };
       
    64 char *chanstatusmsg[CHANNELS] = { " local ", " remote " };
       
    65 char *tochan[CHANNELS] = { " to local ", " to remote " };
       
    66 int chanfdout[CHANNELS] = { 1, 3 };
       
    67 int chanfdin[CHANNELS] = { 2, 4 };
       
    68 int chanskip[CHANNELS] = { 10, 20 };
       
    69 
       
    70 int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
       
    71 int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
       
    72 int flagreadasap = 0; void sighup() { flagreadasap = 1; }
       
    73 
       
    74 void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
       
    75  flagexitasap = 1; }
       
    76 
       
    77 int flagspawnalive[CHANNELS];
       
    78 void spawndied(c) int c; { log1("alert: oh no! lost spawn connection! dying...\n");
       
    79  flagspawnalive[c] = 0; flagexitasap = 1; }
       
    80 
       
    81 #define REPORTMAX 10000
       
    82 
       
    83 datetime_sec recent;
       
    84 
       
    85 
       
    86 /* this file is too long ----------------------------------------- FILENAMES */
       
    87 
       
    88 stralloc fn = {0};
       
    89 stralloc fn2 = {0};
       
    90 char fnmake_strnum[FMT_ULONG];
       
    91 
       
    92 void fnmake_init()
       
    93 {
       
    94  while (!stralloc_ready(&fn,FMTQFN)) nomem();
       
    95  while (!stralloc_ready(&fn2,FMTQFN)) nomem();
       
    96 }
       
    97 
       
    98 void fnmake_info(id) unsigned long id; { fn.len = fmtqfn(fn.s,"info/",id,1); }
       
    99 void fnmake_todo(id) unsigned long id; { fn.len = fmtqfn(fn.s,"todo/",id,0); }
       
   100 void fnmake_mess(id) unsigned long id; { fn.len = fmtqfn(fn.s,"mess/",id,1); }
       
   101 void fnmake_foop(id) unsigned long id; { fn.len = fmtqfn(fn.s,"foop/",id,0); }
       
   102 void fnmake_split(id) unsigned long id; { fn.len = fmtqfn(fn.s,"",id,1); }
       
   103 void fnmake2_bounce(id) unsigned long id;
       
   104 { fn2.len = fmtqfn(fn2.s,"bounce/",id,0); }
       
   105 void fnmake_chanaddr(id,c) unsigned long id; int c;
       
   106 { fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
       
   107 
       
   108 
       
   109 /* this file is too long ----------------------------------------- REWRITING */
       
   110 
       
   111 stralloc rwline = {0};
       
   112 
       
   113 /* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
       
   114 /* may trash recip. must set up rwline, between a T and a \0. */
       
   115 int rewrite(recip)
       
   116 char *recip;
       
   117 {
       
   118   int i;
       
   119   int j;
       
   120   char *x;
       
   121   static stralloc addr = {0};
       
   122   int at;
       
   123 
       
   124   if (!stralloc_copys(&rwline,"T")) return 0;
       
   125   if (!stralloc_copys(&addr,recip)) return 0;
       
   126 
       
   127   i = byte_rchr(addr.s,addr.len,'@');
       
   128   if (i == addr.len) {
       
   129     if (!stralloc_cats(&addr,"@")) return 0;
       
   130     if (!stralloc_cat(&addr,&envnoathost)) return 0;
       
   131   }
       
   132 
       
   133   while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
       
   134     j = byte_rchr(addr.s,i,'%');
       
   135     if (j == i) break;
       
   136     addr.len = i;
       
   137     i = j;
       
   138     addr.s[i] = '@';
       
   139   }
       
   140 
       
   141   at = byte_rchr(addr.s,addr.len,'@');
       
   142 
       
   143   if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) {
       
   144     if (!stralloc_cat(&rwline,&addr)) return 0;
       
   145     if (!stralloc_0(&rwline)) return 0;
       
   146     return 1;
       
   147   }
       
   148 
       
   149   for (i = 0;i <= addr.len;++i)
       
   150     if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.')))
       
   151       if (x = constmap(&mapvdoms,addr.s + i,addr.len - i)) {
       
   152         if (!*x) break;
       
   153         if (!stralloc_cats(&rwline,x)) return 0;
       
   154         if (!stralloc_cats(&rwline,"-")) return 0;
       
   155         if (!stralloc_cat(&rwline,&addr)) return 0;
       
   156         if (!stralloc_0(&rwline)) return 0;
       
   157         return 1;
       
   158       }
       
   159  
       
   160   if (!stralloc_cat(&rwline,&addr)) return 0;
       
   161   if (!stralloc_0(&rwline)) return 0;
       
   162   return 2;
       
   163 }
       
   164 
       
   165 void senderadd(sa,sender,recip)
       
   166 stralloc *sa;
       
   167 char *sender;
       
   168 char *recip;
       
   169 {
       
   170  int i;
       
   171  int j;
       
   172  int k;
       
   173 
       
   174  i = str_len(sender);
       
   175  if (i >= 4)
       
   176    if (str_equal(sender + i - 4,"-@[]"))
       
   177     {
       
   178      j = byte_rchr(sender,i - 4,'@');
       
   179      k = str_rchr(recip,'@');
       
   180      if (recip[k] && (j + 5 <= i))
       
   181       {
       
   182        /* owner-@host-@[] -> owner-recipbox=reciphost@host */
       
   183        while (!stralloc_catb(sa,sender,j)) nomem();
       
   184        while (!stralloc_catb(sa,recip,k)) nomem();
       
   185        while (!stralloc_cats(sa,"=")) nomem();
       
   186        while (!stralloc_cats(sa,recip + k + 1)) nomem();
       
   187        while (!stralloc_cats(sa,"@")) nomem();
       
   188        while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem();
       
   189        return;
       
   190       }
       
   191     }
       
   192  while (!stralloc_cats(sa,sender)) nomem();
       
   193 }
       
   194 
       
   195 
       
   196 /* this file is too long ---------------------------------------------- INFO */
       
   197 
       
   198 int getinfo(sa,dt,id)
       
   199 stralloc *sa;
       
   200 datetime_sec *dt;
       
   201 unsigned long id;
       
   202 {
       
   203  int fdinfo;
       
   204  struct stat st;
       
   205  static stralloc line = {0};
       
   206  int match;
       
   207  substdio ss;
       
   208  char buf[128];
       
   209 
       
   210  fnmake_info(id);
       
   211  fdinfo = open_read(fn.s);
       
   212  if (fdinfo == -1) return 0;
       
   213  if (fstat(fdinfo,&st) == -1) { close(fdinfo); return 0; }
       
   214  substdio_fdbuf(&ss,read,fdinfo,buf,sizeof(buf));
       
   215  if (getln(&ss,&line,&match,'\0') == -1) { close(fdinfo); return 0; }
       
   216  close(fdinfo);
       
   217  if (!match) return 0;
       
   218  if (line.s[0] != 'F') return 0;
       
   219 
       
   220  *dt = st.st_mtime;
       
   221  while (!stralloc_copys(sa,line.s + 1)) nomem();
       
   222  while (!stralloc_0(sa)) nomem();
       
   223  return 1;
       
   224 }
       
   225 
       
   226 
       
   227 /* this file is too long ------------------------------------- COMMUNICATION */
       
   228 
       
   229 substdio sstoqc; char sstoqcbuf[1024];
       
   230 substdio ssfromqc; char ssfromqcbuf[1024];
       
   231 stralloc comm_buf[CHANNELS] = { {0}, {0} };
       
   232 int comm_pos[CHANNELS];
       
   233 
       
   234 void comm_init()
       
   235 {
       
   236  int c;
       
   237  substdio_fdbuf(&sstoqc,write,5,sstoqcbuf,sizeof(sstoqcbuf));
       
   238  substdio_fdbuf(&ssfromqc,read,6,ssfromqcbuf,sizeof(ssfromqcbuf));
       
   239  for (c = 0;c < CHANNELS;++c)
       
   240    if (ndelay_on(chanfdout[c]) == -1)
       
   241    /* this is so stupid: NDELAY semantics should be default on write */
       
   242      spawndied(c); /* drastic, but better than risking deadlock */
       
   243 }
       
   244 
       
   245 int comm_canwrite(c)
       
   246 int c;
       
   247 {
       
   248  /* XXX: could allow a bigger buffer; say 10 recipients */
       
   249  if (comm_buf[c].s && comm_buf[c].len) return 0;
       
   250  return 1;
       
   251 }
       
   252 
       
   253 void comm_write(c,delnum,id,sender,recip)
       
   254 int c;
       
   255 int delnum;
       
   256 unsigned long id;
       
   257 char *sender;
       
   258 char *recip;
       
   259 {
       
   260  char ch;
       
   261  if (comm_buf[c].s && comm_buf[c].len) return;
       
   262  while (!stralloc_copys(&comm_buf[c],"")) nomem();
       
   263  ch = delnum;
       
   264  while (!stralloc_append(&comm_buf[c],&ch)) nomem();
       
   265  fnmake_split(id);
       
   266  while (!stralloc_cats(&comm_buf[c],fn.s)) nomem();
       
   267  while (!stralloc_0(&comm_buf[c])) nomem();
       
   268  senderadd(&comm_buf[c],sender,recip);
       
   269  while (!stralloc_0(&comm_buf[c])) nomem();
       
   270  while (!stralloc_cats(&comm_buf[c],recip)) nomem();
       
   271  while (!stralloc_0(&comm_buf[c])) nomem();
       
   272  comm_pos[c] = 0;
       
   273 }
       
   274 
       
   275 void comm_selprep(nfds,wfds)
       
   276 int *nfds;
       
   277 fd_set *wfds;
       
   278 {
       
   279  int c;
       
   280  for (c = 0;c < CHANNELS;++c)
       
   281    if (flagspawnalive[c])
       
   282      if (comm_buf[c].s && comm_buf[c].len)
       
   283       {
       
   284        FD_SET(chanfdout[c],wfds);
       
   285        if (*nfds <= chanfdout[c])
       
   286          *nfds = chanfdout[c] + 1;
       
   287       }
       
   288 }
       
   289 
       
   290 void comm_do(wfds)
       
   291 fd_set *wfds;
       
   292 {
       
   293  int c;
       
   294  for (c = 0;c < CHANNELS;++c)
       
   295    if (flagspawnalive[c])
       
   296      if (comm_buf[c].s && comm_buf[c].len)
       
   297        if (FD_ISSET(chanfdout[c],wfds))
       
   298         {
       
   299          int w;
       
   300          int len;
       
   301          len = comm_buf[c].len;
       
   302          w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
       
   303          if (w <= 0)
       
   304 	  {
       
   305 	   if ((w == -1) && (errno == error_pipe))
       
   306 	     spawndied(c);
       
   307 	   else
       
   308 	     continue; /* kernel select() bug; can't avoid busy-looping */
       
   309 	  }
       
   310 	 else
       
   311 	  {
       
   312 	   comm_pos[c] += w;
       
   313 	   if (comm_pos[c] == len)
       
   314 	     comm_buf[c].len = 0;
       
   315 	  }
       
   316         }
       
   317 }
       
   318 
       
   319 
       
   320 /* this file is too long ------------------------------------------ CLEANUPS */
       
   321 
       
   322 int flagcleanup; /* if 1, cleanupdir is initialized and ready */
       
   323 readsubdir cleanupdir;
       
   324 datetime_sec cleanuptime;
       
   325 
       
   326 void cleanup_init()
       
   327 {
       
   328  flagcleanup = 0;
       
   329  cleanuptime = now();
       
   330 }
       
   331 
       
   332 void cleanup_selprep(wakeup)
       
   333 datetime_sec *wakeup;
       
   334 {
       
   335  if (flagcleanup) *wakeup = 0;
       
   336  if (*wakeup > cleanuptime) *wakeup = cleanuptime;
       
   337 }
       
   338 
       
   339 void cleanup_do()
       
   340 {
       
   341  char ch;
       
   342  struct stat st;
       
   343  unsigned long id;
       
   344 
       
   345  if (!flagcleanup)
       
   346   {
       
   347    if (recent < cleanuptime) return;
       
   348    readsubdir_init(&cleanupdir,"mess",pausedir);
       
   349    flagcleanup = 1;
       
   350   }
       
   351 
       
   352  switch(readsubdir_next(&cleanupdir,&id))
       
   353   {
       
   354    case 1:
       
   355      break;
       
   356    case 0:
       
   357      flagcleanup = 0;
       
   358      cleanuptime = recent + SLEEP_CLEANUP;
       
   359    default:
       
   360      return;
       
   361   }
       
   362 
       
   363  fnmake_mess(id);
       
   364  if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
       
   365  if (recent <= st.st_atime + OSSIFIED) return;
       
   366 
       
   367  fnmake_info(id);
       
   368  if (stat(fn.s,&st) == 0) return;
       
   369  if (errno != error_noent) return;
       
   370  fnmake_todo(id);
       
   371  if (stat(fn.s,&st) == 0) return;
       
   372  if (errno != error_noent) return;
       
   373 
       
   374  fnmake_foop(id);
       
   375  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
       
   376  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
       
   377  if (ch != '+')
       
   378    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
       
   379 }
       
   380 
       
   381 
       
   382 /* this file is too long ----------------------------------- PRIORITY QUEUES */
       
   383 
       
   384 prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */
       
   385 prioq pqchan[CHANNELS] = { {0}, {0} };
       
   386 /* pqchan 0: -todo +info +local ?remote */
       
   387 /* pqchan 1: -todo +info ?local +remote */
       
   388 prioq pqfail = {0}; /* stat() failure; has to be pqadded again */
       
   389 
       
   390 void pqadd(id)
       
   391 unsigned long id;
       
   392 {
       
   393  struct prioq_elt pe;
       
   394  struct prioq_elt pechan[CHANNELS];
       
   395  int flagchan[CHANNELS];
       
   396  struct stat st;
       
   397  int c;
       
   398 
       
   399 #define CHECKSTAT if (errno != error_noent) goto fail;
       
   400 
       
   401  fnmake_info(id);
       
   402  if (stat(fn.s,&st) == -1)
       
   403   {
       
   404    CHECKSTAT
       
   405    return; /* someone yanking our chain */
       
   406   }
       
   407 
       
   408  fnmake_todo(id);
       
   409  if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
       
   410  CHECKSTAT
       
   411 
       
   412  for (c = 0;c < CHANNELS;++c)
       
   413   {
       
   414    fnmake_chanaddr(id,c);
       
   415    if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT }
       
   416    else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; }
       
   417   }
       
   418 
       
   419  for (c = 0;c < CHANNELS;++c)
       
   420    if (flagchan[c])
       
   421      while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
       
   422 
       
   423  for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
       
   424  if (c == CHANNELS)
       
   425   {
       
   426    pe.id = id; pe.dt = now();
       
   427    while (!prioq_insert(&pqdone,&pe)) nomem();
       
   428   }
       
   429 
       
   430  return;
       
   431 
       
   432  fail:
       
   433  log3("warning: unable to stat ",fn.s,"; will try again later\n");
       
   434  pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
       
   435  while (!prioq_insert(&pqfail,&pe)) nomem();
       
   436 }
       
   437 
       
   438 void pqstart()
       
   439 {
       
   440  readsubdir rs;
       
   441  int x;
       
   442  unsigned long id;
       
   443 
       
   444  readsubdir_init(&rs,"info",pausedir);
       
   445 
       
   446  while (x = readsubdir_next(&rs,&id))
       
   447    if (x > 0)
       
   448      pqadd(id);
       
   449 }
       
   450 
       
   451 void pqfinish()
       
   452 {
       
   453  int c;
       
   454  struct prioq_elt pe;
       
   455  time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */
       
   456 
       
   457  for (c = 0;c < CHANNELS;++c)
       
   458    while (prioq_min(&pqchan[c],&pe))
       
   459     {
       
   460      prioq_delmin(&pqchan[c]);
       
   461      fnmake_chanaddr(pe.id,c);
       
   462      ut[0] = ut[1] = pe.dt;
       
   463      if (utime(fn.s,ut) == -1)
       
   464        log3("warning: unable to utime ",fn.s,"; message will be retried too soon\n");
       
   465     }
       
   466 }
       
   467 
       
   468 void pqrun()
       
   469 {
       
   470  int c;
       
   471  int i;
       
   472  for (c = 0;c < CHANNELS;++c)
       
   473    if (pqchan[c].p)
       
   474      if (pqchan[c].len)
       
   475        for (i = 0;i < pqchan[c].len;++i)
       
   476 	 pqchan[c].p[i].dt = recent;
       
   477 }
       
   478 
       
   479 
       
   480 /* this file is too long ---------------------------------------------- JOBS */
       
   481 
       
   482 struct job
       
   483  {
       
   484   int refs; /* if 0, this struct is unused */
       
   485   unsigned long id;
       
   486   int channel;
       
   487   datetime_sec retry;
       
   488   stralloc sender;
       
   489   int numtodo;
       
   490   int flaghiteof;
       
   491   int flagdying;
       
   492  }
       
   493 ;
       
   494 
       
   495 int numjobs;
       
   496 struct job *jo;
       
   497 
       
   498 void job_init()
       
   499 {
       
   500  int j;
       
   501  while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
       
   502  for (j = 0;j < numjobs;++j)
       
   503   {
       
   504    jo[j].refs = 0;
       
   505    jo[j].sender.s = 0;
       
   506   }
       
   507 }
       
   508 
       
   509 int job_avail()
       
   510 {
       
   511  int j;
       
   512  for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
       
   513  return 0;
       
   514 }
       
   515 
       
   516 int job_open(id,channel)
       
   517 unsigned long id;
       
   518 int channel;
       
   519 {
       
   520  int j;
       
   521  for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
       
   522  if (j == numjobs) return -1;
       
   523  jo[j].refs = 1;
       
   524  jo[j].id = id;
       
   525  jo[j].channel = channel;
       
   526  jo[j].numtodo = 0;
       
   527  jo[j].flaghiteof = 0;
       
   528  return j;
       
   529 }
       
   530 
       
   531 void job_close(j)
       
   532 int j;
       
   533 {
       
   534  struct prioq_elt pe;
       
   535  struct stat st;
       
   536 
       
   537  if (0 < --jo[j].refs) return;
       
   538 
       
   539  pe.id = jo[j].id;
       
   540  pe.dt = jo[j].retry;
       
   541  if (jo[j].flaghiteof && !jo[j].numtodo)
       
   542   {
       
   543    fnmake_chanaddr(jo[j].id,jo[j].channel);
       
   544    if (unlink(fn.s) == -1)
       
   545     {
       
   546      log3("warning: unable to unlink ",fn.s,"; will try again later\n");
       
   547      pe.dt = now() + SLEEP_SYSFAIL;
       
   548     }
       
   549    else
       
   550     {
       
   551      int c;
       
   552      for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
       
   553       {
       
   554        fnmake_chanaddr(jo[j].id,c);
       
   555        if (stat(fn.s,&st) == 0) return; /* more channels going */
       
   556        if (errno != error_noent)
       
   557 	{
       
   558          log3("warning: unable to stat ",fn.s,"\n");
       
   559 	 break; /* this is the only reason for HOPEFULLY */
       
   560 	}
       
   561       }
       
   562      pe.dt = now();
       
   563      while (!prioq_insert(&pqdone,&pe)) nomem();
       
   564      return;
       
   565     }
       
   566   }
       
   567 
       
   568  while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
       
   569 }
       
   570 
       
   571 
       
   572 /* this file is too long ------------------------------------------- BOUNCES */
       
   573 
       
   574 char *stripvdomprepend(recip)
       
   575 char *recip;
       
   576 {
       
   577  int i;
       
   578  char *domain;
       
   579  int domainlen;
       
   580  char *prepend;
       
   581 
       
   582  i = str_rchr(recip,'@');
       
   583  if (!recip[i]) return recip;
       
   584  domain = recip + i + 1;
       
   585  domainlen = str_len(domain);
       
   586 
       
   587  for (i = 0;i <= domainlen;++i)
       
   588    if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
       
   589      if (prepend = constmap(&mapvdoms,domain + i,domainlen - i))
       
   590       {
       
   591        if (!*prepend) break;
       
   592        i = str_len(prepend);
       
   593        if (str_diffn(recip,prepend,i)) break;
       
   594        if (recip[i] != '-') break;
       
   595        return recip + i + 1;
       
   596       }
       
   597  return recip;
       
   598 }
       
   599 
       
   600 stralloc bouncetext = {0};
       
   601 
       
   602 void addbounce(id,recip,report)
       
   603 unsigned long id;
       
   604 char *recip;
       
   605 char *report;
       
   606 {
       
   607  int fd;
       
   608  int pos;
       
   609  int w;
       
   610  while (!stralloc_copys(&bouncetext,"<")) nomem();
       
   611  while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem();
       
   612  for (pos = 0;pos < bouncetext.len;++pos)
       
   613    if (bouncetext.s[pos] == '\n')
       
   614      bouncetext.s[pos] = '_';
       
   615  while (!stralloc_cats(&bouncetext,">:\n")) nomem();
       
   616  while (!stralloc_cats(&bouncetext,report)) nomem();
       
   617  if (report[0])
       
   618    if (report[str_len(report) - 1] != '\n')
       
   619      while (!stralloc_cats(&bouncetext,"\n")) nomem();
       
   620  for (pos = bouncetext.len - 2;pos > 0;--pos)
       
   621    if (bouncetext.s[pos] == '\n')
       
   622      if (bouncetext.s[pos - 1] == '\n')
       
   623        bouncetext.s[pos] = '/';
       
   624  while (!stralloc_cats(&bouncetext,"\n")) nomem();
       
   625  fnmake2_bounce(id);
       
   626  for (;;)
       
   627   {
       
   628    fd = open_append(fn2.s);
       
   629    if (fd != -1) break;
       
   630    log1("alert: unable to append to bounce message; HELP! sleeping...\n");
       
   631    sleep(10);
       
   632   }
       
   633  pos = 0;
       
   634  while (pos < bouncetext.len)
       
   635   {
       
   636    w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
       
   637    if (w <= 0)
       
   638     {
       
   639      log1("alert: unable to append to bounce message; HELP! sleeping...\n");
       
   640      sleep(10);
       
   641     }
       
   642    else
       
   643      pos += w;
       
   644   }
       
   645  close(fd);
       
   646 }
       
   647 
       
   648 int injectbounce(id)
       
   649 unsigned long id;
       
   650 {
       
   651  struct qmail qqt;
       
   652  struct stat st;
       
   653  char *bouncesender;
       
   654  char *bouncerecip;
       
   655  int r;
       
   656  int fd;
       
   657  substdio ssread;
       
   658  char buf[128];
       
   659  char inbuf[128];
       
   660  static stralloc sender = {0};
       
   661  static stralloc quoted = {0};
       
   662  datetime_sec birth;
       
   663  unsigned long qp;
       
   664 
       
   665  if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
       
   666 
       
   667  /* owner-@host-@[] -> owner-@host */
       
   668  if (sender.len >= 5)
       
   669    if (str_equal(sender.s + sender.len - 5,"-@[]"))
       
   670     {
       
   671      sender.len -= 4;
       
   672      sender.s[sender.len - 1] = 0;
       
   673     }
       
   674 
       
   675  fnmake2_bounce(id);
       
   676  fnmake_mess(id);
       
   677  if (stat(fn2.s,&st) == -1)
       
   678   {
       
   679    if (errno == error_noent)
       
   680      return 1;
       
   681    log3("warning: unable to stat ",fn2.s,"\n");
       
   682    return 0;
       
   683   }
       
   684  if (str_equal(sender.s,"#@[]"))
       
   685    log3("triple bounce: discarding ",fn2.s,"\n");
       
   686  else
       
   687   {
       
   688    if (qmail_open(&qqt) == -1)
       
   689     { log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
       
   690    qp = qmail_qp(&qqt);
       
   691 
       
   692    if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
       
   693    else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
       
   694 
       
   695    while (!newfield_datemake(now())) nomem();
       
   696    qmail_put(&qqt,newfield_date.s,newfield_date.len);
       
   697    qmail_puts(&qqt,"From: ");
       
   698    while (!quote(&quoted,&bouncefrom)) nomem();
       
   699    qmail_put(&qqt,quoted.s,quoted.len);
       
   700    qmail_puts(&qqt,"@");
       
   701    qmail_put(&qqt,bouncehost.s,bouncehost.len);
       
   702    qmail_puts(&qqt,"\nTo: ");
       
   703    while (!quote2(&quoted,bouncerecip)) nomem();
       
   704    qmail_put(&qqt,quoted.s,quoted.len);
       
   705    qmail_puts(&qqt,"\n\
       
   706 Subject: failure notice\n\
       
   707 \n\
       
   708 Hi. This is the qmail-send program at ");
       
   709    qmail_put(&qqt,bouncehost.s,bouncehost.len);
       
   710    qmail_puts(&qqt,*sender.s ? ".\n\
       
   711 I'm afraid I wasn't able to deliver your message to the following addresses.\n\
       
   712 This is a permanent error; I've given up. Sorry it didn't work out.\n\
       
   713 \n\
       
   714 " : ".\n\
       
   715 I tried to deliver a bounce message to this address, but the bounce bounced!\n\
       
   716 \n\
       
   717 ");
       
   718 
       
   719    fd = open_read(fn2.s);
       
   720    if (fd == -1)
       
   721      qmail_fail(&qqt);
       
   722    else
       
   723     {
       
   724      substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
       
   725      while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
       
   726        qmail_put(&qqt,buf,r);
       
   727      close(fd);
       
   728      if (r == -1)
       
   729        qmail_fail(&qqt);
       
   730     }
       
   731 
       
   732    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");
       
   733    qmail_puts(&qqt,"Return-Path: <");
       
   734    while (!quote2(&quoted,sender.s)) nomem();
       
   735    qmail_put(&qqt,quoted.s,quoted.len);
       
   736    qmail_puts(&qqt,">\n");
       
   737 
       
   738    fd = open_read(fn.s);
       
   739    if (fd == -1)
       
   740      qmail_fail(&qqt);
       
   741    else
       
   742     {
       
   743      substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
       
   744      while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
       
   745        qmail_put(&qqt,buf,r);
       
   746      close(fd);
       
   747      if (r == -1)
       
   748        qmail_fail(&qqt);
       
   749     }
       
   750 
       
   751    qmail_from(&qqt,bouncesender);
       
   752    qmail_to(&qqt,bouncerecip);
       
   753    if (*qmail_close(&qqt))
       
   754     { log1("warning: trouble injecting bounce message, will try later\n"); return 0; }
       
   755 
       
   756    strnum2[fmt_ulong(strnum2,id)] = 0;
       
   757    log2("bounce msg ",strnum2);
       
   758    strnum2[fmt_ulong(strnum2,qp)] = 0;
       
   759    log3(" qp ",strnum2,"\n");
       
   760   }
       
   761  if (unlink(fn2.s) == -1)
       
   762   {
       
   763    log3("warning: unable to unlink ",fn2.s,"\n");
       
   764    return 0;
       
   765   }
       
   766  return 1;
       
   767 }
       
   768 
       
   769 
       
   770 /* this file is too long ---------------------------------------- DELIVERIES */
       
   771 
       
   772 struct del
       
   773  {
       
   774   int used;
       
   775   int j;
       
   776   unsigned long delid;
       
   777   seek_pos mpos;
       
   778   stralloc recip;
       
   779  }
       
   780 ;
       
   781 
       
   782 unsigned long masterdelid = 1;
       
   783 unsigned int concurrency[CHANNELS] = { 10, 20 };
       
   784 unsigned int concurrencyused[CHANNELS] = { 0, 0 };
       
   785 struct del *d[CHANNELS];
       
   786 stralloc dline[CHANNELS];
       
   787 char delbuf[2048];
       
   788 
       
   789 void del_status()
       
   790 {
       
   791   int c;
       
   792 
       
   793   log1("status:");
       
   794   for (c = 0;c < CHANNELS;++c) {
       
   795     strnum2[fmt_ulong(strnum2,(unsigned long) concurrencyused[c])] = 0;
       
   796     strnum3[fmt_ulong(strnum3,(unsigned long) concurrency[c])] = 0;
       
   797     log2(chanstatusmsg[c],strnum2);
       
   798     log2("/",strnum3);
       
   799   }
       
   800   if (flagexitasap) log1(" exitasap");
       
   801   log1("\n");
       
   802 }
       
   803 
       
   804 void del_init()
       
   805 {
       
   806  int c;
       
   807  int i;
       
   808  for (c = 0;c < CHANNELS;++c)
       
   809   {
       
   810    flagspawnalive[c] = 1;
       
   811    while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
       
   812      nomem();
       
   813    for (i = 0;i < concurrency[c];++i)
       
   814     { d[c][i].used = 0; d[c][i].recip.s = 0; }
       
   815    dline[c].s = 0;
       
   816    while (!stralloc_copys(&dline[c],"")) nomem();
       
   817   }
       
   818  del_status();
       
   819 }
       
   820 
       
   821 int del_canexit()
       
   822 {
       
   823  int c;
       
   824  for (c = 0;c < CHANNELS;++c)
       
   825    if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */
       
   826      if (concurrencyused[c]) return 0;
       
   827  return 1;
       
   828 }
       
   829 
       
   830 int del_avail(c)
       
   831 int c;
       
   832 {
       
   833   return flagspawnalive[c] && comm_canwrite(c) && (concurrencyused[c] < concurrency[c]);
       
   834 }
       
   835 
       
   836 void del_start(j,mpos,recip)
       
   837 int j;
       
   838 seek_pos mpos;
       
   839 char *recip;
       
   840 {
       
   841  int i;
       
   842  int c;
       
   843 
       
   844  c = jo[j].channel;
       
   845  if (!flagspawnalive[c]) return;
       
   846  if (!comm_canwrite(c)) return;
       
   847 
       
   848  for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
       
   849  if (i == concurrency[c]) return;
       
   850 
       
   851  if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; }
       
   852  if (!stralloc_0(&d[c][i].recip)) { nomem(); return; }
       
   853  d[c][i].j = j; ++jo[j].refs;
       
   854  d[c][i].delid = masterdelid++;
       
   855  d[c][i].mpos = mpos;
       
   856  d[c][i].used = 1; ++concurrencyused[c];
       
   857 
       
   858  comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
       
   859 
       
   860  strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0;
       
   861  strnum3[fmt_ulong(strnum3,jo[j].id)] = 0;
       
   862  log2("starting delivery ",strnum2);
       
   863  log3(": msg ",strnum3,tochan[c]);
       
   864  logsafe(recip);
       
   865  log1("\n");
       
   866  del_status();
       
   867 }
       
   868 
       
   869 void markdone(c,id,pos)
       
   870 int c;
       
   871 unsigned long id;
       
   872 seek_pos pos;
       
   873 {
       
   874  struct stat st;
       
   875  int fd;
       
   876  fnmake_chanaddr(id,c);
       
   877  for (;;)
       
   878   {
       
   879    fd = open_write(fn.s);
       
   880    if (fd == -1) break;
       
   881    if (fstat(fd,&st) == -1) { close(fd); break; }
       
   882    if (seek_set(fd,pos) == -1) { close(fd); break; }
       
   883    if (write(fd,"D",1) != 1) { close(fd); break; }
       
   884    /* further errors -> double delivery without us knowing about it, oh well */
       
   885    close(fd);
       
   886    return;
       
   887   }
       
   888  log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
       
   889 }
       
   890 
       
   891 void del_dochan(c)
       
   892 int c;
       
   893 {
       
   894  int r;
       
   895  char ch;
       
   896  int i;
       
   897  int delnum;
       
   898  r = read(chanfdin[c],delbuf,sizeof(delbuf));
       
   899  if (r == -1) return;
       
   900  if (r == 0) { spawndied(c); return; }
       
   901  for (i = 0;i < r;++i)
       
   902   {
       
   903    ch = delbuf[i];
       
   904    while (!stralloc_append(&dline[c],&ch)) nomem();
       
   905    if (dline[c].len > REPORTMAX)
       
   906      dline[c].len = REPORTMAX;
       
   907      /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */
       
   908      /* but from a security point of view, we don't trust rspawn */
       
   909    if (!ch && (dline[c].len > 1))
       
   910     {
       
   911      delnum = (unsigned int) (unsigned char) dline[c].s[0];
       
   912      if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used)
       
   913        log1("warning: internal error: delivery report out of range\n");
       
   914      else
       
   915       {
       
   916        strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0;
       
   917        if (dline[c].s[1] == 'Z')
       
   918 	 if (jo[d[c][delnum].j].flagdying)
       
   919 	  {
       
   920 	   dline[c].s[1] = 'D';
       
   921 	   --dline[c].len;
       
   922 	   while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem();
       
   923 	   while (!stralloc_0(&dline[c])) nomem();
       
   924 	  }
       
   925        switch(dline[c].s[1])
       
   926 	{
       
   927 	 case 'K':
       
   928 	   log3("delivery ",strnum3,": success: ");
       
   929 	   logsafe(dline[c].s + 2);
       
   930 	   log1("\n");
       
   931 	   markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
       
   932 	   --jo[d[c][delnum].j].numtodo;
       
   933 	   break;
       
   934 	 case 'Z':
       
   935 	   log3("delivery ",strnum3,": deferral: ");
       
   936 	   logsafe(dline[c].s + 2);
       
   937 	   log1("\n");
       
   938 	   break;
       
   939 	 case 'D':
       
   940 	   log3("delivery ",strnum3,": failure: ");
       
   941 	   logsafe(dline[c].s + 2);
       
   942 	   log1("\n");
       
   943 	   addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2);
       
   944 	   markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
       
   945 	   --jo[d[c][delnum].j].numtodo;
       
   946 	   break;
       
   947 	 default:
       
   948 	   log3("delivery ",strnum3,": report mangled, will defer\n");
       
   949 	}
       
   950        job_close(d[c][delnum].j);
       
   951        d[c][delnum].used = 0; --concurrencyused[c];
       
   952        del_status();
       
   953       }
       
   954      dline[c].len = 0;
       
   955     }
       
   956   }
       
   957 }
       
   958 
       
   959 void del_selprep(nfds,rfds)
       
   960 int *nfds;
       
   961 fd_set *rfds;
       
   962 {
       
   963  int c;
       
   964  for (c = 0;c < CHANNELS;++c)
       
   965    if (flagspawnalive[c])
       
   966     {
       
   967      FD_SET(chanfdin[c],rfds);
       
   968      if (*nfds <= chanfdin[c])
       
   969        *nfds = chanfdin[c] + 1;
       
   970     }
       
   971 }
       
   972 
       
   973 void del_do(rfds)
       
   974 fd_set *rfds;
       
   975 {
       
   976  int c;
       
   977  for (c = 0;c < CHANNELS;++c)
       
   978    if (flagspawnalive[c])
       
   979      if (FD_ISSET(chanfdin[c],rfds))
       
   980        del_dochan(c);
       
   981 }
       
   982 
       
   983 
       
   984 /* this file is too long -------------------------------------------- PASSES */
       
   985 
       
   986 struct
       
   987  {
       
   988   unsigned long id; /* if 0, need a new pass */
       
   989   int j; /* defined if id; job number */
       
   990   int fd; /* defined if id; reading from {local,remote} */
       
   991   seek_pos mpos; /* defined if id; mark position */
       
   992   substdio ss;
       
   993   char buf[128];
       
   994  }
       
   995 pass[CHANNELS];
       
   996 
       
   997 void pass_init()
       
   998 {
       
   999  int c;
       
  1000  for (c = 0;c < CHANNELS;++c) pass[c].id = 0;
       
  1001 }
       
  1002 
       
  1003 void pass_selprep(wakeup)
       
  1004 datetime_sec *wakeup;
       
  1005 {
       
  1006  int c;
       
  1007  struct prioq_elt pe;
       
  1008  if (flagexitasap) return;
       
  1009  for (c = 0;c < CHANNELS;++c)
       
  1010    if (pass[c].id)
       
  1011      if (del_avail(c))
       
  1012       { *wakeup = 0; return; }
       
  1013  if (job_avail())
       
  1014    for (c = 0;c < CHANNELS;++c)
       
  1015      if (!pass[c].id)
       
  1016        if (prioq_min(&pqchan[c],&pe))
       
  1017          if (*wakeup > pe.dt)
       
  1018            *wakeup = pe.dt;
       
  1019  if (prioq_min(&pqfail,&pe))
       
  1020    if (*wakeup > pe.dt)
       
  1021      *wakeup = pe.dt;
       
  1022  if (prioq_min(&pqdone,&pe))
       
  1023    if (*wakeup > pe.dt)
       
  1024      *wakeup = pe.dt;
       
  1025 }
       
  1026 
       
  1027 static datetime_sec squareroot(x) /* result^2 <= x < (result + 1)^2 */
       
  1028 datetime_sec x; /* assuming: >= 0 */
       
  1029 {
       
  1030  datetime_sec y;
       
  1031  datetime_sec yy;
       
  1032  datetime_sec y21;
       
  1033  int j;
       
  1034 
       
  1035  y = 0; yy = 0;
       
  1036  for (j = 15;j >= 0;--j)
       
  1037   {
       
  1038    y21 = (y << (j + 1)) + (1 << (j + j));
       
  1039    if (y21 <= x - yy) { y += (1 << j); yy += y21; }
       
  1040   }
       
  1041  return y;
       
  1042 }
       
  1043 
       
  1044 datetime_sec nextretry(birth,c)
       
  1045 datetime_sec birth;
       
  1046 int c;
       
  1047 {
       
  1048  int n;
       
  1049 
       
  1050  if (birth > recent) n = 0;
       
  1051  else n = squareroot(recent - birth); /* no need to add fuzz to recent */
       
  1052  n += chanskip[c];
       
  1053  return birth + n * n;
       
  1054 }
       
  1055 
       
  1056 void pass_dochan(c)
       
  1057 int c;
       
  1058 {
       
  1059  datetime_sec birth;
       
  1060  struct prioq_elt pe;
       
  1061  static stralloc line = {0};
       
  1062  int match;
       
  1063 
       
  1064  if (flagexitasap) return;
       
  1065 
       
  1066  if (!pass[c].id)
       
  1067   {
       
  1068    if (!job_avail()) return;
       
  1069    if (!prioq_min(&pqchan[c],&pe)) return;
       
  1070    if (pe.dt > recent) return;
       
  1071    fnmake_chanaddr(pe.id,c);
       
  1072 
       
  1073    prioq_delmin(&pqchan[c]);
       
  1074    pass[c].mpos = 0;
       
  1075    pass[c].fd = open_read(fn.s);
       
  1076    if (pass[c].fd == -1) goto trouble;
       
  1077    if (!getinfo(&line,&birth,pe.id)) { close(pass[c].fd); goto trouble; }
       
  1078    pass[c].id = pe.id;
       
  1079    substdio_fdbuf(&pass[c].ss,read,pass[c].fd,pass[c].buf,sizeof(pass[c].buf));
       
  1080    pass[c].j = job_open(pe.id,c);
       
  1081    jo[pass[c].j].retry = nextretry(birth,c);
       
  1082    jo[pass[c].j].flagdying = (recent > birth + lifetime);
       
  1083    while (!stralloc_copy(&jo[pass[c].j].sender,&line)) nomem();
       
  1084   }
       
  1085 
       
  1086  if (!del_avail(c)) return;
       
  1087 
       
  1088  if (getln(&pass[c].ss,&line,&match,'\0') == -1)
       
  1089   {
       
  1090    fnmake_chanaddr(pass[c].id,c);
       
  1091    log3("warning: trouble reading ",fn.s,"; will try again later\n");
       
  1092    close(pass[c].fd);
       
  1093    job_close(pass[c].j);
       
  1094    pass[c].id = 0;
       
  1095    return;
       
  1096   }
       
  1097  if (!match)
       
  1098   {
       
  1099    close(pass[c].fd);
       
  1100    jo[pass[c].j].flaghiteof = 1;
       
  1101    job_close(pass[c].j);
       
  1102    pass[c].id = 0;
       
  1103    return;
       
  1104   }
       
  1105  switch(line.s[0])
       
  1106   {
       
  1107    case 'T':
       
  1108      ++jo[pass[c].j].numtodo;
       
  1109      del_start(pass[c].j,pass[c].mpos,line.s + 1);
       
  1110      break;
       
  1111    case 'D':
       
  1112      break;
       
  1113    default:
       
  1114      fnmake_chanaddr(pass[c].id,c);
       
  1115      log3("warning: unknown record type in ",fn.s,"!\n");
       
  1116      close(pass[c].fd);
       
  1117      job_close(pass[c].j);
       
  1118      pass[c].id = 0;
       
  1119      return;
       
  1120   }
       
  1121 
       
  1122  pass[c].mpos += line.len;
       
  1123  return;
       
  1124 
       
  1125  trouble:
       
  1126  log3("warning: trouble opening ",fn.s,"; will try again later\n");
       
  1127  pe.dt = recent + SLEEP_SYSFAIL;
       
  1128  while (!prioq_insert(&pqchan[c],&pe)) nomem();
       
  1129 }
       
  1130 
       
  1131 void messdone(id)
       
  1132 unsigned long id;
       
  1133 {
       
  1134  char ch;
       
  1135  int c;
       
  1136  struct prioq_elt pe;
       
  1137  struct stat st;
       
  1138 
       
  1139  for (c = 0;c < CHANNELS;++c)
       
  1140   {
       
  1141    fnmake_chanaddr(id,c);
       
  1142    if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */
       
  1143    if (errno != error_noent)
       
  1144     {
       
  1145      log3("warning: unable to stat ",fn.s,"; will try again later\n");
       
  1146      goto fail;
       
  1147     }
       
  1148   }
       
  1149 
       
  1150  fnmake_todo(id);
       
  1151  if (stat(fn.s,&st) == 0) return;
       
  1152  if (errno != error_noent)
       
  1153   {
       
  1154    log3("warning: unable to stat ",fn.s,"; will try again later\n");
       
  1155    goto fail;
       
  1156   }
       
  1157  
       
  1158  fnmake_info(id);
       
  1159  if (stat(fn.s,&st) == -1)
       
  1160   {
       
  1161    if (errno == error_noent) return;
       
  1162    log3("warning: unable to stat ",fn.s,"; will try again later\n");
       
  1163    goto fail;
       
  1164   }
       
  1165  
       
  1166  /* -todo +info -local -remote ?bounce */
       
  1167  if (!injectbounce(id))
       
  1168    goto fail; /* injectbounce() produced error message */
       
  1169 
       
  1170  strnum3[fmt_ulong(strnum3,id)] = 0;
       
  1171  log3("end msg ",strnum3,"\n");
       
  1172 
       
  1173  /* -todo +info -local -remote -bounce */
       
  1174  fnmake_info(id);
       
  1175  if (unlink(fn.s) == -1)
       
  1176   {
       
  1177    log3("warning: unable to unlink ",fn.s,"; will try again later\n");
       
  1178    goto fail;
       
  1179   }
       
  1180 
       
  1181  /* -todo -info -local -remote -bounce; we can relax */
       
  1182  fnmake_foop(id);
       
  1183  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
       
  1184  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
       
  1185  if (ch != '+')
       
  1186    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
       
  1187 
       
  1188  return;
       
  1189 
       
  1190  fail:
       
  1191  pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
       
  1192  while (!prioq_insert(&pqdone,&pe)) nomem();
       
  1193 }
       
  1194 
       
  1195 void pass_do()
       
  1196 {
       
  1197  int c;
       
  1198  struct prioq_elt pe;
       
  1199 
       
  1200  for (c = 0;c < CHANNELS;++c) pass_dochan(c);
       
  1201  if (prioq_min(&pqfail,&pe))
       
  1202    if (pe.dt <= recent)
       
  1203     {
       
  1204      prioq_delmin(&pqfail);
       
  1205      pqadd(pe.id);
       
  1206     }
       
  1207  if (prioq_min(&pqdone,&pe))
       
  1208    if (pe.dt <= recent)
       
  1209     {
       
  1210      prioq_delmin(&pqdone);
       
  1211      messdone(pe.id);
       
  1212     }
       
  1213 }
       
  1214 
       
  1215 
       
  1216 /* this file is too long ---------------------------------------------- TODO */
       
  1217 
       
  1218 datetime_sec nexttodorun;
       
  1219 DIR *tododir; /* if 0, have to opendir again */
       
  1220 stralloc todoline = {0};
       
  1221 char todobuf[SUBSTDIO_INSIZE];
       
  1222 char todobufinfo[512];
       
  1223 char todobufchan[CHANNELS][1024];
       
  1224 
       
  1225 void todo_init()
       
  1226 {
       
  1227  tododir = 0;
       
  1228  nexttodorun = now();
       
  1229  trigger_set();
       
  1230 }
       
  1231 
       
  1232 void todo_selprep(nfds,rfds,wakeup)
       
  1233 int *nfds;
       
  1234 fd_set *rfds;
       
  1235 datetime_sec *wakeup;
       
  1236 {
       
  1237  if (flagexitasap) return;
       
  1238  trigger_selprep(nfds,rfds);
       
  1239  if (tododir) *wakeup = 0;
       
  1240  if (*wakeup > nexttodorun) *wakeup = nexttodorun;
       
  1241 }
       
  1242 
       
  1243 void todo_do(rfds)
       
  1244 fd_set *rfds;
       
  1245 {
       
  1246  struct stat st;
       
  1247  substdio ss; int fd;
       
  1248  substdio ssinfo; int fdinfo;
       
  1249  substdio sschan[CHANNELS];
       
  1250  int fdchan[CHANNELS];
       
  1251  int flagchan[CHANNELS];
       
  1252  struct prioq_elt pe;
       
  1253  char ch;
       
  1254  int match;
       
  1255  unsigned long id;
       
  1256  unsigned int len;
       
  1257  direntry *d;
       
  1258  int c;
       
  1259  unsigned long uid;
       
  1260  unsigned long pid;
       
  1261 
       
  1262  fd = -1;
       
  1263  fdinfo = -1;
       
  1264  for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
       
  1265 
       
  1266  if (flagexitasap) return;
       
  1267 
       
  1268  if (!tododir)
       
  1269   {
       
  1270    if (!trigger_pulled(rfds))
       
  1271      if (recent < nexttodorun)
       
  1272        return;
       
  1273    trigger_set();
       
  1274    tododir = opendir("todo");
       
  1275    if (!tododir)
       
  1276     {
       
  1277      pausedir("todo");
       
  1278      return;
       
  1279     }
       
  1280    nexttodorun = recent + SLEEP_TODO;
       
  1281   }
       
  1282 
       
  1283  d = readdir(tododir);
       
  1284  if (!d)
       
  1285   {
       
  1286    closedir(tododir);
       
  1287    tododir = 0;
       
  1288    return;
       
  1289   }
       
  1290  if (str_equal(d->d_name,".")) return;
       
  1291  if (str_equal(d->d_name,"..")) return;
       
  1292  len = scan_ulong(d->d_name,&id);
       
  1293  if (!len || d->d_name[len]) return;
       
  1294 
       
  1295  fnmake_todo(id);
       
  1296 
       
  1297  fd = open_read(fn.s);
       
  1298  if (fd == -1) { log3("warning: unable to open ",fn.s,"\n"); return; }
       
  1299 
       
  1300  fnmake_mess(id);
       
  1301  /* just for the statistics */
       
  1302  if (stat(fn.s,&st) == -1)
       
  1303   { log3("warning: unable to stat ",fn.s,"\n"); goto fail; }
       
  1304 
       
  1305  for (c = 0;c < CHANNELS;++c)
       
  1306   {
       
  1307    fnmake_chanaddr(id,c);
       
  1308    if (unlink(fn.s) == -1) if (errno != error_noent)
       
  1309     { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
       
  1310   }
       
  1311 
       
  1312  fnmake_info(id);
       
  1313  if (unlink(fn.s) == -1) if (errno != error_noent)
       
  1314   { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
       
  1315 
       
  1316  fdinfo = open_excl(fn.s);
       
  1317  if (fdinfo == -1)
       
  1318   { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
       
  1319 
       
  1320  strnum3[fmt_ulong(strnum3,id)] = 0;
       
  1321  log3("new msg ",strnum3,"\n");
       
  1322 
       
  1323  for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
       
  1324 
       
  1325  substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
       
  1326  substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
       
  1327 
       
  1328  uid = 0;
       
  1329  pid = 0;
       
  1330 
       
  1331  for (;;)
       
  1332   {
       
  1333    if (getln(&ss,&todoline,&match,'\0') == -1)
       
  1334     {
       
  1335      /* perhaps we're out of memory, perhaps an I/O error */
       
  1336      fnmake_todo(id);
       
  1337      log3("warning: trouble reading ",fn.s,"\n"); goto fail;
       
  1338     }
       
  1339    if (!match) break;
       
  1340 
       
  1341    switch(todoline.s[0])
       
  1342     {
       
  1343      case 'u':
       
  1344        scan_ulong(todoline.s + 1,&uid);
       
  1345        break;
       
  1346      case 'p':
       
  1347        scan_ulong(todoline.s + 1,&pid);
       
  1348        break;
       
  1349      case 'F':
       
  1350        if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
       
  1351 	{
       
  1352 	 fnmake_info(id);
       
  1353          log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
       
  1354 	}
       
  1355        log2("info msg ",strnum3);
       
  1356        strnum2[fmt_ulong(strnum2,(unsigned long) st.st_size)] = 0;
       
  1357        log2(": bytes ",strnum2);
       
  1358        log1(" from <"); logsafe(todoline.s + 1);
       
  1359        strnum2[fmt_ulong(strnum2,pid)] = 0;
       
  1360        log2("> qp ",strnum2);
       
  1361        strnum2[fmt_ulong(strnum2,uid)] = 0;
       
  1362        log2(" uid ",strnum2);
       
  1363        log1("\n");
       
  1364        break;
       
  1365      case 'T':
       
  1366        switch(rewrite(todoline.s + 1))
       
  1367 	{
       
  1368 	 case 0: nomem(); goto fail;
       
  1369 	 case 2: c = 1; break;
       
  1370 	 default: c = 0; break;
       
  1371         }
       
  1372        if (fdchan[c] == -1)
       
  1373 	{
       
  1374 	 fnmake_chanaddr(id,c);
       
  1375 	 fdchan[c] = open_excl(fn.s);
       
  1376 	 if (fdchan[c] == -1)
       
  1377           { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
       
  1378 	 substdio_fdbuf(&sschan[c]
       
  1379 	   ,write,fdchan[c],todobufchan[c],sizeof(todobufchan[c]));
       
  1380 	 flagchan[c] = 1;
       
  1381 	}
       
  1382        if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
       
  1383         {
       
  1384 	 fnmake_chanaddr(id,c);
       
  1385          log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
       
  1386         }
       
  1387        break;
       
  1388      default:
       
  1389        fnmake_todo(id);
       
  1390        log3("warning: unknown record type in ",fn.s,"\n"); goto fail;
       
  1391     }
       
  1392   }
       
  1393 
       
  1394  close(fd); fd = -1;
       
  1395 
       
  1396  fnmake_info(id);
       
  1397  if (substdio_flush(&ssinfo) == -1)
       
  1398   { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
       
  1399  if (fsync(fdinfo) == -1)
       
  1400   { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
       
  1401  close(fdinfo); fdinfo = -1;
       
  1402 
       
  1403  for (c = 0;c < CHANNELS;++c)
       
  1404    if (fdchan[c] != -1)
       
  1405     {
       
  1406      fnmake_chanaddr(id,c);
       
  1407      if (substdio_flush(&sschan[c]) == -1)
       
  1408       { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
       
  1409      if (fsync(fdchan[c]) == -1)
       
  1410       { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
       
  1411      close(fdchan[c]); fdchan[c] = -1;
       
  1412     }
       
  1413 
       
  1414  fnmake_todo(id);
       
  1415  if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
       
  1416  if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
       
  1417  if (ch != '+')
       
  1418   {
       
  1419    log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
       
  1420    return;
       
  1421   }
       
  1422 
       
  1423  pe.id = id; pe.dt = now();
       
  1424  for (c = 0;c < CHANNELS;++c)
       
  1425    if (flagchan[c])
       
  1426      while (!prioq_insert(&pqchan[c],&pe)) nomem();
       
  1427 
       
  1428  for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
       
  1429  if (c == CHANNELS)
       
  1430    while (!prioq_insert(&pqdone,&pe)) nomem();
       
  1431 
       
  1432  return;
       
  1433 
       
  1434  fail:
       
  1435  if (fd != -1) close(fd);
       
  1436  if (fdinfo != -1) close(fdinfo);
       
  1437  for (c = 0;c < CHANNELS;++c)
       
  1438    if (fdchan[c] != -1) close(fdchan[c]);
       
  1439 }
       
  1440 
       
  1441 
       
  1442 /* this file is too long ---------------------------------------------- MAIN */
       
  1443 
       
  1444 int getcontrols() { if (control_init() == -1) return 0;
       
  1445  if (control_readint(&lifetime,"control/queuelifetime") == -1) return 0;
       
  1446  if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) return 0;
       
  1447  if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) return 0;
       
  1448  if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0;
       
  1449  if (control_rldef(&bouncefrom,"control/bouncefrom",0,"MAILER-DAEMON") != 1) return 0;
       
  1450  if (control_rldef(&bouncehost,"control/bouncehost",1,"bouncehost") != 1) return 0;
       
  1451  if (control_rldef(&doublebouncehost,"control/doublebouncehost",1,"doublebouncehost") != 1) return 0;
       
  1452  if (control_rldef(&doublebounceto,"control/doublebounceto",0,"postmaster") != 1) return 0;
       
  1453  if (!stralloc_cats(&doublebounceto,"@")) return 0;
       
  1454  if (!stralloc_cat(&doublebounceto,&doublebouncehost)) return 0;
       
  1455  if (!stralloc_0(&doublebounceto)) return 0;
       
  1456  if (control_readfile(&locals,"control/locals",1) != 1) return 0;
       
  1457  if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0;
       
  1458  switch(control_readfile(&percenthack,"control/percenthack",0))
       
  1459   {
       
  1460    case -1: return 0;
       
  1461    case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break;
       
  1462    case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break;
       
  1463   }
       
  1464  switch(control_readfile(&vdoms,"control/virtualdomains",0))
       
  1465   {
       
  1466    case -1: return 0;
       
  1467    case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break;
       
  1468    case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break;
       
  1469   }
       
  1470  return 1; }
       
  1471 
       
  1472 stralloc newlocals = {0};
       
  1473 stralloc newvdoms = {0};
       
  1474 
       
  1475 void regetcontrols()
       
  1476 {
       
  1477  int r;
       
  1478 
       
  1479  if (control_readfile(&newlocals,"control/locals",1) != 1)
       
  1480   { log1("alert: unable to reread control/locals\n"); return; }
       
  1481  r = control_readfile(&newvdoms,"control/virtualdomains",0);
       
  1482  if (r == -1)
       
  1483   { log1("alert: unable to reread control/virtualdomains\n"); return; }
       
  1484 
       
  1485  constmap_free(&maplocals);
       
  1486  constmap_free(&mapvdoms);
       
  1487 
       
  1488  while (!stralloc_copy(&locals,&newlocals)) nomem();
       
  1489  while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
       
  1490 
       
  1491  if (r)
       
  1492   {
       
  1493    while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
       
  1494    while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
       
  1495   }
       
  1496  else
       
  1497    while (!constmap_init(&mapvdoms,"",0,1)) nomem();
       
  1498 }
       
  1499 
       
  1500 void reread()
       
  1501 {
       
  1502  if (chdir(auto_qmail) == -1)
       
  1503   {
       
  1504    log1("alert: unable to reread controls: unable to switch to home directory\n");
       
  1505    return;
       
  1506   }
       
  1507  regetcontrols();
       
  1508  while (chdir("queue") == -1)
       
  1509   {
       
  1510    log1("alert: unable to switch back to queue directory; HELP! sleeping...\n");
       
  1511    sleep(10);
       
  1512   }
       
  1513 }
       
  1514 
       
  1515 void main()
       
  1516 {
       
  1517  int fd;
       
  1518  datetime_sec wakeup;
       
  1519  fd_set rfds;
       
  1520  fd_set wfds;
       
  1521  int nfds;
       
  1522  struct timeval tv;
       
  1523  int c;
       
  1524 
       
  1525  if (chdir(auto_qmail) == -1)
       
  1526   { log1("alert: cannot start: unable to switch to home directory\n"); _exit(111); }
       
  1527  if (!getcontrols())
       
  1528   { log1("alert: cannot start: unable to read controls\n"); _exit(111); }
       
  1529  if (chdir("queue") == -1)
       
  1530   { log1("alert: cannot start: unable to switch to queue directory\n"); _exit(111); }
       
  1531  sig_pipeignore();
       
  1532  sig_termcatch(sigterm);
       
  1533  sig_alarmcatch(sigalrm);
       
  1534  sig_hangupcatch(sighup);
       
  1535  sig_childdefault();
       
  1536  umask(077);
       
  1537 
       
  1538  fd = open_write("lock/sendmutex");
       
  1539  if (fd == -1)
       
  1540   { log1("alert: cannot start: unable to open mutex\n"); _exit(111); }
       
  1541  if (lock_exnb(fd) == -1)
       
  1542   { log1("alert: cannot start: qmail-send is already running\n"); _exit(111); }
       
  1543 
       
  1544  numjobs = 0;
       
  1545  for (c = 0;c < CHANNELS;++c)
       
  1546   {
       
  1547    char ch;
       
  1548    int u;
       
  1549    int r;
       
  1550    do
       
  1551      r = read(chanfdin[c],&ch,1);
       
  1552    while ((r == -1) && (errno == error_intr));
       
  1553    if (r < 1)
       
  1554     { log1("alert: cannot start: hath the daemon spawn no fire?\n"); _exit(111); }
       
  1555    u = (unsigned int) (unsigned char) ch;
       
  1556    if (concurrency[c] > u) concurrency[c] = u;
       
  1557    numjobs += concurrency[c];
       
  1558   }
       
  1559 
       
  1560  fnmake_init();
       
  1561 
       
  1562  comm_init();
       
  1563 
       
  1564  pqstart();
       
  1565  job_init();
       
  1566  del_init();
       
  1567  pass_init();
       
  1568  todo_init();
       
  1569  cleanup_init();
       
  1570 
       
  1571  while (!flagexitasap || !del_canexit())
       
  1572   {
       
  1573    recent = now();
       
  1574 
       
  1575    if (flagrunasap) { flagrunasap = 0; pqrun(); }
       
  1576    if (flagreadasap) { flagreadasap = 0; reread(); }
       
  1577 
       
  1578    wakeup = recent + SLEEP_FOREVER;
       
  1579    FD_ZERO(&rfds);
       
  1580    FD_ZERO(&wfds);
       
  1581    nfds = 1;
       
  1582 
       
  1583    comm_selprep(&nfds,&wfds);
       
  1584    del_selprep(&nfds,&rfds);
       
  1585    pass_selprep(&wakeup);
       
  1586    todo_selprep(&nfds,&rfds,&wakeup);
       
  1587    cleanup_selprep(&wakeup);
       
  1588 
       
  1589    if (wakeup <= recent) tv.tv_sec = 0;
       
  1590    else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
       
  1591    tv.tv_usec = 0;
       
  1592 
       
  1593    if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1)
       
  1594      if (errno == error_intr)
       
  1595        ;
       
  1596      else
       
  1597        log1("warning: trouble in select\n");
       
  1598    else
       
  1599     {
       
  1600      recent = now();
       
  1601 
       
  1602      comm_do(&wfds);
       
  1603      del_do(&rfds);
       
  1604      todo_do(&rfds);
       
  1605      pass_do();
       
  1606      cleanup_do();
       
  1607     }
       
  1608   }
       
  1609  pqfinish();
       
  1610  log1("status: exiting\n");
       
  1611  _exit(0);
       
  1612 }