xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58716)
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*58716Seric static char sccsid[] = "@(#)queue.c	6.31 (Berkeley) 03/18/93 (with queueing)";
1433731Sbostic #else
15*58716Seric static char sccsid[] = "@(#)queue.c	6.31 (Berkeley) 03/18/93 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
214634Seric # include <signal.h>
224632Seric # include <errno.h>
2340973Sbostic # include <pwd.h>
2457977Seric # ifndef MAXNAMLEN
2557736Seric # include <dirent.h>
2657736Seric # endif
274632Seric 
2833731Sbostic # ifdef QUEUE
294632Seric 
304632Seric /*
319377Seric **  Work queue.
329377Seric */
339377Seric 
349377Seric struct work
359377Seric {
369377Seric 	char		*w_name;	/* name of control file */
379377Seric 	long		w_pri;		/* priority of message, see below */
3825013Seric 	time_t		w_ctime;	/* creation time of message */
399377Seric 	struct work	*w_next;	/* next in queue */
409377Seric };
419377Seric 
429377Seric typedef struct work	WORK;
439377Seric 
449377Seric WORK	*WorkQ;			/* queue of things to be done */
459377Seric /*
464632Seric **  QUEUEUP -- queue a message up for future transmission.
474632Seric **
484632Seric **	Parameters:
496980Seric **		e -- the envelope to queue up.
506999Seric **		queueall -- if TRUE, queue all addresses, rather than
516999Seric **			just those with the QQUEUEUP flag set.
529377Seric **		announce -- if TRUE, tell when you are queueing up.
534632Seric **
544632Seric **	Returns:
5551920Seric **		none.
564632Seric **
574632Seric **	Side Effects:
589377Seric **		The current request are saved in a control file.
5951920Seric **		The queue file is left locked.
604632Seric */
614632Seric 
629377Seric queueup(e, queueall, announce)
636980Seric 	register ENVELOPE *e;
646999Seric 	bool queueall;
659377Seric 	bool announce;
664632Seric {
677812Seric 	char *qf;
687812Seric 	register FILE *tfp;
694632Seric 	register HDR *h;
705007Seric 	register ADDRESS *q;
7151920Seric 	int fd;
7251920Seric 	int i;
7351920Seric 	bool newid;
7453400Seric 	register char *p;
7510173Seric 	MAILER nullmailer;
7654974Seric 	ADDRESS *lastctladdr;
7751920Seric 	char buf[MAXLINE], tf[MAXLINE];
7853400Seric 	extern char *macvalue();
7954974Seric 	extern ADDRESS *getctladdr();
804632Seric 
815037Seric 	/*
8217477Seric 	**  Create control file.
835037Seric 	*/
844632Seric 
8551920Seric 	newid = (e->e_id == NULL);
8651920Seric 	strcpy(tf, queuename(e, 't'));
8751920Seric 	tfp = e->e_lockfp;
8851920Seric 	if (tfp == NULL)
8951920Seric 		newid = FALSE;
9051920Seric 	if (newid)
9151835Seric 	{
9251920Seric 		tfp = e->e_lockfp;
9351920Seric 	}
9451920Seric 	else
9551920Seric 	{
9651920Seric 		/* get a locked tf file */
9751920Seric 		for (i = 100; --i >= 0; )
9851835Seric 		{
9958689Seric 			extern bool lockfile();
10051937Seric 
10151920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10251920Seric 			if (fd < 0)
10351835Seric 			{
10451920Seric 				if (errno == EEXIST)
10551920Seric 					continue;
10658690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10741636Srick 			}
10858689Seric 
10958689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
11051920Seric 				break;
11158689Seric 
11251920Seric 			close(fd);
11341636Srick 		}
11441636Srick 
11551920Seric 		tfp = fdopen(fd, "w");
11651920Seric 	}
1174632Seric 
1187677Seric 	if (tTd(40, 1))
11917468Seric 		printf("queueing %s\n", e->e_id);
1204632Seric 
1214632Seric 	/*
1226980Seric 	**  If there is no data file yet, create one.
1236980Seric 	*/
1246980Seric 
1256980Seric 	if (e->e_df == NULL)
1266980Seric 	{
1276980Seric 		register FILE *dfp;
1289389Seric 		extern putbody();
1296980Seric 
1307812Seric 		e->e_df = newstr(queuename(e, 'd'));
13140934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13240934Srick 		if (fd < 0)
13358690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13440934Srick 		dfp = fdopen(fd, "w");
13510173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
13658680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1379389Seric 		e->e_putbody = putbody;
1386980Seric 	}
1396980Seric 
1406980Seric 	/*
1414632Seric 	**  Output future work requests.
14225687Seric 	**	Priority and creation time should be first, since
14325687Seric 	**	they are required by orderq.
1444632Seric 	*/
1454632Seric 
1469377Seric 	/* output message priority */
1479377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1489377Seric 
1499630Seric 	/* output creation time */
1509630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1519630Seric 
1524632Seric 	/* output name of data file */
1537812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1544632Seric 
15510108Seric 	/* message from envelope, if it exists */
15610108Seric 	if (e->e_message != NULL)
15710108Seric 		fprintf(tfp, "M%s\n", e->e_message);
15810108Seric 
15953400Seric 	/* $r and $s macro values */
16053400Seric 	if ((p = macvalue('r', e)) != NULL)
16153400Seric 		fprintf(tfp, "$r%s\n", p);
16253400Seric 	if ((p = macvalue('s', e)) != NULL)
16353400Seric 		fprintf(tfp, "$s%s\n", p);
16453400Seric 
1654632Seric 	/* output name of sender */
1667812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1674632Seric 
16855360Seric 	/* output list of error recipients */
16955360Seric 	lastctladdr = NULL;
17055360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
17155360Seric 	{
17258680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
17355360Seric 		{
17455360Seric 			ADDRESS *ctladdr;
17555360Seric 
17655360Seric 			ctladdr = getctladdr(q);
17755360Seric 			if (ctladdr != lastctladdr)
17855360Seric 			{
17955360Seric 				printctladdr(ctladdr, tfp);
18055360Seric 				lastctladdr = ctladdr;
18155360Seric 			}
18255360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18355360Seric 		}
18455360Seric 	}
18555360Seric 
1864632Seric 	/* output list of recipient addresses */
1876980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1884632Seric 	{
18958250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
19058680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
1918245Seric 		{
19254974Seric 			ADDRESS *ctladdr;
19340973Sbostic 
19454975Seric 			ctladdr = getctladdr(q);
19554975Seric 			if (ctladdr != lastctladdr)
19654974Seric 			{
19754974Seric 				printctladdr(ctladdr, tfp);
19854974Seric 				lastctladdr = ctladdr;
19954974Seric 			}
2007812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2019377Seric 			if (announce)
2029377Seric 			{
2039377Seric 				e->e_to = q->q_paddr;
20458151Seric 				message("queued");
20558020Seric 				if (LogLevel > 8)
20658337Seric 					logdelivery(NULL, NULL, "queued", e);
2079377Seric 				e->e_to = NULL;
2089377Seric 			}
2099387Seric 			if (tTd(40, 1))
2109387Seric 			{
2119387Seric 				printf("queueing ");
2129387Seric 				printaddr(q, FALSE);
2139387Seric 			}
2148245Seric 		}
2154632Seric 	}
2164632Seric 
2179377Seric 	/*
2189377Seric 	**  Output headers for this message.
2199377Seric 	**	Expand macros completely here.  Queue run will deal with
2209377Seric 	**	everything as absolute headers.
2219377Seric 	**		All headers that must be relative to the recipient
2229377Seric 	**		can be cracked later.
22310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
22410173Seric 	**	no effect on the addresses as they are output.
2259377Seric 	*/
2269377Seric 
22710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
22958020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
23010349Seric 	nullmailer.m_eol = "\n";
23110173Seric 
23258050Seric 	define('g', "\201f", e);
2336980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2344632Seric 	{
23510686Seric 		extern bool bitzerop();
23610686Seric 
23712015Seric 		/* don't output null headers */
2384632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2394632Seric 			continue;
24012015Seric 
24112015Seric 		/* don't output resent headers on non-resent messages */
24212015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
24312015Seric 			continue;
24412015Seric 
24512015Seric 		/* output this header */
2467812Seric 		fprintf(tfp, "H");
24712015Seric 
24812015Seric 		/* if conditional, output the set of conditions */
24910686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
25010686Seric 		{
25110686Seric 			int j;
25210686Seric 
25323098Seric 			(void) putc('?', tfp);
25410686Seric 			for (j = '\0'; j <= '\177'; j++)
25510686Seric 				if (bitnset(j, h->h_mflags))
25623098Seric 					(void) putc(j, tfp);
25723098Seric 			(void) putc('?', tfp);
25810686Seric 		}
25912015Seric 
26012015Seric 		/* output the header: expand macros, convert addresses */
2617763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2627763Seric 		{
2637763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2648236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2657763Seric 		}
2668245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2679348Seric 		{
26855012Seric 			commaize(h, h->h_value, tfp,
26955012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
27055012Seric 				 &nullmailer, e);
2719348Seric 		}
2727763Seric 		else
2738245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2744632Seric 	}
2754632Seric 
2764632Seric 	/*
2774632Seric 	**  Clean up.
2784632Seric 	*/
2794632Seric 
28051920Seric 	if (!newid)
28151920Seric 	{
28251920Seric 		qf = queuename(e, 'q');
28351920Seric 		if (rename(tf, qf) < 0)
28451920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
28551920Seric 		if (e->e_lockfp != NULL)
28658680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
28751920Seric 		e->e_lockfp = tfp;
28851920Seric 	}
28951920Seric 	else
29051920Seric 		qf = tf;
29141636Srick 	errno = 0;
2927391Seric 
2937677Seric # ifdef LOG
2947677Seric 	/* save log info */
29558020Seric 	if (LogLevel > 79)
2967878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
29756795Seric # endif /* LOG */
29840934Srick 	fflush(tfp);
29951920Seric 	return;
3004632Seric }
30154974Seric 
30254974Seric printctladdr(a, tfp)
30354974Seric 	ADDRESS *a;
30454974Seric 	FILE *tfp;
30554974Seric {
30654974Seric 	char *u;
30754974Seric 	struct passwd *pw;
30854974Seric 	extern struct passwd *getpwuid();
30954974Seric 
31054974Seric 	if (a == NULL)
31154974Seric 	{
31254974Seric 		fprintf(tfp, "C\n");
31354974Seric 		return;
31454974Seric 	}
31554974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
31654974Seric 		u = DefUser;
31754974Seric 	else
31854974Seric 		u = pw->pw_name;
31954974Seric 	fprintf(tfp, "C%s\n", u);
32054974Seric }
32154974Seric 
3224632Seric /*
3234632Seric **  RUNQUEUE -- run the jobs in the queue.
3244632Seric **
3254632Seric **	Gets the stuff out of the queue in some presumably logical
3264632Seric **	order and processes them.
3274632Seric **
3284632Seric **	Parameters:
32924941Seric **		forkflag -- TRUE if the queue scanning should be done in
33024941Seric **			a child process.  We double-fork so it is not our
33124941Seric **			child and we don't have to clean up after it.
3324632Seric **
3334632Seric **	Returns:
3344632Seric **		none.
3354632Seric **
3364632Seric **	Side Effects:
3374632Seric **		runs things in the mail queue.
3384632Seric */
3394632Seric 
34055360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
34155360Seric 
34255360Seric runqueue(forkflag)
3434639Seric 	bool forkflag;
3444632Seric {
34524953Seric 	extern bool shouldqueue();
34655360Seric 	register ENVELOPE *e;
34755360Seric 	extern ENVELOPE BlankEnvelope;
34855360Seric 	extern ENVELOPE *newenvelope();
34924953Seric 
3507466Seric 	/*
35124953Seric 	**  If no work will ever be selected, don't even bother reading
35224953Seric 	**  the queue.
35324953Seric 	*/
35424953Seric 
35551920Seric 	CurrentLA = getla();	/* get load average */
35640934Srick 
35758132Seric 	if (shouldqueue(0L, curtime()))
35824953Seric 	{
35924953Seric 		if (Verbose)
36024953Seric 			printf("Skipping queue run -- load average too high\n");
36158107Seric 		if (forkflag && QueueIntvl != 0)
36258107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
36355360Seric 		return;
36424953Seric 	}
36524953Seric 
36624953Seric 	/*
3677466Seric 	**  See if we want to go off and do other useful work.
3687466Seric 	*/
3694639Seric 
3704639Seric 	if (forkflag)
3714639Seric 	{
3727943Seric 		int pid;
3737943Seric 
3747943Seric 		pid = dofork();
3757943Seric 		if (pid != 0)
3764639Seric 		{
37746928Sbostic 			extern void reapchild();
37825184Seric 
3797943Seric 			/* parent -- pick up intermediate zombie */
38025184Seric #ifndef SIGCHLD
3819377Seric 			(void) waitfor(pid);
38256795Seric #else /* SIGCHLD */
38325184Seric 			(void) signal(SIGCHLD, reapchild);
38456795Seric #endif /* SIGCHLD */
3857690Seric 			if (QueueIntvl != 0)
3869348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3874639Seric 			return;
3884639Seric 		}
3897943Seric 		/* child -- double fork */
39025184Seric #ifndef SIGCHLD
3917943Seric 		if (fork() != 0)
3927943Seric 			exit(EX_OK);
39356795Seric #else /* SIGCHLD */
39425184Seric 		(void) signal(SIGCHLD, SIG_DFL);
39556795Seric #endif /* SIGCHLD */
3964639Seric 	}
39724941Seric 
39840934Srick 	setproctitle("running queue: %s", QueueDir);
39924941Seric 
4007876Seric # ifdef LOG
40158020Seric 	if (LogLevel > 69)
40255360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
40355360Seric 			QueueDir, getpid(), forkflag);
40456795Seric # endif /* LOG */
4054639Seric 
4067466Seric 	/*
40710205Seric 	**  Release any resources used by the daemon code.
40810205Seric 	*/
40910205Seric 
41010205Seric # ifdef DAEMON
41110205Seric 	clrdaemon();
41256795Seric # endif /* DAEMON */
41310205Seric 
41410205Seric 	/*
41555360Seric 	**  Create ourselves an envelope
41655360Seric 	*/
41755360Seric 
41855360Seric 	CurEnv = &QueueEnvelope;
41958179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
42055360Seric 	e->e_flags = BlankEnvelope.e_flags;
42155360Seric 
42255360Seric 	/*
42327175Seric 	**  Make sure the alias database is open.
42427175Seric 	*/
42527175Seric 
42655012Seric 	initaliases(AliasFile, FALSE, e);
42727175Seric 
42827175Seric 	/*
4297466Seric 	**  Start making passes through the queue.
4307466Seric 	**	First, read and sort the entire queue.
4317466Seric 	**	Then, process the work in that order.
4327466Seric 	**		But if you take too long, start over.
4337466Seric 	*/
4347466Seric 
4357943Seric 	/* order the existing work requests */
43624954Seric 	(void) orderq(FALSE);
4377690Seric 
4387943Seric 	/* process them once at a time */
4397943Seric 	while (WorkQ != NULL)
4404639Seric 	{
4417943Seric 		WORK *w = WorkQ;
4427881Seric 
4437943Seric 		WorkQ = WorkQ->w_next;
44455012Seric 		dowork(w, e);
4457943Seric 		free(w->w_name);
4467943Seric 		free((char *) w);
4474639Seric 	}
44829866Seric 
44929866Seric 	/* exit without the usual cleanup */
45055467Seric 	e->e_id = NULL;
45155467Seric 	finis();
4524634Seric }
4534634Seric /*
4544632Seric **  ORDERQ -- order the work queue.
4554632Seric **
4564632Seric **	Parameters:
45724941Seric **		doall -- if set, include everything in the queue (even
45824941Seric **			the jobs that cannot be run because the load
45924941Seric **			average is too high).  Otherwise, exclude those
46024941Seric **			jobs.
4614632Seric **
4624632Seric **	Returns:
46310121Seric **		The number of request in the queue (not necessarily
46410121Seric **		the number of requests in WorkQ however).
4654632Seric **
4664632Seric **	Side Effects:
4674632Seric **		Sets WorkQ to the queue of available work, in order.
4684632Seric */
4694632Seric 
47025687Seric # define NEED_P		001
47125687Seric # define NEED_T		002
47258318Seric # define NEED_R		004
47358318Seric # define NEED_S		010
4744632Seric 
47524941Seric orderq(doall)
47624941Seric 	bool doall;
4774632Seric {
4786625Sglickman 	register struct direct *d;
4794632Seric 	register WORK *w;
4806625Sglickman 	DIR *f;
4814632Seric 	register int i;
48225687Seric 	WORK wlist[QUEUESIZE+1];
48310070Seric 	int wn = -1;
4844632Seric 	extern workcmpf();
4854632Seric 
48658318Seric 	if (tTd(41, 1))
48758318Seric 	{
48858318Seric 		printf("orderq:\n");
48958318Seric 		if (QueueLimitId != NULL)
49058318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
49158318Seric 		if (QueueLimitSender != NULL)
49258318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
49358318Seric 		if (QueueLimitRecipient != NULL)
49458318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
49558318Seric 	}
49658318Seric 
4974632Seric 	/* clear out old WorkQ */
4984632Seric 	for (w = WorkQ; w != NULL; )
4994632Seric 	{
5004632Seric 		register WORK *nw = w->w_next;
5014632Seric 
5024632Seric 		WorkQ = nw;
5034632Seric 		free(w->w_name);
5044632Seric 		free((char *) w);
5054632Seric 		w = nw;
5064632Seric 	}
5074632Seric 
5084632Seric 	/* open the queue directory */
5098148Seric 	f = opendir(".");
5104632Seric 	if (f == NULL)
5114632Seric 	{
5128148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
51310070Seric 		return (0);
5144632Seric 	}
5154632Seric 
5164632Seric 	/*
5174632Seric 	**  Read the work directory.
5184632Seric 	*/
5194632Seric 
52010070Seric 	while ((d = readdir(f)) != NULL)
5214632Seric 	{
5229377Seric 		FILE *cf;
5234632Seric 		char lbuf[MAXNAME];
52457642Seric 		extern bool shouldqueue();
52558318Seric 		extern bool strcontainedin();
5264632Seric 
5274632Seric 		/* is this an interesting entry? */
5287812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5294632Seric 			continue;
5304632Seric 
53158318Seric 		if (QueueLimitId != NULL &&
53258318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
53358318Seric 			continue;
53458318Seric 
535*58716Seric 		if (strlen(d->d_name) != 10)
53658020Seric 		{
53758020Seric 			if (Verbose)
53858020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
53958020Seric #ifdef LOG
54058020Seric 			if (LogLevel > 3)
54158020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
54258020Seric 					d->d_name);
54358020Seric #endif
54458020Seric 			if (strlen(d->d_name) >= MAXNAME)
54558020Seric 				d->d_name[MAXNAME - 1] = '\0';
54658020Seric 			strcpy(lbuf, d->d_name);
54758020Seric 			lbuf[0] = 'Q';
54858020Seric 			(void) rename(d->d_name, lbuf);
54958020Seric 			continue;
55058020Seric 		}
55158020Seric 
55210070Seric 		/* yes -- open control file (if not too many files) */
55325687Seric 		if (++wn >= QUEUESIZE)
55410070Seric 			continue;
55558318Seric 
5568148Seric 		cf = fopen(d->d_name, "r");
5574632Seric 		if (cf == NULL)
5584632Seric 		{
5597055Seric 			/* this may be some random person sending hir msgs */
5607055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
56110090Seric 			if (tTd(41, 2))
56210090Seric 				printf("orderq: cannot open %s (%d)\n",
56310090Seric 					d->d_name, errno);
5647055Seric 			errno = 0;
56510090Seric 			wn--;
5664632Seric 			continue;
5674632Seric 		}
56825687Seric 		w = &wlist[wn];
56925687Seric 		w->w_name = newstr(d->d_name);
5704632Seric 
57125027Seric 		/* make sure jobs in creation don't clog queue */
57225687Seric 		w->w_pri = 0x7fffffff;
57325687Seric 		w->w_ctime = 0;
57425027Seric 
5754632Seric 		/* extract useful information */
57625687Seric 		i = NEED_P | NEED_T;
57758318Seric 		if (QueueLimitSender != NULL)
57858318Seric 			i |= NEED_S;
57958318Seric 		if (QueueLimitRecipient != NULL)
58058318Seric 			i |= NEED_R;
58125687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5824632Seric 		{
58324954Seric 			extern long atol();
58458318Seric 			extern bool strcontainedin();
58524954Seric 
58624941Seric 			switch (lbuf[0])
5874632Seric 			{
58824941Seric 			  case 'P':
58925687Seric 				w->w_pri = atol(&lbuf[1]);
59025687Seric 				i &= ~NEED_P;
5914632Seric 				break;
59225013Seric 
59325013Seric 			  case 'T':
59425687Seric 				w->w_ctime = atol(&lbuf[1]);
59525687Seric 				i &= ~NEED_T;
59625013Seric 				break;
59758318Seric 
59858318Seric 			  case 'R':
59958318Seric 				if (QueueLimitRecipient != NULL &&
60058318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
60158318Seric 					i &= ~NEED_R;
60258318Seric 				break;
60358318Seric 
60458318Seric 			  case 'S':
60558318Seric 				if (QueueLimitSender != NULL &&
60658318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
60758318Seric 					i &= ~NEED_S;
60858318Seric 				break;
6094632Seric 			}
6104632Seric 		}
6114632Seric 		(void) fclose(cf);
61224953Seric 
61358318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
61458318Seric 		    bitset(NEED_R|NEED_S, i))
61524953Seric 		{
61624953Seric 			/* don't even bother sorting this job in */
61724953Seric 			wn--;
61824953Seric 		}
6194632Seric 	}
6206625Sglickman 	(void) closedir(f);
62110090Seric 	wn++;
6224632Seric 
6234632Seric 	/*
6244632Seric 	**  Sort the work directory.
6254632Seric 	*/
6264632Seric 
62725687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6284632Seric 
6294632Seric 	/*
6304632Seric 	**  Convert the work list into canonical form.
6319377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6324632Seric 	*/
6334632Seric 
63424981Seric 	WorkQ = NULL;
63525687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6364632Seric 	{
6374632Seric 		w = (WORK *) xalloc(sizeof *w);
6384632Seric 		w->w_name = wlist[i].w_name;
6394632Seric 		w->w_pri = wlist[i].w_pri;
64025013Seric 		w->w_ctime = wlist[i].w_ctime;
64124981Seric 		w->w_next = WorkQ;
64224981Seric 		WorkQ = w;
6434632Seric 	}
6444632Seric 
6457677Seric 	if (tTd(40, 1))
6464632Seric 	{
6474632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6485037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6494632Seric 	}
65010070Seric 
65110090Seric 	return (wn);
6524632Seric }
6534632Seric /*
6547677Seric **  WORKCMPF -- compare function for ordering work.
6554632Seric **
6564632Seric **	Parameters:
6574632Seric **		a -- the first argument.
6584632Seric **		b -- the second argument.
6594632Seric **
6604632Seric **	Returns:
66124981Seric **		-1 if a < b
66224981Seric **		 0 if a == b
66324981Seric **		+1 if a > b
6644632Seric **
6654632Seric **	Side Effects:
6664632Seric **		none.
6674632Seric */
6684632Seric 
6694632Seric workcmpf(a, b)
6705037Seric 	register WORK *a;
6715037Seric 	register WORK *b;
6724632Seric {
67357438Seric 	long pa = a->w_pri;
67457438Seric 	long pb = b->w_pri;
67524941Seric 
67624941Seric 	if (pa == pb)
6774632Seric 		return (0);
67824941Seric 	else if (pa > pb)
67924981Seric 		return (1);
68024981Seric 	else
68110121Seric 		return (-1);
6824632Seric }
6834632Seric /*
6844632Seric **  DOWORK -- do a work request.
6854632Seric **
6864632Seric **	Parameters:
6874632Seric **		w -- the work request to be satisfied.
6884632Seric **
6894632Seric **	Returns:
6904632Seric **		none.
6914632Seric **
6924632Seric **	Side Effects:
6934632Seric **		The work request is satisfied if possible.
6944632Seric */
6954632Seric 
69655012Seric dowork(w, e)
6974632Seric 	register WORK *w;
69855012Seric 	register ENVELOPE *e;
6994632Seric {
7004632Seric 	register int i;
70124941Seric 	extern bool shouldqueue();
70251920Seric 	extern bool readqf();
7034632Seric 
7047677Seric 	if (tTd(40, 1))
7055037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7064632Seric 
7074632Seric 	/*
70824941Seric 	**  Ignore jobs that are too expensive for the moment.
7094632Seric 	*/
7104632Seric 
71157438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7124632Seric 	{
71324941Seric 		if (Verbose)
71424967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7154632Seric 		return;
7164632Seric 	}
7174632Seric 
71824941Seric 	/*
71924941Seric 	**  Fork for work.
72024941Seric 	*/
72124941Seric 
72224941Seric 	if (ForkQueueRuns)
72324941Seric 	{
72424941Seric 		i = fork();
72524941Seric 		if (i < 0)
72624941Seric 		{
72724941Seric 			syserr("dowork: cannot fork");
72824941Seric 			return;
72924941Seric 		}
73024941Seric 	}
73124941Seric 	else
73224941Seric 	{
73324941Seric 		i = 0;
73424941Seric 	}
73524941Seric 
7364632Seric 	if (i == 0)
7374632Seric 	{
7384632Seric 		/*
7394632Seric 		**  CHILD
7408148Seric 		**	Lock the control file to avoid duplicate deliveries.
7418148Seric 		**		Then run the file as though we had just read it.
7427350Seric 		**	We save an idea of the temporary name so we
7437350Seric 		**		can recover on interrupt.
7444632Seric 		*/
7454632Seric 
7467763Seric 		/* set basic modes, etc. */
7477356Seric 		(void) alarm(0);
74855012Seric 		clearenvelope(e, FALSE);
7494632Seric 		QueueRun = TRUE;
7509377Seric 		ErrorMode = EM_MAIL;
75155012Seric 		e->e_id = &w->w_name[2];
7527876Seric # ifdef LOG
75358020Seric 		if (LogLevel > 76)
75455012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7557881Seric 			       getpid());
75656795Seric # endif /* LOG */
7577763Seric 
7587763Seric 		/* don't use the headers from sendmail.cf... */
75955012Seric 		e->e_header = NULL;
7607763Seric 
76151920Seric 		/* read the queue control file -- return if locked */
76255012Seric 		if (!readqf(e))
7636980Seric 		{
76424941Seric 			if (ForkQueueRuns)
76524941Seric 				exit(EX_OK);
76624941Seric 			else
76724941Seric 				return;
7686980Seric 		}
7696980Seric 
77055012Seric 		e->e_flags |= EF_INQUEUE;
77157642Seric 		eatheader(e, TRUE);
7726980Seric 
7736980Seric 		/* do the delivery */
77455012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
77555012Seric 			sendall(e, SM_DELIVER);
7766980Seric 
7776980Seric 		/* finish up and exit */
77824941Seric 		if (ForkQueueRuns)
77924941Seric 			finis();
78024941Seric 		else
78155012Seric 			dropenvelope(e);
7824632Seric 	}
78324941Seric 	else
78424941Seric 	{
78524941Seric 		/*
78624941Seric 		**  Parent -- pick up results.
78724941Seric 		*/
7884632Seric 
78924941Seric 		errno = 0;
79024941Seric 		(void) waitfor(i);
79124941Seric 	}
7924632Seric }
7934632Seric /*
7944632Seric **  READQF -- read queue file and set up environment.
7954632Seric **
7964632Seric **	Parameters:
7979377Seric **		e -- the envelope of the job to run.
7984632Seric **
7994632Seric **	Returns:
80051920Seric **		TRUE if it successfully read the queue file.
80151920Seric **		FALSE otherwise.
8024632Seric **
8034632Seric **	Side Effects:
80451920Seric **		The queue file is returned locked.
8054632Seric */
8064632Seric 
80751920Seric bool
80851920Seric readqf(e)
8099377Seric 	register ENVELOPE *e;
8104632Seric {
81117477Seric 	char *qf;
81217477Seric 	register FILE *qfp;
81354974Seric 	ADDRESS *ctladdr;
81456400Seric 	struct stat st;
81557135Seric 	char *bp;
81657135Seric 	char buf[MAXLINE];
8179348Seric 	extern char *fgetfolded();
81824954Seric 	extern long atol();
81954974Seric 	extern ADDRESS *setctluser();
82058689Seric 	extern bool lockfile();
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 
85958689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
86040934Srick 	{
86158689Seric 		/* being processed by another queuer */
86258689Seric 		if (Verbose)
86358689Seric 			printf("%s: locked\n", e->e_id);
86451920Seric # ifdef LOG
86558689Seric 		if (LogLevel > 19)
86658689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
86756795Seric # endif /* LOG */
86840934Srick 		(void) fclose(qfp);
86951920Seric 		return FALSE;
87040934Srick 	}
87140934Srick 
87251920Seric 	/* save this lock */
87351920Seric 	e->e_lockfp = qfp;
87451920Seric 
87540934Srick 	/* do basic system initialization */
87655012Seric 	initsys(e);
87740934Srick 
87817477Seric 	FileName = qf;
8799377Seric 	LineNumber = 0;
88051920Seric 	if (Verbose)
8819377Seric 		printf("\nRunning %s\n", e->e_id);
88254974Seric 	ctladdr = NULL;
88357135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
8844632Seric 	{
88557529Seric 		struct stat st;
88657529Seric 
88726504Seric 		if (tTd(40, 4))
88857135Seric 			printf("+++++ %s\n", bp);
88957135Seric 		switch (bp[0])
8904632Seric 		{
89140973Sbostic 		  case 'C':		/* specify controlling user */
89257135Seric 			ctladdr = setctluser(&bp[1]);
89340973Sbostic 			break;
89440973Sbostic 
8954632Seric 		  case 'R':		/* specify recipient */
89658082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
8974632Seric 			break;
8984632Seric 
89925687Seric 		  case 'E':		/* specify error recipient */
90058082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
90125687Seric 			break;
90225687Seric 
9034632Seric 		  case 'H':		/* header */
90457135Seric 			(void) chompheader(&bp[1], FALSE, e);
9054632Seric 			break;
9064632Seric 
90710108Seric 		  case 'M':		/* message */
90857135Seric 			e->e_message = newstr(&bp[1]);
90910108Seric 			break;
91010108Seric 
9114632Seric 		  case 'S':		/* sender */
91258704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9134632Seric 			break;
9144632Seric 
9154632Seric 		  case 'D':		/* data file name */
91657135Seric 			e->e_df = newstr(&bp[1]);
9179544Seric 			e->e_dfp = fopen(e->e_df, "r");
9189544Seric 			if (e->e_dfp == NULL)
91958020Seric 			{
9209377Seric 				syserr("readqf: cannot open %s", e->e_df);
92158020Seric 				e->e_msgsize = -1;
92258020Seric 			}
92358020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
92457529Seric 				e->e_msgsize = st.st_size;
9254632Seric 			break;
9264632Seric 
9277860Seric 		  case 'T':		/* init time */
92857135Seric 			e->e_ctime = atol(&bp[1]);
9294632Seric 			break;
9304632Seric 
9314634Seric 		  case 'P':		/* message priority */
93257135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9334634Seric 			break;
9344634Seric 
93553400Seric 		  case '$':		/* define macro */
93657135Seric 			define(bp[1], newstr(&bp[2]), e);
93753400Seric 			break;
93853400Seric 
93924941Seric 		  case '\0':		/* blank line; ignore */
94024941Seric 			break;
94124941Seric 
9424632Seric 		  default:
94324941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
94457135Seric 				LineNumber, bp);
9454632Seric 			break;
9464632Seric 		}
94757135Seric 
94857135Seric 		if (bp != buf)
94957135Seric 			free(bp);
9504632Seric 	}
9519377Seric 
9529377Seric 	FileName = NULL;
95324941Seric 
95424941Seric 	/*
95524941Seric 	**  If we haven't read any lines, this queue file is empty.
95624941Seric 	**  Arrange to remove it without referencing any null pointers.
95724941Seric 	*/
95824941Seric 
95924941Seric 	if (LineNumber == 0)
96024941Seric 	{
96124941Seric 		errno = 0;
96224941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
96324941Seric 	}
96451920Seric 	return TRUE;
9654632Seric }
9664632Seric /*
9679630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9689630Seric **
9699630Seric **	Parameters:
9709630Seric **		none.
9719630Seric **
9729630Seric **	Returns:
9739630Seric **		none.
9749630Seric **
9759630Seric **	Side Effects:
9769630Seric **		Prints a listing of the mail queue on the standard output.
9779630Seric */
9785182Seric 
9799630Seric printqueue()
9809630Seric {
9819630Seric 	register WORK *w;
9829630Seric 	FILE *f;
98310070Seric 	int nrequests;
9849630Seric 	char buf[MAXLINE];
9859630Seric 
9869630Seric 	/*
98758250Seric 	**  Check for permission to print the queue
98858250Seric 	*/
98958250Seric 
99058523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
99158250Seric 	{
99258250Seric 		struct stat st;
99358523Seric # ifdef NGROUPS
99458523Seric 		int n;
99558523Seric 		int gidset[NGROUPS];
99658523Seric # endif
99758250Seric 
99858517Seric 		if (stat(QueueDir, &st) < 0)
99958250Seric 		{
100058250Seric 			syserr("Cannot stat %s", QueueDir);
100158250Seric 			return;
100258250Seric 		}
100358523Seric # ifdef NGROUPS
100458523Seric 		n = getgroups(NGROUPS, gidset);
100558523Seric 		while (--n >= 0)
100658523Seric 		{
100758523Seric 			if (gidset[n] == st.st_gid)
100858523Seric 				break;
100958523Seric 		}
101058523Seric 		if (n < 0)
101158523Seric # else
101258250Seric 		if (getgid() != st.st_gid)
101358523Seric # endif
101458250Seric 		{
101558250Seric 			usrerr("510 You are not permitted to see the queue");
101658250Seric 			setstat(EX_NOPERM);
101758250Seric 			return;
101858250Seric 		}
101958250Seric 	}
102058250Seric 
102158250Seric 	/*
10229630Seric 	**  Read and order the queue.
10239630Seric 	*/
10249630Seric 
102524941Seric 	nrequests = orderq(TRUE);
10269630Seric 
10279630Seric 	/*
10289630Seric 	**  Print the work list that we have read.
10299630Seric 	*/
10309630Seric 
10319630Seric 	/* first see if there is anything */
103210070Seric 	if (nrequests <= 0)
10339630Seric 	{
103410070Seric 		printf("Mail queue is empty\n");
10359630Seric 		return;
10369630Seric 	}
10379630Seric 
103851920Seric 	CurrentLA = getla();	/* get load average */
103940934Srick 
104010096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
104125687Seric 	if (nrequests > QUEUESIZE)
104225687Seric 		printf(", only %d printed", QUEUESIZE);
104324979Seric 	if (Verbose)
1044*58716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
104524979Seric 	else
1046*58716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10479630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10489630Seric 	{
10499630Seric 		struct stat st;
105010070Seric 		auto time_t submittime = 0;
105110070Seric 		long dfsize = -1;
105210108Seric 		char message[MAXLINE];
105324941Seric 		extern bool shouldqueue();
105458689Seric 		extern bool lockfile();
10559630Seric 
105617468Seric 		f = fopen(w->w_name, "r");
105717468Seric 		if (f == NULL)
105817468Seric 		{
105917468Seric 			errno = 0;
106017468Seric 			continue;
106117468Seric 		}
10629630Seric 		printf("%7s", w->w_name + 2);
106358689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
106410070Seric 			printf("*");
106557438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
106624941Seric 			printf("X");
106710070Seric 		else
106810070Seric 			printf(" ");
106910070Seric 		errno = 0;
107017468Seric 
107110108Seric 		message[0] = '\0';
10729630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10739630Seric 		{
107453400Seric 			register int i;
107553400Seric 
10769630Seric 			fixcrlf(buf, TRUE);
10779630Seric 			switch (buf[0])
10789630Seric 			{
107910108Seric 			  case 'M':	/* error message */
108053400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
108153400Seric 					i = sizeof message;
108253400Seric 				bcopy(&buf[1], message, i);
108353400Seric 				message[i] = '\0';
108410108Seric 				break;
108510108Seric 
10869630Seric 			  case 'S':	/* sender name */
108724979Seric 				if (Verbose)
108825027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
108925027Seric 					    w->w_pri, ctime(&submittime) + 4,
109024979Seric 					    &buf[1]);
109124979Seric 				else
109224979Seric 					printf("%8ld %.16s %.45s", dfsize,
109324979Seric 					    ctime(&submittime), &buf[1]);
109410108Seric 				if (message[0] != '\0')
109525027Seric 					printf("\n\t\t (%.60s)", message);
10969630Seric 				break;
109751920Seric 
109840973Sbostic 			  case 'C':	/* controlling user */
109954974Seric 				if (Verbose)
1100*58716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
1101*58716Seric 						&buf[1]);
110240973Sbostic 				break;
11039630Seric 
11049630Seric 			  case 'R':	/* recipient name */
110524979Seric 				if (Verbose)
1106*58716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
110724979Seric 				else
1108*58716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
11099630Seric 				break;
11109630Seric 
11119630Seric 			  case 'T':	/* creation time */
111224941Seric 				submittime = atol(&buf[1]);
11139630Seric 				break;
111410070Seric 
111510070Seric 			  case 'D':	/* data file name */
111610070Seric 				if (stat(&buf[1], &st) >= 0)
111710070Seric 					dfsize = st.st_size;
111810070Seric 				break;
11199630Seric 			}
11209630Seric 		}
112110070Seric 		if (submittime == (time_t) 0)
112210070Seric 			printf(" (no control file)");
11239630Seric 		printf("\n");
112423098Seric 		(void) fclose(f);
11259630Seric 	}
11269630Seric }
11279630Seric 
112856795Seric # endif /* QUEUE */
112917468Seric /*
113017468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
113117468Seric **
113217468Seric **	Assigns an id code if one does not already exist.
113317468Seric **	This code is very careful to avoid trashing existing files
113417468Seric **	under any circumstances.
113517468Seric **
113617468Seric **	Parameters:
113717468Seric **		e -- envelope to build it in/from.
113817468Seric **		type -- the file type, used as the first character
113917468Seric **			of the file name.
114017468Seric **
114117468Seric **	Returns:
114217468Seric **		a pointer to the new file name (in a static buffer).
114317468Seric **
114417468Seric **	Side Effects:
114551920Seric **		If no id code is already assigned, queuename will
114651920Seric **		assign an id code, create a qf file, and leave a
114751920Seric **		locked, open-for-write file pointer in the envelope.
114817468Seric */
114917468Seric 
115017468Seric char *
115117468Seric queuename(e, type)
115217468Seric 	register ENVELOPE *e;
115317468Seric 	char type;
115417468Seric {
115517468Seric 	static int pid = -1;
1156*58716Seric 	char c0;
115758680Seric 	static char c1 = 'A';
115858680Seric 	static char c2 = 'A';
1159*58716Seric 	time_t now;
1160*58716Seric 	struct tm *tm;
116158689Seric 	static char buf[MAXNAME];
116258689Seric 	extern bool lockfile();
116317468Seric 
116417468Seric 	if (e->e_id == NULL)
116517468Seric 	{
116617468Seric 		char qf[20];
116717468Seric 
116817468Seric 		/* find a unique id */
116917468Seric 		if (pid != getpid())
117017468Seric 		{
117117468Seric 			/* new process -- start back at "AA" */
117217468Seric 			pid = getpid();
1173*58716Seric 			now = curtime();
1174*58716Seric 			tm = localtime(&now);
1175*58716Seric 			c0 = 'A' + tm->tm_hour;
117617468Seric 			c1 = 'A';
117717468Seric 			c2 = 'A' - 1;
117817468Seric 		}
1179*58716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
118017468Seric 
118117468Seric 		while (c1 < '~' || c2 < 'Z')
118217468Seric 		{
118317468Seric 			int i;
118417468Seric 
118517468Seric 			if (c2 >= 'Z')
118617468Seric 			{
118717468Seric 				c1++;
118817468Seric 				c2 = 'A' - 1;
118917468Seric 			}
1190*58716Seric 			qf[3] = c1;
1191*58716Seric 			qf[4] = ++c2;
119217468Seric 			if (tTd(7, 20))
119340934Srick 				printf("queuename: trying \"%s\"\n", qf);
119417468Seric 
119540934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
119651920Seric 			if (i < 0)
119751920Seric 			{
119851920Seric 				if (errno == EEXIST)
119951920Seric 					continue;
120051920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
120151920Seric 					qf, QueueDir);
120251920Seric 				exit(EX_UNAVAILABLE);
120351920Seric 			}
120458689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
120551920Seric 			{
120651920Seric 				e->e_lockfp = fdopen(i, "w");
120740934Srick 				break;
120817468Seric 			}
120951920Seric 
121051920Seric 			/* a reader got the file; abandon it and try again */
121151920Seric 			(void) close(i);
121217468Seric 		}
121317468Seric 		if (c1 >= '~' && c2 >= 'Z')
121417468Seric 		{
121517468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
121617468Seric 				qf, QueueDir);
121717468Seric 			exit(EX_OSERR);
121817468Seric 		}
121917468Seric 		e->e_id = newstr(&qf[2]);
122017468Seric 		define('i', e->e_id, e);
122117468Seric 		if (tTd(7, 1))
122217468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
122317468Seric # ifdef LOG
122458020Seric 		if (LogLevel > 93)
122517468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
122656795Seric # endif /* LOG */
122717468Seric 	}
122817468Seric 
122917468Seric 	if (type == '\0')
123017468Seric 		return (NULL);
123117468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
123217468Seric 	if (tTd(7, 2))
123317468Seric 		printf("queuename: %s\n", buf);
123417468Seric 	return (buf);
123517468Seric }
123617468Seric /*
123717468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
123817468Seric **
123917468Seric **	Parameters:
124017468Seric **		e -- the envelope to unlock.
124117468Seric **
124217468Seric **	Returns:
124317468Seric **		none
124417468Seric **
124517468Seric **	Side Effects:
124617468Seric **		unlocks the queue for `e'.
124717468Seric */
124817468Seric 
124917468Seric unlockqueue(e)
125017468Seric 	ENVELOPE *e;
125117468Seric {
125258680Seric 	if (tTd(51, 4))
125358680Seric 		printf("unlockqueue(%s)\n", e->e_id);
125458680Seric 
125551920Seric 	/* if there is a lock file in the envelope, close it */
125651920Seric 	if (e->e_lockfp != NULL)
125758680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
125851920Seric 	e->e_lockfp = NULL;
125951920Seric 
126017468Seric 	/* remove the transcript */
126117468Seric # ifdef LOG
126258020Seric 	if (LogLevel > 87)
126317468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
126456795Seric # endif /* LOG */
126558680Seric 	if (!tTd(51, 104))
126617468Seric 		xunlink(queuename(e, 'x'));
126717468Seric 
126817468Seric }
126940973Sbostic /*
127054974Seric **  SETCTLUSER -- create a controlling address
127140973Sbostic **
127254974Seric **	Create a fake "address" given only a local login name; this is
127354974Seric **	used as a "controlling user" for future recipient addresses.
127440973Sbostic **
127540973Sbostic **	Parameters:
127654974Seric **		user -- the user name of the controlling user.
127740973Sbostic **
127840973Sbostic **	Returns:
127954974Seric **		An address descriptor for the controlling user.
128040973Sbostic **
128140973Sbostic **	Side Effects:
128240973Sbostic **		none.
128340973Sbostic */
128440973Sbostic 
128554974Seric ADDRESS *
128654974Seric setctluser(user)
128754974Seric 	char *user;
128840973Sbostic {
128954974Seric 	register ADDRESS *a;
129040973Sbostic 	struct passwd *pw;
129140973Sbostic 
129240973Sbostic 	/*
129354974Seric 	**  See if this clears our concept of controlling user.
129440973Sbostic 	*/
129540973Sbostic 
129654974Seric 	if (user == NULL || *user == '\0')
129758704Seric 		user = DefUser;
129840973Sbostic 
129940973Sbostic 	/*
130054974Seric 	**  Set up addr fields for controlling user.
130140973Sbostic 	*/
130240973Sbostic 
130354974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
130454974Seric 	bzero((char *) a, sizeof *a);
130554974Seric 	if ((pw = getpwnam(user)) != NULL)
130640973Sbostic 	{
130740973Sbostic 		a->q_home = newstr(pw->pw_dir);
130840973Sbostic 		a->q_uid = pw->pw_uid;
130940973Sbostic 		a->q_gid = pw->pw_gid;
131057642Seric 		a->q_user = newstr(user);
131140973Sbostic 	}
131240973Sbostic 	else
131340973Sbostic 	{
131440973Sbostic 		a->q_uid = DefUid;
131540973Sbostic 		a->q_gid = DefGid;
131657642Seric 		a->q_user = newstr(DefUser);
131740973Sbostic 	}
131840973Sbostic 
131958294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
132056328Seric 	a->q_mailer = LocalMailer;
132154974Seric 	return a;
132240973Sbostic }
1323