14632Seric # include "sendmail.h" 24632Seric # include <sys/stat.h> 38348Seric # include <dir.h> 44634Seric # include <signal.h> 54632Seric # include <errno.h> 64632Seric 75182Seric # ifndef QUEUE 8*9282Seric SCCSID(@(#)queue.c 3.51 11/17/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*9282Seric SCCSID(@(#)queue.c 3.51 11/17/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 { 367812Seric char *tf; 377812Seric char *qf; 387763Seric char buf[MAXLINE]; 397812Seric register FILE *tfp; 404632Seric register HDR *h; 415007Seric register ADDRESS *q; 425902Seric register int i; 434632Seric 445037Seric /* 455037Seric ** Create control file. 465037Seric */ 474632Seric 487812Seric tf = newstr(queuename(e, 't')); 497812Seric tfp = fopen(tf, "w"); 507812Seric if (tfp == NULL) 514632Seric { 527812Seric syserr("queueup: cannot create temp file %s", tf); 534632Seric return; 544632Seric } 559048Seric (void) chmod(tf, FileMode); 564632Seric 574632Seric # ifdef DEBUG 587677Seric if (tTd(40, 1)) 597812Seric printf("queueing in %s\n", tf); 604632Seric # endif DEBUG 614632Seric 624632Seric /* 636980Seric ** If there is no data file yet, create one. 646980Seric */ 656980Seric 666980Seric if (e->e_df == NULL) 676980Seric { 686980Seric register FILE *dfp; 696980Seric 707812Seric e->e_df = newstr(queuename(e, 'd')); 716980Seric dfp = fopen(e->e_df, "w"); 726980Seric if (dfp == NULL) 736980Seric { 746980Seric syserr("queueup: cannot create %s", e->e_df); 757812Seric (void) fclose(tfp); 766980Seric return; 776980Seric } 789048Seric (void) chmod(e->e_df, FileMode); 796980Seric (*e->e_putbody)(dfp, Mailer[1], FALSE); 807009Seric (void) fclose(dfp); 816980Seric } 826980Seric 836980Seric /* 844632Seric ** Output future work requests. 854632Seric */ 864632Seric 874632Seric /* output name of data file */ 887812Seric fprintf(tfp, "D%s\n", e->e_df); 894632Seric 904632Seric /* output name of sender */ 917812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 924632Seric 937860Seric /* output creation time */ 947860Seric fprintf(tfp, "T%ld\n", e->e_ctime); 954632Seric 964634Seric /* output message priority */ 977812Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 984634Seric 996980Seric /* output message class */ 1007812Seric fprintf(tfp, "C%d\n", e->e_class); 1016980Seric 1025902Seric /* output macro definitions */ 1037763Seric /* I don't think this is needed any more..... 1045902Seric for (i = 0; i < 128; i++) 1055902Seric { 1066980Seric register char *p = e->e_macro[i]; 1075902Seric 1085902Seric if (p != NULL && i != (int) 'b') 1097812Seric fprintf(tfp, "M%c%s\n", i, p); 1105902Seric } 1117763Seric ..... */ 1125902Seric 1134632Seric /* output list of recipient addresses */ 1146980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1154632Seric { 1165037Seric # ifdef DEBUG 1177677Seric if (tTd(40, 1)) 1185037Seric { 1195037Seric printf("queueing "); 1205037Seric printaddr(q, FALSE); 1215037Seric } 1225037Seric # endif DEBUG 1237763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1247763Seric bitset(QQUEUEUP, q->q_flags)) 1258245Seric { 1267812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1278245Seric } 1284632Seric } 1294632Seric 1304632Seric /* output headers for this message */ 1317763Seric define('g', "$f"); 1326980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1334632Seric { 1344632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1354632Seric continue; 1367812Seric fprintf(tfp, "H"); 1374632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1387812Seric mfdecode(h->h_mflags, tfp); 1397763Seric if (bitset(H_DEFAULT, h->h_flags)) 1407763Seric { 1417763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 1428236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 1437763Seric } 1448245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 1458245Seric commaize(h, h->h_value, tfp, e->e_oldstyle, NULL); 1467763Seric else 1478245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 1484632Seric } 1494632Seric 1504632Seric /* 1514632Seric ** Clean up. 1524632Seric */ 1534632Seric 1547812Seric (void) fclose(tfp); 1557812Seric qf = queuename(e, 'q'); 1567812Seric (void) unlink(qf); 1577812Seric if (link(tf, qf) < 0) 1587812Seric syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 1596980Seric else 1607812Seric (void) unlink(tf); 1617391Seric 1627677Seric # ifdef LOG 1637677Seric /* save log info */ 1647878Seric if (LogLevel > 15) 1657878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 1667677Seric # endif LOG 1674632Seric } 1684632Seric /* 1694632Seric ** RUNQUEUE -- run the jobs in the queue. 1704632Seric ** 1714632Seric ** Gets the stuff out of the queue in some presumably logical 1724632Seric ** order and processes them. 1734632Seric ** 1744632Seric ** Parameters: 1754632Seric ** none. 1764632Seric ** 1774632Seric ** Returns: 1784632Seric ** none. 1794632Seric ** 1804632Seric ** Side Effects: 1814632Seric ** runs things in the mail queue. 1824632Seric */ 1834632Seric 1844639Seric runqueue(forkflag) 1854639Seric bool forkflag; 1864632Seric { 1877943Seric register int i; 1884632Seric 1897466Seric /* 1907466Seric ** See if we want to go off and do other useful work. 1917466Seric */ 1924639Seric 1934639Seric if (forkflag) 1944639Seric { 1957943Seric int pid; 1967943Seric 1977943Seric pid = dofork(); 1987943Seric if (pid != 0) 1994639Seric { 2007943Seric /* parent -- pick up intermediate zombie */ 2017943Seric do 2027943Seric { 2037943Seric auto int stat; 2047943Seric 2057943Seric i = wait(&stat); 2067943Seric } while (i >= 0 && i != pid); 2077690Seric if (QueueIntvl != 0) 2087943Seric setevent(QueueIntvl, runqueue, TRUE); 2094639Seric return; 2104639Seric } 2117943Seric /* child -- double fork */ 2127943Seric if (fork() != 0) 2137943Seric exit(EX_OK); 2144639Seric } 2157876Seric # ifdef LOG 2167876Seric if (LogLevel > 11) 2177943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 2187876Seric # endif LOG 2194639Seric 2207466Seric /* 2217466Seric ** Start making passes through the queue. 2227466Seric ** First, read and sort the entire queue. 2237466Seric ** Then, process the work in that order. 2247466Seric ** But if you take too long, start over. 2257466Seric */ 2267466Seric 2277943Seric /* order the existing work requests */ 2287943Seric orderq(); 2297690Seric 2307943Seric /* process them once at a time */ 2317943Seric while (WorkQ != NULL) 2324639Seric { 2337943Seric WORK *w = WorkQ; 2347881Seric 2357943Seric WorkQ = WorkQ->w_next; 2367943Seric dowork(w); 2377943Seric free(w->w_name); 2387943Seric free((char *) w); 2394639Seric } 2407943Seric finis(); 2414634Seric } 2424634Seric /* 2434632Seric ** ORDERQ -- order the work queue. 2444632Seric ** 2454632Seric ** Parameters: 2464632Seric ** none. 2474632Seric ** 2484632Seric ** Returns: 2494632Seric ** none. 2504632Seric ** 2514632Seric ** Side Effects: 2524632Seric ** Sets WorkQ to the queue of available work, in order. 2534632Seric */ 2544632Seric 2554632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2564632Seric 2574632Seric orderq() 2584632Seric { 2596625Sglickman register struct direct *d; 2604632Seric register WORK *w; 2614632Seric register WORK **wp; /* parent of w */ 2626625Sglickman DIR *f; 2634632Seric register int i; 2644632Seric WORK wlist[WLSIZE]; 2654632Seric int wn = 0; 2664632Seric extern workcmpf(); 2674632Seric 2684632Seric /* clear out old WorkQ */ 2694632Seric for (w = WorkQ; w != NULL; ) 2704632Seric { 2714632Seric register WORK *nw = w->w_next; 2724632Seric 2734632Seric WorkQ = nw; 2744632Seric free(w->w_name); 2754632Seric free((char *) w); 2764632Seric w = nw; 2774632Seric } 2784632Seric 2794632Seric /* open the queue directory */ 2808148Seric f = opendir("."); 2814632Seric if (f == NULL) 2824632Seric { 2838148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2844632Seric return; 2854632Seric } 2864632Seric 2874632Seric /* 2884632Seric ** Read the work directory. 2894632Seric */ 2904632Seric 2916625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 2924632Seric { 2934632Seric char lbuf[MAXNAME]; 2944632Seric FILE *cf; 2954632Seric 2964632Seric /* is this an interesting entry? */ 2977812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 2984632Seric continue; 2994632Seric 3008148Seric /* yes -- open control file */ 3018148Seric cf = fopen(d->d_name, "r"); 3024632Seric if (cf == NULL) 3034632Seric { 3047055Seric /* this may be some random person sending hir msgs */ 3057055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3067055Seric errno = 0; 3074632Seric continue; 3084632Seric } 3098148Seric wlist[wn].w_name = newstr(d->d_name); 3104632Seric 3114632Seric /* extract useful information */ 3124632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3134632Seric { 3144632Seric fixcrlf(lbuf, TRUE); 3154632Seric 3164632Seric switch (lbuf[0]) 3174632Seric { 3184632Seric case 'P': /* message priority */ 3195037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3204632Seric break; 3214632Seric } 3224632Seric } 3234632Seric wn++; 3244632Seric (void) fclose(cf); 3254632Seric } 3266625Sglickman (void) closedir(f); 3274632Seric 3284632Seric /* 3294632Seric ** Sort the work directory. 3304632Seric */ 3314632Seric 3324632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3334632Seric 3344632Seric /* 3354632Seric ** Convert the work list into canonical form. 3364632Seric */ 3374632Seric 3384632Seric wp = &WorkQ; 3394632Seric for (i = 0; i < wn; i++) 3404632Seric { 3414632Seric w = (WORK *) xalloc(sizeof *w); 3424632Seric w->w_name = wlist[i].w_name; 3434632Seric w->w_pri = wlist[i].w_pri; 3444632Seric w->w_next = NULL; 3454632Seric *wp = w; 3464632Seric wp = &w->w_next; 3474632Seric } 3484632Seric 3494632Seric # ifdef DEBUG 3507677Seric if (tTd(40, 1)) 3514632Seric { 3524632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3535037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3544632Seric } 3554632Seric # endif DEBUG 3564632Seric } 3574632Seric /* 3587677Seric ** WORKCMPF -- compare function for ordering work. 3594632Seric ** 3604632Seric ** Parameters: 3614632Seric ** a -- the first argument. 3624632Seric ** b -- the second argument. 3634632Seric ** 3644632Seric ** Returns: 3654632Seric ** -1 if a < b 3664632Seric ** 0 if a == b 3674632Seric ** 1 if a > b 3684632Seric ** 3694632Seric ** Side Effects: 3704632Seric ** none. 3714632Seric */ 3724632Seric 3734632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3744632Seric 3754632Seric workcmpf(a, b) 3765037Seric register WORK *a; 3775037Seric register WORK *b; 3784632Seric { 3795037Seric if (a->w_pri == b->w_pri) 3804632Seric return (0); 3815037Seric else if (a->w_pri > b->w_pri) 3824632Seric return (1); 3834632Seric else 3844632Seric return (-1); 3854632Seric } 3864632Seric /* 3874632Seric ** DOWORK -- do a work request. 3884632Seric ** 3894632Seric ** Parameters: 3904632Seric ** w -- the work request to be satisfied. 3914632Seric ** 3924632Seric ** Returns: 3934632Seric ** none. 3944632Seric ** 3954632Seric ** Side Effects: 3964632Seric ** The work request is satisfied if possible. 3974632Seric */ 3984632Seric 3994632Seric dowork(w) 4004632Seric register WORK *w; 4014632Seric { 4024632Seric register int i; 4034632Seric auto int xstat; 4044632Seric 4054632Seric # ifdef DEBUG 4067677Seric if (tTd(40, 1)) 4075037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4084632Seric # endif DEBUG 4094632Seric 4104632Seric /* 4114632Seric ** Fork for work. 4124632Seric */ 4134632Seric 4144632Seric i = fork(); 4154632Seric if (i < 0) 4164632Seric { 4174632Seric syserr("dowork: cannot fork"); 4184632Seric return; 4194632Seric } 4204632Seric 4214632Seric if (i == 0) 4224632Seric { 4236980Seric char buf[MAXNAME]; 4246980Seric 4254632Seric /* 4264632Seric ** CHILD 4278148Seric ** Lock the control file to avoid duplicate deliveries. 4288148Seric ** Then run the file as though we had just read it. 4297350Seric ** We save an idea of the temporary name so we 4307350Seric ** can recover on interrupt. 4314632Seric */ 4324632Seric 4337763Seric /* set basic modes, etc. */ 4347356Seric (void) alarm(0); 4357347Seric FatalErrors = FALSE; 4364632Seric QueueRun = TRUE; 4376991Seric MailBack = TRUE; 4387812Seric CurEnv->e_qf = w->w_name; 4398148Seric CurEnv->e_id = &w->w_name[2]; 4407876Seric # ifdef LOG 4417876Seric if (LogLevel > 11) 4427881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4437881Seric getpid()); 4447876Seric # endif LOG 4457763Seric 4467763Seric /* don't use the headers from sendmail.cf... */ 4477763Seric CurEnv->e_header = NULL; 4487763Seric chompheader("from: $q", TRUE); 4497763Seric 4507763Seric /* create the link to the control file during processing */ 4517812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4526980Seric { 4537812Seric /* being processed by another queuer */ 4547881Seric # ifdef LOG 4557881Seric if (LogLevel > 4) 4567881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4577881Seric # endif LOG 4586980Seric exit(EX_OK); 4596980Seric } 4606980Seric 4616980Seric /* create ourselves a transcript file */ 4624634Seric openxscrpt(); 4636980Seric 4646980Seric /* do basic system initialization */ 4654632Seric initsys(); 4666980Seric 4676980Seric /* read the queue control file */ 4687812Seric readqf(CurEnv->e_qf); 4697781Seric eatheader(); 4706980Seric 4716980Seric /* do the delivery */ 4727558Seric if (!FatalErrors) 473*9282Seric sendall(CurEnv, SM_DELIVER); 4746980Seric 4756980Seric /* if still not sent, perhaps we should time out.... */ 4764634Seric # ifdef DEBUG 4777677Seric if (tTd(40, 3)) 4787886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4797860Seric CurEnv->e_ctime + TimeOut); 4804634Seric # endif DEBUG 4817886Seric if (CurEnv->e_queueup && curtime() > CurEnv->e_ctime + TimeOut) 4824634Seric timeout(w); 4836980Seric 4846980Seric /* finish up and exit */ 4854632Seric finis(); 4864632Seric } 4874632Seric 4884632Seric /* 4894632Seric ** Parent -- pick up results. 4904632Seric */ 4914632Seric 4924632Seric errno = 0; 4934632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 4944632Seric { 4954632Seric if (errno == EINTR) 4964632Seric { 4974632Seric errno = 0; 4984632Seric } 4994632Seric } 5004632Seric } 5014632Seric /* 5024632Seric ** READQF -- read queue file and set up environment. 5034632Seric ** 5044632Seric ** Parameters: 5054632Seric ** cf -- name of queue control file. 5064632Seric ** 5074632Seric ** Returns: 5084632Seric ** none. 5094632Seric ** 5104632Seric ** Side Effects: 5114632Seric ** cf is read and created as the current job, as though 5124632Seric ** we had been invoked by argument. 5134632Seric */ 5144632Seric 5154632Seric readqf(cf) 5164632Seric char *cf; 5174632Seric { 5184632Seric register FILE *f; 5197785Seric char buf[MAXFIELD]; 5207753Seric register char *p; 5217753Seric register int i; 5224632Seric 5234632Seric /* 5244632Seric ** Open the file created by queueup. 5254632Seric */ 5264632Seric 5274632Seric f = fopen(cf, "r"); 5284632Seric if (f == NULL) 5294632Seric { 5304632Seric syserr("readqf: no cf file %s", cf); 5314632Seric return; 5324632Seric } 5334632Seric 5344632Seric /* 5354632Seric ** Read and process the file. 5364632Seric */ 5374632Seric 5387356Seric if (Verbose) 5397356Seric printf("\nRunning %s\n", cf); 5407785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5414632Seric { 5424632Seric switch (buf[0]) 5434632Seric { 5444632Seric case 'R': /* specify recipient */ 5458080Seric sendto(&buf[1], (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5464632Seric break; 5474632Seric 5484632Seric case 'H': /* header */ 5494632Seric (void) chompheader(&buf[1], FALSE); 5504632Seric break; 5514632Seric 5524632Seric case 'S': /* sender */ 5534634Seric setsender(newstr(&buf[1])); 5544632Seric break; 5554632Seric 5564632Seric case 'D': /* data file name */ 5576991Seric CurEnv->e_df = newstr(&buf[1]); 5586991Seric TempFile = fopen(CurEnv->e_df, "r"); 5594632Seric if (TempFile == NULL) 5606991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5614632Seric break; 5624632Seric 5637860Seric case 'T': /* init time */ 5647860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5654632Seric break; 5664632Seric 5674634Seric case 'P': /* message priority */ 5686908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5695037Seric 5705037Seric /* make sure that big things get sent eventually */ 5716908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5724634Seric break; 5734634Seric 5746980Seric case 'C': /* message class */ 5756980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5766980Seric break; 5776980Seric 5785902Seric case 'M': /* define macro */ 5795902Seric define(buf[1], newstr(&buf[2])); 5805902Seric break; 5815902Seric 5824632Seric default: 5834632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5844632Seric break; 5854632Seric } 5864632Seric } 5874632Seric } 5884632Seric /* 5894632Seric ** TIMEOUT -- process timeout on queue file. 5904632Seric ** 5914632Seric ** Parameters: 5924632Seric ** w -- pointer to work request that timed out. 5934632Seric ** 5944632Seric ** Returns: 5954632Seric ** none. 5964632Seric ** 5974632Seric ** Side Effects: 5984632Seric ** Returns a message to the sender saying that this 5994632Seric ** message has timed out. 6004632Seric */ 6014632Seric 6024632Seric timeout(w) 6034632Seric register WORK *w; 6044632Seric { 6057369Seric char buf[MAXLINE]; 6067860Seric extern char *pintvl(); 6077369Seric 6084634Seric # ifdef DEBUG 6097677Seric if (tTd(40, 3)) 6104634Seric printf("timeout(%s)\n", w->w_name); 6114634Seric # endif DEBUG 6126991Seric message(Arpa_Info, "Message has timed out"); 6134634Seric 6144634Seric /* return message to sender */ 6157860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6167369Seric (void) returntosender(buf, &CurEnv->e_from, TRUE); 6174634Seric 6184634Seric /* arrange to remove files from queue */ 6197860Seric CurEnv->e_dontqueue = TRUE; 6204632Seric } 6215182Seric 6225182Seric # endif QUEUE 623