xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 69907)
122708Sdist /*
268839Seric  * Copyright (c) 1983, 1995 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*69907Seric static char sccsid[] = "@(#)queue.c	8.88 (Berkeley) 06/16/95 (with queueing)";
1433731Sbostic #else
15*69907Seric static char sccsid[] = "@(#)queue.c	8.88 (Berkeley) 06/16/95 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2057736Seric # include <dirent.h>
214632Seric 
2233731Sbostic # ifdef QUEUE
234632Seric 
244632Seric /*
259377Seric **  Work queue.
269377Seric */
279377Seric 
289377Seric struct work
299377Seric {
309377Seric 	char		*w_name;	/* name of control file */
3168481Seric 	char		*w_host;	/* name of recipient host */
3268481Seric 	bool		w_lock;		/* is message locked? */
339377Seric 	long		w_pri;		/* priority of message, see below */
3425013Seric 	time_t		w_ctime;	/* creation time of message */
359377Seric 	struct work	*w_next;	/* next in queue */
369377Seric };
379377Seric 
389377Seric typedef struct work	WORK;
399377Seric 
409377Seric WORK	*WorkQ;			/* queue of things to be done */
4168481Seric 
4268481Seric #define QF_VERSION	1	/* version number of this queue format */
439377Seric /*
444632Seric **  QUEUEUP -- queue a message up for future transmission.
454632Seric **
464632Seric **	Parameters:
476980Seric **		e -- the envelope to queue up.
486999Seric **		queueall -- if TRUE, queue all addresses, rather than
496999Seric **			just those with the QQUEUEUP flag set.
509377Seric **		announce -- if TRUE, tell when you are queueing up.
514632Seric **
524632Seric **	Returns:
5351920Seric **		none.
544632Seric **
554632Seric **	Side Effects:
569377Seric **		The current request are saved in a control file.
5751920Seric **		The queue file is left locked.
584632Seric */
594632Seric 
6069748Seric void
queueup(e,queueall,announce)619377Seric queueup(e, queueall, announce)
626980Seric 	register ENVELOPE *e;
636999Seric 	bool queueall;
649377Seric 	bool announce;
654632Seric {
667812Seric 	char *qf;
677812Seric 	register FILE *tfp;
684632Seric 	register HDR *h;
695007Seric 	register ADDRESS *q;
7051920Seric 	int fd;
7151920Seric 	int i;
7251920Seric 	bool newid;
7353400Seric 	register char *p;
7410173Seric 	MAILER nullmailer;
7565870Seric 	MCI mcibuf;
7651920Seric 	char buf[MAXLINE], tf[MAXLINE];
7769748Seric 	extern void printctladdr __P((ADDRESS *, FILE *));
784632Seric 
795037Seric 	/*
8017477Seric 	**  Create control file.
815037Seric 	*/
824632Seric 
8364745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
8464277Seric 
8564277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8651920Seric 	strcpy(tf, queuename(e, 't'));
8751920Seric 	tfp = e->e_lockfp;
8851920Seric 	if (tfp == NULL)
8951920Seric 		newid = FALSE;
9064277Seric 
9164277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
9264745Seric 	if (!newid)
9351835Seric 	{
9451920Seric 		/* get a locked tf file */
9564070Seric 		for (i = 0; i < 128; i++)
9651835Seric 		{
9751920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9851920Seric 			if (fd < 0)
9951835Seric 			{
10064070Seric 				if (errno != EEXIST)
10164070Seric 					break;
10264070Seric #ifdef LOG
10364070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10465090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
10565090Seric 						tf, geteuid(), errstring(errno));
10664070Seric #endif
10741636Srick 			}
10865090Seric 			else
10965090Seric 			{
11065090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
11165090Seric 					break;
11264070Seric #ifdef LOG
11365090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
11465090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11565090Seric 						tf, errstring(errno));
11664070Seric #endif
11765090Seric 				close(fd);
11865090Seric 			}
11958689Seric 
12064070Seric 			if ((i % 32) == 31)
12164070Seric 			{
12264070Seric 				/* save the old temp file away */
12364070Seric 				(void) rename(tf, queuename(e, 'T'));
12464070Seric 			}
12564070Seric 			else
12664070Seric 				sleep(i % 32);
12741636Srick 		}
12864724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12964921Seric 		{
13064921Seric 			printopenfds(TRUE);
13165090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
13265090Seric 				tf, geteuid());
13364921Seric 		}
13451920Seric 	}
1354632Seric 
1367677Seric 	if (tTd(40, 1))
13768481Seric 		printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id,
13868481Seric 			newid ? " (new id)" : "", queueall);
13968603Seric 	if (tTd(40, 3))
14068603Seric 	{
14168603Seric 		extern void printenvflags();
14268603Seric 
14368603Seric 		printf("  e_flags=");
14468603Seric 		printenvflags(e);
14568603Seric 	}
14668481Seric 	if (tTd(40, 32))
14768481Seric 	{
14868481Seric 		printf("  sendq=");
14968481Seric 		printaddr(e->e_sendqueue, TRUE);
15068481Seric 	}
15164745Seric 	if (tTd(40, 9))
15264745Seric 	{
15364745Seric 		printf("  tfp=");
15464745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
15564745Seric 		printf("  lockfp=");
15664745Seric 		if (e->e_lockfp == NULL)
15764745Seric 			printf("NULL\n");
15864745Seric 		else
15964745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
16064745Seric 	}
1614632Seric 
1624632Seric 	/*
1636980Seric 	**  If there is no data file yet, create one.
1646980Seric 	*/
1656980Seric 
16668564Seric 	if (!bitset(EF_HAS_DF, e->e_flags))
1676980Seric 	{
1686980Seric 		register FILE *dfp;
16968564Seric 		char dfname[20];
17068481Seric 		struct stat stbuf;
1716980Seric 
17268564Seric 		strcpy(dfname, queuename(e, 'd'));
17368564Seric 		fd = open(dfname, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
17464724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
17565090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
17668564Seric 				dfname, geteuid());
17768481Seric 		if (fstat(fd, &stbuf) < 0)
17868481Seric 			e->e_dfino = -1;
17968481Seric 		else
18068481Seric 		{
18168481Seric 			e->e_dfdev = stbuf.st_dev;
18268481Seric 			e->e_dfino = stbuf.st_ino;
18368481Seric 		}
18468564Seric 		e->e_flags |= EF_HAS_DF;
18565870Seric 		bzero(&mcibuf, sizeof mcibuf);
18665870Seric 		mcibuf.mci_out = dfp;
18765870Seric 		mcibuf.mci_mailer = FileMailer;
18868228Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
18958680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1909389Seric 		e->e_putbody = putbody;
1916980Seric 	}
1926980Seric 
1936980Seric 	/*
1944632Seric 	**  Output future work requests.
19525687Seric 	**	Priority and creation time should be first, since
19625687Seric 	**	they are required by orderq.
1974632Seric 	*/
1984632Seric 
19968481Seric 	/* output queue version number (must be first!) */
20068481Seric 	fprintf(tfp, "V%d\n", QF_VERSION);
20168481Seric 
2029377Seric 	/* output message priority */
2039377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
2049377Seric 
2059630Seric 	/* output creation time */
2069630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
2079630Seric 
20868481Seric 	/* output last delivery time */
20968481Seric 	fprintf(tfp, "K%ld\n", e->e_dtime);
21068481Seric 
21168481Seric 	/* output number of delivery attempts */
21268481Seric 	fprintf(tfp, "N%d\n", e->e_ntries);
21368481Seric 
21468481Seric 	/* output inode number of data file */
21568481Seric 	/* XXX should probably include device major/minor too */
21668481Seric 	if (e->e_dfino != -1)
21768481Seric 		fprintf(tfp, "I%d/%d/%ld\n",
21868481Seric 			major(e->e_dfdev), minor(e->e_dfdev), e->e_dfino);
21968481Seric 
22068564Seric 	/* output body type */
22159093Seric 	if (e->e_bodytype != NULL)
22259093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
2234632Seric 
22410108Seric 	/* message from envelope, if it exists */
22510108Seric 	if (e->e_message != NULL)
22668478Seric 		fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE));
22710108Seric 
22858737Seric 	/* send various flag bits through */
22958737Seric 	p = buf;
23058737Seric 	if (bitset(EF_WARNING, e->e_flags))
23158737Seric 		*p++ = 'w';
23258737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
23358737Seric 		*p++ = 'r';
23468481Seric 	if (bitset(EF_HAS8BIT, e->e_flags))
23568481Seric 		*p++ = '8';
23658737Seric 	*p++ = '\0';
23758737Seric 	if (buf[0] != '\0')
23858737Seric 		fprintf(tfp, "F%s\n", buf);
23958737Seric 
24058957Seric 	/* $r and $s and $_ macro values */
24153400Seric 	if ((p = macvalue('r', e)) != NULL)
24268478Seric 		fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE));
24353400Seric 	if ((p = macvalue('s', e)) != NULL)
24468478Seric 		fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE));
24558957Seric 	if ((p = macvalue('_', e)) != NULL)
24668478Seric 		fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE));
24753400Seric 
2484632Seric 	/* output name of sender */
24968481Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
25068481Seric 		p = e->e_sender;
25168481Seric 	else
25268481Seric 		p = e->e_from.q_paddr;
25368481Seric 	fprintf(tfp, "S%s\n", denlstring(p, TRUE, FALSE));
2544632Seric 
25568481Seric 	/* output ESMTP-supplied "original" information */
25668481Seric 	if (e->e_envid != NULL)
25768481Seric 		fprintf(tfp, "Z%s\n", denlstring(e->e_envid, TRUE, FALSE));
25868481Seric 
25968558Seric 	/* output list of recipient addresses */
26059670Seric 	printctladdr(NULL, NULL);
2616980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2624632Seric 	{
26358250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
26458680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2658245Seric 		{
26659113Seric 			printctladdr(q, tfp);
26768481Seric 			if (q->q_orcpt != NULL)
26868481Seric 				fprintf(tfp, "Q%s\n",
26968481Seric 					denlstring(q->q_orcpt, TRUE, FALSE));
27068481Seric 			putc('R', tfp);
27168481Seric 			if (bitset(QPRIMARY, q->q_flags))
27268481Seric 				putc('P', tfp);
27368481Seric 			if (bitset(QPINGONSUCCESS, q->q_flags))
27468481Seric 				putc('S', tfp);
27568481Seric 			if (bitset(QPINGONFAILURE, q->q_flags))
27668481Seric 				putc('F', tfp);
27768481Seric 			if (bitset(QPINGONDELAY, q->q_flags))
27868481Seric 				putc('D', tfp);
27968481Seric 			putc(':', tfp);
28068481Seric 			fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE));
2819377Seric 			if (announce)
2829377Seric 			{
2839377Seric 				e->e_to = q->q_paddr;
28458151Seric 				message("queued");
28558020Seric 				if (LogLevel > 8)
28668481Seric 					logdelivery(NULL, NULL, "queued",
28768481Seric 						    NULL, (time_t) 0, e);
2889377Seric 				e->e_to = NULL;
2899377Seric 			}
2909387Seric 			if (tTd(40, 1))
2919387Seric 			{
2929387Seric 				printf("queueing ");
2939387Seric 				printaddr(q, FALSE);
2949387Seric 			}
2958245Seric 		}
2964632Seric 	}
2974632Seric 
2989377Seric 	/*
2999377Seric 	**  Output headers for this message.
3009377Seric 	**	Expand macros completely here.  Queue run will deal with
3019377Seric 	**	everything as absolute headers.
3029377Seric 	**		All headers that must be relative to the recipient
3039377Seric 	**		can be cracked later.
30410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
30510173Seric 	**	no effect on the addresses as they are output.
3069377Seric 	*/
3079377Seric 
30810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
30958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
31065584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
31110349Seric 	nullmailer.m_eol = "\n";
31265870Seric 	bzero(&mcibuf, sizeof mcibuf);
31365870Seric 	mcibuf.mci_mailer = &nullmailer;
31465870Seric 	mcibuf.mci_out = tfp;
31510173Seric 
31658050Seric 	define('g', "\201f", e);
3176980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3184632Seric 	{
31910686Seric 		extern bool bitzerop();
32010686Seric 
32112015Seric 		/* don't output null headers */
3224632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
3234632Seric 			continue;
32412015Seric 
32512015Seric 		/* don't output resent headers on non-resent messages */
32612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
32712015Seric 			continue;
32812015Seric 
32965267Seric 		/* expand macros; if null, don't output header at all */
33065267Seric 		if (bitset(H_DEFAULT, h->h_flags))
33165267Seric 		{
33268529Seric 			(void) expand(h->h_value, buf, sizeof buf, e);
33365267Seric 			if (buf[0] == '\0')
33465267Seric 				continue;
33565267Seric 		}
33665267Seric 
33712015Seric 		/* output this header */
3387812Seric 		fprintf(tfp, "H");
33912015Seric 
34012015Seric 		/* if conditional, output the set of conditions */
34110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
34210686Seric 		{
34310686Seric 			int j;
34410686Seric 
34523098Seric 			(void) putc('?', tfp);
34610686Seric 			for (j = '\0'; j <= '\177'; j++)
34710686Seric 				if (bitnset(j, h->h_mflags))
34823098Seric 					(void) putc(j, tfp);
34923098Seric 			(void) putc('?', tfp);
35010686Seric 		}
35112015Seric 
35212015Seric 		/* output the header: expand macros, convert addresses */
3537763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3547763Seric 		{
35565267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3567763Seric 		}
3578245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3589348Seric 		{
35958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
36063753Seric 			FILE *savetrace = TrafficLogFile;
36158737Seric 
36263753Seric 			TrafficLogFile = NULL;
36363753Seric 
36458737Seric 			if (bitset(H_FROM, h->h_flags))
36558737Seric 				oldstyle = FALSE;
36658737Seric 
36765870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
36863753Seric 
36963753Seric 			TrafficLogFile = savetrace;
3709348Seric 		}
3717763Seric 		else
3728245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3734632Seric 	}
3744632Seric 
3754632Seric 	/*
3764632Seric 	**  Clean up.
377*69907Seric 	**
378*69907Seric 	**	Write a terminator record -- this is to prevent
379*69907Seric 	**	scurrilous crackers from appending any data.
3804632Seric 	*/
3814632Seric 
382*69907Seric 	fprintf(tfp, ".\n");
383*69907Seric 
38464762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
38558732Seric 	{
38658732Seric 		if (newid)
38758732Seric 			syserr("!552 Error writing control file %s", tf);
38858732Seric 		else
38958732Seric 			syserr("!452 Error writing control file %s", tf);
39058732Seric 	}
39158732Seric 
39251920Seric 	if (!newid)
39351920Seric 	{
39464277Seric 		/* rename (locked) tf to be (locked) qf */
39551920Seric 		qf = queuename(e, 'q');
39651920Seric 		if (rename(tf, qf) < 0)
39768564Seric 			syserr("cannot rename(%s, %s), uid=%d",
39868564Seric 				tf, qf, geteuid());
39964277Seric 
40064277Seric 		/* close and unlock old (locked) qf */
40151920Seric 		if (e->e_lockfp != NULL)
40258680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
40351920Seric 		e->e_lockfp = tfp;
40451920Seric 	}
40551920Seric 	else
40651920Seric 		qf = tf;
40741636Srick 	errno = 0;
40864745Seric 	e->e_flags |= EF_INQUEUE;
4097391Seric 
4107677Seric # ifdef LOG
4117677Seric 	/* save log info */
41258020Seric 	if (LogLevel > 79)
41368564Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s", e->e_id, qf);
41456795Seric # endif /* LOG */
41564307Seric 
41664307Seric 	if (tTd(40, 1))
41764307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
41851920Seric 	return;
4194632Seric }
42054974Seric 
42169748Seric void
printctladdr(a,tfp)42254974Seric printctladdr(a, tfp)
42359113Seric 	register ADDRESS *a;
42454974Seric 	FILE *tfp;
42554974Seric {
42659113Seric 	char *uname;
42759113Seric 	register struct passwd *pw;
42859113Seric 	register ADDRESS *q;
42959113Seric 	uid_t uid;
43059113Seric 	static ADDRESS *lastctladdr;
43159113Seric 	static uid_t lastuid;
43254974Seric 
43359113Seric 	/* initialization */
43463850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
43554974Seric 	{
43659670Seric 		if (lastctladdr != NULL && tfp != NULL)
43759113Seric 			fprintf(tfp, "C\n");
43859113Seric 		lastctladdr = NULL;
43959113Seric 		lastuid = 0;
44054974Seric 		return;
44154974Seric 	}
44259113Seric 
44359113Seric 	/* find the active uid */
44459113Seric 	q = getctladdr(a);
44559113Seric 	if (q == NULL)
44659113Seric 		uid = 0;
44754974Seric 	else
44859113Seric 		uid = q->q_uid;
44963850Seric 	a = a->q_alias;
45059113Seric 
45159113Seric 	/* check to see if this is the same as last time */
45259113Seric 	if (lastctladdr != NULL && uid == lastuid &&
45359113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
45459113Seric 		return;
45559113Seric 	lastuid = uid;
45659113Seric 	lastctladdr = a;
45759113Seric 
45868693Seric 	if (uid == 0 || (pw = sm_getpwuid(uid)) == NULL)
45959270Seric 		uname = "";
46059113Seric 	else
46159113Seric 		uname = pw->pw_name;
46259113Seric 
46368478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
46454974Seric }
4654632Seric /*
4664632Seric **  RUNQUEUE -- run the jobs in the queue.
4674632Seric **
4684632Seric **	Gets the stuff out of the queue in some presumably logical
4694632Seric **	order and processes them.
4704632Seric **
4714632Seric **	Parameters:
47224941Seric **		forkflag -- TRUE if the queue scanning should be done in
47324941Seric **			a child process.  We double-fork so it is not our
47424941Seric **			child and we don't have to clean up after it.
4754632Seric **
4764632Seric **	Returns:
4774632Seric **		none.
4784632Seric **
4794632Seric **	Side Effects:
4804632Seric **		runs things in the mail queue.
4814632Seric */
4824632Seric 
48355360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
48455360Seric 
48568481Seric void
runqueue(forkflag)48655360Seric runqueue(forkflag)
4874639Seric 	bool forkflag;
4884632Seric {
48955360Seric 	register ENVELOPE *e;
49068481Seric 	int njobs;
49168481Seric 	int sequenceno = 0;
49255360Seric 	extern ENVELOPE BlankEnvelope;
49324953Seric 
4947466Seric 	/*
49524953Seric 	**  If no work will ever be selected, don't even bother reading
49624953Seric 	**  the queue.
49724953Seric 	*/
49824953Seric 
49951920Seric 	CurrentLA = getla();	/* get load average */
50040934Srick 
50158132Seric 	if (shouldqueue(0L, curtime()))
50224953Seric 	{
50368854Seric 		char *msg = "Skipping queue run -- load average too high";
50468854Seric 
50524953Seric 		if (Verbose)
50668854Seric 			printf("%s\n", msg);
50768854Seric #ifdef LOG
50868854Seric 		if (LogLevel > 8)
50968854Seric 			syslog(LOG_INFO, "runqueue: %s", msg);
51068854Seric #endif
51158107Seric 		if (forkflag && QueueIntvl != 0)
51258107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
51355360Seric 		return;
51424953Seric 	}
51524953Seric 
51624953Seric 	/*
5177466Seric 	**  See if we want to go off and do other useful work.
5187466Seric 	*/
5194639Seric 
5204639Seric 	if (forkflag)
5214639Seric 	{
5227943Seric 		int pid;
52365223Seric #ifdef SIGCHLD
52465223Seric 		extern void reapchild();
5257943Seric 
52665223Seric 		(void) setsignal(SIGCHLD, reapchild);
52765223Seric #endif
52865223Seric 
5297943Seric 		pid = dofork();
5307943Seric 		if (pid != 0)
5314639Seric 		{
5327943Seric 			/* parent -- pick up intermediate zombie */
53325184Seric #ifndef SIGCHLD
5349377Seric 			(void) waitfor(pid);
53569839Seric #else
53669839Seric 			CurChildren++;
53756795Seric #endif /* SIGCHLD */
5387690Seric 			if (QueueIntvl != 0)
5399348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5404639Seric 			return;
5414639Seric 		}
5427943Seric 		/* child -- double fork */
54325184Seric #ifndef SIGCHLD
5447943Seric 		if (fork() != 0)
5457943Seric 			exit(EX_OK);
54656795Seric #else /* SIGCHLD */
54764035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
54856795Seric #endif /* SIGCHLD */
5494639Seric 	}
55024941Seric 
55140934Srick 	setproctitle("running queue: %s", QueueDir);
55224941Seric 
5537876Seric # ifdef LOG
55458020Seric 	if (LogLevel > 69)
55555360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
55655360Seric 			QueueDir, getpid(), forkflag);
55756795Seric # endif /* LOG */
5584639Seric 
5597466Seric 	/*
56010205Seric 	**  Release any resources used by the daemon code.
56110205Seric 	*/
56210205Seric 
56310205Seric # ifdef DAEMON
56410205Seric 	clrdaemon();
56556795Seric # endif /* DAEMON */
56610205Seric 
56764658Seric 	/* force it to run expensive jobs */
56864658Seric 	NoConnect = FALSE;
56964658Seric 
57010205Seric 	/*
57155360Seric 	**  Create ourselves an envelope
57255360Seric 	*/
57355360Seric 
57455360Seric 	CurEnv = &QueueEnvelope;
57558179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
57655360Seric 	e->e_flags = BlankEnvelope.e_flags;
57755360Seric 
57855360Seric 	/*
57927175Seric 	**  Make sure the alias database is open.
58027175Seric 	*/
58127175Seric 
58260537Seric 	initmaps(FALSE, e);
58327175Seric 
58427175Seric 	/*
5857466Seric 	**  Start making passes through the queue.
5867466Seric 	**	First, read and sort the entire queue.
5877466Seric 	**	Then, process the work in that order.
5887466Seric 	**		But if you take too long, start over.
5897466Seric 	*/
5907466Seric 
5917943Seric 	/* order the existing work requests */
59268481Seric 	njobs = orderq(FALSE);
5937690Seric 
5947943Seric 	/* process them once at a time */
5957943Seric 	while (WorkQ != NULL)
5964639Seric 	{
5977943Seric 		WORK *w = WorkQ;
5987881Seric 
5997943Seric 		WorkQ = WorkQ->w_next;
60058884Seric 
60158884Seric 		/*
60258884Seric 		**  Ignore jobs that are too expensive for the moment.
60358884Seric 		*/
60458884Seric 
60568481Seric 		sequenceno++;
60658884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
60758884Seric 		{
60858884Seric 			if (Verbose)
60968481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
61068481Seric 					w->w_name + 2, sequenceno, njobs);
61158884Seric 		}
61258930Seric 		else
61358930Seric 		{
61464296Seric 			pid_t pid;
61564296Seric 			extern pid_t dowork();
61664296Seric 
61768481Seric 			if (Verbose)
61868481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
61968481Seric 					w->w_name + 2, sequenceno, njobs);
62064296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
62164296Seric 			errno = 0;
62266316Seric 			if (pid != 0)
62366316Seric 				(void) waitfor(pid);
62458930Seric 		}
6257943Seric 		free(w->w_name);
62668481Seric 		if (w->w_host)
62768481Seric 			free(w->w_host);
6287943Seric 		free((char *) w);
6294639Seric 	}
63029866Seric 
63129866Seric 	/* exit without the usual cleanup */
63255467Seric 	e->e_id = NULL;
63355467Seric 	finis();
6344634Seric }
6354634Seric /*
6364632Seric **  ORDERQ -- order the work queue.
6374632Seric **
6384632Seric **	Parameters:
63924941Seric **		doall -- if set, include everything in the queue (even
64024941Seric **			the jobs that cannot be run because the load
64124941Seric **			average is too high).  Otherwise, exclude those
64224941Seric **			jobs.
6434632Seric **
6444632Seric **	Returns:
64510121Seric **		The number of request in the queue (not necessarily
64610121Seric **		the number of requests in WorkQ however).
6474632Seric **
6484632Seric **	Side Effects:
6494632Seric **		Sets WorkQ to the queue of available work, in order.
6504632Seric */
6514632Seric 
65225687Seric # define NEED_P		001
65325687Seric # define NEED_T		002
65458318Seric # define NEED_R		004
65558318Seric # define NEED_S		010
6564632Seric 
65769722Seric static WORK	*WorkList = NULL;
65869722Seric static int	WorkListSize = 0;
65969722Seric 
66069748Seric int
orderq(doall)66124941Seric orderq(doall)
66224941Seric 	bool doall;
6634632Seric {
66460219Seric 	register struct dirent *d;
6654632Seric 	register WORK *w;
6666625Sglickman 	DIR *f;
6674632Seric 	register int i;
66810070Seric 	int wn = -1;
66968481Seric 	int wc;
6704632Seric 
67158318Seric 	if (tTd(41, 1))
67258318Seric 	{
67358318Seric 		printf("orderq:\n");
67458318Seric 		if (QueueLimitId != NULL)
67558318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
67658318Seric 		if (QueueLimitSender != NULL)
67758318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
67858318Seric 		if (QueueLimitRecipient != NULL)
67958318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
68058318Seric 	}
68158318Seric 
6824632Seric 	/* clear out old WorkQ */
6834632Seric 	for (w = WorkQ; w != NULL; )
6844632Seric 	{
6854632Seric 		register WORK *nw = w->w_next;
6864632Seric 
6874632Seric 		WorkQ = nw;
6884632Seric 		free(w->w_name);
68968481Seric 		if (w->w_host)
69068481Seric 			free(w->w_host);
6914632Seric 		free((char *) w);
6924632Seric 		w = nw;
6934632Seric 	}
6944632Seric 
6954632Seric 	/* open the queue directory */
6968148Seric 	f = opendir(".");
6974632Seric 	if (f == NULL)
6984632Seric 	{
6998148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
70010070Seric 		return (0);
7014632Seric 	}
7024632Seric 
7034632Seric 	/*
7044632Seric 	**  Read the work directory.
7054632Seric 	*/
7064632Seric 
70710070Seric 	while ((d = readdir(f)) != NULL)
7084632Seric 	{
7099377Seric 		FILE *cf;
71064492Seric 		register char *p;
71168528Seric 		char lbuf[MAXNAME + 1];
71258318Seric 		extern bool strcontainedin();
7134632Seric 
7144632Seric 		/* is this an interesting entry? */
7157812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
7164632Seric 			continue;
7174632Seric 
71858318Seric 		if (QueueLimitId != NULL &&
71958318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
72058318Seric 			continue;
72158318Seric 
72268563Seric #ifdef PICKY_QF_NAME_CHECK
72358722Seric 		/*
72458722Seric 		**  Check queue name for plausibility.  This handles
72558722Seric 		**  both old and new type ids.
72658722Seric 		*/
72758722Seric 
72864492Seric 		p = d->d_name + 2;
72964492Seric 		if (isupper(p[0]) && isupper(p[2]))
73064492Seric 			p += 3;
73164492Seric 		else if (isupper(p[1]))
73264492Seric 			p += 2;
73364492Seric 		else
73464492Seric 			p = d->d_name;
73564492Seric 		for (i = 0; isdigit(*p); p++)
73664492Seric 			i++;
73764492Seric 		if (i < 5 || *p != '\0')
73858020Seric 		{
73958020Seric 			if (Verbose)
74058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
74168563Seric # ifdef LOG
74268563Seric 			if (LogLevel > 0)
74368563Seric 				syslog(LOG_ALERT, "orderq: bogus qf name %s",
74458020Seric 					d->d_name);
74568563Seric # endif
74668706Seric 			if (strlen(d->d_name) > (SIZE_T) MAXNAME)
74768528Seric 				d->d_name[MAXNAME] = '\0';
74858020Seric 			strcpy(lbuf, d->d_name);
74958020Seric 			lbuf[0] = 'Q';
75058020Seric 			(void) rename(d->d_name, lbuf);
75158020Seric 			continue;
75258020Seric 		}
75368563Seric #endif
75458020Seric 
75568563Seric 		/* open control file (if not too many files) */
75669746Seric 		if (++wn > MaxQueueRun && MaxQueueRun > 0)
75769722Seric 		{
75869746Seric # ifdef LOG
75969746Seric 			if (wn == MaxQueueRun + 1 && LogLevel > 0)
76069746Seric 				syslog(LOG_ALERT, "WorkList for %s maxed out at %d",
76169746Seric 						QueueDir, MaxQueueRun);
76269746Seric # endif
76369746Seric 			continue;
76469746Seric 		}
76569746Seric 		if (wn >= WorkListSize)
76669746Seric 		{
76769748Seric 			extern void grow_wlist __P((void));
76869748Seric 
76969722Seric 			grow_wlist();
77069722Seric 			if (wn >= WorkListSize)
77169722Seric 				continue;
77269722Seric 		}
77358318Seric 
7748148Seric 		cf = fopen(d->d_name, "r");
7754632Seric 		if (cf == NULL)
7764632Seric 		{
7777055Seric 			/* this may be some random person sending hir msgs */
7787055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
77910090Seric 			if (tTd(41, 2))
78010090Seric 				printf("orderq: cannot open %s (%d)\n",
78110090Seric 					d->d_name, errno);
7827055Seric 			errno = 0;
78310090Seric 			wn--;
7844632Seric 			continue;
7854632Seric 		}
78669722Seric 		w = &WorkList[wn];
78725687Seric 		w->w_name = newstr(d->d_name);
78868481Seric 		w->w_host = NULL;
78968481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7904632Seric 
79125027Seric 		/* make sure jobs in creation don't clog queue */
79225687Seric 		w->w_pri = 0x7fffffff;
79325687Seric 		w->w_ctime = 0;
79425027Seric 
7954632Seric 		/* extract useful information */
79625687Seric 		i = NEED_P | NEED_T;
79758318Seric 		if (QueueLimitSender != NULL)
79858318Seric 			i |= NEED_S;
79968481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
80058318Seric 			i |= NEED_R;
80125687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
8024632Seric 		{
80358318Seric 			extern bool strcontainedin();
80424954Seric 
80524941Seric 			switch (lbuf[0])
8064632Seric 			{
80724941Seric 			  case 'P':
80825687Seric 				w->w_pri = atol(&lbuf[1]);
80925687Seric 				i &= ~NEED_P;
8104632Seric 				break;
81125013Seric 
81225013Seric 			  case 'T':
81325687Seric 				w->w_ctime = atol(&lbuf[1]);
81425687Seric 				i &= ~NEED_T;
81525013Seric 				break;
81658318Seric 
81758318Seric 			  case 'R':
81868481Seric 				if (w->w_host == NULL &&
81968481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
82068481Seric 					w->w_host = newstr(&p[1]);
82168481Seric 				if (QueueLimitRecipient == NULL ||
82258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
82358318Seric 					i &= ~NEED_R;
82458318Seric 				break;
82558318Seric 
82658318Seric 			  case 'S':
82758318Seric 				if (QueueLimitSender != NULL &&
82858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
82958318Seric 					i &= ~NEED_S;
83058318Seric 				break;
8314632Seric 			}
8324632Seric 		}
8334632Seric 		(void) fclose(cf);
83424953Seric 
83558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
83658318Seric 		    bitset(NEED_R|NEED_S, i))
83724953Seric 		{
83824953Seric 			/* don't even bother sorting this job in */
83968481Seric 			free(w->w_name);
84068481Seric 			if (w->w_host)
84168481Seric 				free(w->w_host);
84224953Seric 			wn--;
84324953Seric 		}
8444632Seric 	}
8456625Sglickman 	(void) closedir(f);
84610090Seric 	wn++;
8474632Seric 
84869722Seric 	wc = min(wn, WorkListSize);
8494632Seric 
85068481Seric 	if (QueueSortOrder == QS_BYHOST)
85168481Seric 	{
85268481Seric 		extern workcmpf1();
85368481Seric 		extern workcmpf2();
85467612Seric 
85568481Seric 		/*
85668481Seric 		**  Sort the work directory for the first time,
85768481Seric 		**  based on host name, lock status, and priority.
85868481Seric 		*/
85968481Seric 
86069722Seric 		qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf1);
86168481Seric 
86268481Seric 		/*
86368481Seric 		**  If one message to host is locked, "lock" all messages
86468481Seric 		**  to that host.
86568481Seric 		*/
86668481Seric 
86768481Seric 		i = 0;
86868481Seric 		while (i < wc)
86968481Seric 		{
87069722Seric 			if (!WorkList[i].w_lock)
87168481Seric 			{
87268481Seric 				i++;
87368481Seric 				continue;
87468481Seric 			}
87569722Seric 			w = &WorkList[i];
87668481Seric 			while (++i < wc)
87768481Seric 			{
87869722Seric 				if (WorkList[i].w_host == NULL &&
87968481Seric 				    w->w_host == NULL)
88069722Seric 					WorkList[i].w_lock = TRUE;
88169722Seric 				else if (WorkList[i].w_host != NULL &&
88268481Seric 					 w->w_host != NULL &&
88369722Seric 					 strcmp(WorkList[i].w_host, w->w_host) == 0)
88469722Seric 					WorkList[i].w_lock = TRUE;
88568481Seric 				else
88668481Seric 					break;
88768481Seric 			}
88868481Seric 		}
88968481Seric 
89068481Seric 		/*
89168481Seric 		**  Sort the work directory for the second time,
89268481Seric 		**  based on lock status, host name, and priority.
89368481Seric 		*/
89468481Seric 
89569722Seric 		qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf2);
89668481Seric 	}
89768481Seric 	else
89868481Seric 	{
89968481Seric 		extern workcmpf0();
90068481Seric 
90168481Seric 		/*
90268481Seric 		**  Simple sort based on queue priority only.
90368481Seric 		*/
90468481Seric 
90569722Seric 		qsort((char *) WorkList, wc, sizeof *WorkList, workcmpf0);
90668481Seric 	}
90768481Seric 
9084632Seric 	/*
9094632Seric 	**  Convert the work list into canonical form.
9109377Seric 	**	Should be turning it into a list of envelopes here perhaps.
9114632Seric 	*/
9124632Seric 
91324981Seric 	WorkQ = NULL;
91468481Seric 	for (i = wc; --i >= 0; )
9154632Seric 	{
9164632Seric 		w = (WORK *) xalloc(sizeof *w);
91769722Seric 		w->w_name = WorkList[i].w_name;
91869722Seric 		w->w_host = WorkList[i].w_host;
91969722Seric 		w->w_lock = WorkList[i].w_lock;
92069722Seric 		w->w_pri = WorkList[i].w_pri;
92169722Seric 		w->w_ctime = WorkList[i].w_ctime;
92224981Seric 		w->w_next = WorkQ;
92324981Seric 		WorkQ = w;
9244632Seric 	}
92569722Seric 	free(WorkList);
92669722Seric 	WorkList = NULL;
9274632Seric 
9287677Seric 	if (tTd(40, 1))
9294632Seric 	{
9304632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
9315037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
9324632Seric 	}
93310070Seric 
93410090Seric 	return (wn);
9354632Seric }
93669746Seric /*
93769746Seric **  GROW_WLIST -- make the work list larger
93869746Seric **
93969746Seric **	Parameters:
94069746Seric **		none.
94169746Seric **
94269746Seric **	Returns:
94369746Seric **		none.
94469746Seric **
94569746Seric **	Side Effects:
94669746Seric **		Adds another QUEUESEGSIZE entries to WorkList if possible.
94769746Seric **		It can fail if there isn't enough memory, so WorkListSize
94869746Seric **		should be checked again upon return.
94969746Seric */
95069722Seric 
95169748Seric void
grow_wlist()95269722Seric grow_wlist()
95369722Seric {
95469722Seric 	if (tTd(41, 1))
95569722Seric 		printf("grow_wlist: WorkListSize=%d\n", WorkListSize);
95669746Seric 	if (WorkList == NULL)
95769722Seric 	{
95869722Seric 		WorkList = (WORK *) xalloc(sizeof(WORK) * (QUEUESEGSIZE + 1));
95969722Seric 		WorkListSize = QUEUESEGSIZE;
96069722Seric 	}
96169722Seric 	else
96269722Seric 	{
96369722Seric 		int newsize = WorkListSize + QUEUESEGSIZE;
96469722Seric 		WORK *newlist = (WORK *) realloc((char *)WorkList,
96569722Seric 					  (unsigned)sizeof(WORK) * (newsize + 1));
96669722Seric 
96769722Seric 		if (newlist != NULL)
96869722Seric 		{
96969722Seric 			WorkListSize = newsize;
97069722Seric 			WorkList = newlist;
97169722Seric # ifdef LOG
97269722Seric 			if (LogLevel > 1)
97369722Seric 			{
97469722Seric 				syslog(LOG_NOTICE, "grew WorkList for %s to %d",
97569722Seric 						QueueDir, WorkListSize);
97669722Seric 			}
97769722Seric 		}
97869722Seric 		else if (LogLevel > 0)
97969722Seric 		{
98069722Seric 			syslog(LOG_ALERT, "FAILED to grow WorkList for %s to %d",
98169722Seric 					QueueDir, newsize);
98269722Seric # endif
98369722Seric 		}
98469722Seric 	}
98569722Seric 	if (tTd(41, 1))
98669722Seric 		printf("grow_wlist: WorkListSize now %d\n", WorkListSize);
98769722Seric }
9884632Seric /*
98968481Seric **  WORKCMPF0 -- simple priority-only compare function.
9904632Seric **
9914632Seric **	Parameters:
9924632Seric **		a -- the first argument.
9934632Seric **		b -- the second argument.
9944632Seric **
9954632Seric **	Returns:
99624981Seric **		-1 if a < b
99724981Seric **		 0 if a == b
99824981Seric **		+1 if a > b
9994632Seric **
10004632Seric **	Side Effects:
10014632Seric **		none.
10024632Seric */
10034632Seric 
100469748Seric int
workcmpf0(a,b)100568481Seric workcmpf0(a, b)
10065037Seric 	register WORK *a;
10075037Seric 	register WORK *b;
10084632Seric {
100957438Seric 	long pa = a->w_pri;
101057438Seric 	long pb = b->w_pri;
101124941Seric 
101224941Seric 	if (pa == pb)
101368481Seric 		return 0;
101424941Seric 	else if (pa > pb)
101568481Seric 		return 1;
101624981Seric 	else
101768481Seric 		return -1;
10184632Seric }
10194632Seric /*
102068481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
102168481Seric **
102268481Seric **	Sorts on host name, lock status, and priority in that order.
102368481Seric **
102468481Seric **	Parameters:
102568481Seric **		a -- the first argument.
102668481Seric **		b -- the second argument.
102768481Seric **
102868481Seric **	Returns:
102968481Seric **		<0 if a < b
103068481Seric **		 0 if a == b
103168481Seric **		>0 if a > b
103268481Seric **
103368481Seric **	Side Effects:
103468481Seric **		none.
103568481Seric */
103668481Seric 
103769748Seric int
workcmpf1(a,b)103868481Seric workcmpf1(a, b)
103968481Seric 	register WORK *a;
104068481Seric 	register WORK *b;
104168481Seric {
104268481Seric 	int i;
104368481Seric 
104468481Seric 	/* host name */
104568481Seric 	if (a->w_host != NULL && b->w_host == NULL)
104668481Seric 		return 1;
104768481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
104868481Seric 		return -1;
104968481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
105068481Seric 	    (i = strcmp(a->w_host, b->w_host)))
105168481Seric 		return i;
105268481Seric 
105368481Seric 	/* lock status */
105468481Seric 	if (a->w_lock != b->w_lock)
105568481Seric 		return b->w_lock - a->w_lock;
105668481Seric 
105768481Seric 	/* job priority */
105868481Seric 	return a->w_pri - b->w_pri;
105968481Seric }
106068481Seric /*
106168481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
106268481Seric **
106368481Seric **	Sorts on lock status, host name, and priority in that order.
106468481Seric **
106568481Seric **	Parameters:
106668481Seric **		a -- the first argument.
106768481Seric **		b -- the second argument.
106868481Seric **
106968481Seric **	Returns:
107068481Seric **		<0 if a < b
107168481Seric **		 0 if a == b
107268481Seric **		>0 if a > b
107368481Seric **
107468481Seric **	Side Effects:
107568481Seric **		none.
107668481Seric */
107768481Seric 
107869748Seric int
workcmpf2(a,b)107968481Seric workcmpf2(a, b)
108068481Seric 	register WORK *a;
108168481Seric 	register WORK *b;
108268481Seric {
108368481Seric 	int i;
108468481Seric 
108568481Seric 	/* lock status */
108668481Seric 	if (a->w_lock != b->w_lock)
108768481Seric 		return a->w_lock - b->w_lock;
108868481Seric 
108968481Seric 	/* host name */
109068481Seric 	if (a->w_host != NULL && b->w_host == NULL)
109168481Seric 		return 1;
109268481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
109368481Seric 		return -1;
109468481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
109568481Seric 	    (i = strcmp(a->w_host, b->w_host)))
109668481Seric 		return i;
109768481Seric 
109868481Seric 	/* job priority */
109968481Seric 	return a->w_pri - b->w_pri;
110068481Seric }
110168481Seric /*
11024632Seric **  DOWORK -- do a work request.
11034632Seric **
11044632Seric **	Parameters:
110558884Seric **		id -- the ID of the job to run.
110658884Seric **		forkflag -- if set, run this in background.
110758924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
110858924Seric **			This is used when expanding aliases in the queue.
110961094Seric **			If forkflag is also set, it doesn't wait for the
111061094Seric **			child.
111158884Seric **		e - the envelope in which to run it.
11124632Seric **
11134632Seric **	Returns:
111464296Seric **		process id of process that is running the queue job.
11154632Seric **
11164632Seric **	Side Effects:
11174632Seric **		The work request is satisfied if possible.
11184632Seric */
11194632Seric 
112064296Seric pid_t
dowork(id,forkflag,requeueflag,e)112158924Seric dowork(id, forkflag, requeueflag, e)
112258884Seric 	char *id;
112358884Seric 	bool forkflag;
112458924Seric 	bool requeueflag;
112555012Seric 	register ENVELOPE *e;
11264632Seric {
112764296Seric 	register pid_t pid;
112851920Seric 	extern bool readqf();
11294632Seric 
11307677Seric 	if (tTd(40, 1))
113158884Seric 		printf("dowork(%s)\n", id);
11324632Seric 
11334632Seric 	/*
113424941Seric 	**  Fork for work.
113524941Seric 	*/
113624941Seric 
113758884Seric 	if (forkflag)
113824941Seric 	{
113964296Seric 		pid = fork();
114064296Seric 		if (pid < 0)
114124941Seric 		{
114224941Seric 			syserr("dowork: cannot fork");
114364296Seric 			return 0;
114424941Seric 		}
114564839Seric 		else if (pid > 0)
114664839Seric 		{
114764839Seric 			/* parent -- clean out connection cache */
114864839Seric 			mci_flush(FALSE, NULL);
114964839Seric 		}
115024941Seric 	}
115124941Seric 	else
115224941Seric 	{
115364296Seric 		pid = 0;
115424941Seric 	}
115524941Seric 
115664296Seric 	if (pid == 0)
11574632Seric 	{
11584632Seric 		/*
11594632Seric 		**  CHILD
11608148Seric 		**	Lock the control file to avoid duplicate deliveries.
11618148Seric 		**		Then run the file as though we had just read it.
11627350Seric 		**	We save an idea of the temporary name so we
11637350Seric 		**		can recover on interrupt.
11644632Seric 		*/
11654632Seric 
11667763Seric 		/* set basic modes, etc. */
11677356Seric 		(void) alarm(0);
116855012Seric 		clearenvelope(e, FALSE);
116964554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
117058734Seric 		e->e_errormode = EM_MAIL;
117158884Seric 		e->e_id = id;
117264765Seric 		GrabTo = UseErrorsTo = FALSE;
117366316Seric 		ExitStat = EX_OK;
117463846Seric 		if (forkflag)
117564554Seric 		{
117664296Seric 			disconnect(1, e);
117764554Seric 			OpMode = MD_DELIVER;
117864554Seric 		}
11797876Seric # ifdef LOG
118058020Seric 		if (LogLevel > 76)
118155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
11827881Seric 			       getpid());
118356795Seric # endif /* LOG */
11847763Seric 
11857763Seric 		/* don't use the headers from sendmail.cf... */
118655012Seric 		e->e_header = NULL;
11877763Seric 
118851920Seric 		/* read the queue control file -- return if locked */
118965200Seric 		if (!readqf(e))
11906980Seric 		{
119158884Seric 			if (tTd(40, 4))
119258884Seric 				printf("readqf(%s) failed\n", e->e_id);
119358884Seric 			if (forkflag)
119424941Seric 				exit(EX_OK);
119524941Seric 			else
119666316Seric 				return 0;
11976980Seric 		}
11986980Seric 
119955012Seric 		e->e_flags |= EF_INQUEUE;
12006980Seric 
120168481Seric 		/* if this has been tried recently, let it be */
120268481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
120368481Seric 		{
120468481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
120558924Seric 
120668481Seric 			e->e_flags |= EF_KEEPQUEUE;
120768481Seric 			if (Verbose || tTd(40, 8))
120868481Seric 				printf("%s: too young (%s)\n",
120968481Seric 					e->e_id, howlong);
121068481Seric #ifdef LOG
121168481Seric 			if (LogLevel > 19)
121268481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
121368481Seric 					e->e_id, howlong);
121468481Seric #endif
121568481Seric 		}
121668481Seric 		else
121768481Seric 		{
121868481Seric 			eatheader(e, requeueflag);
12196980Seric 
122068481Seric 			if (requeueflag)
122168481Seric 				queueup(e, TRUE, FALSE);
122268481Seric 
122368481Seric 			/* do the delivery */
122468481Seric 			sendall(e, SM_DELIVER);
122568481Seric 		}
122668481Seric 
12276980Seric 		/* finish up and exit */
122858884Seric 		if (forkflag)
122924941Seric 			finis();
123024941Seric 		else
123155012Seric 			dropenvelope(e);
12324632Seric 	}
123364296Seric 	e->e_id = NULL;
123464296Seric 	return pid;
12354632Seric }
12364632Seric /*
12374632Seric **  READQF -- read queue file and set up environment.
12384632Seric **
12394632Seric **	Parameters:
12409377Seric **		e -- the envelope of the job to run.
12414632Seric **
12424632Seric **	Returns:
124351920Seric **		TRUE if it successfully read the queue file.
124451920Seric **		FALSE otherwise.
12454632Seric **
12464632Seric **	Side Effects:
124751920Seric **		The queue file is returned locked.
12484632Seric */
12494632Seric 
125051920Seric bool
readqf(e)125165200Seric readqf(e)
12529377Seric 	register ENVELOPE *e;
12534632Seric {
125417477Seric 	register FILE *qfp;
125554974Seric 	ADDRESS *ctladdr;
125656400Seric 	struct stat st;
125757135Seric 	char *bp;
125868481Seric 	int qfver = 0;
125968481Seric 	register char *p;
126068481Seric 	char *orcpt = NULL;
1261*69907Seric 	bool nomore = FALSE;
126258915Seric 	char qf[20];
126357135Seric 	char buf[MAXLINE];
126454974Seric 	extern ADDRESS *setctluser();
126568490Seric 	extern void loseqfile();
12664632Seric 
12674632Seric 	/*
126817468Seric 	**  Read and process the file.
12694632Seric 	*/
12704632Seric 
127158915Seric 	strcpy(qf, queuename(e, 'q'));
127251937Seric 	qfp = fopen(qf, "r+");
127317477Seric 	if (qfp == NULL)
127417477Seric 	{
127558884Seric 		if (tTd(40, 8))
127658884Seric 			printf("readqf(%s): fopen failure (%s)\n",
127758884Seric 				qf, errstring(errno));
127840934Srick 		if (errno != ENOENT)
127940934Srick 			syserr("readqf: no control file %s", qf);
128051920Seric 		return FALSE;
128117477Seric 	}
128240934Srick 
128364335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
128464277Seric 	{
128564277Seric 		/* being processed by another queuer */
128668481Seric 		if (Verbose || tTd(40, 8))
128764277Seric 			printf("%s: locked\n", e->e_id);
128864277Seric # ifdef LOG
128964277Seric 		if (LogLevel > 19)
129064277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
129164277Seric # endif /* LOG */
129264277Seric 		(void) fclose(qfp);
129364277Seric 		return FALSE;
129464277Seric 	}
129564277Seric 
129656400Seric 	/*
129756400Seric 	**  Check the queue file for plausibility to avoid attacks.
129856400Seric 	*/
129956400Seric 
130056400Seric 	if (fstat(fileno(qfp), &st) < 0)
130156400Seric 	{
130256400Seric 		/* must have been being processed by someone else */
130358884Seric 		if (tTd(40, 8))
130458884Seric 			printf("readqf(%s): fstat failure (%s)\n",
130558884Seric 				qf, errstring(errno));
130656400Seric 		fclose(qfp);
130756400Seric 		return FALSE;
130856400Seric 	}
130956400Seric 
131068563Seric 	if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode))
131156400Seric 	{
131256400Seric # ifdef LOG
131356400Seric 		if (LogLevel > 0)
131456400Seric 		{
131556400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
131656400Seric 				e->e_id, st.st_uid, st.st_mode);
131756400Seric 		}
131856795Seric # endif /* LOG */
131958884Seric 		if (tTd(40, 8))
132058884Seric 			printf("readqf(%s): bogus file\n", qf);
132168490Seric 		loseqfile(e, "bogus file uid in mqueue");
132256400Seric 		fclose(qfp);
132356400Seric 		return FALSE;
132456400Seric 	}
132556400Seric 
132664277Seric 	if (st.st_size == 0)
132740934Srick 	{
132864277Seric 		/* must be a bogus file -- just remove it */
132964277Seric 		(void) unlink(qf);
133064277Seric 		fclose(qfp);
133151920Seric 		return FALSE;
133240934Srick 	}
133340934Srick 
133464277Seric 	if (st.st_nlink == 0)
133559101Seric 	{
133664277Seric 		/*
133764277Seric 		**  Race condition -- we got a file just as it was being
133864277Seric 		**  unlinked.  Just assume it is zero length.
133964277Seric 		*/
134064277Seric 
134159101Seric 		fclose(qfp);
134259101Seric 		return FALSE;
134359101Seric 	}
134459101Seric 
134564277Seric 	/* good file -- save this lock */
134651920Seric 	e->e_lockfp = qfp;
134751920Seric 
134840934Srick 	/* do basic system initialization */
134955012Seric 	initsys(e);
135065164Seric 	define('i', e->e_id, e);
135140934Srick 
13529377Seric 	LineNumber = 0;
135363850Seric 	e->e_flags |= EF_GLOBALERRS;
135463850Seric 	OpMode = MD_DELIVER;
135554974Seric 	ctladdr = NULL;
135668481Seric 	e->e_dfino = -1;
135768564Seric 	e->e_msgsize = -1;
135857135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
13594632Seric 	{
136058737Seric 		register char *p;
136168481Seric 		u_long qflags;
136268481Seric 		ADDRESS *q;
136357529Seric 
136426504Seric 		if (tTd(40, 4))
136557135Seric 			printf("+++++ %s\n", bp);
1366*69907Seric 		if (nomore)
1367*69907Seric 		{
1368*69907Seric 			/* hack attack */
1369*69907Seric 			syserr("SECURITY ALERT: extra data in qf: %s", bp);
1370*69907Seric 			fclose(qfp);
1371*69907Seric 			loseqfile(e, "bogus queue line");
1372*69907Seric 			return FALSE;
1373*69907Seric 		}
137457135Seric 		switch (bp[0])
13754632Seric 		{
137668481Seric 		  case 'V':		/* queue file version number */
137768481Seric 			qfver = atoi(&bp[1]);
137868481Seric 			if (qfver > QF_VERSION)
137968481Seric 			{
138068481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
138168481Seric 					qfver, QF_VERSION);
138268481Seric 			}
138368481Seric 			break;
138468481Seric 
138540973Sbostic 		  case 'C':		/* specify controlling user */
138657135Seric 			ctladdr = setctluser(&bp[1]);
138740973Sbostic 			break;
138840973Sbostic 
138968481Seric 		  case 'Q':		/* original recipient */
139068481Seric 			orcpt = newstr(&bp[1]);
139168481Seric 			break;
139268481Seric 
13934632Seric 		  case 'R':		/* specify recipient */
139468481Seric 			p = bp;
139568481Seric 			qflags = 0;
139668481Seric 			if (qfver >= 1)
139768481Seric 			{
139868481Seric 				/* get flag bits */
139968481Seric 				while (*++p != '\0' && *p != ':')
140068481Seric 				{
140168481Seric 					switch (*p)
140268481Seric 					{
140368481Seric 					  case 'S':
140468481Seric 						qflags |= QPINGONSUCCESS;
140568481Seric 						break;
140668481Seric 
140768481Seric 					  case 'F':
140868481Seric 						qflags |= QPINGONFAILURE;
140968481Seric 						break;
141068481Seric 
141168481Seric 					  case 'D':
141268481Seric 						qflags |= QPINGONDELAY;
141368481Seric 						break;
141468481Seric 
141568481Seric 					  case 'P':
141668481Seric 						qflags |= QPRIMARY;
141768481Seric 						break;
141868481Seric 					}
141968481Seric 				}
142068481Seric 			}
142168481Seric 			else
142268481Seric 				qflags |= QPRIMARY;
142368481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
142468481Seric 			if (q != NULL)
142568481Seric 			{
142668481Seric 				q->q_alias = ctladdr;
142768481Seric 				q->q_flags |= qflags;
142868481Seric 				q->q_orcpt = orcpt;
142968481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
143068481Seric 			}
143168481Seric 			orcpt = NULL;
14324632Seric 			break;
14334632Seric 
143425687Seric 		  case 'E':		/* specify error recipient */
143568558Seric 			/* no longer used */
143625687Seric 			break;
143725687Seric 
14384632Seric 		  case 'H':		/* header */
143968717Seric 			(void) chompheader(&bp[1], FALSE, NULL, e);
14404632Seric 			break;
14414632Seric 
144210108Seric 		  case 'M':		/* message */
144364705Seric 			/* ignore this; we want a new message next time */
144410108Seric 			break;
144510108Seric 
14464632Seric 		  case 'S':		/* sender */
144758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
14484632Seric 			break;
14494632Seric 
145059093Seric 		  case 'B':		/* body type */
145159093Seric 			e->e_bodytype = newstr(&bp[1]);
145259093Seric 			break;
145359093Seric 
14544632Seric 		  case 'D':		/* data file name */
145568564Seric 			/* obsolete -- ignore */
14564632Seric 			break;
14574632Seric 
14587860Seric 		  case 'T':		/* init time */
145957135Seric 			e->e_ctime = atol(&bp[1]);
14604632Seric 			break;
14614632Seric 
146268481Seric 		  case 'I':		/* data file's inode number */
146368481Seric 			if (e->e_dfino == -1)
146468481Seric 				e->e_dfino = atol(&buf[1]);
146568481Seric 			break;
146668481Seric 
146768481Seric 		  case 'K':		/* time of last deliver attempt */
146868481Seric 			e->e_dtime = atol(&buf[1]);
146968481Seric 			break;
147068481Seric 
147168481Seric 		  case 'N':		/* number of delivery attempts */
147268481Seric 			e->e_ntries = atoi(&buf[1]);
147368481Seric 			break;
147468481Seric 
14754634Seric 		  case 'P':		/* message priority */
147657135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
14774634Seric 			break;
14784634Seric 
147958737Seric 		  case 'F':		/* flag bits */
1480*69907Seric 			if (strncmp(bp, "From ", 5) == 0)
1481*69907Seric 			{
1482*69907Seric 				/* we are being spoofed! */
1483*69907Seric 				syserr("SECURITY ALERT: bogus qf line %s", bp);
1484*69907Seric 				fclose(qfp);
1485*69907Seric 				loseqfile(e, "bogus queue line");
1486*69907Seric 				return FALSE;
1487*69907Seric 			}
148858737Seric 			for (p = &bp[1]; *p != '\0'; p++)
148958737Seric 			{
149058737Seric 				switch (*p)
149158737Seric 				{
149258737Seric 				  case 'w':	/* warning sent */
149358737Seric 					e->e_flags |= EF_WARNING;
149458737Seric 					break;
149558737Seric 
149658737Seric 				  case 'r':	/* response */
149758737Seric 					e->e_flags |= EF_RESPONSE;
149858737Seric 					break;
149968481Seric 
150068481Seric 				  case '8':	/* has 8 bit data */
150168481Seric 					e->e_flags |= EF_HAS8BIT;
150268481Seric 					break;
150358737Seric 				}
150458737Seric 			}
150558737Seric 			break;
150658737Seric 
150768481Seric 		  case 'Z':		/* original envelope id from ESMTP */
150868481Seric 			e->e_envid = newstr(&bp[1]);
150968481Seric 			break;
151068481Seric 
151153400Seric 		  case '$':		/* define macro */
151257135Seric 			define(bp[1], newstr(&bp[2]), e);
151353400Seric 			break;
151453400Seric 
1515*69907Seric 		  case '.':		/* terminate file */
1516*69907Seric 			nomore = TRUE;
151724941Seric 			break;
151824941Seric 
15194632Seric 		  default:
152065960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
152165200Seric 				qf, LineNumber, bp);
152263753Seric 			fclose(qfp);
152368490Seric 			loseqfile(e, "unrecognized line");
152463753Seric 			return FALSE;
15254632Seric 		}
152657135Seric 
152757135Seric 		if (bp != buf)
152857135Seric 			free(bp);
15294632Seric 	}
15309377Seric 
153124941Seric 	/*
153224941Seric 	**  If we haven't read any lines, this queue file is empty.
153324941Seric 	**  Arrange to remove it without referencing any null pointers.
153424941Seric 	*/
153524941Seric 
153624941Seric 	if (LineNumber == 0)
153724941Seric 	{
153824941Seric 		errno = 0;
153924941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
154024941Seric 	}
154168564Seric 	else
154268564Seric 	{
154368564Seric 		/*
154468564Seric 		**  Arrange to read the data file.
154568564Seric 		*/
154668564Seric 
154768564Seric 		p = queuename(e, 'd');
154868564Seric 		e->e_dfp = fopen(p, "r");
154968564Seric 		if (e->e_dfp == NULL)
155068564Seric 		{
155168564Seric 			syserr("readqf: cannot open %s", p);
155268564Seric 		}
155368603Seric 		else
155468564Seric 		{
155568603Seric 			e->e_flags |= EF_HAS_DF;
155668603Seric 			if (fstat(fileno(e->e_dfp), &st) >= 0)
155768603Seric 			{
155868603Seric 				e->e_msgsize = st.st_size;
155968603Seric 				e->e_dfdev = st.st_dev;
156068603Seric 				e->e_dfino = st.st_ino;
156168603Seric 			}
156268564Seric 		}
156368564Seric 	}
156468564Seric 
156551920Seric 	return TRUE;
15664632Seric }
15674632Seric /*
15689630Seric **  PRINTQUEUE -- print out a representation of the mail queue
15699630Seric **
15709630Seric **	Parameters:
15719630Seric **		none.
15729630Seric **
15739630Seric **	Returns:
15749630Seric **		none.
15759630Seric **
15769630Seric **	Side Effects:
15779630Seric **		Prints a listing of the mail queue on the standard output.
15789630Seric */
15795182Seric 
158069748Seric void
printqueue()15819630Seric printqueue()
15829630Seric {
15839630Seric 	register WORK *w;
15849630Seric 	FILE *f;
158510070Seric 	int nrequests;
15869630Seric 	char buf[MAXLINE];
15879630Seric 
15889630Seric 	/*
158958250Seric 	**  Check for permission to print the queue
159058250Seric 	*/
159158250Seric 
159264333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
159358250Seric 	{
159458250Seric 		struct stat st;
159558523Seric # ifdef NGROUPS
159658523Seric 		int n;
159763937Seric 		GIDSET_T gidset[NGROUPS];
159858523Seric # endif
159958250Seric 
160058517Seric 		if (stat(QueueDir, &st) < 0)
160158250Seric 		{
160258250Seric 			syserr("Cannot stat %s", QueueDir);
160358250Seric 			return;
160458250Seric 		}
160558523Seric # ifdef NGROUPS
160658523Seric 		n = getgroups(NGROUPS, gidset);
160758523Seric 		while (--n >= 0)
160858523Seric 		{
160958523Seric 			if (gidset[n] == st.st_gid)
161058523Seric 				break;
161158523Seric 		}
161269835Seric 		if (n < 0 && RealGid != st.st_gid)
161358523Seric # else
161463787Seric 		if (RealGid != st.st_gid)
161558523Seric # endif
161658250Seric 		{
161758250Seric 			usrerr("510 You are not permitted to see the queue");
161858250Seric 			setstat(EX_NOPERM);
161958250Seric 			return;
162058250Seric 		}
162158250Seric 	}
162258250Seric 
162358250Seric 	/*
16249630Seric 	**  Read and order the queue.
16259630Seric 	*/
16269630Seric 
162724941Seric 	nrequests = orderq(TRUE);
16289630Seric 
16299630Seric 	/*
16309630Seric 	**  Print the work list that we have read.
16319630Seric 	*/
16329630Seric 
16339630Seric 	/* first see if there is anything */
163410070Seric 	if (nrequests <= 0)
16359630Seric 	{
163610070Seric 		printf("Mail queue is empty\n");
16379630Seric 		return;
16389630Seric 	}
16399630Seric 
164051920Seric 	CurrentLA = getla();	/* get load average */
164140934Srick 
164210096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
164369722Seric 	if (nrequests > WorkListSize)
164469722Seric 		printf(", only %d printed", WorkListSize);
164524979Seric 	if (Verbose)
164658716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
164724979Seric 	else
164858716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
16499630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
16509630Seric 	{
16519630Seric 		struct stat st;
165210070Seric 		auto time_t submittime = 0;
165368565Seric 		long dfsize;
165458737Seric 		int flags = 0;
165568481Seric 		int qfver;
165669538Seric 		char statmsg[MAXLINE];
165768528Seric 		char bodytype[MAXNAME + 1];
16589630Seric 
165964355Seric 		printf("%8s", w->w_name + 2);
166017468Seric 		f = fopen(w->w_name, "r");
166117468Seric 		if (f == NULL)
166217468Seric 		{
166364355Seric 			printf(" (job completed)\n");
166417468Seric 			errno = 0;
166517468Seric 			continue;
166617468Seric 		}
166768565Seric 		w->w_name[0] = 'd';
166868565Seric 		if (stat(w->w_name, &st) >= 0)
166968565Seric 			dfsize = st.st_size;
167068565Seric 		else
167168565Seric 			dfsize = -1;
167268481Seric 		if (w->w_lock)
167310070Seric 			printf("*");
167457438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
167524941Seric 			printf("X");
167610070Seric 		else
167710070Seric 			printf(" ");
167810070Seric 		errno = 0;
167917468Seric 
168069538Seric 		statmsg[0] = bodytype[0] = '\0';
168168481Seric 		qfver = 0;
16829630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
16839630Seric 		{
168453400Seric 			register int i;
168558737Seric 			register char *p;
168653400Seric 
16879630Seric 			fixcrlf(buf, TRUE);
16889630Seric 			switch (buf[0])
16899630Seric 			{
169068481Seric 			  case 'V':	/* queue file version */
169168481Seric 				qfver = atoi(&buf[1]);
169268481Seric 				break;
169368481Seric 
169410108Seric 			  case 'M':	/* error message */
169569538Seric 				if ((i = strlen(&buf[1])) >= sizeof statmsg)
169669538Seric 					i = sizeof statmsg - 1;
169769538Seric 				bcopy(&buf[1], statmsg, i);
169869538Seric 				statmsg[i] = '\0';
169910108Seric 				break;
170010108Seric 
170159093Seric 			  case 'B':	/* body type */
170259093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
170359093Seric 					i = sizeof bodytype - 1;
170459093Seric 				bcopy(&buf[1], bodytype, i);
170559093Seric 				bodytype[i] = '\0';
170659093Seric 				break;
170759093Seric 
17089630Seric 			  case 'S':	/* sender name */
170924979Seric 				if (Verbose)
171058737Seric 					printf("%8ld %10ld%c%.12s %.38s",
171158737Seric 					    dfsize,
171258737Seric 					    w->w_pri,
171358737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
171458737Seric 					    ctime(&submittime) + 4,
171524979Seric 					    &buf[1]);
171624979Seric 				else
171724979Seric 					printf("%8ld %.16s %.45s", dfsize,
171824979Seric 					    ctime(&submittime), &buf[1]);
171969538Seric 				if (statmsg[0] != '\0' || bodytype[0] != '\0')
172059093Seric 				{
172159093Seric 					printf("\n    %10.10s", bodytype);
172269538Seric 					if (statmsg[0] != '\0')
172369538Seric 						printf("   (%.60s)", statmsg);
172459093Seric 				}
17259630Seric 				break;
172651920Seric 
172740973Sbostic 			  case 'C':	/* controlling user */
172854974Seric 				if (Verbose)
172958716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
173058716Seric 						&buf[1]);
173140973Sbostic 				break;
17329630Seric 
17339630Seric 			  case 'R':	/* recipient name */
173468481Seric 				p = &buf[1];
173568481Seric 				if (qfver >= 1)
173668481Seric 				{
173768481Seric 					p = strchr(p, ':');
173868481Seric 					if (p == NULL)
173968481Seric 						break;
174068481Seric 					p++;
174168481Seric 				}
174224979Seric 				if (Verbose)
174368481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
174424979Seric 				else
174568481Seric 					printf("\n\t\t\t\t   %.45s", p);
17469630Seric 				break;
17479630Seric 
17489630Seric 			  case 'T':	/* creation time */
174924941Seric 				submittime = atol(&buf[1]);
17509630Seric 				break;
175110070Seric 
175258737Seric 			  case 'F':	/* flag bits */
175358737Seric 				for (p = &buf[1]; *p != '\0'; p++)
175458737Seric 				{
175558737Seric 					switch (*p)
175658737Seric 					{
175758737Seric 					  case 'w':
175858737Seric 						flags |= EF_WARNING;
175958737Seric 						break;
176058737Seric 					}
176158737Seric 				}
17629630Seric 			}
17639630Seric 		}
176410070Seric 		if (submittime == (time_t) 0)
176510070Seric 			printf(" (no control file)");
17669630Seric 		printf("\n");
176723098Seric 		(void) fclose(f);
17689630Seric 	}
17699630Seric }
17709630Seric 
177156795Seric # endif /* QUEUE */
177217468Seric /*
177317468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
177417468Seric **
177517468Seric **	Assigns an id code if one does not already exist.
177617468Seric **	This code is very careful to avoid trashing existing files
177717468Seric **	under any circumstances.
177817468Seric **
177917468Seric **	Parameters:
178017468Seric **		e -- envelope to build it in/from.
178117468Seric **		type -- the file type, used as the first character
178217468Seric **			of the file name.
178317468Seric **
178417468Seric **	Returns:
178517468Seric **		a pointer to the new file name (in a static buffer).
178617468Seric **
178717468Seric **	Side Effects:
178851920Seric **		If no id code is already assigned, queuename will
178951920Seric **		assign an id code, create a qf file, and leave a
179051920Seric **		locked, open-for-write file pointer in the envelope.
179117468Seric */
179217468Seric 
179317468Seric char *
queuename(e,type)179417468Seric queuename(e, type)
179517468Seric 	register ENVELOPE *e;
179659700Seric 	int type;
179717468Seric {
179817468Seric 	static int pid = -1;
179958741Seric 	static char c0;
180058741Seric 	static char c1;
180158741Seric 	static char c2;
180258716Seric 	time_t now;
180358716Seric 	struct tm *tm;
180468528Seric 	static char buf[MAXNAME + 1];
180517468Seric 
180617468Seric 	if (e->e_id == NULL)
180717468Seric 	{
180817468Seric 		char qf[20];
180917468Seric 
181017468Seric 		/* find a unique id */
181117468Seric 		if (pid != getpid())
181217468Seric 		{
181317468Seric 			/* new process -- start back at "AA" */
181417468Seric 			pid = getpid();
181558716Seric 			now = curtime();
181658716Seric 			tm = localtime(&now);
181758716Seric 			c0 = 'A' + tm->tm_hour;
181817468Seric 			c1 = 'A';
181917468Seric 			c2 = 'A' - 1;
182017468Seric 		}
182158716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
182217468Seric 
182317468Seric 		while (c1 < '~' || c2 < 'Z')
182417468Seric 		{
182517468Seric 			int i;
182617468Seric 
182717468Seric 			if (c2 >= 'Z')
182817468Seric 			{
182917468Seric 				c1++;
183017468Seric 				c2 = 'A' - 1;
183117468Seric 			}
183258716Seric 			qf[3] = c1;
183358716Seric 			qf[4] = ++c2;
183417468Seric 			if (tTd(7, 20))
183540934Srick 				printf("queuename: trying \"%s\"\n", qf);
183617468Seric 
183740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
183851920Seric 			if (i < 0)
183951920Seric 			{
184051920Seric 				if (errno == EEXIST)
184151920Seric 					continue;
184264705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
184364705Seric 					qf, QueueDir, geteuid());
184451920Seric 				exit(EX_UNAVAILABLE);
184551920Seric 			}
184664335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
184751920Seric 			{
184851920Seric 				e->e_lockfp = fdopen(i, "w");
184940934Srick 				break;
185017468Seric 			}
185151920Seric 
185251920Seric 			/* a reader got the file; abandon it and try again */
185351920Seric 			(void) close(i);
185417468Seric 		}
185517468Seric 		if (c1 >= '~' && c2 >= 'Z')
185617468Seric 		{
185764705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
185864705Seric 				qf, QueueDir, geteuid());
185917468Seric 			exit(EX_OSERR);
186017468Seric 		}
186117468Seric 		e->e_id = newstr(&qf[2]);
186217468Seric 		define('i', e->e_id, e);
186317468Seric 		if (tTd(7, 1))
186417468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
186564745Seric 		if (tTd(7, 9))
186664745Seric 		{
186764745Seric 			printf("  lockfd=");
186864745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
186964745Seric 		}
187017468Seric # ifdef LOG
187158020Seric 		if (LogLevel > 93)
187217468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
187356795Seric # endif /* LOG */
187417468Seric 	}
187517468Seric 
187617468Seric 	if (type == '\0')
187717468Seric 		return (NULL);
187817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
187917468Seric 	if (tTd(7, 2))
188017468Seric 		printf("queuename: %s\n", buf);
188117468Seric 	return (buf);
188217468Seric }
188317468Seric /*
188417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
188517468Seric **
188617468Seric **	Parameters:
188717468Seric **		e -- the envelope to unlock.
188817468Seric **
188917468Seric **	Returns:
189017468Seric **		none
189117468Seric **
189217468Seric **	Side Effects:
189317468Seric **		unlocks the queue for `e'.
189417468Seric */
189517468Seric 
189669748Seric void
unlockqueue(e)189717468Seric unlockqueue(e)
189817468Seric 	ENVELOPE *e;
189917468Seric {
190058680Seric 	if (tTd(51, 4))
190158680Seric 		printf("unlockqueue(%s)\n", e->e_id);
190258680Seric 
190351920Seric 	/* if there is a lock file in the envelope, close it */
190451920Seric 	if (e->e_lockfp != NULL)
190558680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
190651920Seric 	e->e_lockfp = NULL;
190751920Seric 
190858728Seric 	/* don't create a queue id if we don't already have one */
190958728Seric 	if (e->e_id == NULL)
191058728Seric 		return;
191158728Seric 
191217468Seric 	/* remove the transcript */
191317468Seric # ifdef LOG
191458020Seric 	if (LogLevel > 87)
191517468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
191656795Seric # endif /* LOG */
191758680Seric 	if (!tTd(51, 104))
191817468Seric 		xunlink(queuename(e, 'x'));
191917468Seric 
192017468Seric }
192140973Sbostic /*
192254974Seric **  SETCTLUSER -- create a controlling address
192340973Sbostic **
192454974Seric **	Create a fake "address" given only a local login name; this is
192554974Seric **	used as a "controlling user" for future recipient addresses.
192640973Sbostic **
192740973Sbostic **	Parameters:
192854974Seric **		user -- the user name of the controlling user.
192940973Sbostic **
193040973Sbostic **	Returns:
193154974Seric **		An address descriptor for the controlling user.
193240973Sbostic **
193340973Sbostic **	Side Effects:
193440973Sbostic **		none.
193540973Sbostic */
193640973Sbostic 
193754974Seric ADDRESS *
setctluser(user)193854974Seric setctluser(user)
193954974Seric 	char *user;
194040973Sbostic {
194154974Seric 	register ADDRESS *a;
194240973Sbostic 	struct passwd *pw;
194359113Seric 	char *p;
194440973Sbostic 
194540973Sbostic 	/*
194654974Seric 	**  See if this clears our concept of controlling user.
194740973Sbostic 	*/
194840973Sbostic 
194963850Seric 	if (user == NULL || *user == '\0')
195063850Seric 		return NULL;
195140973Sbostic 
195240973Sbostic 	/*
195354974Seric 	**  Set up addr fields for controlling user.
195440973Sbostic 	*/
195540973Sbostic 
195654974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
195754974Seric 	bzero((char *) a, sizeof *a);
195859113Seric 
195959113Seric 	p = strchr(user, ':');
196059113Seric 	if (p != NULL)
196159113Seric 		*p++ = '\0';
196268693Seric 	if (*user != '\0' && (pw = sm_getpwnam(user)) != NULL)
196340973Sbostic 	{
196465822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
196565822Seric 			a->q_home = "";
196665822Seric 		else
196765822Seric 			a->q_home = newstr(pw->pw_dir);
196840973Sbostic 		a->q_uid = pw->pw_uid;
196940973Sbostic 		a->q_gid = pw->pw_gid;
197059270Seric 		a->q_flags |= QGOODUID;
197140973Sbostic 	}
197268603Seric 
197368603Seric 	if (*user != '\0')
197468603Seric 		a->q_user = newstr(user);
197568603Seric 	else if (p != NULL)
197668603Seric 		a->q_user = newstr(p);
197740973Sbostic 	else
197857642Seric 		a->q_user = newstr(DefUser);
197940973Sbostic 
198059270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
198156328Seric 	a->q_mailer = LocalMailer;
198259113Seric 	if (p == NULL)
198359113Seric 		a->q_paddr = a->q_user;
198459113Seric 	else
198559113Seric 		a->q_paddr = newstr(p);
198654974Seric 	return a;
198740973Sbostic }
198868490Seric /*
198968490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
199068490Seric **
199168490Seric **	Parameters:
199268490Seric **		e -- the envelope (e->e_id will be used).
199368490Seric **		why -- reported to whomever can hear.
199468490Seric **
199568490Seric **	Returns:
199668490Seric **		none.
199768490Seric */
199868490Seric 
199968490Seric void
loseqfile(e,why)200068490Seric loseqfile(e, why)
200168490Seric 	register ENVELOPE *e;
200268490Seric 	char *why;
200368490Seric {
200468563Seric 	char *p;
200568490Seric 	char buf[40];
200668490Seric 
200768490Seric 	if (e == NULL || e->e_id == NULL)
200868490Seric 		return;
200968490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
201068490Seric 		return;
201168490Seric 	strcpy(buf, queuename(e, 'q'));
201268563Seric 	p = queuename(e, 'Q');
201368563Seric 	if (rename(buf, p) < 0)
201468563Seric 		syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid());
201568490Seric #ifdef LOG
201668563Seric 	else if (LogLevel > 0)
201768563Seric 		syslog(LOG_ALERT, "Losing %s: %s", buf, why);
201868490Seric #endif
201968490Seric }
2020