14632Seric # include "sendmail.h" 24632Seric # include <sys/stat.h> 36625Sglickman # include <ndir.h> 44634Seric # include <signal.h> 54632Seric # include <errno.h> 64632Seric 75182Seric # ifndef QUEUE 8*7009Seric SCCSID(@(#)queue.c 3.16 05/31/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*7009Seric SCCSID(@(#)queue.c 3.16 05/31/82); 125182Seric 134632Seric /* 144632Seric ** QUEUEUP -- queue a message up for future transmission. 154632Seric ** 164632Seric ** The queued message should already be in the correct place. 174632Seric ** This routine just outputs the control file as appropriate. 184632Seric ** 194632Seric ** Parameters: 206980Seric ** e -- the envelope to queue up. 216999Seric ** queueall -- if TRUE, queue all addresses, rather than 226999Seric ** just those with the QQUEUEUP flag set. 234632Seric ** 244632Seric ** Returns: 254632Seric ** none. 264632Seric ** 274632Seric ** Side Effects: 284632Seric ** The current request (only unsatisfied addresses) 294632Seric ** are saved in a control file. 304632Seric */ 314632Seric 326999Seric queueup(e, queueall) 336980Seric register ENVELOPE *e; 346999Seric bool queueall; 354632Seric { 364632Seric char cf[MAXNAME]; 376980Seric char buf[MAXNAME]; 386980Seric register FILE *cfp; 394632Seric register HDR *h; 405007Seric register ADDRESS *q; 415199Seric extern char *mktemp(); 425902Seric register int i; 434632Seric 445037Seric /* 455037Seric ** Create control file. 465037Seric */ 474632Seric 48*7009Seric (void) strcpy(cf, QueueDir); 49*7009Seric (void) strcat(cf, "/tfXXXXXX"); 505037Seric (void) mktemp(cf); 516980Seric cfp = fopen(cf, "w"); 526980Seric if (cfp == NULL) 534632Seric { 544632Seric syserr("queueup: cannot create control file %s", cf); 554632Seric return; 564632Seric } 574632Seric 584632Seric # ifdef DEBUG 594632Seric if (Debug) 606980Seric printf("queueing in %s\n", cf); 614632Seric # endif DEBUG 624632Seric 634632Seric /* 646980Seric ** If there is no data file yet, create one. 656980Seric */ 666980Seric 676980Seric if (e->e_df == NULL) 686980Seric { 696980Seric register FILE *dfp; 706980Seric 71*7009Seric (void) strcpy(buf, QueueDir); 72*7009Seric (void) strcat(buf, "/dfXXXXXX"); 736980Seric e->e_df = newstr(mktemp(buf)); 746980Seric dfp = fopen(e->e_df, "w"); 756980Seric if (dfp == NULL) 766980Seric { 776980Seric syserr("queueup: cannot create %s", e->e_df); 78*7009Seric (void) fclose(cfp); 796980Seric return; 806980Seric } 816980Seric (*e->e_putbody)(dfp, Mailer[1], FALSE); 82*7009Seric (void) fclose(dfp); 836980Seric } 846980Seric 856980Seric /* 864632Seric ** Output future work requests. 874632Seric */ 884632Seric 894632Seric /* output name of data file */ 906980Seric fprintf(cfp, "D%s\n", e->e_df); 914632Seric 924632Seric /* output name of sender */ 936980Seric fprintf(cfp, "S%s\n", e->e_from.q_paddr); 944632Seric 954632Seric /* output timeout */ 966980Seric fprintf(cfp, "T%ld\n", TimeOut); 974632Seric 984634Seric /* output message priority */ 996980Seric fprintf(cfp, "P%ld\n", e->e_msgpriority); 1004634Seric 1016980Seric /* output message class */ 1026980Seric fprintf(cfp, "C%d\n", e->e_class); 1036980Seric 1045902Seric /* output macro definitions */ 1055902Seric for (i = 0; i < 128; i++) 1065902Seric { 1076980Seric register char *p = e->e_macro[i]; 1085902Seric 1095902Seric if (p != NULL && i != (int) 'b') 1106980Seric fprintf(cfp, "M%c%s\n", i, p); 1115902Seric } 1125902Seric 1134632Seric /* output list of recipient addresses */ 1146980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1154632Seric { 1165037Seric # ifdef DEBUG 1175037Seric if (Debug > 0) 1185037Seric { 1195037Seric printf("queueing "); 1205037Seric printaddr(q, FALSE); 1215037Seric } 1225037Seric # endif DEBUG 1236999Seric if (queueall || bitset(QQUEUEUP, q->q_flags)) 1246980Seric fprintf(cfp, "R%s\n", q->q_paddr); 1254632Seric } 1264632Seric 1274632Seric /* output headers for this message */ 1286980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1294632Seric { 1304632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1314632Seric continue; 1326980Seric fprintf(cfp, "H"); 1334632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1346980Seric mfdecode(h->h_mflags, cfp); 1356980Seric fprintf(cfp, "%s: %s\n", h->h_field, h->h_value); 1364632Seric } 1374632Seric 1384632Seric /* 1394632Seric ** Clean up. 1404632Seric */ 1414632Seric 1426980Seric (void) fclose(cfp); 1436980Seric (void) strcpy(buf, QueueDir); 1446980Seric (void) strcat(buf, "/cfXXXXXX"); 1456980Seric (void) mktemp(buf); 1466980Seric if (link(cf, buf) < 0) 1476980Seric syserr("cannot link(%s, %s), df=%s", cf, buf, e->e_df); 1486980Seric else 149*7009Seric (void) unlink(cf); 1504632Seric } 1514632Seric /* 1524632Seric ** RUNQUEUE -- run the jobs in the queue. 1534632Seric ** 1544632Seric ** Gets the stuff out of the queue in some presumably logical 1554632Seric ** order and processes them. 1564632Seric ** 1574632Seric ** Parameters: 1584632Seric ** none. 1594632Seric ** 1604632Seric ** Returns: 1614632Seric ** none. 1624632Seric ** 1634632Seric ** Side Effects: 1644632Seric ** runs things in the mail queue. 1654632Seric */ 1664632Seric 1674639Seric bool ReorderQueue; /* if set, reorder the send queue */ 1684639Seric int QueuePid; /* pid of child running queue */ 1694634Seric 1704639Seric runqueue(forkflag) 1714639Seric bool forkflag; 1724632Seric { 1734634Seric extern reordersig(); 1744632Seric 1754639Seric if (QueueIntvl != 0) 1764639Seric { 1774836Seric (void) signal(SIGALRM, reordersig); 178*7009Seric (void) alarm(QueueIntvl); 1794639Seric } 1804639Seric 1814639Seric if (forkflag) 1824639Seric { 1834639Seric QueuePid = dofork(); 1844639Seric if (QueuePid > 0) 1854639Seric { 1864639Seric /* parent */ 1874639Seric return; 1884639Seric } 1894639Seric else 190*7009Seric (void) alarm(0); 1914639Seric } 1924639Seric 1934634Seric for (;;) 1944634Seric { 1954634Seric /* 1964634Seric ** Order the existing work requests. 1974634Seric */ 1984632Seric 1994634Seric orderq(); 2004632Seric 2014634Seric if (WorkQ == NULL) 2024634Seric { 2034634Seric /* no work? well, maybe later */ 2044634Seric if (QueueIntvl == 0) 2054634Seric break; 2064639Seric pause(); 2074634Seric continue; 2084634Seric } 2094632Seric 2104634Seric ReorderQueue = FALSE; 2114634Seric 2124634Seric /* 2134634Seric ** Process them once at a time. 2144634Seric ** The queue could be reordered while we do this to take 2154634Seric ** new requests into account. If so, the existing job 2164634Seric ** will be finished but the next thing taken off WorkQ 2174634Seric ** may be something else. 2184634Seric */ 2194634Seric 2204634Seric while (WorkQ != NULL) 2214634Seric { 2224634Seric WORK *w = WorkQ; 2234634Seric 2244634Seric WorkQ = WorkQ->w_next; 2254634Seric dowork(w); 2264634Seric free(w->w_name); 2274634Seric free((char *) w); 2284634Seric if (ReorderQueue) 2294634Seric break; 2304634Seric } 2314634Seric 2324634Seric if (QueueIntvl == 0) 2334634Seric break; 2344632Seric } 2355978Seric 2365978Seric /* no work to do -- just exit */ 2375978Seric finis(); 2384632Seric } 2394632Seric /* 2404634Seric ** REORDERSIG -- catch the alarm signal and tell sendmail to reorder queue. 2414634Seric ** 2424634Seric ** Parameters: 2434634Seric ** none. 2444634Seric ** 2454634Seric ** Returns: 2464634Seric ** none. 2474634Seric ** 2484634Seric ** Side Effects: 2494634Seric ** sets the "reorder work queue" flag. 2504634Seric */ 2514634Seric 2524634Seric reordersig() 2534634Seric { 2544639Seric if (QueuePid == 0) 2554639Seric { 2564639Seric /* we are in a child doing queueing */ 2574639Seric ReorderQueue = TRUE; 2584639Seric } 2594639Seric else 2604639Seric { 2614639Seric /* we are in a parent -- poke child or start new one */ 2624639Seric if (kill(QueuePid, SIGALRM) < 0) 2634639Seric { 2644639Seric /* no child -- get zombie & start new one */ 2654639Seric static int st; 2664639Seric 2674836Seric (void) wait(&st); 2684639Seric QueuePid = dofork(); 2694639Seric if (QueuePid == 0) 2704639Seric { 2714639Seric /* new child; run queue */ 2724836Seric runqueue(FALSE); 2734639Seric finis(); 2744639Seric } 2754639Seric } 2764639Seric } 2774639Seric 2784639Seric /* 2794639Seric ** Arrange to get this signal again. 2804639Seric */ 2814639Seric 2826065Seric (void) signal(SIGALRM, reordersig); 283*7009Seric (void) alarm(QueueIntvl); 2844634Seric } 2854634Seric /* 2864632Seric ** ORDERQ -- order the work queue. 2874632Seric ** 2884632Seric ** Parameters: 2894632Seric ** none. 2904632Seric ** 2914632Seric ** Returns: 2924632Seric ** none. 2934632Seric ** 2944632Seric ** Side Effects: 2954632Seric ** Sets WorkQ to the queue of available work, in order. 2964632Seric */ 2974632Seric 2984632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2994632Seric 3004632Seric orderq() 3014632Seric { 3026625Sglickman register struct direct *d; 3034632Seric register WORK *w; 3044632Seric register WORK **wp; /* parent of w */ 3056625Sglickman DIR *f; 3064632Seric register int i; 3074632Seric WORK wlist[WLSIZE]; 3084632Seric int wn = 0; 3094632Seric extern workcmpf(); 3104632Seric extern char *QueueDir; 3114632Seric 3124632Seric /* clear out old WorkQ */ 3134632Seric for (w = WorkQ; w != NULL; ) 3144632Seric { 3154632Seric register WORK *nw = w->w_next; 3164632Seric 3174632Seric WorkQ = nw; 3184632Seric free(w->w_name); 3194632Seric free((char *) w); 3204632Seric w = nw; 3214632Seric } 3224632Seric 3234632Seric /* open the queue directory */ 3246625Sglickman f = opendir(QueueDir); 3254632Seric if (f == NULL) 3264632Seric { 3274632Seric syserr("orderq: cannot open %s", QueueDir); 3284632Seric return; 3294632Seric } 3304632Seric 3314632Seric /* 3324632Seric ** Read the work directory. 3334632Seric */ 3344632Seric 3356625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 3364632Seric { 3374632Seric char cbuf[MAXNAME]; 3384632Seric char lbuf[MAXNAME]; 3394632Seric FILE *cf; 3404632Seric register char *p; 3414632Seric 3424632Seric /* is this an interesting entry? */ 3436625Sglickman if (d->d_name[0] != 'c') 3444632Seric continue; 3454632Seric 3464632Seric /* yes -- find the control file location */ 347*7009Seric (void) strcpy(cbuf, QueueDir); 348*7009Seric (void) strcat(cbuf, "/"); 3494632Seric p = &cbuf[strlen(cbuf)]; 350*7009Seric (void) strcpy(p, d->d_name); 3514632Seric 3524632Seric /* open control file */ 3534632Seric cf = fopen(cbuf, "r"); 3544632Seric if (cf == NULL) 3554632Seric { 3564632Seric syserr("orderq: cannot open %s", cbuf); 3574632Seric continue; 3584632Seric } 3595037Seric wlist[wn].w_name = newstr(cbuf); 3604632Seric 3614632Seric /* extract useful information */ 3624632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3634632Seric { 3644632Seric fixcrlf(lbuf, TRUE); 3654632Seric 3664632Seric switch (lbuf[0]) 3674632Seric { 3684632Seric case 'P': /* message priority */ 3695037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3704632Seric break; 3714632Seric } 3724632Seric } 3734632Seric wn++; 3744632Seric (void) fclose(cf); 3754632Seric } 3766625Sglickman (void) closedir(f); 3774632Seric 3784632Seric /* 3794632Seric ** Sort the work directory. 3804632Seric */ 3814632Seric 3824632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3834632Seric 3844632Seric /* 3854632Seric ** Convert the work list into canonical form. 3864632Seric */ 3874632Seric 3884632Seric wp = &WorkQ; 3894632Seric for (i = 0; i < wn; i++) 3904632Seric { 3914632Seric w = (WORK *) xalloc(sizeof *w); 3924632Seric w->w_name = wlist[i].w_name; 3934632Seric w->w_pri = wlist[i].w_pri; 3944632Seric w->w_next = NULL; 3954632Seric *wp = w; 3964632Seric wp = &w->w_next; 3974632Seric } 3984632Seric 3994632Seric # ifdef DEBUG 4004632Seric if (Debug) 4014632Seric { 4024632Seric for (w = WorkQ; w != NULL; w = w->w_next) 4035037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 4044632Seric } 4054632Seric # endif DEBUG 4064632Seric } 4074632Seric /* 4084632Seric ** WORKCMPF -- compare function for ordering work. 4094632Seric ** 4104632Seric ** Parameters: 4114632Seric ** a -- the first argument. 4124632Seric ** b -- the second argument. 4134632Seric ** 4144632Seric ** Returns: 4154632Seric ** -1 if a < b 4164632Seric ** 0 if a == b 4174632Seric ** 1 if a > b 4184632Seric ** 4194632Seric ** Side Effects: 4204632Seric ** none. 4214632Seric */ 4224632Seric 4234632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 4244632Seric 4254632Seric workcmpf(a, b) 4265037Seric register WORK *a; 4275037Seric register WORK *b; 4284632Seric { 4295037Seric if (a->w_pri == b->w_pri) 4304632Seric return (0); 4315037Seric else if (a->w_pri > b->w_pri) 4324632Seric return (1); 4334632Seric else 4344632Seric return (-1); 4354632Seric } 4364632Seric /* 4374632Seric ** DOWORK -- do a work request. 4384632Seric ** 4394632Seric ** Parameters: 4404632Seric ** w -- the work request to be satisfied. 4414632Seric ** 4424632Seric ** Returns: 4434632Seric ** none. 4444632Seric ** 4454632Seric ** Side Effects: 4464632Seric ** The work request is satisfied if possible. 4474632Seric */ 4484632Seric 4494632Seric dowork(w) 4504632Seric register WORK *w; 4514632Seric { 4524632Seric register int i; 4534632Seric auto int xstat; 4544632Seric 4554632Seric # ifdef DEBUG 4564632Seric if (Debug) 4575037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4584632Seric # endif DEBUG 4594632Seric 4604632Seric /* 4614632Seric ** Fork for work. 4624632Seric */ 4634632Seric 4644632Seric i = fork(); 4654632Seric if (i < 0) 4664632Seric { 4674632Seric syserr("dowork: cannot fork"); 4684632Seric return; 4694632Seric } 4704632Seric 4714632Seric if (i == 0) 4724632Seric { 4736980Seric char buf[MAXNAME]; 4746980Seric 4754632Seric /* 4764632Seric ** CHILD 4776980Seric ** Change the name of the control file to avoid 4786980Seric ** duplicate deliveries. Then run the file as 4796980Seric ** though we had just read it. 4804632Seric */ 4814632Seric 4824632Seric QueueRun = TRUE; 4836991Seric MailBack = TRUE; 4846980Seric (void) strcpy(buf, QueueDir); 4856980Seric (void) strcat(buf, "/tfXXXXXX"); 4866980Seric (void) mktemp(buf); 4876980Seric if (link(w->w_name, buf) < 0) 4886980Seric { 4896980Seric syserr("dowork: link(%s, %s)", w->w_name, buf); 4906980Seric 4916980Seric /* it's ok to lie -- it will be run later */ 4926980Seric exit(EX_OK); 4936980Seric } 4946980Seric (void) unlink(w->w_name); 4956980Seric 4966980Seric /* create ourselves a transcript file */ 4974634Seric openxscrpt(); 4986980Seric 4996980Seric /* do basic system initialization */ 5004632Seric initsys(); 5016980Seric 5026980Seric /* read the queue control file */ 5036980Seric readqf(buf); 5046980Seric 5056980Seric /* do the delivery */ 5064632Seric sendall(FALSE); 5076980Seric 5086980Seric /* if still not sent, perhaps we should time out.... */ 5094634Seric # ifdef DEBUG 5104634Seric if (Debug > 2) 5114634Seric printf("CurTime=%ld, TimeOut=%ld\n", CurTime, TimeOut); 5124634Seric # endif DEBUG 5136908Seric if (CurEnv->e_queueup && CurTime > TimeOut) 5144634Seric timeout(w); 5156980Seric 5166980Seric /* get rid of the temporary file -- a new cf will be made */ 5176980Seric (void) unlink(buf); 5186980Seric 5196980Seric /* finish up and exit */ 5204632Seric finis(); 5214632Seric } 5224632Seric 5234632Seric /* 5244632Seric ** Parent -- pick up results. 5254632Seric */ 5264632Seric 5274632Seric errno = 0; 5284632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 5294632Seric { 5304632Seric if (errno == EINTR) 5314632Seric { 5324632Seric errno = 0; 5334632Seric } 5344632Seric } 5354632Seric } 5364632Seric /* 5374632Seric ** READQF -- read queue file and set up environment. 5384632Seric ** 5394632Seric ** Parameters: 5404632Seric ** cf -- name of queue control file. 5414632Seric ** 5424632Seric ** Returns: 5434632Seric ** none. 5444632Seric ** 5454632Seric ** Side Effects: 5464632Seric ** cf is read and created as the current job, as though 5474632Seric ** we had been invoked by argument. 5484632Seric */ 5494632Seric 5504632Seric readqf(cf) 5514632Seric char *cf; 5524632Seric { 5534632Seric register FILE *f; 5544632Seric char buf[MAXLINE]; 5554632Seric 5564632Seric /* 5574632Seric ** Open the file created by queueup. 5584632Seric */ 5594632Seric 5604632Seric f = fopen(cf, "r"); 5614632Seric if (f == NULL) 5624632Seric { 5634632Seric syserr("readqf: no cf file %s", cf); 5644632Seric return; 5654632Seric } 5664632Seric 5674632Seric /* 5684632Seric ** Read and process the file. 5694632Seric */ 5704632Seric 5715037Seric if (Verbose) 5726908Seric message(Arpa_Info, "Running %s", cf); 5735037Seric 5744632Seric while (fgets(buf, sizeof buf, f) != NULL) 5754632Seric { 5764632Seric fixcrlf(buf, TRUE); 5774632Seric 5784632Seric switch (buf[0]) 5794632Seric { 5804632Seric case 'R': /* specify recipient */ 5816908Seric sendto(&buf[1], 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5824632Seric break; 5834632Seric 5844632Seric case 'H': /* header */ 5854632Seric (void) chompheader(&buf[1], FALSE); 5864632Seric break; 5874632Seric 5884632Seric case 'S': /* sender */ 5894634Seric setsender(newstr(&buf[1])); 5904632Seric break; 5914632Seric 5924632Seric case 'D': /* data file name */ 5936991Seric CurEnv->e_df = newstr(&buf[1]); 5946991Seric TempFile = fopen(CurEnv->e_df, "r"); 5954632Seric if (TempFile == NULL) 5966991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5974632Seric break; 5984632Seric 5994632Seric case 'T': /* timeout */ 6004632Seric (void) sscanf(&buf[1], "%ld", &TimeOut); 6014632Seric break; 6024632Seric 6034634Seric case 'P': /* message priority */ 6046908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 6055037Seric 6065037Seric /* make sure that big things get sent eventually */ 6076908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 6084634Seric break; 6094634Seric 6106980Seric case 'C': /* message class */ 6116980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 6126980Seric break; 6136980Seric 6145902Seric case 'M': /* define macro */ 6155902Seric define(buf[1], newstr(&buf[2])); 6165902Seric break; 6175902Seric 6184632Seric default: 6194632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 6204632Seric break; 6214632Seric } 6224632Seric } 6234632Seric } 6244632Seric /* 6254632Seric ** TIMEOUT -- process timeout on queue file. 6264632Seric ** 6274632Seric ** Parameters: 6284632Seric ** w -- pointer to work request that timed out. 6294632Seric ** 6304632Seric ** Returns: 6314632Seric ** none. 6324632Seric ** 6334632Seric ** Side Effects: 6344632Seric ** Returns a message to the sender saying that this 6354632Seric ** message has timed out. 6364632Seric */ 6374632Seric 6384632Seric timeout(w) 6394632Seric register WORK *w; 6404632Seric { 6414634Seric # ifdef DEBUG 6424634Seric if (Debug > 0) 6434634Seric printf("timeout(%s)\n", w->w_name); 6444634Seric # endif DEBUG 6456991Seric message(Arpa_Info, "Message has timed out"); 6464634Seric 6474634Seric /* return message to sender */ 6486980Seric (void) returntosender("Cannot send mail for three days", 6496980Seric &CurEnv->e_from, TRUE); 6504634Seric 6514634Seric /* arrange to remove files from queue */ 6526908Seric CurEnv->e_queueup = FALSE; 6534632Seric } 6545182Seric 6555182Seric # endif QUEUE 656