xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64035)
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*64035Seric static char sccsid[] = "@(#)queue.c	8.7 (Berkeley) 07/26/93 (with queueing)";
1433731Sbostic #else
15*64035Seric static char sccsid[] = "@(#)queue.c	8.7 (Berkeley) 07/26/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);
7851920Seric 	strcpy(tf, queuename(e, 't'));
7951920Seric 	tfp = e->e_lockfp;
8051920Seric 	if (tfp == NULL)
8151920Seric 		newid = FALSE;
8251920Seric 	if (newid)
8351835Seric 	{
8451920Seric 		tfp = e->e_lockfp;
8551920Seric 	}
8651920Seric 	else
8751920Seric 	{
8851920Seric 		/* get a locked tf file */
8951920Seric 		for (i = 100; --i >= 0; )
9051835Seric 		{
9151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9251920Seric 			if (fd < 0)
9351835Seric 			{
9451920Seric 				if (errno == EEXIST)
9551920Seric 					continue;
9658801Seric notemp:
9758690Seric 				syserr("!queueup: cannot create temp file %s", tf);
9841636Srick 			}
9958689Seric 
10058689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10151920Seric 				break;
10258689Seric 
10351920Seric 			close(fd);
10458801Seric 			sleep(i);
10541636Srick 		}
10658801Seric 		if (fd < 0)
10758801Seric 			goto notemp;
10841636Srick 
10951920Seric 		tfp = fdopen(fd, "w");
11051920Seric 	}
1114632Seric 
1127677Seric 	if (tTd(40, 1))
11317468Seric 		printf("queueing %s\n", e->e_id);
1144632Seric 
1154632Seric 	/*
1166980Seric 	**  If there is no data file yet, create one.
1176980Seric 	*/
1186980Seric 
1196980Seric 	if (e->e_df == NULL)
1206980Seric 	{
1216980Seric 		register FILE *dfp;
1229389Seric 		extern putbody();
1236980Seric 
1247812Seric 		e->e_df = newstr(queuename(e, 'd'));
12540934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
12640934Srick 		if (fd < 0)
12758690Seric 			syserr("!queueup: cannot create %s", e->e_df);
12840934Srick 		dfp = fdopen(fd, "w");
12959730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
13058680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1319389Seric 		e->e_putbody = putbody;
1326980Seric 	}
1336980Seric 
1346980Seric 	/*
1354632Seric 	**  Output future work requests.
13625687Seric 	**	Priority and creation time should be first, since
13725687Seric 	**	they are required by orderq.
1384632Seric 	*/
1394632Seric 
1409377Seric 	/* output message priority */
1419377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1429377Seric 
1439630Seric 	/* output creation time */
1449630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1459630Seric 
14659093Seric 	/* output type and name of data file */
14759093Seric 	if (e->e_bodytype != NULL)
14859093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1497812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1504632Seric 
15110108Seric 	/* message from envelope, if it exists */
15210108Seric 	if (e->e_message != NULL)
15310108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15410108Seric 
15558737Seric 	/* send various flag bits through */
15658737Seric 	p = buf;
15758737Seric 	if (bitset(EF_WARNING, e->e_flags))
15858737Seric 		*p++ = 'w';
15958737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16058737Seric 		*p++ = 'r';
16158737Seric 	*p++ = '\0';
16258737Seric 	if (buf[0] != '\0')
16358737Seric 		fprintf(tfp, "F%s\n", buf);
16458737Seric 
16558957Seric 	/* $r and $s and $_ macro values */
16653400Seric 	if ((p = macvalue('r', e)) != NULL)
16753400Seric 		fprintf(tfp, "$r%s\n", p);
16853400Seric 	if ((p = macvalue('s', e)) != NULL)
16953400Seric 		fprintf(tfp, "$s%s\n", p);
17058957Seric 	if ((p = macvalue('_', e)) != NULL)
17158957Seric 		fprintf(tfp, "$_%s\n", p);
17253400Seric 
1734632Seric 	/* output name of sender */
1747812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1754632Seric 
17655360Seric 	/* output list of error recipients */
17759670Seric 	printctladdr(NULL, NULL);
17855360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
17955360Seric 	{
18058680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18155360Seric 		{
18259113Seric 			printctladdr(q, tfp);
18355360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18455360Seric 		}
18555360Seric 	}
18655360Seric 
1874632Seric 	/* output list of recipient addresses */
1886980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1894632Seric 	{
19058250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19158680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1928245Seric 		{
19359113Seric 			printctladdr(q, tfp);
1947812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1959377Seric 			if (announce)
1969377Seric 			{
1979377Seric 				e->e_to = q->q_paddr;
19858151Seric 				message("queued");
19958020Seric 				if (LogLevel > 8)
20058337Seric 					logdelivery(NULL, NULL, "queued", e);
2019377Seric 				e->e_to = NULL;
2029377Seric 			}
2039387Seric 			if (tTd(40, 1))
2049387Seric 			{
2059387Seric 				printf("queueing ");
2069387Seric 				printaddr(q, FALSE);
2079387Seric 			}
2088245Seric 		}
2094632Seric 	}
2104632Seric 
2119377Seric 	/*
2129377Seric 	**  Output headers for this message.
2139377Seric 	**	Expand macros completely here.  Queue run will deal with
2149377Seric 	**	everything as absolute headers.
2159377Seric 	**		All headers that must be relative to the recipient
2169377Seric 	**		can be cracked later.
21710173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
21810173Seric 	**	no effect on the addresses as they are output.
2199377Seric 	*/
2209377Seric 
22110686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22258020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
22358020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
22410349Seric 	nullmailer.m_eol = "\n";
22510173Seric 
22658050Seric 	define('g', "\201f", e);
2276980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2284632Seric 	{
22910686Seric 		extern bool bitzerop();
23010686Seric 
23112015Seric 		/* don't output null headers */
2324632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2334632Seric 			continue;
23412015Seric 
23512015Seric 		/* don't output resent headers on non-resent messages */
23612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
23712015Seric 			continue;
23812015Seric 
23912015Seric 		/* output this header */
2407812Seric 		fprintf(tfp, "H");
24112015Seric 
24212015Seric 		/* if conditional, output the set of conditions */
24310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
24410686Seric 		{
24510686Seric 			int j;
24610686Seric 
24723098Seric 			(void) putc('?', tfp);
24810686Seric 			for (j = '\0'; j <= '\177'; j++)
24910686Seric 				if (bitnset(j, h->h_mflags))
25023098Seric 					(void) putc(j, tfp);
25123098Seric 			(void) putc('?', tfp);
25210686Seric 		}
25312015Seric 
25412015Seric 		/* output the header: expand macros, convert addresses */
2557763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2567763Seric 		{
2577763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2588236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2597763Seric 		}
2608245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2619348Seric 		{
26258737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
26363753Seric 			FILE *savetrace = TrafficLogFile;
26458737Seric 
26563753Seric 			TrafficLogFile = NULL;
26663753Seric 
26758737Seric 			if (bitset(H_FROM, h->h_flags))
26858737Seric 				oldstyle = FALSE;
26958737Seric 
27058737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27155012Seric 				 &nullmailer, e);
27263753Seric 
27363753Seric 			TrafficLogFile = savetrace;
2749348Seric 		}
2757763Seric 		else
2768245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2774632Seric 	}
2784632Seric 
2794632Seric 	/*
2804632Seric 	**  Clean up.
2814632Seric 	*/
2824632Seric 
28358732Seric 	fflush(tfp);
28460603Seric 	fsync(fileno(tfp));
28558732Seric 	if (ferror(tfp))
28658732Seric 	{
28758732Seric 		if (newid)
28858732Seric 			syserr("!552 Error writing control file %s", tf);
28958732Seric 		else
29058732Seric 			syserr("!452 Error writing control file %s", tf);
29158732Seric 	}
29258732Seric 
29351920Seric 	if (!newid)
29451920Seric 	{
29551920Seric 		qf = queuename(e, 'q');
29651920Seric 		if (rename(tf, qf) < 0)
29751920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29851920Seric 		if (e->e_lockfp != NULL)
29958680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
30051920Seric 		e->e_lockfp = tfp;
30151920Seric 	}
30251920Seric 	else
30351920Seric 		qf = tf;
30441636Srick 	errno = 0;
3057391Seric 
3067677Seric # ifdef LOG
3077677Seric 	/* save log info */
30858020Seric 	if (LogLevel > 79)
3097878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
31056795Seric # endif /* LOG */
31151920Seric 	return;
3124632Seric }
31354974Seric 
31454974Seric printctladdr(a, tfp)
31559113Seric 	register ADDRESS *a;
31654974Seric 	FILE *tfp;
31754974Seric {
31859113Seric 	char *uname;
31959113Seric 	register struct passwd *pw;
32059113Seric 	register ADDRESS *q;
32159113Seric 	uid_t uid;
32259113Seric 	static ADDRESS *lastctladdr;
32359113Seric 	static uid_t lastuid;
32454974Seric 
32559113Seric 	/* initialization */
32663850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
32754974Seric 	{
32859670Seric 		if (lastctladdr != NULL && tfp != NULL)
32959113Seric 			fprintf(tfp, "C\n");
33059113Seric 		lastctladdr = NULL;
33159113Seric 		lastuid = 0;
33254974Seric 		return;
33354974Seric 	}
33459113Seric 
33559113Seric 	/* find the active uid */
33659113Seric 	q = getctladdr(a);
33759113Seric 	if (q == NULL)
33859113Seric 		uid = 0;
33954974Seric 	else
34059113Seric 		uid = q->q_uid;
34163850Seric 	a = a->q_alias;
34259113Seric 
34359113Seric 	/* check to see if this is the same as last time */
34459113Seric 	if (lastctladdr != NULL && uid == lastuid &&
34559113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
34659113Seric 		return;
34759113Seric 	lastuid = uid;
34859113Seric 	lastctladdr = a;
34959113Seric 
35059113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35159270Seric 		uname = "";
35259113Seric 	else
35359113Seric 		uname = pw->pw_name;
35459113Seric 
35559113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
35654974Seric }
35754974Seric 
3584632Seric /*
3594632Seric **  RUNQUEUE -- run the jobs in the queue.
3604632Seric **
3614632Seric **	Gets the stuff out of the queue in some presumably logical
3624632Seric **	order and processes them.
3634632Seric **
3644632Seric **	Parameters:
36524941Seric **		forkflag -- TRUE if the queue scanning should be done in
36624941Seric **			a child process.  We double-fork so it is not our
36724941Seric **			child and we don't have to clean up after it.
3684632Seric **
3694632Seric **	Returns:
3704632Seric **		none.
3714632Seric **
3724632Seric **	Side Effects:
3734632Seric **		runs things in the mail queue.
3744632Seric */
3754632Seric 
37655360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37755360Seric 
37855360Seric runqueue(forkflag)
3794639Seric 	bool forkflag;
3804632Seric {
38155360Seric 	register ENVELOPE *e;
38255360Seric 	extern ENVELOPE BlankEnvelope;
38324953Seric 
3847466Seric 	/*
38524953Seric 	**  If no work will ever be selected, don't even bother reading
38624953Seric 	**  the queue.
38724953Seric 	*/
38824953Seric 
38951920Seric 	CurrentLA = getla();	/* get load average */
39040934Srick 
39158132Seric 	if (shouldqueue(0L, curtime()))
39224953Seric 	{
39324953Seric 		if (Verbose)
39424953Seric 			printf("Skipping queue run -- load average too high\n");
39558107Seric 		if (forkflag && QueueIntvl != 0)
39658107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39755360Seric 		return;
39824953Seric 	}
39924953Seric 
40024953Seric 	/*
4017466Seric 	**  See if we want to go off and do other useful work.
4027466Seric 	*/
4034639Seric 
4044639Seric 	if (forkflag)
4054639Seric 	{
4067943Seric 		int pid;
4077943Seric 
4087943Seric 		pid = dofork();
4097943Seric 		if (pid != 0)
4104639Seric 		{
41146928Sbostic 			extern void reapchild();
41225184Seric 
4137943Seric 			/* parent -- pick up intermediate zombie */
41425184Seric #ifndef SIGCHLD
4159377Seric 			(void) waitfor(pid);
41656795Seric #else /* SIGCHLD */
417*64035Seric 			(void) setsignal(SIGCHLD, reapchild);
41856795Seric #endif /* SIGCHLD */
4197690Seric 			if (QueueIntvl != 0)
4209348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4214639Seric 			return;
4224639Seric 		}
4237943Seric 		/* child -- double fork */
42425184Seric #ifndef SIGCHLD
4257943Seric 		if (fork() != 0)
4267943Seric 			exit(EX_OK);
42756795Seric #else /* SIGCHLD */
428*64035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
42956795Seric #endif /* SIGCHLD */
4304639Seric 	}
43124941Seric 
43240934Srick 	setproctitle("running queue: %s", QueueDir);
43324941Seric 
4347876Seric # ifdef LOG
43558020Seric 	if (LogLevel > 69)
43655360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43755360Seric 			QueueDir, getpid(), forkflag);
43856795Seric # endif /* LOG */
4394639Seric 
4407466Seric 	/*
44110205Seric 	**  Release any resources used by the daemon code.
44210205Seric 	*/
44310205Seric 
44410205Seric # ifdef DAEMON
44510205Seric 	clrdaemon();
44656795Seric # endif /* DAEMON */
44710205Seric 
44810205Seric 	/*
44955360Seric 	**  Create ourselves an envelope
45055360Seric 	*/
45155360Seric 
45255360Seric 	CurEnv = &QueueEnvelope;
45358179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45455360Seric 	e->e_flags = BlankEnvelope.e_flags;
45555360Seric 
45655360Seric 	/*
45727175Seric 	**  Make sure the alias database is open.
45827175Seric 	*/
45927175Seric 
46060537Seric 	initmaps(FALSE, e);
46127175Seric 
46227175Seric 	/*
4637466Seric 	**  Start making passes through the queue.
4647466Seric 	**	First, read and sort the entire queue.
4657466Seric 	**	Then, process the work in that order.
4667466Seric 	**		But if you take too long, start over.
4677466Seric 	*/
4687466Seric 
4697943Seric 	/* order the existing work requests */
47024954Seric 	(void) orderq(FALSE);
4717690Seric 
4727943Seric 	/* process them once at a time */
4737943Seric 	while (WorkQ != NULL)
4744639Seric 	{
4757943Seric 		WORK *w = WorkQ;
4767881Seric 
4777943Seric 		WorkQ = WorkQ->w_next;
47858884Seric 
47958884Seric 		/*
48058884Seric 		**  Ignore jobs that are too expensive for the moment.
48158884Seric 		*/
48258884Seric 
48358884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
48458884Seric 		{
48558884Seric 			if (Verbose)
48658884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
48758884Seric 		}
48858930Seric 		else
48958930Seric 		{
49058930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
49158930Seric 		}
4927943Seric 		free(w->w_name);
4937943Seric 		free((char *) w);
4944639Seric 	}
49529866Seric 
49629866Seric 	/* exit without the usual cleanup */
49755467Seric 	e->e_id = NULL;
49855467Seric 	finis();
4994634Seric }
5004634Seric /*
5014632Seric **  ORDERQ -- order the work queue.
5024632Seric **
5034632Seric **	Parameters:
50424941Seric **		doall -- if set, include everything in the queue (even
50524941Seric **			the jobs that cannot be run because the load
50624941Seric **			average is too high).  Otherwise, exclude those
50724941Seric **			jobs.
5084632Seric **
5094632Seric **	Returns:
51010121Seric **		The number of request in the queue (not necessarily
51110121Seric **		the number of requests in WorkQ however).
5124632Seric **
5134632Seric **	Side Effects:
5144632Seric **		Sets WorkQ to the queue of available work, in order.
5154632Seric */
5164632Seric 
51725687Seric # define NEED_P		001
51825687Seric # define NEED_T		002
51958318Seric # define NEED_R		004
52058318Seric # define NEED_S		010
5214632Seric 
52224941Seric orderq(doall)
52324941Seric 	bool doall;
5244632Seric {
52560219Seric 	register struct dirent *d;
5264632Seric 	register WORK *w;
5276625Sglickman 	DIR *f;
5284632Seric 	register int i;
52925687Seric 	WORK wlist[QUEUESIZE+1];
53010070Seric 	int wn = -1;
5314632Seric 	extern workcmpf();
5324632Seric 
53358318Seric 	if (tTd(41, 1))
53458318Seric 	{
53558318Seric 		printf("orderq:\n");
53658318Seric 		if (QueueLimitId != NULL)
53758318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
53858318Seric 		if (QueueLimitSender != NULL)
53958318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
54058318Seric 		if (QueueLimitRecipient != NULL)
54158318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
54258318Seric 	}
54358318Seric 
5444632Seric 	/* clear out old WorkQ */
5454632Seric 	for (w = WorkQ; w != NULL; )
5464632Seric 	{
5474632Seric 		register WORK *nw = w->w_next;
5484632Seric 
5494632Seric 		WorkQ = nw;
5504632Seric 		free(w->w_name);
5514632Seric 		free((char *) w);
5524632Seric 		w = nw;
5534632Seric 	}
5544632Seric 
5554632Seric 	/* open the queue directory */
5568148Seric 	f = opendir(".");
5574632Seric 	if (f == NULL)
5584632Seric 	{
5598148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
56010070Seric 		return (0);
5614632Seric 	}
5624632Seric 
5634632Seric 	/*
5644632Seric 	**  Read the work directory.
5654632Seric 	*/
5664632Seric 
56710070Seric 	while ((d = readdir(f)) != NULL)
5684632Seric 	{
5699377Seric 		FILE *cf;
5704632Seric 		char lbuf[MAXNAME];
57158318Seric 		extern bool strcontainedin();
5724632Seric 
5734632Seric 		/* is this an interesting entry? */
5747812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5754632Seric 			continue;
5764632Seric 
57758318Seric 		if (QueueLimitId != NULL &&
57858318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
57958318Seric 			continue;
58058318Seric 
58158722Seric 		/*
58258722Seric 		**  Check queue name for plausibility.  This handles
58358722Seric 		**  both old and new type ids.
58458722Seric 		*/
58558722Seric 
58658722Seric 		i = strlen(d->d_name);
58758722Seric 		if (i != 9 && i != 10)
58858020Seric 		{
58958020Seric 			if (Verbose)
59058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
59158020Seric #ifdef LOG
59258020Seric 			if (LogLevel > 3)
59359615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
59458020Seric 					d->d_name);
59558020Seric #endif
59658020Seric 			if (strlen(d->d_name) >= MAXNAME)
59758020Seric 				d->d_name[MAXNAME - 1] = '\0';
59858020Seric 			strcpy(lbuf, d->d_name);
59958020Seric 			lbuf[0] = 'Q';
60058020Seric 			(void) rename(d->d_name, lbuf);
60158020Seric 			continue;
60258020Seric 		}
60358020Seric 
60410070Seric 		/* yes -- open control file (if not too many files) */
60525687Seric 		if (++wn >= QUEUESIZE)
60610070Seric 			continue;
60758318Seric 
6088148Seric 		cf = fopen(d->d_name, "r");
6094632Seric 		if (cf == NULL)
6104632Seric 		{
6117055Seric 			/* this may be some random person sending hir msgs */
6127055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
61310090Seric 			if (tTd(41, 2))
61410090Seric 				printf("orderq: cannot open %s (%d)\n",
61510090Seric 					d->d_name, errno);
6167055Seric 			errno = 0;
61710090Seric 			wn--;
6184632Seric 			continue;
6194632Seric 		}
62025687Seric 		w = &wlist[wn];
62125687Seric 		w->w_name = newstr(d->d_name);
6224632Seric 
62325027Seric 		/* make sure jobs in creation don't clog queue */
62425687Seric 		w->w_pri = 0x7fffffff;
62525687Seric 		w->w_ctime = 0;
62625027Seric 
6274632Seric 		/* extract useful information */
62825687Seric 		i = NEED_P | NEED_T;
62958318Seric 		if (QueueLimitSender != NULL)
63058318Seric 			i |= NEED_S;
63158318Seric 		if (QueueLimitRecipient != NULL)
63258318Seric 			i |= NEED_R;
63325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6344632Seric 		{
63524954Seric 			extern long atol();
63658318Seric 			extern bool strcontainedin();
63724954Seric 
63824941Seric 			switch (lbuf[0])
6394632Seric 			{
64024941Seric 			  case 'P':
64125687Seric 				w->w_pri = atol(&lbuf[1]);
64225687Seric 				i &= ~NEED_P;
6434632Seric 				break;
64425013Seric 
64525013Seric 			  case 'T':
64625687Seric 				w->w_ctime = atol(&lbuf[1]);
64725687Seric 				i &= ~NEED_T;
64825013Seric 				break;
64958318Seric 
65058318Seric 			  case 'R':
65158318Seric 				if (QueueLimitRecipient != NULL &&
65258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
65358318Seric 					i &= ~NEED_R;
65458318Seric 				break;
65558318Seric 
65658318Seric 			  case 'S':
65758318Seric 				if (QueueLimitSender != NULL &&
65858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
65958318Seric 					i &= ~NEED_S;
66058318Seric 				break;
6614632Seric 			}
6624632Seric 		}
6634632Seric 		(void) fclose(cf);
66424953Seric 
66558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
66658318Seric 		    bitset(NEED_R|NEED_S, i))
66724953Seric 		{
66824953Seric 			/* don't even bother sorting this job in */
66924953Seric 			wn--;
67024953Seric 		}
6714632Seric 	}
6726625Sglickman 	(void) closedir(f);
67310090Seric 	wn++;
6744632Seric 
6754632Seric 	/*
6764632Seric 	**  Sort the work directory.
6774632Seric 	*/
6784632Seric 
67925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6804632Seric 
6814632Seric 	/*
6824632Seric 	**  Convert the work list into canonical form.
6839377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6844632Seric 	*/
6854632Seric 
68624981Seric 	WorkQ = NULL;
68725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6884632Seric 	{
6894632Seric 		w = (WORK *) xalloc(sizeof *w);
6904632Seric 		w->w_name = wlist[i].w_name;
6914632Seric 		w->w_pri = wlist[i].w_pri;
69225013Seric 		w->w_ctime = wlist[i].w_ctime;
69324981Seric 		w->w_next = WorkQ;
69424981Seric 		WorkQ = w;
6954632Seric 	}
6964632Seric 
6977677Seric 	if (tTd(40, 1))
6984632Seric 	{
6994632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7005037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7014632Seric 	}
70210070Seric 
70310090Seric 	return (wn);
7044632Seric }
7054632Seric /*
7067677Seric **  WORKCMPF -- compare function for ordering work.
7074632Seric **
7084632Seric **	Parameters:
7094632Seric **		a -- the first argument.
7104632Seric **		b -- the second argument.
7114632Seric **
7124632Seric **	Returns:
71324981Seric **		-1 if a < b
71424981Seric **		 0 if a == b
71524981Seric **		+1 if a > b
7164632Seric **
7174632Seric **	Side Effects:
7184632Seric **		none.
7194632Seric */
7204632Seric 
7214632Seric workcmpf(a, b)
7225037Seric 	register WORK *a;
7235037Seric 	register WORK *b;
7244632Seric {
72557438Seric 	long pa = a->w_pri;
72657438Seric 	long pb = b->w_pri;
72724941Seric 
72824941Seric 	if (pa == pb)
7294632Seric 		return (0);
73024941Seric 	else if (pa > pb)
73124981Seric 		return (1);
73224981Seric 	else
73310121Seric 		return (-1);
7344632Seric }
7354632Seric /*
7364632Seric **  DOWORK -- do a work request.
7374632Seric **
7384632Seric **	Parameters:
73958884Seric **		id -- the ID of the job to run.
74058884Seric **		forkflag -- if set, run this in background.
74158924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
74258924Seric **			This is used when expanding aliases in the queue.
74361094Seric **			If forkflag is also set, it doesn't wait for the
74461094Seric **			child.
74558884Seric **		e - the envelope in which to run it.
7464632Seric **
7474632Seric **	Returns:
7484632Seric **		none.
7494632Seric **
7504632Seric **	Side Effects:
7514632Seric **		The work request is satisfied if possible.
7524632Seric */
7534632Seric 
75458924Seric dowork(id, forkflag, requeueflag, e)
75558884Seric 	char *id;
75658884Seric 	bool forkflag;
75758924Seric 	bool requeueflag;
75855012Seric 	register ENVELOPE *e;
7594632Seric {
7604632Seric 	register int i;
76151920Seric 	extern bool readqf();
7624632Seric 
7637677Seric 	if (tTd(40, 1))
76458884Seric 		printf("dowork(%s)\n", id);
7654632Seric 
7664632Seric 	/*
76724941Seric 	**  Fork for work.
76824941Seric 	*/
76924941Seric 
77058884Seric 	if (forkflag)
77124941Seric 	{
77224941Seric 		i = fork();
77324941Seric 		if (i < 0)
77424941Seric 		{
77524941Seric 			syserr("dowork: cannot fork");
77624941Seric 			return;
77724941Seric 		}
77824941Seric 	}
77924941Seric 	else
78024941Seric 	{
78124941Seric 		i = 0;
78224941Seric 	}
78324941Seric 
7844632Seric 	if (i == 0)
7854632Seric 	{
7864632Seric 		/*
7874632Seric 		**  CHILD
7888148Seric 		**	Lock the control file to avoid duplicate deliveries.
7898148Seric 		**		Then run the file as though we had just read it.
7907350Seric 		**	We save an idea of the temporary name so we
7917350Seric 		**		can recover on interrupt.
7924632Seric 		*/
7934632Seric 
7947763Seric 		/* set basic modes, etc. */
7957356Seric 		(void) alarm(0);
79655012Seric 		clearenvelope(e, FALSE);
79758737Seric 		e->e_flags |= EF_QUEUERUN;
79858734Seric 		e->e_errormode = EM_MAIL;
79958884Seric 		e->e_id = id;
80063846Seric 		if (forkflag)
80163846Seric 			disconnect(0, e);
8027876Seric # ifdef LOG
80358020Seric 		if (LogLevel > 76)
80455012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8057881Seric 			       getpid());
80656795Seric # endif /* LOG */
8077763Seric 
8087763Seric 		/* don't use the headers from sendmail.cf... */
80955012Seric 		e->e_header = NULL;
8107763Seric 
81151920Seric 		/* read the queue control file -- return if locked */
81263850Seric 		if (!readqf(e, !requeueflag))
8136980Seric 		{
81458884Seric 			if (tTd(40, 4))
81558884Seric 				printf("readqf(%s) failed\n", e->e_id);
81658884Seric 			if (forkflag)
81724941Seric 				exit(EX_OK);
81824941Seric 			else
81924941Seric 				return;
8206980Seric 		}
8216980Seric 
82255012Seric 		e->e_flags |= EF_INQUEUE;
82358929Seric 		eatheader(e, requeueflag);
8246980Seric 
82558924Seric 		if (requeueflag)
82658924Seric 			queueup(e, TRUE, FALSE);
82758924Seric 
8286980Seric 		/* do the delivery */
82958915Seric 		sendall(e, SM_DELIVER);
8306980Seric 
8316980Seric 		/* finish up and exit */
83258884Seric 		if (forkflag)
83324941Seric 			finis();
83424941Seric 		else
83555012Seric 			dropenvelope(e);
8364632Seric 	}
83761094Seric 	else if (!requeueflag)
83824941Seric 	{
83924941Seric 		/*
84024941Seric 		**  Parent -- pick up results.
84124941Seric 		*/
8424632Seric 
84324941Seric 		errno = 0;
84424941Seric 		(void) waitfor(i);
84524941Seric 	}
8464632Seric }
8474632Seric /*
8484632Seric **  READQF -- read queue file and set up environment.
8494632Seric **
8504632Seric **	Parameters:
8519377Seric **		e -- the envelope of the job to run.
85263850Seric **		announcefile -- if set, announce the name of the queue
85363850Seric **			file in error messages.
8544632Seric **
8554632Seric **	Returns:
85651920Seric **		TRUE if it successfully read the queue file.
85751920Seric **		FALSE otherwise.
8584632Seric **
8594632Seric **	Side Effects:
86051920Seric **		The queue file is returned locked.
8614632Seric */
8624632Seric 
86351920Seric bool
86463850Seric readqf(e, announcefile)
8659377Seric 	register ENVELOPE *e;
86663850Seric 	bool announcefile;
8674632Seric {
86817477Seric 	register FILE *qfp;
86954974Seric 	ADDRESS *ctladdr;
87056400Seric 	struct stat st;
87157135Seric 	char *bp;
87258915Seric 	char qf[20];
87357135Seric 	char buf[MAXLINE];
87424954Seric 	extern long atol();
87554974Seric 	extern ADDRESS *setctluser();
8764632Seric 
8774632Seric 	/*
87817468Seric 	**  Read and process the file.
8794632Seric 	*/
8804632Seric 
88158915Seric 	strcpy(qf, queuename(e, 'q'));
88251937Seric 	qfp = fopen(qf, "r+");
88317477Seric 	if (qfp == NULL)
88417477Seric 	{
88558884Seric 		if (tTd(40, 8))
88658884Seric 			printf("readqf(%s): fopen failure (%s)\n",
88758884Seric 				qf, errstring(errno));
88840934Srick 		if (errno != ENOENT)
88940934Srick 			syserr("readqf: no control file %s", qf);
89051920Seric 		return FALSE;
89117477Seric 	}
89240934Srick 
89356400Seric 	/*
89456400Seric 	**  Check the queue file for plausibility to avoid attacks.
89556400Seric 	*/
89656400Seric 
89756400Seric 	if (fstat(fileno(qfp), &st) < 0)
89856400Seric 	{
89956400Seric 		/* must have been being processed by someone else */
90058884Seric 		if (tTd(40, 8))
90158884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90258884Seric 				qf, errstring(errno));
90356400Seric 		fclose(qfp);
90456400Seric 		return FALSE;
90556400Seric 	}
90656400Seric 
90757135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
90856400Seric 	{
90956400Seric # ifdef LOG
91056400Seric 		if (LogLevel > 0)
91156400Seric 		{
91256400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91356400Seric 				e->e_id, st.st_uid, st.st_mode);
91456400Seric 		}
91556795Seric # endif /* LOG */
91658884Seric 		if (tTd(40, 8))
91758884Seric 			printf("readqf(%s): bogus file\n", qf);
91856400Seric 		fclose(qfp);
91963753Seric 		rename(qf, queuename(e, 'Q'));
92056400Seric 		return FALSE;
92156400Seric 	}
92256400Seric 
92358689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
92440934Srick 	{
92558689Seric 		/* being processed by another queuer */
92658884Seric 		if (tTd(40, 8))
92758884Seric 			printf("readqf(%s): locked\n", qf);
92858689Seric 		if (Verbose)
92958689Seric 			printf("%s: locked\n", e->e_id);
93051920Seric # ifdef LOG
93158689Seric 		if (LogLevel > 19)
93258689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
93356795Seric # endif /* LOG */
93440934Srick 		(void) fclose(qfp);
93551920Seric 		return FALSE;
93640934Srick 	}
93740934Srick 
93859101Seric 	if (st.st_size == 0)
93959101Seric 	{
94059101Seric 		/* must be a bogus file -- just remove it */
94159101Seric 		(void) unlink(qf);
94259101Seric 		fclose(qfp);
94359101Seric 		return FALSE;
94459101Seric 	}
94559101Seric 
94651920Seric 	/* save this lock */
94751920Seric 	e->e_lockfp = qfp;
94851920Seric 
94940934Srick 	/* do basic system initialization */
95055012Seric 	initsys(e);
95140934Srick 
95263850Seric 	if (announcefile)
95363850Seric 		FileName = qf;
9549377Seric 	LineNumber = 0;
95563850Seric 	e->e_flags |= EF_GLOBALERRS;
95663850Seric 	OpMode = MD_DELIVER;
95751920Seric 	if (Verbose)
9589377Seric 		printf("\nRunning %s\n", e->e_id);
95954974Seric 	ctladdr = NULL;
96057135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9614632Seric 	{
96258737Seric 		register char *p;
96357529Seric 		struct stat st;
96457529Seric 
96526504Seric 		if (tTd(40, 4))
96657135Seric 			printf("+++++ %s\n", bp);
96757135Seric 		switch (bp[0])
9684632Seric 		{
96940973Sbostic 		  case 'C':		/* specify controlling user */
97057135Seric 			ctladdr = setctluser(&bp[1]);
97140973Sbostic 			break;
97240973Sbostic 
9734632Seric 		  case 'R':		/* specify recipient */
97458082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9754632Seric 			break;
9764632Seric 
97725687Seric 		  case 'E':		/* specify error recipient */
97858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
97925687Seric 			break;
98025687Seric 
9814632Seric 		  case 'H':		/* header */
98257135Seric 			(void) chompheader(&bp[1], FALSE, e);
9834632Seric 			break;
9844632Seric 
98510108Seric 		  case 'M':		/* message */
98657135Seric 			e->e_message = newstr(&bp[1]);
98710108Seric 			break;
98810108Seric 
9894632Seric 		  case 'S':		/* sender */
99058704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9914632Seric 			break;
9924632Seric 
99359093Seric 		  case 'B':		/* body type */
99459093Seric 			e->e_bodytype = newstr(&bp[1]);
99559093Seric 			break;
99659093Seric 
9974632Seric 		  case 'D':		/* data file name */
99857135Seric 			e->e_df = newstr(&bp[1]);
9999544Seric 			e->e_dfp = fopen(e->e_df, "r");
10009544Seric 			if (e->e_dfp == NULL)
100158020Seric 			{
10029377Seric 				syserr("readqf: cannot open %s", e->e_df);
100358020Seric 				e->e_msgsize = -1;
100458020Seric 			}
100558020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
100657529Seric 				e->e_msgsize = st.st_size;
10074632Seric 			break;
10084632Seric 
10097860Seric 		  case 'T':		/* init time */
101057135Seric 			e->e_ctime = atol(&bp[1]);
10114632Seric 			break;
10124632Seric 
10134634Seric 		  case 'P':		/* message priority */
101457135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10154634Seric 			break;
10164634Seric 
101758737Seric 		  case 'F':		/* flag bits */
101858737Seric 			for (p = &bp[1]; *p != '\0'; p++)
101958737Seric 			{
102058737Seric 				switch (*p)
102158737Seric 				{
102258737Seric 				  case 'w':	/* warning sent */
102358737Seric 					e->e_flags |= EF_WARNING;
102458737Seric 					break;
102558737Seric 
102658737Seric 				  case 'r':	/* response */
102758737Seric 					e->e_flags |= EF_RESPONSE;
102858737Seric 					break;
102958737Seric 				}
103058737Seric 			}
103158737Seric 			break;
103258737Seric 
103353400Seric 		  case '$':		/* define macro */
103457135Seric 			define(bp[1], newstr(&bp[2]), e);
103553400Seric 			break;
103653400Seric 
103724941Seric 		  case '\0':		/* blank line; ignore */
103824941Seric 			break;
103924941Seric 
10404632Seric 		  default:
104159700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
104257135Seric 				LineNumber, bp);
104363753Seric 			fclose(qfp);
104463753Seric 			rename(qf, queuename(e, 'Q'));
104563753Seric 			return FALSE;
10464632Seric 		}
104757135Seric 
104857135Seric 		if (bp != buf)
104957135Seric 			free(bp);
10504632Seric 	}
10519377Seric 
10529377Seric 	FileName = NULL;
105324941Seric 
105424941Seric 	/*
105524941Seric 	**  If we haven't read any lines, this queue file is empty.
105624941Seric 	**  Arrange to remove it without referencing any null pointers.
105724941Seric 	*/
105824941Seric 
105924941Seric 	if (LineNumber == 0)
106024941Seric 	{
106124941Seric 		errno = 0;
106224941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
106324941Seric 	}
106451920Seric 	return TRUE;
10654632Seric }
10664632Seric /*
10679630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10689630Seric **
10699630Seric **	Parameters:
10709630Seric **		none.
10719630Seric **
10729630Seric **	Returns:
10739630Seric **		none.
10749630Seric **
10759630Seric **	Side Effects:
10769630Seric **		Prints a listing of the mail queue on the standard output.
10779630Seric */
10785182Seric 
10799630Seric printqueue()
10809630Seric {
10819630Seric 	register WORK *w;
10829630Seric 	FILE *f;
108310070Seric 	int nrequests;
10849630Seric 	char buf[MAXLINE];
10859630Seric 
10869630Seric 	/*
108758250Seric 	**  Check for permission to print the queue
108858250Seric 	*/
108958250Seric 
109063787Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0)
109158250Seric 	{
109258250Seric 		struct stat st;
109358523Seric # ifdef NGROUPS
109458523Seric 		int n;
109563937Seric 		GIDSET_T gidset[NGROUPS];
109658523Seric # endif
109758250Seric 
109858517Seric 		if (stat(QueueDir, &st) < 0)
109958250Seric 		{
110058250Seric 			syserr("Cannot stat %s", QueueDir);
110158250Seric 			return;
110258250Seric 		}
110358523Seric # ifdef NGROUPS
110458523Seric 		n = getgroups(NGROUPS, gidset);
110558523Seric 		while (--n >= 0)
110658523Seric 		{
110758523Seric 			if (gidset[n] == st.st_gid)
110858523Seric 				break;
110958523Seric 		}
111058523Seric 		if (n < 0)
111158523Seric # else
111263787Seric 		if (RealGid != st.st_gid)
111358523Seric # endif
111458250Seric 		{
111558250Seric 			usrerr("510 You are not permitted to see the queue");
111658250Seric 			setstat(EX_NOPERM);
111758250Seric 			return;
111858250Seric 		}
111958250Seric 	}
112058250Seric 
112158250Seric 	/*
11229630Seric 	**  Read and order the queue.
11239630Seric 	*/
11249630Seric 
112524941Seric 	nrequests = orderq(TRUE);
11269630Seric 
11279630Seric 	/*
11289630Seric 	**  Print the work list that we have read.
11299630Seric 	*/
11309630Seric 
11319630Seric 	/* first see if there is anything */
113210070Seric 	if (nrequests <= 0)
11339630Seric 	{
113410070Seric 		printf("Mail queue is empty\n");
11359630Seric 		return;
11369630Seric 	}
11379630Seric 
113851920Seric 	CurrentLA = getla();	/* get load average */
113940934Srick 
114010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
114125687Seric 	if (nrequests > QUEUESIZE)
114225687Seric 		printf(", only %d printed", QUEUESIZE);
114324979Seric 	if (Verbose)
114458716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114524979Seric 	else
114658716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11479630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11489630Seric 	{
11499630Seric 		struct stat st;
115010070Seric 		auto time_t submittime = 0;
115110070Seric 		long dfsize = -1;
115258737Seric 		int flags = 0;
115310108Seric 		char message[MAXLINE];
115459093Seric 		char bodytype[MAXNAME];
11559630Seric 
115617468Seric 		f = fopen(w->w_name, "r");
115717468Seric 		if (f == NULL)
115817468Seric 		{
115917468Seric 			errno = 0;
116017468Seric 			continue;
116117468Seric 		}
116258724Seric 		printf("%8s", w->w_name + 2);
116358689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
116410070Seric 			printf("*");
116557438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
116624941Seric 			printf("X");
116710070Seric 		else
116810070Seric 			printf(" ");
116910070Seric 		errno = 0;
117017468Seric 
117159093Seric 		message[0] = bodytype[0] = '\0';
11729630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11739630Seric 		{
117453400Seric 			register int i;
117558737Seric 			register char *p;
117653400Seric 
11779630Seric 			fixcrlf(buf, TRUE);
11789630Seric 			switch (buf[0])
11799630Seric 			{
118010108Seric 			  case 'M':	/* error message */
118153400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
118258737Seric 					i = sizeof message - 1;
118353400Seric 				bcopy(&buf[1], message, i);
118453400Seric 				message[i] = '\0';
118510108Seric 				break;
118610108Seric 
118759093Seric 			  case 'B':	/* body type */
118859093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
118959093Seric 					i = sizeof bodytype - 1;
119059093Seric 				bcopy(&buf[1], bodytype, i);
119159093Seric 				bodytype[i] = '\0';
119259093Seric 				break;
119359093Seric 
11949630Seric 			  case 'S':	/* sender name */
119524979Seric 				if (Verbose)
119658737Seric 					printf("%8ld %10ld%c%.12s %.38s",
119758737Seric 					    dfsize,
119858737Seric 					    w->w_pri,
119958737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
120058737Seric 					    ctime(&submittime) + 4,
120124979Seric 					    &buf[1]);
120224979Seric 				else
120324979Seric 					printf("%8ld %.16s %.45s", dfsize,
120424979Seric 					    ctime(&submittime), &buf[1]);
120559093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
120659093Seric 				{
120759093Seric 					printf("\n    %10.10s", bodytype);
120859093Seric 					if (message[0] != '\0')
120959093Seric 						printf("   (%.60s)", message);
121059093Seric 				}
12119630Seric 				break;
121251920Seric 
121340973Sbostic 			  case 'C':	/* controlling user */
121454974Seric 				if (Verbose)
121558716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
121658716Seric 						&buf[1]);
121740973Sbostic 				break;
12189630Seric 
12199630Seric 			  case 'R':	/* recipient name */
122024979Seric 				if (Verbose)
122158716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
122224979Seric 				else
122358716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12249630Seric 				break;
12259630Seric 
12269630Seric 			  case 'T':	/* creation time */
122724941Seric 				submittime = atol(&buf[1]);
12289630Seric 				break;
122910070Seric 
123010070Seric 			  case 'D':	/* data file name */
123110070Seric 				if (stat(&buf[1], &st) >= 0)
123210070Seric 					dfsize = st.st_size;
123310070Seric 				break;
123458737Seric 
123558737Seric 			  case 'F':	/* flag bits */
123658737Seric 				for (p = &buf[1]; *p != '\0'; p++)
123758737Seric 				{
123858737Seric 					switch (*p)
123958737Seric 					{
124058737Seric 					  case 'w':
124158737Seric 						flags |= EF_WARNING;
124258737Seric 						break;
124358737Seric 					}
124458737Seric 				}
12459630Seric 			}
12469630Seric 		}
124710070Seric 		if (submittime == (time_t) 0)
124810070Seric 			printf(" (no control file)");
12499630Seric 		printf("\n");
125023098Seric 		(void) fclose(f);
12519630Seric 	}
12529630Seric }
12539630Seric 
125456795Seric # endif /* QUEUE */
125517468Seric /*
125617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
125717468Seric **
125817468Seric **	Assigns an id code if one does not already exist.
125917468Seric **	This code is very careful to avoid trashing existing files
126017468Seric **	under any circumstances.
126117468Seric **
126217468Seric **	Parameters:
126317468Seric **		e -- envelope to build it in/from.
126417468Seric **		type -- the file type, used as the first character
126517468Seric **			of the file name.
126617468Seric **
126717468Seric **	Returns:
126817468Seric **		a pointer to the new file name (in a static buffer).
126917468Seric **
127017468Seric **	Side Effects:
127151920Seric **		If no id code is already assigned, queuename will
127251920Seric **		assign an id code, create a qf file, and leave a
127351920Seric **		locked, open-for-write file pointer in the envelope.
127417468Seric */
127517468Seric 
127617468Seric char *
127717468Seric queuename(e, type)
127817468Seric 	register ENVELOPE *e;
127959700Seric 	int type;
128017468Seric {
128117468Seric 	static int pid = -1;
128258741Seric 	static char c0;
128358741Seric 	static char c1;
128458741Seric 	static char c2;
128558716Seric 	time_t now;
128658716Seric 	struct tm *tm;
128758689Seric 	static char buf[MAXNAME];
128817468Seric 
128917468Seric 	if (e->e_id == NULL)
129017468Seric 	{
129117468Seric 		char qf[20];
129217468Seric 
129317468Seric 		/* find a unique id */
129417468Seric 		if (pid != getpid())
129517468Seric 		{
129617468Seric 			/* new process -- start back at "AA" */
129717468Seric 			pid = getpid();
129858716Seric 			now = curtime();
129958716Seric 			tm = localtime(&now);
130058716Seric 			c0 = 'A' + tm->tm_hour;
130117468Seric 			c1 = 'A';
130217468Seric 			c2 = 'A' - 1;
130317468Seric 		}
130458716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130517468Seric 
130617468Seric 		while (c1 < '~' || c2 < 'Z')
130717468Seric 		{
130817468Seric 			int i;
130917468Seric 
131017468Seric 			if (c2 >= 'Z')
131117468Seric 			{
131217468Seric 				c1++;
131317468Seric 				c2 = 'A' - 1;
131417468Seric 			}
131558716Seric 			qf[3] = c1;
131658716Seric 			qf[4] = ++c2;
131717468Seric 			if (tTd(7, 20))
131840934Srick 				printf("queuename: trying \"%s\"\n", qf);
131917468Seric 
132040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
132151920Seric 			if (i < 0)
132251920Seric 			{
132351920Seric 				if (errno == EEXIST)
132451920Seric 					continue;
132551920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132651920Seric 					qf, QueueDir);
132751920Seric 				exit(EX_UNAVAILABLE);
132851920Seric 			}
132958689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
133051920Seric 			{
133151920Seric 				e->e_lockfp = fdopen(i, "w");
133240934Srick 				break;
133317468Seric 			}
133451920Seric 
133551920Seric 			/* a reader got the file; abandon it and try again */
133651920Seric 			(void) close(i);
133717468Seric 		}
133817468Seric 		if (c1 >= '~' && c2 >= 'Z')
133917468Seric 		{
134017468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
134117468Seric 				qf, QueueDir);
134217468Seric 			exit(EX_OSERR);
134317468Seric 		}
134417468Seric 		e->e_id = newstr(&qf[2]);
134517468Seric 		define('i', e->e_id, e);
134617468Seric 		if (tTd(7, 1))
134717468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
134817468Seric # ifdef LOG
134958020Seric 		if (LogLevel > 93)
135017468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
135156795Seric # endif /* LOG */
135217468Seric 	}
135317468Seric 
135417468Seric 	if (type == '\0')
135517468Seric 		return (NULL);
135617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
135717468Seric 	if (tTd(7, 2))
135817468Seric 		printf("queuename: %s\n", buf);
135917468Seric 	return (buf);
136017468Seric }
136117468Seric /*
136217468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
136317468Seric **
136417468Seric **	Parameters:
136517468Seric **		e -- the envelope to unlock.
136617468Seric **
136717468Seric **	Returns:
136817468Seric **		none
136917468Seric **
137017468Seric **	Side Effects:
137117468Seric **		unlocks the queue for `e'.
137217468Seric */
137317468Seric 
137417468Seric unlockqueue(e)
137517468Seric 	ENVELOPE *e;
137617468Seric {
137758680Seric 	if (tTd(51, 4))
137858680Seric 		printf("unlockqueue(%s)\n", e->e_id);
137958680Seric 
138051920Seric 	/* if there is a lock file in the envelope, close it */
138151920Seric 	if (e->e_lockfp != NULL)
138258680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
138351920Seric 	e->e_lockfp = NULL;
138451920Seric 
138558728Seric 	/* don't create a queue id if we don't already have one */
138658728Seric 	if (e->e_id == NULL)
138758728Seric 		return;
138858728Seric 
138917468Seric 	/* remove the transcript */
139017468Seric # ifdef LOG
139158020Seric 	if (LogLevel > 87)
139217468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
139356795Seric # endif /* LOG */
139458680Seric 	if (!tTd(51, 104))
139517468Seric 		xunlink(queuename(e, 'x'));
139617468Seric 
139717468Seric }
139840973Sbostic /*
139954974Seric **  SETCTLUSER -- create a controlling address
140040973Sbostic **
140154974Seric **	Create a fake "address" given only a local login name; this is
140254974Seric **	used as a "controlling user" for future recipient addresses.
140340973Sbostic **
140440973Sbostic **	Parameters:
140554974Seric **		user -- the user name of the controlling user.
140640973Sbostic **
140740973Sbostic **	Returns:
140854974Seric **		An address descriptor for the controlling user.
140940973Sbostic **
141040973Sbostic **	Side Effects:
141140973Sbostic **		none.
141240973Sbostic */
141340973Sbostic 
141454974Seric ADDRESS *
141554974Seric setctluser(user)
141654974Seric 	char *user;
141740973Sbostic {
141854974Seric 	register ADDRESS *a;
141940973Sbostic 	struct passwd *pw;
142059113Seric 	char *p;
142140973Sbostic 
142240973Sbostic 	/*
142354974Seric 	**  See if this clears our concept of controlling user.
142440973Sbostic 	*/
142540973Sbostic 
142663850Seric 	if (user == NULL || *user == '\0')
142763850Seric 		return NULL;
142840973Sbostic 
142940973Sbostic 	/*
143054974Seric 	**  Set up addr fields for controlling user.
143140973Sbostic 	*/
143240973Sbostic 
143354974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143454974Seric 	bzero((char *) a, sizeof *a);
143559113Seric 
143659113Seric 	p = strchr(user, ':');
143759113Seric 	if (p != NULL)
143859113Seric 		*p++ = '\0';
143959270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
144040973Sbostic 	{
144140973Sbostic 		a->q_home = newstr(pw->pw_dir);
144240973Sbostic 		a->q_uid = pw->pw_uid;
144340973Sbostic 		a->q_gid = pw->pw_gid;
144457642Seric 		a->q_user = newstr(user);
144559270Seric 		a->q_flags |= QGOODUID;
144640973Sbostic 	}
144740973Sbostic 	else
144840973Sbostic 	{
144957642Seric 		a->q_user = newstr(DefUser);
145040973Sbostic 	}
145140973Sbostic 
145259270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
145356328Seric 	a->q_mailer = LocalMailer;
145459113Seric 	if (p == NULL)
145559113Seric 		a->q_paddr = a->q_user;
145659113Seric 	else
145759113Seric 		a->q_paddr = newstr(p);
145854974Seric 	return a;
145940973Sbostic }
1460