122708Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822708Sdist 933731Sbostic # include "sendmail.h" 1022708Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef QUEUE 13*57731Seric static char sccsid[] = "@(#)queue.c 6.6 (Berkeley) 01/26/93 (with queueing)"; 1433731Sbostic #else 15*57731Seric static char sccsid[] = "@(#)queue.c 6.6 (Berkeley) 01/26/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 2140934Srick # include <sys/file.h> 224634Seric # include <signal.h> 234632Seric # include <errno.h> 2440973Sbostic # include <pwd.h> 2551937Seric # ifdef LOCKF 2651937Seric # include <fcntl.h> 2751937Seric # endif 284632Seric 2933731Sbostic # ifdef QUEUE 304632Seric 314632Seric /* 329377Seric ** Work queue. 339377Seric */ 349377Seric 359377Seric struct work 369377Seric { 379377Seric char *w_name; /* name of control file */ 389377Seric long w_pri; /* priority of message, see below */ 3925013Seric time_t w_ctime; /* creation time of message */ 409377Seric struct work *w_next; /* next in queue */ 419377Seric }; 429377Seric 439377Seric typedef struct work WORK; 449377Seric 459377Seric WORK *WorkQ; /* queue of things to be done */ 469377Seric /* 474632Seric ** QUEUEUP -- queue a message up for future transmission. 484632Seric ** 494632Seric ** Parameters: 506980Seric ** e -- the envelope to queue up. 516999Seric ** queueall -- if TRUE, queue all addresses, rather than 526999Seric ** just those with the QQUEUEUP flag set. 539377Seric ** announce -- if TRUE, tell when you are queueing up. 544632Seric ** 554632Seric ** Returns: 5651920Seric ** none. 574632Seric ** 584632Seric ** Side Effects: 599377Seric ** The current request are saved in a control file. 6051920Seric ** The queue file is left locked. 614632Seric */ 624632Seric 639377Seric queueup(e, queueall, announce) 646980Seric register ENVELOPE *e; 656999Seric bool queueall; 669377Seric bool announce; 674632Seric { 687812Seric char *qf; 697812Seric register FILE *tfp; 704632Seric register HDR *h; 715007Seric register ADDRESS *q; 7251920Seric int fd; 7351920Seric int i; 7451920Seric bool newid; 7553400Seric register char *p; 7610173Seric MAILER nullmailer; 7754974Seric ADDRESS *lastctladdr; 7854975Seric static ADDRESS *nullctladdr = NULL; 7951920Seric char buf[MAXLINE], tf[MAXLINE]; 8053400Seric extern char *macvalue(); 8154974Seric extern ADDRESS *getctladdr(); 824632Seric 835037Seric /* 8454975Seric ** If we don't have nullctladdr, create one 8554975Seric */ 8654975Seric 8754975Seric if (nullctladdr == NULL) 8854975Seric { 8954975Seric nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr); 9054975Seric bzero((char *) nullctladdr, sizeof nullctladdr); 9154975Seric } 9254975Seric 9354975Seric /* 9417477Seric ** Create control file. 955037Seric */ 964632Seric 9751920Seric newid = (e->e_id == NULL); 9851920Seric strcpy(tf, queuename(e, 't')); 9951920Seric tfp = e->e_lockfp; 10051920Seric if (tfp == NULL) 10151920Seric newid = FALSE; 10251920Seric if (newid) 10351835Seric { 10451920Seric tfp = e->e_lockfp; 10551920Seric } 10651920Seric else 10751920Seric { 10851920Seric /* get a locked tf file */ 10951920Seric for (i = 100; --i >= 0; ) 11051835Seric { 11151937Seric # ifdef LOCKF 11251937Seric struct flock lfd; 11351937Seric # endif 11451937Seric 11551920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 11651920Seric if (fd < 0) 11751835Seric { 11851920Seric if (errno == EEXIST) 11951920Seric continue; 12051920Seric syserr("queueup: cannot create temp file %s", tf); 12151920Seric return; 12241636Srick } 12351835Seric # ifdef LOCKF 12451937Seric lfd.l_type = F_WRLCK; 12551937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 12651937Seric if (fcntl(fd, F_SETLK, &lfd) >= 0) 12751920Seric break; 12851920Seric if (errno != EACCES && errno != EAGAIN) 12951920Seric syserr("cannot lockf(%s)", tf); 13051835Seric # else 13151920Seric if (flock(fd, LOCK_EX|LOCK_NB) >= 0) 13251920Seric break; 13351920Seric if (errno != EWOULDBLOCK) 13451920Seric syserr("cannot flock(%s)", tf); 13551835Seric # endif 13651920Seric close(fd); 13741636Srick } 13841636Srick 13951920Seric tfp = fdopen(fd, "w"); 14051920Seric } 1414632Seric 1427677Seric if (tTd(40, 1)) 14317468Seric printf("queueing %s\n", e->e_id); 1444632Seric 1454632Seric /* 1466980Seric ** If there is no data file yet, create one. 1476980Seric */ 1486980Seric 1496980Seric if (e->e_df == NULL) 1506980Seric { 1516980Seric register FILE *dfp; 1529389Seric extern putbody(); 1536980Seric 1547812Seric e->e_df = newstr(queuename(e, 'd')); 15540934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 15640934Srick if (fd < 0) 1576980Seric { 1586980Seric syserr("queueup: cannot create %s", e->e_df); 15951920Seric if (!newid) 16051920Seric (void) fclose(tfp); 16151920Seric return; 1626980Seric } 16340934Srick dfp = fdopen(fd, "w"); 16410173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1657009Seric (void) fclose(dfp); 1669389Seric e->e_putbody = putbody; 1676980Seric } 1686980Seric 1696980Seric /* 1704632Seric ** Output future work requests. 17125687Seric ** Priority and creation time should be first, since 17225687Seric ** they are required by orderq. 1734632Seric */ 1744632Seric 1759377Seric /* output message priority */ 1769377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1779377Seric 1789630Seric /* output creation time */ 1799630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1809630Seric 1814632Seric /* output name of data file */ 1827812Seric fprintf(tfp, "D%s\n", e->e_df); 1834632Seric 18410108Seric /* message from envelope, if it exists */ 18510108Seric if (e->e_message != NULL) 18610108Seric fprintf(tfp, "M%s\n", e->e_message); 18710108Seric 18853400Seric /* $r and $s macro values */ 18953400Seric if ((p = macvalue('r', e)) != NULL) 19053400Seric fprintf(tfp, "$r%s\n", p); 19153400Seric if ((p = macvalue('s', e)) != NULL) 19253400Seric fprintf(tfp, "$s%s\n", p); 19353400Seric 1944632Seric /* output name of sender */ 1957812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1964632Seric 19755360Seric /* output list of error recipients */ 19855360Seric lastctladdr = NULL; 19955360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20055360Seric { 20155360Seric if (!bitset(QDONTSEND, q->q_flags)) 20255360Seric { 20355360Seric ADDRESS *ctladdr; 20455360Seric 20555360Seric ctladdr = getctladdr(q); 20655360Seric if (ctladdr == NULL && q->q_alias != NULL) 20755360Seric ctladdr = nullctladdr; 20855360Seric if (ctladdr != lastctladdr) 20955360Seric { 21055360Seric printctladdr(ctladdr, tfp); 21155360Seric lastctladdr = ctladdr; 21255360Seric } 21355360Seric fprintf(tfp, "E%s\n", q->q_paddr); 21455360Seric } 21555360Seric } 21655360Seric 2174632Seric /* output list of recipient addresses */ 2186980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2194632Seric { 22047284Seric if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) : 2217763Seric bitset(QQUEUEUP, q->q_flags)) 2228245Seric { 22354974Seric ADDRESS *ctladdr; 22440973Sbostic 22554975Seric ctladdr = getctladdr(q); 22654975Seric if (ctladdr == NULL && q->q_alias != NULL) 22754975Seric ctladdr = nullctladdr; 22854975Seric if (ctladdr != lastctladdr) 22954974Seric { 23054974Seric printctladdr(ctladdr, tfp); 23154974Seric lastctladdr = ctladdr; 23254974Seric } 2337812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2349377Seric if (announce) 2359377Seric { 2369377Seric e->e_to = q->q_paddr; 2379377Seric message(Arpa_Info, "queued"); 2389377Seric if (LogLevel > 4) 23954967Seric logdelivery("queued", e); 2409377Seric e->e_to = NULL; 2419377Seric } 2429387Seric if (tTd(40, 1)) 2439387Seric { 2449387Seric printf("queueing "); 2459387Seric printaddr(q, FALSE); 2469387Seric } 2478245Seric } 2484632Seric } 2494632Seric 2509377Seric /* 2519377Seric ** Output headers for this message. 2529377Seric ** Expand macros completely here. Queue run will deal with 2539377Seric ** everything as absolute headers. 2549377Seric ** All headers that must be relative to the recipient 2559377Seric ** can be cracked later. 25610173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 25710173Seric ** no effect on the addresses as they are output. 2589377Seric */ 2599377Seric 26010686Seric bzero((char *) &nullmailer, sizeof nullmailer); 26110173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 26210349Seric nullmailer.m_eol = "\n"; 26310173Seric 26416147Seric define('g', "\001f", e); 2656980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2664632Seric { 26710686Seric extern bool bitzerop(); 26810686Seric 26912015Seric /* don't output null headers */ 2704632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2714632Seric continue; 27212015Seric 27312015Seric /* don't output resent headers on non-resent messages */ 27412015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 27512015Seric continue; 27612015Seric 27712015Seric /* output this header */ 2787812Seric fprintf(tfp, "H"); 27912015Seric 28012015Seric /* if conditional, output the set of conditions */ 28110686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 28210686Seric { 28310686Seric int j; 28410686Seric 28523098Seric (void) putc('?', tfp); 28610686Seric for (j = '\0'; j <= '\177'; j++) 28710686Seric if (bitnset(j, h->h_mflags)) 28823098Seric (void) putc(j, tfp); 28923098Seric (void) putc('?', tfp); 29010686Seric } 29112015Seric 29212015Seric /* output the header: expand macros, convert addresses */ 2937763Seric if (bitset(H_DEFAULT, h->h_flags)) 2947763Seric { 2957763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2968236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2977763Seric } 2988245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2999348Seric { 30055012Seric commaize(h, h->h_value, tfp, 30155012Seric bitset(EF_OLDSTYLE, e->e_flags), 30255012Seric &nullmailer, e); 3039348Seric } 3047763Seric else 3058245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3064632Seric } 3074632Seric 3084632Seric /* 3094632Seric ** Clean up. 3104632Seric */ 3114632Seric 31251920Seric if (!newid) 31351920Seric { 31451920Seric qf = queuename(e, 'q'); 31551920Seric if (rename(tf, qf) < 0) 31651920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31751920Seric if (e->e_lockfp != NULL) 31851920Seric (void) fclose(e->e_lockfp); 31951920Seric e->e_lockfp = tfp; 32051920Seric } 32151920Seric else 32251920Seric qf = tf; 32341636Srick errno = 0; 3247391Seric 3257677Seric # ifdef LOG 3267677Seric /* save log info */ 3277878Seric if (LogLevel > 15) 3287878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 32956795Seric # endif /* LOG */ 33040934Srick fflush(tfp); 33151920Seric return; 3324632Seric } 33354974Seric 33454974Seric printctladdr(a, tfp) 33554974Seric ADDRESS *a; 33654974Seric FILE *tfp; 33754974Seric { 33854974Seric char *u; 33954974Seric struct passwd *pw; 34054974Seric extern struct passwd *getpwuid(); 34154974Seric 34254974Seric if (a == NULL) 34354974Seric { 34454974Seric fprintf(tfp, "C\n"); 34554974Seric return; 34654974Seric } 34754974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 34854974Seric u = DefUser; 34954974Seric else 35054974Seric u = pw->pw_name; 35154974Seric fprintf(tfp, "C%s\n", u); 35254974Seric } 35354974Seric 3544632Seric /* 3554632Seric ** RUNQUEUE -- run the jobs in the queue. 3564632Seric ** 3574632Seric ** Gets the stuff out of the queue in some presumably logical 3584632Seric ** order and processes them. 3594632Seric ** 3604632Seric ** Parameters: 36124941Seric ** forkflag -- TRUE if the queue scanning should be done in 36224941Seric ** a child process. We double-fork so it is not our 36324941Seric ** child and we don't have to clean up after it. 3644632Seric ** 3654632Seric ** Returns: 3664632Seric ** none. 3674632Seric ** 3684632Seric ** Side Effects: 3694632Seric ** runs things in the mail queue. 3704632Seric */ 3714632Seric 37255360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 37355360Seric 37455360Seric runqueue(forkflag) 3754639Seric bool forkflag; 3764632Seric { 37724953Seric extern bool shouldqueue(); 37855360Seric register ENVELOPE *e; 37955360Seric extern ENVELOPE BlankEnvelope; 38055360Seric extern ENVELOPE *newenvelope(); 38124953Seric 3827466Seric /* 38324953Seric ** If no work will ever be selected, don't even bother reading 38424953Seric ** the queue. 38524953Seric */ 38624953Seric 38751920Seric CurrentLA = getla(); /* get load average */ 38840934Srick 38957438Seric if (shouldqueue(-100000000L, curtime())) 39024953Seric { 39124953Seric if (Verbose) 39224953Seric printf("Skipping queue run -- load average too high\n"); 39355360Seric return; 39424953Seric } 39524953Seric 39624953Seric /* 3977466Seric ** See if we want to go off and do other useful work. 3987466Seric */ 3994639Seric 4004639Seric if (forkflag) 4014639Seric { 4027943Seric int pid; 4037943Seric 4047943Seric pid = dofork(); 4057943Seric if (pid != 0) 4064639Seric { 40746928Sbostic extern void reapchild(); 40825184Seric 4097943Seric /* parent -- pick up intermediate zombie */ 41025184Seric #ifndef SIGCHLD 4119377Seric (void) waitfor(pid); 41256795Seric #else /* SIGCHLD */ 41325184Seric (void) signal(SIGCHLD, reapchild); 41456795Seric #endif /* SIGCHLD */ 4157690Seric if (QueueIntvl != 0) 4169348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4174639Seric return; 4184639Seric } 4197943Seric /* child -- double fork */ 42025184Seric #ifndef SIGCHLD 4217943Seric if (fork() != 0) 4227943Seric exit(EX_OK); 42356795Seric #else /* SIGCHLD */ 42425184Seric (void) signal(SIGCHLD, SIG_DFL); 42556795Seric #endif /* SIGCHLD */ 4264639Seric } 42724941Seric 42840934Srick setproctitle("running queue: %s", QueueDir); 429*57731Seric ForceMail = TRUE; 43024941Seric 4317876Seric # ifdef LOG 4327876Seric if (LogLevel > 11) 43355360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 43455360Seric QueueDir, getpid(), forkflag); 43556795Seric # endif /* LOG */ 4364639Seric 4377466Seric /* 43810205Seric ** Release any resources used by the daemon code. 43910205Seric */ 44010205Seric 44110205Seric # ifdef DAEMON 44210205Seric clrdaemon(); 44356795Seric # endif /* DAEMON */ 44410205Seric 44510205Seric /* 44655360Seric ** Create ourselves an envelope 44755360Seric */ 44855360Seric 44955360Seric CurEnv = &QueueEnvelope; 45055360Seric e = newenvelope(&QueueEnvelope); 45155360Seric e->e_flags = BlankEnvelope.e_flags; 45255360Seric 45355360Seric /* 45427175Seric ** Make sure the alias database is open. 45527175Seric */ 45627175Seric 45755012Seric initaliases(AliasFile, FALSE, e); 45827175Seric 45927175Seric /* 4607466Seric ** Start making passes through the queue. 4617466Seric ** First, read and sort the entire queue. 4627466Seric ** Then, process the work in that order. 4637466Seric ** But if you take too long, start over. 4647466Seric */ 4657466Seric 4667943Seric /* order the existing work requests */ 46724954Seric (void) orderq(FALSE); 4687690Seric 4697943Seric /* process them once at a time */ 4707943Seric while (WorkQ != NULL) 4714639Seric { 4727943Seric WORK *w = WorkQ; 4737881Seric 4747943Seric WorkQ = WorkQ->w_next; 47555012Seric dowork(w, e); 4767943Seric free(w->w_name); 4777943Seric free((char *) w); 4784639Seric } 47929866Seric 48029866Seric /* exit without the usual cleanup */ 48155467Seric e->e_id = NULL; 48255467Seric finis(); 4834634Seric } 4844634Seric /* 4854632Seric ** ORDERQ -- order the work queue. 4864632Seric ** 4874632Seric ** Parameters: 48824941Seric ** doall -- if set, include everything in the queue (even 48924941Seric ** the jobs that cannot be run because the load 49024941Seric ** average is too high). Otherwise, exclude those 49124941Seric ** jobs. 4924632Seric ** 4934632Seric ** Returns: 49410121Seric ** The number of request in the queue (not necessarily 49510121Seric ** the number of requests in WorkQ however). 4964632Seric ** 4974632Seric ** Side Effects: 4984632Seric ** Sets WorkQ to the queue of available work, in order. 4994632Seric */ 5004632Seric 50125687Seric # define NEED_P 001 50225687Seric # define NEED_T 002 5034632Seric 50424941Seric orderq(doall) 50524941Seric bool doall; 5064632Seric { 5076625Sglickman register struct direct *d; 5084632Seric register WORK *w; 5096625Sglickman DIR *f; 5104632Seric register int i; 51125687Seric WORK wlist[QUEUESIZE+1]; 51210070Seric int wn = -1; 5134632Seric extern workcmpf(); 5144632Seric 5154632Seric /* clear out old WorkQ */ 5164632Seric for (w = WorkQ; w != NULL; ) 5174632Seric { 5184632Seric register WORK *nw = w->w_next; 5194632Seric 5204632Seric WorkQ = nw; 5214632Seric free(w->w_name); 5224632Seric free((char *) w); 5234632Seric w = nw; 5244632Seric } 5254632Seric 5264632Seric /* open the queue directory */ 5278148Seric f = opendir("."); 5284632Seric if (f == NULL) 5294632Seric { 5308148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 53110070Seric return (0); 5324632Seric } 5334632Seric 5344632Seric /* 5354632Seric ** Read the work directory. 5364632Seric */ 5374632Seric 53810070Seric while ((d = readdir(f)) != NULL) 5394632Seric { 5409377Seric FILE *cf; 5414632Seric char lbuf[MAXNAME]; 54257642Seric extern bool shouldqueue(); 5434632Seric 5444632Seric /* is this an interesting entry? */ 5457812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5464632Seric continue; 5474632Seric 54810070Seric /* yes -- open control file (if not too many files) */ 54925687Seric if (++wn >= QUEUESIZE) 55010070Seric continue; 5518148Seric cf = fopen(d->d_name, "r"); 5524632Seric if (cf == NULL) 5534632Seric { 5547055Seric /* this may be some random person sending hir msgs */ 5557055Seric /* syserr("orderq: cannot open %s", cbuf); */ 55610090Seric if (tTd(41, 2)) 55710090Seric printf("orderq: cannot open %s (%d)\n", 55810090Seric d->d_name, errno); 5597055Seric errno = 0; 56010090Seric wn--; 5614632Seric continue; 5624632Seric } 56325687Seric w = &wlist[wn]; 56425687Seric w->w_name = newstr(d->d_name); 5654632Seric 56625027Seric /* make sure jobs in creation don't clog queue */ 56725687Seric w->w_pri = 0x7fffffff; 56825687Seric w->w_ctime = 0; 56925027Seric 5704632Seric /* extract useful information */ 57125687Seric i = NEED_P | NEED_T; 57225687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 5734632Seric { 57424954Seric extern long atol(); 57524954Seric 57624941Seric switch (lbuf[0]) 5774632Seric { 57824941Seric case 'P': 57925687Seric w->w_pri = atol(&lbuf[1]); 58025687Seric i &= ~NEED_P; 5814632Seric break; 58225013Seric 58325013Seric case 'T': 58425687Seric w->w_ctime = atol(&lbuf[1]); 58525687Seric i &= ~NEED_T; 58625013Seric break; 5874632Seric } 5884632Seric } 5894632Seric (void) fclose(cf); 59024953Seric 59157438Seric if (!doall && shouldqueue(w->w_pri, w->w_ctime)) 59224953Seric { 59324953Seric /* don't even bother sorting this job in */ 59424953Seric wn--; 59524953Seric } 5964632Seric } 5976625Sglickman (void) closedir(f); 59810090Seric wn++; 5994632Seric 6004632Seric /* 6014632Seric ** Sort the work directory. 6024632Seric */ 6034632Seric 60425687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6054632Seric 6064632Seric /* 6074632Seric ** Convert the work list into canonical form. 6089377Seric ** Should be turning it into a list of envelopes here perhaps. 6094632Seric */ 6104632Seric 61124981Seric WorkQ = NULL; 61225687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6134632Seric { 6144632Seric w = (WORK *) xalloc(sizeof *w); 6154632Seric w->w_name = wlist[i].w_name; 6164632Seric w->w_pri = wlist[i].w_pri; 61725013Seric w->w_ctime = wlist[i].w_ctime; 61824981Seric w->w_next = WorkQ; 61924981Seric WorkQ = w; 6204632Seric } 6214632Seric 6227677Seric if (tTd(40, 1)) 6234632Seric { 6244632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6255037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6264632Seric } 62710070Seric 62810090Seric return (wn); 6294632Seric } 6304632Seric /* 6317677Seric ** WORKCMPF -- compare function for ordering work. 6324632Seric ** 6334632Seric ** Parameters: 6344632Seric ** a -- the first argument. 6354632Seric ** b -- the second argument. 6364632Seric ** 6374632Seric ** Returns: 63824981Seric ** -1 if a < b 63924981Seric ** 0 if a == b 64024981Seric ** +1 if a > b 6414632Seric ** 6424632Seric ** Side Effects: 6434632Seric ** none. 6444632Seric */ 6454632Seric 6464632Seric workcmpf(a, b) 6475037Seric register WORK *a; 6485037Seric register WORK *b; 6494632Seric { 65057438Seric long pa = a->w_pri; 65157438Seric long pb = b->w_pri; 65224941Seric 65324941Seric if (pa == pb) 6544632Seric return (0); 65524941Seric else if (pa > pb) 65624981Seric return (1); 65724981Seric else 65810121Seric return (-1); 6594632Seric } 6604632Seric /* 6614632Seric ** DOWORK -- do a work request. 6624632Seric ** 6634632Seric ** Parameters: 6644632Seric ** w -- the work request to be satisfied. 6654632Seric ** 6664632Seric ** Returns: 6674632Seric ** none. 6684632Seric ** 6694632Seric ** Side Effects: 6704632Seric ** The work request is satisfied if possible. 6714632Seric */ 6724632Seric 67355012Seric dowork(w, e) 6744632Seric register WORK *w; 67555012Seric register ENVELOPE *e; 6764632Seric { 6774632Seric register int i; 67824941Seric extern bool shouldqueue(); 67951920Seric extern bool readqf(); 6804632Seric 6817677Seric if (tTd(40, 1)) 6825037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 6834632Seric 6844632Seric /* 68524941Seric ** Ignore jobs that are too expensive for the moment. 6864632Seric */ 6874632Seric 68857438Seric if (shouldqueue(w->w_pri, w->w_ctime)) 6894632Seric { 69024941Seric if (Verbose) 69124967Seric printf("\nSkipping %s\n", w->w_name + 2); 6924632Seric return; 6934632Seric } 6944632Seric 69524941Seric /* 69624941Seric ** Fork for work. 69724941Seric */ 69824941Seric 69924941Seric if (ForkQueueRuns) 70024941Seric { 70124941Seric i = fork(); 70224941Seric if (i < 0) 70324941Seric { 70424941Seric syserr("dowork: cannot fork"); 70524941Seric return; 70624941Seric } 70724941Seric } 70824941Seric else 70924941Seric { 71024941Seric i = 0; 71124941Seric } 71224941Seric 7134632Seric if (i == 0) 7144632Seric { 7154632Seric /* 7164632Seric ** CHILD 7178148Seric ** Lock the control file to avoid duplicate deliveries. 7188148Seric ** Then run the file as though we had just read it. 7197350Seric ** We save an idea of the temporary name so we 7207350Seric ** can recover on interrupt. 7214632Seric */ 7224632Seric 7237763Seric /* set basic modes, etc. */ 7247356Seric (void) alarm(0); 72555012Seric clearenvelope(e, FALSE); 7264632Seric QueueRun = TRUE; 7279377Seric ErrorMode = EM_MAIL; 72855012Seric e->e_id = &w->w_name[2]; 7297876Seric # ifdef LOG 73055360Seric if (LogLevel > 12) 73155012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7327881Seric getpid()); 73356795Seric # endif /* LOG */ 7347763Seric 7357763Seric /* don't use the headers from sendmail.cf... */ 73655012Seric e->e_header = NULL; 7377763Seric 73851920Seric /* read the queue control file -- return if locked */ 73955012Seric if (!readqf(e)) 7406980Seric { 74124941Seric if (ForkQueueRuns) 74224941Seric exit(EX_OK); 74324941Seric else 74424941Seric return; 7456980Seric } 7466980Seric 74755012Seric e->e_flags |= EF_INQUEUE; 74857642Seric eatheader(e, TRUE); 7496980Seric 7506980Seric /* do the delivery */ 75155012Seric if (!bitset(EF_FATALERRS, e->e_flags)) 75255012Seric sendall(e, SM_DELIVER); 7536980Seric 7546980Seric /* finish up and exit */ 75524941Seric if (ForkQueueRuns) 75624941Seric finis(); 75724941Seric else 75855012Seric dropenvelope(e); 7594632Seric } 76024941Seric else 76124941Seric { 76224941Seric /* 76324941Seric ** Parent -- pick up results. 76424941Seric */ 7654632Seric 76624941Seric errno = 0; 76724941Seric (void) waitfor(i); 76824941Seric } 7694632Seric } 7704632Seric /* 7714632Seric ** READQF -- read queue file and set up environment. 7724632Seric ** 7734632Seric ** Parameters: 7749377Seric ** e -- the envelope of the job to run. 7754632Seric ** 7764632Seric ** Returns: 77751920Seric ** TRUE if it successfully read the queue file. 77851920Seric ** FALSE otherwise. 7794632Seric ** 7804632Seric ** Side Effects: 78151920Seric ** The queue file is returned locked. 7824632Seric */ 7834632Seric 78451920Seric bool 78551920Seric readqf(e) 7869377Seric register ENVELOPE *e; 7874632Seric { 78817477Seric char *qf; 78917477Seric register FILE *qfp; 79054974Seric ADDRESS *ctladdr; 79156400Seric struct stat st; 79257135Seric char *bp; 79357135Seric char buf[MAXLINE]; 7949348Seric extern char *fgetfolded(); 79524954Seric extern long atol(); 79654974Seric extern ADDRESS *setctluser(); 79751937Seric # ifdef LOCKF 79851937Seric struct flock lfd; 79951937Seric # endif 8004632Seric 8014632Seric /* 80217468Seric ** Read and process the file. 8034632Seric */ 8044632Seric 80517477Seric qf = queuename(e, 'q'); 80651937Seric qfp = fopen(qf, "r+"); 80717477Seric if (qfp == NULL) 80817477Seric { 80940934Srick if (errno != ENOENT) 81040934Srick syserr("readqf: no control file %s", qf); 81151920Seric return FALSE; 81217477Seric } 81340934Srick 81456400Seric /* 81556400Seric ** Check the queue file for plausibility to avoid attacks. 81656400Seric */ 81756400Seric 81856400Seric if (fstat(fileno(qfp), &st) < 0) 81956400Seric { 82056400Seric /* must have been being processed by someone else */ 82156400Seric fclose(qfp); 82256400Seric return FALSE; 82356400Seric } 82456400Seric 82557135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 82656400Seric { 82756400Seric # ifdef LOG 82856400Seric if (LogLevel > 0) 82956400Seric { 83056400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 83156400Seric e->e_id, st.st_uid, st.st_mode); 83256400Seric } 83356795Seric # endif /* LOG */ 83456400Seric fclose(qfp); 83556400Seric return FALSE; 83656400Seric } 83756400Seric 83851835Seric # ifdef LOCKF 83951937Seric lfd.l_type = F_WRLCK; 84051937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 84151937Seric if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 84251835Seric # else 84340934Srick if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 84451835Seric # endif 84540934Srick { 84640934Srick /* being processed by another queuer */ 84740934Srick if (Verbose) 84855012Seric printf("%s: locked\n", e->e_id); 84951920Seric # ifdef LOG 85055173Seric if (LogLevel > 10) 85155012Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 85256795Seric # endif /* LOG */ 85340934Srick (void) fclose(qfp); 85451920Seric return FALSE; 85540934Srick } 85640934Srick 85751920Seric /* save this lock */ 85851920Seric e->e_lockfp = qfp; 85951920Seric 86040934Srick /* do basic system initialization */ 86155012Seric initsys(e); 86240934Srick 86317477Seric FileName = qf; 8649377Seric LineNumber = 0; 86551920Seric if (Verbose) 8669377Seric printf("\nRunning %s\n", e->e_id); 86754974Seric ctladdr = NULL; 86857135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 8694632Seric { 87057529Seric struct stat st; 87157529Seric 87226504Seric if (tTd(40, 4)) 87357135Seric printf("+++++ %s\n", bp); 87457135Seric switch (bp[0]) 8754632Seric { 87640973Sbostic case 'C': /* specify controlling user */ 87757135Seric ctladdr = setctluser(&bp[1]); 87840973Sbostic break; 87940973Sbostic 8804632Seric case 'R': /* specify recipient */ 88157135Seric sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 8824632Seric break; 8834632Seric 88425687Seric case 'E': /* specify error recipient */ 88557135Seric sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 88625687Seric break; 88725687Seric 8884632Seric case 'H': /* header */ 88957135Seric (void) chompheader(&bp[1], FALSE, e); 8904632Seric break; 8914632Seric 89210108Seric case 'M': /* message */ 89357135Seric e->e_message = newstr(&bp[1]); 89410108Seric break; 89510108Seric 8964632Seric case 'S': /* sender */ 89757135Seric setsender(newstr(&bp[1]), e); 8984632Seric break; 8994632Seric 9004632Seric case 'D': /* data file name */ 90157135Seric e->e_df = newstr(&bp[1]); 9029544Seric e->e_dfp = fopen(e->e_df, "r"); 9039544Seric if (e->e_dfp == NULL) 9049377Seric syserr("readqf: cannot open %s", e->e_df); 90557529Seric if (fstat(fileno(e->e_dfp), &st) >= 0) 90657529Seric e->e_msgsize = st.st_size; 9074632Seric break; 9084632Seric 9097860Seric case 'T': /* init time */ 91057135Seric e->e_ctime = atol(&bp[1]); 9114632Seric break; 9124632Seric 9134634Seric case 'P': /* message priority */ 91457135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9154634Seric break; 9164634Seric 91753400Seric case '$': /* define macro */ 91857135Seric define(bp[1], newstr(&bp[2]), e); 91953400Seric break; 92053400Seric 92124941Seric case '\0': /* blank line; ignore */ 92224941Seric break; 92324941Seric 9244632Seric default: 92524941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 92657135Seric LineNumber, bp); 9274632Seric break; 9284632Seric } 92957135Seric 93057135Seric if (bp != buf) 93157135Seric free(bp); 9324632Seric } 9339377Seric 9349377Seric FileName = NULL; 93524941Seric 93624941Seric /* 93724941Seric ** If we haven't read any lines, this queue file is empty. 93824941Seric ** Arrange to remove it without referencing any null pointers. 93924941Seric */ 94024941Seric 94124941Seric if (LineNumber == 0) 94224941Seric { 94324941Seric errno = 0; 94424941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 94524941Seric } 94651920Seric return TRUE; 9474632Seric } 9484632Seric /* 9499630Seric ** PRINTQUEUE -- print out a representation of the mail queue 9509630Seric ** 9519630Seric ** Parameters: 9529630Seric ** none. 9539630Seric ** 9549630Seric ** Returns: 9559630Seric ** none. 9569630Seric ** 9579630Seric ** Side Effects: 9589630Seric ** Prints a listing of the mail queue on the standard output. 9599630Seric */ 9605182Seric 9619630Seric printqueue() 9629630Seric { 9639630Seric register WORK *w; 9649630Seric FILE *f; 96510070Seric int nrequests; 9669630Seric char buf[MAXLINE]; 9679630Seric 9689630Seric /* 9699630Seric ** Read and order the queue. 9709630Seric */ 9719630Seric 97224941Seric nrequests = orderq(TRUE); 9739630Seric 9749630Seric /* 9759630Seric ** Print the work list that we have read. 9769630Seric */ 9779630Seric 9789630Seric /* first see if there is anything */ 97910070Seric if (nrequests <= 0) 9809630Seric { 98110070Seric printf("Mail queue is empty\n"); 9829630Seric return; 9839630Seric } 9849630Seric 98551920Seric CurrentLA = getla(); /* get load average */ 98640934Srick 98710096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 98825687Seric if (nrequests > QUEUESIZE) 98925687Seric printf(", only %d printed", QUEUESIZE); 99024979Seric if (Verbose) 99125032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 99224979Seric else 99324979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 9949630Seric for (w = WorkQ; w != NULL; w = w->w_next) 9959630Seric { 9969630Seric struct stat st; 99710070Seric auto time_t submittime = 0; 99810070Seric long dfsize = -1; 99910108Seric char message[MAXLINE]; 100051937Seric # ifdef LOCKF 100151937Seric struct flock lfd; 100251937Seric # endif 100324941Seric extern bool shouldqueue(); 10049630Seric 100517468Seric f = fopen(w->w_name, "r"); 100617468Seric if (f == NULL) 100717468Seric { 100817468Seric errno = 0; 100917468Seric continue; 101017468Seric } 10119630Seric printf("%7s", w->w_name + 2); 101251835Seric # ifdef LOCKF 101351937Seric lfd.l_type = F_RDLCK; 101451937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 101551937Seric if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 101651835Seric # else 101740934Srick if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 101851835Seric # endif 101910070Seric printf("*"); 102057438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 102124941Seric printf("X"); 102210070Seric else 102310070Seric printf(" "); 102410070Seric errno = 0; 102517468Seric 102610108Seric message[0] = '\0'; 10279630Seric while (fgets(buf, sizeof buf, f) != NULL) 10289630Seric { 102953400Seric register int i; 103053400Seric 10319630Seric fixcrlf(buf, TRUE); 10329630Seric switch (buf[0]) 10339630Seric { 103410108Seric case 'M': /* error message */ 103553400Seric if ((i = strlen(&buf[1])) >= sizeof message) 103653400Seric i = sizeof message; 103753400Seric bcopy(&buf[1], message, i); 103853400Seric message[i] = '\0'; 103910108Seric break; 104010108Seric 10419630Seric case 'S': /* sender name */ 104224979Seric if (Verbose) 104325027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 104425027Seric w->w_pri, ctime(&submittime) + 4, 104524979Seric &buf[1]); 104624979Seric else 104724979Seric printf("%8ld %.16s %.45s", dfsize, 104824979Seric ctime(&submittime), &buf[1]); 104910108Seric if (message[0] != '\0') 105025027Seric printf("\n\t\t (%.60s)", message); 10519630Seric break; 105251920Seric 105340973Sbostic case 'C': /* controlling user */ 105454974Seric if (Verbose) 105554975Seric printf("\n\t\t\t\t (---%.34s---)", &buf[1]); 105640973Sbostic break; 10579630Seric 10589630Seric case 'R': /* recipient name */ 105924979Seric if (Verbose) 106025027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 106124979Seric else 106224979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 10639630Seric break; 10649630Seric 10659630Seric case 'T': /* creation time */ 106624941Seric submittime = atol(&buf[1]); 10679630Seric break; 106810070Seric 106910070Seric case 'D': /* data file name */ 107010070Seric if (stat(&buf[1], &st) >= 0) 107110070Seric dfsize = st.st_size; 107210070Seric break; 10739630Seric } 10749630Seric } 107510070Seric if (submittime == (time_t) 0) 107610070Seric printf(" (no control file)"); 10779630Seric printf("\n"); 107823098Seric (void) fclose(f); 10799630Seric } 10809630Seric } 10819630Seric 108256795Seric # endif /* QUEUE */ 108317468Seric /* 108417468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 108517468Seric ** 108617468Seric ** Assigns an id code if one does not already exist. 108717468Seric ** This code is very careful to avoid trashing existing files 108817468Seric ** under any circumstances. 108917468Seric ** 109017468Seric ** Parameters: 109117468Seric ** e -- envelope to build it in/from. 109217468Seric ** type -- the file type, used as the first character 109317468Seric ** of the file name. 109417468Seric ** 109517468Seric ** Returns: 109617468Seric ** a pointer to the new file name (in a static buffer). 109717468Seric ** 109817468Seric ** Side Effects: 109951920Seric ** If no id code is already assigned, queuename will 110051920Seric ** assign an id code, create a qf file, and leave a 110151920Seric ** locked, open-for-write file pointer in the envelope. 110217468Seric */ 110317468Seric 110417468Seric char * 110517468Seric queuename(e, type) 110617468Seric register ENVELOPE *e; 110717468Seric char type; 110817468Seric { 110917468Seric static char buf[MAXNAME]; 111017468Seric static int pid = -1; 111117468Seric char c1 = 'A'; 111217468Seric char c2 = 'A'; 111317468Seric 111417468Seric if (e->e_id == NULL) 111517468Seric { 111617468Seric char qf[20]; 111717468Seric 111817468Seric /* find a unique id */ 111917468Seric if (pid != getpid()) 112017468Seric { 112117468Seric /* new process -- start back at "AA" */ 112217468Seric pid = getpid(); 112317468Seric c1 = 'A'; 112417468Seric c2 = 'A' - 1; 112517468Seric } 112617468Seric (void) sprintf(qf, "qfAA%05d", pid); 112717468Seric 112817468Seric while (c1 < '~' || c2 < 'Z') 112917468Seric { 113017468Seric int i; 113151937Seric # ifdef LOCKF 113251937Seric struct flock lfd; 113351937Seric # endif 113417468Seric 113517468Seric if (c2 >= 'Z') 113617468Seric { 113717468Seric c1++; 113817468Seric c2 = 'A' - 1; 113917468Seric } 114040934Srick qf[2] = c1; 114140934Srick qf[3] = ++c2; 114217468Seric if (tTd(7, 20)) 114340934Srick printf("queuename: trying \"%s\"\n", qf); 114417468Seric 114540934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 114651920Seric if (i < 0) 114751920Seric { 114851920Seric if (errno == EEXIST) 114951920Seric continue; 115051920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 115151920Seric qf, QueueDir); 115251920Seric exit(EX_UNAVAILABLE); 115351920Seric } 115451920Seric # ifdef LOCKF 115551937Seric lfd.l_type = F_WRLCK; 115651937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 115751937Seric if (fcntl(i, F_SETLK, &lfd) >= 0) 115851920Seric # else 115951920Seric if (flock(i, LOCK_EX|LOCK_NB) >= 0) 116051920Seric # endif 116151920Seric { 116251920Seric e->e_lockfp = fdopen(i, "w"); 116340934Srick break; 116417468Seric } 116551920Seric 116651920Seric /* a reader got the file; abandon it and try again */ 116751920Seric (void) close(i); 116817468Seric } 116917468Seric if (c1 >= '~' && c2 >= 'Z') 117017468Seric { 117117468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 117217468Seric qf, QueueDir); 117317468Seric exit(EX_OSERR); 117417468Seric } 117517468Seric e->e_id = newstr(&qf[2]); 117617468Seric define('i', e->e_id, e); 117717468Seric if (tTd(7, 1)) 117817468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 117917468Seric # ifdef LOG 118017468Seric if (LogLevel > 16) 118117468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 118256795Seric # endif /* LOG */ 118317468Seric } 118417468Seric 118517468Seric if (type == '\0') 118617468Seric return (NULL); 118717468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 118817468Seric if (tTd(7, 2)) 118917468Seric printf("queuename: %s\n", buf); 119017468Seric return (buf); 119117468Seric } 119217468Seric /* 119317468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 119417468Seric ** 119517468Seric ** Parameters: 119617468Seric ** e -- the envelope to unlock. 119717468Seric ** 119817468Seric ** Returns: 119917468Seric ** none 120017468Seric ** 120117468Seric ** Side Effects: 120217468Seric ** unlocks the queue for `e'. 120317468Seric */ 120417468Seric 120517468Seric unlockqueue(e) 120617468Seric ENVELOPE *e; 120717468Seric { 120851920Seric /* if there is a lock file in the envelope, close it */ 120951920Seric if (e->e_lockfp != NULL) 121051920Seric fclose(e->e_lockfp); 121151920Seric e->e_lockfp = NULL; 121251920Seric 121317468Seric /* remove the transcript */ 121417468Seric # ifdef LOG 121517468Seric if (LogLevel > 19) 121617468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 121756795Seric # endif /* LOG */ 121817468Seric if (!tTd(51, 4)) 121917468Seric xunlink(queuename(e, 'x')); 122017468Seric 122117468Seric } 122240973Sbostic /* 122354974Seric ** SETCTLUSER -- create a controlling address 122440973Sbostic ** 122554974Seric ** Create a fake "address" given only a local login name; this is 122654974Seric ** used as a "controlling user" for future recipient addresses. 122740973Sbostic ** 122840973Sbostic ** Parameters: 122954974Seric ** user -- the user name of the controlling user. 123040973Sbostic ** 123140973Sbostic ** Returns: 123254974Seric ** An address descriptor for the controlling user. 123340973Sbostic ** 123440973Sbostic ** Side Effects: 123540973Sbostic ** none. 123640973Sbostic */ 123740973Sbostic 123854974Seric ADDRESS * 123954974Seric setctluser(user) 124054974Seric char *user; 124140973Sbostic { 124254974Seric register ADDRESS *a; 124340973Sbostic struct passwd *pw; 124440973Sbostic 124540973Sbostic /* 124654974Seric ** See if this clears our concept of controlling user. 124740973Sbostic */ 124840973Sbostic 124954974Seric if (user == NULL || *user == '\0') 125054974Seric return NULL; 125140973Sbostic 125240973Sbostic /* 125354974Seric ** Set up addr fields for controlling user. 125440973Sbostic */ 125540973Sbostic 125654974Seric a = (ADDRESS *) xalloc(sizeof *a); 125754974Seric bzero((char *) a, sizeof *a); 125854974Seric if ((pw = getpwnam(user)) != NULL) 125940973Sbostic { 126040973Sbostic a->q_home = newstr(pw->pw_dir); 126140973Sbostic a->q_uid = pw->pw_uid; 126240973Sbostic a->q_gid = pw->pw_gid; 126357642Seric a->q_user = newstr(user); 126440973Sbostic } 126540973Sbostic else 126640973Sbostic { 126740973Sbostic a->q_uid = DefUid; 126840973Sbostic a->q_gid = DefGid; 126957642Seric a->q_user = newstr(DefUser); 127040973Sbostic } 127140973Sbostic 127240973Sbostic a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 127356328Seric a->q_mailer = LocalMailer; 127454974Seric return a; 127540973Sbostic } 1276