xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58732)
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*58732Seric static char sccsid[] = "@(#)queue.c	6.35 (Berkeley) 03/19/93 (with queueing)";
1433731Sbostic #else
15*58732Seric static char sccsid[] = "@(#)queue.c	6.35 (Berkeley) 03/19/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 
280*58732Seric 	fflush(tfp);
281*58732Seric 	if (ferror(tfp))
282*58732Seric 	{
283*58732Seric 		if (newid)
284*58732Seric 			syserr("!552 Error writing control file %s", tf);
285*58732Seric 		else
286*58732Seric 			syserr("!452 Error writing control file %s", tf);
287*58732Seric 	}
288*58732Seric 
28951920Seric 	if (!newid)
29051920Seric 	{
29151920Seric 		qf = queuename(e, 'q');
29251920Seric 		if (rename(tf, qf) < 0)
29351920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29451920Seric 		if (e->e_lockfp != NULL)
29558680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
29651920Seric 		e->e_lockfp = tfp;
29751920Seric 	}
29851920Seric 	else
29951920Seric 		qf = tf;
30041636Srick 	errno = 0;
3017391Seric 
3027677Seric # ifdef LOG
3037677Seric 	/* save log info */
30458020Seric 	if (LogLevel > 79)
3057878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
30656795Seric # endif /* LOG */
30740934Srick 	fflush(tfp);
30851920Seric 	return;
3094632Seric }
31054974Seric 
31154974Seric printctladdr(a, tfp)
31254974Seric 	ADDRESS *a;
31354974Seric 	FILE *tfp;
31454974Seric {
31554974Seric 	char *u;
31654974Seric 	struct passwd *pw;
31754974Seric 	extern struct passwd *getpwuid();
31854974Seric 
31954974Seric 	if (a == NULL)
32054974Seric 	{
32154974Seric 		fprintf(tfp, "C\n");
32254974Seric 		return;
32354974Seric 	}
32454974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
32554974Seric 		u = DefUser;
32654974Seric 	else
32754974Seric 		u = pw->pw_name;
32854974Seric 	fprintf(tfp, "C%s\n", u);
32954974Seric }
33054974Seric 
3314632Seric /*
3324632Seric **  RUNQUEUE -- run the jobs in the queue.
3334632Seric **
3344632Seric **	Gets the stuff out of the queue in some presumably logical
3354632Seric **	order and processes them.
3364632Seric **
3374632Seric **	Parameters:
33824941Seric **		forkflag -- TRUE if the queue scanning should be done in
33924941Seric **			a child process.  We double-fork so it is not our
34024941Seric **			child and we don't have to clean up after it.
3414632Seric **
3424632Seric **	Returns:
3434632Seric **		none.
3444632Seric **
3454632Seric **	Side Effects:
3464632Seric **		runs things in the mail queue.
3474632Seric */
3484632Seric 
34955360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
35055360Seric 
35155360Seric runqueue(forkflag)
3524639Seric 	bool forkflag;
3534632Seric {
35424953Seric 	extern bool shouldqueue();
35555360Seric 	register ENVELOPE *e;
35655360Seric 	extern ENVELOPE BlankEnvelope;
35755360Seric 	extern ENVELOPE *newenvelope();
35824953Seric 
3597466Seric 	/*
36024953Seric 	**  If no work will ever be selected, don't even bother reading
36124953Seric 	**  the queue.
36224953Seric 	*/
36324953Seric 
36451920Seric 	CurrentLA = getla();	/* get load average */
36540934Srick 
36658132Seric 	if (shouldqueue(0L, curtime()))
36724953Seric 	{
36824953Seric 		if (Verbose)
36924953Seric 			printf("Skipping queue run -- load average too high\n");
37058107Seric 		if (forkflag && QueueIntvl != 0)
37158107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
37255360Seric 		return;
37324953Seric 	}
37424953Seric 
37524953Seric 	/*
3767466Seric 	**  See if we want to go off and do other useful work.
3777466Seric 	*/
3784639Seric 
3794639Seric 	if (forkflag)
3804639Seric 	{
3817943Seric 		int pid;
3827943Seric 
3837943Seric 		pid = dofork();
3847943Seric 		if (pid != 0)
3854639Seric 		{
38646928Sbostic 			extern void reapchild();
38725184Seric 
3887943Seric 			/* parent -- pick up intermediate zombie */
38925184Seric #ifndef SIGCHLD
3909377Seric 			(void) waitfor(pid);
39156795Seric #else /* SIGCHLD */
39225184Seric 			(void) signal(SIGCHLD, reapchild);
39356795Seric #endif /* SIGCHLD */
3947690Seric 			if (QueueIntvl != 0)
3959348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3964639Seric 			return;
3974639Seric 		}
3987943Seric 		/* child -- double fork */
39925184Seric #ifndef SIGCHLD
4007943Seric 		if (fork() != 0)
4017943Seric 			exit(EX_OK);
40256795Seric #else /* SIGCHLD */
40325184Seric 		(void) signal(SIGCHLD, SIG_DFL);
40456795Seric #endif /* SIGCHLD */
4054639Seric 	}
40624941Seric 
40740934Srick 	setproctitle("running queue: %s", QueueDir);
40824941Seric 
4097876Seric # ifdef LOG
41058020Seric 	if (LogLevel > 69)
41155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
41255360Seric 			QueueDir, getpid(), forkflag);
41356795Seric # endif /* LOG */
4144639Seric 
4157466Seric 	/*
41610205Seric 	**  Release any resources used by the daemon code.
41710205Seric 	*/
41810205Seric 
41910205Seric # ifdef DAEMON
42010205Seric 	clrdaemon();
42156795Seric # endif /* DAEMON */
42210205Seric 
42310205Seric 	/*
42455360Seric 	**  Create ourselves an envelope
42555360Seric 	*/
42655360Seric 
42755360Seric 	CurEnv = &QueueEnvelope;
42858179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
42955360Seric 	e->e_flags = BlankEnvelope.e_flags;
43055360Seric 
43155360Seric 	/*
43227175Seric 	**  Make sure the alias database is open.
43327175Seric 	*/
43427175Seric 
43555012Seric 	initaliases(AliasFile, FALSE, e);
43627175Seric 
43727175Seric 	/*
4387466Seric 	**  Start making passes through the queue.
4397466Seric 	**	First, read and sort the entire queue.
4407466Seric 	**	Then, process the work in that order.
4417466Seric 	**		But if you take too long, start over.
4427466Seric 	*/
4437466Seric 
4447943Seric 	/* order the existing work requests */
44524954Seric 	(void) orderq(FALSE);
4467690Seric 
4477943Seric 	/* process them once at a time */
4487943Seric 	while (WorkQ != NULL)
4494639Seric 	{
4507943Seric 		WORK *w = WorkQ;
4517881Seric 
4527943Seric 		WorkQ = WorkQ->w_next;
45355012Seric 		dowork(w, e);
4547943Seric 		free(w->w_name);
4557943Seric 		free((char *) w);
4564639Seric 	}
45729866Seric 
45829866Seric 	/* exit without the usual cleanup */
45955467Seric 	e->e_id = NULL;
46055467Seric 	finis();
4614634Seric }
4624634Seric /*
4634632Seric **  ORDERQ -- order the work queue.
4644632Seric **
4654632Seric **	Parameters:
46624941Seric **		doall -- if set, include everything in the queue (even
46724941Seric **			the jobs that cannot be run because the load
46824941Seric **			average is too high).  Otherwise, exclude those
46924941Seric **			jobs.
4704632Seric **
4714632Seric **	Returns:
47210121Seric **		The number of request in the queue (not necessarily
47310121Seric **		the number of requests in WorkQ however).
4744632Seric **
4754632Seric **	Side Effects:
4764632Seric **		Sets WorkQ to the queue of available work, in order.
4774632Seric */
4784632Seric 
47925687Seric # define NEED_P		001
48025687Seric # define NEED_T		002
48158318Seric # define NEED_R		004
48258318Seric # define NEED_S		010
4834632Seric 
48424941Seric orderq(doall)
48524941Seric 	bool doall;
4864632Seric {
4876625Sglickman 	register struct direct *d;
4884632Seric 	register WORK *w;
4896625Sglickman 	DIR *f;
4904632Seric 	register int i;
49125687Seric 	WORK wlist[QUEUESIZE+1];
49210070Seric 	int wn = -1;
4934632Seric 	extern workcmpf();
4944632Seric 
49558318Seric 	if (tTd(41, 1))
49658318Seric 	{
49758318Seric 		printf("orderq:\n");
49858318Seric 		if (QueueLimitId != NULL)
49958318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
50058318Seric 		if (QueueLimitSender != NULL)
50158318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
50258318Seric 		if (QueueLimitRecipient != NULL)
50358318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
50458318Seric 	}
50558318Seric 
5064632Seric 	/* clear out old WorkQ */
5074632Seric 	for (w = WorkQ; w != NULL; )
5084632Seric 	{
5094632Seric 		register WORK *nw = w->w_next;
5104632Seric 
5114632Seric 		WorkQ = nw;
5124632Seric 		free(w->w_name);
5134632Seric 		free((char *) w);
5144632Seric 		w = nw;
5154632Seric 	}
5164632Seric 
5174632Seric 	/* open the queue directory */
5188148Seric 	f = opendir(".");
5194632Seric 	if (f == NULL)
5204632Seric 	{
5218148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
52210070Seric 		return (0);
5234632Seric 	}
5244632Seric 
5254632Seric 	/*
5264632Seric 	**  Read the work directory.
5274632Seric 	*/
5284632Seric 
52910070Seric 	while ((d = readdir(f)) != NULL)
5304632Seric 	{
5319377Seric 		FILE *cf;
5324632Seric 		char lbuf[MAXNAME];
53357642Seric 		extern bool shouldqueue();
53458318Seric 		extern bool strcontainedin();
5354632Seric 
5364632Seric 		/* is this an interesting entry? */
5377812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5384632Seric 			continue;
5394632Seric 
54058318Seric 		if (QueueLimitId != NULL &&
54158318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
54258318Seric 			continue;
54358318Seric 
54458722Seric 		/*
54558722Seric 		**  Check queue name for plausibility.  This handles
54658722Seric 		**  both old and new type ids.
54758722Seric 		*/
54858722Seric 
54958722Seric 		i = strlen(d->d_name);
55058722Seric 		if (i != 9 && i != 10)
55158020Seric 		{
55258020Seric 			if (Verbose)
55358020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
55458020Seric #ifdef LOG
55558020Seric 			if (LogLevel > 3)
55658020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
55758020Seric 					d->d_name);
55858020Seric #endif
55958020Seric 			if (strlen(d->d_name) >= MAXNAME)
56058020Seric 				d->d_name[MAXNAME - 1] = '\0';
56158020Seric 			strcpy(lbuf, d->d_name);
56258020Seric 			lbuf[0] = 'Q';
56358020Seric 			(void) rename(d->d_name, lbuf);
56458020Seric 			continue;
56558020Seric 		}
56658020Seric 
56710070Seric 		/* yes -- open control file (if not too many files) */
56825687Seric 		if (++wn >= QUEUESIZE)
56910070Seric 			continue;
57058318Seric 
5718148Seric 		cf = fopen(d->d_name, "r");
5724632Seric 		if (cf == NULL)
5734632Seric 		{
5747055Seric 			/* this may be some random person sending hir msgs */
5757055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
57610090Seric 			if (tTd(41, 2))
57710090Seric 				printf("orderq: cannot open %s (%d)\n",
57810090Seric 					d->d_name, errno);
5797055Seric 			errno = 0;
58010090Seric 			wn--;
5814632Seric 			continue;
5824632Seric 		}
58325687Seric 		w = &wlist[wn];
58425687Seric 		w->w_name = newstr(d->d_name);
5854632Seric 
58625027Seric 		/* make sure jobs in creation don't clog queue */
58725687Seric 		w->w_pri = 0x7fffffff;
58825687Seric 		w->w_ctime = 0;
58925027Seric 
5904632Seric 		/* extract useful information */
59125687Seric 		i = NEED_P | NEED_T;
59258318Seric 		if (QueueLimitSender != NULL)
59358318Seric 			i |= NEED_S;
59458318Seric 		if (QueueLimitRecipient != NULL)
59558318Seric 			i |= NEED_R;
59625687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5974632Seric 		{
59824954Seric 			extern long atol();
59958318Seric 			extern bool strcontainedin();
60024954Seric 
60124941Seric 			switch (lbuf[0])
6024632Seric 			{
60324941Seric 			  case 'P':
60425687Seric 				w->w_pri = atol(&lbuf[1]);
60525687Seric 				i &= ~NEED_P;
6064632Seric 				break;
60725013Seric 
60825013Seric 			  case 'T':
60925687Seric 				w->w_ctime = atol(&lbuf[1]);
61025687Seric 				i &= ~NEED_T;
61125013Seric 				break;
61258318Seric 
61358318Seric 			  case 'R':
61458318Seric 				if (QueueLimitRecipient != NULL &&
61558318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
61658318Seric 					i &= ~NEED_R;
61758318Seric 				break;
61858318Seric 
61958318Seric 			  case 'S':
62058318Seric 				if (QueueLimitSender != NULL &&
62158318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
62258318Seric 					i &= ~NEED_S;
62358318Seric 				break;
6244632Seric 			}
6254632Seric 		}
6264632Seric 		(void) fclose(cf);
62724953Seric 
62858318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
62958318Seric 		    bitset(NEED_R|NEED_S, i))
63024953Seric 		{
63124953Seric 			/* don't even bother sorting this job in */
63224953Seric 			wn--;
63324953Seric 		}
6344632Seric 	}
6356625Sglickman 	(void) closedir(f);
63610090Seric 	wn++;
6374632Seric 
6384632Seric 	/*
6394632Seric 	**  Sort the work directory.
6404632Seric 	*/
6414632Seric 
64225687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6434632Seric 
6444632Seric 	/*
6454632Seric 	**  Convert the work list into canonical form.
6469377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6474632Seric 	*/
6484632Seric 
64924981Seric 	WorkQ = NULL;
65025687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6514632Seric 	{
6524632Seric 		w = (WORK *) xalloc(sizeof *w);
6534632Seric 		w->w_name = wlist[i].w_name;
6544632Seric 		w->w_pri = wlist[i].w_pri;
65525013Seric 		w->w_ctime = wlist[i].w_ctime;
65624981Seric 		w->w_next = WorkQ;
65724981Seric 		WorkQ = w;
6584632Seric 	}
6594632Seric 
6607677Seric 	if (tTd(40, 1))
6614632Seric 	{
6624632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6635037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6644632Seric 	}
66510070Seric 
66610090Seric 	return (wn);
6674632Seric }
6684632Seric /*
6697677Seric **  WORKCMPF -- compare function for ordering work.
6704632Seric **
6714632Seric **	Parameters:
6724632Seric **		a -- the first argument.
6734632Seric **		b -- the second argument.
6744632Seric **
6754632Seric **	Returns:
67624981Seric **		-1 if a < b
67724981Seric **		 0 if a == b
67824981Seric **		+1 if a > b
6794632Seric **
6804632Seric **	Side Effects:
6814632Seric **		none.
6824632Seric */
6834632Seric 
6844632Seric workcmpf(a, b)
6855037Seric 	register WORK *a;
6865037Seric 	register WORK *b;
6874632Seric {
68857438Seric 	long pa = a->w_pri;
68957438Seric 	long pb = b->w_pri;
69024941Seric 
69124941Seric 	if (pa == pb)
6924632Seric 		return (0);
69324941Seric 	else if (pa > pb)
69424981Seric 		return (1);
69524981Seric 	else
69610121Seric 		return (-1);
6974632Seric }
6984632Seric /*
6994632Seric **  DOWORK -- do a work request.
7004632Seric **
7014632Seric **	Parameters:
7024632Seric **		w -- the work request to be satisfied.
7034632Seric **
7044632Seric **	Returns:
7054632Seric **		none.
7064632Seric **
7074632Seric **	Side Effects:
7084632Seric **		The work request is satisfied if possible.
7094632Seric */
7104632Seric 
71155012Seric dowork(w, e)
7124632Seric 	register WORK *w;
71355012Seric 	register ENVELOPE *e;
7144632Seric {
7154632Seric 	register int i;
71624941Seric 	extern bool shouldqueue();
71751920Seric 	extern bool readqf();
7184632Seric 
7197677Seric 	if (tTd(40, 1))
7205037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
7214632Seric 
7224632Seric 	/*
72324941Seric 	**  Ignore jobs that are too expensive for the moment.
7244632Seric 	*/
7254632Seric 
72657438Seric 	if (shouldqueue(w->w_pri, w->w_ctime))
7274632Seric 	{
72824941Seric 		if (Verbose)
72924967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
7304632Seric 		return;
7314632Seric 	}
7324632Seric 
73324941Seric 	/*
73424941Seric 	**  Fork for work.
73524941Seric 	*/
73624941Seric 
73724941Seric 	if (ForkQueueRuns)
73824941Seric 	{
73924941Seric 		i = fork();
74024941Seric 		if (i < 0)
74124941Seric 		{
74224941Seric 			syserr("dowork: cannot fork");
74324941Seric 			return;
74424941Seric 		}
74524941Seric 	}
74624941Seric 	else
74724941Seric 	{
74824941Seric 		i = 0;
74924941Seric 	}
75024941Seric 
7514632Seric 	if (i == 0)
7524632Seric 	{
7534632Seric 		/*
7544632Seric 		**  CHILD
7558148Seric 		**	Lock the control file to avoid duplicate deliveries.
7568148Seric 		**		Then run the file as though we had just read it.
7577350Seric 		**	We save an idea of the temporary name so we
7587350Seric 		**		can recover on interrupt.
7594632Seric 		*/
7604632Seric 
7617763Seric 		/* set basic modes, etc. */
7627356Seric 		(void) alarm(0);
76355012Seric 		clearenvelope(e, FALSE);
7644632Seric 		QueueRun = TRUE;
7659377Seric 		ErrorMode = EM_MAIL;
76655012Seric 		e->e_id = &w->w_name[2];
7677876Seric # ifdef LOG
76858020Seric 		if (LogLevel > 76)
76955012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7707881Seric 			       getpid());
77156795Seric # endif /* LOG */
7727763Seric 
7737763Seric 		/* don't use the headers from sendmail.cf... */
77455012Seric 		e->e_header = NULL;
7757763Seric 
77651920Seric 		/* read the queue control file -- return if locked */
77755012Seric 		if (!readqf(e))
7786980Seric 		{
77924941Seric 			if (ForkQueueRuns)
78024941Seric 				exit(EX_OK);
78124941Seric 			else
78224941Seric 				return;
7836980Seric 		}
7846980Seric 
78555012Seric 		e->e_flags |= EF_INQUEUE;
78657642Seric 		eatheader(e, TRUE);
7876980Seric 
7886980Seric 		/* do the delivery */
78955012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
79055012Seric 			sendall(e, SM_DELIVER);
7916980Seric 
7926980Seric 		/* finish up and exit */
79324941Seric 		if (ForkQueueRuns)
79424941Seric 			finis();
79524941Seric 		else
79655012Seric 			dropenvelope(e);
7974632Seric 	}
79824941Seric 	else
79924941Seric 	{
80024941Seric 		/*
80124941Seric 		**  Parent -- pick up results.
80224941Seric 		*/
8034632Seric 
80424941Seric 		errno = 0;
80524941Seric 		(void) waitfor(i);
80624941Seric 	}
8074632Seric }
8084632Seric /*
8094632Seric **  READQF -- read queue file and set up environment.
8104632Seric **
8114632Seric **	Parameters:
8129377Seric **		e -- the envelope of the job to run.
8134632Seric **
8144632Seric **	Returns:
81551920Seric **		TRUE if it successfully read the queue file.
81651920Seric **		FALSE otherwise.
8174632Seric **
8184632Seric **	Side Effects:
81951920Seric **		The queue file is returned locked.
8204632Seric */
8214632Seric 
82251920Seric bool
82351920Seric readqf(e)
8249377Seric 	register ENVELOPE *e;
8254632Seric {
82617477Seric 	char *qf;
82717477Seric 	register FILE *qfp;
82854974Seric 	ADDRESS *ctladdr;
82956400Seric 	struct stat st;
83057135Seric 	char *bp;
83157135Seric 	char buf[MAXLINE];
8329348Seric 	extern char *fgetfolded();
83324954Seric 	extern long atol();
83454974Seric 	extern ADDRESS *setctluser();
83558689Seric 	extern bool lockfile();
8364632Seric 
8374632Seric 	/*
83817468Seric 	**  Read and process the file.
8394632Seric 	*/
8404632Seric 
84117477Seric 	qf = queuename(e, 'q');
84251937Seric 	qfp = fopen(qf, "r+");
84317477Seric 	if (qfp == NULL)
84417477Seric 	{
84540934Srick 		if (errno != ENOENT)
84640934Srick 			syserr("readqf: no control file %s", qf);
84751920Seric 		return FALSE;
84817477Seric 	}
84940934Srick 
85056400Seric 	/*
85156400Seric 	**  Check the queue file for plausibility to avoid attacks.
85256400Seric 	*/
85356400Seric 
85456400Seric 	if (fstat(fileno(qfp), &st) < 0)
85556400Seric 	{
85656400Seric 		/* must have been being processed by someone else */
85756400Seric 		fclose(qfp);
85856400Seric 		return FALSE;
85956400Seric 	}
86056400Seric 
86157135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
86256400Seric 	{
86356400Seric # ifdef LOG
86456400Seric 		if (LogLevel > 0)
86556400Seric 		{
86656400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
86756400Seric 				e->e_id, st.st_uid, st.st_mode);
86856400Seric 		}
86956795Seric # endif /* LOG */
87056400Seric 		fclose(qfp);
87156400Seric 		return FALSE;
87256400Seric 	}
87356400Seric 
87458689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
87540934Srick 	{
87658689Seric 		/* being processed by another queuer */
87758689Seric 		if (Verbose)
87858689Seric 			printf("%s: locked\n", e->e_id);
87951920Seric # ifdef LOG
88058689Seric 		if (LogLevel > 19)
88158689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
88256795Seric # endif /* LOG */
88340934Srick 		(void) fclose(qfp);
88451920Seric 		return FALSE;
88540934Srick 	}
88640934Srick 
88751920Seric 	/* save this lock */
88851920Seric 	e->e_lockfp = qfp;
88951920Seric 
89040934Srick 	/* do basic system initialization */
89155012Seric 	initsys(e);
89240934Srick 
89317477Seric 	FileName = qf;
8949377Seric 	LineNumber = 0;
89551920Seric 	if (Verbose)
8969377Seric 		printf("\nRunning %s\n", e->e_id);
89754974Seric 	ctladdr = NULL;
89857135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
8994632Seric 	{
90057529Seric 		struct stat st;
90157529Seric 
90226504Seric 		if (tTd(40, 4))
90357135Seric 			printf("+++++ %s\n", bp);
90457135Seric 		switch (bp[0])
9054632Seric 		{
90640973Sbostic 		  case 'C':		/* specify controlling user */
90757135Seric 			ctladdr = setctluser(&bp[1]);
90840973Sbostic 			break;
90940973Sbostic 
9104632Seric 		  case 'R':		/* specify recipient */
91158082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9124632Seric 			break;
9134632Seric 
91425687Seric 		  case 'E':		/* specify error recipient */
91558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
91625687Seric 			break;
91725687Seric 
9184632Seric 		  case 'H':		/* header */
91957135Seric 			(void) chompheader(&bp[1], FALSE, e);
9204632Seric 			break;
9214632Seric 
92210108Seric 		  case 'M':		/* message */
92357135Seric 			e->e_message = newstr(&bp[1]);
92410108Seric 			break;
92510108Seric 
9264632Seric 		  case 'S':		/* sender */
92758704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9284632Seric 			break;
9294632Seric 
9304632Seric 		  case 'D':		/* data file name */
93157135Seric 			e->e_df = newstr(&bp[1]);
9329544Seric 			e->e_dfp = fopen(e->e_df, "r");
9339544Seric 			if (e->e_dfp == NULL)
93458020Seric 			{
9359377Seric 				syserr("readqf: cannot open %s", e->e_df);
93658020Seric 				e->e_msgsize = -1;
93758020Seric 			}
93858020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
93957529Seric 				e->e_msgsize = st.st_size;
9404632Seric 			break;
9414632Seric 
9427860Seric 		  case 'T':		/* init time */
94357135Seric 			e->e_ctime = atol(&bp[1]);
9444632Seric 			break;
9454632Seric 
9464634Seric 		  case 'P':		/* message priority */
94757135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9484634Seric 			break;
9494634Seric 
95053400Seric 		  case '$':		/* define macro */
95157135Seric 			define(bp[1], newstr(&bp[2]), e);
95253400Seric 			break;
95353400Seric 
95424941Seric 		  case '\0':		/* blank line; ignore */
95524941Seric 			break;
95624941Seric 
9574632Seric 		  default:
95824941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
95957135Seric 				LineNumber, bp);
9604632Seric 			break;
9614632Seric 		}
96257135Seric 
96357135Seric 		if (bp != buf)
96457135Seric 			free(bp);
9654632Seric 	}
9669377Seric 
9679377Seric 	FileName = NULL;
96824941Seric 
96924941Seric 	/*
97024941Seric 	**  If we haven't read any lines, this queue file is empty.
97124941Seric 	**  Arrange to remove it without referencing any null pointers.
97224941Seric 	*/
97324941Seric 
97424941Seric 	if (LineNumber == 0)
97524941Seric 	{
97624941Seric 		errno = 0;
97724941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
97824941Seric 	}
97951920Seric 	return TRUE;
9804632Seric }
9814632Seric /*
9829630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9839630Seric **
9849630Seric **	Parameters:
9859630Seric **		none.
9869630Seric **
9879630Seric **	Returns:
9889630Seric **		none.
9899630Seric **
9909630Seric **	Side Effects:
9919630Seric **		Prints a listing of the mail queue on the standard output.
9929630Seric */
9935182Seric 
9949630Seric printqueue()
9959630Seric {
9969630Seric 	register WORK *w;
9979630Seric 	FILE *f;
99810070Seric 	int nrequests;
9999630Seric 	char buf[MAXLINE];
10009630Seric 
10019630Seric 	/*
100258250Seric 	**  Check for permission to print the queue
100358250Seric 	*/
100458250Seric 
100558523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
100658250Seric 	{
100758250Seric 		struct stat st;
100858523Seric # ifdef NGROUPS
100958523Seric 		int n;
101058523Seric 		int gidset[NGROUPS];
101158523Seric # endif
101258250Seric 
101358517Seric 		if (stat(QueueDir, &st) < 0)
101458250Seric 		{
101558250Seric 			syserr("Cannot stat %s", QueueDir);
101658250Seric 			return;
101758250Seric 		}
101858523Seric # ifdef NGROUPS
101958523Seric 		n = getgroups(NGROUPS, gidset);
102058523Seric 		while (--n >= 0)
102158523Seric 		{
102258523Seric 			if (gidset[n] == st.st_gid)
102358523Seric 				break;
102458523Seric 		}
102558523Seric 		if (n < 0)
102658523Seric # else
102758250Seric 		if (getgid() != st.st_gid)
102858523Seric # endif
102958250Seric 		{
103058250Seric 			usrerr("510 You are not permitted to see the queue");
103158250Seric 			setstat(EX_NOPERM);
103258250Seric 			return;
103358250Seric 		}
103458250Seric 	}
103558250Seric 
103658250Seric 	/*
10379630Seric 	**  Read and order the queue.
10389630Seric 	*/
10399630Seric 
104024941Seric 	nrequests = orderq(TRUE);
10419630Seric 
10429630Seric 	/*
10439630Seric 	**  Print the work list that we have read.
10449630Seric 	*/
10459630Seric 
10469630Seric 	/* first see if there is anything */
104710070Seric 	if (nrequests <= 0)
10489630Seric 	{
104910070Seric 		printf("Mail queue is empty\n");
10509630Seric 		return;
10519630Seric 	}
10529630Seric 
105351920Seric 	CurrentLA = getla();	/* get load average */
105440934Srick 
105510096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
105625687Seric 	if (nrequests > QUEUESIZE)
105725687Seric 		printf(", only %d printed", QUEUESIZE);
105824979Seric 	if (Verbose)
105958716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
106024979Seric 	else
106158716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
10629630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
10639630Seric 	{
10649630Seric 		struct stat st;
106510070Seric 		auto time_t submittime = 0;
106610070Seric 		long dfsize = -1;
106710108Seric 		char message[MAXLINE];
106824941Seric 		extern bool shouldqueue();
106958689Seric 		extern bool lockfile();
10709630Seric 
107117468Seric 		f = fopen(w->w_name, "r");
107217468Seric 		if (f == NULL)
107317468Seric 		{
107417468Seric 			errno = 0;
107517468Seric 			continue;
107617468Seric 		}
107758724Seric 		printf("%8s", w->w_name + 2);
107858689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
107910070Seric 			printf("*");
108057438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
108124941Seric 			printf("X");
108210070Seric 		else
108310070Seric 			printf(" ");
108410070Seric 		errno = 0;
108517468Seric 
108610108Seric 		message[0] = '\0';
10879630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
10889630Seric 		{
108953400Seric 			register int i;
109053400Seric 
10919630Seric 			fixcrlf(buf, TRUE);
10929630Seric 			switch (buf[0])
10939630Seric 			{
109410108Seric 			  case 'M':	/* error message */
109553400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
109653400Seric 					i = sizeof message;
109753400Seric 				bcopy(&buf[1], message, i);
109853400Seric 				message[i] = '\0';
109910108Seric 				break;
110010108Seric 
11019630Seric 			  case 'S':	/* sender name */
110224979Seric 				if (Verbose)
110325027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
110425027Seric 					    w->w_pri, ctime(&submittime) + 4,
110524979Seric 					    &buf[1]);
110624979Seric 				else
110724979Seric 					printf("%8ld %.16s %.45s", dfsize,
110824979Seric 					    ctime(&submittime), &buf[1]);
110910108Seric 				if (message[0] != '\0')
111025027Seric 					printf("\n\t\t (%.60s)", message);
11119630Seric 				break;
111251920Seric 
111340973Sbostic 			  case 'C':	/* controlling user */
111454974Seric 				if (Verbose)
111558716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
111658716Seric 						&buf[1]);
111740973Sbostic 				break;
11189630Seric 
11199630Seric 			  case 'R':	/* recipient name */
112024979Seric 				if (Verbose)
112158716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
112224979Seric 				else
112358716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
11249630Seric 				break;
11259630Seric 
11269630Seric 			  case 'T':	/* creation time */
112724941Seric 				submittime = atol(&buf[1]);
11289630Seric 				break;
112910070Seric 
113010070Seric 			  case 'D':	/* data file name */
113110070Seric 				if (stat(&buf[1], &st) >= 0)
113210070Seric 					dfsize = st.st_size;
113310070Seric 				break;
11349630Seric 			}
11359630Seric 		}
113610070Seric 		if (submittime == (time_t) 0)
113710070Seric 			printf(" (no control file)");
11389630Seric 		printf("\n");
113923098Seric 		(void) fclose(f);
11409630Seric 	}
11419630Seric }
11429630Seric 
114356795Seric # endif /* QUEUE */
114417468Seric /*
114517468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
114617468Seric **
114717468Seric **	Assigns an id code if one does not already exist.
114817468Seric **	This code is very careful to avoid trashing existing files
114917468Seric **	under any circumstances.
115017468Seric **
115117468Seric **	Parameters:
115217468Seric **		e -- envelope to build it in/from.
115317468Seric **		type -- the file type, used as the first character
115417468Seric **			of the file name.
115517468Seric **
115617468Seric **	Returns:
115717468Seric **		a pointer to the new file name (in a static buffer).
115817468Seric **
115917468Seric **	Side Effects:
116051920Seric **		If no id code is already assigned, queuename will
116151920Seric **		assign an id code, create a qf file, and leave a
116251920Seric **		locked, open-for-write file pointer in the envelope.
116317468Seric */
116417468Seric 
116517468Seric char *
116617468Seric queuename(e, type)
116717468Seric 	register ENVELOPE *e;
116817468Seric 	char type;
116917468Seric {
117017468Seric 	static int pid = -1;
117158716Seric 	char c0;
117258680Seric 	static char c1 = 'A';
117358680Seric 	static char c2 = 'A';
117458716Seric 	time_t now;
117558716Seric 	struct tm *tm;
117658689Seric 	static char buf[MAXNAME];
117758689Seric 	extern bool lockfile();
117817468Seric 
117917468Seric 	if (e->e_id == NULL)
118017468Seric 	{
118117468Seric 		char qf[20];
118217468Seric 
118317468Seric 		/* find a unique id */
118417468Seric 		if (pid != getpid())
118517468Seric 		{
118617468Seric 			/* new process -- start back at "AA" */
118717468Seric 			pid = getpid();
118858716Seric 			now = curtime();
118958716Seric 			tm = localtime(&now);
119058716Seric 			c0 = 'A' + tm->tm_hour;
119117468Seric 			c1 = 'A';
119217468Seric 			c2 = 'A' - 1;
119317468Seric 		}
119458716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
119517468Seric 
119617468Seric 		while (c1 < '~' || c2 < 'Z')
119717468Seric 		{
119817468Seric 			int i;
119917468Seric 
120017468Seric 			if (c2 >= 'Z')
120117468Seric 			{
120217468Seric 				c1++;
120317468Seric 				c2 = 'A' - 1;
120417468Seric 			}
120558716Seric 			qf[3] = c1;
120658716Seric 			qf[4] = ++c2;
120717468Seric 			if (tTd(7, 20))
120840934Srick 				printf("queuename: trying \"%s\"\n", qf);
120917468Seric 
121040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
121151920Seric 			if (i < 0)
121251920Seric 			{
121351920Seric 				if (errno == EEXIST)
121451920Seric 					continue;
121551920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
121651920Seric 					qf, QueueDir);
121751920Seric 				exit(EX_UNAVAILABLE);
121851920Seric 			}
121958689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
122051920Seric 			{
122151920Seric 				e->e_lockfp = fdopen(i, "w");
122240934Srick 				break;
122317468Seric 			}
122451920Seric 
122551920Seric 			/* a reader got the file; abandon it and try again */
122651920Seric 			(void) close(i);
122717468Seric 		}
122817468Seric 		if (c1 >= '~' && c2 >= 'Z')
122917468Seric 		{
123017468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
123117468Seric 				qf, QueueDir);
123217468Seric 			exit(EX_OSERR);
123317468Seric 		}
123417468Seric 		e->e_id = newstr(&qf[2]);
123517468Seric 		define('i', e->e_id, e);
123617468Seric 		if (tTd(7, 1))
123717468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
123817468Seric # ifdef LOG
123958020Seric 		if (LogLevel > 93)
124017468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
124156795Seric # endif /* LOG */
124217468Seric 	}
124317468Seric 
124417468Seric 	if (type == '\0')
124517468Seric 		return (NULL);
124617468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
124717468Seric 	if (tTd(7, 2))
124817468Seric 		printf("queuename: %s\n", buf);
124917468Seric 	return (buf);
125017468Seric }
125117468Seric /*
125217468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
125317468Seric **
125417468Seric **	Parameters:
125517468Seric **		e -- the envelope to unlock.
125617468Seric **
125717468Seric **	Returns:
125817468Seric **		none
125917468Seric **
126017468Seric **	Side Effects:
126117468Seric **		unlocks the queue for `e'.
126217468Seric */
126317468Seric 
126417468Seric unlockqueue(e)
126517468Seric 	ENVELOPE *e;
126617468Seric {
126758680Seric 	if (tTd(51, 4))
126858680Seric 		printf("unlockqueue(%s)\n", e->e_id);
126958680Seric 
127051920Seric 	/* if there is a lock file in the envelope, close it */
127151920Seric 	if (e->e_lockfp != NULL)
127258680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
127351920Seric 	e->e_lockfp = NULL;
127451920Seric 
127558728Seric 	/* don't create a queue id if we don't already have one */
127658728Seric 	if (e->e_id == NULL)
127758728Seric 		return;
127858728Seric 
127917468Seric 	/* remove the transcript */
128017468Seric # ifdef LOG
128158020Seric 	if (LogLevel > 87)
128217468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
128356795Seric # endif /* LOG */
128458680Seric 	if (!tTd(51, 104))
128517468Seric 		xunlink(queuename(e, 'x'));
128617468Seric 
128717468Seric }
128840973Sbostic /*
128954974Seric **  SETCTLUSER -- create a controlling address
129040973Sbostic **
129154974Seric **	Create a fake "address" given only a local login name; this is
129254974Seric **	used as a "controlling user" for future recipient addresses.
129340973Sbostic **
129440973Sbostic **	Parameters:
129554974Seric **		user -- the user name of the controlling user.
129640973Sbostic **
129740973Sbostic **	Returns:
129854974Seric **		An address descriptor for the controlling user.
129940973Sbostic **
130040973Sbostic **	Side Effects:
130140973Sbostic **		none.
130240973Sbostic */
130340973Sbostic 
130454974Seric ADDRESS *
130554974Seric setctluser(user)
130654974Seric 	char *user;
130740973Sbostic {
130854974Seric 	register ADDRESS *a;
130940973Sbostic 	struct passwd *pw;
131040973Sbostic 
131140973Sbostic 	/*
131254974Seric 	**  See if this clears our concept of controlling user.
131340973Sbostic 	*/
131440973Sbostic 
131554974Seric 	if (user == NULL || *user == '\0')
131658704Seric 		user = DefUser;
131740973Sbostic 
131840973Sbostic 	/*
131954974Seric 	**  Set up addr fields for controlling user.
132040973Sbostic 	*/
132140973Sbostic 
132254974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
132354974Seric 	bzero((char *) a, sizeof *a);
132454974Seric 	if ((pw = getpwnam(user)) != NULL)
132540973Sbostic 	{
132640973Sbostic 		a->q_home = newstr(pw->pw_dir);
132740973Sbostic 		a->q_uid = pw->pw_uid;
132840973Sbostic 		a->q_gid = pw->pw_gid;
132957642Seric 		a->q_user = newstr(user);
133040973Sbostic 	}
133140973Sbostic 	else
133240973Sbostic 	{
133340973Sbostic 		a->q_uid = DefUid;
133440973Sbostic 		a->q_gid = DefGid;
133557642Seric 		a->q_user = newstr(DefUser);
133640973Sbostic 	}
133740973Sbostic 
133858294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
133956328Seric 	a->q_mailer = LocalMailer;
134054974Seric 	return a;
134140973Sbostic }
1342