xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 57736)
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*57736Seric static char sccsid[] = "@(#)queue.c	6.7 (Berkeley) 01/28/93 (with queueing)";
1433731Sbostic #else
15*57736Seric static char sccsid[] = "@(#)queue.c	6.7 (Berkeley) 01/28/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
2140934Srick # include <sys/file.h>
224634Seric # include <signal.h>
234632Seric # include <errno.h>
2440973Sbostic # include <pwd.h>
2551937Seric # ifdef LOCKF
2651937Seric # include <fcntl.h>
2751937Seric # endif
28*57736Seric # ifdef SYSTEM5
29*57736Seric # include <dirent.h>
30*57736Seric # endif
314632Seric 
3233731Sbostic # ifdef QUEUE
334632Seric 
344632Seric /*
359377Seric **  Work queue.
369377Seric */
379377Seric 
389377Seric struct work
399377Seric {
409377Seric 	char		*w_name;	/* name of control file */
419377Seric 	long		w_pri;		/* priority of message, see below */
4225013Seric 	time_t		w_ctime;	/* creation time of message */
439377Seric 	struct work	*w_next;	/* next in queue */
449377Seric };
459377Seric 
469377Seric typedef struct work	WORK;
479377Seric 
489377Seric WORK	*WorkQ;			/* queue of things to be done */
499377Seric /*
504632Seric **  QUEUEUP -- queue a message up for future transmission.
514632Seric **
524632Seric **	Parameters:
536980Seric **		e -- the envelope to queue up.
546999Seric **		queueall -- if TRUE, queue all addresses, rather than
556999Seric **			just those with the QQUEUEUP flag set.
569377Seric **		announce -- if TRUE, tell when you are queueing up.
574632Seric **
584632Seric **	Returns:
5951920Seric **		none.
604632Seric **
614632Seric **	Side Effects:
629377Seric **		The current request are saved in a control file.
6351920Seric **		The queue file is left locked.
644632Seric */
654632Seric 
669377Seric queueup(e, queueall, announce)
676980Seric 	register ENVELOPE *e;
686999Seric 	bool queueall;
699377Seric 	bool announce;
704632Seric {
717812Seric 	char *qf;
727812Seric 	register FILE *tfp;
734632Seric 	register HDR *h;
745007Seric 	register ADDRESS *q;
7551920Seric 	int fd;
7651920Seric 	int i;
7751920Seric 	bool newid;
7853400Seric 	register char *p;
7910173Seric 	MAILER nullmailer;
8054974Seric 	ADDRESS *lastctladdr;
8154975Seric 	static ADDRESS *nullctladdr = NULL;
8251920Seric 	char buf[MAXLINE], tf[MAXLINE];
8353400Seric 	extern char *macvalue();
8454974Seric 	extern ADDRESS *getctladdr();
854632Seric 
865037Seric 	/*
8754975Seric 	**  If we don't have nullctladdr, create one
8854975Seric 	*/
8954975Seric 
9054975Seric 	if (nullctladdr == NULL)
9154975Seric 	{
9254975Seric 		nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr);
9354975Seric 		bzero((char *) nullctladdr, sizeof nullctladdr);
9454975Seric 	}
9554975Seric 
9654975Seric 	/*
9717477Seric 	**  Create control file.
985037Seric 	*/
994632Seric 
10051920Seric 	newid = (e->e_id == NULL);
10151920Seric 	strcpy(tf, queuename(e, 't'));
10251920Seric 	tfp = e->e_lockfp;
10351920Seric 	if (tfp == NULL)
10451920Seric 		newid = FALSE;
10551920Seric 	if (newid)
10651835Seric 	{
10751920Seric 		tfp = e->e_lockfp;
10851920Seric 	}
10951920Seric 	else
11051920Seric 	{
11151920Seric 		/* get a locked tf file */
11251920Seric 		for (i = 100; --i >= 0; )
11351835Seric 		{
11451937Seric # ifdef LOCKF
11551937Seric 			struct flock lfd;
11651937Seric # endif
11751937Seric 
11851920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
11951920Seric 			if (fd < 0)
12051835Seric 			{
12151920Seric 				if (errno == EEXIST)
12251920Seric 					continue;
12351920Seric 				syserr("queueup: cannot create temp file %s", tf);
12451920Seric 				return;
12541636Srick 			}
12651835Seric # ifdef LOCKF
12751937Seric 			lfd.l_type = F_WRLCK;
12851937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
12951937Seric 			if (fcntl(fd, F_SETLK, &lfd) >= 0)
13051920Seric 				break;
13151920Seric 			if (errno != EACCES && errno != EAGAIN)
13251920Seric 				syserr("cannot lockf(%s)", tf);
13351835Seric # else
13451920Seric 			if (flock(fd, LOCK_EX|LOCK_NB) >= 0)
13551920Seric 				break;
13651920Seric 			if (errno != EWOULDBLOCK)
13751920Seric 				syserr("cannot flock(%s)", tf);
13851835Seric # endif
13951920Seric 			close(fd);
14041636Srick 		}
14141636Srick 
14251920Seric 		tfp = fdopen(fd, "w");
14351920Seric 	}
1444632Seric 
1457677Seric 	if (tTd(40, 1))
14617468Seric 		printf("queueing %s\n", e->e_id);
1474632Seric 
1484632Seric 	/*
1496980Seric 	**  If there is no data file yet, create one.
1506980Seric 	*/
1516980Seric 
1526980Seric 	if (e->e_df == NULL)
1536980Seric 	{
1546980Seric 		register FILE *dfp;
1559389Seric 		extern putbody();
1566980Seric 
1577812Seric 		e->e_df = newstr(queuename(e, 'd'));
15840934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15940934Srick 		if (fd < 0)
1606980Seric 		{
1616980Seric 			syserr("queueup: cannot create %s", e->e_df);
16251920Seric 			if (!newid)
16351920Seric 				(void) fclose(tfp);
16451920Seric 			return;
1656980Seric 		}
16640934Srick 		dfp = fdopen(fd, "w");
16710173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1687009Seric 		(void) fclose(dfp);
1699389Seric 		e->e_putbody = putbody;
1706980Seric 	}
1716980Seric 
1726980Seric 	/*
1734632Seric 	**  Output future work requests.
17425687Seric 	**	Priority and creation time should be first, since
17525687Seric 	**	they are required by orderq.
1764632Seric 	*/
1774632Seric 
1789377Seric 	/* output message priority */
1799377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1809377Seric 
1819630Seric 	/* output creation time */
1829630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1839630Seric 
1844632Seric 	/* output name of data file */
1857812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1864632Seric 
18710108Seric 	/* message from envelope, if it exists */
18810108Seric 	if (e->e_message != NULL)
18910108Seric 		fprintf(tfp, "M%s\n", e->e_message);
19010108Seric 
19153400Seric 	/* $r and $s macro values */
19253400Seric 	if ((p = macvalue('r', e)) != NULL)
19353400Seric 		fprintf(tfp, "$r%s\n", p);
19453400Seric 	if ((p = macvalue('s', e)) != NULL)
19553400Seric 		fprintf(tfp, "$s%s\n", p);
19653400Seric 
1974632Seric 	/* output name of sender */
1987812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1994632Seric 
20055360Seric 	/* output list of error recipients */
20155360Seric 	lastctladdr = NULL;
20255360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20355360Seric 	{
20455360Seric 		if (!bitset(QDONTSEND, q->q_flags))
20555360Seric 		{
20655360Seric 			ADDRESS *ctladdr;
20755360Seric 
20855360Seric 			ctladdr = getctladdr(q);
20955360Seric 			if (ctladdr == NULL && q->q_alias != NULL)
21055360Seric 				ctladdr = nullctladdr;
21155360Seric 			if (ctladdr != lastctladdr)
21255360Seric 			{
21355360Seric 				printctladdr(ctladdr, tfp);
21455360Seric 				lastctladdr = ctladdr;
21555360Seric 			}
21655360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21755360Seric 		}
21855360Seric 	}
21955360Seric 
2204632Seric 	/* output list of recipient addresses */
2216980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2224632Seric 	{
22347284Seric 		if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) :
2247763Seric 			       bitset(QQUEUEUP, q->q_flags))
2258245Seric 		{
22654974Seric 			ADDRESS *ctladdr;
22740973Sbostic 
22854975Seric 			ctladdr = getctladdr(q);
22954975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
23054975Seric 				ctladdr = nullctladdr;
23154975Seric 			if (ctladdr != lastctladdr)
23254974Seric 			{
23354974Seric 				printctladdr(ctladdr, tfp);
23454974Seric 				lastctladdr = ctladdr;
23554974Seric 			}
2367812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2379377Seric 			if (announce)
2389377Seric 			{
2399377Seric 				e->e_to = q->q_paddr;
2409377Seric 				message(Arpa_Info, "queued");
2419377Seric 				if (LogLevel > 4)
24254967Seric 					logdelivery("queued", e);
2439377Seric 				e->e_to = NULL;
2449377Seric 			}
2459387Seric 			if (tTd(40, 1))
2469387Seric 			{
2479387Seric 				printf("queueing ");
2489387Seric 				printaddr(q, FALSE);
2499387Seric 			}
2508245Seric 		}
2514632Seric 	}
2524632Seric 
2539377Seric 	/*
2549377Seric 	**  Output headers for this message.
2559377Seric 	**	Expand macros completely here.  Queue run will deal with
2569377Seric 	**	everything as absolute headers.
2579377Seric 	**		All headers that must be relative to the recipient
2589377Seric 	**		can be cracked later.
25910173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
26010173Seric 	**	no effect on the addresses as they are output.
2619377Seric 	*/
2629377Seric 
26310686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
26410173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
26510349Seric 	nullmailer.m_eol = "\n";
26610173Seric 
26716147Seric 	define('g', "\001f", e);
2686980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2694632Seric 	{
27010686Seric 		extern bool bitzerop();
27110686Seric 
27212015Seric 		/* don't output null headers */
2734632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2744632Seric 			continue;
27512015Seric 
27612015Seric 		/* don't output resent headers on non-resent messages */
27712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
27812015Seric 			continue;
27912015Seric 
28012015Seric 		/* output this header */
2817812Seric 		fprintf(tfp, "H");
28212015Seric 
28312015Seric 		/* if conditional, output the set of conditions */
28410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
28510686Seric 		{
28610686Seric 			int j;
28710686Seric 
28823098Seric 			(void) putc('?', tfp);
28910686Seric 			for (j = '\0'; j <= '\177'; j++)
29010686Seric 				if (bitnset(j, h->h_mflags))
29123098Seric 					(void) putc(j, tfp);
29223098Seric 			(void) putc('?', tfp);
29310686Seric 		}
29412015Seric 
29512015Seric 		/* output the header: expand macros, convert addresses */
2967763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2977763Seric 		{
2987763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2998236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3007763Seric 		}
3018245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3029348Seric 		{
30355012Seric 			commaize(h, h->h_value, tfp,
30455012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
30555012Seric 				 &nullmailer, e);
3069348Seric 		}
3077763Seric 		else
3088245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3094632Seric 	}
3104632Seric 
3114632Seric 	/*
3124632Seric 	**  Clean up.
3134632Seric 	*/
3144632Seric 
31551920Seric 	if (!newid)
31651920Seric 	{
31751920Seric 		qf = queuename(e, 'q');
31851920Seric 		if (rename(tf, qf) < 0)
31951920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
32051920Seric 		if (e->e_lockfp != NULL)
32151920Seric 			(void) fclose(e->e_lockfp);
32251920Seric 		e->e_lockfp = tfp;
32351920Seric 	}
32451920Seric 	else
32551920Seric 		qf = tf;
32641636Srick 	errno = 0;
3277391Seric 
3287677Seric # ifdef LOG
3297677Seric 	/* save log info */
3307878Seric 	if (LogLevel > 15)
3317878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
33256795Seric # endif /* LOG */
33340934Srick 	fflush(tfp);
33451920Seric 	return;
3354632Seric }
33654974Seric 
33754974Seric printctladdr(a, tfp)
33854974Seric 	ADDRESS *a;
33954974Seric 	FILE *tfp;
34054974Seric {
34154974Seric 	char *u;
34254974Seric 	struct passwd *pw;
34354974Seric 	extern struct passwd *getpwuid();
34454974Seric 
34554974Seric 	if (a == NULL)
34654974Seric 	{
34754974Seric 		fprintf(tfp, "C\n");
34854974Seric 		return;
34954974Seric 	}
35054974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
35154974Seric 		u = DefUser;
35254974Seric 	else
35354974Seric 		u = pw->pw_name;
35454974Seric 	fprintf(tfp, "C%s\n", u);
35554974Seric }
35654974Seric 
3574632Seric /*
3584632Seric **  RUNQUEUE -- run the jobs in the queue.
3594632Seric **
3604632Seric **	Gets the stuff out of the queue in some presumably logical
3614632Seric **	order and processes them.
3624632Seric **
3634632Seric **	Parameters:
36424941Seric **		forkflag -- TRUE if the queue scanning should be done in
36524941Seric **			a child process.  We double-fork so it is not our
36624941Seric **			child and we don't have to clean up after it.
3674632Seric **
3684632Seric **	Returns:
3694632Seric **		none.
3704632Seric **
3714632Seric **	Side Effects:
3724632Seric **		runs things in the mail queue.
3734632Seric */
3744632Seric 
37555360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37655360Seric 
37755360Seric runqueue(forkflag)
3784639Seric 	bool forkflag;
3794632Seric {
38024953Seric 	extern bool shouldqueue();
38155360Seric 	register ENVELOPE *e;
38255360Seric 	extern ENVELOPE BlankEnvelope;
38355360Seric 	extern ENVELOPE *newenvelope();
38424953Seric 
3857466Seric 	/*
38624953Seric 	**  If no work will ever be selected, don't even bother reading
38724953Seric 	**  the queue.
38824953Seric 	*/
38924953Seric 
39051920Seric 	CurrentLA = getla();	/* get load average */
39140934Srick 
39257438Seric 	if (shouldqueue(-100000000L, curtime()))
39324953Seric 	{
39424953Seric 		if (Verbose)
39524953Seric 			printf("Skipping queue run -- load average too high\n");
39655360Seric 		return;
39724953Seric 	}
39824953Seric 
39924953Seric 	/*
4007466Seric 	**  See if we want to go off and do other useful work.
4017466Seric 	*/
4024639Seric 
4034639Seric 	if (forkflag)
4044639Seric 	{
4057943Seric 		int pid;
4067943Seric 
4077943Seric 		pid = dofork();
4087943Seric 		if (pid != 0)
4094639Seric 		{
41046928Sbostic 			extern void reapchild();
41125184Seric 
4127943Seric 			/* parent -- pick up intermediate zombie */
41325184Seric #ifndef SIGCHLD
4149377Seric 			(void) waitfor(pid);
41556795Seric #else /* SIGCHLD */
41625184Seric 			(void) signal(SIGCHLD, reapchild);
41756795Seric #endif /* SIGCHLD */
4187690Seric 			if (QueueIntvl != 0)
4199348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4204639Seric 			return;
4214639Seric 		}
4227943Seric 		/* child -- double fork */
42325184Seric #ifndef SIGCHLD
4247943Seric 		if (fork() != 0)
4257943Seric 			exit(EX_OK);
42656795Seric #else /* SIGCHLD */
42725184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42856795Seric #endif /* SIGCHLD */
4294639Seric 	}
43024941Seric 
43140934Srick 	setproctitle("running queue: %s", QueueDir);
43257731Seric 	ForceMail = TRUE;
43324941Seric 
4347876Seric # ifdef LOG
4357876Seric 	if (LogLevel > 11)
43655360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43755360Seric 			QueueDir, getpid(), forkflag);
43856795Seric # endif /* LOG */
4394639Seric 
4407466Seric 	/*
44110205Seric 	**  Release any resources used by the daemon code.
44210205Seric 	*/
44310205Seric 
44410205Seric # ifdef DAEMON
44510205Seric 	clrdaemon();
44656795Seric # endif /* DAEMON */
44710205Seric 
44810205Seric 	/*
44955360Seric 	**  Create ourselves an envelope
45055360Seric 	*/
45155360Seric 
45255360Seric 	CurEnv = &QueueEnvelope;
45355360Seric 	e = newenvelope(&QueueEnvelope);
45455360Seric 	e->e_flags = BlankEnvelope.e_flags;
45555360Seric 
45655360Seric 	/*
45727175Seric 	**  Make sure the alias database is open.
45827175Seric 	*/
45927175Seric 
46055012Seric 	initaliases(AliasFile, FALSE, e);
46127175Seric 
46227175Seric 	/*
4637466Seric 	**  Start making passes through the queue.
4647466Seric 	**	First, read and sort the entire queue.
4657466Seric 	**	Then, process the work in that order.
4667466Seric 	**		But if you take too long, start over.
4677466Seric 	*/
4687466Seric 
4697943Seric 	/* order the existing work requests */
47024954Seric 	(void) orderq(FALSE);
4717690Seric 
4727943Seric 	/* process them once at a time */
4737943Seric 	while (WorkQ != NULL)
4744639Seric 	{
4757943Seric 		WORK *w = WorkQ;
4767881Seric 
4777943Seric 		WorkQ = WorkQ->w_next;
47855012Seric 		dowork(w, e);
4797943Seric 		free(w->w_name);
4807943Seric 		free((char *) w);
4814639Seric 	}
48229866Seric 
48329866Seric 	/* exit without the usual cleanup */
48455467Seric 	e->e_id = NULL;
48555467Seric 	finis();
4864634Seric }
4874634Seric /*
4884632Seric **  ORDERQ -- order the work queue.
4894632Seric **
4904632Seric **	Parameters:
49124941Seric **		doall -- if set, include everything in the queue (even
49224941Seric **			the jobs that cannot be run because the load
49324941Seric **			average is too high).  Otherwise, exclude those
49424941Seric **			jobs.
4954632Seric **
4964632Seric **	Returns:
49710121Seric **		The number of request in the queue (not necessarily
49810121Seric **		the number of requests in WorkQ however).
4994632Seric **
5004632Seric **	Side Effects:
5014632Seric **		Sets WorkQ to the queue of available work, in order.
5024632Seric */
5034632Seric 
50425687Seric # define NEED_P		001
50525687Seric # define NEED_T		002
5064632Seric 
50724941Seric orderq(doall)
50824941Seric 	bool doall;
5094632Seric {
5106625Sglickman 	register struct direct *d;
5114632Seric 	register WORK *w;
5126625Sglickman 	DIR *f;
5134632Seric 	register int i;
51425687Seric 	WORK wlist[QUEUESIZE+1];
51510070Seric 	int wn = -1;
5164632Seric 	extern workcmpf();
5174632Seric 
5184632Seric 	/* clear out old WorkQ */
5194632Seric 	for (w = WorkQ; w != NULL; )
5204632Seric 	{
5214632Seric 		register WORK *nw = w->w_next;
5224632Seric 
5234632Seric 		WorkQ = nw;
5244632Seric 		free(w->w_name);
5254632Seric 		free((char *) w);
5264632Seric 		w = nw;
5274632Seric 	}
5284632Seric 
5294632Seric 	/* open the queue directory */
5308148Seric 	f = opendir(".");
5314632Seric 	if (f == NULL)
5324632Seric 	{
5338148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
53410070Seric 		return (0);
5354632Seric 	}
5364632Seric 
5374632Seric 	/*
5384632Seric 	**  Read the work directory.
5394632Seric 	*/
5404632Seric 
54110070Seric 	while ((d = readdir(f)) != NULL)
5424632Seric 	{
5439377Seric 		FILE *cf;
5444632Seric 		char lbuf[MAXNAME];
54557642Seric 		extern bool shouldqueue();
5464632Seric 
5474632Seric 		/* is this an interesting entry? */
5487812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5494632Seric 			continue;
5504632Seric 
55110070Seric 		/* yes -- open control file (if not too many files) */
55225687Seric 		if (++wn >= QUEUESIZE)
55310070Seric 			continue;
5548148Seric 		cf = fopen(d->d_name, "r");
5554632Seric 		if (cf == NULL)
5564632Seric 		{
5577055Seric 			/* this may be some random person sending hir msgs */
5587055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
55910090Seric 			if (tTd(41, 2))
56010090Seric 				printf("orderq: cannot open %s (%d)\n",
56110090Seric 					d->d_name, errno);
5627055Seric 			errno = 0;
56310090Seric 			wn--;
5644632Seric 			continue;
5654632Seric 		}
56625687Seric 		w = &wlist[wn];
56725687Seric 		w->w_name = newstr(d->d_name);
5684632Seric 
56925027Seric 		/* make sure jobs in creation don't clog queue */
57025687Seric 		w->w_pri = 0x7fffffff;
57125687Seric 		w->w_ctime = 0;
57225027Seric 
5734632Seric 		/* extract useful information */
57425687Seric 		i = NEED_P | NEED_T;
57525687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5764632Seric 		{
57724954Seric 			extern long atol();
57824954Seric 
57924941Seric 			switch (lbuf[0])
5804632Seric 			{
58124941Seric 			  case 'P':
58225687Seric 				w->w_pri = atol(&lbuf[1]);
58325687Seric 				i &= ~NEED_P;
5844632Seric 				break;
58525013Seric 
58625013Seric 			  case 'T':
58725687Seric 				w->w_ctime = atol(&lbuf[1]);
58825687Seric 				i &= ~NEED_T;
58925013Seric 				break;
5904632Seric 			}
5914632Seric 		}
5924632Seric 		(void) fclose(cf);
59324953Seric 
59457438Seric 		if (!doall && shouldqueue(w->w_pri, w->w_ctime))
59524953Seric 		{
59624953Seric 			/* don't even bother sorting this job in */
59724953Seric 			wn--;
59824953Seric 		}
5994632Seric 	}
6006625Sglickman 	(void) closedir(f);
60110090Seric 	wn++;
6024632Seric 
6034632Seric 	/*
6044632Seric 	**  Sort the work directory.
6054632Seric 	*/
6064632Seric 
60725687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6084632Seric 
6094632Seric 	/*
6104632Seric 	**  Convert the work list into canonical form.
6119377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6124632Seric 	*/
6134632Seric 
61424981Seric 	WorkQ = NULL;
61525687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6164632Seric 	{
6174632Seric 		w = (WORK *) xalloc(sizeof *w);
6184632Seric 		w->w_name = wlist[i].w_name;
6194632Seric 		w->w_pri = wlist[i].w_pri;
62025013Seric 		w->w_ctime = wlist[i].w_ctime;
62124981Seric 		w->w_next = WorkQ;
62224981Seric 		WorkQ = w;
6234632Seric 	}
6244632Seric 
6257677Seric 	if (tTd(40, 1))
6264632Seric 	{
6274632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6285037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6294632Seric 	}
63010070Seric 
63110090Seric 	return (wn);
6324632Seric }
6334632Seric /*
6347677Seric **  WORKCMPF -- compare function for ordering work.
6354632Seric **
6364632Seric **	Parameters:
6374632Seric **		a -- the first argument.
6384632Seric **		b -- the second argument.
6394632Seric **
6404632Seric **	Returns:
64124981Seric **		-1 if a < b
64224981Seric **		 0 if a == b
64324981Seric **		+1 if a > b
6444632Seric **
6454632Seric **	Side Effects:
6464632Seric **		none.
6474632Seric */
6484632Seric 
6494632Seric workcmpf(a, b)
6505037Seric 	register WORK *a;
6515037Seric 	register WORK *b;
6524632Seric {
65357438Seric 	long pa = a->w_pri;
65457438Seric 	long pb = b->w_pri;
65524941Seric 
65624941Seric 	if (pa == pb)
6574632Seric 		return (0);
65824941Seric 	else if (pa > pb)
65924981Seric 		return (1);
66024981Seric 	else
66110121Seric 		return (-1);
6624632Seric }
6634632Seric /*
6644632Seric **  DOWORK -- do a work request.
6654632Seric **
6664632Seric **	Parameters:
6674632Seric **		w -- the work request to be satisfied.
6684632Seric **
6694632Seric **	Returns:
6704632Seric **		none.
6714632Seric **
6724632Seric **	Side Effects:
6734632Seric **		The work request is satisfied if possible.
6744632Seric */
6754632Seric 
67655012Seric dowork(w, e)
6774632Seric 	register WORK *w;
67855012Seric 	register ENVELOPE *e;
6794632Seric {
6804632Seric 	register int i;
68124941Seric 	extern bool shouldqueue();
68251920Seric 	extern bool readqf();
6834632Seric 
6847677Seric 	if (tTd(40, 1))
6855037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
6864632Seric 
6874632Seric 	/*
68824941Seric 	**  Ignore jobs that are too expensive for the moment.
6894632Seric 	*/
6904632Seric 
69157438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
6924632Seric 	{
69324941Seric 		if (Verbose)
69424967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
6954632Seric 		return;
6964632Seric 	}
6974632Seric 
69824941Seric 	/*
69924941Seric 	**  Fork for work.
70024941Seric 	*/
70124941Seric 
70224941Seric 	if (ForkQueueRuns)
70324941Seric 	{
70424941Seric 		i = fork();
70524941Seric 		if (i < 0)
70624941Seric 		{
70724941Seric 			syserr("dowork: cannot fork");
70824941Seric 			return;
70924941Seric 		}
71024941Seric 	}
71124941Seric 	else
71224941Seric 	{
71324941Seric 		i = 0;
71424941Seric 	}
71524941Seric 
7164632Seric 	if (i == 0)
7174632Seric 	{
7184632Seric 		/*
7194632Seric 		**  CHILD
7208148Seric 		**	Lock the control file to avoid duplicate deliveries.
7218148Seric 		**		Then run the file as though we had just read it.
7227350Seric 		**	We save an idea of the temporary name so we
7237350Seric 		**		can recover on interrupt.
7244632Seric 		*/
7254632Seric 
7267763Seric 		/* set basic modes, etc. */
7277356Seric 		(void) alarm(0);
72855012Seric 		clearenvelope(e, FALSE);
7294632Seric 		QueueRun = TRUE;
7309377Seric 		ErrorMode = EM_MAIL;
73155012Seric 		e->e_id = &w->w_name[2];
7327876Seric # ifdef LOG
73355360Seric 		if (LogLevel > 12)
73455012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7357881Seric 			       getpid());
73656795Seric # endif /* LOG */
7377763Seric 
7387763Seric 		/* don't use the headers from sendmail.cf... */
73955012Seric 		e->e_header = NULL;
7407763Seric 
74151920Seric 		/* read the queue control file -- return if locked */
74255012Seric 		if (!readqf(e))
7436980Seric 		{
74424941Seric 			if (ForkQueueRuns)
74524941Seric 				exit(EX_OK);
74624941Seric 			else
74724941Seric 				return;
7486980Seric 		}
7496980Seric 
75055012Seric 		e->e_flags |= EF_INQUEUE;
75157642Seric 		eatheader(e, TRUE);
7526980Seric 
7536980Seric 		/* do the delivery */
75455012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
75555012Seric 			sendall(e, SM_DELIVER);
7566980Seric 
7576980Seric 		/* finish up and exit */
75824941Seric 		if (ForkQueueRuns)
75924941Seric 			finis();
76024941Seric 		else
76155012Seric 			dropenvelope(e);
7624632Seric 	}
76324941Seric 	else
76424941Seric 	{
76524941Seric 		/*
76624941Seric 		**  Parent -- pick up results.
76724941Seric 		*/
7684632Seric 
76924941Seric 		errno = 0;
77024941Seric 		(void) waitfor(i);
77124941Seric 	}
7724632Seric }
7734632Seric /*
7744632Seric **  READQF -- read queue file and set up environment.
7754632Seric **
7764632Seric **	Parameters:
7779377Seric **		e -- the envelope of the job to run.
7784632Seric **
7794632Seric **	Returns:
78051920Seric **		TRUE if it successfully read the queue file.
78151920Seric **		FALSE otherwise.
7824632Seric **
7834632Seric **	Side Effects:
78451920Seric **		The queue file is returned locked.
7854632Seric */
7864632Seric 
78751920Seric bool
78851920Seric readqf(e)
7899377Seric 	register ENVELOPE *e;
7904632Seric {
79117477Seric 	char *qf;
79217477Seric 	register FILE *qfp;
79354974Seric 	ADDRESS *ctladdr;
79456400Seric 	struct stat st;
79557135Seric 	char *bp;
79657135Seric 	char buf[MAXLINE];
7979348Seric 	extern char *fgetfolded();
79824954Seric 	extern long atol();
79954974Seric 	extern ADDRESS *setctluser();
80051937Seric # ifdef LOCKF
80151937Seric 	struct flock lfd;
80251937Seric # endif
8034632Seric 
8044632Seric 	/*
80517468Seric 	**  Read and process the file.
8064632Seric 	*/
8074632Seric 
80817477Seric 	qf = queuename(e, 'q');
80951937Seric 	qfp = fopen(qf, "r+");
81017477Seric 	if (qfp == NULL)
81117477Seric 	{
81240934Srick 		if (errno != ENOENT)
81340934Srick 			syserr("readqf: no control file %s", qf);
81451920Seric 		return FALSE;
81517477Seric 	}
81640934Srick 
81756400Seric 	/*
81856400Seric 	**  Check the queue file for plausibility to avoid attacks.
81956400Seric 	*/
82056400Seric 
82156400Seric 	if (fstat(fileno(qfp), &st) < 0)
82256400Seric 	{
82356400Seric 		/* must have been being processed by someone else */
82456400Seric 		fclose(qfp);
82556400Seric 		return FALSE;
82656400Seric 	}
82756400Seric 
82857135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
82956400Seric 	{
83056400Seric # ifdef LOG
83156400Seric 		if (LogLevel > 0)
83256400Seric 		{
83356400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
83456400Seric 				e->e_id, st.st_uid, st.st_mode);
83556400Seric 		}
83656795Seric # endif /* LOG */
83756400Seric 		fclose(qfp);
83856400Seric 		return FALSE;
83956400Seric 	}
84056400Seric 
84151835Seric # ifdef LOCKF
84251937Seric 	lfd.l_type = F_WRLCK;
84351937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
84451937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
84551835Seric # else
84640934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
84751835Seric # endif
84840934Srick 	{
84940934Srick 		/* being processed by another queuer */
85040934Srick 		if (Verbose)
85155012Seric 			printf("%s: locked\n", e->e_id);
85251920Seric # ifdef LOG
85355173Seric 		if (LogLevel > 10)
85455012Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
85556795Seric # endif /* LOG */
85640934Srick 		(void) fclose(qfp);
85751920Seric 		return FALSE;
85840934Srick 	}
85940934Srick 
86051920Seric 	/* save this lock */
86151920Seric 	e->e_lockfp = qfp;
86251920Seric 
86340934Srick 	/* do basic system initialization */
86455012Seric 	initsys(e);
86540934Srick 
86617477Seric 	FileName = qf;
8679377Seric 	LineNumber = 0;
86851920Seric 	if (Verbose)
8699377Seric 		printf("\nRunning %s\n", e->e_id);
87054974Seric 	ctladdr = NULL;
87157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
8724632Seric 	{
87357529Seric 		struct stat st;
87457529Seric 
87526504Seric 		if (tTd(40, 4))
87657135Seric 			printf("+++++ %s\n", bp);
87757135Seric 		switch (bp[0])
8784632Seric 		{
87940973Sbostic 		  case 'C':		/* specify controlling user */
88057135Seric 			ctladdr = setctluser(&bp[1]);
88140973Sbostic 			break;
88240973Sbostic 
8834632Seric 		  case 'R':		/* specify recipient */
88457135Seric 			sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
8854632Seric 			break;
8864632Seric 
88725687Seric 		  case 'E':		/* specify error recipient */
88857135Seric 			sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
88925687Seric 			break;
89025687Seric 
8914632Seric 		  case 'H':		/* header */
89257135Seric 			(void) chompheader(&bp[1], FALSE, e);
8934632Seric 			break;
8944632Seric 
89510108Seric 		  case 'M':		/* message */
89657135Seric 			e->e_message = newstr(&bp[1]);
89710108Seric 			break;
89810108Seric 
8994632Seric 		  case 'S':		/* sender */
90057135Seric 			setsender(newstr(&bp[1]), e);
9014632Seric 			break;
9024632Seric 
9034632Seric 		  case 'D':		/* data file name */
90457135Seric 			e->e_df = newstr(&bp[1]);
9059544Seric 			e->e_dfp = fopen(e->e_df, "r");
9069544Seric 			if (e->e_dfp == NULL)
9079377Seric 				syserr("readqf: cannot open %s", e->e_df);
90857529Seric 			if (fstat(fileno(e->e_dfp), &st) >= 0)
90957529Seric 				e->e_msgsize = st.st_size;
9104632Seric 			break;
9114632Seric 
9127860Seric 		  case 'T':		/* init time */
91357135Seric 			e->e_ctime = atol(&bp[1]);
9144632Seric 			break;
9154632Seric 
9164634Seric 		  case 'P':		/* message priority */
91757135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9184634Seric 			break;
9194634Seric 
92053400Seric 		  case '$':		/* define macro */
92157135Seric 			define(bp[1], newstr(&bp[2]), e);
92253400Seric 			break;
92353400Seric 
92424941Seric 		  case '\0':		/* blank line; ignore */
92524941Seric 			break;
92624941Seric 
9274632Seric 		  default:
92824941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
92957135Seric 				LineNumber, bp);
9304632Seric 			break;
9314632Seric 		}
93257135Seric 
93357135Seric 		if (bp != buf)
93457135Seric 			free(bp);
9354632Seric 	}
9369377Seric 
9379377Seric 	FileName = NULL;
93824941Seric 
93924941Seric 	/*
94024941Seric 	**  If we haven't read any lines, this queue file is empty.
94124941Seric 	**  Arrange to remove it without referencing any null pointers.
94224941Seric 	*/
94324941Seric 
94424941Seric 	if (LineNumber == 0)
94524941Seric 	{
94624941Seric 		errno = 0;
94724941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
94824941Seric 	}
94951920Seric 	return TRUE;
9504632Seric }
9514632Seric /*
9529630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9539630Seric **
9549630Seric **	Parameters:
9559630Seric **		none.
9569630Seric **
9579630Seric **	Returns:
9589630Seric **		none.
9599630Seric **
9609630Seric **	Side Effects:
9619630Seric **		Prints a listing of the mail queue on the standard output.
9629630Seric */
9635182Seric 
9649630Seric printqueue()
9659630Seric {
9669630Seric 	register WORK *w;
9679630Seric 	FILE *f;
96810070Seric 	int nrequests;
9699630Seric 	char buf[MAXLINE];
9709630Seric 
9719630Seric 	/*
9729630Seric 	**  Read and order the queue.
9739630Seric 	*/
9749630Seric 
97524941Seric 	nrequests = orderq(TRUE);
9769630Seric 
9779630Seric 	/*
9789630Seric 	**  Print the work list that we have read.
9799630Seric 	*/
9809630Seric 
9819630Seric 	/* first see if there is anything */
98210070Seric 	if (nrequests <= 0)
9839630Seric 	{
98410070Seric 		printf("Mail queue is empty\n");
9859630Seric 		return;
9869630Seric 	}
9879630Seric 
98851920Seric 	CurrentLA = getla();	/* get load average */
98940934Srick 
99010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
99125687Seric 	if (nrequests > QUEUESIZE)
99225687Seric 		printf(", only %d printed", QUEUESIZE);
99324979Seric 	if (Verbose)
99425032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
99524979Seric 	else
99624979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
9979630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
9989630Seric 	{
9999630Seric 		struct stat st;
100010070Seric 		auto time_t submittime = 0;
100110070Seric 		long dfsize = -1;
100210108Seric 		char message[MAXLINE];
100351937Seric # ifdef LOCKF
100451937Seric 		struct flock lfd;
100551937Seric # endif
100624941Seric 		extern bool shouldqueue();
10079630Seric 
100817468Seric 		f = fopen(w->w_name, "r");
100917468Seric 		if (f == NULL)
101017468Seric 		{
101117468Seric 			errno = 0;
101217468Seric 			continue;
101317468Seric 		}
10149630Seric 		printf("%7s", w->w_name + 2);
101551835Seric # ifdef LOCKF
101651937Seric 		lfd.l_type = F_RDLCK;
101751937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
101851937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
101951835Seric # else
102040934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
102151835Seric # endif
102210070Seric 			printf("*");
102357438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
102424941Seric 			printf("X");
102510070Seric 		else
102610070Seric 			printf(" ");
102710070Seric 		errno = 0;
102817468Seric 
102910108Seric 		message[0] = '\0';
10309630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10319630Seric 		{
103253400Seric 			register int i;
103353400Seric 
10349630Seric 			fixcrlf(buf, TRUE);
10359630Seric 			switch (buf[0])
10369630Seric 			{
103710108Seric 			  case 'M':	/* error message */
103853400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
103953400Seric 					i = sizeof message;
104053400Seric 				bcopy(&buf[1], message, i);
104153400Seric 				message[i] = '\0';
104210108Seric 				break;
104310108Seric 
10449630Seric 			  case 'S':	/* sender name */
104524979Seric 				if (Verbose)
104625027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
104725027Seric 					    w->w_pri, ctime(&submittime) + 4,
104824979Seric 					    &buf[1]);
104924979Seric 				else
105024979Seric 					printf("%8ld %.16s %.45s", dfsize,
105124979Seric 					    ctime(&submittime), &buf[1]);
105210108Seric 				if (message[0] != '\0')
105325027Seric 					printf("\n\t\t (%.60s)", message);
10549630Seric 				break;
105551920Seric 
105640973Sbostic 			  case 'C':	/* controlling user */
105754974Seric 				if (Verbose)
105854975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
105940973Sbostic 				break;
10609630Seric 
10619630Seric 			  case 'R':	/* recipient name */
106224979Seric 				if (Verbose)
106325027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
106424979Seric 				else
106524979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
10669630Seric 				break;
10679630Seric 
10689630Seric 			  case 'T':	/* creation time */
106924941Seric 				submittime = atol(&buf[1]);
10709630Seric 				break;
107110070Seric 
107210070Seric 			  case 'D':	/* data file name */
107310070Seric 				if (stat(&buf[1], &st) >= 0)
107410070Seric 					dfsize = st.st_size;
107510070Seric 				break;
10769630Seric 			}
10779630Seric 		}
107810070Seric 		if (submittime == (time_t) 0)
107910070Seric 			printf(" (no control file)");
10809630Seric 		printf("\n");
108123098Seric 		(void) fclose(f);
10829630Seric 	}
10839630Seric }
10849630Seric 
108556795Seric # endif /* QUEUE */
108617468Seric /*
108717468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
108817468Seric **
108917468Seric **	Assigns an id code if one does not already exist.
109017468Seric **	This code is very careful to avoid trashing existing files
109117468Seric **	under any circumstances.
109217468Seric **
109317468Seric **	Parameters:
109417468Seric **		e -- envelope to build it in/from.
109517468Seric **		type -- the file type, used as the first character
109617468Seric **			of the file name.
109717468Seric **
109817468Seric **	Returns:
109917468Seric **		a pointer to the new file name (in a static buffer).
110017468Seric **
110117468Seric **	Side Effects:
110251920Seric **		If no id code is already assigned, queuename will
110351920Seric **		assign an id code, create a qf file, and leave a
110451920Seric **		locked, open-for-write file pointer in the envelope.
110517468Seric */
110617468Seric 
110717468Seric char *
110817468Seric queuename(e, type)
110917468Seric 	register ENVELOPE *e;
111017468Seric 	char type;
111117468Seric {
111217468Seric 	static char buf[MAXNAME];
111317468Seric 	static int pid = -1;
111417468Seric 	char c1 = 'A';
111517468Seric 	char c2 = 'A';
111617468Seric 
111717468Seric 	if (e->e_id == NULL)
111817468Seric 	{
111917468Seric 		char qf[20];
112017468Seric 
112117468Seric 		/* find a unique id */
112217468Seric 		if (pid != getpid())
112317468Seric 		{
112417468Seric 			/* new process -- start back at "AA" */
112517468Seric 			pid = getpid();
112617468Seric 			c1 = 'A';
112717468Seric 			c2 = 'A' - 1;
112817468Seric 		}
112917468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
113017468Seric 
113117468Seric 		while (c1 < '~' || c2 < 'Z')
113217468Seric 		{
113317468Seric 			int i;
113451937Seric # ifdef LOCKF
113551937Seric 			struct flock lfd;
113651937Seric # endif
113717468Seric 
113817468Seric 			if (c2 >= 'Z')
113917468Seric 			{
114017468Seric 				c1++;
114117468Seric 				c2 = 'A' - 1;
114217468Seric 			}
114340934Srick 			qf[2] = c1;
114440934Srick 			qf[3] = ++c2;
114517468Seric 			if (tTd(7, 20))
114640934Srick 				printf("queuename: trying \"%s\"\n", qf);
114717468Seric 
114840934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
114951920Seric 			if (i < 0)
115051920Seric 			{
115151920Seric 				if (errno == EEXIST)
115251920Seric 					continue;
115351920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
115451920Seric 					qf, QueueDir);
115551920Seric 				exit(EX_UNAVAILABLE);
115651920Seric 			}
115751920Seric # ifdef LOCKF
115851937Seric 			lfd.l_type = F_WRLCK;
115951937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
116051937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
116151920Seric # else
116251920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
116351920Seric # endif
116451920Seric 			{
116551920Seric 				e->e_lockfp = fdopen(i, "w");
116640934Srick 				break;
116717468Seric 			}
116851920Seric 
116951920Seric 			/* a reader got the file; abandon it and try again */
117051920Seric 			(void) close(i);
117117468Seric 		}
117217468Seric 		if (c1 >= '~' && c2 >= 'Z')
117317468Seric 		{
117417468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
117517468Seric 				qf, QueueDir);
117617468Seric 			exit(EX_OSERR);
117717468Seric 		}
117817468Seric 		e->e_id = newstr(&qf[2]);
117917468Seric 		define('i', e->e_id, e);
118017468Seric 		if (tTd(7, 1))
118117468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
118217468Seric # ifdef LOG
118317468Seric 		if (LogLevel > 16)
118417468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
118556795Seric # endif /* LOG */
118617468Seric 	}
118717468Seric 
118817468Seric 	if (type == '\0')
118917468Seric 		return (NULL);
119017468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
119117468Seric 	if (tTd(7, 2))
119217468Seric 		printf("queuename: %s\n", buf);
119317468Seric 	return (buf);
119417468Seric }
119517468Seric /*
119617468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
119717468Seric **
119817468Seric **	Parameters:
119917468Seric **		e -- the envelope to unlock.
120017468Seric **
120117468Seric **	Returns:
120217468Seric **		none
120317468Seric **
120417468Seric **	Side Effects:
120517468Seric **		unlocks the queue for `e'.
120617468Seric */
120717468Seric 
120817468Seric unlockqueue(e)
120917468Seric 	ENVELOPE *e;
121017468Seric {
121151920Seric 	/* if there is a lock file in the envelope, close it */
121251920Seric 	if (e->e_lockfp != NULL)
121351920Seric 		fclose(e->e_lockfp);
121451920Seric 	e->e_lockfp = NULL;
121551920Seric 
121617468Seric 	/* remove the transcript */
121717468Seric # ifdef LOG
121817468Seric 	if (LogLevel > 19)
121917468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
122056795Seric # endif /* LOG */
122117468Seric 	if (!tTd(51, 4))
122217468Seric 		xunlink(queuename(e, 'x'));
122317468Seric 
122417468Seric }
122540973Sbostic /*
122654974Seric **  SETCTLUSER -- create a controlling address
122740973Sbostic **
122854974Seric **	Create a fake "address" given only a local login name; this is
122954974Seric **	used as a "controlling user" for future recipient addresses.
123040973Sbostic **
123140973Sbostic **	Parameters:
123254974Seric **		user -- the user name of the controlling user.
123340973Sbostic **
123440973Sbostic **	Returns:
123554974Seric **		An address descriptor for the controlling user.
123640973Sbostic **
123740973Sbostic **	Side Effects:
123840973Sbostic **		none.
123940973Sbostic */
124040973Sbostic 
124154974Seric ADDRESS *
124254974Seric setctluser(user)
124354974Seric 	char *user;
124440973Sbostic {
124554974Seric 	register ADDRESS *a;
124640973Sbostic 	struct passwd *pw;
124740973Sbostic 
124840973Sbostic 	/*
124954974Seric 	**  See if this clears our concept of controlling user.
125040973Sbostic 	*/
125140973Sbostic 
125254974Seric 	if (user == NULL || *user == '\0')
125354974Seric 		return NULL;
125440973Sbostic 
125540973Sbostic 	/*
125654974Seric 	**  Set up addr fields for controlling user.
125740973Sbostic 	*/
125840973Sbostic 
125954974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
126054974Seric 	bzero((char *) a, sizeof *a);
126154974Seric 	if ((pw = getpwnam(user)) != NULL)
126240973Sbostic 	{
126340973Sbostic 		a->q_home = newstr(pw->pw_dir);
126440973Sbostic 		a->q_uid = pw->pw_uid;
126540973Sbostic 		a->q_gid = pw->pw_gid;
126657642Seric 		a->q_user = newstr(user);
126740973Sbostic 	}
126840973Sbostic 	else
126940973Sbostic 	{
127040973Sbostic 		a->q_uid = DefUid;
127140973Sbostic 		a->q_gid = DefGid;
127257642Seric 		a->q_user = newstr(DefUser);
127340973Sbostic 	}
127440973Sbostic 
127540973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
127656328Seric 	a->q_mailer = LocalMailer;
127754974Seric 	return a;
127840973Sbostic }
1279