xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 63787)
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*63787Seric static char sccsid[] = "@(#)queue.c	8.3 (Berkeley) 07/13/93 (with queueing)";
1433731Sbostic #else
15*63787Seric static char sccsid[] = "@(#)queue.c	8.3 (Berkeley) 07/13/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194634Seric # include <signal.h>
204632Seric # include <errno.h>
2140973Sbostic # include <pwd.h>
2257736Seric # include <dirent.h>
234632Seric 
2433731Sbostic # ifdef QUEUE
254632Seric 
264632Seric /*
279377Seric **  Work queue.
289377Seric */
299377Seric 
309377Seric struct work
319377Seric {
329377Seric 	char		*w_name;	/* name of control file */
339377Seric 	long		w_pri;		/* priority of message, see below */
3425013Seric 	time_t		w_ctime;	/* creation time of message */
359377Seric 	struct work	*w_next;	/* next in queue */
369377Seric };
379377Seric 
389377Seric typedef struct work	WORK;
399377Seric 
409377Seric WORK	*WorkQ;			/* queue of things to be done */
419377Seric /*
424632Seric **  QUEUEUP -- queue a message up for future transmission.
434632Seric **
444632Seric **	Parameters:
456980Seric **		e -- the envelope to queue up.
466999Seric **		queueall -- if TRUE, queue all addresses, rather than
476999Seric **			just those with the QQUEUEUP flag set.
489377Seric **		announce -- if TRUE, tell when you are queueing up.
494632Seric **
504632Seric **	Returns:
5151920Seric **		none.
524632Seric **
534632Seric **	Side Effects:
549377Seric **		The current request are saved in a control file.
5551920Seric **		The queue file is left locked.
564632Seric */
574632Seric 
589377Seric queueup(e, queueall, announce)
596980Seric 	register ENVELOPE *e;
606999Seric 	bool queueall;
619377Seric 	bool announce;
624632Seric {
637812Seric 	char *qf;
647812Seric 	register FILE *tfp;
654632Seric 	register HDR *h;
665007Seric 	register ADDRESS *q;
6751920Seric 	int fd;
6851920Seric 	int i;
6951920Seric 	bool newid;
7053400Seric 	register char *p;
7110173Seric 	MAILER nullmailer;
7251920Seric 	char buf[MAXLINE], tf[MAXLINE];
734632Seric 
745037Seric 	/*
7517477Seric 	**  Create control file.
765037Seric 	*/
774632Seric 
7851920Seric 	newid = (e->e_id == NULL);
7951920Seric 	strcpy(tf, queuename(e, 't'));
8051920Seric 	tfp = e->e_lockfp;
8151920Seric 	if (tfp == NULL)
8251920Seric 		newid = FALSE;
8351920Seric 	if (newid)
8451835Seric 	{
8551920Seric 		tfp = e->e_lockfp;
8651920Seric 	}
8751920Seric 	else
8851920Seric 	{
8951920Seric 		/* get a locked tf file */
9051920Seric 		for (i = 100; --i >= 0; )
9151835Seric 		{
9251920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9351920Seric 			if (fd < 0)
9451835Seric 			{
9551920Seric 				if (errno == EEXIST)
9651920Seric 					continue;
9758801Seric notemp:
9858690Seric 				syserr("!queueup: cannot create temp file %s", tf);
9941636Srick 			}
10058689Seric 
10158689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10251920Seric 				break;
10358689Seric 
10451920Seric 			close(fd);
10558801Seric 			sleep(i);
10641636Srick 		}
10758801Seric 		if (fd < 0)
10858801Seric 			goto notemp;
10941636Srick 
11051920Seric 		tfp = fdopen(fd, "w");
11151920Seric 	}
1124632Seric 
1137677Seric 	if (tTd(40, 1))
11417468Seric 		printf("queueing %s\n", e->e_id);
1154632Seric 
1164632Seric 	/*
1176980Seric 	**  If there is no data file yet, create one.
1186980Seric 	*/
1196980Seric 
1206980Seric 	if (e->e_df == NULL)
1216980Seric 	{
1226980Seric 		register FILE *dfp;
1239389Seric 		extern putbody();
1246980Seric 
1257812Seric 		e->e_df = newstr(queuename(e, 'd'));
12640934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
12740934Srick 		if (fd < 0)
12858690Seric 			syserr("!queueup: cannot create %s", e->e_df);
12940934Srick 		dfp = fdopen(fd, "w");
13059730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
13158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1329389Seric 		e->e_putbody = putbody;
1336980Seric 	}
1346980Seric 
1356980Seric 	/*
1364632Seric 	**  Output future work requests.
13725687Seric 	**	Priority and creation time should be first, since
13825687Seric 	**	they are required by orderq.
1394632Seric 	*/
1404632Seric 
1419377Seric 	/* output message priority */
1429377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1439377Seric 
1449630Seric 	/* output creation time */
1459630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1469630Seric 
14759093Seric 	/* output type and name of data file */
14859093Seric 	if (e->e_bodytype != NULL)
14959093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1507812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1514632Seric 
15210108Seric 	/* message from envelope, if it exists */
15310108Seric 	if (e->e_message != NULL)
15410108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15510108Seric 
15658737Seric 	/* send various flag bits through */
15758737Seric 	p = buf;
15858737Seric 	if (bitset(EF_WARNING, e->e_flags))
15958737Seric 		*p++ = 'w';
16058737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16158737Seric 		*p++ = 'r';
16258737Seric 	*p++ = '\0';
16358737Seric 	if (buf[0] != '\0')
16458737Seric 		fprintf(tfp, "F%s\n", buf);
16558737Seric 
16658957Seric 	/* $r and $s and $_ macro values */
16753400Seric 	if ((p = macvalue('r', e)) != NULL)
16853400Seric 		fprintf(tfp, "$r%s\n", p);
16953400Seric 	if ((p = macvalue('s', e)) != NULL)
17053400Seric 		fprintf(tfp, "$s%s\n", p);
17158957Seric 	if ((p = macvalue('_', e)) != NULL)
17258957Seric 		fprintf(tfp, "$_%s\n", p);
17353400Seric 
1744632Seric 	/* output name of sender */
1757812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1764632Seric 
17755360Seric 	/* output list of error recipients */
17859670Seric 	printctladdr(NULL, NULL);
17955360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18055360Seric 	{
18158680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18255360Seric 		{
18359113Seric 			printctladdr(q, tfp);
18455360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18555360Seric 		}
18655360Seric 	}
18755360Seric 
1884632Seric 	/* output list of recipient addresses */
1896980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1904632Seric 	{
19158250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19258680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1938245Seric 		{
19459113Seric 			printctladdr(q, tfp);
1957812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1969377Seric 			if (announce)
1979377Seric 			{
1989377Seric 				e->e_to = q->q_paddr;
19958151Seric 				message("queued");
20058020Seric 				if (LogLevel > 8)
20158337Seric 					logdelivery(NULL, NULL, "queued", e);
2029377Seric 				e->e_to = NULL;
2039377Seric 			}
2049387Seric 			if (tTd(40, 1))
2059387Seric 			{
2069387Seric 				printf("queueing ");
2079387Seric 				printaddr(q, FALSE);
2089387Seric 			}
2098245Seric 		}
2104632Seric 	}
2114632Seric 
2129377Seric 	/*
2139377Seric 	**  Output headers for this message.
2149377Seric 	**	Expand macros completely here.  Queue run will deal with
2159377Seric 	**	everything as absolute headers.
2169377Seric 	**		All headers that must be relative to the recipient
2179377Seric 	**		can be cracked later.
21810173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
21910173Seric 	**	no effect on the addresses as they are output.
2209377Seric 	*/
2219377Seric 
22210686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22358020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
22458020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
22510349Seric 	nullmailer.m_eol = "\n";
22610173Seric 
22758050Seric 	define('g', "\201f", e);
2286980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2294632Seric 	{
23010686Seric 		extern bool bitzerop();
23110686Seric 
23212015Seric 		/* don't output null headers */
2334632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2344632Seric 			continue;
23512015Seric 
23612015Seric 		/* don't output resent headers on non-resent messages */
23712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
23812015Seric 			continue;
23912015Seric 
24012015Seric 		/* output this header */
2417812Seric 		fprintf(tfp, "H");
24212015Seric 
24312015Seric 		/* if conditional, output the set of conditions */
24410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
24510686Seric 		{
24610686Seric 			int j;
24710686Seric 
24823098Seric 			(void) putc('?', tfp);
24910686Seric 			for (j = '\0'; j <= '\177'; j++)
25010686Seric 				if (bitnset(j, h->h_mflags))
25123098Seric 					(void) putc(j, tfp);
25223098Seric 			(void) putc('?', tfp);
25310686Seric 		}
25412015Seric 
25512015Seric 		/* output the header: expand macros, convert addresses */
2567763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2577763Seric 		{
2587763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2598236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2607763Seric 		}
2618245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2629348Seric 		{
26358737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
26463753Seric 			FILE *savetrace = TrafficLogFile;
26558737Seric 
26663753Seric 			TrafficLogFile = NULL;
26763753Seric 
26858737Seric 			if (bitset(H_FROM, h->h_flags))
26958737Seric 				oldstyle = FALSE;
27058737Seric 
27158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27255012Seric 				 &nullmailer, e);
27363753Seric 
27463753Seric 			TrafficLogFile = savetrace;
2759348Seric 		}
2767763Seric 		else
2778245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2784632Seric 	}
2794632Seric 
2804632Seric 	/*
2814632Seric 	**  Clean up.
2824632Seric 	*/
2834632Seric 
28458732Seric 	fflush(tfp);
28560603Seric 	fsync(fileno(tfp));
28658732Seric 	if (ferror(tfp))
28758732Seric 	{
28858732Seric 		if (newid)
28958732Seric 			syserr("!552 Error writing control file %s", tf);
29058732Seric 		else
29158732Seric 			syserr("!452 Error writing control file %s", tf);
29258732Seric 	}
29358732Seric 
29451920Seric 	if (!newid)
29551920Seric 	{
29651920Seric 		qf = queuename(e, 'q');
29751920Seric 		if (rename(tf, qf) < 0)
29851920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29951920Seric 		if (e->e_lockfp != NULL)
30058680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
30151920Seric 		e->e_lockfp = tfp;
30251920Seric 	}
30351920Seric 	else
30451920Seric 		qf = tf;
30541636Srick 	errno = 0;
3067391Seric 
3077677Seric # ifdef LOG
3087677Seric 	/* save log info */
30958020Seric 	if (LogLevel > 79)
3107878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
31156795Seric # endif /* LOG */
31251920Seric 	return;
3134632Seric }
31454974Seric 
31554974Seric printctladdr(a, tfp)
31659113Seric 	register ADDRESS *a;
31754974Seric 	FILE *tfp;
31854974Seric {
31959113Seric 	char *uname;
32059113Seric 	register struct passwd *pw;
32159113Seric 	register ADDRESS *q;
32259113Seric 	uid_t uid;
32359113Seric 	static ADDRESS *lastctladdr;
32459113Seric 	static uid_t lastuid;
32554974Seric 
32659113Seric 	/* initialization */
32759670Seric 	if (a == NULL || tfp == NULL)
32854974Seric 	{
32959670Seric 		if (lastctladdr != NULL && tfp != NULL)
33059113Seric 			fprintf(tfp, "C\n");
33159113Seric 		lastctladdr = NULL;
33259113Seric 		lastuid = 0;
33354974Seric 		return;
33454974Seric 	}
33559113Seric 
33659113Seric 	/* find the active uid */
33759113Seric 	q = getctladdr(a);
33859113Seric 	if (q == NULL)
33959113Seric 		uid = 0;
34054974Seric 	else
34159113Seric 		uid = q->q_uid;
34259113Seric 
34359113Seric 	/* if a is an alias, use that for printing */
34459113Seric 	if (a->q_alias != NULL)
34559113Seric 		a = a->q_alias;
34659113Seric 
34759113Seric 	/* check to see if this is the same as last time */
34859113Seric 	if (lastctladdr != NULL && uid == lastuid &&
34959113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
35059113Seric 		return;
35159113Seric 	lastuid = uid;
35259113Seric 	lastctladdr = a;
35359113Seric 
35459113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35559270Seric 		uname = "";
35659113Seric 	else
35759113Seric 		uname = pw->pw_name;
35859113Seric 
35959113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
36054974Seric }
36154974Seric 
3624632Seric /*
3634632Seric **  RUNQUEUE -- run the jobs in the queue.
3644632Seric **
3654632Seric **	Gets the stuff out of the queue in some presumably logical
3664632Seric **	order and processes them.
3674632Seric **
3684632Seric **	Parameters:
36924941Seric **		forkflag -- TRUE if the queue scanning should be done in
37024941Seric **			a child process.  We double-fork so it is not our
37124941Seric **			child and we don't have to clean up after it.
3724632Seric **
3734632Seric **	Returns:
3744632Seric **		none.
3754632Seric **
3764632Seric **	Side Effects:
3774632Seric **		runs things in the mail queue.
3784632Seric */
3794632Seric 
38055360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
38155360Seric 
38255360Seric runqueue(forkflag)
3834639Seric 	bool forkflag;
3844632Seric {
38555360Seric 	register ENVELOPE *e;
38655360Seric 	extern ENVELOPE BlankEnvelope;
38724953Seric 
3887466Seric 	/*
38924953Seric 	**  If no work will ever be selected, don't even bother reading
39024953Seric 	**  the queue.
39124953Seric 	*/
39224953Seric 
39351920Seric 	CurrentLA = getla();	/* get load average */
39440934Srick 
39558132Seric 	if (shouldqueue(0L, curtime()))
39624953Seric 	{
39724953Seric 		if (Verbose)
39824953Seric 			printf("Skipping queue run -- load average too high\n");
39958107Seric 		if (forkflag && QueueIntvl != 0)
40058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
40155360Seric 		return;
40224953Seric 	}
40324953Seric 
40424953Seric 	/*
4057466Seric 	**  See if we want to go off and do other useful work.
4067466Seric 	*/
4074639Seric 
4084639Seric 	if (forkflag)
4094639Seric 	{
4107943Seric 		int pid;
4117943Seric 
4127943Seric 		pid = dofork();
4137943Seric 		if (pid != 0)
4144639Seric 		{
41546928Sbostic 			extern void reapchild();
41625184Seric 
4177943Seric 			/* parent -- pick up intermediate zombie */
41825184Seric #ifndef SIGCHLD
4199377Seric 			(void) waitfor(pid);
42056795Seric #else /* SIGCHLD */
42125184Seric 			(void) signal(SIGCHLD, reapchild);
42256795Seric #endif /* SIGCHLD */
4237690Seric 			if (QueueIntvl != 0)
4249348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4254639Seric 			return;
4264639Seric 		}
4277943Seric 		/* child -- double fork */
42825184Seric #ifndef SIGCHLD
4297943Seric 		if (fork() != 0)
4307943Seric 			exit(EX_OK);
43156795Seric #else /* SIGCHLD */
43225184Seric 		(void) signal(SIGCHLD, SIG_DFL);
43356795Seric #endif /* SIGCHLD */
4344639Seric 	}
43524941Seric 
43640934Srick 	setproctitle("running queue: %s", QueueDir);
43724941Seric 
4387876Seric # ifdef LOG
43958020Seric 	if (LogLevel > 69)
44055360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
44155360Seric 			QueueDir, getpid(), forkflag);
44256795Seric # endif /* LOG */
4434639Seric 
4447466Seric 	/*
44510205Seric 	**  Release any resources used by the daemon code.
44610205Seric 	*/
44710205Seric 
44810205Seric # ifdef DAEMON
44910205Seric 	clrdaemon();
45056795Seric # endif /* DAEMON */
45110205Seric 
45210205Seric 	/*
45355360Seric 	**  Create ourselves an envelope
45455360Seric 	*/
45555360Seric 
45655360Seric 	CurEnv = &QueueEnvelope;
45758179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45855360Seric 	e->e_flags = BlankEnvelope.e_flags;
45955360Seric 
46055360Seric 	/*
46127175Seric 	**  Make sure the alias database is open.
46227175Seric 	*/
46327175Seric 
46460537Seric 	initmaps(FALSE, e);
46527175Seric 
46627175Seric 	/*
4677466Seric 	**  Start making passes through the queue.
4687466Seric 	**	First, read and sort the entire queue.
4697466Seric 	**	Then, process the work in that order.
4707466Seric 	**		But if you take too long, start over.
4717466Seric 	*/
4727466Seric 
4737943Seric 	/* order the existing work requests */
47424954Seric 	(void) orderq(FALSE);
4757690Seric 
4767943Seric 	/* process them once at a time */
4777943Seric 	while (WorkQ != NULL)
4784639Seric 	{
4797943Seric 		WORK *w = WorkQ;
4807881Seric 
4817943Seric 		WorkQ = WorkQ->w_next;
48258884Seric 
48358884Seric 		/*
48458884Seric 		**  Ignore jobs that are too expensive for the moment.
48558884Seric 		*/
48658884Seric 
48758884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
48858884Seric 		{
48958884Seric 			if (Verbose)
49058884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
49158884Seric 		}
49258930Seric 		else
49358930Seric 		{
49458930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
49558930Seric 		}
4967943Seric 		free(w->w_name);
4977943Seric 		free((char *) w);
4984639Seric 	}
49929866Seric 
50029866Seric 	/* exit without the usual cleanup */
50155467Seric 	e->e_id = NULL;
50255467Seric 	finis();
5034634Seric }
5044634Seric /*
5054632Seric **  ORDERQ -- order the work queue.
5064632Seric **
5074632Seric **	Parameters:
50824941Seric **		doall -- if set, include everything in the queue (even
50924941Seric **			the jobs that cannot be run because the load
51024941Seric **			average is too high).  Otherwise, exclude those
51124941Seric **			jobs.
5124632Seric **
5134632Seric **	Returns:
51410121Seric **		The number of request in the queue (not necessarily
51510121Seric **		the number of requests in WorkQ however).
5164632Seric **
5174632Seric **	Side Effects:
5184632Seric **		Sets WorkQ to the queue of available work, in order.
5194632Seric */
5204632Seric 
52125687Seric # define NEED_P		001
52225687Seric # define NEED_T		002
52358318Seric # define NEED_R		004
52458318Seric # define NEED_S		010
5254632Seric 
52624941Seric orderq(doall)
52724941Seric 	bool doall;
5284632Seric {
52960219Seric 	register struct dirent *d;
5304632Seric 	register WORK *w;
5316625Sglickman 	DIR *f;
5324632Seric 	register int i;
53325687Seric 	WORK wlist[QUEUESIZE+1];
53410070Seric 	int wn = -1;
5354632Seric 	extern workcmpf();
5364632Seric 
53758318Seric 	if (tTd(41, 1))
53858318Seric 	{
53958318Seric 		printf("orderq:\n");
54058318Seric 		if (QueueLimitId != NULL)
54158318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
54258318Seric 		if (QueueLimitSender != NULL)
54358318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
54458318Seric 		if (QueueLimitRecipient != NULL)
54558318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
54658318Seric 	}
54758318Seric 
5484632Seric 	/* clear out old WorkQ */
5494632Seric 	for (w = WorkQ; w != NULL; )
5504632Seric 	{
5514632Seric 		register WORK *nw = w->w_next;
5524632Seric 
5534632Seric 		WorkQ = nw;
5544632Seric 		free(w->w_name);
5554632Seric 		free((char *) w);
5564632Seric 		w = nw;
5574632Seric 	}
5584632Seric 
5594632Seric 	/* open the queue directory */
5608148Seric 	f = opendir(".");
5614632Seric 	if (f == NULL)
5624632Seric 	{
5638148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
56410070Seric 		return (0);
5654632Seric 	}
5664632Seric 
5674632Seric 	/*
5684632Seric 	**  Read the work directory.
5694632Seric 	*/
5704632Seric 
57110070Seric 	while ((d = readdir(f)) != NULL)
5724632Seric 	{
5739377Seric 		FILE *cf;
5744632Seric 		char lbuf[MAXNAME];
57558318Seric 		extern bool strcontainedin();
5764632Seric 
5774632Seric 		/* is this an interesting entry? */
5787812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5794632Seric 			continue;
5804632Seric 
58158318Seric 		if (QueueLimitId != NULL &&
58258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
58358318Seric 			continue;
58458318Seric 
58558722Seric 		/*
58658722Seric 		**  Check queue name for plausibility.  This handles
58758722Seric 		**  both old and new type ids.
58858722Seric 		*/
58958722Seric 
59058722Seric 		i = strlen(d->d_name);
59158722Seric 		if (i != 9 && i != 10)
59258020Seric 		{
59358020Seric 			if (Verbose)
59458020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
59558020Seric #ifdef LOG
59658020Seric 			if (LogLevel > 3)
59759615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
59858020Seric 					d->d_name);
59958020Seric #endif
60058020Seric 			if (strlen(d->d_name) >= MAXNAME)
60158020Seric 				d->d_name[MAXNAME - 1] = '\0';
60258020Seric 			strcpy(lbuf, d->d_name);
60358020Seric 			lbuf[0] = 'Q';
60458020Seric 			(void) rename(d->d_name, lbuf);
60558020Seric 			continue;
60658020Seric 		}
60758020Seric 
60810070Seric 		/* yes -- open control file (if not too many files) */
60925687Seric 		if (++wn >= QUEUESIZE)
61010070Seric 			continue;
61158318Seric 
6128148Seric 		cf = fopen(d->d_name, "r");
6134632Seric 		if (cf == NULL)
6144632Seric 		{
6157055Seric 			/* this may be some random person sending hir msgs */
6167055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
61710090Seric 			if (tTd(41, 2))
61810090Seric 				printf("orderq: cannot open %s (%d)\n",
61910090Seric 					d->d_name, errno);
6207055Seric 			errno = 0;
62110090Seric 			wn--;
6224632Seric 			continue;
6234632Seric 		}
62425687Seric 		w = &wlist[wn];
62525687Seric 		w->w_name = newstr(d->d_name);
6264632Seric 
62725027Seric 		/* make sure jobs in creation don't clog queue */
62825687Seric 		w->w_pri = 0x7fffffff;
62925687Seric 		w->w_ctime = 0;
63025027Seric 
6314632Seric 		/* extract useful information */
63225687Seric 		i = NEED_P | NEED_T;
63358318Seric 		if (QueueLimitSender != NULL)
63458318Seric 			i |= NEED_S;
63558318Seric 		if (QueueLimitRecipient != NULL)
63658318Seric 			i |= NEED_R;
63725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6384632Seric 		{
63924954Seric 			extern long atol();
64058318Seric 			extern bool strcontainedin();
64124954Seric 
64224941Seric 			switch (lbuf[0])
6434632Seric 			{
64424941Seric 			  case 'P':
64525687Seric 				w->w_pri = atol(&lbuf[1]);
64625687Seric 				i &= ~NEED_P;
6474632Seric 				break;
64825013Seric 
64925013Seric 			  case 'T':
65025687Seric 				w->w_ctime = atol(&lbuf[1]);
65125687Seric 				i &= ~NEED_T;
65225013Seric 				break;
65358318Seric 
65458318Seric 			  case 'R':
65558318Seric 				if (QueueLimitRecipient != NULL &&
65658318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
65758318Seric 					i &= ~NEED_R;
65858318Seric 				break;
65958318Seric 
66058318Seric 			  case 'S':
66158318Seric 				if (QueueLimitSender != NULL &&
66258318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
66358318Seric 					i &= ~NEED_S;
66458318Seric 				break;
6654632Seric 			}
6664632Seric 		}
6674632Seric 		(void) fclose(cf);
66824953Seric 
66958318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
67058318Seric 		    bitset(NEED_R|NEED_S, i))
67124953Seric 		{
67224953Seric 			/* don't even bother sorting this job in */
67324953Seric 			wn--;
67424953Seric 		}
6754632Seric 	}
6766625Sglickman 	(void) closedir(f);
67710090Seric 	wn++;
6784632Seric 
6794632Seric 	/*
6804632Seric 	**  Sort the work directory.
6814632Seric 	*/
6824632Seric 
68325687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6844632Seric 
6854632Seric 	/*
6864632Seric 	**  Convert the work list into canonical form.
6879377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6884632Seric 	*/
6894632Seric 
69024981Seric 	WorkQ = NULL;
69125687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6924632Seric 	{
6934632Seric 		w = (WORK *) xalloc(sizeof *w);
6944632Seric 		w->w_name = wlist[i].w_name;
6954632Seric 		w->w_pri = wlist[i].w_pri;
69625013Seric 		w->w_ctime = wlist[i].w_ctime;
69724981Seric 		w->w_next = WorkQ;
69824981Seric 		WorkQ = w;
6994632Seric 	}
7004632Seric 
7017677Seric 	if (tTd(40, 1))
7024632Seric 	{
7034632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7045037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7054632Seric 	}
70610070Seric 
70710090Seric 	return (wn);
7084632Seric }
7094632Seric /*
7107677Seric **  WORKCMPF -- compare function for ordering work.
7114632Seric **
7124632Seric **	Parameters:
7134632Seric **		a -- the first argument.
7144632Seric **		b -- the second argument.
7154632Seric **
7164632Seric **	Returns:
71724981Seric **		-1 if a < b
71824981Seric **		 0 if a == b
71924981Seric **		+1 if a > b
7204632Seric **
7214632Seric **	Side Effects:
7224632Seric **		none.
7234632Seric */
7244632Seric 
7254632Seric workcmpf(a, b)
7265037Seric 	register WORK *a;
7275037Seric 	register WORK *b;
7284632Seric {
72957438Seric 	long pa = a->w_pri;
73057438Seric 	long pb = b->w_pri;
73124941Seric 
73224941Seric 	if (pa == pb)
7334632Seric 		return (0);
73424941Seric 	else if (pa > pb)
73524981Seric 		return (1);
73624981Seric 	else
73710121Seric 		return (-1);
7384632Seric }
7394632Seric /*
7404632Seric **  DOWORK -- do a work request.
7414632Seric **
7424632Seric **	Parameters:
74358884Seric **		id -- the ID of the job to run.
74458884Seric **		forkflag -- if set, run this in background.
74558924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
74658924Seric **			This is used when expanding aliases in the queue.
74761094Seric **			If forkflag is also set, it doesn't wait for the
74861094Seric **			child.
74958884Seric **		e - the envelope in which to run it.
7504632Seric **
7514632Seric **	Returns:
7524632Seric **		none.
7534632Seric **
7544632Seric **	Side Effects:
7554632Seric **		The work request is satisfied if possible.
7564632Seric */
7574632Seric 
75858924Seric dowork(id, forkflag, requeueflag, e)
75958884Seric 	char *id;
76058884Seric 	bool forkflag;
76158924Seric 	bool requeueflag;
76255012Seric 	register ENVELOPE *e;
7634632Seric {
7644632Seric 	register int i;
76551920Seric 	extern bool readqf();
7664632Seric 
7677677Seric 	if (tTd(40, 1))
76858884Seric 		printf("dowork(%s)\n", id);
7694632Seric 
7704632Seric 	/*
77124941Seric 	**  Fork for work.
77224941Seric 	*/
77324941Seric 
77458884Seric 	if (forkflag)
77524941Seric 	{
77624941Seric 		i = fork();
77724941Seric 		if (i < 0)
77824941Seric 		{
77924941Seric 			syserr("dowork: cannot fork");
78024941Seric 			return;
78124941Seric 		}
78224941Seric 	}
78324941Seric 	else
78424941Seric 	{
78524941Seric 		i = 0;
78624941Seric 	}
78724941Seric 
7884632Seric 	if (i == 0)
7894632Seric 	{
7904632Seric 		/*
7914632Seric 		**  CHILD
7928148Seric 		**	Lock the control file to avoid duplicate deliveries.
7938148Seric 		**		Then run the file as though we had just read it.
7947350Seric 		**	We save an idea of the temporary name so we
7957350Seric 		**		can recover on interrupt.
7964632Seric 		*/
7974632Seric 
7987763Seric 		/* set basic modes, etc. */
7997356Seric 		(void) alarm(0);
80055012Seric 		clearenvelope(e, FALSE);
80158737Seric 		e->e_flags |= EF_QUEUERUN;
80258734Seric 		e->e_errormode = EM_MAIL;
80358884Seric 		e->e_id = id;
8047876Seric # ifdef LOG
80558020Seric 		if (LogLevel > 76)
80655012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8077881Seric 			       getpid());
80856795Seric # endif /* LOG */
8097763Seric 
8107763Seric 		/* don't use the headers from sendmail.cf... */
81155012Seric 		e->e_header = NULL;
8127763Seric 
81351920Seric 		/* read the queue control file -- return if locked */
81455012Seric 		if (!readqf(e))
8156980Seric 		{
81658884Seric 			if (tTd(40, 4))
81758884Seric 				printf("readqf(%s) failed\n", e->e_id);
81858884Seric 			if (forkflag)
81924941Seric 				exit(EX_OK);
82024941Seric 			else
82124941Seric 				return;
8226980Seric 		}
8236980Seric 
82455012Seric 		e->e_flags |= EF_INQUEUE;
82558929Seric 		eatheader(e, requeueflag);
8266980Seric 
82758924Seric 		if (requeueflag)
82858924Seric 			queueup(e, TRUE, FALSE);
82958924Seric 
8306980Seric 		/* do the delivery */
83158915Seric 		sendall(e, SM_DELIVER);
8326980Seric 
8336980Seric 		/* finish up and exit */
83458884Seric 		if (forkflag)
83524941Seric 			finis();
83624941Seric 		else
83755012Seric 			dropenvelope(e);
8384632Seric 	}
83961094Seric 	else if (!requeueflag)
84024941Seric 	{
84124941Seric 		/*
84224941Seric 		**  Parent -- pick up results.
84324941Seric 		*/
8444632Seric 
84524941Seric 		errno = 0;
84624941Seric 		(void) waitfor(i);
84724941Seric 	}
8484632Seric }
8494632Seric /*
8504632Seric **  READQF -- read queue file and set up environment.
8514632Seric **
8524632Seric **	Parameters:
8539377Seric **		e -- the envelope of the job to run.
8544632Seric **
8554632Seric **	Returns:
85651920Seric **		TRUE if it successfully read the queue file.
85751920Seric **		FALSE otherwise.
8584632Seric **
8594632Seric **	Side Effects:
86051920Seric **		The queue file is returned locked.
8614632Seric */
8624632Seric 
86351920Seric bool
86451920Seric readqf(e)
8659377Seric 	register ENVELOPE *e;
8664632Seric {
86717477Seric 	register FILE *qfp;
86854974Seric 	ADDRESS *ctladdr;
86956400Seric 	struct stat st;
87057135Seric 	char *bp;
87158915Seric 	char qf[20];
87257135Seric 	char buf[MAXLINE];
87324954Seric 	extern long atol();
87454974Seric 	extern ADDRESS *setctluser();
8754632Seric 
8764632Seric 	/*
87717468Seric 	**  Read and process the file.
8784632Seric 	*/
8794632Seric 
88058915Seric 	strcpy(qf, queuename(e, 'q'));
88151937Seric 	qfp = fopen(qf, "r+");
88217477Seric 	if (qfp == NULL)
88317477Seric 	{
88458884Seric 		if (tTd(40, 8))
88558884Seric 			printf("readqf(%s): fopen failure (%s)\n",
88658884Seric 				qf, errstring(errno));
88740934Srick 		if (errno != ENOENT)
88840934Srick 			syserr("readqf: no control file %s", qf);
88951920Seric 		return FALSE;
89017477Seric 	}
89140934Srick 
89256400Seric 	/*
89356400Seric 	**  Check the queue file for plausibility to avoid attacks.
89456400Seric 	*/
89556400Seric 
89656400Seric 	if (fstat(fileno(qfp), &st) < 0)
89756400Seric 	{
89856400Seric 		/* must have been being processed by someone else */
89958884Seric 		if (tTd(40, 8))
90058884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90158884Seric 				qf, errstring(errno));
90256400Seric 		fclose(qfp);
90356400Seric 		return FALSE;
90456400Seric 	}
90556400Seric 
90657135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
90756400Seric 	{
90856400Seric # ifdef LOG
90956400Seric 		if (LogLevel > 0)
91056400Seric 		{
91156400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91256400Seric 				e->e_id, st.st_uid, st.st_mode);
91356400Seric 		}
91456795Seric # endif /* LOG */
91558884Seric 		if (tTd(40, 8))
91658884Seric 			printf("readqf(%s): bogus file\n", qf);
91756400Seric 		fclose(qfp);
91863753Seric 		rename(qf, queuename(e, 'Q'));
91956400Seric 		return FALSE;
92056400Seric 	}
92156400Seric 
92258689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
92340934Srick 	{
92458689Seric 		/* being processed by another queuer */
92558884Seric 		if (tTd(40, 8))
92658884Seric 			printf("readqf(%s): locked\n", qf);
92758689Seric 		if (Verbose)
92858689Seric 			printf("%s: locked\n", e->e_id);
92951920Seric # ifdef LOG
93058689Seric 		if (LogLevel > 19)
93158689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
93256795Seric # endif /* LOG */
93340934Srick 		(void) fclose(qfp);
93451920Seric 		return FALSE;
93540934Srick 	}
93640934Srick 
93759101Seric 	if (st.st_size == 0)
93859101Seric 	{
93959101Seric 		/* must be a bogus file -- just remove it */
94059101Seric 		(void) unlink(qf);
94159101Seric 		fclose(qfp);
94259101Seric 		return FALSE;
94359101Seric 	}
94459101Seric 
94551920Seric 	/* save this lock */
94651920Seric 	e->e_lockfp = qfp;
94751920Seric 
94840934Srick 	/* do basic system initialization */
94955012Seric 	initsys(e);
95040934Srick 
95117477Seric 	FileName = qf;
9529377Seric 	LineNumber = 0;
95351920Seric 	if (Verbose)
9549377Seric 		printf("\nRunning %s\n", e->e_id);
95554974Seric 	ctladdr = NULL;
95657135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9574632Seric 	{
95858737Seric 		register char *p;
95957529Seric 		struct stat st;
96057529Seric 
96126504Seric 		if (tTd(40, 4))
96257135Seric 			printf("+++++ %s\n", bp);
96357135Seric 		switch (bp[0])
9644632Seric 		{
96540973Sbostic 		  case 'C':		/* specify controlling user */
96657135Seric 			ctladdr = setctluser(&bp[1]);
96740973Sbostic 			break;
96840973Sbostic 
9694632Seric 		  case 'R':		/* specify recipient */
97058082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9714632Seric 			break;
9724632Seric 
97325687Seric 		  case 'E':		/* specify error recipient */
97458082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
97525687Seric 			break;
97625687Seric 
9774632Seric 		  case 'H':		/* header */
97857135Seric 			(void) chompheader(&bp[1], FALSE, e);
9794632Seric 			break;
9804632Seric 
98110108Seric 		  case 'M':		/* message */
98257135Seric 			e->e_message = newstr(&bp[1]);
98310108Seric 			break;
98410108Seric 
9854632Seric 		  case 'S':		/* sender */
98658704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9874632Seric 			break;
9884632Seric 
98959093Seric 		  case 'B':		/* body type */
99059093Seric 			e->e_bodytype = newstr(&bp[1]);
99159093Seric 			break;
99259093Seric 
9934632Seric 		  case 'D':		/* data file name */
99457135Seric 			e->e_df = newstr(&bp[1]);
9959544Seric 			e->e_dfp = fopen(e->e_df, "r");
9969544Seric 			if (e->e_dfp == NULL)
99758020Seric 			{
9989377Seric 				syserr("readqf: cannot open %s", e->e_df);
99958020Seric 				e->e_msgsize = -1;
100058020Seric 			}
100158020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
100257529Seric 				e->e_msgsize = st.st_size;
10034632Seric 			break;
10044632Seric 
10057860Seric 		  case 'T':		/* init time */
100657135Seric 			e->e_ctime = atol(&bp[1]);
10074632Seric 			break;
10084632Seric 
10094634Seric 		  case 'P':		/* message priority */
101057135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10114634Seric 			break;
10124634Seric 
101358737Seric 		  case 'F':		/* flag bits */
101458737Seric 			for (p = &bp[1]; *p != '\0'; p++)
101558737Seric 			{
101658737Seric 				switch (*p)
101758737Seric 				{
101858737Seric 				  case 'w':	/* warning sent */
101958737Seric 					e->e_flags |= EF_WARNING;
102058737Seric 					break;
102158737Seric 
102258737Seric 				  case 'r':	/* response */
102358737Seric 					e->e_flags |= EF_RESPONSE;
102458737Seric 					break;
102558737Seric 				}
102658737Seric 			}
102758737Seric 			break;
102858737Seric 
102953400Seric 		  case '$':		/* define macro */
103057135Seric 			define(bp[1], newstr(&bp[2]), e);
103153400Seric 			break;
103253400Seric 
103324941Seric 		  case '\0':		/* blank line; ignore */
103424941Seric 			break;
103524941Seric 
10364632Seric 		  default:
103759700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
103857135Seric 				LineNumber, bp);
103963753Seric 			fclose(qfp);
104063753Seric 			rename(qf, queuename(e, 'Q'));
104163753Seric 			return FALSE;
10424632Seric 		}
104357135Seric 
104457135Seric 		if (bp != buf)
104557135Seric 			free(bp);
10464632Seric 	}
10479377Seric 
10489377Seric 	FileName = NULL;
104924941Seric 
105024941Seric 	/*
105124941Seric 	**  If we haven't read any lines, this queue file is empty.
105224941Seric 	**  Arrange to remove it without referencing any null pointers.
105324941Seric 	*/
105424941Seric 
105524941Seric 	if (LineNumber == 0)
105624941Seric 	{
105724941Seric 		errno = 0;
105824941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
105924941Seric 	}
106051920Seric 	return TRUE;
10614632Seric }
10624632Seric /*
10639630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10649630Seric **
10659630Seric **	Parameters:
10669630Seric **		none.
10679630Seric **
10689630Seric **	Returns:
10699630Seric **		none.
10709630Seric **
10719630Seric **	Side Effects:
10729630Seric **		Prints a listing of the mail queue on the standard output.
10739630Seric */
10745182Seric 
10759630Seric printqueue()
10769630Seric {
10779630Seric 	register WORK *w;
10789630Seric 	FILE *f;
107910070Seric 	int nrequests;
10809630Seric 	char buf[MAXLINE];
10819630Seric 
10829630Seric 	/*
108358250Seric 	**  Check for permission to print the queue
108458250Seric 	*/
108558250Seric 
1086*63787Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0)
108758250Seric 	{
108858250Seric 		struct stat st;
108958523Seric # ifdef NGROUPS
109058523Seric 		int n;
109158523Seric 		int gidset[NGROUPS];
109258523Seric # endif
109358250Seric 
109458517Seric 		if (stat(QueueDir, &st) < 0)
109558250Seric 		{
109658250Seric 			syserr("Cannot stat %s", QueueDir);
109758250Seric 			return;
109858250Seric 		}
109958523Seric # ifdef NGROUPS
110058523Seric 		n = getgroups(NGROUPS, gidset);
110158523Seric 		while (--n >= 0)
110258523Seric 		{
110358523Seric 			if (gidset[n] == st.st_gid)
110458523Seric 				break;
110558523Seric 		}
110658523Seric 		if (n < 0)
110758523Seric # else
1108*63787Seric 		if (RealGid != st.st_gid)
110958523Seric # endif
111058250Seric 		{
111158250Seric 			usrerr("510 You are not permitted to see the queue");
111258250Seric 			setstat(EX_NOPERM);
111358250Seric 			return;
111458250Seric 		}
111558250Seric 	}
111658250Seric 
111758250Seric 	/*
11189630Seric 	**  Read and order the queue.
11199630Seric 	*/
11209630Seric 
112124941Seric 	nrequests = orderq(TRUE);
11229630Seric 
11239630Seric 	/*
11249630Seric 	**  Print the work list that we have read.
11259630Seric 	*/
11269630Seric 
11279630Seric 	/* first see if there is anything */
112810070Seric 	if (nrequests <= 0)
11299630Seric 	{
113010070Seric 		printf("Mail queue is empty\n");
11319630Seric 		return;
11329630Seric 	}
11339630Seric 
113451920Seric 	CurrentLA = getla();	/* get load average */
113540934Srick 
113610096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
113725687Seric 	if (nrequests > QUEUESIZE)
113825687Seric 		printf(", only %d printed", QUEUESIZE);
113924979Seric 	if (Verbose)
114058716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114124979Seric 	else
114258716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11439630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11449630Seric 	{
11459630Seric 		struct stat st;
114610070Seric 		auto time_t submittime = 0;
114710070Seric 		long dfsize = -1;
114858737Seric 		int flags = 0;
114910108Seric 		char message[MAXLINE];
115059093Seric 		char bodytype[MAXNAME];
11519630Seric 
115217468Seric 		f = fopen(w->w_name, "r");
115317468Seric 		if (f == NULL)
115417468Seric 		{
115517468Seric 			errno = 0;
115617468Seric 			continue;
115717468Seric 		}
115858724Seric 		printf("%8s", w->w_name + 2);
115958689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
116010070Seric 			printf("*");
116157438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
116224941Seric 			printf("X");
116310070Seric 		else
116410070Seric 			printf(" ");
116510070Seric 		errno = 0;
116617468Seric 
116759093Seric 		message[0] = bodytype[0] = '\0';
11689630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11699630Seric 		{
117053400Seric 			register int i;
117158737Seric 			register char *p;
117253400Seric 
11739630Seric 			fixcrlf(buf, TRUE);
11749630Seric 			switch (buf[0])
11759630Seric 			{
117610108Seric 			  case 'M':	/* error message */
117753400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
117858737Seric 					i = sizeof message - 1;
117953400Seric 				bcopy(&buf[1], message, i);
118053400Seric 				message[i] = '\0';
118110108Seric 				break;
118210108Seric 
118359093Seric 			  case 'B':	/* body type */
118459093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
118559093Seric 					i = sizeof bodytype - 1;
118659093Seric 				bcopy(&buf[1], bodytype, i);
118759093Seric 				bodytype[i] = '\0';
118859093Seric 				break;
118959093Seric 
11909630Seric 			  case 'S':	/* sender name */
119124979Seric 				if (Verbose)
119258737Seric 					printf("%8ld %10ld%c%.12s %.38s",
119358737Seric 					    dfsize,
119458737Seric 					    w->w_pri,
119558737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
119658737Seric 					    ctime(&submittime) + 4,
119724979Seric 					    &buf[1]);
119824979Seric 				else
119924979Seric 					printf("%8ld %.16s %.45s", dfsize,
120024979Seric 					    ctime(&submittime), &buf[1]);
120159093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
120259093Seric 				{
120359093Seric 					printf("\n    %10.10s", bodytype);
120459093Seric 					if (message[0] != '\0')
120559093Seric 						printf("   (%.60s)", message);
120659093Seric 				}
12079630Seric 				break;
120851920Seric 
120940973Sbostic 			  case 'C':	/* controlling user */
121054974Seric 				if (Verbose)
121158716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
121258716Seric 						&buf[1]);
121340973Sbostic 				break;
12149630Seric 
12159630Seric 			  case 'R':	/* recipient name */
121624979Seric 				if (Verbose)
121758716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
121824979Seric 				else
121958716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12209630Seric 				break;
12219630Seric 
12229630Seric 			  case 'T':	/* creation time */
122324941Seric 				submittime = atol(&buf[1]);
12249630Seric 				break;
122510070Seric 
122610070Seric 			  case 'D':	/* data file name */
122710070Seric 				if (stat(&buf[1], &st) >= 0)
122810070Seric 					dfsize = st.st_size;
122910070Seric 				break;
123058737Seric 
123158737Seric 			  case 'F':	/* flag bits */
123258737Seric 				for (p = &buf[1]; *p != '\0'; p++)
123358737Seric 				{
123458737Seric 					switch (*p)
123558737Seric 					{
123658737Seric 					  case 'w':
123758737Seric 						flags |= EF_WARNING;
123858737Seric 						break;
123958737Seric 					}
124058737Seric 				}
12419630Seric 			}
12429630Seric 		}
124310070Seric 		if (submittime == (time_t) 0)
124410070Seric 			printf(" (no control file)");
12459630Seric 		printf("\n");
124623098Seric 		(void) fclose(f);
12479630Seric 	}
12489630Seric }
12499630Seric 
125056795Seric # endif /* QUEUE */
125117468Seric /*
125217468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
125317468Seric **
125417468Seric **	Assigns an id code if one does not already exist.
125517468Seric **	This code is very careful to avoid trashing existing files
125617468Seric **	under any circumstances.
125717468Seric **
125817468Seric **	Parameters:
125917468Seric **		e -- envelope to build it in/from.
126017468Seric **		type -- the file type, used as the first character
126117468Seric **			of the file name.
126217468Seric **
126317468Seric **	Returns:
126417468Seric **		a pointer to the new file name (in a static buffer).
126517468Seric **
126617468Seric **	Side Effects:
126751920Seric **		If no id code is already assigned, queuename will
126851920Seric **		assign an id code, create a qf file, and leave a
126951920Seric **		locked, open-for-write file pointer in the envelope.
127017468Seric */
127117468Seric 
127217468Seric char *
127317468Seric queuename(e, type)
127417468Seric 	register ENVELOPE *e;
127559700Seric 	int type;
127617468Seric {
127717468Seric 	static int pid = -1;
127858741Seric 	static char c0;
127958741Seric 	static char c1;
128058741Seric 	static char c2;
128158716Seric 	time_t now;
128258716Seric 	struct tm *tm;
128358689Seric 	static char buf[MAXNAME];
128417468Seric 
128517468Seric 	if (e->e_id == NULL)
128617468Seric 	{
128717468Seric 		char qf[20];
128817468Seric 
128917468Seric 		/* find a unique id */
129017468Seric 		if (pid != getpid())
129117468Seric 		{
129217468Seric 			/* new process -- start back at "AA" */
129317468Seric 			pid = getpid();
129458716Seric 			now = curtime();
129558716Seric 			tm = localtime(&now);
129658716Seric 			c0 = 'A' + tm->tm_hour;
129717468Seric 			c1 = 'A';
129817468Seric 			c2 = 'A' - 1;
129917468Seric 		}
130058716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130117468Seric 
130217468Seric 		while (c1 < '~' || c2 < 'Z')
130317468Seric 		{
130417468Seric 			int i;
130517468Seric 
130617468Seric 			if (c2 >= 'Z')
130717468Seric 			{
130817468Seric 				c1++;
130917468Seric 				c2 = 'A' - 1;
131017468Seric 			}
131158716Seric 			qf[3] = c1;
131258716Seric 			qf[4] = ++c2;
131317468Seric 			if (tTd(7, 20))
131440934Srick 				printf("queuename: trying \"%s\"\n", qf);
131517468Seric 
131640934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
131751920Seric 			if (i < 0)
131851920Seric 			{
131951920Seric 				if (errno == EEXIST)
132051920Seric 					continue;
132151920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132251920Seric 					qf, QueueDir);
132351920Seric 				exit(EX_UNAVAILABLE);
132451920Seric 			}
132558689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
132651920Seric 			{
132751920Seric 				e->e_lockfp = fdopen(i, "w");
132840934Srick 				break;
132917468Seric 			}
133051920Seric 
133151920Seric 			/* a reader got the file; abandon it and try again */
133251920Seric 			(void) close(i);
133317468Seric 		}
133417468Seric 		if (c1 >= '~' && c2 >= 'Z')
133517468Seric 		{
133617468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
133717468Seric 				qf, QueueDir);
133817468Seric 			exit(EX_OSERR);
133917468Seric 		}
134017468Seric 		e->e_id = newstr(&qf[2]);
134117468Seric 		define('i', e->e_id, e);
134217468Seric 		if (tTd(7, 1))
134317468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
134417468Seric # ifdef LOG
134558020Seric 		if (LogLevel > 93)
134617468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
134756795Seric # endif /* LOG */
134817468Seric 	}
134917468Seric 
135017468Seric 	if (type == '\0')
135117468Seric 		return (NULL);
135217468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
135317468Seric 	if (tTd(7, 2))
135417468Seric 		printf("queuename: %s\n", buf);
135517468Seric 	return (buf);
135617468Seric }
135717468Seric /*
135817468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
135917468Seric **
136017468Seric **	Parameters:
136117468Seric **		e -- the envelope to unlock.
136217468Seric **
136317468Seric **	Returns:
136417468Seric **		none
136517468Seric **
136617468Seric **	Side Effects:
136717468Seric **		unlocks the queue for `e'.
136817468Seric */
136917468Seric 
137017468Seric unlockqueue(e)
137117468Seric 	ENVELOPE *e;
137217468Seric {
137358680Seric 	if (tTd(51, 4))
137458680Seric 		printf("unlockqueue(%s)\n", e->e_id);
137558680Seric 
137651920Seric 	/* if there is a lock file in the envelope, close it */
137751920Seric 	if (e->e_lockfp != NULL)
137858680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
137951920Seric 	e->e_lockfp = NULL;
138051920Seric 
138158728Seric 	/* don't create a queue id if we don't already have one */
138258728Seric 	if (e->e_id == NULL)
138358728Seric 		return;
138458728Seric 
138517468Seric 	/* remove the transcript */
138617468Seric # ifdef LOG
138758020Seric 	if (LogLevel > 87)
138817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
138956795Seric # endif /* LOG */
139058680Seric 	if (!tTd(51, 104))
139117468Seric 		xunlink(queuename(e, 'x'));
139217468Seric 
139317468Seric }
139440973Sbostic /*
139554974Seric **  SETCTLUSER -- create a controlling address
139640973Sbostic **
139754974Seric **	Create a fake "address" given only a local login name; this is
139854974Seric **	used as a "controlling user" for future recipient addresses.
139940973Sbostic **
140040973Sbostic **	Parameters:
140154974Seric **		user -- the user name of the controlling user.
140240973Sbostic **
140340973Sbostic **	Returns:
140454974Seric **		An address descriptor for the controlling user.
140540973Sbostic **
140640973Sbostic **	Side Effects:
140740973Sbostic **		none.
140840973Sbostic */
140940973Sbostic 
141054974Seric ADDRESS *
141154974Seric setctluser(user)
141254974Seric 	char *user;
141340973Sbostic {
141454974Seric 	register ADDRESS *a;
141540973Sbostic 	struct passwd *pw;
141659113Seric 	char *p;
141740973Sbostic 
141840973Sbostic 	/*
141954974Seric 	**  See if this clears our concept of controlling user.
142040973Sbostic 	*/
142140973Sbostic 
142259270Seric 	if (user == NULL)
142359270Seric 		user = "";
142440973Sbostic 
142540973Sbostic 	/*
142654974Seric 	**  Set up addr fields for controlling user.
142740973Sbostic 	*/
142840973Sbostic 
142954974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143054974Seric 	bzero((char *) a, sizeof *a);
143159113Seric 
143259113Seric 	p = strchr(user, ':');
143359113Seric 	if (p != NULL)
143459113Seric 		*p++ = '\0';
143559270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
143640973Sbostic 	{
143740973Sbostic 		a->q_home = newstr(pw->pw_dir);
143840973Sbostic 		a->q_uid = pw->pw_uid;
143940973Sbostic 		a->q_gid = pw->pw_gid;
144057642Seric 		a->q_user = newstr(user);
144159270Seric 		a->q_flags |= QGOODUID;
144240973Sbostic 	}
144340973Sbostic 	else
144440973Sbostic 	{
144557642Seric 		a->q_user = newstr(DefUser);
144640973Sbostic 	}
144740973Sbostic 
144859270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
144956328Seric 	a->q_mailer = LocalMailer;
145059113Seric 	if (p == NULL)
145159113Seric 		a->q_paddr = a->q_user;
145259113Seric 	else
145359113Seric 		a->q_paddr = newstr(p);
145454974Seric 	return a;
145540973Sbostic }
1456