xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 63937)
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*63937Seric static char sccsid[] = "@(#)queue.c	8.6 (Berkeley) 07/19/93 (with queueing)";
1433731Sbostic #else
15*63937Seric static char sccsid[] = "@(#)queue.c	8.6 (Berkeley) 07/19/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194634Seric # include <signal.h>
204632Seric # include <errno.h>
2140973Sbostic # include <pwd.h>
2257736Seric # include <dirent.h>
234632Seric 
2433731Sbostic # ifdef QUEUE
254632Seric 
264632Seric /*
279377Seric **  Work queue.
289377Seric */
299377Seric 
309377Seric struct work
319377Seric {
329377Seric 	char		*w_name;	/* name of control file */
339377Seric 	long		w_pri;		/* priority of message, see below */
3425013Seric 	time_t		w_ctime;	/* creation time of message */
359377Seric 	struct work	*w_next;	/* next in queue */
369377Seric };
379377Seric 
389377Seric typedef struct work	WORK;
399377Seric 
409377Seric WORK	*WorkQ;			/* queue of things to be done */
419377Seric /*
424632Seric **  QUEUEUP -- queue a message up for future transmission.
434632Seric **
444632Seric **	Parameters:
456980Seric **		e -- the envelope to queue up.
466999Seric **		queueall -- if TRUE, queue all addresses, rather than
476999Seric **			just those with the QQUEUEUP flag set.
489377Seric **		announce -- if TRUE, tell when you are queueing up.
494632Seric **
504632Seric **	Returns:
5151920Seric **		none.
524632Seric **
534632Seric **	Side Effects:
549377Seric **		The current request are saved in a control file.
5551920Seric **		The queue file is left locked.
564632Seric */
574632Seric 
589377Seric queueup(e, queueall, announce)
596980Seric 	register ENVELOPE *e;
606999Seric 	bool queueall;
619377Seric 	bool announce;
624632Seric {
637812Seric 	char *qf;
647812Seric 	register FILE *tfp;
654632Seric 	register HDR *h;
665007Seric 	register ADDRESS *q;
6751920Seric 	int fd;
6851920Seric 	int i;
6951920Seric 	bool newid;
7053400Seric 	register char *p;
7110173Seric 	MAILER nullmailer;
7251920Seric 	char buf[MAXLINE], tf[MAXLINE];
734632Seric 
745037Seric 	/*
7517477Seric 	**  Create control file.
765037Seric 	*/
774632Seric 
7851920Seric 	newid = (e->e_id == NULL);
7951920Seric 	strcpy(tf, queuename(e, 't'));
8051920Seric 	tfp = e->e_lockfp;
8151920Seric 	if (tfp == NULL)
8251920Seric 		newid = FALSE;
8351920Seric 	if (newid)
8451835Seric 	{
8551920Seric 		tfp = e->e_lockfp;
8651920Seric 	}
8751920Seric 	else
8851920Seric 	{
8951920Seric 		/* get a locked tf file */
9051920Seric 		for (i = 100; --i >= 0; )
9151835Seric 		{
9251920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9351920Seric 			if (fd < 0)
9451835Seric 			{
9551920Seric 				if (errno == EEXIST)
9651920Seric 					continue;
9758801Seric notemp:
9858690Seric 				syserr("!queueup: cannot create temp file %s", tf);
9941636Srick 			}
10058689Seric 
10158689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10251920Seric 				break;
10358689Seric 
10451920Seric 			close(fd);
10558801Seric 			sleep(i);
10641636Srick 		}
10758801Seric 		if (fd < 0)
10858801Seric 			goto notemp;
10941636Srick 
11051920Seric 		tfp = fdopen(fd, "w");
11151920Seric 	}
1124632Seric 
1137677Seric 	if (tTd(40, 1))
11417468Seric 		printf("queueing %s\n", e->e_id);
1154632Seric 
1164632Seric 	/*
1176980Seric 	**  If there is no data file yet, create one.
1186980Seric 	*/
1196980Seric 
1206980Seric 	if (e->e_df == NULL)
1216980Seric 	{
1226980Seric 		register FILE *dfp;
1239389Seric 		extern putbody();
1246980Seric 
1257812Seric 		e->e_df = newstr(queuename(e, 'd'));
12640934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
12740934Srick 		if (fd < 0)
12858690Seric 			syserr("!queueup: cannot create %s", e->e_df);
12940934Srick 		dfp = fdopen(fd, "w");
13059730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
13158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1329389Seric 		e->e_putbody = putbody;
1336980Seric 	}
1346980Seric 
1356980Seric 	/*
1364632Seric 	**  Output future work requests.
13725687Seric 	**	Priority and creation time should be first, since
13825687Seric 	**	they are required by orderq.
1394632Seric 	*/
1404632Seric 
1419377Seric 	/* output message priority */
1429377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1439377Seric 
1449630Seric 	/* output creation time */
1459630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1469630Seric 
14759093Seric 	/* output type and name of data file */
14859093Seric 	if (e->e_bodytype != NULL)
14959093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1507812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1514632Seric 
15210108Seric 	/* message from envelope, if it exists */
15310108Seric 	if (e->e_message != NULL)
15410108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15510108Seric 
15658737Seric 	/* send various flag bits through */
15758737Seric 	p = buf;
15858737Seric 	if (bitset(EF_WARNING, e->e_flags))
15958737Seric 		*p++ = 'w';
16058737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16158737Seric 		*p++ = 'r';
16258737Seric 	*p++ = '\0';
16358737Seric 	if (buf[0] != '\0')
16458737Seric 		fprintf(tfp, "F%s\n", buf);
16558737Seric 
16658957Seric 	/* $r and $s and $_ macro values */
16753400Seric 	if ((p = macvalue('r', e)) != NULL)
16853400Seric 		fprintf(tfp, "$r%s\n", p);
16953400Seric 	if ((p = macvalue('s', e)) != NULL)
17053400Seric 		fprintf(tfp, "$s%s\n", p);
17158957Seric 	if ((p = macvalue('_', e)) != NULL)
17258957Seric 		fprintf(tfp, "$_%s\n", p);
17353400Seric 
1744632Seric 	/* output name of sender */
1757812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1764632Seric 
17755360Seric 	/* output list of error recipients */
17859670Seric 	printctladdr(NULL, NULL);
17955360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18055360Seric 	{
18158680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18255360Seric 		{
18359113Seric 			printctladdr(q, tfp);
18455360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18555360Seric 		}
18655360Seric 	}
18755360Seric 
1884632Seric 	/* output list of recipient addresses */
1896980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1904632Seric 	{
19158250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19258680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1938245Seric 		{
19459113Seric 			printctladdr(q, tfp);
1957812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1969377Seric 			if (announce)
1979377Seric 			{
1989377Seric 				e->e_to = q->q_paddr;
19958151Seric 				message("queued");
20058020Seric 				if (LogLevel > 8)
20158337Seric 					logdelivery(NULL, NULL, "queued", e);
2029377Seric 				e->e_to = NULL;
2039377Seric 			}
2049387Seric 			if (tTd(40, 1))
2059387Seric 			{
2069387Seric 				printf("queueing ");
2079387Seric 				printaddr(q, FALSE);
2089387Seric 			}
2098245Seric 		}
2104632Seric 	}
2114632Seric 
2129377Seric 	/*
2139377Seric 	**  Output headers for this message.
2149377Seric 	**	Expand macros completely here.  Queue run will deal with
2159377Seric 	**	everything as absolute headers.
2169377Seric 	**		All headers that must be relative to the recipient
2179377Seric 	**		can be cracked later.
21810173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
21910173Seric 	**	no effect on the addresses as they are output.
2209377Seric 	*/
2219377Seric 
22210686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22358020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
22458020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
22510349Seric 	nullmailer.m_eol = "\n";
22610173Seric 
22758050Seric 	define('g', "\201f", e);
2286980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2294632Seric 	{
23010686Seric 		extern bool bitzerop();
23110686Seric 
23212015Seric 		/* don't output null headers */
2334632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2344632Seric 			continue;
23512015Seric 
23612015Seric 		/* don't output resent headers on non-resent messages */
23712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
23812015Seric 			continue;
23912015Seric 
24012015Seric 		/* output this header */
2417812Seric 		fprintf(tfp, "H");
24212015Seric 
24312015Seric 		/* if conditional, output the set of conditions */
24410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
24510686Seric 		{
24610686Seric 			int j;
24710686Seric 
24823098Seric 			(void) putc('?', tfp);
24910686Seric 			for (j = '\0'; j <= '\177'; j++)
25010686Seric 				if (bitnset(j, h->h_mflags))
25123098Seric 					(void) putc(j, tfp);
25223098Seric 			(void) putc('?', tfp);
25310686Seric 		}
25412015Seric 
25512015Seric 		/* output the header: expand macros, convert addresses */
2567763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2577763Seric 		{
2587763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2598236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2607763Seric 		}
2618245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2629348Seric 		{
26358737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
26463753Seric 			FILE *savetrace = TrafficLogFile;
26558737Seric 
26663753Seric 			TrafficLogFile = NULL;
26763753Seric 
26858737Seric 			if (bitset(H_FROM, h->h_flags))
26958737Seric 				oldstyle = FALSE;
27058737Seric 
27158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27255012Seric 				 &nullmailer, e);
27363753Seric 
27463753Seric 			TrafficLogFile = savetrace;
2759348Seric 		}
2767763Seric 		else
2778245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2784632Seric 	}
2794632Seric 
2804632Seric 	/*
2814632Seric 	**  Clean up.
2824632Seric 	*/
2834632Seric 
28458732Seric 	fflush(tfp);
28560603Seric 	fsync(fileno(tfp));
28658732Seric 	if (ferror(tfp))
28758732Seric 	{
28858732Seric 		if (newid)
28958732Seric 			syserr("!552 Error writing control file %s", tf);
29058732Seric 		else
29158732Seric 			syserr("!452 Error writing control file %s", tf);
29258732Seric 	}
29358732Seric 
29451920Seric 	if (!newid)
29551920Seric 	{
29651920Seric 		qf = queuename(e, 'q');
29751920Seric 		if (rename(tf, qf) < 0)
29851920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29951920Seric 		if (e->e_lockfp != NULL)
30058680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
30151920Seric 		e->e_lockfp = tfp;
30251920Seric 	}
30351920Seric 	else
30451920Seric 		qf = tf;
30541636Srick 	errno = 0;
3067391Seric 
3077677Seric # ifdef LOG
3087677Seric 	/* save log info */
30958020Seric 	if (LogLevel > 79)
3107878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
31156795Seric # endif /* LOG */
31251920Seric 	return;
3134632Seric }
31454974Seric 
31554974Seric printctladdr(a, tfp)
31659113Seric 	register ADDRESS *a;
31754974Seric 	FILE *tfp;
31854974Seric {
31959113Seric 	char *uname;
32059113Seric 	register struct passwd *pw;
32159113Seric 	register ADDRESS *q;
32259113Seric 	uid_t uid;
32359113Seric 	static ADDRESS *lastctladdr;
32459113Seric 	static uid_t lastuid;
32554974Seric 
32659113Seric 	/* initialization */
32763850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
32854974Seric 	{
32959670Seric 		if (lastctladdr != NULL && tfp != NULL)
33059113Seric 			fprintf(tfp, "C\n");
33159113Seric 		lastctladdr = NULL;
33259113Seric 		lastuid = 0;
33354974Seric 		return;
33454974Seric 	}
33559113Seric 
33659113Seric 	/* find the active uid */
33759113Seric 	q = getctladdr(a);
33859113Seric 	if (q == NULL)
33959113Seric 		uid = 0;
34054974Seric 	else
34159113Seric 		uid = q->q_uid;
34263850Seric 	a = a->q_alias;
34359113Seric 
34459113Seric 	/* check to see if this is the same as last time */
34559113Seric 	if (lastctladdr != NULL && uid == lastuid &&
34659113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
34759113Seric 		return;
34859113Seric 	lastuid = uid;
34959113Seric 	lastctladdr = a;
35059113Seric 
35159113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35259270Seric 		uname = "";
35359113Seric 	else
35459113Seric 		uname = pw->pw_name;
35559113Seric 
35659113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
35754974Seric }
35854974Seric 
3594632Seric /*
3604632Seric **  RUNQUEUE -- run the jobs in the queue.
3614632Seric **
3624632Seric **	Gets the stuff out of the queue in some presumably logical
3634632Seric **	order and processes them.
3644632Seric **
3654632Seric **	Parameters:
36624941Seric **		forkflag -- TRUE if the queue scanning should be done in
36724941Seric **			a child process.  We double-fork so it is not our
36824941Seric **			child and we don't have to clean up after it.
3694632Seric **
3704632Seric **	Returns:
3714632Seric **		none.
3724632Seric **
3734632Seric **	Side Effects:
3744632Seric **		runs things in the mail queue.
3754632Seric */
3764632Seric 
37755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37855360Seric 
37955360Seric runqueue(forkflag)
3804639Seric 	bool forkflag;
3814632Seric {
38255360Seric 	register ENVELOPE *e;
38355360Seric 	extern ENVELOPE BlankEnvelope;
38424953Seric 
3857466Seric 	/*
38624953Seric 	**  If no work will ever be selected, don't even bother reading
38724953Seric 	**  the queue.
38824953Seric 	*/
38924953Seric 
39051920Seric 	CurrentLA = getla();	/* get load average */
39140934Srick 
39258132Seric 	if (shouldqueue(0L, curtime()))
39324953Seric 	{
39424953Seric 		if (Verbose)
39524953Seric 			printf("Skipping queue run -- load average too high\n");
39658107Seric 		if (forkflag && QueueIntvl != 0)
39758107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39855360Seric 		return;
39924953Seric 	}
40024953Seric 
40124953Seric 	/*
4027466Seric 	**  See if we want to go off and do other useful work.
4037466Seric 	*/
4044639Seric 
4054639Seric 	if (forkflag)
4064639Seric 	{
4077943Seric 		int pid;
4087943Seric 
4097943Seric 		pid = dofork();
4107943Seric 		if (pid != 0)
4114639Seric 		{
41246928Sbostic 			extern void reapchild();
41325184Seric 
4147943Seric 			/* parent -- pick up intermediate zombie */
41525184Seric #ifndef SIGCHLD
4169377Seric 			(void) waitfor(pid);
41756795Seric #else /* SIGCHLD */
41825184Seric 			(void) signal(SIGCHLD, reapchild);
41956795Seric #endif /* SIGCHLD */
4207690Seric 			if (QueueIntvl != 0)
4219348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4224639Seric 			return;
4234639Seric 		}
4247943Seric 		/* child -- double fork */
42525184Seric #ifndef SIGCHLD
4267943Seric 		if (fork() != 0)
4277943Seric 			exit(EX_OK);
42856795Seric #else /* SIGCHLD */
42925184Seric 		(void) signal(SIGCHLD, SIG_DFL);
43056795Seric #endif /* SIGCHLD */
4314639Seric 	}
43224941Seric 
43340934Srick 	setproctitle("running queue: %s", QueueDir);
43424941Seric 
4357876Seric # ifdef LOG
43658020Seric 	if (LogLevel > 69)
43755360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43855360Seric 			QueueDir, getpid(), forkflag);
43956795Seric # endif /* LOG */
4404639Seric 
4417466Seric 	/*
44210205Seric 	**  Release any resources used by the daemon code.
44310205Seric 	*/
44410205Seric 
44510205Seric # ifdef DAEMON
44610205Seric 	clrdaemon();
44756795Seric # endif /* DAEMON */
44810205Seric 
44910205Seric 	/*
45055360Seric 	**  Create ourselves an envelope
45155360Seric 	*/
45255360Seric 
45355360Seric 	CurEnv = &QueueEnvelope;
45458179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45555360Seric 	e->e_flags = BlankEnvelope.e_flags;
45655360Seric 
45755360Seric 	/*
45827175Seric 	**  Make sure the alias database is open.
45927175Seric 	*/
46027175Seric 
46160537Seric 	initmaps(FALSE, e);
46227175Seric 
46327175Seric 	/*
4647466Seric 	**  Start making passes through the queue.
4657466Seric 	**	First, read and sort the entire queue.
4667466Seric 	**	Then, process the work in that order.
4677466Seric 	**		But if you take too long, start over.
4687466Seric 	*/
4697466Seric 
4707943Seric 	/* order the existing work requests */
47124954Seric 	(void) orderq(FALSE);
4727690Seric 
4737943Seric 	/* process them once at a time */
4747943Seric 	while (WorkQ != NULL)
4754639Seric 	{
4767943Seric 		WORK *w = WorkQ;
4777881Seric 
4787943Seric 		WorkQ = WorkQ->w_next;
47958884Seric 
48058884Seric 		/*
48158884Seric 		**  Ignore jobs that are too expensive for the moment.
48258884Seric 		*/
48358884Seric 
48458884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
48558884Seric 		{
48658884Seric 			if (Verbose)
48758884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
48858884Seric 		}
48958930Seric 		else
49058930Seric 		{
49158930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
49258930Seric 		}
4937943Seric 		free(w->w_name);
4947943Seric 		free((char *) w);
4954639Seric 	}
49629866Seric 
49729866Seric 	/* exit without the usual cleanup */
49855467Seric 	e->e_id = NULL;
49955467Seric 	finis();
5004634Seric }
5014634Seric /*
5024632Seric **  ORDERQ -- order the work queue.
5034632Seric **
5044632Seric **	Parameters:
50524941Seric **		doall -- if set, include everything in the queue (even
50624941Seric **			the jobs that cannot be run because the load
50724941Seric **			average is too high).  Otherwise, exclude those
50824941Seric **			jobs.
5094632Seric **
5104632Seric **	Returns:
51110121Seric **		The number of request in the queue (not necessarily
51210121Seric **		the number of requests in WorkQ however).
5134632Seric **
5144632Seric **	Side Effects:
5154632Seric **		Sets WorkQ to the queue of available work, in order.
5164632Seric */
5174632Seric 
51825687Seric # define NEED_P		001
51925687Seric # define NEED_T		002
52058318Seric # define NEED_R		004
52158318Seric # define NEED_S		010
5224632Seric 
52324941Seric orderq(doall)
52424941Seric 	bool doall;
5254632Seric {
52660219Seric 	register struct dirent *d;
5274632Seric 	register WORK *w;
5286625Sglickman 	DIR *f;
5294632Seric 	register int i;
53025687Seric 	WORK wlist[QUEUESIZE+1];
53110070Seric 	int wn = -1;
5324632Seric 	extern workcmpf();
5334632Seric 
53458318Seric 	if (tTd(41, 1))
53558318Seric 	{
53658318Seric 		printf("orderq:\n");
53758318Seric 		if (QueueLimitId != NULL)
53858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
53958318Seric 		if (QueueLimitSender != NULL)
54058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
54158318Seric 		if (QueueLimitRecipient != NULL)
54258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
54358318Seric 	}
54458318Seric 
5454632Seric 	/* clear out old WorkQ */
5464632Seric 	for (w = WorkQ; w != NULL; )
5474632Seric 	{
5484632Seric 		register WORK *nw = w->w_next;
5494632Seric 
5504632Seric 		WorkQ = nw;
5514632Seric 		free(w->w_name);
5524632Seric 		free((char *) w);
5534632Seric 		w = nw;
5544632Seric 	}
5554632Seric 
5564632Seric 	/* open the queue directory */
5578148Seric 	f = opendir(".");
5584632Seric 	if (f == NULL)
5594632Seric 	{
5608148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
56110070Seric 		return (0);
5624632Seric 	}
5634632Seric 
5644632Seric 	/*
5654632Seric 	**  Read the work directory.
5664632Seric 	*/
5674632Seric 
56810070Seric 	while ((d = readdir(f)) != NULL)
5694632Seric 	{
5709377Seric 		FILE *cf;
5714632Seric 		char lbuf[MAXNAME];
57258318Seric 		extern bool strcontainedin();
5734632Seric 
5744632Seric 		/* is this an interesting entry? */
5757812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5764632Seric 			continue;
5774632Seric 
57858318Seric 		if (QueueLimitId != NULL &&
57958318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
58058318Seric 			continue;
58158318Seric 
58258722Seric 		/*
58358722Seric 		**  Check queue name for plausibility.  This handles
58458722Seric 		**  both old and new type ids.
58558722Seric 		*/
58658722Seric 
58758722Seric 		i = strlen(d->d_name);
58858722Seric 		if (i != 9 && i != 10)
58958020Seric 		{
59058020Seric 			if (Verbose)
59158020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
59258020Seric #ifdef LOG
59358020Seric 			if (LogLevel > 3)
59459615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
59558020Seric 					d->d_name);
59658020Seric #endif
59758020Seric 			if (strlen(d->d_name) >= MAXNAME)
59858020Seric 				d->d_name[MAXNAME - 1] = '\0';
59958020Seric 			strcpy(lbuf, d->d_name);
60058020Seric 			lbuf[0] = 'Q';
60158020Seric 			(void) rename(d->d_name, lbuf);
60258020Seric 			continue;
60358020Seric 		}
60458020Seric 
60510070Seric 		/* yes -- open control file (if not too many files) */
60625687Seric 		if (++wn >= QUEUESIZE)
60710070Seric 			continue;
60858318Seric 
6098148Seric 		cf = fopen(d->d_name, "r");
6104632Seric 		if (cf == NULL)
6114632Seric 		{
6127055Seric 			/* this may be some random person sending hir msgs */
6137055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
61410090Seric 			if (tTd(41, 2))
61510090Seric 				printf("orderq: cannot open %s (%d)\n",
61610090Seric 					d->d_name, errno);
6177055Seric 			errno = 0;
61810090Seric 			wn--;
6194632Seric 			continue;
6204632Seric 		}
62125687Seric 		w = &wlist[wn];
62225687Seric 		w->w_name = newstr(d->d_name);
6234632Seric 
62425027Seric 		/* make sure jobs in creation don't clog queue */
62525687Seric 		w->w_pri = 0x7fffffff;
62625687Seric 		w->w_ctime = 0;
62725027Seric 
6284632Seric 		/* extract useful information */
62925687Seric 		i = NEED_P | NEED_T;
63058318Seric 		if (QueueLimitSender != NULL)
63158318Seric 			i |= NEED_S;
63258318Seric 		if (QueueLimitRecipient != NULL)
63358318Seric 			i |= NEED_R;
63425687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6354632Seric 		{
63624954Seric 			extern long atol();
63758318Seric 			extern bool strcontainedin();
63824954Seric 
63924941Seric 			switch (lbuf[0])
6404632Seric 			{
64124941Seric 			  case 'P':
64225687Seric 				w->w_pri = atol(&lbuf[1]);
64325687Seric 				i &= ~NEED_P;
6444632Seric 				break;
64525013Seric 
64625013Seric 			  case 'T':
64725687Seric 				w->w_ctime = atol(&lbuf[1]);
64825687Seric 				i &= ~NEED_T;
64925013Seric 				break;
65058318Seric 
65158318Seric 			  case 'R':
65258318Seric 				if (QueueLimitRecipient != NULL &&
65358318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
65458318Seric 					i &= ~NEED_R;
65558318Seric 				break;
65658318Seric 
65758318Seric 			  case 'S':
65858318Seric 				if (QueueLimitSender != NULL &&
65958318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
66058318Seric 					i &= ~NEED_S;
66158318Seric 				break;
6624632Seric 			}
6634632Seric 		}
6644632Seric 		(void) fclose(cf);
66524953Seric 
66658318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
66758318Seric 		    bitset(NEED_R|NEED_S, i))
66824953Seric 		{
66924953Seric 			/* don't even bother sorting this job in */
67024953Seric 			wn--;
67124953Seric 		}
6724632Seric 	}
6736625Sglickman 	(void) closedir(f);
67410090Seric 	wn++;
6754632Seric 
6764632Seric 	/*
6774632Seric 	**  Sort the work directory.
6784632Seric 	*/
6794632Seric 
68025687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6814632Seric 
6824632Seric 	/*
6834632Seric 	**  Convert the work list into canonical form.
6849377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6854632Seric 	*/
6864632Seric 
68724981Seric 	WorkQ = NULL;
68825687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6894632Seric 	{
6904632Seric 		w = (WORK *) xalloc(sizeof *w);
6914632Seric 		w->w_name = wlist[i].w_name;
6924632Seric 		w->w_pri = wlist[i].w_pri;
69325013Seric 		w->w_ctime = wlist[i].w_ctime;
69424981Seric 		w->w_next = WorkQ;
69524981Seric 		WorkQ = w;
6964632Seric 	}
6974632Seric 
6987677Seric 	if (tTd(40, 1))
6994632Seric 	{
7004632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7015037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7024632Seric 	}
70310070Seric 
70410090Seric 	return (wn);
7054632Seric }
7064632Seric /*
7077677Seric **  WORKCMPF -- compare function for ordering work.
7084632Seric **
7094632Seric **	Parameters:
7104632Seric **		a -- the first argument.
7114632Seric **		b -- the second argument.
7124632Seric **
7134632Seric **	Returns:
71424981Seric **		-1 if a < b
71524981Seric **		 0 if a == b
71624981Seric **		+1 if a > b
7174632Seric **
7184632Seric **	Side Effects:
7194632Seric **		none.
7204632Seric */
7214632Seric 
7224632Seric workcmpf(a, b)
7235037Seric 	register WORK *a;
7245037Seric 	register WORK *b;
7254632Seric {
72657438Seric 	long pa = a->w_pri;
72757438Seric 	long pb = b->w_pri;
72824941Seric 
72924941Seric 	if (pa == pb)
7304632Seric 		return (0);
73124941Seric 	else if (pa > pb)
73224981Seric 		return (1);
73324981Seric 	else
73410121Seric 		return (-1);
7354632Seric }
7364632Seric /*
7374632Seric **  DOWORK -- do a work request.
7384632Seric **
7394632Seric **	Parameters:
74058884Seric **		id -- the ID of the job to run.
74158884Seric **		forkflag -- if set, run this in background.
74258924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
74358924Seric **			This is used when expanding aliases in the queue.
74461094Seric **			If forkflag is also set, it doesn't wait for the
74561094Seric **			child.
74658884Seric **		e - the envelope in which to run it.
7474632Seric **
7484632Seric **	Returns:
7494632Seric **		none.
7504632Seric **
7514632Seric **	Side Effects:
7524632Seric **		The work request is satisfied if possible.
7534632Seric */
7544632Seric 
75558924Seric dowork(id, forkflag, requeueflag, e)
75658884Seric 	char *id;
75758884Seric 	bool forkflag;
75858924Seric 	bool requeueflag;
75955012Seric 	register ENVELOPE *e;
7604632Seric {
7614632Seric 	register int i;
76251920Seric 	extern bool readqf();
7634632Seric 
7647677Seric 	if (tTd(40, 1))
76558884Seric 		printf("dowork(%s)\n", id);
7664632Seric 
7674632Seric 	/*
76824941Seric 	**  Fork for work.
76924941Seric 	*/
77024941Seric 
77158884Seric 	if (forkflag)
77224941Seric 	{
77324941Seric 		i = fork();
77424941Seric 		if (i < 0)
77524941Seric 		{
77624941Seric 			syserr("dowork: cannot fork");
77724941Seric 			return;
77824941Seric 		}
77924941Seric 	}
78024941Seric 	else
78124941Seric 	{
78224941Seric 		i = 0;
78324941Seric 	}
78424941Seric 
7854632Seric 	if (i == 0)
7864632Seric 	{
7874632Seric 		/*
7884632Seric 		**  CHILD
7898148Seric 		**	Lock the control file to avoid duplicate deliveries.
7908148Seric 		**		Then run the file as though we had just read it.
7917350Seric 		**	We save an idea of the temporary name so we
7927350Seric 		**		can recover on interrupt.
7934632Seric 		*/
7944632Seric 
7957763Seric 		/* set basic modes, etc. */
7967356Seric 		(void) alarm(0);
79755012Seric 		clearenvelope(e, FALSE);
79858737Seric 		e->e_flags |= EF_QUEUERUN;
79958734Seric 		e->e_errormode = EM_MAIL;
80058884Seric 		e->e_id = id;
80163846Seric 		if (forkflag)
80263846Seric 			disconnect(0, e);
8037876Seric # ifdef LOG
80458020Seric 		if (LogLevel > 76)
80555012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8067881Seric 			       getpid());
80756795Seric # endif /* LOG */
8087763Seric 
8097763Seric 		/* don't use the headers from sendmail.cf... */
81055012Seric 		e->e_header = NULL;
8117763Seric 
81251920Seric 		/* read the queue control file -- return if locked */
81363850Seric 		if (!readqf(e, !requeueflag))
8146980Seric 		{
81558884Seric 			if (tTd(40, 4))
81658884Seric 				printf("readqf(%s) failed\n", e->e_id);
81758884Seric 			if (forkflag)
81824941Seric 				exit(EX_OK);
81924941Seric 			else
82024941Seric 				return;
8216980Seric 		}
8226980Seric 
82355012Seric 		e->e_flags |= EF_INQUEUE;
82458929Seric 		eatheader(e, requeueflag);
8256980Seric 
82658924Seric 		if (requeueflag)
82758924Seric 			queueup(e, TRUE, FALSE);
82858924Seric 
8296980Seric 		/* do the delivery */
83058915Seric 		sendall(e, SM_DELIVER);
8316980Seric 
8326980Seric 		/* finish up and exit */
83358884Seric 		if (forkflag)
83424941Seric 			finis();
83524941Seric 		else
83655012Seric 			dropenvelope(e);
8374632Seric 	}
83861094Seric 	else if (!requeueflag)
83924941Seric 	{
84024941Seric 		/*
84124941Seric 		**  Parent -- pick up results.
84224941Seric 		*/
8434632Seric 
84424941Seric 		errno = 0;
84524941Seric 		(void) waitfor(i);
84624941Seric 	}
8474632Seric }
8484632Seric /*
8494632Seric **  READQF -- read queue file and set up environment.
8504632Seric **
8514632Seric **	Parameters:
8529377Seric **		e -- the envelope of the job to run.
85363850Seric **		announcefile -- if set, announce the name of the queue
85463850Seric **			file in error messages.
8554632Seric **
8564632Seric **	Returns:
85751920Seric **		TRUE if it successfully read the queue file.
85851920Seric **		FALSE otherwise.
8594632Seric **
8604632Seric **	Side Effects:
86151920Seric **		The queue file is returned locked.
8624632Seric */
8634632Seric 
86451920Seric bool
86563850Seric readqf(e, announcefile)
8669377Seric 	register ENVELOPE *e;
86763850Seric 	bool announcefile;
8684632Seric {
86917477Seric 	register FILE *qfp;
87054974Seric 	ADDRESS *ctladdr;
87156400Seric 	struct stat st;
87257135Seric 	char *bp;
87358915Seric 	char qf[20];
87457135Seric 	char buf[MAXLINE];
87524954Seric 	extern long atol();
87654974Seric 	extern ADDRESS *setctluser();
8774632Seric 
8784632Seric 	/*
87917468Seric 	**  Read and process the file.
8804632Seric 	*/
8814632Seric 
88258915Seric 	strcpy(qf, queuename(e, 'q'));
88351937Seric 	qfp = fopen(qf, "r+");
88417477Seric 	if (qfp == NULL)
88517477Seric 	{
88658884Seric 		if (tTd(40, 8))
88758884Seric 			printf("readqf(%s): fopen failure (%s)\n",
88858884Seric 				qf, errstring(errno));
88940934Srick 		if (errno != ENOENT)
89040934Srick 			syserr("readqf: no control file %s", qf);
89151920Seric 		return FALSE;
89217477Seric 	}
89340934Srick 
89456400Seric 	/*
89556400Seric 	**  Check the queue file for plausibility to avoid attacks.
89656400Seric 	*/
89756400Seric 
89856400Seric 	if (fstat(fileno(qfp), &st) < 0)
89956400Seric 	{
90056400Seric 		/* must have been being processed by someone else */
90158884Seric 		if (tTd(40, 8))
90258884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90358884Seric 				qf, errstring(errno));
90456400Seric 		fclose(qfp);
90556400Seric 		return FALSE;
90656400Seric 	}
90756400Seric 
90857135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
90956400Seric 	{
91056400Seric # ifdef LOG
91156400Seric 		if (LogLevel > 0)
91256400Seric 		{
91356400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91456400Seric 				e->e_id, st.st_uid, st.st_mode);
91556400Seric 		}
91656795Seric # endif /* LOG */
91758884Seric 		if (tTd(40, 8))
91858884Seric 			printf("readqf(%s): bogus file\n", qf);
91956400Seric 		fclose(qfp);
92063753Seric 		rename(qf, queuename(e, 'Q'));
92156400Seric 		return FALSE;
92256400Seric 	}
92356400Seric 
92458689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
92540934Srick 	{
92658689Seric 		/* being processed by another queuer */
92758884Seric 		if (tTd(40, 8))
92858884Seric 			printf("readqf(%s): locked\n", qf);
92958689Seric 		if (Verbose)
93058689Seric 			printf("%s: locked\n", e->e_id);
93151920Seric # ifdef LOG
93258689Seric 		if (LogLevel > 19)
93358689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
93456795Seric # endif /* LOG */
93540934Srick 		(void) fclose(qfp);
93651920Seric 		return FALSE;
93740934Srick 	}
93840934Srick 
93959101Seric 	if (st.st_size == 0)
94059101Seric 	{
94159101Seric 		/* must be a bogus file -- just remove it */
94259101Seric 		(void) unlink(qf);
94359101Seric 		fclose(qfp);
94459101Seric 		return FALSE;
94559101Seric 	}
94659101Seric 
94751920Seric 	/* save this lock */
94851920Seric 	e->e_lockfp = qfp;
94951920Seric 
95040934Srick 	/* do basic system initialization */
95155012Seric 	initsys(e);
95240934Srick 
95363850Seric 	if (announcefile)
95463850Seric 		FileName = qf;
9559377Seric 	LineNumber = 0;
95663850Seric 	e->e_flags |= EF_GLOBALERRS;
95763850Seric 	OpMode = MD_DELIVER;
95851920Seric 	if (Verbose)
9599377Seric 		printf("\nRunning %s\n", e->e_id);
96054974Seric 	ctladdr = NULL;
96157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9624632Seric 	{
96358737Seric 		register char *p;
96457529Seric 		struct stat st;
96557529Seric 
96626504Seric 		if (tTd(40, 4))
96757135Seric 			printf("+++++ %s\n", bp);
96857135Seric 		switch (bp[0])
9694632Seric 		{
97040973Sbostic 		  case 'C':		/* specify controlling user */
97157135Seric 			ctladdr = setctluser(&bp[1]);
97240973Sbostic 			break;
97340973Sbostic 
9744632Seric 		  case 'R':		/* specify recipient */
97558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9764632Seric 			break;
9774632Seric 
97825687Seric 		  case 'E':		/* specify error recipient */
97958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
98025687Seric 			break;
98125687Seric 
9824632Seric 		  case 'H':		/* header */
98357135Seric 			(void) chompheader(&bp[1], FALSE, e);
9844632Seric 			break;
9854632Seric 
98610108Seric 		  case 'M':		/* message */
98757135Seric 			e->e_message = newstr(&bp[1]);
98810108Seric 			break;
98910108Seric 
9904632Seric 		  case 'S':		/* sender */
99158704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9924632Seric 			break;
9934632Seric 
99459093Seric 		  case 'B':		/* body type */
99559093Seric 			e->e_bodytype = newstr(&bp[1]);
99659093Seric 			break;
99759093Seric 
9984632Seric 		  case 'D':		/* data file name */
99957135Seric 			e->e_df = newstr(&bp[1]);
10009544Seric 			e->e_dfp = fopen(e->e_df, "r");
10019544Seric 			if (e->e_dfp == NULL)
100258020Seric 			{
10039377Seric 				syserr("readqf: cannot open %s", e->e_df);
100458020Seric 				e->e_msgsize = -1;
100558020Seric 			}
100658020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
100757529Seric 				e->e_msgsize = st.st_size;
10084632Seric 			break;
10094632Seric 
10107860Seric 		  case 'T':		/* init time */
101157135Seric 			e->e_ctime = atol(&bp[1]);
10124632Seric 			break;
10134632Seric 
10144634Seric 		  case 'P':		/* message priority */
101557135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10164634Seric 			break;
10174634Seric 
101858737Seric 		  case 'F':		/* flag bits */
101958737Seric 			for (p = &bp[1]; *p != '\0'; p++)
102058737Seric 			{
102158737Seric 				switch (*p)
102258737Seric 				{
102358737Seric 				  case 'w':	/* warning sent */
102458737Seric 					e->e_flags |= EF_WARNING;
102558737Seric 					break;
102658737Seric 
102758737Seric 				  case 'r':	/* response */
102858737Seric 					e->e_flags |= EF_RESPONSE;
102958737Seric 					break;
103058737Seric 				}
103158737Seric 			}
103258737Seric 			break;
103358737Seric 
103453400Seric 		  case '$':		/* define macro */
103557135Seric 			define(bp[1], newstr(&bp[2]), e);
103653400Seric 			break;
103753400Seric 
103824941Seric 		  case '\0':		/* blank line; ignore */
103924941Seric 			break;
104024941Seric 
10414632Seric 		  default:
104259700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
104357135Seric 				LineNumber, bp);
104463753Seric 			fclose(qfp);
104563753Seric 			rename(qf, queuename(e, 'Q'));
104663753Seric 			return FALSE;
10474632Seric 		}
104857135Seric 
104957135Seric 		if (bp != buf)
105057135Seric 			free(bp);
10514632Seric 	}
10529377Seric 
10539377Seric 	FileName = NULL;
105424941Seric 
105524941Seric 	/*
105624941Seric 	**  If we haven't read any lines, this queue file is empty.
105724941Seric 	**  Arrange to remove it without referencing any null pointers.
105824941Seric 	*/
105924941Seric 
106024941Seric 	if (LineNumber == 0)
106124941Seric 	{
106224941Seric 		errno = 0;
106324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
106424941Seric 	}
106551920Seric 	return TRUE;
10664632Seric }
10674632Seric /*
10689630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10699630Seric **
10709630Seric **	Parameters:
10719630Seric **		none.
10729630Seric **
10739630Seric **	Returns:
10749630Seric **		none.
10759630Seric **
10769630Seric **	Side Effects:
10779630Seric **		Prints a listing of the mail queue on the standard output.
10789630Seric */
10795182Seric 
10809630Seric printqueue()
10819630Seric {
10829630Seric 	register WORK *w;
10839630Seric 	FILE *f;
108410070Seric 	int nrequests;
10859630Seric 	char buf[MAXLINE];
10869630Seric 
10879630Seric 	/*
108858250Seric 	**  Check for permission to print the queue
108958250Seric 	*/
109058250Seric 
109163787Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0)
109258250Seric 	{
109358250Seric 		struct stat st;
109458523Seric # ifdef NGROUPS
109558523Seric 		int n;
1096*63937Seric 		GIDSET_T gidset[NGROUPS];
109758523Seric # endif
109858250Seric 
109958517Seric 		if (stat(QueueDir, &st) < 0)
110058250Seric 		{
110158250Seric 			syserr("Cannot stat %s", QueueDir);
110258250Seric 			return;
110358250Seric 		}
110458523Seric # ifdef NGROUPS
110558523Seric 		n = getgroups(NGROUPS, gidset);
110658523Seric 		while (--n >= 0)
110758523Seric 		{
110858523Seric 			if (gidset[n] == st.st_gid)
110958523Seric 				break;
111058523Seric 		}
111158523Seric 		if (n < 0)
111258523Seric # else
111363787Seric 		if (RealGid != st.st_gid)
111458523Seric # endif
111558250Seric 		{
111658250Seric 			usrerr("510 You are not permitted to see the queue");
111758250Seric 			setstat(EX_NOPERM);
111858250Seric 			return;
111958250Seric 		}
112058250Seric 	}
112158250Seric 
112258250Seric 	/*
11239630Seric 	**  Read and order the queue.
11249630Seric 	*/
11259630Seric 
112624941Seric 	nrequests = orderq(TRUE);
11279630Seric 
11289630Seric 	/*
11299630Seric 	**  Print the work list that we have read.
11309630Seric 	*/
11319630Seric 
11329630Seric 	/* first see if there is anything */
113310070Seric 	if (nrequests <= 0)
11349630Seric 	{
113510070Seric 		printf("Mail queue is empty\n");
11369630Seric 		return;
11379630Seric 	}
11389630Seric 
113951920Seric 	CurrentLA = getla();	/* get load average */
114040934Srick 
114110096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
114225687Seric 	if (nrequests > QUEUESIZE)
114325687Seric 		printf(", only %d printed", QUEUESIZE);
114424979Seric 	if (Verbose)
114558716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114624979Seric 	else
114758716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11489630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11499630Seric 	{
11509630Seric 		struct stat st;
115110070Seric 		auto time_t submittime = 0;
115210070Seric 		long dfsize = -1;
115358737Seric 		int flags = 0;
115410108Seric 		char message[MAXLINE];
115559093Seric 		char bodytype[MAXNAME];
11569630Seric 
115717468Seric 		f = fopen(w->w_name, "r");
115817468Seric 		if (f == NULL)
115917468Seric 		{
116017468Seric 			errno = 0;
116117468Seric 			continue;
116217468Seric 		}
116358724Seric 		printf("%8s", w->w_name + 2);
116458689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
116510070Seric 			printf("*");
116657438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
116724941Seric 			printf("X");
116810070Seric 		else
116910070Seric 			printf(" ");
117010070Seric 		errno = 0;
117117468Seric 
117259093Seric 		message[0] = bodytype[0] = '\0';
11739630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11749630Seric 		{
117553400Seric 			register int i;
117658737Seric 			register char *p;
117753400Seric 
11789630Seric 			fixcrlf(buf, TRUE);
11799630Seric 			switch (buf[0])
11809630Seric 			{
118110108Seric 			  case 'M':	/* error message */
118253400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
118358737Seric 					i = sizeof message - 1;
118453400Seric 				bcopy(&buf[1], message, i);
118553400Seric 				message[i] = '\0';
118610108Seric 				break;
118710108Seric 
118859093Seric 			  case 'B':	/* body type */
118959093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
119059093Seric 					i = sizeof bodytype - 1;
119159093Seric 				bcopy(&buf[1], bodytype, i);
119259093Seric 				bodytype[i] = '\0';
119359093Seric 				break;
119459093Seric 
11959630Seric 			  case 'S':	/* sender name */
119624979Seric 				if (Verbose)
119758737Seric 					printf("%8ld %10ld%c%.12s %.38s",
119858737Seric 					    dfsize,
119958737Seric 					    w->w_pri,
120058737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
120158737Seric 					    ctime(&submittime) + 4,
120224979Seric 					    &buf[1]);
120324979Seric 				else
120424979Seric 					printf("%8ld %.16s %.45s", dfsize,
120524979Seric 					    ctime(&submittime), &buf[1]);
120659093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
120759093Seric 				{
120859093Seric 					printf("\n    %10.10s", bodytype);
120959093Seric 					if (message[0] != '\0')
121059093Seric 						printf("   (%.60s)", message);
121159093Seric 				}
12129630Seric 				break;
121351920Seric 
121440973Sbostic 			  case 'C':	/* controlling user */
121554974Seric 				if (Verbose)
121658716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
121758716Seric 						&buf[1]);
121840973Sbostic 				break;
12199630Seric 
12209630Seric 			  case 'R':	/* recipient name */
122124979Seric 				if (Verbose)
122258716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
122324979Seric 				else
122458716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12259630Seric 				break;
12269630Seric 
12279630Seric 			  case 'T':	/* creation time */
122824941Seric 				submittime = atol(&buf[1]);
12299630Seric 				break;
123010070Seric 
123110070Seric 			  case 'D':	/* data file name */
123210070Seric 				if (stat(&buf[1], &st) >= 0)
123310070Seric 					dfsize = st.st_size;
123410070Seric 				break;
123558737Seric 
123658737Seric 			  case 'F':	/* flag bits */
123758737Seric 				for (p = &buf[1]; *p != '\0'; p++)
123858737Seric 				{
123958737Seric 					switch (*p)
124058737Seric 					{
124158737Seric 					  case 'w':
124258737Seric 						flags |= EF_WARNING;
124358737Seric 						break;
124458737Seric 					}
124558737Seric 				}
12469630Seric 			}
12479630Seric 		}
124810070Seric 		if (submittime == (time_t) 0)
124910070Seric 			printf(" (no control file)");
12509630Seric 		printf("\n");
125123098Seric 		(void) fclose(f);
12529630Seric 	}
12539630Seric }
12549630Seric 
125556795Seric # endif /* QUEUE */
125617468Seric /*
125717468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
125817468Seric **
125917468Seric **	Assigns an id code if one does not already exist.
126017468Seric **	This code is very careful to avoid trashing existing files
126117468Seric **	under any circumstances.
126217468Seric **
126317468Seric **	Parameters:
126417468Seric **		e -- envelope to build it in/from.
126517468Seric **		type -- the file type, used as the first character
126617468Seric **			of the file name.
126717468Seric **
126817468Seric **	Returns:
126917468Seric **		a pointer to the new file name (in a static buffer).
127017468Seric **
127117468Seric **	Side Effects:
127251920Seric **		If no id code is already assigned, queuename will
127351920Seric **		assign an id code, create a qf file, and leave a
127451920Seric **		locked, open-for-write file pointer in the envelope.
127517468Seric */
127617468Seric 
127717468Seric char *
127817468Seric queuename(e, type)
127917468Seric 	register ENVELOPE *e;
128059700Seric 	int type;
128117468Seric {
128217468Seric 	static int pid = -1;
128358741Seric 	static char c0;
128458741Seric 	static char c1;
128558741Seric 	static char c2;
128658716Seric 	time_t now;
128758716Seric 	struct tm *tm;
128858689Seric 	static char buf[MAXNAME];
128917468Seric 
129017468Seric 	if (e->e_id == NULL)
129117468Seric 	{
129217468Seric 		char qf[20];
129317468Seric 
129417468Seric 		/* find a unique id */
129517468Seric 		if (pid != getpid())
129617468Seric 		{
129717468Seric 			/* new process -- start back at "AA" */
129817468Seric 			pid = getpid();
129958716Seric 			now = curtime();
130058716Seric 			tm = localtime(&now);
130158716Seric 			c0 = 'A' + tm->tm_hour;
130217468Seric 			c1 = 'A';
130317468Seric 			c2 = 'A' - 1;
130417468Seric 		}
130558716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130617468Seric 
130717468Seric 		while (c1 < '~' || c2 < 'Z')
130817468Seric 		{
130917468Seric 			int i;
131017468Seric 
131117468Seric 			if (c2 >= 'Z')
131217468Seric 			{
131317468Seric 				c1++;
131417468Seric 				c2 = 'A' - 1;
131517468Seric 			}
131658716Seric 			qf[3] = c1;
131758716Seric 			qf[4] = ++c2;
131817468Seric 			if (tTd(7, 20))
131940934Srick 				printf("queuename: trying \"%s\"\n", qf);
132017468Seric 
132140934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
132251920Seric 			if (i < 0)
132351920Seric 			{
132451920Seric 				if (errno == EEXIST)
132551920Seric 					continue;
132651920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132751920Seric 					qf, QueueDir);
132851920Seric 				exit(EX_UNAVAILABLE);
132951920Seric 			}
133058689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
133151920Seric 			{
133251920Seric 				e->e_lockfp = fdopen(i, "w");
133340934Srick 				break;
133417468Seric 			}
133551920Seric 
133651920Seric 			/* a reader got the file; abandon it and try again */
133751920Seric 			(void) close(i);
133817468Seric 		}
133917468Seric 		if (c1 >= '~' && c2 >= 'Z')
134017468Seric 		{
134117468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
134217468Seric 				qf, QueueDir);
134317468Seric 			exit(EX_OSERR);
134417468Seric 		}
134517468Seric 		e->e_id = newstr(&qf[2]);
134617468Seric 		define('i', e->e_id, e);
134717468Seric 		if (tTd(7, 1))
134817468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
134917468Seric # ifdef LOG
135058020Seric 		if (LogLevel > 93)
135117468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
135256795Seric # endif /* LOG */
135317468Seric 	}
135417468Seric 
135517468Seric 	if (type == '\0')
135617468Seric 		return (NULL);
135717468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
135817468Seric 	if (tTd(7, 2))
135917468Seric 		printf("queuename: %s\n", buf);
136017468Seric 	return (buf);
136117468Seric }
136217468Seric /*
136317468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
136417468Seric **
136517468Seric **	Parameters:
136617468Seric **		e -- the envelope to unlock.
136717468Seric **
136817468Seric **	Returns:
136917468Seric **		none
137017468Seric **
137117468Seric **	Side Effects:
137217468Seric **		unlocks the queue for `e'.
137317468Seric */
137417468Seric 
137517468Seric unlockqueue(e)
137617468Seric 	ENVELOPE *e;
137717468Seric {
137858680Seric 	if (tTd(51, 4))
137958680Seric 		printf("unlockqueue(%s)\n", e->e_id);
138058680Seric 
138151920Seric 	/* if there is a lock file in the envelope, close it */
138251920Seric 	if (e->e_lockfp != NULL)
138358680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
138451920Seric 	e->e_lockfp = NULL;
138551920Seric 
138658728Seric 	/* don't create a queue id if we don't already have one */
138758728Seric 	if (e->e_id == NULL)
138858728Seric 		return;
138958728Seric 
139017468Seric 	/* remove the transcript */
139117468Seric # ifdef LOG
139258020Seric 	if (LogLevel > 87)
139317468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
139456795Seric # endif /* LOG */
139558680Seric 	if (!tTd(51, 104))
139617468Seric 		xunlink(queuename(e, 'x'));
139717468Seric 
139817468Seric }
139940973Sbostic /*
140054974Seric **  SETCTLUSER -- create a controlling address
140140973Sbostic **
140254974Seric **	Create a fake "address" given only a local login name; this is
140354974Seric **	used as a "controlling user" for future recipient addresses.
140440973Sbostic **
140540973Sbostic **	Parameters:
140654974Seric **		user -- the user name of the controlling user.
140740973Sbostic **
140840973Sbostic **	Returns:
140954974Seric **		An address descriptor for the controlling user.
141040973Sbostic **
141140973Sbostic **	Side Effects:
141240973Sbostic **		none.
141340973Sbostic */
141440973Sbostic 
141554974Seric ADDRESS *
141654974Seric setctluser(user)
141754974Seric 	char *user;
141840973Sbostic {
141954974Seric 	register ADDRESS *a;
142040973Sbostic 	struct passwd *pw;
142159113Seric 	char *p;
142240973Sbostic 
142340973Sbostic 	/*
142454974Seric 	**  See if this clears our concept of controlling user.
142540973Sbostic 	*/
142640973Sbostic 
142763850Seric 	if (user == NULL || *user == '\0')
142863850Seric 		return NULL;
142940973Sbostic 
143040973Sbostic 	/*
143154974Seric 	**  Set up addr fields for controlling user.
143240973Sbostic 	*/
143340973Sbostic 
143454974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143554974Seric 	bzero((char *) a, sizeof *a);
143659113Seric 
143759113Seric 	p = strchr(user, ':');
143859113Seric 	if (p != NULL)
143959113Seric 		*p++ = '\0';
144059270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
144140973Sbostic 	{
144240973Sbostic 		a->q_home = newstr(pw->pw_dir);
144340973Sbostic 		a->q_uid = pw->pw_uid;
144440973Sbostic 		a->q_gid = pw->pw_gid;
144557642Seric 		a->q_user = newstr(user);
144659270Seric 		a->q_flags |= QGOODUID;
144740973Sbostic 	}
144840973Sbostic 	else
144940973Sbostic 	{
145057642Seric 		a->q_user = newstr(DefUser);
145140973Sbostic 	}
145240973Sbostic 
145359270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
145456328Seric 	a->q_mailer = LocalMailer;
145559113Seric 	if (p == NULL)
145659113Seric 		a->q_paddr = a->q_user;
145759113Seric 	else
145859113Seric 		a->q_paddr = newstr(p);
145954974Seric 	return a;
146040973Sbostic }
1461