xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58915)
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*58915Seric static char sccsid[] = "@(#)queue.c	6.42 (Berkeley) 04/01/93 (with queueing)";
1433731Sbostic #else
15*58915Seric static char sccsid[] = "@(#)queue.c	6.42 (Berkeley) 04/01/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;
7654974Seric 	ADDRESS *lastctladdr;
7751920Seric 	char buf[MAXLINE], tf[MAXLINE];
7853400Seric 	extern char *macvalue();
7954974Seric 	extern ADDRESS *getctladdr();
804632Seric 
815037Seric 	/*
8217477Seric 	**  Create control file.
835037Seric 	*/
844632Seric 
8551920Seric 	newid = (e->e_id == NULL);
8651920Seric 	strcpy(tf, queuename(e, 't'));
8751920Seric 	tfp = e->e_lockfp;
8851920Seric 	if (tfp == NULL)
8951920Seric 		newid = FALSE;
9051920Seric 	if (newid)
9151835Seric 	{
9251920Seric 		tfp = e->e_lockfp;
9351920Seric 	}
9451920Seric 	else
9551920Seric 	{
9651920Seric 		/* get a locked tf file */
9751920Seric 		for (i = 100; --i >= 0; )
9851835Seric 		{
9958689Seric 			extern bool lockfile();
10051937Seric 
10151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10251920Seric 			if (fd < 0)
10351835Seric 			{
10451920Seric 				if (errno == EEXIST)
10551920Seric 					continue;
10658801Seric notemp:
10758690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10841636Srick 			}
10958689Seric 
11058689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
11151920Seric 				break;
11258689Seric 
11351920Seric 			close(fd);
11458801Seric 			sleep(i);
11541636Srick 		}
11658801Seric 		if (fd < 0)
11758801Seric 			goto notemp;
11841636Srick 
11951920Seric 		tfp = fdopen(fd, "w");
12051920Seric 	}
1214632Seric 
1227677Seric 	if (tTd(40, 1))
12317468Seric 		printf("queueing %s\n", e->e_id);
1244632Seric 
1254632Seric 	/*
1266980Seric 	**  If there is no data file yet, create one.
1276980Seric 	*/
1286980Seric 
1296980Seric 	if (e->e_df == NULL)
1306980Seric 	{
1316980Seric 		register FILE *dfp;
1329389Seric 		extern putbody();
1336980Seric 
1347812Seric 		e->e_df = newstr(queuename(e, 'd'));
13540934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13640934Srick 		if (fd < 0)
13758690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13840934Srick 		dfp = fdopen(fd, "w");
13910173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
14058680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1419389Seric 		e->e_putbody = putbody;
1426980Seric 	}
1436980Seric 
1446980Seric 	/*
1454632Seric 	**  Output future work requests.
14625687Seric 	**	Priority and creation time should be first, since
14725687Seric 	**	they are required by orderq.
1484632Seric 	*/
1494632Seric 
1509377Seric 	/* output message priority */
1519377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1529377Seric 
1539630Seric 	/* output creation time */
1549630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1559630Seric 
1564632Seric 	/* output name of data file */
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 
17353400Seric 	/* $r and $s 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);
17853400Seric 
1794632Seric 	/* output name of sender */
1807812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1814632Seric 
18255360Seric 	/* output list of error recipients */
18355360Seric 	lastctladdr = NULL;
18455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18555360Seric 	{
18658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18755360Seric 		{
18855360Seric 			ADDRESS *ctladdr;
18955360Seric 
19055360Seric 			ctladdr = getctladdr(q);
19155360Seric 			if (ctladdr != lastctladdr)
19255360Seric 			{
19355360Seric 				printctladdr(ctladdr, tfp);
19455360Seric 				lastctladdr = ctladdr;
19555360Seric 			}
19655360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
19755360Seric 		}
19855360Seric 	}
19955360Seric 
2004632Seric 	/* output list of recipient addresses */
2016980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2024632Seric 	{
20358250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
20458680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2058245Seric 		{
20654974Seric 			ADDRESS *ctladdr;
20740973Sbostic 
20854975Seric 			ctladdr = getctladdr(q);
20954975Seric 			if (ctladdr != lastctladdr)
21054974Seric 			{
21154974Seric 				printctladdr(ctladdr, tfp);
21254974Seric 				lastctladdr = ctladdr;
21354974Seric 			}
2147812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2159377Seric 			if (announce)
2169377Seric 			{
2179377Seric 				e->e_to = q->q_paddr;
21858151Seric 				message("queued");
21958020Seric 				if (LogLevel > 8)
22058337Seric 					logdelivery(NULL, NULL, "queued", e);
2219377Seric 				e->e_to = NULL;
2229377Seric 			}
2239387Seric 			if (tTd(40, 1))
2249387Seric 			{
2259387Seric 				printf("queueing ");
2269387Seric 				printaddr(q, FALSE);
2279387Seric 			}
2288245Seric 		}
2294632Seric 	}
2304632Seric 
2319377Seric 	/*
2329377Seric 	**  Output headers for this message.
2339377Seric 	**	Expand macros completely here.  Queue run will deal with
2349377Seric 	**	everything as absolute headers.
2359377Seric 	**		All headers that must be relative to the recipient
2369377Seric 	**		can be cracked later.
23710173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23810173Seric 	**	no effect on the addresses as they are output.
2399377Seric 	*/
2409377Seric 
24110686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24258020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24358020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24410349Seric 	nullmailer.m_eol = "\n";
24510173Seric 
24658050Seric 	define('g', "\201f", e);
2476980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2484632Seric 	{
24910686Seric 		extern bool bitzerop();
25010686Seric 
25112015Seric 		/* don't output null headers */
2524632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2534632Seric 			continue;
25412015Seric 
25512015Seric 		/* don't output resent headers on non-resent messages */
25612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25712015Seric 			continue;
25812015Seric 
25912015Seric 		/* output this header */
2607812Seric 		fprintf(tfp, "H");
26112015Seric 
26212015Seric 		/* if conditional, output the set of conditions */
26310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26410686Seric 		{
26510686Seric 			int j;
26610686Seric 
26723098Seric 			(void) putc('?', tfp);
26810686Seric 			for (j = '\0'; j <= '\177'; j++)
26910686Seric 				if (bitnset(j, h->h_mflags))
27023098Seric 					(void) putc(j, tfp);
27123098Seric 			(void) putc('?', tfp);
27210686Seric 		}
27312015Seric 
27412015Seric 		/* output the header: expand macros, convert addresses */
2757763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2767763Seric 		{
2777763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2788236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2797763Seric 		}
2808245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2819348Seric 		{
28258737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28358737Seric 
28458737Seric 			if (bitset(H_FROM, h->h_flags))
28558737Seric 				oldstyle = FALSE;
28658737Seric 
28758737Seric 			commaize(h, h->h_value, tfp, oldstyle,
28855012Seric 				 &nullmailer, e);
2899348Seric 		}
2907763Seric 		else
2918245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2924632Seric 	}
2934632Seric 
2944632Seric 	/*
2954632Seric 	**  Clean up.
2964632Seric 	*/
2974632Seric 
29858732Seric 	fflush(tfp);
29958732Seric 	if (ferror(tfp))
30058732Seric 	{
30158732Seric 		if (newid)
30258732Seric 			syserr("!552 Error writing control file %s", tf);
30358732Seric 		else
30458732Seric 			syserr("!452 Error writing control file %s", tf);
30558732Seric 	}
30658732Seric 
30751920Seric 	if (!newid)
30851920Seric 	{
30951920Seric 		qf = queuename(e, 'q');
31051920Seric 		if (rename(tf, qf) < 0)
31151920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31251920Seric 		if (e->e_lockfp != NULL)
31358680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
31451920Seric 		e->e_lockfp = tfp;
31551920Seric 	}
31651920Seric 	else
31751920Seric 		qf = tf;
31841636Srick 	errno = 0;
3197391Seric 
3207677Seric # ifdef LOG
3217677Seric 	/* save log info */
32258020Seric 	if (LogLevel > 79)
3237878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
32456795Seric # endif /* LOG */
32540934Srick 	fflush(tfp);
32651920Seric 	return;
3274632Seric }
32854974Seric 
32954974Seric printctladdr(a, tfp)
33054974Seric 	ADDRESS *a;
33154974Seric 	FILE *tfp;
33254974Seric {
33354974Seric 	char *u;
33454974Seric 	struct passwd *pw;
33554974Seric 	extern struct passwd *getpwuid();
33654974Seric 
33754974Seric 	if (a == NULL)
33854974Seric 	{
33954974Seric 		fprintf(tfp, "C\n");
34054974Seric 		return;
34154974Seric 	}
34254974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
34354974Seric 		u = DefUser;
34454974Seric 	else
34554974Seric 		u = pw->pw_name;
34654974Seric 	fprintf(tfp, "C%s\n", u);
34754974Seric }
34854974Seric 
3494632Seric /*
3504632Seric **  RUNQUEUE -- run the jobs in the queue.
3514632Seric **
3524632Seric **	Gets the stuff out of the queue in some presumably logical
3534632Seric **	order and processes them.
3544632Seric **
3554632Seric **	Parameters:
35624941Seric **		forkflag -- TRUE if the queue scanning should be done in
35724941Seric **			a child process.  We double-fork so it is not our
35824941Seric **			child and we don't have to clean up after it.
3594632Seric **
3604632Seric **	Returns:
3614632Seric **		none.
3624632Seric **
3634632Seric **	Side Effects:
3644632Seric **		runs things in the mail queue.
3654632Seric */
3664632Seric 
36755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
36855360Seric 
36955360Seric runqueue(forkflag)
3704639Seric 	bool forkflag;
3714632Seric {
37224953Seric 	extern bool shouldqueue();
37355360Seric 	register ENVELOPE *e;
37455360Seric 	extern ENVELOPE BlankEnvelope;
37555360Seric 	extern ENVELOPE *newenvelope();
37624953Seric 
3777466Seric 	/*
37824953Seric 	**  If no work will ever be selected, don't even bother reading
37924953Seric 	**  the queue.
38024953Seric 	*/
38124953Seric 
38251920Seric 	CurrentLA = getla();	/* get load average */
38340934Srick 
38458132Seric 	if (shouldqueue(0L, curtime()))
38524953Seric 	{
38624953Seric 		if (Verbose)
38724953Seric 			printf("Skipping queue run -- load average too high\n");
38858107Seric 		if (forkflag && QueueIntvl != 0)
38958107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39055360Seric 		return;
39124953Seric 	}
39224953Seric 
39324953Seric 	/*
3947466Seric 	**  See if we want to go off and do other useful work.
3957466Seric 	*/
3964639Seric 
3974639Seric 	if (forkflag)
3984639Seric 	{
3997943Seric 		int pid;
4007943Seric 
4017943Seric 		pid = dofork();
4027943Seric 		if (pid != 0)
4034639Seric 		{
40446928Sbostic 			extern void reapchild();
40525184Seric 
4067943Seric 			/* parent -- pick up intermediate zombie */
40725184Seric #ifndef SIGCHLD
4089377Seric 			(void) waitfor(pid);
40956795Seric #else /* SIGCHLD */
41025184Seric 			(void) signal(SIGCHLD, reapchild);
41156795Seric #endif /* SIGCHLD */
4127690Seric 			if (QueueIntvl != 0)
4139348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4144639Seric 			return;
4154639Seric 		}
4167943Seric 		/* child -- double fork */
41725184Seric #ifndef SIGCHLD
4187943Seric 		if (fork() != 0)
4197943Seric 			exit(EX_OK);
42056795Seric #else /* SIGCHLD */
42125184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42256795Seric #endif /* SIGCHLD */
4234639Seric 	}
42424941Seric 
42540934Srick 	setproctitle("running queue: %s", QueueDir);
42624941Seric 
4277876Seric # ifdef LOG
42858020Seric 	if (LogLevel > 69)
42955360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43055360Seric 			QueueDir, getpid(), forkflag);
43156795Seric # endif /* LOG */
4324639Seric 
4337466Seric 	/*
43410205Seric 	**  Release any resources used by the daemon code.
43510205Seric 	*/
43610205Seric 
43710205Seric # ifdef DAEMON
43810205Seric 	clrdaemon();
43956795Seric # endif /* DAEMON */
44010205Seric 
44110205Seric 	/*
44255360Seric 	**  Create ourselves an envelope
44355360Seric 	*/
44455360Seric 
44555360Seric 	CurEnv = &QueueEnvelope;
44658179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
44755360Seric 	e->e_flags = BlankEnvelope.e_flags;
44855360Seric 
44955360Seric 	/*
45027175Seric 	**  Make sure the alias database is open.
45127175Seric 	*/
45227175Seric 
45355012Seric 	initaliases(AliasFile, FALSE, e);
45427175Seric 
45527175Seric 	/*
4567466Seric 	**  Start making passes through the queue.
4577466Seric 	**	First, read and sort the entire queue.
4587466Seric 	**	Then, process the work in that order.
4597466Seric 	**		But if you take too long, start over.
4607466Seric 	*/
4617466Seric 
4627943Seric 	/* order the existing work requests */
46324954Seric 	(void) orderq(FALSE);
4647690Seric 
4657943Seric 	/* process them once at a time */
4667943Seric 	while (WorkQ != NULL)
4674639Seric 	{
4687943Seric 		WORK *w = WorkQ;
46958884Seric 		extern bool shouldqueue();
4707881Seric 
4717943Seric 		WorkQ = WorkQ->w_next;
47258884Seric 
47358884Seric 		/*
47458884Seric 		**  Ignore jobs that are too expensive for the moment.
47558884Seric 		*/
47658884Seric 
47758884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
47858884Seric 		{
47958884Seric 			if (Verbose)
48058884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
48158908Seric 			continue;
48258884Seric 		}
48358884Seric 
48458884Seric 		dowork(w->w_name + 2, ForkQueueRuns, e);
4857943Seric 		free(w->w_name);
4867943Seric 		free((char *) w);
4874639Seric 	}
48829866Seric 
48929866Seric 	/* exit without the usual cleanup */
49055467Seric 	e->e_id = NULL;
49155467Seric 	finis();
4924634Seric }
4934634Seric /*
4944632Seric **  ORDERQ -- order the work queue.
4954632Seric **
4964632Seric **	Parameters:
49724941Seric **		doall -- if set, include everything in the queue (even
49824941Seric **			the jobs that cannot be run because the load
49924941Seric **			average is too high).  Otherwise, exclude those
50024941Seric **			jobs.
5014632Seric **
5024632Seric **	Returns:
50310121Seric **		The number of request in the queue (not necessarily
50410121Seric **		the number of requests in WorkQ however).
5054632Seric **
5064632Seric **	Side Effects:
5074632Seric **		Sets WorkQ to the queue of available work, in order.
5084632Seric */
5094632Seric 
51025687Seric # define NEED_P		001
51125687Seric # define NEED_T		002
51258318Seric # define NEED_R		004
51358318Seric # define NEED_S		010
5144632Seric 
51524941Seric orderq(doall)
51624941Seric 	bool doall;
5174632Seric {
5186625Sglickman 	register struct direct *d;
5194632Seric 	register WORK *w;
5206625Sglickman 	DIR *f;
5214632Seric 	register int i;
52225687Seric 	WORK wlist[QUEUESIZE+1];
52310070Seric 	int wn = -1;
5244632Seric 	extern workcmpf();
5254632Seric 
52658318Seric 	if (tTd(41, 1))
52758318Seric 	{
52858318Seric 		printf("orderq:\n");
52958318Seric 		if (QueueLimitId != NULL)
53058318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
53158318Seric 		if (QueueLimitSender != NULL)
53258318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
53358318Seric 		if (QueueLimitRecipient != NULL)
53458318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
53558318Seric 	}
53658318Seric 
5374632Seric 	/* clear out old WorkQ */
5384632Seric 	for (w = WorkQ; w != NULL; )
5394632Seric 	{
5404632Seric 		register WORK *nw = w->w_next;
5414632Seric 
5424632Seric 		WorkQ = nw;
5434632Seric 		free(w->w_name);
5444632Seric 		free((char *) w);
5454632Seric 		w = nw;
5464632Seric 	}
5474632Seric 
5484632Seric 	/* open the queue directory */
5498148Seric 	f = opendir(".");
5504632Seric 	if (f == NULL)
5514632Seric 	{
5528148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
55310070Seric 		return (0);
5544632Seric 	}
5554632Seric 
5564632Seric 	/*
5574632Seric 	**  Read the work directory.
5584632Seric 	*/
5594632Seric 
56010070Seric 	while ((d = readdir(f)) != NULL)
5614632Seric 	{
5629377Seric 		FILE *cf;
5634632Seric 		char lbuf[MAXNAME];
56457642Seric 		extern bool shouldqueue();
56558318Seric 		extern bool strcontainedin();
5664632Seric 
5674632Seric 		/* is this an interesting entry? */
5687812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5694632Seric 			continue;
5704632Seric 
57158318Seric 		if (QueueLimitId != NULL &&
57258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
57358318Seric 			continue;
57458318Seric 
57558722Seric 		/*
57658722Seric 		**  Check queue name for plausibility.  This handles
57758722Seric 		**  both old and new type ids.
57858722Seric 		*/
57958722Seric 
58058722Seric 		i = strlen(d->d_name);
58158722Seric 		if (i != 9 && i != 10)
58258020Seric 		{
58358020Seric 			if (Verbose)
58458020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
58558020Seric #ifdef LOG
58658020Seric 			if (LogLevel > 3)
58758020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
58858020Seric 					d->d_name);
58958020Seric #endif
59058020Seric 			if (strlen(d->d_name) >= MAXNAME)
59158020Seric 				d->d_name[MAXNAME - 1] = '\0';
59258020Seric 			strcpy(lbuf, d->d_name);
59358020Seric 			lbuf[0] = 'Q';
59458020Seric 			(void) rename(d->d_name, lbuf);
59558020Seric 			continue;
59658020Seric 		}
59758020Seric 
59810070Seric 		/* yes -- open control file (if not too many files) */
59925687Seric 		if (++wn >= QUEUESIZE)
60010070Seric 			continue;
60158318Seric 
6028148Seric 		cf = fopen(d->d_name, "r");
6034632Seric 		if (cf == NULL)
6044632Seric 		{
6057055Seric 			/* this may be some random person sending hir msgs */
6067055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
60710090Seric 			if (tTd(41, 2))
60810090Seric 				printf("orderq: cannot open %s (%d)\n",
60910090Seric 					d->d_name, errno);
6107055Seric 			errno = 0;
61110090Seric 			wn--;
6124632Seric 			continue;
6134632Seric 		}
61425687Seric 		w = &wlist[wn];
61525687Seric 		w->w_name = newstr(d->d_name);
6164632Seric 
61725027Seric 		/* make sure jobs in creation don't clog queue */
61825687Seric 		w->w_pri = 0x7fffffff;
61925687Seric 		w->w_ctime = 0;
62025027Seric 
6214632Seric 		/* extract useful information */
62225687Seric 		i = NEED_P | NEED_T;
62358318Seric 		if (QueueLimitSender != NULL)
62458318Seric 			i |= NEED_S;
62558318Seric 		if (QueueLimitRecipient != NULL)
62658318Seric 			i |= NEED_R;
62725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6284632Seric 		{
62924954Seric 			extern long atol();
63058318Seric 			extern bool strcontainedin();
63124954Seric 
63224941Seric 			switch (lbuf[0])
6334632Seric 			{
63424941Seric 			  case 'P':
63525687Seric 				w->w_pri = atol(&lbuf[1]);
63625687Seric 				i &= ~NEED_P;
6374632Seric 				break;
63825013Seric 
63925013Seric 			  case 'T':
64025687Seric 				w->w_ctime = atol(&lbuf[1]);
64125687Seric 				i &= ~NEED_T;
64225013Seric 				break;
64358318Seric 
64458318Seric 			  case 'R':
64558318Seric 				if (QueueLimitRecipient != NULL &&
64658318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
64758318Seric 					i &= ~NEED_R;
64858318Seric 				break;
64958318Seric 
65058318Seric 			  case 'S':
65158318Seric 				if (QueueLimitSender != NULL &&
65258318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
65358318Seric 					i &= ~NEED_S;
65458318Seric 				break;
6554632Seric 			}
6564632Seric 		}
6574632Seric 		(void) fclose(cf);
65824953Seric 
65958318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
66058318Seric 		    bitset(NEED_R|NEED_S, i))
66124953Seric 		{
66224953Seric 			/* don't even bother sorting this job in */
66324953Seric 			wn--;
66424953Seric 		}
6654632Seric 	}
6666625Sglickman 	(void) closedir(f);
66710090Seric 	wn++;
6684632Seric 
6694632Seric 	/*
6704632Seric 	**  Sort the work directory.
6714632Seric 	*/
6724632Seric 
67325687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6744632Seric 
6754632Seric 	/*
6764632Seric 	**  Convert the work list into canonical form.
6779377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6784632Seric 	*/
6794632Seric 
68024981Seric 	WorkQ = NULL;
68125687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6824632Seric 	{
6834632Seric 		w = (WORK *) xalloc(sizeof *w);
6844632Seric 		w->w_name = wlist[i].w_name;
6854632Seric 		w->w_pri = wlist[i].w_pri;
68625013Seric 		w->w_ctime = wlist[i].w_ctime;
68724981Seric 		w->w_next = WorkQ;
68824981Seric 		WorkQ = w;
6894632Seric 	}
6904632Seric 
6917677Seric 	if (tTd(40, 1))
6924632Seric 	{
6934632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6945037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6954632Seric 	}
69610070Seric 
69710090Seric 	return (wn);
6984632Seric }
6994632Seric /*
7007677Seric **  WORKCMPF -- compare function for ordering work.
7014632Seric **
7024632Seric **	Parameters:
7034632Seric **		a -- the first argument.
7044632Seric **		b -- the second argument.
7054632Seric **
7064632Seric **	Returns:
70724981Seric **		-1 if a < b
70824981Seric **		 0 if a == b
70924981Seric **		+1 if a > b
7104632Seric **
7114632Seric **	Side Effects:
7124632Seric **		none.
7134632Seric */
7144632Seric 
7154632Seric workcmpf(a, b)
7165037Seric 	register WORK *a;
7175037Seric 	register WORK *b;
7184632Seric {
71957438Seric 	long pa = a->w_pri;
72057438Seric 	long pb = b->w_pri;
72124941Seric 
72224941Seric 	if (pa == pb)
7234632Seric 		return (0);
72424941Seric 	else if (pa > pb)
72524981Seric 		return (1);
72624981Seric 	else
72710121Seric 		return (-1);
7284632Seric }
7294632Seric /*
7304632Seric **  DOWORK -- do a work request.
7314632Seric **
7324632Seric **	Parameters:
73358884Seric **		id -- the ID of the job to run.
73458884Seric **		forkflag -- if set, run this in background.
73558884Seric **		e - the envelope in which to run it.
7364632Seric **
7374632Seric **	Returns:
7384632Seric **		none.
7394632Seric **
7404632Seric **	Side Effects:
7414632Seric **		The work request is satisfied if possible.
7424632Seric */
7434632Seric 
74458884Seric dowork(id, forkflag, e)
74558884Seric 	char *id;
74658884Seric 	bool forkflag;
74755012Seric 	register ENVELOPE *e;
7484632Seric {
7494632Seric 	register int i;
75051920Seric 	extern bool readqf();
7514632Seric 
7527677Seric 	if (tTd(40, 1))
75358884Seric 		printf("dowork(%s)\n", id);
7544632Seric 
7554632Seric 	/*
75624941Seric 	**  Fork for work.
75724941Seric 	*/
75824941Seric 
75958884Seric 	if (forkflag)
76024941Seric 	{
76124941Seric 		i = fork();
76224941Seric 		if (i < 0)
76324941Seric 		{
76424941Seric 			syserr("dowork: cannot fork");
76524941Seric 			return;
76624941Seric 		}
76724941Seric 	}
76824941Seric 	else
76924941Seric 	{
77024941Seric 		i = 0;
77124941Seric 	}
77224941Seric 
7734632Seric 	if (i == 0)
7744632Seric 	{
7754632Seric 		/*
7764632Seric 		**  CHILD
7778148Seric 		**	Lock the control file to avoid duplicate deliveries.
7788148Seric 		**		Then run the file as though we had just read it.
7797350Seric 		**	We save an idea of the temporary name so we
7807350Seric 		**		can recover on interrupt.
7814632Seric 		*/
7824632Seric 
7837763Seric 		/* set basic modes, etc. */
7847356Seric 		(void) alarm(0);
78555012Seric 		clearenvelope(e, FALSE);
78658737Seric 		e->e_flags |= EF_QUEUERUN;
78758734Seric 		e->e_errormode = EM_MAIL;
78858884Seric 		e->e_id = id;
7897876Seric # ifdef LOG
79058020Seric 		if (LogLevel > 76)
79155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7927881Seric 			       getpid());
79356795Seric # endif /* LOG */
7947763Seric 
7957763Seric 		/* don't use the headers from sendmail.cf... */
79655012Seric 		e->e_header = NULL;
7977763Seric 
79851920Seric 		/* read the queue control file -- return if locked */
79955012Seric 		if (!readqf(e))
8006980Seric 		{
80158884Seric 			if (tTd(40, 4))
80258884Seric 				printf("readqf(%s) failed\n", e->e_id);
80358884Seric 			if (forkflag)
80424941Seric 				exit(EX_OK);
80524941Seric 			else
80624941Seric 				return;
8076980Seric 		}
8086980Seric 
80955012Seric 		e->e_flags |= EF_INQUEUE;
81058737Seric 		eatheader(e);
8116980Seric 
8126980Seric 		/* do the delivery */
813*58915Seric 		sendall(e, SM_DELIVER);
8146980Seric 
8156980Seric 		/* finish up and exit */
81658884Seric 		if (forkflag)
81724941Seric 			finis();
81824941Seric 		else
81955012Seric 			dropenvelope(e);
8204632Seric 	}
82124941Seric 	else
82224941Seric 	{
82324941Seric 		/*
82424941Seric 		**  Parent -- pick up results.
82524941Seric 		*/
8264632Seric 
82724941Seric 		errno = 0;
82824941Seric 		(void) waitfor(i);
82924941Seric 	}
8304632Seric }
8314632Seric /*
8324632Seric **  READQF -- read queue file and set up environment.
8334632Seric **
8344632Seric **	Parameters:
8359377Seric **		e -- the envelope of the job to run.
8364632Seric **
8374632Seric **	Returns:
83851920Seric **		TRUE if it successfully read the queue file.
83951920Seric **		FALSE otherwise.
8404632Seric **
8414632Seric **	Side Effects:
84251920Seric **		The queue file is returned locked.
8434632Seric */
8444632Seric 
84551920Seric bool
84651920Seric readqf(e)
8479377Seric 	register ENVELOPE *e;
8484632Seric {
84917477Seric 	register FILE *qfp;
85054974Seric 	ADDRESS *ctladdr;
85156400Seric 	struct stat st;
85257135Seric 	char *bp;
853*58915Seric 	char qf[20];
85457135Seric 	char buf[MAXLINE];
8559348Seric 	extern char *fgetfolded();
85624954Seric 	extern long atol();
85754974Seric 	extern ADDRESS *setctluser();
85858689Seric 	extern bool lockfile();
8594632Seric 
8604632Seric 	/*
86117468Seric 	**  Read and process the file.
8624632Seric 	*/
8634632Seric 
864*58915Seric 	strcpy(qf, queuename(e, 'q'));
86551937Seric 	qfp = fopen(qf, "r+");
86617477Seric 	if (qfp == NULL)
86717477Seric 	{
86858884Seric 		if (tTd(40, 8))
86958884Seric 			printf("readqf(%s): fopen failure (%s)\n",
87058884Seric 				qf, errstring(errno));
87140934Srick 		if (errno != ENOENT)
87240934Srick 			syserr("readqf: no control file %s", qf);
87351920Seric 		return FALSE;
87417477Seric 	}
87540934Srick 
87656400Seric 	/*
87756400Seric 	**  Check the queue file for plausibility to avoid attacks.
87856400Seric 	*/
87956400Seric 
88056400Seric 	if (fstat(fileno(qfp), &st) < 0)
88156400Seric 	{
88256400Seric 		/* must have been being processed by someone else */
88358884Seric 		if (tTd(40, 8))
88458884Seric 			printf("readqf(%s): fstat failure (%s)\n",
88558884Seric 				qf, errstring(errno));
88656400Seric 		fclose(qfp);
88756400Seric 		return FALSE;
88856400Seric 	}
88956400Seric 
89057135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
89156400Seric 	{
89256400Seric # ifdef LOG
89356400Seric 		if (LogLevel > 0)
89456400Seric 		{
89556400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
89656400Seric 				e->e_id, st.st_uid, st.st_mode);
89756400Seric 		}
89856795Seric # endif /* LOG */
89958884Seric 		if (tTd(40, 8))
90058884Seric 			printf("readqf(%s): bogus file\n", qf);
90156400Seric 		fclose(qfp);
90256400Seric 		return FALSE;
90356400Seric 	}
90456400Seric 
90558689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
90640934Srick 	{
90758689Seric 		/* being processed by another queuer */
90858884Seric 		if (tTd(40, 8))
90958884Seric 			printf("readqf(%s): locked\n", qf);
91058689Seric 		if (Verbose)
91158689Seric 			printf("%s: locked\n", e->e_id);
91251920Seric # ifdef LOG
91358689Seric 		if (LogLevel > 19)
91458689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
91556795Seric # endif /* LOG */
91640934Srick 		(void) fclose(qfp);
91751920Seric 		return FALSE;
91840934Srick 	}
91940934Srick 
92051920Seric 	/* save this lock */
92151920Seric 	e->e_lockfp = qfp;
92251920Seric 
92340934Srick 	/* do basic system initialization */
92455012Seric 	initsys(e);
92540934Srick 
92617477Seric 	FileName = qf;
9279377Seric 	LineNumber = 0;
92851920Seric 	if (Verbose)
9299377Seric 		printf("\nRunning %s\n", e->e_id);
93054974Seric 	ctladdr = NULL;
93157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9324632Seric 	{
93358737Seric 		register char *p;
93457529Seric 		struct stat st;
93557529Seric 
93626504Seric 		if (tTd(40, 4))
93757135Seric 			printf("+++++ %s\n", bp);
93857135Seric 		switch (bp[0])
9394632Seric 		{
94040973Sbostic 		  case 'C':		/* specify controlling user */
94157135Seric 			ctladdr = setctluser(&bp[1]);
94240973Sbostic 			break;
94340973Sbostic 
9444632Seric 		  case 'R':		/* specify recipient */
94558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9464632Seric 			break;
9474632Seric 
94825687Seric 		  case 'E':		/* specify error recipient */
94958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
95025687Seric 			break;
95125687Seric 
9524632Seric 		  case 'H':		/* header */
95357135Seric 			(void) chompheader(&bp[1], FALSE, e);
9544632Seric 			break;
9554632Seric 
95610108Seric 		  case 'M':		/* message */
95757135Seric 			e->e_message = newstr(&bp[1]);
95810108Seric 			break;
95910108Seric 
9604632Seric 		  case 'S':		/* sender */
96158704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9624632Seric 			break;
9634632Seric 
9644632Seric 		  case 'D':		/* data file name */
96557135Seric 			e->e_df = newstr(&bp[1]);
9669544Seric 			e->e_dfp = fopen(e->e_df, "r");
9679544Seric 			if (e->e_dfp == NULL)
96858020Seric 			{
9699377Seric 				syserr("readqf: cannot open %s", e->e_df);
97058020Seric 				e->e_msgsize = -1;
97158020Seric 			}
97258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
97357529Seric 				e->e_msgsize = st.st_size;
9744632Seric 			break;
9754632Seric 
9767860Seric 		  case 'T':		/* init time */
97757135Seric 			e->e_ctime = atol(&bp[1]);
9784632Seric 			break;
9794632Seric 
9804634Seric 		  case 'P':		/* message priority */
98157135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9824634Seric 			break;
9834634Seric 
98458737Seric 		  case 'F':		/* flag bits */
98558737Seric 			for (p = &bp[1]; *p != '\0'; p++)
98658737Seric 			{
98758737Seric 				switch (*p)
98858737Seric 				{
98958737Seric 				  case 'w':	/* warning sent */
99058737Seric 					e->e_flags |= EF_WARNING;
99158737Seric 					break;
99258737Seric 
99358737Seric 				  case 'r':	/* response */
99458737Seric 					e->e_flags |= EF_RESPONSE;
99558737Seric 					break;
99658737Seric 				}
99758737Seric 			}
99858737Seric 			break;
99958737Seric 
100053400Seric 		  case '$':		/* define macro */
100157135Seric 			define(bp[1], newstr(&bp[2]), e);
100253400Seric 			break;
100353400Seric 
100424941Seric 		  case '\0':		/* blank line; ignore */
100524941Seric 			break;
100624941Seric 
10074632Seric 		  default:
100824941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
100957135Seric 				LineNumber, bp);
10104632Seric 			break;
10114632Seric 		}
101257135Seric 
101357135Seric 		if (bp != buf)
101457135Seric 			free(bp);
10154632Seric 	}
10169377Seric 
10179377Seric 	FileName = NULL;
101824941Seric 
101924941Seric 	/*
102024941Seric 	**  If we haven't read any lines, this queue file is empty.
102124941Seric 	**  Arrange to remove it without referencing any null pointers.
102224941Seric 	*/
102324941Seric 
102424941Seric 	if (LineNumber == 0)
102524941Seric 	{
102624941Seric 		errno = 0;
102724941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
102824941Seric 	}
102951920Seric 	return TRUE;
10304632Seric }
10314632Seric /*
10329630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10339630Seric **
10349630Seric **	Parameters:
10359630Seric **		none.
10369630Seric **
10379630Seric **	Returns:
10389630Seric **		none.
10399630Seric **
10409630Seric **	Side Effects:
10419630Seric **		Prints a listing of the mail queue on the standard output.
10429630Seric */
10435182Seric 
10449630Seric printqueue()
10459630Seric {
10469630Seric 	register WORK *w;
10479630Seric 	FILE *f;
104810070Seric 	int nrequests;
10499630Seric 	char buf[MAXLINE];
10509630Seric 
10519630Seric 	/*
105258250Seric 	**  Check for permission to print the queue
105358250Seric 	*/
105458250Seric 
105558523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
105658250Seric 	{
105758250Seric 		struct stat st;
105858523Seric # ifdef NGROUPS
105958523Seric 		int n;
106058523Seric 		int gidset[NGROUPS];
106158523Seric # endif
106258250Seric 
106358517Seric 		if (stat(QueueDir, &st) < 0)
106458250Seric 		{
106558250Seric 			syserr("Cannot stat %s", QueueDir);
106658250Seric 			return;
106758250Seric 		}
106858523Seric # ifdef NGROUPS
106958523Seric 		n = getgroups(NGROUPS, gidset);
107058523Seric 		while (--n >= 0)
107158523Seric 		{
107258523Seric 			if (gidset[n] == st.st_gid)
107358523Seric 				break;
107458523Seric 		}
107558523Seric 		if (n < 0)
107658523Seric # else
107758250Seric 		if (getgid() != st.st_gid)
107858523Seric # endif
107958250Seric 		{
108058250Seric 			usrerr("510 You are not permitted to see the queue");
108158250Seric 			setstat(EX_NOPERM);
108258250Seric 			return;
108358250Seric 		}
108458250Seric 	}
108558250Seric 
108658250Seric 	/*
10879630Seric 	**  Read and order the queue.
10889630Seric 	*/
10899630Seric 
109024941Seric 	nrequests = orderq(TRUE);
10919630Seric 
10929630Seric 	/*
10939630Seric 	**  Print the work list that we have read.
10949630Seric 	*/
10959630Seric 
10969630Seric 	/* first see if there is anything */
109710070Seric 	if (nrequests <= 0)
10989630Seric 	{
109910070Seric 		printf("Mail queue is empty\n");
11009630Seric 		return;
11019630Seric 	}
11029630Seric 
110351920Seric 	CurrentLA = getla();	/* get load average */
110440934Srick 
110510096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
110625687Seric 	if (nrequests > QUEUESIZE)
110725687Seric 		printf(", only %d printed", QUEUESIZE);
110824979Seric 	if (Verbose)
110958716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
111024979Seric 	else
111158716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11129630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11139630Seric 	{
11149630Seric 		struct stat st;
111510070Seric 		auto time_t submittime = 0;
111610070Seric 		long dfsize = -1;
111758737Seric 		int flags = 0;
111810108Seric 		char message[MAXLINE];
111924941Seric 		extern bool shouldqueue();
112058689Seric 		extern bool lockfile();
11219630Seric 
112217468Seric 		f = fopen(w->w_name, "r");
112317468Seric 		if (f == NULL)
112417468Seric 		{
112517468Seric 			errno = 0;
112617468Seric 			continue;
112717468Seric 		}
112858724Seric 		printf("%8s", w->w_name + 2);
112958689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
113010070Seric 			printf("*");
113157438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
113224941Seric 			printf("X");
113310070Seric 		else
113410070Seric 			printf(" ");
113510070Seric 		errno = 0;
113617468Seric 
113710108Seric 		message[0] = '\0';
11389630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11399630Seric 		{
114053400Seric 			register int i;
114158737Seric 			register char *p;
114253400Seric 
11439630Seric 			fixcrlf(buf, TRUE);
11449630Seric 			switch (buf[0])
11459630Seric 			{
114610108Seric 			  case 'M':	/* error message */
114753400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
114858737Seric 					i = sizeof message - 1;
114953400Seric 				bcopy(&buf[1], message, i);
115053400Seric 				message[i] = '\0';
115110108Seric 				break;
115210108Seric 
11539630Seric 			  case 'S':	/* sender name */
115424979Seric 				if (Verbose)
115558737Seric 					printf("%8ld %10ld%c%.12s %.38s",
115658737Seric 					    dfsize,
115758737Seric 					    w->w_pri,
115858737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
115958737Seric 					    ctime(&submittime) + 4,
116024979Seric 					    &buf[1]);
116124979Seric 				else
116224979Seric 					printf("%8ld %.16s %.45s", dfsize,
116324979Seric 					    ctime(&submittime), &buf[1]);
116410108Seric 				if (message[0] != '\0')
116525027Seric 					printf("\n\t\t (%.60s)", message);
11669630Seric 				break;
116751920Seric 
116840973Sbostic 			  case 'C':	/* controlling user */
116954974Seric 				if (Verbose)
117058716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
117158716Seric 						&buf[1]);
117240973Sbostic 				break;
11739630Seric 
11749630Seric 			  case 'R':	/* recipient name */
117524979Seric 				if (Verbose)
117658716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
117724979Seric 				else
117858716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
11799630Seric 				break;
11809630Seric 
11819630Seric 			  case 'T':	/* creation time */
118224941Seric 				submittime = atol(&buf[1]);
11839630Seric 				break;
118410070Seric 
118510070Seric 			  case 'D':	/* data file name */
118610070Seric 				if (stat(&buf[1], &st) >= 0)
118710070Seric 					dfsize = st.st_size;
118810070Seric 				break;
118958737Seric 
119058737Seric 			  case 'F':	/* flag bits */
119158737Seric 				for (p = &buf[1]; *p != '\0'; p++)
119258737Seric 				{
119358737Seric 					switch (*p)
119458737Seric 					{
119558737Seric 					  case 'w':
119658737Seric 						flags |= EF_WARNING;
119758737Seric 						break;
119858737Seric 					}
119958737Seric 				}
12009630Seric 			}
12019630Seric 		}
120210070Seric 		if (submittime == (time_t) 0)
120310070Seric 			printf(" (no control file)");
12049630Seric 		printf("\n");
120523098Seric 		(void) fclose(f);
12069630Seric 	}
12079630Seric }
12089630Seric 
120956795Seric # endif /* QUEUE */
121017468Seric /*
121117468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
121217468Seric **
121317468Seric **	Assigns an id code if one does not already exist.
121417468Seric **	This code is very careful to avoid trashing existing files
121517468Seric **	under any circumstances.
121617468Seric **
121717468Seric **	Parameters:
121817468Seric **		e -- envelope to build it in/from.
121917468Seric **		type -- the file type, used as the first character
122017468Seric **			of the file name.
122117468Seric **
122217468Seric **	Returns:
122317468Seric **		a pointer to the new file name (in a static buffer).
122417468Seric **
122517468Seric **	Side Effects:
122651920Seric **		If no id code is already assigned, queuename will
122751920Seric **		assign an id code, create a qf file, and leave a
122851920Seric **		locked, open-for-write file pointer in the envelope.
122917468Seric */
123017468Seric 
123117468Seric char *
123217468Seric queuename(e, type)
123317468Seric 	register ENVELOPE *e;
123417468Seric 	char type;
123517468Seric {
123617468Seric 	static int pid = -1;
123758741Seric 	static char c0;
123858741Seric 	static char c1;
123958741Seric 	static char c2;
124058716Seric 	time_t now;
124158716Seric 	struct tm *tm;
124258689Seric 	static char buf[MAXNAME];
124358689Seric 	extern bool lockfile();
124417468Seric 
124517468Seric 	if (e->e_id == NULL)
124617468Seric 	{
124717468Seric 		char qf[20];
124817468Seric 
124917468Seric 		/* find a unique id */
125017468Seric 		if (pid != getpid())
125117468Seric 		{
125217468Seric 			/* new process -- start back at "AA" */
125317468Seric 			pid = getpid();
125458716Seric 			now = curtime();
125558716Seric 			tm = localtime(&now);
125658716Seric 			c0 = 'A' + tm->tm_hour;
125717468Seric 			c1 = 'A';
125817468Seric 			c2 = 'A' - 1;
125917468Seric 		}
126058716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
126117468Seric 
126217468Seric 		while (c1 < '~' || c2 < 'Z')
126317468Seric 		{
126417468Seric 			int i;
126517468Seric 
126617468Seric 			if (c2 >= 'Z')
126717468Seric 			{
126817468Seric 				c1++;
126917468Seric 				c2 = 'A' - 1;
127017468Seric 			}
127158716Seric 			qf[3] = c1;
127258716Seric 			qf[4] = ++c2;
127317468Seric 			if (tTd(7, 20))
127440934Srick 				printf("queuename: trying \"%s\"\n", qf);
127517468Seric 
127640934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
127751920Seric 			if (i < 0)
127851920Seric 			{
127951920Seric 				if (errno == EEXIST)
128051920Seric 					continue;
128151920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
128251920Seric 					qf, QueueDir);
128351920Seric 				exit(EX_UNAVAILABLE);
128451920Seric 			}
128558689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
128651920Seric 			{
128751920Seric 				e->e_lockfp = fdopen(i, "w");
128840934Srick 				break;
128917468Seric 			}
129051920Seric 
129151920Seric 			/* a reader got the file; abandon it and try again */
129251920Seric 			(void) close(i);
129317468Seric 		}
129417468Seric 		if (c1 >= '~' && c2 >= 'Z')
129517468Seric 		{
129617468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
129717468Seric 				qf, QueueDir);
129817468Seric 			exit(EX_OSERR);
129917468Seric 		}
130017468Seric 		e->e_id = newstr(&qf[2]);
130117468Seric 		define('i', e->e_id, e);
130217468Seric 		if (tTd(7, 1))
130317468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
130417468Seric # ifdef LOG
130558020Seric 		if (LogLevel > 93)
130617468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
130756795Seric # endif /* LOG */
130817468Seric 	}
130917468Seric 
131017468Seric 	if (type == '\0')
131117468Seric 		return (NULL);
131217468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
131317468Seric 	if (tTd(7, 2))
131417468Seric 		printf("queuename: %s\n", buf);
131517468Seric 	return (buf);
131617468Seric }
131717468Seric /*
131817468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
131917468Seric **
132017468Seric **	Parameters:
132117468Seric **		e -- the envelope to unlock.
132217468Seric **
132317468Seric **	Returns:
132417468Seric **		none
132517468Seric **
132617468Seric **	Side Effects:
132717468Seric **		unlocks the queue for `e'.
132817468Seric */
132917468Seric 
133017468Seric unlockqueue(e)
133117468Seric 	ENVELOPE *e;
133217468Seric {
133358680Seric 	if (tTd(51, 4))
133458680Seric 		printf("unlockqueue(%s)\n", e->e_id);
133558680Seric 
133651920Seric 	/* if there is a lock file in the envelope, close it */
133751920Seric 	if (e->e_lockfp != NULL)
133858680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
133951920Seric 	e->e_lockfp = NULL;
134051920Seric 
134158728Seric 	/* don't create a queue id if we don't already have one */
134258728Seric 	if (e->e_id == NULL)
134358728Seric 		return;
134458728Seric 
134517468Seric 	/* remove the transcript */
134617468Seric # ifdef LOG
134758020Seric 	if (LogLevel > 87)
134817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
134956795Seric # endif /* LOG */
135058680Seric 	if (!tTd(51, 104))
135117468Seric 		xunlink(queuename(e, 'x'));
135217468Seric 
135317468Seric }
135440973Sbostic /*
135554974Seric **  SETCTLUSER -- create a controlling address
135640973Sbostic **
135754974Seric **	Create a fake "address" given only a local login name; this is
135854974Seric **	used as a "controlling user" for future recipient addresses.
135940973Sbostic **
136040973Sbostic **	Parameters:
136154974Seric **		user -- the user name of the controlling user.
136240973Sbostic **
136340973Sbostic **	Returns:
136454974Seric **		An address descriptor for the controlling user.
136540973Sbostic **
136640973Sbostic **	Side Effects:
136740973Sbostic **		none.
136840973Sbostic */
136940973Sbostic 
137054974Seric ADDRESS *
137154974Seric setctluser(user)
137254974Seric 	char *user;
137340973Sbostic {
137454974Seric 	register ADDRESS *a;
137540973Sbostic 	struct passwd *pw;
137640973Sbostic 
137740973Sbostic 	/*
137854974Seric 	**  See if this clears our concept of controlling user.
137940973Sbostic 	*/
138040973Sbostic 
138154974Seric 	if (user == NULL || *user == '\0')
138258704Seric 		user = DefUser;
138340973Sbostic 
138440973Sbostic 	/*
138554974Seric 	**  Set up addr fields for controlling user.
138640973Sbostic 	*/
138740973Sbostic 
138854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
138954974Seric 	bzero((char *) a, sizeof *a);
139054974Seric 	if ((pw = getpwnam(user)) != NULL)
139140973Sbostic 	{
139240973Sbostic 		a->q_home = newstr(pw->pw_dir);
139340973Sbostic 		a->q_uid = pw->pw_uid;
139440973Sbostic 		a->q_gid = pw->pw_gid;
139557642Seric 		a->q_user = newstr(user);
139640973Sbostic 	}
139740973Sbostic 	else
139840973Sbostic 	{
139940973Sbostic 		a->q_uid = DefUid;
140040973Sbostic 		a->q_gid = DefGid;
140157642Seric 		a->q_user = newstr(DefUser);
140240973Sbostic 	}
140340973Sbostic 
140458294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
140556328Seric 	a->q_mailer = LocalMailer;
140654974Seric 	return a;
140740973Sbostic }
1408