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*57977Seric static char sccsid[] = "@(#)queue.c 6.10 (Berkeley) 02/14/93 (with queueing)"; 1433731Sbostic #else 15*57977Seric static char sccsid[] = "@(#)queue.c 6.10 (Berkeley) 02/14/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 2157948Seric # include <sys/file.h> 224634Seric # include <signal.h> 234632Seric # include <errno.h> 2440973Sbostic # include <pwd.h> 2551937Seric # include <fcntl.h> 26*57977Seric # ifndef MAXNAMLEN 2757736Seric # include <dirent.h> 2857736Seric # endif 294632Seric 3033731Sbostic # ifdef QUEUE 314632Seric 324632Seric /* 339377Seric ** Work queue. 349377Seric */ 359377Seric 369377Seric struct work 379377Seric { 389377Seric char *w_name; /* name of control file */ 399377Seric long w_pri; /* priority of message, see below */ 4025013Seric time_t w_ctime; /* creation time of message */ 419377Seric struct work *w_next; /* next in queue */ 429377Seric }; 439377Seric 449377Seric typedef struct work WORK; 459377Seric 469377Seric WORK *WorkQ; /* queue of things to be done */ 479377Seric /* 484632Seric ** QUEUEUP -- queue a message up for future transmission. 494632Seric ** 504632Seric ** Parameters: 516980Seric ** e -- the envelope to queue up. 526999Seric ** queueall -- if TRUE, queue all addresses, rather than 536999Seric ** just those with the QQUEUEUP flag set. 549377Seric ** announce -- if TRUE, tell when you are queueing up. 554632Seric ** 564632Seric ** Returns: 5751920Seric ** none. 584632Seric ** 594632Seric ** Side Effects: 609377Seric ** The current request are saved in a control file. 6151920Seric ** The queue file is left locked. 624632Seric */ 634632Seric 649377Seric queueup(e, queueall, announce) 656980Seric register ENVELOPE *e; 666999Seric bool queueall; 679377Seric bool announce; 684632Seric { 697812Seric char *qf; 707812Seric register FILE *tfp; 714632Seric register HDR *h; 725007Seric register ADDRESS *q; 7351920Seric int fd; 7451920Seric int i; 7551920Seric bool newid; 7653400Seric register char *p; 7710173Seric MAILER nullmailer; 7854974Seric ADDRESS *lastctladdr; 7954975Seric static ADDRESS *nullctladdr = NULL; 8051920Seric char buf[MAXLINE], tf[MAXLINE]; 8153400Seric extern char *macvalue(); 8254974Seric extern ADDRESS *getctladdr(); 834632Seric 845037Seric /* 8554975Seric ** If we don't have nullctladdr, create one 8654975Seric */ 8754975Seric 8854975Seric if (nullctladdr == NULL) 8954975Seric { 9054975Seric nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr); 9154975Seric bzero((char *) nullctladdr, sizeof nullctladdr); 9254975Seric } 9354975Seric 9454975Seric /* 9517477Seric ** Create control file. 965037Seric */ 974632Seric 9851920Seric newid = (e->e_id == NULL); 9951920Seric strcpy(tf, queuename(e, 't')); 10051920Seric tfp = e->e_lockfp; 10151920Seric if (tfp == NULL) 10251920Seric newid = FALSE; 10351920Seric if (newid) 10451835Seric { 10551920Seric tfp = e->e_lockfp; 10651920Seric } 10751920Seric else 10851920Seric { 10951920Seric /* get a locked tf file */ 11051920Seric for (i = 100; --i >= 0; ) 11151835Seric { 11251937Seric # ifdef LOCKF 11351937Seric struct flock lfd; 11451937Seric # endif 11551937Seric 11651920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 11751920Seric if (fd < 0) 11851835Seric { 11951920Seric if (errno == EEXIST) 12051920Seric continue; 12151920Seric syserr("queueup: cannot create temp file %s", tf); 12251920Seric return; 12341636Srick } 12451835Seric # ifdef LOCKF 12551937Seric lfd.l_type = F_WRLCK; 12651937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 12751937Seric if (fcntl(fd, F_SETLK, &lfd) >= 0) 12851920Seric break; 12951920Seric if (errno != EACCES && errno != EAGAIN) 13051920Seric syserr("cannot lockf(%s)", tf); 13151835Seric # else 13251920Seric if (flock(fd, LOCK_EX|LOCK_NB) >= 0) 13351920Seric break; 13451920Seric if (errno != EWOULDBLOCK) 13551920Seric syserr("cannot flock(%s)", tf); 13651835Seric # endif 13751920Seric close(fd); 13841636Srick } 13941636Srick 14051920Seric tfp = fdopen(fd, "w"); 14151920Seric } 1424632Seric 1437677Seric if (tTd(40, 1)) 14417468Seric printf("queueing %s\n", e->e_id); 1454632Seric 1464632Seric /* 1476980Seric ** If there is no data file yet, create one. 1486980Seric */ 1496980Seric 1506980Seric if (e->e_df == NULL) 1516980Seric { 1526980Seric register FILE *dfp; 1539389Seric extern putbody(); 1546980Seric 1557812Seric e->e_df = newstr(queuename(e, 'd')); 15640934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 15740934Srick if (fd < 0) 1586980Seric { 1596980Seric syserr("queueup: cannot create %s", e->e_df); 16051920Seric if (!newid) 16151920Seric (void) fclose(tfp); 16251920Seric return; 1636980Seric } 16440934Srick dfp = fdopen(fd, "w"); 16510173Seric (*e->e_putbody)(dfp, ProgMailer, e); 1667009Seric (void) fclose(dfp); 1679389Seric e->e_putbody = putbody; 1686980Seric } 1696980Seric 1706980Seric /* 1714632Seric ** Output future work requests. 17225687Seric ** Priority and creation time should be first, since 17325687Seric ** they are required by orderq. 1744632Seric */ 1754632Seric 1769377Seric /* output message priority */ 1779377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1789377Seric 1799630Seric /* output creation time */ 1809630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1819630Seric 1824632Seric /* output name of data file */ 1837812Seric fprintf(tfp, "D%s\n", e->e_df); 1844632Seric 18510108Seric /* message from envelope, if it exists */ 18610108Seric if (e->e_message != NULL) 18710108Seric fprintf(tfp, "M%s\n", e->e_message); 18810108Seric 18953400Seric /* $r and $s macro values */ 19053400Seric if ((p = macvalue('r', e)) != NULL) 19153400Seric fprintf(tfp, "$r%s\n", p); 19253400Seric if ((p = macvalue('s', e)) != NULL) 19353400Seric fprintf(tfp, "$s%s\n", p); 19453400Seric 1954632Seric /* output name of sender */ 1967812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1974632Seric 19855360Seric /* output list of error recipients */ 19955360Seric lastctladdr = NULL; 20055360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 20155360Seric { 20255360Seric if (!bitset(QDONTSEND, q->q_flags)) 20355360Seric { 20455360Seric ADDRESS *ctladdr; 20555360Seric 20655360Seric ctladdr = getctladdr(q); 20755360Seric if (ctladdr == NULL && q->q_alias != NULL) 20855360Seric ctladdr = nullctladdr; 20955360Seric if (ctladdr != lastctladdr) 21055360Seric { 21155360Seric printctladdr(ctladdr, tfp); 21255360Seric lastctladdr = ctladdr; 21355360Seric } 21455360Seric fprintf(tfp, "E%s\n", q->q_paddr); 21555360Seric } 21655360Seric } 21755360Seric 2184632Seric /* output list of recipient addresses */ 2196980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2204632Seric { 22147284Seric if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) : 2227763Seric bitset(QQUEUEUP, q->q_flags)) 2238245Seric { 22454974Seric ADDRESS *ctladdr; 22540973Sbostic 22654975Seric ctladdr = getctladdr(q); 22754975Seric if (ctladdr == NULL && q->q_alias != NULL) 22854975Seric ctladdr = nullctladdr; 22954975Seric if (ctladdr != lastctladdr) 23054974Seric { 23154974Seric printctladdr(ctladdr, tfp); 23254974Seric lastctladdr = ctladdr; 23354974Seric } 2347812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2359377Seric if (announce) 2369377Seric { 2379377Seric e->e_to = q->q_paddr; 2389377Seric message(Arpa_Info, "queued"); 2399377Seric if (LogLevel > 4) 24054967Seric logdelivery("queued", e); 2419377Seric e->e_to = NULL; 2429377Seric } 2439387Seric if (tTd(40, 1)) 2449387Seric { 2459387Seric printf("queueing "); 2469387Seric printaddr(q, FALSE); 2479387Seric } 2488245Seric } 2494632Seric } 2504632Seric 2519377Seric /* 2529377Seric ** Output headers for this message. 2539377Seric ** Expand macros completely here. Queue run will deal with 2549377Seric ** everything as absolute headers. 2559377Seric ** All headers that must be relative to the recipient 2569377Seric ** can be cracked later. 25710173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 25810173Seric ** no effect on the addresses as they are output. 2599377Seric */ 2609377Seric 26110686Seric bzero((char *) &nullmailer, sizeof nullmailer); 26210173Seric nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 26310349Seric nullmailer.m_eol = "\n"; 26410173Seric 26516147Seric define('g', "\001f", e); 2666980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2674632Seric { 26810686Seric extern bool bitzerop(); 26910686Seric 27012015Seric /* don't output null headers */ 2714632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2724632Seric continue; 27312015Seric 27412015Seric /* don't output resent headers on non-resent messages */ 27512015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 27612015Seric continue; 27712015Seric 27812015Seric /* output this header */ 2797812Seric fprintf(tfp, "H"); 28012015Seric 28112015Seric /* if conditional, output the set of conditions */ 28210686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 28310686Seric { 28410686Seric int j; 28510686Seric 28623098Seric (void) putc('?', tfp); 28710686Seric for (j = '\0'; j <= '\177'; j++) 28810686Seric if (bitnset(j, h->h_mflags)) 28923098Seric (void) putc(j, tfp); 29023098Seric (void) putc('?', tfp); 29110686Seric } 29212015Seric 29312015Seric /* output the header: expand macros, convert addresses */ 2947763Seric if (bitset(H_DEFAULT, h->h_flags)) 2957763Seric { 2967763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2978236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2987763Seric } 2998245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 3009348Seric { 30155012Seric commaize(h, h->h_value, tfp, 30255012Seric bitset(EF_OLDSTYLE, e->e_flags), 30355012Seric &nullmailer, e); 3049348Seric } 3057763Seric else 3068245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 3074632Seric } 3084632Seric 3094632Seric /* 3104632Seric ** Clean up. 3114632Seric */ 3124632Seric 31351920Seric if (!newid) 31451920Seric { 31551920Seric qf = queuename(e, 'q'); 31651920Seric if (rename(tf, qf) < 0) 31751920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31851920Seric if (e->e_lockfp != NULL) 31951920Seric (void) fclose(e->e_lockfp); 32051920Seric e->e_lockfp = tfp; 32151920Seric } 32251920Seric else 32351920Seric qf = tf; 32441636Srick errno = 0; 3257391Seric 3267677Seric # ifdef LOG 3277677Seric /* save log info */ 3287878Seric if (LogLevel > 15) 3297878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 33056795Seric # endif /* LOG */ 33140934Srick fflush(tfp); 33251920Seric return; 3334632Seric } 33454974Seric 33554974Seric printctladdr(a, tfp) 33654974Seric ADDRESS *a; 33754974Seric FILE *tfp; 33854974Seric { 33954974Seric char *u; 34054974Seric struct passwd *pw; 34154974Seric extern struct passwd *getpwuid(); 34254974Seric 34354974Seric if (a == NULL) 34454974Seric { 34554974Seric fprintf(tfp, "C\n"); 34654974Seric return; 34754974Seric } 34854974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 34954974Seric u = DefUser; 35054974Seric else 35154974Seric u = pw->pw_name; 35254974Seric fprintf(tfp, "C%s\n", u); 35354974Seric } 35454974Seric 3554632Seric /* 3564632Seric ** RUNQUEUE -- run the jobs in the queue. 3574632Seric ** 3584632Seric ** Gets the stuff out of the queue in some presumably logical 3594632Seric ** order and processes them. 3604632Seric ** 3614632Seric ** Parameters: 36224941Seric ** forkflag -- TRUE if the queue scanning should be done in 36324941Seric ** a child process. We double-fork so it is not our 36424941Seric ** child and we don't have to clean up after it. 3654632Seric ** 3664632Seric ** Returns: 3674632Seric ** none. 3684632Seric ** 3694632Seric ** Side Effects: 3704632Seric ** runs things in the mail queue. 3714632Seric */ 3724632Seric 37355360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 37455360Seric 37555360Seric runqueue(forkflag) 3764639Seric bool forkflag; 3774632Seric { 37824953Seric extern bool shouldqueue(); 37955360Seric register ENVELOPE *e; 38055360Seric extern ENVELOPE BlankEnvelope; 38155360Seric extern ENVELOPE *newenvelope(); 38224953Seric 3837466Seric /* 38424953Seric ** If no work will ever be selected, don't even bother reading 38524953Seric ** the queue. 38624953Seric */ 38724953Seric 38851920Seric CurrentLA = getla(); /* get load average */ 38940934Srick 39057438Seric if (shouldqueue(-100000000L, curtime())) 39124953Seric { 39224953Seric if (Verbose) 39324953Seric printf("Skipping queue run -- load average too high\n"); 39455360Seric return; 39524953Seric } 39624953Seric 39724953Seric /* 3987466Seric ** See if we want to go off and do other useful work. 3997466Seric */ 4004639Seric 4014639Seric if (forkflag) 4024639Seric { 4037943Seric int pid; 4047943Seric 4057943Seric pid = dofork(); 4067943Seric if (pid != 0) 4074639Seric { 40846928Sbostic extern void reapchild(); 40925184Seric 4107943Seric /* parent -- pick up intermediate zombie */ 41125184Seric #ifndef SIGCHLD 4129377Seric (void) waitfor(pid); 41356795Seric #else /* SIGCHLD */ 41425184Seric (void) signal(SIGCHLD, reapchild); 41556795Seric #endif /* SIGCHLD */ 4167690Seric if (QueueIntvl != 0) 4179348Seric (void) setevent(QueueIntvl, runqueue, TRUE); 4184639Seric return; 4194639Seric } 4207943Seric /* child -- double fork */ 42125184Seric #ifndef SIGCHLD 4227943Seric if (fork() != 0) 4237943Seric exit(EX_OK); 42456795Seric #else /* SIGCHLD */ 42525184Seric (void) signal(SIGCHLD, SIG_DFL); 42656795Seric #endif /* SIGCHLD */ 4274639Seric } 42824941Seric 42940934Srick setproctitle("running queue: %s", QueueDir); 43057731Seric ForceMail = TRUE; 43124941Seric 4327876Seric # ifdef LOG 4337876Seric if (LogLevel > 11) 43455360Seric syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 43555360Seric QueueDir, getpid(), forkflag); 43656795Seric # endif /* LOG */ 4374639Seric 4387466Seric /* 43910205Seric ** Release any resources used by the daemon code. 44010205Seric */ 44110205Seric 44210205Seric # ifdef DAEMON 44310205Seric clrdaemon(); 44456795Seric # endif /* DAEMON */ 44510205Seric 44610205Seric /* 44755360Seric ** Create ourselves an envelope 44855360Seric */ 44955360Seric 45055360Seric CurEnv = &QueueEnvelope; 45155360Seric e = newenvelope(&QueueEnvelope); 45255360Seric e->e_flags = BlankEnvelope.e_flags; 45355360Seric 45455360Seric /* 45527175Seric ** Make sure the alias database is open. 45627175Seric */ 45727175Seric 45855012Seric initaliases(AliasFile, FALSE, e); 45927175Seric 46027175Seric /* 4617466Seric ** Start making passes through the queue. 4627466Seric ** First, read and sort the entire queue. 4637466Seric ** Then, process the work in that order. 4647466Seric ** But if you take too long, start over. 4657466Seric */ 4667466Seric 4677943Seric /* order the existing work requests */ 46824954Seric (void) orderq(FALSE); 4697690Seric 4707943Seric /* process them once at a time */ 4717943Seric while (WorkQ != NULL) 4724639Seric { 4737943Seric WORK *w = WorkQ; 4747881Seric 4757943Seric WorkQ = WorkQ->w_next; 47655012Seric dowork(w, e); 4777943Seric free(w->w_name); 4787943Seric free((char *) w); 4794639Seric } 48029866Seric 48129866Seric /* exit without the usual cleanup */ 48255467Seric e->e_id = NULL; 48355467Seric finis(); 4844634Seric } 4854634Seric /* 4864632Seric ** ORDERQ -- order the work queue. 4874632Seric ** 4884632Seric ** Parameters: 48924941Seric ** doall -- if set, include everything in the queue (even 49024941Seric ** the jobs that cannot be run because the load 49124941Seric ** average is too high). Otherwise, exclude those 49224941Seric ** jobs. 4934632Seric ** 4944632Seric ** Returns: 49510121Seric ** The number of request in the queue (not necessarily 49610121Seric ** the number of requests in WorkQ however). 4974632Seric ** 4984632Seric ** Side Effects: 4994632Seric ** Sets WorkQ to the queue of available work, in order. 5004632Seric */ 5014632Seric 50225687Seric # define NEED_P 001 50325687Seric # define NEED_T 002 5044632Seric 50524941Seric orderq(doall) 50624941Seric bool doall; 5074632Seric { 5086625Sglickman register struct direct *d; 5094632Seric register WORK *w; 5106625Sglickman DIR *f; 5114632Seric register int i; 51225687Seric WORK wlist[QUEUESIZE+1]; 51310070Seric int wn = -1; 5144632Seric extern workcmpf(); 5154632Seric 5164632Seric /* clear out old WorkQ */ 5174632Seric for (w = WorkQ; w != NULL; ) 5184632Seric { 5194632Seric register WORK *nw = w->w_next; 5204632Seric 5214632Seric WorkQ = nw; 5224632Seric free(w->w_name); 5234632Seric free((char *) w); 5244632Seric w = nw; 5254632Seric } 5264632Seric 5274632Seric /* open the queue directory */ 5288148Seric f = opendir("."); 5294632Seric if (f == NULL) 5304632Seric { 5318148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 53210070Seric return (0); 5334632Seric } 5344632Seric 5354632Seric /* 5364632Seric ** Read the work directory. 5374632Seric */ 5384632Seric 53910070Seric while ((d = readdir(f)) != NULL) 5404632Seric { 5419377Seric FILE *cf; 5424632Seric char lbuf[MAXNAME]; 54357642Seric extern bool shouldqueue(); 5444632Seric 5454632Seric /* is this an interesting entry? */ 5467812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5474632Seric continue; 5484632Seric 54910070Seric /* yes -- open control file (if not too many files) */ 55025687Seric if (++wn >= QUEUESIZE) 55110070Seric continue; 5528148Seric cf = fopen(d->d_name, "r"); 5534632Seric if (cf == NULL) 5544632Seric { 5557055Seric /* this may be some random person sending hir msgs */ 5567055Seric /* syserr("orderq: cannot open %s", cbuf); */ 55710090Seric if (tTd(41, 2)) 55810090Seric printf("orderq: cannot open %s (%d)\n", 55910090Seric d->d_name, errno); 5607055Seric errno = 0; 56110090Seric wn--; 5624632Seric continue; 5634632Seric } 56425687Seric w = &wlist[wn]; 56525687Seric w->w_name = newstr(d->d_name); 5664632Seric 56725027Seric /* make sure jobs in creation don't clog queue */ 56825687Seric w->w_pri = 0x7fffffff; 56925687Seric w->w_ctime = 0; 57025027Seric 5714632Seric /* extract useful information */ 57225687Seric i = NEED_P | NEED_T; 57325687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 5744632Seric { 57524954Seric extern long atol(); 57624954Seric 57724941Seric switch (lbuf[0]) 5784632Seric { 57924941Seric case 'P': 58025687Seric w->w_pri = atol(&lbuf[1]); 58125687Seric i &= ~NEED_P; 5824632Seric break; 58325013Seric 58425013Seric case 'T': 58525687Seric w->w_ctime = atol(&lbuf[1]); 58625687Seric i &= ~NEED_T; 58725013Seric break; 5884632Seric } 5894632Seric } 5904632Seric (void) fclose(cf); 59124953Seric 59257438Seric if (!doall && shouldqueue(w->w_pri, w->w_ctime)) 59324953Seric { 59424953Seric /* don't even bother sorting this job in */ 59524953Seric wn--; 59624953Seric } 5974632Seric } 5986625Sglickman (void) closedir(f); 59910090Seric wn++; 6004632Seric 6014632Seric /* 6024632Seric ** Sort the work directory. 6034632Seric */ 6044632Seric 60525687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6064632Seric 6074632Seric /* 6084632Seric ** Convert the work list into canonical form. 6099377Seric ** Should be turning it into a list of envelopes here perhaps. 6104632Seric */ 6114632Seric 61224981Seric WorkQ = NULL; 61325687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6144632Seric { 6154632Seric w = (WORK *) xalloc(sizeof *w); 6164632Seric w->w_name = wlist[i].w_name; 6174632Seric w->w_pri = wlist[i].w_pri; 61825013Seric w->w_ctime = wlist[i].w_ctime; 61924981Seric w->w_next = WorkQ; 62024981Seric WorkQ = w; 6214632Seric } 6224632Seric 6237677Seric if (tTd(40, 1)) 6244632Seric { 6254632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6265037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 6274632Seric } 62810070Seric 62910090Seric return (wn); 6304632Seric } 6314632Seric /* 6327677Seric ** WORKCMPF -- compare function for ordering work. 6334632Seric ** 6344632Seric ** Parameters: 6354632Seric ** a -- the first argument. 6364632Seric ** b -- the second argument. 6374632Seric ** 6384632Seric ** Returns: 63924981Seric ** -1 if a < b 64024981Seric ** 0 if a == b 64124981Seric ** +1 if a > b 6424632Seric ** 6434632Seric ** Side Effects: 6444632Seric ** none. 6454632Seric */ 6464632Seric 6474632Seric workcmpf(a, b) 6485037Seric register WORK *a; 6495037Seric register WORK *b; 6504632Seric { 65157438Seric long pa = a->w_pri; 65257438Seric long pb = b->w_pri; 65324941Seric 65424941Seric if (pa == pb) 6554632Seric return (0); 65624941Seric else if (pa > pb) 65724981Seric return (1); 65824981Seric else 65910121Seric return (-1); 6604632Seric } 6614632Seric /* 6624632Seric ** DOWORK -- do a work request. 6634632Seric ** 6644632Seric ** Parameters: 6654632Seric ** w -- the work request to be satisfied. 6664632Seric ** 6674632Seric ** Returns: 6684632Seric ** none. 6694632Seric ** 6704632Seric ** Side Effects: 6714632Seric ** The work request is satisfied if possible. 6724632Seric */ 6734632Seric 67455012Seric dowork(w, e) 6754632Seric register WORK *w; 67655012Seric register ENVELOPE *e; 6774632Seric { 6784632Seric register int i; 67924941Seric extern bool shouldqueue(); 68051920Seric extern bool readqf(); 6814632Seric 6827677Seric if (tTd(40, 1)) 6835037Seric printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 6844632Seric 6854632Seric /* 68624941Seric ** Ignore jobs that are too expensive for the moment. 6874632Seric */ 6884632Seric 68957438Seric if (shouldqueue(w->w_pri, w->w_ctime)) 6904632Seric { 69124941Seric if (Verbose) 69224967Seric printf("\nSkipping %s\n", w->w_name + 2); 6934632Seric return; 6944632Seric } 6954632Seric 69624941Seric /* 69724941Seric ** Fork for work. 69824941Seric */ 69924941Seric 70024941Seric if (ForkQueueRuns) 70124941Seric { 70224941Seric i = fork(); 70324941Seric if (i < 0) 70424941Seric { 70524941Seric syserr("dowork: cannot fork"); 70624941Seric return; 70724941Seric } 70824941Seric } 70924941Seric else 71024941Seric { 71124941Seric i = 0; 71224941Seric } 71324941Seric 7144632Seric if (i == 0) 7154632Seric { 7164632Seric /* 7174632Seric ** CHILD 7188148Seric ** Lock the control file to avoid duplicate deliveries. 7198148Seric ** Then run the file as though we had just read it. 7207350Seric ** We save an idea of the temporary name so we 7217350Seric ** can recover on interrupt. 7224632Seric */ 7234632Seric 7247763Seric /* set basic modes, etc. */ 7257356Seric (void) alarm(0); 72655012Seric clearenvelope(e, FALSE); 7274632Seric QueueRun = TRUE; 7289377Seric ErrorMode = EM_MAIL; 72955012Seric e->e_id = &w->w_name[2]; 7307876Seric # ifdef LOG 73155360Seric if (LogLevel > 12) 73255012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 7337881Seric getpid()); 73456795Seric # endif /* LOG */ 7357763Seric 7367763Seric /* don't use the headers from sendmail.cf... */ 73755012Seric e->e_header = NULL; 7387763Seric 73951920Seric /* read the queue control file -- return if locked */ 74055012Seric if (!readqf(e)) 7416980Seric { 74224941Seric if (ForkQueueRuns) 74324941Seric exit(EX_OK); 74424941Seric else 74524941Seric return; 7466980Seric } 7476980Seric 74855012Seric e->e_flags |= EF_INQUEUE; 74957642Seric eatheader(e, TRUE); 7506980Seric 7516980Seric /* do the delivery */ 75255012Seric if (!bitset(EF_FATALERRS, e->e_flags)) 75355012Seric sendall(e, SM_DELIVER); 7546980Seric 7556980Seric /* finish up and exit */ 75624941Seric if (ForkQueueRuns) 75724941Seric finis(); 75824941Seric else 75955012Seric dropenvelope(e); 7604632Seric } 76124941Seric else 76224941Seric { 76324941Seric /* 76424941Seric ** Parent -- pick up results. 76524941Seric */ 7664632Seric 76724941Seric errno = 0; 76824941Seric (void) waitfor(i); 76924941Seric } 7704632Seric } 7714632Seric /* 7724632Seric ** READQF -- read queue file and set up environment. 7734632Seric ** 7744632Seric ** Parameters: 7759377Seric ** e -- the envelope of the job to run. 7764632Seric ** 7774632Seric ** Returns: 77851920Seric ** TRUE if it successfully read the queue file. 77951920Seric ** FALSE otherwise. 7804632Seric ** 7814632Seric ** Side Effects: 78251920Seric ** The queue file is returned locked. 7834632Seric */ 7844632Seric 78551920Seric bool 78651920Seric readqf(e) 7879377Seric register ENVELOPE *e; 7884632Seric { 78917477Seric char *qf; 79017477Seric register FILE *qfp; 79154974Seric ADDRESS *ctladdr; 79256400Seric struct stat st; 79357135Seric char *bp; 79457135Seric char buf[MAXLINE]; 7959348Seric extern char *fgetfolded(); 79624954Seric extern long atol(); 79754974Seric extern ADDRESS *setctluser(); 79851937Seric # ifdef LOCKF 79951937Seric struct flock lfd; 80051937Seric # endif 8014632Seric 8024632Seric /* 80317468Seric ** Read and process the file. 8044632Seric */ 8054632Seric 80617477Seric qf = queuename(e, 'q'); 80751937Seric qfp = fopen(qf, "r+"); 80817477Seric if (qfp == NULL) 80917477Seric { 81040934Srick if (errno != ENOENT) 81140934Srick syserr("readqf: no control file %s", qf); 81251920Seric return FALSE; 81317477Seric } 81440934Srick 81556400Seric /* 81656400Seric ** Check the queue file for plausibility to avoid attacks. 81756400Seric */ 81856400Seric 81956400Seric if (fstat(fileno(qfp), &st) < 0) 82056400Seric { 82156400Seric /* must have been being processed by someone else */ 82256400Seric fclose(qfp); 82356400Seric return FALSE; 82456400Seric } 82556400Seric 82657135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 82756400Seric { 82856400Seric # ifdef LOG 82956400Seric if (LogLevel > 0) 83056400Seric { 83156400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 83256400Seric e->e_id, st.st_uid, st.st_mode); 83356400Seric } 83456795Seric # endif /* LOG */ 83556400Seric fclose(qfp); 83656400Seric return FALSE; 83756400Seric } 83856400Seric 83951835Seric # ifdef LOCKF 84051937Seric lfd.l_type = F_WRLCK; 84151937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 84251937Seric if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 84351835Seric # else 84440934Srick if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 84551835Seric # endif 84640934Srick { 84740934Srick /* being processed by another queuer */ 84840934Srick if (Verbose) 84955012Seric printf("%s: locked\n", e->e_id); 85051920Seric # ifdef LOG 85155173Seric if (LogLevel > 10) 85255012Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 85356795Seric # endif /* LOG */ 85440934Srick (void) fclose(qfp); 85551920Seric return FALSE; 85640934Srick } 85740934Srick 85851920Seric /* save this lock */ 85951920Seric e->e_lockfp = qfp; 86051920Seric 86140934Srick /* do basic system initialization */ 86255012Seric initsys(e); 86340934Srick 86417477Seric FileName = qf; 8659377Seric LineNumber = 0; 86651920Seric if (Verbose) 8679377Seric printf("\nRunning %s\n", e->e_id); 86854974Seric ctladdr = NULL; 86957135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 8704632Seric { 87157529Seric struct stat st; 87257529Seric 87326504Seric if (tTd(40, 4)) 87457135Seric printf("+++++ %s\n", bp); 87557135Seric switch (bp[0]) 8764632Seric { 87740973Sbostic case 'C': /* specify controlling user */ 87857135Seric ctladdr = setctluser(&bp[1]); 87940973Sbostic break; 88040973Sbostic 8814632Seric case 'R': /* specify recipient */ 88257135Seric sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 8834632Seric break; 8844632Seric 88525687Seric case 'E': /* specify error recipient */ 88657135Seric sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 88725687Seric break; 88825687Seric 8894632Seric case 'H': /* header */ 89057135Seric (void) chompheader(&bp[1], FALSE, e); 8914632Seric break; 8924632Seric 89310108Seric case 'M': /* message */ 89457135Seric e->e_message = newstr(&bp[1]); 89510108Seric break; 89610108Seric 8974632Seric case 'S': /* sender */ 89857135Seric setsender(newstr(&bp[1]), e); 8994632Seric break; 9004632Seric 9014632Seric case 'D': /* data file name */ 90257135Seric e->e_df = newstr(&bp[1]); 9039544Seric e->e_dfp = fopen(e->e_df, "r"); 9049544Seric if (e->e_dfp == NULL) 9059377Seric syserr("readqf: cannot open %s", e->e_df); 90657529Seric if (fstat(fileno(e->e_dfp), &st) >= 0) 90757529Seric e->e_msgsize = st.st_size; 9084632Seric break; 9094632Seric 9107860Seric case 'T': /* init time */ 91157135Seric e->e_ctime = atol(&bp[1]); 9124632Seric break; 9134632Seric 9144634Seric case 'P': /* message priority */ 91557135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9164634Seric break; 9174634Seric 91853400Seric case '$': /* define macro */ 91957135Seric define(bp[1], newstr(&bp[2]), e); 92053400Seric break; 92153400Seric 92224941Seric case '\0': /* blank line; ignore */ 92324941Seric break; 92424941Seric 9254632Seric default: 92624941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 92757135Seric LineNumber, bp); 9284632Seric break; 9294632Seric } 93057135Seric 93157135Seric if (bp != buf) 93257135Seric free(bp); 9334632Seric } 9349377Seric 9359377Seric FileName = NULL; 93624941Seric 93724941Seric /* 93824941Seric ** If we haven't read any lines, this queue file is empty. 93924941Seric ** Arrange to remove it without referencing any null pointers. 94024941Seric */ 94124941Seric 94224941Seric if (LineNumber == 0) 94324941Seric { 94424941Seric errno = 0; 94524941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 94624941Seric } 94751920Seric return TRUE; 9484632Seric } 9494632Seric /* 9509630Seric ** PRINTQUEUE -- print out a representation of the mail queue 9519630Seric ** 9529630Seric ** Parameters: 9539630Seric ** none. 9549630Seric ** 9559630Seric ** Returns: 9569630Seric ** none. 9579630Seric ** 9589630Seric ** Side Effects: 9599630Seric ** Prints a listing of the mail queue on the standard output. 9609630Seric */ 9615182Seric 9629630Seric printqueue() 9639630Seric { 9649630Seric register WORK *w; 9659630Seric FILE *f; 96610070Seric int nrequests; 9679630Seric char buf[MAXLINE]; 9689630Seric 9699630Seric /* 9709630Seric ** Read and order the queue. 9719630Seric */ 9729630Seric 97324941Seric nrequests = orderq(TRUE); 9749630Seric 9759630Seric /* 9769630Seric ** Print the work list that we have read. 9779630Seric */ 9789630Seric 9799630Seric /* first see if there is anything */ 98010070Seric if (nrequests <= 0) 9819630Seric { 98210070Seric printf("Mail queue is empty\n"); 9839630Seric return; 9849630Seric } 9859630Seric 98651920Seric CurrentLA = getla(); /* get load average */ 98740934Srick 98810096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 98925687Seric if (nrequests > QUEUESIZE) 99025687Seric printf(", only %d printed", QUEUESIZE); 99124979Seric if (Verbose) 99225032Seric printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 99324979Seric else 99424979Seric printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 9959630Seric for (w = WorkQ; w != NULL; w = w->w_next) 9969630Seric { 9979630Seric struct stat st; 99810070Seric auto time_t submittime = 0; 99910070Seric long dfsize = -1; 100010108Seric char message[MAXLINE]; 100151937Seric # ifdef LOCKF 100251937Seric struct flock lfd; 100351937Seric # endif 100424941Seric extern bool shouldqueue(); 10059630Seric 100617468Seric f = fopen(w->w_name, "r"); 100717468Seric if (f == NULL) 100817468Seric { 100917468Seric errno = 0; 101017468Seric continue; 101117468Seric } 10129630Seric printf("%7s", w->w_name + 2); 101351835Seric # ifdef LOCKF 101451937Seric lfd.l_type = F_RDLCK; 101551937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 101651937Seric if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 101751835Seric # else 101840934Srick if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 101951835Seric # endif 102010070Seric printf("*"); 102157438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 102224941Seric printf("X"); 102310070Seric else 102410070Seric printf(" "); 102510070Seric errno = 0; 102617468Seric 102710108Seric message[0] = '\0'; 10289630Seric while (fgets(buf, sizeof buf, f) != NULL) 10299630Seric { 103053400Seric register int i; 103153400Seric 10329630Seric fixcrlf(buf, TRUE); 10339630Seric switch (buf[0]) 10349630Seric { 103510108Seric case 'M': /* error message */ 103653400Seric if ((i = strlen(&buf[1])) >= sizeof message) 103753400Seric i = sizeof message; 103853400Seric bcopy(&buf[1], message, i); 103953400Seric message[i] = '\0'; 104010108Seric break; 104110108Seric 10429630Seric case 'S': /* sender name */ 104324979Seric if (Verbose) 104425027Seric printf("%8ld %10ld %.12s %.38s", dfsize, 104525027Seric w->w_pri, ctime(&submittime) + 4, 104624979Seric &buf[1]); 104724979Seric else 104824979Seric printf("%8ld %.16s %.45s", dfsize, 104924979Seric ctime(&submittime), &buf[1]); 105010108Seric if (message[0] != '\0') 105125027Seric printf("\n\t\t (%.60s)", message); 10529630Seric break; 105351920Seric 105440973Sbostic case 'C': /* controlling user */ 105554974Seric if (Verbose) 105654975Seric printf("\n\t\t\t\t (---%.34s---)", &buf[1]); 105740973Sbostic break; 10589630Seric 10599630Seric case 'R': /* recipient name */ 106024979Seric if (Verbose) 106125027Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 106224979Seric else 106324979Seric printf("\n\t\t\t\t %.45s", &buf[1]); 10649630Seric break; 10659630Seric 10669630Seric case 'T': /* creation time */ 106724941Seric submittime = atol(&buf[1]); 10689630Seric break; 106910070Seric 107010070Seric case 'D': /* data file name */ 107110070Seric if (stat(&buf[1], &st) >= 0) 107210070Seric dfsize = st.st_size; 107310070Seric break; 10749630Seric } 10759630Seric } 107610070Seric if (submittime == (time_t) 0) 107710070Seric printf(" (no control file)"); 10789630Seric printf("\n"); 107923098Seric (void) fclose(f); 10809630Seric } 10819630Seric } 10829630Seric 108356795Seric # endif /* QUEUE */ 108417468Seric /* 108517468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 108617468Seric ** 108717468Seric ** Assigns an id code if one does not already exist. 108817468Seric ** This code is very careful to avoid trashing existing files 108917468Seric ** under any circumstances. 109017468Seric ** 109117468Seric ** Parameters: 109217468Seric ** e -- envelope to build it in/from. 109317468Seric ** type -- the file type, used as the first character 109417468Seric ** of the file name. 109517468Seric ** 109617468Seric ** Returns: 109717468Seric ** a pointer to the new file name (in a static buffer). 109817468Seric ** 109917468Seric ** Side Effects: 110051920Seric ** If no id code is already assigned, queuename will 110151920Seric ** assign an id code, create a qf file, and leave a 110251920Seric ** locked, open-for-write file pointer in the envelope. 110317468Seric */ 110417468Seric 110517468Seric char * 110617468Seric queuename(e, type) 110717468Seric register ENVELOPE *e; 110817468Seric char type; 110917468Seric { 111017468Seric static char buf[MAXNAME]; 111117468Seric static int pid = -1; 111217468Seric char c1 = 'A'; 111317468Seric char c2 = 'A'; 111417468Seric 111517468Seric if (e->e_id == NULL) 111617468Seric { 111717468Seric char qf[20]; 111817468Seric 111917468Seric /* find a unique id */ 112017468Seric if (pid != getpid()) 112117468Seric { 112217468Seric /* new process -- start back at "AA" */ 112317468Seric pid = getpid(); 112417468Seric c1 = 'A'; 112517468Seric c2 = 'A' - 1; 112617468Seric } 112717468Seric (void) sprintf(qf, "qfAA%05d", pid); 112817468Seric 112917468Seric while (c1 < '~' || c2 < 'Z') 113017468Seric { 113117468Seric int i; 113251937Seric # ifdef LOCKF 113351937Seric struct flock lfd; 113451937Seric # endif 113517468Seric 113617468Seric if (c2 >= 'Z') 113717468Seric { 113817468Seric c1++; 113917468Seric c2 = 'A' - 1; 114017468Seric } 114140934Srick qf[2] = c1; 114240934Srick qf[3] = ++c2; 114317468Seric if (tTd(7, 20)) 114440934Srick printf("queuename: trying \"%s\"\n", qf); 114517468Seric 114640934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 114751920Seric if (i < 0) 114851920Seric { 114951920Seric if (errno == EEXIST) 115051920Seric continue; 115151920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 115251920Seric qf, QueueDir); 115351920Seric exit(EX_UNAVAILABLE); 115451920Seric } 115551920Seric # ifdef LOCKF 115651937Seric lfd.l_type = F_WRLCK; 115751937Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 115851937Seric if (fcntl(i, F_SETLK, &lfd) >= 0) 115951920Seric # else 116051920Seric if (flock(i, LOCK_EX|LOCK_NB) >= 0) 116151920Seric # endif 116251920Seric { 116351920Seric e->e_lockfp = fdopen(i, "w"); 116440934Srick break; 116517468Seric } 116651920Seric 116751920Seric /* a reader got the file; abandon it and try again */ 116851920Seric (void) close(i); 116917468Seric } 117017468Seric if (c1 >= '~' && c2 >= 'Z') 117117468Seric { 117217468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 117317468Seric qf, QueueDir); 117417468Seric exit(EX_OSERR); 117517468Seric } 117617468Seric e->e_id = newstr(&qf[2]); 117717468Seric define('i', e->e_id, e); 117817468Seric if (tTd(7, 1)) 117917468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 118017468Seric # ifdef LOG 118117468Seric if (LogLevel > 16) 118217468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 118356795Seric # endif /* LOG */ 118417468Seric } 118517468Seric 118617468Seric if (type == '\0') 118717468Seric return (NULL); 118817468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 118917468Seric if (tTd(7, 2)) 119017468Seric printf("queuename: %s\n", buf); 119117468Seric return (buf); 119217468Seric } 119317468Seric /* 119417468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 119517468Seric ** 119617468Seric ** Parameters: 119717468Seric ** e -- the envelope to unlock. 119817468Seric ** 119917468Seric ** Returns: 120017468Seric ** none 120117468Seric ** 120217468Seric ** Side Effects: 120317468Seric ** unlocks the queue for `e'. 120417468Seric */ 120517468Seric 120617468Seric unlockqueue(e) 120717468Seric ENVELOPE *e; 120817468Seric { 120951920Seric /* if there is a lock file in the envelope, close it */ 121051920Seric if (e->e_lockfp != NULL) 121151920Seric fclose(e->e_lockfp); 121251920Seric e->e_lockfp = NULL; 121351920Seric 121417468Seric /* remove the transcript */ 121517468Seric # ifdef LOG 121617468Seric if (LogLevel > 19) 121717468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 121856795Seric # endif /* LOG */ 121917468Seric if (!tTd(51, 4)) 122017468Seric xunlink(queuename(e, 'x')); 122117468Seric 122217468Seric } 122340973Sbostic /* 122454974Seric ** SETCTLUSER -- create a controlling address 122540973Sbostic ** 122654974Seric ** Create a fake "address" given only a local login name; this is 122754974Seric ** used as a "controlling user" for future recipient addresses. 122840973Sbostic ** 122940973Sbostic ** Parameters: 123054974Seric ** user -- the user name of the controlling user. 123140973Sbostic ** 123240973Sbostic ** Returns: 123354974Seric ** An address descriptor for the controlling user. 123440973Sbostic ** 123540973Sbostic ** Side Effects: 123640973Sbostic ** none. 123740973Sbostic */ 123840973Sbostic 123954974Seric ADDRESS * 124054974Seric setctluser(user) 124154974Seric char *user; 124240973Sbostic { 124354974Seric register ADDRESS *a; 124440973Sbostic struct passwd *pw; 124540973Sbostic 124640973Sbostic /* 124754974Seric ** See if this clears our concept of controlling user. 124840973Sbostic */ 124940973Sbostic 125054974Seric if (user == NULL || *user == '\0') 125154974Seric return NULL; 125240973Sbostic 125340973Sbostic /* 125454974Seric ** Set up addr fields for controlling user. 125540973Sbostic */ 125640973Sbostic 125754974Seric a = (ADDRESS *) xalloc(sizeof *a); 125854974Seric bzero((char *) a, sizeof *a); 125954974Seric if ((pw = getpwnam(user)) != NULL) 126040973Sbostic { 126140973Sbostic a->q_home = newstr(pw->pw_dir); 126240973Sbostic a->q_uid = pw->pw_uid; 126340973Sbostic a->q_gid = pw->pw_gid; 126457642Seric a->q_user = newstr(user); 126540973Sbostic } 126640973Sbostic else 126740973Sbostic { 126840973Sbostic a->q_uid = DefUid; 126940973Sbostic a->q_gid = DefGid; 127057642Seric a->q_user = newstr(DefUser); 127140973Sbostic } 127240973Sbostic 127340973Sbostic a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 127456328Seric a->q_mailer = LocalMailer; 127554974Seric return a; 127640973Sbostic } 1277