xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 60219)
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*60219Seric static char sccsid[] = "@(#)queue.c	6.57 (Berkeley) 05/22/93 (with queueing)";
1433731Sbostic #else
15*60219Seric static char sccsid[] = "@(#)queue.c	6.57 (Berkeley) 05/22/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];
7353400Seric 	extern char *macvalue();
744632Seric 
755037Seric 	/*
7617477Seric 	**  Create control file.
775037Seric 	*/
784632Seric 
7951920Seric 	newid = (e->e_id == NULL);
8051920Seric 	strcpy(tf, queuename(e, 't'));
8151920Seric 	tfp = e->e_lockfp;
8251920Seric 	if (tfp == NULL)
8351920Seric 		newid = FALSE;
8451920Seric 	if (newid)
8551835Seric 	{
8651920Seric 		tfp = e->e_lockfp;
8751920Seric 	}
8851920Seric 	else
8951920Seric 	{
9051920Seric 		/* get a locked tf file */
9151920Seric 		for (i = 100; --i >= 0; )
9251835Seric 		{
9358689Seric 			extern bool lockfile();
9451937Seric 
9551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9651920Seric 			if (fd < 0)
9751835Seric 			{
9851920Seric 				if (errno == EEXIST)
9951920Seric 					continue;
10058801Seric notemp:
10158690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10241636Srick 			}
10358689Seric 
10458689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10551920Seric 				break;
10658689Seric 
10751920Seric 			close(fd);
10858801Seric 			sleep(i);
10941636Srick 		}
11058801Seric 		if (fd < 0)
11158801Seric 			goto notemp;
11241636Srick 
11351920Seric 		tfp = fdopen(fd, "w");
11451920Seric 	}
1154632Seric 
1167677Seric 	if (tTd(40, 1))
11717468Seric 		printf("queueing %s\n", e->e_id);
1184632Seric 
1194632Seric 	/*
1206980Seric 	**  If there is no data file yet, create one.
1216980Seric 	*/
1226980Seric 
1236980Seric 	if (e->e_df == NULL)
1246980Seric 	{
1256980Seric 		register FILE *dfp;
1269389Seric 		extern putbody();
1276980Seric 
1287812Seric 		e->e_df = newstr(queuename(e, 'd'));
12940934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13040934Srick 		if (fd < 0)
13158690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13240934Srick 		dfp = fdopen(fd, "w");
13359730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
13458680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1359389Seric 		e->e_putbody = putbody;
1366980Seric 	}
1376980Seric 
1386980Seric 	/*
1394632Seric 	**  Output future work requests.
14025687Seric 	**	Priority and creation time should be first, since
14125687Seric 	**	they are required by orderq.
1424632Seric 	*/
1434632Seric 
1449377Seric 	/* output message priority */
1459377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1469377Seric 
1479630Seric 	/* output creation time */
1489630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1499630Seric 
15059093Seric 	/* output type and name of data file */
15159093Seric 	if (e->e_bodytype != NULL)
15259093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1537812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1544632Seric 
15510108Seric 	/* message from envelope, if it exists */
15610108Seric 	if (e->e_message != NULL)
15710108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15810108Seric 
15958737Seric 	/* send various flag bits through */
16058737Seric 	p = buf;
16158737Seric 	if (bitset(EF_WARNING, e->e_flags))
16258737Seric 		*p++ = 'w';
16358737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16458737Seric 		*p++ = 'r';
16558737Seric 	*p++ = '\0';
16658737Seric 	if (buf[0] != '\0')
16758737Seric 		fprintf(tfp, "F%s\n", buf);
16858737Seric 
16958957Seric 	/* $r and $s and $_ macro values */
17053400Seric 	if ((p = macvalue('r', e)) != NULL)
17153400Seric 		fprintf(tfp, "$r%s\n", p);
17253400Seric 	if ((p = macvalue('s', e)) != NULL)
17353400Seric 		fprintf(tfp, "$s%s\n", p);
17458957Seric 	if ((p = macvalue('_', e)) != NULL)
17558957Seric 		fprintf(tfp, "$_%s\n", p);
17653400Seric 
1774632Seric 	/* output name of sender */
1787812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1794632Seric 
18055360Seric 	/* output list of error recipients */
18159670Seric 	printctladdr(NULL, NULL);
18255360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18355360Seric 	{
18458680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18555360Seric 		{
18659113Seric 			printctladdr(q, tfp);
18755360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18855360Seric 		}
18955360Seric 	}
19055360Seric 
1914632Seric 	/* output list of recipient addresses */
1926980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1934632Seric 	{
19458250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19558680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1968245Seric 		{
19759113Seric 			printctladdr(q, tfp);
1987812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1999377Seric 			if (announce)
2009377Seric 			{
2019377Seric 				e->e_to = q->q_paddr;
20258151Seric 				message("queued");
20358020Seric 				if (LogLevel > 8)
20458337Seric 					logdelivery(NULL, NULL, "queued", e);
2059377Seric 				e->e_to = NULL;
2069377Seric 			}
2079387Seric 			if (tTd(40, 1))
2089387Seric 			{
2099387Seric 				printf("queueing ");
2109387Seric 				printaddr(q, FALSE);
2119387Seric 			}
2128245Seric 		}
2134632Seric 	}
2144632Seric 
2159377Seric 	/*
2169377Seric 	**  Output headers for this message.
2179377Seric 	**	Expand macros completely here.  Queue run will deal with
2189377Seric 	**	everything as absolute headers.
2199377Seric 	**		All headers that must be relative to the recipient
2209377Seric 	**		can be cracked later.
22110173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
22210173Seric 	**	no effect on the addresses as they are output.
2239377Seric 	*/
2249377Seric 
22510686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22658020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
22758020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
22810349Seric 	nullmailer.m_eol = "\n";
22910173Seric 
23058050Seric 	define('g', "\201f", e);
2316980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2324632Seric 	{
23310686Seric 		extern bool bitzerop();
23410686Seric 
23512015Seric 		/* don't output null headers */
2364632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2374632Seric 			continue;
23812015Seric 
23912015Seric 		/* don't output resent headers on non-resent messages */
24012015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
24112015Seric 			continue;
24212015Seric 
24312015Seric 		/* output this header */
2447812Seric 		fprintf(tfp, "H");
24512015Seric 
24612015Seric 		/* if conditional, output the set of conditions */
24710686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
24810686Seric 		{
24910686Seric 			int j;
25010686Seric 
25123098Seric 			(void) putc('?', tfp);
25210686Seric 			for (j = '\0'; j <= '\177'; j++)
25310686Seric 				if (bitnset(j, h->h_mflags))
25423098Seric 					(void) putc(j, tfp);
25523098Seric 			(void) putc('?', tfp);
25610686Seric 		}
25712015Seric 
25812015Seric 		/* output the header: expand macros, convert addresses */
2597763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2607763Seric 		{
2617763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2628236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2637763Seric 		}
2648245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2659348Seric 		{
26658737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
26758737Seric 
26858737Seric 			if (bitset(H_FROM, h->h_flags))
26958737Seric 				oldstyle = FALSE;
27058737Seric 
27158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27255012Seric 				 &nullmailer, e);
2739348Seric 		}
2747763Seric 		else
2758245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2764632Seric 	}
2774632Seric 
2784632Seric 	/*
2794632Seric 	**  Clean up.
2804632Seric 	*/
2814632Seric 
28258732Seric 	fflush(tfp);
28358732Seric 	if (ferror(tfp))
28458732Seric 	{
28558732Seric 		if (newid)
28658732Seric 			syserr("!552 Error writing control file %s", tf);
28758732Seric 		else
28858732Seric 			syserr("!452 Error writing control file %s", tf);
28958732Seric 	}
29058732Seric 
29151920Seric 	if (!newid)
29251920Seric 	{
29351920Seric 		qf = queuename(e, 'q');
29451920Seric 		if (rename(tf, qf) < 0)
29551920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29651920Seric 		if (e->e_lockfp != NULL)
29758680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
29851920Seric 		e->e_lockfp = tfp;
29951920Seric 	}
30051920Seric 	else
30151920Seric 		qf = tf;
30241636Srick 	errno = 0;
3037391Seric 
3047677Seric # ifdef LOG
3057677Seric 	/* save log info */
30658020Seric 	if (LogLevel > 79)
3077878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
30856795Seric # endif /* LOG */
30940934Srick 	fflush(tfp);
31051920Seric 	return;
3114632Seric }
31254974Seric 
31354974Seric printctladdr(a, tfp)
31459113Seric 	register ADDRESS *a;
31554974Seric 	FILE *tfp;
31654974Seric {
31759113Seric 	char *uname;
31859113Seric 	register struct passwd *pw;
31959113Seric 	register ADDRESS *q;
32059113Seric 	uid_t uid;
32159113Seric 	static ADDRESS *lastctladdr;
32259113Seric 	static uid_t lastuid;
32359113Seric 	extern ADDRESS *getctladdr();
32454974Seric 
32559113Seric 	/* initialization */
32659670Seric 	if (a == NULL || tfp == NULL)
32754974Seric 	{
32859670Seric 		if (lastctladdr != NULL && tfp != NULL)
32959113Seric 			fprintf(tfp, "C\n");
33059113Seric 		lastctladdr = NULL;
33159113Seric 		lastuid = 0;
33254974Seric 		return;
33354974Seric 	}
33459113Seric 
33559113Seric 	/* find the active uid */
33659113Seric 	q = getctladdr(a);
33759113Seric 	if (q == NULL)
33859113Seric 		uid = 0;
33954974Seric 	else
34059113Seric 		uid = q->q_uid;
34159113Seric 
34259113Seric 	/* if a is an alias, use that for printing */
34359113Seric 	if (a->q_alias != NULL)
34459113Seric 		a = a->q_alias;
34559113Seric 
34659113Seric 	/* check to see if this is the same as last time */
34759113Seric 	if (lastctladdr != NULL && uid == lastuid &&
34859113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
34959113Seric 		return;
35059113Seric 	lastuid = uid;
35159113Seric 	lastctladdr = a;
35259113Seric 
35359113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35459270Seric 		uname = "";
35559113Seric 	else
35659113Seric 		uname = pw->pw_name;
35759113Seric 
35859113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
35954974Seric }
36054974Seric 
3614632Seric /*
3624632Seric **  RUNQUEUE -- run the jobs in the queue.
3634632Seric **
3644632Seric **	Gets the stuff out of the queue in some presumably logical
3654632Seric **	order and processes them.
3664632Seric **
3674632Seric **	Parameters:
36824941Seric **		forkflag -- TRUE if the queue scanning should be done in
36924941Seric **			a child process.  We double-fork so it is not our
37024941Seric **			child and we don't have to clean up after it.
3714632Seric **
3724632Seric **	Returns:
3734632Seric **		none.
3744632Seric **
3754632Seric **	Side Effects:
3764632Seric **		runs things in the mail queue.
3774632Seric */
3784632Seric 
37955360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
38055360Seric 
38155360Seric runqueue(forkflag)
3824639Seric 	bool forkflag;
3834632Seric {
38424953Seric 	extern bool shouldqueue();
38555360Seric 	register ENVELOPE *e;
38655360Seric 	extern ENVELOPE BlankEnvelope;
38755360Seric 	extern ENVELOPE *newenvelope();
38824953Seric 
3897466Seric 	/*
39024953Seric 	**  If no work will ever be selected, don't even bother reading
39124953Seric 	**  the queue.
39224953Seric 	*/
39324953Seric 
39451920Seric 	CurrentLA = getla();	/* get load average */
39540934Srick 
39658132Seric 	if (shouldqueue(0L, curtime()))
39724953Seric 	{
39824953Seric 		if (Verbose)
39924953Seric 			printf("Skipping queue run -- load average too high\n");
40058107Seric 		if (forkflag && QueueIntvl != 0)
40158107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
40255360Seric 		return;
40324953Seric 	}
40424953Seric 
40524953Seric 	/*
4067466Seric 	**  See if we want to go off and do other useful work.
4077466Seric 	*/
4084639Seric 
4094639Seric 	if (forkflag)
4104639Seric 	{
4117943Seric 		int pid;
4127943Seric 
4137943Seric 		pid = dofork();
4147943Seric 		if (pid != 0)
4154639Seric 		{
41646928Sbostic 			extern void reapchild();
41725184Seric 
4187943Seric 			/* parent -- pick up intermediate zombie */
41925184Seric #ifndef SIGCHLD
4209377Seric 			(void) waitfor(pid);
42156795Seric #else /* SIGCHLD */
42225184Seric 			(void) signal(SIGCHLD, reapchild);
42356795Seric #endif /* SIGCHLD */
4247690Seric 			if (QueueIntvl != 0)
4259348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4264639Seric 			return;
4274639Seric 		}
4287943Seric 		/* child -- double fork */
42925184Seric #ifndef SIGCHLD
4307943Seric 		if (fork() != 0)
4317943Seric 			exit(EX_OK);
43256795Seric #else /* SIGCHLD */
43325184Seric 		(void) signal(SIGCHLD, SIG_DFL);
43456795Seric #endif /* SIGCHLD */
4354639Seric 	}
43624941Seric 
43740934Srick 	setproctitle("running queue: %s", QueueDir);
43824941Seric 
4397876Seric # ifdef LOG
44058020Seric 	if (LogLevel > 69)
44155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
44255360Seric 			QueueDir, getpid(), forkflag);
44356795Seric # endif /* LOG */
4444639Seric 
4457466Seric 	/*
44610205Seric 	**  Release any resources used by the daemon code.
44710205Seric 	*/
44810205Seric 
44910205Seric # ifdef DAEMON
45010205Seric 	clrdaemon();
45156795Seric # endif /* DAEMON */
45210205Seric 
45310205Seric 	/*
45455360Seric 	**  Create ourselves an envelope
45555360Seric 	*/
45655360Seric 
45755360Seric 	CurEnv = &QueueEnvelope;
45858179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45955360Seric 	e->e_flags = BlankEnvelope.e_flags;
46055360Seric 
46155360Seric 	/*
46227175Seric 	**  Make sure the alias database is open.
46327175Seric 	*/
46427175Seric 
46559670Seric 	initaliases(FALSE, e);
46627175Seric 
46727175Seric 	/*
4687466Seric 	**  Start making passes through the queue.
4697466Seric 	**	First, read and sort the entire queue.
4707466Seric 	**	Then, process the work in that order.
4717466Seric 	**		But if you take too long, start over.
4727466Seric 	*/
4737466Seric 
4747943Seric 	/* order the existing work requests */
47524954Seric 	(void) orderq(FALSE);
4767690Seric 
4777943Seric 	/* process them once at a time */
4787943Seric 	while (WorkQ != NULL)
4794639Seric 	{
4807943Seric 		WORK *w = WorkQ;
48158884Seric 		extern bool shouldqueue();
4827881Seric 
4837943Seric 		WorkQ = WorkQ->w_next;
48458884Seric 
48558884Seric 		/*
48658884Seric 		**  Ignore jobs that are too expensive for the moment.
48758884Seric 		*/
48858884Seric 
48958884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
49058884Seric 		{
49158884Seric 			if (Verbose)
49258884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
49358884Seric 		}
49458930Seric 		else
49558930Seric 		{
49658930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
49758930Seric 		}
4987943Seric 		free(w->w_name);
4997943Seric 		free((char *) w);
5004639Seric 	}
50129866Seric 
50229866Seric 	/* exit without the usual cleanup */
50355467Seric 	e->e_id = NULL;
50455467Seric 	finis();
5054634Seric }
5064634Seric /*
5074632Seric **  ORDERQ -- order the work queue.
5084632Seric **
5094632Seric **	Parameters:
51024941Seric **		doall -- if set, include everything in the queue (even
51124941Seric **			the jobs that cannot be run because the load
51224941Seric **			average is too high).  Otherwise, exclude those
51324941Seric **			jobs.
5144632Seric **
5154632Seric **	Returns:
51610121Seric **		The number of request in the queue (not necessarily
51710121Seric **		the number of requests in WorkQ however).
5184632Seric **
5194632Seric **	Side Effects:
5204632Seric **		Sets WorkQ to the queue of available work, in order.
5214632Seric */
5224632Seric 
52325687Seric # define NEED_P		001
52425687Seric # define NEED_T		002
52558318Seric # define NEED_R		004
52658318Seric # define NEED_S		010
5274632Seric 
52824941Seric orderq(doall)
52924941Seric 	bool doall;
5304632Seric {
531*60219Seric 	register struct dirent *d;
5324632Seric 	register WORK *w;
5336625Sglickman 	DIR *f;
5344632Seric 	register int i;
53525687Seric 	WORK wlist[QUEUESIZE+1];
53610070Seric 	int wn = -1;
5374632Seric 	extern workcmpf();
5384632Seric 
53958318Seric 	if (tTd(41, 1))
54058318Seric 	{
54158318Seric 		printf("orderq:\n");
54258318Seric 		if (QueueLimitId != NULL)
54358318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
54458318Seric 		if (QueueLimitSender != NULL)
54558318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
54658318Seric 		if (QueueLimitRecipient != NULL)
54758318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
54858318Seric 	}
54958318Seric 
5504632Seric 	/* clear out old WorkQ */
5514632Seric 	for (w = WorkQ; w != NULL; )
5524632Seric 	{
5534632Seric 		register WORK *nw = w->w_next;
5544632Seric 
5554632Seric 		WorkQ = nw;
5564632Seric 		free(w->w_name);
5574632Seric 		free((char *) w);
5584632Seric 		w = nw;
5594632Seric 	}
5604632Seric 
5614632Seric 	/* open the queue directory */
5628148Seric 	f = opendir(".");
5634632Seric 	if (f == NULL)
5644632Seric 	{
5658148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
56610070Seric 		return (0);
5674632Seric 	}
5684632Seric 
5694632Seric 	/*
5704632Seric 	**  Read the work directory.
5714632Seric 	*/
5724632Seric 
57310070Seric 	while ((d = readdir(f)) != NULL)
5744632Seric 	{
5759377Seric 		FILE *cf;
5764632Seric 		char lbuf[MAXNAME];
57757642Seric 		extern bool shouldqueue();
57858318Seric 		extern bool strcontainedin();
5794632Seric 
5804632Seric 		/* is this an interesting entry? */
5817812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5824632Seric 			continue;
5834632Seric 
58458318Seric 		if (QueueLimitId != NULL &&
58558318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
58658318Seric 			continue;
58758318Seric 
58858722Seric 		/*
58958722Seric 		**  Check queue name for plausibility.  This handles
59058722Seric 		**  both old and new type ids.
59158722Seric 		*/
59258722Seric 
59358722Seric 		i = strlen(d->d_name);
59458722Seric 		if (i != 9 && i != 10)
59558020Seric 		{
59658020Seric 			if (Verbose)
59758020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
59858020Seric #ifdef LOG
59958020Seric 			if (LogLevel > 3)
60059615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
60158020Seric 					d->d_name);
60258020Seric #endif
60358020Seric 			if (strlen(d->d_name) >= MAXNAME)
60458020Seric 				d->d_name[MAXNAME - 1] = '\0';
60558020Seric 			strcpy(lbuf, d->d_name);
60658020Seric 			lbuf[0] = 'Q';
60758020Seric 			(void) rename(d->d_name, lbuf);
60858020Seric 			continue;
60958020Seric 		}
61058020Seric 
61110070Seric 		/* yes -- open control file (if not too many files) */
61225687Seric 		if (++wn >= QUEUESIZE)
61310070Seric 			continue;
61458318Seric 
6158148Seric 		cf = fopen(d->d_name, "r");
6164632Seric 		if (cf == NULL)
6174632Seric 		{
6187055Seric 			/* this may be some random person sending hir msgs */
6197055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
62010090Seric 			if (tTd(41, 2))
62110090Seric 				printf("orderq: cannot open %s (%d)\n",
62210090Seric 					d->d_name, errno);
6237055Seric 			errno = 0;
62410090Seric 			wn--;
6254632Seric 			continue;
6264632Seric 		}
62725687Seric 		w = &wlist[wn];
62825687Seric 		w->w_name = newstr(d->d_name);
6294632Seric 
63025027Seric 		/* make sure jobs in creation don't clog queue */
63125687Seric 		w->w_pri = 0x7fffffff;
63225687Seric 		w->w_ctime = 0;
63325027Seric 
6344632Seric 		/* extract useful information */
63525687Seric 		i = NEED_P | NEED_T;
63658318Seric 		if (QueueLimitSender != NULL)
63758318Seric 			i |= NEED_S;
63858318Seric 		if (QueueLimitRecipient != NULL)
63958318Seric 			i |= NEED_R;
64025687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6414632Seric 		{
64224954Seric 			extern long atol();
64358318Seric 			extern bool strcontainedin();
64424954Seric 
64524941Seric 			switch (lbuf[0])
6464632Seric 			{
64724941Seric 			  case 'P':
64825687Seric 				w->w_pri = atol(&lbuf[1]);
64925687Seric 				i &= ~NEED_P;
6504632Seric 				break;
65125013Seric 
65225013Seric 			  case 'T':
65325687Seric 				w->w_ctime = atol(&lbuf[1]);
65425687Seric 				i &= ~NEED_T;
65525013Seric 				break;
65658318Seric 
65758318Seric 			  case 'R':
65858318Seric 				if (QueueLimitRecipient != NULL &&
65958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
66058318Seric 					i &= ~NEED_R;
66158318Seric 				break;
66258318Seric 
66358318Seric 			  case 'S':
66458318Seric 				if (QueueLimitSender != NULL &&
66558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
66658318Seric 					i &= ~NEED_S;
66758318Seric 				break;
6684632Seric 			}
6694632Seric 		}
6704632Seric 		(void) fclose(cf);
67124953Seric 
67258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
67358318Seric 		    bitset(NEED_R|NEED_S, i))
67424953Seric 		{
67524953Seric 			/* don't even bother sorting this job in */
67624953Seric 			wn--;
67724953Seric 		}
6784632Seric 	}
6796625Sglickman 	(void) closedir(f);
68010090Seric 	wn++;
6814632Seric 
6824632Seric 	/*
6834632Seric 	**  Sort the work directory.
6844632Seric 	*/
6854632Seric 
68625687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6874632Seric 
6884632Seric 	/*
6894632Seric 	**  Convert the work list into canonical form.
6909377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6914632Seric 	*/
6924632Seric 
69324981Seric 	WorkQ = NULL;
69425687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6954632Seric 	{
6964632Seric 		w = (WORK *) xalloc(sizeof *w);
6974632Seric 		w->w_name = wlist[i].w_name;
6984632Seric 		w->w_pri = wlist[i].w_pri;
69925013Seric 		w->w_ctime = wlist[i].w_ctime;
70024981Seric 		w->w_next = WorkQ;
70124981Seric 		WorkQ = w;
7024632Seric 	}
7034632Seric 
7047677Seric 	if (tTd(40, 1))
7054632Seric 	{
7064632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7075037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7084632Seric 	}
70910070Seric 
71010090Seric 	return (wn);
7114632Seric }
7124632Seric /*
7137677Seric **  WORKCMPF -- compare function for ordering work.
7144632Seric **
7154632Seric **	Parameters:
7164632Seric **		a -- the first argument.
7174632Seric **		b -- the second argument.
7184632Seric **
7194632Seric **	Returns:
72024981Seric **		-1 if a < b
72124981Seric **		 0 if a == b
72224981Seric **		+1 if a > b
7234632Seric **
7244632Seric **	Side Effects:
7254632Seric **		none.
7264632Seric */
7274632Seric 
7284632Seric workcmpf(a, b)
7295037Seric 	register WORK *a;
7305037Seric 	register WORK *b;
7314632Seric {
73257438Seric 	long pa = a->w_pri;
73357438Seric 	long pb = b->w_pri;
73424941Seric 
73524941Seric 	if (pa == pb)
7364632Seric 		return (0);
73724941Seric 	else if (pa > pb)
73824981Seric 		return (1);
73924981Seric 	else
74010121Seric 		return (-1);
7414632Seric }
7424632Seric /*
7434632Seric **  DOWORK -- do a work request.
7444632Seric **
7454632Seric **	Parameters:
74658884Seric **		id -- the ID of the job to run.
74758884Seric **		forkflag -- if set, run this in background.
74858924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
74958924Seric **			This is used when expanding aliases in the queue.
75058884Seric **		e - the envelope in which to run it.
7514632Seric **
7524632Seric **	Returns:
7534632Seric **		none.
7544632Seric **
7554632Seric **	Side Effects:
7564632Seric **		The work request is satisfied if possible.
7574632Seric */
7584632Seric 
75958924Seric dowork(id, forkflag, requeueflag, e)
76058884Seric 	char *id;
76158884Seric 	bool forkflag;
76258924Seric 	bool requeueflag;
76355012Seric 	register ENVELOPE *e;
7644632Seric {
7654632Seric 	register int i;
76651920Seric 	extern bool readqf();
7674632Seric 
7687677Seric 	if (tTd(40, 1))
76958884Seric 		printf("dowork(%s)\n", id);
7704632Seric 
7714632Seric 	/*
77224941Seric 	**  Fork for work.
77324941Seric 	*/
77424941Seric 
77558884Seric 	if (forkflag)
77624941Seric 	{
77724941Seric 		i = fork();
77824941Seric 		if (i < 0)
77924941Seric 		{
78024941Seric 			syserr("dowork: cannot fork");
78124941Seric 			return;
78224941Seric 		}
78324941Seric 	}
78424941Seric 	else
78524941Seric 	{
78624941Seric 		i = 0;
78724941Seric 	}
78824941Seric 
7894632Seric 	if (i == 0)
7904632Seric 	{
7914632Seric 		/*
7924632Seric 		**  CHILD
7938148Seric 		**	Lock the control file to avoid duplicate deliveries.
7948148Seric 		**		Then run the file as though we had just read it.
7957350Seric 		**	We save an idea of the temporary name so we
7967350Seric 		**		can recover on interrupt.
7974632Seric 		*/
7984632Seric 
7997763Seric 		/* set basic modes, etc. */
8007356Seric 		(void) alarm(0);
80155012Seric 		clearenvelope(e, FALSE);
80258737Seric 		e->e_flags |= EF_QUEUERUN;
80358734Seric 		e->e_errormode = EM_MAIL;
80458884Seric 		e->e_id = id;
8057876Seric # ifdef LOG
80658020Seric 		if (LogLevel > 76)
80755012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8087881Seric 			       getpid());
80956795Seric # endif /* LOG */
8107763Seric 
8117763Seric 		/* don't use the headers from sendmail.cf... */
81255012Seric 		e->e_header = NULL;
8137763Seric 
81451920Seric 		/* read the queue control file -- return if locked */
81555012Seric 		if (!readqf(e))
8166980Seric 		{
81758884Seric 			if (tTd(40, 4))
81858884Seric 				printf("readqf(%s) failed\n", e->e_id);
81958884Seric 			if (forkflag)
82024941Seric 				exit(EX_OK);
82124941Seric 			else
82224941Seric 				return;
8236980Seric 		}
8246980Seric 
82555012Seric 		e->e_flags |= EF_INQUEUE;
82658929Seric 		eatheader(e, requeueflag);
8276980Seric 
82858924Seric 		if (requeueflag)
82958924Seric 			queueup(e, TRUE, FALSE);
83058924Seric 
8316980Seric 		/* do the delivery */
83258915Seric 		sendall(e, SM_DELIVER);
8336980Seric 
8346980Seric 		/* finish up and exit */
83558884Seric 		if (forkflag)
83624941Seric 			finis();
83724941Seric 		else
83855012Seric 			dropenvelope(e);
8394632Seric 	}
84024941Seric 	else
84124941Seric 	{
84224941Seric 		/*
84324941Seric 		**  Parent -- pick up results.
84424941Seric 		*/
8454632Seric 
84624941Seric 		errno = 0;
84724941Seric 		(void) waitfor(i);
84824941Seric 	}
8494632Seric }
8504632Seric /*
8514632Seric **  READQF -- read queue file and set up environment.
8524632Seric **
8534632Seric **	Parameters:
8549377Seric **		e -- the envelope of the job to run.
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
86551920Seric readqf(e)
8669377Seric 	register ENVELOPE *e;
8674632Seric {
86817477Seric 	register FILE *qfp;
86954974Seric 	ADDRESS *ctladdr;
87056400Seric 	struct stat st;
87157135Seric 	char *bp;
87258915Seric 	char qf[20];
87357135Seric 	char buf[MAXLINE];
8749348Seric 	extern char *fgetfolded();
87524954Seric 	extern long atol();
87654974Seric 	extern ADDRESS *setctluser();
87758689Seric 	extern bool lockfile();
8784632Seric 
8794632Seric 	/*
88017468Seric 	**  Read and process the file.
8814632Seric 	*/
8824632Seric 
88358915Seric 	strcpy(qf, queuename(e, 'q'));
88451937Seric 	qfp = fopen(qf, "r+");
88517477Seric 	if (qfp == NULL)
88617477Seric 	{
88758884Seric 		if (tTd(40, 8))
88858884Seric 			printf("readqf(%s): fopen failure (%s)\n",
88958884Seric 				qf, errstring(errno));
89040934Srick 		if (errno != ENOENT)
89140934Srick 			syserr("readqf: no control file %s", qf);
89251920Seric 		return FALSE;
89317477Seric 	}
89440934Srick 
89556400Seric 	/*
89656400Seric 	**  Check the queue file for plausibility to avoid attacks.
89756400Seric 	*/
89856400Seric 
89956400Seric 	if (fstat(fileno(qfp), &st) < 0)
90056400Seric 	{
90156400Seric 		/* must have been being processed by someone else */
90258884Seric 		if (tTd(40, 8))
90358884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90458884Seric 				qf, errstring(errno));
90556400Seric 		fclose(qfp);
90656400Seric 		return FALSE;
90756400Seric 	}
90856400Seric 
90957135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
91056400Seric 	{
91156400Seric # ifdef LOG
91256400Seric 		if (LogLevel > 0)
91356400Seric 		{
91456400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91556400Seric 				e->e_id, st.st_uid, st.st_mode);
91656400Seric 		}
91756795Seric # endif /* LOG */
91858884Seric 		if (tTd(40, 8))
91958884Seric 			printf("readqf(%s): bogus file\n", qf);
92056400Seric 		fclose(qfp);
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 
95317477Seric 	FileName = qf;
9549377Seric 	LineNumber = 0;
95551920Seric 	if (Verbose)
9569377Seric 		printf("\nRunning %s\n", e->e_id);
95754974Seric 	ctladdr = NULL;
95857135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9594632Seric 	{
96058737Seric 		register char *p;
96157529Seric 		struct stat st;
96257529Seric 
96326504Seric 		if (tTd(40, 4))
96457135Seric 			printf("+++++ %s\n", bp);
96557135Seric 		switch (bp[0])
9664632Seric 		{
96740973Sbostic 		  case 'C':		/* specify controlling user */
96857135Seric 			ctladdr = setctluser(&bp[1]);
96940973Sbostic 			break;
97040973Sbostic 
9714632Seric 		  case 'R':		/* specify recipient */
97258082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9734632Seric 			break;
9744632Seric 
97525687Seric 		  case 'E':		/* specify error recipient */
97658082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
97725687Seric 			break;
97825687Seric 
9794632Seric 		  case 'H':		/* header */
98057135Seric 			(void) chompheader(&bp[1], FALSE, e);
9814632Seric 			break;
9824632Seric 
98310108Seric 		  case 'M':		/* message */
98457135Seric 			e->e_message = newstr(&bp[1]);
98510108Seric 			break;
98610108Seric 
9874632Seric 		  case 'S':		/* sender */
98858704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9894632Seric 			break;
9904632Seric 
99159093Seric 		  case 'B':		/* body type */
99259093Seric 			e->e_bodytype = newstr(&bp[1]);
99359093Seric 			break;
99459093Seric 
9954632Seric 		  case 'D':		/* data file name */
99657135Seric 			e->e_df = newstr(&bp[1]);
9979544Seric 			e->e_dfp = fopen(e->e_df, "r");
9989544Seric 			if (e->e_dfp == NULL)
99958020Seric 			{
10009377Seric 				syserr("readqf: cannot open %s", e->e_df);
100158020Seric 				e->e_msgsize = -1;
100258020Seric 			}
100358020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
100457529Seric 				e->e_msgsize = st.st_size;
10054632Seric 			break;
10064632Seric 
10077860Seric 		  case 'T':		/* init time */
100857135Seric 			e->e_ctime = atol(&bp[1]);
10094632Seric 			break;
10104632Seric 
10114634Seric 		  case 'P':		/* message priority */
101257135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10134634Seric 			break;
10144634Seric 
101558737Seric 		  case 'F':		/* flag bits */
101658737Seric 			for (p = &bp[1]; *p != '\0'; p++)
101758737Seric 			{
101858737Seric 				switch (*p)
101958737Seric 				{
102058737Seric 				  case 'w':	/* warning sent */
102158737Seric 					e->e_flags |= EF_WARNING;
102258737Seric 					break;
102358737Seric 
102458737Seric 				  case 'r':	/* response */
102558737Seric 					e->e_flags |= EF_RESPONSE;
102658737Seric 					break;
102758737Seric 				}
102858737Seric 			}
102958737Seric 			break;
103058737Seric 
103153400Seric 		  case '$':		/* define macro */
103257135Seric 			define(bp[1], newstr(&bp[2]), e);
103353400Seric 			break;
103453400Seric 
103524941Seric 		  case '\0':		/* blank line; ignore */
103624941Seric 			break;
103724941Seric 
10384632Seric 		  default:
103959700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
104057135Seric 				LineNumber, bp);
10414632Seric 			break;
10424632Seric 		}
104357135Seric 
104457135Seric 		if (bp != buf)
104557135Seric 			free(bp);
10464632Seric 	}
10479377Seric 
10489377Seric 	FileName = NULL;
104924941Seric 
105024941Seric 	/*
105124941Seric 	**  If we haven't read any lines, this queue file is empty.
105224941Seric 	**  Arrange to remove it without referencing any null pointers.
105324941Seric 	*/
105424941Seric 
105524941Seric 	if (LineNumber == 0)
105624941Seric 	{
105724941Seric 		errno = 0;
105824941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
105924941Seric 	}
106051920Seric 	return TRUE;
10614632Seric }
10624632Seric /*
10639630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10649630Seric **
10659630Seric **	Parameters:
10669630Seric **		none.
10679630Seric **
10689630Seric **	Returns:
10699630Seric **		none.
10709630Seric **
10719630Seric **	Side Effects:
10729630Seric **		Prints a listing of the mail queue on the standard output.
10739630Seric */
10745182Seric 
10759630Seric printqueue()
10769630Seric {
10779630Seric 	register WORK *w;
10789630Seric 	FILE *f;
107910070Seric 	int nrequests;
10809630Seric 	char buf[MAXLINE];
10819630Seric 
10829630Seric 	/*
108358250Seric 	**  Check for permission to print the queue
108458250Seric 	*/
108558250Seric 
108658523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
108758250Seric 	{
108858250Seric 		struct stat st;
108958523Seric # ifdef NGROUPS
109058523Seric 		int n;
109158523Seric 		int gidset[NGROUPS];
109258523Seric # endif
109358250Seric 
109458517Seric 		if (stat(QueueDir, &st) < 0)
109558250Seric 		{
109658250Seric 			syserr("Cannot stat %s", QueueDir);
109758250Seric 			return;
109858250Seric 		}
109958523Seric # ifdef NGROUPS
110058523Seric 		n = getgroups(NGROUPS, gidset);
110158523Seric 		while (--n >= 0)
110258523Seric 		{
110358523Seric 			if (gidset[n] == st.st_gid)
110458523Seric 				break;
110558523Seric 		}
110658523Seric 		if (n < 0)
110758523Seric # else
110858250Seric 		if (getgid() != st.st_gid)
110958523Seric # endif
111058250Seric 		{
111158250Seric 			usrerr("510 You are not permitted to see the queue");
111258250Seric 			setstat(EX_NOPERM);
111358250Seric 			return;
111458250Seric 		}
111558250Seric 	}
111658250Seric 
111758250Seric 	/*
11189630Seric 	**  Read and order the queue.
11199630Seric 	*/
11209630Seric 
112124941Seric 	nrequests = orderq(TRUE);
11229630Seric 
11239630Seric 	/*
11249630Seric 	**  Print the work list that we have read.
11259630Seric 	*/
11269630Seric 
11279630Seric 	/* first see if there is anything */
112810070Seric 	if (nrequests <= 0)
11299630Seric 	{
113010070Seric 		printf("Mail queue is empty\n");
11319630Seric 		return;
11329630Seric 	}
11339630Seric 
113451920Seric 	CurrentLA = getla();	/* get load average */
113540934Srick 
113610096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
113725687Seric 	if (nrequests > QUEUESIZE)
113825687Seric 		printf(", only %d printed", QUEUESIZE);
113924979Seric 	if (Verbose)
114058716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114124979Seric 	else
114258716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11439630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11449630Seric 	{
11459630Seric 		struct stat st;
114610070Seric 		auto time_t submittime = 0;
114710070Seric 		long dfsize = -1;
114858737Seric 		int flags = 0;
114910108Seric 		char message[MAXLINE];
115059093Seric 		char bodytype[MAXNAME];
115124941Seric 		extern bool shouldqueue();
115258689Seric 		extern bool lockfile();
11539630Seric 
115417468Seric 		f = fopen(w->w_name, "r");
115517468Seric 		if (f == NULL)
115617468Seric 		{
115717468Seric 			errno = 0;
115817468Seric 			continue;
115917468Seric 		}
116058724Seric 		printf("%8s", w->w_name + 2);
116158689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
116210070Seric 			printf("*");
116357438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
116424941Seric 			printf("X");
116510070Seric 		else
116610070Seric 			printf(" ");
116710070Seric 		errno = 0;
116817468Seric 
116959093Seric 		message[0] = bodytype[0] = '\0';
11709630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11719630Seric 		{
117253400Seric 			register int i;
117358737Seric 			register char *p;
117453400Seric 
11759630Seric 			fixcrlf(buf, TRUE);
11769630Seric 			switch (buf[0])
11779630Seric 			{
117810108Seric 			  case 'M':	/* error message */
117953400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
118058737Seric 					i = sizeof message - 1;
118153400Seric 				bcopy(&buf[1], message, i);
118253400Seric 				message[i] = '\0';
118310108Seric 				break;
118410108Seric 
118559093Seric 			  case 'B':	/* body type */
118659093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
118759093Seric 					i = sizeof bodytype - 1;
118859093Seric 				bcopy(&buf[1], bodytype, i);
118959093Seric 				bodytype[i] = '\0';
119059093Seric 				break;
119159093Seric 
11929630Seric 			  case 'S':	/* sender name */
119324979Seric 				if (Verbose)
119458737Seric 					printf("%8ld %10ld%c%.12s %.38s",
119558737Seric 					    dfsize,
119658737Seric 					    w->w_pri,
119758737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
119858737Seric 					    ctime(&submittime) + 4,
119924979Seric 					    &buf[1]);
120024979Seric 				else
120124979Seric 					printf("%8ld %.16s %.45s", dfsize,
120224979Seric 					    ctime(&submittime), &buf[1]);
120359093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
120459093Seric 				{
120559093Seric 					printf("\n    %10.10s", bodytype);
120659093Seric 					if (message[0] != '\0')
120759093Seric 						printf("   (%.60s)", message);
120859093Seric 				}
12099630Seric 				break;
121051920Seric 
121140973Sbostic 			  case 'C':	/* controlling user */
121254974Seric 				if (Verbose)
121358716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
121458716Seric 						&buf[1]);
121540973Sbostic 				break;
12169630Seric 
12179630Seric 			  case 'R':	/* recipient name */
121824979Seric 				if (Verbose)
121958716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
122024979Seric 				else
122158716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12229630Seric 				break;
12239630Seric 
12249630Seric 			  case 'T':	/* creation time */
122524941Seric 				submittime = atol(&buf[1]);
12269630Seric 				break;
122710070Seric 
122810070Seric 			  case 'D':	/* data file name */
122910070Seric 				if (stat(&buf[1], &st) >= 0)
123010070Seric 					dfsize = st.st_size;
123110070Seric 				break;
123258737Seric 
123358737Seric 			  case 'F':	/* flag bits */
123458737Seric 				for (p = &buf[1]; *p != '\0'; p++)
123558737Seric 				{
123658737Seric 					switch (*p)
123758737Seric 					{
123858737Seric 					  case 'w':
123958737Seric 						flags |= EF_WARNING;
124058737Seric 						break;
124158737Seric 					}
124258737Seric 				}
12439630Seric 			}
12449630Seric 		}
124510070Seric 		if (submittime == (time_t) 0)
124610070Seric 			printf(" (no control file)");
12479630Seric 		printf("\n");
124823098Seric 		(void) fclose(f);
12499630Seric 	}
12509630Seric }
12519630Seric 
125256795Seric # endif /* QUEUE */
125317468Seric /*
125417468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
125517468Seric **
125617468Seric **	Assigns an id code if one does not already exist.
125717468Seric **	This code is very careful to avoid trashing existing files
125817468Seric **	under any circumstances.
125917468Seric **
126017468Seric **	Parameters:
126117468Seric **		e -- envelope to build it in/from.
126217468Seric **		type -- the file type, used as the first character
126317468Seric **			of the file name.
126417468Seric **
126517468Seric **	Returns:
126617468Seric **		a pointer to the new file name (in a static buffer).
126717468Seric **
126817468Seric **	Side Effects:
126951920Seric **		If no id code is already assigned, queuename will
127051920Seric **		assign an id code, create a qf file, and leave a
127151920Seric **		locked, open-for-write file pointer in the envelope.
127217468Seric */
127317468Seric 
127417468Seric char *
127517468Seric queuename(e, type)
127617468Seric 	register ENVELOPE *e;
127759700Seric 	int type;
127817468Seric {
127917468Seric 	static int pid = -1;
128058741Seric 	static char c0;
128158741Seric 	static char c1;
128258741Seric 	static char c2;
128358716Seric 	time_t now;
128458716Seric 	struct tm *tm;
128558689Seric 	static char buf[MAXNAME];
128658689Seric 	extern bool lockfile();
128717468Seric 
128817468Seric 	if (e->e_id == NULL)
128917468Seric 	{
129017468Seric 		char qf[20];
129117468Seric 
129217468Seric 		/* find a unique id */
129317468Seric 		if (pid != getpid())
129417468Seric 		{
129517468Seric 			/* new process -- start back at "AA" */
129617468Seric 			pid = getpid();
129758716Seric 			now = curtime();
129858716Seric 			tm = localtime(&now);
129958716Seric 			c0 = 'A' + tm->tm_hour;
130017468Seric 			c1 = 'A';
130117468Seric 			c2 = 'A' - 1;
130217468Seric 		}
130358716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130417468Seric 
130517468Seric 		while (c1 < '~' || c2 < 'Z')
130617468Seric 		{
130717468Seric 			int i;
130817468Seric 
130917468Seric 			if (c2 >= 'Z')
131017468Seric 			{
131117468Seric 				c1++;
131217468Seric 				c2 = 'A' - 1;
131317468Seric 			}
131458716Seric 			qf[3] = c1;
131558716Seric 			qf[4] = ++c2;
131617468Seric 			if (tTd(7, 20))
131740934Srick 				printf("queuename: trying \"%s\"\n", qf);
131817468Seric 
131940934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
132051920Seric 			if (i < 0)
132151920Seric 			{
132251920Seric 				if (errno == EEXIST)
132351920Seric 					continue;
132451920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132551920Seric 					qf, QueueDir);
132651920Seric 				exit(EX_UNAVAILABLE);
132751920Seric 			}
132858689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
132951920Seric 			{
133051920Seric 				e->e_lockfp = fdopen(i, "w");
133140934Srick 				break;
133217468Seric 			}
133351920Seric 
133451920Seric 			/* a reader got the file; abandon it and try again */
133551920Seric 			(void) close(i);
133617468Seric 		}
133717468Seric 		if (c1 >= '~' && c2 >= 'Z')
133817468Seric 		{
133917468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
134017468Seric 				qf, QueueDir);
134117468Seric 			exit(EX_OSERR);
134217468Seric 		}
134317468Seric 		e->e_id = newstr(&qf[2]);
134417468Seric 		define('i', e->e_id, e);
134517468Seric 		if (tTd(7, 1))
134617468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
134717468Seric # ifdef LOG
134858020Seric 		if (LogLevel > 93)
134917468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
135056795Seric # endif /* LOG */
135117468Seric 	}
135217468Seric 
135317468Seric 	if (type == '\0')
135417468Seric 		return (NULL);
135517468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
135617468Seric 	if (tTd(7, 2))
135717468Seric 		printf("queuename: %s\n", buf);
135817468Seric 	return (buf);
135917468Seric }
136017468Seric /*
136117468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
136217468Seric **
136317468Seric **	Parameters:
136417468Seric **		e -- the envelope to unlock.
136517468Seric **
136617468Seric **	Returns:
136717468Seric **		none
136817468Seric **
136917468Seric **	Side Effects:
137017468Seric **		unlocks the queue for `e'.
137117468Seric */
137217468Seric 
137317468Seric unlockqueue(e)
137417468Seric 	ENVELOPE *e;
137517468Seric {
137658680Seric 	if (tTd(51, 4))
137758680Seric 		printf("unlockqueue(%s)\n", e->e_id);
137858680Seric 
137951920Seric 	/* if there is a lock file in the envelope, close it */
138051920Seric 	if (e->e_lockfp != NULL)
138158680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
138251920Seric 	e->e_lockfp = NULL;
138351920Seric 
138458728Seric 	/* don't create a queue id if we don't already have one */
138558728Seric 	if (e->e_id == NULL)
138658728Seric 		return;
138758728Seric 
138817468Seric 	/* remove the transcript */
138917468Seric # ifdef LOG
139058020Seric 	if (LogLevel > 87)
139117468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
139256795Seric # endif /* LOG */
139358680Seric 	if (!tTd(51, 104))
139417468Seric 		xunlink(queuename(e, 'x'));
139517468Seric 
139617468Seric }
139740973Sbostic /*
139854974Seric **  SETCTLUSER -- create a controlling address
139940973Sbostic **
140054974Seric **	Create a fake "address" given only a local login name; this is
140154974Seric **	used as a "controlling user" for future recipient addresses.
140240973Sbostic **
140340973Sbostic **	Parameters:
140454974Seric **		user -- the user name of the controlling user.
140540973Sbostic **
140640973Sbostic **	Returns:
140754974Seric **		An address descriptor for the controlling user.
140840973Sbostic **
140940973Sbostic **	Side Effects:
141040973Sbostic **		none.
141140973Sbostic */
141240973Sbostic 
141354974Seric ADDRESS *
141454974Seric setctluser(user)
141554974Seric 	char *user;
141640973Sbostic {
141754974Seric 	register ADDRESS *a;
141840973Sbostic 	struct passwd *pw;
141959113Seric 	char *p;
142040973Sbostic 
142140973Sbostic 	/*
142254974Seric 	**  See if this clears our concept of controlling user.
142340973Sbostic 	*/
142440973Sbostic 
142559270Seric 	if (user == NULL)
142659270Seric 		user = "";
142740973Sbostic 
142840973Sbostic 	/*
142954974Seric 	**  Set up addr fields for controlling user.
143040973Sbostic 	*/
143140973Sbostic 
143254974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143354974Seric 	bzero((char *) a, sizeof *a);
143459113Seric 
143559113Seric 	p = strchr(user, ':');
143659113Seric 	if (p != NULL)
143759113Seric 		*p++ = '\0';
143859270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
143940973Sbostic 	{
144040973Sbostic 		a->q_home = newstr(pw->pw_dir);
144140973Sbostic 		a->q_uid = pw->pw_uid;
144240973Sbostic 		a->q_gid = pw->pw_gid;
144357642Seric 		a->q_user = newstr(user);
144459270Seric 		a->q_flags |= QGOODUID;
144540973Sbostic 	}
144640973Sbostic 	else
144740973Sbostic 	{
144857642Seric 		a->q_user = newstr(DefUser);
144940973Sbostic 	}
145040973Sbostic 
145159270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
145256328Seric 	a->q_mailer = LocalMailer;
145359113Seric 	if (p == NULL)
145459113Seric 		a->q_paddr = a->q_user;
145559113Seric 	else
145659113Seric 		a->q_paddr = newstr(p);
145754974Seric 	return a;
145840973Sbostic }
1459