xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68854)
122708Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362528Sbostic  * Copyright (c) 1988, 1993
462528Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*68854Seric static char sccsid[] = "@(#)queue.c	8.80 (Berkeley) 04/22/95 (with queueing)";
1433731Sbostic #else
15*68854Seric static char sccsid[] = "@(#)queue.c	8.80 (Berkeley) 04/22/95 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2057736Seric # include <dirent.h>
214632Seric 
2233731Sbostic # ifdef QUEUE
234632Seric 
244632Seric /*
259377Seric **  Work queue.
269377Seric */
279377Seric 
289377Seric struct work
299377Seric {
309377Seric 	char		*w_name;	/* name of control file */
3168481Seric 	char		*w_host;	/* name of recipient host */
3268481Seric 	bool		w_lock;		/* is message locked? */
339377Seric 	long		w_pri;		/* priority of message, see below */
3425013Seric 	time_t		w_ctime;	/* creation time of message */
359377Seric 	struct work	*w_next;	/* next in queue */
369377Seric };
379377Seric 
389377Seric typedef struct work	WORK;
399377Seric 
409377Seric WORK	*WorkQ;			/* queue of things to be done */
4168481Seric 
4268481Seric #define QF_VERSION	1	/* version number of this queue format */
439377Seric /*
444632Seric **  QUEUEUP -- queue a message up for future transmission.
454632Seric **
464632Seric **	Parameters:
476980Seric **		e -- the envelope to queue up.
486999Seric **		queueall -- if TRUE, queue all addresses, rather than
496999Seric **			just those with the QQUEUEUP flag set.
509377Seric **		announce -- if TRUE, tell when you are queueing up.
514632Seric **
524632Seric **	Returns:
5351920Seric **		none.
544632Seric **
554632Seric **	Side Effects:
569377Seric **		The current request are saved in a control file.
5751920Seric **		The queue file is left locked.
584632Seric */
594632Seric 
609377Seric queueup(e, queueall, announce)
616980Seric 	register ENVELOPE *e;
626999Seric 	bool queueall;
639377Seric 	bool announce;
644632Seric {
657812Seric 	char *qf;
667812Seric 	register FILE *tfp;
674632Seric 	register HDR *h;
685007Seric 	register ADDRESS *q;
6951920Seric 	int fd;
7051920Seric 	int i;
7151920Seric 	bool newid;
7253400Seric 	register char *p;
7310173Seric 	MAILER nullmailer;
7465870Seric 	MCI mcibuf;
7551920Seric 	char buf[MAXLINE], tf[MAXLINE];
764632Seric 
775037Seric 	/*
7817477Seric 	**  Create control file.
795037Seric 	*/
804632Seric 
8164745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
8264277Seric 
8364277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8451920Seric 	strcpy(tf, queuename(e, 't'));
8551920Seric 	tfp = e->e_lockfp;
8651920Seric 	if (tfp == NULL)
8751920Seric 		newid = FALSE;
8864277Seric 
8964277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
9064745Seric 	if (!newid)
9151835Seric 	{
9251920Seric 		/* get a locked tf file */
9364070Seric 		for (i = 0; i < 128; i++)
9451835Seric 		{
9551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9651920Seric 			if (fd < 0)
9751835Seric 			{
9864070Seric 				if (errno != EEXIST)
9964070Seric 					break;
10064070Seric #ifdef LOG
10164070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10265090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
10365090Seric 						tf, geteuid(), errstring(errno));
10464070Seric #endif
10541636Srick 			}
10665090Seric 			else
10765090Seric 			{
10865090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10965090Seric 					break;
11064070Seric #ifdef LOG
11165090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
11265090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11365090Seric 						tf, errstring(errno));
11464070Seric #endif
11565090Seric 				close(fd);
11665090Seric 			}
11758689Seric 
11864070Seric 			if ((i % 32) == 31)
11964070Seric 			{
12064070Seric 				/* save the old temp file away */
12164070Seric 				(void) rename(tf, queuename(e, 'T'));
12264070Seric 			}
12364070Seric 			else
12464070Seric 				sleep(i % 32);
12541636Srick 		}
12664724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12764921Seric 		{
12864921Seric 			printopenfds(TRUE);
12965090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
13065090Seric 				tf, geteuid());
13164921Seric 		}
13251920Seric 	}
1334632Seric 
1347677Seric 	if (tTd(40, 1))
13568481Seric 		printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id,
13668481Seric 			newid ? " (new id)" : "", queueall);
13768603Seric 	if (tTd(40, 3))
13868603Seric 	{
13968603Seric 		extern void printenvflags();
14068603Seric 
14168603Seric 		printf("  e_flags=");
14268603Seric 		printenvflags(e);
14368603Seric 	}
14468481Seric 	if (tTd(40, 32))
14568481Seric 	{
14668481Seric 		printf("  sendq=");
14768481Seric 		printaddr(e->e_sendqueue, TRUE);
14868481Seric 	}
14964745Seric 	if (tTd(40, 9))
15064745Seric 	{
15164745Seric 		printf("  tfp=");
15264745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
15364745Seric 		printf("  lockfp=");
15464745Seric 		if (e->e_lockfp == NULL)
15564745Seric 			printf("NULL\n");
15664745Seric 		else
15764745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
15864745Seric 	}
1594632Seric 
1604632Seric 	/*
1616980Seric 	**  If there is no data file yet, create one.
1626980Seric 	*/
1636980Seric 
16468564Seric 	if (!bitset(EF_HAS_DF, e->e_flags))
1656980Seric 	{
1666980Seric 		register FILE *dfp;
16768564Seric 		char dfname[20];
16868481Seric 		struct stat stbuf;
1699389Seric 		extern putbody();
1706980Seric 
17168564Seric 		strcpy(dfname, queuename(e, 'd'));
17268564Seric 		fd = open(dfname, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
17364724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
17465090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
17568564Seric 				dfname, geteuid());
17668481Seric 		if (fstat(fd, &stbuf) < 0)
17768481Seric 			e->e_dfino = -1;
17868481Seric 		else
17968481Seric 		{
18068481Seric 			e->e_dfdev = stbuf.st_dev;
18168481Seric 			e->e_dfino = stbuf.st_ino;
18268481Seric 		}
18368564Seric 		e->e_flags |= EF_HAS_DF;
18465870Seric 		bzero(&mcibuf, sizeof mcibuf);
18565870Seric 		mcibuf.mci_out = dfp;
18665870Seric 		mcibuf.mci_mailer = FileMailer;
18768228Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
18858680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1899389Seric 		e->e_putbody = putbody;
1906980Seric 	}
1916980Seric 
1926980Seric 	/*
1934632Seric 	**  Output future work requests.
19425687Seric 	**	Priority and creation time should be first, since
19525687Seric 	**	they are required by orderq.
1964632Seric 	*/
1974632Seric 
19868481Seric 	/* output queue version number (must be first!) */
19968481Seric 	fprintf(tfp, "V%d\n", QF_VERSION);
20068481Seric 
2019377Seric 	/* output message priority */
2029377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
2039377Seric 
2049630Seric 	/* output creation time */
2059630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
2069630Seric 
20768481Seric 	/* output last delivery time */
20868481Seric 	fprintf(tfp, "K%ld\n", e->e_dtime);
20968481Seric 
21068481Seric 	/* output number of delivery attempts */
21168481Seric 	fprintf(tfp, "N%d\n", e->e_ntries);
21268481Seric 
21368481Seric 	/* output inode number of data file */
21468481Seric 	/* XXX should probably include device major/minor too */
21568481Seric 	if (e->e_dfino != -1)
21668481Seric 		fprintf(tfp, "I%d/%d/%ld\n",
21768481Seric 			major(e->e_dfdev), minor(e->e_dfdev), e->e_dfino);
21868481Seric 
21968564Seric 	/* output body type */
22059093Seric 	if (e->e_bodytype != NULL)
22159093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
2224632Seric 
22310108Seric 	/* message from envelope, if it exists */
22410108Seric 	if (e->e_message != NULL)
22568478Seric 		fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE));
22610108Seric 
22758737Seric 	/* send various flag bits through */
22858737Seric 	p = buf;
22958737Seric 	if (bitset(EF_WARNING, e->e_flags))
23058737Seric 		*p++ = 'w';
23158737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
23258737Seric 		*p++ = 'r';
23368481Seric 	if (bitset(EF_HAS8BIT, e->e_flags))
23468481Seric 		*p++ = '8';
23558737Seric 	*p++ = '\0';
23658737Seric 	if (buf[0] != '\0')
23758737Seric 		fprintf(tfp, "F%s\n", buf);
23858737Seric 
23958957Seric 	/* $r and $s and $_ macro values */
24053400Seric 	if ((p = macvalue('r', e)) != NULL)
24168478Seric 		fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE));
24253400Seric 	if ((p = macvalue('s', e)) != NULL)
24368478Seric 		fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE));
24458957Seric 	if ((p = macvalue('_', e)) != NULL)
24568478Seric 		fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE));
24653400Seric 
2474632Seric 	/* output name of sender */
24868481Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
24968481Seric 		p = e->e_sender;
25068481Seric 	else
25168481Seric 		p = e->e_from.q_paddr;
25268481Seric 	fprintf(tfp, "S%s\n", denlstring(p, TRUE, FALSE));
2534632Seric 
25468481Seric 	/* output ESMTP-supplied "original" information */
25568481Seric 	if (e->e_envid != NULL)
25668481Seric 		fprintf(tfp, "Z%s\n", denlstring(e->e_envid, TRUE, FALSE));
25768481Seric 
25868558Seric 	/* output list of recipient addresses */
25959670Seric 	printctladdr(NULL, NULL);
2606980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2614632Seric 	{
26258250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
26358680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2648245Seric 		{
26559113Seric 			printctladdr(q, tfp);
26668481Seric 			if (q->q_orcpt != NULL)
26768481Seric 				fprintf(tfp, "Q%s\n",
26868481Seric 					denlstring(q->q_orcpt, TRUE, FALSE));
26968481Seric 			putc('R', tfp);
27068481Seric 			if (bitset(QPRIMARY, q->q_flags))
27168481Seric 				putc('P', tfp);
27268481Seric 			if (bitset(QPINGONSUCCESS, q->q_flags))
27368481Seric 				putc('S', tfp);
27468481Seric 			if (bitset(QPINGONFAILURE, q->q_flags))
27568481Seric 				putc('F', tfp);
27668481Seric 			if (bitset(QPINGONDELAY, q->q_flags))
27768481Seric 				putc('D', tfp);
27868481Seric 			putc(':', tfp);
27968481Seric 			fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE));
2809377Seric 			if (announce)
2819377Seric 			{
2829377Seric 				e->e_to = q->q_paddr;
28358151Seric 				message("queued");
28458020Seric 				if (LogLevel > 8)
28568481Seric 					logdelivery(NULL, NULL, "queued",
28668481Seric 						    NULL, (time_t) 0, e);
2879377Seric 				e->e_to = NULL;
2889377Seric 			}
2899387Seric 			if (tTd(40, 1))
2909387Seric 			{
2919387Seric 				printf("queueing ");
2929387Seric 				printaddr(q, FALSE);
2939387Seric 			}
2948245Seric 		}
2954632Seric 	}
2964632Seric 
2979377Seric 	/*
2989377Seric 	**  Output headers for this message.
2999377Seric 	**	Expand macros completely here.  Queue run will deal with
3009377Seric 	**	everything as absolute headers.
3019377Seric 	**		All headers that must be relative to the recipient
3029377Seric 	**		can be cracked later.
30310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
30410173Seric 	**	no effect on the addresses as they are output.
3059377Seric 	*/
3069377Seric 
30710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
30858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
30965584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
31010349Seric 	nullmailer.m_eol = "\n";
31165870Seric 	bzero(&mcibuf, sizeof mcibuf);
31265870Seric 	mcibuf.mci_mailer = &nullmailer;
31365870Seric 	mcibuf.mci_out = tfp;
31410173Seric 
31558050Seric 	define('g', "\201f", e);
3166980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3174632Seric 	{
31810686Seric 		extern bool bitzerop();
31910686Seric 
32012015Seric 		/* don't output null headers */
3214632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
3224632Seric 			continue;
32312015Seric 
32412015Seric 		/* don't output resent headers on non-resent messages */
32512015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
32612015Seric 			continue;
32712015Seric 
32865267Seric 		/* expand macros; if null, don't output header at all */
32965267Seric 		if (bitset(H_DEFAULT, h->h_flags))
33065267Seric 		{
33168529Seric 			(void) expand(h->h_value, buf, sizeof buf, e);
33265267Seric 			if (buf[0] == '\0')
33365267Seric 				continue;
33465267Seric 		}
33565267Seric 
33612015Seric 		/* output this header */
3377812Seric 		fprintf(tfp, "H");
33812015Seric 
33912015Seric 		/* if conditional, output the set of conditions */
34010686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
34110686Seric 		{
34210686Seric 			int j;
34310686Seric 
34423098Seric 			(void) putc('?', tfp);
34510686Seric 			for (j = '\0'; j <= '\177'; j++)
34610686Seric 				if (bitnset(j, h->h_mflags))
34723098Seric 					(void) putc(j, tfp);
34823098Seric 			(void) putc('?', tfp);
34910686Seric 		}
35012015Seric 
35112015Seric 		/* output the header: expand macros, convert addresses */
3527763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3537763Seric 		{
35465267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3557763Seric 		}
3568245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3579348Seric 		{
35858737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
35963753Seric 			FILE *savetrace = TrafficLogFile;
36058737Seric 
36163753Seric 			TrafficLogFile = NULL;
36263753Seric 
36358737Seric 			if (bitset(H_FROM, h->h_flags))
36458737Seric 				oldstyle = FALSE;
36558737Seric 
36665870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
36763753Seric 
36863753Seric 			TrafficLogFile = savetrace;
3699348Seric 		}
3707763Seric 		else
3718245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3724632Seric 	}
3734632Seric 
3744632Seric 	/*
3754632Seric 	**  Clean up.
3764632Seric 	*/
3774632Seric 
37864762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
37958732Seric 	{
38058732Seric 		if (newid)
38158732Seric 			syserr("!552 Error writing control file %s", tf);
38258732Seric 		else
38358732Seric 			syserr("!452 Error writing control file %s", tf);
38458732Seric 	}
38558732Seric 
38651920Seric 	if (!newid)
38751920Seric 	{
38864277Seric 		/* rename (locked) tf to be (locked) qf */
38951920Seric 		qf = queuename(e, 'q');
39051920Seric 		if (rename(tf, qf) < 0)
39168564Seric 			syserr("cannot rename(%s, %s), uid=%d",
39268564Seric 				tf, qf, geteuid());
39364277Seric 
39464277Seric 		/* close and unlock old (locked) qf */
39551920Seric 		if (e->e_lockfp != NULL)
39658680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
39751920Seric 		e->e_lockfp = tfp;
39851920Seric 	}
39951920Seric 	else
40051920Seric 		qf = tf;
40141636Srick 	errno = 0;
40264745Seric 	e->e_flags |= EF_INQUEUE;
4037391Seric 
4047677Seric # ifdef LOG
4057677Seric 	/* save log info */
40658020Seric 	if (LogLevel > 79)
40768564Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s", e->e_id, qf);
40856795Seric # endif /* LOG */
40964307Seric 
41064307Seric 	if (tTd(40, 1))
41164307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
41251920Seric 	return;
4134632Seric }
41454974Seric 
41554974Seric printctladdr(a, tfp)
41659113Seric 	register ADDRESS *a;
41754974Seric 	FILE *tfp;
41854974Seric {
41959113Seric 	char *uname;
42059113Seric 	register struct passwd *pw;
42159113Seric 	register ADDRESS *q;
42259113Seric 	uid_t uid;
42359113Seric 	static ADDRESS *lastctladdr;
42459113Seric 	static uid_t lastuid;
42554974Seric 
42659113Seric 	/* initialization */
42763850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
42854974Seric 	{
42959670Seric 		if (lastctladdr != NULL && tfp != NULL)
43059113Seric 			fprintf(tfp, "C\n");
43159113Seric 		lastctladdr = NULL;
43259113Seric 		lastuid = 0;
43354974Seric 		return;
43454974Seric 	}
43559113Seric 
43659113Seric 	/* find the active uid */
43759113Seric 	q = getctladdr(a);
43859113Seric 	if (q == NULL)
43959113Seric 		uid = 0;
44054974Seric 	else
44159113Seric 		uid = q->q_uid;
44263850Seric 	a = a->q_alias;
44359113Seric 
44459113Seric 	/* check to see if this is the same as last time */
44559113Seric 	if (lastctladdr != NULL && uid == lastuid &&
44659113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
44759113Seric 		return;
44859113Seric 	lastuid = uid;
44959113Seric 	lastctladdr = a;
45059113Seric 
45168693Seric 	if (uid == 0 || (pw = sm_getpwuid(uid)) == NULL)
45259270Seric 		uname = "";
45359113Seric 	else
45459113Seric 		uname = pw->pw_name;
45559113Seric 
45668478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
45754974Seric }
4584632Seric /*
4594632Seric **  RUNQUEUE -- run the jobs in the queue.
4604632Seric **
4614632Seric **	Gets the stuff out of the queue in some presumably logical
4624632Seric **	order and processes them.
4634632Seric **
4644632Seric **	Parameters:
46524941Seric **		forkflag -- TRUE if the queue scanning should be done in
46624941Seric **			a child process.  We double-fork so it is not our
46724941Seric **			child and we don't have to clean up after it.
4684632Seric **
4694632Seric **	Returns:
4704632Seric **		none.
4714632Seric **
4724632Seric **	Side Effects:
4734632Seric **		runs things in the mail queue.
4744632Seric */
4754632Seric 
47655360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
47755360Seric 
47868481Seric void
47955360Seric runqueue(forkflag)
4804639Seric 	bool forkflag;
4814632Seric {
48255360Seric 	register ENVELOPE *e;
48368481Seric 	int njobs;
48468481Seric 	int sequenceno = 0;
48555360Seric 	extern ENVELOPE BlankEnvelope;
48624953Seric 
4877466Seric 	/*
48824953Seric 	**  If no work will ever be selected, don't even bother reading
48924953Seric 	**  the queue.
49024953Seric 	*/
49124953Seric 
49251920Seric 	CurrentLA = getla();	/* get load average */
49340934Srick 
49458132Seric 	if (shouldqueue(0L, curtime()))
49524953Seric 	{
496*68854Seric 		char *msg = "Skipping queue run -- load average too high";
497*68854Seric 
49824953Seric 		if (Verbose)
499*68854Seric 			printf("%s\n", msg);
500*68854Seric #ifdef LOG
501*68854Seric 		if (LogLevel > 8)
502*68854Seric 			syslog(LOG_INFO, "runqueue: %s", msg);
503*68854Seric #endif
50458107Seric 		if (forkflag && QueueIntvl != 0)
50558107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
50655360Seric 		return;
50724953Seric 	}
50824953Seric 
50924953Seric 	/*
5107466Seric 	**  See if we want to go off and do other useful work.
5117466Seric 	*/
5124639Seric 
5134639Seric 	if (forkflag)
5144639Seric 	{
5157943Seric 		int pid;
51665223Seric #ifdef SIGCHLD
51765223Seric 		extern void reapchild();
5187943Seric 
51965223Seric 		(void) setsignal(SIGCHLD, reapchild);
52065223Seric #endif
52165223Seric 
5227943Seric 		pid = dofork();
5237943Seric 		if (pid != 0)
5244639Seric 		{
5257943Seric 			/* parent -- pick up intermediate zombie */
52625184Seric #ifndef SIGCHLD
5279377Seric 			(void) waitfor(pid);
52856795Seric #endif /* SIGCHLD */
5297690Seric 			if (QueueIntvl != 0)
5309348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5314639Seric 			return;
5324639Seric 		}
5337943Seric 		/* child -- double fork */
53425184Seric #ifndef SIGCHLD
5357943Seric 		if (fork() != 0)
5367943Seric 			exit(EX_OK);
53756795Seric #else /* SIGCHLD */
53864035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
53956795Seric #endif /* SIGCHLD */
5404639Seric 	}
54124941Seric 
54240934Srick 	setproctitle("running queue: %s", QueueDir);
54324941Seric 
5447876Seric # ifdef LOG
54558020Seric 	if (LogLevel > 69)
54655360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
54755360Seric 			QueueDir, getpid(), forkflag);
54856795Seric # endif /* LOG */
5494639Seric 
5507466Seric 	/*
55110205Seric 	**  Release any resources used by the daemon code.
55210205Seric 	*/
55310205Seric 
55410205Seric # ifdef DAEMON
55510205Seric 	clrdaemon();
55656795Seric # endif /* DAEMON */
55710205Seric 
55864658Seric 	/* force it to run expensive jobs */
55964658Seric 	NoConnect = FALSE;
56064658Seric 
56110205Seric 	/*
56255360Seric 	**  Create ourselves an envelope
56355360Seric 	*/
56455360Seric 
56555360Seric 	CurEnv = &QueueEnvelope;
56658179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
56755360Seric 	e->e_flags = BlankEnvelope.e_flags;
56855360Seric 
56955360Seric 	/*
57027175Seric 	**  Make sure the alias database is open.
57127175Seric 	*/
57227175Seric 
57360537Seric 	initmaps(FALSE, e);
57427175Seric 
57527175Seric 	/*
5767466Seric 	**  Start making passes through the queue.
5777466Seric 	**	First, read and sort the entire queue.
5787466Seric 	**	Then, process the work in that order.
5797466Seric 	**		But if you take too long, start over.
5807466Seric 	*/
5817466Seric 
5827943Seric 	/* order the existing work requests */
58368481Seric 	njobs = orderq(FALSE);
5847690Seric 
5857943Seric 	/* process them once at a time */
5867943Seric 	while (WorkQ != NULL)
5874639Seric 	{
5887943Seric 		WORK *w = WorkQ;
5897881Seric 
5907943Seric 		WorkQ = WorkQ->w_next;
59158884Seric 
59258884Seric 		/*
59358884Seric 		**  Ignore jobs that are too expensive for the moment.
59458884Seric 		*/
59558884Seric 
59668481Seric 		sequenceno++;
59758884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
59858884Seric 		{
59958884Seric 			if (Verbose)
60068481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
60168481Seric 					w->w_name + 2, sequenceno, njobs);
60258884Seric 		}
60358930Seric 		else
60458930Seric 		{
60564296Seric 			pid_t pid;
60664296Seric 			extern pid_t dowork();
60764296Seric 
60868481Seric 			if (Verbose)
60968481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
61068481Seric 					w->w_name + 2, sequenceno, njobs);
61164296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
61264296Seric 			errno = 0;
61366316Seric 			if (pid != 0)
61466316Seric 				(void) waitfor(pid);
61558930Seric 		}
6167943Seric 		free(w->w_name);
61768481Seric 		if (w->w_host)
61868481Seric 			free(w->w_host);
6197943Seric 		free((char *) w);
6204639Seric 	}
62129866Seric 
62229866Seric 	/* exit without the usual cleanup */
62355467Seric 	e->e_id = NULL;
62455467Seric 	finis();
6254634Seric }
6264634Seric /*
6274632Seric **  ORDERQ -- order the work queue.
6284632Seric **
6294632Seric **	Parameters:
63024941Seric **		doall -- if set, include everything in the queue (even
63124941Seric **			the jobs that cannot be run because the load
63224941Seric **			average is too high).  Otherwise, exclude those
63324941Seric **			jobs.
6344632Seric **
6354632Seric **	Returns:
63610121Seric **		The number of request in the queue (not necessarily
63710121Seric **		the number of requests in WorkQ however).
6384632Seric **
6394632Seric **	Side Effects:
6404632Seric **		Sets WorkQ to the queue of available work, in order.
6414632Seric */
6424632Seric 
64325687Seric # define NEED_P		001
64425687Seric # define NEED_T		002
64558318Seric # define NEED_R		004
64658318Seric # define NEED_S		010
6474632Seric 
64824941Seric orderq(doall)
64924941Seric 	bool doall;
6504632Seric {
65160219Seric 	register struct dirent *d;
6524632Seric 	register WORK *w;
6536625Sglickman 	DIR *f;
6544632Seric 	register int i;
65525687Seric 	WORK wlist[QUEUESIZE+1];
65610070Seric 	int wn = -1;
65768481Seric 	int wc;
6584632Seric 
65958318Seric 	if (tTd(41, 1))
66058318Seric 	{
66158318Seric 		printf("orderq:\n");
66258318Seric 		if (QueueLimitId != NULL)
66358318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
66458318Seric 		if (QueueLimitSender != NULL)
66558318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
66658318Seric 		if (QueueLimitRecipient != NULL)
66758318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
66858318Seric 	}
66958318Seric 
6704632Seric 	/* clear out old WorkQ */
6714632Seric 	for (w = WorkQ; w != NULL; )
6724632Seric 	{
6734632Seric 		register WORK *nw = w->w_next;
6744632Seric 
6754632Seric 		WorkQ = nw;
6764632Seric 		free(w->w_name);
67768481Seric 		if (w->w_host)
67868481Seric 			free(w->w_host);
6794632Seric 		free((char *) w);
6804632Seric 		w = nw;
6814632Seric 	}
6824632Seric 
6834632Seric 	/* open the queue directory */
6848148Seric 	f = opendir(".");
6854632Seric 	if (f == NULL)
6864632Seric 	{
6878148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
68810070Seric 		return (0);
6894632Seric 	}
6904632Seric 
6914632Seric 	/*
6924632Seric 	**  Read the work directory.
6934632Seric 	*/
6944632Seric 
69510070Seric 	while ((d = readdir(f)) != NULL)
6964632Seric 	{
6979377Seric 		FILE *cf;
69864492Seric 		register char *p;
69968528Seric 		char lbuf[MAXNAME + 1];
70058318Seric 		extern bool strcontainedin();
7014632Seric 
7024632Seric 		/* is this an interesting entry? */
7037812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
7044632Seric 			continue;
7054632Seric 
70658318Seric 		if (QueueLimitId != NULL &&
70758318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
70858318Seric 			continue;
70958318Seric 
71068563Seric #ifdef PICKY_QF_NAME_CHECK
71158722Seric 		/*
71258722Seric 		**  Check queue name for plausibility.  This handles
71358722Seric 		**  both old and new type ids.
71458722Seric 		*/
71558722Seric 
71664492Seric 		p = d->d_name + 2;
71764492Seric 		if (isupper(p[0]) && isupper(p[2]))
71864492Seric 			p += 3;
71964492Seric 		else if (isupper(p[1]))
72064492Seric 			p += 2;
72164492Seric 		else
72264492Seric 			p = d->d_name;
72364492Seric 		for (i = 0; isdigit(*p); p++)
72464492Seric 			i++;
72564492Seric 		if (i < 5 || *p != '\0')
72658020Seric 		{
72758020Seric 			if (Verbose)
72858020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
72968563Seric # ifdef LOG
73068563Seric 			if (LogLevel > 0)
73168563Seric 				syslog(LOG_ALERT, "orderq: bogus qf name %s",
73258020Seric 					d->d_name);
73368563Seric # endif
73468706Seric 			if (strlen(d->d_name) > (SIZE_T) MAXNAME)
73568528Seric 				d->d_name[MAXNAME] = '\0';
73658020Seric 			strcpy(lbuf, d->d_name);
73758020Seric 			lbuf[0] = 'Q';
73858020Seric 			(void) rename(d->d_name, lbuf);
73958020Seric 			continue;
74058020Seric 		}
74168563Seric #endif
74258020Seric 
74368563Seric 		/* open control file (if not too many files) */
74425687Seric 		if (++wn >= QUEUESIZE)
74510070Seric 			continue;
74658318Seric 
7478148Seric 		cf = fopen(d->d_name, "r");
7484632Seric 		if (cf == NULL)
7494632Seric 		{
7507055Seric 			/* this may be some random person sending hir msgs */
7517055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
75210090Seric 			if (tTd(41, 2))
75310090Seric 				printf("orderq: cannot open %s (%d)\n",
75410090Seric 					d->d_name, errno);
7557055Seric 			errno = 0;
75610090Seric 			wn--;
7574632Seric 			continue;
7584632Seric 		}
75925687Seric 		w = &wlist[wn];
76025687Seric 		w->w_name = newstr(d->d_name);
76168481Seric 		w->w_host = NULL;
76268481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7634632Seric 
76425027Seric 		/* make sure jobs in creation don't clog queue */
76525687Seric 		w->w_pri = 0x7fffffff;
76625687Seric 		w->w_ctime = 0;
76725027Seric 
7684632Seric 		/* extract useful information */
76925687Seric 		i = NEED_P | NEED_T;
77058318Seric 		if (QueueLimitSender != NULL)
77158318Seric 			i |= NEED_S;
77268481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
77358318Seric 			i |= NEED_R;
77425687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7754632Seric 		{
77658318Seric 			extern bool strcontainedin();
77724954Seric 
77824941Seric 			switch (lbuf[0])
7794632Seric 			{
78024941Seric 			  case 'P':
78125687Seric 				w->w_pri = atol(&lbuf[1]);
78225687Seric 				i &= ~NEED_P;
7834632Seric 				break;
78425013Seric 
78525013Seric 			  case 'T':
78625687Seric 				w->w_ctime = atol(&lbuf[1]);
78725687Seric 				i &= ~NEED_T;
78825013Seric 				break;
78958318Seric 
79058318Seric 			  case 'R':
79168481Seric 				if (w->w_host == NULL &&
79268481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
79368481Seric 					w->w_host = newstr(&p[1]);
79468481Seric 				if (QueueLimitRecipient == NULL ||
79558318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
79658318Seric 					i &= ~NEED_R;
79758318Seric 				break;
79858318Seric 
79958318Seric 			  case 'S':
80058318Seric 				if (QueueLimitSender != NULL &&
80158318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
80258318Seric 					i &= ~NEED_S;
80358318Seric 				break;
8044632Seric 			}
8054632Seric 		}
8064632Seric 		(void) fclose(cf);
80724953Seric 
80858318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
80958318Seric 		    bitset(NEED_R|NEED_S, i))
81024953Seric 		{
81124953Seric 			/* don't even bother sorting this job in */
81268481Seric 			free(w->w_name);
81368481Seric 			if (w->w_host)
81468481Seric 				free(w->w_host);
81524953Seric 			wn--;
81624953Seric 		}
8174632Seric 	}
8186625Sglickman 	(void) closedir(f);
81910090Seric 	wn++;
8204632Seric 
82168481Seric 	wc = min(wn, QUEUESIZE);
8224632Seric 
82368481Seric 	if (QueueSortOrder == QS_BYHOST)
82468481Seric 	{
82568481Seric 		extern workcmpf1();
82668481Seric 		extern workcmpf2();
82767612Seric 
82868481Seric 		/*
82968481Seric 		**  Sort the work directory for the first time,
83068481Seric 		**  based on host name, lock status, and priority.
83168481Seric 		*/
83268481Seric 
83368481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
83468481Seric 
83568481Seric 		/*
83668481Seric 		**  If one message to host is locked, "lock" all messages
83768481Seric 		**  to that host.
83868481Seric 		*/
83968481Seric 
84068481Seric 		i = 0;
84168481Seric 		while (i < wc)
84268481Seric 		{
84368481Seric 			if (!wlist[i].w_lock)
84468481Seric 			{
84568481Seric 				i++;
84668481Seric 				continue;
84768481Seric 			}
84868481Seric 			w = &wlist[i];
84968481Seric 			while (++i < wc)
85068481Seric 			{
85168481Seric 				if (wlist[i].w_host == NULL &&
85268481Seric 				    w->w_host == NULL)
85368481Seric 					wlist[i].w_lock = TRUE;
85468481Seric 				else if (wlist[i].w_host != NULL &&
85568481Seric 					 w->w_host != NULL &&
85668481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
85768481Seric 					wlist[i].w_lock = TRUE;
85868481Seric 				else
85968481Seric 					break;
86068481Seric 			}
86168481Seric 		}
86268481Seric 
86368481Seric 		/*
86468481Seric 		**  Sort the work directory for the second time,
86568481Seric 		**  based on lock status, host name, and priority.
86668481Seric 		*/
86768481Seric 
86868481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
86968481Seric 	}
87068481Seric 	else
87168481Seric 	{
87268481Seric 		extern workcmpf0();
87368481Seric 
87468481Seric 		/*
87568481Seric 		**  Simple sort based on queue priority only.
87668481Seric 		*/
87768481Seric 
87868481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
87968481Seric 	}
88068481Seric 
8814632Seric 	/*
8824632Seric 	**  Convert the work list into canonical form.
8839377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8844632Seric 	*/
8854632Seric 
88624981Seric 	WorkQ = NULL;
88768481Seric 	for (i = wc; --i >= 0; )
8884632Seric 	{
8894632Seric 		w = (WORK *) xalloc(sizeof *w);
8904632Seric 		w->w_name = wlist[i].w_name;
89168481Seric 		w->w_host = wlist[i].w_host;
89268481Seric 		w->w_lock = wlist[i].w_lock;
8934632Seric 		w->w_pri = wlist[i].w_pri;
89425013Seric 		w->w_ctime = wlist[i].w_ctime;
89524981Seric 		w->w_next = WorkQ;
89624981Seric 		WorkQ = w;
8974632Seric 	}
8984632Seric 
8997677Seric 	if (tTd(40, 1))
9004632Seric 	{
9014632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
9025037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
9034632Seric 	}
90410070Seric 
90510090Seric 	return (wn);
9064632Seric }
9074632Seric /*
90868481Seric **  WORKCMPF0 -- simple priority-only compare function.
9094632Seric **
9104632Seric **	Parameters:
9114632Seric **		a -- the first argument.
9124632Seric **		b -- the second argument.
9134632Seric **
9144632Seric **	Returns:
91524981Seric **		-1 if a < b
91624981Seric **		 0 if a == b
91724981Seric **		+1 if a > b
9184632Seric **
9194632Seric **	Side Effects:
9204632Seric **		none.
9214632Seric */
9224632Seric 
92368481Seric workcmpf0(a, b)
9245037Seric 	register WORK *a;
9255037Seric 	register WORK *b;
9264632Seric {
92757438Seric 	long pa = a->w_pri;
92857438Seric 	long pb = b->w_pri;
92924941Seric 
93024941Seric 	if (pa == pb)
93168481Seric 		return 0;
93224941Seric 	else if (pa > pb)
93368481Seric 		return 1;
93424981Seric 	else
93568481Seric 		return -1;
9364632Seric }
9374632Seric /*
93868481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
93968481Seric **
94068481Seric **	Sorts on host name, lock status, and priority in that order.
94168481Seric **
94268481Seric **	Parameters:
94368481Seric **		a -- the first argument.
94468481Seric **		b -- the second argument.
94568481Seric **
94668481Seric **	Returns:
94768481Seric **		<0 if a < b
94868481Seric **		 0 if a == b
94968481Seric **		>0 if a > b
95068481Seric **
95168481Seric **	Side Effects:
95268481Seric **		none.
95368481Seric */
95468481Seric 
95568481Seric workcmpf1(a, b)
95668481Seric 	register WORK *a;
95768481Seric 	register WORK *b;
95868481Seric {
95968481Seric 	int i;
96068481Seric 
96168481Seric 	/* host name */
96268481Seric 	if (a->w_host != NULL && b->w_host == NULL)
96368481Seric 		return 1;
96468481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
96568481Seric 		return -1;
96668481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
96768481Seric 	    (i = strcmp(a->w_host, b->w_host)))
96868481Seric 		return i;
96968481Seric 
97068481Seric 	/* lock status */
97168481Seric 	if (a->w_lock != b->w_lock)
97268481Seric 		return b->w_lock - a->w_lock;
97368481Seric 
97468481Seric 	/* job priority */
97568481Seric 	return a->w_pri - b->w_pri;
97668481Seric }
97768481Seric /*
97868481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
97968481Seric **
98068481Seric **	Sorts on lock status, host name, and priority in that order.
98168481Seric **
98268481Seric **	Parameters:
98368481Seric **		a -- the first argument.
98468481Seric **		b -- the second argument.
98568481Seric **
98668481Seric **	Returns:
98768481Seric **		<0 if a < b
98868481Seric **		 0 if a == b
98968481Seric **		>0 if a > b
99068481Seric **
99168481Seric **	Side Effects:
99268481Seric **		none.
99368481Seric */
99468481Seric 
99568481Seric workcmpf2(a, b)
99668481Seric 	register WORK *a;
99768481Seric 	register WORK *b;
99868481Seric {
99968481Seric 	int i;
100068481Seric 
100168481Seric 	/* lock status */
100268481Seric 	if (a->w_lock != b->w_lock)
100368481Seric 		return a->w_lock - b->w_lock;
100468481Seric 
100568481Seric 	/* host name */
100668481Seric 	if (a->w_host != NULL && b->w_host == NULL)
100768481Seric 		return 1;
100868481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
100968481Seric 		return -1;
101068481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
101168481Seric 	    (i = strcmp(a->w_host, b->w_host)))
101268481Seric 		return i;
101368481Seric 
101468481Seric 	/* job priority */
101568481Seric 	return a->w_pri - b->w_pri;
101668481Seric }
101768481Seric /*
10184632Seric **  DOWORK -- do a work request.
10194632Seric **
10204632Seric **	Parameters:
102158884Seric **		id -- the ID of the job to run.
102258884Seric **		forkflag -- if set, run this in background.
102358924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
102458924Seric **			This is used when expanding aliases in the queue.
102561094Seric **			If forkflag is also set, it doesn't wait for the
102661094Seric **			child.
102758884Seric **		e - the envelope in which to run it.
10284632Seric **
10294632Seric **	Returns:
103064296Seric **		process id of process that is running the queue job.
10314632Seric **
10324632Seric **	Side Effects:
10334632Seric **		The work request is satisfied if possible.
10344632Seric */
10354632Seric 
103664296Seric pid_t
103758924Seric dowork(id, forkflag, requeueflag, e)
103858884Seric 	char *id;
103958884Seric 	bool forkflag;
104058924Seric 	bool requeueflag;
104155012Seric 	register ENVELOPE *e;
10424632Seric {
104364296Seric 	register pid_t pid;
104451920Seric 	extern bool readqf();
10454632Seric 
10467677Seric 	if (tTd(40, 1))
104758884Seric 		printf("dowork(%s)\n", id);
10484632Seric 
10494632Seric 	/*
105024941Seric 	**  Fork for work.
105124941Seric 	*/
105224941Seric 
105358884Seric 	if (forkflag)
105424941Seric 	{
105564296Seric 		pid = fork();
105664296Seric 		if (pid < 0)
105724941Seric 		{
105824941Seric 			syserr("dowork: cannot fork");
105964296Seric 			return 0;
106024941Seric 		}
106164839Seric 		else if (pid > 0)
106264839Seric 		{
106364839Seric 			/* parent -- clean out connection cache */
106464839Seric 			mci_flush(FALSE, NULL);
106564839Seric 		}
106624941Seric 	}
106724941Seric 	else
106824941Seric 	{
106964296Seric 		pid = 0;
107024941Seric 	}
107124941Seric 
107264296Seric 	if (pid == 0)
10734632Seric 	{
10744632Seric 		/*
10754632Seric 		**  CHILD
10768148Seric 		**	Lock the control file to avoid duplicate deliveries.
10778148Seric 		**		Then run the file as though we had just read it.
10787350Seric 		**	We save an idea of the temporary name so we
10797350Seric 		**		can recover on interrupt.
10804632Seric 		*/
10814632Seric 
10827763Seric 		/* set basic modes, etc. */
10837356Seric 		(void) alarm(0);
108455012Seric 		clearenvelope(e, FALSE);
108564554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
108658734Seric 		e->e_errormode = EM_MAIL;
108758884Seric 		e->e_id = id;
108864765Seric 		GrabTo = UseErrorsTo = FALSE;
108966316Seric 		ExitStat = EX_OK;
109063846Seric 		if (forkflag)
109164554Seric 		{
109264296Seric 			disconnect(1, e);
109364554Seric 			OpMode = MD_DELIVER;
109464554Seric 		}
10957876Seric # ifdef LOG
109658020Seric 		if (LogLevel > 76)
109755012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
10987881Seric 			       getpid());
109956795Seric # endif /* LOG */
11007763Seric 
11017763Seric 		/* don't use the headers from sendmail.cf... */
110255012Seric 		e->e_header = NULL;
11037763Seric 
110451920Seric 		/* read the queue control file -- return if locked */
110565200Seric 		if (!readqf(e))
11066980Seric 		{
110758884Seric 			if (tTd(40, 4))
110858884Seric 				printf("readqf(%s) failed\n", e->e_id);
110958884Seric 			if (forkflag)
111024941Seric 				exit(EX_OK);
111124941Seric 			else
111266316Seric 				return 0;
11136980Seric 		}
11146980Seric 
111555012Seric 		e->e_flags |= EF_INQUEUE;
11166980Seric 
111768481Seric 		/* if this has been tried recently, let it be */
111868481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
111968481Seric 		{
112068481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
112158924Seric 
112268481Seric 			e->e_flags |= EF_KEEPQUEUE;
112368481Seric 			if (Verbose || tTd(40, 8))
112468481Seric 				printf("%s: too young (%s)\n",
112568481Seric 					e->e_id, howlong);
112668481Seric #ifdef LOG
112768481Seric 			if (LogLevel > 19)
112868481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
112968481Seric 					e->e_id, howlong);
113068481Seric #endif
113168481Seric 		}
113268481Seric 		else
113368481Seric 		{
113468481Seric 			eatheader(e, requeueflag);
11356980Seric 
113668481Seric 			if (requeueflag)
113768481Seric 				queueup(e, TRUE, FALSE);
113868481Seric 
113968481Seric 			/* do the delivery */
114068481Seric 			sendall(e, SM_DELIVER);
114168481Seric 		}
114268481Seric 
11436980Seric 		/* finish up and exit */
114458884Seric 		if (forkflag)
114524941Seric 			finis();
114624941Seric 		else
114755012Seric 			dropenvelope(e);
11484632Seric 	}
114964296Seric 	e->e_id = NULL;
115064296Seric 	return pid;
11514632Seric }
11524632Seric /*
11534632Seric **  READQF -- read queue file and set up environment.
11544632Seric **
11554632Seric **	Parameters:
11569377Seric **		e -- the envelope of the job to run.
11574632Seric **
11584632Seric **	Returns:
115951920Seric **		TRUE if it successfully read the queue file.
116051920Seric **		FALSE otherwise.
11614632Seric **
11624632Seric **	Side Effects:
116351920Seric **		The queue file is returned locked.
11644632Seric */
11654632Seric 
116651920Seric bool
116765200Seric readqf(e)
11689377Seric 	register ENVELOPE *e;
11694632Seric {
117017477Seric 	register FILE *qfp;
117154974Seric 	ADDRESS *ctladdr;
117256400Seric 	struct stat st;
117357135Seric 	char *bp;
117468481Seric 	int qfver = 0;
117568481Seric 	register char *p;
117668481Seric 	char *orcpt = NULL;
117758915Seric 	char qf[20];
117857135Seric 	char buf[MAXLINE];
117954974Seric 	extern ADDRESS *setctluser();
118068490Seric 	extern void loseqfile();
11814632Seric 
11824632Seric 	/*
118317468Seric 	**  Read and process the file.
11844632Seric 	*/
11854632Seric 
118658915Seric 	strcpy(qf, queuename(e, 'q'));
118751937Seric 	qfp = fopen(qf, "r+");
118817477Seric 	if (qfp == NULL)
118917477Seric 	{
119058884Seric 		if (tTd(40, 8))
119158884Seric 			printf("readqf(%s): fopen failure (%s)\n",
119258884Seric 				qf, errstring(errno));
119340934Srick 		if (errno != ENOENT)
119440934Srick 			syserr("readqf: no control file %s", qf);
119551920Seric 		return FALSE;
119617477Seric 	}
119740934Srick 
119864335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
119964277Seric 	{
120064277Seric 		/* being processed by another queuer */
120168481Seric 		if (Verbose || tTd(40, 8))
120264277Seric 			printf("%s: locked\n", e->e_id);
120364277Seric # ifdef LOG
120464277Seric 		if (LogLevel > 19)
120564277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
120664277Seric # endif /* LOG */
120764277Seric 		(void) fclose(qfp);
120864277Seric 		return FALSE;
120964277Seric 	}
121064277Seric 
121156400Seric 	/*
121256400Seric 	**  Check the queue file for plausibility to avoid attacks.
121356400Seric 	*/
121456400Seric 
121556400Seric 	if (fstat(fileno(qfp), &st) < 0)
121656400Seric 	{
121756400Seric 		/* must have been being processed by someone else */
121858884Seric 		if (tTd(40, 8))
121958884Seric 			printf("readqf(%s): fstat failure (%s)\n",
122058884Seric 				qf, errstring(errno));
122156400Seric 		fclose(qfp);
122256400Seric 		return FALSE;
122356400Seric 	}
122456400Seric 
122568563Seric 	if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode))
122656400Seric 	{
122756400Seric # ifdef LOG
122856400Seric 		if (LogLevel > 0)
122956400Seric 		{
123056400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
123156400Seric 				e->e_id, st.st_uid, st.st_mode);
123256400Seric 		}
123356795Seric # endif /* LOG */
123458884Seric 		if (tTd(40, 8))
123558884Seric 			printf("readqf(%s): bogus file\n", qf);
123668490Seric 		loseqfile(e, "bogus file uid in mqueue");
123756400Seric 		fclose(qfp);
123856400Seric 		return FALSE;
123956400Seric 	}
124056400Seric 
124164277Seric 	if (st.st_size == 0)
124240934Srick 	{
124364277Seric 		/* must be a bogus file -- just remove it */
124464277Seric 		(void) unlink(qf);
124564277Seric 		fclose(qfp);
124651920Seric 		return FALSE;
124740934Srick 	}
124840934Srick 
124964277Seric 	if (st.st_nlink == 0)
125059101Seric 	{
125164277Seric 		/*
125264277Seric 		**  Race condition -- we got a file just as it was being
125364277Seric 		**  unlinked.  Just assume it is zero length.
125464277Seric 		*/
125564277Seric 
125659101Seric 		fclose(qfp);
125759101Seric 		return FALSE;
125859101Seric 	}
125959101Seric 
126064277Seric 	/* good file -- save this lock */
126151920Seric 	e->e_lockfp = qfp;
126251920Seric 
126340934Srick 	/* do basic system initialization */
126455012Seric 	initsys(e);
126565164Seric 	define('i', e->e_id, e);
126640934Srick 
12679377Seric 	LineNumber = 0;
126863850Seric 	e->e_flags |= EF_GLOBALERRS;
126963850Seric 	OpMode = MD_DELIVER;
127054974Seric 	ctladdr = NULL;
127168481Seric 	e->e_dfino = -1;
127268564Seric 	e->e_msgsize = -1;
127357135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12744632Seric 	{
127558737Seric 		register char *p;
127668481Seric 		u_long qflags;
127768481Seric 		ADDRESS *q;
127857529Seric 
127926504Seric 		if (tTd(40, 4))
128057135Seric 			printf("+++++ %s\n", bp);
128157135Seric 		switch (bp[0])
12824632Seric 		{
128368481Seric 		  case 'V':		/* queue file version number */
128468481Seric 			qfver = atoi(&bp[1]);
128568481Seric 			if (qfver > QF_VERSION)
128668481Seric 			{
128768481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
128868481Seric 					qfver, QF_VERSION);
128968481Seric 			}
129068481Seric 			break;
129168481Seric 
129240973Sbostic 		  case 'C':		/* specify controlling user */
129357135Seric 			ctladdr = setctluser(&bp[1]);
129440973Sbostic 			break;
129540973Sbostic 
129668481Seric 		  case 'Q':		/* original recipient */
129768481Seric 			orcpt = newstr(&bp[1]);
129868481Seric 			break;
129968481Seric 
13004632Seric 		  case 'R':		/* specify recipient */
130168481Seric 			p = bp;
130268481Seric 			qflags = 0;
130368481Seric 			if (qfver >= 1)
130468481Seric 			{
130568481Seric 				/* get flag bits */
130668481Seric 				while (*++p != '\0' && *p != ':')
130768481Seric 				{
130868481Seric 					switch (*p)
130968481Seric 					{
131068481Seric 					  case 'S':
131168481Seric 						qflags |= QPINGONSUCCESS;
131268481Seric 						break;
131368481Seric 
131468481Seric 					  case 'F':
131568481Seric 						qflags |= QPINGONFAILURE;
131668481Seric 						break;
131768481Seric 
131868481Seric 					  case 'D':
131968481Seric 						qflags |= QPINGONDELAY;
132068481Seric 						break;
132168481Seric 
132268481Seric 					  case 'P':
132368481Seric 						qflags |= QPRIMARY;
132468481Seric 						break;
132568481Seric 					}
132668481Seric 				}
132768481Seric 			}
132868481Seric 			else
132968481Seric 				qflags |= QPRIMARY;
133068481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
133168481Seric 			if (q != NULL)
133268481Seric 			{
133368481Seric 				q->q_alias = ctladdr;
133468481Seric 				q->q_flags |= qflags;
133568481Seric 				q->q_orcpt = orcpt;
133668481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
133768481Seric 			}
133868481Seric 			orcpt = NULL;
13394632Seric 			break;
13404632Seric 
134125687Seric 		  case 'E':		/* specify error recipient */
134268558Seric 			/* no longer used */
134325687Seric 			break;
134425687Seric 
13454632Seric 		  case 'H':		/* header */
134668717Seric 			(void) chompheader(&bp[1], FALSE, NULL, e);
13474632Seric 			break;
13484632Seric 
134910108Seric 		  case 'M':		/* message */
135064705Seric 			/* ignore this; we want a new message next time */
135110108Seric 			break;
135210108Seric 
13534632Seric 		  case 'S':		/* sender */
135458704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13554632Seric 			break;
13564632Seric 
135759093Seric 		  case 'B':		/* body type */
135859093Seric 			e->e_bodytype = newstr(&bp[1]);
135959093Seric 			break;
136059093Seric 
13614632Seric 		  case 'D':		/* data file name */
136268564Seric 			/* obsolete -- ignore */
13634632Seric 			break;
13644632Seric 
13657860Seric 		  case 'T':		/* init time */
136657135Seric 			e->e_ctime = atol(&bp[1]);
13674632Seric 			break;
13684632Seric 
136968481Seric 		  case 'I':		/* data file's inode number */
137068481Seric 			if (e->e_dfino == -1)
137168481Seric 				e->e_dfino = atol(&buf[1]);
137268481Seric 			break;
137368481Seric 
137468481Seric 		  case 'K':		/* time of last deliver attempt */
137568481Seric 			e->e_dtime = atol(&buf[1]);
137668481Seric 			break;
137768481Seric 
137868481Seric 		  case 'N':		/* number of delivery attempts */
137968481Seric 			e->e_ntries = atoi(&buf[1]);
138068481Seric 			break;
138168481Seric 
13824634Seric 		  case 'P':		/* message priority */
138357135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
13844634Seric 			break;
13854634Seric 
138658737Seric 		  case 'F':		/* flag bits */
138758737Seric 			for (p = &bp[1]; *p != '\0'; p++)
138858737Seric 			{
138958737Seric 				switch (*p)
139058737Seric 				{
139158737Seric 				  case 'w':	/* warning sent */
139258737Seric 					e->e_flags |= EF_WARNING;
139358737Seric 					break;
139458737Seric 
139558737Seric 				  case 'r':	/* response */
139658737Seric 					e->e_flags |= EF_RESPONSE;
139758737Seric 					break;
139868481Seric 
139968481Seric 				  case '8':	/* has 8 bit data */
140068481Seric 					e->e_flags |= EF_HAS8BIT;
140168481Seric 					break;
140258737Seric 				}
140358737Seric 			}
140458737Seric 			break;
140558737Seric 
140668481Seric 		  case 'Z':		/* original envelope id from ESMTP */
140768481Seric 			e->e_envid = newstr(&bp[1]);
140868481Seric 			break;
140968481Seric 
141053400Seric 		  case '$':		/* define macro */
141157135Seric 			define(bp[1], newstr(&bp[2]), e);
141253400Seric 			break;
141353400Seric 
141424941Seric 		  case '\0':		/* blank line; ignore */
141524941Seric 			break;
141624941Seric 
14174632Seric 		  default:
141865960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
141965200Seric 				qf, LineNumber, bp);
142063753Seric 			fclose(qfp);
142168490Seric 			loseqfile(e, "unrecognized line");
142263753Seric 			return FALSE;
14234632Seric 		}
142457135Seric 
142557135Seric 		if (bp != buf)
142657135Seric 			free(bp);
14274632Seric 	}
14289377Seric 
142924941Seric 	/*
143024941Seric 	**  If we haven't read any lines, this queue file is empty.
143124941Seric 	**  Arrange to remove it without referencing any null pointers.
143224941Seric 	*/
143324941Seric 
143424941Seric 	if (LineNumber == 0)
143524941Seric 	{
143624941Seric 		errno = 0;
143724941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
143824941Seric 	}
143968564Seric 	else
144068564Seric 	{
144168564Seric 		/*
144268564Seric 		**  Arrange to read the data file.
144368564Seric 		*/
144468564Seric 
144568564Seric 		p = queuename(e, 'd');
144668564Seric 		e->e_dfp = fopen(p, "r");
144768564Seric 		if (e->e_dfp == NULL)
144868564Seric 		{
144968564Seric 			syserr("readqf: cannot open %s", p);
145068564Seric 		}
145168603Seric 		else
145268564Seric 		{
145368603Seric 			e->e_flags |= EF_HAS_DF;
145468603Seric 			if (fstat(fileno(e->e_dfp), &st) >= 0)
145568603Seric 			{
145668603Seric 				e->e_msgsize = st.st_size;
145768603Seric 				e->e_dfdev = st.st_dev;
145868603Seric 				e->e_dfino = st.st_ino;
145968603Seric 			}
146068564Seric 		}
146168564Seric 	}
146268564Seric 
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;
155068565Seric 		long dfsize;
155158737Seric 		int flags = 0;
155268481Seric 		int qfver;
155310108Seric 		char message[MAXLINE];
155468528Seric 		char bodytype[MAXNAME + 1];
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 		}
156468565Seric 		w->w_name[0] = 'd';
156568565Seric 		if (stat(w->w_name, &st) >= 0)
156668565Seric 			dfsize = st.st_size;
156768565Seric 		else
156868565Seric 			dfsize = -1;
156968481Seric 		if (w->w_lock)
157010070Seric 			printf("*");
157157438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
157224941Seric 			printf("X");
157310070Seric 		else
157410070Seric 			printf(" ");
157510070Seric 		errno = 0;
157617468Seric 
157759093Seric 		message[0] = bodytype[0] = '\0';
157868481Seric 		qfver = 0;
15799630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
15809630Seric 		{
158153400Seric 			register int i;
158258737Seric 			register char *p;
158353400Seric 
15849630Seric 			fixcrlf(buf, TRUE);
15859630Seric 			switch (buf[0])
15869630Seric 			{
158768481Seric 			  case 'V':	/* queue file version */
158868481Seric 				qfver = atoi(&buf[1]);
158968481Seric 				break;
159068481Seric 
159110108Seric 			  case 'M':	/* error message */
159253400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
159358737Seric 					i = sizeof message - 1;
159453400Seric 				bcopy(&buf[1], message, i);
159553400Seric 				message[i] = '\0';
159610108Seric 				break;
159710108Seric 
159859093Seric 			  case 'B':	/* body type */
159959093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
160059093Seric 					i = sizeof bodytype - 1;
160159093Seric 				bcopy(&buf[1], bodytype, i);
160259093Seric 				bodytype[i] = '\0';
160359093Seric 				break;
160459093Seric 
16059630Seric 			  case 'S':	/* sender name */
160624979Seric 				if (Verbose)
160758737Seric 					printf("%8ld %10ld%c%.12s %.38s",
160858737Seric 					    dfsize,
160958737Seric 					    w->w_pri,
161058737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
161158737Seric 					    ctime(&submittime) + 4,
161224979Seric 					    &buf[1]);
161324979Seric 				else
161424979Seric 					printf("%8ld %.16s %.45s", dfsize,
161524979Seric 					    ctime(&submittime), &buf[1]);
161659093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
161759093Seric 				{
161859093Seric 					printf("\n    %10.10s", bodytype);
161959093Seric 					if (message[0] != '\0')
162059093Seric 						printf("   (%.60s)", message);
162159093Seric 				}
16229630Seric 				break;
162351920Seric 
162440973Sbostic 			  case 'C':	/* controlling user */
162554974Seric 				if (Verbose)
162658716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
162758716Seric 						&buf[1]);
162840973Sbostic 				break;
16299630Seric 
16309630Seric 			  case 'R':	/* recipient name */
163168481Seric 				p = &buf[1];
163268481Seric 				if (qfver >= 1)
163368481Seric 				{
163468481Seric 					p = strchr(p, ':');
163568481Seric 					if (p == NULL)
163668481Seric 						break;
163768481Seric 					p++;
163868481Seric 				}
163924979Seric 				if (Verbose)
164068481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
164124979Seric 				else
164268481Seric 					printf("\n\t\t\t\t   %.45s", p);
16439630Seric 				break;
16449630Seric 
16459630Seric 			  case 'T':	/* creation time */
164624941Seric 				submittime = atol(&buf[1]);
16479630Seric 				break;
164810070Seric 
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;
170168528Seric 	static char buf[MAXNAME + 1];
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';
185868693Seric 	if (*user != '\0' && (pw = sm_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;
186659270Seric 		a->q_flags |= QGOODUID;
186740973Sbostic 	}
186868603Seric 
186968603Seric 	if (*user != '\0')
187068603Seric 		a->q_user = newstr(user);
187168603Seric 	else if (p != NULL)
187268603Seric 		a->q_user = newstr(p);
187340973Sbostic 	else
187457642Seric 		a->q_user = newstr(DefUser);
187540973Sbostic 
187659270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
187756328Seric 	a->q_mailer = LocalMailer;
187859113Seric 	if (p == NULL)
187959113Seric 		a->q_paddr = a->q_user;
188059113Seric 	else
188159113Seric 		a->q_paddr = newstr(p);
188254974Seric 	return a;
188340973Sbostic }
188468490Seric /*
188568490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
188668490Seric **
188768490Seric **	Parameters:
188868490Seric **		e -- the envelope (e->e_id will be used).
188968490Seric **		why -- reported to whomever can hear.
189068490Seric **
189168490Seric **	Returns:
189268490Seric **		none.
189368490Seric */
189468490Seric 
189568490Seric void
189668490Seric loseqfile(e, why)
189768490Seric 	register ENVELOPE *e;
189868490Seric 	char *why;
189968490Seric {
190068563Seric 	char *p;
190168490Seric 	char buf[40];
190268490Seric 
190368490Seric 	if (e == NULL || e->e_id == NULL)
190468490Seric 		return;
190568490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
190668490Seric 		return;
190768490Seric 	strcpy(buf, queuename(e, 'q'));
190868563Seric 	p = queuename(e, 'Q');
190968563Seric 	if (rename(buf, p) < 0)
191068563Seric 		syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid());
191168490Seric #ifdef LOG
191268563Seric 	else if (LogLevel > 0)
191368563Seric 		syslog(LOG_ALERT, "Losing %s: %s", buf, why);
191468490Seric #endif
191568490Seric }
1916