xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 64296)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362528Sbostic  * Copyright (c) 1988, 1993
462528Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*64296Seric static char sccsid[] = "@(#)queue.c	8.12 (Berkeley) 08/19/93 (with queueing)";
1433731Sbostic #else
15*64296Seric static char sccsid[] = "@(#)queue.c	8.12 (Berkeley) 08/19/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7151920Seric 	char buf[MAXLINE], tf[MAXLINE];
724632Seric 
735037Seric 	/*
7417477Seric 	**  Create control file.
755037Seric 	*/
764632Seric 
7751920Seric 	newid = (e->e_id == NULL);
7864277Seric 
7964277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8051920Seric 	strcpy(tf, queuename(e, 't'));
8151920Seric 	tfp = e->e_lockfp;
8251920Seric 	if (tfp == NULL)
8351920Seric 		newid = FALSE;
8464277Seric 
8564277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
8651920Seric 	if (newid)
8751835Seric 	{
8851920Seric 		tfp = e->e_lockfp;
8951920Seric 	}
9051920Seric 	else
9151920Seric 	{
9251920Seric 		/* get a locked tf file */
9364070Seric 		for (i = 0; i < 128; i++)
9451835Seric 		{
9551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9651920Seric 			if (fd < 0)
9751835Seric 			{
9864070Seric 				if (errno != EEXIST)
9964070Seric 					break;
10064070Seric #ifdef LOG
10164070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10264070Seric 					syslog(LOG_ALERT, "queueup: cannot create %s: %s",
10364070Seric 						tf, errstring(errno));
10464070Seric #endif
10564070Seric 				continue;
10641636Srick 			}
10758689Seric 
10858689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
10951920Seric 				break;
11064070Seric #ifdef LOG
11164070Seric 			else if (LogLevel > 0 && (i % 32) == 0)
11264070Seric 				syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11364070Seric 					tf, errstring(errno));
11464070Seric #endif
11558689Seric 
11651920Seric 			close(fd);
11764070Seric 
11864070Seric 			if ((i % 32) == 31)
11964070Seric 			{
12064070Seric 				/* save the old temp file away */
12164070Seric 				(void) rename(tf, queuename(e, 'T'));
12264070Seric 			}
12364070Seric 			else
12464070Seric 				sleep(i % 32);
12541636Srick 		}
12658801Seric 		if (fd < 0)
12764070Seric 			syserr("!queueup: cannot create temp file %s", tf);
12841636Srick 
12951920Seric 		tfp = fdopen(fd, "w");
13051920Seric 	}
1314632Seric 
1327677Seric 	if (tTd(40, 1))
13317468Seric 		printf("queueing %s\n", e->e_id);
1344632Seric 
1354632Seric 	/*
1366980Seric 	**  If there is no data file yet, create one.
1376980Seric 	*/
1386980Seric 
1396980Seric 	if (e->e_df == NULL)
1406980Seric 	{
1416980Seric 		register FILE *dfp;
1429389Seric 		extern putbody();
1436980Seric 
14464086Seric 		e->e_df = queuename(e, 'd');
14564086Seric 		e->e_df = newstr(e->e_df);
14640934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14740934Srick 		if (fd < 0)
14858690Seric 			syserr("!queueup: cannot create %s", e->e_df);
14940934Srick 		dfp = fdopen(fd, "w");
15059730Seric 		(*e->e_putbody)(dfp, FileMailer, e, NULL);
15158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1529389Seric 		e->e_putbody = putbody;
1536980Seric 	}
1546980Seric 
1556980Seric 	/*
1564632Seric 	**  Output future work requests.
15725687Seric 	**	Priority and creation time should be first, since
15825687Seric 	**	they are required by orderq.
1594632Seric 	*/
1604632Seric 
1619377Seric 	/* output message priority */
1629377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1639377Seric 
1649630Seric 	/* output creation time */
1659630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1669630Seric 
16759093Seric 	/* output type and name of data file */
16859093Seric 	if (e->e_bodytype != NULL)
16959093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1707812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1714632Seric 
17210108Seric 	/* message from envelope, if it exists */
17310108Seric 	if (e->e_message != NULL)
17410108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17510108Seric 
17658737Seric 	/* send various flag bits through */
17758737Seric 	p = buf;
17858737Seric 	if (bitset(EF_WARNING, e->e_flags))
17958737Seric 		*p++ = 'w';
18058737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
18158737Seric 		*p++ = 'r';
18258737Seric 	*p++ = '\0';
18358737Seric 	if (buf[0] != '\0')
18458737Seric 		fprintf(tfp, "F%s\n", buf);
18558737Seric 
18658957Seric 	/* $r and $s and $_ macro values */
18753400Seric 	if ((p = macvalue('r', e)) != NULL)
18853400Seric 		fprintf(tfp, "$r%s\n", p);
18953400Seric 	if ((p = macvalue('s', e)) != NULL)
19053400Seric 		fprintf(tfp, "$s%s\n", p);
19158957Seric 	if ((p = macvalue('_', e)) != NULL)
19258957Seric 		fprintf(tfp, "$_%s\n", p);
19353400Seric 
1944632Seric 	/* output name of sender */
1957812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1964632Seric 
19755360Seric 	/* output list of error recipients */
19859670Seric 	printctladdr(NULL, NULL);
19955360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20055360Seric 	{
20158680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
20255360Seric 		{
20359113Seric 			printctladdr(q, tfp);
20455360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
20555360Seric 		}
20655360Seric 	}
20755360Seric 
2084632Seric 	/* output list of recipient addresses */
2096980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2104632Seric 	{
21158250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
21258680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2138245Seric 		{
21459113Seric 			printctladdr(q, tfp);
2157812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2169377Seric 			if (announce)
2179377Seric 			{
2189377Seric 				e->e_to = q->q_paddr;
21958151Seric 				message("queued");
22058020Seric 				if (LogLevel > 8)
22158337Seric 					logdelivery(NULL, NULL, "queued", e);
2229377Seric 				e->e_to = NULL;
2239377Seric 			}
2249387Seric 			if (tTd(40, 1))
2259387Seric 			{
2269387Seric 				printf("queueing ");
2279387Seric 				printaddr(q, FALSE);
2289387Seric 			}
2298245Seric 		}
2304632Seric 	}
2314632Seric 
2329377Seric 	/*
2339377Seric 	**  Output headers for this message.
2349377Seric 	**	Expand macros completely here.  Queue run will deal with
2359377Seric 	**	everything as absolute headers.
2369377Seric 	**		All headers that must be relative to the recipient
2379377Seric 	**		can be cracked later.
23810173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
23910173Seric 	**	no effect on the addresses as they are output.
2409377Seric 	*/
2419377Seric 
24210686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24358020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24458020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24510349Seric 	nullmailer.m_eol = "\n";
24610173Seric 
24758050Seric 	define('g', "\201f", e);
2486980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2494632Seric 	{
25010686Seric 		extern bool bitzerop();
25110686Seric 
25212015Seric 		/* don't output null headers */
2534632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2544632Seric 			continue;
25512015Seric 
25612015Seric 		/* don't output resent headers on non-resent messages */
25712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25812015Seric 			continue;
25912015Seric 
26012015Seric 		/* output this header */
2617812Seric 		fprintf(tfp, "H");
26212015Seric 
26312015Seric 		/* if conditional, output the set of conditions */
26410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26510686Seric 		{
26610686Seric 			int j;
26710686Seric 
26823098Seric 			(void) putc('?', tfp);
26910686Seric 			for (j = '\0'; j <= '\177'; j++)
27010686Seric 				if (bitnset(j, h->h_mflags))
27123098Seric 					(void) putc(j, tfp);
27223098Seric 			(void) putc('?', tfp);
27310686Seric 		}
27412015Seric 
27512015Seric 		/* output the header: expand macros, convert addresses */
2767763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2777763Seric 		{
2787763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2798236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2807763Seric 		}
2818245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2829348Seric 		{
28358737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28463753Seric 			FILE *savetrace = TrafficLogFile;
28558737Seric 
28663753Seric 			TrafficLogFile = NULL;
28763753Seric 
28858737Seric 			if (bitset(H_FROM, h->h_flags))
28958737Seric 				oldstyle = FALSE;
29058737Seric 
29158737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29255012Seric 				 &nullmailer, e);
29363753Seric 
29463753Seric 			TrafficLogFile = savetrace;
2959348Seric 		}
2967763Seric 		else
2978245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2984632Seric 	}
2994632Seric 
3004632Seric 	/*
3014632Seric 	**  Clean up.
3024632Seric 	*/
3034632Seric 
30458732Seric 	fflush(tfp);
30560603Seric 	fsync(fileno(tfp));
30658732Seric 	if (ferror(tfp))
30758732Seric 	{
30858732Seric 		if (newid)
30958732Seric 			syserr("!552 Error writing control file %s", tf);
31058732Seric 		else
31158732Seric 			syserr("!452 Error writing control file %s", tf);
31258732Seric 	}
31358732Seric 
31451920Seric 	if (!newid)
31551920Seric 	{
31664277Seric 		/* rename (locked) tf to be (locked) qf */
31751920Seric 		qf = queuename(e, 'q');
31851920Seric 		if (rename(tf, qf) < 0)
31951920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
32064277Seric 
32164277Seric 		/* close and unlock old (locked) qf */
32251920Seric 		if (e->e_lockfp != NULL)
32358680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
32451920Seric 		e->e_lockfp = tfp;
32551920Seric 	}
32651920Seric 	else
32751920Seric 		qf = tf;
32841636Srick 	errno = 0;
3297391Seric 
3307677Seric # ifdef LOG
3317677Seric 	/* save log info */
33258020Seric 	if (LogLevel > 79)
3337878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33456795Seric # endif /* LOG */
33551920Seric 	return;
3364632Seric }
33754974Seric 
33854974Seric printctladdr(a, tfp)
33959113Seric 	register ADDRESS *a;
34054974Seric 	FILE *tfp;
34154974Seric {
34259113Seric 	char *uname;
34359113Seric 	register struct passwd *pw;
34459113Seric 	register ADDRESS *q;
34559113Seric 	uid_t uid;
34659113Seric 	static ADDRESS *lastctladdr;
34759113Seric 	static uid_t lastuid;
34854974Seric 
34959113Seric 	/* initialization */
35063850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
35154974Seric 	{
35259670Seric 		if (lastctladdr != NULL && tfp != NULL)
35359113Seric 			fprintf(tfp, "C\n");
35459113Seric 		lastctladdr = NULL;
35559113Seric 		lastuid = 0;
35654974Seric 		return;
35754974Seric 	}
35859113Seric 
35959113Seric 	/* find the active uid */
36059113Seric 	q = getctladdr(a);
36159113Seric 	if (q == NULL)
36259113Seric 		uid = 0;
36354974Seric 	else
36459113Seric 		uid = q->q_uid;
36563850Seric 	a = a->q_alias;
36659113Seric 
36759113Seric 	/* check to see if this is the same as last time */
36859113Seric 	if (lastctladdr != NULL && uid == lastuid &&
36959113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
37059113Seric 		return;
37159113Seric 	lastuid = uid;
37259113Seric 	lastctladdr = a;
37359113Seric 
37459113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
37559270Seric 		uname = "";
37659113Seric 	else
37759113Seric 		uname = pw->pw_name;
37859113Seric 
37959113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
38054974Seric }
38154974Seric 
3824632Seric /*
3834632Seric **  RUNQUEUE -- run the jobs in the queue.
3844632Seric **
3854632Seric **	Gets the stuff out of the queue in some presumably logical
3864632Seric **	order and processes them.
3874632Seric **
3884632Seric **	Parameters:
38924941Seric **		forkflag -- TRUE if the queue scanning should be done in
39024941Seric **			a child process.  We double-fork so it is not our
39124941Seric **			child and we don't have to clean up after it.
3924632Seric **
3934632Seric **	Returns:
3944632Seric **		none.
3954632Seric **
3964632Seric **	Side Effects:
3974632Seric **		runs things in the mail queue.
3984632Seric */
3994632Seric 
40055360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
40155360Seric 
40255360Seric runqueue(forkflag)
4034639Seric 	bool forkflag;
4044632Seric {
40555360Seric 	register ENVELOPE *e;
40655360Seric 	extern ENVELOPE BlankEnvelope;
40724953Seric 
4087466Seric 	/*
40924953Seric 	**  If no work will ever be selected, don't even bother reading
41024953Seric 	**  the queue.
41124953Seric 	*/
41224953Seric 
41351920Seric 	CurrentLA = getla();	/* get load average */
41440934Srick 
41558132Seric 	if (shouldqueue(0L, curtime()))
41624953Seric 	{
41724953Seric 		if (Verbose)
41824953Seric 			printf("Skipping queue run -- load average too high\n");
41958107Seric 		if (forkflag && QueueIntvl != 0)
42058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
42155360Seric 		return;
42224953Seric 	}
42324953Seric 
42424953Seric 	/*
4257466Seric 	**  See if we want to go off and do other useful work.
4267466Seric 	*/
4274639Seric 
4284639Seric 	if (forkflag)
4294639Seric 	{
4307943Seric 		int pid;
4317943Seric 
4327943Seric 		pid = dofork();
4337943Seric 		if (pid != 0)
4344639Seric 		{
43546928Sbostic 			extern void reapchild();
43625184Seric 
4377943Seric 			/* parent -- pick up intermediate zombie */
43825184Seric #ifndef SIGCHLD
4399377Seric 			(void) waitfor(pid);
44056795Seric #else /* SIGCHLD */
44164035Seric 			(void) setsignal(SIGCHLD, reapchild);
44256795Seric #endif /* SIGCHLD */
4437690Seric 			if (QueueIntvl != 0)
4449348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4454639Seric 			return;
4464639Seric 		}
4477943Seric 		/* child -- double fork */
44825184Seric #ifndef SIGCHLD
4497943Seric 		if (fork() != 0)
4507943Seric 			exit(EX_OK);
45156795Seric #else /* SIGCHLD */
45264035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
45356795Seric #endif /* SIGCHLD */
4544639Seric 	}
45524941Seric 
45640934Srick 	setproctitle("running queue: %s", QueueDir);
45724941Seric 
4587876Seric # ifdef LOG
45958020Seric 	if (LogLevel > 69)
46055360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
46155360Seric 			QueueDir, getpid(), forkflag);
46256795Seric # endif /* LOG */
4634639Seric 
4647466Seric 	/*
46510205Seric 	**  Release any resources used by the daemon code.
46610205Seric 	*/
46710205Seric 
46810205Seric # ifdef DAEMON
46910205Seric 	clrdaemon();
47056795Seric # endif /* DAEMON */
47110205Seric 
47210205Seric 	/*
47355360Seric 	**  Create ourselves an envelope
47455360Seric 	*/
47555360Seric 
47655360Seric 	CurEnv = &QueueEnvelope;
47758179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
47855360Seric 	e->e_flags = BlankEnvelope.e_flags;
47955360Seric 
48055360Seric 	/*
48127175Seric 	**  Make sure the alias database is open.
48227175Seric 	*/
48327175Seric 
48460537Seric 	initmaps(FALSE, e);
48527175Seric 
48627175Seric 	/*
4877466Seric 	**  Start making passes through the queue.
4887466Seric 	**	First, read and sort the entire queue.
4897466Seric 	**	Then, process the work in that order.
4907466Seric 	**		But if you take too long, start over.
4917466Seric 	*/
4927466Seric 
4937943Seric 	/* order the existing work requests */
49424954Seric 	(void) orderq(FALSE);
4957690Seric 
4967943Seric 	/* process them once at a time */
4977943Seric 	while (WorkQ != NULL)
4984639Seric 	{
4997943Seric 		WORK *w = WorkQ;
5007881Seric 
5017943Seric 		WorkQ = WorkQ->w_next;
50258884Seric 
50358884Seric 		/*
50458884Seric 		**  Ignore jobs that are too expensive for the moment.
50558884Seric 		*/
50658884Seric 
50758884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
50858884Seric 		{
50958884Seric 			if (Verbose)
51058884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
51158884Seric 		}
51258930Seric 		else
51358930Seric 		{
514*64296Seric 			pid_t pid;
515*64296Seric 			extern pid_t dowork();
516*64296Seric 
517*64296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
518*64296Seric 			errno = 0;
519*64296Seric 			(void) waitfor(pid);
52058930Seric 		}
5217943Seric 		free(w->w_name);
5227943Seric 		free((char *) w);
5234639Seric 	}
52429866Seric 
52529866Seric 	/* exit without the usual cleanup */
52655467Seric 	e->e_id = NULL;
52755467Seric 	finis();
5284634Seric }
5294634Seric /*
5304632Seric **  ORDERQ -- order the work queue.
5314632Seric **
5324632Seric **	Parameters:
53324941Seric **		doall -- if set, include everything in the queue (even
53424941Seric **			the jobs that cannot be run because the load
53524941Seric **			average is too high).  Otherwise, exclude those
53624941Seric **			jobs.
5374632Seric **
5384632Seric **	Returns:
53910121Seric **		The number of request in the queue (not necessarily
54010121Seric **		the number of requests in WorkQ however).
5414632Seric **
5424632Seric **	Side Effects:
5434632Seric **		Sets WorkQ to the queue of available work, in order.
5444632Seric */
5454632Seric 
54625687Seric # define NEED_P		001
54725687Seric # define NEED_T		002
54858318Seric # define NEED_R		004
54958318Seric # define NEED_S		010
5504632Seric 
55124941Seric orderq(doall)
55224941Seric 	bool doall;
5534632Seric {
55460219Seric 	register struct dirent *d;
5554632Seric 	register WORK *w;
5566625Sglickman 	DIR *f;
5574632Seric 	register int i;
55825687Seric 	WORK wlist[QUEUESIZE+1];
55910070Seric 	int wn = -1;
5604632Seric 	extern workcmpf();
5614632Seric 
56258318Seric 	if (tTd(41, 1))
56358318Seric 	{
56458318Seric 		printf("orderq:\n");
56558318Seric 		if (QueueLimitId != NULL)
56658318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
56758318Seric 		if (QueueLimitSender != NULL)
56858318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
56958318Seric 		if (QueueLimitRecipient != NULL)
57058318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
57158318Seric 	}
57258318Seric 
5734632Seric 	/* clear out old WorkQ */
5744632Seric 	for (w = WorkQ; w != NULL; )
5754632Seric 	{
5764632Seric 		register WORK *nw = w->w_next;
5774632Seric 
5784632Seric 		WorkQ = nw;
5794632Seric 		free(w->w_name);
5804632Seric 		free((char *) w);
5814632Seric 		w = nw;
5824632Seric 	}
5834632Seric 
5844632Seric 	/* open the queue directory */
5858148Seric 	f = opendir(".");
5864632Seric 	if (f == NULL)
5874632Seric 	{
5888148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
58910070Seric 		return (0);
5904632Seric 	}
5914632Seric 
5924632Seric 	/*
5934632Seric 	**  Read the work directory.
5944632Seric 	*/
5954632Seric 
59610070Seric 	while ((d = readdir(f)) != NULL)
5974632Seric 	{
5989377Seric 		FILE *cf;
5994632Seric 		char lbuf[MAXNAME];
60058318Seric 		extern bool strcontainedin();
6014632Seric 
6024632Seric 		/* is this an interesting entry? */
6037812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6044632Seric 			continue;
6054632Seric 
60658318Seric 		if (QueueLimitId != NULL &&
60758318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
60858318Seric 			continue;
60958318Seric 
61058722Seric 		/*
61158722Seric 		**  Check queue name for plausibility.  This handles
61258722Seric 		**  both old and new type ids.
61358722Seric 		*/
61458722Seric 
61558722Seric 		i = strlen(d->d_name);
61658722Seric 		if (i != 9 && i != 10)
61758020Seric 		{
61858020Seric 			if (Verbose)
61958020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
62058020Seric #ifdef LOG
62158020Seric 			if (LogLevel > 3)
62259615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
62358020Seric 					d->d_name);
62458020Seric #endif
62558020Seric 			if (strlen(d->d_name) >= MAXNAME)
62658020Seric 				d->d_name[MAXNAME - 1] = '\0';
62758020Seric 			strcpy(lbuf, d->d_name);
62858020Seric 			lbuf[0] = 'Q';
62958020Seric 			(void) rename(d->d_name, lbuf);
63058020Seric 			continue;
63158020Seric 		}
63258020Seric 
63310070Seric 		/* yes -- open control file (if not too many files) */
63425687Seric 		if (++wn >= QUEUESIZE)
63510070Seric 			continue;
63658318Seric 
6378148Seric 		cf = fopen(d->d_name, "r");
6384632Seric 		if (cf == NULL)
6394632Seric 		{
6407055Seric 			/* this may be some random person sending hir msgs */
6417055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
64210090Seric 			if (tTd(41, 2))
64310090Seric 				printf("orderq: cannot open %s (%d)\n",
64410090Seric 					d->d_name, errno);
6457055Seric 			errno = 0;
64610090Seric 			wn--;
6474632Seric 			continue;
6484632Seric 		}
64925687Seric 		w = &wlist[wn];
65025687Seric 		w->w_name = newstr(d->d_name);
6514632Seric 
65225027Seric 		/* make sure jobs in creation don't clog queue */
65325687Seric 		w->w_pri = 0x7fffffff;
65425687Seric 		w->w_ctime = 0;
65525027Seric 
6564632Seric 		/* extract useful information */
65725687Seric 		i = NEED_P | NEED_T;
65858318Seric 		if (QueueLimitSender != NULL)
65958318Seric 			i |= NEED_S;
66058318Seric 		if (QueueLimitRecipient != NULL)
66158318Seric 			i |= NEED_R;
66225687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6634632Seric 		{
66424954Seric 			extern long atol();
66558318Seric 			extern bool strcontainedin();
66624954Seric 
66724941Seric 			switch (lbuf[0])
6684632Seric 			{
66924941Seric 			  case 'P':
67025687Seric 				w->w_pri = atol(&lbuf[1]);
67125687Seric 				i &= ~NEED_P;
6724632Seric 				break;
67325013Seric 
67425013Seric 			  case 'T':
67525687Seric 				w->w_ctime = atol(&lbuf[1]);
67625687Seric 				i &= ~NEED_T;
67725013Seric 				break;
67858318Seric 
67958318Seric 			  case 'R':
68058318Seric 				if (QueueLimitRecipient != NULL &&
68158318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
68258318Seric 					i &= ~NEED_R;
68358318Seric 				break;
68458318Seric 
68558318Seric 			  case 'S':
68658318Seric 				if (QueueLimitSender != NULL &&
68758318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
68858318Seric 					i &= ~NEED_S;
68958318Seric 				break;
6904632Seric 			}
6914632Seric 		}
6924632Seric 		(void) fclose(cf);
69324953Seric 
69458318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
69558318Seric 		    bitset(NEED_R|NEED_S, i))
69624953Seric 		{
69724953Seric 			/* don't even bother sorting this job in */
69824953Seric 			wn--;
69924953Seric 		}
7004632Seric 	}
7016625Sglickman 	(void) closedir(f);
70210090Seric 	wn++;
7034632Seric 
7044632Seric 	/*
7054632Seric 	**  Sort the work directory.
7064632Seric 	*/
7074632Seric 
70825687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7094632Seric 
7104632Seric 	/*
7114632Seric 	**  Convert the work list into canonical form.
7129377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7134632Seric 	*/
7144632Seric 
71524981Seric 	WorkQ = NULL;
71625687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7174632Seric 	{
7184632Seric 		w = (WORK *) xalloc(sizeof *w);
7194632Seric 		w->w_name = wlist[i].w_name;
7204632Seric 		w->w_pri = wlist[i].w_pri;
72125013Seric 		w->w_ctime = wlist[i].w_ctime;
72224981Seric 		w->w_next = WorkQ;
72324981Seric 		WorkQ = w;
7244632Seric 	}
7254632Seric 
7267677Seric 	if (tTd(40, 1))
7274632Seric 	{
7284632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7295037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7304632Seric 	}
73110070Seric 
73210090Seric 	return (wn);
7334632Seric }
7344632Seric /*
7357677Seric **  WORKCMPF -- compare function for ordering work.
7364632Seric **
7374632Seric **	Parameters:
7384632Seric **		a -- the first argument.
7394632Seric **		b -- the second argument.
7404632Seric **
7414632Seric **	Returns:
74224981Seric **		-1 if a < b
74324981Seric **		 0 if a == b
74424981Seric **		+1 if a > b
7454632Seric **
7464632Seric **	Side Effects:
7474632Seric **		none.
7484632Seric */
7494632Seric 
7504632Seric workcmpf(a, b)
7515037Seric 	register WORK *a;
7525037Seric 	register WORK *b;
7534632Seric {
75457438Seric 	long pa = a->w_pri;
75557438Seric 	long pb = b->w_pri;
75624941Seric 
75724941Seric 	if (pa == pb)
7584632Seric 		return (0);
75924941Seric 	else if (pa > pb)
76024981Seric 		return (1);
76124981Seric 	else
76210121Seric 		return (-1);
7634632Seric }
7644632Seric /*
7654632Seric **  DOWORK -- do a work request.
7664632Seric **
7674632Seric **	Parameters:
76858884Seric **		id -- the ID of the job to run.
76958884Seric **		forkflag -- if set, run this in background.
77058924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
77158924Seric **			This is used when expanding aliases in the queue.
77261094Seric **			If forkflag is also set, it doesn't wait for the
77361094Seric **			child.
77458884Seric **		e - the envelope in which to run it.
7754632Seric **
7764632Seric **	Returns:
777*64296Seric **		process id of process that is running the queue job.
7784632Seric **
7794632Seric **	Side Effects:
7804632Seric **		The work request is satisfied if possible.
7814632Seric */
7824632Seric 
783*64296Seric pid_t
78458924Seric dowork(id, forkflag, requeueflag, e)
78558884Seric 	char *id;
78658884Seric 	bool forkflag;
78758924Seric 	bool requeueflag;
78855012Seric 	register ENVELOPE *e;
7894632Seric {
790*64296Seric 	register pid_t pid;
79151920Seric 	extern bool readqf();
7924632Seric 
7937677Seric 	if (tTd(40, 1))
79458884Seric 		printf("dowork(%s)\n", id);
7954632Seric 
7964632Seric 	/*
79724941Seric 	**  Fork for work.
79824941Seric 	*/
79924941Seric 
80058884Seric 	if (forkflag)
80124941Seric 	{
802*64296Seric 		pid = fork();
803*64296Seric 		if (pid < 0)
80424941Seric 		{
80524941Seric 			syserr("dowork: cannot fork");
806*64296Seric 			return 0;
80724941Seric 		}
80824941Seric 	}
80924941Seric 	else
81024941Seric 	{
811*64296Seric 		pid = 0;
81224941Seric 	}
81324941Seric 
814*64296Seric 	if (pid == 0)
8154632Seric 	{
8164632Seric 		/*
8174632Seric 		**  CHILD
8188148Seric 		**	Lock the control file to avoid duplicate deliveries.
8198148Seric 		**		Then run the file as though we had just read it.
8207350Seric 		**	We save an idea of the temporary name so we
8217350Seric 		**		can recover on interrupt.
8224632Seric 		*/
8234632Seric 
8247763Seric 		/* set basic modes, etc. */
8257356Seric 		(void) alarm(0);
82655012Seric 		clearenvelope(e, FALSE);
82758737Seric 		e->e_flags |= EF_QUEUERUN;
82858734Seric 		e->e_errormode = EM_MAIL;
82958884Seric 		e->e_id = id;
83063846Seric 		if (forkflag)
831*64296Seric 			disconnect(1, e);
8327876Seric # ifdef LOG
83358020Seric 		if (LogLevel > 76)
83455012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8357881Seric 			       getpid());
83656795Seric # endif /* LOG */
8377763Seric 
8387763Seric 		/* don't use the headers from sendmail.cf... */
83955012Seric 		e->e_header = NULL;
8407763Seric 
84151920Seric 		/* read the queue control file -- return if locked */
84263850Seric 		if (!readqf(e, !requeueflag))
8436980Seric 		{
84458884Seric 			if (tTd(40, 4))
84558884Seric 				printf("readqf(%s) failed\n", e->e_id);
84658884Seric 			if (forkflag)
84724941Seric 				exit(EX_OK);
84824941Seric 			else
84924941Seric 				return;
8506980Seric 		}
8516980Seric 
85255012Seric 		e->e_flags |= EF_INQUEUE;
85358929Seric 		eatheader(e, requeueflag);
8546980Seric 
85558924Seric 		if (requeueflag)
85658924Seric 			queueup(e, TRUE, FALSE);
85758924Seric 
8586980Seric 		/* do the delivery */
85958915Seric 		sendall(e, SM_DELIVER);
8606980Seric 
8616980Seric 		/* finish up and exit */
86258884Seric 		if (forkflag)
86324941Seric 			finis();
86424941Seric 		else
86555012Seric 			dropenvelope(e);
8664632Seric 	}
867*64296Seric 	e->e_id = NULL;
868*64296Seric 	return pid;
8694632Seric }
8704632Seric /*
8714632Seric **  READQF -- read queue file and set up environment.
8724632Seric **
8734632Seric **	Parameters:
8749377Seric **		e -- the envelope of the job to run.
87563850Seric **		announcefile -- if set, announce the name of the queue
87663850Seric **			file in error messages.
8774632Seric **
8784632Seric **	Returns:
87951920Seric **		TRUE if it successfully read the queue file.
88051920Seric **		FALSE otherwise.
8814632Seric **
8824632Seric **	Side Effects:
88351920Seric **		The queue file is returned locked.
8844632Seric */
8854632Seric 
88651920Seric bool
88763850Seric readqf(e, announcefile)
8889377Seric 	register ENVELOPE *e;
88963850Seric 	bool announcefile;
8904632Seric {
89117477Seric 	register FILE *qfp;
89254974Seric 	ADDRESS *ctladdr;
89356400Seric 	struct stat st;
89457135Seric 	char *bp;
89558915Seric 	char qf[20];
89657135Seric 	char buf[MAXLINE];
89724954Seric 	extern long atol();
89854974Seric 	extern ADDRESS *setctluser();
8994632Seric 
9004632Seric 	/*
90117468Seric 	**  Read and process the file.
9024632Seric 	*/
9034632Seric 
90458915Seric 	strcpy(qf, queuename(e, 'q'));
90551937Seric 	qfp = fopen(qf, "r+");
90617477Seric 	if (qfp == NULL)
90717477Seric 	{
90858884Seric 		if (tTd(40, 8))
90958884Seric 			printf("readqf(%s): fopen failure (%s)\n",
91058884Seric 				qf, errstring(errno));
91140934Srick 		if (errno != ENOENT)
91240934Srick 			syserr("readqf: no control file %s", qf);
91351920Seric 		return FALSE;
91417477Seric 	}
91540934Srick 
91664277Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
91764277Seric 	{
91864277Seric 		/* being processed by another queuer */
91964277Seric 		if (tTd(40, 8))
92064277Seric 			printf("readqf(%s): locked\n", qf);
92164277Seric 		if (Verbose)
92264277Seric 			printf("%s: locked\n", e->e_id);
92364277Seric # ifdef LOG
92464277Seric 		if (LogLevel > 19)
92564277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
92664277Seric # endif /* LOG */
92764277Seric 		(void) fclose(qfp);
92864277Seric 		return FALSE;
92964277Seric 	}
93064277Seric 
93156400Seric 	/*
93256400Seric 	**  Check the queue file for plausibility to avoid attacks.
93356400Seric 	*/
93456400Seric 
93556400Seric 	if (fstat(fileno(qfp), &st) < 0)
93656400Seric 	{
93756400Seric 		/* must have been being processed by someone else */
93858884Seric 		if (tTd(40, 8))
93958884Seric 			printf("readqf(%s): fstat failure (%s)\n",
94058884Seric 				qf, errstring(errno));
94156400Seric 		fclose(qfp);
94256400Seric 		return FALSE;
94356400Seric 	}
94456400Seric 
94564137Seric 	if (st.st_uid != geteuid())
94656400Seric 	{
94756400Seric # ifdef LOG
94856400Seric 		if (LogLevel > 0)
94956400Seric 		{
95056400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
95156400Seric 				e->e_id, st.st_uid, st.st_mode);
95256400Seric 		}
95356795Seric # endif /* LOG */
95458884Seric 		if (tTd(40, 8))
95558884Seric 			printf("readqf(%s): bogus file\n", qf);
95664277Seric 		rename(qf, queuename(e, 'Q'));
95756400Seric 		fclose(qfp);
95856400Seric 		return FALSE;
95956400Seric 	}
96056400Seric 
96164277Seric 	if (st.st_size == 0)
96240934Srick 	{
96364277Seric 		/* must be a bogus file -- just remove it */
96464277Seric 		(void) unlink(qf);
96564277Seric 		fclose(qfp);
96651920Seric 		return FALSE;
96740934Srick 	}
96840934Srick 
96964277Seric 	if (st.st_nlink == 0)
97059101Seric 	{
97164277Seric 		/*
97264277Seric 		**  Race condition -- we got a file just as it was being
97364277Seric 		**  unlinked.  Just assume it is zero length.
97464277Seric 		*/
97564277Seric 
97659101Seric 		fclose(qfp);
97759101Seric 		return FALSE;
97859101Seric 	}
97959101Seric 
98064277Seric 	/* good file -- save this lock */
98151920Seric 	e->e_lockfp = qfp;
98251920Seric 
98340934Srick 	/* do basic system initialization */
98455012Seric 	initsys(e);
98540934Srick 
98663850Seric 	if (announcefile)
98763850Seric 		FileName = qf;
9889377Seric 	LineNumber = 0;
98963850Seric 	e->e_flags |= EF_GLOBALERRS;
99063850Seric 	OpMode = MD_DELIVER;
99151920Seric 	if (Verbose)
9929377Seric 		printf("\nRunning %s\n", e->e_id);
99354974Seric 	ctladdr = NULL;
99457135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9954632Seric 	{
99658737Seric 		register char *p;
99757529Seric 		struct stat st;
99857529Seric 
99926504Seric 		if (tTd(40, 4))
100057135Seric 			printf("+++++ %s\n", bp);
100157135Seric 		switch (bp[0])
10024632Seric 		{
100340973Sbostic 		  case 'C':		/* specify controlling user */
100457135Seric 			ctladdr = setctluser(&bp[1]);
100540973Sbostic 			break;
100640973Sbostic 
10074632Seric 		  case 'R':		/* specify recipient */
100858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10094632Seric 			break;
10104632Seric 
101125687Seric 		  case 'E':		/* specify error recipient */
101258082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
101325687Seric 			break;
101425687Seric 
10154632Seric 		  case 'H':		/* header */
101657135Seric 			(void) chompheader(&bp[1], FALSE, e);
10174632Seric 			break;
10184632Seric 
101910108Seric 		  case 'M':		/* message */
102057135Seric 			e->e_message = newstr(&bp[1]);
102110108Seric 			break;
102210108Seric 
10234632Seric 		  case 'S':		/* sender */
102458704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10254632Seric 			break;
10264632Seric 
102759093Seric 		  case 'B':		/* body type */
102859093Seric 			e->e_bodytype = newstr(&bp[1]);
102959093Seric 			break;
103059093Seric 
10314632Seric 		  case 'D':		/* data file name */
103257135Seric 			e->e_df = newstr(&bp[1]);
10339544Seric 			e->e_dfp = fopen(e->e_df, "r");
10349544Seric 			if (e->e_dfp == NULL)
103558020Seric 			{
10369377Seric 				syserr("readqf: cannot open %s", e->e_df);
103758020Seric 				e->e_msgsize = -1;
103858020Seric 			}
103958020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
104057529Seric 				e->e_msgsize = st.st_size;
10414632Seric 			break;
10424632Seric 
10437860Seric 		  case 'T':		/* init time */
104457135Seric 			e->e_ctime = atol(&bp[1]);
10454632Seric 			break;
10464632Seric 
10474634Seric 		  case 'P':		/* message priority */
104857135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10494634Seric 			break;
10504634Seric 
105158737Seric 		  case 'F':		/* flag bits */
105258737Seric 			for (p = &bp[1]; *p != '\0'; p++)
105358737Seric 			{
105458737Seric 				switch (*p)
105558737Seric 				{
105658737Seric 				  case 'w':	/* warning sent */
105758737Seric 					e->e_flags |= EF_WARNING;
105858737Seric 					break;
105958737Seric 
106058737Seric 				  case 'r':	/* response */
106158737Seric 					e->e_flags |= EF_RESPONSE;
106258737Seric 					break;
106358737Seric 				}
106458737Seric 			}
106558737Seric 			break;
106658737Seric 
106753400Seric 		  case '$':		/* define macro */
106857135Seric 			define(bp[1], newstr(&bp[2]), e);
106953400Seric 			break;
107053400Seric 
107124941Seric 		  case '\0':		/* blank line; ignore */
107224941Seric 			break;
107324941Seric 
10744632Seric 		  default:
107559700Seric 			syserr("readqf: bad line \"%s\"", e->e_id,
107657135Seric 				LineNumber, bp);
107763753Seric 			fclose(qfp);
107863753Seric 			rename(qf, queuename(e, 'Q'));
107963753Seric 			return FALSE;
10804632Seric 		}
108157135Seric 
108257135Seric 		if (bp != buf)
108357135Seric 			free(bp);
10844632Seric 	}
10859377Seric 
10869377Seric 	FileName = NULL;
108724941Seric 
108824941Seric 	/*
108924941Seric 	**  If we haven't read any lines, this queue file is empty.
109024941Seric 	**  Arrange to remove it without referencing any null pointers.
109124941Seric 	*/
109224941Seric 
109324941Seric 	if (LineNumber == 0)
109424941Seric 	{
109524941Seric 		errno = 0;
109624941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
109724941Seric 	}
109851920Seric 	return TRUE;
10994632Seric }
11004632Seric /*
11019630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11029630Seric **
11039630Seric **	Parameters:
11049630Seric **		none.
11059630Seric **
11069630Seric **	Returns:
11079630Seric **		none.
11089630Seric **
11099630Seric **	Side Effects:
11109630Seric **		Prints a listing of the mail queue on the standard output.
11119630Seric */
11125182Seric 
11139630Seric printqueue()
11149630Seric {
11159630Seric 	register WORK *w;
11169630Seric 	FILE *f;
111710070Seric 	int nrequests;
11189630Seric 	char buf[MAXLINE];
11199630Seric 
11209630Seric 	/*
112158250Seric 	**  Check for permission to print the queue
112258250Seric 	*/
112358250Seric 
112463787Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0)
112558250Seric 	{
112658250Seric 		struct stat st;
112758523Seric # ifdef NGROUPS
112858523Seric 		int n;
112963937Seric 		GIDSET_T gidset[NGROUPS];
113058523Seric # endif
113158250Seric 
113258517Seric 		if (stat(QueueDir, &st) < 0)
113358250Seric 		{
113458250Seric 			syserr("Cannot stat %s", QueueDir);
113558250Seric 			return;
113658250Seric 		}
113758523Seric # ifdef NGROUPS
113858523Seric 		n = getgroups(NGROUPS, gidset);
113958523Seric 		while (--n >= 0)
114058523Seric 		{
114158523Seric 			if (gidset[n] == st.st_gid)
114258523Seric 				break;
114358523Seric 		}
114458523Seric 		if (n < 0)
114558523Seric # else
114663787Seric 		if (RealGid != st.st_gid)
114758523Seric # endif
114858250Seric 		{
114958250Seric 			usrerr("510 You are not permitted to see the queue");
115058250Seric 			setstat(EX_NOPERM);
115158250Seric 			return;
115258250Seric 		}
115358250Seric 	}
115458250Seric 
115558250Seric 	/*
11569630Seric 	**  Read and order the queue.
11579630Seric 	*/
11589630Seric 
115924941Seric 	nrequests = orderq(TRUE);
11609630Seric 
11619630Seric 	/*
11629630Seric 	**  Print the work list that we have read.
11639630Seric 	*/
11649630Seric 
11659630Seric 	/* first see if there is anything */
116610070Seric 	if (nrequests <= 0)
11679630Seric 	{
116810070Seric 		printf("Mail queue is empty\n");
11699630Seric 		return;
11709630Seric 	}
11719630Seric 
117251920Seric 	CurrentLA = getla();	/* get load average */
117340934Srick 
117410096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
117525687Seric 	if (nrequests > QUEUESIZE)
117625687Seric 		printf(", only %d printed", QUEUESIZE);
117724979Seric 	if (Verbose)
117858716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
117924979Seric 	else
118058716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11819630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11829630Seric 	{
11839630Seric 		struct stat st;
118410070Seric 		auto time_t submittime = 0;
118510070Seric 		long dfsize = -1;
118658737Seric 		int flags = 0;
118710108Seric 		char message[MAXLINE];
118859093Seric 		char bodytype[MAXNAME];
11899630Seric 
119017468Seric 		f = fopen(w->w_name, "r");
119117468Seric 		if (f == NULL)
119217468Seric 		{
119317468Seric 			errno = 0;
119417468Seric 			continue;
119517468Seric 		}
119658724Seric 		printf("%8s", w->w_name + 2);
119758689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
119810070Seric 			printf("*");
119957438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
120024941Seric 			printf("X");
120110070Seric 		else
120210070Seric 			printf(" ");
120310070Seric 		errno = 0;
120417468Seric 
120559093Seric 		message[0] = bodytype[0] = '\0';
12069630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12079630Seric 		{
120853400Seric 			register int i;
120958737Seric 			register char *p;
121053400Seric 
12119630Seric 			fixcrlf(buf, TRUE);
12129630Seric 			switch (buf[0])
12139630Seric 			{
121410108Seric 			  case 'M':	/* error message */
121553400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
121658737Seric 					i = sizeof message - 1;
121753400Seric 				bcopy(&buf[1], message, i);
121853400Seric 				message[i] = '\0';
121910108Seric 				break;
122010108Seric 
122159093Seric 			  case 'B':	/* body type */
122259093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
122359093Seric 					i = sizeof bodytype - 1;
122459093Seric 				bcopy(&buf[1], bodytype, i);
122559093Seric 				bodytype[i] = '\0';
122659093Seric 				break;
122759093Seric 
12289630Seric 			  case 'S':	/* sender name */
122924979Seric 				if (Verbose)
123058737Seric 					printf("%8ld %10ld%c%.12s %.38s",
123158737Seric 					    dfsize,
123258737Seric 					    w->w_pri,
123358737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
123458737Seric 					    ctime(&submittime) + 4,
123524979Seric 					    &buf[1]);
123624979Seric 				else
123724979Seric 					printf("%8ld %.16s %.45s", dfsize,
123824979Seric 					    ctime(&submittime), &buf[1]);
123959093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
124059093Seric 				{
124159093Seric 					printf("\n    %10.10s", bodytype);
124259093Seric 					if (message[0] != '\0')
124359093Seric 						printf("   (%.60s)", message);
124459093Seric 				}
12459630Seric 				break;
124651920Seric 
124740973Sbostic 			  case 'C':	/* controlling user */
124854974Seric 				if (Verbose)
124958716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
125058716Seric 						&buf[1]);
125140973Sbostic 				break;
12529630Seric 
12539630Seric 			  case 'R':	/* recipient name */
125424979Seric 				if (Verbose)
125558716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
125624979Seric 				else
125758716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
12589630Seric 				break;
12599630Seric 
12609630Seric 			  case 'T':	/* creation time */
126124941Seric 				submittime = atol(&buf[1]);
12629630Seric 				break;
126310070Seric 
126410070Seric 			  case 'D':	/* data file name */
126510070Seric 				if (stat(&buf[1], &st) >= 0)
126610070Seric 					dfsize = st.st_size;
126710070Seric 				break;
126858737Seric 
126958737Seric 			  case 'F':	/* flag bits */
127058737Seric 				for (p = &buf[1]; *p != '\0'; p++)
127158737Seric 				{
127258737Seric 					switch (*p)
127358737Seric 					{
127458737Seric 					  case 'w':
127558737Seric 						flags |= EF_WARNING;
127658737Seric 						break;
127758737Seric 					}
127858737Seric 				}
12799630Seric 			}
12809630Seric 		}
128110070Seric 		if (submittime == (time_t) 0)
128210070Seric 			printf(" (no control file)");
12839630Seric 		printf("\n");
128423098Seric 		(void) fclose(f);
12859630Seric 	}
12869630Seric }
12879630Seric 
128856795Seric # endif /* QUEUE */
128917468Seric /*
129017468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
129117468Seric **
129217468Seric **	Assigns an id code if one does not already exist.
129317468Seric **	This code is very careful to avoid trashing existing files
129417468Seric **	under any circumstances.
129517468Seric **
129617468Seric **	Parameters:
129717468Seric **		e -- envelope to build it in/from.
129817468Seric **		type -- the file type, used as the first character
129917468Seric **			of the file name.
130017468Seric **
130117468Seric **	Returns:
130217468Seric **		a pointer to the new file name (in a static buffer).
130317468Seric **
130417468Seric **	Side Effects:
130551920Seric **		If no id code is already assigned, queuename will
130651920Seric **		assign an id code, create a qf file, and leave a
130751920Seric **		locked, open-for-write file pointer in the envelope.
130817468Seric */
130917468Seric 
131017468Seric char *
131117468Seric queuename(e, type)
131217468Seric 	register ENVELOPE *e;
131359700Seric 	int type;
131417468Seric {
131517468Seric 	static int pid = -1;
131658741Seric 	static char c0;
131758741Seric 	static char c1;
131858741Seric 	static char c2;
131958716Seric 	time_t now;
132058716Seric 	struct tm *tm;
132158689Seric 	static char buf[MAXNAME];
132217468Seric 
132317468Seric 	if (e->e_id == NULL)
132417468Seric 	{
132517468Seric 		char qf[20];
132617468Seric 
132717468Seric 		/* find a unique id */
132817468Seric 		if (pid != getpid())
132917468Seric 		{
133017468Seric 			/* new process -- start back at "AA" */
133117468Seric 			pid = getpid();
133258716Seric 			now = curtime();
133358716Seric 			tm = localtime(&now);
133458716Seric 			c0 = 'A' + tm->tm_hour;
133517468Seric 			c1 = 'A';
133617468Seric 			c2 = 'A' - 1;
133717468Seric 		}
133858716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
133917468Seric 
134017468Seric 		while (c1 < '~' || c2 < 'Z')
134117468Seric 		{
134217468Seric 			int i;
134317468Seric 
134417468Seric 			if (c2 >= 'Z')
134517468Seric 			{
134617468Seric 				c1++;
134717468Seric 				c2 = 'A' - 1;
134817468Seric 			}
134958716Seric 			qf[3] = c1;
135058716Seric 			qf[4] = ++c2;
135117468Seric 			if (tTd(7, 20))
135240934Srick 				printf("queuename: trying \"%s\"\n", qf);
135317468Seric 
135440934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
135551920Seric 			if (i < 0)
135651920Seric 			{
135751920Seric 				if (errno == EEXIST)
135851920Seric 					continue;
135951920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
136051920Seric 					qf, QueueDir);
136151920Seric 				exit(EX_UNAVAILABLE);
136251920Seric 			}
136358689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
136451920Seric 			{
136551920Seric 				e->e_lockfp = fdopen(i, "w");
136640934Srick 				break;
136717468Seric 			}
136851920Seric 
136951920Seric 			/* a reader got the file; abandon it and try again */
137051920Seric 			(void) close(i);
137117468Seric 		}
137217468Seric 		if (c1 >= '~' && c2 >= 'Z')
137317468Seric 		{
137417468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
137517468Seric 				qf, QueueDir);
137617468Seric 			exit(EX_OSERR);
137717468Seric 		}
137817468Seric 		e->e_id = newstr(&qf[2]);
137917468Seric 		define('i', e->e_id, e);
138017468Seric 		if (tTd(7, 1))
138117468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
138217468Seric # ifdef LOG
138358020Seric 		if (LogLevel > 93)
138417468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
138556795Seric # endif /* LOG */
138617468Seric 	}
138717468Seric 
138817468Seric 	if (type == '\0')
138917468Seric 		return (NULL);
139017468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
139117468Seric 	if (tTd(7, 2))
139217468Seric 		printf("queuename: %s\n", buf);
139317468Seric 	return (buf);
139417468Seric }
139517468Seric /*
139617468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
139717468Seric **
139817468Seric **	Parameters:
139917468Seric **		e -- the envelope to unlock.
140017468Seric **
140117468Seric **	Returns:
140217468Seric **		none
140317468Seric **
140417468Seric **	Side Effects:
140517468Seric **		unlocks the queue for `e'.
140617468Seric */
140717468Seric 
140817468Seric unlockqueue(e)
140917468Seric 	ENVELOPE *e;
141017468Seric {
141158680Seric 	if (tTd(51, 4))
141258680Seric 		printf("unlockqueue(%s)\n", e->e_id);
141358680Seric 
141451920Seric 	/* if there is a lock file in the envelope, close it */
141551920Seric 	if (e->e_lockfp != NULL)
141658680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
141751920Seric 	e->e_lockfp = NULL;
141851920Seric 
141958728Seric 	/* don't create a queue id if we don't already have one */
142058728Seric 	if (e->e_id == NULL)
142158728Seric 		return;
142258728Seric 
142317468Seric 	/* remove the transcript */
142417468Seric # ifdef LOG
142558020Seric 	if (LogLevel > 87)
142617468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
142756795Seric # endif /* LOG */
142858680Seric 	if (!tTd(51, 104))
142917468Seric 		xunlink(queuename(e, 'x'));
143017468Seric 
143117468Seric }
143240973Sbostic /*
143354974Seric **  SETCTLUSER -- create a controlling address
143440973Sbostic **
143554974Seric **	Create a fake "address" given only a local login name; this is
143654974Seric **	used as a "controlling user" for future recipient addresses.
143740973Sbostic **
143840973Sbostic **	Parameters:
143954974Seric **		user -- the user name of the controlling user.
144040973Sbostic **
144140973Sbostic **	Returns:
144254974Seric **		An address descriptor for the controlling user.
144340973Sbostic **
144440973Sbostic **	Side Effects:
144540973Sbostic **		none.
144640973Sbostic */
144740973Sbostic 
144854974Seric ADDRESS *
144954974Seric setctluser(user)
145054974Seric 	char *user;
145140973Sbostic {
145254974Seric 	register ADDRESS *a;
145340973Sbostic 	struct passwd *pw;
145459113Seric 	char *p;
145540973Sbostic 
145640973Sbostic 	/*
145754974Seric 	**  See if this clears our concept of controlling user.
145840973Sbostic 	*/
145940973Sbostic 
146063850Seric 	if (user == NULL || *user == '\0')
146163850Seric 		return NULL;
146240973Sbostic 
146340973Sbostic 	/*
146454974Seric 	**  Set up addr fields for controlling user.
146540973Sbostic 	*/
146640973Sbostic 
146754974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
146854974Seric 	bzero((char *) a, sizeof *a);
146959113Seric 
147059113Seric 	p = strchr(user, ':');
147159113Seric 	if (p != NULL)
147259113Seric 		*p++ = '\0';
147359270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
147440973Sbostic 	{
147540973Sbostic 		a->q_home = newstr(pw->pw_dir);
147640973Sbostic 		a->q_uid = pw->pw_uid;
147740973Sbostic 		a->q_gid = pw->pw_gid;
147857642Seric 		a->q_user = newstr(user);
147959270Seric 		a->q_flags |= QGOODUID;
148040973Sbostic 	}
148140973Sbostic 	else
148240973Sbostic 	{
148357642Seric 		a->q_user = newstr(DefUser);
148440973Sbostic 	}
148540973Sbostic 
148659270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
148756328Seric 	a->q_mailer = LocalMailer;
148859113Seric 	if (p == NULL)
148959113Seric 		a->q_paddr = a->q_user;
149059113Seric 	else
149159113Seric 		a->q_paddr = newstr(p);
149254974Seric 	return a;
149340973Sbostic }
1494