xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64335)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362528Sbostic  * Copyright (c) 1988, 1993
462528Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*64335Seric static char sccsid[] = "@(#)queue.c	8.15 (Berkeley) 08/23/93 (with queueing)";
1433731Sbostic #else
15*64335Seric static char sccsid[] = "@(#)queue.c	8.15 (Berkeley) 08/23/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7151920Seric 	char buf[MAXLINE], tf[MAXLINE];
724632Seric 
735037Seric 	/*
7417477Seric 	**  Create control file.
755037Seric 	*/
764632Seric 
7751920Seric 	newid = (e->e_id == NULL);
7864277Seric 
7964277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8051920Seric 	strcpy(tf, queuename(e, 't'));
8151920Seric 	tfp = e->e_lockfp;
8251920Seric 	if (tfp == NULL)
8351920Seric 		newid = FALSE;
8464277Seric 
8564277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
8651920Seric 	if (newid)
8751835Seric 	{
8851920Seric 		tfp = e->e_lockfp;
8951920Seric 	}
9051920Seric 	else
9151920Seric 	{
9251920Seric 		/* get a locked tf file */
9364070Seric 		for (i = 0; i < 128; i++)
9451835Seric 		{
9551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9651920Seric 			if (fd < 0)
9751835Seric 			{
9864070Seric 				if (errno != EEXIST)
9964070Seric 					break;
10064070Seric #ifdef LOG
10164070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10264070Seric 					syslog(LOG_ALERT, "queueup: cannot create %s: %s",
10364070Seric 						tf, errstring(errno));
10464070Seric #endif
10564070Seric 				continue;
10641636Srick 			}
10758689Seric 
108*64335Seric 			if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10951920Seric 				break;
11064070Seric #ifdef LOG
11164070Seric 			else if (LogLevel > 0 && (i % 32) == 0)
11264070Seric 				syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11364070Seric 					tf, errstring(errno));
11464070Seric #endif
11558689Seric 
11651920Seric 			close(fd);
11764070Seric 
11864070Seric 			if ((i % 32) == 31)
11964070Seric 			{
12064070Seric 				/* save the old temp file away */
12164070Seric 				(void) rename(tf, queuename(e, 'T'));
12264070Seric 			}
12364070Seric 			else
12464070Seric 				sleep(i % 32);
12541636Srick 		}
12658801Seric 		if (fd < 0)
12764070Seric 			syserr("!queueup: cannot create temp file %s", tf);
12841636Srick 
12951920Seric 		tfp = fdopen(fd, "w");
13051920Seric 	}
1314632Seric 
1327677Seric 	if (tTd(40, 1))
13364307Seric 		printf("\n>>>>> queueing %s >>>>>\n", e->e_id);
1344632Seric 
1354632Seric 	/*
1366980Seric 	**  If there is no data file yet, create one.
1376980Seric 	*/
1386980Seric 
1396980Seric 	if (e->e_df == NULL)
1406980Seric 	{
1416980Seric 		register FILE *dfp;
1429389Seric 		extern putbody();
1436980Seric 
14464086Seric 		e->e_df = queuename(e, 'd');
14564086Seric 		e->e_df = newstr(e->e_df);
14640934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14740934Srick 		if (fd < 0)
14858690Seric 			syserr("!queueup: cannot create %s", e->e_df);
14940934Srick 		dfp = fdopen(fd, "w");
15059730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
15158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1529389Seric 		e->e_putbody = putbody;
1536980Seric 	}
1546980Seric 
1556980Seric 	/*
1564632Seric 	**  Output future work requests.
15725687Seric 	**	Priority and creation time should be first, since
15825687Seric 	**	they are required by orderq.
1594632Seric 	*/
1604632Seric 
1619377Seric 	/* output message priority */
1629377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1639377Seric 
1649630Seric 	/* output creation time */
1659630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1669630Seric 
16759093Seric 	/* output type and name of data file */
16859093Seric 	if (e->e_bodytype != NULL)
16959093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1707812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1714632Seric 
17210108Seric 	/* message from envelope, if it exists */
17310108Seric 	if (e->e_message != NULL)
17410108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17510108Seric 
17658737Seric 	/* send various flag bits through */
17758737Seric 	p = buf;
17858737Seric 	if (bitset(EF_WARNING, e->e_flags))
17958737Seric 		*p++ = 'w';
18058737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
18158737Seric 		*p++ = 'r';
18258737Seric 	*p++ = '\0';
18358737Seric 	if (buf[0] != '\0')
18458737Seric 		fprintf(tfp, "F%s\n", buf);
18558737Seric 
18658957Seric 	/* $r and $s and $_ macro values */
18753400Seric 	if ((p = macvalue('r', e)) != NULL)
18853400Seric 		fprintf(tfp, "$r%s\n", p);
18953400Seric 	if ((p = macvalue('s', e)) != NULL)
19053400Seric 		fprintf(tfp, "$s%s\n", p);
19158957Seric 	if ((p = macvalue('_', e)) != NULL)
19258957Seric 		fprintf(tfp, "$_%s\n", p);
19353400Seric 
1944632Seric 	/* output name of sender */
1957812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1964632Seric 
19755360Seric 	/* output list of error recipients */
19859670Seric 	printctladdr(NULL, NULL);
19955360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20055360Seric 	{
20158680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
20255360Seric 		{
20359113Seric 			printctladdr(q, tfp);
20455360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
20555360Seric 		}
20655360Seric 	}
20755360Seric 
2084632Seric 	/* output list of recipient addresses */
2096980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2104632Seric 	{
21158250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21258680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2138245Seric 		{
21459113Seric 			printctladdr(q, tfp);
2157812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2169377Seric 			if (announce)
2179377Seric 			{
2189377Seric 				e->e_to = q->q_paddr;
21958151Seric 				message("queued");
22058020Seric 				if (LogLevel > 8)
22158337Seric 					logdelivery(NULL, NULL, "queued", e);
2229377Seric 				e->e_to = NULL;
2239377Seric 			}
2249387Seric 			if (tTd(40, 1))
2259387Seric 			{
2269387Seric 				printf("queueing ");
2279387Seric 				printaddr(q, FALSE);
2289387Seric 			}
2298245Seric 		}
2304632Seric 	}
2314632Seric 
2329377Seric 	/*
2339377Seric 	**  Output headers for this message.
2349377Seric 	**	Expand macros completely here.  Queue run will deal with
2359377Seric 	**	everything as absolute headers.
2369377Seric 	**		All headers that must be relative to the recipient
2379377Seric 	**		can be cracked later.
23810173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23910173Seric 	**	no effect on the addresses as they are output.
2409377Seric 	*/
2419377Seric 
24210686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24358020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24458020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24510349Seric 	nullmailer.m_eol = "\n";
24610173Seric 
24758050Seric 	define('g', "\201f", e);
2486980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2494632Seric 	{
25010686Seric 		extern bool bitzerop();
25110686Seric 
25212015Seric 		/* don't output null headers */
2534632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2544632Seric 			continue;
25512015Seric 
25612015Seric 		/* don't output resent headers on non-resent messages */
25712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25812015Seric 			continue;
25912015Seric 
26012015Seric 		/* output this header */
2617812Seric 		fprintf(tfp, "H");
26212015Seric 
26312015Seric 		/* if conditional, output the set of conditions */
26410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26510686Seric 		{
26610686Seric 			int j;
26710686Seric 
26823098Seric 			(void) putc('?', tfp);
26910686Seric 			for (j = '\0'; j <= '\177'; j++)
27010686Seric 				if (bitnset(j, h->h_mflags))
27123098Seric 					(void) putc(j, tfp);
27223098Seric 			(void) putc('?', tfp);
27310686Seric 		}
27412015Seric 
27512015Seric 		/* output the header: expand macros, convert addresses */
2767763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2777763Seric 		{
2787763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2798236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2807763Seric 		}
2818245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2829348Seric 		{
28358737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28463753Seric 			FILE *savetrace = TrafficLogFile;
28558737Seric 
28663753Seric 			TrafficLogFile = NULL;
28763753Seric 
28858737Seric 			if (bitset(H_FROM, h->h_flags))
28958737Seric 				oldstyle = FALSE;
29058737Seric 
29158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29255012Seric 				 &nullmailer, e);
29363753Seric 
29463753Seric 			TrafficLogFile = savetrace;
2959348Seric 		}
2967763Seric 		else
2978245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2984632Seric 	}
2994632Seric 
3004632Seric 	/*
3014632Seric 	**  Clean up.
3024632Seric 	*/
3034632Seric 
30458732Seric 	fflush(tfp);
30560603Seric 	fsync(fileno(tfp));
30658732Seric 	if (ferror(tfp))
30758732Seric 	{
30858732Seric 		if (newid)
30958732Seric 			syserr("!552 Error writing control file %s", tf);
31058732Seric 		else
31158732Seric 			syserr("!452 Error writing control file %s", tf);
31258732Seric 	}
31358732Seric 
31451920Seric 	if (!newid)
31551920Seric 	{
31664277Seric 		/* rename (locked) tf to be (locked) qf */
31751920Seric 		qf = queuename(e, 'q');
31851920Seric 		if (rename(tf, qf) < 0)
31951920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
32064277Seric 
32164277Seric 		/* close and unlock old (locked) qf */
32251920Seric 		if (e->e_lockfp != NULL)
32358680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
32451920Seric 		e->e_lockfp = tfp;
32551920Seric 	}
32651920Seric 	else
32751920Seric 		qf = tf;
32841636Srick 	errno = 0;
3297391Seric 
3307677Seric # ifdef LOG
3317677Seric 	/* save log info */
33258020Seric 	if (LogLevel > 79)
3337878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33456795Seric # endif /* LOG */
33564307Seric 
33664307Seric 	if (tTd(40, 1))
33764307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
33851920Seric 	return;
3394632Seric }
34054974Seric 
34154974Seric printctladdr(a, tfp)
34259113Seric 	register ADDRESS *a;
34354974Seric 	FILE *tfp;
34454974Seric {
34559113Seric 	char *uname;
34659113Seric 	register struct passwd *pw;
34759113Seric 	register ADDRESS *q;
34859113Seric 	uid_t uid;
34959113Seric 	static ADDRESS *lastctladdr;
35059113Seric 	static uid_t lastuid;
35154974Seric 
35259113Seric 	/* initialization */
35363850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
35454974Seric 	{
35559670Seric 		if (lastctladdr != NULL && tfp != NULL)
35659113Seric 			fprintf(tfp, "C\n");
35759113Seric 		lastctladdr = NULL;
35859113Seric 		lastuid = 0;
35954974Seric 		return;
36054974Seric 	}
36159113Seric 
36259113Seric 	/* find the active uid */
36359113Seric 	q = getctladdr(a);
36459113Seric 	if (q == NULL)
36559113Seric 		uid = 0;
36654974Seric 	else
36759113Seric 		uid = q->q_uid;
36863850Seric 	a = a->q_alias;
36959113Seric 
37059113Seric 	/* check to see if this is the same as last time */
37159113Seric 	if (lastctladdr != NULL && uid == lastuid &&
37259113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
37359113Seric 		return;
37459113Seric 	lastuid = uid;
37559113Seric 	lastctladdr = a;
37659113Seric 
37759113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
37859270Seric 		uname = "";
37959113Seric 	else
38059113Seric 		uname = pw->pw_name;
38159113Seric 
38259113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
38354974Seric }
38454974Seric 
3854632Seric /*
3864632Seric **  RUNQUEUE -- run the jobs in the queue.
3874632Seric **
3884632Seric **	Gets the stuff out of the queue in some presumably logical
3894632Seric **	order and processes them.
3904632Seric **
3914632Seric **	Parameters:
39224941Seric **		forkflag -- TRUE if the queue scanning should be done in
39324941Seric **			a child process.  We double-fork so it is not our
39424941Seric **			child and we don't have to clean up after it.
3954632Seric **
3964632Seric **	Returns:
3974632Seric **		none.
3984632Seric **
3994632Seric **	Side Effects:
4004632Seric **		runs things in the mail queue.
4014632Seric */
4024632Seric 
40355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
40455360Seric 
40555360Seric runqueue(forkflag)
4064639Seric 	bool forkflag;
4074632Seric {
40855360Seric 	register ENVELOPE *e;
40955360Seric 	extern ENVELOPE BlankEnvelope;
41024953Seric 
4117466Seric 	/*
41224953Seric 	**  If no work will ever be selected, don't even bother reading
41324953Seric 	**  the queue.
41424953Seric 	*/
41524953Seric 
41651920Seric 	CurrentLA = getla();	/* get load average */
41740934Srick 
41858132Seric 	if (shouldqueue(0L, curtime()))
41924953Seric 	{
42024953Seric 		if (Verbose)
42124953Seric 			printf("Skipping queue run -- load average too high\n");
42258107Seric 		if (forkflag && QueueIntvl != 0)
42358107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
42455360Seric 		return;
42524953Seric 	}
42624953Seric 
42724953Seric 	/*
4287466Seric 	**  See if we want to go off and do other useful work.
4297466Seric 	*/
4304639Seric 
4314639Seric 	if (forkflag)
4324639Seric 	{
4337943Seric 		int pid;
4347943Seric 
4357943Seric 		pid = dofork();
4367943Seric 		if (pid != 0)
4374639Seric 		{
43846928Sbostic 			extern void reapchild();
43925184Seric 
4407943Seric 			/* parent -- pick up intermediate zombie */
44125184Seric #ifndef SIGCHLD
4429377Seric 			(void) waitfor(pid);
44356795Seric #else /* SIGCHLD */
44464035Seric 			(void) setsignal(SIGCHLD, reapchild);
44556795Seric #endif /* SIGCHLD */
4467690Seric 			if (QueueIntvl != 0)
4479348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4484639Seric 			return;
4494639Seric 		}
4507943Seric 		/* child -- double fork */
45125184Seric #ifndef SIGCHLD
4527943Seric 		if (fork() != 0)
4537943Seric 			exit(EX_OK);
45456795Seric #else /* SIGCHLD */
45564035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
45656795Seric #endif /* SIGCHLD */
4574639Seric 	}
45824941Seric 
45940934Srick 	setproctitle("running queue: %s", QueueDir);
46024941Seric 
4617876Seric # ifdef LOG
46258020Seric 	if (LogLevel > 69)
46355360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
46455360Seric 			QueueDir, getpid(), forkflag);
46556795Seric # endif /* LOG */
4664639Seric 
4677466Seric 	/*
46810205Seric 	**  Release any resources used by the daemon code.
46910205Seric 	*/
47010205Seric 
47110205Seric # ifdef DAEMON
47210205Seric 	clrdaemon();
47356795Seric # endif /* DAEMON */
47410205Seric 
47510205Seric 	/*
47655360Seric 	**  Create ourselves an envelope
47755360Seric 	*/
47855360Seric 
47955360Seric 	CurEnv = &QueueEnvelope;
48058179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
48155360Seric 	e->e_flags = BlankEnvelope.e_flags;
48255360Seric 
48355360Seric 	/*
48427175Seric 	**  Make sure the alias database is open.
48527175Seric 	*/
48627175Seric 
48760537Seric 	initmaps(FALSE, e);
48827175Seric 
48927175Seric 	/*
4907466Seric 	**  Start making passes through the queue.
4917466Seric 	**	First, read and sort the entire queue.
4927466Seric 	**	Then, process the work in that order.
4937466Seric 	**		But if you take too long, start over.
4947466Seric 	*/
4957466Seric 
4967943Seric 	/* order the existing work requests */
49724954Seric 	(void) orderq(FALSE);
4987690Seric 
4997943Seric 	/* process them once at a time */
5007943Seric 	while (WorkQ != NULL)
5014639Seric 	{
5027943Seric 		WORK *w = WorkQ;
5037881Seric 
5047943Seric 		WorkQ = WorkQ->w_next;
50558884Seric 
50658884Seric 		/*
50758884Seric 		**  Ignore jobs that are too expensive for the moment.
50858884Seric 		*/
50958884Seric 
51058884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
51158884Seric 		{
51258884Seric 			if (Verbose)
51358884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
51458884Seric 		}
51558930Seric 		else
51658930Seric 		{
51764296Seric 			pid_t pid;
51864296Seric 			extern pid_t dowork();
51964296Seric 
52064296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
52164296Seric 			errno = 0;
52264296Seric 			(void) waitfor(pid);
52358930Seric 		}
5247943Seric 		free(w->w_name);
5257943Seric 		free((char *) w);
5264639Seric 	}
52729866Seric 
52829866Seric 	/* exit without the usual cleanup */
52955467Seric 	e->e_id = NULL;
53055467Seric 	finis();
5314634Seric }
5324634Seric /*
5334632Seric **  ORDERQ -- order the work queue.
5344632Seric **
5354632Seric **	Parameters:
53624941Seric **		doall -- if set, include everything in the queue (even
53724941Seric **			the jobs that cannot be run because the load
53824941Seric **			average is too high).  Otherwise, exclude those
53924941Seric **			jobs.
5404632Seric **
5414632Seric **	Returns:
54210121Seric **		The number of request in the queue (not necessarily
54310121Seric **		the number of requests in WorkQ however).
5444632Seric **
5454632Seric **	Side Effects:
5464632Seric **		Sets WorkQ to the queue of available work, in order.
5474632Seric */
5484632Seric 
54925687Seric # define NEED_P		001
55025687Seric # define NEED_T		002
55158318Seric # define NEED_R		004
55258318Seric # define NEED_S		010
5534632Seric 
55424941Seric orderq(doall)
55524941Seric 	bool doall;
5564632Seric {
55760219Seric 	register struct dirent *d;
5584632Seric 	register WORK *w;
5596625Sglickman 	DIR *f;
5604632Seric 	register int i;
56125687Seric 	WORK wlist[QUEUESIZE+1];
56210070Seric 	int wn = -1;
5634632Seric 	extern workcmpf();
5644632Seric 
56558318Seric 	if (tTd(41, 1))
56658318Seric 	{
56758318Seric 		printf("orderq:\n");
56858318Seric 		if (QueueLimitId != NULL)
56958318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
57058318Seric 		if (QueueLimitSender != NULL)
57158318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
57258318Seric 		if (QueueLimitRecipient != NULL)
57358318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
57458318Seric 	}
57558318Seric 
5764632Seric 	/* clear out old WorkQ */
5774632Seric 	for (w = WorkQ; w != NULL; )
5784632Seric 	{
5794632Seric 		register WORK *nw = w->w_next;
5804632Seric 
5814632Seric 		WorkQ = nw;
5824632Seric 		free(w->w_name);
5834632Seric 		free((char *) w);
5844632Seric 		w = nw;
5854632Seric 	}
5864632Seric 
5874632Seric 	/* open the queue directory */
5888148Seric 	f = opendir(".");
5894632Seric 	if (f == NULL)
5904632Seric 	{
5918148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
59210070Seric 		return (0);
5934632Seric 	}
5944632Seric 
5954632Seric 	/*
5964632Seric 	**  Read the work directory.
5974632Seric 	*/
5984632Seric 
59910070Seric 	while ((d = readdir(f)) != NULL)
6004632Seric 	{
6019377Seric 		FILE *cf;
6024632Seric 		char lbuf[MAXNAME];
60358318Seric 		extern bool strcontainedin();
6044632Seric 
6054632Seric 		/* is this an interesting entry? */
6067812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6074632Seric 			continue;
6084632Seric 
60958318Seric 		if (QueueLimitId != NULL &&
61058318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
61158318Seric 			continue;
61258318Seric 
61358722Seric 		/*
61458722Seric 		**  Check queue name for plausibility.  This handles
61558722Seric 		**  both old and new type ids.
61658722Seric 		*/
61758722Seric 
61858722Seric 		i = strlen(d->d_name);
61958722Seric 		if (i != 9 && i != 10)
62058020Seric 		{
62158020Seric 			if (Verbose)
62258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
62358020Seric #ifdef LOG
62458020Seric 			if (LogLevel > 3)
62559615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
62658020Seric 					d->d_name);
62758020Seric #endif
62858020Seric 			if (strlen(d->d_name) >= MAXNAME)
62958020Seric 				d->d_name[MAXNAME - 1] = '\0';
63058020Seric 			strcpy(lbuf, d->d_name);
63158020Seric 			lbuf[0] = 'Q';
63258020Seric 			(void) rename(d->d_name, lbuf);
63358020Seric 			continue;
63458020Seric 		}
63558020Seric 
63610070Seric 		/* yes -- open control file (if not too many files) */
63725687Seric 		if (++wn >= QUEUESIZE)
63810070Seric 			continue;
63958318Seric 
6408148Seric 		cf = fopen(d->d_name, "r");
6414632Seric 		if (cf == NULL)
6424632Seric 		{
6437055Seric 			/* this may be some random person sending hir msgs */
6447055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
64510090Seric 			if (tTd(41, 2))
64610090Seric 				printf("orderq: cannot open %s (%d)\n",
64710090Seric 					d->d_name, errno);
6487055Seric 			errno = 0;
64910090Seric 			wn--;
6504632Seric 			continue;
6514632Seric 		}
65225687Seric 		w = &wlist[wn];
65325687Seric 		w->w_name = newstr(d->d_name);
6544632Seric 
65525027Seric 		/* make sure jobs in creation don't clog queue */
65625687Seric 		w->w_pri = 0x7fffffff;
65725687Seric 		w->w_ctime = 0;
65825027Seric 
6594632Seric 		/* extract useful information */
66025687Seric 		i = NEED_P | NEED_T;
66158318Seric 		if (QueueLimitSender != NULL)
66258318Seric 			i |= NEED_S;
66358318Seric 		if (QueueLimitRecipient != NULL)
66458318Seric 			i |= NEED_R;
66525687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6664632Seric 		{
66724954Seric 			extern long atol();
66858318Seric 			extern bool strcontainedin();
66924954Seric 
67024941Seric 			switch (lbuf[0])
6714632Seric 			{
67224941Seric 			  case 'P':
67325687Seric 				w->w_pri = atol(&lbuf[1]);
67425687Seric 				i &= ~NEED_P;
6754632Seric 				break;
67625013Seric 
67725013Seric 			  case 'T':
67825687Seric 				w->w_ctime = atol(&lbuf[1]);
67925687Seric 				i &= ~NEED_T;
68025013Seric 				break;
68158318Seric 
68258318Seric 			  case 'R':
68358318Seric 				if (QueueLimitRecipient != NULL &&
68458318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
68558318Seric 					i &= ~NEED_R;
68658318Seric 				break;
68758318Seric 
68858318Seric 			  case 'S':
68958318Seric 				if (QueueLimitSender != NULL &&
69058318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
69158318Seric 					i &= ~NEED_S;
69258318Seric 				break;
6934632Seric 			}
6944632Seric 		}
6954632Seric 		(void) fclose(cf);
69624953Seric 
69758318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
69858318Seric 		    bitset(NEED_R|NEED_S, i))
69924953Seric 		{
70024953Seric 			/* don't even bother sorting this job in */
70124953Seric 			wn--;
70224953Seric 		}
7034632Seric 	}
7046625Sglickman 	(void) closedir(f);
70510090Seric 	wn++;
7064632Seric 
7074632Seric 	/*
7084632Seric 	**  Sort the work directory.
7094632Seric 	*/
7104632Seric 
71125687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7124632Seric 
7134632Seric 	/*
7144632Seric 	**  Convert the work list into canonical form.
7159377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7164632Seric 	*/
7174632Seric 
71824981Seric 	WorkQ = NULL;
71925687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7204632Seric 	{
7214632Seric 		w = (WORK *) xalloc(sizeof *w);
7224632Seric 		w->w_name = wlist[i].w_name;
7234632Seric 		w->w_pri = wlist[i].w_pri;
72425013Seric 		w->w_ctime = wlist[i].w_ctime;
72524981Seric 		w->w_next = WorkQ;
72624981Seric 		WorkQ = w;
7274632Seric 	}
7284632Seric 
7297677Seric 	if (tTd(40, 1))
7304632Seric 	{
7314632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7325037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7334632Seric 	}
73410070Seric 
73510090Seric 	return (wn);
7364632Seric }
7374632Seric /*
7387677Seric **  WORKCMPF -- compare function for ordering work.
7394632Seric **
7404632Seric **	Parameters:
7414632Seric **		a -- the first argument.
7424632Seric **		b -- the second argument.
7434632Seric **
7444632Seric **	Returns:
74524981Seric **		-1 if a < b
74624981Seric **		 0 if a == b
74724981Seric **		+1 if a > b
7484632Seric **
7494632Seric **	Side Effects:
7504632Seric **		none.
7514632Seric */
7524632Seric 
7534632Seric workcmpf(a, b)
7545037Seric 	register WORK *a;
7555037Seric 	register WORK *b;
7564632Seric {
75757438Seric 	long pa = a->w_pri;
75857438Seric 	long pb = b->w_pri;
75924941Seric 
76024941Seric 	if (pa == pb)
7614632Seric 		return (0);
76224941Seric 	else if (pa > pb)
76324981Seric 		return (1);
76424981Seric 	else
76510121Seric 		return (-1);
7664632Seric }
7674632Seric /*
7684632Seric **  DOWORK -- do a work request.
7694632Seric **
7704632Seric **	Parameters:
77158884Seric **		id -- the ID of the job to run.
77258884Seric **		forkflag -- if set, run this in background.
77358924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
77458924Seric **			This is used when expanding aliases in the queue.
77561094Seric **			If forkflag is also set, it doesn't wait for the
77661094Seric **			child.
77758884Seric **		e - the envelope in which to run it.
7784632Seric **
7794632Seric **	Returns:
78064296Seric **		process id of process that is running the queue job.
7814632Seric **
7824632Seric **	Side Effects:
7834632Seric **		The work request is satisfied if possible.
7844632Seric */
7854632Seric 
78664296Seric pid_t
78758924Seric dowork(id, forkflag, requeueflag, e)
78858884Seric 	char *id;
78958884Seric 	bool forkflag;
79058924Seric 	bool requeueflag;
79155012Seric 	register ENVELOPE *e;
7924632Seric {
79364296Seric 	register pid_t pid;
79451920Seric 	extern bool readqf();
7954632Seric 
7967677Seric 	if (tTd(40, 1))
79758884Seric 		printf("dowork(%s)\n", id);
7984632Seric 
7994632Seric 	/*
80024941Seric 	**  Fork for work.
80124941Seric 	*/
80224941Seric 
80358884Seric 	if (forkflag)
80424941Seric 	{
80564296Seric 		pid = fork();
80664296Seric 		if (pid < 0)
80724941Seric 		{
80824941Seric 			syserr("dowork: cannot fork");
80964296Seric 			return 0;
81024941Seric 		}
81124941Seric 	}
81224941Seric 	else
81324941Seric 	{
81464296Seric 		pid = 0;
81524941Seric 	}
81624941Seric 
81764296Seric 	if (pid == 0)
8184632Seric 	{
8194632Seric 		/*
8204632Seric 		**  CHILD
8218148Seric 		**	Lock the control file to avoid duplicate deliveries.
8228148Seric 		**		Then run the file as though we had just read it.
8237350Seric 		**	We save an idea of the temporary name so we
8247350Seric 		**		can recover on interrupt.
8254632Seric 		*/
8264632Seric 
8277763Seric 		/* set basic modes, etc. */
8287356Seric 		(void) alarm(0);
82955012Seric 		clearenvelope(e, FALSE);
83058737Seric 		e->e_flags |= EF_QUEUERUN;
83158734Seric 		e->e_errormode = EM_MAIL;
83258884Seric 		e->e_id = id;
83363846Seric 		if (forkflag)
83464296Seric 			disconnect(1, e);
8357876Seric # ifdef LOG
83658020Seric 		if (LogLevel > 76)
83755012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8387881Seric 			       getpid());
83956795Seric # endif /* LOG */
8407763Seric 
8417763Seric 		/* don't use the headers from sendmail.cf... */
84255012Seric 		e->e_header = NULL;
8437763Seric 
84451920Seric 		/* read the queue control file -- return if locked */
84563850Seric 		if (!readqf(e, !requeueflag))
8466980Seric 		{
84758884Seric 			if (tTd(40, 4))
84858884Seric 				printf("readqf(%s) failed\n", e->e_id);
84958884Seric 			if (forkflag)
85024941Seric 				exit(EX_OK);
85124941Seric 			else
85224941Seric 				return;
8536980Seric 		}
8546980Seric 
85555012Seric 		e->e_flags |= EF_INQUEUE;
85658929Seric 		eatheader(e, requeueflag);
8576980Seric 
85858924Seric 		if (requeueflag)
85958924Seric 			queueup(e, TRUE, FALSE);
86058924Seric 
8616980Seric 		/* do the delivery */
86258915Seric 		sendall(e, SM_DELIVER);
8636980Seric 
8646980Seric 		/* finish up and exit */
86558884Seric 		if (forkflag)
86624941Seric 			finis();
86724941Seric 		else
86855012Seric 			dropenvelope(e);
8694632Seric 	}
87064296Seric 	e->e_id = NULL;
87164296Seric 	return pid;
8724632Seric }
8734632Seric /*
8744632Seric **  READQF -- read queue file and set up environment.
8754632Seric **
8764632Seric **	Parameters:
8779377Seric **		e -- the envelope of the job to run.
87863850Seric **		announcefile -- if set, announce the name of the queue
87963850Seric **			file in error messages.
8804632Seric **
8814632Seric **	Returns:
88251920Seric **		TRUE if it successfully read the queue file.
88351920Seric **		FALSE otherwise.
8844632Seric **
8854632Seric **	Side Effects:
88651920Seric **		The queue file is returned locked.
8874632Seric */
8884632Seric 
88951920Seric bool
89063850Seric readqf(e, announcefile)
8919377Seric 	register ENVELOPE *e;
89263850Seric 	bool announcefile;
8934632Seric {
89417477Seric 	register FILE *qfp;
89554974Seric 	ADDRESS *ctladdr;
89656400Seric 	struct stat st;
89757135Seric 	char *bp;
89858915Seric 	char qf[20];
89957135Seric 	char buf[MAXLINE];
90024954Seric 	extern long atol();
90154974Seric 	extern ADDRESS *setctluser();
9024632Seric 
9034632Seric 	/*
90417468Seric 	**  Read and process the file.
9054632Seric 	*/
9064632Seric 
90758915Seric 	strcpy(qf, queuename(e, 'q'));
90851937Seric 	qfp = fopen(qf, "r+");
90917477Seric 	if (qfp == NULL)
91017477Seric 	{
91158884Seric 		if (tTd(40, 8))
91258884Seric 			printf("readqf(%s): fopen failure (%s)\n",
91358884Seric 				qf, errstring(errno));
91440934Srick 		if (errno != ENOENT)
91540934Srick 			syserr("readqf: no control file %s", qf);
91651920Seric 		return FALSE;
91717477Seric 	}
91840934Srick 
919*64335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
92064277Seric 	{
92164277Seric 		/* being processed by another queuer */
92264277Seric 		if (tTd(40, 8))
92364277Seric 			printf("readqf(%s): locked\n", qf);
92464277Seric 		if (Verbose)
92564277Seric 			printf("%s: locked\n", e->e_id);
92664277Seric # ifdef LOG
92764277Seric 		if (LogLevel > 19)
92864277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
92964277Seric # endif /* LOG */
93064277Seric 		(void) fclose(qfp);
93164277Seric 		return FALSE;
93264277Seric 	}
93364277Seric 
93456400Seric 	/*
93556400Seric 	**  Check the queue file for plausibility to avoid attacks.
93656400Seric 	*/
93756400Seric 
93856400Seric 	if (fstat(fileno(qfp), &st) < 0)
93956400Seric 	{
94056400Seric 		/* must have been being processed by someone else */
94158884Seric 		if (tTd(40, 8))
94258884Seric 			printf("readqf(%s): fstat failure (%s)\n",
94358884Seric 				qf, errstring(errno));
94456400Seric 		fclose(qfp);
94556400Seric 		return FALSE;
94656400Seric 	}
94756400Seric 
94864137Seric 	if (st.st_uid != geteuid())
94956400Seric 	{
95056400Seric # ifdef LOG
95156400Seric 		if (LogLevel > 0)
95256400Seric 		{
95356400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
95456400Seric 				e->e_id, st.st_uid, st.st_mode);
95556400Seric 		}
95656795Seric # endif /* LOG */
95758884Seric 		if (tTd(40, 8))
95858884Seric 			printf("readqf(%s): bogus file\n", qf);
95964277Seric 		rename(qf, queuename(e, 'Q'));
96056400Seric 		fclose(qfp);
96156400Seric 		return FALSE;
96256400Seric 	}
96356400Seric 
96464277Seric 	if (st.st_size == 0)
96540934Srick 	{
96664277Seric 		/* must be a bogus file -- just remove it */
96764277Seric 		(void) unlink(qf);
96864277Seric 		fclose(qfp);
96951920Seric 		return FALSE;
97040934Srick 	}
97140934Srick 
97264277Seric 	if (st.st_nlink == 0)
97359101Seric 	{
97464277Seric 		/*
97564277Seric 		**  Race condition -- we got a file just as it was being
97664277Seric 		**  unlinked.  Just assume it is zero length.
97764277Seric 		*/
97864277Seric 
97959101Seric 		fclose(qfp);
98059101Seric 		return FALSE;
98159101Seric 	}
98259101Seric 
98364277Seric 	/* good file -- save this lock */
98451920Seric 	e->e_lockfp = qfp;
98551920Seric 
98640934Srick 	/* do basic system initialization */
98755012Seric 	initsys(e);
98840934Srick 
98963850Seric 	if (announcefile)
99063850Seric 		FileName = qf;
9919377Seric 	LineNumber = 0;
99263850Seric 	e->e_flags |= EF_GLOBALERRS;
99363850Seric 	OpMode = MD_DELIVER;
99451920Seric 	if (Verbose)
9959377Seric 		printf("\nRunning %s\n", e->e_id);
99654974Seric 	ctladdr = NULL;
99757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9984632Seric 	{
99958737Seric 		register char *p;
100057529Seric 		struct stat st;
100157529Seric 
100226504Seric 		if (tTd(40, 4))
100357135Seric 			printf("+++++ %s\n", bp);
100457135Seric 		switch (bp[0])
10054632Seric 		{
100640973Sbostic 		  case 'C':		/* specify controlling user */
100757135Seric 			ctladdr = setctluser(&bp[1]);
100840973Sbostic 			break;
100940973Sbostic 
10104632Seric 		  case 'R':		/* specify recipient */
101158082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10124632Seric 			break;
10134632Seric 
101425687Seric 		  case 'E':		/* specify error recipient */
101558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
101625687Seric 			break;
101725687Seric 
10184632Seric 		  case 'H':		/* header */
101957135Seric 			(void) chompheader(&bp[1], FALSE, e);
10204632Seric 			break;
10214632Seric 
102210108Seric 		  case 'M':		/* message */
102357135Seric 			e->e_message = newstr(&bp[1]);
102410108Seric 			break;
102510108Seric 
10264632Seric 		  case 'S':		/* sender */
102758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10284632Seric 			break;
10294632Seric 
103059093Seric 		  case 'B':		/* body type */
103159093Seric 			e->e_bodytype = newstr(&bp[1]);
103259093Seric 			break;
103359093Seric 
10344632Seric 		  case 'D':		/* data file name */
103557135Seric 			e->e_df = newstr(&bp[1]);
10369544Seric 			e->e_dfp = fopen(e->e_df, "r");
10379544Seric 			if (e->e_dfp == NULL)
103858020Seric 			{
10399377Seric 				syserr("readqf: cannot open %s", e->e_df);
104058020Seric 				e->e_msgsize = -1;
104158020Seric 			}
104258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
104357529Seric 				e->e_msgsize = st.st_size;
10444632Seric 			break;
10454632Seric 
10467860Seric 		  case 'T':		/* init time */
104757135Seric 			e->e_ctime = atol(&bp[1]);
10484632Seric 			break;
10494632Seric 
10504634Seric 		  case 'P':		/* message priority */
105157135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10524634Seric 			break;
10534634Seric 
105458737Seric 		  case 'F':		/* flag bits */
105558737Seric 			for (p = &bp[1]; *p != '\0'; p++)
105658737Seric 			{
105758737Seric 				switch (*p)
105858737Seric 				{
105958737Seric 				  case 'w':	/* warning sent */
106058737Seric 					e->e_flags |= EF_WARNING;
106158737Seric 					break;
106258737Seric 
106358737Seric 				  case 'r':	/* response */
106458737Seric 					e->e_flags |= EF_RESPONSE;
106558737Seric 					break;
106658737Seric 				}
106758737Seric 			}
106858737Seric 			break;
106958737Seric 
107053400Seric 		  case '$':		/* define macro */
107157135Seric 			define(bp[1], newstr(&bp[2]), e);
107253400Seric 			break;
107353400Seric 
107424941Seric 		  case '\0':		/* blank line; ignore */
107524941Seric 			break;
107624941Seric 
10774632Seric 		  default:
107859700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
107957135Seric 				LineNumber, bp);
108063753Seric 			fclose(qfp);
108163753Seric 			rename(qf, queuename(e, 'Q'));
108263753Seric 			return FALSE;
10834632Seric 		}
108457135Seric 
108557135Seric 		if (bp != buf)
108657135Seric 			free(bp);
10874632Seric 	}
10889377Seric 
10899377Seric 	FileName = NULL;
109024941Seric 
109124941Seric 	/*
109224941Seric 	**  If we haven't read any lines, this queue file is empty.
109324941Seric 	**  Arrange to remove it without referencing any null pointers.
109424941Seric 	*/
109524941Seric 
109624941Seric 	if (LineNumber == 0)
109724941Seric 	{
109824941Seric 		errno = 0;
109924941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
110024941Seric 	}
110151920Seric 	return TRUE;
11024632Seric }
11034632Seric /*
11049630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11059630Seric **
11069630Seric **	Parameters:
11079630Seric **		none.
11089630Seric **
11099630Seric **	Returns:
11109630Seric **		none.
11119630Seric **
11129630Seric **	Side Effects:
11139630Seric **		Prints a listing of the mail queue on the standard output.
11149630Seric */
11155182Seric 
11169630Seric printqueue()
11179630Seric {
11189630Seric 	register WORK *w;
11199630Seric 	FILE *f;
112010070Seric 	int nrequests;
11219630Seric 	char buf[MAXLINE];
11229630Seric 
11239630Seric 	/*
112458250Seric 	**  Check for permission to print the queue
112558250Seric 	*/
112658250Seric 
112764333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
112858250Seric 	{
112958250Seric 		struct stat st;
113058523Seric # ifdef NGROUPS
113158523Seric 		int n;
113263937Seric 		GIDSET_T gidset[NGROUPS];
113358523Seric # endif
113458250Seric 
113558517Seric 		if (stat(QueueDir, &st) < 0)
113658250Seric 		{
113758250Seric 			syserr("Cannot stat %s", QueueDir);
113858250Seric 			return;
113958250Seric 		}
114058523Seric # ifdef NGROUPS
114158523Seric 		n = getgroups(NGROUPS, gidset);
114258523Seric 		while (--n >= 0)
114358523Seric 		{
114458523Seric 			if (gidset[n] == st.st_gid)
114558523Seric 				break;
114658523Seric 		}
114758523Seric 		if (n < 0)
114858523Seric # else
114963787Seric 		if (RealGid != st.st_gid)
115058523Seric # endif
115158250Seric 		{
115258250Seric 			usrerr("510 You are not permitted to see the queue");
115358250Seric 			setstat(EX_NOPERM);
115458250Seric 			return;
115558250Seric 		}
115658250Seric 	}
115758250Seric 
115858250Seric 	/*
11599630Seric 	**  Read and order the queue.
11609630Seric 	*/
11619630Seric 
116224941Seric 	nrequests = orderq(TRUE);
11639630Seric 
11649630Seric 	/*
11659630Seric 	**  Print the work list that we have read.
11669630Seric 	*/
11679630Seric 
11689630Seric 	/* first see if there is anything */
116910070Seric 	if (nrequests <= 0)
11709630Seric 	{
117110070Seric 		printf("Mail queue is empty\n");
11729630Seric 		return;
11739630Seric 	}
11749630Seric 
117551920Seric 	CurrentLA = getla();	/* get load average */
117640934Srick 
117710096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
117825687Seric 	if (nrequests > QUEUESIZE)
117925687Seric 		printf(", only %d printed", QUEUESIZE);
118024979Seric 	if (Verbose)
118158716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
118224979Seric 	else
118358716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11849630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11859630Seric 	{
11869630Seric 		struct stat st;
118710070Seric 		auto time_t submittime = 0;
118810070Seric 		long dfsize = -1;
118958737Seric 		int flags = 0;
119010108Seric 		char message[MAXLINE];
119159093Seric 		char bodytype[MAXNAME];
11929630Seric 
119317468Seric 		f = fopen(w->w_name, "r");
119417468Seric 		if (f == NULL)
119517468Seric 		{
119617468Seric 			errno = 0;
119717468Seric 			continue;
119817468Seric 		}
119958724Seric 		printf("%8s", w->w_name + 2);
1200*64335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
120110070Seric 			printf("*");
120257438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
120324941Seric 			printf("X");
120410070Seric 		else
120510070Seric 			printf(" ");
120610070Seric 		errno = 0;
120717468Seric 
120859093Seric 		message[0] = bodytype[0] = '\0';
12099630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12109630Seric 		{
121153400Seric 			register int i;
121258737Seric 			register char *p;
121353400Seric 
12149630Seric 			fixcrlf(buf, TRUE);
12159630Seric 			switch (buf[0])
12169630Seric 			{
121710108Seric 			  case 'M':	/* error message */
121853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
121958737Seric 					i = sizeof message - 1;
122053400Seric 				bcopy(&buf[1], message, i);
122153400Seric 				message[i] = '\0';
122210108Seric 				break;
122310108Seric 
122459093Seric 			  case 'B':	/* body type */
122559093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
122659093Seric 					i = sizeof bodytype - 1;
122759093Seric 				bcopy(&buf[1], bodytype, i);
122859093Seric 				bodytype[i] = '\0';
122959093Seric 				break;
123059093Seric 
12319630Seric 			  case 'S':	/* sender name */
123224979Seric 				if (Verbose)
123358737Seric 					printf("%8ld %10ld%c%.12s %.38s",
123458737Seric 					    dfsize,
123558737Seric 					    w->w_pri,
123658737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
123758737Seric 					    ctime(&submittime) + 4,
123824979Seric 					    &buf[1]);
123924979Seric 				else
124024979Seric 					printf("%8ld %.16s %.45s", dfsize,
124124979Seric 					    ctime(&submittime), &buf[1]);
124259093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
124359093Seric 				{
124459093Seric 					printf("\n    %10.10s", bodytype);
124559093Seric 					if (message[0] != '\0')
124659093Seric 						printf("   (%.60s)", message);
124759093Seric 				}
12489630Seric 				break;
124951920Seric 
125040973Sbostic 			  case 'C':	/* controlling user */
125154974Seric 				if (Verbose)
125258716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
125358716Seric 						&buf[1]);
125440973Sbostic 				break;
12559630Seric 
12569630Seric 			  case 'R':	/* recipient name */
125724979Seric 				if (Verbose)
125858716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
125924979Seric 				else
126058716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12619630Seric 				break;
12629630Seric 
12639630Seric 			  case 'T':	/* creation time */
126424941Seric 				submittime = atol(&buf[1]);
12659630Seric 				break;
126610070Seric 
126710070Seric 			  case 'D':	/* data file name */
126810070Seric 				if (stat(&buf[1], &st) >= 0)
126910070Seric 					dfsize = st.st_size;
127010070Seric 				break;
127158737Seric 
127258737Seric 			  case 'F':	/* flag bits */
127358737Seric 				for (p = &buf[1]; *p != '\0'; p++)
127458737Seric 				{
127558737Seric 					switch (*p)
127658737Seric 					{
127758737Seric 					  case 'w':
127858737Seric 						flags |= EF_WARNING;
127958737Seric 						break;
128058737Seric 					}
128158737Seric 				}
12829630Seric 			}
12839630Seric 		}
128410070Seric 		if (submittime == (time_t) 0)
128510070Seric 			printf(" (no control file)");
12869630Seric 		printf("\n");
128723098Seric 		(void) fclose(f);
12889630Seric 	}
12899630Seric }
12909630Seric 
129156795Seric # endif /* QUEUE */
129217468Seric /*
129317468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
129417468Seric **
129517468Seric **	Assigns an id code if one does not already exist.
129617468Seric **	This code is very careful to avoid trashing existing files
129717468Seric **	under any circumstances.
129817468Seric **
129917468Seric **	Parameters:
130017468Seric **		e -- envelope to build it in/from.
130117468Seric **		type -- the file type, used as the first character
130217468Seric **			of the file name.
130317468Seric **
130417468Seric **	Returns:
130517468Seric **		a pointer to the new file name (in a static buffer).
130617468Seric **
130717468Seric **	Side Effects:
130851920Seric **		If no id code is already assigned, queuename will
130951920Seric **		assign an id code, create a qf file, and leave a
131051920Seric **		locked, open-for-write file pointer in the envelope.
131117468Seric */
131217468Seric 
131317468Seric char *
131417468Seric queuename(e, type)
131517468Seric 	register ENVELOPE *e;
131659700Seric 	int type;
131717468Seric {
131817468Seric 	static int pid = -1;
131958741Seric 	static char c0;
132058741Seric 	static char c1;
132158741Seric 	static char c2;
132258716Seric 	time_t now;
132358716Seric 	struct tm *tm;
132458689Seric 	static char buf[MAXNAME];
132517468Seric 
132617468Seric 	if (e->e_id == NULL)
132717468Seric 	{
132817468Seric 		char qf[20];
132917468Seric 
133017468Seric 		/* find a unique id */
133117468Seric 		if (pid != getpid())
133217468Seric 		{
133317468Seric 			/* new process -- start back at "AA" */
133417468Seric 			pid = getpid();
133558716Seric 			now = curtime();
133658716Seric 			tm = localtime(&now);
133758716Seric 			c0 = 'A' + tm->tm_hour;
133817468Seric 			c1 = 'A';
133917468Seric 			c2 = 'A' - 1;
134017468Seric 		}
134158716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
134217468Seric 
134317468Seric 		while (c1 < '~' || c2 < 'Z')
134417468Seric 		{
134517468Seric 			int i;
134617468Seric 
134717468Seric 			if (c2 >= 'Z')
134817468Seric 			{
134917468Seric 				c1++;
135017468Seric 				c2 = 'A' - 1;
135117468Seric 			}
135258716Seric 			qf[3] = c1;
135358716Seric 			qf[4] = ++c2;
135417468Seric 			if (tTd(7, 20))
135540934Srick 				printf("queuename: trying \"%s\"\n", qf);
135617468Seric 
135740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
135851920Seric 			if (i < 0)
135951920Seric 			{
136051920Seric 				if (errno == EEXIST)
136151920Seric 					continue;
136251920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
136351920Seric 					qf, QueueDir);
136451920Seric 				exit(EX_UNAVAILABLE);
136551920Seric 			}
1366*64335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
136751920Seric 			{
136851920Seric 				e->e_lockfp = fdopen(i, "w");
136940934Srick 				break;
137017468Seric 			}
137151920Seric 
137251920Seric 			/* a reader got the file; abandon it and try again */
137351920Seric 			(void) close(i);
137417468Seric 		}
137517468Seric 		if (c1 >= '~' && c2 >= 'Z')
137617468Seric 		{
137717468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
137817468Seric 				qf, QueueDir);
137917468Seric 			exit(EX_OSERR);
138017468Seric 		}
138117468Seric 		e->e_id = newstr(&qf[2]);
138217468Seric 		define('i', e->e_id, e);
138317468Seric 		if (tTd(7, 1))
138417468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
138517468Seric # ifdef LOG
138658020Seric 		if (LogLevel > 93)
138717468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
138856795Seric # endif /* LOG */
138917468Seric 	}
139017468Seric 
139117468Seric 	if (type == '\0')
139217468Seric 		return (NULL);
139317468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
139417468Seric 	if (tTd(7, 2))
139517468Seric 		printf("queuename: %s\n", buf);
139617468Seric 	return (buf);
139717468Seric }
139817468Seric /*
139917468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
140017468Seric **
140117468Seric **	Parameters:
140217468Seric **		e -- the envelope to unlock.
140317468Seric **
140417468Seric **	Returns:
140517468Seric **		none
140617468Seric **
140717468Seric **	Side Effects:
140817468Seric **		unlocks the queue for `e'.
140917468Seric */
141017468Seric 
141117468Seric unlockqueue(e)
141217468Seric 	ENVELOPE *e;
141317468Seric {
141458680Seric 	if (tTd(51, 4))
141558680Seric 		printf("unlockqueue(%s)\n", e->e_id);
141658680Seric 
141751920Seric 	/* if there is a lock file in the envelope, close it */
141851920Seric 	if (e->e_lockfp != NULL)
141958680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
142051920Seric 	e->e_lockfp = NULL;
142151920Seric 
142258728Seric 	/* don't create a queue id if we don't already have one */
142358728Seric 	if (e->e_id == NULL)
142458728Seric 		return;
142558728Seric 
142617468Seric 	/* remove the transcript */
142717468Seric # ifdef LOG
142858020Seric 	if (LogLevel > 87)
142917468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
143056795Seric # endif /* LOG */
143158680Seric 	if (!tTd(51, 104))
143217468Seric 		xunlink(queuename(e, 'x'));
143317468Seric 
143417468Seric }
143540973Sbostic /*
143654974Seric **  SETCTLUSER -- create a controlling address
143740973Sbostic **
143854974Seric **	Create a fake "address" given only a local login name; this is
143954974Seric **	used as a "controlling user" for future recipient addresses.
144040973Sbostic **
144140973Sbostic **	Parameters:
144254974Seric **		user -- the user name of the controlling user.
144340973Sbostic **
144440973Sbostic **	Returns:
144554974Seric **		An address descriptor for the controlling user.
144640973Sbostic **
144740973Sbostic **	Side Effects:
144840973Sbostic **		none.
144940973Sbostic */
145040973Sbostic 
145154974Seric ADDRESS *
145254974Seric setctluser(user)
145354974Seric 	char *user;
145440973Sbostic {
145554974Seric 	register ADDRESS *a;
145640973Sbostic 	struct passwd *pw;
145759113Seric 	char *p;
145840973Sbostic 
145940973Sbostic 	/*
146054974Seric 	**  See if this clears our concept of controlling user.
146140973Sbostic 	*/
146240973Sbostic 
146363850Seric 	if (user == NULL || *user == '\0')
146463850Seric 		return NULL;
146540973Sbostic 
146640973Sbostic 	/*
146754974Seric 	**  Set up addr fields for controlling user.
146840973Sbostic 	*/
146940973Sbostic 
147054974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
147154974Seric 	bzero((char *) a, sizeof *a);
147259113Seric 
147359113Seric 	p = strchr(user, ':');
147459113Seric 	if (p != NULL)
147559113Seric 		*p++ = '\0';
147659270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
147740973Sbostic 	{
147840973Sbostic 		a->q_home = newstr(pw->pw_dir);
147940973Sbostic 		a->q_uid = pw->pw_uid;
148040973Sbostic 		a->q_gid = pw->pw_gid;
148157642Seric 		a->q_user = newstr(user);
148259270Seric 		a->q_flags |= QGOODUID;
148340973Sbostic 	}
148440973Sbostic 	else
148540973Sbostic 	{
148657642Seric 		a->q_user = newstr(DefUser);
148740973Sbostic 	}
148840973Sbostic 
148959270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
149056328Seric 	a->q_mailer = LocalMailer;
149159113Seric 	if (p == NULL)
149259113Seric 		a->q_paddr = a->q_user;
149359113Seric 	else
149459113Seric 		a->q_paddr = newstr(p);
149554974Seric 	return a;
149640973Sbostic }
1497