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*9038Seric SCCSID(@(#)queue.c 3.47 11/03/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*9038Seric SCCSID(@(#)queue.c 3.47 11/03/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)) 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 257*9038Seric # ifndef DIR 258*9038Seric # define DIR FILE 259*9038Seric # define direct dir 260*9038Seric # define opendir(d) fopen(d, "r") 261*9038Seric # define readdir(f) (fread(dbuf, sizeof dbuf, 1, f) > 0 ? &dbuf : 0) 262*9038Seric static struct dir dbuf; 263*9038Seric # define closedir(f) fclose(f) 264*9038Seric # endif DIR 265*9038Seric 2664632Seric orderq() 2674632Seric { 2686625Sglickman register struct direct *d; 2694632Seric register WORK *w; 2704632Seric register WORK **wp; /* parent of w */ 2716625Sglickman DIR *f; 2724632Seric register int i; 2734632Seric WORK wlist[WLSIZE]; 2744632Seric int wn = 0; 2754632Seric extern workcmpf(); 2764632Seric 2774632Seric /* clear out old WorkQ */ 2784632Seric for (w = WorkQ; w != NULL; ) 2794632Seric { 2804632Seric register WORK *nw = w->w_next; 2814632Seric 2824632Seric WorkQ = nw; 2834632Seric free(w->w_name); 2844632Seric free((char *) w); 2854632Seric w = nw; 2864632Seric } 2874632Seric 2884632Seric /* open the queue directory */ 2898148Seric f = opendir("."); 2904632Seric if (f == NULL) 2914632Seric { 2928148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2934632Seric return; 2944632Seric } 2954632Seric 2964632Seric /* 2974632Seric ** Read the work directory. 2984632Seric */ 2994632Seric 3006625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 3014632Seric { 3024632Seric char lbuf[MAXNAME]; 3034632Seric FILE *cf; 3044632Seric 3054632Seric /* is this an interesting entry? */ 3067812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 3074632Seric continue; 3084632Seric 3098148Seric /* yes -- open control file */ 3108148Seric cf = fopen(d->d_name, "r"); 3114632Seric if (cf == NULL) 3124632Seric { 3137055Seric /* this may be some random person sending hir msgs */ 3147055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3157055Seric errno = 0; 3164632Seric continue; 3174632Seric } 3188148Seric wlist[wn].w_name = newstr(d->d_name); 3194632Seric 3204632Seric /* extract useful information */ 3214632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3224632Seric { 3234632Seric fixcrlf(lbuf, TRUE); 3244632Seric 3254632Seric switch (lbuf[0]) 3264632Seric { 3274632Seric case 'P': /* message priority */ 3285037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3294632Seric break; 3304632Seric } 3314632Seric } 3324632Seric wn++; 3334632Seric (void) fclose(cf); 3344632Seric } 3356625Sglickman (void) closedir(f); 3364632Seric 3374632Seric /* 3384632Seric ** Sort the work directory. 3394632Seric */ 3404632Seric 3414632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3424632Seric 3434632Seric /* 3444632Seric ** Convert the work list into canonical form. 3454632Seric */ 3464632Seric 3474632Seric wp = &WorkQ; 3484632Seric for (i = 0; i < wn; i++) 3494632Seric { 3504632Seric w = (WORK *) xalloc(sizeof *w); 3514632Seric w->w_name = wlist[i].w_name; 3524632Seric w->w_pri = wlist[i].w_pri; 3534632Seric w->w_next = NULL; 3544632Seric *wp = w; 3554632Seric wp = &w->w_next; 3564632Seric } 3574632Seric 3584632Seric # ifdef DEBUG 3597677Seric if (tTd(40, 1)) 3604632Seric { 3614632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3625037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3634632Seric } 3644632Seric # endif DEBUG 3654632Seric } 3664632Seric /* 3677677Seric ** WORKCMPF -- compare function for ordering work. 3684632Seric ** 3694632Seric ** Parameters: 3704632Seric ** a -- the first argument. 3714632Seric ** b -- the second argument. 3724632Seric ** 3734632Seric ** Returns: 3744632Seric ** -1 if a < b 3754632Seric ** 0 if a == b 3764632Seric ** 1 if a > b 3774632Seric ** 3784632Seric ** Side Effects: 3794632Seric ** none. 3804632Seric */ 3814632Seric 3824632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3834632Seric 3844632Seric workcmpf(a, b) 3855037Seric register WORK *a; 3865037Seric register WORK *b; 3874632Seric { 3885037Seric if (a->w_pri == b->w_pri) 3894632Seric return (0); 3905037Seric else if (a->w_pri > b->w_pri) 3914632Seric return (1); 3924632Seric else 3934632Seric return (-1); 3944632Seric } 3954632Seric /* 3964632Seric ** DOWORK -- do a work request. 3974632Seric ** 3984632Seric ** Parameters: 3994632Seric ** w -- the work request to be satisfied. 4004632Seric ** 4014632Seric ** Returns: 4024632Seric ** none. 4034632Seric ** 4044632Seric ** Side Effects: 4054632Seric ** The work request is satisfied if possible. 4064632Seric */ 4074632Seric 4084632Seric dowork(w) 4094632Seric register WORK *w; 4104632Seric { 4114632Seric register int i; 4124632Seric auto int xstat; 4134632Seric 4144632Seric # ifdef DEBUG 4157677Seric if (tTd(40, 1)) 4165037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4174632Seric # endif DEBUG 4184632Seric 4194632Seric /* 4204632Seric ** Fork for work. 4214632Seric */ 4224632Seric 4234632Seric i = fork(); 4244632Seric if (i < 0) 4254632Seric { 4264632Seric syserr("dowork: cannot fork"); 4274632Seric return; 4284632Seric } 4294632Seric 4304632Seric if (i == 0) 4314632Seric { 4326980Seric char buf[MAXNAME]; 4336980Seric 4344632Seric /* 4354632Seric ** CHILD 4368148Seric ** Lock the control file to avoid duplicate deliveries. 4378148Seric ** Then run the file as though we had just read it. 4387350Seric ** We save an idea of the temporary name so we 4397350Seric ** can recover on interrupt. 4404632Seric */ 4414632Seric 4427763Seric /* set basic modes, etc. */ 4437356Seric (void) alarm(0); 4447347Seric FatalErrors = FALSE; 4454632Seric QueueRun = TRUE; 4466991Seric MailBack = TRUE; 4477812Seric CurEnv->e_qf = w->w_name; 4488148Seric CurEnv->e_id = &w->w_name[2]; 4497876Seric # ifdef LOG 4507876Seric if (LogLevel > 11) 4517881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4527881Seric getpid()); 4537876Seric # endif LOG 4547763Seric 4557763Seric /* don't use the headers from sendmail.cf... */ 4567763Seric CurEnv->e_header = NULL; 4577763Seric chompheader("from: $q", TRUE); 4587763Seric 4597763Seric /* create the link to the control file during processing */ 4607812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4616980Seric { 4627812Seric /* being processed by another queuer */ 4637881Seric # ifdef LOG 4647881Seric if (LogLevel > 4) 4657881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4667881Seric # endif LOG 4676980Seric exit(EX_OK); 4686980Seric } 4696980Seric 4706980Seric /* create ourselves a transcript file */ 4714634Seric openxscrpt(); 4726980Seric 4736980Seric /* do basic system initialization */ 4744632Seric initsys(); 4756980Seric 4766980Seric /* read the queue control file */ 4777812Seric readqf(CurEnv->e_qf); 4787781Seric eatheader(); 4796980Seric 4806980Seric /* do the delivery */ 4817558Seric if (!FatalErrors) 4827558Seric sendall(CurEnv, FALSE); 4836980Seric 4846980Seric /* if still not sent, perhaps we should time out.... */ 4854634Seric # ifdef DEBUG 4867677Seric if (tTd(40, 3)) 4877886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4887860Seric CurEnv->e_ctime + TimeOut); 4894634Seric # endif DEBUG 4907886Seric if (CurEnv->e_queueup && curtime() > CurEnv->e_ctime + TimeOut) 4914634Seric timeout(w); 4926980Seric 4936980Seric /* finish up and exit */ 4944632Seric finis(); 4954632Seric } 4964632Seric 4974632Seric /* 4984632Seric ** Parent -- pick up results. 4994632Seric */ 5004632Seric 5014632Seric errno = 0; 5024632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 5034632Seric { 5044632Seric if (errno == EINTR) 5054632Seric { 5064632Seric errno = 0; 5074632Seric } 5084632Seric } 5094632Seric } 5104632Seric /* 5114632Seric ** READQF -- read queue file and set up environment. 5124632Seric ** 5134632Seric ** Parameters: 5144632Seric ** cf -- name of queue control file. 5154632Seric ** 5164632Seric ** Returns: 5174632Seric ** none. 5184632Seric ** 5194632Seric ** Side Effects: 5204632Seric ** cf is read and created as the current job, as though 5214632Seric ** we had been invoked by argument. 5224632Seric */ 5234632Seric 5244632Seric readqf(cf) 5254632Seric char *cf; 5264632Seric { 5274632Seric register FILE *f; 5287785Seric char buf[MAXFIELD]; 5297753Seric register char *p; 5307753Seric register int i; 5314632Seric 5324632Seric /* 5334632Seric ** Open the file created by queueup. 5344632Seric */ 5354632Seric 5364632Seric f = fopen(cf, "r"); 5374632Seric if (f == NULL) 5384632Seric { 5394632Seric syserr("readqf: no cf file %s", cf); 5404632Seric return; 5414632Seric } 5424632Seric 5434632Seric /* 5444632Seric ** Read and process the file. 5454632Seric */ 5464632Seric 5477356Seric if (Verbose) 5487356Seric printf("\nRunning %s\n", cf); 5497785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5504632Seric { 5514632Seric switch (buf[0]) 5524632Seric { 5534632Seric case 'R': /* specify recipient */ 5548080Seric sendto(&buf[1], (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5554632Seric break; 5564632Seric 5574632Seric case 'H': /* header */ 5584632Seric (void) chompheader(&buf[1], FALSE); 5594632Seric break; 5604632Seric 5614632Seric case 'S': /* sender */ 5624634Seric setsender(newstr(&buf[1])); 5634632Seric break; 5644632Seric 5654632Seric case 'D': /* data file name */ 5666991Seric CurEnv->e_df = newstr(&buf[1]); 5676991Seric TempFile = fopen(CurEnv->e_df, "r"); 5684632Seric if (TempFile == NULL) 5696991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5704632Seric break; 5714632Seric 5727860Seric case 'T': /* init time */ 5737860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5744632Seric break; 5754632Seric 5764634Seric case 'P': /* message priority */ 5776908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5785037Seric 5795037Seric /* make sure that big things get sent eventually */ 5806908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5814634Seric break; 5824634Seric 5836980Seric case 'C': /* message class */ 5846980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5856980Seric break; 5866980Seric 5875902Seric case 'M': /* define macro */ 5885902Seric define(buf[1], newstr(&buf[2])); 5895902Seric break; 5905902Seric 5914632Seric default: 5924632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5934632Seric break; 5944632Seric } 5954632Seric } 5964632Seric } 5974632Seric /* 5984632Seric ** TIMEOUT -- process timeout on queue file. 5994632Seric ** 6004632Seric ** Parameters: 6014632Seric ** w -- pointer to work request that timed out. 6024632Seric ** 6034632Seric ** Returns: 6044632Seric ** none. 6054632Seric ** 6064632Seric ** Side Effects: 6074632Seric ** Returns a message to the sender saying that this 6084632Seric ** message has timed out. 6094632Seric */ 6104632Seric 6114632Seric timeout(w) 6124632Seric register WORK *w; 6134632Seric { 6147369Seric char buf[MAXLINE]; 6157860Seric extern char *pintvl(); 6167369Seric 6174634Seric # ifdef DEBUG 6187677Seric if (tTd(40, 3)) 6194634Seric printf("timeout(%s)\n", w->w_name); 6204634Seric # endif DEBUG 6216991Seric message(Arpa_Info, "Message has timed out"); 6224634Seric 6234634Seric /* return message to sender */ 6247860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6257369Seric (void) returntosender(buf, &CurEnv->e_from, TRUE); 6264634Seric 6274634Seric /* arrange to remove files from queue */ 6287860Seric CurEnv->e_dontqueue = TRUE; 6294632Seric } 6305182Seric 6315182Seric # endif QUEUE 632