xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58523)
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*58523Seric static char sccsid[] = "@(#)queue.c	6.25 (Berkeley) 03/06/93 (with queueing)";
1433731Sbostic #else
15*58523Seric static char sccsid[] = "@(#)queue.c	6.25 (Berkeley) 03/06/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)
24058337Seric 					logdelivery(NULL, NULL, "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
50658318Seric # define NEED_R		004
50758318Seric # define NEED_S		010
5084632Seric 
50924941Seric orderq(doall)
51024941Seric 	bool doall;
5114632Seric {
5126625Sglickman 	register struct direct *d;
5134632Seric 	register WORK *w;
5146625Sglickman 	DIR *f;
5154632Seric 	register int i;
51625687Seric 	WORK wlist[QUEUESIZE+1];
51710070Seric 	int wn = -1;
5184632Seric 	extern workcmpf();
5194632Seric 
52058318Seric 	if (tTd(41, 1))
52158318Seric 	{
52258318Seric 		printf("orderq:\n");
52358318Seric 		if (QueueLimitId != NULL)
52458318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
52558318Seric 		if (QueueLimitSender != NULL)
52658318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
52758318Seric 		if (QueueLimitRecipient != NULL)
52858318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
52958318Seric 	}
53058318Seric 
5314632Seric 	/* clear out old WorkQ */
5324632Seric 	for (w = WorkQ; w != NULL; )
5334632Seric 	{
5344632Seric 		register WORK *nw = w->w_next;
5354632Seric 
5364632Seric 		WorkQ = nw;
5374632Seric 		free(w->w_name);
5384632Seric 		free((char *) w);
5394632Seric 		w = nw;
5404632Seric 	}
5414632Seric 
5424632Seric 	/* open the queue directory */
5438148Seric 	f = opendir(".");
5444632Seric 	if (f == NULL)
5454632Seric 	{
5468148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
54710070Seric 		return (0);
5484632Seric 	}
5494632Seric 
5504632Seric 	/*
5514632Seric 	**  Read the work directory.
5524632Seric 	*/
5534632Seric 
55410070Seric 	while ((d = readdir(f)) != NULL)
5554632Seric 	{
5569377Seric 		FILE *cf;
5574632Seric 		char lbuf[MAXNAME];
55857642Seric 		extern bool shouldqueue();
55958318Seric 		extern bool strcontainedin();
5604632Seric 
5614632Seric 		/* is this an interesting entry? */
5627812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5634632Seric 			continue;
5644632Seric 
56558318Seric 		if (QueueLimitId != NULL &&
56658318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
56758318Seric 			continue;
56858318Seric 
56958020Seric 		if (strlen(d->d_name) != 9)
57058020Seric 		{
57158020Seric 			if (Verbose)
57258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
57358020Seric #ifdef LOG
57458020Seric 			if (LogLevel > 3)
57558020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
57658020Seric 					d->d_name);
57758020Seric #endif
57858020Seric 			if (strlen(d->d_name) >= MAXNAME)
57958020Seric 				d->d_name[MAXNAME - 1] = '\0';
58058020Seric 			strcpy(lbuf, d->d_name);
58158020Seric 			lbuf[0] = 'Q';
58258020Seric 			(void) rename(d->d_name, lbuf);
58358020Seric 			continue;
58458020Seric 		}
58558020Seric 
58610070Seric 		/* yes -- open control file (if not too many files) */
58725687Seric 		if (++wn >= QUEUESIZE)
58810070Seric 			continue;
58958318Seric 
5908148Seric 		cf = fopen(d->d_name, "r");
5914632Seric 		if (cf == NULL)
5924632Seric 		{
5937055Seric 			/* this may be some random person sending hir msgs */
5947055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
59510090Seric 			if (tTd(41, 2))
59610090Seric 				printf("orderq: cannot open %s (%d)\n",
59710090Seric 					d->d_name, errno);
5987055Seric 			errno = 0;
59910090Seric 			wn--;
6004632Seric 			continue;
6014632Seric 		}
60225687Seric 		w = &wlist[wn];
60325687Seric 		w->w_name = newstr(d->d_name);
6044632Seric 
60525027Seric 		/* make sure jobs in creation don't clog queue */
60625687Seric 		w->w_pri = 0x7fffffff;
60725687Seric 		w->w_ctime = 0;
60825027Seric 
6094632Seric 		/* extract useful information */
61025687Seric 		i = NEED_P | NEED_T;
61158318Seric 		if (QueueLimitSender != NULL)
61258318Seric 			i |= NEED_S;
61358318Seric 		if (QueueLimitRecipient != NULL)
61458318Seric 			i |= NEED_R;
61525687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6164632Seric 		{
61724954Seric 			extern long atol();
61858318Seric 			extern bool strcontainedin();
61924954Seric 
62024941Seric 			switch (lbuf[0])
6214632Seric 			{
62224941Seric 			  case 'P':
62325687Seric 				w->w_pri = atol(&lbuf[1]);
62425687Seric 				i &= ~NEED_P;
6254632Seric 				break;
62625013Seric 
62725013Seric 			  case 'T':
62825687Seric 				w->w_ctime = atol(&lbuf[1]);
62925687Seric 				i &= ~NEED_T;
63025013Seric 				break;
63158318Seric 
63258318Seric 			  case 'R':
63358318Seric 				if (QueueLimitRecipient != NULL &&
63458318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
63558318Seric 					i &= ~NEED_R;
63658318Seric 				break;
63758318Seric 
63858318Seric 			  case 'S':
63958318Seric 				if (QueueLimitSender != NULL &&
64058318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
64158318Seric 					i &= ~NEED_S;
64258318Seric 				break;
6434632Seric 			}
6444632Seric 		}
6454632Seric 		(void) fclose(cf);
64624953Seric 
64758318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
64858318Seric 		    bitset(NEED_R|NEED_S, i))
64924953Seric 		{
65024953Seric 			/* don't even bother sorting this job in */
65124953Seric 			wn--;
65224953Seric 		}
6534632Seric 	}
6546625Sglickman 	(void) closedir(f);
65510090Seric 	wn++;
6564632Seric 
6574632Seric 	/*
6584632Seric 	**  Sort the work directory.
6594632Seric 	*/
6604632Seric 
66125687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6624632Seric 
6634632Seric 	/*
6644632Seric 	**  Convert the work list into canonical form.
6659377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6664632Seric 	*/
6674632Seric 
66824981Seric 	WorkQ = NULL;
66925687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6704632Seric 	{
6714632Seric 		w = (WORK *) xalloc(sizeof *w);
6724632Seric 		w->w_name = wlist[i].w_name;
6734632Seric 		w->w_pri = wlist[i].w_pri;
67425013Seric 		w->w_ctime = wlist[i].w_ctime;
67524981Seric 		w->w_next = WorkQ;
67624981Seric 		WorkQ = w;
6774632Seric 	}
6784632Seric 
6797677Seric 	if (tTd(40, 1))
6804632Seric 	{
6814632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6825037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6834632Seric 	}
68410070Seric 
68510090Seric 	return (wn);
6864632Seric }
6874632Seric /*
6887677Seric **  WORKCMPF -- compare function for ordering work.
6894632Seric **
6904632Seric **	Parameters:
6914632Seric **		a -- the first argument.
6924632Seric **		b -- the second argument.
6934632Seric **
6944632Seric **	Returns:
69524981Seric **		-1 if a < b
69624981Seric **		 0 if a == b
69724981Seric **		+1 if a > b
6984632Seric **
6994632Seric **	Side Effects:
7004632Seric **		none.
7014632Seric */
7024632Seric 
7034632Seric workcmpf(a, b)
7045037Seric 	register WORK *a;
7055037Seric 	register WORK *b;
7064632Seric {
70757438Seric 	long pa = a->w_pri;
70857438Seric 	long pb = b->w_pri;
70924941Seric 
71024941Seric 	if (pa == pb)
7114632Seric 		return (0);
71224941Seric 	else if (pa > pb)
71324981Seric 		return (1);
71424981Seric 	else
71510121Seric 		return (-1);
7164632Seric }
7174632Seric /*
7184632Seric **  DOWORK -- do a work request.
7194632Seric **
7204632Seric **	Parameters:
7214632Seric **		w -- the work request to be satisfied.
7224632Seric **
7234632Seric **	Returns:
7244632Seric **		none.
7254632Seric **
7264632Seric **	Side Effects:
7274632Seric **		The work request is satisfied if possible.
7284632Seric */
7294632Seric 
73055012Seric dowork(w, e)
7314632Seric 	register WORK *w;
73255012Seric 	register ENVELOPE *e;
7334632Seric {
7344632Seric 	register int i;
73524941Seric 	extern bool shouldqueue();
73651920Seric 	extern bool readqf();
7374632Seric 
7387677Seric 	if (tTd(40, 1))
7395037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7404632Seric 
7414632Seric 	/*
74224941Seric 	**  Ignore jobs that are too expensive for the moment.
7434632Seric 	*/
7444632Seric 
74557438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7464632Seric 	{
74724941Seric 		if (Verbose)
74824967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7494632Seric 		return;
7504632Seric 	}
7514632Seric 
75224941Seric 	/*
75324941Seric 	**  Fork for work.
75424941Seric 	*/
75524941Seric 
75624941Seric 	if (ForkQueueRuns)
75724941Seric 	{
75824941Seric 		i = fork();
75924941Seric 		if (i < 0)
76024941Seric 		{
76124941Seric 			syserr("dowork: cannot fork");
76224941Seric 			return;
76324941Seric 		}
76424941Seric 	}
76524941Seric 	else
76624941Seric 	{
76724941Seric 		i = 0;
76824941Seric 	}
76924941Seric 
7704632Seric 	if (i == 0)
7714632Seric 	{
7724632Seric 		/*
7734632Seric 		**  CHILD
7748148Seric 		**	Lock the control file to avoid duplicate deliveries.
7758148Seric 		**		Then run the file as though we had just read it.
7767350Seric 		**	We save an idea of the temporary name so we
7777350Seric 		**		can recover on interrupt.
7784632Seric 		*/
7794632Seric 
7807763Seric 		/* set basic modes, etc. */
7817356Seric 		(void) alarm(0);
78255012Seric 		clearenvelope(e, FALSE);
7834632Seric 		QueueRun = TRUE;
7849377Seric 		ErrorMode = EM_MAIL;
78555012Seric 		e->e_id = &w->w_name[2];
7867876Seric # ifdef LOG
78758020Seric 		if (LogLevel > 76)
78855012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7897881Seric 			       getpid());
79056795Seric # endif /* LOG */
7917763Seric 
7927763Seric 		/* don't use the headers from sendmail.cf... */
79355012Seric 		e->e_header = NULL;
7947763Seric 
79551920Seric 		/* read the queue control file -- return if locked */
79655012Seric 		if (!readqf(e))
7976980Seric 		{
79824941Seric 			if (ForkQueueRuns)
79924941Seric 				exit(EX_OK);
80024941Seric 			else
80124941Seric 				return;
8026980Seric 		}
8036980Seric 
80455012Seric 		e->e_flags |= EF_INQUEUE;
80557642Seric 		eatheader(e, TRUE);
8066980Seric 
8076980Seric 		/* do the delivery */
80855012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
80955012Seric 			sendall(e, SM_DELIVER);
8106980Seric 
8116980Seric 		/* finish up and exit */
81224941Seric 		if (ForkQueueRuns)
81324941Seric 			finis();
81424941Seric 		else
81555012Seric 			dropenvelope(e);
8164632Seric 	}
81724941Seric 	else
81824941Seric 	{
81924941Seric 		/*
82024941Seric 		**  Parent -- pick up results.
82124941Seric 		*/
8224632Seric 
82324941Seric 		errno = 0;
82424941Seric 		(void) waitfor(i);
82524941Seric 	}
8264632Seric }
8274632Seric /*
8284632Seric **  READQF -- read queue file and set up environment.
8294632Seric **
8304632Seric **	Parameters:
8319377Seric **		e -- the envelope of the job to run.
8324632Seric **
8334632Seric **	Returns:
83451920Seric **		TRUE if it successfully read the queue file.
83551920Seric **		FALSE otherwise.
8364632Seric **
8374632Seric **	Side Effects:
83851920Seric **		The queue file is returned locked.
8394632Seric */
8404632Seric 
84151920Seric bool
84251920Seric readqf(e)
8439377Seric 	register ENVELOPE *e;
8444632Seric {
84517477Seric 	char *qf;
84617477Seric 	register FILE *qfp;
84754974Seric 	ADDRESS *ctladdr;
84856400Seric 	struct stat st;
84957135Seric 	char *bp;
85057135Seric 	char buf[MAXLINE];
8519348Seric 	extern char *fgetfolded();
85224954Seric 	extern long atol();
85354974Seric 	extern ADDRESS *setctluser();
85451937Seric # ifdef LOCKF
85551937Seric 	struct flock lfd;
85651937Seric # endif
8574632Seric 
8584632Seric 	/*
85917468Seric 	**  Read and process the file.
8604632Seric 	*/
8614632Seric 
86217477Seric 	qf = queuename(e, 'q');
86351937Seric 	qfp = fopen(qf, "r+");
86417477Seric 	if (qfp == NULL)
86517477Seric 	{
86640934Srick 		if (errno != ENOENT)
86740934Srick 			syserr("readqf: no control file %s", qf);
86851920Seric 		return FALSE;
86917477Seric 	}
87040934Srick 
87156400Seric 	/*
87256400Seric 	**  Check the queue file for plausibility to avoid attacks.
87356400Seric 	*/
87456400Seric 
87556400Seric 	if (fstat(fileno(qfp), &st) < 0)
87656400Seric 	{
87756400Seric 		/* must have been being processed by someone else */
87856400Seric 		fclose(qfp);
87956400Seric 		return FALSE;
88056400Seric 	}
88156400Seric 
88257135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
88356400Seric 	{
88456400Seric # ifdef LOG
88556400Seric 		if (LogLevel > 0)
88656400Seric 		{
88756400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
88856400Seric 				e->e_id, st.st_uid, st.st_mode);
88956400Seric 		}
89056795Seric # endif /* LOG */
89156400Seric 		fclose(qfp);
89256400Seric 		return FALSE;
89356400Seric 	}
89456400Seric 
89551835Seric # ifdef LOCKF
89651937Seric 	lfd.l_type = F_WRLCK;
89751937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
89851937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
89951835Seric # else
90040934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
90151835Seric # endif
90240934Srick 	{
90358062Seric 		if (errno == EWOULDBLOCK)
90458062Seric 		{
90558062Seric 			/* being processed by another queuer */
90658062Seric 			if (Verbose)
90758062Seric 				printf("%s: locked\n", e->e_id);
90851920Seric # ifdef LOG
90958062Seric 			if (LogLevel > 19)
91058062Seric 				syslog(LOG_DEBUG, "%s: locked", e->e_id);
91156795Seric # endif /* LOG */
91258062Seric 		}
91358062Seric 		else
91458062Seric 		{
91558062Seric 			syserr("%s: flock failure", e->e_id);
91658062Seric 		}
91740934Srick 		(void) fclose(qfp);
91851920Seric 		return FALSE;
91940934Srick 	}
92040934Srick 
92151920Seric 	/* save this lock */
92251920Seric 	e->e_lockfp = qfp;
92351920Seric 
92440934Srick 	/* do basic system initialization */
92555012Seric 	initsys(e);
92640934Srick 
92717477Seric 	FileName = qf;
9289377Seric 	LineNumber = 0;
92951920Seric 	if (Verbose)
9309377Seric 		printf("\nRunning %s\n", e->e_id);
93154974Seric 	ctladdr = NULL;
93257135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9334632Seric 	{
93457529Seric 		struct stat st;
93557529Seric 
93626504Seric 		if (tTd(40, 4))
93757135Seric 			printf("+++++ %s\n", bp);
93857135Seric 		switch (bp[0])
9394632Seric 		{
94040973Sbostic 		  case 'C':		/* specify controlling user */
94157135Seric 			ctladdr = setctluser(&bp[1]);
94240973Sbostic 			break;
94340973Sbostic 
9444632Seric 		  case 'R':		/* specify recipient */
94558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9464632Seric 			break;
9474632Seric 
94825687Seric 		  case 'E':		/* specify error recipient */
94958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
95025687Seric 			break;
95125687Seric 
9524632Seric 		  case 'H':		/* header */
95357135Seric 			(void) chompheader(&bp[1], FALSE, e);
9544632Seric 			break;
9554632Seric 
95610108Seric 		  case 'M':		/* message */
95757135Seric 			e->e_message = newstr(&bp[1]);
95810108Seric 			break;
95910108Seric 
9604632Seric 		  case 'S':		/* sender */
96158333Seric 			setsender(newstr(&bp[1]), e, NULL);
9624632Seric 			break;
9634632Seric 
9644632Seric 		  case 'D':		/* data file name */
96557135Seric 			e->e_df = newstr(&bp[1]);
9669544Seric 			e->e_dfp = fopen(e->e_df, "r");
9679544Seric 			if (e->e_dfp == NULL)
96858020Seric 			{
9699377Seric 				syserr("readqf: cannot open %s", e->e_df);
97058020Seric 				e->e_msgsize = -1;
97158020Seric 			}
97258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
97357529Seric 				e->e_msgsize = st.st_size;
9744632Seric 			break;
9754632Seric 
9767860Seric 		  case 'T':		/* init time */
97757135Seric 			e->e_ctime = atol(&bp[1]);
9784632Seric 			break;
9794632Seric 
9804634Seric 		  case 'P':		/* message priority */
98157135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9824634Seric 			break;
9834634Seric 
98453400Seric 		  case '$':		/* define macro */
98557135Seric 			define(bp[1], newstr(&bp[2]), e);
98653400Seric 			break;
98753400Seric 
98824941Seric 		  case '\0':		/* blank line; ignore */
98924941Seric 			break;
99024941Seric 
9914632Seric 		  default:
99224941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
99357135Seric 				LineNumber, bp);
9944632Seric 			break;
9954632Seric 		}
99657135Seric 
99757135Seric 		if (bp != buf)
99857135Seric 			free(bp);
9994632Seric 	}
10009377Seric 
10019377Seric 	FileName = NULL;
100224941Seric 
100324941Seric 	/*
100424941Seric 	**  If we haven't read any lines, this queue file is empty.
100524941Seric 	**  Arrange to remove it without referencing any null pointers.
100624941Seric 	*/
100724941Seric 
100824941Seric 	if (LineNumber == 0)
100924941Seric 	{
101024941Seric 		errno = 0;
101124941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
101224941Seric 	}
101351920Seric 	return TRUE;
10144632Seric }
10154632Seric /*
10169630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10179630Seric **
10189630Seric **	Parameters:
10199630Seric **		none.
10209630Seric **
10219630Seric **	Returns:
10229630Seric **		none.
10239630Seric **
10249630Seric **	Side Effects:
10259630Seric **		Prints a listing of the mail queue on the standard output.
10269630Seric */
10275182Seric 
10289630Seric printqueue()
10299630Seric {
10309630Seric 	register WORK *w;
10319630Seric 	FILE *f;
103210070Seric 	int nrequests;
10339630Seric 	char buf[MAXLINE];
10349630Seric 
10359630Seric 	/*
103658250Seric 	**  Check for permission to print the queue
103758250Seric 	*/
103858250Seric 
1039*58523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
104058250Seric 	{
104158250Seric 		struct stat st;
1042*58523Seric # ifdef NGROUPS
1043*58523Seric 		int n;
1044*58523Seric 		int gidset[NGROUPS];
1045*58523Seric # endif
104658250Seric 
104758517Seric 		if (stat(QueueDir, &st) < 0)
104858250Seric 		{
104958250Seric 			syserr("Cannot stat %s", QueueDir);
105058250Seric 			return;
105158250Seric 		}
1052*58523Seric # ifdef NGROUPS
1053*58523Seric 		n = getgroups(NGROUPS, gidset);
1054*58523Seric 		while (--n >= 0)
1055*58523Seric 		{
1056*58523Seric 			if (gidset[n] == st.st_gid)
1057*58523Seric 				break;
1058*58523Seric 		}
1059*58523Seric 		if (n < 0)
1060*58523Seric # else
106158250Seric 		if (getgid() != st.st_gid)
1062*58523Seric # endif
106358250Seric 		{
106458250Seric 			usrerr("510 You are not permitted to see the queue");
106558250Seric 			setstat(EX_NOPERM);
106658250Seric 			return;
106758250Seric 		}
106858250Seric 	}
106958250Seric 
107058250Seric 	/*
10719630Seric 	**  Read and order the queue.
10729630Seric 	*/
10739630Seric 
107424941Seric 	nrequests = orderq(TRUE);
10759630Seric 
10769630Seric 	/*
10779630Seric 	**  Print the work list that we have read.
10789630Seric 	*/
10799630Seric 
10809630Seric 	/* first see if there is anything */
108110070Seric 	if (nrequests <= 0)
10829630Seric 	{
108310070Seric 		printf("Mail queue is empty\n");
10849630Seric 		return;
10859630Seric 	}
10869630Seric 
108751920Seric 	CurrentLA = getla();	/* get load average */
108840934Srick 
108910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
109025687Seric 	if (nrequests > QUEUESIZE)
109125687Seric 		printf(", only %d printed", QUEUESIZE);
109224979Seric 	if (Verbose)
109325032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
109424979Seric 	else
109524979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10969630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10979630Seric 	{
10989630Seric 		struct stat st;
109910070Seric 		auto time_t submittime = 0;
110010070Seric 		long dfsize = -1;
110110108Seric 		char message[MAXLINE];
110251937Seric # ifdef LOCKF
110351937Seric 		struct flock lfd;
110451937Seric # endif
110524941Seric 		extern bool shouldqueue();
11069630Seric 
110717468Seric 		f = fopen(w->w_name, "r");
110817468Seric 		if (f == NULL)
110917468Seric 		{
111017468Seric 			errno = 0;
111117468Seric 			continue;
111217468Seric 		}
11139630Seric 		printf("%7s", w->w_name + 2);
111451835Seric # ifdef LOCKF
111551937Seric 		lfd.l_type = F_RDLCK;
111651937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
111751937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
111851835Seric # else
111940934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
112051835Seric # endif
112110070Seric 			printf("*");
112257438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
112324941Seric 			printf("X");
112410070Seric 		else
112510070Seric 			printf(" ");
112610070Seric 		errno = 0;
112717468Seric 
112810108Seric 		message[0] = '\0';
11299630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11309630Seric 		{
113153400Seric 			register int i;
113253400Seric 
11339630Seric 			fixcrlf(buf, TRUE);
11349630Seric 			switch (buf[0])
11359630Seric 			{
113610108Seric 			  case 'M':	/* error message */
113753400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
113853400Seric 					i = sizeof message;
113953400Seric 				bcopy(&buf[1], message, i);
114053400Seric 				message[i] = '\0';
114110108Seric 				break;
114210108Seric 
11439630Seric 			  case 'S':	/* sender name */
114424979Seric 				if (Verbose)
114525027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
114625027Seric 					    w->w_pri, ctime(&submittime) + 4,
114724979Seric 					    &buf[1]);
114824979Seric 				else
114924979Seric 					printf("%8ld %.16s %.45s", dfsize,
115024979Seric 					    ctime(&submittime), &buf[1]);
115110108Seric 				if (message[0] != '\0')
115225027Seric 					printf("\n\t\t (%.60s)", message);
11539630Seric 				break;
115451920Seric 
115540973Sbostic 			  case 'C':	/* controlling user */
115654974Seric 				if (Verbose)
115754975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
115840973Sbostic 				break;
11599630Seric 
11609630Seric 			  case 'R':	/* recipient name */
116124979Seric 				if (Verbose)
116225027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
116324979Seric 				else
116424979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
11659630Seric 				break;
11669630Seric 
11679630Seric 			  case 'T':	/* creation time */
116824941Seric 				submittime = atol(&buf[1]);
11699630Seric 				break;
117010070Seric 
117110070Seric 			  case 'D':	/* data file name */
117210070Seric 				if (stat(&buf[1], &st) >= 0)
117310070Seric 					dfsize = st.st_size;
117410070Seric 				break;
11759630Seric 			}
11769630Seric 		}
117710070Seric 		if (submittime == (time_t) 0)
117810070Seric 			printf(" (no control file)");
11799630Seric 		printf("\n");
118023098Seric 		(void) fclose(f);
11819630Seric 	}
11829630Seric }
11839630Seric 
118456795Seric # endif /* QUEUE */
118517468Seric /*
118617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
118717468Seric **
118817468Seric **	Assigns an id code if one does not already exist.
118917468Seric **	This code is very careful to avoid trashing existing files
119017468Seric **	under any circumstances.
119117468Seric **
119217468Seric **	Parameters:
119317468Seric **		e -- envelope to build it in/from.
119417468Seric **		type -- the file type, used as the first character
119517468Seric **			of the file name.
119617468Seric **
119717468Seric **	Returns:
119817468Seric **		a pointer to the new file name (in a static buffer).
119917468Seric **
120017468Seric **	Side Effects:
120151920Seric **		If no id code is already assigned, queuename will
120251920Seric **		assign an id code, create a qf file, and leave a
120351920Seric **		locked, open-for-write file pointer in the envelope.
120417468Seric */
120517468Seric 
120617468Seric char *
120717468Seric queuename(e, type)
120817468Seric 	register ENVELOPE *e;
120917468Seric 	char type;
121017468Seric {
121117468Seric 	static char buf[MAXNAME];
121217468Seric 	static int pid = -1;
121317468Seric 	char c1 = 'A';
121417468Seric 	char c2 = 'A';
121517468Seric 
121617468Seric 	if (e->e_id == NULL)
121717468Seric 	{
121817468Seric 		char qf[20];
121917468Seric 
122017468Seric 		/* find a unique id */
122117468Seric 		if (pid != getpid())
122217468Seric 		{
122317468Seric 			/* new process -- start back at "AA" */
122417468Seric 			pid = getpid();
122517468Seric 			c1 = 'A';
122617468Seric 			c2 = 'A' - 1;
122717468Seric 		}
122817468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
122917468Seric 
123017468Seric 		while (c1 < '~' || c2 < 'Z')
123117468Seric 		{
123217468Seric 			int i;
123351937Seric # ifdef LOCKF
123451937Seric 			struct flock lfd;
123551937Seric # endif
123617468Seric 
123717468Seric 			if (c2 >= 'Z')
123817468Seric 			{
123917468Seric 				c1++;
124017468Seric 				c2 = 'A' - 1;
124117468Seric 			}
124240934Srick 			qf[2] = c1;
124340934Srick 			qf[3] = ++c2;
124417468Seric 			if (tTd(7, 20))
124540934Srick 				printf("queuename: trying \"%s\"\n", qf);
124617468Seric 
124740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
124851920Seric 			if (i < 0)
124951920Seric 			{
125051920Seric 				if (errno == EEXIST)
125151920Seric 					continue;
125251920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
125351920Seric 					qf, QueueDir);
125451920Seric 				exit(EX_UNAVAILABLE);
125551920Seric 			}
125651920Seric # ifdef LOCKF
125751937Seric 			lfd.l_type = F_WRLCK;
125851937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
125951937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
126051920Seric # else
126151920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
126251920Seric # endif
126351920Seric 			{
126451920Seric 				e->e_lockfp = fdopen(i, "w");
126540934Srick 				break;
126617468Seric 			}
126751920Seric 
126851920Seric 			/* a reader got the file; abandon it and try again */
126951920Seric 			(void) close(i);
127017468Seric 		}
127117468Seric 		if (c1 >= '~' && c2 >= 'Z')
127217468Seric 		{
127317468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
127417468Seric 				qf, QueueDir);
127517468Seric 			exit(EX_OSERR);
127617468Seric 		}
127717468Seric 		e->e_id = newstr(&qf[2]);
127817468Seric 		define('i', e->e_id, e);
127917468Seric 		if (tTd(7, 1))
128017468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
128117468Seric # ifdef LOG
128258020Seric 		if (LogLevel > 93)
128317468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
128456795Seric # endif /* LOG */
128517468Seric 	}
128617468Seric 
128717468Seric 	if (type == '\0')
128817468Seric 		return (NULL);
128917468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
129017468Seric 	if (tTd(7, 2))
129117468Seric 		printf("queuename: %s\n", buf);
129217468Seric 	return (buf);
129317468Seric }
129417468Seric /*
129517468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
129617468Seric **
129717468Seric **	Parameters:
129817468Seric **		e -- the envelope to unlock.
129917468Seric **
130017468Seric **	Returns:
130117468Seric **		none
130217468Seric **
130317468Seric **	Side Effects:
130417468Seric **		unlocks the queue for `e'.
130517468Seric */
130617468Seric 
130717468Seric unlockqueue(e)
130817468Seric 	ENVELOPE *e;
130917468Seric {
131051920Seric 	/* if there is a lock file in the envelope, close it */
131151920Seric 	if (e->e_lockfp != NULL)
131251920Seric 		fclose(e->e_lockfp);
131351920Seric 	e->e_lockfp = NULL;
131451920Seric 
131517468Seric 	/* remove the transcript */
131617468Seric # ifdef LOG
131758020Seric 	if (LogLevel > 87)
131817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
131956795Seric # endif /* LOG */
132017468Seric 	if (!tTd(51, 4))
132117468Seric 		xunlink(queuename(e, 'x'));
132217468Seric 
132317468Seric }
132440973Sbostic /*
132554974Seric **  SETCTLUSER -- create a controlling address
132640973Sbostic **
132754974Seric **	Create a fake "address" given only a local login name; this is
132854974Seric **	used as a "controlling user" for future recipient addresses.
132940973Sbostic **
133040973Sbostic **	Parameters:
133154974Seric **		user -- the user name of the controlling user.
133240973Sbostic **
133340973Sbostic **	Returns:
133454974Seric **		An address descriptor for the controlling user.
133540973Sbostic **
133640973Sbostic **	Side Effects:
133740973Sbostic **		none.
133840973Sbostic */
133940973Sbostic 
134054974Seric ADDRESS *
134154974Seric setctluser(user)
134254974Seric 	char *user;
134340973Sbostic {
134454974Seric 	register ADDRESS *a;
134540973Sbostic 	struct passwd *pw;
134640973Sbostic 
134740973Sbostic 	/*
134854974Seric 	**  See if this clears our concept of controlling user.
134940973Sbostic 	*/
135040973Sbostic 
135154974Seric 	if (user == NULL || *user == '\0')
135254974Seric 		return NULL;
135340973Sbostic 
135440973Sbostic 	/*
135554974Seric 	**  Set up addr fields for controlling user.
135640973Sbostic 	*/
135740973Sbostic 
135854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
135954974Seric 	bzero((char *) a, sizeof *a);
136054974Seric 	if ((pw = getpwnam(user)) != NULL)
136140973Sbostic 	{
136240973Sbostic 		a->q_home = newstr(pw->pw_dir);
136340973Sbostic 		a->q_uid = pw->pw_uid;
136440973Sbostic 		a->q_gid = pw->pw_gid;
136557642Seric 		a->q_user = newstr(user);
136640973Sbostic 	}
136740973Sbostic 	else
136840973Sbostic 	{
136940973Sbostic 		a->q_uid = DefUid;
137040973Sbostic 		a->q_gid = DefGid;
137157642Seric 		a->q_user = newstr(DefUser);
137240973Sbostic 	}
137340973Sbostic 
137458294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
137556328Seric 	a->q_mailer = LocalMailer;
137654974Seric 	return a;
137740973Sbostic }
1378