xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64745)
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*64745Seric static char sccsid[] = "@(#)queue.c	8.23 (Berkeley) 10/23/93 (with queueing)";
1433731Sbostic #else
15*64745Seric static char sccsid[] = "@(#)queue.c	8.23 (Berkeley) 10/23/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 
77*64745Seric 	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) */
86*64745Seric 	if (!newid)
8751835Seric 	{
8851920Seric 		/* get a locked tf file */
8964070Seric 		for (i = 0; i < 128; i++)
9051835Seric 		{
9151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9251920Seric 			if (fd < 0)
9351835Seric 			{
9464070Seric 				if (errno != EEXIST)
9564070Seric 					break;
9664070Seric #ifdef LOG
9764070Seric 				if (LogLevel > 0 && (i % 32) == 0)
9864070Seric 					syslog(LOG_ALERT, "queueup: cannot create %s: %s",
9964070Seric 						tf, errstring(errno));
10064070Seric #endif
10164070Seric 				continue;
10241636Srick 			}
10358689Seric 
10464335Seric 			if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10551920Seric 				break;
10664070Seric #ifdef LOG
10764070Seric 			else if (LogLevel > 0 && (i % 32) == 0)
10864070Seric 				syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
10964070Seric 					tf, errstring(errno));
11064070Seric #endif
11158689Seric 
11251920Seric 			close(fd);
11364070Seric 
11464070Seric 			if ((i % 32) == 31)
11564070Seric 			{
11664070Seric 				/* save the old temp file away */
11764070Seric 				(void) rename(tf, queuename(e, 'T'));
11864070Seric 			}
11964070Seric 			else
12064070Seric 				sleep(i % 32);
12141636Srick 		}
12264724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12364724Seric 			syserr("!queueup: cannot create queue temp file %s", tf);
12451920Seric 	}
1254632Seric 
1267677Seric 	if (tTd(40, 1))
127*64745Seric 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
128*64745Seric 			newid ? " (new id)" : "");
129*64745Seric 	if (tTd(40, 9))
130*64745Seric 	{
131*64745Seric 		printf("  tfp=");
132*64745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
133*64745Seric 		printf("  lockfp=");
134*64745Seric 		if (e->e_lockfp == NULL)
135*64745Seric 			printf("NULL\n");
136*64745Seric 		else
137*64745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
138*64745Seric 	}
1394632Seric 
1404632Seric 	/*
1416980Seric 	**  If there is no data file yet, create one.
1426980Seric 	*/
1436980Seric 
1446980Seric 	if (e->e_df == NULL)
1456980Seric 	{
1466980Seric 		register FILE *dfp;
1479389Seric 		extern putbody();
1486980Seric 
14964086Seric 		e->e_df = queuename(e, 'd');
15064086Seric 		e->e_df = newstr(e->e_df);
15140934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15264724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
15364724Seric 			syserr("!queueup: cannot create data temp file %s",
15464724Seric 				e->e_df);
15559730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
15658680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1579389Seric 		e->e_putbody = putbody;
1586980Seric 	}
1596980Seric 
1606980Seric 	/*
1614632Seric 	**  Output future work requests.
16225687Seric 	**	Priority and creation time should be first, since
16325687Seric 	**	they are required by orderq.
1644632Seric 	*/
1654632Seric 
1669377Seric 	/* output message priority */
1679377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1689377Seric 
1699630Seric 	/* output creation time */
1709630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1719630Seric 
17259093Seric 	/* output type and name of data file */
17359093Seric 	if (e->e_bodytype != NULL)
17459093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1757812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1764632Seric 
17710108Seric 	/* message from envelope, if it exists */
17810108Seric 	if (e->e_message != NULL)
17910108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18010108Seric 
18158737Seric 	/* send various flag bits through */
18258737Seric 	p = buf;
18358737Seric 	if (bitset(EF_WARNING, e->e_flags))
18458737Seric 		*p++ = 'w';
18558737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
18658737Seric 		*p++ = 'r';
18758737Seric 	*p++ = '\0';
18858737Seric 	if (buf[0] != '\0')
18958737Seric 		fprintf(tfp, "F%s\n", buf);
19058737Seric 
19158957Seric 	/* $r and $s and $_ macro values */
19253400Seric 	if ((p = macvalue('r', e)) != NULL)
19353400Seric 		fprintf(tfp, "$r%s\n", p);
19453400Seric 	if ((p = macvalue('s', e)) != NULL)
19553400Seric 		fprintf(tfp, "$s%s\n", p);
19658957Seric 	if ((p = macvalue('_', e)) != NULL)
19758957Seric 		fprintf(tfp, "$_%s\n", p);
19853400Seric 
1994632Seric 	/* output name of sender */
2007812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
2014632Seric 
20255360Seric 	/* output list of error recipients */
20359670Seric 	printctladdr(NULL, NULL);
20455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20555360Seric 	{
20658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
20755360Seric 		{
20859113Seric 			printctladdr(q, tfp);
20955360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21055360Seric 		}
21155360Seric 	}
21255360Seric 
2134632Seric 	/* output list of recipient addresses */
2146980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2154632Seric 	{
21658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2188245Seric 		{
21959113Seric 			printctladdr(q, tfp);
2207812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2219377Seric 			if (announce)
2229377Seric 			{
2239377Seric 				e->e_to = q->q_paddr;
22458151Seric 				message("queued");
22558020Seric 				if (LogLevel > 8)
22658337Seric 					logdelivery(NULL, NULL, "queued", e);
2279377Seric 				e->e_to = NULL;
2289377Seric 			}
2299387Seric 			if (tTd(40, 1))
2309387Seric 			{
2319387Seric 				printf("queueing ");
2329387Seric 				printaddr(q, FALSE);
2339387Seric 			}
2348245Seric 		}
2354632Seric 	}
2364632Seric 
2379377Seric 	/*
2389377Seric 	**  Output headers for this message.
2399377Seric 	**	Expand macros completely here.  Queue run will deal with
2409377Seric 	**	everything as absolute headers.
2419377Seric 	**		All headers that must be relative to the recipient
2429377Seric 	**		can be cracked later.
24310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24410173Seric 	**	no effect on the addresses as they are output.
2459377Seric 	*/
2469377Seric 
24710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24958020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
25010349Seric 	nullmailer.m_eol = "\n";
25110173Seric 
25258050Seric 	define('g', "\201f", e);
2536980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2544632Seric 	{
25510686Seric 		extern bool bitzerop();
25610686Seric 
25712015Seric 		/* don't output null headers */
2584632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2594632Seric 			continue;
26012015Seric 
26112015Seric 		/* don't output resent headers on non-resent messages */
26212015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
26312015Seric 			continue;
26412015Seric 
26512015Seric 		/* output this header */
2667812Seric 		fprintf(tfp, "H");
26712015Seric 
26812015Seric 		/* if conditional, output the set of conditions */
26910686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
27010686Seric 		{
27110686Seric 			int j;
27210686Seric 
27323098Seric 			(void) putc('?', tfp);
27410686Seric 			for (j = '\0'; j <= '\177'; j++)
27510686Seric 				if (bitnset(j, h->h_mflags))
27623098Seric 					(void) putc(j, tfp);
27723098Seric 			(void) putc('?', tfp);
27810686Seric 		}
27912015Seric 
28012015Seric 		/* output the header: expand macros, convert addresses */
2817763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2827763Seric 		{
2837763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2848236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2857763Seric 		}
2868245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2879348Seric 		{
28858737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28963753Seric 			FILE *savetrace = TrafficLogFile;
29058737Seric 
29163753Seric 			TrafficLogFile = NULL;
29263753Seric 
29358737Seric 			if (bitset(H_FROM, h->h_flags))
29458737Seric 				oldstyle = FALSE;
29558737Seric 
29658737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29755012Seric 				 &nullmailer, e);
29863753Seric 
29963753Seric 			TrafficLogFile = savetrace;
3009348Seric 		}
3017763Seric 		else
3028245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3034632Seric 	}
3044632Seric 
3054632Seric 	/*
3064632Seric 	**  Clean up.
3074632Seric 	*/
3084632Seric 
30958732Seric 	fflush(tfp);
31060603Seric 	fsync(fileno(tfp));
31158732Seric 	if (ferror(tfp))
31258732Seric 	{
31358732Seric 		if (newid)
31458732Seric 			syserr("!552 Error writing control file %s", tf);
31558732Seric 		else
31658732Seric 			syserr("!452 Error writing control file %s", tf);
31758732Seric 	}
31858732Seric 
31951920Seric 	if (!newid)
32051920Seric 	{
32164277Seric 		/* rename (locked) tf to be (locked) qf */
32251920Seric 		qf = queuename(e, 'q');
32351920Seric 		if (rename(tf, qf) < 0)
32451920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
32564277Seric 
32664277Seric 		/* close and unlock old (locked) qf */
32751920Seric 		if (e->e_lockfp != NULL)
32858680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
32951920Seric 		e->e_lockfp = tfp;
33051920Seric 	}
33151920Seric 	else
33251920Seric 		qf = tf;
33341636Srick 	errno = 0;
334*64745Seric 	e->e_flags |= EF_INQUEUE;
3357391Seric 
3367677Seric # ifdef LOG
3377677Seric 	/* save log info */
33858020Seric 	if (LogLevel > 79)
3397878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
34056795Seric # endif /* LOG */
34164307Seric 
34264307Seric 	if (tTd(40, 1))
34364307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
34451920Seric 	return;
3454632Seric }
34654974Seric 
34754974Seric printctladdr(a, tfp)
34859113Seric 	register ADDRESS *a;
34954974Seric 	FILE *tfp;
35054974Seric {
35159113Seric 	char *uname;
35259113Seric 	register struct passwd *pw;
35359113Seric 	register ADDRESS *q;
35459113Seric 	uid_t uid;
35559113Seric 	static ADDRESS *lastctladdr;
35659113Seric 	static uid_t lastuid;
35754974Seric 
35859113Seric 	/* initialization */
35963850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
36054974Seric 	{
36159670Seric 		if (lastctladdr != NULL && tfp != NULL)
36259113Seric 			fprintf(tfp, "C\n");
36359113Seric 		lastctladdr = NULL;
36459113Seric 		lastuid = 0;
36554974Seric 		return;
36654974Seric 	}
36759113Seric 
36859113Seric 	/* find the active uid */
36959113Seric 	q = getctladdr(a);
37059113Seric 	if (q == NULL)
37159113Seric 		uid = 0;
37254974Seric 	else
37359113Seric 		uid = q->q_uid;
37463850Seric 	a = a->q_alias;
37559113Seric 
37659113Seric 	/* check to see if this is the same as last time */
37759113Seric 	if (lastctladdr != NULL && uid == lastuid &&
37859113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
37959113Seric 		return;
38059113Seric 	lastuid = uid;
38159113Seric 	lastctladdr = a;
38259113Seric 
38359113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
38459270Seric 		uname = "";
38559113Seric 	else
38659113Seric 		uname = pw->pw_name;
38759113Seric 
38859113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
38954974Seric }
39054974Seric 
3914632Seric /*
3924632Seric **  RUNQUEUE -- run the jobs in the queue.
3934632Seric **
3944632Seric **	Gets the stuff out of the queue in some presumably logical
3954632Seric **	order and processes them.
3964632Seric **
3974632Seric **	Parameters:
39824941Seric **		forkflag -- TRUE if the queue scanning should be done in
39924941Seric **			a child process.  We double-fork so it is not our
40024941Seric **			child and we don't have to clean up after it.
4014632Seric **
4024632Seric **	Returns:
4034632Seric **		none.
4044632Seric **
4054632Seric **	Side Effects:
4064632Seric **		runs things in the mail queue.
4074632Seric */
4084632Seric 
40955360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
41055360Seric 
41155360Seric runqueue(forkflag)
4124639Seric 	bool forkflag;
4134632Seric {
41455360Seric 	register ENVELOPE *e;
41555360Seric 	extern ENVELOPE BlankEnvelope;
41624953Seric 
4177466Seric 	/*
41824953Seric 	**  If no work will ever be selected, don't even bother reading
41924953Seric 	**  the queue.
42024953Seric 	*/
42124953Seric 
42251920Seric 	CurrentLA = getla();	/* get load average */
42340934Srick 
42458132Seric 	if (shouldqueue(0L, curtime()))
42524953Seric 	{
42624953Seric 		if (Verbose)
42724953Seric 			printf("Skipping queue run -- load average too high\n");
42858107Seric 		if (forkflag && QueueIntvl != 0)
42958107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
43055360Seric 		return;
43124953Seric 	}
43224953Seric 
43324953Seric 	/*
4347466Seric 	**  See if we want to go off and do other useful work.
4357466Seric 	*/
4364639Seric 
4374639Seric 	if (forkflag)
4384639Seric 	{
4397943Seric 		int pid;
4407943Seric 
4417943Seric 		pid = dofork();
4427943Seric 		if (pid != 0)
4434639Seric 		{
44446928Sbostic 			extern void reapchild();
44525184Seric 
4467943Seric 			/* parent -- pick up intermediate zombie */
44725184Seric #ifndef SIGCHLD
4489377Seric 			(void) waitfor(pid);
44956795Seric #else /* SIGCHLD */
45064035Seric 			(void) setsignal(SIGCHLD, reapchild);
45156795Seric #endif /* SIGCHLD */
4527690Seric 			if (QueueIntvl != 0)
4539348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4544639Seric 			return;
4554639Seric 		}
4567943Seric 		/* child -- double fork */
45725184Seric #ifndef SIGCHLD
4587943Seric 		if (fork() != 0)
4597943Seric 			exit(EX_OK);
46056795Seric #else /* SIGCHLD */
46164035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
46256795Seric #endif /* SIGCHLD */
4634639Seric 	}
46424941Seric 
46540934Srick 	setproctitle("running queue: %s", QueueDir);
46624941Seric 
4677876Seric # ifdef LOG
46858020Seric 	if (LogLevel > 69)
46955360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
47055360Seric 			QueueDir, getpid(), forkflag);
47156795Seric # endif /* LOG */
4724639Seric 
4737466Seric 	/*
47410205Seric 	**  Release any resources used by the daemon code.
47510205Seric 	*/
47610205Seric 
47710205Seric # ifdef DAEMON
47810205Seric 	clrdaemon();
47956795Seric # endif /* DAEMON */
48010205Seric 
48164658Seric 	/* force it to run expensive jobs */
48264658Seric 	NoConnect = FALSE;
48364658Seric 
48410205Seric 	/*
48555360Seric 	**  Create ourselves an envelope
48655360Seric 	*/
48755360Seric 
48855360Seric 	CurEnv = &QueueEnvelope;
48958179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
49055360Seric 	e->e_flags = BlankEnvelope.e_flags;
49155360Seric 
49255360Seric 	/*
49327175Seric 	**  Make sure the alias database is open.
49427175Seric 	*/
49527175Seric 
49660537Seric 	initmaps(FALSE, e);
49727175Seric 
49827175Seric 	/*
4997466Seric 	**  Start making passes through the queue.
5007466Seric 	**	First, read and sort the entire queue.
5017466Seric 	**	Then, process the work in that order.
5027466Seric 	**		But if you take too long, start over.
5037466Seric 	*/
5047466Seric 
5057943Seric 	/* order the existing work requests */
50624954Seric 	(void) orderq(FALSE);
5077690Seric 
5087943Seric 	/* process them once at a time */
5097943Seric 	while (WorkQ != NULL)
5104639Seric 	{
5117943Seric 		WORK *w = WorkQ;
5127881Seric 
5137943Seric 		WorkQ = WorkQ->w_next;
51458884Seric 
51558884Seric 		/*
51658884Seric 		**  Ignore jobs that are too expensive for the moment.
51758884Seric 		*/
51858884Seric 
51958884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
52058884Seric 		{
52158884Seric 			if (Verbose)
52258884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
52358884Seric 		}
52458930Seric 		else
52558930Seric 		{
52664296Seric 			pid_t pid;
52764296Seric 			extern pid_t dowork();
52864296Seric 
52964296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
53064296Seric 			errno = 0;
53164296Seric 			(void) waitfor(pid);
53258930Seric 		}
5337943Seric 		free(w->w_name);
5347943Seric 		free((char *) w);
5354639Seric 	}
53629866Seric 
53729866Seric 	/* exit without the usual cleanup */
53855467Seric 	e->e_id = NULL;
53955467Seric 	finis();
5404634Seric }
5414634Seric /*
5424632Seric **  ORDERQ -- order the work queue.
5434632Seric **
5444632Seric **	Parameters:
54524941Seric **		doall -- if set, include everything in the queue (even
54624941Seric **			the jobs that cannot be run because the load
54724941Seric **			average is too high).  Otherwise, exclude those
54824941Seric **			jobs.
5494632Seric **
5504632Seric **	Returns:
55110121Seric **		The number of request in the queue (not necessarily
55210121Seric **		the number of requests in WorkQ however).
5534632Seric **
5544632Seric **	Side Effects:
5554632Seric **		Sets WorkQ to the queue of available work, in order.
5564632Seric */
5574632Seric 
55825687Seric # define NEED_P		001
55925687Seric # define NEED_T		002
56058318Seric # define NEED_R		004
56158318Seric # define NEED_S		010
5624632Seric 
56324941Seric orderq(doall)
56424941Seric 	bool doall;
5654632Seric {
56660219Seric 	register struct dirent *d;
5674632Seric 	register WORK *w;
5686625Sglickman 	DIR *f;
5694632Seric 	register int i;
57025687Seric 	WORK wlist[QUEUESIZE+1];
57110070Seric 	int wn = -1;
5724632Seric 	extern workcmpf();
5734632Seric 
57458318Seric 	if (tTd(41, 1))
57558318Seric 	{
57658318Seric 		printf("orderq:\n");
57758318Seric 		if (QueueLimitId != NULL)
57858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
57958318Seric 		if (QueueLimitSender != NULL)
58058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
58158318Seric 		if (QueueLimitRecipient != NULL)
58258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
58358318Seric 	}
58458318Seric 
5854632Seric 	/* clear out old WorkQ */
5864632Seric 	for (w = WorkQ; w != NULL; )
5874632Seric 	{
5884632Seric 		register WORK *nw = w->w_next;
5894632Seric 
5904632Seric 		WorkQ = nw;
5914632Seric 		free(w->w_name);
5924632Seric 		free((char *) w);
5934632Seric 		w = nw;
5944632Seric 	}
5954632Seric 
5964632Seric 	/* open the queue directory */
5978148Seric 	f = opendir(".");
5984632Seric 	if (f == NULL)
5994632Seric 	{
6008148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
60110070Seric 		return (0);
6024632Seric 	}
6034632Seric 
6044632Seric 	/*
6054632Seric 	**  Read the work directory.
6064632Seric 	*/
6074632Seric 
60810070Seric 	while ((d = readdir(f)) != NULL)
6094632Seric 	{
6109377Seric 		FILE *cf;
61164492Seric 		register char *p;
6124632Seric 		char lbuf[MAXNAME];
61358318Seric 		extern bool strcontainedin();
6144632Seric 
6154632Seric 		/* is this an interesting entry? */
6167812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6174632Seric 			continue;
6184632Seric 
61958318Seric 		if (QueueLimitId != NULL &&
62058318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
62158318Seric 			continue;
62258318Seric 
62358722Seric 		/*
62458722Seric 		**  Check queue name for plausibility.  This handles
62558722Seric 		**  both old and new type ids.
62658722Seric 		*/
62758722Seric 
62864492Seric 		p = d->d_name + 2;
62964492Seric 		if (isupper(p[0]) && isupper(p[2]))
63064492Seric 			p += 3;
63164492Seric 		else if (isupper(p[1]))
63264492Seric 			p += 2;
63364492Seric 		else
63464492Seric 			p = d->d_name;
63564492Seric 		for (i = 0; isdigit(*p); p++)
63664492Seric 			i++;
63764492Seric 		if (i < 5 || *p != '\0')
63858020Seric 		{
63958020Seric 			if (Verbose)
64058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
64158020Seric #ifdef LOG
64258020Seric 			if (LogLevel > 3)
64359615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
64458020Seric 					d->d_name);
64558020Seric #endif
64658020Seric 			if (strlen(d->d_name) >= MAXNAME)
64758020Seric 				d->d_name[MAXNAME - 1] = '\0';
64858020Seric 			strcpy(lbuf, d->d_name);
64958020Seric 			lbuf[0] = 'Q';
65058020Seric 			(void) rename(d->d_name, lbuf);
65158020Seric 			continue;
65258020Seric 		}
65358020Seric 
65410070Seric 		/* yes -- open control file (if not too many files) */
65525687Seric 		if (++wn >= QUEUESIZE)
65610070Seric 			continue;
65758318Seric 
6588148Seric 		cf = fopen(d->d_name, "r");
6594632Seric 		if (cf == NULL)
6604632Seric 		{
6617055Seric 			/* this may be some random person sending hir msgs */
6627055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
66310090Seric 			if (tTd(41, 2))
66410090Seric 				printf("orderq: cannot open %s (%d)\n",
66510090Seric 					d->d_name, errno);
6667055Seric 			errno = 0;
66710090Seric 			wn--;
6684632Seric 			continue;
6694632Seric 		}
67025687Seric 		w = &wlist[wn];
67125687Seric 		w->w_name = newstr(d->d_name);
6724632Seric 
67325027Seric 		/* make sure jobs in creation don't clog queue */
67425687Seric 		w->w_pri = 0x7fffffff;
67525687Seric 		w->w_ctime = 0;
67625027Seric 
6774632Seric 		/* extract useful information */
67825687Seric 		i = NEED_P | NEED_T;
67958318Seric 		if (QueueLimitSender != NULL)
68058318Seric 			i |= NEED_S;
68158318Seric 		if (QueueLimitRecipient != NULL)
68258318Seric 			i |= NEED_R;
68325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6844632Seric 		{
68524954Seric 			extern long atol();
68658318Seric 			extern bool strcontainedin();
68724954Seric 
68824941Seric 			switch (lbuf[0])
6894632Seric 			{
69024941Seric 			  case 'P':
69125687Seric 				w->w_pri = atol(&lbuf[1]);
69225687Seric 				i &= ~NEED_P;
6934632Seric 				break;
69425013Seric 
69525013Seric 			  case 'T':
69625687Seric 				w->w_ctime = atol(&lbuf[1]);
69725687Seric 				i &= ~NEED_T;
69825013Seric 				break;
69958318Seric 
70058318Seric 			  case 'R':
70158318Seric 				if (QueueLimitRecipient != NULL &&
70258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
70358318Seric 					i &= ~NEED_R;
70458318Seric 				break;
70558318Seric 
70658318Seric 			  case 'S':
70758318Seric 				if (QueueLimitSender != NULL &&
70858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
70958318Seric 					i &= ~NEED_S;
71058318Seric 				break;
7114632Seric 			}
7124632Seric 		}
7134632Seric 		(void) fclose(cf);
71424953Seric 
71558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
71658318Seric 		    bitset(NEED_R|NEED_S, i))
71724953Seric 		{
71824953Seric 			/* don't even bother sorting this job in */
71924953Seric 			wn--;
72024953Seric 		}
7214632Seric 	}
7226625Sglickman 	(void) closedir(f);
72310090Seric 	wn++;
7244632Seric 
7254632Seric 	/*
7264632Seric 	**  Sort the work directory.
7274632Seric 	*/
7284632Seric 
72925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7304632Seric 
7314632Seric 	/*
7324632Seric 	**  Convert the work list into canonical form.
7339377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7344632Seric 	*/
7354632Seric 
73624981Seric 	WorkQ = NULL;
73725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7384632Seric 	{
7394632Seric 		w = (WORK *) xalloc(sizeof *w);
7404632Seric 		w->w_name = wlist[i].w_name;
7414632Seric 		w->w_pri = wlist[i].w_pri;
74225013Seric 		w->w_ctime = wlist[i].w_ctime;
74324981Seric 		w->w_next = WorkQ;
74424981Seric 		WorkQ = w;
7454632Seric 	}
7464632Seric 
7477677Seric 	if (tTd(40, 1))
7484632Seric 	{
7494632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7505037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7514632Seric 	}
75210070Seric 
75310090Seric 	return (wn);
7544632Seric }
7554632Seric /*
7567677Seric **  WORKCMPF -- compare function for ordering work.
7574632Seric **
7584632Seric **	Parameters:
7594632Seric **		a -- the first argument.
7604632Seric **		b -- the second argument.
7614632Seric **
7624632Seric **	Returns:
76324981Seric **		-1 if a < b
76424981Seric **		 0 if a == b
76524981Seric **		+1 if a > b
7664632Seric **
7674632Seric **	Side Effects:
7684632Seric **		none.
7694632Seric */
7704632Seric 
7714632Seric workcmpf(a, b)
7725037Seric 	register WORK *a;
7735037Seric 	register WORK *b;
7744632Seric {
77557438Seric 	long pa = a->w_pri;
77657438Seric 	long pb = b->w_pri;
77724941Seric 
77824941Seric 	if (pa == pb)
7794632Seric 		return (0);
78024941Seric 	else if (pa > pb)
78124981Seric 		return (1);
78224981Seric 	else
78310121Seric 		return (-1);
7844632Seric }
7854632Seric /*
7864632Seric **  DOWORK -- do a work request.
7874632Seric **
7884632Seric **	Parameters:
78958884Seric **		id -- the ID of the job to run.
79058884Seric **		forkflag -- if set, run this in background.
79158924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
79258924Seric **			This is used when expanding aliases in the queue.
79361094Seric **			If forkflag is also set, it doesn't wait for the
79461094Seric **			child.
79558884Seric **		e - the envelope in which to run it.
7964632Seric **
7974632Seric **	Returns:
79864296Seric **		process id of process that is running the queue job.
7994632Seric **
8004632Seric **	Side Effects:
8014632Seric **		The work request is satisfied if possible.
8024632Seric */
8034632Seric 
80464296Seric pid_t
80558924Seric dowork(id, forkflag, requeueflag, e)
80658884Seric 	char *id;
80758884Seric 	bool forkflag;
80858924Seric 	bool requeueflag;
80955012Seric 	register ENVELOPE *e;
8104632Seric {
81164296Seric 	register pid_t pid;
81251920Seric 	extern bool readqf();
8134632Seric 
8147677Seric 	if (tTd(40, 1))
81558884Seric 		printf("dowork(%s)\n", id);
8164632Seric 
8174632Seric 	/*
81824941Seric 	**  Fork for work.
81924941Seric 	*/
82024941Seric 
82158884Seric 	if (forkflag)
82224941Seric 	{
82364296Seric 		pid = fork();
82464296Seric 		if (pid < 0)
82524941Seric 		{
82624941Seric 			syserr("dowork: cannot fork");
82764296Seric 			return 0;
82824941Seric 		}
82924941Seric 	}
83024941Seric 	else
83124941Seric 	{
83264296Seric 		pid = 0;
83324941Seric 	}
83424941Seric 
83564296Seric 	if (pid == 0)
8364632Seric 	{
8374632Seric 		/*
8384632Seric 		**  CHILD
8398148Seric 		**	Lock the control file to avoid duplicate deliveries.
8408148Seric 		**		Then run the file as though we had just read it.
8417350Seric 		**	We save an idea of the temporary name so we
8427350Seric 		**		can recover on interrupt.
8434632Seric 		*/
8444632Seric 
8457763Seric 		/* set basic modes, etc. */
8467356Seric 		(void) alarm(0);
84755012Seric 		clearenvelope(e, FALSE);
84864554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
84958734Seric 		e->e_errormode = EM_MAIL;
85058884Seric 		e->e_id = id;
85164736Seric 		GrabTo = FALSE;
85263846Seric 		if (forkflag)
85364554Seric 		{
85464296Seric 			disconnect(1, e);
85564554Seric 			OpMode = MD_DELIVER;
85664554Seric 		}
8577876Seric # ifdef LOG
85858020Seric 		if (LogLevel > 76)
85955012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8607881Seric 			       getpid());
86156795Seric # endif /* LOG */
8627763Seric 
8637763Seric 		/* don't use the headers from sendmail.cf... */
86455012Seric 		e->e_header = NULL;
8657763Seric 
86651920Seric 		/* read the queue control file -- return if locked */
86763850Seric 		if (!readqf(e, !requeueflag))
8686980Seric 		{
86958884Seric 			if (tTd(40, 4))
87058884Seric 				printf("readqf(%s) failed\n", e->e_id);
87158884Seric 			if (forkflag)
87224941Seric 				exit(EX_OK);
87324941Seric 			else
87424941Seric 				return;
8756980Seric 		}
8766980Seric 
87755012Seric 		e->e_flags |= EF_INQUEUE;
87858929Seric 		eatheader(e, requeueflag);
8796980Seric 
88058924Seric 		if (requeueflag)
88158924Seric 			queueup(e, TRUE, FALSE);
88258924Seric 
8836980Seric 		/* do the delivery */
88458915Seric 		sendall(e, SM_DELIVER);
8856980Seric 
8866980Seric 		/* finish up and exit */
88758884Seric 		if (forkflag)
88824941Seric 			finis();
88924941Seric 		else
89055012Seric 			dropenvelope(e);
8914632Seric 	}
89264296Seric 	e->e_id = NULL;
89364296Seric 	return pid;
8944632Seric }
8954632Seric /*
8964632Seric **  READQF -- read queue file and set up environment.
8974632Seric **
8984632Seric **	Parameters:
8999377Seric **		e -- the envelope of the job to run.
90063850Seric **		announcefile -- if set, announce the name of the queue
90163850Seric **			file in error messages.
9024632Seric **
9034632Seric **	Returns:
90451920Seric **		TRUE if it successfully read the queue file.
90551920Seric **		FALSE otherwise.
9064632Seric **
9074632Seric **	Side Effects:
90851920Seric **		The queue file is returned locked.
9094632Seric */
9104632Seric 
91151920Seric bool
91263850Seric readqf(e, announcefile)
9139377Seric 	register ENVELOPE *e;
91463850Seric 	bool announcefile;
9154632Seric {
91617477Seric 	register FILE *qfp;
91754974Seric 	ADDRESS *ctladdr;
91856400Seric 	struct stat st;
91957135Seric 	char *bp;
92058915Seric 	char qf[20];
92157135Seric 	char buf[MAXLINE];
92224954Seric 	extern long atol();
92354974Seric 	extern ADDRESS *setctluser();
9244632Seric 
9254632Seric 	/*
92617468Seric 	**  Read and process the file.
9274632Seric 	*/
9284632Seric 
92958915Seric 	strcpy(qf, queuename(e, 'q'));
93051937Seric 	qfp = fopen(qf, "r+");
93117477Seric 	if (qfp == NULL)
93217477Seric 	{
93358884Seric 		if (tTd(40, 8))
93458884Seric 			printf("readqf(%s): fopen failure (%s)\n",
93558884Seric 				qf, errstring(errno));
93640934Srick 		if (errno != ENOENT)
93740934Srick 			syserr("readqf: no control file %s", qf);
93851920Seric 		return FALSE;
93917477Seric 	}
94040934Srick 
94164335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
94264277Seric 	{
94364277Seric 		/* being processed by another queuer */
94464277Seric 		if (tTd(40, 8))
94564277Seric 			printf("readqf(%s): locked\n", qf);
94664277Seric 		if (Verbose)
94764277Seric 			printf("%s: locked\n", e->e_id);
94864277Seric # ifdef LOG
94964277Seric 		if (LogLevel > 19)
95064277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
95164277Seric # endif /* LOG */
95264277Seric 		(void) fclose(qfp);
95364277Seric 		return FALSE;
95464277Seric 	}
95564277Seric 
95656400Seric 	/*
95756400Seric 	**  Check the queue file for plausibility to avoid attacks.
95856400Seric 	*/
95956400Seric 
96056400Seric 	if (fstat(fileno(qfp), &st) < 0)
96156400Seric 	{
96256400Seric 		/* must have been being processed by someone else */
96358884Seric 		if (tTd(40, 8))
96458884Seric 			printf("readqf(%s): fstat failure (%s)\n",
96558884Seric 				qf, errstring(errno));
96656400Seric 		fclose(qfp);
96756400Seric 		return FALSE;
96856400Seric 	}
96956400Seric 
97064137Seric 	if (st.st_uid != geteuid())
97156400Seric 	{
97256400Seric # ifdef LOG
97356400Seric 		if (LogLevel > 0)
97456400Seric 		{
97556400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
97656400Seric 				e->e_id, st.st_uid, st.st_mode);
97756400Seric 		}
97856795Seric # endif /* LOG */
97958884Seric 		if (tTd(40, 8))
98058884Seric 			printf("readqf(%s): bogus file\n", qf);
98164277Seric 		rename(qf, queuename(e, 'Q'));
98256400Seric 		fclose(qfp);
98356400Seric 		return FALSE;
98456400Seric 	}
98556400Seric 
98664277Seric 	if (st.st_size == 0)
98740934Srick 	{
98864277Seric 		/* must be a bogus file -- just remove it */
98964277Seric 		(void) unlink(qf);
99064277Seric 		fclose(qfp);
99151920Seric 		return FALSE;
99240934Srick 	}
99340934Srick 
99464277Seric 	if (st.st_nlink == 0)
99559101Seric 	{
99664277Seric 		/*
99764277Seric 		**  Race condition -- we got a file just as it was being
99864277Seric 		**  unlinked.  Just assume it is zero length.
99964277Seric 		*/
100064277Seric 
100159101Seric 		fclose(qfp);
100259101Seric 		return FALSE;
100359101Seric 	}
100459101Seric 
100564277Seric 	/* good file -- save this lock */
100651920Seric 	e->e_lockfp = qfp;
100751920Seric 
100840934Srick 	/* do basic system initialization */
100955012Seric 	initsys(e);
101040934Srick 
101163850Seric 	if (announcefile)
101263850Seric 		FileName = qf;
10139377Seric 	LineNumber = 0;
101463850Seric 	e->e_flags |= EF_GLOBALERRS;
101563850Seric 	OpMode = MD_DELIVER;
101651920Seric 	if (Verbose)
10179377Seric 		printf("\nRunning %s\n", e->e_id);
101854974Seric 	ctladdr = NULL;
101957135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
10204632Seric 	{
102158737Seric 		register char *p;
102257529Seric 		struct stat st;
102357529Seric 
102426504Seric 		if (tTd(40, 4))
102557135Seric 			printf("+++++ %s\n", bp);
102657135Seric 		switch (bp[0])
10274632Seric 		{
102840973Sbostic 		  case 'C':		/* specify controlling user */
102957135Seric 			ctladdr = setctluser(&bp[1]);
103040973Sbostic 			break;
103140973Sbostic 
10324632Seric 		  case 'R':		/* specify recipient */
103358082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10344632Seric 			break;
10354632Seric 
103625687Seric 		  case 'E':		/* specify error recipient */
103758082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
103825687Seric 			break;
103925687Seric 
10404632Seric 		  case 'H':		/* header */
104157135Seric 			(void) chompheader(&bp[1], FALSE, e);
10424632Seric 			break;
10434632Seric 
104410108Seric 		  case 'M':		/* message */
104564705Seric 			/* ignore this; we want a new message next time */
104610108Seric 			break;
104710108Seric 
10484632Seric 		  case 'S':		/* sender */
104958704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10504632Seric 			break;
10514632Seric 
105259093Seric 		  case 'B':		/* body type */
105359093Seric 			e->e_bodytype = newstr(&bp[1]);
105459093Seric 			break;
105559093Seric 
10564632Seric 		  case 'D':		/* data file name */
105757135Seric 			e->e_df = newstr(&bp[1]);
10589544Seric 			e->e_dfp = fopen(e->e_df, "r");
10599544Seric 			if (e->e_dfp == NULL)
106058020Seric 			{
10619377Seric 				syserr("readqf: cannot open %s", e->e_df);
106258020Seric 				e->e_msgsize = -1;
106358020Seric 			}
106458020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
106557529Seric 				e->e_msgsize = st.st_size;
10664632Seric 			break;
10674632Seric 
10687860Seric 		  case 'T':		/* init time */
106957135Seric 			e->e_ctime = atol(&bp[1]);
10704632Seric 			break;
10714632Seric 
10724634Seric 		  case 'P':		/* message priority */
107357135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10744634Seric 			break;
10754634Seric 
107658737Seric 		  case 'F':		/* flag bits */
107758737Seric 			for (p = &bp[1]; *p != '\0'; p++)
107858737Seric 			{
107958737Seric 				switch (*p)
108058737Seric 				{
108158737Seric 				  case 'w':	/* warning sent */
108258737Seric 					e->e_flags |= EF_WARNING;
108358737Seric 					break;
108458737Seric 
108558737Seric 				  case 'r':	/* response */
108658737Seric 					e->e_flags |= EF_RESPONSE;
108758737Seric 					break;
108858737Seric 				}
108958737Seric 			}
109058737Seric 			break;
109158737Seric 
109253400Seric 		  case '$':		/* define macro */
109357135Seric 			define(bp[1], newstr(&bp[2]), e);
109453400Seric 			break;
109553400Seric 
109624941Seric 		  case '\0':		/* blank line; ignore */
109724941Seric 			break;
109824941Seric 
10994632Seric 		  default:
110059700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
110157135Seric 				LineNumber, bp);
110263753Seric 			fclose(qfp);
110363753Seric 			rename(qf, queuename(e, 'Q'));
110463753Seric 			return FALSE;
11054632Seric 		}
110657135Seric 
110757135Seric 		if (bp != buf)
110857135Seric 			free(bp);
11094632Seric 	}
11109377Seric 
11119377Seric 	FileName = NULL;
111224941Seric 
111324941Seric 	/*
111424941Seric 	**  If we haven't read any lines, this queue file is empty.
111524941Seric 	**  Arrange to remove it without referencing any null pointers.
111624941Seric 	*/
111724941Seric 
111824941Seric 	if (LineNumber == 0)
111924941Seric 	{
112024941Seric 		errno = 0;
112124941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
112224941Seric 	}
112351920Seric 	return TRUE;
11244632Seric }
11254632Seric /*
11269630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11279630Seric **
11289630Seric **	Parameters:
11299630Seric **		none.
11309630Seric **
11319630Seric **	Returns:
11329630Seric **		none.
11339630Seric **
11349630Seric **	Side Effects:
11359630Seric **		Prints a listing of the mail queue on the standard output.
11369630Seric */
11375182Seric 
11389630Seric printqueue()
11399630Seric {
11409630Seric 	register WORK *w;
11419630Seric 	FILE *f;
114210070Seric 	int nrequests;
11439630Seric 	char buf[MAXLINE];
11449630Seric 
11459630Seric 	/*
114658250Seric 	**  Check for permission to print the queue
114758250Seric 	*/
114858250Seric 
114964333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
115058250Seric 	{
115158250Seric 		struct stat st;
115258523Seric # ifdef NGROUPS
115358523Seric 		int n;
115463937Seric 		GIDSET_T gidset[NGROUPS];
115558523Seric # endif
115658250Seric 
115758517Seric 		if (stat(QueueDir, &st) < 0)
115858250Seric 		{
115958250Seric 			syserr("Cannot stat %s", QueueDir);
116058250Seric 			return;
116158250Seric 		}
116258523Seric # ifdef NGROUPS
116358523Seric 		n = getgroups(NGROUPS, gidset);
116458523Seric 		while (--n >= 0)
116558523Seric 		{
116658523Seric 			if (gidset[n] == st.st_gid)
116758523Seric 				break;
116858523Seric 		}
116958523Seric 		if (n < 0)
117058523Seric # else
117163787Seric 		if (RealGid != st.st_gid)
117258523Seric # endif
117358250Seric 		{
117458250Seric 			usrerr("510 You are not permitted to see the queue");
117558250Seric 			setstat(EX_NOPERM);
117658250Seric 			return;
117758250Seric 		}
117858250Seric 	}
117958250Seric 
118058250Seric 	/*
11819630Seric 	**  Read and order the queue.
11829630Seric 	*/
11839630Seric 
118424941Seric 	nrequests = orderq(TRUE);
11859630Seric 
11869630Seric 	/*
11879630Seric 	**  Print the work list that we have read.
11889630Seric 	*/
11899630Seric 
11909630Seric 	/* first see if there is anything */
119110070Seric 	if (nrequests <= 0)
11929630Seric 	{
119310070Seric 		printf("Mail queue is empty\n");
11949630Seric 		return;
11959630Seric 	}
11969630Seric 
119751920Seric 	CurrentLA = getla();	/* get load average */
119840934Srick 
119910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
120025687Seric 	if (nrequests > QUEUESIZE)
120125687Seric 		printf(", only %d printed", QUEUESIZE);
120224979Seric 	if (Verbose)
120358716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
120424979Seric 	else
120558716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
12069630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
12079630Seric 	{
12089630Seric 		struct stat st;
120910070Seric 		auto time_t submittime = 0;
121010070Seric 		long dfsize = -1;
121158737Seric 		int flags = 0;
121210108Seric 		char message[MAXLINE];
121359093Seric 		char bodytype[MAXNAME];
12149630Seric 
121564355Seric 		printf("%8s", w->w_name + 2);
121617468Seric 		f = fopen(w->w_name, "r");
121717468Seric 		if (f == NULL)
121817468Seric 		{
121964355Seric 			printf(" (job completed)\n");
122017468Seric 			errno = 0;
122117468Seric 			continue;
122217468Seric 		}
122364335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
122410070Seric 			printf("*");
122557438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
122624941Seric 			printf("X");
122710070Seric 		else
122810070Seric 			printf(" ");
122910070Seric 		errno = 0;
123017468Seric 
123159093Seric 		message[0] = bodytype[0] = '\0';
12329630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12339630Seric 		{
123453400Seric 			register int i;
123558737Seric 			register char *p;
123653400Seric 
12379630Seric 			fixcrlf(buf, TRUE);
12389630Seric 			switch (buf[0])
12399630Seric 			{
124010108Seric 			  case 'M':	/* error message */
124153400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
124258737Seric 					i = sizeof message - 1;
124353400Seric 				bcopy(&buf[1], message, i);
124453400Seric 				message[i] = '\0';
124510108Seric 				break;
124610108Seric 
124759093Seric 			  case 'B':	/* body type */
124859093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
124959093Seric 					i = sizeof bodytype - 1;
125059093Seric 				bcopy(&buf[1], bodytype, i);
125159093Seric 				bodytype[i] = '\0';
125259093Seric 				break;
125359093Seric 
12549630Seric 			  case 'S':	/* sender name */
125524979Seric 				if (Verbose)
125658737Seric 					printf("%8ld %10ld%c%.12s %.38s",
125758737Seric 					    dfsize,
125858737Seric 					    w->w_pri,
125958737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
126058737Seric 					    ctime(&submittime) + 4,
126124979Seric 					    &buf[1]);
126224979Seric 				else
126324979Seric 					printf("%8ld %.16s %.45s", dfsize,
126424979Seric 					    ctime(&submittime), &buf[1]);
126559093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
126659093Seric 				{
126759093Seric 					printf("\n    %10.10s", bodytype);
126859093Seric 					if (message[0] != '\0')
126959093Seric 						printf("   (%.60s)", message);
127059093Seric 				}
12719630Seric 				break;
127251920Seric 
127340973Sbostic 			  case 'C':	/* controlling user */
127454974Seric 				if (Verbose)
127558716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
127658716Seric 						&buf[1]);
127740973Sbostic 				break;
12789630Seric 
12799630Seric 			  case 'R':	/* recipient name */
128024979Seric 				if (Verbose)
128158716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
128224979Seric 				else
128358716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12849630Seric 				break;
12859630Seric 
12869630Seric 			  case 'T':	/* creation time */
128724941Seric 				submittime = atol(&buf[1]);
12889630Seric 				break;
128910070Seric 
129010070Seric 			  case 'D':	/* data file name */
129110070Seric 				if (stat(&buf[1], &st) >= 0)
129210070Seric 					dfsize = st.st_size;
129310070Seric 				break;
129458737Seric 
129558737Seric 			  case 'F':	/* flag bits */
129658737Seric 				for (p = &buf[1]; *p != '\0'; p++)
129758737Seric 				{
129858737Seric 					switch (*p)
129958737Seric 					{
130058737Seric 					  case 'w':
130158737Seric 						flags |= EF_WARNING;
130258737Seric 						break;
130358737Seric 					}
130458737Seric 				}
13059630Seric 			}
13069630Seric 		}
130710070Seric 		if (submittime == (time_t) 0)
130810070Seric 			printf(" (no control file)");
13099630Seric 		printf("\n");
131023098Seric 		(void) fclose(f);
13119630Seric 	}
13129630Seric }
13139630Seric 
131456795Seric # endif /* QUEUE */
131517468Seric /*
131617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
131717468Seric **
131817468Seric **	Assigns an id code if one does not already exist.
131917468Seric **	This code is very careful to avoid trashing existing files
132017468Seric **	under any circumstances.
132117468Seric **
132217468Seric **	Parameters:
132317468Seric **		e -- envelope to build it in/from.
132417468Seric **		type -- the file type, used as the first character
132517468Seric **			of the file name.
132617468Seric **
132717468Seric **	Returns:
132817468Seric **		a pointer to the new file name (in a static buffer).
132917468Seric **
133017468Seric **	Side Effects:
133151920Seric **		If no id code is already assigned, queuename will
133251920Seric **		assign an id code, create a qf file, and leave a
133351920Seric **		locked, open-for-write file pointer in the envelope.
133417468Seric */
133517468Seric 
133617468Seric char *
133717468Seric queuename(e, type)
133817468Seric 	register ENVELOPE *e;
133959700Seric 	int type;
134017468Seric {
134117468Seric 	static int pid = -1;
134258741Seric 	static char c0;
134358741Seric 	static char c1;
134458741Seric 	static char c2;
134558716Seric 	time_t now;
134658716Seric 	struct tm *tm;
134758689Seric 	static char buf[MAXNAME];
134817468Seric 
134917468Seric 	if (e->e_id == NULL)
135017468Seric 	{
135117468Seric 		char qf[20];
135217468Seric 
135317468Seric 		/* find a unique id */
135417468Seric 		if (pid != getpid())
135517468Seric 		{
135617468Seric 			/* new process -- start back at "AA" */
135717468Seric 			pid = getpid();
135858716Seric 			now = curtime();
135958716Seric 			tm = localtime(&now);
136058716Seric 			c0 = 'A' + tm->tm_hour;
136117468Seric 			c1 = 'A';
136217468Seric 			c2 = 'A' - 1;
136317468Seric 		}
136458716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
136517468Seric 
136617468Seric 		while (c1 < '~' || c2 < 'Z')
136717468Seric 		{
136817468Seric 			int i;
136917468Seric 
137017468Seric 			if (c2 >= 'Z')
137117468Seric 			{
137217468Seric 				c1++;
137317468Seric 				c2 = 'A' - 1;
137417468Seric 			}
137558716Seric 			qf[3] = c1;
137658716Seric 			qf[4] = ++c2;
137717468Seric 			if (tTd(7, 20))
137840934Srick 				printf("queuename: trying \"%s\"\n", qf);
137917468Seric 
138040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
138151920Seric 			if (i < 0)
138251920Seric 			{
138351920Seric 				if (errno == EEXIST)
138451920Seric 					continue;
138564705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
138664705Seric 					qf, QueueDir, geteuid());
138751920Seric 				exit(EX_UNAVAILABLE);
138851920Seric 			}
138964335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
139051920Seric 			{
139151920Seric 				e->e_lockfp = fdopen(i, "w");
139240934Srick 				break;
139317468Seric 			}
139451920Seric 
139551920Seric 			/* a reader got the file; abandon it and try again */
139651920Seric 			(void) close(i);
139717468Seric 		}
139817468Seric 		if (c1 >= '~' && c2 >= 'Z')
139917468Seric 		{
140064705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
140164705Seric 				qf, QueueDir, geteuid());
140217468Seric 			exit(EX_OSERR);
140317468Seric 		}
140417468Seric 		e->e_id = newstr(&qf[2]);
140517468Seric 		define('i', e->e_id, e);
140617468Seric 		if (tTd(7, 1))
140717468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
1408*64745Seric 		if (tTd(7, 9))
1409*64745Seric 		{
1410*64745Seric 			printf("  lockfd=");
1411*64745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
1412*64745Seric 		}
141317468Seric # ifdef LOG
141458020Seric 		if (LogLevel > 93)
141517468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
141656795Seric # endif /* LOG */
141717468Seric 	}
141817468Seric 
141917468Seric 	if (type == '\0')
142017468Seric 		return (NULL);
142117468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
142217468Seric 	if (tTd(7, 2))
142317468Seric 		printf("queuename: %s\n", buf);
142417468Seric 	return (buf);
142517468Seric }
142617468Seric /*
142717468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
142817468Seric **
142917468Seric **	Parameters:
143017468Seric **		e -- the envelope to unlock.
143117468Seric **
143217468Seric **	Returns:
143317468Seric **		none
143417468Seric **
143517468Seric **	Side Effects:
143617468Seric **		unlocks the queue for `e'.
143717468Seric */
143817468Seric 
143917468Seric unlockqueue(e)
144017468Seric 	ENVELOPE *e;
144117468Seric {
144258680Seric 	if (tTd(51, 4))
144358680Seric 		printf("unlockqueue(%s)\n", e->e_id);
144458680Seric 
144551920Seric 	/* if there is a lock file in the envelope, close it */
144651920Seric 	if (e->e_lockfp != NULL)
144758680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
144851920Seric 	e->e_lockfp = NULL;
144951920Seric 
145058728Seric 	/* don't create a queue id if we don't already have one */
145158728Seric 	if (e->e_id == NULL)
145258728Seric 		return;
145358728Seric 
145417468Seric 	/* remove the transcript */
145517468Seric # ifdef LOG
145658020Seric 	if (LogLevel > 87)
145717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
145856795Seric # endif /* LOG */
145958680Seric 	if (!tTd(51, 104))
146017468Seric 		xunlink(queuename(e, 'x'));
146117468Seric 
146217468Seric }
146340973Sbostic /*
146454974Seric **  SETCTLUSER -- create a controlling address
146540973Sbostic **
146654974Seric **	Create a fake "address" given only a local login name; this is
146754974Seric **	used as a "controlling user" for future recipient addresses.
146840973Sbostic **
146940973Sbostic **	Parameters:
147054974Seric **		user -- the user name of the controlling user.
147140973Sbostic **
147240973Sbostic **	Returns:
147354974Seric **		An address descriptor for the controlling user.
147440973Sbostic **
147540973Sbostic **	Side Effects:
147640973Sbostic **		none.
147740973Sbostic */
147840973Sbostic 
147954974Seric ADDRESS *
148054974Seric setctluser(user)
148154974Seric 	char *user;
148240973Sbostic {
148354974Seric 	register ADDRESS *a;
148440973Sbostic 	struct passwd *pw;
148559113Seric 	char *p;
148640973Sbostic 
148740973Sbostic 	/*
148854974Seric 	**  See if this clears our concept of controlling user.
148940973Sbostic 	*/
149040973Sbostic 
149163850Seric 	if (user == NULL || *user == '\0')
149263850Seric 		return NULL;
149340973Sbostic 
149440973Sbostic 	/*
149554974Seric 	**  Set up addr fields for controlling user.
149640973Sbostic 	*/
149740973Sbostic 
149854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
149954974Seric 	bzero((char *) a, sizeof *a);
150059113Seric 
150159113Seric 	p = strchr(user, ':');
150259113Seric 	if (p != NULL)
150359113Seric 		*p++ = '\0';
150459270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
150540973Sbostic 	{
150640973Sbostic 		a->q_home = newstr(pw->pw_dir);
150740973Sbostic 		a->q_uid = pw->pw_uid;
150840973Sbostic 		a->q_gid = pw->pw_gid;
150957642Seric 		a->q_user = newstr(user);
151059270Seric 		a->q_flags |= QGOODUID;
151140973Sbostic 	}
151240973Sbostic 	else
151340973Sbostic 	{
151457642Seric 		a->q_user = newstr(DefUser);
151540973Sbostic 	}
151640973Sbostic 
151759270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
151856328Seric 	a->q_mailer = LocalMailer;
151959113Seric 	if (p == NULL)
152059113Seric 		a->q_paddr = a->q_user;
152159113Seric 	else
152259113Seric 		a->q_paddr = newstr(p);
152354974Seric 	return a;
152440973Sbostic }
1525