xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58689)
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*58689Seric static char sccsid[] = "@(#)queue.c	6.27 (Berkeley) 03/17/93 (with queueing)";
1433731Sbostic #else
15*58689Seric static char sccsid[] = "@(#)queue.c	6.27 (Berkeley) 03/17/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>
2557977Seric # ifndef MAXNAMLEN
2657736Seric # include <dirent.h>
2757736Seric # 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 		{
111*58689Seric 			extern bool lockfile();
11251937Seric 
11351920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
11451920Seric 			if (fd < 0)
11551835Seric 			{
11651920Seric 				if (errno == EEXIST)
11751920Seric 					continue;
11851920Seric 				syserr("queueup: cannot create temp file %s", tf);
11951920Seric 				return;
12041636Srick 			}
121*58689Seric 
122*58689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
12351920Seric 				break;
124*58689Seric 
12551920Seric 			close(fd);
12641636Srick 		}
12741636Srick 
12851920Seric 		tfp = fdopen(fd, "w");
12951920Seric 	}
1304632Seric 
1317677Seric 	if (tTd(40, 1))
13217468Seric 		printf("queueing %s\n", e->e_id);
1334632Seric 
1344632Seric 	/*
1356980Seric 	**  If there is no data file yet, create one.
1366980Seric 	*/
1376980Seric 
1386980Seric 	if (e->e_df == NULL)
1396980Seric 	{
1406980Seric 		register FILE *dfp;
1419389Seric 		extern putbody();
1426980Seric 
1437812Seric 		e->e_df = newstr(queuename(e, 'd'));
14440934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14540934Srick 		if (fd < 0)
1466980Seric 		{
1476980Seric 			syserr("queueup: cannot create %s", e->e_df);
14851920Seric 			if (!newid)
14958680Seric 				(void) xfclose(tfp, "queueup tfp", e->e_id);
15051920Seric 			return;
1516980Seric 		}
15240934Srick 		dfp = fdopen(fd, "w");
15310173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
15458680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1559389Seric 		e->e_putbody = putbody;
1566980Seric 	}
1576980Seric 
1586980Seric 	/*
1594632Seric 	**  Output future work requests.
16025687Seric 	**	Priority and creation time should be first, since
16125687Seric 	**	they are required by orderq.
1624632Seric 	*/
1634632Seric 
1649377Seric 	/* output message priority */
1659377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1669377Seric 
1679630Seric 	/* output creation time */
1689630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1699630Seric 
1704632Seric 	/* output name of data file */
1717812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1724632Seric 
17310108Seric 	/* message from envelope, if it exists */
17410108Seric 	if (e->e_message != NULL)
17510108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17610108Seric 
17753400Seric 	/* $r and $s macro values */
17853400Seric 	if ((p = macvalue('r', e)) != NULL)
17953400Seric 		fprintf(tfp, "$r%s\n", p);
18053400Seric 	if ((p = macvalue('s', e)) != NULL)
18153400Seric 		fprintf(tfp, "$s%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 == NULL && q->q_alias != NULL)
19655360Seric 				ctladdr = nullctladdr;
19755360Seric 			if (ctladdr != lastctladdr)
19855360Seric 			{
19955360Seric 				printctladdr(ctladdr, tfp);
20055360Seric 				lastctladdr = ctladdr;
20155360Seric 			}
20255360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
20355360Seric 		}
20455360Seric 	}
20555360Seric 
2064632Seric 	/* output list of recipient addresses */
2076980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2084632Seric 	{
20958250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21058680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2118245Seric 		{
21254974Seric 			ADDRESS *ctladdr;
21340973Sbostic 
21454975Seric 			ctladdr = getctladdr(q);
21554975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
21654975Seric 				ctladdr = nullctladdr;
21754975Seric 			if (ctladdr != lastctladdr)
21854974Seric 			{
21954974Seric 				printctladdr(ctladdr, tfp);
22054974Seric 				lastctladdr = ctladdr;
22154974Seric 			}
2227812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2239377Seric 			if (announce)
2249377Seric 			{
2259377Seric 				e->e_to = q->q_paddr;
22658151Seric 				message("queued");
22758020Seric 				if (LogLevel > 8)
22858337Seric 					logdelivery(NULL, NULL, "queued", e);
2299377Seric 				e->e_to = NULL;
2309377Seric 			}
2319387Seric 			if (tTd(40, 1))
2329387Seric 			{
2339387Seric 				printf("queueing ");
2349387Seric 				printaddr(q, FALSE);
2359387Seric 			}
2368245Seric 		}
2374632Seric 	}
2384632Seric 
2399377Seric 	/*
2409377Seric 	**  Output headers for this message.
2419377Seric 	**	Expand macros completely here.  Queue run will deal with
2429377Seric 	**	everything as absolute headers.
2439377Seric 	**		All headers that must be relative to the recipient
2449377Seric 	**		can be cracked later.
24510173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24610173Seric 	**	no effect on the addresses as they are output.
2479377Seric 	*/
2489377Seric 
24910686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
25058020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
25158020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
25210349Seric 	nullmailer.m_eol = "\n";
25310173Seric 
25458050Seric 	define('g', "\201f", e);
2556980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2564632Seric 	{
25710686Seric 		extern bool bitzerop();
25810686Seric 
25912015Seric 		/* don't output null headers */
2604632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2614632Seric 			continue;
26212015Seric 
26312015Seric 		/* don't output resent headers on non-resent messages */
26412015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
26512015Seric 			continue;
26612015Seric 
26712015Seric 		/* output this header */
2687812Seric 		fprintf(tfp, "H");
26912015Seric 
27012015Seric 		/* if conditional, output the set of conditions */
27110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
27210686Seric 		{
27310686Seric 			int j;
27410686Seric 
27523098Seric 			(void) putc('?', tfp);
27610686Seric 			for (j = '\0'; j <= '\177'; j++)
27710686Seric 				if (bitnset(j, h->h_mflags))
27823098Seric 					(void) putc(j, tfp);
27923098Seric 			(void) putc('?', tfp);
28010686Seric 		}
28112015Seric 
28212015Seric 		/* output the header: expand macros, convert addresses */
2837763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2847763Seric 		{
2857763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2868236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2877763Seric 		}
2888245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2899348Seric 		{
29055012Seric 			commaize(h, h->h_value, tfp,
29155012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
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 
30251920Seric 	if (!newid)
30351920Seric 	{
30451920Seric 		qf = queuename(e, 'q');
30551920Seric 		if (rename(tf, qf) < 0)
30651920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
30751920Seric 		if (e->e_lockfp != NULL)
30858680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
30951920Seric 		e->e_lockfp = tfp;
31051920Seric 	}
31151920Seric 	else
31251920Seric 		qf = tf;
31341636Srick 	errno = 0;
3147391Seric 
3157677Seric # ifdef LOG
3167677Seric 	/* save log info */
31758020Seric 	if (LogLevel > 79)
3187878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
31956795Seric # endif /* LOG */
32040934Srick 	fflush(tfp);
32151920Seric 	return;
3224632Seric }
32354974Seric 
32454974Seric printctladdr(a, tfp)
32554974Seric 	ADDRESS *a;
32654974Seric 	FILE *tfp;
32754974Seric {
32854974Seric 	char *u;
32954974Seric 	struct passwd *pw;
33054974Seric 	extern struct passwd *getpwuid();
33154974Seric 
33254974Seric 	if (a == NULL)
33354974Seric 	{
33454974Seric 		fprintf(tfp, "C\n");
33554974Seric 		return;
33654974Seric 	}
33754974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
33854974Seric 		u = DefUser;
33954974Seric 	else
34054974Seric 		u = pw->pw_name;
34154974Seric 	fprintf(tfp, "C%s\n", u);
34254974Seric }
34354974Seric 
3444632Seric /*
3454632Seric **  RUNQUEUE -- run the jobs in the queue.
3464632Seric **
3474632Seric **	Gets the stuff out of the queue in some presumably logical
3484632Seric **	order and processes them.
3494632Seric **
3504632Seric **	Parameters:
35124941Seric **		forkflag -- TRUE if the queue scanning should be done in
35224941Seric **			a child process.  We double-fork so it is not our
35324941Seric **			child and we don't have to clean up after it.
3544632Seric **
3554632Seric **	Returns:
3564632Seric **		none.
3574632Seric **
3584632Seric **	Side Effects:
3594632Seric **		runs things in the mail queue.
3604632Seric */
3614632Seric 
36255360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
36355360Seric 
36455360Seric runqueue(forkflag)
3654639Seric 	bool forkflag;
3664632Seric {
36724953Seric 	extern bool shouldqueue();
36855360Seric 	register ENVELOPE *e;
36955360Seric 	extern ENVELOPE BlankEnvelope;
37055360Seric 	extern ENVELOPE *newenvelope();
37124953Seric 
3727466Seric 	/*
37324953Seric 	**  If no work will ever be selected, don't even bother reading
37424953Seric 	**  the queue.
37524953Seric 	*/
37624953Seric 
37751920Seric 	CurrentLA = getla();	/* get load average */
37840934Srick 
37958132Seric 	if (shouldqueue(0L, curtime()))
38024953Seric 	{
38124953Seric 		if (Verbose)
38224953Seric 			printf("Skipping queue run -- load average too high\n");
38358107Seric 		if (forkflag && QueueIntvl != 0)
38458107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
38555360Seric 		return;
38624953Seric 	}
38724953Seric 
38824953Seric 	/*
3897466Seric 	**  See if we want to go off and do other useful work.
3907466Seric 	*/
3914639Seric 
3924639Seric 	if (forkflag)
3934639Seric 	{
3947943Seric 		int pid;
3957943Seric 
3967943Seric 		pid = dofork();
3977943Seric 		if (pid != 0)
3984639Seric 		{
39946928Sbostic 			extern void reapchild();
40025184Seric 
4017943Seric 			/* parent -- pick up intermediate zombie */
40225184Seric #ifndef SIGCHLD
4039377Seric 			(void) waitfor(pid);
40456795Seric #else /* SIGCHLD */
40525184Seric 			(void) signal(SIGCHLD, reapchild);
40656795Seric #endif /* SIGCHLD */
4077690Seric 			if (QueueIntvl != 0)
4089348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4094639Seric 			return;
4104639Seric 		}
4117943Seric 		/* child -- double fork */
41225184Seric #ifndef SIGCHLD
4137943Seric 		if (fork() != 0)
4147943Seric 			exit(EX_OK);
41556795Seric #else /* SIGCHLD */
41625184Seric 		(void) signal(SIGCHLD, SIG_DFL);
41756795Seric #endif /* SIGCHLD */
4184639Seric 	}
41924941Seric 
42040934Srick 	setproctitle("running queue: %s", QueueDir);
42124941Seric 
4227876Seric # ifdef LOG
42358020Seric 	if (LogLevel > 69)
42455360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
42555360Seric 			QueueDir, getpid(), forkflag);
42656795Seric # endif /* LOG */
4274639Seric 
4287466Seric 	/*
42910205Seric 	**  Release any resources used by the daemon code.
43010205Seric 	*/
43110205Seric 
43210205Seric # ifdef DAEMON
43310205Seric 	clrdaemon();
43456795Seric # endif /* DAEMON */
43510205Seric 
43610205Seric 	/*
43755360Seric 	**  Create ourselves an envelope
43855360Seric 	*/
43955360Seric 
44055360Seric 	CurEnv = &QueueEnvelope;
44158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
44255360Seric 	e->e_flags = BlankEnvelope.e_flags;
44355360Seric 
44455360Seric 	/*
44527175Seric 	**  Make sure the alias database is open.
44627175Seric 	*/
44727175Seric 
44855012Seric 	initaliases(AliasFile, FALSE, e);
44927175Seric 
45027175Seric 	/*
4517466Seric 	**  Start making passes through the queue.
4527466Seric 	**	First, read and sort the entire queue.
4537466Seric 	**	Then, process the work in that order.
4547466Seric 	**		But if you take too long, start over.
4557466Seric 	*/
4567466Seric 
4577943Seric 	/* order the existing work requests */
45824954Seric 	(void) orderq(FALSE);
4597690Seric 
4607943Seric 	/* process them once at a time */
4617943Seric 	while (WorkQ != NULL)
4624639Seric 	{
4637943Seric 		WORK *w = WorkQ;
4647881Seric 
4657943Seric 		WorkQ = WorkQ->w_next;
46655012Seric 		dowork(w, e);
4677943Seric 		free(w->w_name);
4687943Seric 		free((char *) w);
4694639Seric 	}
47029866Seric 
47129866Seric 	/* exit without the usual cleanup */
47255467Seric 	e->e_id = NULL;
47355467Seric 	finis();
4744634Seric }
4754634Seric /*
4764632Seric **  ORDERQ -- order the work queue.
4774632Seric **
4784632Seric **	Parameters:
47924941Seric **		doall -- if set, include everything in the queue (even
48024941Seric **			the jobs that cannot be run because the load
48124941Seric **			average is too high).  Otherwise, exclude those
48224941Seric **			jobs.
4834632Seric **
4844632Seric **	Returns:
48510121Seric **		The number of request in the queue (not necessarily
48610121Seric **		the number of requests in WorkQ however).
4874632Seric **
4884632Seric **	Side Effects:
4894632Seric **		Sets WorkQ to the queue of available work, in order.
4904632Seric */
4914632Seric 
49225687Seric # define NEED_P		001
49325687Seric # define NEED_T		002
49458318Seric # define NEED_R		004
49558318Seric # define NEED_S		010
4964632Seric 
49724941Seric orderq(doall)
49824941Seric 	bool doall;
4994632Seric {
5006625Sglickman 	register struct direct *d;
5014632Seric 	register WORK *w;
5026625Sglickman 	DIR *f;
5034632Seric 	register int i;
50425687Seric 	WORK wlist[QUEUESIZE+1];
50510070Seric 	int wn = -1;
5064632Seric 	extern workcmpf();
5074632Seric 
50858318Seric 	if (tTd(41, 1))
50958318Seric 	{
51058318Seric 		printf("orderq:\n");
51158318Seric 		if (QueueLimitId != NULL)
51258318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
51358318Seric 		if (QueueLimitSender != NULL)
51458318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
51558318Seric 		if (QueueLimitRecipient != NULL)
51658318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
51758318Seric 	}
51858318Seric 
5194632Seric 	/* clear out old WorkQ */
5204632Seric 	for (w = WorkQ; w != NULL; )
5214632Seric 	{
5224632Seric 		register WORK *nw = w->w_next;
5234632Seric 
5244632Seric 		WorkQ = nw;
5254632Seric 		free(w->w_name);
5264632Seric 		free((char *) w);
5274632Seric 		w = nw;
5284632Seric 	}
5294632Seric 
5304632Seric 	/* open the queue directory */
5318148Seric 	f = opendir(".");
5324632Seric 	if (f == NULL)
5334632Seric 	{
5348148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
53510070Seric 		return (0);
5364632Seric 	}
5374632Seric 
5384632Seric 	/*
5394632Seric 	**  Read the work directory.
5404632Seric 	*/
5414632Seric 
54210070Seric 	while ((d = readdir(f)) != NULL)
5434632Seric 	{
5449377Seric 		FILE *cf;
5454632Seric 		char lbuf[MAXNAME];
54657642Seric 		extern bool shouldqueue();
54758318Seric 		extern bool strcontainedin();
5484632Seric 
5494632Seric 		/* is this an interesting entry? */
5507812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5514632Seric 			continue;
5524632Seric 
55358318Seric 		if (QueueLimitId != NULL &&
55458318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
55558318Seric 			continue;
55658318Seric 
55758020Seric 		if (strlen(d->d_name) != 9)
55858020Seric 		{
55958020Seric 			if (Verbose)
56058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
56158020Seric #ifdef LOG
56258020Seric 			if (LogLevel > 3)
56358020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
56458020Seric 					d->d_name);
56558020Seric #endif
56658020Seric 			if (strlen(d->d_name) >= MAXNAME)
56758020Seric 				d->d_name[MAXNAME - 1] = '\0';
56858020Seric 			strcpy(lbuf, d->d_name);
56958020Seric 			lbuf[0] = 'Q';
57058020Seric 			(void) rename(d->d_name, lbuf);
57158020Seric 			continue;
57258020Seric 		}
57358020Seric 
57410070Seric 		/* yes -- open control file (if not too many files) */
57525687Seric 		if (++wn >= QUEUESIZE)
57610070Seric 			continue;
57758318Seric 
5788148Seric 		cf = fopen(d->d_name, "r");
5794632Seric 		if (cf == NULL)
5804632Seric 		{
5817055Seric 			/* this may be some random person sending hir msgs */
5827055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
58310090Seric 			if (tTd(41, 2))
58410090Seric 				printf("orderq: cannot open %s (%d)\n",
58510090Seric 					d->d_name, errno);
5867055Seric 			errno = 0;
58710090Seric 			wn--;
5884632Seric 			continue;
5894632Seric 		}
59025687Seric 		w = &wlist[wn];
59125687Seric 		w->w_name = newstr(d->d_name);
5924632Seric 
59325027Seric 		/* make sure jobs in creation don't clog queue */
59425687Seric 		w->w_pri = 0x7fffffff;
59525687Seric 		w->w_ctime = 0;
59625027Seric 
5974632Seric 		/* extract useful information */
59825687Seric 		i = NEED_P | NEED_T;
59958318Seric 		if (QueueLimitSender != NULL)
60058318Seric 			i |= NEED_S;
60158318Seric 		if (QueueLimitRecipient != NULL)
60258318Seric 			i |= NEED_R;
60325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6044632Seric 		{
60524954Seric 			extern long atol();
60658318Seric 			extern bool strcontainedin();
60724954Seric 
60824941Seric 			switch (lbuf[0])
6094632Seric 			{
61024941Seric 			  case 'P':
61125687Seric 				w->w_pri = atol(&lbuf[1]);
61225687Seric 				i &= ~NEED_P;
6134632Seric 				break;
61425013Seric 
61525013Seric 			  case 'T':
61625687Seric 				w->w_ctime = atol(&lbuf[1]);
61725687Seric 				i &= ~NEED_T;
61825013Seric 				break;
61958318Seric 
62058318Seric 			  case 'R':
62158318Seric 				if (QueueLimitRecipient != NULL &&
62258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
62358318Seric 					i &= ~NEED_R;
62458318Seric 				break;
62558318Seric 
62658318Seric 			  case 'S':
62758318Seric 				if (QueueLimitSender != NULL &&
62858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
62958318Seric 					i &= ~NEED_S;
63058318Seric 				break;
6314632Seric 			}
6324632Seric 		}
6334632Seric 		(void) fclose(cf);
63424953Seric 
63558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
63658318Seric 		    bitset(NEED_R|NEED_S, i))
63724953Seric 		{
63824953Seric 			/* don't even bother sorting this job in */
63924953Seric 			wn--;
64024953Seric 		}
6414632Seric 	}
6426625Sglickman 	(void) closedir(f);
64310090Seric 	wn++;
6444632Seric 
6454632Seric 	/*
6464632Seric 	**  Sort the work directory.
6474632Seric 	*/
6484632Seric 
64925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6504632Seric 
6514632Seric 	/*
6524632Seric 	**  Convert the work list into canonical form.
6539377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6544632Seric 	*/
6554632Seric 
65624981Seric 	WorkQ = NULL;
65725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6584632Seric 	{
6594632Seric 		w = (WORK *) xalloc(sizeof *w);
6604632Seric 		w->w_name = wlist[i].w_name;
6614632Seric 		w->w_pri = wlist[i].w_pri;
66225013Seric 		w->w_ctime = wlist[i].w_ctime;
66324981Seric 		w->w_next = WorkQ;
66424981Seric 		WorkQ = w;
6654632Seric 	}
6664632Seric 
6677677Seric 	if (tTd(40, 1))
6684632Seric 	{
6694632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6705037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6714632Seric 	}
67210070Seric 
67310090Seric 	return (wn);
6744632Seric }
6754632Seric /*
6767677Seric **  WORKCMPF -- compare function for ordering work.
6774632Seric **
6784632Seric **	Parameters:
6794632Seric **		a -- the first argument.
6804632Seric **		b -- the second argument.
6814632Seric **
6824632Seric **	Returns:
68324981Seric **		-1 if a < b
68424981Seric **		 0 if a == b
68524981Seric **		+1 if a > b
6864632Seric **
6874632Seric **	Side Effects:
6884632Seric **		none.
6894632Seric */
6904632Seric 
6914632Seric workcmpf(a, b)
6925037Seric 	register WORK *a;
6935037Seric 	register WORK *b;
6944632Seric {
69557438Seric 	long pa = a->w_pri;
69657438Seric 	long pb = b->w_pri;
69724941Seric 
69824941Seric 	if (pa == pb)
6994632Seric 		return (0);
70024941Seric 	else if (pa > pb)
70124981Seric 		return (1);
70224981Seric 	else
70310121Seric 		return (-1);
7044632Seric }
7054632Seric /*
7064632Seric **  DOWORK -- do a work request.
7074632Seric **
7084632Seric **	Parameters:
7094632Seric **		w -- the work request to be satisfied.
7104632Seric **
7114632Seric **	Returns:
7124632Seric **		none.
7134632Seric **
7144632Seric **	Side Effects:
7154632Seric **		The work request is satisfied if possible.
7164632Seric */
7174632Seric 
71855012Seric dowork(w, e)
7194632Seric 	register WORK *w;
72055012Seric 	register ENVELOPE *e;
7214632Seric {
7224632Seric 	register int i;
72324941Seric 	extern bool shouldqueue();
72451920Seric 	extern bool readqf();
7254632Seric 
7267677Seric 	if (tTd(40, 1))
7275037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7284632Seric 
7294632Seric 	/*
73024941Seric 	**  Ignore jobs that are too expensive for the moment.
7314632Seric 	*/
7324632Seric 
73357438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7344632Seric 	{
73524941Seric 		if (Verbose)
73624967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7374632Seric 		return;
7384632Seric 	}
7394632Seric 
74024941Seric 	/*
74124941Seric 	**  Fork for work.
74224941Seric 	*/
74324941Seric 
74424941Seric 	if (ForkQueueRuns)
74524941Seric 	{
74624941Seric 		i = fork();
74724941Seric 		if (i < 0)
74824941Seric 		{
74924941Seric 			syserr("dowork: cannot fork");
75024941Seric 			return;
75124941Seric 		}
75224941Seric 	}
75324941Seric 	else
75424941Seric 	{
75524941Seric 		i = 0;
75624941Seric 	}
75724941Seric 
7584632Seric 	if (i == 0)
7594632Seric 	{
7604632Seric 		/*
7614632Seric 		**  CHILD
7628148Seric 		**	Lock the control file to avoid duplicate deliveries.
7638148Seric 		**		Then run the file as though we had just read it.
7647350Seric 		**	We save an idea of the temporary name so we
7657350Seric 		**		can recover on interrupt.
7664632Seric 		*/
7674632Seric 
7687763Seric 		/* set basic modes, etc. */
7697356Seric 		(void) alarm(0);
77055012Seric 		clearenvelope(e, FALSE);
7714632Seric 		QueueRun = TRUE;
7729377Seric 		ErrorMode = EM_MAIL;
77355012Seric 		e->e_id = &w->w_name[2];
7747876Seric # ifdef LOG
77558020Seric 		if (LogLevel > 76)
77655012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7777881Seric 			       getpid());
77856795Seric # endif /* LOG */
7797763Seric 
7807763Seric 		/* don't use the headers from sendmail.cf... */
78155012Seric 		e->e_header = NULL;
7827763Seric 
78351920Seric 		/* read the queue control file -- return if locked */
78455012Seric 		if (!readqf(e))
7856980Seric 		{
78624941Seric 			if (ForkQueueRuns)
78724941Seric 				exit(EX_OK);
78824941Seric 			else
78924941Seric 				return;
7906980Seric 		}
7916980Seric 
79255012Seric 		e->e_flags |= EF_INQUEUE;
79357642Seric 		eatheader(e, TRUE);
7946980Seric 
7956980Seric 		/* do the delivery */
79655012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
79755012Seric 			sendall(e, SM_DELIVER);
7986980Seric 
7996980Seric 		/* finish up and exit */
80024941Seric 		if (ForkQueueRuns)
80124941Seric 			finis();
80224941Seric 		else
80355012Seric 			dropenvelope(e);
8044632Seric 	}
80524941Seric 	else
80624941Seric 	{
80724941Seric 		/*
80824941Seric 		**  Parent -- pick up results.
80924941Seric 		*/
8104632Seric 
81124941Seric 		errno = 0;
81224941Seric 		(void) waitfor(i);
81324941Seric 	}
8144632Seric }
8154632Seric /*
8164632Seric **  READQF -- read queue file and set up environment.
8174632Seric **
8184632Seric **	Parameters:
8199377Seric **		e -- the envelope of the job to run.
8204632Seric **
8214632Seric **	Returns:
82251920Seric **		TRUE if it successfully read the queue file.
82351920Seric **		FALSE otherwise.
8244632Seric **
8254632Seric **	Side Effects:
82651920Seric **		The queue file is returned locked.
8274632Seric */
8284632Seric 
82951920Seric bool
83051920Seric readqf(e)
8319377Seric 	register ENVELOPE *e;
8324632Seric {
83317477Seric 	char *qf;
83417477Seric 	register FILE *qfp;
83554974Seric 	ADDRESS *ctladdr;
83656400Seric 	struct stat st;
83757135Seric 	char *bp;
83857135Seric 	char buf[MAXLINE];
8399348Seric 	extern char *fgetfolded();
84024954Seric 	extern long atol();
84154974Seric 	extern ADDRESS *setctluser();
842*58689Seric 	extern bool lockfile();
8434632Seric 
8444632Seric 	/*
84517468Seric 	**  Read and process the file.
8464632Seric 	*/
8474632Seric 
84817477Seric 	qf = queuename(e, 'q');
84951937Seric 	qfp = fopen(qf, "r+");
85017477Seric 	if (qfp == NULL)
85117477Seric 	{
85240934Srick 		if (errno != ENOENT)
85340934Srick 			syserr("readqf: no control file %s", qf);
85451920Seric 		return FALSE;
85517477Seric 	}
85640934Srick 
85756400Seric 	/*
85856400Seric 	**  Check the queue file for plausibility to avoid attacks.
85956400Seric 	*/
86056400Seric 
86156400Seric 	if (fstat(fileno(qfp), &st) < 0)
86256400Seric 	{
86356400Seric 		/* must have been being processed by someone else */
86456400Seric 		fclose(qfp);
86556400Seric 		return FALSE;
86656400Seric 	}
86756400Seric 
86857135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
86956400Seric 	{
87056400Seric # ifdef LOG
87156400Seric 		if (LogLevel > 0)
87256400Seric 		{
87356400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
87456400Seric 				e->e_id, st.st_uid, st.st_mode);
87556400Seric 		}
87656795Seric # endif /* LOG */
87756400Seric 		fclose(qfp);
87856400Seric 		return FALSE;
87956400Seric 	}
88056400Seric 
881*58689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
88240934Srick 	{
883*58689Seric 		/* being processed by another queuer */
884*58689Seric 		if (Verbose)
885*58689Seric 			printf("%s: locked\n", e->e_id);
88651920Seric # ifdef LOG
887*58689Seric 		if (LogLevel > 19)
888*58689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
88956795Seric # endif /* LOG */
89040934Srick 		(void) fclose(qfp);
89151920Seric 		return FALSE;
89240934Srick 	}
89340934Srick 
89451920Seric 	/* save this lock */
89551920Seric 	e->e_lockfp = qfp;
89651920Seric 
89740934Srick 	/* do basic system initialization */
89855012Seric 	initsys(e);
89940934Srick 
90017477Seric 	FileName = qf;
9019377Seric 	LineNumber = 0;
90251920Seric 	if (Verbose)
9039377Seric 		printf("\nRunning %s\n", e->e_id);
90454974Seric 	ctladdr = NULL;
90557135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9064632Seric 	{
90757529Seric 		struct stat st;
90857529Seric 
90926504Seric 		if (tTd(40, 4))
91057135Seric 			printf("+++++ %s\n", bp);
91157135Seric 		switch (bp[0])
9124632Seric 		{
91340973Sbostic 		  case 'C':		/* specify controlling user */
91457135Seric 			ctladdr = setctluser(&bp[1]);
91540973Sbostic 			break;
91640973Sbostic 
9174632Seric 		  case 'R':		/* specify recipient */
91858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9194632Seric 			break;
9204632Seric 
92125687Seric 		  case 'E':		/* specify error recipient */
92258082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
92325687Seric 			break;
92425687Seric 
9254632Seric 		  case 'H':		/* header */
92657135Seric 			(void) chompheader(&bp[1], FALSE, e);
9274632Seric 			break;
9284632Seric 
92910108Seric 		  case 'M':		/* message */
93057135Seric 			e->e_message = newstr(&bp[1]);
93110108Seric 			break;
93210108Seric 
9334632Seric 		  case 'S':		/* sender */
93458333Seric 			setsender(newstr(&bp[1]), e, NULL);
9354632Seric 			break;
9364632Seric 
9374632Seric 		  case 'D':		/* data file name */
93857135Seric 			e->e_df = newstr(&bp[1]);
9399544Seric 			e->e_dfp = fopen(e->e_df, "r");
9409544Seric 			if (e->e_dfp == NULL)
94158020Seric 			{
9429377Seric 				syserr("readqf: cannot open %s", e->e_df);
94358020Seric 				e->e_msgsize = -1;
94458020Seric 			}
94558020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
94657529Seric 				e->e_msgsize = st.st_size;
9474632Seric 			break;
9484632Seric 
9497860Seric 		  case 'T':		/* init time */
95057135Seric 			e->e_ctime = atol(&bp[1]);
9514632Seric 			break;
9524632Seric 
9534634Seric 		  case 'P':		/* message priority */
95457135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9554634Seric 			break;
9564634Seric 
95753400Seric 		  case '$':		/* define macro */
95857135Seric 			define(bp[1], newstr(&bp[2]), e);
95953400Seric 			break;
96053400Seric 
96124941Seric 		  case '\0':		/* blank line; ignore */
96224941Seric 			break;
96324941Seric 
9644632Seric 		  default:
96524941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
96657135Seric 				LineNumber, bp);
9674632Seric 			break;
9684632Seric 		}
96957135Seric 
97057135Seric 		if (bp != buf)
97157135Seric 			free(bp);
9724632Seric 	}
9739377Seric 
9749377Seric 	FileName = NULL;
97524941Seric 
97624941Seric 	/*
97724941Seric 	**  If we haven't read any lines, this queue file is empty.
97824941Seric 	**  Arrange to remove it without referencing any null pointers.
97924941Seric 	*/
98024941Seric 
98124941Seric 	if (LineNumber == 0)
98224941Seric 	{
98324941Seric 		errno = 0;
98424941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
98524941Seric 	}
98651920Seric 	return TRUE;
9874632Seric }
9884632Seric /*
9899630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9909630Seric **
9919630Seric **	Parameters:
9929630Seric **		none.
9939630Seric **
9949630Seric **	Returns:
9959630Seric **		none.
9969630Seric **
9979630Seric **	Side Effects:
9989630Seric **		Prints a listing of the mail queue on the standard output.
9999630Seric */
10005182Seric 
10019630Seric printqueue()
10029630Seric {
10039630Seric 	register WORK *w;
10049630Seric 	FILE *f;
100510070Seric 	int nrequests;
10069630Seric 	char buf[MAXLINE];
10079630Seric 
10089630Seric 	/*
100958250Seric 	**  Check for permission to print the queue
101058250Seric 	*/
101158250Seric 
101258523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
101358250Seric 	{
101458250Seric 		struct stat st;
101558523Seric # ifdef NGROUPS
101658523Seric 		int n;
101758523Seric 		int gidset[NGROUPS];
101858523Seric # endif
101958250Seric 
102058517Seric 		if (stat(QueueDir, &st) < 0)
102158250Seric 		{
102258250Seric 			syserr("Cannot stat %s", QueueDir);
102358250Seric 			return;
102458250Seric 		}
102558523Seric # ifdef NGROUPS
102658523Seric 		n = getgroups(NGROUPS, gidset);
102758523Seric 		while (--n >= 0)
102858523Seric 		{
102958523Seric 			if (gidset[n] == st.st_gid)
103058523Seric 				break;
103158523Seric 		}
103258523Seric 		if (n < 0)
103358523Seric # else
103458250Seric 		if (getgid() != st.st_gid)
103558523Seric # endif
103658250Seric 		{
103758250Seric 			usrerr("510 You are not permitted to see the queue");
103858250Seric 			setstat(EX_NOPERM);
103958250Seric 			return;
104058250Seric 		}
104158250Seric 	}
104258250Seric 
104358250Seric 	/*
10449630Seric 	**  Read and order the queue.
10459630Seric 	*/
10469630Seric 
104724941Seric 	nrequests = orderq(TRUE);
10489630Seric 
10499630Seric 	/*
10509630Seric 	**  Print the work list that we have read.
10519630Seric 	*/
10529630Seric 
10539630Seric 	/* first see if there is anything */
105410070Seric 	if (nrequests <= 0)
10559630Seric 	{
105610070Seric 		printf("Mail queue is empty\n");
10579630Seric 		return;
10589630Seric 	}
10599630Seric 
106051920Seric 	CurrentLA = getla();	/* get load average */
106140934Srick 
106210096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
106325687Seric 	if (nrequests > QUEUESIZE)
106425687Seric 		printf(", only %d printed", QUEUESIZE);
106524979Seric 	if (Verbose)
106625032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
106724979Seric 	else
106824979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10699630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10709630Seric 	{
10719630Seric 		struct stat st;
107210070Seric 		auto time_t submittime = 0;
107310070Seric 		long dfsize = -1;
107410108Seric 		char message[MAXLINE];
107524941Seric 		extern bool shouldqueue();
1076*58689Seric 		extern bool lockfile();
10779630Seric 
107817468Seric 		f = fopen(w->w_name, "r");
107917468Seric 		if (f == NULL)
108017468Seric 		{
108117468Seric 			errno = 0;
108217468Seric 			continue;
108317468Seric 		}
10849630Seric 		printf("%7s", w->w_name + 2);
1085*58689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
108610070Seric 			printf("*");
108757438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
108824941Seric 			printf("X");
108910070Seric 		else
109010070Seric 			printf(" ");
109110070Seric 		errno = 0;
109217468Seric 
109310108Seric 		message[0] = '\0';
10949630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10959630Seric 		{
109653400Seric 			register int i;
109753400Seric 
10989630Seric 			fixcrlf(buf, TRUE);
10999630Seric 			switch (buf[0])
11009630Seric 			{
110110108Seric 			  case 'M':	/* error message */
110253400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
110353400Seric 					i = sizeof message;
110453400Seric 				bcopy(&buf[1], message, i);
110553400Seric 				message[i] = '\0';
110610108Seric 				break;
110710108Seric 
11089630Seric 			  case 'S':	/* sender name */
110924979Seric 				if (Verbose)
111025027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
111125027Seric 					    w->w_pri, ctime(&submittime) + 4,
111224979Seric 					    &buf[1]);
111324979Seric 				else
111424979Seric 					printf("%8ld %.16s %.45s", dfsize,
111524979Seric 					    ctime(&submittime), &buf[1]);
111610108Seric 				if (message[0] != '\0')
111725027Seric 					printf("\n\t\t (%.60s)", message);
11189630Seric 				break;
111951920Seric 
112040973Sbostic 			  case 'C':	/* controlling user */
112154974Seric 				if (Verbose)
112254975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
112340973Sbostic 				break;
11249630Seric 
11259630Seric 			  case 'R':	/* recipient name */
112624979Seric 				if (Verbose)
112725027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
112824979Seric 				else
112924979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
11309630Seric 				break;
11319630Seric 
11329630Seric 			  case 'T':	/* creation time */
113324941Seric 				submittime = atol(&buf[1]);
11349630Seric 				break;
113510070Seric 
113610070Seric 			  case 'D':	/* data file name */
113710070Seric 				if (stat(&buf[1], &st) >= 0)
113810070Seric 					dfsize = st.st_size;
113910070Seric 				break;
11409630Seric 			}
11419630Seric 		}
114210070Seric 		if (submittime == (time_t) 0)
114310070Seric 			printf(" (no control file)");
11449630Seric 		printf("\n");
114523098Seric 		(void) fclose(f);
11469630Seric 	}
11479630Seric }
11489630Seric 
114956795Seric # endif /* QUEUE */
115017468Seric /*
115117468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
115217468Seric **
115317468Seric **	Assigns an id code if one does not already exist.
115417468Seric **	This code is very careful to avoid trashing existing files
115517468Seric **	under any circumstances.
115617468Seric **
115717468Seric **	Parameters:
115817468Seric **		e -- envelope to build it in/from.
115917468Seric **		type -- the file type, used as the first character
116017468Seric **			of the file name.
116117468Seric **
116217468Seric **	Returns:
116317468Seric **		a pointer to the new file name (in a static buffer).
116417468Seric **
116517468Seric **	Side Effects:
116651920Seric **		If no id code is already assigned, queuename will
116751920Seric **		assign an id code, create a qf file, and leave a
116851920Seric **		locked, open-for-write file pointer in the envelope.
116917468Seric */
117017468Seric 
117117468Seric char *
117217468Seric queuename(e, type)
117317468Seric 	register ENVELOPE *e;
117417468Seric 	char type;
117517468Seric {
117617468Seric 	static int pid = -1;
117758680Seric 	static char c1 = 'A';
117858680Seric 	static char c2 = 'A';
1179*58689Seric 	static char buf[MAXNAME];
1180*58689Seric 	extern bool lockfile();
118117468Seric 
118217468Seric 	if (e->e_id == NULL)
118317468Seric 	{
118417468Seric 		char qf[20];
118517468Seric 
118617468Seric 		/* find a unique id */
118717468Seric 		if (pid != getpid())
118817468Seric 		{
118917468Seric 			/* new process -- start back at "AA" */
119017468Seric 			pid = getpid();
119117468Seric 			c1 = 'A';
119217468Seric 			c2 = 'A' - 1;
119317468Seric 		}
119417468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
119517468Seric 
119617468Seric 		while (c1 < '~' || c2 < 'Z')
119717468Seric 		{
119817468Seric 			int i;
119917468Seric 
120017468Seric 			if (c2 >= 'Z')
120117468Seric 			{
120217468Seric 				c1++;
120317468Seric 				c2 = 'A' - 1;
120417468Seric 			}
120540934Srick 			qf[2] = c1;
120640934Srick 			qf[3] = ++c2;
120717468Seric 			if (tTd(7, 20))
120840934Srick 				printf("queuename: trying \"%s\"\n", qf);
120917468Seric 
121040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
121151920Seric 			if (i < 0)
121251920Seric 			{
121351920Seric 				if (errno == EEXIST)
121451920Seric 					continue;
121551920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
121651920Seric 					qf, QueueDir);
121751920Seric 				exit(EX_UNAVAILABLE);
121851920Seric 			}
1219*58689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
122051920Seric 			{
122151920Seric 				e->e_lockfp = fdopen(i, "w");
122240934Srick 				break;
122317468Seric 			}
122451920Seric 
122551920Seric 			/* a reader got the file; abandon it and try again */
122651920Seric 			(void) close(i);
122717468Seric 		}
122817468Seric 		if (c1 >= '~' && c2 >= 'Z')
122917468Seric 		{
123017468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
123117468Seric 				qf, QueueDir);
123217468Seric 			exit(EX_OSERR);
123317468Seric 		}
123417468Seric 		e->e_id = newstr(&qf[2]);
123517468Seric 		define('i', e->e_id, e);
123617468Seric 		if (tTd(7, 1))
123717468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
123817468Seric # ifdef LOG
123958020Seric 		if (LogLevel > 93)
124017468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
124156795Seric # endif /* LOG */
124217468Seric 	}
124317468Seric 
124417468Seric 	if (type == '\0')
124517468Seric 		return (NULL);
124617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
124717468Seric 	if (tTd(7, 2))
124817468Seric 		printf("queuename: %s\n", buf);
124917468Seric 	return (buf);
125017468Seric }
125117468Seric /*
125217468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
125317468Seric **
125417468Seric **	Parameters:
125517468Seric **		e -- the envelope to unlock.
125617468Seric **
125717468Seric **	Returns:
125817468Seric **		none
125917468Seric **
126017468Seric **	Side Effects:
126117468Seric **		unlocks the queue for `e'.
126217468Seric */
126317468Seric 
126417468Seric unlockqueue(e)
126517468Seric 	ENVELOPE *e;
126617468Seric {
126758680Seric 	if (tTd(51, 4))
126858680Seric 		printf("unlockqueue(%s)\n", e->e_id);
126958680Seric 
127051920Seric 	/* if there is a lock file in the envelope, close it */
127151920Seric 	if (e->e_lockfp != NULL)
127258680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
127351920Seric 	e->e_lockfp = NULL;
127451920Seric 
127517468Seric 	/* remove the transcript */
127617468Seric # ifdef LOG
127758020Seric 	if (LogLevel > 87)
127817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
127956795Seric # endif /* LOG */
128058680Seric 	if (!tTd(51, 104))
128117468Seric 		xunlink(queuename(e, 'x'));
128217468Seric 
128317468Seric }
128440973Sbostic /*
128554974Seric **  SETCTLUSER -- create a controlling address
128640973Sbostic **
128754974Seric **	Create a fake "address" given only a local login name; this is
128854974Seric **	used as a "controlling user" for future recipient addresses.
128940973Sbostic **
129040973Sbostic **	Parameters:
129154974Seric **		user -- the user name of the controlling user.
129240973Sbostic **
129340973Sbostic **	Returns:
129454974Seric **		An address descriptor for the controlling user.
129540973Sbostic **
129640973Sbostic **	Side Effects:
129740973Sbostic **		none.
129840973Sbostic */
129940973Sbostic 
130054974Seric ADDRESS *
130154974Seric setctluser(user)
130254974Seric 	char *user;
130340973Sbostic {
130454974Seric 	register ADDRESS *a;
130540973Sbostic 	struct passwd *pw;
130640973Sbostic 
130740973Sbostic 	/*
130854974Seric 	**  See if this clears our concept of controlling user.
130940973Sbostic 	*/
131040973Sbostic 
131154974Seric 	if (user == NULL || *user == '\0')
131254974Seric 		return NULL;
131340973Sbostic 
131440973Sbostic 	/*
131554974Seric 	**  Set up addr fields for controlling user.
131640973Sbostic 	*/
131740973Sbostic 
131854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
131954974Seric 	bzero((char *) a, sizeof *a);
132054974Seric 	if ((pw = getpwnam(user)) != NULL)
132140973Sbostic 	{
132240973Sbostic 		a->q_home = newstr(pw->pw_dir);
132340973Sbostic 		a->q_uid = pw->pw_uid;
132440973Sbostic 		a->q_gid = pw->pw_gid;
132557642Seric 		a->q_user = newstr(user);
132640973Sbostic 	}
132740973Sbostic 	else
132840973Sbostic 	{
132940973Sbostic 		a->q_uid = DefUid;
133040973Sbostic 		a->q_gid = DefGid;
133157642Seric 		a->q_user = newstr(DefUser);
133240973Sbostic 	}
133340973Sbostic 
133458294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
133556328Seric 	a->q_mailer = LocalMailer;
133654974Seric 	return a;
133740973Sbostic }
1338