xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68558)
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*68558Seric static char sccsid[] = "@(#)queue.c	8.69 (Berkeley) 03/21/95 (with queueing)";
1433731Sbostic #else
15*68558Seric static char sccsid[] = "@(#)queue.c	8.69 (Berkeley) 03/21/95 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
3268481Seric 	char		*w_host;	/* name of recipient host */
3368481Seric 	bool		w_lock;		/* is message locked? */
349377Seric 	long		w_pri;		/* priority of message, see below */
3525013Seric 	time_t		w_ctime;	/* creation time of message */
369377Seric 	struct work	*w_next;	/* next in queue */
379377Seric };
389377Seric 
399377Seric typedef struct work	WORK;
409377Seric 
419377Seric WORK	*WorkQ;			/* queue of things to be done */
4268481Seric 
4368481Seric #define QF_VERSION	1	/* version number of this queue format */
449377Seric /*
454632Seric **  QUEUEUP -- queue a message up for future transmission.
464632Seric **
474632Seric **	Parameters:
486980Seric **		e -- the envelope to queue up.
496999Seric **		queueall -- if TRUE, queue all addresses, rather than
506999Seric **			just those with the QQUEUEUP flag set.
519377Seric **		announce -- if TRUE, tell when you are queueing up.
524632Seric **
534632Seric **	Returns:
5451920Seric **		none.
554632Seric **
564632Seric **	Side Effects:
579377Seric **		The current request are saved in a control file.
5851920Seric **		The queue file is left locked.
594632Seric */
604632Seric 
619377Seric queueup(e, queueall, announce)
626980Seric 	register ENVELOPE *e;
636999Seric 	bool queueall;
649377Seric 	bool announce;
654632Seric {
667812Seric 	char *qf;
677812Seric 	register FILE *tfp;
684632Seric 	register HDR *h;
695007Seric 	register ADDRESS *q;
7051920Seric 	int fd;
7151920Seric 	int i;
7251920Seric 	bool newid;
7353400Seric 	register char *p;
7410173Seric 	MAILER nullmailer;
7565870Seric 	MCI mcibuf;
7651920Seric 	char buf[MAXLINE], tf[MAXLINE];
774632Seric 
785037Seric 	/*
7917477Seric 	**  Create control file.
805037Seric 	*/
814632Seric 
8264745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
8364277Seric 
8464277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8551920Seric 	strcpy(tf, queuename(e, 't'));
8651920Seric 	tfp = e->e_lockfp;
8751920Seric 	if (tfp == NULL)
8851920Seric 		newid = FALSE;
8964277Seric 
9064277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
9164745Seric 	if (!newid)
9251835Seric 	{
9351920Seric 		/* get a locked tf file */
9464070Seric 		for (i = 0; i < 128; i++)
9551835Seric 		{
9651920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9751920Seric 			if (fd < 0)
9851835Seric 			{
9964070Seric 				if (errno != EEXIST)
10064070Seric 					break;
10164070Seric #ifdef LOG
10264070Seric 				if (LogLevel > 0 && (i % 32) == 0)
10365090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
10465090Seric 						tf, geteuid(), errstring(errno));
10564070Seric #endif
10641636Srick 			}
10765090Seric 			else
10865090Seric 			{
10965090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
11065090Seric 					break;
11164070Seric #ifdef LOG
11265090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
11365090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11465090Seric 						tf, errstring(errno));
11564070Seric #endif
11665090Seric 				close(fd);
11765090Seric 			}
11858689Seric 
11964070Seric 			if ((i % 32) == 31)
12064070Seric 			{
12164070Seric 				/* save the old temp file away */
12264070Seric 				(void) rename(tf, queuename(e, 'T'));
12364070Seric 			}
12464070Seric 			else
12564070Seric 				sleep(i % 32);
12641636Srick 		}
12764724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12864921Seric 		{
12964921Seric 			printopenfds(TRUE);
13065090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
13165090Seric 				tf, geteuid());
13264921Seric 		}
13351920Seric 	}
1344632Seric 
1357677Seric 	if (tTd(40, 1))
13668481Seric 		printf("\n>>>>> queueing %s%s queueall=%d >>>>>\n", e->e_id,
13768481Seric 			newid ? " (new id)" : "", queueall);
13868481Seric 	if (tTd(40, 32))
13968481Seric 	{
14068481Seric 		printf("  sendq=");
14168481Seric 		printaddr(e->e_sendqueue, TRUE);
14268481Seric 	}
14364745Seric 	if (tTd(40, 9))
14464745Seric 	{
14564745Seric 		printf("  tfp=");
14664745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
14764745Seric 		printf("  lockfp=");
14864745Seric 		if (e->e_lockfp == NULL)
14964745Seric 			printf("NULL\n");
15064745Seric 		else
15164745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
15264745Seric 	}
1534632Seric 
1544632Seric 	/*
1556980Seric 	**  If there is no data file yet, create one.
1566980Seric 	*/
1576980Seric 
1586980Seric 	if (e->e_df == NULL)
1596980Seric 	{
1606980Seric 		register FILE *dfp;
16168481Seric 		struct stat stbuf;
1629389Seric 		extern putbody();
1636980Seric 
16464086Seric 		e->e_df = queuename(e, 'd');
16564086Seric 		e->e_df = newstr(e->e_df);
16666891Seric 		fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
16764724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
16865090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
16965090Seric 				e->e_df, geteuid());
17068481Seric 		if (fstat(fd, &stbuf) < 0)
17168481Seric 			e->e_dfino = -1;
17268481Seric 		else
17368481Seric 		{
17468481Seric 			e->e_dfdev = stbuf.st_dev;
17568481Seric 			e->e_dfino = stbuf.st_ino;
17668481Seric 		}
17765870Seric 		bzero(&mcibuf, sizeof mcibuf);
17865870Seric 		mcibuf.mci_out = dfp;
17965870Seric 		mcibuf.mci_mailer = FileMailer;
18068228Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
18158680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1829389Seric 		e->e_putbody = putbody;
1836980Seric 	}
1846980Seric 
1856980Seric 	/*
1864632Seric 	**  Output future work requests.
18725687Seric 	**	Priority and creation time should be first, since
18825687Seric 	**	they are required by orderq.
1894632Seric 	*/
1904632Seric 
19168481Seric 	/* output queue version number (must be first!) */
19268481Seric 	fprintf(tfp, "V%d\n", QF_VERSION);
19368481Seric 
1949377Seric 	/* output message priority */
1959377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1969377Seric 
1979630Seric 	/* output creation time */
1989630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1999630Seric 
20068481Seric 	/* output last delivery time */
20168481Seric 	fprintf(tfp, "K%ld\n", e->e_dtime);
20268481Seric 
20368481Seric 	/* output number of delivery attempts */
20468481Seric 	fprintf(tfp, "N%d\n", e->e_ntries);
20568481Seric 
20668481Seric 	/* output inode number of data file */
20768481Seric 	/* XXX should probably include device major/minor too */
20868481Seric 	if (e->e_dfino != -1)
20968481Seric 		fprintf(tfp, "I%d/%d/%ld\n",
21068481Seric 			major(e->e_dfdev), minor(e->e_dfdev), e->e_dfino);
21168481Seric 
21259093Seric 	/* output type and name of data file */
21359093Seric 	if (e->e_bodytype != NULL)
21459093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
2157812Seric 	fprintf(tfp, "D%s\n", e->e_df);
2164632Seric 
21710108Seric 	/* message from envelope, if it exists */
21810108Seric 	if (e->e_message != NULL)
21968478Seric 		fprintf(tfp, "M%s\n", denlstring(e->e_message, TRUE, FALSE));
22010108Seric 
22158737Seric 	/* send various flag bits through */
22258737Seric 	p = buf;
22358737Seric 	if (bitset(EF_WARNING, e->e_flags))
22458737Seric 		*p++ = 'w';
22558737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
22658737Seric 		*p++ = 'r';
22768481Seric 	if (bitset(EF_HAS8BIT, e->e_flags))
22868481Seric 		*p++ = '8';
22958737Seric 	*p++ = '\0';
23058737Seric 	if (buf[0] != '\0')
23158737Seric 		fprintf(tfp, "F%s\n", buf);
23258737Seric 
23358957Seric 	/* $r and $s and $_ macro values */
23453400Seric 	if ((p = macvalue('r', e)) != NULL)
23568478Seric 		fprintf(tfp, "$r%s\n", denlstring(p, TRUE, FALSE));
23653400Seric 	if ((p = macvalue('s', e)) != NULL)
23768478Seric 		fprintf(tfp, "$s%s\n", denlstring(p, TRUE, FALSE));
23858957Seric 	if ((p = macvalue('_', e)) != NULL)
23968478Seric 		fprintf(tfp, "$_%s\n", denlstring(p, TRUE, FALSE));
24053400Seric 
2414632Seric 	/* output name of sender */
24268481Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
24368481Seric 		p = e->e_sender;
24468481Seric 	else
24568481Seric 		p = e->e_from.q_paddr;
24668481Seric 	fprintf(tfp, "S%s\n", denlstring(p, TRUE, FALSE));
2474632Seric 
24868481Seric 	/* output ESMTP-supplied "original" information */
24968481Seric 	if (e->e_envid != NULL)
25068481Seric 		fprintf(tfp, "Z%s\n", denlstring(e->e_envid, TRUE, FALSE));
25168481Seric 
252*68558Seric 	/* output list of recipient addresses */
25359670Seric 	printctladdr(NULL, NULL);
2546980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2554632Seric 	{
25658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
25758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2588245Seric 		{
25959113Seric 			printctladdr(q, tfp);
26068481Seric 			if (q->q_orcpt != NULL)
26168481Seric 				fprintf(tfp, "Q%s\n",
26268481Seric 					denlstring(q->q_orcpt, TRUE, FALSE));
26368481Seric 			putc('R', tfp);
26468481Seric 			if (bitset(QPRIMARY, q->q_flags))
26568481Seric 				putc('P', tfp);
26668481Seric 			if (bitset(QPINGONSUCCESS, q->q_flags))
26768481Seric 				putc('S', tfp);
26868481Seric 			if (bitset(QPINGONFAILURE, q->q_flags))
26968481Seric 				putc('F', tfp);
27068481Seric 			if (bitset(QPINGONDELAY, q->q_flags))
27168481Seric 				putc('D', tfp);
27268481Seric 			if (bitset(QHAS_RET_PARAM, q->q_flags))
27368481Seric 			{
27468481Seric 				if (bitset(QRET_HDRS, q->q_flags))
27568481Seric 					putc('N', tfp);
27668481Seric 				else
27768481Seric 					putc('B', tfp);
27868481Seric 			}
27968481Seric 			putc(':', tfp);
28068481Seric 			fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE));
2819377Seric 			if (announce)
2829377Seric 			{
2839377Seric 				e->e_to = q->q_paddr;
28458151Seric 				message("queued");
28558020Seric 				if (LogLevel > 8)
28668481Seric 					logdelivery(NULL, NULL, "queued",
28768481Seric 						    NULL, (time_t) 0, e);
2889377Seric 				e->e_to = NULL;
2899377Seric 			}
2909387Seric 			if (tTd(40, 1))
2919387Seric 			{
2929387Seric 				printf("queueing ");
2939387Seric 				printaddr(q, FALSE);
2949387Seric 			}
2958245Seric 		}
2964632Seric 	}
2974632Seric 
2989377Seric 	/*
2999377Seric 	**  Output headers for this message.
3009377Seric 	**	Expand macros completely here.  Queue run will deal with
3019377Seric 	**	everything as absolute headers.
3029377Seric 	**		All headers that must be relative to the recipient
3039377Seric 	**		can be cracked later.
30410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
30510173Seric 	**	no effect on the addresses as they are output.
3069377Seric 	*/
3079377Seric 
30810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
30958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
31065584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
31110349Seric 	nullmailer.m_eol = "\n";
31265870Seric 	bzero(&mcibuf, sizeof mcibuf);
31365870Seric 	mcibuf.mci_mailer = &nullmailer;
31465870Seric 	mcibuf.mci_out = tfp;
31510173Seric 
31658050Seric 	define('g', "\201f", e);
3176980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3184632Seric 	{
31910686Seric 		extern bool bitzerop();
32010686Seric 
32112015Seric 		/* don't output null headers */
3224632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
3234632Seric 			continue;
32412015Seric 
32512015Seric 		/* don't output resent headers on non-resent messages */
32612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
32712015Seric 			continue;
32812015Seric 
32965267Seric 		/* expand macros; if null, don't output header at all */
33065267Seric 		if (bitset(H_DEFAULT, h->h_flags))
33165267Seric 		{
33268529Seric 			(void) expand(h->h_value, buf, sizeof buf, e);
33365267Seric 			if (buf[0] == '\0')
33465267Seric 				continue;
33565267Seric 		}
33665267Seric 
33712015Seric 		/* output this header */
3387812Seric 		fprintf(tfp, "H");
33912015Seric 
34012015Seric 		/* if conditional, output the set of conditions */
34110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
34210686Seric 		{
34310686Seric 			int j;
34410686Seric 
34523098Seric 			(void) putc('?', tfp);
34610686Seric 			for (j = '\0'; j <= '\177'; j++)
34710686Seric 				if (bitnset(j, h->h_mflags))
34823098Seric 					(void) putc(j, tfp);
34923098Seric 			(void) putc('?', tfp);
35010686Seric 		}
35112015Seric 
35212015Seric 		/* output the header: expand macros, convert addresses */
3537763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3547763Seric 		{
35565267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3567763Seric 		}
3578245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3589348Seric 		{
35958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
36063753Seric 			FILE *savetrace = TrafficLogFile;
36158737Seric 
36263753Seric 			TrafficLogFile = NULL;
36363753Seric 
36458737Seric 			if (bitset(H_FROM, h->h_flags))
36558737Seric 				oldstyle = FALSE;
36658737Seric 
36765870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
36863753Seric 
36963753Seric 			TrafficLogFile = savetrace;
3709348Seric 		}
3717763Seric 		else
3728245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3734632Seric 	}
3744632Seric 
3754632Seric 	/*
3764632Seric 	**  Clean up.
3774632Seric 	*/
3784632Seric 
37964762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
38058732Seric 	{
38158732Seric 		if (newid)
38258732Seric 			syserr("!552 Error writing control file %s", tf);
38358732Seric 		else
38458732Seric 			syserr("!452 Error writing control file %s", tf);
38558732Seric 	}
38658732Seric 
38751920Seric 	if (!newid)
38851920Seric 	{
38964277Seric 		/* rename (locked) tf to be (locked) qf */
39051920Seric 		qf = queuename(e, 'q');
39151920Seric 		if (rename(tf, qf) < 0)
39265090Seric 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
39365090Seric 				tf, qf, e->e_df, geteuid());
39464277Seric 
39564277Seric 		/* close and unlock old (locked) qf */
39651920Seric 		if (e->e_lockfp != NULL)
39758680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
39851920Seric 		e->e_lockfp = tfp;
39951920Seric 	}
40051920Seric 	else
40151920Seric 		qf = tf;
40241636Srick 	errno = 0;
40364745Seric 	e->e_flags |= EF_INQUEUE;
4047391Seric 
4057677Seric # ifdef LOG
4067677Seric 	/* save log info */
40758020Seric 	if (LogLevel > 79)
4087878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
40956795Seric # endif /* LOG */
41064307Seric 
41164307Seric 	if (tTd(40, 1))
41264307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
41351920Seric 	return;
4144632Seric }
41554974Seric 
41654974Seric printctladdr(a, tfp)
41759113Seric 	register ADDRESS *a;
41854974Seric 	FILE *tfp;
41954974Seric {
42059113Seric 	char *uname;
42159113Seric 	register struct passwd *pw;
42259113Seric 	register ADDRESS *q;
42359113Seric 	uid_t uid;
42459113Seric 	static ADDRESS *lastctladdr;
42559113Seric 	static uid_t lastuid;
42654974Seric 
42759113Seric 	/* initialization */
42863850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
42954974Seric 	{
43059670Seric 		if (lastctladdr != NULL && tfp != NULL)
43159113Seric 			fprintf(tfp, "C\n");
43259113Seric 		lastctladdr = NULL;
43359113Seric 		lastuid = 0;
43454974Seric 		return;
43554974Seric 	}
43659113Seric 
43759113Seric 	/* find the active uid */
43859113Seric 	q = getctladdr(a);
43959113Seric 	if (q == NULL)
44059113Seric 		uid = 0;
44154974Seric 	else
44259113Seric 		uid = q->q_uid;
44363850Seric 	a = a->q_alias;
44459113Seric 
44559113Seric 	/* check to see if this is the same as last time */
44659113Seric 	if (lastctladdr != NULL && uid == lastuid &&
44759113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
44859113Seric 		return;
44959113Seric 	lastuid = uid;
45059113Seric 	lastctladdr = a;
45159113Seric 
45259113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
45359270Seric 		uname = "";
45459113Seric 	else
45559113Seric 		uname = pw->pw_name;
45659113Seric 
45768478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
45854974Seric }
4594632Seric /*
4604632Seric **  RUNQUEUE -- run the jobs in the queue.
4614632Seric **
4624632Seric **	Gets the stuff out of the queue in some presumably logical
4634632Seric **	order and processes them.
4644632Seric **
4654632Seric **	Parameters:
46624941Seric **		forkflag -- TRUE if the queue scanning should be done in
46724941Seric **			a child process.  We double-fork so it is not our
46824941Seric **			child and we don't have to clean up after it.
4694632Seric **
4704632Seric **	Returns:
4714632Seric **		none.
4724632Seric **
4734632Seric **	Side Effects:
4744632Seric **		runs things in the mail queue.
4754632Seric */
4764632Seric 
47755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
47855360Seric 
47968481Seric void
48055360Seric runqueue(forkflag)
4814639Seric 	bool forkflag;
4824632Seric {
48355360Seric 	register ENVELOPE *e;
48468481Seric 	int njobs;
48568481Seric 	int sequenceno = 0;
48655360Seric 	extern ENVELOPE BlankEnvelope;
48724953Seric 
4887466Seric 	/*
48924953Seric 	**  If no work will ever be selected, don't even bother reading
49024953Seric 	**  the queue.
49124953Seric 	*/
49224953Seric 
49351920Seric 	CurrentLA = getla();	/* get load average */
49440934Srick 
49558132Seric 	if (shouldqueue(0L, curtime()))
49624953Seric 	{
49724953Seric 		if (Verbose)
49824953Seric 			printf("Skipping queue run -- load average too high\n");
49958107Seric 		if (forkflag && QueueIntvl != 0)
50058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
50155360Seric 		return;
50224953Seric 	}
50324953Seric 
50424953Seric 	/*
5057466Seric 	**  See if we want to go off and do other useful work.
5067466Seric 	*/
5074639Seric 
5084639Seric 	if (forkflag)
5094639Seric 	{
5107943Seric 		int pid;
51165223Seric #ifdef SIGCHLD
51265223Seric 		extern void reapchild();
5137943Seric 
51465223Seric 		(void) setsignal(SIGCHLD, reapchild);
51565223Seric #endif
51665223Seric 
5177943Seric 		pid = dofork();
5187943Seric 		if (pid != 0)
5194639Seric 		{
5207943Seric 			/* parent -- pick up intermediate zombie */
52125184Seric #ifndef SIGCHLD
5229377Seric 			(void) waitfor(pid);
52356795Seric #endif /* SIGCHLD */
5247690Seric 			if (QueueIntvl != 0)
5259348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5264639Seric 			return;
5274639Seric 		}
5287943Seric 		/* child -- double fork */
52925184Seric #ifndef SIGCHLD
5307943Seric 		if (fork() != 0)
5317943Seric 			exit(EX_OK);
53256795Seric #else /* SIGCHLD */
53364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
53456795Seric #endif /* SIGCHLD */
5354639Seric 	}
53624941Seric 
53740934Srick 	setproctitle("running queue: %s", QueueDir);
53824941Seric 
5397876Seric # ifdef LOG
54058020Seric 	if (LogLevel > 69)
54155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
54255360Seric 			QueueDir, getpid(), forkflag);
54356795Seric # endif /* LOG */
5444639Seric 
5457466Seric 	/*
54610205Seric 	**  Release any resources used by the daemon code.
54710205Seric 	*/
54810205Seric 
54910205Seric # ifdef DAEMON
55010205Seric 	clrdaemon();
55156795Seric # endif /* DAEMON */
55210205Seric 
55364658Seric 	/* force it to run expensive jobs */
55464658Seric 	NoConnect = FALSE;
55564658Seric 
55610205Seric 	/*
55755360Seric 	**  Create ourselves an envelope
55855360Seric 	*/
55955360Seric 
56055360Seric 	CurEnv = &QueueEnvelope;
56158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
56255360Seric 	e->e_flags = BlankEnvelope.e_flags;
56355360Seric 
56455360Seric 	/*
56527175Seric 	**  Make sure the alias database is open.
56627175Seric 	*/
56727175Seric 
56860537Seric 	initmaps(FALSE, e);
56927175Seric 
57027175Seric 	/*
5717466Seric 	**  Start making passes through the queue.
5727466Seric 	**	First, read and sort the entire queue.
5737466Seric 	**	Then, process the work in that order.
5747466Seric 	**		But if you take too long, start over.
5757466Seric 	*/
5767466Seric 
5777943Seric 	/* order the existing work requests */
57868481Seric 	njobs = orderq(FALSE);
5797690Seric 
5807943Seric 	/* process them once at a time */
5817943Seric 	while (WorkQ != NULL)
5824639Seric 	{
5837943Seric 		WORK *w = WorkQ;
5847881Seric 
5857943Seric 		WorkQ = WorkQ->w_next;
58658884Seric 
58758884Seric 		/*
58858884Seric 		**  Ignore jobs that are too expensive for the moment.
58958884Seric 		*/
59058884Seric 
59168481Seric 		sequenceno++;
59258884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
59358884Seric 		{
59458884Seric 			if (Verbose)
59568481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
59668481Seric 					w->w_name + 2, sequenceno, njobs);
59758884Seric 		}
59858930Seric 		else
59958930Seric 		{
60064296Seric 			pid_t pid;
60164296Seric 			extern pid_t dowork();
60264296Seric 
60368481Seric 			if (Verbose)
60468481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
60568481Seric 					w->w_name + 2, sequenceno, njobs);
60664296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
60764296Seric 			errno = 0;
60866316Seric 			if (pid != 0)
60966316Seric 				(void) waitfor(pid);
61058930Seric 		}
6117943Seric 		free(w->w_name);
61268481Seric 		if (w->w_host)
61368481Seric 			free(w->w_host);
6147943Seric 		free((char *) w);
6154639Seric 	}
61629866Seric 
61729866Seric 	/* exit without the usual cleanup */
61855467Seric 	e->e_id = NULL;
61955467Seric 	finis();
6204634Seric }
6214634Seric /*
6224632Seric **  ORDERQ -- order the work queue.
6234632Seric **
6244632Seric **	Parameters:
62524941Seric **		doall -- if set, include everything in the queue (even
62624941Seric **			the jobs that cannot be run because the load
62724941Seric **			average is too high).  Otherwise, exclude those
62824941Seric **			jobs.
6294632Seric **
6304632Seric **	Returns:
63110121Seric **		The number of request in the queue (not necessarily
63210121Seric **		the number of requests in WorkQ however).
6334632Seric **
6344632Seric **	Side Effects:
6354632Seric **		Sets WorkQ to the queue of available work, in order.
6364632Seric */
6374632Seric 
63825687Seric # define NEED_P		001
63925687Seric # define NEED_T		002
64058318Seric # define NEED_R		004
64158318Seric # define NEED_S		010
6424632Seric 
64324941Seric orderq(doall)
64424941Seric 	bool doall;
6454632Seric {
64660219Seric 	register struct dirent *d;
6474632Seric 	register WORK *w;
6486625Sglickman 	DIR *f;
6494632Seric 	register int i;
65025687Seric 	WORK wlist[QUEUESIZE+1];
65110070Seric 	int wn = -1;
65268481Seric 	int wc;
6534632Seric 
65458318Seric 	if (tTd(41, 1))
65558318Seric 	{
65658318Seric 		printf("orderq:\n");
65758318Seric 		if (QueueLimitId != NULL)
65858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
65958318Seric 		if (QueueLimitSender != NULL)
66058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
66158318Seric 		if (QueueLimitRecipient != NULL)
66258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
66358318Seric 	}
66458318Seric 
6654632Seric 	/* clear out old WorkQ */
6664632Seric 	for (w = WorkQ; w != NULL; )
6674632Seric 	{
6684632Seric 		register WORK *nw = w->w_next;
6694632Seric 
6704632Seric 		WorkQ = nw;
6714632Seric 		free(w->w_name);
67268481Seric 		if (w->w_host)
67368481Seric 			free(w->w_host);
6744632Seric 		free((char *) w);
6754632Seric 		w = nw;
6764632Seric 	}
6774632Seric 
6784632Seric 	/* open the queue directory */
6798148Seric 	f = opendir(".");
6804632Seric 	if (f == NULL)
6814632Seric 	{
6828148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
68310070Seric 		return (0);
6844632Seric 	}
6854632Seric 
6864632Seric 	/*
6874632Seric 	**  Read the work directory.
6884632Seric 	*/
6894632Seric 
69010070Seric 	while ((d = readdir(f)) != NULL)
6914632Seric 	{
6929377Seric 		FILE *cf;
69364492Seric 		register char *p;
69468528Seric 		char lbuf[MAXNAME + 1];
69558318Seric 		extern bool strcontainedin();
6964632Seric 
6974632Seric 		/* is this an interesting entry? */
6987812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6994632Seric 			continue;
7004632Seric 
70158318Seric 		if (QueueLimitId != NULL &&
70258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
70358318Seric 			continue;
70458318Seric 
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);
72358020Seric #ifdef LOG
72458020Seric 			if (LogLevel > 3)
72559615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
72658020Seric 					d->d_name);
72758020Seric #endif
72868528Seric 			if (strlen(d->d_name) > 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 		}
73558020Seric 
73610070Seric 		/* yes -- open control file (if not too many files) */
73725687Seric 		if (++wn >= QUEUESIZE)
73810070Seric 			continue;
73958318Seric 
7408148Seric 		cf = fopen(d->d_name, "r");
7414632Seric 		if (cf == NULL)
7424632Seric 		{
7437055Seric 			/* this may be some random person sending hir msgs */
7447055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
74510090Seric 			if (tTd(41, 2))
74610090Seric 				printf("orderq: cannot open %s (%d)\n",
74710090Seric 					d->d_name, errno);
7487055Seric 			errno = 0;
74910090Seric 			wn--;
7504632Seric 			continue;
7514632Seric 		}
75225687Seric 		w = &wlist[wn];
75325687Seric 		w->w_name = newstr(d->d_name);
75468481Seric 		w->w_host = NULL;
75568481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7564632Seric 
75725027Seric 		/* make sure jobs in creation don't clog queue */
75825687Seric 		w->w_pri = 0x7fffffff;
75925687Seric 		w->w_ctime = 0;
76025027Seric 
7614632Seric 		/* extract useful information */
76225687Seric 		i = NEED_P | NEED_T;
76358318Seric 		if (QueueLimitSender != NULL)
76458318Seric 			i |= NEED_S;
76568481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
76658318Seric 			i |= NEED_R;
76725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7684632Seric 		{
76924954Seric 			extern long atol();
77058318Seric 			extern bool strcontainedin();
77124954Seric 
77224941Seric 			switch (lbuf[0])
7734632Seric 			{
77424941Seric 			  case 'P':
77525687Seric 				w->w_pri = atol(&lbuf[1]);
77625687Seric 				i &= ~NEED_P;
7774632Seric 				break;
77825013Seric 
77925013Seric 			  case 'T':
78025687Seric 				w->w_ctime = atol(&lbuf[1]);
78125687Seric 				i &= ~NEED_T;
78225013Seric 				break;
78358318Seric 
78458318Seric 			  case 'R':
78568481Seric 				if (w->w_host == NULL &&
78668481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
78768481Seric 					w->w_host = newstr(&p[1]);
78868481Seric 				if (QueueLimitRecipient == NULL ||
78958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
79058318Seric 					i &= ~NEED_R;
79158318Seric 				break;
79258318Seric 
79358318Seric 			  case 'S':
79458318Seric 				if (QueueLimitSender != NULL &&
79558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
79658318Seric 					i &= ~NEED_S;
79758318Seric 				break;
7984632Seric 			}
7994632Seric 		}
8004632Seric 		(void) fclose(cf);
80124953Seric 
80258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
80358318Seric 		    bitset(NEED_R|NEED_S, i))
80424953Seric 		{
80524953Seric 			/* don't even bother sorting this job in */
80668481Seric 			free(w->w_name);
80768481Seric 			if (w->w_host)
80868481Seric 				free(w->w_host);
80924953Seric 			wn--;
81024953Seric 		}
8114632Seric 	}
8126625Sglickman 	(void) closedir(f);
81310090Seric 	wn++;
8144632Seric 
81568481Seric 	wc = min(wn, QUEUESIZE);
8164632Seric 
81768481Seric 	if (QueueSortOrder == QS_BYHOST)
81868481Seric 	{
81968481Seric 		extern workcmpf1();
82068481Seric 		extern workcmpf2();
82167612Seric 
82268481Seric 		/*
82368481Seric 		**  Sort the work directory for the first time,
82468481Seric 		**  based on host name, lock status, and priority.
82568481Seric 		*/
82668481Seric 
82768481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
82868481Seric 
82968481Seric 		/*
83068481Seric 		**  If one message to host is locked, "lock" all messages
83168481Seric 		**  to that host.
83268481Seric 		*/
83368481Seric 
83468481Seric 		i = 0;
83568481Seric 		while (i < wc)
83668481Seric 		{
83768481Seric 			if (!wlist[i].w_lock)
83868481Seric 			{
83968481Seric 				i++;
84068481Seric 				continue;
84168481Seric 			}
84268481Seric 			w = &wlist[i];
84368481Seric 			while (++i < wc)
84468481Seric 			{
84568481Seric 				if (wlist[i].w_host == NULL &&
84668481Seric 				    w->w_host == NULL)
84768481Seric 					wlist[i].w_lock = TRUE;
84868481Seric 				else if (wlist[i].w_host != NULL &&
84968481Seric 					 w->w_host != NULL &&
85068481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
85168481Seric 					wlist[i].w_lock = TRUE;
85268481Seric 				else
85368481Seric 					break;
85468481Seric 			}
85568481Seric 		}
85668481Seric 
85768481Seric 		/*
85868481Seric 		**  Sort the work directory for the second time,
85968481Seric 		**  based on lock status, host name, and priority.
86068481Seric 		*/
86168481Seric 
86268481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
86368481Seric 	}
86468481Seric 	else
86568481Seric 	{
86668481Seric 		extern workcmpf0();
86768481Seric 
86868481Seric 		/*
86968481Seric 		**  Simple sort based on queue priority only.
87068481Seric 		*/
87168481Seric 
87268481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
87368481Seric 	}
87468481Seric 
8754632Seric 	/*
8764632Seric 	**  Convert the work list into canonical form.
8779377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8784632Seric 	*/
8794632Seric 
88024981Seric 	WorkQ = NULL;
88168481Seric 	for (i = wc; --i >= 0; )
8824632Seric 	{
8834632Seric 		w = (WORK *) xalloc(sizeof *w);
8844632Seric 		w->w_name = wlist[i].w_name;
88568481Seric 		w->w_host = wlist[i].w_host;
88668481Seric 		w->w_lock = wlist[i].w_lock;
8874632Seric 		w->w_pri = wlist[i].w_pri;
88825013Seric 		w->w_ctime = wlist[i].w_ctime;
88924981Seric 		w->w_next = WorkQ;
89024981Seric 		WorkQ = w;
8914632Seric 	}
8924632Seric 
8937677Seric 	if (tTd(40, 1))
8944632Seric 	{
8954632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
8965037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
8974632Seric 	}
89810070Seric 
89910090Seric 	return (wn);
9004632Seric }
9014632Seric /*
90268481Seric **  WORKCMPF0 -- simple priority-only compare function.
9034632Seric **
9044632Seric **	Parameters:
9054632Seric **		a -- the first argument.
9064632Seric **		b -- the second argument.
9074632Seric **
9084632Seric **	Returns:
90924981Seric **		-1 if a < b
91024981Seric **		 0 if a == b
91124981Seric **		+1 if a > b
9124632Seric **
9134632Seric **	Side Effects:
9144632Seric **		none.
9154632Seric */
9164632Seric 
91768481Seric workcmpf0(a, b)
9185037Seric 	register WORK *a;
9195037Seric 	register WORK *b;
9204632Seric {
92157438Seric 	long pa = a->w_pri;
92257438Seric 	long pb = b->w_pri;
92324941Seric 
92424941Seric 	if (pa == pb)
92568481Seric 		return 0;
92624941Seric 	else if (pa > pb)
92768481Seric 		return 1;
92824981Seric 	else
92968481Seric 		return -1;
9304632Seric }
9314632Seric /*
93268481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
93368481Seric **
93468481Seric **	Sorts on host name, lock status, and priority in that order.
93568481Seric **
93668481Seric **	Parameters:
93768481Seric **		a -- the first argument.
93868481Seric **		b -- the second argument.
93968481Seric **
94068481Seric **	Returns:
94168481Seric **		<0 if a < b
94268481Seric **		 0 if a == b
94368481Seric **		>0 if a > b
94468481Seric **
94568481Seric **	Side Effects:
94668481Seric **		none.
94768481Seric */
94868481Seric 
94968481Seric workcmpf1(a, b)
95068481Seric 	register WORK *a;
95168481Seric 	register WORK *b;
95268481Seric {
95368481Seric 	int i;
95468481Seric 
95568481Seric 	/* host name */
95668481Seric 	if (a->w_host != NULL && b->w_host == NULL)
95768481Seric 		return 1;
95868481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
95968481Seric 		return -1;
96068481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
96168481Seric 	    (i = strcmp(a->w_host, b->w_host)))
96268481Seric 		return i;
96368481Seric 
96468481Seric 	/* lock status */
96568481Seric 	if (a->w_lock != b->w_lock)
96668481Seric 		return b->w_lock - a->w_lock;
96768481Seric 
96868481Seric 	/* job priority */
96968481Seric 	return a->w_pri - b->w_pri;
97068481Seric }
97168481Seric /*
97268481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
97368481Seric **
97468481Seric **	Sorts on lock status, host name, and priority in that order.
97568481Seric **
97668481Seric **	Parameters:
97768481Seric **		a -- the first argument.
97868481Seric **		b -- the second argument.
97968481Seric **
98068481Seric **	Returns:
98168481Seric **		<0 if a < b
98268481Seric **		 0 if a == b
98368481Seric **		>0 if a > b
98468481Seric **
98568481Seric **	Side Effects:
98668481Seric **		none.
98768481Seric */
98868481Seric 
98968481Seric workcmpf2(a, b)
99068481Seric 	register WORK *a;
99168481Seric 	register WORK *b;
99268481Seric {
99368481Seric 	int i;
99468481Seric 
99568481Seric 	/* lock status */
99668481Seric 	if (a->w_lock != b->w_lock)
99768481Seric 		return a->w_lock - b->w_lock;
99868481Seric 
99968481Seric 	/* host name */
100068481Seric 	if (a->w_host != NULL && b->w_host == NULL)
100168481Seric 		return 1;
100268481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
100368481Seric 		return -1;
100468481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
100568481Seric 	    (i = strcmp(a->w_host, b->w_host)))
100668481Seric 		return i;
100768481Seric 
100868481Seric 	/* job priority */
100968481Seric 	return a->w_pri - b->w_pri;
101068481Seric }
101168481Seric /*
10124632Seric **  DOWORK -- do a work request.
10134632Seric **
10144632Seric **	Parameters:
101558884Seric **		id -- the ID of the job to run.
101658884Seric **		forkflag -- if set, run this in background.
101758924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
101858924Seric **			This is used when expanding aliases in the queue.
101961094Seric **			If forkflag is also set, it doesn't wait for the
102061094Seric **			child.
102158884Seric **		e - the envelope in which to run it.
10224632Seric **
10234632Seric **	Returns:
102464296Seric **		process id of process that is running the queue job.
10254632Seric **
10264632Seric **	Side Effects:
10274632Seric **		The work request is satisfied if possible.
10284632Seric */
10294632Seric 
103064296Seric pid_t
103158924Seric dowork(id, forkflag, requeueflag, e)
103258884Seric 	char *id;
103358884Seric 	bool forkflag;
103458924Seric 	bool requeueflag;
103555012Seric 	register ENVELOPE *e;
10364632Seric {
103764296Seric 	register pid_t pid;
103851920Seric 	extern bool readqf();
10394632Seric 
10407677Seric 	if (tTd(40, 1))
104158884Seric 		printf("dowork(%s)\n", id);
10424632Seric 
10434632Seric 	/*
104424941Seric 	**  Fork for work.
104524941Seric 	*/
104624941Seric 
104758884Seric 	if (forkflag)
104824941Seric 	{
104964296Seric 		pid = fork();
105064296Seric 		if (pid < 0)
105124941Seric 		{
105224941Seric 			syserr("dowork: cannot fork");
105364296Seric 			return 0;
105424941Seric 		}
105564839Seric 		else if (pid > 0)
105664839Seric 		{
105764839Seric 			/* parent -- clean out connection cache */
105864839Seric 			mci_flush(FALSE, NULL);
105964839Seric 		}
106024941Seric 	}
106124941Seric 	else
106224941Seric 	{
106364296Seric 		pid = 0;
106424941Seric 	}
106524941Seric 
106664296Seric 	if (pid == 0)
10674632Seric 	{
10684632Seric 		/*
10694632Seric 		**  CHILD
10708148Seric 		**	Lock the control file to avoid duplicate deliveries.
10718148Seric 		**		Then run the file as though we had just read it.
10727350Seric 		**	We save an idea of the temporary name so we
10737350Seric 		**		can recover on interrupt.
10744632Seric 		*/
10754632Seric 
10767763Seric 		/* set basic modes, etc. */
10777356Seric 		(void) alarm(0);
107855012Seric 		clearenvelope(e, FALSE);
107964554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
108058734Seric 		e->e_errormode = EM_MAIL;
108158884Seric 		e->e_id = id;
108264765Seric 		GrabTo = UseErrorsTo = FALSE;
108366316Seric 		ExitStat = EX_OK;
108463846Seric 		if (forkflag)
108564554Seric 		{
108664296Seric 			disconnect(1, e);
108764554Seric 			OpMode = MD_DELIVER;
108864554Seric 		}
10897876Seric # ifdef LOG
109058020Seric 		if (LogLevel > 76)
109155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
10927881Seric 			       getpid());
109356795Seric # endif /* LOG */
10947763Seric 
10957763Seric 		/* don't use the headers from sendmail.cf... */
109655012Seric 		e->e_header = NULL;
10977763Seric 
109851920Seric 		/* read the queue control file -- return if locked */
109965200Seric 		if (!readqf(e))
11006980Seric 		{
110158884Seric 			if (tTd(40, 4))
110258884Seric 				printf("readqf(%s) failed\n", e->e_id);
110358884Seric 			if (forkflag)
110424941Seric 				exit(EX_OK);
110524941Seric 			else
110666316Seric 				return 0;
11076980Seric 		}
11086980Seric 
110955012Seric 		e->e_flags |= EF_INQUEUE;
11106980Seric 
111168481Seric 		/* if this has been tried recently, let it be */
111268481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
111368481Seric 		{
111468481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
111558924Seric 
111668481Seric 			e->e_flags |= EF_KEEPQUEUE;
111768481Seric 			if (Verbose || tTd(40, 8))
111868481Seric 				printf("%s: too young (%s)\n",
111968481Seric 					e->e_id, howlong);
112068481Seric #ifdef LOG
112168481Seric 			if (LogLevel > 19)
112268481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
112368481Seric 					e->e_id, howlong);
112468481Seric #endif
112568481Seric 		}
112668481Seric 		else
112768481Seric 		{
112868481Seric 			eatheader(e, requeueflag);
11296980Seric 
113068481Seric 			if (requeueflag)
113168481Seric 				queueup(e, TRUE, FALSE);
113268481Seric 
113368481Seric 			/* do the delivery */
113468481Seric 			sendall(e, SM_DELIVER);
113568481Seric 		}
113668481Seric 
11376980Seric 		/* finish up and exit */
113858884Seric 		if (forkflag)
113924941Seric 			finis();
114024941Seric 		else
114155012Seric 			dropenvelope(e);
11424632Seric 	}
114364296Seric 	e->e_id = NULL;
114464296Seric 	return pid;
11454632Seric }
11464632Seric /*
11474632Seric **  READQF -- read queue file and set up environment.
11484632Seric **
11494632Seric **	Parameters:
11509377Seric **		e -- the envelope of the job to run.
11514632Seric **
11524632Seric **	Returns:
115351920Seric **		TRUE if it successfully read the queue file.
115451920Seric **		FALSE otherwise.
11554632Seric **
11564632Seric **	Side Effects:
115751920Seric **		The queue file is returned locked.
11584632Seric */
11594632Seric 
116051920Seric bool
116165200Seric readqf(e)
11629377Seric 	register ENVELOPE *e;
11634632Seric {
116417477Seric 	register FILE *qfp;
116554974Seric 	ADDRESS *ctladdr;
116656400Seric 	struct stat st;
116757135Seric 	char *bp;
116868481Seric 	int qfver = 0;
116968481Seric 	register char *p;
117068481Seric 	char *orcpt = NULL;
117158915Seric 	char qf[20];
117257135Seric 	char buf[MAXLINE];
117324954Seric 	extern long atol();
117454974Seric 	extern ADDRESS *setctluser();
117568490Seric 	extern void loseqfile();
11764632Seric 
11774632Seric 	/*
117817468Seric 	**  Read and process the file.
11794632Seric 	*/
11804632Seric 
118158915Seric 	strcpy(qf, queuename(e, 'q'));
118251937Seric 	qfp = fopen(qf, "r+");
118317477Seric 	if (qfp == NULL)
118417477Seric 	{
118558884Seric 		if (tTd(40, 8))
118658884Seric 			printf("readqf(%s): fopen failure (%s)\n",
118758884Seric 				qf, errstring(errno));
118840934Srick 		if (errno != ENOENT)
118940934Srick 			syserr("readqf: no control file %s", qf);
119051920Seric 		return FALSE;
119117477Seric 	}
119240934Srick 
119364335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
119464277Seric 	{
119564277Seric 		/* being processed by another queuer */
119668481Seric 		if (Verbose || tTd(40, 8))
119764277Seric 			printf("%s: locked\n", e->e_id);
119864277Seric # ifdef LOG
119964277Seric 		if (LogLevel > 19)
120064277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
120164277Seric # endif /* LOG */
120264277Seric 		(void) fclose(qfp);
120364277Seric 		return FALSE;
120464277Seric 	}
120564277Seric 
120656400Seric 	/*
120756400Seric 	**  Check the queue file for plausibility to avoid attacks.
120856400Seric 	*/
120956400Seric 
121056400Seric 	if (fstat(fileno(qfp), &st) < 0)
121156400Seric 	{
121256400Seric 		/* must have been being processed by someone else */
121358884Seric 		if (tTd(40, 8))
121458884Seric 			printf("readqf(%s): fstat failure (%s)\n",
121558884Seric 				qf, errstring(errno));
121656400Seric 		fclose(qfp);
121756400Seric 		return FALSE;
121856400Seric 	}
121956400Seric 
122064137Seric 	if (st.st_uid != geteuid())
122156400Seric 	{
122256400Seric # ifdef LOG
122356400Seric 		if (LogLevel > 0)
122456400Seric 		{
122556400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
122656400Seric 				e->e_id, st.st_uid, st.st_mode);
122756400Seric 		}
122856795Seric # endif /* LOG */
122958884Seric 		if (tTd(40, 8))
123058884Seric 			printf("readqf(%s): bogus file\n", qf);
123168490Seric 		loseqfile(e, "bogus file uid in mqueue");
123256400Seric 		fclose(qfp);
123356400Seric 		return FALSE;
123456400Seric 	}
123556400Seric 
123664277Seric 	if (st.st_size == 0)
123740934Srick 	{
123864277Seric 		/* must be a bogus file -- just remove it */
123964277Seric 		(void) unlink(qf);
124064277Seric 		fclose(qfp);
124151920Seric 		return FALSE;
124240934Srick 	}
124340934Srick 
124464277Seric 	if (st.st_nlink == 0)
124559101Seric 	{
124664277Seric 		/*
124764277Seric 		**  Race condition -- we got a file just as it was being
124864277Seric 		**  unlinked.  Just assume it is zero length.
124964277Seric 		*/
125064277Seric 
125159101Seric 		fclose(qfp);
125259101Seric 		return FALSE;
125359101Seric 	}
125459101Seric 
125564277Seric 	/* good file -- save this lock */
125651920Seric 	e->e_lockfp = qfp;
125751920Seric 
125840934Srick 	/* do basic system initialization */
125955012Seric 	initsys(e);
126065164Seric 	define('i', e->e_id, e);
126140934Srick 
12629377Seric 	LineNumber = 0;
126363850Seric 	e->e_flags |= EF_GLOBALERRS;
126463850Seric 	OpMode = MD_DELIVER;
126554974Seric 	ctladdr = NULL;
126668481Seric 	e->e_dfino = -1;
126757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12684632Seric 	{
126958737Seric 		register char *p;
127057529Seric 		struct stat st;
127168481Seric 		u_long qflags;
127268481Seric 		ADDRESS *q;
127357529Seric 
127426504Seric 		if (tTd(40, 4))
127557135Seric 			printf("+++++ %s\n", bp);
127657135Seric 		switch (bp[0])
12774632Seric 		{
127868481Seric 		  case 'V':		/* queue file version number */
127968481Seric 			qfver = atoi(&bp[1]);
128068481Seric 			if (qfver > QF_VERSION)
128168481Seric 			{
128268481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
128368481Seric 					qfver, QF_VERSION);
128468481Seric 			}
128568481Seric 			break;
128668481Seric 
128740973Sbostic 		  case 'C':		/* specify controlling user */
128857135Seric 			ctladdr = setctluser(&bp[1]);
128940973Sbostic 			break;
129040973Sbostic 
129168481Seric 		  case 'Q':		/* original recipient */
129268481Seric 			orcpt = newstr(&bp[1]);
129368481Seric 			break;
129468481Seric 
12954632Seric 		  case 'R':		/* specify recipient */
129668481Seric 			p = bp;
129768481Seric 			qflags = 0;
129868481Seric 			if (qfver >= 1)
129968481Seric 			{
130068481Seric 				/* get flag bits */
130168481Seric 				while (*++p != '\0' && *p != ':')
130268481Seric 				{
130368481Seric 					switch (*p)
130468481Seric 					{
130568481Seric 					  case 'S':
130668481Seric 						qflags |= QPINGONSUCCESS;
130768481Seric 						break;
130868481Seric 
130968481Seric 					  case 'F':
131068481Seric 						qflags |= QPINGONFAILURE;
131168481Seric 						break;
131268481Seric 
131368481Seric 					  case 'D':
131468481Seric 						qflags |= QPINGONDELAY;
131568481Seric 						break;
131668481Seric 
131768481Seric 					  case 'B':
131868481Seric 						qflags |= QHAS_RET_PARAM;
131968481Seric 						break;
132068481Seric 
132168481Seric 					  case 'N':
132268481Seric 						qflags |= QHAS_RET_PARAM|QRET_HDRS;
132368481Seric 						break;
132468481Seric 
132568481Seric 					  case 'P':
132668481Seric 						qflags |= QPRIMARY;
132768481Seric 						break;
132868481Seric 					}
132968481Seric 				}
133068481Seric 			}
133168481Seric 			else
133268481Seric 				qflags |= QPRIMARY;
133368481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
133468481Seric 			if (q != NULL)
133568481Seric 			{
133668481Seric 				q->q_alias = ctladdr;
133768481Seric 				q->q_flags |= qflags;
133868481Seric 				q->q_orcpt = orcpt;
133968481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
134068481Seric 			}
134168481Seric 			orcpt = NULL;
13424632Seric 			break;
13434632Seric 
134425687Seric 		  case 'E':		/* specify error recipient */
1345*68558Seric 			/* no longer used */
134625687Seric 			break;
134725687Seric 
13484632Seric 		  case 'H':		/* header */
134957135Seric 			(void) chompheader(&bp[1], FALSE, e);
13504632Seric 			break;
13514632Seric 
135210108Seric 		  case 'M':		/* message */
135364705Seric 			/* ignore this; we want a new message next time */
135410108Seric 			break;
135510108Seric 
13564632Seric 		  case 'S':		/* sender */
135758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13584632Seric 			break;
13594632Seric 
136059093Seric 		  case 'B':		/* body type */
136159093Seric 			e->e_bodytype = newstr(&bp[1]);
136259093Seric 			break;
136359093Seric 
13644632Seric 		  case 'D':		/* data file name */
136557135Seric 			e->e_df = newstr(&bp[1]);
13669544Seric 			e->e_dfp = fopen(e->e_df, "r");
13679544Seric 			if (e->e_dfp == NULL)
136858020Seric 			{
13699377Seric 				syserr("readqf: cannot open %s", e->e_df);
137058020Seric 				e->e_msgsize = -1;
137158020Seric 			}
137258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
137368481Seric 			{
137457529Seric 				e->e_msgsize = st.st_size;
137568481Seric 				e->e_dfdev = st.st_dev;
137668481Seric 				e->e_dfino = st.st_ino;
137768481Seric 			}
13784632Seric 			break;
13794632Seric 
13807860Seric 		  case 'T':		/* init time */
138157135Seric 			e->e_ctime = atol(&bp[1]);
13824632Seric 			break;
13834632Seric 
138468481Seric 		  case 'I':		/* data file's inode number */
138568481Seric 			if (e->e_dfino == -1)
138668481Seric 				e->e_dfino = atol(&buf[1]);
138768481Seric 			break;
138868481Seric 
138968481Seric 		  case 'K':		/* time of last deliver attempt */
139068481Seric 			e->e_dtime = atol(&buf[1]);
139168481Seric 			break;
139268481Seric 
139368481Seric 		  case 'N':		/* number of delivery attempts */
139468481Seric 			e->e_ntries = atoi(&buf[1]);
139568481Seric 			break;
139668481Seric 
13974634Seric 		  case 'P':		/* message priority */
139857135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
13994634Seric 			break;
14004634Seric 
140158737Seric 		  case 'F':		/* flag bits */
140258737Seric 			for (p = &bp[1]; *p != '\0'; p++)
140358737Seric 			{
140458737Seric 				switch (*p)
140558737Seric 				{
140658737Seric 				  case 'w':	/* warning sent */
140758737Seric 					e->e_flags |= EF_WARNING;
140858737Seric 					break;
140958737Seric 
141058737Seric 				  case 'r':	/* response */
141158737Seric 					e->e_flags |= EF_RESPONSE;
141258737Seric 					break;
141368481Seric 
141468481Seric 				  case '8':	/* has 8 bit data */
141568481Seric 					e->e_flags |= EF_HAS8BIT;
141668481Seric 					break;
141758737Seric 				}
141858737Seric 			}
141958737Seric 			break;
142058737Seric 
142168481Seric 		  case 'Z':		/* original envelope id from ESMTP */
142268481Seric 			e->e_envid = newstr(&bp[1]);
142368481Seric 			break;
142468481Seric 
142553400Seric 		  case '$':		/* define macro */
142657135Seric 			define(bp[1], newstr(&bp[2]), e);
142753400Seric 			break;
142853400Seric 
142924941Seric 		  case '\0':		/* blank line; ignore */
143024941Seric 			break;
143124941Seric 
14324632Seric 		  default:
143365960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
143465200Seric 				qf, LineNumber, bp);
143563753Seric 			fclose(qfp);
143668490Seric 			loseqfile(e, "unrecognized line");
143763753Seric 			return FALSE;
14384632Seric 		}
143957135Seric 
144057135Seric 		if (bp != buf)
144157135Seric 			free(bp);
14424632Seric 	}
14439377Seric 
144424941Seric 	/*
144524941Seric 	**  If we haven't read any lines, this queue file is empty.
144624941Seric 	**  Arrange to remove it without referencing any null pointers.
144724941Seric 	*/
144824941Seric 
144924941Seric 	if (LineNumber == 0)
145024941Seric 	{
145124941Seric 		errno = 0;
145224941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
145324941Seric 	}
145451920Seric 	return TRUE;
14554632Seric }
14564632Seric /*
14579630Seric **  PRINTQUEUE -- print out a representation of the mail queue
14589630Seric **
14599630Seric **	Parameters:
14609630Seric **		none.
14619630Seric **
14629630Seric **	Returns:
14639630Seric **		none.
14649630Seric **
14659630Seric **	Side Effects:
14669630Seric **		Prints a listing of the mail queue on the standard output.
14679630Seric */
14685182Seric 
14699630Seric printqueue()
14709630Seric {
14719630Seric 	register WORK *w;
14729630Seric 	FILE *f;
147310070Seric 	int nrequests;
14749630Seric 	char buf[MAXLINE];
14759630Seric 
14769630Seric 	/*
147758250Seric 	**  Check for permission to print the queue
147858250Seric 	*/
147958250Seric 
148064333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
148158250Seric 	{
148258250Seric 		struct stat st;
148358523Seric # ifdef NGROUPS
148458523Seric 		int n;
148563937Seric 		GIDSET_T gidset[NGROUPS];
148658523Seric # endif
148758250Seric 
148858517Seric 		if (stat(QueueDir, &st) < 0)
148958250Seric 		{
149058250Seric 			syserr("Cannot stat %s", QueueDir);
149158250Seric 			return;
149258250Seric 		}
149358523Seric # ifdef NGROUPS
149458523Seric 		n = getgroups(NGROUPS, gidset);
149558523Seric 		while (--n >= 0)
149658523Seric 		{
149758523Seric 			if (gidset[n] == st.st_gid)
149858523Seric 				break;
149958523Seric 		}
150058523Seric 		if (n < 0)
150158523Seric # else
150263787Seric 		if (RealGid != st.st_gid)
150358523Seric # endif
150458250Seric 		{
150558250Seric 			usrerr("510 You are not permitted to see the queue");
150658250Seric 			setstat(EX_NOPERM);
150758250Seric 			return;
150858250Seric 		}
150958250Seric 	}
151058250Seric 
151158250Seric 	/*
15129630Seric 	**  Read and order the queue.
15139630Seric 	*/
15149630Seric 
151524941Seric 	nrequests = orderq(TRUE);
15169630Seric 
15179630Seric 	/*
15189630Seric 	**  Print the work list that we have read.
15199630Seric 	*/
15209630Seric 
15219630Seric 	/* first see if there is anything */
152210070Seric 	if (nrequests <= 0)
15239630Seric 	{
152410070Seric 		printf("Mail queue is empty\n");
15259630Seric 		return;
15269630Seric 	}
15279630Seric 
152851920Seric 	CurrentLA = getla();	/* get load average */
152940934Srick 
153010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
153125687Seric 	if (nrequests > QUEUESIZE)
153225687Seric 		printf(", only %d printed", QUEUESIZE);
153324979Seric 	if (Verbose)
153458716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
153524979Seric 	else
153658716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
15379630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
15389630Seric 	{
15399630Seric 		struct stat st;
154010070Seric 		auto time_t submittime = 0;
154110070Seric 		long dfsize = -1;
154258737Seric 		int flags = 0;
154368481Seric 		int qfver;
154410108Seric 		char message[MAXLINE];
154568528Seric 		char bodytype[MAXNAME + 1];
15469630Seric 
154764355Seric 		printf("%8s", w->w_name + 2);
154817468Seric 		f = fopen(w->w_name, "r");
154917468Seric 		if (f == NULL)
155017468Seric 		{
155164355Seric 			printf(" (job completed)\n");
155217468Seric 			errno = 0;
155317468Seric 			continue;
155417468Seric 		}
155568481Seric 		if (w->w_lock)
155610070Seric 			printf("*");
155757438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
155824941Seric 			printf("X");
155910070Seric 		else
156010070Seric 			printf(" ");
156110070Seric 		errno = 0;
156217468Seric 
156359093Seric 		message[0] = bodytype[0] = '\0';
156468481Seric 		qfver = 0;
15659630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
15669630Seric 		{
156753400Seric 			register int i;
156858737Seric 			register char *p;
156953400Seric 
15709630Seric 			fixcrlf(buf, TRUE);
15719630Seric 			switch (buf[0])
15729630Seric 			{
157368481Seric 			  case 'V':	/* queue file version */
157468481Seric 				qfver = atoi(&buf[1]);
157568481Seric 				break;
157668481Seric 
157710108Seric 			  case 'M':	/* error message */
157853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
157958737Seric 					i = sizeof message - 1;
158053400Seric 				bcopy(&buf[1], message, i);
158153400Seric 				message[i] = '\0';
158210108Seric 				break;
158310108Seric 
158459093Seric 			  case 'B':	/* body type */
158559093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
158659093Seric 					i = sizeof bodytype - 1;
158759093Seric 				bcopy(&buf[1], bodytype, i);
158859093Seric 				bodytype[i] = '\0';
158959093Seric 				break;
159059093Seric 
15919630Seric 			  case 'S':	/* sender name */
159224979Seric 				if (Verbose)
159358737Seric 					printf("%8ld %10ld%c%.12s %.38s",
159458737Seric 					    dfsize,
159558737Seric 					    w->w_pri,
159658737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
159758737Seric 					    ctime(&submittime) + 4,
159824979Seric 					    &buf[1]);
159924979Seric 				else
160024979Seric 					printf("%8ld %.16s %.45s", dfsize,
160124979Seric 					    ctime(&submittime), &buf[1]);
160259093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
160359093Seric 				{
160459093Seric 					printf("\n    %10.10s", bodytype);
160559093Seric 					if (message[0] != '\0')
160659093Seric 						printf("   (%.60s)", message);
160759093Seric 				}
16089630Seric 				break;
160951920Seric 
161040973Sbostic 			  case 'C':	/* controlling user */
161154974Seric 				if (Verbose)
161258716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
161358716Seric 						&buf[1]);
161440973Sbostic 				break;
16159630Seric 
16169630Seric 			  case 'R':	/* recipient name */
161768481Seric 				p = &buf[1];
161868481Seric 				if (qfver >= 1)
161968481Seric 				{
162068481Seric 					p = strchr(p, ':');
162168481Seric 					if (p == NULL)
162268481Seric 						break;
162368481Seric 					p++;
162468481Seric 				}
162524979Seric 				if (Verbose)
162668481Seric 					printf("\n\t\t\t\t\t  %.38s", p);
162724979Seric 				else
162868481Seric 					printf("\n\t\t\t\t   %.45s", p);
16299630Seric 				break;
16309630Seric 
16319630Seric 			  case 'T':	/* creation time */
163224941Seric 				submittime = atol(&buf[1]);
16339630Seric 				break;
163410070Seric 
163510070Seric 			  case 'D':	/* data file name */
163610070Seric 				if (stat(&buf[1], &st) >= 0)
163710070Seric 					dfsize = st.st_size;
163810070Seric 				break;
163958737Seric 
164058737Seric 			  case 'F':	/* flag bits */
164158737Seric 				for (p = &buf[1]; *p != '\0'; p++)
164258737Seric 				{
164358737Seric 					switch (*p)
164458737Seric 					{
164558737Seric 					  case 'w':
164658737Seric 						flags |= EF_WARNING;
164758737Seric 						break;
164858737Seric 					}
164958737Seric 				}
16509630Seric 			}
16519630Seric 		}
165210070Seric 		if (submittime == (time_t) 0)
165310070Seric 			printf(" (no control file)");
16549630Seric 		printf("\n");
165523098Seric 		(void) fclose(f);
16569630Seric 	}
16579630Seric }
16589630Seric 
165956795Seric # endif /* QUEUE */
166017468Seric /*
166117468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
166217468Seric **
166317468Seric **	Assigns an id code if one does not already exist.
166417468Seric **	This code is very careful to avoid trashing existing files
166517468Seric **	under any circumstances.
166617468Seric **
166717468Seric **	Parameters:
166817468Seric **		e -- envelope to build it in/from.
166917468Seric **		type -- the file type, used as the first character
167017468Seric **			of the file name.
167117468Seric **
167217468Seric **	Returns:
167317468Seric **		a pointer to the new file name (in a static buffer).
167417468Seric **
167517468Seric **	Side Effects:
167651920Seric **		If no id code is already assigned, queuename will
167751920Seric **		assign an id code, create a qf file, and leave a
167851920Seric **		locked, open-for-write file pointer in the envelope.
167917468Seric */
168017468Seric 
168117468Seric char *
168217468Seric queuename(e, type)
168317468Seric 	register ENVELOPE *e;
168459700Seric 	int type;
168517468Seric {
168617468Seric 	static int pid = -1;
168758741Seric 	static char c0;
168858741Seric 	static char c1;
168958741Seric 	static char c2;
169058716Seric 	time_t now;
169158716Seric 	struct tm *tm;
169268528Seric 	static char buf[MAXNAME + 1];
169317468Seric 
169417468Seric 	if (e->e_id == NULL)
169517468Seric 	{
169617468Seric 		char qf[20];
169717468Seric 
169817468Seric 		/* find a unique id */
169917468Seric 		if (pid != getpid())
170017468Seric 		{
170117468Seric 			/* new process -- start back at "AA" */
170217468Seric 			pid = getpid();
170358716Seric 			now = curtime();
170458716Seric 			tm = localtime(&now);
170558716Seric 			c0 = 'A' + tm->tm_hour;
170617468Seric 			c1 = 'A';
170717468Seric 			c2 = 'A' - 1;
170817468Seric 		}
170958716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
171017468Seric 
171117468Seric 		while (c1 < '~' || c2 < 'Z')
171217468Seric 		{
171317468Seric 			int i;
171417468Seric 
171517468Seric 			if (c2 >= 'Z')
171617468Seric 			{
171717468Seric 				c1++;
171817468Seric 				c2 = 'A' - 1;
171917468Seric 			}
172058716Seric 			qf[3] = c1;
172158716Seric 			qf[4] = ++c2;
172217468Seric 			if (tTd(7, 20))
172340934Srick 				printf("queuename: trying \"%s\"\n", qf);
172417468Seric 
172540934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
172651920Seric 			if (i < 0)
172751920Seric 			{
172851920Seric 				if (errno == EEXIST)
172951920Seric 					continue;
173064705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
173164705Seric 					qf, QueueDir, geteuid());
173251920Seric 				exit(EX_UNAVAILABLE);
173351920Seric 			}
173464335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
173551920Seric 			{
173651920Seric 				e->e_lockfp = fdopen(i, "w");
173740934Srick 				break;
173817468Seric 			}
173951920Seric 
174051920Seric 			/* a reader got the file; abandon it and try again */
174151920Seric 			(void) close(i);
174217468Seric 		}
174317468Seric 		if (c1 >= '~' && c2 >= 'Z')
174417468Seric 		{
174564705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
174664705Seric 				qf, QueueDir, geteuid());
174717468Seric 			exit(EX_OSERR);
174817468Seric 		}
174917468Seric 		e->e_id = newstr(&qf[2]);
175017468Seric 		define('i', e->e_id, e);
175117468Seric 		if (tTd(7, 1))
175217468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
175364745Seric 		if (tTd(7, 9))
175464745Seric 		{
175564745Seric 			printf("  lockfd=");
175664745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
175764745Seric 		}
175817468Seric # ifdef LOG
175958020Seric 		if (LogLevel > 93)
176017468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
176156795Seric # endif /* LOG */
176217468Seric 	}
176317468Seric 
176417468Seric 	if (type == '\0')
176517468Seric 		return (NULL);
176617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
176717468Seric 	if (tTd(7, 2))
176817468Seric 		printf("queuename: %s\n", buf);
176917468Seric 	return (buf);
177017468Seric }
177117468Seric /*
177217468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
177317468Seric **
177417468Seric **	Parameters:
177517468Seric **		e -- the envelope to unlock.
177617468Seric **
177717468Seric **	Returns:
177817468Seric **		none
177917468Seric **
178017468Seric **	Side Effects:
178117468Seric **		unlocks the queue for `e'.
178217468Seric */
178317468Seric 
178417468Seric unlockqueue(e)
178517468Seric 	ENVELOPE *e;
178617468Seric {
178758680Seric 	if (tTd(51, 4))
178858680Seric 		printf("unlockqueue(%s)\n", e->e_id);
178958680Seric 
179051920Seric 	/* if there is a lock file in the envelope, close it */
179151920Seric 	if (e->e_lockfp != NULL)
179258680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
179351920Seric 	e->e_lockfp = NULL;
179451920Seric 
179558728Seric 	/* don't create a queue id if we don't already have one */
179658728Seric 	if (e->e_id == NULL)
179758728Seric 		return;
179858728Seric 
179917468Seric 	/* remove the transcript */
180017468Seric # ifdef LOG
180158020Seric 	if (LogLevel > 87)
180217468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
180356795Seric # endif /* LOG */
180458680Seric 	if (!tTd(51, 104))
180517468Seric 		xunlink(queuename(e, 'x'));
180617468Seric 
180717468Seric }
180840973Sbostic /*
180954974Seric **  SETCTLUSER -- create a controlling address
181040973Sbostic **
181154974Seric **	Create a fake "address" given only a local login name; this is
181254974Seric **	used as a "controlling user" for future recipient addresses.
181340973Sbostic **
181440973Sbostic **	Parameters:
181554974Seric **		user -- the user name of the controlling user.
181640973Sbostic **
181740973Sbostic **	Returns:
181854974Seric **		An address descriptor for the controlling user.
181940973Sbostic **
182040973Sbostic **	Side Effects:
182140973Sbostic **		none.
182240973Sbostic */
182340973Sbostic 
182454974Seric ADDRESS *
182554974Seric setctluser(user)
182654974Seric 	char *user;
182740973Sbostic {
182854974Seric 	register ADDRESS *a;
182940973Sbostic 	struct passwd *pw;
183059113Seric 	char *p;
183140973Sbostic 
183240973Sbostic 	/*
183354974Seric 	**  See if this clears our concept of controlling user.
183440973Sbostic 	*/
183540973Sbostic 
183663850Seric 	if (user == NULL || *user == '\0')
183763850Seric 		return NULL;
183840973Sbostic 
183940973Sbostic 	/*
184054974Seric 	**  Set up addr fields for controlling user.
184140973Sbostic 	*/
184240973Sbostic 
184354974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
184454974Seric 	bzero((char *) a, sizeof *a);
184559113Seric 
184659113Seric 	p = strchr(user, ':');
184759113Seric 	if (p != NULL)
184859113Seric 		*p++ = '\0';
184959270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
185040973Sbostic 	{
185165822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
185265822Seric 			a->q_home = "";
185365822Seric 		else
185465822Seric 			a->q_home = newstr(pw->pw_dir);
185540973Sbostic 		a->q_uid = pw->pw_uid;
185640973Sbostic 		a->q_gid = pw->pw_gid;
185757642Seric 		a->q_user = newstr(user);
185859270Seric 		a->q_flags |= QGOODUID;
185940973Sbostic 	}
186040973Sbostic 	else
186140973Sbostic 	{
186257642Seric 		a->q_user = newstr(DefUser);
186340973Sbostic 	}
186440973Sbostic 
186559270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
186656328Seric 	a->q_mailer = LocalMailer;
186759113Seric 	if (p == NULL)
186859113Seric 		a->q_paddr = a->q_user;
186959113Seric 	else
187059113Seric 		a->q_paddr = newstr(p);
187154974Seric 	return a;
187240973Sbostic }
187368490Seric /*
187468490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
187568490Seric **
187668490Seric **	Parameters:
187768490Seric **		e -- the envelope (e->e_id will be used).
187868490Seric **		why -- reported to whomever can hear.
187968490Seric **
188068490Seric **	Returns:
188168490Seric **		none.
188268490Seric */
188368490Seric 
188468490Seric void
188568490Seric loseqfile(e, why)
188668490Seric 	register ENVELOPE *e;
188768490Seric 	char *why;
188868490Seric {
188968490Seric 	char buf[40];
189068490Seric 
189168490Seric 	if (e == NULL || e->e_id == NULL)
189268490Seric 		return;
189368490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
189468490Seric 		return;
189568490Seric 	strcpy(buf, queuename(e, 'q'));
189668490Seric 	rename(buf, queuename(e, 'Q'));
189768490Seric #ifdef LOG
189868490Seric 	syslog(LOG_ALERT, "Losing %s: %s", buf, why);
189968490Seric #endif
190068490Seric }
1901