xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68481)
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*68481Seric static char sccsid[] = "@(#)queue.c	8.65 (Berkeley) 03/05/95 (with queueing)";
1433731Sbostic #else
15*68481Seric static char sccsid[] = "@(#)queue.c	8.65 (Berkeley) 03/05/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 */
32*68481Seric 	char		*w_host;	/* name of recipient host */
33*68481Seric 	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 */
42*68481Seric 
43*68481Seric #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))
136*68481Seric 		printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id,
137*68481Seric 			newid ? " (new id)" : "", queueall);
138*68481Seric 	if (tTd(40, 32))
139*68481Seric 	{
140*68481Seric 		printf("  sendq=");
141*68481Seric 		printaddr(e->e_sendqueue, TRUE);
142*68481Seric 	}
14364745Seric 	if (tTd(40, 9))
14464745Seric 	{
14564745Seric 		printf("  tfp=");
14664745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
14764745Seric 		printf("  lockfp=");
14864745Seric 		if (e->e_lockfp == NULL)
14964745Seric 			printf("NULL\n");
15064745Seric 		else
15164745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
15264745Seric 	}
1534632Seric 
1544632Seric 	/*
1556980Seric 	**  If there is no data file yet, create one.
1566980Seric 	*/
1576980Seric 
1586980Seric 	if (e->e_df == NULL)
1596980Seric 	{
1606980Seric 		register FILE *dfp;
161*68481Seric 		struct stat stbuf;
1629389Seric 		extern putbody();
1636980Seric 
16464086Seric 		e->e_df = queuename(e, 'd');
16564086Seric 		e->e_df = newstr(e->e_df);
16666891Seric 		fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
16764724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
16865090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
16965090Seric 				e->e_df, geteuid());
170*68481Seric 		if (fstat(fd, &stbuf) < 0)
171*68481Seric 			e->e_dfino = -1;
172*68481Seric 		else
173*68481Seric 		{
174*68481Seric 			e->e_dfdev = stbuf.st_dev;
175*68481Seric 			e->e_dfino = stbuf.st_ino;
176*68481Seric 		}
17765870Seric 		bzero(&mcibuf, sizeof mcibuf);
17865870Seric 		mcibuf.mci_out = dfp;
17965870Seric 		mcibuf.mci_mailer = FileMailer;
18068228Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
18158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1829389Seric 		e->e_putbody = putbody;
1836980Seric 	}
1846980Seric 
1856980Seric 	/*
1864632Seric 	**  Output future work requests.
18725687Seric 	**	Priority and creation time should be first, since
18825687Seric 	**	they are required by orderq.
1894632Seric 	*/
1904632Seric 
191*68481Seric 	/* output queue version number (must be first!) */
192*68481Seric 	fprintf(tfp, "V%d\n", QF_VERSION);
193*68481Seric 
1949377Seric 	/* output message priority */
1959377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1969377Seric 
1979630Seric 	/* output creation time */
1989630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1999630Seric 
200*68481Seric 	/* output last delivery time */
201*68481Seric 	fprintf(tfp, "K%ld\n", e->e_dtime);
202*68481Seric 
203*68481Seric 	/* output number of delivery attempts */
204*68481Seric 	fprintf(tfp, "N%d\n", e->e_ntries);
205*68481Seric 
206*68481Seric 	/* output inode number of data file */
207*68481Seric 	/* XXX should probably include device major/minor too */
208*68481Seric 	if (e->e_dfino != -1)
209*68481Seric 		fprintf(tfp, "I%d/%d/%ld\n",
210*68481Seric 			major(e->e_dfdev), minor(e->e_dfdev), e->e_dfino);
211*68481Seric 
21259093Seric 	/* output type and name of data file */
21359093Seric 	if (e->e_bodytype != NULL)
21459093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
2157812Seric 	fprintf(tfp, "D%s\n", e->e_df);
2164632Seric 
21710108Seric 	/* message from envelope, if it exists */
21810108Seric 	if (e->e_message != NULL)
21968478Seric 		fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE));
22010108Seric 
22158737Seric 	/* send various flag bits through */
22258737Seric 	p = buf;
22358737Seric 	if (bitset(EF_WARNING, e->e_flags))
22458737Seric 		*p++ = 'w';
22558737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
22658737Seric 		*p++ = 'r';
227*68481Seric 	if (bitset(EF_HAS8BIT, e->e_flags))
228*68481Seric 		*p++ = '8';
22958737Seric 	*p++ = '\0';
23058737Seric 	if (buf[0] != '\0')
23158737Seric 		fprintf(tfp, "F%s\n", buf);
23258737Seric 
23358957Seric 	/* $r and $s and $_ macro values */
23453400Seric 	if ((p = macvalue('r', e)) != NULL)
23568478Seric 		fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE));
23653400Seric 	if ((p = macvalue('s', e)) != NULL)
23768478Seric 		fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE));
23858957Seric 	if ((p = macvalue('_', e)) != NULL)
23968478Seric 		fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE));
24053400Seric 
2414632Seric 	/* output name of sender */
242*68481Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
243*68481Seric 		p = e->e_sender;
244*68481Seric 	else
245*68481Seric 		p = e->e_from.q_paddr;
246*68481Seric 	fprintf(tfp, "S%s\n", denlstring(p, TRUE, FALSE));
2474632Seric 
248*68481Seric 	/* output ESMTP-supplied "original" information */
249*68481Seric 	if (e->e_envid != NULL)
250*68481Seric 		fprintf(tfp, "Z%s\n", denlstring(e->e_envid, TRUE, FALSE));
251*68481Seric 
25255360Seric 	/* output list of error recipients */
25359670Seric 	printctladdr(NULL, NULL);
25455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25555360Seric 	{
25658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
25755360Seric 		{
25859113Seric 			printctladdr(q, tfp);
25968478Seric 			fprintf(tfp, "E%s\n", denlstring(q->q_paddr, TRUE, FALSE));
26055360Seric 		}
26155360Seric 	}
26255360Seric 
2634632Seric 	/* output list of recipient addresses */
2646980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2654632Seric 	{
26658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
26758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2688245Seric 		{
26959113Seric 			printctladdr(q, tfp);
270*68481Seric 			if (q->q_orcpt != NULL)
271*68481Seric 				fprintf(tfp, "Q%s\n",
272*68481Seric 					denlstring(q->q_orcpt, TRUE, FALSE));
273*68481Seric 			putc('R', tfp);
274*68481Seric 			if (bitset(QPRIMARY, q->q_flags))
275*68481Seric 				putc('P', tfp);
276*68481Seric 			if (bitset(QPINGONSUCCESS, q->q_flags))
277*68481Seric 				putc('S', tfp);
278*68481Seric 			if (bitset(QPINGONFAILURE, q->q_flags))
279*68481Seric 				putc('F', tfp);
280*68481Seric 			if (bitset(QPINGONDELAY, q->q_flags))
281*68481Seric 				putc('D', tfp);
282*68481Seric 			if (bitset(QHAS_RET_PARAM, q->q_flags))
283*68481Seric 			{
284*68481Seric 				if (bitset(QRET_HDRS, q->q_flags))
285*68481Seric 					putc('N', tfp);
286*68481Seric 				else
287*68481Seric 					putc('B', tfp);
288*68481Seric 			}
289*68481Seric 			putc(':', tfp);
290*68481Seric 			fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE));
2919377Seric 			if (announce)
2929377Seric 			{
2939377Seric 				e->e_to = q->q_paddr;
29458151Seric 				message("queued");
29558020Seric 				if (LogLevel > 8)
296*68481Seric 					logdelivery(NULL, NULL, "queued",
297*68481Seric 						    NULL, (time_t) 0, e);
2989377Seric 				e->e_to = NULL;
2999377Seric 			}
3009387Seric 			if (tTd(40, 1))
3019387Seric 			{
3029387Seric 				printf("queueing ");
3039387Seric 				printaddr(q, FALSE);
3049387Seric 			}
3058245Seric 		}
3064632Seric 	}
3074632Seric 
3089377Seric 	/*
3099377Seric 	**  Output headers for this message.
3109377Seric 	**	Expand macros completely here.  Queue run will deal with
3119377Seric 	**	everything as absolute headers.
3129377Seric 	**		All headers that must be relative to the recipient
3139377Seric 	**		can be cracked later.
31410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
31510173Seric 	**	no effect on the addresses as they are output.
3169377Seric 	*/
3179377Seric 
31810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
31958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
32065584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
32110349Seric 	nullmailer.m_eol = "\n";
32265870Seric 	bzero(&mcibuf, sizeof mcibuf);
32365870Seric 	mcibuf.mci_mailer = &nullmailer;
32465870Seric 	mcibuf.mci_out = tfp;
32510173Seric 
32658050Seric 	define('g', "\201f", e);
3276980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3284632Seric 	{
32910686Seric 		extern bool bitzerop();
33010686Seric 
33112015Seric 		/* don't output null headers */
3324632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
3334632Seric 			continue;
33412015Seric 
33512015Seric 		/* don't output resent headers on non-resent messages */
33612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
33712015Seric 			continue;
33812015Seric 
33965267Seric 		/* expand macros; if null, don't output header at all */
34065267Seric 		if (bitset(H_DEFAULT, h->h_flags))
34165267Seric 		{
34265267Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
34365267Seric 			if (buf[0] == '\0')
34465267Seric 				continue;
34565267Seric 		}
34665267Seric 
34712015Seric 		/* output this header */
3487812Seric 		fprintf(tfp, "H");
34912015Seric 
35012015Seric 		/* if conditional, output the set of conditions */
35110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
35210686Seric 		{
35310686Seric 			int j;
35410686Seric 
35523098Seric 			(void) putc('?', tfp);
35610686Seric 			for (j = '\0'; j <= '\177'; j++)
35710686Seric 				if (bitnset(j, h->h_mflags))
35823098Seric 					(void) putc(j, tfp);
35923098Seric 			(void) putc('?', tfp);
36010686Seric 		}
36112015Seric 
36212015Seric 		/* output the header: expand macros, convert addresses */
3637763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3647763Seric 		{
36565267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3667763Seric 		}
3678245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3689348Seric 		{
36958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
37063753Seric 			FILE *savetrace = TrafficLogFile;
37158737Seric 
37263753Seric 			TrafficLogFile = NULL;
37363753Seric 
37458737Seric 			if (bitset(H_FROM, h->h_flags))
37558737Seric 				oldstyle = FALSE;
37658737Seric 
37765870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
37863753Seric 
37963753Seric 			TrafficLogFile = savetrace;
3809348Seric 		}
3817763Seric 		else
3828245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3834632Seric 	}
3844632Seric 
3854632Seric 	/*
3864632Seric 	**  Clean up.
3874632Seric 	*/
3884632Seric 
38964762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
39058732Seric 	{
39158732Seric 		if (newid)
39258732Seric 			syserr("!552 Error writing control file %s", tf);
39358732Seric 		else
39458732Seric 			syserr("!452 Error writing control file %s", tf);
39558732Seric 	}
39658732Seric 
39751920Seric 	if (!newid)
39851920Seric 	{
39964277Seric 		/* rename (locked) tf to be (locked) qf */
40051920Seric 		qf = queuename(e, 'q');
40151920Seric 		if (rename(tf, qf) < 0)
40265090Seric 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
40365090Seric 				tf, qf, e->e_df, geteuid());
40464277Seric 
40564277Seric 		/* close and unlock old (locked) qf */
40651920Seric 		if (e->e_lockfp != NULL)
40758680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
40851920Seric 		e->e_lockfp = tfp;
40951920Seric 	}
41051920Seric 	else
41151920Seric 		qf = tf;
41241636Srick 	errno = 0;
41364745Seric 	e->e_flags |= EF_INQUEUE;
4147391Seric 
4157677Seric # ifdef LOG
4167677Seric 	/* save log info */
41758020Seric 	if (LogLevel > 79)
4187878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
41956795Seric # endif /* LOG */
42064307Seric 
42164307Seric 	if (tTd(40, 1))
42264307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
42351920Seric 	return;
4244632Seric }
42554974Seric 
42654974Seric printctladdr(a, tfp)
42759113Seric 	register ADDRESS *a;
42854974Seric 	FILE *tfp;
42954974Seric {
43059113Seric 	char *uname;
43159113Seric 	register struct passwd *pw;
43259113Seric 	register ADDRESS *q;
43359113Seric 	uid_t uid;
43459113Seric 	static ADDRESS *lastctladdr;
43559113Seric 	static uid_t lastuid;
43654974Seric 
43759113Seric 	/* initialization */
43863850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
43954974Seric 	{
44059670Seric 		if (lastctladdr != NULL && tfp != NULL)
44159113Seric 			fprintf(tfp, "C\n");
44259113Seric 		lastctladdr = NULL;
44359113Seric 		lastuid = 0;
44454974Seric 		return;
44554974Seric 	}
44659113Seric 
44759113Seric 	/* find the active uid */
44859113Seric 	q = getctladdr(a);
44959113Seric 	if (q == NULL)
45059113Seric 		uid = 0;
45154974Seric 	else
45259113Seric 		uid = q->q_uid;
45363850Seric 	a = a->q_alias;
45459113Seric 
45559113Seric 	/* check to see if this is the same as last time */
45659113Seric 	if (lastctladdr != NULL && uid == lastuid &&
45759113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
45859113Seric 		return;
45959113Seric 	lastuid = uid;
46059113Seric 	lastctladdr = a;
46159113Seric 
46259113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
46359270Seric 		uname = "";
46459113Seric 	else
46559113Seric 		uname = pw->pw_name;
46659113Seric 
46768478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
46854974Seric }
4694632Seric /*
4704632Seric **  RUNQUEUE -- run the jobs in the queue.
4714632Seric **
4724632Seric **	Gets the stuff out of the queue in some presumably logical
4734632Seric **	order and processes them.
4744632Seric **
4754632Seric **	Parameters:
47624941Seric **		forkflag -- TRUE if the queue scanning should be done in
47724941Seric **			a child process.  We double-fork so it is not our
47824941Seric **			child and we don't have to clean up after it.
4794632Seric **
4804632Seric **	Returns:
4814632Seric **		none.
4824632Seric **
4834632Seric **	Side Effects:
4844632Seric **		runs things in the mail queue.
4854632Seric */
4864632Seric 
48755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
48855360Seric 
489*68481Seric void
49055360Seric runqueue(forkflag)
4914639Seric 	bool forkflag;
4924632Seric {
49355360Seric 	register ENVELOPE *e;
494*68481Seric 	int njobs;
495*68481Seric 	int sequenceno = 0;
49655360Seric 	extern ENVELOPE BlankEnvelope;
49724953Seric 
4987466Seric 	/*
49924953Seric 	**  If no work will ever be selected, don't even bother reading
50024953Seric 	**  the queue.
50124953Seric 	*/
50224953Seric 
50351920Seric 	CurrentLA = getla();	/* get load average */
50440934Srick 
50558132Seric 	if (shouldqueue(0L, curtime()))
50624953Seric 	{
50724953Seric 		if (Verbose)
50824953Seric 			printf("Skipping queue run -- load average too high\n");
50958107Seric 		if (forkflag && QueueIntvl != 0)
51058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
51155360Seric 		return;
51224953Seric 	}
51324953Seric 
51424953Seric 	/*
5157466Seric 	**  See if we want to go off and do other useful work.
5167466Seric 	*/
5174639Seric 
5184639Seric 	if (forkflag)
5194639Seric 	{
5207943Seric 		int pid;
52165223Seric #ifdef SIGCHLD
52265223Seric 		extern void reapchild();
5237943Seric 
52465223Seric 		(void) setsignal(SIGCHLD, reapchild);
52565223Seric #endif
52665223Seric 
5277943Seric 		pid = dofork();
5287943Seric 		if (pid != 0)
5294639Seric 		{
5307943Seric 			/* parent -- pick up intermediate zombie */
53125184Seric #ifndef SIGCHLD
5329377Seric 			(void) waitfor(pid);
53356795Seric #endif /* SIGCHLD */
5347690Seric 			if (QueueIntvl != 0)
5359348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5364639Seric 			return;
5374639Seric 		}
5387943Seric 		/* child -- double fork */
53925184Seric #ifndef SIGCHLD
5407943Seric 		if (fork() != 0)
5417943Seric 			exit(EX_OK);
54256795Seric #else /* SIGCHLD */
54364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
54456795Seric #endif /* SIGCHLD */
5454639Seric 	}
54624941Seric 
54740934Srick 	setproctitle("running queue: %s", QueueDir);
54824941Seric 
5497876Seric # ifdef LOG
55058020Seric 	if (LogLevel > 69)
55155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
55255360Seric 			QueueDir, getpid(), forkflag);
55356795Seric # endif /* LOG */
5544639Seric 
5557466Seric 	/*
55610205Seric 	**  Release any resources used by the daemon code.
55710205Seric 	*/
55810205Seric 
55910205Seric # ifdef DAEMON
56010205Seric 	clrdaemon();
56156795Seric # endif /* DAEMON */
56210205Seric 
56364658Seric 	/* force it to run expensive jobs */
56464658Seric 	NoConnect = FALSE;
56564658Seric 
56610205Seric 	/*
56755360Seric 	**  Create ourselves an envelope
56855360Seric 	*/
56955360Seric 
57055360Seric 	CurEnv = &QueueEnvelope;
57158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
57255360Seric 	e->e_flags = BlankEnvelope.e_flags;
57355360Seric 
57455360Seric 	/*
57527175Seric 	**  Make sure the alias database is open.
57627175Seric 	*/
57727175Seric 
57860537Seric 	initmaps(FALSE, e);
57927175Seric 
58027175Seric 	/*
5817466Seric 	**  Start making passes through the queue.
5827466Seric 	**	First, read and sort the entire queue.
5837466Seric 	**	Then, process the work in that order.
5847466Seric 	**		But if you take too long, start over.
5857466Seric 	*/
5867466Seric 
5877943Seric 	/* order the existing work requests */
588*68481Seric 	njobs = orderq(FALSE);
5897690Seric 
5907943Seric 	/* process them once at a time */
5917943Seric 	while (WorkQ != NULL)
5924639Seric 	{
5937943Seric 		WORK *w = WorkQ;
5947881Seric 
5957943Seric 		WorkQ = WorkQ->w_next;
59658884Seric 
59758884Seric 		/*
59858884Seric 		**  Ignore jobs that are too expensive for the moment.
59958884Seric 		*/
60058884Seric 
601*68481Seric 		sequenceno++;
60258884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
60358884Seric 		{
60458884Seric 			if (Verbose)
605*68481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
606*68481Seric 					w->w_name + 2, sequenceno, njobs);
60758884Seric 		}
60858930Seric 		else
60958930Seric 		{
61064296Seric 			pid_t pid;
61164296Seric 			extern pid_t dowork();
61264296Seric 
613*68481Seric 			if (Verbose)
614*68481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
615*68481Seric 					w->w_name + 2, sequenceno, njobs);
61664296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
61764296Seric 			errno = 0;
61866316Seric 			if (pid != 0)
61966316Seric 				(void) waitfor(pid);
62058930Seric 		}
6217943Seric 		free(w->w_name);
622*68481Seric 		if (w->w_host)
623*68481Seric 			free(w->w_host);
6247943Seric 		free((char *) w);
6254639Seric 	}
62629866Seric 
62729866Seric 	/* exit without the usual cleanup */
62855467Seric 	e->e_id = NULL;
62955467Seric 	finis();
6304634Seric }
6314634Seric /*
6324632Seric **  ORDERQ -- order the work queue.
6334632Seric **
6344632Seric **	Parameters:
63524941Seric **		doall -- if set, include everything in the queue (even
63624941Seric **			the jobs that cannot be run because the load
63724941Seric **			average is too high).  Otherwise, exclude those
63824941Seric **			jobs.
6394632Seric **
6404632Seric **	Returns:
64110121Seric **		The number of request in the queue (not necessarily
64210121Seric **		the number of requests in WorkQ however).
6434632Seric **
6444632Seric **	Side Effects:
6454632Seric **		Sets WorkQ to the queue of available work, in order.
6464632Seric */
6474632Seric 
64825687Seric # define NEED_P		001
64925687Seric # define NEED_T		002
65058318Seric # define NEED_R		004
65158318Seric # define NEED_S		010
6524632Seric 
65324941Seric orderq(doall)
65424941Seric 	bool doall;
6554632Seric {
65660219Seric 	register struct dirent *d;
6574632Seric 	register WORK *w;
6586625Sglickman 	DIR *f;
6594632Seric 	register int i;
66025687Seric 	WORK wlist[QUEUESIZE+1];
66110070Seric 	int wn = -1;
662*68481Seric 	int wc;
6634632Seric 
66458318Seric 	if (tTd(41, 1))
66558318Seric 	{
66658318Seric 		printf("orderq:\n");
66758318Seric 		if (QueueLimitId != NULL)
66858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
66958318Seric 		if (QueueLimitSender != NULL)
67058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
67158318Seric 		if (QueueLimitRecipient != NULL)
67258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
67358318Seric 	}
67458318Seric 
6754632Seric 	/* clear out old WorkQ */
6764632Seric 	for (w = WorkQ; w != NULL; )
6774632Seric 	{
6784632Seric 		register WORK *nw = w->w_next;
6794632Seric 
6804632Seric 		WorkQ = nw;
6814632Seric 		free(w->w_name);
682*68481Seric 		if (w->w_host)
683*68481Seric 			free(w->w_host);
6844632Seric 		free((char *) w);
6854632Seric 		w = nw;
6864632Seric 	}
6874632Seric 
6884632Seric 	/* open the queue directory */
6898148Seric 	f = opendir(".");
6904632Seric 	if (f == NULL)
6914632Seric 	{
6928148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
69310070Seric 		return (0);
6944632Seric 	}
6954632Seric 
6964632Seric 	/*
6974632Seric 	**  Read the work directory.
6984632Seric 	*/
6994632Seric 
70010070Seric 	while ((d = readdir(f)) != NULL)
7014632Seric 	{
7029377Seric 		FILE *cf;
70364492Seric 		register char *p;
7044632Seric 		char lbuf[MAXNAME];
70558318Seric 		extern bool strcontainedin();
7064632Seric 
7074632Seric 		/* is this an interesting entry? */
7087812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
7094632Seric 			continue;
7104632Seric 
71158318Seric 		if (QueueLimitId != NULL &&
71258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
71358318Seric 			continue;
71458318Seric 
71558722Seric 		/*
71658722Seric 		**  Check queue name for plausibility.  This handles
71758722Seric 		**  both old and new type ids.
71858722Seric 		*/
71958722Seric 
72064492Seric 		p = d->d_name + 2;
72164492Seric 		if (isupper(p[0]) && isupper(p[2]))
72264492Seric 			p += 3;
72364492Seric 		else if (isupper(p[1]))
72464492Seric 			p += 2;
72564492Seric 		else
72664492Seric 			p = d->d_name;
72764492Seric 		for (i = 0; isdigit(*p); p++)
72864492Seric 			i++;
72964492Seric 		if (i < 5 || *p != '\0')
73058020Seric 		{
73158020Seric 			if (Verbose)
73258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
73358020Seric #ifdef LOG
73458020Seric 			if (LogLevel > 3)
73559615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
73658020Seric 					d->d_name);
73758020Seric #endif
73858020Seric 			if (strlen(d->d_name) >= MAXNAME)
73958020Seric 				d->d_name[MAXNAME - 1] = '\0';
74058020Seric 			strcpy(lbuf, d->d_name);
74158020Seric 			lbuf[0] = 'Q';
74258020Seric 			(void) rename(d->d_name, lbuf);
74358020Seric 			continue;
74458020Seric 		}
74558020Seric 
74610070Seric 		/* yes -- open control file (if not too many files) */
74725687Seric 		if (++wn >= QUEUESIZE)
74810070Seric 			continue;
74958318Seric 
7508148Seric 		cf = fopen(d->d_name, "r");
7514632Seric 		if (cf == NULL)
7524632Seric 		{
7537055Seric 			/* this may be some random person sending hir msgs */
7547055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
75510090Seric 			if (tTd(41, 2))
75610090Seric 				printf("orderq: cannot open %s (%d)\n",
75710090Seric 					d->d_name, errno);
7587055Seric 			errno = 0;
75910090Seric 			wn--;
7604632Seric 			continue;
7614632Seric 		}
76225687Seric 		w = &wlist[wn];
76325687Seric 		w->w_name = newstr(d->d_name);
764*68481Seric 		w->w_host = NULL;
765*68481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7664632Seric 
76725027Seric 		/* make sure jobs in creation don't clog queue */
76825687Seric 		w->w_pri = 0x7fffffff;
76925687Seric 		w->w_ctime = 0;
77025027Seric 
7714632Seric 		/* extract useful information */
77225687Seric 		i = NEED_P | NEED_T;
77358318Seric 		if (QueueLimitSender != NULL)
77458318Seric 			i |= NEED_S;
775*68481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
77658318Seric 			i |= NEED_R;
77725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7784632Seric 		{
77924954Seric 			extern long atol();
78058318Seric 			extern bool strcontainedin();
78124954Seric 
78224941Seric 			switch (lbuf[0])
7834632Seric 			{
78424941Seric 			  case 'P':
78525687Seric 				w->w_pri = atol(&lbuf[1]);
78625687Seric 				i &= ~NEED_P;
7874632Seric 				break;
78825013Seric 
78925013Seric 			  case 'T':
79025687Seric 				w->w_ctime = atol(&lbuf[1]);
79125687Seric 				i &= ~NEED_T;
79225013Seric 				break;
79358318Seric 
79458318Seric 			  case 'R':
795*68481Seric 				if (w->w_host == NULL &&
796*68481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
797*68481Seric 					w->w_host = newstr(&p[1]);
798*68481Seric 				if (QueueLimitRecipient == NULL ||
79958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
80058318Seric 					i &= ~NEED_R;
80158318Seric 				break;
80258318Seric 
80358318Seric 			  case 'S':
80458318Seric 				if (QueueLimitSender != NULL &&
80558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
80658318Seric 					i &= ~NEED_S;
80758318Seric 				break;
8084632Seric 			}
8094632Seric 		}
8104632Seric 		(void) fclose(cf);
81124953Seric 
81258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
81358318Seric 		    bitset(NEED_R|NEED_S, i))
81424953Seric 		{
81524953Seric 			/* don't even bother sorting this job in */
816*68481Seric 			free(w->w_name);
817*68481Seric 			if (w->w_host)
818*68481Seric 				free(w->w_host);
81924953Seric 			wn--;
82024953Seric 		}
8214632Seric 	}
8226625Sglickman 	(void) closedir(f);
82310090Seric 	wn++;
8244632Seric 
825*68481Seric 	wc = min(wn, QUEUESIZE);
8264632Seric 
827*68481Seric 	if (QueueSortOrder == QS_BYHOST)
828*68481Seric 	{
829*68481Seric 		extern workcmpf1();
830*68481Seric 		extern workcmpf2();
83167612Seric 
832*68481Seric 		/*
833*68481Seric 		**  Sort the work directory for the first time,
834*68481Seric 		**  based on host name, lock status, and priority.
835*68481Seric 		*/
836*68481Seric 
837*68481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
838*68481Seric 
839*68481Seric 		/*
840*68481Seric 		**  If one message to host is locked, "lock" all messages
841*68481Seric 		**  to that host.
842*68481Seric 		*/
843*68481Seric 
844*68481Seric 		i = 0;
845*68481Seric 		while (i < wc)
846*68481Seric 		{
847*68481Seric 			if (!wlist[i].w_lock)
848*68481Seric 			{
849*68481Seric 				i++;
850*68481Seric 				continue;
851*68481Seric 			}
852*68481Seric 			w = &wlist[i];
853*68481Seric 			while (++i < wc)
854*68481Seric 			{
855*68481Seric 				if (wlist[i].w_host == NULL &&
856*68481Seric 				    w->w_host == NULL)
857*68481Seric 					wlist[i].w_lock = TRUE;
858*68481Seric 				else if (wlist[i].w_host != NULL &&
859*68481Seric 					 w->w_host != NULL &&
860*68481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
861*68481Seric 					wlist[i].w_lock = TRUE;
862*68481Seric 				else
863*68481Seric 					break;
864*68481Seric 			}
865*68481Seric 		}
866*68481Seric 
867*68481Seric 		/*
868*68481Seric 		**  Sort the work directory for the second time,
869*68481Seric 		**  based on lock status, host name, and priority.
870*68481Seric 		*/
871*68481Seric 
872*68481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
873*68481Seric 	}
874*68481Seric 	else
875*68481Seric 	{
876*68481Seric 		extern workcmpf0();
877*68481Seric 
878*68481Seric 		/*
879*68481Seric 		**  Simple sort based on queue priority only.
880*68481Seric 		*/
881*68481Seric 
882*68481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
883*68481Seric 	}
884*68481Seric 
8854632Seric 	/*
8864632Seric 	**  Convert the work list into canonical form.
8879377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8884632Seric 	*/
8894632Seric 
89024981Seric 	WorkQ = NULL;
891*68481Seric 	for (i = wc; --i >= 0; )
8924632Seric 	{
8934632Seric 		w = (WORK *) xalloc(sizeof *w);
8944632Seric 		w->w_name = wlist[i].w_name;
895*68481Seric 		w->w_host = wlist[i].w_host;
896*68481Seric 		w->w_lock = wlist[i].w_lock;
8974632Seric 		w->w_pri = wlist[i].w_pri;
89825013Seric 		w->w_ctime = wlist[i].w_ctime;
89924981Seric 		w->w_next = WorkQ;
90024981Seric 		WorkQ = w;
9014632Seric 	}
9024632Seric 
9037677Seric 	if (tTd(40, 1))
9044632Seric 	{
9054632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
9065037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
9074632Seric 	}
90810070Seric 
90910090Seric 	return (wn);
9104632Seric }
9114632Seric /*
912*68481Seric **  WORKCMPF0 -- simple priority-only compare function.
9134632Seric **
9144632Seric **	Parameters:
9154632Seric **		a -- the first argument.
9164632Seric **		b -- the second argument.
9174632Seric **
9184632Seric **	Returns:
91924981Seric **		-1 if a < b
92024981Seric **		 0 if a == b
92124981Seric **		+1 if a > b
9224632Seric **
9234632Seric **	Side Effects:
9244632Seric **		none.
9254632Seric */
9264632Seric 
927*68481Seric workcmpf0(a, b)
9285037Seric 	register WORK *a;
9295037Seric 	register WORK *b;
9304632Seric {
93157438Seric 	long pa = a->w_pri;
93257438Seric 	long pb = b->w_pri;
93324941Seric 
93424941Seric 	if (pa == pb)
935*68481Seric 		return 0;
93624941Seric 	else if (pa > pb)
937*68481Seric 		return 1;
93824981Seric 	else
939*68481Seric 		return -1;
9404632Seric }
9414632Seric /*
942*68481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
943*68481Seric **
944*68481Seric **	Sorts on host name, lock status, and priority in that order.
945*68481Seric **
946*68481Seric **	Parameters:
947*68481Seric **		a -- the first argument.
948*68481Seric **		b -- the second argument.
949*68481Seric **
950*68481Seric **	Returns:
951*68481Seric **		<0 if a < b
952*68481Seric **		 0 if a == b
953*68481Seric **		>0 if a > b
954*68481Seric **
955*68481Seric **	Side Effects:
956*68481Seric **		none.
957*68481Seric */
958*68481Seric 
959*68481Seric workcmpf1(a, b)
960*68481Seric 	register WORK *a;
961*68481Seric 	register WORK *b;
962*68481Seric {
963*68481Seric 	int i;
964*68481Seric 
965*68481Seric 	/* host name */
966*68481Seric 	if (a->w_host != NULL && b->w_host == NULL)
967*68481Seric 		return 1;
968*68481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
969*68481Seric 		return -1;
970*68481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
971*68481Seric 	    (i = strcmp(a->w_host, b->w_host)))
972*68481Seric 		return i;
973*68481Seric 
974*68481Seric 	/* lock status */
975*68481Seric 	if (a->w_lock != b->w_lock)
976*68481Seric 		return b->w_lock - a->w_lock;
977*68481Seric 
978*68481Seric 	/* job priority */
979*68481Seric 	return a->w_pri - b->w_pri;
980*68481Seric }
981*68481Seric /*
982*68481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
983*68481Seric **
984*68481Seric **	Sorts on lock status, host name, and priority in that order.
985*68481Seric **
986*68481Seric **	Parameters:
987*68481Seric **		a -- the first argument.
988*68481Seric **		b -- the second argument.
989*68481Seric **
990*68481Seric **	Returns:
991*68481Seric **		<0 if a < b
992*68481Seric **		 0 if a == b
993*68481Seric **		>0 if a > b
994*68481Seric **
995*68481Seric **	Side Effects:
996*68481Seric **		none.
997*68481Seric */
998*68481Seric 
999*68481Seric workcmpf2(a, b)
1000*68481Seric 	register WORK *a;
1001*68481Seric 	register WORK *b;
1002*68481Seric {
1003*68481Seric 	int i;
1004*68481Seric 
1005*68481Seric 	/* lock status */
1006*68481Seric 	if (a->w_lock != b->w_lock)
1007*68481Seric 		return a->w_lock - b->w_lock;
1008*68481Seric 
1009*68481Seric 	/* host name */
1010*68481Seric 	if (a->w_host != NULL && b->w_host == NULL)
1011*68481Seric 		return 1;
1012*68481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
1013*68481Seric 		return -1;
1014*68481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
1015*68481Seric 	    (i = strcmp(a->w_host, b->w_host)))
1016*68481Seric 		return i;
1017*68481Seric 
1018*68481Seric 	/* job priority */
1019*68481Seric 	return a->w_pri - b->w_pri;
1020*68481Seric }
1021*68481Seric /*
10224632Seric **  DOWORK -- do a work request.
10234632Seric **
10244632Seric **	Parameters:
102558884Seric **		id -- the ID of the job to run.
102658884Seric **		forkflag -- if set, run this in background.
102758924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
102858924Seric **			This is used when expanding aliases in the queue.
102961094Seric **			If forkflag is also set, it doesn't wait for the
103061094Seric **			child.
103158884Seric **		e - the envelope in which to run it.
10324632Seric **
10334632Seric **	Returns:
103464296Seric **		process id of process that is running the queue job.
10354632Seric **
10364632Seric **	Side Effects:
10374632Seric **		The work request is satisfied if possible.
10384632Seric */
10394632Seric 
104064296Seric pid_t
104158924Seric dowork(id, forkflag, requeueflag, e)
104258884Seric 	char *id;
104358884Seric 	bool forkflag;
104458924Seric 	bool requeueflag;
104555012Seric 	register ENVELOPE *e;
10464632Seric {
104764296Seric 	register pid_t pid;
104851920Seric 	extern bool readqf();
10494632Seric 
10507677Seric 	if (tTd(40, 1))
105158884Seric 		printf("dowork(%s)\n", id);
10524632Seric 
10534632Seric 	/*
105424941Seric 	**  Fork for work.
105524941Seric 	*/
105624941Seric 
105758884Seric 	if (forkflag)
105824941Seric 	{
105964296Seric 		pid = fork();
106064296Seric 		if (pid < 0)
106124941Seric 		{
106224941Seric 			syserr("dowork: cannot fork");
106364296Seric 			return 0;
106424941Seric 		}
106564839Seric 		else if (pid > 0)
106664839Seric 		{
106764839Seric 			/* parent -- clean out connection cache */
106864839Seric 			mci_flush(FALSE, NULL);
106964839Seric 		}
107024941Seric 	}
107124941Seric 	else
107224941Seric 	{
107364296Seric 		pid = 0;
107424941Seric 	}
107524941Seric 
107664296Seric 	if (pid == 0)
10774632Seric 	{
10784632Seric 		/*
10794632Seric 		**  CHILD
10808148Seric 		**	Lock the control file to avoid duplicate deliveries.
10818148Seric 		**		Then run the file as though we had just read it.
10827350Seric 		**	We save an idea of the temporary name so we
10837350Seric 		**		can recover on interrupt.
10844632Seric 		*/
10854632Seric 
10867763Seric 		/* set basic modes, etc. */
10877356Seric 		(void) alarm(0);
108855012Seric 		clearenvelope(e, FALSE);
108964554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
109058734Seric 		e->e_errormode = EM_MAIL;
109158884Seric 		e->e_id = id;
109264765Seric 		GrabTo = UseErrorsTo = FALSE;
109366316Seric 		ExitStat = EX_OK;
109463846Seric 		if (forkflag)
109564554Seric 		{
109664296Seric 			disconnect(1, e);
109764554Seric 			OpMode = MD_DELIVER;
109864554Seric 		}
10997876Seric # ifdef LOG
110058020Seric 		if (LogLevel > 76)
110155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
11027881Seric 			       getpid());
110356795Seric # endif /* LOG */
11047763Seric 
11057763Seric 		/* don't use the headers from sendmail.cf... */
110655012Seric 		e->e_header = NULL;
11077763Seric 
110851920Seric 		/* read the queue control file -- return if locked */
110965200Seric 		if (!readqf(e))
11106980Seric 		{
111158884Seric 			if (tTd(40, 4))
111258884Seric 				printf("readqf(%s) failed\n", e->e_id);
111358884Seric 			if (forkflag)
111424941Seric 				exit(EX_OK);
111524941Seric 			else
111666316Seric 				return 0;
11176980Seric 		}
11186980Seric 
111955012Seric 		e->e_flags |= EF_INQUEUE;
11206980Seric 
1121*68481Seric 		/* if this has been tried recently, let it be */
1122*68481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
1123*68481Seric 		{
1124*68481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
112558924Seric 
1126*68481Seric 			e->e_flags |= EF_KEEPQUEUE;
1127*68481Seric 			if (Verbose || tTd(40, 8))
1128*68481Seric 				printf("%s: too young (%s)\n",
1129*68481Seric 					e->e_id, howlong);
1130*68481Seric #ifdef LOG
1131*68481Seric 			if (LogLevel > 19)
1132*68481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
1133*68481Seric 					e->e_id, howlong);
1134*68481Seric #endif
1135*68481Seric 		}
1136*68481Seric 		else
1137*68481Seric 		{
1138*68481Seric 			eatheader(e, requeueflag);
11396980Seric 
1140*68481Seric 			if (requeueflag)
1141*68481Seric 				queueup(e, TRUE, FALSE);
1142*68481Seric 
1143*68481Seric 			/* do the delivery */
1144*68481Seric 			sendall(e, SM_DELIVER);
1145*68481Seric 		}
1146*68481Seric 
11476980Seric 		/* finish up and exit */
114858884Seric 		if (forkflag)
114924941Seric 			finis();
115024941Seric 		else
115155012Seric 			dropenvelope(e);
11524632Seric 	}
115364296Seric 	e->e_id = NULL;
115464296Seric 	return pid;
11554632Seric }
11564632Seric /*
11574632Seric **  READQF -- read queue file and set up environment.
11584632Seric **
11594632Seric **	Parameters:
11609377Seric **		e -- the envelope of the job to run.
11614632Seric **
11624632Seric **	Returns:
116351920Seric **		TRUE if it successfully read the queue file.
116451920Seric **		FALSE otherwise.
11654632Seric **
11664632Seric **	Side Effects:
116751920Seric **		The queue file is returned locked.
11684632Seric */
11694632Seric 
117051920Seric bool
117165200Seric readqf(e)
11729377Seric 	register ENVELOPE *e;
11734632Seric {
117417477Seric 	register FILE *qfp;
117554974Seric 	ADDRESS *ctladdr;
117656400Seric 	struct stat st;
117757135Seric 	char *bp;
1178*68481Seric 	int qfver = 0;
1179*68481Seric 	register char *p;
1180*68481Seric 	char *orcpt = NULL;
118158915Seric 	char qf[20];
118257135Seric 	char buf[MAXLINE];
118324954Seric 	extern long atol();
118454974Seric 	extern ADDRESS *setctluser();
11854632Seric 
11864632Seric 	/*
118717468Seric 	**  Read and process the file.
11884632Seric 	*/
11894632Seric 
119058915Seric 	strcpy(qf, queuename(e, 'q'));
119151937Seric 	qfp = fopen(qf, "r+");
119217477Seric 	if (qfp == NULL)
119317477Seric 	{
119458884Seric 		if (tTd(40, 8))
119558884Seric 			printf("readqf(%s): fopen failure (%s)\n",
119658884Seric 				qf, errstring(errno));
119740934Srick 		if (errno != ENOENT)
119840934Srick 			syserr("readqf: no control file %s", qf);
119951920Seric 		return FALSE;
120017477Seric 	}
120140934Srick 
120264335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
120364277Seric 	{
120464277Seric 		/* being processed by another queuer */
1205*68481Seric 		if (Verbose || tTd(40, 8))
120664277Seric 			printf("%s: locked\n", e->e_id);
120764277Seric # ifdef LOG
120864277Seric 		if (LogLevel > 19)
120964277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
121064277Seric # endif /* LOG */
121164277Seric 		(void) fclose(qfp);
121264277Seric 		return FALSE;
121364277Seric 	}
121464277Seric 
121556400Seric 	/*
121656400Seric 	**  Check the queue file for plausibility to avoid attacks.
121756400Seric 	*/
121856400Seric 
121956400Seric 	if (fstat(fileno(qfp), &st) < 0)
122056400Seric 	{
122156400Seric 		/* must have been being processed by someone else */
122258884Seric 		if (tTd(40, 8))
122358884Seric 			printf("readqf(%s): fstat failure (%s)\n",
122458884Seric 				qf, errstring(errno));
122556400Seric 		fclose(qfp);
122656400Seric 		return FALSE;
122756400Seric 	}
122856400Seric 
122964137Seric 	if (st.st_uid != geteuid())
123056400Seric 	{
123156400Seric # ifdef LOG
123256400Seric 		if (LogLevel > 0)
123356400Seric 		{
123456400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
123556400Seric 				e->e_id, st.st_uid, st.st_mode);
123656400Seric 		}
123756795Seric # endif /* LOG */
123858884Seric 		if (tTd(40, 8))
123958884Seric 			printf("readqf(%s): bogus file\n", qf);
124064277Seric 		rename(qf, queuename(e, 'Q'));
124156400Seric 		fclose(qfp);
124256400Seric 		return FALSE;
124356400Seric 	}
124456400Seric 
124564277Seric 	if (st.st_size == 0)
124640934Srick 	{
124764277Seric 		/* must be a bogus file -- just remove it */
124864277Seric 		(void) unlink(qf);
124964277Seric 		fclose(qfp);
125051920Seric 		return FALSE;
125140934Srick 	}
125240934Srick 
125364277Seric 	if (st.st_nlink == 0)
125459101Seric 	{
125564277Seric 		/*
125664277Seric 		**  Race condition -- we got a file just as it was being
125764277Seric 		**  unlinked.  Just assume it is zero length.
125864277Seric 		*/
125964277Seric 
126059101Seric 		fclose(qfp);
126159101Seric 		return FALSE;
126259101Seric 	}
126359101Seric 
126464277Seric 	/* good file -- save this lock */
126551920Seric 	e->e_lockfp = qfp;
126651920Seric 
126740934Srick 	/* do basic system initialization */
126855012Seric 	initsys(e);
126965164Seric 	define('i', e->e_id, e);
127040934Srick 
12719377Seric 	LineNumber = 0;
127263850Seric 	e->e_flags |= EF_GLOBALERRS;
127363850Seric 	OpMode = MD_DELIVER;
127454974Seric 	ctladdr = NULL;
1275*68481Seric 	e->e_dfino = -1;
127657135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12774632Seric 	{
127858737Seric 		register char *p;
127957529Seric 		struct stat st;
1280*68481Seric 		u_long qflags;
1281*68481Seric 		ADDRESS *q;
128257529Seric 
128326504Seric 		if (tTd(40, 4))
128457135Seric 			printf("+++++ %s\n", bp);
128557135Seric 		switch (bp[0])
12864632Seric 		{
1287*68481Seric 		  case 'V':		/* queue file version number */
1288*68481Seric 			qfver = atoi(&bp[1]);
1289*68481Seric 			if (qfver > QF_VERSION)
1290*68481Seric 			{
1291*68481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
1292*68481Seric 					qfver, QF_VERSION);
1293*68481Seric 			}
1294*68481Seric 			break;
1295*68481Seric 
129640973Sbostic 		  case 'C':		/* specify controlling user */
129757135Seric 			ctladdr = setctluser(&bp[1]);
129840973Sbostic 			break;
129940973Sbostic 
1300*68481Seric 		  case 'Q':		/* original recipient */
1301*68481Seric 			orcpt = newstr(&bp[1]);
1302*68481Seric 			break;
1303*68481Seric 
13044632Seric 		  case 'R':		/* specify recipient */
1305*68481Seric 			p = bp;
1306*68481Seric 			qflags = 0;
1307*68481Seric 			if (qfver >= 1)
1308*68481Seric 			{
1309*68481Seric 				/* get flag bits */
1310*68481Seric 				while (*++p != '\0' && *p != ':')
1311*68481Seric 				{
1312*68481Seric 					switch (*p)
1313*68481Seric 					{
1314*68481Seric 					  case 'S':
1315*68481Seric 						qflags |= QPINGONSUCCESS;
1316*68481Seric 						break;
1317*68481Seric 
1318*68481Seric 					  case 'F':
1319*68481Seric 						qflags |= QPINGONFAILURE;
1320*68481Seric 						break;
1321*68481Seric 
1322*68481Seric 					  case 'D':
1323*68481Seric 						qflags |= QPINGONDELAY;
1324*68481Seric 						break;
1325*68481Seric 
1326*68481Seric 					  case 'B':
1327*68481Seric 						qflags |= QHAS_RET_PARAM;
1328*68481Seric 						break;
1329*68481Seric 
1330*68481Seric 					  case 'N':
1331*68481Seric 						qflags |= QHAS_RET_PARAM|QRET_HDRS;
1332*68481Seric 						break;
1333*68481Seric 
1334*68481Seric 					  case 'P':
1335*68481Seric 						qflags |= QPRIMARY;
1336*68481Seric 						break;
1337*68481Seric 					}
1338*68481Seric 				}
1339*68481Seric 			}
1340*68481Seric 			else
1341*68481Seric 				qflags |= QPRIMARY;
1342*68481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
1343*68481Seric 			if (q != NULL)
1344*68481Seric 			{
1345*68481Seric 				q->q_alias = ctladdr;
1346*68481Seric 				q->q_flags |= qflags;
1347*68481Seric 				q->q_orcpt = orcpt;
1348*68481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
1349*68481Seric 			}
1350*68481Seric 			orcpt = NULL;
13514632Seric 			break;
13524632Seric 
135325687Seric 		  case 'E':		/* specify error recipient */
1354*68481Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, 0, e);
135525687Seric 			break;
135625687Seric 
13574632Seric 		  case 'H':		/* header */
135857135Seric 			(void) chompheader(&bp[1], FALSE, e);
13594632Seric 			break;
13604632Seric 
136110108Seric 		  case 'M':		/* message */
136264705Seric 			/* ignore this; we want a new message next time */
136310108Seric 			break;
136410108Seric 
13654632Seric 		  case 'S':		/* sender */
136658704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13674632Seric 			break;
13684632Seric 
136959093Seric 		  case 'B':		/* body type */
137059093Seric 			e->e_bodytype = newstr(&bp[1]);
137159093Seric 			break;
137259093Seric 
13734632Seric 		  case 'D':		/* data file name */
137457135Seric 			e->e_df = newstr(&bp[1]);
13759544Seric 			e->e_dfp = fopen(e->e_df, "r");
13769544Seric 			if (e->e_dfp == NULL)
137758020Seric 			{
13789377Seric 				syserr("readqf: cannot open %s", e->e_df);
137958020Seric 				e->e_msgsize = -1;
138058020Seric 			}
138158020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
1382*68481Seric 			{
138357529Seric 				e->e_msgsize = st.st_size;
1384*68481Seric 				e->e_dfdev = st.st_dev;
1385*68481Seric 				e->e_dfino = st.st_ino;
1386*68481Seric 			}
13874632Seric 			break;
13884632Seric 
13897860Seric 		  case 'T':		/* init time */
139057135Seric 			e->e_ctime = atol(&bp[1]);
13914632Seric 			break;
13924632Seric 
1393*68481Seric 		  case 'I':		/* data file's inode number */
1394*68481Seric 			if (e->e_dfino == -1)
1395*68481Seric 				e->e_dfino = atol(&buf[1]);
1396*68481Seric 			break;
1397*68481Seric 
1398*68481Seric 		  case 'K':		/* time of last deliver attempt */
1399*68481Seric 			e->e_dtime = atol(&buf[1]);
1400*68481Seric 			break;
1401*68481Seric 
1402*68481Seric 		  case 'N':		/* number of delivery attempts */
1403*68481Seric 			e->e_ntries = atoi(&buf[1]);
1404*68481Seric 			break;
1405*68481Seric 
14064634Seric 		  case 'P':		/* message priority */
140757135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
14084634Seric 			break;
14094634Seric 
141058737Seric 		  case 'F':		/* flag bits */
141158737Seric 			for (p = &bp[1]; *p != '\0'; p++)
141258737Seric 			{
141358737Seric 				switch (*p)
141458737Seric 				{
141558737Seric 				  case 'w':	/* warning sent */
141658737Seric 					e->e_flags |= EF_WARNING;
141758737Seric 					break;
141858737Seric 
141958737Seric 				  case 'r':	/* response */
142058737Seric 					e->e_flags |= EF_RESPONSE;
142158737Seric 					break;
1422*68481Seric 
1423*68481Seric 				  case '8':	/* has 8 bit data */
1424*68481Seric 					e->e_flags |= EF_HAS8BIT;
1425*68481Seric 					break;
142658737Seric 				}
142758737Seric 			}
142858737Seric 			break;
142958737Seric 
1430*68481Seric 		  case 'Z':		/* original envelope id from ESMTP */
1431*68481Seric 			e->e_envid = newstr(&bp[1]);
1432*68481Seric 			break;
1433*68481Seric 
143453400Seric 		  case '$':		/* define macro */
143557135Seric 			define(bp[1], newstr(&bp[2]), e);
143653400Seric 			break;
143753400Seric 
143824941Seric 		  case '\0':		/* blank line; ignore */
143924941Seric 			break;
144024941Seric 
14414632Seric 		  default:
144265960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
144365200Seric 				qf, LineNumber, bp);
144463753Seric 			fclose(qfp);
144563753Seric 			rename(qf, queuename(e, 'Q'));
144663753Seric 			return FALSE;
14474632Seric 		}
144857135Seric 
144957135Seric 		if (bp != buf)
145057135Seric 			free(bp);
14514632Seric 	}
14529377Seric 
145324941Seric 	/*
145424941Seric 	**  If we haven't read any lines, this queue file is empty.
145524941Seric 	**  Arrange to remove it without referencing any null pointers.
145624941Seric 	*/
145724941Seric 
145824941Seric 	if (LineNumber == 0)
145924941Seric 	{
146024941Seric 		errno = 0;
146124941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
146224941Seric 	}
146351920Seric 	return TRUE;
14644632Seric }
14654632Seric /*
14669630Seric **  PRINTQUEUE -- print out a representation of the mail queue
14679630Seric **
14689630Seric **	Parameters:
14699630Seric **		none.
14709630Seric **
14719630Seric **	Returns:
14729630Seric **		none.
14739630Seric **
14749630Seric **	Side Effects:
14759630Seric **		Prints a listing of the mail queue on the standard output.
14769630Seric */
14775182Seric 
14789630Seric printqueue()
14799630Seric {
14809630Seric 	register WORK *w;
14819630Seric 	FILE *f;
148210070Seric 	int nrequests;
14839630Seric 	char buf[MAXLINE];
14849630Seric 
14859630Seric 	/*
148658250Seric 	**  Check for permission to print the queue
148758250Seric 	*/
148858250Seric 
148964333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
149058250Seric 	{
149158250Seric 		struct stat st;
149258523Seric # ifdef NGROUPS
149358523Seric 		int n;
149463937Seric 		GIDSET_T gidset[NGROUPS];
149558523Seric # endif
149658250Seric 
149758517Seric 		if (stat(QueueDir, &st) < 0)
149858250Seric 		{
149958250Seric 			syserr("Cannot stat %s", QueueDir);
150058250Seric 			return;
150158250Seric 		}
150258523Seric # ifdef NGROUPS
150358523Seric 		n = getgroups(NGROUPS, gidset);
150458523Seric 		while (--n >= 0)
150558523Seric 		{
150658523Seric 			if (gidset[n] == st.st_gid)
150758523Seric 				break;
150858523Seric 		}
150958523Seric 		if (n < 0)
151058523Seric # else
151163787Seric 		if (RealGid != st.st_gid)
151258523Seric # endif
151358250Seric 		{
151458250Seric 			usrerr("510 You are not permitted to see the queue");
151558250Seric 			setstat(EX_NOPERM);
151658250Seric 			return;
151758250Seric 		}
151858250Seric 	}
151958250Seric 
152058250Seric 	/*
15219630Seric 	**  Read and order the queue.
15229630Seric 	*/
15239630Seric 
152424941Seric 	nrequests = orderq(TRUE);
15259630Seric 
15269630Seric 	/*
15279630Seric 	**  Print the work list that we have read.
15289630Seric 	*/
15299630Seric 
15309630Seric 	/* first see if there is anything */
153110070Seric 	if (nrequests <= 0)
15329630Seric 	{
153310070Seric 		printf("Mail queue is empty\n");
15349630Seric 		return;
15359630Seric 	}
15369630Seric 
153751920Seric 	CurrentLA = getla();	/* get load average */
153840934Srick 
153910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
154025687Seric 	if (nrequests > QUEUESIZE)
154125687Seric 		printf(", only %d printed", QUEUESIZE);
154224979Seric 	if (Verbose)
154358716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
154424979Seric 	else
154558716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
15469630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
15479630Seric 	{
15489630Seric 		struct stat st;
154910070Seric 		auto time_t submittime = 0;
155010070Seric 		long dfsize = -1;
155158737Seric 		int flags = 0;
1552*68481Seric 		int qfver;
155310108Seric 		char message[MAXLINE];
155459093Seric 		char bodytype[MAXNAME];
15559630Seric 
155664355Seric 		printf("%8s", w->w_name + 2);
155717468Seric 		f = fopen(w->w_name, "r");
155817468Seric 		if (f == NULL)
155917468Seric 		{
156064355Seric 			printf(" (job completed)\n");
156117468Seric 			errno = 0;
156217468Seric 			continue;
156317468Seric 		}
1564*68481Seric 		if (w->w_lock)
156510070Seric 			printf("*");
156657438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
156724941Seric 			printf("X");
156810070Seric 		else
156910070Seric 			printf(" ");
157010070Seric 		errno = 0;
157117468Seric 
157259093Seric 		message[0] = bodytype[0] = '\0';
1573*68481Seric 		qfver = 0;
15749630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
15759630Seric 		{
157653400Seric 			register int i;
157758737Seric 			register char *p;
157853400Seric 
15799630Seric 			fixcrlf(buf, TRUE);
15809630Seric 			switch (buf[0])
15819630Seric 			{
1582*68481Seric 			  case 'V':	/* queue file version */
1583*68481Seric 				qfver = atoi(&buf[1]);
1584*68481Seric 				break;
1585*68481Seric 
158610108Seric 			  case 'M':	/* error message */
158753400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
158858737Seric 					i = sizeof message - 1;
158953400Seric 				bcopy(&buf[1], message, i);
159053400Seric 				message[i] = '\0';
159110108Seric 				break;
159210108Seric 
159359093Seric 			  case 'B':	/* body type */
159459093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
159559093Seric 					i = sizeof bodytype - 1;
159659093Seric 				bcopy(&buf[1], bodytype, i);
159759093Seric 				bodytype[i] = '\0';
159859093Seric 				break;
159959093Seric 
16009630Seric 			  case 'S':	/* sender name */
160124979Seric 				if (Verbose)
160258737Seric 					printf("%8ld %10ld%c%.12s %.38s",
160358737Seric 					    dfsize,
160458737Seric 					    w->w_pri,
160558737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
160658737Seric 					    ctime(&submittime) + 4,
160724979Seric 					    &buf[1]);
160824979Seric 				else
160924979Seric 					printf("%8ld %.16s %.45s", dfsize,
161024979Seric 					    ctime(&submittime), &buf[1]);
161159093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
161259093Seric 				{
161359093Seric 					printf("\n    %10.10s", bodytype);
161459093Seric 					if (message[0] != '\0')
161559093Seric 						printf("   (%.60s)", message);
161659093Seric 				}
16179630Seric 				break;
161851920Seric 
161940973Sbostic 			  case 'C':	/* controlling user */
162054974Seric 				if (Verbose)
162158716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
162258716Seric 						&buf[1]);
162340973Sbostic 				break;
16249630Seric 
16259630Seric 			  case 'R':	/* recipient name */
1626*68481Seric 				p = &buf[1];
1627*68481Seric 				if (qfver >= 1)
1628*68481Seric 				{
1629*68481Seric 					p = strchr(p, ':');
1630*68481Seric 					if (p == NULL)
1631*68481Seric 						break;
1632*68481Seric 					p++;
1633*68481Seric 				}
163424979Seric 				if (Verbose)
1635*68481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
163624979Seric 				else
1637*68481Seric 					printf("\n\t\t\t\t   %.45s", p);
16389630Seric 				break;
16399630Seric 
16409630Seric 			  case 'T':	/* creation time */
164124941Seric 				submittime = atol(&buf[1]);
16429630Seric 				break;
164310070Seric 
164410070Seric 			  case 'D':	/* data file name */
164510070Seric 				if (stat(&buf[1], &st) >= 0)
164610070Seric 					dfsize = st.st_size;
164710070Seric 				break;
164858737Seric 
164958737Seric 			  case 'F':	/* flag bits */
165058737Seric 				for (p = &buf[1]; *p != '\0'; p++)
165158737Seric 				{
165258737Seric 					switch (*p)
165358737Seric 					{
165458737Seric 					  case 'w':
165558737Seric 						flags |= EF_WARNING;
165658737Seric 						break;
165758737Seric 					}
165858737Seric 				}
16599630Seric 			}
16609630Seric 		}
166110070Seric 		if (submittime == (time_t) 0)
166210070Seric 			printf(" (no control file)");
16639630Seric 		printf("\n");
166423098Seric 		(void) fclose(f);
16659630Seric 	}
16669630Seric }
16679630Seric 
166856795Seric # endif /* QUEUE */
166917468Seric /*
167017468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
167117468Seric **
167217468Seric **	Assigns an id code if one does not already exist.
167317468Seric **	This code is very careful to avoid trashing existing files
167417468Seric **	under any circumstances.
167517468Seric **
167617468Seric **	Parameters:
167717468Seric **		e -- envelope to build it in/from.
167817468Seric **		type -- the file type, used as the first character
167917468Seric **			of the file name.
168017468Seric **
168117468Seric **	Returns:
168217468Seric **		a pointer to the new file name (in a static buffer).
168317468Seric **
168417468Seric **	Side Effects:
168551920Seric **		If no id code is already assigned, queuename will
168651920Seric **		assign an id code, create a qf file, and leave a
168751920Seric **		locked, open-for-write file pointer in the envelope.
168817468Seric */
168917468Seric 
169017468Seric char *
169117468Seric queuename(e, type)
169217468Seric 	register ENVELOPE *e;
169359700Seric 	int type;
169417468Seric {
169517468Seric 	static int pid = -1;
169658741Seric 	static char c0;
169758741Seric 	static char c1;
169858741Seric 	static char c2;
169958716Seric 	time_t now;
170058716Seric 	struct tm *tm;
170158689Seric 	static char buf[MAXNAME];
170217468Seric 
170317468Seric 	if (e->e_id == NULL)
170417468Seric 	{
170517468Seric 		char qf[20];
170617468Seric 
170717468Seric 		/* find a unique id */
170817468Seric 		if (pid != getpid())
170917468Seric 		{
171017468Seric 			/* new process -- start back at "AA" */
171117468Seric 			pid = getpid();
171258716Seric 			now = curtime();
171358716Seric 			tm = localtime(&now);
171458716Seric 			c0 = 'A' + tm->tm_hour;
171517468Seric 			c1 = 'A';
171617468Seric 			c2 = 'A' - 1;
171717468Seric 		}
171858716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
171917468Seric 
172017468Seric 		while (c1 < '~' || c2 < 'Z')
172117468Seric 		{
172217468Seric 			int i;
172317468Seric 
172417468Seric 			if (c2 >= 'Z')
172517468Seric 			{
172617468Seric 				c1++;
172717468Seric 				c2 = 'A' - 1;
172817468Seric 			}
172958716Seric 			qf[3] = c1;
173058716Seric 			qf[4] = ++c2;
173117468Seric 			if (tTd(7, 20))
173240934Srick 				printf("queuename: trying \"%s\"\n", qf);
173317468Seric 
173440934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
173551920Seric 			if (i < 0)
173651920Seric 			{
173751920Seric 				if (errno == EEXIST)
173851920Seric 					continue;
173964705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
174064705Seric 					qf, QueueDir, geteuid());
174151920Seric 				exit(EX_UNAVAILABLE);
174251920Seric 			}
174364335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
174451920Seric 			{
174551920Seric 				e->e_lockfp = fdopen(i, "w");
174640934Srick 				break;
174717468Seric 			}
174851920Seric 
174951920Seric 			/* a reader got the file; abandon it and try again */
175051920Seric 			(void) close(i);
175117468Seric 		}
175217468Seric 		if (c1 >= '~' && c2 >= 'Z')
175317468Seric 		{
175464705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
175564705Seric 				qf, QueueDir, geteuid());
175617468Seric 			exit(EX_OSERR);
175717468Seric 		}
175817468Seric 		e->e_id = newstr(&qf[2]);
175917468Seric 		define('i', e->e_id, e);
176017468Seric 		if (tTd(7, 1))
176117468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
176264745Seric 		if (tTd(7, 9))
176364745Seric 		{
176464745Seric 			printf("  lockfd=");
176564745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
176664745Seric 		}
176717468Seric # ifdef LOG
176858020Seric 		if (LogLevel > 93)
176917468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
177056795Seric # endif /* LOG */
177117468Seric 	}
177217468Seric 
177317468Seric 	if (type == '\0')
177417468Seric 		return (NULL);
177517468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
177617468Seric 	if (tTd(7, 2))
177717468Seric 		printf("queuename: %s\n", buf);
177817468Seric 	return (buf);
177917468Seric }
178017468Seric /*
178117468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
178217468Seric **
178317468Seric **	Parameters:
178417468Seric **		e -- the envelope to unlock.
178517468Seric **
178617468Seric **	Returns:
178717468Seric **		none
178817468Seric **
178917468Seric **	Side Effects:
179017468Seric **		unlocks the queue for `e'.
179117468Seric */
179217468Seric 
179317468Seric unlockqueue(e)
179417468Seric 	ENVELOPE *e;
179517468Seric {
179658680Seric 	if (tTd(51, 4))
179758680Seric 		printf("unlockqueue(%s)\n", e->e_id);
179858680Seric 
179951920Seric 	/* if there is a lock file in the envelope, close it */
180051920Seric 	if (e->e_lockfp != NULL)
180158680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
180251920Seric 	e->e_lockfp = NULL;
180351920Seric 
180458728Seric 	/* don't create a queue id if we don't already have one */
180558728Seric 	if (e->e_id == NULL)
180658728Seric 		return;
180758728Seric 
180817468Seric 	/* remove the transcript */
180917468Seric # ifdef LOG
181058020Seric 	if (LogLevel > 87)
181117468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
181256795Seric # endif /* LOG */
181358680Seric 	if (!tTd(51, 104))
181417468Seric 		xunlink(queuename(e, 'x'));
181517468Seric 
181617468Seric }
181740973Sbostic /*
181854974Seric **  SETCTLUSER -- create a controlling address
181940973Sbostic **
182054974Seric **	Create a fake "address" given only a local login name; this is
182154974Seric **	used as a "controlling user" for future recipient addresses.
182240973Sbostic **
182340973Sbostic **	Parameters:
182454974Seric **		user -- the user name of the controlling user.
182540973Sbostic **
182640973Sbostic **	Returns:
182754974Seric **		An address descriptor for the controlling user.
182840973Sbostic **
182940973Sbostic **	Side Effects:
183040973Sbostic **		none.
183140973Sbostic */
183240973Sbostic 
183354974Seric ADDRESS *
183454974Seric setctluser(user)
183554974Seric 	char *user;
183640973Sbostic {
183754974Seric 	register ADDRESS *a;
183840973Sbostic 	struct passwd *pw;
183959113Seric 	char *p;
184040973Sbostic 
184140973Sbostic 	/*
184254974Seric 	**  See if this clears our concept of controlling user.
184340973Sbostic 	*/
184440973Sbostic 
184563850Seric 	if (user == NULL || *user == '\0')
184663850Seric 		return NULL;
184740973Sbostic 
184840973Sbostic 	/*
184954974Seric 	**  Set up addr fields for controlling user.
185040973Sbostic 	*/
185140973Sbostic 
185254974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
185354974Seric 	bzero((char *) a, sizeof *a);
185459113Seric 
185559113Seric 	p = strchr(user, ':');
185659113Seric 	if (p != NULL)
185759113Seric 		*p++ = '\0';
185859270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
185940973Sbostic 	{
186065822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
186165822Seric 			a->q_home = "";
186265822Seric 		else
186365822Seric 			a->q_home = newstr(pw->pw_dir);
186440973Sbostic 		a->q_uid = pw->pw_uid;
186540973Sbostic 		a->q_gid = pw->pw_gid;
186657642Seric 		a->q_user = newstr(user);
186759270Seric 		a->q_flags |= QGOODUID;
186840973Sbostic 	}
186940973Sbostic 	else
187040973Sbostic 	{
187157642Seric 		a->q_user = newstr(DefUser);
187240973Sbostic 	}
187340973Sbostic 
187459270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
187556328Seric 	a->q_mailer = LocalMailer;
187659113Seric 	if (p == NULL)
187759113Seric 		a->q_paddr = a->q_user;
187859113Seric 	else
187959113Seric 		a->q_paddr = newstr(p);
188054974Seric 	return a;
188140973Sbostic }
1882