xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 59275)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*59275Seric static char sccsid[] = "@(#)queue.c	6.51 (Berkeley) 04/26/93 (with queueing)";
1433731Sbostic #else
15*59275Seric static char sccsid[] = "@(#)queue.c	6.51 (Berkeley) 04/26/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
214634Seric # include <signal.h>
224632Seric # include <errno.h>
2340973Sbostic # include <pwd.h>
2457977Seric # ifndef MAXNAMLEN
2557736Seric # include <dirent.h>
2657736Seric # endif
274632Seric 
2833731Sbostic # ifdef QUEUE
294632Seric 
304632Seric /*
319377Seric **  Work queue.
329377Seric */
339377Seric 
349377Seric struct work
359377Seric {
369377Seric 	char		*w_name;	/* name of control file */
379377Seric 	long		w_pri;		/* priority of message, see below */
3825013Seric 	time_t		w_ctime;	/* creation time of message */
399377Seric 	struct work	*w_next;	/* next in queue */
409377Seric };
419377Seric 
429377Seric typedef struct work	WORK;
439377Seric 
449377Seric WORK	*WorkQ;			/* queue of things to be done */
459377Seric /*
464632Seric **  QUEUEUP -- queue a message up for future transmission.
474632Seric **
484632Seric **	Parameters:
496980Seric **		e -- the envelope to queue up.
506999Seric **		queueall -- if TRUE, queue all addresses, rather than
516999Seric **			just those with the QQUEUEUP flag set.
529377Seric **		announce -- if TRUE, tell when you are queueing up.
534632Seric **
544632Seric **	Returns:
5551920Seric **		none.
564632Seric **
574632Seric **	Side Effects:
589377Seric **		The current request are saved in a control file.
5951920Seric **		The queue file is left locked.
604632Seric */
614632Seric 
629377Seric queueup(e, queueall, announce)
636980Seric 	register ENVELOPE *e;
646999Seric 	bool queueall;
659377Seric 	bool announce;
664632Seric {
677812Seric 	char *qf;
687812Seric 	register FILE *tfp;
694632Seric 	register HDR *h;
705007Seric 	register ADDRESS *q;
7151920Seric 	int fd;
7251920Seric 	int i;
7351920Seric 	bool newid;
7453400Seric 	register char *p;
7510173Seric 	MAILER nullmailer;
7651920Seric 	char buf[MAXLINE], tf[MAXLINE];
7753400Seric 	extern char *macvalue();
784632Seric 
795037Seric 	/*
8017477Seric 	**  Create control file.
815037Seric 	*/
824632Seric 
8351920Seric 	newid = (e->e_id == NULL);
8451920Seric 	strcpy(tf, queuename(e, 't'));
8551920Seric 	tfp = e->e_lockfp;
8651920Seric 	if (tfp == NULL)
8751920Seric 		newid = FALSE;
8851920Seric 	if (newid)
8951835Seric 	{
9051920Seric 		tfp = e->e_lockfp;
9151920Seric 	}
9251920Seric 	else
9351920Seric 	{
9451920Seric 		/* get a locked tf file */
9551920Seric 		for (i = 100; --i >= 0; )
9651835Seric 		{
9758689Seric 			extern bool lockfile();
9851937Seric 
9951920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10051920Seric 			if (fd < 0)
10151835Seric 			{
10251920Seric 				if (errno == EEXIST)
10351920Seric 					continue;
10458801Seric notemp:
10558690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10641636Srick 			}
10758689Seric 
10858689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10951920Seric 				break;
11058689Seric 
11151920Seric 			close(fd);
11258801Seric 			sleep(i);
11341636Srick 		}
11458801Seric 		if (fd < 0)
11558801Seric 			goto notemp;
11641636Srick 
11751920Seric 		tfp = fdopen(fd, "w");
11851920Seric 	}
1194632Seric 
1207677Seric 	if (tTd(40, 1))
12117468Seric 		printf("queueing %s\n", e->e_id);
1224632Seric 
1234632Seric 	/*
1246980Seric 	**  If there is no data file yet, create one.
1256980Seric 	*/
1266980Seric 
1276980Seric 	if (e->e_df == NULL)
1286980Seric 	{
1296980Seric 		register FILE *dfp;
1309389Seric 		extern putbody();
1316980Seric 
1327812Seric 		e->e_df = newstr(queuename(e, 'd'));
13340934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13440934Srick 		if (fd < 0)
13558690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13640934Srick 		dfp = fdopen(fd, "w");
137*59275Seric 		(*e->e_putbody)(dfp, FileMailer, e);
13858680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1399389Seric 		e->e_putbody = putbody;
1406980Seric 	}
1416980Seric 
1426980Seric 	/*
1434632Seric 	**  Output future work requests.
14425687Seric 	**	Priority and creation time should be first, since
14525687Seric 	**	they are required by orderq.
1464632Seric 	*/
1474632Seric 
1489377Seric 	/* output message priority */
1499377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1509377Seric 
1519630Seric 	/* output creation time */
1529630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1539630Seric 
15459093Seric 	/* output type and name of data file */
15559093Seric 	if (e->e_bodytype != NULL)
15659093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1577812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1584632Seric 
15910108Seric 	/* message from envelope, if it exists */
16010108Seric 	if (e->e_message != NULL)
16110108Seric 		fprintf(tfp, "M%s\n", e->e_message);
16210108Seric 
16358737Seric 	/* send various flag bits through */
16458737Seric 	p = buf;
16558737Seric 	if (bitset(EF_WARNING, e->e_flags))
16658737Seric 		*p++ = 'w';
16758737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16858737Seric 		*p++ = 'r';
16958737Seric 	*p++ = '\0';
17058737Seric 	if (buf[0] != '\0')
17158737Seric 		fprintf(tfp, "F%s\n", buf);
17258737Seric 
17358957Seric 	/* $r and $s and $_ macro values */
17453400Seric 	if ((p = macvalue('r', e)) != NULL)
17553400Seric 		fprintf(tfp, "$r%s\n", p);
17653400Seric 	if ((p = macvalue('s', e)) != NULL)
17753400Seric 		fprintf(tfp, "$s%s\n", p);
17858957Seric 	if ((p = macvalue('_', e)) != NULL)
17958957Seric 		fprintf(tfp, "$_%s\n", p);
18053400Seric 
1814632Seric 	/* output name of sender */
1827812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1834632Seric 
18455360Seric 	/* output list of error recipients */
18559113Seric 	printctladdr(NULL, tfp);
18655360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18755360Seric 	{
18858680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18955360Seric 		{
19059113Seric 			printctladdr(q, tfp);
19155360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
19255360Seric 		}
19355360Seric 	}
19455360Seric 
1954632Seric 	/* output list of recipient addresses */
1966980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1974632Seric 	{
19858250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19958680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2008245Seric 		{
20159113Seric 			printctladdr(q, tfp);
2027812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2039377Seric 			if (announce)
2049377Seric 			{
2059377Seric 				e->e_to = q->q_paddr;
20658151Seric 				message("queued");
20758020Seric 				if (LogLevel > 8)
20858337Seric 					logdelivery(NULL, NULL, "queued", e);
2099377Seric 				e->e_to = NULL;
2109377Seric 			}
2119387Seric 			if (tTd(40, 1))
2129387Seric 			{
2139387Seric 				printf("queueing ");
2149387Seric 				printaddr(q, FALSE);
2159387Seric 			}
2168245Seric 		}
2174632Seric 	}
2184632Seric 
2199377Seric 	/*
2209377Seric 	**  Output headers for this message.
2219377Seric 	**	Expand macros completely here.  Queue run will deal with
2229377Seric 	**	everything as absolute headers.
2239377Seric 	**		All headers that must be relative to the recipient
2249377Seric 	**		can be cracked later.
22510173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
22610173Seric 	**	no effect on the addresses as they are output.
2279377Seric 	*/
2289377Seric 
22910686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
23058020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
23158020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
23210349Seric 	nullmailer.m_eol = "\n";
23310173Seric 
23458050Seric 	define('g', "\201f", e);
2356980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2364632Seric 	{
23710686Seric 		extern bool bitzerop();
23810686Seric 
23912015Seric 		/* don't output null headers */
2404632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2414632Seric 			continue;
24212015Seric 
24312015Seric 		/* don't output resent headers on non-resent messages */
24412015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
24512015Seric 			continue;
24612015Seric 
24712015Seric 		/* output this header */
2487812Seric 		fprintf(tfp, "H");
24912015Seric 
25012015Seric 		/* if conditional, output the set of conditions */
25110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
25210686Seric 		{
25310686Seric 			int j;
25410686Seric 
25523098Seric 			(void) putc('?', tfp);
25610686Seric 			for (j = '\0'; j <= '\177'; j++)
25710686Seric 				if (bitnset(j, h->h_mflags))
25823098Seric 					(void) putc(j, tfp);
25923098Seric 			(void) putc('?', tfp);
26010686Seric 		}
26112015Seric 
26212015Seric 		/* output the header: expand macros, convert addresses */
2637763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2647763Seric 		{
2657763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2668236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2677763Seric 		}
2688245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2699348Seric 		{
27058737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
27158737Seric 
27258737Seric 			if (bitset(H_FROM, h->h_flags))
27358737Seric 				oldstyle = FALSE;
27458737Seric 
27558737Seric 			commaize(h, h->h_value, tfp, oldstyle,
27655012Seric 				 &nullmailer, e);
2779348Seric 		}
2787763Seric 		else
2798245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2804632Seric 	}
2814632Seric 
2824632Seric 	/*
2834632Seric 	**  Clean up.
2844632Seric 	*/
2854632Seric 
28658732Seric 	fflush(tfp);
28758732Seric 	if (ferror(tfp))
28858732Seric 	{
28958732Seric 		if (newid)
29058732Seric 			syserr("!552 Error writing control file %s", tf);
29158732Seric 		else
29258732Seric 			syserr("!452 Error writing control file %s", tf);
29358732Seric 	}
29458732Seric 
29551920Seric 	if (!newid)
29651920Seric 	{
29751920Seric 		qf = queuename(e, 'q');
29851920Seric 		if (rename(tf, qf) < 0)
29951920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
30051920Seric 		if (e->e_lockfp != NULL)
30158680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
30251920Seric 		e->e_lockfp = tfp;
30351920Seric 	}
30451920Seric 	else
30551920Seric 		qf = tf;
30641636Srick 	errno = 0;
3077391Seric 
3087677Seric # ifdef LOG
3097677Seric 	/* save log info */
31058020Seric 	if (LogLevel > 79)
3117878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
31256795Seric # endif /* LOG */
31340934Srick 	fflush(tfp);
31451920Seric 	return;
3154632Seric }
31654974Seric 
31754974Seric printctladdr(a, tfp)
31859113Seric 	register ADDRESS *a;
31954974Seric 	FILE *tfp;
32054974Seric {
32159113Seric 	char *uname;
32259113Seric 	register struct passwd *pw;
32359113Seric 	register ADDRESS *q;
32459113Seric 	uid_t uid;
32559113Seric 	static ADDRESS *lastctladdr;
32659113Seric 	static uid_t lastuid;
32759113Seric 	extern ADDRESS *getctladdr();
32854974Seric 
32959113Seric 	/* initialization */
33054974Seric 	if (a == NULL)
33154974Seric 	{
33259113Seric 		if (lastctladdr != NULL)
33359113Seric 			fprintf(tfp, "C\n");
33459113Seric 		lastctladdr = NULL;
33559113Seric 		lastuid = 0;
33654974Seric 		return;
33754974Seric 	}
33859113Seric 
33959113Seric 	/* find the active uid */
34059113Seric 	q = getctladdr(a);
34159113Seric 	if (q == NULL)
34259113Seric 		uid = 0;
34354974Seric 	else
34459113Seric 		uid = q->q_uid;
34559113Seric 
34659113Seric 	/* if a is an alias, use that for printing */
34759113Seric 	if (a->q_alias != NULL)
34859113Seric 		a = a->q_alias;
34959113Seric 
35059113Seric 	/* check to see if this is the same as last time */
35159113Seric 	if (lastctladdr != NULL && uid == lastuid &&
35259113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
35359113Seric 		return;
35459113Seric 	lastuid = uid;
35559113Seric 	lastctladdr = a;
35659113Seric 
35759113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
35859270Seric 		uname = "";
35959113Seric 	else
36059113Seric 		uname = pw->pw_name;
36159113Seric 
36259113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
36354974Seric }
36454974Seric 
3654632Seric /*
3664632Seric **  RUNQUEUE -- run the jobs in the queue.
3674632Seric **
3684632Seric **	Gets the stuff out of the queue in some presumably logical
3694632Seric **	order and processes them.
3704632Seric **
3714632Seric **	Parameters:
37224941Seric **		forkflag -- TRUE if the queue scanning should be done in
37324941Seric **			a child process.  We double-fork so it is not our
37424941Seric **			child and we don't have to clean up after it.
3754632Seric **
3764632Seric **	Returns:
3774632Seric **		none.
3784632Seric **
3794632Seric **	Side Effects:
3804632Seric **		runs things in the mail queue.
3814632Seric */
3824632Seric 
38355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
38455360Seric 
38555360Seric runqueue(forkflag)
3864639Seric 	bool forkflag;
3874632Seric {
38824953Seric 	extern bool shouldqueue();
38955360Seric 	register ENVELOPE *e;
39055360Seric 	extern ENVELOPE BlankEnvelope;
39155360Seric 	extern ENVELOPE *newenvelope();
39224953Seric 
3937466Seric 	/*
39424953Seric 	**  If no work will ever be selected, don't even bother reading
39524953Seric 	**  the queue.
39624953Seric 	*/
39724953Seric 
39851920Seric 	CurrentLA = getla();	/* get load average */
39940934Srick 
40058132Seric 	if (shouldqueue(0L, curtime()))
40124953Seric 	{
40224953Seric 		if (Verbose)
40324953Seric 			printf("Skipping queue run -- load average too high\n");
40458107Seric 		if (forkflag && QueueIntvl != 0)
40558107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
40655360Seric 		return;
40724953Seric 	}
40824953Seric 
40924953Seric 	/*
4107466Seric 	**  See if we want to go off and do other useful work.
4117466Seric 	*/
4124639Seric 
4134639Seric 	if (forkflag)
4144639Seric 	{
4157943Seric 		int pid;
4167943Seric 
4177943Seric 		pid = dofork();
4187943Seric 		if (pid != 0)
4194639Seric 		{
42046928Sbostic 			extern void reapchild();
42125184Seric 
4227943Seric 			/* parent -- pick up intermediate zombie */
42325184Seric #ifndef SIGCHLD
4249377Seric 			(void) waitfor(pid);
42556795Seric #else /* SIGCHLD */
42625184Seric 			(void) signal(SIGCHLD, reapchild);
42756795Seric #endif /* SIGCHLD */
4287690Seric 			if (QueueIntvl != 0)
4299348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4304639Seric 			return;
4314639Seric 		}
4327943Seric 		/* child -- double fork */
43325184Seric #ifndef SIGCHLD
4347943Seric 		if (fork() != 0)
4357943Seric 			exit(EX_OK);
43656795Seric #else /* SIGCHLD */
43725184Seric 		(void) signal(SIGCHLD, SIG_DFL);
43856795Seric #endif /* SIGCHLD */
4394639Seric 	}
44024941Seric 
44140934Srick 	setproctitle("running queue: %s", QueueDir);
44224941Seric 
4437876Seric # ifdef LOG
44458020Seric 	if (LogLevel > 69)
44555360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
44655360Seric 			QueueDir, getpid(), forkflag);
44756795Seric # endif /* LOG */
4484639Seric 
4497466Seric 	/*
45010205Seric 	**  Release any resources used by the daemon code.
45110205Seric 	*/
45210205Seric 
45310205Seric # ifdef DAEMON
45410205Seric 	clrdaemon();
45556795Seric # endif /* DAEMON */
45610205Seric 
45710205Seric 	/*
45855360Seric 	**  Create ourselves an envelope
45955360Seric 	*/
46055360Seric 
46155360Seric 	CurEnv = &QueueEnvelope;
46258179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
46355360Seric 	e->e_flags = BlankEnvelope.e_flags;
46455360Seric 
46555360Seric 	/*
46627175Seric 	**  Make sure the alias database is open.
46727175Seric 	*/
46827175Seric 
46955012Seric 	initaliases(AliasFile, FALSE, e);
47027175Seric 
47127175Seric 	/*
4727466Seric 	**  Start making passes through the queue.
4737466Seric 	**	First, read and sort the entire queue.
4747466Seric 	**	Then, process the work in that order.
4757466Seric 	**		But if you take too long, start over.
4767466Seric 	*/
4777466Seric 
4787943Seric 	/* order the existing work requests */
47924954Seric 	(void) orderq(FALSE);
4807690Seric 
4817943Seric 	/* process them once at a time */
4827943Seric 	while (WorkQ != NULL)
4834639Seric 	{
4847943Seric 		WORK *w = WorkQ;
48558884Seric 		extern bool shouldqueue();
4867881Seric 
4877943Seric 		WorkQ = WorkQ->w_next;
48858884Seric 
48958884Seric 		/*
49058884Seric 		**  Ignore jobs that are too expensive for the moment.
49158884Seric 		*/
49258884Seric 
49358884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
49458884Seric 		{
49558884Seric 			if (Verbose)
49658884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
49758884Seric 		}
49858930Seric 		else
49958930Seric 		{
50058930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
50158930Seric 		}
5027943Seric 		free(w->w_name);
5037943Seric 		free((char *) w);
5044639Seric 	}
50529866Seric 
50629866Seric 	/* exit without the usual cleanup */
50755467Seric 	e->e_id = NULL;
50855467Seric 	finis();
5094634Seric }
5104634Seric /*
5114632Seric **  ORDERQ -- order the work queue.
5124632Seric **
5134632Seric **	Parameters:
51424941Seric **		doall -- if set, include everything in the queue (even
51524941Seric **			the jobs that cannot be run because the load
51624941Seric **			average is too high).  Otherwise, exclude those
51724941Seric **			jobs.
5184632Seric **
5194632Seric **	Returns:
52010121Seric **		The number of request in the queue (not necessarily
52110121Seric **		the number of requests in WorkQ however).
5224632Seric **
5234632Seric **	Side Effects:
5244632Seric **		Sets WorkQ to the queue of available work, in order.
5254632Seric */
5264632Seric 
52725687Seric # define NEED_P		001
52825687Seric # define NEED_T		002
52958318Seric # define NEED_R		004
53058318Seric # define NEED_S		010
5314632Seric 
53224941Seric orderq(doall)
53324941Seric 	bool doall;
5344632Seric {
5356625Sglickman 	register struct direct *d;
5364632Seric 	register WORK *w;
5376625Sglickman 	DIR *f;
5384632Seric 	register int i;
53925687Seric 	WORK wlist[QUEUESIZE+1];
54010070Seric 	int wn = -1;
5414632Seric 	extern workcmpf();
5424632Seric 
54358318Seric 	if (tTd(41, 1))
54458318Seric 	{
54558318Seric 		printf("orderq:\n");
54658318Seric 		if (QueueLimitId != NULL)
54758318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
54858318Seric 		if (QueueLimitSender != NULL)
54958318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
55058318Seric 		if (QueueLimitRecipient != NULL)
55158318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
55258318Seric 	}
55358318Seric 
5544632Seric 	/* clear out old WorkQ */
5554632Seric 	for (w = WorkQ; w != NULL; )
5564632Seric 	{
5574632Seric 		register WORK *nw = w->w_next;
5584632Seric 
5594632Seric 		WorkQ = nw;
5604632Seric 		free(w->w_name);
5614632Seric 		free((char *) w);
5624632Seric 		w = nw;
5634632Seric 	}
5644632Seric 
5654632Seric 	/* open the queue directory */
5668148Seric 	f = opendir(".");
5674632Seric 	if (f == NULL)
5684632Seric 	{
5698148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
57010070Seric 		return (0);
5714632Seric 	}
5724632Seric 
5734632Seric 	/*
5744632Seric 	**  Read the work directory.
5754632Seric 	*/
5764632Seric 
57710070Seric 	while ((d = readdir(f)) != NULL)
5784632Seric 	{
5799377Seric 		FILE *cf;
5804632Seric 		char lbuf[MAXNAME];
58157642Seric 		extern bool shouldqueue();
58258318Seric 		extern bool strcontainedin();
5834632Seric 
5844632Seric 		/* is this an interesting entry? */
5857812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5864632Seric 			continue;
5874632Seric 
58858318Seric 		if (QueueLimitId != NULL &&
58958318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
59058318Seric 			continue;
59158318Seric 
59258722Seric 		/*
59358722Seric 		**  Check queue name for plausibility.  This handles
59458722Seric 		**  both old and new type ids.
59558722Seric 		*/
59658722Seric 
59758722Seric 		i = strlen(d->d_name);
59858722Seric 		if (i != 9 && i != 10)
59958020Seric 		{
60058020Seric 			if (Verbose)
60158020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
60258020Seric #ifdef LOG
60358020Seric 			if (LogLevel > 3)
60458020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
60558020Seric 					d->d_name);
60658020Seric #endif
60758020Seric 			if (strlen(d->d_name) >= MAXNAME)
60858020Seric 				d->d_name[MAXNAME - 1] = '\0';
60958020Seric 			strcpy(lbuf, d->d_name);
61058020Seric 			lbuf[0] = 'Q';
61158020Seric 			(void) rename(d->d_name, lbuf);
61258020Seric 			continue;
61358020Seric 		}
61458020Seric 
61510070Seric 		/* yes -- open control file (if not too many files) */
61625687Seric 		if (++wn >= QUEUESIZE)
61710070Seric 			continue;
61858318Seric 
6198148Seric 		cf = fopen(d->d_name, "r");
6204632Seric 		if (cf == NULL)
6214632Seric 		{
6227055Seric 			/* this may be some random person sending hir msgs */
6237055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
62410090Seric 			if (tTd(41, 2))
62510090Seric 				printf("orderq: cannot open %s (%d)\n",
62610090Seric 					d->d_name, errno);
6277055Seric 			errno = 0;
62810090Seric 			wn--;
6294632Seric 			continue;
6304632Seric 		}
63125687Seric 		w = &wlist[wn];
63225687Seric 		w->w_name = newstr(d->d_name);
6334632Seric 
63425027Seric 		/* make sure jobs in creation don't clog queue */
63525687Seric 		w->w_pri = 0x7fffffff;
63625687Seric 		w->w_ctime = 0;
63725027Seric 
6384632Seric 		/* extract useful information */
63925687Seric 		i = NEED_P | NEED_T;
64058318Seric 		if (QueueLimitSender != NULL)
64158318Seric 			i |= NEED_S;
64258318Seric 		if (QueueLimitRecipient != NULL)
64358318Seric 			i |= NEED_R;
64425687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6454632Seric 		{
64624954Seric 			extern long atol();
64758318Seric 			extern bool strcontainedin();
64824954Seric 
64924941Seric 			switch (lbuf[0])
6504632Seric 			{
65124941Seric 			  case 'P':
65225687Seric 				w->w_pri = atol(&lbuf[1]);
65325687Seric 				i &= ~NEED_P;
6544632Seric 				break;
65525013Seric 
65625013Seric 			  case 'T':
65725687Seric 				w->w_ctime = atol(&lbuf[1]);
65825687Seric 				i &= ~NEED_T;
65925013Seric 				break;
66058318Seric 
66158318Seric 			  case 'R':
66258318Seric 				if (QueueLimitRecipient != NULL &&
66358318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
66458318Seric 					i &= ~NEED_R;
66558318Seric 				break;
66658318Seric 
66758318Seric 			  case 'S':
66858318Seric 				if (QueueLimitSender != NULL &&
66958318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
67058318Seric 					i &= ~NEED_S;
67158318Seric 				break;
6724632Seric 			}
6734632Seric 		}
6744632Seric 		(void) fclose(cf);
67524953Seric 
67658318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
67758318Seric 		    bitset(NEED_R|NEED_S, i))
67824953Seric 		{
67924953Seric 			/* don't even bother sorting this job in */
68024953Seric 			wn--;
68124953Seric 		}
6824632Seric 	}
6836625Sglickman 	(void) closedir(f);
68410090Seric 	wn++;
6854632Seric 
6864632Seric 	/*
6874632Seric 	**  Sort the work directory.
6884632Seric 	*/
6894632Seric 
69025687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6914632Seric 
6924632Seric 	/*
6934632Seric 	**  Convert the work list into canonical form.
6949377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6954632Seric 	*/
6964632Seric 
69724981Seric 	WorkQ = NULL;
69825687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6994632Seric 	{
7004632Seric 		w = (WORK *) xalloc(sizeof *w);
7014632Seric 		w->w_name = wlist[i].w_name;
7024632Seric 		w->w_pri = wlist[i].w_pri;
70325013Seric 		w->w_ctime = wlist[i].w_ctime;
70424981Seric 		w->w_next = WorkQ;
70524981Seric 		WorkQ = w;
7064632Seric 	}
7074632Seric 
7087677Seric 	if (tTd(40, 1))
7094632Seric 	{
7104632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7115037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7124632Seric 	}
71310070Seric 
71410090Seric 	return (wn);
7154632Seric }
7164632Seric /*
7177677Seric **  WORKCMPF -- compare function for ordering work.
7184632Seric **
7194632Seric **	Parameters:
7204632Seric **		a -- the first argument.
7214632Seric **		b -- the second argument.
7224632Seric **
7234632Seric **	Returns:
72424981Seric **		-1 if a < b
72524981Seric **		 0 if a == b
72624981Seric **		+1 if a > b
7274632Seric **
7284632Seric **	Side Effects:
7294632Seric **		none.
7304632Seric */
7314632Seric 
7324632Seric workcmpf(a, b)
7335037Seric 	register WORK *a;
7345037Seric 	register WORK *b;
7354632Seric {
73657438Seric 	long pa = a->w_pri;
73757438Seric 	long pb = b->w_pri;
73824941Seric 
73924941Seric 	if (pa == pb)
7404632Seric 		return (0);
74124941Seric 	else if (pa > pb)
74224981Seric 		return (1);
74324981Seric 	else
74410121Seric 		return (-1);
7454632Seric }
7464632Seric /*
7474632Seric **  DOWORK -- do a work request.
7484632Seric **
7494632Seric **	Parameters:
75058884Seric **		id -- the ID of the job to run.
75158884Seric **		forkflag -- if set, run this in background.
75258924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
75358924Seric **			This is used when expanding aliases in the queue.
75458884Seric **		e - the envelope in which to run it.
7554632Seric **
7564632Seric **	Returns:
7574632Seric **		none.
7584632Seric **
7594632Seric **	Side Effects:
7604632Seric **		The work request is satisfied if possible.
7614632Seric */
7624632Seric 
76358924Seric dowork(id, forkflag, requeueflag, e)
76458884Seric 	char *id;
76558884Seric 	bool forkflag;
76658924Seric 	bool requeueflag;
76755012Seric 	register ENVELOPE *e;
7684632Seric {
7694632Seric 	register int i;
77051920Seric 	extern bool readqf();
7714632Seric 
7727677Seric 	if (tTd(40, 1))
77358884Seric 		printf("dowork(%s)\n", id);
7744632Seric 
7754632Seric 	/*
77624941Seric 	**  Fork for work.
77724941Seric 	*/
77824941Seric 
77958884Seric 	if (forkflag)
78024941Seric 	{
78124941Seric 		i = fork();
78224941Seric 		if (i < 0)
78324941Seric 		{
78424941Seric 			syserr("dowork: cannot fork");
78524941Seric 			return;
78624941Seric 		}
78724941Seric 	}
78824941Seric 	else
78924941Seric 	{
79024941Seric 		i = 0;
79124941Seric 	}
79224941Seric 
7934632Seric 	if (i == 0)
7944632Seric 	{
7954632Seric 		/*
7964632Seric 		**  CHILD
7978148Seric 		**	Lock the control file to avoid duplicate deliveries.
7988148Seric 		**		Then run the file as though we had just read it.
7997350Seric 		**	We save an idea of the temporary name so we
8007350Seric 		**		can recover on interrupt.
8014632Seric 		*/
8024632Seric 
8037763Seric 		/* set basic modes, etc. */
8047356Seric 		(void) alarm(0);
80555012Seric 		clearenvelope(e, FALSE);
80658737Seric 		e->e_flags |= EF_QUEUERUN;
80758734Seric 		e->e_errormode = EM_MAIL;
80858884Seric 		e->e_id = id;
8097876Seric # ifdef LOG
81058020Seric 		if (LogLevel > 76)
81155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8127881Seric 			       getpid());
81356795Seric # endif /* LOG */
8147763Seric 
8157763Seric 		/* don't use the headers from sendmail.cf... */
81655012Seric 		e->e_header = NULL;
8177763Seric 
81851920Seric 		/* read the queue control file -- return if locked */
81955012Seric 		if (!readqf(e))
8206980Seric 		{
82158884Seric 			if (tTd(40, 4))
82258884Seric 				printf("readqf(%s) failed\n", e->e_id);
82358884Seric 			if (forkflag)
82424941Seric 				exit(EX_OK);
82524941Seric 			else
82624941Seric 				return;
8276980Seric 		}
8286980Seric 
82955012Seric 		e->e_flags |= EF_INQUEUE;
83058929Seric 		eatheader(e, requeueflag);
8316980Seric 
83258924Seric 		if (requeueflag)
83358924Seric 			queueup(e, TRUE, FALSE);
83458924Seric 
8356980Seric 		/* do the delivery */
83658915Seric 		sendall(e, SM_DELIVER);
8376980Seric 
8386980Seric 		/* finish up and exit */
83958884Seric 		if (forkflag)
84024941Seric 			finis();
84124941Seric 		else
84255012Seric 			dropenvelope(e);
8434632Seric 	}
84424941Seric 	else
84524941Seric 	{
84624941Seric 		/*
84724941Seric 		**  Parent -- pick up results.
84824941Seric 		*/
8494632Seric 
85024941Seric 		errno = 0;
85124941Seric 		(void) waitfor(i);
85224941Seric 	}
8534632Seric }
8544632Seric /*
8554632Seric **  READQF -- read queue file and set up environment.
8564632Seric **
8574632Seric **	Parameters:
8589377Seric **		e -- the envelope of the job to run.
8594632Seric **
8604632Seric **	Returns:
86151920Seric **		TRUE if it successfully read the queue file.
86251920Seric **		FALSE otherwise.
8634632Seric **
8644632Seric **	Side Effects:
86551920Seric **		The queue file is returned locked.
8664632Seric */
8674632Seric 
86851920Seric bool
86951920Seric readqf(e)
8709377Seric 	register ENVELOPE *e;
8714632Seric {
87217477Seric 	register FILE *qfp;
87354974Seric 	ADDRESS *ctladdr;
87456400Seric 	struct stat st;
87557135Seric 	char *bp;
87658915Seric 	char qf[20];
87757135Seric 	char buf[MAXLINE];
8789348Seric 	extern char *fgetfolded();
87924954Seric 	extern long atol();
88054974Seric 	extern ADDRESS *setctluser();
88158689Seric 	extern bool lockfile();
8824632Seric 
8834632Seric 	/*
88417468Seric 	**  Read and process the file.
8854632Seric 	*/
8864632Seric 
88758915Seric 	strcpy(qf, queuename(e, 'q'));
88851937Seric 	qfp = fopen(qf, "r+");
88917477Seric 	if (qfp == NULL)
89017477Seric 	{
89158884Seric 		if (tTd(40, 8))
89258884Seric 			printf("readqf(%s): fopen failure (%s)\n",
89358884Seric 				qf, errstring(errno));
89440934Srick 		if (errno != ENOENT)
89540934Srick 			syserr("readqf: no control file %s", qf);
89651920Seric 		return FALSE;
89717477Seric 	}
89840934Srick 
89956400Seric 	/*
90056400Seric 	**  Check the queue file for plausibility to avoid attacks.
90156400Seric 	*/
90256400Seric 
90356400Seric 	if (fstat(fileno(qfp), &st) < 0)
90456400Seric 	{
90556400Seric 		/* must have been being processed by someone else */
90658884Seric 		if (tTd(40, 8))
90758884Seric 			printf("readqf(%s): fstat failure (%s)\n",
90858884Seric 				qf, errstring(errno));
90956400Seric 		fclose(qfp);
91056400Seric 		return FALSE;
91156400Seric 	}
91256400Seric 
91357135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
91456400Seric 	{
91556400Seric # ifdef LOG
91656400Seric 		if (LogLevel > 0)
91756400Seric 		{
91856400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
91956400Seric 				e->e_id, st.st_uid, st.st_mode);
92056400Seric 		}
92156795Seric # endif /* LOG */
92258884Seric 		if (tTd(40, 8))
92358884Seric 			printf("readqf(%s): bogus file\n", qf);
92456400Seric 		fclose(qfp);
92556400Seric 		return FALSE;
92656400Seric 	}
92756400Seric 
92858689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
92940934Srick 	{
93058689Seric 		/* being processed by another queuer */
93158884Seric 		if (tTd(40, 8))
93258884Seric 			printf("readqf(%s): locked\n", qf);
93358689Seric 		if (Verbose)
93458689Seric 			printf("%s: locked\n", e->e_id);
93551920Seric # ifdef LOG
93658689Seric 		if (LogLevel > 19)
93758689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
93856795Seric # endif /* LOG */
93940934Srick 		(void) fclose(qfp);
94051920Seric 		return FALSE;
94140934Srick 	}
94240934Srick 
94359101Seric 	if (st.st_size == 0)
94459101Seric 	{
94559101Seric 		/* must be a bogus file -- just remove it */
94659101Seric 		(void) unlink(qf);
94759101Seric 		fclose(qfp);
94859101Seric 		return FALSE;
94959101Seric 	}
95059101Seric 
95151920Seric 	/* save this lock */
95251920Seric 	e->e_lockfp = qfp;
95351920Seric 
95440934Srick 	/* do basic system initialization */
95555012Seric 	initsys(e);
95640934Srick 
95717477Seric 	FileName = qf;
9589377Seric 	LineNumber = 0;
95951920Seric 	if (Verbose)
9609377Seric 		printf("\nRunning %s\n", e->e_id);
96154974Seric 	ctladdr = NULL;
96257135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9634632Seric 	{
96458737Seric 		register char *p;
96557529Seric 		struct stat st;
96657529Seric 
96726504Seric 		if (tTd(40, 4))
96857135Seric 			printf("+++++ %s\n", bp);
96957135Seric 		switch (bp[0])
9704632Seric 		{
97140973Sbostic 		  case 'C':		/* specify controlling user */
97257135Seric 			ctladdr = setctluser(&bp[1]);
97340973Sbostic 			break;
97440973Sbostic 
9754632Seric 		  case 'R':		/* specify recipient */
97658082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9774632Seric 			break;
9784632Seric 
97925687Seric 		  case 'E':		/* specify error recipient */
98058082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
98125687Seric 			break;
98225687Seric 
9834632Seric 		  case 'H':		/* header */
98457135Seric 			(void) chompheader(&bp[1], FALSE, e);
9854632Seric 			break;
9864632Seric 
98710108Seric 		  case 'M':		/* message */
98857135Seric 			e->e_message = newstr(&bp[1]);
98910108Seric 			break;
99010108Seric 
9914632Seric 		  case 'S':		/* sender */
99258704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9934632Seric 			break;
9944632Seric 
99559093Seric 		  case 'B':		/* body type */
99659093Seric 			e->e_bodytype = newstr(&bp[1]);
99759093Seric 			break;
99859093Seric 
9994632Seric 		  case 'D':		/* data file name */
100057135Seric 			e->e_df = newstr(&bp[1]);
10019544Seric 			e->e_dfp = fopen(e->e_df, "r");
10029544Seric 			if (e->e_dfp == NULL)
100358020Seric 			{
10049377Seric 				syserr("readqf: cannot open %s", e->e_df);
100558020Seric 				e->e_msgsize = -1;
100658020Seric 			}
100758020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
100857529Seric 				e->e_msgsize = st.st_size;
10094632Seric 			break;
10104632Seric 
10117860Seric 		  case 'T':		/* init time */
101257135Seric 			e->e_ctime = atol(&bp[1]);
10134632Seric 			break;
10144632Seric 
10154634Seric 		  case 'P':		/* message priority */
101657135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10174634Seric 			break;
10184634Seric 
101958737Seric 		  case 'F':		/* flag bits */
102058737Seric 			for (p = &bp[1]; *p != '\0'; p++)
102158737Seric 			{
102258737Seric 				switch (*p)
102358737Seric 				{
102458737Seric 				  case 'w':	/* warning sent */
102558737Seric 					e->e_flags |= EF_WARNING;
102658737Seric 					break;
102758737Seric 
102858737Seric 				  case 'r':	/* response */
102958737Seric 					e->e_flags |= EF_RESPONSE;
103058737Seric 					break;
103158737Seric 				}
103258737Seric 			}
103358737Seric 			break;
103458737Seric 
103553400Seric 		  case '$':		/* define macro */
103657135Seric 			define(bp[1], newstr(&bp[2]), e);
103753400Seric 			break;
103853400Seric 
103924941Seric 		  case '\0':		/* blank line; ignore */
104024941Seric 			break;
104124941Seric 
10424632Seric 		  default:
104324941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
104457135Seric 				LineNumber, bp);
10454632Seric 			break;
10464632Seric 		}
104757135Seric 
104857135Seric 		if (bp != buf)
104957135Seric 			free(bp);
10504632Seric 	}
10519377Seric 
10529377Seric 	FileName = NULL;
105324941Seric 
105424941Seric 	/*
105524941Seric 	**  If we haven't read any lines, this queue file is empty.
105624941Seric 	**  Arrange to remove it without referencing any null pointers.
105724941Seric 	*/
105824941Seric 
105924941Seric 	if (LineNumber == 0)
106024941Seric 	{
106124941Seric 		errno = 0;
106224941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
106324941Seric 	}
106451920Seric 	return TRUE;
10654632Seric }
10664632Seric /*
10679630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10689630Seric **
10699630Seric **	Parameters:
10709630Seric **		none.
10719630Seric **
10729630Seric **	Returns:
10739630Seric **		none.
10749630Seric **
10759630Seric **	Side Effects:
10769630Seric **		Prints a listing of the mail queue on the standard output.
10779630Seric */
10785182Seric 
10799630Seric printqueue()
10809630Seric {
10819630Seric 	register WORK *w;
10829630Seric 	FILE *f;
108310070Seric 	int nrequests;
10849630Seric 	char buf[MAXLINE];
10859630Seric 
10869630Seric 	/*
108758250Seric 	**  Check for permission to print the queue
108858250Seric 	*/
108958250Seric 
109058523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
109158250Seric 	{
109258250Seric 		struct stat st;
109358523Seric # ifdef NGROUPS
109458523Seric 		int n;
109558523Seric 		int gidset[NGROUPS];
109658523Seric # endif
109758250Seric 
109858517Seric 		if (stat(QueueDir, &st) < 0)
109958250Seric 		{
110058250Seric 			syserr("Cannot stat %s", QueueDir);
110158250Seric 			return;
110258250Seric 		}
110358523Seric # ifdef NGROUPS
110458523Seric 		n = getgroups(NGROUPS, gidset);
110558523Seric 		while (--n >= 0)
110658523Seric 		{
110758523Seric 			if (gidset[n] == st.st_gid)
110858523Seric 				break;
110958523Seric 		}
111058523Seric 		if (n < 0)
111158523Seric # else
111258250Seric 		if (getgid() != st.st_gid)
111358523Seric # endif
111458250Seric 		{
111558250Seric 			usrerr("510 You are not permitted to see the queue");
111658250Seric 			setstat(EX_NOPERM);
111758250Seric 			return;
111858250Seric 		}
111958250Seric 	}
112058250Seric 
112158250Seric 	/*
11229630Seric 	**  Read and order the queue.
11239630Seric 	*/
11249630Seric 
112524941Seric 	nrequests = orderq(TRUE);
11269630Seric 
11279630Seric 	/*
11289630Seric 	**  Print the work list that we have read.
11299630Seric 	*/
11309630Seric 
11319630Seric 	/* first see if there is anything */
113210070Seric 	if (nrequests <= 0)
11339630Seric 	{
113410070Seric 		printf("Mail queue is empty\n");
11359630Seric 		return;
11369630Seric 	}
11379630Seric 
113851920Seric 	CurrentLA = getla();	/* get load average */
113940934Srick 
114010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
114125687Seric 	if (nrequests > QUEUESIZE)
114225687Seric 		printf(", only %d printed", QUEUESIZE);
114324979Seric 	if (Verbose)
114458716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
114524979Seric 	else
114658716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11479630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11489630Seric 	{
11499630Seric 		struct stat st;
115010070Seric 		auto time_t submittime = 0;
115110070Seric 		long dfsize = -1;
115258737Seric 		int flags = 0;
115310108Seric 		char message[MAXLINE];
115459093Seric 		char bodytype[MAXNAME];
115524941Seric 		extern bool shouldqueue();
115658689Seric 		extern bool lockfile();
11579630Seric 
115817468Seric 		f = fopen(w->w_name, "r");
115917468Seric 		if (f == NULL)
116017468Seric 		{
116117468Seric 			errno = 0;
116217468Seric 			continue;
116317468Seric 		}
116458724Seric 		printf("%8s", w->w_name + 2);
116558689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
116610070Seric 			printf("*");
116757438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
116824941Seric 			printf("X");
116910070Seric 		else
117010070Seric 			printf(" ");
117110070Seric 		errno = 0;
117217468Seric 
117359093Seric 		message[0] = bodytype[0] = '\0';
11749630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11759630Seric 		{
117653400Seric 			register int i;
117758737Seric 			register char *p;
117853400Seric 
11799630Seric 			fixcrlf(buf, TRUE);
11809630Seric 			switch (buf[0])
11819630Seric 			{
118210108Seric 			  case 'M':	/* error message */
118353400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
118458737Seric 					i = sizeof message - 1;
118553400Seric 				bcopy(&buf[1], message, i);
118653400Seric 				message[i] = '\0';
118710108Seric 				break;
118810108Seric 
118959093Seric 			  case 'B':	/* body type */
119059093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
119159093Seric 					i = sizeof bodytype - 1;
119259093Seric 				bcopy(&buf[1], bodytype, i);
119359093Seric 				bodytype[i] = '\0';
119459093Seric 				break;
119559093Seric 
11969630Seric 			  case 'S':	/* sender name */
119724979Seric 				if (Verbose)
119858737Seric 					printf("%8ld %10ld%c%.12s %.38s",
119958737Seric 					    dfsize,
120058737Seric 					    w->w_pri,
120158737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
120258737Seric 					    ctime(&submittime) + 4,
120324979Seric 					    &buf[1]);
120424979Seric 				else
120524979Seric 					printf("%8ld %.16s %.45s", dfsize,
120624979Seric 					    ctime(&submittime), &buf[1]);
120759093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
120859093Seric 				{
120959093Seric 					printf("\n    %10.10s", bodytype);
121059093Seric 					if (message[0] != '\0')
121159093Seric 						printf("   (%.60s)", message);
121259093Seric 				}
12139630Seric 				break;
121451920Seric 
121540973Sbostic 			  case 'C':	/* controlling user */
121654974Seric 				if (Verbose)
121758716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
121858716Seric 						&buf[1]);
121940973Sbostic 				break;
12209630Seric 
12219630Seric 			  case 'R':	/* recipient name */
122224979Seric 				if (Verbose)
122358716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
122424979Seric 				else
122558716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12269630Seric 				break;
12279630Seric 
12289630Seric 			  case 'T':	/* creation time */
122924941Seric 				submittime = atol(&buf[1]);
12309630Seric 				break;
123110070Seric 
123210070Seric 			  case 'D':	/* data file name */
123310070Seric 				if (stat(&buf[1], &st) >= 0)
123410070Seric 					dfsize = st.st_size;
123510070Seric 				break;
123658737Seric 
123758737Seric 			  case 'F':	/* flag bits */
123858737Seric 				for (p = &buf[1]; *p != '\0'; p++)
123958737Seric 				{
124058737Seric 					switch (*p)
124158737Seric 					{
124258737Seric 					  case 'w':
124358737Seric 						flags |= EF_WARNING;
124458737Seric 						break;
124558737Seric 					}
124658737Seric 				}
12479630Seric 			}
12489630Seric 		}
124910070Seric 		if (submittime == (time_t) 0)
125010070Seric 			printf(" (no control file)");
12519630Seric 		printf("\n");
125223098Seric 		(void) fclose(f);
12539630Seric 	}
12549630Seric }
12559630Seric 
125656795Seric # endif /* QUEUE */
125717468Seric /*
125817468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
125917468Seric **
126017468Seric **	Assigns an id code if one does not already exist.
126117468Seric **	This code is very careful to avoid trashing existing files
126217468Seric **	under any circumstances.
126317468Seric **
126417468Seric **	Parameters:
126517468Seric **		e -- envelope to build it in/from.
126617468Seric **		type -- the file type, used as the first character
126717468Seric **			of the file name.
126817468Seric **
126917468Seric **	Returns:
127017468Seric **		a pointer to the new file name (in a static buffer).
127117468Seric **
127217468Seric **	Side Effects:
127351920Seric **		If no id code is already assigned, queuename will
127451920Seric **		assign an id code, create a qf file, and leave a
127551920Seric **		locked, open-for-write file pointer in the envelope.
127617468Seric */
127717468Seric 
127817468Seric char *
127917468Seric queuename(e, type)
128017468Seric 	register ENVELOPE *e;
128117468Seric 	char type;
128217468Seric {
128317468Seric 	static int pid = -1;
128458741Seric 	static char c0;
128558741Seric 	static char c1;
128658741Seric 	static char c2;
128758716Seric 	time_t now;
128858716Seric 	struct tm *tm;
128958689Seric 	static char buf[MAXNAME];
129058689Seric 	extern bool lockfile();
129117468Seric 
129217468Seric 	if (e->e_id == NULL)
129317468Seric 	{
129417468Seric 		char qf[20];
129517468Seric 
129617468Seric 		/* find a unique id */
129717468Seric 		if (pid != getpid())
129817468Seric 		{
129917468Seric 			/* new process -- start back at "AA" */
130017468Seric 			pid = getpid();
130158716Seric 			now = curtime();
130258716Seric 			tm = localtime(&now);
130358716Seric 			c0 = 'A' + tm->tm_hour;
130417468Seric 			c1 = 'A';
130517468Seric 			c2 = 'A' - 1;
130617468Seric 		}
130758716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
130817468Seric 
130917468Seric 		while (c1 < '~' || c2 < 'Z')
131017468Seric 		{
131117468Seric 			int i;
131217468Seric 
131317468Seric 			if (c2 >= 'Z')
131417468Seric 			{
131517468Seric 				c1++;
131617468Seric 				c2 = 'A' - 1;
131717468Seric 			}
131858716Seric 			qf[3] = c1;
131958716Seric 			qf[4] = ++c2;
132017468Seric 			if (tTd(7, 20))
132140934Srick 				printf("queuename: trying \"%s\"\n", qf);
132217468Seric 
132340934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
132451920Seric 			if (i < 0)
132551920Seric 			{
132651920Seric 				if (errno == EEXIST)
132751920Seric 					continue;
132851920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
132951920Seric 					qf, QueueDir);
133051920Seric 				exit(EX_UNAVAILABLE);
133151920Seric 			}
133258689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
133351920Seric 			{
133451920Seric 				e->e_lockfp = fdopen(i, "w");
133540934Srick 				break;
133617468Seric 			}
133751920Seric 
133851920Seric 			/* a reader got the file; abandon it and try again */
133951920Seric 			(void) close(i);
134017468Seric 		}
134117468Seric 		if (c1 >= '~' && c2 >= 'Z')
134217468Seric 		{
134317468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
134417468Seric 				qf, QueueDir);
134517468Seric 			exit(EX_OSERR);
134617468Seric 		}
134717468Seric 		e->e_id = newstr(&qf[2]);
134817468Seric 		define('i', e->e_id, e);
134917468Seric 		if (tTd(7, 1))
135017468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
135117468Seric # ifdef LOG
135258020Seric 		if (LogLevel > 93)
135317468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
135456795Seric # endif /* LOG */
135517468Seric 	}
135617468Seric 
135717468Seric 	if (type == '\0')
135817468Seric 		return (NULL);
135917468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
136017468Seric 	if (tTd(7, 2))
136117468Seric 		printf("queuename: %s\n", buf);
136217468Seric 	return (buf);
136317468Seric }
136417468Seric /*
136517468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
136617468Seric **
136717468Seric **	Parameters:
136817468Seric **		e -- the envelope to unlock.
136917468Seric **
137017468Seric **	Returns:
137117468Seric **		none
137217468Seric **
137317468Seric **	Side Effects:
137417468Seric **		unlocks the queue for `e'.
137517468Seric */
137617468Seric 
137717468Seric unlockqueue(e)
137817468Seric 	ENVELOPE *e;
137917468Seric {
138058680Seric 	if (tTd(51, 4))
138158680Seric 		printf("unlockqueue(%s)\n", e->e_id);
138258680Seric 
138351920Seric 	/* if there is a lock file in the envelope, close it */
138451920Seric 	if (e->e_lockfp != NULL)
138558680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
138651920Seric 	e->e_lockfp = NULL;
138751920Seric 
138858728Seric 	/* don't create a queue id if we don't already have one */
138958728Seric 	if (e->e_id == NULL)
139058728Seric 		return;
139158728Seric 
139217468Seric 	/* remove the transcript */
139317468Seric # ifdef LOG
139458020Seric 	if (LogLevel > 87)
139517468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
139656795Seric # endif /* LOG */
139758680Seric 	if (!tTd(51, 104))
139817468Seric 		xunlink(queuename(e, 'x'));
139917468Seric 
140017468Seric }
140140973Sbostic /*
140254974Seric **  SETCTLUSER -- create a controlling address
140340973Sbostic **
140454974Seric **	Create a fake "address" given only a local login name; this is
140554974Seric **	used as a "controlling user" for future recipient addresses.
140640973Sbostic **
140740973Sbostic **	Parameters:
140854974Seric **		user -- the user name of the controlling user.
140940973Sbostic **
141040973Sbostic **	Returns:
141154974Seric **		An address descriptor for the controlling user.
141240973Sbostic **
141340973Sbostic **	Side Effects:
141440973Sbostic **		none.
141540973Sbostic */
141640973Sbostic 
141754974Seric ADDRESS *
141854974Seric setctluser(user)
141954974Seric 	char *user;
142040973Sbostic {
142154974Seric 	register ADDRESS *a;
142240973Sbostic 	struct passwd *pw;
142359113Seric 	char *p;
142440973Sbostic 
142540973Sbostic 	/*
142654974Seric 	**  See if this clears our concept of controlling user.
142740973Sbostic 	*/
142840973Sbostic 
142959270Seric 	if (user == NULL)
143059270Seric 		user = "";
143140973Sbostic 
143240973Sbostic 	/*
143354974Seric 	**  Set up addr fields for controlling user.
143440973Sbostic 	*/
143540973Sbostic 
143654974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
143754974Seric 	bzero((char *) a, sizeof *a);
143859113Seric 
143959113Seric 	p = strchr(user, ':');
144059113Seric 	if (p != NULL)
144159113Seric 		*p++ = '\0';
144259270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
144340973Sbostic 	{
144440973Sbostic 		a->q_home = newstr(pw->pw_dir);
144540973Sbostic 		a->q_uid = pw->pw_uid;
144640973Sbostic 		a->q_gid = pw->pw_gid;
144757642Seric 		a->q_user = newstr(user);
144859270Seric 		a->q_flags |= QGOODUID;
144940973Sbostic 	}
145040973Sbostic 	else
145140973Sbostic 	{
145257642Seric 		a->q_user = newstr(DefUser);
145340973Sbostic 	}
145440973Sbostic 
145559270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
145656328Seric 	a->q_mailer = LocalMailer;
145759113Seric 	if (p == NULL)
145859113Seric 		a->q_paddr = a->q_user;
145959113Seric 	else
146059113Seric 		a->q_paddr = newstr(p);
146154974Seric 	return a;
146240973Sbostic }
1463