xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 68490)
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*68490Seric static char sccsid[] = "@(#)queue.c	8.66 (Berkeley) 03/05/95 (with queueing)";
1433731Sbostic #else
15*68490Seric static char sccsid[] = "@(#)queue.c	8.66 (Berkeley) 03/05/95 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
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 
25255360Seric 	/* output list of error recipients */
25359670Seric 	printctladdr(NULL, NULL);
25455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25555360Seric 	{
25658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
25755360Seric 		{
25859113Seric 			printctladdr(q, tfp);
25968478Seric 			fprintf(tfp, "E%s\n", denlstring(q->q_paddr, TRUE, FALSE));
26055360Seric 		}
26155360Seric 	}
26255360Seric 
2634632Seric 	/* output list of recipient addresses */
2646980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2654632Seric 	{
26658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
26758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2688245Seric 		{
26959113Seric 			printctladdr(q, tfp);
27068481Seric 			if (q->q_orcpt != NULL)
27168481Seric 				fprintf(tfp, "Q%s\n",
27268481Seric 					denlstring(q->q_orcpt, TRUE, FALSE));
27368481Seric 			putc('R', tfp);
27468481Seric 			if (bitset(QPRIMARY, q->q_flags))
27568481Seric 				putc('P', tfp);
27668481Seric 			if (bitset(QPINGONSUCCESS, q->q_flags))
27768481Seric 				putc('S', tfp);
27868481Seric 			if (bitset(QPINGONFAILURE, q->q_flags))
27968481Seric 				putc('F', tfp);
28068481Seric 			if (bitset(QPINGONDELAY, q->q_flags))
28168481Seric 				putc('D', tfp);
28268481Seric 			if (bitset(QHAS_RET_PARAM, q->q_flags))
28368481Seric 			{
28468481Seric 				if (bitset(QRET_HDRS, q->q_flags))
28568481Seric 					putc('N', tfp);
28668481Seric 				else
28768481Seric 					putc('B', tfp);
28868481Seric 			}
28968481Seric 			putc(':', tfp);
29068481Seric 			fprintf(tfp, "%s\n", denlstring(q->q_paddr, TRUE, FALSE));
2919377Seric 			if (announce)
2929377Seric 			{
2939377Seric 				e->e_to = q->q_paddr;
29458151Seric 				message("queued");
29558020Seric 				if (LogLevel > 8)
29668481Seric 					logdelivery(NULL, NULL, "queued",
29768481Seric 						    NULL, (time_t) 0, e);
2989377Seric 				e->e_to = NULL;
2999377Seric 			}
3009387Seric 			if (tTd(40, 1))
3019387Seric 			{
3029387Seric 				printf("queueing ");
3039387Seric 				printaddr(q, FALSE);
3049387Seric 			}
3058245Seric 		}
3064632Seric 	}
3074632Seric 
3089377Seric 	/*
3099377Seric 	**  Output headers for this message.
3109377Seric 	**	Expand macros completely here.  Queue run will deal with
3119377Seric 	**	everything as absolute headers.
3129377Seric 	**		All headers that must be relative to the recipient
3139377Seric 	**		can be cracked later.
31410173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
31510173Seric 	**	no effect on the addresses as they are output.
3169377Seric 	*/
3179377Seric 
31810686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
31958020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
32065584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
32110349Seric 	nullmailer.m_eol = "\n";
32265870Seric 	bzero(&mcibuf, sizeof mcibuf);
32365870Seric 	mcibuf.mci_mailer = &nullmailer;
32465870Seric 	mcibuf.mci_out = tfp;
32510173Seric 
32658050Seric 	define('g', "\201f", e);
3276980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3284632Seric 	{
32910686Seric 		extern bool bitzerop();
33010686Seric 
33112015Seric 		/* don't output null headers */
3324632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
3334632Seric 			continue;
33412015Seric 
33512015Seric 		/* don't output resent headers on non-resent messages */
33612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
33712015Seric 			continue;
33812015Seric 
33965267Seric 		/* expand macros; if null, don't output header at all */
34065267Seric 		if (bitset(H_DEFAULT, h->h_flags))
34165267Seric 		{
34265267Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
34365267Seric 			if (buf[0] == '\0')
34465267Seric 				continue;
34565267Seric 		}
34665267Seric 
34712015Seric 		/* output this header */
3487812Seric 		fprintf(tfp, "H");
34912015Seric 
35012015Seric 		/* if conditional, output the set of conditions */
35110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
35210686Seric 		{
35310686Seric 			int j;
35410686Seric 
35523098Seric 			(void) putc('?', tfp);
35610686Seric 			for (j = '\0'; j <= '\177'; j++)
35710686Seric 				if (bitnset(j, h->h_mflags))
35823098Seric 					(void) putc(j, tfp);
35923098Seric 			(void) putc('?', tfp);
36010686Seric 		}
36112015Seric 
36212015Seric 		/* output the header: expand macros, convert addresses */
3637763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3647763Seric 		{
36565267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3667763Seric 		}
3678245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3689348Seric 		{
36958737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
37063753Seric 			FILE *savetrace = TrafficLogFile;
37158737Seric 
37263753Seric 			TrafficLogFile = NULL;
37363753Seric 
37458737Seric 			if (bitset(H_FROM, h->h_flags))
37558737Seric 				oldstyle = FALSE;
37658737Seric 
37765870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
37863753Seric 
37963753Seric 			TrafficLogFile = savetrace;
3809348Seric 		}
3817763Seric 		else
3828245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3834632Seric 	}
3844632Seric 
3854632Seric 	/*
3864632Seric 	**  Clean up.
3874632Seric 	*/
3884632Seric 
38964762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
39058732Seric 	{
39158732Seric 		if (newid)
39258732Seric 			syserr("!552 Error writing control file %s", tf);
39358732Seric 		else
39458732Seric 			syserr("!452 Error writing control file %s", tf);
39558732Seric 	}
39658732Seric 
39751920Seric 	if (!newid)
39851920Seric 	{
39964277Seric 		/* rename (locked) tf to be (locked) qf */
40051920Seric 		qf = queuename(e, 'q');
40151920Seric 		if (rename(tf, qf) < 0)
40265090Seric 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
40365090Seric 				tf, qf, e->e_df, geteuid());
40464277Seric 
40564277Seric 		/* close and unlock old (locked) qf */
40651920Seric 		if (e->e_lockfp != NULL)
40758680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
40851920Seric 		e->e_lockfp = tfp;
40951920Seric 	}
41051920Seric 	else
41151920Seric 		qf = tf;
41241636Srick 	errno = 0;
41364745Seric 	e->e_flags |= EF_INQUEUE;
4147391Seric 
4157677Seric # ifdef LOG
4167677Seric 	/* save log info */
41758020Seric 	if (LogLevel > 79)
4187878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
41956795Seric # endif /* LOG */
42064307Seric 
42164307Seric 	if (tTd(40, 1))
42264307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
42351920Seric 	return;
4244632Seric }
42554974Seric 
42654974Seric printctladdr(a, tfp)
42759113Seric 	register ADDRESS *a;
42854974Seric 	FILE *tfp;
42954974Seric {
43059113Seric 	char *uname;
43159113Seric 	register struct passwd *pw;
43259113Seric 	register ADDRESS *q;
43359113Seric 	uid_t uid;
43459113Seric 	static ADDRESS *lastctladdr;
43559113Seric 	static uid_t lastuid;
43654974Seric 
43759113Seric 	/* initialization */
43863850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
43954974Seric 	{
44059670Seric 		if (lastctladdr != NULL && tfp != NULL)
44159113Seric 			fprintf(tfp, "C\n");
44259113Seric 		lastctladdr = NULL;
44359113Seric 		lastuid = 0;
44454974Seric 		return;
44554974Seric 	}
44659113Seric 
44759113Seric 	/* find the active uid */
44859113Seric 	q = getctladdr(a);
44959113Seric 	if (q == NULL)
45059113Seric 		uid = 0;
45154974Seric 	else
45259113Seric 		uid = q->q_uid;
45363850Seric 	a = a->q_alias;
45459113Seric 
45559113Seric 	/* check to see if this is the same as last time */
45659113Seric 	if (lastctladdr != NULL && uid == lastuid &&
45759113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
45859113Seric 		return;
45959113Seric 	lastuid = uid;
46059113Seric 	lastctladdr = a;
46159113Seric 
46259113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
46359270Seric 		uname = "";
46459113Seric 	else
46559113Seric 		uname = pw->pw_name;
46659113Seric 
46768478Seric 	fprintf(tfp, "C%s:%s\n", uname, denlstring(a->q_paddr, TRUE, FALSE));
46854974Seric }
4694632Seric /*
4704632Seric **  RUNQUEUE -- run the jobs in the queue.
4714632Seric **
4724632Seric **	Gets the stuff out of the queue in some presumably logical
4734632Seric **	order and processes them.
4744632Seric **
4754632Seric **	Parameters:
47624941Seric **		forkflag -- TRUE if the queue scanning should be done in
47724941Seric **			a child process.  We double-fork so it is not our
47824941Seric **			child and we don't have to clean up after it.
4794632Seric **
4804632Seric **	Returns:
4814632Seric **		none.
4824632Seric **
4834632Seric **	Side Effects:
4844632Seric **		runs things in the mail queue.
4854632Seric */
4864632Seric 
48755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
48855360Seric 
48968481Seric void
49055360Seric runqueue(forkflag)
4914639Seric 	bool forkflag;
4924632Seric {
49355360Seric 	register ENVELOPE *e;
49468481Seric 	int njobs;
49568481Seric 	int sequenceno = 0;
49655360Seric 	extern ENVELOPE BlankEnvelope;
49724953Seric 
4987466Seric 	/*
49924953Seric 	**  If no work will ever be selected, don't even bother reading
50024953Seric 	**  the queue.
50124953Seric 	*/
50224953Seric 
50351920Seric 	CurrentLA = getla();	/* get load average */
50440934Srick 
50558132Seric 	if (shouldqueue(0L, curtime()))
50624953Seric 	{
50724953Seric 		if (Verbose)
50824953Seric 			printf("Skipping queue run -- load average too high\n");
50958107Seric 		if (forkflag && QueueIntvl != 0)
51058107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
51155360Seric 		return;
51224953Seric 	}
51324953Seric 
51424953Seric 	/*
5157466Seric 	**  See if we want to go off and do other useful work.
5167466Seric 	*/
5174639Seric 
5184639Seric 	if (forkflag)
5194639Seric 	{
5207943Seric 		int pid;
52165223Seric #ifdef SIGCHLD
52265223Seric 		extern void reapchild();
5237943Seric 
52465223Seric 		(void) setsignal(SIGCHLD, reapchild);
52565223Seric #endif
52665223Seric 
5277943Seric 		pid = dofork();
5287943Seric 		if (pid != 0)
5294639Seric 		{
5307943Seric 			/* parent -- pick up intermediate zombie */
53125184Seric #ifndef SIGCHLD
5329377Seric 			(void) waitfor(pid);
53356795Seric #endif /* SIGCHLD */
5347690Seric 			if (QueueIntvl != 0)
5359348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
5364639Seric 			return;
5374639Seric 		}
5387943Seric 		/* child -- double fork */
53925184Seric #ifndef SIGCHLD
5407943Seric 		if (fork() != 0)
5417943Seric 			exit(EX_OK);
54256795Seric #else /* SIGCHLD */
54364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
54456795Seric #endif /* SIGCHLD */
5454639Seric 	}
54624941Seric 
54740934Srick 	setproctitle("running queue: %s", QueueDir);
54824941Seric 
5497876Seric # ifdef LOG
55058020Seric 	if (LogLevel > 69)
55155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
55255360Seric 			QueueDir, getpid(), forkflag);
55356795Seric # endif /* LOG */
5544639Seric 
5557466Seric 	/*
55610205Seric 	**  Release any resources used by the daemon code.
55710205Seric 	*/
55810205Seric 
55910205Seric # ifdef DAEMON
56010205Seric 	clrdaemon();
56156795Seric # endif /* DAEMON */
56210205Seric 
56364658Seric 	/* force it to run expensive jobs */
56464658Seric 	NoConnect = FALSE;
56564658Seric 
56610205Seric 	/*
56755360Seric 	**  Create ourselves an envelope
56855360Seric 	*/
56955360Seric 
57055360Seric 	CurEnv = &QueueEnvelope;
57158179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
57255360Seric 	e->e_flags = BlankEnvelope.e_flags;
57355360Seric 
57455360Seric 	/*
57527175Seric 	**  Make sure the alias database is open.
57627175Seric 	*/
57727175Seric 
57860537Seric 	initmaps(FALSE, e);
57927175Seric 
58027175Seric 	/*
5817466Seric 	**  Start making passes through the queue.
5827466Seric 	**	First, read and sort the entire queue.
5837466Seric 	**	Then, process the work in that order.
5847466Seric 	**		But if you take too long, start over.
5857466Seric 	*/
5867466Seric 
5877943Seric 	/* order the existing work requests */
58868481Seric 	njobs = orderq(FALSE);
5897690Seric 
5907943Seric 	/* process them once at a time */
5917943Seric 	while (WorkQ != NULL)
5924639Seric 	{
5937943Seric 		WORK *w = WorkQ;
5947881Seric 
5957943Seric 		WorkQ = WorkQ->w_next;
59658884Seric 
59758884Seric 		/*
59858884Seric 		**  Ignore jobs that are too expensive for the moment.
59958884Seric 		*/
60058884Seric 
60168481Seric 		sequenceno++;
60258884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
60358884Seric 		{
60458884Seric 			if (Verbose)
60568481Seric 				printf("\nSkipping %s (sequence %d of %d)\n",
60668481Seric 					w->w_name + 2, sequenceno, njobs);
60758884Seric 		}
60858930Seric 		else
60958930Seric 		{
61064296Seric 			pid_t pid;
61164296Seric 			extern pid_t dowork();
61264296Seric 
61368481Seric 			if (Verbose)
61468481Seric 				printf("\nRunning %s (sequence %d of %d)\n",
61568481Seric 					w->w_name + 2, sequenceno, njobs);
61664296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
61764296Seric 			errno = 0;
61866316Seric 			if (pid != 0)
61966316Seric 				(void) waitfor(pid);
62058930Seric 		}
6217943Seric 		free(w->w_name);
62268481Seric 		if (w->w_host)
62368481Seric 			free(w->w_host);
6247943Seric 		free((char *) w);
6254639Seric 	}
62629866Seric 
62729866Seric 	/* exit without the usual cleanup */
62855467Seric 	e->e_id = NULL;
62955467Seric 	finis();
6304634Seric }
6314634Seric /*
6324632Seric **  ORDERQ -- order the work queue.
6334632Seric **
6344632Seric **	Parameters:
63524941Seric **		doall -- if set, include everything in the queue (even
63624941Seric **			the jobs that cannot be run because the load
63724941Seric **			average is too high).  Otherwise, exclude those
63824941Seric **			jobs.
6394632Seric **
6404632Seric **	Returns:
64110121Seric **		The number of request in the queue (not necessarily
64210121Seric **		the number of requests in WorkQ however).
6434632Seric **
6444632Seric **	Side Effects:
6454632Seric **		Sets WorkQ to the queue of available work, in order.
6464632Seric */
6474632Seric 
64825687Seric # define NEED_P		001
64925687Seric # define NEED_T		002
65058318Seric # define NEED_R		004
65158318Seric # define NEED_S		010
6524632Seric 
65324941Seric orderq(doall)
65424941Seric 	bool doall;
6554632Seric {
65660219Seric 	register struct dirent *d;
6574632Seric 	register WORK *w;
6586625Sglickman 	DIR *f;
6594632Seric 	register int i;
66025687Seric 	WORK wlist[QUEUESIZE+1];
66110070Seric 	int wn = -1;
66268481Seric 	int wc;
6634632Seric 
66458318Seric 	if (tTd(41, 1))
66558318Seric 	{
66658318Seric 		printf("orderq:\n");
66758318Seric 		if (QueueLimitId != NULL)
66858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
66958318Seric 		if (QueueLimitSender != NULL)
67058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
67158318Seric 		if (QueueLimitRecipient != NULL)
67258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
67358318Seric 	}
67458318Seric 
6754632Seric 	/* clear out old WorkQ */
6764632Seric 	for (w = WorkQ; w != NULL; )
6774632Seric 	{
6784632Seric 		register WORK *nw = w->w_next;
6794632Seric 
6804632Seric 		WorkQ = nw;
6814632Seric 		free(w->w_name);
68268481Seric 		if (w->w_host)
68368481Seric 			free(w->w_host);
6844632Seric 		free((char *) w);
6854632Seric 		w = nw;
6864632Seric 	}
6874632Seric 
6884632Seric 	/* open the queue directory */
6898148Seric 	f = opendir(".");
6904632Seric 	if (f == NULL)
6914632Seric 	{
6928148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
69310070Seric 		return (0);
6944632Seric 	}
6954632Seric 
6964632Seric 	/*
6974632Seric 	**  Read the work directory.
6984632Seric 	*/
6994632Seric 
70010070Seric 	while ((d = readdir(f)) != NULL)
7014632Seric 	{
7029377Seric 		FILE *cf;
70364492Seric 		register char *p;
7044632Seric 		char lbuf[MAXNAME];
70558318Seric 		extern bool strcontainedin();
7064632Seric 
7074632Seric 		/* is this an interesting entry? */
7087812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
7094632Seric 			continue;
7104632Seric 
71158318Seric 		if (QueueLimitId != NULL &&
71258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
71358318Seric 			continue;
71458318Seric 
71558722Seric 		/*
71658722Seric 		**  Check queue name for plausibility.  This handles
71758722Seric 		**  both old and new type ids.
71858722Seric 		*/
71958722Seric 
72064492Seric 		p = d->d_name + 2;
72164492Seric 		if (isupper(p[0]) && isupper(p[2]))
72264492Seric 			p += 3;
72364492Seric 		else if (isupper(p[1]))
72464492Seric 			p += 2;
72564492Seric 		else
72664492Seric 			p = d->d_name;
72764492Seric 		for (i = 0; isdigit(*p); p++)
72864492Seric 			i++;
72964492Seric 		if (i < 5 || *p != '\0')
73058020Seric 		{
73158020Seric 			if (Verbose)
73258020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
73358020Seric #ifdef LOG
73458020Seric 			if (LogLevel > 3)
73559615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
73658020Seric 					d->d_name);
73758020Seric #endif
73858020Seric 			if (strlen(d->d_name) >= MAXNAME)
73958020Seric 				d->d_name[MAXNAME - 1] = '\0';
74058020Seric 			strcpy(lbuf, d->d_name);
74158020Seric 			lbuf[0] = 'Q';
74258020Seric 			(void) rename(d->d_name, lbuf);
74358020Seric 			continue;
74458020Seric 		}
74558020Seric 
74610070Seric 		/* yes -- open control file (if not too many files) */
74725687Seric 		if (++wn >= QUEUESIZE)
74810070Seric 			continue;
74958318Seric 
7508148Seric 		cf = fopen(d->d_name, "r");
7514632Seric 		if (cf == NULL)
7524632Seric 		{
7537055Seric 			/* this may be some random person sending hir msgs */
7547055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
75510090Seric 			if (tTd(41, 2))
75610090Seric 				printf("orderq: cannot open %s (%d)\n",
75710090Seric 					d->d_name, errno);
7587055Seric 			errno = 0;
75910090Seric 			wn--;
7604632Seric 			continue;
7614632Seric 		}
76225687Seric 		w = &wlist[wn];
76325687Seric 		w->w_name = newstr(d->d_name);
76468481Seric 		w->w_host = NULL;
76568481Seric 		w->w_lock = !lockfile(fileno(cf), w->w_name, NULL, LOCK_SH|LOCK_NB);
7664632Seric 
76725027Seric 		/* make sure jobs in creation don't clog queue */
76825687Seric 		w->w_pri = 0x7fffffff;
76925687Seric 		w->w_ctime = 0;
77025027Seric 
7714632Seric 		/* extract useful information */
77225687Seric 		i = NEED_P | NEED_T;
77358318Seric 		if (QueueLimitSender != NULL)
77458318Seric 			i |= NEED_S;
77568481Seric 		if (QueueSortOrder == QS_BYHOST || QueueLimitRecipient != NULL)
77658318Seric 			i |= NEED_R;
77725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7784632Seric 		{
77924954Seric 			extern long atol();
78058318Seric 			extern bool strcontainedin();
78124954Seric 
78224941Seric 			switch (lbuf[0])
7834632Seric 			{
78424941Seric 			  case 'P':
78525687Seric 				w->w_pri = atol(&lbuf[1]);
78625687Seric 				i &= ~NEED_P;
7874632Seric 				break;
78825013Seric 
78925013Seric 			  case 'T':
79025687Seric 				w->w_ctime = atol(&lbuf[1]);
79125687Seric 				i &= ~NEED_T;
79225013Seric 				break;
79358318Seric 
79458318Seric 			  case 'R':
79568481Seric 				if (w->w_host == NULL &&
79668481Seric 				    (p = strrchr(&lbuf[1], '@')) != NULL)
79768481Seric 					w->w_host = newstr(&p[1]);
79868481Seric 				if (QueueLimitRecipient == NULL ||
79958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
80058318Seric 					i &= ~NEED_R;
80158318Seric 				break;
80258318Seric 
80358318Seric 			  case 'S':
80458318Seric 				if (QueueLimitSender != NULL &&
80558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
80658318Seric 					i &= ~NEED_S;
80758318Seric 				break;
8084632Seric 			}
8094632Seric 		}
8104632Seric 		(void) fclose(cf);
81124953Seric 
81258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
81358318Seric 		    bitset(NEED_R|NEED_S, i))
81424953Seric 		{
81524953Seric 			/* don't even bother sorting this job in */
81668481Seric 			free(w->w_name);
81768481Seric 			if (w->w_host)
81868481Seric 				free(w->w_host);
81924953Seric 			wn--;
82024953Seric 		}
8214632Seric 	}
8226625Sglickman 	(void) closedir(f);
82310090Seric 	wn++;
8244632Seric 
82568481Seric 	wc = min(wn, QUEUESIZE);
8264632Seric 
82768481Seric 	if (QueueSortOrder == QS_BYHOST)
82868481Seric 	{
82968481Seric 		extern workcmpf1();
83068481Seric 		extern workcmpf2();
83167612Seric 
83268481Seric 		/*
83368481Seric 		**  Sort the work directory for the first time,
83468481Seric 		**  based on host name, lock status, and priority.
83568481Seric 		*/
83668481Seric 
83768481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf1);
83868481Seric 
83968481Seric 		/*
84068481Seric 		**  If one message to host is locked, "lock" all messages
84168481Seric 		**  to that host.
84268481Seric 		*/
84368481Seric 
84468481Seric 		i = 0;
84568481Seric 		while (i < wc)
84668481Seric 		{
84768481Seric 			if (!wlist[i].w_lock)
84868481Seric 			{
84968481Seric 				i++;
85068481Seric 				continue;
85168481Seric 			}
85268481Seric 			w = &wlist[i];
85368481Seric 			while (++i < wc)
85468481Seric 			{
85568481Seric 				if (wlist[i].w_host == NULL &&
85668481Seric 				    w->w_host == NULL)
85768481Seric 					wlist[i].w_lock = TRUE;
85868481Seric 				else if (wlist[i].w_host != NULL &&
85968481Seric 					 w->w_host != NULL &&
86068481Seric 					 strcmp(wlist[i].w_host, w->w_host) == 0)
86168481Seric 					wlist[i].w_lock = TRUE;
86268481Seric 				else
86368481Seric 					break;
86468481Seric 			}
86568481Seric 		}
86668481Seric 
86768481Seric 		/*
86868481Seric 		**  Sort the work directory for the second time,
86968481Seric 		**  based on lock status, host name, and priority.
87068481Seric 		*/
87168481Seric 
87268481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf2);
87368481Seric 	}
87468481Seric 	else
87568481Seric 	{
87668481Seric 		extern workcmpf0();
87768481Seric 
87868481Seric 		/*
87968481Seric 		**  Simple sort based on queue priority only.
88068481Seric 		*/
88168481Seric 
88268481Seric 		qsort((char *) wlist, wc, sizeof *wlist, workcmpf0);
88368481Seric 	}
88468481Seric 
8854632Seric 	/*
8864632Seric 	**  Convert the work list into canonical form.
8879377Seric 	**	Should be turning it into a list of envelopes here perhaps.
8884632Seric 	*/
8894632Seric 
89024981Seric 	WorkQ = NULL;
89168481Seric 	for (i = wc; --i >= 0; )
8924632Seric 	{
8934632Seric 		w = (WORK *) xalloc(sizeof *w);
8944632Seric 		w->w_name = wlist[i].w_name;
89568481Seric 		w->w_host = wlist[i].w_host;
89668481Seric 		w->w_lock = wlist[i].w_lock;
8974632Seric 		w->w_pri = wlist[i].w_pri;
89825013Seric 		w->w_ctime = wlist[i].w_ctime;
89924981Seric 		w->w_next = WorkQ;
90024981Seric 		WorkQ = w;
9014632Seric 	}
9024632Seric 
9037677Seric 	if (tTd(40, 1))
9044632Seric 	{
9054632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
9065037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
9074632Seric 	}
90810070Seric 
90910090Seric 	return (wn);
9104632Seric }
9114632Seric /*
91268481Seric **  WORKCMPF0 -- simple priority-only compare function.
9134632Seric **
9144632Seric **	Parameters:
9154632Seric **		a -- the first argument.
9164632Seric **		b -- the second argument.
9174632Seric **
9184632Seric **	Returns:
91924981Seric **		-1 if a < b
92024981Seric **		 0 if a == b
92124981Seric **		+1 if a > b
9224632Seric **
9234632Seric **	Side Effects:
9244632Seric **		none.
9254632Seric */
9264632Seric 
92768481Seric workcmpf0(a, b)
9285037Seric 	register WORK *a;
9295037Seric 	register WORK *b;
9304632Seric {
93157438Seric 	long pa = a->w_pri;
93257438Seric 	long pb = b->w_pri;
93324941Seric 
93424941Seric 	if (pa == pb)
93568481Seric 		return 0;
93624941Seric 	else if (pa > pb)
93768481Seric 		return 1;
93824981Seric 	else
93968481Seric 		return -1;
9404632Seric }
9414632Seric /*
94268481Seric **  WORKCMPF1 -- first compare function for ordering work based on host name.
94368481Seric **
94468481Seric **	Sorts on host name, lock status, and priority in that order.
94568481Seric **
94668481Seric **	Parameters:
94768481Seric **		a -- the first argument.
94868481Seric **		b -- the second argument.
94968481Seric **
95068481Seric **	Returns:
95168481Seric **		<0 if a < b
95268481Seric **		 0 if a == b
95368481Seric **		>0 if a > b
95468481Seric **
95568481Seric **	Side Effects:
95668481Seric **		none.
95768481Seric */
95868481Seric 
95968481Seric workcmpf1(a, b)
96068481Seric 	register WORK *a;
96168481Seric 	register WORK *b;
96268481Seric {
96368481Seric 	int i;
96468481Seric 
96568481Seric 	/* host name */
96668481Seric 	if (a->w_host != NULL && b->w_host == NULL)
96768481Seric 		return 1;
96868481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
96968481Seric 		return -1;
97068481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
97168481Seric 	    (i = strcmp(a->w_host, b->w_host)))
97268481Seric 		return i;
97368481Seric 
97468481Seric 	/* lock status */
97568481Seric 	if (a->w_lock != b->w_lock)
97668481Seric 		return b->w_lock - a->w_lock;
97768481Seric 
97868481Seric 	/* job priority */
97968481Seric 	return a->w_pri - b->w_pri;
98068481Seric }
98168481Seric /*
98268481Seric **  WORKCMPF2 -- second compare function for ordering work based on host name.
98368481Seric **
98468481Seric **	Sorts on lock status, host name, and priority in that order.
98568481Seric **
98668481Seric **	Parameters:
98768481Seric **		a -- the first argument.
98868481Seric **		b -- the second argument.
98968481Seric **
99068481Seric **	Returns:
99168481Seric **		<0 if a < b
99268481Seric **		 0 if a == b
99368481Seric **		>0 if a > b
99468481Seric **
99568481Seric **	Side Effects:
99668481Seric **		none.
99768481Seric */
99868481Seric 
99968481Seric workcmpf2(a, b)
100068481Seric 	register WORK *a;
100168481Seric 	register WORK *b;
100268481Seric {
100368481Seric 	int i;
100468481Seric 
100568481Seric 	/* lock status */
100668481Seric 	if (a->w_lock != b->w_lock)
100768481Seric 		return a->w_lock - b->w_lock;
100868481Seric 
100968481Seric 	/* host name */
101068481Seric 	if (a->w_host != NULL && b->w_host == NULL)
101168481Seric 		return 1;
101268481Seric 	else if (a->w_host == NULL && b->w_host != NULL)
101368481Seric 		return -1;
101468481Seric 	if (a->w_host != NULL && b->w_host != NULL &&
101568481Seric 	    (i = strcmp(a->w_host, b->w_host)))
101668481Seric 		return i;
101768481Seric 
101868481Seric 	/* job priority */
101968481Seric 	return a->w_pri - b->w_pri;
102068481Seric }
102168481Seric /*
10224632Seric **  DOWORK -- do a work request.
10234632Seric **
10244632Seric **	Parameters:
102558884Seric **		id -- the ID of the job to run.
102658884Seric **		forkflag -- if set, run this in background.
102758924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
102858924Seric **			This is used when expanding aliases in the queue.
102961094Seric **			If forkflag is also set, it doesn't wait for the
103061094Seric **			child.
103158884Seric **		e - the envelope in which to run it.
10324632Seric **
10334632Seric **	Returns:
103464296Seric **		process id of process that is running the queue job.
10354632Seric **
10364632Seric **	Side Effects:
10374632Seric **		The work request is satisfied if possible.
10384632Seric */
10394632Seric 
104064296Seric pid_t
104158924Seric dowork(id, forkflag, requeueflag, e)
104258884Seric 	char *id;
104358884Seric 	bool forkflag;
104458924Seric 	bool requeueflag;
104555012Seric 	register ENVELOPE *e;
10464632Seric {
104764296Seric 	register pid_t pid;
104851920Seric 	extern bool readqf();
10494632Seric 
10507677Seric 	if (tTd(40, 1))
105158884Seric 		printf("dowork(%s)\n", id);
10524632Seric 
10534632Seric 	/*
105424941Seric 	**  Fork for work.
105524941Seric 	*/
105624941Seric 
105758884Seric 	if (forkflag)
105824941Seric 	{
105964296Seric 		pid = fork();
106064296Seric 		if (pid < 0)
106124941Seric 		{
106224941Seric 			syserr("dowork: cannot fork");
106364296Seric 			return 0;
106424941Seric 		}
106564839Seric 		else if (pid > 0)
106664839Seric 		{
106764839Seric 			/* parent -- clean out connection cache */
106864839Seric 			mci_flush(FALSE, NULL);
106964839Seric 		}
107024941Seric 	}
107124941Seric 	else
107224941Seric 	{
107364296Seric 		pid = 0;
107424941Seric 	}
107524941Seric 
107664296Seric 	if (pid == 0)
10774632Seric 	{
10784632Seric 		/*
10794632Seric 		**  CHILD
10808148Seric 		**	Lock the control file to avoid duplicate deliveries.
10818148Seric 		**		Then run the file as though we had just read it.
10827350Seric 		**	We save an idea of the temporary name so we
10837350Seric 		**		can recover on interrupt.
10844632Seric 		*/
10854632Seric 
10867763Seric 		/* set basic modes, etc. */
10877356Seric 		(void) alarm(0);
108855012Seric 		clearenvelope(e, FALSE);
108964554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
109058734Seric 		e->e_errormode = EM_MAIL;
109158884Seric 		e->e_id = id;
109264765Seric 		GrabTo = UseErrorsTo = FALSE;
109366316Seric 		ExitStat = EX_OK;
109463846Seric 		if (forkflag)
109564554Seric 		{
109664296Seric 			disconnect(1, e);
109764554Seric 			OpMode = MD_DELIVER;
109864554Seric 		}
10997876Seric # ifdef LOG
110058020Seric 		if (LogLevel > 76)
110155012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
11027881Seric 			       getpid());
110356795Seric # endif /* LOG */
11047763Seric 
11057763Seric 		/* don't use the headers from sendmail.cf... */
110655012Seric 		e->e_header = NULL;
11077763Seric 
110851920Seric 		/* read the queue control file -- return if locked */
110965200Seric 		if (!readqf(e))
11106980Seric 		{
111158884Seric 			if (tTd(40, 4))
111258884Seric 				printf("readqf(%s) failed\n", e->e_id);
111358884Seric 			if (forkflag)
111424941Seric 				exit(EX_OK);
111524941Seric 			else
111666316Seric 				return 0;
11176980Seric 		}
11186980Seric 
111955012Seric 		e->e_flags |= EF_INQUEUE;
11206980Seric 
112168481Seric 		/* if this has been tried recently, let it be */
112268481Seric 		if (e->e_ntries > 0 && (curtime() - e->e_dtime) < MinQueueAge)
112368481Seric 		{
112468481Seric 			char *howlong = pintvl(curtime() - e->e_dtime, TRUE);
112558924Seric 
112668481Seric 			e->e_flags |= EF_KEEPQUEUE;
112768481Seric 			if (Verbose || tTd(40, 8))
112868481Seric 				printf("%s: too young (%s)\n",
112968481Seric 					e->e_id, howlong);
113068481Seric #ifdef LOG
113168481Seric 			if (LogLevel > 19)
113268481Seric 				syslog(LOG_DEBUG, "%s: too young (%s)",
113368481Seric 					e->e_id, howlong);
113468481Seric #endif
113568481Seric 		}
113668481Seric 		else
113768481Seric 		{
113868481Seric 			eatheader(e, requeueflag);
11396980Seric 
114068481Seric 			if (requeueflag)
114168481Seric 				queueup(e, TRUE, FALSE);
114268481Seric 
114368481Seric 			/* do the delivery */
114468481Seric 			sendall(e, SM_DELIVER);
114568481Seric 		}
114668481Seric 
11476980Seric 		/* finish up and exit */
114858884Seric 		if (forkflag)
114924941Seric 			finis();
115024941Seric 		else
115155012Seric 			dropenvelope(e);
11524632Seric 	}
115364296Seric 	e->e_id = NULL;
115464296Seric 	return pid;
11554632Seric }
11564632Seric /*
11574632Seric **  READQF -- read queue file and set up environment.
11584632Seric **
11594632Seric **	Parameters:
11609377Seric **		e -- the envelope of the job to run.
11614632Seric **
11624632Seric **	Returns:
116351920Seric **		TRUE if it successfully read the queue file.
116451920Seric **		FALSE otherwise.
11654632Seric **
11664632Seric **	Side Effects:
116751920Seric **		The queue file is returned locked.
11684632Seric */
11694632Seric 
117051920Seric bool
117165200Seric readqf(e)
11729377Seric 	register ENVELOPE *e;
11734632Seric {
117417477Seric 	register FILE *qfp;
117554974Seric 	ADDRESS *ctladdr;
117656400Seric 	struct stat st;
117757135Seric 	char *bp;
117868481Seric 	int qfver = 0;
117968481Seric 	register char *p;
118068481Seric 	char *orcpt = NULL;
118158915Seric 	char qf[20];
118257135Seric 	char buf[MAXLINE];
118324954Seric 	extern long atol();
118454974Seric 	extern ADDRESS *setctluser();
1185*68490Seric 	extern void loseqfile();
11864632Seric 
11874632Seric 	/*
118817468Seric 	**  Read and process the file.
11894632Seric 	*/
11904632Seric 
119158915Seric 	strcpy(qf, queuename(e, 'q'));
119251937Seric 	qfp = fopen(qf, "r+");
119317477Seric 	if (qfp == NULL)
119417477Seric 	{
119558884Seric 		if (tTd(40, 8))
119658884Seric 			printf("readqf(%s): fopen failure (%s)\n",
119758884Seric 				qf, errstring(errno));
119840934Srick 		if (errno != ENOENT)
119940934Srick 			syserr("readqf: no control file %s", qf);
120051920Seric 		return FALSE;
120117477Seric 	}
120240934Srick 
120364335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
120464277Seric 	{
120564277Seric 		/* being processed by another queuer */
120668481Seric 		if (Verbose || tTd(40, 8))
120764277Seric 			printf("%s: locked\n", e->e_id);
120864277Seric # ifdef LOG
120964277Seric 		if (LogLevel > 19)
121064277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
121164277Seric # endif /* LOG */
121264277Seric 		(void) fclose(qfp);
121364277Seric 		return FALSE;
121464277Seric 	}
121564277Seric 
121656400Seric 	/*
121756400Seric 	**  Check the queue file for plausibility to avoid attacks.
121856400Seric 	*/
121956400Seric 
122056400Seric 	if (fstat(fileno(qfp), &st) < 0)
122156400Seric 	{
122256400Seric 		/* must have been being processed by someone else */
122358884Seric 		if (tTd(40, 8))
122458884Seric 			printf("readqf(%s): fstat failure (%s)\n",
122558884Seric 				qf, errstring(errno));
122656400Seric 		fclose(qfp);
122756400Seric 		return FALSE;
122856400Seric 	}
122956400Seric 
123064137Seric 	if (st.st_uid != geteuid())
123156400Seric 	{
123256400Seric # ifdef LOG
123356400Seric 		if (LogLevel > 0)
123456400Seric 		{
123556400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
123656400Seric 				e->e_id, st.st_uid, st.st_mode);
123756400Seric 		}
123856795Seric # endif /* LOG */
123958884Seric 		if (tTd(40, 8))
124058884Seric 			printf("readqf(%s): bogus file\n", qf);
1241*68490Seric 		loseqfile(e, "bogus file uid in mqueue");
124256400Seric 		fclose(qfp);
124356400Seric 		return FALSE;
124456400Seric 	}
124556400Seric 
124664277Seric 	if (st.st_size == 0)
124740934Srick 	{
124864277Seric 		/* must be a bogus file -- just remove it */
124964277Seric 		(void) unlink(qf);
125064277Seric 		fclose(qfp);
125151920Seric 		return FALSE;
125240934Srick 	}
125340934Srick 
125464277Seric 	if (st.st_nlink == 0)
125559101Seric 	{
125664277Seric 		/*
125764277Seric 		**  Race condition -- we got a file just as it was being
125864277Seric 		**  unlinked.  Just assume it is zero length.
125964277Seric 		*/
126064277Seric 
126159101Seric 		fclose(qfp);
126259101Seric 		return FALSE;
126359101Seric 	}
126459101Seric 
126564277Seric 	/* good file -- save this lock */
126651920Seric 	e->e_lockfp = qfp;
126751920Seric 
126840934Srick 	/* do basic system initialization */
126955012Seric 	initsys(e);
127065164Seric 	define('i', e->e_id, e);
127140934Srick 
12729377Seric 	LineNumber = 0;
127363850Seric 	e->e_flags |= EF_GLOBALERRS;
127463850Seric 	OpMode = MD_DELIVER;
127554974Seric 	ctladdr = NULL;
127668481Seric 	e->e_dfino = -1;
127757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
12784632Seric 	{
127958737Seric 		register char *p;
128057529Seric 		struct stat st;
128168481Seric 		u_long qflags;
128268481Seric 		ADDRESS *q;
128357529Seric 
128426504Seric 		if (tTd(40, 4))
128557135Seric 			printf("+++++ %s\n", bp);
128657135Seric 		switch (bp[0])
12874632Seric 		{
128868481Seric 		  case 'V':		/* queue file version number */
128968481Seric 			qfver = atoi(&bp[1]);
129068481Seric 			if (qfver > QF_VERSION)
129168481Seric 			{
129268481Seric 				syserr("Version number in qf (%d) greater than max (%d)",
129368481Seric 					qfver, QF_VERSION);
129468481Seric 			}
129568481Seric 			break;
129668481Seric 
129740973Sbostic 		  case 'C':		/* specify controlling user */
129857135Seric 			ctladdr = setctluser(&bp[1]);
129940973Sbostic 			break;
130040973Sbostic 
130168481Seric 		  case 'Q':		/* original recipient */
130268481Seric 			orcpt = newstr(&bp[1]);
130368481Seric 			break;
130468481Seric 
13054632Seric 		  case 'R':		/* specify recipient */
130668481Seric 			p = bp;
130768481Seric 			qflags = 0;
130868481Seric 			if (qfver >= 1)
130968481Seric 			{
131068481Seric 				/* get flag bits */
131168481Seric 				while (*++p != '\0' && *p != ':')
131268481Seric 				{
131368481Seric 					switch (*p)
131468481Seric 					{
131568481Seric 					  case 'S':
131668481Seric 						qflags |= QPINGONSUCCESS;
131768481Seric 						break;
131868481Seric 
131968481Seric 					  case 'F':
132068481Seric 						qflags |= QPINGONFAILURE;
132168481Seric 						break;
132268481Seric 
132368481Seric 					  case 'D':
132468481Seric 						qflags |= QPINGONDELAY;
132568481Seric 						break;
132668481Seric 
132768481Seric 					  case 'B':
132868481Seric 						qflags |= QHAS_RET_PARAM;
132968481Seric 						break;
133068481Seric 
133168481Seric 					  case 'N':
133268481Seric 						qflags |= QHAS_RET_PARAM|QRET_HDRS;
133368481Seric 						break;
133468481Seric 
133568481Seric 					  case 'P':
133668481Seric 						qflags |= QPRIMARY;
133768481Seric 						break;
133868481Seric 					}
133968481Seric 				}
134068481Seric 			}
134168481Seric 			else
134268481Seric 				qflags |= QPRIMARY;
134368481Seric 			q = parseaddr(++p, NULLADDR, RF_COPYALL, '\0', NULL, e);
134468481Seric 			if (q != NULL)
134568481Seric 			{
134668481Seric 				q->q_alias = ctladdr;
134768481Seric 				q->q_flags |= qflags;
134868481Seric 				q->q_orcpt = orcpt;
134968481Seric 				(void) recipient(q, &e->e_sendqueue, 0, e);
135068481Seric 			}
135168481Seric 			orcpt = NULL;
13524632Seric 			break;
13534632Seric 
135425687Seric 		  case 'E':		/* specify error recipient */
135568481Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, 0, e);
135625687Seric 			break;
135725687Seric 
13584632Seric 		  case 'H':		/* header */
135957135Seric 			(void) chompheader(&bp[1], FALSE, e);
13604632Seric 			break;
13614632Seric 
136210108Seric 		  case 'M':		/* message */
136364705Seric 			/* ignore this; we want a new message next time */
136410108Seric 			break;
136510108Seric 
13664632Seric 		  case 'S':		/* sender */
136758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
13684632Seric 			break;
13694632Seric 
137059093Seric 		  case 'B':		/* body type */
137159093Seric 			e->e_bodytype = newstr(&bp[1]);
137259093Seric 			break;
137359093Seric 
13744632Seric 		  case 'D':		/* data file name */
137557135Seric 			e->e_df = newstr(&bp[1]);
13769544Seric 			e->e_dfp = fopen(e->e_df, "r");
13779544Seric 			if (e->e_dfp == NULL)
137858020Seric 			{
13799377Seric 				syserr("readqf: cannot open %s", e->e_df);
138058020Seric 				e->e_msgsize = -1;
138158020Seric 			}
138258020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
138368481Seric 			{
138457529Seric 				e->e_msgsize = st.st_size;
138568481Seric 				e->e_dfdev = st.st_dev;
138668481Seric 				e->e_dfino = st.st_ino;
138768481Seric 			}
13884632Seric 			break;
13894632Seric 
13907860Seric 		  case 'T':		/* init time */
139157135Seric 			e->e_ctime = atol(&bp[1]);
13924632Seric 			break;
13934632Seric 
139468481Seric 		  case 'I':		/* data file's inode number */
139568481Seric 			if (e->e_dfino == -1)
139668481Seric 				e->e_dfino = atol(&buf[1]);
139768481Seric 			break;
139868481Seric 
139968481Seric 		  case 'K':		/* time of last deliver attempt */
140068481Seric 			e->e_dtime = atol(&buf[1]);
140168481Seric 			break;
140268481Seric 
140368481Seric 		  case 'N':		/* number of delivery attempts */
140468481Seric 			e->e_ntries = atoi(&buf[1]);
140568481Seric 			break;
140668481Seric 
14074634Seric 		  case 'P':		/* message priority */
140857135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
14094634Seric 			break;
14104634Seric 
141158737Seric 		  case 'F':		/* flag bits */
141258737Seric 			for (p = &bp[1]; *p != '\0'; p++)
141358737Seric 			{
141458737Seric 				switch (*p)
141558737Seric 				{
141658737Seric 				  case 'w':	/* warning sent */
141758737Seric 					e->e_flags |= EF_WARNING;
141858737Seric 					break;
141958737Seric 
142058737Seric 				  case 'r':	/* response */
142158737Seric 					e->e_flags |= EF_RESPONSE;
142258737Seric 					break;
142368481Seric 
142468481Seric 				  case '8':	/* has 8 bit data */
142568481Seric 					e->e_flags |= EF_HAS8BIT;
142668481Seric 					break;
142758737Seric 				}
142858737Seric 			}
142958737Seric 			break;
143058737Seric 
143168481Seric 		  case 'Z':		/* original envelope id from ESMTP */
143268481Seric 			e->e_envid = newstr(&bp[1]);
143368481Seric 			break;
143468481Seric 
143553400Seric 		  case '$':		/* define macro */
143657135Seric 			define(bp[1], newstr(&bp[2]), e);
143753400Seric 			break;
143853400Seric 
143924941Seric 		  case '\0':		/* blank line; ignore */
144024941Seric 			break;
144124941Seric 
14424632Seric 		  default:
144365960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
144465200Seric 				qf, LineNumber, bp);
144563753Seric 			fclose(qfp);
1446*68490Seric 			loseqfile(e, "unrecognized line");
144763753Seric 			return FALSE;
14484632Seric 		}
144957135Seric 
145057135Seric 		if (bp != buf)
145157135Seric 			free(bp);
14524632Seric 	}
14539377Seric 
145424941Seric 	/*
145524941Seric 	**  If we haven't read any lines, this queue file is empty.
145624941Seric 	**  Arrange to remove it without referencing any null pointers.
145724941Seric 	*/
145824941Seric 
145924941Seric 	if (LineNumber == 0)
146024941Seric 	{
146124941Seric 		errno = 0;
146224941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
146324941Seric 	}
146451920Seric 	return TRUE;
14654632Seric }
14664632Seric /*
14679630Seric **  PRINTQUEUE -- print out a representation of the mail queue
14689630Seric **
14699630Seric **	Parameters:
14709630Seric **		none.
14719630Seric **
14729630Seric **	Returns:
14739630Seric **		none.
14749630Seric **
14759630Seric **	Side Effects:
14769630Seric **		Prints a listing of the mail queue on the standard output.
14779630Seric */
14785182Seric 
14799630Seric printqueue()
14809630Seric {
14819630Seric 	register WORK *w;
14829630Seric 	FILE *f;
148310070Seric 	int nrequests;
14849630Seric 	char buf[MAXLINE];
14859630Seric 
14869630Seric 	/*
148758250Seric 	**  Check for permission to print the queue
148858250Seric 	*/
148958250Seric 
149064333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
149158250Seric 	{
149258250Seric 		struct stat st;
149358523Seric # ifdef NGROUPS
149458523Seric 		int n;
149563937Seric 		GIDSET_T gidset[NGROUPS];
149658523Seric # endif
149758250Seric 
149858517Seric 		if (stat(QueueDir, &st) < 0)
149958250Seric 		{
150058250Seric 			syserr("Cannot stat %s", QueueDir);
150158250Seric 			return;
150258250Seric 		}
150358523Seric # ifdef NGROUPS
150458523Seric 		n = getgroups(NGROUPS, gidset);
150558523Seric 		while (--n >= 0)
150658523Seric 		{
150758523Seric 			if (gidset[n] == st.st_gid)
150858523Seric 				break;
150958523Seric 		}
151058523Seric 		if (n < 0)
151158523Seric # else
151263787Seric 		if (RealGid != st.st_gid)
151358523Seric # endif
151458250Seric 		{
151558250Seric 			usrerr("510 You are not permitted to see the queue");
151658250Seric 			setstat(EX_NOPERM);
151758250Seric 			return;
151858250Seric 		}
151958250Seric 	}
152058250Seric 
152158250Seric 	/*
15229630Seric 	**  Read and order the queue.
15239630Seric 	*/
15249630Seric 
152524941Seric 	nrequests = orderq(TRUE);
15269630Seric 
15279630Seric 	/*
15289630Seric 	**  Print the work list that we have read.
15299630Seric 	*/
15309630Seric 
15319630Seric 	/* first see if there is anything */
153210070Seric 	if (nrequests <= 0)
15339630Seric 	{
153410070Seric 		printf("Mail queue is empty\n");
15359630Seric 		return;
15369630Seric 	}
15379630Seric 
153851920Seric 	CurrentLA = getla();	/* get load average */
153940934Srick 
154010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
154125687Seric 	if (nrequests > QUEUESIZE)
154225687Seric 		printf(", only %d printed", QUEUESIZE);
154324979Seric 	if (Verbose)
154458716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
154524979Seric 	else
154658716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
15479630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
15489630Seric 	{
15499630Seric 		struct stat st;
155010070Seric 		auto time_t submittime = 0;
155110070Seric 		long dfsize = -1;
155258737Seric 		int flags = 0;
155368481Seric 		int qfver;
155410108Seric 		char message[MAXLINE];
155559093Seric 		char bodytype[MAXNAME];
15569630Seric 
155764355Seric 		printf("%8s", w->w_name + 2);
155817468Seric 		f = fopen(w->w_name, "r");
155917468Seric 		if (f == NULL)
156017468Seric 		{
156164355Seric 			printf(" (job completed)\n");
156217468Seric 			errno = 0;
156317468Seric 			continue;
156417468Seric 		}
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 
164510070Seric 			  case 'D':	/* data file name */
164610070Seric 				if (stat(&buf[1], &st) >= 0)
164710070Seric 					dfsize = st.st_size;
164810070Seric 				break;
164958737Seric 
165058737Seric 			  case 'F':	/* flag bits */
165158737Seric 				for (p = &buf[1]; *p != '\0'; p++)
165258737Seric 				{
165358737Seric 					switch (*p)
165458737Seric 					{
165558737Seric 					  case 'w':
165658737Seric 						flags |= EF_WARNING;
165758737Seric 						break;
165858737Seric 					}
165958737Seric 				}
16609630Seric 			}
16619630Seric 		}
166210070Seric 		if (submittime == (time_t) 0)
166310070Seric 			printf(" (no control file)");
16649630Seric 		printf("\n");
166523098Seric 		(void) fclose(f);
16669630Seric 	}
16679630Seric }
16689630Seric 
166956795Seric # endif /* QUEUE */
167017468Seric /*
167117468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
167217468Seric **
167317468Seric **	Assigns an id code if one does not already exist.
167417468Seric **	This code is very careful to avoid trashing existing files
167517468Seric **	under any circumstances.
167617468Seric **
167717468Seric **	Parameters:
167817468Seric **		e -- envelope to build it in/from.
167917468Seric **		type -- the file type, used as the first character
168017468Seric **			of the file name.
168117468Seric **
168217468Seric **	Returns:
168317468Seric **		a pointer to the new file name (in a static buffer).
168417468Seric **
168517468Seric **	Side Effects:
168651920Seric **		If no id code is already assigned, queuename will
168751920Seric **		assign an id code, create a qf file, and leave a
168851920Seric **		locked, open-for-write file pointer in the envelope.
168917468Seric */
169017468Seric 
169117468Seric char *
169217468Seric queuename(e, type)
169317468Seric 	register ENVELOPE *e;
169459700Seric 	int type;
169517468Seric {
169617468Seric 	static int pid = -1;
169758741Seric 	static char c0;
169858741Seric 	static char c1;
169958741Seric 	static char c2;
170058716Seric 	time_t now;
170158716Seric 	struct tm *tm;
170258689Seric 	static char buf[MAXNAME];
170317468Seric 
170417468Seric 	if (e->e_id == NULL)
170517468Seric 	{
170617468Seric 		char qf[20];
170717468Seric 
170817468Seric 		/* find a unique id */
170917468Seric 		if (pid != getpid())
171017468Seric 		{
171117468Seric 			/* new process -- start back at "AA" */
171217468Seric 			pid = getpid();
171358716Seric 			now = curtime();
171458716Seric 			tm = localtime(&now);
171558716Seric 			c0 = 'A' + tm->tm_hour;
171617468Seric 			c1 = 'A';
171717468Seric 			c2 = 'A' - 1;
171817468Seric 		}
171958716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
172017468Seric 
172117468Seric 		while (c1 < '~' || c2 < 'Z')
172217468Seric 		{
172317468Seric 			int i;
172417468Seric 
172517468Seric 			if (c2 >= 'Z')
172617468Seric 			{
172717468Seric 				c1++;
172817468Seric 				c2 = 'A' - 1;
172917468Seric 			}
173058716Seric 			qf[3] = c1;
173158716Seric 			qf[4] = ++c2;
173217468Seric 			if (tTd(7, 20))
173340934Srick 				printf("queuename: trying \"%s\"\n", qf);
173417468Seric 
173540934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
173651920Seric 			if (i < 0)
173751920Seric 			{
173851920Seric 				if (errno == EEXIST)
173951920Seric 					continue;
174064705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
174164705Seric 					qf, QueueDir, geteuid());
174251920Seric 				exit(EX_UNAVAILABLE);
174351920Seric 			}
174464335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
174551920Seric 			{
174651920Seric 				e->e_lockfp = fdopen(i, "w");
174740934Srick 				break;
174817468Seric 			}
174951920Seric 
175051920Seric 			/* a reader got the file; abandon it and try again */
175151920Seric 			(void) close(i);
175217468Seric 		}
175317468Seric 		if (c1 >= '~' && c2 >= 'Z')
175417468Seric 		{
175564705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
175664705Seric 				qf, QueueDir, geteuid());
175717468Seric 			exit(EX_OSERR);
175817468Seric 		}
175917468Seric 		e->e_id = newstr(&qf[2]);
176017468Seric 		define('i', e->e_id, e);
176117468Seric 		if (tTd(7, 1))
176217468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
176364745Seric 		if (tTd(7, 9))
176464745Seric 		{
176564745Seric 			printf("  lockfd=");
176664745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
176764745Seric 		}
176817468Seric # ifdef LOG
176958020Seric 		if (LogLevel > 93)
177017468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
177156795Seric # endif /* LOG */
177217468Seric 	}
177317468Seric 
177417468Seric 	if (type == '\0')
177517468Seric 		return (NULL);
177617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
177717468Seric 	if (tTd(7, 2))
177817468Seric 		printf("queuename: %s\n", buf);
177917468Seric 	return (buf);
178017468Seric }
178117468Seric /*
178217468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
178317468Seric **
178417468Seric **	Parameters:
178517468Seric **		e -- the envelope to unlock.
178617468Seric **
178717468Seric **	Returns:
178817468Seric **		none
178917468Seric **
179017468Seric **	Side Effects:
179117468Seric **		unlocks the queue for `e'.
179217468Seric */
179317468Seric 
179417468Seric unlockqueue(e)
179517468Seric 	ENVELOPE *e;
179617468Seric {
179758680Seric 	if (tTd(51, 4))
179858680Seric 		printf("unlockqueue(%s)\n", e->e_id);
179958680Seric 
180051920Seric 	/* if there is a lock file in the envelope, close it */
180151920Seric 	if (e->e_lockfp != NULL)
180258680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
180351920Seric 	e->e_lockfp = NULL;
180451920Seric 
180558728Seric 	/* don't create a queue id if we don't already have one */
180658728Seric 	if (e->e_id == NULL)
180758728Seric 		return;
180858728Seric 
180917468Seric 	/* remove the transcript */
181017468Seric # ifdef LOG
181158020Seric 	if (LogLevel > 87)
181217468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
181356795Seric # endif /* LOG */
181458680Seric 	if (!tTd(51, 104))
181517468Seric 		xunlink(queuename(e, 'x'));
181617468Seric 
181717468Seric }
181840973Sbostic /*
181954974Seric **  SETCTLUSER -- create a controlling address
182040973Sbostic **
182154974Seric **	Create a fake "address" given only a local login name; this is
182254974Seric **	used as a "controlling user" for future recipient addresses.
182340973Sbostic **
182440973Sbostic **	Parameters:
182554974Seric **		user -- the user name of the controlling user.
182640973Sbostic **
182740973Sbostic **	Returns:
182854974Seric **		An address descriptor for the controlling user.
182940973Sbostic **
183040973Sbostic **	Side Effects:
183140973Sbostic **		none.
183240973Sbostic */
183340973Sbostic 
183454974Seric ADDRESS *
183554974Seric setctluser(user)
183654974Seric 	char *user;
183740973Sbostic {
183854974Seric 	register ADDRESS *a;
183940973Sbostic 	struct passwd *pw;
184059113Seric 	char *p;
184140973Sbostic 
184240973Sbostic 	/*
184354974Seric 	**  See if this clears our concept of controlling user.
184440973Sbostic 	*/
184540973Sbostic 
184663850Seric 	if (user == NULL || *user == '\0')
184763850Seric 		return NULL;
184840973Sbostic 
184940973Sbostic 	/*
185054974Seric 	**  Set up addr fields for controlling user.
185140973Sbostic 	*/
185240973Sbostic 
185354974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
185454974Seric 	bzero((char *) a, sizeof *a);
185559113Seric 
185659113Seric 	p = strchr(user, ':');
185759113Seric 	if (p != NULL)
185859113Seric 		*p++ = '\0';
185959270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
186040973Sbostic 	{
186165822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
186265822Seric 			a->q_home = "";
186365822Seric 		else
186465822Seric 			a->q_home = newstr(pw->pw_dir);
186540973Sbostic 		a->q_uid = pw->pw_uid;
186640973Sbostic 		a->q_gid = pw->pw_gid;
186757642Seric 		a->q_user = newstr(user);
186859270Seric 		a->q_flags |= QGOODUID;
186940973Sbostic 	}
187040973Sbostic 	else
187140973Sbostic 	{
187257642Seric 		a->q_user = newstr(DefUser);
187340973Sbostic 	}
187440973Sbostic 
187559270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
187656328Seric 	a->q_mailer = LocalMailer;
187759113Seric 	if (p == NULL)
187859113Seric 		a->q_paddr = a->q_user;
187959113Seric 	else
188059113Seric 		a->q_paddr = newstr(p);
188154974Seric 	return a;
188240973Sbostic }
1883*68490Seric /*
1884*68490Seric **  LOSEQFILE -- save the qf as Qf and try to let someone know
1885*68490Seric **
1886*68490Seric **	Parameters:
1887*68490Seric **		e -- the envelope (e->e_id will be used).
1888*68490Seric **		why -- reported to whomever can hear.
1889*68490Seric **
1890*68490Seric **	Returns:
1891*68490Seric **		none.
1892*68490Seric */
1893*68490Seric 
1894*68490Seric void
1895*68490Seric loseqfile(e, why)
1896*68490Seric 	register ENVELOPE *e;
1897*68490Seric 	char *why;
1898*68490Seric {
1899*68490Seric 	char buf[40];
1900*68490Seric 
1901*68490Seric 	if (e == NULL || e->e_id == NULL)
1902*68490Seric 		return;
1903*68490Seric 	if (strlen(e->e_id) > sizeof buf - 4)
1904*68490Seric 		return;
1905*68490Seric 	strcpy(buf, queuename(e, 'q'));
1906*68490Seric 	rename(buf, queuename(e, 'Q'));
1907*68490Seric #ifdef LOG
1908*68490Seric 	syslog(LOG_ALERT, "Losing %s: %s", buf, why);
1909*68490Seric #endif
1910*68490Seric }
1911