xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64771)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362528Sbostic  * Copyright (c) 1988, 1993
462528Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*64771Seric static char sccsid[] = "@(#)queue.c	8.27 (Berkeley) 10/29/93 (with queueing)";
1433731Sbostic #else
15*64771Seric static char sccsid[] = "@(#)queue.c	8.27 (Berkeley) 10/29/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7151920Seric 	char buf[MAXLINE], tf[MAXLINE];
724632Seric 
735037Seric 	/*
7417477Seric 	**  Create control file.
755037Seric 	*/
764632Seric 
7764745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
7864277Seric 
7964277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8051920Seric 	strcpy(tf, queuename(e, 't'));
8151920Seric 	tfp = e->e_lockfp;
8251920Seric 	if (tfp == NULL)
8351920Seric 		newid = FALSE;
8464277Seric 
8564277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
8664745Seric 	if (!newid)
8751835Seric 	{
8851920Seric 		/* get a locked tf file */
8964070Seric 		for (i = 0; i < 128; i++)
9051835Seric 		{
9151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9251920Seric 			if (fd < 0)
9351835Seric 			{
9464070Seric 				if (errno != EEXIST)
9564070Seric 					break;
9664070Seric #ifdef LOG
9764070Seric 				if (LogLevel > 0 && (i % 32) == 0)
9864070Seric 					syslog(LOG_ALERT, "queueup: cannot create %s: %s",
9964070Seric 						tf, errstring(errno));
10064070Seric #endif
10164070Seric 				continue;
10241636Srick 			}
10358689Seric 
10464335Seric 			if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10551920Seric 				break;
10664070Seric #ifdef LOG
10764070Seric 			else if (LogLevel > 0 && (i % 32) == 0)
10864070Seric 				syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
10964070Seric 					tf, errstring(errno));
11064070Seric #endif
11158689Seric 
11251920Seric 			close(fd);
11364070Seric 
11464070Seric 			if ((i % 32) == 31)
11564070Seric 			{
11664070Seric 				/* save the old temp file away */
11764070Seric 				(void) rename(tf, queuename(e, 'T'));
11864070Seric 			}
11964070Seric 			else
12064070Seric 				sleep(i % 32);
12141636Srick 		}
12264724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12364724Seric 			syserr("!queueup: cannot create queue temp file %s", tf);
12451920Seric 	}
1254632Seric 
1267677Seric 	if (tTd(40, 1))
12764745Seric 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
12864745Seric 			newid ? " (new id)" : "");
12964745Seric 	if (tTd(40, 9))
13064745Seric 	{
13164745Seric 		printf("  tfp=");
13264745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
13364745Seric 		printf("  lockfp=");
13464745Seric 		if (e->e_lockfp == NULL)
13564745Seric 			printf("NULL\n");
13664745Seric 		else
13764745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
13864745Seric 	}
1394632Seric 
1404632Seric 	/*
1416980Seric 	**  If there is no data file yet, create one.
1426980Seric 	*/
1436980Seric 
1446980Seric 	if (e->e_df == NULL)
1456980Seric 	{
1466980Seric 		register FILE *dfp;
1479389Seric 		extern putbody();
1486980Seric 
14964086Seric 		e->e_df = queuename(e, 'd');
15064086Seric 		e->e_df = newstr(e->e_df);
15140934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15264724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
15364724Seric 			syserr("!queueup: cannot create data temp file %s",
15464724Seric 				e->e_df);
15559730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
15658680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1579389Seric 		e->e_putbody = putbody;
1586980Seric 	}
1596980Seric 
1606980Seric 	/*
1614632Seric 	**  Output future work requests.
16225687Seric 	**	Priority and creation time should be first, since
16325687Seric 	**	they are required by orderq.
1644632Seric 	*/
1654632Seric 
1669377Seric 	/* output message priority */
1679377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1689377Seric 
1699630Seric 	/* output creation time */
1709630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1719630Seric 
17259093Seric 	/* output type and name of data file */
17359093Seric 	if (e->e_bodytype != NULL)
17459093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1757812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1764632Seric 
17710108Seric 	/* message from envelope, if it exists */
17810108Seric 	if (e->e_message != NULL)
17910108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18010108Seric 
18158737Seric 	/* send various flag bits through */
18258737Seric 	p = buf;
18358737Seric 	if (bitset(EF_WARNING, e->e_flags))
18458737Seric 		*p++ = 'w';
18558737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
18658737Seric 		*p++ = 'r';
18758737Seric 	*p++ = '\0';
18858737Seric 	if (buf[0] != '\0')
18958737Seric 		fprintf(tfp, "F%s\n", buf);
19058737Seric 
19158957Seric 	/* $r and $s and $_ macro values */
19253400Seric 	if ((p = macvalue('r', e)) != NULL)
19353400Seric 		fprintf(tfp, "$r%s\n", p);
19453400Seric 	if ((p = macvalue('s', e)) != NULL)
19553400Seric 		fprintf(tfp, "$s%s\n", p);
19658957Seric 	if ((p = macvalue('_', e)) != NULL)
19758957Seric 		fprintf(tfp, "$_%s\n", p);
19853400Seric 
1994632Seric 	/* output name of sender */
2007812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
2014632Seric 
20255360Seric 	/* output list of error recipients */
20359670Seric 	printctladdr(NULL, NULL);
20455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20555360Seric 	{
20658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
20755360Seric 		{
20859113Seric 			printctladdr(q, tfp);
20955360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21055360Seric 		}
21155360Seric 	}
21255360Seric 
2134632Seric 	/* output list of recipient addresses */
2146980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2154632Seric 	{
21658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2188245Seric 		{
21959113Seric 			printctladdr(q, tfp);
2207812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2219377Seric 			if (announce)
2229377Seric 			{
2239377Seric 				e->e_to = q->q_paddr;
22458151Seric 				message("queued");
22558020Seric 				if (LogLevel > 8)
226*64771Seric 					logdelivery(NULL, NULL, "queued", NULL, e);
2279377Seric 				e->e_to = NULL;
2289377Seric 			}
2299387Seric 			if (tTd(40, 1))
2309387Seric 			{
2319387Seric 				printf("queueing ");
2329387Seric 				printaddr(q, FALSE);
2339387Seric 			}
2348245Seric 		}
2354632Seric 	}
2364632Seric 
2379377Seric 	/*
2389377Seric 	**  Output headers for this message.
2399377Seric 	**	Expand macros completely here.  Queue run will deal with
2409377Seric 	**	everything as absolute headers.
2419377Seric 	**		All headers that must be relative to the recipient
2429377Seric 	**		can be cracked later.
24310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24410173Seric 	**	no effect on the addresses as they are output.
2459377Seric 	*/
2469377Seric 
24710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24964746Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = 0;
25010349Seric 	nullmailer.m_eol = "\n";
25110173Seric 
25258050Seric 	define('g', "\201f", e);
2536980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2544632Seric 	{
25510686Seric 		extern bool bitzerop();
25610686Seric 
25712015Seric 		/* don't output null headers */
2584632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2594632Seric 			continue;
26012015Seric 
26112015Seric 		/* don't output resent headers on non-resent messages */
26212015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
26312015Seric 			continue;
26412015Seric 
26512015Seric 		/* output this header */
2667812Seric 		fprintf(tfp, "H");
26712015Seric 
26812015Seric 		/* if conditional, output the set of conditions */
26910686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
27010686Seric 		{
27110686Seric 			int j;
27210686Seric 
27323098Seric 			(void) putc('?', tfp);
27410686Seric 			for (j = '\0'; j <= '\177'; j++)
27510686Seric 				if (bitnset(j, h->h_mflags))
27623098Seric 					(void) putc(j, tfp);
27723098Seric 			(void) putc('?', tfp);
27810686Seric 		}
27912015Seric 
28012015Seric 		/* output the header: expand macros, convert addresses */
2817763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2827763Seric 		{
2837763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2848236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2857763Seric 		}
2868245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2879348Seric 		{
28858737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28963753Seric 			FILE *savetrace = TrafficLogFile;
29058737Seric 
29163753Seric 			TrafficLogFile = NULL;
29263753Seric 
29358737Seric 			if (bitset(H_FROM, h->h_flags))
29458737Seric 				oldstyle = FALSE;
29558737Seric 
29658737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29755012Seric 				 &nullmailer, e);
29863753Seric 
29963753Seric 			TrafficLogFile = savetrace;
3009348Seric 		}
3017763Seric 		else
3028245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3034632Seric 	}
3044632Seric 
3054632Seric 	/*
3064632Seric 	**  Clean up.
3074632Seric 	*/
3084632Seric 
30964762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
31058732Seric 	{
31158732Seric 		if (newid)
31258732Seric 			syserr("!552 Error writing control file %s", tf);
31358732Seric 		else
31458732Seric 			syserr("!452 Error writing control file %s", tf);
31558732Seric 	}
31658732Seric 
31751920Seric 	if (!newid)
31851920Seric 	{
31964277Seric 		/* rename (locked) tf to be (locked) qf */
32051920Seric 		qf = queuename(e, 'q');
32151920Seric 		if (rename(tf, qf) < 0)
32251920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
32364277Seric 
32464277Seric 		/* close and unlock old (locked) qf */
32551920Seric 		if (e->e_lockfp != NULL)
32658680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
32751920Seric 		e->e_lockfp = tfp;
32851920Seric 	}
32951920Seric 	else
33051920Seric 		qf = tf;
33141636Srick 	errno = 0;
33264745Seric 	e->e_flags |= EF_INQUEUE;
3337391Seric 
3347677Seric # ifdef LOG
3357677Seric 	/* save log info */
33658020Seric 	if (LogLevel > 79)
3377878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33856795Seric # endif /* LOG */
33964307Seric 
34064307Seric 	if (tTd(40, 1))
34164307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
34251920Seric 	return;
3434632Seric }
34454974Seric 
34554974Seric printctladdr(a, tfp)
34659113Seric 	register ADDRESS *a;
34754974Seric 	FILE *tfp;
34854974Seric {
34959113Seric 	char *uname;
35059113Seric 	register struct passwd *pw;
35159113Seric 	register ADDRESS *q;
35259113Seric 	uid_t uid;
35359113Seric 	static ADDRESS *lastctladdr;
35459113Seric 	static uid_t lastuid;
35554974Seric 
35659113Seric 	/* initialization */
35763850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
35854974Seric 	{
35959670Seric 		if (lastctladdr != NULL && tfp != NULL)
36059113Seric 			fprintf(tfp, "C\n");
36159113Seric 		lastctladdr = NULL;
36259113Seric 		lastuid = 0;
36354974Seric 		return;
36454974Seric 	}
36559113Seric 
36659113Seric 	/* find the active uid */
36759113Seric 	q = getctladdr(a);
36859113Seric 	if (q == NULL)
36959113Seric 		uid = 0;
37054974Seric 	else
37159113Seric 		uid = q->q_uid;
37263850Seric 	a = a->q_alias;
37359113Seric 
37459113Seric 	/* check to see if this is the same as last time */
37559113Seric 	if (lastctladdr != NULL && uid == lastuid &&
37659113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
37759113Seric 		return;
37859113Seric 	lastuid = uid;
37959113Seric 	lastctladdr = a;
38059113Seric 
38159113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
38259270Seric 		uname = "";
38359113Seric 	else
38459113Seric 		uname = pw->pw_name;
38559113Seric 
38659113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
38754974Seric }
38854974Seric 
3894632Seric /*
3904632Seric **  RUNQUEUE -- run the jobs in the queue.
3914632Seric **
3924632Seric **	Gets the stuff out of the queue in some presumably logical
3934632Seric **	order and processes them.
3944632Seric **
3954632Seric **	Parameters:
39624941Seric **		forkflag -- TRUE if the queue scanning should be done in
39724941Seric **			a child process.  We double-fork so it is not our
39824941Seric **			child and we don't have to clean up after it.
3994632Seric **
4004632Seric **	Returns:
4014632Seric **		none.
4024632Seric **
4034632Seric **	Side Effects:
4044632Seric **		runs things in the mail queue.
4054632Seric */
4064632Seric 
40755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
40855360Seric 
40955360Seric runqueue(forkflag)
4104639Seric 	bool forkflag;
4114632Seric {
41255360Seric 	register ENVELOPE *e;
41355360Seric 	extern ENVELOPE BlankEnvelope;
41424953Seric 
4157466Seric 	/*
41624953Seric 	**  If no work will ever be selected, don't even bother reading
41724953Seric 	**  the queue.
41824953Seric 	*/
41924953Seric 
42051920Seric 	CurrentLA = getla();	/* get load average */
42140934Srick 
42258132Seric 	if (shouldqueue(0L, curtime()))
42324953Seric 	{
42424953Seric 		if (Verbose)
42524953Seric 			printf("Skipping queue run -- load average too high\n");
42658107Seric 		if (forkflag && QueueIntvl != 0)
42758107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
42855360Seric 		return;
42924953Seric 	}
43024953Seric 
43124953Seric 	/*
4327466Seric 	**  See if we want to go off and do other useful work.
4337466Seric 	*/
4344639Seric 
4354639Seric 	if (forkflag)
4364639Seric 	{
4377943Seric 		int pid;
4387943Seric 
4397943Seric 		pid = dofork();
4407943Seric 		if (pid != 0)
4414639Seric 		{
44246928Sbostic 			extern void reapchild();
44325184Seric 
4447943Seric 			/* parent -- pick up intermediate zombie */
44525184Seric #ifndef SIGCHLD
4469377Seric 			(void) waitfor(pid);
44756795Seric #else /* SIGCHLD */
44864035Seric 			(void) setsignal(SIGCHLD, reapchild);
44956795Seric #endif /* SIGCHLD */
4507690Seric 			if (QueueIntvl != 0)
4519348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4524639Seric 			return;
4534639Seric 		}
4547943Seric 		/* child -- double fork */
45525184Seric #ifndef SIGCHLD
4567943Seric 		if (fork() != 0)
4577943Seric 			exit(EX_OK);
45856795Seric #else /* SIGCHLD */
45964035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
46056795Seric #endif /* SIGCHLD */
4614639Seric 	}
46224941Seric 
46340934Srick 	setproctitle("running queue: %s", QueueDir);
46424941Seric 
4657876Seric # ifdef LOG
46658020Seric 	if (LogLevel > 69)
46755360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
46855360Seric 			QueueDir, getpid(), forkflag);
46956795Seric # endif /* LOG */
4704639Seric 
4717466Seric 	/*
47210205Seric 	**  Release any resources used by the daemon code.
47310205Seric 	*/
47410205Seric 
47510205Seric # ifdef DAEMON
47610205Seric 	clrdaemon();
47756795Seric # endif /* DAEMON */
47810205Seric 
47964658Seric 	/* force it to run expensive jobs */
48064658Seric 	NoConnect = FALSE;
48164658Seric 
48210205Seric 	/*
48355360Seric 	**  Create ourselves an envelope
48455360Seric 	*/
48555360Seric 
48655360Seric 	CurEnv = &QueueEnvelope;
48758179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
48855360Seric 	e->e_flags = BlankEnvelope.e_flags;
48955360Seric 
49055360Seric 	/*
49127175Seric 	**  Make sure the alias database is open.
49227175Seric 	*/
49327175Seric 
49460537Seric 	initmaps(FALSE, e);
49527175Seric 
49627175Seric 	/*
4977466Seric 	**  Start making passes through the queue.
4987466Seric 	**	First, read and sort the entire queue.
4997466Seric 	**	Then, process the work in that order.
5007466Seric 	**		But if you take too long, start over.
5017466Seric 	*/
5027466Seric 
5037943Seric 	/* order the existing work requests */
50424954Seric 	(void) orderq(FALSE);
5057690Seric 
5067943Seric 	/* process them once at a time */
5077943Seric 	while (WorkQ != NULL)
5084639Seric 	{
5097943Seric 		WORK *w = WorkQ;
5107881Seric 
5117943Seric 		WorkQ = WorkQ->w_next;
51258884Seric 
51358884Seric 		/*
51458884Seric 		**  Ignore jobs that are too expensive for the moment.
51558884Seric 		*/
51658884Seric 
51758884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
51858884Seric 		{
51958884Seric 			if (Verbose)
52058884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
52158884Seric 		}
52258930Seric 		else
52358930Seric 		{
52464296Seric 			pid_t pid;
52564296Seric 			extern pid_t dowork();
52664296Seric 
52764296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
52864296Seric 			errno = 0;
52964296Seric 			(void) waitfor(pid);
53058930Seric 		}
5317943Seric 		free(w->w_name);
5327943Seric 		free((char *) w);
5334639Seric 	}
53429866Seric 
53529866Seric 	/* exit without the usual cleanup */
53655467Seric 	e->e_id = NULL;
53755467Seric 	finis();
5384634Seric }
5394634Seric /*
5404632Seric **  ORDERQ -- order the work queue.
5414632Seric **
5424632Seric **	Parameters:
54324941Seric **		doall -- if set, include everything in the queue (even
54424941Seric **			the jobs that cannot be run because the load
54524941Seric **			average is too high).  Otherwise, exclude those
54624941Seric **			jobs.
5474632Seric **
5484632Seric **	Returns:
54910121Seric **		The number of request in the queue (not necessarily
55010121Seric **		the number of requests in WorkQ however).
5514632Seric **
5524632Seric **	Side Effects:
5534632Seric **		Sets WorkQ to the queue of available work, in order.
5544632Seric */
5554632Seric 
55625687Seric # define NEED_P		001
55725687Seric # define NEED_T		002
55858318Seric # define NEED_R		004
55958318Seric # define NEED_S		010
5604632Seric 
56124941Seric orderq(doall)
56224941Seric 	bool doall;
5634632Seric {
56460219Seric 	register struct dirent *d;
5654632Seric 	register WORK *w;
5666625Sglickman 	DIR *f;
5674632Seric 	register int i;
56825687Seric 	WORK wlist[QUEUESIZE+1];
56910070Seric 	int wn = -1;
5704632Seric 	extern workcmpf();
5714632Seric 
57258318Seric 	if (tTd(41, 1))
57358318Seric 	{
57458318Seric 		printf("orderq:\n");
57558318Seric 		if (QueueLimitId != NULL)
57658318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
57758318Seric 		if (QueueLimitSender != NULL)
57858318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
57958318Seric 		if (QueueLimitRecipient != NULL)
58058318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
58158318Seric 	}
58258318Seric 
5834632Seric 	/* clear out old WorkQ */
5844632Seric 	for (w = WorkQ; w != NULL; )
5854632Seric 	{
5864632Seric 		register WORK *nw = w->w_next;
5874632Seric 
5884632Seric 		WorkQ = nw;
5894632Seric 		free(w->w_name);
5904632Seric 		free((char *) w);
5914632Seric 		w = nw;
5924632Seric 	}
5934632Seric 
5944632Seric 	/* open the queue directory */
5958148Seric 	f = opendir(".");
5964632Seric 	if (f == NULL)
5974632Seric 	{
5988148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
59910070Seric 		return (0);
6004632Seric 	}
6014632Seric 
6024632Seric 	/*
6034632Seric 	**  Read the work directory.
6044632Seric 	*/
6054632Seric 
60610070Seric 	while ((d = readdir(f)) != NULL)
6074632Seric 	{
6089377Seric 		FILE *cf;
60964492Seric 		register char *p;
6104632Seric 		char lbuf[MAXNAME];
61158318Seric 		extern bool strcontainedin();
6124632Seric 
6134632Seric 		/* is this an interesting entry? */
6147812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6154632Seric 			continue;
6164632Seric 
61758318Seric 		if (QueueLimitId != NULL &&
61858318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
61958318Seric 			continue;
62058318Seric 
62158722Seric 		/*
62258722Seric 		**  Check queue name for plausibility.  This handles
62358722Seric 		**  both old and new type ids.
62458722Seric 		*/
62558722Seric 
62664492Seric 		p = d->d_name + 2;
62764492Seric 		if (isupper(p[0]) && isupper(p[2]))
62864492Seric 			p += 3;
62964492Seric 		else if (isupper(p[1]))
63064492Seric 			p += 2;
63164492Seric 		else
63264492Seric 			p = d->d_name;
63364492Seric 		for (i = 0; isdigit(*p); p++)
63464492Seric 			i++;
63564492Seric 		if (i < 5 || *p != '\0')
63658020Seric 		{
63758020Seric 			if (Verbose)
63858020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
63958020Seric #ifdef LOG
64058020Seric 			if (LogLevel > 3)
64159615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
64258020Seric 					d->d_name);
64358020Seric #endif
64458020Seric 			if (strlen(d->d_name) >= MAXNAME)
64558020Seric 				d->d_name[MAXNAME - 1] = '\0';
64658020Seric 			strcpy(lbuf, d->d_name);
64758020Seric 			lbuf[0] = 'Q';
64858020Seric 			(void) rename(d->d_name, lbuf);
64958020Seric 			continue;
65058020Seric 		}
65158020Seric 
65210070Seric 		/* yes -- open control file (if not too many files) */
65325687Seric 		if (++wn >= QUEUESIZE)
65410070Seric 			continue;
65558318Seric 
6568148Seric 		cf = fopen(d->d_name, "r");
6574632Seric 		if (cf == NULL)
6584632Seric 		{
6597055Seric 			/* this may be some random person sending hir msgs */
6607055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
66110090Seric 			if (tTd(41, 2))
66210090Seric 				printf("orderq: cannot open %s (%d)\n",
66310090Seric 					d->d_name, errno);
6647055Seric 			errno = 0;
66510090Seric 			wn--;
6664632Seric 			continue;
6674632Seric 		}
66825687Seric 		w = &wlist[wn];
66925687Seric 		w->w_name = newstr(d->d_name);
6704632Seric 
67125027Seric 		/* make sure jobs in creation don't clog queue */
67225687Seric 		w->w_pri = 0x7fffffff;
67325687Seric 		w->w_ctime = 0;
67425027Seric 
6754632Seric 		/* extract useful information */
67625687Seric 		i = NEED_P | NEED_T;
67758318Seric 		if (QueueLimitSender != NULL)
67858318Seric 			i |= NEED_S;
67958318Seric 		if (QueueLimitRecipient != NULL)
68058318Seric 			i |= NEED_R;
68125687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6824632Seric 		{
68324954Seric 			extern long atol();
68458318Seric 			extern bool strcontainedin();
68524954Seric 
68624941Seric 			switch (lbuf[0])
6874632Seric 			{
68824941Seric 			  case 'P':
68925687Seric 				w->w_pri = atol(&lbuf[1]);
69025687Seric 				i &= ~NEED_P;
6914632Seric 				break;
69225013Seric 
69325013Seric 			  case 'T':
69425687Seric 				w->w_ctime = atol(&lbuf[1]);
69525687Seric 				i &= ~NEED_T;
69625013Seric 				break;
69758318Seric 
69858318Seric 			  case 'R':
69958318Seric 				if (QueueLimitRecipient != NULL &&
70058318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
70158318Seric 					i &= ~NEED_R;
70258318Seric 				break;
70358318Seric 
70458318Seric 			  case 'S':
70558318Seric 				if (QueueLimitSender != NULL &&
70658318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
70758318Seric 					i &= ~NEED_S;
70858318Seric 				break;
7094632Seric 			}
7104632Seric 		}
7114632Seric 		(void) fclose(cf);
71224953Seric 
71358318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
71458318Seric 		    bitset(NEED_R|NEED_S, i))
71524953Seric 		{
71624953Seric 			/* don't even bother sorting this job in */
71724953Seric 			wn--;
71824953Seric 		}
7194632Seric 	}
7206625Sglickman 	(void) closedir(f);
72110090Seric 	wn++;
7224632Seric 
7234632Seric 	/*
7244632Seric 	**  Sort the work directory.
7254632Seric 	*/
7264632Seric 
72725687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7284632Seric 
7294632Seric 	/*
7304632Seric 	**  Convert the work list into canonical form.
7319377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7324632Seric 	*/
7334632Seric 
73424981Seric 	WorkQ = NULL;
73525687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7364632Seric 	{
7374632Seric 		w = (WORK *) xalloc(sizeof *w);
7384632Seric 		w->w_name = wlist[i].w_name;
7394632Seric 		w->w_pri = wlist[i].w_pri;
74025013Seric 		w->w_ctime = wlist[i].w_ctime;
74124981Seric 		w->w_next = WorkQ;
74224981Seric 		WorkQ = w;
7434632Seric 	}
7444632Seric 
7457677Seric 	if (tTd(40, 1))
7464632Seric 	{
7474632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7485037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7494632Seric 	}
75010070Seric 
75110090Seric 	return (wn);
7524632Seric }
7534632Seric /*
7547677Seric **  WORKCMPF -- compare function for ordering work.
7554632Seric **
7564632Seric **	Parameters:
7574632Seric **		a -- the first argument.
7584632Seric **		b -- the second argument.
7594632Seric **
7604632Seric **	Returns:
76124981Seric **		-1 if a < b
76224981Seric **		 0 if a == b
76324981Seric **		+1 if a > b
7644632Seric **
7654632Seric **	Side Effects:
7664632Seric **		none.
7674632Seric */
7684632Seric 
7694632Seric workcmpf(a, b)
7705037Seric 	register WORK *a;
7715037Seric 	register WORK *b;
7724632Seric {
77357438Seric 	long pa = a->w_pri;
77457438Seric 	long pb = b->w_pri;
77524941Seric 
77624941Seric 	if (pa == pb)
7774632Seric 		return (0);
77824941Seric 	else if (pa > pb)
77924981Seric 		return (1);
78024981Seric 	else
78110121Seric 		return (-1);
7824632Seric }
7834632Seric /*
7844632Seric **  DOWORK -- do a work request.
7854632Seric **
7864632Seric **	Parameters:
78758884Seric **		id -- the ID of the job to run.
78858884Seric **		forkflag -- if set, run this in background.
78958924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
79058924Seric **			This is used when expanding aliases in the queue.
79161094Seric **			If forkflag is also set, it doesn't wait for the
79261094Seric **			child.
79358884Seric **		e - the envelope in which to run it.
7944632Seric **
7954632Seric **	Returns:
79664296Seric **		process id of process that is running the queue job.
7974632Seric **
7984632Seric **	Side Effects:
7994632Seric **		The work request is satisfied if possible.
8004632Seric */
8014632Seric 
80264296Seric pid_t
80358924Seric dowork(id, forkflag, requeueflag, e)
80458884Seric 	char *id;
80558884Seric 	bool forkflag;
80658924Seric 	bool requeueflag;
80755012Seric 	register ENVELOPE *e;
8084632Seric {
80964296Seric 	register pid_t pid;
81051920Seric 	extern bool readqf();
8114632Seric 
8127677Seric 	if (tTd(40, 1))
81358884Seric 		printf("dowork(%s)\n", id);
8144632Seric 
8154632Seric 	/*
81624941Seric 	**  Fork for work.
81724941Seric 	*/
81824941Seric 
81958884Seric 	if (forkflag)
82024941Seric 	{
82164296Seric 		pid = fork();
82264296Seric 		if (pid < 0)
82324941Seric 		{
82424941Seric 			syserr("dowork: cannot fork");
82564296Seric 			return 0;
82624941Seric 		}
82724941Seric 	}
82824941Seric 	else
82924941Seric 	{
83064296Seric 		pid = 0;
83124941Seric 	}
83224941Seric 
83364296Seric 	if (pid == 0)
8344632Seric 	{
8354632Seric 		/*
8364632Seric 		**  CHILD
8378148Seric 		**	Lock the control file to avoid duplicate deliveries.
8388148Seric 		**		Then run the file as though we had just read it.
8397350Seric 		**	We save an idea of the temporary name so we
8407350Seric 		**		can recover on interrupt.
8414632Seric 		*/
8424632Seric 
8437763Seric 		/* set basic modes, etc. */
8447356Seric 		(void) alarm(0);
84555012Seric 		clearenvelope(e, FALSE);
84664554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
84758734Seric 		e->e_errormode = EM_MAIL;
84858884Seric 		e->e_id = id;
84964765Seric 		GrabTo = UseErrorsTo = FALSE;
85063846Seric 		if (forkflag)
85164554Seric 		{
85264296Seric 			disconnect(1, e);
85364554Seric 			OpMode = MD_DELIVER;
85464554Seric 		}
8557876Seric # ifdef LOG
85658020Seric 		if (LogLevel > 76)
85755012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8587881Seric 			       getpid());
85956795Seric # endif /* LOG */
8607763Seric 
8617763Seric 		/* don't use the headers from sendmail.cf... */
86255012Seric 		e->e_header = NULL;
8637763Seric 
86451920Seric 		/* read the queue control file -- return if locked */
86563850Seric 		if (!readqf(e, !requeueflag))
8666980Seric 		{
86758884Seric 			if (tTd(40, 4))
86858884Seric 				printf("readqf(%s) failed\n", e->e_id);
86958884Seric 			if (forkflag)
87024941Seric 				exit(EX_OK);
87124941Seric 			else
87224941Seric 				return;
8736980Seric 		}
8746980Seric 
87555012Seric 		e->e_flags |= EF_INQUEUE;
87658929Seric 		eatheader(e, requeueflag);
8776980Seric 
87858924Seric 		if (requeueflag)
87958924Seric 			queueup(e, TRUE, FALSE);
88058924Seric 
8816980Seric 		/* do the delivery */
88258915Seric 		sendall(e, SM_DELIVER);
8836980Seric 
8846980Seric 		/* finish up and exit */
88558884Seric 		if (forkflag)
88624941Seric 			finis();
88724941Seric 		else
88855012Seric 			dropenvelope(e);
8894632Seric 	}
89064296Seric 	e->e_id = NULL;
89164296Seric 	return pid;
8924632Seric }
8934632Seric /*
8944632Seric **  READQF -- read queue file and set up environment.
8954632Seric **
8964632Seric **	Parameters:
8979377Seric **		e -- the envelope of the job to run.
89863850Seric **		announcefile -- if set, announce the name of the queue
89963850Seric **			file in error messages.
9004632Seric **
9014632Seric **	Returns:
90251920Seric **		TRUE if it successfully read the queue file.
90351920Seric **		FALSE otherwise.
9044632Seric **
9054632Seric **	Side Effects:
90651920Seric **		The queue file is returned locked.
9074632Seric */
9084632Seric 
90951920Seric bool
91063850Seric readqf(e, announcefile)
9119377Seric 	register ENVELOPE *e;
91263850Seric 	bool announcefile;
9134632Seric {
91417477Seric 	register FILE *qfp;
91554974Seric 	ADDRESS *ctladdr;
91656400Seric 	struct stat st;
91757135Seric 	char *bp;
91858915Seric 	char qf[20];
91957135Seric 	char buf[MAXLINE];
92024954Seric 	extern long atol();
92154974Seric 	extern ADDRESS *setctluser();
9224632Seric 
9234632Seric 	/*
92417468Seric 	**  Read and process the file.
9254632Seric 	*/
9264632Seric 
92758915Seric 	strcpy(qf, queuename(e, 'q'));
92851937Seric 	qfp = fopen(qf, "r+");
92917477Seric 	if (qfp == NULL)
93017477Seric 	{
93158884Seric 		if (tTd(40, 8))
93258884Seric 			printf("readqf(%s): fopen failure (%s)\n",
93358884Seric 				qf, errstring(errno));
93440934Srick 		if (errno != ENOENT)
93540934Srick 			syserr("readqf: no control file %s", qf);
93651920Seric 		return FALSE;
93717477Seric 	}
93840934Srick 
93964335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
94064277Seric 	{
94164277Seric 		/* being processed by another queuer */
94264277Seric 		if (tTd(40, 8))
94364277Seric 			printf("readqf(%s): locked\n", qf);
94464277Seric 		if (Verbose)
94564277Seric 			printf("%s: locked\n", e->e_id);
94664277Seric # ifdef LOG
94764277Seric 		if (LogLevel > 19)
94864277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
94964277Seric # endif /* LOG */
95064277Seric 		(void) fclose(qfp);
95164277Seric 		return FALSE;
95264277Seric 	}
95364277Seric 
95456400Seric 	/*
95556400Seric 	**  Check the queue file for plausibility to avoid attacks.
95656400Seric 	*/
95756400Seric 
95856400Seric 	if (fstat(fileno(qfp), &st) < 0)
95956400Seric 	{
96056400Seric 		/* must have been being processed by someone else */
96158884Seric 		if (tTd(40, 8))
96258884Seric 			printf("readqf(%s): fstat failure (%s)\n",
96358884Seric 				qf, errstring(errno));
96456400Seric 		fclose(qfp);
96556400Seric 		return FALSE;
96656400Seric 	}
96756400Seric 
96864137Seric 	if (st.st_uid != geteuid())
96956400Seric 	{
97056400Seric # ifdef LOG
97156400Seric 		if (LogLevel > 0)
97256400Seric 		{
97356400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
97456400Seric 				e->e_id, st.st_uid, st.st_mode);
97556400Seric 		}
97656795Seric # endif /* LOG */
97758884Seric 		if (tTd(40, 8))
97858884Seric 			printf("readqf(%s): bogus file\n", qf);
97964277Seric 		rename(qf, queuename(e, 'Q'));
98056400Seric 		fclose(qfp);
98156400Seric 		return FALSE;
98256400Seric 	}
98356400Seric 
98464277Seric 	if (st.st_size == 0)
98540934Srick 	{
98664277Seric 		/* must be a bogus file -- just remove it */
98764277Seric 		(void) unlink(qf);
98864277Seric 		fclose(qfp);
98951920Seric 		return FALSE;
99040934Srick 	}
99140934Srick 
99264277Seric 	if (st.st_nlink == 0)
99359101Seric 	{
99464277Seric 		/*
99564277Seric 		**  Race condition -- we got a file just as it was being
99664277Seric 		**  unlinked.  Just assume it is zero length.
99764277Seric 		*/
99864277Seric 
99959101Seric 		fclose(qfp);
100059101Seric 		return FALSE;
100159101Seric 	}
100259101Seric 
100364277Seric 	/* good file -- save this lock */
100451920Seric 	e->e_lockfp = qfp;
100551920Seric 
100640934Srick 	/* do basic system initialization */
100755012Seric 	initsys(e);
100840934Srick 
100963850Seric 	if (announcefile)
101063850Seric 		FileName = qf;
10119377Seric 	LineNumber = 0;
101263850Seric 	e->e_flags |= EF_GLOBALERRS;
101363850Seric 	OpMode = MD_DELIVER;
101451920Seric 	if (Verbose)
10159377Seric 		printf("\nRunning %s\n", e->e_id);
101654974Seric 	ctladdr = NULL;
101757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
10184632Seric 	{
101958737Seric 		register char *p;
102057529Seric 		struct stat st;
102157529Seric 
102226504Seric 		if (tTd(40, 4))
102357135Seric 			printf("+++++ %s\n", bp);
102457135Seric 		switch (bp[0])
10254632Seric 		{
102640973Sbostic 		  case 'C':		/* specify controlling user */
102757135Seric 			ctladdr = setctluser(&bp[1]);
102840973Sbostic 			break;
102940973Sbostic 
10304632Seric 		  case 'R':		/* specify recipient */
103158082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10324632Seric 			break;
10334632Seric 
103425687Seric 		  case 'E':		/* specify error recipient */
103558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
103625687Seric 			break;
103725687Seric 
10384632Seric 		  case 'H':		/* header */
103957135Seric 			(void) chompheader(&bp[1], FALSE, e);
10404632Seric 			break;
10414632Seric 
104210108Seric 		  case 'M':		/* message */
104364705Seric 			/* ignore this; we want a new message next time */
104410108Seric 			break;
104510108Seric 
10464632Seric 		  case 'S':		/* sender */
104758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10484632Seric 			break;
10494632Seric 
105059093Seric 		  case 'B':		/* body type */
105159093Seric 			e->e_bodytype = newstr(&bp[1]);
105259093Seric 			break;
105359093Seric 
10544632Seric 		  case 'D':		/* data file name */
105557135Seric 			e->e_df = newstr(&bp[1]);
10569544Seric 			e->e_dfp = fopen(e->e_df, "r");
10579544Seric 			if (e->e_dfp == NULL)
105858020Seric 			{
10599377Seric 				syserr("readqf: cannot open %s", e->e_df);
106058020Seric 				e->e_msgsize = -1;
106158020Seric 			}
106258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
106357529Seric 				e->e_msgsize = st.st_size;
10644632Seric 			break;
10654632Seric 
10667860Seric 		  case 'T':		/* init time */
106757135Seric 			e->e_ctime = atol(&bp[1]);
10684632Seric 			break;
10694632Seric 
10704634Seric 		  case 'P':		/* message priority */
107157135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10724634Seric 			break;
10734634Seric 
107458737Seric 		  case 'F':		/* flag bits */
107558737Seric 			for (p = &bp[1]; *p != '\0'; p++)
107658737Seric 			{
107758737Seric 				switch (*p)
107858737Seric 				{
107958737Seric 				  case 'w':	/* warning sent */
108058737Seric 					e->e_flags |= EF_WARNING;
108158737Seric 					break;
108258737Seric 
108358737Seric 				  case 'r':	/* response */
108458737Seric 					e->e_flags |= EF_RESPONSE;
108558737Seric 					break;
108658737Seric 				}
108758737Seric 			}
108858737Seric 			break;
108958737Seric 
109053400Seric 		  case '$':		/* define macro */
109157135Seric 			define(bp[1], newstr(&bp[2]), e);
109253400Seric 			break;
109353400Seric 
109424941Seric 		  case '\0':		/* blank line; ignore */
109524941Seric 			break;
109624941Seric 
10974632Seric 		  default:
109859700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
109957135Seric 				LineNumber, bp);
110063753Seric 			fclose(qfp);
110163753Seric 			rename(qf, queuename(e, 'Q'));
110263753Seric 			return FALSE;
11034632Seric 		}
110457135Seric 
110557135Seric 		if (bp != buf)
110657135Seric 			free(bp);
11074632Seric 	}
11089377Seric 
11099377Seric 	FileName = NULL;
111024941Seric 
111124941Seric 	/*
111224941Seric 	**  If we haven't read any lines, this queue file is empty.
111324941Seric 	**  Arrange to remove it without referencing any null pointers.
111424941Seric 	*/
111524941Seric 
111624941Seric 	if (LineNumber == 0)
111724941Seric 	{
111824941Seric 		errno = 0;
111924941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
112024941Seric 	}
112151920Seric 	return TRUE;
11224632Seric }
11234632Seric /*
11249630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11259630Seric **
11269630Seric **	Parameters:
11279630Seric **		none.
11289630Seric **
11299630Seric **	Returns:
11309630Seric **		none.
11319630Seric **
11329630Seric **	Side Effects:
11339630Seric **		Prints a listing of the mail queue on the standard output.
11349630Seric */
11355182Seric 
11369630Seric printqueue()
11379630Seric {
11389630Seric 	register WORK *w;
11399630Seric 	FILE *f;
114010070Seric 	int nrequests;
11419630Seric 	char buf[MAXLINE];
11429630Seric 
11439630Seric 	/*
114458250Seric 	**  Check for permission to print the queue
114558250Seric 	*/
114658250Seric 
114764333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
114858250Seric 	{
114958250Seric 		struct stat st;
115058523Seric # ifdef NGROUPS
115158523Seric 		int n;
115263937Seric 		GIDSET_T gidset[NGROUPS];
115358523Seric # endif
115458250Seric 
115558517Seric 		if (stat(QueueDir, &st) < 0)
115658250Seric 		{
115758250Seric 			syserr("Cannot stat %s", QueueDir);
115858250Seric 			return;
115958250Seric 		}
116058523Seric # ifdef NGROUPS
116158523Seric 		n = getgroups(NGROUPS, gidset);
116258523Seric 		while (--n >= 0)
116358523Seric 		{
116458523Seric 			if (gidset[n] == st.st_gid)
116558523Seric 				break;
116658523Seric 		}
116758523Seric 		if (n < 0)
116858523Seric # else
116963787Seric 		if (RealGid != st.st_gid)
117058523Seric # endif
117158250Seric 		{
117258250Seric 			usrerr("510 You are not permitted to see the queue");
117358250Seric 			setstat(EX_NOPERM);
117458250Seric 			return;
117558250Seric 		}
117658250Seric 	}
117758250Seric 
117858250Seric 	/*
11799630Seric 	**  Read and order the queue.
11809630Seric 	*/
11819630Seric 
118224941Seric 	nrequests = orderq(TRUE);
11839630Seric 
11849630Seric 	/*
11859630Seric 	**  Print the work list that we have read.
11869630Seric 	*/
11879630Seric 
11889630Seric 	/* first see if there is anything */
118910070Seric 	if (nrequests <= 0)
11909630Seric 	{
119110070Seric 		printf("Mail queue is empty\n");
11929630Seric 		return;
11939630Seric 	}
11949630Seric 
119551920Seric 	CurrentLA = getla();	/* get load average */
119640934Srick 
119710096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
119825687Seric 	if (nrequests > QUEUESIZE)
119925687Seric 		printf(", only %d printed", QUEUESIZE);
120024979Seric 	if (Verbose)
120158716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
120224979Seric 	else
120358716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
12049630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
12059630Seric 	{
12069630Seric 		struct stat st;
120710070Seric 		auto time_t submittime = 0;
120810070Seric 		long dfsize = -1;
120958737Seric 		int flags = 0;
121010108Seric 		char message[MAXLINE];
121159093Seric 		char bodytype[MAXNAME];
12129630Seric 
121364355Seric 		printf("%8s", w->w_name + 2);
121417468Seric 		f = fopen(w->w_name, "r");
121517468Seric 		if (f == NULL)
121617468Seric 		{
121764355Seric 			printf(" (job completed)\n");
121817468Seric 			errno = 0;
121917468Seric 			continue;
122017468Seric 		}
122164335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
122210070Seric 			printf("*");
122357438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
122424941Seric 			printf("X");
122510070Seric 		else
122610070Seric 			printf(" ");
122710070Seric 		errno = 0;
122817468Seric 
122959093Seric 		message[0] = bodytype[0] = '\0';
12309630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12319630Seric 		{
123253400Seric 			register int i;
123358737Seric 			register char *p;
123453400Seric 
12359630Seric 			fixcrlf(buf, TRUE);
12369630Seric 			switch (buf[0])
12379630Seric 			{
123810108Seric 			  case 'M':	/* error message */
123953400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
124058737Seric 					i = sizeof message - 1;
124153400Seric 				bcopy(&buf[1], message, i);
124253400Seric 				message[i] = '\0';
124310108Seric 				break;
124410108Seric 
124559093Seric 			  case 'B':	/* body type */
124659093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
124759093Seric 					i = sizeof bodytype - 1;
124859093Seric 				bcopy(&buf[1], bodytype, i);
124959093Seric 				bodytype[i] = '\0';
125059093Seric 				break;
125159093Seric 
12529630Seric 			  case 'S':	/* sender name */
125324979Seric 				if (Verbose)
125458737Seric 					printf("%8ld %10ld%c%.12s %.38s",
125558737Seric 					    dfsize,
125658737Seric 					    w->w_pri,
125758737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
125858737Seric 					    ctime(&submittime) + 4,
125924979Seric 					    &buf[1]);
126024979Seric 				else
126124979Seric 					printf("%8ld %.16s %.45s", dfsize,
126224979Seric 					    ctime(&submittime), &buf[1]);
126359093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
126459093Seric 				{
126559093Seric 					printf("\n    %10.10s", bodytype);
126659093Seric 					if (message[0] != '\0')
126759093Seric 						printf("   (%.60s)", message);
126859093Seric 				}
12699630Seric 				break;
127051920Seric 
127140973Sbostic 			  case 'C':	/* controlling user */
127254974Seric 				if (Verbose)
127358716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
127458716Seric 						&buf[1]);
127540973Sbostic 				break;
12769630Seric 
12779630Seric 			  case 'R':	/* recipient name */
127824979Seric 				if (Verbose)
127958716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
128024979Seric 				else
128158716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12829630Seric 				break;
12839630Seric 
12849630Seric 			  case 'T':	/* creation time */
128524941Seric 				submittime = atol(&buf[1]);
12869630Seric 				break;
128710070Seric 
128810070Seric 			  case 'D':	/* data file name */
128910070Seric 				if (stat(&buf[1], &st) >= 0)
129010070Seric 					dfsize = st.st_size;
129110070Seric 				break;
129258737Seric 
129358737Seric 			  case 'F':	/* flag bits */
129458737Seric 				for (p = &buf[1]; *p != '\0'; p++)
129558737Seric 				{
129658737Seric 					switch (*p)
129758737Seric 					{
129858737Seric 					  case 'w':
129958737Seric 						flags |= EF_WARNING;
130058737Seric 						break;
130158737Seric 					}
130258737Seric 				}
13039630Seric 			}
13049630Seric 		}
130510070Seric 		if (submittime == (time_t) 0)
130610070Seric 			printf(" (no control file)");
13079630Seric 		printf("\n");
130823098Seric 		(void) fclose(f);
13099630Seric 	}
13109630Seric }
13119630Seric 
131256795Seric # endif /* QUEUE */
131317468Seric /*
131417468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
131517468Seric **
131617468Seric **	Assigns an id code if one does not already exist.
131717468Seric **	This code is very careful to avoid trashing existing files
131817468Seric **	under any circumstances.
131917468Seric **
132017468Seric **	Parameters:
132117468Seric **		e -- envelope to build it in/from.
132217468Seric **		type -- the file type, used as the first character
132317468Seric **			of the file name.
132417468Seric **
132517468Seric **	Returns:
132617468Seric **		a pointer to the new file name (in a static buffer).
132717468Seric **
132817468Seric **	Side Effects:
132951920Seric **		If no id code is already assigned, queuename will
133051920Seric **		assign an id code, create a qf file, and leave a
133151920Seric **		locked, open-for-write file pointer in the envelope.
133217468Seric */
133317468Seric 
133417468Seric char *
133517468Seric queuename(e, type)
133617468Seric 	register ENVELOPE *e;
133759700Seric 	int type;
133817468Seric {
133917468Seric 	static int pid = -1;
134058741Seric 	static char c0;
134158741Seric 	static char c1;
134258741Seric 	static char c2;
134358716Seric 	time_t now;
134458716Seric 	struct tm *tm;
134558689Seric 	static char buf[MAXNAME];
134617468Seric 
134717468Seric 	if (e->e_id == NULL)
134817468Seric 	{
134917468Seric 		char qf[20];
135017468Seric 
135117468Seric 		/* find a unique id */
135217468Seric 		if (pid != getpid())
135317468Seric 		{
135417468Seric 			/* new process -- start back at "AA" */
135517468Seric 			pid = getpid();
135658716Seric 			now = curtime();
135758716Seric 			tm = localtime(&now);
135858716Seric 			c0 = 'A' + tm->tm_hour;
135917468Seric 			c1 = 'A';
136017468Seric 			c2 = 'A' - 1;
136117468Seric 		}
136258716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
136317468Seric 
136417468Seric 		while (c1 < '~' || c2 < 'Z')
136517468Seric 		{
136617468Seric 			int i;
136717468Seric 
136817468Seric 			if (c2 >= 'Z')
136917468Seric 			{
137017468Seric 				c1++;
137117468Seric 				c2 = 'A' - 1;
137217468Seric 			}
137358716Seric 			qf[3] = c1;
137458716Seric 			qf[4] = ++c2;
137517468Seric 			if (tTd(7, 20))
137640934Srick 				printf("queuename: trying \"%s\"\n", qf);
137717468Seric 
137840934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
137951920Seric 			if (i < 0)
138051920Seric 			{
138151920Seric 				if (errno == EEXIST)
138251920Seric 					continue;
138364705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
138464705Seric 					qf, QueueDir, geteuid());
138551920Seric 				exit(EX_UNAVAILABLE);
138651920Seric 			}
138764335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
138851920Seric 			{
138951920Seric 				e->e_lockfp = fdopen(i, "w");
139040934Srick 				break;
139117468Seric 			}
139251920Seric 
139351920Seric 			/* a reader got the file; abandon it and try again */
139451920Seric 			(void) close(i);
139517468Seric 		}
139617468Seric 		if (c1 >= '~' && c2 >= 'Z')
139717468Seric 		{
139864705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
139964705Seric 				qf, QueueDir, geteuid());
140017468Seric 			exit(EX_OSERR);
140117468Seric 		}
140217468Seric 		e->e_id = newstr(&qf[2]);
140317468Seric 		define('i', e->e_id, e);
140417468Seric 		if (tTd(7, 1))
140517468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
140664745Seric 		if (tTd(7, 9))
140764745Seric 		{
140864745Seric 			printf("  lockfd=");
140964745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
141064745Seric 		}
141117468Seric # ifdef LOG
141258020Seric 		if (LogLevel > 93)
141317468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
141456795Seric # endif /* LOG */
141517468Seric 	}
141617468Seric 
141717468Seric 	if (type == '\0')
141817468Seric 		return (NULL);
141917468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
142017468Seric 	if (tTd(7, 2))
142117468Seric 		printf("queuename: %s\n", buf);
142217468Seric 	return (buf);
142317468Seric }
142417468Seric /*
142517468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
142617468Seric **
142717468Seric **	Parameters:
142817468Seric **		e -- the envelope to unlock.
142917468Seric **
143017468Seric **	Returns:
143117468Seric **		none
143217468Seric **
143317468Seric **	Side Effects:
143417468Seric **		unlocks the queue for `e'.
143517468Seric */
143617468Seric 
143717468Seric unlockqueue(e)
143817468Seric 	ENVELOPE *e;
143917468Seric {
144058680Seric 	if (tTd(51, 4))
144158680Seric 		printf("unlockqueue(%s)\n", e->e_id);
144258680Seric 
144351920Seric 	/* if there is a lock file in the envelope, close it */
144451920Seric 	if (e->e_lockfp != NULL)
144558680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
144651920Seric 	e->e_lockfp = NULL;
144751920Seric 
144858728Seric 	/* don't create a queue id if we don't already have one */
144958728Seric 	if (e->e_id == NULL)
145058728Seric 		return;
145158728Seric 
145217468Seric 	/* remove the transcript */
145317468Seric # ifdef LOG
145458020Seric 	if (LogLevel > 87)
145517468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
145656795Seric # endif /* LOG */
145758680Seric 	if (!tTd(51, 104))
145817468Seric 		xunlink(queuename(e, 'x'));
145917468Seric 
146017468Seric }
146140973Sbostic /*
146254974Seric **  SETCTLUSER -- create a controlling address
146340973Sbostic **
146454974Seric **	Create a fake "address" given only a local login name; this is
146554974Seric **	used as a "controlling user" for future recipient addresses.
146640973Sbostic **
146740973Sbostic **	Parameters:
146854974Seric **		user -- the user name of the controlling user.
146940973Sbostic **
147040973Sbostic **	Returns:
147154974Seric **		An address descriptor for the controlling user.
147240973Sbostic **
147340973Sbostic **	Side Effects:
147440973Sbostic **		none.
147540973Sbostic */
147640973Sbostic 
147754974Seric ADDRESS *
147854974Seric setctluser(user)
147954974Seric 	char *user;
148040973Sbostic {
148154974Seric 	register ADDRESS *a;
148240973Sbostic 	struct passwd *pw;
148359113Seric 	char *p;
148440973Sbostic 
148540973Sbostic 	/*
148654974Seric 	**  See if this clears our concept of controlling user.
148740973Sbostic 	*/
148840973Sbostic 
148963850Seric 	if (user == NULL || *user == '\0')
149063850Seric 		return NULL;
149140973Sbostic 
149240973Sbostic 	/*
149354974Seric 	**  Set up addr fields for controlling user.
149440973Sbostic 	*/
149540973Sbostic 
149654974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
149754974Seric 	bzero((char *) a, sizeof *a);
149859113Seric 
149959113Seric 	p = strchr(user, ':');
150059113Seric 	if (p != NULL)
150159113Seric 		*p++ = '\0';
150259270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
150340973Sbostic 	{
150440973Sbostic 		a->q_home = newstr(pw->pw_dir);
150540973Sbostic 		a->q_uid = pw->pw_uid;
150640973Sbostic 		a->q_gid = pw->pw_gid;
150757642Seric 		a->q_user = newstr(user);
150859270Seric 		a->q_flags |= QGOODUID;
150940973Sbostic 	}
151040973Sbostic 	else
151140973Sbostic 	{
151257642Seric 		a->q_user = newstr(DefUser);
151340973Sbostic 	}
151440973Sbostic 
151559270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
151656328Seric 	a->q_mailer = LocalMailer;
151759113Seric 	if (p == NULL)
151859113Seric 		a->q_paddr = a->q_user;
151959113Seric 	else
152059113Seric 		a->q_paddr = newstr(p);
152154974Seric 	return a;
152240973Sbostic }
1523