xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 65200)
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*65200Seric static char sccsid[] = "@(#)queue.c	8.33 (Berkeley) 12/22/93 (with queueing)";
1433731Sbostic #else
15*65200Seric static char sccsid[] = "@(#)queue.c	8.33 (Berkeley) 12/22/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)
9865090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
9965090Seric 						tf, geteuid(), errstring(errno));
10064070Seric #endif
10141636Srick 			}
10265090Seric 			else
10365090Seric 			{
10465090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10565090Seric 					break;
10664070Seric #ifdef LOG
10765090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
10865090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
10965090Seric 						tf, errstring(errno));
11064070Seric #endif
11165090Seric 				close(fd);
11265090Seric 			}
11358689Seric 
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)
12364921Seric 		{
12464921Seric 			printopenfds(TRUE);
12565090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
12665090Seric 				tf, geteuid());
12764921Seric 		}
12851920Seric 	}
1294632Seric 
1307677Seric 	if (tTd(40, 1))
13164745Seric 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
13264745Seric 			newid ? " (new id)" : "");
13364745Seric 	if (tTd(40, 9))
13464745Seric 	{
13564745Seric 		printf("  tfp=");
13664745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
13764745Seric 		printf("  lockfp=");
13864745Seric 		if (e->e_lockfp == NULL)
13964745Seric 			printf("NULL\n");
14064745Seric 		else
14164745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
14264745Seric 	}
1434632Seric 
1444632Seric 	/*
1456980Seric 	**  If there is no data file yet, create one.
1466980Seric 	*/
1476980Seric 
1486980Seric 	if (e->e_df == NULL)
1496980Seric 	{
1506980Seric 		register FILE *dfp;
1519389Seric 		extern putbody();
1526980Seric 
15364086Seric 		e->e_df = queuename(e, 'd');
15464086Seric 		e->e_df = newstr(e->e_df);
15540934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15664724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
15765090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
15865090Seric 				e->e_df, geteuid());
15959730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
16058680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1619389Seric 		e->e_putbody = putbody;
1626980Seric 	}
1636980Seric 
1646980Seric 	/*
1654632Seric 	**  Output future work requests.
16625687Seric 	**	Priority and creation time should be first, since
16725687Seric 	**	they are required by orderq.
1684632Seric 	*/
1694632Seric 
1709377Seric 	/* output message priority */
1719377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1729377Seric 
1739630Seric 	/* output creation time */
1749630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1759630Seric 
17659093Seric 	/* output type and name of data file */
17759093Seric 	if (e->e_bodytype != NULL)
17859093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1797812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1804632Seric 
18110108Seric 	/* message from envelope, if it exists */
18210108Seric 	if (e->e_message != NULL)
18310108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18410108Seric 
18558737Seric 	/* send various flag bits through */
18658737Seric 	p = buf;
18758737Seric 	if (bitset(EF_WARNING, e->e_flags))
18858737Seric 		*p++ = 'w';
18958737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
19058737Seric 		*p++ = 'r';
19158737Seric 	*p++ = '\0';
19258737Seric 	if (buf[0] != '\0')
19358737Seric 		fprintf(tfp, "F%s\n", buf);
19458737Seric 
19558957Seric 	/* $r and $s and $_ macro values */
19653400Seric 	if ((p = macvalue('r', e)) != NULL)
19753400Seric 		fprintf(tfp, "$r%s\n", p);
19853400Seric 	if ((p = macvalue('s', e)) != NULL)
19953400Seric 		fprintf(tfp, "$s%s\n", p);
20058957Seric 	if ((p = macvalue('_', e)) != NULL)
20158957Seric 		fprintf(tfp, "$_%s\n", p);
20253400Seric 
2034632Seric 	/* output name of sender */
2047812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
2054632Seric 
20655360Seric 	/* output list of error recipients */
20759670Seric 	printctladdr(NULL, NULL);
20855360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20955360Seric 	{
21058680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
21155360Seric 		{
21259113Seric 			printctladdr(q, tfp);
21355360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21455360Seric 		}
21555360Seric 	}
21655360Seric 
2174632Seric 	/* output list of recipient addresses */
2186980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2194632Seric 	{
22058250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
22158680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2228245Seric 		{
22359113Seric 			printctladdr(q, tfp);
2247812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2259377Seric 			if (announce)
2269377Seric 			{
2279377Seric 				e->e_to = q->q_paddr;
22858151Seric 				message("queued");
22958020Seric 				if (LogLevel > 8)
23064771Seric 					logdelivery(NULL, NULL, "queued", NULL, e);
2319377Seric 				e->e_to = NULL;
2329377Seric 			}
2339387Seric 			if (tTd(40, 1))
2349387Seric 			{
2359387Seric 				printf("queueing ");
2369387Seric 				printaddr(q, FALSE);
2379387Seric 			}
2388245Seric 		}
2394632Seric 	}
2404632Seric 
2419377Seric 	/*
2429377Seric 	**  Output headers for this message.
2439377Seric 	**	Expand macros completely here.  Queue run will deal with
2449377Seric 	**	everything as absolute headers.
2459377Seric 	**		All headers that must be relative to the recipient
2469377Seric 	**		can be cracked later.
24710173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24810173Seric 	**	no effect on the addresses as they are output.
2499377Seric 	*/
2509377Seric 
25110686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
25258020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
25364746Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = 0;
25410349Seric 	nullmailer.m_eol = "\n";
25510173Seric 
25658050Seric 	define('g', "\201f", e);
2576980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2584632Seric 	{
25910686Seric 		extern bool bitzerop();
26010686Seric 
26112015Seric 		/* don't output null headers */
2624632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2634632Seric 			continue;
26412015Seric 
26512015Seric 		/* don't output resent headers on non-resent messages */
26612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
26712015Seric 			continue;
26812015Seric 
26912015Seric 		/* output this header */
2707812Seric 		fprintf(tfp, "H");
27112015Seric 
27212015Seric 		/* if conditional, output the set of conditions */
27310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
27410686Seric 		{
27510686Seric 			int j;
27610686Seric 
27723098Seric 			(void) putc('?', tfp);
27810686Seric 			for (j = '\0'; j <= '\177'; j++)
27910686Seric 				if (bitnset(j, h->h_mflags))
28023098Seric 					(void) putc(j, tfp);
28123098Seric 			(void) putc('?', tfp);
28210686Seric 		}
28312015Seric 
28412015Seric 		/* output the header: expand macros, convert addresses */
2857763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2867763Seric 		{
2877763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
28865170Seric 			if (buf[0] != '\0')
28965170Seric 				fprintf(tfp, "%s: %s\n", h->h_field, buf);
2907763Seric 		}
2918245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2929348Seric 		{
29358737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
29463753Seric 			FILE *savetrace = TrafficLogFile;
29558737Seric 
29663753Seric 			TrafficLogFile = NULL;
29763753Seric 
29858737Seric 			if (bitset(H_FROM, h->h_flags))
29958737Seric 				oldstyle = FALSE;
30058737Seric 
30158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
30255012Seric 				 &nullmailer, e);
30363753Seric 
30463753Seric 			TrafficLogFile = savetrace;
3059348Seric 		}
3067763Seric 		else
3078245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3084632Seric 	}
3094632Seric 
3104632Seric 	/*
3114632Seric 	**  Clean up.
3124632Seric 	*/
3134632Seric 
31464762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
31558732Seric 	{
31658732Seric 		if (newid)
31758732Seric 			syserr("!552 Error writing control file %s", tf);
31858732Seric 		else
31958732Seric 			syserr("!452 Error writing control file %s", tf);
32058732Seric 	}
32158732Seric 
32251920Seric 	if (!newid)
32351920Seric 	{
32464277Seric 		/* rename (locked) tf to be (locked) qf */
32551920Seric 		qf = queuename(e, 'q');
32651920Seric 		if (rename(tf, qf) < 0)
32765090Seric 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
32865090Seric 				tf, qf, e->e_df, geteuid());
32964277Seric 
33064277Seric 		/* close and unlock old (locked) qf */
33151920Seric 		if (e->e_lockfp != NULL)
33258680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
33351920Seric 		e->e_lockfp = tfp;
33451920Seric 	}
33551920Seric 	else
33651920Seric 		qf = tf;
33741636Srick 	errno = 0;
33864745Seric 	e->e_flags |= EF_INQUEUE;
3397391Seric 
3407677Seric # ifdef LOG
3417677Seric 	/* save log info */
34258020Seric 	if (LogLevel > 79)
3437878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
34456795Seric # endif /* LOG */
34564307Seric 
34664307Seric 	if (tTd(40, 1))
34764307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
34851920Seric 	return;
3494632Seric }
35054974Seric 
35154974Seric printctladdr(a, tfp)
35259113Seric 	register ADDRESS *a;
35354974Seric 	FILE *tfp;
35454974Seric {
35559113Seric 	char *uname;
35659113Seric 	register struct passwd *pw;
35759113Seric 	register ADDRESS *q;
35859113Seric 	uid_t uid;
35959113Seric 	static ADDRESS *lastctladdr;
36059113Seric 	static uid_t lastuid;
36154974Seric 
36259113Seric 	/* initialization */
36363850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
36454974Seric 	{
36559670Seric 		if (lastctladdr != NULL && tfp != NULL)
36659113Seric 			fprintf(tfp, "C\n");
36759113Seric 		lastctladdr = NULL;
36859113Seric 		lastuid = 0;
36954974Seric 		return;
37054974Seric 	}
37159113Seric 
37259113Seric 	/* find the active uid */
37359113Seric 	q = getctladdr(a);
37459113Seric 	if (q == NULL)
37559113Seric 		uid = 0;
37654974Seric 	else
37759113Seric 		uid = q->q_uid;
37863850Seric 	a = a->q_alias;
37959113Seric 
38059113Seric 	/* check to see if this is the same as last time */
38159113Seric 	if (lastctladdr != NULL && uid == lastuid &&
38259113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
38359113Seric 		return;
38459113Seric 	lastuid = uid;
38559113Seric 	lastctladdr = a;
38659113Seric 
38759113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
38859270Seric 		uname = "";
38959113Seric 	else
39059113Seric 		uname = pw->pw_name;
39159113Seric 
39259113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
39354974Seric }
39454974Seric 
3954632Seric /*
3964632Seric **  RUNQUEUE -- run the jobs in the queue.
3974632Seric **
3984632Seric **	Gets the stuff out of the queue in some presumably logical
3994632Seric **	order and processes them.
4004632Seric **
4014632Seric **	Parameters:
40224941Seric **		forkflag -- TRUE if the queue scanning should be done in
40324941Seric **			a child process.  We double-fork so it is not our
40424941Seric **			child and we don't have to clean up after it.
4054632Seric **
4064632Seric **	Returns:
4074632Seric **		none.
4084632Seric **
4094632Seric **	Side Effects:
4104632Seric **		runs things in the mail queue.
4114632Seric */
4124632Seric 
41355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
41455360Seric 
41555360Seric runqueue(forkflag)
4164639Seric 	bool forkflag;
4174632Seric {
41855360Seric 	register ENVELOPE *e;
41955360Seric 	extern ENVELOPE BlankEnvelope;
42024953Seric 
4217466Seric 	/*
42224953Seric 	**  If no work will ever be selected, don't even bother reading
42324953Seric 	**  the queue.
42424953Seric 	*/
42524953Seric 
42651920Seric 	CurrentLA = getla();	/* get load average */
42740934Srick 
42858132Seric 	if (shouldqueue(0L, curtime()))
42924953Seric 	{
43024953Seric 		if (Verbose)
43124953Seric 			printf("Skipping queue run -- load average too high\n");
43258107Seric 		if (forkflag && QueueIntvl != 0)
43358107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
43455360Seric 		return;
43524953Seric 	}
43624953Seric 
43724953Seric 	/*
4387466Seric 	**  See if we want to go off and do other useful work.
4397466Seric 	*/
4404639Seric 
4414639Seric 	if (forkflag)
4424639Seric 	{
4437943Seric 		int pid;
4447943Seric 
4457943Seric 		pid = dofork();
4467943Seric 		if (pid != 0)
4474639Seric 		{
44846928Sbostic 			extern void reapchild();
44925184Seric 
4507943Seric 			/* parent -- pick up intermediate zombie */
45125184Seric #ifndef SIGCHLD
4529377Seric 			(void) waitfor(pid);
45356795Seric #else /* SIGCHLD */
45464035Seric 			(void) setsignal(SIGCHLD, reapchild);
45556795Seric #endif /* SIGCHLD */
4567690Seric 			if (QueueIntvl != 0)
4579348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4584639Seric 			return;
4594639Seric 		}
4607943Seric 		/* child -- double fork */
46125184Seric #ifndef SIGCHLD
4627943Seric 		if (fork() != 0)
4637943Seric 			exit(EX_OK);
46456795Seric #else /* SIGCHLD */
46564035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
46656795Seric #endif /* SIGCHLD */
4674639Seric 	}
46824941Seric 
46940934Srick 	setproctitle("running queue: %s", QueueDir);
47024941Seric 
4717876Seric # ifdef LOG
47258020Seric 	if (LogLevel > 69)
47355360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
47455360Seric 			QueueDir, getpid(), forkflag);
47556795Seric # endif /* LOG */
4764639Seric 
4777466Seric 	/*
47810205Seric 	**  Release any resources used by the daemon code.
47910205Seric 	*/
48010205Seric 
48110205Seric # ifdef DAEMON
48210205Seric 	clrdaemon();
48356795Seric # endif /* DAEMON */
48410205Seric 
48564658Seric 	/* force it to run expensive jobs */
48664658Seric 	NoConnect = FALSE;
48764658Seric 
48810205Seric 	/*
48955360Seric 	**  Create ourselves an envelope
49055360Seric 	*/
49155360Seric 
49255360Seric 	CurEnv = &QueueEnvelope;
49358179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
49455360Seric 	e->e_flags = BlankEnvelope.e_flags;
49555360Seric 
49655360Seric 	/*
49727175Seric 	**  Make sure the alias database is open.
49827175Seric 	*/
49927175Seric 
50060537Seric 	initmaps(FALSE, e);
50127175Seric 
50227175Seric 	/*
5037466Seric 	**  Start making passes through the queue.
5047466Seric 	**	First, read and sort the entire queue.
5057466Seric 	**	Then, process the work in that order.
5067466Seric 	**		But if you take too long, start over.
5077466Seric 	*/
5087466Seric 
5097943Seric 	/* order the existing work requests */
51024954Seric 	(void) orderq(FALSE);
5117690Seric 
5127943Seric 	/* process them once at a time */
5137943Seric 	while (WorkQ != NULL)
5144639Seric 	{
5157943Seric 		WORK *w = WorkQ;
5167881Seric 
5177943Seric 		WorkQ = WorkQ->w_next;
51858884Seric 
51958884Seric 		/*
52058884Seric 		**  Ignore jobs that are too expensive for the moment.
52158884Seric 		*/
52258884Seric 
52358884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
52458884Seric 		{
52558884Seric 			if (Verbose)
52658884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
52758884Seric 		}
52858930Seric 		else
52958930Seric 		{
53064296Seric 			pid_t pid;
53164296Seric 			extern pid_t dowork();
53264296Seric 
53364296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
53464296Seric 			errno = 0;
53564296Seric 			(void) waitfor(pid);
53658930Seric 		}
5377943Seric 		free(w->w_name);
5387943Seric 		free((char *) w);
5394639Seric 	}
54029866Seric 
54129866Seric 	/* exit without the usual cleanup */
54255467Seric 	e->e_id = NULL;
54355467Seric 	finis();
5444634Seric }
5454634Seric /*
5464632Seric **  ORDERQ -- order the work queue.
5474632Seric **
5484632Seric **	Parameters:
54924941Seric **		doall -- if set, include everything in the queue (even
55024941Seric **			the jobs that cannot be run because the load
55124941Seric **			average is too high).  Otherwise, exclude those
55224941Seric **			jobs.
5534632Seric **
5544632Seric **	Returns:
55510121Seric **		The number of request in the queue (not necessarily
55610121Seric **		the number of requests in WorkQ however).
5574632Seric **
5584632Seric **	Side Effects:
5594632Seric **		Sets WorkQ to the queue of available work, in order.
5604632Seric */
5614632Seric 
56225687Seric # define NEED_P		001
56325687Seric # define NEED_T		002
56458318Seric # define NEED_R		004
56558318Seric # define NEED_S		010
5664632Seric 
56724941Seric orderq(doall)
56824941Seric 	bool doall;
5694632Seric {
57060219Seric 	register struct dirent *d;
5714632Seric 	register WORK *w;
5726625Sglickman 	DIR *f;
5734632Seric 	register int i;
57425687Seric 	WORK wlist[QUEUESIZE+1];
57510070Seric 	int wn = -1;
5764632Seric 	extern workcmpf();
5774632Seric 
57858318Seric 	if (tTd(41, 1))
57958318Seric 	{
58058318Seric 		printf("orderq:\n");
58158318Seric 		if (QueueLimitId != NULL)
58258318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
58358318Seric 		if (QueueLimitSender != NULL)
58458318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
58558318Seric 		if (QueueLimitRecipient != NULL)
58658318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
58758318Seric 	}
58858318Seric 
5894632Seric 	/* clear out old WorkQ */
5904632Seric 	for (w = WorkQ; w != NULL; )
5914632Seric 	{
5924632Seric 		register WORK *nw = w->w_next;
5934632Seric 
5944632Seric 		WorkQ = nw;
5954632Seric 		free(w->w_name);
5964632Seric 		free((char *) w);
5974632Seric 		w = nw;
5984632Seric 	}
5994632Seric 
6004632Seric 	/* open the queue directory */
6018148Seric 	f = opendir(".");
6024632Seric 	if (f == NULL)
6034632Seric 	{
6048148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
60510070Seric 		return (0);
6064632Seric 	}
6074632Seric 
6084632Seric 	/*
6094632Seric 	**  Read the work directory.
6104632Seric 	*/
6114632Seric 
61210070Seric 	while ((d = readdir(f)) != NULL)
6134632Seric 	{
6149377Seric 		FILE *cf;
61564492Seric 		register char *p;
6164632Seric 		char lbuf[MAXNAME];
61758318Seric 		extern bool strcontainedin();
6184632Seric 
6194632Seric 		/* is this an interesting entry? */
6207812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6214632Seric 			continue;
6224632Seric 
62358318Seric 		if (QueueLimitId != NULL &&
62458318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
62558318Seric 			continue;
62658318Seric 
62758722Seric 		/*
62858722Seric 		**  Check queue name for plausibility.  This handles
62958722Seric 		**  both old and new type ids.
63058722Seric 		*/
63158722Seric 
63264492Seric 		p = d->d_name + 2;
63364492Seric 		if (isupper(p[0]) && isupper(p[2]))
63464492Seric 			p += 3;
63564492Seric 		else if (isupper(p[1]))
63664492Seric 			p += 2;
63764492Seric 		else
63864492Seric 			p = d->d_name;
63964492Seric 		for (i = 0; isdigit(*p); p++)
64064492Seric 			i++;
64164492Seric 		if (i < 5 || *p != '\0')
64258020Seric 		{
64358020Seric 			if (Verbose)
64458020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
64558020Seric #ifdef LOG
64658020Seric 			if (LogLevel > 3)
64759615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
64858020Seric 					d->d_name);
64958020Seric #endif
65058020Seric 			if (strlen(d->d_name) >= MAXNAME)
65158020Seric 				d->d_name[MAXNAME - 1] = '\0';
65258020Seric 			strcpy(lbuf, d->d_name);
65358020Seric 			lbuf[0] = 'Q';
65458020Seric 			(void) rename(d->d_name, lbuf);
65558020Seric 			continue;
65658020Seric 		}
65758020Seric 
65810070Seric 		/* yes -- open control file (if not too many files) */
65925687Seric 		if (++wn >= QUEUESIZE)
66010070Seric 			continue;
66158318Seric 
6628148Seric 		cf = fopen(d->d_name, "r");
6634632Seric 		if (cf == NULL)
6644632Seric 		{
6657055Seric 			/* this may be some random person sending hir msgs */
6667055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
66710090Seric 			if (tTd(41, 2))
66810090Seric 				printf("orderq: cannot open %s (%d)\n",
66910090Seric 					d->d_name, errno);
6707055Seric 			errno = 0;
67110090Seric 			wn--;
6724632Seric 			continue;
6734632Seric 		}
67425687Seric 		w = &wlist[wn];
67525687Seric 		w->w_name = newstr(d->d_name);
6764632Seric 
67725027Seric 		/* make sure jobs in creation don't clog queue */
67825687Seric 		w->w_pri = 0x7fffffff;
67925687Seric 		w->w_ctime = 0;
68025027Seric 
6814632Seric 		/* extract useful information */
68225687Seric 		i = NEED_P | NEED_T;
68358318Seric 		if (QueueLimitSender != NULL)
68458318Seric 			i |= NEED_S;
68558318Seric 		if (QueueLimitRecipient != NULL)
68658318Seric 			i |= NEED_R;
68725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6884632Seric 		{
68924954Seric 			extern long atol();
69058318Seric 			extern bool strcontainedin();
69124954Seric 
69224941Seric 			switch (lbuf[0])
6934632Seric 			{
69424941Seric 			  case 'P':
69525687Seric 				w->w_pri = atol(&lbuf[1]);
69625687Seric 				i &= ~NEED_P;
6974632Seric 				break;
69825013Seric 
69925013Seric 			  case 'T':
70025687Seric 				w->w_ctime = atol(&lbuf[1]);
70125687Seric 				i &= ~NEED_T;
70225013Seric 				break;
70358318Seric 
70458318Seric 			  case 'R':
70558318Seric 				if (QueueLimitRecipient != NULL &&
70658318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
70758318Seric 					i &= ~NEED_R;
70858318Seric 				break;
70958318Seric 
71058318Seric 			  case 'S':
71158318Seric 				if (QueueLimitSender != NULL &&
71258318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
71358318Seric 					i &= ~NEED_S;
71458318Seric 				break;
7154632Seric 			}
7164632Seric 		}
7174632Seric 		(void) fclose(cf);
71824953Seric 
71958318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
72058318Seric 		    bitset(NEED_R|NEED_S, i))
72124953Seric 		{
72224953Seric 			/* don't even bother sorting this job in */
72324953Seric 			wn--;
72424953Seric 		}
7254632Seric 	}
7266625Sglickman 	(void) closedir(f);
72710090Seric 	wn++;
7284632Seric 
7294632Seric 	/*
7304632Seric 	**  Sort the work directory.
7314632Seric 	*/
7324632Seric 
73325687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7344632Seric 
7354632Seric 	/*
7364632Seric 	**  Convert the work list into canonical form.
7379377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7384632Seric 	*/
7394632Seric 
74024981Seric 	WorkQ = NULL;
74125687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7424632Seric 	{
7434632Seric 		w = (WORK *) xalloc(sizeof *w);
7444632Seric 		w->w_name = wlist[i].w_name;
7454632Seric 		w->w_pri = wlist[i].w_pri;
74625013Seric 		w->w_ctime = wlist[i].w_ctime;
74724981Seric 		w->w_next = WorkQ;
74824981Seric 		WorkQ = w;
7494632Seric 	}
7504632Seric 
7517677Seric 	if (tTd(40, 1))
7524632Seric 	{
7534632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7545037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7554632Seric 	}
75610070Seric 
75710090Seric 	return (wn);
7584632Seric }
7594632Seric /*
7607677Seric **  WORKCMPF -- compare function for ordering work.
7614632Seric **
7624632Seric **	Parameters:
7634632Seric **		a -- the first argument.
7644632Seric **		b -- the second argument.
7654632Seric **
7664632Seric **	Returns:
76724981Seric **		-1 if a < b
76824981Seric **		 0 if a == b
76924981Seric **		+1 if a > b
7704632Seric **
7714632Seric **	Side Effects:
7724632Seric **		none.
7734632Seric */
7744632Seric 
7754632Seric workcmpf(a, b)
7765037Seric 	register WORK *a;
7775037Seric 	register WORK *b;
7784632Seric {
77957438Seric 	long pa = a->w_pri;
78057438Seric 	long pb = b->w_pri;
78124941Seric 
78224941Seric 	if (pa == pb)
7834632Seric 		return (0);
78424941Seric 	else if (pa > pb)
78524981Seric 		return (1);
78624981Seric 	else
78710121Seric 		return (-1);
7884632Seric }
7894632Seric /*
7904632Seric **  DOWORK -- do a work request.
7914632Seric **
7924632Seric **	Parameters:
79358884Seric **		id -- the ID of the job to run.
79458884Seric **		forkflag -- if set, run this in background.
79558924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
79658924Seric **			This is used when expanding aliases in the queue.
79761094Seric **			If forkflag is also set, it doesn't wait for the
79861094Seric **			child.
79958884Seric **		e - the envelope in which to run it.
8004632Seric **
8014632Seric **	Returns:
80264296Seric **		process id of process that is running the queue job.
8034632Seric **
8044632Seric **	Side Effects:
8054632Seric **		The work request is satisfied if possible.
8064632Seric */
8074632Seric 
80864296Seric pid_t
80958924Seric dowork(id, forkflag, requeueflag, e)
81058884Seric 	char *id;
81158884Seric 	bool forkflag;
81258924Seric 	bool requeueflag;
81355012Seric 	register ENVELOPE *e;
8144632Seric {
81564296Seric 	register pid_t pid;
81651920Seric 	extern bool readqf();
8174632Seric 
8187677Seric 	if (tTd(40, 1))
81958884Seric 		printf("dowork(%s)\n", id);
8204632Seric 
8214632Seric 	/*
82224941Seric 	**  Fork for work.
82324941Seric 	*/
82424941Seric 
82558884Seric 	if (forkflag)
82624941Seric 	{
82764296Seric 		pid = fork();
82864296Seric 		if (pid < 0)
82924941Seric 		{
83024941Seric 			syserr("dowork: cannot fork");
83164296Seric 			return 0;
83224941Seric 		}
83364839Seric 		else if (pid > 0)
83464839Seric 		{
83564839Seric 			/* parent -- clean out connection cache */
83664839Seric 			mci_flush(FALSE, NULL);
83764839Seric 		}
83824941Seric 	}
83924941Seric 	else
84024941Seric 	{
84164296Seric 		pid = 0;
84224941Seric 	}
84324941Seric 
84464296Seric 	if (pid == 0)
8454632Seric 	{
8464632Seric 		/*
8474632Seric 		**  CHILD
8488148Seric 		**	Lock the control file to avoid duplicate deliveries.
8498148Seric 		**		Then run the file as though we had just read it.
8507350Seric 		**	We save an idea of the temporary name so we
8517350Seric 		**		can recover on interrupt.
8524632Seric 		*/
8534632Seric 
8547763Seric 		/* set basic modes, etc. */
8557356Seric 		(void) alarm(0);
85655012Seric 		clearenvelope(e, FALSE);
85764554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
85858734Seric 		e->e_errormode = EM_MAIL;
85958884Seric 		e->e_id = id;
86064765Seric 		GrabTo = UseErrorsTo = FALSE;
86163846Seric 		if (forkflag)
86264554Seric 		{
86364296Seric 			disconnect(1, e);
86464554Seric 			OpMode = MD_DELIVER;
86564554Seric 		}
8667876Seric # ifdef LOG
86758020Seric 		if (LogLevel > 76)
86855012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8697881Seric 			       getpid());
87056795Seric # endif /* LOG */
8717763Seric 
8727763Seric 		/* don't use the headers from sendmail.cf... */
87355012Seric 		e->e_header = NULL;
8747763Seric 
87551920Seric 		/* read the queue control file -- return if locked */
876*65200Seric 		if (!readqf(e))
8776980Seric 		{
87858884Seric 			if (tTd(40, 4))
87958884Seric 				printf("readqf(%s) failed\n", e->e_id);
88058884Seric 			if (forkflag)
88124941Seric 				exit(EX_OK);
88224941Seric 			else
88324941Seric 				return;
8846980Seric 		}
8856980Seric 
88655012Seric 		e->e_flags |= EF_INQUEUE;
88758929Seric 		eatheader(e, requeueflag);
8886980Seric 
88958924Seric 		if (requeueflag)
89058924Seric 			queueup(e, TRUE, FALSE);
89158924Seric 
8926980Seric 		/* do the delivery */
89358915Seric 		sendall(e, SM_DELIVER);
8946980Seric 
8956980Seric 		/* finish up and exit */
89658884Seric 		if (forkflag)
89724941Seric 			finis();
89824941Seric 		else
89955012Seric 			dropenvelope(e);
9004632Seric 	}
90164296Seric 	e->e_id = NULL;
90264296Seric 	return pid;
9034632Seric }
9044632Seric /*
9054632Seric **  READQF -- read queue file and set up environment.
9064632Seric **
9074632Seric **	Parameters:
9089377Seric **		e -- the envelope of the job to run.
9094632Seric **
9104632Seric **	Returns:
91151920Seric **		TRUE if it successfully read the queue file.
91251920Seric **		FALSE otherwise.
9134632Seric **
9144632Seric **	Side Effects:
91551920Seric **		The queue file is returned locked.
9164632Seric */
9174632Seric 
91851920Seric bool
919*65200Seric readqf(e)
9209377Seric 	register ENVELOPE *e;
9214632Seric {
92217477Seric 	register FILE *qfp;
92354974Seric 	ADDRESS *ctladdr;
92456400Seric 	struct stat st;
92557135Seric 	char *bp;
92658915Seric 	char qf[20];
92757135Seric 	char buf[MAXLINE];
92824954Seric 	extern long atol();
92954974Seric 	extern ADDRESS *setctluser();
9304632Seric 
9314632Seric 	/*
93217468Seric 	**  Read and process the file.
9334632Seric 	*/
9344632Seric 
93558915Seric 	strcpy(qf, queuename(e, 'q'));
93651937Seric 	qfp = fopen(qf, "r+");
93717477Seric 	if (qfp == NULL)
93817477Seric 	{
93958884Seric 		if (tTd(40, 8))
94058884Seric 			printf("readqf(%s): fopen failure (%s)\n",
94158884Seric 				qf, errstring(errno));
94240934Srick 		if (errno != ENOENT)
94340934Srick 			syserr("readqf: no control file %s", qf);
94451920Seric 		return FALSE;
94517477Seric 	}
94640934Srick 
94764335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
94864277Seric 	{
94964277Seric 		/* being processed by another queuer */
95064277Seric 		if (tTd(40, 8))
95164277Seric 			printf("readqf(%s): locked\n", qf);
95264277Seric 		if (Verbose)
95364277Seric 			printf("%s: locked\n", e->e_id);
95464277Seric # ifdef LOG
95564277Seric 		if (LogLevel > 19)
95664277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
95764277Seric # endif /* LOG */
95864277Seric 		(void) fclose(qfp);
95964277Seric 		return FALSE;
96064277Seric 	}
96164277Seric 
96256400Seric 	/*
96356400Seric 	**  Check the queue file for plausibility to avoid attacks.
96456400Seric 	*/
96556400Seric 
96656400Seric 	if (fstat(fileno(qfp), &st) < 0)
96756400Seric 	{
96856400Seric 		/* must have been being processed by someone else */
96958884Seric 		if (tTd(40, 8))
97058884Seric 			printf("readqf(%s): fstat failure (%s)\n",
97158884Seric 				qf, errstring(errno));
97256400Seric 		fclose(qfp);
97356400Seric 		return FALSE;
97456400Seric 	}
97556400Seric 
97664137Seric 	if (st.st_uid != geteuid())
97756400Seric 	{
97856400Seric # ifdef LOG
97956400Seric 		if (LogLevel > 0)
98056400Seric 		{
98156400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
98256400Seric 				e->e_id, st.st_uid, st.st_mode);
98356400Seric 		}
98456795Seric # endif /* LOG */
98558884Seric 		if (tTd(40, 8))
98658884Seric 			printf("readqf(%s): bogus file\n", qf);
98764277Seric 		rename(qf, queuename(e, 'Q'));
98856400Seric 		fclose(qfp);
98956400Seric 		return FALSE;
99056400Seric 	}
99156400Seric 
99264277Seric 	if (st.st_size == 0)
99340934Srick 	{
99464277Seric 		/* must be a bogus file -- just remove it */
99564277Seric 		(void) unlink(qf);
99664277Seric 		fclose(qfp);
99751920Seric 		return FALSE;
99840934Srick 	}
99940934Srick 
100064277Seric 	if (st.st_nlink == 0)
100159101Seric 	{
100264277Seric 		/*
100364277Seric 		**  Race condition -- we got a file just as it was being
100464277Seric 		**  unlinked.  Just assume it is zero length.
100564277Seric 		*/
100664277Seric 
100759101Seric 		fclose(qfp);
100859101Seric 		return FALSE;
100959101Seric 	}
101059101Seric 
101164277Seric 	/* good file -- save this lock */
101251920Seric 	e->e_lockfp = qfp;
101351920Seric 
101440934Srick 	/* do basic system initialization */
101555012Seric 	initsys(e);
101665164Seric 	define('i', e->e_id, e);
101740934Srick 
10189377Seric 	LineNumber = 0;
101963850Seric 	e->e_flags |= EF_GLOBALERRS;
102063850Seric 	OpMode = MD_DELIVER;
102151920Seric 	if (Verbose)
10229377Seric 		printf("\nRunning %s\n", e->e_id);
102354974Seric 	ctladdr = NULL;
102457135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
10254632Seric 	{
102658737Seric 		register char *p;
102757529Seric 		struct stat st;
102857529Seric 
102926504Seric 		if (tTd(40, 4))
103057135Seric 			printf("+++++ %s\n", bp);
103157135Seric 		switch (bp[0])
10324632Seric 		{
103340973Sbostic 		  case 'C':		/* specify controlling user */
103457135Seric 			ctladdr = setctluser(&bp[1]);
103540973Sbostic 			break;
103640973Sbostic 
10374632Seric 		  case 'R':		/* specify recipient */
103858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10394632Seric 			break;
10404632Seric 
104125687Seric 		  case 'E':		/* specify error recipient */
104258082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
104325687Seric 			break;
104425687Seric 
10454632Seric 		  case 'H':		/* header */
104657135Seric 			(void) chompheader(&bp[1], FALSE, e);
10474632Seric 			break;
10484632Seric 
104910108Seric 		  case 'M':		/* message */
105064705Seric 			/* ignore this; we want a new message next time */
105110108Seric 			break;
105210108Seric 
10534632Seric 		  case 'S':		/* sender */
105458704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10554632Seric 			break;
10564632Seric 
105759093Seric 		  case 'B':		/* body type */
105859093Seric 			e->e_bodytype = newstr(&bp[1]);
105959093Seric 			break;
106059093Seric 
10614632Seric 		  case 'D':		/* data file name */
106257135Seric 			e->e_df = newstr(&bp[1]);
10639544Seric 			e->e_dfp = fopen(e->e_df, "r");
10649544Seric 			if (e->e_dfp == NULL)
106558020Seric 			{
10669377Seric 				syserr("readqf: cannot open %s", e->e_df);
106758020Seric 				e->e_msgsize = -1;
106858020Seric 			}
106958020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
107057529Seric 				e->e_msgsize = st.st_size;
10714632Seric 			break;
10724632Seric 
10737860Seric 		  case 'T':		/* init time */
107457135Seric 			e->e_ctime = atol(&bp[1]);
10754632Seric 			break;
10764632Seric 
10774634Seric 		  case 'P':		/* message priority */
107857135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10794634Seric 			break;
10804634Seric 
108158737Seric 		  case 'F':		/* flag bits */
108258737Seric 			for (p = &bp[1]; *p != '\0'; p++)
108358737Seric 			{
108458737Seric 				switch (*p)
108558737Seric 				{
108658737Seric 				  case 'w':	/* warning sent */
108758737Seric 					e->e_flags |= EF_WARNING;
108858737Seric 					break;
108958737Seric 
109058737Seric 				  case 'r':	/* response */
109158737Seric 					e->e_flags |= EF_RESPONSE;
109258737Seric 					break;
109358737Seric 				}
109458737Seric 			}
109558737Seric 			break;
109658737Seric 
109753400Seric 		  case '$':		/* define macro */
109857135Seric 			define(bp[1], newstr(&bp[2]), e);
109953400Seric 			break;
110053400Seric 
110124941Seric 		  case '\0':		/* blank line; ignore */
110224941Seric 			break;
110324941Seric 
11044632Seric 		  default:
1105*65200Seric 			syserr("readqf: %s: line %s: bad line \"%s\"",
1106*65200Seric 				qf, LineNumber, bp);
110763753Seric 			fclose(qfp);
110863753Seric 			rename(qf, queuename(e, 'Q'));
110963753Seric 			return FALSE;
11104632Seric 		}
111157135Seric 
111257135Seric 		if (bp != buf)
111357135Seric 			free(bp);
11144632Seric 	}
11159377Seric 
111624941Seric 	/*
111724941Seric 	**  If we haven't read any lines, this queue file is empty.
111824941Seric 	**  Arrange to remove it without referencing any null pointers.
111924941Seric 	*/
112024941Seric 
112124941Seric 	if (LineNumber == 0)
112224941Seric 	{
112324941Seric 		errno = 0;
112424941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
112524941Seric 	}
112651920Seric 	return TRUE;
11274632Seric }
11284632Seric /*
11299630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11309630Seric **
11319630Seric **	Parameters:
11329630Seric **		none.
11339630Seric **
11349630Seric **	Returns:
11359630Seric **		none.
11369630Seric **
11379630Seric **	Side Effects:
11389630Seric **		Prints a listing of the mail queue on the standard output.
11399630Seric */
11405182Seric 
11419630Seric printqueue()
11429630Seric {
11439630Seric 	register WORK *w;
11449630Seric 	FILE *f;
114510070Seric 	int nrequests;
11469630Seric 	char buf[MAXLINE];
11479630Seric 
11489630Seric 	/*
114958250Seric 	**  Check for permission to print the queue
115058250Seric 	*/
115158250Seric 
115264333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
115358250Seric 	{
115458250Seric 		struct stat st;
115558523Seric # ifdef NGROUPS
115658523Seric 		int n;
115763937Seric 		GIDSET_T gidset[NGROUPS];
115858523Seric # endif
115958250Seric 
116058517Seric 		if (stat(QueueDir, &st) < 0)
116158250Seric 		{
116258250Seric 			syserr("Cannot stat %s", QueueDir);
116358250Seric 			return;
116458250Seric 		}
116558523Seric # ifdef NGROUPS
116658523Seric 		n = getgroups(NGROUPS, gidset);
116758523Seric 		while (--n >= 0)
116858523Seric 		{
116958523Seric 			if (gidset[n] == st.st_gid)
117058523Seric 				break;
117158523Seric 		}
117258523Seric 		if (n < 0)
117358523Seric # else
117463787Seric 		if (RealGid != st.st_gid)
117558523Seric # endif
117658250Seric 		{
117758250Seric 			usrerr("510 You are not permitted to see the queue");
117858250Seric 			setstat(EX_NOPERM);
117958250Seric 			return;
118058250Seric 		}
118158250Seric 	}
118258250Seric 
118358250Seric 	/*
11849630Seric 	**  Read and order the queue.
11859630Seric 	*/
11869630Seric 
118724941Seric 	nrequests = orderq(TRUE);
11889630Seric 
11899630Seric 	/*
11909630Seric 	**  Print the work list that we have read.
11919630Seric 	*/
11929630Seric 
11939630Seric 	/* first see if there is anything */
119410070Seric 	if (nrequests <= 0)
11959630Seric 	{
119610070Seric 		printf("Mail queue is empty\n");
11979630Seric 		return;
11989630Seric 	}
11999630Seric 
120051920Seric 	CurrentLA = getla();	/* get load average */
120140934Srick 
120210096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
120325687Seric 	if (nrequests > QUEUESIZE)
120425687Seric 		printf(", only %d printed", QUEUESIZE);
120524979Seric 	if (Verbose)
120658716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
120724979Seric 	else
120858716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
12099630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
12109630Seric 	{
12119630Seric 		struct stat st;
121210070Seric 		auto time_t submittime = 0;
121310070Seric 		long dfsize = -1;
121458737Seric 		int flags = 0;
121510108Seric 		char message[MAXLINE];
121659093Seric 		char bodytype[MAXNAME];
12179630Seric 
121864355Seric 		printf("%8s", w->w_name + 2);
121917468Seric 		f = fopen(w->w_name, "r");
122017468Seric 		if (f == NULL)
122117468Seric 		{
122264355Seric 			printf(" (job completed)\n");
122317468Seric 			errno = 0;
122417468Seric 			continue;
122517468Seric 		}
122664335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
122710070Seric 			printf("*");
122857438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
122924941Seric 			printf("X");
123010070Seric 		else
123110070Seric 			printf(" ");
123210070Seric 		errno = 0;
123317468Seric 
123459093Seric 		message[0] = bodytype[0] = '\0';
12359630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12369630Seric 		{
123753400Seric 			register int i;
123858737Seric 			register char *p;
123953400Seric 
12409630Seric 			fixcrlf(buf, TRUE);
12419630Seric 			switch (buf[0])
12429630Seric 			{
124310108Seric 			  case 'M':	/* error message */
124453400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
124558737Seric 					i = sizeof message - 1;
124653400Seric 				bcopy(&buf[1], message, i);
124753400Seric 				message[i] = '\0';
124810108Seric 				break;
124910108Seric 
125059093Seric 			  case 'B':	/* body type */
125159093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
125259093Seric 					i = sizeof bodytype - 1;
125359093Seric 				bcopy(&buf[1], bodytype, i);
125459093Seric 				bodytype[i] = '\0';
125559093Seric 				break;
125659093Seric 
12579630Seric 			  case 'S':	/* sender name */
125824979Seric 				if (Verbose)
125958737Seric 					printf("%8ld %10ld%c%.12s %.38s",
126058737Seric 					    dfsize,
126158737Seric 					    w->w_pri,
126258737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
126358737Seric 					    ctime(&submittime) + 4,
126424979Seric 					    &buf[1]);
126524979Seric 				else
126624979Seric 					printf("%8ld %.16s %.45s", dfsize,
126724979Seric 					    ctime(&submittime), &buf[1]);
126859093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
126959093Seric 				{
127059093Seric 					printf("\n    %10.10s", bodytype);
127159093Seric 					if (message[0] != '\0')
127259093Seric 						printf("   (%.60s)", message);
127359093Seric 				}
12749630Seric 				break;
127551920Seric 
127640973Sbostic 			  case 'C':	/* controlling user */
127754974Seric 				if (Verbose)
127858716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
127958716Seric 						&buf[1]);
128040973Sbostic 				break;
12819630Seric 
12829630Seric 			  case 'R':	/* recipient name */
128324979Seric 				if (Verbose)
128458716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
128524979Seric 				else
128658716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12879630Seric 				break;
12889630Seric 
12899630Seric 			  case 'T':	/* creation time */
129024941Seric 				submittime = atol(&buf[1]);
12919630Seric 				break;
129210070Seric 
129310070Seric 			  case 'D':	/* data file name */
129410070Seric 				if (stat(&buf[1], &st) >= 0)
129510070Seric 					dfsize = st.st_size;
129610070Seric 				break;
129758737Seric 
129858737Seric 			  case 'F':	/* flag bits */
129958737Seric 				for (p = &buf[1]; *p != '\0'; p++)
130058737Seric 				{
130158737Seric 					switch (*p)
130258737Seric 					{
130358737Seric 					  case 'w':
130458737Seric 						flags |= EF_WARNING;
130558737Seric 						break;
130658737Seric 					}
130758737Seric 				}
13089630Seric 			}
13099630Seric 		}
131010070Seric 		if (submittime == (time_t) 0)
131110070Seric 			printf(" (no control file)");
13129630Seric 		printf("\n");
131323098Seric 		(void) fclose(f);
13149630Seric 	}
13159630Seric }
13169630Seric 
131756795Seric # endif /* QUEUE */
131817468Seric /*
131917468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
132017468Seric **
132117468Seric **	Assigns an id code if one does not already exist.
132217468Seric **	This code is very careful to avoid trashing existing files
132317468Seric **	under any circumstances.
132417468Seric **
132517468Seric **	Parameters:
132617468Seric **		e -- envelope to build it in/from.
132717468Seric **		type -- the file type, used as the first character
132817468Seric **			of the file name.
132917468Seric **
133017468Seric **	Returns:
133117468Seric **		a pointer to the new file name (in a static buffer).
133217468Seric **
133317468Seric **	Side Effects:
133451920Seric **		If no id code is already assigned, queuename will
133551920Seric **		assign an id code, create a qf file, and leave a
133651920Seric **		locked, open-for-write file pointer in the envelope.
133717468Seric */
133817468Seric 
133917468Seric char *
134017468Seric queuename(e, type)
134117468Seric 	register ENVELOPE *e;
134259700Seric 	int type;
134317468Seric {
134417468Seric 	static int pid = -1;
134558741Seric 	static char c0;
134658741Seric 	static char c1;
134758741Seric 	static char c2;
134858716Seric 	time_t now;
134958716Seric 	struct tm *tm;
135058689Seric 	static char buf[MAXNAME];
135117468Seric 
135217468Seric 	if (e->e_id == NULL)
135317468Seric 	{
135417468Seric 		char qf[20];
135517468Seric 
135617468Seric 		/* find a unique id */
135717468Seric 		if (pid != getpid())
135817468Seric 		{
135917468Seric 			/* new process -- start back at "AA" */
136017468Seric 			pid = getpid();
136158716Seric 			now = curtime();
136258716Seric 			tm = localtime(&now);
136358716Seric 			c0 = 'A' + tm->tm_hour;
136417468Seric 			c1 = 'A';
136517468Seric 			c2 = 'A' - 1;
136617468Seric 		}
136758716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
136817468Seric 
136917468Seric 		while (c1 < '~' || c2 < 'Z')
137017468Seric 		{
137117468Seric 			int i;
137217468Seric 
137317468Seric 			if (c2 >= 'Z')
137417468Seric 			{
137517468Seric 				c1++;
137617468Seric 				c2 = 'A' - 1;
137717468Seric 			}
137858716Seric 			qf[3] = c1;
137958716Seric 			qf[4] = ++c2;
138017468Seric 			if (tTd(7, 20))
138140934Srick 				printf("queuename: trying \"%s\"\n", qf);
138217468Seric 
138340934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
138451920Seric 			if (i < 0)
138551920Seric 			{
138651920Seric 				if (errno == EEXIST)
138751920Seric 					continue;
138864705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
138964705Seric 					qf, QueueDir, geteuid());
139051920Seric 				exit(EX_UNAVAILABLE);
139151920Seric 			}
139264335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
139351920Seric 			{
139451920Seric 				e->e_lockfp = fdopen(i, "w");
139540934Srick 				break;
139617468Seric 			}
139751920Seric 
139851920Seric 			/* a reader got the file; abandon it and try again */
139951920Seric 			(void) close(i);
140017468Seric 		}
140117468Seric 		if (c1 >= '~' && c2 >= 'Z')
140217468Seric 		{
140364705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
140464705Seric 				qf, QueueDir, geteuid());
140517468Seric 			exit(EX_OSERR);
140617468Seric 		}
140717468Seric 		e->e_id = newstr(&qf[2]);
140817468Seric 		define('i', e->e_id, e);
140917468Seric 		if (tTd(7, 1))
141017468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
141164745Seric 		if (tTd(7, 9))
141264745Seric 		{
141364745Seric 			printf("  lockfd=");
141464745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
141564745Seric 		}
141617468Seric # ifdef LOG
141758020Seric 		if (LogLevel > 93)
141817468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
141956795Seric # endif /* LOG */
142017468Seric 	}
142117468Seric 
142217468Seric 	if (type == '\0')
142317468Seric 		return (NULL);
142417468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
142517468Seric 	if (tTd(7, 2))
142617468Seric 		printf("queuename: %s\n", buf);
142717468Seric 	return (buf);
142817468Seric }
142917468Seric /*
143017468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
143117468Seric **
143217468Seric **	Parameters:
143317468Seric **		e -- the envelope to unlock.
143417468Seric **
143517468Seric **	Returns:
143617468Seric **		none
143717468Seric **
143817468Seric **	Side Effects:
143917468Seric **		unlocks the queue for `e'.
144017468Seric */
144117468Seric 
144217468Seric unlockqueue(e)
144317468Seric 	ENVELOPE *e;
144417468Seric {
144558680Seric 	if (tTd(51, 4))
144658680Seric 		printf("unlockqueue(%s)\n", e->e_id);
144758680Seric 
144851920Seric 	/* if there is a lock file in the envelope, close it */
144951920Seric 	if (e->e_lockfp != NULL)
145058680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
145151920Seric 	e->e_lockfp = NULL;
145251920Seric 
145358728Seric 	/* don't create a queue id if we don't already have one */
145458728Seric 	if (e->e_id == NULL)
145558728Seric 		return;
145658728Seric 
145717468Seric 	/* remove the transcript */
145817468Seric # ifdef LOG
145958020Seric 	if (LogLevel > 87)
146017468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
146156795Seric # endif /* LOG */
146258680Seric 	if (!tTd(51, 104))
146317468Seric 		xunlink(queuename(e, 'x'));
146417468Seric 
146517468Seric }
146640973Sbostic /*
146754974Seric **  SETCTLUSER -- create a controlling address
146840973Sbostic **
146954974Seric **	Create a fake "address" given only a local login name; this is
147054974Seric **	used as a "controlling user" for future recipient addresses.
147140973Sbostic **
147240973Sbostic **	Parameters:
147354974Seric **		user -- the user name of the controlling user.
147440973Sbostic **
147540973Sbostic **	Returns:
147654974Seric **		An address descriptor for the controlling user.
147740973Sbostic **
147840973Sbostic **	Side Effects:
147940973Sbostic **		none.
148040973Sbostic */
148140973Sbostic 
148254974Seric ADDRESS *
148354974Seric setctluser(user)
148454974Seric 	char *user;
148540973Sbostic {
148654974Seric 	register ADDRESS *a;
148740973Sbostic 	struct passwd *pw;
148859113Seric 	char *p;
148940973Sbostic 
149040973Sbostic 	/*
149154974Seric 	**  See if this clears our concept of controlling user.
149240973Sbostic 	*/
149340973Sbostic 
149463850Seric 	if (user == NULL || *user == '\0')
149563850Seric 		return NULL;
149640973Sbostic 
149740973Sbostic 	/*
149854974Seric 	**  Set up addr fields for controlling user.
149940973Sbostic 	*/
150040973Sbostic 
150154974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
150254974Seric 	bzero((char *) a, sizeof *a);
150359113Seric 
150459113Seric 	p = strchr(user, ':');
150559113Seric 	if (p != NULL)
150659113Seric 		*p++ = '\0';
150759270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
150840973Sbostic 	{
150940973Sbostic 		a->q_home = newstr(pw->pw_dir);
151040973Sbostic 		a->q_uid = pw->pw_uid;
151140973Sbostic 		a->q_gid = pw->pw_gid;
151257642Seric 		a->q_user = newstr(user);
151359270Seric 		a->q_flags |= QGOODUID;
151440973Sbostic 	}
151540973Sbostic 	else
151640973Sbostic 	{
151757642Seric 		a->q_user = newstr(DefUser);
151840973Sbostic 	}
151940973Sbostic 
152059270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
152156328Seric 	a->q_mailer = LocalMailer;
152259113Seric 	if (p == NULL)
152359113Seric 		a->q_paddr = a->q_user;
152459113Seric 	else
152559113Seric 		a->q_paddr = newstr(p);
152654974Seric 	return a;
152740973Sbostic }
1528