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*8245Seric SCCSID(@(#)queue.c 3.44 09/22/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*8245Seric SCCSID(@(#)queue.c 3.44 09/22/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 } 557812Seric (void) chmod(tf, 0600); 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 } 787010Seric (void) chmod(e->e_df, 0600); 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)) 125*8245Seric { 1267812Seric fprintf(tfp, "R%s\n", q->q_paddr); 127*8245Seric } 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 } 144*8245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 145*8245Seric commaize(h, h->h_value, tfp, e->e_oldstyle, NULL); 1467763Seric else 147*8245Seric 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); 1617812Seric e->e_qf = NULL; 1627391Seric 1637677Seric # ifdef LOG 1647677Seric /* save log info */ 1657878Seric if (LogLevel > 15) 1667878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 1677677Seric # endif LOG 1687677Seric 169*8245Seric /* disconnect this temp file from the job; don't requeue later */ 1707391Seric e->e_df = NULL; 171*8245Seric e->e_dontqueue = TRUE; 1724632Seric } 1734632Seric /* 1744632Seric ** RUNQUEUE -- run the jobs in the queue. 1754632Seric ** 1764632Seric ** Gets the stuff out of the queue in some presumably logical 1774632Seric ** order and processes them. 1784632Seric ** 1794632Seric ** Parameters: 1804632Seric ** none. 1814632Seric ** 1824632Seric ** Returns: 1834632Seric ** none. 1844632Seric ** 1854632Seric ** Side Effects: 1864632Seric ** runs things in the mail queue. 1874632Seric */ 1884632Seric 1894639Seric runqueue(forkflag) 1904639Seric bool forkflag; 1914632Seric { 1927943Seric register int i; 1934632Seric 1947466Seric /* 1957466Seric ** See if we want to go off and do other useful work. 1967466Seric */ 1974639Seric 1984639Seric if (forkflag) 1994639Seric { 2007943Seric int pid; 2017943Seric 2027943Seric pid = dofork(); 2037943Seric if (pid != 0) 2044639Seric { 2057943Seric /* parent -- pick up intermediate zombie */ 2067943Seric do 2077943Seric { 2087943Seric auto int stat; 2097943Seric 2107943Seric i = wait(&stat); 2117943Seric } while (i >= 0 && i != pid); 2127690Seric if (QueueIntvl != 0) 2137943Seric setevent(QueueIntvl, runqueue, TRUE); 2144639Seric return; 2154639Seric } 2167943Seric /* child -- double fork */ 2177943Seric if (fork() != 0) 2187943Seric exit(EX_OK); 2194639Seric } 2207876Seric # ifdef LOG 2217876Seric if (LogLevel > 11) 2227943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 2237876Seric # endif LOG 2244639Seric 2257466Seric /* 2267466Seric ** Start making passes through the queue. 2277466Seric ** First, read and sort the entire queue. 2287466Seric ** Then, process the work in that order. 2297466Seric ** But if you take too long, start over. 2307466Seric */ 2317466Seric 2327943Seric /* order the existing work requests */ 2337943Seric orderq(); 2347690Seric 2357943Seric /* process them once at a time */ 2367943Seric while (WorkQ != NULL) 2374639Seric { 2387943Seric WORK *w = WorkQ; 2397881Seric 2407943Seric WorkQ = WorkQ->w_next; 2417943Seric dowork(w); 2427943Seric free(w->w_name); 2437943Seric free((char *) w); 2444639Seric } 2457943Seric finis(); 2464634Seric } 2474634Seric /* 2484632Seric ** ORDERQ -- order the work queue. 2494632Seric ** 2504632Seric ** Parameters: 2514632Seric ** none. 2524632Seric ** 2534632Seric ** Returns: 2544632Seric ** none. 2554632Seric ** 2564632Seric ** Side Effects: 2574632Seric ** Sets WorkQ to the queue of available work, in order. 2584632Seric */ 2594632Seric 2604632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2614632Seric 2624632Seric orderq() 2634632Seric { 2646625Sglickman register struct direct *d; 2654632Seric register WORK *w; 2664632Seric register WORK **wp; /* parent of w */ 2676625Sglickman DIR *f; 2684632Seric register int i; 2694632Seric WORK wlist[WLSIZE]; 2704632Seric int wn = 0; 2714632Seric extern workcmpf(); 2724632Seric 2734632Seric /* clear out old WorkQ */ 2744632Seric for (w = WorkQ; w != NULL; ) 2754632Seric { 2764632Seric register WORK *nw = w->w_next; 2774632Seric 2784632Seric WorkQ = nw; 2794632Seric free(w->w_name); 2804632Seric free((char *) w); 2814632Seric w = nw; 2824632Seric } 2834632Seric 2844632Seric /* open the queue directory */ 2858148Seric f = opendir("."); 2864632Seric if (f == NULL) 2874632Seric { 2888148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2894632Seric return; 2904632Seric } 2914632Seric 2924632Seric /* 2934632Seric ** Read the work directory. 2944632Seric */ 2954632Seric 2966625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 2974632Seric { 2984632Seric char lbuf[MAXNAME]; 2994632Seric FILE *cf; 3004632Seric 3014632Seric /* is this an interesting entry? */ 3027812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 3034632Seric continue; 3044632Seric 3058148Seric /* yes -- open control file */ 3068148Seric cf = fopen(d->d_name, "r"); 3074632Seric if (cf == NULL) 3084632Seric { 3097055Seric /* this may be some random person sending hir msgs */ 3107055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3117055Seric errno = 0; 3124632Seric continue; 3134632Seric } 3148148Seric wlist[wn].w_name = newstr(d->d_name); 3154632Seric 3164632Seric /* extract useful information */ 3174632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3184632Seric { 3194632Seric fixcrlf(lbuf, TRUE); 3204632Seric 3214632Seric switch (lbuf[0]) 3224632Seric { 3234632Seric case 'P': /* message priority */ 3245037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3254632Seric break; 3264632Seric } 3274632Seric } 3284632Seric wn++; 3294632Seric (void) fclose(cf); 3304632Seric } 3316625Sglickman (void) closedir(f); 3324632Seric 3334632Seric /* 3344632Seric ** Sort the work directory. 3354632Seric */ 3364632Seric 3374632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3384632Seric 3394632Seric /* 3404632Seric ** Convert the work list into canonical form. 3414632Seric */ 3424632Seric 3434632Seric wp = &WorkQ; 3444632Seric for (i = 0; i < wn; i++) 3454632Seric { 3464632Seric w = (WORK *) xalloc(sizeof *w); 3474632Seric w->w_name = wlist[i].w_name; 3484632Seric w->w_pri = wlist[i].w_pri; 3494632Seric w->w_next = NULL; 3504632Seric *wp = w; 3514632Seric wp = &w->w_next; 3524632Seric } 3534632Seric 3544632Seric # ifdef DEBUG 3557677Seric if (tTd(40, 1)) 3564632Seric { 3574632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3585037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3594632Seric } 3604632Seric # endif DEBUG 3614632Seric } 3624632Seric /* 3637677Seric ** WORKCMPF -- compare function for ordering work. 3644632Seric ** 3654632Seric ** Parameters: 3664632Seric ** a -- the first argument. 3674632Seric ** b -- the second argument. 3684632Seric ** 3694632Seric ** Returns: 3704632Seric ** -1 if a < b 3714632Seric ** 0 if a == b 3724632Seric ** 1 if a > b 3734632Seric ** 3744632Seric ** Side Effects: 3754632Seric ** none. 3764632Seric */ 3774632Seric 3784632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3794632Seric 3804632Seric workcmpf(a, b) 3815037Seric register WORK *a; 3825037Seric register WORK *b; 3834632Seric { 3845037Seric if (a->w_pri == b->w_pri) 3854632Seric return (0); 3865037Seric else if (a->w_pri > b->w_pri) 3874632Seric return (1); 3884632Seric else 3894632Seric return (-1); 3904632Seric } 3914632Seric /* 3924632Seric ** DOWORK -- do a work request. 3934632Seric ** 3944632Seric ** Parameters: 3954632Seric ** w -- the work request to be satisfied. 3964632Seric ** 3974632Seric ** Returns: 3984632Seric ** none. 3994632Seric ** 4004632Seric ** Side Effects: 4014632Seric ** The work request is satisfied if possible. 4024632Seric */ 4034632Seric 4044632Seric dowork(w) 4054632Seric register WORK *w; 4064632Seric { 4074632Seric register int i; 4084632Seric auto int xstat; 4094632Seric 4104632Seric # ifdef DEBUG 4117677Seric if (tTd(40, 1)) 4125037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4134632Seric # endif DEBUG 4144632Seric 4154632Seric /* 4164632Seric ** Fork for work. 4174632Seric */ 4184632Seric 4194632Seric i = fork(); 4204632Seric if (i < 0) 4214632Seric { 4224632Seric syserr("dowork: cannot fork"); 4234632Seric return; 4244632Seric } 4254632Seric 4264632Seric if (i == 0) 4274632Seric { 4286980Seric char buf[MAXNAME]; 4296980Seric 4304632Seric /* 4314632Seric ** CHILD 4328148Seric ** Lock the control file to avoid duplicate deliveries. 4338148Seric ** Then run the file as though we had just read it. 4347350Seric ** We save an idea of the temporary name so we 4357350Seric ** can recover on interrupt. 4364632Seric */ 4374632Seric 4387763Seric /* set basic modes, etc. */ 4397356Seric (void) alarm(0); 4407347Seric FatalErrors = FALSE; 4414632Seric QueueRun = TRUE; 4426991Seric MailBack = TRUE; 4437812Seric CurEnv->e_qf = w->w_name; 4448148Seric CurEnv->e_id = &w->w_name[2]; 4457876Seric # ifdef LOG 4467876Seric if (LogLevel > 11) 4477881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4487881Seric getpid()); 4497876Seric # endif LOG 4507763Seric 4517763Seric /* don't use the headers from sendmail.cf... */ 4527763Seric CurEnv->e_header = NULL; 4537763Seric chompheader("from: $q", TRUE); 4547763Seric 4557763Seric /* create the link to the control file during processing */ 4567812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4576980Seric { 4587812Seric /* being processed by another queuer */ 4597881Seric # ifdef LOG 4607881Seric if (LogLevel > 4) 4617881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4627881Seric # endif LOG 4636980Seric exit(EX_OK); 4646980Seric } 4656980Seric 4666980Seric /* create ourselves a transcript file */ 4674634Seric openxscrpt(); 4686980Seric 4696980Seric /* do basic system initialization */ 4704632Seric initsys(); 4716980Seric 4726980Seric /* read the queue control file */ 4737812Seric readqf(CurEnv->e_qf); 4747781Seric eatheader(); 4756980Seric 4766980Seric /* do the delivery */ 4777558Seric if (!FatalErrors) 4787558Seric sendall(CurEnv, FALSE); 4796980Seric 4806980Seric /* if still not sent, perhaps we should time out.... */ 4814634Seric # ifdef DEBUG 4827677Seric if (tTd(40, 3)) 4837886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4847860Seric CurEnv->e_ctime + TimeOut); 4854634Seric # endif DEBUG 4867886Seric if (CurEnv->e_queueup && curtime() > CurEnv->e_ctime + TimeOut) 4874634Seric timeout(w); 4886980Seric 4896980Seric /* finish up and exit */ 4904632Seric finis(); 4914632Seric } 4924632Seric 4934632Seric /* 4944632Seric ** Parent -- pick up results. 4954632Seric */ 4964632Seric 4974632Seric errno = 0; 4984632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 4994632Seric { 5004632Seric if (errno == EINTR) 5014632Seric { 5024632Seric errno = 0; 5034632Seric } 5044632Seric } 5054632Seric } 5064632Seric /* 5074632Seric ** READQF -- read queue file and set up environment. 5084632Seric ** 5094632Seric ** Parameters: 5104632Seric ** cf -- name of queue control file. 5114632Seric ** 5124632Seric ** Returns: 5134632Seric ** none. 5144632Seric ** 5154632Seric ** Side Effects: 5164632Seric ** cf is read and created as the current job, as though 5174632Seric ** we had been invoked by argument. 5184632Seric */ 5194632Seric 5204632Seric readqf(cf) 5214632Seric char *cf; 5224632Seric { 5234632Seric register FILE *f; 5247785Seric char buf[MAXFIELD]; 5257753Seric register char *p; 5267753Seric register int i; 5274632Seric 5284632Seric /* 5294632Seric ** Open the file created by queueup. 5304632Seric */ 5314632Seric 5324632Seric f = fopen(cf, "r"); 5334632Seric if (f == NULL) 5344632Seric { 5354632Seric syserr("readqf: no cf file %s", cf); 5364632Seric return; 5374632Seric } 5384632Seric 5394632Seric /* 5404632Seric ** Read and process the file. 5414632Seric */ 5424632Seric 5437356Seric if (Verbose) 5447356Seric printf("\nRunning %s\n", cf); 5457785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5464632Seric { 5474632Seric switch (buf[0]) 5484632Seric { 5494632Seric case 'R': /* specify recipient */ 5508080Seric sendto(&buf[1], (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5514632Seric break; 5524632Seric 5534632Seric case 'H': /* header */ 5544632Seric (void) chompheader(&buf[1], FALSE); 5554632Seric break; 5564632Seric 5574632Seric case 'S': /* sender */ 5584634Seric setsender(newstr(&buf[1])); 5594632Seric break; 5604632Seric 5614632Seric case 'D': /* data file name */ 5626991Seric CurEnv->e_df = newstr(&buf[1]); 5636991Seric TempFile = fopen(CurEnv->e_df, "r"); 5644632Seric if (TempFile == NULL) 5656991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5664632Seric break; 5674632Seric 5687860Seric case 'T': /* init time */ 5697860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5704632Seric break; 5714632Seric 5724634Seric case 'P': /* message priority */ 5736908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5745037Seric 5755037Seric /* make sure that big things get sent eventually */ 5766908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5774634Seric break; 5784634Seric 5796980Seric case 'C': /* message class */ 5806980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5816980Seric break; 5826980Seric 5835902Seric case 'M': /* define macro */ 5845902Seric define(buf[1], newstr(&buf[2])); 5855902Seric break; 5865902Seric 5874632Seric default: 5884632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5894632Seric break; 5904632Seric } 5914632Seric } 5924632Seric } 5934632Seric /* 5944632Seric ** TIMEOUT -- process timeout on queue file. 5954632Seric ** 5964632Seric ** Parameters: 5974632Seric ** w -- pointer to work request that timed out. 5984632Seric ** 5994632Seric ** Returns: 6004632Seric ** none. 6014632Seric ** 6024632Seric ** Side Effects: 6034632Seric ** Returns a message to the sender saying that this 6044632Seric ** message has timed out. 6054632Seric */ 6064632Seric 6074632Seric timeout(w) 6084632Seric register WORK *w; 6094632Seric { 6107369Seric char buf[MAXLINE]; 6117860Seric extern char *pintvl(); 6127369Seric 6134634Seric # ifdef DEBUG 6147677Seric if (tTd(40, 3)) 6154634Seric printf("timeout(%s)\n", w->w_name); 6164634Seric # endif DEBUG 6176991Seric message(Arpa_Info, "Message has timed out"); 6184634Seric 6194634Seric /* return message to sender */ 6207860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6217369Seric (void) returntosender(buf, &CurEnv->e_from, TRUE); 6224634Seric 6234634Seric /* arrange to remove files from queue */ 6247860Seric CurEnv->e_dontqueue = TRUE; 6254632Seric } 6265182Seric 6275182Seric # endif QUEUE 628