xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58737)
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*58737Seric static char sccsid[] = "@(#)queue.c	6.37 (Berkeley) 03/19/93 (with queueing)";
1433731Sbostic #else
15*58737Seric static char sccsid[] = "@(#)queue.c	6.37 (Berkeley) 03/19/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
214634Seric # include <signal.h>
224632Seric # include <errno.h>
2340973Sbostic # include <pwd.h>
2457977Seric # ifndef MAXNAMLEN
2557736Seric # include <dirent.h>
2657736Seric # endif
274632Seric 
2833731Sbostic # ifdef QUEUE
294632Seric 
304632Seric /*
319377Seric **  Work queue.
329377Seric */
339377Seric 
349377Seric struct work
359377Seric {
369377Seric 	char		*w_name;	/* name of control file */
379377Seric 	long		w_pri;		/* priority of message, see below */
3825013Seric 	time_t		w_ctime;	/* creation time of message */
399377Seric 	struct work	*w_next;	/* next in queue */
409377Seric };
419377Seric 
429377Seric typedef struct work	WORK;
439377Seric 
449377Seric WORK	*WorkQ;			/* queue of things to be done */
459377Seric /*
464632Seric **  QUEUEUP -- queue a message up for future transmission.
474632Seric **
484632Seric **	Parameters:
496980Seric **		e -- the envelope to queue up.
506999Seric **		queueall -- if TRUE, queue all addresses, rather than
516999Seric **			just those with the QQUEUEUP flag set.
529377Seric **		announce -- if TRUE, tell when you are queueing up.
534632Seric **
544632Seric **	Returns:
5551920Seric **		none.
564632Seric **
574632Seric **	Side Effects:
589377Seric **		The current request are saved in a control file.
5951920Seric **		The queue file is left locked.
604632Seric */
614632Seric 
629377Seric queueup(e, queueall, announce)
636980Seric 	register ENVELOPE *e;
646999Seric 	bool queueall;
659377Seric 	bool announce;
664632Seric {
677812Seric 	char *qf;
687812Seric 	register FILE *tfp;
694632Seric 	register HDR *h;
705007Seric 	register ADDRESS *q;
7151920Seric 	int fd;
7251920Seric 	int i;
7351920Seric 	bool newid;
7453400Seric 	register char *p;
7510173Seric 	MAILER nullmailer;
7654974Seric 	ADDRESS *lastctladdr;
7751920Seric 	char buf[MAXLINE], tf[MAXLINE];
7853400Seric 	extern char *macvalue();
7954974Seric 	extern ADDRESS *getctladdr();
804632Seric 
815037Seric 	/*
8217477Seric 	**  Create control file.
835037Seric 	*/
844632Seric 
8551920Seric 	newid = (e->e_id == NULL);
8651920Seric 	strcpy(tf, queuename(e, 't'));
8751920Seric 	tfp = e->e_lockfp;
8851920Seric 	if (tfp == NULL)
8951920Seric 		newid = FALSE;
9051920Seric 	if (newid)
9151835Seric 	{
9251920Seric 		tfp = e->e_lockfp;
9351920Seric 	}
9451920Seric 	else
9551920Seric 	{
9651920Seric 		/* get a locked tf file */
9751920Seric 		for (i = 100; --i >= 0; )
9851835Seric 		{
9958689Seric 			extern bool lockfile();
10051937Seric 
10151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10251920Seric 			if (fd < 0)
10351835Seric 			{
10451920Seric 				if (errno == EEXIST)
10551920Seric 					continue;
10658690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10741636Srick 			}
10858689Seric 
10958689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
11051920Seric 				break;
11158689Seric 
11251920Seric 			close(fd);
11341636Srick 		}
11441636Srick 
11551920Seric 		tfp = fdopen(fd, "w");
11651920Seric 	}
1174632Seric 
1187677Seric 	if (tTd(40, 1))
11917468Seric 		printf("queueing %s\n", e->e_id);
1204632Seric 
1214632Seric 	/*
1226980Seric 	**  If there is no data file yet, create one.
1236980Seric 	*/
1246980Seric 
1256980Seric 	if (e->e_df == NULL)
1266980Seric 	{
1276980Seric 		register FILE *dfp;
1289389Seric 		extern putbody();
1296980Seric 
1307812Seric 		e->e_df = newstr(queuename(e, 'd'));
13140934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13240934Srick 		if (fd < 0)
13358690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13440934Srick 		dfp = fdopen(fd, "w");
13510173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
13658680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1379389Seric 		e->e_putbody = putbody;
1386980Seric 	}
1396980Seric 
1406980Seric 	/*
1414632Seric 	**  Output future work requests.
14225687Seric 	**	Priority and creation time should be first, since
14325687Seric 	**	they are required by orderq.
1444632Seric 	*/
1454632Seric 
1469377Seric 	/* output message priority */
1479377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1489377Seric 
1499630Seric 	/* output creation time */
1509630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1519630Seric 
1524632Seric 	/* output name of data file */
1537812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1544632Seric 
15510108Seric 	/* message from envelope, if it exists */
15610108Seric 	if (e->e_message != NULL)
15710108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15810108Seric 
159*58737Seric 	/* send various flag bits through */
160*58737Seric 	p = buf;
161*58737Seric 	if (bitset(EF_WARNING, e->e_flags))
162*58737Seric 		*p++ = 'w';
163*58737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
164*58737Seric 		*p++ = 'r';
165*58737Seric 	*p++ = '\0';
166*58737Seric 	if (buf[0] != '\0')
167*58737Seric 		fprintf(tfp, "F%s\n", buf);
168*58737Seric 
16953400Seric 	/* $r and $s macro values */
17053400Seric 	if ((p = macvalue('r', e)) != NULL)
17153400Seric 		fprintf(tfp, "$r%s\n", p);
17253400Seric 	if ((p = macvalue('s', e)) != NULL)
17353400Seric 		fprintf(tfp, "$s%s\n", p);
17453400Seric 
1754632Seric 	/* output name of sender */
1767812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1774632Seric 
17855360Seric 	/* output list of error recipients */
17955360Seric 	lastctladdr = NULL;
18055360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18155360Seric 	{
18258680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18355360Seric 		{
18455360Seric 			ADDRESS *ctladdr;
18555360Seric 
18655360Seric 			ctladdr = getctladdr(q);
18755360Seric 			if (ctladdr != lastctladdr)
18855360Seric 			{
18955360Seric 				printctladdr(ctladdr, tfp);
19055360Seric 				lastctladdr = ctladdr;
19155360Seric 			}
19255360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
19355360Seric 		}
19455360Seric 	}
19555360Seric 
1964632Seric 	/* output list of recipient addresses */
1976980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1984632Seric 	{
19958250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
20058680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2018245Seric 		{
20254974Seric 			ADDRESS *ctladdr;
20340973Sbostic 
20454975Seric 			ctladdr = getctladdr(q);
20554975Seric 			if (ctladdr != lastctladdr)
20654974Seric 			{
20754974Seric 				printctladdr(ctladdr, tfp);
20854974Seric 				lastctladdr = ctladdr;
20954974Seric 			}
2107812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2119377Seric 			if (announce)
2129377Seric 			{
2139377Seric 				e->e_to = q->q_paddr;
21458151Seric 				message("queued");
21558020Seric 				if (LogLevel > 8)
21658337Seric 					logdelivery(NULL, NULL, "queued", e);
2179377Seric 				e->e_to = NULL;
2189377Seric 			}
2199387Seric 			if (tTd(40, 1))
2209387Seric 			{
2219387Seric 				printf("queueing ");
2229387Seric 				printaddr(q, FALSE);
2239387Seric 			}
2248245Seric 		}
2254632Seric 	}
2264632Seric 
2279377Seric 	/*
2289377Seric 	**  Output headers for this message.
2299377Seric 	**	Expand macros completely here.  Queue run will deal with
2309377Seric 	**	everything as absolute headers.
2319377Seric 	**		All headers that must be relative to the recipient
2329377Seric 	**		can be cracked later.
23310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23410173Seric 	**	no effect on the addresses as they are output.
2359377Seric 	*/
2369377Seric 
23710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
23858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
23958020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24010349Seric 	nullmailer.m_eol = "\n";
24110173Seric 
24258050Seric 	define('g', "\201f", e);
2436980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2444632Seric 	{
24510686Seric 		extern bool bitzerop();
24610686Seric 
24712015Seric 		/* don't output null headers */
2484632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2494632Seric 			continue;
25012015Seric 
25112015Seric 		/* don't output resent headers on non-resent messages */
25212015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25312015Seric 			continue;
25412015Seric 
25512015Seric 		/* output this header */
2567812Seric 		fprintf(tfp, "H");
25712015Seric 
25812015Seric 		/* if conditional, output the set of conditions */
25910686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26010686Seric 		{
26110686Seric 			int j;
26210686Seric 
26323098Seric 			(void) putc('?', tfp);
26410686Seric 			for (j = '\0'; j <= '\177'; j++)
26510686Seric 				if (bitnset(j, h->h_mflags))
26623098Seric 					(void) putc(j, tfp);
26723098Seric 			(void) putc('?', tfp);
26810686Seric 		}
26912015Seric 
27012015Seric 		/* output the header: expand macros, convert addresses */
2717763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2727763Seric 		{
2737763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2748236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2757763Seric 		}
2768245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2779348Seric 		{
278*58737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
279*58737Seric 
280*58737Seric 			if (bitset(H_FROM, h->h_flags))
281*58737Seric 				oldstyle = FALSE;
282*58737Seric 
283*58737Seric 			commaize(h, h->h_value, tfp, oldstyle,
28455012Seric 				 &nullmailer, e);
2859348Seric 		}
2867763Seric 		else
2878245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2884632Seric 	}
2894632Seric 
2904632Seric 	/*
2914632Seric 	**  Clean up.
2924632Seric 	*/
2934632Seric 
29458732Seric 	fflush(tfp);
29558732Seric 	if (ferror(tfp))
29658732Seric 	{
29758732Seric 		if (newid)
29858732Seric 			syserr("!552 Error writing control file %s", tf);
29958732Seric 		else
30058732Seric 			syserr("!452 Error writing control file %s", tf);
30158732Seric 	}
30258732Seric 
30351920Seric 	if (!newid)
30451920Seric 	{
30551920Seric 		qf = queuename(e, 'q');
30651920Seric 		if (rename(tf, qf) < 0)
30751920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
30851920Seric 		if (e->e_lockfp != NULL)
30958680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
31051920Seric 		e->e_lockfp = tfp;
31151920Seric 	}
31251920Seric 	else
31351920Seric 		qf = tf;
31441636Srick 	errno = 0;
3157391Seric 
3167677Seric # ifdef LOG
3177677Seric 	/* save log info */
31858020Seric 	if (LogLevel > 79)
3197878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
32056795Seric # endif /* LOG */
32140934Srick 	fflush(tfp);
32251920Seric 	return;
3234632Seric }
32454974Seric 
32554974Seric printctladdr(a, tfp)
32654974Seric 	ADDRESS *a;
32754974Seric 	FILE *tfp;
32854974Seric {
32954974Seric 	char *u;
33054974Seric 	struct passwd *pw;
33154974Seric 	extern struct passwd *getpwuid();
33254974Seric 
33354974Seric 	if (a == NULL)
33454974Seric 	{
33554974Seric 		fprintf(tfp, "C\n");
33654974Seric 		return;
33754974Seric 	}
33854974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
33954974Seric 		u = DefUser;
34054974Seric 	else
34154974Seric 		u = pw->pw_name;
34254974Seric 	fprintf(tfp, "C%s\n", u);
34354974Seric }
34454974Seric 
3454632Seric /*
3464632Seric **  RUNQUEUE -- run the jobs in the queue.
3474632Seric **
3484632Seric **	Gets the stuff out of the queue in some presumably logical
3494632Seric **	order and processes them.
3504632Seric **
3514632Seric **	Parameters:
35224941Seric **		forkflag -- TRUE if the queue scanning should be done in
35324941Seric **			a child process.  We double-fork so it is not our
35424941Seric **			child and we don't have to clean up after it.
3554632Seric **
3564632Seric **	Returns:
3574632Seric **		none.
3584632Seric **
3594632Seric **	Side Effects:
3604632Seric **		runs things in the mail queue.
3614632Seric */
3624632Seric 
36355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
36455360Seric 
36555360Seric runqueue(forkflag)
3664639Seric 	bool forkflag;
3674632Seric {
36824953Seric 	extern bool shouldqueue();
36955360Seric 	register ENVELOPE *e;
37055360Seric 	extern ENVELOPE BlankEnvelope;
37155360Seric 	extern ENVELOPE *newenvelope();
37224953Seric 
3737466Seric 	/*
37424953Seric 	**  If no work will ever be selected, don't even bother reading
37524953Seric 	**  the queue.
37624953Seric 	*/
37724953Seric 
37851920Seric 	CurrentLA = getla();	/* get load average */
37940934Srick 
38058132Seric 	if (shouldqueue(0L, curtime()))
38124953Seric 	{
38224953Seric 		if (Verbose)
38324953Seric 			printf("Skipping queue run -- load average too high\n");
38458107Seric 		if (forkflag && QueueIntvl != 0)
38558107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
38655360Seric 		return;
38724953Seric 	}
38824953Seric 
38924953Seric 	/*
3907466Seric 	**  See if we want to go off and do other useful work.
3917466Seric 	*/
3924639Seric 
3934639Seric 	if (forkflag)
3944639Seric 	{
3957943Seric 		int pid;
3967943Seric 
3977943Seric 		pid = dofork();
3987943Seric 		if (pid != 0)
3994639Seric 		{
40046928Sbostic 			extern void reapchild();
40125184Seric 
4027943Seric 			/* parent -- pick up intermediate zombie */
40325184Seric #ifndef SIGCHLD
4049377Seric 			(void) waitfor(pid);
40556795Seric #else /* SIGCHLD */
40625184Seric 			(void) signal(SIGCHLD, reapchild);
40756795Seric #endif /* SIGCHLD */
4087690Seric 			if (QueueIntvl != 0)
4099348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4104639Seric 			return;
4114639Seric 		}
4127943Seric 		/* child -- double fork */
41325184Seric #ifndef SIGCHLD
4147943Seric 		if (fork() != 0)
4157943Seric 			exit(EX_OK);
41656795Seric #else /* SIGCHLD */
41725184Seric 		(void) signal(SIGCHLD, SIG_DFL);
41856795Seric #endif /* SIGCHLD */
4194639Seric 	}
42024941Seric 
42140934Srick 	setproctitle("running queue: %s", QueueDir);
42224941Seric 
4237876Seric # ifdef LOG
42458020Seric 	if (LogLevel > 69)
42555360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
42655360Seric 			QueueDir, getpid(), forkflag);
42756795Seric # endif /* LOG */
4284639Seric 
4297466Seric 	/*
43010205Seric 	**  Release any resources used by the daemon code.
43110205Seric 	*/
43210205Seric 
43310205Seric # ifdef DAEMON
43410205Seric 	clrdaemon();
43556795Seric # endif /* DAEMON */
43610205Seric 
43710205Seric 	/*
43855360Seric 	**  Create ourselves an envelope
43955360Seric 	*/
44055360Seric 
44155360Seric 	CurEnv = &QueueEnvelope;
44258179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
44355360Seric 	e->e_flags = BlankEnvelope.e_flags;
44455360Seric 
44555360Seric 	/*
44627175Seric 	**  Make sure the alias database is open.
44727175Seric 	*/
44827175Seric 
44955012Seric 	initaliases(AliasFile, FALSE, e);
45027175Seric 
45127175Seric 	/*
4527466Seric 	**  Start making passes through the queue.
4537466Seric 	**	First, read and sort the entire queue.
4547466Seric 	**	Then, process the work in that order.
4557466Seric 	**		But if you take too long, start over.
4567466Seric 	*/
4577466Seric 
4587943Seric 	/* order the existing work requests */
45924954Seric 	(void) orderq(FALSE);
4607690Seric 
4617943Seric 	/* process them once at a time */
4627943Seric 	while (WorkQ != NULL)
4634639Seric 	{
4647943Seric 		WORK *w = WorkQ;
4657881Seric 
4667943Seric 		WorkQ = WorkQ->w_next;
46755012Seric 		dowork(w, e);
4687943Seric 		free(w->w_name);
4697943Seric 		free((char *) w);
4704639Seric 	}
47129866Seric 
47229866Seric 	/* exit without the usual cleanup */
47355467Seric 	e->e_id = NULL;
47455467Seric 	finis();
4754634Seric }
4764634Seric /*
4774632Seric **  ORDERQ -- order the work queue.
4784632Seric **
4794632Seric **	Parameters:
48024941Seric **		doall -- if set, include everything in the queue (even
48124941Seric **			the jobs that cannot be run because the load
48224941Seric **			average is too high).  Otherwise, exclude those
48324941Seric **			jobs.
4844632Seric **
4854632Seric **	Returns:
48610121Seric **		The number of request in the queue (not necessarily
48710121Seric **		the number of requests in WorkQ however).
4884632Seric **
4894632Seric **	Side Effects:
4904632Seric **		Sets WorkQ to the queue of available work, in order.
4914632Seric */
4924632Seric 
49325687Seric # define NEED_P		001
49425687Seric # define NEED_T		002
49558318Seric # define NEED_R		004
49658318Seric # define NEED_S		010
4974632Seric 
49824941Seric orderq(doall)
49924941Seric 	bool doall;
5004632Seric {
5016625Sglickman 	register struct direct *d;
5024632Seric 	register WORK *w;
5036625Sglickman 	DIR *f;
5044632Seric 	register int i;
50525687Seric 	WORK wlist[QUEUESIZE+1];
50610070Seric 	int wn = -1;
5074632Seric 	extern workcmpf();
5084632Seric 
50958318Seric 	if (tTd(41, 1))
51058318Seric 	{
51158318Seric 		printf("orderq:\n");
51258318Seric 		if (QueueLimitId != NULL)
51358318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
51458318Seric 		if (QueueLimitSender != NULL)
51558318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
51658318Seric 		if (QueueLimitRecipient != NULL)
51758318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
51858318Seric 	}
51958318Seric 
5204632Seric 	/* clear out old WorkQ */
5214632Seric 	for (w = WorkQ; w != NULL; )
5224632Seric 	{
5234632Seric 		register WORK *nw = w->w_next;
5244632Seric 
5254632Seric 		WorkQ = nw;
5264632Seric 		free(w->w_name);
5274632Seric 		free((char *) w);
5284632Seric 		w = nw;
5294632Seric 	}
5304632Seric 
5314632Seric 	/* open the queue directory */
5328148Seric 	f = opendir(".");
5334632Seric 	if (f == NULL)
5344632Seric 	{
5358148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
53610070Seric 		return (0);
5374632Seric 	}
5384632Seric 
5394632Seric 	/*
5404632Seric 	**  Read the work directory.
5414632Seric 	*/
5424632Seric 
54310070Seric 	while ((d = readdir(f)) != NULL)
5444632Seric 	{
5459377Seric 		FILE *cf;
5464632Seric 		char lbuf[MAXNAME];
54757642Seric 		extern bool shouldqueue();
54858318Seric 		extern bool strcontainedin();
5494632Seric 
5504632Seric 		/* is this an interesting entry? */
5517812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5524632Seric 			continue;
5534632Seric 
55458318Seric 		if (QueueLimitId != NULL &&
55558318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
55658318Seric 			continue;
55758318Seric 
55858722Seric 		/*
55958722Seric 		**  Check queue name for plausibility.  This handles
56058722Seric 		**  both old and new type ids.
56158722Seric 		*/
56258722Seric 
56358722Seric 		i = strlen(d->d_name);
56458722Seric 		if (i != 9 && i != 10)
56558020Seric 		{
56658020Seric 			if (Verbose)
56758020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
56858020Seric #ifdef LOG
56958020Seric 			if (LogLevel > 3)
57058020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
57158020Seric 					d->d_name);
57258020Seric #endif
57358020Seric 			if (strlen(d->d_name) >= MAXNAME)
57458020Seric 				d->d_name[MAXNAME - 1] = '\0';
57558020Seric 			strcpy(lbuf, d->d_name);
57658020Seric 			lbuf[0] = 'Q';
57758020Seric 			(void) rename(d->d_name, lbuf);
57858020Seric 			continue;
57958020Seric 		}
58058020Seric 
58110070Seric 		/* yes -- open control file (if not too many files) */
58225687Seric 		if (++wn >= QUEUESIZE)
58310070Seric 			continue;
58458318Seric 
5858148Seric 		cf = fopen(d->d_name, "r");
5864632Seric 		if (cf == NULL)
5874632Seric 		{
5887055Seric 			/* this may be some random person sending hir msgs */
5897055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
59010090Seric 			if (tTd(41, 2))
59110090Seric 				printf("orderq: cannot open %s (%d)\n",
59210090Seric 					d->d_name, errno);
5937055Seric 			errno = 0;
59410090Seric 			wn--;
5954632Seric 			continue;
5964632Seric 		}
59725687Seric 		w = &wlist[wn];
59825687Seric 		w->w_name = newstr(d->d_name);
5994632Seric 
60025027Seric 		/* make sure jobs in creation don't clog queue */
60125687Seric 		w->w_pri = 0x7fffffff;
60225687Seric 		w->w_ctime = 0;
60325027Seric 
6044632Seric 		/* extract useful information */
60525687Seric 		i = NEED_P | NEED_T;
60658318Seric 		if (QueueLimitSender != NULL)
60758318Seric 			i |= NEED_S;
60858318Seric 		if (QueueLimitRecipient != NULL)
60958318Seric 			i |= NEED_R;
61025687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6114632Seric 		{
61224954Seric 			extern long atol();
61358318Seric 			extern bool strcontainedin();
61424954Seric 
61524941Seric 			switch (lbuf[0])
6164632Seric 			{
61724941Seric 			  case 'P':
61825687Seric 				w->w_pri = atol(&lbuf[1]);
61925687Seric 				i &= ~NEED_P;
6204632Seric 				break;
62125013Seric 
62225013Seric 			  case 'T':
62325687Seric 				w->w_ctime = atol(&lbuf[1]);
62425687Seric 				i &= ~NEED_T;
62525013Seric 				break;
62658318Seric 
62758318Seric 			  case 'R':
62858318Seric 				if (QueueLimitRecipient != NULL &&
62958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
63058318Seric 					i &= ~NEED_R;
63158318Seric 				break;
63258318Seric 
63358318Seric 			  case 'S':
63458318Seric 				if (QueueLimitSender != NULL &&
63558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
63658318Seric 					i &= ~NEED_S;
63758318Seric 				break;
6384632Seric 			}
6394632Seric 		}
6404632Seric 		(void) fclose(cf);
64124953Seric 
64258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
64358318Seric 		    bitset(NEED_R|NEED_S, i))
64424953Seric 		{
64524953Seric 			/* don't even bother sorting this job in */
64624953Seric 			wn--;
64724953Seric 		}
6484632Seric 	}
6496625Sglickman 	(void) closedir(f);
65010090Seric 	wn++;
6514632Seric 
6524632Seric 	/*
6534632Seric 	**  Sort the work directory.
6544632Seric 	*/
6554632Seric 
65625687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6574632Seric 
6584632Seric 	/*
6594632Seric 	**  Convert the work list into canonical form.
6609377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6614632Seric 	*/
6624632Seric 
66324981Seric 	WorkQ = NULL;
66425687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6654632Seric 	{
6664632Seric 		w = (WORK *) xalloc(sizeof *w);
6674632Seric 		w->w_name = wlist[i].w_name;
6684632Seric 		w->w_pri = wlist[i].w_pri;
66925013Seric 		w->w_ctime = wlist[i].w_ctime;
67024981Seric 		w->w_next = WorkQ;
67124981Seric 		WorkQ = w;
6724632Seric 	}
6734632Seric 
6747677Seric 	if (tTd(40, 1))
6754632Seric 	{
6764632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6775037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6784632Seric 	}
67910070Seric 
68010090Seric 	return (wn);
6814632Seric }
6824632Seric /*
6837677Seric **  WORKCMPF -- compare function for ordering work.
6844632Seric **
6854632Seric **	Parameters:
6864632Seric **		a -- the first argument.
6874632Seric **		b -- the second argument.
6884632Seric **
6894632Seric **	Returns:
69024981Seric **		-1 if a < b
69124981Seric **		 0 if a == b
69224981Seric **		+1 if a > b
6934632Seric **
6944632Seric **	Side Effects:
6954632Seric **		none.
6964632Seric */
6974632Seric 
6984632Seric workcmpf(a, b)
6995037Seric 	register WORK *a;
7005037Seric 	register WORK *b;
7014632Seric {
70257438Seric 	long pa = a->w_pri;
70357438Seric 	long pb = b->w_pri;
70424941Seric 
70524941Seric 	if (pa == pb)
7064632Seric 		return (0);
70724941Seric 	else if (pa > pb)
70824981Seric 		return (1);
70924981Seric 	else
71010121Seric 		return (-1);
7114632Seric }
7124632Seric /*
7134632Seric **  DOWORK -- do a work request.
7144632Seric **
7154632Seric **	Parameters:
7164632Seric **		w -- the work request to be satisfied.
7174632Seric **
7184632Seric **	Returns:
7194632Seric **		none.
7204632Seric **
7214632Seric **	Side Effects:
7224632Seric **		The work request is satisfied if possible.
7234632Seric */
7244632Seric 
72555012Seric dowork(w, e)
7264632Seric 	register WORK *w;
72755012Seric 	register ENVELOPE *e;
7284632Seric {
7294632Seric 	register int i;
73024941Seric 	extern bool shouldqueue();
73151920Seric 	extern bool readqf();
7324632Seric 
7337677Seric 	if (tTd(40, 1))
7345037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7354632Seric 
7364632Seric 	/*
73724941Seric 	**  Ignore jobs that are too expensive for the moment.
7384632Seric 	*/
7394632Seric 
74057438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7414632Seric 	{
74224941Seric 		if (Verbose)
74324967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7444632Seric 		return;
7454632Seric 	}
7464632Seric 
74724941Seric 	/*
74824941Seric 	**  Fork for work.
74924941Seric 	*/
75024941Seric 
75124941Seric 	if (ForkQueueRuns)
75224941Seric 	{
75324941Seric 		i = fork();
75424941Seric 		if (i < 0)
75524941Seric 		{
75624941Seric 			syserr("dowork: cannot fork");
75724941Seric 			return;
75824941Seric 		}
75924941Seric 	}
76024941Seric 	else
76124941Seric 	{
76224941Seric 		i = 0;
76324941Seric 	}
76424941Seric 
7654632Seric 	if (i == 0)
7664632Seric 	{
7674632Seric 		/*
7684632Seric 		**  CHILD
7698148Seric 		**	Lock the control file to avoid duplicate deliveries.
7708148Seric 		**		Then run the file as though we had just read it.
7717350Seric 		**	We save an idea of the temporary name so we
7727350Seric 		**		can recover on interrupt.
7734632Seric 		*/
7744632Seric 
7757763Seric 		/* set basic modes, etc. */
7767356Seric 		(void) alarm(0);
77755012Seric 		clearenvelope(e, FALSE);
778*58737Seric 		e->e_flags |= EF_QUEUERUN;
77958734Seric 		e->e_errormode = EM_MAIL;
78055012Seric 		e->e_id = &w->w_name[2];
7817876Seric # ifdef LOG
78258020Seric 		if (LogLevel > 76)
78355012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7847881Seric 			       getpid());
78556795Seric # endif /* LOG */
7867763Seric 
7877763Seric 		/* don't use the headers from sendmail.cf... */
78855012Seric 		e->e_header = NULL;
7897763Seric 
79051920Seric 		/* read the queue control file -- return if locked */
79155012Seric 		if (!readqf(e))
7926980Seric 		{
79324941Seric 			if (ForkQueueRuns)
79424941Seric 				exit(EX_OK);
79524941Seric 			else
79624941Seric 				return;
7976980Seric 		}
7986980Seric 
79955012Seric 		e->e_flags |= EF_INQUEUE;
800*58737Seric 		eatheader(e);
8016980Seric 
8026980Seric 		/* do the delivery */
80355012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
80455012Seric 			sendall(e, SM_DELIVER);
8056980Seric 
8066980Seric 		/* finish up and exit */
80724941Seric 		if (ForkQueueRuns)
80824941Seric 			finis();
80924941Seric 		else
81055012Seric 			dropenvelope(e);
8114632Seric 	}
81224941Seric 	else
81324941Seric 	{
81424941Seric 		/*
81524941Seric 		**  Parent -- pick up results.
81624941Seric 		*/
8174632Seric 
81824941Seric 		errno = 0;
81924941Seric 		(void) waitfor(i);
82024941Seric 	}
8214632Seric }
8224632Seric /*
8234632Seric **  READQF -- read queue file and set up environment.
8244632Seric **
8254632Seric **	Parameters:
8269377Seric **		e -- the envelope of the job to run.
8274632Seric **
8284632Seric **	Returns:
82951920Seric **		TRUE if it successfully read the queue file.
83051920Seric **		FALSE otherwise.
8314632Seric **
8324632Seric **	Side Effects:
83351920Seric **		The queue file is returned locked.
8344632Seric */
8354632Seric 
83651920Seric bool
83751920Seric readqf(e)
8389377Seric 	register ENVELOPE *e;
8394632Seric {
84017477Seric 	char *qf;
84117477Seric 	register FILE *qfp;
84254974Seric 	ADDRESS *ctladdr;
84356400Seric 	struct stat st;
84457135Seric 	char *bp;
84557135Seric 	char buf[MAXLINE];
8469348Seric 	extern char *fgetfolded();
84724954Seric 	extern long atol();
84854974Seric 	extern ADDRESS *setctluser();
84958689Seric 	extern bool lockfile();
8504632Seric 
8514632Seric 	/*
85217468Seric 	**  Read and process the file.
8534632Seric 	*/
8544632Seric 
85517477Seric 	qf = queuename(e, 'q');
85651937Seric 	qfp = fopen(qf, "r+");
85717477Seric 	if (qfp == NULL)
85817477Seric 	{
85940934Srick 		if (errno != ENOENT)
86040934Srick 			syserr("readqf: no control file %s", qf);
86151920Seric 		return FALSE;
86217477Seric 	}
86340934Srick 
86456400Seric 	/*
86556400Seric 	**  Check the queue file for plausibility to avoid attacks.
86656400Seric 	*/
86756400Seric 
86856400Seric 	if (fstat(fileno(qfp), &st) < 0)
86956400Seric 	{
87056400Seric 		/* must have been being processed by someone else */
87156400Seric 		fclose(qfp);
87256400Seric 		return FALSE;
87356400Seric 	}
87456400Seric 
87557135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
87656400Seric 	{
87756400Seric # ifdef LOG
87856400Seric 		if (LogLevel > 0)
87956400Seric 		{
88056400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
88156400Seric 				e->e_id, st.st_uid, st.st_mode);
88256400Seric 		}
88356795Seric # endif /* LOG */
88456400Seric 		fclose(qfp);
88556400Seric 		return FALSE;
88656400Seric 	}
88756400Seric 
88858689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
88940934Srick 	{
89058689Seric 		/* being processed by another queuer */
89158689Seric 		if (Verbose)
89258689Seric 			printf("%s: locked\n", e->e_id);
89351920Seric # ifdef LOG
89458689Seric 		if (LogLevel > 19)
89558689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
89656795Seric # endif /* LOG */
89740934Srick 		(void) fclose(qfp);
89851920Seric 		return FALSE;
89940934Srick 	}
90040934Srick 
90151920Seric 	/* save this lock */
90251920Seric 	e->e_lockfp = qfp;
90351920Seric 
90440934Srick 	/* do basic system initialization */
90555012Seric 	initsys(e);
90640934Srick 
90717477Seric 	FileName = qf;
9089377Seric 	LineNumber = 0;
90951920Seric 	if (Verbose)
9109377Seric 		printf("\nRunning %s\n", e->e_id);
91154974Seric 	ctladdr = NULL;
91257135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9134632Seric 	{
914*58737Seric 		register char *p;
91557529Seric 		struct stat st;
91657529Seric 
91726504Seric 		if (tTd(40, 4))
91857135Seric 			printf("+++++ %s\n", bp);
91957135Seric 		switch (bp[0])
9204632Seric 		{
92140973Sbostic 		  case 'C':		/* specify controlling user */
92257135Seric 			ctladdr = setctluser(&bp[1]);
92340973Sbostic 			break;
92440973Sbostic 
9254632Seric 		  case 'R':		/* specify recipient */
92658082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9274632Seric 			break;
9284632Seric 
92925687Seric 		  case 'E':		/* specify error recipient */
93058082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
93125687Seric 			break;
93225687Seric 
9334632Seric 		  case 'H':		/* header */
93457135Seric 			(void) chompheader(&bp[1], FALSE, e);
9354632Seric 			break;
9364632Seric 
93710108Seric 		  case 'M':		/* message */
93857135Seric 			e->e_message = newstr(&bp[1]);
93910108Seric 			break;
94010108Seric 
9414632Seric 		  case 'S':		/* sender */
94258704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9434632Seric 			break;
9444632Seric 
9454632Seric 		  case 'D':		/* data file name */
94657135Seric 			e->e_df = newstr(&bp[1]);
9479544Seric 			e->e_dfp = fopen(e->e_df, "r");
9489544Seric 			if (e->e_dfp == NULL)
94958020Seric 			{
9509377Seric 				syserr("readqf: cannot open %s", e->e_df);
95158020Seric 				e->e_msgsize = -1;
95258020Seric 			}
95358020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
95457529Seric 				e->e_msgsize = st.st_size;
9554632Seric 			break;
9564632Seric 
9577860Seric 		  case 'T':		/* init time */
95857135Seric 			e->e_ctime = atol(&bp[1]);
9594632Seric 			break;
9604632Seric 
9614634Seric 		  case 'P':		/* message priority */
96257135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9634634Seric 			break;
9644634Seric 
965*58737Seric 		  case 'F':		/* flag bits */
966*58737Seric 			for (p = &bp[1]; *p != '\0'; p++)
967*58737Seric 			{
968*58737Seric 				switch (*p)
969*58737Seric 				{
970*58737Seric 				  case 'w':	/* warning sent */
971*58737Seric 					e->e_flags |= EF_WARNING;
972*58737Seric 					break;
973*58737Seric 
974*58737Seric 				  case 'r':	/* response */
975*58737Seric 					e->e_flags |= EF_RESPONSE;
976*58737Seric 					break;
977*58737Seric 				}
978*58737Seric 			}
979*58737Seric 			break;
980*58737Seric 
98153400Seric 		  case '$':		/* define macro */
98257135Seric 			define(bp[1], newstr(&bp[2]), e);
98353400Seric 			break;
98453400Seric 
98524941Seric 		  case '\0':		/* blank line; ignore */
98624941Seric 			break;
98724941Seric 
9884632Seric 		  default:
98924941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
99057135Seric 				LineNumber, bp);
9914632Seric 			break;
9924632Seric 		}
99357135Seric 
99457135Seric 		if (bp != buf)
99557135Seric 			free(bp);
9964632Seric 	}
9979377Seric 
9989377Seric 	FileName = NULL;
99924941Seric 
100024941Seric 	/*
100124941Seric 	**  If we haven't read any lines, this queue file is empty.
100224941Seric 	**  Arrange to remove it without referencing any null pointers.
100324941Seric 	*/
100424941Seric 
100524941Seric 	if (LineNumber == 0)
100624941Seric 	{
100724941Seric 		errno = 0;
100824941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
100924941Seric 	}
101051920Seric 	return TRUE;
10114632Seric }
10124632Seric /*
10139630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10149630Seric **
10159630Seric **	Parameters:
10169630Seric **		none.
10179630Seric **
10189630Seric **	Returns:
10199630Seric **		none.
10209630Seric **
10219630Seric **	Side Effects:
10229630Seric **		Prints a listing of the mail queue on the standard output.
10239630Seric */
10245182Seric 
10259630Seric printqueue()
10269630Seric {
10279630Seric 	register WORK *w;
10289630Seric 	FILE *f;
102910070Seric 	int nrequests;
10309630Seric 	char buf[MAXLINE];
10319630Seric 
10329630Seric 	/*
103358250Seric 	**  Check for permission to print the queue
103458250Seric 	*/
103558250Seric 
103658523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
103758250Seric 	{
103858250Seric 		struct stat st;
103958523Seric # ifdef NGROUPS
104058523Seric 		int n;
104158523Seric 		int gidset[NGROUPS];
104258523Seric # endif
104358250Seric 
104458517Seric 		if (stat(QueueDir, &st) < 0)
104558250Seric 		{
104658250Seric 			syserr("Cannot stat %s", QueueDir);
104758250Seric 			return;
104858250Seric 		}
104958523Seric # ifdef NGROUPS
105058523Seric 		n = getgroups(NGROUPS, gidset);
105158523Seric 		while (--n >= 0)
105258523Seric 		{
105358523Seric 			if (gidset[n] == st.st_gid)
105458523Seric 				break;
105558523Seric 		}
105658523Seric 		if (n < 0)
105758523Seric # else
105858250Seric 		if (getgid() != st.st_gid)
105958523Seric # endif
106058250Seric 		{
106158250Seric 			usrerr("510 You are not permitted to see the queue");
106258250Seric 			setstat(EX_NOPERM);
106358250Seric 			return;
106458250Seric 		}
106558250Seric 	}
106658250Seric 
106758250Seric 	/*
10689630Seric 	**  Read and order the queue.
10699630Seric 	*/
10709630Seric 
107124941Seric 	nrequests = orderq(TRUE);
10729630Seric 
10739630Seric 	/*
10749630Seric 	**  Print the work list that we have read.
10759630Seric 	*/
10769630Seric 
10779630Seric 	/* first see if there is anything */
107810070Seric 	if (nrequests <= 0)
10799630Seric 	{
108010070Seric 		printf("Mail queue is empty\n");
10819630Seric 		return;
10829630Seric 	}
10839630Seric 
108451920Seric 	CurrentLA = getla();	/* get load average */
108540934Srick 
108610096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
108725687Seric 	if (nrequests > QUEUESIZE)
108825687Seric 		printf(", only %d printed", QUEUESIZE);
108924979Seric 	if (Verbose)
109058716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
109124979Seric 	else
109258716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10939630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10949630Seric 	{
10959630Seric 		struct stat st;
109610070Seric 		auto time_t submittime = 0;
109710070Seric 		long dfsize = -1;
1098*58737Seric 		int flags = 0;
109910108Seric 		char message[MAXLINE];
110024941Seric 		extern bool shouldqueue();
110158689Seric 		extern bool lockfile();
11029630Seric 
110317468Seric 		f = fopen(w->w_name, "r");
110417468Seric 		if (f == NULL)
110517468Seric 		{
110617468Seric 			errno = 0;
110717468Seric 			continue;
110817468Seric 		}
110958724Seric 		printf("%8s", w->w_name + 2);
111058689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
111110070Seric 			printf("*");
111257438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
111324941Seric 			printf("X");
111410070Seric 		else
111510070Seric 			printf(" ");
111610070Seric 		errno = 0;
111717468Seric 
111810108Seric 		message[0] = '\0';
11199630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11209630Seric 		{
112153400Seric 			register int i;
1122*58737Seric 			register char *p;
112353400Seric 
11249630Seric 			fixcrlf(buf, TRUE);
11259630Seric 			switch (buf[0])
11269630Seric 			{
112710108Seric 			  case 'M':	/* error message */
112853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
1129*58737Seric 					i = sizeof message - 1;
113053400Seric 				bcopy(&buf[1], message, i);
113153400Seric 				message[i] = '\0';
113210108Seric 				break;
113310108Seric 
11349630Seric 			  case 'S':	/* sender name */
113524979Seric 				if (Verbose)
1136*58737Seric 					printf("%8ld %10ld%c%.12s %.38s",
1137*58737Seric 					    dfsize,
1138*58737Seric 					    w->w_pri,
1139*58737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
1140*58737Seric 					    ctime(&submittime) + 4,
114124979Seric 					    &buf[1]);
114224979Seric 				else
114324979Seric 					printf("%8ld %.16s %.45s", dfsize,
114424979Seric 					    ctime(&submittime), &buf[1]);
114510108Seric 				if (message[0] != '\0')
114625027Seric 					printf("\n\t\t (%.60s)", message);
11479630Seric 				break;
114851920Seric 
114940973Sbostic 			  case 'C':	/* controlling user */
115054974Seric 				if (Verbose)
115158716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
115258716Seric 						&buf[1]);
115340973Sbostic 				break;
11549630Seric 
11559630Seric 			  case 'R':	/* recipient name */
115624979Seric 				if (Verbose)
115758716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
115824979Seric 				else
115958716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
11609630Seric 				break;
11619630Seric 
11629630Seric 			  case 'T':	/* creation time */
116324941Seric 				submittime = atol(&buf[1]);
11649630Seric 				break;
116510070Seric 
116610070Seric 			  case 'D':	/* data file name */
116710070Seric 				if (stat(&buf[1], &st) >= 0)
116810070Seric 					dfsize = st.st_size;
116910070Seric 				break;
1170*58737Seric 
1171*58737Seric 			  case 'F':	/* flag bits */
1172*58737Seric 				for (p = &buf[1]; *p != '\0'; p++)
1173*58737Seric 				{
1174*58737Seric 					switch (*p)
1175*58737Seric 					{
1176*58737Seric 					  case 'w':
1177*58737Seric 						flags |= EF_WARNING;
1178*58737Seric 						break;
1179*58737Seric 					}
1180*58737Seric 				}
11819630Seric 			}
11829630Seric 		}
118310070Seric 		if (submittime == (time_t) 0)
118410070Seric 			printf(" (no control file)");
11859630Seric 		printf("\n");
118623098Seric 		(void) fclose(f);
11879630Seric 	}
11889630Seric }
11899630Seric 
119056795Seric # endif /* QUEUE */
119117468Seric /*
119217468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
119317468Seric **
119417468Seric **	Assigns an id code if one does not already exist.
119517468Seric **	This code is very careful to avoid trashing existing files
119617468Seric **	under any circumstances.
119717468Seric **
119817468Seric **	Parameters:
119917468Seric **		e -- envelope to build it in/from.
120017468Seric **		type -- the file type, used as the first character
120117468Seric **			of the file name.
120217468Seric **
120317468Seric **	Returns:
120417468Seric **		a pointer to the new file name (in a static buffer).
120517468Seric **
120617468Seric **	Side Effects:
120751920Seric **		If no id code is already assigned, queuename will
120851920Seric **		assign an id code, create a qf file, and leave a
120951920Seric **		locked, open-for-write file pointer in the envelope.
121017468Seric */
121117468Seric 
121217468Seric char *
121317468Seric queuename(e, type)
121417468Seric 	register ENVELOPE *e;
121517468Seric 	char type;
121617468Seric {
121717468Seric 	static int pid = -1;
121858716Seric 	char c0;
121958680Seric 	static char c1 = 'A';
122058680Seric 	static char c2 = 'A';
122158716Seric 	time_t now;
122258716Seric 	struct tm *tm;
122358689Seric 	static char buf[MAXNAME];
122458689Seric 	extern bool lockfile();
122517468Seric 
122617468Seric 	if (e->e_id == NULL)
122717468Seric 	{
122817468Seric 		char qf[20];
122917468Seric 
123017468Seric 		/* find a unique id */
123117468Seric 		if (pid != getpid())
123217468Seric 		{
123317468Seric 			/* new process -- start back at "AA" */
123417468Seric 			pid = getpid();
123558716Seric 			now = curtime();
123658716Seric 			tm = localtime(&now);
123758716Seric 			c0 = 'A' + tm->tm_hour;
123817468Seric 			c1 = 'A';
123917468Seric 			c2 = 'A' - 1;
124017468Seric 		}
124158716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
124217468Seric 
124317468Seric 		while (c1 < '~' || c2 < 'Z')
124417468Seric 		{
124517468Seric 			int i;
124617468Seric 
124717468Seric 			if (c2 >= 'Z')
124817468Seric 			{
124917468Seric 				c1++;
125017468Seric 				c2 = 'A' - 1;
125117468Seric 			}
125258716Seric 			qf[3] = c1;
125358716Seric 			qf[4] = ++c2;
125417468Seric 			if (tTd(7, 20))
125540934Srick 				printf("queuename: trying \"%s\"\n", qf);
125617468Seric 
125740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
125851920Seric 			if (i < 0)
125951920Seric 			{
126051920Seric 				if (errno == EEXIST)
126151920Seric 					continue;
126251920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
126351920Seric 					qf, QueueDir);
126451920Seric 				exit(EX_UNAVAILABLE);
126551920Seric 			}
126658689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
126751920Seric 			{
126851920Seric 				e->e_lockfp = fdopen(i, "w");
126940934Srick 				break;
127017468Seric 			}
127151920Seric 
127251920Seric 			/* a reader got the file; abandon it and try again */
127351920Seric 			(void) close(i);
127417468Seric 		}
127517468Seric 		if (c1 >= '~' && c2 >= 'Z')
127617468Seric 		{
127717468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
127817468Seric 				qf, QueueDir);
127917468Seric 			exit(EX_OSERR);
128017468Seric 		}
128117468Seric 		e->e_id = newstr(&qf[2]);
128217468Seric 		define('i', e->e_id, e);
128317468Seric 		if (tTd(7, 1))
128417468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
128517468Seric # ifdef LOG
128658020Seric 		if (LogLevel > 93)
128717468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
128856795Seric # endif /* LOG */
128917468Seric 	}
129017468Seric 
129117468Seric 	if (type == '\0')
129217468Seric 		return (NULL);
129317468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
129417468Seric 	if (tTd(7, 2))
129517468Seric 		printf("queuename: %s\n", buf);
129617468Seric 	return (buf);
129717468Seric }
129817468Seric /*
129917468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
130017468Seric **
130117468Seric **	Parameters:
130217468Seric **		e -- the envelope to unlock.
130317468Seric **
130417468Seric **	Returns:
130517468Seric **		none
130617468Seric **
130717468Seric **	Side Effects:
130817468Seric **		unlocks the queue for `e'.
130917468Seric */
131017468Seric 
131117468Seric unlockqueue(e)
131217468Seric 	ENVELOPE *e;
131317468Seric {
131458680Seric 	if (tTd(51, 4))
131558680Seric 		printf("unlockqueue(%s)\n", e->e_id);
131658680Seric 
131751920Seric 	/* if there is a lock file in the envelope, close it */
131851920Seric 	if (e->e_lockfp != NULL)
131958680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
132051920Seric 	e->e_lockfp = NULL;
132151920Seric 
132258728Seric 	/* don't create a queue id if we don't already have one */
132358728Seric 	if (e->e_id == NULL)
132458728Seric 		return;
132558728Seric 
132617468Seric 	/* remove the transcript */
132717468Seric # ifdef LOG
132858020Seric 	if (LogLevel > 87)
132917468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
133056795Seric # endif /* LOG */
133158680Seric 	if (!tTd(51, 104))
133217468Seric 		xunlink(queuename(e, 'x'));
133317468Seric 
133417468Seric }
133540973Sbostic /*
133654974Seric **  SETCTLUSER -- create a controlling address
133740973Sbostic **
133854974Seric **	Create a fake "address" given only a local login name; this is
133954974Seric **	used as a "controlling user" for future recipient addresses.
134040973Sbostic **
134140973Sbostic **	Parameters:
134254974Seric **		user -- the user name of the controlling user.
134340973Sbostic **
134440973Sbostic **	Returns:
134554974Seric **		An address descriptor for the controlling user.
134640973Sbostic **
134740973Sbostic **	Side Effects:
134840973Sbostic **		none.
134940973Sbostic */
135040973Sbostic 
135154974Seric ADDRESS *
135254974Seric setctluser(user)
135354974Seric 	char *user;
135440973Sbostic {
135554974Seric 	register ADDRESS *a;
135640973Sbostic 	struct passwd *pw;
135740973Sbostic 
135840973Sbostic 	/*
135954974Seric 	**  See if this clears our concept of controlling user.
136040973Sbostic 	*/
136140973Sbostic 
136254974Seric 	if (user == NULL || *user == '\0')
136358704Seric 		user = DefUser;
136440973Sbostic 
136540973Sbostic 	/*
136654974Seric 	**  Set up addr fields for controlling user.
136740973Sbostic 	*/
136840973Sbostic 
136954974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
137054974Seric 	bzero((char *) a, sizeof *a);
137154974Seric 	if ((pw = getpwnam(user)) != NULL)
137240973Sbostic 	{
137340973Sbostic 		a->q_home = newstr(pw->pw_dir);
137440973Sbostic 		a->q_uid = pw->pw_uid;
137540973Sbostic 		a->q_gid = pw->pw_gid;
137657642Seric 		a->q_user = newstr(user);
137740973Sbostic 	}
137840973Sbostic 	else
137940973Sbostic 	{
138040973Sbostic 		a->q_uid = DefUid;
138140973Sbostic 		a->q_gid = DefGid;
138257642Seric 		a->q_user = newstr(DefUser);
138340973Sbostic 	}
138440973Sbostic 
138558294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
138656328Seric 	a->q_mailer = LocalMailer;
138754974Seric 	return a;
138840973Sbostic }
1389