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*59093Seric static char sccsid[] = "@(#)queue.c 6.47 (Berkeley) 04/15/93 (with queueing)"; 1433731Sbostic #else 15*59093Seric static char sccsid[] = "@(#)queue.c 6.47 (Berkeley) 04/15/93 (without queueing)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194632Seric # include <sys/stat.h> 2013707Ssam # include <sys/dir.h> 214634Seric # include <signal.h> 224632Seric # include <errno.h> 2340973Sbostic # include <pwd.h> 2457977Seric # ifndef MAXNAMLEN 2557736Seric # include <dirent.h> 2657736Seric # endif 274632Seric 2833731Sbostic # ifdef QUEUE 294632Seric 304632Seric /* 319377Seric ** Work queue. 329377Seric */ 339377Seric 349377Seric struct work 359377Seric { 369377Seric char *w_name; /* name of control file */ 379377Seric long w_pri; /* priority of message, see below */ 3825013Seric time_t w_ctime; /* creation time of message */ 399377Seric struct work *w_next; /* next in queue */ 409377Seric }; 419377Seric 429377Seric typedef struct work WORK; 439377Seric 449377Seric WORK *WorkQ; /* queue of things to be done */ 459377Seric /* 464632Seric ** QUEUEUP -- queue a message up for future transmission. 474632Seric ** 484632Seric ** Parameters: 496980Seric ** e -- the envelope to queue up. 506999Seric ** queueall -- if TRUE, queue all addresses, rather than 516999Seric ** just those with the QQUEUEUP flag set. 529377Seric ** announce -- if TRUE, tell when you are queueing up. 534632Seric ** 544632Seric ** Returns: 5551920Seric ** none. 564632Seric ** 574632Seric ** Side Effects: 589377Seric ** The current request are saved in a control file. 5951920Seric ** The queue file is left locked. 604632Seric */ 614632Seric 629377Seric queueup(e, queueall, announce) 636980Seric register ENVELOPE *e; 646999Seric bool queueall; 659377Seric bool announce; 664632Seric { 677812Seric char *qf; 687812Seric register FILE *tfp; 694632Seric register HDR *h; 705007Seric register ADDRESS *q; 7151920Seric int fd; 7251920Seric int i; 7351920Seric bool newid; 7453400Seric register char *p; 7510173Seric MAILER nullmailer; 7654974Seric ADDRESS *lastctladdr; 7751920Seric char buf[MAXLINE], tf[MAXLINE]; 7853400Seric extern char *macvalue(); 7954974Seric extern ADDRESS *getctladdr(); 804632Seric 815037Seric /* 8217477Seric ** Create control file. 835037Seric */ 844632Seric 8551920Seric newid = (e->e_id == NULL); 8651920Seric strcpy(tf, queuename(e, 't')); 8751920Seric tfp = e->e_lockfp; 8851920Seric if (tfp == NULL) 8951920Seric newid = FALSE; 9051920Seric if (newid) 9151835Seric { 9251920Seric tfp = e->e_lockfp; 9351920Seric } 9451920Seric else 9551920Seric { 9651920Seric /* get a locked tf file */ 9751920Seric for (i = 100; --i >= 0; ) 9851835Seric { 9958689Seric extern bool lockfile(); 10051937Seric 10151920Seric fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 10251920Seric if (fd < 0) 10351835Seric { 10451920Seric if (errno == EEXIST) 10551920Seric continue; 10658801Seric notemp: 10758690Seric syserr("!queueup: cannot create temp file %s", tf); 10841636Srick } 10958689Seric 11058689Seric if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 11151920Seric break; 11258689Seric 11351920Seric close(fd); 11458801Seric sleep(i); 11541636Srick } 11658801Seric if (fd < 0) 11758801Seric goto notemp; 11841636Srick 11951920Seric tfp = fdopen(fd, "w"); 12051920Seric } 1214632Seric 1227677Seric if (tTd(40, 1)) 12317468Seric printf("queueing %s\n", e->e_id); 1244632Seric 1254632Seric /* 1266980Seric ** If there is no data file yet, create one. 1276980Seric */ 1286980Seric 1296980Seric if (e->e_df == NULL) 1306980Seric { 1316980Seric register FILE *dfp; 1329389Seric extern putbody(); 1336980Seric 1347812Seric e->e_df = newstr(queuename(e, 'd')); 13540934Srick fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 13640934Srick if (fd < 0) 13758690Seric syserr("!queueup: cannot create %s", e->e_df); 13840934Srick dfp = fdopen(fd, "w"); 13910173Seric (*e->e_putbody)(dfp, ProgMailer, e); 14058680Seric (void) xfclose(dfp, "queueup dfp", e->e_id); 1419389Seric e->e_putbody = putbody; 1426980Seric } 1436980Seric 1446980Seric /* 1454632Seric ** Output future work requests. 14625687Seric ** Priority and creation time should be first, since 14725687Seric ** they are required by orderq. 1484632Seric */ 1494632Seric 1509377Seric /* output message priority */ 1519377Seric fprintf(tfp, "P%ld\n", e->e_msgpriority); 1529377Seric 1539630Seric /* output creation time */ 1549630Seric fprintf(tfp, "T%ld\n", e->e_ctime); 1559630Seric 156*59093Seric /* output type and name of data file */ 157*59093Seric if (e->e_bodytype != NULL) 158*59093Seric fprintf(tfp, "B%s\n", e->e_bodytype); 1597812Seric fprintf(tfp, "D%s\n", e->e_df); 1604632Seric 16110108Seric /* message from envelope, if it exists */ 16210108Seric if (e->e_message != NULL) 16310108Seric fprintf(tfp, "M%s\n", e->e_message); 16410108Seric 16558737Seric /* send various flag bits through */ 16658737Seric p = buf; 16758737Seric if (bitset(EF_WARNING, e->e_flags)) 16858737Seric *p++ = 'w'; 16958737Seric if (bitset(EF_RESPONSE, e->e_flags)) 17058737Seric *p++ = 'r'; 17158737Seric *p++ = '\0'; 17258737Seric if (buf[0] != '\0') 17358737Seric fprintf(tfp, "F%s\n", buf); 17458737Seric 17558957Seric /* $r and $s and $_ macro values */ 17653400Seric if ((p = macvalue('r', e)) != NULL) 17753400Seric fprintf(tfp, "$r%s\n", p); 17853400Seric if ((p = macvalue('s', e)) != NULL) 17953400Seric fprintf(tfp, "$s%s\n", p); 18058957Seric if ((p = macvalue('_', e)) != NULL) 18158957Seric fprintf(tfp, "$_%s\n", p); 18253400Seric 1834632Seric /* output name of sender */ 1847812Seric fprintf(tfp, "S%s\n", e->e_from.q_paddr); 1854632Seric 18655360Seric /* output list of error recipients */ 18755360Seric lastctladdr = NULL; 18855360Seric for (q = e->e_errorqueue; q != NULL; q = q->q_next) 18955360Seric { 19058680Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 19155360Seric { 19255360Seric ADDRESS *ctladdr; 19355360Seric 19455360Seric ctladdr = getctladdr(q); 19555360Seric if (ctladdr != lastctladdr) 19655360Seric { 19755360Seric printctladdr(ctladdr, tfp); 19855360Seric lastctladdr = ctladdr; 19955360Seric } 20055360Seric fprintf(tfp, "E%s\n", q->q_paddr); 20155360Seric } 20255360Seric } 20355360Seric 2044632Seric /* output list of recipient addresses */ 2056980Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 2064632Seric { 20758250Seric if (bitset(QQUEUEUP, q->q_flags) || 20858680Seric (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 2098245Seric { 21054974Seric ADDRESS *ctladdr; 21140973Sbostic 21254975Seric ctladdr = getctladdr(q); 21354975Seric if (ctladdr != lastctladdr) 21454974Seric { 21554974Seric printctladdr(ctladdr, tfp); 21654974Seric lastctladdr = ctladdr; 21754974Seric } 2187812Seric fprintf(tfp, "R%s\n", q->q_paddr); 2199377Seric if (announce) 2209377Seric { 2219377Seric e->e_to = q->q_paddr; 22258151Seric message("queued"); 22358020Seric if (LogLevel > 8) 22458337Seric logdelivery(NULL, NULL, "queued", e); 2259377Seric e->e_to = NULL; 2269377Seric } 2279387Seric if (tTd(40, 1)) 2289387Seric { 2299387Seric printf("queueing "); 2309387Seric printaddr(q, FALSE); 2319387Seric } 2328245Seric } 2334632Seric } 2344632Seric 2359377Seric /* 2369377Seric ** Output headers for this message. 2379377Seric ** Expand macros completely here. Queue run will deal with 2389377Seric ** everything as absolute headers. 2399377Seric ** All headers that must be relative to the recipient 2409377Seric ** can be cracked later. 24110173Seric ** We set up a "null mailer" -- i.e., a mailer that will have 24210173Seric ** no effect on the addresses as they are output. 2439377Seric */ 2449377Seric 24510686Seric bzero((char *) &nullmailer, sizeof nullmailer); 24658020Seric nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 24758020Seric nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 24810349Seric nullmailer.m_eol = "\n"; 24910173Seric 25058050Seric define('g', "\201f", e); 2516980Seric for (h = e->e_header; h != NULL; h = h->h_link) 2524632Seric { 25310686Seric extern bool bitzerop(); 25410686Seric 25512015Seric /* don't output null headers */ 2564632Seric if (h->h_value == NULL || h->h_value[0] == '\0') 2574632Seric continue; 25812015Seric 25912015Seric /* don't output resent headers on non-resent messages */ 26012015Seric if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 26112015Seric continue; 26212015Seric 26312015Seric /* output this header */ 2647812Seric fprintf(tfp, "H"); 26512015Seric 26612015Seric /* if conditional, output the set of conditions */ 26710686Seric if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 26810686Seric { 26910686Seric int j; 27010686Seric 27123098Seric (void) putc('?', tfp); 27210686Seric for (j = '\0'; j <= '\177'; j++) 27310686Seric if (bitnset(j, h->h_mflags)) 27423098Seric (void) putc(j, tfp); 27523098Seric (void) putc('?', tfp); 27610686Seric } 27712015Seric 27812015Seric /* output the header: expand macros, convert addresses */ 2797763Seric if (bitset(H_DEFAULT, h->h_flags)) 2807763Seric { 2817763Seric (void) expand(h->h_value, buf, &buf[sizeof buf], e); 2828236Seric fprintf(tfp, "%s: %s\n", h->h_field, buf); 2837763Seric } 2848245Seric else if (bitset(H_FROM|H_RCPT, h->h_flags)) 2859348Seric { 28658737Seric bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 28758737Seric 28858737Seric if (bitset(H_FROM, h->h_flags)) 28958737Seric oldstyle = FALSE; 29058737Seric 29158737Seric commaize(h, h->h_value, tfp, oldstyle, 29255012Seric &nullmailer, e); 2939348Seric } 2947763Seric else 2958245Seric fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 2964632Seric } 2974632Seric 2984632Seric /* 2994632Seric ** Clean up. 3004632Seric */ 3014632Seric 30258732Seric fflush(tfp); 30358732Seric if (ferror(tfp)) 30458732Seric { 30558732Seric if (newid) 30658732Seric syserr("!552 Error writing control file %s", tf); 30758732Seric else 30858732Seric syserr("!452 Error writing control file %s", tf); 30958732Seric } 31058732Seric 31151920Seric if (!newid) 31251920Seric { 31351920Seric qf = queuename(e, 'q'); 31451920Seric if (rename(tf, qf) < 0) 31551920Seric syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 31651920Seric if (e->e_lockfp != NULL) 31758680Seric (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 31851920Seric e->e_lockfp = tfp; 31951920Seric } 32051920Seric else 32151920Seric qf = tf; 32241636Srick errno = 0; 3237391Seric 3247677Seric # ifdef LOG 3257677Seric /* save log info */ 32658020Seric if (LogLevel > 79) 3277878Seric syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 32856795Seric # endif /* LOG */ 32940934Srick fflush(tfp); 33051920Seric return; 3314632Seric } 33254974Seric 33354974Seric printctladdr(a, tfp) 33454974Seric ADDRESS *a; 33554974Seric FILE *tfp; 33654974Seric { 33754974Seric char *u; 33854974Seric struct passwd *pw; 33954974Seric extern struct passwd *getpwuid(); 34054974Seric 34154974Seric if (a == NULL) 34254974Seric { 34354974Seric fprintf(tfp, "C\n"); 34454974Seric return; 34554974Seric } 34654974Seric if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 34754974Seric u = DefUser; 34854974Seric else 34954974Seric u = pw->pw_name; 35054974Seric fprintf(tfp, "C%s\n", u); 35154974Seric } 35254974Seric 3534632Seric /* 3544632Seric ** RUNQUEUE -- run the jobs in the queue. 3554632Seric ** 3564632Seric ** Gets the stuff out of the queue in some presumably logical 3574632Seric ** order and processes them. 3584632Seric ** 3594632Seric ** Parameters: 36024941Seric ** forkflag -- TRUE if the queue scanning should be done in 36124941Seric ** a child process. We double-fork so it is not our 36224941Seric ** child and we don't have to clean up after it. 3634632Seric ** 3644632Seric ** Returns: 3654632Seric ** none. 3664632Seric ** 3674632Seric ** Side Effects: 3684632Seric ** runs things in the mail queue. 3694632Seric */ 3704632Seric 37155360Seric ENVELOPE QueueEnvelope; /* the queue run envelope */ 37255360Seric 37355360Seric runqueue(forkflag) 3744639Seric bool forkflag; 3754632Seric { 37624953Seric extern bool shouldqueue(); 37755360Seric register ENVELOPE *e; 37855360Seric extern ENVELOPE BlankEnvelope; 37955360Seric extern ENVELOPE *newenvelope(); 38024953Seric 3817466Seric /* 38224953Seric ** If no work will ever be selected, don't even bother reading 38324953Seric ** the queue. 38424953Seric */ 38524953Seric 38651920Seric CurrentLA = getla(); /* get load average */ 38740934Srick 38858132Seric if (shouldqueue(0L, curtime())) 38924953Seric { 39024953Seric if (Verbose) 39124953Seric printf("Skipping queue run -- load average too high\n"); 39258107Seric if (forkflag && QueueIntvl != 0) 39358107Seric (void) setevent(QueueIntvl, runqueue, TRUE); 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); 43024941Seric 4317876Seric # ifdef LOG 43258020Seric if (LogLevel > 69) 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; 45058179Seric e = newenvelope(&QueueEnvelope, CurEnv); 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; 47358884Seric extern bool shouldqueue(); 4747881Seric 4757943Seric WorkQ = WorkQ->w_next; 47658884Seric 47758884Seric /* 47858884Seric ** Ignore jobs that are too expensive for the moment. 47958884Seric */ 48058884Seric 48158884Seric if (shouldqueue(w->w_pri, w->w_ctime)) 48258884Seric { 48358884Seric if (Verbose) 48458884Seric printf("\nSkipping %s\n", w->w_name + 2); 48558884Seric } 48658930Seric else 48758930Seric { 48858930Seric dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 48958930Seric } 4907943Seric free(w->w_name); 4917943Seric free((char *) w); 4924639Seric } 49329866Seric 49429866Seric /* exit without the usual cleanup */ 49555467Seric e->e_id = NULL; 49655467Seric finis(); 4974634Seric } 4984634Seric /* 4994632Seric ** ORDERQ -- order the work queue. 5004632Seric ** 5014632Seric ** Parameters: 50224941Seric ** doall -- if set, include everything in the queue (even 50324941Seric ** the jobs that cannot be run because the load 50424941Seric ** average is too high). Otherwise, exclude those 50524941Seric ** jobs. 5064632Seric ** 5074632Seric ** Returns: 50810121Seric ** The number of request in the queue (not necessarily 50910121Seric ** the number of requests in WorkQ however). 5104632Seric ** 5114632Seric ** Side Effects: 5124632Seric ** Sets WorkQ to the queue of available work, in order. 5134632Seric */ 5144632Seric 51525687Seric # define NEED_P 001 51625687Seric # define NEED_T 002 51758318Seric # define NEED_R 004 51858318Seric # define NEED_S 010 5194632Seric 52024941Seric orderq(doall) 52124941Seric bool doall; 5224632Seric { 5236625Sglickman register struct direct *d; 5244632Seric register WORK *w; 5256625Sglickman DIR *f; 5264632Seric register int i; 52725687Seric WORK wlist[QUEUESIZE+1]; 52810070Seric int wn = -1; 5294632Seric extern workcmpf(); 5304632Seric 53158318Seric if (tTd(41, 1)) 53258318Seric { 53358318Seric printf("orderq:\n"); 53458318Seric if (QueueLimitId != NULL) 53558318Seric printf("\tQueueLimitId = %s\n", QueueLimitId); 53658318Seric if (QueueLimitSender != NULL) 53758318Seric printf("\tQueueLimitSender = %s\n", QueueLimitSender); 53858318Seric if (QueueLimitRecipient != NULL) 53958318Seric printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 54058318Seric } 54158318Seric 5424632Seric /* clear out old WorkQ */ 5434632Seric for (w = WorkQ; w != NULL; ) 5444632Seric { 5454632Seric register WORK *nw = w->w_next; 5464632Seric 5474632Seric WorkQ = nw; 5484632Seric free(w->w_name); 5494632Seric free((char *) w); 5504632Seric w = nw; 5514632Seric } 5524632Seric 5534632Seric /* open the queue directory */ 5548148Seric f = opendir("."); 5554632Seric if (f == NULL) 5564632Seric { 5578148Seric syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 55810070Seric return (0); 5594632Seric } 5604632Seric 5614632Seric /* 5624632Seric ** Read the work directory. 5634632Seric */ 5644632Seric 56510070Seric while ((d = readdir(f)) != NULL) 5664632Seric { 5679377Seric FILE *cf; 5684632Seric char lbuf[MAXNAME]; 56957642Seric extern bool shouldqueue(); 57058318Seric extern bool strcontainedin(); 5714632Seric 5724632Seric /* is this an interesting entry? */ 5737812Seric if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 5744632Seric continue; 5754632Seric 57658318Seric if (QueueLimitId != NULL && 57758318Seric !strcontainedin(QueueLimitId, d->d_name)) 57858318Seric continue; 57958318Seric 58058722Seric /* 58158722Seric ** Check queue name for plausibility. This handles 58258722Seric ** both old and new type ids. 58358722Seric */ 58458722Seric 58558722Seric i = strlen(d->d_name); 58658722Seric if (i != 9 && i != 10) 58758020Seric { 58858020Seric if (Verbose) 58958020Seric printf("orderq: bogus qf name %s\n", d->d_name); 59058020Seric #ifdef LOG 59158020Seric if (LogLevel > 3) 59258020Seric syslog(LOG_NOTICE, "orderq: bogus qf name %s", 59358020Seric d->d_name); 59458020Seric #endif 59558020Seric if (strlen(d->d_name) >= MAXNAME) 59658020Seric d->d_name[MAXNAME - 1] = '\0'; 59758020Seric strcpy(lbuf, d->d_name); 59858020Seric lbuf[0] = 'Q'; 59958020Seric (void) rename(d->d_name, lbuf); 60058020Seric continue; 60158020Seric } 60258020Seric 60310070Seric /* yes -- open control file (if not too many files) */ 60425687Seric if (++wn >= QUEUESIZE) 60510070Seric continue; 60658318Seric 6078148Seric cf = fopen(d->d_name, "r"); 6084632Seric if (cf == NULL) 6094632Seric { 6107055Seric /* this may be some random person sending hir msgs */ 6117055Seric /* syserr("orderq: cannot open %s", cbuf); */ 61210090Seric if (tTd(41, 2)) 61310090Seric printf("orderq: cannot open %s (%d)\n", 61410090Seric d->d_name, errno); 6157055Seric errno = 0; 61610090Seric wn--; 6174632Seric continue; 6184632Seric } 61925687Seric w = &wlist[wn]; 62025687Seric w->w_name = newstr(d->d_name); 6214632Seric 62225027Seric /* make sure jobs in creation don't clog queue */ 62325687Seric w->w_pri = 0x7fffffff; 62425687Seric w->w_ctime = 0; 62525027Seric 6264632Seric /* extract useful information */ 62725687Seric i = NEED_P | NEED_T; 62858318Seric if (QueueLimitSender != NULL) 62958318Seric i |= NEED_S; 63058318Seric if (QueueLimitRecipient != NULL) 63158318Seric i |= NEED_R; 63225687Seric while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 6334632Seric { 63424954Seric extern long atol(); 63558318Seric extern bool strcontainedin(); 63624954Seric 63724941Seric switch (lbuf[0]) 6384632Seric { 63924941Seric case 'P': 64025687Seric w->w_pri = atol(&lbuf[1]); 64125687Seric i &= ~NEED_P; 6424632Seric break; 64325013Seric 64425013Seric case 'T': 64525687Seric w->w_ctime = atol(&lbuf[1]); 64625687Seric i &= ~NEED_T; 64725013Seric break; 64858318Seric 64958318Seric case 'R': 65058318Seric if (QueueLimitRecipient != NULL && 65158318Seric strcontainedin(QueueLimitRecipient, &lbuf[1])) 65258318Seric i &= ~NEED_R; 65358318Seric break; 65458318Seric 65558318Seric case 'S': 65658318Seric if (QueueLimitSender != NULL && 65758318Seric strcontainedin(QueueLimitSender, &lbuf[1])) 65858318Seric i &= ~NEED_S; 65958318Seric break; 6604632Seric } 6614632Seric } 6624632Seric (void) fclose(cf); 66324953Seric 66458318Seric if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 66558318Seric bitset(NEED_R|NEED_S, i)) 66624953Seric { 66724953Seric /* don't even bother sorting this job in */ 66824953Seric wn--; 66924953Seric } 6704632Seric } 6716625Sglickman (void) closedir(f); 67210090Seric wn++; 6734632Seric 6744632Seric /* 6754632Seric ** Sort the work directory. 6764632Seric */ 6774632Seric 67825687Seric qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 6794632Seric 6804632Seric /* 6814632Seric ** Convert the work list into canonical form. 6829377Seric ** Should be turning it into a list of envelopes here perhaps. 6834632Seric */ 6844632Seric 68524981Seric WorkQ = NULL; 68625687Seric for (i = min(wn, QUEUESIZE); --i >= 0; ) 6874632Seric { 6884632Seric w = (WORK *) xalloc(sizeof *w); 6894632Seric w->w_name = wlist[i].w_name; 6904632Seric w->w_pri = wlist[i].w_pri; 69125013Seric w->w_ctime = wlist[i].w_ctime; 69224981Seric w->w_next = WorkQ; 69324981Seric WorkQ = w; 6944632Seric } 6954632Seric 6967677Seric if (tTd(40, 1)) 6974632Seric { 6984632Seric for (w = WorkQ; w != NULL; w = w->w_next) 6995037Seric printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 7004632Seric } 70110070Seric 70210090Seric return (wn); 7034632Seric } 7044632Seric /* 7057677Seric ** WORKCMPF -- compare function for ordering work. 7064632Seric ** 7074632Seric ** Parameters: 7084632Seric ** a -- the first argument. 7094632Seric ** b -- the second argument. 7104632Seric ** 7114632Seric ** Returns: 71224981Seric ** -1 if a < b 71324981Seric ** 0 if a == b 71424981Seric ** +1 if a > b 7154632Seric ** 7164632Seric ** Side Effects: 7174632Seric ** none. 7184632Seric */ 7194632Seric 7204632Seric workcmpf(a, b) 7215037Seric register WORK *a; 7225037Seric register WORK *b; 7234632Seric { 72457438Seric long pa = a->w_pri; 72557438Seric long pb = b->w_pri; 72624941Seric 72724941Seric if (pa == pb) 7284632Seric return (0); 72924941Seric else if (pa > pb) 73024981Seric return (1); 73124981Seric else 73210121Seric return (-1); 7334632Seric } 7344632Seric /* 7354632Seric ** DOWORK -- do a work request. 7364632Seric ** 7374632Seric ** Parameters: 73858884Seric ** id -- the ID of the job to run. 73958884Seric ** forkflag -- if set, run this in background. 74058924Seric ** requeueflag -- if set, reinstantiate the queue quickly. 74158924Seric ** This is used when expanding aliases in the queue. 74258884Seric ** e - the envelope in which to run it. 7434632Seric ** 7444632Seric ** Returns: 7454632Seric ** none. 7464632Seric ** 7474632Seric ** Side Effects: 7484632Seric ** The work request is satisfied if possible. 7494632Seric */ 7504632Seric 75158924Seric dowork(id, forkflag, requeueflag, e) 75258884Seric char *id; 75358884Seric bool forkflag; 75458924Seric bool requeueflag; 75555012Seric register ENVELOPE *e; 7564632Seric { 7574632Seric register int i; 75851920Seric extern bool readqf(); 7594632Seric 7607677Seric if (tTd(40, 1)) 76158884Seric printf("dowork(%s)\n", id); 7624632Seric 7634632Seric /* 76424941Seric ** Fork for work. 76524941Seric */ 76624941Seric 76758884Seric if (forkflag) 76824941Seric { 76924941Seric i = fork(); 77024941Seric if (i < 0) 77124941Seric { 77224941Seric syserr("dowork: cannot fork"); 77324941Seric return; 77424941Seric } 77524941Seric } 77624941Seric else 77724941Seric { 77824941Seric i = 0; 77924941Seric } 78024941Seric 7814632Seric if (i == 0) 7824632Seric { 7834632Seric /* 7844632Seric ** CHILD 7858148Seric ** Lock the control file to avoid duplicate deliveries. 7868148Seric ** Then run the file as though we had just read it. 7877350Seric ** We save an idea of the temporary name so we 7887350Seric ** can recover on interrupt. 7894632Seric */ 7904632Seric 7917763Seric /* set basic modes, etc. */ 7927356Seric (void) alarm(0); 79355012Seric clearenvelope(e, FALSE); 79458737Seric e->e_flags |= EF_QUEUERUN; 79558734Seric e->e_errormode = EM_MAIL; 79658884Seric e->e_id = id; 7977876Seric # ifdef LOG 79858020Seric if (LogLevel > 76) 79955012Seric syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 8007881Seric getpid()); 80156795Seric # endif /* LOG */ 8027763Seric 8037763Seric /* don't use the headers from sendmail.cf... */ 80455012Seric e->e_header = NULL; 8057763Seric 80651920Seric /* read the queue control file -- return if locked */ 80755012Seric if (!readqf(e)) 8086980Seric { 80958884Seric if (tTd(40, 4)) 81058884Seric printf("readqf(%s) failed\n", e->e_id); 81158884Seric if (forkflag) 81224941Seric exit(EX_OK); 81324941Seric else 81424941Seric return; 8156980Seric } 8166980Seric 81755012Seric e->e_flags |= EF_INQUEUE; 81858929Seric eatheader(e, requeueflag); 8196980Seric 82058924Seric if (requeueflag) 82158924Seric queueup(e, TRUE, FALSE); 82258924Seric 8236980Seric /* do the delivery */ 82458915Seric sendall(e, SM_DELIVER); 8256980Seric 8266980Seric /* finish up and exit */ 82758884Seric if (forkflag) 82824941Seric finis(); 82924941Seric else 83055012Seric dropenvelope(e); 8314632Seric } 83224941Seric else 83324941Seric { 83424941Seric /* 83524941Seric ** Parent -- pick up results. 83624941Seric */ 8374632Seric 83824941Seric errno = 0; 83924941Seric (void) waitfor(i); 84024941Seric } 8414632Seric } 8424632Seric /* 8434632Seric ** READQF -- read queue file and set up environment. 8444632Seric ** 8454632Seric ** Parameters: 8469377Seric ** e -- the envelope of the job to run. 8474632Seric ** 8484632Seric ** Returns: 84951920Seric ** TRUE if it successfully read the queue file. 85051920Seric ** FALSE otherwise. 8514632Seric ** 8524632Seric ** Side Effects: 85351920Seric ** The queue file is returned locked. 8544632Seric */ 8554632Seric 85651920Seric bool 85751920Seric readqf(e) 8589377Seric register ENVELOPE *e; 8594632Seric { 86017477Seric register FILE *qfp; 86154974Seric ADDRESS *ctladdr; 86256400Seric struct stat st; 86357135Seric char *bp; 86458915Seric char qf[20]; 86557135Seric char buf[MAXLINE]; 8669348Seric extern char *fgetfolded(); 86724954Seric extern long atol(); 86854974Seric extern ADDRESS *setctluser(); 86958689Seric extern bool lockfile(); 8704632Seric 8714632Seric /* 87217468Seric ** Read and process the file. 8734632Seric */ 8744632Seric 87558915Seric strcpy(qf, queuename(e, 'q')); 87651937Seric qfp = fopen(qf, "r+"); 87717477Seric if (qfp == NULL) 87817477Seric { 87958884Seric if (tTd(40, 8)) 88058884Seric printf("readqf(%s): fopen failure (%s)\n", 88158884Seric qf, errstring(errno)); 88240934Srick if (errno != ENOENT) 88340934Srick syserr("readqf: no control file %s", qf); 88451920Seric return FALSE; 88517477Seric } 88640934Srick 88756400Seric /* 88856400Seric ** Check the queue file for plausibility to avoid attacks. 88956400Seric */ 89056400Seric 89156400Seric if (fstat(fileno(qfp), &st) < 0) 89256400Seric { 89356400Seric /* must have been being processed by someone else */ 89458884Seric if (tTd(40, 8)) 89558884Seric printf("readqf(%s): fstat failure (%s)\n", 89658884Seric qf, errstring(errno)); 89756400Seric fclose(qfp); 89856400Seric return FALSE; 89956400Seric } 90056400Seric 90157135Seric if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 90256400Seric { 90356400Seric # ifdef LOG 90456400Seric if (LogLevel > 0) 90556400Seric { 90656400Seric syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 90756400Seric e->e_id, st.st_uid, st.st_mode); 90856400Seric } 90956795Seric # endif /* LOG */ 91058884Seric if (tTd(40, 8)) 91158884Seric printf("readqf(%s): bogus file\n", qf); 91256400Seric fclose(qfp); 91356400Seric return FALSE; 91456400Seric } 91556400Seric 91658689Seric if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 91740934Srick { 91858689Seric /* being processed by another queuer */ 91958884Seric if (tTd(40, 8)) 92058884Seric printf("readqf(%s): locked\n", qf); 92158689Seric if (Verbose) 92258689Seric printf("%s: locked\n", e->e_id); 92351920Seric # ifdef LOG 92458689Seric if (LogLevel > 19) 92558689Seric syslog(LOG_DEBUG, "%s: locked", e->e_id); 92656795Seric # endif /* LOG */ 92740934Srick (void) fclose(qfp); 92851920Seric return FALSE; 92940934Srick } 93040934Srick 93151920Seric /* save this lock */ 93251920Seric e->e_lockfp = qfp; 93351920Seric 93440934Srick /* do basic system initialization */ 93555012Seric initsys(e); 93640934Srick 93717477Seric FileName = qf; 9389377Seric LineNumber = 0; 93951920Seric if (Verbose) 9409377Seric printf("\nRunning %s\n", e->e_id); 94154974Seric ctladdr = NULL; 94257135Seric while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 9434632Seric { 94458737Seric register char *p; 94557529Seric struct stat st; 94657529Seric 94726504Seric if (tTd(40, 4)) 94857135Seric printf("+++++ %s\n", bp); 94957135Seric switch (bp[0]) 9504632Seric { 95140973Sbostic case 'C': /* specify controlling user */ 95257135Seric ctladdr = setctluser(&bp[1]); 95340973Sbostic break; 95440973Sbostic 9554632Seric case 'R': /* specify recipient */ 95658082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 9574632Seric break; 9584632Seric 95925687Seric case 'E': /* specify error recipient */ 96058082Seric (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 96125687Seric break; 96225687Seric 9634632Seric case 'H': /* header */ 96457135Seric (void) chompheader(&bp[1], FALSE, e); 9654632Seric break; 9664632Seric 96710108Seric case 'M': /* message */ 96857135Seric e->e_message = newstr(&bp[1]); 96910108Seric break; 97010108Seric 9714632Seric case 'S': /* sender */ 97258704Seric setsender(newstr(&bp[1]), e, NULL, TRUE); 9734632Seric break; 9744632Seric 975*59093Seric case 'B': /* body type */ 976*59093Seric e->e_bodytype = newstr(&bp[1]); 977*59093Seric break; 978*59093Seric 9794632Seric case 'D': /* data file name */ 98057135Seric e->e_df = newstr(&bp[1]); 9819544Seric e->e_dfp = fopen(e->e_df, "r"); 9829544Seric if (e->e_dfp == NULL) 98358020Seric { 9849377Seric syserr("readqf: cannot open %s", e->e_df); 98558020Seric e->e_msgsize = -1; 98658020Seric } 98758020Seric else if (fstat(fileno(e->e_dfp), &st) >= 0) 98857529Seric e->e_msgsize = st.st_size; 9894632Seric break; 9904632Seric 9917860Seric case 'T': /* init time */ 99257135Seric e->e_ctime = atol(&bp[1]); 9934632Seric break; 9944632Seric 9954634Seric case 'P': /* message priority */ 99657135Seric e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 9974634Seric break; 9984634Seric 99958737Seric case 'F': /* flag bits */ 100058737Seric for (p = &bp[1]; *p != '\0'; p++) 100158737Seric { 100258737Seric switch (*p) 100358737Seric { 100458737Seric case 'w': /* warning sent */ 100558737Seric e->e_flags |= EF_WARNING; 100658737Seric break; 100758737Seric 100858737Seric case 'r': /* response */ 100958737Seric e->e_flags |= EF_RESPONSE; 101058737Seric break; 101158737Seric } 101258737Seric } 101358737Seric break; 101458737Seric 101553400Seric case '$': /* define macro */ 101657135Seric define(bp[1], newstr(&bp[2]), e); 101753400Seric break; 101853400Seric 101924941Seric case '\0': /* blank line; ignore */ 102024941Seric break; 102124941Seric 10224632Seric default: 102324941Seric syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 102457135Seric LineNumber, bp); 10254632Seric break; 10264632Seric } 102757135Seric 102857135Seric if (bp != buf) 102957135Seric free(bp); 10304632Seric } 10319377Seric 10329377Seric FileName = NULL; 103324941Seric 103424941Seric /* 103524941Seric ** If we haven't read any lines, this queue file is empty. 103624941Seric ** Arrange to remove it without referencing any null pointers. 103724941Seric */ 103824941Seric 103924941Seric if (LineNumber == 0) 104024941Seric { 104124941Seric errno = 0; 104224941Seric e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 104324941Seric } 104451920Seric return TRUE; 10454632Seric } 10464632Seric /* 10479630Seric ** PRINTQUEUE -- print out a representation of the mail queue 10489630Seric ** 10499630Seric ** Parameters: 10509630Seric ** none. 10519630Seric ** 10529630Seric ** Returns: 10539630Seric ** none. 10549630Seric ** 10559630Seric ** Side Effects: 10569630Seric ** Prints a listing of the mail queue on the standard output. 10579630Seric */ 10585182Seric 10599630Seric printqueue() 10609630Seric { 10619630Seric register WORK *w; 10629630Seric FILE *f; 106310070Seric int nrequests; 10649630Seric char buf[MAXLINE]; 10659630Seric 10669630Seric /* 106758250Seric ** Check for permission to print the queue 106858250Seric */ 106958250Seric 107058523Seric if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 107158250Seric { 107258250Seric struct stat st; 107358523Seric # ifdef NGROUPS 107458523Seric int n; 107558523Seric int gidset[NGROUPS]; 107658523Seric # endif 107758250Seric 107858517Seric if (stat(QueueDir, &st) < 0) 107958250Seric { 108058250Seric syserr("Cannot stat %s", QueueDir); 108158250Seric return; 108258250Seric } 108358523Seric # ifdef NGROUPS 108458523Seric n = getgroups(NGROUPS, gidset); 108558523Seric while (--n >= 0) 108658523Seric { 108758523Seric if (gidset[n] == st.st_gid) 108858523Seric break; 108958523Seric } 109058523Seric if (n < 0) 109158523Seric # else 109258250Seric if (getgid() != st.st_gid) 109358523Seric # endif 109458250Seric { 109558250Seric usrerr("510 You are not permitted to see the queue"); 109658250Seric setstat(EX_NOPERM); 109758250Seric return; 109858250Seric } 109958250Seric } 110058250Seric 110158250Seric /* 11029630Seric ** Read and order the queue. 11039630Seric */ 11049630Seric 110524941Seric nrequests = orderq(TRUE); 11069630Seric 11079630Seric /* 11089630Seric ** Print the work list that we have read. 11099630Seric */ 11109630Seric 11119630Seric /* first see if there is anything */ 111210070Seric if (nrequests <= 0) 11139630Seric { 111410070Seric printf("Mail queue is empty\n"); 11159630Seric return; 11169630Seric } 11179630Seric 111851920Seric CurrentLA = getla(); /* get load average */ 111940934Srick 112010096Seric printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 112125687Seric if (nrequests > QUEUESIZE) 112225687Seric printf(", only %d printed", QUEUESIZE); 112324979Seric if (Verbose) 112458716Seric printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 112524979Seric else 112658716Seric printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 11279630Seric for (w = WorkQ; w != NULL; w = w->w_next) 11289630Seric { 11299630Seric struct stat st; 113010070Seric auto time_t submittime = 0; 113110070Seric long dfsize = -1; 113258737Seric int flags = 0; 113310108Seric char message[MAXLINE]; 1134*59093Seric char bodytype[MAXNAME]; 113524941Seric extern bool shouldqueue(); 113658689Seric extern bool lockfile(); 11379630Seric 113817468Seric f = fopen(w->w_name, "r"); 113917468Seric if (f == NULL) 114017468Seric { 114117468Seric errno = 0; 114217468Seric continue; 114317468Seric } 114458724Seric printf("%8s", w->w_name + 2); 114558689Seric if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 114610070Seric printf("*"); 114757438Seric else if (shouldqueue(w->w_pri, w->w_ctime)) 114824941Seric printf("X"); 114910070Seric else 115010070Seric printf(" "); 115110070Seric errno = 0; 115217468Seric 1153*59093Seric message[0] = bodytype[0] = '\0'; 11549630Seric while (fgets(buf, sizeof buf, f) != NULL) 11559630Seric { 115653400Seric register int i; 115758737Seric register char *p; 115853400Seric 11599630Seric fixcrlf(buf, TRUE); 11609630Seric switch (buf[0]) 11619630Seric { 116210108Seric case 'M': /* error message */ 116353400Seric if ((i = strlen(&buf[1])) >= sizeof message) 116458737Seric i = sizeof message - 1; 116553400Seric bcopy(&buf[1], message, i); 116653400Seric message[i] = '\0'; 116710108Seric break; 116810108Seric 1169*59093Seric case 'B': /* body type */ 1170*59093Seric if ((i = strlen(&buf[1])) >= sizeof bodytype) 1171*59093Seric i = sizeof bodytype - 1; 1172*59093Seric bcopy(&buf[1], bodytype, i); 1173*59093Seric bodytype[i] = '\0'; 1174*59093Seric break; 1175*59093Seric 11769630Seric case 'S': /* sender name */ 117724979Seric if (Verbose) 117858737Seric printf("%8ld %10ld%c%.12s %.38s", 117958737Seric dfsize, 118058737Seric w->w_pri, 118158737Seric bitset(EF_WARNING, flags) ? '+' : ' ', 118258737Seric ctime(&submittime) + 4, 118324979Seric &buf[1]); 118424979Seric else 118524979Seric printf("%8ld %.16s %.45s", dfsize, 118624979Seric ctime(&submittime), &buf[1]); 1187*59093Seric if (message[0] != '\0' || bodytype[0] != '\0') 1188*59093Seric { 1189*59093Seric printf("\n %10.10s", bodytype); 1190*59093Seric if (message[0] != '\0') 1191*59093Seric printf(" (%.60s)", message); 1192*59093Seric } 11939630Seric break; 119451920Seric 119540973Sbostic case 'C': /* controlling user */ 119654974Seric if (Verbose) 119758716Seric printf("\n\t\t\t\t (---%.34s---)", 119858716Seric &buf[1]); 119940973Sbostic break; 12009630Seric 12019630Seric case 'R': /* recipient name */ 120224979Seric if (Verbose) 120358716Seric printf("\n\t\t\t\t\t %.38s", &buf[1]); 120424979Seric else 120558716Seric printf("\n\t\t\t\t %.45s", &buf[1]); 12069630Seric break; 12079630Seric 12089630Seric case 'T': /* creation time */ 120924941Seric submittime = atol(&buf[1]); 12109630Seric break; 121110070Seric 121210070Seric case 'D': /* data file name */ 121310070Seric if (stat(&buf[1], &st) >= 0) 121410070Seric dfsize = st.st_size; 121510070Seric break; 121658737Seric 121758737Seric case 'F': /* flag bits */ 121858737Seric for (p = &buf[1]; *p != '\0'; p++) 121958737Seric { 122058737Seric switch (*p) 122158737Seric { 122258737Seric case 'w': 122358737Seric flags |= EF_WARNING; 122458737Seric break; 122558737Seric } 122658737Seric } 12279630Seric } 12289630Seric } 122910070Seric if (submittime == (time_t) 0) 123010070Seric printf(" (no control file)"); 12319630Seric printf("\n"); 123223098Seric (void) fclose(f); 12339630Seric } 12349630Seric } 12359630Seric 123656795Seric # endif /* QUEUE */ 123717468Seric /* 123817468Seric ** QUEUENAME -- build a file name in the queue directory for this envelope. 123917468Seric ** 124017468Seric ** Assigns an id code if one does not already exist. 124117468Seric ** This code is very careful to avoid trashing existing files 124217468Seric ** under any circumstances. 124317468Seric ** 124417468Seric ** Parameters: 124517468Seric ** e -- envelope to build it in/from. 124617468Seric ** type -- the file type, used as the first character 124717468Seric ** of the file name. 124817468Seric ** 124917468Seric ** Returns: 125017468Seric ** a pointer to the new file name (in a static buffer). 125117468Seric ** 125217468Seric ** Side Effects: 125351920Seric ** If no id code is already assigned, queuename will 125451920Seric ** assign an id code, create a qf file, and leave a 125551920Seric ** locked, open-for-write file pointer in the envelope. 125617468Seric */ 125717468Seric 125817468Seric char * 125917468Seric queuename(e, type) 126017468Seric register ENVELOPE *e; 126117468Seric char type; 126217468Seric { 126317468Seric static int pid = -1; 126458741Seric static char c0; 126558741Seric static char c1; 126658741Seric static char c2; 126758716Seric time_t now; 126858716Seric struct tm *tm; 126958689Seric static char buf[MAXNAME]; 127058689Seric extern bool lockfile(); 127117468Seric 127217468Seric if (e->e_id == NULL) 127317468Seric { 127417468Seric char qf[20]; 127517468Seric 127617468Seric /* find a unique id */ 127717468Seric if (pid != getpid()) 127817468Seric { 127917468Seric /* new process -- start back at "AA" */ 128017468Seric pid = getpid(); 128158716Seric now = curtime(); 128258716Seric tm = localtime(&now); 128358716Seric c0 = 'A' + tm->tm_hour; 128417468Seric c1 = 'A'; 128517468Seric c2 = 'A' - 1; 128617468Seric } 128758716Seric (void) sprintf(qf, "qf%cAA%05d", c0, pid); 128817468Seric 128917468Seric while (c1 < '~' || c2 < 'Z') 129017468Seric { 129117468Seric int i; 129217468Seric 129317468Seric if (c2 >= 'Z') 129417468Seric { 129517468Seric c1++; 129617468Seric c2 = 'A' - 1; 129717468Seric } 129858716Seric qf[3] = c1; 129958716Seric qf[4] = ++c2; 130017468Seric if (tTd(7, 20)) 130140934Srick printf("queuename: trying \"%s\"\n", qf); 130217468Seric 130340934Srick i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 130451920Seric if (i < 0) 130551920Seric { 130651920Seric if (errno == EEXIST) 130751920Seric continue; 130851920Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 130951920Seric qf, QueueDir); 131051920Seric exit(EX_UNAVAILABLE); 131151920Seric } 131258689Seric if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 131351920Seric { 131451920Seric e->e_lockfp = fdopen(i, "w"); 131540934Srick break; 131617468Seric } 131751920Seric 131851920Seric /* a reader got the file; abandon it and try again */ 131951920Seric (void) close(i); 132017468Seric } 132117468Seric if (c1 >= '~' && c2 >= 'Z') 132217468Seric { 132317468Seric syserr("queuename: Cannot create \"%s\" in \"%s\"", 132417468Seric qf, QueueDir); 132517468Seric exit(EX_OSERR); 132617468Seric } 132717468Seric e->e_id = newstr(&qf[2]); 132817468Seric define('i', e->e_id, e); 132917468Seric if (tTd(7, 1)) 133017468Seric printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 133117468Seric # ifdef LOG 133258020Seric if (LogLevel > 93) 133317468Seric syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 133456795Seric # endif /* LOG */ 133517468Seric } 133617468Seric 133717468Seric if (type == '\0') 133817468Seric return (NULL); 133917468Seric (void) sprintf(buf, "%cf%s", type, e->e_id); 134017468Seric if (tTd(7, 2)) 134117468Seric printf("queuename: %s\n", buf); 134217468Seric return (buf); 134317468Seric } 134417468Seric /* 134517468Seric ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 134617468Seric ** 134717468Seric ** Parameters: 134817468Seric ** e -- the envelope to unlock. 134917468Seric ** 135017468Seric ** Returns: 135117468Seric ** none 135217468Seric ** 135317468Seric ** Side Effects: 135417468Seric ** unlocks the queue for `e'. 135517468Seric */ 135617468Seric 135717468Seric unlockqueue(e) 135817468Seric ENVELOPE *e; 135917468Seric { 136058680Seric if (tTd(51, 4)) 136158680Seric printf("unlockqueue(%s)\n", e->e_id); 136258680Seric 136351920Seric /* if there is a lock file in the envelope, close it */ 136451920Seric if (e->e_lockfp != NULL) 136558680Seric xfclose(e->e_lockfp, "unlockqueue", e->e_id); 136651920Seric e->e_lockfp = NULL; 136751920Seric 136858728Seric /* don't create a queue id if we don't already have one */ 136958728Seric if (e->e_id == NULL) 137058728Seric return; 137158728Seric 137217468Seric /* remove the transcript */ 137317468Seric # ifdef LOG 137458020Seric if (LogLevel > 87) 137517468Seric syslog(LOG_DEBUG, "%s: unlock", e->e_id); 137656795Seric # endif /* LOG */ 137758680Seric if (!tTd(51, 104)) 137817468Seric xunlink(queuename(e, 'x')); 137917468Seric 138017468Seric } 138140973Sbostic /* 138254974Seric ** SETCTLUSER -- create a controlling address 138340973Sbostic ** 138454974Seric ** Create a fake "address" given only a local login name; this is 138554974Seric ** used as a "controlling user" for future recipient addresses. 138640973Sbostic ** 138740973Sbostic ** Parameters: 138854974Seric ** user -- the user name of the controlling user. 138940973Sbostic ** 139040973Sbostic ** Returns: 139154974Seric ** An address descriptor for the controlling user. 139240973Sbostic ** 139340973Sbostic ** Side Effects: 139440973Sbostic ** none. 139540973Sbostic */ 139640973Sbostic 139754974Seric ADDRESS * 139854974Seric setctluser(user) 139954974Seric char *user; 140040973Sbostic { 140154974Seric register ADDRESS *a; 140240973Sbostic struct passwd *pw; 140340973Sbostic 140440973Sbostic /* 140554974Seric ** See if this clears our concept of controlling user. 140640973Sbostic */ 140740973Sbostic 140854974Seric if (user == NULL || *user == '\0') 140958704Seric user = DefUser; 141040973Sbostic 141140973Sbostic /* 141254974Seric ** Set up addr fields for controlling user. 141340973Sbostic */ 141440973Sbostic 141554974Seric a = (ADDRESS *) xalloc(sizeof *a); 141654974Seric bzero((char *) a, sizeof *a); 141754974Seric if ((pw = getpwnam(user)) != NULL) 141840973Sbostic { 141940973Sbostic a->q_home = newstr(pw->pw_dir); 142040973Sbostic a->q_uid = pw->pw_uid; 142140973Sbostic a->q_gid = pw->pw_gid; 142257642Seric a->q_user = newstr(user); 142340973Sbostic } 142440973Sbostic else 142540973Sbostic { 142640973Sbostic a->q_uid = DefUid; 142740973Sbostic a->q_gid = DefGid; 142857642Seric a->q_user = newstr(DefUser); 142940973Sbostic } 143040973Sbostic 143158294Seric a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 143256328Seric a->q_mailer = LocalMailer; 143354974Seric return a; 143440973Sbostic } 1435