xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58294)
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*58294Seric static char sccsid[] = "@(#)queue.c	6.20 (Berkeley) 02/27/93 (with queueing)";
1433731Sbostic #else
15*58294Seric static char sccsid[] = "@(#)queue.c	6.20 (Berkeley) 02/27/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>
2657977Seric # 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 	{
22158250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
22258250Seric 		    (queueall && !bitset(QDONTSEND|QSENT, 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;
23858151Seric 				message("queued");
23958020Seric 				if (LogLevel > 8)
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);
26258020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
26358020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
26410349Seric 	nullmailer.m_eol = "\n";
26510173Seric 
26658050Seric 	define('g', "\201f", e);
2676980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2684632Seric 	{
26910686Seric 		extern bool bitzerop();
27010686Seric 
27112015Seric 		/* don't output null headers */
2724632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2734632Seric 			continue;
27412015Seric 
27512015Seric 		/* don't output resent headers on non-resent messages */
27612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
27712015Seric 			continue;
27812015Seric 
27912015Seric 		/* output this header */
2807812Seric 		fprintf(tfp, "H");
28112015Seric 
28212015Seric 		/* if conditional, output the set of conditions */
28310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
28410686Seric 		{
28510686Seric 			int j;
28610686Seric 
28723098Seric 			(void) putc('?', tfp);
28810686Seric 			for (j = '\0'; j <= '\177'; j++)
28910686Seric 				if (bitnset(j, h->h_mflags))
29023098Seric 					(void) putc(j, tfp);
29123098Seric 			(void) putc('?', tfp);
29210686Seric 		}
29312015Seric 
29412015Seric 		/* output the header: expand macros, convert addresses */
2957763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2967763Seric 		{
2977763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2988236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2997763Seric 		}
3008245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3019348Seric 		{
30255012Seric 			commaize(h, h->h_value, tfp,
30355012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
30455012Seric 				 &nullmailer, e);
3059348Seric 		}
3067763Seric 		else
3078245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3084632Seric 	}
3094632Seric 
3104632Seric 	/*
3114632Seric 	**  Clean up.
3124632Seric 	*/
3134632Seric 
31451920Seric 	if (!newid)
31551920Seric 	{
31651920Seric 		qf = queuename(e, 'q');
31751920Seric 		if (rename(tf, qf) < 0)
31851920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31951920Seric 		if (e->e_lockfp != NULL)
32051920Seric 			(void) fclose(e->e_lockfp);
32151920Seric 		e->e_lockfp = tfp;
32251920Seric 	}
32351920Seric 	else
32451920Seric 		qf = tf;
32541636Srick 	errno = 0;
3267391Seric 
3277677Seric # ifdef LOG
3287677Seric 	/* save log info */
32958020Seric 	if (LogLevel > 79)
3307878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33156795Seric # endif /* LOG */
33240934Srick 	fflush(tfp);
33351920Seric 	return;
3344632Seric }
33554974Seric 
33654974Seric printctladdr(a, tfp)
33754974Seric 	ADDRESS *a;
33854974Seric 	FILE *tfp;
33954974Seric {
34054974Seric 	char *u;
34154974Seric 	struct passwd *pw;
34254974Seric 	extern struct passwd *getpwuid();
34354974Seric 
34454974Seric 	if (a == NULL)
34554974Seric 	{
34654974Seric 		fprintf(tfp, "C\n");
34754974Seric 		return;
34854974Seric 	}
34954974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
35054974Seric 		u = DefUser;
35154974Seric 	else
35254974Seric 		u = pw->pw_name;
35354974Seric 	fprintf(tfp, "C%s\n", u);
35454974Seric }
35554974Seric 
3564632Seric /*
3574632Seric **  RUNQUEUE -- run the jobs in the queue.
3584632Seric **
3594632Seric **	Gets the stuff out of the queue in some presumably logical
3604632Seric **	order and processes them.
3614632Seric **
3624632Seric **	Parameters:
36324941Seric **		forkflag -- TRUE if the queue scanning should be done in
36424941Seric **			a child process.  We double-fork so it is not our
36524941Seric **			child and we don't have to clean up after it.
3664632Seric **
3674632Seric **	Returns:
3684632Seric **		none.
3694632Seric **
3704632Seric **	Side Effects:
3714632Seric **		runs things in the mail queue.
3724632Seric */
3734632Seric 
37455360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37555360Seric 
37655360Seric runqueue(forkflag)
3774639Seric 	bool forkflag;
3784632Seric {
37924953Seric 	extern bool shouldqueue();
38055360Seric 	register ENVELOPE *e;
38155360Seric 	extern ENVELOPE BlankEnvelope;
38255360Seric 	extern ENVELOPE *newenvelope();
38324953Seric 
3847466Seric 	/*
38524953Seric 	**  If no work will ever be selected, don't even bother reading
38624953Seric 	**  the queue.
38724953Seric 	*/
38824953Seric 
38951920Seric 	CurrentLA = getla();	/* get load average */
39040934Srick 
39158132Seric 	if (shouldqueue(0L, curtime()))
39224953Seric 	{
39324953Seric 		if (Verbose)
39424953Seric 			printf("Skipping queue run -- load average too high\n");
39558107Seric 		if (forkflag && QueueIntvl != 0)
39658107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39755360Seric 		return;
39824953Seric 	}
39924953Seric 
40024953Seric 	/*
4017466Seric 	**  See if we want to go off and do other useful work.
4027466Seric 	*/
4034639Seric 
4044639Seric 	if (forkflag)
4054639Seric 	{
4067943Seric 		int pid;
4077943Seric 
4087943Seric 		pid = dofork();
4097943Seric 		if (pid != 0)
4104639Seric 		{
41146928Sbostic 			extern void reapchild();
41225184Seric 
4137943Seric 			/* parent -- pick up intermediate zombie */
41425184Seric #ifndef SIGCHLD
4159377Seric 			(void) waitfor(pid);
41656795Seric #else /* SIGCHLD */
41725184Seric 			(void) signal(SIGCHLD, reapchild);
41856795Seric #endif /* SIGCHLD */
4197690Seric 			if (QueueIntvl != 0)
4209348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4214639Seric 			return;
4224639Seric 		}
4237943Seric 		/* child -- double fork */
42425184Seric #ifndef SIGCHLD
4257943Seric 		if (fork() != 0)
4267943Seric 			exit(EX_OK);
42756795Seric #else /* SIGCHLD */
42825184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42956795Seric #endif /* SIGCHLD */
4304639Seric 	}
43124941Seric 
43240934Srick 	setproctitle("running queue: %s", QueueDir);
43324941Seric 
4347876Seric # ifdef LOG
43558020Seric 	if (LogLevel > 69)
43655360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43755360Seric 			QueueDir, getpid(), forkflag);
43856795Seric # endif /* LOG */
4394639Seric 
4407466Seric 	/*
44110205Seric 	**  Release any resources used by the daemon code.
44210205Seric 	*/
44310205Seric 
44410205Seric # ifdef DAEMON
44510205Seric 	clrdaemon();
44656795Seric # endif /* DAEMON */
44710205Seric 
44810205Seric 	/*
44955360Seric 	**  Create ourselves an envelope
45055360Seric 	*/
45155360Seric 
45255360Seric 	CurEnv = &QueueEnvelope;
45358179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45455360Seric 	e->e_flags = BlankEnvelope.e_flags;
45555360Seric 
45655360Seric 	/*
45727175Seric 	**  Make sure the alias database is open.
45827175Seric 	*/
45927175Seric 
46055012Seric 	initaliases(AliasFile, FALSE, e);
46127175Seric 
46227175Seric 	/*
4637466Seric 	**  Start making passes through the queue.
4647466Seric 	**	First, read and sort the entire queue.
4657466Seric 	**	Then, process the work in that order.
4667466Seric 	**		But if you take too long, start over.
4677466Seric 	*/
4687466Seric 
4697943Seric 	/* order the existing work requests */
47024954Seric 	(void) orderq(FALSE);
4717690Seric 
4727943Seric 	/* process them once at a time */
4737943Seric 	while (WorkQ != NULL)
4744639Seric 	{
4757943Seric 		WORK *w = WorkQ;
4767881Seric 
4777943Seric 		WorkQ = WorkQ->w_next;
47855012Seric 		dowork(w, e);
4797943Seric 		free(w->w_name);
4807943Seric 		free((char *) w);
4814639Seric 	}
48229866Seric 
48329866Seric 	/* exit without the usual cleanup */
48455467Seric 	e->e_id = NULL;
48555467Seric 	finis();
4864634Seric }
4874634Seric /*
4884632Seric **  ORDERQ -- order the work queue.
4894632Seric **
4904632Seric **	Parameters:
49124941Seric **		doall -- if set, include everything in the queue (even
49224941Seric **			the jobs that cannot be run because the load
49324941Seric **			average is too high).  Otherwise, exclude those
49424941Seric **			jobs.
4954632Seric **
4964632Seric **	Returns:
49710121Seric **		The number of request in the queue (not necessarily
49810121Seric **		the number of requests in WorkQ however).
4994632Seric **
5004632Seric **	Side Effects:
5014632Seric **		Sets WorkQ to the queue of available work, in order.
5024632Seric */
5034632Seric 
50425687Seric # define NEED_P		001
50525687Seric # define NEED_T		002
5064632Seric 
50724941Seric orderq(doall)
50824941Seric 	bool doall;
5094632Seric {
5106625Sglickman 	register struct direct *d;
5114632Seric 	register WORK *w;
5126625Sglickman 	DIR *f;
5134632Seric 	register int i;
51425687Seric 	WORK wlist[QUEUESIZE+1];
51510070Seric 	int wn = -1;
5164632Seric 	extern workcmpf();
5174632Seric 
5184632Seric 	/* clear out old WorkQ */
5194632Seric 	for (w = WorkQ; w != NULL; )
5204632Seric 	{
5214632Seric 		register WORK *nw = w->w_next;
5224632Seric 
5234632Seric 		WorkQ = nw;
5244632Seric 		free(w->w_name);
5254632Seric 		free((char *) w);
5264632Seric 		w = nw;
5274632Seric 	}
5284632Seric 
5294632Seric 	/* open the queue directory */
5308148Seric 	f = opendir(".");
5314632Seric 	if (f == NULL)
5324632Seric 	{
5338148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
53410070Seric 		return (0);
5354632Seric 	}
5364632Seric 
5374632Seric 	/*
5384632Seric 	**  Read the work directory.
5394632Seric 	*/
5404632Seric 
54110070Seric 	while ((d = readdir(f)) != NULL)
5424632Seric 	{
5439377Seric 		FILE *cf;
5444632Seric 		char lbuf[MAXNAME];
54557642Seric 		extern bool shouldqueue();
5464632Seric 
5474632Seric 		/* is this an interesting entry? */
5487812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5494632Seric 			continue;
5504632Seric 
55158020Seric 		if (strlen(d->d_name) != 9)
55258020Seric 		{
55358020Seric 			if (Verbose)
55458020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
55558020Seric #ifdef LOG
55658020Seric 			if (LogLevel > 3)
55758020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
55858020Seric 					d->d_name);
55958020Seric #endif
56058020Seric 			if (strlen(d->d_name) >= MAXNAME)
56158020Seric 				d->d_name[MAXNAME - 1] = '\0';
56258020Seric 			strcpy(lbuf, d->d_name);
56358020Seric 			lbuf[0] = 'Q';
56458020Seric 			(void) rename(d->d_name, lbuf);
56558020Seric 			continue;
56658020Seric 		}
56758020Seric 
56810070Seric 		/* yes -- open control file (if not too many files) */
56925687Seric 		if (++wn >= QUEUESIZE)
57010070Seric 			continue;
5718148Seric 		cf = fopen(d->d_name, "r");
5724632Seric 		if (cf == NULL)
5734632Seric 		{
5747055Seric 			/* this may be some random person sending hir msgs */
5757055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
57610090Seric 			if (tTd(41, 2))
57710090Seric 				printf("orderq: cannot open %s (%d)\n",
57810090Seric 					d->d_name, errno);
5797055Seric 			errno = 0;
58010090Seric 			wn--;
5814632Seric 			continue;
5824632Seric 		}
58325687Seric 		w = &wlist[wn];
58425687Seric 		w->w_name = newstr(d->d_name);
5854632Seric 
58625027Seric 		/* make sure jobs in creation don't clog queue */
58725687Seric 		w->w_pri = 0x7fffffff;
58825687Seric 		w->w_ctime = 0;
58925027Seric 
5904632Seric 		/* extract useful information */
59125687Seric 		i = NEED_P | NEED_T;
59225687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5934632Seric 		{
59424954Seric 			extern long atol();
59524954Seric 
59624941Seric 			switch (lbuf[0])
5974632Seric 			{
59824941Seric 			  case 'P':
59925687Seric 				w->w_pri = atol(&lbuf[1]);
60025687Seric 				i &= ~NEED_P;
6014632Seric 				break;
60225013Seric 
60325013Seric 			  case 'T':
60425687Seric 				w->w_ctime = atol(&lbuf[1]);
60525687Seric 				i &= ~NEED_T;
60625013Seric 				break;
6074632Seric 			}
6084632Seric 		}
6094632Seric 		(void) fclose(cf);
61024953Seric 
61157438Seric 		if (!doall && shouldqueue(w->w_pri, w->w_ctime))
61224953Seric 		{
61324953Seric 			/* don't even bother sorting this job in */
61424953Seric 			wn--;
61524953Seric 		}
6164632Seric 	}
6176625Sglickman 	(void) closedir(f);
61810090Seric 	wn++;
6194632Seric 
6204632Seric 	/*
6214632Seric 	**  Sort the work directory.
6224632Seric 	*/
6234632Seric 
62425687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6254632Seric 
6264632Seric 	/*
6274632Seric 	**  Convert the work list into canonical form.
6289377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6294632Seric 	*/
6304632Seric 
63124981Seric 	WorkQ = NULL;
63225687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6334632Seric 	{
6344632Seric 		w = (WORK *) xalloc(sizeof *w);
6354632Seric 		w->w_name = wlist[i].w_name;
6364632Seric 		w->w_pri = wlist[i].w_pri;
63725013Seric 		w->w_ctime = wlist[i].w_ctime;
63824981Seric 		w->w_next = WorkQ;
63924981Seric 		WorkQ = w;
6404632Seric 	}
6414632Seric 
6427677Seric 	if (tTd(40, 1))
6434632Seric 	{
6444632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6455037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6464632Seric 	}
64710070Seric 
64810090Seric 	return (wn);
6494632Seric }
6504632Seric /*
6517677Seric **  WORKCMPF -- compare function for ordering work.
6524632Seric **
6534632Seric **	Parameters:
6544632Seric **		a -- the first argument.
6554632Seric **		b -- the second argument.
6564632Seric **
6574632Seric **	Returns:
65824981Seric **		-1 if a < b
65924981Seric **		 0 if a == b
66024981Seric **		+1 if a > b
6614632Seric **
6624632Seric **	Side Effects:
6634632Seric **		none.
6644632Seric */
6654632Seric 
6664632Seric workcmpf(a, b)
6675037Seric 	register WORK *a;
6685037Seric 	register WORK *b;
6694632Seric {
67057438Seric 	long pa = a->w_pri;
67157438Seric 	long pb = b->w_pri;
67224941Seric 
67324941Seric 	if (pa == pb)
6744632Seric 		return (0);
67524941Seric 	else if (pa > pb)
67624981Seric 		return (1);
67724981Seric 	else
67810121Seric 		return (-1);
6794632Seric }
6804632Seric /*
6814632Seric **  DOWORK -- do a work request.
6824632Seric **
6834632Seric **	Parameters:
6844632Seric **		w -- the work request to be satisfied.
6854632Seric **
6864632Seric **	Returns:
6874632Seric **		none.
6884632Seric **
6894632Seric **	Side Effects:
6904632Seric **		The work request is satisfied if possible.
6914632Seric */
6924632Seric 
69355012Seric dowork(w, e)
6944632Seric 	register WORK *w;
69555012Seric 	register ENVELOPE *e;
6964632Seric {
6974632Seric 	register int i;
69824941Seric 	extern bool shouldqueue();
69951920Seric 	extern bool readqf();
7004632Seric 
7017677Seric 	if (tTd(40, 1))
7025037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7034632Seric 
7044632Seric 	/*
70524941Seric 	**  Ignore jobs that are too expensive for the moment.
7064632Seric 	*/
7074632Seric 
70857438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7094632Seric 	{
71024941Seric 		if (Verbose)
71124967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7124632Seric 		return;
7134632Seric 	}
7144632Seric 
71524941Seric 	/*
71624941Seric 	**  Fork for work.
71724941Seric 	*/
71824941Seric 
71924941Seric 	if (ForkQueueRuns)
72024941Seric 	{
72124941Seric 		i = fork();
72224941Seric 		if (i < 0)
72324941Seric 		{
72424941Seric 			syserr("dowork: cannot fork");
72524941Seric 			return;
72624941Seric 		}
72724941Seric 	}
72824941Seric 	else
72924941Seric 	{
73024941Seric 		i = 0;
73124941Seric 	}
73224941Seric 
7334632Seric 	if (i == 0)
7344632Seric 	{
7354632Seric 		/*
7364632Seric 		**  CHILD
7378148Seric 		**	Lock the control file to avoid duplicate deliveries.
7388148Seric 		**		Then run the file as though we had just read it.
7397350Seric 		**	We save an idea of the temporary name so we
7407350Seric 		**		can recover on interrupt.
7414632Seric 		*/
7424632Seric 
7437763Seric 		/* set basic modes, etc. */
7447356Seric 		(void) alarm(0);
74555012Seric 		clearenvelope(e, FALSE);
7464632Seric 		QueueRun = TRUE;
7479377Seric 		ErrorMode = EM_MAIL;
74855012Seric 		e->e_id = &w->w_name[2];
7497876Seric # ifdef LOG
75058020Seric 		if (LogLevel > 76)
75155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7527881Seric 			       getpid());
75356795Seric # endif /* LOG */
7547763Seric 
7557763Seric 		/* don't use the headers from sendmail.cf... */
75655012Seric 		e->e_header = NULL;
7577763Seric 
75851920Seric 		/* read the queue control file -- return if locked */
75955012Seric 		if (!readqf(e))
7606980Seric 		{
76124941Seric 			if (ForkQueueRuns)
76224941Seric 				exit(EX_OK);
76324941Seric 			else
76424941Seric 				return;
7656980Seric 		}
7666980Seric 
76755012Seric 		e->e_flags |= EF_INQUEUE;
76857642Seric 		eatheader(e, TRUE);
7696980Seric 
7706980Seric 		/* do the delivery */
77155012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
77255012Seric 			sendall(e, SM_DELIVER);
7736980Seric 
7746980Seric 		/* finish up and exit */
77524941Seric 		if (ForkQueueRuns)
77624941Seric 			finis();
77724941Seric 		else
77855012Seric 			dropenvelope(e);
7794632Seric 	}
78024941Seric 	else
78124941Seric 	{
78224941Seric 		/*
78324941Seric 		**  Parent -- pick up results.
78424941Seric 		*/
7854632Seric 
78624941Seric 		errno = 0;
78724941Seric 		(void) waitfor(i);
78824941Seric 	}
7894632Seric }
7904632Seric /*
7914632Seric **  READQF -- read queue file and set up environment.
7924632Seric **
7934632Seric **	Parameters:
7949377Seric **		e -- the envelope of the job to run.
7954632Seric **
7964632Seric **	Returns:
79751920Seric **		TRUE if it successfully read the queue file.
79851920Seric **		FALSE otherwise.
7994632Seric **
8004632Seric **	Side Effects:
80151920Seric **		The queue file is returned locked.
8024632Seric */
8034632Seric 
80451920Seric bool
80551920Seric readqf(e)
8069377Seric 	register ENVELOPE *e;
8074632Seric {
80817477Seric 	char *qf;
80917477Seric 	register FILE *qfp;
81054974Seric 	ADDRESS *ctladdr;
81156400Seric 	struct stat st;
81257135Seric 	char *bp;
81357135Seric 	char buf[MAXLINE];
8149348Seric 	extern char *fgetfolded();
81524954Seric 	extern long atol();
81654974Seric 	extern ADDRESS *setctluser();
81751937Seric # ifdef LOCKF
81851937Seric 	struct flock lfd;
81951937Seric # endif
8204632Seric 
8214632Seric 	/*
82217468Seric 	**  Read and process the file.
8234632Seric 	*/
8244632Seric 
82517477Seric 	qf = queuename(e, 'q');
82651937Seric 	qfp = fopen(qf, "r+");
82717477Seric 	if (qfp == NULL)
82817477Seric 	{
82940934Srick 		if (errno != ENOENT)
83040934Srick 			syserr("readqf: no control file %s", qf);
83151920Seric 		return FALSE;
83217477Seric 	}
83340934Srick 
83456400Seric 	/*
83556400Seric 	**  Check the queue file for plausibility to avoid attacks.
83656400Seric 	*/
83756400Seric 
83856400Seric 	if (fstat(fileno(qfp), &st) < 0)
83956400Seric 	{
84056400Seric 		/* must have been being processed by someone else */
84156400Seric 		fclose(qfp);
84256400Seric 		return FALSE;
84356400Seric 	}
84456400Seric 
84557135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
84656400Seric 	{
84756400Seric # ifdef LOG
84856400Seric 		if (LogLevel > 0)
84956400Seric 		{
85056400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
85156400Seric 				e->e_id, st.st_uid, st.st_mode);
85256400Seric 		}
85356795Seric # endif /* LOG */
85456400Seric 		fclose(qfp);
85556400Seric 		return FALSE;
85656400Seric 	}
85756400Seric 
85851835Seric # ifdef LOCKF
85951937Seric 	lfd.l_type = F_WRLCK;
86051937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
86151937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
86251835Seric # else
86340934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
86451835Seric # endif
86540934Srick 	{
86658062Seric 		if (errno == EWOULDBLOCK)
86758062Seric 		{
86858062Seric 			/* being processed by another queuer */
86958062Seric 			if (Verbose)
87058062Seric 				printf("%s: locked\n", e->e_id);
87151920Seric # ifdef LOG
87258062Seric 			if (LogLevel > 19)
87358062Seric 				syslog(LOG_DEBUG, "%s: locked", e->e_id);
87456795Seric # endif /* LOG */
87558062Seric 		}
87658062Seric 		else
87758062Seric 		{
87858062Seric 			syserr("%s: flock failure", e->e_id);
87958062Seric 		}
88040934Srick 		(void) fclose(qfp);
88151920Seric 		return FALSE;
88240934Srick 	}
88340934Srick 
88451920Seric 	/* save this lock */
88551920Seric 	e->e_lockfp = qfp;
88651920Seric 
88740934Srick 	/* do basic system initialization */
88855012Seric 	initsys(e);
88940934Srick 
89017477Seric 	FileName = qf;
8919377Seric 	LineNumber = 0;
89251920Seric 	if (Verbose)
8939377Seric 		printf("\nRunning %s\n", e->e_id);
89454974Seric 	ctladdr = NULL;
89557135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
8964632Seric 	{
89757529Seric 		struct stat st;
89857529Seric 
89926504Seric 		if (tTd(40, 4))
90057135Seric 			printf("+++++ %s\n", bp);
90157135Seric 		switch (bp[0])
9024632Seric 		{
90340973Sbostic 		  case 'C':		/* specify controlling user */
90457135Seric 			ctladdr = setctluser(&bp[1]);
90540973Sbostic 			break;
90640973Sbostic 
9074632Seric 		  case 'R':		/* specify recipient */
90858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9094632Seric 			break;
9104632Seric 
91125687Seric 		  case 'E':		/* specify error recipient */
91258082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
91325687Seric 			break;
91425687Seric 
9154632Seric 		  case 'H':		/* header */
91657135Seric 			(void) chompheader(&bp[1], FALSE, e);
9174632Seric 			break;
9184632Seric 
91910108Seric 		  case 'M':		/* message */
92057135Seric 			e->e_message = newstr(&bp[1]);
92110108Seric 			break;
92210108Seric 
9234632Seric 		  case 'S':		/* sender */
92457135Seric 			setsender(newstr(&bp[1]), e);
9254632Seric 			break;
9264632Seric 
9274632Seric 		  case 'D':		/* data file name */
92857135Seric 			e->e_df = newstr(&bp[1]);
9299544Seric 			e->e_dfp = fopen(e->e_df, "r");
9309544Seric 			if (e->e_dfp == NULL)
93158020Seric 			{
9329377Seric 				syserr("readqf: cannot open %s", e->e_df);
93358020Seric 				e->e_msgsize = -1;
93458020Seric 			}
93558020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
93657529Seric 				e->e_msgsize = st.st_size;
9374632Seric 			break;
9384632Seric 
9397860Seric 		  case 'T':		/* init time */
94057135Seric 			e->e_ctime = atol(&bp[1]);
9414632Seric 			break;
9424632Seric 
9434634Seric 		  case 'P':		/* message priority */
94457135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9454634Seric 			break;
9464634Seric 
94753400Seric 		  case '$':		/* define macro */
94857135Seric 			define(bp[1], newstr(&bp[2]), e);
94953400Seric 			break;
95053400Seric 
95124941Seric 		  case '\0':		/* blank line; ignore */
95224941Seric 			break;
95324941Seric 
9544632Seric 		  default:
95524941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
95657135Seric 				LineNumber, bp);
9574632Seric 			break;
9584632Seric 		}
95957135Seric 
96057135Seric 		if (bp != buf)
96157135Seric 			free(bp);
9624632Seric 	}
9639377Seric 
9649377Seric 	FileName = NULL;
96524941Seric 
96624941Seric 	/*
96724941Seric 	**  If we haven't read any lines, this queue file is empty.
96824941Seric 	**  Arrange to remove it without referencing any null pointers.
96924941Seric 	*/
97024941Seric 
97124941Seric 	if (LineNumber == 0)
97224941Seric 	{
97324941Seric 		errno = 0;
97424941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
97524941Seric 	}
97651920Seric 	return TRUE;
9774632Seric }
9784632Seric /*
9799630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9809630Seric **
9819630Seric **	Parameters:
9829630Seric **		none.
9839630Seric **
9849630Seric **	Returns:
9859630Seric **		none.
9869630Seric **
9879630Seric **	Side Effects:
9889630Seric **		Prints a listing of the mail queue on the standard output.
9899630Seric */
9905182Seric 
9919630Seric printqueue()
9929630Seric {
9939630Seric 	register WORK *w;
9949630Seric 	FILE *f;
99510070Seric 	int nrequests;
9969630Seric 	char buf[MAXLINE];
9979630Seric 
9989630Seric 	/*
99958250Seric 	**  Check for permission to print the queue
100058250Seric 	*/
100158250Seric 
100258250Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags))
100358250Seric 	{
100458250Seric 		struct stat st;
100558250Seric 
100658250Seric 		if (stat(QueueDir, &st) <= 0)
100758250Seric 		{
100858250Seric 			syserr("Cannot stat %s", QueueDir);
100958250Seric 			return;
101058250Seric 		}
101158250Seric 		if (getgid() != st.st_gid)
101258250Seric 		{
101358250Seric 			usrerr("510 You are not permitted to see the queue");
101458250Seric 			setstat(EX_NOPERM);
101558250Seric 			return;
101658250Seric 		}
101758250Seric 	}
101858250Seric 
101958250Seric 	/*
10209630Seric 	**  Read and order the queue.
10219630Seric 	*/
10229630Seric 
102324941Seric 	nrequests = orderq(TRUE);
10249630Seric 
10259630Seric 	/*
10269630Seric 	**  Print the work list that we have read.
10279630Seric 	*/
10289630Seric 
10299630Seric 	/* first see if there is anything */
103010070Seric 	if (nrequests <= 0)
10319630Seric 	{
103210070Seric 		printf("Mail queue is empty\n");
10339630Seric 		return;
10349630Seric 	}
10359630Seric 
103651920Seric 	CurrentLA = getla();	/* get load average */
103740934Srick 
103810096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
103925687Seric 	if (nrequests > QUEUESIZE)
104025687Seric 		printf(", only %d printed", QUEUESIZE);
104124979Seric 	if (Verbose)
104225032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
104324979Seric 	else
104424979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10459630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10469630Seric 	{
10479630Seric 		struct stat st;
104810070Seric 		auto time_t submittime = 0;
104910070Seric 		long dfsize = -1;
105010108Seric 		char message[MAXLINE];
105151937Seric # ifdef LOCKF
105251937Seric 		struct flock lfd;
105351937Seric # endif
105424941Seric 		extern bool shouldqueue();
10559630Seric 
105617468Seric 		f = fopen(w->w_name, "r");
105717468Seric 		if (f == NULL)
105817468Seric 		{
105917468Seric 			errno = 0;
106017468Seric 			continue;
106117468Seric 		}
10629630Seric 		printf("%7s", w->w_name + 2);
106351835Seric # ifdef LOCKF
106451937Seric 		lfd.l_type = F_RDLCK;
106551937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
106651937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
106751835Seric # else
106840934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
106951835Seric # endif
107010070Seric 			printf("*");
107157438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
107224941Seric 			printf("X");
107310070Seric 		else
107410070Seric 			printf(" ");
107510070Seric 		errno = 0;
107617468Seric 
107710108Seric 		message[0] = '\0';
10789630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10799630Seric 		{
108053400Seric 			register int i;
108153400Seric 
10829630Seric 			fixcrlf(buf, TRUE);
10839630Seric 			switch (buf[0])
10849630Seric 			{
108510108Seric 			  case 'M':	/* error message */
108653400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
108753400Seric 					i = sizeof message;
108853400Seric 				bcopy(&buf[1], message, i);
108953400Seric 				message[i] = '\0';
109010108Seric 				break;
109110108Seric 
10929630Seric 			  case 'S':	/* sender name */
109324979Seric 				if (Verbose)
109425027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
109525027Seric 					    w->w_pri, ctime(&submittime) + 4,
109624979Seric 					    &buf[1]);
109724979Seric 				else
109824979Seric 					printf("%8ld %.16s %.45s", dfsize,
109924979Seric 					    ctime(&submittime), &buf[1]);
110010108Seric 				if (message[0] != '\0')
110125027Seric 					printf("\n\t\t (%.60s)", message);
11029630Seric 				break;
110351920Seric 
110440973Sbostic 			  case 'C':	/* controlling user */
110554974Seric 				if (Verbose)
110654975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
110740973Sbostic 				break;
11089630Seric 
11099630Seric 			  case 'R':	/* recipient name */
111024979Seric 				if (Verbose)
111125027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
111224979Seric 				else
111324979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
11149630Seric 				break;
11159630Seric 
11169630Seric 			  case 'T':	/* creation time */
111724941Seric 				submittime = atol(&buf[1]);
11189630Seric 				break;
111910070Seric 
112010070Seric 			  case 'D':	/* data file name */
112110070Seric 				if (stat(&buf[1], &st) >= 0)
112210070Seric 					dfsize = st.st_size;
112310070Seric 				break;
11249630Seric 			}
11259630Seric 		}
112610070Seric 		if (submittime == (time_t) 0)
112710070Seric 			printf(" (no control file)");
11289630Seric 		printf("\n");
112923098Seric 		(void) fclose(f);
11309630Seric 	}
11319630Seric }
11329630Seric 
113356795Seric # endif /* QUEUE */
113417468Seric /*
113517468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
113617468Seric **
113717468Seric **	Assigns an id code if one does not already exist.
113817468Seric **	This code is very careful to avoid trashing existing files
113917468Seric **	under any circumstances.
114017468Seric **
114117468Seric **	Parameters:
114217468Seric **		e -- envelope to build it in/from.
114317468Seric **		type -- the file type, used as the first character
114417468Seric **			of the file name.
114517468Seric **
114617468Seric **	Returns:
114717468Seric **		a pointer to the new file name (in a static buffer).
114817468Seric **
114917468Seric **	Side Effects:
115051920Seric **		If no id code is already assigned, queuename will
115151920Seric **		assign an id code, create a qf file, and leave a
115251920Seric **		locked, open-for-write file pointer in the envelope.
115317468Seric */
115417468Seric 
115517468Seric char *
115617468Seric queuename(e, type)
115717468Seric 	register ENVELOPE *e;
115817468Seric 	char type;
115917468Seric {
116017468Seric 	static char buf[MAXNAME];
116117468Seric 	static int pid = -1;
116217468Seric 	char c1 = 'A';
116317468Seric 	char c2 = 'A';
116417468Seric 
116517468Seric 	if (e->e_id == NULL)
116617468Seric 	{
116717468Seric 		char qf[20];
116817468Seric 
116917468Seric 		/* find a unique id */
117017468Seric 		if (pid != getpid())
117117468Seric 		{
117217468Seric 			/* new process -- start back at "AA" */
117317468Seric 			pid = getpid();
117417468Seric 			c1 = 'A';
117517468Seric 			c2 = 'A' - 1;
117617468Seric 		}
117717468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
117817468Seric 
117917468Seric 		while (c1 < '~' || c2 < 'Z')
118017468Seric 		{
118117468Seric 			int i;
118251937Seric # ifdef LOCKF
118351937Seric 			struct flock lfd;
118451937Seric # endif
118517468Seric 
118617468Seric 			if (c2 >= 'Z')
118717468Seric 			{
118817468Seric 				c1++;
118917468Seric 				c2 = 'A' - 1;
119017468Seric 			}
119140934Srick 			qf[2] = c1;
119240934Srick 			qf[3] = ++c2;
119317468Seric 			if (tTd(7, 20))
119440934Srick 				printf("queuename: trying \"%s\"\n", qf);
119517468Seric 
119640934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
119751920Seric 			if (i < 0)
119851920Seric 			{
119951920Seric 				if (errno == EEXIST)
120051920Seric 					continue;
120151920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
120251920Seric 					qf, QueueDir);
120351920Seric 				exit(EX_UNAVAILABLE);
120451920Seric 			}
120551920Seric # ifdef LOCKF
120651937Seric 			lfd.l_type = F_WRLCK;
120751937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
120851937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
120951920Seric # else
121051920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
121151920Seric # endif
121251920Seric 			{
121351920Seric 				e->e_lockfp = fdopen(i, "w");
121440934Srick 				break;
121517468Seric 			}
121651920Seric 
121751920Seric 			/* a reader got the file; abandon it and try again */
121851920Seric 			(void) close(i);
121917468Seric 		}
122017468Seric 		if (c1 >= '~' && c2 >= 'Z')
122117468Seric 		{
122217468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
122317468Seric 				qf, QueueDir);
122417468Seric 			exit(EX_OSERR);
122517468Seric 		}
122617468Seric 		e->e_id = newstr(&qf[2]);
122717468Seric 		define('i', e->e_id, e);
122817468Seric 		if (tTd(7, 1))
122917468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
123017468Seric # ifdef LOG
123158020Seric 		if (LogLevel > 93)
123217468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
123356795Seric # endif /* LOG */
123417468Seric 	}
123517468Seric 
123617468Seric 	if (type == '\0')
123717468Seric 		return (NULL);
123817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
123917468Seric 	if (tTd(7, 2))
124017468Seric 		printf("queuename: %s\n", buf);
124117468Seric 	return (buf);
124217468Seric }
124317468Seric /*
124417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
124517468Seric **
124617468Seric **	Parameters:
124717468Seric **		e -- the envelope to unlock.
124817468Seric **
124917468Seric **	Returns:
125017468Seric **		none
125117468Seric **
125217468Seric **	Side Effects:
125317468Seric **		unlocks the queue for `e'.
125417468Seric */
125517468Seric 
125617468Seric unlockqueue(e)
125717468Seric 	ENVELOPE *e;
125817468Seric {
125951920Seric 	/* if there is a lock file in the envelope, close it */
126051920Seric 	if (e->e_lockfp != NULL)
126151920Seric 		fclose(e->e_lockfp);
126251920Seric 	e->e_lockfp = NULL;
126351920Seric 
126417468Seric 	/* remove the transcript */
126517468Seric # ifdef LOG
126658020Seric 	if (LogLevel > 87)
126717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
126856795Seric # endif /* LOG */
126917468Seric 	if (!tTd(51, 4))
127017468Seric 		xunlink(queuename(e, 'x'));
127117468Seric 
127217468Seric }
127340973Sbostic /*
127454974Seric **  SETCTLUSER -- create a controlling address
127540973Sbostic **
127654974Seric **	Create a fake "address" given only a local login name; this is
127754974Seric **	used as a "controlling user" for future recipient addresses.
127840973Sbostic **
127940973Sbostic **	Parameters:
128054974Seric **		user -- the user name of the controlling user.
128140973Sbostic **
128240973Sbostic **	Returns:
128354974Seric **		An address descriptor for the controlling user.
128440973Sbostic **
128540973Sbostic **	Side Effects:
128640973Sbostic **		none.
128740973Sbostic */
128840973Sbostic 
128954974Seric ADDRESS *
129054974Seric setctluser(user)
129154974Seric 	char *user;
129240973Sbostic {
129354974Seric 	register ADDRESS *a;
129440973Sbostic 	struct passwd *pw;
129540973Sbostic 
129640973Sbostic 	/*
129754974Seric 	**  See if this clears our concept of controlling user.
129840973Sbostic 	*/
129940973Sbostic 
130054974Seric 	if (user == NULL || *user == '\0')
130154974Seric 		return NULL;
130240973Sbostic 
130340973Sbostic 	/*
130454974Seric 	**  Set up addr fields for controlling user.
130540973Sbostic 	*/
130640973Sbostic 
130754974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
130854974Seric 	bzero((char *) a, sizeof *a);
130954974Seric 	if ((pw = getpwnam(user)) != NULL)
131040973Sbostic 	{
131140973Sbostic 		a->q_home = newstr(pw->pw_dir);
131240973Sbostic 		a->q_uid = pw->pw_uid;
131340973Sbostic 		a->q_gid = pw->pw_gid;
131457642Seric 		a->q_user = newstr(user);
131540973Sbostic 	}
131640973Sbostic 	else
131740973Sbostic 	{
131840973Sbostic 		a->q_uid = DefUid;
131940973Sbostic 		a->q_gid = DefGid;
132057642Seric 		a->q_user = newstr(DefUser);
132140973Sbostic 	}
132240973Sbostic 
1323*58294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
132456328Seric 	a->q_mailer = LocalMailer;
132554974Seric 	return a;
132640973Sbostic }
1327