xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64736)
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*64736Seric static char sccsid[] = "@(#)queue.c	8.22 (Berkeley) 10/21/93 (with queueing)";
1433731Sbostic #else
15*64736Seric static char sccsid[] = "@(#)queue.c	8.22 (Berkeley) 10/21/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7151920Seric 	char buf[MAXLINE], tf[MAXLINE];
724632Seric 
735037Seric 	/*
7417477Seric 	**  Create control file.
755037Seric 	*/
764632Seric 
7751920Seric 	newid = (e->e_id == NULL);
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) */
8651920Seric 	if (newid)
8751835Seric 	{
8851920Seric 		tfp = e->e_lockfp;
8951920Seric 	}
9051920Seric 	else
9151920Seric 	{
9251920Seric 		/* get a locked tf file */
9364070Seric 		for (i = 0; i < 128; i++)
9451835Seric 		{
9551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9651920Seric 			if (fd < 0)
9751835Seric 			{
9864070Seric 				if (errno != EEXIST)
9964070Seric 					break;
10064070Seric #ifdef LOG
10164070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10264070Seric 					syslog(LOG_ALERT, "queueup: cannot create %s: %s",
10364070Seric 						tf, errstring(errno));
10464070Seric #endif
10564070Seric 				continue;
10641636Srick 			}
10758689Seric 
10864335Seric 			if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10951920Seric 				break;
11064070Seric #ifdef LOG
11164070Seric 			else if (LogLevel > 0 && (i % 32) == 0)
11264070Seric 				syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11364070Seric 					tf, errstring(errno));
11464070Seric #endif
11558689Seric 
11651920Seric 			close(fd);
11764070Seric 
11864070Seric 			if ((i % 32) == 31)
11964070Seric 			{
12064070Seric 				/* save the old temp file away */
12164070Seric 				(void) rename(tf, queuename(e, 'T'));
12264070Seric 			}
12364070Seric 			else
12464070Seric 				sleep(i % 32);
12541636Srick 		}
12664724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12764724Seric 			syserr("!queueup: cannot create queue temp file %s", tf);
12851920Seric 	}
1294632Seric 
1307677Seric 	if (tTd(40, 1))
13164307Seric 		printf("\n>>>>> queueing %s >>>>>\n", e->e_id);
1324632Seric 
1334632Seric 	/*
1346980Seric 	**  If there is no data file yet, create one.
1356980Seric 	*/
1366980Seric 
1376980Seric 	if (e->e_df == NULL)
1386980Seric 	{
1396980Seric 		register FILE *dfp;
1409389Seric 		extern putbody();
1416980Seric 
14264086Seric 		e->e_df = queuename(e, 'd');
14364086Seric 		e->e_df = newstr(e->e_df);
14440934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14564724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
14664724Seric 			syserr("!queueup: cannot create data temp file %s",
14764724Seric 				e->e_df);
14859730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
14958680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1509389Seric 		e->e_putbody = putbody;
1516980Seric 	}
1526980Seric 
1536980Seric 	/*
1544632Seric 	**  Output future work requests.
15525687Seric 	**	Priority and creation time should be first, since
15625687Seric 	**	they are required by orderq.
1574632Seric 	*/
1584632Seric 
1599377Seric 	/* output message priority */
1609377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1619377Seric 
1629630Seric 	/* output creation time */
1639630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1649630Seric 
16559093Seric 	/* output type and name of data file */
16659093Seric 	if (e->e_bodytype != NULL)
16759093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1687812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1694632Seric 
17010108Seric 	/* message from envelope, if it exists */
17110108Seric 	if (e->e_message != NULL)
17210108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17310108Seric 
17458737Seric 	/* send various flag bits through */
17558737Seric 	p = buf;
17658737Seric 	if (bitset(EF_WARNING, e->e_flags))
17758737Seric 		*p++ = 'w';
17858737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
17958737Seric 		*p++ = 'r';
18058737Seric 	*p++ = '\0';
18158737Seric 	if (buf[0] != '\0')
18258737Seric 		fprintf(tfp, "F%s\n", buf);
18358737Seric 
18458957Seric 	/* $r and $s and $_ macro values */
18553400Seric 	if ((p = macvalue('r', e)) != NULL)
18653400Seric 		fprintf(tfp, "$r%s\n", p);
18753400Seric 	if ((p = macvalue('s', e)) != NULL)
18853400Seric 		fprintf(tfp, "$s%s\n", p);
18958957Seric 	if ((p = macvalue('_', e)) != NULL)
19058957Seric 		fprintf(tfp, "$_%s\n", p);
19153400Seric 
1924632Seric 	/* output name of sender */
1937812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1944632Seric 
19555360Seric 	/* output list of error recipients */
19659670Seric 	printctladdr(NULL, NULL);
19755360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
19855360Seric 	{
19958680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
20055360Seric 		{
20159113Seric 			printctladdr(q, tfp);
20255360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
20355360Seric 		}
20455360Seric 	}
20555360Seric 
2064632Seric 	/* output list of recipient addresses */
2076980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2084632Seric 	{
20958250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21058680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2118245Seric 		{
21259113Seric 			printctladdr(q, tfp);
2137812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2149377Seric 			if (announce)
2159377Seric 			{
2169377Seric 				e->e_to = q->q_paddr;
21758151Seric 				message("queued");
21858020Seric 				if (LogLevel > 8)
21958337Seric 					logdelivery(NULL, NULL, "queued", e);
2209377Seric 				e->e_to = NULL;
2219377Seric 			}
2229387Seric 			if (tTd(40, 1))
2239387Seric 			{
2249387Seric 				printf("queueing ");
2259387Seric 				printaddr(q, FALSE);
2269387Seric 			}
2278245Seric 		}
2284632Seric 	}
2294632Seric 
2309377Seric 	/*
2319377Seric 	**  Output headers for this message.
2329377Seric 	**	Expand macros completely here.  Queue run will deal with
2339377Seric 	**	everything as absolute headers.
2349377Seric 	**		All headers that must be relative to the recipient
2359377Seric 	**		can be cracked later.
23610173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23710173Seric 	**	no effect on the addresses as they are output.
2389377Seric 	*/
2399377Seric 
24010686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24158020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24258020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24310349Seric 	nullmailer.m_eol = "\n";
24410173Seric 
24558050Seric 	define('g', "\201f", e);
2466980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2474632Seric 	{
24810686Seric 		extern bool bitzerop();
24910686Seric 
25012015Seric 		/* don't output null headers */
2514632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2524632Seric 			continue;
25312015Seric 
25412015Seric 		/* don't output resent headers on non-resent messages */
25512015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25612015Seric 			continue;
25712015Seric 
25812015Seric 		/* output this header */
2597812Seric 		fprintf(tfp, "H");
26012015Seric 
26112015Seric 		/* if conditional, output the set of conditions */
26210686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26310686Seric 		{
26410686Seric 			int j;
26510686Seric 
26623098Seric 			(void) putc('?', tfp);
26710686Seric 			for (j = '\0'; j <= '\177'; j++)
26810686Seric 				if (bitnset(j, h->h_mflags))
26923098Seric 					(void) putc(j, tfp);
27023098Seric 			(void) putc('?', tfp);
27110686Seric 		}
27212015Seric 
27312015Seric 		/* output the header: expand macros, convert addresses */
2747763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2757763Seric 		{
2767763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2778236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2787763Seric 		}
2798245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2809348Seric 		{
28158737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28263753Seric 			FILE *savetrace = TrafficLogFile;
28358737Seric 
28463753Seric 			TrafficLogFile = NULL;
28563753Seric 
28658737Seric 			if (bitset(H_FROM, h->h_flags))
28758737Seric 				oldstyle = FALSE;
28858737Seric 
28958737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29055012Seric 				 &nullmailer, e);
29163753Seric 
29263753Seric 			TrafficLogFile = savetrace;
2939348Seric 		}
2947763Seric 		else
2958245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2964632Seric 	}
2974632Seric 
2984632Seric 	/*
2994632Seric 	**  Clean up.
3004632Seric 	*/
3014632Seric 
30258732Seric 	fflush(tfp);
30360603Seric 	fsync(fileno(tfp));
30458732Seric 	if (ferror(tfp))
30558732Seric 	{
30658732Seric 		if (newid)
30758732Seric 			syserr("!552 Error writing control file %s", tf);
30858732Seric 		else
30958732Seric 			syserr("!452 Error writing control file %s", tf);
31058732Seric 	}
31158732Seric 
31251920Seric 	if (!newid)
31351920Seric 	{
31464277Seric 		/* rename (locked) tf to be (locked) qf */
31551920Seric 		qf = queuename(e, 'q');
31651920Seric 		if (rename(tf, qf) < 0)
31751920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31864277Seric 
31964277Seric 		/* close and unlock old (locked) qf */
32051920Seric 		if (e->e_lockfp != NULL)
32158680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
32251920Seric 		e->e_lockfp = tfp;
32351920Seric 	}
32451920Seric 	else
32551920Seric 		qf = tf;
32641636Srick 	errno = 0;
3277391Seric 
3287677Seric # ifdef LOG
3297677Seric 	/* save log info */
33058020Seric 	if (LogLevel > 79)
3317878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33256795Seric # endif /* LOG */
33364307Seric 
33464307Seric 	if (tTd(40, 1))
33564307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
33651920Seric 	return;
3374632Seric }
33854974Seric 
33954974Seric printctladdr(a, tfp)
34059113Seric 	register ADDRESS *a;
34154974Seric 	FILE *tfp;
34254974Seric {
34359113Seric 	char *uname;
34459113Seric 	register struct passwd *pw;
34559113Seric 	register ADDRESS *q;
34659113Seric 	uid_t uid;
34759113Seric 	static ADDRESS *lastctladdr;
34859113Seric 	static uid_t lastuid;
34954974Seric 
35059113Seric 	/* initialization */
35163850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
35254974Seric 	{
35359670Seric 		if (lastctladdr != NULL && tfp != NULL)
35459113Seric 			fprintf(tfp, "C\n");
35559113Seric 		lastctladdr = NULL;
35659113Seric 		lastuid = 0;
35754974Seric 		return;
35854974Seric 	}
35959113Seric 
36059113Seric 	/* find the active uid */
36159113Seric 	q = getctladdr(a);
36259113Seric 	if (q == NULL)
36359113Seric 		uid = 0;
36454974Seric 	else
36559113Seric 		uid = q->q_uid;
36663850Seric 	a = a->q_alias;
36759113Seric 
36859113Seric 	/* check to see if this is the same as last time */
36959113Seric 	if (lastctladdr != NULL && uid == lastuid &&
37059113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
37159113Seric 		return;
37259113Seric 	lastuid = uid;
37359113Seric 	lastctladdr = a;
37459113Seric 
37559113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
37659270Seric 		uname = "";
37759113Seric 	else
37859113Seric 		uname = pw->pw_name;
37959113Seric 
38059113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
38154974Seric }
38254974Seric 
3834632Seric /*
3844632Seric **  RUNQUEUE -- run the jobs in the queue.
3854632Seric **
3864632Seric **	Gets the stuff out of the queue in some presumably logical
3874632Seric **	order and processes them.
3884632Seric **
3894632Seric **	Parameters:
39024941Seric **		forkflag -- TRUE if the queue scanning should be done in
39124941Seric **			a child process.  We double-fork so it is not our
39224941Seric **			child and we don't have to clean up after it.
3934632Seric **
3944632Seric **	Returns:
3954632Seric **		none.
3964632Seric **
3974632Seric **	Side Effects:
3984632Seric **		runs things in the mail queue.
3994632Seric */
4004632Seric 
40155360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
40255360Seric 
40355360Seric runqueue(forkflag)
4044639Seric 	bool forkflag;
4054632Seric {
40655360Seric 	register ENVELOPE *e;
40755360Seric 	extern ENVELOPE BlankEnvelope;
40824953Seric 
4097466Seric 	/*
41024953Seric 	**  If no work will ever be selected, don't even bother reading
41124953Seric 	**  the queue.
41224953Seric 	*/
41324953Seric 
41451920Seric 	CurrentLA = getla();	/* get load average */
41540934Srick 
41658132Seric 	if (shouldqueue(0L, curtime()))
41724953Seric 	{
41824953Seric 		if (Verbose)
41924953Seric 			printf("Skipping queue run -- load average too high\n");
42058107Seric 		if (forkflag && QueueIntvl != 0)
42158107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
42255360Seric 		return;
42324953Seric 	}
42424953Seric 
42524953Seric 	/*
4267466Seric 	**  See if we want to go off and do other useful work.
4277466Seric 	*/
4284639Seric 
4294639Seric 	if (forkflag)
4304639Seric 	{
4317943Seric 		int pid;
4327943Seric 
4337943Seric 		pid = dofork();
4347943Seric 		if (pid != 0)
4354639Seric 		{
43646928Sbostic 			extern void reapchild();
43725184Seric 
4387943Seric 			/* parent -- pick up intermediate zombie */
43925184Seric #ifndef SIGCHLD
4409377Seric 			(void) waitfor(pid);
44156795Seric #else /* SIGCHLD */
44264035Seric 			(void) setsignal(SIGCHLD, reapchild);
44356795Seric #endif /* SIGCHLD */
4447690Seric 			if (QueueIntvl != 0)
4459348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4464639Seric 			return;
4474639Seric 		}
4487943Seric 		/* child -- double fork */
44925184Seric #ifndef SIGCHLD
4507943Seric 		if (fork() != 0)
4517943Seric 			exit(EX_OK);
45256795Seric #else /* SIGCHLD */
45364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
45456795Seric #endif /* SIGCHLD */
4554639Seric 	}
45624941Seric 
45740934Srick 	setproctitle("running queue: %s", QueueDir);
45824941Seric 
4597876Seric # ifdef LOG
46058020Seric 	if (LogLevel > 69)
46155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
46255360Seric 			QueueDir, getpid(), forkflag);
46356795Seric # endif /* LOG */
4644639Seric 
4657466Seric 	/*
46610205Seric 	**  Release any resources used by the daemon code.
46710205Seric 	*/
46810205Seric 
46910205Seric # ifdef DAEMON
47010205Seric 	clrdaemon();
47156795Seric # endif /* DAEMON */
47210205Seric 
47364658Seric 	/* force it to run expensive jobs */
47464658Seric 	NoConnect = FALSE;
47564658Seric 
47610205Seric 	/*
47755360Seric 	**  Create ourselves an envelope
47855360Seric 	*/
47955360Seric 
48055360Seric 	CurEnv = &QueueEnvelope;
48158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
48255360Seric 	e->e_flags = BlankEnvelope.e_flags;
48355360Seric 
48455360Seric 	/*
48527175Seric 	**  Make sure the alias database is open.
48627175Seric 	*/
48727175Seric 
48860537Seric 	initmaps(FALSE, e);
48927175Seric 
49027175Seric 	/*
4917466Seric 	**  Start making passes through the queue.
4927466Seric 	**	First, read and sort the entire queue.
4937466Seric 	**	Then, process the work in that order.
4947466Seric 	**		But if you take too long, start over.
4957466Seric 	*/
4967466Seric 
4977943Seric 	/* order the existing work requests */
49824954Seric 	(void) orderq(FALSE);
4997690Seric 
5007943Seric 	/* process them once at a time */
5017943Seric 	while (WorkQ != NULL)
5024639Seric 	{
5037943Seric 		WORK *w = WorkQ;
5047881Seric 
5057943Seric 		WorkQ = WorkQ->w_next;
50658884Seric 
50758884Seric 		/*
50858884Seric 		**  Ignore jobs that are too expensive for the moment.
50958884Seric 		*/
51058884Seric 
51158884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
51258884Seric 		{
51358884Seric 			if (Verbose)
51458884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
51558884Seric 		}
51658930Seric 		else
51758930Seric 		{
51864296Seric 			pid_t pid;
51964296Seric 			extern pid_t dowork();
52064296Seric 
52164296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
52264296Seric 			errno = 0;
52364296Seric 			(void) waitfor(pid);
52458930Seric 		}
5257943Seric 		free(w->w_name);
5267943Seric 		free((char *) w);
5274639Seric 	}
52829866Seric 
52929866Seric 	/* exit without the usual cleanup */
53055467Seric 	e->e_id = NULL;
53155467Seric 	finis();
5324634Seric }
5334634Seric /*
5344632Seric **  ORDERQ -- order the work queue.
5354632Seric **
5364632Seric **	Parameters:
53724941Seric **		doall -- if set, include everything in the queue (even
53824941Seric **			the jobs that cannot be run because the load
53924941Seric **			average is too high).  Otherwise, exclude those
54024941Seric **			jobs.
5414632Seric **
5424632Seric **	Returns:
54310121Seric **		The number of request in the queue (not necessarily
54410121Seric **		the number of requests in WorkQ however).
5454632Seric **
5464632Seric **	Side Effects:
5474632Seric **		Sets WorkQ to the queue of available work, in order.
5484632Seric */
5494632Seric 
55025687Seric # define NEED_P		001
55125687Seric # define NEED_T		002
55258318Seric # define NEED_R		004
55358318Seric # define NEED_S		010
5544632Seric 
55524941Seric orderq(doall)
55624941Seric 	bool doall;
5574632Seric {
55860219Seric 	register struct dirent *d;
5594632Seric 	register WORK *w;
5606625Sglickman 	DIR *f;
5614632Seric 	register int i;
56225687Seric 	WORK wlist[QUEUESIZE+1];
56310070Seric 	int wn = -1;
5644632Seric 	extern workcmpf();
5654632Seric 
56658318Seric 	if (tTd(41, 1))
56758318Seric 	{
56858318Seric 		printf("orderq:\n");
56958318Seric 		if (QueueLimitId != NULL)
57058318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
57158318Seric 		if (QueueLimitSender != NULL)
57258318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
57358318Seric 		if (QueueLimitRecipient != NULL)
57458318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
57558318Seric 	}
57658318Seric 
5774632Seric 	/* clear out old WorkQ */
5784632Seric 	for (w = WorkQ; w != NULL; )
5794632Seric 	{
5804632Seric 		register WORK *nw = w->w_next;
5814632Seric 
5824632Seric 		WorkQ = nw;
5834632Seric 		free(w->w_name);
5844632Seric 		free((char *) w);
5854632Seric 		w = nw;
5864632Seric 	}
5874632Seric 
5884632Seric 	/* open the queue directory */
5898148Seric 	f = opendir(".");
5904632Seric 	if (f == NULL)
5914632Seric 	{
5928148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
59310070Seric 		return (0);
5944632Seric 	}
5954632Seric 
5964632Seric 	/*
5974632Seric 	**  Read the work directory.
5984632Seric 	*/
5994632Seric 
60010070Seric 	while ((d = readdir(f)) != NULL)
6014632Seric 	{
6029377Seric 		FILE *cf;
60364492Seric 		register char *p;
6044632Seric 		char lbuf[MAXNAME];
60558318Seric 		extern bool strcontainedin();
6064632Seric 
6074632Seric 		/* is this an interesting entry? */
6087812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6094632Seric 			continue;
6104632Seric 
61158318Seric 		if (QueueLimitId != NULL &&
61258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
61358318Seric 			continue;
61458318Seric 
61558722Seric 		/*
61658722Seric 		**  Check queue name for plausibility.  This handles
61758722Seric 		**  both old and new type ids.
61858722Seric 		*/
61958722Seric 
62064492Seric 		p = d->d_name + 2;
62164492Seric 		if (isupper(p[0]) && isupper(p[2]))
62264492Seric 			p += 3;
62364492Seric 		else if (isupper(p[1]))
62464492Seric 			p += 2;
62564492Seric 		else
62664492Seric 			p = d->d_name;
62764492Seric 		for (i = 0; isdigit(*p); p++)
62864492Seric 			i++;
62964492Seric 		if (i < 5 || *p != '\0')
63058020Seric 		{
63158020Seric 			if (Verbose)
63258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
63358020Seric #ifdef LOG
63458020Seric 			if (LogLevel > 3)
63559615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
63658020Seric 					d->d_name);
63758020Seric #endif
63858020Seric 			if (strlen(d->d_name) >= MAXNAME)
63958020Seric 				d->d_name[MAXNAME - 1] = '\0';
64058020Seric 			strcpy(lbuf, d->d_name);
64158020Seric 			lbuf[0] = 'Q';
64258020Seric 			(void) rename(d->d_name, lbuf);
64358020Seric 			continue;
64458020Seric 		}
64558020Seric 
64610070Seric 		/* yes -- open control file (if not too many files) */
64725687Seric 		if (++wn >= QUEUESIZE)
64810070Seric 			continue;
64958318Seric 
6508148Seric 		cf = fopen(d->d_name, "r");
6514632Seric 		if (cf == NULL)
6524632Seric 		{
6537055Seric 			/* this may be some random person sending hir msgs */
6547055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
65510090Seric 			if (tTd(41, 2))
65610090Seric 				printf("orderq: cannot open %s (%d)\n",
65710090Seric 					d->d_name, errno);
6587055Seric 			errno = 0;
65910090Seric 			wn--;
6604632Seric 			continue;
6614632Seric 		}
66225687Seric 		w = &wlist[wn];
66325687Seric 		w->w_name = newstr(d->d_name);
6644632Seric 
66525027Seric 		/* make sure jobs in creation don't clog queue */
66625687Seric 		w->w_pri = 0x7fffffff;
66725687Seric 		w->w_ctime = 0;
66825027Seric 
6694632Seric 		/* extract useful information */
67025687Seric 		i = NEED_P | NEED_T;
67158318Seric 		if (QueueLimitSender != NULL)
67258318Seric 			i |= NEED_S;
67358318Seric 		if (QueueLimitRecipient != NULL)
67458318Seric 			i |= NEED_R;
67525687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6764632Seric 		{
67724954Seric 			extern long atol();
67858318Seric 			extern bool strcontainedin();
67924954Seric 
68024941Seric 			switch (lbuf[0])
6814632Seric 			{
68224941Seric 			  case 'P':
68325687Seric 				w->w_pri = atol(&lbuf[1]);
68425687Seric 				i &= ~NEED_P;
6854632Seric 				break;
68625013Seric 
68725013Seric 			  case 'T':
68825687Seric 				w->w_ctime = atol(&lbuf[1]);
68925687Seric 				i &= ~NEED_T;
69025013Seric 				break;
69158318Seric 
69258318Seric 			  case 'R':
69358318Seric 				if (QueueLimitRecipient != NULL &&
69458318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
69558318Seric 					i &= ~NEED_R;
69658318Seric 				break;
69758318Seric 
69858318Seric 			  case 'S':
69958318Seric 				if (QueueLimitSender != NULL &&
70058318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
70158318Seric 					i &= ~NEED_S;
70258318Seric 				break;
7034632Seric 			}
7044632Seric 		}
7054632Seric 		(void) fclose(cf);
70624953Seric 
70758318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
70858318Seric 		    bitset(NEED_R|NEED_S, i))
70924953Seric 		{
71024953Seric 			/* don't even bother sorting this job in */
71124953Seric 			wn--;
71224953Seric 		}
7134632Seric 	}
7146625Sglickman 	(void) closedir(f);
71510090Seric 	wn++;
7164632Seric 
7174632Seric 	/*
7184632Seric 	**  Sort the work directory.
7194632Seric 	*/
7204632Seric 
72125687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7224632Seric 
7234632Seric 	/*
7244632Seric 	**  Convert the work list into canonical form.
7259377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7264632Seric 	*/
7274632Seric 
72824981Seric 	WorkQ = NULL;
72925687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7304632Seric 	{
7314632Seric 		w = (WORK *) xalloc(sizeof *w);
7324632Seric 		w->w_name = wlist[i].w_name;
7334632Seric 		w->w_pri = wlist[i].w_pri;
73425013Seric 		w->w_ctime = wlist[i].w_ctime;
73524981Seric 		w->w_next = WorkQ;
73624981Seric 		WorkQ = w;
7374632Seric 	}
7384632Seric 
7397677Seric 	if (tTd(40, 1))
7404632Seric 	{
7414632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7425037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7434632Seric 	}
74410070Seric 
74510090Seric 	return (wn);
7464632Seric }
7474632Seric /*
7487677Seric **  WORKCMPF -- compare function for ordering work.
7494632Seric **
7504632Seric **	Parameters:
7514632Seric **		a -- the first argument.
7524632Seric **		b -- the second argument.
7534632Seric **
7544632Seric **	Returns:
75524981Seric **		-1 if a < b
75624981Seric **		 0 if a == b
75724981Seric **		+1 if a > b
7584632Seric **
7594632Seric **	Side Effects:
7604632Seric **		none.
7614632Seric */
7624632Seric 
7634632Seric workcmpf(a, b)
7645037Seric 	register WORK *a;
7655037Seric 	register WORK *b;
7664632Seric {
76757438Seric 	long pa = a->w_pri;
76857438Seric 	long pb = b->w_pri;
76924941Seric 
77024941Seric 	if (pa == pb)
7714632Seric 		return (0);
77224941Seric 	else if (pa > pb)
77324981Seric 		return (1);
77424981Seric 	else
77510121Seric 		return (-1);
7764632Seric }
7774632Seric /*
7784632Seric **  DOWORK -- do a work request.
7794632Seric **
7804632Seric **	Parameters:
78158884Seric **		id -- the ID of the job to run.
78258884Seric **		forkflag -- if set, run this in background.
78358924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
78458924Seric **			This is used when expanding aliases in the queue.
78561094Seric **			If forkflag is also set, it doesn't wait for the
78661094Seric **			child.
78758884Seric **		e - the envelope in which to run it.
7884632Seric **
7894632Seric **	Returns:
79064296Seric **		process id of process that is running the queue job.
7914632Seric **
7924632Seric **	Side Effects:
7934632Seric **		The work request is satisfied if possible.
7944632Seric */
7954632Seric 
79664296Seric pid_t
79758924Seric dowork(id, forkflag, requeueflag, e)
79858884Seric 	char *id;
79958884Seric 	bool forkflag;
80058924Seric 	bool requeueflag;
80155012Seric 	register ENVELOPE *e;
8024632Seric {
80364296Seric 	register pid_t pid;
80451920Seric 	extern bool readqf();
8054632Seric 
8067677Seric 	if (tTd(40, 1))
80758884Seric 		printf("dowork(%s)\n", id);
8084632Seric 
8094632Seric 	/*
81024941Seric 	**  Fork for work.
81124941Seric 	*/
81224941Seric 
81358884Seric 	if (forkflag)
81424941Seric 	{
81564296Seric 		pid = fork();
81664296Seric 		if (pid < 0)
81724941Seric 		{
81824941Seric 			syserr("dowork: cannot fork");
81964296Seric 			return 0;
82024941Seric 		}
82124941Seric 	}
82224941Seric 	else
82324941Seric 	{
82464296Seric 		pid = 0;
82524941Seric 	}
82624941Seric 
82764296Seric 	if (pid == 0)
8284632Seric 	{
8294632Seric 		/*
8304632Seric 		**  CHILD
8318148Seric 		**	Lock the control file to avoid duplicate deliveries.
8328148Seric 		**		Then run the file as though we had just read it.
8337350Seric 		**	We save an idea of the temporary name so we
8347350Seric 		**		can recover on interrupt.
8354632Seric 		*/
8364632Seric 
8377763Seric 		/* set basic modes, etc. */
8387356Seric 		(void) alarm(0);
83955012Seric 		clearenvelope(e, FALSE);
84064554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
84158734Seric 		e->e_errormode = EM_MAIL;
84258884Seric 		e->e_id = id;
843*64736Seric 		GrabTo = FALSE;
84463846Seric 		if (forkflag)
84564554Seric 		{
84664296Seric 			disconnect(1, e);
84764554Seric 			OpMode = MD_DELIVER;
84864554Seric 		}
8497876Seric # ifdef LOG
85058020Seric 		if (LogLevel > 76)
85155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8527881Seric 			       getpid());
85356795Seric # endif /* LOG */
8547763Seric 
8557763Seric 		/* don't use the headers from sendmail.cf... */
85655012Seric 		e->e_header = NULL;
8577763Seric 
85851920Seric 		/* read the queue control file -- return if locked */
85963850Seric 		if (!readqf(e, !requeueflag))
8606980Seric 		{
86158884Seric 			if (tTd(40, 4))
86258884Seric 				printf("readqf(%s) failed\n", e->e_id);
86358884Seric 			if (forkflag)
86424941Seric 				exit(EX_OK);
86524941Seric 			else
86624941Seric 				return;
8676980Seric 		}
8686980Seric 
86955012Seric 		e->e_flags |= EF_INQUEUE;
87058929Seric 		eatheader(e, requeueflag);
8716980Seric 
87258924Seric 		if (requeueflag)
87358924Seric 			queueup(e, TRUE, FALSE);
87458924Seric 
8756980Seric 		/* do the delivery */
87658915Seric 		sendall(e, SM_DELIVER);
8776980Seric 
8786980Seric 		/* finish up and exit */
87958884Seric 		if (forkflag)
88024941Seric 			finis();
88124941Seric 		else
88255012Seric 			dropenvelope(e);
8834632Seric 	}
88464296Seric 	e->e_id = NULL;
88564296Seric 	return pid;
8864632Seric }
8874632Seric /*
8884632Seric **  READQF -- read queue file and set up environment.
8894632Seric **
8904632Seric **	Parameters:
8919377Seric **		e -- the envelope of the job to run.
89263850Seric **		announcefile -- if set, announce the name of the queue
89363850Seric **			file in error messages.
8944632Seric **
8954632Seric **	Returns:
89651920Seric **		TRUE if it successfully read the queue file.
89751920Seric **		FALSE otherwise.
8984632Seric **
8994632Seric **	Side Effects:
90051920Seric **		The queue file is returned locked.
9014632Seric */
9024632Seric 
90351920Seric bool
90463850Seric readqf(e, announcefile)
9059377Seric 	register ENVELOPE *e;
90663850Seric 	bool announcefile;
9074632Seric {
90817477Seric 	register FILE *qfp;
90954974Seric 	ADDRESS *ctladdr;
91056400Seric 	struct stat st;
91157135Seric 	char *bp;
91258915Seric 	char qf[20];
91357135Seric 	char buf[MAXLINE];
91424954Seric 	extern long atol();
91554974Seric 	extern ADDRESS *setctluser();
9164632Seric 
9174632Seric 	/*
91817468Seric 	**  Read and process the file.
9194632Seric 	*/
9204632Seric 
92158915Seric 	strcpy(qf, queuename(e, 'q'));
92251937Seric 	qfp = fopen(qf, "r+");
92317477Seric 	if (qfp == NULL)
92417477Seric 	{
92558884Seric 		if (tTd(40, 8))
92658884Seric 			printf("readqf(%s): fopen failure (%s)\n",
92758884Seric 				qf, errstring(errno));
92840934Srick 		if (errno != ENOENT)
92940934Srick 			syserr("readqf: no control file %s", qf);
93051920Seric 		return FALSE;
93117477Seric 	}
93240934Srick 
93364335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
93464277Seric 	{
93564277Seric 		/* being processed by another queuer */
93664277Seric 		if (tTd(40, 8))
93764277Seric 			printf("readqf(%s): locked\n", qf);
93864277Seric 		if (Verbose)
93964277Seric 			printf("%s: locked\n", e->e_id);
94064277Seric # ifdef LOG
94164277Seric 		if (LogLevel > 19)
94264277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
94364277Seric # endif /* LOG */
94464277Seric 		(void) fclose(qfp);
94564277Seric 		return FALSE;
94664277Seric 	}
94764277Seric 
94856400Seric 	/*
94956400Seric 	**  Check the queue file for plausibility to avoid attacks.
95056400Seric 	*/
95156400Seric 
95256400Seric 	if (fstat(fileno(qfp), &st) < 0)
95356400Seric 	{
95456400Seric 		/* must have been being processed by someone else */
95558884Seric 		if (tTd(40, 8))
95658884Seric 			printf("readqf(%s): fstat failure (%s)\n",
95758884Seric 				qf, errstring(errno));
95856400Seric 		fclose(qfp);
95956400Seric 		return FALSE;
96056400Seric 	}
96156400Seric 
96264137Seric 	if (st.st_uid != geteuid())
96356400Seric 	{
96456400Seric # ifdef LOG
96556400Seric 		if (LogLevel > 0)
96656400Seric 		{
96756400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
96856400Seric 				e->e_id, st.st_uid, st.st_mode);
96956400Seric 		}
97056795Seric # endif /* LOG */
97158884Seric 		if (tTd(40, 8))
97258884Seric 			printf("readqf(%s): bogus file\n", qf);
97364277Seric 		rename(qf, queuename(e, 'Q'));
97456400Seric 		fclose(qfp);
97556400Seric 		return FALSE;
97656400Seric 	}
97756400Seric 
97864277Seric 	if (st.st_size == 0)
97940934Srick 	{
98064277Seric 		/* must be a bogus file -- just remove it */
98164277Seric 		(void) unlink(qf);
98264277Seric 		fclose(qfp);
98351920Seric 		return FALSE;
98440934Srick 	}
98540934Srick 
98664277Seric 	if (st.st_nlink == 0)
98759101Seric 	{
98864277Seric 		/*
98964277Seric 		**  Race condition -- we got a file just as it was being
99064277Seric 		**  unlinked.  Just assume it is zero length.
99164277Seric 		*/
99264277Seric 
99359101Seric 		fclose(qfp);
99459101Seric 		return FALSE;
99559101Seric 	}
99659101Seric 
99764277Seric 	/* good file -- save this lock */
99851920Seric 	e->e_lockfp = qfp;
99951920Seric 
100040934Srick 	/* do basic system initialization */
100155012Seric 	initsys(e);
100240934Srick 
100363850Seric 	if (announcefile)
100463850Seric 		FileName = qf;
10059377Seric 	LineNumber = 0;
100663850Seric 	e->e_flags |= EF_GLOBALERRS;
100763850Seric 	OpMode = MD_DELIVER;
100851920Seric 	if (Verbose)
10099377Seric 		printf("\nRunning %s\n", e->e_id);
101054974Seric 	ctladdr = NULL;
101157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
10124632Seric 	{
101358737Seric 		register char *p;
101457529Seric 		struct stat st;
101557529Seric 
101626504Seric 		if (tTd(40, 4))
101757135Seric 			printf("+++++ %s\n", bp);
101857135Seric 		switch (bp[0])
10194632Seric 		{
102040973Sbostic 		  case 'C':		/* specify controlling user */
102157135Seric 			ctladdr = setctluser(&bp[1]);
102240973Sbostic 			break;
102340973Sbostic 
10244632Seric 		  case 'R':		/* specify recipient */
102558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10264632Seric 			break;
10274632Seric 
102825687Seric 		  case 'E':		/* specify error recipient */
102958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
103025687Seric 			break;
103125687Seric 
10324632Seric 		  case 'H':		/* header */
103357135Seric 			(void) chompheader(&bp[1], FALSE, e);
10344632Seric 			break;
10354632Seric 
103610108Seric 		  case 'M':		/* message */
103764705Seric 			/* ignore this; we want a new message next time */
103810108Seric 			break;
103910108Seric 
10404632Seric 		  case 'S':		/* sender */
104158704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10424632Seric 			break;
10434632Seric 
104459093Seric 		  case 'B':		/* body type */
104559093Seric 			e->e_bodytype = newstr(&bp[1]);
104659093Seric 			break;
104759093Seric 
10484632Seric 		  case 'D':		/* data file name */
104957135Seric 			e->e_df = newstr(&bp[1]);
10509544Seric 			e->e_dfp = fopen(e->e_df, "r");
10519544Seric 			if (e->e_dfp == NULL)
105258020Seric 			{
10539377Seric 				syserr("readqf: cannot open %s", e->e_df);
105458020Seric 				e->e_msgsize = -1;
105558020Seric 			}
105658020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
105757529Seric 				e->e_msgsize = st.st_size;
10584632Seric 			break;
10594632Seric 
10607860Seric 		  case 'T':		/* init time */
106157135Seric 			e->e_ctime = atol(&bp[1]);
10624632Seric 			break;
10634632Seric 
10644634Seric 		  case 'P':		/* message priority */
106557135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10664634Seric 			break;
10674634Seric 
106858737Seric 		  case 'F':		/* flag bits */
106958737Seric 			for (p = &bp[1]; *p != '\0'; p++)
107058737Seric 			{
107158737Seric 				switch (*p)
107258737Seric 				{
107358737Seric 				  case 'w':	/* warning sent */
107458737Seric 					e->e_flags |= EF_WARNING;
107558737Seric 					break;
107658737Seric 
107758737Seric 				  case 'r':	/* response */
107858737Seric 					e->e_flags |= EF_RESPONSE;
107958737Seric 					break;
108058737Seric 				}
108158737Seric 			}
108258737Seric 			break;
108358737Seric 
108453400Seric 		  case '$':		/* define macro */
108557135Seric 			define(bp[1], newstr(&bp[2]), e);
108653400Seric 			break;
108753400Seric 
108824941Seric 		  case '\0':		/* blank line; ignore */
108924941Seric 			break;
109024941Seric 
10914632Seric 		  default:
109259700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
109357135Seric 				LineNumber, bp);
109463753Seric 			fclose(qfp);
109563753Seric 			rename(qf, queuename(e, 'Q'));
109663753Seric 			return FALSE;
10974632Seric 		}
109857135Seric 
109957135Seric 		if (bp != buf)
110057135Seric 			free(bp);
11014632Seric 	}
11029377Seric 
11039377Seric 	FileName = NULL;
110424941Seric 
110524941Seric 	/*
110624941Seric 	**  If we haven't read any lines, this queue file is empty.
110724941Seric 	**  Arrange to remove it without referencing any null pointers.
110824941Seric 	*/
110924941Seric 
111024941Seric 	if (LineNumber == 0)
111124941Seric 	{
111224941Seric 		errno = 0;
111324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
111424941Seric 	}
111551920Seric 	return TRUE;
11164632Seric }
11174632Seric /*
11189630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11199630Seric **
11209630Seric **	Parameters:
11219630Seric **		none.
11229630Seric **
11239630Seric **	Returns:
11249630Seric **		none.
11259630Seric **
11269630Seric **	Side Effects:
11279630Seric **		Prints a listing of the mail queue on the standard output.
11289630Seric */
11295182Seric 
11309630Seric printqueue()
11319630Seric {
11329630Seric 	register WORK *w;
11339630Seric 	FILE *f;
113410070Seric 	int nrequests;
11359630Seric 	char buf[MAXLINE];
11369630Seric 
11379630Seric 	/*
113858250Seric 	**  Check for permission to print the queue
113958250Seric 	*/
114058250Seric 
114164333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
114258250Seric 	{
114358250Seric 		struct stat st;
114458523Seric # ifdef NGROUPS
114558523Seric 		int n;
114663937Seric 		GIDSET_T gidset[NGROUPS];
114758523Seric # endif
114858250Seric 
114958517Seric 		if (stat(QueueDir, &st) < 0)
115058250Seric 		{
115158250Seric 			syserr("Cannot stat %s", QueueDir);
115258250Seric 			return;
115358250Seric 		}
115458523Seric # ifdef NGROUPS
115558523Seric 		n = getgroups(NGROUPS, gidset);
115658523Seric 		while (--n >= 0)
115758523Seric 		{
115858523Seric 			if (gidset[n] == st.st_gid)
115958523Seric 				break;
116058523Seric 		}
116158523Seric 		if (n < 0)
116258523Seric # else
116363787Seric 		if (RealGid != st.st_gid)
116458523Seric # endif
116558250Seric 		{
116658250Seric 			usrerr("510 You are not permitted to see the queue");
116758250Seric 			setstat(EX_NOPERM);
116858250Seric 			return;
116958250Seric 		}
117058250Seric 	}
117158250Seric 
117258250Seric 	/*
11739630Seric 	**  Read and order the queue.
11749630Seric 	*/
11759630Seric 
117624941Seric 	nrequests = orderq(TRUE);
11779630Seric 
11789630Seric 	/*
11799630Seric 	**  Print the work list that we have read.
11809630Seric 	*/
11819630Seric 
11829630Seric 	/* first see if there is anything */
118310070Seric 	if (nrequests <= 0)
11849630Seric 	{
118510070Seric 		printf("Mail queue is empty\n");
11869630Seric 		return;
11879630Seric 	}
11889630Seric 
118951920Seric 	CurrentLA = getla();	/* get load average */
119040934Srick 
119110096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
119225687Seric 	if (nrequests > QUEUESIZE)
119325687Seric 		printf(", only %d printed", QUEUESIZE);
119424979Seric 	if (Verbose)
119558716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
119624979Seric 	else
119758716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11989630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11999630Seric 	{
12009630Seric 		struct stat st;
120110070Seric 		auto time_t submittime = 0;
120210070Seric 		long dfsize = -1;
120358737Seric 		int flags = 0;
120410108Seric 		char message[MAXLINE];
120559093Seric 		char bodytype[MAXNAME];
12069630Seric 
120764355Seric 		printf("%8s", w->w_name + 2);
120817468Seric 		f = fopen(w->w_name, "r");
120917468Seric 		if (f == NULL)
121017468Seric 		{
121164355Seric 			printf(" (job completed)\n");
121217468Seric 			errno = 0;
121317468Seric 			continue;
121417468Seric 		}
121564335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
121610070Seric 			printf("*");
121757438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
121824941Seric 			printf("X");
121910070Seric 		else
122010070Seric 			printf(" ");
122110070Seric 		errno = 0;
122217468Seric 
122359093Seric 		message[0] = bodytype[0] = '\0';
12249630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12259630Seric 		{
122653400Seric 			register int i;
122758737Seric 			register char *p;
122853400Seric 
12299630Seric 			fixcrlf(buf, TRUE);
12309630Seric 			switch (buf[0])
12319630Seric 			{
123210108Seric 			  case 'M':	/* error message */
123353400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
123458737Seric 					i = sizeof message - 1;
123553400Seric 				bcopy(&buf[1], message, i);
123653400Seric 				message[i] = '\0';
123710108Seric 				break;
123810108Seric 
123959093Seric 			  case 'B':	/* body type */
124059093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
124159093Seric 					i = sizeof bodytype - 1;
124259093Seric 				bcopy(&buf[1], bodytype, i);
124359093Seric 				bodytype[i] = '\0';
124459093Seric 				break;
124559093Seric 
12469630Seric 			  case 'S':	/* sender name */
124724979Seric 				if (Verbose)
124858737Seric 					printf("%8ld %10ld%c%.12s %.38s",
124958737Seric 					    dfsize,
125058737Seric 					    w->w_pri,
125158737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
125258737Seric 					    ctime(&submittime) + 4,
125324979Seric 					    &buf[1]);
125424979Seric 				else
125524979Seric 					printf("%8ld %.16s %.45s", dfsize,
125624979Seric 					    ctime(&submittime), &buf[1]);
125759093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
125859093Seric 				{
125959093Seric 					printf("\n    %10.10s", bodytype);
126059093Seric 					if (message[0] != '\0')
126159093Seric 						printf("   (%.60s)", message);
126259093Seric 				}
12639630Seric 				break;
126451920Seric 
126540973Sbostic 			  case 'C':	/* controlling user */
126654974Seric 				if (Verbose)
126758716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
126858716Seric 						&buf[1]);
126940973Sbostic 				break;
12709630Seric 
12719630Seric 			  case 'R':	/* recipient name */
127224979Seric 				if (Verbose)
127358716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
127424979Seric 				else
127558716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12769630Seric 				break;
12779630Seric 
12789630Seric 			  case 'T':	/* creation time */
127924941Seric 				submittime = atol(&buf[1]);
12809630Seric 				break;
128110070Seric 
128210070Seric 			  case 'D':	/* data file name */
128310070Seric 				if (stat(&buf[1], &st) >= 0)
128410070Seric 					dfsize = st.st_size;
128510070Seric 				break;
128658737Seric 
128758737Seric 			  case 'F':	/* flag bits */
128858737Seric 				for (p = &buf[1]; *p != '\0'; p++)
128958737Seric 				{
129058737Seric 					switch (*p)
129158737Seric 					{
129258737Seric 					  case 'w':
129358737Seric 						flags |= EF_WARNING;
129458737Seric 						break;
129558737Seric 					}
129658737Seric 				}
12979630Seric 			}
12989630Seric 		}
129910070Seric 		if (submittime == (time_t) 0)
130010070Seric 			printf(" (no control file)");
13019630Seric 		printf("\n");
130223098Seric 		(void) fclose(f);
13039630Seric 	}
13049630Seric }
13059630Seric 
130656795Seric # endif /* QUEUE */
130717468Seric /*
130817468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
130917468Seric **
131017468Seric **	Assigns an id code if one does not already exist.
131117468Seric **	This code is very careful to avoid trashing existing files
131217468Seric **	under any circumstances.
131317468Seric **
131417468Seric **	Parameters:
131517468Seric **		e -- envelope to build it in/from.
131617468Seric **		type -- the file type, used as the first character
131717468Seric **			of the file name.
131817468Seric **
131917468Seric **	Returns:
132017468Seric **		a pointer to the new file name (in a static buffer).
132117468Seric **
132217468Seric **	Side Effects:
132351920Seric **		If no id code is already assigned, queuename will
132451920Seric **		assign an id code, create a qf file, and leave a
132551920Seric **		locked, open-for-write file pointer in the envelope.
132617468Seric */
132717468Seric 
132817468Seric char *
132917468Seric queuename(e, type)
133017468Seric 	register ENVELOPE *e;
133159700Seric 	int type;
133217468Seric {
133317468Seric 	static int pid = -1;
133458741Seric 	static char c0;
133558741Seric 	static char c1;
133658741Seric 	static char c2;
133758716Seric 	time_t now;
133858716Seric 	struct tm *tm;
133958689Seric 	static char buf[MAXNAME];
134017468Seric 
134117468Seric 	if (e->e_id == NULL)
134217468Seric 	{
134317468Seric 		char qf[20];
134417468Seric 
134517468Seric 		/* find a unique id */
134617468Seric 		if (pid != getpid())
134717468Seric 		{
134817468Seric 			/* new process -- start back at "AA" */
134917468Seric 			pid = getpid();
135058716Seric 			now = curtime();
135158716Seric 			tm = localtime(&now);
135258716Seric 			c0 = 'A' + tm->tm_hour;
135317468Seric 			c1 = 'A';
135417468Seric 			c2 = 'A' - 1;
135517468Seric 		}
135658716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
135717468Seric 
135817468Seric 		while (c1 < '~' || c2 < 'Z')
135917468Seric 		{
136017468Seric 			int i;
136117468Seric 
136217468Seric 			if (c2 >= 'Z')
136317468Seric 			{
136417468Seric 				c1++;
136517468Seric 				c2 = 'A' - 1;
136617468Seric 			}
136758716Seric 			qf[3] = c1;
136858716Seric 			qf[4] = ++c2;
136917468Seric 			if (tTd(7, 20))
137040934Srick 				printf("queuename: trying \"%s\"\n", qf);
137117468Seric 
137240934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
137351920Seric 			if (i < 0)
137451920Seric 			{
137551920Seric 				if (errno == EEXIST)
137651920Seric 					continue;
137764705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
137864705Seric 					qf, QueueDir, geteuid());
137951920Seric 				exit(EX_UNAVAILABLE);
138051920Seric 			}
138164335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
138251920Seric 			{
138351920Seric 				e->e_lockfp = fdopen(i, "w");
138440934Srick 				break;
138517468Seric 			}
138651920Seric 
138751920Seric 			/* a reader got the file; abandon it and try again */
138851920Seric 			(void) close(i);
138917468Seric 		}
139017468Seric 		if (c1 >= '~' && c2 >= 'Z')
139117468Seric 		{
139264705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
139364705Seric 				qf, QueueDir, geteuid());
139417468Seric 			exit(EX_OSERR);
139517468Seric 		}
139617468Seric 		e->e_id = newstr(&qf[2]);
139717468Seric 		define('i', e->e_id, e);
139817468Seric 		if (tTd(7, 1))
139917468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
140017468Seric # ifdef LOG
140158020Seric 		if (LogLevel > 93)
140217468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
140356795Seric # endif /* LOG */
140417468Seric 	}
140517468Seric 
140617468Seric 	if (type == '\0')
140717468Seric 		return (NULL);
140817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
140917468Seric 	if (tTd(7, 2))
141017468Seric 		printf("queuename: %s\n", buf);
141117468Seric 	return (buf);
141217468Seric }
141317468Seric /*
141417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
141517468Seric **
141617468Seric **	Parameters:
141717468Seric **		e -- the envelope to unlock.
141817468Seric **
141917468Seric **	Returns:
142017468Seric **		none
142117468Seric **
142217468Seric **	Side Effects:
142317468Seric **		unlocks the queue for `e'.
142417468Seric */
142517468Seric 
142617468Seric unlockqueue(e)
142717468Seric 	ENVELOPE *e;
142817468Seric {
142958680Seric 	if (tTd(51, 4))
143058680Seric 		printf("unlockqueue(%s)\n", e->e_id);
143158680Seric 
143251920Seric 	/* if there is a lock file in the envelope, close it */
143351920Seric 	if (e->e_lockfp != NULL)
143458680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
143551920Seric 	e->e_lockfp = NULL;
143651920Seric 
143758728Seric 	/* don't create a queue id if we don't already have one */
143858728Seric 	if (e->e_id == NULL)
143958728Seric 		return;
144058728Seric 
144117468Seric 	/* remove the transcript */
144217468Seric # ifdef LOG
144358020Seric 	if (LogLevel > 87)
144417468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
144556795Seric # endif /* LOG */
144658680Seric 	if (!tTd(51, 104))
144717468Seric 		xunlink(queuename(e, 'x'));
144817468Seric 
144917468Seric }
145040973Sbostic /*
145154974Seric **  SETCTLUSER -- create a controlling address
145240973Sbostic **
145354974Seric **	Create a fake "address" given only a local login name; this is
145454974Seric **	used as a "controlling user" for future recipient addresses.
145540973Sbostic **
145640973Sbostic **	Parameters:
145754974Seric **		user -- the user name of the controlling user.
145840973Sbostic **
145940973Sbostic **	Returns:
146054974Seric **		An address descriptor for the controlling user.
146140973Sbostic **
146240973Sbostic **	Side Effects:
146340973Sbostic **		none.
146440973Sbostic */
146540973Sbostic 
146654974Seric ADDRESS *
146754974Seric setctluser(user)
146854974Seric 	char *user;
146940973Sbostic {
147054974Seric 	register ADDRESS *a;
147140973Sbostic 	struct passwd *pw;
147259113Seric 	char *p;
147340973Sbostic 
147440973Sbostic 	/*
147554974Seric 	**  See if this clears our concept of controlling user.
147640973Sbostic 	*/
147740973Sbostic 
147863850Seric 	if (user == NULL || *user == '\0')
147963850Seric 		return NULL;
148040973Sbostic 
148140973Sbostic 	/*
148254974Seric 	**  Set up addr fields for controlling user.
148340973Sbostic 	*/
148440973Sbostic 
148554974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
148654974Seric 	bzero((char *) a, sizeof *a);
148759113Seric 
148859113Seric 	p = strchr(user, ':');
148959113Seric 	if (p != NULL)
149059113Seric 		*p++ = '\0';
149159270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
149240973Sbostic 	{
149340973Sbostic 		a->q_home = newstr(pw->pw_dir);
149440973Sbostic 		a->q_uid = pw->pw_uid;
149540973Sbostic 		a->q_gid = pw->pw_gid;
149657642Seric 		a->q_user = newstr(user);
149759270Seric 		a->q_flags |= QGOODUID;
149840973Sbostic 	}
149940973Sbostic 	else
150040973Sbostic 	{
150157642Seric 		a->q_user = newstr(DefUser);
150240973Sbostic 	}
150340973Sbostic 
150459270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
150556328Seric 	a->q_mailer = LocalMailer;
150659113Seric 	if (p == NULL)
150759113Seric 		a->q_paddr = a->q_user;
150859113Seric 	else
150959113Seric 		a->q_paddr = newstr(p);
151054974Seric 	return a;
151140973Sbostic }
1512