xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 58957)
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*58957Seric static char sccsid[] = "@(#)queue.c	6.46 (Berkeley) 04/04/93 (with queueing)";
1433731Sbostic #else
15*58957Seric static char sccsid[] = "@(#)queue.c	6.46 (Berkeley) 04/04/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;
10658801Seric notemp:
10758690Seric 				syserr("!queueup: cannot create temp file %s", tf);
10841636Srick 			}
10958689Seric 
11058689Seric 			if (lockfile(fd, tf, LOCK_EX|LOCK_NB))
11151920Seric 				break;
11258689Seric 
11351920Seric 			close(fd);
11458801Seric 			sleep(i);
11541636Srick 		}
11658801Seric 		if (fd < 0)
11758801Seric 			goto notemp;
11841636Srick 
11951920Seric 		tfp = fdopen(fd, "w");
12051920Seric 	}
1214632Seric 
1227677Seric 	if (tTd(40, 1))
12317468Seric 		printf("queueing %s\n", e->e_id);
1244632Seric 
1254632Seric 	/*
1266980Seric 	**  If there is no data file yet, create one.
1276980Seric 	*/
1286980Seric 
1296980Seric 	if (e->e_df == NULL)
1306980Seric 	{
1316980Seric 		register FILE *dfp;
1329389Seric 		extern putbody();
1336980Seric 
1347812Seric 		e->e_df = newstr(queuename(e, 'd'));
13540934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
13640934Srick 		if (fd < 0)
13758690Seric 			syserr("!queueup: cannot create %s", e->e_df);
13840934Srick 		dfp = fdopen(fd, "w");
13910173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
14058680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1419389Seric 		e->e_putbody = putbody;
1426980Seric 	}
1436980Seric 
1446980Seric 	/*
1454632Seric 	**  Output future work requests.
14625687Seric 	**	Priority and creation time should be first, since
14725687Seric 	**	they are required by orderq.
1484632Seric 	*/
1494632Seric 
1509377Seric 	/* output message priority */
1519377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1529377Seric 
1539630Seric 	/* output creation time */
1549630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1559630Seric 
1564632Seric 	/* output name of data file */
1577812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1584632Seric 
15910108Seric 	/* message from envelope, if it exists */
16010108Seric 	if (e->e_message != NULL)
16110108Seric 		fprintf(tfp, "M%s\n", e->e_message);
16210108Seric 
16358737Seric 	/* send various flag bits through */
16458737Seric 	p = buf;
16558737Seric 	if (bitset(EF_WARNING, e->e_flags))
16658737Seric 		*p++ = 'w';
16758737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
16858737Seric 		*p++ = 'r';
16958737Seric 	*p++ = '\0';
17058737Seric 	if (buf[0] != '\0')
17158737Seric 		fprintf(tfp, "F%s\n", buf);
17258737Seric 
173*58957Seric 	/* $r and $s and $_ macro values */
17453400Seric 	if ((p = macvalue('r', e)) != NULL)
17553400Seric 		fprintf(tfp, "$r%s\n", p);
17653400Seric 	if ((p = macvalue('s', e)) != NULL)
17753400Seric 		fprintf(tfp, "$s%s\n", p);
178*58957Seric 	if ((p = macvalue('_', e)) != NULL)
179*58957Seric 		fprintf(tfp, "$_%s\n", p);
18053400Seric 
1814632Seric 	/* output name of sender */
1827812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1834632Seric 
18455360Seric 	/* output list of error recipients */
18555360Seric 	lastctladdr = NULL;
18655360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
18755360Seric 	{
18858680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
18955360Seric 		{
19055360Seric 			ADDRESS *ctladdr;
19155360Seric 
19255360Seric 			ctladdr = getctladdr(q);
19355360Seric 			if (ctladdr != lastctladdr)
19455360Seric 			{
19555360Seric 				printctladdr(ctladdr, tfp);
19655360Seric 				lastctladdr = ctladdr;
19755360Seric 			}
19855360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
19955360Seric 		}
20055360Seric 	}
20155360Seric 
2024632Seric 	/* output list of recipient addresses */
2036980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2044632Seric 	{
20558250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
20658680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2078245Seric 		{
20854974Seric 			ADDRESS *ctladdr;
20940973Sbostic 
21054975Seric 			ctladdr = getctladdr(q);
21154975Seric 			if (ctladdr != lastctladdr)
21254974Seric 			{
21354974Seric 				printctladdr(ctladdr, tfp);
21454974Seric 				lastctladdr = ctladdr;
21554974Seric 			}
2167812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2179377Seric 			if (announce)
2189377Seric 			{
2199377Seric 				e->e_to = q->q_paddr;
22058151Seric 				message("queued");
22158020Seric 				if (LogLevel > 8)
22258337Seric 					logdelivery(NULL, NULL, "queued", e);
2239377Seric 				e->e_to = NULL;
2249377Seric 			}
2259387Seric 			if (tTd(40, 1))
2269387Seric 			{
2279387Seric 				printf("queueing ");
2289387Seric 				printaddr(q, FALSE);
2299387Seric 			}
2308245Seric 		}
2314632Seric 	}
2324632Seric 
2339377Seric 	/*
2349377Seric 	**  Output headers for this message.
2359377Seric 	**	Expand macros completely here.  Queue run will deal with
2369377Seric 	**	everything as absolute headers.
2379377Seric 	**		All headers that must be relative to the recipient
2389377Seric 	**		can be cracked later.
23910173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24010173Seric 	**	no effect on the addresses as they are output.
2419377Seric 	*/
2429377Seric 
24310686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24458020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
24558020Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
24610349Seric 	nullmailer.m_eol = "\n";
24710173Seric 
24858050Seric 	define('g', "\201f", e);
2496980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2504632Seric 	{
25110686Seric 		extern bool bitzerop();
25210686Seric 
25312015Seric 		/* don't output null headers */
2544632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2554632Seric 			continue;
25612015Seric 
25712015Seric 		/* don't output resent headers on non-resent messages */
25812015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25912015Seric 			continue;
26012015Seric 
26112015Seric 		/* output this header */
2627812Seric 		fprintf(tfp, "H");
26312015Seric 
26412015Seric 		/* if conditional, output the set of conditions */
26510686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26610686Seric 		{
26710686Seric 			int j;
26810686Seric 
26923098Seric 			(void) putc('?', tfp);
27010686Seric 			for (j = '\0'; j <= '\177'; j++)
27110686Seric 				if (bitnset(j, h->h_mflags))
27223098Seric 					(void) putc(j, tfp);
27323098Seric 			(void) putc('?', tfp);
27410686Seric 		}
27512015Seric 
27612015Seric 		/* output the header: expand macros, convert addresses */
2777763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2787763Seric 		{
2797763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2808236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2817763Seric 		}
2828245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2839348Seric 		{
28458737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
28558737Seric 
28658737Seric 			if (bitset(H_FROM, h->h_flags))
28758737Seric 				oldstyle = FALSE;
28858737Seric 
28958737Seric 			commaize(h, h->h_value, tfp, oldstyle,
29055012Seric 				 &nullmailer, e);
2919348Seric 		}
2927763Seric 		else
2938245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2944632Seric 	}
2954632Seric 
2964632Seric 	/*
2974632Seric 	**  Clean up.
2984632Seric 	*/
2994632Seric 
30058732Seric 	fflush(tfp);
30158732Seric 	if (ferror(tfp))
30258732Seric 	{
30358732Seric 		if (newid)
30458732Seric 			syserr("!552 Error writing control file %s", tf);
30558732Seric 		else
30658732Seric 			syserr("!452 Error writing control file %s", tf);
30758732Seric 	}
30858732Seric 
30951920Seric 	if (!newid)
31051920Seric 	{
31151920Seric 		qf = queuename(e, 'q');
31251920Seric 		if (rename(tf, qf) < 0)
31351920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31451920Seric 		if (e->e_lockfp != NULL)
31558680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
31651920Seric 		e->e_lockfp = tfp;
31751920Seric 	}
31851920Seric 	else
31951920Seric 		qf = tf;
32041636Srick 	errno = 0;
3217391Seric 
3227677Seric # ifdef LOG
3237677Seric 	/* save log info */
32458020Seric 	if (LogLevel > 79)
3257878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
32656795Seric # endif /* LOG */
32740934Srick 	fflush(tfp);
32851920Seric 	return;
3294632Seric }
33054974Seric 
33154974Seric printctladdr(a, tfp)
33254974Seric 	ADDRESS *a;
33354974Seric 	FILE *tfp;
33454974Seric {
33554974Seric 	char *u;
33654974Seric 	struct passwd *pw;
33754974Seric 	extern struct passwd *getpwuid();
33854974Seric 
33954974Seric 	if (a == NULL)
34054974Seric 	{
34154974Seric 		fprintf(tfp, "C\n");
34254974Seric 		return;
34354974Seric 	}
34454974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
34554974Seric 		u = DefUser;
34654974Seric 	else
34754974Seric 		u = pw->pw_name;
34854974Seric 	fprintf(tfp, "C%s\n", u);
34954974Seric }
35054974Seric 
3514632Seric /*
3524632Seric **  RUNQUEUE -- run the jobs in the queue.
3534632Seric **
3544632Seric **	Gets the stuff out of the queue in some presumably logical
3554632Seric **	order and processes them.
3564632Seric **
3574632Seric **	Parameters:
35824941Seric **		forkflag -- TRUE if the queue scanning should be done in
35924941Seric **			a child process.  We double-fork so it is not our
36024941Seric **			child and we don't have to clean up after it.
3614632Seric **
3624632Seric **	Returns:
3634632Seric **		none.
3644632Seric **
3654632Seric **	Side Effects:
3664632Seric **		runs things in the mail queue.
3674632Seric */
3684632Seric 
36955360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
37055360Seric 
37155360Seric runqueue(forkflag)
3724639Seric 	bool forkflag;
3734632Seric {
37424953Seric 	extern bool shouldqueue();
37555360Seric 	register ENVELOPE *e;
37655360Seric 	extern ENVELOPE BlankEnvelope;
37755360Seric 	extern ENVELOPE *newenvelope();
37824953Seric 
3797466Seric 	/*
38024953Seric 	**  If no work will ever be selected, don't even bother reading
38124953Seric 	**  the queue.
38224953Seric 	*/
38324953Seric 
38451920Seric 	CurrentLA = getla();	/* get load average */
38540934Srick 
38658132Seric 	if (shouldqueue(0L, curtime()))
38724953Seric 	{
38824953Seric 		if (Verbose)
38924953Seric 			printf("Skipping queue run -- load average too high\n");
39058107Seric 		if (forkflag && QueueIntvl != 0)
39158107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
39255360Seric 		return;
39324953Seric 	}
39424953Seric 
39524953Seric 	/*
3967466Seric 	**  See if we want to go off and do other useful work.
3977466Seric 	*/
3984639Seric 
3994639Seric 	if (forkflag)
4004639Seric 	{
4017943Seric 		int pid;
4027943Seric 
4037943Seric 		pid = dofork();
4047943Seric 		if (pid != 0)
4054639Seric 		{
40646928Sbostic 			extern void reapchild();
40725184Seric 
4087943Seric 			/* parent -- pick up intermediate zombie */
40925184Seric #ifndef SIGCHLD
4109377Seric 			(void) waitfor(pid);
41156795Seric #else /* SIGCHLD */
41225184Seric 			(void) signal(SIGCHLD, reapchild);
41356795Seric #endif /* SIGCHLD */
4147690Seric 			if (QueueIntvl != 0)
4159348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4164639Seric 			return;
4174639Seric 		}
4187943Seric 		/* child -- double fork */
41925184Seric #ifndef SIGCHLD
4207943Seric 		if (fork() != 0)
4217943Seric 			exit(EX_OK);
42256795Seric #else /* SIGCHLD */
42325184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42456795Seric #endif /* SIGCHLD */
4254639Seric 	}
42624941Seric 
42740934Srick 	setproctitle("running queue: %s", QueueDir);
42824941Seric 
4297876Seric # ifdef LOG
43058020Seric 	if (LogLevel > 69)
43155360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
43255360Seric 			QueueDir, getpid(), forkflag);
43356795Seric # endif /* LOG */
4344639Seric 
4357466Seric 	/*
43610205Seric 	**  Release any resources used by the daemon code.
43710205Seric 	*/
43810205Seric 
43910205Seric # ifdef DAEMON
44010205Seric 	clrdaemon();
44156795Seric # endif /* DAEMON */
44210205Seric 
44310205Seric 	/*
44455360Seric 	**  Create ourselves an envelope
44555360Seric 	*/
44655360Seric 
44755360Seric 	CurEnv = &QueueEnvelope;
44858179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
44955360Seric 	e->e_flags = BlankEnvelope.e_flags;
45055360Seric 
45155360Seric 	/*
45227175Seric 	**  Make sure the alias database is open.
45327175Seric 	*/
45427175Seric 
45555012Seric 	initaliases(AliasFile, FALSE, e);
45627175Seric 
45727175Seric 	/*
4587466Seric 	**  Start making passes through the queue.
4597466Seric 	**	First, read and sort the entire queue.
4607466Seric 	**	Then, process the work in that order.
4617466Seric 	**		But if you take too long, start over.
4627466Seric 	*/
4637466Seric 
4647943Seric 	/* order the existing work requests */
46524954Seric 	(void) orderq(FALSE);
4667690Seric 
4677943Seric 	/* process them once at a time */
4687943Seric 	while (WorkQ != NULL)
4694639Seric 	{
4707943Seric 		WORK *w = WorkQ;
47158884Seric 		extern bool shouldqueue();
4727881Seric 
4737943Seric 		WorkQ = WorkQ->w_next;
47458884Seric 
47558884Seric 		/*
47658884Seric 		**  Ignore jobs that are too expensive for the moment.
47758884Seric 		*/
47858884Seric 
47958884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
48058884Seric 		{
48158884Seric 			if (Verbose)
48258884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
48358884Seric 		}
48458930Seric 		else
48558930Seric 		{
48658930Seric 			dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
48758930Seric 		}
4887943Seric 		free(w->w_name);
4897943Seric 		free((char *) w);
4904639Seric 	}
49129866Seric 
49229866Seric 	/* exit without the usual cleanup */
49355467Seric 	e->e_id = NULL;
49455467Seric 	finis();
4954634Seric }
4964634Seric /*
4974632Seric **  ORDERQ -- order the work queue.
4984632Seric **
4994632Seric **	Parameters:
50024941Seric **		doall -- if set, include everything in the queue (even
50124941Seric **			the jobs that cannot be run because the load
50224941Seric **			average is too high).  Otherwise, exclude those
50324941Seric **			jobs.
5044632Seric **
5054632Seric **	Returns:
50610121Seric **		The number of request in the queue (not necessarily
50710121Seric **		the number of requests in WorkQ however).
5084632Seric **
5094632Seric **	Side Effects:
5104632Seric **		Sets WorkQ to the queue of available work, in order.
5114632Seric */
5124632Seric 
51325687Seric # define NEED_P		001
51425687Seric # define NEED_T		002
51558318Seric # define NEED_R		004
51658318Seric # define NEED_S		010
5174632Seric 
51824941Seric orderq(doall)
51924941Seric 	bool doall;
5204632Seric {
5216625Sglickman 	register struct direct *d;
5224632Seric 	register WORK *w;
5236625Sglickman 	DIR *f;
5244632Seric 	register int i;
52525687Seric 	WORK wlist[QUEUESIZE+1];
52610070Seric 	int wn = -1;
5274632Seric 	extern workcmpf();
5284632Seric 
52958318Seric 	if (tTd(41, 1))
53058318Seric 	{
53158318Seric 		printf("orderq:\n");
53258318Seric 		if (QueueLimitId != NULL)
53358318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
53458318Seric 		if (QueueLimitSender != NULL)
53558318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
53658318Seric 		if (QueueLimitRecipient != NULL)
53758318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
53858318Seric 	}
53958318Seric 
5404632Seric 	/* clear out old WorkQ */
5414632Seric 	for (w = WorkQ; w != NULL; )
5424632Seric 	{
5434632Seric 		register WORK *nw = w->w_next;
5444632Seric 
5454632Seric 		WorkQ = nw;
5464632Seric 		free(w->w_name);
5474632Seric 		free((char *) w);
5484632Seric 		w = nw;
5494632Seric 	}
5504632Seric 
5514632Seric 	/* open the queue directory */
5528148Seric 	f = opendir(".");
5534632Seric 	if (f == NULL)
5544632Seric 	{
5558148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
55610070Seric 		return (0);
5574632Seric 	}
5584632Seric 
5594632Seric 	/*
5604632Seric 	**  Read the work directory.
5614632Seric 	*/
5624632Seric 
56310070Seric 	while ((d = readdir(f)) != NULL)
5644632Seric 	{
5659377Seric 		FILE *cf;
5664632Seric 		char lbuf[MAXNAME];
56757642Seric 		extern bool shouldqueue();
56858318Seric 		extern bool strcontainedin();
5694632Seric 
5704632Seric 		/* is this an interesting entry? */
5717812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5724632Seric 			continue;
5734632Seric 
57458318Seric 		if (QueueLimitId != NULL &&
57558318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
57658318Seric 			continue;
57758318Seric 
57858722Seric 		/*
57958722Seric 		**  Check queue name for plausibility.  This handles
58058722Seric 		**  both old and new type ids.
58158722Seric 		*/
58258722Seric 
58358722Seric 		i = strlen(d->d_name);
58458722Seric 		if (i != 9 && i != 10)
58558020Seric 		{
58658020Seric 			if (Verbose)
58758020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
58858020Seric #ifdef LOG
58958020Seric 			if (LogLevel > 3)
59058020Seric 				syslog(LOG_NOTICE, "orderq: bogus qf name %s",
59158020Seric 					d->d_name);
59258020Seric #endif
59358020Seric 			if (strlen(d->d_name) >= MAXNAME)
59458020Seric 				d->d_name[MAXNAME - 1] = '\0';
59558020Seric 			strcpy(lbuf, d->d_name);
59658020Seric 			lbuf[0] = 'Q';
59758020Seric 			(void) rename(d->d_name, lbuf);
59858020Seric 			continue;
59958020Seric 		}
60058020Seric 
60110070Seric 		/* yes -- open control file (if not too many files) */
60225687Seric 		if (++wn >= QUEUESIZE)
60310070Seric 			continue;
60458318Seric 
6058148Seric 		cf = fopen(d->d_name, "r");
6064632Seric 		if (cf == NULL)
6074632Seric 		{
6087055Seric 			/* this may be some random person sending hir msgs */
6097055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
61010090Seric 			if (tTd(41, 2))
61110090Seric 				printf("orderq: cannot open %s (%d)\n",
61210090Seric 					d->d_name, errno);
6137055Seric 			errno = 0;
61410090Seric 			wn--;
6154632Seric 			continue;
6164632Seric 		}
61725687Seric 		w = &wlist[wn];
61825687Seric 		w->w_name = newstr(d->d_name);
6194632Seric 
62025027Seric 		/* make sure jobs in creation don't clog queue */
62125687Seric 		w->w_pri = 0x7fffffff;
62225687Seric 		w->w_ctime = 0;
62325027Seric 
6244632Seric 		/* extract useful information */
62525687Seric 		i = NEED_P | NEED_T;
62658318Seric 		if (QueueLimitSender != NULL)
62758318Seric 			i |= NEED_S;
62858318Seric 		if (QueueLimitRecipient != NULL)
62958318Seric 			i |= NEED_R;
63025687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
6314632Seric 		{
63224954Seric 			extern long atol();
63358318Seric 			extern bool strcontainedin();
63424954Seric 
63524941Seric 			switch (lbuf[0])
6364632Seric 			{
63724941Seric 			  case 'P':
63825687Seric 				w->w_pri = atol(&lbuf[1]);
63925687Seric 				i &= ~NEED_P;
6404632Seric 				break;
64125013Seric 
64225013Seric 			  case 'T':
64325687Seric 				w->w_ctime = atol(&lbuf[1]);
64425687Seric 				i &= ~NEED_T;
64525013Seric 				break;
64658318Seric 
64758318Seric 			  case 'R':
64858318Seric 				if (QueueLimitRecipient != NULL &&
64958318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
65058318Seric 					i &= ~NEED_R;
65158318Seric 				break;
65258318Seric 
65358318Seric 			  case 'S':
65458318Seric 				if (QueueLimitSender != NULL &&
65558318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
65658318Seric 					i &= ~NEED_S;
65758318Seric 				break;
6584632Seric 			}
6594632Seric 		}
6604632Seric 		(void) fclose(cf);
66124953Seric 
66258318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
66358318Seric 		    bitset(NEED_R|NEED_S, i))
66424953Seric 		{
66524953Seric 			/* don't even bother sorting this job in */
66624953Seric 			wn--;
66724953Seric 		}
6684632Seric 	}
6696625Sglickman 	(void) closedir(f);
67010090Seric 	wn++;
6714632Seric 
6724632Seric 	/*
6734632Seric 	**  Sort the work directory.
6744632Seric 	*/
6754632Seric 
67625687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
6774632Seric 
6784632Seric 	/*
6794632Seric 	**  Convert the work list into canonical form.
6809377Seric 	**	Should be turning it into a list of envelopes here perhaps.
6814632Seric 	*/
6824632Seric 
68324981Seric 	WorkQ = NULL;
68425687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6854632Seric 	{
6864632Seric 		w = (WORK *) xalloc(sizeof *w);
6874632Seric 		w->w_name = wlist[i].w_name;
6884632Seric 		w->w_pri = wlist[i].w_pri;
68925013Seric 		w->w_ctime = wlist[i].w_ctime;
69024981Seric 		w->w_next = WorkQ;
69124981Seric 		WorkQ = w;
6924632Seric 	}
6934632Seric 
6947677Seric 	if (tTd(40, 1))
6954632Seric 	{
6964632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6975037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6984632Seric 	}
69910070Seric 
70010090Seric 	return (wn);
7014632Seric }
7024632Seric /*
7037677Seric **  WORKCMPF -- compare function for ordering work.
7044632Seric **
7054632Seric **	Parameters:
7064632Seric **		a -- the first argument.
7074632Seric **		b -- the second argument.
7084632Seric **
7094632Seric **	Returns:
71024981Seric **		-1 if a < b
71124981Seric **		 0 if a == b
71224981Seric **		+1 if a > b
7134632Seric **
7144632Seric **	Side Effects:
7154632Seric **		none.
7164632Seric */
7174632Seric 
7184632Seric workcmpf(a, b)
7195037Seric 	register WORK *a;
7205037Seric 	register WORK *b;
7214632Seric {
72257438Seric 	long pa = a->w_pri;
72357438Seric 	long pb = b->w_pri;
72424941Seric 
72524941Seric 	if (pa == pb)
7264632Seric 		return (0);
72724941Seric 	else if (pa > pb)
72824981Seric 		return (1);
72924981Seric 	else
73010121Seric 		return (-1);
7314632Seric }
7324632Seric /*
7334632Seric **  DOWORK -- do a work request.
7344632Seric **
7354632Seric **	Parameters:
73658884Seric **		id -- the ID of the job to run.
73758884Seric **		forkflag -- if set, run this in background.
73858924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
73958924Seric **			This is used when expanding aliases in the queue.
74058884Seric **		e - the envelope in which to run it.
7414632Seric **
7424632Seric **	Returns:
7434632Seric **		none.
7444632Seric **
7454632Seric **	Side Effects:
7464632Seric **		The work request is satisfied if possible.
7474632Seric */
7484632Seric 
74958924Seric dowork(id, forkflag, requeueflag, e)
75058884Seric 	char *id;
75158884Seric 	bool forkflag;
75258924Seric 	bool requeueflag;
75355012Seric 	register ENVELOPE *e;
7544632Seric {
7554632Seric 	register int i;
75651920Seric 	extern bool readqf();
7574632Seric 
7587677Seric 	if (tTd(40, 1))
75958884Seric 		printf("dowork(%s)\n", id);
7604632Seric 
7614632Seric 	/*
76224941Seric 	**  Fork for work.
76324941Seric 	*/
76424941Seric 
76558884Seric 	if (forkflag)
76624941Seric 	{
76724941Seric 		i = fork();
76824941Seric 		if (i < 0)
76924941Seric 		{
77024941Seric 			syserr("dowork: cannot fork");
77124941Seric 			return;
77224941Seric 		}
77324941Seric 	}
77424941Seric 	else
77524941Seric 	{
77624941Seric 		i = 0;
77724941Seric 	}
77824941Seric 
7794632Seric 	if (i == 0)
7804632Seric 	{
7814632Seric 		/*
7824632Seric 		**  CHILD
7838148Seric 		**	Lock the control file to avoid duplicate deliveries.
7848148Seric 		**		Then run the file as though we had just read it.
7857350Seric 		**	We save an idea of the temporary name so we
7867350Seric 		**		can recover on interrupt.
7874632Seric 		*/
7884632Seric 
7897763Seric 		/* set basic modes, etc. */
7907356Seric 		(void) alarm(0);
79155012Seric 		clearenvelope(e, FALSE);
79258737Seric 		e->e_flags |= EF_QUEUERUN;
79358734Seric 		e->e_errormode = EM_MAIL;
79458884Seric 		e->e_id = id;
7957876Seric # ifdef LOG
79658020Seric 		if (LogLevel > 76)
79755012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7987881Seric 			       getpid());
79956795Seric # endif /* LOG */
8007763Seric 
8017763Seric 		/* don't use the headers from sendmail.cf... */
80255012Seric 		e->e_header = NULL;
8037763Seric 
80451920Seric 		/* read the queue control file -- return if locked */
80555012Seric 		if (!readqf(e))
8066980Seric 		{
80758884Seric 			if (tTd(40, 4))
80858884Seric 				printf("readqf(%s) failed\n", e->e_id);
80958884Seric 			if (forkflag)
81024941Seric 				exit(EX_OK);
81124941Seric 			else
81224941Seric 				return;
8136980Seric 		}
8146980Seric 
81555012Seric 		e->e_flags |= EF_INQUEUE;
81658929Seric 		eatheader(e, requeueflag);
8176980Seric 
81858924Seric 		if (requeueflag)
81958924Seric 			queueup(e, TRUE, FALSE);
82058924Seric 
8216980Seric 		/* do the delivery */
82258915Seric 		sendall(e, SM_DELIVER);
8236980Seric 
8246980Seric 		/* finish up and exit */
82558884Seric 		if (forkflag)
82624941Seric 			finis();
82724941Seric 		else
82855012Seric 			dropenvelope(e);
8294632Seric 	}
83024941Seric 	else
83124941Seric 	{
83224941Seric 		/*
83324941Seric 		**  Parent -- pick up results.
83424941Seric 		*/
8354632Seric 
83624941Seric 		errno = 0;
83724941Seric 		(void) waitfor(i);
83824941Seric 	}
8394632Seric }
8404632Seric /*
8414632Seric **  READQF -- read queue file and set up environment.
8424632Seric **
8434632Seric **	Parameters:
8449377Seric **		e -- the envelope of the job to run.
8454632Seric **
8464632Seric **	Returns:
84751920Seric **		TRUE if it successfully read the queue file.
84851920Seric **		FALSE otherwise.
8494632Seric **
8504632Seric **	Side Effects:
85151920Seric **		The queue file is returned locked.
8524632Seric */
8534632Seric 
85451920Seric bool
85551920Seric readqf(e)
8569377Seric 	register ENVELOPE *e;
8574632Seric {
85817477Seric 	register FILE *qfp;
85954974Seric 	ADDRESS *ctladdr;
86056400Seric 	struct stat st;
86157135Seric 	char *bp;
86258915Seric 	char qf[20];
86357135Seric 	char buf[MAXLINE];
8649348Seric 	extern char *fgetfolded();
86524954Seric 	extern long atol();
86654974Seric 	extern ADDRESS *setctluser();
86758689Seric 	extern bool lockfile();
8684632Seric 
8694632Seric 	/*
87017468Seric 	**  Read and process the file.
8714632Seric 	*/
8724632Seric 
87358915Seric 	strcpy(qf, queuename(e, 'q'));
87451937Seric 	qfp = fopen(qf, "r+");
87517477Seric 	if (qfp == NULL)
87617477Seric 	{
87758884Seric 		if (tTd(40, 8))
87858884Seric 			printf("readqf(%s): fopen failure (%s)\n",
87958884Seric 				qf, errstring(errno));
88040934Srick 		if (errno != ENOENT)
88140934Srick 			syserr("readqf: no control file %s", qf);
88251920Seric 		return FALSE;
88317477Seric 	}
88440934Srick 
88556400Seric 	/*
88656400Seric 	**  Check the queue file for plausibility to avoid attacks.
88756400Seric 	*/
88856400Seric 
88956400Seric 	if (fstat(fileno(qfp), &st) < 0)
89056400Seric 	{
89156400Seric 		/* must have been being processed by someone else */
89258884Seric 		if (tTd(40, 8))
89358884Seric 			printf("readqf(%s): fstat failure (%s)\n",
89458884Seric 				qf, errstring(errno));
89556400Seric 		fclose(qfp);
89656400Seric 		return FALSE;
89756400Seric 	}
89856400Seric 
89957135Seric 	if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode)
90056400Seric 	{
90156400Seric # ifdef LOG
90256400Seric 		if (LogLevel > 0)
90356400Seric 		{
90456400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
90556400Seric 				e->e_id, st.st_uid, st.st_mode);
90656400Seric 		}
90756795Seric # endif /* LOG */
90858884Seric 		if (tTd(40, 8))
90958884Seric 			printf("readqf(%s): bogus file\n", qf);
91056400Seric 		fclose(qfp);
91156400Seric 		return FALSE;
91256400Seric 	}
91356400Seric 
91458689Seric 	if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB))
91540934Srick 	{
91658689Seric 		/* being processed by another queuer */
91758884Seric 		if (tTd(40, 8))
91858884Seric 			printf("readqf(%s): locked\n", qf);
91958689Seric 		if (Verbose)
92058689Seric 			printf("%s: locked\n", e->e_id);
92151920Seric # ifdef LOG
92258689Seric 		if (LogLevel > 19)
92358689Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
92456795Seric # endif /* LOG */
92540934Srick 		(void) fclose(qfp);
92651920Seric 		return FALSE;
92740934Srick 	}
92840934Srick 
92951920Seric 	/* save this lock */
93051920Seric 	e->e_lockfp = qfp;
93151920Seric 
93240934Srick 	/* do basic system initialization */
93355012Seric 	initsys(e);
93440934Srick 
93517477Seric 	FileName = qf;
9369377Seric 	LineNumber = 0;
93751920Seric 	if (Verbose)
9389377Seric 		printf("\nRunning %s\n", e->e_id);
93954974Seric 	ctladdr = NULL;
94057135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
9414632Seric 	{
94258737Seric 		register char *p;
94357529Seric 		struct stat st;
94457529Seric 
94526504Seric 		if (tTd(40, 4))
94657135Seric 			printf("+++++ %s\n", bp);
94757135Seric 		switch (bp[0])
9484632Seric 		{
94940973Sbostic 		  case 'C':		/* specify controlling user */
95057135Seric 			ctladdr = setctluser(&bp[1]);
95140973Sbostic 			break;
95240973Sbostic 
9534632Seric 		  case 'R':		/* specify recipient */
95458082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
9554632Seric 			break;
9564632Seric 
95725687Seric 		  case 'E':		/* specify error recipient */
95858082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
95925687Seric 			break;
96025687Seric 
9614632Seric 		  case 'H':		/* header */
96257135Seric 			(void) chompheader(&bp[1], FALSE, e);
9634632Seric 			break;
9644632Seric 
96510108Seric 		  case 'M':		/* message */
96657135Seric 			e->e_message = newstr(&bp[1]);
96710108Seric 			break;
96810108Seric 
9694632Seric 		  case 'S':		/* sender */
97058704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
9714632Seric 			break;
9724632Seric 
9734632Seric 		  case 'D':		/* data file name */
97457135Seric 			e->e_df = newstr(&bp[1]);
9759544Seric 			e->e_dfp = fopen(e->e_df, "r");
9769544Seric 			if (e->e_dfp == NULL)
97758020Seric 			{
9789377Seric 				syserr("readqf: cannot open %s", e->e_df);
97958020Seric 				e->e_msgsize = -1;
98058020Seric 			}
98158020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
98257529Seric 				e->e_msgsize = st.st_size;
9834632Seric 			break;
9844632Seric 
9857860Seric 		  case 'T':		/* init time */
98657135Seric 			e->e_ctime = atol(&bp[1]);
9874632Seric 			break;
9884632Seric 
9894634Seric 		  case 'P':		/* message priority */
99057135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
9914634Seric 			break;
9924634Seric 
99358737Seric 		  case 'F':		/* flag bits */
99458737Seric 			for (p = &bp[1]; *p != '\0'; p++)
99558737Seric 			{
99658737Seric 				switch (*p)
99758737Seric 				{
99858737Seric 				  case 'w':	/* warning sent */
99958737Seric 					e->e_flags |= EF_WARNING;
100058737Seric 					break;
100158737Seric 
100258737Seric 				  case 'r':	/* response */
100358737Seric 					e->e_flags |= EF_RESPONSE;
100458737Seric 					break;
100558737Seric 				}
100658737Seric 			}
100758737Seric 			break;
100858737Seric 
100953400Seric 		  case '$':		/* define macro */
101057135Seric 			define(bp[1], newstr(&bp[2]), e);
101153400Seric 			break;
101253400Seric 
101324941Seric 		  case '\0':		/* blank line; ignore */
101424941Seric 			break;
101524941Seric 
10164632Seric 		  default:
101724941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
101857135Seric 				LineNumber, bp);
10194632Seric 			break;
10204632Seric 		}
102157135Seric 
102257135Seric 		if (bp != buf)
102357135Seric 			free(bp);
10244632Seric 	}
10259377Seric 
10269377Seric 	FileName = NULL;
102724941Seric 
102824941Seric 	/*
102924941Seric 	**  If we haven't read any lines, this queue file is empty.
103024941Seric 	**  Arrange to remove it without referencing any null pointers.
103124941Seric 	*/
103224941Seric 
103324941Seric 	if (LineNumber == 0)
103424941Seric 	{
103524941Seric 		errno = 0;
103624941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
103724941Seric 	}
103851920Seric 	return TRUE;
10394632Seric }
10404632Seric /*
10419630Seric **  PRINTQUEUE -- print out a representation of the mail queue
10429630Seric **
10439630Seric **	Parameters:
10449630Seric **		none.
10459630Seric **
10469630Seric **	Returns:
10479630Seric **		none.
10489630Seric **
10499630Seric **	Side Effects:
10509630Seric **		Prints a listing of the mail queue on the standard output.
10519630Seric */
10525182Seric 
10539630Seric printqueue()
10549630Seric {
10559630Seric 	register WORK *w;
10569630Seric 	FILE *f;
105710070Seric 	int nrequests;
10589630Seric 	char buf[MAXLINE];
10599630Seric 
10609630Seric 	/*
106158250Seric 	**  Check for permission to print the queue
106258250Seric 	*/
106358250Seric 
106458523Seric 	if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0)
106558250Seric 	{
106658250Seric 		struct stat st;
106758523Seric # ifdef NGROUPS
106858523Seric 		int n;
106958523Seric 		int gidset[NGROUPS];
107058523Seric # endif
107158250Seric 
107258517Seric 		if (stat(QueueDir, &st) < 0)
107358250Seric 		{
107458250Seric 			syserr("Cannot stat %s", QueueDir);
107558250Seric 			return;
107658250Seric 		}
107758523Seric # ifdef NGROUPS
107858523Seric 		n = getgroups(NGROUPS, gidset);
107958523Seric 		while (--n >= 0)
108058523Seric 		{
108158523Seric 			if (gidset[n] == st.st_gid)
108258523Seric 				break;
108358523Seric 		}
108458523Seric 		if (n < 0)
108558523Seric # else
108658250Seric 		if (getgid() != st.st_gid)
108758523Seric # endif
108858250Seric 		{
108958250Seric 			usrerr("510 You are not permitted to see the queue");
109058250Seric 			setstat(EX_NOPERM);
109158250Seric 			return;
109258250Seric 		}
109358250Seric 	}
109458250Seric 
109558250Seric 	/*
10969630Seric 	**  Read and order the queue.
10979630Seric 	*/
10989630Seric 
109924941Seric 	nrequests = orderq(TRUE);
11009630Seric 
11019630Seric 	/*
11029630Seric 	**  Print the work list that we have read.
11039630Seric 	*/
11049630Seric 
11059630Seric 	/* first see if there is anything */
110610070Seric 	if (nrequests <= 0)
11079630Seric 	{
110810070Seric 		printf("Mail queue is empty\n");
11099630Seric 		return;
11109630Seric 	}
11119630Seric 
111251920Seric 	CurrentLA = getla();	/* get load average */
111340934Srick 
111410096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
111525687Seric 	if (nrequests > QUEUESIZE)
111625687Seric 		printf(", only %d printed", QUEUESIZE);
111724979Seric 	if (Verbose)
111858716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
111924979Seric 	else
112058716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
11219630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
11229630Seric 	{
11239630Seric 		struct stat st;
112410070Seric 		auto time_t submittime = 0;
112510070Seric 		long dfsize = -1;
112658737Seric 		int flags = 0;
112710108Seric 		char message[MAXLINE];
112824941Seric 		extern bool shouldqueue();
112958689Seric 		extern bool lockfile();
11309630Seric 
113117468Seric 		f = fopen(w->w_name, "r");
113217468Seric 		if (f == NULL)
113317468Seric 		{
113417468Seric 			errno = 0;
113517468Seric 			continue;
113617468Seric 		}
113758724Seric 		printf("%8s", w->w_name + 2);
113858689Seric 		if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB))
113910070Seric 			printf("*");
114057438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
114124941Seric 			printf("X");
114210070Seric 		else
114310070Seric 			printf(" ");
114410070Seric 		errno = 0;
114517468Seric 
114610108Seric 		message[0] = '\0';
11479630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
11489630Seric 		{
114953400Seric 			register int i;
115058737Seric 			register char *p;
115153400Seric 
11529630Seric 			fixcrlf(buf, TRUE);
11539630Seric 			switch (buf[0])
11549630Seric 			{
115510108Seric 			  case 'M':	/* error message */
115653400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
115758737Seric 					i = sizeof message - 1;
115853400Seric 				bcopy(&buf[1], message, i);
115953400Seric 				message[i] = '\0';
116010108Seric 				break;
116110108Seric 
11629630Seric 			  case 'S':	/* sender name */
116324979Seric 				if (Verbose)
116458737Seric 					printf("%8ld %10ld%c%.12s %.38s",
116558737Seric 					    dfsize,
116658737Seric 					    w->w_pri,
116758737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
116858737Seric 					    ctime(&submittime) + 4,
116924979Seric 					    &buf[1]);
117024979Seric 				else
117124979Seric 					printf("%8ld %.16s %.45s", dfsize,
117224979Seric 					    ctime(&submittime), &buf[1]);
117310108Seric 				if (message[0] != '\0')
117425027Seric 					printf("\n\t\t (%.60s)", message);
11759630Seric 				break;
117651920Seric 
117740973Sbostic 			  case 'C':	/* controlling user */
117854974Seric 				if (Verbose)
117958716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
118058716Seric 						&buf[1]);
118140973Sbostic 				break;
11829630Seric 
11839630Seric 			  case 'R':	/* recipient name */
118424979Seric 				if (Verbose)
118558716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
118624979Seric 				else
118758716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
11889630Seric 				break;
11899630Seric 
11909630Seric 			  case 'T':	/* creation time */
119124941Seric 				submittime = atol(&buf[1]);
11929630Seric 				break;
119310070Seric 
119410070Seric 			  case 'D':	/* data file name */
119510070Seric 				if (stat(&buf[1], &st) >= 0)
119610070Seric 					dfsize = st.st_size;
119710070Seric 				break;
119858737Seric 
119958737Seric 			  case 'F':	/* flag bits */
120058737Seric 				for (p = &buf[1]; *p != '\0'; p++)
120158737Seric 				{
120258737Seric 					switch (*p)
120358737Seric 					{
120458737Seric 					  case 'w':
120558737Seric 						flags |= EF_WARNING;
120658737Seric 						break;
120758737Seric 					}
120858737Seric 				}
12099630Seric 			}
12109630Seric 		}
121110070Seric 		if (submittime == (time_t) 0)
121210070Seric 			printf(" (no control file)");
12139630Seric 		printf("\n");
121423098Seric 		(void) fclose(f);
12159630Seric 	}
12169630Seric }
12179630Seric 
121856795Seric # endif /* QUEUE */
121917468Seric /*
122017468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
122117468Seric **
122217468Seric **	Assigns an id code if one does not already exist.
122317468Seric **	This code is very careful to avoid trashing existing files
122417468Seric **	under any circumstances.
122517468Seric **
122617468Seric **	Parameters:
122717468Seric **		e -- envelope to build it in/from.
122817468Seric **		type -- the file type, used as the first character
122917468Seric **			of the file name.
123017468Seric **
123117468Seric **	Returns:
123217468Seric **		a pointer to the new file name (in a static buffer).
123317468Seric **
123417468Seric **	Side Effects:
123551920Seric **		If no id code is already assigned, queuename will
123651920Seric **		assign an id code, create a qf file, and leave a
123751920Seric **		locked, open-for-write file pointer in the envelope.
123817468Seric */
123917468Seric 
124017468Seric char *
124117468Seric queuename(e, type)
124217468Seric 	register ENVELOPE *e;
124317468Seric 	char type;
124417468Seric {
124517468Seric 	static int pid = -1;
124658741Seric 	static char c0;
124758741Seric 	static char c1;
124858741Seric 	static char c2;
124958716Seric 	time_t now;
125058716Seric 	struct tm *tm;
125158689Seric 	static char buf[MAXNAME];
125258689Seric 	extern bool lockfile();
125317468Seric 
125417468Seric 	if (e->e_id == NULL)
125517468Seric 	{
125617468Seric 		char qf[20];
125717468Seric 
125817468Seric 		/* find a unique id */
125917468Seric 		if (pid != getpid())
126017468Seric 		{
126117468Seric 			/* new process -- start back at "AA" */
126217468Seric 			pid = getpid();
126358716Seric 			now = curtime();
126458716Seric 			tm = localtime(&now);
126558716Seric 			c0 = 'A' + tm->tm_hour;
126617468Seric 			c1 = 'A';
126717468Seric 			c2 = 'A' - 1;
126817468Seric 		}
126958716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
127017468Seric 
127117468Seric 		while (c1 < '~' || c2 < 'Z')
127217468Seric 		{
127317468Seric 			int i;
127417468Seric 
127517468Seric 			if (c2 >= 'Z')
127617468Seric 			{
127717468Seric 				c1++;
127817468Seric 				c2 = 'A' - 1;
127917468Seric 			}
128058716Seric 			qf[3] = c1;
128158716Seric 			qf[4] = ++c2;
128217468Seric 			if (tTd(7, 20))
128340934Srick 				printf("queuename: trying \"%s\"\n", qf);
128417468Seric 
128540934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
128651920Seric 			if (i < 0)
128751920Seric 			{
128851920Seric 				if (errno == EEXIST)
128951920Seric 					continue;
129051920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
129151920Seric 					qf, QueueDir);
129251920Seric 				exit(EX_UNAVAILABLE);
129351920Seric 			}
129458689Seric 			if (lockfile(i, qf, LOCK_EX|LOCK_NB))
129551920Seric 			{
129651920Seric 				e->e_lockfp = fdopen(i, "w");
129740934Srick 				break;
129817468Seric 			}
129951920Seric 
130051920Seric 			/* a reader got the file; abandon it and try again */
130151920Seric 			(void) close(i);
130217468Seric 		}
130317468Seric 		if (c1 >= '~' && c2 >= 'Z')
130417468Seric 		{
130517468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
130617468Seric 				qf, QueueDir);
130717468Seric 			exit(EX_OSERR);
130817468Seric 		}
130917468Seric 		e->e_id = newstr(&qf[2]);
131017468Seric 		define('i', e->e_id, e);
131117468Seric 		if (tTd(7, 1))
131217468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
131317468Seric # ifdef LOG
131458020Seric 		if (LogLevel > 93)
131517468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
131656795Seric # endif /* LOG */
131717468Seric 	}
131817468Seric 
131917468Seric 	if (type == '\0')
132017468Seric 		return (NULL);
132117468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
132217468Seric 	if (tTd(7, 2))
132317468Seric 		printf("queuename: %s\n", buf);
132417468Seric 	return (buf);
132517468Seric }
132617468Seric /*
132717468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
132817468Seric **
132917468Seric **	Parameters:
133017468Seric **		e -- the envelope to unlock.
133117468Seric **
133217468Seric **	Returns:
133317468Seric **		none
133417468Seric **
133517468Seric **	Side Effects:
133617468Seric **		unlocks the queue for `e'.
133717468Seric */
133817468Seric 
133917468Seric unlockqueue(e)
134017468Seric 	ENVELOPE *e;
134117468Seric {
134258680Seric 	if (tTd(51, 4))
134358680Seric 		printf("unlockqueue(%s)\n", e->e_id);
134458680Seric 
134551920Seric 	/* if there is a lock file in the envelope, close it */
134651920Seric 	if (e->e_lockfp != NULL)
134758680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
134851920Seric 	e->e_lockfp = NULL;
134951920Seric 
135058728Seric 	/* don't create a queue id if we don't already have one */
135158728Seric 	if (e->e_id == NULL)
135258728Seric 		return;
135358728Seric 
135417468Seric 	/* remove the transcript */
135517468Seric # ifdef LOG
135658020Seric 	if (LogLevel > 87)
135717468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
135856795Seric # endif /* LOG */
135958680Seric 	if (!tTd(51, 104))
136017468Seric 		xunlink(queuename(e, 'x'));
136117468Seric 
136217468Seric }
136340973Sbostic /*
136454974Seric **  SETCTLUSER -- create a controlling address
136540973Sbostic **
136654974Seric **	Create a fake "address" given only a local login name; this is
136754974Seric **	used as a "controlling user" for future recipient addresses.
136840973Sbostic **
136940973Sbostic **	Parameters:
137054974Seric **		user -- the user name of the controlling user.
137140973Sbostic **
137240973Sbostic **	Returns:
137354974Seric **		An address descriptor for the controlling user.
137440973Sbostic **
137540973Sbostic **	Side Effects:
137640973Sbostic **		none.
137740973Sbostic */
137840973Sbostic 
137954974Seric ADDRESS *
138054974Seric setctluser(user)
138154974Seric 	char *user;
138240973Sbostic {
138354974Seric 	register ADDRESS *a;
138440973Sbostic 	struct passwd *pw;
138540973Sbostic 
138640973Sbostic 	/*
138754974Seric 	**  See if this clears our concept of controlling user.
138840973Sbostic 	*/
138940973Sbostic 
139054974Seric 	if (user == NULL || *user == '\0')
139158704Seric 		user = DefUser;
139240973Sbostic 
139340973Sbostic 	/*
139454974Seric 	**  Set up addr fields for controlling user.
139540973Sbostic 	*/
139640973Sbostic 
139754974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
139854974Seric 	bzero((char *) a, sizeof *a);
139954974Seric 	if ((pw = getpwnam(user)) != NULL)
140040973Sbostic 	{
140140973Sbostic 		a->q_home = newstr(pw->pw_dir);
140240973Sbostic 		a->q_uid = pw->pw_uid;
140340973Sbostic 		a->q_gid = pw->pw_gid;
140457642Seric 		a->q_user = newstr(user);
140540973Sbostic 	}
140640973Sbostic 	else
140740973Sbostic 	{
140840973Sbostic 		a->q_uid = DefUid;
140940973Sbostic 		a->q_gid = DefGid;
141057642Seric 		a->q_user = newstr(DefUser);
141140973Sbostic 	}
141240973Sbostic 
141358294Seric 	a->q_flags |= QGOODUID|QPRIMARY;	/* flag as a "ctladdr"  */
141456328Seric 	a->q_mailer = LocalMailer;
141554974Seric 	return a;
141640973Sbostic }
1417