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*9338Seric SCCSID(@(#)queue.c 3.52 11/24/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*9338Seric SCCSID(@(#)queue.c 3.52 11/24/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 1024632Seric /* output list of recipient addresses */ 1036980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1044632Seric { 1055037Seric # ifdef DEBUG 1067677Seric if (tTd(40, 1)) 1075037Seric { 1085037Seric printf("queueing "); 1095037Seric printaddr(q, FALSE); 1105037Seric } 1115037Seric # endif DEBUG 1127763Seric if (queueall ? !bitset(QDONTSEND, q->q_flags) : 1137763Seric bitset(QQUEUEUP, q->q_flags)) 1148245Seric { 1157812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1168245Seric } 1174632Seric } 1184632Seric 1194632Seric /* output headers for this message */ 1207763Seric define('g', "$f"); 1216980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1224632Seric { 1234632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1244632Seric continue; 1257812Seric fprintf(tfp, "H"); 1264632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1277812Seric mfdecode(h->h_mflags, tfp); 1287763Seric if (bitset(H_DEFAULT, h->h_flags)) 1297763Seric { 1307763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 1318236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 1327763Seric } 1338245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 134*9338Seric commaize(h, h->h_value, tfp, 135*9338Seric bitset(EF_OLDSTYLE, e->e_flags), NULL); 1367763Seric else 1378245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 1384632Seric } 1394632Seric 1404632Seric /* 1414632Seric ** Clean up. 1424632Seric */ 1434632Seric 1447812Seric (void) fclose(tfp); 1457812Seric qf = queuename(e, 'q'); 1467812Seric (void) unlink(qf); 1477812Seric if (link(tf, qf) < 0) 1487812Seric syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 1496980Seric else 1507812Seric (void) unlink(tf); 1517391Seric 1527677Seric # ifdef LOG 1537677Seric /* save log info */ 1547878Seric if (LogLevel > 15) 1557878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 1567677Seric # endif LOG 1574632Seric } 1584632Seric /* 1594632Seric ** RUNQUEUE -- run the jobs in the queue. 1604632Seric ** 1614632Seric ** Gets the stuff out of the queue in some presumably logical 1624632Seric ** order and processes them. 1634632Seric ** 1644632Seric ** Parameters: 1654632Seric ** none. 1664632Seric ** 1674632Seric ** Returns: 1684632Seric ** none. 1694632Seric ** 1704632Seric ** Side Effects: 1714632Seric ** runs things in the mail queue. 1724632Seric */ 1734632Seric 1744639Seric runqueue(forkflag) 1754639Seric bool forkflag; 1764632Seric { 1777943Seric register int i; 1784632Seric 1797466Seric /* 1807466Seric ** See if we want to go off and do other useful work. 1817466Seric */ 1824639Seric 1834639Seric if (forkflag) 1844639Seric { 1857943Seric int pid; 1867943Seric 1877943Seric pid = dofork(); 1887943Seric if (pid != 0) 1894639Seric { 1907943Seric /* parent -- pick up intermediate zombie */ 1917943Seric do 1927943Seric { 1937943Seric auto int stat; 1947943Seric 1957943Seric i = wait(&stat); 1967943Seric } while (i >= 0 && i != pid); 1977690Seric if (QueueIntvl != 0) 1987943Seric setevent(QueueIntvl, runqueue, TRUE); 1994639Seric return; 2004639Seric } 2017943Seric /* child -- double fork */ 2027943Seric if (fork() != 0) 2037943Seric exit(EX_OK); 2044639Seric } 2057876Seric # ifdef LOG 2067876Seric if (LogLevel > 11) 2077943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 2087876Seric # endif LOG 2094639Seric 2107466Seric /* 2117466Seric ** Start making passes through the queue. 2127466Seric ** First, read and sort the entire queue. 2137466Seric ** Then, process the work in that order. 2147466Seric ** But if you take too long, start over. 2157466Seric */ 2167466Seric 2177943Seric /* order the existing work requests */ 2187943Seric orderq(); 2197690Seric 2207943Seric /* process them once at a time */ 2217943Seric while (WorkQ != NULL) 2224639Seric { 2237943Seric WORK *w = WorkQ; 2247881Seric 2257943Seric WorkQ = WorkQ->w_next; 2267943Seric dowork(w); 2277943Seric free(w->w_name); 2287943Seric free((char *) w); 2294639Seric } 2307943Seric finis(); 2314634Seric } 2324634Seric /* 2334632Seric ** ORDERQ -- order the work queue. 2344632Seric ** 2354632Seric ** Parameters: 2364632Seric ** none. 2374632Seric ** 2384632Seric ** Returns: 2394632Seric ** none. 2404632Seric ** 2414632Seric ** Side Effects: 2424632Seric ** Sets WorkQ to the queue of available work, in order. 2434632Seric */ 2444632Seric 2454632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2464632Seric 2474632Seric orderq() 2484632Seric { 2496625Sglickman register struct direct *d; 2504632Seric register WORK *w; 2514632Seric register WORK **wp; /* parent of w */ 2526625Sglickman DIR *f; 2534632Seric register int i; 2544632Seric WORK wlist[WLSIZE]; 2554632Seric int wn = 0; 2564632Seric extern workcmpf(); 2574632Seric 2584632Seric /* clear out old WorkQ */ 2594632Seric for (w = WorkQ; w != NULL; ) 2604632Seric { 2614632Seric register WORK *nw = w->w_next; 2624632Seric 2634632Seric WorkQ = nw; 2644632Seric free(w->w_name); 2654632Seric free((char *) w); 2664632Seric w = nw; 2674632Seric } 2684632Seric 2694632Seric /* open the queue directory */ 2708148Seric f = opendir("."); 2714632Seric if (f == NULL) 2724632Seric { 2738148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2744632Seric return; 2754632Seric } 2764632Seric 2774632Seric /* 2784632Seric ** Read the work directory. 2794632Seric */ 2804632Seric 2816625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 2824632Seric { 2834632Seric char lbuf[MAXNAME]; 2844632Seric FILE *cf; 2854632Seric 2864632Seric /* is this an interesting entry? */ 2877812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 2884632Seric continue; 2894632Seric 2908148Seric /* yes -- open control file */ 2918148Seric cf = fopen(d->d_name, "r"); 2924632Seric if (cf == NULL) 2934632Seric { 2947055Seric /* this may be some random person sending hir msgs */ 2957055Seric /* syserr("orderq: cannot open %s", cbuf); */ 2967055Seric errno = 0; 2974632Seric continue; 2984632Seric } 2998148Seric wlist[wn].w_name = newstr(d->d_name); 3004632Seric 3014632Seric /* extract useful information */ 3024632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3034632Seric { 3044632Seric fixcrlf(lbuf, TRUE); 3054632Seric 3064632Seric switch (lbuf[0]) 3074632Seric { 3084632Seric case 'P': /* message priority */ 3095037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3104632Seric break; 3114632Seric } 3124632Seric } 3134632Seric wn++; 3144632Seric (void) fclose(cf); 3154632Seric } 3166625Sglickman (void) closedir(f); 3174632Seric 3184632Seric /* 3194632Seric ** Sort the work directory. 3204632Seric */ 3214632Seric 3224632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3234632Seric 3244632Seric /* 3254632Seric ** Convert the work list into canonical form. 3264632Seric */ 3274632Seric 3284632Seric wp = &WorkQ; 3294632Seric for (i = 0; i < wn; i++) 3304632Seric { 3314632Seric w = (WORK *) xalloc(sizeof *w); 3324632Seric w->w_name = wlist[i].w_name; 3334632Seric w->w_pri = wlist[i].w_pri; 3344632Seric w->w_next = NULL; 3354632Seric *wp = w; 3364632Seric wp = &w->w_next; 3374632Seric } 3384632Seric 3394632Seric # ifdef DEBUG 3407677Seric if (tTd(40, 1)) 3414632Seric { 3424632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3435037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3444632Seric } 3454632Seric # endif DEBUG 3464632Seric } 3474632Seric /* 3487677Seric ** WORKCMPF -- compare function for ordering work. 3494632Seric ** 3504632Seric ** Parameters: 3514632Seric ** a -- the first argument. 3524632Seric ** b -- the second argument. 3534632Seric ** 3544632Seric ** Returns: 3554632Seric ** -1 if a < b 3564632Seric ** 0 if a == b 3574632Seric ** 1 if a > b 3584632Seric ** 3594632Seric ** Side Effects: 3604632Seric ** none. 3614632Seric */ 3624632Seric 3634632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3644632Seric 3654632Seric workcmpf(a, b) 3665037Seric register WORK *a; 3675037Seric register WORK *b; 3684632Seric { 3695037Seric if (a->w_pri == b->w_pri) 3704632Seric return (0); 3715037Seric else if (a->w_pri > b->w_pri) 3724632Seric return (1); 3734632Seric else 3744632Seric return (-1); 3754632Seric } 3764632Seric /* 3774632Seric ** DOWORK -- do a work request. 3784632Seric ** 3794632Seric ** Parameters: 3804632Seric ** w -- the work request to be satisfied. 3814632Seric ** 3824632Seric ** Returns: 3834632Seric ** none. 3844632Seric ** 3854632Seric ** Side Effects: 3864632Seric ** The work request is satisfied if possible. 3874632Seric */ 3884632Seric 3894632Seric dowork(w) 3904632Seric register WORK *w; 3914632Seric { 3924632Seric register int i; 3934632Seric auto int xstat; 3944632Seric 3954632Seric # ifdef DEBUG 3967677Seric if (tTd(40, 1)) 3975037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 3984632Seric # endif DEBUG 3994632Seric 4004632Seric /* 4014632Seric ** Fork for work. 4024632Seric */ 4034632Seric 4044632Seric i = fork(); 4054632Seric if (i < 0) 4064632Seric { 4074632Seric syserr("dowork: cannot fork"); 4084632Seric return; 4094632Seric } 4104632Seric 4114632Seric if (i == 0) 4124632Seric { 4136980Seric char buf[MAXNAME]; 4146980Seric 4154632Seric /* 4164632Seric ** CHILD 4178148Seric ** Lock the control file to avoid duplicate deliveries. 4188148Seric ** Then run the file as though we had just read it. 4197350Seric ** We save an idea of the temporary name so we 4207350Seric ** can recover on interrupt. 4214632Seric */ 4224632Seric 4237763Seric /* set basic modes, etc. */ 4247356Seric (void) alarm(0); 425*9338Seric CurEnv->e_flags &= ~EF_FATALERRS; 4264632Seric QueueRun = TRUE; 4276991Seric MailBack = TRUE; 4288148Seric CurEnv->e_id = &w->w_name[2]; 4297876Seric # ifdef LOG 4307876Seric if (LogLevel > 11) 4317881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4327881Seric getpid()); 4337876Seric # endif LOG 4347763Seric 4357763Seric /* don't use the headers from sendmail.cf... */ 4367763Seric CurEnv->e_header = NULL; 4377763Seric chompheader("from: $q", TRUE); 4387763Seric 4397763Seric /* create the link to the control file during processing */ 4407812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4416980Seric { 4427812Seric /* being processed by another queuer */ 4437881Seric # ifdef LOG 4447881Seric if (LogLevel > 4) 4457881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4467881Seric # endif LOG 4476980Seric exit(EX_OK); 4486980Seric } 4496980Seric 4506980Seric /* create ourselves a transcript file */ 4514634Seric openxscrpt(); 4526980Seric 4536980Seric /* do basic system initialization */ 4544632Seric initsys(); 4556980Seric 4566980Seric /* read the queue control file */ 457*9338Seric readqf(queuename(CurEnv, 'q')); 458*9338Seric CurEnv->e_flags |= EF_INQUEUE; 4597781Seric eatheader(); 4606980Seric 4616980Seric /* do the delivery */ 462*9338Seric if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 4639282Seric sendall(CurEnv, SM_DELIVER); 4646980Seric 4656980Seric /* if still not sent, perhaps we should time out.... */ 4664634Seric # ifdef DEBUG 4677677Seric if (tTd(40, 3)) 4687886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4697860Seric CurEnv->e_ctime + TimeOut); 4704634Seric # endif DEBUG 471*9338Seric if (curtime() > CurEnv->e_ctime + TimeOut) 472*9338Seric CurEnv->e_flags |= EF_TIMEOUT; 4736980Seric 4746980Seric /* finish up and exit */ 4754632Seric finis(); 4764632Seric } 4774632Seric 4784632Seric /* 4794632Seric ** Parent -- pick up results. 4804632Seric */ 4814632Seric 4824632Seric errno = 0; 4834632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 4844632Seric { 4854632Seric if (errno == EINTR) 4864632Seric { 4874632Seric errno = 0; 4884632Seric } 4894632Seric } 4904632Seric } 4914632Seric /* 4924632Seric ** READQF -- read queue file and set up environment. 4934632Seric ** 4944632Seric ** Parameters: 4954632Seric ** cf -- name of queue control file. 4964632Seric ** 4974632Seric ** Returns: 4984632Seric ** none. 4994632Seric ** 5004632Seric ** Side Effects: 5014632Seric ** cf is read and created as the current job, as though 5024632Seric ** we had been invoked by argument. 5034632Seric */ 5044632Seric 5054632Seric readqf(cf) 5064632Seric char *cf; 5074632Seric { 5084632Seric register FILE *f; 5097785Seric char buf[MAXFIELD]; 5107753Seric register char *p; 5117753Seric register int i; 5124632Seric 5134632Seric /* 5144632Seric ** Open the file created by queueup. 5154632Seric */ 5164632Seric 5174632Seric f = fopen(cf, "r"); 5184632Seric if (f == NULL) 5194632Seric { 5204632Seric syserr("readqf: no cf file %s", cf); 5214632Seric return; 5224632Seric } 5234632Seric 5244632Seric /* 5254632Seric ** Read and process the file. 5264632Seric */ 5274632Seric 5287356Seric if (Verbose) 5297356Seric printf("\nRunning %s\n", cf); 5307785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5314632Seric { 5324632Seric switch (buf[0]) 5334632Seric { 5344632Seric case 'R': /* specify recipient */ 5358080Seric sendto(&buf[1], (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5364632Seric break; 5374632Seric 5384632Seric case 'H': /* header */ 5394632Seric (void) chompheader(&buf[1], FALSE); 5404632Seric break; 5414632Seric 5424632Seric case 'S': /* sender */ 5434634Seric setsender(newstr(&buf[1])); 5444632Seric break; 5454632Seric 5464632Seric case 'D': /* data file name */ 5476991Seric CurEnv->e_df = newstr(&buf[1]); 5486991Seric TempFile = fopen(CurEnv->e_df, "r"); 5494632Seric if (TempFile == NULL) 5506991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5514632Seric break; 5524632Seric 5537860Seric case 'T': /* init time */ 5547860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5554632Seric break; 5564632Seric 5574634Seric case 'P': /* message priority */ 5586908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5595037Seric 5605037Seric /* make sure that big things get sent eventually */ 5616908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5624634Seric break; 5634634Seric 5646980Seric case 'C': /* message class */ 5656980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5666980Seric break; 5676980Seric 5685902Seric case 'M': /* define macro */ 5695902Seric define(buf[1], newstr(&buf[2])); 5705902Seric break; 5715902Seric 5724632Seric default: 5734632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5744632Seric break; 5754632Seric } 5764632Seric } 5774632Seric } 5784632Seric /* 5794632Seric ** TIMEOUT -- process timeout on queue file. 5804632Seric ** 5814632Seric ** Parameters: 582*9338Seric ** e -- the envelope that timed out. 5834632Seric ** 5844632Seric ** Returns: 5854632Seric ** none. 5864632Seric ** 5874632Seric ** Side Effects: 5884632Seric ** Returns a message to the sender saying that this 5894632Seric ** message has timed out. 5904632Seric */ 5914632Seric 592*9338Seric timeout(e) 593*9338Seric register ENVELOPE *e; 5944632Seric { 5957369Seric char buf[MAXLINE]; 5967860Seric extern char *pintvl(); 5977369Seric 5984634Seric # ifdef DEBUG 5997677Seric if (tTd(40, 3)) 600*9338Seric printf("timeout(%s)\n", e->e_id); 6014634Seric # endif DEBUG 602*9338Seric e->e_to = NULL; 6036991Seric message(Arpa_Info, "Message has timed out"); 6044634Seric 6054634Seric /* return message to sender */ 6067860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 607*9338Seric (void) returntosender(buf, &e->e_from, TRUE); 6084634Seric 6094634Seric /* arrange to remove files from queue */ 610*9338Seric e->e_flags |= EF_CLRQUEUE; 6114632Seric } 6125182Seric 6135182Seric # endif QUEUE 614