xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68603)
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*68603Seric static char sccsid[] = "@(#)queue.c	8.74 (Berkeley) 03/27/95 (with queueing)";
1433731Sbostic #else
15*68603Seric static char sccsid[] = "@(#)queue.c	8.74 (Berkeley) 03/27/95 (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 */
3268481Seric 	char		*w_host;	/* name of recipient host */
3368481Seric 	bool		w_lock;		/* is message locked? */
349377Seric 	long		w_pri;		/* priority of message, see below */
3525013Seric 	time_t		w_ctime;	/* creation time of message */
369377Seric 	struct work	*w_next;	/* next in queue */
379377Seric };
389377Seric 
399377Seric typedef struct work	WORK;
409377Seric 
419377Seric WORK	*WorkQ;			/* queue of things to be done */
4268481Seric 
4368481Seric #define QF_VERSION	1	/* version number of this queue format */
449377Seric /*
454632Seric **  QUEUEUP -- queue a message up for future transmission.
464632Seric **
474632Seric **	Parameters:
486980Seric **		e -- the envelope to queue up.
496999Seric **		queueall -- if TRUE, queue all addresses, rather than
506999Seric **			just those with the QQUEUEUP flag set.
519377Seric **		announce -- if TRUE, tell when you are queueing up.
524632Seric **
534632Seric **	Returns:
5451920Seric **		none.
554632Seric **
564632Seric **	Side Effects:
579377Seric **		The current request are saved in a control file.
5851920Seric **		The queue file is left locked.
594632Seric */
604632Seric 
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];
774632Seric 
785037Seric 	/*
7917477Seric 	**  Create control file.
805037Seric 	*/
814632Seric 
8264745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
8364277Seric 
8464277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8551920Seric 	strcpy(tf, queuename(e, 't'));
8651920Seric 	tfp = e->e_lockfp;
8751920Seric 	if (tfp == NULL)
8851920Seric 		newid = FALSE;
8964277Seric 
9064277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
9164745Seric 	if (!newid)
9251835Seric 	{
9351920Seric 		/* get a locked tf file */
9464070Seric 		for (i = 0; i < 128; i++)
9551835Seric 		{
9651920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9751920Seric 			if (fd < 0)
9851835Seric 			{
9964070Seric 				if (errno != EEXIST)
10064070Seric 					break;
10164070Seric #ifdef LOG
10264070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10365090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
10465090Seric 						tf, geteuid(), errstring(errno));
10564070Seric #endif
10641636Srick 			}
10765090Seric 			else
10865090Seric 			{
10965090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
11065090Seric 					break;
11164070Seric #ifdef LOG
11265090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
11365090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11465090Seric 						tf, errstring(errno));
11564070Seric #endif
11665090Seric 				close(fd);
11765090Seric 			}
11858689Seric 
11964070Seric 			if ((i % 32) == 31)
12064070Seric 			{
12164070Seric 				/* save the old temp file away */
12264070Seric 				(void) rename(tf, queuename(e, 'T'));
12364070Seric 			}
12464070Seric 			else
12564070Seric 				sleep(i % 32);
12641636Srick 		}
12764724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12864921Seric 		{
12964921Seric 			printopenfds(TRUE);
13065090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
13165090Seric 				tf, geteuid());
13264921Seric 		}
13351920Seric 	}
1344632Seric 
1357677Seric 	if (tTd(40, 1))
13668481Seric 		printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id,
13768481Seric 			newid ? " (new id)" : "", queueall);
138*68603Seric 	if (tTd(40, 3))
139*68603Seric 	{
140*68603Seric 		extern void printenvflags();
141*68603Seric 
142*68603Seric 		printf("  e_flags=");
143*68603Seric 		printenvflags(e);
144*68603Seric 	}
14568481Seric 	if (tTd(40, 32))
14668481Seric 	{
14768481Seric 		printf("  sendq=");
14868481Seric 		printaddr(e->e_sendqueue, TRUE);
14968481Seric 	}
15064745Seric 	if (tTd(40, 9))
15164745Seric 	{
15264745Seric 		printf("  tfp=");
15364745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
15464745Seric 		printf("  lockfp=");
15564745Seric 		if (e->e_lockfp == NULL)
15664745Seric 			printf("NULL\n");
15764745Seric 		else
15864745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
15964745Seric 	}
1604632Seric 
1614632Seric 	/*
1626980Seric 	**  If there is no data file yet, create one.
1636980Seric 	*/
1646980Seric 
16568564Seric 	if (!bitset(EF_HAS_DF, e->e_flags))
1666980Seric 	{
1676980Seric 		register FILE *dfp;
16868564Seric 		char dfname[20];
16968481Seric 		struct stat stbuf;
1709389Seric 		extern putbody();
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.
3774632Seric 	*/
3784632Seric 
37964762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
38058732Seric 	{
38158732Seric 		if (newid)
38258732Seric 			syserr("!552 Error writing control file %s", tf);
38358732Seric 		else
38458732Seric 			syserr("!452 Error writing control file %s", tf);
38558732Seric 	}
38658732Seric 
38751920Seric 	if (!newid)
38851920Seric 	{
38964277Seric 		/* rename (locked) tf to be (locked) qf */
39051920Seric 		qf = queuename(e, 'q');
39151920Seric 		if (rename(tf, qf) < 0)
39268564Seric 			syserr("cannot rename(%s, %s), uid=%d",
39368564Seric 				tf, qf, geteuid());
39464277Seric 
39564277Seric 		/* close and unlock old (locked) qf */
39651920Seric 		if (e->e_lockfp != NULL)
39758680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
39851920Seric 		e->e_lockfp = tfp;
39951920Seric 	}
40051920Seric 	else
40151920Seric 		qf = tf;
40241636Srick 	errno = 0;
40364745Seric 	e->e_flags |= EF_INQUEUE;
4047391Seric 
4057677Seric # ifdef LOG
4067677Seric 	/* save log info */
40758020Seric 	if (LogLevel > 79)
40868564Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s", e->e_id, qf);
40956795Seric # endif /* LOG */
41064307Seric 
41164307Seric 	if (tTd(40, 1))
41264307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
41351920Seric 	return;
4144632Seric }
41554974Seric 
41654974Seric printctladdr(a, tfp)
41759113Seric 	register ADDRESS *a;
41854974Seric 	FILE *tfp;
41954974Seric {
42059113Seric 	char *uname;
42159113Seric 	register struct passwd *pw;
42259113Seric 	register ADDRESS *q;
42359113Seric 	uid_t uid;
42459113Seric 	static ADDRESS *lastctladdr;
42559113Seric 	static uid_t lastuid;
42654974Seric 
42759113Seric 	/* initialization */
42863850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
42954974Seric 	{
43059670Seric 		if (lastctladdr != NULL && tfp != NULL)
43159113Seric 			fprintf(tfp, "C\n");
43259113Seric 		lastctladdr = NULL;
43359113Seric 		lastuid = 0;
43454974Seric 		return;
43554974Seric 	}
43659113Seric 
43759113Seric 	/* find the active uid */
43859113Seric 	q = getctladdr(a);
43959113Seric 	if (q == NULL)
44059113Seric 		uid = 0;
44154974Seric 	else
44259113Seric 		uid = q->q_uid;
44363850Seric 	a = a->q_alias;
44459113Seric 
44559113Seric 	/* check to see if this is the same as last time */
44659113Seric 	if (lastctladdr != NULL && uid == lastuid &&
44759113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
44859113Seric 		return;
44959113Seric 	lastuid = uid;
45059113Seric 	lastctladdr = a;
45159113Seric 
45259113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
45359270Seric 		uname = "";
45459113Seric 	else
45559113Seric 		uname = pw->pw_name;
45659113Seric 
45768478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
45854974Seric }
4594632Seric /*
4604632Seric **  RUNQUEUE -- run the jobs in the queue.
4614632Seric **
4624632Seric **	Gets the stuff out of the queue in some presumably logical
4634632Seric **	order and processes them.
4644632Seric **
4654632Seric **	Parameters:
46624941Seric **		forkflag -- TRUE if the queue scanning should be done in
46724941Seric **			a child process.  We double-fork so it is not our
46824941Seric **			child and we don't have to clean up after it.
4694632Seric **
4704632Seric **	Returns:
4714632Seric **		none.
4724632Seric **
4734632Seric **	Side Effects:
4744632Seric **		runs things in the mail queue.
4754632Seric */
4764632Seric 
47755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
47855360Seric 
47968481Seric void
48055360Seric runqueue(forkflag)
4814639Seric 	bool forkflag;
4824632Seric {
48355360Seric 	register ENVELOPE *e;
48468481Seric 	int njobs;
48568481Seric 	int sequenceno = 0;
48655360Seric 	extern ENVELOPE BlankEnvelope;
48724953Seric 
4887466Seric 	/*
48924953Seric 	**  If no work will ever be selected, don't even bother reading
49024953Seric 	**  the queue.
49124953Seric 	*/
49224953Seric 
49351920Seric 	CurrentLA = getla();	/* get load average */
49440934Srick 
49558132Seric 	if (shouldqueue(0L, curtime()))
49624953Seric 	{
49724953Seric 		if (Verbose)
49824953Seric 			printf("Skipping queue run -- load average too high\n");
49958107Seric 		if (forkflag && QueueIntvl != 0)
50058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
50155360Seric 		return;
50224953Seric 	}
50324953Seric 
50424953Seric 	/*
5057466Seric 	**  See if we want to go off and do other useful work.
5067466Seric 	*/
5074639Seric 
5084639Seric 	if (forkflag)
5094639Seric 	{
5107943Seric 		int pid;
51165223Seric #ifdef SIGCHLD
51265223Seric 		extern void reapchild();
5137943Seric 
51465223Seric 		(void) setsignal(SIGCHLD, reapchild);
51565223Seric #endif
51665223Seric 
5177943Seric 		pid = dofork();
5187943Seric 		if (pid != 0)
5194639Seric 		{
5207943Seric 			/* parent -- pick up intermediate zombie */
52125184Seric #ifndef SIGCHLD
5229377Seric 			(void) waitfor(pid);
52356795Seric #endif /* SIGCHLD */
5247690Seric 			if (QueueIntvl != 0)
5259348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5264639Seric 			return;
5274639Seric 		}
5287943Seric 		/* child -- double fork */
52925184Seric #ifndef SIGCHLD
5307943Seric 		if (fork() != 0)
5317943Seric 			exit(EX_OK);
53256795Seric #else /* SIGCHLD */
53364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
53456795Seric #endif /* SIGCHLD */
5354639Seric 	}
53624941Seric 
53740934Srick 	setproctitle("running queue: %s", QueueDir);
53824941Seric 
5397876Seric # ifdef LOG
54058020Seric 	if (LogLevel > 69)
54155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
54255360Seric 			QueueDir, getpid(), forkflag);
54356795Seric # endif /* LOG */
5444639Seric 
5457466Seric 	/*
54610205Seric 	**  Release any resources used by the daemon code.
54710205Seric 	*/
54810205Seric 
54910205Seric # ifdef DAEMON
55010205Seric 	clrdaemon();
55156795Seric # endif /* DAEMON */
55210205Seric 
55364658Seric 	/* force it to run expensive jobs */
55464658Seric 	NoConnect = FALSE;
55564658Seric 
55610205Seric 	/*
55755360Seric 	**  Create ourselves an envelope
55855360Seric 	*/
55955360Seric 
56055360Seric 	CurEnv = &QueueEnvelope;
56158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
56255360Seric 	e->e_flags = BlankEnvelope.e_flags;
56355360Seric 
56455360Seric 	/*
56527175Seric 	**  Make sure the alias database is open.
56627175Seric 	*/
56727175Seric 
56860537Seric 	initmaps(FALSE, e);
56927175Seric 
57027175Seric 	/*
5717466Seric 	**  Start making passes through the queue.
5727466Seric 	**	First, read and sort the entire queue.
5737466Seric 	**	Then, process the work in that order.
5747466Seric 	**		But if you take too long, start over.
5757466Seric 	*/
5767466Seric 
5777943Seric 	/* order the existing work requests */
57868481Seric 	njobs = orderq(FALSE);
5797690Seric 
5807943Seric 	/* process them once at a time */
5817943Seric 	while (WorkQ != NULL)
5824639Seric 	{
5837943Seric 		WORK *w = WorkQ;
5847881Seric 
5857943Seric 		WorkQ = WorkQ->w_next;
58658884Seric 
58758884Seric 		/*
58858884Seric 		**  Ignore jobs that are too expensive for the moment.
58958884Seric 		*/
59058884Seric 
59168481Seric 		sequenceno++;
59258884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
59358884Seric 		{
59458884Seric 			if (Verbose)
59568481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
59668481Seric 					w->w_name + 2, sequenceno, njobs);
59758884Seric 		}
59858930Seric 		else
59958930Seric 		{
60064296Seric 			pid_t pid;
60164296Seric 			extern pid_t dowork();
60264296Seric 
60368481Seric 			if (Verbose)
60468481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
60568481Seric 					w->w_name + 2, sequenceno, njobs);
60664296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
60764296Seric 			errno = 0;
60866316Seric 			if (pid != 0)
60966316Seric 				(void) waitfor(pid);
61058930Seric 		}
6117943Seric 		free(w->w_name);
61268481Seric 		if (w->w_host)
61368481Seric 			free(w->w_host);
6147943Seric 		free((char *) w);
6154639Seric 	}
61629866Seric 
61729866Seric 	/* exit without the usual cleanup */
61855467Seric 	e->e_id = NULL;
61955467Seric 	finis();
6204634Seric }
6214634Seric /*
6224632Seric **  ORDERQ -- order the work queue.
6234632Seric **
6244632Seric **	Parameters:
62524941Seric **		doall -- if set, include everything in the queue (even
62624941Seric **			the jobs that cannot be run because the load
62724941Seric **			average is too high).  Otherwise, exclude those
62824941Seric **			jobs.
6294632Seric **
6304632Seric **	Returns:
63110121Seric **		The number of request in the queue (not necessarily
63210121Seric **		the number of requests in WorkQ however).
6334632Seric **
6344632Seric **	Side Effects:
6354632Seric **		Sets WorkQ to the queue of available work, in order.
6364632Seric */
6374632Seric 
63825687Seric # define NEED_P		001
63925687Seric # define NEED_T		002
64058318Seric # define NEED_R		004
64158318Seric # define NEED_S		010
6424632Seric 
64324941Seric orderq(doall)
64424941Seric 	bool doall;
6454632Seric {
64660219Seric 	register struct dirent *d;
6474632Seric 	register WORK *w;
6486625Sglickman 	DIR *f;
6494632Seric 	register int i;
65025687Seric 	WORK wlist[QUEUESIZE+1];
65110070Seric 	int wn = -1;
65268481Seric 	int wc;
6534632Seric 
65458318Seric 	if (tTd(41, 1))
65558318Seric 	{
65658318Seric 		printf("orderq:\n");
65758318Seric 		if (QueueLimitId != NULL)
65858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
65958318Seric 		if (QueueLimitSender != NULL)
66058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
66158318Seric 		if (QueueLimitRecipient != NULL)
66258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
66358318Seric 	}
66458318Seric 
6654632Seric 	/* clear out old WorkQ */
6664632Seric 	for (w = WorkQ; w != NULL; )
6674632Seric 	{
6684632Seric 		register WORK *nw = w->w_next;
6694632Seric 
6704632Seric 		WorkQ = nw;
6714632Seric 		free(w->w_name);
67268481Seric 		if (w->w_host)
67368481Seric 			free(w->w_host);
6744632Seric 		free((char *) w);
6754632Seric 		w = nw;
6764632Seric 	}
6774632Seric 
6784632Seric 	/* open the queue directory */
6798148Seric 	f = opendir(".");
6804632Seric 	if (f == NULL)
6814632Seric 	{
6828148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
68310070Seric 		return (0);
6844632Seric 	}
6854632Seric 
6864632Seric 	/*
6874632Seric 	**  Read the work directory.
6884632Seric 	*/
6894632Seric 
69010070Seric 	while ((d = readdir(f)) != NULL)
6914632Seric 	{
6929377Seric 		FILE *cf;
69364492Seric 		register char *p;
69468528Seric 		char lbuf[MAXNAME + 1];
69558318Seric 		extern bool strcontainedin();
6964632Seric 
6974632Seric 		/* is this an interesting entry? */
6987812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6994632Seric 			continue;
7004632Seric 
70158318Seric 		if (QueueLimitId != NULL &&
70258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
70358318Seric 			continue;
70458318Seric 
70568563Seric #ifdef PICKY_QF_NAME_CHECK
70658722Seric 		/*
70758722Seric 		**  Check queue name for plausibility.  This handles
70858722Seric 		**  both old and new type ids.
70958722Seric 		*/
71058722Seric 
71164492Seric 		p = d->d_name + 2;
71264492Seric 		if (isupper(p[0]) && isupper(p[2]))
71364492Seric 			p += 3;
71464492Seric 		else if (isupper(p[1]))
71564492Seric 			p += 2;
71664492Seric 		else
71764492Seric 			p = d->d_name;
71864492Seric 		for (i = 0; isdigit(*p); p++)
71964492Seric 			i++;
72064492Seric 		if (i < 5 || *p != '\0')
72158020Seric 		{
72258020Seric 			if (Verbose)
72358020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
72468563Seric # ifdef LOG
72568563Seric 			if (LogLevel > 0)
72668563Seric 				syslog(LOG_ALERT, "orderq: bogus qf name %s",
72758020Seric 					d->d_name);
72868563Seric # endif
72968528Seric 			if (strlen(d->d_name) > MAXNAME)
73068528Seric 				d->d_name[MAXNAME] = '\0';
73158020Seric 			strcpy(lbuf, d->d_name);
73258020Seric 			lbuf[0] = 'Q';
73358020Seric 			(void) rename(d->d_name, lbuf);
73458020Seric 			continue;
73558020Seric 		}
73668563Seric #endif
73758020Seric 
73868563Seric 		/* open control file (if not too many files) */
73925687Seric 		if (++wn >= QUEUESIZE)
74010070Seric 			continue;
74158318Seric 
7428148Seric 		cf = fopen(d->d_name, "r");
7434632Seric 		if (cf == NULL)
7444632Seric 		{
7457055Seric 			/* this may be some random person sending hir msgs */
7467055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
74710090Seric 			if (tTd(41, 2))
74810090Seric 				printf("orderq: cannot open %s (%d)\n",
74910090Seric 					d->d_name, errno);
7507055Seric 			errno = 0;
75110090Seric 			wn--;
7524632Seric 			continue;
7534632Seric 		}
75425687Seric 		w = &wlist[wn];
75525687Seric 		w->w_name = newstr(d->d_name);
75668481Seric 		w->w_host = NULL;
75768481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7584632Seric 
75925027Seric 		/* make sure jobs in creation don't clog queue */
76025687Seric 		w->w_pri = 0x7fffffff;
76125687Seric 		w->w_ctime = 0;
76225027Seric 
7634632Seric 		/* extract useful information */
76425687Seric 		i = NEED_P | NEED_T;
76558318Seric 		if (QueueLimitSender != NULL)
76658318Seric 			i |= NEED_S;
76768481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
76858318Seric 			i |= NEED_R;
76925687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7704632Seric 		{
77124954Seric 			extern long atol();
77258318Seric 			extern bool strcontainedin();
77324954Seric 
77424941Seric 			switch (lbuf[0])
7754632Seric 			{
77624941Seric 			  case 'P':
77725687Seric 				w->w_pri = atol(&lbuf[1]);
77825687Seric 				i &= ~NEED_P;
7794632Seric 				break;
78025013Seric 
78125013Seric 			  case 'T':
78225687Seric 				w->w_ctime = atol(&lbuf[1]);
78325687Seric 				i &= ~NEED_T;
78425013Seric 				break;
78558318Seric 
78658318Seric 			  case 'R':
78768481Seric 				if (w->w_host == NULL &&
78868481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
78968481Seric 					w->w_host = newstr(&p[1]);
79068481Seric 				if (QueueLimitRecipient == NULL ||
79158318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
79258318Seric 					i &= ~NEED_R;
79358318Seric 				break;
79458318Seric 
79558318Seric 			  case 'S':
79658318Seric 				if (QueueLimitSender != NULL &&
79758318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
79858318Seric 					i &= ~NEED_S;
79958318Seric 				break;
8004632Seric 			}
8014632Seric 		}
8024632Seric 		(void) fclose(cf);
80324953Seric 
80458318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
80558318Seric 		    bitset(NEED_R|NEED_S, i))
80624953Seric 		{
80724953Seric 			/* don't even bother sorting this job in */
80868481Seric 			free(w->w_name);
80968481Seric 			if (w->w_host)
81068481Seric 				free(w->w_host);
81124953Seric 			wn--;
81224953Seric 		}
8134632Seric 	}
8146625Sglickman 	(void) closedir(f);
81510090Seric 	wn++;
8164632Seric 
81768481Seric 	wc = min(wn, QUEUESIZE);
8184632Seric 
81968481Seric 	if (QueueSortOrder == QS_BYHOST)
82068481Seric 	{
82168481Seric 		extern workcmpf1();
82268481Seric 		extern workcmpf2();
82367612Seric 
82468481Seric 		/*
82568481Seric 		**  Sort the work directory for the first time,
82668481Seric 		**  based on host name, lock status, and priority.
82768481Seric 		*/
82868481Seric 
82968481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
83068481Seric 
83168481Seric 		/*
83268481Seric 		**  If one message to host is locked, "lock" all messages
83368481Seric 		**  to that host.
83468481Seric 		*/
83568481Seric 
83668481Seric 		i = 0;
83768481Seric 		while (i < wc)
83868481Seric 		{
83968481Seric 			if (!wlist[i].w_lock)
84068481Seric 			{
84168481Seric 				i++;
84268481Seric 				continue;
84368481Seric 			}
84468481Seric 			w = &wlist[i];
84568481Seric 			while (++i < wc)
84668481Seric 			{
84768481Seric 				if (wlist[i].w_host == NULL &&
84868481Seric 				    w->w_host == NULL)
84968481Seric 					wlist[i].w_lock = TRUE;
85068481Seric 				else if (wlist[i].w_host != NULL &&
85168481Seric 					 w->w_host != NULL &&
85268481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
85368481Seric 					wlist[i].w_lock = TRUE;
85468481Seric 				else
85568481Seric 					break;
85668481Seric 			}
85768481Seric 		}
85868481Seric 
85968481Seric 		/*
86068481Seric 		**  Sort the work directory for the second time,
86168481Seric 		**  based on lock status, host name, and priority.
86268481Seric 		*/
86368481Seric 
86468481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
86568481Seric 	}
86668481Seric 	else
86768481Seric 	{
86868481Seric 		extern workcmpf0();
86968481Seric 
87068481Seric 		/*
87168481Seric 		**  Simple sort based on queue priority only.
87268481Seric 		*/
87368481Seric 
87468481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
87568481Seric 	}
87668481Seric 
8774632Seric 	/*
8784632Seric 	**  Convert the work list into canonical form.
8799377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8804632Seric 	*/
8814632Seric 
88224981Seric 	WorkQ = NULL;
88368481Seric 	for (i = wc; --i >= 0; )
8844632Seric 	{
8854632Seric 		w = (WORK *) xalloc(sizeof *w);
8864632Seric 		w->w_name = wlist[i].w_name;
88768481Seric 		w->w_host = wlist[i].w_host;
88868481Seric 		w->w_lock = wlist[i].w_lock;
8894632Seric 		w->w_pri = wlist[i].w_pri;
89025013Seric 		w->w_ctime = wlist[i].w_ctime;
89124981Seric 		w->w_next = WorkQ;
89224981Seric 		WorkQ = w;
8934632Seric 	}
8944632Seric 
8957677Seric 	if (tTd(40, 1))
8964632Seric 	{
8974632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
8985037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
8994632Seric 	}
90010070Seric 
90110090Seric 	return (wn);
9024632Seric }
9034632Seric /*
90468481Seric **  WORKCMPF0 -- simple priority-only compare function.
9054632Seric **
9064632Seric **	Parameters:
9074632Seric **		a -- the first argument.
9084632Seric **		b -- the second argument.
9094632Seric **
9104632Seric **	Returns:
91124981Seric **		-1 if a < b
91224981Seric **		 0 if a == b
91324981Seric **		+1 if a > b
9144632Seric **
9154632Seric **	Side Effects:
9164632Seric **		none.
9174632Seric */
9184632Seric 
91968481Seric workcmpf0(a, b)
9205037Seric 	register WORK *a;
9215037Seric 	register WORK *b;
9224632Seric {
92357438Seric 	long pa = a->w_pri;
92457438Seric 	long pb = b->w_pri;
92524941Seric 
92624941Seric 	if (pa == pb)
92768481Seric 		return 0;
92824941Seric 	else if (pa > pb)
92968481Seric 		return 1;
93024981Seric 	else
93168481Seric 		return -1;
9324632Seric }
9334632Seric /*
93468481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
93568481Seric **
93668481Seric **	Sorts on host name, lock status, and priority in that order.
93768481Seric **
93868481Seric **	Parameters:
93968481Seric **		a -- the first argument.
94068481Seric **		b -- the second argument.
94168481Seric **
94268481Seric **	Returns:
94368481Seric **		<0 if a < b
94468481Seric **		 0 if a == b
94568481Seric **		>0 if a > b
94668481Seric **
94768481Seric **	Side Effects:
94868481Seric **		none.
94968481Seric */
95068481Seric 
95168481Seric workcmpf1(a, b)
95268481Seric 	register WORK *a;
95368481Seric 	register WORK *b;
95468481Seric {
95568481Seric 	int i;
95668481Seric 
95768481Seric 	/* host name */
95868481Seric 	if (a->w_host != NULL && b->w_host == NULL)
95968481Seric 		return 1;
96068481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
96168481Seric 		return -1;
96268481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
96368481Seric 	    (i = strcmp(a->w_host, b->w_host)))
96468481Seric 		return i;
96568481Seric 
96668481Seric 	/* lock status */
96768481Seric 	if (a->w_lock != b->w_lock)
96868481Seric 		return b->w_lock - a->w_lock;
96968481Seric 
97068481Seric 	/* job priority */
97168481Seric 	return a->w_pri - b->w_pri;
97268481Seric }
97368481Seric /*
97468481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
97568481Seric **
97668481Seric **	Sorts on lock status, host name, and priority in that order.
97768481Seric **
97868481Seric **	Parameters:
97968481Seric **		a -- the first argument.
98068481Seric **		b -- the second argument.
98168481Seric **
98268481Seric **	Returns:
98368481Seric **		<0 if a < b
98468481Seric **		 0 if a == b
98568481Seric **		>0 if a > b
98668481Seric **
98768481Seric **	Side Effects:
98868481Seric **		none.
98968481Seric */
99068481Seric 
99168481Seric workcmpf2(a, b)
99268481Seric 	register WORK *a;
99368481Seric 	register WORK *b;
99468481Seric {
99568481Seric 	int i;
99668481Seric 
99768481Seric 	/* lock status */
99868481Seric 	if (a->w_lock != b->w_lock)
99968481Seric 		return a->w_lock - b->w_lock;
100068481Seric 
100168481Seric 	/* host name */
100268481Seric 	if (a->w_host != NULL && b->w_host == NULL)
100368481Seric 		return 1;
100468481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
100568481Seric 		return -1;
100668481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
100768481Seric 	    (i = strcmp(a->w_host, b->w_host)))
100868481Seric 		return i;
100968481Seric 
101068481Seric 	/* job priority */
101168481Seric 	return a->w_pri - b->w_pri;
101268481Seric }
101368481Seric /*
10144632Seric **  DOWORK -- do a work request.
10154632Seric **
10164632Seric **	Parameters:
101758884Seric **		id -- the ID of the job to run.
101858884Seric **		forkflag -- if set, run this in background.
101958924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
102058924Seric **			This is used when expanding aliases in the queue.
102161094Seric **			If forkflag is also set, it doesn't wait for the
102261094Seric **			child.
102358884Seric **		e - the envelope in which to run it.
10244632Seric **
10254632Seric **	Returns:
102664296Seric **		process id of process that is running the queue job.
10274632Seric **
10284632Seric **	Side Effects:
10294632Seric **		The work request is satisfied if possible.
10304632Seric */
10314632Seric 
103264296Seric pid_t
103358924Seric dowork(id, forkflag, requeueflag, e)
103458884Seric 	char *id;
103558884Seric 	bool forkflag;
103658924Seric 	bool requeueflag;
103755012Seric 	register ENVELOPE *e;
10384632Seric {
103964296Seric 	register pid_t pid;
104051920Seric 	extern bool readqf();
10414632Seric 
10427677Seric 	if (tTd(40, 1))
104358884Seric 		printf("dowork(%s)\n", id);
10444632Seric 
10454632Seric 	/*
104624941Seric 	**  Fork for work.
104724941Seric 	*/
104824941Seric 
104958884Seric 	if (forkflag)
105024941Seric 	{
105164296Seric 		pid = fork();
105264296Seric 		if (pid < 0)
105324941Seric 		{
105424941Seric 			syserr("dowork: cannot fork");
105564296Seric 			return 0;
105624941Seric 		}
105764839Seric 		else if (pid > 0)
105864839Seric 		{
105964839Seric 			/* parent -- clean out connection cache */
106064839Seric 			mci_flush(FALSE, NULL);
106164839Seric 		}
106224941Seric 	}
106324941Seric 	else
106424941Seric 	{
106564296Seric 		pid = 0;
106624941Seric 	}
106724941Seric 
106864296Seric 	if (pid == 0)
10694632Seric 	{
10704632Seric 		/*
10714632Seric 		**  CHILD
10728148Seric 		**	Lock the control file to avoid duplicate deliveries.
10738148Seric 		**		Then run the file as though we had just read it.
10747350Seric 		**	We save an idea of the temporary name so we
10757350Seric 		**		can recover on interrupt.
10764632Seric 		*/
10774632Seric 
10787763Seric 		/* set basic modes, etc. */
10797356Seric 		(void) alarm(0);
108055012Seric 		clearenvelope(e, FALSE);
108164554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
108258734Seric 		e->e_errormode = EM_MAIL;
108358884Seric 		e->e_id = id;
108464765Seric 		GrabTo = UseErrorsTo = FALSE;
108566316Seric 		ExitStat = EX_OK;
108663846Seric 		if (forkflag)
108764554Seric 		{
108864296Seric 			disconnect(1, e);
108964554Seric 			OpMode = MD_DELIVER;
109064554Seric 		}
10917876Seric # ifdef LOG
109258020Seric 		if (LogLevel > 76)
109355012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
10947881Seric 			       getpid());
109556795Seric # endif /* LOG */
10967763Seric 
10977763Seric 		/* don't use the headers from sendmail.cf... */
109855012Seric 		e->e_header = NULL;
10997763Seric 
110051920Seric 		/* read the queue control file -- return if locked */
110165200Seric 		if (!readqf(e))
11026980Seric 		{
110358884Seric 			if (tTd(40, 4))
110458884Seric 				printf("readqf(%s) failed\n", e->e_id);
110558884Seric 			if (forkflag)
110624941Seric 				exit(EX_OK);
110724941Seric 			else
110866316Seric 				return 0;
11096980Seric 		}
11106980Seric 
111155012Seric 		e->e_flags |= EF_INQUEUE;
11126980Seric 
111368481Seric 		/* if this has been tried recently, let it be */
111468481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
111568481Seric 		{
111668481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
111758924Seric 
111868481Seric 			e->e_flags |= EF_KEEPQUEUE;
111968481Seric 			if (Verbose || tTd(40, 8))
112068481Seric 				printf("%s: too young (%s)\n",
112168481Seric 					e->e_id, howlong);
112268481Seric #ifdef LOG
112368481Seric 			if (LogLevel > 19)
112468481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
112568481Seric 					e->e_id, howlong);
112668481Seric #endif
112768481Seric 		}
112868481Seric 		else
112968481Seric 		{
113068481Seric 			eatheader(e, requeueflag);
11316980Seric 
113268481Seric 			if (requeueflag)
113368481Seric 				queueup(e, TRUE, FALSE);
113468481Seric 
113568481Seric 			/* do the delivery */
113668481Seric 			sendall(e, SM_DELIVER);
113768481Seric 		}
113868481Seric 
11396980Seric 		/* finish up and exit */
114058884Seric 		if (forkflag)
114124941Seric 			finis();
114224941Seric 		else
114355012Seric 			dropenvelope(e);
11444632Seric 	}
114564296Seric 	e->e_id = NULL;
114664296Seric 	return pid;
11474632Seric }
11484632Seric /*
11494632Seric **  READQF -- read queue file and set up environment.
11504632Seric **
11514632Seric **	Parameters:
11529377Seric **		e -- the envelope of the job to run.
11534632Seric **
11544632Seric **	Returns:
115551920Seric **		TRUE if it successfully read the queue file.
115651920Seric **		FALSE otherwise.
11574632Seric **
11584632Seric **	Side Effects:
115951920Seric **		The queue file is returned locked.
11604632Seric */
11614632Seric 
116251920Seric bool
116365200Seric readqf(e)
11649377Seric 	register ENVELOPE *e;
11654632Seric {
116617477Seric 	register FILE *qfp;
116754974Seric 	ADDRESS *ctladdr;
116856400Seric 	struct stat st;
116957135Seric 	char *bp;
117068481Seric 	int qfver = 0;
117168481Seric 	register char *p;
117268481Seric 	char *orcpt = NULL;
117358915Seric 	char qf[20];
117457135Seric 	char buf[MAXLINE];
117524954Seric 	extern long atol();
117654974Seric 	extern ADDRESS *setctluser();
117768490Seric 	extern void loseqfile();
11784632Seric 
11794632Seric 	/*
118017468Seric 	**  Read and process the file.
11814632Seric 	*/
11824632Seric 
118358915Seric 	strcpy(qf, queuename(e, 'q'));
118451937Seric 	qfp = fopen(qf, "r+");
118517477Seric 	if (qfp == NULL)
118617477Seric 	{
118758884Seric 		if (tTd(40, 8))
118858884Seric 			printf("readqf(%s): fopen failure (%s)\n",
118958884Seric 				qf, errstring(errno));
119040934Srick 		if (errno != ENOENT)
119140934Srick 			syserr("readqf: no control file %s", qf);
119251920Seric 		return FALSE;
119317477Seric 	}
119440934Srick 
119564335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
119664277Seric 	{
119764277Seric 		/* being processed by another queuer */
119868481Seric 		if (Verbose || tTd(40, 8))
119964277Seric 			printf("%s: locked\n", e->e_id);
120064277Seric # ifdef LOG
120164277Seric 		if (LogLevel > 19)
120264277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
120364277Seric # endif /* LOG */
120464277Seric 		(void) fclose(qfp);
120564277Seric 		return FALSE;
120664277Seric 	}
120764277Seric 
120856400Seric 	/*
120956400Seric 	**  Check the queue file for plausibility to avoid attacks.
121056400Seric 	*/
121156400Seric 
121256400Seric 	if (fstat(fileno(qfp), &st) < 0)
121356400Seric 	{
121456400Seric 		/* must have been being processed by someone else */
121558884Seric 		if (tTd(40, 8))
121658884Seric 			printf("readqf(%s): fstat failure (%s)\n",
121758884Seric 				qf, errstring(errno));
121856400Seric 		fclose(qfp);
121956400Seric 		return FALSE;
122056400Seric 	}
122156400Seric 
122268563Seric 	if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode))
122356400Seric 	{
122456400Seric # ifdef LOG
122556400Seric 		if (LogLevel > 0)
122656400Seric 		{
122756400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
122856400Seric 				e->e_id, st.st_uid, st.st_mode);
122956400Seric 		}
123056795Seric # endif /* LOG */
123158884Seric 		if (tTd(40, 8))
123258884Seric 			printf("readqf(%s): bogus file\n", qf);
123368490Seric 		loseqfile(e, "bogus file uid in mqueue");
123456400Seric 		fclose(qfp);
123556400Seric 		return FALSE;
123656400Seric 	}
123756400Seric 
123864277Seric 	if (st.st_size == 0)
123940934Srick 	{
124064277Seric 		/* must be a bogus file -- just remove it */
124164277Seric 		(void) unlink(qf);
124264277Seric 		fclose(qfp);
124351920Seric 		return FALSE;
124440934Srick 	}
124540934Srick 
124664277Seric 	if (st.st_nlink == 0)
124759101Seric 	{
124864277Seric 		/*
124964277Seric 		**  Race condition -- we got a file just as it was being
125064277Seric 		**  unlinked.  Just assume it is zero length.
125164277Seric 		*/
125264277Seric 
125359101Seric 		fclose(qfp);
125459101Seric 		return FALSE;
125559101Seric 	}
125659101Seric 
125764277Seric 	/* good file -- save this lock */
125851920Seric 	e->e_lockfp = qfp;
125951920Seric 
126040934Srick 	/* do basic system initialization */
126155012Seric 	initsys(e);
126265164Seric 	define('i', e->e_id, e);
126340934Srick 
12649377Seric 	LineNumber = 0;
126563850Seric 	e->e_flags |= EF_GLOBALERRS;
126663850Seric 	OpMode = MD_DELIVER;
126754974Seric 	ctladdr = NULL;
126868481Seric 	e->e_dfino = -1;
126968564Seric 	e->e_msgsize = -1;
127057135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12714632Seric 	{
127258737Seric 		register char *p;
127368481Seric 		u_long qflags;
127468481Seric 		ADDRESS *q;
127557529Seric 
127626504Seric 		if (tTd(40, 4))
127757135Seric 			printf("+++++ %s\n", bp);
127857135Seric 		switch (bp[0])
12794632Seric 		{
128068481Seric 		  case 'V':		/* queue file version number */
128168481Seric 			qfver = atoi(&bp[1]);
128268481Seric 			if (qfver > QF_VERSION)
128368481Seric 			{
128468481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
128568481Seric 					qfver, QF_VERSION);
128668481Seric 			}
128768481Seric 			break;
128868481Seric 
128940973Sbostic 		  case 'C':		/* specify controlling user */
129057135Seric 			ctladdr = setctluser(&bp[1]);
129140973Sbostic 			break;
129240973Sbostic 
129368481Seric 		  case 'Q':		/* original recipient */
129468481Seric 			orcpt = newstr(&bp[1]);
129568481Seric 			break;
129668481Seric 
12974632Seric 		  case 'R':		/* specify recipient */
129868481Seric 			p = bp;
129968481Seric 			qflags = 0;
130068481Seric 			if (qfver >= 1)
130168481Seric 			{
130268481Seric 				/* get flag bits */
130368481Seric 				while (*++p != '\0' && *p != ':')
130468481Seric 				{
130568481Seric 					switch (*p)
130668481Seric 					{
130768481Seric 					  case 'S':
130868481Seric 						qflags |= QPINGONSUCCESS;
130968481Seric 						break;
131068481Seric 
131168481Seric 					  case 'F':
131268481Seric 						qflags |= QPINGONFAILURE;
131368481Seric 						break;
131468481Seric 
131568481Seric 					  case 'D':
131668481Seric 						qflags |= QPINGONDELAY;
131768481Seric 						break;
131868481Seric 
131968481Seric 					  case 'P':
132068481Seric 						qflags |= QPRIMARY;
132168481Seric 						break;
132268481Seric 					}
132368481Seric 				}
132468481Seric 			}
132568481Seric 			else
132668481Seric 				qflags |= QPRIMARY;
132768481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
132868481Seric 			if (q != NULL)
132968481Seric 			{
133068481Seric 				q->q_alias = ctladdr;
133168481Seric 				q->q_flags |= qflags;
133268481Seric 				q->q_orcpt = orcpt;
133368481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
133468481Seric 			}
133568481Seric 			orcpt = NULL;
13364632Seric 			break;
13374632Seric 
133825687Seric 		  case 'E':		/* specify error recipient */
133968558Seric 			/* no longer used */
134025687Seric 			break;
134125687Seric 
13424632Seric 		  case 'H':		/* header */
134357135Seric 			(void) chompheader(&bp[1], FALSE, e);
13444632Seric 			break;
13454632Seric 
134610108Seric 		  case 'M':		/* message */
134764705Seric 			/* ignore this; we want a new message next time */
134810108Seric 			break;
134910108Seric 
13504632Seric 		  case 'S':		/* sender */
135158704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13524632Seric 			break;
13534632Seric 
135459093Seric 		  case 'B':		/* body type */
135559093Seric 			e->e_bodytype = newstr(&bp[1]);
135659093Seric 			break;
135759093Seric 
13584632Seric 		  case 'D':		/* data file name */
135968564Seric 			/* obsolete -- ignore */
13604632Seric 			break;
13614632Seric 
13627860Seric 		  case 'T':		/* init time */
136357135Seric 			e->e_ctime = atol(&bp[1]);
13644632Seric 			break;
13654632Seric 
136668481Seric 		  case 'I':		/* data file's inode number */
136768481Seric 			if (e->e_dfino == -1)
136868481Seric 				e->e_dfino = atol(&buf[1]);
136968481Seric 			break;
137068481Seric 
137168481Seric 		  case 'K':		/* time of last deliver attempt */
137268481Seric 			e->e_dtime = atol(&buf[1]);
137368481Seric 			break;
137468481Seric 
137568481Seric 		  case 'N':		/* number of delivery attempts */
137668481Seric 			e->e_ntries = atoi(&buf[1]);
137768481Seric 			break;
137868481Seric 
13794634Seric 		  case 'P':		/* message priority */
138057135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
13814634Seric 			break;
13824634Seric 
138358737Seric 		  case 'F':		/* flag bits */
138458737Seric 			for (p = &bp[1]; *p != '\0'; p++)
138558737Seric 			{
138658737Seric 				switch (*p)
138758737Seric 				{
138858737Seric 				  case 'w':	/* warning sent */
138958737Seric 					e->e_flags |= EF_WARNING;
139058737Seric 					break;
139158737Seric 
139258737Seric 				  case 'r':	/* response */
139358737Seric 					e->e_flags |= EF_RESPONSE;
139458737Seric 					break;
139568481Seric 
139668481Seric 				  case '8':	/* has 8 bit data */
139768481Seric 					e->e_flags |= EF_HAS8BIT;
139868481Seric 					break;
139958737Seric 				}
140058737Seric 			}
140158737Seric 			break;
140258737Seric 
140368481Seric 		  case 'Z':		/* original envelope id from ESMTP */
140468481Seric 			e->e_envid = newstr(&bp[1]);
140568481Seric 			break;
140668481Seric 
140753400Seric 		  case '$':		/* define macro */
140857135Seric 			define(bp[1], newstr(&bp[2]), e);
140953400Seric 			break;
141053400Seric 
141124941Seric 		  case '\0':		/* blank line; ignore */
141224941Seric 			break;
141324941Seric 
14144632Seric 		  default:
141565960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
141665200Seric 				qf, LineNumber, bp);
141763753Seric 			fclose(qfp);
141868490Seric 			loseqfile(e, "unrecognized line");
141963753Seric 			return FALSE;
14204632Seric 		}
142157135Seric 
142257135Seric 		if (bp != buf)
142357135Seric 			free(bp);
14244632Seric 	}
14259377Seric 
142624941Seric 	/*
142724941Seric 	**  If we haven't read any lines, this queue file is empty.
142824941Seric 	**  Arrange to remove it without referencing any null pointers.
142924941Seric 	*/
143024941Seric 
143124941Seric 	if (LineNumber == 0)
143224941Seric 	{
143324941Seric 		errno = 0;
143424941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
143524941Seric 	}
143668564Seric 	else
143768564Seric 	{
143868564Seric 		/*
143968564Seric 		**  Arrange to read the data file.
144068564Seric 		*/
144168564Seric 
144268564Seric 		p = queuename(e, 'd');
144368564Seric 		e->e_dfp = fopen(p, "r");
144468564Seric 		if (e->e_dfp == NULL)
144568564Seric 		{
144668564Seric 			syserr("readqf: cannot open %s", p);
144768564Seric 		}
1448*68603Seric 		else
144968564Seric 		{
1450*68603Seric 			e->e_flags |= EF_HAS_DF;
1451*68603Seric 			if (fstat(fileno(e->e_dfp), &st) >= 0)
1452*68603Seric 			{
1453*68603Seric 				e->e_msgsize = st.st_size;
1454*68603Seric 				e->e_dfdev = st.st_dev;
1455*68603Seric 				e->e_dfino = st.st_ino;
1456*68603Seric 			}
145768564Seric 		}
145868564Seric 	}
145968564Seric 
146051920Seric 	return TRUE;
14614632Seric }
14624632Seric /*
14639630Seric **  PRINTQUEUE -- print out a representation of the mail queue
14649630Seric **
14659630Seric **	Parameters:
14669630Seric **		none.
14679630Seric **
14689630Seric **	Returns:
14699630Seric **		none.
14709630Seric **
14719630Seric **	Side Effects:
14729630Seric **		Prints a listing of the mail queue on the standard output.
14739630Seric */
14745182Seric 
14759630Seric printqueue()
14769630Seric {
14779630Seric 	register WORK *w;
14789630Seric 	FILE *f;
147910070Seric 	int nrequests;
14809630Seric 	char buf[MAXLINE];
14819630Seric 
14829630Seric 	/*
148358250Seric 	**  Check for permission to print the queue
148458250Seric 	*/
148558250Seric 
148664333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
148758250Seric 	{
148858250Seric 		struct stat st;
148958523Seric # ifdef NGROUPS
149058523Seric 		int n;
149163937Seric 		GIDSET_T gidset[NGROUPS];
149258523Seric # endif
149358250Seric 
149458517Seric 		if (stat(QueueDir, &st) < 0)
149558250Seric 		{
149658250Seric 			syserr("Cannot stat %s", QueueDir);
149758250Seric 			return;
149858250Seric 		}
149958523Seric # ifdef NGROUPS
150058523Seric 		n = getgroups(NGROUPS, gidset);
150158523Seric 		while (--n >= 0)
150258523Seric 		{
150358523Seric 			if (gidset[n] == st.st_gid)
150458523Seric 				break;
150558523Seric 		}
150658523Seric 		if (n < 0)
150758523Seric # else
150863787Seric 		if (RealGid != st.st_gid)
150958523Seric # endif
151058250Seric 		{
151158250Seric 			usrerr("510 You are not permitted to see the queue");
151258250Seric 			setstat(EX_NOPERM);
151358250Seric 			return;
151458250Seric 		}
151558250Seric 	}
151658250Seric 
151758250Seric 	/*
15189630Seric 	**  Read and order the queue.
15199630Seric 	*/
15209630Seric 
152124941Seric 	nrequests = orderq(TRUE);
15229630Seric 
15239630Seric 	/*
15249630Seric 	**  Print the work list that we have read.
15259630Seric 	*/
15269630Seric 
15279630Seric 	/* first see if there is anything */
152810070Seric 	if (nrequests <= 0)
15299630Seric 	{
153010070Seric 		printf("Mail queue is empty\n");
15319630Seric 		return;
15329630Seric 	}
15339630Seric 
153451920Seric 	CurrentLA = getla();	/* get load average */
153540934Srick 
153610096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
153725687Seric 	if (nrequests > QUEUESIZE)
153825687Seric 		printf(", only %d printed", QUEUESIZE);
153924979Seric 	if (Verbose)
154058716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
154124979Seric 	else
154258716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
15439630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
15449630Seric 	{
15459630Seric 		struct stat st;
154610070Seric 		auto time_t submittime = 0;
154768565Seric 		long dfsize;
154858737Seric 		int flags = 0;
154968481Seric 		int qfver;
155010108Seric 		char message[MAXLINE];
155168528Seric 		char bodytype[MAXNAME + 1];
15529630Seric 
155364355Seric 		printf("%8s", w->w_name + 2);
155417468Seric 		f = fopen(w->w_name, "r");
155517468Seric 		if (f == NULL)
155617468Seric 		{
155764355Seric 			printf(" (job completed)\n");
155817468Seric 			errno = 0;
155917468Seric 			continue;
156017468Seric 		}
156168565Seric 		w->w_name[0] = 'd';
156268565Seric 		if (stat(w->w_name, &st) >= 0)
156368565Seric 			dfsize = st.st_size;
156468565Seric 		else
156568565Seric 			dfsize = -1;
156668481Seric 		if (w->w_lock)
156710070Seric 			printf("*");
156857438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
156924941Seric 			printf("X");
157010070Seric 		else
157110070Seric 			printf(" ");
157210070Seric 		errno = 0;
157317468Seric 
157459093Seric 		message[0] = bodytype[0] = '\0';
157568481Seric 		qfver = 0;
15769630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
15779630Seric 		{
157853400Seric 			register int i;
157958737Seric 			register char *p;
158053400Seric 
15819630Seric 			fixcrlf(buf, TRUE);
15829630Seric 			switch (buf[0])
15839630Seric 			{
158468481Seric 			  case 'V':	/* queue file version */
158568481Seric 				qfver = atoi(&buf[1]);
158668481Seric 				break;
158768481Seric 
158810108Seric 			  case 'M':	/* error message */
158953400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
159058737Seric 					i = sizeof message - 1;
159153400Seric 				bcopy(&buf[1], message, i);
159253400Seric 				message[i] = '\0';
159310108Seric 				break;
159410108Seric 
159559093Seric 			  case 'B':	/* body type */
159659093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
159759093Seric 					i = sizeof bodytype - 1;
159859093Seric 				bcopy(&buf[1], bodytype, i);
159959093Seric 				bodytype[i] = '\0';
160059093Seric 				break;
160159093Seric 
16029630Seric 			  case 'S':	/* sender name */
160324979Seric 				if (Verbose)
160458737Seric 					printf("%8ld %10ld%c%.12s %.38s",
160558737Seric 					    dfsize,
160658737Seric 					    w->w_pri,
160758737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
160858737Seric 					    ctime(&submittime) + 4,
160924979Seric 					    &buf[1]);
161024979Seric 				else
161124979Seric 					printf("%8ld %.16s %.45s", dfsize,
161224979Seric 					    ctime(&submittime), &buf[1]);
161359093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
161459093Seric 				{
161559093Seric 					printf("\n    %10.10s", bodytype);
161659093Seric 					if (message[0] != '\0')
161759093Seric 						printf("   (%.60s)", message);
161859093Seric 				}
16199630Seric 				break;
162051920Seric 
162140973Sbostic 			  case 'C':	/* controlling user */
162254974Seric 				if (Verbose)
162358716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
162458716Seric 						&buf[1]);
162540973Sbostic 				break;
16269630Seric 
16279630Seric 			  case 'R':	/* recipient name */
162868481Seric 				p = &buf[1];
162968481Seric 				if (qfver >= 1)
163068481Seric 				{
163168481Seric 					p = strchr(p, ':');
163268481Seric 					if (p == NULL)
163368481Seric 						break;
163468481Seric 					p++;
163568481Seric 				}
163624979Seric 				if (Verbose)
163768481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
163824979Seric 				else
163968481Seric 					printf("\n\t\t\t\t   %.45s", p);
16409630Seric 				break;
16419630Seric 
16429630Seric 			  case 'T':	/* creation time */
164324941Seric 				submittime = atol(&buf[1]);
16449630Seric 				break;
164510070Seric 
164658737Seric 			  case 'F':	/* flag bits */
164758737Seric 				for (p = &buf[1]; *p != '\0'; p++)
164858737Seric 				{
164958737Seric 					switch (*p)
165058737Seric 					{
165158737Seric 					  case 'w':
165258737Seric 						flags |= EF_WARNING;
165358737Seric 						break;
165458737Seric 					}
165558737Seric 				}
16569630Seric 			}
16579630Seric 		}
165810070Seric 		if (submittime == (time_t) 0)
165910070Seric 			printf(" (no control file)");
16609630Seric 		printf("\n");
166123098Seric 		(void) fclose(f);
16629630Seric 	}
16639630Seric }
16649630Seric 
166556795Seric # endif /* QUEUE */
166617468Seric /*
166717468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
166817468Seric **
166917468Seric **	Assigns an id code if one does not already exist.
167017468Seric **	This code is very careful to avoid trashing existing files
167117468Seric **	under any circumstances.
167217468Seric **
167317468Seric **	Parameters:
167417468Seric **		e -- envelope to build it in/from.
167517468Seric **		type -- the file type, used as the first character
167617468Seric **			of the file name.
167717468Seric **
167817468Seric **	Returns:
167917468Seric **		a pointer to the new file name (in a static buffer).
168017468Seric **
168117468Seric **	Side Effects:
168251920Seric **		If no id code is already assigned, queuename will
168351920Seric **		assign an id code, create a qf file, and leave a
168451920Seric **		locked, open-for-write file pointer in the envelope.
168517468Seric */
168617468Seric 
168717468Seric char *
168817468Seric queuename(e, type)
168917468Seric 	register ENVELOPE *e;
169059700Seric 	int type;
169117468Seric {
169217468Seric 	static int pid = -1;
169358741Seric 	static char c0;
169458741Seric 	static char c1;
169558741Seric 	static char c2;
169658716Seric 	time_t now;
169758716Seric 	struct tm *tm;
169868528Seric 	static char buf[MAXNAME + 1];
169917468Seric 
170017468Seric 	if (e->e_id == NULL)
170117468Seric 	{
170217468Seric 		char qf[20];
170317468Seric 
170417468Seric 		/* find a unique id */
170517468Seric 		if (pid != getpid())
170617468Seric 		{
170717468Seric 			/* new process -- start back at "AA" */
170817468Seric 			pid = getpid();
170958716Seric 			now = curtime();
171058716Seric 			tm = localtime(&now);
171158716Seric 			c0 = 'A' + tm->tm_hour;
171217468Seric 			c1 = 'A';
171317468Seric 			c2 = 'A' - 1;
171417468Seric 		}
171558716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
171617468Seric 
171717468Seric 		while (c1 < '~' || c2 < 'Z')
171817468Seric 		{
171917468Seric 			int i;
172017468Seric 
172117468Seric 			if (c2 >= 'Z')
172217468Seric 			{
172317468Seric 				c1++;
172417468Seric 				c2 = 'A' - 1;
172517468Seric 			}
172658716Seric 			qf[3] = c1;
172758716Seric 			qf[4] = ++c2;
172817468Seric 			if (tTd(7, 20))
172940934Srick 				printf("queuename: trying \"%s\"\n", qf);
173017468Seric 
173140934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
173251920Seric 			if (i < 0)
173351920Seric 			{
173451920Seric 				if (errno == EEXIST)
173551920Seric 					continue;
173664705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
173764705Seric 					qf, QueueDir, geteuid());
173851920Seric 				exit(EX_UNAVAILABLE);
173951920Seric 			}
174064335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
174151920Seric 			{
174251920Seric 				e->e_lockfp = fdopen(i, "w");
174340934Srick 				break;
174417468Seric 			}
174551920Seric 
174651920Seric 			/* a reader got the file; abandon it and try again */
174751920Seric 			(void) close(i);
174817468Seric 		}
174917468Seric 		if (c1 >= '~' && c2 >= 'Z')
175017468Seric 		{
175164705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
175264705Seric 				qf, QueueDir, geteuid());
175317468Seric 			exit(EX_OSERR);
175417468Seric 		}
175517468Seric 		e->e_id = newstr(&qf[2]);
175617468Seric 		define('i', e->e_id, e);
175717468Seric 		if (tTd(7, 1))
175817468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
175964745Seric 		if (tTd(7, 9))
176064745Seric 		{
176164745Seric 			printf("  lockfd=");
176264745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
176364745Seric 		}
176417468Seric # ifdef LOG
176558020Seric 		if (LogLevel > 93)
176617468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
176756795Seric # endif /* LOG */
176817468Seric 	}
176917468Seric 
177017468Seric 	if (type == '\0')
177117468Seric 		return (NULL);
177217468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
177317468Seric 	if (tTd(7, 2))
177417468Seric 		printf("queuename: %s\n", buf);
177517468Seric 	return (buf);
177617468Seric }
177717468Seric /*
177817468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
177917468Seric **
178017468Seric **	Parameters:
178117468Seric **		e -- the envelope to unlock.
178217468Seric **
178317468Seric **	Returns:
178417468Seric **		none
178517468Seric **
178617468Seric **	Side Effects:
178717468Seric **		unlocks the queue for `e'.
178817468Seric */
178917468Seric 
179017468Seric unlockqueue(e)
179117468Seric 	ENVELOPE *e;
179217468Seric {
179358680Seric 	if (tTd(51, 4))
179458680Seric 		printf("unlockqueue(%s)\n", e->e_id);
179558680Seric 
179651920Seric 	/* if there is a lock file in the envelope, close it */
179751920Seric 	if (e->e_lockfp != NULL)
179858680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
179951920Seric 	e->e_lockfp = NULL;
180051920Seric 
180158728Seric 	/* don't create a queue id if we don't already have one */
180258728Seric 	if (e->e_id == NULL)
180358728Seric 		return;
180458728Seric 
180517468Seric 	/* remove the transcript */
180617468Seric # ifdef LOG
180758020Seric 	if (LogLevel > 87)
180817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
180956795Seric # endif /* LOG */
181058680Seric 	if (!tTd(51, 104))
181117468Seric 		xunlink(queuename(e, 'x'));
181217468Seric 
181317468Seric }
181440973Sbostic /*
181554974Seric **  SETCTLUSER -- create a controlling address
181640973Sbostic **
181754974Seric **	Create a fake "address" given only a local login name; this is
181854974Seric **	used as a "controlling user" for future recipient addresses.
181940973Sbostic **
182040973Sbostic **	Parameters:
182154974Seric **		user -- the user name of the controlling user.
182240973Sbostic **
182340973Sbostic **	Returns:
182454974Seric **		An address descriptor for the controlling user.
182540973Sbostic **
182640973Sbostic **	Side Effects:
182740973Sbostic **		none.
182840973Sbostic */
182940973Sbostic 
183054974Seric ADDRESS *
183154974Seric setctluser(user)
183254974Seric 	char *user;
183340973Sbostic {
183454974Seric 	register ADDRESS *a;
183540973Sbostic 	struct passwd *pw;
183659113Seric 	char *p;
183740973Sbostic 
183840973Sbostic 	/*
183954974Seric 	**  See if this clears our concept of controlling user.
184040973Sbostic 	*/
184140973Sbostic 
184263850Seric 	if (user == NULL || *user == '\0')
184363850Seric 		return NULL;
184440973Sbostic 
184540973Sbostic 	/*
184654974Seric 	**  Set up addr fields for controlling user.
184740973Sbostic 	*/
184840973Sbostic 
184954974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
185054974Seric 	bzero((char *) a, sizeof *a);
185159113Seric 
185259113Seric 	p = strchr(user, ':');
185359113Seric 	if (p != NULL)
185459113Seric 		*p++ = '\0';
185559270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
185640973Sbostic 	{
185765822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
185865822Seric 			a->q_home = "";
185965822Seric 		else
186065822Seric 			a->q_home = newstr(pw->pw_dir);
186140973Sbostic 		a->q_uid = pw->pw_uid;
186240973Sbostic 		a->q_gid = pw->pw_gid;
186359270Seric 		a->q_flags |= QGOODUID;
186440973Sbostic 	}
1865*68603Seric 
1866*68603Seric 	if (*user != '\0')
1867*68603Seric 		a->q_user = newstr(user);
1868*68603Seric 	else if (p != NULL)
1869*68603Seric 		a->q_user = newstr(p);
187040973Sbostic 	else
187157642Seric 		a->q_user = newstr(DefUser);
187240973Sbostic 
187359270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
187456328Seric 	a->q_mailer = LocalMailer;
187559113Seric 	if (p == NULL)
187659113Seric 		a->q_paddr = a->q_user;
187759113Seric 	else
187859113Seric 		a->q_paddr = newstr(p);
187954974Seric 	return a;
188040973Sbostic }
188168490Seric /*
188268490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
188368490Seric **
188468490Seric **	Parameters:
188568490Seric **		e -- the envelope (e->e_id will be used).
188668490Seric **		why -- reported to whomever can hear.
188768490Seric **
188868490Seric **	Returns:
188968490Seric **		none.
189068490Seric */
189168490Seric 
189268490Seric void
189368490Seric loseqfile(e, why)
189468490Seric 	register ENVELOPE *e;
189568490Seric 	char *why;
189668490Seric {
189768563Seric 	char *p;
189868490Seric 	char buf[40];
189968490Seric 
190068490Seric 	if (e == NULL || e->e_id == NULL)
190168490Seric 		return;
190268490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
190368490Seric 		return;
190468490Seric 	strcpy(buf, queuename(e, 'q'));
190568563Seric 	p = queuename(e, 'Q');
190668563Seric 	if (rename(buf, p) < 0)
190768563Seric 		syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid());
190868490Seric #ifdef LOG
190968563Seric 	else if (LogLevel > 0)
191068563Seric 		syslog(LOG_ALERT, "Losing %s: %s", buf, why);
191168490Seric #endif
191268490Seric }
1913