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*7943Seric SCCSID(@(#)queue.c 3.40 08/27/82 (no queueing)); 95182Seric # else QUEUE 104632Seric 11*7943Seric SCCSID(@(#)queue.c 3.40 08/27/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 { 188*7943Seric register int i; 1894632Seric 1907466Seric /* 1917466Seric ** See if we want to go off and do other useful work. 1927466Seric */ 1934639Seric 1944639Seric if (forkflag) 1954639Seric { 196*7943Seric int pid; 197*7943Seric 198*7943Seric pid = dofork(); 199*7943Seric if (pid != 0) 2004639Seric { 201*7943Seric /* parent -- pick up intermediate zombie */ 202*7943Seric do 203*7943Seric { 204*7943Seric auto int stat; 205*7943Seric 206*7943Seric i = wait(&stat); 207*7943Seric } while (i >= 0 && i != pid); 2087690Seric if (QueueIntvl != 0) 209*7943Seric setevent(QueueIntvl, runqueue, TRUE); 2104639Seric return; 2114639Seric } 212*7943Seric /* child -- double fork */ 213*7943Seric if (fork() != 0) 214*7943Seric exit(EX_OK); 2154639Seric } 2167876Seric # ifdef LOG 2177876Seric if (LogLevel > 11) 218*7943Seric 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 228*7943Seric /* order the existing work requests */ 229*7943Seric orderq(); 2307690Seric 231*7943Seric /* process them once at a time */ 232*7943Seric while (WorkQ != NULL) 2334639Seric { 234*7943Seric WORK *w = WorkQ; 2357881Seric 236*7943Seric WorkQ = WorkQ->w_next; 237*7943Seric dowork(w); 238*7943Seric free(w->w_name); 239*7943Seric free((char *) w); 2404639Seric } 241*7943Seric 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 extern char *QueueDir; 2694632Seric 2704632Seric /* clear out old WorkQ */ 2714632Seric for (w = WorkQ; w != NULL; ) 2724632Seric { 2734632Seric register WORK *nw = w->w_next; 2744632Seric 2754632Seric WorkQ = nw; 2764632Seric free(w->w_name); 2774632Seric free((char *) w); 2784632Seric w = nw; 2794632Seric } 2804632Seric 2814632Seric /* open the queue directory */ 2826625Sglickman f = opendir(QueueDir); 2834632Seric if (f == NULL) 2844632Seric { 2854632Seric syserr("orderq: cannot open %s", QueueDir); 2864632Seric return; 2874632Seric } 2884632Seric 2894632Seric /* 2904632Seric ** Read the work directory. 2914632Seric */ 2924632Seric 2936625Sglickman while (wn < WLSIZE && (d = readdir(f)) != NULL) 2944632Seric { 2954632Seric char cbuf[MAXNAME]; 2964632Seric char lbuf[MAXNAME]; 2974632Seric FILE *cf; 2984632Seric register char *p; 2994632Seric 3004632Seric /* is this an interesting entry? */ 3017812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 3024632Seric continue; 3034632Seric 3044632Seric /* yes -- find the control file location */ 3057009Seric (void) strcpy(cbuf, QueueDir); 3067009Seric (void) strcat(cbuf, "/"); 3074632Seric p = &cbuf[strlen(cbuf)]; 3087009Seric (void) strcpy(p, d->d_name); 3094632Seric 3104632Seric /* open control file */ 3114632Seric cf = fopen(cbuf, "r"); 3124632Seric if (cf == NULL) 3134632Seric { 3147055Seric /* this may be some random person sending hir msgs */ 3157055Seric /* syserr("orderq: cannot open %s", cbuf); */ 3167055Seric errno = 0; 3174632Seric continue; 3184632Seric } 3195037Seric wlist[wn].w_name = newstr(cbuf); 3204632Seric 3214632Seric /* extract useful information */ 3224632Seric while (fgets(lbuf, sizeof lbuf, cf) != NULL) 3234632Seric { 3244632Seric fixcrlf(lbuf, TRUE); 3254632Seric 3264632Seric switch (lbuf[0]) 3274632Seric { 3284632Seric case 'P': /* message priority */ 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. 3464632Seric */ 3474632Seric 3484632Seric wp = &WorkQ; 3494632Seric for (i = 0; i < wn; i++) 3504632Seric { 3514632Seric w = (WORK *) xalloc(sizeof *w); 3524632Seric w->w_name = wlist[i].w_name; 3534632Seric w->w_pri = wlist[i].w_pri; 3544632Seric w->w_next = NULL; 3554632Seric *wp = w; 3564632Seric wp = &w->w_next; 3574632Seric } 3584632Seric 3594632Seric # ifdef DEBUG 3607677Seric if (tTd(40, 1)) 3614632Seric { 3624632Seric for (w = WorkQ; w != NULL; w = w->w_next) 3635037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 3644632Seric } 3654632Seric # endif DEBUG 3664632Seric } 3674632Seric /* 3687677Seric ** WORKCMPF -- compare function for ordering work. 3694632Seric ** 3704632Seric ** Parameters: 3714632Seric ** a -- the first argument. 3724632Seric ** b -- the second argument. 3734632Seric ** 3744632Seric ** Returns: 3754632Seric ** -1 if a < b 3764632Seric ** 0 if a == b 3774632Seric ** 1 if a > b 3784632Seric ** 3794632Seric ** Side Effects: 3804632Seric ** none. 3814632Seric */ 3824632Seric 3834632Seric # define PRIFACT 1800 /* bytes each priority point is worth */ 3844632Seric 3854632Seric workcmpf(a, b) 3865037Seric register WORK *a; 3875037Seric register WORK *b; 3884632Seric { 3895037Seric if (a->w_pri == b->w_pri) 3904632Seric return (0); 3915037Seric else if (a->w_pri > b->w_pri) 3924632Seric return (1); 3934632Seric else 3944632Seric return (-1); 3954632Seric } 3964632Seric /* 3974632Seric ** DOWORK -- do a work request. 3984632Seric ** 3994632Seric ** Parameters: 4004632Seric ** w -- the work request to be satisfied. 4014632Seric ** 4024632Seric ** Returns: 4034632Seric ** none. 4044632Seric ** 4054632Seric ** Side Effects: 4064632Seric ** The work request is satisfied if possible. 4074632Seric */ 4084632Seric 4094632Seric dowork(w) 4104632Seric register WORK *w; 4114632Seric { 4124632Seric register int i; 4134632Seric auto int xstat; 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 { 4336980Seric char buf[MAXNAME]; 4346980Seric 4354632Seric /* 4364632Seric ** CHILD 4376980Seric ** Change the name of the control file to avoid 4387350Seric ** duplicate deliveries. Then run the file 4397350Seric ** as though we had just read it. 4407350Seric ** We save an idea of the temporary name so we 4417350Seric ** can recover on interrupt. 4424632Seric */ 4434632Seric 4447763Seric /* set basic modes, etc. */ 4457356Seric (void) alarm(0); 4467347Seric FatalErrors = FALSE; 4474632Seric QueueRun = TRUE; 4486991Seric MailBack = TRUE; 4497812Seric CurEnv->e_qf = w->w_name; 4507812Seric CurEnv->e_id = &w->w_name[strlen(QueueDir) + 3]; 4517876Seric # ifdef LOG 4527876Seric if (LogLevel > 11) 4537881Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 4547881Seric getpid()); 4557876Seric # endif LOG 4567763Seric 4577763Seric /* don't use the headers from sendmail.cf... */ 4587763Seric CurEnv->e_header = NULL; 4597763Seric chompheader("from: $q", TRUE); 4607763Seric 4617763Seric /* create the link to the control file during processing */ 4627812Seric if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 4636980Seric { 4647812Seric /* being processed by another queuer */ 4657881Seric # ifdef LOG 4667881Seric if (LogLevel > 4) 4677881Seric syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 4687881Seric # endif LOG 4696980Seric exit(EX_OK); 4706980Seric } 4716980Seric 4726980Seric /* create ourselves a transcript file */ 4734634Seric openxscrpt(); 4746980Seric 4756980Seric /* do basic system initialization */ 4764632Seric initsys(); 4776980Seric 4786980Seric /* read the queue control file */ 4797812Seric readqf(CurEnv->e_qf); 4807781Seric eatheader(); 4816980Seric 4826980Seric /* do the delivery */ 4837558Seric if (!FatalErrors) 4847558Seric sendall(CurEnv, FALSE); 4856980Seric 4866980Seric /* if still not sent, perhaps we should time out.... */ 4874634Seric # ifdef DEBUG 4887677Seric if (tTd(40, 3)) 4897886Seric printf("curtime=%ld, TimeOut=%ld\n", curtime(), 4907860Seric CurEnv->e_ctime + TimeOut); 4914634Seric # endif DEBUG 4927886Seric if (CurEnv->e_queueup && curtime() > CurEnv->e_ctime + TimeOut) 4934634Seric timeout(w); 4946980Seric 4956980Seric /* finish up and exit */ 4964632Seric finis(); 4974632Seric } 4984632Seric 4994632Seric /* 5004632Seric ** Parent -- pick up results. 5014632Seric */ 5024632Seric 5034632Seric errno = 0; 5044632Seric while ((i = wait(&xstat)) > 0 && errno != EINTR) 5054632Seric { 5064632Seric if (errno == EINTR) 5074632Seric { 5084632Seric errno = 0; 5094632Seric } 5104632Seric } 5114632Seric } 5124632Seric /* 5134632Seric ** READQF -- read queue file and set up environment. 5144632Seric ** 5154632Seric ** Parameters: 5164632Seric ** cf -- name of queue control file. 5174632Seric ** 5184632Seric ** Returns: 5194632Seric ** none. 5204632Seric ** 5214632Seric ** Side Effects: 5224632Seric ** cf is read and created as the current job, as though 5234632Seric ** we had been invoked by argument. 5244632Seric */ 5254632Seric 5264632Seric readqf(cf) 5274632Seric char *cf; 5284632Seric { 5294632Seric register FILE *f; 5307785Seric char buf[MAXFIELD]; 5317753Seric register char *p; 5327753Seric register int i; 5334632Seric 5344632Seric /* 5354632Seric ** Open the file created by queueup. 5364632Seric */ 5374632Seric 5384632Seric f = fopen(cf, "r"); 5394632Seric if (f == NULL) 5404632Seric { 5414632Seric syserr("readqf: no cf file %s", cf); 5424632Seric return; 5434632Seric } 5444632Seric 5454632Seric /* 5464632Seric ** Read and process the file. 5474632Seric */ 5484632Seric 5497356Seric if (Verbose) 5507356Seric printf("\nRunning %s\n", cf); 5517785Seric while (fgetfolded(buf, sizeof buf, f) != NULL) 5524632Seric { 5534632Seric switch (buf[0]) 5544632Seric { 5554632Seric case 'R': /* specify recipient */ 5566908Seric sendto(&buf[1], 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5574632Seric break; 5584632Seric 5594632Seric case 'H': /* header */ 5604632Seric (void) chompheader(&buf[1], FALSE); 5614632Seric break; 5624632Seric 5634632Seric case 'S': /* sender */ 5644634Seric setsender(newstr(&buf[1])); 5654632Seric break; 5664632Seric 5674632Seric case 'D': /* data file name */ 5686991Seric CurEnv->e_df = newstr(&buf[1]); 5696991Seric TempFile = fopen(CurEnv->e_df, "r"); 5704632Seric if (TempFile == NULL) 5716991Seric syserr("readqf: cannot open %s", CurEnv->e_df); 5724632Seric break; 5734632Seric 5747860Seric case 'T': /* init time */ 5757860Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_ctime); 5764632Seric break; 5774632Seric 5784634Seric case 'P': /* message priority */ 5796908Seric (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 5805037Seric 5815037Seric /* make sure that big things get sent eventually */ 5826908Seric CurEnv->e_msgpriority -= WKTIMEFACT; 5834634Seric break; 5844634Seric 5856980Seric case 'C': /* message class */ 5866980Seric (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 5876980Seric break; 5886980Seric 5895902Seric case 'M': /* define macro */ 5905902Seric define(buf[1], newstr(&buf[2])); 5915902Seric break; 5925902Seric 5934632Seric default: 5944632Seric syserr("readqf(%s): bad line \"%s\"", cf, buf); 5954632Seric break; 5964632Seric } 5974632Seric } 5984632Seric } 5994632Seric /* 6004632Seric ** TIMEOUT -- process timeout on queue file. 6014632Seric ** 6024632Seric ** Parameters: 6034632Seric ** w -- pointer to work request that timed out. 6044632Seric ** 6054632Seric ** Returns: 6064632Seric ** none. 6074632Seric ** 6084632Seric ** Side Effects: 6094632Seric ** Returns a message to the sender saying that this 6104632Seric ** message has timed out. 6114632Seric */ 6124632Seric 6134632Seric timeout(w) 6144632Seric register WORK *w; 6154632Seric { 6167369Seric char buf[MAXLINE]; 6177860Seric extern char *pintvl(); 6187369Seric 6194634Seric # ifdef DEBUG 6207677Seric if (tTd(40, 3)) 6214634Seric printf("timeout(%s)\n", w->w_name); 6224634Seric # endif DEBUG 6236991Seric message(Arpa_Info, "Message has timed out"); 6244634Seric 6254634Seric /* return message to sender */ 6267860Seric (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 6277369Seric (void) returntosender(buf, &CurEnv->e_from, TRUE); 6284634Seric 6294634Seric /* arrange to remove files from queue */ 6307860Seric CurEnv->e_dontqueue = TRUE; 6314632Seric } 6325182Seric 6335182Seric # endif QUEUE 634