xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64137)
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*64137Seric static char sccsid[] = "@(#)queue.c	8.10 (Berkeley) 08/07/93 (with queueing)";
1433731Sbostic #else
15*64137Seric static char sccsid[] = "@(#)queue.c	8.10 (Berkeley) 08/07/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7151920Seric 	char buf[MAXLINE], tf[MAXLINE];
724632Seric 
735037Seric 	/*
7417477Seric 	**  Create control file.
755037Seric 	*/
764632Seric 
7751920Seric 	newid = (e->e_id == NULL);
7851920Seric 	strcpy(tf, queuename(e, 't'));
7951920Seric 	tfp = e->e_lockfp;
8051920Seric 	if (tfp == NULL)
8151920Seric 		newid = FALSE;
8251920Seric 	if (newid)
8351835Seric 	{
8451920Seric 		tfp = e->e_lockfp;
8551920Seric 	}
8651920Seric 	else
8751920Seric 	{
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 
10458689Seric 			if (lockfile(fd, tf, 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 		}
12258801Seric 		if (fd < 0)
12364070Seric 			syserr("!queueup: cannot create temp file %s", tf);
12441636Srick 
12551920Seric 		tfp = fdopen(fd, "w");
12651920Seric 	}
1274632Seric 
1287677Seric 	if (tTd(40, 1))
12917468Seric 		printf("queueing %s\n", e->e_id);
1304632Seric 
1314632Seric 	/*
1326980Seric 	**  If there is no data file yet, create one.
1336980Seric 	*/
1346980Seric 
1356980Seric 	if (e->e_df == NULL)
1366980Seric 	{
1376980Seric 		register FILE *dfp;
1389389Seric 		extern putbody();
1396980Seric 
14064086Seric 		e->e_df = queuename(e, 'd');
14164086Seric 		e->e_df = newstr(e->e_df);
14240934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14340934Srick 		if (fd < 0)
14458690Seric 			syserr("!queueup: cannot create %s", e->e_df);
14540934Srick 		dfp = fdopen(fd, "w");
14659730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
14758680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1489389Seric 		e->e_putbody = putbody;
1496980Seric 	}
1506980Seric 
1516980Seric 	/*
1524632Seric 	**  Output future work requests.
15325687Seric 	**	Priority and creation time should be first, since
15425687Seric 	**	they are required by orderq.
1554632Seric 	*/
1564632Seric 
1579377Seric 	/* output message priority */
1589377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1599377Seric 
1609630Seric 	/* output creation time */
1619630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1629630Seric 
16359093Seric 	/* output type and name of data file */
16459093Seric 	if (e->e_bodytype != NULL)
16559093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1667812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1674632Seric 
16810108Seric 	/* message from envelope, if it exists */
16910108Seric 	if (e->e_message != NULL)
17010108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17110108Seric 
17258737Seric 	/* send various flag bits through */
17358737Seric 	p = buf;
17458737Seric 	if (bitset(EF_WARNING, e->e_flags))
17558737Seric 		*p++ = 'w';
17658737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
17758737Seric 		*p++ = 'r';
17858737Seric 	*p++ = '\0';
17958737Seric 	if (buf[0] != '\0')
18058737Seric 		fprintf(tfp, "F%s\n", buf);
18158737Seric 
18258957Seric 	/* $r and $s and $_ macro values */
18353400Seric 	if ((p = macvalue('r', e)) != NULL)
18453400Seric 		fprintf(tfp, "$r%s\n", p);
18553400Seric 	if ((p = macvalue('s', e)) != NULL)
18653400Seric 		fprintf(tfp, "$s%s\n", p);
18758957Seric 	if ((p = macvalue('_', e)) != NULL)
18858957Seric 		fprintf(tfp, "$_%s\n", p);
18953400Seric 
1904632Seric 	/* output name of sender */
1917812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1924632Seric 
19355360Seric 	/* output list of error recipients */
19459670Seric 	printctladdr(NULL, NULL);
19555360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
19655360Seric 	{
19758680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
19855360Seric 		{
19959113Seric 			printctladdr(q, tfp);
20055360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
20155360Seric 		}
20255360Seric 	}
20355360Seric 
2044632Seric 	/* output list of recipient addresses */
2056980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2064632Seric 	{
20758250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
20858680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2098245Seric 		{
21059113Seric 			printctladdr(q, tfp);
2117812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2129377Seric 			if (announce)
2139377Seric 			{
2149377Seric 				e->e_to = q->q_paddr;
21558151Seric 				message("queued");
21658020Seric 				if (LogLevel > 8)
21758337Seric 					logdelivery(NULL, NULL, "queued", e);
2189377Seric 				e->e_to = NULL;
2199377Seric 			}
2209387Seric 			if (tTd(40, 1))
2219387Seric 			{
2229387Seric 				printf("queueing ");
2239387Seric 				printaddr(q, FALSE);
2249387Seric 			}
2258245Seric 		}
2264632Seric 	}
2274632Seric 
2289377Seric 	/*
2299377Seric 	**  Output headers for this message.
2309377Seric 	**	Expand macros completely here.  Queue run will deal with
2319377Seric 	**	everything as absolute headers.
2329377Seric 	**		All headers that must be relative to the recipient
2339377Seric 	**		can be cracked later.
23410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23510173Seric 	**	no effect on the addresses as they are output.
2369377Seric 	*/
2379377Seric 
23810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
23958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24058020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24110349Seric 	nullmailer.m_eol = "\n";
24210173Seric 
24358050Seric 	define('g', "\201f", e);
2446980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2454632Seric 	{
24610686Seric 		extern bool bitzerop();
24710686Seric 
24812015Seric 		/* don't output null headers */
2494632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2504632Seric 			continue;
25112015Seric 
25212015Seric 		/* don't output resent headers on non-resent messages */
25312015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25412015Seric 			continue;
25512015Seric 
25612015Seric 		/* output this header */
2577812Seric 		fprintf(tfp, "H");
25812015Seric 
25912015Seric 		/* if conditional, output the set of conditions */
26010686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26110686Seric 		{
26210686Seric 			int j;
26310686Seric 
26423098Seric 			(void) putc('?', tfp);
26510686Seric 			for (j = '\0'; j <= '\177'; j++)
26610686Seric 				if (bitnset(j, h->h_mflags))
26723098Seric 					(void) putc(j, tfp);
26823098Seric 			(void) putc('?', tfp);
26910686Seric 		}
27012015Seric 
27112015Seric 		/* output the header: expand macros, convert addresses */
2727763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2737763Seric 		{
2747763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2758236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2767763Seric 		}
2778245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2789348Seric 		{
27958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28063753Seric 			FILE *savetrace = TrafficLogFile;
28158737Seric 
28263753Seric 			TrafficLogFile = NULL;
28363753Seric 
28458737Seric 			if (bitset(H_FROM, h->h_flags))
28558737Seric 				oldstyle = FALSE;
28658737Seric 
28758737Seric 			commaize(h, h->h_value, tfp, oldstyle,
28855012Seric 				 &nullmailer, e);
28963753Seric 
29063753Seric 			TrafficLogFile = savetrace;
2919348Seric 		}
2927763Seric 		else
2938245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2944632Seric 	}
2954632Seric 
2964632Seric 	/*
2974632Seric 	**  Clean up.
2984632Seric 	*/
2994632Seric 
30058732Seric 	fflush(tfp);
30160603Seric 	fsync(fileno(tfp));
30258732Seric 	if (ferror(tfp))
30358732Seric 	{
30458732Seric 		if (newid)
30558732Seric 			syserr("!552 Error writing control file %s", tf);
30658732Seric 		else
30758732Seric 			syserr("!452 Error writing control file %s", tf);
30858732Seric 	}
30958732Seric 
31051920Seric 	if (!newid)
31151920Seric 	{
31251920Seric 		qf = queuename(e, 'q');
31351920Seric 		if (rename(tf, qf) < 0)
31451920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31551920Seric 		if (e->e_lockfp != NULL)
31658680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
31751920Seric 		e->e_lockfp = tfp;
31851920Seric 	}
31951920Seric 	else
32051920Seric 		qf = tf;
32141636Srick 	errno = 0;
3227391Seric 
3237677Seric # ifdef LOG
3247677Seric 	/* save log info */
32558020Seric 	if (LogLevel > 79)
3267878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
32756795Seric # endif /* LOG */
32851920Seric 	return;
3294632Seric }
33054974Seric 
33154974Seric printctladdr(a, tfp)
33259113Seric 	register ADDRESS *a;
33354974Seric 	FILE *tfp;
33454974Seric {
33559113Seric 	char *uname;
33659113Seric 	register struct passwd *pw;
33759113Seric 	register ADDRESS *q;
33859113Seric 	uid_t uid;
33959113Seric 	static ADDRESS *lastctladdr;
34059113Seric 	static uid_t lastuid;
34154974Seric 
34259113Seric 	/* initialization */
34363850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
34454974Seric 	{
34559670Seric 		if (lastctladdr != NULL && tfp != NULL)
34659113Seric 			fprintf(tfp, "C\n");
34759113Seric 		lastctladdr = NULL;
34859113Seric 		lastuid = 0;
34954974Seric 		return;
35054974Seric 	}
35159113Seric 
35259113Seric 	/* find the active uid */
35359113Seric 	q = getctladdr(a);
35459113Seric 	if (q == NULL)
35559113Seric 		uid = 0;
35654974Seric 	else
35759113Seric 		uid = q->q_uid;
35863850Seric 	a = a->q_alias;
35959113Seric 
36059113Seric 	/* check to see if this is the same as last time */
36159113Seric 	if (lastctladdr != NULL && uid == lastuid &&
36259113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
36359113Seric 		return;
36459113Seric 	lastuid = uid;
36559113Seric 	lastctladdr = a;
36659113Seric 
36759113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
36859270Seric 		uname = "";
36959113Seric 	else
37059113Seric 		uname = pw->pw_name;
37159113Seric 
37259113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
37354974Seric }
37454974Seric 
3754632Seric /*
3764632Seric **  RUNQUEUE -- run the jobs in the queue.
3774632Seric **
3784632Seric **	Gets the stuff out of the queue in some presumably logical
3794632Seric **	order and processes them.
3804632Seric **
3814632Seric **	Parameters:
38224941Seric **		forkflag -- TRUE if the queue scanning should be done in
38324941Seric **			a child process.  We double-fork so it is not our
38424941Seric **			child and we don't have to clean up after it.
3854632Seric **
3864632Seric **	Returns:
3874632Seric **		none.
3884632Seric **
3894632Seric **	Side Effects:
3904632Seric **		runs things in the mail queue.
3914632Seric */
3924632Seric 
39355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
39455360Seric 
39555360Seric runqueue(forkflag)
3964639Seric 	bool forkflag;
3974632Seric {
39855360Seric 	register ENVELOPE *e;
39955360Seric 	extern ENVELOPE BlankEnvelope;
40024953Seric 
4017466Seric 	/*
40224953Seric 	**  If no work will ever be selected, don't even bother reading
40324953Seric 	**  the queue.
40424953Seric 	*/
40524953Seric 
40651920Seric 	CurrentLA = getla();	/* get load average */
40740934Srick 
40858132Seric 	if (shouldqueue(0L, curtime()))
40924953Seric 	{
41024953Seric 		if (Verbose)
41124953Seric 			printf("Skipping queue run -- load average too high\n");
41258107Seric 		if (forkflag && QueueIntvl != 0)
41358107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
41455360Seric 		return;
41524953Seric 	}
41624953Seric 
41724953Seric 	/*
4187466Seric 	**  See if we want to go off and do other useful work.
4197466Seric 	*/
4204639Seric 
4214639Seric 	if (forkflag)
4224639Seric 	{
4237943Seric 		int pid;
4247943Seric 
4257943Seric 		pid = dofork();
4267943Seric 		if (pid != 0)
4274639Seric 		{
42846928Sbostic 			extern void reapchild();
42925184Seric 
4307943Seric 			/* parent -- pick up intermediate zombie */
43125184Seric #ifndef SIGCHLD
4329377Seric 			(void) waitfor(pid);
43356795Seric #else /* SIGCHLD */
43464035Seric 			(void) setsignal(SIGCHLD, reapchild);
43556795Seric #endif /* SIGCHLD */
4367690Seric 			if (QueueIntvl != 0)
4379348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4384639Seric 			return;
4394639Seric 		}
4407943Seric 		/* child -- double fork */
44125184Seric #ifndef SIGCHLD
4427943Seric 		if (fork() != 0)
4437943Seric 			exit(EX_OK);
44456795Seric #else /* SIGCHLD */
44564035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
44656795Seric #endif /* SIGCHLD */
4474639Seric 	}
44824941Seric 
44940934Srick 	setproctitle("running queue: %s", QueueDir);
45024941Seric 
4517876Seric # ifdef LOG
45258020Seric 	if (LogLevel > 69)
45355360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
45455360Seric 			QueueDir, getpid(), forkflag);
45556795Seric # endif /* LOG */
4564639Seric 
4577466Seric 	/*
45810205Seric 	**  Release any resources used by the daemon code.
45910205Seric 	*/
46010205Seric 
46110205Seric # ifdef DAEMON
46210205Seric 	clrdaemon();
46356795Seric # endif /* DAEMON */
46410205Seric 
46510205Seric 	/*
46655360Seric 	**  Create ourselves an envelope
46755360Seric 	*/
46855360Seric 
46955360Seric 	CurEnv = &QueueEnvelope;
47058179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
47155360Seric 	e->e_flags = BlankEnvelope.e_flags;
47255360Seric 
47355360Seric 	/*
47427175Seric 	**  Make sure the alias database is open.
47527175Seric 	*/
47627175Seric 
47760537Seric 	initmaps(FALSE, e);
47827175Seric 
47927175Seric 	/*
4807466Seric 	**  Start making passes through the queue.
4817466Seric 	**	First, read and sort the entire queue.
4827466Seric 	**	Then, process the work in that order.
4837466Seric 	**		But if you take too long, start over.
4847466Seric 	*/
4857466Seric 
4867943Seric 	/* order the existing work requests */
48724954Seric 	(void) orderq(FALSE);
4887690Seric 
4897943Seric 	/* process them once at a time */
4907943Seric 	while (WorkQ != NULL)
4914639Seric 	{
4927943Seric 		WORK *w = WorkQ;
4937881Seric 
4947943Seric 		WorkQ = WorkQ->w_next;
49558884Seric 
49658884Seric 		/*
49758884Seric 		**  Ignore jobs that are too expensive for the moment.
49858884Seric 		*/
49958884Seric 
50058884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
50158884Seric 		{
50258884Seric 			if (Verbose)
50358884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
50458884Seric 		}
50558930Seric 		else
50658930Seric 		{
50758930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
50858930Seric 		}
5097943Seric 		free(w->w_name);
5107943Seric 		free((char *) w);
5114639Seric 	}
51229866Seric 
51329866Seric 	/* exit without the usual cleanup */
51455467Seric 	e->e_id = NULL;
51555467Seric 	finis();
5164634Seric }
5174634Seric /*
5184632Seric **  ORDERQ -- order the work queue.
5194632Seric **
5204632Seric **	Parameters:
52124941Seric **		doall -- if set, include everything in the queue (even
52224941Seric **			the jobs that cannot be run because the load
52324941Seric **			average is too high).  Otherwise, exclude those
52424941Seric **			jobs.
5254632Seric **
5264632Seric **	Returns:
52710121Seric **		The number of request in the queue (not necessarily
52810121Seric **		the number of requests in WorkQ however).
5294632Seric **
5304632Seric **	Side Effects:
5314632Seric **		Sets WorkQ to the queue of available work, in order.
5324632Seric */
5334632Seric 
53425687Seric # define NEED_P		001
53525687Seric # define NEED_T		002
53658318Seric # define NEED_R		004
53758318Seric # define NEED_S		010
5384632Seric 
53924941Seric orderq(doall)
54024941Seric 	bool doall;
5414632Seric {
54260219Seric 	register struct dirent *d;
5434632Seric 	register WORK *w;
5446625Sglickman 	DIR *f;
5454632Seric 	register int i;
54625687Seric 	WORK wlist[QUEUESIZE+1];
54710070Seric 	int wn = -1;
5484632Seric 	extern workcmpf();
5494632Seric 
55058318Seric 	if (tTd(41, 1))
55158318Seric 	{
55258318Seric 		printf("orderq:\n");
55358318Seric 		if (QueueLimitId != NULL)
55458318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
55558318Seric 		if (QueueLimitSender != NULL)
55658318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
55758318Seric 		if (QueueLimitRecipient != NULL)
55858318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
55958318Seric 	}
56058318Seric 
5614632Seric 	/* clear out old WorkQ */
5624632Seric 	for (w = WorkQ; w != NULL; )
5634632Seric 	{
5644632Seric 		register WORK *nw = w->w_next;
5654632Seric 
5664632Seric 		WorkQ = nw;
5674632Seric 		free(w->w_name);
5684632Seric 		free((char *) w);
5694632Seric 		w = nw;
5704632Seric 	}
5714632Seric 
5724632Seric 	/* open the queue directory */
5738148Seric 	f = opendir(".");
5744632Seric 	if (f == NULL)
5754632Seric 	{
5768148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
57710070Seric 		return (0);
5784632Seric 	}
5794632Seric 
5804632Seric 	/*
5814632Seric 	**  Read the work directory.
5824632Seric 	*/
5834632Seric 
58410070Seric 	while ((d = readdir(f)) != NULL)
5854632Seric 	{
5869377Seric 		FILE *cf;
5874632Seric 		char lbuf[MAXNAME];
58858318Seric 		extern bool strcontainedin();
5894632Seric 
5904632Seric 		/* is this an interesting entry? */
5917812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5924632Seric 			continue;
5934632Seric 
59458318Seric 		if (QueueLimitId != NULL &&
59558318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
59658318Seric 			continue;
59758318Seric 
59858722Seric 		/*
59958722Seric 		**  Check queue name for plausibility.  This handles
60058722Seric 		**  both old and new type ids.
60158722Seric 		*/
60258722Seric 
60358722Seric 		i = strlen(d->d_name);
60458722Seric 		if (i != 9 && i != 10)
60558020Seric 		{
60658020Seric 			if (Verbose)
60758020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
60858020Seric #ifdef LOG
60958020Seric 			if (LogLevel > 3)
61059615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
61158020Seric 					d->d_name);
61258020Seric #endif
61358020Seric 			if (strlen(d->d_name) >= MAXNAME)
61458020Seric 				d->d_name[MAXNAME - 1] = '\0';
61558020Seric 			strcpy(lbuf, d->d_name);
61658020Seric 			lbuf[0] = 'Q';
61758020Seric 			(void) rename(d->d_name, lbuf);
61858020Seric 			continue;
61958020Seric 		}
62058020Seric 
62110070Seric 		/* yes -- open control file (if not too many files) */
62225687Seric 		if (++wn >= QUEUESIZE)
62310070Seric 			continue;
62458318Seric 
6258148Seric 		cf = fopen(d->d_name, "r");
6264632Seric 		if (cf == NULL)
6274632Seric 		{
6287055Seric 			/* this may be some random person sending hir msgs */
6297055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
63010090Seric 			if (tTd(41, 2))
63110090Seric 				printf("orderq: cannot open %s (%d)\n",
63210090Seric 					d->d_name, errno);
6337055Seric 			errno = 0;
63410090Seric 			wn--;
6354632Seric 			continue;
6364632Seric 		}
63725687Seric 		w = &wlist[wn];
63825687Seric 		w->w_name = newstr(d->d_name);
6394632Seric 
64025027Seric 		/* make sure jobs in creation don't clog queue */
64125687Seric 		w->w_pri = 0x7fffffff;
64225687Seric 		w->w_ctime = 0;
64325027Seric 
6444632Seric 		/* extract useful information */
64525687Seric 		i = NEED_P | NEED_T;
64658318Seric 		if (QueueLimitSender != NULL)
64758318Seric 			i |= NEED_S;
64858318Seric 		if (QueueLimitRecipient != NULL)
64958318Seric 			i |= NEED_R;
65025687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6514632Seric 		{
65224954Seric 			extern long atol();
65358318Seric 			extern bool strcontainedin();
65424954Seric 
65524941Seric 			switch (lbuf[0])
6564632Seric 			{
65724941Seric 			  case 'P':
65825687Seric 				w->w_pri = atol(&lbuf[1]);
65925687Seric 				i &= ~NEED_P;
6604632Seric 				break;
66125013Seric 
66225013Seric 			  case 'T':
66325687Seric 				w->w_ctime = atol(&lbuf[1]);
66425687Seric 				i &= ~NEED_T;
66525013Seric 				break;
66658318Seric 
66758318Seric 			  case 'R':
66858318Seric 				if (QueueLimitRecipient != NULL &&
66958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
67058318Seric 					i &= ~NEED_R;
67158318Seric 				break;
67258318Seric 
67358318Seric 			  case 'S':
67458318Seric 				if (QueueLimitSender != NULL &&
67558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
67658318Seric 					i &= ~NEED_S;
67758318Seric 				break;
6784632Seric 			}
6794632Seric 		}
6804632Seric 		(void) fclose(cf);
68124953Seric 
68258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
68358318Seric 		    bitset(NEED_R|NEED_S, i))
68424953Seric 		{
68524953Seric 			/* don't even bother sorting this job in */
68624953Seric 			wn--;
68724953Seric 		}
6884632Seric 	}
6896625Sglickman 	(void) closedir(f);
69010090Seric 	wn++;
6914632Seric 
6924632Seric 	/*
6934632Seric 	**  Sort the work directory.
6944632Seric 	*/
6954632Seric 
69625687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6974632Seric 
6984632Seric 	/*
6994632Seric 	**  Convert the work list into canonical form.
7009377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7014632Seric 	*/
7024632Seric 
70324981Seric 	WorkQ = NULL;
70425687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7054632Seric 	{
7064632Seric 		w = (WORK *) xalloc(sizeof *w);
7074632Seric 		w->w_name = wlist[i].w_name;
7084632Seric 		w->w_pri = wlist[i].w_pri;
70925013Seric 		w->w_ctime = wlist[i].w_ctime;
71024981Seric 		w->w_next = WorkQ;
71124981Seric 		WorkQ = w;
7124632Seric 	}
7134632Seric 
7147677Seric 	if (tTd(40, 1))
7154632Seric 	{
7164632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7175037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7184632Seric 	}
71910070Seric 
72010090Seric 	return (wn);
7214632Seric }
7224632Seric /*
7237677Seric **  WORKCMPF -- compare function for ordering work.
7244632Seric **
7254632Seric **	Parameters:
7264632Seric **		a -- the first argument.
7274632Seric **		b -- the second argument.
7284632Seric **
7294632Seric **	Returns:
73024981Seric **		-1 if a < b
73124981Seric **		 0 if a == b
73224981Seric **		+1 if a > b
7334632Seric **
7344632Seric **	Side Effects:
7354632Seric **		none.
7364632Seric */
7374632Seric 
7384632Seric workcmpf(a, b)
7395037Seric 	register WORK *a;
7405037Seric 	register WORK *b;
7414632Seric {
74257438Seric 	long pa = a->w_pri;
74357438Seric 	long pb = b->w_pri;
74424941Seric 
74524941Seric 	if (pa == pb)
7464632Seric 		return (0);
74724941Seric 	else if (pa > pb)
74824981Seric 		return (1);
74924981Seric 	else
75010121Seric 		return (-1);
7514632Seric }
7524632Seric /*
7534632Seric **  DOWORK -- do a work request.
7544632Seric **
7554632Seric **	Parameters:
75658884Seric **		id -- the ID of the job to run.
75758884Seric **		forkflag -- if set, run this in background.
75858924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
75958924Seric **			This is used when expanding aliases in the queue.
76061094Seric **			If forkflag is also set, it doesn't wait for the
76161094Seric **			child.
76258884Seric **		e - the envelope in which to run it.
7634632Seric **
7644632Seric **	Returns:
7654632Seric **		none.
7664632Seric **
7674632Seric **	Side Effects:
7684632Seric **		The work request is satisfied if possible.
7694632Seric */
7704632Seric 
77158924Seric dowork(id, forkflag, requeueflag, e)
77258884Seric 	char *id;
77358884Seric 	bool forkflag;
77458924Seric 	bool requeueflag;
77555012Seric 	register ENVELOPE *e;
7764632Seric {
7774632Seric 	register int i;
77851920Seric 	extern bool readqf();
7794632Seric 
7807677Seric 	if (tTd(40, 1))
78158884Seric 		printf("dowork(%s)\n", id);
7824632Seric 
7834632Seric 	/*
78424941Seric 	**  Fork for work.
78524941Seric 	*/
78624941Seric 
78758884Seric 	if (forkflag)
78824941Seric 	{
78924941Seric 		i = fork();
79024941Seric 		if (i < 0)
79124941Seric 		{
79224941Seric 			syserr("dowork: cannot fork");
79324941Seric 			return;
79424941Seric 		}
79524941Seric 	}
79624941Seric 	else
79724941Seric 	{
79824941Seric 		i = 0;
79924941Seric 	}
80024941Seric 
8014632Seric 	if (i == 0)
8024632Seric 	{
8034632Seric 		/*
8044632Seric 		**  CHILD
8058148Seric 		**	Lock the control file to avoid duplicate deliveries.
8068148Seric 		**		Then run the file as though we had just read it.
8077350Seric 		**	We save an idea of the temporary name so we
8087350Seric 		**		can recover on interrupt.
8094632Seric 		*/
8104632Seric 
8117763Seric 		/* set basic modes, etc. */
8127356Seric 		(void) alarm(0);
81355012Seric 		clearenvelope(e, FALSE);
81458737Seric 		e->e_flags |= EF_QUEUERUN;
81558734Seric 		e->e_errormode = EM_MAIL;
81658884Seric 		e->e_id = id;
81763846Seric 		if (forkflag)
81863846Seric 			disconnect(0, e);
8197876Seric # ifdef LOG
82058020Seric 		if (LogLevel > 76)
82155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8227881Seric 			       getpid());
82356795Seric # endif /* LOG */
8247763Seric 
8257763Seric 		/* don't use the headers from sendmail.cf... */
82655012Seric 		e->e_header = NULL;
8277763Seric 
82851920Seric 		/* read the queue control file -- return if locked */
82963850Seric 		if (!readqf(e, !requeueflag))
8306980Seric 		{
83158884Seric 			if (tTd(40, 4))
83258884Seric 				printf("readqf(%s) failed\n", e->e_id);
83358884Seric 			if (forkflag)
83424941Seric 				exit(EX_OK);
83524941Seric 			else
83624941Seric 				return;
8376980Seric 		}
8386980Seric 
83955012Seric 		e->e_flags |= EF_INQUEUE;
84058929Seric 		eatheader(e, requeueflag);
8416980Seric 
84258924Seric 		if (requeueflag)
84358924Seric 			queueup(e, TRUE, FALSE);
84458924Seric 
8456980Seric 		/* do the delivery */
84658915Seric 		sendall(e, SM_DELIVER);
8476980Seric 
8486980Seric 		/* finish up and exit */
84958884Seric 		if (forkflag)
85024941Seric 			finis();
85124941Seric 		else
85255012Seric 			dropenvelope(e);
8534632Seric 	}
85461094Seric 	else if (!requeueflag)
85524941Seric 	{
85624941Seric 		/*
85724941Seric 		**  Parent -- pick up results.
85824941Seric 		*/
8594632Seric 
86024941Seric 		errno = 0;
86124941Seric 		(void) waitfor(i);
86224941Seric 	}
8634632Seric }
8644632Seric /*
8654632Seric **  READQF -- read queue file and set up environment.
8664632Seric **
8674632Seric **	Parameters:
8689377Seric **		e -- the envelope of the job to run.
86963850Seric **		announcefile -- if set, announce the name of the queue
87063850Seric **			file in error messages.
8714632Seric **
8724632Seric **	Returns:
87351920Seric **		TRUE if it successfully read the queue file.
87451920Seric **		FALSE otherwise.
8754632Seric **
8764632Seric **	Side Effects:
87751920Seric **		The queue file is returned locked.
8784632Seric */
8794632Seric 
88051920Seric bool
88163850Seric readqf(e, announcefile)
8829377Seric 	register ENVELOPE *e;
88363850Seric 	bool announcefile;
8844632Seric {
88517477Seric 	register FILE *qfp;
88654974Seric 	ADDRESS *ctladdr;
88756400Seric 	struct stat st;
88857135Seric 	char *bp;
88958915Seric 	char qf[20];
89057135Seric 	char buf[MAXLINE];
89124954Seric 	extern long atol();
89254974Seric 	extern ADDRESS *setctluser();
8934632Seric 
8944632Seric 	/*
89517468Seric 	**  Read and process the file.
8964632Seric 	*/
8974632Seric 
89858915Seric 	strcpy(qf, queuename(e, 'q'));
89951937Seric 	qfp = fopen(qf, "r+");
90017477Seric 	if (qfp == NULL)
90117477Seric 	{
90258884Seric 		if (tTd(40, 8))
90358884Seric 			printf("readqf(%s): fopen failure (%s)\n",
90458884Seric 				qf, errstring(errno));
90540934Srick 		if (errno != ENOENT)
90640934Srick 			syserr("readqf: no control file %s", qf);
90751920Seric 		return FALSE;
90817477Seric 	}
90940934Srick 
91056400Seric 	/*
91156400Seric 	**  Check the queue file for plausibility to avoid attacks.
91256400Seric 	*/
91356400Seric 
91456400Seric 	if (fstat(fileno(qfp), &st) < 0)
91556400Seric 	{
91656400Seric 		/* must have been being processed by someone else */
91758884Seric 		if (tTd(40, 8))
91858884Seric 			printf("readqf(%s): fstat failure (%s)\n",
91958884Seric 				qf, errstring(errno));
92056400Seric 		fclose(qfp);
92156400Seric 		return FALSE;
92256400Seric 	}
92356400Seric 
924*64137Seric 	if (st.st_uid != geteuid())
92556400Seric 	{
92656400Seric # ifdef LOG
92756400Seric 		if (LogLevel > 0)
92856400Seric 		{
92956400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
93056400Seric 				e->e_id, st.st_uid, st.st_mode);
93156400Seric 		}
93256795Seric # endif /* LOG */
93358884Seric 		if (tTd(40, 8))
93458884Seric 			printf("readqf(%s): bogus file\n", qf);
93556400Seric 		fclose(qfp);
93663753Seric 		rename(qf, queuename(e, 'Q'));
93756400Seric 		return FALSE;
93856400Seric 	}
93956400Seric 
94058689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
94140934Srick 	{
94258689Seric 		/* being processed by another queuer */
94358884Seric 		if (tTd(40, 8))
94458884Seric 			printf("readqf(%s): locked\n", qf);
94558689Seric 		if (Verbose)
94658689Seric 			printf("%s: locked\n", e->e_id);
94751920Seric # ifdef LOG
94858689Seric 		if (LogLevel > 19)
94958689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
95056795Seric # endif /* LOG */
95140934Srick 		(void) fclose(qfp);
95251920Seric 		return FALSE;
95340934Srick 	}
95440934Srick 
95559101Seric 	if (st.st_size == 0)
95659101Seric 	{
95759101Seric 		/* must be a bogus file -- just remove it */
95859101Seric 		(void) unlink(qf);
95959101Seric 		fclose(qfp);
96059101Seric 		return FALSE;
96159101Seric 	}
96259101Seric 
96351920Seric 	/* save this lock */
96451920Seric 	e->e_lockfp = qfp;
96551920Seric 
96640934Srick 	/* do basic system initialization */
96755012Seric 	initsys(e);
96840934Srick 
96963850Seric 	if (announcefile)
97063850Seric 		FileName = qf;
9719377Seric 	LineNumber = 0;
97263850Seric 	e->e_flags |= EF_GLOBALERRS;
97363850Seric 	OpMode = MD_DELIVER;
97451920Seric 	if (Verbose)
9759377Seric 		printf("\nRunning %s\n", e->e_id);
97654974Seric 	ctladdr = NULL;
97757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9784632Seric 	{
97958737Seric 		register char *p;
98057529Seric 		struct stat st;
98157529Seric 
98226504Seric 		if (tTd(40, 4))
98357135Seric 			printf("+++++ %s\n", bp);
98457135Seric 		switch (bp[0])
9854632Seric 		{
98640973Sbostic 		  case 'C':		/* specify controlling user */
98757135Seric 			ctladdr = setctluser(&bp[1]);
98840973Sbostic 			break;
98940973Sbostic 
9904632Seric 		  case 'R':		/* specify recipient */
99158082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9924632Seric 			break;
9934632Seric 
99425687Seric 		  case 'E':		/* specify error recipient */
99558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
99625687Seric 			break;
99725687Seric 
9984632Seric 		  case 'H':		/* header */
99957135Seric 			(void) chompheader(&bp[1], FALSE, e);
10004632Seric 			break;
10014632Seric 
100210108Seric 		  case 'M':		/* message */
100357135Seric 			e->e_message = newstr(&bp[1]);
100410108Seric 			break;
100510108Seric 
10064632Seric 		  case 'S':		/* sender */
100758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10084632Seric 			break;
10094632Seric 
101059093Seric 		  case 'B':		/* body type */
101159093Seric 			e->e_bodytype = newstr(&bp[1]);
101259093Seric 			break;
101359093Seric 
10144632Seric 		  case 'D':		/* data file name */
101557135Seric 			e->e_df = newstr(&bp[1]);
10169544Seric 			e->e_dfp = fopen(e->e_df, "r");
10179544Seric 			if (e->e_dfp == NULL)
101858020Seric 			{
10199377Seric 				syserr("readqf: cannot open %s", e->e_df);
102058020Seric 				e->e_msgsize = -1;
102158020Seric 			}
102258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
102357529Seric 				e->e_msgsize = st.st_size;
10244632Seric 			break;
10254632Seric 
10267860Seric 		  case 'T':		/* init time */
102757135Seric 			e->e_ctime = atol(&bp[1]);
10284632Seric 			break;
10294632Seric 
10304634Seric 		  case 'P':		/* message priority */
103157135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10324634Seric 			break;
10334634Seric 
103458737Seric 		  case 'F':		/* flag bits */
103558737Seric 			for (p = &bp[1]; *p != '\0'; p++)
103658737Seric 			{
103758737Seric 				switch (*p)
103858737Seric 				{
103958737Seric 				  case 'w':	/* warning sent */
104058737Seric 					e->e_flags |= EF_WARNING;
104158737Seric 					break;
104258737Seric 
104358737Seric 				  case 'r':	/* response */
104458737Seric 					e->e_flags |= EF_RESPONSE;
104558737Seric 					break;
104658737Seric 				}
104758737Seric 			}
104858737Seric 			break;
104958737Seric 
105053400Seric 		  case '$':		/* define macro */
105157135Seric 			define(bp[1], newstr(&bp[2]), e);
105253400Seric 			break;
105353400Seric 
105424941Seric 		  case '\0':		/* blank line; ignore */
105524941Seric 			break;
105624941Seric 
10574632Seric 		  default:
105859700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
105957135Seric 				LineNumber, bp);
106063753Seric 			fclose(qfp);
106163753Seric 			rename(qf, queuename(e, 'Q'));
106263753Seric 			return FALSE;
10634632Seric 		}
106457135Seric 
106557135Seric 		if (bp != buf)
106657135Seric 			free(bp);
10674632Seric 	}
10689377Seric 
10699377Seric 	FileName = NULL;
107024941Seric 
107124941Seric 	/*
107224941Seric 	**  If we haven't read any lines, this queue file is empty.
107324941Seric 	**  Arrange to remove it without referencing any null pointers.
107424941Seric 	*/
107524941Seric 
107624941Seric 	if (LineNumber == 0)
107724941Seric 	{
107824941Seric 		errno = 0;
107924941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
108024941Seric 	}
108151920Seric 	return TRUE;
10824632Seric }
10834632Seric /*
10849630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10859630Seric **
10869630Seric **	Parameters:
10879630Seric **		none.
10889630Seric **
10899630Seric **	Returns:
10909630Seric **		none.
10919630Seric **
10929630Seric **	Side Effects:
10939630Seric **		Prints a listing of the mail queue on the standard output.
10949630Seric */
10955182Seric 
10969630Seric printqueue()
10979630Seric {
10989630Seric 	register WORK *w;
10999630Seric 	FILE *f;
110010070Seric 	int nrequests;
11019630Seric 	char buf[MAXLINE];
11029630Seric 
11039630Seric 	/*
110458250Seric 	**  Check for permission to print the queue
110558250Seric 	*/
110658250Seric 
110763787Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0)
110858250Seric 	{
110958250Seric 		struct stat st;
111058523Seric # ifdef NGROUPS
111158523Seric 		int n;
111263937Seric 		GIDSET_T gidset[NGROUPS];
111358523Seric # endif
111458250Seric 
111558517Seric 		if (stat(QueueDir, &st) < 0)
111658250Seric 		{
111758250Seric 			syserr("Cannot stat %s", QueueDir);
111858250Seric 			return;
111958250Seric 		}
112058523Seric # ifdef NGROUPS
112158523Seric 		n = getgroups(NGROUPS, gidset);
112258523Seric 		while (--n >= 0)
112358523Seric 		{
112458523Seric 			if (gidset[n] == st.st_gid)
112558523Seric 				break;
112658523Seric 		}
112758523Seric 		if (n < 0)
112858523Seric # else
112963787Seric 		if (RealGid != st.st_gid)
113058523Seric # endif
113158250Seric 		{
113258250Seric 			usrerr("510 You are not permitted to see the queue");
113358250Seric 			setstat(EX_NOPERM);
113458250Seric 			return;
113558250Seric 		}
113658250Seric 	}
113758250Seric 
113858250Seric 	/*
11399630Seric 	**  Read and order the queue.
11409630Seric 	*/
11419630Seric 
114224941Seric 	nrequests = orderq(TRUE);
11439630Seric 
11449630Seric 	/*
11459630Seric 	**  Print the work list that we have read.
11469630Seric 	*/
11479630Seric 
11489630Seric 	/* first see if there is anything */
114910070Seric 	if (nrequests <= 0)
11509630Seric 	{
115110070Seric 		printf("Mail queue is empty\n");
11529630Seric 		return;
11539630Seric 	}
11549630Seric 
115551920Seric 	CurrentLA = getla();	/* get load average */
115640934Srick 
115710096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
115825687Seric 	if (nrequests > QUEUESIZE)
115925687Seric 		printf(", only %d printed", QUEUESIZE);
116024979Seric 	if (Verbose)
116158716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
116224979Seric 	else
116358716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11649630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11659630Seric 	{
11669630Seric 		struct stat st;
116710070Seric 		auto time_t submittime = 0;
116810070Seric 		long dfsize = -1;
116958737Seric 		int flags = 0;
117010108Seric 		char message[MAXLINE];
117159093Seric 		char bodytype[MAXNAME];
11729630Seric 
117317468Seric 		f = fopen(w->w_name, "r");
117417468Seric 		if (f == NULL)
117517468Seric 		{
117617468Seric 			errno = 0;
117717468Seric 			continue;
117817468Seric 		}
117958724Seric 		printf("%8s", w->w_name + 2);
118058689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
118110070Seric 			printf("*");
118257438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
118324941Seric 			printf("X");
118410070Seric 		else
118510070Seric 			printf(" ");
118610070Seric 		errno = 0;
118717468Seric 
118859093Seric 		message[0] = bodytype[0] = '\0';
11899630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11909630Seric 		{
119153400Seric 			register int i;
119258737Seric 			register char *p;
119353400Seric 
11949630Seric 			fixcrlf(buf, TRUE);
11959630Seric 			switch (buf[0])
11969630Seric 			{
119710108Seric 			  case 'M':	/* error message */
119853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
119958737Seric 					i = sizeof message - 1;
120053400Seric 				bcopy(&buf[1], message, i);
120153400Seric 				message[i] = '\0';
120210108Seric 				break;
120310108Seric 
120459093Seric 			  case 'B':	/* body type */
120559093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
120659093Seric 					i = sizeof bodytype - 1;
120759093Seric 				bcopy(&buf[1], bodytype, i);
120859093Seric 				bodytype[i] = '\0';
120959093Seric 				break;
121059093Seric 
12119630Seric 			  case 'S':	/* sender name */
121224979Seric 				if (Verbose)
121358737Seric 					printf("%8ld %10ld%c%.12s %.38s",
121458737Seric 					    dfsize,
121558737Seric 					    w->w_pri,
121658737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
121758737Seric 					    ctime(&submittime) + 4,
121824979Seric 					    &buf[1]);
121924979Seric 				else
122024979Seric 					printf("%8ld %.16s %.45s", dfsize,
122124979Seric 					    ctime(&submittime), &buf[1]);
122259093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
122359093Seric 				{
122459093Seric 					printf("\n    %10.10s", bodytype);
122559093Seric 					if (message[0] != '\0')
122659093Seric 						printf("   (%.60s)", message);
122759093Seric 				}
12289630Seric 				break;
122951920Seric 
123040973Sbostic 			  case 'C':	/* controlling user */
123154974Seric 				if (Verbose)
123258716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
123358716Seric 						&buf[1]);
123440973Sbostic 				break;
12359630Seric 
12369630Seric 			  case 'R':	/* recipient name */
123724979Seric 				if (Verbose)
123858716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
123924979Seric 				else
124058716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12419630Seric 				break;
12429630Seric 
12439630Seric 			  case 'T':	/* creation time */
124424941Seric 				submittime = atol(&buf[1]);
12459630Seric 				break;
124610070Seric 
124710070Seric 			  case 'D':	/* data file name */
124810070Seric 				if (stat(&buf[1], &st) >= 0)
124910070Seric 					dfsize = st.st_size;
125010070Seric 				break;
125158737Seric 
125258737Seric 			  case 'F':	/* flag bits */
125358737Seric 				for (p = &buf[1]; *p != '\0'; p++)
125458737Seric 				{
125558737Seric 					switch (*p)
125658737Seric 					{
125758737Seric 					  case 'w':
125858737Seric 						flags |= EF_WARNING;
125958737Seric 						break;
126058737Seric 					}
126158737Seric 				}
12629630Seric 			}
12639630Seric 		}
126410070Seric 		if (submittime == (time_t) 0)
126510070Seric 			printf(" (no control file)");
12669630Seric 		printf("\n");
126723098Seric 		(void) fclose(f);
12689630Seric 	}
12699630Seric }
12709630Seric 
127156795Seric # endif /* QUEUE */
127217468Seric /*
127317468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
127417468Seric **
127517468Seric **	Assigns an id code if one does not already exist.
127617468Seric **	This code is very careful to avoid trashing existing files
127717468Seric **	under any circumstances.
127817468Seric **
127917468Seric **	Parameters:
128017468Seric **		e -- envelope to build it in/from.
128117468Seric **		type -- the file type, used as the first character
128217468Seric **			of the file name.
128317468Seric **
128417468Seric **	Returns:
128517468Seric **		a pointer to the new file name (in a static buffer).
128617468Seric **
128717468Seric **	Side Effects:
128851920Seric **		If no id code is already assigned, queuename will
128951920Seric **		assign an id code, create a qf file, and leave a
129051920Seric **		locked, open-for-write file pointer in the envelope.
129117468Seric */
129217468Seric 
129317468Seric char *
129417468Seric queuename(e, type)
129517468Seric 	register ENVELOPE *e;
129659700Seric 	int type;
129717468Seric {
129817468Seric 	static int pid = -1;
129958741Seric 	static char c0;
130058741Seric 	static char c1;
130158741Seric 	static char c2;
130258716Seric 	time_t now;
130358716Seric 	struct tm *tm;
130458689Seric 	static char buf[MAXNAME];
130517468Seric 
130617468Seric 	if (e->e_id == NULL)
130717468Seric 	{
130817468Seric 		char qf[20];
130917468Seric 
131017468Seric 		/* find a unique id */
131117468Seric 		if (pid != getpid())
131217468Seric 		{
131317468Seric 			/* new process -- start back at "AA" */
131417468Seric 			pid = getpid();
131558716Seric 			now = curtime();
131658716Seric 			tm = localtime(&now);
131758716Seric 			c0 = 'A' + tm->tm_hour;
131817468Seric 			c1 = 'A';
131917468Seric 			c2 = 'A' - 1;
132017468Seric 		}
132158716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
132217468Seric 
132317468Seric 		while (c1 < '~' || c2 < 'Z')
132417468Seric 		{
132517468Seric 			int i;
132617468Seric 
132717468Seric 			if (c2 >= 'Z')
132817468Seric 			{
132917468Seric 				c1++;
133017468Seric 				c2 = 'A' - 1;
133117468Seric 			}
133258716Seric 			qf[3] = c1;
133358716Seric 			qf[4] = ++c2;
133417468Seric 			if (tTd(7, 20))
133540934Srick 				printf("queuename: trying \"%s\"\n", qf);
133617468Seric 
133740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
133851920Seric 			if (i < 0)
133951920Seric 			{
134051920Seric 				if (errno == EEXIST)
134151920Seric 					continue;
134251920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
134351920Seric 					qf, QueueDir);
134451920Seric 				exit(EX_UNAVAILABLE);
134551920Seric 			}
134658689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
134751920Seric 			{
134851920Seric 				e->e_lockfp = fdopen(i, "w");
134940934Srick 				break;
135017468Seric 			}
135151920Seric 
135251920Seric 			/* a reader got the file; abandon it and try again */
135351920Seric 			(void) close(i);
135417468Seric 		}
135517468Seric 		if (c1 >= '~' && c2 >= 'Z')
135617468Seric 		{
135717468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
135817468Seric 				qf, QueueDir);
135917468Seric 			exit(EX_OSERR);
136017468Seric 		}
136117468Seric 		e->e_id = newstr(&qf[2]);
136217468Seric 		define('i', e->e_id, e);
136317468Seric 		if (tTd(7, 1))
136417468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
136517468Seric # ifdef LOG
136658020Seric 		if (LogLevel > 93)
136717468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
136856795Seric # endif /* LOG */
136917468Seric 	}
137017468Seric 
137117468Seric 	if (type == '\0')
137217468Seric 		return (NULL);
137317468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
137417468Seric 	if (tTd(7, 2))
137517468Seric 		printf("queuename: %s\n", buf);
137617468Seric 	return (buf);
137717468Seric }
137817468Seric /*
137917468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
138017468Seric **
138117468Seric **	Parameters:
138217468Seric **		e -- the envelope to unlock.
138317468Seric **
138417468Seric **	Returns:
138517468Seric **		none
138617468Seric **
138717468Seric **	Side Effects:
138817468Seric **		unlocks the queue for `e'.
138917468Seric */
139017468Seric 
139117468Seric unlockqueue(e)
139217468Seric 	ENVELOPE *e;
139317468Seric {
139458680Seric 	if (tTd(51, 4))
139558680Seric 		printf("unlockqueue(%s)\n", e->e_id);
139658680Seric 
139751920Seric 	/* if there is a lock file in the envelope, close it */
139851920Seric 	if (e->e_lockfp != NULL)
139958680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
140051920Seric 	e->e_lockfp = NULL;
140151920Seric 
140258728Seric 	/* don't create a queue id if we don't already have one */
140358728Seric 	if (e->e_id == NULL)
140458728Seric 		return;
140558728Seric 
140617468Seric 	/* remove the transcript */
140717468Seric # ifdef LOG
140858020Seric 	if (LogLevel > 87)
140917468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
141056795Seric # endif /* LOG */
141158680Seric 	if (!tTd(51, 104))
141217468Seric 		xunlink(queuename(e, 'x'));
141317468Seric 
141417468Seric }
141540973Sbostic /*
141654974Seric **  SETCTLUSER -- create a controlling address
141740973Sbostic **
141854974Seric **	Create a fake "address" given only a local login name; this is
141954974Seric **	used as a "controlling user" for future recipient addresses.
142040973Sbostic **
142140973Sbostic **	Parameters:
142254974Seric **		user -- the user name of the controlling user.
142340973Sbostic **
142440973Sbostic **	Returns:
142554974Seric **		An address descriptor for the controlling user.
142640973Sbostic **
142740973Sbostic **	Side Effects:
142840973Sbostic **		none.
142940973Sbostic */
143040973Sbostic 
143154974Seric ADDRESS *
143254974Seric setctluser(user)
143354974Seric 	char *user;
143440973Sbostic {
143554974Seric 	register ADDRESS *a;
143640973Sbostic 	struct passwd *pw;
143759113Seric 	char *p;
143840973Sbostic 
143940973Sbostic 	/*
144054974Seric 	**  See if this clears our concept of controlling user.
144140973Sbostic 	*/
144240973Sbostic 
144363850Seric 	if (user == NULL || *user == '\0')
144463850Seric 		return NULL;
144540973Sbostic 
144640973Sbostic 	/*
144754974Seric 	**  Set up addr fields for controlling user.
144840973Sbostic 	*/
144940973Sbostic 
145054974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
145154974Seric 	bzero((char *) a, sizeof *a);
145259113Seric 
145359113Seric 	p = strchr(user, ':');
145459113Seric 	if (p != NULL)
145559113Seric 		*p++ = '\0';
145659270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
145740973Sbostic 	{
145840973Sbostic 		a->q_home = newstr(pw->pw_dir);
145940973Sbostic 		a->q_uid = pw->pw_uid;
146040973Sbostic 		a->q_gid = pw->pw_gid;
146157642Seric 		a->q_user = newstr(user);
146259270Seric 		a->q_flags |= QGOODUID;
146340973Sbostic 	}
146440973Sbostic 	else
146540973Sbostic 	{
146657642Seric 		a->q_user = newstr(DefUser);
146740973Sbostic 	}
146840973Sbostic 
146959270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
147056328Seric 	a->q_mailer = LocalMailer;
147159113Seric 	if (p == NULL)
147259113Seric 		a->q_paddr = a->q_user;
147359113Seric 	else
147459113Seric 		a->q_paddr = newstr(p);
147554974Seric 	return a;
147640973Sbostic }
1477