xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58179)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*58179Seric static char sccsid[] = "@(#)queue.c	6.18 (Berkeley) 02/24/93 (with queueing)";
1433731Sbostic #else
15*58179Seric static char sccsid[] = "@(#)queue.c	6.18 (Berkeley) 02/24/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
2157948Seric # include <sys/file.h>
224634Seric # include <signal.h>
234632Seric # include <errno.h>
2440973Sbostic # include <pwd.h>
2551937Seric # include <fcntl.h>
2657977Seric # ifndef MAXNAMLEN
2757736Seric # include <dirent.h>
2857736Seric # endif
294632Seric 
3033731Sbostic # ifdef QUEUE
314632Seric 
324632Seric /*
339377Seric **  Work queue.
349377Seric */
359377Seric 
369377Seric struct work
379377Seric {
389377Seric 	char		*w_name;	/* name of control file */
399377Seric 	long		w_pri;		/* priority of message, see below */
4025013Seric 	time_t		w_ctime;	/* creation time of message */
419377Seric 	struct work	*w_next;	/* next in queue */
429377Seric };
439377Seric 
449377Seric typedef struct work	WORK;
459377Seric 
469377Seric WORK	*WorkQ;			/* queue of things to be done */
479377Seric /*
484632Seric **  QUEUEUP -- queue a message up for future transmission.
494632Seric **
504632Seric **	Parameters:
516980Seric **		e -- the envelope to queue up.
526999Seric **		queueall -- if TRUE, queue all addresses, rather than
536999Seric **			just those with the QQUEUEUP flag set.
549377Seric **		announce -- if TRUE, tell when you are queueing up.
554632Seric **
564632Seric **	Returns:
5751920Seric **		none.
584632Seric **
594632Seric **	Side Effects:
609377Seric **		The current request are saved in a control file.
6151920Seric **		The queue file is left locked.
624632Seric */
634632Seric 
649377Seric queueup(e, queueall, announce)
656980Seric 	register ENVELOPE *e;
666999Seric 	bool queueall;
679377Seric 	bool announce;
684632Seric {
697812Seric 	char *qf;
707812Seric 	register FILE *tfp;
714632Seric 	register HDR *h;
725007Seric 	register ADDRESS *q;
7351920Seric 	int fd;
7451920Seric 	int i;
7551920Seric 	bool newid;
7653400Seric 	register char *p;
7710173Seric 	MAILER nullmailer;
7854974Seric 	ADDRESS *lastctladdr;
7954975Seric 	static ADDRESS *nullctladdr = NULL;
8051920Seric 	char buf[MAXLINE], tf[MAXLINE];
8153400Seric 	extern char *macvalue();
8254974Seric 	extern ADDRESS *getctladdr();
834632Seric 
845037Seric 	/*
8554975Seric 	**  If we don't have nullctladdr, create one
8654975Seric 	*/
8754975Seric 
8854975Seric 	if (nullctladdr == NULL)
8954975Seric 	{
9054975Seric 		nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr);
9154975Seric 		bzero((char *) nullctladdr, sizeof nullctladdr);
9254975Seric 	}
9354975Seric 
9454975Seric 	/*
9517477Seric 	**  Create control file.
965037Seric 	*/
974632Seric 
9851920Seric 	newid = (e->e_id == NULL);
9951920Seric 	strcpy(tf, queuename(e, 't'));
10051920Seric 	tfp = e->e_lockfp;
10151920Seric 	if (tfp == NULL)
10251920Seric 		newid = FALSE;
10351920Seric 	if (newid)
10451835Seric 	{
10551920Seric 		tfp = e->e_lockfp;
10651920Seric 	}
10751920Seric 	else
10851920Seric 	{
10951920Seric 		/* get a locked tf file */
11051920Seric 		for (i = 100; --i >= 0; )
11151835Seric 		{
11251937Seric # ifdef LOCKF
11351937Seric 			struct flock lfd;
11451937Seric # endif
11551937Seric 
11651920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
11751920Seric 			if (fd < 0)
11851835Seric 			{
11951920Seric 				if (errno == EEXIST)
12051920Seric 					continue;
12151920Seric 				syserr("queueup: cannot create temp file %s", tf);
12251920Seric 				return;
12341636Srick 			}
12451835Seric # ifdef LOCKF
12551937Seric 			lfd.l_type = F_WRLCK;
12651937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
12751937Seric 			if (fcntl(fd, F_SETLK, &lfd) >= 0)
12851920Seric 				break;
12951920Seric 			if (errno != EACCES && errno != EAGAIN)
13051920Seric 				syserr("cannot lockf(%s)", tf);
13151835Seric # else
13251920Seric 			if (flock(fd, LOCK_EX|LOCK_NB) >= 0)
13351920Seric 				break;
13451920Seric 			if (errno != EWOULDBLOCK)
13551920Seric 				syserr("cannot flock(%s)", tf);
13651835Seric # endif
13751920Seric 			close(fd);
13841636Srick 		}
13941636Srick 
14051920Seric 		tfp = fdopen(fd, "w");
14151920Seric 	}
1424632Seric 
1437677Seric 	if (tTd(40, 1))
14417468Seric 		printf("queueing %s\n", e->e_id);
1454632Seric 
1464632Seric 	/*
1476980Seric 	**  If there is no data file yet, create one.
1486980Seric 	*/
1496980Seric 
1506980Seric 	if (e->e_df == NULL)
1516980Seric 	{
1526980Seric 		register FILE *dfp;
1539389Seric 		extern putbody();
1546980Seric 
1557812Seric 		e->e_df = newstr(queuename(e, 'd'));
15640934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15740934Srick 		if (fd < 0)
1586980Seric 		{
1596980Seric 			syserr("queueup: cannot create %s", e->e_df);
16051920Seric 			if (!newid)
16151920Seric 				(void) fclose(tfp);
16251920Seric 			return;
1636980Seric 		}
16440934Srick 		dfp = fdopen(fd, "w");
16510173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1667009Seric 		(void) fclose(dfp);
1679389Seric 		e->e_putbody = putbody;
1686980Seric 	}
1696980Seric 
1706980Seric 	/*
1714632Seric 	**  Output future work requests.
17225687Seric 	**	Priority and creation time should be first, since
17325687Seric 	**	they are required by orderq.
1744632Seric 	*/
1754632Seric 
1769377Seric 	/* output message priority */
1779377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1789377Seric 
1799630Seric 	/* output creation time */
1809630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1819630Seric 
1824632Seric 	/* output name of data file */
1837812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1844632Seric 
18510108Seric 	/* message from envelope, if it exists */
18610108Seric 	if (e->e_message != NULL)
18710108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18810108Seric 
18953400Seric 	/* $r and $s macro values */
19053400Seric 	if ((p = macvalue('r', e)) != NULL)
19153400Seric 		fprintf(tfp, "$r%s\n", p);
19253400Seric 	if ((p = macvalue('s', e)) != NULL)
19353400Seric 		fprintf(tfp, "$s%s\n", p);
19453400Seric 
1954632Seric 	/* output name of sender */
1967812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1974632Seric 
19855360Seric 	/* output list of error recipients */
19955360Seric 	lastctladdr = NULL;
20055360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20155360Seric 	{
20255360Seric 		if (!bitset(QDONTSEND, q->q_flags))
20355360Seric 		{
20455360Seric 			ADDRESS *ctladdr;
20555360Seric 
20655360Seric 			ctladdr = getctladdr(q);
20755360Seric 			if (ctladdr == NULL && q->q_alias != NULL)
20855360Seric 				ctladdr = nullctladdr;
20955360Seric 			if (ctladdr != lastctladdr)
21055360Seric 			{
21155360Seric 				printctladdr(ctladdr, tfp);
21255360Seric 				lastctladdr = ctladdr;
21355360Seric 			}
21455360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21555360Seric 		}
21655360Seric 	}
21755360Seric 
2184632Seric 	/* output list of recipient addresses */
2196980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2204632Seric 	{
22147284Seric 		if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) :
2227763Seric 			       bitset(QQUEUEUP, q->q_flags))
2238245Seric 		{
22454974Seric 			ADDRESS *ctladdr;
22540973Sbostic 
22654975Seric 			ctladdr = getctladdr(q);
22754975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
22854975Seric 				ctladdr = nullctladdr;
22954975Seric 			if (ctladdr != lastctladdr)
23054974Seric 			{
23154974Seric 				printctladdr(ctladdr, tfp);
23254974Seric 				lastctladdr = ctladdr;
23354974Seric 			}
2347812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2359377Seric 			if (announce)
2369377Seric 			{
2379377Seric 				e->e_to = q->q_paddr;
23858151Seric 				message("queued");
23958020Seric 				if (LogLevel > 8)
24054967Seric 					logdelivery("queued", e);
2419377Seric 				e->e_to = NULL;
2429377Seric 			}
2439387Seric 			if (tTd(40, 1))
2449387Seric 			{
2459387Seric 				printf("queueing ");
2469387Seric 				printaddr(q, FALSE);
2479387Seric 			}
2488245Seric 		}
2494632Seric 	}
2504632Seric 
2519377Seric 	/*
2529377Seric 	**  Output headers for this message.
2539377Seric 	**	Expand macros completely here.  Queue run will deal with
2549377Seric 	**	everything as absolute headers.
2559377Seric 	**		All headers that must be relative to the recipient
2569377Seric 	**		can be cracked later.
25710173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
25810173Seric 	**	no effect on the addresses as they are output.
2599377Seric 	*/
2609377Seric 
26110686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
26258020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
26358020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
26410349Seric 	nullmailer.m_eol = "\n";
26510173Seric 
26658050Seric 	define('g', "\201f", e);
2676980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2684632Seric 	{
26910686Seric 		extern bool bitzerop();
27010686Seric 
27112015Seric 		/* don't output null headers */
2724632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2734632Seric 			continue;
27412015Seric 
27512015Seric 		/* don't output resent headers on non-resent messages */
27612015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
27712015Seric 			continue;
27812015Seric 
27912015Seric 		/* output this header */
2807812Seric 		fprintf(tfp, "H");
28112015Seric 
28212015Seric 		/* if conditional, output the set of conditions */
28310686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
28410686Seric 		{
28510686Seric 			int j;
28610686Seric 
28723098Seric 			(void) putc('?', tfp);
28810686Seric 			for (j = '\0'; j <= '\177'; j++)
28910686Seric 				if (bitnset(j, h->h_mflags))
29023098Seric 					(void) putc(j, tfp);
29123098Seric 			(void) putc('?', tfp);
29210686Seric 		}
29312015Seric 
29412015Seric 		/* output the header: expand macros, convert addresses */
2957763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2967763Seric 		{
2977763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2988236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2997763Seric 		}
3008245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3019348Seric 		{
30255012Seric 			commaize(h, h->h_value, tfp,
30355012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
30455012Seric 				 &nullmailer, e);
3059348Seric 		}
3067763Seric 		else
3078245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3084632Seric 	}
3094632Seric 
3104632Seric 	/*
3114632Seric 	**  Clean up.
3124632Seric 	*/
3134632Seric 
31451920Seric 	if (!newid)
31551920Seric 	{
31651920Seric 		qf = queuename(e, 'q');
31751920Seric 		if (rename(tf, qf) < 0)
31851920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31951920Seric 		if (e->e_lockfp != NULL)
32051920Seric 			(void) fclose(e->e_lockfp);
32151920Seric 		e->e_lockfp = tfp;
32251920Seric 	}
32351920Seric 	else
32451920Seric 		qf = tf;
32541636Srick 	errno = 0;
3267391Seric 
3277677Seric # ifdef LOG
3287677Seric 	/* save log info */
32958020Seric 	if (LogLevel > 79)
3307878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33156795Seric # endif /* LOG */
33240934Srick 	fflush(tfp);
33351920Seric 	return;
3344632Seric }
33554974Seric 
33654974Seric printctladdr(a, tfp)
33754974Seric 	ADDRESS *a;
33854974Seric 	FILE *tfp;
33954974Seric {
34054974Seric 	char *u;
34154974Seric 	struct passwd *pw;
34254974Seric 	extern struct passwd *getpwuid();
34354974Seric 
34454974Seric 	if (a == NULL)
34554974Seric 	{
34654974Seric 		fprintf(tfp, "C\n");
34754974Seric 		return;
34854974Seric 	}
34954974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
35054974Seric 		u = DefUser;
35154974Seric 	else
35254974Seric 		u = pw->pw_name;
35354974Seric 	fprintf(tfp, "C%s\n", u);
35454974Seric }
35554974Seric 
3564632Seric /*
3574632Seric **  RUNQUEUE -- run the jobs in the queue.
3584632Seric **
3594632Seric **	Gets the stuff out of the queue in some presumably logical
3604632Seric **	order and processes them.
3614632Seric **
3624632Seric **	Parameters:
36324941Seric **		forkflag -- TRUE if the queue scanning should be done in
36424941Seric **			a child process.  We double-fork so it is not our
36524941Seric **			child and we don't have to clean up after it.
3664632Seric **
3674632Seric **	Returns:
3684632Seric **		none.
3694632Seric **
3704632Seric **	Side Effects:
3714632Seric **		runs things in the mail queue.
3724632Seric */
3734632Seric 
37455360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37555360Seric 
37655360Seric runqueue(forkflag)
3774639Seric 	bool forkflag;
3784632Seric {
37924953Seric 	extern bool shouldqueue();
38055360Seric 	register ENVELOPE *e;
38155360Seric 	extern ENVELOPE BlankEnvelope;
38255360Seric 	extern ENVELOPE *newenvelope();
38324953Seric 
3847466Seric 	/*
38524953Seric 	**  If no work will ever be selected, don't even bother reading
38624953Seric 	**  the queue.
38724953Seric 	*/
38824953Seric 
38951920Seric 	CurrentLA = getla();	/* get load average */
39040934Srick 
39158132Seric 	if (shouldqueue(0L, curtime()))
39224953Seric 	{
39324953Seric 		if (Verbose)
39424953Seric 			printf("Skipping queue run -- load average too high\n");
39558107Seric 		if (forkflag && QueueIntvl != 0)
39658107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39755360Seric 		return;
39824953Seric 	}
39924953Seric 
40024953Seric 	/*
4017466Seric 	**  See if we want to go off and do other useful work.
4027466Seric 	*/
4034639Seric 
4044639Seric 	if (forkflag)
4054639Seric 	{
4067943Seric 		int pid;
4077943Seric 
4087943Seric 		pid = dofork();
4097943Seric 		if (pid != 0)
4104639Seric 		{
41146928Sbostic 			extern void reapchild();
41225184Seric 
4137943Seric 			/* parent -- pick up intermediate zombie */
41425184Seric #ifndef SIGCHLD
4159377Seric 			(void) waitfor(pid);
41656795Seric #else /* SIGCHLD */
41725184Seric 			(void) signal(SIGCHLD, reapchild);
41856795Seric #endif /* SIGCHLD */
4197690Seric 			if (QueueIntvl != 0)
4209348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4214639Seric 			return;
4224639Seric 		}
4237943Seric 		/* child -- double fork */
42425184Seric #ifndef SIGCHLD
4257943Seric 		if (fork() != 0)
4267943Seric 			exit(EX_OK);
42756795Seric #else /* SIGCHLD */
42825184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42956795Seric #endif /* SIGCHLD */
4304639Seric 	}
43124941Seric 
43240934Srick 	setproctitle("running queue: %s", QueueDir);
43357731Seric 	ForceMail = TRUE;
43424941Seric 
4357876Seric # ifdef LOG
43658020Seric 	if (LogLevel > 69)
43755360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43855360Seric 			QueueDir, getpid(), forkflag);
43956795Seric # endif /* LOG */
4404639Seric 
4417466Seric 	/*
44210205Seric 	**  Release any resources used by the daemon code.
44310205Seric 	*/
44410205Seric 
44510205Seric # ifdef DAEMON
44610205Seric 	clrdaemon();
44756795Seric # endif /* DAEMON */
44810205Seric 
44910205Seric 	/*
45055360Seric 	**  Create ourselves an envelope
45155360Seric 	*/
45255360Seric 
45355360Seric 	CurEnv = &QueueEnvelope;
454*58179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
45555360Seric 	e->e_flags = BlankEnvelope.e_flags;
45655360Seric 
45755360Seric 	/*
45827175Seric 	**  Make sure the alias database is open.
45927175Seric 	*/
46027175Seric 
46155012Seric 	initaliases(AliasFile, FALSE, e);
46227175Seric 
46327175Seric 	/*
4647466Seric 	**  Start making passes through the queue.
4657466Seric 	**	First, read and sort the entire queue.
4667466Seric 	**	Then, process the work in that order.
4677466Seric 	**		But if you take too long, start over.
4687466Seric 	*/
4697466Seric 
4707943Seric 	/* order the existing work requests */
47124954Seric 	(void) orderq(FALSE);
4727690Seric 
4737943Seric 	/* process them once at a time */
4747943Seric 	while (WorkQ != NULL)
4754639Seric 	{
4767943Seric 		WORK *w = WorkQ;
4777881Seric 
4787943Seric 		WorkQ = WorkQ->w_next;
47955012Seric 		dowork(w, e);
4807943Seric 		free(w->w_name);
4817943Seric 		free((char *) w);
4824639Seric 	}
48329866Seric 
48429866Seric 	/* exit without the usual cleanup */
48555467Seric 	e->e_id = NULL;
48655467Seric 	finis();
4874634Seric }
4884634Seric /*
4894632Seric **  ORDERQ -- order the work queue.
4904632Seric **
4914632Seric **	Parameters:
49224941Seric **		doall -- if set, include everything in the queue (even
49324941Seric **			the jobs that cannot be run because the load
49424941Seric **			average is too high).  Otherwise, exclude those
49524941Seric **			jobs.
4964632Seric **
4974632Seric **	Returns:
49810121Seric **		The number of request in the queue (not necessarily
49910121Seric **		the number of requests in WorkQ however).
5004632Seric **
5014632Seric **	Side Effects:
5024632Seric **		Sets WorkQ to the queue of available work, in order.
5034632Seric */
5044632Seric 
50525687Seric # define NEED_P		001
50625687Seric # define NEED_T		002
5074632Seric 
50824941Seric orderq(doall)
50924941Seric 	bool doall;
5104632Seric {
5116625Sglickman 	register struct direct *d;
5124632Seric 	register WORK *w;
5136625Sglickman 	DIR *f;
5144632Seric 	register int i;
51525687Seric 	WORK wlist[QUEUESIZE+1];
51610070Seric 	int wn = -1;
5174632Seric 	extern workcmpf();
5184632Seric 
5194632Seric 	/* clear out old WorkQ */
5204632Seric 	for (w = WorkQ; w != NULL; )
5214632Seric 	{
5224632Seric 		register WORK *nw = w->w_next;
5234632Seric 
5244632Seric 		WorkQ = nw;
5254632Seric 		free(w->w_name);
5264632Seric 		free((char *) w);
5274632Seric 		w = nw;
5284632Seric 	}
5294632Seric 
5304632Seric 	/* open the queue directory */
5318148Seric 	f = opendir(".");
5324632Seric 	if (f == NULL)
5334632Seric 	{
5348148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
53510070Seric 		return (0);
5364632Seric 	}
5374632Seric 
5384632Seric 	/*
5394632Seric 	**  Read the work directory.
5404632Seric 	*/
5414632Seric 
54210070Seric 	while ((d = readdir(f)) != NULL)
5434632Seric 	{
5449377Seric 		FILE *cf;
5454632Seric 		char lbuf[MAXNAME];
54657642Seric 		extern bool shouldqueue();
5474632Seric 
5484632Seric 		/* is this an interesting entry? */
5497812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5504632Seric 			continue;
5514632Seric 
55258020Seric 		if (strlen(d->d_name) != 9)
55358020Seric 		{
55458020Seric 			if (Verbose)
55558020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
55658020Seric #ifdef LOG
55758020Seric 			if (LogLevel > 3)
55858020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
55958020Seric 					d->d_name);
56058020Seric #endif
56158020Seric 			if (strlen(d->d_name) >= MAXNAME)
56258020Seric 				d->d_name[MAXNAME - 1] = '\0';
56358020Seric 			strcpy(lbuf, d->d_name);
56458020Seric 			lbuf[0] = 'Q';
56558020Seric 			(void) rename(d->d_name, lbuf);
56658020Seric 			continue;
56758020Seric 		}
56858020Seric 
56910070Seric 		/* yes -- open control file (if not too many files) */
57025687Seric 		if (++wn >= QUEUESIZE)
57110070Seric 			continue;
5728148Seric 		cf = fopen(d->d_name, "r");
5734632Seric 		if (cf == NULL)
5744632Seric 		{
5757055Seric 			/* this may be some random person sending hir msgs */
5767055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
57710090Seric 			if (tTd(41, 2))
57810090Seric 				printf("orderq: cannot open %s (%d)\n",
57910090Seric 					d->d_name, errno);
5807055Seric 			errno = 0;
58110090Seric 			wn--;
5824632Seric 			continue;
5834632Seric 		}
58425687Seric 		w = &wlist[wn];
58525687Seric 		w->w_name = newstr(d->d_name);
5864632Seric 
58725027Seric 		/* make sure jobs in creation don't clog queue */
58825687Seric 		w->w_pri = 0x7fffffff;
58925687Seric 		w->w_ctime = 0;
59025027Seric 
5914632Seric 		/* extract useful information */
59225687Seric 		i = NEED_P | NEED_T;
59325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5944632Seric 		{
59524954Seric 			extern long atol();
59624954Seric 
59724941Seric 			switch (lbuf[0])
5984632Seric 			{
59924941Seric 			  case 'P':
60025687Seric 				w->w_pri = atol(&lbuf[1]);
60125687Seric 				i &= ~NEED_P;
6024632Seric 				break;
60325013Seric 
60425013Seric 			  case 'T':
60525687Seric 				w->w_ctime = atol(&lbuf[1]);
60625687Seric 				i &= ~NEED_T;
60725013Seric 				break;
6084632Seric 			}
6094632Seric 		}
6104632Seric 		(void) fclose(cf);
61124953Seric 
61257438Seric 		if (!doall && shouldqueue(w->w_pri, w->w_ctime))
61324953Seric 		{
61424953Seric 			/* don't even bother sorting this job in */
61524953Seric 			wn--;
61624953Seric 		}
6174632Seric 	}
6186625Sglickman 	(void) closedir(f);
61910090Seric 	wn++;
6204632Seric 
6214632Seric 	/*
6224632Seric 	**  Sort the work directory.
6234632Seric 	*/
6244632Seric 
62525687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6264632Seric 
6274632Seric 	/*
6284632Seric 	**  Convert the work list into canonical form.
6299377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6304632Seric 	*/
6314632Seric 
63224981Seric 	WorkQ = NULL;
63325687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6344632Seric 	{
6354632Seric 		w = (WORK *) xalloc(sizeof *w);
6364632Seric 		w->w_name = wlist[i].w_name;
6374632Seric 		w->w_pri = wlist[i].w_pri;
63825013Seric 		w->w_ctime = wlist[i].w_ctime;
63924981Seric 		w->w_next = WorkQ;
64024981Seric 		WorkQ = w;
6414632Seric 	}
6424632Seric 
6437677Seric 	if (tTd(40, 1))
6444632Seric 	{
6454632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6465037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6474632Seric 	}
64810070Seric 
64910090Seric 	return (wn);
6504632Seric }
6514632Seric /*
6527677Seric **  WORKCMPF -- compare function for ordering work.
6534632Seric **
6544632Seric **	Parameters:
6554632Seric **		a -- the first argument.
6564632Seric **		b -- the second argument.
6574632Seric **
6584632Seric **	Returns:
65924981Seric **		-1 if a < b
66024981Seric **		 0 if a == b
66124981Seric **		+1 if a > b
6624632Seric **
6634632Seric **	Side Effects:
6644632Seric **		none.
6654632Seric */
6664632Seric 
6674632Seric workcmpf(a, b)
6685037Seric 	register WORK *a;
6695037Seric 	register WORK *b;
6704632Seric {
67157438Seric 	long pa = a->w_pri;
67257438Seric 	long pb = b->w_pri;
67324941Seric 
67424941Seric 	if (pa == pb)
6754632Seric 		return (0);
67624941Seric 	else if (pa > pb)
67724981Seric 		return (1);
67824981Seric 	else
67910121Seric 		return (-1);
6804632Seric }
6814632Seric /*
6824632Seric **  DOWORK -- do a work request.
6834632Seric **
6844632Seric **	Parameters:
6854632Seric **		w -- the work request to be satisfied.
6864632Seric **
6874632Seric **	Returns:
6884632Seric **		none.
6894632Seric **
6904632Seric **	Side Effects:
6914632Seric **		The work request is satisfied if possible.
6924632Seric */
6934632Seric 
69455012Seric dowork(w, e)
6954632Seric 	register WORK *w;
69655012Seric 	register ENVELOPE *e;
6974632Seric {
6984632Seric 	register int i;
69924941Seric 	extern bool shouldqueue();
70051920Seric 	extern bool readqf();
7014632Seric 
7027677Seric 	if (tTd(40, 1))
7035037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7044632Seric 
7054632Seric 	/*
70624941Seric 	**  Ignore jobs that are too expensive for the moment.
7074632Seric 	*/
7084632Seric 
70957438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7104632Seric 	{
71124941Seric 		if (Verbose)
71224967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7134632Seric 		return;
7144632Seric 	}
7154632Seric 
71624941Seric 	/*
71724941Seric 	**  Fork for work.
71824941Seric 	*/
71924941Seric 
72024941Seric 	if (ForkQueueRuns)
72124941Seric 	{
72224941Seric 		i = fork();
72324941Seric 		if (i < 0)
72424941Seric 		{
72524941Seric 			syserr("dowork: cannot fork");
72624941Seric 			return;
72724941Seric 		}
72824941Seric 	}
72924941Seric 	else
73024941Seric 	{
73124941Seric 		i = 0;
73224941Seric 	}
73324941Seric 
7344632Seric 	if (i == 0)
7354632Seric 	{
7364632Seric 		/*
7374632Seric 		**  CHILD
7388148Seric 		**	Lock the control file to avoid duplicate deliveries.
7398148Seric 		**		Then run the file as though we had just read it.
7407350Seric 		**	We save an idea of the temporary name so we
7417350Seric 		**		can recover on interrupt.
7424632Seric 		*/
7434632Seric 
7447763Seric 		/* set basic modes, etc. */
7457356Seric 		(void) alarm(0);
74655012Seric 		clearenvelope(e, FALSE);
7474632Seric 		QueueRun = TRUE;
7489377Seric 		ErrorMode = EM_MAIL;
74955012Seric 		e->e_id = &w->w_name[2];
7507876Seric # ifdef LOG
75158020Seric 		if (LogLevel > 76)
75255012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7537881Seric 			       getpid());
75456795Seric # endif /* LOG */
7557763Seric 
7567763Seric 		/* don't use the headers from sendmail.cf... */
75755012Seric 		e->e_header = NULL;
7587763Seric 
75951920Seric 		/* read the queue control file -- return if locked */
76055012Seric 		if (!readqf(e))
7616980Seric 		{
76224941Seric 			if (ForkQueueRuns)
76324941Seric 				exit(EX_OK);
76424941Seric 			else
76524941Seric 				return;
7666980Seric 		}
7676980Seric 
76855012Seric 		e->e_flags |= EF_INQUEUE;
76957642Seric 		eatheader(e, TRUE);
7706980Seric 
7716980Seric 		/* do the delivery */
77255012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
77355012Seric 			sendall(e, SM_DELIVER);
7746980Seric 
7756980Seric 		/* finish up and exit */
77624941Seric 		if (ForkQueueRuns)
77724941Seric 			finis();
77824941Seric 		else
77955012Seric 			dropenvelope(e);
7804632Seric 	}
78124941Seric 	else
78224941Seric 	{
78324941Seric 		/*
78424941Seric 		**  Parent -- pick up results.
78524941Seric 		*/
7864632Seric 
78724941Seric 		errno = 0;
78824941Seric 		(void) waitfor(i);
78924941Seric 	}
7904632Seric }
7914632Seric /*
7924632Seric **  READQF -- read queue file and set up environment.
7934632Seric **
7944632Seric **	Parameters:
7959377Seric **		e -- the envelope of the job to run.
7964632Seric **
7974632Seric **	Returns:
79851920Seric **		TRUE if it successfully read the queue file.
79951920Seric **		FALSE otherwise.
8004632Seric **
8014632Seric **	Side Effects:
80251920Seric **		The queue file is returned locked.
8034632Seric */
8044632Seric 
80551920Seric bool
80651920Seric readqf(e)
8079377Seric 	register ENVELOPE *e;
8084632Seric {
80917477Seric 	char *qf;
81017477Seric 	register FILE *qfp;
81154974Seric 	ADDRESS *ctladdr;
81256400Seric 	struct stat st;
81357135Seric 	char *bp;
81457135Seric 	char buf[MAXLINE];
8159348Seric 	extern char *fgetfolded();
81624954Seric 	extern long atol();
81754974Seric 	extern ADDRESS *setctluser();
81851937Seric # ifdef LOCKF
81951937Seric 	struct flock lfd;
82051937Seric # endif
8214632Seric 
8224632Seric 	/*
82317468Seric 	**  Read and process the file.
8244632Seric 	*/
8254632Seric 
82617477Seric 	qf = queuename(e, 'q');
82751937Seric 	qfp = fopen(qf, "r+");
82817477Seric 	if (qfp == NULL)
82917477Seric 	{
83040934Srick 		if (errno != ENOENT)
83140934Srick 			syserr("readqf: no control file %s", qf);
83251920Seric 		return FALSE;
83317477Seric 	}
83440934Srick 
83556400Seric 	/*
83656400Seric 	**  Check the queue file for plausibility to avoid attacks.
83756400Seric 	*/
83856400Seric 
83956400Seric 	if (fstat(fileno(qfp), &st) < 0)
84056400Seric 	{
84156400Seric 		/* must have been being processed by someone else */
84256400Seric 		fclose(qfp);
84356400Seric 		return FALSE;
84456400Seric 	}
84556400Seric 
84657135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
84756400Seric 	{
84856400Seric # ifdef LOG
84956400Seric 		if (LogLevel > 0)
85056400Seric 		{
85156400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
85256400Seric 				e->e_id, st.st_uid, st.st_mode);
85356400Seric 		}
85456795Seric # endif /* LOG */
85556400Seric 		fclose(qfp);
85656400Seric 		return FALSE;
85756400Seric 	}
85856400Seric 
85951835Seric # ifdef LOCKF
86051937Seric 	lfd.l_type = F_WRLCK;
86151937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
86251937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
86351835Seric # else
86440934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
86551835Seric # endif
86640934Srick 	{
86758062Seric 		if (errno == EWOULDBLOCK)
86858062Seric 		{
86958062Seric 			/* being processed by another queuer */
87058062Seric 			if (Verbose)
87158062Seric 				printf("%s: locked\n", e->e_id);
87251920Seric # ifdef LOG
87358062Seric 			if (LogLevel > 19)
87458062Seric 				syslog(LOG_DEBUG, "%s: locked", e->e_id);
87556795Seric # endif /* LOG */
87658062Seric 		}
87758062Seric 		else
87858062Seric 		{
87958062Seric 			syserr("%s: flock failure", e->e_id);
88058062Seric 		}
88140934Srick 		(void) fclose(qfp);
88251920Seric 		return FALSE;
88340934Srick 	}
88440934Srick 
88551920Seric 	/* save this lock */
88651920Seric 	e->e_lockfp = qfp;
88751920Seric 
88840934Srick 	/* do basic system initialization */
88955012Seric 	initsys(e);
89040934Srick 
89117477Seric 	FileName = qf;
8929377Seric 	LineNumber = 0;
89351920Seric 	if (Verbose)
8949377Seric 		printf("\nRunning %s\n", e->e_id);
89554974Seric 	ctladdr = NULL;
89657135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
8974632Seric 	{
89857529Seric 		struct stat st;
89957529Seric 
90026504Seric 		if (tTd(40, 4))
90157135Seric 			printf("+++++ %s\n", bp);
90257135Seric 		switch (bp[0])
9034632Seric 		{
90440973Sbostic 		  case 'C':		/* specify controlling user */
90557135Seric 			ctladdr = setctluser(&bp[1]);
90640973Sbostic 			break;
90740973Sbostic 
9084632Seric 		  case 'R':		/* specify recipient */
90958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9104632Seric 			break;
9114632Seric 
91225687Seric 		  case 'E':		/* specify error recipient */
91358082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
91425687Seric 			break;
91525687Seric 
9164632Seric 		  case 'H':		/* header */
91757135Seric 			(void) chompheader(&bp[1], FALSE, e);
9184632Seric 			break;
9194632Seric 
92010108Seric 		  case 'M':		/* message */
92157135Seric 			e->e_message = newstr(&bp[1]);
92210108Seric 			break;
92310108Seric 
9244632Seric 		  case 'S':		/* sender */
92557135Seric 			setsender(newstr(&bp[1]), e);
9264632Seric 			break;
9274632Seric 
9284632Seric 		  case 'D':		/* data file name */
92957135Seric 			e->e_df = newstr(&bp[1]);
9309544Seric 			e->e_dfp = fopen(e->e_df, "r");
9319544Seric 			if (e->e_dfp == NULL)
93258020Seric 			{
9339377Seric 				syserr("readqf: cannot open %s", e->e_df);
93458020Seric 				e->e_msgsize = -1;
93558020Seric 			}
93658020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
93757529Seric 				e->e_msgsize = st.st_size;
9384632Seric 			break;
9394632Seric 
9407860Seric 		  case 'T':		/* init time */
94157135Seric 			e->e_ctime = atol(&bp[1]);
9424632Seric 			break;
9434632Seric 
9444634Seric 		  case 'P':		/* message priority */
94557135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9464634Seric 			break;
9474634Seric 
94853400Seric 		  case '$':		/* define macro */
94957135Seric 			define(bp[1], newstr(&bp[2]), e);
95053400Seric 			break;
95153400Seric 
95224941Seric 		  case '\0':		/* blank line; ignore */
95324941Seric 			break;
95424941Seric 
9554632Seric 		  default:
95624941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
95757135Seric 				LineNumber, bp);
9584632Seric 			break;
9594632Seric 		}
96057135Seric 
96157135Seric 		if (bp != buf)
96257135Seric 			free(bp);
9634632Seric 	}
9649377Seric 
9659377Seric 	FileName = NULL;
96624941Seric 
96724941Seric 	/*
96824941Seric 	**  If we haven't read any lines, this queue file is empty.
96924941Seric 	**  Arrange to remove it without referencing any null pointers.
97024941Seric 	*/
97124941Seric 
97224941Seric 	if (LineNumber == 0)
97324941Seric 	{
97424941Seric 		errno = 0;
97524941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
97624941Seric 	}
97751920Seric 	return TRUE;
9784632Seric }
9794632Seric /*
9809630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9819630Seric **
9829630Seric **	Parameters:
9839630Seric **		none.
9849630Seric **
9859630Seric **	Returns:
9869630Seric **		none.
9879630Seric **
9889630Seric **	Side Effects:
9899630Seric **		Prints a listing of the mail queue on the standard output.
9909630Seric */
9915182Seric 
9929630Seric printqueue()
9939630Seric {
9949630Seric 	register WORK *w;
9959630Seric 	FILE *f;
99610070Seric 	int nrequests;
9979630Seric 	char buf[MAXLINE];
9989630Seric 
9999630Seric 	/*
10009630Seric 	**  Read and order the queue.
10019630Seric 	*/
10029630Seric 
100324941Seric 	nrequests = orderq(TRUE);
10049630Seric 
10059630Seric 	/*
10069630Seric 	**  Print the work list that we have read.
10079630Seric 	*/
10089630Seric 
10099630Seric 	/* first see if there is anything */
101010070Seric 	if (nrequests <= 0)
10119630Seric 	{
101210070Seric 		printf("Mail queue is empty\n");
10139630Seric 		return;
10149630Seric 	}
10159630Seric 
101651920Seric 	CurrentLA = getla();	/* get load average */
101740934Srick 
101810096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
101925687Seric 	if (nrequests > QUEUESIZE)
102025687Seric 		printf(", only %d printed", QUEUESIZE);
102124979Seric 	if (Verbose)
102225032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
102324979Seric 	else
102424979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10259630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10269630Seric 	{
10279630Seric 		struct stat st;
102810070Seric 		auto time_t submittime = 0;
102910070Seric 		long dfsize = -1;
103010108Seric 		char message[MAXLINE];
103151937Seric # ifdef LOCKF
103251937Seric 		struct flock lfd;
103351937Seric # endif
103424941Seric 		extern bool shouldqueue();
10359630Seric 
103617468Seric 		f = fopen(w->w_name, "r");
103717468Seric 		if (f == NULL)
103817468Seric 		{
103917468Seric 			errno = 0;
104017468Seric 			continue;
104117468Seric 		}
10429630Seric 		printf("%7s", w->w_name + 2);
104351835Seric # ifdef LOCKF
104451937Seric 		lfd.l_type = F_RDLCK;
104551937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
104651937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
104751835Seric # else
104840934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
104951835Seric # endif
105010070Seric 			printf("*");
105157438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
105224941Seric 			printf("X");
105310070Seric 		else
105410070Seric 			printf(" ");
105510070Seric 		errno = 0;
105617468Seric 
105710108Seric 		message[0] = '\0';
10589630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10599630Seric 		{
106053400Seric 			register int i;
106153400Seric 
10629630Seric 			fixcrlf(buf, TRUE);
10639630Seric 			switch (buf[0])
10649630Seric 			{
106510108Seric 			  case 'M':	/* error message */
106653400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
106753400Seric 					i = sizeof message;
106853400Seric 				bcopy(&buf[1], message, i);
106953400Seric 				message[i] = '\0';
107010108Seric 				break;
107110108Seric 
10729630Seric 			  case 'S':	/* sender name */
107324979Seric 				if (Verbose)
107425027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
107525027Seric 					    w->w_pri, ctime(&submittime) + 4,
107624979Seric 					    &buf[1]);
107724979Seric 				else
107824979Seric 					printf("%8ld %.16s %.45s", dfsize,
107924979Seric 					    ctime(&submittime), &buf[1]);
108010108Seric 				if (message[0] != '\0')
108125027Seric 					printf("\n\t\t (%.60s)", message);
10829630Seric 				break;
108351920Seric 
108440973Sbostic 			  case 'C':	/* controlling user */
108554974Seric 				if (Verbose)
108654975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
108740973Sbostic 				break;
10889630Seric 
10899630Seric 			  case 'R':	/* recipient name */
109024979Seric 				if (Verbose)
109125027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
109224979Seric 				else
109324979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
10949630Seric 				break;
10959630Seric 
10969630Seric 			  case 'T':	/* creation time */
109724941Seric 				submittime = atol(&buf[1]);
10989630Seric 				break;
109910070Seric 
110010070Seric 			  case 'D':	/* data file name */
110110070Seric 				if (stat(&buf[1], &st) >= 0)
110210070Seric 					dfsize = st.st_size;
110310070Seric 				break;
11049630Seric 			}
11059630Seric 		}
110610070Seric 		if (submittime == (time_t) 0)
110710070Seric 			printf(" (no control file)");
11089630Seric 		printf("\n");
110923098Seric 		(void) fclose(f);
11109630Seric 	}
11119630Seric }
11129630Seric 
111356795Seric # endif /* QUEUE */
111417468Seric /*
111517468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
111617468Seric **
111717468Seric **	Assigns an id code if one does not already exist.
111817468Seric **	This code is very careful to avoid trashing existing files
111917468Seric **	under any circumstances.
112017468Seric **
112117468Seric **	Parameters:
112217468Seric **		e -- envelope to build it in/from.
112317468Seric **		type -- the file type, used as the first character
112417468Seric **			of the file name.
112517468Seric **
112617468Seric **	Returns:
112717468Seric **		a pointer to the new file name (in a static buffer).
112817468Seric **
112917468Seric **	Side Effects:
113051920Seric **		If no id code is already assigned, queuename will
113151920Seric **		assign an id code, create a qf file, and leave a
113251920Seric **		locked, open-for-write file pointer in the envelope.
113317468Seric */
113417468Seric 
113517468Seric char *
113617468Seric queuename(e, type)
113717468Seric 	register ENVELOPE *e;
113817468Seric 	char type;
113917468Seric {
114017468Seric 	static char buf[MAXNAME];
114117468Seric 	static int pid = -1;
114217468Seric 	char c1 = 'A';
114317468Seric 	char c2 = 'A';
114417468Seric 
114517468Seric 	if (e->e_id == NULL)
114617468Seric 	{
114717468Seric 		char qf[20];
114817468Seric 
114917468Seric 		/* find a unique id */
115017468Seric 		if (pid != getpid())
115117468Seric 		{
115217468Seric 			/* new process -- start back at "AA" */
115317468Seric 			pid = getpid();
115417468Seric 			c1 = 'A';
115517468Seric 			c2 = 'A' - 1;
115617468Seric 		}
115717468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
115817468Seric 
115917468Seric 		while (c1 < '~' || c2 < 'Z')
116017468Seric 		{
116117468Seric 			int i;
116251937Seric # ifdef LOCKF
116351937Seric 			struct flock lfd;
116451937Seric # endif
116517468Seric 
116617468Seric 			if (c2 >= 'Z')
116717468Seric 			{
116817468Seric 				c1++;
116917468Seric 				c2 = 'A' - 1;
117017468Seric 			}
117140934Srick 			qf[2] = c1;
117240934Srick 			qf[3] = ++c2;
117317468Seric 			if (tTd(7, 20))
117440934Srick 				printf("queuename: trying \"%s\"\n", qf);
117517468Seric 
117640934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
117751920Seric 			if (i < 0)
117851920Seric 			{
117951920Seric 				if (errno == EEXIST)
118051920Seric 					continue;
118151920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
118251920Seric 					qf, QueueDir);
118351920Seric 				exit(EX_UNAVAILABLE);
118451920Seric 			}
118551920Seric # ifdef LOCKF
118651937Seric 			lfd.l_type = F_WRLCK;
118751937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
118851937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
118951920Seric # else
119051920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
119151920Seric # endif
119251920Seric 			{
119351920Seric 				e->e_lockfp = fdopen(i, "w");
119440934Srick 				break;
119517468Seric 			}
119651920Seric 
119751920Seric 			/* a reader got the file; abandon it and try again */
119851920Seric 			(void) close(i);
119917468Seric 		}
120017468Seric 		if (c1 >= '~' && c2 >= 'Z')
120117468Seric 		{
120217468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
120317468Seric 				qf, QueueDir);
120417468Seric 			exit(EX_OSERR);
120517468Seric 		}
120617468Seric 		e->e_id = newstr(&qf[2]);
120717468Seric 		define('i', e->e_id, e);
120817468Seric 		if (tTd(7, 1))
120917468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
121017468Seric # ifdef LOG
121158020Seric 		if (LogLevel > 93)
121217468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
121356795Seric # endif /* LOG */
121417468Seric 	}
121517468Seric 
121617468Seric 	if (type == '\0')
121717468Seric 		return (NULL);
121817468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
121917468Seric 	if (tTd(7, 2))
122017468Seric 		printf("queuename: %s\n", buf);
122117468Seric 	return (buf);
122217468Seric }
122317468Seric /*
122417468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
122517468Seric **
122617468Seric **	Parameters:
122717468Seric **		e -- the envelope to unlock.
122817468Seric **
122917468Seric **	Returns:
123017468Seric **		none
123117468Seric **
123217468Seric **	Side Effects:
123317468Seric **		unlocks the queue for `e'.
123417468Seric */
123517468Seric 
123617468Seric unlockqueue(e)
123717468Seric 	ENVELOPE *e;
123817468Seric {
123951920Seric 	/* if there is a lock file in the envelope, close it */
124051920Seric 	if (e->e_lockfp != NULL)
124151920Seric 		fclose(e->e_lockfp);
124251920Seric 	e->e_lockfp = NULL;
124351920Seric 
124417468Seric 	/* remove the transcript */
124517468Seric # ifdef LOG
124658020Seric 	if (LogLevel > 87)
124717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
124856795Seric # endif /* LOG */
124917468Seric 	if (!tTd(51, 4))
125017468Seric 		xunlink(queuename(e, 'x'));
125117468Seric 
125217468Seric }
125340973Sbostic /*
125454974Seric **  SETCTLUSER -- create a controlling address
125540973Sbostic **
125654974Seric **	Create a fake "address" given only a local login name; this is
125754974Seric **	used as a "controlling user" for future recipient addresses.
125840973Sbostic **
125940973Sbostic **	Parameters:
126054974Seric **		user -- the user name of the controlling user.
126140973Sbostic **
126240973Sbostic **	Returns:
126354974Seric **		An address descriptor for the controlling user.
126440973Sbostic **
126540973Sbostic **	Side Effects:
126640973Sbostic **		none.
126740973Sbostic */
126840973Sbostic 
126954974Seric ADDRESS *
127054974Seric setctluser(user)
127154974Seric 	char *user;
127240973Sbostic {
127354974Seric 	register ADDRESS *a;
127440973Sbostic 	struct passwd *pw;
127540973Sbostic 
127640973Sbostic 	/*
127754974Seric 	**  See if this clears our concept of controlling user.
127840973Sbostic 	*/
127940973Sbostic 
128054974Seric 	if (user == NULL || *user == '\0')
128154974Seric 		return NULL;
128240973Sbostic 
128340973Sbostic 	/*
128454974Seric 	**  Set up addr fields for controlling user.
128540973Sbostic 	*/
128640973Sbostic 
128754974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
128854974Seric 	bzero((char *) a, sizeof *a);
128954974Seric 	if ((pw = getpwnam(user)) != NULL)
129040973Sbostic 	{
129140973Sbostic 		a->q_home = newstr(pw->pw_dir);
129240973Sbostic 		a->q_uid = pw->pw_uid;
129340973Sbostic 		a->q_gid = pw->pw_gid;
129457642Seric 		a->q_user = newstr(user);
129540973Sbostic 	}
129640973Sbostic 	else
129740973Sbostic 	{
129840973Sbostic 		a->q_uid = DefUid;
129940973Sbostic 		a->q_gid = DefGid;
130057642Seric 		a->q_user = newstr(DefUser);
130140973Sbostic 	}
130240973Sbostic 
130340973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
130456328Seric 	a->q_mailer = LocalMailer;
130554974Seric 	return a;
130640973Sbostic }
1307