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*9544Seric SCCSID(@(#)queue.c 3.57 12/05/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*9544Seric SCCSID(@(#)queue.c 3.57 12/05/82); 125182Seric 134632Seric /* 149377Seric ** Work queue. 159377Seric */ 169377Seric 179377Seric struct work 189377Seric { 199377Seric char *w_name; /* name of control file */ 209377Seric long w_pri; /* priority of message, see below */ 219377Seric struct work *w_next; /* next in queue */ 229377Seric }; 239377Seric 249377Seric typedef struct work WORK; 259377Seric 269377Seric WORK *WorkQ; /* queue of things to be done */ 279377Seric /* 284632Seric ** QUEUEUP -- queue a message up for future transmission. 294632Seric ** 304632Seric ** Parameters: 316980Seric ** e -- the envelope to queue up. 326999Seric ** queueall -- if TRUE, queue all addresses, rather than 336999Seric ** just those with the QQUEUEUP flag set. 349377Seric ** announce -- if TRUE, tell when you are queueing up. 354632Seric ** 364632Seric ** Returns: 374632Seric ** none. 384632Seric ** 394632Seric ** Side Effects: 409377Seric ** The current request are saved in a control file. 414632Seric */ 424632Seric 439377Seric queueup(e, queueall, announce) 446980Seric register ENVELOPE *e; 456999Seric bool queueall; 469377Seric bool announce; 474632Seric { 487812Seric char *tf; 497812Seric char *qf; 507763Seric char buf[MAXLINE]; 517812Seric register FILE *tfp; 524632Seric register HDR *h; 535007Seric register ADDRESS *q; 544632Seric 555037Seric /* 565037Seric ** Create control file. 575037Seric */ 584632Seric 597812Seric tf = newstr(queuename(e, 't')); 607812Seric tfp = fopen(tf, "w"); 617812Seric if (tfp == NULL) 624632Seric { 637812Seric syserr("queueup: cannot create temp file %s", tf); 644632Seric return; 654632Seric } 669048Seric (void) chmod(tf, FileMode); 674632Seric 684632Seric # ifdef DEBUG 697677Seric if (tTd(40, 1)) 707812Seric printf("queueing in %s\n", tf); 714632Seric # endif DEBUG 724632Seric 734632Seric /* 746980Seric ** If there is no data file yet, create one. 756980Seric */ 766980Seric 776980Seric if (e->e_df == NULL) 786980Seric { 796980Seric register FILE *dfp; 809389Seric extern putbody(); 816980Seric 827812Seric e->e_df = newstr(queuename(e, 'd')); 836980Seric dfp = fopen(e->e_df, "w"); 846980Seric if (dfp == NULL) 856980Seric { 866980Seric syserr("queueup: cannot create %s", e->e_df); 877812Seric (void) fclose(tfp); 886980Seric return; 896980Seric } 909048Seric (void) chmod(e->e_df, FileMode); 91*9544Seric (*e->e_putbody)(dfp, ProgMailer, FALSE, e); 927009Seric (void) fclose(dfp); 939389Seric e->e_putbody = putbody; 946980Seric } 956980Seric 966980Seric /* 974632Seric ** Output future work requests. 989377Seric ** Priority should be first, since it is read by orderq. 994632Seric */ 1004632Seric 1019377Seric /* output message priority */ 1029377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1039377Seric 1044632Seric /* output name of data file */ 1057812Seric fprintf(tfp, "D%s\n", e->e_df); 1064632Seric 1074632Seric /* output name of sender */ 1087812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1094632Seric 1107860Seric /* output creation time */ 1117860Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1124632Seric 1134632Seric /* output list of recipient addresses */ 1146980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1154632Seric { 1167763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1177763Seric bitset(QQUEUEUP, q->q_flags)) 1188245Seric { 1197812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1209377Seric if (announce) 1219377Seric { 1229377Seric e->e_to = q->q_paddr; 1239377Seric message(Arpa_Info, "queued"); 1249377Seric if (LogLevel > 4) 1259377Seric logdelivery("queued"); 1269377Seric e->e_to = NULL; 1279377Seric } 1289387Seric #ifdef DEBUG 1299387Seric if (tTd(40, 1)) 1309387Seric { 1319387Seric printf("queueing "); 1329387Seric printaddr(q, FALSE); 1339387Seric } 1349387Seric #endif DEBUG 1358245Seric } 1364632Seric } 1374632Seric 1389377Seric /* 1399377Seric ** Output headers for this message. 1409377Seric ** Expand macros completely here. Queue run will deal with 1419377Seric ** everything as absolute headers. 1429377Seric ** All headers that must be relative to the recipient 1439377Seric ** can be cracked later. 1449377Seric */ 1459377Seric 1469377Seric define('g', "$f", e); 1476980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1484632Seric { 1494632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1504632Seric continue; 1517812Seric fprintf(tfp, "H"); 1524632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1537812Seric mfdecode(h->h_mflags, tfp); 1547763Seric if (bitset(H_DEFAULT, h->h_flags)) 1557763Seric { 1567763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 1578236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 1587763Seric } 1598245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 1609348Seric { 1619348Seric commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 1629348Seric (MAILER *) NULL); 1639348Seric } 1647763Seric else 1658245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 1664632Seric } 1674632Seric 1684632Seric /* 1694632Seric ** Clean up. 1704632Seric */ 1714632Seric 1727812Seric (void) fclose(tfp); 1737812Seric qf = queuename(e, 'q'); 1749377Seric holdsigs(); 1757812Seric (void) unlink(qf); 1767812Seric if (link(tf, qf) < 0) 1777812Seric syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 1786980Seric else 1797812Seric (void) unlink(tf); 1809377Seric rlsesigs(); 1817391Seric 1827677Seric # ifdef LOG 1837677Seric /* save log info */ 1847878Seric if (LogLevel > 15) 1857878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 1867677Seric # endif LOG 1874632Seric } 1884632Seric /* 1894632Seric ** RUNQUEUE -- run the jobs in the queue. 1904632Seric ** 1914632Seric ** Gets the stuff out of the queue in some presumably logical 1924632Seric ** order and processes them. 1934632Seric ** 1944632Seric ** Parameters: 1954632Seric ** none. 1964632Seric ** 1974632Seric ** Returns: 1984632Seric ** none. 1994632Seric ** 2004632Seric ** Side Effects: 2014632Seric ** runs things in the mail queue. 2024632Seric */ 2034632Seric 2044639Seric runqueue(forkflag) 2054639Seric bool forkflag; 2064632Seric { 2077466Seric /* 2087466Seric ** See if we want to go off and do other useful work. 2097466Seric */ 2104639Seric 2114639Seric if (forkflag) 2124639Seric { 2137943Seric int pid; 2147943Seric 2157943Seric pid = dofork(); 2167943Seric if (pid != 0) 2174639Seric { 2187943Seric /* parent -- pick up intermediate zombie */ 2199377Seric (void) waitfor(pid); 2207690Seric if (QueueIntvl != 0) 2219348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 2224639Seric return; 2234639Seric } 2247943Seric /* child -- double fork */ 2257943Seric if (fork() != 0) 2267943Seric exit(EX_OK); 2274639Seric } 2287876Seric # ifdef LOG 2297876Seric if (LogLevel > 11) 2307943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 2317876Seric # endif LOG 2324639Seric 2337466Seric /* 2347466Seric ** Start making passes through the queue. 2357466Seric ** First, read and sort the entire queue. 2367466Seric ** Then, process the work in that order. 2377466Seric ** But if you take too long, start over. 2387466Seric */ 2397466Seric 2407943Seric /* order the existing work requests */ 2417943Seric orderq(); 2427690Seric 2437943Seric /* process them once at a time */ 2447943Seric while (WorkQ != NULL) 2454639Seric { 2467943Seric WORK *w = WorkQ; 2477881Seric 2487943Seric WorkQ = WorkQ->w_next; 2497943Seric dowork(w); 2507943Seric free(w->w_name); 2517943Seric free((char *) w); 2524639Seric } 2537943Seric finis(); 2544634Seric } 2554634Seric /* 2564632Seric ** ORDERQ -- order the work queue. 2574632Seric ** 2584632Seric ** Parameters: 2594632Seric ** none. 2604632Seric ** 2614632Seric ** Returns: 2624632Seric ** none. 2634632Seric ** 2644632Seric ** Side Effects: 2654632Seric ** Sets WorkQ to the queue of available work, in order. 2664632Seric */ 2674632Seric 2684632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2694632Seric 2704632Seric orderq() 2714632Seric { 2726625Sglickman register struct direct *d; 2734632Seric register WORK *w; 2744632Seric register WORK **wp; /* parent of w */ 2756625Sglickman DIR *f; 2764632Seric register int i; 2774632Seric WORK wlist[WLSIZE]; 2784632Seric int wn = 0; 2794632Seric extern workcmpf(); 2804632Seric 2814632Seric /* clear out old WorkQ */ 2824632Seric for (w = WorkQ; w != NULL; ) 2834632Seric { 2844632Seric register WORK *nw = w->w_next; 2854632Seric 2864632Seric WorkQ = nw; 2874632Seric free(w->w_name); 2884632Seric free((char *) w); 2894632Seric w = nw; 2904632Seric } 2914632Seric 2924632Seric /* open the queue directory */ 2938148Seric f = opendir("."); 2944632Seric if (f == NULL) 2954632Seric { 2968148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2974632Seric return; 2984632Seric } 2994632Seric 3004632Seric /* 3014632Seric ** Read the work directory. 3024632Seric */ 3034632Seric 3046625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 3054632Seric { 3069377Seric FILE *cf; 3074632Seric char lbuf[MAXNAME]; 3084632Seric 3094632Seric /* is this an interesting entry? */ 3107812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 3114632Seric continue; 3124632Seric 3138148Seric /* yes -- open control file */ 3148148Seric cf = fopen(d->d_name, "r"); 3154632Seric if (cf == NULL) 3164632Seric { 3177055Seric /* this may be some random person sending hir msgs */ 3187055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3197055Seric errno = 0; 3204632Seric continue; 3214632Seric } 3228148Seric wlist[wn].w_name = newstr(d->d_name); 3234632Seric 3244632Seric /* extract useful information */ 3254632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3264632Seric { 3279377Seric if (lbuf[0] == 'P') 3284632Seric { 3295037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3304632Seric break; 3314632Seric } 3324632Seric } 3334632Seric wn++; 3344632Seric (void) fclose(cf); 3354632Seric } 3366625Sglickman (void) closedir(f); 3374632Seric 3384632Seric /* 3394632Seric ** Sort the work directory. 3404632Seric */ 3414632Seric 3424632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3434632Seric 3444632Seric /* 3454632Seric ** Convert the work list into canonical form. 3469377Seric ** Should be turning it into a list of envelopes here perhaps. 3474632Seric */ 3484632Seric 3494632Seric wp = &WorkQ; 3504632Seric for (i = 0; i < wn; i++) 3514632Seric { 3524632Seric w = (WORK *) xalloc(sizeof *w); 3534632Seric w->w_name = wlist[i].w_name; 3544632Seric w->w_pri = wlist[i].w_pri; 3554632Seric w->w_next = NULL; 3564632Seric *wp = w; 3574632Seric wp = &w->w_next; 3584632Seric } 3594632Seric 3604632Seric # ifdef DEBUG 3617677Seric if (tTd(40, 1)) 3624632Seric { 3634632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3645037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3654632Seric } 3664632Seric # endif DEBUG 3674632Seric } 3684632Seric /* 3697677Seric ** WORKCMPF -- compare function for ordering work. 3704632Seric ** 3714632Seric ** Parameters: 3724632Seric ** a -- the first argument. 3734632Seric ** b -- the second argument. 3744632Seric ** 3754632Seric ** Returns: 3764632Seric ** -1 if a < b 3774632Seric ** 0 if a == b 3784632Seric ** 1 if a > b 3794632Seric ** 3804632Seric ** Side Effects: 3814632Seric ** none. 3824632Seric */ 3834632Seric 3844632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3854632Seric 3864632Seric workcmpf(a, b) 3875037Seric register WORK *a; 3885037Seric register WORK *b; 3894632Seric { 3905037Seric if (a->w_pri == b->w_pri) 3914632Seric return (0); 3925037Seric else if (a->w_pri > b->w_pri) 3934632Seric return (1); 3944632Seric else 3954632Seric return (-1); 3964632Seric } 3974632Seric /* 3984632Seric ** DOWORK -- do a work request. 3994632Seric ** 4004632Seric ** Parameters: 4014632Seric ** w -- the work request to be satisfied. 4024632Seric ** 4034632Seric ** Returns: 4044632Seric ** none. 4054632Seric ** 4064632Seric ** Side Effects: 4074632Seric ** The work request is satisfied if possible. 4084632Seric */ 4094632Seric 4104632Seric dowork(w) 4114632Seric register WORK *w; 4124632Seric { 4134632Seric register int i; 4144632Seric 4154632Seric # ifdef DEBUG 4167677Seric if (tTd(40, 1)) 4175037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4184632Seric # endif DEBUG 4194632Seric 4204632Seric /* 4214632Seric ** Fork for work. 4224632Seric */ 4234632Seric 4244632Seric i = fork(); 4254632Seric if (i < 0) 4264632Seric { 4274632Seric syserr("dowork: cannot fork"); 4284632Seric return; 4294632Seric } 4304632Seric 4314632Seric if (i == 0) 4324632Seric { 4334632Seric /* 4344632Seric ** CHILD 4358148Seric ** Lock the control file to avoid duplicate deliveries. 4368148Seric ** Then run the file as though we had just read it. 4377350Seric ** We save an idea of the temporary name so we 4387350Seric ** can recover on interrupt. 4394632Seric */ 4404632Seric 4417763Seric /* set basic modes, etc. */ 4427356Seric (void) alarm(0); 4439338Seric CurEnv->e_flags &= ~EF_FATALERRS; 4444632Seric QueueRun = TRUE; 4459387Seric SendMode = SM_DELIVER; 4469377Seric ErrorMode = EM_MAIL; 4478148Seric CurEnv->e_id = &w->w_name[2]; 4487876Seric # ifdef LOG 4497876Seric if (LogLevel > 11) 4507881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4517881Seric getpid()); 4527876Seric # endif LOG 4537763Seric 4547763Seric /* don't use the headers from sendmail.cf... */ 4557763Seric CurEnv->e_header = NULL; 4569348Seric (void) chompheader("from: $q", TRUE); 4577763Seric 4587763Seric /* create the link to the control file during processing */ 4597812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4606980Seric { 4617812Seric /* being processed by another queuer */ 4627881Seric # ifdef LOG 4637881Seric if (LogLevel > 4) 4647881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4657881Seric # endif LOG 4666980Seric exit(EX_OK); 4676980Seric } 4686980Seric 4696980Seric /* do basic system initialization */ 4704632Seric initsys(); 4716980Seric 4726980Seric /* read the queue control file */ 4739377Seric readqf(CurEnv); 4749338Seric CurEnv->e_flags |= EF_INQUEUE; 4759377Seric eatheader(CurEnv); 4766980Seric 4776980Seric /* do the delivery */ 4789338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 4799282Seric sendall(CurEnv, SM_DELIVER); 4806980Seric 4816980Seric /* if still not sent, perhaps we should time out.... */ 4824634Seric # ifdef DEBUG 4837677Seric if (tTd(40, 3)) 4847886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4857860Seric CurEnv->e_ctime + TimeOut); 4864634Seric # endif DEBUG 4879338Seric if (curtime() > CurEnv->e_ctime + TimeOut) 4889338Seric CurEnv->e_flags |= EF_TIMEOUT; 4896980Seric 4906980Seric /* finish up and exit */ 4914632Seric finis(); 4924632Seric } 4934632Seric 4944632Seric /* 4954632Seric ** Parent -- pick up results. 4964632Seric */ 4974632Seric 4984632Seric errno = 0; 4999377Seric (void) waitfor(i); 5004632Seric } 5014632Seric /* 5024632Seric ** READQF -- read queue file and set up environment. 5034632Seric ** 5044632Seric ** Parameters: 5059377Seric ** e -- the envelope of the job to run. 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 5159377Seric readqf(e) 5169377Seric register ENVELOPE *e; 5174632Seric { 5184632Seric register FILE *f; 5197785Seric char buf[MAXFIELD]; 5209348Seric extern char *fgetfolded(); 5219377Seric register char *p; 5224632Seric 5234632Seric /* 5244632Seric ** Open the file created by queueup. 5254632Seric */ 5264632Seric 5279377Seric p = queuename(e, 'q'); 5289377Seric f = fopen(p, "r"); 5294632Seric if (f == NULL) 5304632Seric { 5319377Seric syserr("readqf: no control file %s", p); 5324632Seric return; 5334632Seric } 5349377Seric FileName = p; 5359377Seric LineNumber = 0; 5364632Seric 5374632Seric /* 5384632Seric ** Read and process the file. 5394632Seric */ 5404632Seric 5417356Seric if (Verbose) 5429377Seric printf("\nRunning %s\n", e->e_id); 5437785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5444632Seric { 5454632Seric switch (buf[0]) 5464632Seric { 5474632Seric case 'R': /* specify recipient */ 5489377Seric sendto(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 5494632Seric break; 5504632Seric 5514632Seric case 'H': /* header */ 5524632Seric (void) chompheader(&buf[1], FALSE); 5534632Seric break; 5544632Seric 5554632Seric case 'S': /* sender */ 5564634Seric setsender(newstr(&buf[1])); 5574632Seric break; 5584632Seric 5594632Seric case 'D': /* data file name */ 5609377Seric e->e_df = newstr(&buf[1]); 561*9544Seric e->e_dfp = fopen(e->e_df, "r"); 562*9544Seric if (e->e_dfp == NULL) 5639377Seric syserr("readqf: cannot open %s", e->e_df); 5644632Seric break; 5654632Seric 5667860Seric case 'T': /* init time */ 5679377Seric (void) sscanf(&buf[1], "%ld", &e->e_ctime); 5684632Seric break; 5694632Seric 5704634Seric case 'P': /* message priority */ 5719377Seric (void) sscanf(&buf[1], "%ld", &e->e_msgpriority); 5725037Seric 5735037Seric /* make sure that big things get sent eventually */ 5749377Seric e->e_msgpriority -= WKTIMEFACT; 5754634Seric break; 5764634Seric 5775902Seric case 'M': /* define macro */ 5789377Seric define(buf[1], newstr(&buf[2]), e); 5795902Seric break; 5805902Seric 5814632Seric default: 5829377Seric syserr("readqf(%s): bad line \"%s\"", e->e_id, buf); 5834632Seric break; 5844632Seric } 5854632Seric } 5869377Seric 5879377Seric FileName = NULL; 5884632Seric } 5894632Seric /* 5904632Seric ** TIMEOUT -- process timeout on queue file. 5914632Seric ** 5924632Seric ** Parameters: 5939338Seric ** e -- the envelope that timed out. 5944632Seric ** 5954632Seric ** Returns: 5964632Seric ** none. 5974632Seric ** 5984632Seric ** Side Effects: 5994632Seric ** Returns a message to the sender saying that this 6004632Seric ** message has timed out. 6014632Seric */ 6024632Seric 6039338Seric timeout(e) 6049338Seric register ENVELOPE *e; 6054632Seric { 6067369Seric char buf[MAXLINE]; 6077860Seric extern char *pintvl(); 6087369Seric 6094634Seric # ifdef DEBUG 6107677Seric if (tTd(40, 3)) 6119338Seric printf("timeout(%s)\n", e->e_id); 6124634Seric # endif DEBUG 6139338Seric e->e_to = NULL; 6146991Seric message(Arpa_Info, "Message has timed out"); 6154634Seric 6164634Seric /* return message to sender */ 6177860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6189338Seric (void) returntosender(buf, &e->e_from, TRUE); 6194634Seric 6204634Seric /* arrange to remove files from queue */ 6219338Seric e->e_flags |= EF_CLRQUEUE; 6224632Seric } 6235182Seric 6245182Seric # endif QUEUE 625