xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68706)
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*68706Seric static char sccsid[] = "@(#)queue.c	8.76 (Berkeley) 03/31/95 (with queueing)";
1433731Sbostic #else
15*68706Seric static char sccsid[] = "@(#)queue.c	8.76 (Berkeley) 03/31/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 	{
49624953Seric 		if (Verbose)
49724953Seric 			printf("Skipping queue run -- load average too high\n");
49858107Seric 		if (forkflag && QueueIntvl != 0)
49958107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
50055360Seric 		return;
50124953Seric 	}
50224953Seric 
50324953Seric 	/*
5047466Seric 	**  See if we want to go off and do other useful work.
5057466Seric 	*/
5064639Seric 
5074639Seric 	if (forkflag)
5084639Seric 	{
5097943Seric 		int pid;
51065223Seric #ifdef SIGCHLD
51165223Seric 		extern void reapchild();
5127943Seric 
51365223Seric 		(void) setsignal(SIGCHLD, reapchild);
51465223Seric #endif
51565223Seric 
5167943Seric 		pid = dofork();
5177943Seric 		if (pid != 0)
5184639Seric 		{
5197943Seric 			/* parent -- pick up intermediate zombie */
52025184Seric #ifndef SIGCHLD
5219377Seric 			(void) waitfor(pid);
52256795Seric #endif /* SIGCHLD */
5237690Seric 			if (QueueIntvl != 0)
5249348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5254639Seric 			return;
5264639Seric 		}
5277943Seric 		/* child -- double fork */
52825184Seric #ifndef SIGCHLD
5297943Seric 		if (fork() != 0)
5307943Seric 			exit(EX_OK);
53156795Seric #else /* SIGCHLD */
53264035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
53356795Seric #endif /* SIGCHLD */
5344639Seric 	}
53524941Seric 
53640934Srick 	setproctitle("running queue: %s", QueueDir);
53724941Seric 
5387876Seric # ifdef LOG
53958020Seric 	if (LogLevel > 69)
54055360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
54155360Seric 			QueueDir, getpid(), forkflag);
54256795Seric # endif /* LOG */
5434639Seric 
5447466Seric 	/*
54510205Seric 	**  Release any resources used by the daemon code.
54610205Seric 	*/
54710205Seric 
54810205Seric # ifdef DAEMON
54910205Seric 	clrdaemon();
55056795Seric # endif /* DAEMON */
55110205Seric 
55264658Seric 	/* force it to run expensive jobs */
55364658Seric 	NoConnect = FALSE;
55464658Seric 
55510205Seric 	/*
55655360Seric 	**  Create ourselves an envelope
55755360Seric 	*/
55855360Seric 
55955360Seric 	CurEnv = &QueueEnvelope;
56058179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
56155360Seric 	e->e_flags = BlankEnvelope.e_flags;
56255360Seric 
56355360Seric 	/*
56427175Seric 	**  Make sure the alias database is open.
56527175Seric 	*/
56627175Seric 
56760537Seric 	initmaps(FALSE, e);
56827175Seric 
56927175Seric 	/*
5707466Seric 	**  Start making passes through the queue.
5717466Seric 	**	First, read and sort the entire queue.
5727466Seric 	**	Then, process the work in that order.
5737466Seric 	**		But if you take too long, start over.
5747466Seric 	*/
5757466Seric 
5767943Seric 	/* order the existing work requests */
57768481Seric 	njobs = orderq(FALSE);
5787690Seric 
5797943Seric 	/* process them once at a time */
5807943Seric 	while (WorkQ != NULL)
5814639Seric 	{
5827943Seric 		WORK *w = WorkQ;
5837881Seric 
5847943Seric 		WorkQ = WorkQ->w_next;
58558884Seric 
58658884Seric 		/*
58758884Seric 		**  Ignore jobs that are too expensive for the moment.
58858884Seric 		*/
58958884Seric 
59068481Seric 		sequenceno++;
59158884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
59258884Seric 		{
59358884Seric 			if (Verbose)
59468481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
59568481Seric 					w->w_name + 2, sequenceno, njobs);
59658884Seric 		}
59758930Seric 		else
59858930Seric 		{
59964296Seric 			pid_t pid;
60064296Seric 			extern pid_t dowork();
60164296Seric 
60268481Seric 			if (Verbose)
60368481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
60468481Seric 					w->w_name + 2, sequenceno, njobs);
60564296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
60664296Seric 			errno = 0;
60766316Seric 			if (pid != 0)
60866316Seric 				(void) waitfor(pid);
60958930Seric 		}
6107943Seric 		free(w->w_name);
61168481Seric 		if (w->w_host)
61268481Seric 			free(w->w_host);
6137943Seric 		free((char *) w);
6144639Seric 	}
61529866Seric 
61629866Seric 	/* exit without the usual cleanup */
61755467Seric 	e->e_id = NULL;
61855467Seric 	finis();
6194634Seric }
6204634Seric /*
6214632Seric **  ORDERQ -- order the work queue.
6224632Seric **
6234632Seric **	Parameters:
62424941Seric **		doall -- if set, include everything in the queue (even
62524941Seric **			the jobs that cannot be run because the load
62624941Seric **			average is too high).  Otherwise, exclude those
62724941Seric **			jobs.
6284632Seric **
6294632Seric **	Returns:
63010121Seric **		The number of request in the queue (not necessarily
63110121Seric **		the number of requests in WorkQ however).
6324632Seric **
6334632Seric **	Side Effects:
6344632Seric **		Sets WorkQ to the queue of available work, in order.
6354632Seric */
6364632Seric 
63725687Seric # define NEED_P		001
63825687Seric # define NEED_T		002
63958318Seric # define NEED_R		004
64058318Seric # define NEED_S		010
6414632Seric 
64224941Seric orderq(doall)
64324941Seric 	bool doall;
6444632Seric {
64560219Seric 	register struct dirent *d;
6464632Seric 	register WORK *w;
6476625Sglickman 	DIR *f;
6484632Seric 	register int i;
64925687Seric 	WORK wlist[QUEUESIZE+1];
65010070Seric 	int wn = -1;
65168481Seric 	int wc;
6524632Seric 
65358318Seric 	if (tTd(41, 1))
65458318Seric 	{
65558318Seric 		printf("orderq:\n");
65658318Seric 		if (QueueLimitId != NULL)
65758318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
65858318Seric 		if (QueueLimitSender != NULL)
65958318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
66058318Seric 		if (QueueLimitRecipient != NULL)
66158318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
66258318Seric 	}
66358318Seric 
6644632Seric 	/* clear out old WorkQ */
6654632Seric 	for (w = WorkQ; w != NULL; )
6664632Seric 	{
6674632Seric 		register WORK *nw = w->w_next;
6684632Seric 
6694632Seric 		WorkQ = nw;
6704632Seric 		free(w->w_name);
67168481Seric 		if (w->w_host)
67268481Seric 			free(w->w_host);
6734632Seric 		free((char *) w);
6744632Seric 		w = nw;
6754632Seric 	}
6764632Seric 
6774632Seric 	/* open the queue directory */
6788148Seric 	f = opendir(".");
6794632Seric 	if (f == NULL)
6804632Seric 	{
6818148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
68210070Seric 		return (0);
6834632Seric 	}
6844632Seric 
6854632Seric 	/*
6864632Seric 	**  Read the work directory.
6874632Seric 	*/
6884632Seric 
68910070Seric 	while ((d = readdir(f)) != NULL)
6904632Seric 	{
6919377Seric 		FILE *cf;
69264492Seric 		register char *p;
69368528Seric 		char lbuf[MAXNAME + 1];
69458318Seric 		extern bool strcontainedin();
6954632Seric 
6964632Seric 		/* is this an interesting entry? */
6977812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6984632Seric 			continue;
6994632Seric 
70058318Seric 		if (QueueLimitId != NULL &&
70158318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
70258318Seric 			continue;
70358318Seric 
70468563Seric #ifdef PICKY_QF_NAME_CHECK
70558722Seric 		/*
70658722Seric 		**  Check queue name for plausibility.  This handles
70758722Seric 		**  both old and new type ids.
70858722Seric 		*/
70958722Seric 
71064492Seric 		p = d->d_name + 2;
71164492Seric 		if (isupper(p[0]) && isupper(p[2]))
71264492Seric 			p += 3;
71364492Seric 		else if (isupper(p[1]))
71464492Seric 			p += 2;
71564492Seric 		else
71664492Seric 			p = d->d_name;
71764492Seric 		for (i = 0; isdigit(*p); p++)
71864492Seric 			i++;
71964492Seric 		if (i < 5 || *p != '\0')
72058020Seric 		{
72158020Seric 			if (Verbose)
72258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
72368563Seric # ifdef LOG
72468563Seric 			if (LogLevel > 0)
72568563Seric 				syslog(LOG_ALERT, "orderq: bogus qf name %s",
72658020Seric 					d->d_name);
72768563Seric # endif
728*68706Seric 			if (strlen(d->d_name) > (SIZE_T) MAXNAME)
72968528Seric 				d->d_name[MAXNAME] = '\0';
73058020Seric 			strcpy(lbuf, d->d_name);
73158020Seric 			lbuf[0] = 'Q';
73258020Seric 			(void) rename(d->d_name, lbuf);
73358020Seric 			continue;
73458020Seric 		}
73568563Seric #endif
73658020Seric 
73768563Seric 		/* open control file (if not too many files) */
73825687Seric 		if (++wn >= QUEUESIZE)
73910070Seric 			continue;
74058318Seric 
7418148Seric 		cf = fopen(d->d_name, "r");
7424632Seric 		if (cf == NULL)
7434632Seric 		{
7447055Seric 			/* this may be some random person sending hir msgs */
7457055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
74610090Seric 			if (tTd(41, 2))
74710090Seric 				printf("orderq: cannot open %s (%d)\n",
74810090Seric 					d->d_name, errno);
7497055Seric 			errno = 0;
75010090Seric 			wn--;
7514632Seric 			continue;
7524632Seric 		}
75325687Seric 		w = &wlist[wn];
75425687Seric 		w->w_name = newstr(d->d_name);
75568481Seric 		w->w_host = NULL;
75668481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7574632Seric 
75825027Seric 		/* make sure jobs in creation don't clog queue */
75925687Seric 		w->w_pri = 0x7fffffff;
76025687Seric 		w->w_ctime = 0;
76125027Seric 
7624632Seric 		/* extract useful information */
76325687Seric 		i = NEED_P | NEED_T;
76458318Seric 		if (QueueLimitSender != NULL)
76558318Seric 			i |= NEED_S;
76668481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
76758318Seric 			i |= NEED_R;
76825687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7694632Seric 		{
77024954Seric 			extern long atol();
77158318Seric 			extern bool strcontainedin();
77224954Seric 
77324941Seric 			switch (lbuf[0])
7744632Seric 			{
77524941Seric 			  case 'P':
77625687Seric 				w->w_pri = atol(&lbuf[1]);
77725687Seric 				i &= ~NEED_P;
7784632Seric 				break;
77925013Seric 
78025013Seric 			  case 'T':
78125687Seric 				w->w_ctime = atol(&lbuf[1]);
78225687Seric 				i &= ~NEED_T;
78325013Seric 				break;
78458318Seric 
78558318Seric 			  case 'R':
78668481Seric 				if (w->w_host == NULL &&
78768481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
78868481Seric 					w->w_host = newstr(&p[1]);
78968481Seric 				if (QueueLimitRecipient == NULL ||
79058318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
79158318Seric 					i &= ~NEED_R;
79258318Seric 				break;
79358318Seric 
79458318Seric 			  case 'S':
79558318Seric 				if (QueueLimitSender != NULL &&
79658318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
79758318Seric 					i &= ~NEED_S;
79858318Seric 				break;
7994632Seric 			}
8004632Seric 		}
8014632Seric 		(void) fclose(cf);
80224953Seric 
80358318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
80458318Seric 		    bitset(NEED_R|NEED_S, i))
80524953Seric 		{
80624953Seric 			/* don't even bother sorting this job in */
80768481Seric 			free(w->w_name);
80868481Seric 			if (w->w_host)
80968481Seric 				free(w->w_host);
81024953Seric 			wn--;
81124953Seric 		}
8124632Seric 	}
8136625Sglickman 	(void) closedir(f);
81410090Seric 	wn++;
8154632Seric 
81668481Seric 	wc = min(wn, QUEUESIZE);
8174632Seric 
81868481Seric 	if (QueueSortOrder == QS_BYHOST)
81968481Seric 	{
82068481Seric 		extern workcmpf1();
82168481Seric 		extern workcmpf2();
82267612Seric 
82368481Seric 		/*
82468481Seric 		**  Sort the work directory for the first time,
82568481Seric 		**  based on host name, lock status, and priority.
82668481Seric 		*/
82768481Seric 
82868481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
82968481Seric 
83068481Seric 		/*
83168481Seric 		**  If one message to host is locked, "lock" all messages
83268481Seric 		**  to that host.
83368481Seric 		*/
83468481Seric 
83568481Seric 		i = 0;
83668481Seric 		while (i < wc)
83768481Seric 		{
83868481Seric 			if (!wlist[i].w_lock)
83968481Seric 			{
84068481Seric 				i++;
84168481Seric 				continue;
84268481Seric 			}
84368481Seric 			w = &wlist[i];
84468481Seric 			while (++i < wc)
84568481Seric 			{
84668481Seric 				if (wlist[i].w_host == NULL &&
84768481Seric 				    w->w_host == NULL)
84868481Seric 					wlist[i].w_lock = TRUE;
84968481Seric 				else if (wlist[i].w_host != NULL &&
85068481Seric 					 w->w_host != NULL &&
85168481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
85268481Seric 					wlist[i].w_lock = TRUE;
85368481Seric 				else
85468481Seric 					break;
85568481Seric 			}
85668481Seric 		}
85768481Seric 
85868481Seric 		/*
85968481Seric 		**  Sort the work directory for the second time,
86068481Seric 		**  based on lock status, host name, and priority.
86168481Seric 		*/
86268481Seric 
86368481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
86468481Seric 	}
86568481Seric 	else
86668481Seric 	{
86768481Seric 		extern workcmpf0();
86868481Seric 
86968481Seric 		/*
87068481Seric 		**  Simple sort based on queue priority only.
87168481Seric 		*/
87268481Seric 
87368481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
87468481Seric 	}
87568481Seric 
8764632Seric 	/*
8774632Seric 	**  Convert the work list into canonical form.
8789377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8794632Seric 	*/
8804632Seric 
88124981Seric 	WorkQ = NULL;
88268481Seric 	for (i = wc; --i >= 0; )
8834632Seric 	{
8844632Seric 		w = (WORK *) xalloc(sizeof *w);
8854632Seric 		w->w_name = wlist[i].w_name;
88668481Seric 		w->w_host = wlist[i].w_host;
88768481Seric 		w->w_lock = wlist[i].w_lock;
8884632Seric 		w->w_pri = wlist[i].w_pri;
88925013Seric 		w->w_ctime = wlist[i].w_ctime;
89024981Seric 		w->w_next = WorkQ;
89124981Seric 		WorkQ = w;
8924632Seric 	}
8934632Seric 
8947677Seric 	if (tTd(40, 1))
8954632Seric 	{
8964632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
8975037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
8984632Seric 	}
89910070Seric 
90010090Seric 	return (wn);
9014632Seric }
9024632Seric /*
90368481Seric **  WORKCMPF0 -- simple priority-only compare function.
9044632Seric **
9054632Seric **	Parameters:
9064632Seric **		a -- the first argument.
9074632Seric **		b -- the second argument.
9084632Seric **
9094632Seric **	Returns:
91024981Seric **		-1 if a < b
91124981Seric **		 0 if a == b
91224981Seric **		+1 if a > b
9134632Seric **
9144632Seric **	Side Effects:
9154632Seric **		none.
9164632Seric */
9174632Seric 
91868481Seric workcmpf0(a, b)
9195037Seric 	register WORK *a;
9205037Seric 	register WORK *b;
9214632Seric {
92257438Seric 	long pa = a->w_pri;
92357438Seric 	long pb = b->w_pri;
92424941Seric 
92524941Seric 	if (pa == pb)
92668481Seric 		return 0;
92724941Seric 	else if (pa > pb)
92868481Seric 		return 1;
92924981Seric 	else
93068481Seric 		return -1;
9314632Seric }
9324632Seric /*
93368481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
93468481Seric **
93568481Seric **	Sorts on host name, lock status, and priority in that order.
93668481Seric **
93768481Seric **	Parameters:
93868481Seric **		a -- the first argument.
93968481Seric **		b -- the second argument.
94068481Seric **
94168481Seric **	Returns:
94268481Seric **		<0 if a < b
94368481Seric **		 0 if a == b
94468481Seric **		>0 if a > b
94568481Seric **
94668481Seric **	Side Effects:
94768481Seric **		none.
94868481Seric */
94968481Seric 
95068481Seric workcmpf1(a, b)
95168481Seric 	register WORK *a;
95268481Seric 	register WORK *b;
95368481Seric {
95468481Seric 	int i;
95568481Seric 
95668481Seric 	/* host name */
95768481Seric 	if (a->w_host != NULL && b->w_host == NULL)
95868481Seric 		return 1;
95968481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
96068481Seric 		return -1;
96168481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
96268481Seric 	    (i = strcmp(a->w_host, b->w_host)))
96368481Seric 		return i;
96468481Seric 
96568481Seric 	/* lock status */
96668481Seric 	if (a->w_lock != b->w_lock)
96768481Seric 		return b->w_lock - a->w_lock;
96868481Seric 
96968481Seric 	/* job priority */
97068481Seric 	return a->w_pri - b->w_pri;
97168481Seric }
97268481Seric /*
97368481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
97468481Seric **
97568481Seric **	Sorts on lock status, host name, and priority in that order.
97668481Seric **
97768481Seric **	Parameters:
97868481Seric **		a -- the first argument.
97968481Seric **		b -- the second argument.
98068481Seric **
98168481Seric **	Returns:
98268481Seric **		<0 if a < b
98368481Seric **		 0 if a == b
98468481Seric **		>0 if a > b
98568481Seric **
98668481Seric **	Side Effects:
98768481Seric **		none.
98868481Seric */
98968481Seric 
99068481Seric workcmpf2(a, b)
99168481Seric 	register WORK *a;
99268481Seric 	register WORK *b;
99368481Seric {
99468481Seric 	int i;
99568481Seric 
99668481Seric 	/* lock status */
99768481Seric 	if (a->w_lock != b->w_lock)
99868481Seric 		return a->w_lock - b->w_lock;
99968481Seric 
100068481Seric 	/* host name */
100168481Seric 	if (a->w_host != NULL && b->w_host == NULL)
100268481Seric 		return 1;
100368481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
100468481Seric 		return -1;
100568481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
100668481Seric 	    (i = strcmp(a->w_host, b->w_host)))
100768481Seric 		return i;
100868481Seric 
100968481Seric 	/* job priority */
101068481Seric 	return a->w_pri - b->w_pri;
101168481Seric }
101268481Seric /*
10134632Seric **  DOWORK -- do a work request.
10144632Seric **
10154632Seric **	Parameters:
101658884Seric **		id -- the ID of the job to run.
101758884Seric **		forkflag -- if set, run this in background.
101858924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
101958924Seric **			This is used when expanding aliases in the queue.
102061094Seric **			If forkflag is also set, it doesn't wait for the
102161094Seric **			child.
102258884Seric **		e - the envelope in which to run it.
10234632Seric **
10244632Seric **	Returns:
102564296Seric **		process id of process that is running the queue job.
10264632Seric **
10274632Seric **	Side Effects:
10284632Seric **		The work request is satisfied if possible.
10294632Seric */
10304632Seric 
103164296Seric pid_t
103258924Seric dowork(id, forkflag, requeueflag, e)
103358884Seric 	char *id;
103458884Seric 	bool forkflag;
103558924Seric 	bool requeueflag;
103655012Seric 	register ENVELOPE *e;
10374632Seric {
103864296Seric 	register pid_t pid;
103951920Seric 	extern bool readqf();
10404632Seric 
10417677Seric 	if (tTd(40, 1))
104258884Seric 		printf("dowork(%s)\n", id);
10434632Seric 
10444632Seric 	/*
104524941Seric 	**  Fork for work.
104624941Seric 	*/
104724941Seric 
104858884Seric 	if (forkflag)
104924941Seric 	{
105064296Seric 		pid = fork();
105164296Seric 		if (pid < 0)
105224941Seric 		{
105324941Seric 			syserr("dowork: cannot fork");
105464296Seric 			return 0;
105524941Seric 		}
105664839Seric 		else if (pid > 0)
105764839Seric 		{
105864839Seric 			/* parent -- clean out connection cache */
105964839Seric 			mci_flush(FALSE, NULL);
106064839Seric 		}
106124941Seric 	}
106224941Seric 	else
106324941Seric 	{
106464296Seric 		pid = 0;
106524941Seric 	}
106624941Seric 
106764296Seric 	if (pid == 0)
10684632Seric 	{
10694632Seric 		/*
10704632Seric 		**  CHILD
10718148Seric 		**	Lock the control file to avoid duplicate deliveries.
10728148Seric 		**		Then run the file as though we had just read it.
10737350Seric 		**	We save an idea of the temporary name so we
10747350Seric 		**		can recover on interrupt.
10754632Seric 		*/
10764632Seric 
10777763Seric 		/* set basic modes, etc. */
10787356Seric 		(void) alarm(0);
107955012Seric 		clearenvelope(e, FALSE);
108064554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
108158734Seric 		e->e_errormode = EM_MAIL;
108258884Seric 		e->e_id = id;
108364765Seric 		GrabTo = UseErrorsTo = FALSE;
108466316Seric 		ExitStat = EX_OK;
108563846Seric 		if (forkflag)
108664554Seric 		{
108764296Seric 			disconnect(1, e);
108864554Seric 			OpMode = MD_DELIVER;
108964554Seric 		}
10907876Seric # ifdef LOG
109158020Seric 		if (LogLevel > 76)
109255012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
10937881Seric 			       getpid());
109456795Seric # endif /* LOG */
10957763Seric 
10967763Seric 		/* don't use the headers from sendmail.cf... */
109755012Seric 		e->e_header = NULL;
10987763Seric 
109951920Seric 		/* read the queue control file -- return if locked */
110065200Seric 		if (!readqf(e))
11016980Seric 		{
110258884Seric 			if (tTd(40, 4))
110358884Seric 				printf("readqf(%s) failed\n", e->e_id);
110458884Seric 			if (forkflag)
110524941Seric 				exit(EX_OK);
110624941Seric 			else
110766316Seric 				return 0;
11086980Seric 		}
11096980Seric 
111055012Seric 		e->e_flags |= EF_INQUEUE;
11116980Seric 
111268481Seric 		/* if this has been tried recently, let it be */
111368481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
111468481Seric 		{
111568481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
111658924Seric 
111768481Seric 			e->e_flags |= EF_KEEPQUEUE;
111868481Seric 			if (Verbose || tTd(40, 8))
111968481Seric 				printf("%s: too young (%s)\n",
112068481Seric 					e->e_id, howlong);
112168481Seric #ifdef LOG
112268481Seric 			if (LogLevel > 19)
112368481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
112468481Seric 					e->e_id, howlong);
112568481Seric #endif
112668481Seric 		}
112768481Seric 		else
112868481Seric 		{
112968481Seric 			eatheader(e, requeueflag);
11306980Seric 
113168481Seric 			if (requeueflag)
113268481Seric 				queueup(e, TRUE, FALSE);
113368481Seric 
113468481Seric 			/* do the delivery */
113568481Seric 			sendall(e, SM_DELIVER);
113668481Seric 		}
113768481Seric 
11386980Seric 		/* finish up and exit */
113958884Seric 		if (forkflag)
114024941Seric 			finis();
114124941Seric 		else
114255012Seric 			dropenvelope(e);
11434632Seric 	}
114464296Seric 	e->e_id = NULL;
114564296Seric 	return pid;
11464632Seric }
11474632Seric /*
11484632Seric **  READQF -- read queue file and set up environment.
11494632Seric **
11504632Seric **	Parameters:
11519377Seric **		e -- the envelope of the job to run.
11524632Seric **
11534632Seric **	Returns:
115451920Seric **		TRUE if it successfully read the queue file.
115551920Seric **		FALSE otherwise.
11564632Seric **
11574632Seric **	Side Effects:
115851920Seric **		The queue file is returned locked.
11594632Seric */
11604632Seric 
116151920Seric bool
116265200Seric readqf(e)
11639377Seric 	register ENVELOPE *e;
11644632Seric {
116517477Seric 	register FILE *qfp;
116654974Seric 	ADDRESS *ctladdr;
116756400Seric 	struct stat st;
116857135Seric 	char *bp;
116968481Seric 	int qfver = 0;
117068481Seric 	register char *p;
117168481Seric 	char *orcpt = NULL;
117258915Seric 	char qf[20];
117357135Seric 	char buf[MAXLINE];
117424954Seric 	extern long atol();
117554974Seric 	extern ADDRESS *setctluser();
117668490Seric 	extern void loseqfile();
11774632Seric 
11784632Seric 	/*
117917468Seric 	**  Read and process the file.
11804632Seric 	*/
11814632Seric 
118258915Seric 	strcpy(qf, queuename(e, 'q'));
118351937Seric 	qfp = fopen(qf, "r+");
118417477Seric 	if (qfp == NULL)
118517477Seric 	{
118658884Seric 		if (tTd(40, 8))
118758884Seric 			printf("readqf(%s): fopen failure (%s)\n",
118858884Seric 				qf, errstring(errno));
118940934Srick 		if (errno != ENOENT)
119040934Srick 			syserr("readqf: no control file %s", qf);
119151920Seric 		return FALSE;
119217477Seric 	}
119340934Srick 
119464335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
119564277Seric 	{
119664277Seric 		/* being processed by another queuer */
119768481Seric 		if (Verbose || tTd(40, 8))
119864277Seric 			printf("%s: locked\n", e->e_id);
119964277Seric # ifdef LOG
120064277Seric 		if (LogLevel > 19)
120164277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
120264277Seric # endif /* LOG */
120364277Seric 		(void) fclose(qfp);
120464277Seric 		return FALSE;
120564277Seric 	}
120664277Seric 
120756400Seric 	/*
120856400Seric 	**  Check the queue file for plausibility to avoid attacks.
120956400Seric 	*/
121056400Seric 
121156400Seric 	if (fstat(fileno(qfp), &st) < 0)
121256400Seric 	{
121356400Seric 		/* must have been being processed by someone else */
121458884Seric 		if (tTd(40, 8))
121558884Seric 			printf("readqf(%s): fstat failure (%s)\n",
121658884Seric 				qf, errstring(errno));
121756400Seric 		fclose(qfp);
121856400Seric 		return FALSE;
121956400Seric 	}
122056400Seric 
122168563Seric 	if (st.st_uid != geteuid() || bitset(S_IWOTH|S_IWGRP, st.st_mode))
122256400Seric 	{
122356400Seric # ifdef LOG
122456400Seric 		if (LogLevel > 0)
122556400Seric 		{
122656400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
122756400Seric 				e->e_id, st.st_uid, st.st_mode);
122856400Seric 		}
122956795Seric # endif /* LOG */
123058884Seric 		if (tTd(40, 8))
123158884Seric 			printf("readqf(%s): bogus file\n", qf);
123268490Seric 		loseqfile(e, "bogus file uid in mqueue");
123356400Seric 		fclose(qfp);
123456400Seric 		return FALSE;
123556400Seric 	}
123656400Seric 
123764277Seric 	if (st.st_size == 0)
123840934Srick 	{
123964277Seric 		/* must be a bogus file -- just remove it */
124064277Seric 		(void) unlink(qf);
124164277Seric 		fclose(qfp);
124251920Seric 		return FALSE;
124340934Srick 	}
124440934Srick 
124564277Seric 	if (st.st_nlink == 0)
124659101Seric 	{
124764277Seric 		/*
124864277Seric 		**  Race condition -- we got a file just as it was being
124964277Seric 		**  unlinked.  Just assume it is zero length.
125064277Seric 		*/
125164277Seric 
125259101Seric 		fclose(qfp);
125359101Seric 		return FALSE;
125459101Seric 	}
125559101Seric 
125664277Seric 	/* good file -- save this lock */
125751920Seric 	e->e_lockfp = qfp;
125851920Seric 
125940934Srick 	/* do basic system initialization */
126055012Seric 	initsys(e);
126165164Seric 	define('i', e->e_id, e);
126240934Srick 
12639377Seric 	LineNumber = 0;
126463850Seric 	e->e_flags |= EF_GLOBALERRS;
126563850Seric 	OpMode = MD_DELIVER;
126654974Seric 	ctladdr = NULL;
126768481Seric 	e->e_dfino = -1;
126868564Seric 	e->e_msgsize = -1;
126957135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12704632Seric 	{
127158737Seric 		register char *p;
127268481Seric 		u_long qflags;
127368481Seric 		ADDRESS *q;
127457529Seric 
127526504Seric 		if (tTd(40, 4))
127657135Seric 			printf("+++++ %s\n", bp);
127757135Seric 		switch (bp[0])
12784632Seric 		{
127968481Seric 		  case 'V':		/* queue file version number */
128068481Seric 			qfver = atoi(&bp[1]);
128168481Seric 			if (qfver > QF_VERSION)
128268481Seric 			{
128368481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
128468481Seric 					qfver, QF_VERSION);
128568481Seric 			}
128668481Seric 			break;
128768481Seric 
128840973Sbostic 		  case 'C':		/* specify controlling user */
128957135Seric 			ctladdr = setctluser(&bp[1]);
129040973Sbostic 			break;
129140973Sbostic 
129268481Seric 		  case 'Q':		/* original recipient */
129368481Seric 			orcpt = newstr(&bp[1]);
129468481Seric 			break;
129568481Seric 
12964632Seric 		  case 'R':		/* specify recipient */
129768481Seric 			p = bp;
129868481Seric 			qflags = 0;
129968481Seric 			if (qfver >= 1)
130068481Seric 			{
130168481Seric 				/* get flag bits */
130268481Seric 				while (*++p != '\0' && *p != ':')
130368481Seric 				{
130468481Seric 					switch (*p)
130568481Seric 					{
130668481Seric 					  case 'S':
130768481Seric 						qflags |= QPINGONSUCCESS;
130868481Seric 						break;
130968481Seric 
131068481Seric 					  case 'F':
131168481Seric 						qflags |= QPINGONFAILURE;
131268481Seric 						break;
131368481Seric 
131468481Seric 					  case 'D':
131568481Seric 						qflags |= QPINGONDELAY;
131668481Seric 						break;
131768481Seric 
131868481Seric 					  case 'P':
131968481Seric 						qflags |= QPRIMARY;
132068481Seric 						break;
132168481Seric 					}
132268481Seric 				}
132368481Seric 			}
132468481Seric 			else
132568481Seric 				qflags |= QPRIMARY;
132668481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
132768481Seric 			if (q != NULL)
132868481Seric 			{
132968481Seric 				q->q_alias = ctladdr;
133068481Seric 				q->q_flags |= qflags;
133168481Seric 				q->q_orcpt = orcpt;
133268481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
133368481Seric 			}
133468481Seric 			orcpt = NULL;
13354632Seric 			break;
13364632Seric 
133725687Seric 		  case 'E':		/* specify error recipient */
133868558Seric 			/* no longer used */
133925687Seric 			break;
134025687Seric 
13414632Seric 		  case 'H':		/* header */
134257135Seric 			(void) chompheader(&bp[1], FALSE, e);
13434632Seric 			break;
13444632Seric 
134510108Seric 		  case 'M':		/* message */
134664705Seric 			/* ignore this; we want a new message next time */
134710108Seric 			break;
134810108Seric 
13494632Seric 		  case 'S':		/* sender */
135058704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13514632Seric 			break;
13524632Seric 
135359093Seric 		  case 'B':		/* body type */
135459093Seric 			e->e_bodytype = newstr(&bp[1]);
135559093Seric 			break;
135659093Seric 
13574632Seric 		  case 'D':		/* data file name */
135868564Seric 			/* obsolete -- ignore */
13594632Seric 			break;
13604632Seric 
13617860Seric 		  case 'T':		/* init time */
136257135Seric 			e->e_ctime = atol(&bp[1]);
13634632Seric 			break;
13644632Seric 
136568481Seric 		  case 'I':		/* data file's inode number */
136668481Seric 			if (e->e_dfino == -1)
136768481Seric 				e->e_dfino = atol(&buf[1]);
136868481Seric 			break;
136968481Seric 
137068481Seric 		  case 'K':		/* time of last deliver attempt */
137168481Seric 			e->e_dtime = atol(&buf[1]);
137268481Seric 			break;
137368481Seric 
137468481Seric 		  case 'N':		/* number of delivery attempts */
137568481Seric 			e->e_ntries = atoi(&buf[1]);
137668481Seric 			break;
137768481Seric 
13784634Seric 		  case 'P':		/* message priority */
137957135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
13804634Seric 			break;
13814634Seric 
138258737Seric 		  case 'F':		/* flag bits */
138358737Seric 			for (p = &bp[1]; *p != '\0'; p++)
138458737Seric 			{
138558737Seric 				switch (*p)
138658737Seric 				{
138758737Seric 				  case 'w':	/* warning sent */
138858737Seric 					e->e_flags |= EF_WARNING;
138958737Seric 					break;
139058737Seric 
139158737Seric 				  case 'r':	/* response */
139258737Seric 					e->e_flags |= EF_RESPONSE;
139358737Seric 					break;
139468481Seric 
139568481Seric 				  case '8':	/* has 8 bit data */
139668481Seric 					e->e_flags |= EF_HAS8BIT;
139768481Seric 					break;
139858737Seric 				}
139958737Seric 			}
140058737Seric 			break;
140158737Seric 
140268481Seric 		  case 'Z':		/* original envelope id from ESMTP */
140368481Seric 			e->e_envid = newstr(&bp[1]);
140468481Seric 			break;
140568481Seric 
140653400Seric 		  case '$':		/* define macro */
140757135Seric 			define(bp[1], newstr(&bp[2]), e);
140853400Seric 			break;
140953400Seric 
141024941Seric 		  case '\0':		/* blank line; ignore */
141124941Seric 			break;
141224941Seric 
14134632Seric 		  default:
141465960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
141565200Seric 				qf, LineNumber, bp);
141663753Seric 			fclose(qfp);
141768490Seric 			loseqfile(e, "unrecognized line");
141863753Seric 			return FALSE;
14194632Seric 		}
142057135Seric 
142157135Seric 		if (bp != buf)
142257135Seric 			free(bp);
14234632Seric 	}
14249377Seric 
142524941Seric 	/*
142624941Seric 	**  If we haven't read any lines, this queue file is empty.
142724941Seric 	**  Arrange to remove it without referencing any null pointers.
142824941Seric 	*/
142924941Seric 
143024941Seric 	if (LineNumber == 0)
143124941Seric 	{
143224941Seric 		errno = 0;
143324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
143424941Seric 	}
143568564Seric 	else
143668564Seric 	{
143768564Seric 		/*
143868564Seric 		**  Arrange to read the data file.
143968564Seric 		*/
144068564Seric 
144168564Seric 		p = queuename(e, 'd');
144268564Seric 		e->e_dfp = fopen(p, "r");
144368564Seric 		if (e->e_dfp == NULL)
144468564Seric 		{
144568564Seric 			syserr("readqf: cannot open %s", p);
144668564Seric 		}
144768603Seric 		else
144868564Seric 		{
144968603Seric 			e->e_flags |= EF_HAS_DF;
145068603Seric 			if (fstat(fileno(e->e_dfp), &st) >= 0)
145168603Seric 			{
145268603Seric 				e->e_msgsize = st.st_size;
145368603Seric 				e->e_dfdev = st.st_dev;
145468603Seric 				e->e_dfino = st.st_ino;
145568603Seric 			}
145668564Seric 		}
145768564Seric 	}
145868564Seric 
145951920Seric 	return TRUE;
14604632Seric }
14614632Seric /*
14629630Seric **  PRINTQUEUE -- print out a representation of the mail queue
14639630Seric **
14649630Seric **	Parameters:
14659630Seric **		none.
14669630Seric **
14679630Seric **	Returns:
14689630Seric **		none.
14699630Seric **
14709630Seric **	Side Effects:
14719630Seric **		Prints a listing of the mail queue on the standard output.
14729630Seric */
14735182Seric 
14749630Seric printqueue()
14759630Seric {
14769630Seric 	register WORK *w;
14779630Seric 	FILE *f;
147810070Seric 	int nrequests;
14799630Seric 	char buf[MAXLINE];
14809630Seric 
14819630Seric 	/*
148258250Seric 	**  Check for permission to print the queue
148358250Seric 	*/
148458250Seric 
148564333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
148658250Seric 	{
148758250Seric 		struct stat st;
148858523Seric # ifdef NGROUPS
148958523Seric 		int n;
149063937Seric 		GIDSET_T gidset[NGROUPS];
149158523Seric # endif
149258250Seric 
149358517Seric 		if (stat(QueueDir, &st) < 0)
149458250Seric 		{
149558250Seric 			syserr("Cannot stat %s", QueueDir);
149658250Seric 			return;
149758250Seric 		}
149858523Seric # ifdef NGROUPS
149958523Seric 		n = getgroups(NGROUPS, gidset);
150058523Seric 		while (--n >= 0)
150158523Seric 		{
150258523Seric 			if (gidset[n] == st.st_gid)
150358523Seric 				break;
150458523Seric 		}
150558523Seric 		if (n < 0)
150658523Seric # else
150763787Seric 		if (RealGid != st.st_gid)
150858523Seric # endif
150958250Seric 		{
151058250Seric 			usrerr("510 You are not permitted to see the queue");
151158250Seric 			setstat(EX_NOPERM);
151258250Seric 			return;
151358250Seric 		}
151458250Seric 	}
151558250Seric 
151658250Seric 	/*
15179630Seric 	**  Read and order the queue.
15189630Seric 	*/
15199630Seric 
152024941Seric 	nrequests = orderq(TRUE);
15219630Seric 
15229630Seric 	/*
15239630Seric 	**  Print the work list that we have read.
15249630Seric 	*/
15259630Seric 
15269630Seric 	/* first see if there is anything */
152710070Seric 	if (nrequests <= 0)
15289630Seric 	{
152910070Seric 		printf("Mail queue is empty\n");
15309630Seric 		return;
15319630Seric 	}
15329630Seric 
153351920Seric 	CurrentLA = getla();	/* get load average */
153440934Srick 
153510096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
153625687Seric 	if (nrequests > QUEUESIZE)
153725687Seric 		printf(", only %d printed", QUEUESIZE);
153824979Seric 	if (Verbose)
153958716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
154024979Seric 	else
154158716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
15429630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
15439630Seric 	{
15449630Seric 		struct stat st;
154510070Seric 		auto time_t submittime = 0;
154668565Seric 		long dfsize;
154758737Seric 		int flags = 0;
154868481Seric 		int qfver;
154910108Seric 		char message[MAXLINE];
155068528Seric 		char bodytype[MAXNAME + 1];
15519630Seric 
155264355Seric 		printf("%8s", w->w_name + 2);
155317468Seric 		f = fopen(w->w_name, "r");
155417468Seric 		if (f == NULL)
155517468Seric 		{
155664355Seric 			printf(" (job completed)\n");
155717468Seric 			errno = 0;
155817468Seric 			continue;
155917468Seric 		}
156068565Seric 		w->w_name[0] = 'd';
156168565Seric 		if (stat(w->w_name, &st) >= 0)
156268565Seric 			dfsize = st.st_size;
156368565Seric 		else
156468565Seric 			dfsize = -1;
156568481Seric 		if (w->w_lock)
156610070Seric 			printf("*");
156757438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
156824941Seric 			printf("X");
156910070Seric 		else
157010070Seric 			printf(" ");
157110070Seric 		errno = 0;
157217468Seric 
157359093Seric 		message[0] = bodytype[0] = '\0';
157468481Seric 		qfver = 0;
15759630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
15769630Seric 		{
157753400Seric 			register int i;
157858737Seric 			register char *p;
157953400Seric 
15809630Seric 			fixcrlf(buf, TRUE);
15819630Seric 			switch (buf[0])
15829630Seric 			{
158368481Seric 			  case 'V':	/* queue file version */
158468481Seric 				qfver = atoi(&buf[1]);
158568481Seric 				break;
158668481Seric 
158710108Seric 			  case 'M':	/* error message */
158853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
158958737Seric 					i = sizeof message - 1;
159053400Seric 				bcopy(&buf[1], message, i);
159153400Seric 				message[i] = '\0';
159210108Seric 				break;
159310108Seric 
159459093Seric 			  case 'B':	/* body type */
159559093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
159659093Seric 					i = sizeof bodytype - 1;
159759093Seric 				bcopy(&buf[1], bodytype, i);
159859093Seric 				bodytype[i] = '\0';
159959093Seric 				break;
160059093Seric 
16019630Seric 			  case 'S':	/* sender name */
160224979Seric 				if (Verbose)
160358737Seric 					printf("%8ld %10ld%c%.12s %.38s",
160458737Seric 					    dfsize,
160558737Seric 					    w->w_pri,
160658737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
160758737Seric 					    ctime(&submittime) + 4,
160824979Seric 					    &buf[1]);
160924979Seric 				else
161024979Seric 					printf("%8ld %.16s %.45s", dfsize,
161124979Seric 					    ctime(&submittime), &buf[1]);
161259093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
161359093Seric 				{
161459093Seric 					printf("\n    %10.10s", bodytype);
161559093Seric 					if (message[0] != '\0')
161659093Seric 						printf("   (%.60s)", message);
161759093Seric 				}
16189630Seric 				break;
161951920Seric 
162040973Sbostic 			  case 'C':	/* controlling user */
162154974Seric 				if (Verbose)
162258716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
162358716Seric 						&buf[1]);
162440973Sbostic 				break;
16259630Seric 
16269630Seric 			  case 'R':	/* recipient name */
162768481Seric 				p = &buf[1];
162868481Seric 				if (qfver >= 1)
162968481Seric 				{
163068481Seric 					p = strchr(p, ':');
163168481Seric 					if (p == NULL)
163268481Seric 						break;
163368481Seric 					p++;
163468481Seric 				}
163524979Seric 				if (Verbose)
163668481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
163724979Seric 				else
163868481Seric 					printf("\n\t\t\t\t   %.45s", p);
16399630Seric 				break;
16409630Seric 
16419630Seric 			  case 'T':	/* creation time */
164224941Seric 				submittime = atol(&buf[1]);
16439630Seric 				break;
164410070Seric 
164558737Seric 			  case 'F':	/* flag bits */
164658737Seric 				for (p = &buf[1]; *p != '\0'; p++)
164758737Seric 				{
164858737Seric 					switch (*p)
164958737Seric 					{
165058737Seric 					  case 'w':
165158737Seric 						flags |= EF_WARNING;
165258737Seric 						break;
165358737Seric 					}
165458737Seric 				}
16559630Seric 			}
16569630Seric 		}
165710070Seric 		if (submittime == (time_t) 0)
165810070Seric 			printf(" (no control file)");
16599630Seric 		printf("\n");
166023098Seric 		(void) fclose(f);
16619630Seric 	}
16629630Seric }
16639630Seric 
166456795Seric # endif /* QUEUE */
166517468Seric /*
166617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
166717468Seric **
166817468Seric **	Assigns an id code if one does not already exist.
166917468Seric **	This code is very careful to avoid trashing existing files
167017468Seric **	under any circumstances.
167117468Seric **
167217468Seric **	Parameters:
167317468Seric **		e -- envelope to build it in/from.
167417468Seric **		type -- the file type, used as the first character
167517468Seric **			of the file name.
167617468Seric **
167717468Seric **	Returns:
167817468Seric **		a pointer to the new file name (in a static buffer).
167917468Seric **
168017468Seric **	Side Effects:
168151920Seric **		If no id code is already assigned, queuename will
168251920Seric **		assign an id code, create a qf file, and leave a
168351920Seric **		locked, open-for-write file pointer in the envelope.
168417468Seric */
168517468Seric 
168617468Seric char *
168717468Seric queuename(e, type)
168817468Seric 	register ENVELOPE *e;
168959700Seric 	int type;
169017468Seric {
169117468Seric 	static int pid = -1;
169258741Seric 	static char c0;
169358741Seric 	static char c1;
169458741Seric 	static char c2;
169558716Seric 	time_t now;
169658716Seric 	struct tm *tm;
169768528Seric 	static char buf[MAXNAME + 1];
169817468Seric 
169917468Seric 	if (e->e_id == NULL)
170017468Seric 	{
170117468Seric 		char qf[20];
170217468Seric 
170317468Seric 		/* find a unique id */
170417468Seric 		if (pid != getpid())
170517468Seric 		{
170617468Seric 			/* new process -- start back at "AA" */
170717468Seric 			pid = getpid();
170858716Seric 			now = curtime();
170958716Seric 			tm = localtime(&now);
171058716Seric 			c0 = 'A' + tm->tm_hour;
171117468Seric 			c1 = 'A';
171217468Seric 			c2 = 'A' - 1;
171317468Seric 		}
171458716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
171517468Seric 
171617468Seric 		while (c1 < '~' || c2 < 'Z')
171717468Seric 		{
171817468Seric 			int i;
171917468Seric 
172017468Seric 			if (c2 >= 'Z')
172117468Seric 			{
172217468Seric 				c1++;
172317468Seric 				c2 = 'A' - 1;
172417468Seric 			}
172558716Seric 			qf[3] = c1;
172658716Seric 			qf[4] = ++c2;
172717468Seric 			if (tTd(7, 20))
172840934Srick 				printf("queuename: trying \"%s\"\n", qf);
172917468Seric 
173040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
173151920Seric 			if (i < 0)
173251920Seric 			{
173351920Seric 				if (errno == EEXIST)
173451920Seric 					continue;
173564705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
173664705Seric 					qf, QueueDir, geteuid());
173751920Seric 				exit(EX_UNAVAILABLE);
173851920Seric 			}
173964335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
174051920Seric 			{
174151920Seric 				e->e_lockfp = fdopen(i, "w");
174240934Srick 				break;
174317468Seric 			}
174451920Seric 
174551920Seric 			/* a reader got the file; abandon it and try again */
174651920Seric 			(void) close(i);
174717468Seric 		}
174817468Seric 		if (c1 >= '~' && c2 >= 'Z')
174917468Seric 		{
175064705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
175164705Seric 				qf, QueueDir, geteuid());
175217468Seric 			exit(EX_OSERR);
175317468Seric 		}
175417468Seric 		e->e_id = newstr(&qf[2]);
175517468Seric 		define('i', e->e_id, e);
175617468Seric 		if (tTd(7, 1))
175717468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
175864745Seric 		if (tTd(7, 9))
175964745Seric 		{
176064745Seric 			printf("  lockfd=");
176164745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
176264745Seric 		}
176317468Seric # ifdef LOG
176458020Seric 		if (LogLevel > 93)
176517468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
176656795Seric # endif /* LOG */
176717468Seric 	}
176817468Seric 
176917468Seric 	if (type == '\0')
177017468Seric 		return (NULL);
177117468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
177217468Seric 	if (tTd(7, 2))
177317468Seric 		printf("queuename: %s\n", buf);
177417468Seric 	return (buf);
177517468Seric }
177617468Seric /*
177717468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
177817468Seric **
177917468Seric **	Parameters:
178017468Seric **		e -- the envelope to unlock.
178117468Seric **
178217468Seric **	Returns:
178317468Seric **		none
178417468Seric **
178517468Seric **	Side Effects:
178617468Seric **		unlocks the queue for `e'.
178717468Seric */
178817468Seric 
178917468Seric unlockqueue(e)
179017468Seric 	ENVELOPE *e;
179117468Seric {
179258680Seric 	if (tTd(51, 4))
179358680Seric 		printf("unlockqueue(%s)\n", e->e_id);
179458680Seric 
179551920Seric 	/* if there is a lock file in the envelope, close it */
179651920Seric 	if (e->e_lockfp != NULL)
179758680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
179851920Seric 	e->e_lockfp = NULL;
179951920Seric 
180058728Seric 	/* don't create a queue id if we don't already have one */
180158728Seric 	if (e->e_id == NULL)
180258728Seric 		return;
180358728Seric 
180417468Seric 	/* remove the transcript */
180517468Seric # ifdef LOG
180658020Seric 	if (LogLevel > 87)
180717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
180856795Seric # endif /* LOG */
180958680Seric 	if (!tTd(51, 104))
181017468Seric 		xunlink(queuename(e, 'x'));
181117468Seric 
181217468Seric }
181340973Sbostic /*
181454974Seric **  SETCTLUSER -- create a controlling address
181540973Sbostic **
181654974Seric **	Create a fake "address" given only a local login name; this is
181754974Seric **	used as a "controlling user" for future recipient addresses.
181840973Sbostic **
181940973Sbostic **	Parameters:
182054974Seric **		user -- the user name of the controlling user.
182140973Sbostic **
182240973Sbostic **	Returns:
182354974Seric **		An address descriptor for the controlling user.
182440973Sbostic **
182540973Sbostic **	Side Effects:
182640973Sbostic **		none.
182740973Sbostic */
182840973Sbostic 
182954974Seric ADDRESS *
183054974Seric setctluser(user)
183154974Seric 	char *user;
183240973Sbostic {
183354974Seric 	register ADDRESS *a;
183440973Sbostic 	struct passwd *pw;
183559113Seric 	char *p;
183640973Sbostic 
183740973Sbostic 	/*
183854974Seric 	**  See if this clears our concept of controlling user.
183940973Sbostic 	*/
184040973Sbostic 
184163850Seric 	if (user == NULL || *user == '\0')
184263850Seric 		return NULL;
184340973Sbostic 
184440973Sbostic 	/*
184554974Seric 	**  Set up addr fields for controlling user.
184640973Sbostic 	*/
184740973Sbostic 
184854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
184954974Seric 	bzero((char *) a, sizeof *a);
185059113Seric 
185159113Seric 	p = strchr(user, ':');
185259113Seric 	if (p != NULL)
185359113Seric 		*p++ = '\0';
185468693Seric 	if (*user != '\0' && (pw = sm_getpwnam(user)) != NULL)
185540973Sbostic 	{
185665822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
185765822Seric 			a->q_home = "";
185865822Seric 		else
185965822Seric 			a->q_home = newstr(pw->pw_dir);
186040973Sbostic 		a->q_uid = pw->pw_uid;
186140973Sbostic 		a->q_gid = pw->pw_gid;
186259270Seric 		a->q_flags |= QGOODUID;
186340973Sbostic 	}
186468603Seric 
186568603Seric 	if (*user != '\0')
186668603Seric 		a->q_user = newstr(user);
186768603Seric 	else if (p != NULL)
186868603Seric 		a->q_user = newstr(p);
186940973Sbostic 	else
187057642Seric 		a->q_user = newstr(DefUser);
187140973Sbostic 
187259270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
187356328Seric 	a->q_mailer = LocalMailer;
187459113Seric 	if (p == NULL)
187559113Seric 		a->q_paddr = a->q_user;
187659113Seric 	else
187759113Seric 		a->q_paddr = newstr(p);
187854974Seric 	return a;
187940973Sbostic }
188068490Seric /*
188168490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
188268490Seric **
188368490Seric **	Parameters:
188468490Seric **		e -- the envelope (e->e_id will be used).
188568490Seric **		why -- reported to whomever can hear.
188668490Seric **
188768490Seric **	Returns:
188868490Seric **		none.
188968490Seric */
189068490Seric 
189168490Seric void
189268490Seric loseqfile(e, why)
189368490Seric 	register ENVELOPE *e;
189468490Seric 	char *why;
189568490Seric {
189668563Seric 	char *p;
189768490Seric 	char buf[40];
189868490Seric 
189968490Seric 	if (e == NULL || e->e_id == NULL)
190068490Seric 		return;
190168490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
190268490Seric 		return;
190368490Seric 	strcpy(buf, queuename(e, 'q'));
190468563Seric 	p = queuename(e, 'Q');
190568563Seric 	if (rename(buf, p) < 0)
190668563Seric 		syserr("cannot rename(%s, %s), uid=%d", buf, p, geteuid());
190768490Seric #ifdef LOG
190868563Seric 	else if (LogLevel > 0)
190968563Seric 		syslog(LOG_ALERT, "Losing %s: %s", buf, why);
191068490Seric #endif
191168490Seric }
1912