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*6991Seric SCCSID(@(#)queue.c 3.14 05/31/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*6991Seric SCCSID(@(#)queue.c 3.14 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. 214632Seric ** 224632Seric ** Returns: 234632Seric ** none. 244632Seric ** 254632Seric ** Side Effects: 264632Seric ** The current request (only unsatisfied addresses) 274632Seric ** are saved in a control file. 284632Seric */ 294632Seric 306980Seric queueup(e) 316980Seric register ENVELOPE *e; 324632Seric { 334632Seric char cf[MAXNAME]; 346980Seric char buf[MAXNAME]; 356980Seric register FILE *cfp; 364632Seric register HDR *h; 375007Seric register ADDRESS *q; 385199Seric extern char *mktemp(); 395902Seric register int i; 404632Seric 415037Seric /* 425037Seric ** Create control file. 435037Seric */ 444632Seric 455037Seric strcpy(cf, QueueDir); 466980Seric strcat(cf, "/tfXXXXXX"); 475037Seric (void) mktemp(cf); 486980Seric cfp = fopen(cf, "w"); 496980Seric if (cfp == NULL) 504632Seric { 514632Seric syserr("queueup: cannot create control file %s", cf); 524632Seric return; 534632Seric } 544632Seric 554632Seric # ifdef DEBUG 564632Seric if (Debug) 576980Seric printf("queueing in %s\n", cf); 584632Seric # endif DEBUG 594632Seric 604632Seric /* 616980Seric ** If there is no data file yet, create one. 626980Seric */ 636980Seric 646980Seric if (e->e_df == NULL) 656980Seric { 666980Seric register FILE *dfp; 676980Seric 686980Seric strcpy(buf, QueueDir); 696980Seric strcat(buf, "/dfXXXXXX"); 706980Seric e->e_df = newstr(mktemp(buf)); 716980Seric dfp = fopen(e->e_df, "w"); 726980Seric if (dfp == NULL) 736980Seric { 746980Seric syserr("queueup: cannot create %s", e->e_df); 756980Seric fclose(cfp); 766980Seric return; 776980Seric } 786980Seric (*e->e_putbody)(dfp, Mailer[1], FALSE); 796980Seric fclose(dfp); 806980Seric } 816980Seric 826980Seric /* 834632Seric ** Output future work requests. 844632Seric */ 854632Seric 864632Seric /* output name of data file */ 876980Seric fprintf(cfp, "D%s\n", e->e_df); 884632Seric 894632Seric /* output name of sender */ 906980Seric fprintf(cfp, "S%s\n", e->e_from.q_paddr); 914632Seric 924632Seric /* output timeout */ 936980Seric fprintf(cfp, "T%ld\n", TimeOut); 944632Seric 954634Seric /* output message priority */ 966980Seric fprintf(cfp, "P%ld\n", e->e_msgpriority); 974634Seric 986980Seric /* output message class */ 996980Seric fprintf(cfp, "C%d\n", e->e_class); 1006980Seric 1015902Seric /* output macro definitions */ 1025902Seric for (i = 0; i < 128; i++) 1035902Seric { 1046980Seric register char *p = e->e_macro[i]; 1055902Seric 1065902Seric if (p != NULL && i != (int) 'b') 1076980Seric fprintf(cfp, "M%c%s\n", i, p); 1085902Seric } 1095902Seric 1104632Seric /* output list of recipient addresses */ 1116980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1124632Seric { 1135037Seric # ifdef DEBUG 1145037Seric if (Debug > 0) 1155037Seric { 1165037Seric printf("queueing "); 1175037Seric printaddr(q, FALSE); 1185037Seric } 1195037Seric # endif DEBUG 1205037Seric if (bitset(QQUEUEUP, q->q_flags)) 1216980Seric fprintf(cfp, "R%s\n", q->q_paddr); 1224632Seric } 1234632Seric 1244632Seric /* output headers for this message */ 1256980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1264632Seric { 1274632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1284632Seric continue; 1296980Seric fprintf(cfp, "H"); 1304632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1316980Seric mfdecode(h->h_mflags, cfp); 1326980Seric fprintf(cfp, "%s: %s\n", h->h_field, h->h_value); 1334632Seric } 1344632Seric 1354632Seric /* 1364632Seric ** Clean up. 1374632Seric */ 1384632Seric 1396980Seric (void) fclose(cfp); 1406980Seric (void) strcpy(buf, QueueDir); 1416980Seric (void) strcat(buf, "/cfXXXXXX"); 1426980Seric (void) mktemp(buf); 1436980Seric if (link(cf, buf) < 0) 1446980Seric syserr("cannot link(%s, %s), df=%s", cf, buf, e->e_df); 1456980Seric else 1466980Seric unlink(cf); 1474632Seric } 1484632Seric /* 1494632Seric ** RUNQUEUE -- run the jobs in the queue. 1504632Seric ** 1514632Seric ** Gets the stuff out of the queue in some presumably logical 1524632Seric ** order and processes them. 1534632Seric ** 1544632Seric ** Parameters: 1554632Seric ** none. 1564632Seric ** 1574632Seric ** Returns: 1584632Seric ** none. 1594632Seric ** 1604632Seric ** Side Effects: 1614632Seric ** runs things in the mail queue. 1624632Seric */ 1634632Seric 1644639Seric bool ReorderQueue; /* if set, reorder the send queue */ 1654639Seric int QueuePid; /* pid of child running queue */ 1664634Seric 1674639Seric runqueue(forkflag) 1684639Seric bool forkflag; 1694632Seric { 1704634Seric extern reordersig(); 1714632Seric 1724639Seric if (QueueIntvl != 0) 1734639Seric { 1744836Seric (void) signal(SIGALRM, reordersig); 1754836Seric (void) alarm((unsigned) QueueIntvl); 1764639Seric } 1774639Seric 1784639Seric if (forkflag) 1794639Seric { 1804639Seric QueuePid = dofork(); 1814639Seric if (QueuePid > 0) 1824639Seric { 1834639Seric /* parent */ 1844639Seric return; 1854639Seric } 1864639Seric else 1874836Seric (void) alarm((unsigned) 0); 1884639Seric } 1894639Seric 1904634Seric for (;;) 1914634Seric { 1924634Seric /* 1934634Seric ** Order the existing work requests. 1944634Seric */ 1954632Seric 1964634Seric orderq(); 1974632Seric 1984634Seric if (WorkQ == NULL) 1994634Seric { 2004634Seric /* no work? well, maybe later */ 2014634Seric if (QueueIntvl == 0) 2024634Seric break; 2034639Seric pause(); 2044634Seric continue; 2054634Seric } 2064632Seric 2074634Seric ReorderQueue = FALSE; 2084634Seric 2094634Seric /* 2104634Seric ** Process them once at a time. 2114634Seric ** The queue could be reordered while we do this to take 2124634Seric ** new requests into account. If so, the existing job 2134634Seric ** will be finished but the next thing taken off WorkQ 2144634Seric ** may be something else. 2154634Seric */ 2164634Seric 2174634Seric while (WorkQ != NULL) 2184634Seric { 2194634Seric WORK *w = WorkQ; 2204634Seric 2214634Seric WorkQ = WorkQ->w_next; 2224634Seric dowork(w); 2234634Seric free(w->w_name); 2244634Seric free((char *) w); 2254634Seric if (ReorderQueue) 2264634Seric break; 2274634Seric } 2284634Seric 2294634Seric if (QueueIntvl == 0) 2304634Seric break; 2314632Seric } 2325978Seric 2335978Seric /* no work to do -- just exit */ 2345978Seric finis(); 2354632Seric } 2364632Seric /* 2374634Seric ** REORDERSIG -- catch the alarm signal and tell sendmail to reorder queue. 2384634Seric ** 2394634Seric ** Parameters: 2404634Seric ** none. 2414634Seric ** 2424634Seric ** Returns: 2434634Seric ** none. 2444634Seric ** 2454634Seric ** Side Effects: 2464634Seric ** sets the "reorder work queue" flag. 2474634Seric */ 2484634Seric 2494634Seric reordersig() 2504634Seric { 2514639Seric if (QueuePid == 0) 2524639Seric { 2534639Seric /* we are in a child doing queueing */ 2544639Seric ReorderQueue = TRUE; 2554639Seric } 2564639Seric else 2574639Seric { 2584639Seric /* we are in a parent -- poke child or start new one */ 2594639Seric if (kill(QueuePid, SIGALRM) < 0) 2604639Seric { 2614639Seric /* no child -- get zombie & start new one */ 2624639Seric static int st; 2634639Seric 2644836Seric (void) wait(&st); 2654639Seric QueuePid = dofork(); 2664639Seric if (QueuePid == 0) 2674639Seric { 2684639Seric /* new child; run queue */ 2694836Seric runqueue(FALSE); 2704639Seric finis(); 2714639Seric } 2724639Seric } 2734639Seric } 2744639Seric 2754639Seric /* 2764639Seric ** Arrange to get this signal again. 2774639Seric */ 2784639Seric 2796065Seric (void) signal(SIGALRM, reordersig); 2804836Seric (void) alarm((unsigned) QueueIntvl); 2814634Seric } 2824634Seric /* 2834632Seric ** ORDERQ -- order the work queue. 2844632Seric ** 2854632Seric ** Parameters: 2864632Seric ** none. 2874632Seric ** 2884632Seric ** Returns: 2894632Seric ** none. 2904632Seric ** 2914632Seric ** Side Effects: 2924632Seric ** Sets WorkQ to the queue of available work, in order. 2934632Seric */ 2944632Seric 2954632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2964632Seric 2974632Seric orderq() 2984632Seric { 2996625Sglickman register struct direct *d; 3004632Seric register WORK *w; 3014632Seric register WORK **wp; /* parent of w */ 3026625Sglickman DIR *f; 3034632Seric register int i; 3044632Seric WORK wlist[WLSIZE]; 3054632Seric int wn = 0; 3064632Seric extern workcmpf(); 3074632Seric extern char *QueueDir; 3084632Seric 3094632Seric /* clear out old WorkQ */ 3104632Seric for (w = WorkQ; w != NULL; ) 3114632Seric { 3124632Seric register WORK *nw = w->w_next; 3134632Seric 3144632Seric WorkQ = nw; 3154632Seric free(w->w_name); 3164632Seric free((char *) w); 3174632Seric w = nw; 3184632Seric } 3194632Seric 3204632Seric /* open the queue directory */ 3216625Sglickman f = opendir(QueueDir); 3224632Seric if (f == NULL) 3234632Seric { 3244632Seric syserr("orderq: cannot open %s", QueueDir); 3254632Seric return; 3264632Seric } 3274632Seric 3284632Seric /* 3294632Seric ** Read the work directory. 3304632Seric */ 3314632Seric 3326625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 3334632Seric { 3344632Seric char cbuf[MAXNAME]; 3354632Seric char lbuf[MAXNAME]; 3364632Seric FILE *cf; 3374632Seric register char *p; 3384632Seric 3394632Seric /* is this an interesting entry? */ 3406625Sglickman if (d->d_name[0] != 'c') 3414632Seric continue; 3424632Seric 3434632Seric /* yes -- find the control file location */ 3444632Seric strcpy(cbuf, QueueDir); 3454632Seric strcat(cbuf, "/"); 3464632Seric p = &cbuf[strlen(cbuf)]; 3476625Sglickman strcpy(p, d->d_name); 3484632Seric 3494632Seric /* open control file */ 3504632Seric cf = fopen(cbuf, "r"); 3514632Seric if (cf == NULL) 3524632Seric { 3534632Seric syserr("orderq: cannot open %s", cbuf); 3544632Seric continue; 3554632Seric } 3565037Seric wlist[wn].w_name = newstr(cbuf); 3574632Seric 3584632Seric /* extract useful information */ 3594632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3604632Seric { 3614632Seric fixcrlf(lbuf, TRUE); 3624632Seric 3634632Seric switch (lbuf[0]) 3644632Seric { 3654632Seric case 'P': /* message priority */ 3665037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3674632Seric break; 3684632Seric } 3694632Seric } 3704632Seric wn++; 3714632Seric (void) fclose(cf); 3724632Seric } 3736625Sglickman (void) closedir(f); 3744632Seric 3754632Seric /* 3764632Seric ** Sort the work directory. 3774632Seric */ 3784632Seric 3794632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3804632Seric 3814632Seric /* 3824632Seric ** Convert the work list into canonical form. 3834632Seric */ 3844632Seric 3854632Seric wp = &WorkQ; 3864632Seric for (i = 0; i < wn; i++) 3874632Seric { 3884632Seric w = (WORK *) xalloc(sizeof *w); 3894632Seric w->w_name = wlist[i].w_name; 3904632Seric w->w_pri = wlist[i].w_pri; 3914632Seric w->w_next = NULL; 3924632Seric *wp = w; 3934632Seric wp = &w->w_next; 3944632Seric } 3954632Seric 3964632Seric # ifdef DEBUG 3974632Seric if (Debug) 3984632Seric { 3994632Seric for (w = WorkQ; w != NULL; w = w->w_next) 4005037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 4014632Seric } 4024632Seric # endif DEBUG 4034632Seric } 4044632Seric /* 4054632Seric ** WORKCMPF -- compare function for ordering work. 4064632Seric ** 4074632Seric ** Parameters: 4084632Seric ** a -- the first argument. 4094632Seric ** b -- the second argument. 4104632Seric ** 4114632Seric ** Returns: 4124632Seric ** -1 if a < b 4134632Seric ** 0 if a == b 4144632Seric ** 1 if a > b 4154632Seric ** 4164632Seric ** Side Effects: 4174632Seric ** none. 4184632Seric */ 4194632Seric 4204632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 4214632Seric 4224632Seric workcmpf(a, b) 4235037Seric register WORK *a; 4245037Seric register WORK *b; 4254632Seric { 4265037Seric if (a->w_pri == b->w_pri) 4274632Seric return (0); 4285037Seric else if (a->w_pri > b->w_pri) 4294632Seric return (1); 4304632Seric else 4314632Seric return (-1); 4324632Seric } 4334632Seric /* 4344632Seric ** DOWORK -- do a work request. 4354632Seric ** 4364632Seric ** Parameters: 4374632Seric ** w -- the work request to be satisfied. 4384632Seric ** 4394632Seric ** Returns: 4404632Seric ** none. 4414632Seric ** 4424632Seric ** Side Effects: 4434632Seric ** The work request is satisfied if possible. 4444632Seric */ 4454632Seric 4464632Seric dowork(w) 4474632Seric register WORK *w; 4484632Seric { 4494632Seric register int i; 4504632Seric auto int xstat; 4514632Seric 4524632Seric # ifdef DEBUG 4534632Seric if (Debug) 4545037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4554632Seric # endif DEBUG 4564632Seric 4574632Seric /* 4584632Seric ** Fork for work. 4594632Seric */ 4604632Seric 4614632Seric i = fork(); 4624632Seric if (i < 0) 4634632Seric { 4644632Seric syserr("dowork: cannot fork"); 4654632Seric return; 4664632Seric } 4674632Seric 4684632Seric if (i == 0) 4694632Seric { 4706980Seric char buf[MAXNAME]; 4716980Seric 4724632Seric /* 4734632Seric ** CHILD 4746980Seric ** Change the name of the control file to avoid 4756980Seric ** duplicate deliveries. Then run the file as 4766980Seric ** though we had just read it. 4774632Seric */ 4784632Seric 4794632Seric QueueRun = TRUE; 480*6991Seric MailBack = TRUE; 4816980Seric (void) strcpy(buf, QueueDir); 4826980Seric (void) strcat(buf, "/tfXXXXXX"); 4836980Seric (void) mktemp(buf); 4846980Seric if (link(w->w_name, buf) < 0) 4856980Seric { 4866980Seric syserr("dowork: link(%s, %s)", w->w_name, buf); 4876980Seric 4886980Seric /* it's ok to lie -- it will be run later */ 4896980Seric exit(EX_OK); 4906980Seric } 4916980Seric (void) unlink(w->w_name); 4926980Seric 4936980Seric /* create ourselves a transcript file */ 4944634Seric openxscrpt(); 4956980Seric 4966980Seric /* do basic system initialization */ 4974632Seric initsys(); 4986980Seric 4996980Seric /* read the queue control file */ 5006980Seric readqf(buf); 5016980Seric 5026980Seric /* do the delivery */ 5034632Seric sendall(FALSE); 5046980Seric 5056980Seric /* if still not sent, perhaps we should time out.... */ 5064634Seric # ifdef DEBUG 5074634Seric if (Debug > 2) 5084634Seric printf("CurTime=%ld, TimeOut=%ld\n", CurTime, TimeOut); 5094634Seric # endif DEBUG 5106908Seric if (CurEnv->e_queueup && CurTime > TimeOut) 5114634Seric timeout(w); 5126980Seric 5136980Seric /* get rid of the temporary file -- a new cf will be made */ 5146980Seric (void) unlink(buf); 5156980Seric 5166980Seric /* finish up and exit */ 5174632Seric finis(); 5184632Seric } 5194632Seric 5204632Seric /* 5214632Seric ** Parent -- pick up results. 5224632Seric */ 5234632Seric 5244632Seric errno = 0; 5254632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 5264632Seric { 5274632Seric if (errno == EINTR) 5284632Seric { 5294632Seric errno = 0; 5304632Seric } 5314632Seric } 5324632Seric } 5334632Seric /* 5344632Seric ** READQF -- read queue file and set up environment. 5354632Seric ** 5364632Seric ** Parameters: 5374632Seric ** cf -- name of queue control file. 5384632Seric ** 5394632Seric ** Returns: 5404632Seric ** none. 5414632Seric ** 5424632Seric ** Side Effects: 5434632Seric ** cf is read and created as the current job, as though 5444632Seric ** we had been invoked by argument. 5454632Seric */ 5464632Seric 5474632Seric readqf(cf) 5484632Seric char *cf; 5494632Seric { 5504632Seric register FILE *f; 5514632Seric char buf[MAXLINE]; 5524632Seric 5534632Seric /* 5544632Seric ** Open the file created by queueup. 5554632Seric */ 5564632Seric 5574632Seric f = fopen(cf, "r"); 5584632Seric if (f == NULL) 5594632Seric { 5604632Seric syserr("readqf: no cf file %s", cf); 5614632Seric return; 5624632Seric } 5634632Seric 5644632Seric /* 5654632Seric ** Read and process the file. 5664632Seric */ 5674632Seric 5685037Seric if (Verbose) 5696908Seric message(Arpa_Info, "Running %s", cf); 5705037Seric 5714632Seric while (fgets(buf, sizeof buf, f) != NULL) 5724632Seric { 5734632Seric fixcrlf(buf, TRUE); 5744632Seric 5754632Seric switch (buf[0]) 5764632Seric { 5774632Seric case 'R': /* specify recipient */ 5786908Seric sendto(&buf[1], 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5794632Seric break; 5804632Seric 5814632Seric case 'H': /* header */ 5824632Seric (void) chompheader(&buf[1], FALSE); 5834632Seric break; 5844632Seric 5854632Seric case 'S': /* sender */ 5864634Seric setsender(newstr(&buf[1])); 5874632Seric break; 5884632Seric 5894632Seric case 'D': /* data file name */ 590*6991Seric CurEnv->e_df = newstr(&buf[1]); 591*6991Seric TempFile = fopen(CurEnv->e_df, "r"); 5924632Seric if (TempFile == NULL) 593*6991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5944632Seric break; 5954632Seric 5964632Seric case 'T': /* timeout */ 5974632Seric (void) sscanf(&buf[1], "%ld", &TimeOut); 5984632Seric break; 5994632Seric 6004634Seric case 'P': /* message priority */ 6016908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 6025037Seric 6035037Seric /* make sure that big things get sent eventually */ 6046908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 6054634Seric break; 6064634Seric 6076980Seric case 'C': /* message class */ 6086980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 6096980Seric break; 6106980Seric 6115902Seric case 'M': /* define macro */ 6125902Seric define(buf[1], newstr(&buf[2])); 6135902Seric break; 6145902Seric 6154632Seric default: 6164632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 6174632Seric break; 6184632Seric } 6194632Seric } 6204632Seric } 6214632Seric /* 6224632Seric ** TIMEOUT -- process timeout on queue file. 6234632Seric ** 6244632Seric ** Parameters: 6254632Seric ** w -- pointer to work request that timed out. 6264632Seric ** 6274632Seric ** Returns: 6284632Seric ** none. 6294632Seric ** 6304632Seric ** Side Effects: 6314632Seric ** Returns a message to the sender saying that this 6324632Seric ** message has timed out. 6334632Seric */ 6344632Seric 6354632Seric timeout(w) 6364632Seric register WORK *w; 6374632Seric { 6384634Seric # ifdef DEBUG 6394634Seric if (Debug > 0) 6404634Seric printf("timeout(%s)\n", w->w_name); 6414634Seric # endif DEBUG 642*6991Seric message(Arpa_Info, "Message has timed out"); 6434634Seric 6444634Seric /* return message to sender */ 6456980Seric (void) returntosender("Cannot send mail for three days", 6466980Seric &CurEnv->e_from, TRUE); 6474634Seric 6484634Seric /* arrange to remove files from queue */ 6496908Seric CurEnv->e_queueup = FALSE; 6504632Seric } 6515182Seric 6525182Seric # endif QUEUE 653