xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 59730)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*59730Seric static char sccsid[] = "@(#)queue.c	6.56 (Berkeley) 05/04/93 (with queueing)";
1433731Sbostic #else
15*59730Seric static char sccsid[] = "@(#)queue.c	6.56 (Berkeley) 05/04/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
1913707Ssam # include <sys/dir.h>
204634Seric # include <signal.h>
214632Seric # include <errno.h>
2240973Sbostic # include <pwd.h>
2357977Seric # ifndef MAXNAMLEN
2457736Seric # include <dirent.h>
2557736Seric # endif
264632Seric 
2733731Sbostic # ifdef QUEUE
284632Seric 
294632Seric /*
309377Seric **  Work queue.
319377Seric */
329377Seric 
339377Seric struct work
349377Seric {
359377Seric 	char		*w_name;	/* name of control file */
369377Seric 	long		w_pri;		/* priority of message, see below */
3725013Seric 	time_t		w_ctime;	/* creation time of message */
389377Seric 	struct work	*w_next;	/* next in queue */
399377Seric };
409377Seric 
419377Seric typedef struct work	WORK;
429377Seric 
439377Seric WORK	*WorkQ;			/* queue of things to be done */
449377Seric /*
454632Seric **  QUEUEUP -- queue a message up for future transmission.
464632Seric **
474632Seric **	Parameters:
486980Seric **		e -- the envelope to queue up.
496999Seric **		queueall -- if TRUE, queue all addresses, rather than
506999Seric **			just those with the QQUEUEUP flag set.
519377Seric **		announce -- if TRUE, tell when you are queueing up.
524632Seric **
534632Seric **	Returns:
5451920Seric **		none.
554632Seric **
564632Seric **	Side Effects:
579377Seric **		The current request are saved in a control file.
5851920Seric **		The queue file is left locked.
594632Seric */
604632Seric 
619377Seric queueup(e, queueall, announce)
626980Seric 	register ENVELOPE *e;
636999Seric 	bool queueall;
649377Seric 	bool announce;
654632Seric {
667812Seric 	char *qf;
677812Seric 	register FILE *tfp;
684632Seric 	register HDR *h;
695007Seric 	register ADDRESS *q;
7051920Seric 	int fd;
7151920Seric 	int i;
7251920Seric 	bool newid;
7353400Seric 	register char *p;
7410173Seric 	MAILER nullmailer;
7551920Seric 	char buf[MAXLINE], tf[MAXLINE];
7653400Seric 	extern char *macvalue();
774632Seric 
785037Seric 	/*
7917477Seric 	**  Create control file.
805037Seric 	*/
814632Seric 
8251920Seric 	newid = (e->e_id == NULL);
8351920Seric 	strcpy(tf, queuename(e, 't'));
8451920Seric 	tfp = e->e_lockfp;
8551920Seric 	if (tfp == NULL)
8651920Seric 		newid = FALSE;
8751920Seric 	if (newid)
8851835Seric 	{
8951920Seric 		tfp = e->e_lockfp;
9051920Seric 	}
9151920Seric 	else
9251920Seric 	{
9351920Seric 		/* get a locked tf file */
9451920Seric 		for (i = 100; --i >= 0; )
9551835Seric 		{
9658689Seric 			extern bool lockfile();
9751937Seric 
9851920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9951920Seric 			if (fd < 0)
10051835Seric 			{
10151920Seric 				if (errno == EEXIST)
10251920Seric 					continue;
10358801Seric notemp:
10458690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10541636Srick 			}
10658689Seric 
10758689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10851920Seric 				break;
10958689Seric 
11051920Seric 			close(fd);
11158801Seric 			sleep(i);
11241636Srick 		}
11358801Seric 		if (fd < 0)
11458801Seric 			goto notemp;
11541636Srick 
11651920Seric 		tfp = fdopen(fd, "w");
11751920Seric 	}
1184632Seric 
1197677Seric 	if (tTd(40, 1))
12017468Seric 		printf("queueing %s\n", e->e_id);
1214632Seric 
1224632Seric 	/*
1236980Seric 	**  If there is no data file yet, create one.
1246980Seric 	*/
1256980Seric 
1266980Seric 	if (e->e_df == NULL)
1276980Seric 	{
1286980Seric 		register FILE *dfp;
1299389Seric 		extern putbody();
1306980Seric 
1317812Seric 		e->e_df = newstr(queuename(e, 'd'));
13240934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13340934Srick 		if (fd < 0)
13458690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13540934Srick 		dfp = fdopen(fd, "w");
136*59730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
13758680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1389389Seric 		e->e_putbody = putbody;
1396980Seric 	}
1406980Seric 
1416980Seric 	/*
1424632Seric 	**  Output future work requests.
14325687Seric 	**	Priority and creation time should be first, since
14425687Seric 	**	they are required by orderq.
1454632Seric 	*/
1464632Seric 
1479377Seric 	/* output message priority */
1489377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1499377Seric 
1509630Seric 	/* output creation time */
1519630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1529630Seric 
15359093Seric 	/* output type and name of data file */
15459093Seric 	if (e->e_bodytype != NULL)
15559093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1567812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1574632Seric 
15810108Seric 	/* message from envelope, if it exists */
15910108Seric 	if (e->e_message != NULL)
16010108Seric 		fprintf(tfp, "M%s\n", e->e_message);
16110108Seric 
16258737Seric 	/* send various flag bits through */
16358737Seric 	p = buf;
16458737Seric 	if (bitset(EF_WARNING, e->e_flags))
16558737Seric 		*p++ = 'w';
16658737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16758737Seric 		*p++ = 'r';
16858737Seric 	*p++ = '\0';
16958737Seric 	if (buf[0] != '\0')
17058737Seric 		fprintf(tfp, "F%s\n", buf);
17158737Seric 
17258957Seric 	/* $r and $s and $_ macro values */
17353400Seric 	if ((p = macvalue('r', e)) != NULL)
17453400Seric 		fprintf(tfp, "$r%s\n", p);
17553400Seric 	if ((p = macvalue('s', e)) != NULL)
17653400Seric 		fprintf(tfp, "$s%s\n", p);
17758957Seric 	if ((p = macvalue('_', e)) != NULL)
17858957Seric 		fprintf(tfp, "$_%s\n", p);
17953400Seric 
1804632Seric 	/* output name of sender */
1817812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1824632Seric 
18355360Seric 	/* output list of error recipients */
18459670Seric 	printctladdr(NULL, NULL);
18555360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18655360Seric 	{
18758680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18855360Seric 		{
18959113Seric 			printctladdr(q, tfp);
19055360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
19155360Seric 		}
19255360Seric 	}
19355360Seric 
1944632Seric 	/* output list of recipient addresses */
1956980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1964632Seric 	{
19758250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19858680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1998245Seric 		{
20059113Seric 			printctladdr(q, tfp);
2017812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2029377Seric 			if (announce)
2039377Seric 			{
2049377Seric 				e->e_to = q->q_paddr;
20558151Seric 				message("queued");
20658020Seric 				if (LogLevel > 8)
20758337Seric 					logdelivery(NULL, NULL, "queued", e);
2089377Seric 				e->e_to = NULL;
2099377Seric 			}
2109387Seric 			if (tTd(40, 1))
2119387Seric 			{
2129387Seric 				printf("queueing ");
2139387Seric 				printaddr(q, FALSE);
2149387Seric 			}
2158245Seric 		}
2164632Seric 	}
2174632Seric 
2189377Seric 	/*
2199377Seric 	**  Output headers for this message.
2209377Seric 	**	Expand macros completely here.  Queue run will deal with
2219377Seric 	**	everything as absolute headers.
2229377Seric 	**		All headers that must be relative to the recipient
2239377Seric 	**		can be cracked later.
22410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
22510173Seric 	**	no effect on the addresses as they are output.
2269377Seric 	*/
2279377Seric 
22810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
23058020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
23110349Seric 	nullmailer.m_eol = "\n";
23210173Seric 
23358050Seric 	define('g', "\201f", e);
2346980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2354632Seric 	{
23610686Seric 		extern bool bitzerop();
23710686Seric 
23812015Seric 		/* don't output null headers */
2394632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2404632Seric 			continue;
24112015Seric 
24212015Seric 		/* don't output resent headers on non-resent messages */
24312015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
24412015Seric 			continue;
24512015Seric 
24612015Seric 		/* output this header */
2477812Seric 		fprintf(tfp, "H");
24812015Seric 
24912015Seric 		/* if conditional, output the set of conditions */
25010686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
25110686Seric 		{
25210686Seric 			int j;
25310686Seric 
25423098Seric 			(void) putc('?', tfp);
25510686Seric 			for (j = '\0'; j <= '\177'; j++)
25610686Seric 				if (bitnset(j, h->h_mflags))
25723098Seric 					(void) putc(j, tfp);
25823098Seric 			(void) putc('?', tfp);
25910686Seric 		}
26012015Seric 
26112015Seric 		/* output the header: expand macros, convert addresses */
2627763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2637763Seric 		{
2647763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2658236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2667763Seric 		}
2678245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2689348Seric 		{
26958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
27058737Seric 
27158737Seric 			if (bitset(H_FROM, h->h_flags))
27258737Seric 				oldstyle = FALSE;
27358737Seric 
27458737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27555012Seric 				 &nullmailer, e);
2769348Seric 		}
2777763Seric 		else
2788245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2794632Seric 	}
2804632Seric 
2814632Seric 	/*
2824632Seric 	**  Clean up.
2834632Seric 	*/
2844632Seric 
28558732Seric 	fflush(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 */
31240934Srick 	fflush(tfp);
31351920Seric 	return;
3144632Seric }
31554974Seric 
31654974Seric printctladdr(a, tfp)
31759113Seric 	register ADDRESS *a;
31854974Seric 	FILE *tfp;
31954974Seric {
32059113Seric 	char *uname;
32159113Seric 	register struct passwd *pw;
32259113Seric 	register ADDRESS *q;
32359113Seric 	uid_t uid;
32459113Seric 	static ADDRESS *lastctladdr;
32559113Seric 	static uid_t lastuid;
32659113Seric 	extern ADDRESS *getctladdr();
32754974Seric 
32859113Seric 	/* initialization */
32959670Seric 	if (a == NULL || tfp == NULL)
33054974Seric 	{
33159670Seric 		if (lastctladdr != NULL && tfp != NULL)
33259113Seric 			fprintf(tfp, "C\n");
33359113Seric 		lastctladdr = NULL;
33459113Seric 		lastuid = 0;
33554974Seric 		return;
33654974Seric 	}
33759113Seric 
33859113Seric 	/* find the active uid */
33959113Seric 	q = getctladdr(a);
34059113Seric 	if (q == NULL)
34159113Seric 		uid = 0;
34254974Seric 	else
34359113Seric 		uid = q->q_uid;
34459113Seric 
34559113Seric 	/* if a is an alias, use that for printing */
34659113Seric 	if (a->q_alias != NULL)
34759113Seric 		a = a->q_alias;
34859113Seric 
34959113Seric 	/* check to see if this is the same as last time */
35059113Seric 	if (lastctladdr != NULL && uid == lastuid &&
35159113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
35259113Seric 		return;
35359113Seric 	lastuid = uid;
35459113Seric 	lastctladdr = a;
35559113Seric 
35659113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35759270Seric 		uname = "";
35859113Seric 	else
35959113Seric 		uname = pw->pw_name;
36059113Seric 
36159113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
36254974Seric }
36354974Seric 
3644632Seric /*
3654632Seric **  RUNQUEUE -- run the jobs in the queue.
3664632Seric **
3674632Seric **	Gets the stuff out of the queue in some presumably logical
3684632Seric **	order and processes them.
3694632Seric **
3704632Seric **	Parameters:
37124941Seric **		forkflag -- TRUE if the queue scanning should be done in
37224941Seric **			a child process.  We double-fork so it is not our
37324941Seric **			child and we don't have to clean up after it.
3744632Seric **
3754632Seric **	Returns:
3764632Seric **		none.
3774632Seric **
3784632Seric **	Side Effects:
3794632Seric **		runs things in the mail queue.
3804632Seric */
3814632Seric 
38255360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
38355360Seric 
38455360Seric runqueue(forkflag)
3854639Seric 	bool forkflag;
3864632Seric {
38724953Seric 	extern bool shouldqueue();
38855360Seric 	register ENVELOPE *e;
38955360Seric 	extern ENVELOPE BlankEnvelope;
39055360Seric 	extern ENVELOPE *newenvelope();
39124953Seric 
3927466Seric 	/*
39324953Seric 	**  If no work will ever be selected, don't even bother reading
39424953Seric 	**  the queue.
39524953Seric 	*/
39624953Seric 
39751920Seric 	CurrentLA = getla();	/* get load average */
39840934Srick 
39958132Seric 	if (shouldqueue(0L, curtime()))
40024953Seric 	{
40124953Seric 		if (Verbose)
40224953Seric 			printf("Skipping queue run -- load average too high\n");
40358107Seric 		if (forkflag && QueueIntvl != 0)
40458107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
40555360Seric 		return;
40624953Seric 	}
40724953Seric 
40824953Seric 	/*
4097466Seric 	**  See if we want to go off and do other useful work.
4107466Seric 	*/
4114639Seric 
4124639Seric 	if (forkflag)
4134639Seric 	{
4147943Seric 		int pid;
4157943Seric 
4167943Seric 		pid = dofork();
4177943Seric 		if (pid != 0)
4184639Seric 		{
41946928Sbostic 			extern void reapchild();
42025184Seric 
4217943Seric 			/* parent -- pick up intermediate zombie */
42225184Seric #ifndef SIGCHLD
4239377Seric 			(void) waitfor(pid);
42456795Seric #else /* SIGCHLD */
42525184Seric 			(void) signal(SIGCHLD, reapchild);
42656795Seric #endif /* SIGCHLD */
4277690Seric 			if (QueueIntvl != 0)
4289348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4294639Seric 			return;
4304639Seric 		}
4317943Seric 		/* child -- double fork */
43225184Seric #ifndef SIGCHLD
4337943Seric 		if (fork() != 0)
4347943Seric 			exit(EX_OK);
43556795Seric #else /* SIGCHLD */
43625184Seric 		(void) signal(SIGCHLD, SIG_DFL);
43756795Seric #endif /* SIGCHLD */
4384639Seric 	}
43924941Seric 
44040934Srick 	setproctitle("running queue: %s", QueueDir);
44124941Seric 
4427876Seric # ifdef LOG
44358020Seric 	if (LogLevel > 69)
44455360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
44555360Seric 			QueueDir, getpid(), forkflag);
44656795Seric # endif /* LOG */
4474639Seric 
4487466Seric 	/*
44910205Seric 	**  Release any resources used by the daemon code.
45010205Seric 	*/
45110205Seric 
45210205Seric # ifdef DAEMON
45310205Seric 	clrdaemon();
45456795Seric # endif /* DAEMON */
45510205Seric 
45610205Seric 	/*
45755360Seric 	**  Create ourselves an envelope
45855360Seric 	*/
45955360Seric 
46055360Seric 	CurEnv = &QueueEnvelope;
46158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
46255360Seric 	e->e_flags = BlankEnvelope.e_flags;
46355360Seric 
46455360Seric 	/*
46527175Seric 	**  Make sure the alias database is open.
46627175Seric 	*/
46727175Seric 
46859670Seric 	initaliases(FALSE, e);
46927175Seric 
47027175Seric 	/*
4717466Seric 	**  Start making passes through the queue.
4727466Seric 	**	First, read and sort the entire queue.
4737466Seric 	**	Then, process the work in that order.
4747466Seric 	**		But if you take too long, start over.
4757466Seric 	*/
4767466Seric 
4777943Seric 	/* order the existing work requests */
47824954Seric 	(void) orderq(FALSE);
4797690Seric 
4807943Seric 	/* process them once at a time */
4817943Seric 	while (WorkQ != NULL)
4824639Seric 	{
4837943Seric 		WORK *w = WorkQ;
48458884Seric 		extern bool shouldqueue();
4857881Seric 
4867943Seric 		WorkQ = WorkQ->w_next;
48758884Seric 
48858884Seric 		/*
48958884Seric 		**  Ignore jobs that are too expensive for the moment.
49058884Seric 		*/
49158884Seric 
49258884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
49358884Seric 		{
49458884Seric 			if (Verbose)
49558884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
49658884Seric 		}
49758930Seric 		else
49858930Seric 		{
49958930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
50058930Seric 		}
5017943Seric 		free(w->w_name);
5027943Seric 		free((char *) w);
5034639Seric 	}
50429866Seric 
50529866Seric 	/* exit without the usual cleanup */
50655467Seric 	e->e_id = NULL;
50755467Seric 	finis();
5084634Seric }
5094634Seric /*
5104632Seric **  ORDERQ -- order the work queue.
5114632Seric **
5124632Seric **	Parameters:
51324941Seric **		doall -- if set, include everything in the queue (even
51424941Seric **			the jobs that cannot be run because the load
51524941Seric **			average is too high).  Otherwise, exclude those
51624941Seric **			jobs.
5174632Seric **
5184632Seric **	Returns:
51910121Seric **		The number of request in the queue (not necessarily
52010121Seric **		the number of requests in WorkQ however).
5214632Seric **
5224632Seric **	Side Effects:
5234632Seric **		Sets WorkQ to the queue of available work, in order.
5244632Seric */
5254632Seric 
52625687Seric # define NEED_P		001
52725687Seric # define NEED_T		002
52858318Seric # define NEED_R		004
52958318Seric # define NEED_S		010
5304632Seric 
53124941Seric orderq(doall)
53224941Seric 	bool doall;
5334632Seric {
5346625Sglickman 	register struct direct *d;
5354632Seric 	register WORK *w;
5366625Sglickman 	DIR *f;
5374632Seric 	register int i;
53825687Seric 	WORK wlist[QUEUESIZE+1];
53910070Seric 	int wn = -1;
5404632Seric 	extern workcmpf();
5414632Seric 
54258318Seric 	if (tTd(41, 1))
54358318Seric 	{
54458318Seric 		printf("orderq:\n");
54558318Seric 		if (QueueLimitId != NULL)
54658318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
54758318Seric 		if (QueueLimitSender != NULL)
54858318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
54958318Seric 		if (QueueLimitRecipient != NULL)
55058318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
55158318Seric 	}
55258318Seric 
5534632Seric 	/* clear out old WorkQ */
5544632Seric 	for (w = WorkQ; w != NULL; )
5554632Seric 	{
5564632Seric 		register WORK *nw = w->w_next;
5574632Seric 
5584632Seric 		WorkQ = nw;
5594632Seric 		free(w->w_name);
5604632Seric 		free((char *) w);
5614632Seric 		w = nw;
5624632Seric 	}
5634632Seric 
5644632Seric 	/* open the queue directory */
5658148Seric 	f = opendir(".");
5664632Seric 	if (f == NULL)
5674632Seric 	{
5688148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
56910070Seric 		return (0);
5704632Seric 	}
5714632Seric 
5724632Seric 	/*
5734632Seric 	**  Read the work directory.
5744632Seric 	*/
5754632Seric 
57610070Seric 	while ((d = readdir(f)) != NULL)
5774632Seric 	{
5789377Seric 		FILE *cf;
5794632Seric 		char lbuf[MAXNAME];
58057642Seric 		extern bool shouldqueue();
58158318Seric 		extern bool strcontainedin();
5824632Seric 
5834632Seric 		/* is this an interesting entry? */
5847812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5854632Seric 			continue;
5864632Seric 
58758318Seric 		if (QueueLimitId != NULL &&
58858318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
58958318Seric 			continue;
59058318Seric 
59158722Seric 		/*
59258722Seric 		**  Check queue name for plausibility.  This handles
59358722Seric 		**  both old and new type ids.
59458722Seric 		*/
59558722Seric 
59658722Seric 		i = strlen(d->d_name);
59758722Seric 		if (i != 9 && i != 10)
59858020Seric 		{
59958020Seric 			if (Verbose)
60058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
60158020Seric #ifdef LOG
60258020Seric 			if (LogLevel > 3)
60359615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
60458020Seric 					d->d_name);
60558020Seric #endif
60658020Seric 			if (strlen(d->d_name) >= MAXNAME)
60758020Seric 				d->d_name[MAXNAME - 1] = '\0';
60858020Seric 			strcpy(lbuf, d->d_name);
60958020Seric 			lbuf[0] = 'Q';
61058020Seric 			(void) rename(d->d_name, lbuf);
61158020Seric 			continue;
61258020Seric 		}
61358020Seric 
61410070Seric 		/* yes -- open control file (if not too many files) */
61525687Seric 		if (++wn >= QUEUESIZE)
61610070Seric 			continue;
61758318Seric 
6188148Seric 		cf = fopen(d->d_name, "r");
6194632Seric 		if (cf == NULL)
6204632Seric 		{
6217055Seric 			/* this may be some random person sending hir msgs */
6227055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
62310090Seric 			if (tTd(41, 2))
62410090Seric 				printf("orderq: cannot open %s (%d)\n",
62510090Seric 					d->d_name, errno);
6267055Seric 			errno = 0;
62710090Seric 			wn--;
6284632Seric 			continue;
6294632Seric 		}
63025687Seric 		w = &wlist[wn];
63125687Seric 		w->w_name = newstr(d->d_name);
6324632Seric 
63325027Seric 		/* make sure jobs in creation don't clog queue */
63425687Seric 		w->w_pri = 0x7fffffff;
63525687Seric 		w->w_ctime = 0;
63625027Seric 
6374632Seric 		/* extract useful information */
63825687Seric 		i = NEED_P | NEED_T;
63958318Seric 		if (QueueLimitSender != NULL)
64058318Seric 			i |= NEED_S;
64158318Seric 		if (QueueLimitRecipient != NULL)
64258318Seric 			i |= NEED_R;
64325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6444632Seric 		{
64524954Seric 			extern long atol();
64658318Seric 			extern bool strcontainedin();
64724954Seric 
64824941Seric 			switch (lbuf[0])
6494632Seric 			{
65024941Seric 			  case 'P':
65125687Seric 				w->w_pri = atol(&lbuf[1]);
65225687Seric 				i &= ~NEED_P;
6534632Seric 				break;
65425013Seric 
65525013Seric 			  case 'T':
65625687Seric 				w->w_ctime = atol(&lbuf[1]);
65725687Seric 				i &= ~NEED_T;
65825013Seric 				break;
65958318Seric 
66058318Seric 			  case 'R':
66158318Seric 				if (QueueLimitRecipient != NULL &&
66258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
66358318Seric 					i &= ~NEED_R;
66458318Seric 				break;
66558318Seric 
66658318Seric 			  case 'S':
66758318Seric 				if (QueueLimitSender != NULL &&
66858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
66958318Seric 					i &= ~NEED_S;
67058318Seric 				break;
6714632Seric 			}
6724632Seric 		}
6734632Seric 		(void) fclose(cf);
67424953Seric 
67558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
67658318Seric 		    bitset(NEED_R|NEED_S, i))
67724953Seric 		{
67824953Seric 			/* don't even bother sorting this job in */
67924953Seric 			wn--;
68024953Seric 		}
6814632Seric 	}
6826625Sglickman 	(void) closedir(f);
68310090Seric 	wn++;
6844632Seric 
6854632Seric 	/*
6864632Seric 	**  Sort the work directory.
6874632Seric 	*/
6884632Seric 
68925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6904632Seric 
6914632Seric 	/*
6924632Seric 	**  Convert the work list into canonical form.
6939377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6944632Seric 	*/
6954632Seric 
69624981Seric 	WorkQ = NULL;
69725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6984632Seric 	{
6994632Seric 		w = (WORK *) xalloc(sizeof *w);
7004632Seric 		w->w_name = wlist[i].w_name;
7014632Seric 		w->w_pri = wlist[i].w_pri;
70225013Seric 		w->w_ctime = wlist[i].w_ctime;
70324981Seric 		w->w_next = WorkQ;
70424981Seric 		WorkQ = w;
7054632Seric 	}
7064632Seric 
7077677Seric 	if (tTd(40, 1))
7084632Seric 	{
7094632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7105037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7114632Seric 	}
71210070Seric 
71310090Seric 	return (wn);
7144632Seric }
7154632Seric /*
7167677Seric **  WORKCMPF -- compare function for ordering work.
7174632Seric **
7184632Seric **	Parameters:
7194632Seric **		a -- the first argument.
7204632Seric **		b -- the second argument.
7214632Seric **
7224632Seric **	Returns:
72324981Seric **		-1 if a < b
72424981Seric **		 0 if a == b
72524981Seric **		+1 if a > b
7264632Seric **
7274632Seric **	Side Effects:
7284632Seric **		none.
7294632Seric */
7304632Seric 
7314632Seric workcmpf(a, b)
7325037Seric 	register WORK *a;
7335037Seric 	register WORK *b;
7344632Seric {
73557438Seric 	long pa = a->w_pri;
73657438Seric 	long pb = b->w_pri;
73724941Seric 
73824941Seric 	if (pa == pb)
7394632Seric 		return (0);
74024941Seric 	else if (pa > pb)
74124981Seric 		return (1);
74224981Seric 	else
74310121Seric 		return (-1);
7444632Seric }
7454632Seric /*
7464632Seric **  DOWORK -- do a work request.
7474632Seric **
7484632Seric **	Parameters:
74958884Seric **		id -- the ID of the job to run.
75058884Seric **		forkflag -- if set, run this in background.
75158924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
75258924Seric **			This is used when expanding aliases in the queue.
75358884Seric **		e - the envelope in which to run it.
7544632Seric **
7554632Seric **	Returns:
7564632Seric **		none.
7574632Seric **
7584632Seric **	Side Effects:
7594632Seric **		The work request is satisfied if possible.
7604632Seric */
7614632Seric 
76258924Seric dowork(id, forkflag, requeueflag, e)
76358884Seric 	char *id;
76458884Seric 	bool forkflag;
76558924Seric 	bool requeueflag;
76655012Seric 	register ENVELOPE *e;
7674632Seric {
7684632Seric 	register int i;
76951920Seric 	extern bool readqf();
7704632Seric 
7717677Seric 	if (tTd(40, 1))
77258884Seric 		printf("dowork(%s)\n", id);
7734632Seric 
7744632Seric 	/*
77524941Seric 	**  Fork for work.
77624941Seric 	*/
77724941Seric 
77858884Seric 	if (forkflag)
77924941Seric 	{
78024941Seric 		i = fork();
78124941Seric 		if (i < 0)
78224941Seric 		{
78324941Seric 			syserr("dowork: cannot fork");
78424941Seric 			return;
78524941Seric 		}
78624941Seric 	}
78724941Seric 	else
78824941Seric 	{
78924941Seric 		i = 0;
79024941Seric 	}
79124941Seric 
7924632Seric 	if (i == 0)
7934632Seric 	{
7944632Seric 		/*
7954632Seric 		**  CHILD
7968148Seric 		**	Lock the control file to avoid duplicate deliveries.
7978148Seric 		**		Then run the file as though we had just read it.
7987350Seric 		**	We save an idea of the temporary name so we
7997350Seric 		**		can recover on interrupt.
8004632Seric 		*/
8014632Seric 
8027763Seric 		/* set basic modes, etc. */
8037356Seric 		(void) alarm(0);
80455012Seric 		clearenvelope(e, FALSE);
80558737Seric 		e->e_flags |= EF_QUEUERUN;
80658734Seric 		e->e_errormode = EM_MAIL;
80758884Seric 		e->e_id = id;
8087876Seric # ifdef LOG
80958020Seric 		if (LogLevel > 76)
81055012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8117881Seric 			       getpid());
81256795Seric # endif /* LOG */
8137763Seric 
8147763Seric 		/* don't use the headers from sendmail.cf... */
81555012Seric 		e->e_header = NULL;
8167763Seric 
81751920Seric 		/* read the queue control file -- return if locked */
81855012Seric 		if (!readqf(e))
8196980Seric 		{
82058884Seric 			if (tTd(40, 4))
82158884Seric 				printf("readqf(%s) failed\n", e->e_id);
82258884Seric 			if (forkflag)
82324941Seric 				exit(EX_OK);
82424941Seric 			else
82524941Seric 				return;
8266980Seric 		}
8276980Seric 
82855012Seric 		e->e_flags |= EF_INQUEUE;
82958929Seric 		eatheader(e, requeueflag);
8306980Seric 
83158924Seric 		if (requeueflag)
83258924Seric 			queueup(e, TRUE, FALSE);
83358924Seric 
8346980Seric 		/* do the delivery */
83558915Seric 		sendall(e, SM_DELIVER);
8366980Seric 
8376980Seric 		/* finish up and exit */
83858884Seric 		if (forkflag)
83924941Seric 			finis();
84024941Seric 		else
84155012Seric 			dropenvelope(e);
8424632Seric 	}
84324941Seric 	else
84424941Seric 	{
84524941Seric 		/*
84624941Seric 		**  Parent -- pick up results.
84724941Seric 		*/
8484632Seric 
84924941Seric 		errno = 0;
85024941Seric 		(void) waitfor(i);
85124941Seric 	}
8524632Seric }
8534632Seric /*
8544632Seric **  READQF -- read queue file and set up environment.
8554632Seric **
8564632Seric **	Parameters:
8579377Seric **		e -- the envelope of the job to run.
8584632Seric **
8594632Seric **	Returns:
86051920Seric **		TRUE if it successfully read the queue file.
86151920Seric **		FALSE otherwise.
8624632Seric **
8634632Seric **	Side Effects:
86451920Seric **		The queue file is returned locked.
8654632Seric */
8664632Seric 
86751920Seric bool
86851920Seric readqf(e)
8699377Seric 	register ENVELOPE *e;
8704632Seric {
87117477Seric 	register FILE *qfp;
87254974Seric 	ADDRESS *ctladdr;
87356400Seric 	struct stat st;
87457135Seric 	char *bp;
87558915Seric 	char qf[20];
87657135Seric 	char buf[MAXLINE];
8779348Seric 	extern char *fgetfolded();
87824954Seric 	extern long atol();
87954974Seric 	extern ADDRESS *setctluser();
88058689Seric 	extern bool lockfile();
8814632Seric 
8824632Seric 	/*
88317468Seric 	**  Read and process the file.
8844632Seric 	*/
8854632Seric 
88658915Seric 	strcpy(qf, queuename(e, 'q'));
88751937Seric 	qfp = fopen(qf, "r+");
88817477Seric 	if (qfp == NULL)
88917477Seric 	{
89058884Seric 		if (tTd(40, 8))
89158884Seric 			printf("readqf(%s): fopen failure (%s)\n",
89258884Seric 				qf, errstring(errno));
89340934Srick 		if (errno != ENOENT)
89440934Srick 			syserr("readqf: no control file %s", qf);
89551920Seric 		return FALSE;
89617477Seric 	}
89740934Srick 
89856400Seric 	/*
89956400Seric 	**  Check the queue file for plausibility to avoid attacks.
90056400Seric 	*/
90156400Seric 
90256400Seric 	if (fstat(fileno(qfp), &st) < 0)
90356400Seric 	{
90456400Seric 		/* must have been being processed by someone else */
90558884Seric 		if (tTd(40, 8))
90658884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90758884Seric 				qf, errstring(errno));
90856400Seric 		fclose(qfp);
90956400Seric 		return FALSE;
91056400Seric 	}
91156400Seric 
91257135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
91356400Seric 	{
91456400Seric # ifdef LOG
91556400Seric 		if (LogLevel > 0)
91656400Seric 		{
91756400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91856400Seric 				e->e_id, st.st_uid, st.st_mode);
91956400Seric 		}
92056795Seric # endif /* LOG */
92158884Seric 		if (tTd(40, 8))
92258884Seric 			printf("readqf(%s): bogus file\n", qf);
92356400Seric 		fclose(qfp);
92456400Seric 		return FALSE;
92556400Seric 	}
92656400Seric 
92758689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
92840934Srick 	{
92958689Seric 		/* being processed by another queuer */
93058884Seric 		if (tTd(40, 8))
93158884Seric 			printf("readqf(%s): locked\n", qf);
93258689Seric 		if (Verbose)
93358689Seric 			printf("%s: locked\n", e->e_id);
93451920Seric # ifdef LOG
93558689Seric 		if (LogLevel > 19)
93658689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
93756795Seric # endif /* LOG */
93840934Srick 		(void) fclose(qfp);
93951920Seric 		return FALSE;
94040934Srick 	}
94140934Srick 
94259101Seric 	if (st.st_size == 0)
94359101Seric 	{
94459101Seric 		/* must be a bogus file -- just remove it */
94559101Seric 		(void) unlink(qf);
94659101Seric 		fclose(qfp);
94759101Seric 		return FALSE;
94859101Seric 	}
94959101Seric 
95051920Seric 	/* save this lock */
95151920Seric 	e->e_lockfp = qfp;
95251920Seric 
95340934Srick 	/* do basic system initialization */
95455012Seric 	initsys(e);
95540934Srick 
95617477Seric 	FileName = qf;
9579377Seric 	LineNumber = 0;
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);
10444632Seric 			break;
10454632Seric 		}
104657135Seric 
104757135Seric 		if (bp != buf)
104857135Seric 			free(bp);
10494632Seric 	}
10509377Seric 
10519377Seric 	FileName = NULL;
105224941Seric 
105324941Seric 	/*
105424941Seric 	**  If we haven't read any lines, this queue file is empty.
105524941Seric 	**  Arrange to remove it without referencing any null pointers.
105624941Seric 	*/
105724941Seric 
105824941Seric 	if (LineNumber == 0)
105924941Seric 	{
106024941Seric 		errno = 0;
106124941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
106224941Seric 	}
106351920Seric 	return TRUE;
10644632Seric }
10654632Seric /*
10669630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10679630Seric **
10689630Seric **	Parameters:
10699630Seric **		none.
10709630Seric **
10719630Seric **	Returns:
10729630Seric **		none.
10739630Seric **
10749630Seric **	Side Effects:
10759630Seric **		Prints a listing of the mail queue on the standard output.
10769630Seric */
10775182Seric 
10789630Seric printqueue()
10799630Seric {
10809630Seric 	register WORK *w;
10819630Seric 	FILE *f;
108210070Seric 	int nrequests;
10839630Seric 	char buf[MAXLINE];
10849630Seric 
10859630Seric 	/*
108658250Seric 	**  Check for permission to print the queue
108758250Seric 	*/
108858250Seric 
108958523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
109058250Seric 	{
109158250Seric 		struct stat st;
109258523Seric # ifdef NGROUPS
109358523Seric 		int n;
109458523Seric 		int gidset[NGROUPS];
109558523Seric # endif
109658250Seric 
109758517Seric 		if (stat(QueueDir, &st) < 0)
109858250Seric 		{
109958250Seric 			syserr("Cannot stat %s", QueueDir);
110058250Seric 			return;
110158250Seric 		}
110258523Seric # ifdef NGROUPS
110358523Seric 		n = getgroups(NGROUPS, gidset);
110458523Seric 		while (--n >= 0)
110558523Seric 		{
110658523Seric 			if (gidset[n] == st.st_gid)
110758523Seric 				break;
110858523Seric 		}
110958523Seric 		if (n < 0)
111058523Seric # else
111158250Seric 		if (getgid() != st.st_gid)
111258523Seric # endif
111358250Seric 		{
111458250Seric 			usrerr("510 You are not permitted to see the queue");
111558250Seric 			setstat(EX_NOPERM);
111658250Seric 			return;
111758250Seric 		}
111858250Seric 	}
111958250Seric 
112058250Seric 	/*
11219630Seric 	**  Read and order the queue.
11229630Seric 	*/
11239630Seric 
112424941Seric 	nrequests = orderq(TRUE);
11259630Seric 
11269630Seric 	/*
11279630Seric 	**  Print the work list that we have read.
11289630Seric 	*/
11299630Seric 
11309630Seric 	/* first see if there is anything */
113110070Seric 	if (nrequests <= 0)
11329630Seric 	{
113310070Seric 		printf("Mail queue is empty\n");
11349630Seric 		return;
11359630Seric 	}
11369630Seric 
113751920Seric 	CurrentLA = getla();	/* get load average */
113840934Srick 
113910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
114025687Seric 	if (nrequests > QUEUESIZE)
114125687Seric 		printf(", only %d printed", QUEUESIZE);
114224979Seric 	if (Verbose)
114358716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114424979Seric 	else
114558716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11469630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11479630Seric 	{
11489630Seric 		struct stat st;
114910070Seric 		auto time_t submittime = 0;
115010070Seric 		long dfsize = -1;
115158737Seric 		int flags = 0;
115210108Seric 		char message[MAXLINE];
115359093Seric 		char bodytype[MAXNAME];
115424941Seric 		extern bool shouldqueue();
115558689Seric 		extern bool lockfile();
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];
128958689Seric 	extern bool lockfile();
129017468Seric 
129117468Seric 	if (e->e_id == NULL)
129217468Seric 	{
129317468Seric 		char qf[20];
129417468Seric 
129517468Seric 		/* find a unique id */
129617468Seric 		if (pid != getpid())
129717468Seric 		{
129817468Seric 			/* new process -- start back at "AA" */
129917468Seric 			pid = getpid();
130058716Seric 			now = curtime();
130158716Seric 			tm = localtime(&now);
130258716Seric 			c0 = 'A' + tm->tm_hour;
130317468Seric 			c1 = 'A';
130417468Seric 			c2 = 'A' - 1;
130517468Seric 		}
130658716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130717468Seric 
130817468Seric 		while (c1 < '~' || c2 < 'Z')
130917468Seric 		{
131017468Seric 			int i;
131117468Seric 
131217468Seric 			if (c2 >= 'Z')
131317468Seric 			{
131417468Seric 				c1++;
131517468Seric 				c2 = 'A' - 1;
131617468Seric 			}
131758716Seric 			qf[3] = c1;
131858716Seric 			qf[4] = ++c2;
131917468Seric 			if (tTd(7, 20))
132040934Srick 				printf("queuename: trying \"%s\"\n", qf);
132117468Seric 
132240934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
132351920Seric 			if (i < 0)
132451920Seric 			{
132551920Seric 				if (errno == EEXIST)
132651920Seric 					continue;
132751920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132851920Seric 					qf, QueueDir);
132951920Seric 				exit(EX_UNAVAILABLE);
133051920Seric 			}
133158689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
133251920Seric 			{
133351920Seric 				e->e_lockfp = fdopen(i, "w");
133440934Srick 				break;
133517468Seric 			}
133651920Seric 
133751920Seric 			/* a reader got the file; abandon it and try again */
133851920Seric 			(void) close(i);
133917468Seric 		}
134017468Seric 		if (c1 >= '~' && c2 >= 'Z')
134117468Seric 		{
134217468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
134317468Seric 				qf, QueueDir);
134417468Seric 			exit(EX_OSERR);
134517468Seric 		}
134617468Seric 		e->e_id = newstr(&qf[2]);
134717468Seric 		define('i', e->e_id, e);
134817468Seric 		if (tTd(7, 1))
134917468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
135017468Seric # ifdef LOG
135158020Seric 		if (LogLevel > 93)
135217468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
135356795Seric # endif /* LOG */
135417468Seric 	}
135517468Seric 
135617468Seric 	if (type == '\0')
135717468Seric 		return (NULL);
135817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
135917468Seric 	if (tTd(7, 2))
136017468Seric 		printf("queuename: %s\n", buf);
136117468Seric 	return (buf);
136217468Seric }
136317468Seric /*
136417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
136517468Seric **
136617468Seric **	Parameters:
136717468Seric **		e -- the envelope to unlock.
136817468Seric **
136917468Seric **	Returns:
137017468Seric **		none
137117468Seric **
137217468Seric **	Side Effects:
137317468Seric **		unlocks the queue for `e'.
137417468Seric */
137517468Seric 
137617468Seric unlockqueue(e)
137717468Seric 	ENVELOPE *e;
137817468Seric {
137958680Seric 	if (tTd(51, 4))
138058680Seric 		printf("unlockqueue(%s)\n", e->e_id);
138158680Seric 
138251920Seric 	/* if there is a lock file in the envelope, close it */
138351920Seric 	if (e->e_lockfp != NULL)
138458680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
138551920Seric 	e->e_lockfp = NULL;
138651920Seric 
138758728Seric 	/* don't create a queue id if we don't already have one */
138858728Seric 	if (e->e_id == NULL)
138958728Seric 		return;
139058728Seric 
139117468Seric 	/* remove the transcript */
139217468Seric # ifdef LOG
139358020Seric 	if (LogLevel > 87)
139417468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
139556795Seric # endif /* LOG */
139658680Seric 	if (!tTd(51, 104))
139717468Seric 		xunlink(queuename(e, 'x'));
139817468Seric 
139917468Seric }
140040973Sbostic /*
140154974Seric **  SETCTLUSER -- create a controlling address
140240973Sbostic **
140354974Seric **	Create a fake "address" given only a local login name; this is
140454974Seric **	used as a "controlling user" for future recipient addresses.
140540973Sbostic **
140640973Sbostic **	Parameters:
140754974Seric **		user -- the user name of the controlling user.
140840973Sbostic **
140940973Sbostic **	Returns:
141054974Seric **		An address descriptor for the controlling user.
141140973Sbostic **
141240973Sbostic **	Side Effects:
141340973Sbostic **		none.
141440973Sbostic */
141540973Sbostic 
141654974Seric ADDRESS *
141754974Seric setctluser(user)
141854974Seric 	char *user;
141940973Sbostic {
142054974Seric 	register ADDRESS *a;
142140973Sbostic 	struct passwd *pw;
142259113Seric 	char *p;
142340973Sbostic 
142440973Sbostic 	/*
142554974Seric 	**  See if this clears our concept of controlling user.
142640973Sbostic 	*/
142740973Sbostic 
142859270Seric 	if (user == NULL)
142959270Seric 		user = "";
143040973Sbostic 
143140973Sbostic 	/*
143254974Seric 	**  Set up addr fields for controlling user.
143340973Sbostic 	*/
143440973Sbostic 
143554974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143654974Seric 	bzero((char *) a, sizeof *a);
143759113Seric 
143859113Seric 	p = strchr(user, ':');
143959113Seric 	if (p != NULL)
144059113Seric 		*p++ = '\0';
144159270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
144240973Sbostic 	{
144340973Sbostic 		a->q_home = newstr(pw->pw_dir);
144440973Sbostic 		a->q_uid = pw->pw_uid;
144540973Sbostic 		a->q_gid = pw->pw_gid;
144657642Seric 		a->q_user = newstr(user);
144759270Seric 		a->q_flags |= QGOODUID;
144840973Sbostic 	}
144940973Sbostic 	else
145040973Sbostic 	{
145157642Seric 		a->q_user = newstr(DefUser);
145240973Sbostic 	}
145340973Sbostic 
145459270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
145556328Seric 	a->q_mailer = LocalMailer;
145659113Seric 	if (p == NULL)
145759113Seric 		a->q_paddr = a->q_user;
145859113Seric 	else
145959113Seric 		a->q_paddr = newstr(p);
146054974Seric 	return a;
146140973Sbostic }
1462