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*8148Seric SCCSID(@(#)queue.c 3.42 09/11/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*8148Seric SCCSID(@(#)queue.c 3.42 09/11/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)) 1257812Seric fprintf(tfp, "R%s\n", q->q_paddr); 1264632Seric } 1274632Seric 1284632Seric /* output headers for this message */ 1297763Seric define('g', "$f"); 1306980Seric for (h = e->e_header; h != NULL; h = h->h_link) 1314632Seric { 1324632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 1334632Seric continue; 1347812Seric fprintf(tfp, "H"); 1354632Seric if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 1367812Seric mfdecode(h->h_mflags, tfp); 1377812Seric fprintf(tfp, "%s: ", h->h_field); 1387763Seric if (bitset(H_DEFAULT, h->h_flags)) 1397763Seric { 1407763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 1417812Seric fprintf(tfp, "%s\n", buf); 1427763Seric } 1437763Seric else 1447812Seric fprintf(tfp, "%s\n", h->h_value); 1454632Seric } 1464632Seric 1474632Seric /* 1484632Seric ** Clean up. 1494632Seric */ 1504632Seric 1517812Seric (void) fclose(tfp); 1527812Seric qf = queuename(e, 'q'); 1537812Seric (void) unlink(qf); 1547812Seric if (link(tf, qf) < 0) 1557812Seric syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 1566980Seric else 1577812Seric (void) unlink(tf); 1587812Seric e->e_qf = NULL; 1597391Seric 1607677Seric # ifdef LOG 1617677Seric /* save log info */ 1627878Seric if (LogLevel > 15) 1637878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 1647677Seric # endif LOG 1657677Seric 1667391Seric /* disconnect this temp file from the job */ 1677391Seric e->e_df = NULL; 1684632Seric } 1694632Seric /* 1704632Seric ** RUNQUEUE -- run the jobs in the queue. 1714632Seric ** 1724632Seric ** Gets the stuff out of the queue in some presumably logical 1734632Seric ** order and processes them. 1744632Seric ** 1754632Seric ** Parameters: 1764632Seric ** none. 1774632Seric ** 1784632Seric ** Returns: 1794632Seric ** none. 1804632Seric ** 1814632Seric ** Side Effects: 1824632Seric ** runs things in the mail queue. 1834632Seric */ 1844632Seric 1854639Seric runqueue(forkflag) 1864639Seric bool forkflag; 1874632Seric { 1887943Seric register int i; 1894632Seric 1907466Seric /* 1917466Seric ** See if we want to go off and do other useful work. 1927466Seric */ 1934639Seric 1944639Seric if (forkflag) 1954639Seric { 1967943Seric int pid; 1977943Seric 1987943Seric pid = dofork(); 1997943Seric if (pid != 0) 2004639Seric { 2017943Seric /* parent -- pick up intermediate zombie */ 2027943Seric do 2037943Seric { 2047943Seric auto int stat; 2057943Seric 2067943Seric i = wait(&stat); 2077943Seric } while (i >= 0 && i != pid); 2087690Seric if (QueueIntvl != 0) 2097943Seric setevent(QueueIntvl, runqueue, TRUE); 2104639Seric return; 2114639Seric } 2127943Seric /* child -- double fork */ 2137943Seric if (fork() != 0) 2147943Seric exit(EX_OK); 2154639Seric } 2167876Seric # ifdef LOG 2177876Seric if (LogLevel > 11) 2187943Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 2197876Seric # endif LOG 2204639Seric 2217466Seric /* 2227466Seric ** Start making passes through the queue. 2237466Seric ** First, read and sort the entire queue. 2247466Seric ** Then, process the work in that order. 2257466Seric ** But if you take too long, start over. 2267466Seric */ 2277466Seric 2287943Seric /* order the existing work requests */ 2297943Seric orderq(); 2307690Seric 2317943Seric /* process them once at a time */ 2327943Seric while (WorkQ != NULL) 2334639Seric { 2347943Seric WORK *w = WorkQ; 2357881Seric 2367943Seric WorkQ = WorkQ->w_next; 2377943Seric dowork(w); 2387943Seric free(w->w_name); 2397943Seric free((char *) w); 2404639Seric } 2417943Seric finis(); 2424634Seric } 2434634Seric /* 2444632Seric ** ORDERQ -- order the work queue. 2454632Seric ** 2464632Seric ** Parameters: 2474632Seric ** none. 2484632Seric ** 2494632Seric ** Returns: 2504632Seric ** none. 2514632Seric ** 2524632Seric ** Side Effects: 2534632Seric ** Sets WorkQ to the queue of available work, in order. 2544632Seric */ 2554632Seric 2564632Seric # define WLSIZE 120 /* max size of worklist per sort */ 2574632Seric 2584632Seric orderq() 2594632Seric { 2606625Sglickman register struct direct *d; 2614632Seric register WORK *w; 2624632Seric register WORK **wp; /* parent of w */ 2636625Sglickman DIR *f; 2644632Seric register int i; 2654632Seric WORK wlist[WLSIZE]; 2664632Seric int wn = 0; 2674632Seric extern workcmpf(); 2684632Seric 2694632Seric /* clear out old WorkQ */ 2704632Seric for (w = WorkQ; w != NULL; ) 2714632Seric { 2724632Seric register WORK *nw = w->w_next; 2734632Seric 2744632Seric WorkQ = nw; 2754632Seric free(w->w_name); 2764632Seric free((char *) w); 2774632Seric w = nw; 2784632Seric } 2794632Seric 2804632Seric /* open the queue directory */ 281*8148Seric f = opendir("."); 2824632Seric if (f == NULL) 2834632Seric { 284*8148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 2854632Seric return; 2864632Seric } 2874632Seric 2884632Seric /* 2894632Seric ** Read the work directory. 2904632Seric */ 2914632Seric 2926625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 2934632Seric { 2944632Seric char lbuf[MAXNAME]; 2954632Seric FILE *cf; 2964632Seric 2974632Seric /* is this an interesting entry? */ 2987812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 2994632Seric continue; 3004632Seric 301*8148Seric /* yes -- open control file */ 302*8148Seric cf = fopen(d->d_name, "r"); 3034632Seric if (cf == NULL) 3044632Seric { 3057055Seric /* this may be some random person sending hir msgs */ 3067055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3077055Seric errno = 0; 3084632Seric continue; 3094632Seric } 310*8148Seric wlist[wn].w_name = newstr(d->d_name); 3114632Seric 3124632Seric /* extract useful information */ 3134632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3144632Seric { 3154632Seric fixcrlf(lbuf, TRUE); 3164632Seric 3174632Seric switch (lbuf[0]) 3184632Seric { 3194632Seric case 'P': /* message priority */ 3205037Seric (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 3214632Seric break; 3224632Seric } 3234632Seric } 3244632Seric wn++; 3254632Seric (void) fclose(cf); 3264632Seric } 3276625Sglickman (void) closedir(f); 3284632Seric 3294632Seric /* 3304632Seric ** Sort the work directory. 3314632Seric */ 3324632Seric 3334632Seric qsort(wlist, wn, sizeof *wlist, workcmpf); 3344632Seric 3354632Seric /* 3364632Seric ** Convert the work list into canonical form. 3374632Seric */ 3384632Seric 3394632Seric wp = &WorkQ; 3404632Seric for (i = 0; i < wn; i++) 3414632Seric { 3424632Seric w = (WORK *) xalloc(sizeof *w); 3434632Seric w->w_name = wlist[i].w_name; 3444632Seric w->w_pri = wlist[i].w_pri; 3454632Seric w->w_next = NULL; 3464632Seric *wp = w; 3474632Seric wp = &w->w_next; 3484632Seric } 3494632Seric 3504632Seric # ifdef DEBUG 3517677Seric if (tTd(40, 1)) 3524632Seric { 3534632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3545037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3554632Seric } 3564632Seric # endif DEBUG 3574632Seric } 3584632Seric /* 3597677Seric ** WORKCMPF -- compare function for ordering work. 3604632Seric ** 3614632Seric ** Parameters: 3624632Seric ** a -- the first argument. 3634632Seric ** b -- the second argument. 3644632Seric ** 3654632Seric ** Returns: 3664632Seric ** -1 if a < b 3674632Seric ** 0 if a == b 3684632Seric ** 1 if a > b 3694632Seric ** 3704632Seric ** Side Effects: 3714632Seric ** none. 3724632Seric */ 3734632Seric 3744632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3754632Seric 3764632Seric workcmpf(a, b) 3775037Seric register WORK *a; 3785037Seric register WORK *b; 3794632Seric { 3805037Seric if (a->w_pri == b->w_pri) 3814632Seric return (0); 3825037Seric else if (a->w_pri > b->w_pri) 3834632Seric return (1); 3844632Seric else 3854632Seric return (-1); 3864632Seric } 3874632Seric /* 3884632Seric ** DOWORK -- do a work request. 3894632Seric ** 3904632Seric ** Parameters: 3914632Seric ** w -- the work request to be satisfied. 3924632Seric ** 3934632Seric ** Returns: 3944632Seric ** none. 3954632Seric ** 3964632Seric ** Side Effects: 3974632Seric ** The work request is satisfied if possible. 3984632Seric */ 3994632Seric 4004632Seric dowork(w) 4014632Seric register WORK *w; 4024632Seric { 4034632Seric register int i; 4044632Seric auto int xstat; 4054632Seric 4064632Seric # ifdef DEBUG 4077677Seric if (tTd(40, 1)) 4085037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 4094632Seric # endif DEBUG 4104632Seric 4114632Seric /* 4124632Seric ** Fork for work. 4134632Seric */ 4144632Seric 4154632Seric i = fork(); 4164632Seric if (i < 0) 4174632Seric { 4184632Seric syserr("dowork: cannot fork"); 4194632Seric return; 4204632Seric } 4214632Seric 4224632Seric if (i == 0) 4234632Seric { 4246980Seric char buf[MAXNAME]; 4256980Seric 4264632Seric /* 4274632Seric ** CHILD 428*8148Seric ** Lock the control file to avoid duplicate deliveries. 429*8148Seric ** Then run the file as though we had just read it. 4307350Seric ** We save an idea of the temporary name so we 4317350Seric ** can recover on interrupt. 4324632Seric */ 4334632Seric 4347763Seric /* set basic modes, etc. */ 4357356Seric (void) alarm(0); 4367347Seric FatalErrors = FALSE; 4374632Seric QueueRun = TRUE; 4386991Seric MailBack = TRUE; 4397812Seric CurEnv->e_qf = w->w_name; 440*8148Seric CurEnv->e_id = &w->w_name[2]; 4417876Seric # ifdef LOG 4427876Seric if (LogLevel > 11) 4437881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4447881Seric getpid()); 4457876Seric # endif LOG 4467763Seric 4477763Seric /* don't use the headers from sendmail.cf... */ 4487763Seric CurEnv->e_header = NULL; 4497763Seric chompheader("from: $q", TRUE); 4507763Seric 4517763Seric /* create the link to the control file during processing */ 4527812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4536980Seric { 4547812Seric /* being processed by another queuer */ 4557881Seric # ifdef LOG 4567881Seric if (LogLevel > 4) 4577881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4587881Seric # endif LOG 4596980Seric exit(EX_OK); 4606980Seric } 4616980Seric 4626980Seric /* create ourselves a transcript file */ 4634634Seric openxscrpt(); 4646980Seric 4656980Seric /* do basic system initialization */ 4664632Seric initsys(); 4676980Seric 4686980Seric /* read the queue control file */ 4697812Seric readqf(CurEnv->e_qf); 4707781Seric eatheader(); 4716980Seric 4726980Seric /* do the delivery */ 4737558Seric if (!FatalErrors) 4747558Seric sendall(CurEnv, FALSE); 4756980Seric 4766980Seric /* if still not sent, perhaps we should time out.... */ 4774634Seric # ifdef DEBUG 4787677Seric if (tTd(40, 3)) 4797886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4807860Seric CurEnv->e_ctime + TimeOut); 4814634Seric # endif DEBUG 4827886Seric if (CurEnv->e_queueup && curtime() > CurEnv->e_ctime + TimeOut) 4834634Seric timeout(w); 4846980Seric 4856980Seric /* finish up and exit */ 4864632Seric finis(); 4874632Seric } 4884632Seric 4894632Seric /* 4904632Seric ** Parent -- pick up results. 4914632Seric */ 4924632Seric 4934632Seric errno = 0; 4944632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 4954632Seric { 4964632Seric if (errno == EINTR) 4974632Seric { 4984632Seric errno = 0; 4994632Seric } 5004632Seric } 5014632Seric } 5024632Seric /* 5034632Seric ** READQF -- read queue file and set up environment. 5044632Seric ** 5054632Seric ** Parameters: 5064632Seric ** cf -- name of queue control file. 5074632Seric ** 5084632Seric ** Returns: 5094632Seric ** none. 5104632Seric ** 5114632Seric ** Side Effects: 5124632Seric ** cf is read and created as the current job, as though 5134632Seric ** we had been invoked by argument. 5144632Seric */ 5154632Seric 5164632Seric readqf(cf) 5174632Seric char *cf; 5184632Seric { 5194632Seric register FILE *f; 5207785Seric char buf[MAXFIELD]; 5217753Seric register char *p; 5227753Seric register int i; 5234632Seric 5244632Seric /* 5254632Seric ** Open the file created by queueup. 5264632Seric */ 5274632Seric 5284632Seric f = fopen(cf, "r"); 5294632Seric if (f == NULL) 5304632Seric { 5314632Seric syserr("readqf: no cf file %s", cf); 5324632Seric return; 5334632Seric } 5344632Seric 5354632Seric /* 5364632Seric ** Read and process the file. 5374632Seric */ 5384632Seric 5397356Seric if (Verbose) 5407356Seric printf("\nRunning %s\n", cf); 5417785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5424632Seric { 5434632Seric switch (buf[0]) 5444632Seric { 5454632Seric case 'R': /* specify recipient */ 5468080Seric sendto(&buf[1], (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5474632Seric break; 5484632Seric 5494632Seric case 'H': /* header */ 5504632Seric (void) chompheader(&buf[1], FALSE); 5514632Seric break; 5524632Seric 5534632Seric case 'S': /* sender */ 5544634Seric setsender(newstr(&buf[1])); 5554632Seric break; 5564632Seric 5574632Seric case 'D': /* data file name */ 5586991Seric CurEnv->e_df = newstr(&buf[1]); 5596991Seric TempFile = fopen(CurEnv->e_df, "r"); 5604632Seric if (TempFile == NULL) 5616991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5624632Seric break; 5634632Seric 5647860Seric case 'T': /* init time */ 5657860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5664632Seric break; 5674632Seric 5684634Seric case 'P': /* message priority */ 5696908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5705037Seric 5715037Seric /* make sure that big things get sent eventually */ 5726908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5734634Seric break; 5744634Seric 5756980Seric case 'C': /* message class */ 5766980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5776980Seric break; 5786980Seric 5795902Seric case 'M': /* define macro */ 5805902Seric define(buf[1], newstr(&buf[2])); 5815902Seric break; 5825902Seric 5834632Seric default: 5844632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5854632Seric break; 5864632Seric } 5874632Seric } 5884632Seric } 5894632Seric /* 5904632Seric ** TIMEOUT -- process timeout on queue file. 5914632Seric ** 5924632Seric ** Parameters: 5934632Seric ** w -- pointer to work request 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 6034632Seric timeout(w) 6044632Seric register WORK *w; 6054632Seric { 6067369Seric char buf[MAXLINE]; 6077860Seric extern char *pintvl(); 6087369Seric 6094634Seric # ifdef DEBUG 6107677Seric if (tTd(40, 3)) 6114634Seric printf("timeout(%s)\n", w->w_name); 6124634Seric # endif DEBUG 6136991Seric message(Arpa_Info, "Message has timed out"); 6144634Seric 6154634Seric /* return message to sender */ 6167860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6177369Seric (void) returntosender(buf, &CurEnv->e_from, TRUE); 6184634Seric 6194634Seric /* arrange to remove files from queue */ 6207860Seric CurEnv->e_dontqueue = TRUE; 6214632Seric } 6225182Seric 6235182Seric # endif QUEUE 624